file.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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/debugfs.h>
  19. static ssize_t default_read_file(struct file *file, char __user *buf,
  20. size_t count, loff_t *ppos)
  21. {
  22. return 0;
  23. }
  24. static ssize_t default_write_file(struct file *file, const char __user *buf,
  25. size_t count, loff_t *ppos)
  26. {
  27. return count;
  28. }
  29. static int default_open(struct inode *inode, struct file *file)
  30. {
  31. if (inode->i_private)
  32. file->private_data = inode->i_private;
  33. return 0;
  34. }
  35. const struct file_operations debugfs_file_operations = {
  36. .read = default_read_file,
  37. .write = default_write_file,
  38. .open = default_open,
  39. };
  40. static void debugfs_u8_set(void *data, u64 val)
  41. {
  42. *(u8 *)data = val;
  43. }
  44. static u64 debugfs_u8_get(void *data)
  45. {
  46. return *(u8 *)data;
  47. }
  48. DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
  49. /**
  50. * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
  51. * @name: a pointer to a string containing the name of the file to create.
  52. * @mode: the permission that the file should have
  53. * @parent: a pointer to the parent dentry for this file. This should be a
  54. * directory dentry if set. If this parameter is %NULL, then the
  55. * file will be created in the root of the debugfs filesystem.
  56. * @value: a pointer to the variable that the file should read to and write
  57. * from.
  58. *
  59. * This function creates a file in debugfs with the given name that
  60. * contains the value of the variable @value. If the @mode variable is so
  61. * set, it can be read from, and written to.
  62. *
  63. * This function will return a pointer to a dentry if it succeeds. This
  64. * pointer must be passed to the debugfs_remove() function when the file is
  65. * to be removed (no automatic cleanup happens if your module is unloaded,
  66. * you are responsible here.) If an error occurs, %NULL will be returned.
  67. *
  68. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  69. * returned. It is not wise to check for this value, but rather, check for
  70. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  71. * code.
  72. */
  73. struct dentry *debugfs_create_u8(const char *name, mode_t mode,
  74. struct dentry *parent, u8 *value)
  75. {
  76. return debugfs_create_file(name, mode, parent, value, &fops_u8);
  77. }
  78. EXPORT_SYMBOL_GPL(debugfs_create_u8);
  79. static void debugfs_u16_set(void *data, u64 val)
  80. {
  81. *(u16 *)data = val;
  82. }
  83. static u64 debugfs_u16_get(void *data)
  84. {
  85. return *(u16 *)data;
  86. }
  87. DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
  88. /**
  89. * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
  90. * @name: a pointer to a string containing the name of the file to create.
  91. * @mode: the permission that the file should have
  92. * @parent: a pointer to the parent dentry for this file. This should be a
  93. * directory dentry if set. If this parameter is %NULL, then the
  94. * file will be created in the root of the debugfs filesystem.
  95. * @value: a pointer to the variable that the file should read to and write
  96. * from.
  97. *
  98. * This function creates a file in debugfs with the given name that
  99. * contains the value of the variable @value. If the @mode variable is so
  100. * set, it can be read from, and written to.
  101. *
  102. * This function will return a pointer to a dentry if it succeeds. This
  103. * pointer must be passed to the debugfs_remove() function when the file is
  104. * to be removed (no automatic cleanup happens if your module is unloaded,
  105. * you are responsible here.) If an error occurs, %NULL will be returned.
  106. *
  107. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  108. * returned. It is not wise to check for this value, but rather, check for
  109. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  110. * code.
  111. */
  112. struct dentry *debugfs_create_u16(const char *name, mode_t mode,
  113. struct dentry *parent, u16 *value)
  114. {
  115. return debugfs_create_file(name, mode, parent, value, &fops_u16);
  116. }
  117. EXPORT_SYMBOL_GPL(debugfs_create_u16);
  118. static void debugfs_u32_set(void *data, u64 val)
  119. {
  120. *(u32 *)data = val;
  121. }
  122. static u64 debugfs_u32_get(void *data)
  123. {
  124. return *(u32 *)data;
  125. }
  126. DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
  127. /**
  128. * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
  129. * @name: a pointer to a string containing the name of the file to create.
  130. * @mode: the permission that the file should have
  131. * @parent: a pointer to the parent dentry for this file. This should be a
  132. * directory dentry if set. If this parameter is %NULL, then the
  133. * file will be created in the root of the debugfs filesystem.
  134. * @value: a pointer to the variable that the file should read to and write
  135. * from.
  136. *
  137. * This function creates a file in debugfs with the given name that
  138. * contains the value of the variable @value. If the @mode variable is so
  139. * set, it can be read from, and written to.
  140. *
  141. * This function will return a pointer to a dentry if it succeeds. This
  142. * pointer must be passed to the debugfs_remove() function when the file is
  143. * to be removed (no automatic cleanup happens if your module is unloaded,
  144. * you are responsible here.) If an error occurs, %NULL will be returned.
  145. *
  146. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  147. * returned. It is not wise to check for this value, but rather, check for
  148. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  149. * code.
  150. */
  151. struct dentry *debugfs_create_u32(const char *name, mode_t mode,
  152. struct dentry *parent, u32 *value)
  153. {
  154. return debugfs_create_file(name, mode, parent, value, &fops_u32);
  155. }
  156. EXPORT_SYMBOL_GPL(debugfs_create_u32);
  157. static ssize_t read_file_bool(struct file *file, char __user *user_buf,
  158. size_t count, loff_t *ppos)
  159. {
  160. char buf[3];
  161. u32 *val = file->private_data;
  162. if (*val)
  163. buf[0] = 'Y';
  164. else
  165. buf[0] = 'N';
  166. buf[1] = '\n';
  167. buf[2] = 0x00;
  168. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  169. }
  170. static ssize_t write_file_bool(struct file *file, const char __user *user_buf,
  171. size_t count, loff_t *ppos)
  172. {
  173. char buf[32];
  174. int buf_size;
  175. u32 *val = file->private_data;
  176. buf_size = min(count, (sizeof(buf)-1));
  177. if (copy_from_user(buf, user_buf, buf_size))
  178. return -EFAULT;
  179. switch (buf[0]) {
  180. case 'y':
  181. case 'Y':
  182. case '1':
  183. *val = 1;
  184. break;
  185. case 'n':
  186. case 'N':
  187. case '0':
  188. *val = 0;
  189. break;
  190. }
  191. return count;
  192. }
  193. static const struct file_operations fops_bool = {
  194. .read = read_file_bool,
  195. .write = write_file_bool,
  196. .open = default_open,
  197. };
  198. /**
  199. * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
  200. * @name: a pointer to a string containing the name of the file to create.
  201. * @mode: the permission that the file should have
  202. * @parent: a pointer to the parent dentry for this file. This should be a
  203. * directory dentry if set. If this parameter is %NULL, then the
  204. * file will be created in the root of the debugfs filesystem.
  205. * @value: a pointer to the variable that the file should read to and write
  206. * from.
  207. *
  208. * This function creates a file in debugfs with the given name that
  209. * contains the value of the variable @value. If the @mode variable is so
  210. * set, it can be read from, and written to.
  211. *
  212. * This function will return a pointer to a dentry if it succeeds. This
  213. * pointer must be passed to the debugfs_remove() function when the file is
  214. * to be removed (no automatic cleanup happens if your module is unloaded,
  215. * you are responsible here.) If an error occurs, %NULL will be returned.
  216. *
  217. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  218. * returned. It is not wise to check for this value, but rather, check for
  219. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  220. * code.
  221. */
  222. struct dentry *debugfs_create_bool(const char *name, mode_t mode,
  223. struct dentry *parent, u32 *value)
  224. {
  225. return debugfs_create_file(name, mode, parent, value, &fops_bool);
  226. }
  227. EXPORT_SYMBOL_GPL(debugfs_create_bool);
  228. static ssize_t read_file_blob(struct file *file, char __user *user_buf,
  229. size_t count, loff_t *ppos)
  230. {
  231. struct debugfs_blob_wrapper *blob = file->private_data;
  232. return simple_read_from_buffer(user_buf, count, ppos, blob->data,
  233. blob->size);
  234. }
  235. static struct file_operations fops_blob = {
  236. .read = read_file_blob,
  237. .open = default_open,
  238. };
  239. /**
  240. * debugfs_create_blob - create a debugfs file that is used to read and write a binary blob
  241. * @name: a pointer to a string containing the name of the file to create.
  242. * @mode: the permission that the file should have
  243. * @parent: a pointer to the parent dentry for this file. This should be a
  244. * directory dentry if set. If this parameter is %NULL, then the
  245. * file will be created in the root of the debugfs filesystem.
  246. * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
  247. * to the blob data and the size of the data.
  248. *
  249. * This function creates a file in debugfs with the given name that exports
  250. * @blob->data as a binary blob. If the @mode variable is so set it can be
  251. * read from. Writing is not supported.
  252. *
  253. * This function will return a pointer to a dentry if it succeeds. This
  254. * pointer must be passed to the debugfs_remove() function when the file is
  255. * to be removed (no automatic cleanup happens if your module is unloaded,
  256. * you are responsible here.) If an error occurs, %NULL will be returned.
  257. *
  258. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  259. * returned. It is not wise to check for this value, but rather, check for
  260. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  261. * code.
  262. */
  263. struct dentry *debugfs_create_blob(const char *name, mode_t mode,
  264. struct dentry *parent,
  265. struct debugfs_blob_wrapper *blob)
  266. {
  267. return debugfs_create_file(name, mode, parent, blob, &fops_blob);
  268. }
  269. EXPORT_SYMBOL_GPL(debugfs_create_blob);