inode.c 25 KB

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