seq_file.c 22 KB

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