file.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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/filesystems for more details.
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/fs.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/namei.h>
  19. #include <linux/debugfs.h>
  20. static ssize_t default_read_file(struct file *file, char __user *buf,
  21. size_t count, loff_t *ppos)
  22. {
  23. return 0;
  24. }
  25. static ssize_t default_write_file(struct file *file, const char __user *buf,
  26. size_t count, loff_t *ppos)
  27. {
  28. return count;
  29. }
  30. static int default_open(struct inode *inode, struct file *file)
  31. {
  32. if (inode->i_private)
  33. file->private_data = inode->i_private;
  34. return 0;
  35. }
  36. const struct file_operations debugfs_file_operations = {
  37. .read = default_read_file,
  38. .write = default_write_file,
  39. .open = default_open,
  40. };
  41. static void *debugfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  42. {
  43. nd_set_link(nd, dentry->d_inode->i_private);
  44. return NULL;
  45. }
  46. const struct inode_operations debugfs_link_operations = {
  47. .readlink = generic_readlink,
  48. .follow_link = debugfs_follow_link,
  49. };
  50. static int debugfs_u8_set(void *data, u64 val)
  51. {
  52. *(u8 *)data = val;
  53. return 0;
  54. }
  55. static int debugfs_u8_get(void *data, u64 *val)
  56. {
  57. *val = *(u8 *)data;
  58. return 0;
  59. }
  60. DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
  61. /**
  62. * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
  63. * @name: a pointer to a string containing the name of the file to create.
  64. * @mode: the permission that the file should have
  65. * @parent: a pointer to the parent dentry for this file. This should be a
  66. * directory dentry if set. If this parameter is %NULL, then the
  67. * file will be created in the root of the debugfs filesystem.
  68. * @value: a pointer to the variable that the file should read to and write
  69. * from.
  70. *
  71. * This function creates a file in debugfs with the given name that
  72. * contains the value of the variable @value. If the @mode variable is so
  73. * set, it can be read from, and written to.
  74. *
  75. * This function will return a pointer to a dentry if it succeeds. This
  76. * pointer must be passed to the debugfs_remove() function when the file is
  77. * to be removed (no automatic cleanup happens if your module is unloaded,
  78. * you are responsible here.) If an error occurs, %NULL will be returned.
  79. *
  80. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  81. * returned. It is not wise to check for this value, but rather, check for
  82. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  83. * code.
  84. */
  85. struct dentry *debugfs_create_u8(const char *name, mode_t mode,
  86. struct dentry *parent, u8 *value)
  87. {
  88. return debugfs_create_file(name, mode, parent, value, &fops_u8);
  89. }
  90. EXPORT_SYMBOL_GPL(debugfs_create_u8);
  91. static int debugfs_u16_set(void *data, u64 val)
  92. {
  93. *(u16 *)data = val;
  94. return 0;
  95. }
  96. static int debugfs_u16_get(void *data, u64 *val)
  97. {
  98. *val = *(u16 *)data;
  99. return 0;
  100. }
  101. DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
  102. /**
  103. * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
  104. * @name: a pointer to a string containing the name of the file to create.
  105. * @mode: the permission that the file should have
  106. * @parent: a pointer to the parent dentry for this file. This should be a
  107. * directory dentry if set. If this parameter is %NULL, then the
  108. * file will be created in the root of the debugfs filesystem.
  109. * @value: a pointer to the variable that the file should read to and write
  110. * from.
  111. *
  112. * This function creates a file in debugfs with the given name that
  113. * contains the value of the variable @value. If the @mode variable is so
  114. * set, it can be read from, and written to.
  115. *
  116. * This function will return a pointer to a dentry if it succeeds. This
  117. * pointer must be passed to the debugfs_remove() function when the file is
  118. * to be removed (no automatic cleanup happens if your module is unloaded,
  119. * you are responsible here.) If an error occurs, %NULL will be returned.
  120. *
  121. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  122. * returned. It is not wise to check for this value, but rather, check for
  123. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  124. * code.
  125. */
  126. struct dentry *debugfs_create_u16(const char *name, mode_t mode,
  127. struct dentry *parent, u16 *value)
  128. {
  129. return debugfs_create_file(name, mode, parent, value, &fops_u16);
  130. }
  131. EXPORT_SYMBOL_GPL(debugfs_create_u16);
  132. static int debugfs_u32_set(void *data, u64 val)
  133. {
  134. *(u32 *)data = val;
  135. return 0;
  136. }
  137. static int debugfs_u32_get(void *data, u64 *val)
  138. {
  139. *val = *(u32 *)data;
  140. return 0;
  141. }
  142. DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
  143. /**
  144. * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
  145. * @name: a pointer to a string containing the name of the file to create.
  146. * @mode: the permission that the file should have
  147. * @parent: a pointer to the parent dentry for this file. This should be a
  148. * directory dentry if set. If this parameter is %NULL, then the
  149. * file will be created in the root of the debugfs filesystem.
  150. * @value: a pointer to the variable that the file should read to and write
  151. * from.
  152. *
  153. * This function creates a file in debugfs with the given name that
  154. * contains the value of the variable @value. If the @mode variable is so
  155. * set, it can be read from, and written to.
  156. *
  157. * This function will return a pointer to a dentry if it succeeds. This
  158. * pointer must be passed to the debugfs_remove() function when the file is
  159. * to be removed (no automatic cleanup happens if your module is unloaded,
  160. * you are responsible here.) If an error occurs, %NULL will be returned.
  161. *
  162. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  163. * returned. It is not wise to check for this value, but rather, check for
  164. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  165. * code.
  166. */
  167. struct dentry *debugfs_create_u32(const char *name, mode_t mode,
  168. struct dentry *parent, u32 *value)
  169. {
  170. return debugfs_create_file(name, mode, parent, value, &fops_u32);
  171. }
  172. EXPORT_SYMBOL_GPL(debugfs_create_u32);
  173. static int debugfs_u64_set(void *data, u64 val)
  174. {
  175. *(u64 *)data = val;
  176. return 0;
  177. }
  178. static int debugfs_u64_get(void *data, u64 *val)
  179. {
  180. *val = *(u64 *)data;
  181. return 0;
  182. }
  183. DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
  184. /**
  185. * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
  186. * @name: a pointer to a string containing the name of the file to create.
  187. * @mode: the permission that the file should have
  188. * @parent: a pointer to the parent dentry for this file. This should be a
  189. * directory dentry if set. If this parameter is %NULL, then the
  190. * file will be created in the root of the debugfs filesystem.
  191. * @value: a pointer to the variable that the file should read to and write
  192. * from.
  193. *
  194. * This function creates a file in debugfs with the given name that
  195. * contains the value of the variable @value. If the @mode variable is so
  196. * set, it can be read from, and written to.
  197. *
  198. * This function will return a pointer to a dentry if it succeeds. This
  199. * pointer must be passed to the debugfs_remove() function when the file is
  200. * to be removed (no automatic cleanup happens if your module is unloaded,
  201. * you are responsible here.) If an error occurs, %NULL will be returned.
  202. *
  203. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  204. * returned. It is not wise to check for this value, but rather, check for
  205. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  206. * code.
  207. */
  208. struct dentry *debugfs_create_u64(const char *name, mode_t mode,
  209. struct dentry *parent, u64 *value)
  210. {
  211. return debugfs_create_file(name, mode, parent, value, &fops_u64);
  212. }
  213. EXPORT_SYMBOL_GPL(debugfs_create_u64);
  214. DEFINE_SIMPLE_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
  215. DEFINE_SIMPLE_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%04llx\n");
  216. DEFINE_SIMPLE_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%08llx\n");
  217. /*
  218. * debugfs_create_x{8,16,32} - create a debugfs file that is used to read and write an unsigned {8,16,32}-bit value
  219. *
  220. * These functions are exactly the same as the above functions (but use a hex
  221. * output for the decimal challenged). For details look at the above unsigned
  222. * decimal functions.
  223. */
  224. /**
  225. * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
  226. * @name: a pointer to a string containing the name of the file to create.
  227. * @mode: the permission that the file should have
  228. * @parent: a pointer to the parent dentry for this file. This should be a
  229. * directory dentry if set. If this parameter is %NULL, then the
  230. * file will be created in the root of the debugfs filesystem.
  231. * @value: a pointer to the variable that the file should read to and write
  232. * from.
  233. */
  234. struct dentry *debugfs_create_x8(const char *name, mode_t mode,
  235. struct dentry *parent, u8 *value)
  236. {
  237. return debugfs_create_file(name, mode, parent, value, &fops_x8);
  238. }
  239. EXPORT_SYMBOL_GPL(debugfs_create_x8);
  240. /**
  241. * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
  242. * @name: a pointer to a string containing the name of the file to create.
  243. * @mode: the permission that the file should have
  244. * @parent: a pointer to the parent dentry for this file. This should be a
  245. * directory dentry if set. If this parameter is %NULL, then the
  246. * file will be created in the root of the debugfs filesystem.
  247. * @value: a pointer to the variable that the file should read to and write
  248. * from.
  249. */
  250. struct dentry *debugfs_create_x16(const char *name, mode_t mode,
  251. struct dentry *parent, u16 *value)
  252. {
  253. return debugfs_create_file(name, mode, parent, value, &fops_x16);
  254. }
  255. EXPORT_SYMBOL_GPL(debugfs_create_x16);
  256. /**
  257. * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
  258. * @name: a pointer to a string containing the name of the file to create.
  259. * @mode: the permission that the file should have
  260. * @parent: a pointer to the parent dentry for this file. This should be a
  261. * directory dentry if set. If this parameter is %NULL, then the
  262. * file will be created in the root of the debugfs filesystem.
  263. * @value: a pointer to the variable that the file should read to and write
  264. * from.
  265. */
  266. struct dentry *debugfs_create_x32(const char *name, mode_t mode,
  267. struct dentry *parent, u32 *value)
  268. {
  269. return debugfs_create_file(name, mode, parent, value, &fops_x32);
  270. }
  271. EXPORT_SYMBOL_GPL(debugfs_create_x32);
  272. static int debugfs_size_t_set(void *data, u64 val)
  273. {
  274. *(size_t *)data = val;
  275. return 0;
  276. }
  277. static int debugfs_size_t_get(void *data, u64 *val)
  278. {
  279. *val = *(size_t *)data;
  280. return 0;
  281. }
  282. DEFINE_SIMPLE_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
  283. "%llu\n"); /* %llu and %zu are more or less the same */
  284. /**
  285. * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
  286. * @name: a pointer to a string containing the name of the file to create.
  287. * @mode: the permission that the file should have
  288. * @parent: a pointer to the parent dentry for this file. This should be a
  289. * directory dentry if set. If this parameter is %NULL, then the
  290. * file will be created in the root of the debugfs filesystem.
  291. * @value: a pointer to the variable that the file should read to and write
  292. * from.
  293. */
  294. struct dentry *debugfs_create_size_t(const char *name, mode_t mode,
  295. struct dentry *parent, size_t *value)
  296. {
  297. return debugfs_create_file(name, mode, parent, value, &fops_size_t);
  298. }
  299. EXPORT_SYMBOL_GPL(debugfs_create_size_t);
  300. static ssize_t read_file_bool(struct file *file, char __user *user_buf,
  301. size_t count, loff_t *ppos)
  302. {
  303. char buf[3];
  304. u32 *val = file->private_data;
  305. if (*val)
  306. buf[0] = 'Y';
  307. else
  308. buf[0] = 'N';
  309. buf[1] = '\n';
  310. buf[2] = 0x00;
  311. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  312. }
  313. static ssize_t write_file_bool(struct file *file, const char __user *user_buf,
  314. size_t count, loff_t *ppos)
  315. {
  316. char buf[32];
  317. int buf_size;
  318. u32 *val = file->private_data;
  319. buf_size = min(count, (sizeof(buf)-1));
  320. if (copy_from_user(buf, user_buf, buf_size))
  321. return -EFAULT;
  322. switch (buf[0]) {
  323. case 'y':
  324. case 'Y':
  325. case '1':
  326. *val = 1;
  327. break;
  328. case 'n':
  329. case 'N':
  330. case '0':
  331. *val = 0;
  332. break;
  333. }
  334. return count;
  335. }
  336. static const struct file_operations fops_bool = {
  337. .read = read_file_bool,
  338. .write = write_file_bool,
  339. .open = default_open,
  340. };
  341. /**
  342. * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
  343. * @name: a pointer to a string containing the name of the file to create.
  344. * @mode: the permission that the file should have
  345. * @parent: a pointer to the parent dentry for this file. This should be a
  346. * directory dentry if set. If this parameter is %NULL, then the
  347. * file will be created in the root of the debugfs filesystem.
  348. * @value: a pointer to the variable that the file should read to and write
  349. * from.
  350. *
  351. * This function creates a file in debugfs with the given name that
  352. * contains the value of the variable @value. If the @mode variable is so
  353. * set, it can be read from, and written to.
  354. *
  355. * This function will return a pointer to a dentry if it succeeds. This
  356. * pointer must be passed to the debugfs_remove() function when the file is
  357. * to be removed (no automatic cleanup happens if your module is unloaded,
  358. * you are responsible here.) If an error occurs, %NULL will be returned.
  359. *
  360. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  361. * returned. It is not wise to check for this value, but rather, check for
  362. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  363. * code.
  364. */
  365. struct dentry *debugfs_create_bool(const char *name, mode_t mode,
  366. struct dentry *parent, u32 *value)
  367. {
  368. return debugfs_create_file(name, mode, parent, value, &fops_bool);
  369. }
  370. EXPORT_SYMBOL_GPL(debugfs_create_bool);
  371. static ssize_t read_file_blob(struct file *file, char __user *user_buf,
  372. size_t count, loff_t *ppos)
  373. {
  374. struct debugfs_blob_wrapper *blob = file->private_data;
  375. return simple_read_from_buffer(user_buf, count, ppos, blob->data,
  376. blob->size);
  377. }
  378. static const struct file_operations fops_blob = {
  379. .read = read_file_blob,
  380. .open = default_open,
  381. };
  382. /**
  383. * debugfs_create_blob - create a debugfs file that is used to read and write a binary blob
  384. * @name: a pointer to a string containing the name of the file to create.
  385. * @mode: the permission that the file should have
  386. * @parent: a pointer to the parent dentry for this file. This should be a
  387. * directory dentry if set. If this parameter is %NULL, then the
  388. * file will be created in the root of the debugfs filesystem.
  389. * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
  390. * to the blob data and the size of the data.
  391. *
  392. * This function creates a file in debugfs with the given name that exports
  393. * @blob->data as a binary blob. If the @mode variable is so set it can be
  394. * read from. Writing is not supported.
  395. *
  396. * This function will return a pointer to a dentry if it succeeds. This
  397. * pointer must be passed to the debugfs_remove() function when the file is
  398. * to be removed (no automatic cleanup happens if your module is unloaded,
  399. * you are responsible here.) If an error occurs, %NULL will be returned.
  400. *
  401. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  402. * returned. It is not wise to check for this value, but rather, check for
  403. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  404. * code.
  405. */
  406. struct dentry *debugfs_create_blob(const char *name, mode_t mode,
  407. struct dentry *parent,
  408. struct debugfs_blob_wrapper *blob)
  409. {
  410. return debugfs_create_file(name, mode, parent, blob, &fops_blob);
  411. }
  412. EXPORT_SYMBOL_GPL(debugfs_create_blob);