seq_file.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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. seq_write(m, s, strlen(s));
  606. }
  607. EXPORT_SYMBOL(__seq_puts);
  608. /**
  609. * seq_put_decimal_ull_width - A helper routine for putting decimal numbers
  610. * without rich format of printf().
  611. * only 'unsigned long long' is supported.
  612. * @m: seq_file identifying the buffer to which data should be written
  613. * @delimiter: a string which is printed before the number
  614. * @num: the number
  615. * @width: a minimum field width
  616. *
  617. * This routine will put strlen(delimiter) + number into seq_filed.
  618. * This routine is very quick when you show lots of numbers.
  619. * In usual cases, it will be better to use seq_printf(). It's easier to read.
  620. */
  621. void seq_put_decimal_ull_width(struct seq_file *m, const char *delimiter,
  622. unsigned long long num, unsigned int width)
  623. {
  624. int len;
  625. if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
  626. goto overflow;
  627. if (delimiter && delimiter[0]) {
  628. if (delimiter[1] == 0)
  629. seq_putc(m, delimiter[0]);
  630. else
  631. seq_puts(m, delimiter);
  632. }
  633. if (!width)
  634. width = 1;
  635. if (m->count + width >= m->size)
  636. goto overflow;
  637. len = num_to_str(m->buf + m->count, m->size - m->count, num, width);
  638. if (!len)
  639. goto overflow;
  640. m->count += len;
  641. return;
  642. overflow:
  643. seq_set_overflow(m);
  644. }
  645. void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
  646. unsigned long long num)
  647. {
  648. return seq_put_decimal_ull_width(m, delimiter, num, 0);
  649. }
  650. EXPORT_SYMBOL(seq_put_decimal_ull);
  651. /**
  652. * seq_put_hex_ll - put a number in hexadecimal notation
  653. * @m: seq_file identifying the buffer to which data should be written
  654. * @delimiter: a string which is printed before the number
  655. * @v: the number
  656. * @width: a minimum field width
  657. *
  658. * seq_put_hex_ll(m, "", v, 8) is equal to seq_printf(m, "%08llx", v)
  659. *
  660. * This routine is very quick when you show lots of numbers.
  661. * In usual cases, it will be better to use seq_printf(). It's easier to read.
  662. */
  663. void seq_put_hex_ll(struct seq_file *m, const char *delimiter,
  664. unsigned long long v, unsigned int width)
  665. {
  666. unsigned int len;
  667. int i;
  668. if (delimiter && delimiter[0]) {
  669. if (delimiter[1] == 0)
  670. seq_putc(m, delimiter[0]);
  671. else
  672. seq_puts(m, delimiter);
  673. }
  674. /* If x is 0, the result of __builtin_clzll is undefined */
  675. if (v == 0)
  676. len = 1;
  677. else
  678. len = (sizeof(v) * 8 - __builtin_clzll(v) + 3) / 4;
  679. if (len < width)
  680. len = width;
  681. if (m->count + len > m->size) {
  682. seq_set_overflow(m);
  683. return;
  684. }
  685. for (i = len - 1; i >= 0; i--) {
  686. m->buf[m->count + i] = hex_asc[0xf & v];
  687. v = v >> 4;
  688. }
  689. m->count += len;
  690. }
  691. void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num)
  692. {
  693. int len;
  694. if (m->count + 3 >= m->size) /* we'll write 2 bytes at least */
  695. goto overflow;
  696. if (delimiter && delimiter[0]) {
  697. if (delimiter[1] == 0)
  698. seq_putc(m, delimiter[0]);
  699. else
  700. seq_puts(m, delimiter);
  701. }
  702. if (m->count + 2 >= m->size)
  703. goto overflow;
  704. if (num < 0) {
  705. m->buf[m->count++] = '-';
  706. num = -num;
  707. }
  708. if (num < 10) {
  709. m->buf[m->count++] = num + '0';
  710. return;
  711. }
  712. len = num_to_str(m->buf + m->count, m->size - m->count, num, 0);
  713. if (!len)
  714. goto overflow;
  715. m->count += len;
  716. return;
  717. overflow:
  718. seq_set_overflow(m);
  719. }
  720. EXPORT_SYMBOL(seq_put_decimal_ll);
  721. /**
  722. * seq_write - write arbitrary data to buffer
  723. * @seq: seq_file identifying the buffer to which data should be written
  724. * @data: data address
  725. * @len: number of bytes
  726. *
  727. * Return 0 on success, non-zero otherwise.
  728. */
  729. int seq_write(struct seq_file *seq, const void *data, size_t len)
  730. {
  731. if (seq->count + len < seq->size) {
  732. memcpy(seq->buf + seq->count, data, len);
  733. seq->count += len;
  734. return 0;
  735. }
  736. seq_set_overflow(seq);
  737. return -1;
  738. }
  739. EXPORT_SYMBOL(seq_write);
  740. /**
  741. * seq_pad - write padding spaces to buffer
  742. * @m: seq_file identifying the buffer to which data should be written
  743. * @c: the byte to append after padding if non-zero
  744. */
  745. void seq_pad(struct seq_file *m, char c)
  746. {
  747. int size = m->pad_until - m->count;
  748. if (size > 0) {
  749. if (size + m->count > m->size) {
  750. seq_set_overflow(m);
  751. return;
  752. }
  753. memset(m->buf + m->count, ' ', size);
  754. m->count += size;
  755. }
  756. if (c)
  757. seq_putc(m, c);
  758. }
  759. EXPORT_SYMBOL(seq_pad);
  760. /* A complete analogue of print_hex_dump() */
  761. void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
  762. int rowsize, int groupsize, const void *buf, size_t len,
  763. bool ascii)
  764. {
  765. const u8 *ptr = buf;
  766. int i, linelen, remaining = len;
  767. char *buffer;
  768. size_t size;
  769. int ret;
  770. if (rowsize != 16 && rowsize != 32)
  771. rowsize = 16;
  772. for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
  773. linelen = min(remaining, rowsize);
  774. remaining -= rowsize;
  775. switch (prefix_type) {
  776. case DUMP_PREFIX_ADDRESS:
  777. seq_printf(m, "%s%p: ", prefix_str, ptr + i);
  778. break;
  779. case DUMP_PREFIX_OFFSET:
  780. seq_printf(m, "%s%.8x: ", prefix_str, i);
  781. break;
  782. default:
  783. seq_printf(m, "%s", prefix_str);
  784. break;
  785. }
  786. size = seq_get_buf(m, &buffer);
  787. ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
  788. buffer, size, ascii);
  789. seq_commit(m, ret < size ? ret : -1);
  790. seq_putc(m, '\n');
  791. }
  792. }
  793. EXPORT_SYMBOL(seq_hex_dump);
  794. struct list_head *seq_list_start(struct list_head *head, loff_t pos)
  795. {
  796. struct list_head *lh;
  797. list_for_each(lh, head)
  798. if (pos-- == 0)
  799. return lh;
  800. return NULL;
  801. }
  802. EXPORT_SYMBOL(seq_list_start);
  803. struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
  804. {
  805. if (!pos)
  806. return head;
  807. return seq_list_start(head, pos - 1);
  808. }
  809. EXPORT_SYMBOL(seq_list_start_head);
  810. struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
  811. {
  812. struct list_head *lh;
  813. lh = ((struct list_head *)v)->next;
  814. ++*ppos;
  815. return lh == head ? NULL : lh;
  816. }
  817. EXPORT_SYMBOL(seq_list_next);
  818. struct list_head *seq_list_start_rcu(struct list_head *head, loff_t pos)
  819. {
  820. struct list_head *lh;
  821. list_for_each_rcu(lh, head)
  822. if (pos-- == 0)
  823. return lh;
  824. return NULL;
  825. }
  826. EXPORT_SYMBOL(seq_list_start_rcu);
  827. struct list_head *seq_list_start_head_rcu(struct list_head *head, loff_t pos)
  828. {
  829. if (!pos)
  830. return head;
  831. return seq_list_start_rcu(head, pos - 1);
  832. }
  833. EXPORT_SYMBOL(seq_list_start_head_rcu);
  834. struct list_head *seq_list_next_rcu(void *v, struct list_head *head,
  835. loff_t *ppos)
  836. {
  837. struct list_head *lh;
  838. lh = list_next_rcu((struct list_head *)v);
  839. ++*ppos;
  840. return lh == head ? NULL : lh;
  841. }
  842. EXPORT_SYMBOL(seq_list_next_rcu);
  843. /**
  844. * seq_hlist_start - start an iteration of a hlist
  845. * @head: the head of the hlist
  846. * @pos: the start position of the sequence
  847. *
  848. * Called at seq_file->op->start().
  849. */
  850. struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
  851. {
  852. struct hlist_node *node;
  853. hlist_for_each(node, head)
  854. if (pos-- == 0)
  855. return node;
  856. return NULL;
  857. }
  858. EXPORT_SYMBOL(seq_hlist_start);
  859. /**
  860. * seq_hlist_start_head - start an iteration of a hlist
  861. * @head: the head of the hlist
  862. * @pos: the start position of the sequence
  863. *
  864. * Called at seq_file->op->start(). Call this function if you want to
  865. * print a header at the top of the output.
  866. */
  867. struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
  868. {
  869. if (!pos)
  870. return SEQ_START_TOKEN;
  871. return seq_hlist_start(head, pos - 1);
  872. }
  873. EXPORT_SYMBOL(seq_hlist_start_head);
  874. /**
  875. * seq_hlist_next - move to the next position of the hlist
  876. * @v: the current iterator
  877. * @head: the head of the hlist
  878. * @ppos: the current position
  879. *
  880. * Called at seq_file->op->next().
  881. */
  882. struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
  883. loff_t *ppos)
  884. {
  885. struct hlist_node *node = v;
  886. ++*ppos;
  887. if (v == SEQ_START_TOKEN)
  888. return head->first;
  889. else
  890. return node->next;
  891. }
  892. EXPORT_SYMBOL(seq_hlist_next);
  893. /**
  894. * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
  895. * @head: the head of the hlist
  896. * @pos: the start position of the sequence
  897. *
  898. * Called at seq_file->op->start().
  899. *
  900. * This list-traversal primitive may safely run concurrently with
  901. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  902. * as long as the traversal is guarded by rcu_read_lock().
  903. */
  904. struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
  905. loff_t pos)
  906. {
  907. struct hlist_node *node;
  908. __hlist_for_each_rcu(node, head)
  909. if (pos-- == 0)
  910. return node;
  911. return NULL;
  912. }
  913. EXPORT_SYMBOL(seq_hlist_start_rcu);
  914. /**
  915. * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
  916. * @head: the head of the hlist
  917. * @pos: the start position of the sequence
  918. *
  919. * Called at seq_file->op->start(). Call this function if you want to
  920. * print a header at the top of the output.
  921. *
  922. * This list-traversal primitive may safely run concurrently with
  923. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  924. * as long as the traversal is guarded by rcu_read_lock().
  925. */
  926. struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
  927. loff_t pos)
  928. {
  929. if (!pos)
  930. return SEQ_START_TOKEN;
  931. return seq_hlist_start_rcu(head, pos - 1);
  932. }
  933. EXPORT_SYMBOL(seq_hlist_start_head_rcu);
  934. /**
  935. * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
  936. * @v: the current iterator
  937. * @head: the head of the hlist
  938. * @ppos: the current position
  939. *
  940. * Called at seq_file->op->next().
  941. *
  942. * This list-traversal primitive may safely run concurrently with
  943. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  944. * as long as the traversal is guarded by rcu_read_lock().
  945. */
  946. struct hlist_node *seq_hlist_next_rcu(void *v,
  947. struct hlist_head *head,
  948. loff_t *ppos)
  949. {
  950. struct hlist_node *node = v;
  951. ++*ppos;
  952. if (v == SEQ_START_TOKEN)
  953. return rcu_dereference(head->first);
  954. else
  955. return rcu_dereference(node->next);
  956. }
  957. EXPORT_SYMBOL(seq_hlist_next_rcu);
  958. /**
  959. * seq_hlist_start_percpu - start an iteration of a percpu hlist array
  960. * @head: pointer to percpu array of struct hlist_heads
  961. * @cpu: pointer to cpu "cursor"
  962. * @pos: start position of sequence
  963. *
  964. * Called at seq_file->op->start().
  965. */
  966. struct hlist_node *
  967. seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
  968. {
  969. struct hlist_node *node;
  970. for_each_possible_cpu(*cpu) {
  971. hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
  972. if (pos-- == 0)
  973. return node;
  974. }
  975. }
  976. return NULL;
  977. }
  978. EXPORT_SYMBOL(seq_hlist_start_percpu);
  979. /**
  980. * seq_hlist_next_percpu - move to the next position of the percpu hlist array
  981. * @v: pointer to current hlist_node
  982. * @head: pointer to percpu array of struct hlist_heads
  983. * @cpu: pointer to cpu "cursor"
  984. * @pos: start position of sequence
  985. *
  986. * Called at seq_file->op->next().
  987. */
  988. struct hlist_node *
  989. seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
  990. int *cpu, loff_t *pos)
  991. {
  992. struct hlist_node *node = v;
  993. ++*pos;
  994. if (node->next)
  995. return node->next;
  996. for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
  997. *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
  998. struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
  999. if (!hlist_empty(bucket))
  1000. return bucket->first;
  1001. }
  1002. return NULL;
  1003. }
  1004. EXPORT_SYMBOL(seq_hlist_next_percpu);
  1005. void __init seq_file_init(void)
  1006. {
  1007. seq_file_cache = KMEM_CACHE(seq_file, SLAB_ACCOUNT|SLAB_PANIC);
  1008. }