inode.c 28 KB

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