seq_file.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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 <linux/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. /*
  174. * if request is to read from zero offset, reset iterator to first
  175. * record as it might have been already advanced by previous requests
  176. */
  177. if (*ppos == 0)
  178. m->index = 0;
  179. /* Don't assume *ppos is where we left it */
  180. if (unlikely(*ppos != m->read_pos)) {
  181. while ((err = traverse(m, *ppos)) == -EAGAIN)
  182. ;
  183. if (err) {
  184. /* With prejudice... */
  185. m->read_pos = 0;
  186. m->version = 0;
  187. m->index = 0;
  188. m->count = 0;
  189. goto Done;
  190. } else {
  191. m->read_pos = *ppos;
  192. }
  193. }
  194. /* grab buffer if we didn't have one */
  195. if (!m->buf) {
  196. m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
  197. if (!m->buf)
  198. goto Enomem;
  199. }
  200. /* if not empty - flush it first */
  201. if (m->count) {
  202. n = min(m->count, size);
  203. err = copy_to_user(buf, m->buf + m->from, n);
  204. if (err)
  205. goto Efault;
  206. m->count -= n;
  207. m->from += n;
  208. size -= n;
  209. buf += n;
  210. copied += n;
  211. if (!m->count) {
  212. m->from = 0;
  213. m->index++;
  214. }
  215. if (!size)
  216. goto Done;
  217. }
  218. /* we need at least one record in buffer */
  219. pos = m->index;
  220. p = m->op->start(m, &pos);
  221. while (1) {
  222. err = PTR_ERR(p);
  223. if (!p || IS_ERR(p))
  224. break;
  225. err = m->op->show(m, p);
  226. if (err < 0)
  227. break;
  228. if (unlikely(err))
  229. m->count = 0;
  230. if (unlikely(!m->count)) {
  231. p = m->op->next(m, p, &pos);
  232. m->index = pos;
  233. continue;
  234. }
  235. if (m->count < m->size)
  236. goto Fill;
  237. m->op->stop(m, p);
  238. kvfree(m->buf);
  239. m->count = 0;
  240. m->buf = seq_buf_alloc(m->size <<= 1);
  241. if (!m->buf)
  242. goto Enomem;
  243. m->version = 0;
  244. pos = m->index;
  245. p = m->op->start(m, &pos);
  246. }
  247. m->op->stop(m, p);
  248. m->count = 0;
  249. goto Done;
  250. Fill:
  251. /* they want more? let's try to get some more */
  252. while (m->count < size) {
  253. size_t offs = m->count;
  254. loff_t next = pos;
  255. p = m->op->next(m, p, &next);
  256. if (!p || IS_ERR(p)) {
  257. err = PTR_ERR(p);
  258. break;
  259. }
  260. err = m->op->show(m, p);
  261. if (seq_has_overflowed(m) || err) {
  262. m->count = offs;
  263. if (likely(err <= 0))
  264. break;
  265. }
  266. pos = next;
  267. }
  268. m->op->stop(m, p);
  269. n = min(m->count, size);
  270. err = copy_to_user(buf, m->buf, n);
  271. if (err)
  272. goto Efault;
  273. copied += n;
  274. m->count -= n;
  275. if (m->count)
  276. m->from = n;
  277. else
  278. pos++;
  279. m->index = pos;
  280. Done:
  281. if (!copied)
  282. copied = err;
  283. else {
  284. *ppos += copied;
  285. m->read_pos += copied;
  286. }
  287. file->f_version = m->version;
  288. mutex_unlock(&m->lock);
  289. return copied;
  290. Enomem:
  291. err = -ENOMEM;
  292. goto Done;
  293. Efault:
  294. err = -EFAULT;
  295. goto Done;
  296. }
  297. EXPORT_SYMBOL(seq_read);
  298. /**
  299. * seq_lseek - ->llseek() method for sequential files.
  300. * @file: the file in question
  301. * @offset: new position
  302. * @whence: 0 for absolute, 1 for relative position
  303. *
  304. * Ready-made ->f_op->llseek()
  305. */
  306. loff_t seq_lseek(struct file *file, loff_t offset, int whence)
  307. {
  308. struct seq_file *m = file->private_data;
  309. loff_t retval = -EINVAL;
  310. mutex_lock(&m->lock);
  311. m->version = file->f_version;
  312. switch (whence) {
  313. case SEEK_CUR:
  314. offset += file->f_pos;
  315. case SEEK_SET:
  316. if (offset < 0)
  317. break;
  318. retval = offset;
  319. if (offset != m->read_pos) {
  320. while ((retval = traverse(m, offset)) == -EAGAIN)
  321. ;
  322. if (retval) {
  323. /* with extreme prejudice... */
  324. file->f_pos = 0;
  325. m->read_pos = 0;
  326. m->version = 0;
  327. m->index = 0;
  328. m->count = 0;
  329. } else {
  330. m->read_pos = offset;
  331. retval = file->f_pos = offset;
  332. }
  333. } else {
  334. file->f_pos = offset;
  335. }
  336. }
  337. file->f_version = m->version;
  338. mutex_unlock(&m->lock);
  339. return retval;
  340. }
  341. EXPORT_SYMBOL(seq_lseek);
  342. /**
  343. * seq_release - free the structures associated with sequential file.
  344. * @file: file in question
  345. * @inode: its inode
  346. *
  347. * Frees the structures associated with sequential file; can be used
  348. * as ->f_op->release() if you don't have private data to destroy.
  349. */
  350. int seq_release(struct inode *inode, struct file *file)
  351. {
  352. struct seq_file *m = file->private_data;
  353. kvfree(m->buf);
  354. kfree(m);
  355. return 0;
  356. }
  357. EXPORT_SYMBOL(seq_release);
  358. /**
  359. * seq_escape - print string into buffer, escaping some characters
  360. * @m: target buffer
  361. * @s: string
  362. * @esc: set of characters that need escaping
  363. *
  364. * Puts string into buffer, replacing each occurrence of character from
  365. * @esc with usual octal escape.
  366. * Use seq_has_overflowed() to check for errors.
  367. */
  368. void seq_escape(struct seq_file *m, const char *s, const char *esc)
  369. {
  370. char *buf;
  371. size_t size = seq_get_buf(m, &buf);
  372. int ret;
  373. ret = string_escape_str(s, buf, size, ESCAPE_OCTAL, esc);
  374. seq_commit(m, ret < size ? ret : -1);
  375. }
  376. EXPORT_SYMBOL(seq_escape);
  377. void seq_vprintf(struct seq_file *m, const char *f, va_list args)
  378. {
  379. int len;
  380. if (m->count < m->size) {
  381. len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
  382. if (m->count + len < m->size) {
  383. m->count += len;
  384. return;
  385. }
  386. }
  387. seq_set_overflow(m);
  388. }
  389. EXPORT_SYMBOL(seq_vprintf);
  390. void seq_printf(struct seq_file *m, const char *f, ...)
  391. {
  392. va_list args;
  393. va_start(args, f);
  394. seq_vprintf(m, f, args);
  395. va_end(args);
  396. }
  397. EXPORT_SYMBOL(seq_printf);
  398. /**
  399. * mangle_path - mangle and copy path to buffer beginning
  400. * @s: buffer start
  401. * @p: beginning of path in above buffer
  402. * @esc: set of characters that need escaping
  403. *
  404. * Copy the path from @p to @s, replacing each occurrence of character from
  405. * @esc with usual octal escape.
  406. * Returns pointer past last written character in @s, or NULL in case of
  407. * failure.
  408. */
  409. char *mangle_path(char *s, const char *p, const char *esc)
  410. {
  411. while (s <= p) {
  412. char c = *p++;
  413. if (!c) {
  414. return s;
  415. } else if (!strchr(esc, c)) {
  416. *s++ = c;
  417. } else if (s + 4 > p) {
  418. break;
  419. } else {
  420. *s++ = '\\';
  421. *s++ = '0' + ((c & 0300) >> 6);
  422. *s++ = '0' + ((c & 070) >> 3);
  423. *s++ = '0' + (c & 07);
  424. }
  425. }
  426. return NULL;
  427. }
  428. EXPORT_SYMBOL(mangle_path);
  429. /**
  430. * seq_path - seq_file interface to print a pathname
  431. * @m: the seq_file handle
  432. * @path: the struct path to print
  433. * @esc: set of characters to escape in the output
  434. *
  435. * return the absolute path of 'path', as represented by the
  436. * dentry / mnt pair in the path parameter.
  437. */
  438. int seq_path(struct seq_file *m, const struct path *path, const char *esc)
  439. {
  440. char *buf;
  441. size_t size = seq_get_buf(m, &buf);
  442. int res = -1;
  443. if (size) {
  444. char *p = d_path(path, buf, size);
  445. if (!IS_ERR(p)) {
  446. char *end = mangle_path(buf, p, esc);
  447. if (end)
  448. res = end - buf;
  449. }
  450. }
  451. seq_commit(m, res);
  452. return res;
  453. }
  454. EXPORT_SYMBOL(seq_path);
  455. /**
  456. * seq_file_path - seq_file interface to print a pathname of a file
  457. * @m: the seq_file handle
  458. * @file: the struct file to print
  459. * @esc: set of characters to escape in the output
  460. *
  461. * return the absolute path to the file.
  462. */
  463. int seq_file_path(struct seq_file *m, struct file *file, const char *esc)
  464. {
  465. return seq_path(m, &file->f_path, esc);
  466. }
  467. EXPORT_SYMBOL(seq_file_path);
  468. /*
  469. * Same as seq_path, but relative to supplied root.
  470. */
  471. int seq_path_root(struct seq_file *m, const struct path *path,
  472. const struct path *root, const char *esc)
  473. {
  474. char *buf;
  475. size_t size = seq_get_buf(m, &buf);
  476. int res = -ENAMETOOLONG;
  477. if (size) {
  478. char *p;
  479. p = __d_path(path, root, buf, size);
  480. if (!p)
  481. return SEQ_SKIP;
  482. res = PTR_ERR(p);
  483. if (!IS_ERR(p)) {
  484. char *end = mangle_path(buf, p, esc);
  485. if (end)
  486. res = end - buf;
  487. else
  488. res = -ENAMETOOLONG;
  489. }
  490. }
  491. seq_commit(m, res);
  492. return res < 0 && res != -ENAMETOOLONG ? res : 0;
  493. }
  494. /*
  495. * returns the path of the 'dentry' from the root of its filesystem.
  496. */
  497. int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
  498. {
  499. char *buf;
  500. size_t size = seq_get_buf(m, &buf);
  501. int res = -1;
  502. if (size) {
  503. char *p = dentry_path(dentry, buf, size);
  504. if (!IS_ERR(p)) {
  505. char *end = mangle_path(buf, p, esc);
  506. if (end)
  507. res = end - buf;
  508. }
  509. }
  510. seq_commit(m, res);
  511. return res;
  512. }
  513. EXPORT_SYMBOL(seq_dentry);
  514. static void *single_start(struct seq_file *p, loff_t *pos)
  515. {
  516. return NULL + (*pos == 0);
  517. }
  518. static void *single_next(struct seq_file *p, void *v, loff_t *pos)
  519. {
  520. ++*pos;
  521. return NULL;
  522. }
  523. static void single_stop(struct seq_file *p, void *v)
  524. {
  525. }
  526. int single_open(struct file *file, int (*show)(struct seq_file *, void *),
  527. void *data)
  528. {
  529. struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
  530. int res = -ENOMEM;
  531. if (op) {
  532. op->start = single_start;
  533. op->next = single_next;
  534. op->stop = single_stop;
  535. op->show = show;
  536. res = seq_open(file, op);
  537. if (!res)
  538. ((struct seq_file *)file->private_data)->private = data;
  539. else
  540. kfree(op);
  541. }
  542. return res;
  543. }
  544. EXPORT_SYMBOL(single_open);
  545. int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
  546. void *data, size_t size)
  547. {
  548. char *buf = seq_buf_alloc(size);
  549. int ret;
  550. if (!buf)
  551. return -ENOMEM;
  552. ret = single_open(file, show, data);
  553. if (ret) {
  554. kvfree(buf);
  555. return ret;
  556. }
  557. ((struct seq_file *)file->private_data)->buf = buf;
  558. ((struct seq_file *)file->private_data)->size = size;
  559. return 0;
  560. }
  561. EXPORT_SYMBOL(single_open_size);
  562. int single_release(struct inode *inode, struct file *file)
  563. {
  564. const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
  565. int res = seq_release(inode, file);
  566. kfree(op);
  567. return res;
  568. }
  569. EXPORT_SYMBOL(single_release);
  570. int seq_release_private(struct inode *inode, struct file *file)
  571. {
  572. struct seq_file *seq = file->private_data;
  573. kfree(seq->private);
  574. seq->private = NULL;
  575. return seq_release(inode, file);
  576. }
  577. EXPORT_SYMBOL(seq_release_private);
  578. void *__seq_open_private(struct file *f, const struct seq_operations *ops,
  579. int psize)
  580. {
  581. int rc;
  582. void *private;
  583. struct seq_file *seq;
  584. private = kzalloc(psize, GFP_KERNEL);
  585. if (private == NULL)
  586. goto out;
  587. rc = seq_open(f, ops);
  588. if (rc < 0)
  589. goto out_free;
  590. seq = f->private_data;
  591. seq->private = private;
  592. return private;
  593. out_free:
  594. kfree(private);
  595. out:
  596. return NULL;
  597. }
  598. EXPORT_SYMBOL(__seq_open_private);
  599. int seq_open_private(struct file *filp, const struct seq_operations *ops,
  600. int psize)
  601. {
  602. return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
  603. }
  604. EXPORT_SYMBOL(seq_open_private);
  605. void seq_putc(struct seq_file *m, char c)
  606. {
  607. if (m->count >= m->size)
  608. return;
  609. m->buf[m->count++] = c;
  610. }
  611. EXPORT_SYMBOL(seq_putc);
  612. void seq_puts(struct seq_file *m, const char *s)
  613. {
  614. int len = strlen(s);
  615. if (m->count + len >= m->size) {
  616. seq_set_overflow(m);
  617. return;
  618. }
  619. memcpy(m->buf + m->count, s, len);
  620. m->count += len;
  621. }
  622. EXPORT_SYMBOL(seq_puts);
  623. /*
  624. * A helper routine for putting decimal numbers without rich format of printf().
  625. * only 'unsigned long long' is supported.
  626. * This routine will put strlen(delimiter) + number into seq_file.
  627. * This routine is very quick when you show lots of numbers.
  628. * In usual cases, it will be better to use seq_printf(). It's easier to read.
  629. */
  630. void seq_put_decimal_ull(struct seq_file *m, const char *delimiter,
  631. unsigned long long num)
  632. {
  633. int len;
  634. if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
  635. goto overflow;
  636. len = strlen(delimiter);
  637. if (m->count + len >= m->size)
  638. goto overflow;
  639. memcpy(m->buf + m->count, delimiter, len);
  640. m->count += len;
  641. if (m->count + 1 >= m->size)
  642. goto overflow;
  643. if (num < 10) {
  644. m->buf[m->count++] = num + '0';
  645. return;
  646. }
  647. len = num_to_str(m->buf + m->count, m->size - m->count, num);
  648. if (!len)
  649. goto overflow;
  650. m->count += len;
  651. return;
  652. overflow:
  653. seq_set_overflow(m);
  654. }
  655. EXPORT_SYMBOL(seq_put_decimal_ull);
  656. void seq_put_decimal_ll(struct seq_file *m, const char *delimiter, long long num)
  657. {
  658. int len;
  659. if (m->count + 3 >= m->size) /* we'll write 2 bytes at least */
  660. goto overflow;
  661. len = strlen(delimiter);
  662. if (m->count + len >= m->size)
  663. goto overflow;
  664. memcpy(m->buf + m->count, delimiter, len);
  665. m->count += len;
  666. if (m->count + 2 >= m->size)
  667. goto overflow;
  668. if (num < 0) {
  669. m->buf[m->count++] = '-';
  670. num = -num;
  671. }
  672. if (num < 10) {
  673. m->buf[m->count++] = num + '0';
  674. return;
  675. }
  676. len = num_to_str(m->buf + m->count, m->size - m->count, num);
  677. if (!len)
  678. goto overflow;
  679. m->count += len;
  680. return;
  681. overflow:
  682. seq_set_overflow(m);
  683. }
  684. EXPORT_SYMBOL(seq_put_decimal_ll);
  685. /**
  686. * seq_write - write arbitrary data to buffer
  687. * @seq: seq_file identifying the buffer to which data should be written
  688. * @data: data address
  689. * @len: number of bytes
  690. *
  691. * Return 0 on success, non-zero otherwise.
  692. */
  693. int seq_write(struct seq_file *seq, const void *data, size_t len)
  694. {
  695. if (seq->count + len < seq->size) {
  696. memcpy(seq->buf + seq->count, data, len);
  697. seq->count += len;
  698. return 0;
  699. }
  700. seq_set_overflow(seq);
  701. return -1;
  702. }
  703. EXPORT_SYMBOL(seq_write);
  704. /**
  705. * seq_pad - write padding spaces to buffer
  706. * @m: seq_file identifying the buffer to which data should be written
  707. * @c: the byte to append after padding if non-zero
  708. */
  709. void seq_pad(struct seq_file *m, char c)
  710. {
  711. int size = m->pad_until - m->count;
  712. if (size > 0)
  713. seq_printf(m, "%*s", size, "");
  714. if (c)
  715. seq_putc(m, c);
  716. }
  717. EXPORT_SYMBOL(seq_pad);
  718. /* A complete analogue of print_hex_dump() */
  719. void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
  720. int rowsize, int groupsize, const void *buf, size_t len,
  721. bool ascii)
  722. {
  723. const u8 *ptr = buf;
  724. int i, linelen, remaining = len;
  725. char *buffer;
  726. size_t size;
  727. int ret;
  728. if (rowsize != 16 && rowsize != 32)
  729. rowsize = 16;
  730. for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
  731. linelen = min(remaining, rowsize);
  732. remaining -= rowsize;
  733. switch (prefix_type) {
  734. case DUMP_PREFIX_ADDRESS:
  735. seq_printf(m, "%s%p: ", prefix_str, ptr + i);
  736. break;
  737. case DUMP_PREFIX_OFFSET:
  738. seq_printf(m, "%s%.8x: ", prefix_str, i);
  739. break;
  740. default:
  741. seq_printf(m, "%s", prefix_str);
  742. break;
  743. }
  744. size = seq_get_buf(m, &buffer);
  745. ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
  746. buffer, size, ascii);
  747. seq_commit(m, ret < size ? ret : -1);
  748. seq_putc(m, '\n');
  749. }
  750. }
  751. EXPORT_SYMBOL(seq_hex_dump);
  752. struct list_head *seq_list_start(struct list_head *head, loff_t pos)
  753. {
  754. struct list_head *lh;
  755. list_for_each(lh, head)
  756. if (pos-- == 0)
  757. return lh;
  758. return NULL;
  759. }
  760. EXPORT_SYMBOL(seq_list_start);
  761. struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
  762. {
  763. if (!pos)
  764. return head;
  765. return seq_list_start(head, pos - 1);
  766. }
  767. EXPORT_SYMBOL(seq_list_start_head);
  768. struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
  769. {
  770. struct list_head *lh;
  771. lh = ((struct list_head *)v)->next;
  772. ++*ppos;
  773. return lh == head ? NULL : lh;
  774. }
  775. EXPORT_SYMBOL(seq_list_next);
  776. /**
  777. * seq_hlist_start - start an iteration of a hlist
  778. * @head: the head of the hlist
  779. * @pos: the start position of the sequence
  780. *
  781. * Called at seq_file->op->start().
  782. */
  783. struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
  784. {
  785. struct hlist_node *node;
  786. hlist_for_each(node, head)
  787. if (pos-- == 0)
  788. return node;
  789. return NULL;
  790. }
  791. EXPORT_SYMBOL(seq_hlist_start);
  792. /**
  793. * seq_hlist_start_head - start an iteration of a hlist
  794. * @head: the head of the hlist
  795. * @pos: the start position of the sequence
  796. *
  797. * Called at seq_file->op->start(). Call this function if you want to
  798. * print a header at the top of the output.
  799. */
  800. struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
  801. {
  802. if (!pos)
  803. return SEQ_START_TOKEN;
  804. return seq_hlist_start(head, pos - 1);
  805. }
  806. EXPORT_SYMBOL(seq_hlist_start_head);
  807. /**
  808. * seq_hlist_next - move to the next position of the hlist
  809. * @v: the current iterator
  810. * @head: the head of the hlist
  811. * @ppos: the current position
  812. *
  813. * Called at seq_file->op->next().
  814. */
  815. struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
  816. loff_t *ppos)
  817. {
  818. struct hlist_node *node = v;
  819. ++*ppos;
  820. if (v == SEQ_START_TOKEN)
  821. return head->first;
  822. else
  823. return node->next;
  824. }
  825. EXPORT_SYMBOL(seq_hlist_next);
  826. /**
  827. * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
  828. * @head: the head of the hlist
  829. * @pos: the start position of the sequence
  830. *
  831. * Called at seq_file->op->start().
  832. *
  833. * This list-traversal primitive may safely run concurrently with
  834. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  835. * as long as the traversal is guarded by rcu_read_lock().
  836. */
  837. struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
  838. loff_t pos)
  839. {
  840. struct hlist_node *node;
  841. __hlist_for_each_rcu(node, head)
  842. if (pos-- == 0)
  843. return node;
  844. return NULL;
  845. }
  846. EXPORT_SYMBOL(seq_hlist_start_rcu);
  847. /**
  848. * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
  849. * @head: the head of the hlist
  850. * @pos: the start position of the sequence
  851. *
  852. * Called at seq_file->op->start(). Call this function if you want to
  853. * print a header at the top of the output.
  854. *
  855. * This list-traversal primitive may safely run concurrently with
  856. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  857. * as long as the traversal is guarded by rcu_read_lock().
  858. */
  859. struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
  860. loff_t pos)
  861. {
  862. if (!pos)
  863. return SEQ_START_TOKEN;
  864. return seq_hlist_start_rcu(head, pos - 1);
  865. }
  866. EXPORT_SYMBOL(seq_hlist_start_head_rcu);
  867. /**
  868. * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
  869. * @v: the current iterator
  870. * @head: the head of the hlist
  871. * @ppos: the current position
  872. *
  873. * Called at seq_file->op->next().
  874. *
  875. * This list-traversal primitive may safely run concurrently with
  876. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  877. * as long as the traversal is guarded by rcu_read_lock().
  878. */
  879. struct hlist_node *seq_hlist_next_rcu(void *v,
  880. struct hlist_head *head,
  881. loff_t *ppos)
  882. {
  883. struct hlist_node *node = v;
  884. ++*ppos;
  885. if (v == SEQ_START_TOKEN)
  886. return rcu_dereference(head->first);
  887. else
  888. return rcu_dereference(node->next);
  889. }
  890. EXPORT_SYMBOL(seq_hlist_next_rcu);
  891. /**
  892. * seq_hlist_start_precpu - start an iteration of a percpu hlist array
  893. * @head: pointer to percpu array of struct hlist_heads
  894. * @cpu: pointer to cpu "cursor"
  895. * @pos: start position of sequence
  896. *
  897. * Called at seq_file->op->start().
  898. */
  899. struct hlist_node *
  900. seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
  901. {
  902. struct hlist_node *node;
  903. for_each_possible_cpu(*cpu) {
  904. hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
  905. if (pos-- == 0)
  906. return node;
  907. }
  908. }
  909. return NULL;
  910. }
  911. EXPORT_SYMBOL(seq_hlist_start_percpu);
  912. /**
  913. * seq_hlist_next_percpu - move to the next position of the percpu hlist array
  914. * @v: pointer to current hlist_node
  915. * @head: pointer to percpu array of struct hlist_heads
  916. * @cpu: pointer to cpu "cursor"
  917. * @pos: start position of sequence
  918. *
  919. * Called at seq_file->op->next().
  920. */
  921. struct hlist_node *
  922. seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
  923. int *cpu, loff_t *pos)
  924. {
  925. struct hlist_node *node = v;
  926. ++*pos;
  927. if (node->next)
  928. return node->next;
  929. for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
  930. *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
  931. struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
  932. if (!hlist_empty(bucket))
  933. return bucket->first;
  934. }
  935. return NULL;
  936. }
  937. EXPORT_SYMBOL(seq_hlist_next_percpu);