blob: 06dff2c30c9b48ad1a9f55ebaa5b9bd627b8bc95 [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
Tejun Heo42b37df2007-06-14 03:45:17 +090023static int sysfs_alloc_ino(ino_t *pino)
Tejun Heo2b611bb2007-06-14 03:45:13 +090024{
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 Heo0ab66082007-06-14 03:45:16 +090056 /* If @sd is being released after deletion, s_active is write
57 * locked. If @sd is cursor for directory walk or being
58 * released prematurely, s_active has no reader or writer.
59 *
60 * sysfs_deactivate() lies to lockdep that s_active is
61 * unlocked immediately. Lie one more time to cover the
62 * previous lie.
63 */
64 if (!down_write_trylock(&sd->s_active))
65 rwsem_acquire(&sd->s_active.dep_map,
66 SYSFS_S_ACTIVE_DEACTIVATE, 0, _RET_IP_);
67 up_write(&sd->s_active);
68
Tejun Heo3e519032007-06-14 03:45:15 +090069 if (sd->s_type & SYSFS_KOBJ_LINK)
Tejun Heo2b29ac22007-06-14 03:45:15 +090070 sysfs_put(sd->s_elem.symlink.target_sd);
Tejun Heo0c096b52007-06-14 03:45:15 +090071 if (sd->s_type & SYSFS_COPY_NAME)
72 kfree(sd->s_name);
Tejun Heofa7f9122007-06-14 03:45:13 +090073 kfree(sd->s_iattr);
Tejun Heo2b611bb2007-06-14 03:45:13 +090074 sysfs_free_ino(sd->s_ino);
Tejun Heofa7f9122007-06-14 03:45:13 +090075 kmem_cache_free(sysfs_dir_cachep, sd);
Tejun Heo13b30862007-06-14 03:45:14 +090076
77 sd = parent_sd;
78 if (sd && atomic_dec_and_test(&sd->s_count))
79 goto repeat;
Tejun Heofa7f9122007-06-14 03:45:13 +090080}
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
83{
84 struct sysfs_dirent * sd = dentry->d_fsdata;
85
86 if (sd) {
Tejun Heodd14cbc2007-06-11 14:04:01 +090087 /* sd->s_dentry is protected with sysfs_lock. This
88 * allows sysfs_drop_dentry() to dereference it.
89 */
90 spin_lock(&sysfs_lock);
91
92 /* The dentry might have been deleted or another
93 * lookup could have happened updating sd->s_dentry to
94 * point the new dentry. Ignore if it isn't pointing
95 * to this dentry.
96 */
97 if (sd->s_dentry == dentry)
98 sd->s_dentry = NULL;
99 spin_unlock(&sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 sysfs_put(sd);
101 }
102 iput(inode);
103}
104
105static struct dentry_operations sysfs_dentry_ops = {
106 .d_iput = sysfs_d_iput,
107};
108
Tejun Heo3e519032007-06-14 03:45:15 +0900109struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Tejun Heo0c096b52007-06-14 03:45:15 +0900111 char *dup_name = NULL;
112 struct sysfs_dirent *sd = NULL;
113
114 if (type & SYSFS_COPY_NAME) {
115 name = dup_name = kstrdup(name, GFP_KERNEL);
116 if (!name)
117 goto err_out;
118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800120 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (!sd)
Tejun Heo0c096b52007-06-14 03:45:15 +0900122 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Tejun Heo0c096b52007-06-14 03:45:15 +0900124 if (sysfs_alloc_ino(&sd->s_ino))
125 goto err_out;
Tejun Heo2b611bb2007-06-14 03:45:13 +0900126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 atomic_set(&sd->s_count, 1);
Juha Yrjöläeea3f892006-08-03 19:06:25 +0300128 atomic_set(&sd->s_event, 1);
Tejun Heo0ab66082007-06-14 03:45:16 +0900129 init_rwsem(&sd->s_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 INIT_LIST_HEAD(&sd->s_children);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700131 INIT_LIST_HEAD(&sd->s_sibling);
Tejun Heoa26cd722007-06-14 03:45:14 +0900132
Tejun Heo0c096b52007-06-14 03:45:15 +0900133 sd->s_name = name;
Tejun Heoa26cd722007-06-14 03:45:14 +0900134 sd->s_mode = mode;
135 sd->s_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 return sd;
Tejun Heo0c096b52007-06-14 03:45:15 +0900138
139 err_out:
140 kfree(dup_name);
141 kmem_cache_free(sysfs_dir_cachep, sd);
142 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
Tejun Heo198a2a82007-06-14 03:45:16 +0900145static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry)
146{
147 dentry->d_op = &sysfs_dentry_ops;
148 dentry->d_fsdata = sysfs_get(sd);
149
150 /* protect sd->s_dentry against sysfs_d_iput */
151 spin_lock(&sysfs_lock);
152 sd->s_dentry = dentry;
153 spin_unlock(&sysfs_lock);
154
155 d_rehash(dentry);
156}
157
Tejun Heoa26cd722007-06-14 03:45:14 +0900158void sysfs_attach_dirent(struct sysfs_dirent *sd,
159 struct sysfs_dirent *parent_sd, struct dentry *dentry)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700160{
Tejun Heo198a2a82007-06-14 03:45:16 +0900161 if (dentry)
162 sysfs_attach_dentry(sd, dentry);
Tejun Heoa26cd722007-06-14 03:45:14 +0900163
Tejun Heo13b30862007-06-14 03:45:14 +0900164 if (parent_sd) {
165 sd->s_parent = sysfs_get(parent_sd);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700166 list_add(&sd->s_sibling, &parent_sd->s_children);
Tejun Heo13b30862007-06-14 03:45:14 +0900167 }
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700168}
169
Martin Waitza5802902006-04-02 13:59:55 +0200170/*
Maneesh Sonic5168652006-03-09 19:40:14 +0530171 *
172 * Return -EEXIST if there is already a sysfs element with the same name for
173 * the same parent.
174 *
175 * called with parent inode's i_mutex held
176 */
177int sysfs_dirent_exist(struct sysfs_dirent *parent_sd,
178 const unsigned char *new)
179{
180 struct sysfs_dirent * sd;
181
182 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
Tejun Heo3e519032007-06-14 03:45:15 +0900183 if (sd->s_type) {
Tejun Heo0c096b52007-06-14 03:45:15 +0900184 if (strcmp(sd->s_name, new))
Maneesh Sonic5168652006-03-09 19:40:14 +0530185 continue;
186 else
187 return -EEXIST;
188 }
189 }
190
191 return 0;
192}
193
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900194static int create_dir(struct kobject *kobj, struct dentry *parent,
195 const char *name, struct dentry **p_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 int error;
198 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900199 struct dentry *dentry;
Tejun Heofc9f54b2007-06-14 03:45:17 +0900200 struct inode *inode;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900201 struct sysfs_dirent *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900203 mutex_lock(&parent->d_inode->i_mutex);
204
Tejun Heofc9f54b2007-06-14 03:45:17 +0900205 /* allocate */
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900206 dentry = lookup_one_len(name, parent, strlen(name));
207 if (IS_ERR(dentry)) {
208 error = PTR_ERR(dentry);
209 goto out_unlock;
210 }
211
212 error = -EEXIST;
Tejun Heofc9f54b2007-06-14 03:45:17 +0900213 if (dentry->d_inode)
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900214 goto out_dput;
215
Tejun Heoa26cd722007-06-14 03:45:14 +0900216 error = -ENOMEM;
Tejun Heo3e519032007-06-14 03:45:15 +0900217 sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
Tejun Heoa26cd722007-06-14 03:45:14 +0900218 if (!sd)
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900219 goto out_drop;
Tejun Heo3e519032007-06-14 03:45:15 +0900220 sd->s_elem.dir.kobj = kobj;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900221
Tejun Heo8312a8d2007-06-14 03:45:17 +0900222 inode = sysfs_get_inode(sd);
Tejun Heofc9f54b2007-06-14 03:45:17 +0900223 if (!inode)
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900224 goto out_sput;
225
Tejun Heo8312a8d2007-06-14 03:45:17 +0900226 if (inode->i_state & I_NEW) {
227 inode->i_op = &sysfs_dir_inode_operations;
228 inode->i_fop = &sysfs_dir_operations;
229 /* directory inodes start off with i_nlink == 2 (for ".") */
230 inc_nlink(inode);
231 }
Tejun Heofc9f54b2007-06-14 03:45:17 +0900232
233 /* link in */
234 error = -EEXIST;
235 if (sysfs_dirent_exist(parent->d_fsdata, name))
236 goto out_iput;
237
238 sysfs_instantiate(dentry, inode);
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900239 inc_nlink(parent->d_inode);
Tejun Heo198a2a82007-06-14 03:45:16 +0900240 sysfs_attach_dirent(sd, parent->d_fsdata, dentry);
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900241
242 *p_dentry = dentry;
243 error = 0;
Tejun Heofc9f54b2007-06-14 03:45:17 +0900244 goto out_unlock; /* pin directory dentry in core */
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900245
Tejun Heofc9f54b2007-06-14 03:45:17 +0900246 out_iput:
247 iput(inode);
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900248 out_sput:
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900249 sysfs_put(sd);
250 out_drop:
251 d_drop(dentry);
252 out_dput:
253 dput(dentry);
254 out_unlock:
255 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return error;
257}
258
259
260int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d)
261{
262 return create_dir(k,k->dentry,n,d);
263}
264
265/**
266 * sysfs_create_dir - create a directory for an object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 * @kobj: object we're creating directory for.
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700268 * @shadow_parent: parent parent object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 */
270
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700271int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 struct dentry * dentry = NULL;
274 struct dentry * parent;
275 int error = 0;
276
277 BUG_ON(!kobj);
278
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700279 if (shadow_parent)
280 parent = shadow_parent;
281 else if (kobj->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 parent = kobj->parent->dentry;
283 else if (sysfs_mount && sysfs_mount->mnt_sb)
284 parent = sysfs_mount->mnt_sb->s_root;
285 else
286 return -EFAULT;
287
288 error = create_dir(kobj,parent,kobject_name(kobj),&dentry);
289 if (!error)
290 kobj->dentry = dentry;
291 return error;
292}
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
295 struct nameidata *nd)
296{
297 struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
298 struct sysfs_dirent * sd;
Tejun Heofc9f54b2007-06-14 03:45:17 +0900299 struct inode *inode;
300 int found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
Tejun Heofc9f54b2007-06-14 03:45:17 +0900303 if ((sd->s_type & SYSFS_NOT_PINNED) &&
304 !strcmp(sd->s_name, dentry->d_name.name)) {
305 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 break;
307 }
308 }
309
Tejun Heofc9f54b2007-06-14 03:45:17 +0900310 /* no such entry */
311 if (!found)
312 return NULL;
313
314 /* attach dentry and inode */
Tejun Heo8312a8d2007-06-14 03:45:17 +0900315 inode = sysfs_get_inode(sd);
Tejun Heofc9f54b2007-06-14 03:45:17 +0900316 if (!inode)
317 return ERR_PTR(-ENOMEM);
318
Tejun Heo8312a8d2007-06-14 03:45:17 +0900319 if (inode->i_state & I_NEW) {
320 /* initialize inode according to type */
321 if (sd->s_type & SYSFS_KOBJ_ATTR) {
322 inode->i_size = PAGE_SIZE;
323 inode->i_fop = &sysfs_file_operations;
324 } else if (sd->s_type & SYSFS_KOBJ_BIN_ATTR) {
325 struct bin_attribute *bin_attr =
326 sd->s_elem.bin_attr.bin_attr;
327 inode->i_size = bin_attr->size;
328 inode->i_fop = &bin_fops;
329 } else if (sd->s_type & SYSFS_KOBJ_LINK)
330 inode->i_op = &sysfs_symlink_inode_operations;
331 }
Tejun Heofc9f54b2007-06-14 03:45:17 +0900332
333 sysfs_instantiate(dentry, inode);
334 sysfs_attach_dentry(sd, dentry);
335
336 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800339const struct inode_operations sysfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 .lookup = sysfs_lookup,
Maneesh Soni988d1862005-05-31 10:39:14 +0530341 .setattr = sysfs_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342};
343
344static void remove_dir(struct dentry * d)
345{
Tejun Heodbde0fc2007-06-14 03:45:16 +0900346 struct dentry *parent = d->d_parent;
347 struct sysfs_dirent *sd = d->d_fsdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800349 mutex_lock(&parent->d_inode->i_mutex);
Tejun Heodbde0fc2007-06-14 03:45:16 +0900350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 list_del_init(&sd->s_sibling);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 pr_debug(" o %s removing done (%d)\n",d->d_name.name,
354 atomic_read(&d->d_count));
355
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800356 mutex_unlock(&parent->d_inode->i_mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900357
Tejun Heodbde0fc2007-06-14 03:45:16 +0900358 sysfs_drop_dentry(sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900359 sysfs_deactivate(sd);
360 sysfs_put(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363void sysfs_remove_subdir(struct dentry * d)
364{
365 remove_dir(d);
366}
367
368
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700369static void __sysfs_remove_dir(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Tejun Heo0ab66082007-06-14 03:45:16 +0900371 LIST_HEAD(removed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 struct sysfs_dirent * parent_sd;
373 struct sysfs_dirent * sd, * tmp;
374
375 if (!dentry)
376 return;
377
378 pr_debug("sysfs %s: removing dir\n",dentry->d_name.name);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800379 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 parent_sd = dentry->d_fsdata;
381 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
Tejun Heo3e519032007-06-14 03:45:15 +0900382 if (!sd->s_type || !(sd->s_type & SYSFS_NOT_PINNED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 continue;
Tejun Heo0ab66082007-06-14 03:45:16 +0900384 list_move(&sd->s_sibling, &removed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800386 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Tejun Heo0ab66082007-06-14 03:45:16 +0900388 list_for_each_entry_safe(sd, tmp, &removed, s_sibling) {
389 list_del_init(&sd->s_sibling);
Tejun Heodbde0fc2007-06-14 03:45:16 +0900390 sysfs_drop_dentry(sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900391 sysfs_deactivate(sd);
392 sysfs_put(sd);
393 }
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 remove_dir(dentry);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700396}
397
398/**
399 * sysfs_remove_dir - remove an object's directory.
400 * @kobj: object.
401 *
402 * The only thing special about this is that we remove any files in
403 * the directory before we remove the directory, and we've inlined
404 * what used to be sysfs_rmdir() below, instead of calling separately.
405 */
406
407void sysfs_remove_dir(struct kobject * kobj)
408{
Tejun Heoaecdced2007-06-14 03:45:15 +0900409 struct dentry *d = kobj->dentry;
410
411 spin_lock(&kobj_sysfs_assoc_lock);
Greg Kroah-Hartman641e6f32006-03-16 15:44:26 -0800412 kobj->dentry = NULL;
Tejun Heoaecdced2007-06-14 03:45:15 +0900413 spin_unlock(&kobj_sysfs_assoc_lock);
414
415 __sysfs_remove_dir(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700418int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent,
419 const char *new_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Tejun Heo0c096b52007-06-14 03:45:15 +0900421 struct sysfs_dirent *sd = kobj->dentry->d_fsdata;
422 struct sysfs_dirent *parent_sd = new_parent->d_fsdata;
423 struct dentry *new_dentry;
424 char *dup_name;
Tejun Heo996b7372007-06-14 03:45:14 +0900425 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700427 if (!new_parent)
428 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 down_write(&sysfs_rename_sem);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700431 mutex_lock(&new_parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700433 new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
Tejun Heo996b7372007-06-14 03:45:14 +0900434 if (IS_ERR(new_dentry)) {
435 error = PTR_ERR(new_dentry);
436 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
Tejun Heo996b7372007-06-14 03:45:14 +0900438
439 /* By allowing two different directories with the same
440 * d_parent we allow this routine to move between different
441 * shadows of the same directory
442 */
443 error = -EINVAL;
444 if (kobj->dentry->d_parent->d_inode != new_parent->d_inode ||
445 new_dentry->d_parent->d_inode != new_parent->d_inode ||
446 new_dentry == kobj->dentry)
447 goto out_dput;
448
449 error = -EEXIST;
450 if (new_dentry->d_inode)
451 goto out_dput;
452
Tejun Heo0c096b52007-06-14 03:45:15 +0900453 /* rename kobject and sysfs_dirent */
454 error = -ENOMEM;
455 new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
456 if (!new_name)
Tejun Heo996b7372007-06-14 03:45:14 +0900457 goto out_drop;
458
Tejun Heo0c096b52007-06-14 03:45:15 +0900459 error = kobject_set_name(kobj, "%s", new_name);
460 if (error)
461 goto out_free;
462
463 kfree(sd->s_name);
464 sd->s_name = new_name;
465
466 /* move under the new parent */
Tejun Heo996b7372007-06-14 03:45:14 +0900467 d_add(new_dentry, NULL);
468 d_move(kobj->dentry, new_dentry);
469
Tejun Heo996b7372007-06-14 03:45:14 +0900470 list_del_init(&sd->s_sibling);
Tejun Heo7f7cfff2007-06-14 03:45:17 +0900471 sysfs_get(parent_sd);
472 sysfs_put(sd->s_parent);
473 sd->s_parent = parent_sd;
Tejun Heo996b7372007-06-14 03:45:14 +0900474 list_add(&sd->s_sibling, &parent_sd->s_children);
475
476 error = 0;
477 goto out_unlock;
478
Tejun Heo0c096b52007-06-14 03:45:15 +0900479 out_free:
480 kfree(dup_name);
Tejun Heo996b7372007-06-14 03:45:14 +0900481 out_drop:
482 d_drop(new_dentry);
483 out_dput:
484 dput(new_dentry);
485 out_unlock:
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700486 mutex_unlock(&new_parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 up_write(&sysfs_rename_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return error;
489}
490
Cornelia Huck8a824722006-11-20 17:07:51 +0100491int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
492{
493 struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
494 struct sysfs_dirent *new_parent_sd, *sd;
495 int error;
496
Cornelia Huck8a824722006-11-20 17:07:51 +0100497 old_parent_dentry = kobj->parent ?
498 kobj->parent->dentry : sysfs_mount->mnt_sb->s_root;
Cornelia Huckc744aea2007-01-08 20:16:44 +0100499 new_parent_dentry = new_parent ?
500 new_parent->dentry : sysfs_mount->mnt_sb->s_root;
Cornelia Huck8a824722006-11-20 17:07:51 +0100501
Mark Lord0de15172007-03-06 01:42:03 -0800502 if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
503 return 0; /* nothing to move */
Cornelia Huck8a824722006-11-20 17:07:51 +0100504again:
505 mutex_lock(&old_parent_dentry->d_inode->i_mutex);
506 if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
507 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
508 goto again;
509 }
510
511 new_parent_sd = new_parent_dentry->d_fsdata;
512 sd = kobj->dentry->d_fsdata;
513
514 new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
515 strlen(kobj->name));
516 if (IS_ERR(new_dentry)) {
517 error = PTR_ERR(new_dentry);
518 goto out;
519 } else
520 error = 0;
521 d_add(new_dentry, NULL);
522 d_move(kobj->dentry, new_dentry);
523 dput(new_dentry);
524
525 /* Remove from old parent's list and insert into new parent's list. */
526 list_del_init(&sd->s_sibling);
Tejun Heo7f7cfff2007-06-14 03:45:17 +0900527 sysfs_get(new_parent_sd);
528 sysfs_put(sd->s_parent);
529 sd->s_parent = new_parent_sd;
Cornelia Huck8a824722006-11-20 17:07:51 +0100530 list_add(&sd->s_sibling, &new_parent_sd->s_children);
531
532out:
533 mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
534 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
535
536 return error;
537}
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539static int sysfs_dir_open(struct inode *inode, struct file *file)
540{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800541 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
Tejun Heoa26cd722007-06-14 03:45:14 +0900543 struct sysfs_dirent * sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800545 mutex_lock(&dentry->d_inode->i_mutex);
Tejun Heo3e519032007-06-14 03:45:15 +0900546 sd = sysfs_new_dirent("_DIR_", 0, 0);
Tejun Heoa26cd722007-06-14 03:45:14 +0900547 if (sd)
548 sysfs_attach_dirent(sd, parent_sd, NULL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800549 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Tejun Heoa26cd722007-06-14 03:45:14 +0900551 file->private_data = sd;
552 return sd ? 0 : -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
555static int sysfs_dir_close(struct inode *inode, struct file *file)
556{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800557 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 struct sysfs_dirent * cursor = file->private_data;
559
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800560 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 list_del_init(&cursor->s_sibling);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800562 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 release_sysfs_dirent(cursor);
565
566 return 0;
567}
568
569/* Relationship between s_mode and the DT_xxx types */
570static inline unsigned char dt_type(struct sysfs_dirent *sd)
571{
572 return (sd->s_mode >> 12) & 15;
573}
574
575static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
576{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800577 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
579 struct sysfs_dirent *cursor = filp->private_data;
580 struct list_head *p, *q = &cursor->s_sibling;
581 ino_t ino;
582 int i = filp->f_pos;
583
584 switch (i) {
585 case 0:
Eric Sandeendc351252007-06-11 14:02:45 +0900586 ino = parent_sd->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
588 break;
589 filp->f_pos++;
590 i++;
591 /* fallthrough */
592 case 1:
Tejun Heo13b30862007-06-14 03:45:14 +0900593 if (parent_sd->s_parent)
594 ino = parent_sd->s_parent->s_ino;
595 else
596 ino = parent_sd->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
598 break;
599 filp->f_pos++;
600 i++;
601 /* fallthrough */
602 default:
Akinobu Mita1bfba4e2006-06-26 00:24:40 -0700603 if (filp->f_pos == 2)
604 list_move(q, &parent_sd->s_children);
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
607 struct sysfs_dirent *next;
608 const char * name;
609 int len;
610
611 next = list_entry(p, struct sysfs_dirent,
612 s_sibling);
Tejun Heo3e519032007-06-14 03:45:15 +0900613 if (!next->s_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 continue;
615
Tejun Heo0c096b52007-06-14 03:45:15 +0900616 name = next->s_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 len = strlen(name);
Eric Sandeendc351252007-06-11 14:02:45 +0900618 ino = next->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 if (filldir(dirent, name, len, filp->f_pos, ino,
621 dt_type(next)) < 0)
622 return 0;
623
Akinobu Mita1bfba4e2006-06-26 00:24:40 -0700624 list_move(q, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 p = q;
626 filp->f_pos++;
627 }
628 }
629 return 0;
630}
631
632static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
633{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800634 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800636 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 switch (origin) {
638 case 1:
639 offset += file->f_pos;
640 case 0:
641 if (offset >= 0)
642 break;
643 default:
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800644 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return -EINVAL;
646 }
647 if (offset != file->f_pos) {
648 file->f_pos = offset;
649 if (file->f_pos >= 2) {
650 struct sysfs_dirent *sd = dentry->d_fsdata;
651 struct sysfs_dirent *cursor = file->private_data;
652 struct list_head *p;
653 loff_t n = file->f_pos - 2;
654
655 list_del(&cursor->s_sibling);
656 p = sd->s_children.next;
657 while (n && p != &sd->s_children) {
658 struct sysfs_dirent *next;
659 next = list_entry(p, struct sysfs_dirent,
660 s_sibling);
Tejun Heo3e519032007-06-14 03:45:15 +0900661 if (next->s_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 n--;
663 p = p->next;
664 }
665 list_add_tail(&cursor->s_sibling, p);
666 }
667 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800668 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return offset;
670}
671
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700672
673/**
674 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
675 * @kobj: object we're creating shadow of.
676 */
677
678int sysfs_make_shadowed_dir(struct kobject *kobj,
679 void * (*follow_link)(struct dentry *, struct nameidata *))
680{
681 struct inode *inode;
682 struct inode_operations *i_op;
683
684 inode = kobj->dentry->d_inode;
685 if (inode->i_op != &sysfs_dir_inode_operations)
686 return -EINVAL;
687
688 i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
689 if (!i_op)
690 return -ENOMEM;
691
692 memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
693 i_op->follow_link = follow_link;
694
695 /* Locking of inode->i_op?
696 * Since setting i_op is a single word write and they
697 * are atomic we should be ok here.
698 */
699 inode->i_op = i_op;
700 return 0;
701}
702
703/**
704 * sysfs_create_shadow_dir - create a shadow directory for an object.
705 * @kobj: object we're creating directory for.
706 *
707 * sysfs_make_shadowed_dir must already have been called on this
708 * directory.
709 */
710
711struct dentry *sysfs_create_shadow_dir(struct kobject *kobj)
712{
Tejun Heo13b30862007-06-14 03:45:14 +0900713 struct dentry *dir = kobj->dentry;
714 struct inode *inode = dir->d_inode;
715 struct dentry *parent = dir->d_parent;
716 struct sysfs_dirent *parent_sd = parent->d_fsdata;
717 struct dentry *shadow;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700718 struct sysfs_dirent *sd;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700719
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700720 shadow = ERR_PTR(-EINVAL);
721 if (!sysfs_is_shadowed_inode(inode))
722 goto out;
723
724 shadow = d_alloc(parent, &dir->d_name);
725 if (!shadow)
726 goto nomem;
727
Tejun Heo3e519032007-06-14 03:45:15 +0900728 sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700729 if (!sd)
730 goto nomem;
Tejun Heo3e519032007-06-14 03:45:15 +0900731 sd->s_elem.dir.kobj = kobj;
Tejun Heo13b30862007-06-14 03:45:14 +0900732 /* point to parent_sd but don't attach to it */
733 sd->s_parent = sysfs_get(parent_sd);
Tejun Heoa26cd722007-06-14 03:45:14 +0900734 sysfs_attach_dirent(sd, NULL, shadow);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700735
736 d_instantiate(shadow, igrab(inode));
737 inc_nlink(inode);
738 inc_nlink(parent->d_inode);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700739
740 dget(shadow); /* Extra count - pin the dentry in core */
741
742out:
743 return shadow;
744nomem:
745 dput(shadow);
746 shadow = ERR_PTR(-ENOMEM);
747 goto out;
748}
749
750/**
751 * sysfs_remove_shadow_dir - remove an object's directory.
752 * @shadow: dentry of shadow directory
753 *
754 * The only thing special about this is that we remove any files in
755 * the directory before we remove the directory, and we've inlined
756 * what used to be sysfs_rmdir() below, instead of calling separately.
757 */
758
759void sysfs_remove_shadow_dir(struct dentry *shadow)
760{
761 __sysfs_remove_dir(shadow);
762}
763
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800764const struct file_operations sysfs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 .open = sysfs_dir_open,
766 .release = sysfs_dir_close,
767 .llseek = sysfs_dir_lseek,
768 .read = generic_read_dir,
769 .readdir = sysfs_readdir,
770};