seq_file.c 20 KB

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