xref: /linux-6.15/fs/debugfs/inode.c (revision cfa57c11)
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 
31d6e48686SLudwig Nussel #define DEBUGFS_DEFAULT_MODE	0755
32d6e48686SLudwig Nussel 
331da177e4SLinus Torvalds static struct vfsmount *debugfs_mount;
341da177e4SLinus Torvalds static int debugfs_mount_count;
35c0f92ba9SFrederic Weisbecker static bool debugfs_registered;
361da177e4SLinus Torvalds 
37f4ae40a6SAl Viro static struct inode *debugfs_get_inode(struct super_block *sb, umode_t mode, dev_t dev,
38d3a3b0adSMathieu Desnoyers 				       void *data, const struct file_operations *fops)
39d3a3b0adSMathieu Desnoyers 
401da177e4SLinus Torvalds {
411da177e4SLinus Torvalds 	struct inode *inode = new_inode(sb);
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds 	if (inode) {
4485fe4025SChristoph Hellwig 		inode->i_ino = get_next_ino();
451da177e4SLinus Torvalds 		inode->i_mode = mode;
461da177e4SLinus Torvalds 		inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
471da177e4SLinus Torvalds 		switch (mode & S_IFMT) {
481da177e4SLinus Torvalds 		default:
491da177e4SLinus Torvalds 			init_special_inode(inode, mode, dev);
501da177e4SLinus Torvalds 			break;
511da177e4SLinus Torvalds 		case S_IFREG:
52d3a3b0adSMathieu Desnoyers 			inode->i_fop = fops ? fops : &debugfs_file_operations;
53d3a3b0adSMathieu Desnoyers 			inode->i_private = data;
541da177e4SLinus Torvalds 			break;
5566f54963SPeter Oberparleiter 		case S_IFLNK:
5666f54963SPeter Oberparleiter 			inode->i_op = &debugfs_link_operations;
57d3a3b0adSMathieu Desnoyers 			inode->i_fop = fops;
58d3a3b0adSMathieu Desnoyers 			inode->i_private = data;
5966f54963SPeter Oberparleiter 			break;
601da177e4SLinus Torvalds 		case S_IFDIR:
611da177e4SLinus Torvalds 			inode->i_op = &simple_dir_inode_operations;
62d3a3b0adSMathieu Desnoyers 			inode->i_fop = fops ? fops : &simple_dir_operations;
63d3a3b0adSMathieu Desnoyers 			inode->i_private = data;
641da177e4SLinus Torvalds 
65bafb232eSMathieu Desnoyers 			/* directory inodes start off with i_nlink == 2
66bafb232eSMathieu Desnoyers 			 * (for "." entry) */
67d8c76e6fSDave Hansen 			inc_nlink(inode);
681da177e4SLinus Torvalds 			break;
691da177e4SLinus Torvalds 		}
701da177e4SLinus Torvalds 	}
711da177e4SLinus Torvalds 	return inode;
721da177e4SLinus Torvalds }
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds /* SMP-safe */
751da177e4SLinus Torvalds static int debugfs_mknod(struct inode *dir, struct dentry *dentry,
76f4ae40a6SAl Viro 			 umode_t mode, dev_t dev, void *data,
77d3a3b0adSMathieu Desnoyers 			 const struct file_operations *fops)
781da177e4SLinus Torvalds {
7971601e2bSJens Axboe 	struct inode *inode;
801da177e4SLinus Torvalds 	int error = -EPERM;
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 	if (dentry->d_inode)
831da177e4SLinus Torvalds 		return -EEXIST;
841da177e4SLinus Torvalds 
85d3a3b0adSMathieu Desnoyers 	inode = debugfs_get_inode(dir->i_sb, mode, dev, data, fops);
861da177e4SLinus Torvalds 	if (inode) {
871da177e4SLinus Torvalds 		d_instantiate(dentry, inode);
881da177e4SLinus Torvalds 		dget(dentry);
891da177e4SLinus Torvalds 		error = 0;
901da177e4SLinus Torvalds 	}
911da177e4SLinus Torvalds 	return error;
921da177e4SLinus Torvalds }
931da177e4SLinus Torvalds 
94f4ae40a6SAl Viro static int debugfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode,
95d3a3b0adSMathieu Desnoyers 			 void *data, const struct file_operations *fops)
961da177e4SLinus Torvalds {
971da177e4SLinus Torvalds 	int res;
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 	mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
100d3a3b0adSMathieu Desnoyers 	res = debugfs_mknod(dir, dentry, mode, 0, data, fops);
1014f36557fSMathieu Desnoyers 	if (!res) {
102d8c76e6fSDave Hansen 		inc_nlink(dir);
1034f36557fSMathieu Desnoyers 		fsnotify_mkdir(dir, dentry);
1044f36557fSMathieu Desnoyers 	}
1051da177e4SLinus Torvalds 	return res;
1061da177e4SLinus Torvalds }
1071da177e4SLinus Torvalds 
108f4ae40a6SAl Viro static int debugfs_link(struct inode *dir, struct dentry *dentry, umode_t mode,
109d3a3b0adSMathieu Desnoyers 			void *data, const struct file_operations *fops)
11066f54963SPeter Oberparleiter {
11166f54963SPeter Oberparleiter 	mode = (mode & S_IALLUGO) | S_IFLNK;
112d3a3b0adSMathieu Desnoyers 	return debugfs_mknod(dir, dentry, mode, 0, data, fops);
11366f54963SPeter Oberparleiter }
11466f54963SPeter Oberparleiter 
115f4ae40a6SAl Viro static int debugfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
116d3a3b0adSMathieu Desnoyers 			  void *data, const struct file_operations *fops)
1171da177e4SLinus Torvalds {
1184f36557fSMathieu Desnoyers 	int res;
1194f36557fSMathieu Desnoyers 
1201da177e4SLinus Torvalds 	mode = (mode & S_IALLUGO) | S_IFREG;
121d3a3b0adSMathieu Desnoyers 	res = debugfs_mknod(dir, dentry, mode, 0, data, fops);
1224f36557fSMathieu Desnoyers 	if (!res)
1234f36557fSMathieu Desnoyers 		fsnotify_create(dir, dentry);
1244f36557fSMathieu Desnoyers 	return res;
1251da177e4SLinus Torvalds }
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds static inline int debugfs_positive(struct dentry *dentry)
1281da177e4SLinus Torvalds {
1291da177e4SLinus Torvalds 	return dentry->d_inode && !d_unhashed(dentry);
1301da177e4SLinus Torvalds }
1311da177e4SLinus Torvalds 
132d6e48686SLudwig Nussel struct debugfs_mount_opts {
133d6e48686SLudwig Nussel 	uid_t uid;
134d6e48686SLudwig Nussel 	gid_t gid;
135d6e48686SLudwig Nussel 	umode_t mode;
136d6e48686SLudwig Nussel };
137d6e48686SLudwig Nussel 
138d6e48686SLudwig Nussel enum {
139d6e48686SLudwig Nussel 	Opt_uid,
140d6e48686SLudwig Nussel 	Opt_gid,
141d6e48686SLudwig Nussel 	Opt_mode,
142d6e48686SLudwig Nussel 	Opt_err
143d6e48686SLudwig Nussel };
144d6e48686SLudwig Nussel 
145d6e48686SLudwig Nussel static const match_table_t tokens = {
146d6e48686SLudwig Nussel 	{Opt_uid, "uid=%u"},
147d6e48686SLudwig Nussel 	{Opt_gid, "gid=%u"},
148d6e48686SLudwig Nussel 	{Opt_mode, "mode=%o"},
149d6e48686SLudwig Nussel 	{Opt_err, NULL}
150d6e48686SLudwig Nussel };
151d6e48686SLudwig Nussel 
152d6e48686SLudwig Nussel struct debugfs_fs_info {
153d6e48686SLudwig Nussel 	struct debugfs_mount_opts mount_opts;
154d6e48686SLudwig Nussel };
155d6e48686SLudwig Nussel 
156d6e48686SLudwig Nussel static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts)
157d6e48686SLudwig Nussel {
158d6e48686SLudwig Nussel 	substring_t args[MAX_OPT_ARGS];
159d6e48686SLudwig Nussel 	int option;
160d6e48686SLudwig Nussel 	int token;
161d6e48686SLudwig Nussel 	char *p;
162d6e48686SLudwig Nussel 
163d6e48686SLudwig Nussel 	opts->mode = DEBUGFS_DEFAULT_MODE;
164d6e48686SLudwig Nussel 
165d6e48686SLudwig Nussel 	while ((p = strsep(&data, ",")) != NULL) {
166d6e48686SLudwig Nussel 		if (!*p)
167d6e48686SLudwig Nussel 			continue;
168d6e48686SLudwig Nussel 
169d6e48686SLudwig Nussel 		token = match_token(p, tokens, args);
170d6e48686SLudwig Nussel 		switch (token) {
171d6e48686SLudwig Nussel 		case Opt_uid:
172d6e48686SLudwig Nussel 			if (match_int(&args[0], &option))
173d6e48686SLudwig Nussel 				return -EINVAL;
174d6e48686SLudwig Nussel 			opts->uid = option;
175d6e48686SLudwig Nussel 			break;
176d6e48686SLudwig Nussel 		case Opt_gid:
177d6e48686SLudwig Nussel 			if (match_octal(&args[0], &option))
178d6e48686SLudwig Nussel 				return -EINVAL;
179d6e48686SLudwig Nussel 			opts->gid = option;
180d6e48686SLudwig Nussel 			break;
181d6e48686SLudwig Nussel 		case Opt_mode:
182d6e48686SLudwig Nussel 			if (match_octal(&args[0], &option))
183d6e48686SLudwig Nussel 				return -EINVAL;
184d6e48686SLudwig Nussel 			opts->mode = option & S_IALLUGO;
185d6e48686SLudwig Nussel 			break;
186d6e48686SLudwig Nussel 		/*
187d6e48686SLudwig Nussel 		 * We might like to report bad mount options here;
188d6e48686SLudwig Nussel 		 * but traditionally debugfs has ignored all mount options
189d6e48686SLudwig Nussel 		 */
190d6e48686SLudwig Nussel 		}
191d6e48686SLudwig Nussel 	}
192d6e48686SLudwig Nussel 
193d6e48686SLudwig Nussel 	return 0;
194d6e48686SLudwig Nussel }
195d6e48686SLudwig Nussel 
196d6e48686SLudwig Nussel static int debugfs_apply_options(struct super_block *sb)
197d6e48686SLudwig Nussel {
198d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi = sb->s_fs_info;
199d6e48686SLudwig Nussel 	struct inode *inode = sb->s_root->d_inode;
200d6e48686SLudwig Nussel 	struct debugfs_mount_opts *opts = &fsi->mount_opts;
201d6e48686SLudwig Nussel 
202d6e48686SLudwig Nussel 	inode->i_mode &= ~S_IALLUGO;
203d6e48686SLudwig Nussel 	inode->i_mode |= opts->mode;
204d6e48686SLudwig Nussel 
205d6e48686SLudwig Nussel 	inode->i_uid = opts->uid;
206d6e48686SLudwig Nussel 	inode->i_gid = opts->gid;
207d6e48686SLudwig Nussel 
208d6e48686SLudwig Nussel 	return 0;
209d6e48686SLudwig Nussel }
210d6e48686SLudwig Nussel 
211d6e48686SLudwig Nussel static int debugfs_remount(struct super_block *sb, int *flags, char *data)
212d6e48686SLudwig Nussel {
213d6e48686SLudwig Nussel 	int err;
214d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi = sb->s_fs_info;
215d6e48686SLudwig Nussel 
216d6e48686SLudwig Nussel 	err = debugfs_parse_options(data, &fsi->mount_opts);
217d6e48686SLudwig Nussel 	if (err)
218d6e48686SLudwig Nussel 		goto fail;
219d6e48686SLudwig Nussel 
220d6e48686SLudwig Nussel 	debugfs_apply_options(sb);
221d6e48686SLudwig Nussel 
222d6e48686SLudwig Nussel fail:
223d6e48686SLudwig Nussel 	return err;
224d6e48686SLudwig Nussel }
225d6e48686SLudwig Nussel 
226d6e48686SLudwig Nussel static int debugfs_show_options(struct seq_file *m, struct dentry *root)
227d6e48686SLudwig Nussel {
228d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi = root->d_sb->s_fs_info;
229d6e48686SLudwig Nussel 	struct debugfs_mount_opts *opts = &fsi->mount_opts;
230d6e48686SLudwig Nussel 
231d6e48686SLudwig Nussel 	if (opts->uid != 0)
232d6e48686SLudwig Nussel 		seq_printf(m, ",uid=%u", opts->uid);
233d6e48686SLudwig Nussel 	if (opts->gid != 0)
234d6e48686SLudwig Nussel 		seq_printf(m, ",gid=%u", opts->gid);
235d6e48686SLudwig Nussel 	if (opts->mode != DEBUGFS_DEFAULT_MODE)
236d6e48686SLudwig Nussel 		seq_printf(m, ",mode=%o", opts->mode);
237d6e48686SLudwig Nussel 
238d6e48686SLudwig Nussel 	return 0;
239d6e48686SLudwig Nussel }
240d6e48686SLudwig Nussel 
241d6e48686SLudwig Nussel static const struct super_operations debugfs_super_operations = {
242d6e48686SLudwig Nussel 	.statfs		= simple_statfs,
243d6e48686SLudwig Nussel 	.remount_fs	= debugfs_remount,
244d6e48686SLudwig Nussel 	.show_options	= debugfs_show_options,
245d6e48686SLudwig Nussel };
246d6e48686SLudwig Nussel 
2471da177e4SLinus Torvalds static int debug_fill_super(struct super_block *sb, void *data, int silent)
2481da177e4SLinus Torvalds {
2491da177e4SLinus Torvalds 	static struct tree_descr debug_files[] = {{""}};
250d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi;
251d6e48686SLudwig Nussel 	int err;
2521da177e4SLinus Torvalds 
253d6e48686SLudwig Nussel 	save_mount_options(sb, data);
254d6e48686SLudwig Nussel 
255d6e48686SLudwig Nussel 	fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL);
256d6e48686SLudwig Nussel 	sb->s_fs_info = fsi;
257d6e48686SLudwig Nussel 	if (!fsi) {
258d6e48686SLudwig Nussel 		err = -ENOMEM;
259d6e48686SLudwig Nussel 		goto fail;
260d6e48686SLudwig Nussel 	}
261d6e48686SLudwig Nussel 
262d6e48686SLudwig Nussel 	err = debugfs_parse_options(data, &fsi->mount_opts);
263d6e48686SLudwig Nussel 	if (err)
264d6e48686SLudwig Nussel 		goto fail;
265d6e48686SLudwig Nussel 
266d6e48686SLudwig Nussel 	err  =  simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
267d6e48686SLudwig Nussel 	if (err)
268d6e48686SLudwig Nussel 		goto fail;
269d6e48686SLudwig Nussel 
270d6e48686SLudwig Nussel 	sb->s_op = &debugfs_super_operations;
271d6e48686SLudwig Nussel 
272d6e48686SLudwig Nussel 	debugfs_apply_options(sb);
273d6e48686SLudwig Nussel 
274d6e48686SLudwig Nussel 	return 0;
275d6e48686SLudwig Nussel 
276d6e48686SLudwig Nussel fail:
277d6e48686SLudwig Nussel 	kfree(fsi);
278d6e48686SLudwig Nussel 	sb->s_fs_info = NULL;
279d6e48686SLudwig Nussel 	return err;
2801da177e4SLinus Torvalds }
2811da177e4SLinus Torvalds 
282fc14f2feSAl Viro static struct dentry *debug_mount(struct file_system_type *fs_type,
2831da177e4SLinus Torvalds 			int flags, const char *dev_name,
284fc14f2feSAl Viro 			void *data)
2851da177e4SLinus Torvalds {
286fc14f2feSAl Viro 	return mount_single(fs_type, flags, data, debug_fill_super);
2871da177e4SLinus Torvalds }
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds static struct file_system_type debug_fs_type = {
2901da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
2911da177e4SLinus Torvalds 	.name =		"debugfs",
292fc14f2feSAl Viro 	.mount =	debug_mount,
2931da177e4SLinus Torvalds 	.kill_sb =	kill_litter_super,
2941da177e4SLinus Torvalds };
2951da177e4SLinus Torvalds 
296c3b1a350SAl Viro struct dentry *__create_file(const char *name, umode_t mode,
297c3b1a350SAl Viro 				   struct dentry *parent, void *data,
298c3b1a350SAl Viro 				   const struct file_operations *fops)
299c3b1a350SAl Viro {
300c3b1a350SAl Viro 	struct dentry *dentry = NULL;
301c3b1a350SAl Viro 	int error;
302c3b1a350SAl Viro 
303c3b1a350SAl Viro 	pr_debug("debugfs: creating file '%s'\n",name);
304c3b1a350SAl Viro 
305c3b1a350SAl Viro 	error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
306c3b1a350SAl Viro 			      &debugfs_mount_count);
307c3b1a350SAl Viro 	if (error)
308c3b1a350SAl Viro 		goto exit;
309c3b1a350SAl Viro 
310*cfa57c11SAl Viro 	/* If the parent is not specified, we create it in the root.
311*cfa57c11SAl Viro 	 * We need the root dentry to do this, which is in the super
312*cfa57c11SAl Viro 	 * block. A pointer to that is in the struct vfsmount that we
313*cfa57c11SAl Viro 	 * have around.
314*cfa57c11SAl Viro 	 */
315*cfa57c11SAl Viro 	if (!parent)
316*cfa57c11SAl Viro 		parent = debugfs_mount->mnt_root;
317*cfa57c11SAl Viro 
318*cfa57c11SAl Viro 	dentry = NULL;
319*cfa57c11SAl Viro 	mutex_lock(&parent->d_inode->i_mutex);
320*cfa57c11SAl Viro 	dentry = lookup_one_len(name, parent, strlen(name));
321*cfa57c11SAl Viro 	if (!IS_ERR(dentry)) {
322*cfa57c11SAl Viro 		switch (mode & S_IFMT) {
323*cfa57c11SAl Viro 		case S_IFDIR:
324*cfa57c11SAl Viro 			error = debugfs_mkdir(parent->d_inode, dentry, mode,
325c3b1a350SAl Viro 					      data, fops);
326*cfa57c11SAl Viro 			break;
327*cfa57c11SAl Viro 		case S_IFLNK:
328*cfa57c11SAl Viro 			error = debugfs_link(parent->d_inode, dentry, mode,
329*cfa57c11SAl Viro 					     data, fops);
330*cfa57c11SAl Viro 			break;
331*cfa57c11SAl Viro 		default:
332*cfa57c11SAl Viro 			error = debugfs_create(parent->d_inode, dentry, mode,
333*cfa57c11SAl Viro 					       data, fops);
334*cfa57c11SAl Viro 			break;
335*cfa57c11SAl Viro 		}
336*cfa57c11SAl Viro 		dput(dentry);
337*cfa57c11SAl Viro 	} else
338*cfa57c11SAl Viro 		error = PTR_ERR(dentry);
339*cfa57c11SAl Viro 	mutex_unlock(&parent->d_inode->i_mutex);
340*cfa57c11SAl Viro 
341c3b1a350SAl Viro 	if (error) {
342c3b1a350SAl Viro 		dentry = NULL;
343c3b1a350SAl Viro 		simple_release_fs(&debugfs_mount, &debugfs_mount_count);
344c3b1a350SAl Viro 	}
345c3b1a350SAl Viro exit:
346c3b1a350SAl Viro 	return dentry;
347c3b1a350SAl Viro }
348c3b1a350SAl Viro 
3491da177e4SLinus Torvalds /**
3501da177e4SLinus Torvalds  * debugfs_create_file - create a file in the debugfs filesystem
3511da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
352be030e65SAlberto Bertogli  * @mode: the permission that the file should have.
3531da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
3541da177e4SLinus Torvalds  *          directory dentry if set.  If this paramater is NULL, then the
3551da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
3561da177e4SLinus Torvalds  * @data: a pointer to something that the caller will want to get to later
3578e18e294STheodore Ts'o  *        on.  The inode.i_private pointer will point to this value on
3581da177e4SLinus Torvalds  *        the open() call.
3591da177e4SLinus Torvalds  * @fops: a pointer to a struct file_operations that should be used for
3601da177e4SLinus Torvalds  *        this file.
3611da177e4SLinus Torvalds  *
3621da177e4SLinus Torvalds  * This is the basic "create a file" function for debugfs.  It allows for a
363be030e65SAlberto Bertogli  * wide range of flexibility in creating a file, or a directory (if you want
364be030e65SAlberto Bertogli  * to create a directory, the debugfs_create_dir() function is
3651da177e4SLinus Torvalds  * recommended to be used instead.)
3661da177e4SLinus Torvalds  *
3671da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
3681da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
3691da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
3706468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
3711da177e4SLinus Torvalds  *
3726468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
373873760fbSCornelia Huck  * returned.
3741da177e4SLinus Torvalds  */
375f4ae40a6SAl Viro struct dentry *debugfs_create_file(const char *name, umode_t mode,
3761da177e4SLinus Torvalds 				   struct dentry *parent, void *data,
37799ac48f5SArjan van de Ven 				   const struct file_operations *fops)
3781da177e4SLinus Torvalds {
379c3b1a350SAl Viro 	switch (mode & S_IFMT) {
380c3b1a350SAl Viro 	case S_IFREG:
381c3b1a350SAl Viro 	case 0:
382c3b1a350SAl Viro 		break;
383c3b1a350SAl Viro 	default:
384c3b1a350SAl Viro 		BUG();
3851da177e4SLinus Torvalds 	}
386c3b1a350SAl Viro 
387c3b1a350SAl Viro 	return __create_file(name, mode, parent, data, fops);
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_file);
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds /**
3921da177e4SLinus Torvalds  * debugfs_create_dir - create a directory in the debugfs filesystem
3931da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the directory to
3941da177e4SLinus Torvalds  *        create.
3951da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
3961da177e4SLinus Torvalds  *          directory dentry if set.  If this paramater is NULL, then the
3971da177e4SLinus Torvalds  *          directory will be created in the root of the debugfs filesystem.
3981da177e4SLinus Torvalds  *
3991da177e4SLinus Torvalds  * This function creates a directory in debugfs with the given name.
4001da177e4SLinus Torvalds  *
4011da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
4021da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
4031da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
4046468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
4051da177e4SLinus Torvalds  *
4066468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
407873760fbSCornelia Huck  * returned.
4081da177e4SLinus Torvalds  */
4091da177e4SLinus Torvalds struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
4101da177e4SLinus Torvalds {
411c3b1a350SAl Viro 	return __create_file(name, S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
4121da177e4SLinus Torvalds 				   parent, NULL, NULL);
4131da177e4SLinus Torvalds }
4141da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_dir);
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds /**
41766f54963SPeter Oberparleiter  * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
41866f54963SPeter Oberparleiter  * @name: a pointer to a string containing the name of the symbolic link to
41966f54963SPeter Oberparleiter  *        create.
42066f54963SPeter Oberparleiter  * @parent: a pointer to the parent dentry for this symbolic link.  This
42166f54963SPeter Oberparleiter  *          should be a directory dentry if set.  If this paramater is NULL,
42266f54963SPeter Oberparleiter  *          then the symbolic link will be created in the root of the debugfs
42366f54963SPeter Oberparleiter  *          filesystem.
42466f54963SPeter Oberparleiter  * @target: a pointer to a string containing the path to the target of the
42566f54963SPeter Oberparleiter  *          symbolic link.
42666f54963SPeter Oberparleiter  *
42766f54963SPeter Oberparleiter  * This function creates a symbolic link with the given name in debugfs that
42866f54963SPeter Oberparleiter  * links to the given target path.
42966f54963SPeter Oberparleiter  *
43066f54963SPeter Oberparleiter  * This function will return a pointer to a dentry if it succeeds.  This
43166f54963SPeter Oberparleiter  * pointer must be passed to the debugfs_remove() function when the symbolic
43266f54963SPeter Oberparleiter  * link is to be removed (no automatic cleanup happens if your module is
43366f54963SPeter Oberparleiter  * unloaded, you are responsible here.)  If an error occurs, %NULL will be
43466f54963SPeter Oberparleiter  * returned.
43566f54963SPeter Oberparleiter  *
43666f54963SPeter Oberparleiter  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
437873760fbSCornelia Huck  * returned.
43866f54963SPeter Oberparleiter  */
43966f54963SPeter Oberparleiter struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
44066f54963SPeter Oberparleiter 				      const char *target)
44166f54963SPeter Oberparleiter {
44266f54963SPeter Oberparleiter 	struct dentry *result;
44366f54963SPeter Oberparleiter 	char *link;
44466f54963SPeter Oberparleiter 
44566f54963SPeter Oberparleiter 	link = kstrdup(target, GFP_KERNEL);
44666f54963SPeter Oberparleiter 	if (!link)
44766f54963SPeter Oberparleiter 		return NULL;
44866f54963SPeter Oberparleiter 
449c3b1a350SAl Viro 	result = __create_file(name, S_IFLNK | S_IRWXUGO, parent, link, NULL);
45066f54963SPeter Oberparleiter 	if (!result)
45166f54963SPeter Oberparleiter 		kfree(link);
45266f54963SPeter Oberparleiter 	return result;
45366f54963SPeter Oberparleiter }
45466f54963SPeter Oberparleiter EXPORT_SYMBOL_GPL(debugfs_create_symlink);
45566f54963SPeter Oberparleiter 
45625d41d84SJan Kara static int __debugfs_remove(struct dentry *dentry, struct dentry *parent)
4571da177e4SLinus Torvalds {
45865c33336SMathieu Desnoyers 	int ret = 0;
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	if (debugfs_positive(dentry)) {
4611da177e4SLinus Torvalds 		if (dentry->d_inode) {
46229a7f3adSMathieu Desnoyers 			dget(dentry);
46366f54963SPeter Oberparleiter 			switch (dentry->d_inode->i_mode & S_IFMT) {
46466f54963SPeter Oberparleiter 			case S_IFDIR:
46565c33336SMathieu Desnoyers 				ret = simple_rmdir(parent->d_inode, dentry);
46666f54963SPeter Oberparleiter 				break;
46766f54963SPeter Oberparleiter 			case S_IFLNK:
46866f54963SPeter Oberparleiter 				kfree(dentry->d_inode->i_private);
46966f54963SPeter Oberparleiter 				/* fall through */
47066f54963SPeter Oberparleiter 			default:
4711da177e4SLinus Torvalds 				simple_unlink(parent->d_inode, dentry);
47266f54963SPeter Oberparleiter 				break;
47366f54963SPeter Oberparleiter 			}
47429a7f3adSMathieu Desnoyers 			if (!ret)
47529a7f3adSMathieu Desnoyers 				d_delete(dentry);
47629a7f3adSMathieu Desnoyers 			dput(dentry);
4771da177e4SLinus Torvalds 		}
4781da177e4SLinus Torvalds 	}
47925d41d84SJan Kara 	return ret;
4809505e637SHaavard Skinnemoen }
4819505e637SHaavard Skinnemoen 
4829505e637SHaavard Skinnemoen /**
4839505e637SHaavard Skinnemoen  * debugfs_remove - removes a file or directory from the debugfs filesystem
4849505e637SHaavard Skinnemoen  * @dentry: a pointer to a the dentry of the file or directory to be
4859505e637SHaavard Skinnemoen  *          removed.
4869505e637SHaavard Skinnemoen  *
4879505e637SHaavard Skinnemoen  * This function removes a file or directory in debugfs that was previously
4889505e637SHaavard Skinnemoen  * created with a call to another debugfs function (like
4899505e637SHaavard Skinnemoen  * debugfs_create_file() or variants thereof.)
4909505e637SHaavard Skinnemoen  *
4919505e637SHaavard Skinnemoen  * This function is required to be called in order for the file to be
4929505e637SHaavard Skinnemoen  * removed, no automatic cleanup of files will happen when a module is
4939505e637SHaavard Skinnemoen  * removed, you are responsible here.
4949505e637SHaavard Skinnemoen  */
4959505e637SHaavard Skinnemoen void debugfs_remove(struct dentry *dentry)
4969505e637SHaavard Skinnemoen {
4979505e637SHaavard Skinnemoen 	struct dentry *parent;
49825d41d84SJan Kara 	int ret;
4999505e637SHaavard Skinnemoen 
5009505e637SHaavard Skinnemoen 	if (!dentry)
5019505e637SHaavard Skinnemoen 		return;
5029505e637SHaavard Skinnemoen 
5039505e637SHaavard Skinnemoen 	parent = dentry->d_parent;
5049505e637SHaavard Skinnemoen 	if (!parent || !parent->d_inode)
5059505e637SHaavard Skinnemoen 		return;
5069505e637SHaavard Skinnemoen 
5079505e637SHaavard Skinnemoen 	mutex_lock(&parent->d_inode->i_mutex);
50825d41d84SJan Kara 	ret = __debugfs_remove(dentry, parent);
5091b1dcc1bSJes Sorensen 	mutex_unlock(&parent->d_inode->i_mutex);
51025d41d84SJan Kara 	if (!ret)
5111da177e4SLinus Torvalds 		simple_release_fs(&debugfs_mount, &debugfs_mount_count);
5121da177e4SLinus Torvalds }
5131da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_remove);
5141da177e4SLinus Torvalds 
515cfc94cdfSJan Kara /**
5169505e637SHaavard Skinnemoen  * debugfs_remove_recursive - recursively removes a directory
5179505e637SHaavard Skinnemoen  * @dentry: a pointer to a the dentry of the directory to be removed.
5189505e637SHaavard Skinnemoen  *
5199505e637SHaavard Skinnemoen  * This function recursively removes a directory tree in debugfs that
5209505e637SHaavard Skinnemoen  * was previously created with a call to another debugfs function
5219505e637SHaavard Skinnemoen  * (like debugfs_create_file() or variants thereof.)
5229505e637SHaavard Skinnemoen  *
5239505e637SHaavard Skinnemoen  * This function is required to be called in order for the file to be
5249505e637SHaavard Skinnemoen  * removed, no automatic cleanup of files will happen when a module is
5259505e637SHaavard Skinnemoen  * removed, you are responsible here.
5269505e637SHaavard Skinnemoen  */
5279505e637SHaavard Skinnemoen void debugfs_remove_recursive(struct dentry *dentry)
5289505e637SHaavard Skinnemoen {
5299505e637SHaavard Skinnemoen 	struct dentry *child;
5309505e637SHaavard Skinnemoen 	struct dentry *parent;
5319505e637SHaavard Skinnemoen 
5329505e637SHaavard Skinnemoen 	if (!dentry)
5339505e637SHaavard Skinnemoen 		return;
5349505e637SHaavard Skinnemoen 
5359505e637SHaavard Skinnemoen 	parent = dentry->d_parent;
5369505e637SHaavard Skinnemoen 	if (!parent || !parent->d_inode)
5379505e637SHaavard Skinnemoen 		return;
5389505e637SHaavard Skinnemoen 
5399505e637SHaavard Skinnemoen 	parent = dentry;
5409505e637SHaavard Skinnemoen 	mutex_lock(&parent->d_inode->i_mutex);
5419505e637SHaavard Skinnemoen 
5429505e637SHaavard Skinnemoen 	while (1) {
5439505e637SHaavard Skinnemoen 		/*
5449505e637SHaavard Skinnemoen 		 * When all dentries under "parent" has been removed,
5459505e637SHaavard Skinnemoen 		 * walk up the tree until we reach our starting point.
5469505e637SHaavard Skinnemoen 		 */
5479505e637SHaavard Skinnemoen 		if (list_empty(&parent->d_subdirs)) {
5489505e637SHaavard Skinnemoen 			mutex_unlock(&parent->d_inode->i_mutex);
5499505e637SHaavard Skinnemoen 			if (parent == dentry)
5509505e637SHaavard Skinnemoen 				break;
5519505e637SHaavard Skinnemoen 			parent = parent->d_parent;
5529505e637SHaavard Skinnemoen 			mutex_lock(&parent->d_inode->i_mutex);
5539505e637SHaavard Skinnemoen 		}
5549505e637SHaavard Skinnemoen 		child = list_entry(parent->d_subdirs.next, struct dentry,
5559505e637SHaavard Skinnemoen 				d_u.d_child);
55656a83cc9SSteven Rostedt  next_sibling:
5579505e637SHaavard Skinnemoen 
5589505e637SHaavard Skinnemoen 		/*
5599505e637SHaavard Skinnemoen 		 * If "child" isn't empty, walk down the tree and
5609505e637SHaavard Skinnemoen 		 * remove all its descendants first.
5619505e637SHaavard Skinnemoen 		 */
5629505e637SHaavard Skinnemoen 		if (!list_empty(&child->d_subdirs)) {
5639505e637SHaavard Skinnemoen 			mutex_unlock(&parent->d_inode->i_mutex);
5649505e637SHaavard Skinnemoen 			parent = child;
5659505e637SHaavard Skinnemoen 			mutex_lock(&parent->d_inode->i_mutex);
5669505e637SHaavard Skinnemoen 			continue;
5679505e637SHaavard Skinnemoen 		}
5689505e637SHaavard Skinnemoen 		__debugfs_remove(child, parent);
5699505e637SHaavard Skinnemoen 		if (parent->d_subdirs.next == &child->d_u.d_child) {
5709505e637SHaavard Skinnemoen 			/*
57156a83cc9SSteven Rostedt 			 * Try the next sibling.
57256a83cc9SSteven Rostedt 			 */
57356a83cc9SSteven Rostedt 			if (child->d_u.d_child.next != &parent->d_subdirs) {
57456a83cc9SSteven Rostedt 				child = list_entry(child->d_u.d_child.next,
57556a83cc9SSteven Rostedt 						   struct dentry,
57656a83cc9SSteven Rostedt 						   d_u.d_child);
57756a83cc9SSteven Rostedt 				goto next_sibling;
57856a83cc9SSteven Rostedt 			}
57956a83cc9SSteven Rostedt 
58056a83cc9SSteven Rostedt 			/*
5819505e637SHaavard Skinnemoen 			 * Avoid infinite loop if we fail to remove
5829505e637SHaavard Skinnemoen 			 * one dentry.
5839505e637SHaavard Skinnemoen 			 */
5849505e637SHaavard Skinnemoen 			mutex_unlock(&parent->d_inode->i_mutex);
5859505e637SHaavard Skinnemoen 			break;
5869505e637SHaavard Skinnemoen 		}
5879505e637SHaavard Skinnemoen 		simple_release_fs(&debugfs_mount, &debugfs_mount_count);
5889505e637SHaavard Skinnemoen 	}
5899505e637SHaavard Skinnemoen 
5909505e637SHaavard Skinnemoen 	parent = dentry->d_parent;
5919505e637SHaavard Skinnemoen 	mutex_lock(&parent->d_inode->i_mutex);
5929505e637SHaavard Skinnemoen 	__debugfs_remove(dentry, parent);
5939505e637SHaavard Skinnemoen 	mutex_unlock(&parent->d_inode->i_mutex);
5949505e637SHaavard Skinnemoen 	simple_release_fs(&debugfs_mount, &debugfs_mount_count);
5959505e637SHaavard Skinnemoen }
5969505e637SHaavard Skinnemoen EXPORT_SYMBOL_GPL(debugfs_remove_recursive);
5979505e637SHaavard Skinnemoen 
5989505e637SHaavard Skinnemoen /**
599cfc94cdfSJan Kara  * debugfs_rename - rename a file/directory in the debugfs filesystem
600cfc94cdfSJan Kara  * @old_dir: a pointer to the parent dentry for the renamed object. This
601cfc94cdfSJan Kara  *          should be a directory dentry.
602cfc94cdfSJan Kara  * @old_dentry: dentry of an object to be renamed.
603cfc94cdfSJan Kara  * @new_dir: a pointer to the parent dentry where the object should be
604cfc94cdfSJan Kara  *          moved. This should be a directory dentry.
605cfc94cdfSJan Kara  * @new_name: a pointer to a string containing the target name.
606cfc94cdfSJan Kara  *
607cfc94cdfSJan Kara  * This function renames a file/directory in debugfs.  The target must not
608cfc94cdfSJan Kara  * exist for rename to succeed.
609cfc94cdfSJan Kara  *
610cfc94cdfSJan Kara  * This function will return a pointer to old_dentry (which is updated to
611cfc94cdfSJan Kara  * reflect renaming) if it succeeds. If an error occurs, %NULL will be
612cfc94cdfSJan Kara  * returned.
613cfc94cdfSJan Kara  *
614cfc94cdfSJan Kara  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
615cfc94cdfSJan Kara  * returned.
616cfc94cdfSJan Kara  */
617cfc94cdfSJan Kara struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
618cfc94cdfSJan Kara 		struct dentry *new_dir, const char *new_name)
619cfc94cdfSJan Kara {
620cfc94cdfSJan Kara 	int error;
621cfc94cdfSJan Kara 	struct dentry *dentry = NULL, *trap;
622cfc94cdfSJan Kara 	const char *old_name;
623cfc94cdfSJan Kara 
624cfc94cdfSJan Kara 	trap = lock_rename(new_dir, old_dir);
625cfc94cdfSJan Kara 	/* Source or destination directories don't exist? */
626cfc94cdfSJan Kara 	if (!old_dir->d_inode || !new_dir->d_inode)
627cfc94cdfSJan Kara 		goto exit;
628cfc94cdfSJan Kara 	/* Source does not exist, cyclic rename, or mountpoint? */
629cfc94cdfSJan Kara 	if (!old_dentry->d_inode || old_dentry == trap ||
630cfc94cdfSJan Kara 	    d_mountpoint(old_dentry))
631cfc94cdfSJan Kara 		goto exit;
632cfc94cdfSJan Kara 	dentry = lookup_one_len(new_name, new_dir, strlen(new_name));
633cfc94cdfSJan Kara 	/* Lookup failed, cyclic rename or target exists? */
634cfc94cdfSJan Kara 	if (IS_ERR(dentry) || dentry == trap || dentry->d_inode)
635cfc94cdfSJan Kara 		goto exit;
636cfc94cdfSJan Kara 
637cfc94cdfSJan Kara 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
638cfc94cdfSJan Kara 
639cfc94cdfSJan Kara 	error = simple_rename(old_dir->d_inode, old_dentry, new_dir->d_inode,
640cfc94cdfSJan Kara 		dentry);
641cfc94cdfSJan Kara 	if (error) {
642cfc94cdfSJan Kara 		fsnotify_oldname_free(old_name);
643cfc94cdfSJan Kara 		goto exit;
644cfc94cdfSJan Kara 	}
645cfc94cdfSJan Kara 	d_move(old_dentry, dentry);
646cfc94cdfSJan Kara 	fsnotify_move(old_dir->d_inode, new_dir->d_inode, old_name,
647123df294SAl Viro 		S_ISDIR(old_dentry->d_inode->i_mode),
6485a190ae6SAl Viro 		NULL, old_dentry);
649cfc94cdfSJan Kara 	fsnotify_oldname_free(old_name);
650cfc94cdfSJan Kara 	unlock_rename(new_dir, old_dir);
651cfc94cdfSJan Kara 	dput(dentry);
652cfc94cdfSJan Kara 	return old_dentry;
653cfc94cdfSJan Kara exit:
654cfc94cdfSJan Kara 	if (dentry && !IS_ERR(dentry))
655cfc94cdfSJan Kara 		dput(dentry);
656cfc94cdfSJan Kara 	unlock_rename(new_dir, old_dir);
657cfc94cdfSJan Kara 	return NULL;
658cfc94cdfSJan Kara }
659cfc94cdfSJan Kara EXPORT_SYMBOL_GPL(debugfs_rename);
660cfc94cdfSJan Kara 
661c0f92ba9SFrederic Weisbecker /**
662c0f92ba9SFrederic Weisbecker  * debugfs_initialized - Tells whether debugfs has been registered
663c0f92ba9SFrederic Weisbecker  */
664c0f92ba9SFrederic Weisbecker bool debugfs_initialized(void)
665c0f92ba9SFrederic Weisbecker {
666c0f92ba9SFrederic Weisbecker 	return debugfs_registered;
667c0f92ba9SFrederic Weisbecker }
668c0f92ba9SFrederic Weisbecker EXPORT_SYMBOL_GPL(debugfs_initialized);
669c0f92ba9SFrederic Weisbecker 
670c0f92ba9SFrederic Weisbecker 
671191e186bSGreg Kroah-Hartman static struct kobject *debug_kobj;
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds static int __init debugfs_init(void)
6741da177e4SLinus Torvalds {
6751da177e4SLinus Torvalds 	int retval;
6761da177e4SLinus Torvalds 
6770ff21e46SGreg Kroah-Hartman 	debug_kobj = kobject_create_and_add("debug", kernel_kobj);
678191e186bSGreg Kroah-Hartman 	if (!debug_kobj)
679191e186bSGreg Kroah-Hartman 		return -EINVAL;
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 	retval = register_filesystem(&debug_fs_type);
6821da177e4SLinus Torvalds 	if (retval)
683197b12d6SGreg Kroah-Hartman 		kobject_put(debug_kobj);
684c0f92ba9SFrederic Weisbecker 	else
685c0f92ba9SFrederic Weisbecker 		debugfs_registered = true;
686c0f92ba9SFrederic Weisbecker 
6871da177e4SLinus Torvalds 	return retval;
6881da177e4SLinus Torvalds }
6891da177e4SLinus Torvalds core_initcall(debugfs_init);
6901da177e4SLinus Torvalds 
691