blob: 3ac93737174af67f3b7888d478c973c60aec1efd [file] [log] [blame]
Tejun Heob8441ed2013-11-24 09:54:58 -05001/*
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 */
Tejun Heofd7b9f72013-11-28 14:54:33 -050010
Tejun Heoabd54f02014-02-03 14:02:55 -050011#include <linux/sched.h>
Tejun Heofd7b9f72013-11-28 14:54:33 -050012#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
Tejun Heoa797bfc2013-12-11 14:11:57 -050021DEFINE_MUTEX(kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -050022
Tejun Heoadc5e8b2013-12-11 14:11:54 -050023#define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb)
Tejun Heofd7b9f72013-11-28 14:54:33 -050024
Tejun Heofd7b9f72013-11-28 14:54:33 -050025/**
Tejun Heoc637b8a2013-12-11 14:11:58 -050026 * kernfs_name_hash
Tejun Heofd7b9f72013-11-28 14:54:33 -050027 * @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 */
Tejun Heoc637b8a2013-12-11 14:11:58 -050032static unsigned int kernfs_name_hash(const char *name, const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -050033{
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
Tejun Heoc637b8a2013-12-11 14:11:58 -050048static int kernfs_name_compare(unsigned int hash, const char *name,
49 const void *ns, const struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -050050{
Tejun Heoadc5e8b2013-12-11 14:11:54 -050051 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);
Tejun Heofd7b9f72013-11-28 14:54:33 -050056}
57
Tejun Heoc637b8a2013-12-11 14:11:58 -050058static int kernfs_sd_compare(const struct kernfs_node *left,
59 const struct kernfs_node *right)
Tejun Heofd7b9f72013-11-28 14:54:33 -050060{
Tejun Heoc637b8a2013-12-11 14:11:58 -050061 return kernfs_name_compare(left->hash, left->name, left->ns, right);
Tejun Heofd7b9f72013-11-28 14:54:33 -050062}
63
64/**
Tejun Heoc637b8a2013-12-11 14:11:58 -050065 * kernfs_link_sibling - link kernfs_node into sibling rbtree
Tejun Heo324a56e2013-12-11 14:11:53 -050066 * @kn: kernfs_node of interest
Tejun Heofd7b9f72013-11-28 14:54:33 -050067 *
Tejun Heo324a56e2013-12-11 14:11:53 -050068 * Link @kn into its sibling rbtree which starts from
Tejun Heoadc5e8b2013-12-11 14:11:54 -050069 * @kn->parent->dir.children.
Tejun Heofd7b9f72013-11-28 14:54:33 -050070 *
71 * Locking:
Tejun Heoa797bfc2013-12-11 14:11:57 -050072 * mutex_lock(kernfs_mutex)
Tejun Heofd7b9f72013-11-28 14:54:33 -050073 *
74 * RETURNS:
75 * 0 on susccess -EEXIST on failure.
76 */
Tejun Heoc637b8a2013-12-11 14:11:58 -050077static int kernfs_link_sibling(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -050078{
Tejun Heoadc5e8b2013-12-11 14:11:54 -050079 struct rb_node **node = &kn->parent->dir.children.rb_node;
Tejun Heofd7b9f72013-11-28 14:54:33 -050080 struct rb_node *parent = NULL;
81
Tejun Heodf23fc32013-12-11 14:11:56 -050082 if (kernfs_type(kn) == KERNFS_DIR)
Tejun Heoadc5e8b2013-12-11 14:11:54 -050083 kn->parent->dir.subdirs++;
Tejun Heofd7b9f72013-11-28 14:54:33 -050084
85 while (*node) {
Tejun Heo324a56e2013-12-11 14:11:53 -050086 struct kernfs_node *pos;
Tejun Heofd7b9f72013-11-28 14:54:33 -050087 int result;
88
Tejun Heo324a56e2013-12-11 14:11:53 -050089 pos = rb_to_kn(*node);
Tejun Heofd7b9f72013-11-28 14:54:33 -050090 parent = *node;
Tejun Heoc637b8a2013-12-11 14:11:58 -050091 result = kernfs_sd_compare(kn, pos);
Tejun Heofd7b9f72013-11-28 14:54:33 -050092 if (result < 0)
Tejun Heoadc5e8b2013-12-11 14:11:54 -050093 node = &pos->rb.rb_left;
Tejun Heofd7b9f72013-11-28 14:54:33 -050094 else if (result > 0)
Tejun Heoadc5e8b2013-12-11 14:11:54 -050095 node = &pos->rb.rb_right;
Tejun Heofd7b9f72013-11-28 14:54:33 -050096 else
97 return -EEXIST;
98 }
99 /* add new node and rebalance the tree */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500100 rb_link_node(&kn->rb, parent, node);
101 rb_insert_color(&kn->rb, &kn->parent->dir.children);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500102 return 0;
103}
104
105/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500106 * kernfs_unlink_sibling - unlink kernfs_node from sibling rbtree
Tejun Heo324a56e2013-12-11 14:11:53 -0500107 * @kn: kernfs_node of interest
Tejun Heofd7b9f72013-11-28 14:54:33 -0500108 *
Tejun Heo35beab02014-02-03 14:02:56 -0500109 * Try to unlink @kn from its sibling rbtree which starts from
110 * kn->parent->dir.children. Returns %true if @kn was actually
111 * removed, %false if @kn wasn't on the rbtree.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500112 *
113 * Locking:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500114 * mutex_lock(kernfs_mutex)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500115 */
Tejun Heo35beab02014-02-03 14:02:56 -0500116static bool kernfs_unlink_sibling(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500117{
Tejun Heo35beab02014-02-03 14:02:56 -0500118 if (RB_EMPTY_NODE(&kn->rb))
119 return false;
120
Tejun Heodf23fc32013-12-11 14:11:56 -0500121 if (kernfs_type(kn) == KERNFS_DIR)
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500122 kn->parent->dir.subdirs--;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500123
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500124 rb_erase(&kn->rb, &kn->parent->dir.children);
Tejun Heo35beab02014-02-03 14:02:56 -0500125 RB_CLEAR_NODE(&kn->rb);
126 return true;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500127}
128
129/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500130 * kernfs_get_active - get an active reference to kernfs_node
Tejun Heo324a56e2013-12-11 14:11:53 -0500131 * @kn: kernfs_node to get an active reference to
Tejun Heofd7b9f72013-11-28 14:54:33 -0500132 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500133 * Get an active reference of @kn. This function is noop if @kn
Tejun Heofd7b9f72013-11-28 14:54:33 -0500134 * is NULL.
135 *
136 * RETURNS:
Tejun Heo324a56e2013-12-11 14:11:53 -0500137 * Pointer to @kn on success, NULL on failure.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500138 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500139struct kernfs_node *kernfs_get_active(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500140{
Tejun Heo324a56e2013-12-11 14:11:53 -0500141 if (unlikely(!kn))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500142 return NULL;
143
Greg Kroah-Hartmanf4b3e632014-01-13 14:13:39 -0800144 if (!atomic_inc_unless_negative(&kn->active))
145 return NULL;
146
Greg Kroah-Hartman08901472014-01-13 14:39:52 -0800147 if (kn->flags & KERNFS_LOCKDEP)
Tejun Heo324a56e2013-12-11 14:11:53 -0500148 rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_);
Greg Kroah-Hartmanf4b3e632014-01-13 14:13:39 -0800149 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500150}
151
152/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500153 * kernfs_put_active - put an active reference to kernfs_node
Tejun Heo324a56e2013-12-11 14:11:53 -0500154 * @kn: kernfs_node to put an active reference to
Tejun Heofd7b9f72013-11-28 14:54:33 -0500155 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500156 * Put an active reference to @kn. This function is noop if @kn
Tejun Heofd7b9f72013-11-28 14:54:33 -0500157 * is NULL.
158 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500159void kernfs_put_active(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500160{
Tejun Heoabd54f02014-02-03 14:02:55 -0500161 struct kernfs_root *root = kernfs_root(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500162 int v;
163
Tejun Heo324a56e2013-12-11 14:11:53 -0500164 if (unlikely(!kn))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500165 return;
166
Greg Kroah-Hartman08901472014-01-13 14:39:52 -0800167 if (kn->flags & KERNFS_LOCKDEP)
Tejun Heo324a56e2013-12-11 14:11:53 -0500168 rwsem_release(&kn->dep_map, 1, _RET_IP_);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500169 v = atomic_dec_return(&kn->active);
Tejun Heodf23fc32013-12-11 14:11:56 -0500170 if (likely(v != KN_DEACTIVATED_BIAS))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500171 return;
172
Tejun Heoabd54f02014-02-03 14:02:55 -0500173 wake_up_all(&root->deactivate_waitq);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500174}
175
176/**
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800177 * kernfs_deactivate - deactivate kernfs_node
178 * @kn: kernfs_node to deactivate
Tejun Heofd7b9f72013-11-28 14:54:33 -0500179 *
Tejun Heo35beab02014-02-03 14:02:56 -0500180 * Deny new active references and drain existing ones. Mutiple
181 * removers may invoke this function concurrently on @kn and all will
182 * return after deactivation and draining are complete.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500183 */
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800184static void kernfs_deactivate(struct kernfs_node *kn)
Tejun Heo35beab02014-02-03 14:02:56 -0500185 __releases(&kernfs_mutex) __acquires(&kernfs_mutex)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500186{
Tejun Heoabd54f02014-02-03 14:02:55 -0500187 struct kernfs_root *root = kernfs_root(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500188
Tejun Heo35beab02014-02-03 14:02:56 -0500189 lockdep_assert_held(&kernfs_mutex);
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800190 BUG_ON(!(kn->flags & KERNFS_REMOVED));
191
Greg Kroah-Hartman08901472014-01-13 14:39:52 -0800192 if (!(kernfs_type(kn) & KERNFS_ACTIVE_REF))
193 return;
194
Tejun Heo35beab02014-02-03 14:02:56 -0500195 /* only the first invocation on @kn should deactivate it */
196 if (atomic_read(&kn->active) >= 0)
197 atomic_add(KN_DEACTIVATED_BIAS, &kn->active);
198
199 mutex_unlock(&kernfs_mutex);
200
201 if (kn->flags & KERNFS_LOCKDEP) {
Tejun Heoa6607932014-02-03 14:02:54 -0500202 rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_);
Tejun Heo35beab02014-02-03 14:02:56 -0500203 if (atomic_read(&kn->active) != KN_DEACTIVATED_BIAS)
204 lock_contended(&kn->dep_map, _RET_IP_);
205 }
Greg Kroah-Hartman08901472014-01-13 14:39:52 -0800206
Tejun Heo35beab02014-02-03 14:02:56 -0500207 /* but everyone should wait for draining */
Tejun Heoabd54f02014-02-03 14:02:55 -0500208 wait_event(root->deactivate_waitq,
209 atomic_read(&kn->active) == KN_DEACTIVATED_BIAS);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500210
Tejun Heoa6607932014-02-03 14:02:54 -0500211 if (kn->flags & KERNFS_LOCKDEP) {
212 lock_acquired(&kn->dep_map, _RET_IP_);
213 rwsem_release(&kn->dep_map, 1, _RET_IP_);
214 }
Tejun Heo35beab02014-02-03 14:02:56 -0500215
216 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500217}
218
Tejun Heofd7b9f72013-11-28 14:54:33 -0500219/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500220 * kernfs_get - get a reference count on a kernfs_node
221 * @kn: the target kernfs_node
Tejun Heofd7b9f72013-11-28 14:54:33 -0500222 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500223void kernfs_get(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500224{
Tejun Heo324a56e2013-12-11 14:11:53 -0500225 if (kn) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500226 WARN_ON(!atomic_read(&kn->count));
227 atomic_inc(&kn->count);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500228 }
229}
230EXPORT_SYMBOL_GPL(kernfs_get);
231
232/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500233 * kernfs_put - put a reference count on a kernfs_node
234 * @kn: the target kernfs_node
Tejun Heofd7b9f72013-11-28 14:54:33 -0500235 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500236 * Put a reference count of @kn and destroy it if it reached zero.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500237 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500238void kernfs_put(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500239{
Tejun Heo324a56e2013-12-11 14:11:53 -0500240 struct kernfs_node *parent;
Tejun Heoba7443b2013-11-28 14:54:40 -0500241 struct kernfs_root *root;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500242
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500243 if (!kn || !atomic_dec_and_test(&kn->count))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500244 return;
Tejun Heo324a56e2013-12-11 14:11:53 -0500245 root = kernfs_root(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500246 repeat:
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800247 /* Moving/renaming is always done while holding reference.
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500248 * kn->parent won't change beneath us.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500249 */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500250 parent = kn->parent;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500251
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800252 WARN(!(kn->flags & KERNFS_REMOVED), "kernfs: free using entry: %s/%s\n",
253 parent ? parent->name : "", kn->name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500254
Tejun Heodf23fc32013-12-11 14:11:56 -0500255 if (kernfs_type(kn) == KERNFS_LINK)
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500256 kernfs_put(kn->symlink.target_kn);
Tejun Heo2063d602013-12-11 16:02:57 -0500257 if (!(kn->flags & KERNFS_STATIC_NAME))
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500258 kfree(kn->name);
259 if (kn->iattr) {
260 if (kn->iattr->ia_secdata)
261 security_release_secctx(kn->iattr->ia_secdata,
262 kn->iattr->ia_secdata_len);
263 simple_xattrs_free(&kn->iattr->xattrs);
Tejun Heo23223922013-11-23 17:40:02 -0500264 }
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500265 kfree(kn->iattr);
266 ida_simple_remove(&root->ino_ida, kn->ino);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500267 kmem_cache_free(kernfs_node_cache, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500268
Tejun Heo324a56e2013-12-11 14:11:53 -0500269 kn = parent;
270 if (kn) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500271 if (atomic_dec_and_test(&kn->count))
Tejun Heoba7443b2013-11-28 14:54:40 -0500272 goto repeat;
273 } else {
Tejun Heo324a56e2013-12-11 14:11:53 -0500274 /* just released the root kn, free @root too */
Tejun Heobc755552013-11-28 14:54:41 -0500275 ida_destroy(&root->ino_ida);
Tejun Heoba7443b2013-11-28 14:54:40 -0500276 kfree(root);
277 }
Tejun Heofd7b9f72013-11-28 14:54:33 -0500278}
279EXPORT_SYMBOL_GPL(kernfs_put);
280
Tejun Heoc637b8a2013-12-11 14:11:58 -0500281static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500282{
Tejun Heo324a56e2013-12-11 14:11:53 -0500283 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500284
285 if (flags & LOOKUP_RCU)
286 return -ECHILD;
287
Tejun Heo19bbb922013-12-11 16:02:59 -0500288 /* Always perform fresh lookup for negatives */
289 if (!dentry->d_inode)
290 goto out_bad_unlocked;
291
Tejun Heo324a56e2013-12-11 14:11:53 -0500292 kn = dentry->d_fsdata;
Tejun Heoa797bfc2013-12-11 14:11:57 -0500293 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500294
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800295 /* The kernfs node has been deleted */
296 if (kn->flags & KERNFS_REMOVED)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500297 goto out_bad;
298
Tejun Heoc637b8a2013-12-11 14:11:58 -0500299 /* The kernfs node has been moved? */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500300 if (dentry->d_parent->d_fsdata != kn->parent)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500301 goto out_bad;
302
Tejun Heoc637b8a2013-12-11 14:11:58 -0500303 /* The kernfs node has been renamed */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500304 if (strcmp(dentry->d_name.name, kn->name) != 0)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500305 goto out_bad;
306
Tejun Heoc637b8a2013-12-11 14:11:58 -0500307 /* The kernfs node has been moved to a different namespace */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500308 if (kn->parent && kernfs_ns_enabled(kn->parent) &&
Tejun Heoc525aad2013-12-11 14:11:55 -0500309 kernfs_info(dentry->d_sb)->ns != kn->ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500310 goto out_bad;
311
Tejun Heoa797bfc2013-12-11 14:11:57 -0500312 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500313out_valid:
314 return 1;
315out_bad:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500316 mutex_unlock(&kernfs_mutex);
Tejun Heo19bbb922013-12-11 16:02:59 -0500317out_bad_unlocked:
318 /*
319 * @dentry doesn't match the underlying kernfs node, drop the
320 * dentry and force lookup. If we have submounts we must allow the
321 * vfs caches to lie about the state of the filesystem to prevent
322 * leaks and other nasty things, so use check_submounts_and_drop()
323 * instead of d_drop().
Tejun Heofd7b9f72013-11-28 14:54:33 -0500324 */
325 if (check_submounts_and_drop(dentry) != 0)
326 goto out_valid;
327
328 return 0;
329}
330
Tejun Heoc637b8a2013-12-11 14:11:58 -0500331static void kernfs_dop_release(struct dentry *dentry)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500332{
333 kernfs_put(dentry->d_fsdata);
334}
335
Tejun Heoa797bfc2013-12-11 14:11:57 -0500336const struct dentry_operations kernfs_dops = {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500337 .d_revalidate = kernfs_dop_revalidate,
Tejun Heoc637b8a2013-12-11 14:11:58 -0500338 .d_release = kernfs_dop_release,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500339};
340
Tejun Heodb4aad22014-01-17 09:58:25 -0500341static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
342 const char *name, umode_t mode,
343 unsigned flags)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500344{
345 char *dup_name = NULL;
Tejun Heo324a56e2013-12-11 14:11:53 -0500346 struct kernfs_node *kn;
Tejun Heobc755552013-11-28 14:54:41 -0500347 int ret;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500348
Tejun Heo2063d602013-12-11 16:02:57 -0500349 if (!(flags & KERNFS_STATIC_NAME)) {
Tejun Heofd7b9f72013-11-28 14:54:33 -0500350 name = dup_name = kstrdup(name, GFP_KERNEL);
351 if (!name)
352 return NULL;
353 }
354
Tejun Heoa797bfc2013-12-11 14:11:57 -0500355 kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL);
Tejun Heo324a56e2013-12-11 14:11:53 -0500356 if (!kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500357 goto err_out1;
358
Tejun Heobc755552013-11-28 14:54:41 -0500359 ret = ida_simple_get(&root->ino_ida, 1, 0, GFP_KERNEL);
360 if (ret < 0)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500361 goto err_out2;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500362 kn->ino = ret;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500363
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500364 atomic_set(&kn->count, 1);
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800365 atomic_set(&kn->active, 0);
Tejun Heo35beab02014-02-03 14:02:56 -0500366 RB_CLEAR_NODE(&kn->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500367
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500368 kn->name = name;
369 kn->mode = mode;
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800370 kn->flags = flags | KERNFS_REMOVED;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500371
Tejun Heo324a56e2013-12-11 14:11:53 -0500372 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500373
374 err_out2:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500375 kmem_cache_free(kernfs_node_cache, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500376 err_out1:
377 kfree(dup_name);
378 return NULL;
379}
380
Tejun Heodb4aad22014-01-17 09:58:25 -0500381struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
382 const char *name, umode_t mode,
383 unsigned flags)
384{
385 struct kernfs_node *kn;
386
387 kn = __kernfs_new_node(kernfs_root(parent), name, mode, flags);
388 if (kn) {
389 kernfs_get(parent);
390 kn->parent = parent;
391 }
392 return kn;
393}
394
Tejun Heofd7b9f72013-11-28 14:54:33 -0500395/**
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800396 * kernfs_addrm_start - prepare for kernfs_node add/remove
397 * @acxt: pointer to kernfs_addrm_cxt to be used
398 *
399 * This function is called when the caller is about to add or remove
400 * kernfs_node. This function acquires kernfs_mutex. @acxt is used
401 * to keep and pass context to other addrm functions.
402 *
403 * LOCKING:
404 * Kernel thread context (may sleep). kernfs_mutex is locked on
405 * return.
406 */
407void kernfs_addrm_start(struct kernfs_addrm_cxt *acxt)
408 __acquires(kernfs_mutex)
409{
410 memset(acxt, 0, sizeof(*acxt));
411
412 mutex_lock(&kernfs_mutex);
413}
414
415/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500416 * kernfs_add_one - add kernfs_node to parent without warning
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800417 * @acxt: addrm context to use
Tejun Heo324a56e2013-12-11 14:11:53 -0500418 * @kn: kernfs_node to be added
Tejun Heofd7b9f72013-11-28 14:54:33 -0500419 *
Tejun Heodb4aad22014-01-17 09:58:25 -0500420 * The caller must already have initialized @kn->parent. This
421 * function increments nlink of the parent's inode if @kn is a
422 * directory and link into the children list of the parent.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500423 *
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800424 * This function should be called between calls to
425 * kernfs_addrm_start() and kernfs_addrm_finish() and should be passed
426 * the same @acxt as passed to kernfs_addrm_start().
427 *
428 * LOCKING:
429 * Determined by kernfs_addrm_start().
430 *
Tejun Heofd7b9f72013-11-28 14:54:33 -0500431 * RETURNS:
432 * 0 on success, -EEXIST if entry with the given name already
433 * exists.
434 */
Tejun Heodb4aad22014-01-17 09:58:25 -0500435int kernfs_add_one(struct kernfs_addrm_cxt *acxt, struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500436{
Tejun Heodb4aad22014-01-17 09:58:25 -0500437 struct kernfs_node *parent = kn->parent;
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800438 bool has_ns = kernfs_ns_enabled(parent);
Tejun Heoc525aad2013-12-11 14:11:55 -0500439 struct kernfs_iattrs *ps_iattr;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500440 int ret;
441
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800442 if (has_ns != (bool)kn->ns) {
443 WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
444 has_ns ? "required" : "invalid", parent->name, kn->name);
445 return -EINVAL;
446 }
Tejun Heofd7b9f72013-11-28 14:54:33 -0500447
Tejun Heodf23fc32013-12-11 14:11:56 -0500448 if (kernfs_type(parent) != KERNFS_DIR)
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800449 return -EINVAL;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500450
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800451 if (parent->flags & KERNFS_REMOVED)
452 return -ENOENT;
453
Tejun Heoc637b8a2013-12-11 14:11:58 -0500454 kn->hash = kernfs_name_hash(kn->name, kn->ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500455
Tejun Heoc637b8a2013-12-11 14:11:58 -0500456 ret = kernfs_link_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500457 if (ret)
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800458 return ret;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500459
460 /* Update timestamps on the parent */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500461 ps_iattr = parent->iattr;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500462 if (ps_iattr) {
463 struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
464 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
465 }
466
467 /* Mark the entry added into directory tree */
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800468 kn->flags &= ~KERNFS_REMOVED;
469
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800470 return 0;
471}
472
473/**
474 * kernfs_addrm_finish - finish up kernfs_node add/remove
475 * @acxt: addrm context to finish up
476 *
477 * Finish up kernfs_node add/remove. Resources acquired by
478 * kernfs_addrm_start() are released and removed kernfs_nodes are
479 * cleaned up.
480 *
481 * LOCKING:
482 * kernfs_mutex is released.
483 */
484void kernfs_addrm_finish(struct kernfs_addrm_cxt *acxt)
485 __releases(kernfs_mutex)
486{
487 /* release resources acquired by kernfs_addrm_start() */
Tejun Heoa797bfc2013-12-11 14:11:57 -0500488 mutex_unlock(&kernfs_mutex);
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800489
490 /* kill removed kernfs_nodes */
491 while (acxt->removed) {
492 struct kernfs_node *kn = acxt->removed;
493
494 acxt->removed = kn->u.removed_list;
495
Greg Kroah-Hartman55f6e302014-01-13 14:27:16 -0800496 kernfs_unmap_bin_file(kn);
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800497 kernfs_put(kn);
498 }
Tejun Heofd7b9f72013-11-28 14:54:33 -0500499}
500
501/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500502 * kernfs_find_ns - find kernfs_node with the given name
503 * @parent: kernfs_node to search under
Tejun Heofd7b9f72013-11-28 14:54:33 -0500504 * @name: name to look for
505 * @ns: the namespace tag to use
506 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500507 * Look for kernfs_node with name @name under @parent. Returns pointer to
508 * the found kernfs_node on success, %NULL on failure.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500509 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500510static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
511 const unsigned char *name,
512 const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500513{
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500514 struct rb_node *node = parent->dir.children.rb_node;
Tejun Heoac9bba02013-11-29 17:19:09 -0500515 bool has_ns = kernfs_ns_enabled(parent);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500516 unsigned int hash;
517
Tejun Heoa797bfc2013-12-11 14:11:57 -0500518 lockdep_assert_held(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500519
520 if (has_ns != (bool)ns) {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500521 WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500522 has_ns ? "required" : "invalid", parent->name, name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500523 return NULL;
524 }
525
Tejun Heoc637b8a2013-12-11 14:11:58 -0500526 hash = kernfs_name_hash(name, ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500527 while (node) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500528 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500529 int result;
530
Tejun Heo324a56e2013-12-11 14:11:53 -0500531 kn = rb_to_kn(node);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500532 result = kernfs_name_compare(hash, name, ns, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500533 if (result < 0)
534 node = node->rb_left;
535 else if (result > 0)
536 node = node->rb_right;
537 else
Tejun Heo324a56e2013-12-11 14:11:53 -0500538 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500539 }
540 return NULL;
541}
542
543/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500544 * kernfs_find_and_get_ns - find and get kernfs_node with the given name
545 * @parent: kernfs_node to search under
Tejun Heofd7b9f72013-11-28 14:54:33 -0500546 * @name: name to look for
547 * @ns: the namespace tag to use
548 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500549 * Look for kernfs_node with name @name under @parent and get a reference
Tejun Heofd7b9f72013-11-28 14:54:33 -0500550 * if found. This function may sleep and returns pointer to the found
Tejun Heo324a56e2013-12-11 14:11:53 -0500551 * kernfs_node on success, %NULL on failure.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500552 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500553struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
554 const char *name, const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500555{
Tejun Heo324a56e2013-12-11 14:11:53 -0500556 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500557
Tejun Heoa797bfc2013-12-11 14:11:57 -0500558 mutex_lock(&kernfs_mutex);
Tejun Heo324a56e2013-12-11 14:11:53 -0500559 kn = kernfs_find_ns(parent, name, ns);
560 kernfs_get(kn);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500561 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500562
Tejun Heo324a56e2013-12-11 14:11:53 -0500563 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500564}
565EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
566
567/**
Tejun Heoba7443b2013-11-28 14:54:40 -0500568 * kernfs_create_root - create a new kernfs hierarchy
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500569 * @kdops: optional directory syscall operations for the hierarchy
Tejun Heoba7443b2013-11-28 14:54:40 -0500570 * @priv: opaque data associated with the new directory
571 *
572 * Returns the root of the new hierarchy on success, ERR_PTR() value on
573 * failure.
574 */
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500575struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops, void *priv)
Tejun Heoba7443b2013-11-28 14:54:40 -0500576{
577 struct kernfs_root *root;
Tejun Heo324a56e2013-12-11 14:11:53 -0500578 struct kernfs_node *kn;
Tejun Heoba7443b2013-11-28 14:54:40 -0500579
580 root = kzalloc(sizeof(*root), GFP_KERNEL);
581 if (!root)
582 return ERR_PTR(-ENOMEM);
583
Tejun Heobc755552013-11-28 14:54:41 -0500584 ida_init(&root->ino_ida);
585
Tejun Heodb4aad22014-01-17 09:58:25 -0500586 kn = __kernfs_new_node(root, "", S_IFDIR | S_IRUGO | S_IXUGO,
587 KERNFS_DIR);
Tejun Heo324a56e2013-12-11 14:11:53 -0500588 if (!kn) {
Tejun Heobc755552013-11-28 14:54:41 -0500589 ida_destroy(&root->ino_ida);
Tejun Heoba7443b2013-11-28 14:54:40 -0500590 kfree(root);
591 return ERR_PTR(-ENOMEM);
592 }
593
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800594 kn->flags &= ~KERNFS_REMOVED;
Tejun Heo324a56e2013-12-11 14:11:53 -0500595 kn->priv = priv;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500596 kn->dir.root = root;
Tejun Heoba7443b2013-11-28 14:54:40 -0500597
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500598 root->dir_ops = kdops;
Tejun Heo324a56e2013-12-11 14:11:53 -0500599 root->kn = kn;
Tejun Heoabd54f02014-02-03 14:02:55 -0500600 init_waitqueue_head(&root->deactivate_waitq);
Tejun Heoba7443b2013-11-28 14:54:40 -0500601
602 return root;
603}
604
605/**
606 * kernfs_destroy_root - destroy a kernfs hierarchy
607 * @root: root of the hierarchy to destroy
608 *
609 * Destroy the hierarchy anchored at @root by removing all existing
610 * directories and destroying @root.
611 */
612void kernfs_destroy_root(struct kernfs_root *root)
613{
Tejun Heo324a56e2013-12-11 14:11:53 -0500614 kernfs_remove(root->kn); /* will also free @root */
Tejun Heoba7443b2013-11-28 14:54:40 -0500615}
616
617/**
Tejun Heofd7b9f72013-11-28 14:54:33 -0500618 * kernfs_create_dir_ns - create a directory
619 * @parent: parent in which to create a new directory
620 * @name: name of the new directory
Tejun Heobb8b9d02013-12-11 16:02:55 -0500621 * @mode: mode of the new directory
Tejun Heofd7b9f72013-11-28 14:54:33 -0500622 * @priv: opaque data associated with the new directory
623 * @ns: optional namespace tag of the directory
624 *
625 * Returns the created node on success, ERR_PTR() value on failure.
626 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500627struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
Tejun Heobb8b9d02013-12-11 16:02:55 -0500628 const char *name, umode_t mode,
629 void *priv, const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500630{
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800631 struct kernfs_addrm_cxt acxt;
Tejun Heo324a56e2013-12-11 14:11:53 -0500632 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500633 int rc;
634
635 /* allocate */
Tejun Heodb4aad22014-01-17 09:58:25 -0500636 kn = kernfs_new_node(parent, name, mode | S_IFDIR, KERNFS_DIR);
Tejun Heo324a56e2013-12-11 14:11:53 -0500637 if (!kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500638 return ERR_PTR(-ENOMEM);
639
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500640 kn->dir.root = parent->dir.root;
641 kn->ns = ns;
Tejun Heo324a56e2013-12-11 14:11:53 -0500642 kn->priv = priv;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500643
644 /* link in */
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800645 kernfs_addrm_start(&acxt);
Tejun Heodb4aad22014-01-17 09:58:25 -0500646 rc = kernfs_add_one(&acxt, kn);
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800647 kernfs_addrm_finish(&acxt);
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800648
Tejun Heofd7b9f72013-11-28 14:54:33 -0500649 if (!rc)
Tejun Heo324a56e2013-12-11 14:11:53 -0500650 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500651
Tejun Heo324a56e2013-12-11 14:11:53 -0500652 kernfs_put(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500653 return ERR_PTR(rc);
654}
655
Tejun Heoc637b8a2013-12-11 14:11:58 -0500656static struct dentry *kernfs_iop_lookup(struct inode *dir,
657 struct dentry *dentry,
658 unsigned int flags)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500659{
Tejun Heo19bbb922013-12-11 16:02:59 -0500660 struct dentry *ret;
Tejun Heo324a56e2013-12-11 14:11:53 -0500661 struct kernfs_node *parent = dentry->d_parent->d_fsdata;
662 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500663 struct inode *inode;
664 const void *ns = NULL;
665
Tejun Heoa797bfc2013-12-11 14:11:57 -0500666 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500667
Tejun Heo324a56e2013-12-11 14:11:53 -0500668 if (kernfs_ns_enabled(parent))
Tejun Heoc525aad2013-12-11 14:11:55 -0500669 ns = kernfs_info(dir->i_sb)->ns;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500670
Tejun Heo324a56e2013-12-11 14:11:53 -0500671 kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500672
673 /* no such entry */
Tejun Heo324a56e2013-12-11 14:11:53 -0500674 if (!kn) {
Tejun Heo19bbb922013-12-11 16:02:59 -0500675 ret = NULL;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500676 goto out_unlock;
677 }
Tejun Heo324a56e2013-12-11 14:11:53 -0500678 kernfs_get(kn);
679 dentry->d_fsdata = kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500680
681 /* attach dentry and inode */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500682 inode = kernfs_get_inode(dir->i_sb, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500683 if (!inode) {
684 ret = ERR_PTR(-ENOMEM);
685 goto out_unlock;
686 }
687
688 /* instantiate and hash dentry */
689 ret = d_materialise_unique(dentry, inode);
690 out_unlock:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500691 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500692 return ret;
693}
694
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500695static int kernfs_iop_mkdir(struct inode *dir, struct dentry *dentry,
696 umode_t mode)
697{
698 struct kernfs_node *parent = dir->i_private;
699 struct kernfs_dir_ops *kdops = kernfs_root(parent)->dir_ops;
700
701 if (!kdops || !kdops->mkdir)
702 return -EPERM;
703
704 return kdops->mkdir(parent, dentry->d_name.name, mode);
705}
706
707static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
708{
709 struct kernfs_node *kn = dentry->d_fsdata;
710 struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops;
711
712 if (!kdops || !kdops->rmdir)
713 return -EPERM;
714
715 return kdops->rmdir(kn);
716}
717
718static int kernfs_iop_rename(struct inode *old_dir, struct dentry *old_dentry,
719 struct inode *new_dir, struct dentry *new_dentry)
720{
721 struct kernfs_node *kn = old_dentry->d_fsdata;
722 struct kernfs_node *new_parent = new_dir->i_private;
723 struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops;
724
725 if (!kdops || !kdops->rename)
726 return -EPERM;
727
728 return kdops->rename(kn, new_parent, new_dentry->d_name.name);
729}
730
Tejun Heoa797bfc2013-12-11 14:11:57 -0500731const struct inode_operations kernfs_dir_iops = {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500732 .lookup = kernfs_iop_lookup,
733 .permission = kernfs_iop_permission,
734 .setattr = kernfs_iop_setattr,
735 .getattr = kernfs_iop_getattr,
736 .setxattr = kernfs_iop_setxattr,
737 .removexattr = kernfs_iop_removexattr,
738 .getxattr = kernfs_iop_getxattr,
739 .listxattr = kernfs_iop_listxattr,
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500740
741 .mkdir = kernfs_iop_mkdir,
742 .rmdir = kernfs_iop_rmdir,
743 .rename = kernfs_iop_rename,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500744};
745
Tejun Heoc637b8a2013-12-11 14:11:58 -0500746static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500747{
Tejun Heo324a56e2013-12-11 14:11:53 -0500748 struct kernfs_node *last;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500749
750 while (true) {
751 struct rb_node *rbn;
752
753 last = pos;
754
Tejun Heodf23fc32013-12-11 14:11:56 -0500755 if (kernfs_type(pos) != KERNFS_DIR)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500756 break;
757
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500758 rbn = rb_first(&pos->dir.children);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500759 if (!rbn)
760 break;
761
Tejun Heo324a56e2013-12-11 14:11:53 -0500762 pos = rb_to_kn(rbn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500763 }
764
765 return last;
766}
767
768/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500769 * kernfs_next_descendant_post - find the next descendant for post-order walk
Tejun Heofd7b9f72013-11-28 14:54:33 -0500770 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo324a56e2013-12-11 14:11:53 -0500771 * @root: kernfs_node whose descendants to walk
Tejun Heofd7b9f72013-11-28 14:54:33 -0500772 *
773 * Find the next descendant to visit for post-order traversal of @root's
774 * descendants. @root is included in the iteration and the last node to be
775 * visited.
776 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500777static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
778 struct kernfs_node *root)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500779{
780 struct rb_node *rbn;
781
Tejun Heoa797bfc2013-12-11 14:11:57 -0500782 lockdep_assert_held(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500783
784 /* if first iteration, visit leftmost descendant which may be root */
785 if (!pos)
Tejun Heoc637b8a2013-12-11 14:11:58 -0500786 return kernfs_leftmost_descendant(root);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500787
788 /* if we visited @root, we're done */
789 if (pos == root)
790 return NULL;
791
792 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500793 rbn = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500794 if (rbn)
Tejun Heoc637b8a2013-12-11 14:11:58 -0500795 return kernfs_leftmost_descendant(rb_to_kn(rbn));
Tejun Heofd7b9f72013-11-28 14:54:33 -0500796
797 /* no sibling left, visit parent */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500798 return pos->parent;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500799}
800
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800801static void __kernfs_remove(struct kernfs_addrm_cxt *acxt,
802 struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500803{
Tejun Heo35beab02014-02-03 14:02:56 -0500804 struct kernfs_node *pos;
805
806 lockdep_assert_held(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500807
Greg Kroah-Hartmance9b4992014-01-13 13:50:31 -0800808 if (!kn)
809 return;
810
Tejun Heoc637b8a2013-12-11 14:11:58 -0500811 pr_debug("kernfs %s: removing\n", kn->name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500812
Tejun Heo35beab02014-02-03 14:02:56 -0500813 /* disable lookup and node creation under @kn */
814 pos = NULL;
815 while ((pos = kernfs_next_descendant_post(pos, kn)))
816 pos->flags |= KERNFS_REMOVED;
817
818 /* deactivate and unlink the subtree node-by-node */
Tejun Heofd7b9f72013-11-28 14:54:33 -0500819 do {
Tejun Heo35beab02014-02-03 14:02:56 -0500820 pos = kernfs_leftmost_descendant(kn);
821
822 /*
823 * kernfs_deactivate() drops kernfs_mutex temporarily and
824 * @pos's base ref could have been put by someone else by
825 * the time the function returns. Make sure it doesn't go
826 * away underneath us.
827 */
828 kernfs_get(pos);
829
830 kernfs_deactivate(pos);
831
832 /*
833 * kernfs_unlink_sibling() succeeds once per node. Use it
834 * to decide who's responsible for cleanups.
835 */
836 if (!pos->parent || kernfs_unlink_sibling(pos)) {
837 struct kernfs_iattrs *ps_iattr =
838 pos->parent ? pos->parent->iattr : NULL;
839
840 /* update timestamps on the parent */
841 if (ps_iattr) {
842 ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME;
843 ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME;
844 }
845
846 pos->u.removed_list = acxt->removed;
847 acxt->removed = pos;
848 }
849
850 kernfs_put(pos);
851 } while (pos != kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500852}
853
854/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500855 * kernfs_remove - remove a kernfs_node recursively
856 * @kn: the kernfs_node to remove
Tejun Heofd7b9f72013-11-28 14:54:33 -0500857 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500858 * Remove @kn along with all its subdirectories and files.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500859 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500860void kernfs_remove(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500861{
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800862 struct kernfs_addrm_cxt acxt;
863
864 kernfs_addrm_start(&acxt);
865 __kernfs_remove(&acxt, kn);
866 kernfs_addrm_finish(&acxt);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500867}
868
869/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500870 * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
871 * @parent: parent of the target
872 * @name: name of the kernfs_node to remove
873 * @ns: namespace tag of the kernfs_node to remove
Tejun Heofd7b9f72013-11-28 14:54:33 -0500874 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500875 * Look for the kernfs_node with @name and @ns under @parent and remove it.
876 * Returns 0 on success, -ENOENT if such entry doesn't exist.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500877 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500878int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500879 const void *ns)
880{
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800881 struct kernfs_addrm_cxt acxt;
Tejun Heo324a56e2013-12-11 14:11:53 -0500882 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500883
Tejun Heo324a56e2013-12-11 14:11:53 -0500884 if (!parent) {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500885 WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
Tejun Heofd7b9f72013-11-28 14:54:33 -0500886 name);
887 return -ENOENT;
888 }
889
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800890 kernfs_addrm_start(&acxt);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500891
Tejun Heo324a56e2013-12-11 14:11:53 -0500892 kn = kernfs_find_ns(parent, name, ns);
893 if (kn)
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800894 __kernfs_remove(&acxt, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500895
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800896 kernfs_addrm_finish(&acxt);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500897
Tejun Heo324a56e2013-12-11 14:11:53 -0500898 if (kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500899 return 0;
900 else
901 return -ENOENT;
902}
903
904/**
905 * kernfs_rename_ns - move and rename a kernfs_node
Tejun Heo324a56e2013-12-11 14:11:53 -0500906 * @kn: target node
Tejun Heofd7b9f72013-11-28 14:54:33 -0500907 * @new_parent: new parent to put @sd under
908 * @new_name: new name
909 * @new_ns: new namespace tag
910 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500911int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500912 const char *new_name, const void *new_ns)
913{
914 int error;
915
Tejun Heoae343722014-01-10 08:57:21 -0500916 mutex_lock(&kernfs_mutex);
Tejun Heod0ae3d42013-12-11 16:02:56 -0500917
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800918 error = -ENOENT;
919 if ((kn->flags | new_parent->flags) & KERNFS_REMOVED)
920 goto out;
921
Tejun Heofd7b9f72013-11-28 14:54:33 -0500922 error = 0;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500923 if ((kn->parent == new_parent) && (kn->ns == new_ns) &&
924 (strcmp(kn->name, new_name) == 0))
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800925 goto out; /* nothing to rename */
Tejun Heofd7b9f72013-11-28 14:54:33 -0500926
927 error = -EEXIST;
928 if (kernfs_find_ns(new_parent, new_name, new_ns))
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800929 goto out;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500930
Tejun Heo324a56e2013-12-11 14:11:53 -0500931 /* rename kernfs_node */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500932 if (strcmp(kn->name, new_name) != 0) {
Tejun Heofd7b9f72013-11-28 14:54:33 -0500933 error = -ENOMEM;
934 new_name = kstrdup(new_name, GFP_KERNEL);
935 if (!new_name)
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800936 goto out;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500937
Tejun Heo47a52e92013-12-11 16:02:58 -0500938 if (kn->flags & KERNFS_STATIC_NAME)
939 kn->flags &= ~KERNFS_STATIC_NAME;
940 else
941 kfree(kn->name);
942
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500943 kn->name = new_name;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500944 }
945
946 /*
947 * Move to the appropriate place in the appropriate directories rbtree.
948 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500949 kernfs_unlink_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500950 kernfs_get(new_parent);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500951 kernfs_put(kn->parent);
952 kn->ns = new_ns;
Tejun Heoc637b8a2013-12-11 14:11:58 -0500953 kn->hash = kernfs_name_hash(kn->name, kn->ns);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500954 kn->parent = new_parent;
Tejun Heoc637b8a2013-12-11 14:11:58 -0500955 kernfs_link_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500956
957 error = 0;
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800958 out:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500959 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500960 return error;
961}
962
Tejun Heofd7b9f72013-11-28 14:54:33 -0500963/* Relationship between s_mode and the DT_xxx types */
Tejun Heo324a56e2013-12-11 14:11:53 -0500964static inline unsigned char dt_type(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500965{
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500966 return (kn->mode >> 12) & 15;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500967}
968
Tejun Heoc637b8a2013-12-11 14:11:58 -0500969static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500970{
971 kernfs_put(filp->private_data);
972 return 0;
973}
974
Tejun Heoc637b8a2013-12-11 14:11:58 -0500975static struct kernfs_node *kernfs_dir_pos(const void *ns,
Tejun Heo324a56e2013-12-11 14:11:53 -0500976 struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500977{
978 if (pos) {
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800979 int valid = !(pos->flags & KERNFS_REMOVED) &&
980 pos->parent == parent && hash == pos->hash;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500981 kernfs_put(pos);
982 if (!valid)
983 pos = NULL;
984 }
985 if (!pos && (hash > 1) && (hash < INT_MAX)) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500986 struct rb_node *node = parent->dir.children.rb_node;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500987 while (node) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500988 pos = rb_to_kn(node);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500989
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500990 if (hash < pos->hash)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500991 node = node->rb_left;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500992 else if (hash > pos->hash)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500993 node = node->rb_right;
994 else
995 break;
996 }
997 }
998 /* Skip over entries in the wrong namespace */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500999 while (pos && pos->ns != ns) {
1000 struct rb_node *node = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001001 if (!node)
1002 pos = NULL;
1003 else
Tejun Heo324a56e2013-12-11 14:11:53 -05001004 pos = rb_to_kn(node);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001005 }
1006 return pos;
1007}
1008
Tejun Heoc637b8a2013-12-11 14:11:58 -05001009static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
Tejun Heo324a56e2013-12-11 14:11:53 -05001010 struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001011{
Tejun Heoc637b8a2013-12-11 14:11:58 -05001012 pos = kernfs_dir_pos(ns, parent, ino, pos);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001013 if (pos)
1014 do {
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001015 struct rb_node *node = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001016 if (!node)
1017 pos = NULL;
1018 else
Tejun Heo324a56e2013-12-11 14:11:53 -05001019 pos = rb_to_kn(node);
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001020 } while (pos && pos->ns != ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001021 return pos;
1022}
1023
Tejun Heoc637b8a2013-12-11 14:11:58 -05001024static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001025{
1026 struct dentry *dentry = file->f_path.dentry;
Tejun Heo324a56e2013-12-11 14:11:53 -05001027 struct kernfs_node *parent = dentry->d_fsdata;
1028 struct kernfs_node *pos = file->private_data;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001029 const void *ns = NULL;
1030
1031 if (!dir_emit_dots(file, ctx))
1032 return 0;
Tejun Heoa797bfc2013-12-11 14:11:57 -05001033 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001034
Tejun Heo324a56e2013-12-11 14:11:53 -05001035 if (kernfs_ns_enabled(parent))
Tejun Heoc525aad2013-12-11 14:11:55 -05001036 ns = kernfs_info(dentry->d_sb)->ns;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001037
Tejun Heoc637b8a2013-12-11 14:11:58 -05001038 for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001039 pos;
Tejun Heoc637b8a2013-12-11 14:11:58 -05001040 pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001041 const char *name = pos->name;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001042 unsigned int type = dt_type(pos);
1043 int len = strlen(name);
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001044 ino_t ino = pos->ino;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001045
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001046 ctx->pos = pos->hash;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001047 file->private_data = pos;
1048 kernfs_get(pos);
1049
Tejun Heoa797bfc2013-12-11 14:11:57 -05001050 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001051 if (!dir_emit(ctx, name, len, ino, type))
1052 return 0;
Tejun Heoa797bfc2013-12-11 14:11:57 -05001053 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001054 }
Tejun Heoa797bfc2013-12-11 14:11:57 -05001055 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001056 file->private_data = NULL;
1057 ctx->pos = INT_MAX;
1058 return 0;
1059}
1060
Tejun Heoc637b8a2013-12-11 14:11:58 -05001061static loff_t kernfs_dir_fop_llseek(struct file *file, loff_t offset,
1062 int whence)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001063{
1064 struct inode *inode = file_inode(file);
1065 loff_t ret;
1066
1067 mutex_lock(&inode->i_mutex);
1068 ret = generic_file_llseek(file, offset, whence);
1069 mutex_unlock(&inode->i_mutex);
1070
1071 return ret;
1072}
1073
Tejun Heoa797bfc2013-12-11 14:11:57 -05001074const struct file_operations kernfs_dir_fops = {
Tejun Heofd7b9f72013-11-28 14:54:33 -05001075 .read = generic_read_dir,
Tejun Heoc637b8a2013-12-11 14:11:58 -05001076 .iterate = kernfs_fop_readdir,
1077 .release = kernfs_dir_fop_release,
1078 .llseek = kernfs_dir_fop_llseek,
Tejun Heofd7b9f72013-11-28 14:54:33 -05001079};