seq_file.c 18 KB

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