blob: ed62de6cdf8fd2d1fd2caf0a90a36882fad86d55 [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 Heoea1c4722014-01-10 08:57:19 -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 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500115static 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
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500138 if (!atomic_inc_unless_negative(&kn->active))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500139 return NULL;
140
Tejun Heodf23fc32013-12-11 14:11:56 -0500141 if (kn->flags & KERNFS_LOCKDEP)
Tejun Heo324a56e2013-12-11 14:11:53 -0500142 rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_);
143 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 Heoea1c4722014-01-10 08:57:19 -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
Tejun Heodf23fc32013-12-11 14:11:56 -0500161 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 Heoea1c4722014-01-10 08:57:19 -0500167 wake_up_all(&root->deactivate_waitq);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500168}
169
170/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500171 * kernfs_deactivate - deactivate kernfs_node
Tejun Heo324a56e2013-12-11 14:11:53 -0500172 * @kn: kernfs_node to deactivate
Tejun Heofd7b9f72013-11-28 14:54:33 -0500173 *
174 * Deny new active references and drain existing ones.
175 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500176static void kernfs_deactivate(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500177{
Tejun Heoea1c4722014-01-10 08:57:19 -0500178 struct kernfs_root *root = kernfs_root(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500179
Tejun Heodf23fc32013-12-11 14:11:56 -0500180 BUG_ON(!(kn->flags & KERNFS_REMOVED));
Tejun Heofd7b9f72013-11-28 14:54:33 -0500181
Tejun Heodf23fc32013-12-11 14:11:56 -0500182 if (!(kernfs_type(kn) & KERNFS_ACTIVE_REF))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500183 return;
184
Tejun Heo324a56e2013-12-11 14:11:53 -0500185 rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500186
Tejun Heoea1c4722014-01-10 08:57:19 -0500187 atomic_add(KN_DEACTIVATED_BIAS, &kn->active);
188
189 if (atomic_read(&kn->active) != KN_DEACTIVATED_BIAS)
Tejun Heo324a56e2013-12-11 14:11:53 -0500190 lock_contended(&kn->dep_map, _RET_IP_);
Tejun Heoea1c4722014-01-10 08:57:19 -0500191
192 wait_event(root->deactivate_waitq,
193 atomic_read(&kn->active) == KN_DEACTIVATED_BIAS);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500194
Tejun Heo324a56e2013-12-11 14:11:53 -0500195 lock_acquired(&kn->dep_map, _RET_IP_);
196 rwsem_release(&kn->dep_map, 1, _RET_IP_);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500197}
198
Tejun Heofd7b9f72013-11-28 14:54:33 -0500199/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500200 * kernfs_get - get a reference count on a kernfs_node
201 * @kn: the target kernfs_node
Tejun Heofd7b9f72013-11-28 14:54:33 -0500202 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500203void kernfs_get(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500204{
Tejun Heo324a56e2013-12-11 14:11:53 -0500205 if (kn) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500206 WARN_ON(!atomic_read(&kn->count));
207 atomic_inc(&kn->count);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500208 }
209}
210EXPORT_SYMBOL_GPL(kernfs_get);
211
212/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500213 * kernfs_put - put a reference count on a kernfs_node
214 * @kn: the target kernfs_node
Tejun Heofd7b9f72013-11-28 14:54:33 -0500215 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500216 * Put a reference count of @kn and destroy it if it reached zero.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500217 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500218void kernfs_put(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500219{
Tejun Heo324a56e2013-12-11 14:11:53 -0500220 struct kernfs_node *parent;
Tejun Heoba7443b2013-11-28 14:54:40 -0500221 struct kernfs_root *root;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500222
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500223 if (!kn || !atomic_dec_and_test(&kn->count))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500224 return;
Tejun Heo324a56e2013-12-11 14:11:53 -0500225 root = kernfs_root(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500226 repeat:
227 /* Moving/renaming is always done while holding reference.
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500228 * kn->parent won't change beneath us.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500229 */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500230 parent = kn->parent;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500231
Tejun Heoc637b8a2013-12-11 14:11:58 -0500232 WARN(!(kn->flags & KERNFS_REMOVED), "kernfs: free using entry: %s/%s\n",
233 parent ? parent->name : "", kn->name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500234
Tejun Heodf23fc32013-12-11 14:11:56 -0500235 if (kernfs_type(kn) == KERNFS_LINK)
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500236 kernfs_put(kn->symlink.target_kn);
Tejun Heo2063d602013-12-11 16:02:57 -0500237 if (!(kn->flags & KERNFS_STATIC_NAME))
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500238 kfree(kn->name);
239 if (kn->iattr) {
240 if (kn->iattr->ia_secdata)
241 security_release_secctx(kn->iattr->ia_secdata,
242 kn->iattr->ia_secdata_len);
243 simple_xattrs_free(&kn->iattr->xattrs);
Tejun Heo23223922013-11-23 17:40:02 -0500244 }
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500245 kfree(kn->iattr);
246 ida_simple_remove(&root->ino_ida, kn->ino);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500247 kmem_cache_free(kernfs_node_cache, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500248
Tejun Heo324a56e2013-12-11 14:11:53 -0500249 kn = parent;
250 if (kn) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500251 if (atomic_dec_and_test(&kn->count))
Tejun Heoba7443b2013-11-28 14:54:40 -0500252 goto repeat;
253 } else {
Tejun Heo324a56e2013-12-11 14:11:53 -0500254 /* just released the root kn, free @root too */
Tejun Heobc755552013-11-28 14:54:41 -0500255 ida_destroy(&root->ino_ida);
Tejun Heoba7443b2013-11-28 14:54:40 -0500256 kfree(root);
257 }
Tejun Heofd7b9f72013-11-28 14:54:33 -0500258}
259EXPORT_SYMBOL_GPL(kernfs_put);
260
Tejun Heoc637b8a2013-12-11 14:11:58 -0500261static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500262{
Tejun Heo324a56e2013-12-11 14:11:53 -0500263 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500264
265 if (flags & LOOKUP_RCU)
266 return -ECHILD;
267
Tejun Heo19bbb922013-12-11 16:02:59 -0500268 /* Always perform fresh lookup for negatives */
269 if (!dentry->d_inode)
270 goto out_bad_unlocked;
271
Tejun Heo324a56e2013-12-11 14:11:53 -0500272 kn = dentry->d_fsdata;
Tejun Heoa797bfc2013-12-11 14:11:57 -0500273 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500274
Tejun Heoc637b8a2013-12-11 14:11:58 -0500275 /* The kernfs node has been deleted */
Tejun Heodf23fc32013-12-11 14:11:56 -0500276 if (kn->flags & KERNFS_REMOVED)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500277 goto out_bad;
278
Tejun Heoc637b8a2013-12-11 14:11:58 -0500279 /* The kernfs node has been moved? */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500280 if (dentry->d_parent->d_fsdata != kn->parent)
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 renamed */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500284 if (strcmp(dentry->d_name.name, kn->name) != 0)
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 moved to a different namespace */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500288 if (kn->parent && kernfs_ns_enabled(kn->parent) &&
Tejun Heoc525aad2013-12-11 14:11:55 -0500289 kernfs_info(dentry->d_sb)->ns != kn->ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500290 goto out_bad;
291
Tejun Heoa797bfc2013-12-11 14:11:57 -0500292 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500293out_valid:
294 return 1;
295out_bad:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500296 mutex_unlock(&kernfs_mutex);
Tejun Heo19bbb922013-12-11 16:02:59 -0500297out_bad_unlocked:
298 /*
299 * @dentry doesn't match the underlying kernfs node, drop the
300 * dentry and force lookup. If we have submounts we must allow the
301 * vfs caches to lie about the state of the filesystem to prevent
302 * leaks and other nasty things, so use check_submounts_and_drop()
303 * instead of d_drop().
Tejun Heofd7b9f72013-11-28 14:54:33 -0500304 */
305 if (check_submounts_and_drop(dentry) != 0)
306 goto out_valid;
307
308 return 0;
309}
310
Tejun Heoc637b8a2013-12-11 14:11:58 -0500311static void kernfs_dop_release(struct dentry *dentry)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500312{
313 kernfs_put(dentry->d_fsdata);
314}
315
Tejun Heoa797bfc2013-12-11 14:11:57 -0500316const struct dentry_operations kernfs_dops = {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500317 .d_revalidate = kernfs_dop_revalidate,
Tejun Heoc637b8a2013-12-11 14:11:58 -0500318 .d_release = kernfs_dop_release,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500319};
320
Tejun Heoc637b8a2013-12-11 14:11:58 -0500321struct kernfs_node *kernfs_new_node(struct kernfs_root *root, const char *name,
Tejun Heo2063d602013-12-11 16:02:57 -0500322 umode_t mode, unsigned flags)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500323{
324 char *dup_name = NULL;
Tejun Heo324a56e2013-12-11 14:11:53 -0500325 struct kernfs_node *kn;
Tejun Heobc755552013-11-28 14:54:41 -0500326 int ret;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500327
Tejun Heo2063d602013-12-11 16:02:57 -0500328 if (!(flags & KERNFS_STATIC_NAME)) {
Tejun Heofd7b9f72013-11-28 14:54:33 -0500329 name = dup_name = kstrdup(name, GFP_KERNEL);
330 if (!name)
331 return NULL;
332 }
333
Tejun Heoa797bfc2013-12-11 14:11:57 -0500334 kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL);
Tejun Heo324a56e2013-12-11 14:11:53 -0500335 if (!kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500336 goto err_out1;
337
Tejun Heobc755552013-11-28 14:54:41 -0500338 ret = ida_simple_get(&root->ino_ida, 1, 0, GFP_KERNEL);
339 if (ret < 0)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500340 goto err_out2;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500341 kn->ino = ret;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500342
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500343 atomic_set(&kn->count, 1);
344 atomic_set(&kn->active, 0);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500345
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500346 kn->name = name;
347 kn->mode = mode;
Tejun Heo2063d602013-12-11 16:02:57 -0500348 kn->flags = flags | KERNFS_REMOVED;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500349
Tejun Heo324a56e2013-12-11 14:11:53 -0500350 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500351
352 err_out2:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500353 kmem_cache_free(kernfs_node_cache, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500354 err_out1:
355 kfree(dup_name);
356 return NULL;
357}
358
359/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500360 * kernfs_addrm_start - prepare for kernfs_node add/remove
Tejun Heoc525aad2013-12-11 14:11:55 -0500361 * @acxt: pointer to kernfs_addrm_cxt to be used
Tejun Heofd7b9f72013-11-28 14:54:33 -0500362 *
363 * This function is called when the caller is about to add or remove
Tejun Heoa797bfc2013-12-11 14:11:57 -0500364 * kernfs_node. This function acquires kernfs_mutex. @acxt is used
365 * to keep and pass context to other addrm functions.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500366 *
367 * LOCKING:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500368 * Kernel thread context (may sleep). kernfs_mutex is locked on
Tejun Heofd7b9f72013-11-28 14:54:33 -0500369 * return.
370 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500371void kernfs_addrm_start(struct kernfs_addrm_cxt *acxt)
Tejun Heoa797bfc2013-12-11 14:11:57 -0500372 __acquires(kernfs_mutex)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500373{
374 memset(acxt, 0, sizeof(*acxt));
375
Tejun Heoa797bfc2013-12-11 14:11:57 -0500376 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500377}
378
379/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500380 * kernfs_add_one - add kernfs_node to parent without warning
Tejun Heofd7b9f72013-11-28 14:54:33 -0500381 * @acxt: addrm context to use
Tejun Heo324a56e2013-12-11 14:11:53 -0500382 * @kn: kernfs_node to be added
383 * @parent: the parent kernfs_node to add @kn to
Tejun Heofd7b9f72013-11-28 14:54:33 -0500384 *
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500385 * Get @parent and set @kn->parent to it and increment nlink of the
386 * parent inode if @kn is a directory and link into the children list
387 * of the parent.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500388 *
389 * This function should be called between calls to
Tejun Heoc637b8a2013-12-11 14:11:58 -0500390 * kernfs_addrm_start() and kernfs_addrm_finish() and should be passed
391 * the same @acxt as passed to kernfs_addrm_start().
Tejun Heofd7b9f72013-11-28 14:54:33 -0500392 *
393 * LOCKING:
Tejun Heoc637b8a2013-12-11 14:11:58 -0500394 * Determined by kernfs_addrm_start().
Tejun Heofd7b9f72013-11-28 14:54:33 -0500395 *
396 * RETURNS:
397 * 0 on success, -EEXIST if entry with the given name already
398 * exists.
399 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500400int kernfs_add_one(struct kernfs_addrm_cxt *acxt, struct kernfs_node *kn,
Tejun Heo324a56e2013-12-11 14:11:53 -0500401 struct kernfs_node *parent)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500402{
Tejun Heo324a56e2013-12-11 14:11:53 -0500403 bool has_ns = kernfs_ns_enabled(parent);
Tejun Heoc525aad2013-12-11 14:11:55 -0500404 struct kernfs_iattrs *ps_iattr;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500405 int ret;
406
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500407 if (has_ns != (bool)kn->ns) {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500408 WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500409 has_ns ? "required" : "invalid", parent->name, kn->name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500410 return -EINVAL;
411 }
412
Tejun Heodf23fc32013-12-11 14:11:56 -0500413 if (kernfs_type(parent) != KERNFS_DIR)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500414 return -EINVAL;
415
Tejun Heod0ae3d42013-12-11 16:02:56 -0500416 if (parent->flags & KERNFS_REMOVED)
417 return -ENOENT;
418
Tejun Heoc637b8a2013-12-11 14:11:58 -0500419 kn->hash = kernfs_name_hash(kn->name, kn->ns);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500420 kn->parent = parent;
Tejun Heo324a56e2013-12-11 14:11:53 -0500421 kernfs_get(parent);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500422
Tejun Heoc637b8a2013-12-11 14:11:58 -0500423 ret = kernfs_link_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500424 if (ret)
425 return ret;
426
427 /* Update timestamps on the parent */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500428 ps_iattr = parent->iattr;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500429 if (ps_iattr) {
430 struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
431 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
432 }
433
434 /* Mark the entry added into directory tree */
Tejun Heodf23fc32013-12-11 14:11:56 -0500435 kn->flags &= ~KERNFS_REMOVED;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500436
437 return 0;
438}
439
440/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500441 * kernfs_remove_one - remove kernfs_node from parent
Tejun Heofd7b9f72013-11-28 14:54:33 -0500442 * @acxt: addrm context to use
Tejun Heo324a56e2013-12-11 14:11:53 -0500443 * @kn: kernfs_node to be removed
Tejun Heofd7b9f72013-11-28 14:54:33 -0500444 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500445 * Mark @kn removed and drop nlink of parent inode if @kn is a
446 * directory. @kn is unlinked from the children list.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500447 *
448 * This function should be called between calls to
Tejun Heoc637b8a2013-12-11 14:11:58 -0500449 * kernfs_addrm_start() and kernfs_addrm_finish() and should be
450 * passed the same @acxt as passed to kernfs_addrm_start().
Tejun Heofd7b9f72013-11-28 14:54:33 -0500451 *
452 * LOCKING:
Tejun Heoc637b8a2013-12-11 14:11:58 -0500453 * Determined by kernfs_addrm_start().
Tejun Heofd7b9f72013-11-28 14:54:33 -0500454 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500455static void kernfs_remove_one(struct kernfs_addrm_cxt *acxt,
456 struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500457{
Tejun Heoc525aad2013-12-11 14:11:55 -0500458 struct kernfs_iattrs *ps_iattr;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500459
460 /*
461 * Removal can be called multiple times on the same node. Only the
462 * first invocation is effective and puts the base ref.
463 */
Tejun Heodf23fc32013-12-11 14:11:56 -0500464 if (kn->flags & KERNFS_REMOVED)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500465 return;
466
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500467 if (kn->parent) {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500468 kernfs_unlink_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500469
Tejun Heoba7443b2013-11-28 14:54:40 -0500470 /* Update timestamps on the parent */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500471 ps_iattr = kn->parent->iattr;
Tejun Heoba7443b2013-11-28 14:54:40 -0500472 if (ps_iattr) {
473 ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME;
474 ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME;
475 }
Tejun Heofd7b9f72013-11-28 14:54:33 -0500476 }
477
Tejun Heodf23fc32013-12-11 14:11:56 -0500478 kn->flags |= KERNFS_REMOVED;
Tejun Heo324a56e2013-12-11 14:11:53 -0500479 kn->u.removed_list = acxt->removed;
480 acxt->removed = kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500481}
482
483/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500484 * kernfs_addrm_finish - finish up kernfs_node add/remove
Tejun Heofd7b9f72013-11-28 14:54:33 -0500485 * @acxt: addrm context to finish up
486 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500487 * Finish up kernfs_node add/remove. Resources acquired by
Tejun Heoc637b8a2013-12-11 14:11:58 -0500488 * kernfs_addrm_start() are released and removed kernfs_nodes are
Tejun Heofd7b9f72013-11-28 14:54:33 -0500489 * cleaned up.
490 *
491 * LOCKING:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500492 * kernfs_mutex is released.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500493 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500494void kernfs_addrm_finish(struct kernfs_addrm_cxt *acxt)
Tejun Heoa797bfc2013-12-11 14:11:57 -0500495 __releases(kernfs_mutex)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500496{
Tejun Heoc637b8a2013-12-11 14:11:58 -0500497 /* release resources acquired by kernfs_addrm_start() */
Tejun Heoa797bfc2013-12-11 14:11:57 -0500498 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500499
Tejun Heo324a56e2013-12-11 14:11:53 -0500500 /* kill removed kernfs_nodes */
Tejun Heofd7b9f72013-11-28 14:54:33 -0500501 while (acxt->removed) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500502 struct kernfs_node *kn = acxt->removed;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500503
Tejun Heo324a56e2013-12-11 14:11:53 -0500504 acxt->removed = kn->u.removed_list;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500505
Tejun Heoc637b8a2013-12-11 14:11:58 -0500506 kernfs_deactivate(kn);
507 kernfs_unmap_bin_file(kn);
Tejun Heo324a56e2013-12-11 14:11:53 -0500508 kernfs_put(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500509 }
510}
511
512/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500513 * kernfs_find_ns - find kernfs_node with the given name
514 * @parent: kernfs_node to search under
Tejun Heofd7b9f72013-11-28 14:54:33 -0500515 * @name: name to look for
516 * @ns: the namespace tag to use
517 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500518 * Look for kernfs_node with name @name under @parent. Returns pointer to
519 * the found kernfs_node on success, %NULL on failure.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500520 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500521static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
522 const unsigned char *name,
523 const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500524{
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500525 struct rb_node *node = parent->dir.children.rb_node;
Tejun Heoac9bba02013-11-29 17:19:09 -0500526 bool has_ns = kernfs_ns_enabled(parent);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500527 unsigned int hash;
528
Tejun Heoa797bfc2013-12-11 14:11:57 -0500529 lockdep_assert_held(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500530
531 if (has_ns != (bool)ns) {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500532 WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500533 has_ns ? "required" : "invalid", parent->name, name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500534 return NULL;
535 }
536
Tejun Heoc637b8a2013-12-11 14:11:58 -0500537 hash = kernfs_name_hash(name, ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500538 while (node) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500539 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500540 int result;
541
Tejun Heo324a56e2013-12-11 14:11:53 -0500542 kn = rb_to_kn(node);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500543 result = kernfs_name_compare(hash, name, ns, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500544 if (result < 0)
545 node = node->rb_left;
546 else if (result > 0)
547 node = node->rb_right;
548 else
Tejun Heo324a56e2013-12-11 14:11:53 -0500549 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500550 }
551 return NULL;
552}
553
554/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500555 * kernfs_find_and_get_ns - find and get kernfs_node with the given name
556 * @parent: kernfs_node to search under
Tejun Heofd7b9f72013-11-28 14:54:33 -0500557 * @name: name to look for
558 * @ns: the namespace tag to use
559 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500560 * Look for kernfs_node with name @name under @parent and get a reference
Tejun Heofd7b9f72013-11-28 14:54:33 -0500561 * if found. This function may sleep and returns pointer to the found
Tejun Heo324a56e2013-12-11 14:11:53 -0500562 * kernfs_node on success, %NULL on failure.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500563 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500564struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
565 const char *name, const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500566{
Tejun Heo324a56e2013-12-11 14:11:53 -0500567 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500568
Tejun Heoa797bfc2013-12-11 14:11:57 -0500569 mutex_lock(&kernfs_mutex);
Tejun Heo324a56e2013-12-11 14:11:53 -0500570 kn = kernfs_find_ns(parent, name, ns);
571 kernfs_get(kn);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500572 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500573
Tejun Heo324a56e2013-12-11 14:11:53 -0500574 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500575}
576EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
577
578/**
Tejun Heoba7443b2013-11-28 14:54:40 -0500579 * kernfs_create_root - create a new kernfs hierarchy
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500580 * @kdops: optional directory syscall operations for the hierarchy
Tejun Heoba7443b2013-11-28 14:54:40 -0500581 * @priv: opaque data associated with the new directory
582 *
583 * Returns the root of the new hierarchy on success, ERR_PTR() value on
584 * failure.
585 */
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500586struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops, void *priv)
Tejun Heoba7443b2013-11-28 14:54:40 -0500587{
588 struct kernfs_root *root;
Tejun Heo324a56e2013-12-11 14:11:53 -0500589 struct kernfs_node *kn;
Tejun Heoba7443b2013-11-28 14:54:40 -0500590
591 root = kzalloc(sizeof(*root), GFP_KERNEL);
592 if (!root)
593 return ERR_PTR(-ENOMEM);
594
Tejun Heobc755552013-11-28 14:54:41 -0500595 ida_init(&root->ino_ida);
596
Tejun Heoc637b8a2013-12-11 14:11:58 -0500597 kn = kernfs_new_node(root, "", S_IFDIR | S_IRUGO | S_IXUGO, KERNFS_DIR);
Tejun Heo324a56e2013-12-11 14:11:53 -0500598 if (!kn) {
Tejun Heobc755552013-11-28 14:54:41 -0500599 ida_destroy(&root->ino_ida);
Tejun Heoba7443b2013-11-28 14:54:40 -0500600 kfree(root);
601 return ERR_PTR(-ENOMEM);
602 }
603
Tejun Heodf23fc32013-12-11 14:11:56 -0500604 kn->flags &= ~KERNFS_REMOVED;
Tejun Heo324a56e2013-12-11 14:11:53 -0500605 kn->priv = priv;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500606 kn->dir.root = root;
Tejun Heoba7443b2013-11-28 14:54:40 -0500607
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500608 root->dir_ops = kdops;
Tejun Heo324a56e2013-12-11 14:11:53 -0500609 root->kn = kn;
Tejun Heoea1c4722014-01-10 08:57:19 -0500610 init_waitqueue_head(&root->deactivate_waitq);
Tejun Heoba7443b2013-11-28 14:54:40 -0500611
612 return root;
613}
614
615/**
616 * kernfs_destroy_root - destroy a kernfs hierarchy
617 * @root: root of the hierarchy to destroy
618 *
619 * Destroy the hierarchy anchored at @root by removing all existing
620 * directories and destroying @root.
621 */
622void kernfs_destroy_root(struct kernfs_root *root)
623{
Tejun Heo324a56e2013-12-11 14:11:53 -0500624 kernfs_remove(root->kn); /* will also free @root */
Tejun Heoba7443b2013-11-28 14:54:40 -0500625}
626
627/**
Tejun Heofd7b9f72013-11-28 14:54:33 -0500628 * kernfs_create_dir_ns - create a directory
629 * @parent: parent in which to create a new directory
630 * @name: name of the new directory
Tejun Heobb8b9d02013-12-11 16:02:55 -0500631 * @mode: mode of the new directory
Tejun Heofd7b9f72013-11-28 14:54:33 -0500632 * @priv: opaque data associated with the new directory
633 * @ns: optional namespace tag of the directory
634 *
635 * Returns the created node on success, ERR_PTR() value on failure.
636 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500637struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
Tejun Heobb8b9d02013-12-11 16:02:55 -0500638 const char *name, umode_t mode,
639 void *priv, const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500640{
Tejun Heoc525aad2013-12-11 14:11:55 -0500641 struct kernfs_addrm_cxt acxt;
Tejun Heo324a56e2013-12-11 14:11:53 -0500642 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500643 int rc;
644
645 /* allocate */
Tejun Heobb8b9d02013-12-11 16:02:55 -0500646 kn = kernfs_new_node(kernfs_root(parent), name, mode | S_IFDIR,
647 KERNFS_DIR);
Tejun Heo324a56e2013-12-11 14:11:53 -0500648 if (!kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500649 return ERR_PTR(-ENOMEM);
650
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500651 kn->dir.root = parent->dir.root;
652 kn->ns = ns;
Tejun Heo324a56e2013-12-11 14:11:53 -0500653 kn->priv = priv;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500654
655 /* link in */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500656 kernfs_addrm_start(&acxt);
657 rc = kernfs_add_one(&acxt, kn, parent);
658 kernfs_addrm_finish(&acxt);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500659
660 if (!rc)
Tejun Heo324a56e2013-12-11 14:11:53 -0500661 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500662
Tejun Heo324a56e2013-12-11 14:11:53 -0500663 kernfs_put(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500664 return ERR_PTR(rc);
665}
666
Tejun Heoc637b8a2013-12-11 14:11:58 -0500667static struct dentry *kernfs_iop_lookup(struct inode *dir,
668 struct dentry *dentry,
669 unsigned int flags)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500670{
Tejun Heo19bbb922013-12-11 16:02:59 -0500671 struct dentry *ret;
Tejun Heo324a56e2013-12-11 14:11:53 -0500672 struct kernfs_node *parent = dentry->d_parent->d_fsdata;
673 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500674 struct inode *inode;
675 const void *ns = NULL;
676
Tejun Heoa797bfc2013-12-11 14:11:57 -0500677 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500678
Tejun Heo324a56e2013-12-11 14:11:53 -0500679 if (kernfs_ns_enabled(parent))
Tejun Heoc525aad2013-12-11 14:11:55 -0500680 ns = kernfs_info(dir->i_sb)->ns;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500681
Tejun Heo324a56e2013-12-11 14:11:53 -0500682 kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500683
684 /* no such entry */
Tejun Heo324a56e2013-12-11 14:11:53 -0500685 if (!kn) {
Tejun Heo19bbb922013-12-11 16:02:59 -0500686 ret = NULL;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500687 goto out_unlock;
688 }
Tejun Heo324a56e2013-12-11 14:11:53 -0500689 kernfs_get(kn);
690 dentry->d_fsdata = kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500691
692 /* attach dentry and inode */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500693 inode = kernfs_get_inode(dir->i_sb, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500694 if (!inode) {
695 ret = ERR_PTR(-ENOMEM);
696 goto out_unlock;
697 }
698
699 /* instantiate and hash dentry */
700 ret = d_materialise_unique(dentry, inode);
701 out_unlock:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500702 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500703 return ret;
704}
705
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500706static int kernfs_iop_mkdir(struct inode *dir, struct dentry *dentry,
707 umode_t mode)
708{
709 struct kernfs_node *parent = dir->i_private;
710 struct kernfs_dir_ops *kdops = kernfs_root(parent)->dir_ops;
711
712 if (!kdops || !kdops->mkdir)
713 return -EPERM;
714
715 return kdops->mkdir(parent, dentry->d_name.name, mode);
716}
717
718static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
719{
720 struct kernfs_node *kn = dentry->d_fsdata;
721 struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops;
722
723 if (!kdops || !kdops->rmdir)
724 return -EPERM;
725
726 return kdops->rmdir(kn);
727}
728
729static int kernfs_iop_rename(struct inode *old_dir, struct dentry *old_dentry,
730 struct inode *new_dir, struct dentry *new_dentry)
731{
732 struct kernfs_node *kn = old_dentry->d_fsdata;
733 struct kernfs_node *new_parent = new_dir->i_private;
734 struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops;
735
736 if (!kdops || !kdops->rename)
737 return -EPERM;
738
739 return kdops->rename(kn, new_parent, new_dentry->d_name.name);
740}
741
Tejun Heoa797bfc2013-12-11 14:11:57 -0500742const struct inode_operations kernfs_dir_iops = {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500743 .lookup = kernfs_iop_lookup,
744 .permission = kernfs_iop_permission,
745 .setattr = kernfs_iop_setattr,
746 .getattr = kernfs_iop_getattr,
747 .setxattr = kernfs_iop_setxattr,
748 .removexattr = kernfs_iop_removexattr,
749 .getxattr = kernfs_iop_getxattr,
750 .listxattr = kernfs_iop_listxattr,
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500751
752 .mkdir = kernfs_iop_mkdir,
753 .rmdir = kernfs_iop_rmdir,
754 .rename = kernfs_iop_rename,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500755};
756
Tejun Heoc637b8a2013-12-11 14:11:58 -0500757static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500758{
Tejun Heo324a56e2013-12-11 14:11:53 -0500759 struct kernfs_node *last;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500760
761 while (true) {
762 struct rb_node *rbn;
763
764 last = pos;
765
Tejun Heodf23fc32013-12-11 14:11:56 -0500766 if (kernfs_type(pos) != KERNFS_DIR)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500767 break;
768
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500769 rbn = rb_first(&pos->dir.children);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500770 if (!rbn)
771 break;
772
Tejun Heo324a56e2013-12-11 14:11:53 -0500773 pos = rb_to_kn(rbn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500774 }
775
776 return last;
777}
778
779/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500780 * kernfs_next_descendant_post - find the next descendant for post-order walk
Tejun Heofd7b9f72013-11-28 14:54:33 -0500781 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo324a56e2013-12-11 14:11:53 -0500782 * @root: kernfs_node whose descendants to walk
Tejun Heofd7b9f72013-11-28 14:54:33 -0500783 *
784 * Find the next descendant to visit for post-order traversal of @root's
785 * descendants. @root is included in the iteration and the last node to be
786 * visited.
787 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500788static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
789 struct kernfs_node *root)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500790{
791 struct rb_node *rbn;
792
Tejun Heoa797bfc2013-12-11 14:11:57 -0500793 lockdep_assert_held(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500794
795 /* if first iteration, visit leftmost descendant which may be root */
796 if (!pos)
Tejun Heoc637b8a2013-12-11 14:11:58 -0500797 return kernfs_leftmost_descendant(root);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500798
799 /* if we visited @root, we're done */
800 if (pos == root)
801 return NULL;
802
803 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500804 rbn = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500805 if (rbn)
Tejun Heoc637b8a2013-12-11 14:11:58 -0500806 return kernfs_leftmost_descendant(rb_to_kn(rbn));
Tejun Heofd7b9f72013-11-28 14:54:33 -0500807
808 /* no sibling left, visit parent */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500809 return pos->parent;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500810}
811
Tejun Heoc525aad2013-12-11 14:11:55 -0500812static void __kernfs_remove(struct kernfs_addrm_cxt *acxt,
Tejun Heo324a56e2013-12-11 14:11:53 -0500813 struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500814{
Tejun Heo324a56e2013-12-11 14:11:53 -0500815 struct kernfs_node *pos, *next;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500816
Tejun Heo324a56e2013-12-11 14:11:53 -0500817 if (!kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500818 return;
819
Tejun Heoc637b8a2013-12-11 14:11:58 -0500820 pr_debug("kernfs %s: removing\n", kn->name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500821
822 next = NULL;
823 do {
824 pos = next;
Tejun Heoc637b8a2013-12-11 14:11:58 -0500825 next = kernfs_next_descendant_post(pos, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500826 if (pos)
Tejun Heoc637b8a2013-12-11 14:11:58 -0500827 kernfs_remove_one(acxt, pos);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500828 } while (next);
829}
830
831/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500832 * kernfs_remove - remove a kernfs_node recursively
833 * @kn: the kernfs_node to remove
Tejun Heofd7b9f72013-11-28 14:54:33 -0500834 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500835 * Remove @kn along with all its subdirectories and files.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500836 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500837void kernfs_remove(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500838{
Tejun Heoc525aad2013-12-11 14:11:55 -0500839 struct kernfs_addrm_cxt acxt;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500840
Tejun Heoc637b8a2013-12-11 14:11:58 -0500841 kernfs_addrm_start(&acxt);
Tejun Heo324a56e2013-12-11 14:11:53 -0500842 __kernfs_remove(&acxt, kn);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500843 kernfs_addrm_finish(&acxt);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500844}
845
846/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500847 * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
848 * @parent: parent of the target
849 * @name: name of the kernfs_node to remove
850 * @ns: namespace tag of the kernfs_node to remove
Tejun Heofd7b9f72013-11-28 14:54:33 -0500851 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500852 * Look for the kernfs_node with @name and @ns under @parent and remove it.
853 * Returns 0 on success, -ENOENT if such entry doesn't exist.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500854 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500855int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500856 const void *ns)
857{
Tejun Heoc525aad2013-12-11 14:11:55 -0500858 struct kernfs_addrm_cxt acxt;
Tejun Heo324a56e2013-12-11 14:11:53 -0500859 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500860
Tejun Heo324a56e2013-12-11 14:11:53 -0500861 if (!parent) {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500862 WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
Tejun Heofd7b9f72013-11-28 14:54:33 -0500863 name);
864 return -ENOENT;
865 }
866
Tejun Heoc637b8a2013-12-11 14:11:58 -0500867 kernfs_addrm_start(&acxt);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500868
Tejun Heo324a56e2013-12-11 14:11:53 -0500869 kn = kernfs_find_ns(parent, name, ns);
870 if (kn)
871 __kernfs_remove(&acxt, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500872
Tejun Heoc637b8a2013-12-11 14:11:58 -0500873 kernfs_addrm_finish(&acxt);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500874
Tejun Heo324a56e2013-12-11 14:11:53 -0500875 if (kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500876 return 0;
877 else
878 return -ENOENT;
879}
880
881/**
882 * kernfs_rename_ns - move and rename a kernfs_node
Tejun Heo324a56e2013-12-11 14:11:53 -0500883 * @kn: target node
Tejun Heofd7b9f72013-11-28 14:54:33 -0500884 * @new_parent: new parent to put @sd under
885 * @new_name: new name
886 * @new_ns: new namespace tag
887 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500888int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500889 const char *new_name, const void *new_ns)
890{
891 int error;
892
Tejun Heoa797bfc2013-12-11 14:11:57 -0500893 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500894
Tejun Heod0ae3d42013-12-11 16:02:56 -0500895 error = -ENOENT;
896 if ((kn->flags | new_parent->flags) & KERNFS_REMOVED)
897 goto out;
898
Tejun Heofd7b9f72013-11-28 14:54:33 -0500899 error = 0;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500900 if ((kn->parent == new_parent) && (kn->ns == new_ns) &&
901 (strcmp(kn->name, new_name) == 0))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500902 goto out; /* nothing to rename */
903
904 error = -EEXIST;
905 if (kernfs_find_ns(new_parent, new_name, new_ns))
906 goto out;
907
Tejun Heo324a56e2013-12-11 14:11:53 -0500908 /* rename kernfs_node */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500909 if (strcmp(kn->name, new_name) != 0) {
Tejun Heofd7b9f72013-11-28 14:54:33 -0500910 error = -ENOMEM;
911 new_name = kstrdup(new_name, GFP_KERNEL);
912 if (!new_name)
913 goto out;
914
Tejun Heo47a52e92013-12-11 16:02:58 -0500915 if (kn->flags & KERNFS_STATIC_NAME)
916 kn->flags &= ~KERNFS_STATIC_NAME;
917 else
918 kfree(kn->name);
919
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500920 kn->name = new_name;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500921 }
922
923 /*
924 * Move to the appropriate place in the appropriate directories rbtree.
925 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500926 kernfs_unlink_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500927 kernfs_get(new_parent);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500928 kernfs_put(kn->parent);
929 kn->ns = new_ns;
Tejun Heoc637b8a2013-12-11 14:11:58 -0500930 kn->hash = kernfs_name_hash(kn->name, kn->ns);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500931 kn->parent = new_parent;
Tejun Heoc637b8a2013-12-11 14:11:58 -0500932 kernfs_link_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500933
934 error = 0;
935 out:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500936 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500937 return error;
938}
939
Tejun Heofd7b9f72013-11-28 14:54:33 -0500940/* Relationship between s_mode and the DT_xxx types */
Tejun Heo324a56e2013-12-11 14:11:53 -0500941static inline unsigned char dt_type(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500942{
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500943 return (kn->mode >> 12) & 15;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500944}
945
Tejun Heoc637b8a2013-12-11 14:11:58 -0500946static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500947{
948 kernfs_put(filp->private_data);
949 return 0;
950}
951
Tejun Heoc637b8a2013-12-11 14:11:58 -0500952static struct kernfs_node *kernfs_dir_pos(const void *ns,
Tejun Heo324a56e2013-12-11 14:11:53 -0500953 struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500954{
955 if (pos) {
Tejun Heodf23fc32013-12-11 14:11:56 -0500956 int valid = !(pos->flags & KERNFS_REMOVED) &&
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500957 pos->parent == parent && hash == pos->hash;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500958 kernfs_put(pos);
959 if (!valid)
960 pos = NULL;
961 }
962 if (!pos && (hash > 1) && (hash < INT_MAX)) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500963 struct rb_node *node = parent->dir.children.rb_node;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500964 while (node) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500965 pos = rb_to_kn(node);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500966
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500967 if (hash < pos->hash)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500968 node = node->rb_left;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500969 else if (hash > pos->hash)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500970 node = node->rb_right;
971 else
972 break;
973 }
974 }
975 /* Skip over entries in the wrong namespace */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500976 while (pos && pos->ns != ns) {
977 struct rb_node *node = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500978 if (!node)
979 pos = NULL;
980 else
Tejun Heo324a56e2013-12-11 14:11:53 -0500981 pos = rb_to_kn(node);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500982 }
983 return pos;
984}
985
Tejun Heoc637b8a2013-12-11 14:11:58 -0500986static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
Tejun Heo324a56e2013-12-11 14:11:53 -0500987 struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500988{
Tejun Heoc637b8a2013-12-11 14:11:58 -0500989 pos = kernfs_dir_pos(ns, parent, ino, pos);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500990 if (pos)
991 do {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500992 struct rb_node *node = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500993 if (!node)
994 pos = NULL;
995 else
Tejun Heo324a56e2013-12-11 14:11:53 -0500996 pos = rb_to_kn(node);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500997 } while (pos && pos->ns != ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500998 return pos;
999}
1000
Tejun Heoc637b8a2013-12-11 14:11:58 -05001001static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001002{
1003 struct dentry *dentry = file->f_path.dentry;
Tejun Heo324a56e2013-12-11 14:11:53 -05001004 struct kernfs_node *parent = dentry->d_fsdata;
1005 struct kernfs_node *pos = file->private_data;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001006 const void *ns = NULL;
1007
1008 if (!dir_emit_dots(file, ctx))
1009 return 0;
Tejun Heoa797bfc2013-12-11 14:11:57 -05001010 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001011
Tejun Heo324a56e2013-12-11 14:11:53 -05001012 if (kernfs_ns_enabled(parent))
Tejun Heoc525aad2013-12-11 14:11:55 -05001013 ns = kernfs_info(dentry->d_sb)->ns;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001014
Tejun Heoc637b8a2013-12-11 14:11:58 -05001015 for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001016 pos;
Tejun Heoc637b8a2013-12-11 14:11:58 -05001017 pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001018 const char *name = pos->name;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001019 unsigned int type = dt_type(pos);
1020 int len = strlen(name);
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001021 ino_t ino = pos->ino;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001022
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001023 ctx->pos = pos->hash;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001024 file->private_data = pos;
1025 kernfs_get(pos);
1026
Tejun Heoa797bfc2013-12-11 14:11:57 -05001027 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001028 if (!dir_emit(ctx, name, len, ino, type))
1029 return 0;
Tejun Heoa797bfc2013-12-11 14:11:57 -05001030 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001031 }
Tejun Heoa797bfc2013-12-11 14:11:57 -05001032 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001033 file->private_data = NULL;
1034 ctx->pos = INT_MAX;
1035 return 0;
1036}
1037
Tejun Heoc637b8a2013-12-11 14:11:58 -05001038static loff_t kernfs_dir_fop_llseek(struct file *file, loff_t offset,
1039 int whence)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001040{
1041 struct inode *inode = file_inode(file);
1042 loff_t ret;
1043
1044 mutex_lock(&inode->i_mutex);
1045 ret = generic_file_llseek(file, offset, whence);
1046 mutex_unlock(&inode->i_mutex);
1047
1048 return ret;
1049}
1050
Tejun Heoa797bfc2013-12-11 14:11:57 -05001051const struct file_operations kernfs_dir_fops = {
Tejun Heofd7b9f72013-11-28 14:54:33 -05001052 .read = generic_read_dir,
Tejun Heoc637b8a2013-12-11 14:11:58 -05001053 .iterate = kernfs_fop_readdir,
1054 .release = kernfs_dir_fop_release,
1055 .llseek = kernfs_dir_fop_llseek,
Tejun Heofd7b9f72013-11-28 14:54:33 -05001056};