inode.c 26 KB

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