blob: 6e8d6f54f08296146b81e7a0548b7bcccbd0b00a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * dir.c - Operations for sysfs directories.
3 */
4
5#undef DEBUG
6
7#include <linux/fs.h>
8#include <linux/mount.h>
9#include <linux/module.h>
10#include <linux/kobject.h>
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -070011#include <linux/namei.h>
Tejun Heo2b611bb2007-06-14 03:45:13 +090012#include <linux/idr.h>
Oliver Neukum94bebf42006-12-20 10:52:44 +010013#include <asm/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "sysfs.h"
15
16DECLARE_RWSEM(sysfs_rename_sem);
Tejun Heodd14cbc2007-06-11 14:04:01 +090017spinlock_t sysfs_lock = SPIN_LOCK_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Tejun Heo2b611bb2007-06-14 03:45:13 +090019static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED;
20static DEFINE_IDA(sysfs_ino_ida);
21
22int sysfs_alloc_ino(ino_t *pino)
23{
24 int ino, rc;
25
26 retry:
27 spin_lock(&sysfs_ino_lock);
28 rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
29 spin_unlock(&sysfs_ino_lock);
30
31 if (rc == -EAGAIN) {
32 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
33 goto retry;
34 rc = -ENOMEM;
35 }
36
37 *pino = ino;
38 return rc;
39}
40
41static void sysfs_free_ino(ino_t ino)
42{
43 spin_lock(&sysfs_ino_lock);
44 ida_remove(&sysfs_ino_ida, ino);
45 spin_unlock(&sysfs_ino_lock);
46}
47
Tejun Heofa7f9122007-06-14 03:45:13 +090048void release_sysfs_dirent(struct sysfs_dirent * sd)
49{
Tejun Heo13b30862007-06-14 03:45:14 +090050 struct sysfs_dirent *parent_sd;
51
52 repeat:
53 parent_sd = sd->s_parent;
54
Tejun Heofa7f9122007-06-14 03:45:13 +090055 if (sd->s_type & SYSFS_KOBJ_LINK) {
56 struct sysfs_symlink * sl = sd->s_element;
Tejun Heofa7f9122007-06-14 03:45:13 +090057 kobject_put(sl->target_kobj);
58 kfree(sl);
59 }
Tejun Heo0c096b52007-06-14 03:45:15 +090060 if (sd->s_type & SYSFS_COPY_NAME)
61 kfree(sd->s_name);
Tejun Heofa7f9122007-06-14 03:45:13 +090062 kfree(sd->s_iattr);
Tejun Heo2b611bb2007-06-14 03:45:13 +090063 sysfs_free_ino(sd->s_ino);
Tejun Heofa7f9122007-06-14 03:45:13 +090064 kmem_cache_free(sysfs_dir_cachep, sd);
Tejun Heo13b30862007-06-14 03:45:14 +090065
66 sd = parent_sd;
67 if (sd && atomic_dec_and_test(&sd->s_count))
68 goto repeat;
Tejun Heofa7f9122007-06-14 03:45:13 +090069}
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
72{
73 struct sysfs_dirent * sd = dentry->d_fsdata;
74
75 if (sd) {
Tejun Heodd14cbc2007-06-11 14:04:01 +090076 /* sd->s_dentry is protected with sysfs_lock. This
77 * allows sysfs_drop_dentry() to dereference it.
78 */
79 spin_lock(&sysfs_lock);
80
81 /* The dentry might have been deleted or another
82 * lookup could have happened updating sd->s_dentry to
83 * point the new dentry. Ignore if it isn't pointing
84 * to this dentry.
85 */
86 if (sd->s_dentry == dentry)
87 sd->s_dentry = NULL;
88 spin_unlock(&sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 sysfs_put(sd);
90 }
91 iput(inode);
92}
93
94static struct dentry_operations sysfs_dentry_ops = {
95 .d_iput = sysfs_d_iput,
96};
97
Tejun Heo0c096b52007-06-14 03:45:15 +090098struct sysfs_dirent *sysfs_new_dirent(const char *name, void *element,
99 umode_t mode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Tejun Heo0c096b52007-06-14 03:45:15 +0900101 char *dup_name = NULL;
102 struct sysfs_dirent *sd = NULL;
103
104 if (type & SYSFS_COPY_NAME) {
105 name = dup_name = kstrdup(name, GFP_KERNEL);
106 if (!name)
107 goto err_out;
108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800110 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (!sd)
Tejun Heo0c096b52007-06-14 03:45:15 +0900112 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Tejun Heo0c096b52007-06-14 03:45:15 +0900114 if (sysfs_alloc_ino(&sd->s_ino))
115 goto err_out;
Tejun Heo2b611bb2007-06-14 03:45:13 +0900116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 atomic_set(&sd->s_count, 1);
Juha Yrjöläeea3f892006-08-03 19:06:25 +0300118 atomic_set(&sd->s_event, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 INIT_LIST_HEAD(&sd->s_children);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700120 INIT_LIST_HEAD(&sd->s_sibling);
Tejun Heoa26cd722007-06-14 03:45:14 +0900121
Tejun Heo0c096b52007-06-14 03:45:15 +0900122 sd->s_name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 sd->s_element = element;
Tejun Heoa26cd722007-06-14 03:45:14 +0900124 sd->s_mode = mode;
125 sd->s_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 return sd;
Tejun Heo0c096b52007-06-14 03:45:15 +0900128
129 err_out:
130 kfree(dup_name);
131 kmem_cache_free(sysfs_dir_cachep, sd);
132 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Tejun Heoa26cd722007-06-14 03:45:14 +0900135void sysfs_attach_dirent(struct sysfs_dirent *sd,
136 struct sysfs_dirent *parent_sd, struct dentry *dentry)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700137{
Tejun Heoa26cd722007-06-14 03:45:14 +0900138 if (dentry) {
139 sd->s_dentry = dentry;
140 dentry->d_fsdata = sysfs_get(sd);
141 dentry->d_op = &sysfs_dentry_ops;
142 }
143
Tejun Heo13b30862007-06-14 03:45:14 +0900144 if (parent_sd) {
145 sd->s_parent = sysfs_get(parent_sd);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700146 list_add(&sd->s_sibling, &parent_sd->s_children);
Tejun Heo13b30862007-06-14 03:45:14 +0900147 }
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700148}
149
Martin Waitza5802902006-04-02 13:59:55 +0200150/*
Maneesh Sonic5168652006-03-09 19:40:14 +0530151 *
152 * Return -EEXIST if there is already a sysfs element with the same name for
153 * the same parent.
154 *
155 * called with parent inode's i_mutex held
156 */
157int sysfs_dirent_exist(struct sysfs_dirent *parent_sd,
158 const unsigned char *new)
159{
160 struct sysfs_dirent * sd;
161
162 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
163 if (sd->s_element) {
Tejun Heo0c096b52007-06-14 03:45:15 +0900164 if (strcmp(sd->s_name, new))
Maneesh Sonic5168652006-03-09 19:40:14 +0530165 continue;
166 else
167 return -EEXIST;
168 }
169 }
170
171 return 0;
172}
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174static int init_dir(struct inode * inode)
175{
176 inode->i_op = &sysfs_dir_inode_operations;
177 inode->i_fop = &sysfs_dir_operations;
178
179 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -0700180 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return 0;
182}
183
184static int init_file(struct inode * inode)
185{
186 inode->i_size = PAGE_SIZE;
187 inode->i_fop = &sysfs_file_operations;
188 return 0;
189}
190
191static int init_symlink(struct inode * inode)
192{
193 inode->i_op = &sysfs_symlink_inode_operations;
194 return 0;
195}
196
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900197static int create_dir(struct kobject *kobj, struct dentry *parent,
198 const char *name, struct dentry **p_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 int error;
201 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900202 struct dentry *dentry;
203 struct sysfs_dirent *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900205 mutex_lock(&parent->d_inode->i_mutex);
206
207 dentry = lookup_one_len(name, parent, strlen(name));
208 if (IS_ERR(dentry)) {
209 error = PTR_ERR(dentry);
210 goto out_unlock;
211 }
212
213 error = -EEXIST;
214 if (sysfs_dirent_exist(parent->d_fsdata, name))
215 goto out_dput;
216
Tejun Heoa26cd722007-06-14 03:45:14 +0900217 error = -ENOMEM;
Tejun Heo0c096b52007-06-14 03:45:15 +0900218 sd = sysfs_new_dirent(name, kobj, mode, SYSFS_DIR);
Tejun Heoa26cd722007-06-14 03:45:14 +0900219 if (!sd)
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900220 goto out_drop;
Tejun Heoa26cd722007-06-14 03:45:14 +0900221 sysfs_attach_dirent(sd, parent->d_fsdata, dentry);
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900222
223 error = sysfs_create(dentry, mode, init_dir);
224 if (error)
225 goto out_sput;
226
227 inc_nlink(parent->d_inode);
228 dentry->d_op = &sysfs_dentry_ops;
229 d_rehash(dentry);
230
231 *p_dentry = dentry;
232 error = 0;
233 goto out_dput;
234
235 out_sput:
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900236 list_del_init(&sd->s_sibling);
237 sysfs_put(sd);
238 out_drop:
239 d_drop(dentry);
240 out_dput:
241 dput(dentry);
242 out_unlock:
243 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return error;
245}
246
247
248int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d)
249{
250 return create_dir(k,k->dentry,n,d);
251}
252
253/**
254 * sysfs_create_dir - create a directory for an object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 * @kobj: object we're creating directory for.
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700256 * @shadow_parent: parent parent object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 */
258
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700259int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 struct dentry * dentry = NULL;
262 struct dentry * parent;
263 int error = 0;
264
265 BUG_ON(!kobj);
266
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700267 if (shadow_parent)
268 parent = shadow_parent;
269 else if (kobj->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 parent = kobj->parent->dentry;
271 else if (sysfs_mount && sysfs_mount->mnt_sb)
272 parent = sysfs_mount->mnt_sb->s_root;
273 else
274 return -EFAULT;
275
276 error = create_dir(kobj,parent,kobject_name(kobj),&dentry);
277 if (!error)
278 kobj->dentry = dentry;
279 return error;
280}
281
282/* attaches attribute's sysfs_dirent to the dentry corresponding to the
283 * attribute file
284 */
285static int sysfs_attach_attr(struct sysfs_dirent * sd, struct dentry * dentry)
286{
287 struct attribute * attr = NULL;
288 struct bin_attribute * bin_attr = NULL;
289 int (* init) (struct inode *) = NULL;
290 int error = 0;
291
292 if (sd->s_type & SYSFS_KOBJ_BIN_ATTR) {
293 bin_attr = sd->s_element;
294 attr = &bin_attr->attr;
295 } else {
296 attr = sd->s_element;
297 init = init_file;
298 }
299
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530300 dentry->d_fsdata = sysfs_get(sd);
Tejun Heodd14cbc2007-06-11 14:04:01 +0900301 /* protect sd->s_dentry against sysfs_d_iput */
302 spin_lock(&sysfs_lock);
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530303 sd->s_dentry = dentry;
Tejun Heodd14cbc2007-06-11 14:04:01 +0900304 spin_unlock(&sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 error = sysfs_create(dentry, (attr->mode & S_IALLUGO) | S_IFREG, init);
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530306 if (error) {
307 sysfs_put(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return error;
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 if (bin_attr) {
312 dentry->d_inode->i_size = bin_attr->size;
313 dentry->d_inode->i_fop = &bin_fops;
314 }
315 dentry->d_op = &sysfs_dentry_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 d_rehash(dentry);
317
318 return 0;
319}
320
321static int sysfs_attach_link(struct sysfs_dirent * sd, struct dentry * dentry)
322{
323 int err = 0;
324
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530325 dentry->d_fsdata = sysfs_get(sd);
Tejun Heodd14cbc2007-06-11 14:04:01 +0900326 /* protect sd->s_dentry against sysfs_d_iput */
327 spin_lock(&sysfs_lock);
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530328 sd->s_dentry = dentry;
Tejun Heodd14cbc2007-06-11 14:04:01 +0900329 spin_unlock(&sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 err = sysfs_create(dentry, S_IFLNK|S_IRWXUGO, init_symlink);
331 if (!err) {
332 dentry->d_op = &sysfs_dentry_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 d_rehash(dentry);
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530334 } else
335 sysfs_put(sd);
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return err;
338}
339
340static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
341 struct nameidata *nd)
342{
343 struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
344 struct sysfs_dirent * sd;
345 int err = 0;
346
347 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
348 if (sd->s_type & SYSFS_NOT_PINNED) {
Tejun Heo0c096b52007-06-14 03:45:15 +0900349 if (strcmp(sd->s_name, dentry->d_name.name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 continue;
351
352 if (sd->s_type & SYSFS_KOBJ_LINK)
353 err = sysfs_attach_link(sd, dentry);
354 else
355 err = sysfs_attach_attr(sd, dentry);
356 break;
357 }
358 }
359
360 return ERR_PTR(err);
361}
362
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800363const struct inode_operations sysfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 .lookup = sysfs_lookup,
Maneesh Soni988d1862005-05-31 10:39:14 +0530365 .setattr = sysfs_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366};
367
368static void remove_dir(struct dentry * d)
369{
370 struct dentry * parent = dget(d->d_parent);
371 struct sysfs_dirent * sd;
372
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800373 mutex_lock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 d_delete(d);
375 sd = d->d_fsdata;
376 list_del_init(&sd->s_sibling);
377 sysfs_put(sd);
378 if (d->d_inode)
379 simple_rmdir(parent->d_inode,d);
380
381 pr_debug(" o %s removing done (%d)\n",d->d_name.name,
382 atomic_read(&d->d_count));
383
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800384 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 dput(parent);
386}
387
388void sysfs_remove_subdir(struct dentry * d)
389{
390 remove_dir(d);
391}
392
393
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700394static void __sysfs_remove_dir(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 struct sysfs_dirent * parent_sd;
397 struct sysfs_dirent * sd, * tmp;
398
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700399 dget(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (!dentry)
401 return;
402
403 pr_debug("sysfs %s: removing dir\n",dentry->d_name.name);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800404 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 parent_sd = dentry->d_fsdata;
406 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
407 if (!sd->s_element || !(sd->s_type & SYSFS_NOT_PINNED))
408 continue;
409 list_del_init(&sd->s_sibling);
410 sysfs_drop_dentry(sd, dentry);
411 sysfs_put(sd);
412 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800413 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 remove_dir(dentry);
416 /**
417 * Drop reference from dget() on entrance.
418 */
419 dput(dentry);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700420}
421
422/**
423 * sysfs_remove_dir - remove an object's directory.
424 * @kobj: object.
425 *
426 * The only thing special about this is that we remove any files in
427 * the directory before we remove the directory, and we've inlined
428 * what used to be sysfs_rmdir() below, instead of calling separately.
429 */
430
431void sysfs_remove_dir(struct kobject * kobj)
432{
433 __sysfs_remove_dir(kobj->dentry);
Greg Kroah-Hartman641e6f32006-03-16 15:44:26 -0800434 kobj->dentry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700437int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent,
438 const char *new_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Tejun Heo0c096b52007-06-14 03:45:15 +0900440 struct sysfs_dirent *sd = kobj->dentry->d_fsdata;
441 struct sysfs_dirent *parent_sd = new_parent->d_fsdata;
442 struct dentry *new_dentry;
443 char *dup_name;
Tejun Heo996b7372007-06-14 03:45:14 +0900444 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700446 if (!new_parent)
447 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 down_write(&sysfs_rename_sem);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700450 mutex_lock(&new_parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700452 new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
Tejun Heo996b7372007-06-14 03:45:14 +0900453 if (IS_ERR(new_dentry)) {
454 error = PTR_ERR(new_dentry);
455 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
Tejun Heo996b7372007-06-14 03:45:14 +0900457
458 /* By allowing two different directories with the same
459 * d_parent we allow this routine to move between different
460 * shadows of the same directory
461 */
462 error = -EINVAL;
463 if (kobj->dentry->d_parent->d_inode != new_parent->d_inode ||
464 new_dentry->d_parent->d_inode != new_parent->d_inode ||
465 new_dentry == kobj->dentry)
466 goto out_dput;
467
468 error = -EEXIST;
469 if (new_dentry->d_inode)
470 goto out_dput;
471
Tejun Heo0c096b52007-06-14 03:45:15 +0900472 /* rename kobject and sysfs_dirent */
473 error = -ENOMEM;
474 new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
475 if (!new_name)
Tejun Heo996b7372007-06-14 03:45:14 +0900476 goto out_drop;
477
Tejun Heo0c096b52007-06-14 03:45:15 +0900478 error = kobject_set_name(kobj, "%s", new_name);
479 if (error)
480 goto out_free;
481
482 kfree(sd->s_name);
483 sd->s_name = new_name;
484
485 /* move under the new parent */
Tejun Heo996b7372007-06-14 03:45:14 +0900486 d_add(new_dentry, NULL);
487 d_move(kobj->dentry, new_dentry);
488
Tejun Heo996b7372007-06-14 03:45:14 +0900489 list_del_init(&sd->s_sibling);
490 list_add(&sd->s_sibling, &parent_sd->s_children);
491
492 error = 0;
493 goto out_unlock;
494
Tejun Heo0c096b52007-06-14 03:45:15 +0900495 out_free:
496 kfree(dup_name);
Tejun Heo996b7372007-06-14 03:45:14 +0900497 out_drop:
498 d_drop(new_dentry);
499 out_dput:
500 dput(new_dentry);
501 out_unlock:
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700502 mutex_unlock(&new_parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 up_write(&sysfs_rename_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return error;
505}
506
Cornelia Huck8a824722006-11-20 17:07:51 +0100507int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
508{
509 struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
510 struct sysfs_dirent *new_parent_sd, *sd;
511 int error;
512
Cornelia Huck8a824722006-11-20 17:07:51 +0100513 old_parent_dentry = kobj->parent ?
514 kobj->parent->dentry : sysfs_mount->mnt_sb->s_root;
Cornelia Huckc744aea2007-01-08 20:16:44 +0100515 new_parent_dentry = new_parent ?
516 new_parent->dentry : sysfs_mount->mnt_sb->s_root;
Cornelia Huck8a824722006-11-20 17:07:51 +0100517
Mark Lord0de15172007-03-06 01:42:03 -0800518 if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
519 return 0; /* nothing to move */
Cornelia Huck8a824722006-11-20 17:07:51 +0100520again:
521 mutex_lock(&old_parent_dentry->d_inode->i_mutex);
522 if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
523 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
524 goto again;
525 }
526
527 new_parent_sd = new_parent_dentry->d_fsdata;
528 sd = kobj->dentry->d_fsdata;
529
530 new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
531 strlen(kobj->name));
532 if (IS_ERR(new_dentry)) {
533 error = PTR_ERR(new_dentry);
534 goto out;
535 } else
536 error = 0;
537 d_add(new_dentry, NULL);
538 d_move(kobj->dentry, new_dentry);
539 dput(new_dentry);
540
541 /* Remove from old parent's list and insert into new parent's list. */
542 list_del_init(&sd->s_sibling);
543 list_add(&sd->s_sibling, &new_parent_sd->s_children);
544
545out:
546 mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
547 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
548
549 return error;
550}
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552static int sysfs_dir_open(struct inode *inode, struct file *file)
553{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800554 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
Tejun Heoa26cd722007-06-14 03:45:14 +0900556 struct sysfs_dirent * sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800558 mutex_lock(&dentry->d_inode->i_mutex);
Tejun Heo0c096b52007-06-14 03:45:15 +0900559 sd = sysfs_new_dirent("_DIR_", NULL, 0, 0);
Tejun Heoa26cd722007-06-14 03:45:14 +0900560 if (sd)
561 sysfs_attach_dirent(sd, parent_sd, NULL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800562 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Tejun Heoa26cd722007-06-14 03:45:14 +0900564 file->private_data = sd;
565 return sd ? 0 : -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
568static int sysfs_dir_close(struct inode *inode, struct file *file)
569{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800570 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 struct sysfs_dirent * cursor = file->private_data;
572
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800573 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 list_del_init(&cursor->s_sibling);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800575 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 release_sysfs_dirent(cursor);
578
579 return 0;
580}
581
582/* Relationship between s_mode and the DT_xxx types */
583static inline unsigned char dt_type(struct sysfs_dirent *sd)
584{
585 return (sd->s_mode >> 12) & 15;
586}
587
588static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
589{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800590 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
592 struct sysfs_dirent *cursor = filp->private_data;
593 struct list_head *p, *q = &cursor->s_sibling;
594 ino_t ino;
595 int i = filp->f_pos;
596
597 switch (i) {
598 case 0:
Eric Sandeendc351252007-06-11 14:02:45 +0900599 ino = parent_sd->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
601 break;
602 filp->f_pos++;
603 i++;
604 /* fallthrough */
605 case 1:
Tejun Heo13b30862007-06-14 03:45:14 +0900606 if (parent_sd->s_parent)
607 ino = parent_sd->s_parent->s_ino;
608 else
609 ino = parent_sd->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
611 break;
612 filp->f_pos++;
613 i++;
614 /* fallthrough */
615 default:
Akinobu Mita1bfba4e2006-06-26 00:24:40 -0700616 if (filp->f_pos == 2)
617 list_move(q, &parent_sd->s_children);
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
620 struct sysfs_dirent *next;
621 const char * name;
622 int len;
623
624 next = list_entry(p, struct sysfs_dirent,
625 s_sibling);
626 if (!next->s_element)
627 continue;
628
Tejun Heo0c096b52007-06-14 03:45:15 +0900629 name = next->s_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 len = strlen(name);
Eric Sandeendc351252007-06-11 14:02:45 +0900631 ino = next->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 if (filldir(dirent, name, len, filp->f_pos, ino,
634 dt_type(next)) < 0)
635 return 0;
636
Akinobu Mita1bfba4e2006-06-26 00:24:40 -0700637 list_move(q, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 p = q;
639 filp->f_pos++;
640 }
641 }
642 return 0;
643}
644
645static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
646{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800647 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800649 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 switch (origin) {
651 case 1:
652 offset += file->f_pos;
653 case 0:
654 if (offset >= 0)
655 break;
656 default:
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800657 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return -EINVAL;
659 }
660 if (offset != file->f_pos) {
661 file->f_pos = offset;
662 if (file->f_pos >= 2) {
663 struct sysfs_dirent *sd = dentry->d_fsdata;
664 struct sysfs_dirent *cursor = file->private_data;
665 struct list_head *p;
666 loff_t n = file->f_pos - 2;
667
668 list_del(&cursor->s_sibling);
669 p = sd->s_children.next;
670 while (n && p != &sd->s_children) {
671 struct sysfs_dirent *next;
672 next = list_entry(p, struct sysfs_dirent,
673 s_sibling);
674 if (next->s_element)
675 n--;
676 p = p->next;
677 }
678 list_add_tail(&cursor->s_sibling, p);
679 }
680 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800681 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return offset;
683}
684
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700685
686/**
687 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
688 * @kobj: object we're creating shadow of.
689 */
690
691int sysfs_make_shadowed_dir(struct kobject *kobj,
692 void * (*follow_link)(struct dentry *, struct nameidata *))
693{
694 struct inode *inode;
695 struct inode_operations *i_op;
696
697 inode = kobj->dentry->d_inode;
698 if (inode->i_op != &sysfs_dir_inode_operations)
699 return -EINVAL;
700
701 i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
702 if (!i_op)
703 return -ENOMEM;
704
705 memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
706 i_op->follow_link = follow_link;
707
708 /* Locking of inode->i_op?
709 * Since setting i_op is a single word write and they
710 * are atomic we should be ok here.
711 */
712 inode->i_op = i_op;
713 return 0;
714}
715
716/**
717 * sysfs_create_shadow_dir - create a shadow directory for an object.
718 * @kobj: object we're creating directory for.
719 *
720 * sysfs_make_shadowed_dir must already have been called on this
721 * directory.
722 */
723
724struct dentry *sysfs_create_shadow_dir(struct kobject *kobj)
725{
Tejun Heo13b30862007-06-14 03:45:14 +0900726 struct dentry *dir = kobj->dentry;
727 struct inode *inode = dir->d_inode;
728 struct dentry *parent = dir->d_parent;
729 struct sysfs_dirent *parent_sd = parent->d_fsdata;
730 struct dentry *shadow;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700731 struct sysfs_dirent *sd;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700732
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700733 shadow = ERR_PTR(-EINVAL);
734 if (!sysfs_is_shadowed_inode(inode))
735 goto out;
736
737 shadow = d_alloc(parent, &dir->d_name);
738 if (!shadow)
739 goto nomem;
740
Tejun Heo0c096b52007-06-14 03:45:15 +0900741 sd = sysfs_new_dirent("_SHADOW_", kobj, inode->i_mode, SYSFS_DIR);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700742 if (!sd)
743 goto nomem;
Tejun Heo13b30862007-06-14 03:45:14 +0900744 /* point to parent_sd but don't attach to it */
745 sd->s_parent = sysfs_get(parent_sd);
Tejun Heoa26cd722007-06-14 03:45:14 +0900746 sysfs_attach_dirent(sd, NULL, shadow);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700747
748 d_instantiate(shadow, igrab(inode));
749 inc_nlink(inode);
750 inc_nlink(parent->d_inode);
751 shadow->d_op = &sysfs_dentry_ops;
752
753 dget(shadow); /* Extra count - pin the dentry in core */
754
755out:
756 return shadow;
757nomem:
758 dput(shadow);
759 shadow = ERR_PTR(-ENOMEM);
760 goto out;
761}
762
763/**
764 * sysfs_remove_shadow_dir - remove an object's directory.
765 * @shadow: dentry of shadow directory
766 *
767 * The only thing special about this is that we remove any files in
768 * the directory before we remove the directory, and we've inlined
769 * what used to be sysfs_rmdir() below, instead of calling separately.
770 */
771
772void sysfs_remove_shadow_dir(struct dentry *shadow)
773{
774 __sysfs_remove_dir(shadow);
775}
776
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800777const struct file_operations sysfs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 .open = sysfs_dir_open,
779 .release = sysfs_dir_close,
780 .llseek = sysfs_dir_lseek,
781 .read = generic_read_dir,
782 .readdir = sysfs_readdir,
783};