Merge tag 'pm-6.16-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
[linux-2.6-block.git] / fs / kernfs / dir.c
CommitLineData
55716d26 1// SPDX-License-Identifier: GPL-2.0-only
b8441ed2
TH
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>
b8441ed2 8 */
fd7b9f7b 9
abd54f02 10#include <linux/sched.h>
fd7b9f7b
TH
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
1a702dc8
HL
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 */
27static DEFINE_SPINLOCK(kernfs_pr_cont_lock);
28static char kernfs_pr_cont_buf[PATH_MAX]; /* protected by pr_cont_lock */
fd7b9f7b 29
adc5e8b5 30#define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb)
fd7b9f7b 31
1edfe4ea
TH
32static bool __kernfs_active(struct kernfs_node *kn)
33{
34 return atomic_read(&kn->active) >= 0;
35}
36
81c173cb
TH
37static bool kernfs_active(struct kernfs_node *kn)
38{
393c3714 39 lockdep_assert_held(&kernfs_root(kn)->kernfs_rwsem);
1edfe4ea 40 return __kernfs_active(kn);
81c173cb
TH
41}
42
182fd64b
TH
43static 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
9f6df573
AK
52/* kernfs_node_depth - compute depth from @from to @to */
53static size_t kernfs_depth(struct kernfs_node *from, struct kernfs_node *to)
3eef34ad 54{
9f6df573 55 size_t depth = 0;
3eef34ad 56
63348894 57 while (rcu_dereference(to->__parent) && to != from) {
9f6df573 58 depth++;
63348894 59 to = rcu_dereference(to->__parent);
9f6df573
AK
60 }
61 return depth;
62}
3eef34ad 63
9f6df573
AK
64static 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) {
63348894 77 a = rcu_dereference(a->__parent);
9f6df573
AK
78 da--;
79 }
80 while (db > da) {
63348894 81 b = rcu_dereference(b->__parent);
9f6df573
AK
82 db--;
83 }
84
85 /* worst case b and a will be the same at root */
86 while (b != a) {
63348894
SAS
87 b = rcu_dereference(b->__parent);
88 a = rcu_dereference(a->__parent);
9f6df573
AK
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 *
24b3e3dd 118 * [3] when @kn_to is %NULL result will be "(null)"
17627157 119 *
ff6d413b 120 * Return: the length of the constructed path. If the path would have been
3abb1d90
TH
121 * greater than @buflen, @buf contains the truncated path with the trailing
122 * '\0'. On error, -errno is returned.
9f6df573
AK
123 */
124static 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[] = "/..";
3abb1d90 130 size_t depth_from, depth_to, len = 0;
ff6d413b 131 ssize_t copied;
3abb1d90 132 int i, j;
9f6df573 133
17627157 134 if (!kn_to)
ff6d413b 135 return strscpy(buf, "(null)", buflen);
17627157 136
9f6df573
AK
137 if (!kn_from)
138 kn_from = kernfs_root(kn_to)->kn;
139
140 if (kn_from == kn_to)
ff6d413b 141 return strscpy(buf, "/", buflen);
9f6df573
AK
142
143 common = kernfs_common_ancestor(kn_from, kn_to);
144 if (WARN_ON(!common))
3abb1d90 145 return -EINVAL;
9f6df573
AK
146
147 depth_to = kernfs_depth(common, kn_to);
148 depth_from = kernfs_depth(common, kn_from);
149
bbe70e4e 150 buf[0] = '\0';
9f6df573 151
ff6d413b
KC
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 }
9f6df573
AK
158
159 /* Calculate how many bytes we need for the rest */
3abb1d90 160 for (i = depth_to - 1; i >= 0; i--) {
741c10b0 161 const char *name;
63348894 162
3abb1d90 163 for (kn = kn_to, j = 0; j < i; j++)
63348894 164 kn = rcu_dereference(kn->__parent);
ff6d413b 165
741c10b0
SAS
166 name = rcu_dereference(kn->name);
167 len += scnprintf(buf + len, buflen - len, "/%s", name);
9f6df573 168 }
3eef34ad 169
3abb1d90 170 return len;
3eef34ad
TH
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
5b56bf5c 180 * similar to strscpy().
3eef34ad 181 *
24b3e3dd
RD
182 * Fills buffer with "(null)" if @kn is %NULL.
183 *
5b56bf5c
KC
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.
17627157 186 *
3eef34ad
TH
187 * This function can be called from any context.
188 */
189int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen)
190{
741c10b0 191 struct kernfs_node *kn_parent;
3eef34ad 192
741c10b0
SAS
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);
3eef34ad
TH
203}
204
9f6df573
AK
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 *
ff6d413b 217 * Return: the length of the constructed path. If the path would have been
3abb1d90
TH
218 * greater than @buflen, @buf contains the truncated path with the trailing
219 * '\0'. On error, -errno is returned.
9f6df573
AK
220 */
221int kernfs_path_from_node(struct kernfs_node *to, struct kernfs_node *from,
222 char *buf, size_t buflen)
223{
741c10b0 224 struct kernfs_root *root;
9f6df573 225
63348894 226 guard(rcu)();
741c10b0
SAS
227 if (to) {
228 root = kernfs_root(to);
229 if (!(root->flags & KERNFS_ROOT_INVARIANT_PARENT)) {
93b27a84 230 guard(read_lock_irqsave)(&root->kernfs_rename_lock);
741c10b0
SAS
231 return kernfs_path_from_node_locked(to, from, buf, buflen);
232 }
233 }
234 return kernfs_path_from_node_locked(to, from, buf, buflen);
9f6df573
AK
235}
236EXPORT_SYMBOL_GPL(kernfs_path_from_node);
237
3eef34ad
TH
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 */
244void pr_cont_kernfs_name(struct kernfs_node *kn)
245{
246 unsigned long flags;
247
1a702dc8 248 spin_lock_irqsave(&kernfs_pr_cont_lock, flags);
3eef34ad 249
1a702dc8 250 kernfs_name(kn, kernfs_pr_cont_buf, sizeof(kernfs_pr_cont_buf));
3eef34ad
TH
251 pr_cont("%s", kernfs_pr_cont_buf);
252
1a702dc8 253 spin_unlock_irqrestore(&kernfs_pr_cont_lock, flags);
3eef34ad
TH
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 */
262void pr_cont_kernfs_path(struct kernfs_node *kn)
263{
264 unsigned long flags;
9f6df573 265 int sz;
3eef34ad 266
1a702dc8 267 spin_lock_irqsave(&kernfs_pr_cont_lock, flags);
3eef34ad 268
1a702dc8
HL
269 sz = kernfs_path_from_node(kn, NULL, kernfs_pr_cont_buf,
270 sizeof(kernfs_pr_cont_buf));
9f6df573 271 if (sz < 0) {
ff6d413b
KC
272 if (sz == -E2BIG)
273 pr_cont("(name too long)");
274 else
275 pr_cont("(error)");
9f6df573
AK
276 goto out;
277 }
278
279 pr_cont("%s", kernfs_pr_cont_buf);
3eef34ad 280
9f6df573 281out:
1a702dc8 282 spin_unlock_irqrestore(&kernfs_pr_cont_lock, flags);
3eef34ad
TH
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.
24b3e3dd
RD
291 *
292 * Return: parent node of @kn
3eef34ad
TH
293 */
294struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn)
295{
296 struct kernfs_node *parent;
93b27a84 297 struct kernfs_root *root;
3eef34ad
TH
298 unsigned long flags;
299
93b27a84
JZ
300 root = kernfs_root(kn);
301 read_lock_irqsave(&root->kernfs_rename_lock, flags);
63348894 302 parent = kernfs_parent(kn);
3eef34ad 303 kernfs_get(parent);
93b27a84 304 read_unlock_irqrestore(&root->kernfs_rename_lock, flags);
3eef34ad
TH
305
306 return parent;
307}
308
fd7b9f7b 309/**
24b3e3dd 310 * kernfs_name_hash - calculate hash of @ns + @name
fd7b9f7b
TH
311 * @name: Null terminated string to hash
312 * @ns: Namespace tag to hash
313 *
24b3e3dd 314 * Return: 31-bit hash of ns + name (so it fits in an off_t)
fd7b9f7b 315 */
c637b8ac 316static unsigned int kernfs_name_hash(const char *name, const void *ns)
fd7b9f7b 317{
8387ff25 318 unsigned long hash = init_name_hash(ns);
fd7b9f7b
TH
319 unsigned int len = strlen(name);
320 while (len--)
321 hash = partial_name_hash(*name++, hash);
8387ff25 322 hash = end_name_hash(hash);
fd7b9f7b
TH
323 hash &= 0x7fffffffU;
324 /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */
88391d49 325 if (hash < 2)
fd7b9f7b
TH
326 hash += 2;
327 if (hash >= INT_MAX)
328 hash = INT_MAX - 1;
329 return hash;
330}
331
c637b8ac
TH
332static int kernfs_name_compare(unsigned int hash, const char *name,
333 const void *ns, const struct kernfs_node *kn)
fd7b9f7b 334{
72392ed0
RV
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;
741c10b0 343 return strcmp(name, kernfs_rcu_name(kn));
fd7b9f7b
TH
344}
345
c637b8ac
TH
346static int kernfs_sd_compare(const struct kernfs_node *left,
347 const struct kernfs_node *right)
fd7b9f7b 348{
741c10b0 349 return kernfs_name_compare(left->hash, kernfs_rcu_name(left), left->ns, right);
fd7b9f7b
TH
350}
351
352/**
c637b8ac 353 * kernfs_link_sibling - link kernfs_node into sibling rbtree
324a56e1 354 * @kn: kernfs_node of interest
fd7b9f7b 355 *
324a56e1 356 * Link @kn into its sibling rbtree which starts from
adc5e8b5 357 * @kn->parent->dir.children.
fd7b9f7b
TH
358 *
359 * Locking:
7ba0273b 360 * kernfs_rwsem held exclusive
fd7b9f7b 361 *
24b3e3dd
RD
362 * Return:
363 * %0 on success, -EEXIST on failure.
fd7b9f7b 364 */
c637b8ac 365static int kernfs_link_sibling(struct kernfs_node *kn)
fd7b9f7b 366{
fd7b9f7b 367 struct rb_node *parent = NULL;
63348894
SAS
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;
fd7b9f7b 373
fd7b9f7b 374 while (*node) {
324a56e1 375 struct kernfs_node *pos;
fd7b9f7b
TH
376 int result;
377
324a56e1 378 pos = rb_to_kn(*node);
fd7b9f7b 379 parent = *node;
c637b8ac 380 result = kernfs_sd_compare(kn, pos);
fd7b9f7b 381 if (result < 0)
adc5e8b5 382 node = &pos->rb.rb_left;
fd7b9f7b 383 else if (result > 0)
adc5e8b5 384 node = &pos->rb.rb_right;
fd7b9f7b
TH
385 else
386 return -EEXIST;
387 }
c1befb88 388
fd7b9f7b 389 /* add new node and rebalance the tree */
adc5e8b5 390 rb_link_node(&kn->rb, parent, node);
63348894 391 rb_insert_color(&kn->rb, &kn_parent->dir.children);
c1befb88
JZ
392
393 /* successfully added, account subdir number */
0559f630 394 down_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
c1befb88 395 if (kernfs_type(kn) == KERNFS_DIR)
63348894
SAS
396 kn_parent->dir.subdirs++;
397 kernfs_inc_rev(kn_parent);
0559f630 398 up_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
c1befb88 399
fd7b9f7b
TH
400 return 0;
401}
402
403/**
c637b8ac 404 * kernfs_unlink_sibling - unlink kernfs_node from sibling rbtree
324a56e1 405 * @kn: kernfs_node of interest
fd7b9f7b 406 *
35beab06 407 * Try to unlink @kn from its sibling rbtree which starts from
24b3e3dd
RD
408 * kn->parent->dir.children.
409 *
410 * Return: %true if @kn was actually removed,
411 * %false if @kn wasn't on the rbtree.
fd7b9f7b
TH
412 *
413 * Locking:
7ba0273b 414 * kernfs_rwsem held exclusive
fd7b9f7b 415 */
35beab06 416static bool kernfs_unlink_sibling(struct kernfs_node *kn)
fd7b9f7b 417{
63348894
SAS
418 struct kernfs_node *kn_parent;
419
35beab06
TH
420 if (RB_EMPTY_NODE(&kn->rb))
421 return false;
422
63348894 423 kn_parent = kernfs_parent(kn);
0559f630 424 down_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
df23fc39 425 if (kernfs_type(kn) == KERNFS_DIR)
63348894
SAS
426 kn_parent->dir.subdirs--;
427 kernfs_inc_rev(kn_parent);
0559f630 428 up_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
fd7b9f7b 429
63348894 430 rb_erase(&kn->rb, &kn_parent->dir.children);
35beab06
TH
431 RB_CLEAR_NODE(&kn->rb);
432 return true;
fd7b9f7b
TH
433}
434
435/**
c637b8ac 436 * kernfs_get_active - get an active reference to kernfs_node
324a56e1 437 * @kn: kernfs_node to get an active reference to
fd7b9f7b 438 *
324a56e1 439 * Get an active reference of @kn. This function is noop if @kn
24b3e3dd 440 * is %NULL.
fd7b9f7b 441 *
24b3e3dd
RD
442 * Return:
443 * Pointer to @kn on success, %NULL on failure.
fd7b9f7b 444 */
c637b8ac 445struct kernfs_node *kernfs_get_active(struct kernfs_node *kn)
fd7b9f7b 446{
324a56e1 447 if (unlikely(!kn))
fd7b9f7b
TH
448 return NULL;
449
f4b3e631
GKH
450 if (!atomic_inc_unless_negative(&kn->active))
451 return NULL;
895a068a 452
182fd64b 453 if (kernfs_lockdep(kn))
f4b3e631
GKH
454 rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_);
455 return kn;
fd7b9f7b
TH
456}
457
458/**
c637b8ac 459 * kernfs_put_active - put an active reference to kernfs_node
324a56e1 460 * @kn: kernfs_node to put an active reference to
fd7b9f7b 461 *
324a56e1 462 * Put an active reference to @kn. This function is noop if @kn
24b3e3dd 463 * is %NULL.
fd7b9f7b 464 */
c637b8ac 465void kernfs_put_active(struct kernfs_node *kn)
fd7b9f7b
TH
466{
467 int v;
468
324a56e1 469 if (unlikely(!kn))
fd7b9f7b
TH
470 return;
471
182fd64b 472 if (kernfs_lockdep(kn))
5facae4f 473 rwsem_release(&kn->dep_map, _RET_IP_);
adc5e8b5 474 v = atomic_dec_return(&kn->active);
df23fc39 475 if (likely(v != KN_DEACTIVATED_BIAS))
fd7b9f7b
TH
476 return;
477
2fd60da4 478 wake_up_all(&kernfs_root(kn)->deactivate_waitq);
fd7b9f7b
TH
479}
480
481/**
81c173cb
TH
482 * kernfs_drain - drain kernfs_node
483 * @kn: kernfs_node to drain
fd7b9f7b 484 *
24b3e3dd 485 * Drain existing usages and nuke all existing mmaps of @kn. Multiple
81c173cb
TH
486 * removers may invoke this function concurrently on @kn and all will
487 * return after draining is complete.
fd7b9f7b 488 */
81c173cb 489static void kernfs_drain(struct kernfs_node *kn)
393c3714
MK
490 __releases(&kernfs_root(kn)->kernfs_rwsem)
491 __acquires(&kernfs_root(kn)->kernfs_rwsem)
fd7b9f7b 492{
abd54f02 493 struct kernfs_root *root = kernfs_root(kn);
fd7b9f7b 494
393c3714 495 lockdep_assert_held_write(&root->kernfs_rwsem);
81c173cb 496 WARN_ON_ONCE(kernfs_active(kn));
ea1c472d 497
2d7f9f8c
TH
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
393c3714 508 up_write(&root->kernfs_rwsem);
abd54f02 509
182fd64b 510 if (kernfs_lockdep(kn)) {
35beab06
TH
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 }
abd54f02
TH
515
516 wait_event(root->deactivate_waitq,
517 atomic_read(&kn->active) == KN_DEACTIVATED_BIAS);
fd7b9f7b 518
182fd64b 519 if (kernfs_lockdep(kn)) {
a6607930 520 lock_acquired(&kn->dep_map, _RET_IP_);
5facae4f 521 rwsem_release(&kn->dep_map, _RET_IP_);
a6607930 522 }
35beab06 523
bdb2fd7f
TH
524 if (kernfs_should_drain_open_files(kn))
525 kernfs_drain_open_files(kn);
ccf02aaf 526
393c3714 527 down_write(&root->kernfs_rwsem);
fd7b9f7b
TH
528}
529
fd7b9f7b 530/**
324a56e1
TH
531 * kernfs_get - get a reference count on a kernfs_node
532 * @kn: the target kernfs_node
fd7b9f7b 533 */
324a56e1 534void kernfs_get(struct kernfs_node *kn)
fd7b9f7b 535{
324a56e1 536 if (kn) {
adc5e8b5
TH
537 WARN_ON(!atomic_read(&kn->count));
538 atomic_inc(&kn->count);
fd7b9f7b
TH
539 }
540}
541EXPORT_SYMBOL_GPL(kernfs_get);
542
4207b556
TH
543static void kernfs_free_rcu(struct rcu_head *rcu)
544{
545 struct kernfs_node *kn = container_of(rcu, struct kernfs_node, rcu);
546
741c10b0
SAS
547 /* If the whole node goes away, then name can't be used outside */
548 kfree_const(rcu_access_pointer(kn->name));
4207b556
TH
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
fd7b9f7b 558/**
324a56e1
TH
559 * kernfs_put - put a reference count on a kernfs_node
560 * @kn: the target kernfs_node
fd7b9f7b 561 *
324a56e1 562 * Put a reference count of @kn and destroy it if it reached zero.
fd7b9f7b 563 */
324a56e1 564void kernfs_put(struct kernfs_node *kn)
fd7b9f7b 565{
324a56e1 566 struct kernfs_node *parent;
ba7443bc 567 struct kernfs_root *root;
fd7b9f7b 568
adc5e8b5 569 if (!kn || !atomic_dec_and_test(&kn->count))
fd7b9f7b 570 return;
324a56e1 571 root = kernfs_root(kn);
fd7b9f7b 572 repeat:
81c173cb
TH
573 /*
574 * Moving/renaming is always done while holding reference.
adc5e8b5 575 * kn->parent won't change beneath us.
fd7b9f7b 576 */
63348894 577 parent = kernfs_parent(kn);
fd7b9f7b 578
81c173cb
TH
579 WARN_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS,
580 "kernfs_put: %s/%s: released with incorrect active_ref %d\n",
741c10b0
SAS
581 parent ? rcu_dereference(parent->name) : "",
582 rcu_dereference(kn->name), atomic_read(&kn->active));
324a56e1 583
df23fc39 584 if (kernfs_type(kn) == KERNFS_LINK)
adc5e8b5 585 kernfs_put(kn->symlink.target_kn);
dfeb0750 586
cec59c44 587 spin_lock(&root->kernfs_idr_lock);
40430452 588 idr_remove(&root->ino_idr, (u32)kernfs_ino(kn));
cec59c44 589 spin_unlock(&root->kernfs_idr_lock);
4207b556
TH
590
591 call_rcu(&kn->rcu, kernfs_free_rcu);
fd7b9f7b 592
324a56e1
TH
593 kn = parent;
594 if (kn) {
adc5e8b5 595 if (atomic_dec_and_test(&kn->count))
ba7443bc
TH
596 goto repeat;
597 } else {
324a56e1 598 /* just released the root kn, free @root too */
7d35079f 599 idr_destroy(&root->ino_idr);
4207b556 600 kfree_rcu(root, rcu);
ba7443bc 601 }
fd7b9f7b
TH
602}
603EXPORT_SYMBOL_GPL(kernfs_put);
604
0c23b225
TH
605/**
606 * kernfs_node_from_dentry - determine kernfs_node associated with a dentry
607 * @dentry: the dentry in question
608 *
24b3e3dd 609 * Return: the kernfs_node associated with @dentry. If @dentry is not a
0c23b225
TH
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 */
616struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry)
617{
0288e7fa 618 if (dentry->d_sb->s_op == &kernfs_sops)
319ba91d 619 return kernfs_dentry_node(dentry);
0c23b225
TH
620 return NULL;
621}
622
db4aad20 623static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
e19dfdc8 624 struct kernfs_node *parent,
db4aad20 625 const char *name, umode_t mode,
488dee96 626 kuid_t uid, kgid_t gid,
db4aad20 627 unsigned flags)
fd7b9f7b 628{
324a56e1 629 struct kernfs_node *kn;
40430452 630 u32 id_highbits;
bc755553 631 int ret;
fd7b9f7b 632
dfeb0750
TH
633 name = kstrdup_const(name, GFP_KERNEL);
634 if (!name)
635 return NULL;
fd7b9f7b 636
a797bfc3 637 kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL);
324a56e1 638 if (!kn)
fd7b9f7b
TH
639 goto err_out1;
640
7d35079f 641 idr_preload(GFP_KERNEL);
cec59c44 642 spin_lock(&root->kernfs_idr_lock);
4a3ef68a 643 ret = idr_alloc_cyclic(&root->ino_idr, kn, 1, 0, GFP_ATOMIC);
40430452
TH
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;
cec59c44 648 spin_unlock(&root->kernfs_idr_lock);
7d35079f 649 idr_preload_end();
bc755553 650 if (ret < 0)
fd7b9f7b 651 goto err_out2;
67c0496e 652
40430452 653 kn->id = (u64)id_highbits << 32 | ret;
fd7b9f7b 654
b680b081 655 atomic_set(&kn->count, 1);
81c173cb 656 atomic_set(&kn->active, KN_DEACTIVATED_BIAS);
35beab06 657 RB_CLEAR_NODE(&kn->rb);
fd7b9f7b 658
741c10b0 659 rcu_assign_pointer(kn->name, name);
adc5e8b5 660 kn->mode = mode;
81c173cb 661 kn->flags = flags;
fd7b9f7b 662
488dee96
DT
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
e19dfdc8
OM
675 if (parent) {
676 ret = security_kernfs_init_security(parent, kn);
677 if (ret)
678 goto err_out3;
679 }
680
324a56e1 681 return kn;
fd7b9f7b 682
488dee96 683 err_out3:
cec59c44 684 spin_lock(&root->kernfs_idr_lock);
40430452 685 idr_remove(&root->ino_idr, (u32)kernfs_ino(kn));
cec59c44 686 spin_unlock(&root->kernfs_idr_lock);
fd7b9f7b 687 err_out2:
a797bfc3 688 kmem_cache_free(kernfs_node_cache, kn);
fd7b9f7b 689 err_out1:
dfeb0750 690 kfree_const(name);
fd7b9f7b
TH
691 return NULL;
692}
693
db4aad20
TH
694struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
695 const char *name, umode_t mode,
488dee96 696 kuid_t uid, kgid_t gid,
db4aad20
TH
697 unsigned flags)
698{
699 struct kernfs_node *kn;
700
5133bee6
MK
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
e19dfdc8 713 kn = __kernfs_new_node(kernfs_root(parent), parent,
488dee96 714 name, mode, uid, gid, flags);
db4aad20
TH
715 if (kn) {
716 kernfs_get(parent);
63348894 717 rcu_assign_pointer(kn->__parent, parent);
db4aad20
TH
718 }
719 return kn;
720}
721
ba16b284 722/*
fe0f726c 723 * kernfs_find_and_get_node_by_id - get kernfs_node from node id
ba16b284 724 * @root: the kernfs root
fe0f726c
TH
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.
ba16b284 729 *
24b3e3dd
RD
730 * Return: %NULL on failure,
731 * otherwise a kernfs node with reference counter incremented.
ba16b284 732 */
fe0f726c
TH
733struct kernfs_node *kernfs_find_and_get_node_by_id(struct kernfs_root *root,
734 u64 id)
ba16b284
SL
735{
736 struct kernfs_node *kn;
fe0f726c
TH
737 ino_t ino = kernfs_id_ino(id);
738 u32 gen = kernfs_id_gen(id);
ba16b284 739
4207b556 740 rcu_read_lock();
b680b081 741
40430452 742 kn = idr_find(&root->ino_idr, (u32)ino);
ba16b284 743 if (!kn)
b680b081 744 goto err_unlock;
ba16b284 745
40430452
TH
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 }
fe0f726c 755
1edfe4ea
TH
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)))
b680b081 762 goto err_unlock;
ba16b284 763
4207b556 764 rcu_read_unlock();
ba16b284 765 return kn;
b680b081 766err_unlock:
4207b556 767 rcu_read_unlock();
ba16b284
SL
768 return NULL;
769}
770
fd7b9f7b 771/**
c637b8ac 772 * kernfs_add_one - add kernfs_node to parent without warning
324a56e1 773 * @kn: kernfs_node to be added
fd7b9f7b 774 *
db4aad20
TH
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.
fd7b9f7b 778 *
24b3e3dd
RD
779 * Return:
780 * %0 on success, -EEXIST if entry with the given name already
fd7b9f7b
TH
781 * exists.
782 */
988cd7af 783int kernfs_add_one(struct kernfs_node *kn)
fd7b9f7b 784{
63348894 785 struct kernfs_root *root = kernfs_root(kn);
c525aadd 786 struct kernfs_iattrs *ps_iattr;
63348894 787 struct kernfs_node *parent;
988cd7af 788 bool has_ns;
fd7b9f7b
TH
789 int ret;
790
393c3714 791 down_write(&root->kernfs_rwsem);
63348894 792 parent = kernfs_parent(kn);
988cd7af
TH
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",
741c10b0
SAS
797 has_ns ? "required" : "invalid",
798 kernfs_rcu_name(parent), kernfs_rcu_name(kn)))
988cd7af 799 goto out_unlock;
fd7b9f7b 800
df23fc39 801 if (kernfs_type(parent) != KERNFS_DIR)
988cd7af 802 goto out_unlock;
fd7b9f7b 803
988cd7af 804 ret = -ENOENT;
c2549174 805 if (parent->flags & (KERNFS_REMOVING | KERNFS_EMPTY_DIR))
988cd7af 806 goto out_unlock;
798c75a0 807
741c10b0 808 kn->hash = kernfs_name_hash(kernfs_rcu_name(kn), kn->ns);
fd7b9f7b 809
c637b8ac 810 ret = kernfs_link_sibling(kn);
fd7b9f7b 811 if (ret)
988cd7af 812 goto out_unlock;
fd7b9f7b
TH
813
814 /* Update timestamps on the parent */
9caf6961
IK
815 down_write(&root->kernfs_iattr_rwsem);
816
adc5e8b5 817 ps_iattr = parent->iattr;
fd7b9f7b 818 if (ps_iattr) {
05895219
OM
819 ktime_get_real_ts64(&ps_iattr->ia_ctime);
820 ps_iattr->ia_mtime = ps_iattr->ia_ctime;
fd7b9f7b
TH
821 }
822
9caf6961 823 up_write(&root->kernfs_iattr_rwsem);
393c3714 824 up_write(&root->kernfs_rwsem);
d35258ef
TH
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
988cd7af 837out_unlock:
393c3714 838 up_write(&root->kernfs_rwsem);
988cd7af 839 return ret;
fd7b9f7b
TH
840}
841
842/**
324a56e1
TH
843 * kernfs_find_ns - find kernfs_node with the given name
844 * @parent: kernfs_node to search under
fd7b9f7b
TH
845 * @name: name to look for
846 * @ns: the namespace tag to use
847 *
24b3e3dd
RD
848 * Look for kernfs_node with name @name under @parent.
849 *
850 * Return: pointer to the found kernfs_node on success, %NULL on failure.
fd7b9f7b 851 */
324a56e1
TH
852static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
853 const unsigned char *name,
854 const void *ns)
fd7b9f7b 855{
adc5e8b5 856 struct rb_node *node = parent->dir.children.rb_node;
ac9bba03 857 bool has_ns = kernfs_ns_enabled(parent);
fd7b9f7b
TH
858 unsigned int hash;
859
393c3714 860 lockdep_assert_held(&kernfs_root(parent)->kernfs_rwsem);
fd7b9f7b
TH
861
862 if (has_ns != (bool)ns) {
c637b8ac 863 WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
741c10b0 864 has_ns ? "required" : "invalid", kernfs_rcu_name(parent), name);
fd7b9f7b
TH
865 return NULL;
866 }
867
c637b8ac 868 hash = kernfs_name_hash(name, ns);
fd7b9f7b 869 while (node) {
324a56e1 870 struct kernfs_node *kn;
fd7b9f7b
TH
871 int result;
872
324a56e1 873 kn = rb_to_kn(node);
c637b8ac 874 result = kernfs_name_compare(hash, name, ns, kn);
fd7b9f7b
TH
875 if (result < 0)
876 node = node->rb_left;
877 else if (result > 0)
878 node = node->rb_right;
879 else
324a56e1 880 return kn;
fd7b9f7b
TH
881 }
882 return NULL;
883}
884
bd96f76a
TH
885static struct kernfs_node *kernfs_walk_ns(struct kernfs_node *parent,
886 const unsigned char *path,
887 const void *ns)
888{
792e0476 889 ssize_t len;
e56ed358 890 char *p, *name;
bd96f76a 891
393c3714 892 lockdep_assert_held_read(&kernfs_root(parent)->kernfs_rwsem);
bd96f76a 893
1a702dc8 894 spin_lock_irq(&kernfs_pr_cont_lock);
e56ed358 895
792e0476 896 len = strscpy(kernfs_pr_cont_buf, path, sizeof(kernfs_pr_cont_buf));
e56ed358 897
792e0476 898 if (len < 0) {
1a702dc8 899 spin_unlock_irq(&kernfs_pr_cont_lock);
bd96f76a 900 return NULL;
e56ed358
TH
901 }
902
903 p = kernfs_pr_cont_buf;
bd96f76a
TH
904
905 while ((name = strsep(&p, "/")) && parent) {
906 if (*name == '\0')
907 continue;
908 parent = kernfs_find_ns(parent, name, ns);
909 }
910
1a702dc8 911 spin_unlock_irq(&kernfs_pr_cont_lock);
e56ed358 912
bd96f76a
TH
913 return parent;
914}
915
fd7b9f7b 916/**
324a56e1
TH
917 * kernfs_find_and_get_ns - find and get kernfs_node with the given name
918 * @parent: kernfs_node to search under
fd7b9f7b
TH
919 * @name: name to look for
920 * @ns: the namespace tag to use
921 *
324a56e1 922 * Look for kernfs_node with name @name under @parent and get a reference
24b3e3dd
RD
923 * if found. This function may sleep.
924 *
925 * Return: pointer to the found kernfs_node on success, %NULL on failure.
fd7b9f7b 926 */
324a56e1
TH
927struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
928 const char *name, const void *ns)
fd7b9f7b 929{
324a56e1 930 struct kernfs_node *kn;
393c3714 931 struct kernfs_root *root = kernfs_root(parent);
fd7b9f7b 932
393c3714 933 down_read(&root->kernfs_rwsem);
324a56e1
TH
934 kn = kernfs_find_ns(parent, name, ns);
935 kernfs_get(kn);
393c3714 936 up_read(&root->kernfs_rwsem);
fd7b9f7b 937
324a56e1 938 return kn;
fd7b9f7b
TH
939}
940EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
941
bd96f76a
TH
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
24b3e3dd
RD
949 * if found. This function may sleep.
950 *
951 * Return: pointer to the found kernfs_node on success, %NULL on failure.
bd96f76a
TH
952 */
953struct kernfs_node *kernfs_walk_and_get_ns(struct kernfs_node *parent,
954 const char *path, const void *ns)
955{
956 struct kernfs_node *kn;
393c3714 957 struct kernfs_root *root = kernfs_root(parent);
bd96f76a 958
393c3714 959 down_read(&root->kernfs_rwsem);
bd96f76a
TH
960 kn = kernfs_walk_ns(parent, path, ns);
961 kernfs_get(kn);
393c3714 962 up_read(&root->kernfs_rwsem);
bd96f76a
TH
963
964 return kn;
965}
966
63348894
SAS
967unsigned int kernfs_root_flags(struct kernfs_node *kn)
968{
969 return kernfs_root(kn)->flags;
970}
971
ba7443bc
TH
972/**
973 * kernfs_create_root - create a new kernfs hierarchy
90c07c89 974 * @scops: optional syscall operations for the hierarchy
d35258ef 975 * @flags: KERNFS_ROOT_* flags
ba7443bc
TH
976 * @priv: opaque data associated with the new directory
977 *
24b3e3dd 978 * Return: the root of the new hierarchy on success, ERR_PTR() value on
ba7443bc
TH
979 * failure.
980 */
90c07c89 981struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
d35258ef 982 unsigned int flags, void *priv)
ba7443bc
TH
983{
984 struct kernfs_root *root;
324a56e1 985 struct kernfs_node *kn;
ba7443bc
TH
986
987 root = kzalloc(sizeof(*root), GFP_KERNEL);
988 if (!root)
989 return ERR_PTR(-ENOMEM);
990
7d35079f 991 idr_init(&root->ino_idr);
cec59c44 992 spin_lock_init(&root->kernfs_idr_lock);
393c3714 993 init_rwsem(&root->kernfs_rwsem);
9caf6961 994 init_rwsem(&root->kernfs_iattr_rwsem);
c9f2dfb7 995 init_rwsem(&root->kernfs_supers_rwsem);
7d568a83 996 INIT_LIST_HEAD(&root->supers);
93b27a84 997 rwlock_init(&root->kernfs_rename_lock);
40430452
TH
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;
bc755553 1009
e19dfdc8 1010 kn = __kernfs_new_node(root, NULL, "", S_IFDIR | S_IRUGO | S_IXUGO,
488dee96 1011 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
db4aad20 1012 KERNFS_DIR);
324a56e1 1013 if (!kn) {
7d35079f 1014 idr_destroy(&root->ino_idr);
ba7443bc
TH
1015 kfree(root);
1016 return ERR_PTR(-ENOMEM);
1017 }
1018
324a56e1 1019 kn->priv = priv;
adc5e8b5 1020 kn->dir.root = root;
ba7443bc 1021
90c07c89 1022 root->syscall_ops = scops;
d35258ef 1023 root->flags = flags;
324a56e1 1024 root->kn = kn;
abd54f02 1025 init_waitqueue_head(&root->deactivate_waitq);
ba7443bc 1026
d35258ef
TH
1027 if (!(root->flags & KERNFS_ROOT_CREATE_DEACTIVATED))
1028 kernfs_activate(kn);
1029
ba7443bc
TH
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 */
1040void kernfs_destroy_root(struct kernfs_root *root)
1041{
555a0ce4
MK
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 */
ba7443bc
TH
1049}
1050
f2eb478f
GKH
1051/**
1052 * kernfs_root_to_node - return the kernfs_node associated with a kernfs_root
1053 * @root: root to use to lookup
24b3e3dd
RD
1054 *
1055 * Return: @root's kernfs_node
f2eb478f
GKH
1056 */
1057struct kernfs_node *kernfs_root_to_node(struct kernfs_root *root)
1058{
1059 return root->kn;
1060}
1061
fd7b9f7b
TH
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
bb8b9d09 1066 * @mode: mode of the new directory
488dee96
DT
1067 * @uid: uid of the new directory
1068 * @gid: gid of the new directory
fd7b9f7b
TH
1069 * @priv: opaque data associated with the new directory
1070 * @ns: optional namespace tag of the directory
1071 *
24b3e3dd 1072 * Return: the created node on success, ERR_PTR() value on failure.
fd7b9f7b 1073 */
324a56e1 1074struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
bb8b9d09 1075 const char *name, umode_t mode,
488dee96 1076 kuid_t uid, kgid_t gid,
bb8b9d09 1077 void *priv, const void *ns)
fd7b9f7b 1078{
324a56e1 1079 struct kernfs_node *kn;
fd7b9f7b
TH
1080 int rc;
1081
1082 /* allocate */
488dee96
DT
1083 kn = kernfs_new_node(parent, name, mode | S_IFDIR,
1084 uid, gid, KERNFS_DIR);
324a56e1 1085 if (!kn)
fd7b9f7b
TH
1086 return ERR_PTR(-ENOMEM);
1087
adc5e8b5
TH
1088 kn->dir.root = parent->dir.root;
1089 kn->ns = ns;
324a56e1 1090 kn->priv = priv;
fd7b9f7b
TH
1091
1092 /* link in */
988cd7af 1093 rc = kernfs_add_one(kn);
fd7b9f7b 1094 if (!rc)
324a56e1 1095 return kn;
fd7b9f7b 1096
324a56e1 1097 kernfs_put(kn);
fd7b9f7b
TH
1098 return ERR_PTR(rc);
1099}
1100
ea015218
EB
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 *
24b3e3dd 1106 * Return: the created node on success, ERR_PTR() value on failure.
ea015218
EB
1107 */
1108struct 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 */
488dee96
DT
1115 kn = kernfs_new_node(parent, name, S_IRUGO|S_IXUGO|S_IFDIR,
1116 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, KERNFS_DIR);
ea015218
EB
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
5be1fa8a
AV
1134static int kernfs_dop_revalidate(struct inode *dir, const struct qstr *name,
1135 struct dentry *dentry, unsigned int flags)
d826e036 1136{
63348894 1137 struct kernfs_node *kn, *parent;
393c3714 1138 struct kernfs_root *root;
d826e036
IK
1139
1140 if (flags & LOOKUP_RCU)
1141 return -ECHILD;
1142
c7e7c042
IK
1143 /* Negative hashed dentry? */
1144 if (d_really_is_negative(dentry)) {
c7e7c042
IK
1145 /* If the kernfs parent node has changed discard and
1146 * proceed to ->lookup.
92b57842
IK
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.
c7e7c042 1160 */
92b57842
IK
1161 root = kernfs_root_from_sb(dentry->d_sb);
1162 down_read(&root->kernfs_rwsem);
c7e7c042
IK
1163 parent = kernfs_dentry_node(dentry->d_parent);
1164 if (parent) {
1165 if (kernfs_dir_changed(parent, dentry)) {
393c3714 1166 up_read(&root->kernfs_rwsem);
c7e7c042
IK
1167 return 0;
1168 }
92b57842
IK
1169 }
1170 up_read(&root->kernfs_rwsem);
c7e7c042
IK
1171
1172 /* The kernfs parent node hasn't changed, leave the
1173 * dentry negative and return success.
1174 */
1175 return 1;
1176 }
d826e036
IK
1177
1178 kn = kernfs_dentry_node(dentry);
393c3714
MK
1179 root = kernfs_root(kn);
1180 down_read(&root->kernfs_rwsem);
d826e036
IK
1181
1182 /* The kernfs node has been deactivated */
1183 if (!kernfs_active(kn))
1184 goto out_bad;
1185
63348894 1186 parent = kernfs_parent(kn);
d826e036 1187 /* The kernfs node has been moved? */
63348894 1188 if (kernfs_dentry_node(dentry->d_parent) != parent)
d826e036
IK
1189 goto out_bad;
1190
1191 /* The kernfs node has been renamed */
741c10b0 1192 if (strcmp(dentry->d_name.name, kernfs_rcu_name(kn)) != 0)
d826e036
IK
1193 goto out_bad;
1194
1195 /* The kernfs node has been moved to a different namespace */
63348894 1196 if (parent && kernfs_ns_enabled(parent) &&
d826e036
IK
1197 kernfs_info(dentry->d_sb)->ns != kn->ns)
1198 goto out_bad;
1199
393c3714 1200 up_read(&root->kernfs_rwsem);
d826e036
IK
1201 return 1;
1202out_bad:
393c3714 1203 up_read(&root->kernfs_rwsem);
d826e036
IK
1204 return 0;
1205}
1206
1207const struct dentry_operations kernfs_dops = {
1208 .d_revalidate = kernfs_dop_revalidate,
1209};
1210
c637b8ac
TH
1211static struct dentry *kernfs_iop_lookup(struct inode *dir,
1212 struct dentry *dentry,
1213 unsigned int flags)
fd7b9f7b 1214{
319ba91d 1215 struct kernfs_node *parent = dir->i_private;
324a56e1 1216 struct kernfs_node *kn;
393c3714 1217 struct kernfs_root *root;
c7e7c042 1218 struct inode *inode = NULL;
fd7b9f7b
TH
1219 const void *ns = NULL;
1220
393c3714
MK
1221 root = kernfs_root(parent);
1222 down_read(&root->kernfs_rwsem);
324a56e1 1223 if (kernfs_ns_enabled(parent))
c525aadd 1224 ns = kernfs_info(dir->i_sb)->ns;
fd7b9f7b 1225
324a56e1 1226 kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
fd7b9f7b 1227 /* attach dentry and inode */
410d591a
IK
1228 if (kn) {
1229 /* Inactive nodes are invisible to the VFS so don't
1230 * create a negative.
1231 */
1232 if (!kernfs_active(kn)) {
393c3714 1233 up_read(&root->kernfs_rwsem);
410d591a
IK
1234 return NULL;
1235 }
c7e7c042
IK
1236 inode = kernfs_get_inode(dir->i_sb, kn);
1237 if (!inode)
1238 inode = ERR_PTR(-ENOMEM);
fd7b9f7b 1239 }
df38d852
HT
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))
c7e7c042 1247 kernfs_set_rev(parent, dentry);
393c3714 1248 up_read(&root->kernfs_rwsem);
c7e7c042 1249
df6192f4
IK
1250 /* instantiate and hash (possibly negative) dentry */
1251 return d_splice_alias(inode, dentry);
fd7b9f7b
TH
1252}
1253
88d5baf6
N
1254static struct dentry *kernfs_iop_mkdir(struct mnt_idmap *idmap,
1255 struct inode *dir, struct dentry *dentry,
1256 umode_t mode)
80b9bbef
TH
1257{
1258 struct kernfs_node *parent = dir->i_private;
90c07c89 1259 struct kernfs_syscall_ops *scops = kernfs_root(parent)->syscall_ops;
07c7530d 1260 int ret;
80b9bbef 1261
90c07c89 1262 if (!scops || !scops->mkdir)
88d5baf6 1263 return ERR_PTR(-EPERM);
80b9bbef 1264
07c7530d 1265 if (!kernfs_get_active(parent))
88d5baf6 1266 return ERR_PTR(-ENODEV);
07c7530d 1267
90c07c89 1268 ret = scops->mkdir(parent, dentry->d_name.name, mode);
07c7530d
TH
1269
1270 kernfs_put_active(parent);
88d5baf6 1271 return ERR_PTR(ret);
80b9bbef
TH
1272}
1273
1274static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
1275{
319ba91d 1276 struct kernfs_node *kn = kernfs_dentry_node(dentry);
90c07c89 1277 struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops;
07c7530d 1278 int ret;
80b9bbef 1279
90c07c89 1280 if (!scops || !scops->rmdir)
80b9bbef
TH
1281 return -EPERM;
1282
07c7530d
TH
1283 if (!kernfs_get_active(kn))
1284 return -ENODEV;
1285
90c07c89 1286 ret = scops->rmdir(kn);
07c7530d
TH
1287
1288 kernfs_put_active(kn);
1289 return ret;
80b9bbef
TH
1290}
1291
e18275ae 1292static int kernfs_iop_rename(struct mnt_idmap *idmap,
549c7297 1293 struct inode *old_dir, struct dentry *old_dentry,
1cd66c93
MS
1294 struct inode *new_dir, struct dentry *new_dentry,
1295 unsigned int flags)
80b9bbef 1296{
319ba91d 1297 struct kernfs_node *kn = kernfs_dentry_node(old_dentry);
80b9bbef 1298 struct kernfs_node *new_parent = new_dir->i_private;
90c07c89 1299 struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops;
07c7530d 1300 int ret;
80b9bbef 1301
1cd66c93
MS
1302 if (flags)
1303 return -EINVAL;
1304
90c07c89 1305 if (!scops || !scops->rename)
80b9bbef
TH
1306 return -EPERM;
1307
07c7530d
TH
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
90c07c89 1316 ret = scops->rename(kn, new_parent, new_dentry->d_name.name);
07c7530d
TH
1317
1318 kernfs_put_active(new_parent);
1319 kernfs_put_active(kn);
1320 return ret;
80b9bbef
TH
1321}
1322
a797bfc3 1323const struct inode_operations kernfs_dir_iops = {
c637b8ac
TH
1324 .lookup = kernfs_iop_lookup,
1325 .permission = kernfs_iop_permission,
1326 .setattr = kernfs_iop_setattr,
1327 .getattr = kernfs_iop_getattr,
c637b8ac 1328 .listxattr = kernfs_iop_listxattr,
80b9bbef
TH
1329
1330 .mkdir = kernfs_iop_mkdir,
1331 .rmdir = kernfs_iop_rmdir,
1332 .rename = kernfs_iop_rename,
fd7b9f7b
TH
1333};
1334
c637b8ac 1335static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
fd7b9f7b 1336{
324a56e1 1337 struct kernfs_node *last;
fd7b9f7b
TH
1338
1339 while (true) {
1340 struct rb_node *rbn;
1341
1342 last = pos;
1343
df23fc39 1344 if (kernfs_type(pos) != KERNFS_DIR)
fd7b9f7b
TH
1345 break;
1346
adc5e8b5 1347 rbn = rb_first(&pos->dir.children);
fd7b9f7b
TH
1348 if (!rbn)
1349 break;
1350
324a56e1 1351 pos = rb_to_kn(rbn);
fd7b9f7b
TH
1352 }
1353
1354 return last;
1355}
1356
1357/**
c637b8ac 1358 * kernfs_next_descendant_post - find the next descendant for post-order walk
fd7b9f7b 1359 * @pos: the current position (%NULL to initiate traversal)
324a56e1 1360 * @root: kernfs_node whose descendants to walk
fd7b9f7b
TH
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.
24b3e3dd
RD
1365 *
1366 * Return: the next descendant to visit or %NULL when done.
fd7b9f7b 1367 */
c637b8ac
TH
1368static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
1369 struct kernfs_node *root)
fd7b9f7b
TH
1370{
1371 struct rb_node *rbn;
1372
393c3714 1373 lockdep_assert_held_write(&kernfs_root(root)->kernfs_rwsem);
fd7b9f7b
TH
1374
1375 /* if first iteration, visit leftmost descendant which may be root */
1376 if (!pos)
c637b8ac 1377 return kernfs_leftmost_descendant(root);
fd7b9f7b
TH
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 */
adc5e8b5 1384 rbn = rb_next(&pos->rb);
fd7b9f7b 1385 if (rbn)
c637b8ac 1386 return kernfs_leftmost_descendant(rb_to_kn(rbn));
fd7b9f7b
TH
1387
1388 /* no sibling left, visit parent */
63348894 1389 return kernfs_parent(pos);
fd7b9f7b
TH
1390}
1391
f8eb145e
TH
1392static 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
783bd07d 1398 if (kernfs_active(kn) || (kn->flags & (KERNFS_HIDDEN | KERNFS_REMOVING)))
f8eb145e
TH
1399 return;
1400
63348894 1401 WARN_ON_ONCE(rcu_access_pointer(kn->__parent) && RB_EMPTY_NODE(&kn->rb));
f8eb145e
TH
1402 WARN_ON_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS);
1403
1404 atomic_sub(KN_DEACTIVATED_BIAS, &kn->active);
1405}
1406
d35258ef
TH
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 */
1420void kernfs_activate(struct kernfs_node *kn)
1421{
1422 struct kernfs_node *pos;
393c3714 1423 struct kernfs_root *root = kernfs_root(kn);
d35258ef 1424
393c3714 1425 down_write(&root->kernfs_rwsem);
d35258ef
TH
1426
1427 pos = NULL;
f8eb145e
TH
1428 while ((pos = kernfs_next_descendant_post(pos, kn)))
1429 kernfs_activate_one(pos);
d35258ef 1430
393c3714 1431 up_write(&root->kernfs_rwsem);
d35258ef
TH
1432}
1433
783bd07d
TH
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 */
1446void 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
988cd7af 1469static void __kernfs_remove(struct kernfs_node *kn)
fd7b9f7b 1470{
63348894 1471 struct kernfs_node *pos, *parent;
35beab06 1472
72b5d5ae
YZ
1473 /* Short-circuit if non-root @kn has already finished removal. */
1474 if (!kn)
1475 return;
1476
393c3714 1477 lockdep_assert_held_write(&kernfs_root(kn)->kernfs_rwsem);
fd7b9f7b 1478
6b0afc2a 1479 /*
6b0afc2a
TH
1480 * This is for kernfs_remove_self() which plays with active ref
1481 * after removal.
1482 */
63348894 1483 if (kernfs_parent(kn) && RB_EMPTY_NODE(&kn->rb))
ce9b499c
GKH
1484 return;
1485
741c10b0 1486 pr_debug("kernfs %s: removing\n", kernfs_rcu_name(kn));
fd7b9f7b 1487
c2549174 1488 /* prevent new usage by marking all nodes removing and deactivating */
35beab06 1489 pos = NULL;
c2549174
TH
1490 while ((pos = kernfs_next_descendant_post(pos, kn))) {
1491 pos->flags |= KERNFS_REMOVING;
81c173cb
TH
1492 if (kernfs_active(pos))
1493 atomic_add(KN_DEACTIVATED_BIAS, &pos->active);
c2549174 1494 }
35beab06
TH
1495
1496 /* deactivate and unlink the subtree node-by-node */
fd7b9f7b 1497 do {
35beab06
TH
1498 pos = kernfs_leftmost_descendant(kn);
1499
1500 /*
2d7f9f8c 1501 * kernfs_drain() may drop kernfs_rwsem temporarily and @pos's
81c173cb
TH
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.
35beab06
TH
1505 */
1506 kernfs_get(pos);
1507
2d7f9f8c 1508 kernfs_drain(pos);
63348894 1509 parent = kernfs_parent(pos);
35beab06
TH
1510 /*
1511 * kernfs_unlink_sibling() succeeds once per node. Use it
1512 * to decide who's responsible for cleanups.
1513 */
63348894 1514 if (!parent || kernfs_unlink_sibling(pos)) {
35beab06 1515 struct kernfs_iattrs *ps_iattr =
63348894 1516 parent ? parent->iattr : NULL;
35beab06
TH
1517
1518 /* update timestamps on the parent */
9caf6961
IK
1519 down_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
1520
35beab06 1521 if (ps_iattr) {
05895219
OM
1522 ktime_get_real_ts64(&ps_iattr->ia_ctime);
1523 ps_iattr->ia_mtime = ps_iattr->ia_ctime;
35beab06
TH
1524 }
1525
9caf6961 1526 up_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
988cd7af 1527 kernfs_put(pos);
35beab06
TH
1528 }
1529
1530 kernfs_put(pos);
1531 } while (pos != kn);
fd7b9f7b
TH
1532}
1533
1534/**
324a56e1
TH
1535 * kernfs_remove - remove a kernfs_node recursively
1536 * @kn: the kernfs_node to remove
fd7b9f7b 1537 *
324a56e1 1538 * Remove @kn along with all its subdirectories and files.
fd7b9f7b 1539 */
324a56e1 1540void kernfs_remove(struct kernfs_node *kn)
fd7b9f7b 1541{
ad8d8693
MK
1542 struct kernfs_root *root;
1543
1544 if (!kn)
1545 return;
1546
1547 root = kernfs_root(kn);
393c3714
MK
1548
1549 down_write(&root->kernfs_rwsem);
988cd7af 1550 __kernfs_remove(kn);
393c3714 1551 up_write(&root->kernfs_rwsem);
fd7b9f7b
TH
1552}
1553
6b0afc2a
TH
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 */
1568void 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
071d8e4c
MK
1585 * being drained and removed. Once kernfs_break_active_protection() is
1586 * invoked, that protection is irreversibly gone for the kernfs operation
1587 * instance.
6b0afc2a
TH
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 */
1593void 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.
24b3e3dd
RD
1634 *
1635 * Return: %true if @kn is removed by this call, otherwise %false.
6b0afc2a
TH
1636 */
1637bool kernfs_remove_self(struct kernfs_node *kn)
1638{
1639 bool ret;
393c3714 1640 struct kernfs_root *root = kernfs_root(kn);
6b0afc2a 1641
393c3714 1642 down_write(&root->kernfs_rwsem);
6b0afc2a
TH
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
7ba0273b
IK
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.
6b0afc2a
TH
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
393c3714 1670 up_write(&root->kernfs_rwsem);
6b0afc2a 1671 schedule();
393c3714 1672 down_write(&root->kernfs_rwsem);
6b0afc2a
TH
1673 }
1674 finish_wait(waitq, &wait);
1675 WARN_ON_ONCE(!RB_EMPTY_NODE(&kn->rb));
1676 ret = false;
1677 }
1678
1679 /*
7ba0273b
IK
1680 * This must be done while kernfs_rwsem held exclusive; otherwise,
1681 * waiting for SUICIDED && deactivated could finish prematurely.
6b0afc2a
TH
1682 */
1683 kernfs_unbreak_active_protection(kn);
1684
393c3714 1685 up_write(&root->kernfs_rwsem);
6b0afc2a
TH
1686 return ret;
1687}
1688
fd7b9f7b 1689/**
324a56e1
TH
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
fd7b9f7b 1694 *
324a56e1 1695 * Look for the kernfs_node with @name and @ns under @parent and remove it.
24b3e3dd
RD
1696 *
1697 * Return: %0 on success, -ENOENT if such entry doesn't exist.
fd7b9f7b 1698 */
324a56e1 1699int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
fd7b9f7b
TH
1700 const void *ns)
1701{
324a56e1 1702 struct kernfs_node *kn;
393c3714 1703 struct kernfs_root *root;
fd7b9f7b 1704
324a56e1 1705 if (!parent) {
c637b8ac 1706 WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
fd7b9f7b
TH
1707 name);
1708 return -ENOENT;
1709 }
1710
393c3714
MK
1711 root = kernfs_root(parent);
1712 down_write(&root->kernfs_rwsem);
fd7b9f7b 1713
324a56e1 1714 kn = kernfs_find_ns(parent, name, ns);
4abc9965
CE
1715 if (kn) {
1716 kernfs_get(kn);
988cd7af 1717 __kernfs_remove(kn);
4abc9965
CE
1718 kernfs_put(kn);
1719 }
fd7b9f7b 1720
393c3714 1721 up_write(&root->kernfs_rwsem);
fd7b9f7b 1722
324a56e1 1723 if (kn)
fd7b9f7b
TH
1724 return 0;
1725 else
1726 return -ENOENT;
1727}
1728
1729/**
1730 * kernfs_rename_ns - move and rename a kernfs_node
324a56e1 1731 * @kn: target node
fd7b9f7b
TH
1732 * @new_parent: new parent to put @sd under
1733 * @new_name: new name
1734 * @new_ns: new namespace tag
24b3e3dd
RD
1735 *
1736 * Return: %0 on success, -errno on failure.
fd7b9f7b 1737 */
324a56e1 1738int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
fd7b9f7b
TH
1739 const char *new_name, const void *new_ns)
1740{
3eef34ad 1741 struct kernfs_node *old_parent;
393c3714 1742 struct kernfs_root *root;
741c10b0 1743 const char *old_name;
fd7b9f7b
TH
1744 int error;
1745
3eef34ad 1746 /* can't move or rename root */
63348894 1747 if (!rcu_access_pointer(kn->__parent))
3eef34ad
TH
1748 return -EINVAL;
1749
393c3714
MK
1750 root = kernfs_root(kn);
1751 down_write(&root->kernfs_rwsem);
798c75a0 1752
d0ae3d43 1753 error = -ENOENT;
ea015218
EB
1754 if (!kernfs_active(kn) || !kernfs_active(new_parent) ||
1755 (new_parent->flags & KERNFS_EMPTY_DIR))
d0ae3d43
TH
1756 goto out;
1757
63348894
SAS
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
fd7b9f7b 1765 error = 0;
741c10b0
SAS
1766 old_name = kernfs_rcu_name(kn);
1767 if (!new_name)
1768 new_name = old_name;
63348894 1769 if ((old_parent == new_parent) && (kn->ns == new_ns) &&
741c10b0 1770 (strcmp(old_name, new_name) == 0))
798c75a0 1771 goto out; /* nothing to rename */
fd7b9f7b
TH
1772
1773 error = -EEXIST;
1774 if (kernfs_find_ns(new_parent, new_name, new_ns))
798c75a0 1775 goto out;
fd7b9f7b 1776
324a56e1 1777 /* rename kernfs_node */
741c10b0 1778 if (strcmp(old_name, new_name) != 0) {
fd7b9f7b 1779 error = -ENOMEM;
75287a67 1780 new_name = kstrdup_const(new_name, GFP_KERNEL);
fd7b9f7b 1781 if (!new_name)
798c75a0 1782 goto out;
3eef34ad
TH
1783 } else {
1784 new_name = NULL;
fd7b9f7b
TH
1785 }
1786
1787 /*
1788 * Move to the appropriate place in the appropriate directories rbtree.
1789 */
c637b8ac 1790 kernfs_unlink_sibling(kn);
3eef34ad 1791
741c10b0
SAS
1792 /* rename_lock protects ->parent accessors */
1793 if (old_parent != new_parent) {
1794 kernfs_get(new_parent);
93b27a84 1795 write_lock_irq(&root->kernfs_rename_lock);
3eef34ad 1796
741c10b0 1797 rcu_assign_pointer(kn->__parent, new_parent);
3eef34ad 1798
741c10b0
SAS
1799 kn->ns = new_ns;
1800 if (new_name)
1801 rcu_assign_pointer(kn->name, new_name);
3eef34ad 1802
93b27a84 1803 write_unlock_irq(&root->kernfs_rename_lock);
741c10b0
SAS
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 }
3eef34ad 1811
741c10b0 1812 kn->hash = kernfs_name_hash(new_name ?: old_name, kn->ns);
c637b8ac 1813 kernfs_link_sibling(kn);
fd7b9f7b 1814
741c10b0
SAS
1815 if (new_name && !is_kernel_rodata((unsigned long)old_name))
1816 kfree_rcu_mightsleep(old_name);
3eef34ad 1817
fd7b9f7b 1818 error = 0;
798c75a0 1819 out:
393c3714 1820 up_write(&root->kernfs_rwsem);
fd7b9f7b
TH
1821 return error;
1822}
1823
c637b8ac 1824static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
fd7b9f7b
TH
1825{
1826 kernfs_put(filp->private_data);
1827 return 0;
1828}
1829
c637b8ac 1830static struct kernfs_node *kernfs_dir_pos(const void *ns,
324a56e1 1831 struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
fd7b9f7b
TH
1832{
1833 if (pos) {
81c173cb 1834 int valid = kernfs_active(pos) &&
63348894
SAS
1835 rcu_access_pointer(pos->__parent) == parent &&
1836 hash == pos->hash;
fd7b9f7b
TH
1837 kernfs_put(pos);
1838 if (!valid)
1839 pos = NULL;
1840 }
1841 if (!pos && (hash > 1) && (hash < INT_MAX)) {
adc5e8b5 1842 struct rb_node *node = parent->dir.children.rb_node;
fd7b9f7b 1843 while (node) {
324a56e1 1844 pos = rb_to_kn(node);
fd7b9f7b 1845
adc5e8b5 1846 if (hash < pos->hash)
fd7b9f7b 1847 node = node->rb_left;
adc5e8b5 1848 else if (hash > pos->hash)
fd7b9f7b
TH
1849 node = node->rb_right;
1850 else
1851 break;
1852 }
1853 }
b9c9dad0
TH
1854 /* Skip over entries which are dying/dead or in the wrong namespace */
1855 while (pos && (!kernfs_active(pos) || pos->ns != ns)) {
adc5e8b5 1856 struct rb_node *node = rb_next(&pos->rb);
fd7b9f7b
TH
1857 if (!node)
1858 pos = NULL;
1859 else
324a56e1 1860 pos = rb_to_kn(node);
fd7b9f7b
TH
1861 }
1862 return pos;
1863}
1864
c637b8ac 1865static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
324a56e1 1866 struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
fd7b9f7b 1867{
c637b8ac 1868 pos = kernfs_dir_pos(ns, parent, ino, pos);
b9c9dad0 1869 if (pos) {
fd7b9f7b 1870 do {
adc5e8b5 1871 struct rb_node *node = rb_next(&pos->rb);
fd7b9f7b
TH
1872 if (!node)
1873 pos = NULL;
1874 else
324a56e1 1875 pos = rb_to_kn(node);
b9c9dad0
TH
1876 } while (pos && (!kernfs_active(pos) || pos->ns != ns));
1877 }
fd7b9f7b
TH
1878 return pos;
1879}
1880
c637b8ac 1881static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
fd7b9f7b
TH
1882{
1883 struct dentry *dentry = file->f_path.dentry;
319ba91d 1884 struct kernfs_node *parent = kernfs_dentry_node(dentry);
324a56e1 1885 struct kernfs_node *pos = file->private_data;
393c3714 1886 struct kernfs_root *root;
fd7b9f7b
TH
1887 const void *ns = NULL;
1888
1889 if (!dir_emit_dots(file, ctx))
1890 return 0;
393c3714
MK
1891
1892 root = kernfs_root(parent);
1893 down_read(&root->kernfs_rwsem);
fd7b9f7b 1894
324a56e1 1895 if (kernfs_ns_enabled(parent))
c525aadd 1896 ns = kernfs_info(dentry->d_sb)->ns;
fd7b9f7b 1897
c637b8ac 1898 for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos);
fd7b9f7b 1899 pos;
c637b8ac 1900 pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
741c10b0 1901 const char *name = kernfs_rcu_name(pos);
364595a6 1902 unsigned int type = fs_umode_to_dtype(pos->mode);
fd7b9f7b 1903 int len = strlen(name);
67c0496e 1904 ino_t ino = kernfs_ino(pos);
fd7b9f7b 1905
adc5e8b5 1906 ctx->pos = pos->hash;
fd7b9f7b
TH
1907 file->private_data = pos;
1908 kernfs_get(pos);
1909
9aab10a0
SAS
1910 if (!dir_emit(ctx, name, len, ino, type)) {
1911 up_read(&root->kernfs_rwsem);
fd7b9f7b 1912 return 0;
9aab10a0 1913 }
fd7b9f7b 1914 }
393c3714 1915 up_read(&root->kernfs_rwsem);
fd7b9f7b
TH
1916 file->private_data = NULL;
1917 ctx->pos = INT_MAX;
1918 return 0;
1919}
1920
a797bfc3 1921const struct file_operations kernfs_dir_fops = {
fd7b9f7b 1922 .read = generic_read_dir,
8cb0d2c1 1923 .iterate_shared = kernfs_fop_readdir,
c637b8ac 1924 .release = kernfs_dir_fop_release,
8cb0d2c1 1925 .llseek = generic_file_llseek,
fd7b9f7b 1926};