inode.c 15 KB

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