blob: 8c63ae1bccb6daae85cbb7317c518e059f238d3f [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 Heo81c173c2014-02-03 14:03:00 -0500438 if (!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
454 /* Mark the entry added into directory tree */
Tejun Heo81c173c2014-02-03 14:03:00 -0500455 atomic_sub(KN_DEACTIVATED_BIAS, &kn->active);
Tejun Heo988cd7a2014-02-03 14:02:58 -0500456 ret = 0;
457out_unlock:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500458 mutex_unlock(&kernfs_mutex);
Tejun Heo988cd7a2014-02-03 14:02:58 -0500459 return ret;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500460}
461
462/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500463 * kernfs_find_ns - find kernfs_node with the given name
464 * @parent: kernfs_node to search under
Tejun Heofd7b9f72013-11-28 14:54:33 -0500465 * @name: name to look for
466 * @ns: the namespace tag to use
467 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500468 * Look for kernfs_node with name @name under @parent. Returns pointer to
469 * the found kernfs_node on success, %NULL on failure.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500470 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500471static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
472 const unsigned char *name,
473 const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500474{
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500475 struct rb_node *node = parent->dir.children.rb_node;
Tejun Heoac9bba02013-11-29 17:19:09 -0500476 bool has_ns = kernfs_ns_enabled(parent);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500477 unsigned int hash;
478
Tejun Heoa797bfc2013-12-11 14:11:57 -0500479 lockdep_assert_held(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500480
481 if (has_ns != (bool)ns) {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500482 WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500483 has_ns ? "required" : "invalid", parent->name, name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500484 return NULL;
485 }
486
Tejun Heoc637b8a2013-12-11 14:11:58 -0500487 hash = kernfs_name_hash(name, ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500488 while (node) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500489 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500490 int result;
491
Tejun Heo324a56e2013-12-11 14:11:53 -0500492 kn = rb_to_kn(node);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500493 result = kernfs_name_compare(hash, name, ns, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500494 if (result < 0)
495 node = node->rb_left;
496 else if (result > 0)
497 node = node->rb_right;
498 else
Tejun Heo324a56e2013-12-11 14:11:53 -0500499 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500500 }
501 return NULL;
502}
503
504/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500505 * kernfs_find_and_get_ns - find and get kernfs_node with the given name
506 * @parent: kernfs_node to search under
Tejun Heofd7b9f72013-11-28 14:54:33 -0500507 * @name: name to look for
508 * @ns: the namespace tag to use
509 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500510 * Look for kernfs_node with name @name under @parent and get a reference
Tejun Heofd7b9f72013-11-28 14:54:33 -0500511 * if found. This function may sleep and returns pointer to the found
Tejun Heo324a56e2013-12-11 14:11:53 -0500512 * kernfs_node on success, %NULL on failure.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500513 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500514struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
515 const char *name, const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500516{
Tejun Heo324a56e2013-12-11 14:11:53 -0500517 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500518
Tejun Heoa797bfc2013-12-11 14:11:57 -0500519 mutex_lock(&kernfs_mutex);
Tejun Heo324a56e2013-12-11 14:11:53 -0500520 kn = kernfs_find_ns(parent, name, ns);
521 kernfs_get(kn);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500522 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500523
Tejun Heo324a56e2013-12-11 14:11:53 -0500524 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500525}
526EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
527
528/**
Tejun Heoba7443b2013-11-28 14:54:40 -0500529 * kernfs_create_root - create a new kernfs hierarchy
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500530 * @kdops: optional directory syscall operations for the hierarchy
Tejun Heoba7443b2013-11-28 14:54:40 -0500531 * @priv: opaque data associated with the new directory
532 *
533 * Returns the root of the new hierarchy on success, ERR_PTR() value on
534 * failure.
535 */
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500536struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops, void *priv)
Tejun Heoba7443b2013-11-28 14:54:40 -0500537{
538 struct kernfs_root *root;
Tejun Heo324a56e2013-12-11 14:11:53 -0500539 struct kernfs_node *kn;
Tejun Heoba7443b2013-11-28 14:54:40 -0500540
541 root = kzalloc(sizeof(*root), GFP_KERNEL);
542 if (!root)
543 return ERR_PTR(-ENOMEM);
544
Tejun Heobc755552013-11-28 14:54:41 -0500545 ida_init(&root->ino_ida);
546
Tejun Heodb4aad22014-01-17 09:58:25 -0500547 kn = __kernfs_new_node(root, "", S_IFDIR | S_IRUGO | S_IXUGO,
548 KERNFS_DIR);
Tejun Heo324a56e2013-12-11 14:11:53 -0500549 if (!kn) {
Tejun Heobc755552013-11-28 14:54:41 -0500550 ida_destroy(&root->ino_ida);
Tejun Heoba7443b2013-11-28 14:54:40 -0500551 kfree(root);
552 return ERR_PTR(-ENOMEM);
553 }
554
Tejun Heo81c173c2014-02-03 14:03:00 -0500555 atomic_sub(KN_DEACTIVATED_BIAS, &kn->active);
Tejun Heo324a56e2013-12-11 14:11:53 -0500556 kn->priv = priv;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500557 kn->dir.root = root;
Tejun Heoba7443b2013-11-28 14:54:40 -0500558
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500559 root->dir_ops = kdops;
Tejun Heo324a56e2013-12-11 14:11:53 -0500560 root->kn = kn;
Tejun Heoabd54f02014-02-03 14:02:55 -0500561 init_waitqueue_head(&root->deactivate_waitq);
Tejun Heoba7443b2013-11-28 14:54:40 -0500562
563 return root;
564}
565
566/**
567 * kernfs_destroy_root - destroy a kernfs hierarchy
568 * @root: root of the hierarchy to destroy
569 *
570 * Destroy the hierarchy anchored at @root by removing all existing
571 * directories and destroying @root.
572 */
573void kernfs_destroy_root(struct kernfs_root *root)
574{
Tejun Heo324a56e2013-12-11 14:11:53 -0500575 kernfs_remove(root->kn); /* will also free @root */
Tejun Heoba7443b2013-11-28 14:54:40 -0500576}
577
578/**
Tejun Heofd7b9f72013-11-28 14:54:33 -0500579 * kernfs_create_dir_ns - create a directory
580 * @parent: parent in which to create a new directory
581 * @name: name of the new directory
Tejun Heobb8b9d02013-12-11 16:02:55 -0500582 * @mode: mode of the new directory
Tejun Heofd7b9f72013-11-28 14:54:33 -0500583 * @priv: opaque data associated with the new directory
584 * @ns: optional namespace tag of the directory
585 *
586 * Returns the created node on success, ERR_PTR() value on failure.
587 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500588struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
Tejun Heobb8b9d02013-12-11 16:02:55 -0500589 const char *name, umode_t mode,
590 void *priv, const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500591{
Tejun Heo324a56e2013-12-11 14:11:53 -0500592 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500593 int rc;
594
595 /* allocate */
Tejun Heodb4aad22014-01-17 09:58:25 -0500596 kn = kernfs_new_node(parent, name, mode | S_IFDIR, KERNFS_DIR);
Tejun Heo324a56e2013-12-11 14:11:53 -0500597 if (!kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500598 return ERR_PTR(-ENOMEM);
599
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500600 kn->dir.root = parent->dir.root;
601 kn->ns = ns;
Tejun Heo324a56e2013-12-11 14:11:53 -0500602 kn->priv = priv;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500603
604 /* link in */
Tejun Heo988cd7a2014-02-03 14:02:58 -0500605 rc = kernfs_add_one(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500606 if (!rc)
Tejun Heo324a56e2013-12-11 14:11:53 -0500607 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500608
Tejun Heo324a56e2013-12-11 14:11:53 -0500609 kernfs_put(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500610 return ERR_PTR(rc);
611}
612
Tejun Heoc637b8a2013-12-11 14:11:58 -0500613static struct dentry *kernfs_iop_lookup(struct inode *dir,
614 struct dentry *dentry,
615 unsigned int flags)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500616{
Tejun Heo19bbb922013-12-11 16:02:59 -0500617 struct dentry *ret;
Tejun Heo324a56e2013-12-11 14:11:53 -0500618 struct kernfs_node *parent = dentry->d_parent->d_fsdata;
619 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500620 struct inode *inode;
621 const void *ns = NULL;
622
Tejun Heoa797bfc2013-12-11 14:11:57 -0500623 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500624
Tejun Heo324a56e2013-12-11 14:11:53 -0500625 if (kernfs_ns_enabled(parent))
Tejun Heoc525aad2013-12-11 14:11:55 -0500626 ns = kernfs_info(dir->i_sb)->ns;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500627
Tejun Heo324a56e2013-12-11 14:11:53 -0500628 kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500629
630 /* no such entry */
Tejun Heo324a56e2013-12-11 14:11:53 -0500631 if (!kn) {
Tejun Heo19bbb922013-12-11 16:02:59 -0500632 ret = NULL;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500633 goto out_unlock;
634 }
Tejun Heo324a56e2013-12-11 14:11:53 -0500635 kernfs_get(kn);
636 dentry->d_fsdata = kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500637
638 /* attach dentry and inode */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500639 inode = kernfs_get_inode(dir->i_sb, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500640 if (!inode) {
641 ret = ERR_PTR(-ENOMEM);
642 goto out_unlock;
643 }
644
645 /* instantiate and hash dentry */
646 ret = d_materialise_unique(dentry, inode);
647 out_unlock:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500648 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500649 return ret;
650}
651
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500652static int kernfs_iop_mkdir(struct inode *dir, struct dentry *dentry,
653 umode_t mode)
654{
655 struct kernfs_node *parent = dir->i_private;
656 struct kernfs_dir_ops *kdops = kernfs_root(parent)->dir_ops;
657
658 if (!kdops || !kdops->mkdir)
659 return -EPERM;
660
661 return kdops->mkdir(parent, dentry->d_name.name, mode);
662}
663
664static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
665{
666 struct kernfs_node *kn = dentry->d_fsdata;
667 struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops;
668
669 if (!kdops || !kdops->rmdir)
670 return -EPERM;
671
672 return kdops->rmdir(kn);
673}
674
675static int kernfs_iop_rename(struct inode *old_dir, struct dentry *old_dentry,
676 struct inode *new_dir, struct dentry *new_dentry)
677{
678 struct kernfs_node *kn = old_dentry->d_fsdata;
679 struct kernfs_node *new_parent = new_dir->i_private;
680 struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops;
681
682 if (!kdops || !kdops->rename)
683 return -EPERM;
684
685 return kdops->rename(kn, new_parent, new_dentry->d_name.name);
686}
687
Tejun Heoa797bfc2013-12-11 14:11:57 -0500688const struct inode_operations kernfs_dir_iops = {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500689 .lookup = kernfs_iop_lookup,
690 .permission = kernfs_iop_permission,
691 .setattr = kernfs_iop_setattr,
692 .getattr = kernfs_iop_getattr,
693 .setxattr = kernfs_iop_setxattr,
694 .removexattr = kernfs_iop_removexattr,
695 .getxattr = kernfs_iop_getxattr,
696 .listxattr = kernfs_iop_listxattr,
Tejun Heo80b9bbe2013-12-11 16:03:00 -0500697
698 .mkdir = kernfs_iop_mkdir,
699 .rmdir = kernfs_iop_rmdir,
700 .rename = kernfs_iop_rename,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500701};
702
Tejun Heoc637b8a2013-12-11 14:11:58 -0500703static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500704{
Tejun Heo324a56e2013-12-11 14:11:53 -0500705 struct kernfs_node *last;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500706
707 while (true) {
708 struct rb_node *rbn;
709
710 last = pos;
711
Tejun Heodf23fc32013-12-11 14:11:56 -0500712 if (kernfs_type(pos) != KERNFS_DIR)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500713 break;
714
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500715 rbn = rb_first(&pos->dir.children);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500716 if (!rbn)
717 break;
718
Tejun Heo324a56e2013-12-11 14:11:53 -0500719 pos = rb_to_kn(rbn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500720 }
721
722 return last;
723}
724
725/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500726 * kernfs_next_descendant_post - find the next descendant for post-order walk
Tejun Heofd7b9f72013-11-28 14:54:33 -0500727 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo324a56e2013-12-11 14:11:53 -0500728 * @root: kernfs_node whose descendants to walk
Tejun Heofd7b9f72013-11-28 14:54:33 -0500729 *
730 * Find the next descendant to visit for post-order traversal of @root's
731 * descendants. @root is included in the iteration and the last node to be
732 * visited.
733 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500734static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
735 struct kernfs_node *root)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500736{
737 struct rb_node *rbn;
738
Tejun Heoa797bfc2013-12-11 14:11:57 -0500739 lockdep_assert_held(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500740
741 /* if first iteration, visit leftmost descendant which may be root */
742 if (!pos)
Tejun Heoc637b8a2013-12-11 14:11:58 -0500743 return kernfs_leftmost_descendant(root);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500744
745 /* if we visited @root, we're done */
746 if (pos == root)
747 return NULL;
748
749 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500750 rbn = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500751 if (rbn)
Tejun Heoc637b8a2013-12-11 14:11:58 -0500752 return kernfs_leftmost_descendant(rb_to_kn(rbn));
Tejun Heofd7b9f72013-11-28 14:54:33 -0500753
754 /* no sibling left, visit parent */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500755 return pos->parent;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500756}
757
Tejun Heo988cd7a2014-02-03 14:02:58 -0500758static void __kernfs_remove(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500759{
Tejun Heo35beab02014-02-03 14:02:56 -0500760 struct kernfs_node *pos;
761
762 lockdep_assert_held(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500763
Tejun Heo6b0afc22014-02-03 14:03:01 -0500764 /*
765 * Short-circuit if non-root @kn has already finished removal.
766 * This is for kernfs_remove_self() which plays with active ref
767 * after removal.
768 */
769 if (!kn || (kn->parent && RB_EMPTY_NODE(&kn->rb)))
Greg Kroah-Hartmance9b4992014-01-13 13:50:31 -0800770 return;
771
Tejun Heoc637b8a2013-12-11 14:11:58 -0500772 pr_debug("kernfs %s: removing\n", kn->name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500773
Tejun Heo81c173c2014-02-03 14:03:00 -0500774 /* prevent any new usage under @kn by deactivating all nodes */
Tejun Heo35beab02014-02-03 14:02:56 -0500775 pos = NULL;
776 while ((pos = kernfs_next_descendant_post(pos, kn)))
Tejun Heo81c173c2014-02-03 14:03:00 -0500777 if (kernfs_active(pos))
778 atomic_add(KN_DEACTIVATED_BIAS, &pos->active);
Tejun Heo35beab02014-02-03 14:02:56 -0500779
780 /* deactivate and unlink the subtree node-by-node */
Tejun Heofd7b9f72013-11-28 14:54:33 -0500781 do {
Tejun Heo35beab02014-02-03 14:02:56 -0500782 pos = kernfs_leftmost_descendant(kn);
783
784 /*
Tejun Heo81c173c2014-02-03 14:03:00 -0500785 * kernfs_drain() drops kernfs_mutex temporarily and @pos's
786 * base ref could have been put by someone else by the time
787 * the function returns. Make sure it doesn't go away
788 * underneath us.
Tejun Heo35beab02014-02-03 14:02:56 -0500789 */
790 kernfs_get(pos);
791
Tejun Heo81c173c2014-02-03 14:03:00 -0500792 kernfs_drain(pos);
Tejun Heo35beab02014-02-03 14:02:56 -0500793
794 /*
795 * kernfs_unlink_sibling() succeeds once per node. Use it
796 * to decide who's responsible for cleanups.
797 */
798 if (!pos->parent || kernfs_unlink_sibling(pos)) {
799 struct kernfs_iattrs *ps_iattr =
800 pos->parent ? pos->parent->iattr : NULL;
801
802 /* update timestamps on the parent */
803 if (ps_iattr) {
804 ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME;
805 ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME;
806 }
807
Tejun Heo988cd7a2014-02-03 14:02:58 -0500808 kernfs_put(pos);
Tejun Heo35beab02014-02-03 14:02:56 -0500809 }
810
811 kernfs_put(pos);
812 } while (pos != kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500813}
814
815/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500816 * kernfs_remove - remove a kernfs_node recursively
817 * @kn: the kernfs_node to remove
Tejun Heofd7b9f72013-11-28 14:54:33 -0500818 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500819 * Remove @kn along with all its subdirectories and files.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500820 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500821void kernfs_remove(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500822{
Tejun Heo988cd7a2014-02-03 14:02:58 -0500823 mutex_lock(&kernfs_mutex);
824 __kernfs_remove(kn);
825 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500826}
827
828/**
Tejun Heo6b0afc22014-02-03 14:03:01 -0500829 * kernfs_break_active_protection - break out of active protection
830 * @kn: the self kernfs_node
831 *
832 * The caller must be running off of a kernfs operation which is invoked
833 * with an active reference - e.g. one of kernfs_ops. Each invocation of
834 * this function must also be matched with an invocation of
835 * kernfs_unbreak_active_protection().
836 *
837 * This function releases the active reference of @kn the caller is
838 * holding. Once this function is called, @kn may be removed at any point
839 * and the caller is solely responsible for ensuring that the objects it
840 * dereferences are accessible.
841 */
842void kernfs_break_active_protection(struct kernfs_node *kn)
843{
844 /*
845 * Take out ourself out of the active ref dependency chain. If
846 * we're called without an active ref, lockdep will complain.
847 */
848 kernfs_put_active(kn);
849}
850
851/**
852 * kernfs_unbreak_active_protection - undo kernfs_break_active_protection()
853 * @kn: the self kernfs_node
854 *
855 * If kernfs_break_active_protection() was called, this function must be
856 * invoked before finishing the kernfs operation. Note that while this
857 * function restores the active reference, it doesn't and can't actually
858 * restore the active protection - @kn may already or be in the process of
859 * being removed. Once kernfs_break_active_protection() is invoked, that
860 * protection is irreversibly gone for the kernfs operation instance.
861 *
862 * While this function may be called at any point after
863 * kernfs_break_active_protection() is invoked, its most useful location
864 * would be right before the enclosing kernfs operation returns.
865 */
866void kernfs_unbreak_active_protection(struct kernfs_node *kn)
867{
868 /*
869 * @kn->active could be in any state; however, the increment we do
870 * here will be undone as soon as the enclosing kernfs operation
871 * finishes and this temporary bump can't break anything. If @kn
872 * is alive, nothing changes. If @kn is being deactivated, the
873 * soon-to-follow put will either finish deactivation or restore
874 * deactivated state. If @kn is already removed, the temporary
875 * bump is guaranteed to be gone before @kn is released.
876 */
877 atomic_inc(&kn->active);
878 if (kernfs_lockdep(kn))
879 rwsem_acquire(&kn->dep_map, 0, 1, _RET_IP_);
880}
881
882/**
883 * kernfs_remove_self - remove a kernfs_node from its own method
884 * @kn: the self kernfs_node to remove
885 *
886 * The caller must be running off of a kernfs operation which is invoked
887 * with an active reference - e.g. one of kernfs_ops. This can be used to
888 * implement a file operation which deletes itself.
889 *
890 * For example, the "delete" file for a sysfs device directory can be
891 * implemented by invoking kernfs_remove_self() on the "delete" file
892 * itself. This function breaks the circular dependency of trying to
893 * deactivate self while holding an active ref itself. It isn't necessary
894 * to modify the usual removal path to use kernfs_remove_self(). The
895 * "delete" implementation can simply invoke kernfs_remove_self() on self
896 * before proceeding with the usual removal path. kernfs will ignore later
897 * kernfs_remove() on self.
898 *
899 * kernfs_remove_self() can be called multiple times concurrently on the
900 * same kernfs_node. Only the first one actually performs removal and
901 * returns %true. All others will wait until the kernfs operation which
902 * won self-removal finishes and return %false. Note that the losers wait
903 * for the completion of not only the winning kernfs_remove_self() but also
904 * the whole kernfs_ops which won the arbitration. This can be used to
905 * guarantee, for example, all concurrent writes to a "delete" file to
906 * finish only after the whole operation is complete.
907 */
908bool kernfs_remove_self(struct kernfs_node *kn)
909{
910 bool ret;
911
912 mutex_lock(&kernfs_mutex);
913 kernfs_break_active_protection(kn);
914
915 /*
916 * SUICIDAL is used to arbitrate among competing invocations. Only
917 * the first one will actually perform removal. When the removal
918 * is complete, SUICIDED is set and the active ref is restored
919 * while holding kernfs_mutex. The ones which lost arbitration
920 * waits for SUICDED && drained which can happen only after the
921 * enclosing kernfs operation which executed the winning instance
922 * of kernfs_remove_self() finished.
923 */
924 if (!(kn->flags & KERNFS_SUICIDAL)) {
925 kn->flags |= KERNFS_SUICIDAL;
926 __kernfs_remove(kn);
927 kn->flags |= KERNFS_SUICIDED;
928 ret = true;
929 } else {
930 wait_queue_head_t *waitq = &kernfs_root(kn)->deactivate_waitq;
931 DEFINE_WAIT(wait);
932
933 while (true) {
934 prepare_to_wait(waitq, &wait, TASK_UNINTERRUPTIBLE);
935
936 if ((kn->flags & KERNFS_SUICIDED) &&
937 atomic_read(&kn->active) == KN_DEACTIVATED_BIAS)
938 break;
939
940 mutex_unlock(&kernfs_mutex);
941 schedule();
942 mutex_lock(&kernfs_mutex);
943 }
944 finish_wait(waitq, &wait);
945 WARN_ON_ONCE(!RB_EMPTY_NODE(&kn->rb));
946 ret = false;
947 }
948
949 /*
950 * This must be done while holding kernfs_mutex; otherwise, waiting
951 * for SUICIDED && deactivated could finish prematurely.
952 */
953 kernfs_unbreak_active_protection(kn);
954
955 mutex_unlock(&kernfs_mutex);
956 return ret;
957}
958
959/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500960 * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
961 * @parent: parent of the target
962 * @name: name of the kernfs_node to remove
963 * @ns: namespace tag of the kernfs_node to remove
Tejun Heofd7b9f72013-11-28 14:54:33 -0500964 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500965 * Look for the kernfs_node with @name and @ns under @parent and remove it.
966 * Returns 0 on success, -ENOENT if such entry doesn't exist.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500967 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500968int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500969 const void *ns)
970{
Tejun Heo324a56e2013-12-11 14:11:53 -0500971 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500972
Tejun Heo324a56e2013-12-11 14:11:53 -0500973 if (!parent) {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500974 WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
Tejun Heofd7b9f72013-11-28 14:54:33 -0500975 name);
976 return -ENOENT;
977 }
978
Tejun Heo988cd7a2014-02-03 14:02:58 -0500979 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500980
Tejun Heo324a56e2013-12-11 14:11:53 -0500981 kn = kernfs_find_ns(parent, name, ns);
982 if (kn)
Tejun Heo988cd7a2014-02-03 14:02:58 -0500983 __kernfs_remove(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500984
Tejun Heo988cd7a2014-02-03 14:02:58 -0500985 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500986
Tejun Heo324a56e2013-12-11 14:11:53 -0500987 if (kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500988 return 0;
989 else
990 return -ENOENT;
991}
992
993/**
994 * kernfs_rename_ns - move and rename a kernfs_node
Tejun Heo324a56e2013-12-11 14:11:53 -0500995 * @kn: target node
Tejun Heofd7b9f72013-11-28 14:54:33 -0500996 * @new_parent: new parent to put @sd under
997 * @new_name: new name
998 * @new_ns: new namespace tag
999 */
Tejun Heo324a56e2013-12-11 14:11:53 -05001000int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
Tejun Heofd7b9f72013-11-28 14:54:33 -05001001 const char *new_name, const void *new_ns)
1002{
1003 int error;
1004
Tejun Heoae343722014-01-10 08:57:21 -05001005 mutex_lock(&kernfs_mutex);
Tejun Heod0ae3d42013-12-11 16:02:56 -05001006
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -08001007 error = -ENOENT;
Tejun Heo81c173c2014-02-03 14:03:00 -05001008 if (!kernfs_active(kn) || !kernfs_active(new_parent))
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -08001009 goto out;
1010
Tejun Heofd7b9f72013-11-28 14:54:33 -05001011 error = 0;
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001012 if ((kn->parent == new_parent) && (kn->ns == new_ns) &&
1013 (strcmp(kn->name, new_name) == 0))
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -08001014 goto out; /* nothing to rename */
Tejun Heofd7b9f72013-11-28 14:54:33 -05001015
1016 error = -EEXIST;
1017 if (kernfs_find_ns(new_parent, new_name, new_ns))
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -08001018 goto out;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001019
Tejun Heo324a56e2013-12-11 14:11:53 -05001020 /* rename kernfs_node */
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001021 if (strcmp(kn->name, new_name) != 0) {
Tejun Heofd7b9f72013-11-28 14:54:33 -05001022 error = -ENOMEM;
1023 new_name = kstrdup(new_name, GFP_KERNEL);
1024 if (!new_name)
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -08001025 goto out;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001026
Tejun Heo47a52e92013-12-11 16:02:58 -05001027 if (kn->flags & KERNFS_STATIC_NAME)
1028 kn->flags &= ~KERNFS_STATIC_NAME;
1029 else
1030 kfree(kn->name);
1031
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001032 kn->name = new_name;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001033 }
1034
1035 /*
1036 * Move to the appropriate place in the appropriate directories rbtree.
1037 */
Tejun Heoc637b8a2013-12-11 14:11:58 -05001038 kernfs_unlink_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001039 kernfs_get(new_parent);
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001040 kernfs_put(kn->parent);
1041 kn->ns = new_ns;
Tejun Heoc637b8a2013-12-11 14:11:58 -05001042 kn->hash = kernfs_name_hash(kn->name, kn->ns);
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001043 kn->parent = new_parent;
Tejun Heoc637b8a2013-12-11 14:11:58 -05001044 kernfs_link_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001045
1046 error = 0;
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -08001047 out:
Tejun Heoa797bfc2013-12-11 14:11:57 -05001048 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001049 return error;
1050}
1051
Tejun Heofd7b9f72013-11-28 14:54:33 -05001052/* Relationship between s_mode and the DT_xxx types */
Tejun Heo324a56e2013-12-11 14:11:53 -05001053static inline unsigned char dt_type(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001054{
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001055 return (kn->mode >> 12) & 15;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001056}
1057
Tejun Heoc637b8a2013-12-11 14:11:58 -05001058static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001059{
1060 kernfs_put(filp->private_data);
1061 return 0;
1062}
1063
Tejun Heoc637b8a2013-12-11 14:11:58 -05001064static struct kernfs_node *kernfs_dir_pos(const void *ns,
Tejun Heo324a56e2013-12-11 14:11:53 -05001065 struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001066{
1067 if (pos) {
Tejun Heo81c173c2014-02-03 14:03:00 -05001068 int valid = kernfs_active(pos) &&
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -08001069 pos->parent == parent && hash == pos->hash;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001070 kernfs_put(pos);
1071 if (!valid)
1072 pos = NULL;
1073 }
1074 if (!pos && (hash > 1) && (hash < INT_MAX)) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001075 struct rb_node *node = parent->dir.children.rb_node;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001076 while (node) {
Tejun Heo324a56e2013-12-11 14:11:53 -05001077 pos = rb_to_kn(node);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001078
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001079 if (hash < pos->hash)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001080 node = node->rb_left;
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001081 else if (hash > pos->hash)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001082 node = node->rb_right;
1083 else
1084 break;
1085 }
1086 }
1087 /* Skip over entries in the wrong namespace */
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001088 while (pos && pos->ns != ns) {
1089 struct rb_node *node = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001090 if (!node)
1091 pos = NULL;
1092 else
Tejun Heo324a56e2013-12-11 14:11:53 -05001093 pos = rb_to_kn(node);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001094 }
1095 return pos;
1096}
1097
Tejun Heoc637b8a2013-12-11 14:11:58 -05001098static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
Tejun Heo324a56e2013-12-11 14:11:53 -05001099 struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001100{
Tejun Heoc637b8a2013-12-11 14:11:58 -05001101 pos = kernfs_dir_pos(ns, parent, ino, pos);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001102 if (pos)
1103 do {
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001104 struct rb_node *node = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001105 if (!node)
1106 pos = NULL;
1107 else
Tejun Heo324a56e2013-12-11 14:11:53 -05001108 pos = rb_to_kn(node);
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001109 } while (pos && pos->ns != ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001110 return pos;
1111}
1112
Tejun Heoc637b8a2013-12-11 14:11:58 -05001113static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001114{
1115 struct dentry *dentry = file->f_path.dentry;
Tejun Heo324a56e2013-12-11 14:11:53 -05001116 struct kernfs_node *parent = dentry->d_fsdata;
1117 struct kernfs_node *pos = file->private_data;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001118 const void *ns = NULL;
1119
1120 if (!dir_emit_dots(file, ctx))
1121 return 0;
Tejun Heoa797bfc2013-12-11 14:11:57 -05001122 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001123
Tejun Heo324a56e2013-12-11 14:11:53 -05001124 if (kernfs_ns_enabled(parent))
Tejun Heoc525aad2013-12-11 14:11:55 -05001125 ns = kernfs_info(dentry->d_sb)->ns;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001126
Tejun Heoc637b8a2013-12-11 14:11:58 -05001127 for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001128 pos;
Tejun Heoc637b8a2013-12-11 14:11:58 -05001129 pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001130 const char *name = pos->name;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001131 unsigned int type = dt_type(pos);
1132 int len = strlen(name);
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001133 ino_t ino = pos->ino;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001134
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001135 ctx->pos = pos->hash;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001136 file->private_data = pos;
1137 kernfs_get(pos);
1138
Tejun Heoa797bfc2013-12-11 14:11:57 -05001139 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001140 if (!dir_emit(ctx, name, len, ino, type))
1141 return 0;
Tejun Heoa797bfc2013-12-11 14:11:57 -05001142 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001143 }
Tejun Heoa797bfc2013-12-11 14:11:57 -05001144 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001145 file->private_data = NULL;
1146 ctx->pos = INT_MAX;
1147 return 0;
1148}
1149
Tejun Heoc637b8a2013-12-11 14:11:58 -05001150static loff_t kernfs_dir_fop_llseek(struct file *file, loff_t offset,
1151 int whence)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001152{
1153 struct inode *inode = file_inode(file);
1154 loff_t ret;
1155
1156 mutex_lock(&inode->i_mutex);
1157 ret = generic_file_llseek(file, offset, whence);
1158 mutex_unlock(&inode->i_mutex);
1159
1160 return ret;
1161}
1162
Tejun Heoa797bfc2013-12-11 14:11:57 -05001163const struct file_operations kernfs_dir_fops = {
Tejun Heofd7b9f72013-11-28 14:54:33 -05001164 .read = generic_read_dir,
Tejun Heoc637b8a2013-12-11 14:11:58 -05001165 .iterate = kernfs_fop_readdir,
1166 .release = kernfs_dir_fop_release,
1167 .llseek = kernfs_dir_fop_llseek,
Tejun Heofd7b9f72013-11-28 14:54:33 -05001168};