blob: c69e00d69ff10100e1f61e8100f7fd53c1e224d7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Harry Weibd33d122011-07-16 16:45:13 +08002 * inode.c - part of debugfs, a tiny little debug file system
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/fs.h>
18#include <linux/mount.h>
19#include <linux/pagemap.h>
20#include <linux/init.h>
Randy Dunlap4d8ebdd2006-11-25 11:09:26 -080021#include <linux/kobject.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/namei.h>
23#include <linux/debugfs.h>
Mathieu Desnoyers4f365572006-11-24 13:45:37 -050024#include <linux/fsnotify.h>
Peter Oberparleiter66f54962007-02-13 12:13:54 +010025#include <linux/string.h>
Ludwig Nusseld6e48682012-01-25 11:52:28 +010026#include <linux/seq_file.h>
27#include <linux/parser.h>
Mimi Zohar92562922008-10-07 14:00:12 -040028#include <linux/magic.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Kees Cook82aceae2012-08-27 13:32:15 -070031#define DEBUGFS_DEFAULT_MODE 0700
Ludwig Nusseld6e48682012-01-25 11:52:28 +010032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033static struct vfsmount *debugfs_mount;
34static int debugfs_mount_count;
Frederic Weisbeckerc0f92ba2009-03-22 23:10:44 +010035static bool debugfs_registered;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Al Virof4ae40a2011-07-24 04:33:43 -040037static struct inode *debugfs_get_inode(struct super_block *sb, umode_t mode, dev_t dev,
Mathieu Desnoyersd3a3b0a2009-11-17 14:40:26 -080038 void *data, const struct file_operations *fops)
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
41 struct inode *inode = new_inode(sb);
42
43 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -040044 inode->i_ino = get_next_ino();
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 inode->i_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
47 switch (mode & S_IFMT) {
48 default:
49 init_special_inode(inode, mode, dev);
50 break;
51 case S_IFREG:
Mathieu Desnoyersd3a3b0a2009-11-17 14:40:26 -080052 inode->i_fop = fops ? fops : &debugfs_file_operations;
53 inode->i_private = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 break;
Peter Oberparleiter66f54962007-02-13 12:13:54 +010055 case S_IFLNK:
56 inode->i_op = &debugfs_link_operations;
Mathieu Desnoyersd3a3b0a2009-11-17 14:40:26 -080057 inode->i_private = data;
Peter Oberparleiter66f54962007-02-13 12:13:54 +010058 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 case S_IFDIR:
60 inode->i_op = &simple_dir_inode_operations;
Al Viroac481d62012-06-09 20:40:20 -040061 inode->i_fop = &simple_dir_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Mathieu Desnoyersbafb2322006-11-24 13:46:30 -050063 /* directory inodes start off with i_nlink == 2
64 * (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -070065 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 break;
67 }
68 }
Rahul Bedarkar88e412e2014-06-06 23:12:04 +053069 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
72/* SMP-safe */
73static int debugfs_mknod(struct inode *dir, struct dentry *dentry,
Al Virof4ae40a2011-07-24 04:33:43 -040074 umode_t mode, dev_t dev, void *data,
Mathieu Desnoyersd3a3b0a2009-11-17 14:40:26 -080075 const struct file_operations *fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Jens Axboe71601e2b2006-06-08 10:26:39 +020077 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 int error = -EPERM;
79
80 if (dentry->d_inode)
81 return -EEXIST;
82
Mathieu Desnoyersd3a3b0a2009-11-17 14:40:26 -080083 inode = debugfs_get_inode(dir->i_sb, mode, dev, data, fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (inode) {
85 d_instantiate(dentry, inode);
86 dget(dentry);
87 error = 0;
88 }
89 return error;
90}
91
Al Viroe09ddf32015-01-25 13:50:23 -050092static int debugfs_mkdir(struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Al Viroe09ddf32015-01-25 13:50:23 -050094 struct inode *dir = dentry->d_parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 int res;
96
97 mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
Al Viroac481d62012-06-09 20:40:20 -040098 res = debugfs_mknod(dir, dentry, mode, 0, NULL, NULL);
Mathieu Desnoyers4f365572006-11-24 13:45:37 -050099 if (!res) {
Dave Hansend8c76e62006-09-30 23:29:04 -0700100 inc_nlink(dir);
Mathieu Desnoyers4f365572006-11-24 13:45:37 -0500101 fsnotify_mkdir(dir, dentry);
102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return res;
104}
105
Al Viroe09ddf32015-01-25 13:50:23 -0500106static int debugfs_create(struct dentry *dentry, umode_t mode,
Mathieu Desnoyersd3a3b0a2009-11-17 14:40:26 -0800107 void *data, const struct file_operations *fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Al Viroe09ddf32015-01-25 13:50:23 -0500109 struct inode *dir = dentry->d_parent->d_inode;
Mathieu Desnoyers4f365572006-11-24 13:45:37 -0500110 int res;
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 mode = (mode & S_IALLUGO) | S_IFREG;
Mathieu Desnoyersd3a3b0a2009-11-17 14:40:26 -0800113 res = debugfs_mknod(dir, dentry, mode, 0, data, fops);
Mathieu Desnoyers4f365572006-11-24 13:45:37 -0500114 if (!res)
115 fsnotify_create(dir, dentry);
116 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119static inline int debugfs_positive(struct dentry *dentry)
120{
121 return dentry->d_inode && !d_unhashed(dentry);
122}
123
Ludwig Nusseld6e48682012-01-25 11:52:28 +0100124struct debugfs_mount_opts {
Eric W. Biederman7dc05882012-04-03 14:01:31 -0700125 kuid_t uid;
126 kgid_t gid;
Ludwig Nusseld6e48682012-01-25 11:52:28 +0100127 umode_t mode;
128};
129
130enum {
131 Opt_uid,
132 Opt_gid,
133 Opt_mode,
134 Opt_err
135};
136
137static const match_table_t tokens = {
138 {Opt_uid, "uid=%u"},
139 {Opt_gid, "gid=%u"},
140 {Opt_mode, "mode=%o"},
141 {Opt_err, NULL}
142};
143
144struct debugfs_fs_info {
145 struct debugfs_mount_opts mount_opts;
146};
147
148static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts)
149{
150 substring_t args[MAX_OPT_ARGS];
151 int option;
152 int token;
Eric W. Biederman7dc05882012-04-03 14:01:31 -0700153 kuid_t uid;
154 kgid_t gid;
Ludwig Nusseld6e48682012-01-25 11:52:28 +0100155 char *p;
156
157 opts->mode = DEBUGFS_DEFAULT_MODE;
158
159 while ((p = strsep(&data, ",")) != NULL) {
160 if (!*p)
161 continue;
162
163 token = match_token(p, tokens, args);
164 switch (token) {
165 case Opt_uid:
166 if (match_int(&args[0], &option))
167 return -EINVAL;
Eric W. Biederman7dc05882012-04-03 14:01:31 -0700168 uid = make_kuid(current_user_ns(), option);
169 if (!uid_valid(uid))
170 return -EINVAL;
171 opts->uid = uid;
Ludwig Nusseld6e48682012-01-25 11:52:28 +0100172 break;
173 case Opt_gid:
Dave Reisnerf1688e02013-01-02 08:54:37 -0500174 if (match_int(&args[0], &option))
Ludwig Nusseld6e48682012-01-25 11:52:28 +0100175 return -EINVAL;
Eric W. Biederman7dc05882012-04-03 14:01:31 -0700176 gid = make_kgid(current_user_ns(), option);
177 if (!gid_valid(gid))
178 return -EINVAL;
179 opts->gid = gid;
Ludwig Nusseld6e48682012-01-25 11:52:28 +0100180 break;
181 case Opt_mode:
182 if (match_octal(&args[0], &option))
183 return -EINVAL;
184 opts->mode = option & S_IALLUGO;
185 break;
186 /*
187 * We might like to report bad mount options here;
188 * but traditionally debugfs has ignored all mount options
189 */
190 }
191 }
192
193 return 0;
194}
195
196static int debugfs_apply_options(struct super_block *sb)
197{
198 struct debugfs_fs_info *fsi = sb->s_fs_info;
199 struct inode *inode = sb->s_root->d_inode;
200 struct debugfs_mount_opts *opts = &fsi->mount_opts;
201
202 inode->i_mode &= ~S_IALLUGO;
203 inode->i_mode |= opts->mode;
204
205 inode->i_uid = opts->uid;
206 inode->i_gid = opts->gid;
207
208 return 0;
209}
210
211static int debugfs_remount(struct super_block *sb, int *flags, char *data)
212{
213 int err;
214 struct debugfs_fs_info *fsi = sb->s_fs_info;
215
Theodore Ts'o02b99842014-03-13 10:14:33 -0400216 sync_filesystem(sb);
Ludwig Nusseld6e48682012-01-25 11:52:28 +0100217 err = debugfs_parse_options(data, &fsi->mount_opts);
218 if (err)
219 goto fail;
220
221 debugfs_apply_options(sb);
222
223fail:
224 return err;
225}
226
227static int debugfs_show_options(struct seq_file *m, struct dentry *root)
228{
229 struct debugfs_fs_info *fsi = root->d_sb->s_fs_info;
230 struct debugfs_mount_opts *opts = &fsi->mount_opts;
231
Eric W. Biederman7dc05882012-04-03 14:01:31 -0700232 if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
233 seq_printf(m, ",uid=%u",
234 from_kuid_munged(&init_user_ns, opts->uid));
235 if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
236 seq_printf(m, ",gid=%u",
237 from_kgid_munged(&init_user_ns, opts->gid));
Ludwig Nusseld6e48682012-01-25 11:52:28 +0100238 if (opts->mode != DEBUGFS_DEFAULT_MODE)
239 seq_printf(m, ",mode=%o", opts->mode);
240
241 return 0;
242}
243
244static const struct super_operations debugfs_super_operations = {
245 .statfs = simple_statfs,
246 .remount_fs = debugfs_remount,
247 .show_options = debugfs_show_options,
248};
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250static int debug_fill_super(struct super_block *sb, void *data, int silent)
251{
252 static struct tree_descr debug_files[] = {{""}};
Ludwig Nusseld6e48682012-01-25 11:52:28 +0100253 struct debugfs_fs_info *fsi;
254 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Ludwig Nusseld6e48682012-01-25 11:52:28 +0100256 save_mount_options(sb, data);
257
258 fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL);
259 sb->s_fs_info = fsi;
260 if (!fsi) {
261 err = -ENOMEM;
262 goto fail;
263 }
264
265 err = debugfs_parse_options(data, &fsi->mount_opts);
266 if (err)
267 goto fail;
268
269 err = simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
270 if (err)
271 goto fail;
272
273 sb->s_op = &debugfs_super_operations;
274
275 debugfs_apply_options(sb);
276
277 return 0;
278
279fail:
280 kfree(fsi);
281 sb->s_fs_info = NULL;
282 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
Al Virofc14f2f2010-07-25 01:48:30 +0400285static struct dentry *debug_mount(struct file_system_type *fs_type,
David Howells454e2392006-06-23 02:02:57 -0700286 int flags, const char *dev_name,
Al Virofc14f2f2010-07-25 01:48:30 +0400287 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Al Virofc14f2f2010-07-25 01:48:30 +0400289 return mount_single(fs_type, flags, data, debug_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
292static struct file_system_type debug_fs_type = {
293 .owner = THIS_MODULE,
294 .name = "debugfs",
Al Virofc14f2f2010-07-25 01:48:30 +0400295 .mount = debug_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 .kill_sb = kill_litter_super,
297};
Eric W. Biederman7f78e032013-03-02 19:39:14 -0800298MODULE_ALIAS_FS("debugfs");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Al Viro190afd82015-01-25 13:55:55 -0500300static struct dentry *start_creating(const char *name, struct dentry *parent)
Al Viroc3b1a352012-06-09 20:28:22 -0400301{
Al Viro190afd82015-01-25 13:55:55 -0500302 struct dentry *dentry;
Al Viroc3b1a352012-06-09 20:28:22 -0400303 int error;
304
305 pr_debug("debugfs: creating file '%s'\n",name);
306
307 error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
308 &debugfs_mount_count);
309 if (error)
Al Viro190afd82015-01-25 13:55:55 -0500310 return ERR_PTR(error);
Al Viroc3b1a352012-06-09 20:28:22 -0400311
Al Virocfa57c12012-06-09 20:33:28 -0400312 /* If the parent is not specified, we create it in the root.
Rahul Bedarkar88e412e2014-06-06 23:12:04 +0530313 * We need the root dentry to do this, which is in the super
Al Virocfa57c12012-06-09 20:33:28 -0400314 * block. A pointer to that is in the struct vfsmount that we
315 * have around.
316 */
317 if (!parent)
318 parent = debugfs_mount->mnt_root;
319
Al Virocfa57c12012-06-09 20:33:28 -0400320 mutex_lock(&parent->d_inode->i_mutex);
321 dentry = lookup_one_len(name, parent, strlen(name));
Al Viro190afd82015-01-25 13:55:55 -0500322 if (!IS_ERR(dentry) && dentry->d_inode) {
Al Virocfa57c12012-06-09 20:33:28 -0400323 dput(dentry);
Al Viro190afd82015-01-25 13:55:55 -0500324 dentry = ERR_PTR(-EEXIST);
325 }
326 if (IS_ERR(dentry))
327 mutex_unlock(&parent->d_inode->i_mutex);
328 return dentry;
329}
330
331static struct dentry *end_creating(struct dentry *dentry, int error)
332{
333 mutex_unlock(&dentry->d_parent->d_inode->i_mutex);
334 dput(dentry);
Al Virocfa57c12012-06-09 20:33:28 -0400335
Al Viroc3b1a352012-06-09 20:28:22 -0400336 if (error) {
337 dentry = NULL;
338 simple_release_fs(&debugfs_mount, &debugfs_mount_count);
339 }
Al Viroc3b1a352012-06-09 20:28:22 -0400340 return dentry;
341}
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343/**
344 * debugfs_create_file - create a file in the debugfs filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 * @name: a pointer to a string containing the name of the file to create.
Alberto Bertoglibe030e62009-10-31 18:26:52 -0300346 * @mode: the permission that the file should have.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 * @parent: a pointer to the parent dentry for this file. This should be a
Masanari Iidae2278672014-02-18 22:54:36 +0900348 * directory dentry if set. If this parameter is NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 * file will be created in the root of the debugfs filesystem.
350 * @data: a pointer to something that the caller will want to get to later
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700351 * on. The inode.i_private pointer will point to this value on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 * the open() call.
353 * @fops: a pointer to a struct file_operations that should be used for
354 * this file.
355 *
356 * This is the basic "create a file" function for debugfs. It allows for a
Alberto Bertoglibe030e62009-10-31 18:26:52 -0300357 * wide range of flexibility in creating a file, or a directory (if you want
358 * to create a directory, the debugfs_create_dir() function is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 * recommended to be used instead.)
360 *
361 * This function will return a pointer to a dentry if it succeeds. This
362 * pointer must be passed to the debugfs_remove() function when the file is
363 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700364 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700366 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Cornelia Huck873760f2007-02-14 07:57:47 +0100367 * returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 */
Al Virof4ae40a2011-07-24 04:33:43 -0400369struct dentry *debugfs_create_file(const char *name, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 struct dentry *parent, void *data,
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800371 const struct file_operations *fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Al Viroad5abd52015-01-25 14:02:31 -0500373 struct dentry *dentry;
374 int error;
Al Viroc3b1a352012-06-09 20:28:22 -0400375
Al Viroad5abd52015-01-25 14:02:31 -0500376 if (!(mode & S_IFMT))
377 mode |= S_IFREG;
378 BUG_ON(!S_ISREG(mode));
379 dentry = start_creating(name, parent);
380
381 if (IS_ERR(dentry))
382 return NULL;
383
384 error = debugfs_create(dentry, mode, data, fops);
385 return end_creating(dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387EXPORT_SYMBOL_GPL(debugfs_create_file);
388
389/**
390 * debugfs_create_dir - create a directory in the debugfs filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 * @name: a pointer to a string containing the name of the directory to
392 * create.
393 * @parent: a pointer to the parent dentry for this file. This should be a
Masanari Iidae2278672014-02-18 22:54:36 +0900394 * directory dentry if set. If this parameter is NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 * directory will be created in the root of the debugfs filesystem.
396 *
397 * This function creates a directory in debugfs with the given name.
398 *
399 * This function will return a pointer to a dentry if it succeeds. This
400 * pointer must be passed to the debugfs_remove() function when the file is
401 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700402 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700404 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Cornelia Huck873760f2007-02-14 07:57:47 +0100405 * returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 */
407struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
408{
Al Viroad5abd52015-01-25 14:02:31 -0500409 struct dentry *dentry = start_creating(name, parent);
410 int error;
411
412 if (IS_ERR(dentry))
413 return NULL;
414
415 error = debugfs_mkdir(dentry, S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO);
416 return end_creating(dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418EXPORT_SYMBOL_GPL(debugfs_create_dir);
419
420/**
Peter Oberparleiter66f54962007-02-13 12:13:54 +0100421 * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
422 * @name: a pointer to a string containing the name of the symbolic link to
423 * create.
424 * @parent: a pointer to the parent dentry for this symbolic link. This
Masanari Iidae2278672014-02-18 22:54:36 +0900425 * should be a directory dentry if set. If this parameter is NULL,
Peter Oberparleiter66f54962007-02-13 12:13:54 +0100426 * then the symbolic link will be created in the root of the debugfs
427 * filesystem.
428 * @target: a pointer to a string containing the path to the target of the
429 * symbolic link.
430 *
431 * This function creates a symbolic link with the given name in debugfs that
432 * links to the given target path.
433 *
434 * This function will return a pointer to a dentry if it succeeds. This
435 * pointer must be passed to the debugfs_remove() function when the symbolic
436 * link is to be removed (no automatic cleanup happens if your module is
437 * unloaded, you are responsible here.) If an error occurs, %NULL will be
438 * returned.
439 *
440 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Cornelia Huck873760f2007-02-14 07:57:47 +0100441 * returned.
Peter Oberparleiter66f54962007-02-13 12:13:54 +0100442 */
443struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
444 const char *target)
445{
Al Viroad5abd52015-01-25 14:02:31 -0500446 struct dentry *dentry;
Peter Oberparleiter66f54962007-02-13 12:13:54 +0100447 char *link;
Al Viroad5abd52015-01-25 14:02:31 -0500448 int error;
Peter Oberparleiter66f54962007-02-13 12:13:54 +0100449
450 link = kstrdup(target, GFP_KERNEL);
451 if (!link)
452 return NULL;
453
Al Viroad5abd52015-01-25 14:02:31 -0500454 dentry = start_creating(name, parent);
455
456 if (IS_ERR(dentry)) {
Peter Oberparleiter66f54962007-02-13 12:13:54 +0100457 kfree(link);
Al Viroad5abd52015-01-25 14:02:31 -0500458 return NULL;
459 }
460
Al Viro9b73fab2015-01-25 14:05:55 -0500461 error = debugfs_mknod(dentry->d_parent->d_inode, dentry,
462 S_IFLNK | S_IRWXUGO, 0, link, NULL);
Al Viroad5abd52015-01-25 14:02:31 -0500463 if (error)
464 kfree(link);
465
466 return end_creating(dentry, error);
Peter Oberparleiter66f54962007-02-13 12:13:54 +0100467}
468EXPORT_SYMBOL_GPL(debugfs_create_symlink);
469
Jan Kara25d41d82011-02-07 15:00:27 +0100470static int __debugfs_remove(struct dentry *dentry, struct dentry *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Mathieu Desnoyers65c33332006-11-24 13:50:09 -0500472 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (debugfs_positive(dentry)) {
475 if (dentry->d_inode) {
Mathieu Desnoyers29a7f3a2006-11-24 13:51:14 -0500476 dget(dentry);
Peter Oberparleiter66f54962007-02-13 12:13:54 +0100477 switch (dentry->d_inode->i_mode & S_IFMT) {
478 case S_IFDIR:
Mathieu Desnoyers65c33332006-11-24 13:50:09 -0500479 ret = simple_rmdir(parent->d_inode, dentry);
Peter Oberparleiter66f54962007-02-13 12:13:54 +0100480 break;
481 case S_IFLNK:
482 kfree(dentry->d_inode->i_private);
483 /* fall through */
484 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 simple_unlink(parent->d_inode, dentry);
Peter Oberparleiter66f54962007-02-13 12:13:54 +0100486 break;
487 }
Mathieu Desnoyers29a7f3a2006-11-24 13:51:14 -0500488 if (!ret)
489 d_delete(dentry);
490 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492 }
Jan Kara25d41d82011-02-07 15:00:27 +0100493 return ret;
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200494}
495
496/**
497 * debugfs_remove - removes a file or directory from the debugfs filesystem
498 * @dentry: a pointer to a the dentry of the file or directory to be
499 * removed.
500 *
501 * This function removes a file or directory in debugfs that was previously
502 * created with a call to another debugfs function (like
503 * debugfs_create_file() or variants thereof.)
504 *
505 * This function is required to be called in order for the file to be
506 * removed, no automatic cleanup of files will happen when a module is
507 * removed, you are responsible here.
508 */
509void debugfs_remove(struct dentry *dentry)
510{
511 struct dentry *parent;
Jan Kara25d41d82011-02-07 15:00:27 +0100512 int ret;
513
Arend van Spriela59d6292012-05-23 15:13:07 +0200514 if (IS_ERR_OR_NULL(dentry))
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200515 return;
516
517 parent = dentry->d_parent;
518 if (!parent || !parent->d_inode)
519 return;
520
521 mutex_lock(&parent->d_inode->i_mutex);
Jan Kara25d41d82011-02-07 15:00:27 +0100522 ret = __debugfs_remove(dentry, parent);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800523 mutex_unlock(&parent->d_inode->i_mutex);
Jan Kara25d41d82011-02-07 15:00:27 +0100524 if (!ret)
525 simple_release_fs(&debugfs_mount, &debugfs_mount_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527EXPORT_SYMBOL_GPL(debugfs_remove);
528
Jan Karacfc94cd2007-05-09 13:19:52 +0200529/**
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200530 * debugfs_remove_recursive - recursively removes a directory
531 * @dentry: a pointer to a the dentry of the directory to be removed.
532 *
533 * This function recursively removes a directory tree in debugfs that
534 * was previously created with a call to another debugfs function
535 * (like debugfs_create_file() or variants thereof.)
536 *
537 * This function is required to be called in order for the file to be
538 * removed, no automatic cleanup of files will happen when a module is
539 * removed, you are responsible here.
540 */
541void debugfs_remove_recursive(struct dentry *dentry)
542{
Steven Rostedt485d4402014-06-09 14:06:07 -0400543 struct dentry *child, *parent;
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200544
Arend van Spriela59d6292012-05-23 15:13:07 +0200545 if (IS_ERR_OR_NULL(dentry))
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200546 return;
547
548 parent = dentry->d_parent;
549 if (!parent || !parent->d_inode)
550 return;
551
552 parent = dentry;
Oleg Nesterov776164c2013-07-26 17:12:56 +0200553 down:
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200554 mutex_lock(&parent->d_inode->i_mutex);
Steven Rostedt485d4402014-06-09 14:06:07 -0400555 loop:
556 /*
557 * The parent->d_subdirs is protected by the d_lock. Outside that
558 * lock, the child can be unlinked and set to be freed which can
559 * use the d_u.d_child as the rcu head and corrupt this list.
560 */
561 spin_lock(&parent->d_lock);
Al Viro946e51f2014-10-26 19:19:16 -0400562 list_for_each_entry(child, &parent->d_subdirs, d_child) {
Oleg Nesterov776164c2013-07-26 17:12:56 +0200563 if (!debugfs_positive(child))
564 continue;
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200565
Oleg Nesterov776164c2013-07-26 17:12:56 +0200566 /* perhaps simple_empty(child) makes more sense */
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200567 if (!list_empty(&child->d_subdirs)) {
Steven Rostedt485d4402014-06-09 14:06:07 -0400568 spin_unlock(&parent->d_lock);
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200569 mutex_unlock(&parent->d_inode->i_mutex);
570 parent = child;
Oleg Nesterov776164c2013-07-26 17:12:56 +0200571 goto down;
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200572 }
Steven Rostedt485d4402014-06-09 14:06:07 -0400573
574 spin_unlock(&parent->d_lock);
575
Oleg Nesterov776164c2013-07-26 17:12:56 +0200576 if (!__debugfs_remove(child, parent))
577 simple_release_fs(&debugfs_mount, &debugfs_mount_count);
Steven Rostedt485d4402014-06-09 14:06:07 -0400578
579 /*
580 * The parent->d_lock protects agaist child from unlinking
581 * from d_subdirs. When releasing the parent->d_lock we can
582 * no longer trust that the next pointer is valid.
583 * Restart the loop. We'll skip this one with the
584 * debugfs_positive() check.
585 */
586 goto loop;
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200587 }
Steven Rostedt485d4402014-06-09 14:06:07 -0400588 spin_unlock(&parent->d_lock);
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200589
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200590 mutex_unlock(&parent->d_inode->i_mutex);
Oleg Nesterov776164c2013-07-26 17:12:56 +0200591 child = parent;
592 parent = parent->d_parent;
593 mutex_lock(&parent->d_inode->i_mutex);
594
Steven Rostedt485d4402014-06-09 14:06:07 -0400595 if (child != dentry)
596 /* go up */
597 goto loop;
Oleg Nesterov776164c2013-07-26 17:12:56 +0200598
599 if (!__debugfs_remove(child, parent))
600 simple_release_fs(&debugfs_mount, &debugfs_mount_count);
601 mutex_unlock(&parent->d_inode->i_mutex);
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200602}
603EXPORT_SYMBOL_GPL(debugfs_remove_recursive);
604
605/**
Jan Karacfc94cd2007-05-09 13:19:52 +0200606 * debugfs_rename - rename a file/directory in the debugfs filesystem
607 * @old_dir: a pointer to the parent dentry for the renamed object. This
608 * should be a directory dentry.
609 * @old_dentry: dentry of an object to be renamed.
610 * @new_dir: a pointer to the parent dentry where the object should be
611 * moved. This should be a directory dentry.
612 * @new_name: a pointer to a string containing the target name.
613 *
614 * This function renames a file/directory in debugfs. The target must not
615 * exist for rename to succeed.
616 *
617 * This function will return a pointer to old_dentry (which is updated to
618 * reflect renaming) if it succeeds. If an error occurs, %NULL will be
619 * returned.
620 *
621 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
622 * returned.
623 */
624struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
625 struct dentry *new_dir, const char *new_name)
626{
627 int error;
628 struct dentry *dentry = NULL, *trap;
629 const char *old_name;
630
631 trap = lock_rename(new_dir, old_dir);
632 /* Source or destination directories don't exist? */
633 if (!old_dir->d_inode || !new_dir->d_inode)
634 goto exit;
635 /* Source does not exist, cyclic rename, or mountpoint? */
636 if (!old_dentry->d_inode || old_dentry == trap ||
637 d_mountpoint(old_dentry))
638 goto exit;
639 dentry = lookup_one_len(new_name, new_dir, strlen(new_name));
640 /* Lookup failed, cyclic rename or target exists? */
641 if (IS_ERR(dentry) || dentry == trap || dentry->d_inode)
642 goto exit;
643
644 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
645
646 error = simple_rename(old_dir->d_inode, old_dentry, new_dir->d_inode,
647 dentry);
648 if (error) {
649 fsnotify_oldname_free(old_name);
650 goto exit;
651 }
652 d_move(old_dentry, dentry);
653 fsnotify_move(old_dir->d_inode, new_dir->d_inode, old_name,
Al Viro123df292009-12-25 04:57:57 -0500654 S_ISDIR(old_dentry->d_inode->i_mode),
Al Viro5a190ae2007-06-07 12:19:32 -0400655 NULL, old_dentry);
Jan Karacfc94cd2007-05-09 13:19:52 +0200656 fsnotify_oldname_free(old_name);
657 unlock_rename(new_dir, old_dir);
658 dput(dentry);
659 return old_dentry;
660exit:
661 if (dentry && !IS_ERR(dentry))
662 dput(dentry);
663 unlock_rename(new_dir, old_dir);
664 return NULL;
665}
666EXPORT_SYMBOL_GPL(debugfs_rename);
667
Frederic Weisbeckerc0f92ba2009-03-22 23:10:44 +0100668/**
669 * debugfs_initialized - Tells whether debugfs has been registered
670 */
671bool debugfs_initialized(void)
672{
673 return debugfs_registered;
674}
675EXPORT_SYMBOL_GPL(debugfs_initialized);
676
677
Greg Kroah-Hartman191e1862007-10-29 20:13:17 +0100678static struct kobject *debug_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680static int __init debugfs_init(void)
681{
682 int retval;
683
Greg Kroah-Hartman0ff21e42007-11-06 10:36:58 -0800684 debug_kobj = kobject_create_and_add("debug", kernel_kobj);
Greg Kroah-Hartman191e1862007-10-29 20:13:17 +0100685 if (!debug_kobj)
686 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 retval = register_filesystem(&debug_fs_type);
689 if (retval)
Greg Kroah-Hartman197b12d2007-12-20 08:13:05 -0800690 kobject_put(debug_kobj);
Frederic Weisbeckerc0f92ba2009-03-22 23:10:44 +0100691 else
692 debugfs_registered = true;
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return retval;
695}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696core_initcall(debugfs_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697