blob: 2a94dc36d166d3b64b049f3e3ba49e7f7571ddf4 [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;
Tejun Heoaecdced2007-06-14 03:45:15 +090018spinlock_t kobj_sysfs_assoc_lock = SPIN_LOCK_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Tejun Heo2b611bb2007-06-14 03:45:13 +090020static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED;
21static DEFINE_IDA(sysfs_ino_ida);
22
23int sysfs_alloc_ino(ino_t *pino)
24{
25 int ino, rc;
26
27 retry:
28 spin_lock(&sysfs_ino_lock);
29 rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
30 spin_unlock(&sysfs_ino_lock);
31
32 if (rc == -EAGAIN) {
33 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
34 goto retry;
35 rc = -ENOMEM;
36 }
37
38 *pino = ino;
39 return rc;
40}
41
42static void sysfs_free_ino(ino_t ino)
43{
44 spin_lock(&sysfs_ino_lock);
45 ida_remove(&sysfs_ino_ida, ino);
46 spin_unlock(&sysfs_ino_lock);
47}
48
Tejun Heofa7f9122007-06-14 03:45:13 +090049void release_sysfs_dirent(struct sysfs_dirent * sd)
50{
Tejun Heo13b30862007-06-14 03:45:14 +090051 struct sysfs_dirent *parent_sd;
52
53 repeat:
54 parent_sd = sd->s_parent;
55
Tejun Heo3e519032007-06-14 03:45:15 +090056 if (sd->s_type & SYSFS_KOBJ_LINK)
Tejun Heo2b29ac22007-06-14 03:45:15 +090057 sysfs_put(sd->s_elem.symlink.target_sd);
Tejun Heo0c096b52007-06-14 03:45:15 +090058 if (sd->s_type & SYSFS_COPY_NAME)
59 kfree(sd->s_name);
Tejun Heofa7f9122007-06-14 03:45:13 +090060 kfree(sd->s_iattr);
Tejun Heo2b611bb2007-06-14 03:45:13 +090061 sysfs_free_ino(sd->s_ino);
Tejun Heofa7f9122007-06-14 03:45:13 +090062 kmem_cache_free(sysfs_dir_cachep, sd);
Tejun Heo13b30862007-06-14 03:45:14 +090063
64 sd = parent_sd;
65 if (sd && atomic_dec_and_test(&sd->s_count))
66 goto repeat;
Tejun Heofa7f9122007-06-14 03:45:13 +090067}
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
70{
71 struct sysfs_dirent * sd = dentry->d_fsdata;
72
73 if (sd) {
Tejun Heodd14cbc2007-06-11 14:04:01 +090074 /* sd->s_dentry is protected with sysfs_lock. This
75 * allows sysfs_drop_dentry() to dereference it.
76 */
77 spin_lock(&sysfs_lock);
78
79 /* The dentry might have been deleted or another
80 * lookup could have happened updating sd->s_dentry to
81 * point the new dentry. Ignore if it isn't pointing
82 * to this dentry.
83 */
84 if (sd->s_dentry == dentry)
85 sd->s_dentry = NULL;
86 spin_unlock(&sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 sysfs_put(sd);
88 }
89 iput(inode);
90}
91
92static struct dentry_operations sysfs_dentry_ops = {
93 .d_iput = sysfs_d_iput,
94};
95
Tejun Heo3e519032007-06-14 03:45:15 +090096struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Tejun Heo0c096b52007-06-14 03:45:15 +090098 char *dup_name = NULL;
99 struct sysfs_dirent *sd = NULL;
100
101 if (type & SYSFS_COPY_NAME) {
102 name = dup_name = kstrdup(name, GFP_KERNEL);
103 if (!name)
104 goto err_out;
105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800107 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 if (!sd)
Tejun Heo0c096b52007-06-14 03:45:15 +0900109 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Tejun Heo0c096b52007-06-14 03:45:15 +0900111 if (sysfs_alloc_ino(&sd->s_ino))
112 goto err_out;
Tejun Heo2b611bb2007-06-14 03:45:13 +0900113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 atomic_set(&sd->s_count, 1);
Juha Yrjöläeea3f892006-08-03 19:06:25 +0300115 atomic_set(&sd->s_event, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 INIT_LIST_HEAD(&sd->s_children);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700117 INIT_LIST_HEAD(&sd->s_sibling);
Tejun Heoa26cd722007-06-14 03:45:14 +0900118
Tejun Heo0c096b52007-06-14 03:45:15 +0900119 sd->s_name = name;
Tejun Heoa26cd722007-06-14 03:45:14 +0900120 sd->s_mode = mode;
121 sd->s_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 return sd;
Tejun Heo0c096b52007-06-14 03:45:15 +0900124
125 err_out:
126 kfree(dup_name);
127 kmem_cache_free(sysfs_dir_cachep, sd);
128 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Tejun Heoa26cd722007-06-14 03:45:14 +0900131void sysfs_attach_dirent(struct sysfs_dirent *sd,
132 struct sysfs_dirent *parent_sd, struct dentry *dentry)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700133{
Tejun Heoa26cd722007-06-14 03:45:14 +0900134 if (dentry) {
135 sd->s_dentry = dentry;
136 dentry->d_fsdata = sysfs_get(sd);
137 dentry->d_op = &sysfs_dentry_ops;
138 }
139
Tejun Heo13b30862007-06-14 03:45:14 +0900140 if (parent_sd) {
141 sd->s_parent = sysfs_get(parent_sd);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700142 list_add(&sd->s_sibling, &parent_sd->s_children);
Tejun Heo13b30862007-06-14 03:45:14 +0900143 }
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700144}
145
Martin Waitza5802902006-04-02 13:59:55 +0200146/*
Maneesh Sonic5168652006-03-09 19:40:14 +0530147 *
148 * Return -EEXIST if there is already a sysfs element with the same name for
149 * the same parent.
150 *
151 * called with parent inode's i_mutex held
152 */
153int sysfs_dirent_exist(struct sysfs_dirent *parent_sd,
154 const unsigned char *new)
155{
156 struct sysfs_dirent * sd;
157
158 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
Tejun Heo3e519032007-06-14 03:45:15 +0900159 if (sd->s_type) {
Tejun Heo0c096b52007-06-14 03:45:15 +0900160 if (strcmp(sd->s_name, new))
Maneesh Sonic5168652006-03-09 19:40:14 +0530161 continue;
162 else
163 return -EEXIST;
164 }
165 }
166
167 return 0;
168}
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static int init_dir(struct inode * inode)
171{
172 inode->i_op = &sysfs_dir_inode_operations;
173 inode->i_fop = &sysfs_dir_operations;
174
175 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -0700176 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return 0;
178}
179
180static int init_file(struct inode * inode)
181{
182 inode->i_size = PAGE_SIZE;
183 inode->i_fop = &sysfs_file_operations;
184 return 0;
185}
186
187static int init_symlink(struct inode * inode)
188{
189 inode->i_op = &sysfs_symlink_inode_operations;
190 return 0;
191}
192
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900193static int create_dir(struct kobject *kobj, struct dentry *parent,
194 const char *name, struct dentry **p_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 int error;
197 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900198 struct dentry *dentry;
199 struct sysfs_dirent *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900201 mutex_lock(&parent->d_inode->i_mutex);
202
203 dentry = lookup_one_len(name, parent, strlen(name));
204 if (IS_ERR(dentry)) {
205 error = PTR_ERR(dentry);
206 goto out_unlock;
207 }
208
209 error = -EEXIST;
210 if (sysfs_dirent_exist(parent->d_fsdata, name))
211 goto out_dput;
212
Tejun Heoa26cd722007-06-14 03:45:14 +0900213 error = -ENOMEM;
Tejun Heo3e519032007-06-14 03:45:15 +0900214 sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
Tejun Heoa26cd722007-06-14 03:45:14 +0900215 if (!sd)
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900216 goto out_drop;
Tejun Heo3e519032007-06-14 03:45:15 +0900217 sd->s_elem.dir.kobj = kobj;
Tejun Heoa26cd722007-06-14 03:45:14 +0900218 sysfs_attach_dirent(sd, parent->d_fsdata, dentry);
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900219
220 error = sysfs_create(dentry, mode, init_dir);
221 if (error)
222 goto out_sput;
223
224 inc_nlink(parent->d_inode);
225 dentry->d_op = &sysfs_dentry_ops;
226 d_rehash(dentry);
227
228 *p_dentry = dentry;
229 error = 0;
230 goto out_dput;
231
232 out_sput:
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900233 list_del_init(&sd->s_sibling);
234 sysfs_put(sd);
235 out_drop:
236 d_drop(dentry);
237 out_dput:
238 dput(dentry);
239 out_unlock:
240 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return error;
242}
243
244
245int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d)
246{
247 return create_dir(k,k->dentry,n,d);
248}
249
250/**
251 * sysfs_create_dir - create a directory for an object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 * @kobj: object we're creating directory for.
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700253 * @shadow_parent: parent parent object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 */
255
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700256int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 struct dentry * dentry = NULL;
259 struct dentry * parent;
260 int error = 0;
261
262 BUG_ON(!kobj);
263
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700264 if (shadow_parent)
265 parent = shadow_parent;
266 else if (kobj->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 parent = kobj->parent->dentry;
268 else if (sysfs_mount && sysfs_mount->mnt_sb)
269 parent = sysfs_mount->mnt_sb->s_root;
270 else
271 return -EFAULT;
272
273 error = create_dir(kobj,parent,kobject_name(kobj),&dentry);
274 if (!error)
275 kobj->dentry = dentry;
276 return error;
277}
278
279/* attaches attribute's sysfs_dirent to the dentry corresponding to the
280 * attribute file
281 */
282static int sysfs_attach_attr(struct sysfs_dirent * sd, struct dentry * dentry)
283{
284 struct attribute * attr = NULL;
285 struct bin_attribute * bin_attr = NULL;
286 int (* init) (struct inode *) = NULL;
287 int error = 0;
288
289 if (sd->s_type & SYSFS_KOBJ_BIN_ATTR) {
Tejun Heo3e519032007-06-14 03:45:15 +0900290 bin_attr = sd->s_elem.bin_attr.bin_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 attr = &bin_attr->attr;
292 } else {
Tejun Heo3e519032007-06-14 03:45:15 +0900293 attr = sd->s_elem.attr.attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 init = init_file;
295 }
296
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530297 dentry->d_fsdata = sysfs_get(sd);
Tejun Heodd14cbc2007-06-11 14:04:01 +0900298 /* protect sd->s_dentry against sysfs_d_iput */
299 spin_lock(&sysfs_lock);
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530300 sd->s_dentry = dentry;
Tejun Heodd14cbc2007-06-11 14:04:01 +0900301 spin_unlock(&sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 error = sysfs_create(dentry, (attr->mode & S_IALLUGO) | S_IFREG, init);
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530303 if (error) {
304 sysfs_put(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return error;
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 if (bin_attr) {
309 dentry->d_inode->i_size = bin_attr->size;
310 dentry->d_inode->i_fop = &bin_fops;
311 }
312 dentry->d_op = &sysfs_dentry_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 d_rehash(dentry);
314
315 return 0;
316}
317
318static int sysfs_attach_link(struct sysfs_dirent * sd, struct dentry * dentry)
319{
320 int err = 0;
321
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530322 dentry->d_fsdata = sysfs_get(sd);
Tejun Heodd14cbc2007-06-11 14:04:01 +0900323 /* protect sd->s_dentry against sysfs_d_iput */
324 spin_lock(&sysfs_lock);
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530325 sd->s_dentry = dentry;
Tejun Heodd14cbc2007-06-11 14:04:01 +0900326 spin_unlock(&sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 err = sysfs_create(dentry, S_IFLNK|S_IRWXUGO, init_symlink);
328 if (!err) {
329 dentry->d_op = &sysfs_dentry_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 d_rehash(dentry);
Maneesh Soni6fa5c822005-05-31 10:38:12 +0530331 } else
332 sysfs_put(sd);
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return err;
335}
336
337static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
338 struct nameidata *nd)
339{
340 struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
341 struct sysfs_dirent * sd;
342 int err = 0;
343
344 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
345 if (sd->s_type & SYSFS_NOT_PINNED) {
Tejun Heo0c096b52007-06-14 03:45:15 +0900346 if (strcmp(sd->s_name, dentry->d_name.name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 continue;
348
349 if (sd->s_type & SYSFS_KOBJ_LINK)
350 err = sysfs_attach_link(sd, dentry);
351 else
352 err = sysfs_attach_attr(sd, dentry);
353 break;
354 }
355 }
356
357 return ERR_PTR(err);
358}
359
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800360const struct inode_operations sysfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 .lookup = sysfs_lookup,
Maneesh Soni988d1862005-05-31 10:39:14 +0530362 .setattr = sysfs_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363};
364
365static void remove_dir(struct dentry * d)
366{
367 struct dentry * parent = dget(d->d_parent);
368 struct sysfs_dirent * sd;
369
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800370 mutex_lock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 d_delete(d);
372 sd = d->d_fsdata;
373 list_del_init(&sd->s_sibling);
374 sysfs_put(sd);
375 if (d->d_inode)
376 simple_rmdir(parent->d_inode,d);
377
378 pr_debug(" o %s removing done (%d)\n",d->d_name.name,
379 atomic_read(&d->d_count));
380
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800381 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 dput(parent);
383}
384
385void sysfs_remove_subdir(struct dentry * d)
386{
387 remove_dir(d);
388}
389
390
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700391static void __sysfs_remove_dir(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 struct sysfs_dirent * parent_sd;
394 struct sysfs_dirent * sd, * tmp;
395
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700396 dget(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (!dentry)
398 return;
399
400 pr_debug("sysfs %s: removing dir\n",dentry->d_name.name);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800401 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 parent_sd = dentry->d_fsdata;
403 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
Tejun Heo3e519032007-06-14 03:45:15 +0900404 if (!sd->s_type || !(sd->s_type & SYSFS_NOT_PINNED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 continue;
406 list_del_init(&sd->s_sibling);
407 sysfs_drop_dentry(sd, dentry);
408 sysfs_put(sd);
409 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800410 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 remove_dir(dentry);
413 /**
414 * Drop reference from dget() on entrance.
415 */
416 dput(dentry);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700417}
418
419/**
420 * sysfs_remove_dir - remove an object's directory.
421 * @kobj: object.
422 *
423 * The only thing special about this is that we remove any files in
424 * the directory before we remove the directory, and we've inlined
425 * what used to be sysfs_rmdir() below, instead of calling separately.
426 */
427
428void sysfs_remove_dir(struct kobject * kobj)
429{
Tejun Heoaecdced2007-06-14 03:45:15 +0900430 struct dentry *d = kobj->dentry;
431
432 spin_lock(&kobj_sysfs_assoc_lock);
Greg Kroah-Hartman641e6f32006-03-16 15:44:26 -0800433 kobj->dentry = NULL;
Tejun Heoaecdced2007-06-14 03:45:15 +0900434 spin_unlock(&kobj_sysfs_assoc_lock);
435
436 __sysfs_remove_dir(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700439int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent,
440 const char *new_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Tejun Heo0c096b52007-06-14 03:45:15 +0900442 struct sysfs_dirent *sd = kobj->dentry->d_fsdata;
443 struct sysfs_dirent *parent_sd = new_parent->d_fsdata;
444 struct dentry *new_dentry;
445 char *dup_name;
Tejun Heo996b7372007-06-14 03:45:14 +0900446 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700448 if (!new_parent)
449 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 down_write(&sysfs_rename_sem);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700452 mutex_lock(&new_parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700454 new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
Tejun Heo996b7372007-06-14 03:45:14 +0900455 if (IS_ERR(new_dentry)) {
456 error = PTR_ERR(new_dentry);
457 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
Tejun Heo996b7372007-06-14 03:45:14 +0900459
460 /* By allowing two different directories with the same
461 * d_parent we allow this routine to move between different
462 * shadows of the same directory
463 */
464 error = -EINVAL;
465 if (kobj->dentry->d_parent->d_inode != new_parent->d_inode ||
466 new_dentry->d_parent->d_inode != new_parent->d_inode ||
467 new_dentry == kobj->dentry)
468 goto out_dput;
469
470 error = -EEXIST;
471 if (new_dentry->d_inode)
472 goto out_dput;
473
Tejun Heo0c096b52007-06-14 03:45:15 +0900474 /* rename kobject and sysfs_dirent */
475 error = -ENOMEM;
476 new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
477 if (!new_name)
Tejun Heo996b7372007-06-14 03:45:14 +0900478 goto out_drop;
479
Tejun Heo0c096b52007-06-14 03:45:15 +0900480 error = kobject_set_name(kobj, "%s", new_name);
481 if (error)
482 goto out_free;
483
484 kfree(sd->s_name);
485 sd->s_name = new_name;
486
487 /* move under the new parent */
Tejun Heo996b7372007-06-14 03:45:14 +0900488 d_add(new_dentry, NULL);
489 d_move(kobj->dentry, new_dentry);
490
Tejun Heo996b7372007-06-14 03:45:14 +0900491 list_del_init(&sd->s_sibling);
492 list_add(&sd->s_sibling, &parent_sd->s_children);
493
494 error = 0;
495 goto out_unlock;
496
Tejun Heo0c096b52007-06-14 03:45:15 +0900497 out_free:
498 kfree(dup_name);
Tejun Heo996b7372007-06-14 03:45:14 +0900499 out_drop:
500 d_drop(new_dentry);
501 out_dput:
502 dput(new_dentry);
503 out_unlock:
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700504 mutex_unlock(&new_parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 up_write(&sysfs_rename_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return error;
507}
508
Cornelia Huck8a824722006-11-20 17:07:51 +0100509int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
510{
511 struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
512 struct sysfs_dirent *new_parent_sd, *sd;
513 int error;
514
Cornelia Huck8a824722006-11-20 17:07:51 +0100515 old_parent_dentry = kobj->parent ?
516 kobj->parent->dentry : sysfs_mount->mnt_sb->s_root;
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100517 new_parent_dentry = new_parent ?
518 new_parent->dentry : sysfs_mount->mnt_sb->s_root;
Cornelia Huck8a824722006-11-20 17:07:51 +0100519
Mark Lord0de15172007-03-06 01:42:03 -0800520 if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
521 return 0; /* nothing to move */
Cornelia Huck8a824722006-11-20 17:07:51 +0100522again:
523 mutex_lock(&old_parent_dentry->d_inode->i_mutex);
524 if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
525 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
526 goto again;
527 }
528
529 new_parent_sd = new_parent_dentry->d_fsdata;
530 sd = kobj->dentry->d_fsdata;
531
532 new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
533 strlen(kobj->name));
534 if (IS_ERR(new_dentry)) {
535 error = PTR_ERR(new_dentry);
536 goto out;
537 } else
538 error = 0;
539 d_add(new_dentry, NULL);
540 d_move(kobj->dentry, new_dentry);
541 dput(new_dentry);
542
543 /* Remove from old parent's list and insert into new parent's list. */
544 list_del_init(&sd->s_sibling);
545 list_add(&sd->s_sibling, &new_parent_sd->s_children);
546
547out:
548 mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
549 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
550
551 return error;
552}
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554static int sysfs_dir_open(struct inode *inode, struct file *file)
555{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800556 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
Tejun Heoa26cd722007-06-14 03:45:14 +0900558 struct sysfs_dirent * sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800560 mutex_lock(&dentry->d_inode->i_mutex);
Tejun Heo3e519032007-06-14 03:45:15 +0900561 sd = sysfs_new_dirent("_DIR_", 0, 0);
Tejun Heoa26cd722007-06-14 03:45:14 +0900562 if (sd)
563 sysfs_attach_dirent(sd, parent_sd, NULL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800564 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Tejun Heoa26cd722007-06-14 03:45:14 +0900566 file->private_data = sd;
567 return sd ? 0 : -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
570static int sysfs_dir_close(struct inode *inode, struct file *file)
571{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800572 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 struct sysfs_dirent * cursor = file->private_data;
574
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800575 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 list_del_init(&cursor->s_sibling);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800577 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 release_sysfs_dirent(cursor);
580
581 return 0;
582}
583
584/* Relationship between s_mode and the DT_xxx types */
585static inline unsigned char dt_type(struct sysfs_dirent *sd)
586{
587 return (sd->s_mode >> 12) & 15;
588}
589
590static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
591{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800592 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
594 struct sysfs_dirent *cursor = filp->private_data;
595 struct list_head *p, *q = &cursor->s_sibling;
596 ino_t ino;
597 int i = filp->f_pos;
598
599 switch (i) {
600 case 0:
Eric Sandeendc351252007-06-11 14:02:45 +0900601 ino = parent_sd->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
603 break;
604 filp->f_pos++;
605 i++;
606 /* fallthrough */
607 case 1:
Tejun Heo13b30862007-06-14 03:45:14 +0900608 if (parent_sd->s_parent)
609 ino = parent_sd->s_parent->s_ino;
610 else
611 ino = parent_sd->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
613 break;
614 filp->f_pos++;
615 i++;
616 /* fallthrough */
617 default:
Akinobu Mita1bfba4e2006-06-26 00:24:40 -0700618 if (filp->f_pos == 2)
619 list_move(q, &parent_sd->s_children);
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
622 struct sysfs_dirent *next;
623 const char * name;
624 int len;
625
626 next = list_entry(p, struct sysfs_dirent,
627 s_sibling);
Tejun Heo3e519032007-06-14 03:45:15 +0900628 if (!next->s_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 continue;
630
Tejun Heo0c096b52007-06-14 03:45:15 +0900631 name = next->s_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 len = strlen(name);
Eric Sandeendc351252007-06-11 14:02:45 +0900633 ino = next->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 if (filldir(dirent, name, len, filp->f_pos, ino,
636 dt_type(next)) < 0)
637 return 0;
638
Akinobu Mita1bfba4e2006-06-26 00:24:40 -0700639 list_move(q, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 p = q;
641 filp->f_pos++;
642 }
643 }
644 return 0;
645}
646
647static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
648{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800649 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800651 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 switch (origin) {
653 case 1:
654 offset += file->f_pos;
655 case 0:
656 if (offset >= 0)
657 break;
658 default:
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800659 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return -EINVAL;
661 }
662 if (offset != file->f_pos) {
663 file->f_pos = offset;
664 if (file->f_pos >= 2) {
665 struct sysfs_dirent *sd = dentry->d_fsdata;
666 struct sysfs_dirent *cursor = file->private_data;
667 struct list_head *p;
668 loff_t n = file->f_pos - 2;
669
670 list_del(&cursor->s_sibling);
671 p = sd->s_children.next;
672 while (n && p != &sd->s_children) {
673 struct sysfs_dirent *next;
674 next = list_entry(p, struct sysfs_dirent,
675 s_sibling);
Tejun Heo3e519032007-06-14 03:45:15 +0900676 if (next->s_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 n--;
678 p = p->next;
679 }
680 list_add_tail(&cursor->s_sibling, p);
681 }
682 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800683 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return offset;
685}
686
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700687
688/**
689 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
690 * @kobj: object we're creating shadow of.
691 */
692
693int sysfs_make_shadowed_dir(struct kobject *kobj,
694 void * (*follow_link)(struct dentry *, struct nameidata *))
695{
696 struct inode *inode;
697 struct inode_operations *i_op;
698
699 inode = kobj->dentry->d_inode;
700 if (inode->i_op != &sysfs_dir_inode_operations)
701 return -EINVAL;
702
703 i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
704 if (!i_op)
705 return -ENOMEM;
706
707 memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
708 i_op->follow_link = follow_link;
709
710 /* Locking of inode->i_op?
711 * Since setting i_op is a single word write and they
712 * are atomic we should be ok here.
713 */
714 inode->i_op = i_op;
715 return 0;
716}
717
718/**
719 * sysfs_create_shadow_dir - create a shadow directory for an object.
720 * @kobj: object we're creating directory for.
721 *
722 * sysfs_make_shadowed_dir must already have been called on this
723 * directory.
724 */
725
726struct dentry *sysfs_create_shadow_dir(struct kobject *kobj)
727{
Tejun Heo13b30862007-06-14 03:45:14 +0900728 struct dentry *dir = kobj->dentry;
729 struct inode *inode = dir->d_inode;
730 struct dentry *parent = dir->d_parent;
731 struct sysfs_dirent *parent_sd = parent->d_fsdata;
732 struct dentry *shadow;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700733 struct sysfs_dirent *sd;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700734
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700735 shadow = ERR_PTR(-EINVAL);
736 if (!sysfs_is_shadowed_inode(inode))
737 goto out;
738
739 shadow = d_alloc(parent, &dir->d_name);
740 if (!shadow)
741 goto nomem;
742
Tejun Heo3e519032007-06-14 03:45:15 +0900743 sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700744 if (!sd)
745 goto nomem;
Tejun Heo3e519032007-06-14 03:45:15 +0900746 sd->s_elem.dir.kobj = kobj;
Tejun Heo13b30862007-06-14 03:45:14 +0900747 /* point to parent_sd but don't attach to it */
748 sd->s_parent = sysfs_get(parent_sd);
Tejun Heoa26cd722007-06-14 03:45:14 +0900749 sysfs_attach_dirent(sd, NULL, shadow);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700750
751 d_instantiate(shadow, igrab(inode));
752 inc_nlink(inode);
753 inc_nlink(parent->d_inode);
754 shadow->d_op = &sysfs_dentry_ops;
755
756 dget(shadow); /* Extra count - pin the dentry in core */
757
758out:
759 return shadow;
760nomem:
761 dput(shadow);
762 shadow = ERR_PTR(-ENOMEM);
763 goto out;
764}
765
766/**
767 * sysfs_remove_shadow_dir - remove an object's directory.
768 * @shadow: dentry of shadow directory
769 *
770 * The only thing special about this is that we remove any files in
771 * the directory before we remove the directory, and we've inlined
772 * what used to be sysfs_rmdir() below, instead of calling separately.
773 */
774
775void sysfs_remove_shadow_dir(struct dentry *shadow)
776{
777 __sysfs_remove_dir(shadow);
778}
779
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800780const struct file_operations sysfs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 .open = sysfs_dir_open,
782 .release = sysfs_dir_close,
783 .llseek = sysfs_dir_lseek,
784 .read = generic_read_dir,
785 .readdir = sysfs_readdir,
786};