symlink.c 3.4 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * fs/kernfs/symlink.c - kernfs symlink implementation
 *
 * Copyright (c) 2001-3 Patrick Mochel
 * Copyright (c) 2007 SUSE Linux Products GmbH
 * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
 *
 * This file is released under the GPLv2.
 */
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

#include <linux/fs.h>
#include <linux/gfp.h>
#include <linux/namei.h>

#include "kernfs-internal.h"

/**
 * kernfs_create_link - create a symlink
 * @parent: directory to create the symlink in
 * @name: name of the symlink
 * @target: target node for the symlink to point to
 *
 * Returns the created node on success, ERR_PTR() value on error.
 */
25 26 27
struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,
				       const char *name,
				       struct kernfs_node *target)
28
{
29
	struct kernfs_node *kn;
30
	struct kernfs_addrm_cxt acxt;
31 32
	int error;

33
	kn = sysfs_new_dirent(kernfs_root(parent), name, S_IFLNK|S_IRWXUGO,
34
			      SYSFS_KOBJ_LINK);
35
	if (!kn)
36 37
		return ERR_PTR(-ENOMEM);

38
	if (kernfs_ns_enabled(parent))
39 40
		kn->ns = target->ns;
	kn->symlink.target_kn = target;
41 42 43
	kernfs_get(target);	/* ref owned by symlink */

	sysfs_addrm_start(&acxt);
44
	error = sysfs_add_one(&acxt, kn, parent);
45 46 47
	sysfs_addrm_finish(&acxt);

	if (!error)
48
		return kn;
49

50
	kernfs_put(kn);
51 52 53
	return ERR_PTR(error);
}

54 55
static int sysfs_get_target_path(struct kernfs_node *parent,
				 struct kernfs_node *target, char *path)
56
{
57
	struct kernfs_node *base, *kn;
58 59 60 61
	char *s = path;
	int len = 0;

	/* go up to the root, stop at the base */
62
	base = parent;
63 64 65 66
	while (base->parent) {
		kn = target->parent;
		while (kn->parent && base != kn)
			kn = kn->parent;
67

68
		if (base == kn)
69 70 71 72
			break;

		strcpy(s, "../");
		s += 3;
73
		base = base->parent;
74 75 76
	}

	/* determine end of target string for reverse fillup */
77
	kn = target;
78 79 80
	while (kn->parent && kn != base) {
		len += strlen(kn->name) + 1;
		kn = kn->parent;
81 82 83 84 85 86 87 88 89 90
	}

	/* check limits */
	if (len < 2)
		return -EINVAL;
	len--;
	if ((s - path) + len > PATH_MAX)
		return -ENAMETOOLONG;

	/* reverse fillup of target string from target to base */
91
	kn = target;
92 93
	while (kn->parent && kn != base) {
		int slen = strlen(kn->name);
94 95

		len -= slen;
96
		strncpy(s + len, kn->name, slen);
97 98 99
		if (len)
			s[--len] = '/';

100
		kn = kn->parent;
101 102 103 104 105 106 107
	}

	return 0;
}

static int sysfs_getlink(struct dentry *dentry, char *path)
{
108
	struct kernfs_node *kn = dentry->d_fsdata;
109 110
	struct kernfs_node *parent = kn->parent;
	struct kernfs_node *target = kn->symlink.target_kn;
111 112 113
	int error;

	mutex_lock(&sysfs_mutex);
114
	error = sysfs_get_target_path(parent, target, path);
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
	mutex_unlock(&sysfs_mutex);

	return error;
}

static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
{
	int error = -ENOMEM;
	unsigned long page = get_zeroed_page(GFP_KERNEL);
	if (page) {
		error = sysfs_getlink(dentry, (char *) page);
		if (error < 0)
			free_page((unsigned long)page);
	}
	nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
	return NULL;
}

static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd,
			   void *cookie)
{
	char *page = nd_get_link(nd);
	if (!IS_ERR(page))
		free_page((unsigned long)page);
}

const struct inode_operations sysfs_symlink_inode_operations = {
	.setxattr	= sysfs_setxattr,
143 144 145
	.removexattr	= sysfs_removexattr,
	.getxattr	= sysfs_getxattr,
	.listxattr	= sysfs_listxattr,
146 147 148 149 150 151 152
	.readlink	= generic_readlink,
	.follow_link	= sysfs_follow_link,
	.put_link	= sysfs_put_link,
	.setattr	= sysfs_setattr,
	.getattr	= sysfs_getattr,
	.permission	= sysfs_permission,
};