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