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