]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
6d66f5cd TH |
2 | * fs/sysfs/dir.c - sysfs core and dir operation implementation |
3 | * | |
4 | * Copyright (c) 2001-3 Patrick Mochel | |
5 | * Copyright (c) 2007 SUSE Linux Products GmbH | |
6 | * Copyright (c) 2007 Tejun Heo <teheo@suse.de> | |
7 | * | |
8 | * This file is released under the GPLv2. | |
9 | * | |
10 | * Please see Documentation/filesystems/sysfs.txt for more information. | |
1da177e4 LT |
11 | */ |
12 | ||
13 | #undef DEBUG | |
14 | ||
15 | #include <linux/fs.h> | |
16 | #include <linux/mount.h> | |
17 | #include <linux/module.h> | |
18 | #include <linux/kobject.h> | |
5f45f1a7 | 19 | #include <linux/namei.h> |
2b611bb7 | 20 | #include <linux/idr.h> |
8619f979 | 21 | #include <linux/completion.h> |
869512ab | 22 | #include <linux/mutex.h> |
c6f87733 | 23 | #include <linux/slab.h> |
4c3da220 | 24 | #include <linux/security.h> |
4e4d6d86 | 25 | #include <linux/hash.h> |
1da177e4 LT |
26 | #include "sysfs.h" |
27 | ||
3007e997 | 28 | DEFINE_MUTEX(sysfs_mutex); |
f7a75f0a | 29 | DEFINE_SPINLOCK(sysfs_assoc_lock); |
1da177e4 | 30 | |
bcac3769 | 31 | #define to_sysfs_dirent(X) rb_entry((X), struct sysfs_dirent, s_rb) |
4e4d6d86 | 32 | |
f7a75f0a | 33 | static DEFINE_SPINLOCK(sysfs_ino_lock); |
2b611bb7 TH |
34 | static DEFINE_IDA(sysfs_ino_ida); |
35 | ||
0c73f18b | 36 | /** |
4e4d6d86 | 37 | * sysfs_name_hash |
4e4d6d86 | 38 | * @name: Null terminated string to hash |
cfec0bc8 | 39 | * @ns: Namespace tag to hash |
4e4d6d86 EB |
40 | * |
41 | * Returns 31 bit hash of ns + name (so it fits in an off_t ) | |
42 | */ | |
cfec0bc8 | 43 | static unsigned int sysfs_name_hash(const char *name, const void *ns) |
4e4d6d86 EB |
44 | { |
45 | unsigned long hash = init_name_hash(); | |
46 | unsigned int len = strlen(name); | |
47 | while (len--) | |
48 | hash = partial_name_hash(*name++, hash); | |
1b18dc2b | 49 | hash = (end_name_hash(hash) ^ hash_ptr((void *)ns, 31)); |
4e4d6d86 EB |
50 | hash &= 0x7fffffffU; |
51 | /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */ | |
52 | if (hash < 1) | |
53 | hash += 2; | |
54 | if (hash >= INT_MAX) | |
55 | hash = INT_MAX - 1; | |
56 | return hash; | |
57 | } | |
58 | ||
cfec0bc8 TH |
59 | static int sysfs_name_compare(unsigned int hash, const char *name, |
60 | const void *ns, const struct sysfs_dirent *sd) | |
4e4d6d86 EB |
61 | { |
62 | if (hash != sd->s_hash) | |
63 | return hash - sd->s_hash; | |
64 | if (ns != sd->s_ns) | |
65 | return ns - sd->s_ns; | |
66 | return strcmp(name, sd->s_name); | |
67 | } | |
68 | ||
69 | static int sysfs_sd_compare(const struct sysfs_dirent *left, | |
70 | const struct sysfs_dirent *right) | |
71 | { | |
cfec0bc8 | 72 | return sysfs_name_compare(left->s_hash, left->s_name, left->s_ns, |
4e4d6d86 EB |
73 | right); |
74 | } | |
75 | ||
76 | /** | |
43474910 | 77 | * sysfs_link_sibling - link sysfs_dirent into sibling rbtree |
0c73f18b TH |
78 | * @sd: sysfs_dirent of interest |
79 | * | |
4e4d6d86 | 80 | * Link @sd into its sibling rbtree which starts from |
bc747f37 | 81 | * sd->s_parent->s_dir.children. |
0c73f18b TH |
82 | * |
83 | * Locking: | |
3007e997 | 84 | * mutex_lock(sysfs_mutex) |
4e4d6d86 EB |
85 | * |
86 | * RETURNS: | |
87 | * 0 on susccess -EEXIST on failure. | |
0c73f18b | 88 | */ |
4e4d6d86 | 89 | static int sysfs_link_sibling(struct sysfs_dirent *sd) |
0c73f18b | 90 | { |
4e4d6d86 EB |
91 | struct rb_node **node = &sd->s_parent->s_dir.children.rb_node; |
92 | struct rb_node *parent = NULL; | |
4f72c0ca | 93 | |
54d20f00 GKH |
94 | if (sysfs_type(sd) == SYSFS_DIR) |
95 | sd->s_parent->s_dir.subdirs++; | |
96 | ||
4e4d6d86 EB |
97 | while (*node) { |
98 | struct sysfs_dirent *pos; | |
99 | int result; | |
100 | ||
101 | pos = to_sysfs_dirent(*node); | |
102 | parent = *node; | |
103 | result = sysfs_sd_compare(sd, pos); | |
104 | if (result < 0) | |
105 | node = &pos->s_rb.rb_left; | |
106 | else if (result > 0) | |
107 | node = &pos->s_rb.rb_right; | |
108 | else | |
109 | return -EEXIST; | |
4f72c0ca | 110 | } |
4e4d6d86 EB |
111 | /* add new node and rebalance the tree */ |
112 | rb_link_node(&sd->s_rb, parent, node); | |
113 | rb_insert_color(&sd->s_rb, &sd->s_parent->s_dir.children); | |
cb26a311 TH |
114 | |
115 | /* if @sd has ns tag, mark the parent to enable ns filtering */ | |
116 | if (sd->s_ns) | |
117 | sd->s_parent->s_flags |= SYSFS_FLAG_HAS_NS; | |
118 | ||
4e4d6d86 | 119 | return 0; |
0c73f18b TH |
120 | } |
121 | ||
122 | /** | |
4e4d6d86 | 123 | * sysfs_unlink_sibling - unlink sysfs_dirent from sibling rbtree |
0c73f18b TH |
124 | * @sd: sysfs_dirent of interest |
125 | * | |
4e4d6d86 | 126 | * Unlink @sd from its sibling rbtree which starts from |
bc747f37 | 127 | * sd->s_parent->s_dir.children. |
0c73f18b TH |
128 | * |
129 | * Locking: | |
3007e997 | 130 | * mutex_lock(sysfs_mutex) |
0c73f18b | 131 | */ |
41fc1c27 | 132 | static void sysfs_unlink_sibling(struct sysfs_dirent *sd) |
0c73f18b | 133 | { |
54d20f00 GKH |
134 | if (sysfs_type(sd) == SYSFS_DIR) |
135 | sd->s_parent->s_dir.subdirs--; | |
136 | ||
4e4d6d86 | 137 | rb_erase(&sd->s_rb, &sd->s_parent->s_dir.children); |
cb26a311 TH |
138 | |
139 | /* | |
140 | * Either all or none of the children have tags. Clearing HAS_NS | |
141 | * when there's no child left is enough to keep the flag synced. | |
142 | */ | |
143 | if (RB_EMPTY_ROOT(&sd->s_parent->s_dir.children)) | |
144 | sd->s_parent->s_flags &= ~SYSFS_FLAG_HAS_NS; | |
0c73f18b TH |
145 | } |
146 | ||
b6b4a439 TH |
147 | /** |
148 | * sysfs_get_active - get an active reference to sysfs_dirent | |
149 | * @sd: sysfs_dirent to get an active reference to | |
150 | * | |
151 | * Get an active reference of @sd. This function is noop if @sd | |
152 | * is NULL. | |
153 | * | |
154 | * RETURNS: | |
155 | * Pointer to @sd on success, NULL on failure. | |
156 | */ | |
e72ceb8c | 157 | struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd) |
b6b4a439 | 158 | { |
8619f979 TH |
159 | if (unlikely(!sd)) |
160 | return NULL; | |
161 | ||
3db3c625 ML |
162 | if (!atomic_inc_unless_negative(&sd->s_active)) |
163 | return NULL; | |
356c05d5 | 164 | |
785a162d | 165 | if (likely(!sysfs_ignore_lockdep(sd))) |
356c05d5 AS |
166 | rwsem_acquire_read(&sd->dep_map, 0, 1, _RET_IP_); |
167 | return sd; | |
b6b4a439 TH |
168 | } |
169 | ||
170 | /** | |
171 | * sysfs_put_active - put an active reference to sysfs_dirent | |
172 | * @sd: sysfs_dirent to put an active reference to | |
173 | * | |
174 | * Put an active reference to @sd. This function is noop if @sd | |
175 | * is NULL. | |
176 | */ | |
e72ceb8c | 177 | void sysfs_put_active(struct sysfs_dirent *sd) |
b6b4a439 | 178 | { |
8619f979 TH |
179 | int v; |
180 | ||
181 | if (unlikely(!sd)) | |
182 | return; | |
183 | ||
785a162d | 184 | if (likely(!sysfs_ignore_lockdep(sd))) |
356c05d5 | 185 | rwsem_release(&sd->dep_map, 1, _RET_IP_); |
8619f979 TH |
186 | v = atomic_dec_return(&sd->s_active); |
187 | if (likely(v != SD_DEACTIVATED_BIAS)) | |
188 | return; | |
189 | ||
190 | /* atomic_dec_return() is a mb(), we'll always see the updated | |
58f2a4c7 | 191 | * sd->u.completion. |
8619f979 | 192 | */ |
58f2a4c7 | 193 | complete(sd->u.completion); |
b6b4a439 TH |
194 | } |
195 | ||
b6b4a439 TH |
196 | /** |
197 | * sysfs_deactivate - deactivate sysfs_dirent | |
198 | * @sd: sysfs_dirent to deactivate | |
199 | * | |
8619f979 | 200 | * Deny new active references and drain existing ones. |
b6b4a439 | 201 | */ |
fb6896da | 202 | static void sysfs_deactivate(struct sysfs_dirent *sd) |
b6b4a439 | 203 | { |
8619f979 TH |
204 | DECLARE_COMPLETION_ONSTACK(wait); |
205 | int v; | |
b6b4a439 | 206 | |
58f2a4c7 | 207 | BUG_ON(!(sd->s_flags & SYSFS_FLAG_REMOVED)); |
a2db6842 EB |
208 | |
209 | if (!(sysfs_type(sd) & SYSFS_ACTIVE_REF)) | |
210 | return; | |
211 | ||
58f2a4c7 | 212 | sd->u.completion = (void *)&wait; |
8619f979 | 213 | |
846f9974 | 214 | rwsem_acquire(&sd->dep_map, 0, 0, _RET_IP_); |
8619f979 | 215 | /* atomic_add_return() is a mb(), put_active() will always see |
58f2a4c7 | 216 | * the updated sd->u.completion. |
b6b4a439 | 217 | */ |
8619f979 TH |
218 | v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active); |
219 | ||
846f9974 EB |
220 | if (v != SD_DEACTIVATED_BIAS) { |
221 | lock_contended(&sd->dep_map, _RET_IP_); | |
8619f979 | 222 | wait_for_completion(&wait); |
846f9974 | 223 | } |
8619f979 | 224 | |
846f9974 EB |
225 | lock_acquired(&sd->dep_map, _RET_IP_); |
226 | rwsem_release(&sd->dep_map, 1, _RET_IP_); | |
b6b4a439 TH |
227 | } |
228 | ||
cafa6b5d | 229 | static int sysfs_alloc_ino(unsigned int *pino) |
2b611bb7 TH |
230 | { |
231 | int ino, rc; | |
232 | ||
233 | retry: | |
234 | spin_lock(&sysfs_ino_lock); | |
235 | rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino); | |
236 | spin_unlock(&sysfs_ino_lock); | |
237 | ||
238 | if (rc == -EAGAIN) { | |
239 | if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL)) | |
240 | goto retry; | |
241 | rc = -ENOMEM; | |
242 | } | |
243 | ||
244 | *pino = ino; | |
245 | return rc; | |
246 | } | |
247 | ||
cafa6b5d | 248 | static void sysfs_free_ino(unsigned int ino) |
2b611bb7 TH |
249 | { |
250 | spin_lock(&sysfs_ino_lock); | |
251 | ida_remove(&sysfs_ino_ida, ino); | |
252 | spin_unlock(&sysfs_ino_lock); | |
253 | } | |
254 | ||
1b18dc2b | 255 | void release_sysfs_dirent(struct sysfs_dirent *sd) |
fa7f912a | 256 | { |
13b3086d TH |
257 | struct sysfs_dirent *parent_sd; |
258 | ||
259 | repeat: | |
3007e997 TH |
260 | /* Moving/renaming is always done while holding reference. |
261 | * sd->s_parent won't change beneath us. | |
262 | */ | |
13b3086d TH |
263 | parent_sd = sd->s_parent; |
264 | ||
bb2b0051 ML |
265 | WARN(!(sd->s_flags & SYSFS_FLAG_REMOVED), |
266 | "sysfs: free using entry: %s/%s\n", | |
267 | parent_sd ? parent_sd->s_name : "", sd->s_name); | |
268 | ||
b402d72c | 269 | if (sysfs_type(sd) == SYSFS_KOBJ_LINK) |
b1fc3d61 | 270 | sysfs_put(sd->s_symlink.target_sd); |
b402d72c | 271 | if (sysfs_type(sd) & SYSFS_COPY_NAME) |
0c096b50 | 272 | kfree(sd->s_name); |
4c3da220 EB |
273 | if (sd->s_iattr && sd->s_iattr->ia_secdata) |
274 | security_release_secctx(sd->s_iattr->ia_secdata, | |
275 | sd->s_iattr->ia_secdata_len); | |
fa7f912a | 276 | kfree(sd->s_iattr); |
2b611bb7 | 277 | sysfs_free_ino(sd->s_ino); |
fa7f912a | 278 | kmem_cache_free(sysfs_dir_cachep, sd); |
13b3086d TH |
279 | |
280 | sd = parent_sd; | |
281 | if (sd && atomic_dec_and_test(&sd->s_count)) | |
282 | goto repeat; | |
fa7f912a TH |
283 | } |
284 | ||
fe15ce44 | 285 | static int sysfs_dentry_delete(const struct dentry *dentry) |
e8f077c8 EB |
286 | { |
287 | struct sysfs_dirent *sd = dentry->d_fsdata; | |
469796d1 | 288 | return !(sd && !(sd->s_flags & SYSFS_FLAG_REMOVED)); |
e8f077c8 EB |
289 | } |
290 | ||
0b728e19 | 291 | static int sysfs_dentry_revalidate(struct dentry *dentry, unsigned int flags) |
e8f077c8 | 292 | { |
34286d66 | 293 | struct sysfs_dirent *sd; |
e8f077c8 | 294 | |
0b728e19 | 295 | if (flags & LOOKUP_RCU) |
34286d66 NP |
296 | return -ECHILD; |
297 | ||
298 | sd = dentry->d_fsdata; | |
e8f077c8 EB |
299 | mutex_lock(&sysfs_mutex); |
300 | ||
301 | /* The sysfs dirent has been deleted */ | |
302 | if (sd->s_flags & SYSFS_FLAG_REMOVED) | |
303 | goto out_bad; | |
304 | ||
832b6af1 EB |
305 | /* The sysfs dirent has been moved? */ |
306 | if (dentry->d_parent->d_fsdata != sd->s_parent) | |
307 | goto out_bad; | |
308 | ||
309 | /* The sysfs dirent has been renamed */ | |
310 | if (strcmp(dentry->d_name.name, sd->s_name) != 0) | |
311 | goto out_bad; | |
312 | ||
e5bcac61 | 313 | /* The sysfs dirent has been moved to a different namespace */ |
cb26a311 TH |
314 | if (sd->s_ns && sd->s_ns != sysfs_info(dentry->d_sb)->ns) |
315 | goto out_bad; | |
e5bcac61 | 316 | |
e8f077c8 EB |
317 | mutex_unlock(&sysfs_mutex); |
318 | out_valid: | |
319 | return 1; | |
320 | out_bad: | |
321 | /* Remove the dentry from the dcache hashes. | |
322 | * If this is a deleted dentry we use d_drop instead of d_delete | |
323 | * so sysfs doesn't need to cope with negative dentries. | |
832b6af1 EB |
324 | * |
325 | * If this is a dentry that has simply been renamed we | |
326 | * use d_drop to remove it from the dcache lookup on its | |
327 | * old parent. If this dentry persists later when a lookup | |
328 | * is performed at its new name the dentry will be readded | |
329 | * to the dcache hashes. | |
e8f077c8 | 330 | */ |
e8f077c8 | 331 | mutex_unlock(&sysfs_mutex); |
6497d160 MS |
332 | |
333 | /* If we have submounts we must allow the vfs caches | |
334 | * to lie about the state of the filesystem to prevent | |
335 | * leaks and other nasty things. | |
336 | */ | |
337 | if (check_submounts_and_drop(dentry) != 0) | |
338 | goto out_valid; | |
339 | ||
e8f077c8 EB |
340 | return 0; |
341 | } | |
342 | ||
469796d1 | 343 | static void sysfs_dentry_release(struct dentry *dentry) |
1da177e4 | 344 | { |
469796d1 | 345 | sysfs_put(dentry->d_fsdata); |
1da177e4 LT |
346 | } |
347 | ||
469796d1 | 348 | const struct dentry_operations sysfs_dentry_ops = { |
e8f077c8 EB |
349 | .d_revalidate = sysfs_dentry_revalidate, |
350 | .d_delete = sysfs_dentry_delete, | |
469796d1 | 351 | .d_release = sysfs_dentry_release, |
1da177e4 LT |
352 | }; |
353 | ||
3e519038 | 354 | struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type) |
1da177e4 | 355 | { |
0c096b50 | 356 | char *dup_name = NULL; |
01da2425 | 357 | struct sysfs_dirent *sd; |
0c096b50 TH |
358 | |
359 | if (type & SYSFS_COPY_NAME) { | |
360 | name = dup_name = kstrdup(name, GFP_KERNEL); | |
361 | if (!name) | |
01da2425 | 362 | return NULL; |
0c096b50 | 363 | } |
1da177e4 | 364 | |
c3762229 | 365 | sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL); |
1da177e4 | 366 | if (!sd) |
01da2425 | 367 | goto err_out1; |
1da177e4 | 368 | |
0c096b50 | 369 | if (sysfs_alloc_ino(&sd->s_ino)) |
01da2425 | 370 | goto err_out2; |
2b611bb7 | 371 | |
1da177e4 | 372 | atomic_set(&sd->s_count, 1); |
8619f979 | 373 | atomic_set(&sd->s_active, 0); |
a26cd722 | 374 | |
0c096b50 | 375 | sd->s_name = name; |
a26cd722 | 376 | sd->s_mode = mode; |
bb2b0051 | 377 | sd->s_flags = type | SYSFS_FLAG_REMOVED; |
1da177e4 LT |
378 | |
379 | return sd; | |
0c096b50 | 380 | |
01da2425 | 381 | err_out2: |
0c096b50 | 382 | kmem_cache_free(sysfs_dir_cachep, sd); |
01da2425 AM |
383 | err_out1: |
384 | kfree(dup_name); | |
0c096b50 | 385 | return NULL; |
1da177e4 LT |
386 | } |
387 | ||
3007e997 | 388 | /** |
fb6896da TH |
389 | * sysfs_addrm_start - prepare for sysfs_dirent add/remove |
390 | * @acxt: pointer to sysfs_addrm_cxt to be used | |
3007e997 | 391 | * |
d69ac5a0 TH |
392 | * This function is called when the caller is about to add or remove |
393 | * sysfs_dirent. This function acquires sysfs_mutex. @acxt is used | |
394 | * to keep and pass context to other addrm functions. | |
3007e997 TH |
395 | * |
396 | * LOCKING: | |
fb6896da | 397 | * Kernel thread context (may sleep). sysfs_mutex is locked on |
a16bbc34 | 398 | * return. |
3007e997 | 399 | */ |
d69ac5a0 TH |
400 | void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt) |
401 | __acquires(sysfs_mutex) | |
b592fcfe | 402 | { |
fb6896da | 403 | memset(acxt, 0, sizeof(*acxt)); |
fb6896da | 404 | |
fb6896da | 405 | mutex_lock(&sysfs_mutex); |
fb6896da TH |
406 | } |
407 | ||
408 | /** | |
36ce6dad | 409 | * __sysfs_add_one - add sysfs_dirent to parent without warning |
fb6896da TH |
410 | * @acxt: addrm context to use |
411 | * @sd: sysfs_dirent to be added | |
d69ac5a0 | 412 | * @parent_sd: the parent sysfs_dirent to add @sd to |
fb6896da | 413 | * |
d69ac5a0 TH |
414 | * Get @parent_sd and set @sd->s_parent to it and increment nlink of |
415 | * the parent inode if @sd is a directory and link into the children | |
416 | * list of the parent. | |
fb6896da TH |
417 | * |
418 | * This function should be called between calls to | |
419 | * sysfs_addrm_start() and sysfs_addrm_finish() and should be | |
420 | * passed the same @acxt as passed to sysfs_addrm_start(). | |
421 | * | |
422 | * LOCKING: | |
423 | * Determined by sysfs_addrm_start(). | |
23dc2799 TH |
424 | * |
425 | * RETURNS: | |
426 | * 0 on success, -EEXIST if entry with the given name already | |
427 | * exists. | |
fb6896da | 428 | */ |
d69ac5a0 TH |
429 | int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd, |
430 | struct sysfs_dirent *parent_sd) | |
fb6896da | 431 | { |
6b0bfe93 | 432 | struct sysfs_inode_attrs *ps_iattr; |
4e4d6d86 | 433 | int ret; |
6b0bfe93 | 434 | |
cfec0bc8 | 435 | sd->s_hash = sysfs_name_hash(sd->s_name, sd->s_ns); |
d69ac5a0 | 436 | sd->s_parent = sysfs_get(parent_sd); |
fb6896da | 437 | |
4e4d6d86 EB |
438 | ret = sysfs_link_sibling(sd); |
439 | if (ret) | |
440 | return ret; | |
23dc2799 | 441 | |
6b0bfe93 | 442 | /* Update timestamps on the parent */ |
d69ac5a0 | 443 | ps_iattr = parent_sd->s_iattr; |
6b0bfe93 EB |
444 | if (ps_iattr) { |
445 | struct iattr *ps_iattrs = &ps_iattr->ia_iattr; | |
446 | ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME; | |
447 | } | |
448 | ||
bb2b0051 ML |
449 | /* Mark the entry added into directory tree */ |
450 | sd->s_flags &= ~SYSFS_FLAG_REMOVED; | |
451 | ||
23dc2799 | 452 | return 0; |
fb6896da TH |
453 | } |
454 | ||
425cb029 AC |
455 | /** |
456 | * sysfs_pathname - return full path to sysfs dirent | |
457 | * @sd: sysfs_dirent whose path we want | |
66081a72 | 458 | * @path: caller allocated buffer of size PATH_MAX |
425cb029 AC |
459 | * |
460 | * Gives the name "/" to the sysfs_root entry; any path returned | |
461 | * is relative to wherever sysfs is mounted. | |
425cb029 AC |
462 | */ |
463 | static char *sysfs_pathname(struct sysfs_dirent *sd, char *path) | |
464 | { | |
465 | if (sd->s_parent) { | |
466 | sysfs_pathname(sd->s_parent, path); | |
66081a72 | 467 | strlcat(path, "/", PATH_MAX); |
425cb029 | 468 | } |
66081a72 | 469 | strlcat(path, sd->s_name, PATH_MAX); |
425cb029 AC |
470 | return path; |
471 | } | |
472 | ||
d1c1459e TH |
473 | void sysfs_warn_dup(struct sysfs_dirent *parent, const char *name) |
474 | { | |
475 | char *path; | |
476 | ||
477 | path = kzalloc(PATH_MAX, GFP_KERNEL); | |
478 | if (path) { | |
479 | sysfs_pathname(parent, path); | |
480 | strlcat(path, "/", PATH_MAX); | |
481 | strlcat(path, name, PATH_MAX); | |
482 | } | |
483 | ||
484 | WARN(1, KERN_WARNING "sysfs: cannot create duplicate filename '%s'\n", | |
485 | path ? path : name); | |
486 | ||
487 | kfree(path); | |
488 | } | |
489 | ||
36ce6dad CH |
490 | /** |
491 | * sysfs_add_one - add sysfs_dirent to parent | |
492 | * @acxt: addrm context to use | |
493 | * @sd: sysfs_dirent to be added | |
d69ac5a0 | 494 | * @parent_sd: the parent sysfs_dirent to add @sd to |
36ce6dad | 495 | * |
d69ac5a0 TH |
496 | * Get @parent_sd and set @sd->s_parent to it and increment nlink of |
497 | * the parent inode if @sd is a directory and link into the children | |
498 | * list of the parent. | |
36ce6dad CH |
499 | * |
500 | * This function should be called between calls to | |
501 | * sysfs_addrm_start() and sysfs_addrm_finish() and should be | |
502 | * passed the same @acxt as passed to sysfs_addrm_start(). | |
503 | * | |
504 | * LOCKING: | |
505 | * Determined by sysfs_addrm_start(). | |
506 | * | |
507 | * RETURNS: | |
508 | * 0 on success, -EEXIST if entry with the given name already | |
509 | * exists. | |
510 | */ | |
d69ac5a0 TH |
511 | int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd, |
512 | struct sysfs_dirent *parent_sd) | |
36ce6dad CH |
513 | { |
514 | int ret; | |
515 | ||
d69ac5a0 | 516 | ret = __sysfs_add_one(acxt, sd, parent_sd); |
425cb029 | 517 | |
d1c1459e TH |
518 | if (ret == -EEXIST) |
519 | sysfs_warn_dup(parent_sd, sd->s_name); | |
36ce6dad CH |
520 | return ret; |
521 | } | |
522 | ||
fb6896da TH |
523 | /** |
524 | * sysfs_remove_one - remove sysfs_dirent from parent | |
525 | * @acxt: addrm context to use | |
9fd5b1c9 | 526 | * @sd: sysfs_dirent to be removed |
fb6896da TH |
527 | * |
528 | * Mark @sd removed and drop nlink of parent inode if @sd is a | |
181b2e4b | 529 | * directory. @sd is unlinked from the children list. |
fb6896da TH |
530 | * |
531 | * This function should be called between calls to | |
532 | * sysfs_addrm_start() and sysfs_addrm_finish() and should be | |
533 | * passed the same @acxt as passed to sysfs_addrm_start(). | |
534 | * | |
535 | * LOCKING: | |
536 | * Determined by sysfs_addrm_start(). | |
537 | */ | |
250f7c3f TH |
538 | static void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, |
539 | struct sysfs_dirent *sd) | |
fb6896da | 540 | { |
6b0bfe93 EB |
541 | struct sysfs_inode_attrs *ps_iattr; |
542 | ||
26ea12de TH |
543 | /* |
544 | * Removal can be called multiple times on the same node. Only the | |
545 | * first invocation is effective and puts the base ref. | |
546 | */ | |
547 | if (sd->s_flags & SYSFS_FLAG_REMOVED) | |
548 | return; | |
41fc1c27 TH |
549 | |
550 | sysfs_unlink_sibling(sd); | |
fb6896da | 551 | |
6b0bfe93 | 552 | /* Update timestamps on the parent */ |
d69ac5a0 | 553 | ps_iattr = sd->s_parent->s_iattr; |
6b0bfe93 EB |
554 | if (ps_iattr) { |
555 | struct iattr *ps_iattrs = &ps_iattr->ia_iattr; | |
556 | ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME; | |
557 | } | |
558 | ||
fb6896da | 559 | sd->s_flags |= SYSFS_FLAG_REMOVED; |
58f2a4c7 | 560 | sd->u.removed_list = acxt->removed; |
fb6896da | 561 | acxt->removed = sd; |
a0edd7c8 TH |
562 | } |
563 | ||
fb6896da TH |
564 | /** |
565 | * sysfs_addrm_finish - finish up sysfs_dirent add/remove | |
566 | * @acxt: addrm context to finish up | |
567 | * | |
568 | * Finish up sysfs_dirent add/remove. Resources acquired by | |
569 | * sysfs_addrm_start() are released and removed sysfs_dirents are | |
a16bbc34 | 570 | * cleaned up. |
fb6896da TH |
571 | * |
572 | * LOCKING: | |
a16bbc34 | 573 | * sysfs_mutex is released. |
fb6896da | 574 | */ |
990e53f8 | 575 | void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt) |
d69ac5a0 | 576 | __releases(sysfs_mutex) |
fb6896da TH |
577 | { |
578 | /* release resources acquired by sysfs_addrm_start() */ | |
579 | mutex_unlock(&sysfs_mutex); | |
fb6896da TH |
580 | |
581 | /* kill removed sysfs_dirents */ | |
582 | while (acxt->removed) { | |
583 | struct sysfs_dirent *sd = acxt->removed; | |
584 | ||
58f2a4c7 | 585 | acxt->removed = sd->u.removed_list; |
fb6896da | 586 | |
fb6896da | 587 | sysfs_deactivate(sd); |
73d97146 | 588 | sysfs_unmap_bin_file(sd); |
fb6896da | 589 | sysfs_put(sd); |
13b3086d | 590 | } |
b592fcfe EB |
591 | } |
592 | ||
f0b0af47 TH |
593 | /** |
594 | * sysfs_find_dirent - find sysfs_dirent with the given name | |
595 | * @parent_sd: sysfs_dirent to search under | |
596 | * @name: name to look for | |
cfec0bc8 | 597 | * @ns: the namespace tag to use |
f0b0af47 TH |
598 | * |
599 | * Look for sysfs_dirent with name @name under @parent_sd. | |
c516865c | 600 | * |
f0b0af47 | 601 | * LOCKING: |
3007e997 | 602 | * mutex_lock(sysfs_mutex) |
c516865c | 603 | * |
f0b0af47 TH |
604 | * RETURNS: |
605 | * Pointer to sysfs_dirent if found, NULL if not. | |
c516865c | 606 | */ |
f0b0af47 | 607 | struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd, |
cfec0bc8 TH |
608 | const unsigned char *name, |
609 | const void *ns) | |
c516865c | 610 | { |
4e4d6d86 EB |
611 | struct rb_node *node = parent_sd->s_dir.children.rb_node; |
612 | unsigned int hash; | |
4f72c0ca | 613 | |
cfec0bc8 | 614 | hash = sysfs_name_hash(name, ns); |
4e4d6d86 EB |
615 | while (node) { |
616 | struct sysfs_dirent *sd; | |
617 | int result; | |
618 | ||
619 | sd = to_sysfs_dirent(node); | |
cfec0bc8 | 620 | result = sysfs_name_compare(hash, name, ns, sd); |
4e4d6d86 EB |
621 | if (result < 0) |
622 | node = node->rb_left; | |
623 | else if (result > 0) | |
624 | node = node->rb_right; | |
625 | else | |
626 | return sd; | |
3ff195b0 | 627 | } |
4e4d6d86 | 628 | return NULL; |
f0b0af47 | 629 | } |
c516865c | 630 | |
f0b0af47 | 631 | /** |
388975cc | 632 | * sysfs_get_dirent_ns - find and get sysfs_dirent with the given name |
f0b0af47 TH |
633 | * @parent_sd: sysfs_dirent to search under |
634 | * @name: name to look for | |
388975cc | 635 | * @ns: the namespace tag to use |
f0b0af47 TH |
636 | * |
637 | * Look for sysfs_dirent with name @name under @parent_sd and get | |
638 | * it if found. | |
639 | * | |
640 | * LOCKING: | |
3007e997 | 641 | * Kernel thread context (may sleep). Grabs sysfs_mutex. |
f0b0af47 TH |
642 | * |
643 | * RETURNS: | |
644 | * Pointer to sysfs_dirent if found, NULL if not. | |
645 | */ | |
388975cc TH |
646 | struct sysfs_dirent *sysfs_get_dirent_ns(struct sysfs_dirent *parent_sd, |
647 | const unsigned char *name, | |
648 | const void *ns) | |
f0b0af47 TH |
649 | { |
650 | struct sysfs_dirent *sd; | |
651 | ||
3007e997 | 652 | mutex_lock(&sysfs_mutex); |
cfec0bc8 | 653 | sd = sysfs_find_dirent(parent_sd, name, ns); |
f0b0af47 | 654 | sysfs_get(sd); |
3007e997 | 655 | mutex_unlock(&sysfs_mutex); |
f0b0af47 TH |
656 | |
657 | return sd; | |
c516865c | 658 | } |
388975cc | 659 | EXPORT_SYMBOL_GPL(sysfs_get_dirent_ns); |
c516865c | 660 | |
608e266a | 661 | static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd, |
cfec0bc8 TH |
662 | const char *name, const void *ns, |
663 | struct sysfs_dirent **p_sd) | |
1da177e4 | 664 | { |
1b18dc2b | 665 | umode_t mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO; |
51225039 | 666 | struct sysfs_addrm_cxt acxt; |
dfeb9fb0 | 667 | struct sysfs_dirent *sd; |
23dc2799 | 668 | int rc; |
1da177e4 | 669 | |
fc9f54b9 | 670 | /* allocate */ |
3e519038 | 671 | sd = sysfs_new_dirent(name, mode, SYSFS_DIR); |
a26cd722 | 672 | if (!sd) |
51225039 | 673 | return -ENOMEM; |
3ff195b0 | 674 | |
3ff195b0 | 675 | sd->s_ns = ns; |
b1fc3d61 | 676 | sd->s_dir.kobj = kobj; |
dfeb9fb0 | 677 | |
fc9f54b9 | 678 | /* link in */ |
d69ac5a0 TH |
679 | sysfs_addrm_start(&acxt); |
680 | rc = sysfs_add_one(&acxt, sd, parent_sd); | |
23dc2799 | 681 | sysfs_addrm_finish(&acxt); |
967e35dc | 682 | |
23dc2799 TH |
683 | if (rc == 0) |
684 | *p_sd = sd; | |
685 | else | |
967e35dc | 686 | sysfs_put(sd); |
dfeb9fb0 | 687 | |
23dc2799 | 688 | return rc; |
1da177e4 LT |
689 | } |
690 | ||
608e266a TH |
691 | int sysfs_create_subdir(struct kobject *kobj, const char *name, |
692 | struct sysfs_dirent **p_sd) | |
1da177e4 | 693 | { |
cfec0bc8 | 694 | return create_dir(kobj, kobj->sd, name, NULL, p_sd); |
1da177e4 LT |
695 | } |
696 | ||
697 | /** | |
e34ff490 TH |
698 | * sysfs_create_dir_ns - create a directory for an object with a namespace tag |
699 | * @kobj: object we're creating directory for | |
700 | * @ns: the namespace tag to use | |
1da177e4 | 701 | */ |
e34ff490 | 702 | int sysfs_create_dir_ns(struct kobject *kobj, const void *ns) |
1da177e4 | 703 | { |
608e266a | 704 | struct sysfs_dirent *parent_sd, *sd; |
1da177e4 LT |
705 | int error = 0; |
706 | ||
707 | BUG_ON(!kobj); | |
708 | ||
90bc6135 | 709 | if (kobj->parent) |
608e266a | 710 | parent_sd = kobj->parent->sd; |
1da177e4 | 711 | else |
7d0c7d67 | 712 | parent_sd = &sysfs_root; |
1da177e4 | 713 | |
3a198886 DW |
714 | if (!parent_sd) |
715 | return -ENOENT; | |
716 | ||
cfec0bc8 | 717 | error = create_dir(kobj, parent_sd, kobject_name(kobj), ns, &sd); |
1da177e4 | 718 | if (!error) |
608e266a | 719 | kobj->sd = sd; |
1da177e4 LT |
720 | return error; |
721 | } | |
722 | ||
1b18dc2b GKH |
723 | static struct dentry *sysfs_lookup(struct inode *dir, struct dentry *dentry, |
724 | unsigned int flags) | |
1da177e4 | 725 | { |
6cb52147 | 726 | struct dentry *ret = NULL; |
3ff195b0 EB |
727 | struct dentry *parent = dentry->d_parent; |
728 | struct sysfs_dirent *parent_sd = parent->d_fsdata; | |
a7a04754 | 729 | struct sysfs_dirent *sd; |
fc9f54b9 | 730 | struct inode *inode; |
cb26a311 | 731 | const void *ns = NULL; |
1da177e4 | 732 | |
6cb52147 TH |
733 | mutex_lock(&sysfs_mutex); |
734 | ||
cb26a311 TH |
735 | if (parent_sd->s_flags & SYSFS_FLAG_HAS_NS) |
736 | ns = sysfs_info(dir->i_sb)->ns; | |
3ff195b0 | 737 | |
cfec0bc8 | 738 | sd = sysfs_find_dirent(parent_sd, dentry->d_name.name, ns); |
1da177e4 | 739 | |
fc9f54b9 | 740 | /* no such entry */ |
e49452c6 TH |
741 | if (!sd) { |
742 | ret = ERR_PTR(-ENOENT); | |
6cb52147 | 743 | goto out_unlock; |
e49452c6 | 744 | } |
469796d1 | 745 | dentry->d_fsdata = sysfs_get(sd); |
fc9f54b9 TH |
746 | |
747 | /* attach dentry and inode */ | |
fac2622b | 748 | inode = sysfs_get_inode(dir->i_sb, sd); |
6cb52147 TH |
749 | if (!inode) { |
750 | ret = ERR_PTR(-ENOMEM); | |
751 | goto out_unlock; | |
752 | } | |
3007e997 | 753 | |
d6b4fd2f | 754 | /* instantiate and hash dentry */ |
e77fb7ce | 755 | ret = d_materialise_unique(dentry, inode); |
6cb52147 | 756 | out_unlock: |
3007e997 | 757 | mutex_unlock(&sysfs_mutex); |
6cb52147 | 758 | return ret; |
1da177e4 LT |
759 | } |
760 | ||
c5ef1c42 | 761 | const struct inode_operations sysfs_dir_inode_operations = { |
1da177e4 | 762 | .lookup = sysfs_lookup, |
e61ab4ae | 763 | .permission = sysfs_permission, |
988d186d | 764 | .setattr = sysfs_setattr, |
e61ab4ae | 765 | .getattr = sysfs_getattr, |
ddd29ec6 | 766 | .setxattr = sysfs_setxattr, |
1da177e4 LT |
767 | }; |
768 | ||
bcdde7e2 TH |
769 | static struct sysfs_dirent *sysfs_leftmost_descendant(struct sysfs_dirent *pos) |
770 | { | |
771 | struct sysfs_dirent *last; | |
772 | ||
773 | while (true) { | |
774 | struct rb_node *rbn; | |
775 | ||
776 | last = pos; | |
777 | ||
778 | if (sysfs_type(pos) != SYSFS_DIR) | |
779 | break; | |
780 | ||
781 | rbn = rb_first(&pos->s_dir.children); | |
782 | if (!rbn) | |
783 | break; | |
784 | ||
785 | pos = to_sysfs_dirent(rbn); | |
786 | } | |
787 | ||
788 | return last; | |
789 | } | |
790 | ||
791 | /** | |
792 | * sysfs_next_descendant_post - find the next descendant for post-order walk | |
793 | * @pos: the current position (%NULL to initiate traversal) | |
794 | * @root: sysfs_dirent whose descendants to walk | |
795 | * | |
796 | * Find the next descendant to visit for post-order traversal of @root's | |
797 | * descendants. @root is included in the iteration and the last node to be | |
798 | * visited. | |
799 | */ | |
800 | static struct sysfs_dirent *sysfs_next_descendant_post(struct sysfs_dirent *pos, | |
801 | struct sysfs_dirent *root) | |
802 | { | |
803 | struct rb_node *rbn; | |
804 | ||
805 | lockdep_assert_held(&sysfs_mutex); | |
806 | ||
807 | /* if first iteration, visit leftmost descendant which may be root */ | |
808 | if (!pos) | |
809 | return sysfs_leftmost_descendant(root); | |
810 | ||
811 | /* if we visited @root, we're done */ | |
812 | if (pos == root) | |
813 | return NULL; | |
814 | ||
815 | /* if there's an unvisited sibling, visit its leftmost descendant */ | |
816 | rbn = rb_next(&pos->s_rb); | |
817 | if (rbn) | |
818 | return sysfs_leftmost_descendant(to_sysfs_dirent(rbn)); | |
819 | ||
820 | /* no sibling left, visit parent */ | |
821 | return pos->s_parent; | |
822 | } | |
1da177e4 | 823 | |
7eed6ecb TH |
824 | static void __sysfs_remove(struct sysfs_addrm_cxt *acxt, |
825 | struct sysfs_dirent *sd) | |
1da177e4 | 826 | { |
bcdde7e2 | 827 | struct sysfs_dirent *pos, *next; |
1da177e4 | 828 | |
250f7c3f | 829 | if (!sd) |
1da177e4 LT |
830 | return; |
831 | ||
250f7c3f | 832 | pr_debug("sysfs %s: removing\n", sd->s_name); |
0ab66088 | 833 | |
bcdde7e2 TH |
834 | next = NULL; |
835 | do { | |
836 | pos = next; | |
250f7c3f | 837 | next = sysfs_next_descendant_post(pos, sd); |
bcdde7e2 | 838 | if (pos) |
250f7c3f | 839 | sysfs_remove_one(acxt, pos); |
bcdde7e2 | 840 | } while (next); |
250f7c3f | 841 | } |
bcdde7e2 | 842 | |
250f7c3f TH |
843 | /** |
844 | * sysfs_remove - remove a sysfs_dirent recursively | |
845 | * @sd: the sysfs_dirent to remove | |
846 | * | |
847 | * Remove @sd along with all its subdirectories and files. | |
848 | */ | |
849 | void sysfs_remove(struct sysfs_dirent *sd) | |
850 | { | |
851 | struct sysfs_addrm_cxt acxt; | |
852 | ||
853 | sysfs_addrm_start(&acxt); | |
854 | __sysfs_remove(&acxt, sd); | |
bcdde7e2 | 855 | sysfs_addrm_finish(&acxt); |
b592fcfe EB |
856 | } |
857 | ||
7eed6ecb TH |
858 | /** |
859 | * sysfs_hash_and_remove - find a sysfs_dirent by name and remove it | |
860 | * @dir_sd: parent of the target | |
861 | * @name: name of the sysfs_dirent to remove | |
862 | * @ns: namespace tag of the sysfs_dirent to remove | |
863 | * | |
864 | * Look for the sysfs_dirent with @name and @ns under @dir_sd and remove | |
865 | * it. Returns 0 on success, -ENOENT if such entry doesn't exist. | |
866 | */ | |
867 | int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const char *name, | |
868 | const void *ns) | |
869 | { | |
870 | struct sysfs_addrm_cxt acxt; | |
871 | struct sysfs_dirent *sd; | |
872 | ||
873 | if (!dir_sd) { | |
874 | WARN(1, KERN_WARNING "sysfs: can not remove '%s', no directory\n", | |
875 | name); | |
876 | return -ENOENT; | |
877 | } | |
878 | ||
879 | sysfs_addrm_start(&acxt); | |
880 | ||
881 | sd = sysfs_find_dirent(dir_sd, name, ns); | |
882 | if (sd) | |
883 | __sysfs_remove(&acxt, sd); | |
884 | ||
885 | sysfs_addrm_finish(&acxt); | |
886 | ||
887 | if (sd) | |
888 | return 0; | |
889 | else | |
890 | return -ENOENT; | |
891 | } | |
892 | ||
b592fcfe EB |
893 | /** |
894 | * sysfs_remove_dir - remove an object's directory. | |
895 | * @kobj: object. | |
896 | * | |
897 | * The only thing special about this is that we remove any files in | |
898 | * the directory before we remove the directory, and we've inlined | |
899 | * what used to be sysfs_rmdir() below, instead of calling separately. | |
900 | */ | |
1b18dc2b | 901 | void sysfs_remove_dir(struct kobject *kobj) |
b592fcfe | 902 | { |
608e266a | 903 | struct sysfs_dirent *sd = kobj->sd; |
aecdceda | 904 | |
5f995323 | 905 | spin_lock(&sysfs_assoc_lock); |
608e266a | 906 | kobj->sd = NULL; |
5f995323 | 907 | spin_unlock(&sysfs_assoc_lock); |
aecdceda | 908 | |
250f7c3f TH |
909 | if (sd) { |
910 | WARN_ON_ONCE(sysfs_type(sd) != SYSFS_DIR); | |
911 | sysfs_remove(sd); | |
912 | } | |
1da177e4 LT |
913 | } |
914 | ||
cfec0bc8 TH |
915 | int sysfs_rename(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent_sd, |
916 | const char *new_name, const void *new_ns) | |
1da177e4 | 917 | { |
996b7376 | 918 | int error; |
1da177e4 | 919 | |
832b6af1 | 920 | mutex_lock(&sysfs_mutex); |
932ea2e3 | 921 | |
9918f9a4 | 922 | error = 0; |
3ff195b0 | 923 | if ((sd->s_parent == new_parent_sd) && (sd->s_ns == new_ns) && |
ca1bab38 | 924 | (strcmp(sd->s_name, new_name) == 0)) |
9918f9a4 EB |
925 | goto out; /* nothing to rename */ |
926 | ||
9918f9a4 | 927 | error = -EEXIST; |
cfec0bc8 | 928 | if (sysfs_find_dirent(new_parent_sd, new_name, new_ns)) |
832b6af1 | 929 | goto out; |
996b7376 | 930 | |
0b4a4fea | 931 | /* rename sysfs_dirent */ |
ca1bab38 EB |
932 | if (strcmp(sd->s_name, new_name) != 0) { |
933 | error = -ENOMEM; | |
b4eafca1 | 934 | new_name = kstrdup(new_name, GFP_KERNEL); |
ca1bab38 EB |
935 | if (!new_name) |
936 | goto out; | |
937 | ||
b4eafca1 | 938 | kfree(sd->s_name); |
ca1bab38 EB |
939 | sd->s_name = new_name; |
940 | } | |
0c096b50 | 941 | |
ddfd6d07 GKH |
942 | /* |
943 | * Move to the appropriate place in the appropriate directories rbtree. | |
944 | */ | |
f6d90b4f EB |
945 | sysfs_unlink_sibling(sd); |
946 | sysfs_get(new_parent_sd); | |
947 | sysfs_put(sd->s_parent); | |
3ff195b0 | 948 | sd->s_ns = new_ns; |
cfec0bc8 | 949 | sd->s_hash = sysfs_name_hash(sd->s_name, sd->s_ns); |
f6d90b4f EB |
950 | sd->s_parent = new_parent_sd; |
951 | sysfs_link_sibling(sd); | |
0c096b50 | 952 | |
996b7376 | 953 | error = 0; |
832b6af1 | 954 | out: |
9918f9a4 | 955 | mutex_unlock(&sysfs_mutex); |
1da177e4 LT |
956 | return error; |
957 | } | |
958 | ||
e34ff490 TH |
959 | int sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name, |
960 | const void *new_ns) | |
ca1bab38 | 961 | { |
3ff195b0 | 962 | struct sysfs_dirent *parent_sd = kobj->sd->s_parent; |
3ff195b0 | 963 | |
cfec0bc8 | 964 | return sysfs_rename(kobj->sd, parent_sd, new_name, new_ns); |
ca1bab38 EB |
965 | } |
966 | ||
e34ff490 TH |
967 | int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj, |
968 | const void *new_ns) | |
8a82472f | 969 | { |
51225039 TH |
970 | struct sysfs_dirent *sd = kobj->sd; |
971 | struct sysfs_dirent *new_parent_sd; | |
8a82472f | 972 | |
51225039 | 973 | BUG_ON(!sd->s_parent); |
ca1bab38 | 974 | new_parent_sd = new_parent_kobj && new_parent_kobj->sd ? |
a6a83577 | 975 | new_parent_kobj->sd : &sysfs_root; |
51225039 | 976 | |
cfec0bc8 | 977 | return sysfs_rename(sd, new_parent_sd, sd->s_name, new_ns); |
8a82472f CH |
978 | } |
979 | ||
1da177e4 LT |
980 | /* Relationship between s_mode and the DT_xxx types */ |
981 | static inline unsigned char dt_type(struct sysfs_dirent *sd) | |
982 | { | |
983 | return (sd->s_mode >> 12) & 15; | |
984 | } | |
985 | ||
1e5289c9 EB |
986 | static int sysfs_dir_release(struct inode *inode, struct file *filp) |
987 | { | |
988 | sysfs_put(filp->private_data); | |
989 | return 0; | |
990 | } | |
991 | ||
3ff195b0 | 992 | static struct sysfs_dirent *sysfs_dir_pos(const void *ns, |
4e4d6d86 | 993 | struct sysfs_dirent *parent_sd, loff_t hash, struct sysfs_dirent *pos) |
1e5289c9 EB |
994 | { |
995 | if (pos) { | |
996 | int valid = !(pos->s_flags & SYSFS_FLAG_REMOVED) && | |
997 | pos->s_parent == parent_sd && | |
4e4d6d86 | 998 | hash == pos->s_hash; |
1e5289c9 | 999 | sysfs_put(pos); |
3ff195b0 EB |
1000 | if (!valid) |
1001 | pos = NULL; | |
1e5289c9 | 1002 | } |
4e4d6d86 EB |
1003 | if (!pos && (hash > 1) && (hash < INT_MAX)) { |
1004 | struct rb_node *node = parent_sd->s_dir.children.rb_node; | |
1005 | while (node) { | |
1006 | pos = to_sysfs_dirent(node); | |
1007 | ||
1008 | if (hash < pos->s_hash) | |
1009 | node = node->rb_left; | |
1010 | else if (hash > pos->s_hash) | |
1011 | node = node->rb_right; | |
1012 | else | |
a406f758 | 1013 | break; |
a406f758 MP |
1014 | } |
1015 | } | |
4e4d6d86 | 1016 | /* Skip over entries in the wrong namespace */ |
b9e2780d | 1017 | while (pos && pos->s_ns != ns) { |
4e4d6d86 EB |
1018 | struct rb_node *node = rb_next(&pos->s_rb); |
1019 | if (!node) | |
a406f758 MP |
1020 | pos = NULL; |
1021 | else | |
4e4d6d86 | 1022 | pos = to_sysfs_dirent(node); |
1e5289c9 EB |
1023 | } |
1024 | return pos; | |
1025 | } | |
1026 | ||
3ff195b0 EB |
1027 | static struct sysfs_dirent *sysfs_dir_next_pos(const void *ns, |
1028 | struct sysfs_dirent *parent_sd, ino_t ino, struct sysfs_dirent *pos) | |
1e5289c9 | 1029 | { |
3ff195b0 | 1030 | pos = sysfs_dir_pos(ns, parent_sd, ino, pos); |
37814ee0 GKH |
1031 | if (pos) |
1032 | do { | |
1033 | struct rb_node *node = rb_next(&pos->s_rb); | |
1034 | if (!node) | |
1035 | pos = NULL; | |
1036 | else | |
1037 | pos = to_sysfs_dirent(node); | |
1038 | } while (pos && pos->s_ns != ns); | |
1e5289c9 EB |
1039 | return pos; |
1040 | } | |
1041 | ||
d55fea8d | 1042 | static int sysfs_readdir(struct file *file, struct dir_context *ctx) |
1da177e4 | 1043 | { |
d55fea8d | 1044 | struct dentry *dentry = file->f_path.dentry; |
1b18dc2b | 1045 | struct sysfs_dirent *parent_sd = dentry->d_fsdata; |
d55fea8d | 1046 | struct sysfs_dirent *pos = file->private_data; |
cb26a311 | 1047 | const void *ns = NULL; |
3ff195b0 | 1048 | |
d55fea8d AV |
1049 | if (!dir_emit_dots(file, ctx)) |
1050 | return 0; | |
1e5289c9 | 1051 | mutex_lock(&sysfs_mutex); |
cb26a311 TH |
1052 | |
1053 | if (parent_sd->s_flags & SYSFS_FLAG_HAS_NS) | |
1054 | ns = sysfs_info(dentry->d_sb)->ns; | |
1055 | ||
d55fea8d | 1056 | for (pos = sysfs_dir_pos(ns, parent_sd, ctx->pos, pos); |
1e5289c9 | 1057 | pos; |
d55fea8d AV |
1058 | pos = sysfs_dir_next_pos(ns, parent_sd, ctx->pos, pos)) { |
1059 | const char *name = pos->s_name; | |
1060 | unsigned int type = dt_type(pos); | |
1061 | int len = strlen(name); | |
1062 | ino_t ino = pos->s_ino; | |
1063 | ctx->pos = pos->s_hash; | |
1064 | file->private_data = sysfs_get(pos); | |
1da177e4 | 1065 | |
3007e997 | 1066 | mutex_unlock(&sysfs_mutex); |
d55fea8d AV |
1067 | if (!dir_emit(ctx, name, len, ino, type)) |
1068 | return 0; | |
1e5289c9 | 1069 | mutex_lock(&sysfs_mutex); |
1e5289c9 EB |
1070 | } |
1071 | mutex_unlock(&sysfs_mutex); | |
d55fea8d AV |
1072 | file->private_data = NULL; |
1073 | ctx->pos = INT_MAX; | |
3efa65b9 | 1074 | return 0; |
1da177e4 LT |
1075 | } |
1076 | ||
991f76f8 ML |
1077 | static loff_t sysfs_dir_llseek(struct file *file, loff_t offset, int whence) |
1078 | { | |
1079 | struct inode *inode = file_inode(file); | |
1080 | loff_t ret; | |
1081 | ||
1082 | mutex_lock(&inode->i_mutex); | |
1083 | ret = generic_file_llseek(file, offset, whence); | |
1084 | mutex_unlock(&inode->i_mutex); | |
1085 | ||
1086 | return ret; | |
1087 | } | |
3efa65b9 | 1088 | |
4b6f5d20 | 1089 | const struct file_operations sysfs_dir_operations = { |
1da177e4 | 1090 | .read = generic_read_dir, |
d55fea8d | 1091 | .iterate = sysfs_readdir, |
1e5289c9 | 1092 | .release = sysfs_dir_release, |
991f76f8 | 1093 | .llseek = sysfs_dir_llseek, |
1da177e4 | 1094 | }; |