seq_file.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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_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. int seq_putc(struct seq_file *m, char c)
  600. {
  601. if (m->count < m->size) {
  602. m->buf[m->count++] = c;
  603. return 0;
  604. }
  605. return -1;
  606. }
  607. EXPORT_SYMBOL(seq_putc);
  608. int seq_puts(struct seq_file *m, const char *s)
  609. {
  610. int len = strlen(s);
  611. if (m->count + len < m->size) {
  612. memcpy(m->buf + m->count, s, len);
  613. m->count += len;
  614. return 0;
  615. }
  616. seq_set_overflow(m);
  617. return -1;
  618. }
  619. EXPORT_SYMBOL(seq_puts);
  620. /*
  621. * A helper routine for putting decimal numbers without rich format of printf().
  622. * only 'unsigned long long' is supported.
  623. * This routine will put one byte delimiter + number into seq_file.
  624. * This routine is very quick when you show lots of numbers.
  625. * In usual cases, it will be better to use seq_printf(). It's easier to read.
  626. */
  627. int seq_put_decimal_ull(struct seq_file *m, char delimiter,
  628. unsigned long long num)
  629. {
  630. int len;
  631. if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
  632. goto overflow;
  633. if (delimiter)
  634. m->buf[m->count++] = delimiter;
  635. if (num < 10) {
  636. m->buf[m->count++] = num + '0';
  637. return 0;
  638. }
  639. len = num_to_str(m->buf + m->count, m->size - m->count, num);
  640. if (!len)
  641. goto overflow;
  642. m->count += len;
  643. return 0;
  644. overflow:
  645. seq_set_overflow(m);
  646. return -1;
  647. }
  648. EXPORT_SYMBOL(seq_put_decimal_ull);
  649. int seq_put_decimal_ll(struct seq_file *m, char delimiter,
  650. long long num)
  651. {
  652. if (num < 0) {
  653. if (m->count + 3 >= m->size) {
  654. seq_set_overflow(m);
  655. return -1;
  656. }
  657. if (delimiter)
  658. m->buf[m->count++] = delimiter;
  659. num = -num;
  660. delimiter = '-';
  661. }
  662. return seq_put_decimal_ull(m, delimiter, num);
  663. }
  664. EXPORT_SYMBOL(seq_put_decimal_ll);
  665. /**
  666. * seq_write - write arbitrary data to buffer
  667. * @seq: seq_file identifying the buffer to which data should be written
  668. * @data: data address
  669. * @len: number of bytes
  670. *
  671. * Return 0 on success, non-zero otherwise.
  672. */
  673. int seq_write(struct seq_file *seq, const void *data, size_t len)
  674. {
  675. if (seq->count + len < seq->size) {
  676. memcpy(seq->buf + seq->count, data, len);
  677. seq->count += len;
  678. return 0;
  679. }
  680. seq_set_overflow(seq);
  681. return -1;
  682. }
  683. EXPORT_SYMBOL(seq_write);
  684. struct list_head *seq_list_start(struct list_head *head, loff_t pos)
  685. {
  686. struct list_head *lh;
  687. list_for_each(lh, head)
  688. if (pos-- == 0)
  689. return lh;
  690. return NULL;
  691. }
  692. EXPORT_SYMBOL(seq_list_start);
  693. struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
  694. {
  695. if (!pos)
  696. return head;
  697. return seq_list_start(head, pos - 1);
  698. }
  699. EXPORT_SYMBOL(seq_list_start_head);
  700. struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
  701. {
  702. struct list_head *lh;
  703. lh = ((struct list_head *)v)->next;
  704. ++*ppos;
  705. return lh == head ? NULL : lh;
  706. }
  707. EXPORT_SYMBOL(seq_list_next);
  708. /**
  709. * seq_hlist_start - start an iteration of a hlist
  710. * @head: the head of the hlist
  711. * @pos: the start position of the sequence
  712. *
  713. * Called at seq_file->op->start().
  714. */
  715. struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
  716. {
  717. struct hlist_node *node;
  718. hlist_for_each(node, head)
  719. if (pos-- == 0)
  720. return node;
  721. return NULL;
  722. }
  723. EXPORT_SYMBOL(seq_hlist_start);
  724. /**
  725. * seq_hlist_start_head - start an iteration of a hlist
  726. * @head: the head of the hlist
  727. * @pos: the start position of the sequence
  728. *
  729. * Called at seq_file->op->start(). Call this function if you want to
  730. * print a header at the top of the output.
  731. */
  732. struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
  733. {
  734. if (!pos)
  735. return SEQ_START_TOKEN;
  736. return seq_hlist_start(head, pos - 1);
  737. }
  738. EXPORT_SYMBOL(seq_hlist_start_head);
  739. /**
  740. * seq_hlist_next - move to the next position of the hlist
  741. * @v: the current iterator
  742. * @head: the head of the hlist
  743. * @ppos: the current position
  744. *
  745. * Called at seq_file->op->next().
  746. */
  747. struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
  748. loff_t *ppos)
  749. {
  750. struct hlist_node *node = v;
  751. ++*ppos;
  752. if (v == SEQ_START_TOKEN)
  753. return head->first;
  754. else
  755. return node->next;
  756. }
  757. EXPORT_SYMBOL(seq_hlist_next);
  758. /**
  759. * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
  760. * @head: the head of the hlist
  761. * @pos: the start position of the sequence
  762. *
  763. * Called at seq_file->op->start().
  764. *
  765. * This list-traversal primitive may safely run concurrently with
  766. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  767. * as long as the traversal is guarded by rcu_read_lock().
  768. */
  769. struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
  770. loff_t pos)
  771. {
  772. struct hlist_node *node;
  773. __hlist_for_each_rcu(node, head)
  774. if (pos-- == 0)
  775. return node;
  776. return NULL;
  777. }
  778. EXPORT_SYMBOL(seq_hlist_start_rcu);
  779. /**
  780. * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
  781. * @head: the head of the hlist
  782. * @pos: the start position of the sequence
  783. *
  784. * Called at seq_file->op->start(). Call this function if you want to
  785. * print a header at the top of the output.
  786. *
  787. * This list-traversal primitive may safely run concurrently with
  788. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  789. * as long as the traversal is guarded by rcu_read_lock().
  790. */
  791. struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
  792. loff_t pos)
  793. {
  794. if (!pos)
  795. return SEQ_START_TOKEN;
  796. return seq_hlist_start_rcu(head, pos - 1);
  797. }
  798. EXPORT_SYMBOL(seq_hlist_start_head_rcu);
  799. /**
  800. * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
  801. * @v: the current iterator
  802. * @head: the head of the hlist
  803. * @ppos: the current position
  804. *
  805. * Called at seq_file->op->next().
  806. *
  807. * This list-traversal primitive may safely run concurrently with
  808. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  809. * as long as the traversal is guarded by rcu_read_lock().
  810. */
  811. struct hlist_node *seq_hlist_next_rcu(void *v,
  812. struct hlist_head *head,
  813. loff_t *ppos)
  814. {
  815. struct hlist_node *node = v;
  816. ++*ppos;
  817. if (v == SEQ_START_TOKEN)
  818. return rcu_dereference(head->first);
  819. else
  820. return rcu_dereference(node->next);
  821. }
  822. EXPORT_SYMBOL(seq_hlist_next_rcu);