blob: 07912269567596f045b0ce8d8df4f5f657d81d55 [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 Heo3e519032007-06-14 03:45:15 +090055 if (sd->s_type & SYSFS_KOBJ_LINK)
56 kobject_put(sd->s_elem.symlink.target_kobj);
Tejun Heo0c096b52007-06-14 03:45:15 +090057 if (sd->s_type & SYSFS_COPY_NAME)
58 kfree(sd->s_name);
Tejun Heofa7f9122007-06-14 03:45:13 +090059 kfree(sd->s_iattr);
Tejun Heo2b611bb2007-06-14 03:45:13 +090060 sysfs_free_ino(sd->s_ino);
Tejun Heofa7f9122007-06-14 03:45:13 +090061 kmem_cache_free(sysfs_dir_cachep, sd);
Tejun Heo13b30862007-06-14 03:45:14 +090062
63 sd = parent_sd;
64 if (sd && atomic_dec_and_test(&sd->s_count))
65 goto repeat;
Tejun Heofa7f9122007-06-14 03:45:13 +090066}
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
69{
70 struct sysfs_dirent * sd = dentry->d_fsdata;
71
72 if (sd) {
Tejun Heodd14cbc2007-06-11 14:04:01 +090073 /* sd->s_dentry is protected with sysfs_lock. This
74 * allows sysfs_drop_dentry() to dereference it.
75 */
76 spin_lock(&sysfs_lock);
77
78 /* The dentry might have been deleted or another
79 * lookup could have happened updating sd->s_dentry to
80 * point the new dentry. Ignore if it isn't pointing
81 * to this dentry.
82 */
83 if (sd->s_dentry == dentry)
84 sd->s_dentry = NULL;
85 spin_unlock(&sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 sysfs_put(sd);
87 }
88 iput(inode);
89}
90
91static struct dentry_operations sysfs_dentry_ops = {
92 .d_iput = sysfs_d_iput,
93};
94
Tejun Heo3e519032007-06-14 03:45:15 +090095struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Tejun Heo0c096b52007-06-14 03:45:15 +090097 char *dup_name = NULL;
98 struct sysfs_dirent *sd = NULL;
99
100 if (type & SYSFS_COPY_NAME) {
101 name = dup_name = kstrdup(name, GFP_KERNEL);
102 if (!name)
103 goto err_out;
104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800106 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 if (!sd)
Tejun Heo0c096b52007-06-14 03:45:15 +0900108 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Tejun Heo0c096b52007-06-14 03:45:15 +0900110 if (sysfs_alloc_ino(&sd->s_ino))
111 goto err_out;
Tejun Heo2b611bb2007-06-14 03:45:13 +0900112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 atomic_set(&sd->s_count, 1);
Juha Yrjöläeea3f892006-08-03 19:06:25 +0300114 atomic_set(&sd->s_event, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 INIT_LIST_HEAD(&sd->s_children);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700116 INIT_LIST_HEAD(&sd->s_sibling);
Tejun Heoa26cd722007-06-14 03:45:14 +0900117
Tejun Heo0c096b52007-06-14 03:45:15 +0900118 sd->s_name = name;
Tejun Heoa26cd722007-06-14 03:45:14 +0900119 sd->s_mode = mode;
120 sd->s_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 return sd;
Tejun Heo0c096b52007-06-14 03:45:15 +0900123
124 err_out:
125 kfree(dup_name);
126 kmem_cache_free(sysfs_dir_cachep, sd);
127 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Tejun Heoa26cd722007-06-14 03:45:14 +0900130void sysfs_attach_dirent(struct sysfs_dirent *sd,
131 struct sysfs_dirent *parent_sd, struct dentry *dentry)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700132{
Tejun Heoa26cd722007-06-14 03:45:14 +0900133 if (dentry) {
134 sd->s_dentry = dentry;
135 dentry->d_fsdata = sysfs_get(sd);
136 dentry->d_op = &sysfs_dentry_ops;
137 }
138
Tejun Heo13b30862007-06-14 03:45:14 +0900139 if (parent_sd) {
140 sd->s_parent = sysfs_get(parent_sd);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700141 list_add(&sd->s_sibling, &parent_sd->s_children);
Tejun Heo13b30862007-06-14 03:45:14 +0900142 }
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700143}
144
Martin Waitza5802902006-04-02 13:59:55 +0200145/*
Maneesh Sonic5168652006-03-09 19:40:14 +0530146 *
147 * Return -EEXIST if there is already a sysfs element with the same name for
148 * the same parent.
149 *
150 * called with parent inode's i_mutex held
151 */
152int sysfs_dirent_exist(struct sysfs_dirent *parent_sd,
153 const unsigned char *new)
154{
155 struct sysfs_dirent * sd;
156
157 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
Tejun Heo3e519032007-06-14 03:45:15 +0900158 if (sd->s_type) {
Tejun Heo0c096b52007-06-14 03:45:15 +0900159 if (strcmp(sd->s_name, new))
Maneesh Sonic5168652006-03-09 19:40:14 +0530160 continue;
161 else
162 return -EEXIST;
163 }
164 }
165
166 return 0;
167}
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169static int init_dir(struct inode * inode)
170{
171 inode->i_op = &sysfs_dir_inode_operations;
172 inode->i_fop = &sysfs_dir_operations;
173
174 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -0700175 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return 0;
177}
178
179static int init_file(struct inode * inode)
180{
181 inode->i_size = PAGE_SIZE;
182 inode->i_fop = &sysfs_file_operations;
183 return 0;
184}
185
186static int init_symlink(struct inode * inode)
187{
188 inode->i_op = &sysfs_symlink_inode_operations;
189 return 0;
190}
191
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900192static int create_dir(struct kobject *kobj, struct dentry *parent,
193 const char *name, struct dentry **p_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 int error;
196 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900197 struct dentry *dentry;
198 struct sysfs_dirent *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900200 mutex_lock(&parent->d_inode->i_mutex);
201
202 dentry = lookup_one_len(name, parent, strlen(name));
203 if (IS_ERR(dentry)) {
204 error = PTR_ERR(dentry);
205 goto out_unlock;
206 }
207
208 error = -EEXIST;
209 if (sysfs_dirent_exist(parent->d_fsdata, name))
210 goto out_dput;
211
Tejun Heoa26cd722007-06-14 03:45:14 +0900212 error = -ENOMEM;
Tejun Heo3e519032007-06-14 03:45:15 +0900213 sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
Tejun Heoa26cd722007-06-14 03:45:14 +0900214 if (!sd)
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900215 goto out_drop;
Tejun Heo3e519032007-06-14 03:45:15 +0900216 sd->s_elem.dir.kobj = kobj;
Tejun Heoa26cd722007-06-14 03:45:14 +0900217 sysfs_attach_dirent(sd, parent->d_fsdata, dentry);
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900218
219 error = sysfs_create(dentry, mode, init_dir);
220 if (error)
221 goto out_sput;
222
223 inc_nlink(parent->d_inode);
224 dentry->d_op = &sysfs_dentry_ops;
225 d_rehash(dentry);
226
227 *p_dentry = dentry;
228 error = 0;
229 goto out_dput;
230
231 out_sput:
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900232 list_del_init(&sd->s_sibling);
233 sysfs_put(sd);
234 out_drop:
235 d_drop(dentry);
236 out_dput:
237 dput(dentry);
238 out_unlock:
239 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return error;
241}
242
243
244int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d)
245{
246 return create_dir(k,k->dentry,n,d);
247}
248
249/**
250 * sysfs_create_dir - create a directory for an object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 * @kobj: object we're creating directory for.
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700252 * @shadow_parent: parent parent object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 */
254
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700255int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 struct dentry * dentry = NULL;
258 struct dentry * parent;
259 int error = 0;
260
261 BUG_ON(!kobj);
262
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700263 if (shadow_parent)
264 parent = shadow_parent;
265 else if (kobj->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 parent = kobj->parent->dentry;
267 else if (sysfs_mount && sysfs_mount->mnt_sb)
268 parent = sysfs_mount->mnt_sb->s_root;
269 else
270 return -EFAULT;
271
272 error = create_dir(kobj,parent,kobject_name(kobj),&dentry);
273 if (!error)
274 kobj->dentry = dentry;
275 return error;
276}
277
278/* attaches attribute's sysfs_dirent to the dentry corresponding to the
279 * attribute file
280 */
281static int sysfs_attach_attr(struct sysfs_dirent * sd, struct dentry * dentry)
282{
283 struct attribute * attr = NULL;
284 struct bin_attribute * bin_attr = NULL;
285 int (* init) (struct inode *) = NULL;
286 int error = 0;
287
288 if (sd->s_type & SYSFS_KOBJ_BIN_ATTR) {
Tejun Heo3e519032007-06-14 03:45:15 +0900289 bin_attr = sd->s_elem.bin_attr.bin_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 attr = &bin_attr->attr;
291 } else {
Tejun Heo3e519032007-06-14 03:45:15 +0900292 attr = sd->s_elem.attr.attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 init = init_file;
294 }
295
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530296 dentry->d_fsdata = sysfs_get(sd);
Tejun Heodd14cbc2007-06-11 14:04:01 +0900297 /* protect sd->s_dentry against sysfs_d_iput */
298 spin_lock(&sysfs_lock);
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530299 sd->s_dentry = dentry;
Tejun Heodd14cbc2007-06-11 14:04:01 +0900300 spin_unlock(&sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 error = sysfs_create(dentry, (attr->mode & S_IALLUGO) | S_IFREG, init);
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530302 if (error) {
303 sysfs_put(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return error;
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 if (bin_attr) {
308 dentry->d_inode->i_size = bin_attr->size;
309 dentry->d_inode->i_fop = &bin_fops;
310 }
311 dentry->d_op = &sysfs_dentry_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 d_rehash(dentry);
313
314 return 0;
315}
316
317static int sysfs_attach_link(struct sysfs_dirent * sd, struct dentry * dentry)
318{
319 int err = 0;
320
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530321 dentry->d_fsdata = sysfs_get(sd);
Tejun Heodd14cbc2007-06-11 14:04:01 +0900322 /* protect sd->s_dentry against sysfs_d_iput */
323 spin_lock(&sysfs_lock);
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530324 sd->s_dentry = dentry;
Tejun Heodd14cbc2007-06-11 14:04:01 +0900325 spin_unlock(&sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 err = sysfs_create(dentry, S_IFLNK|S_IRWXUGO, init_symlink);
327 if (!err) {
328 dentry->d_op = &sysfs_dentry_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 d_rehash(dentry);
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530330 } else
331 sysfs_put(sd);
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return err;
334}
335
336static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
337 struct nameidata *nd)
338{
339 struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
340 struct sysfs_dirent * sd;
341 int err = 0;
342
343 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
344 if (sd->s_type & SYSFS_NOT_PINNED) {
Tejun Heo0c096b52007-06-14 03:45:15 +0900345 if (strcmp(sd->s_name, dentry->d_name.name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 continue;
347
348 if (sd->s_type & SYSFS_KOBJ_LINK)
349 err = sysfs_attach_link(sd, dentry);
350 else
351 err = sysfs_attach_attr(sd, dentry);
352 break;
353 }
354 }
355
356 return ERR_PTR(err);
357}
358
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800359const struct inode_operations sysfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 .lookup = sysfs_lookup,
Maneesh Soni988d1862005-05-31 10:39:14 +0530361 .setattr = sysfs_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362};
363
364static void remove_dir(struct dentry * d)
365{
366 struct dentry * parent = dget(d->d_parent);
367 struct sysfs_dirent * sd;
368
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800369 mutex_lock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 d_delete(d);
371 sd = d->d_fsdata;
372 list_del_init(&sd->s_sibling);
373 sysfs_put(sd);
374 if (d->d_inode)
375 simple_rmdir(parent->d_inode,d);
376
377 pr_debug(" o %s removing done (%d)\n",d->d_name.name,
378 atomic_read(&d->d_count));
379
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800380 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 dput(parent);
382}
383
384void sysfs_remove_subdir(struct dentry * d)
385{
386 remove_dir(d);
387}
388
389
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700390static void __sysfs_remove_dir(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 struct sysfs_dirent * parent_sd;
393 struct sysfs_dirent * sd, * tmp;
394
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700395 dget(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (!dentry)
397 return;
398
399 pr_debug("sysfs %s: removing dir\n",dentry->d_name.name);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800400 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 parent_sd = dentry->d_fsdata;
402 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
Tejun Heo3e519032007-06-14 03:45:15 +0900403 if (!sd->s_type || !(sd->s_type & SYSFS_NOT_PINNED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 continue;
405 list_del_init(&sd->s_sibling);
406 sysfs_drop_dentry(sd, dentry);
407 sysfs_put(sd);
408 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800409 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 remove_dir(dentry);
412 /**
413 * Drop reference from dget() on entrance.
414 */
415 dput(dentry);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700416}
417
418/**
419 * sysfs_remove_dir - remove an object's directory.
420 * @kobj: object.
421 *
422 * The only thing special about this is that we remove any files in
423 * the directory before we remove the directory, and we've inlined
424 * what used to be sysfs_rmdir() below, instead of calling separately.
425 */
426
427void sysfs_remove_dir(struct kobject * kobj)
428{
429 __sysfs_remove_dir(kobj->dentry);
Greg Kroah-Hartman641e6f32006-03-16 15:44:26 -0800430 kobj->dentry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700433int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent,
434 const char *new_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Tejun Heo0c096b52007-06-14 03:45:15 +0900436 struct sysfs_dirent *sd = kobj->dentry->d_fsdata;
437 struct sysfs_dirent *parent_sd = new_parent->d_fsdata;
438 struct dentry *new_dentry;
439 char *dup_name;
Tejun Heo996b7372007-06-14 03:45:14 +0900440 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700442 if (!new_parent)
443 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 down_write(&sysfs_rename_sem);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700446 mutex_lock(&new_parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700448 new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
Tejun Heo996b7372007-06-14 03:45:14 +0900449 if (IS_ERR(new_dentry)) {
450 error = PTR_ERR(new_dentry);
451 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
Tejun Heo996b7372007-06-14 03:45:14 +0900453
454 /* By allowing two different directories with the same
455 * d_parent we allow this routine to move between different
456 * shadows of the same directory
457 */
458 error = -EINVAL;
459 if (kobj->dentry->d_parent->d_inode != new_parent->d_inode ||
460 new_dentry->d_parent->d_inode != new_parent->d_inode ||
461 new_dentry == kobj->dentry)
462 goto out_dput;
463
464 error = -EEXIST;
465 if (new_dentry->d_inode)
466 goto out_dput;
467
Tejun Heo0c096b52007-06-14 03:45:15 +0900468 /* rename kobject and sysfs_dirent */
469 error = -ENOMEM;
470 new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
471 if (!new_name)
Tejun Heo996b7372007-06-14 03:45:14 +0900472 goto out_drop;
473
Tejun Heo0c096b52007-06-14 03:45:15 +0900474 error = kobject_set_name(kobj, "%s", new_name);
475 if (error)
476 goto out_free;
477
478 kfree(sd->s_name);
479 sd->s_name = new_name;
480
481 /* move under the new parent */
Tejun Heo996b7372007-06-14 03:45:14 +0900482 d_add(new_dentry, NULL);
483 d_move(kobj->dentry, new_dentry);
484
Tejun Heo996b7372007-06-14 03:45:14 +0900485 list_del_init(&sd->s_sibling);
486 list_add(&sd->s_sibling, &parent_sd->s_children);
487
488 error = 0;
489 goto out_unlock;
490
Tejun Heo0c096b52007-06-14 03:45:15 +0900491 out_free:
492 kfree(dup_name);
Tejun Heo996b7372007-06-14 03:45:14 +0900493 out_drop:
494 d_drop(new_dentry);
495 out_dput:
496 dput(new_dentry);
497 out_unlock:
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700498 mutex_unlock(&new_parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 up_write(&sysfs_rename_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return error;
501}
502
Cornelia Huck8a824722006-11-20 17:07:51 +0100503int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
504{
505 struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
506 struct sysfs_dirent *new_parent_sd, *sd;
507 int error;
508
Cornelia Huck8a824722006-11-20 17:07:51 +0100509 old_parent_dentry = kobj->parent ?
510 kobj->parent->dentry : sysfs_mount->mnt_sb->s_root;
Cornelia Huckc744aea2007-01-08 20:16:44 +0100511 new_parent_dentry = new_parent ?
512 new_parent->dentry : sysfs_mount->mnt_sb->s_root;
Cornelia Huck8a824722006-11-20 17:07:51 +0100513
Mark Lord0de15172007-03-06 01:42:03 -0800514 if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
515 return 0; /* nothing to move */
Cornelia Huck8a824722006-11-20 17:07:51 +0100516again:
517 mutex_lock(&old_parent_dentry->d_inode->i_mutex);
518 if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
519 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
520 goto again;
521 }
522
523 new_parent_sd = new_parent_dentry->d_fsdata;
524 sd = kobj->dentry->d_fsdata;
525
526 new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
527 strlen(kobj->name));
528 if (IS_ERR(new_dentry)) {
529 error = PTR_ERR(new_dentry);
530 goto out;
531 } else
532 error = 0;
533 d_add(new_dentry, NULL);
534 d_move(kobj->dentry, new_dentry);
535 dput(new_dentry);
536
537 /* Remove from old parent's list and insert into new parent's list. */
538 list_del_init(&sd->s_sibling);
539 list_add(&sd->s_sibling, &new_parent_sd->s_children);
540
541out:
542 mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
543 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
544
545 return error;
546}
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548static int sysfs_dir_open(struct inode *inode, struct file *file)
549{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800550 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
Tejun Heoa26cd722007-06-14 03:45:14 +0900552 struct sysfs_dirent * sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800554 mutex_lock(&dentry->d_inode->i_mutex);
Tejun Heo3e519032007-06-14 03:45:15 +0900555 sd = sysfs_new_dirent("_DIR_", 0, 0);
Tejun Heoa26cd722007-06-14 03:45:14 +0900556 if (sd)
557 sysfs_attach_dirent(sd, parent_sd, NULL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800558 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Tejun Heoa26cd722007-06-14 03:45:14 +0900560 file->private_data = sd;
561 return sd ? 0 : -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
564static int sysfs_dir_close(struct inode *inode, struct file *file)
565{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800566 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 struct sysfs_dirent * cursor = file->private_data;
568
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800569 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 list_del_init(&cursor->s_sibling);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800571 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 release_sysfs_dirent(cursor);
574
575 return 0;
576}
577
578/* Relationship between s_mode and the DT_xxx types */
579static inline unsigned char dt_type(struct sysfs_dirent *sd)
580{
581 return (sd->s_mode >> 12) & 15;
582}
583
584static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
585{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800586 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
588 struct sysfs_dirent *cursor = filp->private_data;
589 struct list_head *p, *q = &cursor->s_sibling;
590 ino_t ino;
591 int i = filp->f_pos;
592
593 switch (i) {
594 case 0:
Eric Sandeendc351252007-06-11 14:02:45 +0900595 ino = parent_sd->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
597 break;
598 filp->f_pos++;
599 i++;
600 /* fallthrough */
601 case 1:
Tejun Heo13b30862007-06-14 03:45:14 +0900602 if (parent_sd->s_parent)
603 ino = parent_sd->s_parent->s_ino;
604 else
605 ino = parent_sd->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
607 break;
608 filp->f_pos++;
609 i++;
610 /* fallthrough */
611 default:
Akinobu Mita1bfba4e2006-06-26 00:24:40 -0700612 if (filp->f_pos == 2)
613 list_move(q, &parent_sd->s_children);
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
616 struct sysfs_dirent *next;
617 const char * name;
618 int len;
619
620 next = list_entry(p, struct sysfs_dirent,
621 s_sibling);
Tejun Heo3e519032007-06-14 03:45:15 +0900622 if (!next->s_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 continue;
624
Tejun Heo0c096b52007-06-14 03:45:15 +0900625 name = next->s_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 len = strlen(name);
Eric Sandeendc351252007-06-11 14:02:45 +0900627 ino = next->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 if (filldir(dirent, name, len, filp->f_pos, ino,
630 dt_type(next)) < 0)
631 return 0;
632
Akinobu Mita1bfba4e2006-06-26 00:24:40 -0700633 list_move(q, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 p = q;
635 filp->f_pos++;
636 }
637 }
638 return 0;
639}
640
641static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
642{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800643 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800645 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 switch (origin) {
647 case 1:
648 offset += file->f_pos;
649 case 0:
650 if (offset >= 0)
651 break;
652 default:
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800653 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return -EINVAL;
655 }
656 if (offset != file->f_pos) {
657 file->f_pos = offset;
658 if (file->f_pos >= 2) {
659 struct sysfs_dirent *sd = dentry->d_fsdata;
660 struct sysfs_dirent *cursor = file->private_data;
661 struct list_head *p;
662 loff_t n = file->f_pos - 2;
663
664 list_del(&cursor->s_sibling);
665 p = sd->s_children.next;
666 while (n && p != &sd->s_children) {
667 struct sysfs_dirent *next;
668 next = list_entry(p, struct sysfs_dirent,
669 s_sibling);
Tejun Heo3e519032007-06-14 03:45:15 +0900670 if (next->s_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 n--;
672 p = p->next;
673 }
674 list_add_tail(&cursor->s_sibling, p);
675 }
676 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800677 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return offset;
679}
680
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700681
682/**
683 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
684 * @kobj: object we're creating shadow of.
685 */
686
687int sysfs_make_shadowed_dir(struct kobject *kobj,
688 void * (*follow_link)(struct dentry *, struct nameidata *))
689{
690 struct inode *inode;
691 struct inode_operations *i_op;
692
693 inode = kobj->dentry->d_inode;
694 if (inode->i_op != &sysfs_dir_inode_operations)
695 return -EINVAL;
696
697 i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
698 if (!i_op)
699 return -ENOMEM;
700
701 memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
702 i_op->follow_link = follow_link;
703
704 /* Locking of inode->i_op?
705 * Since setting i_op is a single word write and they
706 * are atomic we should be ok here.
707 */
708 inode->i_op = i_op;
709 return 0;
710}
711
712/**
713 * sysfs_create_shadow_dir - create a shadow directory for an object.
714 * @kobj: object we're creating directory for.
715 *
716 * sysfs_make_shadowed_dir must already have been called on this
717 * directory.
718 */
719
720struct dentry *sysfs_create_shadow_dir(struct kobject *kobj)
721{
Tejun Heo13b30862007-06-14 03:45:14 +0900722 struct dentry *dir = kobj->dentry;
723 struct inode *inode = dir->d_inode;
724 struct dentry *parent = dir->d_parent;
725 struct sysfs_dirent *parent_sd = parent->d_fsdata;
726 struct dentry *shadow;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700727 struct sysfs_dirent *sd;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700728
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700729 shadow = ERR_PTR(-EINVAL);
730 if (!sysfs_is_shadowed_inode(inode))
731 goto out;
732
733 shadow = d_alloc(parent, &dir->d_name);
734 if (!shadow)
735 goto nomem;
736
Tejun Heo3e519032007-06-14 03:45:15 +0900737 sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700738 if (!sd)
739 goto nomem;
Tejun Heo3e519032007-06-14 03:45:15 +0900740 sd->s_elem.dir.kobj = kobj;
Tejun Heo13b30862007-06-14 03:45:14 +0900741 /* point to parent_sd but don't attach to it */
742 sd->s_parent = sysfs_get(parent_sd);
Tejun Heoa26cd722007-06-14 03:45:14 +0900743 sysfs_attach_dirent(sd, NULL, shadow);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700744
745 d_instantiate(shadow, igrab(inode));
746 inc_nlink(inode);
747 inc_nlink(parent->d_inode);
748 shadow->d_op = &sysfs_dentry_ops;
749
750 dget(shadow); /* Extra count - pin the dentry in core */
751
752out:
753 return shadow;
754nomem:
755 dput(shadow);
756 shadow = ERR_PTR(-ENOMEM);
757 goto out;
758}
759
760/**
761 * sysfs_remove_shadow_dir - remove an object's directory.
762 * @shadow: dentry of shadow directory
763 *
764 * The only thing special about this is that we remove any files in
765 * the directory before we remove the directory, and we've inlined
766 * what used to be sysfs_rmdir() below, instead of calling separately.
767 */
768
769void sysfs_remove_shadow_dir(struct dentry *shadow)
770{
771 __sysfs_remove_dir(shadow);
772}
773
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800774const struct file_operations sysfs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 .open = sysfs_dir_open,
776 .release = sysfs_dir_close,
777 .llseek = sysfs_dir_lseek,
778 .read = generic_read_dir,
779 .readdir = sysfs_readdir,
780};