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