seq_file.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/seq_file.c
  4. *
  5. * helper functions for making synthetic files from sequences of records.
  6. * initial implementation -- AV, Oct 2001.
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/cache.h>
  10. #include <linux/fs.h>
  11. #include <linux/export.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/vmalloc.h>
  14. #include <linux/slab.h>
  15. #include <linux/cred.h>
  16. #include <linux/mm.h>
  17. #include <linux/printk.h>
  18. #include <linux/string_helpers.h>
  19. #include <linux/uio.h>
  20. #include <linux/uaccess.h>
  21. #include <asm/page.h>
  22. static struct kmem_cache *seq_file_cache __ro_after_init;
  23. static void seq_set_overflow(struct seq_file *m)
  24. {
  25. m->count = m->size;
  26. }
  27. static void *seq_buf_alloc(unsigned long size)
  28. {
  29. if (unlikely(size > MAX_RW_COUNT))
  30. return NULL;
  31. return kvmalloc(size, GFP_KERNEL_ACCOUNT);
  32. }
  33. /**
  34. * seq_open - initialize sequential file
  35. * @file: file we initialize
  36. * @op: method table describing the sequence
  37. *
  38. * seq_open() sets @file, associating it with a sequence described
  39. * by @op. @op->start() sets the iterator up and returns the first
  40. * element of sequence. @op->stop() shuts it down. @op->next()
  41. * returns the next element of sequence. @op->show() prints element
  42. * into the buffer. In case of error ->start() and ->next() return
  43. * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
  44. * returns 0 in case of success and negative number in case of error.
  45. * Returning SEQ_SKIP means "discard this element and move on".
  46. * Note: seq_open() will allocate a struct seq_file and store its
  47. * pointer in @file->private_data. This pointer should not be modified.
  48. */
  49. int seq_open(struct file *file, const struct seq_operations *op)
  50. {
  51. struct seq_file *p;
  52. WARN_ON(file->private_data);
  53. p = kmem_cache_zalloc(seq_file_cache, GFP_KERNEL);
  54. if (!p)
  55. return -ENOMEM;
  56. file->private_data = p;
  57. mutex_init(&p->lock);
  58. p->op = op;
  59. // No refcounting: the lifetime of 'p' is constrained
  60. // to the lifetime of the file.
  61. p->file = file;
  62. /*
  63. * seq_files support lseek() and pread(). They do not implement
  64. * write() at all, but we clear FMODE_PWRITE here for historical
  65. * reasons.
  66. *
  67. * If a client of seq_files a) implements file.write() and b) wishes to
  68. * support pwrite() then that client will need to implement its own
  69. * file.open() which calls seq_open() and then sets FMODE_PWRITE.
  70. */
  71. file->f_mode &= ~FMODE_PWRITE;
  72. return 0;
  73. }
  74. EXPORT_SYMBOL(seq_open);
  75. static int traverse(struct seq_file *m, loff_t offset)
  76. {
  77. loff_t pos = 0;
  78. int error = 0;
  79. void *p;
  80. m->index = 0;
  81. m->count = m->from = 0;
  82. if (!offset)
  83. return 0;
  84. if (!m->buf) {
  85. m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
  86. if (!m->buf)
  87. return -ENOMEM;
  88. }
  89. p = m->op->start(m, &m->index);
  90. while (p) {
  91. error = PTR_ERR(p);
  92. if (IS_ERR(p))
  93. break;
  94. error = m->op->show(m, p);
  95. if (error < 0)
  96. break;
  97. if (unlikely(error)) {
  98. error = 0;
  99. m->count = 0;
  100. }
  101. if (seq_has_overflowed(m))
  102. goto Eoverflow;
  103. p = m->op->next(m, p, &m->index);
  104. if (pos + m->count > offset) {
  105. m->from = offset - pos;
  106. m->count -= m->from;
  107. break;
  108. }
  109. pos += m->count;
  110. m->count = 0;
  111. if (pos == offset)
  112. break;
  113. }
  114. m->op->stop(m, p);
  115. return error;
  116. Eoverflow:
  117. m->op->stop(m, p);
  118. kvfree(m->buf);
  119. m->count = 0;
  120. m->buf = seq_buf_alloc(m->size <<= 1);
  121. return !m->buf ? -ENOMEM : -EAGAIN;
  122. }
  123. /**
  124. * seq_read - ->read() method for sequential files.
  125. * @file: the file to read from
  126. * @buf: the buffer to read to
  127. * @size: the maximum number of bytes to read
  128. * @ppos: the current position in the file
  129. *
  130. * Ready-made ->f_op->read()
  131. */
  132. ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
  133. {
  134. struct iovec iov = { .iov_base = buf, .iov_len = size};
  135. struct kiocb kiocb;
  136. struct iov_iter iter;
  137. ssize_t ret;
  138. init_sync_kiocb(&kiocb, file);
  139. iov_iter_init(&iter, ITER_DEST, &iov, 1, size);
  140. kiocb.ki_pos = *ppos;
  141. ret = seq_read_iter(&kiocb, &iter);
  142. *ppos = kiocb.ki_pos;
  143. return ret;
  144. }
  145. EXPORT_SYMBOL(seq_read);
  146. /*
  147. * Ready-made ->f_op->read_iter()
  148. */
  149. ssize_t seq_read_iter(struct kiocb *iocb, struct iov_iter *iter)
  150. {
  151. struct seq_file *m = iocb->ki_filp->private_data;
  152. size_t copied = 0;
  153. size_t n;
  154. void *p;
  155. int err = 0;
  156. if (!iov_iter_count(iter))
  157. return 0;
  158. mutex_lock(&m->lock);
  159. /*
  160. * if request is to read from zero offset, reset iterator to first
  161. * record as it might have been already advanced by previous requests
  162. */
  163. if (iocb->ki_pos == 0) {
  164. m->index = 0;
  165. m->count = 0;
  166. }
  167. /* Don't assume ki_pos is where we left it */
  168. if (unlikely(iocb->ki_pos != m->read_pos)) {
  169. while ((err = traverse(m, iocb->ki_pos)) == -EAGAIN)
  170. ;
  171. if (err) {
  172. /* With prejudice... */
  173. m->read_pos = 0;
  174. m->index = 0;
  175. m->count = 0;
  176. goto Done;
  177. } else {
  178. m->read_pos = iocb->ki_pos;
  179. }
  180. }
  181. /* grab buffer if we didn't have one */
  182. if (!m->buf) {
  183. m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
  184. if (!m->buf)
  185. goto Enomem;
  186. }
  187. // something left in the buffer - copy it out first
  188. if (m->count) {
  189. n = copy_to_iter(m->buf + m->from, m->count, iter);
  190. m->count -= n;
  191. m->from += n;
  192. copied += n;
  193. if (m->count) // hadn't managed to copy everything
  194. goto Done;
  195. }
  196. // get a non-empty record in the buffer
  197. m->from = 0;
  198. p = m->op->start(m, &m->index);
  199. while (1) {
  200. err = PTR_ERR(p);
  201. if (!p || IS_ERR(p)) // EOF or an error
  202. break;
  203. err = m->op->show(m, p);
  204. if (err < 0) // hard error
  205. break;
  206. if (unlikely(err)) // ->show() says "skip it"
  207. m->count = 0;
  208. if (unlikely(!m->count)) { // empty record
  209. p = m->op->next(m, p, &m->index);
  210. continue;
  211. }
  212. if (!seq_has_overflowed(m)) // got it
  213. goto Fill;
  214. // need a bigger buffer
  215. m->op->stop(m, p);
  216. kvfree(m->buf);
  217. m->count = 0;
  218. m->buf = seq_buf_alloc(m->size <<= 1);
  219. if (!m->buf)
  220. goto Enomem;
  221. p = m->op->start(m, &m->index);
  222. }
  223. // EOF or an error
  224. m->op->stop(m, p);
  225. m->count = 0;
  226. goto Done;
  227. Fill:
  228. // one non-empty record is in the buffer; if they want more,
  229. // try to fit more in, but in any case we need to advance
  230. // the iterator once for every record shown.
  231. while (1) {
  232. size_t offs = m->count;
  233. loff_t pos = m->index;
  234. p = m->op->next(m, p, &m->index);
  235. if (pos == m->index) {
  236. pr_info_ratelimited("buggy .next function %ps did not update position index\n",
  237. m->op->next);
  238. m->index++;
  239. }
  240. if (!p || IS_ERR(p)) // no next record for us
  241. break;
  242. if (m->count >= iov_iter_count(iter))
  243. break;
  244. err = m->op->show(m, p);
  245. if (err > 0) { // ->show() says "skip it"
  246. m->count = offs;
  247. } else if (err || seq_has_overflowed(m)) {
  248. m->count = offs;
  249. break;
  250. }
  251. }
  252. m->op->stop(m, p);
  253. n = copy_to_iter(m->buf, m->count, iter);
  254. copied += n;
  255. m->count -= n;
  256. m->from = n;
  257. Done:
  258. if (unlikely(!copied)) {
  259. copied = m->count ? -EFAULT : err;
  260. } else {
  261. iocb->ki_pos += copied;
  262. m->read_pos += copied;
  263. }
  264. mutex_unlock(&m->lock);
  265. return copied;
  266. Enomem:
  267. err = -ENOMEM;
  268. goto Done;
  269. }
  270. EXPORT_SYMBOL(seq_read_iter);
  271. /**
  272. * seq_lseek - ->llseek() method for sequential files.
  273. * @file: the file in question
  274. * @offset: new position
  275. * @whence: 0 for absolute, 1 for relative position
  276. *
  277. * Ready-made ->f_op->llseek()
  278. */
  279. loff_t seq_lseek(struct file *file, loff_t offset, int whence)
  280. {
  281. struct seq_file *m = file->private_data;
  282. loff_t retval = -EINVAL;
  283. mutex_lock(&m->lock);
  284. switch (whence) {
  285. case SEEK_CUR:
  286. offset += file->f_pos;
  287. fallthrough;
  288. case SEEK_SET:
  289. if (offset < 0)
  290. break;
  291. retval = offset;
  292. if (offset != m->read_pos) {
  293. while ((retval = traverse(m, offset)) == -EAGAIN)
  294. ;
  295. if (retval) {
  296. /* with extreme prejudice... */
  297. file->f_pos = 0;
  298. m->read_pos = 0;
  299. m->index = 0;
  300. m->count = 0;
  301. } else {
  302. m->read_pos = offset;
  303. retval = file->f_pos = offset;
  304. }
  305. } else {
  306. file->f_pos = offset;
  307. }
  308. }
  309. mutex_unlock(&m->lock);
  310. return retval;
  311. }
  312. EXPORT_SYMBOL(seq_lseek);
  313. /**
  314. * seq_release - free the structures associated with sequential file.
  315. * @file: file in question
  316. * @inode: its inode
  317. *
  318. * Frees the structures associated with sequential file; can be used
  319. * as ->f_op->release() if you don't have private data to destroy.
  320. */
  321. int seq_release(struct inode *inode, struct file *file)
  322. {
  323. struct seq_file *m = file->private_data;
  324. kvfree(m->buf);
  325. kmem_cache_free(seq_file_cache, m);
  326. return 0;
  327. }
  328. EXPORT_SYMBOL(seq_release);
  329. /**
  330. * seq_escape_mem - print data into buffer, escaping some characters
  331. * @m: target buffer
  332. * @src: source buffer
  333. * @len: size of source buffer
  334. * @flags: flags to pass to string_escape_mem()
  335. * @esc: set of characters that need escaping
  336. *
  337. * Puts data into buffer, replacing each occurrence of character from
  338. * given class (defined by @flags and @esc) with printable escaped sequence.
  339. *
  340. * Use seq_has_overflowed() to check for errors.
  341. */
  342. void seq_escape_mem(struct seq_file *m, const char *src, size_t len,
  343. unsigned int flags, const char *esc)
  344. {
  345. char *buf;
  346. size_t size = seq_get_buf(m, &buf);
  347. int ret;
  348. ret = string_escape_mem(src, len, buf, size, flags, esc);
  349. seq_commit(m, ret < size ? ret : -1);
  350. }
  351. EXPORT_SYMBOL(seq_escape_mem);
  352. void seq_vprintf(struct seq_file *m, const char *f, va_list args)
  353. {
  354. int len;
  355. if (m->count < m->size) {
  356. len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
  357. if (m->count + len < m->size) {
  358. m->count += len;
  359. return;
  360. }
  361. }
  362. seq_set_overflow(m);
  363. }
  364. EXPORT_SYMBOL(seq_vprintf);
  365. void seq_printf(struct seq_file *m, const char *f, ...)
  366. {
  367. va_list args;
  368. va_start(args, f);
  369. seq_vprintf(m, f, args);
  370. va_end(args);
  371. }
  372. EXPORT_SYMBOL(seq_printf);
  373. #ifdef CONFIG_BINARY_PRINTF
  374. void seq_bprintf(struct seq_file *m, const char *f, const u32 *binary)
  375. {
  376. int len;
  377. if (m->count < m->size) {
  378. len = bstr_printf(m->buf + m->count, m->size - m->count, f,
  379. binary);
  380. if (m->count + len < m->size) {
  381. m->count += len;
  382. return;
  383. }
  384. }
  385. seq_set_overflow(m);
  386. }
  387. EXPORT_SYMBOL(seq_bprintf);
  388. #endif /* CONFIG_BINARY_PRINTF */
  389. /**
  390. * mangle_path - mangle and copy path to buffer beginning
  391. * @s: buffer start
  392. * @p: beginning of path in above buffer
  393. * @esc: set of characters that need escaping
  394. *
  395. * Copy the path from @p to @s, replacing each occurrence of character from
  396. * @esc with usual octal escape.
  397. * Returns pointer past last written character in @s, or NULL in case of
  398. * failure.
  399. */
  400. char *mangle_path(char *s, const char *p, const char *esc)
  401. {
  402. while (s <= p) {
  403. char c = *p++;
  404. if (!c) {
  405. return s;
  406. } else if (!strchr(esc, c)) {
  407. *s++ = c;
  408. } else if (s + 4 > p) {
  409. break;
  410. } else {
  411. *s++ = '\\';
  412. *s++ = '0' + ((c & 0300) >> 6);
  413. *s++ = '0' + ((c & 070) >> 3);
  414. *s++ = '0' + (c & 07);
  415. }
  416. }
  417. return NULL;
  418. }
  419. EXPORT_SYMBOL(mangle_path);
  420. /**
  421. * seq_path - seq_file interface to print a pathname
  422. * @m: the seq_file handle
  423. * @path: the struct path to print
  424. * @esc: set of characters to escape in the output
  425. *
  426. * return the absolute path of 'path', as represented by the
  427. * dentry / mnt pair in the path parameter.
  428. */
  429. int seq_path(struct seq_file *m, const struct path *path, const char *esc)
  430. {
  431. char *buf;
  432. size_t size = seq_get_buf(m, &buf);
  433. int res = -1;
  434. if (size) {
  435. char *p = d_path(path, buf, size);
  436. if (!IS_ERR(p)) {
  437. char *end = mangle_path(buf, p, esc);
  438. if (end)
  439. res = end - buf;
  440. }
  441. }
  442. seq_commit(m, res);
  443. return res;
  444. }
  445. EXPORT_SYMBOL(seq_path);
  446. /**
  447. * seq_file_path - seq_file interface to print a pathname of a file
  448. * @m: the seq_file handle
  449. * @file: the struct file to print
  450. * @esc: set of characters to escape in the output
  451. *
  452. * return the absolute path to the file.
  453. */
  454. int seq_file_path(struct seq_file *m, struct file *file, const char *esc)
  455. {
  456. return seq_path(m, &file->f_path, esc);
  457. }
  458. EXPORT_SYMBOL(seq_file_path);
  459. /*
  460. * Same as seq_path, but relative to supplied root.
  461. */
  462. int seq_path_root(struct seq_file *m, const struct path *path,
  463. const struct path *root, const char *esc)
  464. {
  465. char *buf;
  466. size_t size = seq_get_buf(m, &buf);
  467. int res = -ENAMETOOLONG;
  468. if (size) {
  469. char *p;
  470. p = __d_path(path, root, buf, size);
  471. if (!p)
  472. return SEQ_SKIP;
  473. res = PTR_ERR(p);
  474. if (!IS_ERR(p)) {
  475. char *end = mangle_path(buf, p, esc);
  476. if (end)
  477. res = end - buf;
  478. else
  479. res = -ENAMETOOLONG;
  480. }
  481. }
  482. seq_commit(m, res);
  483. return res < 0 && res != -ENAMETOOLONG ? res : 0;
  484. }
  485. /*
  486. * returns the path of the 'dentry' from the root of its filesystem.
  487. */
  488. int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
  489. {
  490. char *buf;
  491. size_t size = seq_get_buf(m, &buf);
  492. int res = -1;
  493. if (size) {
  494. char *p = dentry_path(dentry, buf, size);
  495. if (!IS_ERR(p)) {
  496. char *end = mangle_path(buf, p, esc);
  497. if (end)
  498. res = end - buf;
  499. }
  500. }
  501. seq_commit(m, res);
  502. return res;
  503. }
  504. EXPORT_SYMBOL(seq_dentry);
  505. void *single_start(struct seq_file *p, loff_t *pos)
  506. {
  507. return *pos ? NULL : SEQ_START_TOKEN;
  508. }
  509. static void *single_next(struct seq_file *p, void *v, loff_t *pos)
  510. {
  511. ++*pos;
  512. return NULL;
  513. }
  514. static void single_stop(struct seq_file *p, void *v)
  515. {
  516. }
  517. int single_open(struct file *file, int (*show)(struct seq_file *, void *),
  518. void *data)
  519. {
  520. struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL_ACCOUNT);
  521. int res = -ENOMEM;
  522. if (op) {
  523. op->start = single_start;
  524. op->next = single_next;
  525. op->stop = single_stop;
  526. op->show = show;
  527. res = seq_open(file, op);
  528. if (!res)
  529. ((struct seq_file *)file->private_data)->private = data;
  530. else
  531. kfree(op);
  532. }
  533. return res;
  534. }
  535. EXPORT_SYMBOL(single_open);
  536. int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
  537. void *data, size_t size)
  538. {
  539. char *buf = seq_buf_alloc(size);
  540. int ret;
  541. if (!buf)
  542. return -ENOMEM;
  543. ret = single_open(file, show, data);
  544. if (ret) {
  545. kvfree(buf);
  546. return ret;
  547. }
  548. ((struct seq_file *)file->private_data)->buf = buf;
  549. ((struct seq_file *)file->private_data)->size = size;
  550. return 0;
  551. }
  552. EXPORT_SYMBOL(single_open_size);
  553. int single_release(struct inode *inode, struct file *file)
  554. {
  555. const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
  556. int res = seq_release(inode, file);
  557. kfree(op);
  558. return res;
  559. }
  560. EXPORT_SYMBOL(single_release);
  561. int seq_release_private(struct inode *inode, struct file *file)
  562. {
  563. struct seq_file *seq = file->private_data;
  564. kfree(seq->private);
  565. seq->private = NULL;
  566. return seq_release(inode, file);
  567. }
  568. EXPORT_SYMBOL(seq_release_private);
  569. void *__seq_open_private(struct file *f, const struct seq_operations *ops,
  570. int psize)
  571. {
  572. int rc;
  573. void *private;
  574. struct seq_file *seq;
  575. private = kzalloc(psize, GFP_KERNEL_ACCOUNT);
  576. if (private == NULL)
  577. goto out;
  578. rc = seq_open(f, ops);
  579. if (rc < 0)
  580. goto out_free;
  581. seq = f->private_data;
  582. seq->private = private;
  583. return private;
  584. out_free:
  585. kfree(private);
  586. out:
  587. return NULL;
  588. }
  589. EXPORT_SYMBOL(__seq_open_private);
  590. int seq_open_private(struct file *filp, const struct seq_operations *ops,
  591. int psize)
  592. {
  593. return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
  594. }
  595. EXPORT_SYMBOL(seq_open_private);
  596. void seq_putc(struct seq_file *m, char c)
  597. {
  598. if (m->count >= m->size)
  599. return;
  600. m->buf[m->count++] = c;
  601. }
  602. EXPORT_SYMBOL(seq_putc);
  603. void seq_puts(struct seq_file *m, const char *s)
  604. {
  605. int len = strlen(s);
  606. if (m->count + len >= m->size) {
  607. seq_set_overflow(m);
  608. return;
  609. }
  610. memcpy(m->buf + m->count, s, len);
  611. m->count += len;
  612. }
  613. EXPORT_SYMBOL(seq_puts);
  614. /**
  615. * seq_put_decimal_ull_width - A helper routine for putting decimal numbers
  616. * without rich format of printf().
  617. * only 'unsigned long long' is supported.
  618. * @m: seq_file identifying the buffer to which data should be written
  619. * @delimiter: a string which is printed before the number
  620. * @num: the number
  621. * @width: a minimum field width
  622. *
  623. * This routine will put strlen(delimiter) + number into seq_filed.
  624. * This routine is very quick when you show lots of numbers.
  625. * In usual cases, it will be better to use seq_printf(). It's easier to read.
  626. */
  627. void seq_put_decimal_ull_width(struct seq_file *m, const char *delimiter,
  628. unsigned long long num, unsigned int width)
  629. {
  630. int len;
  631. if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
  632. goto overflow;
  633. if (delimiter && delimiter[0]) {
  634. if (delimiter[1] == 0)
  635. seq_putc(m, delimiter[0]);
  636. else
  637. seq_puts(m, delimiter);
  638. }
  639. if (!width)
  640. width = 1;
  641. if (m->count + width >= m->size)
  642. goto overflow;
  643. len = num_to_str(m->buf + m->count, m->size - m->count, num, width);
  644. if (!len)
  645. goto overflow;
  646. m->count += len;
  647. return;
  648. overflow:
  649. seq_set_overflow(m);
  650. }
  651. void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
  652. unsigned long long num)
  653. {
  654. return seq_put_decimal_ull_width(m, delimiter, num, 0);
  655. }
  656. EXPORT_SYMBOL(seq_put_decimal_ull);
  657. /**
  658. * seq_put_hex_ll - put a number in hexadecimal notation
  659. * @m: seq_file identifying the buffer to which data should be written
  660. * @delimiter: a string which is printed before the number
  661. * @v: the number
  662. * @width: a minimum field width
  663. *
  664. * seq_put_hex_ll(m, "", v, 8) is equal to seq_printf(m, "%08llx", v)
  665. *
  666. * This routine is very quick when you show lots of numbers.
  667. * In usual cases, it will be better to use seq_printf(). It's easier to read.
  668. */
  669. void seq_put_hex_ll(struct seq_file *m, const char *delimiter,
  670. unsigned long long v, unsigned int width)
  671. {
  672. unsigned int len;
  673. int i;
  674. if (delimiter && delimiter[0]) {
  675. if (delimiter[1] == 0)
  676. seq_putc(m, delimiter[0]);
  677. else
  678. seq_puts(m, delimiter);
  679. }
  680. /* If x is 0, the result of __builtin_clzll is undefined */
  681. if (v == 0)
  682. len = 1;
  683. else
  684. len = (sizeof(v) * 8 - __builtin_clzll(v) + 3) / 4;
  685. if (len < width)
  686. len = width;
  687. if (m->count + len > m->size) {
  688. seq_set_overflow(m);
  689. return;
  690. }
  691. for (i = len - 1; i >= 0; i--) {
  692. m->buf[m->count + i] = hex_asc[0xf & v];
  693. v = v >> 4;
  694. }
  695. m->count += len;
  696. }
  697. void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num)
  698. {
  699. int len;
  700. if (m->count + 3 >= m->size) /* we'll write 2 bytes at least */
  701. goto overflow;
  702. if (delimiter && delimiter[0]) {
  703. if (delimiter[1] == 0)
  704. seq_putc(m, delimiter[0]);
  705. else
  706. seq_puts(m, delimiter);
  707. }
  708. if (m->count + 2 >= m->size)
  709. goto overflow;
  710. if (num < 0) {
  711. m->buf[m->count++] = '-';
  712. num = -num;
  713. }
  714. if (num < 10) {
  715. m->buf[m->count++] = num + '0';
  716. return;
  717. }
  718. len = num_to_str(m->buf + m->count, m->size - m->count, num, 0);
  719. if (!len)
  720. goto overflow;
  721. m->count += len;
  722. return;
  723. overflow:
  724. seq_set_overflow(m);
  725. }
  726. EXPORT_SYMBOL(seq_put_decimal_ll);
  727. /**
  728. * seq_write - write arbitrary data to buffer
  729. * @seq: seq_file identifying the buffer to which data should be written
  730. * @data: data address
  731. * @len: number of bytes
  732. *
  733. * Return 0 on success, non-zero otherwise.
  734. */
  735. int seq_write(struct seq_file *seq, const void *data, size_t len)
  736. {
  737. if (seq->count + len < seq->size) {
  738. memcpy(seq->buf + seq->count, data, len);
  739. seq->count += len;
  740. return 0;
  741. }
  742. seq_set_overflow(seq);
  743. return -1;
  744. }
  745. EXPORT_SYMBOL(seq_write);
  746. /**
  747. * seq_pad - write padding spaces to buffer
  748. * @m: seq_file identifying the buffer to which data should be written
  749. * @c: the byte to append after padding if non-zero
  750. */
  751. void seq_pad(struct seq_file *m, char c)
  752. {
  753. int size = m->pad_until - m->count;
  754. if (size > 0) {
  755. if (size + m->count > m->size) {
  756. seq_set_overflow(m);
  757. return;
  758. }
  759. memset(m->buf + m->count, ' ', size);
  760. m->count += size;
  761. }
  762. if (c)
  763. seq_putc(m, c);
  764. }
  765. EXPORT_SYMBOL(seq_pad);
  766. /* A complete analogue of print_hex_dump() */
  767. void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
  768. int rowsize, int groupsize, const void *buf, size_t len,
  769. bool ascii)
  770. {
  771. const u8 *ptr = buf;
  772. int i, linelen, remaining = len;
  773. char *buffer;
  774. size_t size;
  775. int ret;
  776. if (rowsize != 16 && rowsize != 32)
  777. rowsize = 16;
  778. for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
  779. linelen = min(remaining, rowsize);
  780. remaining -= rowsize;
  781. switch (prefix_type) {
  782. case DUMP_PREFIX_ADDRESS:
  783. seq_printf(m, "%s%p: ", prefix_str, ptr + i);
  784. break;
  785. case DUMP_PREFIX_OFFSET:
  786. seq_printf(m, "%s%.8x: ", prefix_str, i);
  787. break;
  788. default:
  789. seq_printf(m, "%s", prefix_str);
  790. break;
  791. }
  792. size = seq_get_buf(m, &buffer);
  793. ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
  794. buffer, size, ascii);
  795. seq_commit(m, ret < size ? ret : -1);
  796. seq_putc(m, '\n');
  797. }
  798. }
  799. EXPORT_SYMBOL(seq_hex_dump);
  800. struct list_head *seq_list_start(struct list_head *head, loff_t pos)
  801. {
  802. struct list_head *lh;
  803. list_for_each(lh, head)
  804. if (pos-- == 0)
  805. return lh;
  806. return NULL;
  807. }
  808. EXPORT_SYMBOL(seq_list_start);
  809. struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
  810. {
  811. if (!pos)
  812. return head;
  813. return seq_list_start(head, pos - 1);
  814. }
  815. EXPORT_SYMBOL(seq_list_start_head);
  816. struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
  817. {
  818. struct list_head *lh;
  819. lh = ((struct list_head *)v)->next;
  820. ++*ppos;
  821. return lh == head ? NULL : lh;
  822. }
  823. EXPORT_SYMBOL(seq_list_next);
  824. struct list_head *seq_list_start_rcu(struct list_head *head, loff_t pos)
  825. {
  826. struct list_head *lh;
  827. list_for_each_rcu(lh, head)
  828. if (pos-- == 0)
  829. return lh;
  830. return NULL;
  831. }
  832. EXPORT_SYMBOL(seq_list_start_rcu);
  833. struct list_head *seq_list_start_head_rcu(struct list_head *head, loff_t pos)
  834. {
  835. if (!pos)
  836. return head;
  837. return seq_list_start_rcu(head, pos - 1);
  838. }
  839. EXPORT_SYMBOL(seq_list_start_head_rcu);
  840. struct list_head *seq_list_next_rcu(void *v, struct list_head *head,
  841. loff_t *ppos)
  842. {
  843. struct list_head *lh;
  844. lh = list_next_rcu((struct list_head *)v);
  845. ++*ppos;
  846. return lh == head ? NULL : lh;
  847. }
  848. EXPORT_SYMBOL(seq_list_next_rcu);
  849. /**
  850. * seq_hlist_start - start an iteration of a hlist
  851. * @head: the head of the hlist
  852. * @pos: the start position of the sequence
  853. *
  854. * Called at seq_file->op->start().
  855. */
  856. struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
  857. {
  858. struct hlist_node *node;
  859. hlist_for_each(node, head)
  860. if (pos-- == 0)
  861. return node;
  862. return NULL;
  863. }
  864. EXPORT_SYMBOL(seq_hlist_start);
  865. /**
  866. * seq_hlist_start_head - start an iteration of a hlist
  867. * @head: the head of the hlist
  868. * @pos: the start position of the sequence
  869. *
  870. * Called at seq_file->op->start(). Call this function if you want to
  871. * print a header at the top of the output.
  872. */
  873. struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
  874. {
  875. if (!pos)
  876. return SEQ_START_TOKEN;
  877. return seq_hlist_start(head, pos - 1);
  878. }
  879. EXPORT_SYMBOL(seq_hlist_start_head);
  880. /**
  881. * seq_hlist_next - move to the next position of the hlist
  882. * @v: the current iterator
  883. * @head: the head of the hlist
  884. * @ppos: the current position
  885. *
  886. * Called at seq_file->op->next().
  887. */
  888. struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
  889. loff_t *ppos)
  890. {
  891. struct hlist_node *node = v;
  892. ++*ppos;
  893. if (v == SEQ_START_TOKEN)
  894. return head->first;
  895. else
  896. return node->next;
  897. }
  898. EXPORT_SYMBOL(seq_hlist_next);
  899. /**
  900. * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
  901. * @head: the head of the hlist
  902. * @pos: the start position of the sequence
  903. *
  904. * Called at seq_file->op->start().
  905. *
  906. * This list-traversal primitive may safely run concurrently with
  907. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  908. * as long as the traversal is guarded by rcu_read_lock().
  909. */
  910. struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
  911. loff_t pos)
  912. {
  913. struct hlist_node *node;
  914. __hlist_for_each_rcu(node, head)
  915. if (pos-- == 0)
  916. return node;
  917. return NULL;
  918. }
  919. EXPORT_SYMBOL(seq_hlist_start_rcu);
  920. /**
  921. * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
  922. * @head: the head of the hlist
  923. * @pos: the start position of the sequence
  924. *
  925. * Called at seq_file->op->start(). Call this function if you want to
  926. * print a header at the top of the output.
  927. *
  928. * This list-traversal primitive may safely run concurrently with
  929. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  930. * as long as the traversal is guarded by rcu_read_lock().
  931. */
  932. struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
  933. loff_t pos)
  934. {
  935. if (!pos)
  936. return SEQ_START_TOKEN;
  937. return seq_hlist_start_rcu(head, pos - 1);
  938. }
  939. EXPORT_SYMBOL(seq_hlist_start_head_rcu);
  940. /**
  941. * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
  942. * @v: the current iterator
  943. * @head: the head of the hlist
  944. * @ppos: the current position
  945. *
  946. * Called at seq_file->op->next().
  947. *
  948. * This list-traversal primitive may safely run concurrently with
  949. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  950. * as long as the traversal is guarded by rcu_read_lock().
  951. */
  952. struct hlist_node *seq_hlist_next_rcu(void *v,
  953. struct hlist_head *head,
  954. loff_t *ppos)
  955. {
  956. struct hlist_node *node = v;
  957. ++*ppos;
  958. if (v == SEQ_START_TOKEN)
  959. return rcu_dereference(head->first);
  960. else
  961. return rcu_dereference(node->next);
  962. }
  963. EXPORT_SYMBOL(seq_hlist_next_rcu);
  964. /**
  965. * seq_hlist_start_percpu - start an iteration of a percpu hlist array
  966. * @head: pointer to percpu array of struct hlist_heads
  967. * @cpu: pointer to cpu "cursor"
  968. * @pos: start position of sequence
  969. *
  970. * Called at seq_file->op->start().
  971. */
  972. struct hlist_node *
  973. seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
  974. {
  975. struct hlist_node *node;
  976. for_each_possible_cpu(*cpu) {
  977. hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
  978. if (pos-- == 0)
  979. return node;
  980. }
  981. }
  982. return NULL;
  983. }
  984. EXPORT_SYMBOL(seq_hlist_start_percpu);
  985. /**
  986. * seq_hlist_next_percpu - move to the next position of the percpu hlist array
  987. * @v: pointer to current hlist_node
  988. * @head: pointer to percpu array of struct hlist_heads
  989. * @cpu: pointer to cpu "cursor"
  990. * @pos: start position of sequence
  991. *
  992. * Called at seq_file->op->next().
  993. */
  994. struct hlist_node *
  995. seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
  996. int *cpu, loff_t *pos)
  997. {
  998. struct hlist_node *node = v;
  999. ++*pos;
  1000. if (node->next)
  1001. return node->next;
  1002. for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
  1003. *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
  1004. struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
  1005. if (!hlist_empty(bucket))
  1006. return bucket->first;
  1007. }
  1008. return NULL;
  1009. }
  1010. EXPORT_SYMBOL(seq_hlist_next_percpu);
  1011. void __init seq_file_init(void)
  1012. {
  1013. seq_file_cache = KMEM_CACHE(seq_file, SLAB_ACCOUNT|SLAB_PANIC);
  1014. }