file.c 42 KB

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