blob: 510b5062ef302184736503ec076d9ffafdb136aa [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
11#include <linux/fs.h>
12#include <linux/namei.h>
13#include <linux/idr.h>
14#include <linux/slab.h>
15#include <linux/security.h>
16#include <linux/hash.h>
17
18#include "kernfs-internal.h"
19
Tejun Heoa797bfc2013-12-11 14:11:57 -050020DEFINE_MUTEX(kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -050021
Tejun Heoadc5e8b2013-12-11 14:11:54 -050022#define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb)
Tejun Heofd7b9f72013-11-28 14:54:33 -050023
Tejun Heofd7b9f72013-11-28 14:54:33 -050024/**
Tejun Heoc637b8a2013-12-11 14:11:58 -050025 * kernfs_name_hash
Tejun Heofd7b9f72013-11-28 14:54:33 -050026 * @name: Null terminated string to hash
27 * @ns: Namespace tag to hash
28 *
29 * Returns 31 bit hash of ns + name (so it fits in an off_t )
30 */
Tejun Heoc637b8a2013-12-11 14:11:58 -050031static unsigned int kernfs_name_hash(const char *name, const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -050032{
33 unsigned long hash = init_name_hash();
34 unsigned int len = strlen(name);
35 while (len--)
36 hash = partial_name_hash(*name++, hash);
37 hash = (end_name_hash(hash) ^ hash_ptr((void *)ns, 31));
38 hash &= 0x7fffffffU;
39 /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */
40 if (hash < 1)
41 hash += 2;
42 if (hash >= INT_MAX)
43 hash = INT_MAX - 1;
44 return hash;
45}
46
Tejun Heoc637b8a2013-12-11 14:11:58 -050047static int kernfs_name_compare(unsigned int hash, const char *name,
48 const void *ns, const struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -050049{
Tejun Heoadc5e8b2013-12-11 14:11:54 -050050 if (hash != kn->hash)
51 return hash - kn->hash;
52 if (ns != kn->ns)
53 return ns - kn->ns;
54 return strcmp(name, kn->name);
Tejun Heofd7b9f72013-11-28 14:54:33 -050055}
56
Tejun Heoc637b8a2013-12-11 14:11:58 -050057static int kernfs_sd_compare(const struct kernfs_node *left,
58 const struct kernfs_node *right)
Tejun Heofd7b9f72013-11-28 14:54:33 -050059{
Tejun Heoc637b8a2013-12-11 14:11:58 -050060 return kernfs_name_compare(left->hash, left->name, left->ns, right);
Tejun Heofd7b9f72013-11-28 14:54:33 -050061}
62
63/**
Tejun Heoc637b8a2013-12-11 14:11:58 -050064 * kernfs_link_sibling - link kernfs_node into sibling rbtree
Tejun Heo324a56e2013-12-11 14:11:53 -050065 * @kn: kernfs_node of interest
Tejun Heofd7b9f72013-11-28 14:54:33 -050066 *
Tejun Heo324a56e2013-12-11 14:11:53 -050067 * Link @kn into its sibling rbtree which starts from
Tejun Heoadc5e8b2013-12-11 14:11:54 -050068 * @kn->parent->dir.children.
Tejun Heofd7b9f72013-11-28 14:54:33 -050069 *
70 * Locking:
Tejun Heoa797bfc2013-12-11 14:11:57 -050071 * mutex_lock(kernfs_mutex)
Tejun Heofd7b9f72013-11-28 14:54:33 -050072 *
73 * RETURNS:
74 * 0 on susccess -EEXIST on failure.
75 */
Tejun Heoc637b8a2013-12-11 14:11:58 -050076static int kernfs_link_sibling(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -050077{
Tejun Heoadc5e8b2013-12-11 14:11:54 -050078 struct rb_node **node = &kn->parent->dir.children.rb_node;
Tejun Heofd7b9f72013-11-28 14:54:33 -050079 struct rb_node *parent = NULL;
80
Tejun Heodf23fc32013-12-11 14:11:56 -050081 if (kernfs_type(kn) == KERNFS_DIR)
Tejun Heoadc5e8b2013-12-11 14:11:54 -050082 kn->parent->dir.subdirs++;
Tejun Heofd7b9f72013-11-28 14:54:33 -050083
84 while (*node) {
Tejun Heo324a56e2013-12-11 14:11:53 -050085 struct kernfs_node *pos;
Tejun Heofd7b9f72013-11-28 14:54:33 -050086 int result;
87
Tejun Heo324a56e2013-12-11 14:11:53 -050088 pos = rb_to_kn(*node);
Tejun Heofd7b9f72013-11-28 14:54:33 -050089 parent = *node;
Tejun Heoc637b8a2013-12-11 14:11:58 -050090 result = kernfs_sd_compare(kn, pos);
Tejun Heofd7b9f72013-11-28 14:54:33 -050091 if (result < 0)
Tejun Heoadc5e8b2013-12-11 14:11:54 -050092 node = &pos->rb.rb_left;
Tejun Heofd7b9f72013-11-28 14:54:33 -050093 else if (result > 0)
Tejun Heoadc5e8b2013-12-11 14:11:54 -050094 node = &pos->rb.rb_right;
Tejun Heofd7b9f72013-11-28 14:54:33 -050095 else
96 return -EEXIST;
97 }
98 /* add new node and rebalance the tree */
Tejun Heoadc5e8b2013-12-11 14:11:54 -050099 rb_link_node(&kn->rb, parent, node);
100 rb_insert_color(&kn->rb, &kn->parent->dir.children);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500101 return 0;
102}
103
104/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500105 * kernfs_unlink_sibling - unlink kernfs_node from sibling rbtree
Tejun Heo324a56e2013-12-11 14:11:53 -0500106 * @kn: kernfs_node of interest
Tejun Heofd7b9f72013-11-28 14:54:33 -0500107 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500108 * Unlink @kn from its sibling rbtree which starts from
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500109 * kn->parent->dir.children.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500110 *
111 * Locking:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500112 * mutex_lock(kernfs_mutex)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500113 */
Greg Kroah-Hartman55f6e302014-01-13 14:27:16 -0800114static void kernfs_unlink_sibling(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500115{
Tejun Heodf23fc32013-12-11 14:11:56 -0500116 if (kernfs_type(kn) == KERNFS_DIR)
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500117 kn->parent->dir.subdirs--;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500118
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500119 rb_erase(&kn->rb, &kn->parent->dir.children);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500120}
121
122/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500123 * kernfs_get_active - get an active reference to kernfs_node
Tejun Heo324a56e2013-12-11 14:11:53 -0500124 * @kn: kernfs_node to get an active reference to
Tejun Heofd7b9f72013-11-28 14:54:33 -0500125 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500126 * Get an active reference of @kn. This function is noop if @kn
Tejun Heofd7b9f72013-11-28 14:54:33 -0500127 * is NULL.
128 *
129 * RETURNS:
Tejun Heo324a56e2013-12-11 14:11:53 -0500130 * Pointer to @kn on success, NULL on failure.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500131 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500132struct kernfs_node *kernfs_get_active(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500133{
Tejun Heo324a56e2013-12-11 14:11:53 -0500134 if (unlikely(!kn))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500135 return NULL;
136
Greg Kroah-Hartmanf4b3e632014-01-13 14:13:39 -0800137 if (!atomic_inc_unless_negative(&kn->active))
138 return NULL;
139
Greg Kroah-Hartman08901472014-01-13 14:39:52 -0800140 if (kn->flags & KERNFS_LOCKDEP)
Tejun Heo324a56e2013-12-11 14:11:53 -0500141 rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_);
Greg Kroah-Hartmanf4b3e632014-01-13 14:13:39 -0800142 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500143}
144
145/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500146 * kernfs_put_active - put an active reference to kernfs_node
Tejun Heo324a56e2013-12-11 14:11:53 -0500147 * @kn: kernfs_node to put an active reference to
Tejun Heofd7b9f72013-11-28 14:54:33 -0500148 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500149 * Put an active reference to @kn. This function is noop if @kn
Tejun Heofd7b9f72013-11-28 14:54:33 -0500150 * is NULL.
151 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500152void kernfs_put_active(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500153{
154 int v;
155
Tejun Heo324a56e2013-12-11 14:11:53 -0500156 if (unlikely(!kn))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500157 return;
158
Greg Kroah-Hartman08901472014-01-13 14:39:52 -0800159 if (kn->flags & KERNFS_LOCKDEP)
Tejun Heo324a56e2013-12-11 14:11:53 -0500160 rwsem_release(&kn->dep_map, 1, _RET_IP_);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500161 v = atomic_dec_return(&kn->active);
Tejun Heodf23fc32013-12-11 14:11:56 -0500162 if (likely(v != KN_DEACTIVATED_BIAS))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500163 return;
164
Greg Kroah-Hartman87da1492014-01-13 14:43:11 -0800165 /*
166 * atomic_dec_return() is a mb(), we'll always see the updated
167 * kn->u.completion.
168 */
169 complete(kn->u.completion);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500170}
171
172/**
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800173 * kernfs_deactivate - deactivate kernfs_node
174 * @kn: kernfs_node to deactivate
Tejun Heofd7b9f72013-11-28 14:54:33 -0500175 *
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800176 * Deny new active references and drain existing ones.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500177 */
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800178static void kernfs_deactivate(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500179{
Greg Kroah-Hartman87da1492014-01-13 14:43:11 -0800180 DECLARE_COMPLETION_ONSTACK(wait);
181 int v;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500182
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800183 BUG_ON(!(kn->flags & KERNFS_REMOVED));
184
Greg Kroah-Hartman08901472014-01-13 14:39:52 -0800185 if (!(kernfs_type(kn) & KERNFS_ACTIVE_REF))
186 return;
187
Greg Kroah-Hartman87da1492014-01-13 14:43:11 -0800188 kn->u.completion = (void *)&wait;
189
Greg Kroah-Hartman08901472014-01-13 14:39:52 -0800190 rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_);
Greg Kroah-Hartman87da1492014-01-13 14:43:11 -0800191 /* atomic_add_return() is a mb(), put_active() will always see
192 * the updated kn->u.completion.
193 */
194 v = atomic_add_return(KN_DEACTIVATED_BIAS, &kn->active);
Greg Kroah-Hartman08901472014-01-13 14:39:52 -0800195
Greg Kroah-Hartman87da1492014-01-13 14:43:11 -0800196 if (v != KN_DEACTIVATED_BIAS) {
Greg Kroah-Hartman08901472014-01-13 14:39:52 -0800197 lock_contended(&kn->dep_map, _RET_IP_);
Greg Kroah-Hartman87da1492014-01-13 14:43:11 -0800198 wait_for_completion(&wait);
199 }
Tejun Heofd7b9f72013-11-28 14:54:33 -0500200
Greg Kroah-Hartman08901472014-01-13 14:39:52 -0800201 lock_acquired(&kn->dep_map, _RET_IP_);
202 rwsem_release(&kn->dep_map, 1, _RET_IP_);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500203}
204
Tejun Heofd7b9f72013-11-28 14:54:33 -0500205/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500206 * kernfs_get - get a reference count on a kernfs_node
207 * @kn: the target kernfs_node
Tejun Heofd7b9f72013-11-28 14:54:33 -0500208 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500209void kernfs_get(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500210{
Tejun Heo324a56e2013-12-11 14:11:53 -0500211 if (kn) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500212 WARN_ON(!atomic_read(&kn->count));
213 atomic_inc(&kn->count);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500214 }
215}
216EXPORT_SYMBOL_GPL(kernfs_get);
217
218/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500219 * kernfs_put - put a reference count on a kernfs_node
220 * @kn: the target kernfs_node
Tejun Heofd7b9f72013-11-28 14:54:33 -0500221 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500222 * Put a reference count of @kn and destroy it if it reached zero.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500223 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500224void kernfs_put(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500225{
Tejun Heo324a56e2013-12-11 14:11:53 -0500226 struct kernfs_node *parent;
Tejun Heoba7443b2013-11-28 14:54:40 -0500227 struct kernfs_root *root;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500228
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500229 if (!kn || !atomic_dec_and_test(&kn->count))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500230 return;
Tejun Heo324a56e2013-12-11 14:11:53 -0500231 root = kernfs_root(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500232 repeat:
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800233 /* Moving/renaming is always done while holding reference.
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500234 * kn->parent won't change beneath us.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500235 */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500236 parent = kn->parent;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500237
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800238 WARN(!(kn->flags & KERNFS_REMOVED), "kernfs: free using entry: %s/%s\n",
239 parent ? parent->name : "", kn->name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500240
Tejun Heodf23fc32013-12-11 14:11:56 -0500241 if (kernfs_type(kn) == KERNFS_LINK)
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500242 kernfs_put(kn->symlink.target_kn);
Tejun Heo2063d602013-12-11 16:02:57 -0500243 if (!(kn->flags & KERNFS_STATIC_NAME))
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500244 kfree(kn->name);
245 if (kn->iattr) {
246 if (kn->iattr->ia_secdata)
247 security_release_secctx(kn->iattr->ia_secdata,
248 kn->iattr->ia_secdata_len);
249 simple_xattrs_free(&kn->iattr->xattrs);
Tejun Heo23223922013-11-23 17:40:02 -0500250 }
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500251 kfree(kn->iattr);
252 ida_simple_remove(&root->ino_ida, kn->ino);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500253 kmem_cache_free(kernfs_node_cache, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500254
Tejun Heo324a56e2013-12-11 14:11:53 -0500255 kn = parent;
256 if (kn) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500257 if (atomic_dec_and_test(&kn->count))
Tejun Heoba7443b2013-11-28 14:54:40 -0500258 goto repeat;
259 } else {
Tejun Heo324a56e2013-12-11 14:11:53 -0500260 /* just released the root kn, free @root too */
Tejun Heobc755552013-11-28 14:54:41 -0500261 ida_destroy(&root->ino_ida);
Tejun Heoba7443b2013-11-28 14:54:40 -0500262 kfree(root);
263 }
Tejun Heofd7b9f72013-11-28 14:54:33 -0500264}
265EXPORT_SYMBOL_GPL(kernfs_put);
266
Tejun Heoc637b8a2013-12-11 14:11:58 -0500267static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500268{
Tejun Heo324a56e2013-12-11 14:11:53 -0500269 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500270
271 if (flags & LOOKUP_RCU)
272 return -ECHILD;
273
Tejun Heo19bbb922013-12-11 16:02:59 -0500274 /* Always perform fresh lookup for negatives */
275 if (!dentry->d_inode)
276 goto out_bad_unlocked;
277
Tejun Heo324a56e2013-12-11 14:11:53 -0500278 kn = dentry->d_fsdata;
Tejun Heoa797bfc2013-12-11 14:11:57 -0500279 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500280
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800281 /* The kernfs node has been deleted */
282 if (kn->flags & KERNFS_REMOVED)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500283 goto out_bad;
284
Tejun Heoc637b8a2013-12-11 14:11:58 -0500285 /* The kernfs node has been moved? */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500286 if (dentry->d_parent->d_fsdata != kn->parent)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500287 goto out_bad;
288
Tejun Heoc637b8a2013-12-11 14:11:58 -0500289 /* The kernfs node has been renamed */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500290 if (strcmp(dentry->d_name.name, kn->name) != 0)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500291 goto out_bad;
292
Tejun Heoc637b8a2013-12-11 14:11:58 -0500293 /* The kernfs node has been moved to a different namespace */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500294 if (kn->parent && kernfs_ns_enabled(kn->parent) &&
Tejun Heoc525aad2013-12-11 14:11:55 -0500295 kernfs_info(dentry->d_sb)->ns != kn->ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500296 goto out_bad;
297
Tejun Heoa797bfc2013-12-11 14:11:57 -0500298 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500299out_valid:
300 return 1;
301out_bad:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500302 mutex_unlock(&kernfs_mutex);
Tejun Heo19bbb922013-12-11 16:02:59 -0500303out_bad_unlocked:
304 /*
305 * @dentry doesn't match the underlying kernfs node, drop the
306 * dentry and force lookup. If we have submounts we must allow the
307 * vfs caches to lie about the state of the filesystem to prevent
308 * leaks and other nasty things, so use check_submounts_and_drop()
309 * instead of d_drop().
Tejun Heofd7b9f72013-11-28 14:54:33 -0500310 */
311 if (check_submounts_and_drop(dentry) != 0)
312 goto out_valid;
313
314 return 0;
315}
316
Tejun Heoc637b8a2013-12-11 14:11:58 -0500317static void kernfs_dop_release(struct dentry *dentry)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500318{
319 kernfs_put(dentry->d_fsdata);
320}
321
Tejun Heoa797bfc2013-12-11 14:11:57 -0500322const struct dentry_operations kernfs_dops = {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500323 .d_revalidate = kernfs_dop_revalidate,
Tejun Heoc637b8a2013-12-11 14:11:58 -0500324 .d_release = kernfs_dop_release,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500325};
326
Tejun Heoc637b8a2013-12-11 14:11:58 -0500327struct kernfs_node *kernfs_new_node(struct kernfs_root *root, const char *name,
Tejun Heo2063d602013-12-11 16:02:57 -0500328 umode_t mode, unsigned flags)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500329{
330 char *dup_name = NULL;
Tejun Heo324a56e2013-12-11 14:11:53 -0500331 struct kernfs_node *kn;
Tejun Heobc755552013-11-28 14:54:41 -0500332 int ret;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500333
Tejun Heo2063d602013-12-11 16:02:57 -0500334 if (!(flags & KERNFS_STATIC_NAME)) {
Tejun Heofd7b9f72013-11-28 14:54:33 -0500335 name = dup_name = kstrdup(name, GFP_KERNEL);
336 if (!name)
337 return NULL;
338 }
339
Tejun Heoa797bfc2013-12-11 14:11:57 -0500340 kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL);
Tejun Heo324a56e2013-12-11 14:11:53 -0500341 if (!kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500342 goto err_out1;
343
Tejun Heobc755552013-11-28 14:54:41 -0500344 ret = ida_simple_get(&root->ino_ida, 1, 0, GFP_KERNEL);
345 if (ret < 0)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500346 goto err_out2;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500347 kn->ino = ret;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500348
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500349 atomic_set(&kn->count, 1);
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800350 atomic_set(&kn->active, 0);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500351
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500352 kn->name = name;
353 kn->mode = mode;
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800354 kn->flags = flags | KERNFS_REMOVED;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500355
Tejun Heo324a56e2013-12-11 14:11:53 -0500356 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500357
358 err_out2:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500359 kmem_cache_free(kernfs_node_cache, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500360 err_out1:
361 kfree(dup_name);
362 return NULL;
363}
364
365/**
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800366 * kernfs_addrm_start - prepare for kernfs_node add/remove
367 * @acxt: pointer to kernfs_addrm_cxt to be used
368 *
369 * This function is called when the caller is about to add or remove
370 * kernfs_node. This function acquires kernfs_mutex. @acxt is used
371 * to keep and pass context to other addrm functions.
372 *
373 * LOCKING:
374 * Kernel thread context (may sleep). kernfs_mutex is locked on
375 * return.
376 */
377void kernfs_addrm_start(struct kernfs_addrm_cxt *acxt)
378 __acquires(kernfs_mutex)
379{
380 memset(acxt, 0, sizeof(*acxt));
381
382 mutex_lock(&kernfs_mutex);
383}
384
385/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500386 * kernfs_add_one - add kernfs_node to parent without warning
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800387 * @acxt: addrm context to use
Tejun Heo324a56e2013-12-11 14:11:53 -0500388 * @kn: kernfs_node to be added
389 * @parent: the parent kernfs_node to add @kn to
Tejun Heofd7b9f72013-11-28 14:54:33 -0500390 *
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500391 * Get @parent and set @kn->parent to it and increment nlink of the
392 * parent inode if @kn is a directory and link into the children list
393 * of the parent.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500394 *
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800395 * This function should be called between calls to
396 * kernfs_addrm_start() and kernfs_addrm_finish() and should be passed
397 * the same @acxt as passed to kernfs_addrm_start().
398 *
399 * LOCKING:
400 * Determined by kernfs_addrm_start().
401 *
Tejun Heofd7b9f72013-11-28 14:54:33 -0500402 * RETURNS:
403 * 0 on success, -EEXIST if entry with the given name already
404 * exists.
405 */
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800406int kernfs_add_one(struct kernfs_addrm_cxt *acxt, struct kernfs_node *kn,
407 struct kernfs_node *parent)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500408{
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800409 bool has_ns = kernfs_ns_enabled(parent);
Tejun Heoc525aad2013-12-11 14:11:55 -0500410 struct kernfs_iattrs *ps_iattr;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500411 int ret;
412
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800413 if (has_ns != (bool)kn->ns) {
414 WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
415 has_ns ? "required" : "invalid", parent->name, kn->name);
416 return -EINVAL;
417 }
Tejun Heofd7b9f72013-11-28 14:54:33 -0500418
Tejun Heodf23fc32013-12-11 14:11:56 -0500419 if (kernfs_type(parent) != KERNFS_DIR)
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800420 return -EINVAL;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500421
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800422 if (parent->flags & KERNFS_REMOVED)
423 return -ENOENT;
424
Tejun Heoc637b8a2013-12-11 14:11:58 -0500425 kn->hash = kernfs_name_hash(kn->name, kn->ns);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500426 kn->parent = parent;
Tejun Heo324a56e2013-12-11 14:11:53 -0500427 kernfs_get(parent);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500428
Tejun Heoc637b8a2013-12-11 14:11:58 -0500429 ret = kernfs_link_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500430 if (ret)
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800431 return ret;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500432
433 /* Update timestamps on the parent */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500434 ps_iattr = parent->iattr;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500435 if (ps_iattr) {
436 struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
437 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
438 }
439
440 /* Mark the entry added into directory tree */
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800441 kn->flags &= ~KERNFS_REMOVED;
442
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800443 return 0;
444}
445
446/**
Greg Kroah-Hartman4f4b1b62014-01-13 14:30:47 -0800447 * kernfs_remove_one - remove kernfs_node from parent
448 * @acxt: addrm context to use
449 * @kn: kernfs_node to be removed
450 *
451 * Mark @kn removed and drop nlink of parent inode if @kn is a
452 * directory. @kn is unlinked from the children list.
453 *
454 * This function should be called between calls to
455 * kernfs_addrm_start() and kernfs_addrm_finish() and should be
456 * passed the same @acxt as passed to kernfs_addrm_start().
457 *
458 * LOCKING:
459 * Determined by kernfs_addrm_start().
460 */
461static void kernfs_remove_one(struct kernfs_addrm_cxt *acxt,
462 struct kernfs_node *kn)
463{
464 struct kernfs_iattrs *ps_iattr;
465
466 /*
467 * Removal can be called multiple times on the same node. Only the
468 * first invocation is effective and puts the base ref.
469 */
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800470 if (kn->flags & KERNFS_REMOVED)
Greg Kroah-Hartman4f4b1b62014-01-13 14:30:47 -0800471 return;
472
473 if (kn->parent) {
474 kernfs_unlink_sibling(kn);
475
476 /* Update timestamps on the parent */
477 ps_iattr = kn->parent->iattr;
478 if (ps_iattr) {
479 ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME;
480 ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME;
481 }
482 }
483
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800484 kn->flags |= KERNFS_REMOVED;
Greg Kroah-Hartman4f4b1b62014-01-13 14:30:47 -0800485 kn->u.removed_list = acxt->removed;
486 acxt->removed = kn;
487}
488
489/**
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800490 * kernfs_addrm_finish - finish up kernfs_node add/remove
491 * @acxt: addrm context to finish up
492 *
493 * Finish up kernfs_node add/remove. Resources acquired by
494 * kernfs_addrm_start() are released and removed kernfs_nodes are
495 * cleaned up.
496 *
497 * LOCKING:
498 * kernfs_mutex is released.
499 */
500void kernfs_addrm_finish(struct kernfs_addrm_cxt *acxt)
501 __releases(kernfs_mutex)
502{
503 /* release resources acquired by kernfs_addrm_start() */
Tejun Heoa797bfc2013-12-11 14:11:57 -0500504 mutex_unlock(&kernfs_mutex);
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800505
506 /* kill removed kernfs_nodes */
507 while (acxt->removed) {
508 struct kernfs_node *kn = acxt->removed;
509
510 acxt->removed = kn->u.removed_list;
511
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800512 kernfs_deactivate(kn);
Greg Kroah-Hartman55f6e302014-01-13 14:27:16 -0800513 kernfs_unmap_bin_file(kn);
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800514 kernfs_put(kn);
515 }
Tejun Heofd7b9f72013-11-28 14:54:33 -0500516}
517
518/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500519 * kernfs_find_ns - find kernfs_node with the given name
520 * @parent: kernfs_node to search under
Tejun Heofd7b9f72013-11-28 14:54:33 -0500521 * @name: name to look for
522 * @ns: the namespace tag to use
523 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500524 * Look for kernfs_node with name @name under @parent. Returns pointer to
525 * the found kernfs_node on success, %NULL on failure.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500526 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500527static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
528 const unsigned char *name,
529 const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500530{
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500531 struct rb_node *node = parent->dir.children.rb_node;
Tejun Heoac9bba02013-11-29 17:19:09 -0500532 bool has_ns = kernfs_ns_enabled(parent);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500533 unsigned int hash;
534
Tejun Heoa797bfc2013-12-11 14:11:57 -0500535 lockdep_assert_held(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500536
537 if (has_ns != (bool)ns) {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500538 WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500539 has_ns ? "required" : "invalid", parent->name, name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500540 return NULL;
541 }
542
Tejun Heoc637b8a2013-12-11 14:11:58 -0500543 hash = kernfs_name_hash(name, ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500544 while (node) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500545 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500546 int result;
547
Tejun Heo324a56e2013-12-11 14:11:53 -0500548 kn = rb_to_kn(node);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500549 result = kernfs_name_compare(hash, name, ns, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500550 if (result < 0)
551 node = node->rb_left;
552 else if (result > 0)
553 node = node->rb_right;
554 else
Tejun Heo324a56e2013-12-11 14:11:53 -0500555 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500556 }
557 return NULL;
558}
559
560/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500561 * kernfs_find_and_get_ns - find and get kernfs_node with the given name
562 * @parent: kernfs_node to search under
Tejun Heofd7b9f72013-11-28 14:54:33 -0500563 * @name: name to look for
564 * @ns: the namespace tag to use
565 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500566 * Look for kernfs_node with name @name under @parent and get a reference
Tejun Heofd7b9f72013-11-28 14:54:33 -0500567 * if found. This function may sleep and returns pointer to the found
Tejun Heo324a56e2013-12-11 14:11:53 -0500568 * kernfs_node on success, %NULL on failure.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500569 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500570struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
571 const char *name, const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500572{
Tejun Heo324a56e2013-12-11 14:11:53 -0500573 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500574
Tejun Heoa797bfc2013-12-11 14:11:57 -0500575 mutex_lock(&kernfs_mutex);
Tejun Heo324a56e2013-12-11 14:11:53 -0500576 kn = kernfs_find_ns(parent, name, ns);
577 kernfs_get(kn);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500578 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500579
Tejun Heo324a56e2013-12-11 14:11:53 -0500580 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500581}
582EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
583
584/**
Tejun Heoba7443b2013-11-28 14:54:40 -0500585 * kernfs_create_root - create a new kernfs hierarchy
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500586 * @kdops: optional directory syscall operations for the hierarchy
Tejun Heoba7443b2013-11-28 14:54:40 -0500587 * @priv: opaque data associated with the new directory
588 *
589 * Returns the root of the new hierarchy on success, ERR_PTR() value on
590 * failure.
591 */
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500592struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops, void *priv)
Tejun Heoba7443b2013-11-28 14:54:40 -0500593{
594 struct kernfs_root *root;
Tejun Heo324a56e2013-12-11 14:11:53 -0500595 struct kernfs_node *kn;
Tejun Heoba7443b2013-11-28 14:54:40 -0500596
597 root = kzalloc(sizeof(*root), GFP_KERNEL);
598 if (!root)
599 return ERR_PTR(-ENOMEM);
600
Tejun Heobc755552013-11-28 14:54:41 -0500601 ida_init(&root->ino_ida);
602
Tejun Heoc637b8a2013-12-11 14:11:58 -0500603 kn = kernfs_new_node(root, "", S_IFDIR | S_IRUGO | S_IXUGO, KERNFS_DIR);
Tejun Heo324a56e2013-12-11 14:11:53 -0500604 if (!kn) {
Tejun Heobc755552013-11-28 14:54:41 -0500605 ida_destroy(&root->ino_ida);
Tejun Heoba7443b2013-11-28 14:54:40 -0500606 kfree(root);
607 return ERR_PTR(-ENOMEM);
608 }
609
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800610 kn->flags &= ~KERNFS_REMOVED;
Tejun Heo324a56e2013-12-11 14:11:53 -0500611 kn->priv = priv;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500612 kn->dir.root = root;
Tejun Heoba7443b2013-11-28 14:54:40 -0500613
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500614 root->dir_ops = kdops;
Tejun Heo324a56e2013-12-11 14:11:53 -0500615 root->kn = kn;
Tejun Heoba7443b2013-11-28 14:54:40 -0500616
617 return root;
618}
619
620/**
621 * kernfs_destroy_root - destroy a kernfs hierarchy
622 * @root: root of the hierarchy to destroy
623 *
624 * Destroy the hierarchy anchored at @root by removing all existing
625 * directories and destroying @root.
626 */
627void kernfs_destroy_root(struct kernfs_root *root)
628{
Tejun Heo324a56e2013-12-11 14:11:53 -0500629 kernfs_remove(root->kn); /* will also free @root */
Tejun Heoba7443b2013-11-28 14:54:40 -0500630}
631
632/**
Tejun Heofd7b9f72013-11-28 14:54:33 -0500633 * kernfs_create_dir_ns - create a directory
634 * @parent: parent in which to create a new directory
635 * @name: name of the new directory
Tejun Heobb8b9d02013-12-11 16:02:55 -0500636 * @mode: mode of the new directory
Tejun Heofd7b9f72013-11-28 14:54:33 -0500637 * @priv: opaque data associated with the new directory
638 * @ns: optional namespace tag of the directory
639 *
640 * Returns the created node on success, ERR_PTR() value on failure.
641 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500642struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
Tejun Heobb8b9d02013-12-11 16:02:55 -0500643 const char *name, umode_t mode,
644 void *priv, const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500645{
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800646 struct kernfs_addrm_cxt acxt;
Tejun Heo324a56e2013-12-11 14:11:53 -0500647 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500648 int rc;
649
650 /* allocate */
Tejun Heobb8b9d02013-12-11 16:02:55 -0500651 kn = kernfs_new_node(kernfs_root(parent), name, mode | S_IFDIR,
652 KERNFS_DIR);
Tejun Heo324a56e2013-12-11 14:11:53 -0500653 if (!kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500654 return ERR_PTR(-ENOMEM);
655
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500656 kn->dir.root = parent->dir.root;
657 kn->ns = ns;
Tejun Heo324a56e2013-12-11 14:11:53 -0500658 kn->priv = priv;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500659
660 /* link in */
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800661 kernfs_addrm_start(&acxt);
662 rc = kernfs_add_one(&acxt, kn, parent);
663 kernfs_addrm_finish(&acxt);
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800664
Tejun Heofd7b9f72013-11-28 14:54:33 -0500665 if (!rc)
Tejun Heo324a56e2013-12-11 14:11:53 -0500666 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500667
Tejun Heo324a56e2013-12-11 14:11:53 -0500668 kernfs_put(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500669 return ERR_PTR(rc);
670}
671
Tejun Heoc637b8a2013-12-11 14:11:58 -0500672static struct dentry *kernfs_iop_lookup(struct inode *dir,
673 struct dentry *dentry,
674 unsigned int flags)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500675{
Tejun Heo19bbb922013-12-11 16:02:59 -0500676 struct dentry *ret;
Tejun Heo324a56e2013-12-11 14:11:53 -0500677 struct kernfs_node *parent = dentry->d_parent->d_fsdata;
678 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500679 struct inode *inode;
680 const void *ns = NULL;
681
Tejun Heoa797bfc2013-12-11 14:11:57 -0500682 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500683
Tejun Heo324a56e2013-12-11 14:11:53 -0500684 if (kernfs_ns_enabled(parent))
Tejun Heoc525aad2013-12-11 14:11:55 -0500685 ns = kernfs_info(dir->i_sb)->ns;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500686
Tejun Heo324a56e2013-12-11 14:11:53 -0500687 kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500688
689 /* no such entry */
Tejun Heo324a56e2013-12-11 14:11:53 -0500690 if (!kn) {
Tejun Heo19bbb922013-12-11 16:02:59 -0500691 ret = NULL;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500692 goto out_unlock;
693 }
Tejun Heo324a56e2013-12-11 14:11:53 -0500694 kernfs_get(kn);
695 dentry->d_fsdata = kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500696
697 /* attach dentry and inode */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500698 inode = kernfs_get_inode(dir->i_sb, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500699 if (!inode) {
700 ret = ERR_PTR(-ENOMEM);
701 goto out_unlock;
702 }
703
704 /* instantiate and hash dentry */
705 ret = d_materialise_unique(dentry, inode);
706 out_unlock:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500707 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500708 return ret;
709}
710
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500711static int kernfs_iop_mkdir(struct inode *dir, struct dentry *dentry,
712 umode_t mode)
713{
714 struct kernfs_node *parent = dir->i_private;
715 struct kernfs_dir_ops *kdops = kernfs_root(parent)->dir_ops;
716
717 if (!kdops || !kdops->mkdir)
718 return -EPERM;
719
720 return kdops->mkdir(parent, dentry->d_name.name, mode);
721}
722
723static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
724{
725 struct kernfs_node *kn = dentry->d_fsdata;
726 struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops;
727
728 if (!kdops || !kdops->rmdir)
729 return -EPERM;
730
731 return kdops->rmdir(kn);
732}
733
734static int kernfs_iop_rename(struct inode *old_dir, struct dentry *old_dentry,
735 struct inode *new_dir, struct dentry *new_dentry)
736{
737 struct kernfs_node *kn = old_dentry->d_fsdata;
738 struct kernfs_node *new_parent = new_dir->i_private;
739 struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops;
740
741 if (!kdops || !kdops->rename)
742 return -EPERM;
743
744 return kdops->rename(kn, new_parent, new_dentry->d_name.name);
745}
746
Tejun Heoa797bfc2013-12-11 14:11:57 -0500747const struct inode_operations kernfs_dir_iops = {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500748 .lookup = kernfs_iop_lookup,
749 .permission = kernfs_iop_permission,
750 .setattr = kernfs_iop_setattr,
751 .getattr = kernfs_iop_getattr,
752 .setxattr = kernfs_iop_setxattr,
753 .removexattr = kernfs_iop_removexattr,
754 .getxattr = kernfs_iop_getxattr,
755 .listxattr = kernfs_iop_listxattr,
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500756
757 .mkdir = kernfs_iop_mkdir,
758 .rmdir = kernfs_iop_rmdir,
759 .rename = kernfs_iop_rename,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500760};
761
Tejun Heoc637b8a2013-12-11 14:11:58 -0500762static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500763{
Tejun Heo324a56e2013-12-11 14:11:53 -0500764 struct kernfs_node *last;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500765
766 while (true) {
767 struct rb_node *rbn;
768
769 last = pos;
770
Tejun Heodf23fc32013-12-11 14:11:56 -0500771 if (kernfs_type(pos) != KERNFS_DIR)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500772 break;
773
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500774 rbn = rb_first(&pos->dir.children);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500775 if (!rbn)
776 break;
777
Tejun Heo324a56e2013-12-11 14:11:53 -0500778 pos = rb_to_kn(rbn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500779 }
780
781 return last;
782}
783
784/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500785 * kernfs_next_descendant_post - find the next descendant for post-order walk
Tejun Heofd7b9f72013-11-28 14:54:33 -0500786 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo324a56e2013-12-11 14:11:53 -0500787 * @root: kernfs_node whose descendants to walk
Tejun Heofd7b9f72013-11-28 14:54:33 -0500788 *
789 * Find the next descendant to visit for post-order traversal of @root's
790 * descendants. @root is included in the iteration and the last node to be
791 * visited.
792 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500793static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
794 struct kernfs_node *root)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500795{
796 struct rb_node *rbn;
797
Tejun Heoa797bfc2013-12-11 14:11:57 -0500798 lockdep_assert_held(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500799
800 /* if first iteration, visit leftmost descendant which may be root */
801 if (!pos)
Tejun Heoc637b8a2013-12-11 14:11:58 -0500802 return kernfs_leftmost_descendant(root);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500803
804 /* if we visited @root, we're done */
805 if (pos == root)
806 return NULL;
807
808 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500809 rbn = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500810 if (rbn)
Tejun Heoc637b8a2013-12-11 14:11:58 -0500811 return kernfs_leftmost_descendant(rb_to_kn(rbn));
Tejun Heofd7b9f72013-11-28 14:54:33 -0500812
813 /* no sibling left, visit parent */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500814 return pos->parent;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500815}
816
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800817static void __kernfs_remove(struct kernfs_addrm_cxt *acxt,
818 struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500819{
Greg Kroah-Hartman4f4b1b62014-01-13 14:30:47 -0800820 struct kernfs_node *pos, *next;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500821
Greg Kroah-Hartmance9b4992014-01-13 13:50:31 -0800822 if (!kn)
823 return;
824
Tejun Heoc637b8a2013-12-11 14:11:58 -0500825 pr_debug("kernfs %s: removing\n", kn->name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500826
Greg Kroah-Hartman4f4b1b62014-01-13 14:30:47 -0800827 next = NULL;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500828 do {
Greg Kroah-Hartman4f4b1b62014-01-13 14:30:47 -0800829 pos = next;
830 next = kernfs_next_descendant_post(pos, kn);
831 if (pos)
832 kernfs_remove_one(acxt, pos);
833 } while (next);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500834}
835
836/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500837 * kernfs_remove - remove a kernfs_node recursively
838 * @kn: the kernfs_node to remove
Tejun Heofd7b9f72013-11-28 14:54:33 -0500839 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500840 * Remove @kn along with all its subdirectories and files.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500841 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500842void kernfs_remove(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500843{
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800844 struct kernfs_addrm_cxt acxt;
845
846 kernfs_addrm_start(&acxt);
847 __kernfs_remove(&acxt, kn);
848 kernfs_addrm_finish(&acxt);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500849}
850
851/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500852 * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
853 * @parent: parent of the target
854 * @name: name of the kernfs_node to remove
855 * @ns: namespace tag of the kernfs_node to remove
Tejun Heofd7b9f72013-11-28 14:54:33 -0500856 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500857 * Look for the kernfs_node with @name and @ns under @parent and remove it.
858 * Returns 0 on success, -ENOENT if such entry doesn't exist.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500859 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500860int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500861 const void *ns)
862{
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800863 struct kernfs_addrm_cxt acxt;
Tejun Heo324a56e2013-12-11 14:11:53 -0500864 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500865
Tejun Heo324a56e2013-12-11 14:11:53 -0500866 if (!parent) {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500867 WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
Tejun Heofd7b9f72013-11-28 14:54:33 -0500868 name);
869 return -ENOENT;
870 }
871
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800872 kernfs_addrm_start(&acxt);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500873
Tejun Heo324a56e2013-12-11 14:11:53 -0500874 kn = kernfs_find_ns(parent, name, ns);
875 if (kn)
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800876 __kernfs_remove(&acxt, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500877
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800878 kernfs_addrm_finish(&acxt);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500879
Tejun Heo324a56e2013-12-11 14:11:53 -0500880 if (kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500881 return 0;
882 else
883 return -ENOENT;
884}
885
886/**
887 * kernfs_rename_ns - move and rename a kernfs_node
Tejun Heo324a56e2013-12-11 14:11:53 -0500888 * @kn: target node
Tejun Heofd7b9f72013-11-28 14:54:33 -0500889 * @new_parent: new parent to put @sd under
890 * @new_name: new name
891 * @new_ns: new namespace tag
892 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500893int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500894 const char *new_name, const void *new_ns)
895{
896 int error;
897
Tejun Heoae343722014-01-10 08:57:21 -0500898 mutex_lock(&kernfs_mutex);
Tejun Heod0ae3d42013-12-11 16:02:56 -0500899
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800900 error = -ENOENT;
901 if ((kn->flags | new_parent->flags) & KERNFS_REMOVED)
902 goto out;
903
Tejun Heofd7b9f72013-11-28 14:54:33 -0500904 error = 0;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500905 if ((kn->parent == new_parent) && (kn->ns == new_ns) &&
906 (strcmp(kn->name, new_name) == 0))
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800907 goto out; /* nothing to rename */
Tejun Heofd7b9f72013-11-28 14:54:33 -0500908
909 error = -EEXIST;
910 if (kernfs_find_ns(new_parent, new_name, new_ns))
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800911 goto out;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500912
Tejun Heo324a56e2013-12-11 14:11:53 -0500913 /* rename kernfs_node */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500914 if (strcmp(kn->name, new_name) != 0) {
Tejun Heofd7b9f72013-11-28 14:54:33 -0500915 error = -ENOMEM;
916 new_name = kstrdup(new_name, GFP_KERNEL);
917 if (!new_name)
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800918 goto out;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500919
Tejun Heo47a52e92013-12-11 16:02:58 -0500920 if (kn->flags & KERNFS_STATIC_NAME)
921 kn->flags &= ~KERNFS_STATIC_NAME;
922 else
923 kfree(kn->name);
924
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500925 kn->name = new_name;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500926 }
927
928 /*
929 * Move to the appropriate place in the appropriate directories rbtree.
930 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500931 kernfs_unlink_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500932 kernfs_get(new_parent);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500933 kernfs_put(kn->parent);
934 kn->ns = new_ns;
Tejun Heoc637b8a2013-12-11 14:11:58 -0500935 kn->hash = kernfs_name_hash(kn->name, kn->ns);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500936 kn->parent = new_parent;
Tejun Heoc637b8a2013-12-11 14:11:58 -0500937 kernfs_link_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500938
939 error = 0;
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800940 out:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500941 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500942 return error;
943}
944
Tejun Heofd7b9f72013-11-28 14:54:33 -0500945/* Relationship between s_mode and the DT_xxx types */
Tejun Heo324a56e2013-12-11 14:11:53 -0500946static inline unsigned char dt_type(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500947{
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500948 return (kn->mode >> 12) & 15;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500949}
950
Tejun Heoc637b8a2013-12-11 14:11:58 -0500951static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500952{
953 kernfs_put(filp->private_data);
954 return 0;
955}
956
Tejun Heoc637b8a2013-12-11 14:11:58 -0500957static struct kernfs_node *kernfs_dir_pos(const void *ns,
Tejun Heo324a56e2013-12-11 14:11:53 -0500958 struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500959{
960 if (pos) {
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800961 int valid = !(pos->flags & KERNFS_REMOVED) &&
962 pos->parent == parent && hash == pos->hash;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500963 kernfs_put(pos);
964 if (!valid)
965 pos = NULL;
966 }
967 if (!pos && (hash > 1) && (hash < INT_MAX)) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500968 struct rb_node *node = parent->dir.children.rb_node;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500969 while (node) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500970 pos = rb_to_kn(node);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500971
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500972 if (hash < pos->hash)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500973 node = node->rb_left;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500974 else if (hash > pos->hash)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500975 node = node->rb_right;
976 else
977 break;
978 }
979 }
980 /* Skip over entries in the wrong namespace */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500981 while (pos && pos->ns != ns) {
982 struct rb_node *node = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500983 if (!node)
984 pos = NULL;
985 else
Tejun Heo324a56e2013-12-11 14:11:53 -0500986 pos = rb_to_kn(node);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500987 }
988 return pos;
989}
990
Tejun Heoc637b8a2013-12-11 14:11:58 -0500991static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
Tejun Heo324a56e2013-12-11 14:11:53 -0500992 struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500993{
Tejun Heoc637b8a2013-12-11 14:11:58 -0500994 pos = kernfs_dir_pos(ns, parent, ino, pos);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500995 if (pos)
996 do {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500997 struct rb_node *node = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500998 if (!node)
999 pos = NULL;
1000 else
Tejun Heo324a56e2013-12-11 14:11:53 -05001001 pos = rb_to_kn(node);
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001002 } while (pos && pos->ns != ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001003 return pos;
1004}
1005
Tejun Heoc637b8a2013-12-11 14:11:58 -05001006static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001007{
1008 struct dentry *dentry = file->f_path.dentry;
Tejun Heo324a56e2013-12-11 14:11:53 -05001009 struct kernfs_node *parent = dentry->d_fsdata;
1010 struct kernfs_node *pos = file->private_data;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001011 const void *ns = NULL;
1012
1013 if (!dir_emit_dots(file, ctx))
1014 return 0;
Tejun Heoa797bfc2013-12-11 14:11:57 -05001015 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001016
Tejun Heo324a56e2013-12-11 14:11:53 -05001017 if (kernfs_ns_enabled(parent))
Tejun Heoc525aad2013-12-11 14:11:55 -05001018 ns = kernfs_info(dentry->d_sb)->ns;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001019
Tejun Heoc637b8a2013-12-11 14:11:58 -05001020 for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001021 pos;
Tejun Heoc637b8a2013-12-11 14:11:58 -05001022 pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001023 const char *name = pos->name;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001024 unsigned int type = dt_type(pos);
1025 int len = strlen(name);
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001026 ino_t ino = pos->ino;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001027
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001028 ctx->pos = pos->hash;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001029 file->private_data = pos;
1030 kernfs_get(pos);
1031
Tejun Heoa797bfc2013-12-11 14:11:57 -05001032 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001033 if (!dir_emit(ctx, name, len, ino, type))
1034 return 0;
Tejun Heoa797bfc2013-12-11 14:11:57 -05001035 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001036 }
Tejun Heoa797bfc2013-12-11 14:11:57 -05001037 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001038 file->private_data = NULL;
1039 ctx->pos = INT_MAX;
1040 return 0;
1041}
1042
Tejun Heoc637b8a2013-12-11 14:11:58 -05001043static loff_t kernfs_dir_fop_llseek(struct file *file, loff_t offset,
1044 int whence)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001045{
1046 struct inode *inode = file_inode(file);
1047 loff_t ret;
1048
1049 mutex_lock(&inode->i_mutex);
1050 ret = generic_file_llseek(file, offset, whence);
1051 mutex_unlock(&inode->i_mutex);
1052
1053 return ret;
1054}
1055
Tejun Heoa797bfc2013-12-11 14:11:57 -05001056const struct file_operations kernfs_dir_fops = {
Tejun Heofd7b9f72013-11-28 14:54:33 -05001057 .read = generic_read_dir,
Tejun Heoc637b8a2013-12-11 14:11:58 -05001058 .iterate = kernfs_fop_readdir,
1059 .release = kernfs_dir_fop_release,
1060 .llseek = kernfs_dir_fop_llseek,
Tejun Heofd7b9f72013-11-28 14:54:33 -05001061};