file.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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. static struct dentry *debugfs_create_mode(const char *name, umode_t mode,
  250. struct dentry *parent, void *value,
  251. const struct file_operations *fops,
  252. const struct file_operations *fops_ro,
  253. const struct file_operations *fops_wo)
  254. {
  255. /* if there are no write bits set, make read only */
  256. if (!(mode & S_IWUGO))
  257. return debugfs_create_file(name, mode, parent, value, fops_ro);
  258. /* if there are no read bits set, make write only */
  259. if (!(mode & S_IRUGO))
  260. return debugfs_create_file(name, mode, parent, value, fops_wo);
  261. return debugfs_create_file(name, mode, parent, value, fops);
  262. }
  263. static int debugfs_u8_set(void *data, u64 val)
  264. {
  265. *(u8 *)data = val;
  266. return 0;
  267. }
  268. static int debugfs_u8_get(void *data, u64 *val)
  269. {
  270. *val = *(u8 *)data;
  271. return 0;
  272. }
  273. DEFINE_SIMPLE_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
  274. DEFINE_SIMPLE_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
  275. DEFINE_SIMPLE_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
  276. /**
  277. * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
  278. * @name: a pointer to a string containing the name of the file to create.
  279. * @mode: the permission that the file should have
  280. * @parent: a pointer to the parent dentry for this file. This should be a
  281. * directory dentry if set. If this parameter is %NULL, then the
  282. * file will be created in the root of the debugfs filesystem.
  283. * @value: a pointer to the variable that the file should read to and write
  284. * from.
  285. *
  286. * This function creates a file in debugfs with the given name that
  287. * contains the value of the variable @value. If the @mode variable is so
  288. * set, it can be read from, and written to.
  289. *
  290. * This function will return a pointer to a dentry if it succeeds. This
  291. * pointer must be passed to the debugfs_remove() function when the file is
  292. * to be removed (no automatic cleanup happens if your module is unloaded,
  293. * you are responsible here.) If an error occurs, %NULL will be returned.
  294. *
  295. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  296. * returned. It is not wise to check for this value, but rather, check for
  297. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  298. * code.
  299. */
  300. struct dentry *debugfs_create_u8(const char *name, umode_t mode,
  301. struct dentry *parent, u8 *value)
  302. {
  303. return debugfs_create_mode(name, mode, parent, value, &fops_u8,
  304. &fops_u8_ro, &fops_u8_wo);
  305. }
  306. EXPORT_SYMBOL_GPL(debugfs_create_u8);
  307. static int debugfs_u16_set(void *data, u64 val)
  308. {
  309. *(u16 *)data = val;
  310. return 0;
  311. }
  312. static int debugfs_u16_get(void *data, u64 *val)
  313. {
  314. *val = *(u16 *)data;
  315. return 0;
  316. }
  317. DEFINE_SIMPLE_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
  318. DEFINE_SIMPLE_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
  319. DEFINE_SIMPLE_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
  320. /**
  321. * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
  322. * @name: a pointer to a string containing the name of the file to create.
  323. * @mode: the permission that the file should have
  324. * @parent: a pointer to the parent dentry for this file. This should be a
  325. * directory dentry if set. If this parameter is %NULL, then the
  326. * file will be created in the root of the debugfs filesystem.
  327. * @value: a pointer to the variable that the file should read to and write
  328. * from.
  329. *
  330. * This function creates a file in debugfs with the given name that
  331. * contains the value of the variable @value. If the @mode variable is so
  332. * set, it can be read from, and written to.
  333. *
  334. * This function will return a pointer to a dentry if it succeeds. This
  335. * pointer must be passed to the debugfs_remove() function when the file is
  336. * to be removed (no automatic cleanup happens if your module is unloaded,
  337. * you are responsible here.) If an error occurs, %NULL will be returned.
  338. *
  339. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  340. * returned. It is not wise to check for this value, but rather, check for
  341. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  342. * code.
  343. */
  344. struct dentry *debugfs_create_u16(const char *name, umode_t mode,
  345. struct dentry *parent, u16 *value)
  346. {
  347. return debugfs_create_mode(name, mode, parent, value, &fops_u16,
  348. &fops_u16_ro, &fops_u16_wo);
  349. }
  350. EXPORT_SYMBOL_GPL(debugfs_create_u16);
  351. static int debugfs_u32_set(void *data, u64 val)
  352. {
  353. *(u32 *)data = val;
  354. return 0;
  355. }
  356. static int debugfs_u32_get(void *data, u64 *val)
  357. {
  358. *val = *(u32 *)data;
  359. return 0;
  360. }
  361. DEFINE_SIMPLE_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
  362. DEFINE_SIMPLE_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
  363. DEFINE_SIMPLE_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
  364. /**
  365. * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
  366. * @name: a pointer to a string containing the name of the file to create.
  367. * @mode: the permission that the file should have
  368. * @parent: a pointer to the parent dentry for this file. This should be a
  369. * directory dentry if set. If this parameter is %NULL, then the
  370. * file will be created in the root of the debugfs filesystem.
  371. * @value: a pointer to the variable that the file should read to and write
  372. * from.
  373. *
  374. * This function creates a file in debugfs with the given name that
  375. * contains the value of the variable @value. If the @mode variable is so
  376. * set, it can be read from, and written to.
  377. *
  378. * This function will return a pointer to a dentry if it succeeds. This
  379. * pointer must be passed to the debugfs_remove() function when the file is
  380. * to be removed (no automatic cleanup happens if your module is unloaded,
  381. * you are responsible here.) If an error occurs, %NULL will be returned.
  382. *
  383. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  384. * returned. It is not wise to check for this value, but rather, check for
  385. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  386. * code.
  387. */
  388. struct dentry *debugfs_create_u32(const char *name, umode_t mode,
  389. struct dentry *parent, u32 *value)
  390. {
  391. return debugfs_create_mode(name, mode, parent, value, &fops_u32,
  392. &fops_u32_ro, &fops_u32_wo);
  393. }
  394. EXPORT_SYMBOL_GPL(debugfs_create_u32);
  395. static int debugfs_u64_set(void *data, u64 val)
  396. {
  397. *(u64 *)data = val;
  398. return 0;
  399. }
  400. static int debugfs_u64_get(void *data, u64 *val)
  401. {
  402. *val = *(u64 *)data;
  403. return 0;
  404. }
  405. DEFINE_SIMPLE_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
  406. DEFINE_SIMPLE_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
  407. DEFINE_SIMPLE_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
  408. /**
  409. * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
  410. * @name: a pointer to a string containing the name of the file to create.
  411. * @mode: the permission that the file should have
  412. * @parent: a pointer to the parent dentry for this file. This should be a
  413. * directory dentry if set. If this parameter is %NULL, then the
  414. * file will be created in the root of the debugfs filesystem.
  415. * @value: a pointer to the variable that the file should read to and write
  416. * from.
  417. *
  418. * This function creates a file in debugfs with the given name that
  419. * contains the value of the variable @value. If the @mode variable is so
  420. * set, it can be read from, and written to.
  421. *
  422. * This function will return a pointer to a dentry if it succeeds. This
  423. * pointer must be passed to the debugfs_remove() function when the file is
  424. * to be removed (no automatic cleanup happens if your module is unloaded,
  425. * you are responsible here.) If an error occurs, %NULL will be returned.
  426. *
  427. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  428. * returned. It is not wise to check for this value, but rather, check for
  429. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  430. * code.
  431. */
  432. struct dentry *debugfs_create_u64(const char *name, umode_t mode,
  433. struct dentry *parent, u64 *value)
  434. {
  435. return debugfs_create_mode(name, mode, parent, value, &fops_u64,
  436. &fops_u64_ro, &fops_u64_wo);
  437. }
  438. EXPORT_SYMBOL_GPL(debugfs_create_u64);
  439. static int debugfs_ulong_set(void *data, u64 val)
  440. {
  441. *(unsigned long *)data = val;
  442. return 0;
  443. }
  444. static int debugfs_ulong_get(void *data, u64 *val)
  445. {
  446. *val = *(unsigned long *)data;
  447. return 0;
  448. }
  449. DEFINE_SIMPLE_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set, "%llu\n");
  450. DEFINE_SIMPLE_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
  451. DEFINE_SIMPLE_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
  452. /**
  453. * debugfs_create_ulong - create a debugfs file that is used to read and write
  454. * an unsigned long value.
  455. * @name: a pointer to a string containing the name of the file to create.
  456. * @mode: the permission that the file should have
  457. * @parent: a pointer to the parent dentry for this file. This should be a
  458. * directory dentry if set. If this parameter is %NULL, then the
  459. * file will be created in the root of the debugfs filesystem.
  460. * @value: a pointer to the variable that the file should read to and write
  461. * from.
  462. *
  463. * This function creates a file in debugfs with the given name that
  464. * contains the value of the variable @value. If the @mode variable is so
  465. * set, it can be read from, and written to.
  466. *
  467. * This function will return a pointer to a dentry if it succeeds. This
  468. * pointer must be passed to the debugfs_remove() function when the file is
  469. * to be removed (no automatic cleanup happens if your module is unloaded,
  470. * you are responsible here.) If an error occurs, %NULL will be returned.
  471. *
  472. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  473. * returned. It is not wise to check for this value, but rather, check for
  474. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  475. * code.
  476. */
  477. struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
  478. struct dentry *parent, unsigned long *value)
  479. {
  480. return debugfs_create_mode(name, mode, parent, value, &fops_ulong,
  481. &fops_ulong_ro, &fops_ulong_wo);
  482. }
  483. EXPORT_SYMBOL_GPL(debugfs_create_ulong);
  484. DEFINE_SIMPLE_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
  485. DEFINE_SIMPLE_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
  486. DEFINE_SIMPLE_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
  487. DEFINE_SIMPLE_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, "0x%04llx\n");
  488. DEFINE_SIMPLE_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
  489. DEFINE_SIMPLE_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
  490. DEFINE_SIMPLE_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, "0x%08llx\n");
  491. DEFINE_SIMPLE_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
  492. DEFINE_SIMPLE_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
  493. DEFINE_SIMPLE_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, "0x%016llx\n");
  494. DEFINE_SIMPLE_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
  495. DEFINE_SIMPLE_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
  496. /*
  497. * 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
  498. *
  499. * These functions are exactly the same as the above functions (but use a hex
  500. * output for the decimal challenged). For details look at the above unsigned
  501. * decimal functions.
  502. */
  503. /**
  504. * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
  505. * @name: a pointer to a string containing the name of the file to create.
  506. * @mode: the permission that the file should have
  507. * @parent: a pointer to the parent dentry for this file. This should be a
  508. * directory dentry if set. If this parameter is %NULL, then the
  509. * file will be created in the root of the debugfs filesystem.
  510. * @value: a pointer to the variable that the file should read to and write
  511. * from.
  512. */
  513. struct dentry *debugfs_create_x8(const char *name, umode_t mode,
  514. struct dentry *parent, u8 *value)
  515. {
  516. return debugfs_create_mode(name, mode, parent, value, &fops_x8,
  517. &fops_x8_ro, &fops_x8_wo);
  518. }
  519. EXPORT_SYMBOL_GPL(debugfs_create_x8);
  520. /**
  521. * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
  522. * @name: a pointer to a string containing the name of the file to create.
  523. * @mode: the permission that the file should have
  524. * @parent: a pointer to the parent dentry for this file. This should be a
  525. * directory dentry if set. If this parameter is %NULL, then the
  526. * file will be created in the root of the debugfs filesystem.
  527. * @value: a pointer to the variable that the file should read to and write
  528. * from.
  529. */
  530. struct dentry *debugfs_create_x16(const char *name, umode_t mode,
  531. struct dentry *parent, u16 *value)
  532. {
  533. return debugfs_create_mode(name, mode, parent, value, &fops_x16,
  534. &fops_x16_ro, &fops_x16_wo);
  535. }
  536. EXPORT_SYMBOL_GPL(debugfs_create_x16);
  537. /**
  538. * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
  539. * @name: a pointer to a string containing the name of the file to create.
  540. * @mode: the permission that the file should have
  541. * @parent: a pointer to the parent dentry for this file. This should be a
  542. * directory dentry if set. If this parameter is %NULL, then the
  543. * file will be created in the root of the debugfs filesystem.
  544. * @value: a pointer to the variable that the file should read to and write
  545. * from.
  546. */
  547. struct dentry *debugfs_create_x32(const char *name, umode_t mode,
  548. struct dentry *parent, u32 *value)
  549. {
  550. return debugfs_create_mode(name, mode, parent, value, &fops_x32,
  551. &fops_x32_ro, &fops_x32_wo);
  552. }
  553. EXPORT_SYMBOL_GPL(debugfs_create_x32);
  554. /**
  555. * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
  556. * @name: a pointer to a string containing the name of the file to create.
  557. * @mode: the permission that the file should have
  558. * @parent: a pointer to the parent dentry for this file. This should be a
  559. * directory dentry if set. If this parameter is %NULL, then the
  560. * file will be created in the root of the debugfs filesystem.
  561. * @value: a pointer to the variable that the file should read to and write
  562. * from.
  563. */
  564. struct dentry *debugfs_create_x64(const char *name, umode_t mode,
  565. struct dentry *parent, u64 *value)
  566. {
  567. return debugfs_create_mode(name, mode, parent, value, &fops_x64,
  568. &fops_x64_ro, &fops_x64_wo);
  569. }
  570. EXPORT_SYMBOL_GPL(debugfs_create_x64);
  571. static int debugfs_size_t_set(void *data, u64 val)
  572. {
  573. *(size_t *)data = val;
  574. return 0;
  575. }
  576. static int debugfs_size_t_get(void *data, u64 *val)
  577. {
  578. *val = *(size_t *)data;
  579. return 0;
  580. }
  581. DEFINE_SIMPLE_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
  582. "%llu\n"); /* %llu and %zu are more or less the same */
  583. DEFINE_SIMPLE_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
  584. DEFINE_SIMPLE_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
  585. /**
  586. * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
  587. * @name: a pointer to a string containing the name of the file to create.
  588. * @mode: the permission that the file should have
  589. * @parent: a pointer to the parent dentry for this file. This should be a
  590. * directory dentry if set. If this parameter is %NULL, then the
  591. * file will be created in the root of the debugfs filesystem.
  592. * @value: a pointer to the variable that the file should read to and write
  593. * from.
  594. */
  595. struct dentry *debugfs_create_size_t(const char *name, umode_t mode,
  596. struct dentry *parent, size_t *value)
  597. {
  598. return debugfs_create_mode(name, mode, parent, value, &fops_size_t,
  599. &fops_size_t_ro, &fops_size_t_wo);
  600. }
  601. EXPORT_SYMBOL_GPL(debugfs_create_size_t);
  602. static int debugfs_atomic_t_set(void *data, u64 val)
  603. {
  604. atomic_set((atomic_t *)data, val);
  605. return 0;
  606. }
  607. static int debugfs_atomic_t_get(void *data, u64 *val)
  608. {
  609. *val = atomic_read((atomic_t *)data);
  610. return 0;
  611. }
  612. DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
  613. debugfs_atomic_t_set, "%lld\n");
  614. DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, "%lld\n");
  615. DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, "%lld\n");
  616. /**
  617. * debugfs_create_atomic_t - create a debugfs file that is used to read and
  618. * write an atomic_t value
  619. * @name: a pointer to a string containing the name of the file to create.
  620. * @mode: the permission that the file should have
  621. * @parent: a pointer to the parent dentry for this file. This should be a
  622. * directory dentry if set. If this parameter is %NULL, then the
  623. * file will be created in the root of the debugfs filesystem.
  624. * @value: a pointer to the variable that the file should read to and write
  625. * from.
  626. */
  627. struct dentry *debugfs_create_atomic_t(const char *name, umode_t mode,
  628. struct dentry *parent, atomic_t *value)
  629. {
  630. return debugfs_create_mode(name, mode, parent, value, &fops_atomic_t,
  631. &fops_atomic_t_ro, &fops_atomic_t_wo);
  632. }
  633. EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
  634. ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
  635. size_t count, loff_t *ppos)
  636. {
  637. char buf[3];
  638. bool *val = file->private_data;
  639. if (*val)
  640. buf[0] = 'Y';
  641. else
  642. buf[0] = 'N';
  643. buf[1] = '\n';
  644. buf[2] = 0x00;
  645. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  646. }
  647. EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
  648. ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
  649. size_t count, loff_t *ppos)
  650. {
  651. char buf[32];
  652. size_t buf_size;
  653. bool bv;
  654. bool *val = file->private_data;
  655. buf_size = min(count, (sizeof(buf)-1));
  656. if (copy_from_user(buf, user_buf, buf_size))
  657. return -EFAULT;
  658. buf[buf_size] = '\0';
  659. if (strtobool(buf, &bv) == 0)
  660. *val = bv;
  661. return count;
  662. }
  663. EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
  664. static const struct file_operations fops_bool = {
  665. .read = debugfs_read_file_bool,
  666. .write = debugfs_write_file_bool,
  667. .open = simple_open,
  668. .llseek = default_llseek,
  669. };
  670. static const struct file_operations fops_bool_ro = {
  671. .read = debugfs_read_file_bool,
  672. .open = simple_open,
  673. .llseek = default_llseek,
  674. };
  675. static const struct file_operations fops_bool_wo = {
  676. .write = debugfs_write_file_bool,
  677. .open = simple_open,
  678. .llseek = default_llseek,
  679. };
  680. /**
  681. * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
  682. * @name: a pointer to a string containing the name of the file to create.
  683. * @mode: the permission that the file should have
  684. * @parent: a pointer to the parent dentry for this file. This should be a
  685. * directory dentry if set. If this parameter is %NULL, then the
  686. * file will be created in the root of the debugfs filesystem.
  687. * @value: a pointer to the variable that the file should read to and write
  688. * from.
  689. *
  690. * This function creates a file in debugfs with the given name that
  691. * contains the value of the variable @value. If the @mode variable is so
  692. * set, it can be read from, and written to.
  693. *
  694. * This function will return a pointer to a dentry if it succeeds. This
  695. * pointer must be passed to the debugfs_remove() function when the file is
  696. * to be removed (no automatic cleanup happens if your module is unloaded,
  697. * you are responsible here.) If an error occurs, %NULL will be returned.
  698. *
  699. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  700. * returned. It is not wise to check for this value, but rather, check for
  701. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  702. * code.
  703. */
  704. struct dentry *debugfs_create_bool(const char *name, umode_t mode,
  705. struct dentry *parent, bool *value)
  706. {
  707. return debugfs_create_mode(name, mode, parent, value, &fops_bool,
  708. &fops_bool_ro, &fops_bool_wo);
  709. }
  710. EXPORT_SYMBOL_GPL(debugfs_create_bool);
  711. static ssize_t read_file_blob(struct file *file, char __user *user_buf,
  712. size_t count, loff_t *ppos)
  713. {
  714. struct debugfs_blob_wrapper *blob = file->private_data;
  715. return simple_read_from_buffer(user_buf, count, ppos, blob->data,
  716. blob->size);
  717. }
  718. static const struct file_operations fops_blob = {
  719. .read = read_file_blob,
  720. .open = simple_open,
  721. .llseek = default_llseek,
  722. };
  723. /**
  724. * debugfs_create_blob - create a debugfs file that is used to read a binary blob
  725. * @name: a pointer to a string containing the name of the file to create.
  726. * @mode: the permission that the file should have
  727. * @parent: a pointer to the parent dentry for this file. This should be a
  728. * directory dentry if set. If this parameter is %NULL, then the
  729. * file will be created in the root of the debugfs filesystem.
  730. * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
  731. * to the blob data and the size of the data.
  732. *
  733. * This function creates a file in debugfs with the given name that exports
  734. * @blob->data as a binary blob. If the @mode variable is so set it can be
  735. * read from. Writing is not supported.
  736. *
  737. * This function will return a pointer to a dentry if it succeeds. This
  738. * pointer must be passed to the debugfs_remove() function when the file is
  739. * to be removed (no automatic cleanup happens if your module is unloaded,
  740. * you are responsible here.) If an error occurs, %NULL will be returned.
  741. *
  742. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  743. * returned. It is not wise to check for this value, but rather, check for
  744. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  745. * code.
  746. */
  747. struct dentry *debugfs_create_blob(const char *name, umode_t mode,
  748. struct dentry *parent,
  749. struct debugfs_blob_wrapper *blob)
  750. {
  751. return debugfs_create_file(name, mode, parent, blob, &fops_blob);
  752. }
  753. EXPORT_SYMBOL_GPL(debugfs_create_blob);
  754. struct array_data {
  755. void *array;
  756. u32 elements;
  757. };
  758. static size_t u32_format_array(char *buf, size_t bufsize,
  759. u32 *array, int array_size)
  760. {
  761. size_t ret = 0;
  762. while (--array_size >= 0) {
  763. size_t len;
  764. char term = array_size ? ' ' : '\n';
  765. len = snprintf(buf, bufsize, "%u%c", *array++, term);
  766. ret += len;
  767. buf += len;
  768. bufsize -= len;
  769. }
  770. return ret;
  771. }
  772. static int u32_array_open(struct inode *inode, struct file *file)
  773. {
  774. struct array_data *data = inode->i_private;
  775. int size, elements = data->elements;
  776. char *buf;
  777. /*
  778. * Max size:
  779. * - 10 digits + ' '/'\n' = 11 bytes per number
  780. * - terminating NUL character
  781. */
  782. size = elements*11;
  783. buf = kmalloc(size+1, GFP_KERNEL);
  784. if (!buf)
  785. return -ENOMEM;
  786. buf[size] = 0;
  787. file->private_data = buf;
  788. u32_format_array(buf, size, data->array, data->elements);
  789. return nonseekable_open(inode, file);
  790. }
  791. static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
  792. loff_t *ppos)
  793. {
  794. size_t size = strlen(file->private_data);
  795. return simple_read_from_buffer(buf, len, ppos,
  796. file->private_data, size);
  797. }
  798. static int u32_array_release(struct inode *inode, struct file *file)
  799. {
  800. kfree(file->private_data);
  801. return 0;
  802. }
  803. static const struct file_operations u32_array_fops = {
  804. .owner = THIS_MODULE,
  805. .open = u32_array_open,
  806. .release = u32_array_release,
  807. .read = u32_array_read,
  808. .llseek = no_llseek,
  809. };
  810. /**
  811. * debugfs_create_u32_array - create a debugfs file that is used to read u32
  812. * array.
  813. * @name: a pointer to a string containing the name of the file to create.
  814. * @mode: the permission that the file should have.
  815. * @parent: a pointer to the parent dentry for this file. This should be a
  816. * directory dentry if set. If this parameter is %NULL, then the
  817. * file will be created in the root of the debugfs filesystem.
  818. * @array: u32 array that provides data.
  819. * @elements: total number of elements in the array.
  820. *
  821. * This function creates a file in debugfs with the given name that exports
  822. * @array as data. If the @mode variable is so set it can be read from.
  823. * Writing is not supported. Seek within the file is also not supported.
  824. * Once array is created its size can not be changed.
  825. *
  826. * The function returns a pointer to dentry on success. If debugfs is not
  827. * enabled in the kernel, the value -%ENODEV will be returned.
  828. */
  829. struct dentry *debugfs_create_u32_array(const char *name, umode_t mode,
  830. struct dentry *parent,
  831. u32 *array, u32 elements)
  832. {
  833. struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL);
  834. if (data == NULL)
  835. return NULL;
  836. data->array = array;
  837. data->elements = elements;
  838. return debugfs_create_file(name, mode, parent, data, &u32_array_fops);
  839. }
  840. EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
  841. #ifdef CONFIG_HAS_IOMEM
  842. /*
  843. * The regset32 stuff is used to print 32-bit registers using the
  844. * seq_file utilities. We offer printing a register set in an already-opened
  845. * sequential file or create a debugfs file that only prints a regset32.
  846. */
  847. /**
  848. * debugfs_print_regs32 - use seq_print to describe a set of registers
  849. * @s: the seq_file structure being used to generate output
  850. * @regs: an array if struct debugfs_reg32 structures
  851. * @nregs: the length of the above array
  852. * @base: the base address to be used in reading the registers
  853. * @prefix: a string to be prefixed to every output line
  854. *
  855. * This function outputs a text block describing the current values of
  856. * some 32-bit hardware registers. It is meant to be used within debugfs
  857. * files based on seq_file that need to show registers, intermixed with other
  858. * information. The prefix argument may be used to specify a leading string,
  859. * because some peripherals have several blocks of identical registers,
  860. * for example configuration of dma channels
  861. */
  862. void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
  863. int nregs, void __iomem *base, char *prefix)
  864. {
  865. int i;
  866. for (i = 0; i < nregs; i++, regs++) {
  867. if (prefix)
  868. seq_printf(s, "%s", prefix);
  869. seq_printf(s, "%s = 0x%08x\n", regs->name,
  870. readl(base + regs->offset));
  871. if (seq_has_overflowed(s))
  872. break;
  873. }
  874. }
  875. EXPORT_SYMBOL_GPL(debugfs_print_regs32);
  876. static int debugfs_show_regset32(struct seq_file *s, void *data)
  877. {
  878. struct debugfs_regset32 *regset = s->private;
  879. debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
  880. return 0;
  881. }
  882. static int debugfs_open_regset32(struct inode *inode, struct file *file)
  883. {
  884. return single_open(file, debugfs_show_regset32, inode->i_private);
  885. }
  886. static const struct file_operations fops_regset32 = {
  887. .open = debugfs_open_regset32,
  888. .read = seq_read,
  889. .llseek = seq_lseek,
  890. .release = single_release,
  891. };
  892. /**
  893. * debugfs_create_regset32 - create a debugfs file that returns register values
  894. * @name: a pointer to a string containing the name of the file to create.
  895. * @mode: the permission that the file should have
  896. * @parent: a pointer to the parent dentry for this file. This should be a
  897. * directory dentry if set. If this parameter is %NULL, then the
  898. * file will be created in the root of the debugfs filesystem.
  899. * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
  900. * to an array of register definitions, the array size and the base
  901. * address where the register bank is to be found.
  902. *
  903. * This function creates a file in debugfs with the given name that reports
  904. * the names and values of a set of 32-bit registers. If the @mode variable
  905. * is so set it can be read from. Writing is not supported.
  906. *
  907. * This function will return a pointer to a dentry if it succeeds. This
  908. * pointer must be passed to the debugfs_remove() function when the file is
  909. * to be removed (no automatic cleanup happens if your module is unloaded,
  910. * you are responsible here.) If an error occurs, %NULL will be returned.
  911. *
  912. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  913. * returned. It is not wise to check for this value, but rather, check for
  914. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  915. * code.
  916. */
  917. struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
  918. struct dentry *parent,
  919. struct debugfs_regset32 *regset)
  920. {
  921. return debugfs_create_file(name, mode, parent, regset, &fops_regset32);
  922. }
  923. EXPORT_SYMBOL_GPL(debugfs_create_regset32);
  924. #endif /* CONFIG_HAS_IOMEM */
  925. struct debugfs_devm_entry {
  926. int (*read)(struct seq_file *seq, void *data);
  927. struct device *dev;
  928. };
  929. static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
  930. {
  931. struct debugfs_devm_entry *entry = inode->i_private;
  932. return single_open(f, entry->read, entry->dev);
  933. }
  934. static const struct file_operations debugfs_devm_entry_ops = {
  935. .owner = THIS_MODULE,
  936. .open = debugfs_devm_entry_open,
  937. .release = single_release,
  938. .read = seq_read,
  939. .llseek = seq_lseek
  940. };
  941. /**
  942. * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
  943. *
  944. * @dev: device related to this debugfs file.
  945. * @name: name of the debugfs file.
  946. * @parent: a pointer to the parent dentry for this file. This should be a
  947. * directory dentry if set. If this parameter is %NULL, then the
  948. * file will be created in the root of the debugfs filesystem.
  949. * @read_fn: function pointer called to print the seq_file content.
  950. */
  951. struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
  952. struct dentry *parent,
  953. int (*read_fn)(struct seq_file *s,
  954. void *data))
  955. {
  956. struct debugfs_devm_entry *entry;
  957. if (IS_ERR(parent))
  958. return ERR_PTR(-ENOENT);
  959. entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
  960. if (!entry)
  961. return ERR_PTR(-ENOMEM);
  962. entry->read = read_fn;
  963. entry->dev = dev;
  964. return debugfs_create_file(name, S_IRUGO, parent, entry,
  965. &debugfs_devm_entry_ops);
  966. }
  967. EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);