xref: /linux-6.15/fs/debugfs/inode.c (revision 93faccbb)
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>
309fd4dcecSNicolai Stange #include <linux/srcu.h>
319fd4dcecSNicolai Stange 
329fd4dcecSNicolai Stange #include "internal.h"
331da177e4SLinus Torvalds 
3482aceae4SKees Cook #define DEBUGFS_DEFAULT_MODE	0700
35d6e48686SLudwig Nussel 
369fd4dcecSNicolai Stange DEFINE_SRCU(debugfs_srcu);
379fd4dcecSNicolai Stange 
381da177e4SLinus Torvalds static struct vfsmount *debugfs_mount;
391da177e4SLinus Torvalds static int debugfs_mount_count;
40c0f92ba9SFrederic Weisbecker static bool debugfs_registered;
411da177e4SLinus Torvalds 
42edac65eaSAl Viro static struct inode *debugfs_get_inode(struct super_block *sb)
431da177e4SLinus Torvalds {
441da177e4SLinus Torvalds 	struct inode *inode = new_inode(sb);
451da177e4SLinus Torvalds 	if (inode) {
4685fe4025SChristoph Hellwig 		inode->i_ino = get_next_ino();
471b48b530SDeepa Dinamani 		inode->i_atime = inode->i_mtime =
48c2050a45SDeepa Dinamani 			inode->i_ctime = current_time(inode);
491da177e4SLinus Torvalds 	}
501da177e4SLinus Torvalds 	return inode;
511da177e4SLinus Torvalds }
521da177e4SLinus Torvalds 
53d6e48686SLudwig Nussel struct debugfs_mount_opts {
547dc05881SEric W. Biederman 	kuid_t uid;
557dc05881SEric W. Biederman 	kgid_t gid;
56d6e48686SLudwig Nussel 	umode_t mode;
57d6e48686SLudwig Nussel };
58d6e48686SLudwig Nussel 
59d6e48686SLudwig Nussel enum {
60d6e48686SLudwig Nussel 	Opt_uid,
61d6e48686SLudwig Nussel 	Opt_gid,
62d6e48686SLudwig Nussel 	Opt_mode,
63d6e48686SLudwig Nussel 	Opt_err
64d6e48686SLudwig Nussel };
65d6e48686SLudwig Nussel 
66d6e48686SLudwig Nussel static const match_table_t tokens = {
67d6e48686SLudwig Nussel 	{Opt_uid, "uid=%u"},
68d6e48686SLudwig Nussel 	{Opt_gid, "gid=%u"},
69d6e48686SLudwig Nussel 	{Opt_mode, "mode=%o"},
70d6e48686SLudwig Nussel 	{Opt_err, NULL}
71d6e48686SLudwig Nussel };
72d6e48686SLudwig Nussel 
73d6e48686SLudwig Nussel struct debugfs_fs_info {
74d6e48686SLudwig Nussel 	struct debugfs_mount_opts mount_opts;
75d6e48686SLudwig Nussel };
76d6e48686SLudwig Nussel 
77d6e48686SLudwig Nussel static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts)
78d6e48686SLudwig Nussel {
79d6e48686SLudwig Nussel 	substring_t args[MAX_OPT_ARGS];
80d6e48686SLudwig Nussel 	int option;
81d6e48686SLudwig Nussel 	int token;
827dc05881SEric W. Biederman 	kuid_t uid;
837dc05881SEric W. Biederman 	kgid_t gid;
84d6e48686SLudwig Nussel 	char *p;
85d6e48686SLudwig Nussel 
86d6e48686SLudwig Nussel 	opts->mode = DEBUGFS_DEFAULT_MODE;
87d6e48686SLudwig Nussel 
88d6e48686SLudwig Nussel 	while ((p = strsep(&data, ",")) != NULL) {
89d6e48686SLudwig Nussel 		if (!*p)
90d6e48686SLudwig Nussel 			continue;
91d6e48686SLudwig Nussel 
92d6e48686SLudwig Nussel 		token = match_token(p, tokens, args);
93d6e48686SLudwig Nussel 		switch (token) {
94d6e48686SLudwig Nussel 		case Opt_uid:
95d6e48686SLudwig Nussel 			if (match_int(&args[0], &option))
96d6e48686SLudwig Nussel 				return -EINVAL;
977dc05881SEric W. Biederman 			uid = make_kuid(current_user_ns(), option);
987dc05881SEric W. Biederman 			if (!uid_valid(uid))
997dc05881SEric W. Biederman 				return -EINVAL;
1007dc05881SEric W. Biederman 			opts->uid = uid;
101d6e48686SLudwig Nussel 			break;
102d6e48686SLudwig Nussel 		case Opt_gid:
103f1688e04SDave Reisner 			if (match_int(&args[0], &option))
104d6e48686SLudwig Nussel 				return -EINVAL;
1057dc05881SEric W. Biederman 			gid = make_kgid(current_user_ns(), option);
1067dc05881SEric W. Biederman 			if (!gid_valid(gid))
1077dc05881SEric W. Biederman 				return -EINVAL;
1087dc05881SEric W. Biederman 			opts->gid = gid;
109d6e48686SLudwig Nussel 			break;
110d6e48686SLudwig Nussel 		case Opt_mode:
111d6e48686SLudwig Nussel 			if (match_octal(&args[0], &option))
112d6e48686SLudwig Nussel 				return -EINVAL;
113d6e48686SLudwig Nussel 			opts->mode = option & S_IALLUGO;
114d6e48686SLudwig Nussel 			break;
115d6e48686SLudwig Nussel 		/*
116d6e48686SLudwig Nussel 		 * We might like to report bad mount options here;
117d6e48686SLudwig Nussel 		 * but traditionally debugfs has ignored all mount options
118d6e48686SLudwig Nussel 		 */
119d6e48686SLudwig Nussel 		}
120d6e48686SLudwig Nussel 	}
121d6e48686SLudwig Nussel 
122d6e48686SLudwig Nussel 	return 0;
123d6e48686SLudwig Nussel }
124d6e48686SLudwig Nussel 
125d6e48686SLudwig Nussel static int debugfs_apply_options(struct super_block *sb)
126d6e48686SLudwig Nussel {
127d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi = sb->s_fs_info;
1282b0143b5SDavid Howells 	struct inode *inode = d_inode(sb->s_root);
129d6e48686SLudwig Nussel 	struct debugfs_mount_opts *opts = &fsi->mount_opts;
130d6e48686SLudwig Nussel 
131d6e48686SLudwig Nussel 	inode->i_mode &= ~S_IALLUGO;
132d6e48686SLudwig Nussel 	inode->i_mode |= opts->mode;
133d6e48686SLudwig Nussel 
134d6e48686SLudwig Nussel 	inode->i_uid = opts->uid;
135d6e48686SLudwig Nussel 	inode->i_gid = opts->gid;
136d6e48686SLudwig Nussel 
137d6e48686SLudwig Nussel 	return 0;
138d6e48686SLudwig Nussel }
139d6e48686SLudwig Nussel 
140d6e48686SLudwig Nussel static int debugfs_remount(struct super_block *sb, int *flags, char *data)
141d6e48686SLudwig Nussel {
142d6e48686SLudwig Nussel 	int err;
143d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi = sb->s_fs_info;
144d6e48686SLudwig Nussel 
14502b9984dSTheodore Ts'o 	sync_filesystem(sb);
146d6e48686SLudwig Nussel 	err = debugfs_parse_options(data, &fsi->mount_opts);
147d6e48686SLudwig Nussel 	if (err)
148d6e48686SLudwig Nussel 		goto fail;
149d6e48686SLudwig Nussel 
150d6e48686SLudwig Nussel 	debugfs_apply_options(sb);
151d6e48686SLudwig Nussel 
152d6e48686SLudwig Nussel fail:
153d6e48686SLudwig Nussel 	return err;
154d6e48686SLudwig Nussel }
155d6e48686SLudwig Nussel 
156d6e48686SLudwig Nussel static int debugfs_show_options(struct seq_file *m, struct dentry *root)
157d6e48686SLudwig Nussel {
158d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi = root->d_sb->s_fs_info;
159d6e48686SLudwig Nussel 	struct debugfs_mount_opts *opts = &fsi->mount_opts;
160d6e48686SLudwig Nussel 
1617dc05881SEric W. Biederman 	if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
1627dc05881SEric W. Biederman 		seq_printf(m, ",uid=%u",
1637dc05881SEric W. Biederman 			   from_kuid_munged(&init_user_ns, opts->uid));
1647dc05881SEric W. Biederman 	if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
1657dc05881SEric W. Biederman 		seq_printf(m, ",gid=%u",
1667dc05881SEric W. Biederman 			   from_kgid_munged(&init_user_ns, opts->gid));
167d6e48686SLudwig Nussel 	if (opts->mode != DEBUGFS_DEFAULT_MODE)
168d6e48686SLudwig Nussel 		seq_printf(m, ",mode=%o", opts->mode);
169d6e48686SLudwig Nussel 
170d6e48686SLudwig Nussel 	return 0;
171d6e48686SLudwig Nussel }
172d6e48686SLudwig Nussel 
1730db59e59SAl Viro static void debugfs_evict_inode(struct inode *inode)
1740db59e59SAl Viro {
1750db59e59SAl Viro 	truncate_inode_pages_final(&inode->i_data);
1760db59e59SAl Viro 	clear_inode(inode);
1770db59e59SAl Viro 	if (S_ISLNK(inode->i_mode))
1785723cb01SAl Viro 		kfree(inode->i_link);
1790db59e59SAl Viro }
1800db59e59SAl Viro 
181d6e48686SLudwig Nussel static const struct super_operations debugfs_super_operations = {
182d6e48686SLudwig Nussel 	.statfs		= simple_statfs,
183d6e48686SLudwig Nussel 	.remount_fs	= debugfs_remount,
184d6e48686SLudwig Nussel 	.show_options	= debugfs_show_options,
1850db59e59SAl Viro 	.evict_inode	= debugfs_evict_inode,
186d6e48686SLudwig Nussel };
187d6e48686SLudwig Nussel 
18877b3da6eSAl Viro static struct vfsmount *debugfs_automount(struct path *path)
18977b3da6eSAl Viro {
190*93faccbbSEric W. Biederman 	debugfs_automount_t f;
191*93faccbbSEric W. Biederman 	f = (debugfs_automount_t)path->dentry->d_fsdata;
192*93faccbbSEric W. Biederman 	return f(path->dentry, d_inode(path->dentry)->i_private);
19377b3da6eSAl Viro }
19477b3da6eSAl Viro 
19577b3da6eSAl Viro static const struct dentry_operations debugfs_dops = {
19677b3da6eSAl Viro 	.d_delete = always_delete_dentry,
19777b3da6eSAl Viro 	.d_automount = debugfs_automount,
19877b3da6eSAl Viro };
19977b3da6eSAl Viro 
2001da177e4SLinus Torvalds static int debug_fill_super(struct super_block *sb, void *data, int silent)
2011da177e4SLinus Torvalds {
2021da177e4SLinus Torvalds 	static struct tree_descr debug_files[] = {{""}};
203d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi;
204d6e48686SLudwig Nussel 	int err;
2051da177e4SLinus Torvalds 
206d6e48686SLudwig Nussel 	save_mount_options(sb, data);
207d6e48686SLudwig Nussel 
208d6e48686SLudwig Nussel 	fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL);
209d6e48686SLudwig Nussel 	sb->s_fs_info = fsi;
210d6e48686SLudwig Nussel 	if (!fsi) {
211d6e48686SLudwig Nussel 		err = -ENOMEM;
212d6e48686SLudwig Nussel 		goto fail;
213d6e48686SLudwig Nussel 	}
214d6e48686SLudwig Nussel 
215d6e48686SLudwig Nussel 	err = debugfs_parse_options(data, &fsi->mount_opts);
216d6e48686SLudwig Nussel 	if (err)
217d6e48686SLudwig Nussel 		goto fail;
218d6e48686SLudwig Nussel 
219d6e48686SLudwig Nussel 	err  =  simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
220d6e48686SLudwig Nussel 	if (err)
221d6e48686SLudwig Nussel 		goto fail;
222d6e48686SLudwig Nussel 
223d6e48686SLudwig Nussel 	sb->s_op = &debugfs_super_operations;
22477b3da6eSAl Viro 	sb->s_d_op = &debugfs_dops;
225d6e48686SLudwig Nussel 
226d6e48686SLudwig Nussel 	debugfs_apply_options(sb);
227d6e48686SLudwig Nussel 
228d6e48686SLudwig Nussel 	return 0;
229d6e48686SLudwig Nussel 
230d6e48686SLudwig Nussel fail:
231d6e48686SLudwig Nussel 	kfree(fsi);
232d6e48686SLudwig Nussel 	sb->s_fs_info = NULL;
233d6e48686SLudwig Nussel 	return err;
2341da177e4SLinus Torvalds }
2351da177e4SLinus Torvalds 
236fc14f2feSAl Viro static struct dentry *debug_mount(struct file_system_type *fs_type,
2371da177e4SLinus Torvalds 			int flags, const char *dev_name,
238fc14f2feSAl Viro 			void *data)
2391da177e4SLinus Torvalds {
240fc14f2feSAl Viro 	return mount_single(fs_type, flags, data, debug_fill_super);
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds static struct file_system_type debug_fs_type = {
2441da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
2451da177e4SLinus Torvalds 	.name =		"debugfs",
246fc14f2feSAl Viro 	.mount =	debug_mount,
2471da177e4SLinus Torvalds 	.kill_sb =	kill_litter_super,
2481da177e4SLinus Torvalds };
2497f78e035SEric W. Biederman MODULE_ALIAS_FS("debugfs");
2501da177e4SLinus Torvalds 
251190afd81SAl Viro static struct dentry *start_creating(const char *name, struct dentry *parent)
252c3b1a350SAl Viro {
253190afd81SAl Viro 	struct dentry *dentry;
254c3b1a350SAl Viro 	int error;
255c3b1a350SAl Viro 
256c3b1a350SAl Viro 	pr_debug("debugfs: creating file '%s'\n",name);
257c3b1a350SAl Viro 
258c9e15f25SGreg KH 	if (IS_ERR(parent))
259c9e15f25SGreg KH 		return parent;
260c9e15f25SGreg KH 
261c3b1a350SAl Viro 	error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
262c3b1a350SAl Viro 			      &debugfs_mount_count);
263c3b1a350SAl Viro 	if (error)
264190afd81SAl Viro 		return ERR_PTR(error);
265c3b1a350SAl Viro 
266cfa57c11SAl Viro 	/* If the parent is not specified, we create it in the root.
267cfa57c11SAl Viro 	 * We need the root dentry to do this, which is in the super
268cfa57c11SAl Viro 	 * block. A pointer to that is in the struct vfsmount that we
269cfa57c11SAl Viro 	 * have around.
270cfa57c11SAl Viro 	 */
271cfa57c11SAl Viro 	if (!parent)
272cfa57c11SAl Viro 		parent = debugfs_mount->mnt_root;
273cfa57c11SAl Viro 
2745955102cSAl Viro 	inode_lock(d_inode(parent));
275cfa57c11SAl Viro 	dentry = lookup_one_len(name, parent, strlen(name));
2762b0143b5SDavid Howells 	if (!IS_ERR(dentry) && d_really_is_positive(dentry)) {
277190afd81SAl Viro 		dput(dentry);
278190afd81SAl Viro 		dentry = ERR_PTR(-EEXIST);
279190afd81SAl Viro 	}
2800ee9608cSDaniel Borkmann 
2810ee9608cSDaniel Borkmann 	if (IS_ERR(dentry)) {
2825955102cSAl Viro 		inode_unlock(d_inode(parent));
2830ee9608cSDaniel Borkmann 		simple_release_fs(&debugfs_mount, &debugfs_mount_count);
2840ee9608cSDaniel Borkmann 	}
2850ee9608cSDaniel Borkmann 
286190afd81SAl Viro 	return dentry;
287190afd81SAl Viro }
288190afd81SAl Viro 
2895233e311SAl Viro static struct dentry *failed_creating(struct dentry *dentry)
290190afd81SAl Viro {
2915955102cSAl Viro 	inode_unlock(d_inode(dentry->d_parent));
292190afd81SAl Viro 	dput(dentry);
293190afd81SAl Viro 	simple_release_fs(&debugfs_mount, &debugfs_mount_count);
2945233e311SAl Viro 	return NULL;
295190afd81SAl Viro }
2965233e311SAl Viro 
2975233e311SAl Viro static struct dentry *end_creating(struct dentry *dentry)
2985233e311SAl Viro {
2995955102cSAl Viro 	inode_unlock(d_inode(dentry->d_parent));
300190afd81SAl Viro 	return dentry;
301190afd81SAl Viro }
302190afd81SAl Viro 
30349d200deSNicolai Stange static struct dentry *__debugfs_create_file(const char *name, umode_t mode,
30449d200deSNicolai Stange 				struct dentry *parent, void *data,
30549d200deSNicolai Stange 				const struct file_operations *proxy_fops,
30649d200deSNicolai Stange 				const struct file_operations *real_fops)
30749d200deSNicolai Stange {
30849d200deSNicolai Stange 	struct dentry *dentry;
30949d200deSNicolai Stange 	struct inode *inode;
31049d200deSNicolai Stange 
31149d200deSNicolai Stange 	if (!(mode & S_IFMT))
31249d200deSNicolai Stange 		mode |= S_IFREG;
31349d200deSNicolai Stange 	BUG_ON(!S_ISREG(mode));
31449d200deSNicolai Stange 	dentry = start_creating(name, parent);
31549d200deSNicolai Stange 
31649d200deSNicolai Stange 	if (IS_ERR(dentry))
31749d200deSNicolai Stange 		return NULL;
31849d200deSNicolai Stange 
31949d200deSNicolai Stange 	inode = debugfs_get_inode(dentry->d_sb);
32049d200deSNicolai Stange 	if (unlikely(!inode))
32149d200deSNicolai Stange 		return failed_creating(dentry);
32249d200deSNicolai Stange 
32349d200deSNicolai Stange 	inode->i_mode = mode;
32449d200deSNicolai Stange 	inode->i_private = data;
32549d200deSNicolai Stange 
32649d200deSNicolai Stange 	inode->i_fop = proxy_fops;
32749d200deSNicolai Stange 	dentry->d_fsdata = (void *)real_fops;
32849d200deSNicolai Stange 
32949d200deSNicolai Stange 	d_instantiate(dentry, inode);
33049d200deSNicolai Stange 	fsnotify_create(d_inode(dentry->d_parent), dentry);
33149d200deSNicolai Stange 	return end_creating(dentry);
33249d200deSNicolai Stange }
33349d200deSNicolai Stange 
3341da177e4SLinus Torvalds /**
3351da177e4SLinus Torvalds  * debugfs_create_file - create a file in the debugfs filesystem
3361da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
337be030e65SAlberto Bertogli  * @mode: the permission that the file should have.
3381da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
339e227867fSMasanari Iida  *          directory dentry if set.  If this parameter is NULL, then the
3401da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
3411da177e4SLinus Torvalds  * @data: a pointer to something that the caller will want to get to later
3428e18e294STheodore Ts'o  *        on.  The inode.i_private pointer will point to this value on
3431da177e4SLinus Torvalds  *        the open() call.
3441da177e4SLinus Torvalds  * @fops: a pointer to a struct file_operations that should be used for
3451da177e4SLinus Torvalds  *        this file.
3461da177e4SLinus Torvalds  *
3471da177e4SLinus Torvalds  * This is the basic "create a file" function for debugfs.  It allows for a
348be030e65SAlberto Bertogli  * wide range of flexibility in creating a file, or a directory (if you want
349be030e65SAlberto Bertogli  * to create a directory, the debugfs_create_dir() function is
3501da177e4SLinus Torvalds  * recommended to be used instead.)
3511da177e4SLinus Torvalds  *
3521da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
3531da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
3541da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
3556468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
3561da177e4SLinus Torvalds  *
3576468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
358873760fbSCornelia Huck  * returned.
3591da177e4SLinus Torvalds  */
360f4ae40a6SAl Viro struct dentry *debugfs_create_file(const char *name, umode_t mode,
3611da177e4SLinus Torvalds 				   struct dentry *parent, void *data,
36299ac48f5SArjan van de Ven 				   const struct file_operations *fops)
3631da177e4SLinus Torvalds {
364c3b1a350SAl Viro 
36549d200deSNicolai Stange 	return __debugfs_create_file(name, mode, parent, data,
36649d200deSNicolai Stange 				fops ? &debugfs_full_proxy_file_operations :
36749d200deSNicolai Stange 					&debugfs_noop_file_operations,
36849d200deSNicolai Stange 				fops);
3691da177e4SLinus Torvalds }
3701da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_file);
3711da177e4SLinus Torvalds 
372c6468808SNicolai Stange /**
373c6468808SNicolai Stange  * debugfs_create_file_unsafe - create a file in the debugfs filesystem
374c6468808SNicolai Stange  * @name: a pointer to a string containing the name of the file to create.
375c6468808SNicolai Stange  * @mode: the permission that the file should have.
376c6468808SNicolai Stange  * @parent: a pointer to the parent dentry for this file.  This should be a
377c6468808SNicolai Stange  *          directory dentry if set.  If this parameter is NULL, then the
378c6468808SNicolai Stange  *          file will be created in the root of the debugfs filesystem.
379c6468808SNicolai Stange  * @data: a pointer to something that the caller will want to get to later
380c6468808SNicolai Stange  *        on.  The inode.i_private pointer will point to this value on
381c6468808SNicolai Stange  *        the open() call.
382c6468808SNicolai Stange  * @fops: a pointer to a struct file_operations that should be used for
383c6468808SNicolai Stange  *        this file.
384c6468808SNicolai Stange  *
385c6468808SNicolai Stange  * debugfs_create_file_unsafe() is completely analogous to
386c6468808SNicolai Stange  * debugfs_create_file(), the only difference being that the fops
387c6468808SNicolai Stange  * handed it will not get protected against file removals by the
388c6468808SNicolai Stange  * debugfs core.
389c6468808SNicolai Stange  *
390c6468808SNicolai Stange  * It is your responsibility to protect your struct file_operation
391c6468808SNicolai Stange  * methods against file removals by means of debugfs_use_file_start()
392c6468808SNicolai Stange  * and debugfs_use_file_finish(). ->open() is still protected by
393c6468808SNicolai Stange  * debugfs though.
394c6468808SNicolai Stange  *
395c6468808SNicolai Stange  * Any struct file_operations defined by means of
396c6468808SNicolai Stange  * DEFINE_DEBUGFS_ATTRIBUTE() is protected against file removals and
397c6468808SNicolai Stange  * thus, may be used here.
398c6468808SNicolai Stange  */
39949d200deSNicolai Stange struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
40049d200deSNicolai Stange 				   struct dentry *parent, void *data,
40149d200deSNicolai Stange 				   const struct file_operations *fops)
40249d200deSNicolai Stange {
40349d200deSNicolai Stange 
40449d200deSNicolai Stange 	return __debugfs_create_file(name, mode, parent, data,
40549d200deSNicolai Stange 				fops ? &debugfs_open_proxy_file_operations :
40649d200deSNicolai Stange 					&debugfs_noop_file_operations,
40749d200deSNicolai Stange 				fops);
40849d200deSNicolai Stange }
409c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_create_file_unsafe);
41049d200deSNicolai Stange 
4111da177e4SLinus Torvalds /**
412e59b4e91SDavid Howells  * debugfs_create_file_size - create a file in the debugfs filesystem
413e59b4e91SDavid Howells  * @name: a pointer to a string containing the name of the file to create.
414e59b4e91SDavid Howells  * @mode: the permission that the file should have.
415e59b4e91SDavid Howells  * @parent: a pointer to the parent dentry for this file.  This should be a
416e59b4e91SDavid Howells  *          directory dentry if set.  If this parameter is NULL, then the
417e59b4e91SDavid Howells  *          file will be created in the root of the debugfs filesystem.
418e59b4e91SDavid Howells  * @data: a pointer to something that the caller will want to get to later
419e59b4e91SDavid Howells  *        on.  The inode.i_private pointer will point to this value on
420e59b4e91SDavid Howells  *        the open() call.
421e59b4e91SDavid Howells  * @fops: a pointer to a struct file_operations that should be used for
422e59b4e91SDavid Howells  *        this file.
423e59b4e91SDavid Howells  * @file_size: initial file size
424e59b4e91SDavid Howells  *
425e59b4e91SDavid Howells  * This is the basic "create a file" function for debugfs.  It allows for a
426e59b4e91SDavid Howells  * wide range of flexibility in creating a file, or a directory (if you want
427e59b4e91SDavid Howells  * to create a directory, the debugfs_create_dir() function is
428e59b4e91SDavid Howells  * recommended to be used instead.)
429e59b4e91SDavid Howells  *
430e59b4e91SDavid Howells  * This function will return a pointer to a dentry if it succeeds.  This
431e59b4e91SDavid Howells  * pointer must be passed to the debugfs_remove() function when the file is
432e59b4e91SDavid Howells  * to be removed (no automatic cleanup happens if your module is unloaded,
433e59b4e91SDavid Howells  * you are responsible here.)  If an error occurs, %NULL will be returned.
434e59b4e91SDavid Howells  *
435e59b4e91SDavid Howells  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
436e59b4e91SDavid Howells  * returned.
437e59b4e91SDavid Howells  */
438e59b4e91SDavid Howells struct dentry *debugfs_create_file_size(const char *name, umode_t mode,
439e59b4e91SDavid Howells 					struct dentry *parent, void *data,
440e59b4e91SDavid Howells 					const struct file_operations *fops,
441e59b4e91SDavid Howells 					loff_t file_size)
442e59b4e91SDavid Howells {
443e59b4e91SDavid Howells 	struct dentry *de = debugfs_create_file(name, mode, parent, data, fops);
444e59b4e91SDavid Howells 
445e59b4e91SDavid Howells 	if (de)
4462b0143b5SDavid Howells 		d_inode(de)->i_size = file_size;
447e59b4e91SDavid Howells 	return de;
448e59b4e91SDavid Howells }
449e59b4e91SDavid Howells EXPORT_SYMBOL_GPL(debugfs_create_file_size);
450e59b4e91SDavid Howells 
451e59b4e91SDavid Howells /**
4521da177e4SLinus Torvalds  * debugfs_create_dir - create a directory in the debugfs filesystem
4531da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the directory to
4541da177e4SLinus Torvalds  *        create.
4551da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
456e227867fSMasanari Iida  *          directory dentry if set.  If this parameter is NULL, then the
4571da177e4SLinus Torvalds  *          directory will be created in the root of the debugfs filesystem.
4581da177e4SLinus Torvalds  *
4591da177e4SLinus Torvalds  * This function creates a directory in debugfs with the given name.
4601da177e4SLinus Torvalds  *
4611da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
4621da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
4631da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
4646468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
4651da177e4SLinus Torvalds  *
4666468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
467873760fbSCornelia Huck  * returned.
4681da177e4SLinus Torvalds  */
4691da177e4SLinus Torvalds struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
4701da177e4SLinus Torvalds {
471ad5abd5bSAl Viro 	struct dentry *dentry = start_creating(name, parent);
472680b3024SAl Viro 	struct inode *inode;
473ad5abd5bSAl Viro 
474ad5abd5bSAl Viro 	if (IS_ERR(dentry))
475ad5abd5bSAl Viro 		return NULL;
476ad5abd5bSAl Viro 
477edac65eaSAl Viro 	inode = debugfs_get_inode(dentry->d_sb);
478680b3024SAl Viro 	if (unlikely(!inode))
4795233e311SAl Viro 		return failed_creating(dentry);
480680b3024SAl Viro 
481edac65eaSAl Viro 	inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
482edac65eaSAl Viro 	inode->i_op = &simple_dir_inode_operations;
483edac65eaSAl Viro 	inode->i_fop = &simple_dir_operations;
484edac65eaSAl Viro 
485edac65eaSAl Viro 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
486edac65eaSAl Viro 	inc_nlink(inode);
487680b3024SAl Viro 	d_instantiate(dentry, inode);
4882b0143b5SDavid Howells 	inc_nlink(d_inode(dentry->d_parent));
4892b0143b5SDavid Howells 	fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
4905233e311SAl Viro 	return end_creating(dentry);
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_dir);
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds /**
49577b3da6eSAl Viro  * debugfs_create_automount - create automount point in the debugfs filesystem
49677b3da6eSAl Viro  * @name: a pointer to a string containing the name of the file to create.
49777b3da6eSAl Viro  * @parent: a pointer to the parent dentry for this file.  This should be a
49877b3da6eSAl Viro  *          directory dentry if set.  If this parameter is NULL, then the
49977b3da6eSAl Viro  *          file will be created in the root of the debugfs filesystem.
50077b3da6eSAl Viro  * @f: function to be called when pathname resolution steps on that one.
50177b3da6eSAl Viro  * @data: opaque argument to pass to f().
50277b3da6eSAl Viro  *
50377b3da6eSAl Viro  * @f should return what ->d_automount() would.
50477b3da6eSAl Viro  */
50577b3da6eSAl Viro struct dentry *debugfs_create_automount(const char *name,
50677b3da6eSAl Viro 					struct dentry *parent,
507*93faccbbSEric W. Biederman 					debugfs_automount_t f,
50877b3da6eSAl Viro 					void *data)
50977b3da6eSAl Viro {
51077b3da6eSAl Viro 	struct dentry *dentry = start_creating(name, parent);
51177b3da6eSAl Viro 	struct inode *inode;
51277b3da6eSAl Viro 
51377b3da6eSAl Viro 	if (IS_ERR(dentry))
51477b3da6eSAl Viro 		return NULL;
51577b3da6eSAl Viro 
51677b3da6eSAl Viro 	inode = debugfs_get_inode(dentry->d_sb);
51777b3da6eSAl Viro 	if (unlikely(!inode))
51877b3da6eSAl Viro 		return failed_creating(dentry);
51977b3da6eSAl Viro 
52087243debSSeth Forshee 	make_empty_dir_inode(inode);
52177b3da6eSAl Viro 	inode->i_flags |= S_AUTOMOUNT;
52277b3da6eSAl Viro 	inode->i_private = data;
52377b3da6eSAl Viro 	dentry->d_fsdata = (void *)f;
524a8f324a4SRoman Pen 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
525a8f324a4SRoman Pen 	inc_nlink(inode);
52677b3da6eSAl Viro 	d_instantiate(dentry, inode);
527a8f324a4SRoman Pen 	inc_nlink(d_inode(dentry->d_parent));
528a8f324a4SRoman Pen 	fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
52977b3da6eSAl Viro 	return end_creating(dentry);
53077b3da6eSAl Viro }
53177b3da6eSAl Viro EXPORT_SYMBOL(debugfs_create_automount);
53277b3da6eSAl Viro 
53377b3da6eSAl Viro /**
53466f54963SPeter Oberparleiter  * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
53566f54963SPeter Oberparleiter  * @name: a pointer to a string containing the name of the symbolic link to
53666f54963SPeter Oberparleiter  *        create.
53766f54963SPeter Oberparleiter  * @parent: a pointer to the parent dentry for this symbolic link.  This
538e227867fSMasanari Iida  *          should be a directory dentry if set.  If this parameter is NULL,
53966f54963SPeter Oberparleiter  *          then the symbolic link will be created in the root of the debugfs
54066f54963SPeter Oberparleiter  *          filesystem.
54166f54963SPeter Oberparleiter  * @target: a pointer to a string containing the path to the target of the
54266f54963SPeter Oberparleiter  *          symbolic link.
54366f54963SPeter Oberparleiter  *
54466f54963SPeter Oberparleiter  * This function creates a symbolic link with the given name in debugfs that
54566f54963SPeter Oberparleiter  * links to the given target path.
54666f54963SPeter Oberparleiter  *
54766f54963SPeter Oberparleiter  * This function will return a pointer to a dentry if it succeeds.  This
54866f54963SPeter Oberparleiter  * pointer must be passed to the debugfs_remove() function when the symbolic
54966f54963SPeter Oberparleiter  * link is to be removed (no automatic cleanup happens if your module is
55066f54963SPeter Oberparleiter  * unloaded, you are responsible here.)  If an error occurs, %NULL will be
55166f54963SPeter Oberparleiter  * returned.
55266f54963SPeter Oberparleiter  *
55366f54963SPeter Oberparleiter  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
554873760fbSCornelia Huck  * returned.
55566f54963SPeter Oberparleiter  */
55666f54963SPeter Oberparleiter struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
55766f54963SPeter Oberparleiter 				      const char *target)
55866f54963SPeter Oberparleiter {
559ad5abd5bSAl Viro 	struct dentry *dentry;
560680b3024SAl Viro 	struct inode *inode;
561680b3024SAl Viro 	char *link = kstrdup(target, GFP_KERNEL);
56266f54963SPeter Oberparleiter 	if (!link)
56366f54963SPeter Oberparleiter 		return NULL;
56466f54963SPeter Oberparleiter 
565ad5abd5bSAl Viro 	dentry = start_creating(name, parent);
566ad5abd5bSAl Viro 	if (IS_ERR(dentry)) {
56766f54963SPeter Oberparleiter 		kfree(link);
568ad5abd5bSAl Viro 		return NULL;
569ad5abd5bSAl Viro 	}
570ad5abd5bSAl Viro 
571edac65eaSAl Viro 	inode = debugfs_get_inode(dentry->d_sb);
572680b3024SAl Viro 	if (unlikely(!inode)) {
573ad5abd5bSAl Viro 		kfree(link);
5745233e311SAl Viro 		return failed_creating(dentry);
575680b3024SAl Viro 	}
576edac65eaSAl Viro 	inode->i_mode = S_IFLNK | S_IRWXUGO;
5775723cb01SAl Viro 	inode->i_op = &simple_symlink_inode_operations;
5785723cb01SAl Viro 	inode->i_link = link;
579680b3024SAl Viro 	d_instantiate(dentry, inode);
5805233e311SAl Viro 	return end_creating(dentry);
58166f54963SPeter Oberparleiter }
58266f54963SPeter Oberparleiter EXPORT_SYMBOL_GPL(debugfs_create_symlink);
58366f54963SPeter Oberparleiter 
58425d41d84SJan Kara static int __debugfs_remove(struct dentry *dentry, struct dentry *parent)
5851da177e4SLinus Torvalds {
58665c33336SMathieu Desnoyers 	int ret = 0;
5871da177e4SLinus Torvalds 
588dc3f4198SAl Viro 	if (simple_positive(dentry)) {
58929a7f3adSMathieu Desnoyers 		dget(dentry);
5907ceab50cSDavid Howells 		if (d_is_dir(dentry))
5912b0143b5SDavid Howells 			ret = simple_rmdir(d_inode(parent), dentry);
5920db59e59SAl Viro 		else
5932b0143b5SDavid Howells 			simple_unlink(d_inode(parent), dentry);
59429a7f3adSMathieu Desnoyers 		if (!ret)
59529a7f3adSMathieu Desnoyers 			d_delete(dentry);
59629a7f3adSMathieu Desnoyers 		dput(dentry);
5971da177e4SLinus Torvalds 	}
59825d41d84SJan Kara 	return ret;
5999505e637SHaavard Skinnemoen }
6009505e637SHaavard Skinnemoen 
6019505e637SHaavard Skinnemoen /**
6029505e637SHaavard Skinnemoen  * debugfs_remove - removes a file or directory from the debugfs filesystem
6039505e637SHaavard Skinnemoen  * @dentry: a pointer to a the dentry of the file or directory to be
604398dc4adSUlf Magnusson  *          removed.  If this parameter is NULL or an error value, nothing
605398dc4adSUlf Magnusson  *          will be done.
6069505e637SHaavard Skinnemoen  *
6079505e637SHaavard Skinnemoen  * This function removes a file or directory in debugfs that was previously
6089505e637SHaavard Skinnemoen  * created with a call to another debugfs function (like
6099505e637SHaavard Skinnemoen  * debugfs_create_file() or variants thereof.)
6109505e637SHaavard Skinnemoen  *
6119505e637SHaavard Skinnemoen  * This function is required to be called in order for the file to be
6129505e637SHaavard Skinnemoen  * removed, no automatic cleanup of files will happen when a module is
6139505e637SHaavard Skinnemoen  * removed, you are responsible here.
6149505e637SHaavard Skinnemoen  */
6159505e637SHaavard Skinnemoen void debugfs_remove(struct dentry *dentry)
6169505e637SHaavard Skinnemoen {
6179505e637SHaavard Skinnemoen 	struct dentry *parent;
61825d41d84SJan Kara 	int ret;
6199505e637SHaavard Skinnemoen 
620a59d6293SArend van Spriel 	if (IS_ERR_OR_NULL(dentry))
6219505e637SHaavard Skinnemoen 		return;
6229505e637SHaavard Skinnemoen 
6239505e637SHaavard Skinnemoen 	parent = dentry->d_parent;
6245955102cSAl Viro 	inode_lock(d_inode(parent));
62525d41d84SJan Kara 	ret = __debugfs_remove(dentry, parent);
6265955102cSAl Viro 	inode_unlock(d_inode(parent));
62725d41d84SJan Kara 	if (!ret)
6281da177e4SLinus Torvalds 		simple_release_fs(&debugfs_mount, &debugfs_mount_count);
62949d200deSNicolai Stange 
6309fd4dcecSNicolai Stange 	synchronize_srcu(&debugfs_srcu);
6311da177e4SLinus Torvalds }
6321da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_remove);
6331da177e4SLinus Torvalds 
634cfc94cdfSJan Kara /**
6359505e637SHaavard Skinnemoen  * debugfs_remove_recursive - recursively removes a directory
636398dc4adSUlf Magnusson  * @dentry: a pointer to a the dentry of the directory to be removed.  If this
637398dc4adSUlf Magnusson  *          parameter is NULL or an error value, nothing will be done.
6389505e637SHaavard Skinnemoen  *
6399505e637SHaavard Skinnemoen  * This function recursively removes a directory tree in debugfs that
6409505e637SHaavard Skinnemoen  * was previously created with a call to another debugfs function
6419505e637SHaavard Skinnemoen  * (like debugfs_create_file() or variants thereof.)
6429505e637SHaavard Skinnemoen  *
6439505e637SHaavard Skinnemoen  * This function is required to be called in order for the file to be
6449505e637SHaavard Skinnemoen  * removed, no automatic cleanup of files will happen when a module is
6459505e637SHaavard Skinnemoen  * removed, you are responsible here.
6469505e637SHaavard Skinnemoen  */
6479505e637SHaavard Skinnemoen void debugfs_remove_recursive(struct dentry *dentry)
6489505e637SHaavard Skinnemoen {
649485d4402SSteven Rostedt 	struct dentry *child, *parent;
6509505e637SHaavard Skinnemoen 
651a59d6293SArend van Spriel 	if (IS_ERR_OR_NULL(dentry))
6529505e637SHaavard Skinnemoen 		return;
6539505e637SHaavard Skinnemoen 
6549505e637SHaavard Skinnemoen 	parent = dentry;
655776164c1SOleg Nesterov  down:
6565955102cSAl Viro 	inode_lock(d_inode(parent));
657485d4402SSteven Rostedt  loop:
658485d4402SSteven Rostedt 	/*
659485d4402SSteven Rostedt 	 * The parent->d_subdirs is protected by the d_lock. Outside that
660485d4402SSteven Rostedt 	 * lock, the child can be unlinked and set to be freed which can
661485d4402SSteven Rostedt 	 * use the d_u.d_child as the rcu head and corrupt this list.
662485d4402SSteven Rostedt 	 */
663485d4402SSteven Rostedt 	spin_lock(&parent->d_lock);
664946e51f2SAl Viro 	list_for_each_entry(child, &parent->d_subdirs, d_child) {
665dc3f4198SAl Viro 		if (!simple_positive(child))
666776164c1SOleg Nesterov 			continue;
6679505e637SHaavard Skinnemoen 
668776164c1SOleg Nesterov 		/* perhaps simple_empty(child) makes more sense */
6699505e637SHaavard Skinnemoen 		if (!list_empty(&child->d_subdirs)) {
670485d4402SSteven Rostedt 			spin_unlock(&parent->d_lock);
6715955102cSAl Viro 			inode_unlock(d_inode(parent));
6729505e637SHaavard Skinnemoen 			parent = child;
673776164c1SOleg Nesterov 			goto down;
6749505e637SHaavard Skinnemoen 		}
675485d4402SSteven Rostedt 
676485d4402SSteven Rostedt 		spin_unlock(&parent->d_lock);
677485d4402SSteven Rostedt 
678776164c1SOleg Nesterov 		if (!__debugfs_remove(child, parent))
679776164c1SOleg Nesterov 			simple_release_fs(&debugfs_mount, &debugfs_mount_count);
680485d4402SSteven Rostedt 
681485d4402SSteven Rostedt 		/*
682485d4402SSteven Rostedt 		 * The parent->d_lock protects agaist child from unlinking
683485d4402SSteven Rostedt 		 * from d_subdirs. When releasing the parent->d_lock we can
684485d4402SSteven Rostedt 		 * no longer trust that the next pointer is valid.
685485d4402SSteven Rostedt 		 * Restart the loop. We'll skip this one with the
686dc3f4198SAl Viro 		 * simple_positive() check.
687485d4402SSteven Rostedt 		 */
688485d4402SSteven Rostedt 		goto loop;
689776164c1SOleg Nesterov 	}
690485d4402SSteven Rostedt 	spin_unlock(&parent->d_lock);
691776164c1SOleg Nesterov 
6925955102cSAl Viro 	inode_unlock(d_inode(parent));
693776164c1SOleg Nesterov 	child = parent;
694776164c1SOleg Nesterov 	parent = parent->d_parent;
6955955102cSAl Viro 	inode_lock(d_inode(parent));
696776164c1SOleg Nesterov 
697485d4402SSteven Rostedt 	if (child != dentry)
698485d4402SSteven Rostedt 		/* go up */
699485d4402SSteven Rostedt 		goto loop;
70056a83cc9SSteven Rostedt 
701776164c1SOleg Nesterov 	if (!__debugfs_remove(child, parent))
7029505e637SHaavard Skinnemoen 		simple_release_fs(&debugfs_mount, &debugfs_mount_count);
7035955102cSAl Viro 	inode_unlock(d_inode(parent));
70449d200deSNicolai Stange 
7059fd4dcecSNicolai Stange 	synchronize_srcu(&debugfs_srcu);
7069505e637SHaavard Skinnemoen }
7079505e637SHaavard Skinnemoen EXPORT_SYMBOL_GPL(debugfs_remove_recursive);
7089505e637SHaavard Skinnemoen 
7099505e637SHaavard Skinnemoen /**
710cfc94cdfSJan Kara  * debugfs_rename - rename a file/directory in the debugfs filesystem
711cfc94cdfSJan Kara  * @old_dir: a pointer to the parent dentry for the renamed object. This
712cfc94cdfSJan Kara  *          should be a directory dentry.
713cfc94cdfSJan Kara  * @old_dentry: dentry of an object to be renamed.
714cfc94cdfSJan Kara  * @new_dir: a pointer to the parent dentry where the object should be
715cfc94cdfSJan Kara  *          moved. This should be a directory dentry.
716cfc94cdfSJan Kara  * @new_name: a pointer to a string containing the target name.
717cfc94cdfSJan Kara  *
718cfc94cdfSJan Kara  * This function renames a file/directory in debugfs.  The target must not
719cfc94cdfSJan Kara  * exist for rename to succeed.
720cfc94cdfSJan Kara  *
721cfc94cdfSJan Kara  * This function will return a pointer to old_dentry (which is updated to
722cfc94cdfSJan Kara  * reflect renaming) if it succeeds. If an error occurs, %NULL will be
723cfc94cdfSJan Kara  * returned.
724cfc94cdfSJan Kara  *
725cfc94cdfSJan Kara  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
726cfc94cdfSJan Kara  * returned.
727cfc94cdfSJan Kara  */
728cfc94cdfSJan Kara struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
729cfc94cdfSJan Kara 		struct dentry *new_dir, const char *new_name)
730cfc94cdfSJan Kara {
731cfc94cdfSJan Kara 	int error;
732cfc94cdfSJan Kara 	struct dentry *dentry = NULL, *trap;
733cfc94cdfSJan Kara 	const char *old_name;
734cfc94cdfSJan Kara 
735cfc94cdfSJan Kara 	trap = lock_rename(new_dir, old_dir);
736cfc94cdfSJan Kara 	/* Source or destination directories don't exist? */
7372b0143b5SDavid Howells 	if (d_really_is_negative(old_dir) || d_really_is_negative(new_dir))
738cfc94cdfSJan Kara 		goto exit;
739cfc94cdfSJan Kara 	/* Source does not exist, cyclic rename, or mountpoint? */
7402b0143b5SDavid Howells 	if (d_really_is_negative(old_dentry) || old_dentry == trap ||
741cfc94cdfSJan Kara 	    d_mountpoint(old_dentry))
742cfc94cdfSJan Kara 		goto exit;
743cfc94cdfSJan Kara 	dentry = lookup_one_len(new_name, new_dir, strlen(new_name));
744cfc94cdfSJan Kara 	/* Lookup failed, cyclic rename or target exists? */
7452b0143b5SDavid Howells 	if (IS_ERR(dentry) || dentry == trap || d_really_is_positive(dentry))
746cfc94cdfSJan Kara 		goto exit;
747cfc94cdfSJan Kara 
748cfc94cdfSJan Kara 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
749cfc94cdfSJan Kara 
7502b0143b5SDavid Howells 	error = simple_rename(d_inode(old_dir), old_dentry, d_inode(new_dir),
751e0e0be8aSMiklos Szeredi 			      dentry, 0);
752cfc94cdfSJan Kara 	if (error) {
753cfc94cdfSJan Kara 		fsnotify_oldname_free(old_name);
754cfc94cdfSJan Kara 		goto exit;
755cfc94cdfSJan Kara 	}
756cfc94cdfSJan Kara 	d_move(old_dentry, dentry);
7572b0143b5SDavid Howells 	fsnotify_move(d_inode(old_dir), d_inode(new_dir), old_name,
758e36cb0b8SDavid Howells 		d_is_dir(old_dentry),
7595a190ae6SAl Viro 		NULL, old_dentry);
760cfc94cdfSJan Kara 	fsnotify_oldname_free(old_name);
761cfc94cdfSJan Kara 	unlock_rename(new_dir, old_dir);
762cfc94cdfSJan Kara 	dput(dentry);
763cfc94cdfSJan Kara 	return old_dentry;
764cfc94cdfSJan Kara exit:
765cfc94cdfSJan Kara 	if (dentry && !IS_ERR(dentry))
766cfc94cdfSJan Kara 		dput(dentry);
767cfc94cdfSJan Kara 	unlock_rename(new_dir, old_dir);
768cfc94cdfSJan Kara 	return NULL;
769cfc94cdfSJan Kara }
770cfc94cdfSJan Kara EXPORT_SYMBOL_GPL(debugfs_rename);
771cfc94cdfSJan Kara 
772c0f92ba9SFrederic Weisbecker /**
773c0f92ba9SFrederic Weisbecker  * debugfs_initialized - Tells whether debugfs has been registered
774c0f92ba9SFrederic Weisbecker  */
775c0f92ba9SFrederic Weisbecker bool debugfs_initialized(void)
776c0f92ba9SFrederic Weisbecker {
777c0f92ba9SFrederic Weisbecker 	return debugfs_registered;
778c0f92ba9SFrederic Weisbecker }
779c0f92ba9SFrederic Weisbecker EXPORT_SYMBOL_GPL(debugfs_initialized);
780c0f92ba9SFrederic Weisbecker 
7811da177e4SLinus Torvalds static int __init debugfs_init(void)
7821da177e4SLinus Torvalds {
7831da177e4SLinus Torvalds 	int retval;
7841da177e4SLinus Torvalds 
785f9bb4882SEric W. Biederman 	retval = sysfs_create_mount_point(kernel_kobj, "debug");
786f9bb4882SEric W. Biederman 	if (retval)
787f9bb4882SEric W. Biederman 		return retval;
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds 	retval = register_filesystem(&debug_fs_type);
7901da177e4SLinus Torvalds 	if (retval)
791f9bb4882SEric W. Biederman 		sysfs_remove_mount_point(kernel_kobj, "debug");
792c0f92ba9SFrederic Weisbecker 	else
793c0f92ba9SFrederic Weisbecker 		debugfs_registered = true;
794c0f92ba9SFrederic Weisbecker 
7951da177e4SLinus Torvalds 	return retval;
7961da177e4SLinus Torvalds }
7971da177e4SLinus Torvalds core_initcall(debugfs_init);
7981da177e4SLinus Torvalds 
799