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