]>
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 | { | |
50 | if (sd->s_type & SYSFS_KOBJ_LINK) { | |
51 | struct sysfs_symlink * sl = sd->s_element; | |
52 | kfree(sl->link_name); | |
53 | kobject_put(sl->target_kobj); | |
54 | kfree(sl); | |
55 | } | |
56 | kfree(sd->s_iattr); | |
2b611bb7 | 57 | sysfs_free_ino(sd->s_ino); |
fa7f912a TH |
58 | kmem_cache_free(sysfs_dir_cachep, sd); |
59 | } | |
60 | ||
1da177e4 LT |
61 | static void sysfs_d_iput(struct dentry * dentry, struct inode * inode) |
62 | { | |
63 | struct sysfs_dirent * sd = dentry->d_fsdata; | |
64 | ||
65 | if (sd) { | |
dd14cbc9 TH |
66 | /* sd->s_dentry is protected with sysfs_lock. This |
67 | * allows sysfs_drop_dentry() to dereference it. | |
68 | */ | |
69 | spin_lock(&sysfs_lock); | |
70 | ||
71 | /* The dentry might have been deleted or another | |
72 | * lookup could have happened updating sd->s_dentry to | |
73 | * point the new dentry. Ignore if it isn't pointing | |
74 | * to this dentry. | |
75 | */ | |
76 | if (sd->s_dentry == dentry) | |
77 | sd->s_dentry = NULL; | |
78 | spin_unlock(&sysfs_lock); | |
1da177e4 LT |
79 | sysfs_put(sd); |
80 | } | |
81 | iput(inode); | |
82 | } | |
83 | ||
84 | static struct dentry_operations sysfs_dentry_ops = { | |
85 | .d_iput = sysfs_d_iput, | |
86 | }; | |
87 | ||
88 | /* | |
89 | * Allocates a new sysfs_dirent and links it to the parent sysfs_dirent | |
90 | */ | |
b592fcfe | 91 | static struct sysfs_dirent * __sysfs_new_dirent(void * element) |
1da177e4 LT |
92 | { |
93 | struct sysfs_dirent * sd; | |
94 | ||
c3762229 | 95 | sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL); |
1da177e4 LT |
96 | if (!sd) |
97 | return NULL; | |
98 | ||
2b611bb7 TH |
99 | if (sysfs_alloc_ino(&sd->s_ino)) { |
100 | kmem_cache_free(sysfs_dir_cachep, sd); | |
101 | return NULL; | |
102 | } | |
103 | ||
1da177e4 | 104 | atomic_set(&sd->s_count, 1); |
eea3f891 | 105 | atomic_set(&sd->s_event, 1); |
1da177e4 | 106 | INIT_LIST_HEAD(&sd->s_children); |
b592fcfe | 107 | INIT_LIST_HEAD(&sd->s_sibling); |
1da177e4 LT |
108 | sd->s_element = element; |
109 | ||
110 | return sd; | |
111 | } | |
112 | ||
b592fcfe EB |
113 | static void __sysfs_list_dirent(struct sysfs_dirent *parent_sd, |
114 | struct sysfs_dirent *sd) | |
115 | { | |
116 | if (sd) | |
117 | list_add(&sd->s_sibling, &parent_sd->s_children); | |
118 | } | |
119 | ||
120 | static struct sysfs_dirent * sysfs_new_dirent(struct sysfs_dirent *parent_sd, | |
121 | void * element) | |
122 | { | |
123 | struct sysfs_dirent *sd; | |
124 | sd = __sysfs_new_dirent(element); | |
125 | __sysfs_list_dirent(parent_sd, sd); | |
126 | return sd; | |
127 | } | |
128 | ||
a580290c | 129 | /* |
c516865c MS |
130 | * |
131 | * Return -EEXIST if there is already a sysfs element with the same name for | |
132 | * the same parent. | |
133 | * | |
134 | * called with parent inode's i_mutex held | |
135 | */ | |
136 | int sysfs_dirent_exist(struct sysfs_dirent *parent_sd, | |
137 | const unsigned char *new) | |
138 | { | |
139 | struct sysfs_dirent * sd; | |
140 | ||
141 | list_for_each_entry(sd, &parent_sd->s_children, s_sibling) { | |
142 | if (sd->s_element) { | |
143 | const unsigned char *existing = sysfs_get_name(sd); | |
144 | if (strcmp(existing, new)) | |
145 | continue; | |
146 | else | |
147 | return -EEXIST; | |
148 | } | |
149 | } | |
150 | ||
151 | return 0; | |
152 | } | |
153 | ||
154 | ||
b592fcfe EB |
155 | static struct sysfs_dirent * |
156 | __sysfs_make_dirent(struct dentry *dentry, void *element, mode_t mode, int type) | |
1da177e4 LT |
157 | { |
158 | struct sysfs_dirent * sd; | |
159 | ||
b592fcfe | 160 | sd = __sysfs_new_dirent(element); |
1da177e4 | 161 | if (!sd) |
b592fcfe | 162 | goto out; |
1da177e4 LT |
163 | |
164 | sd->s_mode = mode; | |
165 | sd->s_type = type; | |
166 | sd->s_dentry = dentry; | |
167 | if (dentry) { | |
168 | dentry->d_fsdata = sysfs_get(sd); | |
169 | dentry->d_op = &sysfs_dentry_ops; | |
170 | } | |
171 | ||
b592fcfe EB |
172 | out: |
173 | return sd; | |
174 | } | |
175 | ||
176 | int sysfs_make_dirent(struct sysfs_dirent * parent_sd, struct dentry * dentry, | |
177 | void * element, umode_t mode, int type) | |
178 | { | |
179 | struct sysfs_dirent *sd; | |
180 | ||
181 | sd = __sysfs_make_dirent(dentry, element, mode, type); | |
182 | __sysfs_list_dirent(parent_sd, sd); | |
183 | ||
184 | return sd ? 0 : -ENOMEM; | |
1da177e4 LT |
185 | } |
186 | ||
187 | static int init_dir(struct inode * inode) | |
188 | { | |
189 | inode->i_op = &sysfs_dir_inode_operations; | |
190 | inode->i_fop = &sysfs_dir_operations; | |
191 | ||
192 | /* directory inodes start off with i_nlink == 2 (for "." entry) */ | |
d8c76e6f | 193 | inc_nlink(inode); |
1da177e4 LT |
194 | return 0; |
195 | } | |
196 | ||
197 | static int init_file(struct inode * inode) | |
198 | { | |
199 | inode->i_size = PAGE_SIZE; | |
200 | inode->i_fop = &sysfs_file_operations; | |
201 | return 0; | |
202 | } | |
203 | ||
204 | static int init_symlink(struct inode * inode) | |
205 | { | |
206 | inode->i_op = &sysfs_symlink_inode_operations; | |
207 | return 0; | |
208 | } | |
209 | ||
210 | static int create_dir(struct kobject * k, struct dentry * p, | |
211 | const char * n, struct dentry ** d) | |
212 | { | |
213 | int error; | |
214 | umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO; | |
215 | ||
1b1dcc1b | 216 | mutex_lock(&p->d_inode->i_mutex); |
5f45f1a7 | 217 | *d = lookup_one_len(n, p, strlen(n)); |
1da177e4 | 218 | if (!IS_ERR(*d)) { |
c516865c MS |
219 | if (sysfs_dirent_exist(p->d_fsdata, n)) |
220 | error = -EEXIST; | |
221 | else | |
222 | error = sysfs_make_dirent(p->d_fsdata, *d, k, mode, | |
223 | SYSFS_DIR); | |
1da177e4 | 224 | if (!error) { |
6fa5c828 | 225 | error = sysfs_create(*d, mode, init_dir); |
1da177e4 | 226 | if (!error) { |
d8c76e6f | 227 | inc_nlink(p->d_inode); |
1da177e4 LT |
228 | (*d)->d_op = &sysfs_dentry_ops; |
229 | d_rehash(*d); | |
230 | } | |
231 | } | |
6fa5c828 | 232 | if (error && (error != -EEXIST)) { |
e80a5dea SR |
233 | struct sysfs_dirent *sd = (*d)->d_fsdata; |
234 | if (sd) { | |
235 | list_del_init(&sd->s_sibling); | |
236 | sysfs_put(sd); | |
237 | } | |
1da177e4 | 238 | d_drop(*d); |
6fa5c828 | 239 | } |
1da177e4 LT |
240 | dput(*d); |
241 | } else | |
242 | error = PTR_ERR(*d); | |
1b1dcc1b | 243 | mutex_unlock(&p->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) { | |
349 | const unsigned char * name = sysfs_get_name(sd); | |
350 | ||
351 | if (strcmp(name, dentry->d_name.name)) | |
352 | continue; | |
353 | ||
354 | if (sd->s_type & SYSFS_KOBJ_LINK) | |
355 | err = sysfs_attach_link(sd, dentry); | |
356 | else | |
357 | err = sysfs_attach_attr(sd, dentry); | |
358 | break; | |
359 | } | |
360 | } | |
361 | ||
362 | return ERR_PTR(err); | |
363 | } | |
364 | ||
c5ef1c42 | 365 | const struct inode_operations sysfs_dir_inode_operations = { |
1da177e4 | 366 | .lookup = sysfs_lookup, |
988d186d | 367 | .setattr = sysfs_setattr, |
1da177e4 LT |
368 | }; |
369 | ||
370 | static void remove_dir(struct dentry * d) | |
371 | { | |
372 | struct dentry * parent = dget(d->d_parent); | |
373 | struct sysfs_dirent * sd; | |
374 | ||
1b1dcc1b | 375 | mutex_lock(&parent->d_inode->i_mutex); |
1da177e4 LT |
376 | d_delete(d); |
377 | sd = d->d_fsdata; | |
378 | list_del_init(&sd->s_sibling); | |
379 | sysfs_put(sd); | |
380 | if (d->d_inode) | |
381 | simple_rmdir(parent->d_inode,d); | |
382 | ||
383 | pr_debug(" o %s removing done (%d)\n",d->d_name.name, | |
384 | atomic_read(&d->d_count)); | |
385 | ||
1b1dcc1b | 386 | mutex_unlock(&parent->d_inode->i_mutex); |
1da177e4 LT |
387 | dput(parent); |
388 | } | |
389 | ||
390 | void sysfs_remove_subdir(struct dentry * d) | |
391 | { | |
392 | remove_dir(d); | |
393 | } | |
394 | ||
395 | ||
b592fcfe | 396 | static void __sysfs_remove_dir(struct dentry *dentry) |
1da177e4 | 397 | { |
1da177e4 LT |
398 | struct sysfs_dirent * parent_sd; |
399 | struct sysfs_dirent * sd, * tmp; | |
400 | ||
b592fcfe | 401 | dget(dentry); |
1da177e4 LT |
402 | if (!dentry) |
403 | return; | |
404 | ||
405 | pr_debug("sysfs %s: removing dir\n",dentry->d_name.name); | |
1b1dcc1b | 406 | mutex_lock(&dentry->d_inode->i_mutex); |
1da177e4 LT |
407 | parent_sd = dentry->d_fsdata; |
408 | list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) { | |
409 | if (!sd->s_element || !(sd->s_type & SYSFS_NOT_PINNED)) | |
410 | continue; | |
411 | list_del_init(&sd->s_sibling); | |
412 | sysfs_drop_dentry(sd, dentry); | |
413 | sysfs_put(sd); | |
414 | } | |
1b1dcc1b | 415 | mutex_unlock(&dentry->d_inode->i_mutex); |
1da177e4 LT |
416 | |
417 | remove_dir(dentry); | |
418 | /** | |
419 | * Drop reference from dget() on entrance. | |
420 | */ | |
421 | dput(dentry); | |
b592fcfe EB |
422 | } |
423 | ||
424 | /** | |
425 | * sysfs_remove_dir - remove an object's directory. | |
426 | * @kobj: object. | |
427 | * | |
428 | * The only thing special about this is that we remove any files in | |
429 | * the directory before we remove the directory, and we've inlined | |
430 | * what used to be sysfs_rmdir() below, instead of calling separately. | |
431 | */ | |
432 | ||
433 | void sysfs_remove_dir(struct kobject * kobj) | |
434 | { | |
435 | __sysfs_remove_dir(kobj->dentry); | |
641e6f30 | 436 | kobj->dentry = NULL; |
1da177e4 LT |
437 | } |
438 | ||
b592fcfe EB |
439 | int sysfs_rename_dir(struct kobject * kobj, struct dentry *new_parent, |
440 | const char *new_name) | |
1da177e4 LT |
441 | { |
442 | int error = 0; | |
b592fcfe | 443 | struct dentry * new_dentry; |
1da177e4 | 444 | |
b592fcfe EB |
445 | if (!new_parent) |
446 | return -EFAULT; | |
1da177e4 LT |
447 | |
448 | down_write(&sysfs_rename_sem); | |
b592fcfe | 449 | mutex_lock(&new_parent->d_inode->i_mutex); |
1da177e4 | 450 | |
b592fcfe | 451 | new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name)); |
1da177e4 | 452 | if (!IS_ERR(new_dentry)) { |
b592fcfe EB |
453 | /* By allowing two different directories with the |
454 | * same d_parent we allow this routine to move | |
455 | * between different shadows of the same directory | |
456 | */ | |
457 | if (kobj->dentry->d_parent->d_inode != new_parent->d_inode) | |
458 | return -EINVAL; | |
459 | else if (new_dentry->d_parent->d_inode != new_parent->d_inode) | |
460 | error = -EINVAL; | |
461 | else if (new_dentry == kobj->dentry) | |
462 | error = -EINVAL; | |
463 | else if (!new_dentry->d_inode) { | |
1da177e4 LT |
464 | error = kobject_set_name(kobj, "%s", new_name); |
465 | if (!error) { | |
b592fcfe EB |
466 | struct sysfs_dirent *sd, *parent_sd; |
467 | ||
1da177e4 LT |
468 | d_add(new_dentry, NULL); |
469 | d_move(kobj->dentry, new_dentry); | |
b592fcfe EB |
470 | |
471 | sd = kobj->dentry->d_fsdata; | |
472 | parent_sd = new_parent->d_fsdata; | |
473 | ||
474 | list_del_init(&sd->s_sibling); | |
475 | list_add(&sd->s_sibling, &parent_sd->s_children); | |
1da177e4 LT |
476 | } |
477 | else | |
478 | d_drop(new_dentry); | |
479 | } else | |
480 | error = -EEXIST; | |
481 | dput(new_dentry); | |
482 | } | |
b592fcfe | 483 | mutex_unlock(&new_parent->d_inode->i_mutex); |
1da177e4 LT |
484 | up_write(&sysfs_rename_sem); |
485 | ||
486 | return error; | |
487 | } | |
488 | ||
8a82472f CH |
489 | int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent) |
490 | { | |
491 | struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry; | |
492 | struct sysfs_dirent *new_parent_sd, *sd; | |
493 | int error; | |
494 | ||
8a82472f CH |
495 | old_parent_dentry = kobj->parent ? |
496 | kobj->parent->dentry : sysfs_mount->mnt_sb->s_root; | |
c744aeae CH |
497 | new_parent_dentry = new_parent ? |
498 | new_parent->dentry : sysfs_mount->mnt_sb->s_root; | |
8a82472f | 499 | |
0de1517e ML |
500 | if (old_parent_dentry->d_inode == new_parent_dentry->d_inode) |
501 | return 0; /* nothing to move */ | |
8a82472f CH |
502 | again: |
503 | mutex_lock(&old_parent_dentry->d_inode->i_mutex); | |
504 | if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) { | |
505 | mutex_unlock(&old_parent_dentry->d_inode->i_mutex); | |
506 | goto again; | |
507 | } | |
508 | ||
509 | new_parent_sd = new_parent_dentry->d_fsdata; | |
510 | sd = kobj->dentry->d_fsdata; | |
511 | ||
512 | new_dentry = lookup_one_len(kobj->name, new_parent_dentry, | |
513 | strlen(kobj->name)); | |
514 | if (IS_ERR(new_dentry)) { | |
515 | error = PTR_ERR(new_dentry); | |
516 | goto out; | |
517 | } else | |
518 | error = 0; | |
519 | d_add(new_dentry, NULL); | |
520 | d_move(kobj->dentry, new_dentry); | |
521 | dput(new_dentry); | |
522 | ||
523 | /* Remove from old parent's list and insert into new parent's list. */ | |
524 | list_del_init(&sd->s_sibling); | |
525 | list_add(&sd->s_sibling, &new_parent_sd->s_children); | |
526 | ||
527 | out: | |
528 | mutex_unlock(&new_parent_dentry->d_inode->i_mutex); | |
529 | mutex_unlock(&old_parent_dentry->d_inode->i_mutex); | |
530 | ||
531 | return error; | |
532 | } | |
533 | ||
1da177e4 LT |
534 | static int sysfs_dir_open(struct inode *inode, struct file *file) |
535 | { | |
f427f5d5 | 536 | struct dentry * dentry = file->f_path.dentry; |
1da177e4 LT |
537 | struct sysfs_dirent * parent_sd = dentry->d_fsdata; |
538 | ||
1b1dcc1b | 539 | mutex_lock(&dentry->d_inode->i_mutex); |
1da177e4 | 540 | file->private_data = sysfs_new_dirent(parent_sd, NULL); |
1b1dcc1b | 541 | mutex_unlock(&dentry->d_inode->i_mutex); |
1da177e4 LT |
542 | |
543 | return file->private_data ? 0 : -ENOMEM; | |
544 | ||
545 | } | |
546 | ||
547 | static int sysfs_dir_close(struct inode *inode, struct file *file) | |
548 | { | |
f427f5d5 | 549 | struct dentry * dentry = file->f_path.dentry; |
1da177e4 LT |
550 | struct sysfs_dirent * cursor = file->private_data; |
551 | ||
1b1dcc1b | 552 | mutex_lock(&dentry->d_inode->i_mutex); |
1da177e4 | 553 | list_del_init(&cursor->s_sibling); |
1b1dcc1b | 554 | mutex_unlock(&dentry->d_inode->i_mutex); |
1da177e4 LT |
555 | |
556 | release_sysfs_dirent(cursor); | |
557 | ||
558 | return 0; | |
559 | } | |
560 | ||
561 | /* Relationship between s_mode and the DT_xxx types */ | |
562 | static inline unsigned char dt_type(struct sysfs_dirent *sd) | |
563 | { | |
564 | return (sd->s_mode >> 12) & 15; | |
565 | } | |
566 | ||
567 | static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir) | |
568 | { | |
f427f5d5 | 569 | struct dentry *dentry = filp->f_path.dentry; |
1da177e4 LT |
570 | struct sysfs_dirent * parent_sd = dentry->d_fsdata; |
571 | struct sysfs_dirent *cursor = filp->private_data; | |
572 | struct list_head *p, *q = &cursor->s_sibling; | |
573 | ino_t ino; | |
574 | int i = filp->f_pos; | |
575 | ||
576 | switch (i) { | |
577 | case 0: | |
dc351252 | 578 | ino = parent_sd->s_ino; |
1da177e4 LT |
579 | if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) |
580 | break; | |
581 | filp->f_pos++; | |
582 | i++; | |
583 | /* fallthrough */ | |
584 | case 1: | |
585 | ino = parent_ino(dentry); | |
586 | if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) | |
587 | break; | |
588 | filp->f_pos++; | |
589 | i++; | |
590 | /* fallthrough */ | |
591 | default: | |
1bfba4e8 AM |
592 | if (filp->f_pos == 2) |
593 | list_move(q, &parent_sd->s_children); | |
594 | ||
1da177e4 LT |
595 | for (p=q->next; p!= &parent_sd->s_children; p=p->next) { |
596 | struct sysfs_dirent *next; | |
597 | const char * name; | |
598 | int len; | |
599 | ||
600 | next = list_entry(p, struct sysfs_dirent, | |
601 | s_sibling); | |
602 | if (!next->s_element) | |
603 | continue; | |
604 | ||
605 | name = sysfs_get_name(next); | |
606 | len = strlen(name); | |
dc351252 | 607 | ino = next->s_ino; |
1da177e4 LT |
608 | |
609 | if (filldir(dirent, name, len, filp->f_pos, ino, | |
610 | dt_type(next)) < 0) | |
611 | return 0; | |
612 | ||
1bfba4e8 | 613 | list_move(q, p); |
1da177e4 LT |
614 | p = q; |
615 | filp->f_pos++; | |
616 | } | |
617 | } | |
618 | return 0; | |
619 | } | |
620 | ||
621 | static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin) | |
622 | { | |
f427f5d5 | 623 | struct dentry * dentry = file->f_path.dentry; |
1da177e4 | 624 | |
1b1dcc1b | 625 | mutex_lock(&dentry->d_inode->i_mutex); |
1da177e4 LT |
626 | switch (origin) { |
627 | case 1: | |
628 | offset += file->f_pos; | |
629 | case 0: | |
630 | if (offset >= 0) | |
631 | break; | |
632 | default: | |
f427f5d5 | 633 | mutex_unlock(&file->f_path.dentry->d_inode->i_mutex); |
1da177e4 LT |
634 | return -EINVAL; |
635 | } | |
636 | if (offset != file->f_pos) { | |
637 | file->f_pos = offset; | |
638 | if (file->f_pos >= 2) { | |
639 | struct sysfs_dirent *sd = dentry->d_fsdata; | |
640 | struct sysfs_dirent *cursor = file->private_data; | |
641 | struct list_head *p; | |
642 | loff_t n = file->f_pos - 2; | |
643 | ||
644 | list_del(&cursor->s_sibling); | |
645 | p = sd->s_children.next; | |
646 | while (n && p != &sd->s_children) { | |
647 | struct sysfs_dirent *next; | |
648 | next = list_entry(p, struct sysfs_dirent, | |
649 | s_sibling); | |
650 | if (next->s_element) | |
651 | n--; | |
652 | p = p->next; | |
653 | } | |
654 | list_add_tail(&cursor->s_sibling, p); | |
655 | } | |
656 | } | |
1b1dcc1b | 657 | mutex_unlock(&dentry->d_inode->i_mutex); |
1da177e4 LT |
658 | return offset; |
659 | } | |
660 | ||
b592fcfe EB |
661 | |
662 | /** | |
663 | * sysfs_make_shadowed_dir - Setup so a directory can be shadowed | |
664 | * @kobj: object we're creating shadow of. | |
665 | */ | |
666 | ||
667 | int sysfs_make_shadowed_dir(struct kobject *kobj, | |
668 | void * (*follow_link)(struct dentry *, struct nameidata *)) | |
669 | { | |
670 | struct inode *inode; | |
671 | struct inode_operations *i_op; | |
672 | ||
673 | inode = kobj->dentry->d_inode; | |
674 | if (inode->i_op != &sysfs_dir_inode_operations) | |
675 | return -EINVAL; | |
676 | ||
677 | i_op = kmalloc(sizeof(*i_op), GFP_KERNEL); | |
678 | if (!i_op) | |
679 | return -ENOMEM; | |
680 | ||
681 | memcpy(i_op, &sysfs_dir_inode_operations, sizeof(*i_op)); | |
682 | i_op->follow_link = follow_link; | |
683 | ||
684 | /* Locking of inode->i_op? | |
685 | * Since setting i_op is a single word write and they | |
686 | * are atomic we should be ok here. | |
687 | */ | |
688 | inode->i_op = i_op; | |
689 | return 0; | |
690 | } | |
691 | ||
692 | /** | |
693 | * sysfs_create_shadow_dir - create a shadow directory for an object. | |
694 | * @kobj: object we're creating directory for. | |
695 | * | |
696 | * sysfs_make_shadowed_dir must already have been called on this | |
697 | * directory. | |
698 | */ | |
699 | ||
700 | struct dentry *sysfs_create_shadow_dir(struct kobject *kobj) | |
701 | { | |
702 | struct sysfs_dirent *sd; | |
703 | struct dentry *parent, *dir, *shadow; | |
704 | struct inode *inode; | |
705 | ||
706 | dir = kobj->dentry; | |
707 | inode = dir->d_inode; | |
708 | parent = dir->d_parent; | |
709 | shadow = ERR_PTR(-EINVAL); | |
710 | if (!sysfs_is_shadowed_inode(inode)) | |
711 | goto out; | |
712 | ||
713 | shadow = d_alloc(parent, &dir->d_name); | |
714 | if (!shadow) | |
715 | goto nomem; | |
716 | ||
717 | sd = __sysfs_make_dirent(shadow, kobj, inode->i_mode, SYSFS_DIR); | |
718 | if (!sd) | |
719 | goto nomem; | |
720 | ||
721 | d_instantiate(shadow, igrab(inode)); | |
722 | inc_nlink(inode); | |
723 | inc_nlink(parent->d_inode); | |
724 | shadow->d_op = &sysfs_dentry_ops; | |
725 | ||
726 | dget(shadow); /* Extra count - pin the dentry in core */ | |
727 | ||
728 | out: | |
729 | return shadow; | |
730 | nomem: | |
731 | dput(shadow); | |
732 | shadow = ERR_PTR(-ENOMEM); | |
733 | goto out; | |
734 | } | |
735 | ||
736 | /** | |
737 | * sysfs_remove_shadow_dir - remove an object's directory. | |
738 | * @shadow: dentry of shadow directory | |
739 | * | |
740 | * The only thing special about this is that we remove any files in | |
741 | * the directory before we remove the directory, and we've inlined | |
742 | * what used to be sysfs_rmdir() below, instead of calling separately. | |
743 | */ | |
744 | ||
745 | void sysfs_remove_shadow_dir(struct dentry *shadow) | |
746 | { | |
747 | __sysfs_remove_dir(shadow); | |
748 | } | |
749 | ||
4b6f5d20 | 750 | const struct file_operations sysfs_dir_operations = { |
1da177e4 LT |
751 | .open = sysfs_dir_open, |
752 | .release = sysfs_dir_close, | |
753 | .llseek = sysfs_dir_lseek, | |
754 | .read = generic_read_dir, | |
755 | .readdir = sysfs_readdir, | |
756 | }; |