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