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