2 * inode.c - part of debugfs, a tiny little debug file system
4 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2004 IBM Inc.
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.
11 * debugfs is for people to use instead of /proc or /sys.
12 * See Documentation/DocBook/kernel-api for more details.
16 #include <linux/module.h>
18 #include <linux/mount.h>
19 #include <linux/pagemap.h>
20 #include <linux/init.h>
21 #include <linux/kobject.h>
22 #include <linux/namei.h>
23 #include <linux/debugfs.h>
24 #include <linux/fsnotify.h>
25 #include <linux/string.h>
26 #include <linux/seq_file.h>
27 #include <linux/parser.h>
28 #include <linux/magic.h>
29 #include <linux/slab.h>
31 #define DEBUGFS_DEFAULT_MODE 0700
33 static struct vfsmount
*debugfs_mount
;
34 static int debugfs_mount_count
;
35 static bool debugfs_registered
;
37 static struct inode
*debugfs_get_inode(struct super_block
*sb
)
39 struct inode
*inode
= new_inode(sb
);
41 inode
->i_ino
= get_next_ino();
42 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
47 struct debugfs_mount_opts
{
60 static const match_table_t tokens
= {
63 {Opt_mode
, "mode=%o"},
67 struct debugfs_fs_info
{
68 struct debugfs_mount_opts mount_opts
;
71 static int debugfs_parse_options(char *data
, struct debugfs_mount_opts
*opts
)
73 substring_t args
[MAX_OPT_ARGS
];
80 opts
->mode
= DEBUGFS_DEFAULT_MODE
;
82 while ((p
= strsep(&data
, ",")) != NULL
) {
86 token
= match_token(p
, tokens
, args
);
89 if (match_int(&args
[0], &option
))
91 uid
= make_kuid(current_user_ns(), option
);
97 if (match_int(&args
[0], &option
))
99 gid
= make_kgid(current_user_ns(), option
);
105 if (match_octal(&args
[0], &option
))
107 opts
->mode
= option
& S_IALLUGO
;
110 * We might like to report bad mount options here;
111 * but traditionally debugfs has ignored all mount options
119 static int debugfs_apply_options(struct super_block
*sb
)
121 struct debugfs_fs_info
*fsi
= sb
->s_fs_info
;
122 struct inode
*inode
= d_inode(sb
->s_root
);
123 struct debugfs_mount_opts
*opts
= &fsi
->mount_opts
;
125 inode
->i_mode
&= ~S_IALLUGO
;
126 inode
->i_mode
|= opts
->mode
;
128 inode
->i_uid
= opts
->uid
;
129 inode
->i_gid
= opts
->gid
;
134 static int debugfs_remount(struct super_block
*sb
, int *flags
, char *data
)
137 struct debugfs_fs_info
*fsi
= sb
->s_fs_info
;
140 err
= debugfs_parse_options(data
, &fsi
->mount_opts
);
144 debugfs_apply_options(sb
);
150 static int debugfs_show_options(struct seq_file
*m
, struct dentry
*root
)
152 struct debugfs_fs_info
*fsi
= root
->d_sb
->s_fs_info
;
153 struct debugfs_mount_opts
*opts
= &fsi
->mount_opts
;
155 if (!uid_eq(opts
->uid
, GLOBAL_ROOT_UID
))
156 seq_printf(m
, ",uid=%u",
157 from_kuid_munged(&init_user_ns
, opts
->uid
));
158 if (!gid_eq(opts
->gid
, GLOBAL_ROOT_GID
))
159 seq_printf(m
, ",gid=%u",
160 from_kgid_munged(&init_user_ns
, opts
->gid
));
161 if (opts
->mode
!= DEBUGFS_DEFAULT_MODE
)
162 seq_printf(m
, ",mode=%o", opts
->mode
);
167 static void debugfs_i_callback(struct rcu_head
*head
)
169 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
170 if (S_ISLNK(inode
->i_mode
))
171 kfree(inode
->i_link
);
172 free_inode_nonrcu(inode
);
175 static void debugfs_destroy_inode(struct inode
*inode
)
177 call_rcu(&inode
->i_rcu
, debugfs_i_callback
);
180 static const struct super_operations debugfs_super_operations
= {
181 .statfs
= simple_statfs
,
182 .remount_fs
= debugfs_remount
,
183 .show_options
= debugfs_show_options
,
184 .destroy_inode
= debugfs_destroy_inode
,
187 static struct vfsmount
*debugfs_automount(struct path
*path
)
189 struct vfsmount
*(*f
)(void *);
190 f
= (struct vfsmount
*(*)(void *))path
->dentry
->d_fsdata
;
191 return f(d_inode(path
->dentry
)->i_private
);
194 static const struct dentry_operations debugfs_dops
= {
195 .d_delete
= always_delete_dentry
,
196 .d_automount
= debugfs_automount
,
199 static int debug_fill_super(struct super_block
*sb
, void *data
, int silent
)
201 static struct tree_descr debug_files
[] = {{""}};
202 struct debugfs_fs_info
*fsi
;
205 save_mount_options(sb
, data
);
207 fsi
= kzalloc(sizeof(struct debugfs_fs_info
), GFP_KERNEL
);
214 err
= debugfs_parse_options(data
, &fsi
->mount_opts
);
218 err
= simple_fill_super(sb
, DEBUGFS_MAGIC
, debug_files
);
222 sb
->s_op
= &debugfs_super_operations
;
223 sb
->s_d_op
= &debugfs_dops
;
225 debugfs_apply_options(sb
);
231 sb
->s_fs_info
= NULL
;
235 static struct dentry
*debug_mount(struct file_system_type
*fs_type
,
236 int flags
, const char *dev_name
,
239 return mount_single(fs_type
, flags
, data
, debug_fill_super
);
242 static struct file_system_type debug_fs_type
= {
243 .owner
= THIS_MODULE
,
245 .mount
= debug_mount
,
246 .kill_sb
= kill_litter_super
,
248 MODULE_ALIAS_FS("debugfs");
250 static struct dentry
*start_creating(const char *name
, struct dentry
*parent
)
252 struct dentry
*dentry
;
255 pr_debug("debugfs: creating file '%s'\n",name
);
260 error
= simple_pin_fs(&debug_fs_type
, &debugfs_mount
,
261 &debugfs_mount_count
);
263 return ERR_PTR(error
);
265 /* If the parent is not specified, we create it in the root.
266 * We need the root dentry to do this, which is in the super
267 * block. A pointer to that is in the struct vfsmount that we
271 parent
= debugfs_mount
->mnt_root
;
273 mutex_lock(&d_inode(parent
)->i_mutex
);
274 dentry
= lookup_one_len(name
, parent
, strlen(name
));
275 if (!IS_ERR(dentry
) && d_really_is_positive(dentry
)) {
277 dentry
= ERR_PTR(-EEXIST
);
280 if (IS_ERR(dentry
)) {
281 mutex_unlock(&d_inode(parent
)->i_mutex
);
282 simple_release_fs(&debugfs_mount
, &debugfs_mount_count
);
288 static struct dentry
*failed_creating(struct dentry
*dentry
)
290 mutex_unlock(&d_inode(dentry
->d_parent
)->i_mutex
);
292 simple_release_fs(&debugfs_mount
, &debugfs_mount_count
);
296 static struct dentry
*end_creating(struct dentry
*dentry
)
298 mutex_unlock(&d_inode(dentry
->d_parent
)->i_mutex
);
303 * debugfs_create_file - create a file in the debugfs filesystem
304 * @name: a pointer to a string containing the name of the file to create.
305 * @mode: the permission that the file should have.
306 * @parent: a pointer to the parent dentry for this file. This should be a
307 * directory dentry if set. If this parameter is NULL, then the
308 * file will be created in the root of the debugfs filesystem.
309 * @data: a pointer to something that the caller will want to get to later
310 * on. The inode.i_private pointer will point to this value on
312 * @fops: a pointer to a struct file_operations that should be used for
315 * This is the basic "create a file" function for debugfs. It allows for a
316 * wide range of flexibility in creating a file, or a directory (if you want
317 * to create a directory, the debugfs_create_dir() function is
318 * recommended to be used instead.)
320 * This function will return a pointer to a dentry if it succeeds. This
321 * pointer must be passed to the debugfs_remove() function when the file is
322 * to be removed (no automatic cleanup happens if your module is unloaded,
323 * you are responsible here.) If an error occurs, %NULL will be returned.
325 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
328 struct dentry
*debugfs_create_file(const char *name
, umode_t mode
,
329 struct dentry
*parent
, void *data
,
330 const struct file_operations
*fops
)
332 struct dentry
*dentry
;
335 if (!(mode
& S_IFMT
))
337 BUG_ON(!S_ISREG(mode
));
338 dentry
= start_creating(name
, parent
);
343 inode
= debugfs_get_inode(dentry
->d_sb
);
344 if (unlikely(!inode
))
345 return failed_creating(dentry
);
347 inode
->i_mode
= mode
;
348 inode
->i_fop
= fops
? fops
: &debugfs_file_operations
;
349 inode
->i_private
= data
;
350 d_instantiate(dentry
, inode
);
351 fsnotify_create(d_inode(dentry
->d_parent
), dentry
);
352 return end_creating(dentry
);
354 EXPORT_SYMBOL_GPL(debugfs_create_file
);
357 * debugfs_create_file_size - create a file in the debugfs filesystem
358 * @name: a pointer to a string containing the name of the file to create.
359 * @mode: the permission that the file should have.
360 * @parent: a pointer to the parent dentry for this file. This should be a
361 * directory dentry if set. If this parameter is NULL, then the
362 * file will be created in the root of the debugfs filesystem.
363 * @data: a pointer to something that the caller will want to get to later
364 * on. The inode.i_private pointer will point to this value on
366 * @fops: a pointer to a struct file_operations that should be used for
368 * @file_size: initial file size
370 * This is the basic "create a file" function for debugfs. It allows for a
371 * wide range of flexibility in creating a file, or a directory (if you want
372 * to create a directory, the debugfs_create_dir() function is
373 * recommended to be used instead.)
375 * This function will return a pointer to a dentry if it succeeds. This
376 * pointer must be passed to the debugfs_remove() function when the file is
377 * to be removed (no automatic cleanup happens if your module is unloaded,
378 * you are responsible here.) If an error occurs, %NULL will be returned.
380 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
383 struct dentry
*debugfs_create_file_size(const char *name
, umode_t mode
,
384 struct dentry
*parent
, void *data
,
385 const struct file_operations
*fops
,
388 struct dentry
*de
= debugfs_create_file(name
, mode
, parent
, data
, fops
);
391 d_inode(de
)->i_size
= file_size
;
394 EXPORT_SYMBOL_GPL(debugfs_create_file_size
);
397 * debugfs_create_dir - create a directory in the debugfs filesystem
398 * @name: a pointer to a string containing the name of the directory to
400 * @parent: a pointer to the parent dentry for this file. This should be a
401 * directory dentry if set. If this parameter is NULL, then the
402 * directory will be created in the root of the debugfs filesystem.
404 * This function creates a directory in debugfs with the given name.
406 * This function will return a pointer to a dentry if it succeeds. This
407 * pointer must be passed to the debugfs_remove() function when the file is
408 * to be removed (no automatic cleanup happens if your module is unloaded,
409 * you are responsible here.) If an error occurs, %NULL will be returned.
411 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
414 struct dentry
*debugfs_create_dir(const char *name
, struct dentry
*parent
)
416 struct dentry
*dentry
= start_creating(name
, parent
);
422 inode
= debugfs_get_inode(dentry
->d_sb
);
423 if (unlikely(!inode
))
424 return failed_creating(dentry
);
426 inode
->i_mode
= S_IFDIR
| S_IRWXU
| S_IRUGO
| S_IXUGO
;
427 inode
->i_op
= &simple_dir_inode_operations
;
428 inode
->i_fop
= &simple_dir_operations
;
430 /* directory inodes start off with i_nlink == 2 (for "." entry) */
432 d_instantiate(dentry
, inode
);
433 inc_nlink(d_inode(dentry
->d_parent
));
434 fsnotify_mkdir(d_inode(dentry
->d_parent
), dentry
);
435 return end_creating(dentry
);
437 EXPORT_SYMBOL_GPL(debugfs_create_dir
);
440 * debugfs_create_automount - create automount point in the debugfs filesystem
441 * @name: a pointer to a string containing the name of the file to create.
442 * @parent: a pointer to the parent dentry for this file. This should be a
443 * directory dentry if set. If this parameter is NULL, then the
444 * file will be created in the root of the debugfs filesystem.
445 * @f: function to be called when pathname resolution steps on that one.
446 * @data: opaque argument to pass to f().
448 * @f should return what ->d_automount() would.
450 struct dentry
*debugfs_create_automount(const char *name
,
451 struct dentry
*parent
,
452 struct vfsmount
*(*f
)(void *),
455 struct dentry
*dentry
= start_creating(name
, parent
);
461 inode
= debugfs_get_inode(dentry
->d_sb
);
462 if (unlikely(!inode
))
463 return failed_creating(dentry
);
465 make_empty_dir_inode(inode
);
466 inode
->i_flags
|= S_AUTOMOUNT
;
467 inode
->i_private
= data
;
468 dentry
->d_fsdata
= (void *)f
;
469 d_instantiate(dentry
, inode
);
470 return end_creating(dentry
);
472 EXPORT_SYMBOL(debugfs_create_automount
);
475 * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
476 * @name: a pointer to a string containing the name of the symbolic link to
478 * @parent: a pointer to the parent dentry for this symbolic link. This
479 * should be a directory dentry if set. If this parameter is NULL,
480 * then the symbolic link will be created in the root of the debugfs
482 * @target: a pointer to a string containing the path to the target of the
485 * This function creates a symbolic link with the given name in debugfs that
486 * links to the given target path.
488 * This function will return a pointer to a dentry if it succeeds. This
489 * pointer must be passed to the debugfs_remove() function when the symbolic
490 * link is to be removed (no automatic cleanup happens if your module is
491 * unloaded, you are responsible here.) If an error occurs, %NULL will be
494 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
497 struct dentry
*debugfs_create_symlink(const char *name
, struct dentry
*parent
,
500 struct dentry
*dentry
;
502 char *link
= kstrdup(target
, GFP_KERNEL
);
506 dentry
= start_creating(name
, parent
);
507 if (IS_ERR(dentry
)) {
512 inode
= debugfs_get_inode(dentry
->d_sb
);
513 if (unlikely(!inode
)) {
515 return failed_creating(dentry
);
517 inode
->i_mode
= S_IFLNK
| S_IRWXUGO
;
518 inode
->i_op
= &simple_symlink_inode_operations
;
519 inode
->i_link
= link
;
520 d_instantiate(dentry
, inode
);
521 return end_creating(dentry
);
523 EXPORT_SYMBOL_GPL(debugfs_create_symlink
);
525 static int __debugfs_remove(struct dentry
*dentry
, struct dentry
*parent
)
529 if (simple_positive(dentry
)) {
531 if (d_is_dir(dentry
))
532 ret
= simple_rmdir(d_inode(parent
), dentry
);
534 simple_unlink(d_inode(parent
), dentry
);
543 * debugfs_remove - removes a file or directory from the debugfs filesystem
544 * @dentry: a pointer to a the dentry of the file or directory to be
545 * removed. If this parameter is NULL or an error value, nothing
548 * This function removes a file or directory in debugfs that was previously
549 * created with a call to another debugfs function (like
550 * debugfs_create_file() or variants thereof.)
552 * This function is required to be called in order for the file to be
553 * removed, no automatic cleanup of files will happen when a module is
554 * removed, you are responsible here.
556 void debugfs_remove(struct dentry
*dentry
)
558 struct dentry
*parent
;
561 if (IS_ERR_OR_NULL(dentry
))
564 parent
= dentry
->d_parent
;
565 if (!parent
|| d_really_is_negative(parent
))
568 mutex_lock(&d_inode(parent
)->i_mutex
);
569 ret
= __debugfs_remove(dentry
, parent
);
570 mutex_unlock(&d_inode(parent
)->i_mutex
);
572 simple_release_fs(&debugfs_mount
, &debugfs_mount_count
);
574 EXPORT_SYMBOL_GPL(debugfs_remove
);
577 * debugfs_remove_recursive - recursively removes a directory
578 * @dentry: a pointer to a the dentry of the directory to be removed. If this
579 * parameter is NULL or an error value, nothing will be done.
581 * This function recursively removes a directory tree in debugfs that
582 * was previously created with a call to another debugfs function
583 * (like debugfs_create_file() or variants thereof.)
585 * This function is required to be called in order for the file to be
586 * removed, no automatic cleanup of files will happen when a module is
587 * removed, you are responsible here.
589 void debugfs_remove_recursive(struct dentry
*dentry
)
591 struct dentry
*child
, *parent
;
593 if (IS_ERR_OR_NULL(dentry
))
596 parent
= dentry
->d_parent
;
597 if (!parent
|| d_really_is_negative(parent
))
602 mutex_lock(&d_inode(parent
)->i_mutex
);
605 * The parent->d_subdirs is protected by the d_lock. Outside that
606 * lock, the child can be unlinked and set to be freed which can
607 * use the d_u.d_child as the rcu head and corrupt this list.
609 spin_lock(&parent
->d_lock
);
610 list_for_each_entry(child
, &parent
->d_subdirs
, d_child
) {
611 if (!simple_positive(child
))
614 /* perhaps simple_empty(child) makes more sense */
615 if (!list_empty(&child
->d_subdirs
)) {
616 spin_unlock(&parent
->d_lock
);
617 mutex_unlock(&d_inode(parent
)->i_mutex
);
622 spin_unlock(&parent
->d_lock
);
624 if (!__debugfs_remove(child
, parent
))
625 simple_release_fs(&debugfs_mount
, &debugfs_mount_count
);
628 * The parent->d_lock protects agaist child from unlinking
629 * from d_subdirs. When releasing the parent->d_lock we can
630 * no longer trust that the next pointer is valid.
631 * Restart the loop. We'll skip this one with the
632 * simple_positive() check.
636 spin_unlock(&parent
->d_lock
);
638 mutex_unlock(&d_inode(parent
)->i_mutex
);
640 parent
= parent
->d_parent
;
641 mutex_lock(&d_inode(parent
)->i_mutex
);
647 if (!__debugfs_remove(child
, parent
))
648 simple_release_fs(&debugfs_mount
, &debugfs_mount_count
);
649 mutex_unlock(&d_inode(parent
)->i_mutex
);
651 EXPORT_SYMBOL_GPL(debugfs_remove_recursive
);
654 * debugfs_rename - rename a file/directory in the debugfs filesystem
655 * @old_dir: a pointer to the parent dentry for the renamed object. This
656 * should be a directory dentry.
657 * @old_dentry: dentry of an object to be renamed.
658 * @new_dir: a pointer to the parent dentry where the object should be
659 * moved. This should be a directory dentry.
660 * @new_name: a pointer to a string containing the target name.
662 * This function renames a file/directory in debugfs. The target must not
663 * exist for rename to succeed.
665 * This function will return a pointer to old_dentry (which is updated to
666 * reflect renaming) if it succeeds. If an error occurs, %NULL will be
669 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
672 struct dentry
*debugfs_rename(struct dentry
*old_dir
, struct dentry
*old_dentry
,
673 struct dentry
*new_dir
, const char *new_name
)
676 struct dentry
*dentry
= NULL
, *trap
;
677 struct name_snapshot old_name
;
683 if (IS_ERR_OR_NULL(old_dentry
))
686 trap
= lock_rename(new_dir
, old_dir
);
687 /* Source or destination directories don't exist? */
688 if (d_really_is_negative(old_dir
) || d_really_is_negative(new_dir
))
690 /* Source does not exist, cyclic rename, or mountpoint? */
691 if (d_really_is_negative(old_dentry
) || old_dentry
== trap
||
692 d_mountpoint(old_dentry
))
694 dentry
= lookup_one_len(new_name
, new_dir
, strlen(new_name
));
695 /* Lookup failed, cyclic rename or target exists? */
696 if (IS_ERR(dentry
) || dentry
== trap
|| d_really_is_positive(dentry
))
699 take_dentry_name_snapshot(&old_name
, old_dentry
);
701 error
= simple_rename(d_inode(old_dir
), old_dentry
, d_inode(new_dir
),
704 release_dentry_name_snapshot(&old_name
);
707 d_move(old_dentry
, dentry
);
708 fsnotify_move(d_inode(old_dir
), d_inode(new_dir
), old_name
.name
,
709 d_is_dir(old_dentry
),
711 release_dentry_name_snapshot(&old_name
);
712 unlock_rename(new_dir
, old_dir
);
716 if (dentry
&& !IS_ERR(dentry
))
718 unlock_rename(new_dir
, old_dir
);
721 EXPORT_SYMBOL_GPL(debugfs_rename
);
724 * debugfs_initialized - Tells whether debugfs has been registered
726 bool debugfs_initialized(void)
728 return debugfs_registered
;
730 EXPORT_SYMBOL_GPL(debugfs_initialized
);
732 static int __init
debugfs_init(void)
736 retval
= sysfs_create_mount_point(kernel_kobj
, "debug");
740 retval
= register_filesystem(&debug_fs_type
);
742 sysfs_remove_mount_point(kernel_kobj
, "debug");
744 debugfs_registered
= true;
748 core_initcall(debugfs_init
);