inode.c 26 KB

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