Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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> | |
5f45f1a7 | 11 | #include <linux/namei.h> |
2b611bb7 | 12 | #include <linux/idr.h> |
94bebf4d | 13 | #include <asm/semaphore.h> |
1da177e4 LT |
14 | #include "sysfs.h" |
15 | ||
16 | DECLARE_RWSEM(sysfs_rename_sem); | |
dd14cbc9 | 17 | spinlock_t sysfs_lock = SPIN_LOCK_UNLOCKED; |
1da177e4 | 18 | |
2b611bb7 TH |
19 | static spinlock_t sysfs_ino_lock = SPIN_LOCK_UNLOCKED; |
20 | static DEFINE_IDA(sysfs_ino_ida); | |
21 | ||
22 | int sysfs_alloc_ino(ino_t *pino) | |
23 | { | |
24 | int ino, rc; | |
25 | ||
26 | retry: | |
27 | spin_lock(&sysfs_ino_lock); | |
28 | rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino); | |
29 | spin_unlock(&sysfs_ino_lock); | |
30 | ||
31 | if (rc == -EAGAIN) { | |
32 | if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL)) | |
33 | goto retry; | |
34 | rc = -ENOMEM; | |
35 | } | |
36 | ||
37 | *pino = ino; | |
38 | return rc; | |
39 | } | |
40 | ||
41 | static void sysfs_free_ino(ino_t ino) | |
42 | { | |
43 | spin_lock(&sysfs_ino_lock); | |
44 | ida_remove(&sysfs_ino_ida, ino); | |
45 | spin_unlock(&sysfs_ino_lock); | |
46 | } | |
47 | ||
fa7f912a TH |
48 | void release_sysfs_dirent(struct sysfs_dirent * sd) |
49 | { | |
13b3086d TH |
50 | struct sysfs_dirent *parent_sd; |
51 | ||
52 | repeat: | |
53 | parent_sd = sd->s_parent; | |
54 | ||
fa7f912a TH |
55 | if (sd->s_type & SYSFS_KOBJ_LINK) { |
56 | struct sysfs_symlink * sl = sd->s_element; | |
fa7f912a TH |
57 | kobject_put(sl->target_kobj); |
58 | kfree(sl); | |
59 | } | |
0c096b50 TH |
60 | if (sd->s_type & SYSFS_COPY_NAME) |
61 | kfree(sd->s_name); | |
fa7f912a | 62 | kfree(sd->s_iattr); |
2b611bb7 | 63 | sysfs_free_ino(sd->s_ino); |
fa7f912a | 64 | kmem_cache_free(sysfs_dir_cachep, sd); |
13b3086d TH |
65 | |
66 | sd = parent_sd; | |
67 | if (sd && atomic_dec_and_test(&sd->s_count)) | |
68 | goto repeat; | |
fa7f912a TH |
69 | } |
70 | ||
1da177e4 LT |
71 | static void sysfs_d_iput(struct dentry * dentry, struct inode * inode) |
72 | { | |
73 | struct sysfs_dirent * sd = dentry->d_fsdata; | |
74 | ||
75 | if (sd) { | |
dd14cbc9 TH |
76 | /* sd->s_dentry is protected with sysfs_lock. This |
77 | * allows sysfs_drop_dentry() to dereference it. | |
78 | */ | |
79 | spin_lock(&sysfs_lock); | |
80 | ||
81 | /* The dentry might have been deleted or another | |
82 | * lookup could have happened updating sd->s_dentry to | |
83 | * point the new dentry. Ignore if it isn't pointing | |
84 | * to this dentry. | |
85 | */ | |
86 | if (sd->s_dentry == dentry) | |
87 | sd->s_dentry = NULL; | |
88 | spin_unlock(&sysfs_lock); | |
1da177e4 LT |
89 | sysfs_put(sd); |
90 | } | |
91 | iput(inode); | |
92 | } | |
93 | ||
94 | static struct dentry_operations sysfs_dentry_ops = { | |
95 | .d_iput = sysfs_d_iput, | |
96 | }; | |
97 | ||
0c096b50 TH |
98 | struct sysfs_dirent *sysfs_new_dirent(const char *name, void *element, |
99 | umode_t mode, int type) | |
1da177e4 | 100 | { |
0c096b50 TH |
101 | char *dup_name = NULL; |
102 | struct sysfs_dirent *sd = NULL; | |
103 | ||
104 | if (type & SYSFS_COPY_NAME) { | |
105 | name = dup_name = kstrdup(name, GFP_KERNEL); | |
106 | if (!name) | |
107 | goto err_out; | |
108 | } | |
1da177e4 | 109 | |
c3762229 | 110 | sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL); |
1da177e4 | 111 | if (!sd) |
0c096b50 | 112 | goto err_out; |
1da177e4 | 113 | |
0c096b50 TH |
114 | if (sysfs_alloc_ino(&sd->s_ino)) |
115 | goto err_out; | |
2b611bb7 | 116 | |
1da177e4 | 117 | atomic_set(&sd->s_count, 1); |
eea3f891 | 118 | atomic_set(&sd->s_event, 1); |
1da177e4 | 119 | INIT_LIST_HEAD(&sd->s_children); |
b592fcfe | 120 | INIT_LIST_HEAD(&sd->s_sibling); |
a26cd722 | 121 | |
0c096b50 | 122 | sd->s_name = name; |
1da177e4 | 123 | sd->s_element = element; |
a26cd722 TH |
124 | sd->s_mode = mode; |
125 | sd->s_type = type; | |
1da177e4 LT |
126 | |
127 | return sd; | |
0c096b50 TH |
128 | |
129 | err_out: | |
130 | kfree(dup_name); | |
131 | kmem_cache_free(sysfs_dir_cachep, sd); | |
132 | return NULL; | |
1da177e4 LT |
133 | } |
134 | ||
a26cd722 TH |
135 | void sysfs_attach_dirent(struct sysfs_dirent *sd, |
136 | struct sysfs_dirent *parent_sd, struct dentry *dentry) | |
b592fcfe | 137 | { |
a26cd722 TH |
138 | if (dentry) { |
139 | sd->s_dentry = dentry; | |
140 | dentry->d_fsdata = sysfs_get(sd); | |
141 | dentry->d_op = &sysfs_dentry_ops; | |
142 | } | |
b592fcfe | 143 | |
13b3086d TH |
144 | if (parent_sd) { |
145 | sd->s_parent = sysfs_get(parent_sd); | |
a26cd722 | 146 | list_add(&sd->s_sibling, &parent_sd->s_children); |
13b3086d | 147 | } |
b592fcfe EB |
148 | } |
149 | ||
a580290c | 150 | /* |
c516865c MS |
151 | * |
152 | * Return -EEXIST if there is already a sysfs element with the same name for | |
153 | * the same parent. | |
154 | * | |
155 | * called with parent inode's i_mutex held | |
156 | */ | |
157 | int sysfs_dirent_exist(struct sysfs_dirent *parent_sd, | |
158 | const unsigned char *new) | |
159 | { | |
160 | struct sysfs_dirent * sd; | |
161 | ||
162 | list_for_each_entry(sd, &parent_sd->s_children, s_sibling) { | |
163 | if (sd->s_element) { | |
0c096b50 | 164 | if (strcmp(sd->s_name, new)) |
c516865c MS |
165 | continue; |
166 | else | |
167 | return -EEXIST; | |
168 | } | |
169 | } | |
170 | ||
171 | return 0; | |
172 | } | |
173 | ||
1da177e4 LT |
174 | static int init_dir(struct inode * inode) |
175 | { | |
176 | inode->i_op = &sysfs_dir_inode_operations; | |
177 | inode->i_fop = &sysfs_dir_operations; | |
178 | ||
179 | /* directory inodes start off with i_nlink == 2 (for "." entry) */ | |
d8c76e6f | 180 | inc_nlink(inode); |
1da177e4 LT |
181 | return 0; |
182 | } | |
183 | ||
184 | static int init_file(struct inode * inode) | |
185 | { | |
186 | inode->i_size = PAGE_SIZE; | |
187 | inode->i_fop = &sysfs_file_operations; | |
188 | return 0; | |
189 | } | |
190 | ||
191 | static int init_symlink(struct inode * inode) | |
192 | { | |
193 | inode->i_op = &sysfs_symlink_inode_operations; | |
194 | return 0; | |
195 | } | |
196 | ||
dfeb9fb0 TH |
197 | static int create_dir(struct kobject *kobj, struct dentry *parent, |
198 | const char *name, struct dentry **p_dentry) | |
1da177e4 LT |
199 | { |
200 | int error; | |
201 | umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO; | |
dfeb9fb0 TH |
202 | struct dentry *dentry; |
203 | struct sysfs_dirent *sd; | |
1da177e4 | 204 | |
dfeb9fb0 TH |
205 | mutex_lock(&parent->d_inode->i_mutex); |
206 | ||
207 | dentry = lookup_one_len(name, parent, strlen(name)); | |
208 | if (IS_ERR(dentry)) { | |
209 | error = PTR_ERR(dentry); | |
210 | goto out_unlock; | |
211 | } | |
212 | ||
213 | error = -EEXIST; | |
214 | if (sysfs_dirent_exist(parent->d_fsdata, name)) | |
215 | goto out_dput; | |
216 | ||
a26cd722 | 217 | error = -ENOMEM; |
0c096b50 | 218 | sd = sysfs_new_dirent(name, kobj, mode, SYSFS_DIR); |
a26cd722 | 219 | if (!sd) |
dfeb9fb0 | 220 | goto out_drop; |
a26cd722 | 221 | sysfs_attach_dirent(sd, parent->d_fsdata, dentry); |
dfeb9fb0 TH |
222 | |
223 | error = sysfs_create(dentry, mode, init_dir); | |
224 | if (error) | |
225 | goto out_sput; | |
226 | ||
227 | inc_nlink(parent->d_inode); | |
228 | dentry->d_op = &sysfs_dentry_ops; | |
229 | d_rehash(dentry); | |
230 | ||
231 | *p_dentry = dentry; | |
232 | error = 0; | |
233 | goto out_dput; | |
234 | ||
235 | out_sput: | |
dfeb9fb0 TH |
236 | list_del_init(&sd->s_sibling); |
237 | sysfs_put(sd); | |
238 | out_drop: | |
239 | d_drop(dentry); | |
240 | out_dput: | |
241 | dput(dentry); | |
242 | out_unlock: | |
243 | mutex_unlock(&parent->d_inode->i_mutex); | |
1da177e4 LT |
244 | return error; |
245 | } | |
246 | ||
247 | ||
248 | int sysfs_create_subdir(struct kobject * k, const char * n, struct dentry ** d) | |
249 | { | |
250 | return create_dir(k,k->dentry,n,d); | |
251 | } | |
252 | ||
253 | /** | |
254 | * sysfs_create_dir - create a directory for an object. | |
1da177e4 | 255 | * @kobj: object we're creating directory for. |
b592fcfe | 256 | * @shadow_parent: parent parent object. |
1da177e4 LT |
257 | */ |
258 | ||
b592fcfe | 259 | int sysfs_create_dir(struct kobject * kobj, struct dentry *shadow_parent) |
1da177e4 LT |
260 | { |
261 | struct dentry * dentry = NULL; | |
262 | struct dentry * parent; | |
263 | int error = 0; | |
264 | ||
265 | BUG_ON(!kobj); | |
266 | ||
b592fcfe EB |
267 | if (shadow_parent) |
268 | parent = shadow_parent; | |
269 | else if (kobj->parent) | |
1da177e4 LT |
270 | parent = kobj->parent->dentry; |
271 | else if (sysfs_mount && sysfs_mount->mnt_sb) | |
272 | parent = sysfs_mount->mnt_sb->s_root; | |
273 | else | |
274 | return -EFAULT; | |
275 | ||
276 | error = create_dir(kobj,parent,kobject_name(kobj),&dentry); | |
277 | if (!error) | |
278 | kobj->dentry = dentry; | |
279 | return error; | |
280 | } | |
281 | ||
282 | /* attaches attribute's sysfs_dirent to the dentry corresponding to the | |
283 | * attribute file | |
284 | */ | |
285 | static int sysfs_attach_attr(struct sysfs_dirent * sd, struct dentry * dentry) | |
286 | { | |
287 | struct attribute * attr = NULL; | |
288 | struct bin_attribute * bin_attr = NULL; | |
289 | int (* init) (struct inode *) = NULL; | |
290 | int error = 0; | |
291 | ||
292 | if (sd->s_type & SYSFS_KOBJ_BIN_ATTR) { | |
293 | bin_attr = sd->s_element; | |
294 | attr = &bin_attr->attr; | |
295 | } else { | |
296 | attr = sd->s_element; | |
297 | init = init_file; | |
298 | } | |
299 | ||
6fa5c828 | 300 | dentry->d_fsdata = sysfs_get(sd); |
dd14cbc9 TH |
301 | /* protect sd->s_dentry against sysfs_d_iput */ |
302 | spin_lock(&sysfs_lock); | |
6fa5c828 | 303 | sd->s_dentry = dentry; |
dd14cbc9 | 304 | spin_unlock(&sysfs_lock); |
1da177e4 | 305 | error = sysfs_create(dentry, (attr->mode & S_IALLUGO) | S_IFREG, init); |
6fa5c828 MS |
306 | if (error) { |
307 | sysfs_put(sd); | |
1da177e4 | 308 | return error; |
6fa5c828 | 309 | } |
1da177e4 LT |
310 | |
311 | if (bin_attr) { | |
312 | dentry->d_inode->i_size = bin_attr->size; | |
313 | dentry->d_inode->i_fop = &bin_fops; | |
314 | } | |
315 | dentry->d_op = &sysfs_dentry_ops; | |
1da177e4 LT |
316 | d_rehash(dentry); |
317 | ||
318 | return 0; | |
319 | } | |
320 | ||
321 | static int sysfs_attach_link(struct sysfs_dirent * sd, struct dentry * dentry) | |
322 | { | |
323 | int err = 0; | |
324 | ||
6fa5c828 | 325 | dentry->d_fsdata = sysfs_get(sd); |
dd14cbc9 TH |
326 | /* protect sd->s_dentry against sysfs_d_iput */ |
327 | spin_lock(&sysfs_lock); | |
6fa5c828 | 328 | sd->s_dentry = dentry; |
dd14cbc9 | 329 | spin_unlock(&sysfs_lock); |
1da177e4 LT |
330 | err = sysfs_create(dentry, S_IFLNK|S_IRWXUGO, init_symlink); |
331 | if (!err) { | |
332 | dentry->d_op = &sysfs_dentry_ops; | |
1da177e4 | 333 | d_rehash(dentry); |
6fa5c828 MS |
334 | } else |
335 | sysfs_put(sd); | |
336 | ||
1da177e4 LT |
337 | return err; |
338 | } | |
339 | ||
340 | static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry, | |
341 | struct nameidata *nd) | |
342 | { | |
343 | struct sysfs_dirent * parent_sd = dentry->d_parent->d_fsdata; | |
344 | struct sysfs_dirent * sd; | |
345 | int err = 0; | |
346 | ||
347 | list_for_each_entry(sd, &parent_sd->s_children, s_sibling) { | |
348 | if (sd->s_type & SYSFS_NOT_PINNED) { | |
0c096b50 | 349 | if (strcmp(sd->s_name, dentry->d_name.name)) |
1da177e4 LT |
350 | continue; |
351 | ||
352 | if (sd->s_type & SYSFS_KOBJ_LINK) | |
353 | err = sysfs_attach_link(sd, dentry); | |
354 | else | |
355 | err = sysfs_attach_attr(sd, dentry); | |
356 | break; | |
357 | } | |
358 | } | |
359 | ||
360 | return ERR_PTR(err); | |
361 | } | |
362 | ||
c5ef1c42 | 363 | const struct inode_operations sysfs_dir_inode_operations = { |
1da177e4 | 364 | .lookup = sysfs_lookup, |
988d186d | 365 | .setattr = sysfs_setattr, |
1da177e4 LT |
366 | }; |
367 | ||
368 | static void remove_dir(struct dentry * d) | |
369 | { | |
370 | struct dentry * parent = dget(d->d_parent); | |
371 | struct sysfs_dirent * sd; | |
372 | ||
1b1dcc1b | 373 | mutex_lock(&parent->d_inode->i_mutex); |
1da177e4 LT |
374 | d_delete(d); |
375 | sd = d->d_fsdata; | |
376 | list_del_init(&sd->s_sibling); | |
377 | sysfs_put(sd); | |
378 | if (d->d_inode) | |
379 | simple_rmdir(parent->d_inode,d); | |
380 | ||
381 | pr_debug(" o %s removing done (%d)\n",d->d_name.name, | |
382 | atomic_read(&d->d_count)); | |
383 | ||
1b1dcc1b | 384 | mutex_unlock(&parent->d_inode->i_mutex); |
1da177e4 LT |
385 | dput(parent); |
386 | } | |
387 | ||
388 | void sysfs_remove_subdir(struct dentry * d) | |
389 | { | |
390 | remove_dir(d); | |
391 | } | |
392 | ||
393 | ||
b592fcfe | 394 | static void __sysfs_remove_dir(struct dentry *dentry) |
1da177e4 | 395 | { |
1da177e4 LT |
396 | struct sysfs_dirent * parent_sd; |
397 | struct sysfs_dirent * sd, * tmp; | |
398 | ||
b592fcfe | 399 | dget(dentry); |
1da177e4 LT |
400 | if (!dentry) |
401 | return; | |
402 | ||
403 | pr_debug("sysfs %s: removing dir\n",dentry->d_name.name); | |
1b1dcc1b | 404 | mutex_lock(&dentry->d_inode->i_mutex); |
1da177e4 LT |
405 | parent_sd = dentry->d_fsdata; |
406 | list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) { | |
407 | if (!sd->s_element || !(sd->s_type & SYSFS_NOT_PINNED)) | |
408 | continue; | |
409 | list_del_init(&sd->s_sibling); | |
410 | sysfs_drop_dentry(sd, dentry); | |
411 | sysfs_put(sd); | |
412 | } | |
1b1dcc1b | 413 | mutex_unlock(&dentry->d_inode->i_mutex); |
1da177e4 LT |
414 | |
415 | remove_dir(dentry); | |
416 | /** | |
417 | * Drop reference from dget() on entrance. | |
418 | */ | |
419 | dput(dentry); | |
b592fcfe EB |
420 | } |
421 | ||
422 | /** | |
423 | * sysfs_remove_dir - remove an object's directory. | |
424 | * @kobj: object. | |
425 | * | |
426 | * The only thing special about this is that we remove any files in | |
427 | * the directory before we remove the directory, and we've inlined | |
428 | * what used to be sysfs_rmdir() below, instead of calling separately. | |
429 | */ | |
430 | ||
431 | void sysfs_remove_dir(struct kobject * kobj) | |
432 | { | |
433 | __sysfs_remove_dir(kobj->dentry); | |
641e6f30 | 434 | kobj->dentry = NULL; |
1da177e4 LT |
435 | } |
436 | ||
b592fcfe EB |
437 | int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent, |
438 | const char *new_name) | |
1da177e4 | 439 | { |
0c096b50 TH |
440 | struct sysfs_dirent *sd = kobj->dentry->d_fsdata; |
441 | struct sysfs_dirent *parent_sd = new_parent->d_fsdata; | |
442 | struct dentry *new_dentry; | |
443 | char *dup_name; | |
996b7376 | 444 | int error; |
1da177e4 | 445 | |
b592fcfe EB |
446 | if (!new_parent) |
447 | return -EFAULT; | |
1da177e4 LT |
448 | |
449 | down_write(&sysfs_rename_sem); | |
b592fcfe | 450 | mutex_lock(&new_parent->d_inode->i_mutex); |
1da177e4 | 451 | |
b592fcfe | 452 | new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name)); |
996b7376 TH |
453 | if (IS_ERR(new_dentry)) { |
454 | error = PTR_ERR(new_dentry); | |
455 | goto out_unlock; | |
1da177e4 | 456 | } |
996b7376 TH |
457 | |
458 | /* By allowing two different directories with the same | |
459 | * d_parent we allow this routine to move between different | |
460 | * shadows of the same directory | |
461 | */ | |
462 | error = -EINVAL; | |
463 | if (kobj->dentry->d_parent->d_inode != new_parent->d_inode || | |
464 | new_dentry->d_parent->d_inode != new_parent->d_inode || | |
465 | new_dentry == kobj->dentry) | |
466 | goto out_dput; | |
467 | ||
468 | error = -EEXIST; | |
469 | if (new_dentry->d_inode) | |
470 | goto out_dput; | |
471 | ||
0c096b50 TH |
472 | /* rename kobject and sysfs_dirent */ |
473 | error = -ENOMEM; | |
474 | new_name = dup_name = kstrdup(new_name, GFP_KERNEL); | |
475 | if (!new_name) | |
476 | goto out_drop; | |
477 | ||
996b7376 TH |
478 | error = kobject_set_name(kobj, "%s", new_name); |
479 | if (error) | |
0c096b50 | 480 | goto out_free; |
996b7376 | 481 | |
0c096b50 TH |
482 | kfree(sd->s_name); |
483 | sd->s_name = new_name; | |
484 | ||
485 | /* move under the new parent */ | |
996b7376 TH |
486 | d_add(new_dentry, NULL); |
487 | d_move(kobj->dentry, new_dentry); | |
488 | ||
996b7376 TH |
489 | list_del_init(&sd->s_sibling); |
490 | list_add(&sd->s_sibling, &parent_sd->s_children); | |
491 | ||
492 | error = 0; | |
493 | goto out_unlock; | |
494 | ||
0c096b50 TH |
495 | out_free: |
496 | kfree(dup_name); | |
996b7376 TH |
497 | out_drop: |
498 | d_drop(new_dentry); | |
499 | out_dput: | |
500 | dput(new_dentry); | |
501 | out_unlock: | |
b592fcfe | 502 | mutex_unlock(&new_parent->d_inode->i_mutex); |
1da177e4 | 503 | up_write(&sysfs_rename_sem); |
1da177e4 LT |
504 | return error; |
505 | } | |
506 | ||
8a82472f CH |
507 | int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent) |
508 | { | |
509 | struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry; | |
510 | struct sysfs_dirent *new_parent_sd, *sd; | |
511 | int error; | |
512 | ||
8a82472f CH |
513 | old_parent_dentry = kobj->parent ? |
514 | kobj->parent->dentry : sysfs_mount->mnt_sb->s_root; | |
c744aeae CH |
515 | new_parent_dentry = new_parent ? |
516 | new_parent->dentry : sysfs_mount->mnt_sb->s_root; | |
8a82472f | 517 | |
0de1517e ML |
518 | if (old_parent_dentry->d_inode == new_parent_dentry->d_inode) |
519 | return 0; /* nothing to move */ | |
8a82472f CH |
520 | again: |
521 | mutex_lock(&old_parent_dentry->d_inode->i_mutex); | |
522 | if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) { | |
523 | mutex_unlock(&old_parent_dentry->d_inode->i_mutex); | |
524 | goto again; | |
525 | } | |
526 | ||
527 | new_parent_sd = new_parent_dentry->d_fsdata; | |
528 | sd = kobj->dentry->d_fsdata; | |
529 | ||
530 | new_dentry = lookup_one_len(kobj->name, new_parent_dentry, | |
531 | strlen(kobj->name)); | |
532 | if (IS_ERR(new_dentry)) { | |
533 | error = PTR_ERR(new_dentry); | |
534 | goto out; | |
535 | } else | |
536 | error = 0; | |
537 | d_add(new_dentry, NULL); | |
538 | d_move(kobj->dentry, new_dentry); | |
539 | dput(new_dentry); | |
540 | ||
541 | /* Remove from old parent's list and insert into new parent's list. */ | |
542 | list_del_init(&sd->s_sibling); | |
543 | list_add(&sd->s_sibling, &new_parent_sd->s_children); | |
544 | ||
545 | out: | |
546 | mutex_unlock(&new_parent_dentry->d_inode->i_mutex); | |
547 | mutex_unlock(&old_parent_dentry->d_inode->i_mutex); | |
548 | ||
549 | return error; | |
550 | } | |
551 | ||
1da177e4 LT |
552 | static int sysfs_dir_open(struct inode *inode, struct file *file) |
553 | { | |
f427f5d5 | 554 | struct dentry * dentry = file->f_path.dentry; |
1da177e4 | 555 | struct sysfs_dirent * parent_sd = dentry->d_fsdata; |
a26cd722 | 556 | struct sysfs_dirent * sd; |
1da177e4 | 557 | |
1b1dcc1b | 558 | mutex_lock(&dentry->d_inode->i_mutex); |
0c096b50 | 559 | sd = sysfs_new_dirent("_DIR_", NULL, 0, 0); |
a26cd722 TH |
560 | if (sd) |
561 | sysfs_attach_dirent(sd, parent_sd, NULL); | |
1b1dcc1b | 562 | mutex_unlock(&dentry->d_inode->i_mutex); |
1da177e4 | 563 | |
a26cd722 TH |
564 | file->private_data = sd; |
565 | return sd ? 0 : -ENOMEM; | |
1da177e4 LT |
566 | } |
567 | ||
568 | static int sysfs_dir_close(struct inode *inode, struct file *file) | |
569 | { | |
f427f5d5 | 570 | struct dentry * dentry = file->f_path.dentry; |
1da177e4 LT |
571 | struct sysfs_dirent * cursor = file->private_data; |
572 | ||
1b1dcc1b | 573 | mutex_lock(&dentry->d_inode->i_mutex); |
1da177e4 | 574 | list_del_init(&cursor->s_sibling); |
1b1dcc1b | 575 | mutex_unlock(&dentry->d_inode->i_mutex); |
1da177e4 LT |
576 | |
577 | release_sysfs_dirent(cursor); | |
578 | ||
579 | return 0; | |
580 | } | |
581 | ||
582 | /* Relationship between s_mode and the DT_xxx types */ | |
583 | static inline unsigned char dt_type(struct sysfs_dirent *sd) | |
584 | { | |
585 | return (sd->s_mode >> 12) & 15; | |
586 | } | |
587 | ||
588 | static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir) | |
589 | { | |
f427f5d5 | 590 | struct dentry *dentry = filp->f_path.dentry; |
1da177e4 LT |
591 | struct sysfs_dirent * parent_sd = dentry->d_fsdata; |
592 | struct sysfs_dirent *cursor = filp->private_data; | |
593 | struct list_head *p, *q = &cursor->s_sibling; | |
594 | ino_t ino; | |
595 | int i = filp->f_pos; | |
596 | ||
597 | switch (i) { | |
598 | case 0: | |
dc351252 | 599 | ino = parent_sd->s_ino; |
1da177e4 LT |
600 | if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) |
601 | break; | |
602 | filp->f_pos++; | |
603 | i++; | |
604 | /* fallthrough */ | |
605 | case 1: | |
13b3086d TH |
606 | if (parent_sd->s_parent) |
607 | ino = parent_sd->s_parent->s_ino; | |
608 | else | |
609 | ino = parent_sd->s_ino; | |
1da177e4 LT |
610 | if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) |
611 | break; | |
612 | filp->f_pos++; | |
613 | i++; | |
614 | /* fallthrough */ | |
615 | default: | |
1bfba4e8 AM |
616 | if (filp->f_pos == 2) |
617 | list_move(q, &parent_sd->s_children); | |
618 | ||
1da177e4 LT |
619 | for (p=q->next; p!= &parent_sd->s_children; p=p->next) { |
620 | struct sysfs_dirent *next; | |
621 | const char * name; | |
622 | int len; | |
623 | ||
624 | next = list_entry(p, struct sysfs_dirent, | |
625 | s_sibling); | |
626 | if (!next->s_element) | |
627 | continue; | |
628 | ||
0c096b50 | 629 | name = next->s_name; |
1da177e4 | 630 | len = strlen(name); |
dc351252 | 631 | ino = next->s_ino; |
1da177e4 LT |
632 | |
633 | if (filldir(dirent, name, len, filp->f_pos, ino, | |
634 | dt_type(next)) < 0) | |
635 | return 0; | |
636 | ||
1bfba4e8 | 637 | list_move(q, p); |
1da177e4 LT |
638 | p = q; |
639 | filp->f_pos++; | |
640 | } | |
641 | } | |
642 | return 0; | |
643 | } | |
644 | ||
645 | static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin) | |
646 | { | |
f427f5d5 | 647 | struct dentry * dentry = file->f_path.dentry; |
1da177e4 | 648 | |
1b1dcc1b | 649 | mutex_lock(&dentry->d_inode->i_mutex); |
1da177e4 LT |
650 | switch (origin) { |
651 | case 1: | |
652 | offset += file->f_pos; | |
653 | case 0: | |
654 | if (offset >= 0) | |
655 | break; | |
656 | default: | |
f427f5d5 | 657 | mutex_unlock(&file->f_path.dentry->d_inode->i_mutex); |
1da177e4 LT |
658 | return -EINVAL; |
659 | } | |
660 | if (offset != file->f_pos) { | |
661 | file->f_pos = offset; | |
662 | if (file->f_pos >= 2) { | |
663 | struct sysfs_dirent *sd = dentry->d_fsdata; | |
664 | struct sysfs_dirent *cursor = file->private_data; | |
665 | struct list_head *p; | |
666 | loff_t n = file->f_pos - 2; | |
667 | ||
668 | list_del(&cursor->s_sibling); | |
669 | p = sd->s_children.next; | |
670 | while (n && p != &sd->s_children) { | |
671 | struct sysfs_dirent *next; | |
672 | next = list_entry(p, struct sysfs_dirent, | |
673 | s_sibling); | |
674 | if (next->s_element) | |
675 | n--; | |
676 | p = p->next; | |
677 | } | |
678 | list_add_tail(&cursor->s_sibling, p); | |
679 | } | |
680 | } | |
1b1dcc1b | 681 | mutex_unlock(&dentry->d_inode->i_mutex); |
1da177e4 LT |
682 | return offset; |
683 | } | |
684 | ||
b592fcfe EB |
685 | |
686 | /** | |
687 | * sysfs_make_shadowed_dir - Setup so a directory can be shadowed | |
688 | * @kobj: object we're creating shadow of. | |
689 | */ | |
690 | ||
691 | int sysfs_make_shadowed_dir(struct kobject *kobj, | |
692 | void * (*follow_link)(struct dentry *, struct nameidata *)) | |
693 | { | |
694 | struct inode *inode; | |
695 | struct inode_operations *i_op; | |
696 | ||
697 | inode = kobj->dentry->d_inode; | |
698 | if (inode->i_op != &sysfs_dir_inode_operations) | |
699 | return -EINVAL; | |
700 | ||
701 | i_op = kmalloc(sizeof(*i_op), GFP_KERNEL); | |
702 | if (!i_op) | |
703 | return -ENOMEM; | |
704 | ||
705 | memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op)); | |
706 | i_op->follow_link = follow_link; | |
707 | ||
708 | /* Locking of inode->i_op? | |
709 | * Since setting i_op is a single word write and they | |
710 | * are atomic we should be ok here. | |
711 | */ | |
712 | inode->i_op = i_op; | |
713 | return 0; | |
714 | } | |
715 | ||
716 | /** | |
717 | * sysfs_create_shadow_dir - create a shadow directory for an object. | |
718 | * @kobj: object we're creating directory for. | |
719 | * | |
720 | * sysfs_make_shadowed_dir must already have been called on this | |
721 | * directory. | |
722 | */ | |
723 | ||
724 | struct dentry *sysfs_create_shadow_dir(struct kobject *kobj) | |
725 | { | |
13b3086d TH |
726 | struct dentry *dir = kobj->dentry; |
727 | struct inode *inode = dir->d_inode; | |
728 | struct dentry *parent = dir->d_parent; | |
729 | struct sysfs_dirent *parent_sd = parent->d_fsdata; | |
730 | struct dentry *shadow; | |
b592fcfe | 731 | struct sysfs_dirent *sd; |
b592fcfe | 732 | |
b592fcfe EB |
733 | shadow = ERR_PTR(-EINVAL); |
734 | if (!sysfs_is_shadowed_inode(inode)) | |
735 | goto out; | |
736 | ||
737 | shadow = d_alloc(parent, &dir->d_name); | |
738 | if (!shadow) | |
739 | goto nomem; | |
740 | ||
0c096b50 | 741 | sd = sysfs_new_dirent("_SHADOW_", kobj, inode->i_mode, SYSFS_DIR); |
b592fcfe EB |
742 | if (!sd) |
743 | goto nomem; | |
13b3086d TH |
744 | /* point to parent_sd but don't attach to it */ |
745 | sd->s_parent = sysfs_get(parent_sd); | |
a26cd722 | 746 | sysfs_attach_dirent(sd, NULL, shadow); |
b592fcfe EB |
747 | |
748 | d_instantiate(shadow, igrab(inode)); | |
749 | inc_nlink(inode); | |
750 | inc_nlink(parent->d_inode); | |
751 | shadow->d_op = &sysfs_dentry_ops; | |
752 | ||
753 | dget(shadow); /* Extra count - pin the dentry in core */ | |
754 | ||
755 | out: | |
756 | return shadow; | |
757 | nomem: | |
758 | dput(shadow); | |
759 | shadow = ERR_PTR(-ENOMEM); | |
760 | goto out; | |
761 | } | |
762 | ||
763 | /** | |
764 | * sysfs_remove_shadow_dir - remove an object's directory. | |
765 | * @shadow: dentry of shadow directory | |
766 | * | |
767 | * The only thing special about this is that we remove any files in | |
768 | * the directory before we remove the directory, and we've inlined | |
769 | * what used to be sysfs_rmdir() below, instead of calling separately. | |
770 | */ | |
771 | ||
772 | void sysfs_remove_shadow_dir(struct dentry *shadow) | |
773 | { | |
774 | __sysfs_remove_dir(shadow); | |
775 | } | |
776 | ||
4b6f5d20 | 777 | const struct file_operations sysfs_dir_operations = { |
1da177e4 LT |
778 | .open = sysfs_dir_open, |
779 | .release = sysfs_dir_close, | |
780 | .llseek = sysfs_dir_lseek, | |
781 | .read = generic_read_dir, | |
782 | .readdir = sysfs_readdir, | |
783 | }; |