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