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