xref: /linux-6.15/fs/debugfs/inode.c (revision 526ee72d)
13bce94fdSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
3bd33d12fSHarry Wei  *  inode.c - part of debugfs, a tiny little debug file system
41da177e4SLinus Torvalds  *
543e23b6cSGreg Kroah-Hartman  *  Copyright (C) 2004,2019 Greg Kroah-Hartman <[email protected]>
61da177e4SLinus Torvalds  *  Copyright (C) 2004 IBM Inc.
743e23b6cSGreg Kroah-Hartman  *  Copyright (C) 2019 Linux Foundation <[email protected]>
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *  debugfs is for people to use instead of /proc or /sys.
10e1511a84SMauro Carvalho Chehab  *  See ./Documentation/core-api/kernel-api.rst for more details.
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
13d03ae477SGreg Kroah-Hartman #define pr_fmt(fmt)	"debugfs: " fmt
14d03ae477SGreg Kroah-Hartman 
151da177e4SLinus Torvalds #include <linux/module.h>
161da177e4SLinus Torvalds #include <linux/fs.h>
171da177e4SLinus Torvalds #include <linux/mount.h>
181da177e4SLinus Torvalds #include <linux/pagemap.h>
191da177e4SLinus Torvalds #include <linux/init.h>
204d8ebddcSRandy Dunlap #include <linux/kobject.h>
211da177e4SLinus Torvalds #include <linux/namei.h>
221da177e4SLinus Torvalds #include <linux/debugfs.h>
234f36557fSMathieu Desnoyers #include <linux/fsnotify.h>
2466f54963SPeter Oberparleiter #include <linux/string.h>
25d6e48686SLudwig Nussel #include <linux/seq_file.h>
26d6e48686SLudwig Nussel #include <linux/parser.h>
2792562927SMimi Zohar #include <linux/magic.h>
285a0e3ad6STejun Heo #include <linux/slab.h>
295496197fSDavid Howells #include <linux/security.h>
309fd4dcecSNicolai Stange 
319fd4dcecSNicolai Stange #include "internal.h"
321da177e4SLinus Torvalds 
3382aceae4SKees Cook #define DEBUGFS_DEFAULT_MODE	0700
34d6e48686SLudwig Nussel 
351da177e4SLinus Torvalds static struct vfsmount *debugfs_mount;
361da177e4SLinus Torvalds static int debugfs_mount_count;
37c0f92ba9SFrederic Weisbecker static bool debugfs_registered;
381da177e4SLinus Torvalds 
395496197fSDavid Howells /*
405496197fSDavid Howells  * Don't allow access attributes to be changed whilst the kernel is locked down
415496197fSDavid Howells  * so that we can use the file mode as part of a heuristic to determine whether
425496197fSDavid Howells  * to lock down individual files.
435496197fSDavid Howells  */
445496197fSDavid Howells static int debugfs_setattr(struct dentry *dentry, struct iattr *ia)
455496197fSDavid Howells {
465496197fSDavid Howells 	int ret = security_locked_down(LOCKDOWN_DEBUGFS);
475496197fSDavid Howells 
485496197fSDavid Howells 	if (ret && (ia->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)))
495496197fSDavid Howells 		return ret;
505496197fSDavid Howells 	return simple_setattr(dentry, ia);
515496197fSDavid Howells }
525496197fSDavid Howells 
535496197fSDavid Howells static const struct inode_operations debugfs_file_inode_operations = {
545496197fSDavid Howells 	.setattr	= debugfs_setattr,
555496197fSDavid Howells };
565496197fSDavid Howells static const struct inode_operations debugfs_dir_inode_operations = {
575496197fSDavid Howells 	.lookup		= simple_lookup,
585496197fSDavid Howells 	.setattr	= debugfs_setattr,
595496197fSDavid Howells };
605496197fSDavid Howells static const struct inode_operations debugfs_symlink_inode_operations = {
615496197fSDavid Howells 	.get_link	= simple_get_link,
625496197fSDavid Howells 	.setattr	= debugfs_setattr,
635496197fSDavid Howells };
645496197fSDavid Howells 
65edac65eaSAl Viro static struct inode *debugfs_get_inode(struct super_block *sb)
661da177e4SLinus Torvalds {
671da177e4SLinus Torvalds 	struct inode *inode = new_inode(sb);
681da177e4SLinus Torvalds 	if (inode) {
6985fe4025SChristoph Hellwig 		inode->i_ino = get_next_ino();
701b48b530SDeepa Dinamani 		inode->i_atime = inode->i_mtime =
71c2050a45SDeepa Dinamani 			inode->i_ctime = current_time(inode);
721da177e4SLinus Torvalds 	}
731da177e4SLinus Torvalds 	return inode;
741da177e4SLinus Torvalds }
751da177e4SLinus Torvalds 
76d6e48686SLudwig Nussel struct debugfs_mount_opts {
777dc05881SEric W. Biederman 	kuid_t uid;
787dc05881SEric W. Biederman 	kgid_t gid;
79d6e48686SLudwig Nussel 	umode_t mode;
80d6e48686SLudwig Nussel };
81d6e48686SLudwig Nussel 
82d6e48686SLudwig Nussel enum {
83d6e48686SLudwig Nussel 	Opt_uid,
84d6e48686SLudwig Nussel 	Opt_gid,
85d6e48686SLudwig Nussel 	Opt_mode,
86d6e48686SLudwig Nussel 	Opt_err
87d6e48686SLudwig Nussel };
88d6e48686SLudwig Nussel 
89d6e48686SLudwig Nussel static const match_table_t tokens = {
90d6e48686SLudwig Nussel 	{Opt_uid, "uid=%u"},
91d6e48686SLudwig Nussel 	{Opt_gid, "gid=%u"},
92d6e48686SLudwig Nussel 	{Opt_mode, "mode=%o"},
93d6e48686SLudwig Nussel 	{Opt_err, NULL}
94d6e48686SLudwig Nussel };
95d6e48686SLudwig Nussel 
96d6e48686SLudwig Nussel struct debugfs_fs_info {
97d6e48686SLudwig Nussel 	struct debugfs_mount_opts mount_opts;
98d6e48686SLudwig Nussel };
99d6e48686SLudwig Nussel 
100d6e48686SLudwig Nussel static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts)
101d6e48686SLudwig Nussel {
102d6e48686SLudwig Nussel 	substring_t args[MAX_OPT_ARGS];
103d6e48686SLudwig Nussel 	int option;
104d6e48686SLudwig Nussel 	int token;
1057dc05881SEric W. Biederman 	kuid_t uid;
1067dc05881SEric W. Biederman 	kgid_t gid;
107d6e48686SLudwig Nussel 	char *p;
108d6e48686SLudwig Nussel 
109d6e48686SLudwig Nussel 	opts->mode = DEBUGFS_DEFAULT_MODE;
110d6e48686SLudwig Nussel 
111d6e48686SLudwig Nussel 	while ((p = strsep(&data, ",")) != NULL) {
112d6e48686SLudwig Nussel 		if (!*p)
113d6e48686SLudwig Nussel 			continue;
114d6e48686SLudwig Nussel 
115d6e48686SLudwig Nussel 		token = match_token(p, tokens, args);
116d6e48686SLudwig Nussel 		switch (token) {
117d6e48686SLudwig Nussel 		case Opt_uid:
118d6e48686SLudwig Nussel 			if (match_int(&args[0], &option))
119d6e48686SLudwig Nussel 				return -EINVAL;
1207dc05881SEric W. Biederman 			uid = make_kuid(current_user_ns(), option);
1217dc05881SEric W. Biederman 			if (!uid_valid(uid))
1227dc05881SEric W. Biederman 				return -EINVAL;
1237dc05881SEric W. Biederman 			opts->uid = uid;
124d6e48686SLudwig Nussel 			break;
125d6e48686SLudwig Nussel 		case Opt_gid:
126f1688e04SDave Reisner 			if (match_int(&args[0], &option))
127d6e48686SLudwig Nussel 				return -EINVAL;
1287dc05881SEric W. Biederman 			gid = make_kgid(current_user_ns(), option);
1297dc05881SEric W. Biederman 			if (!gid_valid(gid))
1307dc05881SEric W. Biederman 				return -EINVAL;
1317dc05881SEric W. Biederman 			opts->gid = gid;
132d6e48686SLudwig Nussel 			break;
133d6e48686SLudwig Nussel 		case Opt_mode:
134d6e48686SLudwig Nussel 			if (match_octal(&args[0], &option))
135d6e48686SLudwig Nussel 				return -EINVAL;
136d6e48686SLudwig Nussel 			opts->mode = option & S_IALLUGO;
137d6e48686SLudwig Nussel 			break;
138d6e48686SLudwig Nussel 		/*
139d6e48686SLudwig Nussel 		 * We might like to report bad mount options here;
140d6e48686SLudwig Nussel 		 * but traditionally debugfs has ignored all mount options
141d6e48686SLudwig Nussel 		 */
142d6e48686SLudwig Nussel 		}
143d6e48686SLudwig Nussel 	}
144d6e48686SLudwig Nussel 
145d6e48686SLudwig Nussel 	return 0;
146d6e48686SLudwig Nussel }
147d6e48686SLudwig Nussel 
148d6e48686SLudwig Nussel static int debugfs_apply_options(struct super_block *sb)
149d6e48686SLudwig Nussel {
150d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi = sb->s_fs_info;
1512b0143b5SDavid Howells 	struct inode *inode = d_inode(sb->s_root);
152d6e48686SLudwig Nussel 	struct debugfs_mount_opts *opts = &fsi->mount_opts;
153d6e48686SLudwig Nussel 
154d6e48686SLudwig Nussel 	inode->i_mode &= ~S_IALLUGO;
155d6e48686SLudwig Nussel 	inode->i_mode |= opts->mode;
156d6e48686SLudwig Nussel 
157d6e48686SLudwig Nussel 	inode->i_uid = opts->uid;
158d6e48686SLudwig Nussel 	inode->i_gid = opts->gid;
159d6e48686SLudwig Nussel 
160d6e48686SLudwig Nussel 	return 0;
161d6e48686SLudwig Nussel }
162d6e48686SLudwig Nussel 
163d6e48686SLudwig Nussel static int debugfs_remount(struct super_block *sb, int *flags, char *data)
164d6e48686SLudwig Nussel {
165d6e48686SLudwig Nussel 	int err;
166d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi = sb->s_fs_info;
167d6e48686SLudwig Nussel 
16802b9984dSTheodore Ts'o 	sync_filesystem(sb);
169d6e48686SLudwig Nussel 	err = debugfs_parse_options(data, &fsi->mount_opts);
170d6e48686SLudwig Nussel 	if (err)
171d6e48686SLudwig Nussel 		goto fail;
172d6e48686SLudwig Nussel 
173d6e48686SLudwig Nussel 	debugfs_apply_options(sb);
174d6e48686SLudwig Nussel 
175d6e48686SLudwig Nussel fail:
176d6e48686SLudwig Nussel 	return err;
177d6e48686SLudwig Nussel }
178d6e48686SLudwig Nussel 
179d6e48686SLudwig Nussel static int debugfs_show_options(struct seq_file *m, struct dentry *root)
180d6e48686SLudwig Nussel {
181d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi = root->d_sb->s_fs_info;
182d6e48686SLudwig Nussel 	struct debugfs_mount_opts *opts = &fsi->mount_opts;
183d6e48686SLudwig Nussel 
1847dc05881SEric W. Biederman 	if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
1857dc05881SEric W. Biederman 		seq_printf(m, ",uid=%u",
1867dc05881SEric W. Biederman 			   from_kuid_munged(&init_user_ns, opts->uid));
1877dc05881SEric W. Biederman 	if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
1887dc05881SEric W. Biederman 		seq_printf(m, ",gid=%u",
1897dc05881SEric W. Biederman 			   from_kgid_munged(&init_user_ns, opts->gid));
190d6e48686SLudwig Nussel 	if (opts->mode != DEBUGFS_DEFAULT_MODE)
191d6e48686SLudwig Nussel 		seq_printf(m, ",mode=%o", opts->mode);
192d6e48686SLudwig Nussel 
193d6e48686SLudwig Nussel 	return 0;
194d6e48686SLudwig Nussel }
195d6e48686SLudwig Nussel 
1966234ddf4SAl Viro static void debugfs_free_inode(struct inode *inode)
1970db59e59SAl Viro {
1980db59e59SAl Viro 	if (S_ISLNK(inode->i_mode))
1995723cb01SAl Viro 		kfree(inode->i_link);
20093b919daSAl Viro 	free_inode_nonrcu(inode);
20193b919daSAl Viro }
20293b919daSAl Viro 
203d6e48686SLudwig Nussel static const struct super_operations debugfs_super_operations = {
204d6e48686SLudwig Nussel 	.statfs		= simple_statfs,
205d6e48686SLudwig Nussel 	.remount_fs	= debugfs_remount,
206d6e48686SLudwig Nussel 	.show_options	= debugfs_show_options,
2076234ddf4SAl Viro 	.free_inode	= debugfs_free_inode,
208d6e48686SLudwig Nussel };
209d6e48686SLudwig Nussel 
2107c8d4698SNicolai Stange static void debugfs_release_dentry(struct dentry *dentry)
2117c8d4698SNicolai Stange {
2127d39bc50SNicolai Stange 	void *fsd = dentry->d_fsdata;
2137d39bc50SNicolai Stange 
2147d39bc50SNicolai Stange 	if (!((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT))
2157c8d4698SNicolai Stange 		kfree(dentry->d_fsdata);
2167c8d4698SNicolai Stange }
2177c8d4698SNicolai Stange 
21877b3da6eSAl Viro static struct vfsmount *debugfs_automount(struct path *path)
21977b3da6eSAl Viro {
22093faccbbSEric W. Biederman 	debugfs_automount_t f;
22193faccbbSEric W. Biederman 	f = (debugfs_automount_t)path->dentry->d_fsdata;
22293faccbbSEric W. Biederman 	return f(path->dentry, d_inode(path->dentry)->i_private);
22377b3da6eSAl Viro }
22477b3da6eSAl Viro 
22577b3da6eSAl Viro static const struct dentry_operations debugfs_dops = {
22677b3da6eSAl Viro 	.d_delete = always_delete_dentry,
2277c8d4698SNicolai Stange 	.d_release = debugfs_release_dentry,
22877b3da6eSAl Viro 	.d_automount = debugfs_automount,
22977b3da6eSAl Viro };
23077b3da6eSAl Viro 
2311da177e4SLinus Torvalds static int debug_fill_super(struct super_block *sb, void *data, int silent)
2321da177e4SLinus Torvalds {
233cda37124SEric Biggers 	static const struct tree_descr debug_files[] = {{""}};
234d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi;
235d6e48686SLudwig Nussel 	int err;
2361da177e4SLinus Torvalds 
237d6e48686SLudwig Nussel 	fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL);
238d6e48686SLudwig Nussel 	sb->s_fs_info = fsi;
239d6e48686SLudwig Nussel 	if (!fsi) {
240d6e48686SLudwig Nussel 		err = -ENOMEM;
241d6e48686SLudwig Nussel 		goto fail;
242d6e48686SLudwig Nussel 	}
243d6e48686SLudwig Nussel 
244d6e48686SLudwig Nussel 	err = debugfs_parse_options(data, &fsi->mount_opts);
245d6e48686SLudwig Nussel 	if (err)
246d6e48686SLudwig Nussel 		goto fail;
247d6e48686SLudwig Nussel 
248d6e48686SLudwig Nussel 	err  =  simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
249d6e48686SLudwig Nussel 	if (err)
250d6e48686SLudwig Nussel 		goto fail;
251d6e48686SLudwig Nussel 
252d6e48686SLudwig Nussel 	sb->s_op = &debugfs_super_operations;
25377b3da6eSAl Viro 	sb->s_d_op = &debugfs_dops;
254d6e48686SLudwig Nussel 
255d6e48686SLudwig Nussel 	debugfs_apply_options(sb);
256d6e48686SLudwig Nussel 
257d6e48686SLudwig Nussel 	return 0;
258d6e48686SLudwig Nussel 
259d6e48686SLudwig Nussel fail:
260d6e48686SLudwig Nussel 	kfree(fsi);
261d6e48686SLudwig Nussel 	sb->s_fs_info = NULL;
262d6e48686SLudwig Nussel 	return err;
2631da177e4SLinus Torvalds }
2641da177e4SLinus Torvalds 
265fc14f2feSAl Viro static struct dentry *debug_mount(struct file_system_type *fs_type,
2661da177e4SLinus Torvalds 			int flags, const char *dev_name,
267fc14f2feSAl Viro 			void *data)
2681da177e4SLinus Torvalds {
269fc14f2feSAl Viro 	return mount_single(fs_type, flags, data, debug_fill_super);
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds static struct file_system_type debug_fs_type = {
2731da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
2741da177e4SLinus Torvalds 	.name =		"debugfs",
275fc14f2feSAl Viro 	.mount =	debug_mount,
2761da177e4SLinus Torvalds 	.kill_sb =	kill_litter_super,
2771da177e4SLinus Torvalds };
2787f78e035SEric W. Biederman MODULE_ALIAS_FS("debugfs");
2791da177e4SLinus Torvalds 
280a7c5437bSOmar Sandoval /**
281a7c5437bSOmar Sandoval  * debugfs_lookup() - look up an existing debugfs file
282a7c5437bSOmar Sandoval  * @name: a pointer to a string containing the name of the file to look up.
283a7c5437bSOmar Sandoval  * @parent: a pointer to the parent dentry of the file.
284a7c5437bSOmar Sandoval  *
285a7c5437bSOmar Sandoval  * This function will return a pointer to a dentry if it succeeds.  If the file
286a7c5437bSOmar Sandoval  * doesn't exist or an error occurs, %NULL will be returned.  The returned
287a7c5437bSOmar Sandoval  * dentry must be passed to dput() when it is no longer needed.
288a7c5437bSOmar Sandoval  *
289a7c5437bSOmar Sandoval  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
290a7c5437bSOmar Sandoval  * returned.
291a7c5437bSOmar Sandoval  */
292a7c5437bSOmar Sandoval struct dentry *debugfs_lookup(const char *name, struct dentry *parent)
293a7c5437bSOmar Sandoval {
294a7c5437bSOmar Sandoval 	struct dentry *dentry;
295a7c5437bSOmar Sandoval 
296a7c5437bSOmar Sandoval 	if (IS_ERR(parent))
297a7c5437bSOmar Sandoval 		return NULL;
298a7c5437bSOmar Sandoval 
299a7c5437bSOmar Sandoval 	if (!parent)
300a7c5437bSOmar Sandoval 		parent = debugfs_mount->mnt_root;
301a7c5437bSOmar Sandoval 
3026c2d4798SAl Viro 	dentry = lookup_positive_unlocked(name, parent, strlen(name));
303a7c5437bSOmar Sandoval 	if (IS_ERR(dentry))
304a7c5437bSOmar Sandoval 		return NULL;
305a7c5437bSOmar Sandoval 	return dentry;
306a7c5437bSOmar Sandoval }
307a7c5437bSOmar Sandoval EXPORT_SYMBOL_GPL(debugfs_lookup);
308a7c5437bSOmar Sandoval 
309190afd81SAl Viro static struct dentry *start_creating(const char *name, struct dentry *parent)
310c3b1a350SAl Viro {
311190afd81SAl Viro 	struct dentry *dentry;
312c3b1a350SAl Viro 	int error;
313c3b1a350SAl Viro 
314d03ae477SGreg Kroah-Hartman 	pr_debug("creating file '%s'\n", name);
315c3b1a350SAl Viro 
316c9e15f25SGreg KH 	if (IS_ERR(parent))
317c9e15f25SGreg KH 		return parent;
318c9e15f25SGreg KH 
319c3b1a350SAl Viro 	error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
320c3b1a350SAl Viro 			      &debugfs_mount_count);
32143e23b6cSGreg Kroah-Hartman 	if (error) {
32243e23b6cSGreg Kroah-Hartman 		pr_err("Unable to pin filesystem for file '%s'\n", name);
323190afd81SAl Viro 		return ERR_PTR(error);
32443e23b6cSGreg Kroah-Hartman 	}
325c3b1a350SAl Viro 
326cfa57c11SAl Viro 	/* If the parent is not specified, we create it in the root.
327cfa57c11SAl Viro 	 * We need the root dentry to do this, which is in the super
328cfa57c11SAl Viro 	 * block. A pointer to that is in the struct vfsmount that we
329cfa57c11SAl Viro 	 * have around.
330cfa57c11SAl Viro 	 */
331cfa57c11SAl Viro 	if (!parent)
332cfa57c11SAl Viro 		parent = debugfs_mount->mnt_root;
333cfa57c11SAl Viro 
3345955102cSAl Viro 	inode_lock(d_inode(parent));
335a3d1e7ebSAl Viro 	if (unlikely(IS_DEADDIR(d_inode(parent))))
336a3d1e7ebSAl Viro 		dentry = ERR_PTR(-ENOENT);
337a3d1e7ebSAl Viro 	else
338cfa57c11SAl Viro 		dentry = lookup_one_len(name, parent, strlen(name));
3392b0143b5SDavid Howells 	if (!IS_ERR(dentry) && d_really_is_positive(dentry)) {
340c33d4423SGreg Kroah-Hartman 		if (d_is_dir(dentry))
341c33d4423SGreg Kroah-Hartman 			pr_err("Directory '%s' with parent '%s' already present!\n",
342c33d4423SGreg Kroah-Hartman 			       name, parent->d_name.name);
343c33d4423SGreg Kroah-Hartman 		else
344c33d4423SGreg Kroah-Hartman 			pr_err("File '%s' in directory '%s' already present!\n",
345c33d4423SGreg Kroah-Hartman 			       name, parent->d_name.name);
346190afd81SAl Viro 		dput(dentry);
347190afd81SAl Viro 		dentry = ERR_PTR(-EEXIST);
348190afd81SAl Viro 	}
3490ee9608cSDaniel Borkmann 
3500ee9608cSDaniel Borkmann 	if (IS_ERR(dentry)) {
3515955102cSAl Viro 		inode_unlock(d_inode(parent));
3520ee9608cSDaniel Borkmann 		simple_release_fs(&debugfs_mount, &debugfs_mount_count);
3530ee9608cSDaniel Borkmann 	}
3540ee9608cSDaniel Borkmann 
355190afd81SAl Viro 	return dentry;
356190afd81SAl Viro }
357190afd81SAl Viro 
3585233e311SAl Viro static struct dentry *failed_creating(struct dentry *dentry)
359190afd81SAl Viro {
3605955102cSAl Viro 	inode_unlock(d_inode(dentry->d_parent));
361190afd81SAl Viro 	dput(dentry);
362190afd81SAl Viro 	simple_release_fs(&debugfs_mount, &debugfs_mount_count);
363ff9fb72bSGreg Kroah-Hartman 	return ERR_PTR(-ENOMEM);
364190afd81SAl Viro }
3655233e311SAl Viro 
3665233e311SAl Viro static struct dentry *end_creating(struct dentry *dentry)
3675233e311SAl Viro {
3685955102cSAl Viro 	inode_unlock(d_inode(dentry->d_parent));
369190afd81SAl Viro 	return dentry;
370190afd81SAl Viro }
371190afd81SAl Viro 
37249d200deSNicolai Stange static struct dentry *__debugfs_create_file(const char *name, umode_t mode,
37349d200deSNicolai Stange 				struct dentry *parent, void *data,
37449d200deSNicolai Stange 				const struct file_operations *proxy_fops,
37549d200deSNicolai Stange 				const struct file_operations *real_fops)
37649d200deSNicolai Stange {
37749d200deSNicolai Stange 	struct dentry *dentry;
37849d200deSNicolai Stange 	struct inode *inode;
37949d200deSNicolai Stange 
38049d200deSNicolai Stange 	if (!(mode & S_IFMT))
38149d200deSNicolai Stange 		mode |= S_IFREG;
38249d200deSNicolai Stange 	BUG_ON(!S_ISREG(mode));
38349d200deSNicolai Stange 	dentry = start_creating(name, parent);
38449d200deSNicolai Stange 
3857d39bc50SNicolai Stange 	if (IS_ERR(dentry))
386ff9fb72bSGreg Kroah-Hartman 		return dentry;
38749d200deSNicolai Stange 
38849d200deSNicolai Stange 	inode = debugfs_get_inode(dentry->d_sb);
38943e23b6cSGreg Kroah-Hartman 	if (unlikely(!inode)) {
39043e23b6cSGreg Kroah-Hartman 		pr_err("out of free dentries, can not create file '%s'\n",
39143e23b6cSGreg Kroah-Hartman 		       name);
39249d200deSNicolai Stange 		return failed_creating(dentry);
39343e23b6cSGreg Kroah-Hartman 	}
39449d200deSNicolai Stange 
39549d200deSNicolai Stange 	inode->i_mode = mode;
39649d200deSNicolai Stange 	inode->i_private = data;
39749d200deSNicolai Stange 
3985496197fSDavid Howells 	inode->i_op = &debugfs_file_inode_operations;
39949d200deSNicolai Stange 	inode->i_fop = proxy_fops;
4007d39bc50SNicolai Stange 	dentry->d_fsdata = (void *)((unsigned long)real_fops |
4017d39bc50SNicolai Stange 				DEBUGFS_FSDATA_IS_REAL_FOPS_BIT);
40249d200deSNicolai Stange 
40349d200deSNicolai Stange 	d_instantiate(dentry, inode);
40449d200deSNicolai Stange 	fsnotify_create(d_inode(dentry->d_parent), dentry);
40549d200deSNicolai Stange 	return end_creating(dentry);
40649d200deSNicolai Stange }
40749d200deSNicolai Stange 
4081da177e4SLinus Torvalds /**
4091da177e4SLinus Torvalds  * debugfs_create_file - create a file in the debugfs filesystem
4101da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
411be030e65SAlberto Bertogli  * @mode: the permission that the file should have.
4121da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
413e227867fSMasanari Iida  *          directory dentry if set.  If this parameter is NULL, then the
4141da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
4151da177e4SLinus Torvalds  * @data: a pointer to something that the caller will want to get to later
4168e18e294STheodore Ts'o  *        on.  The inode.i_private pointer will point to this value on
4171da177e4SLinus Torvalds  *        the open() call.
4181da177e4SLinus Torvalds  * @fops: a pointer to a struct file_operations that should be used for
4191da177e4SLinus Torvalds  *        this file.
4201da177e4SLinus Torvalds  *
4211da177e4SLinus Torvalds  * This is the basic "create a file" function for debugfs.  It allows for a
422be030e65SAlberto Bertogli  * wide range of flexibility in creating a file, or a directory (if you want
423be030e65SAlberto Bertogli  * to create a directory, the debugfs_create_dir() function is
4241da177e4SLinus Torvalds  * recommended to be used instead.)
4251da177e4SLinus Torvalds  *
4261da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
4271da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
4281da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
429adc92dd4SDaniel W. S. Almeida  * you are responsible here.)  If an error occurs, ERR_PTR(-ERROR) will be
430ff9fb72bSGreg Kroah-Hartman  * returned.
4311da177e4SLinus Torvalds  *
4326468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
433873760fbSCornelia Huck  * returned.
4341da177e4SLinus Torvalds  */
435f4ae40a6SAl Viro struct dentry *debugfs_create_file(const char *name, umode_t mode,
4361da177e4SLinus Torvalds 				   struct dentry *parent, void *data,
43799ac48f5SArjan van de Ven 				   const struct file_operations *fops)
4381da177e4SLinus Torvalds {
439c3b1a350SAl Viro 
44049d200deSNicolai Stange 	return __debugfs_create_file(name, mode, parent, data,
44149d200deSNicolai Stange 				fops ? &debugfs_full_proxy_file_operations :
44249d200deSNicolai Stange 					&debugfs_noop_file_operations,
44349d200deSNicolai Stange 				fops);
4441da177e4SLinus Torvalds }
4451da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_file);
4461da177e4SLinus Torvalds 
447c6468808SNicolai Stange /**
448c6468808SNicolai Stange  * debugfs_create_file_unsafe - create a file in the debugfs filesystem
449c6468808SNicolai Stange  * @name: a pointer to a string containing the name of the file to create.
450c6468808SNicolai Stange  * @mode: the permission that the file should have.
451c6468808SNicolai Stange  * @parent: a pointer to the parent dentry for this file.  This should be a
452c6468808SNicolai Stange  *          directory dentry if set.  If this parameter is NULL, then the
453c6468808SNicolai Stange  *          file will be created in the root of the debugfs filesystem.
454c6468808SNicolai Stange  * @data: a pointer to something that the caller will want to get to later
455c6468808SNicolai Stange  *        on.  The inode.i_private pointer will point to this value on
456c6468808SNicolai Stange  *        the open() call.
457c6468808SNicolai Stange  * @fops: a pointer to a struct file_operations that should be used for
458c6468808SNicolai Stange  *        this file.
459c6468808SNicolai Stange  *
460c6468808SNicolai Stange  * debugfs_create_file_unsafe() is completely analogous to
461c6468808SNicolai Stange  * debugfs_create_file(), the only difference being that the fops
462c6468808SNicolai Stange  * handed it will not get protected against file removals by the
463c6468808SNicolai Stange  * debugfs core.
464c6468808SNicolai Stange  *
465c6468808SNicolai Stange  * It is your responsibility to protect your struct file_operation
4660eeb2731SSergey Senozhatsky  * methods against file removals by means of debugfs_file_get()
4670eeb2731SSergey Senozhatsky  * and debugfs_file_put(). ->open() is still protected by
468c6468808SNicolai Stange  * debugfs though.
469c6468808SNicolai Stange  *
470c6468808SNicolai Stange  * Any struct file_operations defined by means of
471c6468808SNicolai Stange  * DEFINE_DEBUGFS_ATTRIBUTE() is protected against file removals and
472c6468808SNicolai Stange  * thus, may be used here.
473c6468808SNicolai Stange  */
47449d200deSNicolai Stange struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
47549d200deSNicolai Stange 				   struct dentry *parent, void *data,
47649d200deSNicolai Stange 				   const struct file_operations *fops)
47749d200deSNicolai Stange {
47849d200deSNicolai Stange 
47949d200deSNicolai Stange 	return __debugfs_create_file(name, mode, parent, data,
48049d200deSNicolai Stange 				fops ? &debugfs_open_proxy_file_operations :
48149d200deSNicolai Stange 					&debugfs_noop_file_operations,
48249d200deSNicolai Stange 				fops);
48349d200deSNicolai Stange }
484c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_create_file_unsafe);
48549d200deSNicolai Stange 
4861da177e4SLinus Torvalds /**
487e59b4e91SDavid Howells  * debugfs_create_file_size - create a file in the debugfs filesystem
488e59b4e91SDavid Howells  * @name: a pointer to a string containing the name of the file to create.
489e59b4e91SDavid Howells  * @mode: the permission that the file should have.
490e59b4e91SDavid Howells  * @parent: a pointer to the parent dentry for this file.  This should be a
491e59b4e91SDavid Howells  *          directory dentry if set.  If this parameter is NULL, then the
492e59b4e91SDavid Howells  *          file will be created in the root of the debugfs filesystem.
493e59b4e91SDavid Howells  * @data: a pointer to something that the caller will want to get to later
494e59b4e91SDavid Howells  *        on.  The inode.i_private pointer will point to this value on
495e59b4e91SDavid Howells  *        the open() call.
496e59b4e91SDavid Howells  * @fops: a pointer to a struct file_operations that should be used for
497e59b4e91SDavid Howells  *        this file.
498e59b4e91SDavid Howells  * @file_size: initial file size
499e59b4e91SDavid Howells  *
500e59b4e91SDavid Howells  * This is the basic "create a file" function for debugfs.  It allows for a
501e59b4e91SDavid Howells  * wide range of flexibility in creating a file, or a directory (if you want
502e59b4e91SDavid Howells  * to create a directory, the debugfs_create_dir() function is
503e59b4e91SDavid Howells  * recommended to be used instead.)
504e59b4e91SDavid Howells  */
505*526ee72dSGreg Kroah-Hartman void debugfs_create_file_size(const char *name, umode_t mode,
506e59b4e91SDavid Howells 			      struct dentry *parent, void *data,
507e59b4e91SDavid Howells 			      const struct file_operations *fops,
508e59b4e91SDavid Howells 			      loff_t file_size)
509e59b4e91SDavid Howells {
510e59b4e91SDavid Howells 	struct dentry *de = debugfs_create_file(name, mode, parent, data, fops);
511e59b4e91SDavid Howells 
512e59b4e91SDavid Howells 	if (de)
5132b0143b5SDavid Howells 		d_inode(de)->i_size = file_size;
514e59b4e91SDavid Howells }
515e59b4e91SDavid Howells EXPORT_SYMBOL_GPL(debugfs_create_file_size);
516e59b4e91SDavid Howells 
517e59b4e91SDavid Howells /**
5181da177e4SLinus Torvalds  * debugfs_create_dir - create a directory in the debugfs filesystem
5191da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the directory to
5201da177e4SLinus Torvalds  *        create.
5211da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
522e227867fSMasanari Iida  *          directory dentry if set.  If this parameter is NULL, then the
5231da177e4SLinus Torvalds  *          directory will be created in the root of the debugfs filesystem.
5241da177e4SLinus Torvalds  *
5251da177e4SLinus Torvalds  * This function creates a directory in debugfs with the given name.
5261da177e4SLinus Torvalds  *
5271da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
5281da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
5291da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
530adc92dd4SDaniel W. S. Almeida  * you are responsible here.)  If an error occurs, ERR_PTR(-ERROR) will be
531ff9fb72bSGreg Kroah-Hartman  * returned.
5321da177e4SLinus Torvalds  *
5336468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
534873760fbSCornelia Huck  * returned.
5351da177e4SLinus Torvalds  */
5361da177e4SLinus Torvalds struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
5371da177e4SLinus Torvalds {
538ad5abd5bSAl Viro 	struct dentry *dentry = start_creating(name, parent);
539680b3024SAl Viro 	struct inode *inode;
540ad5abd5bSAl Viro 
541ad5abd5bSAl Viro 	if (IS_ERR(dentry))
542ff9fb72bSGreg Kroah-Hartman 		return dentry;
543ad5abd5bSAl Viro 
544edac65eaSAl Viro 	inode = debugfs_get_inode(dentry->d_sb);
54543e23b6cSGreg Kroah-Hartman 	if (unlikely(!inode)) {
54643e23b6cSGreg Kroah-Hartman 		pr_err("out of free dentries, can not create directory '%s'\n",
54743e23b6cSGreg Kroah-Hartman 		       name);
5485233e311SAl Viro 		return failed_creating(dentry);
54943e23b6cSGreg Kroah-Hartman 	}
550680b3024SAl Viro 
551f5b7769eSLinus Torvalds 	inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
5525496197fSDavid Howells 	inode->i_op = &debugfs_dir_inode_operations;
553edac65eaSAl Viro 	inode->i_fop = &simple_dir_operations;
554edac65eaSAl Viro 
555edac65eaSAl Viro 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
556edac65eaSAl Viro 	inc_nlink(inode);
557680b3024SAl Viro 	d_instantiate(dentry, inode);
5582b0143b5SDavid Howells 	inc_nlink(d_inode(dentry->d_parent));
5592b0143b5SDavid Howells 	fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
5605233e311SAl Viro 	return end_creating(dentry);
5611da177e4SLinus Torvalds }
5621da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_dir);
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds /**
56577b3da6eSAl Viro  * debugfs_create_automount - create automount point in the debugfs filesystem
56677b3da6eSAl Viro  * @name: a pointer to a string containing the name of the file to create.
56777b3da6eSAl Viro  * @parent: a pointer to the parent dentry for this file.  This should be a
56877b3da6eSAl Viro  *          directory dentry if set.  If this parameter is NULL, then the
56977b3da6eSAl Viro  *          file will be created in the root of the debugfs filesystem.
57077b3da6eSAl Viro  * @f: function to be called when pathname resolution steps on that one.
57177b3da6eSAl Viro  * @data: opaque argument to pass to f().
57277b3da6eSAl Viro  *
57377b3da6eSAl Viro  * @f should return what ->d_automount() would.
57477b3da6eSAl Viro  */
57577b3da6eSAl Viro struct dentry *debugfs_create_automount(const char *name,
57677b3da6eSAl Viro 					struct dentry *parent,
57793faccbbSEric W. Biederman 					debugfs_automount_t f,
57877b3da6eSAl Viro 					void *data)
57977b3da6eSAl Viro {
58077b3da6eSAl Viro 	struct dentry *dentry = start_creating(name, parent);
58177b3da6eSAl Viro 	struct inode *inode;
58277b3da6eSAl Viro 
58377b3da6eSAl Viro 	if (IS_ERR(dentry))
584ff9fb72bSGreg Kroah-Hartman 		return dentry;
58577b3da6eSAl Viro 
58677b3da6eSAl Viro 	inode = debugfs_get_inode(dentry->d_sb);
58743e23b6cSGreg Kroah-Hartman 	if (unlikely(!inode)) {
58843e23b6cSGreg Kroah-Hartman 		pr_err("out of free dentries, can not create automount '%s'\n",
58943e23b6cSGreg Kroah-Hartman 		       name);
59077b3da6eSAl Viro 		return failed_creating(dentry);
59143e23b6cSGreg Kroah-Hartman 	}
59277b3da6eSAl Viro 
59387243debSSeth Forshee 	make_empty_dir_inode(inode);
59477b3da6eSAl Viro 	inode->i_flags |= S_AUTOMOUNT;
59577b3da6eSAl Viro 	inode->i_private = data;
59677b3da6eSAl Viro 	dentry->d_fsdata = (void *)f;
597a8f324a4SRoman Pen 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
598a8f324a4SRoman Pen 	inc_nlink(inode);
59977b3da6eSAl Viro 	d_instantiate(dentry, inode);
600a8f324a4SRoman Pen 	inc_nlink(d_inode(dentry->d_parent));
601a8f324a4SRoman Pen 	fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
60277b3da6eSAl Viro 	return end_creating(dentry);
60377b3da6eSAl Viro }
60477b3da6eSAl Viro EXPORT_SYMBOL(debugfs_create_automount);
60577b3da6eSAl Viro 
60677b3da6eSAl Viro /**
60766f54963SPeter Oberparleiter  * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
60866f54963SPeter Oberparleiter  * @name: a pointer to a string containing the name of the symbolic link to
60966f54963SPeter Oberparleiter  *        create.
61066f54963SPeter Oberparleiter  * @parent: a pointer to the parent dentry for this symbolic link.  This
611e227867fSMasanari Iida  *          should be a directory dentry if set.  If this parameter is NULL,
61266f54963SPeter Oberparleiter  *          then the symbolic link will be created in the root of the debugfs
61366f54963SPeter Oberparleiter  *          filesystem.
61466f54963SPeter Oberparleiter  * @target: a pointer to a string containing the path to the target of the
61566f54963SPeter Oberparleiter  *          symbolic link.
61666f54963SPeter Oberparleiter  *
61766f54963SPeter Oberparleiter  * This function creates a symbolic link with the given name in debugfs that
61866f54963SPeter Oberparleiter  * links to the given target path.
61966f54963SPeter Oberparleiter  *
62066f54963SPeter Oberparleiter  * This function will return a pointer to a dentry if it succeeds.  This
62166f54963SPeter Oberparleiter  * pointer must be passed to the debugfs_remove() function when the symbolic
62266f54963SPeter Oberparleiter  * link is to be removed (no automatic cleanup happens if your module is
623adc92dd4SDaniel W. S. Almeida  * unloaded, you are responsible here.)  If an error occurs, ERR_PTR(-ERROR)
624ff9fb72bSGreg Kroah-Hartman  * will be returned.
62566f54963SPeter Oberparleiter  *
62666f54963SPeter Oberparleiter  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
627873760fbSCornelia Huck  * returned.
62866f54963SPeter Oberparleiter  */
62966f54963SPeter Oberparleiter struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
63066f54963SPeter Oberparleiter 				      const char *target)
63166f54963SPeter Oberparleiter {
632ad5abd5bSAl Viro 	struct dentry *dentry;
633680b3024SAl Viro 	struct inode *inode;
634680b3024SAl Viro 	char *link = kstrdup(target, GFP_KERNEL);
63566f54963SPeter Oberparleiter 	if (!link)
636ff9fb72bSGreg Kroah-Hartman 		return ERR_PTR(-ENOMEM);
63766f54963SPeter Oberparleiter 
638ad5abd5bSAl Viro 	dentry = start_creating(name, parent);
639ad5abd5bSAl Viro 	if (IS_ERR(dentry)) {
64066f54963SPeter Oberparleiter 		kfree(link);
641ff9fb72bSGreg Kroah-Hartman 		return dentry;
642ad5abd5bSAl Viro 	}
643ad5abd5bSAl Viro 
644edac65eaSAl Viro 	inode = debugfs_get_inode(dentry->d_sb);
645680b3024SAl Viro 	if (unlikely(!inode)) {
64643e23b6cSGreg Kroah-Hartman 		pr_err("out of free dentries, can not create symlink '%s'\n",
64743e23b6cSGreg Kroah-Hartman 		       name);
648ad5abd5bSAl Viro 		kfree(link);
6495233e311SAl Viro 		return failed_creating(dentry);
650680b3024SAl Viro 	}
651edac65eaSAl Viro 	inode->i_mode = S_IFLNK | S_IRWXUGO;
6525496197fSDavid Howells 	inode->i_op = &debugfs_symlink_inode_operations;
6535723cb01SAl Viro 	inode->i_link = link;
654680b3024SAl Viro 	d_instantiate(dentry, inode);
6555233e311SAl Viro 	return end_creating(dentry);
65666f54963SPeter Oberparleiter }
65766f54963SPeter Oberparleiter EXPORT_SYMBOL_GPL(debugfs_create_symlink);
65866f54963SPeter Oberparleiter 
659823e545cSAmir Goldstein static void __debugfs_file_removed(struct dentry *dentry)
660e9117a5aSNicolai Stange {
661e9117a5aSNicolai Stange 	struct debugfs_fsdata *fsd;
662e9117a5aSNicolai Stange 
6637d39bc50SNicolai Stange 	/*
6647d39bc50SNicolai Stange 	 * Paired with the closing smp_mb() implied by a successful
6657d39bc50SNicolai Stange 	 * cmpxchg() in debugfs_file_get(): either
6667d39bc50SNicolai Stange 	 * debugfs_file_get() must see a dead dentry or we must see a
6677d39bc50SNicolai Stange 	 * debugfs_fsdata instance at ->d_fsdata here (or both).
6687d39bc50SNicolai Stange 	 */
6697d39bc50SNicolai Stange 	smp_mb();
6707d39bc50SNicolai Stange 	fsd = READ_ONCE(dentry->d_fsdata);
6717d39bc50SNicolai Stange 	if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)
6727d39bc50SNicolai Stange 		return;
673e9117a5aSNicolai Stange 	if (!refcount_dec_and_test(&fsd->active_users))
674e9117a5aSNicolai Stange 		wait_for_completion(&fsd->active_users_drained);
675e9117a5aSNicolai Stange }
676e9117a5aSNicolai Stange 
677a3d1e7ebSAl Viro static void remove_one(struct dentry *victim)
6781da177e4SLinus Torvalds {
679a3d1e7ebSAl Viro         if (d_is_reg(victim))
680a3d1e7ebSAl Viro 		__debugfs_file_removed(victim);
6811da177e4SLinus Torvalds 	simple_release_fs(&debugfs_mount, &debugfs_mount_count);
6821da177e4SLinus Torvalds }
6831da177e4SLinus Torvalds 
684cfc94cdfSJan Kara /**
685a3d1e7ebSAl Viro  * debugfs_remove - recursively removes a directory
686398dc4adSUlf Magnusson  * @dentry: a pointer to a the dentry of the directory to be removed.  If this
687398dc4adSUlf Magnusson  *          parameter is NULL or an error value, nothing will be done.
6889505e637SHaavard Skinnemoen  *
6899505e637SHaavard Skinnemoen  * This function recursively removes a directory tree in debugfs that
6909505e637SHaavard Skinnemoen  * was previously created with a call to another debugfs function
6919505e637SHaavard Skinnemoen  * (like debugfs_create_file() or variants thereof.)
6929505e637SHaavard Skinnemoen  *
6939505e637SHaavard Skinnemoen  * This function is required to be called in order for the file to be
6949505e637SHaavard Skinnemoen  * removed, no automatic cleanup of files will happen when a module is
6959505e637SHaavard Skinnemoen  * removed, you are responsible here.
6969505e637SHaavard Skinnemoen  */
697a3d1e7ebSAl Viro void debugfs_remove(struct dentry *dentry)
6989505e637SHaavard Skinnemoen {
699a59d6293SArend van Spriel 	if (IS_ERR_OR_NULL(dentry))
7009505e637SHaavard Skinnemoen 		return;
7019505e637SHaavard Skinnemoen 
702a3d1e7ebSAl Viro 	simple_pin_fs(&debug_fs_type, &debugfs_mount, &debugfs_mount_count);
703a3d1e7ebSAl Viro 	simple_recursive_removal(dentry, remove_one);
704776164c1SOleg Nesterov 	simple_release_fs(&debugfs_mount, &debugfs_mount_count);
705776164c1SOleg Nesterov }
706a3d1e7ebSAl Viro EXPORT_SYMBOL_GPL(debugfs_remove);
7079505e637SHaavard Skinnemoen 
7089505e637SHaavard Skinnemoen /**
709cfc94cdfSJan Kara  * debugfs_rename - rename a file/directory in the debugfs filesystem
710cfc94cdfSJan Kara  * @old_dir: a pointer to the parent dentry for the renamed object. This
711cfc94cdfSJan Kara  *          should be a directory dentry.
712cfc94cdfSJan Kara  * @old_dentry: dentry of an object to be renamed.
713cfc94cdfSJan Kara  * @new_dir: a pointer to the parent dentry where the object should be
714cfc94cdfSJan Kara  *          moved. This should be a directory dentry.
715cfc94cdfSJan Kara  * @new_name: a pointer to a string containing the target name.
716cfc94cdfSJan Kara  *
717cfc94cdfSJan Kara  * This function renames a file/directory in debugfs.  The target must not
718cfc94cdfSJan Kara  * exist for rename to succeed.
719cfc94cdfSJan Kara  *
720cfc94cdfSJan Kara  * This function will return a pointer to old_dentry (which is updated to
721cfc94cdfSJan Kara  * reflect renaming) if it succeeds. If an error occurs, %NULL will be
722cfc94cdfSJan Kara  * returned.
723cfc94cdfSJan Kara  *
724cfc94cdfSJan Kara  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
725cfc94cdfSJan Kara  * returned.
726cfc94cdfSJan Kara  */
727cfc94cdfSJan Kara struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
728cfc94cdfSJan Kara 		struct dentry *new_dir, const char *new_name)
729cfc94cdfSJan Kara {
730cfc94cdfSJan Kara 	int error;
731cfc94cdfSJan Kara 	struct dentry *dentry = NULL, *trap;
73249d31c2fSAl Viro 	struct name_snapshot old_name;
733cfc94cdfSJan Kara 
734d88c93f0SGreg Kroah-Hartman 	if (IS_ERR(old_dir))
735d88c93f0SGreg Kroah-Hartman 		return old_dir;
736d88c93f0SGreg Kroah-Hartman 	if (IS_ERR(new_dir))
737d88c93f0SGreg Kroah-Hartman 		return new_dir;
738d88c93f0SGreg Kroah-Hartman 	if (IS_ERR_OR_NULL(old_dentry))
739d88c93f0SGreg Kroah-Hartman 		return old_dentry;
740d88c93f0SGreg Kroah-Hartman 
741cfc94cdfSJan Kara 	trap = lock_rename(new_dir, old_dir);
742cfc94cdfSJan Kara 	/* Source or destination directories don't exist? */
7432b0143b5SDavid Howells 	if (d_really_is_negative(old_dir) || d_really_is_negative(new_dir))
744cfc94cdfSJan Kara 		goto exit;
745cfc94cdfSJan Kara 	/* Source does not exist, cyclic rename, or mountpoint? */
7462b0143b5SDavid Howells 	if (d_really_is_negative(old_dentry) || old_dentry == trap ||
747cfc94cdfSJan Kara 	    d_mountpoint(old_dentry))
748cfc94cdfSJan Kara 		goto exit;
749cfc94cdfSJan Kara 	dentry = lookup_one_len(new_name, new_dir, strlen(new_name));
750cfc94cdfSJan Kara 	/* Lookup failed, cyclic rename or target exists? */
7512b0143b5SDavid Howells 	if (IS_ERR(dentry) || dentry == trap || d_really_is_positive(dentry))
752cfc94cdfSJan Kara 		goto exit;
753cfc94cdfSJan Kara 
75449d31c2fSAl Viro 	take_dentry_name_snapshot(&old_name, old_dentry);
755cfc94cdfSJan Kara 
7562b0143b5SDavid Howells 	error = simple_rename(d_inode(old_dir), old_dentry, d_inode(new_dir),
757e0e0be8aSMiklos Szeredi 			      dentry, 0);
758cfc94cdfSJan Kara 	if (error) {
75949d31c2fSAl Viro 		release_dentry_name_snapshot(&old_name);
760cfc94cdfSJan Kara 		goto exit;
761cfc94cdfSJan Kara 	}
762cfc94cdfSJan Kara 	d_move(old_dentry, dentry);
763f4ec3a3dSAl Viro 	fsnotify_move(d_inode(old_dir), d_inode(new_dir), &old_name.name,
764e36cb0b8SDavid Howells 		d_is_dir(old_dentry),
7655a190ae6SAl Viro 		NULL, old_dentry);
76649d31c2fSAl Viro 	release_dentry_name_snapshot(&old_name);
767cfc94cdfSJan Kara 	unlock_rename(new_dir, old_dir);
768cfc94cdfSJan Kara 	dput(dentry);
769cfc94cdfSJan Kara 	return old_dentry;
770cfc94cdfSJan Kara exit:
771cfc94cdfSJan Kara 	if (dentry && !IS_ERR(dentry))
772cfc94cdfSJan Kara 		dput(dentry);
773cfc94cdfSJan Kara 	unlock_rename(new_dir, old_dir);
774ff9fb72bSGreg Kroah-Hartman 	if (IS_ERR(dentry))
775ff9fb72bSGreg Kroah-Hartman 		return dentry;
776ff9fb72bSGreg Kroah-Hartman 	return ERR_PTR(-EINVAL);
777cfc94cdfSJan Kara }
778cfc94cdfSJan Kara EXPORT_SYMBOL_GPL(debugfs_rename);
779cfc94cdfSJan Kara 
780c0f92ba9SFrederic Weisbecker /**
781c0f92ba9SFrederic Weisbecker  * debugfs_initialized - Tells whether debugfs has been registered
782c0f92ba9SFrederic Weisbecker  */
783c0f92ba9SFrederic Weisbecker bool debugfs_initialized(void)
784c0f92ba9SFrederic Weisbecker {
785c0f92ba9SFrederic Weisbecker 	return debugfs_registered;
786c0f92ba9SFrederic Weisbecker }
787c0f92ba9SFrederic Weisbecker EXPORT_SYMBOL_GPL(debugfs_initialized);
788c0f92ba9SFrederic Weisbecker 
7891da177e4SLinus Torvalds static int __init debugfs_init(void)
7901da177e4SLinus Torvalds {
7911da177e4SLinus Torvalds 	int retval;
7921da177e4SLinus Torvalds 
793f9bb4882SEric W. Biederman 	retval = sysfs_create_mount_point(kernel_kobj, "debug");
794f9bb4882SEric W. Biederman 	if (retval)
795f9bb4882SEric W. Biederman 		return retval;
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds 	retval = register_filesystem(&debug_fs_type);
7981da177e4SLinus Torvalds 	if (retval)
799f9bb4882SEric W. Biederman 		sysfs_remove_mount_point(kernel_kobj, "debug");
800c0f92ba9SFrederic Weisbecker 	else
801c0f92ba9SFrederic Weisbecker 		debugfs_registered = true;
802c0f92ba9SFrederic Weisbecker 
8031da177e4SLinus Torvalds 	return retval;
8041da177e4SLinus Torvalds }
8051da177e4SLinus Torvalds core_initcall(debugfs_init);
806