inode.c 24 KB

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