]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
bd33d12f | 2 | * inode.c - part of debugfs, a tiny little debug file system |
1da177e4 LT |
3 | * |
4 | * Copyright (C) 2004 Greg Kroah-Hartman <[email protected]> | |
5 | * Copyright (C) 2004 IBM Inc. | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU General Public License version | |
9 | * 2 as published by the Free Software Foundation. | |
10 | * | |
11 | * debugfs is for people to use instead of /proc or /sys. | |
12 | * See Documentation/DocBook/kernel-api for more details. | |
13 | * | |
14 | */ | |
15 | ||
1da177e4 LT |
16 | #include <linux/module.h> |
17 | #include <linux/fs.h> | |
18 | #include <linux/mount.h> | |
19 | #include <linux/pagemap.h> | |
20 | #include <linux/init.h> | |
4d8ebddc | 21 | #include <linux/kobject.h> |
1da177e4 LT |
22 | #include <linux/namei.h> |
23 | #include <linux/debugfs.h> | |
4f36557f | 24 | #include <linux/fsnotify.h> |
66f54963 | 25 | #include <linux/string.h> |
d6e48686 LN |
26 | #include <linux/seq_file.h> |
27 | #include <linux/parser.h> | |
92562927 | 28 | #include <linux/magic.h> |
5a0e3ad6 | 29 | #include <linux/slab.h> |
1da177e4 | 30 | |
82aceae4 | 31 | #define DEBUGFS_DEFAULT_MODE 0700 |
d6e48686 | 32 | |
1da177e4 LT |
33 | static struct vfsmount *debugfs_mount; |
34 | static int debugfs_mount_count; | |
c0f92ba9 | 35 | static bool debugfs_registered; |
1da177e4 | 36 | |
f4ae40a6 | 37 | static struct inode *debugfs_get_inode(struct super_block *sb, umode_t mode, dev_t dev, |
d3a3b0ad MD |
38 | void *data, const struct file_operations *fops) |
39 | ||
1da177e4 LT |
40 | { |
41 | struct inode *inode = new_inode(sb); | |
42 | ||
43 | if (inode) { | |
85fe4025 | 44 | inode->i_ino = get_next_ino(); |
1da177e4 | 45 | inode->i_mode = mode; |
1da177e4 LT |
46 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
47 | switch (mode & S_IFMT) { | |
48 | default: | |
49 | init_special_inode(inode, mode, dev); | |
50 | break; | |
51 | case S_IFREG: | |
d3a3b0ad MD |
52 | inode->i_fop = fops ? fops : &debugfs_file_operations; |
53 | inode->i_private = data; | |
1da177e4 | 54 | break; |
66f54963 PO |
55 | case S_IFLNK: |
56 | inode->i_op = &debugfs_link_operations; | |
d3a3b0ad | 57 | inode->i_private = data; |
66f54963 | 58 | break; |
1da177e4 LT |
59 | case S_IFDIR: |
60 | inode->i_op = &simple_dir_inode_operations; | |
ac481d6c | 61 | inode->i_fop = &simple_dir_operations; |
1da177e4 | 62 | |
bafb232e MD |
63 | /* directory inodes start off with i_nlink == 2 |
64 | * (for "." entry) */ | |
d8c76e6f | 65 | inc_nlink(inode); |
1da177e4 LT |
66 | break; |
67 | } | |
68 | } | |
88e412ea | 69 | return inode; |
1da177e4 LT |
70 | } |
71 | ||
72 | /* SMP-safe */ | |
160f7592 AV |
73 | static int debugfs_mknod(struct dentry *dentry, |
74 | umode_t mode, void *data, | |
d3a3b0ad | 75 | const struct file_operations *fops) |
1da177e4 | 76 | { |
71601e2b | 77 | struct inode *inode; |
1da177e4 | 78 | |
160f7592 AV |
79 | inode = debugfs_get_inode(dentry->d_sb, mode, 0, data, fops); |
80 | if (unlikely(!inode)) | |
81 | return -EPERM; | |
82 | d_instantiate(dentry, inode); | |
83 | dget(dentry); | |
84 | return 0; | |
1da177e4 LT |
85 | } |
86 | ||
1da177e4 LT |
87 | static inline int debugfs_positive(struct dentry *dentry) |
88 | { | |
89 | return dentry->d_inode && !d_unhashed(dentry); | |
90 | } | |
91 | ||
d6e48686 | 92 | struct debugfs_mount_opts { |
7dc05881 EB |
93 | kuid_t uid; |
94 | kgid_t gid; | |
d6e48686 LN |
95 | umode_t mode; |
96 | }; | |
97 | ||
98 | enum { | |
99 | Opt_uid, | |
100 | Opt_gid, | |
101 | Opt_mode, | |
102 | Opt_err | |
103 | }; | |
104 | ||
105 | static const match_table_t tokens = { | |
106 | {Opt_uid, "uid=%u"}, | |
107 | {Opt_gid, "gid=%u"}, | |
108 | {Opt_mode, "mode=%o"}, | |
109 | {Opt_err, NULL} | |
110 | }; | |
111 | ||
112 | struct debugfs_fs_info { | |
113 | struct debugfs_mount_opts mount_opts; | |
114 | }; | |
115 | ||
116 | static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts) | |
117 | { | |
118 | substring_t args[MAX_OPT_ARGS]; | |
119 | int option; | |
120 | int token; | |
7dc05881 EB |
121 | kuid_t uid; |
122 | kgid_t gid; | |
d6e48686 LN |
123 | char *p; |
124 | ||
125 | opts->mode = DEBUGFS_DEFAULT_MODE; | |
126 | ||
127 | while ((p = strsep(&data, ",")) != NULL) { | |
128 | if (!*p) | |
129 | continue; | |
130 | ||
131 | token = match_token(p, tokens, args); | |
132 | switch (token) { | |
133 | case Opt_uid: | |
134 | if (match_int(&args[0], &option)) | |
135 | return -EINVAL; | |
7dc05881 EB |
136 | uid = make_kuid(current_user_ns(), option); |
137 | if (!uid_valid(uid)) | |
138 | return -EINVAL; | |
139 | opts->uid = uid; | |
d6e48686 LN |
140 | break; |
141 | case Opt_gid: | |
f1688e04 | 142 | if (match_int(&args[0], &option)) |
d6e48686 | 143 | return -EINVAL; |
7dc05881 EB |
144 | gid = make_kgid(current_user_ns(), option); |
145 | if (!gid_valid(gid)) | |
146 | return -EINVAL; | |
147 | opts->gid = gid; | |
d6e48686 LN |
148 | break; |
149 | case Opt_mode: | |
150 | if (match_octal(&args[0], &option)) | |
151 | return -EINVAL; | |
152 | opts->mode = option & S_IALLUGO; | |
153 | break; | |
154 | /* | |
155 | * We might like to report bad mount options here; | |
156 | * but traditionally debugfs has ignored all mount options | |
157 | */ | |
158 | } | |
159 | } | |
160 | ||
161 | return 0; | |
162 | } | |
163 | ||
164 | static int debugfs_apply_options(struct super_block *sb) | |
165 | { | |
166 | struct debugfs_fs_info *fsi = sb->s_fs_info; | |
167 | struct inode *inode = sb->s_root->d_inode; | |
168 | struct debugfs_mount_opts *opts = &fsi->mount_opts; | |
169 | ||
170 | inode->i_mode &= ~S_IALLUGO; | |
171 | inode->i_mode |= opts->mode; | |
172 | ||
173 | inode->i_uid = opts->uid; | |
174 | inode->i_gid = opts->gid; | |
175 | ||
176 | return 0; | |
177 | } | |
178 | ||
179 | static int debugfs_remount(struct super_block *sb, int *flags, char *data) | |
180 | { | |
181 | int err; | |
182 | struct debugfs_fs_info *fsi = sb->s_fs_info; | |
183 | ||
02b9984d | 184 | sync_filesystem(sb); |
d6e48686 LN |
185 | err = debugfs_parse_options(data, &fsi->mount_opts); |
186 | if (err) | |
187 | goto fail; | |
188 | ||
189 | debugfs_apply_options(sb); | |
190 | ||
191 | fail: | |
192 | return err; | |
193 | } | |
194 | ||
195 | static int debugfs_show_options(struct seq_file *m, struct dentry *root) | |
196 | { | |
197 | struct debugfs_fs_info *fsi = root->d_sb->s_fs_info; | |
198 | struct debugfs_mount_opts *opts = &fsi->mount_opts; | |
199 | ||
7dc05881 EB |
200 | if (!uid_eq(opts->uid, GLOBAL_ROOT_UID)) |
201 | seq_printf(m, ",uid=%u", | |
202 | from_kuid_munged(&init_user_ns, opts->uid)); | |
203 | if (!gid_eq(opts->gid, GLOBAL_ROOT_GID)) | |
204 | seq_printf(m, ",gid=%u", | |
205 | from_kgid_munged(&init_user_ns, opts->gid)); | |
d6e48686 LN |
206 | if (opts->mode != DEBUGFS_DEFAULT_MODE) |
207 | seq_printf(m, ",mode=%o", opts->mode); | |
208 | ||
209 | return 0; | |
210 | } | |
211 | ||
212 | static const struct super_operations debugfs_super_operations = { | |
213 | .statfs = simple_statfs, | |
214 | .remount_fs = debugfs_remount, | |
215 | .show_options = debugfs_show_options, | |
216 | }; | |
217 | ||
1da177e4 LT |
218 | static int debug_fill_super(struct super_block *sb, void *data, int silent) |
219 | { | |
220 | static struct tree_descr debug_files[] = {{""}}; | |
d6e48686 LN |
221 | struct debugfs_fs_info *fsi; |
222 | int err; | |
223 | ||
224 | save_mount_options(sb, data); | |
225 | ||
226 | fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL); | |
227 | sb->s_fs_info = fsi; | |
228 | if (!fsi) { | |
229 | err = -ENOMEM; | |
230 | goto fail; | |
231 | } | |
232 | ||
233 | err = debugfs_parse_options(data, &fsi->mount_opts); | |
234 | if (err) | |
235 | goto fail; | |
236 | ||
237 | err = simple_fill_super(sb, DEBUGFS_MAGIC, debug_files); | |
238 | if (err) | |
239 | goto fail; | |
240 | ||
241 | sb->s_op = &debugfs_super_operations; | |
242 | ||
243 | debugfs_apply_options(sb); | |
244 | ||
245 | return 0; | |
1da177e4 | 246 | |
d6e48686 LN |
247 | fail: |
248 | kfree(fsi); | |
249 | sb->s_fs_info = NULL; | |
250 | return err; | |
1da177e4 LT |
251 | } |
252 | ||
fc14f2fe | 253 | static struct dentry *debug_mount(struct file_system_type *fs_type, |
454e2398 | 254 | int flags, const char *dev_name, |
fc14f2fe | 255 | void *data) |
1da177e4 | 256 | { |
fc14f2fe | 257 | return mount_single(fs_type, flags, data, debug_fill_super); |
1da177e4 LT |
258 | } |
259 | ||
260 | static struct file_system_type debug_fs_type = { | |
261 | .owner = THIS_MODULE, | |
262 | .name = "debugfs", | |
fc14f2fe | 263 | .mount = debug_mount, |
1da177e4 LT |
264 | .kill_sb = kill_litter_super, |
265 | }; | |
7f78e035 | 266 | MODULE_ALIAS_FS("debugfs"); |
1da177e4 | 267 | |
190afd81 | 268 | static struct dentry *start_creating(const char *name, struct dentry *parent) |
1da177e4 | 269 | { |
190afd81 | 270 | struct dentry *dentry; |
cfa57c11 AV |
271 | int error; |
272 | ||
273 | pr_debug("debugfs: creating file '%s'\n",name); | |
274 | ||
275 | error = simple_pin_fs(&debug_fs_type, &debugfs_mount, | |
276 | &debugfs_mount_count); | |
277 | if (error) | |
190afd81 | 278 | return ERR_PTR(error); |
1da177e4 LT |
279 | |
280 | /* If the parent is not specified, we create it in the root. | |
88e412ea | 281 | * We need the root dentry to do this, which is in the super |
1da177e4 LT |
282 | * block. A pointer to that is in the struct vfsmount that we |
283 | * have around. | |
284 | */ | |
ef52c75e | 285 | if (!parent) |
4c1d5a64 | 286 | parent = debugfs_mount->mnt_root; |
1da177e4 | 287 | |
1b1dcc1b | 288 | mutex_lock(&parent->d_inode->i_mutex); |
cfa57c11 | 289 | dentry = lookup_one_len(name, parent, strlen(name)); |
190afd81 | 290 | if (!IS_ERR(dentry) && dentry->d_inode) { |
cfa57c11 | 291 | dput(dentry); |
190afd81 AV |
292 | dentry = ERR_PTR(-EEXIST); |
293 | } | |
294 | if (IS_ERR(dentry)) | |
295 | mutex_unlock(&parent->d_inode->i_mutex); | |
296 | return dentry; | |
297 | } | |
298 | ||
299 | static struct dentry *end_creating(struct dentry *dentry, int error) | |
300 | { | |
301 | mutex_unlock(&dentry->d_parent->d_inode->i_mutex); | |
302 | dput(dentry); | |
1da177e4 | 303 | |
c3b1a350 AV |
304 | if (error) { |
305 | dentry = NULL; | |
306 | simple_release_fs(&debugfs_mount, &debugfs_mount_count); | |
307 | } | |
c3b1a350 AV |
308 | return dentry; |
309 | } | |
310 | ||
1da177e4 LT |
311 | /** |
312 | * debugfs_create_file - create a file in the debugfs filesystem | |
1da177e4 | 313 | * @name: a pointer to a string containing the name of the file to create. |
be030e65 | 314 | * @mode: the permission that the file should have. |
1da177e4 | 315 | * @parent: a pointer to the parent dentry for this file. This should be a |
e227867f | 316 | * directory dentry if set. If this parameter is NULL, then the |
1da177e4 LT |
317 | * file will be created in the root of the debugfs filesystem. |
318 | * @data: a pointer to something that the caller will want to get to later | |
8e18e294 | 319 | * on. The inode.i_private pointer will point to this value on |
1da177e4 LT |
320 | * the open() call. |
321 | * @fops: a pointer to a struct file_operations that should be used for | |
322 | * this file. | |
323 | * | |
324 | * This is the basic "create a file" function for debugfs. It allows for a | |
be030e65 AB |
325 | * wide range of flexibility in creating a file, or a directory (if you want |
326 | * to create a directory, the debugfs_create_dir() function is | |
1da177e4 LT |
327 | * recommended to be used instead.) |
328 | * | |
329 | * This function will return a pointer to a dentry if it succeeds. This | |
330 | * pointer must be passed to the debugfs_remove() function when the file is | |
331 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
6468b3af | 332 | * you are responsible here.) If an error occurs, %NULL will be returned. |
1da177e4 | 333 | * |
6468b3af | 334 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
873760fb | 335 | * returned. |
1da177e4 | 336 | */ |
f4ae40a6 | 337 | struct dentry *debugfs_create_file(const char *name, umode_t mode, |
1da177e4 | 338 | struct dentry *parent, void *data, |
99ac48f5 | 339 | const struct file_operations *fops) |
1da177e4 | 340 | { |
ad5abd5b AV |
341 | struct dentry *dentry; |
342 | int error; | |
343 | ||
344 | if (!(mode & S_IFMT)) | |
345 | mode |= S_IFREG; | |
346 | BUG_ON(!S_ISREG(mode)); | |
347 | dentry = start_creating(name, parent); | |
348 | ||
349 | if (IS_ERR(dentry)) | |
350 | return NULL; | |
c3b1a350 | 351 | |
3473cde5 AV |
352 | error = debugfs_mknod(dentry, mode, data, fops); |
353 | if (!error) | |
354 | fsnotify_create(dentry->d_parent->d_inode, dentry); | |
ad5abd5b | 355 | return end_creating(dentry, error); |
1da177e4 LT |
356 | } |
357 | EXPORT_SYMBOL_GPL(debugfs_create_file); | |
358 | ||
359 | /** | |
360 | * debugfs_create_dir - create a directory in the debugfs filesystem | |
1da177e4 LT |
361 | * @name: a pointer to a string containing the name of the directory to |
362 | * create. | |
363 | * @parent: a pointer to the parent dentry for this file. This should be a | |
e227867f | 364 | * directory dentry if set. If this parameter is NULL, then the |
1da177e4 LT |
365 | * directory will be created in the root of the debugfs filesystem. |
366 | * | |
367 | * This function creates a directory in debugfs with the given name. | |
368 | * | |
369 | * This function will return a pointer to a dentry if it succeeds. This | |
370 | * pointer must be passed to the debugfs_remove() function when the file is | |
371 | * to be removed (no automatic cleanup happens if your module is unloaded, | |
6468b3af | 372 | * you are responsible here.) If an error occurs, %NULL will be returned. |
1da177e4 | 373 | * |
6468b3af | 374 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be |
873760fb | 375 | * returned. |
1da177e4 LT |
376 | */ |
377 | struct dentry *debugfs_create_dir(const char *name, struct dentry *parent) | |
378 | { | |
ad5abd5b AV |
379 | struct dentry *dentry = start_creating(name, parent); |
380 | int error; | |
381 | ||
382 | if (IS_ERR(dentry)) | |
383 | return NULL; | |
384 | ||
02538a75 AV |
385 | error = debugfs_mknod(dentry, S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO, |
386 | NULL, NULL); | |
387 | if (!error) { | |
388 | inc_nlink(dentry->d_parent->d_inode); | |
389 | fsnotify_mkdir(dentry->d_parent->d_inode, dentry); | |
390 | } | |
ad5abd5b | 391 | return end_creating(dentry, error); |
1da177e4 LT |
392 | } |
393 | EXPORT_SYMBOL_GPL(debugfs_create_dir); | |
394 | ||
66f54963 PO |
395 | /** |
396 | * debugfs_create_symlink- create a symbolic link in the debugfs filesystem | |
397 | * @name: a pointer to a string containing the name of the symbolic link to | |
398 | * create. | |
399 | * @parent: a pointer to the parent dentry for this symbolic link. This | |
e227867f | 400 | * should be a directory dentry if set. If this parameter is NULL, |
66f54963 PO |
401 | * then the symbolic link will be created in the root of the debugfs |
402 | * filesystem. | |
403 | * @target: a pointer to a string containing the path to the target of the | |
404 | * symbolic link. | |
405 | * | |
406 | * This function creates a symbolic link with the given name in debugfs that | |
407 | * links to the given target path. | |
408 | * | |
409 | * This function will return a pointer to a dentry if it succeeds. This | |
410 | * pointer must be passed to the debugfs_remove() function when the symbolic | |
411 | * link is to be removed (no automatic cleanup happens if your module is | |
412 | * unloaded, you are responsible here.) If an error occurs, %NULL will be | |
413 | * returned. | |
414 | * | |
415 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be | |
873760fb | 416 | * returned. |
66f54963 PO |
417 | */ |
418 | struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent, | |
419 | const char *target) | |
420 | { | |
ad5abd5b | 421 | struct dentry *dentry; |
66f54963 | 422 | char *link; |
ad5abd5b | 423 | int error; |
66f54963 PO |
424 | |
425 | link = kstrdup(target, GFP_KERNEL); | |
426 | if (!link) | |
427 | return NULL; | |
428 | ||
ad5abd5b AV |
429 | dentry = start_creating(name, parent); |
430 | ||
431 | if (IS_ERR(dentry)) { | |
66f54963 | 432 | kfree(link); |
ad5abd5b AV |
433 | return NULL; |
434 | } | |
435 | ||
160f7592 | 436 | error = debugfs_mknod(dentry, S_IFLNK | S_IRWXUGO, link, NULL); |
ad5abd5b AV |
437 | if (error) |
438 | kfree(link); | |
439 | ||
440 | return end_creating(dentry, error); | |
66f54963 PO |
441 | } |
442 | EXPORT_SYMBOL_GPL(debugfs_create_symlink); | |
443 | ||
25d41d84 | 444 | static int __debugfs_remove(struct dentry *dentry, struct dentry *parent) |
9505e637 HS |
445 | { |
446 | int ret = 0; | |
447 | ||
448 | if (debugfs_positive(dentry)) { | |
449 | if (dentry->d_inode) { | |
450 | dget(dentry); | |
451 | switch (dentry->d_inode->i_mode & S_IFMT) { | |
452 | case S_IFDIR: | |
453 | ret = simple_rmdir(parent->d_inode, dentry); | |
454 | break; | |
455 | case S_IFLNK: | |
456 | kfree(dentry->d_inode->i_private); | |
457 | /* fall through */ | |
458 | default: | |
459 | simple_unlink(parent->d_inode, dentry); | |
460 | break; | |
461 | } | |
462 | if (!ret) | |
463 | d_delete(dentry); | |
464 | dput(dentry); | |
465 | } | |
466 | } | |
25d41d84 | 467 | return ret; |
9505e637 HS |
468 | } |
469 | ||
1da177e4 LT |
470 | /** |
471 | * debugfs_remove - removes a file or directory from the debugfs filesystem | |
1da177e4 LT |
472 | * @dentry: a pointer to a the dentry of the file or directory to be |
473 | * removed. | |
474 | * | |
475 | * This function removes a file or directory in debugfs that was previously | |
476 | * created with a call to another debugfs function (like | |
5a65980e | 477 | * debugfs_create_file() or variants thereof.) |
1da177e4 LT |
478 | * |
479 | * This function is required to be called in order for the file to be | |
480 | * removed, no automatic cleanup of files will happen when a module is | |
481 | * removed, you are responsible here. | |
482 | */ | |
483 | void debugfs_remove(struct dentry *dentry) | |
484 | { | |
485 | struct dentry *parent; | |
25d41d84 JK |
486 | int ret; |
487 | ||
a59d6293 | 488 | if (IS_ERR_OR_NULL(dentry)) |
1da177e4 LT |
489 | return; |
490 | ||
491 | parent = dentry->d_parent; | |
492 | if (!parent || !parent->d_inode) | |
493 | return; | |
494 | ||
1b1dcc1b | 495 | mutex_lock(&parent->d_inode->i_mutex); |
25d41d84 | 496 | ret = __debugfs_remove(dentry, parent); |
9505e637 | 497 | mutex_unlock(&parent->d_inode->i_mutex); |
25d41d84 JK |
498 | if (!ret) |
499 | simple_release_fs(&debugfs_mount, &debugfs_mount_count); | |
9505e637 HS |
500 | } |
501 | EXPORT_SYMBOL_GPL(debugfs_remove); | |
502 | ||
503 | /** | |
504 | * debugfs_remove_recursive - recursively removes a directory | |
505 | * @dentry: a pointer to a the dentry of the directory to be removed. | |
506 | * | |
507 | * This function recursively removes a directory tree in debugfs that | |
508 | * was previously created with a call to another debugfs function | |
509 | * (like debugfs_create_file() or variants thereof.) | |
510 | * | |
511 | * This function is required to be called in order for the file to be | |
512 | * removed, no automatic cleanup of files will happen when a module is | |
513 | * removed, you are responsible here. | |
514 | */ | |
515 | void debugfs_remove_recursive(struct dentry *dentry) | |
516 | { | |
485d4402 | 517 | struct dentry *child, *parent; |
9505e637 | 518 | |
a59d6293 | 519 | if (IS_ERR_OR_NULL(dentry)) |
9505e637 HS |
520 | return; |
521 | ||
522 | parent = dentry->d_parent; | |
523 | if (!parent || !parent->d_inode) | |
524 | return; | |
525 | ||
526 | parent = dentry; | |
776164c1 | 527 | down: |
9505e637 | 528 | mutex_lock(&parent->d_inode->i_mutex); |
485d4402 SR |
529 | loop: |
530 | /* | |
531 | * The parent->d_subdirs is protected by the d_lock. Outside that | |
532 | * lock, the child can be unlinked and set to be freed which can | |
533 | * use the d_u.d_child as the rcu head and corrupt this list. | |
534 | */ | |
535 | spin_lock(&parent->d_lock); | |
946e51f2 | 536 | list_for_each_entry(child, &parent->d_subdirs, d_child) { |
776164c1 ON |
537 | if (!debugfs_positive(child)) |
538 | continue; | |
9505e637 | 539 | |
776164c1 | 540 | /* perhaps simple_empty(child) makes more sense */ |
9505e637 | 541 | if (!list_empty(&child->d_subdirs)) { |
485d4402 | 542 | spin_unlock(&parent->d_lock); |
9505e637 HS |
543 | mutex_unlock(&parent->d_inode->i_mutex); |
544 | parent = child; | |
776164c1 | 545 | goto down; |
1da177e4 | 546 | } |
485d4402 SR |
547 | |
548 | spin_unlock(&parent->d_lock); | |
549 | ||
776164c1 ON |
550 | if (!__debugfs_remove(child, parent)) |
551 | simple_release_fs(&debugfs_mount, &debugfs_mount_count); | |
485d4402 SR |
552 | |
553 | /* | |
554 | * The parent->d_lock protects agaist child from unlinking | |
555 | * from d_subdirs. When releasing the parent->d_lock we can | |
556 | * no longer trust that the next pointer is valid. | |
557 | * Restart the loop. We'll skip this one with the | |
558 | * debugfs_positive() check. | |
559 | */ | |
560 | goto loop; | |
1da177e4 | 561 | } |
485d4402 | 562 | spin_unlock(&parent->d_lock); |
9505e637 | 563 | |
776164c1 ON |
564 | mutex_unlock(&parent->d_inode->i_mutex); |
565 | child = parent; | |
566 | parent = parent->d_parent; | |
9505e637 | 567 | mutex_lock(&parent->d_inode->i_mutex); |
776164c1 | 568 | |
485d4402 SR |
569 | if (child != dentry) |
570 | /* go up */ | |
571 | goto loop; | |
776164c1 ON |
572 | |
573 | if (!__debugfs_remove(child, parent)) | |
574 | simple_release_fs(&debugfs_mount, &debugfs_mount_count); | |
1b1dcc1b | 575 | mutex_unlock(&parent->d_inode->i_mutex); |
1da177e4 | 576 | } |
9505e637 | 577 | EXPORT_SYMBOL_GPL(debugfs_remove_recursive); |
1da177e4 | 578 | |
cfc94cdf JK |
579 | /** |
580 | * debugfs_rename - rename a file/directory in the debugfs filesystem | |
581 | * @old_dir: a pointer to the parent dentry for the renamed object. This | |
582 | * should be a directory dentry. | |
583 | * @old_dentry: dentry of an object to be renamed. | |
584 | * @new_dir: a pointer to the parent dentry where the object should be | |
585 | * moved. This should be a directory dentry. | |
586 | * @new_name: a pointer to a string containing the target name. | |
587 | * | |
588 | * This function renames a file/directory in debugfs. The target must not | |
589 | * exist for rename to succeed. | |
590 | * | |
591 | * This function will return a pointer to old_dentry (which is updated to | |
592 | * reflect renaming) if it succeeds. If an error occurs, %NULL will be | |
593 | * returned. | |
594 | * | |
595 | * If debugfs is not enabled in the kernel, the value -%ENODEV will be | |
596 | * returned. | |
597 | */ | |
598 | struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry, | |
599 | struct dentry *new_dir, const char *new_name) | |
600 | { | |
601 | int error; | |
602 | struct dentry *dentry = NULL, *trap; | |
603 | const char *old_name; | |
604 | ||
605 | trap = lock_rename(new_dir, old_dir); | |
606 | /* Source or destination directories don't exist? */ | |
607 | if (!old_dir->d_inode || !new_dir->d_inode) | |
608 | goto exit; | |
609 | /* Source does not exist, cyclic rename, or mountpoint? */ | |
610 | if (!old_dentry->d_inode || old_dentry == trap || | |
611 | d_mountpoint(old_dentry)) | |
612 | goto exit; | |
613 | dentry = lookup_one_len(new_name, new_dir, strlen(new_name)); | |
614 | /* Lookup failed, cyclic rename or target exists? */ | |
615 | if (IS_ERR(dentry) || dentry == trap || dentry->d_inode) | |
616 | goto exit; | |
617 | ||
618 | old_name = fsnotify_oldname_init(old_dentry->d_name.name); | |
619 | ||
620 | error = simple_rename(old_dir->d_inode, old_dentry, new_dir->d_inode, | |
621 | dentry); | |
622 | if (error) { | |
623 | fsnotify_oldname_free(old_name); | |
624 | goto exit; | |
625 | } | |
626 | d_move(old_dentry, dentry); | |
627 | fsnotify_move(old_dir->d_inode, new_dir->d_inode, old_name, | |
123df294 | 628 | S_ISDIR(old_dentry->d_inode->i_mode), |
5a190ae6 | 629 | NULL, old_dentry); |
cfc94cdf JK |
630 | fsnotify_oldname_free(old_name); |
631 | unlock_rename(new_dir, old_dir); | |
632 | dput(dentry); | |
633 | return old_dentry; | |
634 | exit: | |
635 | if (dentry && !IS_ERR(dentry)) | |
636 | dput(dentry); | |
637 | unlock_rename(new_dir, old_dir); | |
638 | return NULL; | |
639 | } | |
640 | EXPORT_SYMBOL_GPL(debugfs_rename); | |
641 | ||
c0f92ba9 FW |
642 | /** |
643 | * debugfs_initialized - Tells whether debugfs has been registered | |
644 | */ | |
645 | bool debugfs_initialized(void) | |
646 | { | |
647 | return debugfs_registered; | |
648 | } | |
649 | EXPORT_SYMBOL_GPL(debugfs_initialized); | |
650 | ||
651 | ||
191e186b | 652 | static struct kobject *debug_kobj; |
1da177e4 LT |
653 | |
654 | static int __init debugfs_init(void) | |
655 | { | |
656 | int retval; | |
657 | ||
0ff21e46 | 658 | debug_kobj = kobject_create_and_add("debug", kernel_kobj); |
191e186b GKH |
659 | if (!debug_kobj) |
660 | return -EINVAL; | |
1da177e4 LT |
661 | |
662 | retval = register_filesystem(&debug_fs_type); | |
663 | if (retval) | |
197b12d6 | 664 | kobject_put(debug_kobj); |
c0f92ba9 FW |
665 | else |
666 | debugfs_registered = true; | |
667 | ||
1da177e4 LT |
668 | return retval; |
669 | } | |
1da177e4 | 670 | core_initcall(debugfs_init); |
1da177e4 | 671 |