1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * fs/kernfs/dir.c - kernfs directory implementation
4 *
5 * Copyright (c) 2001-3 Patrick Mochel
6 * Copyright (c) 2007 SUSE Linux Products GmbH
7 * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
8 */
9
10 #include <linux/sched.h>
11 #include <linux/fs.h>
12 #include <linux/namei.h>
13 #include <linux/idr.h>
14 #include <linux/slab.h>
15 #include <linux/security.h>
16 #include <linux/hash.h>
17
18 #include "kernfs-internal.h"
19
20 /*
21 * Don't use rename_lock to piggy back on pr_cont_buf. We don't want to
22 * call pr_cont() while holding rename_lock. Because sometimes pr_cont()
23 * will perform wakeups when releasing console_sem. Holding rename_lock
24 * will introduce deadlock if the scheduler reads the kernfs_name in the
25 * wakeup path.
26 */
27 static DEFINE_SPINLOCK(kernfs_pr_cont_lock);
28 static char kernfs_pr_cont_buf[PATH_MAX]; /* protected by pr_cont_lock */
29
30 #define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb)
31
__kernfs_active(struct kernfs_node * kn)32 static bool __kernfs_active(struct kernfs_node *kn)
33 {
34 return atomic_read(&kn->active) >= 0;
35 }
36
kernfs_active(struct kernfs_node * kn)37 static bool kernfs_active(struct kernfs_node *kn)
38 {
39 lockdep_assert_held(&kernfs_root(kn)->kernfs_rwsem);
40 return __kernfs_active(kn);
41 }
42
kernfs_lockdep(struct kernfs_node * kn)43 static bool kernfs_lockdep(struct kernfs_node *kn)
44 {
45 #ifdef CONFIG_DEBUG_LOCK_ALLOC
46 return kn->flags & KERNFS_LOCKDEP;
47 #else
48 return false;
49 #endif
50 }
51
52 /* kernfs_node_depth - compute depth from @from to @to */
kernfs_depth(struct kernfs_node * from,struct kernfs_node * to)53 static size_t kernfs_depth(struct kernfs_node *from, struct kernfs_node *to)
54 {
55 size_t depth = 0;
56
57 while (rcu_dereference(to->__parent) && to != from) {
58 depth++;
59 to = rcu_dereference(to->__parent);
60 }
61 return depth;
62 }
63
kernfs_common_ancestor(struct kernfs_node * a,struct kernfs_node * b)64 static struct kernfs_node *kernfs_common_ancestor(struct kernfs_node *a,
65 struct kernfs_node *b)
66 {
67 size_t da, db;
68 struct kernfs_root *ra = kernfs_root(a), *rb = kernfs_root(b);
69
70 if (ra != rb)
71 return NULL;
72
73 da = kernfs_depth(ra->kn, a);
74 db = kernfs_depth(rb->kn, b);
75
76 while (da > db) {
77 a = rcu_dereference(a->__parent);
78 da--;
79 }
80 while (db > da) {
81 b = rcu_dereference(b->__parent);
82 db--;
83 }
84
85 /* worst case b and a will be the same at root */
86 while (b != a) {
87 b = rcu_dereference(b->__parent);
88 a = rcu_dereference(a->__parent);
89 }
90
91 return a;
92 }
93
94 /**
95 * kernfs_path_from_node_locked - find a pseudo-absolute path to @kn_to,
96 * where kn_from is treated as root of the path.
97 * @kn_from: kernfs node which should be treated as root for the path
98 * @kn_to: kernfs node to which path is needed
99 * @buf: buffer to copy the path into
100 * @buflen: size of @buf
101 *
102 * We need to handle couple of scenarios here:
103 * [1] when @kn_from is an ancestor of @kn_to at some level
104 * kn_from: /n1/n2/n3
105 * kn_to: /n1/n2/n3/n4/n5
106 * result: /n4/n5
107 *
108 * [2] when @kn_from is on a different hierarchy and we need to find common
109 * ancestor between @kn_from and @kn_to.
110 * kn_from: /n1/n2/n3/n4
111 * kn_to: /n1/n2/n5
112 * result: /../../n5
113 * OR
114 * kn_from: /n1/n2/n3/n4/n5 [depth=5]
115 * kn_to: /n1/n2/n3 [depth=3]
116 * result: /../..
117 *
118 * [3] when @kn_to is %NULL result will be "(null)"
119 *
120 * Return: the length of the constructed path. If the path would have been
121 * greater than @buflen, @buf contains the truncated path with the trailing
122 * '\0'. On error, -errno is returned.
123 */
kernfs_path_from_node_locked(struct kernfs_node * kn_to,struct kernfs_node * kn_from,char * buf,size_t buflen)124 static int kernfs_path_from_node_locked(struct kernfs_node *kn_to,
125 struct kernfs_node *kn_from,
126 char *buf, size_t buflen)
127 {
128 struct kernfs_node *kn, *common;
129 const char parent_str[] = "/..";
130 size_t depth_from, depth_to, len = 0;
131 ssize_t copied;
132 int i, j;
133
134 if (!kn_to)
135 return strscpy(buf, "(null)", buflen);
136
137 if (!kn_from)
138 kn_from = kernfs_root(kn_to)->kn;
139
140 if (kn_from == kn_to)
141 return strscpy(buf, "/", buflen);
142
143 common = kernfs_common_ancestor(kn_from, kn_to);
144 if (WARN_ON(!common))
145 return -EINVAL;
146
147 depth_to = kernfs_depth(common, kn_to);
148 depth_from = kernfs_depth(common, kn_from);
149
150 buf[0] = '\0';
151
152 for (i = 0; i < depth_from; i++) {
153 copied = strscpy(buf + len, parent_str, buflen - len);
154 if (copied < 0)
155 return copied;
156 len += copied;
157 }
158
159 /* Calculate how many bytes we need for the rest */
160 for (i = depth_to - 1; i >= 0; i--) {
161 const char *name;
162
163 for (kn = kn_to, j = 0; j < i; j++)
164 kn = rcu_dereference(kn->__parent);
165
166 name = rcu_dereference(kn->name);
167 len += scnprintf(buf + len, buflen - len, "/%s", name);
168 }
169
170 return len;
171 }
172
173 /**
174 * kernfs_name - obtain the name of a given node
175 * @kn: kernfs_node of interest
176 * @buf: buffer to copy @kn's name into
177 * @buflen: size of @buf
178 *
179 * Copies the name of @kn into @buf of @buflen bytes. The behavior is
180 * similar to strscpy().
181 *
182 * Fills buffer with "(null)" if @kn is %NULL.
183 *
184 * Return: the resulting length of @buf. If @buf isn't long enough,
185 * it's filled up to @buflen-1 and nul terminated, and returns -E2BIG.
186 *
187 * This function can be called from any context.
188 */
kernfs_name(struct kernfs_node * kn,char * buf,size_t buflen)189 int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen)
190 {
191 struct kernfs_node *kn_parent;
192
193 if (!kn)
194 return strscpy(buf, "(null)", buflen);
195
196 guard(rcu)();
197 /*
198 * KERNFS_ROOT_INVARIANT_PARENT is ignored here. The name is RCU freed and
199 * the parent is either existing or not.
200 */
201 kn_parent = rcu_dereference(kn->__parent);
202 return strscpy(buf, kn_parent ? rcu_dereference(kn->name) : "/", buflen);
203 }
204
205 /**
206 * kernfs_path_from_node - build path of node @to relative to @from.
207 * @from: parent kernfs_node relative to which we need to build the path
208 * @to: kernfs_node of interest
209 * @buf: buffer to copy @to's path into
210 * @buflen: size of @buf
211 *
212 * Builds @to's path relative to @from in @buf. @from and @to must
213 * be on the same kernfs-root. If @from is not parent of @to, then a relative
214 * path (which includes '..'s) as needed to reach from @from to @to is
215 * returned.
216 *
217 * Return: the length of the constructed path. If the path would have been
218 * greater than @buflen, @buf contains the truncated path with the trailing
219 * '\0'. On error, -errno is returned.
220 */
kernfs_path_from_node(struct kernfs_node * to,struct kernfs_node * from,char * buf,size_t buflen)221 int kernfs_path_from_node(struct kernfs_node *to, struct kernfs_node *from,
222 char *buf, size_t buflen)
223 {
224 struct kernfs_root *root;
225
226 guard(rcu)();
227 if (to) {
228 root = kernfs_root(to);
229 if (!(root->flags & KERNFS_ROOT_INVARIANT_PARENT)) {
230 guard(read_lock_irqsave)(&root->kernfs_rename_lock);
231 return kernfs_path_from_node_locked(to, from, buf, buflen);
232 }
233 }
234 return kernfs_path_from_node_locked(to, from, buf, buflen);
235 }
236 EXPORT_SYMBOL_GPL(kernfs_path_from_node);
237
238 /**
239 * pr_cont_kernfs_name - pr_cont name of a kernfs_node
240 * @kn: kernfs_node of interest
241 *
242 * This function can be called from any context.
243 */
pr_cont_kernfs_name(struct kernfs_node * kn)244 void pr_cont_kernfs_name(struct kernfs_node *kn)
245 {
246 unsigned long flags;
247
248 spin_lock_irqsave(&kernfs_pr_cont_lock, flags);
249
250 kernfs_name(kn, kernfs_pr_cont_buf, sizeof(kernfs_pr_cont_buf));
251 pr_cont("%s", kernfs_pr_cont_buf);
252
253 spin_unlock_irqrestore(&kernfs_pr_cont_lock, flags);
254 }
255
256 /**
257 * pr_cont_kernfs_path - pr_cont path of a kernfs_node
258 * @kn: kernfs_node of interest
259 *
260 * This function can be called from any context.
261 */
pr_cont_kernfs_path(struct kernfs_node * kn)262 void pr_cont_kernfs_path(struct kernfs_node *kn)
263 {
264 unsigned long flags;
265 int sz;
266
267 spin_lock_irqsave(&kernfs_pr_cont_lock, flags);
268
269 sz = kernfs_path_from_node(kn, NULL, kernfs_pr_cont_buf,
270 sizeof(kernfs_pr_cont_buf));
271 if (sz < 0) {
272 if (sz == -E2BIG)
273 pr_cont("(name too long)");
274 else
275 pr_cont("(error)");
276 goto out;
277 }
278
279 pr_cont("%s", kernfs_pr_cont_buf);
280
281 out:
282 spin_unlock_irqrestore(&kernfs_pr_cont_lock, flags);
283 }
284
285 /**
286 * kernfs_get_parent - determine the parent node and pin it
287 * @kn: kernfs_node of interest
288 *
289 * Determines @kn's parent, pins and returns it. This function can be
290 * called from any context.
291 *
292 * Return: parent node of @kn
293 */
kernfs_get_parent(struct kernfs_node * kn)294 struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn)
295 {
296 struct kernfs_node *parent;
297 struct kernfs_root *root;
298 unsigned long flags;
299
300 root = kernfs_root(kn);
301 read_lock_irqsave(&root->kernfs_rename_lock, flags);
302 parent = kernfs_parent(kn);
303 kernfs_get(parent);
304 read_unlock_irqrestore(&root->kernfs_rename_lock, flags);
305
306 return parent;
307 }
308
309 /**
310 * kernfs_name_hash - calculate hash of @ns + @name
311 * @name: Null terminated string to hash
312 * @ns: Namespace tag to hash
313 *
314 * Return: 31-bit hash of ns + name (so it fits in an off_t)
315 */
kernfs_name_hash(const char * name,const void * ns)316 static unsigned int kernfs_name_hash(const char *name, const void *ns)
317 {
318 unsigned long hash = init_name_hash(ns);
319 unsigned int len = strlen(name);
320 while (len--)
321 hash = partial_name_hash(*name++, hash);
322 hash = end_name_hash(hash);
323 hash &= 0x7fffffffU;
324 /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */
325 if (hash < 2)
326 hash += 2;
327 if (hash >= INT_MAX)
328 hash = INT_MAX - 1;
329 return hash;
330 }
331
kernfs_name_compare(unsigned int hash,const char * name,const void * ns,const struct kernfs_node * kn)332 static int kernfs_name_compare(unsigned int hash, const char *name,
333 const void *ns, const struct kernfs_node *kn)
334 {
335 if (hash < kn->hash)
336 return -1;
337 if (hash > kn->hash)
338 return 1;
339 if (ns < kn->ns)
340 return -1;
341 if (ns > kn->ns)
342 return 1;
343 return strcmp(name, kernfs_rcu_name(kn));
344 }
345
kernfs_sd_compare(const struct kernfs_node * left,const struct kernfs_node * right)346 static int kernfs_sd_compare(const struct kernfs_node *left,
347 const struct kernfs_node *right)
348 {
349 return kernfs_name_compare(left->hash, kernfs_rcu_name(left), left->ns, right);
350 }
351
352 /**
353 * kernfs_link_sibling - link kernfs_node into sibling rbtree
354 * @kn: kernfs_node of interest
355 *
356 * Link @kn into its sibling rbtree which starts from
357 * @kn->parent->dir.children.
358 *
359 * Locking:
360 * kernfs_rwsem held exclusive
361 *
362 * Return:
363 * %0 on success, -EEXIST on failure.
364 */
kernfs_link_sibling(struct kernfs_node * kn)365 static int kernfs_link_sibling(struct kernfs_node *kn)
366 {
367 struct rb_node *parent = NULL;
368 struct kernfs_node *kn_parent;
369 struct rb_node **node;
370
371 kn_parent = kernfs_parent(kn);
372 node = &kn_parent->dir.children.rb_node;
373
374 while (*node) {
375 struct kernfs_node *pos;
376 int result;
377
378 pos = rb_to_kn(*node);
379 parent = *node;
380 result = kernfs_sd_compare(kn, pos);
381 if (result < 0)
382 node = &pos->rb.rb_left;
383 else if (result > 0)
384 node = &pos->rb.rb_right;
385 else
386 return -EEXIST;
387 }
388
389 /* add new node and rebalance the tree */
390 rb_link_node(&kn->rb, parent, node);
391 rb_insert_color(&kn->rb, &kn_parent->dir.children);
392
393 /* successfully added, account subdir number */
394 down_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
395 if (kernfs_type(kn) == KERNFS_DIR)
396 kn_parent->dir.subdirs++;
397 kernfs_inc_rev(kn_parent);
398 up_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
399
400 return 0;
401 }
402
403 /**
404 * kernfs_unlink_sibling - unlink kernfs_node from sibling rbtree
405 * @kn: kernfs_node of interest
406 *
407 * Try to unlink @kn from its sibling rbtree which starts from
408 * kn->parent->dir.children.
409 *
410 * Return: %true if @kn was actually removed,
411 * %false if @kn wasn't on the rbtree.
412 *
413 * Locking:
414 * kernfs_rwsem held exclusive
415 */
kernfs_unlink_sibling(struct kernfs_node * kn)416 static bool kernfs_unlink_sibling(struct kernfs_node *kn)
417 {
418 struct kernfs_node *kn_parent;
419
420 if (RB_EMPTY_NODE(&kn->rb))
421 return false;
422
423 kn_parent = kernfs_parent(kn);
424 down_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
425 if (kernfs_type(kn) == KERNFS_DIR)
426 kn_parent->dir.subdirs--;
427 kernfs_inc_rev(kn_parent);
428 up_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
429
430 rb_erase(&kn->rb, &kn_parent->dir.children);
431 RB_CLEAR_NODE(&kn->rb);
432 return true;
433 }
434
435 /**
436 * kernfs_get_active - get an active reference to kernfs_node
437 * @kn: kernfs_node to get an active reference to
438 *
439 * Get an active reference of @kn. This function is noop if @kn
440 * is %NULL.
441 *
442 * Return:
443 * Pointer to @kn on success, %NULL on failure.
444 */
kernfs_get_active(struct kernfs_node * kn)445 struct kernfs_node *kernfs_get_active(struct kernfs_node *kn)
446 {
447 if (unlikely(!kn))
448 return NULL;
449
450 if (!atomic_inc_unless_negative(&kn->active))
451 return NULL;
452
453 if (kernfs_lockdep(kn))
454 rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_);
455 return kn;
456 }
457
458 /**
459 * kernfs_put_active - put an active reference to kernfs_node
460 * @kn: kernfs_node to put an active reference to
461 *
462 * Put an active reference to @kn. This function is noop if @kn
463 * is %NULL.
464 */
kernfs_put_active(struct kernfs_node * kn)465 void kernfs_put_active(struct kernfs_node *kn)
466 {
467 int v;
468
469 if (unlikely(!kn))
470 return;
471
472 if (kernfs_lockdep(kn))
473 rwsem_release(&kn->dep_map, _RET_IP_);
474 v = atomic_dec_return(&kn->active);
475 if (likely(v != KN_DEACTIVATED_BIAS))
476 return;
477
478 wake_up_all(&kernfs_root(kn)->deactivate_waitq);
479 }
480
481 /**
482 * kernfs_drain - drain kernfs_node
483 * @kn: kernfs_node to drain
484 *
485 * Drain existing usages and nuke all existing mmaps of @kn. Multiple
486 * removers may invoke this function concurrently on @kn and all will
487 * return after draining is complete.
488 */
kernfs_drain(struct kernfs_node * kn)489 static void kernfs_drain(struct kernfs_node *kn)
490 __releases(&kernfs_root(kn)->kernfs_rwsem)
491 __acquires(&kernfs_root(kn)->kernfs_rwsem)
492 {
493 struct kernfs_root *root = kernfs_root(kn);
494
495 lockdep_assert_held_write(&root->kernfs_rwsem);
496 WARN_ON_ONCE(kernfs_active(kn));
497
498 /*
499 * Skip draining if already fully drained. This avoids draining and its
500 * lockdep annotations for nodes which have never been activated
501 * allowing embedding kernfs_remove() in create error paths without
502 * worrying about draining.
503 */
504 if (atomic_read(&kn->active) == KN_DEACTIVATED_BIAS &&
505 !kernfs_should_drain_open_files(kn))
506 return;
507
508 up_write(&root->kernfs_rwsem);
509
510 if (kernfs_lockdep(kn)) {
511 rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_);
512 if (atomic_read(&kn->active) != KN_DEACTIVATED_BIAS)
513 lock_contended(&kn->dep_map, _RET_IP_);
514 }
515
516 wait_event(root->deactivate_waitq,
517 atomic_read(&kn->active) == KN_DEACTIVATED_BIAS);
518
519 if (kernfs_lockdep(kn)) {
520 lock_acquired(&kn->dep_map, _RET_IP_);
521 rwsem_release(&kn->dep_map, _RET_IP_);
522 }
523
524 if (kernfs_should_drain_open_files(kn))
525 kernfs_drain_open_files(kn);
526
527 down_write(&root->kernfs_rwsem);
528 }
529
530 /**
531 * kernfs_get - get a reference count on a kernfs_node
532 * @kn: the target kernfs_node
533 */
kernfs_get(struct kernfs_node * kn)534 void kernfs_get(struct kernfs_node *kn)
535 {
536 if (kn) {
537 WARN_ON(!atomic_read(&kn->count));
538 atomic_inc(&kn->count);
539 }
540 }
541 EXPORT_SYMBOL_GPL(kernfs_get);
542
kernfs_free_rcu(struct rcu_head * rcu)543 static void kernfs_free_rcu(struct rcu_head *rcu)
544 {
545 struct kernfs_node *kn = container_of(rcu, struct kernfs_node, rcu);
546
547 /* If the whole node goes away, then name can't be used outside */
548 kfree_const(rcu_access_pointer(kn->name));
549
550 if (kn->iattr) {
551 simple_xattrs_free(&kn->iattr->xattrs, NULL);
552 kmem_cache_free(kernfs_iattrs_cache, kn->iattr);
553 }
554
555 kmem_cache_free(kernfs_node_cache, kn);
556 }
557
558 /**
559 * kernfs_put - put a reference count on a kernfs_node
560 * @kn: the target kernfs_node
561 *
562 * Put a reference count of @kn and destroy it if it reached zero.
563 */
kernfs_put(struct kernfs_node * kn)564 void kernfs_put(struct kernfs_node *kn)
565 {
566 struct kernfs_node *parent;
567 struct kernfs_root *root;
568
569 if (!kn || !atomic_dec_and_test(&kn->count))
570 return;
571 root = kernfs_root(kn);
572 repeat:
573 /*
574 * Moving/renaming is always done while holding reference.
575 * kn->parent won't change beneath us.
576 */
577 parent = kernfs_parent(kn);
578
579 WARN_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS,
580 "kernfs_put: %s/%s: released with incorrect active_ref %d\n",
581 parent ? rcu_dereference(parent->name) : "",
582 rcu_dereference(kn->name), atomic_read(&kn->active));
583
584 if (kernfs_type(kn) == KERNFS_LINK)
585 kernfs_put(kn->symlink.target_kn);
586
587 spin_lock(&root->kernfs_idr_lock);
588 idr_remove(&root->ino_idr, (u32)kernfs_ino(kn));
589 spin_unlock(&root->kernfs_idr_lock);
590
591 call_rcu(&kn->rcu, kernfs_free_rcu);
592
593 kn = parent;
594 if (kn) {
595 if (atomic_dec_and_test(&kn->count))
596 goto repeat;
597 } else {
598 /* just released the root kn, free @root too */
599 idr_destroy(&root->ino_idr);
600 kfree_rcu(root, rcu);
601 }
602 }
603 EXPORT_SYMBOL_GPL(kernfs_put);
604
605 /**
606 * kernfs_node_from_dentry - determine kernfs_node associated with a dentry
607 * @dentry: the dentry in question
608 *
609 * Return: the kernfs_node associated with @dentry. If @dentry is not a
610 * kernfs one, %NULL is returned.
611 *
612 * While the returned kernfs_node will stay accessible as long as @dentry
613 * is accessible, the returned node can be in any state and the caller is
614 * fully responsible for determining what's accessible.
615 */
kernfs_node_from_dentry(struct dentry * dentry)616 struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry)
617 {
618 if (dentry->d_sb->s_op == &kernfs_sops)
619 return kernfs_dentry_node(dentry);
620 return NULL;
621 }
622
__kernfs_new_node(struct kernfs_root * root,struct kernfs_node * parent,const char * name,umode_t mode,kuid_t uid,kgid_t gid,unsigned flags)623 static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
624 struct kernfs_node *parent,
625 const char *name, umode_t mode,
626 kuid_t uid, kgid_t gid,
627 unsigned flags)
628 {
629 struct kernfs_node *kn;
630 u32 id_highbits;
631 int ret;
632
633 name = kstrdup_const(name, GFP_KERNEL);
634 if (!name)
635 return NULL;
636
637 kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL);
638 if (!kn)
639 goto err_out1;
640
641 idr_preload(GFP_KERNEL);
642 spin_lock(&root->kernfs_idr_lock);
643 ret = idr_alloc_cyclic(&root->ino_idr, kn, 1, 0, GFP_ATOMIC);
644 if (ret >= 0 && ret < root->last_id_lowbits)
645 root->id_highbits++;
646 id_highbits = root->id_highbits;
647 root->last_id_lowbits = ret;
648 spin_unlock(&root->kernfs_idr_lock);
649 idr_preload_end();
650 if (ret < 0)
651 goto err_out2;
652
653 kn->id = (u64)id_highbits << 32 | ret;
654
655 atomic_set(&kn->count, 1);
656 atomic_set(&kn->active, KN_DEACTIVATED_BIAS);
657 RB_CLEAR_NODE(&kn->rb);
658
659 rcu_assign_pointer(kn->name, name);
660 kn->mode = mode;
661 kn->flags = flags;
662
663 if (!uid_eq(uid, GLOBAL_ROOT_UID) || !gid_eq(gid, GLOBAL_ROOT_GID)) {
664 struct iattr iattr = {
665 .ia_valid = ATTR_UID | ATTR_GID,
666 .ia_uid = uid,
667 .ia_gid = gid,
668 };
669
670 ret = __kernfs_setattr(kn, &iattr);
671 if (ret < 0)
672 goto err_out3;
673 }
674
675 if (parent) {
676 ret = security_kernfs_init_security(parent, kn);
677 if (ret)
678 goto err_out3;
679 }
680
681 return kn;
682
683 err_out3:
684 spin_lock(&root->kernfs_idr_lock);
685 idr_remove(&root->ino_idr, (u32)kernfs_ino(kn));
686 spin_unlock(&root->kernfs_idr_lock);
687 err_out2:
688 kmem_cache_free(kernfs_node_cache, kn);
689 err_out1:
690 kfree_const(name);
691 return NULL;
692 }
693
kernfs_new_node(struct kernfs_node * parent,const char * name,umode_t mode,kuid_t uid,kgid_t gid,unsigned flags)694 struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
695 const char *name, umode_t mode,
696 kuid_t uid, kgid_t gid,
697 unsigned flags)
698 {
699 struct kernfs_node *kn;
700
701 if (parent->mode & S_ISGID) {
702 /* this code block imitates inode_init_owner() for
703 * kernfs
704 */
705
706 if (parent->iattr)
707 gid = parent->iattr->ia_gid;
708
709 if (flags & KERNFS_DIR)
710 mode |= S_ISGID;
711 }
712
713 kn = __kernfs_new_node(kernfs_root(parent), parent,
714 name, mode, uid, gid, flags);
715 if (kn) {
716 kernfs_get(parent);
717 rcu_assign_pointer(kn->__parent, parent);
718 }
719 return kn;
720 }
721
722 /*
723 * kernfs_find_and_get_node_by_id - get kernfs_node from node id
724 * @root: the kernfs root
725 * @id: the target node id
726 *
727 * @id's lower 32bits encode ino and upper gen. If the gen portion is
728 * zero, all generations are matched.
729 *
730 * Return: %NULL on failure,
731 * otherwise a kernfs node with reference counter incremented.
732 */
kernfs_find_and_get_node_by_id(struct kernfs_root * root,u64 id)733 struct kernfs_node *kernfs_find_and_get_node_by_id(struct kernfs_root *root,
734 u64 id)
735 {
736 struct kernfs_node *kn;
737 ino_t ino = kernfs_id_ino(id);
738 u32 gen = kernfs_id_gen(id);
739
740 rcu_read_lock();
741
742 kn = idr_find(&root->ino_idr, (u32)ino);
743 if (!kn)
744 goto err_unlock;
745
746 if (sizeof(ino_t) >= sizeof(u64)) {
747 /* we looked up with the low 32bits, compare the whole */
748 if (kernfs_ino(kn) != ino)
749 goto err_unlock;
750 } else {
751 /* 0 matches all generations */
752 if (unlikely(gen && kernfs_gen(kn) != gen))
753 goto err_unlock;
754 }
755
756 /*
757 * We should fail if @kn has never been activated and guarantee success
758 * if the caller knows that @kn is active. Both can be achieved by
759 * __kernfs_active() which tests @kn->active without kernfs_rwsem.
760 */
761 if (unlikely(!__kernfs_active(kn) || !atomic_inc_not_zero(&kn->count)))
762 goto err_unlock;
763
764 rcu_read_unlock();
765 return kn;
766 err_unlock:
767 rcu_read_unlock();
768 return NULL;
769 }
770
771 /**
772 * kernfs_add_one - add kernfs_node to parent without warning
773 * @kn: kernfs_node to be added
774 *
775 * The caller must already have initialized @kn->parent. This
776 * function increments nlink of the parent's inode if @kn is a
777 * directory and link into the children list of the parent.
778 *
779 * Return:
780 * %0 on success, -EEXIST if entry with the given name already
781 * exists.
782 */
kernfs_add_one(struct kernfs_node * kn)783 int kernfs_add_one(struct kernfs_node *kn)
784 {
785 struct kernfs_root *root = kernfs_root(kn);
786 struct kernfs_iattrs *ps_iattr;
787 struct kernfs_node *parent;
788 bool has_ns;
789 int ret;
790
791 down_write(&root->kernfs_rwsem);
792 parent = kernfs_parent(kn);
793
794 ret = -EINVAL;
795 has_ns = kernfs_ns_enabled(parent);
796 if (WARN(has_ns != (bool)kn->ns, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
797 has_ns ? "required" : "invalid",
798 kernfs_rcu_name(parent), kernfs_rcu_name(kn)))
799 goto out_unlock;
800
801 if (kernfs_type(parent) != KERNFS_DIR)
802 goto out_unlock;
803
804 ret = -ENOENT;
805 if (parent->flags & (KERNFS_REMOVING | KERNFS_EMPTY_DIR))
806 goto out_unlock;
807
808 kn->hash = kernfs_name_hash(kernfs_rcu_name(kn), kn->ns);
809
810 ret = kernfs_link_sibling(kn);
811 if (ret)
812 goto out_unlock;
813
814 /* Update timestamps on the parent */
815 down_write(&root->kernfs_iattr_rwsem);
816
817 ps_iattr = parent->iattr;
818 if (ps_iattr) {
819 ktime_get_real_ts64(&ps_iattr->ia_ctime);
820 ps_iattr->ia_mtime = ps_iattr->ia_ctime;
821 }
822
823 up_write(&root->kernfs_iattr_rwsem);
824 up_write(&root->kernfs_rwsem);
825
826 /*
827 * Activate the new node unless CREATE_DEACTIVATED is requested.
828 * If not activated here, the kernfs user is responsible for
829 * activating the node with kernfs_activate(). A node which hasn't
830 * been activated is not visible to userland and its removal won't
831 * trigger deactivation.
832 */
833 if (!(kernfs_root(kn)->flags & KERNFS_ROOT_CREATE_DEACTIVATED))
834 kernfs_activate(kn);
835 return 0;
836
837 out_unlock:
838 up_write(&root->kernfs_rwsem);
839 return ret;
840 }
841
842 /**
843 * kernfs_find_ns - find kernfs_node with the given name
844 * @parent: kernfs_node to search under
845 * @name: name to look for
846 * @ns: the namespace tag to use
847 *
848 * Look for kernfs_node with name @name under @parent.
849 *
850 * Return: pointer to the found kernfs_node on success, %NULL on failure.
851 */
kernfs_find_ns(struct kernfs_node * parent,const unsigned char * name,const void * ns)852 static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
853 const unsigned char *name,
854 const void *ns)
855 {
856 struct rb_node *node = parent->dir.children.rb_node;
857 bool has_ns = kernfs_ns_enabled(parent);
858 unsigned int hash;
859
860 lockdep_assert_held(&kernfs_root(parent)->kernfs_rwsem);
861
862 if (has_ns != (bool)ns) {
863 WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
864 has_ns ? "required" : "invalid", kernfs_rcu_name(parent), name);
865 return NULL;
866 }
867
868 hash = kernfs_name_hash(name, ns);
869 while (node) {
870 struct kernfs_node *kn;
871 int result;
872
873 kn = rb_to_kn(node);
874 result = kernfs_name_compare(hash, name, ns, kn);
875 if (result < 0)
876 node = node->rb_left;
877 else if (result > 0)
878 node = node->rb_right;
879 else
880 return kn;
881 }
882 return NULL;
883 }
884
kernfs_walk_ns(struct kernfs_node * parent,const unsigned char * path,const void * ns)885 static struct kernfs_node *kernfs_walk_ns(struct kernfs_node *parent,
886 const unsigned char *path,
887 const void *ns)
888 {
889 ssize_t len;
890 char *p, *name;
891
892 lockdep_assert_held_read(&kernfs_root(parent)->kernfs_rwsem);
893
894 spin_lock_irq(&kernfs_pr_cont_lock);
895
896 len = strscpy(kernfs_pr_cont_buf, path, sizeof(kernfs_pr_cont_buf));
897
898 if (len < 0) {
899 spin_unlock_irq(&kernfs_pr_cont_lock);
900 return NULL;
901 }
902
903 p = kernfs_pr_cont_buf;
904
905 while ((name = strsep(&p, "/")) && parent) {
906 if (*name == '\0')
907 continue;
908 parent = kernfs_find_ns(parent, name, ns);
909 }
910
911 spin_unlock_irq(&kernfs_pr_cont_lock);
912
913 return parent;
914 }
915
916 /**
917 * kernfs_find_and_get_ns - find and get kernfs_node with the given name
918 * @parent: kernfs_node to search under
919 * @name: name to look for
920 * @ns: the namespace tag to use
921 *
922 * Look for kernfs_node with name @name under @parent and get a reference
923 * if found. This function may sleep.
924 *
925 * Return: pointer to the found kernfs_node on success, %NULL on failure.
926 */
kernfs_find_and_get_ns(struct kernfs_node * parent,const char * name,const void * ns)927 struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
928 const char *name, const void *ns)
929 {
930 struct kernfs_node *kn;
931 struct kernfs_root *root = kernfs_root(parent);
932
933 down_read(&root->kernfs_rwsem);
934 kn = kernfs_find_ns(parent, name, ns);
935 kernfs_get(kn);
936 up_read(&root->kernfs_rwsem);
937
938 return kn;
939 }
940 EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
941
942 /**
943 * kernfs_walk_and_get_ns - find and get kernfs_node with the given path
944 * @parent: kernfs_node to search under
945 * @path: path to look for
946 * @ns: the namespace tag to use
947 *
948 * Look for kernfs_node with path @path under @parent and get a reference
949 * if found. This function may sleep.
950 *
951 * Return: pointer to the found kernfs_node on success, %NULL on failure.
952 */
kernfs_walk_and_get_ns(struct kernfs_node * parent,const char * path,const void * ns)953 struct kernfs_node *kernfs_walk_and_get_ns(struct kernfs_node *parent,
954 const char *path, const void *ns)
955 {
956 struct kernfs_node *kn;
957 struct kernfs_root *root = kernfs_root(parent);
958
959 down_read(&root->kernfs_rwsem);
960 kn = kernfs_walk_ns(parent, path, ns);
961 kernfs_get(kn);
962 up_read(&root->kernfs_rwsem);
963
964 return kn;
965 }
966
kernfs_root_flags(struct kernfs_node * kn)967 unsigned int kernfs_root_flags(struct kernfs_node *kn)
968 {
969 return kernfs_root(kn)->flags;
970 }
971
972 /**
973 * kernfs_create_root - create a new kernfs hierarchy
974 * @scops: optional syscall operations for the hierarchy
975 * @flags: KERNFS_ROOT_* flags
976 * @priv: opaque data associated with the new directory
977 *
978 * Return: the root of the new hierarchy on success, ERR_PTR() value on
979 * failure.
980 */
kernfs_create_root(struct kernfs_syscall_ops * scops,unsigned int flags,void * priv)981 struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
982 unsigned int flags, void *priv)
983 {
984 struct kernfs_root *root;
985 struct kernfs_node *kn;
986
987 root = kzalloc(sizeof(*root), GFP_KERNEL);
988 if (!root)
989 return ERR_PTR(-ENOMEM);
990
991 idr_init(&root->ino_idr);
992 spin_lock_init(&root->kernfs_idr_lock);
993 init_rwsem(&root->kernfs_rwsem);
994 init_rwsem(&root->kernfs_iattr_rwsem);
995 init_rwsem(&root->kernfs_supers_rwsem);
996 INIT_LIST_HEAD(&root->supers);
997 rwlock_init(&root->kernfs_rename_lock);
998
999 /*
1000 * On 64bit ino setups, id is ino. On 32bit, low 32bits are ino.
1001 * High bits generation. The starting value for both ino and
1002 * genenration is 1. Initialize upper 32bit allocation
1003 * accordingly.
1004 */
1005 if (sizeof(ino_t) >= sizeof(u64))
1006 root->id_highbits = 0;
1007 else
1008 root->id_highbits = 1;
1009
1010 kn = __kernfs_new_node(root, NULL, "", S_IFDIR | S_IRUGO | S_IXUGO,
1011 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
1012 KERNFS_DIR);
1013 if (!kn) {
1014 idr_destroy(&root->ino_idr);
1015 kfree(root);
1016 return ERR_PTR(-ENOMEM);
1017 }
1018
1019 kn->priv = priv;
1020 kn->dir.root = root;
1021
1022 root->syscall_ops = scops;
1023 root->flags = flags;
1024 root->kn = kn;
1025 init_waitqueue_head(&root->deactivate_waitq);
1026
1027 if (!(root->flags & KERNFS_ROOT_CREATE_DEACTIVATED))
1028 kernfs_activate(kn);
1029
1030 return root;
1031 }
1032
1033 /**
1034 * kernfs_destroy_root - destroy a kernfs hierarchy
1035 * @root: root of the hierarchy to destroy
1036 *
1037 * Destroy the hierarchy anchored at @root by removing all existing
1038 * directories and destroying @root.
1039 */
kernfs_destroy_root(struct kernfs_root * root)1040 void kernfs_destroy_root(struct kernfs_root *root)
1041 {
1042 /*
1043 * kernfs_remove holds kernfs_rwsem from the root so the root
1044 * shouldn't be freed during the operation.
1045 */
1046 kernfs_get(root->kn);
1047 kernfs_remove(root->kn);
1048 kernfs_put(root->kn); /* will also free @root */
1049 }
1050
1051 /**
1052 * kernfs_root_to_node - return the kernfs_node associated with a kernfs_root
1053 * @root: root to use to lookup
1054 *
1055 * Return: @root's kernfs_node
1056 */
kernfs_root_to_node(struct kernfs_root * root)1057 struct kernfs_node *kernfs_root_to_node(struct kernfs_root *root)
1058 {
1059 return root->kn;
1060 }
1061
1062 /**
1063 * kernfs_create_dir_ns - create a directory
1064 * @parent: parent in which to create a new directory
1065 * @name: name of the new directory
1066 * @mode: mode of the new directory
1067 * @uid: uid of the new directory
1068 * @gid: gid of the new directory
1069 * @priv: opaque data associated with the new directory
1070 * @ns: optional namespace tag of the directory
1071 *
1072 * Return: the created node on success, ERR_PTR() value on failure.
1073 */
kernfs_create_dir_ns(struct kernfs_node * parent,const char * name,umode_t mode,kuid_t uid,kgid_t gid,void * priv,const void * ns)1074 struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
1075 const char *name, umode_t mode,
1076 kuid_t uid, kgid_t gid,
1077 void *priv, const void *ns)
1078 {
1079 struct kernfs_node *kn;
1080 int rc;
1081
1082 /* allocate */
1083 kn = kernfs_new_node(parent, name, mode | S_IFDIR,
1084 uid, gid, KERNFS_DIR);
1085 if (!kn)
1086 return ERR_PTR(-ENOMEM);
1087
1088 kn->dir.root = parent->dir.root;
1089 kn->ns = ns;
1090 kn->priv = priv;
1091
1092 /* link in */
1093 rc = kernfs_add_one(kn);
1094 if (!rc)
1095 return kn;
1096
1097 kernfs_put(kn);
1098 return ERR_PTR(rc);
1099 }
1100
1101 /**
1102 * kernfs_create_empty_dir - create an always empty directory
1103 * @parent: parent in which to create a new directory
1104 * @name: name of the new directory
1105 *
1106 * Return: the created node on success, ERR_PTR() value on failure.
1107 */
kernfs_create_empty_dir(struct kernfs_node * parent,const char * name)1108 struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent,
1109 const char *name)
1110 {
1111 struct kernfs_node *kn;
1112 int rc;
1113
1114 /* allocate */
1115 kn = kernfs_new_node(parent, name, S_IRUGO|S_IXUGO|S_IFDIR,
1116 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, KERNFS_DIR);
1117 if (!kn)
1118 return ERR_PTR(-ENOMEM);
1119
1120 kn->flags |= KERNFS_EMPTY_DIR;
1121 kn->dir.root = parent->dir.root;
1122 kn->ns = NULL;
1123 kn->priv = NULL;
1124
1125 /* link in */
1126 rc = kernfs_add_one(kn);
1127 if (!rc)
1128 return kn;
1129
1130 kernfs_put(kn);
1131 return ERR_PTR(rc);
1132 }
1133
kernfs_dop_revalidate(struct inode * dir,const struct qstr * name,struct dentry * dentry,unsigned int flags)1134 static int kernfs_dop_revalidate(struct inode *dir, const struct qstr *name,
1135 struct dentry *dentry, unsigned int flags)
1136 {
1137 struct kernfs_node *kn, *parent;
1138 struct kernfs_root *root;
1139
1140 if (flags & LOOKUP_RCU)
1141 return -ECHILD;
1142
1143 /* Negative hashed dentry? */
1144 if (d_really_is_negative(dentry)) {
1145 /* If the kernfs parent node has changed discard and
1146 * proceed to ->lookup.
1147 *
1148 * There's nothing special needed here when getting the
1149 * dentry parent, even if a concurrent rename is in
1150 * progress. That's because the dentry is negative so
1151 * it can only be the target of the rename and it will
1152 * be doing a d_move() not a replace. Consequently the
1153 * dentry d_parent won't change over the d_move().
1154 *
1155 * Also kernfs negative dentries transitioning from
1156 * negative to positive during revalidate won't happen
1157 * because they are invalidated on containing directory
1158 * changes and the lookup re-done so that a new positive
1159 * dentry can be properly created.
1160 */
1161 root = kernfs_root_from_sb(dentry->d_sb);
1162 down_read(&root->kernfs_rwsem);
1163 parent = kernfs_dentry_node(dentry->d_parent);
1164 if (parent) {
1165 if (kernfs_dir_changed(parent, dentry)) {
1166 up_read(&root->kernfs_rwsem);
1167 return 0;
1168 }
1169 }
1170 up_read(&root->kernfs_rwsem);
1171
1172 /* The kernfs parent node hasn't changed, leave the
1173 * dentry negative and return success.
1174 */
1175 return 1;
1176 }
1177
1178 kn = kernfs_dentry_node(dentry);
1179 root = kernfs_root(kn);
1180 down_read(&root->kernfs_rwsem);
1181
1182 /* The kernfs node has been deactivated */
1183 if (!kernfs_active(kn))
1184 goto out_bad;
1185
1186 parent = kernfs_parent(kn);
1187 /* The kernfs node has been moved? */
1188 if (kernfs_dentry_node(dentry->d_parent) != parent)
1189 goto out_bad;
1190
1191 /* The kernfs node has been renamed */
1192 if (strcmp(dentry->d_name.name, kernfs_rcu_name(kn)) != 0)
1193 goto out_bad;
1194
1195 /* The kernfs node has been moved to a different namespace */
1196 if (parent && kernfs_ns_enabled(parent) &&
1197 kernfs_info(dentry->d_sb)->ns != kn->ns)
1198 goto out_bad;
1199
1200 up_read(&root->kernfs_rwsem);
1201 return 1;
1202 out_bad:
1203 up_read(&root->kernfs_rwsem);
1204 return 0;
1205 }
1206
1207 const struct dentry_operations kernfs_dops = {
1208 .d_revalidate = kernfs_dop_revalidate,
1209 };
1210
kernfs_iop_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)1211 static struct dentry *kernfs_iop_lookup(struct inode *dir,
1212 struct dentry *dentry,
1213 unsigned int flags)
1214 {
1215 struct kernfs_node *parent = dir->i_private;
1216 struct kernfs_node *kn;
1217 struct kernfs_root *root;
1218 struct inode *inode = NULL;
1219 const void *ns = NULL;
1220
1221 root = kernfs_root(parent);
1222 down_read(&root->kernfs_rwsem);
1223 if (kernfs_ns_enabled(parent))
1224 ns = kernfs_info(dir->i_sb)->ns;
1225
1226 kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
1227 /* attach dentry and inode */
1228 if (kn) {
1229 /* Inactive nodes are invisible to the VFS so don't
1230 * create a negative.
1231 */
1232 if (!kernfs_active(kn)) {
1233 up_read(&root->kernfs_rwsem);
1234 return NULL;
1235 }
1236 inode = kernfs_get_inode(dir->i_sb, kn);
1237 if (!inode)
1238 inode = ERR_PTR(-ENOMEM);
1239 }
1240 /*
1241 * Needed for negative dentry validation.
1242 * The negative dentry can be created in kernfs_iop_lookup()
1243 * or transforms from positive dentry in dentry_unlink_inode()
1244 * called from vfs_rmdir().
1245 */
1246 if (!IS_ERR(inode))
1247 kernfs_set_rev(parent, dentry);
1248 up_read(&root->kernfs_rwsem);
1249
1250 /* instantiate and hash (possibly negative) dentry */
1251 return d_splice_alias(inode, dentry);
1252 }
1253
kernfs_iop_mkdir(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode)1254 static struct dentry *kernfs_iop_mkdir(struct mnt_idmap *idmap,
1255 struct inode *dir, struct dentry *dentry,
1256 umode_t mode)
1257 {
1258 struct kernfs_node *parent = dir->i_private;
1259 struct kernfs_syscall_ops *scops = kernfs_root(parent)->syscall_ops;
1260 int ret;
1261
1262 if (!scops || !scops->mkdir)
1263 return ERR_PTR(-EPERM);
1264
1265 if (!kernfs_get_active(parent))
1266 return ERR_PTR(-ENODEV);
1267
1268 ret = scops->mkdir(parent, dentry->d_name.name, mode);
1269
1270 kernfs_put_active(parent);
1271 return ERR_PTR(ret);
1272 }
1273
kernfs_iop_rmdir(struct inode * dir,struct dentry * dentry)1274 static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
1275 {
1276 struct kernfs_node *kn = kernfs_dentry_node(dentry);
1277 struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops;
1278 int ret;
1279
1280 if (!scops || !scops->rmdir)
1281 return -EPERM;
1282
1283 if (!kernfs_get_active(kn))
1284 return -ENODEV;
1285
1286 ret = scops->rmdir(kn);
1287
1288 kernfs_put_active(kn);
1289 return ret;
1290 }
1291
kernfs_iop_rename(struct mnt_idmap * idmap,struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)1292 static int kernfs_iop_rename(struct mnt_idmap *idmap,
1293 struct inode *old_dir, struct dentry *old_dentry,
1294 struct inode *new_dir, struct dentry *new_dentry,
1295 unsigned int flags)
1296 {
1297 struct kernfs_node *kn = kernfs_dentry_node(old_dentry);
1298 struct kernfs_node *new_parent = new_dir->i_private;
1299 struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops;
1300 int ret;
1301
1302 if (flags)
1303 return -EINVAL;
1304
1305 if (!scops || !scops->rename)
1306 return -EPERM;
1307
1308 if (!kernfs_get_active(kn))
1309 return -ENODEV;
1310
1311 if (!kernfs_get_active(new_parent)) {
1312 kernfs_put_active(kn);
1313 return -ENODEV;
1314 }
1315
1316 ret = scops->rename(kn, new_parent, new_dentry->d_name.name);
1317
1318 kernfs_put_active(new_parent);
1319 kernfs_put_active(kn);
1320 return ret;
1321 }
1322
1323 const struct inode_operations kernfs_dir_iops = {
1324 .lookup = kernfs_iop_lookup,
1325 .permission = kernfs_iop_permission,
1326 .setattr = kernfs_iop_setattr,
1327 .getattr = kernfs_iop_getattr,
1328 .listxattr = kernfs_iop_listxattr,
1329
1330 .mkdir = kernfs_iop_mkdir,
1331 .rmdir = kernfs_iop_rmdir,
1332 .rename = kernfs_iop_rename,
1333 };
1334
kernfs_leftmost_descendant(struct kernfs_node * pos)1335 static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
1336 {
1337 struct kernfs_node *last;
1338
1339 while (true) {
1340 struct rb_node *rbn;
1341
1342 last = pos;
1343
1344 if (kernfs_type(pos) != KERNFS_DIR)
1345 break;
1346
1347 rbn = rb_first(&pos->dir.children);
1348 if (!rbn)
1349 break;
1350
1351 pos = rb_to_kn(rbn);
1352 }
1353
1354 return last;
1355 }
1356
1357 /**
1358 * kernfs_next_descendant_post - find the next descendant for post-order walk
1359 * @pos: the current position (%NULL to initiate traversal)
1360 * @root: kernfs_node whose descendants to walk
1361 *
1362 * Find the next descendant to visit for post-order traversal of @root's
1363 * descendants. @root is included in the iteration and the last node to be
1364 * visited.
1365 *
1366 * Return: the next descendant to visit or %NULL when done.
1367 */
kernfs_next_descendant_post(struct kernfs_node * pos,struct kernfs_node * root)1368 static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
1369 struct kernfs_node *root)
1370 {
1371 struct rb_node *rbn;
1372
1373 lockdep_assert_held_write(&kernfs_root(root)->kernfs_rwsem);
1374
1375 /* if first iteration, visit leftmost descendant which may be root */
1376 if (!pos)
1377 return kernfs_leftmost_descendant(root);
1378
1379 /* if we visited @root, we're done */
1380 if (pos == root)
1381 return NULL;
1382
1383 /* if there's an unvisited sibling, visit its leftmost descendant */
1384 rbn = rb_next(&pos->rb);
1385 if (rbn)
1386 return kernfs_leftmost_descendant(rb_to_kn(rbn));
1387
1388 /* no sibling left, visit parent */
1389 return kernfs_parent(pos);
1390 }
1391
kernfs_activate_one(struct kernfs_node * kn)1392 static void kernfs_activate_one(struct kernfs_node *kn)
1393 {
1394 lockdep_assert_held_write(&kernfs_root(kn)->kernfs_rwsem);
1395
1396 kn->flags |= KERNFS_ACTIVATED;
1397
1398 if (kernfs_active(kn) || (kn->flags & (KERNFS_HIDDEN | KERNFS_REMOVING)))
1399 return;
1400
1401 WARN_ON_ONCE(rcu_access_pointer(kn->__parent) && RB_EMPTY_NODE(&kn->rb));
1402 WARN_ON_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS);
1403
1404 atomic_sub(KN_DEACTIVATED_BIAS, &kn->active);
1405 }
1406
1407 /**
1408 * kernfs_activate - activate a node which started deactivated
1409 * @kn: kernfs_node whose subtree is to be activated
1410 *
1411 * If the root has KERNFS_ROOT_CREATE_DEACTIVATED set, a newly created node
1412 * needs to be explicitly activated. A node which hasn't been activated
1413 * isn't visible to userland and deactivation is skipped during its
1414 * removal. This is useful to construct atomic init sequences where
1415 * creation of multiple nodes should either succeed or fail atomically.
1416 *
1417 * The caller is responsible for ensuring that this function is not called
1418 * after kernfs_remove*() is invoked on @kn.
1419 */
kernfs_activate(struct kernfs_node * kn)1420 void kernfs_activate(struct kernfs_node *kn)
1421 {
1422 struct kernfs_node *pos;
1423 struct kernfs_root *root = kernfs_root(kn);
1424
1425 down_write(&root->kernfs_rwsem);
1426
1427 pos = NULL;
1428 while ((pos = kernfs_next_descendant_post(pos, kn)))
1429 kernfs_activate_one(pos);
1430
1431 up_write(&root->kernfs_rwsem);
1432 }
1433
1434 /**
1435 * kernfs_show - show or hide a node
1436 * @kn: kernfs_node to show or hide
1437 * @show: whether to show or hide
1438 *
1439 * If @show is %false, @kn is marked hidden and deactivated. A hidden node is
1440 * ignored in future activaitons. If %true, the mark is removed and activation
1441 * state is restored. This function won't implicitly activate a new node in a
1442 * %KERNFS_ROOT_CREATE_DEACTIVATED root which hasn't been activated yet.
1443 *
1444 * To avoid recursion complexities, directories aren't supported for now.
1445 */
kernfs_show(struct kernfs_node * kn,bool show)1446 void kernfs_show(struct kernfs_node *kn, bool show)
1447 {
1448 struct kernfs_root *root = kernfs_root(kn);
1449
1450 if (WARN_ON_ONCE(kernfs_type(kn) == KERNFS_DIR))
1451 return;
1452
1453 down_write(&root->kernfs_rwsem);
1454
1455 if (show) {
1456 kn->flags &= ~KERNFS_HIDDEN;
1457 if (kn->flags & KERNFS_ACTIVATED)
1458 kernfs_activate_one(kn);
1459 } else {
1460 kn->flags |= KERNFS_HIDDEN;
1461 if (kernfs_active(kn))
1462 atomic_add(KN_DEACTIVATED_BIAS, &kn->active);
1463 kernfs_drain(kn);
1464 }
1465
1466 up_write(&root->kernfs_rwsem);
1467 }
1468
__kernfs_remove(struct kernfs_node * kn)1469 static void __kernfs_remove(struct kernfs_node *kn)
1470 {
1471 struct kernfs_node *pos, *parent;
1472
1473 /* Short-circuit if non-root @kn has already finished removal. */
1474 if (!kn)
1475 return;
1476
1477 lockdep_assert_held_write(&kernfs_root(kn)->kernfs_rwsem);
1478
1479 /*
1480 * This is for kernfs_remove_self() which plays with active ref
1481 * after removal.
1482 */
1483 if (kernfs_parent(kn) && RB_EMPTY_NODE(&kn->rb))
1484 return;
1485
1486 pr_debug("kernfs %s: removing\n", kernfs_rcu_name(kn));
1487
1488 /* prevent new usage by marking all nodes removing and deactivating */
1489 pos = NULL;
1490 while ((pos = kernfs_next_descendant_post(pos, kn))) {
1491 pos->flags |= KERNFS_REMOVING;
1492 if (kernfs_active(pos))
1493 atomic_add(KN_DEACTIVATED_BIAS, &pos->active);
1494 }
1495
1496 /* deactivate and unlink the subtree node-by-node */
1497 do {
1498 pos = kernfs_leftmost_descendant(kn);
1499
1500 /*
1501 * kernfs_drain() may drop kernfs_rwsem temporarily and @pos's
1502 * base ref could have been put by someone else by the time
1503 * the function returns. Make sure it doesn't go away
1504 * underneath us.
1505 */
1506 kernfs_get(pos);
1507
1508 kernfs_drain(pos);
1509 parent = kernfs_parent(pos);
1510 /*
1511 * kernfs_unlink_sibling() succeeds once per node. Use it
1512 * to decide who's responsible for cleanups.
1513 */
1514 if (!parent || kernfs_unlink_sibling(pos)) {
1515 struct kernfs_iattrs *ps_iattr =
1516 parent ? parent->iattr : NULL;
1517
1518 /* update timestamps on the parent */
1519 down_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
1520
1521 if (ps_iattr) {
1522 ktime_get_real_ts64(&ps_iattr->ia_ctime);
1523 ps_iattr->ia_mtime = ps_iattr->ia_ctime;
1524 }
1525
1526 up_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
1527 kernfs_put(pos);
1528 }
1529
1530 kernfs_put(pos);
1531 } while (pos != kn);
1532 }
1533
1534 /**
1535 * kernfs_remove - remove a kernfs_node recursively
1536 * @kn: the kernfs_node to remove
1537 *
1538 * Remove @kn along with all its subdirectories and files.
1539 */
kernfs_remove(struct kernfs_node * kn)1540 void kernfs_remove(struct kernfs_node *kn)
1541 {
1542 struct kernfs_root *root;
1543
1544 if (!kn)
1545 return;
1546
1547 root = kernfs_root(kn);
1548
1549 down_write(&root->kernfs_rwsem);
1550 __kernfs_remove(kn);
1551 up_write(&root->kernfs_rwsem);
1552 }
1553
1554 /**
1555 * kernfs_break_active_protection - break out of active protection
1556 * @kn: the self kernfs_node
1557 *
1558 * The caller must be running off of a kernfs operation which is invoked
1559 * with an active reference - e.g. one of kernfs_ops. Each invocation of
1560 * this function must also be matched with an invocation of
1561 * kernfs_unbreak_active_protection().
1562 *
1563 * This function releases the active reference of @kn the caller is
1564 * holding. Once this function is called, @kn may be removed at any point
1565 * and the caller is solely responsible for ensuring that the objects it
1566 * dereferences are accessible.
1567 */
kernfs_break_active_protection(struct kernfs_node * kn)1568 void kernfs_break_active_protection(struct kernfs_node *kn)
1569 {
1570 /*
1571 * Take out ourself out of the active ref dependency chain. If
1572 * we're called without an active ref, lockdep will complain.
1573 */
1574 kernfs_put_active(kn);
1575 }
1576
1577 /**
1578 * kernfs_unbreak_active_protection - undo kernfs_break_active_protection()
1579 * @kn: the self kernfs_node
1580 *
1581 * If kernfs_break_active_protection() was called, this function must be
1582 * invoked before finishing the kernfs operation. Note that while this
1583 * function restores the active reference, it doesn't and can't actually
1584 * restore the active protection - @kn may already or be in the process of
1585 * being drained and removed. Once kernfs_break_active_protection() is
1586 * invoked, that protection is irreversibly gone for the kernfs operation
1587 * instance.
1588 *
1589 * While this function may be called at any point after
1590 * kernfs_break_active_protection() is invoked, its most useful location
1591 * would be right before the enclosing kernfs operation returns.
1592 */
kernfs_unbreak_active_protection(struct kernfs_node * kn)1593 void kernfs_unbreak_active_protection(struct kernfs_node *kn)
1594 {
1595 /*
1596 * @kn->active could be in any state; however, the increment we do
1597 * here will be undone as soon as the enclosing kernfs operation
1598 * finishes and this temporary bump can't break anything. If @kn
1599 * is alive, nothing changes. If @kn is being deactivated, the
1600 * soon-to-follow put will either finish deactivation or restore
1601 * deactivated state. If @kn is already removed, the temporary
1602 * bump is guaranteed to be gone before @kn is released.
1603 */
1604 atomic_inc(&kn->active);
1605 if (kernfs_lockdep(kn))
1606 rwsem_acquire(&kn->dep_map, 0, 1, _RET_IP_);
1607 }
1608
1609 /**
1610 * kernfs_remove_self - remove a kernfs_node from its own method
1611 * @kn: the self kernfs_node to remove
1612 *
1613 * The caller must be running off of a kernfs operation which is invoked
1614 * with an active reference - e.g. one of kernfs_ops. This can be used to
1615 * implement a file operation which deletes itself.
1616 *
1617 * For example, the "delete" file for a sysfs device directory can be
1618 * implemented by invoking kernfs_remove_self() on the "delete" file
1619 * itself. This function breaks the circular dependency of trying to
1620 * deactivate self while holding an active ref itself. It isn't necessary
1621 * to modify the usual removal path to use kernfs_remove_self(). The
1622 * "delete" implementation can simply invoke kernfs_remove_self() on self
1623 * before proceeding with the usual removal path. kernfs will ignore later
1624 * kernfs_remove() on self.
1625 *
1626 * kernfs_remove_self() can be called multiple times concurrently on the
1627 * same kernfs_node. Only the first one actually performs removal and
1628 * returns %true. All others will wait until the kernfs operation which
1629 * won self-removal finishes and return %false. Note that the losers wait
1630 * for the completion of not only the winning kernfs_remove_self() but also
1631 * the whole kernfs_ops which won the arbitration. This can be used to
1632 * guarantee, for example, all concurrent writes to a "delete" file to
1633 * finish only after the whole operation is complete.
1634 *
1635 * Return: %true if @kn is removed by this call, otherwise %false.
1636 */
kernfs_remove_self(struct kernfs_node * kn)1637 bool kernfs_remove_self(struct kernfs_node *kn)
1638 {
1639 bool ret;
1640 struct kernfs_root *root = kernfs_root(kn);
1641
1642 down_write(&root->kernfs_rwsem);
1643 kernfs_break_active_protection(kn);
1644
1645 /*
1646 * SUICIDAL is used to arbitrate among competing invocations. Only
1647 * the first one will actually perform removal. When the removal
1648 * is complete, SUICIDED is set and the active ref is restored
1649 * while kernfs_rwsem for held exclusive. The ones which lost
1650 * arbitration waits for SUICIDED && drained which can happen only
1651 * after the enclosing kernfs operation which executed the winning
1652 * instance of kernfs_remove_self() finished.
1653 */
1654 if (!(kn->flags & KERNFS_SUICIDAL)) {
1655 kn->flags |= KERNFS_SUICIDAL;
1656 __kernfs_remove(kn);
1657 kn->flags |= KERNFS_SUICIDED;
1658 ret = true;
1659 } else {
1660 wait_queue_head_t *waitq = &kernfs_root(kn)->deactivate_waitq;
1661 DEFINE_WAIT(wait);
1662
1663 while (true) {
1664 prepare_to_wait(waitq, &wait, TASK_UNINTERRUPTIBLE);
1665
1666 if ((kn->flags & KERNFS_SUICIDED) &&
1667 atomic_read(&kn->active) == KN_DEACTIVATED_BIAS)
1668 break;
1669
1670 up_write(&root->kernfs_rwsem);
1671 schedule();
1672 down_write(&root->kernfs_rwsem);
1673 }
1674 finish_wait(waitq, &wait);
1675 WARN_ON_ONCE(!RB_EMPTY_NODE(&kn->rb));
1676 ret = false;
1677 }
1678
1679 /*
1680 * This must be done while kernfs_rwsem held exclusive; otherwise,
1681 * waiting for SUICIDED && deactivated could finish prematurely.
1682 */
1683 kernfs_unbreak_active_protection(kn);
1684
1685 up_write(&root->kernfs_rwsem);
1686 return ret;
1687 }
1688
1689 /**
1690 * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
1691 * @parent: parent of the target
1692 * @name: name of the kernfs_node to remove
1693 * @ns: namespace tag of the kernfs_node to remove
1694 *
1695 * Look for the kernfs_node with @name and @ns under @parent and remove it.
1696 *
1697 * Return: %0 on success, -ENOENT if such entry doesn't exist.
1698 */
kernfs_remove_by_name_ns(struct kernfs_node * parent,const char * name,const void * ns)1699 int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
1700 const void *ns)
1701 {
1702 struct kernfs_node *kn;
1703 struct kernfs_root *root;
1704
1705 if (!parent) {
1706 WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
1707 name);
1708 return -ENOENT;
1709 }
1710
1711 root = kernfs_root(parent);
1712 down_write(&root->kernfs_rwsem);
1713
1714 kn = kernfs_find_ns(parent, name, ns);
1715 if (kn) {
1716 kernfs_get(kn);
1717 __kernfs_remove(kn);
1718 kernfs_put(kn);
1719 }
1720
1721 up_write(&root->kernfs_rwsem);
1722
1723 if (kn)
1724 return 0;
1725 else
1726 return -ENOENT;
1727 }
1728
1729 /**
1730 * kernfs_rename_ns - move and rename a kernfs_node
1731 * @kn: target node
1732 * @new_parent: new parent to put @sd under
1733 * @new_name: new name
1734 * @new_ns: new namespace tag
1735 *
1736 * Return: %0 on success, -errno on failure.
1737 */
kernfs_rename_ns(struct kernfs_node * kn,struct kernfs_node * new_parent,const char * new_name,const void * new_ns)1738 int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
1739 const char *new_name, const void *new_ns)
1740 {
1741 struct kernfs_node *old_parent;
1742 struct kernfs_root *root;
1743 const char *old_name;
1744 int error;
1745
1746 /* can't move or rename root */
1747 if (!rcu_access_pointer(kn->__parent))
1748 return -EINVAL;
1749
1750 root = kernfs_root(kn);
1751 down_write(&root->kernfs_rwsem);
1752
1753 error = -ENOENT;
1754 if (!kernfs_active(kn) || !kernfs_active(new_parent) ||
1755 (new_parent->flags & KERNFS_EMPTY_DIR))
1756 goto out;
1757
1758 old_parent = kernfs_parent(kn);
1759 if (root->flags & KERNFS_ROOT_INVARIANT_PARENT) {
1760 error = -EINVAL;
1761 if (WARN_ON_ONCE(old_parent != new_parent))
1762 goto out;
1763 }
1764
1765 error = 0;
1766 old_name = kernfs_rcu_name(kn);
1767 if (!new_name)
1768 new_name = old_name;
1769 if ((old_parent == new_parent) && (kn->ns == new_ns) &&
1770 (strcmp(old_name, new_name) == 0))
1771 goto out; /* nothing to rename */
1772
1773 error = -EEXIST;
1774 if (kernfs_find_ns(new_parent, new_name, new_ns))
1775 goto out;
1776
1777 /* rename kernfs_node */
1778 if (strcmp(old_name, new_name) != 0) {
1779 error = -ENOMEM;
1780 new_name = kstrdup_const(new_name, GFP_KERNEL);
1781 if (!new_name)
1782 goto out;
1783 } else {
1784 new_name = NULL;
1785 }
1786
1787 /*
1788 * Move to the appropriate place in the appropriate directories rbtree.
1789 */
1790 kernfs_unlink_sibling(kn);
1791
1792 /* rename_lock protects ->parent accessors */
1793 if (old_parent != new_parent) {
1794 kernfs_get(new_parent);
1795 write_lock_irq(&root->kernfs_rename_lock);
1796
1797 rcu_assign_pointer(kn->__parent, new_parent);
1798
1799 kn->ns = new_ns;
1800 if (new_name)
1801 rcu_assign_pointer(kn->name, new_name);
1802
1803 write_unlock_irq(&root->kernfs_rename_lock);
1804 kernfs_put(old_parent);
1805 } else {
1806 /* name assignment is RCU protected, parent is the same */
1807 kn->ns = new_ns;
1808 if (new_name)
1809 rcu_assign_pointer(kn->name, new_name);
1810 }
1811
1812 kn->hash = kernfs_name_hash(new_name ?: old_name, kn->ns);
1813 kernfs_link_sibling(kn);
1814
1815 if (new_name && !is_kernel_rodata((unsigned long)old_name))
1816 kfree_rcu_mightsleep(old_name);
1817
1818 error = 0;
1819 out:
1820 up_write(&root->kernfs_rwsem);
1821 return error;
1822 }
1823
kernfs_dir_fop_release(struct inode * inode,struct file * filp)1824 static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
1825 {
1826 kernfs_put(filp->private_data);
1827 return 0;
1828 }
1829
kernfs_dir_pos(const void * ns,struct kernfs_node * parent,loff_t hash,struct kernfs_node * pos)1830 static struct kernfs_node *kernfs_dir_pos(const void *ns,
1831 struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
1832 {
1833 if (pos) {
1834 int valid = kernfs_active(pos) &&
1835 rcu_access_pointer(pos->__parent) == parent &&
1836 hash == pos->hash;
1837 kernfs_put(pos);
1838 if (!valid)
1839 pos = NULL;
1840 }
1841 if (!pos && (hash > 1) && (hash < INT_MAX)) {
1842 struct rb_node *node = parent->dir.children.rb_node;
1843 while (node) {
1844 pos = rb_to_kn(node);
1845
1846 if (hash < pos->hash)
1847 node = node->rb_left;
1848 else if (hash > pos->hash)
1849 node = node->rb_right;
1850 else
1851 break;
1852 }
1853 }
1854 /* Skip over entries which are dying/dead or in the wrong namespace */
1855 while (pos && (!kernfs_active(pos) || pos->ns != ns)) {
1856 struct rb_node *node = rb_next(&pos->rb);
1857 if (!node)
1858 pos = NULL;
1859 else
1860 pos = rb_to_kn(node);
1861 }
1862 return pos;
1863 }
1864
kernfs_dir_next_pos(const void * ns,struct kernfs_node * parent,ino_t ino,struct kernfs_node * pos)1865 static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
1866 struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
1867 {
1868 pos = kernfs_dir_pos(ns, parent, ino, pos);
1869 if (pos) {
1870 do {
1871 struct rb_node *node = rb_next(&pos->rb);
1872 if (!node)
1873 pos = NULL;
1874 else
1875 pos = rb_to_kn(node);
1876 } while (pos && (!kernfs_active(pos) || pos->ns != ns));
1877 }
1878 return pos;
1879 }
1880
kernfs_fop_readdir(struct file * file,struct dir_context * ctx)1881 static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
1882 {
1883 struct dentry *dentry = file->f_path.dentry;
1884 struct kernfs_node *parent = kernfs_dentry_node(dentry);
1885 struct kernfs_node *pos = file->private_data;
1886 struct kernfs_root *root;
1887 const void *ns = NULL;
1888
1889 if (!dir_emit_dots(file, ctx))
1890 return 0;
1891
1892 root = kernfs_root(parent);
1893 down_read(&root->kernfs_rwsem);
1894
1895 if (kernfs_ns_enabled(parent))
1896 ns = kernfs_info(dentry->d_sb)->ns;
1897
1898 for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos);
1899 pos;
1900 pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
1901 const char *name = kernfs_rcu_name(pos);
1902 unsigned int type = fs_umode_to_dtype(pos->mode);
1903 int len = strlen(name);
1904 ino_t ino = kernfs_ino(pos);
1905
1906 ctx->pos = pos->hash;
1907 file->private_data = pos;
1908 kernfs_get(pos);
1909
1910 if (!dir_emit(ctx, name, len, ino, type)) {
1911 up_read(&root->kernfs_rwsem);
1912 return 0;
1913 }
1914 }
1915 up_read(&root->kernfs_rwsem);
1916 file->private_data = NULL;
1917 ctx->pos = INT_MAX;
1918 return 0;
1919 }
1920
1921 const struct file_operations kernfs_dir_fops = {
1922 .read = generic_read_dir,
1923 .iterate_shared = kernfs_fop_readdir,
1924 .release = kernfs_dir_fop_release,
1925 .llseek = generic_file_llseek,
1926 };
1927