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