seq_file.c 26 KB

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