kernfs: implement kernfs_syscall_ops->remount_fs() and ->show_options()
[deliverable/linux.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
db4aad20
TH
353static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
354 const char *name, umode_t mode,
355 unsigned flags)
fd7b9f7b
TH
356{
357 char *dup_name = NULL;
324a56e1 358 struct kernfs_node *kn;
bc755553 359 int ret;
fd7b9f7b 360
2063d608 361 if (!(flags & KERNFS_STATIC_NAME)) {
fd7b9f7b
TH
362 name = dup_name = kstrdup(name, GFP_KERNEL);
363 if (!name)
364 return NULL;
365 }
366
a797bfc3 367 kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL);
324a56e1 368 if (!kn)
fd7b9f7b
TH
369 goto err_out1;
370
bc755553
TH
371 ret = ida_simple_get(&root->ino_ida, 1, 0, GFP_KERNEL);
372 if (ret < 0)
fd7b9f7b 373 goto err_out2;
adc5e8b5 374 kn->ino = ret;
fd7b9f7b 375
adc5e8b5 376 atomic_set(&kn->count, 1);
81c173cb 377 atomic_set(&kn->active, KN_DEACTIVATED_BIAS);
35beab06 378 RB_CLEAR_NODE(&kn->rb);
fd7b9f7b 379
adc5e8b5
TH
380 kn->name = name;
381 kn->mode = mode;
81c173cb 382 kn->flags = flags;
fd7b9f7b 383
324a56e1 384 return kn;
fd7b9f7b
TH
385
386 err_out2:
a797bfc3 387 kmem_cache_free(kernfs_node_cache, kn);
fd7b9f7b
TH
388 err_out1:
389 kfree(dup_name);
390 return NULL;
391}
392
db4aad20
TH
393struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
394 const char *name, umode_t mode,
395 unsigned flags)
396{
397 struct kernfs_node *kn;
398
399 kn = __kernfs_new_node(kernfs_root(parent), name, mode, flags);
400 if (kn) {
401 kernfs_get(parent);
402 kn->parent = parent;
403 }
404 return kn;
405}
406
fd7b9f7b 407/**
c637b8ac 408 * kernfs_add_one - add kernfs_node to parent without warning
324a56e1 409 * @kn: kernfs_node to be added
fd7b9f7b 410 *
db4aad20
TH
411 * The caller must already have initialized @kn->parent. This
412 * function increments nlink of the parent's inode if @kn is a
413 * directory and link into the children list of the parent.
fd7b9f7b 414 *
fd7b9f7b
TH
415 * RETURNS:
416 * 0 on success, -EEXIST if entry with the given name already
417 * exists.
418 */
988cd7af 419int kernfs_add_one(struct kernfs_node *kn)
fd7b9f7b 420{
db4aad20 421 struct kernfs_node *parent = kn->parent;
c525aadd 422 struct kernfs_iattrs *ps_iattr;
988cd7af 423 bool has_ns;
fd7b9f7b
TH
424 int ret;
425
988cd7af
TH
426 mutex_lock(&kernfs_mutex);
427
428 ret = -EINVAL;
429 has_ns = kernfs_ns_enabled(parent);
430 if (WARN(has_ns != (bool)kn->ns, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
431 has_ns ? "required" : "invalid", parent->name, kn->name))
432 goto out_unlock;
fd7b9f7b 433
df23fc39 434 if (kernfs_type(parent) != KERNFS_DIR)
988cd7af 435 goto out_unlock;
fd7b9f7b 436
988cd7af 437 ret = -ENOENT;
81c173cb 438 if (!kernfs_active(parent))
988cd7af 439 goto out_unlock;
798c75a0 440
c637b8ac 441 kn->hash = kernfs_name_hash(kn->name, kn->ns);
fd7b9f7b 442
c637b8ac 443 ret = kernfs_link_sibling(kn);
fd7b9f7b 444 if (ret)
988cd7af 445 goto out_unlock;
fd7b9f7b
TH
446
447 /* Update timestamps on the parent */
adc5e8b5 448 ps_iattr = parent->iattr;
fd7b9f7b
TH
449 if (ps_iattr) {
450 struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
451 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
452 }
453
454 /* Mark the entry added into directory tree */
81c173cb 455 atomic_sub(KN_DEACTIVATED_BIAS, &kn->active);
988cd7af
TH
456 ret = 0;
457out_unlock:
a797bfc3 458 mutex_unlock(&kernfs_mutex);
988cd7af 459 return ret;
fd7b9f7b
TH
460}
461
462/**
324a56e1
TH
463 * kernfs_find_ns - find kernfs_node with the given name
464 * @parent: kernfs_node to search under
fd7b9f7b
TH
465 * @name: name to look for
466 * @ns: the namespace tag to use
467 *
324a56e1
TH
468 * Look for kernfs_node with name @name under @parent. Returns pointer to
469 * the found kernfs_node on success, %NULL on failure.
fd7b9f7b 470 */
324a56e1
TH
471static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
472 const unsigned char *name,
473 const void *ns)
fd7b9f7b 474{
adc5e8b5 475 struct rb_node *node = parent->dir.children.rb_node;
ac9bba03 476 bool has_ns = kernfs_ns_enabled(parent);
fd7b9f7b
TH
477 unsigned int hash;
478
a797bfc3 479 lockdep_assert_held(&kernfs_mutex);
fd7b9f7b
TH
480
481 if (has_ns != (bool)ns) {
c637b8ac 482 WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
adc5e8b5 483 has_ns ? "required" : "invalid", parent->name, name);
fd7b9f7b
TH
484 return NULL;
485 }
486
c637b8ac 487 hash = kernfs_name_hash(name, ns);
fd7b9f7b 488 while (node) {
324a56e1 489 struct kernfs_node *kn;
fd7b9f7b
TH
490 int result;
491
324a56e1 492 kn = rb_to_kn(node);
c637b8ac 493 result = kernfs_name_compare(hash, name, ns, kn);
fd7b9f7b
TH
494 if (result < 0)
495 node = node->rb_left;
496 else if (result > 0)
497 node = node->rb_right;
498 else
324a56e1 499 return kn;
fd7b9f7b
TH
500 }
501 return NULL;
502}
503
504/**
324a56e1
TH
505 * kernfs_find_and_get_ns - find and get kernfs_node with the given name
506 * @parent: kernfs_node to search under
fd7b9f7b
TH
507 * @name: name to look for
508 * @ns: the namespace tag to use
509 *
324a56e1 510 * Look for kernfs_node with name @name under @parent and get a reference
fd7b9f7b 511 * if found. This function may sleep and returns pointer to the found
324a56e1 512 * kernfs_node on success, %NULL on failure.
fd7b9f7b 513 */
324a56e1
TH
514struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
515 const char *name, const void *ns)
fd7b9f7b 516{
324a56e1 517 struct kernfs_node *kn;
fd7b9f7b 518
a797bfc3 519 mutex_lock(&kernfs_mutex);
324a56e1
TH
520 kn = kernfs_find_ns(parent, name, ns);
521 kernfs_get(kn);
a797bfc3 522 mutex_unlock(&kernfs_mutex);
fd7b9f7b 523
324a56e1 524 return kn;
fd7b9f7b
TH
525}
526EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
527
ba7443bc
TH
528/**
529 * kernfs_create_root - create a new kernfs hierarchy
90c07c89 530 * @scops: optional syscall operations for the hierarchy
ba7443bc
TH
531 * @priv: opaque data associated with the new directory
532 *
533 * Returns the root of the new hierarchy on success, ERR_PTR() value on
534 * failure.
535 */
90c07c89
TH
536struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
537 void *priv)
ba7443bc
TH
538{
539 struct kernfs_root *root;
324a56e1 540 struct kernfs_node *kn;
ba7443bc
TH
541
542 root = kzalloc(sizeof(*root), GFP_KERNEL);
543 if (!root)
544 return ERR_PTR(-ENOMEM);
545
bc755553
TH
546 ida_init(&root->ino_ida);
547
db4aad20
TH
548 kn = __kernfs_new_node(root, "", S_IFDIR | S_IRUGO | S_IXUGO,
549 KERNFS_DIR);
324a56e1 550 if (!kn) {
bc755553 551 ida_destroy(&root->ino_ida);
ba7443bc
TH
552 kfree(root);
553 return ERR_PTR(-ENOMEM);
554 }
555
81c173cb 556 atomic_sub(KN_DEACTIVATED_BIAS, &kn->active);
324a56e1 557 kn->priv = priv;
adc5e8b5 558 kn->dir.root = root;
ba7443bc 559
90c07c89 560 root->syscall_ops = scops;
324a56e1 561 root->kn = kn;
abd54f02 562 init_waitqueue_head(&root->deactivate_waitq);
ba7443bc
TH
563
564 return root;
565}
566
567/**
568 * kernfs_destroy_root - destroy a kernfs hierarchy
569 * @root: root of the hierarchy to destroy
570 *
571 * Destroy the hierarchy anchored at @root by removing all existing
572 * directories and destroying @root.
573 */
574void kernfs_destroy_root(struct kernfs_root *root)
575{
324a56e1 576 kernfs_remove(root->kn); /* will also free @root */
ba7443bc
TH
577}
578
fd7b9f7b
TH
579/**
580 * kernfs_create_dir_ns - create a directory
581 * @parent: parent in which to create a new directory
582 * @name: name of the new directory
bb8b9d09 583 * @mode: mode of the new directory
fd7b9f7b
TH
584 * @priv: opaque data associated with the new directory
585 * @ns: optional namespace tag of the directory
586 *
587 * Returns the created node on success, ERR_PTR() value on failure.
588 */
324a56e1 589struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
bb8b9d09
TH
590 const char *name, umode_t mode,
591 void *priv, const void *ns)
fd7b9f7b 592{
324a56e1 593 struct kernfs_node *kn;
fd7b9f7b
TH
594 int rc;
595
596 /* allocate */
db4aad20 597 kn = kernfs_new_node(parent, name, mode | S_IFDIR, KERNFS_DIR);
324a56e1 598 if (!kn)
fd7b9f7b
TH
599 return ERR_PTR(-ENOMEM);
600
adc5e8b5
TH
601 kn->dir.root = parent->dir.root;
602 kn->ns = ns;
324a56e1 603 kn->priv = priv;
fd7b9f7b
TH
604
605 /* link in */
988cd7af 606 rc = kernfs_add_one(kn);
fd7b9f7b 607 if (!rc)
324a56e1 608 return kn;
fd7b9f7b 609
324a56e1 610 kernfs_put(kn);
fd7b9f7b
TH
611 return ERR_PTR(rc);
612}
613
c637b8ac
TH
614static struct dentry *kernfs_iop_lookup(struct inode *dir,
615 struct dentry *dentry,
616 unsigned int flags)
fd7b9f7b 617{
19bbb926 618 struct dentry *ret;
324a56e1
TH
619 struct kernfs_node *parent = dentry->d_parent->d_fsdata;
620 struct kernfs_node *kn;
fd7b9f7b
TH
621 struct inode *inode;
622 const void *ns = NULL;
623
a797bfc3 624 mutex_lock(&kernfs_mutex);
fd7b9f7b 625
324a56e1 626 if (kernfs_ns_enabled(parent))
c525aadd 627 ns = kernfs_info(dir->i_sb)->ns;
fd7b9f7b 628
324a56e1 629 kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
fd7b9f7b
TH
630
631 /* no such entry */
324a56e1 632 if (!kn) {
19bbb926 633 ret = NULL;
fd7b9f7b
TH
634 goto out_unlock;
635 }
324a56e1
TH
636 kernfs_get(kn);
637 dentry->d_fsdata = kn;
fd7b9f7b
TH
638
639 /* attach dentry and inode */
c637b8ac 640 inode = kernfs_get_inode(dir->i_sb, kn);
fd7b9f7b
TH
641 if (!inode) {
642 ret = ERR_PTR(-ENOMEM);
643 goto out_unlock;
644 }
645
646 /* instantiate and hash dentry */
647 ret = d_materialise_unique(dentry, inode);
648 out_unlock:
a797bfc3 649 mutex_unlock(&kernfs_mutex);
fd7b9f7b
TH
650 return ret;
651}
652
80b9bbef
TH
653static int kernfs_iop_mkdir(struct inode *dir, struct dentry *dentry,
654 umode_t mode)
655{
656 struct kernfs_node *parent = dir->i_private;
90c07c89 657 struct kernfs_syscall_ops *scops = kernfs_root(parent)->syscall_ops;
07c7530d 658 int ret;
80b9bbef 659
90c07c89 660 if (!scops || !scops->mkdir)
80b9bbef
TH
661 return -EPERM;
662
07c7530d
TH
663 if (!kernfs_get_active(parent))
664 return -ENODEV;
665
90c07c89 666 ret = scops->mkdir(parent, dentry->d_name.name, mode);
07c7530d
TH
667
668 kernfs_put_active(parent);
669 return ret;
80b9bbef
TH
670}
671
672static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
673{
674 struct kernfs_node *kn = dentry->d_fsdata;
90c07c89 675 struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops;
07c7530d 676 int ret;
80b9bbef 677
90c07c89 678 if (!scops || !scops->rmdir)
80b9bbef
TH
679 return -EPERM;
680
07c7530d
TH
681 if (!kernfs_get_active(kn))
682 return -ENODEV;
683
90c07c89 684 ret = scops->rmdir(kn);
07c7530d
TH
685
686 kernfs_put_active(kn);
687 return ret;
80b9bbef
TH
688}
689
690static int kernfs_iop_rename(struct inode *old_dir, struct dentry *old_dentry,
691 struct inode *new_dir, struct dentry *new_dentry)
692{
693 struct kernfs_node *kn = old_dentry->d_fsdata;
694 struct kernfs_node *new_parent = new_dir->i_private;
90c07c89 695 struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops;
07c7530d 696 int ret;
80b9bbef 697
90c07c89 698 if (!scops || !scops->rename)
80b9bbef
TH
699 return -EPERM;
700
07c7530d
TH
701 if (!kernfs_get_active(kn))
702 return -ENODEV;
703
704 if (!kernfs_get_active(new_parent)) {
705 kernfs_put_active(kn);
706 return -ENODEV;
707 }
708
90c07c89 709 ret = scops->rename(kn, new_parent, new_dentry->d_name.name);
07c7530d
TH
710
711 kernfs_put_active(new_parent);
712 kernfs_put_active(kn);
713 return ret;
80b9bbef
TH
714}
715
a797bfc3 716const struct inode_operations kernfs_dir_iops = {
c637b8ac
TH
717 .lookup = kernfs_iop_lookup,
718 .permission = kernfs_iop_permission,
719 .setattr = kernfs_iop_setattr,
720 .getattr = kernfs_iop_getattr,
721 .setxattr = kernfs_iop_setxattr,
722 .removexattr = kernfs_iop_removexattr,
723 .getxattr = kernfs_iop_getxattr,
724 .listxattr = kernfs_iop_listxattr,
80b9bbef
TH
725
726 .mkdir = kernfs_iop_mkdir,
727 .rmdir = kernfs_iop_rmdir,
728 .rename = kernfs_iop_rename,
fd7b9f7b
TH
729};
730
c637b8ac 731static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
fd7b9f7b 732{
324a56e1 733 struct kernfs_node *last;
fd7b9f7b
TH
734
735 while (true) {
736 struct rb_node *rbn;
737
738 last = pos;
739
df23fc39 740 if (kernfs_type(pos) != KERNFS_DIR)
fd7b9f7b
TH
741 break;
742
adc5e8b5 743 rbn = rb_first(&pos->dir.children);
fd7b9f7b
TH
744 if (!rbn)
745 break;
746
324a56e1 747 pos = rb_to_kn(rbn);
fd7b9f7b
TH
748 }
749
750 return last;
751}
752
753/**
c637b8ac 754 * kernfs_next_descendant_post - find the next descendant for post-order walk
fd7b9f7b 755 * @pos: the current position (%NULL to initiate traversal)
324a56e1 756 * @root: kernfs_node whose descendants to walk
fd7b9f7b
TH
757 *
758 * Find the next descendant to visit for post-order traversal of @root's
759 * descendants. @root is included in the iteration and the last node to be
760 * visited.
761 */
c637b8ac
TH
762static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
763 struct kernfs_node *root)
fd7b9f7b
TH
764{
765 struct rb_node *rbn;
766
a797bfc3 767 lockdep_assert_held(&kernfs_mutex);
fd7b9f7b
TH
768
769 /* if first iteration, visit leftmost descendant which may be root */
770 if (!pos)
c637b8ac 771 return kernfs_leftmost_descendant(root);
fd7b9f7b
TH
772
773 /* if we visited @root, we're done */
774 if (pos == root)
775 return NULL;
776
777 /* if there's an unvisited sibling, visit its leftmost descendant */
adc5e8b5 778 rbn = rb_next(&pos->rb);
fd7b9f7b 779 if (rbn)
c637b8ac 780 return kernfs_leftmost_descendant(rb_to_kn(rbn));
fd7b9f7b
TH
781
782 /* no sibling left, visit parent */
adc5e8b5 783 return pos->parent;
fd7b9f7b
TH
784}
785
988cd7af 786static void __kernfs_remove(struct kernfs_node *kn)
fd7b9f7b 787{
35beab06
TH
788 struct kernfs_node *pos;
789
790 lockdep_assert_held(&kernfs_mutex);
fd7b9f7b 791
6b0afc2a
TH
792 /*
793 * Short-circuit if non-root @kn has already finished removal.
794 * This is for kernfs_remove_self() which plays with active ref
795 * after removal.
796 */
797 if (!kn || (kn->parent && RB_EMPTY_NODE(&kn->rb)))
ce9b499c
GKH
798 return;
799
c637b8ac 800 pr_debug("kernfs %s: removing\n", kn->name);
fd7b9f7b 801
81c173cb 802 /* prevent any new usage under @kn by deactivating all nodes */
35beab06
TH
803 pos = NULL;
804 while ((pos = kernfs_next_descendant_post(pos, kn)))
81c173cb
TH
805 if (kernfs_active(pos))
806 atomic_add(KN_DEACTIVATED_BIAS, &pos->active);
35beab06
TH
807
808 /* deactivate and unlink the subtree node-by-node */
fd7b9f7b 809 do {
35beab06
TH
810 pos = kernfs_leftmost_descendant(kn);
811
812 /*
81c173cb
TH
813 * kernfs_drain() drops kernfs_mutex temporarily and @pos's
814 * base ref could have been put by someone else by the time
815 * the function returns. Make sure it doesn't go away
816 * underneath us.
35beab06
TH
817 */
818 kernfs_get(pos);
819
81c173cb 820 kernfs_drain(pos);
35beab06
TH
821
822 /*
823 * kernfs_unlink_sibling() succeeds once per node. Use it
824 * to decide who's responsible for cleanups.
825 */
826 if (!pos->parent || kernfs_unlink_sibling(pos)) {
827 struct kernfs_iattrs *ps_iattr =
828 pos->parent ? pos->parent->iattr : NULL;
829
830 /* update timestamps on the parent */
831 if (ps_iattr) {
832 ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME;
833 ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME;
834 }
835
988cd7af 836 kernfs_put(pos);
35beab06
TH
837 }
838
839 kernfs_put(pos);
840 } while (pos != kn);
fd7b9f7b
TH
841}
842
843/**
324a56e1
TH
844 * kernfs_remove - remove a kernfs_node recursively
845 * @kn: the kernfs_node to remove
fd7b9f7b 846 *
324a56e1 847 * Remove @kn along with all its subdirectories and files.
fd7b9f7b 848 */
324a56e1 849void kernfs_remove(struct kernfs_node *kn)
fd7b9f7b 850{
988cd7af
TH
851 mutex_lock(&kernfs_mutex);
852 __kernfs_remove(kn);
853 mutex_unlock(&kernfs_mutex);
fd7b9f7b
TH
854}
855
6b0afc2a
TH
856/**
857 * kernfs_break_active_protection - break out of active protection
858 * @kn: the self kernfs_node
859 *
860 * The caller must be running off of a kernfs operation which is invoked
861 * with an active reference - e.g. one of kernfs_ops. Each invocation of
862 * this function must also be matched with an invocation of
863 * kernfs_unbreak_active_protection().
864 *
865 * This function releases the active reference of @kn the caller is
866 * holding. Once this function is called, @kn may be removed at any point
867 * and the caller is solely responsible for ensuring that the objects it
868 * dereferences are accessible.
869 */
870void kernfs_break_active_protection(struct kernfs_node *kn)
871{
872 /*
873 * Take out ourself out of the active ref dependency chain. If
874 * we're called without an active ref, lockdep will complain.
875 */
876 kernfs_put_active(kn);
877}
878
879/**
880 * kernfs_unbreak_active_protection - undo kernfs_break_active_protection()
881 * @kn: the self kernfs_node
882 *
883 * If kernfs_break_active_protection() was called, this function must be
884 * invoked before finishing the kernfs operation. Note that while this
885 * function restores the active reference, it doesn't and can't actually
886 * restore the active protection - @kn may already or be in the process of
887 * being removed. Once kernfs_break_active_protection() is invoked, that
888 * protection is irreversibly gone for the kernfs operation instance.
889 *
890 * While this function may be called at any point after
891 * kernfs_break_active_protection() is invoked, its most useful location
892 * would be right before the enclosing kernfs operation returns.
893 */
894void kernfs_unbreak_active_protection(struct kernfs_node *kn)
895{
896 /*
897 * @kn->active could be in any state; however, the increment we do
898 * here will be undone as soon as the enclosing kernfs operation
899 * finishes and this temporary bump can't break anything. If @kn
900 * is alive, nothing changes. If @kn is being deactivated, the
901 * soon-to-follow put will either finish deactivation or restore
902 * deactivated state. If @kn is already removed, the temporary
903 * bump is guaranteed to be gone before @kn is released.
904 */
905 atomic_inc(&kn->active);
906 if (kernfs_lockdep(kn))
907 rwsem_acquire(&kn->dep_map, 0, 1, _RET_IP_);
908}
909
910/**
911 * kernfs_remove_self - remove a kernfs_node from its own method
912 * @kn: the self kernfs_node to remove
913 *
914 * The caller must be running off of a kernfs operation which is invoked
915 * with an active reference - e.g. one of kernfs_ops. This can be used to
916 * implement a file operation which deletes itself.
917 *
918 * For example, the "delete" file for a sysfs device directory can be
919 * implemented by invoking kernfs_remove_self() on the "delete" file
920 * itself. This function breaks the circular dependency of trying to
921 * deactivate self while holding an active ref itself. It isn't necessary
922 * to modify the usual removal path to use kernfs_remove_self(). The
923 * "delete" implementation can simply invoke kernfs_remove_self() on self
924 * before proceeding with the usual removal path. kernfs will ignore later
925 * kernfs_remove() on self.
926 *
927 * kernfs_remove_self() can be called multiple times concurrently on the
928 * same kernfs_node. Only the first one actually performs removal and
929 * returns %true. All others will wait until the kernfs operation which
930 * won self-removal finishes and return %false. Note that the losers wait
931 * for the completion of not only the winning kernfs_remove_self() but also
932 * the whole kernfs_ops which won the arbitration. This can be used to
933 * guarantee, for example, all concurrent writes to a "delete" file to
934 * finish only after the whole operation is complete.
935 */
936bool kernfs_remove_self(struct kernfs_node *kn)
937{
938 bool ret;
939
940 mutex_lock(&kernfs_mutex);
941 kernfs_break_active_protection(kn);
942
943 /*
944 * SUICIDAL is used to arbitrate among competing invocations. Only
945 * the first one will actually perform removal. When the removal
946 * is complete, SUICIDED is set and the active ref is restored
947 * while holding kernfs_mutex. The ones which lost arbitration
948 * waits for SUICDED && drained which can happen only after the
949 * enclosing kernfs operation which executed the winning instance
950 * of kernfs_remove_self() finished.
951 */
952 if (!(kn->flags & KERNFS_SUICIDAL)) {
953 kn->flags |= KERNFS_SUICIDAL;
954 __kernfs_remove(kn);
955 kn->flags |= KERNFS_SUICIDED;
956 ret = true;
957 } else {
958 wait_queue_head_t *waitq = &kernfs_root(kn)->deactivate_waitq;
959 DEFINE_WAIT(wait);
960
961 while (true) {
962 prepare_to_wait(waitq, &wait, TASK_UNINTERRUPTIBLE);
963
964 if ((kn->flags & KERNFS_SUICIDED) &&
965 atomic_read(&kn->active) == KN_DEACTIVATED_BIAS)
966 break;
967
968 mutex_unlock(&kernfs_mutex);
969 schedule();
970 mutex_lock(&kernfs_mutex);
971 }
972 finish_wait(waitq, &wait);
973 WARN_ON_ONCE(!RB_EMPTY_NODE(&kn->rb));
974 ret = false;
975 }
976
977 /*
978 * This must be done while holding kernfs_mutex; otherwise, waiting
979 * for SUICIDED && deactivated could finish prematurely.
980 */
981 kernfs_unbreak_active_protection(kn);
982
983 mutex_unlock(&kernfs_mutex);
984 return ret;
985}
986
fd7b9f7b 987/**
324a56e1
TH
988 * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
989 * @parent: parent of the target
990 * @name: name of the kernfs_node to remove
991 * @ns: namespace tag of the kernfs_node to remove
fd7b9f7b 992 *
324a56e1
TH
993 * Look for the kernfs_node with @name and @ns under @parent and remove it.
994 * Returns 0 on success, -ENOENT if such entry doesn't exist.
fd7b9f7b 995 */
324a56e1 996int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
fd7b9f7b
TH
997 const void *ns)
998{
324a56e1 999 struct kernfs_node *kn;
fd7b9f7b 1000
324a56e1 1001 if (!parent) {
c637b8ac 1002 WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
fd7b9f7b
TH
1003 name);
1004 return -ENOENT;
1005 }
1006
988cd7af 1007 mutex_lock(&kernfs_mutex);
fd7b9f7b 1008
324a56e1
TH
1009 kn = kernfs_find_ns(parent, name, ns);
1010 if (kn)
988cd7af 1011 __kernfs_remove(kn);
fd7b9f7b 1012
988cd7af 1013 mutex_unlock(&kernfs_mutex);
fd7b9f7b 1014
324a56e1 1015 if (kn)
fd7b9f7b
TH
1016 return 0;
1017 else
1018 return -ENOENT;
1019}
1020
1021/**
1022 * kernfs_rename_ns - move and rename a kernfs_node
324a56e1 1023 * @kn: target node
fd7b9f7b
TH
1024 * @new_parent: new parent to put @sd under
1025 * @new_name: new name
1026 * @new_ns: new namespace tag
1027 */
324a56e1 1028int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
fd7b9f7b
TH
1029 const char *new_name, const void *new_ns)
1030{
1031 int error;
1032
798c75a0
GKH
1033 mutex_lock(&kernfs_mutex);
1034
d0ae3d43 1035 error = -ENOENT;
81c173cb 1036 if (!kernfs_active(kn) || !kernfs_active(new_parent))
d0ae3d43
TH
1037 goto out;
1038
fd7b9f7b 1039 error = 0;
adc5e8b5
TH
1040 if ((kn->parent == new_parent) && (kn->ns == new_ns) &&
1041 (strcmp(kn->name, new_name) == 0))
798c75a0 1042 goto out; /* nothing to rename */
fd7b9f7b
TH
1043
1044 error = -EEXIST;
1045 if (kernfs_find_ns(new_parent, new_name, new_ns))
798c75a0 1046 goto out;
fd7b9f7b 1047
324a56e1 1048 /* rename kernfs_node */
adc5e8b5 1049 if (strcmp(kn->name, new_name) != 0) {
fd7b9f7b
TH
1050 error = -ENOMEM;
1051 new_name = kstrdup(new_name, GFP_KERNEL);
1052 if (!new_name)
798c75a0 1053 goto out;
fd7b9f7b 1054
47a52e91
TH
1055 if (kn->flags & KERNFS_STATIC_NAME)
1056 kn->flags &= ~KERNFS_STATIC_NAME;
1057 else
1058 kfree(kn->name);
1059
adc5e8b5 1060 kn->name = new_name;
fd7b9f7b
TH
1061 }
1062
1063 /*
1064 * Move to the appropriate place in the appropriate directories rbtree.
1065 */
c637b8ac 1066 kernfs_unlink_sibling(kn);
fd7b9f7b 1067 kernfs_get(new_parent);
adc5e8b5
TH
1068 kernfs_put(kn->parent);
1069 kn->ns = new_ns;
c637b8ac 1070 kn->hash = kernfs_name_hash(kn->name, kn->ns);
adc5e8b5 1071 kn->parent = new_parent;
c637b8ac 1072 kernfs_link_sibling(kn);
fd7b9f7b
TH
1073
1074 error = 0;
798c75a0 1075 out:
a797bfc3 1076 mutex_unlock(&kernfs_mutex);
fd7b9f7b
TH
1077 return error;
1078}
1079
fd7b9f7b 1080/* Relationship between s_mode and the DT_xxx types */
324a56e1 1081static inline unsigned char dt_type(struct kernfs_node *kn)
fd7b9f7b 1082{
adc5e8b5 1083 return (kn->mode >> 12) & 15;
fd7b9f7b
TH
1084}
1085
c637b8ac 1086static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
fd7b9f7b
TH
1087{
1088 kernfs_put(filp->private_data);
1089 return 0;
1090}
1091
c637b8ac 1092static struct kernfs_node *kernfs_dir_pos(const void *ns,
324a56e1 1093 struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
fd7b9f7b
TH
1094{
1095 if (pos) {
81c173cb 1096 int valid = kernfs_active(pos) &&
798c75a0 1097 pos->parent == parent && hash == pos->hash;
fd7b9f7b
TH
1098 kernfs_put(pos);
1099 if (!valid)
1100 pos = NULL;
1101 }
1102 if (!pos && (hash > 1) && (hash < INT_MAX)) {
adc5e8b5 1103 struct rb_node *node = parent->dir.children.rb_node;
fd7b9f7b 1104 while (node) {
324a56e1 1105 pos = rb_to_kn(node);
fd7b9f7b 1106
adc5e8b5 1107 if (hash < pos->hash)
fd7b9f7b 1108 node = node->rb_left;
adc5e8b5 1109 else if (hash > pos->hash)
fd7b9f7b
TH
1110 node = node->rb_right;
1111 else
1112 break;
1113 }
1114 }
1115 /* Skip over entries in the wrong namespace */
adc5e8b5
TH
1116 while (pos && pos->ns != ns) {
1117 struct rb_node *node = rb_next(&pos->rb);
fd7b9f7b
TH
1118 if (!node)
1119 pos = NULL;
1120 else
324a56e1 1121 pos = rb_to_kn(node);
fd7b9f7b
TH
1122 }
1123 return pos;
1124}
1125
c637b8ac 1126static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
324a56e1 1127 struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
fd7b9f7b 1128{
c637b8ac 1129 pos = kernfs_dir_pos(ns, parent, ino, pos);
fd7b9f7b
TH
1130 if (pos)
1131 do {
adc5e8b5 1132 struct rb_node *node = rb_next(&pos->rb);
fd7b9f7b
TH
1133 if (!node)
1134 pos = NULL;
1135 else
324a56e1 1136 pos = rb_to_kn(node);
adc5e8b5 1137 } while (pos && pos->ns != ns);
fd7b9f7b
TH
1138 return pos;
1139}
1140
c637b8ac 1141static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
fd7b9f7b
TH
1142{
1143 struct dentry *dentry = file->f_path.dentry;
324a56e1
TH
1144 struct kernfs_node *parent = dentry->d_fsdata;
1145 struct kernfs_node *pos = file->private_data;
fd7b9f7b
TH
1146 const void *ns = NULL;
1147
1148 if (!dir_emit_dots(file, ctx))
1149 return 0;
a797bfc3 1150 mutex_lock(&kernfs_mutex);
fd7b9f7b 1151
324a56e1 1152 if (kernfs_ns_enabled(parent))
c525aadd 1153 ns = kernfs_info(dentry->d_sb)->ns;
fd7b9f7b 1154
c637b8ac 1155 for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos);
fd7b9f7b 1156 pos;
c637b8ac 1157 pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
adc5e8b5 1158 const char *name = pos->name;
fd7b9f7b
TH
1159 unsigned int type = dt_type(pos);
1160 int len = strlen(name);
adc5e8b5 1161 ino_t ino = pos->ino;
fd7b9f7b 1162
adc5e8b5 1163 ctx->pos = pos->hash;
fd7b9f7b
TH
1164 file->private_data = pos;
1165 kernfs_get(pos);
1166
a797bfc3 1167 mutex_unlock(&kernfs_mutex);
fd7b9f7b
TH
1168 if (!dir_emit(ctx, name, len, ino, type))
1169 return 0;
a797bfc3 1170 mutex_lock(&kernfs_mutex);
fd7b9f7b 1171 }
a797bfc3 1172 mutex_unlock(&kernfs_mutex);
fd7b9f7b
TH
1173 file->private_data = NULL;
1174 ctx->pos = INT_MAX;
1175 return 0;
1176}
1177
c637b8ac
TH
1178static loff_t kernfs_dir_fop_llseek(struct file *file, loff_t offset,
1179 int whence)
fd7b9f7b
TH
1180{
1181 struct inode *inode = file_inode(file);
1182 loff_t ret;
1183
1184 mutex_lock(&inode->i_mutex);
1185 ret = generic_file_llseek(file, offset, whence);
1186 mutex_unlock(&inode->i_mutex);
1187
1188 return ret;
1189}
1190
a797bfc3 1191const struct file_operations kernfs_dir_fops = {
fd7b9f7b 1192 .read = generic_read_dir,
c637b8ac
TH
1193 .iterate = kernfs_fop_readdir,
1194 .release = kernfs_dir_fop_release,
1195 .llseek = kernfs_dir_fop_llseek,
fd7b9f7b 1196};
This page took 0.098491 seconds and 5 git commands to generate.