file.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. DEFINE_SIMPLE_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
  62. DEFINE_SIMPLE_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
  63. /**
  64. * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
  65. * @name: a pointer to a string containing the name of the file to create.
  66. * @mode: the permission that the file should have
  67. * @parent: a pointer to the parent dentry for this file. This should be a
  68. * directory dentry if set. If this parameter is %NULL, then the
  69. * file will be created in the root of the debugfs filesystem.
  70. * @value: a pointer to the variable that the file should read to and write
  71. * from.
  72. *
  73. * This function creates a file in debugfs with the given name that
  74. * contains the value of the variable @value. If the @mode variable is so
  75. * set, it can be read from, and written to.
  76. *
  77. * This function will return a pointer to a dentry if it succeeds. This
  78. * pointer must be passed to the debugfs_remove() function when the file is
  79. * to be removed (no automatic cleanup happens if your module is unloaded,
  80. * you are responsible here.) If an error occurs, %NULL will be returned.
  81. *
  82. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  83. * returned. It is not wise to check for this value, but rather, check for
  84. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  85. * code.
  86. */
  87. struct dentry *debugfs_create_u8(const char *name, mode_t mode,
  88. struct dentry *parent, u8 *value)
  89. {
  90. /* if there are no write bits set, make read only */
  91. if (!(mode & S_IWUGO))
  92. return debugfs_create_file(name, mode, parent, value, &fops_u8_ro);
  93. /* if there are no read bits set, make write only */
  94. if (!(mode & S_IRUGO))
  95. return debugfs_create_file(name, mode, parent, value, &fops_u8_wo);
  96. return debugfs_create_file(name, mode, parent, value, &fops_u8);
  97. }
  98. EXPORT_SYMBOL_GPL(debugfs_create_u8);
  99. static int debugfs_u16_set(void *data, u64 val)
  100. {
  101. *(u16 *)data = val;
  102. return 0;
  103. }
  104. static int debugfs_u16_get(void *data, u64 *val)
  105. {
  106. *val = *(u16 *)data;
  107. return 0;
  108. }
  109. DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
  110. DEFINE_SIMPLE_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
  111. DEFINE_SIMPLE_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
  112. /**
  113. * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
  114. * @name: a pointer to a string containing the name of the file to create.
  115. * @mode: the permission that the file should have
  116. * @parent: a pointer to the parent dentry for this file. This should be a
  117. * directory dentry if set. If this parameter is %NULL, then the
  118. * file will be created in the root of the debugfs filesystem.
  119. * @value: a pointer to the variable that the file should read to and write
  120. * from.
  121. *
  122. * This function creates a file in debugfs with the given name that
  123. * contains the value of the variable @value. If the @mode variable is so
  124. * set, it can be read from, and written to.
  125. *
  126. * This function will return a pointer to a dentry if it succeeds. This
  127. * pointer must be passed to the debugfs_remove() function when the file is
  128. * to be removed (no automatic cleanup happens if your module is unloaded,
  129. * you are responsible here.) If an error occurs, %NULL will be returned.
  130. *
  131. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  132. * returned. It is not wise to check for this value, but rather, check for
  133. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  134. * code.
  135. */
  136. struct dentry *debugfs_create_u16(const char *name, mode_t mode,
  137. struct dentry *parent, u16 *value)
  138. {
  139. /* if there are no write bits set, make read only */
  140. if (!(mode & S_IWUGO))
  141. return debugfs_create_file(name, mode, parent, value, &fops_u16_ro);
  142. /* if there are no read bits set, make write only */
  143. if (!(mode & S_IRUGO))
  144. return debugfs_create_file(name, mode, parent, value, &fops_u16_wo);
  145. return debugfs_create_file(name, mode, parent, value, &fops_u16);
  146. }
  147. EXPORT_SYMBOL_GPL(debugfs_create_u16);
  148. static int debugfs_u32_set(void *data, u64 val)
  149. {
  150. *(u32 *)data = val;
  151. return 0;
  152. }
  153. static int debugfs_u32_get(void *data, u64 *val)
  154. {
  155. *val = *(u32 *)data;
  156. return 0;
  157. }
  158. DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
  159. DEFINE_SIMPLE_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
  160. DEFINE_SIMPLE_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
  161. /**
  162. * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
  163. * @name: a pointer to a string containing the name of the file to create.
  164. * @mode: the permission that the file should have
  165. * @parent: a pointer to the parent dentry for this file. This should be a
  166. * directory dentry if set. If this parameter is %NULL, then the
  167. * file will be created in the root of the debugfs filesystem.
  168. * @value: a pointer to the variable that the file should read to and write
  169. * from.
  170. *
  171. * This function creates a file in debugfs with the given name that
  172. * contains the value of the variable @value. If the @mode variable is so
  173. * set, it can be read from, and written to.
  174. *
  175. * This function will return a pointer to a dentry if it succeeds. This
  176. * pointer must be passed to the debugfs_remove() function when the file is
  177. * to be removed (no automatic cleanup happens if your module is unloaded,
  178. * you are responsible here.) If an error occurs, %NULL will be returned.
  179. *
  180. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  181. * returned. It is not wise to check for this value, but rather, check for
  182. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  183. * code.
  184. */
  185. struct dentry *debugfs_create_u32(const char *name, mode_t mode,
  186. struct dentry *parent, u32 *value)
  187. {
  188. /* if there are no write bits set, make read only */
  189. if (!(mode & S_IWUGO))
  190. return debugfs_create_file(name, mode, parent, value, &fops_u32_ro);
  191. /* if there are no read bits set, make write only */
  192. if (!(mode & S_IRUGO))
  193. return debugfs_create_file(name, mode, parent, value, &fops_u32_wo);
  194. return debugfs_create_file(name, mode, parent, value, &fops_u32);
  195. }
  196. EXPORT_SYMBOL_GPL(debugfs_create_u32);
  197. static int debugfs_u64_set(void *data, u64 val)
  198. {
  199. *(u64 *)data = val;
  200. return 0;
  201. }
  202. static int debugfs_u64_get(void *data, u64 *val)
  203. {
  204. *val = *(u64 *)data;
  205. return 0;
  206. }
  207. DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
  208. DEFINE_SIMPLE_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
  209. DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
  210. /**
  211. * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
  212. * @name: a pointer to a string containing the name of the file to create.
  213. * @mode: the permission that the file should have
  214. * @parent: a pointer to the parent dentry for this file. This should be a
  215. * directory dentry if set. If this parameter is %NULL, then the
  216. * file will be created in the root of the debugfs filesystem.
  217. * @value: a pointer to the variable that the file should read to and write
  218. * from.
  219. *
  220. * This function creates a file in debugfs with the given name that
  221. * contains the value of the variable @value. If the @mode variable is so
  222. * set, it can be read from, and written to.
  223. *
  224. * This function will return a pointer to a dentry if it succeeds. This
  225. * pointer must be passed to the debugfs_remove() function when the file is
  226. * to be removed (no automatic cleanup happens if your module is unloaded,
  227. * you are responsible here.) If an error occurs, %NULL will be returned.
  228. *
  229. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  230. * returned. It is not wise to check for this value, but rather, check for
  231. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  232. * code.
  233. */
  234. struct dentry *debugfs_create_u64(const char *name, mode_t mode,
  235. struct dentry *parent, u64 *value)
  236. {
  237. /* if there are no write bits set, make read only */
  238. if (!(mode & S_IWUGO))
  239. return debugfs_create_file(name, mode, parent, value, &fops_u64_ro);
  240. /* if there are no read bits set, make write only */
  241. if (!(mode & S_IRUGO))
  242. return debugfs_create_file(name, mode, parent, value, &fops_u64_wo);
  243. return debugfs_create_file(name, mode, parent, value, &fops_u64);
  244. }
  245. EXPORT_SYMBOL_GPL(debugfs_create_u64);
  246. DEFINE_SIMPLE_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
  247. DEFINE_SIMPLE_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
  248. DEFINE_SIMPLE_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
  249. DEFINE_SIMPLE_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%04llx\n");
  250. DEFINE_SIMPLE_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
  251. DEFINE_SIMPLE_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
  252. DEFINE_SIMPLE_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%08llx\n");
  253. DEFINE_SIMPLE_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
  254. DEFINE_SIMPLE_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
  255. /*
  256. * debugfs_create_x{8,16,32} - create a debugfs file that is used to read and write an unsigned {8,16,32}-bit value
  257. *
  258. * These functions are exactly the same as the above functions (but use a hex
  259. * output for the decimal challenged). For details look at the above unsigned
  260. * decimal functions.
  261. */
  262. /**
  263. * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
  264. * @name: a pointer to a string containing the name of the file to create.
  265. * @mode: the permission that the file should have
  266. * @parent: a pointer to the parent dentry for this file. This should be a
  267. * directory dentry if set. If this parameter is %NULL, then the
  268. * file will be created in the root of the debugfs filesystem.
  269. * @value: a pointer to the variable that the file should read to and write
  270. * from.
  271. */
  272. struct dentry *debugfs_create_x8(const char *name, mode_t mode,
  273. struct dentry *parent, u8 *value)
  274. {
  275. /* if there are no write bits set, make read only */
  276. if (!(mode & S_IWUGO))
  277. return debugfs_create_file(name, mode, parent, value, &fops_x8_ro);
  278. /* if there are no read bits set, make write only */
  279. if (!(mode & S_IRUGO))
  280. return debugfs_create_file(name, mode, parent, value, &fops_x8_wo);
  281. return debugfs_create_file(name, mode, parent, value, &fops_x8);
  282. }
  283. EXPORT_SYMBOL_GPL(debugfs_create_x8);
  284. /**
  285. * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit 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_x16(const char *name, mode_t mode,
  295. struct dentry *parent, u16 *value)
  296. {
  297. /* if there are no write bits set, make read only */
  298. if (!(mode & S_IWUGO))
  299. return debugfs_create_file(name, mode, parent, value, &fops_x16_ro);
  300. /* if there are no read bits set, make write only */
  301. if (!(mode & S_IRUGO))
  302. return debugfs_create_file(name, mode, parent, value, &fops_x16_wo);
  303. return debugfs_create_file(name, mode, parent, value, &fops_x16);
  304. }
  305. EXPORT_SYMBOL_GPL(debugfs_create_x16);
  306. /**
  307. * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
  308. * @name: a pointer to a string containing the name of the file to create.
  309. * @mode: the permission that the file should have
  310. * @parent: a pointer to the parent dentry for this file. This should be a
  311. * directory dentry if set. If this parameter is %NULL, then the
  312. * file will be created in the root of the debugfs filesystem.
  313. * @value: a pointer to the variable that the file should read to and write
  314. * from.
  315. */
  316. struct dentry *debugfs_create_x32(const char *name, mode_t mode,
  317. struct dentry *parent, u32 *value)
  318. {
  319. /* if there are no write bits set, make read only */
  320. if (!(mode & S_IWUGO))
  321. return debugfs_create_file(name, mode, parent, value, &fops_x32_ro);
  322. /* if there are no read bits set, make write only */
  323. if (!(mode & S_IRUGO))
  324. return debugfs_create_file(name, mode, parent, value, &fops_x32_wo);
  325. return debugfs_create_file(name, mode, parent, value, &fops_x32);
  326. }
  327. EXPORT_SYMBOL_GPL(debugfs_create_x32);
  328. static int debugfs_size_t_set(void *data, u64 val)
  329. {
  330. *(size_t *)data = val;
  331. return 0;
  332. }
  333. static int debugfs_size_t_get(void *data, u64 *val)
  334. {
  335. *val = *(size_t *)data;
  336. return 0;
  337. }
  338. DEFINE_SIMPLE_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
  339. "%llu\n"); /* %llu and %zu are more or less the same */
  340. /**
  341. * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
  342. * @name: a pointer to a string containing the name of the file to create.
  343. * @mode: the permission that the file should have
  344. * @parent: a pointer to the parent dentry for this file. This should be a
  345. * directory dentry if set. If this parameter is %NULL, then the
  346. * file will be created in the root of the debugfs filesystem.
  347. * @value: a pointer to the variable that the file should read to and write
  348. * from.
  349. */
  350. struct dentry *debugfs_create_size_t(const char *name, mode_t mode,
  351. struct dentry *parent, size_t *value)
  352. {
  353. return debugfs_create_file(name, mode, parent, value, &fops_size_t);
  354. }
  355. EXPORT_SYMBOL_GPL(debugfs_create_size_t);
  356. static ssize_t read_file_bool(struct file *file, char __user *user_buf,
  357. size_t count, loff_t *ppos)
  358. {
  359. char buf[3];
  360. u32 *val = file->private_data;
  361. if (*val)
  362. buf[0] = 'Y';
  363. else
  364. buf[0] = 'N';
  365. buf[1] = '\n';
  366. buf[2] = 0x00;
  367. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  368. }
  369. static ssize_t write_file_bool(struct file *file, const char __user *user_buf,
  370. size_t count, loff_t *ppos)
  371. {
  372. char buf[32];
  373. int buf_size;
  374. u32 *val = file->private_data;
  375. buf_size = min(count, (sizeof(buf)-1));
  376. if (copy_from_user(buf, user_buf, buf_size))
  377. return -EFAULT;
  378. switch (buf[0]) {
  379. case 'y':
  380. case 'Y':
  381. case '1':
  382. *val = 1;
  383. break;
  384. case 'n':
  385. case 'N':
  386. case '0':
  387. *val = 0;
  388. break;
  389. }
  390. return count;
  391. }
  392. static const struct file_operations fops_bool = {
  393. .read = read_file_bool,
  394. .write = write_file_bool,
  395. .open = default_open,
  396. };
  397. /**
  398. * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
  399. * @name: a pointer to a string containing the name of the file to create.
  400. * @mode: the permission that the file should have
  401. * @parent: a pointer to the parent dentry for this file. This should be a
  402. * directory dentry if set. If this parameter is %NULL, then the
  403. * file will be created in the root of the debugfs filesystem.
  404. * @value: a pointer to the variable that the file should read to and write
  405. * from.
  406. *
  407. * This function creates a file in debugfs with the given name that
  408. * contains the value of the variable @value. If the @mode variable is so
  409. * set, it can be read from, and written to.
  410. *
  411. * This function will return a pointer to a dentry if it succeeds. This
  412. * pointer must be passed to the debugfs_remove() function when the file is
  413. * to be removed (no automatic cleanup happens if your module is unloaded,
  414. * you are responsible here.) If an error occurs, %NULL will be returned.
  415. *
  416. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  417. * returned. It is not wise to check for this value, but rather, check for
  418. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  419. * code.
  420. */
  421. struct dentry *debugfs_create_bool(const char *name, mode_t mode,
  422. struct dentry *parent, u32 *value)
  423. {
  424. return debugfs_create_file(name, mode, parent, value, &fops_bool);
  425. }
  426. EXPORT_SYMBOL_GPL(debugfs_create_bool);
  427. static ssize_t read_file_blob(struct file *file, char __user *user_buf,
  428. size_t count, loff_t *ppos)
  429. {
  430. struct debugfs_blob_wrapper *blob = file->private_data;
  431. return simple_read_from_buffer(user_buf, count, ppos, blob->data,
  432. blob->size);
  433. }
  434. static const struct file_operations fops_blob = {
  435. .read = read_file_blob,
  436. .open = default_open,
  437. };
  438. /**
  439. * debugfs_create_blob - create a debugfs file that is used to read a binary blob
  440. * @name: a pointer to a string containing the name of the file to create.
  441. * @mode: the permission that the file should have
  442. * @parent: a pointer to the parent dentry for this file. This should be a
  443. * directory dentry if set. If this parameter is %NULL, then the
  444. * file will be created in the root of the debugfs filesystem.
  445. * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
  446. * to the blob data and the size of the data.
  447. *
  448. * This function creates a file in debugfs with the given name that exports
  449. * @blob->data as a binary blob. If the @mode variable is so set it can be
  450. * read from. Writing is not supported.
  451. *
  452. * This function will return a pointer to a dentry if it succeeds. This
  453. * pointer must be passed to the debugfs_remove() function when the file is
  454. * to be removed (no automatic cleanup happens if your module is unloaded,
  455. * you are responsible here.) If an error occurs, %NULL will be returned.
  456. *
  457. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  458. * returned. It is not wise to check for this value, but rather, check for
  459. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  460. * code.
  461. */
  462. struct dentry *debugfs_create_blob(const char *name, mode_t mode,
  463. struct dentry *parent,
  464. struct debugfs_blob_wrapper *blob)
  465. {
  466. return debugfs_create_file(name, mode, parent, blob, &fops_blob);
  467. }
  468. EXPORT_SYMBOL_GPL(debugfs_create_blob);