blob: 2193d30156efeaf1bf07a772e9fee731f3d6f40d [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 Heo324a56e2013-12-11 14:11:53 -0500109 * Unlink @kn from its sibling rbtree which starts from
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500110 * kn->parent->dir.children.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500111 *
112 * Locking:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500113 * mutex_lock(kernfs_mutex)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500114 */
Greg Kroah-Hartman55f6e302014-01-13 14:27:16 -0800115static void kernfs_unlink_sibling(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500116{
Tejun Heodf23fc32013-12-11 14:11:56 -0500117 if (kernfs_type(kn) == KERNFS_DIR)
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500118 kn->parent->dir.subdirs--;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500119
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500120 rb_erase(&kn->rb, &kn->parent->dir.children);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500121}
122
123/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500124 * kernfs_get_active - get an active reference to kernfs_node
Tejun Heo324a56e2013-12-11 14:11:53 -0500125 * @kn: kernfs_node to get an active reference to
Tejun Heofd7b9f72013-11-28 14:54:33 -0500126 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500127 * Get an active reference of @kn. This function is noop if @kn
Tejun Heofd7b9f72013-11-28 14:54:33 -0500128 * is NULL.
129 *
130 * RETURNS:
Tejun Heo324a56e2013-12-11 14:11:53 -0500131 * Pointer to @kn on success, NULL on failure.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500132 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500133struct kernfs_node *kernfs_get_active(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500134{
Tejun Heo324a56e2013-12-11 14:11:53 -0500135 if (unlikely(!kn))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500136 return NULL;
137
Greg Kroah-Hartmanf4b3e632014-01-13 14:13:39 -0800138 if (!atomic_inc_unless_negative(&kn->active))
139 return NULL;
140
Greg Kroah-Hartman08901472014-01-13 14:39:52 -0800141 if (kn->flags & KERNFS_LOCKDEP)
Tejun Heo324a56e2013-12-11 14:11:53 -0500142 rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_);
Greg Kroah-Hartmanf4b3e632014-01-13 14:13:39 -0800143 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500144}
145
146/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500147 * kernfs_put_active - put an active reference to kernfs_node
Tejun Heo324a56e2013-12-11 14:11:53 -0500148 * @kn: kernfs_node to put an active reference to
Tejun Heofd7b9f72013-11-28 14:54:33 -0500149 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500150 * Put an active reference to @kn. This function is noop if @kn
Tejun Heofd7b9f72013-11-28 14:54:33 -0500151 * is NULL.
152 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500153void kernfs_put_active(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500154{
Tejun Heoabd54f02014-02-03 14:02:55 -0500155 struct kernfs_root *root = kernfs_root(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500156 int v;
157
Tejun Heo324a56e2013-12-11 14:11:53 -0500158 if (unlikely(!kn))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500159 return;
160
Greg Kroah-Hartman08901472014-01-13 14:39:52 -0800161 if (kn->flags & KERNFS_LOCKDEP)
Tejun Heo324a56e2013-12-11 14:11:53 -0500162 rwsem_release(&kn->dep_map, 1, _RET_IP_);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500163 v = atomic_dec_return(&kn->active);
Tejun Heodf23fc32013-12-11 14:11:56 -0500164 if (likely(v != KN_DEACTIVATED_BIAS))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500165 return;
166
Tejun Heoabd54f02014-02-03 14:02:55 -0500167 wake_up_all(&root->deactivate_waitq);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500168}
169
170/**
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800171 * kernfs_deactivate - deactivate kernfs_node
172 * @kn: kernfs_node to deactivate
Tejun Heofd7b9f72013-11-28 14:54:33 -0500173 *
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800174 * Deny new active references and drain existing ones.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500175 */
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800176static void kernfs_deactivate(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500177{
Tejun Heoabd54f02014-02-03 14:02:55 -0500178 struct kernfs_root *root = kernfs_root(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500179
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800180 BUG_ON(!(kn->flags & KERNFS_REMOVED));
181
Greg Kroah-Hartman08901472014-01-13 14:39:52 -0800182 if (!(kernfs_type(kn) & KERNFS_ACTIVE_REF))
183 return;
184
Tejun Heoa6607932014-02-03 14:02:54 -0500185 if (kn->flags & KERNFS_LOCKDEP)
186 rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_);
Greg Kroah-Hartman08901472014-01-13 14:39:52 -0800187
Tejun Heoabd54f02014-02-03 14:02:55 -0500188 atomic_add(KN_DEACTIVATED_BIAS, &kn->active);
189
190 if ((kn->flags & KERNFS_LOCKDEP) &&
191 atomic_read(&kn->active) != KN_DEACTIVATED_BIAS)
192 lock_contended(&kn->dep_map, _RET_IP_);
193
194 wait_event(root->deactivate_waitq,
195 atomic_read(&kn->active) == KN_DEACTIVATED_BIAS);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500196
Tejun Heoa6607932014-02-03 14:02:54 -0500197 if (kn->flags & KERNFS_LOCKDEP) {
198 lock_acquired(&kn->dep_map, _RET_IP_);
199 rwsem_release(&kn->dep_map, 1, _RET_IP_);
200 }
Tejun Heofd7b9f72013-11-28 14:54:33 -0500201}
202
Tejun Heofd7b9f72013-11-28 14:54:33 -0500203/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500204 * kernfs_get - get a reference count on a kernfs_node
205 * @kn: the target kernfs_node
Tejun Heofd7b9f72013-11-28 14:54:33 -0500206 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500207void kernfs_get(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500208{
Tejun Heo324a56e2013-12-11 14:11:53 -0500209 if (kn) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500210 WARN_ON(!atomic_read(&kn->count));
211 atomic_inc(&kn->count);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500212 }
213}
214EXPORT_SYMBOL_GPL(kernfs_get);
215
216/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500217 * kernfs_put - put a reference count on a kernfs_node
218 * @kn: the target kernfs_node
Tejun Heofd7b9f72013-11-28 14:54:33 -0500219 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500220 * Put a reference count of @kn and destroy it if it reached zero.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500221 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500222void kernfs_put(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500223{
Tejun Heo324a56e2013-12-11 14:11:53 -0500224 struct kernfs_node *parent;
Tejun Heoba7443b2013-11-28 14:54:40 -0500225 struct kernfs_root *root;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500226
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500227 if (!kn || !atomic_dec_and_test(&kn->count))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500228 return;
Tejun Heo324a56e2013-12-11 14:11:53 -0500229 root = kernfs_root(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500230 repeat:
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800231 /* Moving/renaming is always done while holding reference.
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500232 * kn->parent won't change beneath us.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500233 */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500234 parent = kn->parent;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500235
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800236 WARN(!(kn->flags & KERNFS_REMOVED), "kernfs: free using entry: %s/%s\n",
237 parent ? parent->name : "", kn->name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500238
Tejun Heodf23fc32013-12-11 14:11:56 -0500239 if (kernfs_type(kn) == KERNFS_LINK)
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500240 kernfs_put(kn->symlink.target_kn);
Tejun Heo2063d602013-12-11 16:02:57 -0500241 if (!(kn->flags & KERNFS_STATIC_NAME))
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500242 kfree(kn->name);
243 if (kn->iattr) {
244 if (kn->iattr->ia_secdata)
245 security_release_secctx(kn->iattr->ia_secdata,
246 kn->iattr->ia_secdata_len);
247 simple_xattrs_free(&kn->iattr->xattrs);
Tejun Heo23223922013-11-23 17:40:02 -0500248 }
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500249 kfree(kn->iattr);
250 ida_simple_remove(&root->ino_ida, kn->ino);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500251 kmem_cache_free(kernfs_node_cache, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500252
Tejun Heo324a56e2013-12-11 14:11:53 -0500253 kn = parent;
254 if (kn) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500255 if (atomic_dec_and_test(&kn->count))
Tejun Heoba7443b2013-11-28 14:54:40 -0500256 goto repeat;
257 } else {
Tejun Heo324a56e2013-12-11 14:11:53 -0500258 /* just released the root kn, free @root too */
Tejun Heobc755552013-11-28 14:54:41 -0500259 ida_destroy(&root->ino_ida);
Tejun Heoba7443b2013-11-28 14:54:40 -0500260 kfree(root);
261 }
Tejun Heofd7b9f72013-11-28 14:54:33 -0500262}
263EXPORT_SYMBOL_GPL(kernfs_put);
264
Tejun Heoc637b8a2013-12-11 14:11:58 -0500265static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500266{
Tejun Heo324a56e2013-12-11 14:11:53 -0500267 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500268
269 if (flags & LOOKUP_RCU)
270 return -ECHILD;
271
Tejun Heo19bbb922013-12-11 16:02:59 -0500272 /* Always perform fresh lookup for negatives */
273 if (!dentry->d_inode)
274 goto out_bad_unlocked;
275
Tejun Heo324a56e2013-12-11 14:11:53 -0500276 kn = dentry->d_fsdata;
Tejun Heoa797bfc2013-12-11 14:11:57 -0500277 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500278
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800279 /* The kernfs node has been deleted */
280 if (kn->flags & KERNFS_REMOVED)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500281 goto out_bad;
282
Tejun Heoc637b8a2013-12-11 14:11:58 -0500283 /* The kernfs node has been moved? */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500284 if (dentry->d_parent->d_fsdata != kn->parent)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500285 goto out_bad;
286
Tejun Heoc637b8a2013-12-11 14:11:58 -0500287 /* The kernfs node has been renamed */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500288 if (strcmp(dentry->d_name.name, kn->name) != 0)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500289 goto out_bad;
290
Tejun Heoc637b8a2013-12-11 14:11:58 -0500291 /* The kernfs node has been moved to a different namespace */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500292 if (kn->parent && kernfs_ns_enabled(kn->parent) &&
Tejun Heoc525aad2013-12-11 14:11:55 -0500293 kernfs_info(dentry->d_sb)->ns != kn->ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500294 goto out_bad;
295
Tejun Heoa797bfc2013-12-11 14:11:57 -0500296 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500297out_valid:
298 return 1;
299out_bad:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500300 mutex_unlock(&kernfs_mutex);
Tejun Heo19bbb922013-12-11 16:02:59 -0500301out_bad_unlocked:
302 /*
303 * @dentry doesn't match the underlying kernfs node, drop the
304 * dentry and force lookup. If we have submounts we must allow the
305 * vfs caches to lie about the state of the filesystem to prevent
306 * leaks and other nasty things, so use check_submounts_and_drop()
307 * instead of d_drop().
Tejun Heofd7b9f72013-11-28 14:54:33 -0500308 */
309 if (check_submounts_and_drop(dentry) != 0)
310 goto out_valid;
311
312 return 0;
313}
314
Tejun Heoc637b8a2013-12-11 14:11:58 -0500315static void kernfs_dop_release(struct dentry *dentry)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500316{
317 kernfs_put(dentry->d_fsdata);
318}
319
Tejun Heoa797bfc2013-12-11 14:11:57 -0500320const struct dentry_operations kernfs_dops = {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500321 .d_revalidate = kernfs_dop_revalidate,
Tejun Heoc637b8a2013-12-11 14:11:58 -0500322 .d_release = kernfs_dop_release,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500323};
324
Tejun Heodb4aad22014-01-17 09:58:25 -0500325static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
326 const char *name, umode_t mode,
327 unsigned flags)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500328{
329 char *dup_name = NULL;
Tejun Heo324a56e2013-12-11 14:11:53 -0500330 struct kernfs_node *kn;
Tejun Heobc755552013-11-28 14:54:41 -0500331 int ret;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500332
Tejun Heo2063d602013-12-11 16:02:57 -0500333 if (!(flags & KERNFS_STATIC_NAME)) {
Tejun Heofd7b9f72013-11-28 14:54:33 -0500334 name = dup_name = kstrdup(name, GFP_KERNEL);
335 if (!name)
336 return NULL;
337 }
338
Tejun Heoa797bfc2013-12-11 14:11:57 -0500339 kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL);
Tejun Heo324a56e2013-12-11 14:11:53 -0500340 if (!kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500341 goto err_out1;
342
Tejun Heobc755552013-11-28 14:54:41 -0500343 ret = ida_simple_get(&root->ino_ida, 1, 0, GFP_KERNEL);
344 if (ret < 0)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500345 goto err_out2;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500346 kn->ino = ret;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500347
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500348 atomic_set(&kn->count, 1);
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800349 atomic_set(&kn->active, 0);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500350
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500351 kn->name = name;
352 kn->mode = mode;
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800353 kn->flags = flags | KERNFS_REMOVED;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500354
Tejun Heo324a56e2013-12-11 14:11:53 -0500355 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500356
357 err_out2:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500358 kmem_cache_free(kernfs_node_cache, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500359 err_out1:
360 kfree(dup_name);
361 return NULL;
362}
363
Tejun Heodb4aad22014-01-17 09:58:25 -0500364struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
365 const char *name, umode_t mode,
366 unsigned flags)
367{
368 struct kernfs_node *kn;
369
370 kn = __kernfs_new_node(kernfs_root(parent), name, mode, flags);
371 if (kn) {
372 kernfs_get(parent);
373 kn->parent = parent;
374 }
375 return kn;
376}
377
Tejun Heofd7b9f72013-11-28 14:54:33 -0500378/**
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800379 * kernfs_addrm_start - prepare for kernfs_node add/remove
380 * @acxt: pointer to kernfs_addrm_cxt to be used
381 *
382 * This function is called when the caller is about to add or remove
383 * kernfs_node. This function acquires kernfs_mutex. @acxt is used
384 * to keep and pass context to other addrm functions.
385 *
386 * LOCKING:
387 * Kernel thread context (may sleep). kernfs_mutex is locked on
388 * return.
389 */
390void kernfs_addrm_start(struct kernfs_addrm_cxt *acxt)
391 __acquires(kernfs_mutex)
392{
393 memset(acxt, 0, sizeof(*acxt));
394
395 mutex_lock(&kernfs_mutex);
396}
397
398/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500399 * kernfs_add_one - add kernfs_node to parent without warning
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800400 * @acxt: addrm context to use
Tejun Heo324a56e2013-12-11 14:11:53 -0500401 * @kn: kernfs_node to be added
Tejun Heofd7b9f72013-11-28 14:54:33 -0500402 *
Tejun Heodb4aad22014-01-17 09:58:25 -0500403 * The caller must already have initialized @kn->parent. This
404 * function increments nlink of the parent's inode if @kn is a
405 * directory and link into the children list of the parent.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500406 *
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800407 * This function should be called between calls to
408 * kernfs_addrm_start() and kernfs_addrm_finish() and should be passed
409 * the same @acxt as passed to kernfs_addrm_start().
410 *
411 * LOCKING:
412 * Determined by kernfs_addrm_start().
413 *
Tejun Heofd7b9f72013-11-28 14:54:33 -0500414 * RETURNS:
415 * 0 on success, -EEXIST if entry with the given name already
416 * exists.
417 */
Tejun Heodb4aad22014-01-17 09:58:25 -0500418int kernfs_add_one(struct kernfs_addrm_cxt *acxt, struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500419{
Tejun Heodb4aad22014-01-17 09:58:25 -0500420 struct kernfs_node *parent = kn->parent;
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800421 bool has_ns = kernfs_ns_enabled(parent);
Tejun Heoc525aad2013-12-11 14:11:55 -0500422 struct kernfs_iattrs *ps_iattr;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500423 int ret;
424
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800425 if (has_ns != (bool)kn->ns) {
426 WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
427 has_ns ? "required" : "invalid", parent->name, kn->name);
428 return -EINVAL;
429 }
Tejun Heofd7b9f72013-11-28 14:54:33 -0500430
Tejun Heodf23fc32013-12-11 14:11:56 -0500431 if (kernfs_type(parent) != KERNFS_DIR)
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800432 return -EINVAL;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500433
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800434 if (parent->flags & KERNFS_REMOVED)
435 return -ENOENT;
436
Tejun Heoc637b8a2013-12-11 14:11:58 -0500437 kn->hash = kernfs_name_hash(kn->name, kn->ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500438
Tejun Heoc637b8a2013-12-11 14:11:58 -0500439 ret = kernfs_link_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500440 if (ret)
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800441 return ret;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500442
443 /* Update timestamps on the parent */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500444 ps_iattr = parent->iattr;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500445 if (ps_iattr) {
446 struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
447 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
448 }
449
450 /* Mark the entry added into directory tree */
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800451 kn->flags &= ~KERNFS_REMOVED;
452
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800453 return 0;
454}
455
456/**
Greg Kroah-Hartman4f4b1b62014-01-13 14:30:47 -0800457 * kernfs_remove_one - remove kernfs_node from parent
458 * @acxt: addrm context to use
459 * @kn: kernfs_node to be removed
460 *
461 * Mark @kn removed and drop nlink of parent inode if @kn is a
462 * directory. @kn is unlinked from the children list.
463 *
464 * This function should be called between calls to
465 * kernfs_addrm_start() and kernfs_addrm_finish() and should be
466 * passed the same @acxt as passed to kernfs_addrm_start().
467 *
468 * LOCKING:
469 * Determined by kernfs_addrm_start().
470 */
471static void kernfs_remove_one(struct kernfs_addrm_cxt *acxt,
472 struct kernfs_node *kn)
473{
474 struct kernfs_iattrs *ps_iattr;
475
476 /*
477 * Removal can be called multiple times on the same node. Only the
478 * first invocation is effective and puts the base ref.
479 */
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800480 if (kn->flags & KERNFS_REMOVED)
Greg Kroah-Hartman4f4b1b62014-01-13 14:30:47 -0800481 return;
482
483 if (kn->parent) {
484 kernfs_unlink_sibling(kn);
485
486 /* Update timestamps on the parent */
487 ps_iattr = kn->parent->iattr;
488 if (ps_iattr) {
489 ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME;
490 ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME;
491 }
492 }
493
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800494 kn->flags |= KERNFS_REMOVED;
Greg Kroah-Hartman4f4b1b62014-01-13 14:30:47 -0800495 kn->u.removed_list = acxt->removed;
496 acxt->removed = kn;
497}
498
499/**
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800500 * kernfs_addrm_finish - finish up kernfs_node add/remove
501 * @acxt: addrm context to finish up
502 *
503 * Finish up kernfs_node add/remove. Resources acquired by
504 * kernfs_addrm_start() are released and removed kernfs_nodes are
505 * cleaned up.
506 *
507 * LOCKING:
508 * kernfs_mutex is released.
509 */
510void kernfs_addrm_finish(struct kernfs_addrm_cxt *acxt)
511 __releases(kernfs_mutex)
512{
513 /* release resources acquired by kernfs_addrm_start() */
Tejun Heoa797bfc2013-12-11 14:11:57 -0500514 mutex_unlock(&kernfs_mutex);
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800515
516 /* kill removed kernfs_nodes */
517 while (acxt->removed) {
518 struct kernfs_node *kn = acxt->removed;
519
520 acxt->removed = kn->u.removed_list;
521
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800522 kernfs_deactivate(kn);
Greg Kroah-Hartman55f6e302014-01-13 14:27:16 -0800523 kernfs_unmap_bin_file(kn);
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800524 kernfs_put(kn);
525 }
Tejun Heofd7b9f72013-11-28 14:54:33 -0500526}
527
528/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500529 * kernfs_find_ns - find kernfs_node with the given name
530 * @parent: kernfs_node to search under
Tejun Heofd7b9f72013-11-28 14:54:33 -0500531 * @name: name to look for
532 * @ns: the namespace tag to use
533 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500534 * Look for kernfs_node with name @name under @parent. Returns pointer to
535 * the found kernfs_node on success, %NULL on failure.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500536 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500537static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
538 const unsigned char *name,
539 const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500540{
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500541 struct rb_node *node = parent->dir.children.rb_node;
Tejun Heoac9bba02013-11-29 17:19:09 -0500542 bool has_ns = kernfs_ns_enabled(parent);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500543 unsigned int hash;
544
Tejun Heoa797bfc2013-12-11 14:11:57 -0500545 lockdep_assert_held(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500546
547 if (has_ns != (bool)ns) {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500548 WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500549 has_ns ? "required" : "invalid", parent->name, name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500550 return NULL;
551 }
552
Tejun Heoc637b8a2013-12-11 14:11:58 -0500553 hash = kernfs_name_hash(name, ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500554 while (node) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500555 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500556 int result;
557
Tejun Heo324a56e2013-12-11 14:11:53 -0500558 kn = rb_to_kn(node);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500559 result = kernfs_name_compare(hash, name, ns, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500560 if (result < 0)
561 node = node->rb_left;
562 else if (result > 0)
563 node = node->rb_right;
564 else
Tejun Heo324a56e2013-12-11 14:11:53 -0500565 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500566 }
567 return NULL;
568}
569
570/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500571 * kernfs_find_and_get_ns - find and get kernfs_node with the given name
572 * @parent: kernfs_node to search under
Tejun Heofd7b9f72013-11-28 14:54:33 -0500573 * @name: name to look for
574 * @ns: the namespace tag to use
575 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500576 * Look for kernfs_node with name @name under @parent and get a reference
Tejun Heofd7b9f72013-11-28 14:54:33 -0500577 * if found. This function may sleep and returns pointer to the found
Tejun Heo324a56e2013-12-11 14:11:53 -0500578 * kernfs_node on success, %NULL on failure.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500579 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500580struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
581 const char *name, const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500582{
Tejun Heo324a56e2013-12-11 14:11:53 -0500583 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500584
Tejun Heoa797bfc2013-12-11 14:11:57 -0500585 mutex_lock(&kernfs_mutex);
Tejun Heo324a56e2013-12-11 14:11:53 -0500586 kn = kernfs_find_ns(parent, name, ns);
587 kernfs_get(kn);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500588 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500589
Tejun Heo324a56e2013-12-11 14:11:53 -0500590 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500591}
592EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
593
594/**
Tejun Heoba7443b2013-11-28 14:54:40 -0500595 * kernfs_create_root - create a new kernfs hierarchy
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500596 * @kdops: optional directory syscall operations for the hierarchy
Tejun Heoba7443b2013-11-28 14:54:40 -0500597 * @priv: opaque data associated with the new directory
598 *
599 * Returns the root of the new hierarchy on success, ERR_PTR() value on
600 * failure.
601 */
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500602struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops, void *priv)
Tejun Heoba7443b2013-11-28 14:54:40 -0500603{
604 struct kernfs_root *root;
Tejun Heo324a56e2013-12-11 14:11:53 -0500605 struct kernfs_node *kn;
Tejun Heoba7443b2013-11-28 14:54:40 -0500606
607 root = kzalloc(sizeof(*root), GFP_KERNEL);
608 if (!root)
609 return ERR_PTR(-ENOMEM);
610
Tejun Heobc755552013-11-28 14:54:41 -0500611 ida_init(&root->ino_ida);
612
Tejun Heodb4aad22014-01-17 09:58:25 -0500613 kn = __kernfs_new_node(root, "", S_IFDIR | S_IRUGO | S_IXUGO,
614 KERNFS_DIR);
Tejun Heo324a56e2013-12-11 14:11:53 -0500615 if (!kn) {
Tejun Heobc755552013-11-28 14:54:41 -0500616 ida_destroy(&root->ino_ida);
Tejun Heoba7443b2013-11-28 14:54:40 -0500617 kfree(root);
618 return ERR_PTR(-ENOMEM);
619 }
620
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800621 kn->flags &= ~KERNFS_REMOVED;
Tejun Heo324a56e2013-12-11 14:11:53 -0500622 kn->priv = priv;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500623 kn->dir.root = root;
Tejun Heoba7443b2013-11-28 14:54:40 -0500624
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500625 root->dir_ops = kdops;
Tejun Heo324a56e2013-12-11 14:11:53 -0500626 root->kn = kn;
Tejun Heoabd54f02014-02-03 14:02:55 -0500627 init_waitqueue_head(&root->deactivate_waitq);
Tejun Heoba7443b2013-11-28 14:54:40 -0500628
629 return root;
630}
631
632/**
633 * kernfs_destroy_root - destroy a kernfs hierarchy
634 * @root: root of the hierarchy to destroy
635 *
636 * Destroy the hierarchy anchored at @root by removing all existing
637 * directories and destroying @root.
638 */
639void kernfs_destroy_root(struct kernfs_root *root)
640{
Tejun Heo324a56e2013-12-11 14:11:53 -0500641 kernfs_remove(root->kn); /* will also free @root */
Tejun Heoba7443b2013-11-28 14:54:40 -0500642}
643
644/**
Tejun Heofd7b9f72013-11-28 14:54:33 -0500645 * kernfs_create_dir_ns - create a directory
646 * @parent: parent in which to create a new directory
647 * @name: name of the new directory
Tejun Heobb8b9d02013-12-11 16:02:55 -0500648 * @mode: mode of the new directory
Tejun Heofd7b9f72013-11-28 14:54:33 -0500649 * @priv: opaque data associated with the new directory
650 * @ns: optional namespace tag of the directory
651 *
652 * Returns the created node on success, ERR_PTR() value on failure.
653 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500654struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
Tejun Heobb8b9d02013-12-11 16:02:55 -0500655 const char *name, umode_t mode,
656 void *priv, const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500657{
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800658 struct kernfs_addrm_cxt acxt;
Tejun Heo324a56e2013-12-11 14:11:53 -0500659 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500660 int rc;
661
662 /* allocate */
Tejun Heodb4aad22014-01-17 09:58:25 -0500663 kn = kernfs_new_node(parent, name, mode | S_IFDIR, KERNFS_DIR);
Tejun Heo324a56e2013-12-11 14:11:53 -0500664 if (!kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500665 return ERR_PTR(-ENOMEM);
666
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500667 kn->dir.root = parent->dir.root;
668 kn->ns = ns;
Tejun Heo324a56e2013-12-11 14:11:53 -0500669 kn->priv = priv;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500670
671 /* link in */
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800672 kernfs_addrm_start(&acxt);
Tejun Heodb4aad22014-01-17 09:58:25 -0500673 rc = kernfs_add_one(&acxt, kn);
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800674 kernfs_addrm_finish(&acxt);
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800675
Tejun Heofd7b9f72013-11-28 14:54:33 -0500676 if (!rc)
Tejun Heo324a56e2013-12-11 14:11:53 -0500677 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500678
Tejun Heo324a56e2013-12-11 14:11:53 -0500679 kernfs_put(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500680 return ERR_PTR(rc);
681}
682
Tejun Heoc637b8a2013-12-11 14:11:58 -0500683static struct dentry *kernfs_iop_lookup(struct inode *dir,
684 struct dentry *dentry,
685 unsigned int flags)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500686{
Tejun Heo19bbb922013-12-11 16:02:59 -0500687 struct dentry *ret;
Tejun Heo324a56e2013-12-11 14:11:53 -0500688 struct kernfs_node *parent = dentry->d_parent->d_fsdata;
689 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500690 struct inode *inode;
691 const void *ns = NULL;
692
Tejun Heoa797bfc2013-12-11 14:11:57 -0500693 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500694
Tejun Heo324a56e2013-12-11 14:11:53 -0500695 if (kernfs_ns_enabled(parent))
Tejun Heoc525aad2013-12-11 14:11:55 -0500696 ns = kernfs_info(dir->i_sb)->ns;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500697
Tejun Heo324a56e2013-12-11 14:11:53 -0500698 kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500699
700 /* no such entry */
Tejun Heo324a56e2013-12-11 14:11:53 -0500701 if (!kn) {
Tejun Heo19bbb922013-12-11 16:02:59 -0500702 ret = NULL;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500703 goto out_unlock;
704 }
Tejun Heo324a56e2013-12-11 14:11:53 -0500705 kernfs_get(kn);
706 dentry->d_fsdata = kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500707
708 /* attach dentry and inode */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500709 inode = kernfs_get_inode(dir->i_sb, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500710 if (!inode) {
711 ret = ERR_PTR(-ENOMEM);
712 goto out_unlock;
713 }
714
715 /* instantiate and hash dentry */
716 ret = d_materialise_unique(dentry, inode);
717 out_unlock:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500718 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500719 return ret;
720}
721
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500722static int kernfs_iop_mkdir(struct inode *dir, struct dentry *dentry,
723 umode_t mode)
724{
725 struct kernfs_node *parent = dir->i_private;
726 struct kernfs_dir_ops *kdops = kernfs_root(parent)->dir_ops;
727
728 if (!kdops || !kdops->mkdir)
729 return -EPERM;
730
731 return kdops->mkdir(parent, dentry->d_name.name, mode);
732}
733
734static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
735{
736 struct kernfs_node *kn = dentry->d_fsdata;
737 struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops;
738
739 if (!kdops || !kdops->rmdir)
740 return -EPERM;
741
742 return kdops->rmdir(kn);
743}
744
745static int kernfs_iop_rename(struct inode *old_dir, struct dentry *old_dentry,
746 struct inode *new_dir, struct dentry *new_dentry)
747{
748 struct kernfs_node *kn = old_dentry->d_fsdata;
749 struct kernfs_node *new_parent = new_dir->i_private;
750 struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops;
751
752 if (!kdops || !kdops->rename)
753 return -EPERM;
754
755 return kdops->rename(kn, new_parent, new_dentry->d_name.name);
756}
757
Tejun Heoa797bfc2013-12-11 14:11:57 -0500758const struct inode_operations kernfs_dir_iops = {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500759 .lookup = kernfs_iop_lookup,
760 .permission = kernfs_iop_permission,
761 .setattr = kernfs_iop_setattr,
762 .getattr = kernfs_iop_getattr,
763 .setxattr = kernfs_iop_setxattr,
764 .removexattr = kernfs_iop_removexattr,
765 .getxattr = kernfs_iop_getxattr,
766 .listxattr = kernfs_iop_listxattr,
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500767
768 .mkdir = kernfs_iop_mkdir,
769 .rmdir = kernfs_iop_rmdir,
770 .rename = kernfs_iop_rename,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500771};
772
Tejun Heoc637b8a2013-12-11 14:11:58 -0500773static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500774{
Tejun Heo324a56e2013-12-11 14:11:53 -0500775 struct kernfs_node *last;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500776
777 while (true) {
778 struct rb_node *rbn;
779
780 last = pos;
781
Tejun Heodf23fc32013-12-11 14:11:56 -0500782 if (kernfs_type(pos) != KERNFS_DIR)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500783 break;
784
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500785 rbn = rb_first(&pos->dir.children);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500786 if (!rbn)
787 break;
788
Tejun Heo324a56e2013-12-11 14:11:53 -0500789 pos = rb_to_kn(rbn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500790 }
791
792 return last;
793}
794
795/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500796 * kernfs_next_descendant_post - find the next descendant for post-order walk
Tejun Heofd7b9f72013-11-28 14:54:33 -0500797 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo324a56e2013-12-11 14:11:53 -0500798 * @root: kernfs_node whose descendants to walk
Tejun Heofd7b9f72013-11-28 14:54:33 -0500799 *
800 * Find the next descendant to visit for post-order traversal of @root's
801 * descendants. @root is included in the iteration and the last node to be
802 * visited.
803 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500804static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
805 struct kernfs_node *root)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500806{
807 struct rb_node *rbn;
808
Tejun Heoa797bfc2013-12-11 14:11:57 -0500809 lockdep_assert_held(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500810
811 /* if first iteration, visit leftmost descendant which may be root */
812 if (!pos)
Tejun Heoc637b8a2013-12-11 14:11:58 -0500813 return kernfs_leftmost_descendant(root);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500814
815 /* if we visited @root, we're done */
816 if (pos == root)
817 return NULL;
818
819 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500820 rbn = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500821 if (rbn)
Tejun Heoc637b8a2013-12-11 14:11:58 -0500822 return kernfs_leftmost_descendant(rb_to_kn(rbn));
Tejun Heofd7b9f72013-11-28 14:54:33 -0500823
824 /* no sibling left, visit parent */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500825 return pos->parent;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500826}
827
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800828static void __kernfs_remove(struct kernfs_addrm_cxt *acxt,
829 struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500830{
Greg Kroah-Hartman4f4b1b62014-01-13 14:30:47 -0800831 struct kernfs_node *pos, *next;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500832
Greg Kroah-Hartmance9b4992014-01-13 13:50:31 -0800833 if (!kn)
834 return;
835
Tejun Heoc637b8a2013-12-11 14:11:58 -0500836 pr_debug("kernfs %s: removing\n", kn->name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500837
Greg Kroah-Hartman4f4b1b62014-01-13 14:30:47 -0800838 next = NULL;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500839 do {
Greg Kroah-Hartman4f4b1b62014-01-13 14:30:47 -0800840 pos = next;
841 next = kernfs_next_descendant_post(pos, kn);
842 if (pos)
843 kernfs_remove_one(acxt, pos);
844 } while (next);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500845}
846
847/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500848 * kernfs_remove - remove a kernfs_node recursively
849 * @kn: the kernfs_node to remove
Tejun Heofd7b9f72013-11-28 14:54:33 -0500850 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500851 * Remove @kn along with all its subdirectories and files.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500852 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500853void kernfs_remove(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500854{
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800855 struct kernfs_addrm_cxt acxt;
856
857 kernfs_addrm_start(&acxt);
858 __kernfs_remove(&acxt, kn);
859 kernfs_addrm_finish(&acxt);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500860}
861
862/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500863 * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
864 * @parent: parent of the target
865 * @name: name of the kernfs_node to remove
866 * @ns: namespace tag of the kernfs_node to remove
Tejun Heofd7b9f72013-11-28 14:54:33 -0500867 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500868 * Look for the kernfs_node with @name and @ns under @parent and remove it.
869 * Returns 0 on success, -ENOENT if such entry doesn't exist.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500870 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500871int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500872 const void *ns)
873{
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800874 struct kernfs_addrm_cxt acxt;
Tejun Heo324a56e2013-12-11 14:11:53 -0500875 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500876
Tejun Heo324a56e2013-12-11 14:11:53 -0500877 if (!parent) {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500878 WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
Tejun Heofd7b9f72013-11-28 14:54:33 -0500879 name);
880 return -ENOENT;
881 }
882
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800883 kernfs_addrm_start(&acxt);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500884
Tejun Heo324a56e2013-12-11 14:11:53 -0500885 kn = kernfs_find_ns(parent, name, ns);
886 if (kn)
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800887 __kernfs_remove(&acxt, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500888
Greg Kroah-Hartman7653fe92014-01-13 14:20:56 -0800889 kernfs_addrm_finish(&acxt);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500890
Tejun Heo324a56e2013-12-11 14:11:53 -0500891 if (kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500892 return 0;
893 else
894 return -ENOENT;
895}
896
897/**
898 * kernfs_rename_ns - move and rename a kernfs_node
Tejun Heo324a56e2013-12-11 14:11:53 -0500899 * @kn: target node
Tejun Heofd7b9f72013-11-28 14:54:33 -0500900 * @new_parent: new parent to put @sd under
901 * @new_name: new name
902 * @new_ns: new namespace tag
903 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500904int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500905 const char *new_name, const void *new_ns)
906{
907 int error;
908
Tejun Heoae343722014-01-10 08:57:21 -0500909 mutex_lock(&kernfs_mutex);
Tejun Heod0ae3d42013-12-11 16:02:56 -0500910
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800911 error = -ENOENT;
912 if ((kn->flags | new_parent->flags) & KERNFS_REMOVED)
913 goto out;
914
Tejun Heofd7b9f72013-11-28 14:54:33 -0500915 error = 0;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500916 if ((kn->parent == new_parent) && (kn->ns == new_ns) &&
917 (strcmp(kn->name, new_name) == 0))
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800918 goto out; /* nothing to rename */
Tejun Heofd7b9f72013-11-28 14:54:33 -0500919
920 error = -EEXIST;
921 if (kernfs_find_ns(new_parent, new_name, new_ns))
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800922 goto out;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500923
Tejun Heo324a56e2013-12-11 14:11:53 -0500924 /* rename kernfs_node */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500925 if (strcmp(kn->name, new_name) != 0) {
Tejun Heofd7b9f72013-11-28 14:54:33 -0500926 error = -ENOMEM;
927 new_name = kstrdup(new_name, GFP_KERNEL);
928 if (!new_name)
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800929 goto out;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500930
Tejun Heo47a52e92013-12-11 16:02:58 -0500931 if (kn->flags & KERNFS_STATIC_NAME)
932 kn->flags &= ~KERNFS_STATIC_NAME;
933 else
934 kfree(kn->name);
935
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500936 kn->name = new_name;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500937 }
938
939 /*
940 * Move to the appropriate place in the appropriate directories rbtree.
941 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500942 kernfs_unlink_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500943 kernfs_get(new_parent);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500944 kernfs_put(kn->parent);
945 kn->ns = new_ns;
Tejun Heoc637b8a2013-12-11 14:11:58 -0500946 kn->hash = kernfs_name_hash(kn->name, kn->ns);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500947 kn->parent = new_parent;
Tejun Heoc637b8a2013-12-11 14:11:58 -0500948 kernfs_link_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500949
950 error = 0;
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800951 out:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500952 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500953 return error;
954}
955
Tejun Heofd7b9f72013-11-28 14:54:33 -0500956/* Relationship between s_mode and the DT_xxx types */
Tejun Heo324a56e2013-12-11 14:11:53 -0500957static inline unsigned char dt_type(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500958{
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500959 return (kn->mode >> 12) & 15;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500960}
961
Tejun Heoc637b8a2013-12-11 14:11:58 -0500962static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500963{
964 kernfs_put(filp->private_data);
965 return 0;
966}
967
Tejun Heoc637b8a2013-12-11 14:11:58 -0500968static struct kernfs_node *kernfs_dir_pos(const void *ns,
Tejun Heo324a56e2013-12-11 14:11:53 -0500969 struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500970{
971 if (pos) {
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800972 int valid = !(pos->flags & KERNFS_REMOVED) &&
973 pos->parent == parent && hash == pos->hash;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500974 kernfs_put(pos);
975 if (!valid)
976 pos = NULL;
977 }
978 if (!pos && (hash > 1) && (hash < INT_MAX)) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500979 struct rb_node *node = parent->dir.children.rb_node;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500980 while (node) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500981 pos = rb_to_kn(node);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500982
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500983 if (hash < pos->hash)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500984 node = node->rb_left;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500985 else if (hash > pos->hash)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500986 node = node->rb_right;
987 else
988 break;
989 }
990 }
991 /* Skip over entries in the wrong namespace */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500992 while (pos && pos->ns != ns) {
993 struct rb_node *node = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500994 if (!node)
995 pos = NULL;
996 else
Tejun Heo324a56e2013-12-11 14:11:53 -0500997 pos = rb_to_kn(node);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500998 }
999 return pos;
1000}
1001
Tejun Heoc637b8a2013-12-11 14:11:58 -05001002static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
Tejun Heo324a56e2013-12-11 14:11:53 -05001003 struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001004{
Tejun Heoc637b8a2013-12-11 14:11:58 -05001005 pos = kernfs_dir_pos(ns, parent, ino, pos);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001006 if (pos)
1007 do {
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001008 struct rb_node *node = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001009 if (!node)
1010 pos = NULL;
1011 else
Tejun Heo324a56e2013-12-11 14:11:53 -05001012 pos = rb_to_kn(node);
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001013 } while (pos && pos->ns != ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001014 return pos;
1015}
1016
Tejun Heoc637b8a2013-12-11 14:11:58 -05001017static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001018{
1019 struct dentry *dentry = file->f_path.dentry;
Tejun Heo324a56e2013-12-11 14:11:53 -05001020 struct kernfs_node *parent = dentry->d_fsdata;
1021 struct kernfs_node *pos = file->private_data;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001022 const void *ns = NULL;
1023
1024 if (!dir_emit_dots(file, ctx))
1025 return 0;
Tejun Heoa797bfc2013-12-11 14:11:57 -05001026 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001027
Tejun Heo324a56e2013-12-11 14:11:53 -05001028 if (kernfs_ns_enabled(parent))
Tejun Heoc525aad2013-12-11 14:11:55 -05001029 ns = kernfs_info(dentry->d_sb)->ns;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001030
Tejun Heoc637b8a2013-12-11 14:11:58 -05001031 for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001032 pos;
Tejun Heoc637b8a2013-12-11 14:11:58 -05001033 pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001034 const char *name = pos->name;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001035 unsigned int type = dt_type(pos);
1036 int len = strlen(name);
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001037 ino_t ino = pos->ino;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001038
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001039 ctx->pos = pos->hash;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001040 file->private_data = pos;
1041 kernfs_get(pos);
1042
Tejun Heoa797bfc2013-12-11 14:11:57 -05001043 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001044 if (!dir_emit(ctx, name, len, ino, type))
1045 return 0;
Tejun Heoa797bfc2013-12-11 14:11:57 -05001046 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001047 }
Tejun Heoa797bfc2013-12-11 14:11:57 -05001048 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001049 file->private_data = NULL;
1050 ctx->pos = INT_MAX;
1051 return 0;
1052}
1053
Tejun Heoc637b8a2013-12-11 14:11:58 -05001054static loff_t kernfs_dir_fop_llseek(struct file *file, loff_t offset,
1055 int whence)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001056{
1057 struct inode *inode = file_inode(file);
1058 loff_t ret;
1059
1060 mutex_lock(&inode->i_mutex);
1061 ret = generic_file_llseek(file, offset, whence);
1062 mutex_unlock(&inode->i_mutex);
1063
1064 return ret;
1065}
1066
Tejun Heoa797bfc2013-12-11 14:11:57 -05001067const struct file_operations kernfs_dir_fops = {
Tejun Heofd7b9f72013-11-28 14:54:33 -05001068 .read = generic_read_dir,
Tejun Heoc637b8a2013-12-11 14:11:58 -05001069 .iterate = kernfs_fop_readdir,
1070 .release = kernfs_dir_fop_release,
1071 .llseek = kernfs_dir_fop_llseek,
Tejun Heofd7b9f72013-11-28 14:54:33 -05001072};