blob: 09242aaa8829e4ca8031154f4ca0ee50f69b2462 [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 Heo3eef34a2014-02-07 13:32:07 -050022static DEFINE_SPINLOCK(kernfs_rename_lock); /* kn->parent and ->name */
23static char kernfs_pr_cont_buf[PATH_MAX]; /* protected by rename_lock */
Tejun Heofd7b9f72013-11-28 14:54:33 -050024
Tejun Heoadc5e8b2013-12-11 14:11:54 -050025#define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb)
Tejun Heofd7b9f72013-11-28 14:54:33 -050026
Tejun Heo81c173c2014-02-03 14:03:00 -050027static bool kernfs_active(struct kernfs_node *kn)
28{
29 lockdep_assert_held(&kernfs_mutex);
30 return atomic_read(&kn->active) >= 0;
31}
32
Tejun Heo182fd642014-02-03 14:02:59 -050033static bool kernfs_lockdep(struct kernfs_node *kn)
34{
35#ifdef CONFIG_DEBUG_LOCK_ALLOC
36 return kn->flags & KERNFS_LOCKDEP;
37#else
38 return false;
39#endif
40}
41
Tejun Heo3eef34a2014-02-07 13:32:07 -050042static int kernfs_name_locked(struct kernfs_node *kn, char *buf, size_t buflen)
43{
44 return strlcpy(buf, kn->parent ? kn->name : "/", buflen);
45}
46
Aditya Kali9f6df572016-01-29 02:54:04 -060047/* kernfs_node_depth - compute depth from @from to @to */
48static size_t kernfs_depth(struct kernfs_node *from, struct kernfs_node *to)
Tejun Heo3eef34a2014-02-07 13:32:07 -050049{
Aditya Kali9f6df572016-01-29 02:54:04 -060050 size_t depth = 0;
Tejun Heo3eef34a2014-02-07 13:32:07 -050051
Aditya Kali9f6df572016-01-29 02:54:04 -060052 while (to->parent && to != from) {
53 depth++;
54 to = to->parent;
55 }
56 return depth;
57}
Tejun Heo3eef34a2014-02-07 13:32:07 -050058
Aditya Kali9f6df572016-01-29 02:54:04 -060059static struct kernfs_node *kernfs_common_ancestor(struct kernfs_node *a,
60 struct kernfs_node *b)
61{
62 size_t da, db;
63 struct kernfs_root *ra = kernfs_root(a), *rb = kernfs_root(b);
Tejun Heo3eef34a2014-02-07 13:32:07 -050064
Aditya Kali9f6df572016-01-29 02:54:04 -060065 if (ra != rb)
66 return NULL;
67
68 da = kernfs_depth(ra->kn, a);
69 db = kernfs_depth(rb->kn, b);
70
71 while (da > db) {
72 a = a->parent;
73 da--;
74 }
75 while (db > da) {
76 b = b->parent;
77 db--;
78 }
79
80 /* worst case b and a will be the same at root */
81 while (b != a) {
82 b = b->parent;
83 a = a->parent;
84 }
85
86 return a;
87}
88
89/**
90 * kernfs_path_from_node_locked - find a pseudo-absolute path to @kn_to,
91 * where kn_from is treated as root of the path.
92 * @kn_from: kernfs node which should be treated as root for the path
93 * @kn_to: kernfs node to which path is needed
94 * @buf: buffer to copy the path into
95 * @buflen: size of @buf
96 *
97 * We need to handle couple of scenarios here:
98 * [1] when @kn_from is an ancestor of @kn_to at some level
99 * kn_from: /n1/n2/n3
100 * kn_to: /n1/n2/n3/n4/n5
101 * result: /n4/n5
102 *
103 * [2] when @kn_from is on a different hierarchy and we need to find common
104 * ancestor between @kn_from and @kn_to.
105 * kn_from: /n1/n2/n3/n4
106 * kn_to: /n1/n2/n5
107 * result: /../../n5
108 * OR
109 * kn_from: /n1/n2/n3/n4/n5 [depth=5]
110 * kn_to: /n1/n2/n3 [depth=3]
111 * result: /../..
112 *
Tejun Heo3abb1d92016-08-10 11:23:44 -0400113 * Returns the length of the full path. If the full length is equal to or
114 * greater than @buflen, @buf contains the truncated path with the trailing
115 * '\0'. On error, -errno is returned.
Aditya Kali9f6df572016-01-29 02:54:04 -0600116 */
117static int kernfs_path_from_node_locked(struct kernfs_node *kn_to,
118 struct kernfs_node *kn_from,
119 char *buf, size_t buflen)
120{
121 struct kernfs_node *kn, *common;
122 const char parent_str[] = "/..";
Tejun Heo3abb1d92016-08-10 11:23:44 -0400123 size_t depth_from, depth_to, len = 0;
124 int i, j;
Aditya Kali9f6df572016-01-29 02:54:04 -0600125
126 if (!kn_from)
127 kn_from = kernfs_root(kn_to)->kn;
128
129 if (kn_from == kn_to)
130 return strlcpy(buf, "/", buflen);
131
132 common = kernfs_common_ancestor(kn_from, kn_to);
133 if (WARN_ON(!common))
Tejun Heo3abb1d92016-08-10 11:23:44 -0400134 return -EINVAL;
Aditya Kali9f6df572016-01-29 02:54:04 -0600135
136 depth_to = kernfs_depth(common, kn_to);
137 depth_from = kernfs_depth(common, kn_from);
138
139 if (buf)
140 buf[0] = '\0';
141
142 for (i = 0; i < depth_from; i++)
143 len += strlcpy(buf + len, parent_str,
144 len < buflen ? buflen - len : 0);
145
146 /* Calculate how many bytes we need for the rest */
Tejun Heo3abb1d92016-08-10 11:23:44 -0400147 for (i = depth_to - 1; i >= 0; i--) {
148 for (kn = kn_to, j = 0; j < i; j++)
149 kn = kn->parent;
150 len += strlcpy(buf + len, "/",
151 len < buflen ? buflen - len : 0);
152 len += strlcpy(buf + len, kn->name,
153 len < buflen ? buflen - len : 0);
Aditya Kali9f6df572016-01-29 02:54:04 -0600154 }
155
Tejun Heo3abb1d92016-08-10 11:23:44 -0400156 return len;
Tejun Heo3eef34a2014-02-07 13:32:07 -0500157}
158
159/**
160 * kernfs_name - obtain the name of a given node
161 * @kn: kernfs_node of interest
162 * @buf: buffer to copy @kn's name into
163 * @buflen: size of @buf
164 *
165 * Copies the name of @kn into @buf of @buflen bytes. The behavior is
166 * similar to strlcpy(). It returns the length of @kn's name and if @buf
167 * isn't long enough, it's filled upto @buflen-1 and nul terminated.
168 *
169 * This function can be called from any context.
170 */
171int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen)
172{
173 unsigned long flags;
174 int ret;
175
176 spin_lock_irqsave(&kernfs_rename_lock, flags);
177 ret = kernfs_name_locked(kn, buf, buflen);
178 spin_unlock_irqrestore(&kernfs_rename_lock, flags);
179 return ret;
180}
181
182/**
Tejun Heo9acee9c2015-08-18 14:54:55 -0700183 * kernfs_path_len - determine the length of the full path of a given node
184 * @kn: kernfs_node of interest
185 *
186 * The returned length doesn't include the space for the terminating '\0'.
187 */
188size_t kernfs_path_len(struct kernfs_node *kn)
189{
190 size_t len = 0;
191 unsigned long flags;
192
193 spin_lock_irqsave(&kernfs_rename_lock, flags);
194
195 do {
196 len += strlen(kn->name) + 1;
197 kn = kn->parent;
198 } while (kn && kn->parent);
199
200 spin_unlock_irqrestore(&kernfs_rename_lock, flags);
201
202 return len;
203}
204
205/**
Aditya Kali9f6df572016-01-29 02:54:04 -0600206 * kernfs_path_from_node - build path of node @to relative to @from.
207 * @from: parent kernfs_node relative to which we need to build the path
208 * @to: kernfs_node of interest
209 * @buf: buffer to copy @to's path into
210 * @buflen: size of @buf
211 *
212 * Builds @to's path relative to @from in @buf. @from and @to must
213 * be on the same kernfs-root. If @from is not parent of @to, then a relative
214 * path (which includes '..'s) as needed to reach from @from to @to is
215 * returned.
216 *
Tejun Heo3abb1d92016-08-10 11:23:44 -0400217 * Returns the length of the full path. If the full length is equal to or
218 * greater than @buflen, @buf contains the truncated path with the trailing
219 * '\0'. On error, -errno is returned.
Aditya Kali9f6df572016-01-29 02:54:04 -0600220 */
221int kernfs_path_from_node(struct kernfs_node *to, struct kernfs_node *from,
222 char *buf, size_t buflen)
223{
224 unsigned long flags;
225 int ret;
226
227 spin_lock_irqsave(&kernfs_rename_lock, flags);
228 ret = kernfs_path_from_node_locked(to, from, buf, buflen);
229 spin_unlock_irqrestore(&kernfs_rename_lock, flags);
230 return ret;
231}
232EXPORT_SYMBOL_GPL(kernfs_path_from_node);
233
234/**
Tejun Heo3eef34a2014-02-07 13:32:07 -0500235 * pr_cont_kernfs_name - pr_cont name of a kernfs_node
236 * @kn: kernfs_node of interest
237 *
238 * This function can be called from any context.
239 */
240void pr_cont_kernfs_name(struct kernfs_node *kn)
241{
242 unsigned long flags;
243
244 spin_lock_irqsave(&kernfs_rename_lock, flags);
245
246 kernfs_name_locked(kn, kernfs_pr_cont_buf, sizeof(kernfs_pr_cont_buf));
247 pr_cont("%s", kernfs_pr_cont_buf);
248
249 spin_unlock_irqrestore(&kernfs_rename_lock, flags);
250}
251
252/**
253 * pr_cont_kernfs_path - pr_cont path of a kernfs_node
254 * @kn: kernfs_node of interest
255 *
256 * This function can be called from any context.
257 */
258void pr_cont_kernfs_path(struct kernfs_node *kn)
259{
260 unsigned long flags;
Aditya Kali9f6df572016-01-29 02:54:04 -0600261 int sz;
Tejun Heo3eef34a2014-02-07 13:32:07 -0500262
263 spin_lock_irqsave(&kernfs_rename_lock, flags);
264
Aditya Kali9f6df572016-01-29 02:54:04 -0600265 sz = kernfs_path_from_node_locked(kn, NULL, kernfs_pr_cont_buf,
266 sizeof(kernfs_pr_cont_buf));
267 if (sz < 0) {
268 pr_cont("(error)");
269 goto out;
270 }
Tejun Heo3eef34a2014-02-07 13:32:07 -0500271
Aditya Kali9f6df572016-01-29 02:54:04 -0600272 if (sz >= sizeof(kernfs_pr_cont_buf)) {
273 pr_cont("(name too long)");
274 goto out;
275 }
276
277 pr_cont("%s", kernfs_pr_cont_buf);
278
279out:
Tejun Heo3eef34a2014-02-07 13:32:07 -0500280 spin_unlock_irqrestore(&kernfs_rename_lock, flags);
281}
282
283/**
284 * kernfs_get_parent - determine the parent node and pin it
285 * @kn: kernfs_node of interest
286 *
287 * Determines @kn's parent, pins and returns it. This function can be
288 * called from any context.
289 */
290struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn)
291{
292 struct kernfs_node *parent;
293 unsigned long flags;
294
295 spin_lock_irqsave(&kernfs_rename_lock, flags);
296 parent = kn->parent;
297 kernfs_get(parent);
298 spin_unlock_irqrestore(&kernfs_rename_lock, flags);
299
300 return parent;
301}
302
Tejun Heofd7b9f72013-11-28 14:54:33 -0500303/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500304 * kernfs_name_hash
Tejun Heofd7b9f72013-11-28 14:54:33 -0500305 * @name: Null terminated string to hash
306 * @ns: Namespace tag to hash
307 *
308 * Returns 31 bit hash of ns + name (so it fits in an off_t )
309 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500310static unsigned int kernfs_name_hash(const char *name, const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500311{
Linus Torvalds8387ff22016-06-10 07:51:30 -0700312 unsigned long hash = init_name_hash(ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500313 unsigned int len = strlen(name);
314 while (len--)
315 hash = partial_name_hash(*name++, hash);
Linus Torvalds8387ff22016-06-10 07:51:30 -0700316 hash = end_name_hash(hash);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500317 hash &= 0x7fffffffU;
318 /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */
Richard Cochran88391d42014-03-05 17:10:52 +0100319 if (hash < 2)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500320 hash += 2;
321 if (hash >= INT_MAX)
322 hash = INT_MAX - 1;
323 return hash;
324}
325
Tejun Heoc637b8a2013-12-11 14:11:58 -0500326static int kernfs_name_compare(unsigned int hash, const char *name,
327 const void *ns, const struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500328{
Rasmus Villemoes72392ed2014-12-05 23:41:33 +0100329 if (hash < kn->hash)
330 return -1;
331 if (hash > kn->hash)
332 return 1;
333 if (ns < kn->ns)
334 return -1;
335 if (ns > kn->ns)
336 return 1;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500337 return strcmp(name, kn->name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500338}
339
Tejun Heoc637b8a2013-12-11 14:11:58 -0500340static int kernfs_sd_compare(const struct kernfs_node *left,
341 const struct kernfs_node *right)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500342{
Tejun Heoc637b8a2013-12-11 14:11:58 -0500343 return kernfs_name_compare(left->hash, left->name, left->ns, right);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500344}
345
346/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500347 * kernfs_link_sibling - link kernfs_node into sibling rbtree
Tejun Heo324a56e2013-12-11 14:11:53 -0500348 * @kn: kernfs_node of interest
Tejun Heofd7b9f72013-11-28 14:54:33 -0500349 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500350 * Link @kn into its sibling rbtree which starts from
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500351 * @kn->parent->dir.children.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500352 *
353 * Locking:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500354 * mutex_lock(kernfs_mutex)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500355 *
356 * RETURNS:
357 * 0 on susccess -EEXIST on failure.
358 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500359static int kernfs_link_sibling(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500360{
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500361 struct rb_node **node = &kn->parent->dir.children.rb_node;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500362 struct rb_node *parent = NULL;
363
Tejun Heofd7b9f72013-11-28 14:54:33 -0500364 while (*node) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500365 struct kernfs_node *pos;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500366 int result;
367
Tejun Heo324a56e2013-12-11 14:11:53 -0500368 pos = rb_to_kn(*node);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500369 parent = *node;
Tejun Heoc637b8a2013-12-11 14:11:58 -0500370 result = kernfs_sd_compare(kn, pos);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500371 if (result < 0)
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500372 node = &pos->rb.rb_left;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500373 else if (result > 0)
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500374 node = &pos->rb.rb_right;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500375 else
376 return -EEXIST;
377 }
Jianyu Zhanc1befb82014-04-17 17:52:10 +0800378
Tejun Heofd7b9f72013-11-28 14:54:33 -0500379 /* add new node and rebalance the tree */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500380 rb_link_node(&kn->rb, parent, node);
381 rb_insert_color(&kn->rb, &kn->parent->dir.children);
Jianyu Zhanc1befb82014-04-17 17:52:10 +0800382
383 /* successfully added, account subdir number */
384 if (kernfs_type(kn) == KERNFS_DIR)
385 kn->parent->dir.subdirs++;
386
Tejun Heofd7b9f72013-11-28 14:54:33 -0500387 return 0;
388}
389
390/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500391 * kernfs_unlink_sibling - unlink kernfs_node from sibling rbtree
Tejun Heo324a56e2013-12-11 14:11:53 -0500392 * @kn: kernfs_node of interest
Tejun Heofd7b9f72013-11-28 14:54:33 -0500393 *
Tejun Heo35beab02014-02-03 14:02:56 -0500394 * Try to unlink @kn from its sibling rbtree which starts from
395 * kn->parent->dir.children. Returns %true if @kn was actually
396 * removed, %false if @kn wasn't on the rbtree.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500397 *
398 * Locking:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500399 * mutex_lock(kernfs_mutex)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500400 */
Tejun Heo35beab02014-02-03 14:02:56 -0500401static bool kernfs_unlink_sibling(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500402{
Tejun Heo35beab02014-02-03 14:02:56 -0500403 if (RB_EMPTY_NODE(&kn->rb))
404 return false;
405
Tejun Heodf23fc32013-12-11 14:11:56 -0500406 if (kernfs_type(kn) == KERNFS_DIR)
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500407 kn->parent->dir.subdirs--;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500408
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500409 rb_erase(&kn->rb, &kn->parent->dir.children);
Tejun Heo35beab02014-02-03 14:02:56 -0500410 RB_CLEAR_NODE(&kn->rb);
411 return true;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500412}
413
414/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500415 * kernfs_get_active - get an active reference to kernfs_node
Tejun Heo324a56e2013-12-11 14:11:53 -0500416 * @kn: kernfs_node to get an active reference to
Tejun Heofd7b9f72013-11-28 14:54:33 -0500417 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500418 * Get an active reference of @kn. This function is noop if @kn
Tejun Heofd7b9f72013-11-28 14:54:33 -0500419 * is NULL.
420 *
421 * RETURNS:
Tejun Heo324a56e2013-12-11 14:11:53 -0500422 * Pointer to @kn on success, NULL on failure.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500423 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500424struct kernfs_node *kernfs_get_active(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500425{
Tejun Heo324a56e2013-12-11 14:11:53 -0500426 if (unlikely(!kn))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500427 return NULL;
428
Greg Kroah-Hartmanf4b3e632014-01-13 14:13:39 -0800429 if (!atomic_inc_unless_negative(&kn->active))
430 return NULL;
431
Tejun Heo182fd642014-02-03 14:02:59 -0500432 if (kernfs_lockdep(kn))
Tejun Heo324a56e2013-12-11 14:11:53 -0500433 rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_);
Greg Kroah-Hartmanf4b3e632014-01-13 14:13:39 -0800434 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500435}
436
437/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500438 * kernfs_put_active - put an active reference to kernfs_node
Tejun Heo324a56e2013-12-11 14:11:53 -0500439 * @kn: kernfs_node to put an active reference to
Tejun Heofd7b9f72013-11-28 14:54:33 -0500440 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500441 * Put an active reference to @kn. This function is noop if @kn
Tejun Heofd7b9f72013-11-28 14:54:33 -0500442 * is NULL.
443 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500444void kernfs_put_active(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500445{
Tejun Heoabd54f02014-02-03 14:02:55 -0500446 struct kernfs_root *root = kernfs_root(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500447 int v;
448
Tejun Heo324a56e2013-12-11 14:11:53 -0500449 if (unlikely(!kn))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500450 return;
451
Tejun Heo182fd642014-02-03 14:02:59 -0500452 if (kernfs_lockdep(kn))
Tejun Heo324a56e2013-12-11 14:11:53 -0500453 rwsem_release(&kn->dep_map, 1, _RET_IP_);
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500454 v = atomic_dec_return(&kn->active);
Tejun Heodf23fc32013-12-11 14:11:56 -0500455 if (likely(v != KN_DEACTIVATED_BIAS))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500456 return;
457
Tejun Heoabd54f02014-02-03 14:02:55 -0500458 wake_up_all(&root->deactivate_waitq);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500459}
460
461/**
Tejun Heo81c173c2014-02-03 14:03:00 -0500462 * kernfs_drain - drain kernfs_node
463 * @kn: kernfs_node to drain
Tejun Heofd7b9f72013-11-28 14:54:33 -0500464 *
Tejun Heo81c173c2014-02-03 14:03:00 -0500465 * Drain existing usages and nuke all existing mmaps of @kn. Mutiple
466 * removers may invoke this function concurrently on @kn and all will
467 * return after draining is complete.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500468 */
Tejun Heo81c173c2014-02-03 14:03:00 -0500469static void kernfs_drain(struct kernfs_node *kn)
Tejun Heo35beab02014-02-03 14:02:56 -0500470 __releases(&kernfs_mutex) __acquires(&kernfs_mutex)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500471{
Tejun Heoabd54f02014-02-03 14:02:55 -0500472 struct kernfs_root *root = kernfs_root(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500473
Tejun Heo35beab02014-02-03 14:02:56 -0500474 lockdep_assert_held(&kernfs_mutex);
Tejun Heo81c173c2014-02-03 14:03:00 -0500475 WARN_ON_ONCE(kernfs_active(kn));
Tejun Heo35beab02014-02-03 14:02:56 -0500476
477 mutex_unlock(&kernfs_mutex);
478
Tejun Heo182fd642014-02-03 14:02:59 -0500479 if (kernfs_lockdep(kn)) {
Tejun Heoa6607932014-02-03 14:02:54 -0500480 rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_);
Tejun Heo35beab02014-02-03 14:02:56 -0500481 if (atomic_read(&kn->active) != KN_DEACTIVATED_BIAS)
482 lock_contended(&kn->dep_map, _RET_IP_);
483 }
Greg Kroah-Hartman08901472014-01-13 14:39:52 -0800484
Tejun Heo35beab02014-02-03 14:02:56 -0500485 /* but everyone should wait for draining */
Tejun Heoabd54f02014-02-03 14:02:55 -0500486 wait_event(root->deactivate_waitq,
487 atomic_read(&kn->active) == KN_DEACTIVATED_BIAS);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500488
Tejun Heo182fd642014-02-03 14:02:59 -0500489 if (kernfs_lockdep(kn)) {
Tejun Heoa6607932014-02-03 14:02:54 -0500490 lock_acquired(&kn->dep_map, _RET_IP_);
491 rwsem_release(&kn->dep_map, 1, _RET_IP_);
492 }
Tejun Heo35beab02014-02-03 14:02:56 -0500493
Tejun Heoccf02aa2014-02-03 14:02:57 -0500494 kernfs_unmap_bin_file(kn);
495
Tejun Heo35beab02014-02-03 14:02:56 -0500496 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500497}
498
Tejun Heofd7b9f72013-11-28 14:54:33 -0500499/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500500 * kernfs_get - get a reference count on a kernfs_node
501 * @kn: the target kernfs_node
Tejun Heofd7b9f72013-11-28 14:54:33 -0500502 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500503void kernfs_get(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500504{
Tejun Heo324a56e2013-12-11 14:11:53 -0500505 if (kn) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500506 WARN_ON(!atomic_read(&kn->count));
507 atomic_inc(&kn->count);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500508 }
509}
510EXPORT_SYMBOL_GPL(kernfs_get);
511
512/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500513 * kernfs_put - put a reference count on a kernfs_node
514 * @kn: the target kernfs_node
Tejun Heofd7b9f72013-11-28 14:54:33 -0500515 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500516 * Put a reference count of @kn and destroy it if it reached zero.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500517 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500518void kernfs_put(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500519{
Tejun Heo324a56e2013-12-11 14:11:53 -0500520 struct kernfs_node *parent;
Tejun Heoba7443b2013-11-28 14:54:40 -0500521 struct kernfs_root *root;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500522
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500523 if (!kn || !atomic_dec_and_test(&kn->count))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500524 return;
Tejun Heo324a56e2013-12-11 14:11:53 -0500525 root = kernfs_root(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500526 repeat:
Tejun Heo81c173c2014-02-03 14:03:00 -0500527 /*
528 * Moving/renaming is always done while holding reference.
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500529 * kn->parent won't change beneath us.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500530 */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500531 parent = kn->parent;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500532
Tejun Heo81c173c2014-02-03 14:03:00 -0500533 WARN_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS,
534 "kernfs_put: %s/%s: released with incorrect active_ref %d\n",
535 parent ? parent->name : "", kn->name, atomic_read(&kn->active));
Tejun Heofd7b9f72013-11-28 14:54:33 -0500536
Tejun Heodf23fc32013-12-11 14:11:56 -0500537 if (kernfs_type(kn) == KERNFS_LINK)
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500538 kernfs_put(kn->symlink.target_kn);
Tejun Heodfeb07502015-02-13 14:36:31 -0800539
540 kfree_const(kn->name);
541
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500542 if (kn->iattr) {
543 if (kn->iattr->ia_secdata)
544 security_release_secctx(kn->iattr->ia_secdata,
545 kn->iattr->ia_secdata_len);
546 simple_xattrs_free(&kn->iattr->xattrs);
Tejun Heo23223922013-11-23 17:40:02 -0500547 }
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500548 kfree(kn->iattr);
549 ida_simple_remove(&root->ino_ida, kn->ino);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500550 kmem_cache_free(kernfs_node_cache, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500551
Tejun Heo324a56e2013-12-11 14:11:53 -0500552 kn = parent;
553 if (kn) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500554 if (atomic_dec_and_test(&kn->count))
Tejun Heoba7443b2013-11-28 14:54:40 -0500555 goto repeat;
556 } else {
Tejun Heo324a56e2013-12-11 14:11:53 -0500557 /* just released the root kn, free @root too */
Tejun Heobc755552013-11-28 14:54:41 -0500558 ida_destroy(&root->ino_ida);
Tejun Heoba7443b2013-11-28 14:54:40 -0500559 kfree(root);
560 }
Tejun Heofd7b9f72013-11-28 14:54:33 -0500561}
562EXPORT_SYMBOL_GPL(kernfs_put);
563
Tejun Heoc637b8a2013-12-11 14:11:58 -0500564static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500565{
Tejun Heo324a56e2013-12-11 14:11:53 -0500566 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500567
568 if (flags & LOOKUP_RCU)
569 return -ECHILD;
570
Tejun Heo19bbb922013-12-11 16:02:59 -0500571 /* Always perform fresh lookup for negatives */
David Howells2b0143b2015-03-17 22:25:59 +0000572 if (d_really_is_negative(dentry))
Tejun Heo19bbb922013-12-11 16:02:59 -0500573 goto out_bad_unlocked;
574
Tejun Heo324a56e2013-12-11 14:11:53 -0500575 kn = dentry->d_fsdata;
Tejun Heoa797bfc2013-12-11 14:11:57 -0500576 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500577
Tejun Heo81c173c2014-02-03 14:03:00 -0500578 /* The kernfs node has been deactivated */
579 if (!kernfs_active(kn))
Tejun Heofd7b9f72013-11-28 14:54:33 -0500580 goto out_bad;
581
Tejun Heoc637b8a2013-12-11 14:11:58 -0500582 /* The kernfs node has been moved? */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500583 if (dentry->d_parent->d_fsdata != kn->parent)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500584 goto out_bad;
585
Tejun Heoc637b8a2013-12-11 14:11:58 -0500586 /* The kernfs node has been renamed */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500587 if (strcmp(dentry->d_name.name, kn->name) != 0)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500588 goto out_bad;
589
Tejun Heoc637b8a2013-12-11 14:11:58 -0500590 /* The kernfs node has been moved to a different namespace */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500591 if (kn->parent && kernfs_ns_enabled(kn->parent) &&
Tejun Heoc525aad2013-12-11 14:11:55 -0500592 kernfs_info(dentry->d_sb)->ns != kn->ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500593 goto out_bad;
594
Tejun Heoa797bfc2013-12-11 14:11:57 -0500595 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500596 return 1;
597out_bad:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500598 mutex_unlock(&kernfs_mutex);
Tejun Heo19bbb922013-12-11 16:02:59 -0500599out_bad_unlocked:
Tejun Heofd7b9f72013-11-28 14:54:33 -0500600 return 0;
601}
602
Tejun Heoc637b8a2013-12-11 14:11:58 -0500603static void kernfs_dop_release(struct dentry *dentry)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500604{
605 kernfs_put(dentry->d_fsdata);
606}
607
Tejun Heoa797bfc2013-12-11 14:11:57 -0500608const struct dentry_operations kernfs_dops = {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500609 .d_revalidate = kernfs_dop_revalidate,
Tejun Heoc637b8a2013-12-11 14:11:58 -0500610 .d_release = kernfs_dop_release,
Tejun Heofd7b9f72013-11-28 14:54:33 -0500611};
612
Tejun Heo0c23b222014-02-03 14:09:15 -0500613/**
614 * kernfs_node_from_dentry - determine kernfs_node associated with a dentry
615 * @dentry: the dentry in question
616 *
617 * Return the kernfs_node associated with @dentry. If @dentry is not a
618 * kernfs one, %NULL is returned.
619 *
620 * While the returned kernfs_node will stay accessible as long as @dentry
621 * is accessible, the returned node can be in any state and the caller is
622 * fully responsible for determining what's accessible.
623 */
624struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry)
625{
Li Zefanf41c5932014-02-14 16:57:27 +0800626 if (dentry->d_sb->s_op == &kernfs_sops)
Tejun Heo0c23b222014-02-03 14:09:15 -0500627 return dentry->d_fsdata;
628 return NULL;
629}
630
Tejun Heodb4aad22014-01-17 09:58:25 -0500631static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
632 const char *name, umode_t mode,
633 unsigned flags)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500634{
Tejun Heo324a56e2013-12-11 14:11:53 -0500635 struct kernfs_node *kn;
Tejun Heobc755552013-11-28 14:54:41 -0500636 int ret;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500637
Tejun Heodfeb07502015-02-13 14:36:31 -0800638 name = kstrdup_const(name, GFP_KERNEL);
639 if (!name)
640 return NULL;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500641
Tejun Heoa797bfc2013-12-11 14:11:57 -0500642 kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL);
Tejun Heo324a56e2013-12-11 14:11:53 -0500643 if (!kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500644 goto err_out1;
645
Vladimir Davydovb2a209f2016-01-14 15:18:05 -0800646 ret = ida_simple_get(&root->ino_ida, 1, 0, GFP_KERNEL);
Tejun Heobc755552013-11-28 14:54:41 -0500647 if (ret < 0)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500648 goto err_out2;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500649 kn->ino = ret;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500650
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500651 atomic_set(&kn->count, 1);
Tejun Heo81c173c2014-02-03 14:03:00 -0500652 atomic_set(&kn->active, KN_DEACTIVATED_BIAS);
Tejun Heo35beab02014-02-03 14:02:56 -0500653 RB_CLEAR_NODE(&kn->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500654
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500655 kn->name = name;
656 kn->mode = mode;
Tejun Heo81c173c2014-02-03 14:03:00 -0500657 kn->flags = flags;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500658
Tejun Heo324a56e2013-12-11 14:11:53 -0500659 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500660
661 err_out2:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500662 kmem_cache_free(kernfs_node_cache, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500663 err_out1:
Tejun Heodfeb07502015-02-13 14:36:31 -0800664 kfree_const(name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500665 return NULL;
666}
667
Tejun Heodb4aad22014-01-17 09:58:25 -0500668struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
669 const char *name, umode_t mode,
670 unsigned flags)
671{
672 struct kernfs_node *kn;
673
674 kn = __kernfs_new_node(kernfs_root(parent), name, mode, flags);
675 if (kn) {
676 kernfs_get(parent);
677 kn->parent = parent;
678 }
679 return kn;
680}
681
Tejun Heofd7b9f72013-11-28 14:54:33 -0500682/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500683 * kernfs_add_one - add kernfs_node to parent without warning
Tejun Heo324a56e2013-12-11 14:11:53 -0500684 * @kn: kernfs_node to be added
Tejun Heofd7b9f72013-11-28 14:54:33 -0500685 *
Tejun Heodb4aad22014-01-17 09:58:25 -0500686 * The caller must already have initialized @kn->parent. This
687 * function increments nlink of the parent's inode if @kn is a
688 * directory and link into the children list of the parent.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500689 *
Tejun Heofd7b9f72013-11-28 14:54:33 -0500690 * RETURNS:
691 * 0 on success, -EEXIST if entry with the given name already
692 * exists.
693 */
Tejun Heo988cd7a2014-02-03 14:02:58 -0500694int kernfs_add_one(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500695{
Tejun Heodb4aad22014-01-17 09:58:25 -0500696 struct kernfs_node *parent = kn->parent;
Tejun Heoc525aad2013-12-11 14:11:55 -0500697 struct kernfs_iattrs *ps_iattr;
Tejun Heo988cd7a2014-02-03 14:02:58 -0500698 bool has_ns;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500699 int ret;
700
Tejun Heo988cd7a2014-02-03 14:02:58 -0500701 mutex_lock(&kernfs_mutex);
702
703 ret = -EINVAL;
704 has_ns = kernfs_ns_enabled(parent);
705 if (WARN(has_ns != (bool)kn->ns, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
706 has_ns ? "required" : "invalid", parent->name, kn->name))
707 goto out_unlock;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500708
Tejun Heodf23fc32013-12-11 14:11:56 -0500709 if (kernfs_type(parent) != KERNFS_DIR)
Tejun Heo988cd7a2014-02-03 14:02:58 -0500710 goto out_unlock;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500711
Tejun Heo988cd7a2014-02-03 14:02:58 -0500712 ret = -ENOENT;
Eric W. Biedermanea015212015-05-13 16:09:29 -0500713 if (parent->flags & KERNFS_EMPTY_DIR)
714 goto out_unlock;
715
Tejun Heod35258e2014-02-03 14:09:12 -0500716 if ((parent->flags & KERNFS_ACTIVATED) && !kernfs_active(parent))
Tejun Heo988cd7a2014-02-03 14:02:58 -0500717 goto out_unlock;
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -0800718
Tejun Heoc637b8a2013-12-11 14:11:58 -0500719 kn->hash = kernfs_name_hash(kn->name, kn->ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500720
Tejun Heoc637b8a2013-12-11 14:11:58 -0500721 ret = kernfs_link_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500722 if (ret)
Tejun Heo988cd7a2014-02-03 14:02:58 -0500723 goto out_unlock;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500724
725 /* Update timestamps on the parent */
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500726 ps_iattr = parent->iattr;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500727 if (ps_iattr) {
728 struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
Deepa Dinamani3a3a5fe2016-02-22 07:17:53 -0800729 ktime_get_real_ts(&ps_iattrs->ia_ctime);
730 ps_iattrs->ia_mtime = ps_iattrs->ia_ctime;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500731 }
732
Tejun Heod35258e2014-02-03 14:09:12 -0500733 mutex_unlock(&kernfs_mutex);
734
735 /*
736 * Activate the new node unless CREATE_DEACTIVATED is requested.
737 * If not activated here, the kernfs user is responsible for
738 * activating the node with kernfs_activate(). A node which hasn't
739 * been activated is not visible to userland and its removal won't
740 * trigger deactivation.
741 */
742 if (!(kernfs_root(kn)->flags & KERNFS_ROOT_CREATE_DEACTIVATED))
743 kernfs_activate(kn);
744 return 0;
745
Tejun Heo988cd7a2014-02-03 14:02:58 -0500746out_unlock:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500747 mutex_unlock(&kernfs_mutex);
Tejun Heo988cd7a2014-02-03 14:02:58 -0500748 return ret;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500749}
750
751/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500752 * kernfs_find_ns - find kernfs_node with the given name
753 * @parent: kernfs_node to search under
Tejun Heofd7b9f72013-11-28 14:54:33 -0500754 * @name: name to look for
755 * @ns: the namespace tag to use
756 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500757 * Look for kernfs_node with name @name under @parent. Returns pointer to
758 * the found kernfs_node on success, %NULL on failure.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500759 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500760static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
761 const unsigned char *name,
762 const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500763{
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500764 struct rb_node *node = parent->dir.children.rb_node;
Tejun Heoac9bba02013-11-29 17:19:09 -0500765 bool has_ns = kernfs_ns_enabled(parent);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500766 unsigned int hash;
767
Tejun Heoa797bfc2013-12-11 14:11:57 -0500768 lockdep_assert_held(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500769
770 if (has_ns != (bool)ns) {
Tejun Heoc637b8a2013-12-11 14:11:58 -0500771 WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500772 has_ns ? "required" : "invalid", parent->name, name);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500773 return NULL;
774 }
775
Tejun Heoc637b8a2013-12-11 14:11:58 -0500776 hash = kernfs_name_hash(name, ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500777 while (node) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500778 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500779 int result;
780
Tejun Heo324a56e2013-12-11 14:11:53 -0500781 kn = rb_to_kn(node);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500782 result = kernfs_name_compare(hash, name, ns, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500783 if (result < 0)
784 node = node->rb_left;
785 else if (result > 0)
786 node = node->rb_right;
787 else
Tejun Heo324a56e2013-12-11 14:11:53 -0500788 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500789 }
790 return NULL;
791}
792
Tejun Heobd96f762015-11-20 15:55:52 -0500793static struct kernfs_node *kernfs_walk_ns(struct kernfs_node *parent,
794 const unsigned char *path,
795 const void *ns)
796{
Tejun Heoe56ed3582016-01-15 12:30:14 -0500797 size_t len;
798 char *p, *name;
Tejun Heobd96f762015-11-20 15:55:52 -0500799
800 lockdep_assert_held(&kernfs_mutex);
801
Tejun Heoe56ed3582016-01-15 12:30:14 -0500802 /* grab kernfs_rename_lock to piggy back on kernfs_pr_cont_buf */
803 spin_lock_irq(&kernfs_rename_lock);
804
805 len = strlcpy(kernfs_pr_cont_buf, path, sizeof(kernfs_pr_cont_buf));
806
807 if (len >= sizeof(kernfs_pr_cont_buf)) {
808 spin_unlock_irq(&kernfs_rename_lock);
Tejun Heobd96f762015-11-20 15:55:52 -0500809 return NULL;
Tejun Heoe56ed3582016-01-15 12:30:14 -0500810 }
811
812 p = kernfs_pr_cont_buf;
Tejun Heobd96f762015-11-20 15:55:52 -0500813
814 while ((name = strsep(&p, "/")) && parent) {
815 if (*name == '\0')
816 continue;
817 parent = kernfs_find_ns(parent, name, ns);
818 }
819
Tejun Heoe56ed3582016-01-15 12:30:14 -0500820 spin_unlock_irq(&kernfs_rename_lock);
821
Tejun Heobd96f762015-11-20 15:55:52 -0500822 return parent;
823}
824
Tejun Heofd7b9f72013-11-28 14:54:33 -0500825/**
Tejun Heo324a56e2013-12-11 14:11:53 -0500826 * kernfs_find_and_get_ns - find and get kernfs_node with the given name
827 * @parent: kernfs_node to search under
Tejun Heofd7b9f72013-11-28 14:54:33 -0500828 * @name: name to look for
829 * @ns: the namespace tag to use
830 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500831 * Look for kernfs_node with name @name under @parent and get a reference
Tejun Heofd7b9f72013-11-28 14:54:33 -0500832 * if found. This function may sleep and returns pointer to the found
Tejun Heo324a56e2013-12-11 14:11:53 -0500833 * kernfs_node on success, %NULL on failure.
Tejun Heofd7b9f72013-11-28 14:54:33 -0500834 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500835struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
836 const char *name, const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500837{
Tejun Heo324a56e2013-12-11 14:11:53 -0500838 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500839
Tejun Heoa797bfc2013-12-11 14:11:57 -0500840 mutex_lock(&kernfs_mutex);
Tejun Heo324a56e2013-12-11 14:11:53 -0500841 kn = kernfs_find_ns(parent, name, ns);
842 kernfs_get(kn);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500843 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500844
Tejun Heo324a56e2013-12-11 14:11:53 -0500845 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500846}
847EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
848
849/**
Tejun Heobd96f762015-11-20 15:55:52 -0500850 * kernfs_walk_and_get_ns - find and get kernfs_node with the given path
851 * @parent: kernfs_node to search under
852 * @path: path to look for
853 * @ns: the namespace tag to use
854 *
855 * Look for kernfs_node with path @path under @parent and get a reference
856 * if found. This function may sleep and returns pointer to the found
857 * kernfs_node on success, %NULL on failure.
858 */
859struct kernfs_node *kernfs_walk_and_get_ns(struct kernfs_node *parent,
860 const char *path, const void *ns)
861{
862 struct kernfs_node *kn;
863
864 mutex_lock(&kernfs_mutex);
865 kn = kernfs_walk_ns(parent, path, ns);
866 kernfs_get(kn);
867 mutex_unlock(&kernfs_mutex);
868
869 return kn;
870}
871
872/**
Tejun Heoba7443b2013-11-28 14:54:40 -0500873 * kernfs_create_root - create a new kernfs hierarchy
Tejun Heo90c07c82014-02-03 14:09:09 -0500874 * @scops: optional syscall operations for the hierarchy
Tejun Heod35258e2014-02-03 14:09:12 -0500875 * @flags: KERNFS_ROOT_* flags
Tejun Heoba7443b2013-11-28 14:54:40 -0500876 * @priv: opaque data associated with the new directory
877 *
878 * Returns the root of the new hierarchy on success, ERR_PTR() value on
879 * failure.
880 */
Tejun Heo90c07c82014-02-03 14:09:09 -0500881struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
Tejun Heod35258e2014-02-03 14:09:12 -0500882 unsigned int flags, void *priv)
Tejun Heoba7443b2013-11-28 14:54:40 -0500883{
884 struct kernfs_root *root;
Tejun Heo324a56e2013-12-11 14:11:53 -0500885 struct kernfs_node *kn;
Tejun Heoba7443b2013-11-28 14:54:40 -0500886
887 root = kzalloc(sizeof(*root), GFP_KERNEL);
888 if (!root)
889 return ERR_PTR(-ENOMEM);
890
Tejun Heobc755552013-11-28 14:54:41 -0500891 ida_init(&root->ino_ida);
Tejun Heo7d568a82014-04-09 11:07:30 -0400892 INIT_LIST_HEAD(&root->supers);
Tejun Heobc755552013-11-28 14:54:41 -0500893
Tejun Heodb4aad22014-01-17 09:58:25 -0500894 kn = __kernfs_new_node(root, "", S_IFDIR | S_IRUGO | S_IXUGO,
895 KERNFS_DIR);
Tejun Heo324a56e2013-12-11 14:11:53 -0500896 if (!kn) {
Tejun Heobc755552013-11-28 14:54:41 -0500897 ida_destroy(&root->ino_ida);
Tejun Heoba7443b2013-11-28 14:54:40 -0500898 kfree(root);
899 return ERR_PTR(-ENOMEM);
900 }
901
Tejun Heo324a56e2013-12-11 14:11:53 -0500902 kn->priv = priv;
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500903 kn->dir.root = root;
Tejun Heoba7443b2013-11-28 14:54:40 -0500904
Tejun Heo90c07c82014-02-03 14:09:09 -0500905 root->syscall_ops = scops;
Tejun Heod35258e2014-02-03 14:09:12 -0500906 root->flags = flags;
Tejun Heo324a56e2013-12-11 14:11:53 -0500907 root->kn = kn;
Tejun Heoabd54f02014-02-03 14:02:55 -0500908 init_waitqueue_head(&root->deactivate_waitq);
Tejun Heoba7443b2013-11-28 14:54:40 -0500909
Tejun Heod35258e2014-02-03 14:09:12 -0500910 if (!(root->flags & KERNFS_ROOT_CREATE_DEACTIVATED))
911 kernfs_activate(kn);
912
Tejun Heoba7443b2013-11-28 14:54:40 -0500913 return root;
914}
915
916/**
917 * kernfs_destroy_root - destroy a kernfs hierarchy
918 * @root: root of the hierarchy to destroy
919 *
920 * Destroy the hierarchy anchored at @root by removing all existing
921 * directories and destroying @root.
922 */
923void kernfs_destroy_root(struct kernfs_root *root)
924{
Tejun Heo324a56e2013-12-11 14:11:53 -0500925 kernfs_remove(root->kn); /* will also free @root */
Tejun Heoba7443b2013-11-28 14:54:40 -0500926}
927
928/**
Tejun Heofd7b9f72013-11-28 14:54:33 -0500929 * kernfs_create_dir_ns - create a directory
930 * @parent: parent in which to create a new directory
931 * @name: name of the new directory
Tejun Heobb8b9d02013-12-11 16:02:55 -0500932 * @mode: mode of the new directory
Tejun Heofd7b9f72013-11-28 14:54:33 -0500933 * @priv: opaque data associated with the new directory
934 * @ns: optional namespace tag of the directory
935 *
936 * Returns the created node on success, ERR_PTR() value on failure.
937 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500938struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
Tejun Heobb8b9d02013-12-11 16:02:55 -0500939 const char *name, umode_t mode,
940 void *priv, const void *ns)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500941{
Tejun Heo324a56e2013-12-11 14:11:53 -0500942 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500943 int rc;
944
945 /* allocate */
Tejun Heodb4aad22014-01-17 09:58:25 -0500946 kn = kernfs_new_node(parent, name, mode | S_IFDIR, KERNFS_DIR);
Tejun Heo324a56e2013-12-11 14:11:53 -0500947 if (!kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500948 return ERR_PTR(-ENOMEM);
949
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500950 kn->dir.root = parent->dir.root;
951 kn->ns = ns;
Tejun Heo324a56e2013-12-11 14:11:53 -0500952 kn->priv = priv;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500953
954 /* link in */
Tejun Heo988cd7a2014-02-03 14:02:58 -0500955 rc = kernfs_add_one(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500956 if (!rc)
Tejun Heo324a56e2013-12-11 14:11:53 -0500957 return kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -0500958
Tejun Heo324a56e2013-12-11 14:11:53 -0500959 kernfs_put(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -0500960 return ERR_PTR(rc);
961}
962
Eric W. Biedermanea015212015-05-13 16:09:29 -0500963/**
964 * kernfs_create_empty_dir - create an always empty directory
965 * @parent: parent in which to create a new directory
966 * @name: name of the new directory
967 *
968 * Returns the created node on success, ERR_PTR() value on failure.
969 */
970struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent,
971 const char *name)
972{
973 struct kernfs_node *kn;
974 int rc;
975
976 /* allocate */
977 kn = kernfs_new_node(parent, name, S_IRUGO|S_IXUGO|S_IFDIR, KERNFS_DIR);
978 if (!kn)
979 return ERR_PTR(-ENOMEM);
980
981 kn->flags |= KERNFS_EMPTY_DIR;
982 kn->dir.root = parent->dir.root;
983 kn->ns = NULL;
984 kn->priv = NULL;
985
986 /* link in */
987 rc = kernfs_add_one(kn);
988 if (!rc)
989 return kn;
990
991 kernfs_put(kn);
992 return ERR_PTR(rc);
993}
994
Tejun Heoc637b8a2013-12-11 14:11:58 -0500995static struct dentry *kernfs_iop_lookup(struct inode *dir,
996 struct dentry *dentry,
997 unsigned int flags)
Tejun Heofd7b9f72013-11-28 14:54:33 -0500998{
Tejun Heo19bbb922013-12-11 16:02:59 -0500999 struct dentry *ret;
Tejun Heo324a56e2013-12-11 14:11:53 -05001000 struct kernfs_node *parent = dentry->d_parent->d_fsdata;
1001 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001002 struct inode *inode;
1003 const void *ns = NULL;
1004
Tejun Heoa797bfc2013-12-11 14:11:57 -05001005 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001006
Tejun Heo324a56e2013-12-11 14:11:53 -05001007 if (kernfs_ns_enabled(parent))
Tejun Heoc525aad2013-12-11 14:11:55 -05001008 ns = kernfs_info(dir->i_sb)->ns;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001009
Tejun Heo324a56e2013-12-11 14:11:53 -05001010 kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001011
1012 /* no such entry */
Tejun Heob9c9dad2014-02-03 14:09:11 -05001013 if (!kn || !kernfs_active(kn)) {
Tejun Heo19bbb922013-12-11 16:02:59 -05001014 ret = NULL;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001015 goto out_unlock;
1016 }
Tejun Heo324a56e2013-12-11 14:11:53 -05001017 kernfs_get(kn);
1018 dentry->d_fsdata = kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001019
1020 /* attach dentry and inode */
Tejun Heoc637b8a2013-12-11 14:11:58 -05001021 inode = kernfs_get_inode(dir->i_sb, kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001022 if (!inode) {
1023 ret = ERR_PTR(-ENOMEM);
1024 goto out_unlock;
1025 }
1026
1027 /* instantiate and hash dentry */
Al Viro41d28bc2014-10-12 22:24:21 -04001028 ret = d_splice_alias(inode, dentry);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001029 out_unlock:
Tejun Heoa797bfc2013-12-11 14:11:57 -05001030 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001031 return ret;
1032}
1033
Tejun Heo80b9bbe2013-12-11 16:03:00 -05001034static int kernfs_iop_mkdir(struct inode *dir, struct dentry *dentry,
1035 umode_t mode)
1036{
1037 struct kernfs_node *parent = dir->i_private;
Tejun Heo90c07c82014-02-03 14:09:09 -05001038 struct kernfs_syscall_ops *scops = kernfs_root(parent)->syscall_ops;
Tejun Heo07c75302014-02-03 14:09:08 -05001039 int ret;
Tejun Heo80b9bbe2013-12-11 16:03:00 -05001040
Tejun Heo90c07c82014-02-03 14:09:09 -05001041 if (!scops || !scops->mkdir)
Tejun Heo80b9bbe2013-12-11 16:03:00 -05001042 return -EPERM;
1043
Tejun Heo07c75302014-02-03 14:09:08 -05001044 if (!kernfs_get_active(parent))
1045 return -ENODEV;
1046
Tejun Heo90c07c82014-02-03 14:09:09 -05001047 ret = scops->mkdir(parent, dentry->d_name.name, mode);
Tejun Heo07c75302014-02-03 14:09:08 -05001048
1049 kernfs_put_active(parent);
1050 return ret;
Tejun Heo80b9bbe2013-12-11 16:03:00 -05001051}
1052
1053static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
1054{
1055 struct kernfs_node *kn = dentry->d_fsdata;
Tejun Heo90c07c82014-02-03 14:09:09 -05001056 struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops;
Tejun Heo07c75302014-02-03 14:09:08 -05001057 int ret;
Tejun Heo80b9bbe2013-12-11 16:03:00 -05001058
Tejun Heo90c07c82014-02-03 14:09:09 -05001059 if (!scops || !scops->rmdir)
Tejun Heo80b9bbe2013-12-11 16:03:00 -05001060 return -EPERM;
1061
Tejun Heo07c75302014-02-03 14:09:08 -05001062 if (!kernfs_get_active(kn))
1063 return -ENODEV;
1064
Tejun Heo90c07c82014-02-03 14:09:09 -05001065 ret = scops->rmdir(kn);
Tejun Heo07c75302014-02-03 14:09:08 -05001066
1067 kernfs_put_active(kn);
1068 return ret;
Tejun Heo80b9bbe2013-12-11 16:03:00 -05001069}
1070
1071static int kernfs_iop_rename(struct inode *old_dir, struct dentry *old_dentry,
1072 struct inode *new_dir, struct dentry *new_dentry)
1073{
1074 struct kernfs_node *kn = old_dentry->d_fsdata;
1075 struct kernfs_node *new_parent = new_dir->i_private;
Tejun Heo90c07c82014-02-03 14:09:09 -05001076 struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops;
Tejun Heo07c75302014-02-03 14:09:08 -05001077 int ret;
Tejun Heo80b9bbe2013-12-11 16:03:00 -05001078
Tejun Heo90c07c82014-02-03 14:09:09 -05001079 if (!scops || !scops->rename)
Tejun Heo80b9bbe2013-12-11 16:03:00 -05001080 return -EPERM;
1081
Tejun Heo07c75302014-02-03 14:09:08 -05001082 if (!kernfs_get_active(kn))
1083 return -ENODEV;
1084
1085 if (!kernfs_get_active(new_parent)) {
1086 kernfs_put_active(kn);
1087 return -ENODEV;
1088 }
1089
Tejun Heo90c07c82014-02-03 14:09:09 -05001090 ret = scops->rename(kn, new_parent, new_dentry->d_name.name);
Tejun Heo07c75302014-02-03 14:09:08 -05001091
1092 kernfs_put_active(new_parent);
1093 kernfs_put_active(kn);
1094 return ret;
Tejun Heo80b9bbe2013-12-11 16:03:00 -05001095}
1096
Tejun Heoa797bfc2013-12-11 14:11:57 -05001097const struct inode_operations kernfs_dir_iops = {
Tejun Heoc637b8a2013-12-11 14:11:58 -05001098 .lookup = kernfs_iop_lookup,
1099 .permission = kernfs_iop_permission,
1100 .setattr = kernfs_iop_setattr,
1101 .getattr = kernfs_iop_getattr,
1102 .setxattr = kernfs_iop_setxattr,
1103 .removexattr = kernfs_iop_removexattr,
1104 .getxattr = kernfs_iop_getxattr,
1105 .listxattr = kernfs_iop_listxattr,
Tejun Heo80b9bbe2013-12-11 16:03:00 -05001106
1107 .mkdir = kernfs_iop_mkdir,
1108 .rmdir = kernfs_iop_rmdir,
1109 .rename = kernfs_iop_rename,
Tejun Heofd7b9f72013-11-28 14:54:33 -05001110};
1111
Tejun Heoc637b8a2013-12-11 14:11:58 -05001112static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001113{
Tejun Heo324a56e2013-12-11 14:11:53 -05001114 struct kernfs_node *last;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001115
1116 while (true) {
1117 struct rb_node *rbn;
1118
1119 last = pos;
1120
Tejun Heodf23fc32013-12-11 14:11:56 -05001121 if (kernfs_type(pos) != KERNFS_DIR)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001122 break;
1123
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001124 rbn = rb_first(&pos->dir.children);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001125 if (!rbn)
1126 break;
1127
Tejun Heo324a56e2013-12-11 14:11:53 -05001128 pos = rb_to_kn(rbn);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001129 }
1130
1131 return last;
1132}
1133
1134/**
Tejun Heoc637b8a2013-12-11 14:11:58 -05001135 * kernfs_next_descendant_post - find the next descendant for post-order walk
Tejun Heofd7b9f72013-11-28 14:54:33 -05001136 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo324a56e2013-12-11 14:11:53 -05001137 * @root: kernfs_node whose descendants to walk
Tejun Heofd7b9f72013-11-28 14:54:33 -05001138 *
1139 * Find the next descendant to visit for post-order traversal of @root's
1140 * descendants. @root is included in the iteration and the last node to be
1141 * visited.
1142 */
Tejun Heoc637b8a2013-12-11 14:11:58 -05001143static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
1144 struct kernfs_node *root)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001145{
1146 struct rb_node *rbn;
1147
Tejun Heoa797bfc2013-12-11 14:11:57 -05001148 lockdep_assert_held(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001149
1150 /* if first iteration, visit leftmost descendant which may be root */
1151 if (!pos)
Tejun Heoc637b8a2013-12-11 14:11:58 -05001152 return kernfs_leftmost_descendant(root);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001153
1154 /* if we visited @root, we're done */
1155 if (pos == root)
1156 return NULL;
1157
1158 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001159 rbn = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001160 if (rbn)
Tejun Heoc637b8a2013-12-11 14:11:58 -05001161 return kernfs_leftmost_descendant(rb_to_kn(rbn));
Tejun Heofd7b9f72013-11-28 14:54:33 -05001162
1163 /* no sibling left, visit parent */
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001164 return pos->parent;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001165}
1166
Tejun Heod35258e2014-02-03 14:09:12 -05001167/**
1168 * kernfs_activate - activate a node which started deactivated
1169 * @kn: kernfs_node whose subtree is to be activated
1170 *
1171 * If the root has KERNFS_ROOT_CREATE_DEACTIVATED set, a newly created node
1172 * needs to be explicitly activated. A node which hasn't been activated
1173 * isn't visible to userland and deactivation is skipped during its
1174 * removal. This is useful to construct atomic init sequences where
1175 * creation of multiple nodes should either succeed or fail atomically.
1176 *
1177 * The caller is responsible for ensuring that this function is not called
1178 * after kernfs_remove*() is invoked on @kn.
1179 */
1180void kernfs_activate(struct kernfs_node *kn)
1181{
1182 struct kernfs_node *pos;
1183
1184 mutex_lock(&kernfs_mutex);
1185
1186 pos = NULL;
1187 while ((pos = kernfs_next_descendant_post(pos, kn))) {
1188 if (!pos || (pos->flags & KERNFS_ACTIVATED))
1189 continue;
1190
1191 WARN_ON_ONCE(pos->parent && RB_EMPTY_NODE(&pos->rb));
1192 WARN_ON_ONCE(atomic_read(&pos->active) != KN_DEACTIVATED_BIAS);
1193
1194 atomic_sub(KN_DEACTIVATED_BIAS, &pos->active);
1195 pos->flags |= KERNFS_ACTIVATED;
1196 }
1197
1198 mutex_unlock(&kernfs_mutex);
1199}
1200
Tejun Heo988cd7a2014-02-03 14:02:58 -05001201static void __kernfs_remove(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001202{
Tejun Heo35beab02014-02-03 14:02:56 -05001203 struct kernfs_node *pos;
1204
1205 lockdep_assert_held(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001206
Tejun Heo6b0afc22014-02-03 14:03:01 -05001207 /*
1208 * Short-circuit if non-root @kn has already finished removal.
1209 * This is for kernfs_remove_self() which plays with active ref
1210 * after removal.
1211 */
1212 if (!kn || (kn->parent && RB_EMPTY_NODE(&kn->rb)))
Greg Kroah-Hartmance9b4992014-01-13 13:50:31 -08001213 return;
1214
Tejun Heoc637b8a2013-12-11 14:11:58 -05001215 pr_debug("kernfs %s: removing\n", kn->name);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001216
Tejun Heo81c173c2014-02-03 14:03:00 -05001217 /* prevent any new usage under @kn by deactivating all nodes */
Tejun Heo35beab02014-02-03 14:02:56 -05001218 pos = NULL;
1219 while ((pos = kernfs_next_descendant_post(pos, kn)))
Tejun Heo81c173c2014-02-03 14:03:00 -05001220 if (kernfs_active(pos))
1221 atomic_add(KN_DEACTIVATED_BIAS, &pos->active);
Tejun Heo35beab02014-02-03 14:02:56 -05001222
1223 /* deactivate and unlink the subtree node-by-node */
Tejun Heofd7b9f72013-11-28 14:54:33 -05001224 do {
Tejun Heo35beab02014-02-03 14:02:56 -05001225 pos = kernfs_leftmost_descendant(kn);
1226
1227 /*
Tejun Heo81c173c2014-02-03 14:03:00 -05001228 * kernfs_drain() drops kernfs_mutex temporarily and @pos's
1229 * base ref could have been put by someone else by the time
1230 * the function returns. Make sure it doesn't go away
1231 * underneath us.
Tejun Heo35beab02014-02-03 14:02:56 -05001232 */
1233 kernfs_get(pos);
1234
Tejun Heod35258e2014-02-03 14:09:12 -05001235 /*
1236 * Drain iff @kn was activated. This avoids draining and
1237 * its lockdep annotations for nodes which have never been
1238 * activated and allows embedding kernfs_remove() in create
1239 * error paths without worrying about draining.
1240 */
1241 if (kn->flags & KERNFS_ACTIVATED)
1242 kernfs_drain(pos);
1243 else
1244 WARN_ON_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS);
Tejun Heo35beab02014-02-03 14:02:56 -05001245
1246 /*
1247 * kernfs_unlink_sibling() succeeds once per node. Use it
1248 * to decide who's responsible for cleanups.
1249 */
1250 if (!pos->parent || kernfs_unlink_sibling(pos)) {
1251 struct kernfs_iattrs *ps_iattr =
1252 pos->parent ? pos->parent->iattr : NULL;
1253
1254 /* update timestamps on the parent */
1255 if (ps_iattr) {
Deepa Dinamani3a3a5fe2016-02-22 07:17:53 -08001256 ktime_get_real_ts(&ps_iattr->ia_iattr.ia_ctime);
1257 ps_iattr->ia_iattr.ia_mtime =
1258 ps_iattr->ia_iattr.ia_ctime;
Tejun Heo35beab02014-02-03 14:02:56 -05001259 }
1260
Tejun Heo988cd7a2014-02-03 14:02:58 -05001261 kernfs_put(pos);
Tejun Heo35beab02014-02-03 14:02:56 -05001262 }
1263
1264 kernfs_put(pos);
1265 } while (pos != kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001266}
1267
1268/**
Tejun Heo324a56e2013-12-11 14:11:53 -05001269 * kernfs_remove - remove a kernfs_node recursively
1270 * @kn: the kernfs_node to remove
Tejun Heofd7b9f72013-11-28 14:54:33 -05001271 *
Tejun Heo324a56e2013-12-11 14:11:53 -05001272 * Remove @kn along with all its subdirectories and files.
Tejun Heofd7b9f72013-11-28 14:54:33 -05001273 */
Tejun Heo324a56e2013-12-11 14:11:53 -05001274void kernfs_remove(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001275{
Tejun Heo988cd7a2014-02-03 14:02:58 -05001276 mutex_lock(&kernfs_mutex);
1277 __kernfs_remove(kn);
1278 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001279}
1280
1281/**
Tejun Heo6b0afc22014-02-03 14:03:01 -05001282 * kernfs_break_active_protection - break out of active protection
1283 * @kn: the self kernfs_node
1284 *
1285 * The caller must be running off of a kernfs operation which is invoked
1286 * with an active reference - e.g. one of kernfs_ops. Each invocation of
1287 * this function must also be matched with an invocation of
1288 * kernfs_unbreak_active_protection().
1289 *
1290 * This function releases the active reference of @kn the caller is
1291 * holding. Once this function is called, @kn may be removed at any point
1292 * and the caller is solely responsible for ensuring that the objects it
1293 * dereferences are accessible.
1294 */
1295void kernfs_break_active_protection(struct kernfs_node *kn)
1296{
1297 /*
1298 * Take out ourself out of the active ref dependency chain. If
1299 * we're called without an active ref, lockdep will complain.
1300 */
1301 kernfs_put_active(kn);
1302}
1303
1304/**
1305 * kernfs_unbreak_active_protection - undo kernfs_break_active_protection()
1306 * @kn: the self kernfs_node
1307 *
1308 * If kernfs_break_active_protection() was called, this function must be
1309 * invoked before finishing the kernfs operation. Note that while this
1310 * function restores the active reference, it doesn't and can't actually
1311 * restore the active protection - @kn may already or be in the process of
1312 * being removed. Once kernfs_break_active_protection() is invoked, that
1313 * protection is irreversibly gone for the kernfs operation instance.
1314 *
1315 * While this function may be called at any point after
1316 * kernfs_break_active_protection() is invoked, its most useful location
1317 * would be right before the enclosing kernfs operation returns.
1318 */
1319void kernfs_unbreak_active_protection(struct kernfs_node *kn)
1320{
1321 /*
1322 * @kn->active could be in any state; however, the increment we do
1323 * here will be undone as soon as the enclosing kernfs operation
1324 * finishes and this temporary bump can't break anything. If @kn
1325 * is alive, nothing changes. If @kn is being deactivated, the
1326 * soon-to-follow put will either finish deactivation or restore
1327 * deactivated state. If @kn is already removed, the temporary
1328 * bump is guaranteed to be gone before @kn is released.
1329 */
1330 atomic_inc(&kn->active);
1331 if (kernfs_lockdep(kn))
1332 rwsem_acquire(&kn->dep_map, 0, 1, _RET_IP_);
1333}
1334
1335/**
1336 * kernfs_remove_self - remove a kernfs_node from its own method
1337 * @kn: the self kernfs_node to remove
1338 *
1339 * The caller must be running off of a kernfs operation which is invoked
1340 * with an active reference - e.g. one of kernfs_ops. This can be used to
1341 * implement a file operation which deletes itself.
1342 *
1343 * For example, the "delete" file for a sysfs device directory can be
1344 * implemented by invoking kernfs_remove_self() on the "delete" file
1345 * itself. This function breaks the circular dependency of trying to
1346 * deactivate self while holding an active ref itself. It isn't necessary
1347 * to modify the usual removal path to use kernfs_remove_self(). The
1348 * "delete" implementation can simply invoke kernfs_remove_self() on self
1349 * before proceeding with the usual removal path. kernfs will ignore later
1350 * kernfs_remove() on self.
1351 *
1352 * kernfs_remove_self() can be called multiple times concurrently on the
1353 * same kernfs_node. Only the first one actually performs removal and
1354 * returns %true. All others will wait until the kernfs operation which
1355 * won self-removal finishes and return %false. Note that the losers wait
1356 * for the completion of not only the winning kernfs_remove_self() but also
1357 * the whole kernfs_ops which won the arbitration. This can be used to
1358 * guarantee, for example, all concurrent writes to a "delete" file to
1359 * finish only after the whole operation is complete.
1360 */
1361bool kernfs_remove_self(struct kernfs_node *kn)
1362{
1363 bool ret;
1364
1365 mutex_lock(&kernfs_mutex);
1366 kernfs_break_active_protection(kn);
1367
1368 /*
1369 * SUICIDAL is used to arbitrate among competing invocations. Only
1370 * the first one will actually perform removal. When the removal
1371 * is complete, SUICIDED is set and the active ref is restored
1372 * while holding kernfs_mutex. The ones which lost arbitration
1373 * waits for SUICDED && drained which can happen only after the
1374 * enclosing kernfs operation which executed the winning instance
1375 * of kernfs_remove_self() finished.
1376 */
1377 if (!(kn->flags & KERNFS_SUICIDAL)) {
1378 kn->flags |= KERNFS_SUICIDAL;
1379 __kernfs_remove(kn);
1380 kn->flags |= KERNFS_SUICIDED;
1381 ret = true;
1382 } else {
1383 wait_queue_head_t *waitq = &kernfs_root(kn)->deactivate_waitq;
1384 DEFINE_WAIT(wait);
1385
1386 while (true) {
1387 prepare_to_wait(waitq, &wait, TASK_UNINTERRUPTIBLE);
1388
1389 if ((kn->flags & KERNFS_SUICIDED) &&
1390 atomic_read(&kn->active) == KN_DEACTIVATED_BIAS)
1391 break;
1392
1393 mutex_unlock(&kernfs_mutex);
1394 schedule();
1395 mutex_lock(&kernfs_mutex);
1396 }
1397 finish_wait(waitq, &wait);
1398 WARN_ON_ONCE(!RB_EMPTY_NODE(&kn->rb));
1399 ret = false;
1400 }
1401
1402 /*
1403 * This must be done while holding kernfs_mutex; otherwise, waiting
1404 * for SUICIDED && deactivated could finish prematurely.
1405 */
1406 kernfs_unbreak_active_protection(kn);
1407
1408 mutex_unlock(&kernfs_mutex);
1409 return ret;
1410}
1411
1412/**
Tejun Heo324a56e2013-12-11 14:11:53 -05001413 * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
1414 * @parent: parent of the target
1415 * @name: name of the kernfs_node to remove
1416 * @ns: namespace tag of the kernfs_node to remove
Tejun Heofd7b9f72013-11-28 14:54:33 -05001417 *
Tejun Heo324a56e2013-12-11 14:11:53 -05001418 * Look for the kernfs_node with @name and @ns under @parent and remove it.
1419 * Returns 0 on success, -ENOENT if such entry doesn't exist.
Tejun Heofd7b9f72013-11-28 14:54:33 -05001420 */
Tejun Heo324a56e2013-12-11 14:11:53 -05001421int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
Tejun Heofd7b9f72013-11-28 14:54:33 -05001422 const void *ns)
1423{
Tejun Heo324a56e2013-12-11 14:11:53 -05001424 struct kernfs_node *kn;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001425
Tejun Heo324a56e2013-12-11 14:11:53 -05001426 if (!parent) {
Tejun Heoc637b8a2013-12-11 14:11:58 -05001427 WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
Tejun Heofd7b9f72013-11-28 14:54:33 -05001428 name);
1429 return -ENOENT;
1430 }
1431
Tejun Heo988cd7a2014-02-03 14:02:58 -05001432 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001433
Tejun Heo324a56e2013-12-11 14:11:53 -05001434 kn = kernfs_find_ns(parent, name, ns);
1435 if (kn)
Tejun Heo988cd7a2014-02-03 14:02:58 -05001436 __kernfs_remove(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001437
Tejun Heo988cd7a2014-02-03 14:02:58 -05001438 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001439
Tejun Heo324a56e2013-12-11 14:11:53 -05001440 if (kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001441 return 0;
1442 else
1443 return -ENOENT;
1444}
1445
1446/**
1447 * kernfs_rename_ns - move and rename a kernfs_node
Tejun Heo324a56e2013-12-11 14:11:53 -05001448 * @kn: target node
Tejun Heofd7b9f72013-11-28 14:54:33 -05001449 * @new_parent: new parent to put @sd under
1450 * @new_name: new name
1451 * @new_ns: new namespace tag
1452 */
Tejun Heo324a56e2013-12-11 14:11:53 -05001453int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
Tejun Heofd7b9f72013-11-28 14:54:33 -05001454 const char *new_name, const void *new_ns)
1455{
Tejun Heo3eef34a2014-02-07 13:32:07 -05001456 struct kernfs_node *old_parent;
1457 const char *old_name = NULL;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001458 int error;
1459
Tejun Heo3eef34a2014-02-07 13:32:07 -05001460 /* can't move or rename root */
1461 if (!kn->parent)
1462 return -EINVAL;
1463
Tejun Heoae343722014-01-10 08:57:21 -05001464 mutex_lock(&kernfs_mutex);
Tejun Heod0ae3d42013-12-11 16:02:56 -05001465
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -08001466 error = -ENOENT;
Eric W. Biedermanea015212015-05-13 16:09:29 -05001467 if (!kernfs_active(kn) || !kernfs_active(new_parent) ||
1468 (new_parent->flags & KERNFS_EMPTY_DIR))
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -08001469 goto out;
1470
Tejun Heofd7b9f72013-11-28 14:54:33 -05001471 error = 0;
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001472 if ((kn->parent == new_parent) && (kn->ns == new_ns) &&
1473 (strcmp(kn->name, new_name) == 0))
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -08001474 goto out; /* nothing to rename */
Tejun Heofd7b9f72013-11-28 14:54:33 -05001475
1476 error = -EEXIST;
1477 if (kernfs_find_ns(new_parent, new_name, new_ns))
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -08001478 goto out;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001479
Tejun Heo324a56e2013-12-11 14:11:53 -05001480 /* rename kernfs_node */
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001481 if (strcmp(kn->name, new_name) != 0) {
Tejun Heofd7b9f72013-11-28 14:54:33 -05001482 error = -ENOMEM;
Andrzej Hajda75287a62015-02-13 14:36:27 -08001483 new_name = kstrdup_const(new_name, GFP_KERNEL);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001484 if (!new_name)
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -08001485 goto out;
Tejun Heo3eef34a2014-02-07 13:32:07 -05001486 } else {
1487 new_name = NULL;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001488 }
1489
1490 /*
1491 * Move to the appropriate place in the appropriate directories rbtree.
1492 */
Tejun Heoc637b8a2013-12-11 14:11:58 -05001493 kernfs_unlink_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001494 kernfs_get(new_parent);
Tejun Heo3eef34a2014-02-07 13:32:07 -05001495
1496 /* rename_lock protects ->parent and ->name accessors */
1497 spin_lock_irq(&kernfs_rename_lock);
1498
1499 old_parent = kn->parent;
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001500 kn->parent = new_parent;
Tejun Heo3eef34a2014-02-07 13:32:07 -05001501
1502 kn->ns = new_ns;
1503 if (new_name) {
Tejun Heodfeb07502015-02-13 14:36:31 -08001504 old_name = kn->name;
Tejun Heo3eef34a2014-02-07 13:32:07 -05001505 kn->name = new_name;
1506 }
1507
1508 spin_unlock_irq(&kernfs_rename_lock);
1509
Tejun Heo9561a892014-02-10 17:57:09 -05001510 kn->hash = kernfs_name_hash(kn->name, kn->ns);
Tejun Heoc637b8a2013-12-11 14:11:58 -05001511 kernfs_link_sibling(kn);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001512
Tejun Heo3eef34a2014-02-07 13:32:07 -05001513 kernfs_put(old_parent);
Andrzej Hajda75287a62015-02-13 14:36:27 -08001514 kfree_const(old_name);
Tejun Heo3eef34a2014-02-07 13:32:07 -05001515
Tejun Heofd7b9f72013-11-28 14:54:33 -05001516 error = 0;
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -08001517 out:
Tejun Heoa797bfc2013-12-11 14:11:57 -05001518 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001519 return error;
1520}
1521
Tejun Heofd7b9f72013-11-28 14:54:33 -05001522/* Relationship between s_mode and the DT_xxx types */
Tejun Heo324a56e2013-12-11 14:11:53 -05001523static inline unsigned char dt_type(struct kernfs_node *kn)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001524{
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001525 return (kn->mode >> 12) & 15;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001526}
1527
Tejun Heoc637b8a2013-12-11 14:11:58 -05001528static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001529{
1530 kernfs_put(filp->private_data);
1531 return 0;
1532}
1533
Tejun Heoc637b8a2013-12-11 14:11:58 -05001534static struct kernfs_node *kernfs_dir_pos(const void *ns,
Tejun Heo324a56e2013-12-11 14:11:53 -05001535 struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001536{
1537 if (pos) {
Tejun Heo81c173c2014-02-03 14:03:00 -05001538 int valid = kernfs_active(pos) &&
Greg Kroah-Hartman798c75a2014-01-13 14:36:03 -08001539 pos->parent == parent && hash == pos->hash;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001540 kernfs_put(pos);
1541 if (!valid)
1542 pos = NULL;
1543 }
1544 if (!pos && (hash > 1) && (hash < INT_MAX)) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001545 struct rb_node *node = parent->dir.children.rb_node;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001546 while (node) {
Tejun Heo324a56e2013-12-11 14:11:53 -05001547 pos = rb_to_kn(node);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001548
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001549 if (hash < pos->hash)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001550 node = node->rb_left;
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001551 else if (hash > pos->hash)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001552 node = node->rb_right;
1553 else
1554 break;
1555 }
1556 }
Tejun Heob9c9dad2014-02-03 14:09:11 -05001557 /* Skip over entries which are dying/dead or in the wrong namespace */
1558 while (pos && (!kernfs_active(pos) || pos->ns != ns)) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001559 struct rb_node *node = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001560 if (!node)
1561 pos = NULL;
1562 else
Tejun Heo324a56e2013-12-11 14:11:53 -05001563 pos = rb_to_kn(node);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001564 }
1565 return pos;
1566}
1567
Tejun Heoc637b8a2013-12-11 14:11:58 -05001568static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
Tejun Heo324a56e2013-12-11 14:11:53 -05001569 struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001570{
Tejun Heoc637b8a2013-12-11 14:11:58 -05001571 pos = kernfs_dir_pos(ns, parent, ino, pos);
Tejun Heob9c9dad2014-02-03 14:09:11 -05001572 if (pos) {
Tejun Heofd7b9f72013-11-28 14:54:33 -05001573 do {
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001574 struct rb_node *node = rb_next(&pos->rb);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001575 if (!node)
1576 pos = NULL;
1577 else
Tejun Heo324a56e2013-12-11 14:11:53 -05001578 pos = rb_to_kn(node);
Tejun Heob9c9dad2014-02-03 14:09:11 -05001579 } while (pos && (!kernfs_active(pos) || pos->ns != ns));
1580 }
Tejun Heofd7b9f72013-11-28 14:54:33 -05001581 return pos;
1582}
1583
Tejun Heoc637b8a2013-12-11 14:11:58 -05001584static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
Tejun Heofd7b9f72013-11-28 14:54:33 -05001585{
1586 struct dentry *dentry = file->f_path.dentry;
Tejun Heo324a56e2013-12-11 14:11:53 -05001587 struct kernfs_node *parent = dentry->d_fsdata;
1588 struct kernfs_node *pos = file->private_data;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001589 const void *ns = NULL;
1590
1591 if (!dir_emit_dots(file, ctx))
1592 return 0;
Tejun Heoa797bfc2013-12-11 14:11:57 -05001593 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001594
Tejun Heo324a56e2013-12-11 14:11:53 -05001595 if (kernfs_ns_enabled(parent))
Tejun Heoc525aad2013-12-11 14:11:55 -05001596 ns = kernfs_info(dentry->d_sb)->ns;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001597
Tejun Heoc637b8a2013-12-11 14:11:58 -05001598 for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001599 pos;
Tejun Heoc637b8a2013-12-11 14:11:58 -05001600 pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001601 const char *name = pos->name;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001602 unsigned int type = dt_type(pos);
1603 int len = strlen(name);
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001604 ino_t ino = pos->ino;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001605
Tejun Heoadc5e8b2013-12-11 14:11:54 -05001606 ctx->pos = pos->hash;
Tejun Heofd7b9f72013-11-28 14:54:33 -05001607 file->private_data = pos;
1608 kernfs_get(pos);
1609
Tejun Heoa797bfc2013-12-11 14:11:57 -05001610 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001611 if (!dir_emit(ctx, name, len, ino, type))
1612 return 0;
Tejun Heoa797bfc2013-12-11 14:11:57 -05001613 mutex_lock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001614 }
Tejun Heoa797bfc2013-12-11 14:11:57 -05001615 mutex_unlock(&kernfs_mutex);
Tejun Heofd7b9f72013-11-28 14:54:33 -05001616 file->private_data = NULL;
1617 ctx->pos = INT_MAX;
1618 return 0;
1619}
1620
Tejun Heoa797bfc2013-12-11 14:11:57 -05001621const struct file_operations kernfs_dir_fops = {
Tejun Heofd7b9f72013-11-28 14:54:33 -05001622 .read = generic_read_dir,
Al Viro8cb0d2c2016-04-20 19:59:01 -04001623 .iterate_shared = kernfs_fop_readdir,
Tejun Heoc637b8a2013-12-11 14:11:58 -05001624 .release = kernfs_dir_fop_release,
Al Viro8cb0d2c2016-04-20 19:59:01 -04001625 .llseek = generic_file_llseek,
Tejun Heofd7b9f72013-11-28 14:54:33 -05001626};