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