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