seq_file.c 18 KB

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