blob: 1b5643407a9571429951233e8bce44f43a95534b [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 Heo5f995322007-06-14 04:27:23 +090018spinlock_t 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 Heob6b4a432007-06-14 03:45:18 +090023/**
Tejun Heo0c73f182007-06-14 03:45:18 +090024 * sysfs_link_sibling - link sysfs_dirent into sibling list
25 * @sd: sysfs_dirent of interest
26 *
27 * Link @sd into its sibling list which starts from
28 * sd->s_parent->s_children.
29 *
30 * Locking:
31 * mutex_lock(sd->s_parent->dentry->d_inode->i_mutex)
32 */
33static void sysfs_link_sibling(struct sysfs_dirent *sd)
34{
35 struct sysfs_dirent *parent_sd = sd->s_parent;
36
37 BUG_ON(sd->s_sibling);
38 sd->s_sibling = parent_sd->s_children;
39 parent_sd->s_children = sd;
40}
41
42/**
43 * sysfs_unlink_sibling - unlink sysfs_dirent from sibling list
44 * @sd: sysfs_dirent of interest
45 *
46 * Unlink @sd from its sibling list which starts from
47 * sd->s_parent->s_children.
48 *
49 * Locking:
50 * mutex_lock(sd->s_parent->dentry->d_inode->i_mutex)
51 */
52static void sysfs_unlink_sibling(struct sysfs_dirent *sd)
53{
54 struct sysfs_dirent **pos;
55
56 for (pos = &sd->s_parent->s_children; *pos; pos = &(*pos)->s_sibling) {
57 if (*pos == sd) {
58 *pos = sd->s_sibling;
59 sd->s_sibling = NULL;
60 break;
61 }
62 }
63}
64
65/**
Tejun Heob6b4a432007-06-14 03:45:18 +090066 * sysfs_get_active - get an active reference to sysfs_dirent
67 * @sd: sysfs_dirent to get an active reference to
68 *
69 * Get an active reference of @sd. This function is noop if @sd
70 * is NULL.
71 *
72 * RETURNS:
73 * Pointer to @sd on success, NULL on failure.
74 */
75struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd)
76{
Tejun Heo8619f972007-06-14 03:45:18 +090077 if (unlikely(!sd))
78 return NULL;
79
80 while (1) {
81 int v, t;
82
83 v = atomic_read(&sd->s_active);
84 if (unlikely(v < 0))
85 return NULL;
86
87 t = atomic_cmpxchg(&sd->s_active, v, v + 1);
88 if (likely(t == v))
89 return sd;
90 if (t < 0)
91 return NULL;
92
93 cpu_relax();
Tejun Heob6b4a432007-06-14 03:45:18 +090094 }
Tejun Heob6b4a432007-06-14 03:45:18 +090095}
96
97/**
98 * sysfs_put_active - put an active reference to sysfs_dirent
99 * @sd: sysfs_dirent to put an active reference to
100 *
101 * Put an active reference to @sd. This function is noop if @sd
102 * is NULL.
103 */
104void sysfs_put_active(struct sysfs_dirent *sd)
105{
Tejun Heo8619f972007-06-14 03:45:18 +0900106 struct completion *cmpl;
107 int v;
108
109 if (unlikely(!sd))
110 return;
111
112 v = atomic_dec_return(&sd->s_active);
113 if (likely(v != SD_DEACTIVATED_BIAS))
114 return;
115
116 /* atomic_dec_return() is a mb(), we'll always see the updated
Tejun Heo0c73f182007-06-14 03:45:18 +0900117 * sd->s_sibling.
Tejun Heo8619f972007-06-14 03:45:18 +0900118 */
Tejun Heo0c73f182007-06-14 03:45:18 +0900119 cmpl = (void *)sd->s_sibling;
Tejun Heo8619f972007-06-14 03:45:18 +0900120 complete(cmpl);
Tejun Heob6b4a432007-06-14 03:45:18 +0900121}
122
123/**
124 * sysfs_get_active_two - get active references to sysfs_dirent and parent
125 * @sd: sysfs_dirent of interest
126 *
127 * Get active reference to @sd and its parent. Parent's active
128 * reference is grabbed first. This function is noop if @sd is
129 * NULL.
130 *
131 * RETURNS:
132 * Pointer to @sd on success, NULL on failure.
133 */
134struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd)
135{
136 if (sd) {
137 if (sd->s_parent && unlikely(!sysfs_get_active(sd->s_parent)))
138 return NULL;
139 if (unlikely(!sysfs_get_active(sd))) {
140 sysfs_put_active(sd->s_parent);
141 return NULL;
142 }
143 }
144 return sd;
145}
146
147/**
148 * sysfs_put_active_two - put active references to sysfs_dirent and parent
149 * @sd: sysfs_dirent of interest
150 *
151 * Put active references to @sd and its parent. This function is
152 * noop if @sd is NULL.
153 */
154void sysfs_put_active_two(struct sysfs_dirent *sd)
155{
156 if (sd) {
157 sysfs_put_active(sd);
158 sysfs_put_active(sd->s_parent);
159 }
160}
161
162/**
163 * sysfs_deactivate - deactivate sysfs_dirent
164 * @sd: sysfs_dirent to deactivate
165 *
Tejun Heo8619f972007-06-14 03:45:18 +0900166 * Deny new active references and drain existing ones.
Tejun Heob6b4a432007-06-14 03:45:18 +0900167 */
168void sysfs_deactivate(struct sysfs_dirent *sd)
169{
Tejun Heo8619f972007-06-14 03:45:18 +0900170 DECLARE_COMPLETION_ONSTACK(wait);
171 int v;
Tejun Heob6b4a432007-06-14 03:45:18 +0900172
Tejun Heo380e6fb2007-06-14 04:27:22 +0900173 BUG_ON(sd->s_sibling || !(sd->s_flags & SYSFS_FLAG_REMOVED));
Tejun Heo0c73f182007-06-14 03:45:18 +0900174 sd->s_sibling = (void *)&wait;
Tejun Heo8619f972007-06-14 03:45:18 +0900175
176 /* atomic_add_return() is a mb(), put_active() will always see
Tejun Heo0c73f182007-06-14 03:45:18 +0900177 * the updated sd->s_sibling.
Tejun Heob6b4a432007-06-14 03:45:18 +0900178 */
Tejun Heo8619f972007-06-14 03:45:18 +0900179 v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active);
180
181 if (v != SD_DEACTIVATED_BIAS)
182 wait_for_completion(&wait);
183
Tejun Heo0c73f182007-06-14 03:45:18 +0900184 sd->s_sibling = NULL;
Tejun Heob6b4a432007-06-14 03:45:18 +0900185}
186
Tejun Heo42b37df2007-06-14 03:45:17 +0900187static int sysfs_alloc_ino(ino_t *pino)
Tejun Heo2b611bb2007-06-14 03:45:13 +0900188{
189 int ino, rc;
190
191 retry:
192 spin_lock(&sysfs_ino_lock);
193 rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
194 spin_unlock(&sysfs_ino_lock);
195
196 if (rc == -EAGAIN) {
197 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL))
198 goto retry;
199 rc = -ENOMEM;
200 }
201
202 *pino = ino;
203 return rc;
204}
205
206static void sysfs_free_ino(ino_t ino)
207{
208 spin_lock(&sysfs_ino_lock);
209 ida_remove(&sysfs_ino_ida, ino);
210 spin_unlock(&sysfs_ino_lock);
211}
212
Tejun Heofa7f9122007-06-14 03:45:13 +0900213void release_sysfs_dirent(struct sysfs_dirent * sd)
214{
Tejun Heo13b30862007-06-14 03:45:14 +0900215 struct sysfs_dirent *parent_sd;
216
217 repeat:
218 parent_sd = sd->s_parent;
219
Tejun Heob402d722007-06-14 04:27:21 +0900220 if (sysfs_type(sd) == SYSFS_KOBJ_LINK)
Tejun Heo2b29ac22007-06-14 03:45:15 +0900221 sysfs_put(sd->s_elem.symlink.target_sd);
Tejun Heob402d722007-06-14 04:27:21 +0900222 if (sysfs_type(sd) & SYSFS_COPY_NAME)
Tejun Heo0c096b52007-06-14 03:45:15 +0900223 kfree(sd->s_name);
Tejun Heofa7f9122007-06-14 03:45:13 +0900224 kfree(sd->s_iattr);
Tejun Heo2b611bb2007-06-14 03:45:13 +0900225 sysfs_free_ino(sd->s_ino);
Tejun Heofa7f9122007-06-14 03:45:13 +0900226 kmem_cache_free(sysfs_dir_cachep, sd);
Tejun Heo13b30862007-06-14 03:45:14 +0900227
228 sd = parent_sd;
229 if (sd && atomic_dec_and_test(&sd->s_count))
230 goto repeat;
Tejun Heofa7f9122007-06-14 03:45:13 +0900231}
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233static void sysfs_d_iput(struct dentry * dentry, struct inode * inode)
234{
235 struct sysfs_dirent * sd = dentry->d_fsdata;
236
237 if (sd) {
Tejun Heo5f995322007-06-14 04:27:23 +0900238 /* sd->s_dentry is protected with sysfs_assoc_lock.
239 * This allows sysfs_drop_dentry() to dereference it.
Tejun Heodd14cbc2007-06-11 14:04:01 +0900240 */
Tejun Heo5f995322007-06-14 04:27:23 +0900241 spin_lock(&sysfs_assoc_lock);
Tejun Heodd14cbc2007-06-11 14:04:01 +0900242
243 /* The dentry might have been deleted or another
244 * lookup could have happened updating sd->s_dentry to
245 * point the new dentry. Ignore if it isn't pointing
246 * to this dentry.
247 */
248 if (sd->s_dentry == dentry)
249 sd->s_dentry = NULL;
Tejun Heo5f995322007-06-14 04:27:23 +0900250 spin_unlock(&sysfs_assoc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 sysfs_put(sd);
252 }
253 iput(inode);
254}
255
256static struct dentry_operations sysfs_dentry_ops = {
257 .d_iput = sysfs_d_iput,
258};
259
Tejun Heo3e519032007-06-14 03:45:15 +0900260struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Tejun Heo0c096b52007-06-14 03:45:15 +0900262 char *dup_name = NULL;
263 struct sysfs_dirent *sd = NULL;
264
265 if (type & SYSFS_COPY_NAME) {
266 name = dup_name = kstrdup(name, GFP_KERNEL);
267 if (!name)
268 goto err_out;
269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800271 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (!sd)
Tejun Heo0c096b52007-06-14 03:45:15 +0900273 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Tejun Heo0c096b52007-06-14 03:45:15 +0900275 if (sysfs_alloc_ino(&sd->s_ino))
276 goto err_out;
Tejun Heo2b611bb2007-06-14 03:45:13 +0900277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 atomic_set(&sd->s_count, 1);
Tejun Heo8619f972007-06-14 03:45:18 +0900279 atomic_set(&sd->s_active, 0);
Juha Yrjöläeea3f892006-08-03 19:06:25 +0300280 atomic_set(&sd->s_event, 1);
Tejun Heoa26cd722007-06-14 03:45:14 +0900281
Tejun Heo0c096b52007-06-14 03:45:15 +0900282 sd->s_name = name;
Tejun Heoa26cd722007-06-14 03:45:14 +0900283 sd->s_mode = mode;
Tejun Heob402d722007-06-14 04:27:21 +0900284 sd->s_flags = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 return sd;
Tejun Heo0c096b52007-06-14 03:45:15 +0900287
288 err_out:
289 kfree(dup_name);
290 kmem_cache_free(sysfs_dir_cachep, sd);
291 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
Tejun Heo198a2a82007-06-14 03:45:16 +0900294static void sysfs_attach_dentry(struct sysfs_dirent *sd, struct dentry *dentry)
295{
296 dentry->d_op = &sysfs_dentry_ops;
297 dentry->d_fsdata = sysfs_get(sd);
298
299 /* protect sd->s_dentry against sysfs_d_iput */
Tejun Heo5f995322007-06-14 04:27:23 +0900300 spin_lock(&sysfs_assoc_lock);
Tejun Heo198a2a82007-06-14 03:45:16 +0900301 sd->s_dentry = dentry;
Tejun Heo5f995322007-06-14 04:27:23 +0900302 spin_unlock(&sysfs_assoc_lock);
Tejun Heo198a2a82007-06-14 03:45:16 +0900303
304 d_rehash(dentry);
305}
306
Tejun Heoa26cd722007-06-14 03:45:14 +0900307void sysfs_attach_dirent(struct sysfs_dirent *sd,
308 struct sysfs_dirent *parent_sd, struct dentry *dentry)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700309{
Tejun Heo198a2a82007-06-14 03:45:16 +0900310 if (dentry)
311 sysfs_attach_dentry(sd, dentry);
Tejun Heoa26cd722007-06-14 03:45:14 +0900312
Tejun Heo13b30862007-06-14 03:45:14 +0900313 if (parent_sd) {
314 sd->s_parent = sysfs_get(parent_sd);
Tejun Heo0c73f182007-06-14 03:45:18 +0900315 sysfs_link_sibling(sd);
Tejun Heo13b30862007-06-14 03:45:14 +0900316 }
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700317}
318
Tejun Heof0b0af42007-06-14 04:27:22 +0900319/**
320 * sysfs_find_dirent - find sysfs_dirent with the given name
321 * @parent_sd: sysfs_dirent to search under
322 * @name: name to look for
Maneesh Sonic5168652006-03-09 19:40:14 +0530323 *
Tejun Heof0b0af42007-06-14 04:27:22 +0900324 * Look for sysfs_dirent with name @name under @parent_sd.
Maneesh Sonic5168652006-03-09 19:40:14 +0530325 *
Tejun Heof0b0af42007-06-14 04:27:22 +0900326 * LOCKING:
327 * mutex_lock(parent->i_mutex)
328 *
329 * RETURNS:
330 * Pointer to sysfs_dirent if found, NULL if not.
Maneesh Sonic5168652006-03-09 19:40:14 +0530331 */
Tejun Heof0b0af42007-06-14 04:27:22 +0900332struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd,
333 const unsigned char *name)
Maneesh Sonic5168652006-03-09 19:40:14 +0530334{
Tejun Heof0b0af42007-06-14 04:27:22 +0900335 struct sysfs_dirent *sd;
Maneesh Sonic5168652006-03-09 19:40:14 +0530336
Tejun Heof0b0af42007-06-14 04:27:22 +0900337 for (sd = parent_sd->s_children; sd; sd = sd->s_sibling)
338 if (sysfs_type(sd) && !strcmp(sd->s_name, name))
339 return sd;
340 return NULL;
341}
Maneesh Sonic5168652006-03-09 19:40:14 +0530342
Tejun Heof0b0af42007-06-14 04:27:22 +0900343/**
344 * sysfs_get_dirent - find and get sysfs_dirent with the given name
345 * @parent_sd: sysfs_dirent to search under
346 * @name: name to look for
347 *
348 * Look for sysfs_dirent with name @name under @parent_sd and get
349 * it if found.
350 *
351 * LOCKING:
352 * Kernel thread context (may sleep)
353 *
354 * RETURNS:
355 * Pointer to sysfs_dirent if found, NULL if not.
356 */
357struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,
358 const unsigned char *name)
359{
360 struct sysfs_dirent *sd;
361
362 mutex_lock(&parent_sd->s_dentry->d_inode->i_mutex);
363 sd = sysfs_find_dirent(parent_sd, name);
364 sysfs_get(sd);
365 mutex_unlock(&parent_sd->s_dentry->d_inode->i_mutex);
366
367 return sd;
Maneesh Sonic5168652006-03-09 19:40:14 +0530368}
369
Tejun Heo608e2662007-06-14 04:27:22 +0900370static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
371 const char *name, struct sysfs_dirent **p_sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Tejun Heo608e2662007-06-14 04:27:22 +0900373 struct dentry *parent = parent_sd->s_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 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 Heo608e2662007-06-14 04:27:22 +0900412 if (sysfs_find_dirent(parent_sd, 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 Heo608e2662007-06-14 04:27:22 +0900417 sysfs_attach_dirent(sd, parent_sd, dentry);
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900418
Tejun Heo608e2662007-06-14 04:27:22 +0900419 *p_sd = sd;
Tejun Heodfeb9fb2007-06-14 03:45:14 +0900420 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
Tejun Heo608e2662007-06-14 04:27:22 +0900436int sysfs_create_subdir(struct kobject *kobj, const char *name,
437 struct sysfs_dirent **p_sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Tejun Heo608e2662007-06-14 04:27:22 +0900439 return create_dir(kobj, kobj->sd, name, p_sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
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.
Tejun Heo608e2662007-06-14 04:27:22 +0900445 * @shadow_parent: parent object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 */
Tejun Heo608e2662007-06-14 04:27:22 +0900447int sysfs_create_dir(struct kobject *kobj,
448 struct sysfs_dirent *shadow_parent_sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Tejun Heo608e2662007-06-14 04:27:22 +0900450 struct sysfs_dirent *parent_sd, *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 int error = 0;
452
453 BUG_ON(!kobj);
454
Tejun Heo608e2662007-06-14 04:27:22 +0900455 if (shadow_parent_sd)
456 parent_sd = shadow_parent_sd;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700457 else if (kobj->parent)
Tejun Heo608e2662007-06-14 04:27:22 +0900458 parent_sd = kobj->parent->sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 else if (sysfs_mount && sysfs_mount->mnt_sb)
Tejun Heo608e2662007-06-14 04:27:22 +0900460 parent_sd = sysfs_mount->mnt_sb->s_root->d_fsdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 else
462 return -EFAULT;
463
Tejun Heo608e2662007-06-14 04:27:22 +0900464 error = create_dir(kobj, parent_sd, kobject_name(kobj), &sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (!error)
Tejun Heo608e2662007-06-14 04:27:22 +0900466 kobj->sd = sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return error;
468}
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry,
471 struct nameidata *nd)
472{
473 struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
474 struct sysfs_dirent * sd;
Tejun Heob402d722007-06-14 04:27:21 +0900475 struct bin_attribute *bin_attr;
Tejun Heofc9f54b2007-06-14 03:45:17 +0900476 struct inode *inode;
477 int found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Tejun Heo0c73f182007-06-14 03:45:18 +0900479 for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) {
Tejun Heob402d722007-06-14 04:27:21 +0900480 if ((sysfs_type(sd) & SYSFS_NOT_PINNED) &&
Tejun Heofc9f54b2007-06-14 03:45:17 +0900481 !strcmp(sd->s_name, dentry->d_name.name)) {
482 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 break;
484 }
485 }
486
Tejun Heofc9f54b2007-06-14 03:45:17 +0900487 /* no such entry */
488 if (!found)
489 return NULL;
490
491 /* attach dentry and inode */
Tejun Heo8312a8d2007-06-14 03:45:17 +0900492 inode = sysfs_get_inode(sd);
Tejun Heofc9f54b2007-06-14 03:45:17 +0900493 if (!inode)
494 return ERR_PTR(-ENOMEM);
495
Tejun Heo8312a8d2007-06-14 03:45:17 +0900496 if (inode->i_state & I_NEW) {
497 /* initialize inode according to type */
Tejun Heob402d722007-06-14 04:27:21 +0900498 switch (sysfs_type(sd)) {
499 case SYSFS_KOBJ_ATTR:
Tejun Heo8312a8d2007-06-14 03:45:17 +0900500 inode->i_size = PAGE_SIZE;
501 inode->i_fop = &sysfs_file_operations;
Tejun Heob402d722007-06-14 04:27:21 +0900502 break;
503 case SYSFS_KOBJ_BIN_ATTR:
504 bin_attr = sd->s_elem.bin_attr.bin_attr;
Tejun Heo8312a8d2007-06-14 03:45:17 +0900505 inode->i_size = bin_attr->size;
506 inode->i_fop = &bin_fops;
Tejun Heob402d722007-06-14 04:27:21 +0900507 break;
508 case SYSFS_KOBJ_LINK:
Tejun Heo8312a8d2007-06-14 03:45:17 +0900509 inode->i_op = &sysfs_symlink_inode_operations;
Tejun Heob402d722007-06-14 04:27:21 +0900510 break;
511 default:
512 BUG();
513 }
Tejun Heo8312a8d2007-06-14 03:45:17 +0900514 }
Tejun Heofc9f54b2007-06-14 03:45:17 +0900515
516 sysfs_instantiate(dentry, inode);
517 sysfs_attach_dentry(sd, dentry);
518
519 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800522const struct inode_operations sysfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 .lookup = sysfs_lookup,
Maneesh Soni988d1862005-05-31 10:39:14 +0530524 .setattr = sysfs_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525};
526
Tejun Heo608e2662007-06-14 04:27:22 +0900527static void remove_dir(struct sysfs_dirent *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Tejun Heo608e2662007-06-14 04:27:22 +0900529 struct dentry *parent = sd->s_parent->s_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800531 mutex_lock(&parent->d_inode->i_mutex);
Tejun Heodbde0fc2007-06-14 03:45:16 +0900532
Tejun Heo0c73f182007-06-14 03:45:18 +0900533 sysfs_unlink_sibling(sd);
Tejun Heo380e6fb2007-06-14 04:27:22 +0900534 sd->s_flags |= SYSFS_FLAG_REMOVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Tejun Heo608e2662007-06-14 04:27:22 +0900536 pr_debug(" o %s removing done\n", sd->s_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800538 mutex_unlock(&parent->d_inode->i_mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900539
Tejun Heodbde0fc2007-06-14 03:45:16 +0900540 sysfs_drop_dentry(sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900541 sysfs_deactivate(sd);
542 sysfs_put(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Tejun Heo608e2662007-06-14 04:27:22 +0900545void sysfs_remove_subdir(struct sysfs_dirent *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Tejun Heo608e2662007-06-14 04:27:22 +0900547 remove_dir(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
550
Tejun Heo608e2662007-06-14 04:27:22 +0900551static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Tejun Heo0c73f182007-06-14 03:45:18 +0900553 struct sysfs_dirent *removed = NULL;
Tejun Heo0c73f182007-06-14 03:45:18 +0900554 struct sysfs_dirent **pos;
Tejun Heo608e2662007-06-14 04:27:22 +0900555 struct dentry *dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Tejun Heo608e2662007-06-14 04:27:22 +0900557 if (!dir_sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return;
559
Tejun Heo608e2662007-06-14 04:27:22 +0900560 dir = dir_sd->s_dentry;
561
562 pr_debug("sysfs %s: removing dir\n", dir_sd->s_name);
563 mutex_lock(&dir->d_inode->i_mutex);
564 pos = &dir_sd->s_children;
Tejun Heo0c73f182007-06-14 03:45:18 +0900565 while (*pos) {
566 struct sysfs_dirent *sd = *pos;
567
Tejun Heob402d722007-06-14 04:27:21 +0900568 if (sysfs_type(sd) && (sysfs_type(sd) & SYSFS_NOT_PINNED)) {
Tejun Heo380e6fb2007-06-14 04:27:22 +0900569 sd->s_flags |= SYSFS_FLAG_REMOVED;
Tejun Heo0c73f182007-06-14 03:45:18 +0900570 *pos = sd->s_sibling;
571 sd->s_sibling = removed;
572 removed = sd;
573 } else
574 pos = &(*pos)->s_sibling;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
Tejun Heo608e2662007-06-14 04:27:22 +0900576 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Tejun Heo0c73f182007-06-14 03:45:18 +0900578 while (removed) {
579 struct sysfs_dirent *sd = removed;
580
581 removed = sd->s_sibling;
582 sd->s_sibling = NULL;
583
Tejun Heodbde0fc2007-06-14 03:45:16 +0900584 sysfs_drop_dentry(sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900585 sysfs_deactivate(sd);
586 sysfs_put(sd);
587 }
588
Tejun Heo608e2662007-06-14 04:27:22 +0900589 remove_dir(dir_sd);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700590}
591
592/**
593 * sysfs_remove_dir - remove an object's directory.
594 * @kobj: object.
595 *
596 * The only thing special about this is that we remove any files in
597 * the directory before we remove the directory, and we've inlined
598 * what used to be sysfs_rmdir() below, instead of calling separately.
599 */
600
601void sysfs_remove_dir(struct kobject * kobj)
602{
Tejun Heo608e2662007-06-14 04:27:22 +0900603 struct sysfs_dirent *sd = kobj->sd;
Tejun Heoaecdced2007-06-14 03:45:15 +0900604
Tejun Heo5f995322007-06-14 04:27:23 +0900605 spin_lock(&sysfs_assoc_lock);
Tejun Heo608e2662007-06-14 04:27:22 +0900606 kobj->sd = NULL;
Tejun Heo5f995322007-06-14 04:27:23 +0900607 spin_unlock(&sysfs_assoc_lock);
Tejun Heoaecdced2007-06-14 03:45:15 +0900608
Tejun Heo608e2662007-06-14 04:27:22 +0900609 __sysfs_remove_dir(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
Tejun Heo608e2662007-06-14 04:27:22 +0900612int sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent *new_parent_sd,
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700613 const char *new_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
Tejun Heo608e2662007-06-14 04:27:22 +0900615 struct sysfs_dirent *sd = kobj->sd;
616 struct dentry *new_parent = new_parent_sd->s_dentry;
Tejun Heo0c096b52007-06-14 03:45:15 +0900617 struct dentry *new_dentry;
618 char *dup_name;
Tejun Heo996b7372007-06-14 03:45:14 +0900619 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Tejun Heo608e2662007-06-14 04:27:22 +0900621 if (!new_parent_sd)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700622 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 down_write(&sysfs_rename_sem);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700625 mutex_lock(&new_parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700627 new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name));
Tejun Heo996b7372007-06-14 03:45:14 +0900628 if (IS_ERR(new_dentry)) {
629 error = PTR_ERR(new_dentry);
630 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
Tejun Heo996b7372007-06-14 03:45:14 +0900632
633 /* By allowing two different directories with the same
634 * d_parent we allow this routine to move between different
635 * shadows of the same directory
636 */
637 error = -EINVAL;
Tejun Heo608e2662007-06-14 04:27:22 +0900638 if (sd->s_parent->s_dentry->d_inode != new_parent->d_inode ||
Tejun Heo996b7372007-06-14 03:45:14 +0900639 new_dentry->d_parent->d_inode != new_parent->d_inode ||
Tejun Heo608e2662007-06-14 04:27:22 +0900640 new_dentry == sd->s_dentry)
Tejun Heo996b7372007-06-14 03:45:14 +0900641 goto out_dput;
642
643 error = -EEXIST;
644 if (new_dentry->d_inode)
645 goto out_dput;
646
Tejun Heo0c096b52007-06-14 03:45:15 +0900647 /* rename kobject and sysfs_dirent */
648 error = -ENOMEM;
649 new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
650 if (!new_name)
Tejun Heo996b7372007-06-14 03:45:14 +0900651 goto out_drop;
652
Tejun Heo0c096b52007-06-14 03:45:15 +0900653 error = kobject_set_name(kobj, "%s", new_name);
654 if (error)
655 goto out_free;
656
657 kfree(sd->s_name);
658 sd->s_name = new_name;
659
660 /* move under the new parent */
Tejun Heo996b7372007-06-14 03:45:14 +0900661 d_add(new_dentry, NULL);
Tejun Heo608e2662007-06-14 04:27:22 +0900662 d_move(sd->s_dentry, new_dentry);
Tejun Heo996b7372007-06-14 03:45:14 +0900663
Tejun Heo0c73f182007-06-14 03:45:18 +0900664 sysfs_unlink_sibling(sd);
Tejun Heo608e2662007-06-14 04:27:22 +0900665 sysfs_get(new_parent_sd);
Tejun Heo7f7cfff2007-06-14 03:45:17 +0900666 sysfs_put(sd->s_parent);
Tejun Heo608e2662007-06-14 04:27:22 +0900667 sd->s_parent = new_parent_sd;
Tejun Heo0c73f182007-06-14 03:45:18 +0900668 sysfs_link_sibling(sd);
Tejun Heo996b7372007-06-14 03:45:14 +0900669
670 error = 0;
671 goto out_unlock;
672
Tejun Heo0c096b52007-06-14 03:45:15 +0900673 out_free:
674 kfree(dup_name);
Tejun Heo996b7372007-06-14 03:45:14 +0900675 out_drop:
676 d_drop(new_dentry);
677 out_dput:
678 dput(new_dentry);
679 out_unlock:
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700680 mutex_unlock(&new_parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 up_write(&sysfs_rename_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return error;
683}
684
Cornelia Huck8a824722006-11-20 17:07:51 +0100685int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
686{
687 struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
688 struct sysfs_dirent *new_parent_sd, *sd;
689 int error;
690
Cornelia Huck8a824722006-11-20 17:07:51 +0100691 old_parent_dentry = kobj->parent ?
Tejun Heo608e2662007-06-14 04:27:22 +0900692 kobj->parent->sd->s_dentry : sysfs_mount->mnt_sb->s_root;
Cornelia Huckc744aeae2007-01-08 20:16:44 +0100693 new_parent_dentry = new_parent ?
Tejun Heo608e2662007-06-14 04:27:22 +0900694 new_parent->sd->s_dentry : sysfs_mount->mnt_sb->s_root;
Cornelia Huck8a824722006-11-20 17:07:51 +0100695
Mark Lord0de15172007-03-06 01:42:03 -0800696 if (old_parent_dentry->d_inode == new_parent_dentry->d_inode)
697 return 0; /* nothing to move */
Cornelia Huck8a824722006-11-20 17:07:51 +0100698again:
699 mutex_lock(&old_parent_dentry->d_inode->i_mutex);
700 if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
701 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
702 goto again;
703 }
704
705 new_parent_sd = new_parent_dentry->d_fsdata;
Tejun Heo608e2662007-06-14 04:27:22 +0900706 sd = kobj->sd;
Cornelia Huck8a824722006-11-20 17:07:51 +0100707
708 new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
709 strlen(kobj->name));
710 if (IS_ERR(new_dentry)) {
711 error = PTR_ERR(new_dentry);
712 goto out;
713 } else
714 error = 0;
715 d_add(new_dentry, NULL);
Tejun Heo608e2662007-06-14 04:27:22 +0900716 d_move(sd->s_dentry, new_dentry);
Cornelia Huck8a824722006-11-20 17:07:51 +0100717 dput(new_dentry);
718
719 /* Remove from old parent's list and insert into new parent's list. */
Tejun Heo0c73f182007-06-14 03:45:18 +0900720 sysfs_unlink_sibling(sd);
Tejun Heo7f7cfff2007-06-14 03:45:17 +0900721 sysfs_get(new_parent_sd);
722 sysfs_put(sd->s_parent);
723 sd->s_parent = new_parent_sd;
Tejun Heo0c73f182007-06-14 03:45:18 +0900724 sysfs_link_sibling(sd);
Cornelia Huck8a824722006-11-20 17:07:51 +0100725
726out:
727 mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
728 mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
729
730 return error;
731}
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733static int sysfs_dir_open(struct inode *inode, struct file *file)
734{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800735 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
Tejun Heoa26cd722007-06-14 03:45:14 +0900737 struct sysfs_dirent * sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800739 mutex_lock(&dentry->d_inode->i_mutex);
Tejun Heo3e519032007-06-14 03:45:15 +0900740 sd = sysfs_new_dirent("_DIR_", 0, 0);
Tejun Heoa26cd722007-06-14 03:45:14 +0900741 if (sd)
742 sysfs_attach_dirent(sd, parent_sd, NULL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800743 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Tejun Heoa26cd722007-06-14 03:45:14 +0900745 file->private_data = sd;
746 return sd ? 0 : -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
749static int sysfs_dir_close(struct inode *inode, struct file *file)
750{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800751 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 struct sysfs_dirent * cursor = file->private_data;
753
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800754 mutex_lock(&dentry->d_inode->i_mutex);
Tejun Heo0c73f182007-06-14 03:45:18 +0900755 sysfs_unlink_sibling(cursor);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800756 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 release_sysfs_dirent(cursor);
759
760 return 0;
761}
762
763/* Relationship between s_mode and the DT_xxx types */
764static inline unsigned char dt_type(struct sysfs_dirent *sd)
765{
766 return (sd->s_mode >> 12) & 15;
767}
768
769static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
770{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800771 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 struct sysfs_dirent * parent_sd = dentry->d_fsdata;
773 struct sysfs_dirent *cursor = filp->private_data;
Tejun Heo0c73f182007-06-14 03:45:18 +0900774 struct sysfs_dirent **pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 ino_t ino;
776 int i = filp->f_pos;
777
778 switch (i) {
779 case 0:
Eric Sandeendc351252007-06-11 14:02:45 +0900780 ino = parent_sd->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
782 break;
783 filp->f_pos++;
784 i++;
785 /* fallthrough */
786 case 1:
Tejun Heo13b30862007-06-14 03:45:14 +0900787 if (parent_sd->s_parent)
788 ino = parent_sd->s_parent->s_ino;
789 else
790 ino = parent_sd->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
792 break;
793 filp->f_pos++;
794 i++;
795 /* fallthrough */
796 default:
Tejun Heo0c73f182007-06-14 03:45:18 +0900797 pos = &parent_sd->s_children;
798 while (*pos != cursor)
799 pos = &(*pos)->s_sibling;
Akinobu Mita1bfba4e2006-06-26 00:24:40 -0700800
Tejun Heo0c73f182007-06-14 03:45:18 +0900801 /* unlink cursor */
802 *pos = cursor->s_sibling;
803
804 if (filp->f_pos == 2)
805 pos = &parent_sd->s_children;
806
807 for ( ; *pos; pos = &(*pos)->s_sibling) {
808 struct sysfs_dirent *next = *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 const char * name;
810 int len;
811
Tejun Heob402d722007-06-14 04:27:21 +0900812 if (!sysfs_type(next))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 continue;
814
Tejun Heo0c096b52007-06-14 03:45:15 +0900815 name = next->s_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 len = strlen(name);
Eric Sandeendc351252007-06-11 14:02:45 +0900817 ino = next->s_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 if (filldir(dirent, name, len, filp->f_pos, ino,
820 dt_type(next)) < 0)
Tejun Heo0c73f182007-06-14 03:45:18 +0900821 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 filp->f_pos++;
824 }
Tejun Heo0c73f182007-06-14 03:45:18 +0900825
826 /* put cursor back in */
827 cursor->s_sibling = *pos;
828 *pos = cursor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830 return 0;
831}
832
833static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
834{
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800835 struct dentry * dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800837 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 switch (origin) {
839 case 1:
840 offset += file->f_pos;
841 case 0:
842 if (offset >= 0)
843 break;
844 default:
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800845 mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return -EINVAL;
847 }
848 if (offset != file->f_pos) {
849 file->f_pos = offset;
850 if (file->f_pos >= 2) {
851 struct sysfs_dirent *sd = dentry->d_fsdata;
852 struct sysfs_dirent *cursor = file->private_data;
Tejun Heo0c73f182007-06-14 03:45:18 +0900853 struct sysfs_dirent **pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 loff_t n = file->f_pos - 2;
855
Tejun Heo0c73f182007-06-14 03:45:18 +0900856 sysfs_unlink_sibling(cursor);
857
858 pos = &sd->s_children;
859 while (n && *pos) {
860 struct sysfs_dirent *next = *pos;
Tejun Heob402d722007-06-14 04:27:21 +0900861 if (sysfs_type(next))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 n--;
Tejun Heo0c73f182007-06-14 03:45:18 +0900863 pos = &(*pos)->s_sibling;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
Tejun Heo0c73f182007-06-14 03:45:18 +0900865
866 cursor->s_sibling = *pos;
867 *pos = cursor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800870 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return offset;
872}
873
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700874
875/**
876 * sysfs_make_shadowed_dir - Setup so a directory can be shadowed
877 * @kobj: object we're creating shadow of.
878 */
879
880int sysfs_make_shadowed_dir(struct kobject *kobj,
881 void * (*follow_link)(struct dentry *, struct nameidata *))
882{
883 struct inode *inode;
884 struct inode_operations *i_op;
885
Tejun Heo608e2662007-06-14 04:27:22 +0900886 inode = kobj->sd->s_dentry->d_inode;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700887 if (inode->i_op != &sysfs_dir_inode_operations)
888 return -EINVAL;
889
890 i_op = kmalloc(sizeof(*i_op), GFP_KERNEL);
891 if (!i_op)
892 return -ENOMEM;
893
894 memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op));
895 i_op->follow_link = follow_link;
896
897 /* Locking of inode->i_op?
898 * Since setting i_op is a single word write and they
899 * are atomic we should be ok here.
900 */
901 inode->i_op = i_op;
902 return 0;
903}
904
905/**
906 * sysfs_create_shadow_dir - create a shadow directory for an object.
907 * @kobj: object we're creating directory for.
908 *
909 * sysfs_make_shadowed_dir must already have been called on this
910 * directory.
911 */
912
Tejun Heo608e2662007-06-14 04:27:22 +0900913struct sysfs_dirent *sysfs_create_shadow_dir(struct kobject *kobj)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700914{
Tejun Heo608e2662007-06-14 04:27:22 +0900915 struct dentry *dir = kobj->sd->s_dentry;
Tejun Heo13b30862007-06-14 03:45:14 +0900916 struct inode *inode = dir->d_inode;
917 struct dentry *parent = dir->d_parent;
918 struct sysfs_dirent *parent_sd = parent->d_fsdata;
919 struct dentry *shadow;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700920 struct sysfs_dirent *sd;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700921
Tejun Heo608e2662007-06-14 04:27:22 +0900922 sd = ERR_PTR(-EINVAL);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700923 if (!sysfs_is_shadowed_inode(inode))
924 goto out;
925
926 shadow = d_alloc(parent, &dir->d_name);
927 if (!shadow)
928 goto nomem;
929
Tejun Heo3e519032007-06-14 03:45:15 +0900930 sd = sysfs_new_dirent("_SHADOW_", inode->i_mode, SYSFS_DIR);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700931 if (!sd)
932 goto nomem;
Tejun Heo3e519032007-06-14 03:45:15 +0900933 sd->s_elem.dir.kobj = kobj;
Tejun Heo13b30862007-06-14 03:45:14 +0900934 /* point to parent_sd but don't attach to it */
935 sd->s_parent = sysfs_get(parent_sd);
Tejun Heoa26cd722007-06-14 03:45:14 +0900936 sysfs_attach_dirent(sd, NULL, shadow);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700937
938 d_instantiate(shadow, igrab(inode));
939 inc_nlink(inode);
940 inc_nlink(parent->d_inode);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700941
942 dget(shadow); /* Extra count - pin the dentry in core */
943
944out:
Tejun Heo608e2662007-06-14 04:27:22 +0900945 return sd;
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700946nomem:
947 dput(shadow);
Tejun Heo608e2662007-06-14 04:27:22 +0900948 sd = ERR_PTR(-ENOMEM);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700949 goto out;
950}
951
952/**
953 * sysfs_remove_shadow_dir - remove an object's directory.
Tejun Heo608e2662007-06-14 04:27:22 +0900954 * @shadow_sd: sysfs_dirent of shadow directory
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700955 *
956 * The only thing special about this is that we remove any files in
957 * the directory before we remove the directory, and we've inlined
958 * what used to be sysfs_rmdir() below, instead of calling separately.
959 */
960
Tejun Heo608e2662007-06-14 04:27:22 +0900961void sysfs_remove_shadow_dir(struct sysfs_dirent *shadow_sd)
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700962{
Tejun Heo608e2662007-06-14 04:27:22 +0900963 __sysfs_remove_dir(shadow_sd);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -0700964}
965
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800966const struct file_operations sysfs_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 .open = sysfs_dir_open,
968 .release = sysfs_dir_close,
969 .llseek = sysfs_dir_lseek,
970 .read = generic_read_dir,
971 .readdir = sysfs_readdir,
972};