seq_file.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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, READ, &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 - print string into buffer, escaping some characters
  331. * @m: target buffer
  332. * @s: string
  333. * @esc: set of characters that need escaping
  334. *
  335. * Puts string into buffer, replacing each occurrence of character from
  336. * @esc with usual octal escape.
  337. * Use seq_has_overflowed() to check for errors.
  338. */
  339. void seq_escape(struct seq_file *m, const char *s, const char *esc)
  340. {
  341. char *buf;
  342. size_t size = seq_get_buf(m, &buf);
  343. int ret;
  344. ret = string_escape_str(s, buf, size, ESCAPE_OCTAL, esc);
  345. seq_commit(m, ret < size ? ret : -1);
  346. }
  347. EXPORT_SYMBOL(seq_escape);
  348. void seq_escape_mem_ascii(struct seq_file *m, const char *src, size_t isz)
  349. {
  350. char *buf;
  351. size_t size = seq_get_buf(m, &buf);
  352. int ret;
  353. ret = string_escape_mem_ascii(src, isz, buf, size);
  354. seq_commit(m, ret < size ? ret : -1);
  355. }
  356. EXPORT_SYMBOL(seq_escape_mem_ascii);
  357. void seq_vprintf(struct seq_file *m, const char *f, va_list args)
  358. {
  359. int len;
  360. if (m->count < m->size) {
  361. len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
  362. if (m->count + len < m->size) {
  363. m->count += len;
  364. return;
  365. }
  366. }
  367. seq_set_overflow(m);
  368. }
  369. EXPORT_SYMBOL(seq_vprintf);
  370. void seq_printf(struct seq_file *m, const char *f, ...)
  371. {
  372. va_list args;
  373. va_start(args, f);
  374. seq_vprintf(m, f, args);
  375. va_end(args);
  376. }
  377. EXPORT_SYMBOL(seq_printf);
  378. /**
  379. * mangle_path - mangle and copy path to buffer beginning
  380. * @s: buffer start
  381. * @p: beginning of path in above buffer
  382. * @esc: set of characters that need escaping
  383. *
  384. * Copy the path from @p to @s, replacing each occurrence of character from
  385. * @esc with usual octal escape.
  386. * Returns pointer past last written character in @s, or NULL in case of
  387. * failure.
  388. */
  389. char *mangle_path(char *s, const char *p, const char *esc)
  390. {
  391. while (s <= p) {
  392. char c = *p++;
  393. if (!c) {
  394. return s;
  395. } else if (!strchr(esc, c)) {
  396. *s++ = c;
  397. } else if (s + 4 > p) {
  398. break;
  399. } else {
  400. *s++ = '\\';
  401. *s++ = '0' + ((c & 0300) >> 6);
  402. *s++ = '0' + ((c & 070) >> 3);
  403. *s++ = '0' + (c & 07);
  404. }
  405. }
  406. return NULL;
  407. }
  408. EXPORT_SYMBOL(mangle_path);
  409. /**
  410. * seq_path - seq_file interface to print a pathname
  411. * @m: the seq_file handle
  412. * @path: the struct path to print
  413. * @esc: set of characters to escape in the output
  414. *
  415. * return the absolute path of 'path', as represented by the
  416. * dentry / mnt pair in the path parameter.
  417. */
  418. int seq_path(struct seq_file *m, const struct path *path, const char *esc)
  419. {
  420. char *buf;
  421. size_t size = seq_get_buf(m, &buf);
  422. int res = -1;
  423. if (size) {
  424. char *p = d_path(path, buf, size);
  425. if (!IS_ERR(p)) {
  426. char *end = mangle_path(buf, p, esc);
  427. if (end)
  428. res = end - buf;
  429. }
  430. }
  431. seq_commit(m, res);
  432. return res;
  433. }
  434. EXPORT_SYMBOL(seq_path);
  435. /**
  436. * seq_file_path - seq_file interface to print a pathname of a file
  437. * @m: the seq_file handle
  438. * @file: the struct file to print
  439. * @esc: set of characters to escape in the output
  440. *
  441. * return the absolute path to the file.
  442. */
  443. int seq_file_path(struct seq_file *m, struct file *file, const char *esc)
  444. {
  445. return seq_path(m, &file->f_path, esc);
  446. }
  447. EXPORT_SYMBOL(seq_file_path);
  448. /*
  449. * Same as seq_path, but relative to supplied root.
  450. */
  451. int seq_path_root(struct seq_file *m, const struct path *path,
  452. const struct path *root, const char *esc)
  453. {
  454. char *buf;
  455. size_t size = seq_get_buf(m, &buf);
  456. int res = -ENAMETOOLONG;
  457. if (size) {
  458. char *p;
  459. p = __d_path(path, root, buf, size);
  460. if (!p)
  461. return SEQ_SKIP;
  462. res = PTR_ERR(p);
  463. if (!IS_ERR(p)) {
  464. char *end = mangle_path(buf, p, esc);
  465. if (end)
  466. res = end - buf;
  467. else
  468. res = -ENAMETOOLONG;
  469. }
  470. }
  471. seq_commit(m, res);
  472. return res < 0 && res != -ENAMETOOLONG ? res : 0;
  473. }
  474. /*
  475. * returns the path of the 'dentry' from the root of its filesystem.
  476. */
  477. int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
  478. {
  479. char *buf;
  480. size_t size = seq_get_buf(m, &buf);
  481. int res = -1;
  482. if (size) {
  483. char *p = dentry_path(dentry, buf, size);
  484. if (!IS_ERR(p)) {
  485. char *end = mangle_path(buf, p, esc);
  486. if (end)
  487. res = end - buf;
  488. }
  489. }
  490. seq_commit(m, res);
  491. return res;
  492. }
  493. EXPORT_SYMBOL(seq_dentry);
  494. static void *single_start(struct seq_file *p, loff_t *pos)
  495. {
  496. return NULL + (*pos == 0);
  497. }
  498. static void *single_next(struct seq_file *p, void *v, loff_t *pos)
  499. {
  500. ++*pos;
  501. return NULL;
  502. }
  503. static void single_stop(struct seq_file *p, void *v)
  504. {
  505. }
  506. int single_open(struct file *file, int (*show)(struct seq_file *, void *),
  507. void *data)
  508. {
  509. struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL_ACCOUNT);
  510. int res = -ENOMEM;
  511. if (op) {
  512. op->start = single_start;
  513. op->next = single_next;
  514. op->stop = single_stop;
  515. op->show = show;
  516. res = seq_open(file, op);
  517. if (!res)
  518. ((struct seq_file *)file->private_data)->private = data;
  519. else
  520. kfree(op);
  521. }
  522. return res;
  523. }
  524. EXPORT_SYMBOL(single_open);
  525. int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
  526. void *data, size_t size)
  527. {
  528. char *buf = seq_buf_alloc(size);
  529. int ret;
  530. if (!buf)
  531. return -ENOMEM;
  532. ret = single_open(file, show, data);
  533. if (ret) {
  534. kvfree(buf);
  535. return ret;
  536. }
  537. ((struct seq_file *)file->private_data)->buf = buf;
  538. ((struct seq_file *)file->private_data)->size = size;
  539. return 0;
  540. }
  541. EXPORT_SYMBOL(single_open_size);
  542. int single_release(struct inode *inode, struct file *file)
  543. {
  544. const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
  545. int res = seq_release(inode, file);
  546. kfree(op);
  547. return res;
  548. }
  549. EXPORT_SYMBOL(single_release);
  550. int seq_release_private(struct inode *inode, struct file *file)
  551. {
  552. struct seq_file *seq = file->private_data;
  553. kfree(seq->private);
  554. seq->private = NULL;
  555. return seq_release(inode, file);
  556. }
  557. EXPORT_SYMBOL(seq_release_private);
  558. void *__seq_open_private(struct file *f, const struct seq_operations *ops,
  559. int psize)
  560. {
  561. int rc;
  562. void *private;
  563. struct seq_file *seq;
  564. private = kzalloc(psize, GFP_KERNEL_ACCOUNT);
  565. if (private == NULL)
  566. goto out;
  567. rc = seq_open(f, ops);
  568. if (rc < 0)
  569. goto out_free;
  570. seq = f->private_data;
  571. seq->private = private;
  572. return private;
  573. out_free:
  574. kfree(private);
  575. out:
  576. return NULL;
  577. }
  578. EXPORT_SYMBOL(__seq_open_private);
  579. int seq_open_private(struct file *filp, const struct seq_operations *ops,
  580. int psize)
  581. {
  582. return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
  583. }
  584. EXPORT_SYMBOL(seq_open_private);
  585. void seq_putc(struct seq_file *m, char c)
  586. {
  587. if (m->count >= m->size)
  588. return;
  589. m->buf[m->count++] = c;
  590. }
  591. EXPORT_SYMBOL(seq_putc);
  592. void seq_puts(struct seq_file *m, const char *s)
  593. {
  594. int len = strlen(s);
  595. if (m->count + len >= m->size) {
  596. seq_set_overflow(m);
  597. return;
  598. }
  599. memcpy(m->buf + m->count, s, len);
  600. m->count += len;
  601. }
  602. EXPORT_SYMBOL(seq_puts);
  603. /**
  604. * A helper routine for putting decimal numbers without rich format of printf().
  605. * only 'unsigned long long' is supported.
  606. * @m: seq_file identifying the buffer to which data should be written
  607. * @delimiter: a string which is printed before the number
  608. * @num: the number
  609. * @width: a minimum field width
  610. *
  611. * This routine will put strlen(delimiter) + number into seq_filed.
  612. * This routine is very quick when you show lots of numbers.
  613. * In usual cases, it will be better to use seq_printf(). It's easier to read.
  614. */
  615. void seq_put_decimal_ull_width(struct seq_file *m, const char *delimiter,
  616. unsigned long long num, unsigned int width)
  617. {
  618. int len;
  619. if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
  620. goto overflow;
  621. if (delimiter && delimiter[0]) {
  622. if (delimiter[1] == 0)
  623. seq_putc(m, delimiter[0]);
  624. else
  625. seq_puts(m, delimiter);
  626. }
  627. if (!width)
  628. width = 1;
  629. if (m->count + width >= m->size)
  630. goto overflow;
  631. len = num_to_str(m->buf + m->count, m->size - m->count, num, width);
  632. if (!len)
  633. goto overflow;
  634. m->count += len;
  635. return;
  636. overflow:
  637. seq_set_overflow(m);
  638. }
  639. void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
  640. unsigned long long num)
  641. {
  642. return seq_put_decimal_ull_width(m, delimiter, num, 0);
  643. }
  644. EXPORT_SYMBOL(seq_put_decimal_ull);
  645. /**
  646. * seq_put_hex_ll - put a number in hexadecimal notation
  647. * @m: seq_file identifying the buffer to which data should be written
  648. * @delimiter: a string which is printed before the number
  649. * @v: the number
  650. * @width: a minimum field width
  651. *
  652. * seq_put_hex_ll(m, "", v, 8) is equal to seq_printf(m, "%08llx", v)
  653. *
  654. * This routine is very quick when you show lots of numbers.
  655. * In usual cases, it will be better to use seq_printf(). It's easier to read.
  656. */
  657. void seq_put_hex_ll(struct seq_file *m, const char *delimiter,
  658. unsigned long long v, unsigned int width)
  659. {
  660. unsigned int len;
  661. int i;
  662. if (delimiter && delimiter[0]) {
  663. if (delimiter[1] == 0)
  664. seq_putc(m, delimiter[0]);
  665. else
  666. seq_puts(m, delimiter);
  667. }
  668. /* If x is 0, the result of __builtin_clzll is undefined */
  669. if (v == 0)
  670. len = 1;
  671. else
  672. len = (sizeof(v) * 8 - __builtin_clzll(v) + 3) / 4;
  673. if (len < width)
  674. len = width;
  675. if (m->count + len > m->size) {
  676. seq_set_overflow(m);
  677. return;
  678. }
  679. for (i = len - 1; i >= 0; i--) {
  680. m->buf[m->count + i] = hex_asc[0xf & v];
  681. v = v >> 4;
  682. }
  683. m->count += len;
  684. }
  685. void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num)
  686. {
  687. int len;
  688. if (m->count + 3 >= m->size) /* we'll write 2 bytes at least */
  689. goto overflow;
  690. if (delimiter && delimiter[0]) {
  691. if (delimiter[1] == 0)
  692. seq_putc(m, delimiter[0]);
  693. else
  694. seq_puts(m, delimiter);
  695. }
  696. if (m->count + 2 >= m->size)
  697. goto overflow;
  698. if (num < 0) {
  699. m->buf[m->count++] = '-';
  700. num = -num;
  701. }
  702. if (num < 10) {
  703. m->buf[m->count++] = num + '0';
  704. return;
  705. }
  706. len = num_to_str(m->buf + m->count, m->size - m->count, num, 0);
  707. if (!len)
  708. goto overflow;
  709. m->count += len;
  710. return;
  711. overflow:
  712. seq_set_overflow(m);
  713. }
  714. EXPORT_SYMBOL(seq_put_decimal_ll);
  715. /**
  716. * seq_write - write arbitrary data to buffer
  717. * @seq: seq_file identifying the buffer to which data should be written
  718. * @data: data address
  719. * @len: number of bytes
  720. *
  721. * Return 0 on success, non-zero otherwise.
  722. */
  723. int seq_write(struct seq_file *seq, const void *data, size_t len)
  724. {
  725. if (seq->count + len < seq->size) {
  726. memcpy(seq->buf + seq->count, data, len);
  727. seq->count += len;
  728. return 0;
  729. }
  730. seq_set_overflow(seq);
  731. return -1;
  732. }
  733. EXPORT_SYMBOL(seq_write);
  734. /**
  735. * seq_pad - write padding spaces to buffer
  736. * @m: seq_file identifying the buffer to which data should be written
  737. * @c: the byte to append after padding if non-zero
  738. */
  739. void seq_pad(struct seq_file *m, char c)
  740. {
  741. int size = m->pad_until - m->count;
  742. if (size > 0) {
  743. if (size + m->count > m->size) {
  744. seq_set_overflow(m);
  745. return;
  746. }
  747. memset(m->buf + m->count, ' ', size);
  748. m->count += size;
  749. }
  750. if (c)
  751. seq_putc(m, c);
  752. }
  753. EXPORT_SYMBOL(seq_pad);
  754. /* A complete analogue of print_hex_dump() */
  755. void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
  756. int rowsize, int groupsize, const void *buf, size_t len,
  757. bool ascii)
  758. {
  759. const u8 *ptr = buf;
  760. int i, linelen, remaining = len;
  761. char *buffer;
  762. size_t size;
  763. int ret;
  764. if (rowsize != 16 && rowsize != 32)
  765. rowsize = 16;
  766. for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
  767. linelen = min(remaining, rowsize);
  768. remaining -= rowsize;
  769. switch (prefix_type) {
  770. case DUMP_PREFIX_ADDRESS:
  771. seq_printf(m, "%s%p: ", prefix_str, ptr + i);
  772. break;
  773. case DUMP_PREFIX_OFFSET:
  774. seq_printf(m, "%s%.8x: ", prefix_str, i);
  775. break;
  776. default:
  777. seq_printf(m, "%s", prefix_str);
  778. break;
  779. }
  780. size = seq_get_buf(m, &buffer);
  781. ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
  782. buffer, size, ascii);
  783. seq_commit(m, ret < size ? ret : -1);
  784. seq_putc(m, '\n');
  785. }
  786. }
  787. EXPORT_SYMBOL(seq_hex_dump);
  788. struct list_head *seq_list_start(struct list_head *head, loff_t pos)
  789. {
  790. struct list_head *lh;
  791. list_for_each(lh, head)
  792. if (pos-- == 0)
  793. return lh;
  794. return NULL;
  795. }
  796. EXPORT_SYMBOL(seq_list_start);
  797. struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
  798. {
  799. if (!pos)
  800. return head;
  801. return seq_list_start(head, pos - 1);
  802. }
  803. EXPORT_SYMBOL(seq_list_start_head);
  804. struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
  805. {
  806. struct list_head *lh;
  807. lh = ((struct list_head *)v)->next;
  808. ++*ppos;
  809. return lh == head ? NULL : lh;
  810. }
  811. EXPORT_SYMBOL(seq_list_next);
  812. /**
  813. * seq_hlist_start - start an iteration of a hlist
  814. * @head: the head of the hlist
  815. * @pos: the start position of the sequence
  816. *
  817. * Called at seq_file->op->start().
  818. */
  819. struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
  820. {
  821. struct hlist_node *node;
  822. hlist_for_each(node, head)
  823. if (pos-- == 0)
  824. return node;
  825. return NULL;
  826. }
  827. EXPORT_SYMBOL(seq_hlist_start);
  828. /**
  829. * seq_hlist_start_head - start an iteration of a hlist
  830. * @head: the head of the hlist
  831. * @pos: the start position of the sequence
  832. *
  833. * Called at seq_file->op->start(). Call this function if you want to
  834. * print a header at the top of the output.
  835. */
  836. struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
  837. {
  838. if (!pos)
  839. return SEQ_START_TOKEN;
  840. return seq_hlist_start(head, pos - 1);
  841. }
  842. EXPORT_SYMBOL(seq_hlist_start_head);
  843. /**
  844. * seq_hlist_next - move to the next position of the hlist
  845. * @v: the current iterator
  846. * @head: the head of the hlist
  847. * @ppos: the current position
  848. *
  849. * Called at seq_file->op->next().
  850. */
  851. struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
  852. loff_t *ppos)
  853. {
  854. struct hlist_node *node = v;
  855. ++*ppos;
  856. if (v == SEQ_START_TOKEN)
  857. return head->first;
  858. else
  859. return node->next;
  860. }
  861. EXPORT_SYMBOL(seq_hlist_next);
  862. /**
  863. * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
  864. * @head: the head of the hlist
  865. * @pos: the start position of the sequence
  866. *
  867. * Called at seq_file->op->start().
  868. *
  869. * This list-traversal primitive may safely run concurrently with
  870. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  871. * as long as the traversal is guarded by rcu_read_lock().
  872. */
  873. struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
  874. loff_t pos)
  875. {
  876. struct hlist_node *node;
  877. __hlist_for_each_rcu(node, head)
  878. if (pos-- == 0)
  879. return node;
  880. return NULL;
  881. }
  882. EXPORT_SYMBOL(seq_hlist_start_rcu);
  883. /**
  884. * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
  885. * @head: the head of the hlist
  886. * @pos: the start position of the sequence
  887. *
  888. * Called at seq_file->op->start(). Call this function if you want to
  889. * print a header at the top of the output.
  890. *
  891. * This list-traversal primitive may safely run concurrently with
  892. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  893. * as long as the traversal is guarded by rcu_read_lock().
  894. */
  895. struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
  896. loff_t pos)
  897. {
  898. if (!pos)
  899. return SEQ_START_TOKEN;
  900. return seq_hlist_start_rcu(head, pos - 1);
  901. }
  902. EXPORT_SYMBOL(seq_hlist_start_head_rcu);
  903. /**
  904. * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
  905. * @v: the current iterator
  906. * @head: the head of the hlist
  907. * @ppos: the current position
  908. *
  909. * Called at seq_file->op->next().
  910. *
  911. * This list-traversal primitive may safely run concurrently with
  912. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  913. * as long as the traversal is guarded by rcu_read_lock().
  914. */
  915. struct hlist_node *seq_hlist_next_rcu(void *v,
  916. struct hlist_head *head,
  917. loff_t *ppos)
  918. {
  919. struct hlist_node *node = v;
  920. ++*ppos;
  921. if (v == SEQ_START_TOKEN)
  922. return rcu_dereference(head->first);
  923. else
  924. return rcu_dereference(node->next);
  925. }
  926. EXPORT_SYMBOL(seq_hlist_next_rcu);
  927. /**
  928. * seq_hlist_start_precpu - start an iteration of a percpu hlist array
  929. * @head: pointer to percpu array of struct hlist_heads
  930. * @cpu: pointer to cpu "cursor"
  931. * @pos: start position of sequence
  932. *
  933. * Called at seq_file->op->start().
  934. */
  935. struct hlist_node *
  936. seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
  937. {
  938. struct hlist_node *node;
  939. for_each_possible_cpu(*cpu) {
  940. hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
  941. if (pos-- == 0)
  942. return node;
  943. }
  944. }
  945. return NULL;
  946. }
  947. EXPORT_SYMBOL(seq_hlist_start_percpu);
  948. /**
  949. * seq_hlist_next_percpu - move to the next position of the percpu hlist array
  950. * @v: pointer to current hlist_node
  951. * @head: pointer to percpu array of struct hlist_heads
  952. * @cpu: pointer to cpu "cursor"
  953. * @pos: start position of sequence
  954. *
  955. * Called at seq_file->op->next().
  956. */
  957. struct hlist_node *
  958. seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
  959. int *cpu, loff_t *pos)
  960. {
  961. struct hlist_node *node = v;
  962. ++*pos;
  963. if (node->next)
  964. return node->next;
  965. for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
  966. *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
  967. struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
  968. if (!hlist_empty(bucket))
  969. return bucket->first;
  970. }
  971. return NULL;
  972. }
  973. EXPORT_SYMBOL(seq_hlist_next_percpu);
  974. void __init seq_file_init(void)
  975. {
  976. seq_file_cache = KMEM_CACHE(seq_file, SLAB_ACCOUNT|SLAB_PANIC);
  977. }