file.c 36 KB

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