blob: 18564b08884c1f7c1fd73ceac89259baf9031a86 [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 Cook82aceae42012-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 Virof4ae40a62011-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 Desnoyersbafb232e2006-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 Virof4ae40a62011-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_link(struct dentry *dentry, umode_t mode,
Al Viroac481d62012-06-09 20:40:20 -0400107 void *data)
Peter Oberparleiter66f54962007-02-13 12:13:54 +0100108{
Al Viroe09ddf32015-01-25 13:50:23 -0500109 struct inode *dir = dentry->d_parent->d_inode;
Peter Oberparleiter66f54962007-02-13 12:13:54 +0100110 mode = (mode & S_IALLUGO) | S_IFLNK;
Al Viroac481d62012-06-09 20:40:20 -0400111 return debugfs_mknod(dir, dentry, mode, 0, data, NULL);
Peter Oberparleiter66f54962007-02-13 12:13:54 +0100112}
113
Al Viroe09ddf32015-01-25 13:50:23 -0500114static int debugfs_create(struct dentry *dentry, umode_t mode,
Mathieu Desnoyersd3a3b0a2009-11-17 14:40:26 -0800115 void *data, const struct file_operations *fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Al Viroe09ddf32015-01-25 13:50:23 -0500117 struct inode *dir = dentry->d_parent->d_inode;
Mathieu Desnoyers4f365572006-11-24 13:45:37 -0500118 int res;
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 mode = (mode & S_IALLUGO) | S_IFREG;
Mathieu Desnoyersd3a3b0a2009-11-17 14:40:26 -0800121 res = debugfs_mknod(dir, dentry, mode, 0, data, fops);
Mathieu Desnoyers4f365572006-11-24 13:45:37 -0500122 if (!res)
123 fsnotify_create(dir, dentry);
124 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
127static inline int debugfs_positive(struct dentry *dentry)
128{
129 return dentry->d_inode && !d_unhashed(dentry);
130}
131
Ludwig Nusseld6e48682012-01-25 11:52:28 +0100132struct debugfs_mount_opts {
Eric W. Biederman7dc05882012-04-03 14:01:31 -0700133 kuid_t uid;
134 kgid_t gid;
Ludwig Nusseld6e48682012-01-25 11:52:28 +0100135 umode_t mode;
136};
137
138enum {
139 Opt_uid,
140 Opt_gid,
141 Opt_mode,
142 Opt_err
143};
144
145static const match_table_t tokens = {
146 {Opt_uid, "uid=%u"},
147 {Opt_gid, "gid=%u"},
148 {Opt_mode, "mode=%o"},
149 {Opt_err, NULL}
150};
151
152struct debugfs_fs_info {
153 struct debugfs_mount_opts mount_opts;
154};
155
156static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts)
157{
158 substring_t args[MAX_OPT_ARGS];
159 int option;
160 int token;
Eric W. Biederman7dc05882012-04-03 14:01:31 -0700161 kuid_t uid;
162 kgid_t gid;
Ludwig Nusseld6e48682012-01-25 11:52:28 +0100163 char *p;
164
165 opts->mode = DEBUGFS_DEFAULT_MODE;
166
167 while ((p = strsep(&data, ",")) != NULL) {
168 if (!*p)
169 continue;
170
171 token = match_token(p, tokens, args);
172 switch (token) {
173 case Opt_uid:
174 if (match_int(&args[0], &option))
175 return -EINVAL;
Eric W. Biederman7dc05882012-04-03 14:01:31 -0700176 uid = make_kuid(current_user_ns(), option);
177 if (!uid_valid(uid))
178 return -EINVAL;
179 opts->uid = uid;
Ludwig Nusseld6e48682012-01-25 11:52:28 +0100180 break;
181 case Opt_gid:
Dave Reisnerf1688e02013-01-02 08:54:37 -0500182 if (match_int(&args[0], &option))
Ludwig Nusseld6e48682012-01-25 11:52:28 +0100183 return -EINVAL;
Eric W. Biederman7dc05882012-04-03 14:01:31 -0700184 gid = make_kgid(current_user_ns(), option);
185 if (!gid_valid(gid))
186 return -EINVAL;
187 opts->gid = gid;
Ludwig Nusseld6e48682012-01-25 11:52:28 +0100188 break;
189 case Opt_mode:
190 if (match_octal(&args[0], &option))
191 return -EINVAL;
192 opts->mode = option & S_IALLUGO;
193 break;
194 /*
195 * We might like to report bad mount options here;
196 * but traditionally debugfs has ignored all mount options
197 */
198 }
199 }
200
201 return 0;
202}
203
204static int debugfs_apply_options(struct super_block *sb)
205{
206 struct debugfs_fs_info *fsi = sb->s_fs_info;
207 struct inode *inode = sb->s_root->d_inode;
208 struct debugfs_mount_opts *opts = &fsi->mount_opts;
209
210 inode->i_mode &= ~S_IALLUGO;
211 inode->i_mode |= opts->mode;
212
213 inode->i_uid = opts->uid;
214 inode->i_gid = opts->gid;
215
216 return 0;
217}
218
219static int debugfs_remount(struct super_block *sb, int *flags, char *data)
220{
221 int err;
222 struct debugfs_fs_info *fsi = sb->s_fs_info;
223
Theodore Ts'o02b99842014-03-13 10:14:33 -0400224 sync_filesystem(sb);
Ludwig Nusseld6e48682012-01-25 11:52:28 +0100225 err = debugfs_parse_options(data, &fsi->mount_opts);
226 if (err)
227 goto fail;
228
229 debugfs_apply_options(sb);
230
231fail:
232 return err;
233}
234
235static int debugfs_show_options(struct seq_file *m, struct dentry *root)
236{
237 struct debugfs_fs_info *fsi = root->d_sb->s_fs_info;
238 struct debugfs_mount_opts *opts = &fsi->mount_opts;
239
Eric W. Biederman7dc05882012-04-03 14:01:31 -0700240 if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
241 seq_printf(m, ",uid=%u",
242 from_kuid_munged(&init_user_ns, opts->uid));
243 if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
244 seq_printf(m, ",gid=%u",
245 from_kgid_munged(&init_user_ns, opts->gid));
Ludwig Nusseld6e48682012-01-25 11:52:28 +0100246 if (opts->mode != DEBUGFS_DEFAULT_MODE)
247 seq_printf(m, ",mode=%o", opts->mode);
248
249 return 0;
250}
251
252static const struct super_operations debugfs_super_operations = {
253 .statfs = simple_statfs,
254 .remount_fs = debugfs_remount,
255 .show_options = debugfs_show_options,
256};
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258static int debug_fill_super(struct super_block *sb, void *data, int silent)
259{
260 static struct tree_descr debug_files[] = {{""}};
Ludwig Nusseld6e48682012-01-25 11:52:28 +0100261 struct debugfs_fs_info *fsi;
262 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Ludwig Nusseld6e48682012-01-25 11:52:28 +0100264 save_mount_options(sb, data);
265
266 fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL);
267 sb->s_fs_info = fsi;
268 if (!fsi) {
269 err = -ENOMEM;
270 goto fail;
271 }
272
273 err = debugfs_parse_options(data, &fsi->mount_opts);
274 if (err)
275 goto fail;
276
277 err = simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
278 if (err)
279 goto fail;
280
281 sb->s_op = &debugfs_super_operations;
282
283 debugfs_apply_options(sb);
284
285 return 0;
286
287fail:
288 kfree(fsi);
289 sb->s_fs_info = NULL;
290 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Al Virofc14f2f2010-07-25 01:48:30 +0400293static struct dentry *debug_mount(struct file_system_type *fs_type,
David Howells454e2392006-06-23 02:02:57 -0700294 int flags, const char *dev_name,
Al Virofc14f2f2010-07-25 01:48:30 +0400295 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Al Virofc14f2f2010-07-25 01:48:30 +0400297 return mount_single(fs_type, flags, data, debug_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300static struct file_system_type debug_fs_type = {
301 .owner = THIS_MODULE,
302 .name = "debugfs",
Al Virofc14f2f2010-07-25 01:48:30 +0400303 .mount = debug_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 .kill_sb = kill_litter_super,
305};
Eric W. Biederman7f78e032013-03-02 19:39:14 -0800306MODULE_ALIAS_FS("debugfs");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Al Viro190afd82015-01-25 13:55:55 -0500308static struct dentry *start_creating(const char *name, struct dentry *parent)
Al Viroc3b1a352012-06-09 20:28:22 -0400309{
Al Viro190afd82015-01-25 13:55:55 -0500310 struct dentry *dentry;
Al Viroc3b1a352012-06-09 20:28:22 -0400311 int error;
312
313 pr_debug("debugfs: creating file '%s'\n",name);
314
315 error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
316 &debugfs_mount_count);
317 if (error)
Al Viro190afd82015-01-25 13:55:55 -0500318 return ERR_PTR(error);
Al Viroc3b1a352012-06-09 20:28:22 -0400319
Al Virocfa57c12012-06-09 20:33:28 -0400320 /* If the parent is not specified, we create it in the root.
Rahul Bedarkar88e412e2014-06-06 23:12:04 +0530321 * We need the root dentry to do this, which is in the super
Al Virocfa57c12012-06-09 20:33:28 -0400322 * block. A pointer to that is in the struct vfsmount that we
323 * have around.
324 */
325 if (!parent)
326 parent = debugfs_mount->mnt_root;
327
Al Virocfa57c12012-06-09 20:33:28 -0400328 mutex_lock(&parent->d_inode->i_mutex);
329 dentry = lookup_one_len(name, parent, strlen(name));
Al Viro190afd82015-01-25 13:55:55 -0500330 if (!IS_ERR(dentry) && dentry->d_inode) {
Al Virocfa57c12012-06-09 20:33:28 -0400331 dput(dentry);
Al Viro190afd82015-01-25 13:55:55 -0500332 dentry = ERR_PTR(-EEXIST);
333 }
334 if (IS_ERR(dentry))
335 mutex_unlock(&parent->d_inode->i_mutex);
336 return dentry;
337}
338
339static struct dentry *end_creating(struct dentry *dentry, int error)
340{
341 mutex_unlock(&dentry->d_parent->d_inode->i_mutex);
342 dput(dentry);
Al Virocfa57c12012-06-09 20:33:28 -0400343
Al Viroc3b1a352012-06-09 20:28:22 -0400344 if (error) {
345 dentry = NULL;
346 simple_release_fs(&debugfs_mount, &debugfs_mount_count);
347 }
Al Viroc3b1a352012-06-09 20:28:22 -0400348 return dentry;
349}
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351/**
352 * debugfs_create_file - create a file in the debugfs filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 * @name: a pointer to a string containing the name of the file to create.
Alberto Bertoglibe030e62009-10-31 18:26:52 -0300354 * @mode: the permission that the file should have.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 * @parent: a pointer to the parent dentry for this file. This should be a
Masanari Iidae2278672014-02-18 22:54:36 +0900356 * directory dentry if set. If this parameter is NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 * file will be created in the root of the debugfs filesystem.
358 * @data: a pointer to something that the caller will want to get to later
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700359 * on. The inode.i_private pointer will point to this value on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 * the open() call.
361 * @fops: a pointer to a struct file_operations that should be used for
362 * this file.
363 *
364 * This is the basic "create a file" function for debugfs. It allows for a
Alberto Bertoglibe030e62009-10-31 18:26:52 -0300365 * wide range of flexibility in creating a file, or a directory (if you want
366 * to create a directory, the debugfs_create_dir() function is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 * recommended to be used instead.)
368 *
369 * This function will return a pointer to a dentry if it succeeds. This
370 * pointer must be passed to the debugfs_remove() function when the file is
371 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700372 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700374 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Cornelia Huck873760f2007-02-14 07:57:47 +0100375 * returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 */
Al Virof4ae40a62011-07-24 04:33:43 -0400377struct dentry *debugfs_create_file(const char *name, umode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 struct dentry *parent, void *data,
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800379 const struct file_operations *fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Al Viroad5abd52015-01-25 14:02:31 -0500381 struct dentry *dentry;
382 int error;
Al Viroc3b1a352012-06-09 20:28:22 -0400383
Al Viroad5abd52015-01-25 14:02:31 -0500384 if (!(mode & S_IFMT))
385 mode |= S_IFREG;
386 BUG_ON(!S_ISREG(mode));
387 dentry = start_creating(name, parent);
388
389 if (IS_ERR(dentry))
390 return NULL;
391
392 error = debugfs_create(dentry, mode, data, fops);
393 return end_creating(dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395EXPORT_SYMBOL_GPL(debugfs_create_file);
396
397/**
398 * debugfs_create_dir - create a directory in the debugfs filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 * @name: a pointer to a string containing the name of the directory to
400 * create.
401 * @parent: a pointer to the parent dentry for this file. This should be a
Masanari Iidae2278672014-02-18 22:54:36 +0900402 * directory dentry if set. If this parameter is NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 * directory will be created in the root of the debugfs filesystem.
404 *
405 * This function creates a directory in debugfs with the given name.
406 *
407 * This function will return a pointer to a dentry if it succeeds. This
408 * pointer must be passed to the debugfs_remove() function when the file is
409 * to be removed (no automatic cleanup happens if your module is unloaded,
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700410 * you are responsible here.) If an error occurs, %NULL will be returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 *
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700412 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Cornelia Huck873760f2007-02-14 07:57:47 +0100413 * returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 */
415struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
416{
Al Viroad5abd52015-01-25 14:02:31 -0500417 struct dentry *dentry = start_creating(name, parent);
418 int error;
419
420 if (IS_ERR(dentry))
421 return NULL;
422
423 error = debugfs_mkdir(dentry, S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO);
424 return end_creating(dentry, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426EXPORT_SYMBOL_GPL(debugfs_create_dir);
427
428/**
Peter Oberparleiter66f54962007-02-13 12:13:54 +0100429 * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
430 * @name: a pointer to a string containing the name of the symbolic link to
431 * create.
432 * @parent: a pointer to the parent dentry for this symbolic link. This
Masanari Iidae2278672014-02-18 22:54:36 +0900433 * should be a directory dentry if set. If this parameter is NULL,
Peter Oberparleiter66f54962007-02-13 12:13:54 +0100434 * then the symbolic link will be created in the root of the debugfs
435 * filesystem.
436 * @target: a pointer to a string containing the path to the target of the
437 * symbolic link.
438 *
439 * This function creates a symbolic link with the given name in debugfs that
440 * links to the given target path.
441 *
442 * This function will return a pointer to a dentry if it succeeds. This
443 * pointer must be passed to the debugfs_remove() function when the symbolic
444 * link is to be removed (no automatic cleanup happens if your module is
445 * unloaded, you are responsible here.) If an error occurs, %NULL will be
446 * returned.
447 *
448 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
Cornelia Huck873760f2007-02-14 07:57:47 +0100449 * returned.
Peter Oberparleiter66f54962007-02-13 12:13:54 +0100450 */
451struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
452 const char *target)
453{
Al Viroad5abd52015-01-25 14:02:31 -0500454 struct dentry *dentry;
Peter Oberparleiter66f54962007-02-13 12:13:54 +0100455 char *link;
Al Viroad5abd52015-01-25 14:02:31 -0500456 int error;
Peter Oberparleiter66f54962007-02-13 12:13:54 +0100457
458 link = kstrdup(target, GFP_KERNEL);
459 if (!link)
460 return NULL;
461
Al Viroad5abd52015-01-25 14:02:31 -0500462 dentry = start_creating(name, parent);
463
464 if (IS_ERR(dentry)) {
Peter Oberparleiter66f54962007-02-13 12:13:54 +0100465 kfree(link);
Al Viroad5abd52015-01-25 14:02:31 -0500466 return NULL;
467 }
468
469 error = debugfs_link(dentry, S_IFLNK | S_IRWXUGO, link);
470 if (error)
471 kfree(link);
472
473 return end_creating(dentry, error);
Peter Oberparleiter66f54962007-02-13 12:13:54 +0100474}
475EXPORT_SYMBOL_GPL(debugfs_create_symlink);
476
Jan Kara25d41d82011-02-07 15:00:27 +0100477static int __debugfs_remove(struct dentry *dentry, struct dentry *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Mathieu Desnoyers65c33332006-11-24 13:50:09 -0500479 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (debugfs_positive(dentry)) {
482 if (dentry->d_inode) {
Mathieu Desnoyers29a7f3a2006-11-24 13:51:14 -0500483 dget(dentry);
Peter Oberparleiter66f54962007-02-13 12:13:54 +0100484 switch (dentry->d_inode->i_mode & S_IFMT) {
485 case S_IFDIR:
Mathieu Desnoyers65c33332006-11-24 13:50:09 -0500486 ret = simple_rmdir(parent->d_inode, dentry);
Peter Oberparleiter66f54962007-02-13 12:13:54 +0100487 break;
488 case S_IFLNK:
489 kfree(dentry->d_inode->i_private);
490 /* fall through */
491 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 simple_unlink(parent->d_inode, dentry);
Peter Oberparleiter66f54962007-02-13 12:13:54 +0100493 break;
494 }
Mathieu Desnoyers29a7f3a2006-11-24 13:51:14 -0500495 if (!ret)
496 d_delete(dentry);
497 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499 }
Jan Kara25d41d82011-02-07 15:00:27 +0100500 return ret;
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200501}
502
503/**
504 * debugfs_remove - removes a file or directory from the debugfs filesystem
505 * @dentry: a pointer to a the dentry of the file or directory to be
506 * removed.
507 *
508 * This function removes a file or directory in debugfs that was previously
509 * created with a call to another debugfs function (like
510 * debugfs_create_file() or variants thereof.)
511 *
512 * This function is required to be called in order for the file to be
513 * removed, no automatic cleanup of files will happen when a module is
514 * removed, you are responsible here.
515 */
516void debugfs_remove(struct dentry *dentry)
517{
518 struct dentry *parent;
Jan Kara25d41d82011-02-07 15:00:27 +0100519 int ret;
520
Arend van Spriela59d6292012-05-23 15:13:07 +0200521 if (IS_ERR_OR_NULL(dentry))
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200522 return;
523
524 parent = dentry->d_parent;
525 if (!parent || !parent->d_inode)
526 return;
527
528 mutex_lock(&parent->d_inode->i_mutex);
Jan Kara25d41d82011-02-07 15:00:27 +0100529 ret = __debugfs_remove(dentry, parent);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800530 mutex_unlock(&parent->d_inode->i_mutex);
Jan Kara25d41d82011-02-07 15:00:27 +0100531 if (!ret)
532 simple_release_fs(&debugfs_mount, &debugfs_mount_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534EXPORT_SYMBOL_GPL(debugfs_remove);
535
Jan Karacfc94cd2007-05-09 13:19:52 +0200536/**
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200537 * debugfs_remove_recursive - recursively removes a directory
538 * @dentry: a pointer to a the dentry of the directory to be removed.
539 *
540 * This function recursively removes a directory tree in debugfs that
541 * was previously created with a call to another debugfs function
542 * (like debugfs_create_file() or variants thereof.)
543 *
544 * This function is required to be called in order for the file to be
545 * removed, no automatic cleanup of files will happen when a module is
546 * removed, you are responsible here.
547 */
548void debugfs_remove_recursive(struct dentry *dentry)
549{
Steven Rostedt485d4402014-06-09 14:06:07 -0400550 struct dentry *child, *parent;
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200551
Arend van Spriela59d6292012-05-23 15:13:07 +0200552 if (IS_ERR_OR_NULL(dentry))
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200553 return;
554
555 parent = dentry->d_parent;
556 if (!parent || !parent->d_inode)
557 return;
558
559 parent = dentry;
Oleg Nesterov776164c2013-07-26 17:12:56 +0200560 down:
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200561 mutex_lock(&parent->d_inode->i_mutex);
Steven Rostedt485d4402014-06-09 14:06:07 -0400562 loop:
563 /*
564 * The parent->d_subdirs is protected by the d_lock. Outside that
565 * lock, the child can be unlinked and set to be freed which can
566 * use the d_u.d_child as the rcu head and corrupt this list.
567 */
568 spin_lock(&parent->d_lock);
Al Viro946e51f2014-10-26 19:19:16 -0400569 list_for_each_entry(child, &parent->d_subdirs, d_child) {
Oleg Nesterov776164c2013-07-26 17:12:56 +0200570 if (!debugfs_positive(child))
571 continue;
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200572
Oleg Nesterov776164c2013-07-26 17:12:56 +0200573 /* perhaps simple_empty(child) makes more sense */
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200574 if (!list_empty(&child->d_subdirs)) {
Steven Rostedt485d4402014-06-09 14:06:07 -0400575 spin_unlock(&parent->d_lock);
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200576 mutex_unlock(&parent->d_inode->i_mutex);
577 parent = child;
Oleg Nesterov776164c2013-07-26 17:12:56 +0200578 goto down;
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200579 }
Steven Rostedt485d4402014-06-09 14:06:07 -0400580
581 spin_unlock(&parent->d_lock);
582
Oleg Nesterov776164c2013-07-26 17:12:56 +0200583 if (!__debugfs_remove(child, parent))
584 simple_release_fs(&debugfs_mount, &debugfs_mount_count);
Steven Rostedt485d4402014-06-09 14:06:07 -0400585
586 /*
587 * The parent->d_lock protects agaist child from unlinking
588 * from d_subdirs. When releasing the parent->d_lock we can
589 * no longer trust that the next pointer is valid.
590 * Restart the loop. We'll skip this one with the
591 * debugfs_positive() check.
592 */
593 goto loop;
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200594 }
Steven Rostedt485d4402014-06-09 14:06:07 -0400595 spin_unlock(&parent->d_lock);
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200596
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200597 mutex_unlock(&parent->d_inode->i_mutex);
Oleg Nesterov776164c2013-07-26 17:12:56 +0200598 child = parent;
599 parent = parent->d_parent;
600 mutex_lock(&parent->d_inode->i_mutex);
601
Steven Rostedt485d4402014-06-09 14:06:07 -0400602 if (child != dentry)
603 /* go up */
604 goto loop;
Oleg Nesterov776164c2013-07-26 17:12:56 +0200605
606 if (!__debugfs_remove(child, parent))
607 simple_release_fs(&debugfs_mount, &debugfs_mount_count);
608 mutex_unlock(&parent->d_inode->i_mutex);
Haavard Skinnemoen9505e632008-07-01 15:14:51 +0200609}
610EXPORT_SYMBOL_GPL(debugfs_remove_recursive);
611
612/**
Jan Karacfc94cd2007-05-09 13:19:52 +0200613 * debugfs_rename - rename a file/directory in the debugfs filesystem
614 * @old_dir: a pointer to the parent dentry for the renamed object. This
615 * should be a directory dentry.
616 * @old_dentry: dentry of an object to be renamed.
617 * @new_dir: a pointer to the parent dentry where the object should be
618 * moved. This should be a directory dentry.
619 * @new_name: a pointer to a string containing the target name.
620 *
621 * This function renames a file/directory in debugfs. The target must not
622 * exist for rename to succeed.
623 *
624 * This function will return a pointer to old_dentry (which is updated to
625 * reflect renaming) if it succeeds. If an error occurs, %NULL will be
626 * returned.
627 *
628 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
629 * returned.
630 */
631struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
632 struct dentry *new_dir, const char *new_name)
633{
634 int error;
635 struct dentry *dentry = NULL, *trap;
636 const char *old_name;
637
638 trap = lock_rename(new_dir, old_dir);
639 /* Source or destination directories don't exist? */
640 if (!old_dir->d_inode || !new_dir->d_inode)
641 goto exit;
642 /* Source does not exist, cyclic rename, or mountpoint? */
643 if (!old_dentry->d_inode || old_dentry == trap ||
644 d_mountpoint(old_dentry))
645 goto exit;
646 dentry = lookup_one_len(new_name, new_dir, strlen(new_name));
647 /* Lookup failed, cyclic rename or target exists? */
648 if (IS_ERR(dentry) || dentry == trap || dentry->d_inode)
649 goto exit;
650
651 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
652
653 error = simple_rename(old_dir->d_inode, old_dentry, new_dir->d_inode,
654 dentry);
655 if (error) {
656 fsnotify_oldname_free(old_name);
657 goto exit;
658 }
659 d_move(old_dentry, dentry);
660 fsnotify_move(old_dir->d_inode, new_dir->d_inode, old_name,
Al Viro123df292009-12-25 04:57:57 -0500661 S_ISDIR(old_dentry->d_inode->i_mode),
Al Viro5a190ae2007-06-07 12:19:32 -0400662 NULL, old_dentry);
Jan Karacfc94cd2007-05-09 13:19:52 +0200663 fsnotify_oldname_free(old_name);
664 unlock_rename(new_dir, old_dir);
665 dput(dentry);
666 return old_dentry;
667exit:
668 if (dentry && !IS_ERR(dentry))
669 dput(dentry);
670 unlock_rename(new_dir, old_dir);
671 return NULL;
672}
673EXPORT_SYMBOL_GPL(debugfs_rename);
674
Frederic Weisbeckerc0f92ba2009-03-22 23:10:44 +0100675/**
676 * debugfs_initialized - Tells whether debugfs has been registered
677 */
678bool debugfs_initialized(void)
679{
680 return debugfs_registered;
681}
682EXPORT_SYMBOL_GPL(debugfs_initialized);
683
684
Greg Kroah-Hartman191e1862007-10-29 20:13:17 +0100685static struct kobject *debug_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687static int __init debugfs_init(void)
688{
689 int retval;
690
Greg Kroah-Hartman0ff21e42007-11-06 10:36:58 -0800691 debug_kobj = kobject_create_and_add("debug", kernel_kobj);
Greg Kroah-Hartman191e1862007-10-29 20:13:17 +0100692 if (!debug_kobj)
693 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 retval = register_filesystem(&debug_fs_type);
696 if (retval)
Greg Kroah-Hartman197b12d2007-12-20 08:13:05 -0800697 kobject_put(debug_kobj);
Frederic Weisbeckerc0f92ba2009-03-22 23:10:44 +0100698 else
699 debugfs_registered = true;
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return retval;
702}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703core_initcall(debugfs_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704