]>
Commit | Line | Data |
---|---|---|
b8441ed2 TH |
1 | /* |
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 | */ | |
fd7b9f7b | 10 | |
abd54f02 | 11 | #include <linux/sched.h> |
fd7b9f7b TH |
12 | #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 | ||
a797bfc3 | 21 | DEFINE_MUTEX(kernfs_mutex); |
fd7b9f7b | 22 | |
adc5e8b5 | 23 | #define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb) |
fd7b9f7b | 24 | |
182fd64b TH |
25 | static bool kernfs_lockdep(struct kernfs_node *kn) |
26 | { | |
27 | #ifdef CONFIG_DEBUG_LOCK_ALLOC | |
28 | return kn->flags & KERNFS_LOCKDEP; | |
29 | #else | |
30 | return false; | |
31 | #endif | |
32 | } | |
33 | ||
fd7b9f7b | 34 | /** |
c637b8ac | 35 | * kernfs_name_hash |
fd7b9f7b TH |
36 | * @name: Null terminated string to hash |
37 | * @ns: Namespace tag to hash | |
38 | * | |
39 | * Returns 31 bit hash of ns + name (so it fits in an off_t ) | |
40 | */ | |
c637b8ac | 41 | static unsigned int kernfs_name_hash(const char *name, const void *ns) |
fd7b9f7b TH |
42 | { |
43 | unsigned long hash = init_name_hash(); | |
44 | unsigned int len = strlen(name); | |
45 | while (len--) | |
46 | hash = partial_name_hash(*name++, hash); | |
47 | hash = (end_name_hash(hash) ^ hash_ptr((void *)ns, 31)); | |
48 | hash &= 0x7fffffffU; | |
49 | /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */ | |
50 | if (hash < 1) | |
51 | hash += 2; | |
52 | if (hash >= INT_MAX) | |
53 | hash = INT_MAX - 1; | |
54 | return hash; | |
55 | } | |
56 | ||
c637b8ac TH |
57 | static int kernfs_name_compare(unsigned int hash, const char *name, |
58 | const void *ns, const struct kernfs_node *kn) | |
fd7b9f7b | 59 | { |
adc5e8b5 TH |
60 | if (hash != kn->hash) |
61 | return hash - kn->hash; | |
62 | if (ns != kn->ns) | |
63 | return ns - kn->ns; | |
64 | return strcmp(name, kn->name); | |
fd7b9f7b TH |
65 | } |
66 | ||
c637b8ac TH |
67 | static int kernfs_sd_compare(const struct kernfs_node *left, |
68 | const struct kernfs_node *right) | |
fd7b9f7b | 69 | { |
c637b8ac | 70 | return kernfs_name_compare(left->hash, left->name, left->ns, right); |
fd7b9f7b TH |
71 | } |
72 | ||
73 | /** | |
c637b8ac | 74 | * kernfs_link_sibling - link kernfs_node into sibling rbtree |
324a56e1 | 75 | * @kn: kernfs_node of interest |
fd7b9f7b | 76 | * |
324a56e1 | 77 | * Link @kn into its sibling rbtree which starts from |
adc5e8b5 | 78 | * @kn->parent->dir.children. |
fd7b9f7b TH |
79 | * |
80 | * Locking: | |
a797bfc3 | 81 | * mutex_lock(kernfs_mutex) |
fd7b9f7b TH |
82 | * |
83 | * RETURNS: | |
84 | * 0 on susccess -EEXIST on failure. | |
85 | */ | |
c637b8ac | 86 | static int kernfs_link_sibling(struct kernfs_node *kn) |
fd7b9f7b | 87 | { |
adc5e8b5 | 88 | struct rb_node **node = &kn->parent->dir.children.rb_node; |
fd7b9f7b TH |
89 | struct rb_node *parent = NULL; |
90 | ||
df23fc39 | 91 | if (kernfs_type(kn) == KERNFS_DIR) |
adc5e8b5 | 92 | kn->parent->dir.subdirs++; |
fd7b9f7b TH |
93 | |
94 | while (*node) { | |
324a56e1 | 95 | struct kernfs_node *pos; |
fd7b9f7b TH |
96 | int result; |
97 | ||
324a56e1 | 98 | pos = rb_to_kn(*node); |
fd7b9f7b | 99 | parent = *node; |
c637b8ac | 100 | result = kernfs_sd_compare(kn, pos); |
fd7b9f7b | 101 | if (result < 0) |
adc5e8b5 | 102 | node = &pos->rb.rb_left; |
fd7b9f7b | 103 | else if (result > 0) |
adc5e8b5 | 104 | node = &pos->rb.rb_right; |
fd7b9f7b TH |
105 | else |
106 | return -EEXIST; | |
107 | } | |
108 | /* add new node and rebalance the tree */ | |
adc5e8b5 TH |
109 | rb_link_node(&kn->rb, parent, node); |
110 | rb_insert_color(&kn->rb, &kn->parent->dir.children); | |
fd7b9f7b TH |
111 | return 0; |
112 | } | |
113 | ||
114 | /** | |
c637b8ac | 115 | * kernfs_unlink_sibling - unlink kernfs_node from sibling rbtree |
324a56e1 | 116 | * @kn: kernfs_node of interest |
fd7b9f7b | 117 | * |
35beab06 TH |
118 | * Try to unlink @kn from its sibling rbtree which starts from |
119 | * kn->parent->dir.children. Returns %true if @kn was actually | |
120 | * removed, %false if @kn wasn't on the rbtree. | |
fd7b9f7b TH |
121 | * |
122 | * Locking: | |
a797bfc3 | 123 | * mutex_lock(kernfs_mutex) |
fd7b9f7b | 124 | */ |
35beab06 | 125 | static bool kernfs_unlink_sibling(struct kernfs_node *kn) |
fd7b9f7b | 126 | { |
35beab06 TH |
127 | if (RB_EMPTY_NODE(&kn->rb)) |
128 | return false; | |
129 | ||
df23fc39 | 130 | if (kernfs_type(kn) == KERNFS_DIR) |
adc5e8b5 | 131 | kn->parent->dir.subdirs--; |
fd7b9f7b | 132 | |
adc5e8b5 | 133 | rb_erase(&kn->rb, &kn->parent->dir.children); |
35beab06 TH |
134 | RB_CLEAR_NODE(&kn->rb); |
135 | return true; | |
fd7b9f7b TH |
136 | } |
137 | ||
138 | /** | |
c637b8ac | 139 | * kernfs_get_active - get an active reference to kernfs_node |
324a56e1 | 140 | * @kn: kernfs_node to get an active reference to |
fd7b9f7b | 141 | * |
324a56e1 | 142 | * Get an active reference of @kn. This function is noop if @kn |
fd7b9f7b TH |
143 | * is NULL. |
144 | * | |
145 | * RETURNS: | |
324a56e1 | 146 | * Pointer to @kn on success, NULL on failure. |
fd7b9f7b | 147 | */ |
c637b8ac | 148 | struct kernfs_node *kernfs_get_active(struct kernfs_node *kn) |
fd7b9f7b | 149 | { |
324a56e1 | 150 | if (unlikely(!kn)) |
fd7b9f7b TH |
151 | return NULL; |
152 | ||
f4b3e631 GKH |
153 | if (!atomic_inc_unless_negative(&kn->active)) |
154 | return NULL; | |
895a068a | 155 | |
182fd64b | 156 | if (kernfs_lockdep(kn)) |
f4b3e631 GKH |
157 | rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_); |
158 | return kn; | |
fd7b9f7b TH |
159 | } |
160 | ||
161 | /** | |
c637b8ac | 162 | * kernfs_put_active - put an active reference to kernfs_node |
324a56e1 | 163 | * @kn: kernfs_node to put an active reference to |
fd7b9f7b | 164 | * |
324a56e1 | 165 | * Put an active reference to @kn. This function is noop if @kn |
fd7b9f7b TH |
166 | * is NULL. |
167 | */ | |
c637b8ac | 168 | void kernfs_put_active(struct kernfs_node *kn) |
fd7b9f7b | 169 | { |
abd54f02 | 170 | struct kernfs_root *root = kernfs_root(kn); |
fd7b9f7b TH |
171 | int v; |
172 | ||
324a56e1 | 173 | if (unlikely(!kn)) |
fd7b9f7b TH |
174 | return; |
175 | ||
182fd64b | 176 | if (kernfs_lockdep(kn)) |
324a56e1 | 177 | rwsem_release(&kn->dep_map, 1, _RET_IP_); |
adc5e8b5 | 178 | v = atomic_dec_return(&kn->active); |
df23fc39 | 179 | if (likely(v != KN_DEACTIVATED_BIAS)) |
fd7b9f7b TH |
180 | return; |
181 | ||
abd54f02 | 182 | wake_up_all(&root->deactivate_waitq); |
fd7b9f7b TH |
183 | } |
184 | ||
185 | /** | |
798c75a0 GKH |
186 | * kernfs_deactivate - deactivate kernfs_node |
187 | * @kn: kernfs_node to deactivate | |
fd7b9f7b | 188 | * |
ccf02aaf TH |
189 | * Deny new active references, drain existing ones and nuke all |
190 | * existing mmaps. Mutiple removers may invoke this function | |
191 | * concurrently on @kn and all will return after deactivation and | |
192 | * draining are complete. | |
fd7b9f7b | 193 | */ |
798c75a0 | 194 | static void kernfs_deactivate(struct kernfs_node *kn) |
35beab06 | 195 | __releases(&kernfs_mutex) __acquires(&kernfs_mutex) |
fd7b9f7b | 196 | { |
abd54f02 | 197 | struct kernfs_root *root = kernfs_root(kn); |
fd7b9f7b | 198 | |
35beab06 | 199 | lockdep_assert_held(&kernfs_mutex); |
798c75a0 GKH |
200 | BUG_ON(!(kn->flags & KERNFS_REMOVED)); |
201 | ||
35beab06 TH |
202 | /* only the first invocation on @kn should deactivate it */ |
203 | if (atomic_read(&kn->active) >= 0) | |
204 | atomic_add(KN_DEACTIVATED_BIAS, &kn->active); | |
ea1c472d | 205 | |
35beab06 | 206 | mutex_unlock(&kernfs_mutex); |
abd54f02 | 207 | |
182fd64b | 208 | if (kernfs_lockdep(kn)) { |
35beab06 TH |
209 | rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_); |
210 | if (atomic_read(&kn->active) != KN_DEACTIVATED_BIAS) | |
211 | lock_contended(&kn->dep_map, _RET_IP_); | |
212 | } | |
abd54f02 | 213 | |
35beab06 | 214 | /* but everyone should wait for draining */ |
abd54f02 TH |
215 | wait_event(root->deactivate_waitq, |
216 | atomic_read(&kn->active) == KN_DEACTIVATED_BIAS); | |
fd7b9f7b | 217 | |
182fd64b | 218 | if (kernfs_lockdep(kn)) { |
a6607930 TH |
219 | lock_acquired(&kn->dep_map, _RET_IP_); |
220 | rwsem_release(&kn->dep_map, 1, _RET_IP_); | |
221 | } | |
35beab06 | 222 | |
ccf02aaf TH |
223 | kernfs_unmap_bin_file(kn); |
224 | ||
35beab06 | 225 | mutex_lock(&kernfs_mutex); |
fd7b9f7b TH |
226 | } |
227 | ||
fd7b9f7b | 228 | /** |
324a56e1 TH |
229 | * kernfs_get - get a reference count on a kernfs_node |
230 | * @kn: the target kernfs_node | |
fd7b9f7b | 231 | */ |
324a56e1 | 232 | void kernfs_get(struct kernfs_node *kn) |
fd7b9f7b | 233 | { |
324a56e1 | 234 | if (kn) { |
adc5e8b5 TH |
235 | WARN_ON(!atomic_read(&kn->count)); |
236 | atomic_inc(&kn->count); | |
fd7b9f7b TH |
237 | } |
238 | } | |
239 | EXPORT_SYMBOL_GPL(kernfs_get); | |
240 | ||
241 | /** | |
324a56e1 TH |
242 | * kernfs_put - put a reference count on a kernfs_node |
243 | * @kn: the target kernfs_node | |
fd7b9f7b | 244 | * |
324a56e1 | 245 | * Put a reference count of @kn and destroy it if it reached zero. |
fd7b9f7b | 246 | */ |
324a56e1 | 247 | void kernfs_put(struct kernfs_node *kn) |
fd7b9f7b | 248 | { |
324a56e1 | 249 | struct kernfs_node *parent; |
ba7443bc | 250 | struct kernfs_root *root; |
fd7b9f7b | 251 | |
adc5e8b5 | 252 | if (!kn || !atomic_dec_and_test(&kn->count)) |
fd7b9f7b | 253 | return; |
324a56e1 | 254 | root = kernfs_root(kn); |
fd7b9f7b | 255 | repeat: |
798c75a0 | 256 | /* Moving/renaming is always done while holding reference. |
adc5e8b5 | 257 | * kn->parent won't change beneath us. |
fd7b9f7b | 258 | */ |
adc5e8b5 | 259 | parent = kn->parent; |
fd7b9f7b | 260 | |
798c75a0 GKH |
261 | WARN(!(kn->flags & KERNFS_REMOVED), "kernfs: free using entry: %s/%s\n", |
262 | parent ? parent->name : "", kn->name); | |
324a56e1 | 263 | |
df23fc39 | 264 | if (kernfs_type(kn) == KERNFS_LINK) |
adc5e8b5 | 265 | kernfs_put(kn->symlink.target_kn); |
2063d608 | 266 | if (!(kn->flags & KERNFS_STATIC_NAME)) |
adc5e8b5 TH |
267 | kfree(kn->name); |
268 | if (kn->iattr) { | |
269 | if (kn->iattr->ia_secdata) | |
270 | security_release_secctx(kn->iattr->ia_secdata, | |
271 | kn->iattr->ia_secdata_len); | |
272 | simple_xattrs_free(&kn->iattr->xattrs); | |
2322392b | 273 | } |
adc5e8b5 TH |
274 | kfree(kn->iattr); |
275 | ida_simple_remove(&root->ino_ida, kn->ino); | |
a797bfc3 | 276 | kmem_cache_free(kernfs_node_cache, kn); |
fd7b9f7b | 277 | |
324a56e1 TH |
278 | kn = parent; |
279 | if (kn) { | |
adc5e8b5 | 280 | if (atomic_dec_and_test(&kn->count)) |
ba7443bc TH |
281 | goto repeat; |
282 | } else { | |
324a56e1 | 283 | /* just released the root kn, free @root too */ |
bc755553 | 284 | ida_destroy(&root->ino_ida); |
ba7443bc TH |
285 | kfree(root); |
286 | } | |
fd7b9f7b TH |
287 | } |
288 | EXPORT_SYMBOL_GPL(kernfs_put); | |
289 | ||
c637b8ac | 290 | static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags) |
fd7b9f7b | 291 | { |
324a56e1 | 292 | struct kernfs_node *kn; |
fd7b9f7b TH |
293 | |
294 | if (flags & LOOKUP_RCU) | |
295 | return -ECHILD; | |
296 | ||
19bbb926 TH |
297 | /* Always perform fresh lookup for negatives */ |
298 | if (!dentry->d_inode) | |
299 | goto out_bad_unlocked; | |
300 | ||
324a56e1 | 301 | kn = dentry->d_fsdata; |
a797bfc3 | 302 | mutex_lock(&kernfs_mutex); |
fd7b9f7b | 303 | |
798c75a0 GKH |
304 | /* The kernfs node has been deleted */ |
305 | if (kn->flags & KERNFS_REMOVED) | |
fd7b9f7b TH |
306 | goto out_bad; |
307 | ||
c637b8ac | 308 | /* The kernfs node has been moved? */ |
adc5e8b5 | 309 | if (dentry->d_parent->d_fsdata != kn->parent) |
fd7b9f7b TH |
310 | goto out_bad; |
311 | ||
c637b8ac | 312 | /* The kernfs node has been renamed */ |
adc5e8b5 | 313 | if (strcmp(dentry->d_name.name, kn->name) != 0) |
fd7b9f7b TH |
314 | goto out_bad; |
315 | ||
c637b8ac | 316 | /* The kernfs node has been moved to a different namespace */ |
adc5e8b5 | 317 | if (kn->parent && kernfs_ns_enabled(kn->parent) && |
c525aadd | 318 | kernfs_info(dentry->d_sb)->ns != kn->ns) |
fd7b9f7b TH |
319 | goto out_bad; |
320 | ||
a797bfc3 | 321 | mutex_unlock(&kernfs_mutex); |
fd7b9f7b TH |
322 | out_valid: |
323 | return 1; | |
324 | out_bad: | |
a797bfc3 | 325 | mutex_unlock(&kernfs_mutex); |
19bbb926 TH |
326 | out_bad_unlocked: |
327 | /* | |
328 | * @dentry doesn't match the underlying kernfs node, drop the | |
329 | * dentry and force lookup. If we have submounts we must allow the | |
330 | * vfs caches to lie about the state of the filesystem to prevent | |
331 | * leaks and other nasty things, so use check_submounts_and_drop() | |
332 | * instead of d_drop(). | |
fd7b9f7b TH |
333 | */ |
334 | if (check_submounts_and_drop(dentry) != 0) | |
335 | goto out_valid; | |
336 | ||
337 | return 0; | |
338 | } | |
339 | ||
c637b8ac | 340 | static void kernfs_dop_release(struct dentry *dentry) |
fd7b9f7b TH |
341 | { |
342 | kernfs_put(dentry->d_fsdata); | |
343 | } | |
344 | ||
a797bfc3 | 345 | const struct dentry_operations kernfs_dops = { |
c637b8ac | 346 | .d_revalidate = kernfs_dop_revalidate, |
c637b8ac | 347 | .d_release = kernfs_dop_release, |
fd7b9f7b TH |
348 | }; |
349 | ||
db4aad20 TH |
350 | static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root, |
351 | const char *name, umode_t mode, | |
352 | unsigned flags) | |
fd7b9f7b TH |
353 | { |
354 | char *dup_name = NULL; | |
324a56e1 | 355 | struct kernfs_node *kn; |
bc755553 | 356 | int ret; |
fd7b9f7b | 357 | |
2063d608 | 358 | if (!(flags & KERNFS_STATIC_NAME)) { |
fd7b9f7b TH |
359 | name = dup_name = kstrdup(name, GFP_KERNEL); |
360 | if (!name) | |
361 | return NULL; | |
362 | } | |
363 | ||
a797bfc3 | 364 | kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL); |
324a56e1 | 365 | if (!kn) |
fd7b9f7b TH |
366 | goto err_out1; |
367 | ||
bc755553 TH |
368 | ret = ida_simple_get(&root->ino_ida, 1, 0, GFP_KERNEL); |
369 | if (ret < 0) | |
fd7b9f7b | 370 | goto err_out2; |
adc5e8b5 | 371 | kn->ino = ret; |
fd7b9f7b | 372 | |
adc5e8b5 | 373 | atomic_set(&kn->count, 1); |
798c75a0 | 374 | atomic_set(&kn->active, 0); |
35beab06 | 375 | RB_CLEAR_NODE(&kn->rb); |
fd7b9f7b | 376 | |
adc5e8b5 TH |
377 | kn->name = name; |
378 | kn->mode = mode; | |
798c75a0 | 379 | kn->flags = flags | KERNFS_REMOVED; |
fd7b9f7b | 380 | |
324a56e1 | 381 | return kn; |
fd7b9f7b TH |
382 | |
383 | err_out2: | |
a797bfc3 | 384 | kmem_cache_free(kernfs_node_cache, kn); |
fd7b9f7b TH |
385 | err_out1: |
386 | kfree(dup_name); | |
387 | return NULL; | |
388 | } | |
389 | ||
db4aad20 TH |
390 | struct kernfs_node *kernfs_new_node(struct kernfs_node *parent, |
391 | const char *name, umode_t mode, | |
392 | unsigned flags) | |
393 | { | |
394 | struct kernfs_node *kn; | |
395 | ||
396 | kn = __kernfs_new_node(kernfs_root(parent), name, mode, flags); | |
397 | if (kn) { | |
398 | kernfs_get(parent); | |
399 | kn->parent = parent; | |
400 | } | |
401 | return kn; | |
402 | } | |
403 | ||
fd7b9f7b | 404 | /** |
c637b8ac | 405 | * kernfs_add_one - add kernfs_node to parent without warning |
324a56e1 | 406 | * @kn: kernfs_node to be added |
fd7b9f7b | 407 | * |
db4aad20 TH |
408 | * The caller must already have initialized @kn->parent. This |
409 | * function increments nlink of the parent's inode if @kn is a | |
410 | * directory and link into the children list of the parent. | |
fd7b9f7b | 411 | * |
fd7b9f7b TH |
412 | * RETURNS: |
413 | * 0 on success, -EEXIST if entry with the given name already | |
414 | * exists. | |
415 | */ | |
988cd7af | 416 | int kernfs_add_one(struct kernfs_node *kn) |
fd7b9f7b | 417 | { |
db4aad20 | 418 | struct kernfs_node *parent = kn->parent; |
c525aadd | 419 | struct kernfs_iattrs *ps_iattr; |
988cd7af | 420 | bool has_ns; |
fd7b9f7b TH |
421 | int ret; |
422 | ||
988cd7af TH |
423 | mutex_lock(&kernfs_mutex); |
424 | ||
425 | ret = -EINVAL; | |
426 | has_ns = kernfs_ns_enabled(parent); | |
427 | if (WARN(has_ns != (bool)kn->ns, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n", | |
428 | has_ns ? "required" : "invalid", parent->name, kn->name)) | |
429 | goto out_unlock; | |
fd7b9f7b | 430 | |
df23fc39 | 431 | if (kernfs_type(parent) != KERNFS_DIR) |
988cd7af | 432 | goto out_unlock; |
fd7b9f7b | 433 | |
988cd7af | 434 | ret = -ENOENT; |
798c75a0 | 435 | if (parent->flags & KERNFS_REMOVED) |
988cd7af | 436 | goto out_unlock; |
798c75a0 | 437 | |
c637b8ac | 438 | kn->hash = kernfs_name_hash(kn->name, kn->ns); |
fd7b9f7b | 439 | |
c637b8ac | 440 | ret = kernfs_link_sibling(kn); |
fd7b9f7b | 441 | if (ret) |
988cd7af | 442 | goto out_unlock; |
fd7b9f7b TH |
443 | |
444 | /* Update timestamps on the parent */ | |
adc5e8b5 | 445 | ps_iattr = parent->iattr; |
fd7b9f7b TH |
446 | if (ps_iattr) { |
447 | struct iattr *ps_iattrs = &ps_iattr->ia_iattr; | |
448 | ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME; | |
449 | } | |
450 | ||
451 | /* Mark the entry added into directory tree */ | |
798c75a0 | 452 | kn->flags &= ~KERNFS_REMOVED; |
988cd7af TH |
453 | ret = 0; |
454 | out_unlock: | |
a797bfc3 | 455 | mutex_unlock(&kernfs_mutex); |
988cd7af | 456 | return ret; |
fd7b9f7b TH |
457 | } |
458 | ||
459 | /** | |
324a56e1 TH |
460 | * kernfs_find_ns - find kernfs_node with the given name |
461 | * @parent: kernfs_node to search under | |
fd7b9f7b TH |
462 | * @name: name to look for |
463 | * @ns: the namespace tag to use | |
464 | * | |
324a56e1 TH |
465 | * Look for kernfs_node with name @name under @parent. Returns pointer to |
466 | * the found kernfs_node on success, %NULL on failure. | |
fd7b9f7b | 467 | */ |
324a56e1 TH |
468 | static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent, |
469 | const unsigned char *name, | |
470 | const void *ns) | |
fd7b9f7b | 471 | { |
adc5e8b5 | 472 | struct rb_node *node = parent->dir.children.rb_node; |
ac9bba03 | 473 | bool has_ns = kernfs_ns_enabled(parent); |
fd7b9f7b TH |
474 | unsigned int hash; |
475 | ||
a797bfc3 | 476 | lockdep_assert_held(&kernfs_mutex); |
fd7b9f7b TH |
477 | |
478 | if (has_ns != (bool)ns) { | |
c637b8ac | 479 | WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n", |
adc5e8b5 | 480 | has_ns ? "required" : "invalid", parent->name, name); |
fd7b9f7b TH |
481 | return NULL; |
482 | } | |
483 | ||
c637b8ac | 484 | hash = kernfs_name_hash(name, ns); |
fd7b9f7b | 485 | while (node) { |
324a56e1 | 486 | struct kernfs_node *kn; |
fd7b9f7b TH |
487 | int result; |
488 | ||
324a56e1 | 489 | kn = rb_to_kn(node); |
c637b8ac | 490 | result = kernfs_name_compare(hash, name, ns, kn); |
fd7b9f7b TH |
491 | if (result < 0) |
492 | node = node->rb_left; | |
493 | else if (result > 0) | |
494 | node = node->rb_right; | |
495 | else | |
324a56e1 | 496 | return kn; |
fd7b9f7b TH |
497 | } |
498 | return NULL; | |
499 | } | |
500 | ||
501 | /** | |
324a56e1 TH |
502 | * kernfs_find_and_get_ns - find and get kernfs_node with the given name |
503 | * @parent: kernfs_node to search under | |
fd7b9f7b TH |
504 | * @name: name to look for |
505 | * @ns: the namespace tag to use | |
506 | * | |
324a56e1 | 507 | * Look for kernfs_node with name @name under @parent and get a reference |
fd7b9f7b | 508 | * if found. This function may sleep and returns pointer to the found |
324a56e1 | 509 | * kernfs_node on success, %NULL on failure. |
fd7b9f7b | 510 | */ |
324a56e1 TH |
511 | struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent, |
512 | const char *name, const void *ns) | |
fd7b9f7b | 513 | { |
324a56e1 | 514 | struct kernfs_node *kn; |
fd7b9f7b | 515 | |
a797bfc3 | 516 | mutex_lock(&kernfs_mutex); |
324a56e1 TH |
517 | kn = kernfs_find_ns(parent, name, ns); |
518 | kernfs_get(kn); | |
a797bfc3 | 519 | mutex_unlock(&kernfs_mutex); |
fd7b9f7b | 520 | |
324a56e1 | 521 | return kn; |
fd7b9f7b TH |
522 | } |
523 | EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns); | |
524 | ||
ba7443bc TH |
525 | /** |
526 | * kernfs_create_root - create a new kernfs hierarchy | |
80b9bbef | 527 | * @kdops: optional directory syscall operations for the hierarchy |
ba7443bc TH |
528 | * @priv: opaque data associated with the new directory |
529 | * | |
530 | * Returns the root of the new hierarchy on success, ERR_PTR() value on | |
531 | * failure. | |
532 | */ | |
80b9bbef | 533 | struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops, void *priv) |
ba7443bc TH |
534 | { |
535 | struct kernfs_root *root; | |
324a56e1 | 536 | struct kernfs_node *kn; |
ba7443bc TH |
537 | |
538 | root = kzalloc(sizeof(*root), GFP_KERNEL); | |
539 | if (!root) | |
540 | return ERR_PTR(-ENOMEM); | |
541 | ||
bc755553 TH |
542 | ida_init(&root->ino_ida); |
543 | ||
db4aad20 TH |
544 | kn = __kernfs_new_node(root, "", S_IFDIR | S_IRUGO | S_IXUGO, |
545 | KERNFS_DIR); | |
324a56e1 | 546 | if (!kn) { |
bc755553 | 547 | ida_destroy(&root->ino_ida); |
ba7443bc TH |
548 | kfree(root); |
549 | return ERR_PTR(-ENOMEM); | |
550 | } | |
551 | ||
798c75a0 | 552 | kn->flags &= ~KERNFS_REMOVED; |
324a56e1 | 553 | kn->priv = priv; |
adc5e8b5 | 554 | kn->dir.root = root; |
ba7443bc | 555 | |
80b9bbef | 556 | root->dir_ops = kdops; |
324a56e1 | 557 | root->kn = kn; |
abd54f02 | 558 | init_waitqueue_head(&root->deactivate_waitq); |
ba7443bc TH |
559 | |
560 | return root; | |
561 | } | |
562 | ||
563 | /** | |
564 | * kernfs_destroy_root - destroy a kernfs hierarchy | |
565 | * @root: root of the hierarchy to destroy | |
566 | * | |
567 | * Destroy the hierarchy anchored at @root by removing all existing | |
568 | * directories and destroying @root. | |
569 | */ | |
570 | void kernfs_destroy_root(struct kernfs_root *root) | |
571 | { | |
324a56e1 | 572 | kernfs_remove(root->kn); /* will also free @root */ |
ba7443bc TH |
573 | } |
574 | ||
fd7b9f7b TH |
575 | /** |
576 | * kernfs_create_dir_ns - create a directory | |
577 | * @parent: parent in which to create a new directory | |
578 | * @name: name of the new directory | |
bb8b9d09 | 579 | * @mode: mode of the new directory |
fd7b9f7b TH |
580 | * @priv: opaque data associated with the new directory |
581 | * @ns: optional namespace tag of the directory | |
582 | * | |
583 | * Returns the created node on success, ERR_PTR() value on failure. | |
584 | */ | |
324a56e1 | 585 | struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent, |
bb8b9d09 TH |
586 | const char *name, umode_t mode, |
587 | void *priv, const void *ns) | |
fd7b9f7b | 588 | { |
324a56e1 | 589 | struct kernfs_node *kn; |
fd7b9f7b TH |
590 | int rc; |
591 | ||
592 | /* allocate */ | |
db4aad20 | 593 | kn = kernfs_new_node(parent, name, mode | S_IFDIR, KERNFS_DIR); |
324a56e1 | 594 | if (!kn) |
fd7b9f7b TH |
595 | return ERR_PTR(-ENOMEM); |
596 | ||
adc5e8b5 TH |
597 | kn->dir.root = parent->dir.root; |
598 | kn->ns = ns; | |
324a56e1 | 599 | kn->priv = priv; |
fd7b9f7b TH |
600 | |
601 | /* link in */ | |
988cd7af | 602 | rc = kernfs_add_one(kn); |
fd7b9f7b | 603 | if (!rc) |
324a56e1 | 604 | return kn; |
fd7b9f7b | 605 | |
324a56e1 | 606 | kernfs_put(kn); |
fd7b9f7b TH |
607 | return ERR_PTR(rc); |
608 | } | |
609 | ||
c637b8ac TH |
610 | static struct dentry *kernfs_iop_lookup(struct inode *dir, |
611 | struct dentry *dentry, | |
612 | unsigned int flags) | |
fd7b9f7b | 613 | { |
19bbb926 | 614 | struct dentry *ret; |
324a56e1 TH |
615 | struct kernfs_node *parent = dentry->d_parent->d_fsdata; |
616 | struct kernfs_node *kn; | |
fd7b9f7b TH |
617 | struct inode *inode; |
618 | const void *ns = NULL; | |
619 | ||
a797bfc3 | 620 | mutex_lock(&kernfs_mutex); |
fd7b9f7b | 621 | |
324a56e1 | 622 | if (kernfs_ns_enabled(parent)) |
c525aadd | 623 | ns = kernfs_info(dir->i_sb)->ns; |
fd7b9f7b | 624 | |
324a56e1 | 625 | kn = kernfs_find_ns(parent, dentry->d_name.name, ns); |
fd7b9f7b TH |
626 | |
627 | /* no such entry */ | |
324a56e1 | 628 | if (!kn) { |
19bbb926 | 629 | ret = NULL; |
fd7b9f7b TH |
630 | goto out_unlock; |
631 | } | |
324a56e1 TH |
632 | kernfs_get(kn); |
633 | dentry->d_fsdata = kn; | |
fd7b9f7b TH |
634 | |
635 | /* attach dentry and inode */ | |
c637b8ac | 636 | inode = kernfs_get_inode(dir->i_sb, kn); |
fd7b9f7b TH |
637 | if (!inode) { |
638 | ret = ERR_PTR(-ENOMEM); | |
639 | goto out_unlock; | |
640 | } | |
641 | ||
642 | /* instantiate and hash dentry */ | |
643 | ret = d_materialise_unique(dentry, inode); | |
644 | out_unlock: | |
a797bfc3 | 645 | mutex_unlock(&kernfs_mutex); |
fd7b9f7b TH |
646 | return ret; |
647 | } | |
648 | ||
80b9bbef TH |
649 | static int kernfs_iop_mkdir(struct inode *dir, struct dentry *dentry, |
650 | umode_t mode) | |
651 | { | |
652 | struct kernfs_node *parent = dir->i_private; | |
653 | struct kernfs_dir_ops *kdops = kernfs_root(parent)->dir_ops; | |
654 | ||
655 | if (!kdops || !kdops->mkdir) | |
656 | return -EPERM; | |
657 | ||
658 | return kdops->mkdir(parent, dentry->d_name.name, mode); | |
659 | } | |
660 | ||
661 | static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry) | |
662 | { | |
663 | struct kernfs_node *kn = dentry->d_fsdata; | |
664 | struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops; | |
665 | ||
666 | if (!kdops || !kdops->rmdir) | |
667 | return -EPERM; | |
668 | ||
669 | return kdops->rmdir(kn); | |
670 | } | |
671 | ||
672 | static int kernfs_iop_rename(struct inode *old_dir, struct dentry *old_dentry, | |
673 | struct inode *new_dir, struct dentry *new_dentry) | |
674 | { | |
675 | struct kernfs_node *kn = old_dentry->d_fsdata; | |
676 | struct kernfs_node *new_parent = new_dir->i_private; | |
677 | struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops; | |
678 | ||
679 | if (!kdops || !kdops->rename) | |
680 | return -EPERM; | |
681 | ||
682 | return kdops->rename(kn, new_parent, new_dentry->d_name.name); | |
683 | } | |
684 | ||
a797bfc3 | 685 | const struct inode_operations kernfs_dir_iops = { |
c637b8ac TH |
686 | .lookup = kernfs_iop_lookup, |
687 | .permission = kernfs_iop_permission, | |
688 | .setattr = kernfs_iop_setattr, | |
689 | .getattr = kernfs_iop_getattr, | |
690 | .setxattr = kernfs_iop_setxattr, | |
691 | .removexattr = kernfs_iop_removexattr, | |
692 | .getxattr = kernfs_iop_getxattr, | |
693 | .listxattr = kernfs_iop_listxattr, | |
80b9bbef TH |
694 | |
695 | .mkdir = kernfs_iop_mkdir, | |
696 | .rmdir = kernfs_iop_rmdir, | |
697 | .rename = kernfs_iop_rename, | |
fd7b9f7b TH |
698 | }; |
699 | ||
c637b8ac | 700 | static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos) |
fd7b9f7b | 701 | { |
324a56e1 | 702 | struct kernfs_node *last; |
fd7b9f7b TH |
703 | |
704 | while (true) { | |
705 | struct rb_node *rbn; | |
706 | ||
707 | last = pos; | |
708 | ||
df23fc39 | 709 | if (kernfs_type(pos) != KERNFS_DIR) |
fd7b9f7b TH |
710 | break; |
711 | ||
adc5e8b5 | 712 | rbn = rb_first(&pos->dir.children); |
fd7b9f7b TH |
713 | if (!rbn) |
714 | break; | |
715 | ||
324a56e1 | 716 | pos = rb_to_kn(rbn); |
fd7b9f7b TH |
717 | } |
718 | ||
719 | return last; | |
720 | } | |
721 | ||
722 | /** | |
c637b8ac | 723 | * kernfs_next_descendant_post - find the next descendant for post-order walk |
fd7b9f7b | 724 | * @pos: the current position (%NULL to initiate traversal) |
324a56e1 | 725 | * @root: kernfs_node whose descendants to walk |
fd7b9f7b TH |
726 | * |
727 | * Find the next descendant to visit for post-order traversal of @root's | |
728 | * descendants. @root is included in the iteration and the last node to be | |
729 | * visited. | |
730 | */ | |
c637b8ac TH |
731 | static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos, |
732 | struct kernfs_node *root) | |
fd7b9f7b TH |
733 | { |
734 | struct rb_node *rbn; | |
735 | ||
a797bfc3 | 736 | lockdep_assert_held(&kernfs_mutex); |
fd7b9f7b TH |
737 | |
738 | /* if first iteration, visit leftmost descendant which may be root */ | |
739 | if (!pos) | |
c637b8ac | 740 | return kernfs_leftmost_descendant(root); |
fd7b9f7b TH |
741 | |
742 | /* if we visited @root, we're done */ | |
743 | if (pos == root) | |
744 | return NULL; | |
745 | ||
746 | /* if there's an unvisited sibling, visit its leftmost descendant */ | |
adc5e8b5 | 747 | rbn = rb_next(&pos->rb); |
fd7b9f7b | 748 | if (rbn) |
c637b8ac | 749 | return kernfs_leftmost_descendant(rb_to_kn(rbn)); |
fd7b9f7b TH |
750 | |
751 | /* no sibling left, visit parent */ | |
adc5e8b5 | 752 | return pos->parent; |
fd7b9f7b TH |
753 | } |
754 | ||
988cd7af | 755 | static void __kernfs_remove(struct kernfs_node *kn) |
fd7b9f7b | 756 | { |
35beab06 TH |
757 | struct kernfs_node *pos; |
758 | ||
759 | lockdep_assert_held(&kernfs_mutex); | |
fd7b9f7b | 760 | |
ce9b499c GKH |
761 | if (!kn) |
762 | return; | |
763 | ||
c637b8ac | 764 | pr_debug("kernfs %s: removing\n", kn->name); |
fd7b9f7b | 765 | |
35beab06 TH |
766 | /* disable lookup and node creation under @kn */ |
767 | pos = NULL; | |
768 | while ((pos = kernfs_next_descendant_post(pos, kn))) | |
769 | pos->flags |= KERNFS_REMOVED; | |
770 | ||
771 | /* deactivate and unlink the subtree node-by-node */ | |
fd7b9f7b | 772 | do { |
35beab06 TH |
773 | pos = kernfs_leftmost_descendant(kn); |
774 | ||
775 | /* | |
776 | * kernfs_deactivate() drops kernfs_mutex temporarily and | |
777 | * @pos's base ref could have been put by someone else by | |
778 | * the time the function returns. Make sure it doesn't go | |
779 | * away underneath us. | |
780 | */ | |
781 | kernfs_get(pos); | |
782 | ||
783 | kernfs_deactivate(pos); | |
784 | ||
785 | /* | |
786 | * kernfs_unlink_sibling() succeeds once per node. Use it | |
787 | * to decide who's responsible for cleanups. | |
788 | */ | |
789 | if (!pos->parent || kernfs_unlink_sibling(pos)) { | |
790 | struct kernfs_iattrs *ps_iattr = | |
791 | pos->parent ? pos->parent->iattr : NULL; | |
792 | ||
793 | /* update timestamps on the parent */ | |
794 | if (ps_iattr) { | |
795 | ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME; | |
796 | ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME; | |
797 | } | |
798 | ||
988cd7af | 799 | kernfs_put(pos); |
35beab06 TH |
800 | } |
801 | ||
802 | kernfs_put(pos); | |
803 | } while (pos != kn); | |
fd7b9f7b TH |
804 | } |
805 | ||
806 | /** | |
324a56e1 TH |
807 | * kernfs_remove - remove a kernfs_node recursively |
808 | * @kn: the kernfs_node to remove | |
fd7b9f7b | 809 | * |
324a56e1 | 810 | * Remove @kn along with all its subdirectories and files. |
fd7b9f7b | 811 | */ |
324a56e1 | 812 | void kernfs_remove(struct kernfs_node *kn) |
fd7b9f7b | 813 | { |
988cd7af TH |
814 | mutex_lock(&kernfs_mutex); |
815 | __kernfs_remove(kn); | |
816 | mutex_unlock(&kernfs_mutex); | |
fd7b9f7b TH |
817 | } |
818 | ||
819 | /** | |
324a56e1 TH |
820 | * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it |
821 | * @parent: parent of the target | |
822 | * @name: name of the kernfs_node to remove | |
823 | * @ns: namespace tag of the kernfs_node to remove | |
fd7b9f7b | 824 | * |
324a56e1 TH |
825 | * Look for the kernfs_node with @name and @ns under @parent and remove it. |
826 | * Returns 0 on success, -ENOENT if such entry doesn't exist. | |
fd7b9f7b | 827 | */ |
324a56e1 | 828 | int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name, |
fd7b9f7b TH |
829 | const void *ns) |
830 | { | |
324a56e1 | 831 | struct kernfs_node *kn; |
fd7b9f7b | 832 | |
324a56e1 | 833 | if (!parent) { |
c637b8ac | 834 | WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n", |
fd7b9f7b TH |
835 | name); |
836 | return -ENOENT; | |
837 | } | |
838 | ||
988cd7af | 839 | mutex_lock(&kernfs_mutex); |
fd7b9f7b | 840 | |
324a56e1 TH |
841 | kn = kernfs_find_ns(parent, name, ns); |
842 | if (kn) | |
988cd7af | 843 | __kernfs_remove(kn); |
fd7b9f7b | 844 | |
988cd7af | 845 | mutex_unlock(&kernfs_mutex); |
fd7b9f7b | 846 | |
324a56e1 | 847 | if (kn) |
fd7b9f7b TH |
848 | return 0; |
849 | else | |
850 | return -ENOENT; | |
851 | } | |
852 | ||
853 | /** | |
854 | * kernfs_rename_ns - move and rename a kernfs_node | |
324a56e1 | 855 | * @kn: target node |
fd7b9f7b TH |
856 | * @new_parent: new parent to put @sd under |
857 | * @new_name: new name | |
858 | * @new_ns: new namespace tag | |
859 | */ | |
324a56e1 | 860 | int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent, |
fd7b9f7b TH |
861 | const char *new_name, const void *new_ns) |
862 | { | |
863 | int error; | |
864 | ||
798c75a0 GKH |
865 | mutex_lock(&kernfs_mutex); |
866 | ||
d0ae3d43 | 867 | error = -ENOENT; |
798c75a0 | 868 | if ((kn->flags | new_parent->flags) & KERNFS_REMOVED) |
d0ae3d43 TH |
869 | goto out; |
870 | ||
fd7b9f7b | 871 | error = 0; |
adc5e8b5 TH |
872 | if ((kn->parent == new_parent) && (kn->ns == new_ns) && |
873 | (strcmp(kn->name, new_name) == 0)) | |
798c75a0 | 874 | goto out; /* nothing to rename */ |
fd7b9f7b TH |
875 | |
876 | error = -EEXIST; | |
877 | if (kernfs_find_ns(new_parent, new_name, new_ns)) | |
798c75a0 | 878 | goto out; |
fd7b9f7b | 879 | |
324a56e1 | 880 | /* rename kernfs_node */ |
adc5e8b5 | 881 | if (strcmp(kn->name, new_name) != 0) { |
fd7b9f7b TH |
882 | error = -ENOMEM; |
883 | new_name = kstrdup(new_name, GFP_KERNEL); | |
884 | if (!new_name) | |
798c75a0 | 885 | goto out; |
fd7b9f7b | 886 | |
47a52e91 TH |
887 | if (kn->flags & KERNFS_STATIC_NAME) |
888 | kn->flags &= ~KERNFS_STATIC_NAME; | |
889 | else | |
890 | kfree(kn->name); | |
891 | ||
adc5e8b5 | 892 | kn->name = new_name; |
fd7b9f7b TH |
893 | } |
894 | ||
895 | /* | |
896 | * Move to the appropriate place in the appropriate directories rbtree. | |
897 | */ | |
c637b8ac | 898 | kernfs_unlink_sibling(kn); |
fd7b9f7b | 899 | kernfs_get(new_parent); |
adc5e8b5 TH |
900 | kernfs_put(kn->parent); |
901 | kn->ns = new_ns; | |
c637b8ac | 902 | kn->hash = kernfs_name_hash(kn->name, kn->ns); |
adc5e8b5 | 903 | kn->parent = new_parent; |
c637b8ac | 904 | kernfs_link_sibling(kn); |
fd7b9f7b TH |
905 | |
906 | error = 0; | |
798c75a0 | 907 | out: |
a797bfc3 | 908 | mutex_unlock(&kernfs_mutex); |
fd7b9f7b TH |
909 | return error; |
910 | } | |
911 | ||
fd7b9f7b | 912 | /* Relationship between s_mode and the DT_xxx types */ |
324a56e1 | 913 | static inline unsigned char dt_type(struct kernfs_node *kn) |
fd7b9f7b | 914 | { |
adc5e8b5 | 915 | return (kn->mode >> 12) & 15; |
fd7b9f7b TH |
916 | } |
917 | ||
c637b8ac | 918 | static int kernfs_dir_fop_release(struct inode *inode, struct file *filp) |
fd7b9f7b TH |
919 | { |
920 | kernfs_put(filp->private_data); | |
921 | return 0; | |
922 | } | |
923 | ||
c637b8ac | 924 | static struct kernfs_node *kernfs_dir_pos(const void *ns, |
324a56e1 | 925 | struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos) |
fd7b9f7b TH |
926 | { |
927 | if (pos) { | |
798c75a0 GKH |
928 | int valid = !(pos->flags & KERNFS_REMOVED) && |
929 | pos->parent == parent && hash == pos->hash; | |
fd7b9f7b TH |
930 | kernfs_put(pos); |
931 | if (!valid) | |
932 | pos = NULL; | |
933 | } | |
934 | if (!pos && (hash > 1) && (hash < INT_MAX)) { | |
adc5e8b5 | 935 | struct rb_node *node = parent->dir.children.rb_node; |
fd7b9f7b | 936 | while (node) { |
324a56e1 | 937 | pos = rb_to_kn(node); |
fd7b9f7b | 938 | |
adc5e8b5 | 939 | if (hash < pos->hash) |
fd7b9f7b | 940 | node = node->rb_left; |
adc5e8b5 | 941 | else if (hash > pos->hash) |
fd7b9f7b TH |
942 | node = node->rb_right; |
943 | else | |
944 | break; | |
945 | } | |
946 | } | |
947 | /* Skip over entries in the wrong namespace */ | |
adc5e8b5 TH |
948 | while (pos && pos->ns != ns) { |
949 | struct rb_node *node = rb_next(&pos->rb); | |
fd7b9f7b TH |
950 | if (!node) |
951 | pos = NULL; | |
952 | else | |
324a56e1 | 953 | pos = rb_to_kn(node); |
fd7b9f7b TH |
954 | } |
955 | return pos; | |
956 | } | |
957 | ||
c637b8ac | 958 | static struct kernfs_node *kernfs_dir_next_pos(const void *ns, |
324a56e1 | 959 | struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos) |
fd7b9f7b | 960 | { |
c637b8ac | 961 | pos = kernfs_dir_pos(ns, parent, ino, pos); |
fd7b9f7b TH |
962 | if (pos) |
963 | do { | |
adc5e8b5 | 964 | struct rb_node *node = rb_next(&pos->rb); |
fd7b9f7b TH |
965 | if (!node) |
966 | pos = NULL; | |
967 | else | |
324a56e1 | 968 | pos = rb_to_kn(node); |
adc5e8b5 | 969 | } while (pos && pos->ns != ns); |
fd7b9f7b TH |
970 | return pos; |
971 | } | |
972 | ||
c637b8ac | 973 | static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx) |
fd7b9f7b TH |
974 | { |
975 | struct dentry *dentry = file->f_path.dentry; | |
324a56e1 TH |
976 | struct kernfs_node *parent = dentry->d_fsdata; |
977 | struct kernfs_node *pos = file->private_data; | |
fd7b9f7b TH |
978 | const void *ns = NULL; |
979 | ||
980 | if (!dir_emit_dots(file, ctx)) | |
981 | return 0; | |
a797bfc3 | 982 | mutex_lock(&kernfs_mutex); |
fd7b9f7b | 983 | |
324a56e1 | 984 | if (kernfs_ns_enabled(parent)) |
c525aadd | 985 | ns = kernfs_info(dentry->d_sb)->ns; |
fd7b9f7b | 986 | |
c637b8ac | 987 | for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos); |
fd7b9f7b | 988 | pos; |
c637b8ac | 989 | pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) { |
adc5e8b5 | 990 | const char *name = pos->name; |
fd7b9f7b TH |
991 | unsigned int type = dt_type(pos); |
992 | int len = strlen(name); | |
adc5e8b5 | 993 | ino_t ino = pos->ino; |
fd7b9f7b | 994 | |
adc5e8b5 | 995 | ctx->pos = pos->hash; |
fd7b9f7b TH |
996 | file->private_data = pos; |
997 | kernfs_get(pos); | |
998 | ||
a797bfc3 | 999 | mutex_unlock(&kernfs_mutex); |
fd7b9f7b TH |
1000 | if (!dir_emit(ctx, name, len, ino, type)) |
1001 | return 0; | |
a797bfc3 | 1002 | mutex_lock(&kernfs_mutex); |
fd7b9f7b | 1003 | } |
a797bfc3 | 1004 | mutex_unlock(&kernfs_mutex); |
fd7b9f7b TH |
1005 | file->private_data = NULL; |
1006 | ctx->pos = INT_MAX; | |
1007 | return 0; | |
1008 | } | |
1009 | ||
c637b8ac TH |
1010 | static loff_t kernfs_dir_fop_llseek(struct file *file, loff_t offset, |
1011 | int whence) | |
fd7b9f7b TH |
1012 | { |
1013 | struct inode *inode = file_inode(file); | |
1014 | loff_t ret; | |
1015 | ||
1016 | mutex_lock(&inode->i_mutex); | |
1017 | ret = generic_file_llseek(file, offset, whence); | |
1018 | mutex_unlock(&inode->i_mutex); | |
1019 | ||
1020 | return ret; | |
1021 | } | |
1022 | ||
a797bfc3 | 1023 | const struct file_operations kernfs_dir_fops = { |
fd7b9f7b | 1024 | .read = generic_read_dir, |
c637b8ac TH |
1025 | .iterate = kernfs_fop_readdir, |
1026 | .release = kernfs_dir_fop_release, | |
1027 | .llseek = kernfs_dir_fop_llseek, | |
fd7b9f7b | 1028 | }; |