Commit | Line | Data |
---|---|---|
a8fa94e0 TH |
1 | /* |
2 | * fs/kernfs/dir.c - kernfs directory implementation | |
3 | * | |
4 | * Copyright (c) 2001-3 Patrick Mochel | |
5 | * Copyright (c) 2007 SUSE Linux Products GmbH | |
6 | * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org> | |
7 | * | |
8 | * This file is released under the GPLv2. | |
9 | */ | |
10 | ||
11 | #include <linux/sched.h> | |
12 | #include <linux/fs.h> | |
13 | #include <linux/namei.h> | |
14 | #include <linux/idr.h> | |
15 | #include <linux/slab.h> | |
16 | #include <linux/security.h> | |
17 | #include <linux/hash.h> | |
18 | ||
19 | #include "kernfs-internal.h" | |
20 | ||
21 | DEFINE_MUTEX(kernfs_mutex); | |
22 | ||
23 | #define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb) | |
24 | ||
25 | static bool kernfs_active(struct kernfs_node *kn) | |
26 | { | |
27 | lockdep_assert_held(&kernfs_mutex); | |
28 | return atomic_read(&kn->active) >= 0; | |
29 | } | |
30 | ||
31 | static bool kernfs_lockdep(struct kernfs_node *kn) | |
32 | { | |
33 | #ifdef CONFIG_DEBUG_LOCK_ALLOC | |
34 | return kn->flags & KERNFS_LOCKDEP; | |
35 | #else | |
36 | return false; | |
37 | #endif | |
38 | } | |
39 | ||
40 | /** | |
41 | * kernfs_name_hash | |
42 | * @name: Null terminated string to hash | |
43 | * @ns: Namespace tag to hash | |
44 | * | |
45 | * Returns 31 bit hash of ns + name (so it fits in an off_t ) | |
46 | */ | |
47 | static unsigned int kernfs_name_hash(const char *name, const void *ns) | |
48 | { | |
49 | unsigned long hash = init_name_hash(); | |
50 | unsigned int len = strlen(name); | |
51 | while (len--) | |
52 | hash = partial_name_hash(*name++, hash); | |
53 | hash = (end_name_hash(hash) ^ hash_ptr((void *)ns, 31)); | |
54 | hash &= 0x7fffffffU; | |
55 | /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */ | |
56 | if (hash < 1) | |
57 | hash += 2; | |
58 | if (hash >= INT_MAX) | |
59 | hash = INT_MAX - 1; | |
60 | return hash; | |
61 | } | |
62 | ||
63 | static int kernfs_name_compare(unsigned int hash, const char *name, | |
64 | const void *ns, const struct kernfs_node *kn) | |
65 | { | |
66 | if (hash != kn->hash) | |
67 | return hash - kn->hash; | |
68 | if (ns != kn->ns) | |
69 | return ns - kn->ns; | |
70 | return strcmp(name, kn->name); | |
71 | } | |
72 | ||
73 | static int kernfs_sd_compare(const struct kernfs_node *left, | |
74 | const struct kernfs_node *right) | |
75 | { | |
76 | return kernfs_name_compare(left->hash, left->name, left->ns, right); | |
77 | } | |
78 | ||
79 | /** | |
80 | * kernfs_link_sibling - link kernfs_node into sibling rbtree | |
81 | * @kn: kernfs_node of interest | |
82 | * | |
83 | * Link @kn into its sibling rbtree which starts from | |
84 | * @kn->parent->dir.children. | |
85 | * | |
86 | * Locking: | |
87 | * mutex_lock(kernfs_mutex) | |
88 | * | |
89 | * RETURNS: | |
90 | * 0 on susccess -EEXIST on failure. | |
91 | */ | |
92 | static int kernfs_link_sibling(struct kernfs_node *kn) | |
93 | { | |
94 | struct rb_node **node = &kn->parent->dir.children.rb_node; | |
95 | struct rb_node *parent = NULL; | |
96 | ||
97 | if (kernfs_type(kn) == KERNFS_DIR) | |
98 | kn->parent->dir.subdirs++; | |
99 | ||
100 | while (*node) { | |
101 | struct kernfs_node *pos; | |
102 | int result; | |
103 | ||
104 | pos = rb_to_kn(*node); | |
105 | parent = *node; | |
106 | result = kernfs_sd_compare(kn, pos); | |
107 | if (result < 0) | |
108 | node = &pos->rb.rb_left; | |
109 | else if (result > 0) | |
110 | node = &pos->rb.rb_right; | |
111 | else | |
112 | return -EEXIST; | |
113 | } | |
114 | /* add new node and rebalance the tree */ | |
115 | rb_link_node(&kn->rb, parent, node); | |
116 | rb_insert_color(&kn->rb, &kn->parent->dir.children); | |
117 | return 0; | |
118 | } | |
119 | ||
120 | /** | |
121 | * kernfs_unlink_sibling - unlink kernfs_node from sibling rbtree | |
122 | * @kn: kernfs_node of interest | |
123 | * | |
124 | * Try to unlink @kn from its sibling rbtree which starts from | |
125 | * kn->parent->dir.children. Returns %true if @kn was actually | |
126 | * removed, %false if @kn wasn't on the rbtree. | |
127 | * | |
128 | * Locking: | |
129 | * mutex_lock(kernfs_mutex) | |
130 | */ | |
131 | static bool kernfs_unlink_sibling(struct kernfs_node *kn) | |
132 | { | |
133 | if (RB_EMPTY_NODE(&kn->rb)) | |
134 | return false; | |
135 | ||
136 | if (kernfs_type(kn) == KERNFS_DIR) | |
137 | kn->parent->dir.subdirs--; | |
138 | ||
139 | rb_erase(&kn->rb, &kn->parent->dir.children); | |
140 | RB_CLEAR_NODE(&kn->rb); | |
141 | return true; | |
142 | } | |
143 | ||
144 | /** | |
145 | * kernfs_get_active - get an active reference to kernfs_node | |
146 | * @kn: kernfs_node to get an active reference to | |
147 | * | |
148 | * Get an active reference of @kn. This function is noop if @kn | |
149 | * is NULL. | |
150 | * | |
151 | * RETURNS: | |
152 | * Pointer to @kn on success, NULL on failure. | |
153 | */ | |
154 | struct kernfs_node *kernfs_get_active(struct kernfs_node *kn) | |
155 | { | |
156 | if (unlikely(!kn)) | |
157 | return NULL; | |
158 | ||
159 | if (!atomic_inc_unless_negative(&kn->active)) | |
160 | return NULL; | |
161 | ||
162 | if (kernfs_lockdep(kn)) | |
163 | rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_); | |
164 | return kn; | |
165 | } | |
166 | ||
167 | /** | |
168 | * kernfs_put_active - put an active reference to kernfs_node | |
169 | * @kn: kernfs_node to put an active reference to | |
170 | * | |
171 | * Put an active reference to @kn. This function is noop if @kn | |
172 | * is NULL. | |
173 | */ | |
174 | void kernfs_put_active(struct kernfs_node *kn) | |
175 | { | |
176 | struct kernfs_root *root = kernfs_root(kn); | |
177 | int v; | |
178 | ||
179 | if (unlikely(!kn)) | |
180 | return; | |
181 | ||
182 | if (kernfs_lockdep(kn)) | |
183 | rwsem_release(&kn->dep_map, 1, _RET_IP_); | |
184 | v = atomic_dec_return(&kn->active); | |
185 | if (likely(v != KN_DEACTIVATED_BIAS)) | |
186 | return; | |
187 | ||
188 | wake_up_all(&root->deactivate_waitq); | |
189 | } | |
190 | ||
191 | /** | |
192 | * kernfs_drain - drain kernfs_node | |
193 | * @kn: kernfs_node to drain | |
194 | * | |
195 | * Drain existing usages and nuke all existing mmaps of @kn. Mutiple | |
196 | * removers may invoke this function concurrently on @kn and all will | |
197 | * return after draining is complete. | |
198 | */ | |
199 | static void kernfs_drain(struct kernfs_node *kn) | |
200 | __releases(&kernfs_mutex) __acquires(&kernfs_mutex) | |
201 | { | |
202 | struct kernfs_root *root = kernfs_root(kn); | |
203 | ||
204 | lockdep_assert_held(&kernfs_mutex); | |
205 | WARN_ON_ONCE(kernfs_active(kn)); | |
206 | ||
207 | mutex_unlock(&kernfs_mutex); | |
208 | ||
209 | if (kernfs_lockdep(kn)) { | |
210 | rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_); | |
211 | if (atomic_read(&kn->active) != KN_DEACTIVATED_BIAS) | |
212 | lock_contended(&kn->dep_map, _RET_IP_); | |
213 | } | |
214 | ||
215 | /* but everyone should wait for draining */ | |
216 | wait_event(root->deactivate_waitq, | |
217 | atomic_read(&kn->active) == KN_DEACTIVATED_BIAS); | |
218 | ||
219 | if (kernfs_lockdep(kn)) { | |
220 | lock_acquired(&kn->dep_map, _RET_IP_); | |
221 | rwsem_release(&kn->dep_map, 1, _RET_IP_); | |
222 | } | |
223 | ||
224 | kernfs_unmap_bin_file(kn); | |
225 | ||
226 | mutex_lock(&kernfs_mutex); | |
227 | } | |
228 | ||
229 | /** | |
230 | * kernfs_get - get a reference count on a kernfs_node | |
231 | * @kn: the target kernfs_node | |
232 | */ | |
233 | void kernfs_get(struct kernfs_node *kn) | |
234 | { | |
235 | if (kn) { | |
236 | WARN_ON(!atomic_read(&kn->count)); | |
237 | atomic_inc(&kn->count); | |
238 | } | |
239 | } | |
240 | EXPORT_SYMBOL_GPL(kernfs_get); | |
241 | ||
242 | /** | |
243 | * kernfs_put - put a reference count on a kernfs_node | |
244 | * @kn: the target kernfs_node | |
245 | * | |
246 | * Put a reference count of @kn and destroy it if it reached zero. | |
247 | */ | |
248 | void kernfs_put(struct kernfs_node *kn) | |
249 | { | |
250 | struct kernfs_node *parent; | |
251 | struct kernfs_root *root; | |
252 | ||
253 | if (!kn || !atomic_dec_and_test(&kn->count)) | |
254 | return; | |
255 | root = kernfs_root(kn); | |
256 | repeat: | |
257 | /* | |
258 | * Moving/renaming is always done while holding reference. | |
259 | * kn->parent won't change beneath us. | |
260 | */ | |
261 | parent = kn->parent; | |
262 | ||
263 | WARN_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS, | |
264 | "kernfs_put: %s/%s: released with incorrect active_ref %d\n", | |
265 | parent ? parent->name : "", kn->name, atomic_read(&kn->active)); | |
266 | ||
267 | if (kernfs_type(kn) == KERNFS_LINK) | |
268 | kernfs_put(kn->symlink.target_kn); | |
269 | if (!(kn->flags & KERNFS_STATIC_NAME)) | |
270 | kfree(kn->name); | |
271 | if (kn->iattr) { | |
272 | if (kn->iattr->ia_secdata) | |
273 | security_release_secctx(kn->iattr->ia_secdata, | |
274 | kn->iattr->ia_secdata_len); | |
275 | simple_xattrs_free(&kn->iattr->xattrs); | |
276 | } | |
277 | kfree(kn->iattr); | |
278 | ida_simple_remove(&root->ino_ida, kn->ino); | |
279 | kmem_cache_free(kernfs_node_cache, kn); | |
280 | ||
281 | kn = parent; | |
282 | if (kn) { | |
283 | if (atomic_dec_and_test(&kn->count)) | |
284 | goto repeat; | |
285 | } else { | |
286 | /* just released the root kn, free @root too */ | |
287 | ida_destroy(&root->ino_ida); | |
288 | kfree(root); | |
289 | } | |
290 | } | |
291 | EXPORT_SYMBOL_GPL(kernfs_put); | |
292 | ||
293 | static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags) | |
294 | { | |
295 | struct kernfs_node *kn; | |
296 | ||
297 | if (flags & LOOKUP_RCU) | |
298 | return -ECHILD; | |
299 | ||
300 | /* Always perform fresh lookup for negatives */ | |
301 | if (!dentry->d_inode) | |
302 | goto out_bad_unlocked; | |
303 | ||
304 | kn = dentry->d_fsdata; | |
305 | mutex_lock(&kernfs_mutex); | |
306 | ||
307 | /* The kernfs node has been deactivated */ | |
308 | if (!kernfs_active(kn)) | |
309 | goto out_bad; | |
310 | ||
311 | /* The kernfs node has been moved? */ | |
312 | if (dentry->d_parent->d_fsdata != kn->parent) | |
313 | goto out_bad; | |
314 | ||
315 | /* The kernfs node has been renamed */ | |
316 | if (strcmp(dentry->d_name.name, kn->name) != 0) | |
317 | goto out_bad; | |
318 | ||
319 | /* The kernfs node has been moved to a different namespace */ | |
320 | if (kn->parent && kernfs_ns_enabled(kn->parent) && | |
321 | kernfs_info(dentry->d_sb)->ns != kn->ns) | |
322 | goto out_bad; | |
323 | ||
324 | mutex_unlock(&kernfs_mutex); | |
325 | out_valid: | |
326 | return 1; | |
327 | out_bad: | |
328 | mutex_unlock(&kernfs_mutex); | |
329 | out_bad_unlocked: | |
330 | /* | |
331 | * @dentry doesn't match the underlying kernfs node, drop the | |
332 | * dentry and force lookup. If we have submounts we must allow the | |
333 | * vfs caches to lie about the state of the filesystem to prevent | |
334 | * leaks and other nasty things, so use check_submounts_and_drop() | |
335 | * instead of d_drop(). | |
336 | */ | |
337 | if (check_submounts_and_drop(dentry) != 0) | |
338 | goto out_valid; | |
339 | ||
340 | return 0; | |
341 | } | |
342 | ||
343 | static void kernfs_dop_release(struct dentry *dentry) | |
344 | { | |
345 | kernfs_put(dentry->d_fsdata); | |
346 | } | |
347 | ||
348 | const struct dentry_operations kernfs_dops = { | |
349 | .d_revalidate = kernfs_dop_revalidate, | |
350 | .d_release = kernfs_dop_release, | |
351 | }; | |
352 | ||
353 | static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root, | |
354 | const char *name, umode_t mode, | |
355 | unsigned flags) | |
356 | { | |
357 | char *dup_name = NULL; | |
358 | struct kernfs_node *kn; | |
359 | int ret; | |
360 | ||
361 | if (!(flags & KERNFS_STATIC_NAME)) { | |
362 | name = dup_name = kstrdup(name, GFP_KERNEL); | |
363 | if (!name) | |
364 | return NULL; | |
365 | } | |
366 | ||
367 | kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL); | |
368 | if (!kn) | |
369 | goto err_out1; | |
370 | ||
371 | ret = ida_simple_get(&root->ino_ida, 1, 0, GFP_KERNEL); | |
372 | if (ret < 0) | |
373 | goto err_out2; | |
374 | kn->ino = ret; | |
375 | ||
376 | atomic_set(&kn->count, 1); | |
377 | atomic_set(&kn->active, KN_DEACTIVATED_BIAS); | |
378 | RB_CLEAR_NODE(&kn->rb); | |
379 | ||
380 | kn->name = name; | |
381 | kn->mode = mode; | |
382 | kn->flags = flags; | |
383 | ||
384 | return kn; | |
385 | ||
386 | err_out2: | |
387 | kmem_cache_free(kernfs_node_cache, kn); | |
388 | err_out1: | |
389 | kfree(dup_name); | |
390 | return NULL; | |
391 | } | |
392 | ||
393 | struct kernfs_node *kernfs_new_node(struct kernfs_node *parent, | |
394 | const char *name, umode_t mode, | |
395 | unsigned flags) | |
396 | { | |
397 | struct kernfs_node *kn; | |
398 | ||
399 | kn = __kernfs_new_node(kernfs_root(parent), name, mode, flags); | |
400 | if (kn) { | |
401 | kernfs_get(parent); | |
402 | kn->parent = parent; | |
403 | } | |
404 | return kn; | |
405 | } | |
406 | ||
407 | /** | |
408 | * kernfs_add_one - add kernfs_node to parent without warning | |
409 | * @kn: kernfs_node to be added | |
410 | * | |
411 | * The caller must already have initialized @kn->parent. This | |
412 | * function increments nlink of the parent's inode if @kn is a | |
413 | * directory and link into the children list of the parent. | |
414 | * | |
415 | * RETURNS: | |
416 | * 0 on success, -EEXIST if entry with the given name already | |
417 | * exists. | |
418 | */ | |
419 | int kernfs_add_one(struct kernfs_node *kn) | |
420 | { | |
421 | struct kernfs_node *parent = kn->parent; | |
422 | struct kernfs_iattrs *ps_iattr; | |
423 | bool has_ns; | |
424 | int ret; | |
425 | ||
426 | mutex_lock(&kernfs_mutex); | |
427 | ||
428 | ret = -EINVAL; | |
429 | has_ns = kernfs_ns_enabled(parent); | |
430 | if (WARN(has_ns != (bool)kn->ns, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n", | |
431 | has_ns ? "required" : "invalid", parent->name, kn->name)) | |
432 | goto out_unlock; | |
433 | ||
434 | if (kernfs_type(parent) != KERNFS_DIR) | |
435 | goto out_unlock; | |
436 | ||
437 | ret = -ENOENT; | |
438 | if ((parent->flags & KERNFS_ACTIVATED) && !kernfs_active(parent)) | |
439 | goto out_unlock; | |
440 | ||
441 | kn->hash = kernfs_name_hash(kn->name, kn->ns); | |
442 | ||
443 | ret = kernfs_link_sibling(kn); | |
444 | if (ret) | |
445 | goto out_unlock; | |
446 | ||
447 | /* Update timestamps on the parent */ | |
448 | ps_iattr = parent->iattr; | |
449 | if (ps_iattr) { | |
450 | struct iattr *ps_iattrs = &ps_iattr->ia_iattr; | |
451 | ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME; | |
452 | } | |
453 | ||
454 | mutex_unlock(&kernfs_mutex); | |
455 | ||
456 | /* | |
457 | * Activate the new node unless CREATE_DEACTIVATED is requested. | |
458 | * If not activated here, the kernfs user is responsible for | |
459 | * activating the node with kernfs_activate(). A node which hasn't | |
460 | * been activated is not visible to userland and its removal won't | |
461 | * trigger deactivation. | |
462 | */ | |
463 | if (!(kernfs_root(kn)->flags & KERNFS_ROOT_CREATE_DEACTIVATED)) | |
464 | kernfs_activate(kn); | |
465 | return 0; | |
466 | ||
467 | out_unlock: | |
468 | mutex_unlock(&kernfs_mutex); | |
469 | return ret; | |
470 | } | |
471 | ||
472 | /** | |
473 | * kernfs_find_ns - find kernfs_node with the given name | |
474 | * @parent: kernfs_node to search under | |
475 | * @name: name to look for | |
476 | * @ns: the namespace tag to use | |
477 | * | |
478 | * Look for kernfs_node with name @name under @parent. Returns pointer to | |
479 | * the found kernfs_node on success, %NULL on failure. | |
480 | */ | |
481 | static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent, | |
482 | const unsigned char *name, | |
483 | const void *ns) | |
484 | { | |
485 | struct rb_node *node = parent->dir.children.rb_node; | |
486 | bool has_ns = kernfs_ns_enabled(parent); | |
487 | unsigned int hash; | |
488 | ||
489 | lockdep_assert_held(&kernfs_mutex); | |
490 | ||
491 | if (has_ns != (bool)ns) { | |
492 | WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n", | |
493 | has_ns ? "required" : "invalid", parent->name, name); | |
494 | return NULL; | |
495 | } | |
496 | ||
497 | hash = kernfs_name_hash(name, ns); | |
498 | while (node) { | |
499 | struct kernfs_node *kn; | |
500 | int result; | |
501 | ||
502 | kn = rb_to_kn(node); | |
503 | result = kernfs_name_compare(hash, name, ns, kn); | |
504 | if (result < 0) | |
505 | node = node->rb_left; | |
506 | else if (result > 0) | |
507 | node = node->rb_right; | |
508 | else | |
509 | return kn; | |
510 | } | |
511 | return NULL; | |
512 | } | |
513 | ||
514 | /** | |
515 | * kernfs_find_and_get_ns - find and get kernfs_node with the given name | |
516 | * @parent: kernfs_node to search under | |
517 | * @name: name to look for | |
518 | * @ns: the namespace tag to use | |
519 | * | |
520 | * Look for kernfs_node with name @name under @parent and get a reference | |
521 | * if found. This function may sleep and returns pointer to the found | |
522 | * kernfs_node on success, %NULL on failure. | |
523 | */ | |
524 | struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent, | |
525 | const char *name, const void *ns) | |
526 | { | |
527 | struct kernfs_node *kn; | |
528 | ||
529 | mutex_lock(&kernfs_mutex); | |
530 | kn = kernfs_find_ns(parent, name, ns); | |
531 | kernfs_get(kn); | |
532 | mutex_unlock(&kernfs_mutex); | |
533 | ||
534 | return kn; | |
535 | } | |
536 | EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns); | |
537 | ||
538 | /** | |
539 | * kernfs_create_root - create a new kernfs hierarchy | |
540 | * @scops: optional syscall operations for the hierarchy | |
541 | * @priv: opaque data associated with the new directory | |
542 | * | |
543 | * Returns the root of the new hierarchy on success, ERR_PTR() value on | |
544 | * failure. | |
545 | */ | |
546 | struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops, | |
547 | void *priv) | |
548 | { | |
549 | struct kernfs_root *root; | |
550 | struct kernfs_node *kn; | |
551 | ||
552 | root = kzalloc(sizeof(*root), GFP_KERNEL); | |
553 | if (!root) | |
554 | return ERR_PTR(-ENOMEM); | |
555 | ||
556 | ida_init(&root->ino_ida); | |
557 | ||
558 | kn = __kernfs_new_node(root, "", S_IFDIR | S_IRUGO | S_IXUGO, | |
559 | KERNFS_DIR); | |
560 | if (!kn) { | |
561 | ida_destroy(&root->ino_ida); | |
562 | kfree(root); | |
563 | return ERR_PTR(-ENOMEM); | |
564 | } | |
565 | ||
566 | kernfs_activate(kn); | |
567 | kn->priv = priv; | |
568 | kn->dir.root = root; | |
569 | ||
570 | root->syscall_ops = scops; | |
571 | root->kn = kn; | |
572 | init_waitqueue_head(&root->deactivate_waitq); | |
573 | ||
574 | return root; | |
575 | } | |
576 | ||
577 | /** | |
578 | * kernfs_destroy_root - destroy a kernfs hierarchy | |
579 | * @root: root of the hierarchy to destroy | |
580 | * | |
581 | * Destroy the hierarchy anchored at @root by removing all existing | |
582 | * directories and destroying @root. | |
583 | */ | |
584 | void kernfs_destroy_root(struct kernfs_root *root) | |
585 | { | |
586 | kernfs_remove(root->kn); /* will also free @root */ | |
587 | } | |
588 | ||
589 | /** | |
590 | * kernfs_create_dir_ns - create a directory | |
591 | * @parent: parent in which to create a new directory | |
592 | * @name: name of the new directory | |
593 | * @mode: mode of the new directory | |
594 | * @priv: opaque data associated with the new directory | |
595 | * @ns: optional namespace tag of the directory | |
596 | * | |
597 | * Returns the created node on success, ERR_PTR() value on failure. | |
598 | */ | |
599 | struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent, | |
600 | const char *name, umode_t mode, | |
601 | void *priv, const void *ns) | |
602 | { | |
603 | struct kernfs_node *kn; | |
604 | int rc; | |
605 | ||
606 | /* allocate */ | |
607 | kn = kernfs_new_node(parent, name, mode | S_IFDIR, KERNFS_DIR); | |
608 | if (!kn) | |
609 | return ERR_PTR(-ENOMEM); | |
610 | ||
611 | kn->dir.root = parent->dir.root; | |
612 | kn->ns = ns; | |
613 | kn->priv = priv; | |
614 | ||
615 | /* link in */ | |
616 | rc = kernfs_add_one(kn); | |
617 | if (!rc) | |
618 | return kn; | |
619 | ||
620 | kernfs_put(kn); | |
621 | return ERR_PTR(rc); | |
622 | } | |
623 | ||
624 | static struct dentry *kernfs_iop_lookup(struct inode *dir, | |
625 | struct dentry *dentry, | |
626 | unsigned int flags) | |
627 | { | |
628 | struct dentry *ret; | |
629 | struct kernfs_node *parent = dentry->d_parent->d_fsdata; | |
630 | struct kernfs_node *kn; | |
631 | struct inode *inode; | |
632 | const void *ns = NULL; | |
633 | ||
634 | mutex_lock(&kernfs_mutex); | |
635 | ||
636 | if (kernfs_ns_enabled(parent)) | |
637 | ns = kernfs_info(dir->i_sb)->ns; | |
638 | ||
639 | kn = kernfs_find_ns(parent, dentry->d_name.name, ns); | |
640 | ||
641 | /* no such entry */ | |
642 | if (!kn || !kernfs_active(kn)) { | |
643 | ret = NULL; | |
644 | goto out_unlock; | |
645 | } | |
646 | kernfs_get(kn); | |
647 | dentry->d_fsdata = kn; | |
648 | ||
649 | /* attach dentry and inode */ | |
650 | inode = kernfs_get_inode(dir->i_sb, kn); | |
651 | if (!inode) { | |
652 | ret = ERR_PTR(-ENOMEM); | |
653 | goto out_unlock; | |
654 | } | |
655 | ||
656 | /* instantiate and hash dentry */ | |
657 | ret = d_materialise_unique(dentry, inode); | |
658 | out_unlock: | |
659 | mutex_unlock(&kernfs_mutex); | |
660 | return ret; | |
661 | } | |
662 | ||
663 | static int kernfs_iop_mkdir(struct inode *dir, struct dentry *dentry, | |
664 | umode_t mode) | |
665 | { | |
666 | struct kernfs_node *parent = dir->i_private; | |
667 | struct kernfs_syscall_ops *scops = kernfs_root(parent)->syscall_ops; | |
668 | int ret; | |
669 | ||
670 | if (!scops || !scops->mkdir) | |
671 | return -EPERM; | |
672 | ||
673 | if (!kernfs_get_active(parent)) | |
674 | return -ENODEV; | |
675 | ||
676 | ret = scops->mkdir(parent, dentry->d_name.name, mode); | |
677 | ||
678 | kernfs_put_active(parent); | |
679 | return ret; | |
680 | } | |
681 | ||
682 | static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry) | |
683 | { | |
684 | struct kernfs_node *kn = dentry->d_fsdata; | |
685 | struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops; | |
686 | int ret; | |
687 | ||
688 | if (!scops || !scops->rmdir) | |
689 | return -EPERM; | |
690 | ||
691 | if (!kernfs_get_active(kn)) | |
692 | return -ENODEV; | |
693 | ||
694 | ret = scops->rmdir(kn); | |
695 | ||
696 | kernfs_put_active(kn); | |
697 | return ret; | |
698 | } | |
699 | ||
700 | static int kernfs_iop_rename(struct inode *old_dir, struct dentry *old_dentry, | |
701 | struct inode *new_dir, struct dentry *new_dentry) | |
702 | { | |
703 | struct kernfs_node *kn = old_dentry->d_fsdata; | |
704 | struct kernfs_node *new_parent = new_dir->i_private; | |
705 | struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops; | |
706 | int ret; | |
707 | ||
708 | if (!scops || !scops->rename) | |
709 | return -EPERM; | |
710 | ||
711 | if (!kernfs_get_active(kn)) | |
712 | return -ENODEV; | |
713 | ||
714 | if (!kernfs_get_active(new_parent)) { | |
715 | kernfs_put_active(kn); | |
716 | return -ENODEV; | |
717 | } | |
718 | ||
719 | ret = scops->rename(kn, new_parent, new_dentry->d_name.name); | |
720 | ||
721 | kernfs_put_active(new_parent); | |
722 | kernfs_put_active(kn); | |
723 | return ret; | |
724 | } | |
725 | ||
726 | const struct inode_operations kernfs_dir_iops = { | |
727 | .lookup = kernfs_iop_lookup, | |
728 | .permission = kernfs_iop_permission, | |
729 | .setattr = kernfs_iop_setattr, | |
730 | .getattr = kernfs_iop_getattr, | |
731 | .setxattr = kernfs_iop_setxattr, | |
732 | .removexattr = kernfs_iop_removexattr, | |
733 | .getxattr = kernfs_iop_getxattr, | |
734 | .listxattr = kernfs_iop_listxattr, | |
735 | ||
736 | .mkdir = kernfs_iop_mkdir, | |
737 | .rmdir = kernfs_iop_rmdir, | |
738 | .rename = kernfs_iop_rename, | |
739 | }; | |
740 | ||
741 | static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos) | |
742 | { | |
743 | struct kernfs_node *last; | |
744 | ||
745 | while (true) { | |
746 | struct rb_node *rbn; | |
747 | ||
748 | last = pos; | |
749 | ||
750 | if (kernfs_type(pos) != KERNFS_DIR) | |
751 | break; | |
752 | ||
753 | rbn = rb_first(&pos->dir.children); | |
754 | if (!rbn) | |
755 | break; | |
756 | ||
757 | pos = rb_to_kn(rbn); | |
758 | } | |
759 | ||
760 | return last; | |
761 | } | |
762 | ||
763 | /** | |
764 | * kernfs_next_descendant_post - find the next descendant for post-order walk | |
765 | * @pos: the current position (%NULL to initiate traversal) | |
766 | * @root: kernfs_node whose descendants to walk | |
767 | * | |
768 | * Find the next descendant to visit for post-order traversal of @root's | |
769 | * descendants. @root is included in the iteration and the last node to be | |
770 | * visited. | |
771 | */ | |
772 | static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos, | |
773 | struct kernfs_node *root) | |
774 | { | |
775 | struct rb_node *rbn; | |
776 | ||
777 | lockdep_assert_held(&kernfs_mutex); | |
778 | ||
779 | /* if first iteration, visit leftmost descendant which may be root */ | |
780 | if (!pos) | |
781 | return kernfs_leftmost_descendant(root); | |
782 | ||
783 | /* if we visited @root, we're done */ | |
784 | if (pos == root) | |
785 | return NULL; | |
786 | ||
787 | /* if there's an unvisited sibling, visit its leftmost descendant */ | |
788 | rbn = rb_next(&pos->rb); | |
789 | if (rbn) | |
790 | return kernfs_leftmost_descendant(rb_to_kn(rbn)); | |
791 | ||
792 | /* no sibling left, visit parent */ | |
793 | return pos->parent; | |
794 | } | |
795 | ||
796 | /** | |
797 | * kernfs_activate - activate a node which started deactivated | |
798 | * @kn: kernfs_node whose subtree is to be activated | |
799 | * | |
800 | * If the root has KERNFS_ROOT_CREATE_DEACTIVATED set, a newly created node | |
801 | * needs to be explicitly activated. A node which hasn't been activated | |
802 | * isn't visible to userland and deactivation is skipped during its | |
803 | * removal. This is useful to construct atomic init sequences where | |
804 | * creation of multiple nodes should either succeed or fail atomically. | |
805 | * | |
806 | * The caller is responsible for ensuring that this function is not called | |
807 | * after kernfs_remove*() is invoked on @kn. | |
808 | */ | |
809 | void kernfs_activate(struct kernfs_node *kn) | |
810 | { | |
811 | struct kernfs_node *pos; | |
812 | ||
813 | mutex_lock(&kernfs_mutex); | |
814 | ||
815 | pos = NULL; | |
816 | while ((pos = kernfs_next_descendant_post(pos, kn))) { | |
817 | if (!pos || (pos->flags & KERNFS_ACTIVATED)) | |
818 | continue; | |
819 | ||
820 | WARN_ON_ONCE(pos->parent && RB_EMPTY_NODE(&pos->rb)); | |
821 | WARN_ON_ONCE(atomic_read(&pos->active) != KN_DEACTIVATED_BIAS); | |
822 | ||
823 | atomic_sub(KN_DEACTIVATED_BIAS, &pos->active); | |
824 | pos->flags |= KERNFS_ACTIVATED; | |
825 | } | |
826 | ||
827 | mutex_unlock(&kernfs_mutex); | |
828 | } | |
829 | ||
830 | static void __kernfs_remove(struct kernfs_node *kn) | |
831 | { | |
832 | struct kernfs_node *pos; | |
833 | ||
834 | lockdep_assert_held(&kernfs_mutex); | |
835 | ||
836 | /* | |
837 | * Short-circuit if @kn has already finished removal. This is for | |
838 | * kernfs_remove_self() which plays with active ref after removal. | |
839 | */ | |
840 | if (!kn || RB_EMPTY_NODE(&kn->rb)) | |
841 | return; | |
842 | ||
843 | pr_debug("kernfs %s: removing\n", kn->name); | |
844 | ||
845 | /* prevent any new usage under @kn by deactivating all nodes */ | |
846 | pos = NULL; | |
847 | while ((pos = kernfs_next_descendant_post(pos, kn))) | |
848 | if (kernfs_active(pos)) | |
849 | atomic_add(KN_DEACTIVATED_BIAS, &pos->active); | |
850 | ||
851 | /* deactivate and unlink the subtree node-by-node */ | |
852 | do { | |
853 | pos = kernfs_leftmost_descendant(kn); | |
854 | ||
855 | /* | |
856 | * kernfs_drain() drops kernfs_mutex temporarily and @pos's | |
857 | * base ref could have been put by someone else by the time | |
858 | * the function returns. Make sure it doesn't go away | |
859 | * underneath us. | |
860 | */ | |
861 | kernfs_get(pos); | |
862 | ||
863 | /* | |
864 | * Drain iff @kn was activated. This avoids draining and | |
865 | * its lockdep annotations for nodes which have never been | |
866 | * activated and allows embedding kernfs_remove() in create | |
867 | * error paths without worrying about draining. | |
868 | */ | |
869 | if (kn->flags & KERNFS_ACTIVATED) | |
870 | kernfs_drain(pos); | |
871 | else | |
872 | WARN_ON_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS); | |
873 | ||
874 | /* | |
875 | * kernfs_unlink_sibling() succeeds once per node. Use it | |
876 | * to decide who's responsible for cleanups. | |
877 | */ | |
878 | if (!pos->parent || kernfs_unlink_sibling(pos)) { | |
879 | struct kernfs_iattrs *ps_iattr = | |
880 | pos->parent ? pos->parent->iattr : NULL; | |
881 | ||
882 | /* update timestamps on the parent */ | |
883 | if (ps_iattr) { | |
884 | ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME; | |
885 | ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME; | |
886 | } | |
887 | ||
888 | kernfs_put(pos); | |
889 | } | |
890 | ||
891 | kernfs_put(pos); | |
892 | } while (pos != kn); | |
893 | } | |
894 | ||
895 | /** | |
896 | * kernfs_remove - remove a kernfs_node recursively | |
897 | * @kn: the kernfs_node to remove | |
898 | * | |
899 | * Remove @kn along with all its subdirectories and files. | |
900 | */ | |
901 | void kernfs_remove(struct kernfs_node *kn) | |
902 | { | |
903 | mutex_lock(&kernfs_mutex); | |
904 | __kernfs_remove(kn); | |
905 | mutex_unlock(&kernfs_mutex); | |
906 | } | |
907 | ||
908 | /** | |
909 | * kernfs_break_active_protection - break out of active protection | |
910 | * @kn: the self kernfs_node | |
911 | * | |
912 | * The caller must be running off of a kernfs operation which is invoked | |
913 | * with an active reference - e.g. one of kernfs_ops. Each invocation of | |
914 | * this function must also be matched with an invocation of | |
915 | * kernfs_unbreak_active_protection(). | |
916 | * | |
917 | * This function releases the active reference of @kn the caller is | |
918 | * holding. Once this function is called, @kn may be removed at any point | |
919 | * and the caller is solely responsible for ensuring that the objects it | |
920 | * dereferences are accessible. | |
921 | */ | |
922 | void kernfs_break_active_protection(struct kernfs_node *kn) | |
923 | { | |
924 | /* | |
925 | * Take out ourself out of the active ref dependency chain. If | |
926 | * we're called without an active ref, lockdep will complain. | |
927 | */ | |
928 | kernfs_put_active(kn); | |
929 | } | |
930 | ||
931 | /** | |
932 | * kernfs_unbreak_active_protection - undo kernfs_break_active_protection() | |
933 | * @kn: the self kernfs_node | |
934 | * | |
935 | * If kernfs_break_active_protection() was called, this function must be | |
936 | * invoked before finishing the kernfs operation. Note that while this | |
937 | * function restores the active reference, it doesn't and can't actually | |
938 | * restore the active protection - @kn may already or be in the process of | |
939 | * being removed. Once kernfs_break_active_protection() is invoked, that | |
940 | * protection is irreversibly gone for the kernfs operation instance. | |
941 | * | |
942 | * While this function may be called at any point after | |
943 | * kernfs_break_active_protection() is invoked, its most useful location | |
944 | * would be right before the enclosing kernfs operation returns. | |
945 | */ | |
946 | void kernfs_unbreak_active_protection(struct kernfs_node *kn) | |
947 | { | |
948 | /* | |
949 | * @kn->active could be in any state; however, the increment we do | |
950 | * here will be undone as soon as the enclosing kernfs operation | |
951 | * finishes and this temporary bump can't break anything. If @kn | |
952 | * is alive, nothing changes. If @kn is being deactivated, the | |
953 | * soon-to-follow put will either finish deactivation or restore | |
954 | * deactivated state. If @kn is already removed, the temporary | |
955 | * bump is guaranteed to be gone before @kn is released. | |
956 | */ | |
957 | atomic_inc(&kn->active); | |
958 | if (kernfs_lockdep(kn)) | |
959 | rwsem_acquire(&kn->dep_map, 0, 1, _RET_IP_); | |
960 | } | |
961 | ||
962 | /** | |
963 | * kernfs_remove_self - remove a kernfs_node from its own method | |
964 | * @kn: the self kernfs_node to remove | |
965 | * | |
966 | * The caller must be running off of a kernfs operation which is invoked | |
967 | * with an active reference - e.g. one of kernfs_ops. This can be used to | |
968 | * implement a file operation which deletes itself. | |
969 | * | |
970 | * For example, the "delete" file for a sysfs device directory can be | |
971 | * implemented by invoking kernfs_remove_self() on the "delete" file | |
972 | * itself. This function breaks the circular dependency of trying to | |
973 | * deactivate self while holding an active ref itself. It isn't necessary | |
974 | * to modify the usual removal path to use kernfs_remove_self(). The | |
975 | * "delete" implementation can simply invoke kernfs_remove_self() on self | |
976 | * before proceeding with the usual removal path. kernfs will ignore later | |
977 | * kernfs_remove() on self. | |
978 | * | |
979 | * kernfs_remove_self() can be called multiple times concurrently on the | |
980 | * same kernfs_node. Only the first one actually performs removal and | |
981 | * returns %true. All others will wait until the kernfs operation which | |
982 | * won self-removal finishes and return %false. Note that the losers wait | |
983 | * for the completion of not only the winning kernfs_remove_self() but also | |
984 | * the whole kernfs_ops which won the arbitration. This can be used to | |
985 | * guarantee, for example, all concurrent writes to a "delete" file to | |
986 | * finish only after the whole operation is complete. | |
987 | */ | |
988 | bool kernfs_remove_self(struct kernfs_node *kn) | |
989 | { | |
990 | bool ret; | |
991 | ||
992 | mutex_lock(&kernfs_mutex); | |
993 | kernfs_break_active_protection(kn); | |
994 | ||
995 | /* | |
996 | * SUICIDAL is used to arbitrate among competing invocations. Only | |
997 | * the first one will actually perform removal. When the removal | |
998 | * is complete, SUICIDED is set and the active ref is restored | |
999 | * while holding kernfs_mutex. The ones which lost arbitration | |
1000 | * waits for SUICDED && drained which can happen only after the | |
1001 | * enclosing kernfs operation which executed the winning instance | |
1002 | * of kernfs_remove_self() finished. | |
1003 | */ | |
1004 | if (!(kn->flags & KERNFS_SUICIDAL)) { | |
1005 | kn->flags |= KERNFS_SUICIDAL; | |
1006 | __kernfs_remove(kn); | |
1007 | kn->flags |= KERNFS_SUICIDED; | |
1008 | ret = true; | |
1009 | } else { | |
1010 | wait_queue_head_t *waitq = &kernfs_root(kn)->deactivate_waitq; | |
1011 | DEFINE_WAIT(wait); | |
1012 | ||
1013 | while (true) { | |
1014 | prepare_to_wait(waitq, &wait, TASK_UNINTERRUPTIBLE); | |
1015 | ||
1016 | if ((kn->flags & KERNFS_SUICIDED) && | |
1017 | atomic_read(&kn->active) == KN_DEACTIVATED_BIAS) | |
1018 | break; | |
1019 | ||
1020 | mutex_unlock(&kernfs_mutex); | |
1021 | schedule(); | |
1022 | mutex_lock(&kernfs_mutex); | |
1023 | } | |
1024 | finish_wait(waitq, &wait); | |
1025 | WARN_ON_ONCE(!RB_EMPTY_NODE(&kn->rb)); | |
1026 | ret = false; | |
1027 | } | |
1028 | ||
1029 | /* | |
1030 | * This must be done while holding kernfs_mutex; otherwise, waiting | |
1031 | * for SUICIDED && deactivated could finish prematurely. | |
1032 | */ | |
1033 | kernfs_unbreak_active_protection(kn); | |
1034 | ||
1035 | mutex_unlock(&kernfs_mutex); | |
1036 | return ret; | |
1037 | } | |
1038 | ||
1039 | /** | |
1040 | * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it | |
1041 | * @parent: parent of the target | |
1042 | * @name: name of the kernfs_node to remove | |
1043 | * @ns: namespace tag of the kernfs_node to remove | |
1044 | * | |
1045 | * Look for the kernfs_node with @name and @ns under @parent and remove it. | |
1046 | * Returns 0 on success, -ENOENT if such entry doesn't exist. | |
1047 | */ | |
1048 | int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name, | |
1049 | const void *ns) | |
1050 | { | |
1051 | struct kernfs_node *kn; | |
1052 | ||
1053 | if (!parent) { | |
1054 | WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n", | |
1055 | name); | |
1056 | return -ENOENT; | |
1057 | } | |
1058 | ||
1059 | mutex_lock(&kernfs_mutex); | |
1060 | ||
1061 | kn = kernfs_find_ns(parent, name, ns); | |
1062 | if (kn) | |
1063 | __kernfs_remove(kn); | |
1064 | ||
1065 | mutex_unlock(&kernfs_mutex); | |
1066 | ||
1067 | if (kn) | |
1068 | return 0; | |
1069 | else | |
1070 | return -ENOENT; | |
1071 | } | |
1072 | ||
1073 | /** | |
1074 | * kernfs_rename_ns - move and rename a kernfs_node | |
1075 | * @kn: target node | |
1076 | * @new_parent: new parent to put @sd under | |
1077 | * @new_name: new name | |
1078 | * @new_ns: new namespace tag | |
1079 | */ | |
1080 | int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent, | |
1081 | const char *new_name, const void *new_ns) | |
1082 | { | |
1083 | int error; | |
1084 | ||
1085 | mutex_lock(&kernfs_mutex); | |
1086 | ||
1087 | error = -ENOENT; | |
1088 | if (!kernfs_active(kn) || !kernfs_active(new_parent)) | |
1089 | goto out; | |
1090 | ||
1091 | error = 0; | |
1092 | if ((kn->parent == new_parent) && (kn->ns == new_ns) && | |
1093 | (strcmp(kn->name, new_name) == 0)) | |
1094 | goto out; /* nothing to rename */ | |
1095 | ||
1096 | error = -EEXIST; | |
1097 | if (kernfs_find_ns(new_parent, new_name, new_ns)) | |
1098 | goto out; | |
1099 | ||
1100 | /* rename kernfs_node */ | |
1101 | if (strcmp(kn->name, new_name) != 0) { | |
1102 | error = -ENOMEM; | |
1103 | new_name = kstrdup(new_name, GFP_KERNEL); | |
1104 | if (!new_name) | |
1105 | goto out; | |
1106 | ||
1107 | if (kn->flags & KERNFS_STATIC_NAME) | |
1108 | kn->flags &= ~KERNFS_STATIC_NAME; | |
1109 | else | |
1110 | kfree(kn->name); | |
1111 | ||
1112 | kn->name = new_name; | |
1113 | } | |
1114 | ||
1115 | /* | |
1116 | * Move to the appropriate place in the appropriate directories rbtree. | |
1117 | */ | |
1118 | kernfs_unlink_sibling(kn); | |
1119 | kernfs_get(new_parent); | |
1120 | kernfs_put(kn->parent); | |
1121 | kn->ns = new_ns; | |
1122 | kn->hash = kernfs_name_hash(kn->name, kn->ns); | |
1123 | kn->parent = new_parent; | |
1124 | kernfs_link_sibling(kn); | |
1125 | ||
1126 | error = 0; | |
1127 | out: | |
1128 | mutex_unlock(&kernfs_mutex); | |
1129 | return error; | |
1130 | } | |
1131 | ||
1132 | /* Relationship between s_mode and the DT_xxx types */ | |
1133 | static inline unsigned char dt_type(struct kernfs_node *kn) | |
1134 | { | |
1135 | return (kn->mode >> 12) & 15; | |
1136 | } | |
1137 | ||
1138 | static int kernfs_dir_fop_release(struct inode *inode, struct file *filp) | |
1139 | { | |
1140 | kernfs_put(filp->private_data); | |
1141 | return 0; | |
1142 | } | |
1143 | ||
1144 | static struct kernfs_node *kernfs_dir_pos(const void *ns, | |
1145 | struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos) | |
1146 | { | |
1147 | if (pos) { | |
1148 | int valid = kernfs_active(pos) && | |
1149 | pos->parent == parent && hash == pos->hash; | |
1150 | kernfs_put(pos); | |
1151 | if (!valid) | |
1152 | pos = NULL; | |
1153 | } | |
1154 | if (!pos && (hash > 1) && (hash < INT_MAX)) { | |
1155 | struct rb_node *node = parent->dir.children.rb_node; | |
1156 | while (node) { | |
1157 | pos = rb_to_kn(node); | |
1158 | ||
1159 | if (hash < pos->hash) | |
1160 | node = node->rb_left; | |
1161 | else if (hash > pos->hash) | |
1162 | node = node->rb_right; | |
1163 | else | |
1164 | break; | |
1165 | } | |
1166 | } | |
1167 | /* Skip over entries which are dying/dead or in the wrong namespace */ | |
1168 | while (pos && (!kernfs_active(pos) || pos->ns != ns)) { | |
1169 | struct rb_node *node = rb_next(&pos->rb); | |
1170 | if (!node) | |
1171 | pos = NULL; | |
1172 | else | |
1173 | pos = rb_to_kn(node); | |
1174 | } | |
1175 | return pos; | |
1176 | } | |
1177 | ||
1178 | static struct kernfs_node *kernfs_dir_next_pos(const void *ns, | |
1179 | struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos) | |
1180 | { | |
1181 | pos = kernfs_dir_pos(ns, parent, ino, pos); | |
1182 | if (pos) | |
1183 | do { | |
1184 | struct rb_node *node = rb_next(&pos->rb); | |
1185 | if (!node) | |
1186 | pos = NULL; | |
1187 | else | |
1188 | pos = rb_to_kn(node); | |
1189 | } while (pos && (!kernfs_active(pos) || pos->ns != ns)); | |
1190 | return pos; | |
1191 | } | |
1192 | ||
1193 | static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx) | |
1194 | { | |
1195 | struct dentry *dentry = file->f_path.dentry; | |
1196 | struct kernfs_node *parent = dentry->d_fsdata; | |
1197 | struct kernfs_node *pos = file->private_data; | |
1198 | const void *ns = NULL; | |
1199 | ||
1200 | if (!dir_emit_dots(file, ctx)) | |
1201 | return 0; | |
1202 | mutex_lock(&kernfs_mutex); | |
1203 | ||
1204 | if (kernfs_ns_enabled(parent)) | |
1205 | ns = kernfs_info(dentry->d_sb)->ns; | |
1206 | ||
1207 | for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos); | |
1208 | pos; | |
1209 | pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) { | |
1210 | const char *name = pos->name; | |
1211 | unsigned int type = dt_type(pos); | |
1212 | int len = strlen(name); | |
1213 | ino_t ino = pos->ino; | |
1214 | ||
1215 | ctx->pos = pos->hash; | |
1216 | file->private_data = pos; | |
1217 | kernfs_get(pos); | |
1218 | ||
1219 | mutex_unlock(&kernfs_mutex); | |
1220 | if (!dir_emit(ctx, name, len, ino, type)) | |
1221 | return 0; | |
1222 | mutex_lock(&kernfs_mutex); | |
1223 | } | |
1224 | mutex_unlock(&kernfs_mutex); | |
1225 | file->private_data = NULL; | |
1226 | ctx->pos = INT_MAX; | |
1227 | return 0; | |
1228 | } | |
1229 | ||
1230 | static loff_t kernfs_dir_fop_llseek(struct file *file, loff_t offset, | |
1231 | int whence) | |
1232 | { | |
1233 | struct inode *inode = file_inode(file); | |
1234 | loff_t ret; | |
1235 | ||
1236 | mutex_lock(&inode->i_mutex); | |
1237 | ret = generic_file_llseek(file, offset, whence); | |
1238 | mutex_unlock(&inode->i_mutex); | |
1239 | ||
1240 | return ret; | |
1241 | } | |
1242 | ||
1243 | const struct file_operations kernfs_dir_fops = { | |
1244 | .read = generic_read_dir, | |
1245 | .iterate = kernfs_fop_readdir, | |
1246 | .release = kernfs_dir_fop_release, | |
1247 | .llseek = kernfs_dir_fop_llseek, | |
1248 | }; |