[L2TP]: Changes to existing ppp and socket kernel headers for L2TP
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / sysfs / symlink.c
blob7b9c5bfde920fb1ea30af8f81d4313f84bf981ec
1 /*
2 * symlink.c - operations for sysfs symlinks.
3 */
5 #include <linux/fs.h>
6 #include <linux/mount.h>
7 #include <linux/module.h>
8 #include <linux/kobject.h>
9 #include <linux/namei.h>
10 #include <asm/semaphore.h>
12 #include "sysfs.h"
14 static int object_depth(struct kobject * kobj)
16 struct kobject * p = kobj;
17 int depth = 0;
18 do { depth++; } while ((p = p->parent));
19 return depth;
22 static int object_path_length(struct kobject * kobj)
24 struct kobject * p = kobj;
25 int length = 1;
26 do {
27 length += strlen(kobject_name(p)) + 1;
28 p = p->parent;
29 } while (p);
30 return length;
33 static void fill_object_path(struct kobject * kobj, char * buffer, int length)
35 struct kobject * p;
37 --length;
38 for (p = kobj; p; p = p->parent) {
39 int cur = strlen(kobject_name(p));
41 /* back up enough to print this bus id with '/' */
42 length -= cur;
43 strncpy(buffer + length,kobject_name(p),cur);
44 *(buffer + --length) = '/';
48 static int sysfs_add_link(struct dentry * parent, const char * name, struct kobject * target)
50 struct sysfs_dirent * parent_sd = parent->d_fsdata;
51 struct sysfs_symlink * sl;
52 int error = 0;
54 error = -ENOMEM;
55 sl = kmalloc(sizeof(*sl), GFP_KERNEL);
56 if (!sl)
57 goto exit1;
59 sl->link_name = kmalloc(strlen(name) + 1, GFP_KERNEL);
60 if (!sl->link_name)
61 goto exit2;
63 strcpy(sl->link_name, name);
64 sl->target_kobj = kobject_get(target);
66 error = sysfs_make_dirent(parent_sd, NULL, sl, S_IFLNK|S_IRWXUGO,
67 SYSFS_KOBJ_LINK);
68 if (!error)
69 return 0;
71 kobject_put(target);
72 kfree(sl->link_name);
73 exit2:
74 kfree(sl);
75 exit1:
76 return error;
79 /**
80 * sysfs_create_link - create symlink between two objects.
81 * @kobj: object whose directory we're creating the link in.
82 * @target: object we're pointing to.
83 * @name: name of the symlink.
85 int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * name)
87 struct dentry *dentry = NULL;
88 int error = -EEXIST;
90 BUG_ON(!name);
92 if (!kobj) {
93 if (sysfs_mount && sysfs_mount->mnt_sb)
94 dentry = sysfs_mount->mnt_sb->s_root;
95 } else
96 dentry = kobj->dentry;
98 if (!dentry)
99 return -EFAULT;
101 mutex_lock(&dentry->d_inode->i_mutex);
102 if (!sysfs_dirent_exist(dentry->d_fsdata, name))
103 error = sysfs_add_link(dentry, name, target);
104 mutex_unlock(&dentry->d_inode->i_mutex);
105 return error;
110 * sysfs_remove_link - remove symlink in object's directory.
111 * @kobj: object we're acting for.
112 * @name: name of the symlink to remove.
115 void sysfs_remove_link(struct kobject * kobj, const char * name)
117 sysfs_hash_and_remove(kobj->dentry,name);
120 static int sysfs_get_target_path(struct kobject * kobj, struct kobject * target,
121 char *path)
123 char * s;
124 int depth, size;
126 depth = object_depth(kobj);
127 size = object_path_length(target) + depth * 3 - 1;
128 if (size > PATH_MAX)
129 return -ENAMETOOLONG;
131 pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size);
133 for (s = path; depth--; s += 3)
134 strcpy(s,"../");
136 fill_object_path(target, path, size);
137 pr_debug("%s: path = '%s'\n", __FUNCTION__, path);
139 return 0;
142 static int sysfs_getlink(struct dentry *dentry, char * path)
144 struct kobject *kobj, *target_kobj;
145 int error = 0;
147 kobj = sysfs_get_kobject(dentry->d_parent);
148 if (!kobj)
149 return -EINVAL;
151 target_kobj = sysfs_get_kobject(dentry);
152 if (!target_kobj) {
153 kobject_put(kobj);
154 return -EINVAL;
157 down_read(&sysfs_rename_sem);
158 error = sysfs_get_target_path(kobj, target_kobj, path);
159 up_read(&sysfs_rename_sem);
161 kobject_put(kobj);
162 kobject_put(target_kobj);
163 return error;
167 static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
169 int error = -ENOMEM;
170 unsigned long page = get_zeroed_page(GFP_KERNEL);
171 if (page)
172 error = sysfs_getlink(dentry, (char *) page);
173 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
174 return NULL;
177 static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
179 char *page = nd_get_link(nd);
180 if (!IS_ERR(page))
181 free_page((unsigned long)page);
184 const struct inode_operations sysfs_symlink_inode_operations = {
185 .readlink = generic_readlink,
186 .follow_link = sysfs_follow_link,
187 .put_link = sysfs_put_link,
191 EXPORT_SYMBOL_GPL(sysfs_create_link);
192 EXPORT_SYMBOL_GPL(sysfs_remove_link);