blob: 42a250f83b98cf1c294be3f4136aa9e837333e50 [file] [log] [blame]
Tejun Heob8441ed2013-11-24 09:54:58 -05001/*
2 * fs/kernfs/dir.c - kernfs directory implementation
3 *
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
7 *
8 * This file is released under the GPLv2.
9 */
Tejun Heofd7b9f72013-11-28 14:54:33 -050010
Tejun Heoabd54f02014-02-03 14:02:55 -050011#include <linux/sched.h>
Tejun Heofd7b9f72013-11-28 14:54:33 -050012#include <linux/fs.h>
13#include <linux/namei.h>
14#include <linux/idr.h>
15#include <linux/slab.h>
16#include <linux/security.h>
17#include <linux/hash.h>
18
19#include "kernfs-internal.h"
20
Tejun Heoa797bfc2013-12-11 14:11:57 -050021DEFINE_MUTEX(kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -050022
Tejun Heoadc5e8b2013-12-11 14:11:54 -050023#define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb)
Tejun Heofd7b9f72013-11-28 14:54:33 -050024
Tejun Heo81c173c2014-02-03 14:03:00 -050025static bool kernfs_active(struct kernfs_node *kn)
26{
27 lockdep_assert_held(&kernfs_mutex);
28 return atomic_read(&kn->active) >= 0;
29}
30
Tejun Heo182fd642014-02-03 14:02:59 -050031static bool kernfs_lockdep(struct kernfs_node *kn)
32{
33#ifdef CONFIG_DEBUG_LOCK_ALLOC
34 return kn->flags & KERNFS_LOCKDEP;
35#else
36 return false;
37#endif
38}
39
Tejun Heofd7b9f72013-11-28 14:54:33 -050040/**
Tejun Heoc637b8a2013-12-11 14:11:58 -050041 * kernfs_name_hash
Tejun Heofd7b9f72013-11-28 14:54:33 -050042 * @name: Null terminated string to hash
43 * @ns: Namespace tag to hash
44 *
45 * Returns 31 bit hash of ns + name (so it fits in an off_t )
46 */
Tejun Heoc637b8a2013-12-11 14:11:58 -050047static unsigned int kernfs_name_hash(const char *name, const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -050048{
49 unsigned long hash = init_name_hash();
50 unsigned int len = strlen(name);
51 while (len--)
52 hash = partial_name_hash(*name++, hash);
53 hash = (end_name_hash(hash) ^ hash_ptr((void *)ns, 31));
54 hash &= 0x7fffffffU;
55 /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */
56 if (hash < 1)
57 hash += 2;
58 if (hash >= INT_MAX)
59 hash = INT_MAX - 1;
60 return hash;
61}
62
Tejun Heoc637b8a2013-12-11 14:11:58 -050063static int kernfs_name_compare(unsigned int hash, const char *name,
64 const void *ns, const struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -050065{
Tejun Heoadc5e8b2013-12-11 14:11:54 -050066 if (hash != kn->hash)
67 return hash - kn->hash;
68 if (ns != kn->ns)
69 return ns - kn->ns;
70 return strcmp(name, kn->name);
Tejun Heofd7b9f72013-11-28 14:54:33 -050071}
72
Tejun Heoc637b8a2013-12-11 14:11:58 -050073static int kernfs_sd_compare(const struct kernfs_node *left,
74 const struct kernfs_node *right)
Tejun Heofd7b9f72013-11-28 14:54:33 -050075{
Tejun Heoc637b8a2013-12-11 14:11:58 -050076 return kernfs_name_compare(left->hash, left->name, left->ns, right);
Tejun Heofd7b9f72013-11-28 14:54:33 -050077}
78
79/**
Tejun Heoc637b8a2013-12-11 14:11:58 -050080 * kernfs_link_sibling - link kernfs_node into sibling rbtree
Tejun Heo324a56e2013-12-11 14:11:53 -050081 * @kn: kernfs_node of interest
Tejun Heofd7b9f72013-11-28 14:54:33 -050082 *
Tejun Heo324a56e2013-12-11 14:11:53 -050083 * Link @kn into its sibling rbtree which starts from
Tejun Heoadc5e8b2013-12-11 14:11:54 -050084 * @kn->parent->dir.children.
Tejun Heofd7b9f72013-11-28 14:54:33 -050085 *
86 * Locking:
Tejun Heoa797bfc2013-12-11 14:11:57 -050087 * mutex_lock(kernfs_mutex)
Tejun Heofd7b9f72013-11-28 14:54:33 -050088 *
89 * RETURNS:
90 * 0 on susccess -EEXIST on failure.
91 */
Tejun Heoc637b8a2013-12-11 14:11:58 -050092static int kernfs_link_sibling(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -050093{
Tejun Heoadc5e8b2013-12-11 14:11:54 -050094 struct rb_node **node = &kn->parent->dir.children.rb_node;
Tejun Heofd7b9f72013-11-28 14:54:33 -050095 struct rb_node *parent = NULL;
96
Tejun Heodf23fc32013-12-11 14:11:56 -050097 if (kernfs_type(kn) == KERNFS_DIR)
Tejun Heoadc5e8b2013-12-11 14:11:54 -050098 kn->parent->dir.subdirs++;
Tejun Heofd7b9f72013-11-28 14:54:33 -050099
100 while (*node) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500101 struct kernfs_node *pos;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500102 int result;
103
Tejun Heo324a56e2013-12-11 14:11:53 -0500104 pos = rb_to_kn(*node);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500105 parent = *node;
Tejun Heoc637b8a2013-12-11 14:11:58 -0500106 result = kernfs_sd_compare(kn, pos);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500107 if (result < 0)
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500108 node = &pos->rb.rb_left;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500109 else if (result > 0)
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500110 node = &pos->rb.rb_right;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500111 else
112 return -EEXIST;
113 }
114 /* add new node and rebalance the tree */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500115 rb_link_node(&kn->rb, parent, node);
116 rb_insert_color(&kn->rb, &kn->parent->dir.children);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500117 return 0;
118}
119
120/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500121 * kernfs_unlink_sibling - unlink kernfs_node from sibling rbtree
Tejun Heo324a56e2013-12-11 14:11:53 -0500122 * @kn: kernfs_node of interest
Tejun Heofd7b9f72013-11-28 14:54:33 -0500123 *
Tejun Heo35beab02014-02-03 14:02:56 -0500124 * Try to unlink @kn from its sibling rbtree which starts from
125 * kn->parent->dir.children. Returns %true if @kn was actually
126 * removed, %false if @kn wasn't on the rbtree.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500127 *
128 * Locking:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500129 * mutex_lock(kernfs_mutex)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500130 */
Tejun Heo35beab02014-02-03 14:02:56 -0500131static bool kernfs_unlink_sibling(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500132{
Tejun Heo35beab02014-02-03 14:02:56 -0500133 if (RB_EMPTY_NODE(&kn->rb))
134 return false;
135
Tejun Heodf23fc32013-12-11 14:11:56 -0500136 if (kernfs_type(kn) == KERNFS_DIR)
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500137 kn->parent->dir.subdirs--;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500138
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500139 rb_erase(&kn->rb, &kn->parent->dir.children);
Tejun Heo35beab02014-02-03 14:02:56 -0500140 RB_CLEAR_NODE(&kn->rb);
141 return true;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500142}
143
144/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500145 * kernfs_get_active - get an active reference to kernfs_node
Tejun Heo324a56e2013-12-11 14:11:53 -0500146 * @kn: kernfs_node to get an active reference to
Tejun Heofd7b9f72013-11-28 14:54:33 -0500147 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500148 * Get an active reference of @kn. This function is noop if @kn
Tejun Heofd7b9f72013-11-28 14:54:33 -0500149 * is NULL.
150 *
151 * RETURNS:
Tejun Heo324a56e2013-12-11 14:11:53 -0500152 * Pointer to @kn on success, NULL on failure.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500153 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500154struct kernfs_node *kernfs_get_active(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500155{
Tejun Heo324a56e2013-12-11 14:11:53 -0500156 if (unlikely(!kn))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500157 return NULL;
158
Greg Kroah-Hartmanf4b3e632014-01-13 14:13:39 -0800159 if (!atomic_inc_unless_negative(&kn->active))
160 return NULL;
161
Tejun Heo182fd642014-02-03 14:02:59 -0500162 if (kernfs_lockdep(kn))
Tejun Heo324a56e2013-12-11 14:11:53 -0500163 rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_);
Greg Kroah-Hartmanf4b3e632014-01-13 14:13:39 -0800164 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500165}
166
167/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500168 * kernfs_put_active - put an active reference to kernfs_node
Tejun Heo324a56e2013-12-11 14:11:53 -0500169 * @kn: kernfs_node to put an active reference to
Tejun Heofd7b9f72013-11-28 14:54:33 -0500170 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500171 * Put an active reference to @kn. This function is noop if @kn
Tejun Heofd7b9f72013-11-28 14:54:33 -0500172 * is NULL.
173 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500174void kernfs_put_active(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500175{
Tejun Heoabd54f02014-02-03 14:02:55 -0500176 struct kernfs_root *root = kernfs_root(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500177 int v;
178
Tejun Heo324a56e2013-12-11 14:11:53 -0500179 if (unlikely(!kn))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500180 return;
181
Tejun Heo182fd642014-02-03 14:02:59 -0500182 if (kernfs_lockdep(kn))
Tejun Heo324a56e2013-12-11 14:11:53 -0500183 rwsem_release(&kn->dep_map, 1, _RET_IP_);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500184 v = atomic_dec_return(&kn->active);
Tejun Heodf23fc32013-12-11 14:11:56 -0500185 if (likely(v != KN_DEACTIVATED_BIAS))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500186 return;
187
Tejun Heoabd54f02014-02-03 14:02:55 -0500188 wake_up_all(&root->deactivate_waitq);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500189}
190
191/**
Tejun Heo81c173c2014-02-03 14:03:00 -0500192 * kernfs_drain - drain kernfs_node
193 * @kn: kernfs_node to drain
Tejun Heofd7b9f72013-11-28 14:54:33 -0500194 *
Tejun Heo81c173c2014-02-03 14:03:00 -0500195 * Drain existing usages and nuke all existing mmaps of @kn. Mutiple
196 * removers may invoke this function concurrently on @kn and all will
197 * return after draining is complete.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500198 */
Tejun Heo81c173c2014-02-03 14:03:00 -0500199static void kernfs_drain(struct kernfs_node *kn)
Tejun Heo35beab02014-02-03 14:02:56 -0500200 __releases(&kernfs_mutex) __acquires(&kernfs_mutex)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500201{
Tejun Heoabd54f02014-02-03 14:02:55 -0500202 struct kernfs_root *root = kernfs_root(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500203
Tejun Heo35beab02014-02-03 14:02:56 -0500204 lockdep_assert_held(&kernfs_mutex);
Tejun Heo81c173c2014-02-03 14:03:00 -0500205 WARN_ON_ONCE(kernfs_active(kn));
Tejun Heo35beab02014-02-03 14:02:56 -0500206
207 mutex_unlock(&kernfs_mutex);
208
Tejun Heo182fd642014-02-03 14:02:59 -0500209 if (kernfs_lockdep(kn)) {
Tejun Heoa6607932014-02-03 14:02:54 -0500210 rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_);
Tejun Heo35beab02014-02-03 14:02:56 -0500211 if (atomic_read(&kn->active) != KN_DEACTIVATED_BIAS)
212 lock_contended(&kn->dep_map, _RET_IP_);
213 }
Greg Kroah-Hartman08901472014-01-13 14:39:52 -0800214
Tejun Heo35beab02014-02-03 14:02:56 -0500215 /* but everyone should wait for draining */
Tejun Heoabd54f02014-02-03 14:02:55 -0500216 wait_event(root->deactivate_waitq,
217 atomic_read(&kn->active) == KN_DEACTIVATED_BIAS);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500218
Tejun Heo182fd642014-02-03 14:02:59 -0500219 if (kernfs_lockdep(kn)) {
Tejun Heoa6607932014-02-03 14:02:54 -0500220 lock_acquired(&kn->dep_map, _RET_IP_);
221 rwsem_release(&kn->dep_map, 1, _RET_IP_);
222 }
Tejun Heo35beab02014-02-03 14:02:56 -0500223
Tejun Heoccf02aa2014-02-03 14:02:57 -0500224 kernfs_unmap_bin_file(kn);
225
Tejun Heo35beab02014-02-03 14:02:56 -0500226 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500227}
228
Tejun Heofd7b9f72013-11-28 14:54:33 -0500229/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500230 * kernfs_get - get a reference count on a kernfs_node
231 * @kn: the target kernfs_node
Tejun Heofd7b9f72013-11-28 14:54:33 -0500232 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500233void kernfs_get(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500234{
Tejun Heo324a56e2013-12-11 14:11:53 -0500235 if (kn) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500236 WARN_ON(!atomic_read(&kn->count));
237 atomic_inc(&kn->count);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500238 }
239}
240EXPORT_SYMBOL_GPL(kernfs_get);
241
242/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500243 * kernfs_put - put a reference count on a kernfs_node
244 * @kn: the target kernfs_node
Tejun Heofd7b9f72013-11-28 14:54:33 -0500245 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500246 * Put a reference count of @kn and destroy it if it reached zero.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500247 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500248void kernfs_put(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500249{
Tejun Heo324a56e2013-12-11 14:11:53 -0500250 struct kernfs_node *parent;
Tejun Heoba7443b2013-11-28 14:54:40 -0500251 struct kernfs_root *root;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500252
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500253 if (!kn || !atomic_dec_and_test(&kn->count))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500254 return;
Tejun Heo324a56e2013-12-11 14:11:53 -0500255 root = kernfs_root(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500256 repeat:
Tejun Heo81c173c2014-02-03 14:03:00 -0500257 /*
258 * Moving/renaming is always done while holding reference.
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500259 * kn->parent won't change beneath us.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500260 */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500261 parent = kn->parent;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500262
Tejun Heo81c173c2014-02-03 14:03:00 -0500263 WARN_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS,
264 "kernfs_put: %s/%s: released with incorrect active_ref %d\n",
265 parent ? parent->name : "", kn->name, atomic_read(&kn->active));
Tejun Heofd7b9f72013-11-28 14:54:33 -0500266
Tejun Heodf23fc32013-12-11 14:11:56 -0500267 if (kernfs_type(kn) == KERNFS_LINK)
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500268 kernfs_put(kn->symlink.target_kn);
Tejun Heo2063d602013-12-11 16:02:57 -0500269 if (!(kn->flags & KERNFS_STATIC_NAME))
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500270 kfree(kn->name);
271 if (kn->iattr) {
272 if (kn->iattr->ia_secdata)
273 security_release_secctx(kn->iattr->ia_secdata,
274 kn->iattr->ia_secdata_len);
275 simple_xattrs_free(&kn->iattr->xattrs);
Tejun Heo23223922013-11-23 17:40:02 -0500276 }
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500277 kfree(kn->iattr);
278 ida_simple_remove(&root->ino_ida, kn->ino);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500279 kmem_cache_free(kernfs_node_cache, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500280
Tejun Heo324a56e2013-12-11 14:11:53 -0500281 kn = parent;
282 if (kn) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500283 if (atomic_dec_and_test(&kn->count))
Tejun Heoba7443b2013-11-28 14:54:40 -0500284 goto repeat;
285 } else {
Tejun Heo324a56e2013-12-11 14:11:53 -0500286 /* just released the root kn, free @root too */
Tejun Heobc755552013-11-28 14:54:41 -0500287 ida_destroy(&root->ino_ida);
Tejun Heoba7443b2013-11-28 14:54:40 -0500288 kfree(root);
289 }
Tejun Heofd7b9f72013-11-28 14:54:33 -0500290}
291EXPORT_SYMBOL_GPL(kernfs_put);
292
Tejun Heoc637b8a2013-12-11 14:11:58 -0500293static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500294{
Tejun Heo324a56e2013-12-11 14:11:53 -0500295 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500296
297 if (flags & LOOKUP_RCU)
298 return -ECHILD;
299
Tejun Heo19bbb922013-12-11 16:02:59 -0500300 /* Always perform fresh lookup for negatives */
301 if (!dentry->d_inode)
302 goto out_bad_unlocked;
303
Tejun Heo324a56e2013-12-11 14:11:53 -0500304 kn = dentry->d_fsdata;
Tejun Heoa797bfc2013-12-11 14:11:57 -0500305 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500306
Tejun Heo81c173c2014-02-03 14:03:00 -0500307 /* The kernfs node has been deactivated */
308 if (!kernfs_active(kn))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500309 goto out_bad;
310
Tejun Heoc637b8a2013-12-11 14:11:58 -0500311 /* The kernfs node has been moved? */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500312 if (dentry->d_parent->d_fsdata != kn->parent)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500313 goto out_bad;
314
Tejun Heoc637b8a2013-12-11 14:11:58 -0500315 /* The kernfs node has been renamed */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500316 if (strcmp(dentry->d_name.name, kn->name) != 0)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500317 goto out_bad;
318
Tejun Heoc637b8a2013-12-11 14:11:58 -0500319 /* The kernfs node has been moved to a different namespace */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500320 if (kn->parent && kernfs_ns_enabled(kn->parent) &&
Tejun Heoc525aad2013-12-11 14:11:55 -0500321 kernfs_info(dentry->d_sb)->ns != kn->ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500322 goto out_bad;
323
Tejun Heoa797bfc2013-12-11 14:11:57 -0500324 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500325out_valid:
326 return 1;
327out_bad:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500328 mutex_unlock(&kernfs_mutex);
Tejun Heo19bbb922013-12-11 16:02:59 -0500329out_bad_unlocked:
330 /*
331 * @dentry doesn't match the underlying kernfs node, drop the
332 * dentry and force lookup. If we have submounts we must allow the
333 * vfs caches to lie about the state of the filesystem to prevent
334 * leaks and other nasty things, so use check_submounts_and_drop()
335 * instead of d_drop().
Tejun Heofd7b9f72013-11-28 14:54:33 -0500336 */
337 if (check_submounts_and_drop(dentry) != 0)
338 goto out_valid;
339
340 return 0;
341}
342
Tejun Heoc637b8a2013-12-11 14:11:58 -0500343static void kernfs_dop_release(struct dentry *dentry)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500344{
345 kernfs_put(dentry->d_fsdata);
346}
347
Tejun Heoa797bfc2013-12-11 14:11:57 -0500348const struct dentry_operations kernfs_dops = {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500349 .d_revalidate = kernfs_dop_revalidate,
Tejun Heoc637b8a2013-12-11 14:11:58 -0500350 .d_release = kernfs_dop_release,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500351};
352
Tejun Heo0c23b222014-02-03 14:09:15 -0500353/**
354 * kernfs_node_from_dentry - determine kernfs_node associated with a dentry
355 * @dentry: the dentry in question
356 *
357 * Return the kernfs_node associated with @dentry. If @dentry is not a
358 * kernfs one, %NULL is returned.
359 *
360 * While the returned kernfs_node will stay accessible as long as @dentry
361 * is accessible, the returned node can be in any state and the caller is
362 * fully responsible for determining what's accessible.
363 */
364struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry)
365{
366 if (dentry->d_op == &kernfs_dops)
367 return dentry->d_fsdata;
368 return NULL;
369}
370
Tejun Heodb4aad22014-01-17 09:58:25 -0500371static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
372 const char *name, umode_t mode,
373 unsigned flags)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500374{
375 char *dup_name = NULL;
Tejun Heo324a56e2013-12-11 14:11:53 -0500376 struct kernfs_node *kn;
Tejun Heobc755552013-11-28 14:54:41 -0500377 int ret;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500378
Tejun Heo2063d602013-12-11 16:02:57 -0500379 if (!(flags & KERNFS_STATIC_NAME)) {
Tejun Heofd7b9f72013-11-28 14:54:33 -0500380 name = dup_name = kstrdup(name, GFP_KERNEL);
381 if (!name)
382 return NULL;
383 }
384
Tejun Heoa797bfc2013-12-11 14:11:57 -0500385 kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL);
Tejun Heo324a56e2013-12-11 14:11:53 -0500386 if (!kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500387 goto err_out1;
388
Tejun Heobc755552013-11-28 14:54:41 -0500389 ret = ida_simple_get(&root->ino_ida, 1, 0, GFP_KERNEL);
390 if (ret < 0)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500391 goto err_out2;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500392 kn->ino = ret;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500393
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500394 atomic_set(&kn->count, 1);
Tejun Heo81c173c2014-02-03 14:03:00 -0500395 atomic_set(&kn->active, KN_DEACTIVATED_BIAS);
Tejun Heo35beab02014-02-03 14:02:56 -0500396 RB_CLEAR_NODE(&kn->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500397
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500398 kn->name = name;
399 kn->mode = mode;
Tejun Heo81c173c2014-02-03 14:03:00 -0500400 kn->flags = flags;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500401
Tejun Heo324a56e2013-12-11 14:11:53 -0500402 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500403
404 err_out2:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500405 kmem_cache_free(kernfs_node_cache, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500406 err_out1:
407 kfree(dup_name);
408 return NULL;
409}
410
Tejun Heodb4aad22014-01-17 09:58:25 -0500411struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
412 const char *name, umode_t mode,
413 unsigned flags)
414{
415 struct kernfs_node *kn;
416
417 kn = __kernfs_new_node(kernfs_root(parent), name, mode, flags);
418 if (kn) {
419 kernfs_get(parent);
420 kn->parent = parent;
421 }
422 return kn;
423}
424
Tejun Heofd7b9f72013-11-28 14:54:33 -0500425/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500426 * kernfs_add_one - add kernfs_node to parent without warning
Tejun Heo324a56e2013-12-11 14:11:53 -0500427 * @kn: kernfs_node to be added
Tejun Heofd7b9f72013-11-28 14:54:33 -0500428 *
Tejun Heodb4aad22014-01-17 09:58:25 -0500429 * The caller must already have initialized @kn->parent. This
430 * function increments nlink of the parent's inode if @kn is a
431 * directory and link into the children list of the parent.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500432 *
Tejun Heofd7b9f72013-11-28 14:54:33 -0500433 * RETURNS:
434 * 0 on success, -EEXIST if entry with the given name already
435 * exists.
436 */
Tejun Heo988cd7a2014-02-03 14:02:58 -0500437int kernfs_add_one(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500438{
Tejun Heodb4aad22014-01-17 09:58:25 -0500439 struct kernfs_node *parent = kn->parent;
Tejun Heoc525aad2013-12-11 14:11:55 -0500440 struct kernfs_iattrs *ps_iattr;
Tejun Heo988cd7a2014-02-03 14:02:58 -0500441 bool has_ns;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500442 int ret;
443
Tejun Heo988cd7a2014-02-03 14:02:58 -0500444 mutex_lock(&kernfs_mutex);
445
446 ret = -EINVAL;
447 has_ns = kernfs_ns_enabled(parent);
448 if (WARN(has_ns != (bool)kn->ns, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
449 has_ns ? "required" : "invalid", parent->name, kn->name))
450 goto out_unlock;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500451
Tejun Heodf23fc32013-12-11 14:11:56 -0500452 if (kernfs_type(parent) != KERNFS_DIR)
Tejun Heo988cd7a2014-02-03 14:02:58 -0500453 goto out_unlock;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500454
Tejun Heo988cd7a2014-02-03 14:02:58 -0500455 ret = -ENOENT;
Tejun Heod35258e2014-02-03 14:09:12 -0500456 if ((parent->flags & KERNFS_ACTIVATED) && !kernfs_active(parent))
Tejun Heo988cd7a2014-02-03 14:02:58 -0500457 goto out_unlock;
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800458
Tejun Heoc637b8a2013-12-11 14:11:58 -0500459 kn->hash = kernfs_name_hash(kn->name, kn->ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500460
Tejun Heoc637b8a2013-12-11 14:11:58 -0500461 ret = kernfs_link_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500462 if (ret)
Tejun Heo988cd7a2014-02-03 14:02:58 -0500463 goto out_unlock;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500464
465 /* Update timestamps on the parent */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500466 ps_iattr = parent->iattr;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500467 if (ps_iattr) {
468 struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
469 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
470 }
471
Tejun Heod35258e2014-02-03 14:09:12 -0500472 mutex_unlock(&kernfs_mutex);
473
474 /*
475 * Activate the new node unless CREATE_DEACTIVATED is requested.
476 * If not activated here, the kernfs user is responsible for
477 * activating the node with kernfs_activate(). A node which hasn't
478 * been activated is not visible to userland and its removal won't
479 * trigger deactivation.
480 */
481 if (!(kernfs_root(kn)->flags & KERNFS_ROOT_CREATE_DEACTIVATED))
482 kernfs_activate(kn);
483 return 0;
484
Tejun Heo988cd7a2014-02-03 14:02:58 -0500485out_unlock:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500486 mutex_unlock(&kernfs_mutex);
Tejun Heo988cd7a2014-02-03 14:02:58 -0500487 return ret;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500488}
489
490/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500491 * kernfs_find_ns - find kernfs_node with the given name
492 * @parent: kernfs_node to search under
Tejun Heofd7b9f72013-11-28 14:54:33 -0500493 * @name: name to look for
494 * @ns: the namespace tag to use
495 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500496 * Look for kernfs_node with name @name under @parent. Returns pointer to
497 * the found kernfs_node on success, %NULL on failure.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500498 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500499static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
500 const unsigned char *name,
501 const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500502{
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500503 struct rb_node *node = parent->dir.children.rb_node;
Tejun Heoac9bba02013-11-29 17:19:09 -0500504 bool has_ns = kernfs_ns_enabled(parent);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500505 unsigned int hash;
506
Tejun Heoa797bfc2013-12-11 14:11:57 -0500507 lockdep_assert_held(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500508
509 if (has_ns != (bool)ns) {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500510 WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500511 has_ns ? "required" : "invalid", parent->name, name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500512 return NULL;
513 }
514
Tejun Heoc637b8a2013-12-11 14:11:58 -0500515 hash = kernfs_name_hash(name, ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500516 while (node) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500517 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500518 int result;
519
Tejun Heo324a56e2013-12-11 14:11:53 -0500520 kn = rb_to_kn(node);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500521 result = kernfs_name_compare(hash, name, ns, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500522 if (result < 0)
523 node = node->rb_left;
524 else if (result > 0)
525 node = node->rb_right;
526 else
Tejun Heo324a56e2013-12-11 14:11:53 -0500527 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500528 }
529 return NULL;
530}
531
532/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500533 * kernfs_find_and_get_ns - find and get kernfs_node with the given name
534 * @parent: kernfs_node to search under
Tejun Heofd7b9f72013-11-28 14:54:33 -0500535 * @name: name to look for
536 * @ns: the namespace tag to use
537 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500538 * Look for kernfs_node with name @name under @parent and get a reference
Tejun Heofd7b9f72013-11-28 14:54:33 -0500539 * if found. This function may sleep and returns pointer to the found
Tejun Heo324a56e2013-12-11 14:11:53 -0500540 * kernfs_node on success, %NULL on failure.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500541 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500542struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
543 const char *name, const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500544{
Tejun Heo324a56e2013-12-11 14:11:53 -0500545 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500546
Tejun Heoa797bfc2013-12-11 14:11:57 -0500547 mutex_lock(&kernfs_mutex);
Tejun Heo324a56e2013-12-11 14:11:53 -0500548 kn = kernfs_find_ns(parent, name, ns);
549 kernfs_get(kn);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500550 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500551
Tejun Heo324a56e2013-12-11 14:11:53 -0500552 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500553}
554EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
555
556/**
Tejun Heoba7443b2013-11-28 14:54:40 -0500557 * kernfs_create_root - create a new kernfs hierarchy
Tejun Heo90c07c82014-02-03 14:09:09 -0500558 * @scops: optional syscall operations for the hierarchy
Tejun Heod35258e2014-02-03 14:09:12 -0500559 * @flags: KERNFS_ROOT_* flags
Tejun Heoba7443b2013-11-28 14:54:40 -0500560 * @priv: opaque data associated with the new directory
561 *
562 * Returns the root of the new hierarchy on success, ERR_PTR() value on
563 * failure.
564 */
Tejun Heo90c07c82014-02-03 14:09:09 -0500565struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
Tejun Heod35258e2014-02-03 14:09:12 -0500566 unsigned int flags, void *priv)
Tejun Heoba7443b2013-11-28 14:54:40 -0500567{
568 struct kernfs_root *root;
Tejun Heo324a56e2013-12-11 14:11:53 -0500569 struct kernfs_node *kn;
Tejun Heoba7443b2013-11-28 14:54:40 -0500570
571 root = kzalloc(sizeof(*root), GFP_KERNEL);
572 if (!root)
573 return ERR_PTR(-ENOMEM);
574
Tejun Heobc755552013-11-28 14:54:41 -0500575 ida_init(&root->ino_ida);
576
Tejun Heodb4aad22014-01-17 09:58:25 -0500577 kn = __kernfs_new_node(root, "", S_IFDIR | S_IRUGO | S_IXUGO,
578 KERNFS_DIR);
Tejun Heo324a56e2013-12-11 14:11:53 -0500579 if (!kn) {
Tejun Heobc755552013-11-28 14:54:41 -0500580 ida_destroy(&root->ino_ida);
Tejun Heoba7443b2013-11-28 14:54:40 -0500581 kfree(root);
582 return ERR_PTR(-ENOMEM);
583 }
584
Tejun Heo324a56e2013-12-11 14:11:53 -0500585 kn->priv = priv;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500586 kn->dir.root = root;
Tejun Heoba7443b2013-11-28 14:54:40 -0500587
Tejun Heo90c07c82014-02-03 14:09:09 -0500588 root->syscall_ops = scops;
Tejun Heod35258e2014-02-03 14:09:12 -0500589 root->flags = flags;
Tejun Heo324a56e2013-12-11 14:11:53 -0500590 root->kn = kn;
Tejun Heoabd54f02014-02-03 14:02:55 -0500591 init_waitqueue_head(&root->deactivate_waitq);
Tejun Heoba7443b2013-11-28 14:54:40 -0500592
Tejun Heod35258e2014-02-03 14:09:12 -0500593 if (!(root->flags & KERNFS_ROOT_CREATE_DEACTIVATED))
594 kernfs_activate(kn);
595
Tejun Heoba7443b2013-11-28 14:54:40 -0500596 return root;
597}
598
599/**
600 * kernfs_destroy_root - destroy a kernfs hierarchy
601 * @root: root of the hierarchy to destroy
602 *
603 * Destroy the hierarchy anchored at @root by removing all existing
604 * directories and destroying @root.
605 */
606void kernfs_destroy_root(struct kernfs_root *root)
607{
Tejun Heo324a56e2013-12-11 14:11:53 -0500608 kernfs_remove(root->kn); /* will also free @root */
Tejun Heoba7443b2013-11-28 14:54:40 -0500609}
610
611/**
Tejun Heofd7b9f72013-11-28 14:54:33 -0500612 * kernfs_create_dir_ns - create a directory
613 * @parent: parent in which to create a new directory
614 * @name: name of the new directory
Tejun Heobb8b9d02013-12-11 16:02:55 -0500615 * @mode: mode of the new directory
Tejun Heofd7b9f72013-11-28 14:54:33 -0500616 * @priv: opaque data associated with the new directory
617 * @ns: optional namespace tag of the directory
618 *
619 * Returns the created node on success, ERR_PTR() value on failure.
620 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500621struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
Tejun Heobb8b9d02013-12-11 16:02:55 -0500622 const char *name, umode_t mode,
623 void *priv, const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500624{
Tejun Heo324a56e2013-12-11 14:11:53 -0500625 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500626 int rc;
627
628 /* allocate */
Tejun Heodb4aad22014-01-17 09:58:25 -0500629 kn = kernfs_new_node(parent, name, mode | S_IFDIR, KERNFS_DIR);
Tejun Heo324a56e2013-12-11 14:11:53 -0500630 if (!kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500631 return ERR_PTR(-ENOMEM);
632
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500633 kn->dir.root = parent->dir.root;
634 kn->ns = ns;
Tejun Heo324a56e2013-12-11 14:11:53 -0500635 kn->priv = priv;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500636
637 /* link in */
Tejun Heo988cd7a2014-02-03 14:02:58 -0500638 rc = kernfs_add_one(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500639 if (!rc)
Tejun Heo324a56e2013-12-11 14:11:53 -0500640 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500641
Tejun Heo324a56e2013-12-11 14:11:53 -0500642 kernfs_put(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500643 return ERR_PTR(rc);
644}
645
Tejun Heoc637b8a2013-12-11 14:11:58 -0500646static struct dentry *kernfs_iop_lookup(struct inode *dir,
647 struct dentry *dentry,
648 unsigned int flags)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500649{
Tejun Heo19bbb922013-12-11 16:02:59 -0500650 struct dentry *ret;
Tejun Heo324a56e2013-12-11 14:11:53 -0500651 struct kernfs_node *parent = dentry->d_parent->d_fsdata;
652 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500653 struct inode *inode;
654 const void *ns = NULL;
655
Tejun Heoa797bfc2013-12-11 14:11:57 -0500656 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500657
Tejun Heo324a56e2013-12-11 14:11:53 -0500658 if (kernfs_ns_enabled(parent))
Tejun Heoc525aad2013-12-11 14:11:55 -0500659 ns = kernfs_info(dir->i_sb)->ns;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500660
Tejun Heo324a56e2013-12-11 14:11:53 -0500661 kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500662
663 /* no such entry */
Tejun Heob9c9dad2014-02-03 14:09:11 -0500664 if (!kn || !kernfs_active(kn)) {
Tejun Heo19bbb922013-12-11 16:02:59 -0500665 ret = NULL;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500666 goto out_unlock;
667 }
Tejun Heo324a56e2013-12-11 14:11:53 -0500668 kernfs_get(kn);
669 dentry->d_fsdata = kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500670
671 /* attach dentry and inode */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500672 inode = kernfs_get_inode(dir->i_sb, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500673 if (!inode) {
674 ret = ERR_PTR(-ENOMEM);
675 goto out_unlock;
676 }
677
678 /* instantiate and hash dentry */
679 ret = d_materialise_unique(dentry, inode);
680 out_unlock:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500681 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500682 return ret;
683}
684
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500685static int kernfs_iop_mkdir(struct inode *dir, struct dentry *dentry,
686 umode_t mode)
687{
688 struct kernfs_node *parent = dir->i_private;
Tejun Heo90c07c82014-02-03 14:09:09 -0500689 struct kernfs_syscall_ops *scops = kernfs_root(parent)->syscall_ops;
Tejun Heo07c75302014-02-03 14:09:08 -0500690 int ret;
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500691
Tejun Heo90c07c82014-02-03 14:09:09 -0500692 if (!scops || !scops->mkdir)
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500693 return -EPERM;
694
Tejun Heo07c75302014-02-03 14:09:08 -0500695 if (!kernfs_get_active(parent))
696 return -ENODEV;
697
Tejun Heo90c07c82014-02-03 14:09:09 -0500698 ret = scops->mkdir(parent, dentry->d_name.name, mode);
Tejun Heo07c75302014-02-03 14:09:08 -0500699
700 kernfs_put_active(parent);
701 return ret;
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500702}
703
704static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
705{
706 struct kernfs_node *kn = dentry->d_fsdata;
Tejun Heo90c07c82014-02-03 14:09:09 -0500707 struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops;
Tejun Heo07c75302014-02-03 14:09:08 -0500708 int ret;
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500709
Tejun Heo90c07c82014-02-03 14:09:09 -0500710 if (!scops || !scops->rmdir)
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500711 return -EPERM;
712
Tejun Heo07c75302014-02-03 14:09:08 -0500713 if (!kernfs_get_active(kn))
714 return -ENODEV;
715
Tejun Heo90c07c82014-02-03 14:09:09 -0500716 ret = scops->rmdir(kn);
Tejun Heo07c75302014-02-03 14:09:08 -0500717
718 kernfs_put_active(kn);
719 return ret;
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500720}
721
722static int kernfs_iop_rename(struct inode *old_dir, struct dentry *old_dentry,
723 struct inode *new_dir, struct dentry *new_dentry)
724{
725 struct kernfs_node *kn = old_dentry->d_fsdata;
726 struct kernfs_node *new_parent = new_dir->i_private;
Tejun Heo90c07c82014-02-03 14:09:09 -0500727 struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops;
Tejun Heo07c75302014-02-03 14:09:08 -0500728 int ret;
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500729
Tejun Heo90c07c82014-02-03 14:09:09 -0500730 if (!scops || !scops->rename)
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500731 return -EPERM;
732
Tejun Heo07c75302014-02-03 14:09:08 -0500733 if (!kernfs_get_active(kn))
734 return -ENODEV;
735
736 if (!kernfs_get_active(new_parent)) {
737 kernfs_put_active(kn);
738 return -ENODEV;
739 }
740
Tejun Heo90c07c82014-02-03 14:09:09 -0500741 ret = scops->rename(kn, new_parent, new_dentry->d_name.name);
Tejun Heo07c75302014-02-03 14:09:08 -0500742
743 kernfs_put_active(new_parent);
744 kernfs_put_active(kn);
745 return ret;
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500746}
747
Tejun Heoa797bfc2013-12-11 14:11:57 -0500748const struct inode_operations kernfs_dir_iops = {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500749 .lookup = kernfs_iop_lookup,
750 .permission = kernfs_iop_permission,
751 .setattr = kernfs_iop_setattr,
752 .getattr = kernfs_iop_getattr,
753 .setxattr = kernfs_iop_setxattr,
754 .removexattr = kernfs_iop_removexattr,
755 .getxattr = kernfs_iop_getxattr,
756 .listxattr = kernfs_iop_listxattr,
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500757
758 .mkdir = kernfs_iop_mkdir,
759 .rmdir = kernfs_iop_rmdir,
760 .rename = kernfs_iop_rename,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500761};
762
Tejun Heoc637b8a2013-12-11 14:11:58 -0500763static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500764{
Tejun Heo324a56e2013-12-11 14:11:53 -0500765 struct kernfs_node *last;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500766
767 while (true) {
768 struct rb_node *rbn;
769
770 last = pos;
771
Tejun Heodf23fc32013-12-11 14:11:56 -0500772 if (kernfs_type(pos) != KERNFS_DIR)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500773 break;
774
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500775 rbn = rb_first(&pos->dir.children);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500776 if (!rbn)
777 break;
778
Tejun Heo324a56e2013-12-11 14:11:53 -0500779 pos = rb_to_kn(rbn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500780 }
781
782 return last;
783}
784
785/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500786 * kernfs_next_descendant_post - find the next descendant for post-order walk
Tejun Heofd7b9f72013-11-28 14:54:33 -0500787 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo324a56e2013-12-11 14:11:53 -0500788 * @root: kernfs_node whose descendants to walk
Tejun Heofd7b9f72013-11-28 14:54:33 -0500789 *
790 * Find the next descendant to visit for post-order traversal of @root's
791 * descendants. @root is included in the iteration and the last node to be
792 * visited.
793 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500794static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
795 struct kernfs_node *root)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500796{
797 struct rb_node *rbn;
798
Tejun Heoa797bfc2013-12-11 14:11:57 -0500799 lockdep_assert_held(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500800
801 /* if first iteration, visit leftmost descendant which may be root */
802 if (!pos)
Tejun Heoc637b8a2013-12-11 14:11:58 -0500803 return kernfs_leftmost_descendant(root);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500804
805 /* if we visited @root, we're done */
806 if (pos == root)
807 return NULL;
808
809 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500810 rbn = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500811 if (rbn)
Tejun Heoc637b8a2013-12-11 14:11:58 -0500812 return kernfs_leftmost_descendant(rb_to_kn(rbn));
Tejun Heofd7b9f72013-11-28 14:54:33 -0500813
814 /* no sibling left, visit parent */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500815 return pos->parent;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500816}
817
Tejun Heod35258e2014-02-03 14:09:12 -0500818/**
819 * kernfs_activate - activate a node which started deactivated
820 * @kn: kernfs_node whose subtree is to be activated
821 *
822 * If the root has KERNFS_ROOT_CREATE_DEACTIVATED set, a newly created node
823 * needs to be explicitly activated. A node which hasn't been activated
824 * isn't visible to userland and deactivation is skipped during its
825 * removal. This is useful to construct atomic init sequences where
826 * creation of multiple nodes should either succeed or fail atomically.
827 *
828 * The caller is responsible for ensuring that this function is not called
829 * after kernfs_remove*() is invoked on @kn.
830 */
831void kernfs_activate(struct kernfs_node *kn)
832{
833 struct kernfs_node *pos;
834
835 mutex_lock(&kernfs_mutex);
836
837 pos = NULL;
838 while ((pos = kernfs_next_descendant_post(pos, kn))) {
839 if (!pos || (pos->flags & KERNFS_ACTIVATED))
840 continue;
841
842 WARN_ON_ONCE(pos->parent && RB_EMPTY_NODE(&pos->rb));
843 WARN_ON_ONCE(atomic_read(&pos->active) != KN_DEACTIVATED_BIAS);
844
845 atomic_sub(KN_DEACTIVATED_BIAS, &pos->active);
846 pos->flags |= KERNFS_ACTIVATED;
847 }
848
849 mutex_unlock(&kernfs_mutex);
850}
851
Tejun Heo988cd7a2014-02-03 14:02:58 -0500852static void __kernfs_remove(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500853{
Tejun Heo35beab02014-02-03 14:02:56 -0500854 struct kernfs_node *pos;
855
856 lockdep_assert_held(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500857
Tejun Heo6b0afc22014-02-03 14:03:01 -0500858 /*
859 * Short-circuit if non-root @kn has already finished removal.
860 * This is for kernfs_remove_self() which plays with active ref
861 * after removal.
862 */
863 if (!kn || (kn->parent && RB_EMPTY_NODE(&kn->rb)))
Greg Kroah-Hartmance9b4992014-01-13 13:50:31 -0800864 return;
865
Tejun Heoc637b8a2013-12-11 14:11:58 -0500866 pr_debug("kernfs %s: removing\n", kn->name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500867
Tejun Heo81c173c2014-02-03 14:03:00 -0500868 /* prevent any new usage under @kn by deactivating all nodes */
Tejun Heo35beab02014-02-03 14:02:56 -0500869 pos = NULL;
870 while ((pos = kernfs_next_descendant_post(pos, kn)))
Tejun Heo81c173c2014-02-03 14:03:00 -0500871 if (kernfs_active(pos))
872 atomic_add(KN_DEACTIVATED_BIAS, &pos->active);
Tejun Heo35beab02014-02-03 14:02:56 -0500873
874 /* deactivate and unlink the subtree node-by-node */
Tejun Heofd7b9f72013-11-28 14:54:33 -0500875 do {
Tejun Heo35beab02014-02-03 14:02:56 -0500876 pos = kernfs_leftmost_descendant(kn);
877
878 /*
Tejun Heo81c173c2014-02-03 14:03:00 -0500879 * kernfs_drain() drops kernfs_mutex temporarily and @pos's
880 * base ref could have been put by someone else by the time
881 * the function returns. Make sure it doesn't go away
882 * underneath us.
Tejun Heo35beab02014-02-03 14:02:56 -0500883 */
884 kernfs_get(pos);
885
Tejun Heod35258e2014-02-03 14:09:12 -0500886 /*
887 * Drain iff @kn was activated. This avoids draining and
888 * its lockdep annotations for nodes which have never been
889 * activated and allows embedding kernfs_remove() in create
890 * error paths without worrying about draining.
891 */
892 if (kn->flags & KERNFS_ACTIVATED)
893 kernfs_drain(pos);
894 else
895 WARN_ON_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS);
Tejun Heo35beab02014-02-03 14:02:56 -0500896
897 /*
898 * kernfs_unlink_sibling() succeeds once per node. Use it
899 * to decide who's responsible for cleanups.
900 */
901 if (!pos->parent || kernfs_unlink_sibling(pos)) {
902 struct kernfs_iattrs *ps_iattr =
903 pos->parent ? pos->parent->iattr : NULL;
904
905 /* update timestamps on the parent */
906 if (ps_iattr) {
907 ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME;
908 ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME;
909 }
910
Tejun Heo988cd7a2014-02-03 14:02:58 -0500911 kernfs_put(pos);
Tejun Heo35beab02014-02-03 14:02:56 -0500912 }
913
914 kernfs_put(pos);
915 } while (pos != kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500916}
917
918/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500919 * kernfs_remove - remove a kernfs_node recursively
920 * @kn: the kernfs_node to remove
Tejun Heofd7b9f72013-11-28 14:54:33 -0500921 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500922 * Remove @kn along with all its subdirectories and files.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500923 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500924void kernfs_remove(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500925{
Tejun Heo988cd7a2014-02-03 14:02:58 -0500926 mutex_lock(&kernfs_mutex);
927 __kernfs_remove(kn);
928 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500929}
930
931/**
Tejun Heo6b0afc22014-02-03 14:03:01 -0500932 * kernfs_break_active_protection - break out of active protection
933 * @kn: the self kernfs_node
934 *
935 * The caller must be running off of a kernfs operation which is invoked
936 * with an active reference - e.g. one of kernfs_ops. Each invocation of
937 * this function must also be matched with an invocation of
938 * kernfs_unbreak_active_protection().
939 *
940 * This function releases the active reference of @kn the caller is
941 * holding. Once this function is called, @kn may be removed at any point
942 * and the caller is solely responsible for ensuring that the objects it
943 * dereferences are accessible.
944 */
945void kernfs_break_active_protection(struct kernfs_node *kn)
946{
947 /*
948 * Take out ourself out of the active ref dependency chain. If
949 * we're called without an active ref, lockdep will complain.
950 */
951 kernfs_put_active(kn);
952}
953
954/**
955 * kernfs_unbreak_active_protection - undo kernfs_break_active_protection()
956 * @kn: the self kernfs_node
957 *
958 * If kernfs_break_active_protection() was called, this function must be
959 * invoked before finishing the kernfs operation. Note that while this
960 * function restores the active reference, it doesn't and can't actually
961 * restore the active protection - @kn may already or be in the process of
962 * being removed. Once kernfs_break_active_protection() is invoked, that
963 * protection is irreversibly gone for the kernfs operation instance.
964 *
965 * While this function may be called at any point after
966 * kernfs_break_active_protection() is invoked, its most useful location
967 * would be right before the enclosing kernfs operation returns.
968 */
969void kernfs_unbreak_active_protection(struct kernfs_node *kn)
970{
971 /*
972 * @kn->active could be in any state; however, the increment we do
973 * here will be undone as soon as the enclosing kernfs operation
974 * finishes and this temporary bump can't break anything. If @kn
975 * is alive, nothing changes. If @kn is being deactivated, the
976 * soon-to-follow put will either finish deactivation or restore
977 * deactivated state. If @kn is already removed, the temporary
978 * bump is guaranteed to be gone before @kn is released.
979 */
980 atomic_inc(&kn->active);
981 if (kernfs_lockdep(kn))
982 rwsem_acquire(&kn->dep_map, 0, 1, _RET_IP_);
983}
984
985/**
986 * kernfs_remove_self - remove a kernfs_node from its own method
987 * @kn: the self kernfs_node to remove
988 *
989 * The caller must be running off of a kernfs operation which is invoked
990 * with an active reference - e.g. one of kernfs_ops. This can be used to
991 * implement a file operation which deletes itself.
992 *
993 * For example, the "delete" file for a sysfs device directory can be
994 * implemented by invoking kernfs_remove_self() on the "delete" file
995 * itself. This function breaks the circular dependency of trying to
996 * deactivate self while holding an active ref itself. It isn't necessary
997 * to modify the usual removal path to use kernfs_remove_self(). The
998 * "delete" implementation can simply invoke kernfs_remove_self() on self
999 * before proceeding with the usual removal path. kernfs will ignore later
1000 * kernfs_remove() on self.
1001 *
1002 * kernfs_remove_self() can be called multiple times concurrently on the
1003 * same kernfs_node. Only the first one actually performs removal and
1004 * returns %true. All others will wait until the kernfs operation which
1005 * won self-removal finishes and return %false. Note that the losers wait
1006 * for the completion of not only the winning kernfs_remove_self() but also
1007 * the whole kernfs_ops which won the arbitration. This can be used to
1008 * guarantee, for example, all concurrent writes to a "delete" file to
1009 * finish only after the whole operation is complete.
1010 */
1011bool kernfs_remove_self(struct kernfs_node *kn)
1012{
1013 bool ret;
1014
1015 mutex_lock(&kernfs_mutex);
1016 kernfs_break_active_protection(kn);
1017
1018 /*
1019 * SUICIDAL is used to arbitrate among competing invocations. Only
1020 * the first one will actually perform removal. When the removal
1021 * is complete, SUICIDED is set and the active ref is restored
1022 * while holding kernfs_mutex. The ones which lost arbitration
1023 * waits for SUICDED && drained which can happen only after the
1024 * enclosing kernfs operation which executed the winning instance
1025 * of kernfs_remove_self() finished.
1026 */
1027 if (!(kn->flags & KERNFS_SUICIDAL)) {
1028 kn->flags |= KERNFS_SUICIDAL;
1029 __kernfs_remove(kn);
1030 kn->flags |= KERNFS_SUICIDED;
1031 ret = true;
1032 } else {
1033 wait_queue_head_t *waitq = &kernfs_root(kn)->deactivate_waitq;
1034 DEFINE_WAIT(wait);
1035
1036 while (true) {
1037 prepare_to_wait(waitq, &wait, TASK_UNINTERRUPTIBLE);
1038
1039 if ((kn->flags & KERNFS_SUICIDED) &&
1040 atomic_read(&kn->active) == KN_DEACTIVATED_BIAS)
1041 break;
1042
1043 mutex_unlock(&kernfs_mutex);
1044 schedule();
1045 mutex_lock(&kernfs_mutex);
1046 }
1047 finish_wait(waitq, &wait);
1048 WARN_ON_ONCE(!RB_EMPTY_NODE(&kn->rb));
1049 ret = false;
1050 }
1051
1052 /*
1053 * This must be done while holding kernfs_mutex; otherwise, waiting
1054 * for SUICIDED && deactivated could finish prematurely.
1055 */
1056 kernfs_unbreak_active_protection(kn);
1057
1058 mutex_unlock(&kernfs_mutex);
1059 return ret;
1060}
1061
1062/**
Tejun Heo324a56e2013-12-11 14:11:53 -05001063 * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
1064 * @parent: parent of the target
1065 * @name: name of the kernfs_node to remove
1066 * @ns: namespace tag of the kernfs_node to remove
Tejun Heofd7b9f72013-11-28 14:54:33 -05001067 *
Tejun Heo324a56e2013-12-11 14:11:53 -05001068 * Look for the kernfs_node with @name and @ns under @parent and remove it.
1069 * Returns 0 on success, -ENOENT if such entry doesn't exist.
Tejun Heofd7b9f72013-11-28 14:54:33 -05001070 */
Tejun Heo324a56e2013-12-11 14:11:53 -05001071int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
Tejun Heofd7b9f72013-11-28 14:54:33 -05001072 const void *ns)
1073{
Tejun Heo324a56e2013-12-11 14:11:53 -05001074 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001075
Tejun Heo324a56e2013-12-11 14:11:53 -05001076 if (!parent) {
Tejun Heoc637b8a2013-12-11 14:11:58 -05001077 WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
Tejun Heofd7b9f72013-11-28 14:54:33 -05001078 name);
1079 return -ENOENT;
1080 }
1081
Tejun Heo988cd7a2014-02-03 14:02:58 -05001082 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001083
Tejun Heo324a56e2013-12-11 14:11:53 -05001084 kn = kernfs_find_ns(parent, name, ns);
1085 if (kn)
Tejun Heo988cd7a2014-02-03 14:02:58 -05001086 __kernfs_remove(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001087
Tejun Heo988cd7a2014-02-03 14:02:58 -05001088 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001089
Tejun Heo324a56e2013-12-11 14:11:53 -05001090 if (kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001091 return 0;
1092 else
1093 return -ENOENT;
1094}
1095
1096/**
1097 * kernfs_rename_ns - move and rename a kernfs_node
Tejun Heo324a56e2013-12-11 14:11:53 -05001098 * @kn: target node
Tejun Heofd7b9f72013-11-28 14:54:33 -05001099 * @new_parent: new parent to put @sd under
1100 * @new_name: new name
1101 * @new_ns: new namespace tag
1102 */
Tejun Heo324a56e2013-12-11 14:11:53 -05001103int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
Tejun Heofd7b9f72013-11-28 14:54:33 -05001104 const char *new_name, const void *new_ns)
1105{
1106 int error;
1107
Tejun Heoae343722014-01-10 08:57:21 -05001108 mutex_lock(&kernfs_mutex);
Tejun Heod0ae3d42013-12-11 16:02:56 -05001109
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -08001110 error = -ENOENT;
Tejun Heo81c173c2014-02-03 14:03:00 -05001111 if (!kernfs_active(kn) || !kernfs_active(new_parent))
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -08001112 goto out;
1113
Tejun Heofd7b9f72013-11-28 14:54:33 -05001114 error = 0;
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001115 if ((kn->parent == new_parent) && (kn->ns == new_ns) &&
1116 (strcmp(kn->name, new_name) == 0))
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -08001117 goto out; /* nothing to rename */
Tejun Heofd7b9f72013-11-28 14:54:33 -05001118
1119 error = -EEXIST;
1120 if (kernfs_find_ns(new_parent, new_name, new_ns))
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -08001121 goto out;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001122
Tejun Heo324a56e2013-12-11 14:11:53 -05001123 /* rename kernfs_node */
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001124 if (strcmp(kn->name, new_name) != 0) {
Tejun Heofd7b9f72013-11-28 14:54:33 -05001125 error = -ENOMEM;
1126 new_name = kstrdup(new_name, GFP_KERNEL);
1127 if (!new_name)
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -08001128 goto out;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001129
Tejun Heo47a52e92013-12-11 16:02:58 -05001130 if (kn->flags & KERNFS_STATIC_NAME)
1131 kn->flags &= ~KERNFS_STATIC_NAME;
1132 else
1133 kfree(kn->name);
1134
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001135 kn->name = new_name;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001136 }
1137
1138 /*
1139 * Move to the appropriate place in the appropriate directories rbtree.
1140 */
Tejun Heoc637b8a2013-12-11 14:11:58 -05001141 kernfs_unlink_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001142 kernfs_get(new_parent);
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001143 kernfs_put(kn->parent);
1144 kn->ns = new_ns;
Tejun Heoc637b8a2013-12-11 14:11:58 -05001145 kn->hash = kernfs_name_hash(kn->name, kn->ns);
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001146 kn->parent = new_parent;
Tejun Heoc637b8a2013-12-11 14:11:58 -05001147 kernfs_link_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001148
1149 error = 0;
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -08001150 out:
Tejun Heoa797bfc2013-12-11 14:11:57 -05001151 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001152 return error;
1153}
1154
Tejun Heofd7b9f72013-11-28 14:54:33 -05001155/* Relationship between s_mode and the DT_xxx types */
Tejun Heo324a56e2013-12-11 14:11:53 -05001156static inline unsigned char dt_type(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001157{
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001158 return (kn->mode >> 12) & 15;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001159}
1160
Tejun Heoc637b8a2013-12-11 14:11:58 -05001161static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001162{
1163 kernfs_put(filp->private_data);
1164 return 0;
1165}
1166
Tejun Heoc637b8a2013-12-11 14:11:58 -05001167static struct kernfs_node *kernfs_dir_pos(const void *ns,
Tejun Heo324a56e2013-12-11 14:11:53 -05001168 struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001169{
1170 if (pos) {
Tejun Heo81c173c2014-02-03 14:03:00 -05001171 int valid = kernfs_active(pos) &&
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -08001172 pos->parent == parent && hash == pos->hash;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001173 kernfs_put(pos);
1174 if (!valid)
1175 pos = NULL;
1176 }
1177 if (!pos && (hash > 1) && (hash < INT_MAX)) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001178 struct rb_node *node = parent->dir.children.rb_node;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001179 while (node) {
Tejun Heo324a56e2013-12-11 14:11:53 -05001180 pos = rb_to_kn(node);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001181
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001182 if (hash < pos->hash)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001183 node = node->rb_left;
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001184 else if (hash > pos->hash)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001185 node = node->rb_right;
1186 else
1187 break;
1188 }
1189 }
Tejun Heob9c9dad2014-02-03 14:09:11 -05001190 /* Skip over entries which are dying/dead or in the wrong namespace */
1191 while (pos && (!kernfs_active(pos) || pos->ns != ns)) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001192 struct rb_node *node = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001193 if (!node)
1194 pos = NULL;
1195 else
Tejun Heo324a56e2013-12-11 14:11:53 -05001196 pos = rb_to_kn(node);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001197 }
1198 return pos;
1199}
1200
Tejun Heoc637b8a2013-12-11 14:11:58 -05001201static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
Tejun Heo324a56e2013-12-11 14:11:53 -05001202 struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001203{
Tejun Heoc637b8a2013-12-11 14:11:58 -05001204 pos = kernfs_dir_pos(ns, parent, ino, pos);
Tejun Heob9c9dad2014-02-03 14:09:11 -05001205 if (pos) {
Tejun Heofd7b9f72013-11-28 14:54:33 -05001206 do {
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001207 struct rb_node *node = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001208 if (!node)
1209 pos = NULL;
1210 else
Tejun Heo324a56e2013-12-11 14:11:53 -05001211 pos = rb_to_kn(node);
Tejun Heob9c9dad2014-02-03 14:09:11 -05001212 } while (pos && (!kernfs_active(pos) || pos->ns != ns));
1213 }
Tejun Heofd7b9f72013-11-28 14:54:33 -05001214 return pos;
1215}
1216
Tejun Heoc637b8a2013-12-11 14:11:58 -05001217static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001218{
1219 struct dentry *dentry = file->f_path.dentry;
Tejun Heo324a56e2013-12-11 14:11:53 -05001220 struct kernfs_node *parent = dentry->d_fsdata;
1221 struct kernfs_node *pos = file->private_data;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001222 const void *ns = NULL;
1223
1224 if (!dir_emit_dots(file, ctx))
1225 return 0;
Tejun Heoa797bfc2013-12-11 14:11:57 -05001226 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001227
Tejun Heo324a56e2013-12-11 14:11:53 -05001228 if (kernfs_ns_enabled(parent))
Tejun Heoc525aad2013-12-11 14:11:55 -05001229 ns = kernfs_info(dentry->d_sb)->ns;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001230
Tejun Heoc637b8a2013-12-11 14:11:58 -05001231 for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001232 pos;
Tejun Heoc637b8a2013-12-11 14:11:58 -05001233 pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001234 const char *name = pos->name;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001235 unsigned int type = dt_type(pos);
1236 int len = strlen(name);
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001237 ino_t ino = pos->ino;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001238
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001239 ctx->pos = pos->hash;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001240 file->private_data = pos;
1241 kernfs_get(pos);
1242
Tejun Heoa797bfc2013-12-11 14:11:57 -05001243 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001244 if (!dir_emit(ctx, name, len, ino, type))
1245 return 0;
Tejun Heoa797bfc2013-12-11 14:11:57 -05001246 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001247 }
Tejun Heoa797bfc2013-12-11 14:11:57 -05001248 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001249 file->private_data = NULL;
1250 ctx->pos = INT_MAX;
1251 return 0;
1252}
1253
Tejun Heoc637b8a2013-12-11 14:11:58 -05001254static loff_t kernfs_dir_fop_llseek(struct file *file, loff_t offset,
1255 int whence)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001256{
1257 struct inode *inode = file_inode(file);
1258 loff_t ret;
1259
1260 mutex_lock(&inode->i_mutex);
1261 ret = generic_file_llseek(file, offset, whence);
1262 mutex_unlock(&inode->i_mutex);
1263
1264 return ret;
1265}
1266
Tejun Heoa797bfc2013-12-11 14:11:57 -05001267const struct file_operations kernfs_dir_fops = {
Tejun Heofd7b9f72013-11-28 14:54:33 -05001268 .read = generic_read_dir,
Tejun Heoc637b8a2013-12-11 14:11:58 -05001269 .iterate = kernfs_fop_readdir,
1270 .release = kernfs_dir_fop_release,
1271 .llseek = kernfs_dir_fop_llseek,
Tejun Heofd7b9f72013-11-28 14:54:33 -05001272};