xref: /linux-6.15/fs/debugfs/inode.c (revision 77b3da6e)
11da177e4SLinus Torvalds /*
2bd33d12fSHarry Wei  *  inode.c - part of debugfs, a tiny little debug file system
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 2004 Greg Kroah-Hartman <[email protected]>
51da177e4SLinus Torvalds  *  Copyright (C) 2004 IBM Inc.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
81da177e4SLinus Torvalds  *	modify it under the terms of the GNU General Public License version
91da177e4SLinus Torvalds  *	2 as published by the Free Software Foundation.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *  debugfs is for people to use instead of /proc or /sys.
121da177e4SLinus Torvalds  *  See Documentation/DocBook/kernel-api for more details.
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #include <linux/module.h>
171da177e4SLinus Torvalds #include <linux/fs.h>
181da177e4SLinus Torvalds #include <linux/mount.h>
191da177e4SLinus Torvalds #include <linux/pagemap.h>
201da177e4SLinus Torvalds #include <linux/init.h>
214d8ebddcSRandy Dunlap #include <linux/kobject.h>
221da177e4SLinus Torvalds #include <linux/namei.h>
231da177e4SLinus Torvalds #include <linux/debugfs.h>
244f36557fSMathieu Desnoyers #include <linux/fsnotify.h>
2566f54963SPeter Oberparleiter #include <linux/string.h>
26d6e48686SLudwig Nussel #include <linux/seq_file.h>
27d6e48686SLudwig Nussel #include <linux/parser.h>
2892562927SMimi Zohar #include <linux/magic.h>
295a0e3ad6STejun Heo #include <linux/slab.h>
301da177e4SLinus Torvalds 
3182aceae4SKees Cook #define DEBUGFS_DEFAULT_MODE	0700
32d6e48686SLudwig Nussel 
331da177e4SLinus Torvalds static struct vfsmount *debugfs_mount;
341da177e4SLinus Torvalds static int debugfs_mount_count;
35c0f92ba9SFrederic Weisbecker static bool debugfs_registered;
361da177e4SLinus Torvalds 
37edac65eaSAl Viro static struct inode *debugfs_get_inode(struct super_block *sb)
381da177e4SLinus Torvalds {
391da177e4SLinus Torvalds 	struct inode *inode = new_inode(sb);
401da177e4SLinus Torvalds 	if (inode) {
4185fe4025SChristoph Hellwig 		inode->i_ino = get_next_ino();
421da177e4SLinus Torvalds 		inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
431da177e4SLinus Torvalds 	}
441da177e4SLinus Torvalds 	return inode;
451da177e4SLinus Torvalds }
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds static inline int debugfs_positive(struct dentry *dentry)
481da177e4SLinus Torvalds {
491da177e4SLinus Torvalds 	return dentry->d_inode && !d_unhashed(dentry);
501da177e4SLinus Torvalds }
511da177e4SLinus Torvalds 
52d6e48686SLudwig Nussel struct debugfs_mount_opts {
537dc05881SEric W. Biederman 	kuid_t uid;
547dc05881SEric W. Biederman 	kgid_t gid;
55d6e48686SLudwig Nussel 	umode_t mode;
56d6e48686SLudwig Nussel };
57d6e48686SLudwig Nussel 
58d6e48686SLudwig Nussel enum {
59d6e48686SLudwig Nussel 	Opt_uid,
60d6e48686SLudwig Nussel 	Opt_gid,
61d6e48686SLudwig Nussel 	Opt_mode,
62d6e48686SLudwig Nussel 	Opt_err
63d6e48686SLudwig Nussel };
64d6e48686SLudwig Nussel 
65d6e48686SLudwig Nussel static const match_table_t tokens = {
66d6e48686SLudwig Nussel 	{Opt_uid, "uid=%u"},
67d6e48686SLudwig Nussel 	{Opt_gid, "gid=%u"},
68d6e48686SLudwig Nussel 	{Opt_mode, "mode=%o"},
69d6e48686SLudwig Nussel 	{Opt_err, NULL}
70d6e48686SLudwig Nussel };
71d6e48686SLudwig Nussel 
72d6e48686SLudwig Nussel struct debugfs_fs_info {
73d6e48686SLudwig Nussel 	struct debugfs_mount_opts mount_opts;
74d6e48686SLudwig Nussel };
75d6e48686SLudwig Nussel 
76d6e48686SLudwig Nussel static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts)
77d6e48686SLudwig Nussel {
78d6e48686SLudwig Nussel 	substring_t args[MAX_OPT_ARGS];
79d6e48686SLudwig Nussel 	int option;
80d6e48686SLudwig Nussel 	int token;
817dc05881SEric W. Biederman 	kuid_t uid;
827dc05881SEric W. Biederman 	kgid_t gid;
83d6e48686SLudwig Nussel 	char *p;
84d6e48686SLudwig Nussel 
85d6e48686SLudwig Nussel 	opts->mode = DEBUGFS_DEFAULT_MODE;
86d6e48686SLudwig Nussel 
87d6e48686SLudwig Nussel 	while ((p = strsep(&data, ",")) != NULL) {
88d6e48686SLudwig Nussel 		if (!*p)
89d6e48686SLudwig Nussel 			continue;
90d6e48686SLudwig Nussel 
91d6e48686SLudwig Nussel 		token = match_token(p, tokens, args);
92d6e48686SLudwig Nussel 		switch (token) {
93d6e48686SLudwig Nussel 		case Opt_uid:
94d6e48686SLudwig Nussel 			if (match_int(&args[0], &option))
95d6e48686SLudwig Nussel 				return -EINVAL;
967dc05881SEric W. Biederman 			uid = make_kuid(current_user_ns(), option);
977dc05881SEric W. Biederman 			if (!uid_valid(uid))
987dc05881SEric W. Biederman 				return -EINVAL;
997dc05881SEric W. Biederman 			opts->uid = uid;
100d6e48686SLudwig Nussel 			break;
101d6e48686SLudwig Nussel 		case Opt_gid:
102f1688e04SDave Reisner 			if (match_int(&args[0], &option))
103d6e48686SLudwig Nussel 				return -EINVAL;
1047dc05881SEric W. Biederman 			gid = make_kgid(current_user_ns(), option);
1057dc05881SEric W. Biederman 			if (!gid_valid(gid))
1067dc05881SEric W. Biederman 				return -EINVAL;
1077dc05881SEric W. Biederman 			opts->gid = gid;
108d6e48686SLudwig Nussel 			break;
109d6e48686SLudwig Nussel 		case Opt_mode:
110d6e48686SLudwig Nussel 			if (match_octal(&args[0], &option))
111d6e48686SLudwig Nussel 				return -EINVAL;
112d6e48686SLudwig Nussel 			opts->mode = option & S_IALLUGO;
113d6e48686SLudwig Nussel 			break;
114d6e48686SLudwig Nussel 		/*
115d6e48686SLudwig Nussel 		 * We might like to report bad mount options here;
116d6e48686SLudwig Nussel 		 * but traditionally debugfs has ignored all mount options
117d6e48686SLudwig Nussel 		 */
118d6e48686SLudwig Nussel 		}
119d6e48686SLudwig Nussel 	}
120d6e48686SLudwig Nussel 
121d6e48686SLudwig Nussel 	return 0;
122d6e48686SLudwig Nussel }
123d6e48686SLudwig Nussel 
124d6e48686SLudwig Nussel static int debugfs_apply_options(struct super_block *sb)
125d6e48686SLudwig Nussel {
126d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi = sb->s_fs_info;
127d6e48686SLudwig Nussel 	struct inode *inode = sb->s_root->d_inode;
128d6e48686SLudwig Nussel 	struct debugfs_mount_opts *opts = &fsi->mount_opts;
129d6e48686SLudwig Nussel 
130d6e48686SLudwig Nussel 	inode->i_mode &= ~S_IALLUGO;
131d6e48686SLudwig Nussel 	inode->i_mode |= opts->mode;
132d6e48686SLudwig Nussel 
133d6e48686SLudwig Nussel 	inode->i_uid = opts->uid;
134d6e48686SLudwig Nussel 	inode->i_gid = opts->gid;
135d6e48686SLudwig Nussel 
136d6e48686SLudwig Nussel 	return 0;
137d6e48686SLudwig Nussel }
138d6e48686SLudwig Nussel 
139d6e48686SLudwig Nussel static int debugfs_remount(struct super_block *sb, int *flags, char *data)
140d6e48686SLudwig Nussel {
141d6e48686SLudwig Nussel 	int err;
142d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi = sb->s_fs_info;
143d6e48686SLudwig Nussel 
14402b9984dSTheodore Ts'o 	sync_filesystem(sb);
145d6e48686SLudwig Nussel 	err = debugfs_parse_options(data, &fsi->mount_opts);
146d6e48686SLudwig Nussel 	if (err)
147d6e48686SLudwig Nussel 		goto fail;
148d6e48686SLudwig Nussel 
149d6e48686SLudwig Nussel 	debugfs_apply_options(sb);
150d6e48686SLudwig Nussel 
151d6e48686SLudwig Nussel fail:
152d6e48686SLudwig Nussel 	return err;
153d6e48686SLudwig Nussel }
154d6e48686SLudwig Nussel 
155d6e48686SLudwig Nussel static int debugfs_show_options(struct seq_file *m, struct dentry *root)
156d6e48686SLudwig Nussel {
157d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi = root->d_sb->s_fs_info;
158d6e48686SLudwig Nussel 	struct debugfs_mount_opts *opts = &fsi->mount_opts;
159d6e48686SLudwig Nussel 
1607dc05881SEric W. Biederman 	if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
1617dc05881SEric W. Biederman 		seq_printf(m, ",uid=%u",
1627dc05881SEric W. Biederman 			   from_kuid_munged(&init_user_ns, opts->uid));
1637dc05881SEric W. Biederman 	if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
1647dc05881SEric W. Biederman 		seq_printf(m, ",gid=%u",
1657dc05881SEric W. Biederman 			   from_kgid_munged(&init_user_ns, opts->gid));
166d6e48686SLudwig Nussel 	if (opts->mode != DEBUGFS_DEFAULT_MODE)
167d6e48686SLudwig Nussel 		seq_printf(m, ",mode=%o", opts->mode);
168d6e48686SLudwig Nussel 
169d6e48686SLudwig Nussel 	return 0;
170d6e48686SLudwig Nussel }
171d6e48686SLudwig Nussel 
172d6e48686SLudwig Nussel static const struct super_operations debugfs_super_operations = {
173d6e48686SLudwig Nussel 	.statfs		= simple_statfs,
174d6e48686SLudwig Nussel 	.remount_fs	= debugfs_remount,
175d6e48686SLudwig Nussel 	.show_options	= debugfs_show_options,
176d6e48686SLudwig Nussel };
177d6e48686SLudwig Nussel 
178*77b3da6eSAl Viro static struct vfsmount *debugfs_automount(struct path *path)
179*77b3da6eSAl Viro {
180*77b3da6eSAl Viro 	struct vfsmount *(*f)(void *);
181*77b3da6eSAl Viro 	f = (struct vfsmount *(*)(void *))path->dentry->d_fsdata;
182*77b3da6eSAl Viro 	return f(path->dentry->d_inode->i_private);
183*77b3da6eSAl Viro }
184*77b3da6eSAl Viro 
185*77b3da6eSAl Viro static const struct dentry_operations debugfs_dops = {
186*77b3da6eSAl Viro 	.d_delete = always_delete_dentry,
187*77b3da6eSAl Viro 	.d_automount = debugfs_automount,
188*77b3da6eSAl Viro };
189*77b3da6eSAl Viro 
1901da177e4SLinus Torvalds static int debug_fill_super(struct super_block *sb, void *data, int silent)
1911da177e4SLinus Torvalds {
1921da177e4SLinus Torvalds 	static struct tree_descr debug_files[] = {{""}};
193d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi;
194d6e48686SLudwig Nussel 	int err;
1951da177e4SLinus Torvalds 
196d6e48686SLudwig Nussel 	save_mount_options(sb, data);
197d6e48686SLudwig Nussel 
198d6e48686SLudwig Nussel 	fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL);
199d6e48686SLudwig Nussel 	sb->s_fs_info = fsi;
200d6e48686SLudwig Nussel 	if (!fsi) {
201d6e48686SLudwig Nussel 		err = -ENOMEM;
202d6e48686SLudwig Nussel 		goto fail;
203d6e48686SLudwig Nussel 	}
204d6e48686SLudwig Nussel 
205d6e48686SLudwig Nussel 	err = debugfs_parse_options(data, &fsi->mount_opts);
206d6e48686SLudwig Nussel 	if (err)
207d6e48686SLudwig Nussel 		goto fail;
208d6e48686SLudwig Nussel 
209d6e48686SLudwig Nussel 	err  =  simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
210d6e48686SLudwig Nussel 	if (err)
211d6e48686SLudwig Nussel 		goto fail;
212d6e48686SLudwig Nussel 
213d6e48686SLudwig Nussel 	sb->s_op = &debugfs_super_operations;
214*77b3da6eSAl Viro 	sb->s_d_op = &debugfs_dops;
215d6e48686SLudwig Nussel 
216d6e48686SLudwig Nussel 	debugfs_apply_options(sb);
217d6e48686SLudwig Nussel 
218d6e48686SLudwig Nussel 	return 0;
219d6e48686SLudwig Nussel 
220d6e48686SLudwig Nussel fail:
221d6e48686SLudwig Nussel 	kfree(fsi);
222d6e48686SLudwig Nussel 	sb->s_fs_info = NULL;
223d6e48686SLudwig Nussel 	return err;
2241da177e4SLinus Torvalds }
2251da177e4SLinus Torvalds 
226fc14f2feSAl Viro static struct dentry *debug_mount(struct file_system_type *fs_type,
2271da177e4SLinus Torvalds 			int flags, const char *dev_name,
228fc14f2feSAl Viro 			void *data)
2291da177e4SLinus Torvalds {
230fc14f2feSAl Viro 	return mount_single(fs_type, flags, data, debug_fill_super);
2311da177e4SLinus Torvalds }
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds static struct file_system_type debug_fs_type = {
2341da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
2351da177e4SLinus Torvalds 	.name =		"debugfs",
236fc14f2feSAl Viro 	.mount =	debug_mount,
2371da177e4SLinus Torvalds 	.kill_sb =	kill_litter_super,
2381da177e4SLinus Torvalds };
2397f78e035SEric W. Biederman MODULE_ALIAS_FS("debugfs");
2401da177e4SLinus Torvalds 
241190afd81SAl Viro static struct dentry *start_creating(const char *name, struct dentry *parent)
242c3b1a350SAl Viro {
243190afd81SAl Viro 	struct dentry *dentry;
244c3b1a350SAl Viro 	int error;
245c3b1a350SAl Viro 
246c3b1a350SAl Viro 	pr_debug("debugfs: creating file '%s'\n",name);
247c3b1a350SAl Viro 
248c3b1a350SAl Viro 	error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
249c3b1a350SAl Viro 			      &debugfs_mount_count);
250c3b1a350SAl Viro 	if (error)
251190afd81SAl Viro 		return ERR_PTR(error);
252c3b1a350SAl Viro 
253cfa57c11SAl Viro 	/* If the parent is not specified, we create it in the root.
254cfa57c11SAl Viro 	 * We need the root dentry to do this, which is in the super
255cfa57c11SAl Viro 	 * block. A pointer to that is in the struct vfsmount that we
256cfa57c11SAl Viro 	 * have around.
257cfa57c11SAl Viro 	 */
258cfa57c11SAl Viro 	if (!parent)
259cfa57c11SAl Viro 		parent = debugfs_mount->mnt_root;
260cfa57c11SAl Viro 
261cfa57c11SAl Viro 	mutex_lock(&parent->d_inode->i_mutex);
262cfa57c11SAl Viro 	dentry = lookup_one_len(name, parent, strlen(name));
263190afd81SAl Viro 	if (!IS_ERR(dentry) && dentry->d_inode) {
264190afd81SAl Viro 		dput(dentry);
265190afd81SAl Viro 		dentry = ERR_PTR(-EEXIST);
266190afd81SAl Viro 	}
267190afd81SAl Viro 	if (IS_ERR(dentry))
268190afd81SAl Viro 		mutex_unlock(&parent->d_inode->i_mutex);
269190afd81SAl Viro 	return dentry;
270190afd81SAl Viro }
271190afd81SAl Viro 
2725233e311SAl Viro static struct dentry *failed_creating(struct dentry *dentry)
273190afd81SAl Viro {
274190afd81SAl Viro 	mutex_unlock(&dentry->d_parent->d_inode->i_mutex);
275190afd81SAl Viro 	dput(dentry);
276190afd81SAl Viro 	simple_release_fs(&debugfs_mount, &debugfs_mount_count);
2775233e311SAl Viro 	return NULL;
278190afd81SAl Viro }
2795233e311SAl Viro 
2805233e311SAl Viro static struct dentry *end_creating(struct dentry *dentry)
2815233e311SAl Viro {
2825233e311SAl Viro 	mutex_unlock(&dentry->d_parent->d_inode->i_mutex);
283190afd81SAl Viro 	return dentry;
284190afd81SAl Viro }
285190afd81SAl Viro 
2861da177e4SLinus Torvalds /**
2871da177e4SLinus Torvalds  * debugfs_create_file - create a file in the debugfs filesystem
2881da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
289be030e65SAlberto Bertogli  * @mode: the permission that the file should have.
2901da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
291e227867fSMasanari Iida  *          directory dentry if set.  If this parameter is NULL, then the
2921da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
2931da177e4SLinus Torvalds  * @data: a pointer to something that the caller will want to get to later
2948e18e294STheodore Ts'o  *        on.  The inode.i_private pointer will point to this value on
2951da177e4SLinus Torvalds  *        the open() call.
2961da177e4SLinus Torvalds  * @fops: a pointer to a struct file_operations that should be used for
2971da177e4SLinus Torvalds  *        this file.
2981da177e4SLinus Torvalds  *
2991da177e4SLinus Torvalds  * This is the basic "create a file" function for debugfs.  It allows for a
300be030e65SAlberto Bertogli  * wide range of flexibility in creating a file, or a directory (if you want
301be030e65SAlberto Bertogli  * to create a directory, the debugfs_create_dir() function is
3021da177e4SLinus Torvalds  * recommended to be used instead.)
3031da177e4SLinus Torvalds  *
3041da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
3051da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
3061da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
3076468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
3081da177e4SLinus Torvalds  *
3096468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
310873760fbSCornelia Huck  * returned.
3111da177e4SLinus Torvalds  */
312f4ae40a6SAl Viro struct dentry *debugfs_create_file(const char *name, umode_t mode,
3131da177e4SLinus Torvalds 				   struct dentry *parent, void *data,
31499ac48f5SArjan van de Ven 				   const struct file_operations *fops)
3151da177e4SLinus Torvalds {
316ad5abd5bSAl Viro 	struct dentry *dentry;
317680b3024SAl Viro 	struct inode *inode;
318c3b1a350SAl Viro 
319ad5abd5bSAl Viro 	if (!(mode & S_IFMT))
320ad5abd5bSAl Viro 		mode |= S_IFREG;
321ad5abd5bSAl Viro 	BUG_ON(!S_ISREG(mode));
322ad5abd5bSAl Viro 	dentry = start_creating(name, parent);
323ad5abd5bSAl Viro 
324ad5abd5bSAl Viro 	if (IS_ERR(dentry))
325ad5abd5bSAl Viro 		return NULL;
326ad5abd5bSAl Viro 
327edac65eaSAl Viro 	inode = debugfs_get_inode(dentry->d_sb);
328680b3024SAl Viro 	if (unlikely(!inode))
3295233e311SAl Viro 		return failed_creating(dentry);
330680b3024SAl Viro 
331edac65eaSAl Viro 	inode->i_mode = mode;
332edac65eaSAl Viro 	inode->i_fop = fops ? fops : &debugfs_file_operations;
333edac65eaSAl Viro 	inode->i_private = data;
334680b3024SAl Viro 	d_instantiate(dentry, inode);
3353473cde5SAl Viro 	fsnotify_create(dentry->d_parent->d_inode, dentry);
3365233e311SAl Viro 	return end_creating(dentry);
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_file);
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds /**
3411da177e4SLinus Torvalds  * debugfs_create_dir - create a directory in the debugfs filesystem
3421da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the directory to
3431da177e4SLinus Torvalds  *        create.
3441da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
345e227867fSMasanari Iida  *          directory dentry if set.  If this parameter is NULL, then the
3461da177e4SLinus Torvalds  *          directory will be created in the root of the debugfs filesystem.
3471da177e4SLinus Torvalds  *
3481da177e4SLinus Torvalds  * This function creates a directory in debugfs with the given name.
3491da177e4SLinus Torvalds  *
3501da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
3511da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
3521da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
3536468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
3541da177e4SLinus Torvalds  *
3556468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
356873760fbSCornelia Huck  * returned.
3571da177e4SLinus Torvalds  */
3581da177e4SLinus Torvalds struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
3591da177e4SLinus Torvalds {
360ad5abd5bSAl Viro 	struct dentry *dentry = start_creating(name, parent);
361680b3024SAl Viro 	struct inode *inode;
362ad5abd5bSAl Viro 
363ad5abd5bSAl Viro 	if (IS_ERR(dentry))
364ad5abd5bSAl Viro 		return NULL;
365ad5abd5bSAl Viro 
366edac65eaSAl Viro 	inode = debugfs_get_inode(dentry->d_sb);
367680b3024SAl Viro 	if (unlikely(!inode))
3685233e311SAl Viro 		return failed_creating(dentry);
369680b3024SAl Viro 
370edac65eaSAl Viro 	inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
371edac65eaSAl Viro 	inode->i_op = &simple_dir_inode_operations;
372edac65eaSAl Viro 	inode->i_fop = &simple_dir_operations;
373edac65eaSAl Viro 
374edac65eaSAl Viro 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
375edac65eaSAl Viro 	inc_nlink(inode);
376680b3024SAl Viro 	d_instantiate(dentry, inode);
37702538a75SAl Viro 	inc_nlink(dentry->d_parent->d_inode);
37802538a75SAl Viro 	fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
3795233e311SAl Viro 	return end_creating(dentry);
3801da177e4SLinus Torvalds }
3811da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_dir);
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds /**
384*77b3da6eSAl Viro  * debugfs_create_automount - create automount point in the debugfs filesystem
385*77b3da6eSAl Viro  * @name: a pointer to a string containing the name of the file to create.
386*77b3da6eSAl Viro  * @parent: a pointer to the parent dentry for this file.  This should be a
387*77b3da6eSAl Viro  *          directory dentry if set.  If this parameter is NULL, then the
388*77b3da6eSAl Viro  *          file will be created in the root of the debugfs filesystem.
389*77b3da6eSAl Viro  * @f: function to be called when pathname resolution steps on that one.
390*77b3da6eSAl Viro  * @data: opaque argument to pass to f().
391*77b3da6eSAl Viro  *
392*77b3da6eSAl Viro  * @f should return what ->d_automount() would.
393*77b3da6eSAl Viro  */
394*77b3da6eSAl Viro struct dentry *debugfs_create_automount(const char *name,
395*77b3da6eSAl Viro 					struct dentry *parent,
396*77b3da6eSAl Viro 					struct vfsmount *(*f)(void *),
397*77b3da6eSAl Viro 					void *data)
398*77b3da6eSAl Viro {
399*77b3da6eSAl Viro 	struct dentry *dentry = start_creating(name, parent);
400*77b3da6eSAl Viro 	struct inode *inode;
401*77b3da6eSAl Viro 
402*77b3da6eSAl Viro 	if (IS_ERR(dentry))
403*77b3da6eSAl Viro 		return NULL;
404*77b3da6eSAl Viro 
405*77b3da6eSAl Viro 	inode = debugfs_get_inode(dentry->d_sb);
406*77b3da6eSAl Viro 	if (unlikely(!inode))
407*77b3da6eSAl Viro 		return failed_creating(dentry);
408*77b3da6eSAl Viro 
409*77b3da6eSAl Viro 	inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
410*77b3da6eSAl Viro 	inode->i_flags |= S_AUTOMOUNT;
411*77b3da6eSAl Viro 	inode->i_private = data;
412*77b3da6eSAl Viro 	dentry->d_fsdata = (void *)f;
413*77b3da6eSAl Viro 	d_instantiate(dentry, inode);
414*77b3da6eSAl Viro 	return end_creating(dentry);
415*77b3da6eSAl Viro }
416*77b3da6eSAl Viro EXPORT_SYMBOL(debugfs_create_automount);
417*77b3da6eSAl Viro 
418*77b3da6eSAl Viro /**
41966f54963SPeter Oberparleiter  * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
42066f54963SPeter Oberparleiter  * @name: a pointer to a string containing the name of the symbolic link to
42166f54963SPeter Oberparleiter  *        create.
42266f54963SPeter Oberparleiter  * @parent: a pointer to the parent dentry for this symbolic link.  This
423e227867fSMasanari Iida  *          should be a directory dentry if set.  If this parameter is NULL,
42466f54963SPeter Oberparleiter  *          then the symbolic link will be created in the root of the debugfs
42566f54963SPeter Oberparleiter  *          filesystem.
42666f54963SPeter Oberparleiter  * @target: a pointer to a string containing the path to the target of the
42766f54963SPeter Oberparleiter  *          symbolic link.
42866f54963SPeter Oberparleiter  *
42966f54963SPeter Oberparleiter  * This function creates a symbolic link with the given name in debugfs that
43066f54963SPeter Oberparleiter  * links to the given target path.
43166f54963SPeter Oberparleiter  *
43266f54963SPeter Oberparleiter  * This function will return a pointer to a dentry if it succeeds.  This
43366f54963SPeter Oberparleiter  * pointer must be passed to the debugfs_remove() function when the symbolic
43466f54963SPeter Oberparleiter  * link is to be removed (no automatic cleanup happens if your module is
43566f54963SPeter Oberparleiter  * unloaded, you are responsible here.)  If an error occurs, %NULL will be
43666f54963SPeter Oberparleiter  * returned.
43766f54963SPeter Oberparleiter  *
43866f54963SPeter Oberparleiter  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
439873760fbSCornelia Huck  * returned.
44066f54963SPeter Oberparleiter  */
44166f54963SPeter Oberparleiter struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
44266f54963SPeter Oberparleiter 				      const char *target)
44366f54963SPeter Oberparleiter {
444ad5abd5bSAl Viro 	struct dentry *dentry;
445680b3024SAl Viro 	struct inode *inode;
446680b3024SAl Viro 	char *link = kstrdup(target, GFP_KERNEL);
44766f54963SPeter Oberparleiter 	if (!link)
44866f54963SPeter Oberparleiter 		return NULL;
44966f54963SPeter Oberparleiter 
450ad5abd5bSAl Viro 	dentry = start_creating(name, parent);
451ad5abd5bSAl Viro 	if (IS_ERR(dentry)) {
45266f54963SPeter Oberparleiter 		kfree(link);
453ad5abd5bSAl Viro 		return NULL;
454ad5abd5bSAl Viro 	}
455ad5abd5bSAl Viro 
456edac65eaSAl Viro 	inode = debugfs_get_inode(dentry->d_sb);
457680b3024SAl Viro 	if (unlikely(!inode)) {
458ad5abd5bSAl Viro 		kfree(link);
4595233e311SAl Viro 		return failed_creating(dentry);
460680b3024SAl Viro 	}
461edac65eaSAl Viro 	inode->i_mode = S_IFLNK | S_IRWXUGO;
462edac65eaSAl Viro 	inode->i_op = &debugfs_link_operations;
463edac65eaSAl Viro 	inode->i_private = link;
464680b3024SAl Viro 	d_instantiate(dentry, inode);
4655233e311SAl Viro 	return end_creating(dentry);
46666f54963SPeter Oberparleiter }
46766f54963SPeter Oberparleiter EXPORT_SYMBOL_GPL(debugfs_create_symlink);
46866f54963SPeter Oberparleiter 
46925d41d84SJan Kara static int __debugfs_remove(struct dentry *dentry, struct dentry *parent)
4701da177e4SLinus Torvalds {
47165c33336SMathieu Desnoyers 	int ret = 0;
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	if (debugfs_positive(dentry)) {
4741da177e4SLinus Torvalds 		if (dentry->d_inode) {
47529a7f3adSMathieu Desnoyers 			dget(dentry);
47666f54963SPeter Oberparleiter 			switch (dentry->d_inode->i_mode & S_IFMT) {
47766f54963SPeter Oberparleiter 			case S_IFDIR:
47865c33336SMathieu Desnoyers 				ret = simple_rmdir(parent->d_inode, dentry);
47966f54963SPeter Oberparleiter 				break;
48066f54963SPeter Oberparleiter 			case S_IFLNK:
48166f54963SPeter Oberparleiter 				kfree(dentry->d_inode->i_private);
48266f54963SPeter Oberparleiter 				/* fall through */
48366f54963SPeter Oberparleiter 			default:
4841da177e4SLinus Torvalds 				simple_unlink(parent->d_inode, dentry);
48566f54963SPeter Oberparleiter 				break;
48666f54963SPeter Oberparleiter 			}
48729a7f3adSMathieu Desnoyers 			if (!ret)
48829a7f3adSMathieu Desnoyers 				d_delete(dentry);
48929a7f3adSMathieu Desnoyers 			dput(dentry);
4901da177e4SLinus Torvalds 		}
4911da177e4SLinus Torvalds 	}
49225d41d84SJan Kara 	return ret;
4939505e637SHaavard Skinnemoen }
4949505e637SHaavard Skinnemoen 
4959505e637SHaavard Skinnemoen /**
4969505e637SHaavard Skinnemoen  * debugfs_remove - removes a file or directory from the debugfs filesystem
4979505e637SHaavard Skinnemoen  * @dentry: a pointer to a the dentry of the file or directory to be
4989505e637SHaavard Skinnemoen  *          removed.
4999505e637SHaavard Skinnemoen  *
5009505e637SHaavard Skinnemoen  * This function removes a file or directory in debugfs that was previously
5019505e637SHaavard Skinnemoen  * created with a call to another debugfs function (like
5029505e637SHaavard Skinnemoen  * debugfs_create_file() or variants thereof.)
5039505e637SHaavard Skinnemoen  *
5049505e637SHaavard Skinnemoen  * This function is required to be called in order for the file to be
5059505e637SHaavard Skinnemoen  * removed, no automatic cleanup of files will happen when a module is
5069505e637SHaavard Skinnemoen  * removed, you are responsible here.
5079505e637SHaavard Skinnemoen  */
5089505e637SHaavard Skinnemoen void debugfs_remove(struct dentry *dentry)
5099505e637SHaavard Skinnemoen {
5109505e637SHaavard Skinnemoen 	struct dentry *parent;
51125d41d84SJan Kara 	int ret;
5129505e637SHaavard Skinnemoen 
513a59d6293SArend van Spriel 	if (IS_ERR_OR_NULL(dentry))
5149505e637SHaavard Skinnemoen 		return;
5159505e637SHaavard Skinnemoen 
5169505e637SHaavard Skinnemoen 	parent = dentry->d_parent;
5179505e637SHaavard Skinnemoen 	if (!parent || !parent->d_inode)
5189505e637SHaavard Skinnemoen 		return;
5199505e637SHaavard Skinnemoen 
5209505e637SHaavard Skinnemoen 	mutex_lock(&parent->d_inode->i_mutex);
52125d41d84SJan Kara 	ret = __debugfs_remove(dentry, parent);
5221b1dcc1bSJes Sorensen 	mutex_unlock(&parent->d_inode->i_mutex);
52325d41d84SJan Kara 	if (!ret)
5241da177e4SLinus Torvalds 		simple_release_fs(&debugfs_mount, &debugfs_mount_count);
5251da177e4SLinus Torvalds }
5261da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_remove);
5271da177e4SLinus Torvalds 
528cfc94cdfSJan Kara /**
5299505e637SHaavard Skinnemoen  * debugfs_remove_recursive - recursively removes a directory
5309505e637SHaavard Skinnemoen  * @dentry: a pointer to a the dentry of the directory to be removed.
5319505e637SHaavard Skinnemoen  *
5329505e637SHaavard Skinnemoen  * This function recursively removes a directory tree in debugfs that
5339505e637SHaavard Skinnemoen  * was previously created with a call to another debugfs function
5349505e637SHaavard Skinnemoen  * (like debugfs_create_file() or variants thereof.)
5359505e637SHaavard Skinnemoen  *
5369505e637SHaavard Skinnemoen  * This function is required to be called in order for the file to be
5379505e637SHaavard Skinnemoen  * removed, no automatic cleanup of files will happen when a module is
5389505e637SHaavard Skinnemoen  * removed, you are responsible here.
5399505e637SHaavard Skinnemoen  */
5409505e637SHaavard Skinnemoen void debugfs_remove_recursive(struct dentry *dentry)
5419505e637SHaavard Skinnemoen {
542485d4402SSteven Rostedt 	struct dentry *child, *parent;
5439505e637SHaavard Skinnemoen 
544a59d6293SArend van Spriel 	if (IS_ERR_OR_NULL(dentry))
5459505e637SHaavard Skinnemoen 		return;
5469505e637SHaavard Skinnemoen 
5479505e637SHaavard Skinnemoen 	parent = dentry->d_parent;
5489505e637SHaavard Skinnemoen 	if (!parent || !parent->d_inode)
5499505e637SHaavard Skinnemoen 		return;
5509505e637SHaavard Skinnemoen 
5519505e637SHaavard Skinnemoen 	parent = dentry;
552776164c1SOleg Nesterov  down:
5539505e637SHaavard Skinnemoen 	mutex_lock(&parent->d_inode->i_mutex);
554485d4402SSteven Rostedt  loop:
555485d4402SSteven Rostedt 	/*
556485d4402SSteven Rostedt 	 * The parent->d_subdirs is protected by the d_lock. Outside that
557485d4402SSteven Rostedt 	 * lock, the child can be unlinked and set to be freed which can
558485d4402SSteven Rostedt 	 * use the d_u.d_child as the rcu head and corrupt this list.
559485d4402SSteven Rostedt 	 */
560485d4402SSteven Rostedt 	spin_lock(&parent->d_lock);
561946e51f2SAl Viro 	list_for_each_entry(child, &parent->d_subdirs, d_child) {
562776164c1SOleg Nesterov 		if (!debugfs_positive(child))
563776164c1SOleg Nesterov 			continue;
5649505e637SHaavard Skinnemoen 
565776164c1SOleg Nesterov 		/* perhaps simple_empty(child) makes more sense */
5669505e637SHaavard Skinnemoen 		if (!list_empty(&child->d_subdirs)) {
567485d4402SSteven Rostedt 			spin_unlock(&parent->d_lock);
5689505e637SHaavard Skinnemoen 			mutex_unlock(&parent->d_inode->i_mutex);
5699505e637SHaavard Skinnemoen 			parent = child;
570776164c1SOleg Nesterov 			goto down;
5719505e637SHaavard Skinnemoen 		}
572485d4402SSteven Rostedt 
573485d4402SSteven Rostedt 		spin_unlock(&parent->d_lock);
574485d4402SSteven Rostedt 
575776164c1SOleg Nesterov 		if (!__debugfs_remove(child, parent))
576776164c1SOleg Nesterov 			simple_release_fs(&debugfs_mount, &debugfs_mount_count);
577485d4402SSteven Rostedt 
578485d4402SSteven Rostedt 		/*
579485d4402SSteven Rostedt 		 * The parent->d_lock protects agaist child from unlinking
580485d4402SSteven Rostedt 		 * from d_subdirs. When releasing the parent->d_lock we can
581485d4402SSteven Rostedt 		 * no longer trust that the next pointer is valid.
582485d4402SSteven Rostedt 		 * Restart the loop. We'll skip this one with the
583485d4402SSteven Rostedt 		 * debugfs_positive() check.
584485d4402SSteven Rostedt 		 */
585485d4402SSteven Rostedt 		goto loop;
586776164c1SOleg Nesterov 	}
587485d4402SSteven Rostedt 	spin_unlock(&parent->d_lock);
588776164c1SOleg Nesterov 
589776164c1SOleg Nesterov 	mutex_unlock(&parent->d_inode->i_mutex);
590776164c1SOleg Nesterov 	child = parent;
591776164c1SOleg Nesterov 	parent = parent->d_parent;
592776164c1SOleg Nesterov 	mutex_lock(&parent->d_inode->i_mutex);
593776164c1SOleg Nesterov 
594485d4402SSteven Rostedt 	if (child != dentry)
595485d4402SSteven Rostedt 		/* go up */
596485d4402SSteven Rostedt 		goto loop;
59756a83cc9SSteven Rostedt 
598776164c1SOleg Nesterov 	if (!__debugfs_remove(child, parent))
5999505e637SHaavard Skinnemoen 		simple_release_fs(&debugfs_mount, &debugfs_mount_count);
6009505e637SHaavard Skinnemoen 	mutex_unlock(&parent->d_inode->i_mutex);
6019505e637SHaavard Skinnemoen }
6029505e637SHaavard Skinnemoen EXPORT_SYMBOL_GPL(debugfs_remove_recursive);
6039505e637SHaavard Skinnemoen 
6049505e637SHaavard Skinnemoen /**
605cfc94cdfSJan Kara  * debugfs_rename - rename a file/directory in the debugfs filesystem
606cfc94cdfSJan Kara  * @old_dir: a pointer to the parent dentry for the renamed object. This
607cfc94cdfSJan Kara  *          should be a directory dentry.
608cfc94cdfSJan Kara  * @old_dentry: dentry of an object to be renamed.
609cfc94cdfSJan Kara  * @new_dir: a pointer to the parent dentry where the object should be
610cfc94cdfSJan Kara  *          moved. This should be a directory dentry.
611cfc94cdfSJan Kara  * @new_name: a pointer to a string containing the target name.
612cfc94cdfSJan Kara  *
613cfc94cdfSJan Kara  * This function renames a file/directory in debugfs.  The target must not
614cfc94cdfSJan Kara  * exist for rename to succeed.
615cfc94cdfSJan Kara  *
616cfc94cdfSJan Kara  * This function will return a pointer to old_dentry (which is updated to
617cfc94cdfSJan Kara  * reflect renaming) if it succeeds. If an error occurs, %NULL will be
618cfc94cdfSJan Kara  * returned.
619cfc94cdfSJan Kara  *
620cfc94cdfSJan Kara  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
621cfc94cdfSJan Kara  * returned.
622cfc94cdfSJan Kara  */
623cfc94cdfSJan Kara struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
624cfc94cdfSJan Kara 		struct dentry *new_dir, const char *new_name)
625cfc94cdfSJan Kara {
626cfc94cdfSJan Kara 	int error;
627cfc94cdfSJan Kara 	struct dentry *dentry = NULL, *trap;
628cfc94cdfSJan Kara 	const char *old_name;
629cfc94cdfSJan Kara 
630cfc94cdfSJan Kara 	trap = lock_rename(new_dir, old_dir);
631cfc94cdfSJan Kara 	/* Source or destination directories don't exist? */
632cfc94cdfSJan Kara 	if (!old_dir->d_inode || !new_dir->d_inode)
633cfc94cdfSJan Kara 		goto exit;
634cfc94cdfSJan Kara 	/* Source does not exist, cyclic rename, or mountpoint? */
635cfc94cdfSJan Kara 	if (!old_dentry->d_inode || old_dentry == trap ||
636cfc94cdfSJan Kara 	    d_mountpoint(old_dentry))
637cfc94cdfSJan Kara 		goto exit;
638cfc94cdfSJan Kara 	dentry = lookup_one_len(new_name, new_dir, strlen(new_name));
639cfc94cdfSJan Kara 	/* Lookup failed, cyclic rename or target exists? */
640cfc94cdfSJan Kara 	if (IS_ERR(dentry) || dentry == trap || dentry->d_inode)
641cfc94cdfSJan Kara 		goto exit;
642cfc94cdfSJan Kara 
643cfc94cdfSJan Kara 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
644cfc94cdfSJan Kara 
645cfc94cdfSJan Kara 	error = simple_rename(old_dir->d_inode, old_dentry, new_dir->d_inode,
646cfc94cdfSJan Kara 		dentry);
647cfc94cdfSJan Kara 	if (error) {
648cfc94cdfSJan Kara 		fsnotify_oldname_free(old_name);
649cfc94cdfSJan Kara 		goto exit;
650cfc94cdfSJan Kara 	}
651cfc94cdfSJan Kara 	d_move(old_dentry, dentry);
652cfc94cdfSJan Kara 	fsnotify_move(old_dir->d_inode, new_dir->d_inode, old_name,
653123df294SAl Viro 		S_ISDIR(old_dentry->d_inode->i_mode),
6545a190ae6SAl Viro 		NULL, old_dentry);
655cfc94cdfSJan Kara 	fsnotify_oldname_free(old_name);
656cfc94cdfSJan Kara 	unlock_rename(new_dir, old_dir);
657cfc94cdfSJan Kara 	dput(dentry);
658cfc94cdfSJan Kara 	return old_dentry;
659cfc94cdfSJan Kara exit:
660cfc94cdfSJan Kara 	if (dentry && !IS_ERR(dentry))
661cfc94cdfSJan Kara 		dput(dentry);
662cfc94cdfSJan Kara 	unlock_rename(new_dir, old_dir);
663cfc94cdfSJan Kara 	return NULL;
664cfc94cdfSJan Kara }
665cfc94cdfSJan Kara EXPORT_SYMBOL_GPL(debugfs_rename);
666cfc94cdfSJan Kara 
667c0f92ba9SFrederic Weisbecker /**
668c0f92ba9SFrederic Weisbecker  * debugfs_initialized - Tells whether debugfs has been registered
669c0f92ba9SFrederic Weisbecker  */
670c0f92ba9SFrederic Weisbecker bool debugfs_initialized(void)
671c0f92ba9SFrederic Weisbecker {
672c0f92ba9SFrederic Weisbecker 	return debugfs_registered;
673c0f92ba9SFrederic Weisbecker }
674c0f92ba9SFrederic Weisbecker EXPORT_SYMBOL_GPL(debugfs_initialized);
675c0f92ba9SFrederic Weisbecker 
676c0f92ba9SFrederic Weisbecker 
677191e186bSGreg Kroah-Hartman static struct kobject *debug_kobj;
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds static int __init debugfs_init(void)
6801da177e4SLinus Torvalds {
6811da177e4SLinus Torvalds 	int retval;
6821da177e4SLinus Torvalds 
6830ff21e46SGreg Kroah-Hartman 	debug_kobj = kobject_create_and_add("debug", kernel_kobj);
684191e186bSGreg Kroah-Hartman 	if (!debug_kobj)
685191e186bSGreg Kroah-Hartman 		return -EINVAL;
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 	retval = register_filesystem(&debug_fs_type);
6881da177e4SLinus Torvalds 	if (retval)
689197b12d6SGreg Kroah-Hartman 		kobject_put(debug_kobj);
690c0f92ba9SFrederic Weisbecker 	else
691c0f92ba9SFrederic Weisbecker 		debugfs_registered = true;
692c0f92ba9SFrederic Weisbecker 
6931da177e4SLinus Torvalds 	return retval;
6941da177e4SLinus Torvalds }
6951da177e4SLinus Torvalds core_initcall(debugfs_init);
6961da177e4SLinus Torvalds 
697