file.c 15 KB

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