• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  inode.c - part of debugfs, a tiny little debug file system
3  *
4  *  Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
5  *  Copyright (C) 2004 IBM Inc.
6  *
7  *	This program is free software; you can redistribute it and/or
8  *	modify it under the terms of the GNU General Public License version
9  *	2 as published by the Free Software Foundation.
10  *
11  *  debugfs is for people to use instead of /proc or /sys.
12  *  See Documentation/DocBook/kernel-api for more details.
13  *
14  */
15 
16 #include <linux/module.h>
17 #include <linux/fs.h>
18 #include <linux/mount.h>
19 #include <linux/pagemap.h>
20 #include <linux/init.h>
21 #include <linux/kobject.h>
22 #include <linux/namei.h>
23 #include <linux/debugfs.h>
24 #include <linux/fsnotify.h>
25 #include <linux/string.h>
26 #include <linux/seq_file.h>
27 #include <linux/parser.h>
28 #include <linux/magic.h>
29 #include <linux/slab.h>
30 
31 #define DEBUGFS_DEFAULT_MODE	0700
32 
33 static struct vfsmount *debugfs_mount;
34 static int debugfs_mount_count;
35 static bool debugfs_registered;
36 
debugfs_get_inode(struct super_block * sb)37 static struct inode *debugfs_get_inode(struct super_block *sb)
38 {
39 	struct inode *inode = new_inode(sb);
40 	if (inode) {
41 		inode->i_ino = get_next_ino();
42 		inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
43 	}
44 	return inode;
45 }
46 
47 struct debugfs_mount_opts {
48 	kuid_t uid;
49 	kgid_t gid;
50 	umode_t mode;
51 };
52 
53 enum {
54 	Opt_uid,
55 	Opt_gid,
56 	Opt_mode,
57 	Opt_err
58 };
59 
60 static const match_table_t tokens = {
61 	{Opt_uid, "uid=%u"},
62 	{Opt_gid, "gid=%u"},
63 	{Opt_mode, "mode=%o"},
64 	{Opt_err, NULL}
65 };
66 
67 struct debugfs_fs_info {
68 	struct debugfs_mount_opts mount_opts;
69 };
70 
debugfs_parse_options(char * data,struct debugfs_mount_opts * opts)71 static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts)
72 {
73 	substring_t args[MAX_OPT_ARGS];
74 	int option;
75 	int token;
76 	kuid_t uid;
77 	kgid_t gid;
78 	char *p;
79 
80 	opts->mode = DEBUGFS_DEFAULT_MODE;
81 
82 	while ((p = strsep(&data, ",")) != NULL) {
83 		if (!*p)
84 			continue;
85 
86 		token = match_token(p, tokens, args);
87 		switch (token) {
88 		case Opt_uid:
89 			if (match_int(&args[0], &option))
90 				return -EINVAL;
91 			uid = make_kuid(current_user_ns(), option);
92 			if (!uid_valid(uid))
93 				return -EINVAL;
94 			opts->uid = uid;
95 			break;
96 		case Opt_gid:
97 			if (match_int(&args[0], &option))
98 				return -EINVAL;
99 			gid = make_kgid(current_user_ns(), option);
100 			if (!gid_valid(gid))
101 				return -EINVAL;
102 			opts->gid = gid;
103 			break;
104 		case Opt_mode:
105 			if (match_octal(&args[0], &option))
106 				return -EINVAL;
107 			opts->mode = option & S_IALLUGO;
108 			break;
109 		/*
110 		 * We might like to report bad mount options here;
111 		 * but traditionally debugfs has ignored all mount options
112 		 */
113 		}
114 	}
115 
116 	return 0;
117 }
118 
debugfs_apply_options(struct super_block * sb)119 static int debugfs_apply_options(struct super_block *sb)
120 {
121 	struct debugfs_fs_info *fsi = sb->s_fs_info;
122 	struct inode *inode = d_inode(sb->s_root);
123 	struct debugfs_mount_opts *opts = &fsi->mount_opts;
124 
125 	inode->i_mode &= ~S_IALLUGO;
126 	inode->i_mode |= opts->mode;
127 
128 	inode->i_uid = opts->uid;
129 	inode->i_gid = opts->gid;
130 
131 	return 0;
132 }
133 
debugfs_remount(struct super_block * sb,int * flags,char * data)134 static int debugfs_remount(struct super_block *sb, int *flags, char *data)
135 {
136 	int err;
137 	struct debugfs_fs_info *fsi = sb->s_fs_info;
138 
139 	sync_filesystem(sb);
140 	err = debugfs_parse_options(data, &fsi->mount_opts);
141 	if (err)
142 		goto fail;
143 
144 	debugfs_apply_options(sb);
145 
146 fail:
147 	return err;
148 }
149 
debugfs_show_options(struct seq_file * m,struct dentry * root)150 static int debugfs_show_options(struct seq_file *m, struct dentry *root)
151 {
152 	struct debugfs_fs_info *fsi = root->d_sb->s_fs_info;
153 	struct debugfs_mount_opts *opts = &fsi->mount_opts;
154 
155 	if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
156 		seq_printf(m, ",uid=%u",
157 			   from_kuid_munged(&init_user_ns, opts->uid));
158 	if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
159 		seq_printf(m, ",gid=%u",
160 			   from_kgid_munged(&init_user_ns, opts->gid));
161 	if (opts->mode != DEBUGFS_DEFAULT_MODE)
162 		seq_printf(m, ",mode=%o", opts->mode);
163 
164 	return 0;
165 }
166 
debugfs_i_callback(struct rcu_head * head)167 static void debugfs_i_callback(struct rcu_head *head)
168 {
169 	struct inode *inode = container_of(head, struct inode, i_rcu);
170 	if (S_ISLNK(inode->i_mode))
171 		kfree(inode->i_link);
172 	free_inode_nonrcu(inode);
173 }
174 
debugfs_destroy_inode(struct inode * inode)175 static void debugfs_destroy_inode(struct inode *inode)
176 {
177 	call_rcu(&inode->i_rcu, debugfs_i_callback);
178 }
179 
180 static const struct super_operations debugfs_super_operations = {
181 	.statfs		= simple_statfs,
182 	.remount_fs	= debugfs_remount,
183 	.show_options	= debugfs_show_options,
184 	.destroy_inode	= debugfs_destroy_inode,
185 };
186 
debugfs_automount(struct path * path)187 static struct vfsmount *debugfs_automount(struct path *path)
188 {
189 	struct vfsmount *(*f)(void *);
190 	f = (struct vfsmount *(*)(void *))path->dentry->d_fsdata;
191 	return f(d_inode(path->dentry)->i_private);
192 }
193 
194 static const struct dentry_operations debugfs_dops = {
195 	.d_delete = always_delete_dentry,
196 	.d_automount = debugfs_automount,
197 };
198 
debug_fill_super(struct super_block * sb,void * data,int silent)199 static int debug_fill_super(struct super_block *sb, void *data, int silent)
200 {
201 	static struct tree_descr debug_files[] = {{""}};
202 	struct debugfs_fs_info *fsi;
203 	int err;
204 
205 	save_mount_options(sb, data);
206 
207 	fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL);
208 	sb->s_fs_info = fsi;
209 	if (!fsi) {
210 		err = -ENOMEM;
211 		goto fail;
212 	}
213 
214 	err = debugfs_parse_options(data, &fsi->mount_opts);
215 	if (err)
216 		goto fail;
217 
218 	err  =  simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
219 	if (err)
220 		goto fail;
221 
222 	sb->s_op = &debugfs_super_operations;
223 	sb->s_d_op = &debugfs_dops;
224 
225 	debugfs_apply_options(sb);
226 
227 	return 0;
228 
229 fail:
230 	kfree(fsi);
231 	sb->s_fs_info = NULL;
232 	return err;
233 }
234 
debug_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)235 static struct dentry *debug_mount(struct file_system_type *fs_type,
236 			int flags, const char *dev_name,
237 			void *data)
238 {
239 	return mount_single(fs_type, flags, data, debug_fill_super);
240 }
241 
242 static struct file_system_type debug_fs_type = {
243 	.owner =	THIS_MODULE,
244 	.name =		"debugfs",
245 	.mount =	debug_mount,
246 	.kill_sb =	kill_litter_super,
247 };
248 MODULE_ALIAS_FS("debugfs");
249 
start_creating(const char * name,struct dentry * parent)250 static struct dentry *start_creating(const char *name, struct dentry *parent)
251 {
252 	struct dentry *dentry;
253 	int error;
254 
255 	pr_debug("debugfs: creating file '%s'\n",name);
256 
257 	if (IS_ERR(parent))
258 		return parent;
259 
260 	error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
261 			      &debugfs_mount_count);
262 	if (error)
263 		return ERR_PTR(error);
264 
265 	/* If the parent is not specified, we create it in the root.
266 	 * We need the root dentry to do this, which is in the super
267 	 * block. A pointer to that is in the struct vfsmount that we
268 	 * have around.
269 	 */
270 	if (!parent)
271 		parent = debugfs_mount->mnt_root;
272 
273 	mutex_lock(&d_inode(parent)->i_mutex);
274 	dentry = lookup_one_len(name, parent, strlen(name));
275 	if (!IS_ERR(dentry) && d_really_is_positive(dentry)) {
276 		dput(dentry);
277 		dentry = ERR_PTR(-EEXIST);
278 	}
279 
280 	if (IS_ERR(dentry)) {
281 		mutex_unlock(&d_inode(parent)->i_mutex);
282 		simple_release_fs(&debugfs_mount, &debugfs_mount_count);
283 	}
284 
285 	return dentry;
286 }
287 
failed_creating(struct dentry * dentry)288 static struct dentry *failed_creating(struct dentry *dentry)
289 {
290 	mutex_unlock(&d_inode(dentry->d_parent)->i_mutex);
291 	dput(dentry);
292 	simple_release_fs(&debugfs_mount, &debugfs_mount_count);
293 	return NULL;
294 }
295 
end_creating(struct dentry * dentry)296 static struct dentry *end_creating(struct dentry *dentry)
297 {
298 	mutex_unlock(&d_inode(dentry->d_parent)->i_mutex);
299 	return dentry;
300 }
301 
302 /**
303  * debugfs_create_file - create a file in the debugfs filesystem
304  * @name: a pointer to a string containing the name of the file to create.
305  * @mode: the permission that the file should have.
306  * @parent: a pointer to the parent dentry for this file.  This should be a
307  *          directory dentry if set.  If this parameter is NULL, then the
308  *          file will be created in the root of the debugfs filesystem.
309  * @data: a pointer to something that the caller will want to get to later
310  *        on.  The inode.i_private pointer will point to this value on
311  *        the open() call.
312  * @fops: a pointer to a struct file_operations that should be used for
313  *        this file.
314  *
315  * This is the basic "create a file" function for debugfs.  It allows for a
316  * wide range of flexibility in creating a file, or a directory (if you want
317  * to create a directory, the debugfs_create_dir() function is
318  * recommended to be used instead.)
319  *
320  * This function will return a pointer to a dentry if it succeeds.  This
321  * pointer must be passed to the debugfs_remove() function when the file is
322  * to be removed (no automatic cleanup happens if your module is unloaded,
323  * you are responsible here.)  If an error occurs, %NULL will be returned.
324  *
325  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
326  * returned.
327  */
debugfs_create_file(const char * name,umode_t mode,struct dentry * parent,void * data,const struct file_operations * fops)328 struct dentry *debugfs_create_file(const char *name, umode_t mode,
329 				   struct dentry *parent, void *data,
330 				   const struct file_operations *fops)
331 {
332 	struct dentry *dentry;
333 	struct inode *inode;
334 
335 	if (!(mode & S_IFMT))
336 		mode |= S_IFREG;
337 	BUG_ON(!S_ISREG(mode));
338 	dentry = start_creating(name, parent);
339 
340 	if (IS_ERR(dentry))
341 		return NULL;
342 
343 	inode = debugfs_get_inode(dentry->d_sb);
344 	if (unlikely(!inode))
345 		return failed_creating(dentry);
346 
347 	inode->i_mode = mode;
348 	inode->i_fop = fops ? fops : &debugfs_file_operations;
349 	inode->i_private = data;
350 	d_instantiate(dentry, inode);
351 	fsnotify_create(d_inode(dentry->d_parent), dentry);
352 	return end_creating(dentry);
353 }
354 EXPORT_SYMBOL_GPL(debugfs_create_file);
355 
356 /**
357  * debugfs_create_file_size - create a file in the debugfs filesystem
358  * @name: a pointer to a string containing the name of the file to create.
359  * @mode: the permission that the file should have.
360  * @parent: a pointer to the parent dentry for this file.  This should be a
361  *          directory dentry if set.  If this parameter is NULL, then the
362  *          file will be created in the root of the debugfs filesystem.
363  * @data: a pointer to something that the caller will want to get to later
364  *        on.  The inode.i_private pointer will point to this value on
365  *        the open() call.
366  * @fops: a pointer to a struct file_operations that should be used for
367  *        this file.
368  * @file_size: initial file size
369  *
370  * This is the basic "create a file" function for debugfs.  It allows for a
371  * wide range of flexibility in creating a file, or a directory (if you want
372  * to create a directory, the debugfs_create_dir() function is
373  * recommended to be used instead.)
374  *
375  * This function will return a pointer to a dentry if it succeeds.  This
376  * pointer must be passed to the debugfs_remove() function when the file is
377  * to be removed (no automatic cleanup happens if your module is unloaded,
378  * you are responsible here.)  If an error occurs, %NULL will be returned.
379  *
380  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
381  * returned.
382  */
debugfs_create_file_size(const char * name,umode_t mode,struct dentry * parent,void * data,const struct file_operations * fops,loff_t file_size)383 struct dentry *debugfs_create_file_size(const char *name, umode_t mode,
384 					struct dentry *parent, void *data,
385 					const struct file_operations *fops,
386 					loff_t file_size)
387 {
388 	struct dentry *de = debugfs_create_file(name, mode, parent, data, fops);
389 
390 	if (de)
391 		d_inode(de)->i_size = file_size;
392 	return de;
393 }
394 EXPORT_SYMBOL_GPL(debugfs_create_file_size);
395 
396 /**
397  * debugfs_create_dir - create a directory in the debugfs filesystem
398  * @name: a pointer to a string containing the name of the directory to
399  *        create.
400  * @parent: a pointer to the parent dentry for this file.  This should be a
401  *          directory dentry if set.  If this parameter is NULL, then the
402  *          directory will be created in the root of the debugfs filesystem.
403  *
404  * This function creates a directory in debugfs with the given name.
405  *
406  * This function will return a pointer to a dentry if it succeeds.  This
407  * pointer must be passed to the debugfs_remove() function when the file is
408  * to be removed (no automatic cleanup happens if your module is unloaded,
409  * you are responsible here.)  If an error occurs, %NULL will be returned.
410  *
411  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
412  * returned.
413  */
debugfs_create_dir(const char * name,struct dentry * parent)414 struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
415 {
416 	struct dentry *dentry = start_creating(name, parent);
417 	struct inode *inode;
418 
419 	if (IS_ERR(dentry))
420 		return NULL;
421 
422 	inode = debugfs_get_inode(dentry->d_sb);
423 	if (unlikely(!inode))
424 		return failed_creating(dentry);
425 
426 	inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
427 	inode->i_op = &simple_dir_inode_operations;
428 	inode->i_fop = &simple_dir_operations;
429 
430 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
431 	inc_nlink(inode);
432 	d_instantiate(dentry, inode);
433 	inc_nlink(d_inode(dentry->d_parent));
434 	fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
435 	return end_creating(dentry);
436 }
437 EXPORT_SYMBOL_GPL(debugfs_create_dir);
438 
439 /**
440  * debugfs_create_automount - create automount point in the debugfs filesystem
441  * @name: a pointer to a string containing the name of the file to create.
442  * @parent: a pointer to the parent dentry for this file.  This should be a
443  *          directory dentry if set.  If this parameter is NULL, then the
444  *          file will be created in the root of the debugfs filesystem.
445  * @f: function to be called when pathname resolution steps on that one.
446  * @data: opaque argument to pass to f().
447  *
448  * @f should return what ->d_automount() would.
449  */
debugfs_create_automount(const char * name,struct dentry * parent,struct vfsmount * (* f)(void *),void * data)450 struct dentry *debugfs_create_automount(const char *name,
451 					struct dentry *parent,
452 					struct vfsmount *(*f)(void *),
453 					void *data)
454 {
455 	struct dentry *dentry = start_creating(name, parent);
456 	struct inode *inode;
457 
458 	if (IS_ERR(dentry))
459 		return NULL;
460 
461 	inode = debugfs_get_inode(dentry->d_sb);
462 	if (unlikely(!inode))
463 		return failed_creating(dentry);
464 
465 	make_empty_dir_inode(inode);
466 	inode->i_flags |= S_AUTOMOUNT;
467 	inode->i_private = data;
468 	dentry->d_fsdata = (void *)f;
469 	d_instantiate(dentry, inode);
470 	return end_creating(dentry);
471 }
472 EXPORT_SYMBOL(debugfs_create_automount);
473 
474 /**
475  * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
476  * @name: a pointer to a string containing the name of the symbolic link to
477  *        create.
478  * @parent: a pointer to the parent dentry for this symbolic link.  This
479  *          should be a directory dentry if set.  If this parameter is NULL,
480  *          then the symbolic link will be created in the root of the debugfs
481  *          filesystem.
482  * @target: a pointer to a string containing the path to the target of the
483  *          symbolic link.
484  *
485  * This function creates a symbolic link with the given name in debugfs that
486  * links to the given target path.
487  *
488  * This function will return a pointer to a dentry if it succeeds.  This
489  * pointer must be passed to the debugfs_remove() function when the symbolic
490  * link is to be removed (no automatic cleanup happens if your module is
491  * unloaded, you are responsible here.)  If an error occurs, %NULL will be
492  * returned.
493  *
494  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
495  * returned.
496  */
debugfs_create_symlink(const char * name,struct dentry * parent,const char * target)497 struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
498 				      const char *target)
499 {
500 	struct dentry *dentry;
501 	struct inode *inode;
502 	char *link = kstrdup(target, GFP_KERNEL);
503 	if (!link)
504 		return NULL;
505 
506 	dentry = start_creating(name, parent);
507 	if (IS_ERR(dentry)) {
508 		kfree(link);
509 		return NULL;
510 	}
511 
512 	inode = debugfs_get_inode(dentry->d_sb);
513 	if (unlikely(!inode)) {
514 		kfree(link);
515 		return failed_creating(dentry);
516 	}
517 	inode->i_mode = S_IFLNK | S_IRWXUGO;
518 	inode->i_op = &simple_symlink_inode_operations;
519 	inode->i_link = link;
520 	d_instantiate(dentry, inode);
521 	return end_creating(dentry);
522 }
523 EXPORT_SYMBOL_GPL(debugfs_create_symlink);
524 
__debugfs_remove(struct dentry * dentry,struct dentry * parent)525 static int __debugfs_remove(struct dentry *dentry, struct dentry *parent)
526 {
527 	int ret = 0;
528 
529 	if (simple_positive(dentry)) {
530 		dget(dentry);
531 		if (d_is_dir(dentry))
532 			ret = simple_rmdir(d_inode(parent), dentry);
533 		else
534 			simple_unlink(d_inode(parent), dentry);
535 		if (!ret)
536 			d_delete(dentry);
537 		dput(dentry);
538 	}
539 	return ret;
540 }
541 
542 /**
543  * debugfs_remove - removes a file or directory from the debugfs filesystem
544  * @dentry: a pointer to a the dentry of the file or directory to be
545  *          removed.  If this parameter is NULL or an error value, nothing
546  *          will be done.
547  *
548  * This function removes a file or directory in debugfs that was previously
549  * created with a call to another debugfs function (like
550  * debugfs_create_file() or variants thereof.)
551  *
552  * This function is required to be called in order for the file to be
553  * removed, no automatic cleanup of files will happen when a module is
554  * removed, you are responsible here.
555  */
debugfs_remove(struct dentry * dentry)556 void debugfs_remove(struct dentry *dentry)
557 {
558 	struct dentry *parent;
559 	int ret;
560 
561 	if (IS_ERR_OR_NULL(dentry))
562 		return;
563 
564 	parent = dentry->d_parent;
565 	if (!parent || d_really_is_negative(parent))
566 		return;
567 
568 	mutex_lock(&d_inode(parent)->i_mutex);
569 	ret = __debugfs_remove(dentry, parent);
570 	mutex_unlock(&d_inode(parent)->i_mutex);
571 	if (!ret)
572 		simple_release_fs(&debugfs_mount, &debugfs_mount_count);
573 }
574 EXPORT_SYMBOL_GPL(debugfs_remove);
575 
576 /**
577  * debugfs_remove_recursive - recursively removes a directory
578  * @dentry: a pointer to a the dentry of the directory to be removed.  If this
579  *          parameter is NULL or an error value, nothing will be done.
580  *
581  * This function recursively removes a directory tree in debugfs that
582  * was previously created with a call to another debugfs function
583  * (like debugfs_create_file() or variants thereof.)
584  *
585  * This function is required to be called in order for the file to be
586  * removed, no automatic cleanup of files will happen when a module is
587  * removed, you are responsible here.
588  */
debugfs_remove_recursive(struct dentry * dentry)589 void debugfs_remove_recursive(struct dentry *dentry)
590 {
591 	struct dentry *child, *parent;
592 
593 	if (IS_ERR_OR_NULL(dentry))
594 		return;
595 
596 	parent = dentry->d_parent;
597 	if (!parent || d_really_is_negative(parent))
598 		return;
599 
600 	parent = dentry;
601  down:
602 	mutex_lock(&d_inode(parent)->i_mutex);
603  loop:
604 	/*
605 	 * The parent->d_subdirs is protected by the d_lock. Outside that
606 	 * lock, the child can be unlinked and set to be freed which can
607 	 * use the d_u.d_child as the rcu head and corrupt this list.
608 	 */
609 	spin_lock(&parent->d_lock);
610 	list_for_each_entry(child, &parent->d_subdirs, d_child) {
611 		if (!simple_positive(child))
612 			continue;
613 
614 		/* perhaps simple_empty(child) makes more sense */
615 		if (!list_empty(&child->d_subdirs)) {
616 			spin_unlock(&parent->d_lock);
617 			mutex_unlock(&d_inode(parent)->i_mutex);
618 			parent = child;
619 			goto down;
620 		}
621 
622 		spin_unlock(&parent->d_lock);
623 
624 		if (!__debugfs_remove(child, parent))
625 			simple_release_fs(&debugfs_mount, &debugfs_mount_count);
626 
627 		/*
628 		 * The parent->d_lock protects agaist child from unlinking
629 		 * from d_subdirs. When releasing the parent->d_lock we can
630 		 * no longer trust that the next pointer is valid.
631 		 * Restart the loop. We'll skip this one with the
632 		 * simple_positive() check.
633 		 */
634 		goto loop;
635 	}
636 	spin_unlock(&parent->d_lock);
637 
638 	mutex_unlock(&d_inode(parent)->i_mutex);
639 	child = parent;
640 	parent = parent->d_parent;
641 	mutex_lock(&d_inode(parent)->i_mutex);
642 
643 	if (child != dentry)
644 		/* go up */
645 		goto loop;
646 
647 	if (!__debugfs_remove(child, parent))
648 		simple_release_fs(&debugfs_mount, &debugfs_mount_count);
649 	mutex_unlock(&d_inode(parent)->i_mutex);
650 }
651 EXPORT_SYMBOL_GPL(debugfs_remove_recursive);
652 
653 /**
654  * debugfs_rename - rename a file/directory in the debugfs filesystem
655  * @old_dir: a pointer to the parent dentry for the renamed object. This
656  *          should be a directory dentry.
657  * @old_dentry: dentry of an object to be renamed.
658  * @new_dir: a pointer to the parent dentry where the object should be
659  *          moved. This should be a directory dentry.
660  * @new_name: a pointer to a string containing the target name.
661  *
662  * This function renames a file/directory in debugfs.  The target must not
663  * exist for rename to succeed.
664  *
665  * This function will return a pointer to old_dentry (which is updated to
666  * reflect renaming) if it succeeds. If an error occurs, %NULL will be
667  * returned.
668  *
669  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
670  * returned.
671  */
debugfs_rename(struct dentry * old_dir,struct dentry * old_dentry,struct dentry * new_dir,const char * new_name)672 struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
673 		struct dentry *new_dir, const char *new_name)
674 {
675 	int error;
676 	struct dentry *dentry = NULL, *trap;
677 	struct name_snapshot old_name;
678 
679 	if (IS_ERR(old_dir))
680 		return old_dir;
681 	if (IS_ERR(new_dir))
682 		return new_dir;
683 	if (IS_ERR_OR_NULL(old_dentry))
684 		return old_dentry;
685 
686 	trap = lock_rename(new_dir, old_dir);
687 	/* Source or destination directories don't exist? */
688 	if (d_really_is_negative(old_dir) || d_really_is_negative(new_dir))
689 		goto exit;
690 	/* Source does not exist, cyclic rename, or mountpoint? */
691 	if (d_really_is_negative(old_dentry) || old_dentry == trap ||
692 	    d_mountpoint(old_dentry))
693 		goto exit;
694 	dentry = lookup_one_len(new_name, new_dir, strlen(new_name));
695 	/* Lookup failed, cyclic rename or target exists? */
696 	if (IS_ERR(dentry) || dentry == trap || d_really_is_positive(dentry))
697 		goto exit;
698 
699 	take_dentry_name_snapshot(&old_name, old_dentry);
700 
701 	error = simple_rename(d_inode(old_dir), old_dentry, d_inode(new_dir),
702 		dentry);
703 	if (error) {
704 		release_dentry_name_snapshot(&old_name);
705 		goto exit;
706 	}
707 	d_move(old_dentry, dentry);
708 	fsnotify_move(d_inode(old_dir), d_inode(new_dir), old_name.name,
709 		d_is_dir(old_dentry),
710 		NULL, old_dentry);
711 	release_dentry_name_snapshot(&old_name);
712 	unlock_rename(new_dir, old_dir);
713 	dput(dentry);
714 	return old_dentry;
715 exit:
716 	if (dentry && !IS_ERR(dentry))
717 		dput(dentry);
718 	unlock_rename(new_dir, old_dir);
719 	return NULL;
720 }
721 EXPORT_SYMBOL_GPL(debugfs_rename);
722 
723 /**
724  * debugfs_initialized - Tells whether debugfs has been registered
725  */
debugfs_initialized(void)726 bool debugfs_initialized(void)
727 {
728 	return debugfs_registered;
729 }
730 EXPORT_SYMBOL_GPL(debugfs_initialized);
731 
debugfs_init(void)732 static int __init debugfs_init(void)
733 {
734 	int retval;
735 
736 	retval = sysfs_create_mount_point(kernel_kobj, "debug");
737 	if (retval)
738 		return retval;
739 
740 	retval = register_filesystem(&debug_fs_type);
741 	if (retval)
742 		sysfs_remove_mount_point(kernel_kobj, "debug");
743 	else
744 		debugfs_registered = true;
745 
746 	return retval;
747 }
748 core_initcall(debugfs_init);
749 
750