blob: 4762a9aa0b27a6459cb6acae65f79b3b6e8c2238 [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>
Tejun Heo8619f972007-06-14 03:45:18 +090013#include <linux/completion.h>
Oliver Neukum94bebf42006-12-20 10:52:44 +010014#include <asm/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "sysfs.h"
16
17DECLARE_RWSEM(sysfs_rename_sem);
Tejun Heodd14cbc2007-06-11 14:04:01 +090018spinlock_t sysfs_lock = SPIN_LOCK_UNLOCKED;
Tejun Heoaecdced2007-06-14 03:45:15 +090019spinlock_t kobj_sysfs_assoc_lock = SPIN_LOCK_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Tejun Heo2b611bb2007-06-14 03:45:13 +090021static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED;
22static DEFINE_IDA(sysfs_ino_ida);
23
Tejun Heob6b4a432007-06-14 03:45:18 +090024/**
Tejun Heo0c73f182007-06-14 03:45:18 +090025 * sysfs_link_sibling - link sysfs_dirent into sibling list
26 * @sd: sysfs_dirent of interest
27 *
28 * Link @sd into its sibling list which starts from
29 * sd->s_parent->s_children.
30 *
31 * Locking:
32 * mutex_lock(sd->s_parent->dentry->d_inode->i_mutex)
33 */
34static void sysfs_link_sibling(struct sysfs_dirent *sd)
35{
36 struct sysfs_dirent *parent_sd = sd->s_parent;
37
38 BUG_ON(sd->s_sibling);
39 sd->s_sibling = parent_sd->s_children;
40 parent_sd->s_children = sd;
41}
42
43/**
44 * sysfs_unlink_sibling - unlink sysfs_dirent from sibling list
45 * @sd: sysfs_dirent of interest
46 *
47 * Unlink @sd from its sibling list which starts from
48 * sd->s_parent->s_children.
49 *
50 * Locking:
51 * mutex_lock(sd->s_parent->dentry->d_inode->i_mutex)
52 */
53static void sysfs_unlink_sibling(struct sysfs_dirent *sd)
54{
55 struct sysfs_dirent **pos;
56
57 for (pos = &sd->s_parent->s_children; *pos; pos = &(*pos)->s_sibling) {
58 if (*pos == sd) {
59 *pos = sd->s_sibling;
60 sd->s_sibling = NULL;
61 break;
62 }
63 }
64}
65
66/**
Tejun Heob6b4a432007-06-14 03:45:18 +090067 * sysfs_get_active - get an active reference to sysfs_dirent
68 * @sd: sysfs_dirent to get an active reference to
69 *
70 * Get an active reference of @sd. This function is noop if @sd
71 * is NULL.
72 *
73 * RETURNS:
74 * Pointer to @sd on success, NULL on failure.
75 */
76struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
77{
Tejun Heo8619f972007-06-14 03:45:18 +090078 if (unlikely(!sd))
79 return NULL;
80
81 while (1) {
82 int v, t;
83
84 v = atomic_read(&sd->s_active);
85 if (unlikely(v < 0))
86 return NULL;
87
88 t = atomic_cmpxchg(&sd->s_active, v, v + 1);
89 if (likely(t == v))
90 return sd;
91 if (t < 0)
92 return NULL;
93
94 cpu_relax();
Tejun Heob6b4a432007-06-14 03:45:18 +090095 }
Tejun Heob6b4a432007-06-14 03:45:18 +090096}
97
98/**
99 * sysfs_put_active - put an active reference to sysfs_dirent
100 * @sd: sysfs_dirent to put an active reference to
101 *
102 * Put an active reference to @sd. This function is noop if @sd
103 * is NULL.
104 */
105void sysfs_put_active(struct sysfs_dirent *sd)
106{
Tejun Heo8619f972007-06-14 03:45:18 +0900107 struct completion *cmpl;
108 int v;
109
110 if (unlikely(!sd))
111 return;
112
113 v = atomic_dec_return(&sd->s_active);
114 if (likely(v != SD_DEACTIVATED_BIAS))
115 return;
116
117 /* atomic_dec_return() is a mb(), we'll always see the updated
Tejun Heo0c73f182007-06-14 03:45:18 +0900118 * sd->s_sibling.
Tejun Heo8619f972007-06-14 03:45:18 +0900119 */
Tejun Heo0c73f182007-06-14 03:45:18 +0900120 cmpl = (void *)sd->s_sibling;
Tejun Heo8619f972007-06-14 03:45:18 +0900121 complete(cmpl);
Tejun Heob6b4a432007-06-14 03:45:18 +0900122}
123
124/**
125 * sysfs_get_active_two - get active references to sysfs_dirent and parent
126 * @sd: sysfs_dirent of interest
127 *
128 * Get active reference to @sd and its parent. Parent's active
129 * reference is grabbed first. This function is noop if @sd is
130 * NULL.
131 *
132 * RETURNS:
133 * Pointer to @sd on success, NULL on failure.
134 */
135struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd)
136{
137 if (sd) {
138 if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent)))
139 return NULL;
140 if (unlikely(!sysfs_get_active(sd))) {
141 sysfs_put_active(sd->s_parent);
142 return NULL;
143 }
144 }
145 return sd;
146}
147
148/**
149 * sysfs_put_active_two - put active references to sysfs_dirent and parent
150 * @sd: sysfs_dirent of interest
151 *
152 * Put active references to @sd and its parent. This function is
153 * noop if @sd is NULL.
154 */
155void sysfs_put_active_two(struct sysfs_dirent *sd)
156{
157 if (sd) {
158 sysfs_put_active(sd);
159 sysfs_put_active(sd->s_parent);
160 }
161}
162
163/**
164 * sysfs_deactivate - deactivate sysfs_dirent
165 * @sd: sysfs_dirent to deactivate
166 *
Tejun Heo8619f972007-06-14 03:45:18 +0900167 * Deny new active references and drain existing ones.
Tejun Heob6b4a432007-06-14 03:45:18 +0900168 */
169void sysfs_deactivate(struct sysfs_dirent *sd)
170{
Tejun Heo8619f972007-06-14 03:45:18 +0900171 DECLARE_COMPLETION_ONSTACK(wait);
172 int v;
Tejun Heob6b4a432007-06-14 03:45:18 +0900173
Tejun Heo380e6fb2007-06-14 04:27:22 +0900174 BUG_ON(sd->s_sibling || !(sd->s_flags & SYSFS_FLAG_REMOVED));
Tejun Heo0c73f182007-06-14 03:45:18 +0900175 sd->s_sibling = (void *)&wait;
Tejun Heo8619f972007-06-14 03:45:18 +0900176
177 /* atomic_add_return() is a mb(), put_active() will always see
Tejun Heo0c73f182007-06-14 03:45:18 +0900178 * the updated sd->s_sibling.
Tejun Heob6b4a432007-06-14 03:45:18 +0900179 */
Tejun Heo8619f972007-06-14 03:45:18 +0900180 v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
181
182 if (v != SD_DEACTIVATED_BIAS)
183 wait_for_completion(&wait);
184
Tejun Heo0c73f182007-06-14 03:45:18 +0900185 sd->s_sibling = NULL;
Tejun Heob6b4a432007-06-14 03:45:18 +0900186}
187
Tejun Heo42b37df2007-06-14 03:45:17 +0900188static int sysfs_alloc_ino(ino_t *pino)
Tejun Heo2b611bb2007-06-14 03:45:13 +0900189{
190 int ino, rc;
191
192 retry:
193 spin_lock(&sysfs_ino_lock);
194 rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
195 spin_unlock(&sysfs_ino_lock);
196
197 if (rc == -EAGAIN) {
198 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
199 goto retry;
200 rc = -ENOMEM;
201 }
202
203 *pino = ino;
204 return rc;
205}
206
207static void sysfs_free_ino(ino_t ino)
208{
209 spin_lock(&sysfs_ino_lock);
210 ida_remove(&sysfs_ino_ida, ino);
211 spin_unlock(&sysfs_ino_lock);
212}
213
Tejun Heofa7f9122007-06-14 03:45:13 +0900214void release_sysfs_dirent(struct sysfs_dirent * sd)
215{
Tejun Heo13b30862007-06-14 03:45:14 +0900216 struct sysfs_dirent *parent_sd;
217
218 repeat:
219 parent_sd = sd->s_parent;
220
Tejun Heob402d722007-06-14 04:27:21 +0900221 if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
Tejun Heo2b29ac22007-06-14 03:45:15 +0900222 sysfs_put(sd->s_elem.symlink.target_sd);
Tejun Heob402d722007-06-14 04:27:21 +0900223 if (sysfs_type(sd) & SYSFS_COPY_NAME)
Tejun Heo0c096b52007-06-14 03:45:15 +0900224 kfree(sd->s_name);
Tejun Heofa7f9122007-06-14 03:45:13 +0900225 kfree(sd->s_iattr);
Tejun Heo2b611bb2007-06-14 03:45:13 +0900226 sysfs_free_ino(sd->s_ino);
Tejun Heofa7f9122007-06-14 03:45:13 +0900227 kmem_cache_free(sysfs_dir_cachep, sd);
Tejun Heo13b30862007-06-14 03:45:14 +0900228
229 sd = parent_sd;
230 if (sd && atomic_dec_and_test(&sd->s_count))
231 goto repeat;
Tejun Heofa7f9122007-06-14 03:45:13 +0900232}
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
235{
236 struct sysfs_dirent * sd = dentry->d_fsdata;
237
238 if (sd) {
Tejun Heodd14cbc2007-06-11 14:04:01 +0900239 /* sd->s_dentry is protected with sysfs_lock. This
240 * allows sysfs_drop_dentry() to dereference it.
241 */
242 spin_lock(&sysfs_lock);
243
244 /* The dentry might have been deleted or another
245 * lookup could have happened updating sd->s_dentry to
246 * point the new dentry. Ignore if it isn't pointing
247 * to this dentry.
248 */
249 if (sd->s_dentry == dentry)
250 sd->s_dentry = NULL;
251 spin_unlock(&sysfs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 sysfs_put(sd);
253 }
254 iput(inode);
255}
256
257static struct dentry_operations sysfs_dentry_ops = {
258 .d_iput = sysfs_d_iput,
259};
260
Tejun Heo3e519032007-06-14 03:45:15 +0900261struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Tejun Heo0c096b52007-06-14 03:45:15 +0900263 char *dup_name = NULL;
264 struct sysfs_dirent *sd = NULL;
265
266 if (type & SYSFS_COPY_NAME) {
267 name = dup_name = kstrdup(name, GFP_KERNEL);
268 if (!name)
269 goto err_out;
270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800272 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (!sd)
Tejun Heo0c096b52007-06-14 03:45:15 +0900274 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Tejun Heo0c096b52007-06-14 03:45:15 +0900276 if (sysfs_alloc_ino(&sd->s_ino))
277 goto err_out;
Tejun Heo2b611bb2007-06-14 03:45:13 +0900278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 atomic_set(&sd->s_count, 1);
Tejun Heo8619f972007-06-14 03:45:18 +0900280 atomic_set(&sd->s_active, 0);
Juha Yrjöläeea3f892006-08-03 19:06:25 +0300281 atomic_set(&sd->s_event, 1);
Tejun Heoa26cd722007-06-14 03:45:14 +0900282
Tejun Heo0c096b52007-06-14 03:45:15 +0900283 sd->s_name = name;
Tejun Heoa26cd722007-06-14 03:45:14 +0900284 sd->s_mode = mode;
Tejun Heob402d722007-06-14 04:27:21 +0900285 sd->s_flags = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 return sd;
Tejun Heo0c096b52007-06-14 03:45:15 +0900288
289 err_out:
290 kfree(dup_name);
291 kmem_cache_free(sysfs_dir_cachep, sd);
292 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
Tejun Heo198a2a82007-06-14 03:45:16 +0900295static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry)
296{
297 dentry->d_op = &sysfs_dentry_ops;
298 dentry->d_fsdata = sysfs_get(sd);
299
300 /* protect sd->s_dentry against sysfs_d_iput */
301 spin_lock(&sysfs_lock);
302 sd->s_dentry = dentry;
303 spin_unlock(&sysfs_lock);
304
305 d_rehash(dentry);
306}
307
Tejun Heoa26cd722007-06-14 03:45:14 +0900308void sysfs_attach_dirent(struct sysfs_dirent *sd,
309 struct sysfs_dirent *parent_sd, struct dentry *dentry)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700310{
Tejun Heo198a2a82007-06-14 03:45:16 +0900311 if (dentry)
312 sysfs_attach_dentry(sd, dentry);
Tejun Heoa26cd722007-06-14 03:45:14 +0900313
Tejun Heo13b30862007-06-14 03:45:14 +0900314 if (parent_sd) {
315 sd->s_parent = sysfs_get(parent_sd);
Tejun Heo0c73f182007-06-14 03:45:18 +0900316 sysfs_link_sibling(sd);
Tejun Heo13b30862007-06-14 03:45:14 +0900317 }
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700318}
319
Tejun Heof0b0af42007-06-14 04:27:22 +0900320/**
321 * sysfs_find_dirent - find sysfs_dirent with the given name
322 * @parent_sd: sysfs_dirent to search under
323 * @name: name to look for
Maneesh Sonic5168652006-03-09 19:40:14 +0530324 *
Tejun Heof0b0af42007-06-14 04:27:22 +0900325 * Look for sysfs_dirent with name @name under @parent_sd.
Maneesh Sonic5168652006-03-09 19:40:14 +0530326 *
Tejun Heof0b0af42007-06-14 04:27:22 +0900327 * LOCKING:
328 * mutex_lock(parent->i_mutex)
329 *
330 * RETURNS:
331 * Pointer to sysfs_dirent if found, NULL if not.
Maneesh Sonic5168652006-03-09 19:40:14 +0530332 */
Tejun Heof0b0af42007-06-14 04:27:22 +0900333struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
334 const unsigned char *name)
Maneesh Sonic5168652006-03-09 19:40:14 +0530335{
Tejun Heof0b0af42007-06-14 04:27:22 +0900336 struct sysfs_dirent *sd;
Maneesh Sonic5168652006-03-09 19:40:14 +0530337
Tejun Heof0b0af42007-06-14 04:27:22 +0900338 for (sd = parent_sd->s_children; sd; sd = sd->s_sibling)
339 if (sysfs_type(sd) && !strcmp(sd->s_name, name))
340 return sd;
341 return NULL;
342}
Maneesh Sonic5168652006-03-09 19:40:14 +0530343
Tejun Heof0b0af42007-06-14 04:27:22 +0900344/**
345 * sysfs_get_dirent - find and get sysfs_dirent with the given name
346 * @parent_sd: sysfs_dirent to search under
347 * @name: name to look for
348 *
349 * Look for sysfs_dirent with name @name under @parent_sd and get
350 * it if found.
351 *
352 * LOCKING:
353 * Kernel thread context (may sleep)
354 *
355 * RETURNS:
356 * Pointer to sysfs_dirent if found, NULL if not.
357 */
358struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
359 const unsigned char *name)
360{
361 struct sysfs_dirent *sd;
362
363 mutex_lock(&parent_sd->s_dentry->d_inode->i_mutex);
364 sd = sysfs_find_dirent(parent_sd, name);
365 sysfs_get(sd);
366 mutex_unlock(&parent_sd->s_dentry->d_inode->i_mutex);
367
368 return sd;
Maneesh Sonic5168652006-03-09 19:40:14 +0530369}
370
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900371static int create_dir(struct kobject *kobj, struct dentry *parent,
372 const char *name, struct dentry **p_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 int error;
375 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900376 struct dentry *dentry;
Tejun Heofc9f54b2007-06-14 03:45:17 +0900377 struct inode *inode;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900378 struct sysfs_dirent *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900380 mutex_lock(&parent->d_inode->i_mutex);
381
Tejun Heofc9f54b2007-06-14 03:45:17 +0900382 /* allocate */
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900383 dentry = lookup_one_len(name, parent, strlen(name));
384 if (IS_ERR(dentry)) {
385 error = PTR_ERR(dentry);
386 goto out_unlock;
387 }
388
389 error = -EEXIST;
Tejun Heofc9f54b2007-06-14 03:45:17 +0900390 if (dentry->d_inode)
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900391 goto out_dput;
392
Tejun Heoa26cd722007-06-14 03:45:14 +0900393 error = -ENOMEM;
Tejun Heo3e519032007-06-14 03:45:15 +0900394 sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
Tejun Heoa26cd722007-06-14 03:45:14 +0900395 if (!sd)
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900396 goto out_drop;
Tejun Heo3e519032007-06-14 03:45:15 +0900397 sd->s_elem.dir.kobj = kobj;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900398
Tejun Heo8312a8d2007-06-14 03:45:17 +0900399 inode = sysfs_get_inode(sd);
Tejun Heofc9f54b2007-06-14 03:45:17 +0900400 if (!inode)
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900401 goto out_sput;
402
Tejun Heo8312a8d2007-06-14 03:45:17 +0900403 if (inode->i_state & I_NEW) {
404 inode->i_op = &sysfs_dir_inode_operations;
405 inode->i_fop = &sysfs_dir_operations;
406 /* directory inodes start off with i_nlink == 2 (for ".") */
407 inc_nlink(inode);
408 }
Tejun Heofc9f54b2007-06-14 03:45:17 +0900409
410 /* link in */
411 error = -EEXIST;
Tejun Heof0b0af42007-06-14 04:27:22 +0900412 if (sysfs_find_dirent(parent->d_fsdata, name))
Tejun Heofc9f54b2007-06-14 03:45:17 +0900413 goto out_iput;
414
415 sysfs_instantiate(dentry, inode);
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900416 inc_nlink(parent->d_inode);
Tejun Heo198a2a82007-06-14 03:45:16 +0900417 sysfs_attach_dirent(sd, parent->d_fsdata, dentry);
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900418
419 *p_dentry = dentry;
420 error = 0;
Tejun Heofc9f54b2007-06-14 03:45:17 +0900421 goto out_unlock; /* pin directory dentry in core */
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900422
Tejun Heofc9f54b2007-06-14 03:45:17 +0900423 out_iput:
424 iput(inode);
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900425 out_sput:
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900426 sysfs_put(sd);
427 out_drop:
428 d_drop(dentry);
429 out_dput:
430 dput(dentry);
431 out_unlock:
432 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return error;
434}
435
436
437int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d)
438{
439 return create_dir(k,k->dentry,n,d);
440}
441
442/**
443 * sysfs_create_dir - create a directory for an object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 * @kobj: object we're creating directory for.
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700445 * @shadow_parent: parent parent object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 */
447
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700448int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 struct dentry * dentry = NULL;
451 struct dentry * parent;
452 int error = 0;
453
454 BUG_ON(!kobj);
455
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700456 if (shadow_parent)
457 parent = shadow_parent;
458 else if (kobj->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 parent = kobj->parent->dentry;
460 else if (sysfs_mount && sysfs_mount->mnt_sb)
461 parent = sysfs_mount->mnt_sb->s_root;
462 else
463 return -EFAULT;
464
465 error = create_dir(kobj,parent,kobject_name(kobj),&dentry);
466 if (!error)
467 kobj->dentry = dentry;
468 return error;
469}
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
472 struct nameidata *nd)
473{
474 struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
475 struct sysfs_dirent * sd;
Tejun Heob402d722007-06-14 04:27:21 +0900476 struct bin_attribute *bin_attr;
Tejun Heofc9f54b2007-06-14 03:45:17 +0900477 struct inode *inode;
478 int found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Tejun Heo0c73f182007-06-14 03:45:18 +0900480 for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) {
Tejun Heob402d722007-06-14 04:27:21 +0900481 if ((sysfs_type(sd) & SYSFS_NOT_PINNED) &&
Tejun Heofc9f54b2007-06-14 03:45:17 +0900482 !strcmp(sd->s_name, dentry->d_name.name)) {
483 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 break;
485 }
486 }
487
Tejun Heofc9f54b2007-06-14 03:45:17 +0900488 /* no such entry */
489 if (!found)
490 return NULL;
491
492 /* attach dentry and inode */
Tejun Heo8312a8d2007-06-14 03:45:17 +0900493 inode = sysfs_get_inode(sd);
Tejun Heofc9f54b2007-06-14 03:45:17 +0900494 if (!inode)
495 return ERR_PTR(-ENOMEM);
496
Tejun Heo8312a8d2007-06-14 03:45:17 +0900497 if (inode->i_state & I_NEW) {
498 /* initialize inode according to type */
Tejun Heob402d722007-06-14 04:27:21 +0900499 switch (sysfs_type(sd)) {
500 case SYSFS_KOBJ_ATTR:
Tejun Heo8312a8d2007-06-14 03:45:17 +0900501 inode->i_size = PAGE_SIZE;
502 inode->i_fop = &sysfs_file_operations;
Tejun Heob402d722007-06-14 04:27:21 +0900503 break;
504 case SYSFS_KOBJ_BIN_ATTR:
505 bin_attr = sd->s_elem.bin_attr.bin_attr;
Tejun Heo8312a8d2007-06-14 03:45:17 +0900506 inode->i_size = bin_attr->size;
507 inode->i_fop = &bin_fops;
Tejun Heob402d722007-06-14 04:27:21 +0900508 break;
509 case SYSFS_KOBJ_LINK:
Tejun Heo8312a8d2007-06-14 03:45:17 +0900510 inode->i_op = &sysfs_symlink_inode_operations;
Tejun Heob402d722007-06-14 04:27:21 +0900511 break;
512 default:
513 BUG();
514 }
Tejun Heo8312a8d2007-06-14 03:45:17 +0900515 }
Tejun Heofc9f54b2007-06-14 03:45:17 +0900516
517 sysfs_instantiate(dentry, inode);
518 sysfs_attach_dentry(sd, dentry);
519
520 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800523const struct inode_operations sysfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 .lookup = sysfs_lookup,
Maneesh Soni988d1862005-05-31 10:39:14 +0530525 .setattr = sysfs_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526};
527
528static void remove_dir(struct dentry * d)
529{
Tejun Heodbde0fc2007-06-14 03:45:16 +0900530 struct dentry *parent = d->d_parent;
531 struct sysfs_dirent *sd = d->d_fsdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800533 mutex_lock(&parent->d_inode->i_mutex);
Tejun Heodbde0fc2007-06-14 03:45:16 +0900534
Tejun Heo0c73f182007-06-14 03:45:18 +0900535 sysfs_unlink_sibling(sd);
Tejun Heo380e6fb2007-06-14 04:27:22 +0900536 sd->s_flags |= SYSFS_FLAG_REMOVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 pr_debug(" o %s removing done (%d)\n",d->d_name.name,
539 atomic_read(&d->d_count));
540
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800541 mutex_unlock(&parent->d_inode->i_mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900542
Tejun Heodbde0fc2007-06-14 03:45:16 +0900543 sysfs_drop_dentry(sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900544 sysfs_deactivate(sd);
545 sysfs_put(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
548void sysfs_remove_subdir(struct dentry * d)
549{
550 remove_dir(d);
551}
552
553
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700554static void __sysfs_remove_dir(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Tejun Heo0c73f182007-06-14 03:45:18 +0900556 struct sysfs_dirent *removed = NULL;
557 struct sysfs_dirent *parent_sd;
558 struct sysfs_dirent **pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 if (!dentry)
561 return;
562
563 pr_debug("sysfs %s: removing dir\n",dentry->d_name.name);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800564 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 parent_sd = dentry->d_fsdata;
Tejun Heo0c73f182007-06-14 03:45:18 +0900566 pos = &parent_sd->s_children;
567 while (*pos) {
568 struct sysfs_dirent *sd = *pos;
569
Tejun Heob402d722007-06-14 04:27:21 +0900570 if (sysfs_type(sd) && (sysfs_type(sd) & SYSFS_NOT_PINNED)) {
Tejun Heo380e6fb2007-06-14 04:27:22 +0900571 sd->s_flags |= SYSFS_FLAG_REMOVED;
Tejun Heo0c73f182007-06-14 03:45:18 +0900572 *pos = sd->s_sibling;
573 sd->s_sibling = removed;
574 removed = sd;
575 } else
576 pos = &(*pos)->s_sibling;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800578 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Tejun Heo0c73f182007-06-14 03:45:18 +0900580 while (removed) {
581 struct sysfs_dirent *sd = removed;
582
583 removed = sd->s_sibling;
584 sd->s_sibling = NULL;
585
Tejun Heodbde0fc2007-06-14 03:45:16 +0900586 sysfs_drop_dentry(sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900587 sysfs_deactivate(sd);
588 sysfs_put(sd);
589 }
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 remove_dir(dentry);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700592}
593
594/**
595 * sysfs_remove_dir - remove an object's directory.
596 * @kobj: object.
597 *
598 * The only thing special about this is that we remove any files in
599 * the directory before we remove the directory, and we've inlined
600 * what used to be sysfs_rmdir() below, instead of calling separately.
601 */
602
603void sysfs_remove_dir(struct kobject * kobj)
604{
Tejun Heoaecdced2007-06-14 03:45:15 +0900605 struct dentry *d = kobj->dentry;
606
607 spin_lock(&kobj_sysfs_assoc_lock);
Greg Kroah-Hartman641e6f32006-03-16 15:44:26 -0800608 kobj->dentry = NULL;
Tejun Heoaecdced2007-06-14 03:45:15 +0900609 spin_unlock(&kobj_sysfs_assoc_lock);
610
611 __sysfs_remove_dir(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700614int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent,
615 const char *new_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Tejun Heo0c096b52007-06-14 03:45:15 +0900617 struct sysfs_dirent *sd = kobj->dentry->d_fsdata;
618 struct sysfs_dirent *parent_sd = new_parent->d_fsdata;
619 struct dentry *new_dentry;
620 char *dup_name;
Tejun Heo996b7372007-06-14 03:45:14 +0900621 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700623 if (!new_parent)
624 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 down_write(&sysfs_rename_sem);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700627 mutex_lock(&new_parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700629 new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
Tejun Heo996b7372007-06-14 03:45:14 +0900630 if (IS_ERR(new_dentry)) {
631 error = PTR_ERR(new_dentry);
632 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
Tejun Heo996b7372007-06-14 03:45:14 +0900634
635 /* By allowing two different directories with the same
636 * d_parent we allow this routine to move between different
637 * shadows of the same directory
638 */
639 error = -EINVAL;
640 if (kobj->dentry->d_parent->d_inode != new_parent->d_inode ||
641 new_dentry->d_parent->d_inode != new_parent->d_inode ||
642 new_dentry == kobj->dentry)
643 goto out_dput;
644
645 error = -EEXIST;
646 if (new_dentry->d_inode)
647 goto out_dput;
648
Tejun Heo0c096b52007-06-14 03:45:15 +0900649 /* rename kobject and sysfs_dirent */
650 error = -ENOMEM;
651 new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
652 if (!new_name)
Tejun Heo996b7372007-06-14 03:45:14 +0900653 goto out_drop;
654
Tejun Heo0c096b52007-06-14 03:45:15 +0900655 error = kobject_set_name(kobj, "%s", new_name);
656 if (error)
657 goto out_free;
658
659 kfree(sd->s_name);
660 sd->s_name = new_name;
661
662 /* move under the new parent */
Tejun Heo996b7372007-06-14 03:45:14 +0900663 d_add(new_dentry, NULL);
664 d_move(kobj->dentry, new_dentry);
665
Tejun Heo0c73f182007-06-14 03:45:18 +0900666 sysfs_unlink_sibling(sd);
Tejun Heo7f7cfff2007-06-14 03:45:17 +0900667 sysfs_get(parent_sd);
668 sysfs_put(sd->s_parent);
669 sd->s_parent = parent_sd;
Tejun Heo0c73f182007-06-14 03:45:18 +0900670 sysfs_link_sibling(sd);
Tejun Heo996b7372007-06-14 03:45:14 +0900671
672 error = 0;
673 goto out_unlock;
674
Tejun Heo0c096b52007-06-14 03:45:15 +0900675 out_free:
676 kfree(dup_name);
Tejun Heo996b7372007-06-14 03:45:14 +0900677 out_drop:
678 d_drop(new_dentry);
679 out_dput:
680 dput(new_dentry);
681 out_unlock:
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700682 mutex_unlock(&new_parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 up_write(&sysfs_rename_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return error;
685}
686
Cornelia Huck8a824722006-11-20 17:07:51 +0100687int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
688{
689 struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
690 struct sysfs_dirent *new_parent_sd, *sd;
691 int error;
692
Cornelia Huck8a824722006-11-20 17:07:51 +0100693 old_parent_dentry = kobj->parent ?
694 kobj->parent->dentry : sysfs_mount->mnt_sb->s_root;
Cornelia Huckc744aea2007-01-08 20:16:44 +0100695 new_parent_dentry = new_parent ?
696 new_parent->dentry : sysfs_mount->mnt_sb->s_root;
Cornelia Huck8a824722006-11-20 17:07:51 +0100697
Mark Lord0de15172007-03-06 01:42:03 -0800698 if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
699 return 0; /* nothing to move */
Cornelia Huck8a824722006-11-20 17:07:51 +0100700again:
701 mutex_lock(&old_parent_dentry->d_inode->i_mutex);
702 if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
703 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
704 goto again;
705 }
706
707 new_parent_sd = new_parent_dentry->d_fsdata;
708 sd = kobj->dentry->d_fsdata;
709
710 new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
711 strlen(kobj->name));
712 if (IS_ERR(new_dentry)) {
713 error = PTR_ERR(new_dentry);
714 goto out;
715 } else
716 error = 0;
717 d_add(new_dentry, NULL);
718 d_move(kobj->dentry, new_dentry);
719 dput(new_dentry);
720
721 /* Remove from old parent's list and insert into new parent's list. */
Tejun Heo0c73f182007-06-14 03:45:18 +0900722 sysfs_unlink_sibling(sd);
Tejun Heo7f7cfff2007-06-14 03:45:17 +0900723 sysfs_get(new_parent_sd);
724 sysfs_put(sd->s_parent);
725 sd->s_parent = new_parent_sd;
Tejun Heo0c73f182007-06-14 03:45:18 +0900726 sysfs_link_sibling(sd);
Cornelia Huck8a824722006-11-20 17:07:51 +0100727
728out:
729 mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
730 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
731
732 return error;
733}
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735static int sysfs_dir_open(struct inode *inode, struct file *file)
736{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800737 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
Tejun Heoa26cd722007-06-14 03:45:14 +0900739 struct sysfs_dirent * sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800741 mutex_lock(&dentry->d_inode->i_mutex);
Tejun Heo3e519032007-06-14 03:45:15 +0900742 sd = sysfs_new_dirent("_DIR_", 0, 0);
Tejun Heoa26cd722007-06-14 03:45:14 +0900743 if (sd)
744 sysfs_attach_dirent(sd, parent_sd, NULL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800745 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Tejun Heoa26cd722007-06-14 03:45:14 +0900747 file->private_data = sd;
748 return sd ? 0 : -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
751static int sysfs_dir_close(struct inode *inode, struct file *file)
752{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800753 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 struct sysfs_dirent * cursor = file->private_data;
755
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800756 mutex_lock(&dentry->d_inode->i_mutex);
Tejun Heo0c73f182007-06-14 03:45:18 +0900757 sysfs_unlink_sibling(cursor);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800758 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 release_sysfs_dirent(cursor);
761
762 return 0;
763}
764
765/* Relationship between s_mode and the DT_xxx types */
766static inline unsigned char dt_type(struct sysfs_dirent *sd)
767{
768 return (sd->s_mode >> 12) & 15;
769}
770
771static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
772{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800773 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
775 struct sysfs_dirent *cursor = filp->private_data;
Tejun Heo0c73f182007-06-14 03:45:18 +0900776 struct sysfs_dirent **pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 ino_t ino;
778 int i = filp->f_pos;
779
780 switch (i) {
781 case 0:
Eric Sandeendc351252007-06-11 14:02:45 +0900782 ino = parent_sd->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
784 break;
785 filp->f_pos++;
786 i++;
787 /* fallthrough */
788 case 1:
Tejun Heo13b30862007-06-14 03:45:14 +0900789 if (parent_sd->s_parent)
790 ino = parent_sd->s_parent->s_ino;
791 else
792 ino = parent_sd->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
794 break;
795 filp->f_pos++;
796 i++;
797 /* fallthrough */
798 default:
Tejun Heo0c73f182007-06-14 03:45:18 +0900799 pos = &parent_sd->s_children;
800 while (*pos != cursor)
801 pos = &(*pos)->s_sibling;
Akinobu Mita1bfba4e2006-06-26 00:24:40 -0700802
Tejun Heo0c73f182007-06-14 03:45:18 +0900803 /* unlink cursor */
804 *pos = cursor->s_sibling;
805
806 if (filp->f_pos == 2)
807 pos = &parent_sd->s_children;
808
809 for ( ; *pos; pos = &(*pos)->s_sibling) {
810 struct sysfs_dirent *next = *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 const char * name;
812 int len;
813
Tejun Heob402d722007-06-14 04:27:21 +0900814 if (!sysfs_type(next))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 continue;
816
Tejun Heo0c096b52007-06-14 03:45:15 +0900817 name = next->s_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 len = strlen(name);
Eric Sandeendc351252007-06-11 14:02:45 +0900819 ino = next->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 if (filldir(dirent, name, len, filp->f_pos, ino,
822 dt_type(next)) < 0)
Tejun Heo0c73f182007-06-14 03:45:18 +0900823 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 filp->f_pos++;
826 }
Tejun Heo0c73f182007-06-14 03:45:18 +0900827
828 /* put cursor back in */
829 cursor->s_sibling = *pos;
830 *pos = cursor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832 return 0;
833}
834
835static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
836{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800837 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800839 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 switch (origin) {
841 case 1:
842 offset += file->f_pos;
843 case 0:
844 if (offset >= 0)
845 break;
846 default:
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800847 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return -EINVAL;
849 }
850 if (offset != file->f_pos) {
851 file->f_pos = offset;
852 if (file->f_pos >= 2) {
853 struct sysfs_dirent *sd = dentry->d_fsdata;
854 struct sysfs_dirent *cursor = file->private_data;
Tejun Heo0c73f182007-06-14 03:45:18 +0900855 struct sysfs_dirent **pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 loff_t n = file->f_pos - 2;
857
Tejun Heo0c73f182007-06-14 03:45:18 +0900858 sysfs_unlink_sibling(cursor);
859
860 pos = &sd->s_children;
861 while (n && *pos) {
862 struct sysfs_dirent *next = *pos;
Tejun Heob402d722007-06-14 04:27:21 +0900863 if (sysfs_type(next))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 n--;
Tejun Heo0c73f182007-06-14 03:45:18 +0900865 pos = &(*pos)->s_sibling;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
Tejun Heo0c73f182007-06-14 03:45:18 +0900867
868 cursor->s_sibling = *pos;
869 *pos = cursor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
871 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800872 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return offset;
874}
875
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700876
877/**
878 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
879 * @kobj: object we're creating shadow of.
880 */
881
882int sysfs_make_shadowed_dir(struct kobject *kobj,
883 void * (*follow_link)(struct dentry *, struct nameidata *))
884{
885 struct inode *inode;
886 struct inode_operations *i_op;
887
888 inode = kobj->dentry->d_inode;
889 if (inode->i_op != &sysfs_dir_inode_operations)
890 return -EINVAL;
891
892 i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
893 if (!i_op)
894 return -ENOMEM;
895
896 memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
897 i_op->follow_link = follow_link;
898
899 /* Locking of inode->i_op?
900 * Since setting i_op is a single word write and they
901 * are atomic we should be ok here.
902 */
903 inode->i_op = i_op;
904 return 0;
905}
906
907/**
908 * sysfs_create_shadow_dir - create a shadow directory for an object.
909 * @kobj: object we're creating directory for.
910 *
911 * sysfs_make_shadowed_dir must already have been called on this
912 * directory.
913 */
914
915struct dentry *sysfs_create_shadow_dir(struct kobject *kobj)
916{
Tejun Heo13b30862007-06-14 03:45:14 +0900917 struct dentry *dir = kobj->dentry;
918 struct inode *inode = dir->d_inode;
919 struct dentry *parent = dir->d_parent;
920 struct sysfs_dirent *parent_sd = parent->d_fsdata;
921 struct dentry *shadow;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700922 struct sysfs_dirent *sd;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700923
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700924 shadow = ERR_PTR(-EINVAL);
925 if (!sysfs_is_shadowed_inode(inode))
926 goto out;
927
928 shadow = d_alloc(parent, &dir->d_name);
929 if (!shadow)
930 goto nomem;
931
Tejun Heo3e519032007-06-14 03:45:15 +0900932 sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700933 if (!sd)
934 goto nomem;
Tejun Heo3e519032007-06-14 03:45:15 +0900935 sd->s_elem.dir.kobj = kobj;
Tejun Heo13b30862007-06-14 03:45:14 +0900936 /* point to parent_sd but don't attach to it */
937 sd->s_parent = sysfs_get(parent_sd);
Tejun Heoa26cd722007-06-14 03:45:14 +0900938 sysfs_attach_dirent(sd, NULL, shadow);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700939
940 d_instantiate(shadow, igrab(inode));
941 inc_nlink(inode);
942 inc_nlink(parent->d_inode);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700943
944 dget(shadow); /* Extra count - pin the dentry in core */
945
946out:
947 return shadow;
948nomem:
949 dput(shadow);
950 shadow = ERR_PTR(-ENOMEM);
951 goto out;
952}
953
954/**
955 * sysfs_remove_shadow_dir - remove an object's directory.
956 * @shadow: dentry of shadow directory
957 *
958 * The only thing special about this is that we remove any files in
959 * the directory before we remove the directory, and we've inlined
960 * what used to be sysfs_rmdir() below, instead of calling separately.
961 */
962
963void sysfs_remove_shadow_dir(struct dentry *shadow)
964{
965 __sysfs_remove_dir(shadow);
966}
967
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800968const struct file_operations sysfs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 .open = sysfs_dir_open,
970 .release = sysfs_dir_close,
971 .llseek = sysfs_dir_lseek,
972 .read = generic_read_dir,
973 .readdir = sysfs_readdir,
974};