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