seq_file.c 19 KB

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