blob: 3cff0a233cd14b4c6767aa9b4ffbae07f3be5adb [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 Heodb4aad22014-01-17 09:58:25 -0500353static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
354 const char *name, umode_t mode,
355 unsigned flags)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500356{
357 char *dup_name = NULL;
Tejun Heo324a56e2013-12-11 14:11:53 -0500358 struct kernfs_node *kn;
Tejun Heobc755552013-11-28 14:54:41 -0500359 int ret;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500360
Tejun Heo2063d602013-12-11 16:02:57 -0500361 if (!(flags & KERNFS_STATIC_NAME)) {
Tejun Heofd7b9f72013-11-28 14:54:33 -0500362 name = dup_name = kstrdup(name, GFP_KERNEL);
363 if (!name)
364 return NULL;
365 }
366
Tejun Heoa797bfc2013-12-11 14:11:57 -0500367 kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL);
Tejun Heo324a56e2013-12-11 14:11:53 -0500368 if (!kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500369 goto err_out1;
370
Tejun Heobc755552013-11-28 14:54:41 -0500371 ret = ida_simple_get(&root->ino_ida, 1, 0, GFP_KERNEL);
372 if (ret < 0)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500373 goto err_out2;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500374 kn->ino = ret;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500375
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500376 atomic_set(&kn->count, 1);
Tejun Heo81c173c2014-02-03 14:03:00 -0500377 atomic_set(&kn->active, KN_DEACTIVATED_BIAS);
Tejun Heo35beab02014-02-03 14:02:56 -0500378 RB_CLEAR_NODE(&kn->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500379
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500380 kn->name = name;
381 kn->mode = mode;
Tejun Heo81c173c2014-02-03 14:03:00 -0500382 kn->flags = flags;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500383
Tejun Heo324a56e2013-12-11 14:11:53 -0500384 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500385
386 err_out2:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500387 kmem_cache_free(kernfs_node_cache, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500388 err_out1:
389 kfree(dup_name);
390 return NULL;
391}
392
Tejun Heodb4aad22014-01-17 09:58:25 -0500393struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
394 const char *name, umode_t mode,
395 unsigned flags)
396{
397 struct kernfs_node *kn;
398
399 kn = __kernfs_new_node(kernfs_root(parent), name, mode, flags);
400 if (kn) {
401 kernfs_get(parent);
402 kn->parent = parent;
403 }
404 return kn;
405}
406
Tejun Heofd7b9f72013-11-28 14:54:33 -0500407/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500408 * kernfs_add_one - add kernfs_node to parent without warning
Tejun Heo324a56e2013-12-11 14:11:53 -0500409 * @kn: kernfs_node to be added
Tejun Heofd7b9f72013-11-28 14:54:33 -0500410 *
Tejun Heodb4aad22014-01-17 09:58:25 -0500411 * The caller must already have initialized @kn->parent. This
412 * function increments nlink of the parent's inode if @kn is a
413 * directory and link into the children list of the parent.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500414 *
Tejun Heofd7b9f72013-11-28 14:54:33 -0500415 * RETURNS:
416 * 0 on success, -EEXIST if entry with the given name already
417 * exists.
418 */
Tejun Heo988cd7a2014-02-03 14:02:58 -0500419int kernfs_add_one(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500420{
Tejun Heodb4aad22014-01-17 09:58:25 -0500421 struct kernfs_node *parent = kn->parent;
Tejun Heoc525aad2013-12-11 14:11:55 -0500422 struct kernfs_iattrs *ps_iattr;
Tejun Heo988cd7a2014-02-03 14:02:58 -0500423 bool has_ns;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500424 int ret;
425
Tejun Heo988cd7a2014-02-03 14:02:58 -0500426 mutex_lock(&kernfs_mutex);
427
428 ret = -EINVAL;
429 has_ns = kernfs_ns_enabled(parent);
430 if (WARN(has_ns != (bool)kn->ns, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
431 has_ns ? "required" : "invalid", parent->name, kn->name))
432 goto out_unlock;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500433
Tejun Heodf23fc32013-12-11 14:11:56 -0500434 if (kernfs_type(parent) != KERNFS_DIR)
Tejun Heo988cd7a2014-02-03 14:02:58 -0500435 goto out_unlock;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500436
Tejun Heo988cd7a2014-02-03 14:02:58 -0500437 ret = -ENOENT;
Tejun Heod35258e2014-02-03 14:09:12 -0500438 if ((parent->flags & KERNFS_ACTIVATED) && !kernfs_active(parent))
Tejun Heo988cd7a2014-02-03 14:02:58 -0500439 goto out_unlock;
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800440
Tejun Heoc637b8a2013-12-11 14:11:58 -0500441 kn->hash = kernfs_name_hash(kn->name, kn->ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500442
Tejun Heoc637b8a2013-12-11 14:11:58 -0500443 ret = kernfs_link_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500444 if (ret)
Tejun Heo988cd7a2014-02-03 14:02:58 -0500445 goto out_unlock;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500446
447 /* Update timestamps on the parent */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500448 ps_iattr = parent->iattr;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500449 if (ps_iattr) {
450 struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
451 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
452 }
453
Tejun Heod35258e2014-02-03 14:09:12 -0500454 mutex_unlock(&kernfs_mutex);
455
456 /*
457 * Activate the new node unless CREATE_DEACTIVATED is requested.
458 * If not activated here, the kernfs user is responsible for
459 * activating the node with kernfs_activate(). A node which hasn't
460 * been activated is not visible to userland and its removal won't
461 * trigger deactivation.
462 */
463 if (!(kernfs_root(kn)->flags & KERNFS_ROOT_CREATE_DEACTIVATED))
464 kernfs_activate(kn);
465 return 0;
466
Tejun Heo988cd7a2014-02-03 14:02:58 -0500467out_unlock:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500468 mutex_unlock(&kernfs_mutex);
Tejun Heo988cd7a2014-02-03 14:02:58 -0500469 return ret;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500470}
471
472/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500473 * kernfs_find_ns - find kernfs_node with the given name
474 * @parent: kernfs_node to search under
Tejun Heofd7b9f72013-11-28 14:54:33 -0500475 * @name: name to look for
476 * @ns: the namespace tag to use
477 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500478 * Look for kernfs_node with name @name under @parent. Returns pointer to
479 * the found kernfs_node on success, %NULL on failure.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500480 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500481static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
482 const unsigned char *name,
483 const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500484{
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500485 struct rb_node *node = parent->dir.children.rb_node;
Tejun Heoac9bba02013-11-29 17:19:09 -0500486 bool has_ns = kernfs_ns_enabled(parent);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500487 unsigned int hash;
488
Tejun Heoa797bfc2013-12-11 14:11:57 -0500489 lockdep_assert_held(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500490
491 if (has_ns != (bool)ns) {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500492 WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500493 has_ns ? "required" : "invalid", parent->name, name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500494 return NULL;
495 }
496
Tejun Heoc637b8a2013-12-11 14:11:58 -0500497 hash = kernfs_name_hash(name, ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500498 while (node) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500499 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500500 int result;
501
Tejun Heo324a56e2013-12-11 14:11:53 -0500502 kn = rb_to_kn(node);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500503 result = kernfs_name_compare(hash, name, ns, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500504 if (result < 0)
505 node = node->rb_left;
506 else if (result > 0)
507 node = node->rb_right;
508 else
Tejun Heo324a56e2013-12-11 14:11:53 -0500509 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500510 }
511 return NULL;
512}
513
514/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500515 * kernfs_find_and_get_ns - find and get kernfs_node with the given name
516 * @parent: kernfs_node to search under
Tejun Heofd7b9f72013-11-28 14:54:33 -0500517 * @name: name to look for
518 * @ns: the namespace tag to use
519 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500520 * Look for kernfs_node with name @name under @parent and get a reference
Tejun Heofd7b9f72013-11-28 14:54:33 -0500521 * if found. This function may sleep and returns pointer to the found
Tejun Heo324a56e2013-12-11 14:11:53 -0500522 * kernfs_node on success, %NULL on failure.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500523 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500524struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
525 const char *name, const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500526{
Tejun Heo324a56e2013-12-11 14:11:53 -0500527 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500528
Tejun Heoa797bfc2013-12-11 14:11:57 -0500529 mutex_lock(&kernfs_mutex);
Tejun Heo324a56e2013-12-11 14:11:53 -0500530 kn = kernfs_find_ns(parent, name, ns);
531 kernfs_get(kn);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500532 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500533
Tejun Heo324a56e2013-12-11 14:11:53 -0500534 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500535}
536EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
537
538/**
Tejun Heoba7443b2013-11-28 14:54:40 -0500539 * kernfs_create_root - create a new kernfs hierarchy
Tejun Heo90c07c82014-02-03 14:09:09 -0500540 * @scops: optional syscall operations for the hierarchy
Tejun Heod35258e2014-02-03 14:09:12 -0500541 * @flags: KERNFS_ROOT_* flags
Tejun Heoba7443b2013-11-28 14:54:40 -0500542 * @priv: opaque data associated with the new directory
543 *
544 * Returns the root of the new hierarchy on success, ERR_PTR() value on
545 * failure.
546 */
Tejun Heo90c07c82014-02-03 14:09:09 -0500547struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
Tejun Heod35258e2014-02-03 14:09:12 -0500548 unsigned int flags, void *priv)
Tejun Heoba7443b2013-11-28 14:54:40 -0500549{
550 struct kernfs_root *root;
Tejun Heo324a56e2013-12-11 14:11:53 -0500551 struct kernfs_node *kn;
Tejun Heoba7443b2013-11-28 14:54:40 -0500552
553 root = kzalloc(sizeof(*root), GFP_KERNEL);
554 if (!root)
555 return ERR_PTR(-ENOMEM);
556
Tejun Heobc755552013-11-28 14:54:41 -0500557 ida_init(&root->ino_ida);
558
Tejun Heodb4aad22014-01-17 09:58:25 -0500559 kn = __kernfs_new_node(root, "", S_IFDIR | S_IRUGO | S_IXUGO,
560 KERNFS_DIR);
Tejun Heo324a56e2013-12-11 14:11:53 -0500561 if (!kn) {
Tejun Heobc755552013-11-28 14:54:41 -0500562 ida_destroy(&root->ino_ida);
Tejun Heoba7443b2013-11-28 14:54:40 -0500563 kfree(root);
564 return ERR_PTR(-ENOMEM);
565 }
566
Tejun Heo324a56e2013-12-11 14:11:53 -0500567 kn->priv = priv;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500568 kn->dir.root = root;
Tejun Heoba7443b2013-11-28 14:54:40 -0500569
Tejun Heo90c07c82014-02-03 14:09:09 -0500570 root->syscall_ops = scops;
Tejun Heod35258e2014-02-03 14:09:12 -0500571 root->flags = flags;
Tejun Heo324a56e2013-12-11 14:11:53 -0500572 root->kn = kn;
Tejun Heoabd54f02014-02-03 14:02:55 -0500573 init_waitqueue_head(&root->deactivate_waitq);
Tejun Heoba7443b2013-11-28 14:54:40 -0500574
Tejun Heod35258e2014-02-03 14:09:12 -0500575 if (!(root->flags & KERNFS_ROOT_CREATE_DEACTIVATED))
576 kernfs_activate(kn);
577
Tejun Heoba7443b2013-11-28 14:54:40 -0500578 return root;
579}
580
581/**
582 * kernfs_destroy_root - destroy a kernfs hierarchy
583 * @root: root of the hierarchy to destroy
584 *
585 * Destroy the hierarchy anchored at @root by removing all existing
586 * directories and destroying @root.
587 */
588void kernfs_destroy_root(struct kernfs_root *root)
589{
Tejun Heo324a56e2013-12-11 14:11:53 -0500590 kernfs_remove(root->kn); /* will also free @root */
Tejun Heoba7443b2013-11-28 14:54:40 -0500591}
592
593/**
Tejun Heofd7b9f72013-11-28 14:54:33 -0500594 * kernfs_create_dir_ns - create a directory
595 * @parent: parent in which to create a new directory
596 * @name: name of the new directory
Tejun Heobb8b9d02013-12-11 16:02:55 -0500597 * @mode: mode of the new directory
Tejun Heofd7b9f72013-11-28 14:54:33 -0500598 * @priv: opaque data associated with the new directory
599 * @ns: optional namespace tag of the directory
600 *
601 * Returns the created node on success, ERR_PTR() value on failure.
602 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500603struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
Tejun Heobb8b9d02013-12-11 16:02:55 -0500604 const char *name, umode_t mode,
605 void *priv, const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500606{
Tejun Heo324a56e2013-12-11 14:11:53 -0500607 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500608 int rc;
609
610 /* allocate */
Tejun Heodb4aad22014-01-17 09:58:25 -0500611 kn = kernfs_new_node(parent, name, mode | S_IFDIR, KERNFS_DIR);
Tejun Heo324a56e2013-12-11 14:11:53 -0500612 if (!kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500613 return ERR_PTR(-ENOMEM);
614
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500615 kn->dir.root = parent->dir.root;
616 kn->ns = ns;
Tejun Heo324a56e2013-12-11 14:11:53 -0500617 kn->priv = priv;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500618
619 /* link in */
Tejun Heo988cd7a2014-02-03 14:02:58 -0500620 rc = kernfs_add_one(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500621 if (!rc)
Tejun Heo324a56e2013-12-11 14:11:53 -0500622 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500623
Tejun Heo324a56e2013-12-11 14:11:53 -0500624 kernfs_put(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500625 return ERR_PTR(rc);
626}
627
Tejun Heoc637b8a2013-12-11 14:11:58 -0500628static struct dentry *kernfs_iop_lookup(struct inode *dir,
629 struct dentry *dentry,
630 unsigned int flags)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500631{
Tejun Heo19bbb922013-12-11 16:02:59 -0500632 struct dentry *ret;
Tejun Heo324a56e2013-12-11 14:11:53 -0500633 struct kernfs_node *parent = dentry->d_parent->d_fsdata;
634 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500635 struct inode *inode;
636 const void *ns = NULL;
637
Tejun Heoa797bfc2013-12-11 14:11:57 -0500638 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500639
Tejun Heo324a56e2013-12-11 14:11:53 -0500640 if (kernfs_ns_enabled(parent))
Tejun Heoc525aad2013-12-11 14:11:55 -0500641 ns = kernfs_info(dir->i_sb)->ns;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500642
Tejun Heo324a56e2013-12-11 14:11:53 -0500643 kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500644
645 /* no such entry */
Tejun Heob9c9dad2014-02-03 14:09:11 -0500646 if (!kn || !kernfs_active(kn)) {
Tejun Heo19bbb922013-12-11 16:02:59 -0500647 ret = NULL;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500648 goto out_unlock;
649 }
Tejun Heo324a56e2013-12-11 14:11:53 -0500650 kernfs_get(kn);
651 dentry->d_fsdata = kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500652
653 /* attach dentry and inode */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500654 inode = kernfs_get_inode(dir->i_sb, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500655 if (!inode) {
656 ret = ERR_PTR(-ENOMEM);
657 goto out_unlock;
658 }
659
660 /* instantiate and hash dentry */
661 ret = d_materialise_unique(dentry, inode);
662 out_unlock:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500663 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500664 return ret;
665}
666
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500667static int kernfs_iop_mkdir(struct inode *dir, struct dentry *dentry,
668 umode_t mode)
669{
670 struct kernfs_node *parent = dir->i_private;
Tejun Heo90c07c82014-02-03 14:09:09 -0500671 struct kernfs_syscall_ops *scops = kernfs_root(parent)->syscall_ops;
Tejun Heo07c75302014-02-03 14:09:08 -0500672 int ret;
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500673
Tejun Heo90c07c82014-02-03 14:09:09 -0500674 if (!scops || !scops->mkdir)
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500675 return -EPERM;
676
Tejun Heo07c75302014-02-03 14:09:08 -0500677 if (!kernfs_get_active(parent))
678 return -ENODEV;
679
Tejun Heo90c07c82014-02-03 14:09:09 -0500680 ret = scops->mkdir(parent, dentry->d_name.name, mode);
Tejun Heo07c75302014-02-03 14:09:08 -0500681
682 kernfs_put_active(parent);
683 return ret;
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500684}
685
686static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
687{
688 struct kernfs_node *kn = dentry->d_fsdata;
Tejun Heo90c07c82014-02-03 14:09:09 -0500689 struct kernfs_syscall_ops *scops = kernfs_root(kn)->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->rmdir)
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500693 return -EPERM;
694
Tejun Heo07c75302014-02-03 14:09:08 -0500695 if (!kernfs_get_active(kn))
696 return -ENODEV;
697
Tejun Heo90c07c82014-02-03 14:09:09 -0500698 ret = scops->rmdir(kn);
Tejun Heo07c75302014-02-03 14:09:08 -0500699
700 kernfs_put_active(kn);
701 return ret;
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500702}
703
704static int kernfs_iop_rename(struct inode *old_dir, struct dentry *old_dentry,
705 struct inode *new_dir, struct dentry *new_dentry)
706{
707 struct kernfs_node *kn = old_dentry->d_fsdata;
708 struct kernfs_node *new_parent = new_dir->i_private;
Tejun Heo90c07c82014-02-03 14:09:09 -0500709 struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops;
Tejun Heo07c75302014-02-03 14:09:08 -0500710 int ret;
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500711
Tejun Heo90c07c82014-02-03 14:09:09 -0500712 if (!scops || !scops->rename)
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500713 return -EPERM;
714
Tejun Heo07c75302014-02-03 14:09:08 -0500715 if (!kernfs_get_active(kn))
716 return -ENODEV;
717
718 if (!kernfs_get_active(new_parent)) {
719 kernfs_put_active(kn);
720 return -ENODEV;
721 }
722
Tejun Heo90c07c82014-02-03 14:09:09 -0500723 ret = scops->rename(kn, new_parent, new_dentry->d_name.name);
Tejun Heo07c75302014-02-03 14:09:08 -0500724
725 kernfs_put_active(new_parent);
726 kernfs_put_active(kn);
727 return ret;
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500728}
729
Tejun Heoa797bfc2013-12-11 14:11:57 -0500730const struct inode_operations kernfs_dir_iops = {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500731 .lookup = kernfs_iop_lookup,
732 .permission = kernfs_iop_permission,
733 .setattr = kernfs_iop_setattr,
734 .getattr = kernfs_iop_getattr,
735 .setxattr = kernfs_iop_setxattr,
736 .removexattr = kernfs_iop_removexattr,
737 .getxattr = kernfs_iop_getxattr,
738 .listxattr = kernfs_iop_listxattr,
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500739
740 .mkdir = kernfs_iop_mkdir,
741 .rmdir = kernfs_iop_rmdir,
742 .rename = kernfs_iop_rename,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500743};
744
Tejun Heoc637b8a2013-12-11 14:11:58 -0500745static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500746{
Tejun Heo324a56e2013-12-11 14:11:53 -0500747 struct kernfs_node *last;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500748
749 while (true) {
750 struct rb_node *rbn;
751
752 last = pos;
753
Tejun Heodf23fc32013-12-11 14:11:56 -0500754 if (kernfs_type(pos) != KERNFS_DIR)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500755 break;
756
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500757 rbn = rb_first(&pos->dir.children);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500758 if (!rbn)
759 break;
760
Tejun Heo324a56e2013-12-11 14:11:53 -0500761 pos = rb_to_kn(rbn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500762 }
763
764 return last;
765}
766
767/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500768 * kernfs_next_descendant_post - find the next descendant for post-order walk
Tejun Heofd7b9f72013-11-28 14:54:33 -0500769 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo324a56e2013-12-11 14:11:53 -0500770 * @root: kernfs_node whose descendants to walk
Tejun Heofd7b9f72013-11-28 14:54:33 -0500771 *
772 * Find the next descendant to visit for post-order traversal of @root's
773 * descendants. @root is included in the iteration and the last node to be
774 * visited.
775 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500776static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
777 struct kernfs_node *root)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500778{
779 struct rb_node *rbn;
780
Tejun Heoa797bfc2013-12-11 14:11:57 -0500781 lockdep_assert_held(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500782
783 /* if first iteration, visit leftmost descendant which may be root */
784 if (!pos)
Tejun Heoc637b8a2013-12-11 14:11:58 -0500785 return kernfs_leftmost_descendant(root);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500786
787 /* if we visited @root, we're done */
788 if (pos == root)
789 return NULL;
790
791 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500792 rbn = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500793 if (rbn)
Tejun Heoc637b8a2013-12-11 14:11:58 -0500794 return kernfs_leftmost_descendant(rb_to_kn(rbn));
Tejun Heofd7b9f72013-11-28 14:54:33 -0500795
796 /* no sibling left, visit parent */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500797 return pos->parent;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500798}
799
Tejun Heod35258e2014-02-03 14:09:12 -0500800/**
801 * kernfs_activate - activate a node which started deactivated
802 * @kn: kernfs_node whose subtree is to be activated
803 *
804 * If the root has KERNFS_ROOT_CREATE_DEACTIVATED set, a newly created node
805 * needs to be explicitly activated. A node which hasn't been activated
806 * isn't visible to userland and deactivation is skipped during its
807 * removal. This is useful to construct atomic init sequences where
808 * creation of multiple nodes should either succeed or fail atomically.
809 *
810 * The caller is responsible for ensuring that this function is not called
811 * after kernfs_remove*() is invoked on @kn.
812 */
813void kernfs_activate(struct kernfs_node *kn)
814{
815 struct kernfs_node *pos;
816
817 mutex_lock(&kernfs_mutex);
818
819 pos = NULL;
820 while ((pos = kernfs_next_descendant_post(pos, kn))) {
821 if (!pos || (pos->flags & KERNFS_ACTIVATED))
822 continue;
823
824 WARN_ON_ONCE(pos->parent && RB_EMPTY_NODE(&pos->rb));
825 WARN_ON_ONCE(atomic_read(&pos->active) != KN_DEACTIVATED_BIAS);
826
827 atomic_sub(KN_DEACTIVATED_BIAS, &pos->active);
828 pos->flags |= KERNFS_ACTIVATED;
829 }
830
831 mutex_unlock(&kernfs_mutex);
832}
833
Tejun Heo988cd7a2014-02-03 14:02:58 -0500834static void __kernfs_remove(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500835{
Tejun Heo35beab02014-02-03 14:02:56 -0500836 struct kernfs_node *pos;
837
838 lockdep_assert_held(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500839
Tejun Heo6b0afc22014-02-03 14:03:01 -0500840 /*
841 * Short-circuit if non-root @kn has already finished removal.
842 * This is for kernfs_remove_self() which plays with active ref
843 * after removal.
844 */
845 if (!kn || (kn->parent && RB_EMPTY_NODE(&kn->rb)))
Greg Kroah-Hartmance9b4992014-01-13 13:50:31 -0800846 return;
847
Tejun Heoc637b8a2013-12-11 14:11:58 -0500848 pr_debug("kernfs %s: removing\n", kn->name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500849
Tejun Heo81c173c2014-02-03 14:03:00 -0500850 /* prevent any new usage under @kn by deactivating all nodes */
Tejun Heo35beab02014-02-03 14:02:56 -0500851 pos = NULL;
852 while ((pos = kernfs_next_descendant_post(pos, kn)))
Tejun Heo81c173c2014-02-03 14:03:00 -0500853 if (kernfs_active(pos))
854 atomic_add(KN_DEACTIVATED_BIAS, &pos->active);
Tejun Heo35beab02014-02-03 14:02:56 -0500855
856 /* deactivate and unlink the subtree node-by-node */
Tejun Heofd7b9f72013-11-28 14:54:33 -0500857 do {
Tejun Heo35beab02014-02-03 14:02:56 -0500858 pos = kernfs_leftmost_descendant(kn);
859
860 /*
Tejun Heo81c173c2014-02-03 14:03:00 -0500861 * kernfs_drain() drops kernfs_mutex temporarily and @pos's
862 * base ref could have been put by someone else by the time
863 * the function returns. Make sure it doesn't go away
864 * underneath us.
Tejun Heo35beab02014-02-03 14:02:56 -0500865 */
866 kernfs_get(pos);
867
Tejun Heod35258e2014-02-03 14:09:12 -0500868 /*
869 * Drain iff @kn was activated. This avoids draining and
870 * its lockdep annotations for nodes which have never been
871 * activated and allows embedding kernfs_remove() in create
872 * error paths without worrying about draining.
873 */
874 if (kn->flags & KERNFS_ACTIVATED)
875 kernfs_drain(pos);
876 else
877 WARN_ON_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS);
Tejun Heo35beab02014-02-03 14:02:56 -0500878
879 /*
880 * kernfs_unlink_sibling() succeeds once per node. Use it
881 * to decide who's responsible for cleanups.
882 */
883 if (!pos->parent || kernfs_unlink_sibling(pos)) {
884 struct kernfs_iattrs *ps_iattr =
885 pos->parent ? pos->parent->iattr : NULL;
886
887 /* update timestamps on the parent */
888 if (ps_iattr) {
889 ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME;
890 ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME;
891 }
892
Tejun Heo988cd7a2014-02-03 14:02:58 -0500893 kernfs_put(pos);
Tejun Heo35beab02014-02-03 14:02:56 -0500894 }
895
896 kernfs_put(pos);
897 } while (pos != kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500898}
899
900/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500901 * kernfs_remove - remove a kernfs_node recursively
902 * @kn: the kernfs_node to remove
Tejun Heofd7b9f72013-11-28 14:54:33 -0500903 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500904 * Remove @kn along with all its subdirectories and files.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500905 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500906void kernfs_remove(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500907{
Tejun Heo988cd7a2014-02-03 14:02:58 -0500908 mutex_lock(&kernfs_mutex);
909 __kernfs_remove(kn);
910 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500911}
912
913/**
Tejun Heo6b0afc22014-02-03 14:03:01 -0500914 * kernfs_break_active_protection - break out of active protection
915 * @kn: the self kernfs_node
916 *
917 * The caller must be running off of a kernfs operation which is invoked
918 * with an active reference - e.g. one of kernfs_ops. Each invocation of
919 * this function must also be matched with an invocation of
920 * kernfs_unbreak_active_protection().
921 *
922 * This function releases the active reference of @kn the caller is
923 * holding. Once this function is called, @kn may be removed at any point
924 * and the caller is solely responsible for ensuring that the objects it
925 * dereferences are accessible.
926 */
927void kernfs_break_active_protection(struct kernfs_node *kn)
928{
929 /*
930 * Take out ourself out of the active ref dependency chain. If
931 * we're called without an active ref, lockdep will complain.
932 */
933 kernfs_put_active(kn);
934}
935
936/**
937 * kernfs_unbreak_active_protection - undo kernfs_break_active_protection()
938 * @kn: the self kernfs_node
939 *
940 * If kernfs_break_active_protection() was called, this function must be
941 * invoked before finishing the kernfs operation. Note that while this
942 * function restores the active reference, it doesn't and can't actually
943 * restore the active protection - @kn may already or be in the process of
944 * being removed. Once kernfs_break_active_protection() is invoked, that
945 * protection is irreversibly gone for the kernfs operation instance.
946 *
947 * While this function may be called at any point after
948 * kernfs_break_active_protection() is invoked, its most useful location
949 * would be right before the enclosing kernfs operation returns.
950 */
951void kernfs_unbreak_active_protection(struct kernfs_node *kn)
952{
953 /*
954 * @kn->active could be in any state; however, the increment we do
955 * here will be undone as soon as the enclosing kernfs operation
956 * finishes and this temporary bump can't break anything. If @kn
957 * is alive, nothing changes. If @kn is being deactivated, the
958 * soon-to-follow put will either finish deactivation or restore
959 * deactivated state. If @kn is already removed, the temporary
960 * bump is guaranteed to be gone before @kn is released.
961 */
962 atomic_inc(&kn->active);
963 if (kernfs_lockdep(kn))
964 rwsem_acquire(&kn->dep_map, 0, 1, _RET_IP_);
965}
966
967/**
968 * kernfs_remove_self - remove a kernfs_node from its own method
969 * @kn: the self kernfs_node to remove
970 *
971 * The caller must be running off of a kernfs operation which is invoked
972 * with an active reference - e.g. one of kernfs_ops. This can be used to
973 * implement a file operation which deletes itself.
974 *
975 * For example, the "delete" file for a sysfs device directory can be
976 * implemented by invoking kernfs_remove_self() on the "delete" file
977 * itself. This function breaks the circular dependency of trying to
978 * deactivate self while holding an active ref itself. It isn't necessary
979 * to modify the usual removal path to use kernfs_remove_self(). The
980 * "delete" implementation can simply invoke kernfs_remove_self() on self
981 * before proceeding with the usual removal path. kernfs will ignore later
982 * kernfs_remove() on self.
983 *
984 * kernfs_remove_self() can be called multiple times concurrently on the
985 * same kernfs_node. Only the first one actually performs removal and
986 * returns %true. All others will wait until the kernfs operation which
987 * won self-removal finishes and return %false. Note that the losers wait
988 * for the completion of not only the winning kernfs_remove_self() but also
989 * the whole kernfs_ops which won the arbitration. This can be used to
990 * guarantee, for example, all concurrent writes to a "delete" file to
991 * finish only after the whole operation is complete.
992 */
993bool kernfs_remove_self(struct kernfs_node *kn)
994{
995 bool ret;
996
997 mutex_lock(&kernfs_mutex);
998 kernfs_break_active_protection(kn);
999
1000 /*
1001 * SUICIDAL is used to arbitrate among competing invocations. Only
1002 * the first one will actually perform removal. When the removal
1003 * is complete, SUICIDED is set and the active ref is restored
1004 * while holding kernfs_mutex. The ones which lost arbitration
1005 * waits for SUICDED && drained which can happen only after the
1006 * enclosing kernfs operation which executed the winning instance
1007 * of kernfs_remove_self() finished.
1008 */
1009 if (!(kn->flags & KERNFS_SUICIDAL)) {
1010 kn->flags |= KERNFS_SUICIDAL;
1011 __kernfs_remove(kn);
1012 kn->flags |= KERNFS_SUICIDED;
1013 ret = true;
1014 } else {
1015 wait_queue_head_t *waitq = &kernfs_root(kn)->deactivate_waitq;
1016 DEFINE_WAIT(wait);
1017
1018 while (true) {
1019 prepare_to_wait(waitq, &wait, TASK_UNINTERRUPTIBLE);
1020
1021 if ((kn->flags & KERNFS_SUICIDED) &&
1022 atomic_read(&kn->active) == KN_DEACTIVATED_BIAS)
1023 break;
1024
1025 mutex_unlock(&kernfs_mutex);
1026 schedule();
1027 mutex_lock(&kernfs_mutex);
1028 }
1029 finish_wait(waitq, &wait);
1030 WARN_ON_ONCE(!RB_EMPTY_NODE(&kn->rb));
1031 ret = false;
1032 }
1033
1034 /*
1035 * This must be done while holding kernfs_mutex; otherwise, waiting
1036 * for SUICIDED && deactivated could finish prematurely.
1037 */
1038 kernfs_unbreak_active_protection(kn);
1039
1040 mutex_unlock(&kernfs_mutex);
1041 return ret;
1042}
1043
1044/**
Tejun Heo324a56e2013-12-11 14:11:53 -05001045 * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
1046 * @parent: parent of the target
1047 * @name: name of the kernfs_node to remove
1048 * @ns: namespace tag of the kernfs_node to remove
Tejun Heofd7b9f72013-11-28 14:54:33 -05001049 *
Tejun Heo324a56e2013-12-11 14:11:53 -05001050 * Look for the kernfs_node with @name and @ns under @parent and remove it.
1051 * Returns 0 on success, -ENOENT if such entry doesn't exist.
Tejun Heofd7b9f72013-11-28 14:54:33 -05001052 */
Tejun Heo324a56e2013-12-11 14:11:53 -05001053int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
Tejun Heofd7b9f72013-11-28 14:54:33 -05001054 const void *ns)
1055{
Tejun Heo324a56e2013-12-11 14:11:53 -05001056 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001057
Tejun Heo324a56e2013-12-11 14:11:53 -05001058 if (!parent) {
Tejun Heoc637b8a2013-12-11 14:11:58 -05001059 WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
Tejun Heofd7b9f72013-11-28 14:54:33 -05001060 name);
1061 return -ENOENT;
1062 }
1063
Tejun Heo988cd7a2014-02-03 14:02:58 -05001064 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001065
Tejun Heo324a56e2013-12-11 14:11:53 -05001066 kn = kernfs_find_ns(parent, name, ns);
1067 if (kn)
Tejun Heo988cd7a2014-02-03 14:02:58 -05001068 __kernfs_remove(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001069
Tejun Heo988cd7a2014-02-03 14:02:58 -05001070 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001071
Tejun Heo324a56e2013-12-11 14:11:53 -05001072 if (kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001073 return 0;
1074 else
1075 return -ENOENT;
1076}
1077
1078/**
1079 * kernfs_rename_ns - move and rename a kernfs_node
Tejun Heo324a56e2013-12-11 14:11:53 -05001080 * @kn: target node
Tejun Heofd7b9f72013-11-28 14:54:33 -05001081 * @new_parent: new parent to put @sd under
1082 * @new_name: new name
1083 * @new_ns: new namespace tag
1084 */
Tejun Heo324a56e2013-12-11 14:11:53 -05001085int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
Tejun Heofd7b9f72013-11-28 14:54:33 -05001086 const char *new_name, const void *new_ns)
1087{
1088 int error;
1089
Tejun Heoae343722014-01-10 08:57:21 -05001090 mutex_lock(&kernfs_mutex);
Tejun Heod0ae3d42013-12-11 16:02:56 -05001091
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -08001092 error = -ENOENT;
Tejun Heo81c173c2014-02-03 14:03:00 -05001093 if (!kernfs_active(kn) || !kernfs_active(new_parent))
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -08001094 goto out;
1095
Tejun Heofd7b9f72013-11-28 14:54:33 -05001096 error = 0;
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001097 if ((kn->parent == new_parent) && (kn->ns == new_ns) &&
1098 (strcmp(kn->name, new_name) == 0))
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -08001099 goto out; /* nothing to rename */
Tejun Heofd7b9f72013-11-28 14:54:33 -05001100
1101 error = -EEXIST;
1102 if (kernfs_find_ns(new_parent, new_name, new_ns))
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -08001103 goto out;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001104
Tejun Heo324a56e2013-12-11 14:11:53 -05001105 /* rename kernfs_node */
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001106 if (strcmp(kn->name, new_name) != 0) {
Tejun Heofd7b9f72013-11-28 14:54:33 -05001107 error = -ENOMEM;
1108 new_name = kstrdup(new_name, GFP_KERNEL);
1109 if (!new_name)
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -08001110 goto out;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001111
Tejun Heo47a52e92013-12-11 16:02:58 -05001112 if (kn->flags & KERNFS_STATIC_NAME)
1113 kn->flags &= ~KERNFS_STATIC_NAME;
1114 else
1115 kfree(kn->name);
1116
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001117 kn->name = new_name;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001118 }
1119
1120 /*
1121 * Move to the appropriate place in the appropriate directories rbtree.
1122 */
Tejun Heoc637b8a2013-12-11 14:11:58 -05001123 kernfs_unlink_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001124 kernfs_get(new_parent);
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001125 kernfs_put(kn->parent);
1126 kn->ns = new_ns;
Tejun Heoc637b8a2013-12-11 14:11:58 -05001127 kn->hash = kernfs_name_hash(kn->name, kn->ns);
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001128 kn->parent = new_parent;
Tejun Heoc637b8a2013-12-11 14:11:58 -05001129 kernfs_link_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001130
1131 error = 0;
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -08001132 out:
Tejun Heoa797bfc2013-12-11 14:11:57 -05001133 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001134 return error;
1135}
1136
Tejun Heofd7b9f72013-11-28 14:54:33 -05001137/* Relationship between s_mode and the DT_xxx types */
Tejun Heo324a56e2013-12-11 14:11:53 -05001138static inline unsigned char dt_type(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001139{
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001140 return (kn->mode >> 12) & 15;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001141}
1142
Tejun Heoc637b8a2013-12-11 14:11:58 -05001143static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001144{
1145 kernfs_put(filp->private_data);
1146 return 0;
1147}
1148
Tejun Heoc637b8a2013-12-11 14:11:58 -05001149static struct kernfs_node *kernfs_dir_pos(const void *ns,
Tejun Heo324a56e2013-12-11 14:11:53 -05001150 struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001151{
1152 if (pos) {
Tejun Heo81c173c2014-02-03 14:03:00 -05001153 int valid = kernfs_active(pos) &&
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -08001154 pos->parent == parent && hash == pos->hash;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001155 kernfs_put(pos);
1156 if (!valid)
1157 pos = NULL;
1158 }
1159 if (!pos && (hash > 1) && (hash < INT_MAX)) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001160 struct rb_node *node = parent->dir.children.rb_node;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001161 while (node) {
Tejun Heo324a56e2013-12-11 14:11:53 -05001162 pos = rb_to_kn(node);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001163
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001164 if (hash < pos->hash)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001165 node = node->rb_left;
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001166 else if (hash > pos->hash)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001167 node = node->rb_right;
1168 else
1169 break;
1170 }
1171 }
Tejun Heob9c9dad2014-02-03 14:09:11 -05001172 /* Skip over entries which are dying/dead or in the wrong namespace */
1173 while (pos && (!kernfs_active(pos) || pos->ns != ns)) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001174 struct rb_node *node = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001175 if (!node)
1176 pos = NULL;
1177 else
Tejun Heo324a56e2013-12-11 14:11:53 -05001178 pos = rb_to_kn(node);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001179 }
1180 return pos;
1181}
1182
Tejun Heoc637b8a2013-12-11 14:11:58 -05001183static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
Tejun Heo324a56e2013-12-11 14:11:53 -05001184 struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001185{
Tejun Heoc637b8a2013-12-11 14:11:58 -05001186 pos = kernfs_dir_pos(ns, parent, ino, pos);
Tejun Heob9c9dad2014-02-03 14:09:11 -05001187 if (pos) {
Tejun Heofd7b9f72013-11-28 14:54:33 -05001188 do {
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001189 struct rb_node *node = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001190 if (!node)
1191 pos = NULL;
1192 else
Tejun Heo324a56e2013-12-11 14:11:53 -05001193 pos = rb_to_kn(node);
Tejun Heob9c9dad2014-02-03 14:09:11 -05001194 } while (pos && (!kernfs_active(pos) || pos->ns != ns));
1195 }
Tejun Heofd7b9f72013-11-28 14:54:33 -05001196 return pos;
1197}
1198
Tejun Heoc637b8a2013-12-11 14:11:58 -05001199static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001200{
1201 struct dentry *dentry = file->f_path.dentry;
Tejun Heo324a56e2013-12-11 14:11:53 -05001202 struct kernfs_node *parent = dentry->d_fsdata;
1203 struct kernfs_node *pos = file->private_data;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001204 const void *ns = NULL;
1205
1206 if (!dir_emit_dots(file, ctx))
1207 return 0;
Tejun Heoa797bfc2013-12-11 14:11:57 -05001208 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001209
Tejun Heo324a56e2013-12-11 14:11:53 -05001210 if (kernfs_ns_enabled(parent))
Tejun Heoc525aad2013-12-11 14:11:55 -05001211 ns = kernfs_info(dentry->d_sb)->ns;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001212
Tejun Heoc637b8a2013-12-11 14:11:58 -05001213 for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001214 pos;
Tejun Heoc637b8a2013-12-11 14:11:58 -05001215 pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001216 const char *name = pos->name;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001217 unsigned int type = dt_type(pos);
1218 int len = strlen(name);
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001219 ino_t ino = pos->ino;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001220
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001221 ctx->pos = pos->hash;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001222 file->private_data = pos;
1223 kernfs_get(pos);
1224
Tejun Heoa797bfc2013-12-11 14:11:57 -05001225 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001226 if (!dir_emit(ctx, name, len, ino, type))
1227 return 0;
Tejun Heoa797bfc2013-12-11 14:11:57 -05001228 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001229 }
Tejun Heoa797bfc2013-12-11 14:11:57 -05001230 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001231 file->private_data = NULL;
1232 ctx->pos = INT_MAX;
1233 return 0;
1234}
1235
Tejun Heoc637b8a2013-12-11 14:11:58 -05001236static loff_t kernfs_dir_fop_llseek(struct file *file, loff_t offset,
1237 int whence)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001238{
1239 struct inode *inode = file_inode(file);
1240 loff_t ret;
1241
1242 mutex_lock(&inode->i_mutex);
1243 ret = generic_file_llseek(file, offset, whence);
1244 mutex_unlock(&inode->i_mutex);
1245
1246 return ret;
1247}
1248
Tejun Heoa797bfc2013-12-11 14:11:57 -05001249const struct file_operations kernfs_dir_fops = {
Tejun Heofd7b9f72013-11-28 14:54:33 -05001250 .read = generic_read_dir,
Tejun Heoc637b8a2013-12-11 14:11:58 -05001251 .iterate = kernfs_fop_readdir,
1252 .release = kernfs_dir_fop_release,
1253 .llseek = kernfs_dir_fop_llseek,
Tejun Heofd7b9f72013-11-28 14:54:33 -05001254};