file.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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/seq_file.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/debugfs.h>
  20. #include <linux/io.h>
  21. #include <linux/slab.h>
  22. #include <linux/atomic.h>
  23. #include <linux/device.h>
  24. #include <linux/srcu.h>
  25. #include <asm/poll.h>
  26. #include "internal.h"
  27. struct poll_table_struct;
  28. static ssize_t default_read_file(struct file *file, char __user *buf,
  29. size_t count, loff_t *ppos)
  30. {
  31. return 0;
  32. }
  33. static ssize_t default_write_file(struct file *file, const char __user *buf,
  34. size_t count, loff_t *ppos)
  35. {
  36. return count;
  37. }
  38. const struct file_operations debugfs_noop_file_operations = {
  39. .read = default_read_file,
  40. .write = default_write_file,
  41. .open = simple_open,
  42. .llseek = noop_llseek,
  43. };
  44. /**
  45. * debugfs_use_file_start - mark the beginning of file data access
  46. * @dentry: the dentry object whose data is being accessed.
  47. * @srcu_idx: a pointer to some memory to store a SRCU index in.
  48. *
  49. * Up to a matching call to debugfs_use_file_finish(), any
  50. * successive call into the file removing functions debugfs_remove()
  51. * and debugfs_remove_recursive() will block. Since associated private
  52. * file data may only get freed after a successful return of any of
  53. * the removal functions, you may safely access it after a successful
  54. * call to debugfs_use_file_start() without worrying about
  55. * lifetime issues.
  56. *
  57. * If -%EIO is returned, the file has already been removed and thus,
  58. * it is not safe to access any of its data. If, on the other hand,
  59. * it is allowed to access the file data, zero is returned.
  60. *
  61. * Regardless of the return code, any call to
  62. * debugfs_use_file_start() must be followed by a matching call
  63. * to debugfs_use_file_finish().
  64. */
  65. int debugfs_use_file_start(const struct dentry *dentry, int *srcu_idx)
  66. __acquires(&debugfs_srcu)
  67. {
  68. *srcu_idx = srcu_read_lock(&debugfs_srcu);
  69. barrier();
  70. if (d_unlinked(dentry))
  71. return -EIO;
  72. return 0;
  73. }
  74. EXPORT_SYMBOL_GPL(debugfs_use_file_start);
  75. /**
  76. * debugfs_use_file_finish - mark the end of file data access
  77. * @srcu_idx: the SRCU index "created" by a former call to
  78. * debugfs_use_file_start().
  79. *
  80. * Allow any ongoing concurrent call into debugfs_remove() or
  81. * debugfs_remove_recursive() blocked by a former call to
  82. * debugfs_use_file_start() to proceed and return to its caller.
  83. */
  84. void debugfs_use_file_finish(int srcu_idx) __releases(&debugfs_srcu)
  85. {
  86. srcu_read_unlock(&debugfs_srcu, srcu_idx);
  87. }
  88. EXPORT_SYMBOL_GPL(debugfs_use_file_finish);
  89. #define F_DENTRY(filp) ((filp)->f_path.dentry)
  90. #define REAL_FOPS_DEREF(dentry) \
  91. ((const struct file_operations *)(dentry)->d_fsdata)
  92. static int open_proxy_open(struct inode *inode, struct file *filp)
  93. {
  94. const struct dentry *dentry = F_DENTRY(filp);
  95. const struct file_operations *real_fops = NULL;
  96. int srcu_idx, r;
  97. r = debugfs_use_file_start(dentry, &srcu_idx);
  98. if (r) {
  99. r = -ENOENT;
  100. goto out;
  101. }
  102. real_fops = REAL_FOPS_DEREF(dentry);
  103. real_fops = fops_get(real_fops);
  104. if (!real_fops) {
  105. /* Huh? Module did not clean up after itself at exit? */
  106. WARN(1, "debugfs file owner did not clean up at exit: %pd",
  107. dentry);
  108. r = -ENXIO;
  109. goto out;
  110. }
  111. replace_fops(filp, real_fops);
  112. if (real_fops->open)
  113. r = real_fops->open(inode, filp);
  114. out:
  115. fops_put(real_fops);
  116. debugfs_use_file_finish(srcu_idx);
  117. return r;
  118. }
  119. const struct file_operations debugfs_open_proxy_file_operations = {
  120. .open = open_proxy_open,
  121. };
  122. #define PROTO(args...) args
  123. #define ARGS(args...) args
  124. #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args) \
  125. static ret_type full_proxy_ ## name(proto) \
  126. { \
  127. const struct dentry *dentry = F_DENTRY(filp); \
  128. const struct file_operations *real_fops = \
  129. REAL_FOPS_DEREF(dentry); \
  130. int srcu_idx; \
  131. ret_type r; \
  132. \
  133. r = debugfs_use_file_start(dentry, &srcu_idx); \
  134. if (likely(!r)) \
  135. r = real_fops->name(args); \
  136. debugfs_use_file_finish(srcu_idx); \
  137. return r; \
  138. }
  139. FULL_PROXY_FUNC(llseek, loff_t, filp,
  140. PROTO(struct file *filp, loff_t offset, int whence),
  141. ARGS(filp, offset, whence));
  142. FULL_PROXY_FUNC(read, ssize_t, filp,
  143. PROTO(struct file *filp, char __user *buf, size_t size,
  144. loff_t *ppos),
  145. ARGS(filp, buf, size, ppos));
  146. FULL_PROXY_FUNC(write, ssize_t, filp,
  147. PROTO(struct file *filp, const char __user *buf, size_t size,
  148. loff_t *ppos),
  149. ARGS(filp, buf, size, ppos));
  150. FULL_PROXY_FUNC(unlocked_ioctl, long, filp,
  151. PROTO(struct file *filp, unsigned int cmd, unsigned long arg),
  152. ARGS(filp, cmd, arg));
  153. static unsigned int full_proxy_poll(struct file *filp,
  154. struct poll_table_struct *wait)
  155. {
  156. const struct dentry *dentry = F_DENTRY(filp);
  157. const struct file_operations *real_fops = REAL_FOPS_DEREF(dentry);
  158. int srcu_idx;
  159. unsigned int r = 0;
  160. if (debugfs_use_file_start(dentry, &srcu_idx)) {
  161. debugfs_use_file_finish(srcu_idx);
  162. return POLLHUP;
  163. }
  164. r = real_fops->poll(filp, wait);
  165. debugfs_use_file_finish(srcu_idx);
  166. return r;
  167. }
  168. static int full_proxy_release(struct inode *inode, struct file *filp)
  169. {
  170. const struct dentry *dentry = F_DENTRY(filp);
  171. const struct file_operations *real_fops = REAL_FOPS_DEREF(dentry);
  172. const struct file_operations *proxy_fops = filp->f_op;
  173. int r = 0;
  174. /*
  175. * We must not protect this against removal races here: the
  176. * original releaser should be called unconditionally in order
  177. * not to leak any resources. Releasers must not assume that
  178. * ->i_private is still being meaningful here.
  179. */
  180. if (real_fops->release)
  181. r = real_fops->release(inode, filp);
  182. replace_fops(filp, d_inode(dentry)->i_fop);
  183. kfree((void *)proxy_fops);
  184. fops_put(real_fops);
  185. return 0;
  186. }
  187. static void __full_proxy_fops_init(struct file_operations *proxy_fops,
  188. const struct file_operations *real_fops)
  189. {
  190. proxy_fops->release = full_proxy_release;
  191. if (real_fops->llseek)
  192. proxy_fops->llseek = full_proxy_llseek;
  193. if (real_fops->read)
  194. proxy_fops->read = full_proxy_read;
  195. if (real_fops->write)
  196. proxy_fops->write = full_proxy_write;
  197. if (real_fops->poll)
  198. proxy_fops->poll = full_proxy_poll;
  199. if (real_fops->unlocked_ioctl)
  200. proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl;
  201. }
  202. static int full_proxy_open(struct inode *inode, struct file *filp)
  203. {
  204. const struct dentry *dentry = F_DENTRY(filp);
  205. const struct file_operations *real_fops = NULL;
  206. struct file_operations *proxy_fops = NULL;
  207. int srcu_idx, r;
  208. r = debugfs_use_file_start(dentry, &srcu_idx);
  209. if (r) {
  210. r = -ENOENT;
  211. goto out;
  212. }
  213. real_fops = REAL_FOPS_DEREF(dentry);
  214. real_fops = fops_get(real_fops);
  215. if (!real_fops) {
  216. /* Huh? Module did not cleanup after itself at exit? */
  217. WARN(1, "debugfs file owner did not clean up at exit: %pd",
  218. dentry);
  219. r = -ENXIO;
  220. goto out;
  221. }
  222. proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL);
  223. if (!proxy_fops) {
  224. r = -ENOMEM;
  225. goto free_proxy;
  226. }
  227. __full_proxy_fops_init(proxy_fops, real_fops);
  228. replace_fops(filp, proxy_fops);
  229. if (real_fops->open) {
  230. r = real_fops->open(inode, filp);
  231. if (filp->f_op != proxy_fops) {
  232. /* No protection against file removal anymore. */
  233. WARN(1, "debugfs file owner replaced proxy fops: %pd",
  234. dentry);
  235. goto free_proxy;
  236. }
  237. }
  238. goto out;
  239. free_proxy:
  240. kfree(proxy_fops);
  241. fops_put(real_fops);
  242. out:
  243. debugfs_use_file_finish(srcu_idx);
  244. return r;
  245. }
  246. const struct file_operations debugfs_full_proxy_file_operations = {
  247. .open = full_proxy_open,
  248. };
  249. ssize_t debugfs_attr_read(struct file *file, char __user *buf,
  250. size_t len, loff_t *ppos)
  251. {
  252. ssize_t ret;
  253. int srcu_idx;
  254. ret = debugfs_use_file_start(F_DENTRY(file), &srcu_idx);
  255. if (likely(!ret))
  256. ret = simple_attr_read(file, buf, len, ppos);
  257. debugfs_use_file_finish(srcu_idx);
  258. return ret;
  259. }
  260. EXPORT_SYMBOL_GPL(debugfs_attr_read);
  261. ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
  262. size_t len, loff_t *ppos)
  263. {
  264. ssize_t ret;
  265. int srcu_idx;
  266. ret = debugfs_use_file_start(F_DENTRY(file), &srcu_idx);
  267. if (likely(!ret))
  268. ret = simple_attr_write(file, buf, len, ppos);
  269. debugfs_use_file_finish(srcu_idx);
  270. return ret;
  271. }
  272. EXPORT_SYMBOL_GPL(debugfs_attr_write);
  273. static struct dentry *debugfs_create_mode(const char *name, umode_t mode,
  274. struct dentry *parent, void *value,
  275. const struct file_operations *fops,
  276. const struct file_operations *fops_ro,
  277. const struct file_operations *fops_wo)
  278. {
  279. /* if there are no write bits set, make read only */
  280. if (!(mode & S_IWUGO))
  281. return debugfs_create_file(name, mode, parent, value, fops_ro);
  282. /* if there are no read bits set, make write only */
  283. if (!(mode & S_IRUGO))
  284. return debugfs_create_file(name, mode, parent, value, fops_wo);
  285. return debugfs_create_file(name, mode, parent, value, fops);
  286. }
  287. static int debugfs_u8_set(void *data, u64 val)
  288. {
  289. *(u8 *)data = val;
  290. return 0;
  291. }
  292. static int debugfs_u8_get(void *data, u64 *val)
  293. {
  294. *val = *(u8 *)data;
  295. return 0;
  296. }
  297. DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
  298. DEFINE_SIMPLE_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
  299. DEFINE_SIMPLE_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
  300. /**
  301. * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
  302. * @name: a pointer to a string containing the name of the file to create.
  303. * @mode: the permission that the file should have
  304. * @parent: a pointer to the parent dentry for this file. This should be a
  305. * directory dentry if set. If this parameter is %NULL, then the
  306. * file will be created in the root of the debugfs filesystem.
  307. * @value: a pointer to the variable that the file should read to and write
  308. * from.
  309. *
  310. * This function creates a file in debugfs with the given name that
  311. * contains the value of the variable @value. If the @mode variable is so
  312. * set, it can be read from, and written to.
  313. *
  314. * This function will return a pointer to a dentry if it succeeds. This
  315. * pointer must be passed to the debugfs_remove() function when the file is
  316. * to be removed (no automatic cleanup happens if your module is unloaded,
  317. * you are responsible here.) If an error occurs, %NULL will be returned.
  318. *
  319. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  320. * returned. It is not wise to check for this value, but rather, check for
  321. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  322. * code.
  323. */
  324. struct dentry *debugfs_create_u8(const char *name, umode_t mode,
  325. struct dentry *parent, u8 *value)
  326. {
  327. return debugfs_create_mode(name, mode, parent, value, &fops_u8,
  328. &fops_u8_ro, &fops_u8_wo);
  329. }
  330. EXPORT_SYMBOL_GPL(debugfs_create_u8);
  331. static int debugfs_u16_set(void *data, u64 val)
  332. {
  333. *(u16 *)data = val;
  334. return 0;
  335. }
  336. static int debugfs_u16_get(void *data, u64 *val)
  337. {
  338. *val = *(u16 *)data;
  339. return 0;
  340. }
  341. DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
  342. DEFINE_SIMPLE_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
  343. DEFINE_SIMPLE_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
  344. /**
  345. * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
  346. * @name: a pointer to a string containing the name of the file to create.
  347. * @mode: the permission that the file should have
  348. * @parent: a pointer to the parent dentry for this file. This should be a
  349. * directory dentry if set. If this parameter is %NULL, then the
  350. * file will be created in the root of the debugfs filesystem.
  351. * @value: a pointer to the variable that the file should read to and write
  352. * from.
  353. *
  354. * This function creates a file in debugfs with the given name that
  355. * contains the value of the variable @value. If the @mode variable is so
  356. * set, it can be read from, and written to.
  357. *
  358. * This function will return a pointer to a dentry if it succeeds. This
  359. * pointer must be passed to the debugfs_remove() function when the file is
  360. * to be removed (no automatic cleanup happens if your module is unloaded,
  361. * you are responsible here.) If an error occurs, %NULL will be returned.
  362. *
  363. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  364. * returned. It is not wise to check for this value, but rather, check for
  365. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  366. * code.
  367. */
  368. struct dentry *debugfs_create_u16(const char *name, umode_t mode,
  369. struct dentry *parent, u16 *value)
  370. {
  371. return debugfs_create_mode(name, mode, parent, value, &fops_u16,
  372. &fops_u16_ro, &fops_u16_wo);
  373. }
  374. EXPORT_SYMBOL_GPL(debugfs_create_u16);
  375. static int debugfs_u32_set(void *data, u64 val)
  376. {
  377. *(u32 *)data = val;
  378. return 0;
  379. }
  380. static int debugfs_u32_get(void *data, u64 *val)
  381. {
  382. *val = *(u32 *)data;
  383. return 0;
  384. }
  385. DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
  386. DEFINE_SIMPLE_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
  387. DEFINE_SIMPLE_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
  388. /**
  389. * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
  390. * @name: a pointer to a string containing the name of the file to create.
  391. * @mode: the permission that the file should have
  392. * @parent: a pointer to the parent dentry for this file. This should be a
  393. * directory dentry if set. If this parameter is %NULL, then the
  394. * file will be created in the root of the debugfs filesystem.
  395. * @value: a pointer to the variable that the file should read to and write
  396. * from.
  397. *
  398. * This function creates a file in debugfs with the given name that
  399. * contains the value of the variable @value. If the @mode variable is so
  400. * set, it can be read from, and written to.
  401. *
  402. * This function will return a pointer to a dentry if it succeeds. This
  403. * pointer must be passed to the debugfs_remove() function when the file is
  404. * to be removed (no automatic cleanup happens if your module is unloaded,
  405. * you are responsible here.) If an error occurs, %NULL will be returned.
  406. *
  407. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  408. * returned. It is not wise to check for this value, but rather, check for
  409. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  410. * code.
  411. */
  412. struct dentry *debugfs_create_u32(const char *name, umode_t mode,
  413. struct dentry *parent, u32 *value)
  414. {
  415. return debugfs_create_mode(name, mode, parent, value, &fops_u32,
  416. &fops_u32_ro, &fops_u32_wo);
  417. }
  418. EXPORT_SYMBOL_GPL(debugfs_create_u32);
  419. static int debugfs_u64_set(void *data, u64 val)
  420. {
  421. *(u64 *)data = val;
  422. return 0;
  423. }
  424. static int debugfs_u64_get(void *data, u64 *val)
  425. {
  426. *val = *(u64 *)data;
  427. return 0;
  428. }
  429. DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
  430. DEFINE_SIMPLE_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
  431. DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
  432. /**
  433. * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
  434. * @name: a pointer to a string containing the name of the file to create.
  435. * @mode: the permission that the file should have
  436. * @parent: a pointer to the parent dentry for this file. This should be a
  437. * directory dentry if set. If this parameter is %NULL, then the
  438. * file will be created in the root of the debugfs filesystem.
  439. * @value: a pointer to the variable that the file should read to and write
  440. * from.
  441. *
  442. * This function creates a file in debugfs with the given name that
  443. * contains the value of the variable @value. If the @mode variable is so
  444. * set, it can be read from, and written to.
  445. *
  446. * This function will return a pointer to a dentry if it succeeds. This
  447. * pointer must be passed to the debugfs_remove() function when the file is
  448. * to be removed (no automatic cleanup happens if your module is unloaded,
  449. * you are responsible here.) If an error occurs, %NULL will be returned.
  450. *
  451. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  452. * returned. It is not wise to check for this value, but rather, check for
  453. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  454. * code.
  455. */
  456. struct dentry *debugfs_create_u64(const char *name, umode_t mode,
  457. struct dentry *parent, u64 *value)
  458. {
  459. return debugfs_create_mode(name, mode, parent, value, &fops_u64,
  460. &fops_u64_ro, &fops_u64_wo);
  461. }
  462. EXPORT_SYMBOL_GPL(debugfs_create_u64);
  463. static int debugfs_ulong_set(void *data, u64 val)
  464. {
  465. *(unsigned long *)data = val;
  466. return 0;
  467. }
  468. static int debugfs_ulong_get(void *data, u64 *val)
  469. {
  470. *val = *(unsigned long *)data;
  471. return 0;
  472. }
  473. DEFINE_SIMPLE_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set, "%llu\n");
  474. DEFINE_SIMPLE_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
  475. DEFINE_SIMPLE_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
  476. /**
  477. * debugfs_create_ulong - create a debugfs file that is used to read and write
  478. * an unsigned long value.
  479. * @name: a pointer to a string containing the name of the file to create.
  480. * @mode: the permission that the file should have
  481. * @parent: a pointer to the parent dentry for this file. This should be a
  482. * directory dentry if set. If this parameter is %NULL, then the
  483. * file will be created in the root of the debugfs filesystem.
  484. * @value: a pointer to the variable that the file should read to and write
  485. * from.
  486. *
  487. * This function creates a file in debugfs with the given name that
  488. * contains the value of the variable @value. If the @mode variable is so
  489. * set, it can be read from, and written to.
  490. *
  491. * This function will return a pointer to a dentry if it succeeds. This
  492. * pointer must be passed to the debugfs_remove() function when the file is
  493. * to be removed (no automatic cleanup happens if your module is unloaded,
  494. * you are responsible here.) If an error occurs, %NULL will be returned.
  495. *
  496. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  497. * returned. It is not wise to check for this value, but rather, check for
  498. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  499. * code.
  500. */
  501. struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
  502. struct dentry *parent, unsigned long *value)
  503. {
  504. return debugfs_create_mode(name, mode, parent, value, &fops_ulong,
  505. &fops_ulong_ro, &fops_ulong_wo);
  506. }
  507. EXPORT_SYMBOL_GPL(debugfs_create_ulong);
  508. DEFINE_SIMPLE_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
  509. DEFINE_SIMPLE_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
  510. DEFINE_SIMPLE_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
  511. DEFINE_SIMPLE_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%04llx\n");
  512. DEFINE_SIMPLE_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
  513. DEFINE_SIMPLE_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
  514. DEFINE_SIMPLE_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%08llx\n");
  515. DEFINE_SIMPLE_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
  516. DEFINE_SIMPLE_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
  517. DEFINE_SIMPLE_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n");
  518. DEFINE_SIMPLE_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
  519. DEFINE_SIMPLE_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
  520. /*
  521. * debugfs_create_x{8,16,32,64} - create a debugfs file that is used to read and write an unsigned {8,16,32,64}-bit value
  522. *
  523. * These functions are exactly the same as the above functions (but use a hex
  524. * output for the decimal challenged). For details look at the above unsigned
  525. * decimal functions.
  526. */
  527. /**
  528. * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
  529. * @name: a pointer to a string containing the name of the file to create.
  530. * @mode: the permission that the file should have
  531. * @parent: a pointer to the parent dentry for this file. This should be a
  532. * directory dentry if set. If this parameter is %NULL, then the
  533. * file will be created in the root of the debugfs filesystem.
  534. * @value: a pointer to the variable that the file should read to and write
  535. * from.
  536. */
  537. struct dentry *debugfs_create_x8(const char *name, umode_t mode,
  538. struct dentry *parent, u8 *value)
  539. {
  540. return debugfs_create_mode(name, mode, parent, value, &fops_x8,
  541. &fops_x8_ro, &fops_x8_wo);
  542. }
  543. EXPORT_SYMBOL_GPL(debugfs_create_x8);
  544. /**
  545. * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
  546. * @name: a pointer to a string containing the name of the file to create.
  547. * @mode: the permission that the file should have
  548. * @parent: a pointer to the parent dentry for this file. This should be a
  549. * directory dentry if set. If this parameter is %NULL, then the
  550. * file will be created in the root of the debugfs filesystem.
  551. * @value: a pointer to the variable that the file should read to and write
  552. * from.
  553. */
  554. struct dentry *debugfs_create_x16(const char *name, umode_t mode,
  555. struct dentry *parent, u16 *value)
  556. {
  557. return debugfs_create_mode(name, mode, parent, value, &fops_x16,
  558. &fops_x16_ro, &fops_x16_wo);
  559. }
  560. EXPORT_SYMBOL_GPL(debugfs_create_x16);
  561. /**
  562. * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
  563. * @name: a pointer to a string containing the name of the file to create.
  564. * @mode: the permission that the file should have
  565. * @parent: a pointer to the parent dentry for this file. This should be a
  566. * directory dentry if set. If this parameter is %NULL, then the
  567. * file will be created in the root of the debugfs filesystem.
  568. * @value: a pointer to the variable that the file should read to and write
  569. * from.
  570. */
  571. struct dentry *debugfs_create_x32(const char *name, umode_t mode,
  572. struct dentry *parent, u32 *value)
  573. {
  574. return debugfs_create_mode(name, mode, parent, value, &fops_x32,
  575. &fops_x32_ro, &fops_x32_wo);
  576. }
  577. EXPORT_SYMBOL_GPL(debugfs_create_x32);
  578. /**
  579. * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
  580. * @name: a pointer to a string containing the name of the file to create.
  581. * @mode: the permission that the file should have
  582. * @parent: a pointer to the parent dentry for this file. This should be a
  583. * directory dentry if set. If this parameter is %NULL, then the
  584. * file will be created in the root of the debugfs filesystem.
  585. * @value: a pointer to the variable that the file should read to and write
  586. * from.
  587. */
  588. struct dentry *debugfs_create_x64(const char *name, umode_t mode,
  589. struct dentry *parent, u64 *value)
  590. {
  591. return debugfs_create_mode(name, mode, parent, value, &fops_x64,
  592. &fops_x64_ro, &fops_x64_wo);
  593. }
  594. EXPORT_SYMBOL_GPL(debugfs_create_x64);
  595. static int debugfs_size_t_set(void *data, u64 val)
  596. {
  597. *(size_t *)data = val;
  598. return 0;
  599. }
  600. static int debugfs_size_t_get(void *data, u64 *val)
  601. {
  602. *val = *(size_t *)data;
  603. return 0;
  604. }
  605. DEFINE_SIMPLE_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
  606. "%llu\n"); /* %llu and %zu are more or less the same */
  607. DEFINE_SIMPLE_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
  608. DEFINE_SIMPLE_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
  609. /**
  610. * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
  611. * @name: a pointer to a string containing the name of the file to create.
  612. * @mode: the permission that the file should have
  613. * @parent: a pointer to the parent dentry for this file. This should be a
  614. * directory dentry if set. If this parameter is %NULL, then the
  615. * file will be created in the root of the debugfs filesystem.
  616. * @value: a pointer to the variable that the file should read to and write
  617. * from.
  618. */
  619. struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
  620. struct dentry *parent, size_t *value)
  621. {
  622. return debugfs_create_mode(name, mode, parent, value, &fops_size_t,
  623. &fops_size_t_ro, &fops_size_t_wo);
  624. }
  625. EXPORT_SYMBOL_GPL(debugfs_create_size_t);
  626. static int debugfs_atomic_t_set(void *data, u64 val)
  627. {
  628. atomic_set((atomic_t *)data, val);
  629. return 0;
  630. }
  631. static int debugfs_atomic_t_get(void *data, u64 *val)
  632. {
  633. *val = atomic_read((atomic_t *)data);
  634. return 0;
  635. }
  636. DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
  637. debugfs_atomic_t_set, "%lld\n");
  638. DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, "%lld\n");
  639. DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, "%lld\n");
  640. /**
  641. * debugfs_create_atomic_t - create a debugfs file that is used to read and
  642. * write an atomic_t value
  643. * @name: a pointer to a string containing the name of the file to create.
  644. * @mode: the permission that the file should have
  645. * @parent: a pointer to the parent dentry for this file. This should be a
  646. * directory dentry if set. If this parameter is %NULL, then the
  647. * file will be created in the root of the debugfs filesystem.
  648. * @value: a pointer to the variable that the file should read to and write
  649. * from.
  650. */
  651. struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
  652. struct dentry *parent, atomic_t *value)
  653. {
  654. return debugfs_create_mode(name, mode, parent, value, &fops_atomic_t,
  655. &fops_atomic_t_ro, &fops_atomic_t_wo);
  656. }
  657. EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
  658. ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
  659. size_t count, loff_t *ppos)
  660. {
  661. char buf[3];
  662. bool *val = file->private_data;
  663. if (*val)
  664. buf[0] = 'Y';
  665. else
  666. buf[0] = 'N';
  667. buf[1] = '\n';
  668. buf[2] = 0x00;
  669. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  670. }
  671. EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
  672. ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
  673. size_t count, loff_t *ppos)
  674. {
  675. char buf[32];
  676. size_t buf_size;
  677. bool bv;
  678. bool *val = file->private_data;
  679. buf_size = min(count, (sizeof(buf)-1));
  680. if (copy_from_user(buf, user_buf, buf_size))
  681. return -EFAULT;
  682. buf[buf_size] = '\0';
  683. if (strtobool(buf, &bv) == 0)
  684. *val = bv;
  685. return count;
  686. }
  687. EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
  688. static const struct file_operations fops_bool = {
  689. .read = debugfs_read_file_bool,
  690. .write = debugfs_write_file_bool,
  691. .open = simple_open,
  692. .llseek = default_llseek,
  693. };
  694. static const struct file_operations fops_bool_ro = {
  695. .read = debugfs_read_file_bool,
  696. .open = simple_open,
  697. .llseek = default_llseek,
  698. };
  699. static const struct file_operations fops_bool_wo = {
  700. .write = debugfs_write_file_bool,
  701. .open = simple_open,
  702. .llseek = default_llseek,
  703. };
  704. /**
  705. * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
  706. * @name: a pointer to a string containing the name of the file to create.
  707. * @mode: the permission that the file should have
  708. * @parent: a pointer to the parent dentry for this file. This should be a
  709. * directory dentry if set. If this parameter is %NULL, then the
  710. * file will be created in the root of the debugfs filesystem.
  711. * @value: a pointer to the variable that the file should read to and write
  712. * from.
  713. *
  714. * This function creates a file in debugfs with the given name that
  715. * contains the value of the variable @value. If the @mode variable is so
  716. * set, it can be read from, and written to.
  717. *
  718. * This function will return a pointer to a dentry if it succeeds. This
  719. * pointer must be passed to the debugfs_remove() function when the file is
  720. * to be removed (no automatic cleanup happens if your module is unloaded,
  721. * you are responsible here.) If an error occurs, %NULL will be returned.
  722. *
  723. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  724. * returned. It is not wise to check for this value, but rather, check for
  725. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  726. * code.
  727. */
  728. struct dentry *debugfs_create_bool(const char *name, umode_t mode,
  729. struct dentry *parent, bool *value)
  730. {
  731. return debugfs_create_mode(name, mode, parent, value, &fops_bool,
  732. &fops_bool_ro, &fops_bool_wo);
  733. }
  734. EXPORT_SYMBOL_GPL(debugfs_create_bool);
  735. static ssize_t read_file_blob(struct file *file, char __user *user_buf,
  736. size_t count, loff_t *ppos)
  737. {
  738. struct debugfs_blob_wrapper *blob = file->private_data;
  739. return simple_read_from_buffer(user_buf, count, ppos, blob->data,
  740. blob->size);
  741. }
  742. static const struct file_operations fops_blob = {
  743. .read = read_file_blob,
  744. .open = simple_open,
  745. .llseek = default_llseek,
  746. };
  747. /**
  748. * debugfs_create_blob - create a debugfs file that is used to read a binary blob
  749. * @name: a pointer to a string containing the name of the file to create.
  750. * @mode: the permission that the file should have
  751. * @parent: a pointer to the parent dentry for this file. This should be a
  752. * directory dentry if set. If this parameter is %NULL, then the
  753. * file will be created in the root of the debugfs filesystem.
  754. * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
  755. * to the blob data and the size of the data.
  756. *
  757. * This function creates a file in debugfs with the given name that exports
  758. * @blob->data as a binary blob. If the @mode variable is so set it can be
  759. * read from. Writing is not supported.
  760. *
  761. * This function will return a pointer to a dentry if it succeeds. This
  762. * pointer must be passed to the debugfs_remove() function when the file is
  763. * to be removed (no automatic cleanup happens if your module is unloaded,
  764. * you are responsible here.) If an error occurs, %NULL will be returned.
  765. *
  766. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  767. * returned. It is not wise to check for this value, but rather, check for
  768. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  769. * code.
  770. */
  771. struct dentry *debugfs_create_blob(const char *name, umode_t mode,
  772. struct dentry *parent,
  773. struct debugfs_blob_wrapper *blob)
  774. {
  775. return debugfs_create_file(name, mode, parent, blob, &fops_blob);
  776. }
  777. EXPORT_SYMBOL_GPL(debugfs_create_blob);
  778. struct array_data {
  779. void *array;
  780. u32 elements;
  781. };
  782. static size_t u32_format_array(char *buf, size_t bufsize,
  783. u32 *array, int array_size)
  784. {
  785. size_t ret = 0;
  786. while (--array_size >= 0) {
  787. size_t len;
  788. char term = array_size ? ' ' : '\n';
  789. len = snprintf(buf, bufsize, "%u%c", *array++, term);
  790. ret += len;
  791. buf += len;
  792. bufsize -= len;
  793. }
  794. return ret;
  795. }
  796. static int u32_array_open(struct inode *inode, struct file *file)
  797. {
  798. struct array_data *data = inode->i_private;
  799. int size, elements = data->elements;
  800. char *buf;
  801. /*
  802. * Max size:
  803. * - 10 digits + ' '/'\n' = 11 bytes per number
  804. * - terminating NUL character
  805. */
  806. size = elements*11;
  807. buf = kmalloc(size+1, GFP_KERNEL);
  808. if (!buf)
  809. return -ENOMEM;
  810. buf[size] = 0;
  811. file->private_data = buf;
  812. u32_format_array(buf, size, data->array, data->elements);
  813. return nonseekable_open(inode, file);
  814. }
  815. static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
  816. loff_t *ppos)
  817. {
  818. size_t size = strlen(file->private_data);
  819. return simple_read_from_buffer(buf, len, ppos,
  820. file->private_data, size);
  821. }
  822. static int u32_array_release(struct inode *inode, struct file *file)
  823. {
  824. kfree(file->private_data);
  825. return 0;
  826. }
  827. static const struct file_operations u32_array_fops = {
  828. .owner = THIS_MODULE,
  829. .open = u32_array_open,
  830. .release = u32_array_release,
  831. .read = u32_array_read,
  832. .llseek = no_llseek,
  833. };
  834. /**
  835. * debugfs_create_u32_array - create a debugfs file that is used to read u32
  836. * array.
  837. * @name: a pointer to a string containing the name of the file to create.
  838. * @mode: the permission that the file should have.
  839. * @parent: a pointer to the parent dentry for this file. This should be a
  840. * directory dentry if set. If this parameter is %NULL, then the
  841. * file will be created in the root of the debugfs filesystem.
  842. * @array: u32 array that provides data.
  843. * @elements: total number of elements in the array.
  844. *
  845. * This function creates a file in debugfs with the given name that exports
  846. * @array as data. If the @mode variable is so set it can be read from.
  847. * Writing is not supported. Seek within the file is also not supported.
  848. * Once array is created its size can not be changed.
  849. *
  850. * The function returns a pointer to dentry on success. If debugfs is not
  851. * enabled in the kernel, the value -%ENODEV will be returned.
  852. */
  853. struct dentry *debugfs_create_u32_array(const char *name, umode_t mode,
  854. struct dentry *parent,
  855. u32 *array, u32 elements)
  856. {
  857. struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL);
  858. if (data == NULL)
  859. return NULL;
  860. data->array = array;
  861. data->elements = elements;
  862. return debugfs_create_file(name, mode, parent, data, &u32_array_fops);
  863. }
  864. EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
  865. #ifdef CONFIG_HAS_IOMEM
  866. /*
  867. * The regset32 stuff is used to print 32-bit registers using the
  868. * seq_file utilities. We offer printing a register set in an already-opened
  869. * sequential file or create a debugfs file that only prints a regset32.
  870. */
  871. /**
  872. * debugfs_print_regs32 - use seq_print to describe a set of registers
  873. * @s: the seq_file structure being used to generate output
  874. * @regs: an array if struct debugfs_reg32 structures
  875. * @nregs: the length of the above array
  876. * @base: the base address to be used in reading the registers
  877. * @prefix: a string to be prefixed to every output line
  878. *
  879. * This function outputs a text block describing the current values of
  880. * some 32-bit hardware registers. It is meant to be used within debugfs
  881. * files based on seq_file that need to show registers, intermixed with other
  882. * information. The prefix argument may be used to specify a leading string,
  883. * because some peripherals have several blocks of identical registers,
  884. * for example configuration of dma channels
  885. */
  886. void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
  887. int nregs, void __iomem *base, char *prefix)
  888. {
  889. int i;
  890. for (i = 0; i < nregs; i++, regs++) {
  891. if (prefix)
  892. seq_printf(s, "%s", prefix);
  893. seq_printf(s, "%s = 0x%08x\n", regs->name,
  894. readl(base + regs->offset));
  895. if (seq_has_overflowed(s))
  896. break;
  897. }
  898. }
  899. EXPORT_SYMBOL_GPL(debugfs_print_regs32);
  900. static int debugfs_show_regset32(struct seq_file *s, void *data)
  901. {
  902. struct debugfs_regset32 *regset = s->private;
  903. debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
  904. return 0;
  905. }
  906. static int debugfs_open_regset32(struct inode *inode, struct file *file)
  907. {
  908. return single_open(file, debugfs_show_regset32, inode->i_private);
  909. }
  910. static const struct file_operations fops_regset32 = {
  911. .open = debugfs_open_regset32,
  912. .read = seq_read,
  913. .llseek = seq_lseek,
  914. .release = single_release,
  915. };
  916. /**
  917. * debugfs_create_regset32 - create a debugfs file that returns register values
  918. * @name: a pointer to a string containing the name of the file to create.
  919. * @mode: the permission that the file should have
  920. * @parent: a pointer to the parent dentry for this file. This should be a
  921. * directory dentry if set. If this parameter is %NULL, then the
  922. * file will be created in the root of the debugfs filesystem.
  923. * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
  924. * to an array of register definitions, the array size and the base
  925. * address where the register bank is to be found.
  926. *
  927. * This function creates a file in debugfs with the given name that reports
  928. * the names and values of a set of 32-bit registers. If the @mode variable
  929. * is so set it can be read from. Writing is not supported.
  930. *
  931. * This function will return a pointer to a dentry if it succeeds. This
  932. * pointer must be passed to the debugfs_remove() function when the file is
  933. * to be removed (no automatic cleanup happens if your module is unloaded,
  934. * you are responsible here.) If an error occurs, %NULL will be returned.
  935. *
  936. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  937. * returned. It is not wise to check for this value, but rather, check for
  938. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  939. * code.
  940. */
  941. struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
  942. struct dentry *parent,
  943. struct debugfs_regset32 *regset)
  944. {
  945. return debugfs_create_file(name, mode, parent, regset, &fops_regset32);
  946. }
  947. EXPORT_SYMBOL_GPL(debugfs_create_regset32);
  948. #endif /* CONFIG_HAS_IOMEM */
  949. struct debugfs_devm_entry {
  950. int (*read)(struct seq_file *seq, void *data);
  951. struct device *dev;
  952. };
  953. static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
  954. {
  955. struct debugfs_devm_entry *entry = inode->i_private;
  956. return single_open(f, entry->read, entry->dev);
  957. }
  958. static const struct file_operations debugfs_devm_entry_ops = {
  959. .owner = THIS_MODULE,
  960. .open = debugfs_devm_entry_open,
  961. .release = single_release,
  962. .read = seq_read,
  963. .llseek = seq_lseek
  964. };
  965. /**
  966. * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
  967. *
  968. * @dev: device related to this debugfs file.
  969. * @name: name of the debugfs file.
  970. * @parent: a pointer to the parent dentry for this file. This should be a
  971. * directory dentry if set. If this parameter is %NULL, then the
  972. * file will be created in the root of the debugfs filesystem.
  973. * @read_fn: function pointer called to print the seq_file content.
  974. */
  975. struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
  976. struct dentry *parent,
  977. int (*read_fn)(struct seq_file *s,
  978. void *data))
  979. {
  980. struct debugfs_devm_entry *entry;
  981. if (IS_ERR(parent))
  982. return ERR_PTR(-ENOENT);
  983. entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
  984. if (!entry)
  985. return ERR_PTR(-ENOMEM);
  986. entry->read = read_fn;
  987. entry->dev = dev;
  988. return debugfs_create_file(name, S_IRUGO, parent, entry,
  989. &debugfs_devm_entry_ops);
  990. }
  991. EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);