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 
debugfs_positive(struct dentry * dentry)47 static inline int debugfs_positive(struct dentry *dentry)
48 {
49 	return d_really_is_positive(dentry) && !d_unhashed(dentry);
50 }
51 
52 struct debugfs_mount_opts {
53 	kuid_t uid;
54 	kgid_t gid;
55 	umode_t mode;
56 };
57 
58 enum {
59 	Opt_uid,
60 	Opt_gid,
61 	Opt_mode,
62 	Opt_err
63 };
64 
65 static const match_table_t tokens = {
66 	{Opt_uid, "uid=%u"},
67 	{Opt_gid, "gid=%u"},
68 	{Opt_mode, "mode=%o"},
69 	{Opt_err, NULL}
70 };
71 
72 struct debugfs_fs_info {
73 	struct debugfs_mount_opts mount_opts;
74 };
75 
debugfs_parse_options(char * data,struct debugfs_mount_opts * opts)76 static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts)
77 {
78 	substring_t args[MAX_OPT_ARGS];
79 	int option;
80 	int token;
81 	kuid_t uid;
82 	kgid_t gid;
83 	char *p;
84 
85 	opts->mode = DEBUGFS_DEFAULT_MODE;
86 
87 	while ((p = strsep(&data, ",")) != NULL) {
88 		if (!*p)
89 			continue;
90 
91 		token = match_token(p, tokens, args);
92 		switch (token) {
93 		case Opt_uid:
94 			if (match_int(&args[0], &option))
95 				return -EINVAL;
96 			uid = make_kuid(current_user_ns(), option);
97 			if (!uid_valid(uid))
98 				return -EINVAL;
99 			opts->uid = uid;
100 			break;
101 		case Opt_gid:
102 			if (match_int(&args[0], &option))
103 				return -EINVAL;
104 			gid = make_kgid(current_user_ns(), option);
105 			if (!gid_valid(gid))
106 				return -EINVAL;
107 			opts->gid = gid;
108 			break;
109 		case Opt_mode:
110 			if (match_octal(&args[0], &option))
111 				return -EINVAL;
112 			opts->mode = option & S_IALLUGO;
113 			break;
114 		/*
115 		 * We might like to report bad mount options here;
116 		 * but traditionally debugfs has ignored all mount options
117 		 */
118 		}
119 	}
120 
121 	return 0;
122 }
123 
debugfs_apply_options(struct super_block * sb)124 static int debugfs_apply_options(struct super_block *sb)
125 {
126 	struct debugfs_fs_info *fsi = sb->s_fs_info;
127 	struct inode *inode = d_inode(sb->s_root);
128 	struct debugfs_mount_opts *opts = &fsi->mount_opts;
129 
130 	inode->i_mode &= ~S_IALLUGO;
131 	inode->i_mode |= opts->mode;
132 
133 	inode->i_uid = opts->uid;
134 	inode->i_gid = opts->gid;
135 
136 	return 0;
137 }
138 
debugfs_remount(struct super_block * sb,int * flags,char * data)139 static int debugfs_remount(struct super_block *sb, int *flags, char *data)
140 {
141 	int err;
142 	struct debugfs_fs_info *fsi = sb->s_fs_info;
143 
144 	sync_filesystem(sb);
145 	err = debugfs_parse_options(data, &fsi->mount_opts);
146 	if (err)
147 		goto fail;
148 
149 	debugfs_apply_options(sb);
150 
151 fail:
152 	return err;
153 }
154 
debugfs_show_options(struct seq_file * m,struct dentry * root)155 static int debugfs_show_options(struct seq_file *m, struct dentry *root)
156 {
157 	struct debugfs_fs_info *fsi = root->d_sb->s_fs_info;
158 	struct debugfs_mount_opts *opts = &fsi->mount_opts;
159 
160 	if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
161 		seq_printf(m, ",uid=%u",
162 			   from_kuid_munged(&init_user_ns, opts->uid));
163 	if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
164 		seq_printf(m, ",gid=%u",
165 			   from_kgid_munged(&init_user_ns, opts->gid));
166 	if (opts->mode != DEBUGFS_DEFAULT_MODE)
167 		seq_printf(m, ",mode=%o", opts->mode);
168 
169 	return 0;
170 }
171 
debugfs_evict_inode(struct inode * inode)172 static void debugfs_evict_inode(struct inode *inode)
173 {
174 	truncate_inode_pages_final(&inode->i_data);
175 	clear_inode(inode);
176 	if (S_ISLNK(inode->i_mode))
177 		kfree(inode->i_private);
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 	.evict_inode	= debugfs_evict_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 = &debugfs_link_operations;
519 	inode->i_private = 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 (debugfs_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.
546  *
547  * This function removes a file or directory in debugfs that was previously
548  * created with a call to another debugfs function (like
549  * debugfs_create_file() or variants thereof.)
550  *
551  * This function is required to be called in order for the file to be
552  * removed, no automatic cleanup of files will happen when a module is
553  * removed, you are responsible here.
554  */
debugfs_remove(struct dentry * dentry)555 void debugfs_remove(struct dentry *dentry)
556 {
557 	struct dentry *parent;
558 	int ret;
559 
560 	if (IS_ERR_OR_NULL(dentry))
561 		return;
562 
563 	parent = dentry->d_parent;
564 	if (!parent || d_really_is_negative(parent))
565 		return;
566 
567 	mutex_lock(&d_inode(parent)->i_mutex);
568 	ret = __debugfs_remove(dentry, parent);
569 	mutex_unlock(&d_inode(parent)->i_mutex);
570 	if (!ret)
571 		simple_release_fs(&debugfs_mount, &debugfs_mount_count);
572 }
573 EXPORT_SYMBOL_GPL(debugfs_remove);
574 
575 /**
576  * debugfs_remove_recursive - recursively removes a directory
577  * @dentry: a pointer to a the dentry of the directory to be removed.
578  *
579  * This function recursively removes a directory tree in debugfs that
580  * was previously created with a call to another debugfs function
581  * (like debugfs_create_file() or variants thereof.)
582  *
583  * This function is required to be called in order for the file to be
584  * removed, no automatic cleanup of files will happen when a module is
585  * removed, you are responsible here.
586  */
debugfs_remove_recursive(struct dentry * dentry)587 void debugfs_remove_recursive(struct dentry *dentry)
588 {
589 	struct dentry *child, *parent;
590 
591 	if (IS_ERR_OR_NULL(dentry))
592 		return;
593 
594 	parent = dentry->d_parent;
595 	if (!parent || d_really_is_negative(parent))
596 		return;
597 
598 	parent = dentry;
599  down:
600 	mutex_lock(&d_inode(parent)->i_mutex);
601  loop:
602 	/*
603 	 * The parent->d_subdirs is protected by the d_lock. Outside that
604 	 * lock, the child can be unlinked and set to be freed which can
605 	 * use the d_u.d_child as the rcu head and corrupt this list.
606 	 */
607 	spin_lock(&parent->d_lock);
608 	list_for_each_entry(child, &parent->d_subdirs, d_child) {
609 		if (!debugfs_positive(child))
610 			continue;
611 
612 		/* perhaps simple_empty(child) makes more sense */
613 		if (!list_empty(&child->d_subdirs)) {
614 			spin_unlock(&parent->d_lock);
615 			mutex_unlock(&d_inode(parent)->i_mutex);
616 			parent = child;
617 			goto down;
618 		}
619 
620 		spin_unlock(&parent->d_lock);
621 
622 		if (!__debugfs_remove(child, parent))
623 			simple_release_fs(&debugfs_mount, &debugfs_mount_count);
624 
625 		/*
626 		 * The parent->d_lock protects agaist child from unlinking
627 		 * from d_subdirs. When releasing the parent->d_lock we can
628 		 * no longer trust that the next pointer is valid.
629 		 * Restart the loop. We'll skip this one with the
630 		 * debugfs_positive() check.
631 		 */
632 		goto loop;
633 	}
634 	spin_unlock(&parent->d_lock);
635 
636 	mutex_unlock(&d_inode(parent)->i_mutex);
637 	child = parent;
638 	parent = parent->d_parent;
639 	mutex_lock(&d_inode(parent)->i_mutex);
640 
641 	if (child != dentry)
642 		/* go up */
643 		goto loop;
644 
645 	if (!__debugfs_remove(child, parent))
646 		simple_release_fs(&debugfs_mount, &debugfs_mount_count);
647 	mutex_unlock(&d_inode(parent)->i_mutex);
648 }
649 EXPORT_SYMBOL_GPL(debugfs_remove_recursive);
650 
651 /**
652  * debugfs_rename - rename a file/directory in the debugfs filesystem
653  * @old_dir: a pointer to the parent dentry for the renamed object. This
654  *          should be a directory dentry.
655  * @old_dentry: dentry of an object to be renamed.
656  * @new_dir: a pointer to the parent dentry where the object should be
657  *          moved. This should be a directory dentry.
658  * @new_name: a pointer to a string containing the target name.
659  *
660  * This function renames a file/directory in debugfs.  The target must not
661  * exist for rename to succeed.
662  *
663  * This function will return a pointer to old_dentry (which is updated to
664  * reflect renaming) if it succeeds. If an error occurs, %NULL will be
665  * returned.
666  *
667  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
668  * returned.
669  */
debugfs_rename(struct dentry * old_dir,struct dentry * old_dentry,struct dentry * new_dir,const char * new_name)670 struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
671 		struct dentry *new_dir, const char *new_name)
672 {
673 	int error;
674 	struct dentry *dentry = NULL, *trap;
675 	const char *old_name;
676 
677 	trap = lock_rename(new_dir, old_dir);
678 	/* Source or destination directories don't exist? */
679 	if (d_really_is_negative(old_dir) || d_really_is_negative(new_dir))
680 		goto exit;
681 	/* Source does not exist, cyclic rename, or mountpoint? */
682 	if (d_really_is_negative(old_dentry) || old_dentry == trap ||
683 	    d_mountpoint(old_dentry))
684 		goto exit;
685 	dentry = lookup_one_len(new_name, new_dir, strlen(new_name));
686 	/* Lookup failed, cyclic rename or target exists? */
687 	if (IS_ERR(dentry) || dentry == trap || d_really_is_positive(dentry))
688 		goto exit;
689 
690 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
691 
692 	error = simple_rename(d_inode(old_dir), old_dentry, d_inode(new_dir),
693 		dentry);
694 	if (error) {
695 		fsnotify_oldname_free(old_name);
696 		goto exit;
697 	}
698 	d_move(old_dentry, dentry);
699 	fsnotify_move(d_inode(old_dir), d_inode(new_dir), old_name,
700 		d_is_dir(old_dentry),
701 		NULL, old_dentry);
702 	fsnotify_oldname_free(old_name);
703 	unlock_rename(new_dir, old_dir);
704 	dput(dentry);
705 	return old_dentry;
706 exit:
707 	if (dentry && !IS_ERR(dentry))
708 		dput(dentry);
709 	unlock_rename(new_dir, old_dir);
710 	return NULL;
711 }
712 EXPORT_SYMBOL_GPL(debugfs_rename);
713 
714 /**
715  * debugfs_initialized - Tells whether debugfs has been registered
716  */
debugfs_initialized(void)717 bool debugfs_initialized(void)
718 {
719 	return debugfs_registered;
720 }
721 EXPORT_SYMBOL_GPL(debugfs_initialized);
722 
debugfs_init(void)723 static int __init debugfs_init(void)
724 {
725 	int retval;
726 
727 	retval = sysfs_create_mount_point(kernel_kobj, "debug");
728 	if (retval)
729 		return retval;
730 
731 	retval = register_filesystem(&debug_fs_type);
732 	if (retval)
733 		sysfs_remove_mount_point(kernel_kobj, "debug");
734 	else
735 		debugfs_registered = true;
736 
737 	return retval;
738 }
739 core_initcall(debugfs_init);
740 
741