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