file.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  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. void *debugfs_get_aux(const struct file *file)
  43. {
  44. return DEBUGFS_I(file_inode(file))->aux;
  45. }
  46. EXPORT_SYMBOL_GPL(debugfs_get_aux);
  47. enum dbgfs_get_mode {
  48. DBGFS_GET_ALREADY,
  49. DBGFS_GET_REGULAR,
  50. DBGFS_GET_SHORT,
  51. };
  52. static int __debugfs_file_get(struct dentry *dentry, enum dbgfs_get_mode mode)
  53. {
  54. struct debugfs_fsdata *fsd;
  55. void *d_fsd;
  56. /*
  57. * This could only happen if some debugfs user erroneously calls
  58. * debugfs_file_get() on a dentry that isn't even a file, let
  59. * them know about it.
  60. */
  61. if (WARN_ON(!d_is_reg(dentry)))
  62. return -EINVAL;
  63. d_fsd = READ_ONCE(dentry->d_fsdata);
  64. if (d_fsd) {
  65. fsd = d_fsd;
  66. } else {
  67. struct inode *inode = dentry->d_inode;
  68. unsigned int methods = 0;
  69. if (WARN_ON(mode == DBGFS_GET_ALREADY))
  70. return -EINVAL;
  71. fsd = kmalloc_obj(*fsd);
  72. if (!fsd)
  73. return -ENOMEM;
  74. if (mode == DBGFS_GET_SHORT) {
  75. const struct debugfs_short_fops *ops;
  76. ops = fsd->short_fops = DEBUGFS_I(inode)->short_fops;
  77. if (ops->llseek)
  78. methods |= HAS_LSEEK;
  79. if (ops->read)
  80. methods |= HAS_READ;
  81. if (ops->write)
  82. methods |= HAS_WRITE;
  83. fsd->real_fops = NULL;
  84. } else {
  85. const struct file_operations *ops;
  86. ops = fsd->real_fops = DEBUGFS_I(inode)->real_fops;
  87. if (ops->llseek)
  88. methods |= HAS_LSEEK;
  89. if (ops->read)
  90. methods |= HAS_READ;
  91. if (ops->write)
  92. methods |= HAS_WRITE;
  93. if (ops->unlocked_ioctl)
  94. methods |= HAS_IOCTL;
  95. if (ops->poll)
  96. methods |= HAS_POLL;
  97. fsd->short_fops = NULL;
  98. }
  99. fsd->methods = methods;
  100. refcount_set(&fsd->active_users, 1);
  101. init_completion(&fsd->active_users_drained);
  102. INIT_LIST_HEAD(&fsd->cancellations);
  103. mutex_init(&fsd->cancellations_mtx);
  104. d_fsd = cmpxchg(&dentry->d_fsdata, NULL, fsd);
  105. if (d_fsd) {
  106. mutex_destroy(&fsd->cancellations_mtx);
  107. kfree(fsd);
  108. fsd = d_fsd;
  109. }
  110. }
  111. /*
  112. * In case of a successful cmpxchg() above, this check is
  113. * strictly necessary and must follow it, see the comment in
  114. * __debugfs_remove_file().
  115. * OTOH, if the cmpxchg() hasn't been executed or wasn't
  116. * successful, this serves the purpose of not starving
  117. * removers.
  118. */
  119. if (d_unlinked(dentry))
  120. return -EIO;
  121. if (!refcount_inc_not_zero(&fsd->active_users))
  122. return -EIO;
  123. return 0;
  124. }
  125. /**
  126. * debugfs_file_get - mark the beginning of file data access
  127. * @dentry: the dentry object whose data is being accessed.
  128. *
  129. * Up to a matching call to debugfs_file_put(), any successive call
  130. * into the file removing functions debugfs_remove() and
  131. * debugfs_remove_recursive() will block. Since associated private
  132. * file data may only get freed after a successful return of any of
  133. * the removal functions, you may safely access it after a successful
  134. * call to debugfs_file_get() without worrying about lifetime issues.
  135. *
  136. * If -%EIO is returned, the file has already been removed and thus,
  137. * it is not safe to access any of its data. If, on the other hand,
  138. * it is allowed to access the file data, zero is returned.
  139. */
  140. int debugfs_file_get(struct dentry *dentry)
  141. {
  142. return __debugfs_file_get(dentry, DBGFS_GET_ALREADY);
  143. }
  144. EXPORT_SYMBOL_GPL(debugfs_file_get);
  145. /**
  146. * debugfs_file_put - mark the end of file data access
  147. * @dentry: the dentry object formerly passed to
  148. * debugfs_file_get().
  149. *
  150. * Allow any ongoing concurrent call into debugfs_remove() or
  151. * debugfs_remove_recursive() blocked by a former call to
  152. * debugfs_file_get() to proceed and return to its caller.
  153. */
  154. void debugfs_file_put(struct dentry *dentry)
  155. {
  156. struct debugfs_fsdata *fsd = READ_ONCE(dentry->d_fsdata);
  157. if (refcount_dec_and_test(&fsd->active_users))
  158. complete(&fsd->active_users_drained);
  159. }
  160. EXPORT_SYMBOL_GPL(debugfs_file_put);
  161. /**
  162. * debugfs_enter_cancellation - enter a debugfs cancellation
  163. * @file: the file being accessed
  164. * @cancellation: the cancellation object, the cancel callback
  165. * inside of it must be initialized
  166. *
  167. * When a debugfs file is removed it needs to wait for all active
  168. * operations to complete. However, the operation itself may need
  169. * to wait for hardware or completion of some asynchronous process
  170. * or similar. As such, it may need to be cancelled to avoid long
  171. * waits or even deadlocks.
  172. *
  173. * This function can be used inside a debugfs handler that may
  174. * need to be cancelled. As soon as this function is called, the
  175. * cancellation's 'cancel' callback may be called, at which point
  176. * the caller should proceed to call debugfs_leave_cancellation()
  177. * and leave the debugfs handler function as soon as possible.
  178. * Note that the 'cancel' callback is only ever called in the
  179. * context of some kind of debugfs_remove().
  180. *
  181. * This function must be paired with debugfs_leave_cancellation().
  182. */
  183. void debugfs_enter_cancellation(struct file *file,
  184. struct debugfs_cancellation *cancellation)
  185. {
  186. struct debugfs_fsdata *fsd;
  187. struct dentry *dentry = F_DENTRY(file);
  188. INIT_LIST_HEAD(&cancellation->list);
  189. if (WARN_ON(!d_is_reg(dentry)))
  190. return;
  191. if (WARN_ON(!cancellation->cancel))
  192. return;
  193. fsd = READ_ONCE(dentry->d_fsdata);
  194. if (WARN_ON(!fsd))
  195. return;
  196. mutex_lock(&fsd->cancellations_mtx);
  197. list_add(&cancellation->list, &fsd->cancellations);
  198. mutex_unlock(&fsd->cancellations_mtx);
  199. /* if we're already removing wake it up to cancel */
  200. if (d_unlinked(dentry))
  201. complete(&fsd->active_users_drained);
  202. }
  203. EXPORT_SYMBOL_GPL(debugfs_enter_cancellation);
  204. /**
  205. * debugfs_leave_cancellation - leave cancellation section
  206. * @file: the file being accessed
  207. * @cancellation: the cancellation previously registered with
  208. * debugfs_enter_cancellation()
  209. *
  210. * See the documentation of debugfs_enter_cancellation().
  211. */
  212. void debugfs_leave_cancellation(struct file *file,
  213. struct debugfs_cancellation *cancellation)
  214. {
  215. struct debugfs_fsdata *fsd;
  216. struct dentry *dentry = F_DENTRY(file);
  217. if (WARN_ON(!d_is_reg(dentry)))
  218. return;
  219. fsd = READ_ONCE(dentry->d_fsdata);
  220. if (WARN_ON(!fsd))
  221. return;
  222. mutex_lock(&fsd->cancellations_mtx);
  223. if (!list_empty(&cancellation->list))
  224. list_del(&cancellation->list);
  225. mutex_unlock(&fsd->cancellations_mtx);
  226. }
  227. EXPORT_SYMBOL_GPL(debugfs_leave_cancellation);
  228. /*
  229. * Only permit access to world-readable files when the kernel is locked down.
  230. * We also need to exclude any file that has ways to write or alter it as root
  231. * can bypass the permissions check.
  232. */
  233. static int debugfs_locked_down(struct inode *inode,
  234. struct file *filp,
  235. const struct file_operations *real_fops)
  236. {
  237. if ((inode->i_mode & 07777 & ~0444) == 0 &&
  238. !(filp->f_mode & FMODE_WRITE) &&
  239. (!real_fops ||
  240. (!real_fops->unlocked_ioctl &&
  241. !real_fops->compat_ioctl &&
  242. !real_fops->mmap)))
  243. return 0;
  244. if (security_locked_down(LOCKDOWN_DEBUGFS))
  245. return -EPERM;
  246. return 0;
  247. }
  248. static int open_proxy_open(struct inode *inode, struct file *filp)
  249. {
  250. struct dentry *dentry = F_DENTRY(filp);
  251. const struct file_operations *real_fops = DEBUGFS_I(inode)->real_fops;
  252. int r;
  253. r = __debugfs_file_get(dentry, DBGFS_GET_REGULAR);
  254. if (r)
  255. return r == -EIO ? -ENOENT : r;
  256. r = debugfs_locked_down(inode, filp, real_fops);
  257. if (r)
  258. goto out;
  259. if (!fops_get(real_fops)) {
  260. #ifdef CONFIG_MODULES
  261. if (real_fops->owner &&
  262. real_fops->owner->state == MODULE_STATE_GOING) {
  263. r = -ENXIO;
  264. goto out;
  265. }
  266. #endif
  267. /* Huh? Module did not clean up after itself at exit? */
  268. WARN(1, "debugfs file owner did not clean up at exit: %pd",
  269. dentry);
  270. r = -ENXIO;
  271. goto out;
  272. }
  273. replace_fops(filp, real_fops);
  274. if (real_fops->open)
  275. r = real_fops->open(inode, filp);
  276. out:
  277. debugfs_file_put(dentry);
  278. return r;
  279. }
  280. const struct file_operations debugfs_open_proxy_file_operations = {
  281. .open = open_proxy_open,
  282. };
  283. #define PROTO(args...) args
  284. #define ARGS(args...) args
  285. #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args, bit, ret) \
  286. static ret_type full_proxy_ ## name(proto) \
  287. { \
  288. struct dentry *dentry = F_DENTRY(filp); \
  289. struct debugfs_fsdata *fsd = dentry->d_fsdata; \
  290. ret_type r; \
  291. \
  292. if (!(fsd->methods & bit)) \
  293. return ret; \
  294. r = debugfs_file_get(dentry); \
  295. if (unlikely(r)) \
  296. return r; \
  297. r = fsd->real_fops->name(args); \
  298. debugfs_file_put(dentry); \
  299. return r; \
  300. }
  301. #define SHORT_PROXY_FUNC(name, ret_type, filp, proto, args, bit, ret) \
  302. static ret_type short_proxy_ ## name(proto) \
  303. { \
  304. struct dentry *dentry = F_DENTRY(filp); \
  305. struct debugfs_fsdata *fsd = dentry->d_fsdata; \
  306. ret_type r; \
  307. \
  308. if (!(fsd->methods & bit)) \
  309. return ret; \
  310. r = debugfs_file_get(dentry); \
  311. if (unlikely(r)) \
  312. return r; \
  313. r = fsd->short_fops->name(args); \
  314. debugfs_file_put(dentry); \
  315. return r; \
  316. }
  317. SHORT_PROXY_FUNC(llseek, loff_t, filp,
  318. PROTO(struct file *filp, loff_t offset, int whence),
  319. ARGS(filp, offset, whence), HAS_LSEEK, -ESPIPE);
  320. FULL_PROXY_FUNC(llseek, loff_t, filp,
  321. PROTO(struct file *filp, loff_t offset, int whence),
  322. ARGS(filp, offset, whence), HAS_LSEEK, -ESPIPE);
  323. SHORT_PROXY_FUNC(read, ssize_t, filp,
  324. PROTO(struct file *filp, char __user *buf, size_t size,
  325. loff_t *ppos),
  326. ARGS(filp, buf, size, ppos), HAS_READ, -EINVAL);
  327. FULL_PROXY_FUNC(read, ssize_t, filp,
  328. PROTO(struct file *filp, char __user *buf, size_t size,
  329. loff_t *ppos),
  330. ARGS(filp, buf, size, ppos), HAS_READ, -EINVAL);
  331. SHORT_PROXY_FUNC(write, ssize_t, filp,
  332. PROTO(struct file *filp, const char __user *buf,
  333. size_t size, loff_t *ppos),
  334. ARGS(filp, buf, size, ppos), HAS_WRITE, -EINVAL);
  335. FULL_PROXY_FUNC(write, ssize_t, filp,
  336. PROTO(struct file *filp, const char __user *buf,
  337. size_t size, loff_t *ppos),
  338. ARGS(filp, buf, size, ppos), HAS_WRITE, -EINVAL);
  339. FULL_PROXY_FUNC(unlocked_ioctl, long, filp,
  340. PROTO(struct file *filp, unsigned int cmd, unsigned long arg),
  341. ARGS(filp, cmd, arg), HAS_IOCTL, -ENOTTY);
  342. static __poll_t full_proxy_poll(struct file *filp,
  343. struct poll_table_struct *wait)
  344. {
  345. struct dentry *dentry = F_DENTRY(filp);
  346. struct debugfs_fsdata *fsd = dentry->d_fsdata;
  347. __poll_t r = 0;
  348. if (!(fsd->methods & HAS_POLL))
  349. return DEFAULT_POLLMASK;
  350. if (debugfs_file_get(dentry))
  351. return EPOLLHUP;
  352. r = fsd->real_fops->poll(filp, wait);
  353. debugfs_file_put(dentry);
  354. return r;
  355. }
  356. static int full_proxy_release(struct inode *inode, struct file *file)
  357. {
  358. struct debugfs_fsdata *fsd = F_DENTRY(file)->d_fsdata;
  359. const struct file_operations *real_fops = fsd->real_fops;
  360. int r = 0;
  361. /*
  362. * We must not protect this against removal races here: the
  363. * original releaser should be called unconditionally in order
  364. * not to leak any resources. Releasers must not assume that
  365. * ->i_private is still being meaningful here.
  366. */
  367. if (real_fops->release)
  368. r = real_fops->release(inode, file);
  369. fops_put(real_fops);
  370. return r;
  371. }
  372. static int full_proxy_open_regular(struct inode *inode, struct file *filp)
  373. {
  374. struct dentry *dentry = F_DENTRY(filp);
  375. const struct file_operations *real_fops;
  376. struct debugfs_fsdata *fsd;
  377. int r;
  378. r = __debugfs_file_get(dentry, DBGFS_GET_REGULAR);
  379. if (r)
  380. return r == -EIO ? -ENOENT : r;
  381. fsd = dentry->d_fsdata;
  382. real_fops = fsd->real_fops;
  383. r = debugfs_locked_down(inode, filp, real_fops);
  384. if (r)
  385. goto out;
  386. if (!fops_get(real_fops)) {
  387. #ifdef CONFIG_MODULES
  388. if (real_fops->owner &&
  389. real_fops->owner->state == MODULE_STATE_GOING) {
  390. r = -ENXIO;
  391. goto out;
  392. }
  393. #endif
  394. /* Huh? Module did not cleanup after itself at exit? */
  395. WARN(1, "debugfs file owner did not clean up at exit: %pd",
  396. dentry);
  397. r = -ENXIO;
  398. goto out;
  399. }
  400. if (real_fops->open) {
  401. r = real_fops->open(inode, filp);
  402. if (r) {
  403. fops_put(real_fops);
  404. } else if (filp->f_op != &debugfs_full_proxy_file_operations) {
  405. /* No protection against file removal anymore. */
  406. WARN(1, "debugfs file owner replaced proxy fops: %pd",
  407. dentry);
  408. fops_put(real_fops);
  409. }
  410. }
  411. out:
  412. debugfs_file_put(dentry);
  413. return r;
  414. }
  415. const struct file_operations debugfs_full_proxy_file_operations = {
  416. .open = full_proxy_open_regular,
  417. .release = full_proxy_release,
  418. .llseek = full_proxy_llseek,
  419. .read = full_proxy_read,
  420. .write = full_proxy_write,
  421. .poll = full_proxy_poll,
  422. .unlocked_ioctl = full_proxy_unlocked_ioctl
  423. };
  424. static int full_proxy_open_short(struct inode *inode, struct file *filp)
  425. {
  426. struct dentry *dentry = F_DENTRY(filp);
  427. int r;
  428. r = __debugfs_file_get(dentry, DBGFS_GET_SHORT);
  429. if (r)
  430. return r == -EIO ? -ENOENT : r;
  431. r = debugfs_locked_down(inode, filp, NULL);
  432. if (!r)
  433. r = simple_open(inode, filp);
  434. debugfs_file_put(dentry);
  435. return r;
  436. }
  437. const struct file_operations debugfs_full_short_proxy_file_operations = {
  438. .open = full_proxy_open_short,
  439. .llseek = short_proxy_llseek,
  440. .read = short_proxy_read,
  441. .write = short_proxy_write,
  442. };
  443. ssize_t debugfs_attr_read(struct file *file, char __user *buf,
  444. size_t len, loff_t *ppos)
  445. {
  446. struct dentry *dentry = F_DENTRY(file);
  447. ssize_t ret;
  448. ret = debugfs_file_get(dentry);
  449. if (unlikely(ret))
  450. return ret;
  451. ret = simple_attr_read(file, buf, len, ppos);
  452. debugfs_file_put(dentry);
  453. return ret;
  454. }
  455. EXPORT_SYMBOL_GPL(debugfs_attr_read);
  456. static ssize_t debugfs_attr_write_xsigned(struct file *file, const char __user *buf,
  457. size_t len, loff_t *ppos, bool is_signed)
  458. {
  459. struct dentry *dentry = F_DENTRY(file);
  460. ssize_t ret;
  461. ret = debugfs_file_get(dentry);
  462. if (unlikely(ret))
  463. return ret;
  464. if (is_signed)
  465. ret = simple_attr_write_signed(file, buf, len, ppos);
  466. else
  467. ret = simple_attr_write(file, buf, len, ppos);
  468. debugfs_file_put(dentry);
  469. return ret;
  470. }
  471. ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
  472. size_t len, loff_t *ppos)
  473. {
  474. return debugfs_attr_write_xsigned(file, buf, len, ppos, false);
  475. }
  476. EXPORT_SYMBOL_GPL(debugfs_attr_write);
  477. ssize_t debugfs_attr_write_signed(struct file *file, const char __user *buf,
  478. size_t len, loff_t *ppos)
  479. {
  480. return debugfs_attr_write_xsigned(file, buf, len, ppos, true);
  481. }
  482. EXPORT_SYMBOL_GPL(debugfs_attr_write_signed);
  483. static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode,
  484. struct dentry *parent, void *value,
  485. const struct file_operations *fops,
  486. const struct file_operations *fops_ro,
  487. const struct file_operations *fops_wo)
  488. {
  489. /* if there are no write bits set, make read only */
  490. if (!(mode & S_IWUGO))
  491. return debugfs_create_file_unsafe(name, mode, parent, value,
  492. fops_ro);
  493. /* if there are no read bits set, make write only */
  494. if (!(mode & S_IRUGO))
  495. return debugfs_create_file_unsafe(name, mode, parent, value,
  496. fops_wo);
  497. return debugfs_create_file_unsafe(name, mode, parent, value, fops);
  498. }
  499. static int debugfs_u8_set(void *data, u64 val)
  500. {
  501. *(u8 *)data = val;
  502. return 0;
  503. }
  504. static int debugfs_u8_get(void *data, u64 *val)
  505. {
  506. *val = *(u8 *)data;
  507. return 0;
  508. }
  509. DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
  510. DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
  511. DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
  512. /**
  513. * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
  514. * @name: a pointer to a string containing the name of the file to create.
  515. * @mode: the permission that the file should have
  516. * @parent: a pointer to the parent dentry for this file. This should be a
  517. * directory dentry if set. If this parameter is %NULL, then the
  518. * file will be created in the root of the debugfs filesystem.
  519. * @value: a pointer to the variable that the file should read to and write
  520. * from.
  521. *
  522. * This function creates a file in debugfs with the given name that
  523. * contains the value of the variable @value. If the @mode variable is so
  524. * set, it can be read from, and written to.
  525. */
  526. void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent,
  527. u8 *value)
  528. {
  529. debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8,
  530. &fops_u8_ro, &fops_u8_wo);
  531. }
  532. EXPORT_SYMBOL_GPL(debugfs_create_u8);
  533. static int debugfs_u16_set(void *data, u64 val)
  534. {
  535. *(u16 *)data = val;
  536. return 0;
  537. }
  538. static int debugfs_u16_get(void *data, u64 *val)
  539. {
  540. *val = *(u16 *)data;
  541. return 0;
  542. }
  543. DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
  544. DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
  545. DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
  546. /**
  547. * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
  548. * @name: a pointer to a string containing the name of the file to create.
  549. * @mode: the permission that the file should have
  550. * @parent: a pointer to the parent dentry for this file. This should be a
  551. * directory dentry if set. If this parameter is %NULL, then the
  552. * file will be created in the root of the debugfs filesystem.
  553. * @value: a pointer to the variable that the file should read to and write
  554. * from.
  555. *
  556. * This function creates a file in debugfs with the given name that
  557. * contains the value of the variable @value. If the @mode variable is so
  558. * set, it can be read from, and written to.
  559. */
  560. void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent,
  561. u16 *value)
  562. {
  563. debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16,
  564. &fops_u16_ro, &fops_u16_wo);
  565. }
  566. EXPORT_SYMBOL_GPL(debugfs_create_u16);
  567. static int debugfs_u32_set(void *data, u64 val)
  568. {
  569. *(u32 *)data = val;
  570. return 0;
  571. }
  572. static int debugfs_u32_get(void *data, u64 *val)
  573. {
  574. *val = *(u32 *)data;
  575. return 0;
  576. }
  577. DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
  578. DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
  579. DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
  580. /**
  581. * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
  582. * @name: a pointer to a string containing the name of the file to create.
  583. * @mode: the permission that the file should have
  584. * @parent: a pointer to the parent dentry for this file. This should be a
  585. * directory dentry if set. If this parameter is %NULL, then the
  586. * file will be created in the root of the debugfs filesystem.
  587. * @value: a pointer to the variable that the file should read to and write
  588. * from.
  589. *
  590. * This function creates a file in debugfs with the given name that
  591. * contains the value of the variable @value. If the @mode variable is so
  592. * set, it can be read from, and written to.
  593. */
  594. void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent,
  595. u32 *value)
  596. {
  597. debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32,
  598. &fops_u32_ro, &fops_u32_wo);
  599. }
  600. EXPORT_SYMBOL_GPL(debugfs_create_u32);
  601. static int debugfs_u64_set(void *data, u64 val)
  602. {
  603. *(u64 *)data = val;
  604. return 0;
  605. }
  606. static int debugfs_u64_get(void *data, u64 *val)
  607. {
  608. *val = *(u64 *)data;
  609. return 0;
  610. }
  611. DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
  612. DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
  613. DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
  614. /**
  615. * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
  616. * @name: a pointer to a string containing the name of the file to create.
  617. * @mode: the permission that the file should have
  618. * @parent: a pointer to the parent dentry for this file. This should be a
  619. * directory dentry if set. If this parameter is %NULL, then the
  620. * file will be created in the root of the debugfs filesystem.
  621. * @value: a pointer to the variable that the file should read to and write
  622. * from.
  623. *
  624. * This function creates a file in debugfs with the given name that
  625. * contains the value of the variable @value. If the @mode variable is so
  626. * set, it can be read from, and written to.
  627. */
  628. void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent,
  629. u64 *value)
  630. {
  631. debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64,
  632. &fops_u64_ro, &fops_u64_wo);
  633. }
  634. EXPORT_SYMBOL_GPL(debugfs_create_u64);
  635. static int debugfs_ulong_set(void *data, u64 val)
  636. {
  637. *(unsigned long *)data = val;
  638. return 0;
  639. }
  640. static int debugfs_ulong_get(void *data, u64 *val)
  641. {
  642. *val = *(unsigned long *)data;
  643. return 0;
  644. }
  645. DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set,
  646. "%llu\n");
  647. DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
  648. DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
  649. /**
  650. * debugfs_create_ulong - create a debugfs file that is used to read and write
  651. * an unsigned long value.
  652. * @name: a pointer to a string containing the name of the file to create.
  653. * @mode: the permission that the file should have
  654. * @parent: a pointer to the parent dentry for this file. This should be a
  655. * directory dentry if set. If this parameter is %NULL, then the
  656. * file will be created in the root of the debugfs filesystem.
  657. * @value: a pointer to the variable that the file should read to and write
  658. * from.
  659. *
  660. * This function creates a file in debugfs with the given name that
  661. * contains the value of the variable @value. If the @mode variable is so
  662. * set, it can be read from, and written to.
  663. */
  664. void debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent,
  665. unsigned long *value)
  666. {
  667. debugfs_create_mode_unsafe(name, mode, parent, value, &fops_ulong,
  668. &fops_ulong_ro, &fops_ulong_wo);
  669. }
  670. EXPORT_SYMBOL_GPL(debugfs_create_ulong);
  671. DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
  672. DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
  673. DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
  674. DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set,
  675. "0x%04llx\n");
  676. DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
  677. DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
  678. DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set,
  679. "0x%08llx\n");
  680. DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
  681. DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
  682. DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set,
  683. "0x%016llx\n");
  684. DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
  685. DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
  686. /*
  687. * 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
  688. *
  689. * These functions are exactly the same as the above functions (but use a hex
  690. * output for the decimal challenged). For details look at the above unsigned
  691. * decimal functions.
  692. */
  693. /**
  694. * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
  695. * @name: a pointer to a string containing the name of the file to create.
  696. * @mode: the permission that the file should have
  697. * @parent: a pointer to the parent dentry for this file. This should be a
  698. * directory dentry if set. If this parameter is %NULL, then the
  699. * file will be created in the root of the debugfs filesystem.
  700. * @value: a pointer to the variable that the file should read to and write
  701. * from.
  702. */
  703. void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent,
  704. u8 *value)
  705. {
  706. debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8,
  707. &fops_x8_ro, &fops_x8_wo);
  708. }
  709. EXPORT_SYMBOL_GPL(debugfs_create_x8);
  710. /**
  711. * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
  712. * @name: a pointer to a string containing the name of the file to create.
  713. * @mode: the permission that the file should have
  714. * @parent: a pointer to the parent dentry for this file. This should be a
  715. * directory dentry if set. If this parameter is %NULL, then the
  716. * file will be created in the root of the debugfs filesystem.
  717. * @value: a pointer to the variable that the file should read to and write
  718. * from.
  719. */
  720. void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent,
  721. u16 *value)
  722. {
  723. debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16,
  724. &fops_x16_ro, &fops_x16_wo);
  725. }
  726. EXPORT_SYMBOL_GPL(debugfs_create_x16);
  727. /**
  728. * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
  729. * @name: a pointer to a string containing the name of the file to create.
  730. * @mode: the permission that the file should have
  731. * @parent: a pointer to the parent dentry for this file. This should be a
  732. * directory dentry if set. If this parameter is %NULL, then the
  733. * file will be created in the root of the debugfs filesystem.
  734. * @value: a pointer to the variable that the file should read to and write
  735. * from.
  736. */
  737. void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent,
  738. u32 *value)
  739. {
  740. debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32,
  741. &fops_x32_ro, &fops_x32_wo);
  742. }
  743. EXPORT_SYMBOL_GPL(debugfs_create_x32);
  744. /**
  745. * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
  746. * @name: a pointer to a string containing the name of the file to create.
  747. * @mode: the permission that the file should have
  748. * @parent: a pointer to the parent dentry for this file. This should be a
  749. * directory dentry if set. If this parameter is %NULL, then the
  750. * file will be created in the root of the debugfs filesystem.
  751. * @value: a pointer to the variable that the file should read to and write
  752. * from.
  753. */
  754. void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent,
  755. u64 *value)
  756. {
  757. debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64,
  758. &fops_x64_ro, &fops_x64_wo);
  759. }
  760. EXPORT_SYMBOL_GPL(debugfs_create_x64);
  761. static int debugfs_size_t_set(void *data, u64 val)
  762. {
  763. *(size_t *)data = val;
  764. return 0;
  765. }
  766. static int debugfs_size_t_get(void *data, u64 *val)
  767. {
  768. *val = *(size_t *)data;
  769. return 0;
  770. }
  771. DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
  772. "%llu\n"); /* %llu and %zu are more or less the same */
  773. DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
  774. DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
  775. /**
  776. * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
  777. * @name: a pointer to a string containing the name of the file to create.
  778. * @mode: the permission that the file should have
  779. * @parent: a pointer to the parent dentry for this file. This should be a
  780. * directory dentry if set. If this parameter is %NULL, then the
  781. * file will be created in the root of the debugfs filesystem.
  782. * @value: a pointer to the variable that the file should read to and write
  783. * from.
  784. */
  785. void debugfs_create_size_t(const char *name, umode_t mode,
  786. struct dentry *parent, size_t *value)
  787. {
  788. debugfs_create_mode_unsafe(name, mode, parent, value, &fops_size_t,
  789. &fops_size_t_ro, &fops_size_t_wo);
  790. }
  791. EXPORT_SYMBOL_GPL(debugfs_create_size_t);
  792. static int debugfs_atomic_t_set(void *data, u64 val)
  793. {
  794. atomic_set((atomic_t *)data, val);
  795. return 0;
  796. }
  797. static int debugfs_atomic_t_get(void *data, u64 *val)
  798. {
  799. *val = atomic_read((atomic_t *)data);
  800. return 0;
  801. }
  802. DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t, debugfs_atomic_t_get,
  803. debugfs_atomic_t_set, "%lld\n");
  804. DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_ro, debugfs_atomic_t_get, NULL,
  805. "%lld\n");
  806. DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_wo, NULL, debugfs_atomic_t_set,
  807. "%lld\n");
  808. /**
  809. * debugfs_create_atomic_t - create a debugfs file that is used to read and
  810. * write an atomic_t value
  811. * @name: a pointer to a string containing the name of the file to create.
  812. * @mode: the permission that the file should have
  813. * @parent: a pointer to the parent dentry for this file. This should be a
  814. * directory dentry if set. If this parameter is %NULL, then the
  815. * file will be created in the root of the debugfs filesystem.
  816. * @value: a pointer to the variable that the file should read to and write
  817. * from.
  818. */
  819. void debugfs_create_atomic_t(const char *name, umode_t mode,
  820. struct dentry *parent, atomic_t *value)
  821. {
  822. debugfs_create_mode_unsafe(name, mode, parent, value, &fops_atomic_t,
  823. &fops_atomic_t_ro, &fops_atomic_t_wo);
  824. }
  825. EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
  826. ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
  827. size_t count, loff_t *ppos)
  828. {
  829. char buf[2];
  830. bool val;
  831. int r;
  832. struct dentry *dentry = F_DENTRY(file);
  833. r = debugfs_file_get(dentry);
  834. if (unlikely(r))
  835. return r;
  836. val = *(bool *)file->private_data;
  837. debugfs_file_put(dentry);
  838. if (val)
  839. buf[0] = 'Y';
  840. else
  841. buf[0] = 'N';
  842. buf[1] = '\n';
  843. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  844. }
  845. EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
  846. ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
  847. size_t count, loff_t *ppos)
  848. {
  849. bool bv;
  850. int r;
  851. bool *val = file->private_data;
  852. struct dentry *dentry = F_DENTRY(file);
  853. r = kstrtobool_from_user(user_buf, count, &bv);
  854. if (!r) {
  855. r = debugfs_file_get(dentry);
  856. if (unlikely(r))
  857. return r;
  858. *val = bv;
  859. debugfs_file_put(dentry);
  860. }
  861. return count;
  862. }
  863. EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
  864. static const struct file_operations fops_bool = {
  865. .read = debugfs_read_file_bool,
  866. .write = debugfs_write_file_bool,
  867. .open = simple_open,
  868. .llseek = default_llseek,
  869. };
  870. static const struct file_operations fops_bool_ro = {
  871. .read = debugfs_read_file_bool,
  872. .open = simple_open,
  873. .llseek = default_llseek,
  874. };
  875. static const struct file_operations fops_bool_wo = {
  876. .write = debugfs_write_file_bool,
  877. .open = simple_open,
  878. .llseek = default_llseek,
  879. };
  880. /**
  881. * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
  882. * @name: a pointer to a string containing the name of the file to create.
  883. * @mode: the permission that the file should have
  884. * @parent: a pointer to the parent dentry for this file. This should be a
  885. * directory dentry if set. If this parameter is %NULL, then the
  886. * file will be created in the root of the debugfs filesystem.
  887. * @value: a pointer to the variable that the file should read to and write
  888. * from.
  889. *
  890. * This function creates a file in debugfs with the given name that
  891. * contains the value of the variable @value. If the @mode variable is so
  892. * set, it can be read from, and written to.
  893. */
  894. void debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent,
  895. bool *value)
  896. {
  897. debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool,
  898. &fops_bool_ro, &fops_bool_wo);
  899. }
  900. EXPORT_SYMBOL_GPL(debugfs_create_bool);
  901. ssize_t debugfs_read_file_str(struct file *file, char __user *user_buf,
  902. size_t count, loff_t *ppos)
  903. {
  904. struct dentry *dentry = F_DENTRY(file);
  905. char *str, *copy = NULL;
  906. int copy_len, len;
  907. ssize_t ret;
  908. ret = debugfs_file_get(dentry);
  909. if (unlikely(ret))
  910. return ret;
  911. str = *(char **)file->private_data;
  912. len = strlen(str) + 1;
  913. copy = kmalloc(len, GFP_KERNEL);
  914. if (!copy) {
  915. debugfs_file_put(dentry);
  916. return -ENOMEM;
  917. }
  918. copy_len = strscpy(copy, str, len);
  919. debugfs_file_put(dentry);
  920. if (copy_len < 0) {
  921. kfree(copy);
  922. return copy_len;
  923. }
  924. copy[copy_len] = '\n';
  925. ret = simple_read_from_buffer(user_buf, count, ppos, copy, len);
  926. kfree(copy);
  927. return ret;
  928. }
  929. EXPORT_SYMBOL_GPL(debugfs_create_str);
  930. static ssize_t debugfs_write_file_str(struct file *file, const char __user *user_buf,
  931. size_t count, loff_t *ppos)
  932. {
  933. struct dentry *dentry = F_DENTRY(file);
  934. char *old, *new = NULL;
  935. int pos = *ppos;
  936. int r;
  937. r = debugfs_file_get(dentry);
  938. if (unlikely(r))
  939. return r;
  940. old = *(char **)file->private_data;
  941. /* only allow strict concatenation */
  942. r = -EINVAL;
  943. if (pos && pos != strlen(old))
  944. goto error;
  945. r = -E2BIG;
  946. if (pos + count + 1 > PAGE_SIZE)
  947. goto error;
  948. r = -ENOMEM;
  949. new = kmalloc(pos + count + 1, GFP_KERNEL);
  950. if (!new)
  951. goto error;
  952. if (pos)
  953. memcpy(new, old, pos);
  954. r = -EFAULT;
  955. if (copy_from_user(new + pos, user_buf, count))
  956. goto error;
  957. new[pos + count] = '\0';
  958. strim(new);
  959. rcu_assign_pointer(*(char __rcu **)file->private_data, new);
  960. synchronize_rcu();
  961. kfree(old);
  962. debugfs_file_put(dentry);
  963. return count;
  964. error:
  965. kfree(new);
  966. debugfs_file_put(dentry);
  967. return r;
  968. }
  969. static const struct file_operations fops_str = {
  970. .read = debugfs_read_file_str,
  971. .write = debugfs_write_file_str,
  972. .open = simple_open,
  973. .llseek = default_llseek,
  974. };
  975. static const struct file_operations fops_str_ro = {
  976. .read = debugfs_read_file_str,
  977. .open = simple_open,
  978. .llseek = default_llseek,
  979. };
  980. static const struct file_operations fops_str_wo = {
  981. .write = debugfs_write_file_str,
  982. .open = simple_open,
  983. .llseek = default_llseek,
  984. };
  985. /**
  986. * debugfs_create_str - create a debugfs file that is used to read and write a string value
  987. * @name: a pointer to a string containing the name of the file to create.
  988. * @mode: the permission that the file should have
  989. * @parent: a pointer to the parent dentry for this file. This should be a
  990. * directory dentry if set. If this parameter is %NULL, then the
  991. * file will be created in the root of the debugfs filesystem.
  992. * @value: a pointer to the variable that the file should read to and write
  993. * from.
  994. *
  995. * This function creates a file in debugfs with the given name that
  996. * contains the value of the variable @value. If the @mode variable is so
  997. * set, it can be read from, and written to.
  998. */
  999. void debugfs_create_str(const char *name, umode_t mode,
  1000. struct dentry *parent, char **value)
  1001. {
  1002. debugfs_create_mode_unsafe(name, mode, parent, value, &fops_str,
  1003. &fops_str_ro, &fops_str_wo);
  1004. }
  1005. static ssize_t read_file_blob(struct file *file, char __user *user_buf,
  1006. size_t count, loff_t *ppos)
  1007. {
  1008. struct debugfs_blob_wrapper *blob = file->private_data;
  1009. struct dentry *dentry = F_DENTRY(file);
  1010. ssize_t r;
  1011. r = debugfs_file_get(dentry);
  1012. if (unlikely(r))
  1013. return r;
  1014. r = simple_read_from_buffer(user_buf, count, ppos, blob->data,
  1015. blob->size);
  1016. debugfs_file_put(dentry);
  1017. return r;
  1018. }
  1019. static ssize_t write_file_blob(struct file *file, const char __user *user_buf,
  1020. size_t count, loff_t *ppos)
  1021. {
  1022. struct debugfs_blob_wrapper *blob = file->private_data;
  1023. struct dentry *dentry = F_DENTRY(file);
  1024. ssize_t r;
  1025. r = debugfs_file_get(dentry);
  1026. if (unlikely(r))
  1027. return r;
  1028. r = simple_write_to_buffer(blob->data, blob->size, ppos, user_buf,
  1029. count);
  1030. debugfs_file_put(dentry);
  1031. return r;
  1032. }
  1033. static const struct file_operations fops_blob = {
  1034. .read = read_file_blob,
  1035. .write = write_file_blob,
  1036. .open = simple_open,
  1037. .llseek = default_llseek,
  1038. };
  1039. /**
  1040. * debugfs_create_blob - create a debugfs file that is used to read and write
  1041. * a binary blob
  1042. * @name: a pointer to a string containing the name of the file to create.
  1043. * @mode: the permission that the file should have
  1044. * @parent: a pointer to the parent dentry for this file. This should be a
  1045. * directory dentry if set. If this parameter is %NULL, then the
  1046. * file will be created in the root of the debugfs filesystem.
  1047. * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
  1048. * to the blob data and the size of the data.
  1049. *
  1050. * This function creates a file in debugfs with the given name that exports
  1051. * @blob->data as a binary blob. If the @mode variable is so set it can be
  1052. * read from and written to.
  1053. *
  1054. * This function will return a pointer to a dentry if it succeeds. This
  1055. * pointer must be passed to the debugfs_remove() function when the file is
  1056. * to be removed (no automatic cleanup happens if your module is unloaded,
  1057. * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be
  1058. * returned.
  1059. *
  1060. * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
  1061. * be returned.
  1062. */
  1063. struct dentry *debugfs_create_blob(const char *name, umode_t mode,
  1064. struct dentry *parent,
  1065. struct debugfs_blob_wrapper *blob)
  1066. {
  1067. return debugfs_create_file_unsafe(name, mode & 0644, parent, blob, &fops_blob);
  1068. }
  1069. EXPORT_SYMBOL_GPL(debugfs_create_blob);
  1070. static size_t u32_format_array(char *buf, size_t bufsize,
  1071. u32 *array, int array_size)
  1072. {
  1073. size_t ret = 0;
  1074. while (--array_size >= 0) {
  1075. size_t len;
  1076. char term = array_size ? ' ' : '\n';
  1077. len = snprintf(buf, bufsize, "%u%c", *array++, term);
  1078. ret += len;
  1079. buf += len;
  1080. bufsize -= len;
  1081. }
  1082. return ret;
  1083. }
  1084. static int u32_array_open(struct inode *inode, struct file *file)
  1085. {
  1086. struct debugfs_u32_array *data = inode->i_private;
  1087. int size, elements = data->n_elements;
  1088. char *buf;
  1089. /*
  1090. * Max size:
  1091. * - 10 digits + ' '/'\n' = 11 bytes per number
  1092. * - terminating NUL character
  1093. */
  1094. size = elements*11;
  1095. buf = kmalloc(size+1, GFP_KERNEL);
  1096. if (!buf)
  1097. return -ENOMEM;
  1098. buf[size] = 0;
  1099. file->private_data = buf;
  1100. u32_format_array(buf, size, data->array, data->n_elements);
  1101. return nonseekable_open(inode, file);
  1102. }
  1103. static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
  1104. loff_t *ppos)
  1105. {
  1106. size_t size = strlen(file->private_data);
  1107. return simple_read_from_buffer(buf, len, ppos,
  1108. file->private_data, size);
  1109. }
  1110. static int u32_array_release(struct inode *inode, struct file *file)
  1111. {
  1112. kfree(file->private_data);
  1113. return 0;
  1114. }
  1115. static const struct file_operations u32_array_fops = {
  1116. .owner = THIS_MODULE,
  1117. .open = u32_array_open,
  1118. .release = u32_array_release,
  1119. .read = u32_array_read,
  1120. };
  1121. /**
  1122. * debugfs_create_u32_array - create a debugfs file that is used to read u32
  1123. * array.
  1124. * @name: a pointer to a string containing the name of the file to create.
  1125. * @mode: the permission that the file should have.
  1126. * @parent: a pointer to the parent dentry for this file. This should be a
  1127. * directory dentry if set. If this parameter is %NULL, then the
  1128. * file will be created in the root of the debugfs filesystem.
  1129. * @array: wrapper struct containing data pointer and size of the array.
  1130. *
  1131. * This function creates a file in debugfs with the given name that exports
  1132. * @array as data. If the @mode variable is so set it can be read from.
  1133. * Writing is not supported. Seek within the file is also not supported.
  1134. * Once array is created its size can not be changed.
  1135. */
  1136. void debugfs_create_u32_array(const char *name, umode_t mode,
  1137. struct dentry *parent,
  1138. struct debugfs_u32_array *array)
  1139. {
  1140. debugfs_create_file_unsafe(name, mode, parent, array, &u32_array_fops);
  1141. }
  1142. EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
  1143. #ifdef CONFIG_HAS_IOMEM
  1144. /*
  1145. * The regset32 stuff is used to print 32-bit registers using the
  1146. * seq_file utilities. We offer printing a register set in an already-opened
  1147. * sequential file or create a debugfs file that only prints a regset32.
  1148. */
  1149. /**
  1150. * debugfs_print_regs32 - use seq_print to describe a set of registers
  1151. * @s: the seq_file structure being used to generate output
  1152. * @regs: an array if struct debugfs_reg32 structures
  1153. * @nregs: the length of the above array
  1154. * @base: the base address to be used in reading the registers
  1155. * @prefix: a string to be prefixed to every output line
  1156. *
  1157. * This function outputs a text block describing the current values of
  1158. * some 32-bit hardware registers. It is meant to be used within debugfs
  1159. * files based on seq_file that need to show registers, intermixed with other
  1160. * information. The prefix argument may be used to specify a leading string,
  1161. * because some peripherals have several blocks of identical registers,
  1162. * for example configuration of dma channels
  1163. */
  1164. void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
  1165. int nregs, void __iomem *base, char *prefix)
  1166. {
  1167. int i;
  1168. for (i = 0; i < nregs; i++, regs++) {
  1169. if (prefix)
  1170. seq_printf(s, "%s", prefix);
  1171. seq_printf(s, "%s = 0x%08x\n", regs->name,
  1172. readl(base + regs->offset));
  1173. if (seq_has_overflowed(s))
  1174. break;
  1175. }
  1176. }
  1177. EXPORT_SYMBOL_GPL(debugfs_print_regs32);
  1178. static int debugfs_regset32_show(struct seq_file *s, void *data)
  1179. {
  1180. struct debugfs_regset32 *regset = s->private;
  1181. if (regset->dev)
  1182. pm_runtime_get_sync(regset->dev);
  1183. debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
  1184. if (regset->dev)
  1185. pm_runtime_put(regset->dev);
  1186. return 0;
  1187. }
  1188. DEFINE_SHOW_ATTRIBUTE(debugfs_regset32);
  1189. /**
  1190. * debugfs_create_regset32 - create a debugfs file that returns register values
  1191. * @name: a pointer to a string containing the name of the file to create.
  1192. * @mode: the permission that the file should have
  1193. * @parent: a pointer to the parent dentry for this file. This should be a
  1194. * directory dentry if set. If this parameter is %NULL, then the
  1195. * file will be created in the root of the debugfs filesystem.
  1196. * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
  1197. * to an array of register definitions, the array size and the base
  1198. * address where the register bank is to be found.
  1199. *
  1200. * This function creates a file in debugfs with the given name that reports
  1201. * the names and values of a set of 32-bit registers. If the @mode variable
  1202. * is so set it can be read from. Writing is not supported.
  1203. */
  1204. void debugfs_create_regset32(const char *name, umode_t mode,
  1205. struct dentry *parent,
  1206. struct debugfs_regset32 *regset)
  1207. {
  1208. debugfs_create_file(name, mode, parent, regset, &debugfs_regset32_fops);
  1209. }
  1210. EXPORT_SYMBOL_GPL(debugfs_create_regset32);
  1211. #endif /* CONFIG_HAS_IOMEM */
  1212. struct debugfs_devm_entry {
  1213. int (*read)(struct seq_file *seq, void *data);
  1214. struct device *dev;
  1215. };
  1216. static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
  1217. {
  1218. struct debugfs_devm_entry *entry = inode->i_private;
  1219. return single_open(f, entry->read, entry->dev);
  1220. }
  1221. static const struct file_operations debugfs_devm_entry_ops = {
  1222. .owner = THIS_MODULE,
  1223. .open = debugfs_devm_entry_open,
  1224. .release = single_release,
  1225. .read = seq_read,
  1226. .llseek = seq_lseek
  1227. };
  1228. /**
  1229. * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
  1230. *
  1231. * @dev: device related to this debugfs file.
  1232. * @name: name of the debugfs file.
  1233. * @parent: a pointer to the parent dentry for this file. This should be a
  1234. * directory dentry if set. If this parameter is %NULL, then the
  1235. * file will be created in the root of the debugfs filesystem.
  1236. * @read_fn: function pointer called to print the seq_file content.
  1237. */
  1238. void debugfs_create_devm_seqfile(struct device *dev, const char *name,
  1239. struct dentry *parent,
  1240. int (*read_fn)(struct seq_file *s, void *data))
  1241. {
  1242. struct debugfs_devm_entry *entry;
  1243. if (IS_ERR(parent))
  1244. return;
  1245. entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
  1246. if (!entry)
  1247. return;
  1248. entry->read = read_fn;
  1249. entry->dev = dev;
  1250. debugfs_create_file(name, S_IRUGO, parent, entry,
  1251. &debugfs_devm_entry_ops);
  1252. }
  1253. EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);