blob: d038bb204b54ef80d4da19963835dfb174b9d388 [file] [log] [blame]
Tejun Heob8441ed2013-11-24 09:54:58 -05001/*
2 * fs/kernfs/dir.c - kernfs directory implementation
3 *
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
7 *
8 * This file is released under the GPLv2.
9 */
Tejun Heofd7b9f72013-11-28 14:54:33 -050010
11#include <linux/fs.h>
12#include <linux/namei.h>
13#include <linux/idr.h>
14#include <linux/slab.h>
15#include <linux/security.h>
16#include <linux/hash.h>
17
18#include "kernfs-internal.h"
19
20DEFINE_MUTEX(sysfs_mutex);
21
Tejun Heoadc5e8b2013-12-11 14:11:54 -050022#define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb)
Tejun Heofd7b9f72013-11-28 14:54:33 -050023
Tejun Heofd7b9f72013-11-28 14:54:33 -050024/**
25 * sysfs_name_hash
26 * @name: Null terminated string to hash
27 * @ns: Namespace tag to hash
28 *
29 * Returns 31 bit hash of ns + name (so it fits in an off_t )
30 */
31static unsigned int sysfs_name_hash(const char *name, const void *ns)
32{
33 unsigned long hash = init_name_hash();
34 unsigned int len = strlen(name);
35 while (len--)
36 hash = partial_name_hash(*name++, hash);
37 hash = (end_name_hash(hash) ^ hash_ptr((void *)ns, 31));
38 hash &= 0x7fffffffU;
39 /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */
40 if (hash < 1)
41 hash += 2;
42 if (hash >= INT_MAX)
43 hash = INT_MAX - 1;
44 return hash;
45}
46
47static int sysfs_name_compare(unsigned int hash, const char *name,
Tejun Heo324a56e2013-12-11 14:11:53 -050048 const void *ns, const struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -050049{
Tejun Heoadc5e8b2013-12-11 14:11:54 -050050 if (hash != kn->hash)
51 return hash - kn->hash;
52 if (ns != kn->ns)
53 return ns - kn->ns;
54 return strcmp(name, kn->name);
Tejun Heofd7b9f72013-11-28 14:54:33 -050055}
56
Tejun Heo324a56e2013-12-11 14:11:53 -050057static int sysfs_sd_compare(const struct kernfs_node *left,
58 const struct kernfs_node *right)
Tejun Heofd7b9f72013-11-28 14:54:33 -050059{
Tejun Heoadc5e8b2013-12-11 14:11:54 -050060 return sysfs_name_compare(left->hash, left->name, left->ns, right);
Tejun Heofd7b9f72013-11-28 14:54:33 -050061}
62
63/**
Tejun Heo324a56e2013-12-11 14:11:53 -050064 * sysfs_link_sibling - link kernfs_node into sibling rbtree
65 * @kn: kernfs_node of interest
Tejun Heofd7b9f72013-11-28 14:54:33 -050066 *
Tejun Heo324a56e2013-12-11 14:11:53 -050067 * Link @kn into its sibling rbtree which starts from
Tejun Heoadc5e8b2013-12-11 14:11:54 -050068 * @kn->parent->dir.children.
Tejun Heofd7b9f72013-11-28 14:54:33 -050069 *
70 * Locking:
71 * mutex_lock(sysfs_mutex)
72 *
73 * RETURNS:
74 * 0 on susccess -EEXIST on failure.
75 */
Tejun Heo324a56e2013-12-11 14:11:53 -050076static int sysfs_link_sibling(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -050077{
Tejun Heoadc5e8b2013-12-11 14:11:54 -050078 struct rb_node **node = &kn->parent->dir.children.rb_node;
Tejun Heofd7b9f72013-11-28 14:54:33 -050079 struct rb_node *parent = NULL;
80
Tejun Heo324a56e2013-12-11 14:11:53 -050081 if (sysfs_type(kn) == SYSFS_DIR)
Tejun Heoadc5e8b2013-12-11 14:11:54 -050082 kn->parent->dir.subdirs++;
Tejun Heofd7b9f72013-11-28 14:54:33 -050083
84 while (*node) {
Tejun Heo324a56e2013-12-11 14:11:53 -050085 struct kernfs_node *pos;
Tejun Heofd7b9f72013-11-28 14:54:33 -050086 int result;
87
Tejun Heo324a56e2013-12-11 14:11:53 -050088 pos = rb_to_kn(*node);
Tejun Heofd7b9f72013-11-28 14:54:33 -050089 parent = *node;
Tejun Heo324a56e2013-12-11 14:11:53 -050090 result = sysfs_sd_compare(kn, pos);
Tejun Heofd7b9f72013-11-28 14:54:33 -050091 if (result < 0)
Tejun Heoadc5e8b2013-12-11 14:11:54 -050092 node = &pos->rb.rb_left;
Tejun Heofd7b9f72013-11-28 14:54:33 -050093 else if (result > 0)
Tejun Heoadc5e8b2013-12-11 14:11:54 -050094 node = &pos->rb.rb_right;
Tejun Heofd7b9f72013-11-28 14:54:33 -050095 else
96 return -EEXIST;
97 }
98 /* add new node and rebalance the tree */
Tejun Heoadc5e8b2013-12-11 14:11:54 -050099 rb_link_node(&kn->rb, parent, node);
100 rb_insert_color(&kn->rb, &kn->parent->dir.children);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500101 return 0;
102}
103
104/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500105 * sysfs_unlink_sibling - unlink kernfs_node from sibling rbtree
106 * @kn: kernfs_node of interest
Tejun Heofd7b9f72013-11-28 14:54:33 -0500107 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500108 * Unlink @kn from its sibling rbtree which starts from
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500109 * kn->parent->dir.children.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500110 *
111 * Locking:
112 * mutex_lock(sysfs_mutex)
113 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500114static void sysfs_unlink_sibling(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500115{
Tejun Heo324a56e2013-12-11 14:11:53 -0500116 if (sysfs_type(kn) == SYSFS_DIR)
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500117 kn->parent->dir.subdirs--;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500118
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500119 rb_erase(&kn->rb, &kn->parent->dir.children);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500120}
121
122/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500123 * sysfs_get_active - get an active reference to kernfs_node
124 * @kn: kernfs_node to get an active reference to
Tejun Heofd7b9f72013-11-28 14:54:33 -0500125 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500126 * Get an active reference of @kn. This function is noop if @kn
Tejun Heofd7b9f72013-11-28 14:54:33 -0500127 * is NULL.
128 *
129 * RETURNS:
Tejun Heo324a56e2013-12-11 14:11:53 -0500130 * Pointer to @kn on success, NULL on failure.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500131 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500132struct kernfs_node *sysfs_get_active(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500133{
Tejun Heo324a56e2013-12-11 14:11:53 -0500134 if (unlikely(!kn))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500135 return NULL;
136
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500137 if (!atomic_inc_unless_negative(&kn->active))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500138 return NULL;
139
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500140 if (kn->flags & SYSFS_FLAG_LOCKDEP)
Tejun Heo324a56e2013-12-11 14:11:53 -0500141 rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_);
142 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500143}
144
145/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500146 * sysfs_put_active - put an active reference to kernfs_node
147 * @kn: kernfs_node to put an active reference to
Tejun Heofd7b9f72013-11-28 14:54:33 -0500148 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500149 * Put an active reference to @kn. This function is noop if @kn
Tejun Heofd7b9f72013-11-28 14:54:33 -0500150 * is NULL.
151 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500152void sysfs_put_active(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500153{
154 int v;
155
Tejun Heo324a56e2013-12-11 14:11:53 -0500156 if (unlikely(!kn))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500157 return;
158
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500159 if (kn->flags & SYSFS_FLAG_LOCKDEP)
Tejun Heo324a56e2013-12-11 14:11:53 -0500160 rwsem_release(&kn->dep_map, 1, _RET_IP_);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500161 v = atomic_dec_return(&kn->active);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500162 if (likely(v != SD_DEACTIVATED_BIAS))
163 return;
164
Tejun Heo324a56e2013-12-11 14:11:53 -0500165 /*
166 * atomic_dec_return() is a mb(), we'll always see the updated
167 * kn->u.completion.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500168 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500169 complete(kn->u.completion);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500170}
171
172/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500173 * sysfs_deactivate - deactivate kernfs_node
174 * @kn: kernfs_node to deactivate
Tejun Heofd7b9f72013-11-28 14:54:33 -0500175 *
176 * Deny new active references and drain existing ones.
177 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500178static void sysfs_deactivate(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500179{
180 DECLARE_COMPLETION_ONSTACK(wait);
181 int v;
182
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500183 BUG_ON(!(kn->flags & SYSFS_FLAG_REMOVED));
Tejun Heofd7b9f72013-11-28 14:54:33 -0500184
Tejun Heo324a56e2013-12-11 14:11:53 -0500185 if (!(sysfs_type(kn) & SYSFS_ACTIVE_REF))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500186 return;
187
Tejun Heo324a56e2013-12-11 14:11:53 -0500188 kn->u.completion = (void *)&wait;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500189
Tejun Heo324a56e2013-12-11 14:11:53 -0500190 rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500191 /* atomic_add_return() is a mb(), put_active() will always see
Tejun Heo324a56e2013-12-11 14:11:53 -0500192 * the updated kn->u.completion.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500193 */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500194 v = atomic_add_return(SD_DEACTIVATED_BIAS, &kn->active);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500195
196 if (v != SD_DEACTIVATED_BIAS) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500197 lock_contended(&kn->dep_map, _RET_IP_);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500198 wait_for_completion(&wait);
199 }
200
Tejun Heo324a56e2013-12-11 14:11:53 -0500201 lock_acquired(&kn->dep_map, _RET_IP_);
202 rwsem_release(&kn->dep_map, 1, _RET_IP_);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500203}
204
Tejun Heofd7b9f72013-11-28 14:54:33 -0500205/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500206 * kernfs_get - get a reference count on a kernfs_node
207 * @kn: the target kernfs_node
Tejun Heofd7b9f72013-11-28 14:54:33 -0500208 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500209void kernfs_get(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500210{
Tejun Heo324a56e2013-12-11 14:11:53 -0500211 if (kn) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500212 WARN_ON(!atomic_read(&kn->count));
213 atomic_inc(&kn->count);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500214 }
215}
216EXPORT_SYMBOL_GPL(kernfs_get);
217
218/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500219 * kernfs_put - put a reference count on a kernfs_node
220 * @kn: the target kernfs_node
Tejun Heofd7b9f72013-11-28 14:54:33 -0500221 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500222 * Put a reference count of @kn and destroy it if it reached zero.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500223 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500224void kernfs_put(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500225{
Tejun Heo324a56e2013-12-11 14:11:53 -0500226 struct kernfs_node *parent;
Tejun Heoba7443b2013-11-28 14:54:40 -0500227 struct kernfs_root *root;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500228
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500229 if (!kn || !atomic_dec_and_test(&kn->count))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500230 return;
Tejun Heo324a56e2013-12-11 14:11:53 -0500231 root = kernfs_root(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500232 repeat:
233 /* Moving/renaming is always done while holding reference.
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500234 * kn->parent won't change beneath us.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500235 */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500236 parent = kn->parent;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500237
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500238 WARN(!(kn->flags & SYSFS_FLAG_REMOVED),
Tejun Heofd7b9f72013-11-28 14:54:33 -0500239 "sysfs: free using entry: %s/%s\n",
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500240 parent ? parent->name : "", kn->name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500241
Tejun Heo324a56e2013-12-11 14:11:53 -0500242 if (sysfs_type(kn) == SYSFS_KOBJ_LINK)
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500243 kernfs_put(kn->symlink.target_kn);
Tejun Heo324a56e2013-12-11 14:11:53 -0500244 if (sysfs_type(kn) & SYSFS_COPY_NAME)
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500245 kfree(kn->name);
246 if (kn->iattr) {
247 if (kn->iattr->ia_secdata)
248 security_release_secctx(kn->iattr->ia_secdata,
249 kn->iattr->ia_secdata_len);
250 simple_xattrs_free(&kn->iattr->xattrs);
Tejun Heo23223922013-11-23 17:40:02 -0500251 }
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500252 kfree(kn->iattr);
253 ida_simple_remove(&root->ino_ida, kn->ino);
Tejun Heo324a56e2013-12-11 14:11:53 -0500254 kmem_cache_free(sysfs_dir_cachep, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500255
Tejun Heo324a56e2013-12-11 14:11:53 -0500256 kn = parent;
257 if (kn) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500258 if (atomic_dec_and_test(&kn->count))
Tejun Heoba7443b2013-11-28 14:54:40 -0500259 goto repeat;
260 } else {
Tejun Heo324a56e2013-12-11 14:11:53 -0500261 /* just released the root kn, free @root too */
Tejun Heobc755552013-11-28 14:54:41 -0500262 ida_destroy(&root->ino_ida);
Tejun Heoba7443b2013-11-28 14:54:40 -0500263 kfree(root);
264 }
Tejun Heofd7b9f72013-11-28 14:54:33 -0500265}
266EXPORT_SYMBOL_GPL(kernfs_put);
267
268static int sysfs_dentry_delete(const struct dentry *dentry)
269{
Tejun Heo324a56e2013-12-11 14:11:53 -0500270 struct kernfs_node *kn = dentry->d_fsdata;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500271 return !(kn && !(kn->flags & SYSFS_FLAG_REMOVED));
Tejun Heofd7b9f72013-11-28 14:54:33 -0500272}
273
274static int sysfs_dentry_revalidate(struct dentry *dentry, unsigned int flags)
275{
Tejun Heo324a56e2013-12-11 14:11:53 -0500276 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500277
278 if (flags & LOOKUP_RCU)
279 return -ECHILD;
280
Tejun Heo324a56e2013-12-11 14:11:53 -0500281 kn = dentry->d_fsdata;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500282 mutex_lock(&sysfs_mutex);
283
284 /* The sysfs dirent has been deleted */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500285 if (kn->flags & SYSFS_FLAG_REMOVED)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500286 goto out_bad;
287
288 /* The sysfs dirent has been moved? */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500289 if (dentry->d_parent->d_fsdata != kn->parent)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500290 goto out_bad;
291
292 /* The sysfs dirent has been renamed */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500293 if (strcmp(dentry->d_name.name, kn->name) != 0)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500294 goto out_bad;
295
296 /* The sysfs dirent has been moved to a different namespace */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500297 if (kn->parent && kernfs_ns_enabled(kn->parent) &&
Tejun Heoc525aad2013-12-11 14:11:55 -0500298 kernfs_info(dentry->d_sb)->ns != kn->ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500299 goto out_bad;
300
301 mutex_unlock(&sysfs_mutex);
302out_valid:
303 return 1;
304out_bad:
305 /* Remove the dentry from the dcache hashes.
306 * If this is a deleted dentry we use d_drop instead of d_delete
307 * so sysfs doesn't need to cope with negative dentries.
308 *
309 * If this is a dentry that has simply been renamed we
310 * use d_drop to remove it from the dcache lookup on its
311 * old parent. If this dentry persists later when a lookup
312 * is performed at its new name the dentry will be readded
313 * to the dcache hashes.
314 */
315 mutex_unlock(&sysfs_mutex);
316
317 /* If we have submounts we must allow the vfs caches
318 * to lie about the state of the filesystem to prevent
319 * leaks and other nasty things.
320 */
321 if (check_submounts_and_drop(dentry) != 0)
322 goto out_valid;
323
324 return 0;
325}
326
327static void sysfs_dentry_release(struct dentry *dentry)
328{
329 kernfs_put(dentry->d_fsdata);
330}
331
332const struct dentry_operations sysfs_dentry_ops = {
333 .d_revalidate = sysfs_dentry_revalidate,
334 .d_delete = sysfs_dentry_delete,
335 .d_release = sysfs_dentry_release,
336};
337
Tejun Heo324a56e2013-12-11 14:11:53 -0500338struct kernfs_node *sysfs_new_dirent(struct kernfs_root *root,
339 const char *name, umode_t mode, int type)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500340{
341 char *dup_name = NULL;
Tejun Heo324a56e2013-12-11 14:11:53 -0500342 struct kernfs_node *kn;
Tejun Heobc755552013-11-28 14:54:41 -0500343 int ret;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500344
345 if (type & SYSFS_COPY_NAME) {
346 name = dup_name = kstrdup(name, GFP_KERNEL);
347 if (!name)
348 return NULL;
349 }
350
Tejun Heo324a56e2013-12-11 14:11:53 -0500351 kn = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL);
352 if (!kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500353 goto err_out1;
354
Tejun Heobc755552013-11-28 14:54:41 -0500355 ret = ida_simple_get(&root->ino_ida, 1, 0, GFP_KERNEL);
356 if (ret < 0)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500357 goto err_out2;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500358 kn->ino = ret;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500359
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500360 atomic_set(&kn->count, 1);
361 atomic_set(&kn->active, 0);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500362
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500363 kn->name = name;
364 kn->mode = mode;
365 kn->flags = type | SYSFS_FLAG_REMOVED;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500366
Tejun Heo324a56e2013-12-11 14:11:53 -0500367 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500368
369 err_out2:
Tejun Heo324a56e2013-12-11 14:11:53 -0500370 kmem_cache_free(sysfs_dir_cachep, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500371 err_out1:
372 kfree(dup_name);
373 return NULL;
374}
375
376/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500377 * sysfs_addrm_start - prepare for kernfs_node add/remove
Tejun Heoc525aad2013-12-11 14:11:55 -0500378 * @acxt: pointer to kernfs_addrm_cxt to be used
Tejun Heofd7b9f72013-11-28 14:54:33 -0500379 *
380 * This function is called when the caller is about to add or remove
Tejun Heo324a56e2013-12-11 14:11:53 -0500381 * kernfs_node. This function acquires sysfs_mutex. @acxt is used to
382 * keep and pass context to other addrm functions.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500383 *
384 * LOCKING:
385 * Kernel thread context (may sleep). sysfs_mutex is locked on
386 * return.
387 */
Tejun Heoc525aad2013-12-11 14:11:55 -0500388void sysfs_addrm_start(struct kernfs_addrm_cxt *acxt)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500389 __acquires(sysfs_mutex)
390{
391 memset(acxt, 0, sizeof(*acxt));
392
393 mutex_lock(&sysfs_mutex);
394}
395
396/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500397 * sysfs_add_one - add kernfs_node to parent without warning
Tejun Heofd7b9f72013-11-28 14:54:33 -0500398 * @acxt: addrm context to use
Tejun Heo324a56e2013-12-11 14:11:53 -0500399 * @kn: kernfs_node to be added
400 * @parent: the parent kernfs_node to add @kn to
Tejun Heofd7b9f72013-11-28 14:54:33 -0500401 *
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500402 * Get @parent and set @kn->parent to it and increment nlink of the
403 * parent inode if @kn is a directory and link into the children list
404 * of the parent.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500405 *
406 * This function should be called between calls to
407 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
408 * passed the same @acxt as passed to sysfs_addrm_start().
409 *
410 * LOCKING:
411 * Determined by sysfs_addrm_start().
412 *
413 * RETURNS:
414 * 0 on success, -EEXIST if entry with the given name already
415 * exists.
416 */
Tejun Heoc525aad2013-12-11 14:11:55 -0500417int sysfs_add_one(struct kernfs_addrm_cxt *acxt, struct kernfs_node *kn,
Tejun Heo324a56e2013-12-11 14:11:53 -0500418 struct kernfs_node *parent)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500419{
Tejun Heo324a56e2013-12-11 14:11:53 -0500420 bool has_ns = kernfs_ns_enabled(parent);
Tejun Heoc525aad2013-12-11 14:11:55 -0500421 struct kernfs_iattrs *ps_iattr;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500422 int ret;
423
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500424 if (has_ns != (bool)kn->ns) {
Tejun Heofd7b9f72013-11-28 14:54:33 -0500425 WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n",
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500426 has_ns ? "required" : "invalid", parent->name, kn->name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500427 return -EINVAL;
428 }
429
Tejun Heo324a56e2013-12-11 14:11:53 -0500430 if (sysfs_type(parent) != SYSFS_DIR)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500431 return -EINVAL;
432
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500433 kn->hash = sysfs_name_hash(kn->name, kn->ns);
434 kn->parent = parent;
Tejun Heo324a56e2013-12-11 14:11:53 -0500435 kernfs_get(parent);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500436
Tejun Heo324a56e2013-12-11 14:11:53 -0500437 ret = sysfs_link_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500438 if (ret)
439 return ret;
440
441 /* Update timestamps on the parent */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500442 ps_iattr = parent->iattr;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500443 if (ps_iattr) {
444 struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
445 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
446 }
447
448 /* Mark the entry added into directory tree */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500449 kn->flags &= ~SYSFS_FLAG_REMOVED;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500450
451 return 0;
452}
453
454/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500455 * sysfs_remove_one - remove kernfs_node from parent
Tejun Heofd7b9f72013-11-28 14:54:33 -0500456 * @acxt: addrm context to use
Tejun Heo324a56e2013-12-11 14:11:53 -0500457 * @kn: kernfs_node to be removed
Tejun Heofd7b9f72013-11-28 14:54:33 -0500458 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500459 * Mark @kn removed and drop nlink of parent inode if @kn is a
460 * directory. @kn is unlinked from the children list.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500461 *
462 * This function should be called between calls to
463 * sysfs_addrm_start() and sysfs_addrm_finish() and should be
464 * passed the same @acxt as passed to sysfs_addrm_start().
465 *
466 * LOCKING:
467 * Determined by sysfs_addrm_start().
468 */
Tejun Heoc525aad2013-12-11 14:11:55 -0500469static void sysfs_remove_one(struct kernfs_addrm_cxt *acxt,
Tejun Heo324a56e2013-12-11 14:11:53 -0500470 struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500471{
Tejun Heoc525aad2013-12-11 14:11:55 -0500472 struct kernfs_iattrs *ps_iattr;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500473
474 /*
475 * Removal can be called multiple times on the same node. Only the
476 * first invocation is effective and puts the base ref.
477 */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500478 if (kn->flags & SYSFS_FLAG_REMOVED)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500479 return;
480
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500481 if (kn->parent) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500482 sysfs_unlink_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500483
Tejun Heoba7443b2013-11-28 14:54:40 -0500484 /* Update timestamps on the parent */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500485 ps_iattr = kn->parent->iattr;
Tejun Heoba7443b2013-11-28 14:54:40 -0500486 if (ps_iattr) {
487 ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME;
488 ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME;
489 }
Tejun Heofd7b9f72013-11-28 14:54:33 -0500490 }
491
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500492 kn->flags |= SYSFS_FLAG_REMOVED;
Tejun Heo324a56e2013-12-11 14:11:53 -0500493 kn->u.removed_list = acxt->removed;
494 acxt->removed = kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500495}
496
497/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500498 * sysfs_addrm_finish - finish up kernfs_node add/remove
Tejun Heofd7b9f72013-11-28 14:54:33 -0500499 * @acxt: addrm context to finish up
500 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500501 * Finish up kernfs_node add/remove. Resources acquired by
502 * sysfs_addrm_start() are released and removed kernfs_nodes are
Tejun Heofd7b9f72013-11-28 14:54:33 -0500503 * cleaned up.
504 *
505 * LOCKING:
506 * sysfs_mutex is released.
507 */
Tejun Heoc525aad2013-12-11 14:11:55 -0500508void sysfs_addrm_finish(struct kernfs_addrm_cxt *acxt)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500509 __releases(sysfs_mutex)
510{
511 /* release resources acquired by sysfs_addrm_start() */
512 mutex_unlock(&sysfs_mutex);
513
Tejun Heo324a56e2013-12-11 14:11:53 -0500514 /* kill removed kernfs_nodes */
Tejun Heofd7b9f72013-11-28 14:54:33 -0500515 while (acxt->removed) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500516 struct kernfs_node *kn = acxt->removed;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500517
Tejun Heo324a56e2013-12-11 14:11:53 -0500518 acxt->removed = kn->u.removed_list;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500519
Tejun Heo324a56e2013-12-11 14:11:53 -0500520 sysfs_deactivate(kn);
521 sysfs_unmap_bin_file(kn);
522 kernfs_put(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500523 }
524}
525
526/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500527 * kernfs_find_ns - find kernfs_node with the given name
528 * @parent: kernfs_node to search under
Tejun Heofd7b9f72013-11-28 14:54:33 -0500529 * @name: name to look for
530 * @ns: the namespace tag to use
531 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500532 * Look for kernfs_node with name @name under @parent. Returns pointer to
533 * the found kernfs_node on success, %NULL on failure.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500534 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500535static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
536 const unsigned char *name,
537 const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500538{
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500539 struct rb_node *node = parent->dir.children.rb_node;
Tejun Heoac9bba02013-11-29 17:19:09 -0500540 bool has_ns = kernfs_ns_enabled(parent);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500541 unsigned int hash;
542
543 lockdep_assert_held(&sysfs_mutex);
544
545 if (has_ns != (bool)ns) {
546 WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n",
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500547 has_ns ? "required" : "invalid", parent->name, name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500548 return NULL;
549 }
550
551 hash = sysfs_name_hash(name, ns);
552 while (node) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500553 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500554 int result;
555
Tejun Heo324a56e2013-12-11 14:11:53 -0500556 kn = rb_to_kn(node);
557 result = sysfs_name_compare(hash, name, ns, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500558 if (result < 0)
559 node = node->rb_left;
560 else if (result > 0)
561 node = node->rb_right;
562 else
Tejun Heo324a56e2013-12-11 14:11:53 -0500563 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500564 }
565 return NULL;
566}
567
568/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500569 * kernfs_find_and_get_ns - find and get kernfs_node with the given name
570 * @parent: kernfs_node to search under
Tejun Heofd7b9f72013-11-28 14:54:33 -0500571 * @name: name to look for
572 * @ns: the namespace tag to use
573 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500574 * Look for kernfs_node with name @name under @parent and get a reference
Tejun Heofd7b9f72013-11-28 14:54:33 -0500575 * if found. This function may sleep and returns pointer to the found
Tejun Heo324a56e2013-12-11 14:11:53 -0500576 * kernfs_node on success, %NULL on failure.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500577 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500578struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
579 const char *name, const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500580{
Tejun Heo324a56e2013-12-11 14:11:53 -0500581 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500582
583 mutex_lock(&sysfs_mutex);
Tejun Heo324a56e2013-12-11 14:11:53 -0500584 kn = kernfs_find_ns(parent, name, ns);
585 kernfs_get(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500586 mutex_unlock(&sysfs_mutex);
587
Tejun Heo324a56e2013-12-11 14:11:53 -0500588 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500589}
590EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
591
592/**
Tejun Heoba7443b2013-11-28 14:54:40 -0500593 * kernfs_create_root - create a new kernfs hierarchy
594 * @priv: opaque data associated with the new directory
595 *
596 * Returns the root of the new hierarchy on success, ERR_PTR() value on
597 * failure.
598 */
599struct kernfs_root *kernfs_create_root(void *priv)
600{
601 struct kernfs_root *root;
Tejun Heo324a56e2013-12-11 14:11:53 -0500602 struct kernfs_node *kn;
Tejun Heoba7443b2013-11-28 14:54:40 -0500603
604 root = kzalloc(sizeof(*root), GFP_KERNEL);
605 if (!root)
606 return ERR_PTR(-ENOMEM);
607
Tejun Heobc755552013-11-28 14:54:41 -0500608 ida_init(&root->ino_ida);
609
Tejun Heo324a56e2013-12-11 14:11:53 -0500610 kn = sysfs_new_dirent(root, "", S_IFDIR | S_IRUGO | S_IXUGO, SYSFS_DIR);
611 if (!kn) {
Tejun Heobc755552013-11-28 14:54:41 -0500612 ida_destroy(&root->ino_ida);
Tejun Heoba7443b2013-11-28 14:54:40 -0500613 kfree(root);
614 return ERR_PTR(-ENOMEM);
615 }
616
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500617 kn->flags &= ~SYSFS_FLAG_REMOVED;
Tejun Heo324a56e2013-12-11 14:11:53 -0500618 kn->priv = priv;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500619 kn->dir.root = root;
Tejun Heoba7443b2013-11-28 14:54:40 -0500620
Tejun Heo324a56e2013-12-11 14:11:53 -0500621 root->kn = kn;
Tejun Heoba7443b2013-11-28 14:54:40 -0500622
623 return root;
624}
625
626/**
627 * kernfs_destroy_root - destroy a kernfs hierarchy
628 * @root: root of the hierarchy to destroy
629 *
630 * Destroy the hierarchy anchored at @root by removing all existing
631 * directories and destroying @root.
632 */
633void kernfs_destroy_root(struct kernfs_root *root)
634{
Tejun Heo324a56e2013-12-11 14:11:53 -0500635 kernfs_remove(root->kn); /* will also free @root */
Tejun Heoba7443b2013-11-28 14:54:40 -0500636}
637
638/**
Tejun Heofd7b9f72013-11-28 14:54:33 -0500639 * kernfs_create_dir_ns - create a directory
640 * @parent: parent in which to create a new directory
641 * @name: name of the new directory
642 * @priv: opaque data associated with the new directory
643 * @ns: optional namespace tag of the directory
644 *
645 * Returns the created node on success, ERR_PTR() value on failure.
646 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500647struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
648 const char *name, void *priv,
649 const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500650{
651 umode_t mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
Tejun Heoc525aad2013-12-11 14:11:55 -0500652 struct kernfs_addrm_cxt acxt;
Tejun Heo324a56e2013-12-11 14:11:53 -0500653 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500654 int rc;
655
656 /* allocate */
Tejun Heo324a56e2013-12-11 14:11:53 -0500657 kn = sysfs_new_dirent(kernfs_root(parent), name, mode, SYSFS_DIR);
658 if (!kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500659 return ERR_PTR(-ENOMEM);
660
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500661 kn->dir.root = parent->dir.root;
662 kn->ns = ns;
Tejun Heo324a56e2013-12-11 14:11:53 -0500663 kn->priv = priv;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500664
665 /* link in */
666 sysfs_addrm_start(&acxt);
Tejun Heo324a56e2013-12-11 14:11:53 -0500667 rc = sysfs_add_one(&acxt, kn, parent);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500668 sysfs_addrm_finish(&acxt);
669
670 if (!rc)
Tejun Heo324a56e2013-12-11 14:11:53 -0500671 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500672
Tejun Heo324a56e2013-12-11 14:11:53 -0500673 kernfs_put(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500674 return ERR_PTR(rc);
675}
676
677static struct dentry *sysfs_lookup(struct inode *dir, struct dentry *dentry,
678 unsigned int flags)
679{
680 struct dentry *ret = NULL;
Tejun Heo324a56e2013-12-11 14:11:53 -0500681 struct kernfs_node *parent = dentry->d_parent->d_fsdata;
682 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500683 struct inode *inode;
684 const void *ns = NULL;
685
686 mutex_lock(&sysfs_mutex);
687
Tejun Heo324a56e2013-12-11 14:11:53 -0500688 if (kernfs_ns_enabled(parent))
Tejun Heoc525aad2013-12-11 14:11:55 -0500689 ns = kernfs_info(dir->i_sb)->ns;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500690
Tejun Heo324a56e2013-12-11 14:11:53 -0500691 kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500692
693 /* no such entry */
Tejun Heo324a56e2013-12-11 14:11:53 -0500694 if (!kn) {
Tejun Heofd7b9f72013-11-28 14:54:33 -0500695 ret = ERR_PTR(-ENOENT);
696 goto out_unlock;
697 }
Tejun Heo324a56e2013-12-11 14:11:53 -0500698 kernfs_get(kn);
699 dentry->d_fsdata = kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500700
701 /* attach dentry and inode */
Tejun Heo324a56e2013-12-11 14:11:53 -0500702 inode = sysfs_get_inode(dir->i_sb, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500703 if (!inode) {
704 ret = ERR_PTR(-ENOMEM);
705 goto out_unlock;
706 }
707
708 /* instantiate and hash dentry */
709 ret = d_materialise_unique(dentry, inode);
710 out_unlock:
711 mutex_unlock(&sysfs_mutex);
712 return ret;
713}
714
715const struct inode_operations sysfs_dir_inode_operations = {
716 .lookup = sysfs_lookup,
717 .permission = sysfs_permission,
718 .setattr = sysfs_setattr,
719 .getattr = sysfs_getattr,
720 .setxattr = sysfs_setxattr,
Tejun Heo23223922013-11-23 17:40:02 -0500721 .removexattr = sysfs_removexattr,
722 .getxattr = sysfs_getxattr,
723 .listxattr = sysfs_listxattr,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500724};
725
Tejun Heo324a56e2013-12-11 14:11:53 -0500726static struct kernfs_node *sysfs_leftmost_descendant(struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500727{
Tejun Heo324a56e2013-12-11 14:11:53 -0500728 struct kernfs_node *last;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500729
730 while (true) {
731 struct rb_node *rbn;
732
733 last = pos;
734
735 if (sysfs_type(pos) != SYSFS_DIR)
736 break;
737
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500738 rbn = rb_first(&pos->dir.children);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500739 if (!rbn)
740 break;
741
Tejun Heo324a56e2013-12-11 14:11:53 -0500742 pos = rb_to_kn(rbn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500743 }
744
745 return last;
746}
747
748/**
749 * sysfs_next_descendant_post - find the next descendant for post-order walk
750 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo324a56e2013-12-11 14:11:53 -0500751 * @root: kernfs_node whose descendants to walk
Tejun Heofd7b9f72013-11-28 14:54:33 -0500752 *
753 * Find the next descendant to visit for post-order traversal of @root's
754 * descendants. @root is included in the iteration and the last node to be
755 * visited.
756 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500757static struct kernfs_node *sysfs_next_descendant_post(struct kernfs_node *pos,
758 struct kernfs_node *root)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500759{
760 struct rb_node *rbn;
761
762 lockdep_assert_held(&sysfs_mutex);
763
764 /* if first iteration, visit leftmost descendant which may be root */
765 if (!pos)
766 return sysfs_leftmost_descendant(root);
767
768 /* if we visited @root, we're done */
769 if (pos == root)
770 return NULL;
771
772 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500773 rbn = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500774 if (rbn)
Tejun Heo324a56e2013-12-11 14:11:53 -0500775 return sysfs_leftmost_descendant(rb_to_kn(rbn));
Tejun Heofd7b9f72013-11-28 14:54:33 -0500776
777 /* no sibling left, visit parent */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500778 return pos->parent;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500779}
780
Tejun Heoc525aad2013-12-11 14:11:55 -0500781static void __kernfs_remove(struct kernfs_addrm_cxt *acxt,
Tejun Heo324a56e2013-12-11 14:11:53 -0500782 struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500783{
Tejun Heo324a56e2013-12-11 14:11:53 -0500784 struct kernfs_node *pos, *next;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500785
Tejun Heo324a56e2013-12-11 14:11:53 -0500786 if (!kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500787 return;
788
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500789 pr_debug("sysfs %s: removing\n", kn->name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500790
791 next = NULL;
792 do {
793 pos = next;
Tejun Heo324a56e2013-12-11 14:11:53 -0500794 next = sysfs_next_descendant_post(pos, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500795 if (pos)
796 sysfs_remove_one(acxt, pos);
797 } while (next);
798}
799
800/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500801 * kernfs_remove - remove a kernfs_node recursively
802 * @kn: the kernfs_node to remove
Tejun Heofd7b9f72013-11-28 14:54:33 -0500803 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500804 * Remove @kn along with all its subdirectories and files.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500805 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500806void kernfs_remove(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500807{
Tejun Heoc525aad2013-12-11 14:11:55 -0500808 struct kernfs_addrm_cxt acxt;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500809
810 sysfs_addrm_start(&acxt);
Tejun Heo324a56e2013-12-11 14:11:53 -0500811 __kernfs_remove(&acxt, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500812 sysfs_addrm_finish(&acxt);
813}
814
815/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500816 * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
817 * @parent: parent of the target
818 * @name: name of the kernfs_node to remove
819 * @ns: namespace tag of the kernfs_node to remove
Tejun Heofd7b9f72013-11-28 14:54:33 -0500820 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500821 * Look for the kernfs_node with @name and @ns under @parent and remove it.
822 * Returns 0 on success, -ENOENT if such entry doesn't exist.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500823 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500824int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500825 const void *ns)
826{
Tejun Heoc525aad2013-12-11 14:11:55 -0500827 struct kernfs_addrm_cxt acxt;
Tejun Heo324a56e2013-12-11 14:11:53 -0500828 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500829
Tejun Heo324a56e2013-12-11 14:11:53 -0500830 if (!parent) {
Tejun Heofd7b9f72013-11-28 14:54:33 -0500831 WARN(1, KERN_WARNING "sysfs: can not remove '%s', no directory\n",
832 name);
833 return -ENOENT;
834 }
835
836 sysfs_addrm_start(&acxt);
837
Tejun Heo324a56e2013-12-11 14:11:53 -0500838 kn = kernfs_find_ns(parent, name, ns);
839 if (kn)
840 __kernfs_remove(&acxt, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500841
842 sysfs_addrm_finish(&acxt);
843
Tejun Heo324a56e2013-12-11 14:11:53 -0500844 if (kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500845 return 0;
846 else
847 return -ENOENT;
848}
849
850/**
851 * kernfs_rename_ns - move and rename a kernfs_node
Tejun Heo324a56e2013-12-11 14:11:53 -0500852 * @kn: target node
Tejun Heofd7b9f72013-11-28 14:54:33 -0500853 * @new_parent: new parent to put @sd under
854 * @new_name: new name
855 * @new_ns: new namespace tag
856 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500857int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500858 const char *new_name, const void *new_ns)
859{
860 int error;
861
862 mutex_lock(&sysfs_mutex);
863
864 error = 0;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500865 if ((kn->parent == new_parent) && (kn->ns == new_ns) &&
866 (strcmp(kn->name, new_name) == 0))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500867 goto out; /* nothing to rename */
868
869 error = -EEXIST;
870 if (kernfs_find_ns(new_parent, new_name, new_ns))
871 goto out;
872
Tejun Heo324a56e2013-12-11 14:11:53 -0500873 /* rename kernfs_node */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500874 if (strcmp(kn->name, new_name) != 0) {
Tejun Heofd7b9f72013-11-28 14:54:33 -0500875 error = -ENOMEM;
876 new_name = kstrdup(new_name, GFP_KERNEL);
877 if (!new_name)
878 goto out;
879
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500880 kfree(kn->name);
881 kn->name = new_name;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500882 }
883
884 /*
885 * Move to the appropriate place in the appropriate directories rbtree.
886 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500887 sysfs_unlink_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500888 kernfs_get(new_parent);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500889 kernfs_put(kn->parent);
890 kn->ns = new_ns;
891 kn->hash = sysfs_name_hash(kn->name, kn->ns);
892 kn->parent = new_parent;
Tejun Heo324a56e2013-12-11 14:11:53 -0500893 sysfs_link_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500894
895 error = 0;
896 out:
897 mutex_unlock(&sysfs_mutex);
898 return error;
899}
900
Tejun Heofd7b9f72013-11-28 14:54:33 -0500901/* Relationship between s_mode and the DT_xxx types */
Tejun Heo324a56e2013-12-11 14:11:53 -0500902static inline unsigned char dt_type(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500903{
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500904 return (kn->mode >> 12) & 15;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500905}
906
907static int sysfs_dir_release(struct inode *inode, struct file *filp)
908{
909 kernfs_put(filp->private_data);
910 return 0;
911}
912
Tejun Heo324a56e2013-12-11 14:11:53 -0500913static struct kernfs_node *sysfs_dir_pos(const void *ns,
914 struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500915{
916 if (pos) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500917 int valid = !(pos->flags & SYSFS_FLAG_REMOVED) &&
918 pos->parent == parent && hash == pos->hash;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500919 kernfs_put(pos);
920 if (!valid)
921 pos = NULL;
922 }
923 if (!pos && (hash > 1) && (hash < INT_MAX)) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500924 struct rb_node *node = parent->dir.children.rb_node;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500925 while (node) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500926 pos = rb_to_kn(node);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500927
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500928 if (hash < pos->hash)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500929 node = node->rb_left;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500930 else if (hash > pos->hash)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500931 node = node->rb_right;
932 else
933 break;
934 }
935 }
936 /* Skip over entries in the wrong namespace */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500937 while (pos && pos->ns != ns) {
938 struct rb_node *node = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500939 if (!node)
940 pos = NULL;
941 else
Tejun Heo324a56e2013-12-11 14:11:53 -0500942 pos = rb_to_kn(node);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500943 }
944 return pos;
945}
946
Tejun Heo324a56e2013-12-11 14:11:53 -0500947static struct kernfs_node *sysfs_dir_next_pos(const void *ns,
948 struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500949{
Tejun Heo324a56e2013-12-11 14:11:53 -0500950 pos = sysfs_dir_pos(ns, parent, ino, pos);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500951 if (pos)
952 do {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500953 struct rb_node *node = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500954 if (!node)
955 pos = NULL;
956 else
Tejun Heo324a56e2013-12-11 14:11:53 -0500957 pos = rb_to_kn(node);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500958 } while (pos && pos->ns != ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500959 return pos;
960}
961
962static int sysfs_readdir(struct file *file, struct dir_context *ctx)
963{
964 struct dentry *dentry = file->f_path.dentry;
Tejun Heo324a56e2013-12-11 14:11:53 -0500965 struct kernfs_node *parent = dentry->d_fsdata;
966 struct kernfs_node *pos = file->private_data;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500967 const void *ns = NULL;
968
969 if (!dir_emit_dots(file, ctx))
970 return 0;
971 mutex_lock(&sysfs_mutex);
972
Tejun Heo324a56e2013-12-11 14:11:53 -0500973 if (kernfs_ns_enabled(parent))
Tejun Heoc525aad2013-12-11 14:11:55 -0500974 ns = kernfs_info(dentry->d_sb)->ns;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500975
Tejun Heo324a56e2013-12-11 14:11:53 -0500976 for (pos = sysfs_dir_pos(ns, parent, ctx->pos, pos);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500977 pos;
Tejun Heo324a56e2013-12-11 14:11:53 -0500978 pos = sysfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500979 const char *name = pos->name;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500980 unsigned int type = dt_type(pos);
981 int len = strlen(name);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500982 ino_t ino = pos->ino;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500983
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500984 ctx->pos = pos->hash;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500985 file->private_data = pos;
986 kernfs_get(pos);
987
988 mutex_unlock(&sysfs_mutex);
989 if (!dir_emit(ctx, name, len, ino, type))
990 return 0;
991 mutex_lock(&sysfs_mutex);
992 }
993 mutex_unlock(&sysfs_mutex);
994 file->private_data = NULL;
995 ctx->pos = INT_MAX;
996 return 0;
997}
998
999static loff_t sysfs_dir_llseek(struct file *file, loff_t offset, int whence)
1000{
1001 struct inode *inode = file_inode(file);
1002 loff_t ret;
1003
1004 mutex_lock(&inode->i_mutex);
1005 ret = generic_file_llseek(file, offset, whence);
1006 mutex_unlock(&inode->i_mutex);
1007
1008 return ret;
1009}
1010
1011const struct file_operations sysfs_dir_operations = {
1012 .read = generic_read_dir,
1013 .iterate = sysfs_readdir,
1014 .release = sysfs_dir_release,
1015 .llseek = sysfs_dir_llseek,
1016};