inode.c 24 KB

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