inode.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  2. * file.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/DocBook/kernel-api for more details.
  13. *
  14. */
  15. /* uncomment to get debug messages from the debug filesystem, ah the irony. */
  16. /* #define DEBUG */
  17. #include <linux/module.h>
  18. #include <linux/fs.h>
  19. #include <linux/mount.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/init.h>
  22. #include <linux/kobject.h>
  23. #include <linux/namei.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/fsnotify.h>
  26. #include <linux/string.h>
  27. #include <linux/magic.h>
  28. #include <linux/slab.h>
  29. static struct vfsmount *debugfs_mount;
  30. static int debugfs_mount_count;
  31. static bool debugfs_registered;
  32. static struct inode *debugfs_get_inode(struct super_block *sb, int mode, dev_t dev,
  33. void *data, const struct file_operations *fops)
  34. {
  35. struct inode *inode = new_inode(sb);
  36. if (inode) {
  37. inode->i_mode = mode;
  38. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  39. switch (mode & S_IFMT) {
  40. default:
  41. init_special_inode(inode, mode, dev);
  42. break;
  43. case S_IFREG:
  44. inode->i_fop = fops ? fops : &debugfs_file_operations;
  45. inode->i_private = data;
  46. break;
  47. case S_IFLNK:
  48. inode->i_op = &debugfs_link_operations;
  49. inode->i_fop = fops;
  50. inode->i_private = data;
  51. break;
  52. case S_IFDIR:
  53. inode->i_op = &simple_dir_inode_operations;
  54. inode->i_fop = fops ? fops : &simple_dir_operations;
  55. inode->i_private = data;
  56. /* directory inodes start off with i_nlink == 2
  57. * (for "." entry) */
  58. inc_nlink(inode);
  59. break;
  60. }
  61. }
  62. return inode;
  63. }
  64. /* SMP-safe */
  65. static int debugfs_mknod(struct inode *dir, struct dentry *dentry,
  66. int mode, dev_t dev, void *data,
  67. const struct file_operations *fops)
  68. {
  69. struct inode *inode;
  70. int error = -EPERM;
  71. if (dentry->d_inode)
  72. return -EEXIST;
  73. inode = debugfs_get_inode(dir->i_sb, mode, dev, data, fops);
  74. if (inode) {
  75. d_instantiate(dentry, inode);
  76. dget(dentry);
  77. error = 0;
  78. }
  79. return error;
  80. }
  81. static int debugfs_mkdir(struct inode *dir, struct dentry *dentry, int mode,
  82. void *data, const struct file_operations *fops)
  83. {
  84. int res;
  85. mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
  86. res = debugfs_mknod(dir, dentry, mode, 0, data, fops);
  87. if (!res) {
  88. inc_nlink(dir);
  89. fsnotify_mkdir(dir, dentry);
  90. }
  91. return res;
  92. }
  93. static int debugfs_link(struct inode *dir, struct dentry *dentry, int mode,
  94. void *data, const struct file_operations *fops)
  95. {
  96. mode = (mode & S_IALLUGO) | S_IFLNK;
  97. return debugfs_mknod(dir, dentry, mode, 0, data, fops);
  98. }
  99. static int debugfs_create(struct inode *dir, struct dentry *dentry, int mode,
  100. void *data, const struct file_operations *fops)
  101. {
  102. int res;
  103. mode = (mode & S_IALLUGO) | S_IFREG;
  104. res = debugfs_mknod(dir, dentry, mode, 0, data, fops);
  105. if (!res)
  106. fsnotify_create(dir, dentry);
  107. return res;
  108. }
  109. static inline int debugfs_positive(struct dentry *dentry)
  110. {
  111. return dentry->d_inode && !d_unhashed(dentry);
  112. }
  113. static int debug_fill_super(struct super_block *sb, void *data, int silent)
  114. {
  115. static struct tree_descr debug_files[] = {{""}};
  116. return simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
  117. }
  118. static int debug_get_sb(struct file_system_type *fs_type,
  119. int flags, const char *dev_name,
  120. void *data, struct vfsmount *mnt)
  121. {
  122. return get_sb_single(fs_type, flags, data, debug_fill_super, mnt);
  123. }
  124. static struct file_system_type debug_fs_type = {
  125. .owner = THIS_MODULE,
  126. .name = "debugfs",
  127. .get_sb = debug_get_sb,
  128. .kill_sb = kill_litter_super,
  129. };
  130. static int debugfs_create_by_name(const char *name, mode_t mode,
  131. struct dentry *parent,
  132. struct dentry **dentry,
  133. void *data,
  134. const struct file_operations *fops)
  135. {
  136. int error = 0;
  137. /* If the parent is not specified, we create it in the root.
  138. * We need the root dentry to do this, which is in the super
  139. * block. A pointer to that is in the struct vfsmount that we
  140. * have around.
  141. */
  142. if (!parent)
  143. parent = debugfs_mount->mnt_sb->s_root;
  144. *dentry = NULL;
  145. mutex_lock(&parent->d_inode->i_mutex);
  146. *dentry = lookup_one_len(name, parent, strlen(name));
  147. if (!IS_ERR(*dentry)) {
  148. switch (mode & S_IFMT) {
  149. case S_IFDIR:
  150. error = debugfs_mkdir(parent->d_inode, *dentry, mode,
  151. data, fops);
  152. break;
  153. case S_IFLNK:
  154. error = debugfs_link(parent->d_inode, *dentry, mode,
  155. data, fops);
  156. break;
  157. default:
  158. error = debugfs_create(parent->d_inode, *dentry, mode,
  159. data, fops);
  160. break;
  161. }
  162. dput(*dentry);
  163. } else
  164. error = PTR_ERR(*dentry);
  165. mutex_unlock(&parent->d_inode->i_mutex);
  166. return error;
  167. }
  168. /**
  169. * debugfs_create_file - create a file in the debugfs filesystem
  170. * @name: a pointer to a string containing the name of the file to create.
  171. * @mode: the permission that the file should have.
  172. * @parent: a pointer to the parent dentry for this file. This should be a
  173. * directory dentry if set. If this paramater is NULL, then the
  174. * file will be created in the root of the debugfs filesystem.
  175. * @data: a pointer to something that the caller will want to get to later
  176. * on. The inode.i_private pointer will point to this value on
  177. * the open() call.
  178. * @fops: a pointer to a struct file_operations that should be used for
  179. * this file.
  180. *
  181. * This is the basic "create a file" function for debugfs. It allows for a
  182. * wide range of flexibility in creating a file, or a directory (if you want
  183. * to create a directory, the debugfs_create_dir() function is
  184. * recommended to be used instead.)
  185. *
  186. * This function will return a pointer to a dentry if it succeeds. This
  187. * pointer must be passed to the debugfs_remove() function when the file is
  188. * to be removed (no automatic cleanup happens if your module is unloaded,
  189. * you are responsible here.) If an error occurs, %NULL will be returned.
  190. *
  191. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  192. * returned.
  193. */
  194. struct dentry *debugfs_create_file(const char *name, mode_t mode,
  195. struct dentry *parent, void *data,
  196. const struct file_operations *fops)
  197. {
  198. struct dentry *dentry = NULL;
  199. int error;
  200. pr_debug("debugfs: creating file '%s'\n",name);
  201. error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
  202. &debugfs_mount_count);
  203. if (error)
  204. goto exit;
  205. error = debugfs_create_by_name(name, mode, parent, &dentry,
  206. data, fops);
  207. if (error) {
  208. dentry = NULL;
  209. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  210. goto exit;
  211. }
  212. exit:
  213. return dentry;
  214. }
  215. EXPORT_SYMBOL_GPL(debugfs_create_file);
  216. /**
  217. * debugfs_create_dir - create a directory in the debugfs filesystem
  218. * @name: a pointer to a string containing the name of the directory to
  219. * create.
  220. * @parent: a pointer to the parent dentry for this file. This should be a
  221. * directory dentry if set. If this paramater is NULL, then the
  222. * directory will be created in the root of the debugfs filesystem.
  223. *
  224. * This function creates a directory in debugfs with the given name.
  225. *
  226. * This function will return a pointer to a dentry if it succeeds. This
  227. * pointer must be passed to the debugfs_remove() function when the file is
  228. * to be removed (no automatic cleanup happens if your module is unloaded,
  229. * you are responsible here.) If an error occurs, %NULL will be returned.
  230. *
  231. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  232. * returned.
  233. */
  234. struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
  235. {
  236. return debugfs_create_file(name,
  237. S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
  238. parent, NULL, NULL);
  239. }
  240. EXPORT_SYMBOL_GPL(debugfs_create_dir);
  241. /**
  242. * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
  243. * @name: a pointer to a string containing the name of the symbolic link to
  244. * create.
  245. * @parent: a pointer to the parent dentry for this symbolic link. This
  246. * should be a directory dentry if set. If this paramater is NULL,
  247. * then the symbolic link will be created in the root of the debugfs
  248. * filesystem.
  249. * @target: a pointer to a string containing the path to the target of the
  250. * symbolic link.
  251. *
  252. * This function creates a symbolic link with the given name in debugfs that
  253. * links to the given target path.
  254. *
  255. * This function will return a pointer to a dentry if it succeeds. This
  256. * pointer must be passed to the debugfs_remove() function when the symbolic
  257. * link is to be removed (no automatic cleanup happens if your module is
  258. * unloaded, you are responsible here.) If an error occurs, %NULL will be
  259. * returned.
  260. *
  261. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  262. * returned.
  263. */
  264. struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
  265. const char *target)
  266. {
  267. struct dentry *result;
  268. char *link;
  269. link = kstrdup(target, GFP_KERNEL);
  270. if (!link)
  271. return NULL;
  272. result = debugfs_create_file(name, S_IFLNK | S_IRWXUGO, parent, link,
  273. NULL);
  274. if (!result)
  275. kfree(link);
  276. return result;
  277. }
  278. EXPORT_SYMBOL_GPL(debugfs_create_symlink);
  279. static void __debugfs_remove(struct dentry *dentry, struct dentry *parent)
  280. {
  281. int ret = 0;
  282. if (debugfs_positive(dentry)) {
  283. if (dentry->d_inode) {
  284. dget(dentry);
  285. switch (dentry->d_inode->i_mode & S_IFMT) {
  286. case S_IFDIR:
  287. ret = simple_rmdir(parent->d_inode, dentry);
  288. break;
  289. case S_IFLNK:
  290. kfree(dentry->d_inode->i_private);
  291. /* fall through */
  292. default:
  293. simple_unlink(parent->d_inode, dentry);
  294. break;
  295. }
  296. if (!ret)
  297. d_delete(dentry);
  298. dput(dentry);
  299. }
  300. }
  301. }
  302. /**
  303. * debugfs_remove - removes a file or directory from the debugfs filesystem
  304. * @dentry: a pointer to a the dentry of the file or directory to be
  305. * removed.
  306. *
  307. * This function removes a file or directory in debugfs that was previously
  308. * created with a call to another debugfs function (like
  309. * debugfs_create_file() or variants thereof.)
  310. *
  311. * This function is required to be called in order for the file to be
  312. * removed, no automatic cleanup of files will happen when a module is
  313. * removed, you are responsible here.
  314. */
  315. void debugfs_remove(struct dentry *dentry)
  316. {
  317. struct dentry *parent;
  318. if (!dentry)
  319. return;
  320. parent = dentry->d_parent;
  321. if (!parent || !parent->d_inode)
  322. return;
  323. mutex_lock(&parent->d_inode->i_mutex);
  324. __debugfs_remove(dentry, parent);
  325. mutex_unlock(&parent->d_inode->i_mutex);
  326. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  327. }
  328. EXPORT_SYMBOL_GPL(debugfs_remove);
  329. /**
  330. * debugfs_remove_recursive - recursively removes a directory
  331. * @dentry: a pointer to a the dentry of the directory to be removed.
  332. *
  333. * This function recursively removes a directory tree in debugfs that
  334. * was previously created with a call to another debugfs function
  335. * (like debugfs_create_file() or variants thereof.)
  336. *
  337. * This function is required to be called in order for the file to be
  338. * removed, no automatic cleanup of files will happen when a module is
  339. * removed, you are responsible here.
  340. */
  341. void debugfs_remove_recursive(struct dentry *dentry)
  342. {
  343. struct dentry *child;
  344. struct dentry *parent;
  345. if (!dentry)
  346. return;
  347. parent = dentry->d_parent;
  348. if (!parent || !parent->d_inode)
  349. return;
  350. parent = dentry;
  351. mutex_lock(&parent->d_inode->i_mutex);
  352. while (1) {
  353. /*
  354. * When all dentries under "parent" has been removed,
  355. * walk up the tree until we reach our starting point.
  356. */
  357. if (list_empty(&parent->d_subdirs)) {
  358. mutex_unlock(&parent->d_inode->i_mutex);
  359. if (parent == dentry)
  360. break;
  361. parent = parent->d_parent;
  362. mutex_lock(&parent->d_inode->i_mutex);
  363. }
  364. child = list_entry(parent->d_subdirs.next, struct dentry,
  365. d_u.d_child);
  366. next_sibling:
  367. /*
  368. * If "child" isn't empty, walk down the tree and
  369. * remove all its descendants first.
  370. */
  371. if (!list_empty(&child->d_subdirs)) {
  372. mutex_unlock(&parent->d_inode->i_mutex);
  373. parent = child;
  374. mutex_lock(&parent->d_inode->i_mutex);
  375. continue;
  376. }
  377. __debugfs_remove(child, parent);
  378. if (parent->d_subdirs.next == &child->d_u.d_child) {
  379. /*
  380. * Try the next sibling.
  381. */
  382. if (child->d_u.d_child.next != &parent->d_subdirs) {
  383. child = list_entry(child->d_u.d_child.next,
  384. struct dentry,
  385. d_u.d_child);
  386. goto next_sibling;
  387. }
  388. /*
  389. * Avoid infinite loop if we fail to remove
  390. * one dentry.
  391. */
  392. mutex_unlock(&parent->d_inode->i_mutex);
  393. break;
  394. }
  395. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  396. }
  397. parent = dentry->d_parent;
  398. mutex_lock(&parent->d_inode->i_mutex);
  399. __debugfs_remove(dentry, parent);
  400. mutex_unlock(&parent->d_inode->i_mutex);
  401. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  402. }
  403. EXPORT_SYMBOL_GPL(debugfs_remove_recursive);
  404. /**
  405. * debugfs_rename - rename a file/directory in the debugfs filesystem
  406. * @old_dir: a pointer to the parent dentry for the renamed object. This
  407. * should be a directory dentry.
  408. * @old_dentry: dentry of an object to be renamed.
  409. * @new_dir: a pointer to the parent dentry where the object should be
  410. * moved. This should be a directory dentry.
  411. * @new_name: a pointer to a string containing the target name.
  412. *
  413. * This function renames a file/directory in debugfs. The target must not
  414. * exist for rename to succeed.
  415. *
  416. * This function will return a pointer to old_dentry (which is updated to
  417. * reflect renaming) if it succeeds. If an error occurs, %NULL will be
  418. * returned.
  419. *
  420. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  421. * returned.
  422. */
  423. struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
  424. struct dentry *new_dir, const char *new_name)
  425. {
  426. int error;
  427. struct dentry *dentry = NULL, *trap;
  428. const char *old_name;
  429. trap = lock_rename(new_dir, old_dir);
  430. /* Source or destination directories don't exist? */
  431. if (!old_dir->d_inode || !new_dir->d_inode)
  432. goto exit;
  433. /* Source does not exist, cyclic rename, or mountpoint? */
  434. if (!old_dentry->d_inode || old_dentry == trap ||
  435. d_mountpoint(old_dentry))
  436. goto exit;
  437. dentry = lookup_one_len(new_name, new_dir, strlen(new_name));
  438. /* Lookup failed, cyclic rename or target exists? */
  439. if (IS_ERR(dentry) || dentry == trap || dentry->d_inode)
  440. goto exit;
  441. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  442. error = simple_rename(old_dir->d_inode, old_dentry, new_dir->d_inode,
  443. dentry);
  444. if (error) {
  445. fsnotify_oldname_free(old_name);
  446. goto exit;
  447. }
  448. d_move(old_dentry, dentry);
  449. fsnotify_move(old_dir->d_inode, new_dir->d_inode, old_name,
  450. S_ISDIR(old_dentry->d_inode->i_mode),
  451. NULL, old_dentry);
  452. fsnotify_oldname_free(old_name);
  453. unlock_rename(new_dir, old_dir);
  454. dput(dentry);
  455. return old_dentry;
  456. exit:
  457. if (dentry && !IS_ERR(dentry))
  458. dput(dentry);
  459. unlock_rename(new_dir, old_dir);
  460. return NULL;
  461. }
  462. EXPORT_SYMBOL_GPL(debugfs_rename);
  463. /**
  464. * debugfs_initialized - Tells whether debugfs has been registered
  465. */
  466. bool debugfs_initialized(void)
  467. {
  468. return debugfs_registered;
  469. }
  470. EXPORT_SYMBOL_GPL(debugfs_initialized);
  471. static struct kobject *debug_kobj;
  472. static int __init debugfs_init(void)
  473. {
  474. int retval;
  475. debug_kobj = kobject_create_and_add("debug", kernel_kobj);
  476. if (!debug_kobj)
  477. return -EINVAL;
  478. retval = register_filesystem(&debug_fs_type);
  479. if (retval)
  480. kobject_put(debug_kobj);
  481. else
  482. debugfs_registered = true;
  483. return retval;
  484. }
  485. static void __exit debugfs_exit(void)
  486. {
  487. debugfs_registered = false;
  488. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  489. unregister_filesystem(&debug_fs_type);
  490. kobject_put(debug_kobj);
  491. }
  492. core_initcall(debugfs_init);
  493. module_exit(debugfs_exit);
  494. MODULE_LICENSE("GPL");