Revert "kernfs: invoke kernfs_unmap_bin_file() directly from __kernfs_remove()"
[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
ea1c472d 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
a69d001c
TH
25static bool kernfs_lockdep(struct kernfs_node *kn)
26{
27#ifdef CONFIG_DEBUG_LOCK_ALLOC
28 return kn->flags & KERNFS_LOCKDEP;
29#else
30 return false;
31#endif
32}
33
fd7b9f7b 34/**
c637b8ac 35 * kernfs_name_hash
fd7b9f7b
TH
36 * @name: Null terminated string to hash
37 * @ns: Namespace tag to hash
38 *
39 * Returns 31 bit hash of ns + name (so it fits in an off_t )
40 */
c637b8ac 41static unsigned int kernfs_name_hash(const char *name, const void *ns)
fd7b9f7b
TH
42{
43 unsigned long hash = init_name_hash();
44 unsigned int len = strlen(name);
45 while (len--)
46 hash = partial_name_hash(*name++, hash);
47 hash = (end_name_hash(hash) ^ hash_ptr((void *)ns, 31));
48 hash &= 0x7fffffffU;
49 /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */
50 if (hash < 1)
51 hash += 2;
52 if (hash >= INT_MAX)
53 hash = INT_MAX - 1;
54 return hash;
55}
56
c637b8ac
TH
57static int kernfs_name_compare(unsigned int hash, const char *name,
58 const void *ns, const struct kernfs_node *kn)
fd7b9f7b 59{
adc5e8b5
TH
60 if (hash != kn->hash)
61 return hash - kn->hash;
62 if (ns != kn->ns)
63 return ns - kn->ns;
64 return strcmp(name, kn->name);
fd7b9f7b
TH
65}
66
c637b8ac
TH
67static int kernfs_sd_compare(const struct kernfs_node *left,
68 const struct kernfs_node *right)
fd7b9f7b 69{
c637b8ac 70 return kernfs_name_compare(left->hash, left->name, left->ns, right);
fd7b9f7b
TH
71}
72
73/**
c637b8ac 74 * kernfs_link_sibling - link kernfs_node into sibling rbtree
324a56e1 75 * @kn: kernfs_node of interest
fd7b9f7b 76 *
324a56e1 77 * Link @kn into its sibling rbtree which starts from
adc5e8b5 78 * @kn->parent->dir.children.
fd7b9f7b
TH
79 *
80 * Locking:
a797bfc3 81 * mutex_lock(kernfs_mutex)
fd7b9f7b
TH
82 *
83 * RETURNS:
84 * 0 on susccess -EEXIST on failure.
85 */
c637b8ac 86static int kernfs_link_sibling(struct kernfs_node *kn)
fd7b9f7b 87{
adc5e8b5 88 struct rb_node **node = &kn->parent->dir.children.rb_node;
fd7b9f7b
TH
89 struct rb_node *parent = NULL;
90
df23fc39 91 if (kernfs_type(kn) == KERNFS_DIR)
adc5e8b5 92 kn->parent->dir.subdirs++;
fd7b9f7b
TH
93
94 while (*node) {
324a56e1 95 struct kernfs_node *pos;
fd7b9f7b
TH
96 int result;
97
324a56e1 98 pos = rb_to_kn(*node);
fd7b9f7b 99 parent = *node;
c637b8ac 100 result = kernfs_sd_compare(kn, pos);
fd7b9f7b 101 if (result < 0)
adc5e8b5 102 node = &pos->rb.rb_left;
fd7b9f7b 103 else if (result > 0)
adc5e8b5 104 node = &pos->rb.rb_right;
fd7b9f7b
TH
105 else
106 return -EEXIST;
107 }
108 /* add new node and rebalance the tree */
adc5e8b5
TH
109 rb_link_node(&kn->rb, parent, node);
110 rb_insert_color(&kn->rb, &kn->parent->dir.children);
fd7b9f7b
TH
111 return 0;
112}
113
114/**
c637b8ac 115 * kernfs_unlink_sibling - unlink kernfs_node from sibling rbtree
324a56e1 116 * @kn: kernfs_node of interest
fd7b9f7b 117 *
324a56e1 118 * Unlink @kn from its sibling rbtree which starts from
adc5e8b5 119 * kn->parent->dir.children.
fd7b9f7b
TH
120 *
121 * Locking:
a797bfc3 122 * mutex_lock(kernfs_mutex)
fd7b9f7b 123 */
55f6e30d 124static void kernfs_unlink_sibling(struct kernfs_node *kn)
fd7b9f7b 125{
df23fc39 126 if (kernfs_type(kn) == KERNFS_DIR)
adc5e8b5 127 kn->parent->dir.subdirs--;
fd7b9f7b 128
adc5e8b5 129 rb_erase(&kn->rb, &kn->parent->dir.children);
ae34372e 130 RB_CLEAR_NODE(&kn->rb);
fd7b9f7b
TH
131}
132
133/**
c637b8ac 134 * kernfs_get_active - get an active reference to kernfs_node
324a56e1 135 * @kn: kernfs_node to get an active reference to
fd7b9f7b 136 *
324a56e1 137 * Get an active reference of @kn. This function is noop if @kn
fd7b9f7b
TH
138 * is NULL.
139 *
140 * RETURNS:
324a56e1 141 * Pointer to @kn on success, NULL on failure.
fd7b9f7b 142 */
c637b8ac 143struct kernfs_node *kernfs_get_active(struct kernfs_node *kn)
fd7b9f7b 144{
324a56e1 145 if (unlikely(!kn))
fd7b9f7b
TH
146 return NULL;
147
f4b3e631
GKH
148 if (!atomic_inc_unless_negative(&kn->active))
149 return NULL;
895a068a
TH
150
151 if (kernfs_lockdep(kn))
f4b3e631
GKH
152 rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_);
153 return kn;
fd7b9f7b
TH
154}
155
156/**
c637b8ac 157 * kernfs_put_active - put an active reference to kernfs_node
324a56e1 158 * @kn: kernfs_node to put an active reference to
fd7b9f7b 159 *
324a56e1 160 * Put an active reference to @kn. This function is noop if @kn
fd7b9f7b
TH
161 * is NULL.
162 */
c637b8ac 163void kernfs_put_active(struct kernfs_node *kn)
fd7b9f7b 164{
ea1c472d 165 struct kernfs_root *root = kernfs_root(kn);
fd7b9f7b
TH
166 int v;
167
324a56e1 168 if (unlikely(!kn))
fd7b9f7b
TH
169 return;
170
a69d001c 171 if (kernfs_lockdep(kn))
324a56e1 172 rwsem_release(&kn->dep_map, 1, _RET_IP_);
adc5e8b5 173 v = atomic_dec_return(&kn->active);
df23fc39 174 if (likely(v != KN_DEACTIVATED_BIAS))
fd7b9f7b
TH
175 return;
176
ea1c472d 177 wake_up_all(&root->deactivate_waitq);
fd7b9f7b
TH
178}
179
180/**
ae34372e
TH
181 * kernfs_drain - drain kernfs_node
182 * @kn: kernfs_node to drain
fd7b9f7b 183 *
45a140e5
TH
184 * Drain existing usages of @kn. Mutiple removers may invoke this function
185 * concurrently on @kn and all will return after draining is complete.
186 * Returns %true if drain is performed and kernfs_mutex was temporarily
187 * released. %false if @kn was already drained and no operation was
188 * necessary.
189 *
190 * The caller is responsible for ensuring @kn stays pinned while this
191 * function is in progress even if it gets removed by someone else.
fd7b9f7b 192 */
45a140e5
TH
193static bool kernfs_drain(struct kernfs_node *kn)
194 __releases(&kernfs_mutex) __acquires(&kernfs_mutex)
fd7b9f7b 195{
ea1c472d 196 struct kernfs_root *root = kernfs_root(kn);
fd7b9f7b 197
45a140e5 198 lockdep_assert_held(&kernfs_mutex);
ae34372e 199 WARN_ON_ONCE(atomic_read(&kn->active) >= 0);
ea1c472d 200
45a140e5
TH
201 /*
202 * We want to go through the active ref lockdep annotation at least
203 * once for all node removals, but the lockdep annotation can't be
204 * nested inside kernfs_mutex and deactivation can't make forward
205 * progress if we keep dropping the mutex. Use JUST_ACTIVATED to
206 * force the slow path once for each deactivation if lockdep is
207 * enabled.
208 */
209 if ((!kernfs_lockdep(kn) || !(kn->flags & KERNFS_JUST_DEACTIVATED)) &&
210 atomic_read(&kn->active) == KN_DEACTIVATED_BIAS)
211 return false;
212
213 kn->flags &= ~KERNFS_JUST_DEACTIVATED;
214 mutex_unlock(&kernfs_mutex);
215
a69d001c
TH
216 if (kernfs_lockdep(kn)) {
217 rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_);
218 if (atomic_read(&kn->active) != KN_DEACTIVATED_BIAS)
219 lock_contended(&kn->dep_map, _RET_IP_);
220 }
ea1c472d
TH
221
222 wait_event(root->deactivate_waitq,
223 atomic_read(&kn->active) == KN_DEACTIVATED_BIAS);
fd7b9f7b 224
a69d001c
TH
225 if (kernfs_lockdep(kn)) {
226 lock_acquired(&kn->dep_map, _RET_IP_);
227 rwsem_release(&kn->dep_map, 1, _RET_IP_);
228 }
45a140e5
TH
229
230 mutex_lock(&kernfs_mutex);
231 return true;
fd7b9f7b
TH
232}
233
fd7b9f7b 234/**
324a56e1
TH
235 * kernfs_get - get a reference count on a kernfs_node
236 * @kn: the target kernfs_node
fd7b9f7b 237 */
324a56e1 238void kernfs_get(struct kernfs_node *kn)
fd7b9f7b 239{
324a56e1 240 if (kn) {
adc5e8b5
TH
241 WARN_ON(!atomic_read(&kn->count));
242 atomic_inc(&kn->count);
fd7b9f7b
TH
243 }
244}
245EXPORT_SYMBOL_GPL(kernfs_get);
246
247/**
324a56e1
TH
248 * kernfs_put - put a reference count on a kernfs_node
249 * @kn: the target kernfs_node
fd7b9f7b 250 *
324a56e1 251 * Put a reference count of @kn and destroy it if it reached zero.
fd7b9f7b 252 */
324a56e1 253void kernfs_put(struct kernfs_node *kn)
fd7b9f7b 254{
324a56e1 255 struct kernfs_node *parent;
ba7443bc 256 struct kernfs_root *root;
fd7b9f7b 257
adc5e8b5 258 if (!kn || !atomic_dec_and_test(&kn->count))
fd7b9f7b 259 return;
324a56e1 260 root = kernfs_root(kn);
fd7b9f7b 261 repeat:
ae34372e
TH
262 /*
263 * Moving/renaming is always done while holding reference.
adc5e8b5 264 * kn->parent won't change beneath us.
fd7b9f7b 265 */
adc5e8b5 266 parent = kn->parent;
fd7b9f7b 267
ae34372e
TH
268 WARN_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS,
269 "kernfs_put: %s/%s: released with incorrect active_ref %d\n",
270 parent ? parent->name : "", kn->name, atomic_read(&kn->active));
324a56e1 271
df23fc39 272 if (kernfs_type(kn) == KERNFS_LINK)
adc5e8b5 273 kernfs_put(kn->symlink.target_kn);
2063d608 274 if (!(kn->flags & KERNFS_STATIC_NAME))
adc5e8b5
TH
275 kfree(kn->name);
276 if (kn->iattr) {
277 if (kn->iattr->ia_secdata)
278 security_release_secctx(kn->iattr->ia_secdata,
279 kn->iattr->ia_secdata_len);
280 simple_xattrs_free(&kn->iattr->xattrs);
2322392b 281 }
adc5e8b5
TH
282 kfree(kn->iattr);
283 ida_simple_remove(&root->ino_ida, kn->ino);
a797bfc3 284 kmem_cache_free(kernfs_node_cache, kn);
fd7b9f7b 285
324a56e1
TH
286 kn = parent;
287 if (kn) {
adc5e8b5 288 if (atomic_dec_and_test(&kn->count))
ba7443bc
TH
289 goto repeat;
290 } else {
324a56e1 291 /* just released the root kn, free @root too */
bc755553 292 ida_destroy(&root->ino_ida);
ba7443bc
TH
293 kfree(root);
294 }
fd7b9f7b
TH
295}
296EXPORT_SYMBOL_GPL(kernfs_put);
297
c637b8ac 298static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags)
fd7b9f7b 299{
324a56e1 300 struct kernfs_node *kn;
fd7b9f7b
TH
301
302 if (flags & LOOKUP_RCU)
303 return -ECHILD;
304
19bbb926
TH
305 /* Always perform fresh lookup for negatives */
306 if (!dentry->d_inode)
307 goto out_bad_unlocked;
308
324a56e1 309 kn = dentry->d_fsdata;
a797bfc3 310 mutex_lock(&kernfs_mutex);
fd7b9f7b 311
ae34372e
TH
312 /* Force fresh lookup if removed */
313 if (kn->parent && RB_EMPTY_NODE(&kn->rb))
fd7b9f7b
TH
314 goto out_bad;
315
c637b8ac 316 /* The kernfs node has been moved? */
adc5e8b5 317 if (dentry->d_parent->d_fsdata != kn->parent)
fd7b9f7b
TH
318 goto out_bad;
319
c637b8ac 320 /* The kernfs node has been renamed */
adc5e8b5 321 if (strcmp(dentry->d_name.name, kn->name) != 0)
fd7b9f7b
TH
322 goto out_bad;
323
c637b8ac 324 /* The kernfs node has been moved to a different namespace */
adc5e8b5 325 if (kn->parent && kernfs_ns_enabled(kn->parent) &&
c525aadd 326 kernfs_info(dentry->d_sb)->ns != kn->ns)
fd7b9f7b
TH
327 goto out_bad;
328
a797bfc3 329 mutex_unlock(&kernfs_mutex);
fd7b9f7b
TH
330out_valid:
331 return 1;
332out_bad:
a797bfc3 333 mutex_unlock(&kernfs_mutex);
19bbb926
TH
334out_bad_unlocked:
335 /*
336 * @dentry doesn't match the underlying kernfs node, drop the
337 * dentry and force lookup. If we have submounts we must allow the
338 * vfs caches to lie about the state of the filesystem to prevent
339 * leaks and other nasty things, so use check_submounts_and_drop()
340 * instead of d_drop().
fd7b9f7b
TH
341 */
342 if (check_submounts_and_drop(dentry) != 0)
343 goto out_valid;
344
345 return 0;
346}
347
c637b8ac 348static void kernfs_dop_release(struct dentry *dentry)
fd7b9f7b
TH
349{
350 kernfs_put(dentry->d_fsdata);
351}
352
a797bfc3 353const struct dentry_operations kernfs_dops = {
c637b8ac 354 .d_revalidate = kernfs_dop_revalidate,
c637b8ac 355 .d_release = kernfs_dop_release,
fd7b9f7b
TH
356};
357
c637b8ac 358struct kernfs_node *kernfs_new_node(struct kernfs_root *root, const char *name,
2063d608 359 umode_t mode, unsigned flags)
fd7b9f7b
TH
360{
361 char *dup_name = NULL;
324a56e1 362 struct kernfs_node *kn;
bc755553 363 int ret;
fd7b9f7b 364
2063d608 365 if (!(flags & KERNFS_STATIC_NAME)) {
fd7b9f7b
TH
366 name = dup_name = kstrdup(name, GFP_KERNEL);
367 if (!name)
368 return NULL;
369 }
370
a797bfc3 371 kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL);
324a56e1 372 if (!kn)
fd7b9f7b
TH
373 goto err_out1;
374
bc755553
TH
375 ret = ida_simple_get(&root->ino_ida, 1, 0, GFP_KERNEL);
376 if (ret < 0)
fd7b9f7b 377 goto err_out2;
adc5e8b5 378 kn->ino = ret;
fd7b9f7b 379
adc5e8b5 380 atomic_set(&kn->count, 1);
ae34372e
TH
381 atomic_set(&kn->active, KN_DEACTIVATED_BIAS);
382 RB_CLEAR_NODE(&kn->rb);
fd7b9f7b 383
adc5e8b5
TH
384 kn->name = name;
385 kn->mode = mode;
ae34372e 386 kn->flags = flags;
fd7b9f7b 387
324a56e1 388 return kn;
fd7b9f7b
TH
389
390 err_out2:
a797bfc3 391 kmem_cache_free(kernfs_node_cache, kn);
fd7b9f7b
TH
392 err_out1:
393 kfree(dup_name);
394 return NULL;
395}
396
7653fe9d
GKH
397/**
398 * kernfs_addrm_start - prepare for kernfs_node add/remove
399 * @acxt: pointer to kernfs_addrm_cxt to be used
400 *
401 * This function is called when the caller is about to add or remove
402 * kernfs_node. This function acquires kernfs_mutex. @acxt is used
403 * to keep and pass context to other addrm functions.
404 *
405 * LOCKING:
406 * Kernel thread context (may sleep). kernfs_mutex is locked on
407 * return.
408 */
409void kernfs_addrm_start(struct kernfs_addrm_cxt *acxt)
410 __acquires(kernfs_mutex)
411{
412 memset(acxt, 0, sizeof(*acxt));
413
414 mutex_lock(&kernfs_mutex);
415}
416
fd7b9f7b 417/**
c637b8ac 418 * kernfs_add_one - add kernfs_node to parent without warning
7653fe9d 419 * @acxt: addrm context to use
324a56e1
TH
420 * @kn: kernfs_node to be added
421 * @parent: the parent kernfs_node to add @kn to
fd7b9f7b 422 *
adc5e8b5
TH
423 * Get @parent and set @kn->parent to it and increment nlink of the
424 * parent inode if @kn is a directory and link into the children list
425 * of the parent.
fd7b9f7b 426 *
7653fe9d
GKH
427 * This function should be called between calls to
428 * kernfs_addrm_start() and kernfs_addrm_finish() and should be passed
429 * the same @acxt as passed to kernfs_addrm_start().
430 *
431 * LOCKING:
432 * Determined by kernfs_addrm_start().
433 *
fd7b9f7b
TH
434 * RETURNS:
435 * 0 on success, -EEXIST if entry with the given name already
436 * exists.
437 */
7653fe9d
GKH
438int kernfs_add_one(struct kernfs_addrm_cxt *acxt, struct kernfs_node *kn,
439 struct kernfs_node *parent)
fd7b9f7b 440{
7653fe9d 441 bool has_ns = kernfs_ns_enabled(parent);
c525aadd 442 struct kernfs_iattrs *ps_iattr;
fd7b9f7b
TH
443 int ret;
444
7653fe9d 445 WARN_ON_ONCE(atomic_read(&parent->active) < 0);
99177a34 446
7653fe9d
GKH
447 if (has_ns != (bool)kn->ns) {
448 WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
449 has_ns ? "required" : "invalid", parent->name, kn->name);
450 return -EINVAL;
451 }
fd7b9f7b 452
df23fc39 453 if (kernfs_type(parent) != KERNFS_DIR)
7653fe9d 454 return -EINVAL;
fd7b9f7b 455
c637b8ac 456 kn->hash = kernfs_name_hash(kn->name, kn->ns);
adc5e8b5 457 kn->parent = parent;
324a56e1 458 kernfs_get(parent);
fd7b9f7b 459
c637b8ac 460 ret = kernfs_link_sibling(kn);
fd7b9f7b 461 if (ret)
7653fe9d 462 return ret;
fd7b9f7b
TH
463
464 /* Update timestamps on the parent */
adc5e8b5 465 ps_iattr = parent->iattr;
fd7b9f7b
TH
466 if (ps_iattr) {
467 struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
468 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
469 }
470
471 /* Mark the entry added into directory tree */
ae34372e 472 atomic_sub(KN_DEACTIVATED_BIAS, &kn->active);
7653fe9d
GKH
473 return 0;
474}
475
476/**
477 * kernfs_addrm_finish - finish up kernfs_node add/remove
478 * @acxt: addrm context to finish up
479 *
480 * Finish up kernfs_node add/remove. Resources acquired by
481 * kernfs_addrm_start() are released and removed kernfs_nodes are
482 * cleaned up.
483 *
484 * LOCKING:
485 * kernfs_mutex is released.
486 */
487void kernfs_addrm_finish(struct kernfs_addrm_cxt *acxt)
488 __releases(kernfs_mutex)
489{
490 /* release resources acquired by kernfs_addrm_start() */
a797bfc3 491 mutex_unlock(&kernfs_mutex);
7653fe9d
GKH
492
493 /* kill removed kernfs_nodes */
494 while (acxt->removed) {
495 struct kernfs_node *kn = acxt->removed;
496
497 acxt->removed = kn->u.removed_list;
498
55f6e30d 499 kernfs_unmap_bin_file(kn);
7653fe9d
GKH
500 kernfs_put(kn);
501 }
fd7b9f7b
TH
502}
503
504/**
324a56e1
TH
505 * kernfs_find_ns - find 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
TH
510 * Look for kernfs_node with name @name under @parent. Returns pointer to
511 * the found kernfs_node on success, %NULL on failure.
fd7b9f7b 512 */
324a56e1
TH
513static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
514 const unsigned char *name,
515 const void *ns)
fd7b9f7b 516{
adc5e8b5 517 struct rb_node *node = parent->dir.children.rb_node;
ac9bba03 518 bool has_ns = kernfs_ns_enabled(parent);
fd7b9f7b
TH
519 unsigned int hash;
520
a797bfc3 521 lockdep_assert_held(&kernfs_mutex);
fd7b9f7b
TH
522
523 if (has_ns != (bool)ns) {
c637b8ac 524 WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
adc5e8b5 525 has_ns ? "required" : "invalid", parent->name, name);
fd7b9f7b
TH
526 return NULL;
527 }
528
c637b8ac 529 hash = kernfs_name_hash(name, ns);
fd7b9f7b 530 while (node) {
324a56e1 531 struct kernfs_node *kn;
fd7b9f7b
TH
532 int result;
533
324a56e1 534 kn = rb_to_kn(node);
c637b8ac 535 result = kernfs_name_compare(hash, name, ns, kn);
fd7b9f7b
TH
536 if (result < 0)
537 node = node->rb_left;
538 else if (result > 0)
539 node = node->rb_right;
540 else
324a56e1 541 return kn;
fd7b9f7b
TH
542 }
543 return NULL;
544}
545
546/**
324a56e1
TH
547 * kernfs_find_and_get_ns - find and get kernfs_node with the given name
548 * @parent: kernfs_node to search under
fd7b9f7b
TH
549 * @name: name to look for
550 * @ns: the namespace tag to use
551 *
324a56e1 552 * Look for kernfs_node with name @name under @parent and get a reference
fd7b9f7b 553 * if found. This function may sleep and returns pointer to the found
324a56e1 554 * kernfs_node on success, %NULL on failure.
fd7b9f7b 555 */
324a56e1
TH
556struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
557 const char *name, const void *ns)
fd7b9f7b 558{
324a56e1 559 struct kernfs_node *kn;
fd7b9f7b 560
a797bfc3 561 mutex_lock(&kernfs_mutex);
324a56e1
TH
562 kn = kernfs_find_ns(parent, name, ns);
563 kernfs_get(kn);
a797bfc3 564 mutex_unlock(&kernfs_mutex);
fd7b9f7b 565
324a56e1 566 return kn;
fd7b9f7b
TH
567}
568EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
569
ba7443bc
TH
570/**
571 * kernfs_create_root - create a new kernfs hierarchy
80b9bbef 572 * @kdops: optional directory syscall operations for the hierarchy
ba7443bc
TH
573 * @priv: opaque data associated with the new directory
574 *
575 * Returns the root of the new hierarchy on success, ERR_PTR() value on
576 * failure.
577 */
80b9bbef 578struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops, void *priv)
ba7443bc
TH
579{
580 struct kernfs_root *root;
324a56e1 581 struct kernfs_node *kn;
ba7443bc
TH
582
583 root = kzalloc(sizeof(*root), GFP_KERNEL);
584 if (!root)
585 return ERR_PTR(-ENOMEM);
586
bc755553
TH
587 ida_init(&root->ino_ida);
588
c637b8ac 589 kn = kernfs_new_node(root, "", S_IFDIR | S_IRUGO | S_IXUGO, KERNFS_DIR);
324a56e1 590 if (!kn) {
bc755553 591 ida_destroy(&root->ino_ida);
ba7443bc
TH
592 kfree(root);
593 return ERR_PTR(-ENOMEM);
594 }
595
ae34372e 596 atomic_sub(KN_DEACTIVATED_BIAS, &kn->active);
324a56e1 597 kn->priv = priv;
adc5e8b5 598 kn->dir.root = root;
ba7443bc 599
80b9bbef 600 root->dir_ops = kdops;
324a56e1 601 root->kn = kn;
ea1c472d 602 init_waitqueue_head(&root->deactivate_waitq);
ba7443bc
TH
603
604 return root;
605}
606
607/**
608 * kernfs_destroy_root - destroy a kernfs hierarchy
609 * @root: root of the hierarchy to destroy
610 *
611 * Destroy the hierarchy anchored at @root by removing all existing
612 * directories and destroying @root.
613 */
614void kernfs_destroy_root(struct kernfs_root *root)
615{
324a56e1 616 kernfs_remove(root->kn); /* will also free @root */
ba7443bc
TH
617}
618
fd7b9f7b
TH
619/**
620 * kernfs_create_dir_ns - create a directory
621 * @parent: parent in which to create a new directory
622 * @name: name of the new directory
bb8b9d09 623 * @mode: mode of the new directory
fd7b9f7b
TH
624 * @priv: opaque data associated with the new directory
625 * @ns: optional namespace tag of the directory
626 *
627 * Returns the created node on success, ERR_PTR() value on failure.
628 */
324a56e1 629struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
bb8b9d09
TH
630 const char *name, umode_t mode,
631 void *priv, const void *ns)
fd7b9f7b 632{
7653fe9d 633 struct kernfs_addrm_cxt acxt;
324a56e1 634 struct kernfs_node *kn;
fd7b9f7b
TH
635 int rc;
636
637 /* allocate */
bb8b9d09
TH
638 kn = kernfs_new_node(kernfs_root(parent), name, mode | S_IFDIR,
639 KERNFS_DIR);
324a56e1 640 if (!kn)
fd7b9f7b
TH
641 return ERR_PTR(-ENOMEM);
642
adc5e8b5
TH
643 kn->dir.root = parent->dir.root;
644 kn->ns = ns;
324a56e1 645 kn->priv = priv;
fd7b9f7b
TH
646
647 /* link in */
7653fe9d
GKH
648 rc = -ENOENT;
649 if (kernfs_get_active(parent)) {
650 kernfs_addrm_start(&acxt);
651 rc = kernfs_add_one(&acxt, kn, parent);
652 kernfs_addrm_finish(&acxt);
653 kernfs_put_active(parent);
654 }
655
fd7b9f7b 656 if (!rc)
324a56e1 657 return kn;
fd7b9f7b 658
324a56e1 659 kernfs_put(kn);
fd7b9f7b
TH
660 return ERR_PTR(rc);
661}
662
c637b8ac
TH
663static struct dentry *kernfs_iop_lookup(struct inode *dir,
664 struct dentry *dentry,
665 unsigned int flags)
fd7b9f7b 666{
19bbb926 667 struct dentry *ret;
324a56e1
TH
668 struct kernfs_node *parent = dentry->d_parent->d_fsdata;
669 struct kernfs_node *kn;
fd7b9f7b
TH
670 struct inode *inode;
671 const void *ns = NULL;
672
a797bfc3 673 mutex_lock(&kernfs_mutex);
fd7b9f7b 674
324a56e1 675 if (kernfs_ns_enabled(parent))
c525aadd 676 ns = kernfs_info(dir->i_sb)->ns;
fd7b9f7b 677
324a56e1 678 kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
fd7b9f7b
TH
679
680 /* no such entry */
324a56e1 681 if (!kn) {
19bbb926 682 ret = NULL;
fd7b9f7b
TH
683 goto out_unlock;
684 }
324a56e1
TH
685 kernfs_get(kn);
686 dentry->d_fsdata = kn;
fd7b9f7b
TH
687
688 /* attach dentry and inode */
c637b8ac 689 inode = kernfs_get_inode(dir->i_sb, kn);
fd7b9f7b
TH
690 if (!inode) {
691 ret = ERR_PTR(-ENOMEM);
692 goto out_unlock;
693 }
694
695 /* instantiate and hash dentry */
696 ret = d_materialise_unique(dentry, inode);
697 out_unlock:
a797bfc3 698 mutex_unlock(&kernfs_mutex);
fd7b9f7b
TH
699 return ret;
700}
701
80b9bbef
TH
702static int kernfs_iop_mkdir(struct inode *dir, struct dentry *dentry,
703 umode_t mode)
704{
705 struct kernfs_node *parent = dir->i_private;
706 struct kernfs_dir_ops *kdops = kernfs_root(parent)->dir_ops;
707
708 if (!kdops || !kdops->mkdir)
709 return -EPERM;
710
711 return kdops->mkdir(parent, dentry->d_name.name, mode);
712}
713
714static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
715{
716 struct kernfs_node *kn = dentry->d_fsdata;
717 struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops;
718
719 if (!kdops || !kdops->rmdir)
720 return -EPERM;
721
722 return kdops->rmdir(kn);
723}
724
725static int kernfs_iop_rename(struct inode *old_dir, struct dentry *old_dentry,
726 struct inode *new_dir, struct dentry *new_dentry)
727{
728 struct kernfs_node *kn = old_dentry->d_fsdata;
729 struct kernfs_node *new_parent = new_dir->i_private;
730 struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops;
731
732 if (!kdops || !kdops->rename)
733 return -EPERM;
734
735 return kdops->rename(kn, new_parent, new_dentry->d_name.name);
736}
737
a797bfc3 738const struct inode_operations kernfs_dir_iops = {
c637b8ac
TH
739 .lookup = kernfs_iop_lookup,
740 .permission = kernfs_iop_permission,
741 .setattr = kernfs_iop_setattr,
742 .getattr = kernfs_iop_getattr,
743 .setxattr = kernfs_iop_setxattr,
744 .removexattr = kernfs_iop_removexattr,
745 .getxattr = kernfs_iop_getxattr,
746 .listxattr = kernfs_iop_listxattr,
80b9bbef
TH
747
748 .mkdir = kernfs_iop_mkdir,
749 .rmdir = kernfs_iop_rmdir,
750 .rename = kernfs_iop_rename,
fd7b9f7b
TH
751};
752
c637b8ac 753static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
fd7b9f7b 754{
324a56e1 755 struct kernfs_node *last;
fd7b9f7b
TH
756
757 while (true) {
758 struct rb_node *rbn;
759
760 last = pos;
761
df23fc39 762 if (kernfs_type(pos) != KERNFS_DIR)
fd7b9f7b
TH
763 break;
764
adc5e8b5 765 rbn = rb_first(&pos->dir.children);
fd7b9f7b
TH
766 if (!rbn)
767 break;
768
324a56e1 769 pos = rb_to_kn(rbn);
fd7b9f7b
TH
770 }
771
772 return last;
773}
774
775/**
c637b8ac 776 * kernfs_next_descendant_post - find the next descendant for post-order walk
fd7b9f7b 777 * @pos: the current position (%NULL to initiate traversal)
324a56e1 778 * @root: kernfs_node whose descendants to walk
fd7b9f7b
TH
779 *
780 * Find the next descendant to visit for post-order traversal of @root's
781 * descendants. @root is included in the iteration and the last node to be
782 * visited.
783 */
c637b8ac
TH
784static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
785 struct kernfs_node *root)
fd7b9f7b
TH
786{
787 struct rb_node *rbn;
788
a797bfc3 789 lockdep_assert_held(&kernfs_mutex);
fd7b9f7b
TH
790
791 /* if first iteration, visit leftmost descendant which may be root */
792 if (!pos)
c637b8ac 793 return kernfs_leftmost_descendant(root);
fd7b9f7b
TH
794
795 /* if we visited @root, we're done */
796 if (pos == root)
797 return NULL;
798
799 /* if there's an unvisited sibling, visit its leftmost descendant */
adc5e8b5 800 rbn = rb_next(&pos->rb);
fd7b9f7b 801 if (rbn)
c637b8ac 802 return kernfs_leftmost_descendant(rb_to_kn(rbn));
fd7b9f7b
TH
803
804 /* no sibling left, visit parent */
adc5e8b5 805 return pos->parent;
fd7b9f7b
TH
806}
807
45a140e5
TH
808static void __kernfs_deactivate(struct kernfs_node *kn)
809{
810 struct kernfs_node *pos;
811
812 lockdep_assert_held(&kernfs_mutex);
813
814 /* prevent any new usage under @kn by deactivating all nodes */
815 pos = NULL;
816 while ((pos = kernfs_next_descendant_post(pos, kn))) {
9b0925a6 817 if (atomic_read(&pos->active) >= 0) {
45a140e5
TH
818 atomic_add(KN_DEACTIVATED_BIAS, &pos->active);
819 pos->flags |= KERNFS_JUST_DEACTIVATED;
820 }
821 }
822
823 /*
824 * Drain the subtree. If kernfs_drain() blocked to drain, which is
825 * indicated by %true return, it temporarily released kernfs_mutex
826 * and the rbtree might have been modified inbetween breaking our
827 * future walk. Restart the walk after each %true return.
828 */
829 pos = NULL;
830 while ((pos = kernfs_next_descendant_post(pos, kn))) {
831 bool drained;
832
833 kernfs_get(pos);
834 drained = kernfs_drain(pos);
835 kernfs_put(pos);
836 if (drained)
837 pos = NULL;
838 }
839}
840
7653fe9d
GKH
841static void __kernfs_remove(struct kernfs_addrm_cxt *acxt,
842 struct kernfs_node *kn)
fd7b9f7b 843{
45a140e5
TH
844 struct kernfs_node *pos;
845
846 lockdep_assert_held(&kernfs_mutex);
fd7b9f7b 847
ce9b499c
GKH
848 if (!kn)
849 return;
850
c637b8ac 851 pr_debug("kernfs %s: removing\n", kn->name);
fd7b9f7b 852
45a140e5
TH
853 __kernfs_deactivate(kn);
854
855 /* unlink the subtree node-by-node */
fd7b9f7b 856 do {
55f6e30d 857 struct kernfs_iattrs *ps_iattr;
f601f9a2 858
55f6e30d 859 pos = kernfs_leftmost_descendant(kn);
f601f9a2 860
55f6e30d
GKH
861 if (pos->parent) {
862 kernfs_unlink_sibling(pos);
45a140e5
TH
863
864 /* update timestamps on the parent */
55f6e30d 865 ps_iattr = pos->parent->iattr;
45a140e5
TH
866 if (ps_iattr) {
867 ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME;
868 ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME;
869 }
870 }
871
55f6e30d
GKH
872 pos->u.removed_list = acxt->removed;
873 acxt->removed = pos;
45a140e5 874 } while (pos != kn);
fd7b9f7b
TH
875}
876
877/**
324a56e1
TH
878 * kernfs_remove - remove a kernfs_node recursively
879 * @kn: the kernfs_node to remove
fd7b9f7b 880 *
324a56e1 881 * Remove @kn along with all its subdirectories and files.
fd7b9f7b 882 */
324a56e1 883void kernfs_remove(struct kernfs_node *kn)
fd7b9f7b 884{
7653fe9d
GKH
885 struct kernfs_addrm_cxt acxt;
886
887 kernfs_addrm_start(&acxt);
888 __kernfs_remove(&acxt, kn);
889 kernfs_addrm_finish(&acxt);
fd7b9f7b
TH
890}
891
892/**
324a56e1
TH
893 * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
894 * @parent: parent of the target
895 * @name: name of the kernfs_node to remove
896 * @ns: namespace tag of the kernfs_node to remove
fd7b9f7b 897 *
324a56e1
TH
898 * Look for the kernfs_node with @name and @ns under @parent and remove it.
899 * Returns 0 on success, -ENOENT if such entry doesn't exist.
fd7b9f7b 900 */
324a56e1 901int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
fd7b9f7b
TH
902 const void *ns)
903{
7653fe9d 904 struct kernfs_addrm_cxt acxt;
324a56e1 905 struct kernfs_node *kn;
fd7b9f7b 906
324a56e1 907 if (!parent) {
c637b8ac 908 WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
fd7b9f7b
TH
909 name);
910 return -ENOENT;
911 }
912
7653fe9d 913 kernfs_addrm_start(&acxt);
fd7b9f7b 914
324a56e1
TH
915 kn = kernfs_find_ns(parent, name, ns);
916 if (kn)
7653fe9d 917 __kernfs_remove(&acxt, kn);
fd7b9f7b 918
7653fe9d 919 kernfs_addrm_finish(&acxt);
fd7b9f7b 920
324a56e1 921 if (kn)
fd7b9f7b
TH
922 return 0;
923 else
924 return -ENOENT;
925}
926
927/**
928 * kernfs_rename_ns - move and rename a kernfs_node
324a56e1 929 * @kn: target node
fd7b9f7b
TH
930 * @new_parent: new parent to put @sd under
931 * @new_name: new name
932 * @new_ns: new namespace tag
933 */
324a56e1 934int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
fd7b9f7b
TH
935 const char *new_name, const void *new_ns)
936{
937 int error;
938
d0ae3d43 939 error = -ENOENT;
ae34372e 940 if (!kernfs_get_active(new_parent))
d0ae3d43 941 goto out;
ae34372e
TH
942 if (!kernfs_get_active(kn))
943 goto out_put_new_parent;
944
945 mutex_lock(&kernfs_mutex);
d0ae3d43 946
fd7b9f7b 947 error = 0;
adc5e8b5
TH
948 if ((kn->parent == new_parent) && (kn->ns == new_ns) &&
949 (strcmp(kn->name, new_name) == 0))
ae34372e 950 goto out_unlock; /* nothing to rename */
fd7b9f7b
TH
951
952 error = -EEXIST;
953 if (kernfs_find_ns(new_parent, new_name, new_ns))
ae34372e 954 goto out_unlock;
fd7b9f7b 955
324a56e1 956 /* rename kernfs_node */
adc5e8b5 957 if (strcmp(kn->name, new_name) != 0) {
fd7b9f7b
TH
958 error = -ENOMEM;
959 new_name = kstrdup(new_name, GFP_KERNEL);
960 if (!new_name)
ae34372e 961 goto out_unlock;
fd7b9f7b 962
47a52e91
TH
963 if (kn->flags & KERNFS_STATIC_NAME)
964 kn->flags &= ~KERNFS_STATIC_NAME;
965 else
966 kfree(kn->name);
967
adc5e8b5 968 kn->name = new_name;
fd7b9f7b
TH
969 }
970
971 /*
972 * Move to the appropriate place in the appropriate directories rbtree.
973 */
c637b8ac 974 kernfs_unlink_sibling(kn);
fd7b9f7b 975 kernfs_get(new_parent);
adc5e8b5
TH
976 kernfs_put(kn->parent);
977 kn->ns = new_ns;
c637b8ac 978 kn->hash = kernfs_name_hash(kn->name, kn->ns);
adc5e8b5 979 kn->parent = new_parent;
c637b8ac 980 kernfs_link_sibling(kn);
fd7b9f7b
TH
981
982 error = 0;
ae34372e 983out_unlock:
a797bfc3 984 mutex_unlock(&kernfs_mutex);
ae34372e
TH
985 kernfs_put_active(kn);
986out_put_new_parent:
987 kernfs_put_active(new_parent);
988out:
fd7b9f7b
TH
989 return error;
990}
991
fd7b9f7b 992/* Relationship between s_mode and the DT_xxx types */
324a56e1 993static inline unsigned char dt_type(struct kernfs_node *kn)
fd7b9f7b 994{
adc5e8b5 995 return (kn->mode >> 12) & 15;
fd7b9f7b
TH
996}
997
c637b8ac 998static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
fd7b9f7b
TH
999{
1000 kernfs_put(filp->private_data);
1001 return 0;
1002}
1003
c637b8ac 1004static struct kernfs_node *kernfs_dir_pos(const void *ns,
324a56e1 1005 struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
fd7b9f7b
TH
1006{
1007 if (pos) {
ae34372e 1008 int valid = pos->parent == parent && hash == pos->hash;
fd7b9f7b
TH
1009 kernfs_put(pos);
1010 if (!valid)
1011 pos = NULL;
1012 }
1013 if (!pos && (hash > 1) && (hash < INT_MAX)) {
adc5e8b5 1014 struct rb_node *node = parent->dir.children.rb_node;
fd7b9f7b 1015 while (node) {
324a56e1 1016 pos = rb_to_kn(node);
fd7b9f7b 1017
adc5e8b5 1018 if (hash < pos->hash)
fd7b9f7b 1019 node = node->rb_left;
adc5e8b5 1020 else if (hash > pos->hash)
fd7b9f7b
TH
1021 node = node->rb_right;
1022 else
1023 break;
1024 }
1025 }
1026 /* Skip over entries in the wrong namespace */
adc5e8b5
TH
1027 while (pos && pos->ns != ns) {
1028 struct rb_node *node = rb_next(&pos->rb);
fd7b9f7b
TH
1029 if (!node)
1030 pos = NULL;
1031 else
324a56e1 1032 pos = rb_to_kn(node);
fd7b9f7b
TH
1033 }
1034 return pos;
1035}
1036
c637b8ac 1037static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
324a56e1 1038 struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
fd7b9f7b 1039{
c637b8ac 1040 pos = kernfs_dir_pos(ns, parent, ino, pos);
fd7b9f7b
TH
1041 if (pos)
1042 do {
adc5e8b5 1043 struct rb_node *node = rb_next(&pos->rb);
fd7b9f7b
TH
1044 if (!node)
1045 pos = NULL;
1046 else
324a56e1 1047 pos = rb_to_kn(node);
adc5e8b5 1048 } while (pos && pos->ns != ns);
fd7b9f7b
TH
1049 return pos;
1050}
1051
c637b8ac 1052static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
fd7b9f7b
TH
1053{
1054 struct dentry *dentry = file->f_path.dentry;
324a56e1
TH
1055 struct kernfs_node *parent = dentry->d_fsdata;
1056 struct kernfs_node *pos = file->private_data;
fd7b9f7b
TH
1057 const void *ns = NULL;
1058
1059 if (!dir_emit_dots(file, ctx))
1060 return 0;
a797bfc3 1061 mutex_lock(&kernfs_mutex);
fd7b9f7b 1062
324a56e1 1063 if (kernfs_ns_enabled(parent))
c525aadd 1064 ns = kernfs_info(dentry->d_sb)->ns;
fd7b9f7b 1065
c637b8ac 1066 for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos);
fd7b9f7b 1067 pos;
c637b8ac 1068 pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
adc5e8b5 1069 const char *name = pos->name;
fd7b9f7b
TH
1070 unsigned int type = dt_type(pos);
1071 int len = strlen(name);
adc5e8b5 1072 ino_t ino = pos->ino;
fd7b9f7b 1073
adc5e8b5 1074 ctx->pos = pos->hash;
fd7b9f7b
TH
1075 file->private_data = pos;
1076 kernfs_get(pos);
1077
a797bfc3 1078 mutex_unlock(&kernfs_mutex);
fd7b9f7b
TH
1079 if (!dir_emit(ctx, name, len, ino, type))
1080 return 0;
a797bfc3 1081 mutex_lock(&kernfs_mutex);
fd7b9f7b 1082 }
a797bfc3 1083 mutex_unlock(&kernfs_mutex);
fd7b9f7b
TH
1084 file->private_data = NULL;
1085 ctx->pos = INT_MAX;
1086 return 0;
1087}
1088
c637b8ac
TH
1089static loff_t kernfs_dir_fop_llseek(struct file *file, loff_t offset,
1090 int whence)
fd7b9f7b
TH
1091{
1092 struct inode *inode = file_inode(file);
1093 loff_t ret;
1094
1095 mutex_lock(&inode->i_mutex);
1096 ret = generic_file_llseek(file, offset, whence);
1097 mutex_unlock(&inode->i_mutex);
1098
1099 return ret;
1100}
1101
a797bfc3 1102const struct file_operations kernfs_dir_fops = {
fd7b9f7b 1103 .read = generic_read_dir,
c637b8ac
TH
1104 .iterate = kernfs_fop_readdir,
1105 .release = kernfs_dir_fop_release,
1106 .llseek = kernfs_dir_fop_llseek,
fd7b9f7b 1107};
This page took 0.169022 seconds and 5 git commands to generate.