blob: e55bb02f15a473bdea2a29b9eb7fc3dbf972e054 [file] [log] [blame]
Tejun Heob8441ed2013-11-24 09:54:58 -05001/*
2 * fs/kernfs/dir.c - kernfs directory implementation
3 *
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
7 *
8 * This file is released under the GPLv2.
9 */
Tejun Heofd7b9f72013-11-28 14:54:33 -050010
11#include <linux/fs.h>
12#include <linux/namei.h>
13#include <linux/idr.h>
14#include <linux/slab.h>
15#include <linux/security.h>
16#include <linux/hash.h>
17
18#include "kernfs-internal.h"
19
Tejun Heoa797bfc2013-12-11 14:11:57 -050020DEFINE_MUTEX(kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -050021
Tejun Heoadc5e8b2013-12-11 14:11:54 -050022#define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb)
Tejun Heofd7b9f72013-11-28 14:54:33 -050023
Tejun Heofd7b9f72013-11-28 14:54:33 -050024/**
Tejun Heoc637b8a2013-12-11 14:11:58 -050025 * kernfs_name_hash
Tejun Heofd7b9f72013-11-28 14:54:33 -050026 * @name: Null terminated string to hash
27 * @ns: Namespace tag to hash
28 *
29 * Returns 31 bit hash of ns + name (so it fits in an off_t )
30 */
Tejun Heoc637b8a2013-12-11 14:11:58 -050031static unsigned int kernfs_name_hash(const char *name, const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -050032{
33 unsigned long hash = init_name_hash();
34 unsigned int len = strlen(name);
35 while (len--)
36 hash = partial_name_hash(*name++, hash);
37 hash = (end_name_hash(hash) ^ hash_ptr((void *)ns, 31));
38 hash &= 0x7fffffffU;
39 /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */
40 if (hash < 1)
41 hash += 2;
42 if (hash >= INT_MAX)
43 hash = INT_MAX - 1;
44 return hash;
45}
46
Tejun Heoc637b8a2013-12-11 14:11:58 -050047static int kernfs_name_compare(unsigned int hash, const char *name,
48 const void *ns, const struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -050049{
Tejun Heoadc5e8b2013-12-11 14:11:54 -050050 if (hash != kn->hash)
51 return hash - kn->hash;
52 if (ns != kn->ns)
53 return ns - kn->ns;
54 return strcmp(name, kn->name);
Tejun Heofd7b9f72013-11-28 14:54:33 -050055}
56
Tejun Heoc637b8a2013-12-11 14:11:58 -050057static int kernfs_sd_compare(const struct kernfs_node *left,
58 const struct kernfs_node *right)
Tejun Heofd7b9f72013-11-28 14:54:33 -050059{
Tejun Heoc637b8a2013-12-11 14:11:58 -050060 return kernfs_name_compare(left->hash, left->name, left->ns, right);
Tejun Heofd7b9f72013-11-28 14:54:33 -050061}
62
63/**
Tejun Heoc637b8a2013-12-11 14:11:58 -050064 * kernfs_link_sibling - link kernfs_node into sibling rbtree
Tejun Heo324a56e2013-12-11 14:11:53 -050065 * @kn: kernfs_node of interest
Tejun Heofd7b9f72013-11-28 14:54:33 -050066 *
Tejun Heo324a56e2013-12-11 14:11:53 -050067 * Link @kn into its sibling rbtree which starts from
Tejun Heoadc5e8b2013-12-11 14:11:54 -050068 * @kn->parent->dir.children.
Tejun Heofd7b9f72013-11-28 14:54:33 -050069 *
70 * Locking:
Tejun Heoa797bfc2013-12-11 14:11:57 -050071 * mutex_lock(kernfs_mutex)
Tejun Heofd7b9f72013-11-28 14:54:33 -050072 *
73 * RETURNS:
74 * 0 on susccess -EEXIST on failure.
75 */
Tejun Heoc637b8a2013-12-11 14:11:58 -050076static int kernfs_link_sibling(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -050077{
Tejun Heoadc5e8b2013-12-11 14:11:54 -050078 struct rb_node **node = &kn->parent->dir.children.rb_node;
Tejun Heofd7b9f72013-11-28 14:54:33 -050079 struct rb_node *parent = NULL;
80
Tejun Heodf23fc32013-12-11 14:11:56 -050081 if (kernfs_type(kn) == KERNFS_DIR)
Tejun Heoadc5e8b2013-12-11 14:11:54 -050082 kn->parent->dir.subdirs++;
Tejun Heofd7b9f72013-11-28 14:54:33 -050083
84 while (*node) {
Tejun Heo324a56e2013-12-11 14:11:53 -050085 struct kernfs_node *pos;
Tejun Heofd7b9f72013-11-28 14:54:33 -050086 int result;
87
Tejun Heo324a56e2013-12-11 14:11:53 -050088 pos = rb_to_kn(*node);
Tejun Heofd7b9f72013-11-28 14:54:33 -050089 parent = *node;
Tejun Heoc637b8a2013-12-11 14:11:58 -050090 result = kernfs_sd_compare(kn, pos);
Tejun Heofd7b9f72013-11-28 14:54:33 -050091 if (result < 0)
Tejun Heoadc5e8b2013-12-11 14:11:54 -050092 node = &pos->rb.rb_left;
Tejun Heofd7b9f72013-11-28 14:54:33 -050093 else if (result > 0)
Tejun Heoadc5e8b2013-12-11 14:11:54 -050094 node = &pos->rb.rb_right;
Tejun Heofd7b9f72013-11-28 14:54:33 -050095 else
96 return -EEXIST;
97 }
98 /* add new node and rebalance the tree */
Tejun Heoadc5e8b2013-12-11 14:11:54 -050099 rb_link_node(&kn->rb, parent, node);
100 rb_insert_color(&kn->rb, &kn->parent->dir.children);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500101 return 0;
102}
103
104/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500105 * kernfs_unlink_sibling - unlink kernfs_node from sibling rbtree
Tejun Heo324a56e2013-12-11 14:11:53 -0500106 * @kn: kernfs_node of interest
Tejun Heofd7b9f72013-11-28 14:54:33 -0500107 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500108 * Unlink @kn from its sibling rbtree which starts from
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500109 * kn->parent->dir.children.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500110 *
111 * Locking:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500112 * mutex_lock(kernfs_mutex)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500113 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500114static void kernfs_unlink_sibling(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500115{
Tejun Heodf23fc32013-12-11 14:11:56 -0500116 if (kernfs_type(kn) == KERNFS_DIR)
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500117 kn->parent->dir.subdirs--;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500118
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500119 rb_erase(&kn->rb, &kn->parent->dir.children);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500120}
121
122/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500123 * kernfs_get_active - get an active reference to kernfs_node
Tejun Heo324a56e2013-12-11 14:11:53 -0500124 * @kn: kernfs_node to get an active reference to
Tejun Heofd7b9f72013-11-28 14:54:33 -0500125 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500126 * Get an active reference of @kn. This function is noop if @kn
Tejun Heofd7b9f72013-11-28 14:54:33 -0500127 * is NULL.
128 *
129 * RETURNS:
Tejun Heo324a56e2013-12-11 14:11:53 -0500130 * Pointer to @kn on success, NULL on failure.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500131 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500132struct kernfs_node *kernfs_get_active(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500133{
Tejun Heo324a56e2013-12-11 14:11:53 -0500134 if (unlikely(!kn))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500135 return NULL;
136
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500137 if (!atomic_inc_unless_negative(&kn->active))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500138 return NULL;
139
Tejun Heodf23fc32013-12-11 14:11:56 -0500140 if (kn->flags & KERNFS_LOCKDEP)
Tejun Heo324a56e2013-12-11 14:11:53 -0500141 rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_);
142 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500143}
144
145/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500146 * kernfs_put_active - put an active reference to kernfs_node
Tejun Heo324a56e2013-12-11 14:11:53 -0500147 * @kn: kernfs_node to put an active reference to
Tejun Heofd7b9f72013-11-28 14:54:33 -0500148 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500149 * Put an active reference to @kn. This function is noop if @kn
Tejun Heofd7b9f72013-11-28 14:54:33 -0500150 * is NULL.
151 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500152void kernfs_put_active(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500153{
154 int v;
155
Tejun Heo324a56e2013-12-11 14:11:53 -0500156 if (unlikely(!kn))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500157 return;
158
Tejun Heodf23fc32013-12-11 14:11:56 -0500159 if (kn->flags & KERNFS_LOCKDEP)
Tejun Heo324a56e2013-12-11 14:11:53 -0500160 rwsem_release(&kn->dep_map, 1, _RET_IP_);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500161 v = atomic_dec_return(&kn->active);
Tejun Heodf23fc32013-12-11 14:11:56 -0500162 if (likely(v != KN_DEACTIVATED_BIAS))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500163 return;
164
Tejun Heo324a56e2013-12-11 14:11:53 -0500165 /*
166 * atomic_dec_return() is a mb(), we'll always see the updated
167 * kn->u.completion.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500168 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500169 complete(kn->u.completion);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500170}
171
172/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500173 * kernfs_deactivate - deactivate kernfs_node
Tejun Heo324a56e2013-12-11 14:11:53 -0500174 * @kn: kernfs_node to deactivate
Tejun Heofd7b9f72013-11-28 14:54:33 -0500175 *
176 * Deny new active references and drain existing ones.
177 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500178static void kernfs_deactivate(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500179{
180 DECLARE_COMPLETION_ONSTACK(wait);
181 int v;
182
Tejun Heodf23fc32013-12-11 14:11:56 -0500183 BUG_ON(!(kn->flags & KERNFS_REMOVED));
Tejun Heofd7b9f72013-11-28 14:54:33 -0500184
Tejun Heodf23fc32013-12-11 14:11:56 -0500185 if (!(kernfs_type(kn) & KERNFS_ACTIVE_REF))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500186 return;
187
Tejun Heo324a56e2013-12-11 14:11:53 -0500188 kn->u.completion = (void *)&wait;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500189
Tejun Heo324a56e2013-12-11 14:11:53 -0500190 rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500191 /* atomic_add_return() is a mb(), put_active() will always see
Tejun Heo324a56e2013-12-11 14:11:53 -0500192 * the updated kn->u.completion.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500193 */
Tejun Heodf23fc32013-12-11 14:11:56 -0500194 v = atomic_add_return(KN_DEACTIVATED_BIAS, &kn->active);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500195
Tejun Heodf23fc32013-12-11 14:11:56 -0500196 if (v != KN_DEACTIVATED_BIAS) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500197 lock_contended(&kn->dep_map, _RET_IP_);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500198 wait_for_completion(&wait);
199 }
200
Tejun Heo324a56e2013-12-11 14:11:53 -0500201 lock_acquired(&kn->dep_map, _RET_IP_);
202 rwsem_release(&kn->dep_map, 1, _RET_IP_);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500203}
204
Tejun Heofd7b9f72013-11-28 14:54:33 -0500205/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500206 * kernfs_get - get a reference count on a kernfs_node
207 * @kn: the target kernfs_node
Tejun Heofd7b9f72013-11-28 14:54:33 -0500208 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500209void kernfs_get(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500210{
Tejun Heo324a56e2013-12-11 14:11:53 -0500211 if (kn) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500212 WARN_ON(!atomic_read(&kn->count));
213 atomic_inc(&kn->count);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500214 }
215}
216EXPORT_SYMBOL_GPL(kernfs_get);
217
218/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500219 * kernfs_put - put a reference count on a kernfs_node
220 * @kn: the target kernfs_node
Tejun Heofd7b9f72013-11-28 14:54:33 -0500221 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500222 * Put a reference count of @kn and destroy it if it reached zero.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500223 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500224void kernfs_put(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500225{
Tejun Heo324a56e2013-12-11 14:11:53 -0500226 struct kernfs_node *parent;
Tejun Heoba7443b2013-11-28 14:54:40 -0500227 struct kernfs_root *root;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500228
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500229 if (!kn || !atomic_dec_and_test(&kn->count))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500230 return;
Tejun Heo324a56e2013-12-11 14:11:53 -0500231 root = kernfs_root(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500232 repeat:
233 /* Moving/renaming is always done while holding reference.
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500234 * kn->parent won't change beneath us.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500235 */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500236 parent = kn->parent;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500237
Tejun Heoc637b8a2013-12-11 14:11:58 -0500238 WARN(!(kn->flags & KERNFS_REMOVED), "kernfs: free using entry: %s/%s\n",
239 parent ? parent->name : "", kn->name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500240
Tejun Heodf23fc32013-12-11 14:11:56 -0500241 if (kernfs_type(kn) == KERNFS_LINK)
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500242 kernfs_put(kn->symlink.target_kn);
Tejun Heodf23fc32013-12-11 14:11:56 -0500243 if (kernfs_type(kn) & KERNFS_COPY_NAME)
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500244 kfree(kn->name);
245 if (kn->iattr) {
246 if (kn->iattr->ia_secdata)
247 security_release_secctx(kn->iattr->ia_secdata,
248 kn->iattr->ia_secdata_len);
249 simple_xattrs_free(&kn->iattr->xattrs);
Tejun Heo23223922013-11-23 17:40:02 -0500250 }
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500251 kfree(kn->iattr);
252 ida_simple_remove(&root->ino_ida, kn->ino);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500253 kmem_cache_free(kernfs_node_cache, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500254
Tejun Heo324a56e2013-12-11 14:11:53 -0500255 kn = parent;
256 if (kn) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500257 if (atomic_dec_and_test(&kn->count))
Tejun Heoba7443b2013-11-28 14:54:40 -0500258 goto repeat;
259 } else {
Tejun Heo324a56e2013-12-11 14:11:53 -0500260 /* just released the root kn, free @root too */
Tejun Heobc755552013-11-28 14:54:41 -0500261 ida_destroy(&root->ino_ida);
Tejun Heoba7443b2013-11-28 14:54:40 -0500262 kfree(root);
263 }
Tejun Heofd7b9f72013-11-28 14:54:33 -0500264}
265EXPORT_SYMBOL_GPL(kernfs_put);
266
Tejun Heoc637b8a2013-12-11 14:11:58 -0500267static int kernfs_dop_delete(const struct dentry *dentry)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500268{
Tejun Heo324a56e2013-12-11 14:11:53 -0500269 struct kernfs_node *kn = dentry->d_fsdata;
Tejun Heodf23fc32013-12-11 14:11:56 -0500270 return !(kn && !(kn->flags & KERNFS_REMOVED));
Tejun Heofd7b9f72013-11-28 14:54:33 -0500271}
272
Tejun Heoc637b8a2013-12-11 14:11:58 -0500273static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500274{
Tejun Heo324a56e2013-12-11 14:11:53 -0500275 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500276
277 if (flags & LOOKUP_RCU)
278 return -ECHILD;
279
Tejun Heo324a56e2013-12-11 14:11:53 -0500280 kn = dentry->d_fsdata;
Tejun Heoa797bfc2013-12-11 14:11:57 -0500281 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500282
Tejun Heoc637b8a2013-12-11 14:11:58 -0500283 /* The kernfs node has been deleted */
Tejun Heodf23fc32013-12-11 14:11:56 -0500284 if (kn->flags & KERNFS_REMOVED)
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? */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500288 if (dentry->d_parent->d_fsdata != kn->parent)
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 renamed */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500292 if (strcmp(dentry->d_name.name, kn->name) != 0)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500293 goto out_bad;
294
Tejun Heoc637b8a2013-12-11 14:11:58 -0500295 /* The kernfs node has been moved to a different namespace */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500296 if (kn->parent && kernfs_ns_enabled(kn->parent) &&
Tejun Heoc525aad2013-12-11 14:11:55 -0500297 kernfs_info(dentry->d_sb)->ns != kn->ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500298 goto out_bad;
299
Tejun Heoa797bfc2013-12-11 14:11:57 -0500300 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500301out_valid:
302 return 1;
303out_bad:
Tejun Heoc637b8a2013-12-11 14:11:58 -0500304 /*
305 * Remove the dentry from the dcache hashes.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500306 * If this is a deleted dentry we use d_drop instead of d_delete
Tejun Heoc637b8a2013-12-11 14:11:58 -0500307 * so kernfs doesn't need to cope with negative dentries.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500308 *
309 * If this is a dentry that has simply been renamed we
310 * use d_drop to remove it from the dcache lookup on its
311 * old parent. If this dentry persists later when a lookup
312 * is performed at its new name the dentry will be readded
313 * to the dcache hashes.
314 */
Tejun Heoa797bfc2013-12-11 14:11:57 -0500315 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500316
317 /* If we have submounts we must allow the vfs caches
318 * to lie about the state of the filesystem to prevent
319 * leaks and other nasty things.
320 */
321 if (check_submounts_and_drop(dentry) != 0)
322 goto out_valid;
323
324 return 0;
325}
326
Tejun Heoc637b8a2013-12-11 14:11:58 -0500327static void kernfs_dop_release(struct dentry *dentry)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500328{
329 kernfs_put(dentry->d_fsdata);
330}
331
Tejun Heoa797bfc2013-12-11 14:11:57 -0500332const struct dentry_operations kernfs_dops = {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500333 .d_revalidate = kernfs_dop_revalidate,
334 .d_delete = kernfs_dop_delete,
335 .d_release = kernfs_dop_release,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500336};
337
Tejun Heoc637b8a2013-12-11 14:11:58 -0500338struct kernfs_node *kernfs_new_node(struct kernfs_root *root, const char *name,
339 umode_t mode, int type)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500340{
341 char *dup_name = NULL;
Tejun Heo324a56e2013-12-11 14:11:53 -0500342 struct kernfs_node *kn;
Tejun Heobc755552013-11-28 14:54:41 -0500343 int ret;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500344
Tejun Heodf23fc32013-12-11 14:11:56 -0500345 if (type & KERNFS_COPY_NAME) {
Tejun Heofd7b9f72013-11-28 14:54:33 -0500346 name = dup_name = kstrdup(name, GFP_KERNEL);
347 if (!name)
348 return NULL;
349 }
350
Tejun Heoa797bfc2013-12-11 14:11:57 -0500351 kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL);
Tejun Heo324a56e2013-12-11 14:11:53 -0500352 if (!kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500353 goto err_out1;
354
Tejun Heobc755552013-11-28 14:54:41 -0500355 ret = ida_simple_get(&root->ino_ida, 1, 0, GFP_KERNEL);
356 if (ret < 0)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500357 goto err_out2;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500358 kn->ino = ret;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500359
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500360 atomic_set(&kn->count, 1);
361 atomic_set(&kn->active, 0);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500362
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500363 kn->name = name;
364 kn->mode = mode;
Tejun Heodf23fc32013-12-11 14:11:56 -0500365 kn->flags = type | KERNFS_REMOVED;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500366
Tejun Heo324a56e2013-12-11 14:11:53 -0500367 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500368
369 err_out2:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500370 kmem_cache_free(kernfs_node_cache, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500371 err_out1:
372 kfree(dup_name);
373 return NULL;
374}
375
376/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500377 * kernfs_addrm_start - prepare for kernfs_node add/remove
Tejun Heoc525aad2013-12-11 14:11:55 -0500378 * @acxt: pointer to kernfs_addrm_cxt to be used
Tejun Heofd7b9f72013-11-28 14:54:33 -0500379 *
380 * This function is called when the caller is about to add or remove
Tejun Heoa797bfc2013-12-11 14:11:57 -0500381 * kernfs_node. This function acquires kernfs_mutex. @acxt is used
382 * to keep and pass context to other addrm functions.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500383 *
384 * LOCKING:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500385 * Kernel thread context (may sleep). kernfs_mutex is locked on
Tejun Heofd7b9f72013-11-28 14:54:33 -0500386 * return.
387 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500388void kernfs_addrm_start(struct kernfs_addrm_cxt *acxt)
Tejun Heoa797bfc2013-12-11 14:11:57 -0500389 __acquires(kernfs_mutex)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500390{
391 memset(acxt, 0, sizeof(*acxt));
392
Tejun Heoa797bfc2013-12-11 14:11:57 -0500393 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500394}
395
396/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500397 * kernfs_add_one - add kernfs_node to parent without warning
Tejun Heofd7b9f72013-11-28 14:54:33 -0500398 * @acxt: addrm context to use
Tejun Heo324a56e2013-12-11 14:11:53 -0500399 * @kn: kernfs_node to be added
400 * @parent: the parent kernfs_node to add @kn to
Tejun Heofd7b9f72013-11-28 14:54:33 -0500401 *
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500402 * Get @parent and set @kn->parent to it and increment nlink of the
403 * parent inode if @kn is a directory and link into the children list
404 * of the parent.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500405 *
406 * This function should be called between calls to
Tejun Heoc637b8a2013-12-11 14:11:58 -0500407 * kernfs_addrm_start() and kernfs_addrm_finish() and should be passed
408 * the same @acxt as passed to kernfs_addrm_start().
Tejun Heofd7b9f72013-11-28 14:54:33 -0500409 *
410 * LOCKING:
Tejun Heoc637b8a2013-12-11 14:11:58 -0500411 * Determined by kernfs_addrm_start().
Tejun Heofd7b9f72013-11-28 14:54:33 -0500412 *
413 * RETURNS:
414 * 0 on success, -EEXIST if entry with the given name already
415 * exists.
416 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500417int kernfs_add_one(struct kernfs_addrm_cxt *acxt, struct kernfs_node *kn,
Tejun Heo324a56e2013-12-11 14:11:53 -0500418 struct kernfs_node *parent)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500419{
Tejun Heo324a56e2013-12-11 14:11:53 -0500420 bool has_ns = kernfs_ns_enabled(parent);
Tejun Heoc525aad2013-12-11 14:11:55 -0500421 struct kernfs_iattrs *ps_iattr;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500422 int ret;
423
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500424 if (has_ns != (bool)kn->ns) {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500425 WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500426 has_ns ? "required" : "invalid", parent->name, kn->name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500427 return -EINVAL;
428 }
429
Tejun Heodf23fc32013-12-11 14:11:56 -0500430 if (kernfs_type(parent) != KERNFS_DIR)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500431 return -EINVAL;
432
Tejun Heoc637b8a2013-12-11 14:11:58 -0500433 kn->hash = kernfs_name_hash(kn->name, kn->ns);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500434 kn->parent = parent;
Tejun Heo324a56e2013-12-11 14:11:53 -0500435 kernfs_get(parent);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500436
Tejun Heoc637b8a2013-12-11 14:11:58 -0500437 ret = kernfs_link_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500438 if (ret)
439 return ret;
440
441 /* Update timestamps on the parent */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500442 ps_iattr = parent->iattr;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500443 if (ps_iattr) {
444 struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
445 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
446 }
447
448 /* Mark the entry added into directory tree */
Tejun Heodf23fc32013-12-11 14:11:56 -0500449 kn->flags &= ~KERNFS_REMOVED;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500450
451 return 0;
452}
453
454/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500455 * kernfs_remove_one - remove kernfs_node from parent
Tejun Heofd7b9f72013-11-28 14:54:33 -0500456 * @acxt: addrm context to use
Tejun Heo324a56e2013-12-11 14:11:53 -0500457 * @kn: kernfs_node to be removed
Tejun Heofd7b9f72013-11-28 14:54:33 -0500458 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500459 * Mark @kn removed and drop nlink of parent inode if @kn is a
460 * directory. @kn is unlinked from the children list.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500461 *
462 * This function should be called between calls to
Tejun Heoc637b8a2013-12-11 14:11:58 -0500463 * kernfs_addrm_start() and kernfs_addrm_finish() and should be
464 * passed the same @acxt as passed to kernfs_addrm_start().
Tejun Heofd7b9f72013-11-28 14:54:33 -0500465 *
466 * LOCKING:
Tejun Heoc637b8a2013-12-11 14:11:58 -0500467 * Determined by kernfs_addrm_start().
Tejun Heofd7b9f72013-11-28 14:54:33 -0500468 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500469static void kernfs_remove_one(struct kernfs_addrm_cxt *acxt,
470 struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500471{
Tejun Heoc525aad2013-12-11 14:11:55 -0500472 struct kernfs_iattrs *ps_iattr;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500473
474 /*
475 * Removal can be called multiple times on the same node. Only the
476 * first invocation is effective and puts the base ref.
477 */
Tejun Heodf23fc32013-12-11 14:11:56 -0500478 if (kn->flags & KERNFS_REMOVED)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500479 return;
480
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500481 if (kn->parent) {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500482 kernfs_unlink_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500483
Tejun Heoba7443b2013-11-28 14:54:40 -0500484 /* Update timestamps on the parent */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500485 ps_iattr = kn->parent->iattr;
Tejun Heoba7443b2013-11-28 14:54:40 -0500486 if (ps_iattr) {
487 ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME;
488 ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME;
489 }
Tejun Heofd7b9f72013-11-28 14:54:33 -0500490 }
491
Tejun Heodf23fc32013-12-11 14:11:56 -0500492 kn->flags |= KERNFS_REMOVED;
Tejun Heo324a56e2013-12-11 14:11:53 -0500493 kn->u.removed_list = acxt->removed;
494 acxt->removed = kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500495}
496
497/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500498 * kernfs_addrm_finish - finish up kernfs_node add/remove
Tejun Heofd7b9f72013-11-28 14:54:33 -0500499 * @acxt: addrm context to finish up
500 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500501 * Finish up kernfs_node add/remove. Resources acquired by
Tejun Heoc637b8a2013-12-11 14:11:58 -0500502 * kernfs_addrm_start() are released and removed kernfs_nodes are
Tejun Heofd7b9f72013-11-28 14:54:33 -0500503 * cleaned up.
504 *
505 * LOCKING:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500506 * kernfs_mutex is released.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500507 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500508void kernfs_addrm_finish(struct kernfs_addrm_cxt *acxt)
Tejun Heoa797bfc2013-12-11 14:11:57 -0500509 __releases(kernfs_mutex)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500510{
Tejun Heoc637b8a2013-12-11 14:11:58 -0500511 /* release resources acquired by kernfs_addrm_start() */
Tejun Heoa797bfc2013-12-11 14:11:57 -0500512 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500513
Tejun Heo324a56e2013-12-11 14:11:53 -0500514 /* kill removed kernfs_nodes */
Tejun Heofd7b9f72013-11-28 14:54:33 -0500515 while (acxt->removed) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500516 struct kernfs_node *kn = acxt->removed;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500517
Tejun Heo324a56e2013-12-11 14:11:53 -0500518 acxt->removed = kn->u.removed_list;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500519
Tejun Heoc637b8a2013-12-11 14:11:58 -0500520 kernfs_deactivate(kn);
521 kernfs_unmap_bin_file(kn);
Tejun Heo324a56e2013-12-11 14:11:53 -0500522 kernfs_put(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500523 }
524}
525
526/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500527 * kernfs_find_ns - find kernfs_node with the given name
528 * @parent: kernfs_node to search under
Tejun Heofd7b9f72013-11-28 14:54:33 -0500529 * @name: name to look for
530 * @ns: the namespace tag to use
531 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500532 * Look for kernfs_node with name @name under @parent. Returns pointer to
533 * the found kernfs_node on success, %NULL on failure.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500534 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500535static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
536 const unsigned char *name,
537 const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500538{
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500539 struct rb_node *node = parent->dir.children.rb_node;
Tejun Heoac9bba02013-11-29 17:19:09 -0500540 bool has_ns = kernfs_ns_enabled(parent);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500541 unsigned int hash;
542
Tejun Heoa797bfc2013-12-11 14:11:57 -0500543 lockdep_assert_held(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500544
545 if (has_ns != (bool)ns) {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500546 WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500547 has_ns ? "required" : "invalid", parent->name, name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500548 return NULL;
549 }
550
Tejun Heoc637b8a2013-12-11 14:11:58 -0500551 hash = kernfs_name_hash(name, ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500552 while (node) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500553 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500554 int result;
555
Tejun Heo324a56e2013-12-11 14:11:53 -0500556 kn = rb_to_kn(node);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500557 result = kernfs_name_compare(hash, name, ns, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500558 if (result < 0)
559 node = node->rb_left;
560 else if (result > 0)
561 node = node->rb_right;
562 else
Tejun Heo324a56e2013-12-11 14:11:53 -0500563 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500564 }
565 return NULL;
566}
567
568/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500569 * kernfs_find_and_get_ns - find and get kernfs_node with the given name
570 * @parent: kernfs_node to search under
Tejun Heofd7b9f72013-11-28 14:54:33 -0500571 * @name: name to look for
572 * @ns: the namespace tag to use
573 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500574 * Look for kernfs_node with name @name under @parent and get a reference
Tejun Heofd7b9f72013-11-28 14:54:33 -0500575 * if found. This function may sleep and returns pointer to the found
Tejun Heo324a56e2013-12-11 14:11:53 -0500576 * kernfs_node on success, %NULL on failure.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500577 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500578struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
579 const char *name, const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500580{
Tejun Heo324a56e2013-12-11 14:11:53 -0500581 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500582
Tejun Heoa797bfc2013-12-11 14:11:57 -0500583 mutex_lock(&kernfs_mutex);
Tejun Heo324a56e2013-12-11 14:11:53 -0500584 kn = kernfs_find_ns(parent, name, ns);
585 kernfs_get(kn);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500586 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500587
Tejun Heo324a56e2013-12-11 14:11:53 -0500588 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500589}
590EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
591
592/**
Tejun Heoba7443b2013-11-28 14:54:40 -0500593 * kernfs_create_root - create a new kernfs hierarchy
594 * @priv: opaque data associated with the new directory
595 *
596 * Returns the root of the new hierarchy on success, ERR_PTR() value on
597 * failure.
598 */
599struct kernfs_root *kernfs_create_root(void *priv)
600{
601 struct kernfs_root *root;
Tejun Heo324a56e2013-12-11 14:11:53 -0500602 struct kernfs_node *kn;
Tejun Heoba7443b2013-11-28 14:54:40 -0500603
604 root = kzalloc(sizeof(*root), GFP_KERNEL);
605 if (!root)
606 return ERR_PTR(-ENOMEM);
607
Tejun Heobc755552013-11-28 14:54:41 -0500608 ida_init(&root->ino_ida);
609
Tejun Heoc637b8a2013-12-11 14:11:58 -0500610 kn = kernfs_new_node(root, "", S_IFDIR | S_IRUGO | S_IXUGO, KERNFS_DIR);
Tejun Heo324a56e2013-12-11 14:11:53 -0500611 if (!kn) {
Tejun Heobc755552013-11-28 14:54:41 -0500612 ida_destroy(&root->ino_ida);
Tejun Heoba7443b2013-11-28 14:54:40 -0500613 kfree(root);
614 return ERR_PTR(-ENOMEM);
615 }
616
Tejun Heodf23fc32013-12-11 14:11:56 -0500617 kn->flags &= ~KERNFS_REMOVED;
Tejun Heo324a56e2013-12-11 14:11:53 -0500618 kn->priv = priv;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500619 kn->dir.root = root;
Tejun Heoba7443b2013-11-28 14:54:40 -0500620
Tejun Heo324a56e2013-12-11 14:11:53 -0500621 root->kn = kn;
Tejun Heoba7443b2013-11-28 14:54:40 -0500622
623 return root;
624}
625
626/**
627 * kernfs_destroy_root - destroy a kernfs hierarchy
628 * @root: root of the hierarchy to destroy
629 *
630 * Destroy the hierarchy anchored at @root by removing all existing
631 * directories and destroying @root.
632 */
633void kernfs_destroy_root(struct kernfs_root *root)
634{
Tejun Heo324a56e2013-12-11 14:11:53 -0500635 kernfs_remove(root->kn); /* will also free @root */
Tejun Heoba7443b2013-11-28 14:54:40 -0500636}
637
638/**
Tejun Heofd7b9f72013-11-28 14:54:33 -0500639 * kernfs_create_dir_ns - create a directory
640 * @parent: parent in which to create a new directory
641 * @name: name of the new directory
Tejun Heobb8b9d02013-12-11 16:02:55 -0500642 * @mode: mode of the new directory
Tejun Heofd7b9f72013-11-28 14:54:33 -0500643 * @priv: opaque data associated with the new directory
644 * @ns: optional namespace tag of the directory
645 *
646 * Returns the created node on success, ERR_PTR() value on failure.
647 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500648struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
Tejun Heobb8b9d02013-12-11 16:02:55 -0500649 const char *name, umode_t mode,
650 void *priv, const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500651{
Tejun Heoc525aad2013-12-11 14:11:55 -0500652 struct kernfs_addrm_cxt acxt;
Tejun Heo324a56e2013-12-11 14:11:53 -0500653 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500654 int rc;
655
656 /* allocate */
Tejun Heobb8b9d02013-12-11 16:02:55 -0500657 kn = kernfs_new_node(kernfs_root(parent), name, mode | S_IFDIR,
658 KERNFS_DIR);
Tejun Heo324a56e2013-12-11 14:11:53 -0500659 if (!kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500660 return ERR_PTR(-ENOMEM);
661
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500662 kn->dir.root = parent->dir.root;
663 kn->ns = ns;
Tejun Heo324a56e2013-12-11 14:11:53 -0500664 kn->priv = priv;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500665
666 /* link in */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500667 kernfs_addrm_start(&acxt);
668 rc = kernfs_add_one(&acxt, kn, parent);
669 kernfs_addrm_finish(&acxt);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500670
671 if (!rc)
Tejun Heo324a56e2013-12-11 14:11:53 -0500672 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500673
Tejun Heo324a56e2013-12-11 14:11:53 -0500674 kernfs_put(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500675 return ERR_PTR(rc);
676}
677
Tejun Heoc637b8a2013-12-11 14:11:58 -0500678static struct dentry *kernfs_iop_lookup(struct inode *dir,
679 struct dentry *dentry,
680 unsigned int flags)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500681{
682 struct dentry *ret = NULL;
Tejun Heo324a56e2013-12-11 14:11:53 -0500683 struct kernfs_node *parent = dentry->d_parent->d_fsdata;
684 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500685 struct inode *inode;
686 const void *ns = NULL;
687
Tejun Heoa797bfc2013-12-11 14:11:57 -0500688 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500689
Tejun Heo324a56e2013-12-11 14:11:53 -0500690 if (kernfs_ns_enabled(parent))
Tejun Heoc525aad2013-12-11 14:11:55 -0500691 ns = kernfs_info(dir->i_sb)->ns;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500692
Tejun Heo324a56e2013-12-11 14:11:53 -0500693 kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500694
695 /* no such entry */
Tejun Heo324a56e2013-12-11 14:11:53 -0500696 if (!kn) {
Tejun Heofd7b9f72013-11-28 14:54:33 -0500697 ret = ERR_PTR(-ENOENT);
698 goto out_unlock;
699 }
Tejun Heo324a56e2013-12-11 14:11:53 -0500700 kernfs_get(kn);
701 dentry->d_fsdata = kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500702
703 /* attach dentry and inode */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500704 inode = kernfs_get_inode(dir->i_sb, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500705 if (!inode) {
706 ret = ERR_PTR(-ENOMEM);
707 goto out_unlock;
708 }
709
710 /* instantiate and hash dentry */
711 ret = d_materialise_unique(dentry, inode);
712 out_unlock:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500713 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500714 return ret;
715}
716
Tejun Heoa797bfc2013-12-11 14:11:57 -0500717const struct inode_operations kernfs_dir_iops = {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500718 .lookup = kernfs_iop_lookup,
719 .permission = kernfs_iop_permission,
720 .setattr = kernfs_iop_setattr,
721 .getattr = kernfs_iop_getattr,
722 .setxattr = kernfs_iop_setxattr,
723 .removexattr = kernfs_iop_removexattr,
724 .getxattr = kernfs_iop_getxattr,
725 .listxattr = kernfs_iop_listxattr,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500726};
727
Tejun Heoc637b8a2013-12-11 14:11:58 -0500728static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500729{
Tejun Heo324a56e2013-12-11 14:11:53 -0500730 struct kernfs_node *last;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500731
732 while (true) {
733 struct rb_node *rbn;
734
735 last = pos;
736
Tejun Heodf23fc32013-12-11 14:11:56 -0500737 if (kernfs_type(pos) != KERNFS_DIR)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500738 break;
739
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500740 rbn = rb_first(&pos->dir.children);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500741 if (!rbn)
742 break;
743
Tejun Heo324a56e2013-12-11 14:11:53 -0500744 pos = rb_to_kn(rbn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500745 }
746
747 return last;
748}
749
750/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500751 * kernfs_next_descendant_post - find the next descendant for post-order walk
Tejun Heofd7b9f72013-11-28 14:54:33 -0500752 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo324a56e2013-12-11 14:11:53 -0500753 * @root: kernfs_node whose descendants to walk
Tejun Heofd7b9f72013-11-28 14:54:33 -0500754 *
755 * Find the next descendant to visit for post-order traversal of @root's
756 * descendants. @root is included in the iteration and the last node to be
757 * visited.
758 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500759static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
760 struct kernfs_node *root)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500761{
762 struct rb_node *rbn;
763
Tejun Heoa797bfc2013-12-11 14:11:57 -0500764 lockdep_assert_held(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500765
766 /* if first iteration, visit leftmost descendant which may be root */
767 if (!pos)
Tejun Heoc637b8a2013-12-11 14:11:58 -0500768 return kernfs_leftmost_descendant(root);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500769
770 /* if we visited @root, we're done */
771 if (pos == root)
772 return NULL;
773
774 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500775 rbn = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500776 if (rbn)
Tejun Heoc637b8a2013-12-11 14:11:58 -0500777 return kernfs_leftmost_descendant(rb_to_kn(rbn));
Tejun Heofd7b9f72013-11-28 14:54:33 -0500778
779 /* no sibling left, visit parent */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500780 return pos->parent;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500781}
782
Tejun Heoc525aad2013-12-11 14:11:55 -0500783static void __kernfs_remove(struct kernfs_addrm_cxt *acxt,
Tejun Heo324a56e2013-12-11 14:11:53 -0500784 struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500785{
Tejun Heo324a56e2013-12-11 14:11:53 -0500786 struct kernfs_node *pos, *next;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500787
Tejun Heo324a56e2013-12-11 14:11:53 -0500788 if (!kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500789 return;
790
Tejun Heoc637b8a2013-12-11 14:11:58 -0500791 pr_debug("kernfs %s: removing\n", kn->name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500792
793 next = NULL;
794 do {
795 pos = next;
Tejun Heoc637b8a2013-12-11 14:11:58 -0500796 next = kernfs_next_descendant_post(pos, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500797 if (pos)
Tejun Heoc637b8a2013-12-11 14:11:58 -0500798 kernfs_remove_one(acxt, pos);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500799 } while (next);
800}
801
802/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500803 * kernfs_remove - remove a kernfs_node recursively
804 * @kn: the kernfs_node to remove
Tejun Heofd7b9f72013-11-28 14:54:33 -0500805 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500806 * Remove @kn along with all its subdirectories and files.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500807 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500808void kernfs_remove(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500809{
Tejun Heoc525aad2013-12-11 14:11:55 -0500810 struct kernfs_addrm_cxt acxt;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500811
Tejun Heoc637b8a2013-12-11 14:11:58 -0500812 kernfs_addrm_start(&acxt);
Tejun Heo324a56e2013-12-11 14:11:53 -0500813 __kernfs_remove(&acxt, kn);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500814 kernfs_addrm_finish(&acxt);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500815}
816
817/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500818 * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
819 * @parent: parent of the target
820 * @name: name of the kernfs_node to remove
821 * @ns: namespace tag of the kernfs_node to remove
Tejun Heofd7b9f72013-11-28 14:54:33 -0500822 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500823 * Look for the kernfs_node with @name and @ns under @parent and remove it.
824 * Returns 0 on success, -ENOENT if such entry doesn't exist.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500825 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500826int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500827 const void *ns)
828{
Tejun Heoc525aad2013-12-11 14:11:55 -0500829 struct kernfs_addrm_cxt acxt;
Tejun Heo324a56e2013-12-11 14:11:53 -0500830 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500831
Tejun Heo324a56e2013-12-11 14:11:53 -0500832 if (!parent) {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500833 WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
Tejun Heofd7b9f72013-11-28 14:54:33 -0500834 name);
835 return -ENOENT;
836 }
837
Tejun Heoc637b8a2013-12-11 14:11:58 -0500838 kernfs_addrm_start(&acxt);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500839
Tejun Heo324a56e2013-12-11 14:11:53 -0500840 kn = kernfs_find_ns(parent, name, ns);
841 if (kn)
842 __kernfs_remove(&acxt, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500843
Tejun Heoc637b8a2013-12-11 14:11:58 -0500844 kernfs_addrm_finish(&acxt);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500845
Tejun Heo324a56e2013-12-11 14:11:53 -0500846 if (kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500847 return 0;
848 else
849 return -ENOENT;
850}
851
852/**
853 * kernfs_rename_ns - move and rename a kernfs_node
Tejun Heo324a56e2013-12-11 14:11:53 -0500854 * @kn: target node
Tejun Heofd7b9f72013-11-28 14:54:33 -0500855 * @new_parent: new parent to put @sd under
856 * @new_name: new name
857 * @new_ns: new namespace tag
858 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500859int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500860 const char *new_name, const void *new_ns)
861{
862 int error;
863
Tejun Heoa797bfc2013-12-11 14:11:57 -0500864 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500865
866 error = 0;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500867 if ((kn->parent == new_parent) && (kn->ns == new_ns) &&
868 (strcmp(kn->name, new_name) == 0))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500869 goto out; /* nothing to rename */
870
871 error = -EEXIST;
872 if (kernfs_find_ns(new_parent, new_name, new_ns))
873 goto out;
874
Tejun Heo324a56e2013-12-11 14:11:53 -0500875 /* rename kernfs_node */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500876 if (strcmp(kn->name, new_name) != 0) {
Tejun Heofd7b9f72013-11-28 14:54:33 -0500877 error = -ENOMEM;
878 new_name = kstrdup(new_name, GFP_KERNEL);
879 if (!new_name)
880 goto out;
881
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500882 kfree(kn->name);
883 kn->name = new_name;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500884 }
885
886 /*
887 * Move to the appropriate place in the appropriate directories rbtree.
888 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500889 kernfs_unlink_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500890 kernfs_get(new_parent);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500891 kernfs_put(kn->parent);
892 kn->ns = new_ns;
Tejun Heoc637b8a2013-12-11 14:11:58 -0500893 kn->hash = kernfs_name_hash(kn->name, kn->ns);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500894 kn->parent = new_parent;
Tejun Heoc637b8a2013-12-11 14:11:58 -0500895 kernfs_link_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500896
897 error = 0;
898 out:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500899 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500900 return error;
901}
902
Tejun Heofd7b9f72013-11-28 14:54:33 -0500903/* Relationship between s_mode and the DT_xxx types */
Tejun Heo324a56e2013-12-11 14:11:53 -0500904static inline unsigned char dt_type(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500905{
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500906 return (kn->mode >> 12) & 15;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500907}
908
Tejun Heoc637b8a2013-12-11 14:11:58 -0500909static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500910{
911 kernfs_put(filp->private_data);
912 return 0;
913}
914
Tejun Heoc637b8a2013-12-11 14:11:58 -0500915static struct kernfs_node *kernfs_dir_pos(const void *ns,
Tejun Heo324a56e2013-12-11 14:11:53 -0500916 struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500917{
918 if (pos) {
Tejun Heodf23fc32013-12-11 14:11:56 -0500919 int valid = !(pos->flags & KERNFS_REMOVED) &&
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500920 pos->parent == parent && hash == pos->hash;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500921 kernfs_put(pos);
922 if (!valid)
923 pos = NULL;
924 }
925 if (!pos && (hash > 1) && (hash < INT_MAX)) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500926 struct rb_node *node = parent->dir.children.rb_node;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500927 while (node) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500928 pos = rb_to_kn(node);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500929
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500930 if (hash < pos->hash)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500931 node = node->rb_left;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500932 else if (hash > pos->hash)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500933 node = node->rb_right;
934 else
935 break;
936 }
937 }
938 /* Skip over entries in the wrong namespace */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500939 while (pos && pos->ns != ns) {
940 struct rb_node *node = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500941 if (!node)
942 pos = NULL;
943 else
Tejun Heo324a56e2013-12-11 14:11:53 -0500944 pos = rb_to_kn(node);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500945 }
946 return pos;
947}
948
Tejun Heoc637b8a2013-12-11 14:11:58 -0500949static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
Tejun Heo324a56e2013-12-11 14:11:53 -0500950 struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500951{
Tejun Heoc637b8a2013-12-11 14:11:58 -0500952 pos = kernfs_dir_pos(ns, parent, ino, pos);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500953 if (pos)
954 do {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500955 struct rb_node *node = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500956 if (!node)
957 pos = NULL;
958 else
Tejun Heo324a56e2013-12-11 14:11:53 -0500959 pos = rb_to_kn(node);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500960 } while (pos && pos->ns != ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500961 return pos;
962}
963
Tejun Heoc637b8a2013-12-11 14:11:58 -0500964static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500965{
966 struct dentry *dentry = file->f_path.dentry;
Tejun Heo324a56e2013-12-11 14:11:53 -0500967 struct kernfs_node *parent = dentry->d_fsdata;
968 struct kernfs_node *pos = file->private_data;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500969 const void *ns = NULL;
970
971 if (!dir_emit_dots(file, ctx))
972 return 0;
Tejun Heoa797bfc2013-12-11 14:11:57 -0500973 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500974
Tejun Heo324a56e2013-12-11 14:11:53 -0500975 if (kernfs_ns_enabled(parent))
Tejun Heoc525aad2013-12-11 14:11:55 -0500976 ns = kernfs_info(dentry->d_sb)->ns;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500977
Tejun Heoc637b8a2013-12-11 14:11:58 -0500978 for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500979 pos;
Tejun Heoc637b8a2013-12-11 14:11:58 -0500980 pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500981 const char *name = pos->name;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500982 unsigned int type = dt_type(pos);
983 int len = strlen(name);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500984 ino_t ino = pos->ino;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500985
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500986 ctx->pos = pos->hash;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500987 file->private_data = pos;
988 kernfs_get(pos);
989
Tejun Heoa797bfc2013-12-11 14:11:57 -0500990 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500991 if (!dir_emit(ctx, name, len, ino, type))
992 return 0;
Tejun Heoa797bfc2013-12-11 14:11:57 -0500993 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500994 }
Tejun Heoa797bfc2013-12-11 14:11:57 -0500995 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500996 file->private_data = NULL;
997 ctx->pos = INT_MAX;
998 return 0;
999}
1000
Tejun Heoc637b8a2013-12-11 14:11:58 -05001001static loff_t kernfs_dir_fop_llseek(struct file *file, loff_t offset,
1002 int whence)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001003{
1004 struct inode *inode = file_inode(file);
1005 loff_t ret;
1006
1007 mutex_lock(&inode->i_mutex);
1008 ret = generic_file_llseek(file, offset, whence);
1009 mutex_unlock(&inode->i_mutex);
1010
1011 return ret;
1012}
1013
Tejun Heoa797bfc2013-12-11 14:11:57 -05001014const struct file_operations kernfs_dir_fops = {
Tejun Heofd7b9f72013-11-28 14:54:33 -05001015 .read = generic_read_dir,
Tejun Heoc637b8a2013-12-11 14:11:58 -05001016 .iterate = kernfs_fop_readdir,
1017 .release = kernfs_dir_fop_release,
1018 .llseek = kernfs_dir_fop_llseek,
Tejun Heofd7b9f72013-11-28 14:54:33 -05001019};