]>
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 | ||
20 | DEFINE_MUTEX(sysfs_mutex); | |
21 | ||
adc5e8b5 | 22 | #define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb) |
fd7b9f7b | 23 | |
fd7b9f7b TH |
24 | /** |
25 | * sysfs_name_hash | |
26 | * @name: Null terminated string to hash | |
27 | * @ns: Namespace tag to hash | |
28 | * | |
29 | * Returns 31 bit hash of ns + name (so it fits in an off_t ) | |
30 | */ | |
31 | static unsigned int sysfs_name_hash(const char *name, const void *ns) | |
32 | { | |
33 | unsigned long hash = init_name_hash(); | |
34 | unsigned int len = strlen(name); | |
35 | while (len--) | |
36 | hash = partial_name_hash(*name++, hash); | |
37 | hash = (end_name_hash(hash) ^ hash_ptr((void *)ns, 31)); | |
38 | hash &= 0x7fffffffU; | |
39 | /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */ | |
40 | if (hash < 1) | |
41 | hash += 2; | |
42 | if (hash >= INT_MAX) | |
43 | hash = INT_MAX - 1; | |
44 | return hash; | |
45 | } | |
46 | ||
47 | static int sysfs_name_compare(unsigned int hash, const char *name, | |
324a56e1 | 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 | ||
324a56e1 TH |
57 | static int sysfs_sd_compare(const struct kernfs_node *left, |
58 | const struct kernfs_node *right) | |
fd7b9f7b | 59 | { |
adc5e8b5 | 60 | return sysfs_name_compare(left->hash, left->name, left->ns, right); |
fd7b9f7b TH |
61 | } |
62 | ||
63 | /** | |
324a56e1 TH |
64 | * sysfs_link_sibling - link kernfs_node into sibling rbtree |
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: | |
71 | * mutex_lock(sysfs_mutex) | |
72 | * | |
73 | * RETURNS: | |
74 | * 0 on susccess -EEXIST on failure. | |
75 | */ | |
324a56e1 | 76 | static int sysfs_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 | ||
324a56e1 | 81 | if (sysfs_type(kn) == SYSFS_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; |
324a56e1 | 90 | result = sysfs_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 | /** | |
324a56e1 TH |
105 | * sysfs_unlink_sibling - unlink kernfs_node from sibling rbtree |
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: | |
112 | * mutex_lock(sysfs_mutex) | |
113 | */ | |
324a56e1 | 114 | static void sysfs_unlink_sibling(struct kernfs_node *kn) |
fd7b9f7b | 115 | { |
324a56e1 | 116 | if (sysfs_type(kn) == SYSFS_DIR) |
adc5e8b5 | 117 | kn->parent->dir.subdirs--; |
fd7b9f7b | 118 | |
adc5e8b5 | 119 | rb_erase(&kn->rb, &kn->parent->dir.children); |
fd7b9f7b TH |
120 | } |
121 | ||
122 | /** | |
324a56e1 TH |
123 | * sysfs_get_active - get an active reference to kernfs_node |
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 | */ |
324a56e1 | 132 | struct kernfs_node *sysfs_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 | ||
adc5e8b5 | 140 | if (kn->flags & SYSFS_FLAG_LOCKDEP) |
324a56e1 TH |
141 | rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_); |
142 | return kn; | |
fd7b9f7b TH |
143 | } |
144 | ||
145 | /** | |
324a56e1 TH |
146 | * sysfs_put_active - put an active reference to kernfs_node |
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 | */ | |
324a56e1 | 152 | void sysfs_put_active(struct kernfs_node *kn) |
fd7b9f7b TH |
153 | { |
154 | int v; | |
155 | ||
324a56e1 | 156 | if (unlikely(!kn)) |
fd7b9f7b TH |
157 | return; |
158 | ||
adc5e8b5 | 159 | if (kn->flags & SYSFS_FLAG_LOCKDEP) |
324a56e1 | 160 | rwsem_release(&kn->dep_map, 1, _RET_IP_); |
adc5e8b5 | 161 | v = atomic_dec_return(&kn->active); |
fd7b9f7b TH |
162 | if (likely(v != SD_DEACTIVATED_BIAS)) |
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 | /** | |
324a56e1 TH |
173 | * sysfs_deactivate - deactivate kernfs_node |
174 | * @kn: kernfs_node to deactivate | |
fd7b9f7b TH |
175 | * |
176 | * Deny new active references and drain existing ones. | |
177 | */ | |
324a56e1 | 178 | static void sysfs_deactivate(struct kernfs_node *kn) |
fd7b9f7b TH |
179 | { |
180 | DECLARE_COMPLETION_ONSTACK(wait); | |
181 | int v; | |
182 | ||
adc5e8b5 | 183 | BUG_ON(!(kn->flags & SYSFS_FLAG_REMOVED)); |
fd7b9f7b | 184 | |
324a56e1 | 185 | if (!(sysfs_type(kn) & SYSFS_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 | */ |
adc5e8b5 | 194 | v = atomic_add_return(SD_DEACTIVATED_BIAS, &kn->active); |
fd7b9f7b TH |
195 | |
196 | if (v != SD_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 | |
adc5e8b5 | 238 | WARN(!(kn->flags & SYSFS_FLAG_REMOVED), |
fd7b9f7b | 239 | "sysfs: free using entry: %s/%s\n", |
adc5e8b5 | 240 | parent ? parent->name : "", kn->name); |
324a56e1 TH |
241 | |
242 | if (sysfs_type(kn) == SYSFS_KOBJ_LINK) | |
adc5e8b5 | 243 | kernfs_put(kn->symlink.target_kn); |
324a56e1 | 244 | if (sysfs_type(kn) & SYSFS_COPY_NAME) |
adc5e8b5 TH |
245 | kfree(kn->name); |
246 | if (kn->iattr) { | |
247 | if (kn->iattr->ia_secdata) | |
248 | security_release_secctx(kn->iattr->ia_secdata, | |
249 | kn->iattr->ia_secdata_len); | |
250 | simple_xattrs_free(&kn->iattr->xattrs); | |
2322392b | 251 | } |
adc5e8b5 TH |
252 | kfree(kn->iattr); |
253 | ida_simple_remove(&root->ino_ida, kn->ino); | |
324a56e1 | 254 | kmem_cache_free(sysfs_dir_cachep, kn); |
fd7b9f7b | 255 | |
324a56e1 TH |
256 | kn = parent; |
257 | if (kn) { | |
adc5e8b5 | 258 | if (atomic_dec_and_test(&kn->count)) |
ba7443bc TH |
259 | goto repeat; |
260 | } else { | |
324a56e1 | 261 | /* just released the root kn, free @root too */ |
bc755553 | 262 | ida_destroy(&root->ino_ida); |
ba7443bc TH |
263 | kfree(root); |
264 | } | |
fd7b9f7b TH |
265 | } |
266 | EXPORT_SYMBOL_GPL(kernfs_put); | |
267 | ||
268 | static int sysfs_dentry_delete(const struct dentry *dentry) | |
269 | { | |
324a56e1 | 270 | struct kernfs_node *kn = dentry->d_fsdata; |
adc5e8b5 | 271 | return !(kn && !(kn->flags & SYSFS_FLAG_REMOVED)); |
fd7b9f7b TH |
272 | } |
273 | ||
274 | static int sysfs_dentry_revalidate(struct dentry *dentry, unsigned int flags) | |
275 | { | |
324a56e1 | 276 | struct kernfs_node *kn; |
fd7b9f7b TH |
277 | |
278 | if (flags & LOOKUP_RCU) | |
279 | return -ECHILD; | |
280 | ||
324a56e1 | 281 | kn = dentry->d_fsdata; |
fd7b9f7b TH |
282 | mutex_lock(&sysfs_mutex); |
283 | ||
284 | /* The sysfs dirent has been deleted */ | |
adc5e8b5 | 285 | if (kn->flags & SYSFS_FLAG_REMOVED) |
fd7b9f7b TH |
286 | goto out_bad; |
287 | ||
288 | /* The sysfs dirent has been moved? */ | |
adc5e8b5 | 289 | if (dentry->d_parent->d_fsdata != kn->parent) |
fd7b9f7b TH |
290 | goto out_bad; |
291 | ||
292 | /* The sysfs dirent has been renamed */ | |
adc5e8b5 | 293 | if (strcmp(dentry->d_name.name, kn->name) != 0) |
fd7b9f7b TH |
294 | goto out_bad; |
295 | ||
296 | /* The sysfs dirent has been moved to a different namespace */ | |
adc5e8b5 TH |
297 | if (kn->parent && kernfs_ns_enabled(kn->parent) && |
298 | sysfs_info(dentry->d_sb)->ns != kn->ns) | |
fd7b9f7b TH |
299 | goto out_bad; |
300 | ||
301 | mutex_unlock(&sysfs_mutex); | |
302 | out_valid: | |
303 | return 1; | |
304 | out_bad: | |
305 | /* Remove the dentry from the dcache hashes. | |
306 | * If this is a deleted dentry we use d_drop instead of d_delete | |
307 | * so sysfs doesn't need to cope with negative dentries. | |
308 | * | |
309 | * If this is a dentry that has simply been renamed we | |
310 | * use d_drop to remove it from the dcache lookup on its | |
311 | * old parent. If this dentry persists later when a lookup | |
312 | * is performed at its new name the dentry will be readded | |
313 | * to the dcache hashes. | |
314 | */ | |
315 | mutex_unlock(&sysfs_mutex); | |
316 | ||
317 | /* If we have submounts we must allow the vfs caches | |
318 | * to lie about the state of the filesystem to prevent | |
319 | * leaks and other nasty things. | |
320 | */ | |
321 | if (check_submounts_and_drop(dentry) != 0) | |
322 | goto out_valid; | |
323 | ||
324 | return 0; | |
325 | } | |
326 | ||
327 | static void sysfs_dentry_release(struct dentry *dentry) | |
328 | { | |
329 | kernfs_put(dentry->d_fsdata); | |
330 | } | |
331 | ||
332 | const struct dentry_operations sysfs_dentry_ops = { | |
333 | .d_revalidate = sysfs_dentry_revalidate, | |
334 | .d_delete = sysfs_dentry_delete, | |
335 | .d_release = sysfs_dentry_release, | |
336 | }; | |
337 | ||
324a56e1 TH |
338 | struct kernfs_node *sysfs_new_dirent(struct kernfs_root *root, |
339 | const char *name, umode_t mode, int type) | |
fd7b9f7b TH |
340 | { |
341 | char *dup_name = NULL; | |
324a56e1 | 342 | struct kernfs_node *kn; |
bc755553 | 343 | int ret; |
fd7b9f7b TH |
344 | |
345 | if (type & SYSFS_COPY_NAME) { | |
346 | name = dup_name = kstrdup(name, GFP_KERNEL); | |
347 | if (!name) | |
348 | return NULL; | |
349 | } | |
350 | ||
324a56e1 TH |
351 | kn = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL); |
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; | |
365 | kn->flags = type | SYSFS_FLAG_REMOVED; | |
fd7b9f7b | 366 | |
324a56e1 | 367 | return kn; |
fd7b9f7b TH |
368 | |
369 | err_out2: | |
324a56e1 | 370 | kmem_cache_free(sysfs_dir_cachep, kn); |
fd7b9f7b TH |
371 | err_out1: |
372 | kfree(dup_name); | |
373 | return NULL; | |
374 | } | |
375 | ||
376 | /** | |
324a56e1 | 377 | * sysfs_addrm_start - prepare for kernfs_node add/remove |
fd7b9f7b TH |
378 | * @acxt: pointer to sysfs_addrm_cxt to be used |
379 | * | |
380 | * This function is called when the caller is about to add or remove | |
324a56e1 TH |
381 | * kernfs_node. This function acquires sysfs_mutex. @acxt is used to |
382 | * keep and pass context to other addrm functions. | |
fd7b9f7b TH |
383 | * |
384 | * LOCKING: | |
385 | * Kernel thread context (may sleep). sysfs_mutex is locked on | |
386 | * return. | |
387 | */ | |
388 | void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt) | |
389 | __acquires(sysfs_mutex) | |
390 | { | |
391 | memset(acxt, 0, sizeof(*acxt)); | |
392 | ||
393 | mutex_lock(&sysfs_mutex); | |
394 | } | |
395 | ||
396 | /** | |
324a56e1 | 397 | * sysfs_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 | |
407 | * sysfs_addrm_start() and sysfs_addrm_finish() and should be | |
408 | * passed the same @acxt as passed to sysfs_addrm_start(). | |
409 | * | |
410 | * LOCKING: | |
411 | * Determined by sysfs_addrm_start(). | |
412 | * | |
413 | * RETURNS: | |
414 | * 0 on success, -EEXIST if entry with the given name already | |
415 | * exists. | |
416 | */ | |
324a56e1 TH |
417 | int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct kernfs_node *kn, |
418 | struct kernfs_node *parent) | |
fd7b9f7b | 419 | { |
324a56e1 | 420 | bool has_ns = kernfs_ns_enabled(parent); |
fd7b9f7b TH |
421 | struct sysfs_inode_attrs *ps_iattr; |
422 | int ret; | |
423 | ||
adc5e8b5 | 424 | if (has_ns != (bool)kn->ns) { |
fd7b9f7b | 425 | WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n", |
adc5e8b5 | 426 | has_ns ? "required" : "invalid", parent->name, kn->name); |
fd7b9f7b TH |
427 | return -EINVAL; |
428 | } | |
429 | ||
324a56e1 | 430 | if (sysfs_type(parent) != SYSFS_DIR) |
fd7b9f7b TH |
431 | return -EINVAL; |
432 | ||
adc5e8b5 TH |
433 | kn->hash = sysfs_name_hash(kn->name, kn->ns); |
434 | kn->parent = parent; | |
324a56e1 | 435 | kernfs_get(parent); |
fd7b9f7b | 436 | |
324a56e1 | 437 | ret = sysfs_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 */ | |
adc5e8b5 | 449 | kn->flags &= ~SYSFS_FLAG_REMOVED; |
fd7b9f7b TH |
450 | |
451 | return 0; | |
452 | } | |
453 | ||
454 | /** | |
324a56e1 | 455 | * sysfs_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 | |
463 | * sysfs_addrm_start() and sysfs_addrm_finish() and should be | |
464 | * passed the same @acxt as passed to sysfs_addrm_start(). | |
465 | * | |
466 | * LOCKING: | |
467 | * Determined by sysfs_addrm_start(). | |
468 | */ | |
469 | static void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, | |
324a56e1 | 470 | struct kernfs_node *kn) |
fd7b9f7b TH |
471 | { |
472 | struct sysfs_inode_attrs *ps_iattr; | |
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 | */ | |
adc5e8b5 | 478 | if (kn->flags & SYSFS_FLAG_REMOVED) |
fd7b9f7b TH |
479 | return; |
480 | ||
adc5e8b5 | 481 | if (kn->parent) { |
324a56e1 | 482 | sysfs_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 | ||
adc5e8b5 | 492 | kn->flags |= SYSFS_FLAG_REMOVED; |
324a56e1 TH |
493 | kn->u.removed_list = acxt->removed; |
494 | acxt->removed = kn; | |
fd7b9f7b TH |
495 | } |
496 | ||
497 | /** | |
324a56e1 | 498 | * sysfs_addrm_finish - finish up kernfs_node add/remove |
fd7b9f7b TH |
499 | * @acxt: addrm context to finish up |
500 | * | |
324a56e1 TH |
501 | * Finish up kernfs_node add/remove. Resources acquired by |
502 | * sysfs_addrm_start() are released and removed kernfs_nodes are | |
fd7b9f7b TH |
503 | * cleaned up. |
504 | * | |
505 | * LOCKING: | |
506 | * sysfs_mutex is released. | |
507 | */ | |
508 | void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt) | |
509 | __releases(sysfs_mutex) | |
510 | { | |
511 | /* release resources acquired by sysfs_addrm_start() */ | |
512 | mutex_unlock(&sysfs_mutex); | |
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 | |
324a56e1 TH |
520 | sysfs_deactivate(kn); |
521 | sysfs_unmap_bin_file(kn); | |
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 | ||
543 | lockdep_assert_held(&sysfs_mutex); | |
544 | ||
545 | if (has_ns != (bool)ns) { | |
546 | WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n", | |
adc5e8b5 | 547 | has_ns ? "required" : "invalid", parent->name, name); |
fd7b9f7b TH |
548 | return NULL; |
549 | } | |
550 | ||
551 | hash = sysfs_name_hash(name, ns); | |
552 | while (node) { | |
324a56e1 | 553 | struct kernfs_node *kn; |
fd7b9f7b TH |
554 | int result; |
555 | ||
324a56e1 TH |
556 | kn = rb_to_kn(node); |
557 | result = sysfs_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 TH |
582 | |
583 | mutex_lock(&sysfs_mutex); | |
324a56e1 TH |
584 | kn = kernfs_find_ns(parent, name, ns); |
585 | kernfs_get(kn); | |
fd7b9f7b TH |
586 | mutex_unlock(&sysfs_mutex); |
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 | ||
324a56e1 TH |
610 | kn = sysfs_new_dirent(root, "", S_IFDIR | S_IRUGO | S_IXUGO, SYSFS_DIR); |
611 | if (!kn) { | |
bc755553 | 612 | ida_destroy(&root->ino_ida); |
ba7443bc TH |
613 | kfree(root); |
614 | return ERR_PTR(-ENOMEM); | |
615 | } | |
616 | ||
adc5e8b5 | 617 | kn->flags &= ~SYSFS_FLAG_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; | |
652 | struct sysfs_addrm_cxt acxt; | |
324a56e1 | 653 | struct kernfs_node *kn; |
fd7b9f7b TH |
654 | int rc; |
655 | ||
656 | /* allocate */ | |
324a56e1 TH |
657 | kn = sysfs_new_dirent(kernfs_root(parent), name, mode, SYSFS_DIR); |
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 */ | |
666 | sysfs_addrm_start(&acxt); | |
324a56e1 | 667 | rc = sysfs_add_one(&acxt, kn, parent); |
fd7b9f7b TH |
668 | sysfs_addrm_finish(&acxt); |
669 | ||
670 | if (!rc) | |
324a56e1 | 671 | return kn; |
fd7b9f7b | 672 | |
324a56e1 | 673 | kernfs_put(kn); |
fd7b9f7b TH |
674 | return ERR_PTR(rc); |
675 | } | |
676 | ||
677 | static struct dentry *sysfs_lookup(struct inode *dir, struct dentry *dentry, | |
678 | unsigned int flags) | |
679 | { | |
680 | struct dentry *ret = NULL; | |
324a56e1 TH |
681 | struct kernfs_node *parent = dentry->d_parent->d_fsdata; |
682 | struct kernfs_node *kn; | |
fd7b9f7b TH |
683 | struct inode *inode; |
684 | const void *ns = NULL; | |
685 | ||
686 | mutex_lock(&sysfs_mutex); | |
687 | ||
324a56e1 | 688 | if (kernfs_ns_enabled(parent)) |
fd7b9f7b TH |
689 | ns = sysfs_info(dir->i_sb)->ns; |
690 | ||
324a56e1 | 691 | kn = kernfs_find_ns(parent, dentry->d_name.name, ns); |
fd7b9f7b TH |
692 | |
693 | /* no such entry */ | |
324a56e1 | 694 | if (!kn) { |
fd7b9f7b TH |
695 | ret = ERR_PTR(-ENOENT); |
696 | goto out_unlock; | |
697 | } | |
324a56e1 TH |
698 | kernfs_get(kn); |
699 | dentry->d_fsdata = kn; | |
fd7b9f7b TH |
700 | |
701 | /* attach dentry and inode */ | |
324a56e1 | 702 | inode = sysfs_get_inode(dir->i_sb, kn); |
fd7b9f7b TH |
703 | if (!inode) { |
704 | ret = ERR_PTR(-ENOMEM); | |
705 | goto out_unlock; | |
706 | } | |
707 | ||
708 | /* instantiate and hash dentry */ | |
709 | ret = d_materialise_unique(dentry, inode); | |
710 | out_unlock: | |
711 | mutex_unlock(&sysfs_mutex); | |
712 | return ret; | |
713 | } | |
714 | ||
715 | const struct inode_operations sysfs_dir_inode_operations = { | |
716 | .lookup = sysfs_lookup, | |
717 | .permission = sysfs_permission, | |
718 | .setattr = sysfs_setattr, | |
719 | .getattr = sysfs_getattr, | |
720 | .setxattr = sysfs_setxattr, | |
2322392b TH |
721 | .removexattr = sysfs_removexattr, |
722 | .getxattr = sysfs_getxattr, | |
723 | .listxattr = sysfs_listxattr, | |
fd7b9f7b TH |
724 | }; |
725 | ||
324a56e1 | 726 | static struct kernfs_node *sysfs_leftmost_descendant(struct kernfs_node *pos) |
fd7b9f7b | 727 | { |
324a56e1 | 728 | struct kernfs_node *last; |
fd7b9f7b TH |
729 | |
730 | while (true) { | |
731 | struct rb_node *rbn; | |
732 | ||
733 | last = pos; | |
734 | ||
735 | if (sysfs_type(pos) != SYSFS_DIR) | |
736 | break; | |
737 | ||
adc5e8b5 | 738 | rbn = rb_first(&pos->dir.children); |
fd7b9f7b TH |
739 | if (!rbn) |
740 | break; | |
741 | ||
324a56e1 | 742 | pos = rb_to_kn(rbn); |
fd7b9f7b TH |
743 | } |
744 | ||
745 | return last; | |
746 | } | |
747 | ||
748 | /** | |
749 | * sysfs_next_descendant_post - find the next descendant for post-order walk | |
750 | * @pos: the current position (%NULL to initiate traversal) | |
324a56e1 | 751 | * @root: kernfs_node whose descendants to walk |
fd7b9f7b TH |
752 | * |
753 | * Find the next descendant to visit for post-order traversal of @root's | |
754 | * descendants. @root is included in the iteration and the last node to be | |
755 | * visited. | |
756 | */ | |
324a56e1 TH |
757 | static struct kernfs_node *sysfs_next_descendant_post(struct kernfs_node *pos, |
758 | struct kernfs_node *root) | |
fd7b9f7b TH |
759 | { |
760 | struct rb_node *rbn; | |
761 | ||
762 | lockdep_assert_held(&sysfs_mutex); | |
763 | ||
764 | /* if first iteration, visit leftmost descendant which may be root */ | |
765 | if (!pos) | |
766 | return sysfs_leftmost_descendant(root); | |
767 | ||
768 | /* if we visited @root, we're done */ | |
769 | if (pos == root) | |
770 | return NULL; | |
771 | ||
772 | /* if there's an unvisited sibling, visit its leftmost descendant */ | |
adc5e8b5 | 773 | rbn = rb_next(&pos->rb); |
fd7b9f7b | 774 | if (rbn) |
324a56e1 | 775 | return sysfs_leftmost_descendant(rb_to_kn(rbn)); |
fd7b9f7b TH |
776 | |
777 | /* no sibling left, visit parent */ | |
adc5e8b5 | 778 | return pos->parent; |
fd7b9f7b TH |
779 | } |
780 | ||
781 | static void __kernfs_remove(struct sysfs_addrm_cxt *acxt, | |
324a56e1 | 782 | struct kernfs_node *kn) |
fd7b9f7b | 783 | { |
324a56e1 | 784 | struct kernfs_node *pos, *next; |
fd7b9f7b | 785 | |
324a56e1 | 786 | if (!kn) |
fd7b9f7b TH |
787 | return; |
788 | ||
adc5e8b5 | 789 | pr_debug("sysfs %s: removing\n", kn->name); |
fd7b9f7b TH |
790 | |
791 | next = NULL; | |
792 | do { | |
793 | pos = next; | |
324a56e1 | 794 | next = sysfs_next_descendant_post(pos, kn); |
fd7b9f7b TH |
795 | if (pos) |
796 | sysfs_remove_one(acxt, pos); | |
797 | } while (next); | |
798 | } | |
799 | ||
800 | /** | |
324a56e1 TH |
801 | * kernfs_remove - remove a kernfs_node recursively |
802 | * @kn: the kernfs_node to remove | |
fd7b9f7b | 803 | * |
324a56e1 | 804 | * Remove @kn along with all its subdirectories and files. |
fd7b9f7b | 805 | */ |
324a56e1 | 806 | void kernfs_remove(struct kernfs_node *kn) |
fd7b9f7b TH |
807 | { |
808 | struct sysfs_addrm_cxt acxt; | |
809 | ||
810 | sysfs_addrm_start(&acxt); | |
324a56e1 | 811 | __kernfs_remove(&acxt, kn); |
fd7b9f7b TH |
812 | sysfs_addrm_finish(&acxt); |
813 | } | |
814 | ||
815 | /** | |
324a56e1 TH |
816 | * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it |
817 | * @parent: parent of the target | |
818 | * @name: name of the kernfs_node to remove | |
819 | * @ns: namespace tag of the kernfs_node to remove | |
fd7b9f7b | 820 | * |
324a56e1 TH |
821 | * Look for the kernfs_node with @name and @ns under @parent and remove it. |
822 | * Returns 0 on success, -ENOENT if such entry doesn't exist. | |
fd7b9f7b | 823 | */ |
324a56e1 | 824 | int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name, |
fd7b9f7b TH |
825 | const void *ns) |
826 | { | |
827 | struct sysfs_addrm_cxt acxt; | |
324a56e1 | 828 | struct kernfs_node *kn; |
fd7b9f7b | 829 | |
324a56e1 | 830 | if (!parent) { |
fd7b9f7b TH |
831 | WARN(1, KERN_WARNING "sysfs: can not remove '%s', no directory\n", |
832 | name); | |
833 | return -ENOENT; | |
834 | } | |
835 | ||
836 | sysfs_addrm_start(&acxt); | |
837 | ||
324a56e1 TH |
838 | kn = kernfs_find_ns(parent, name, ns); |
839 | if (kn) | |
840 | __kernfs_remove(&acxt, kn); | |
fd7b9f7b TH |
841 | |
842 | sysfs_addrm_finish(&acxt); | |
843 | ||
324a56e1 | 844 | if (kn) |
fd7b9f7b TH |
845 | return 0; |
846 | else | |
847 | return -ENOENT; | |
848 | } | |
849 | ||
850 | /** | |
851 | * kernfs_rename_ns - move and rename a kernfs_node | |
324a56e1 | 852 | * @kn: target node |
fd7b9f7b TH |
853 | * @new_parent: new parent to put @sd under |
854 | * @new_name: new name | |
855 | * @new_ns: new namespace tag | |
856 | */ | |
324a56e1 | 857 | int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent, |
fd7b9f7b TH |
858 | const char *new_name, const void *new_ns) |
859 | { | |
860 | int error; | |
861 | ||
862 | mutex_lock(&sysfs_mutex); | |
863 | ||
864 | error = 0; | |
adc5e8b5 TH |
865 | if ((kn->parent == new_parent) && (kn->ns == new_ns) && |
866 | (strcmp(kn->name, new_name) == 0)) | |
fd7b9f7b TH |
867 | goto out; /* nothing to rename */ |
868 | ||
869 | error = -EEXIST; | |
870 | if (kernfs_find_ns(new_parent, new_name, new_ns)) | |
871 | goto out; | |
872 | ||
324a56e1 | 873 | /* rename kernfs_node */ |
adc5e8b5 | 874 | if (strcmp(kn->name, new_name) != 0) { |
fd7b9f7b TH |
875 | error = -ENOMEM; |
876 | new_name = kstrdup(new_name, GFP_KERNEL); | |
877 | if (!new_name) | |
878 | goto out; | |
879 | ||
adc5e8b5 TH |
880 | kfree(kn->name); |
881 | kn->name = new_name; | |
fd7b9f7b TH |
882 | } |
883 | ||
884 | /* | |
885 | * Move to the appropriate place in the appropriate directories rbtree. | |
886 | */ | |
324a56e1 | 887 | sysfs_unlink_sibling(kn); |
fd7b9f7b | 888 | kernfs_get(new_parent); |
adc5e8b5 TH |
889 | kernfs_put(kn->parent); |
890 | kn->ns = new_ns; | |
891 | kn->hash = sysfs_name_hash(kn->name, kn->ns); | |
892 | kn->parent = new_parent; | |
324a56e1 | 893 | sysfs_link_sibling(kn); |
fd7b9f7b TH |
894 | |
895 | error = 0; | |
896 | out: | |
897 | mutex_unlock(&sysfs_mutex); | |
898 | return error; | |
899 | } | |
900 | ||
fd7b9f7b | 901 | /* Relationship between s_mode and the DT_xxx types */ |
324a56e1 | 902 | static inline unsigned char dt_type(struct kernfs_node *kn) |
fd7b9f7b | 903 | { |
adc5e8b5 | 904 | return (kn->mode >> 12) & 15; |
fd7b9f7b TH |
905 | } |
906 | ||
907 | static int sysfs_dir_release(struct inode *inode, struct file *filp) | |
908 | { | |
909 | kernfs_put(filp->private_data); | |
910 | return 0; | |
911 | } | |
912 | ||
324a56e1 TH |
913 | static struct kernfs_node *sysfs_dir_pos(const void *ns, |
914 | struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos) | |
fd7b9f7b TH |
915 | { |
916 | if (pos) { | |
adc5e8b5 TH |
917 | int valid = !(pos->flags & SYSFS_FLAG_REMOVED) && |
918 | pos->parent == parent && hash == pos->hash; | |
fd7b9f7b TH |
919 | kernfs_put(pos); |
920 | if (!valid) | |
921 | pos = NULL; | |
922 | } | |
923 | if (!pos && (hash > 1) && (hash < INT_MAX)) { | |
adc5e8b5 | 924 | struct rb_node *node = parent->dir.children.rb_node; |
fd7b9f7b | 925 | while (node) { |
324a56e1 | 926 | pos = rb_to_kn(node); |
fd7b9f7b | 927 | |
adc5e8b5 | 928 | if (hash < pos->hash) |
fd7b9f7b | 929 | node = node->rb_left; |
adc5e8b5 | 930 | else if (hash > pos->hash) |
fd7b9f7b TH |
931 | node = node->rb_right; |
932 | else | |
933 | break; | |
934 | } | |
935 | } | |
936 | /* Skip over entries in the wrong namespace */ | |
adc5e8b5 TH |
937 | while (pos && pos->ns != ns) { |
938 | struct rb_node *node = rb_next(&pos->rb); | |
fd7b9f7b TH |
939 | if (!node) |
940 | pos = NULL; | |
941 | else | |
324a56e1 | 942 | pos = rb_to_kn(node); |
fd7b9f7b TH |
943 | } |
944 | return pos; | |
945 | } | |
946 | ||
324a56e1 TH |
947 | static struct kernfs_node *sysfs_dir_next_pos(const void *ns, |
948 | struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos) | |
fd7b9f7b | 949 | { |
324a56e1 | 950 | pos = sysfs_dir_pos(ns, parent, ino, pos); |
fd7b9f7b TH |
951 | if (pos) |
952 | do { | |
adc5e8b5 | 953 | struct rb_node *node = rb_next(&pos->rb); |
fd7b9f7b TH |
954 | if (!node) |
955 | pos = NULL; | |
956 | else | |
324a56e1 | 957 | pos = rb_to_kn(node); |
adc5e8b5 | 958 | } while (pos && pos->ns != ns); |
fd7b9f7b TH |
959 | return pos; |
960 | } | |
961 | ||
962 | static int sysfs_readdir(struct file *file, struct dir_context *ctx) | |
963 | { | |
964 | struct dentry *dentry = file->f_path.dentry; | |
324a56e1 TH |
965 | struct kernfs_node *parent = dentry->d_fsdata; |
966 | struct kernfs_node *pos = file->private_data; | |
fd7b9f7b TH |
967 | const void *ns = NULL; |
968 | ||
969 | if (!dir_emit_dots(file, ctx)) | |
970 | return 0; | |
971 | mutex_lock(&sysfs_mutex); | |
972 | ||
324a56e1 | 973 | if (kernfs_ns_enabled(parent)) |
fd7b9f7b TH |
974 | ns = sysfs_info(dentry->d_sb)->ns; |
975 | ||
324a56e1 | 976 | for (pos = sysfs_dir_pos(ns, parent, ctx->pos, pos); |
fd7b9f7b | 977 | pos; |
324a56e1 | 978 | pos = sysfs_dir_next_pos(ns, parent, ctx->pos, pos)) { |
adc5e8b5 | 979 | const char *name = pos->name; |
fd7b9f7b TH |
980 | unsigned int type = dt_type(pos); |
981 | int len = strlen(name); | |
adc5e8b5 | 982 | ino_t ino = pos->ino; |
fd7b9f7b | 983 | |
adc5e8b5 | 984 | ctx->pos = pos->hash; |
fd7b9f7b TH |
985 | file->private_data = pos; |
986 | kernfs_get(pos); | |
987 | ||
988 | mutex_unlock(&sysfs_mutex); | |
989 | if (!dir_emit(ctx, name, len, ino, type)) | |
990 | return 0; | |
991 | mutex_lock(&sysfs_mutex); | |
992 | } | |
993 | mutex_unlock(&sysfs_mutex); | |
994 | file->private_data = NULL; | |
995 | ctx->pos = INT_MAX; | |
996 | return 0; | |
997 | } | |
998 | ||
999 | static loff_t sysfs_dir_llseek(struct file *file, loff_t offset, int whence) | |
1000 | { | |
1001 | struct inode *inode = file_inode(file); | |
1002 | loff_t ret; | |
1003 | ||
1004 | mutex_lock(&inode->i_mutex); | |
1005 | ret = generic_file_llseek(file, offset, whence); | |
1006 | mutex_unlock(&inode->i_mutex); | |
1007 | ||
1008 | return ret; | |
1009 | } | |
1010 | ||
1011 | const struct file_operations sysfs_dir_operations = { | |
1012 | .read = generic_read_dir, | |
1013 | .iterate = sysfs_readdir, | |
1014 | .release = sysfs_dir_release, | |
1015 | .llseek = sysfs_dir_llseek, | |
1016 | }; |