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