seq_file.c 23 KB

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