xref: /OK3568_Linux_fs/kernel/fs/debugfs/inode.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  inode.c - part of debugfs, a tiny little debug file system
4  *
5  *  Copyright (C) 2004,2019 Greg Kroah-Hartman <greg@kroah.com>
6  *  Copyright (C) 2004 IBM Inc.
7  *  Copyright (C) 2019 Linux Foundation <gregkh@linuxfoundation.org>
8  *
9  *  debugfs is for people to use instead of /proc or /sys.
10  *  See ./Documentation/core-api/kernel-api.rst for more details.
11  */
12 
13 #define pr_fmt(fmt)	"debugfs: " fmt
14 
15 #include <linux/module.h>
16 #include <linux/fs.h>
17 #include <linux/mount.h>
18 #include <linux/pagemap.h>
19 #include <linux/init.h>
20 #include <linux/kobject.h>
21 #include <linux/namei.h>
22 #include <linux/debugfs.h>
23 #include <linux/fsnotify.h>
24 #include <linux/string.h>
25 #include <linux/seq_file.h>
26 #include <linux/parser.h>
27 #include <linux/magic.h>
28 #include <linux/slab.h>
29 #include <linux/security.h>
30 
31 #include "internal.h"
32 
33 #define DEBUGFS_DEFAULT_MODE	0700
34 
35 static struct vfsmount *debugfs_mount;
36 static int debugfs_mount_count;
37 static bool debugfs_registered;
38 static unsigned int debugfs_allow __ro_after_init = DEFAULT_DEBUGFS_ALLOW_BITS;
39 
40 /*
41  * Don't allow access attributes to be changed whilst the kernel is locked down
42  * so that we can use the file mode as part of a heuristic to determine whether
43  * to lock down individual files.
44  */
debugfs_setattr(struct dentry * dentry,struct iattr * ia)45 static int debugfs_setattr(struct dentry *dentry, struct iattr *ia)
46 {
47 	int ret = security_locked_down(LOCKDOWN_DEBUGFS);
48 
49 	if (ret && (ia->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)))
50 		return ret;
51 	return simple_setattr(dentry, ia);
52 }
53 
54 static const struct inode_operations debugfs_file_inode_operations = {
55 	.setattr	= debugfs_setattr,
56 };
57 static const struct inode_operations debugfs_dir_inode_operations = {
58 	.lookup		= simple_lookup,
59 	.setattr	= debugfs_setattr,
60 };
61 static const struct inode_operations debugfs_symlink_inode_operations = {
62 	.get_link	= simple_get_link,
63 	.setattr	= debugfs_setattr,
64 };
65 
debugfs_get_inode(struct super_block * sb)66 static struct inode *debugfs_get_inode(struct super_block *sb)
67 {
68 	struct inode *inode = new_inode(sb);
69 	if (inode) {
70 		inode->i_ino = get_next_ino();
71 		inode->i_atime = inode->i_mtime =
72 			inode->i_ctime = current_time(inode);
73 	}
74 	return inode;
75 }
76 
77 struct debugfs_mount_opts {
78 	kuid_t uid;
79 	kgid_t gid;
80 	umode_t mode;
81 };
82 
83 enum {
84 	Opt_uid,
85 	Opt_gid,
86 	Opt_mode,
87 	Opt_err
88 };
89 
90 static const match_table_t tokens = {
91 	{Opt_uid, "uid=%u"},
92 	{Opt_gid, "gid=%u"},
93 	{Opt_mode, "mode=%o"},
94 	{Opt_err, NULL}
95 };
96 
97 struct debugfs_fs_info {
98 	struct debugfs_mount_opts mount_opts;
99 };
100 
debugfs_parse_options(char * data,struct debugfs_mount_opts * opts)101 static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts)
102 {
103 	substring_t args[MAX_OPT_ARGS];
104 	int option;
105 	int token;
106 	kuid_t uid;
107 	kgid_t gid;
108 	char *p;
109 
110 	opts->mode = DEBUGFS_DEFAULT_MODE;
111 
112 	while ((p = strsep(&data, ",")) != NULL) {
113 		if (!*p)
114 			continue;
115 
116 		token = match_token(p, tokens, args);
117 		switch (token) {
118 		case Opt_uid:
119 			if (match_int(&args[0], &option))
120 				return -EINVAL;
121 			uid = make_kuid(current_user_ns(), option);
122 			if (!uid_valid(uid))
123 				return -EINVAL;
124 			opts->uid = uid;
125 			break;
126 		case Opt_gid:
127 			if (match_int(&args[0], &option))
128 				return -EINVAL;
129 			gid = make_kgid(current_user_ns(), option);
130 			if (!gid_valid(gid))
131 				return -EINVAL;
132 			opts->gid = gid;
133 			break;
134 		case Opt_mode:
135 			if (match_octal(&args[0], &option))
136 				return -EINVAL;
137 			opts->mode = option & S_IALLUGO;
138 			break;
139 		/*
140 		 * We might like to report bad mount options here;
141 		 * but traditionally debugfs has ignored all mount options
142 		 */
143 		}
144 	}
145 
146 	return 0;
147 }
148 
debugfs_apply_options(struct super_block * sb)149 static int debugfs_apply_options(struct super_block *sb)
150 {
151 	struct debugfs_fs_info *fsi = sb->s_fs_info;
152 	struct inode *inode = d_inode(sb->s_root);
153 	struct debugfs_mount_opts *opts = &fsi->mount_opts;
154 
155 	inode->i_mode &= ~S_IALLUGO;
156 	inode->i_mode |= opts->mode;
157 
158 	inode->i_uid = opts->uid;
159 	inode->i_gid = opts->gid;
160 
161 	return 0;
162 }
163 
debugfs_remount(struct super_block * sb,int * flags,char * data)164 static int debugfs_remount(struct super_block *sb, int *flags, char *data)
165 {
166 	int err;
167 	struct debugfs_fs_info *fsi = sb->s_fs_info;
168 
169 	sync_filesystem(sb);
170 	err = debugfs_parse_options(data, &fsi->mount_opts);
171 	if (err)
172 		goto fail;
173 
174 	debugfs_apply_options(sb);
175 
176 fail:
177 	return err;
178 }
179 
debugfs_show_options(struct seq_file * m,struct dentry * root)180 static int debugfs_show_options(struct seq_file *m, struct dentry *root)
181 {
182 	struct debugfs_fs_info *fsi = root->d_sb->s_fs_info;
183 	struct debugfs_mount_opts *opts = &fsi->mount_opts;
184 
185 	if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
186 		seq_printf(m, ",uid=%u",
187 			   from_kuid_munged(&init_user_ns, opts->uid));
188 	if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
189 		seq_printf(m, ",gid=%u",
190 			   from_kgid_munged(&init_user_ns, opts->gid));
191 	if (opts->mode != DEBUGFS_DEFAULT_MODE)
192 		seq_printf(m, ",mode=%o", opts->mode);
193 
194 	return 0;
195 }
196 
debugfs_free_inode(struct inode * inode)197 static void debugfs_free_inode(struct inode *inode)
198 {
199 	if (S_ISLNK(inode->i_mode))
200 		kfree(inode->i_link);
201 	free_inode_nonrcu(inode);
202 }
203 
204 static const struct super_operations debugfs_super_operations = {
205 	.statfs		= simple_statfs,
206 	.remount_fs	= debugfs_remount,
207 	.show_options	= debugfs_show_options,
208 	.free_inode	= debugfs_free_inode,
209 };
210 
debugfs_release_dentry(struct dentry * dentry)211 static void debugfs_release_dentry(struct dentry *dentry)
212 {
213 	void *fsd = dentry->d_fsdata;
214 
215 	if (!((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT))
216 		kfree(dentry->d_fsdata);
217 }
218 
debugfs_automount(struct path * path)219 static struct vfsmount *debugfs_automount(struct path *path)
220 {
221 	debugfs_automount_t f;
222 	f = (debugfs_automount_t)path->dentry->d_fsdata;
223 	return f(path->dentry, d_inode(path->dentry)->i_private);
224 }
225 
226 static const struct dentry_operations debugfs_dops = {
227 	.d_delete = always_delete_dentry,
228 	.d_release = debugfs_release_dentry,
229 	.d_automount = debugfs_automount,
230 };
231 
debug_fill_super(struct super_block * sb,void * data,int silent)232 static int debug_fill_super(struct super_block *sb, void *data, int silent)
233 {
234 	static const struct tree_descr debug_files[] = {{""}};
235 	struct debugfs_fs_info *fsi;
236 	int err;
237 
238 	fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL);
239 	sb->s_fs_info = fsi;
240 	if (!fsi) {
241 		err = -ENOMEM;
242 		goto fail;
243 	}
244 
245 	err = debugfs_parse_options(data, &fsi->mount_opts);
246 	if (err)
247 		goto fail;
248 
249 	err  =  simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
250 	if (err)
251 		goto fail;
252 
253 	sb->s_op = &debugfs_super_operations;
254 	sb->s_d_op = &debugfs_dops;
255 
256 	debugfs_apply_options(sb);
257 
258 	return 0;
259 
260 fail:
261 	kfree(fsi);
262 	sb->s_fs_info = NULL;
263 	return err;
264 }
265 
debug_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)266 static struct dentry *debug_mount(struct file_system_type *fs_type,
267 			int flags, const char *dev_name,
268 			void *data)
269 {
270 	if (!(debugfs_allow & DEBUGFS_ALLOW_API))
271 		return ERR_PTR(-EPERM);
272 
273 	return mount_single(fs_type, flags, data, debug_fill_super);
274 }
275 
276 static struct file_system_type debug_fs_type = {
277 	.owner =	THIS_MODULE,
278 	.name =		"debugfs",
279 	.mount =	debug_mount,
280 	.kill_sb =	kill_litter_super,
281 };
282 MODULE_ALIAS_FS("debugfs");
283 
284 /**
285  * debugfs_lookup() - look up an existing debugfs file
286  * @name: a pointer to a string containing the name of the file to look up.
287  * @parent: a pointer to the parent dentry of the file.
288  *
289  * This function will return a pointer to a dentry if it succeeds.  If the file
290  * doesn't exist or an error occurs, %NULL will be returned.  The returned
291  * dentry must be passed to dput() when it is no longer needed.
292  *
293  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
294  * returned.
295  */
debugfs_lookup(const char * name,struct dentry * parent)296 struct dentry *debugfs_lookup(const char *name, struct dentry *parent)
297 {
298 	struct dentry *dentry;
299 
300 	if (!debugfs_initialized() || IS_ERR_OR_NULL(name) || IS_ERR(parent))
301 		return NULL;
302 
303 	if (!parent)
304 		parent = debugfs_mount->mnt_root;
305 
306 	dentry = lookup_positive_unlocked(name, parent, strlen(name));
307 	if (IS_ERR(dentry))
308 		return NULL;
309 	return dentry;
310 }
311 EXPORT_SYMBOL_GPL(debugfs_lookup);
312 
start_creating(const char * name,struct dentry * parent)313 static struct dentry *start_creating(const char *name, struct dentry *parent)
314 {
315 	struct dentry *dentry;
316 	int error;
317 
318 	if (!(debugfs_allow & DEBUGFS_ALLOW_API))
319 		return ERR_PTR(-EPERM);
320 
321 	if (!debugfs_initialized())
322 		return ERR_PTR(-ENOENT);
323 
324 	pr_debug("creating file '%s'\n", name);
325 
326 	if (IS_ERR(parent))
327 		return parent;
328 
329 	error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
330 			      &debugfs_mount_count);
331 	if (error) {
332 		pr_err("Unable to pin filesystem for file '%s'\n", name);
333 		return ERR_PTR(error);
334 	}
335 
336 	/* If the parent is not specified, we create it in the root.
337 	 * We need the root dentry to do this, which is in the super
338 	 * block. A pointer to that is in the struct vfsmount that we
339 	 * have around.
340 	 */
341 	if (!parent)
342 		parent = debugfs_mount->mnt_root;
343 
344 	inode_lock(d_inode(parent));
345 	if (unlikely(IS_DEADDIR(d_inode(parent))))
346 		dentry = ERR_PTR(-ENOENT);
347 	else
348 		dentry = lookup_one_len(name, parent, strlen(name));
349 	if (!IS_ERR(dentry) && d_really_is_positive(dentry)) {
350 		if (d_is_dir(dentry))
351 			pr_err("Directory '%s' with parent '%s' already present!\n",
352 			       name, parent->d_name.name);
353 		else
354 			pr_err("File '%s' in directory '%s' already present!\n",
355 			       name, parent->d_name.name);
356 		dput(dentry);
357 		dentry = ERR_PTR(-EEXIST);
358 	}
359 
360 	if (IS_ERR(dentry)) {
361 		inode_unlock(d_inode(parent));
362 		simple_release_fs(&debugfs_mount, &debugfs_mount_count);
363 	}
364 
365 	return dentry;
366 }
367 
failed_creating(struct dentry * dentry)368 static struct dentry *failed_creating(struct dentry *dentry)
369 {
370 	inode_unlock(d_inode(dentry->d_parent));
371 	dput(dentry);
372 	simple_release_fs(&debugfs_mount, &debugfs_mount_count);
373 	return ERR_PTR(-ENOMEM);
374 }
375 
end_creating(struct dentry * dentry)376 static struct dentry *end_creating(struct dentry *dentry)
377 {
378 	inode_unlock(d_inode(dentry->d_parent));
379 	return dentry;
380 }
381 
__debugfs_create_file(const char * name,umode_t mode,struct dentry * parent,void * data,const struct file_operations * proxy_fops,const struct file_operations * real_fops)382 static struct dentry *__debugfs_create_file(const char *name, umode_t mode,
383 				struct dentry *parent, void *data,
384 				const struct file_operations *proxy_fops,
385 				const struct file_operations *real_fops)
386 {
387 	struct dentry *dentry;
388 	struct inode *inode;
389 
390 	if (!(mode & S_IFMT))
391 		mode |= S_IFREG;
392 	BUG_ON(!S_ISREG(mode));
393 	dentry = start_creating(name, parent);
394 
395 	if (IS_ERR(dentry))
396 		return dentry;
397 
398 	if (!(debugfs_allow & DEBUGFS_ALLOW_API)) {
399 		failed_creating(dentry);
400 		return ERR_PTR(-EPERM);
401 	}
402 
403 	inode = debugfs_get_inode(dentry->d_sb);
404 	if (unlikely(!inode)) {
405 		pr_err("out of free dentries, can not create file '%s'\n",
406 		       name);
407 		return failed_creating(dentry);
408 	}
409 
410 	inode->i_mode = mode;
411 	inode->i_private = data;
412 
413 	inode->i_op = &debugfs_file_inode_operations;
414 	inode->i_fop = proxy_fops;
415 	dentry->d_fsdata = (void *)((unsigned long)real_fops |
416 				DEBUGFS_FSDATA_IS_REAL_FOPS_BIT);
417 
418 	d_instantiate(dentry, inode);
419 	fsnotify_create(d_inode(dentry->d_parent), dentry);
420 	return end_creating(dentry);
421 }
422 
423 /**
424  * debugfs_create_file - create a file in the debugfs filesystem
425  * @name: a pointer to a string containing the name of the file to create.
426  * @mode: the permission that the file should have.
427  * @parent: a pointer to the parent dentry for this file.  This should be a
428  *          directory dentry if set.  If this parameter is NULL, then the
429  *          file will be created in the root of the debugfs filesystem.
430  * @data: a pointer to something that the caller will want to get to later
431  *        on.  The inode.i_private pointer will point to this value on
432  *        the open() call.
433  * @fops: a pointer to a struct file_operations that should be used for
434  *        this file.
435  *
436  * This is the basic "create a file" function for debugfs.  It allows for a
437  * wide range of flexibility in creating a file, or a directory (if you want
438  * to create a directory, the debugfs_create_dir() function is
439  * recommended to be used instead.)
440  *
441  * This function will return a pointer to a dentry if it succeeds.  This
442  * pointer must be passed to the debugfs_remove() function when the file is
443  * to be removed (no automatic cleanup happens if your module is unloaded,
444  * you are responsible here.)  If an error occurs, ERR_PTR(-ERROR) will be
445  * returned.
446  *
447  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
448  * returned.
449  */
debugfs_create_file(const char * name,umode_t mode,struct dentry * parent,void * data,const struct file_operations * fops)450 struct dentry *debugfs_create_file(const char *name, umode_t mode,
451 				   struct dentry *parent, void *data,
452 				   const struct file_operations *fops)
453 {
454 
455 	return __debugfs_create_file(name, mode, parent, data,
456 				fops ? &debugfs_full_proxy_file_operations :
457 					&debugfs_noop_file_operations,
458 				fops);
459 }
460 EXPORT_SYMBOL_GPL(debugfs_create_file);
461 
462 /**
463  * debugfs_create_file_unsafe - create a file in the debugfs filesystem
464  * @name: a pointer to a string containing the name of the file to create.
465  * @mode: the permission that the file should have.
466  * @parent: a pointer to the parent dentry for this file.  This should be a
467  *          directory dentry if set.  If this parameter is NULL, then the
468  *          file will be created in the root of the debugfs filesystem.
469  * @data: a pointer to something that the caller will want to get to later
470  *        on.  The inode.i_private pointer will point to this value on
471  *        the open() call.
472  * @fops: a pointer to a struct file_operations that should be used for
473  *        this file.
474  *
475  * debugfs_create_file_unsafe() is completely analogous to
476  * debugfs_create_file(), the only difference being that the fops
477  * handed it will not get protected against file removals by the
478  * debugfs core.
479  *
480  * It is your responsibility to protect your struct file_operation
481  * methods against file removals by means of debugfs_file_get()
482  * and debugfs_file_put(). ->open() is still protected by
483  * debugfs though.
484  *
485  * Any struct file_operations defined by means of
486  * DEFINE_DEBUGFS_ATTRIBUTE() is protected against file removals and
487  * thus, may be used here.
488  */
debugfs_create_file_unsafe(const char * name,umode_t mode,struct dentry * parent,void * data,const struct file_operations * fops)489 struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
490 				   struct dentry *parent, void *data,
491 				   const struct file_operations *fops)
492 {
493 
494 	return __debugfs_create_file(name, mode, parent, data,
495 				fops ? &debugfs_open_proxy_file_operations :
496 					&debugfs_noop_file_operations,
497 				fops);
498 }
499 EXPORT_SYMBOL_GPL(debugfs_create_file_unsafe);
500 
501 /**
502  * debugfs_create_file_size - create a file in the debugfs filesystem
503  * @name: a pointer to a string containing the name of the file to create.
504  * @mode: the permission that the file should have.
505  * @parent: a pointer to the parent dentry for this file.  This should be a
506  *          directory dentry if set.  If this parameter is NULL, then the
507  *          file will be created in the root of the debugfs filesystem.
508  * @data: a pointer to something that the caller will want to get to later
509  *        on.  The inode.i_private pointer will point to this value on
510  *        the open() call.
511  * @fops: a pointer to a struct file_operations that should be used for
512  *        this file.
513  * @file_size: initial file size
514  *
515  * This is the basic "create a file" function for debugfs.  It allows for a
516  * wide range of flexibility in creating a file, or a directory (if you want
517  * to create a directory, the debugfs_create_dir() function is
518  * recommended to be used instead.)
519  */
debugfs_create_file_size(const char * name,umode_t mode,struct dentry * parent,void * data,const struct file_operations * fops,loff_t file_size)520 void debugfs_create_file_size(const char *name, umode_t mode,
521 			      struct dentry *parent, void *data,
522 			      const struct file_operations *fops,
523 			      loff_t file_size)
524 {
525 	struct dentry *de = debugfs_create_file(name, mode, parent, data, fops);
526 
527 	if (!IS_ERR(de))
528 		d_inode(de)->i_size = file_size;
529 }
530 EXPORT_SYMBOL_GPL(debugfs_create_file_size);
531 
532 /**
533  * debugfs_create_dir - create a directory in the debugfs filesystem
534  * @name: a pointer to a string containing the name of the directory to
535  *        create.
536  * @parent: a pointer to the parent dentry for this file.  This should be a
537  *          directory dentry if set.  If this parameter is NULL, then the
538  *          directory will be created in the root of the debugfs filesystem.
539  *
540  * This function creates a directory in debugfs with the given name.
541  *
542  * This function will return a pointer to a dentry if it succeeds.  This
543  * pointer must be passed to the debugfs_remove() function when the file is
544  * to be removed (no automatic cleanup happens if your module is unloaded,
545  * you are responsible here.)  If an error occurs, ERR_PTR(-ERROR) will be
546  * returned.
547  *
548  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
549  * returned.
550  */
debugfs_create_dir(const char * name,struct dentry * parent)551 struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
552 {
553 	struct dentry *dentry = start_creating(name, parent);
554 	struct inode *inode;
555 
556 	if (IS_ERR(dentry))
557 		return dentry;
558 
559 	if (!(debugfs_allow & DEBUGFS_ALLOW_API)) {
560 		failed_creating(dentry);
561 		return ERR_PTR(-EPERM);
562 	}
563 
564 	inode = debugfs_get_inode(dentry->d_sb);
565 	if (unlikely(!inode)) {
566 		pr_err("out of free dentries, can not create directory '%s'\n",
567 		       name);
568 		return failed_creating(dentry);
569 	}
570 
571 	inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
572 	inode->i_op = &debugfs_dir_inode_operations;
573 	inode->i_fop = &simple_dir_operations;
574 
575 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
576 	inc_nlink(inode);
577 	d_instantiate(dentry, inode);
578 	inc_nlink(d_inode(dentry->d_parent));
579 	fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
580 	return end_creating(dentry);
581 }
582 EXPORT_SYMBOL_GPL(debugfs_create_dir);
583 
584 /**
585  * debugfs_create_automount - create automount point in the debugfs filesystem
586  * @name: a pointer to a string containing the name of the file to create.
587  * @parent: a pointer to the parent dentry for this file.  This should be a
588  *          directory dentry if set.  If this parameter is NULL, then the
589  *          file will be created in the root of the debugfs filesystem.
590  * @f: function to be called when pathname resolution steps on that one.
591  * @data: opaque argument to pass to f().
592  *
593  * @f should return what ->d_automount() would.
594  */
debugfs_create_automount(const char * name,struct dentry * parent,debugfs_automount_t f,void * data)595 struct dentry *debugfs_create_automount(const char *name,
596 					struct dentry *parent,
597 					debugfs_automount_t f,
598 					void *data)
599 {
600 	struct dentry *dentry = start_creating(name, parent);
601 	struct inode *inode;
602 
603 	if (IS_ERR(dentry))
604 		return dentry;
605 
606 	if (!(debugfs_allow & DEBUGFS_ALLOW_API)) {
607 		failed_creating(dentry);
608 		return ERR_PTR(-EPERM);
609 	}
610 
611 	inode = debugfs_get_inode(dentry->d_sb);
612 	if (unlikely(!inode)) {
613 		pr_err("out of free dentries, can not create automount '%s'\n",
614 		       name);
615 		return failed_creating(dentry);
616 	}
617 
618 	make_empty_dir_inode(inode);
619 	inode->i_flags |= S_AUTOMOUNT;
620 	inode->i_private = data;
621 	dentry->d_fsdata = (void *)f;
622 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
623 	inc_nlink(inode);
624 	d_instantiate(dentry, inode);
625 	inc_nlink(d_inode(dentry->d_parent));
626 	fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
627 	return end_creating(dentry);
628 }
629 EXPORT_SYMBOL(debugfs_create_automount);
630 
631 /**
632  * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
633  * @name: a pointer to a string containing the name of the symbolic link to
634  *        create.
635  * @parent: a pointer to the parent dentry for this symbolic link.  This
636  *          should be a directory dentry if set.  If this parameter is NULL,
637  *          then the symbolic link will be created in the root of the debugfs
638  *          filesystem.
639  * @target: a pointer to a string containing the path to the target of the
640  *          symbolic link.
641  *
642  * This function creates a symbolic link with the given name in debugfs that
643  * links to the given target path.
644  *
645  * This function will return a pointer to a dentry if it succeeds.  This
646  * pointer must be passed to the debugfs_remove() function when the symbolic
647  * link is to be removed (no automatic cleanup happens if your module is
648  * unloaded, you are responsible here.)  If an error occurs, ERR_PTR(-ERROR)
649  * will be returned.
650  *
651  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
652  * returned.
653  */
debugfs_create_symlink(const char * name,struct dentry * parent,const char * target)654 struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
655 				      const char *target)
656 {
657 	struct dentry *dentry;
658 	struct inode *inode;
659 	char *link = kstrdup(target, GFP_KERNEL);
660 	if (!link)
661 		return ERR_PTR(-ENOMEM);
662 
663 	dentry = start_creating(name, parent);
664 	if (IS_ERR(dentry)) {
665 		kfree(link);
666 		return dentry;
667 	}
668 
669 	inode = debugfs_get_inode(dentry->d_sb);
670 	if (unlikely(!inode)) {
671 		pr_err("out of free dentries, can not create symlink '%s'\n",
672 		       name);
673 		kfree(link);
674 		return failed_creating(dentry);
675 	}
676 	inode->i_mode = S_IFLNK | S_IRWXUGO;
677 	inode->i_op = &debugfs_symlink_inode_operations;
678 	inode->i_link = link;
679 	d_instantiate(dentry, inode);
680 	return end_creating(dentry);
681 }
682 EXPORT_SYMBOL_GPL(debugfs_create_symlink);
683 
__debugfs_file_removed(struct dentry * dentry)684 static void __debugfs_file_removed(struct dentry *dentry)
685 {
686 	struct debugfs_fsdata *fsd;
687 
688 	/*
689 	 * Paired with the closing smp_mb() implied by a successful
690 	 * cmpxchg() in debugfs_file_get(): either
691 	 * debugfs_file_get() must see a dead dentry or we must see a
692 	 * debugfs_fsdata instance at ->d_fsdata here (or both).
693 	 */
694 	smp_mb();
695 	fsd = READ_ONCE(dentry->d_fsdata);
696 	if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)
697 		return;
698 	if (!refcount_dec_and_test(&fsd->active_users))
699 		wait_for_completion(&fsd->active_users_drained);
700 }
701 
remove_one(struct dentry * victim)702 static void remove_one(struct dentry *victim)
703 {
704         if (d_is_reg(victim))
705 		__debugfs_file_removed(victim);
706 	simple_release_fs(&debugfs_mount, &debugfs_mount_count);
707 }
708 
709 /**
710  * debugfs_remove - recursively removes a directory
711  * @dentry: a pointer to a the dentry of the directory to be removed.  If this
712  *          parameter is NULL or an error value, nothing will be done.
713  *
714  * This function recursively removes a directory tree in debugfs that
715  * was previously created with a call to another debugfs function
716  * (like debugfs_create_file() or variants thereof.)
717  *
718  * This function is required to be called in order for the file to be
719  * removed, no automatic cleanup of files will happen when a module is
720  * removed, you are responsible here.
721  */
debugfs_remove(struct dentry * dentry)722 void debugfs_remove(struct dentry *dentry)
723 {
724 	if (IS_ERR_OR_NULL(dentry))
725 		return;
726 
727 	simple_pin_fs(&debug_fs_type, &debugfs_mount, &debugfs_mount_count);
728 	simple_recursive_removal(dentry, remove_one);
729 	simple_release_fs(&debugfs_mount, &debugfs_mount_count);
730 }
731 EXPORT_SYMBOL_GPL(debugfs_remove);
732 
733 /**
734  * debugfs_lookup_and_remove - lookup a directory or file and recursively remove it
735  * @name: a pointer to a string containing the name of the item to look up.
736  * @parent: a pointer to the parent dentry of the item.
737  *
738  * This is the equlivant of doing something like
739  * debugfs_remove(debugfs_lookup(..)) but with the proper reference counting
740  * handled for the directory being looked up.
741  */
debugfs_lookup_and_remove(const char * name,struct dentry * parent)742 void debugfs_lookup_and_remove(const char *name, struct dentry *parent)
743 {
744 	struct dentry *dentry;
745 
746 	dentry = debugfs_lookup(name, parent);
747 	if (!dentry)
748 		return;
749 
750 	debugfs_remove(dentry);
751 	dput(dentry);
752 }
753 EXPORT_SYMBOL_GPL(debugfs_lookup_and_remove);
754 
755 /**
756  * debugfs_rename - rename a file/directory in the debugfs filesystem
757  * @old_dir: a pointer to the parent dentry for the renamed object. This
758  *          should be a directory dentry.
759  * @old_dentry: dentry of an object to be renamed.
760  * @new_dir: a pointer to the parent dentry where the object should be
761  *          moved. This should be a directory dentry.
762  * @new_name: a pointer to a string containing the target name.
763  *
764  * This function renames a file/directory in debugfs.  The target must not
765  * exist for rename to succeed.
766  *
767  * This function will return a pointer to old_dentry (which is updated to
768  * reflect renaming) if it succeeds. If an error occurs, %NULL will be
769  * returned.
770  *
771  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
772  * returned.
773  */
debugfs_rename(struct dentry * old_dir,struct dentry * old_dentry,struct dentry * new_dir,const char * new_name)774 struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
775 		struct dentry *new_dir, const char *new_name)
776 {
777 	int error;
778 	struct dentry *dentry = NULL, *trap;
779 	struct name_snapshot old_name;
780 
781 	if (IS_ERR(old_dir))
782 		return old_dir;
783 	if (IS_ERR(new_dir))
784 		return new_dir;
785 	if (IS_ERR_OR_NULL(old_dentry))
786 		return old_dentry;
787 
788 	trap = lock_rename(new_dir, old_dir);
789 	/* Source or destination directories don't exist? */
790 	if (d_really_is_negative(old_dir) || d_really_is_negative(new_dir))
791 		goto exit;
792 	/* Source does not exist, cyclic rename, or mountpoint? */
793 	if (d_really_is_negative(old_dentry) || old_dentry == trap ||
794 	    d_mountpoint(old_dentry))
795 		goto exit;
796 	dentry = lookup_one_len(new_name, new_dir, strlen(new_name));
797 	/* Lookup failed, cyclic rename or target exists? */
798 	if (IS_ERR(dentry) || dentry == trap || d_really_is_positive(dentry))
799 		goto exit;
800 
801 	take_dentry_name_snapshot(&old_name, old_dentry);
802 
803 	error = simple_rename(d_inode(old_dir), old_dentry, d_inode(new_dir),
804 			      dentry, 0);
805 	if (error) {
806 		release_dentry_name_snapshot(&old_name);
807 		goto exit;
808 	}
809 	d_move(old_dentry, dentry);
810 	fsnotify_move(d_inode(old_dir), d_inode(new_dir), &old_name.name,
811 		d_is_dir(old_dentry),
812 		NULL, old_dentry);
813 	release_dentry_name_snapshot(&old_name);
814 	unlock_rename(new_dir, old_dir);
815 	dput(dentry);
816 	return old_dentry;
817 exit:
818 	if (dentry && !IS_ERR(dentry))
819 		dput(dentry);
820 	unlock_rename(new_dir, old_dir);
821 	if (IS_ERR(dentry))
822 		return dentry;
823 	return ERR_PTR(-EINVAL);
824 }
825 EXPORT_SYMBOL_GPL(debugfs_rename);
826 
827 /**
828  * debugfs_initialized - Tells whether debugfs has been registered
829  */
debugfs_initialized(void)830 bool debugfs_initialized(void)
831 {
832 	return debugfs_registered;
833 }
834 EXPORT_SYMBOL_GPL(debugfs_initialized);
835 
debugfs_kernel(char * str)836 static int __init debugfs_kernel(char *str)
837 {
838 	if (str) {
839 		if (!strcmp(str, "on"))
840 			debugfs_allow = DEBUGFS_ALLOW_API | DEBUGFS_ALLOW_MOUNT;
841 		else if (!strcmp(str, "no-mount"))
842 			debugfs_allow = DEBUGFS_ALLOW_API;
843 		else if (!strcmp(str, "off"))
844 			debugfs_allow = 0;
845 	}
846 
847 	return 0;
848 }
849 early_param("debugfs", debugfs_kernel);
debugfs_init(void)850 static int __init debugfs_init(void)
851 {
852 	int retval;
853 
854 	if (!(debugfs_allow & DEBUGFS_ALLOW_MOUNT))
855 		return -EPERM;
856 
857 	retval = sysfs_create_mount_point(kernel_kobj, "debug");
858 	if (retval)
859 		return retval;
860 
861 	retval = register_filesystem(&debug_fs_type);
862 	if (retval)
863 		sysfs_remove_mount_point(kernel_kobj, "debug");
864 	else
865 		debugfs_registered = true;
866 
867 	return retval;
868 }
869 core_initcall(debugfs_init);
870