OSDN Git Service

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