inode.c 28 KB

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