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