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 = (struct seq_file *)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 = (struct seq_file *)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 = (struct seq_file *)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. * root may be changed, see __d_path().
  429. */
  430. int seq_path_root(struct seq_file *m, struct path *path, struct path *root,
  431. char *esc)
  432. {
  433. char *buf;
  434. size_t size = seq_get_buf(m, &buf);
  435. int res = -ENAMETOOLONG;
  436. if (size) {
  437. char *p;
  438. spin_lock(&dcache_lock);
  439. p = __d_path(path, root, buf, size);
  440. spin_unlock(&dcache_lock);
  441. res = PTR_ERR(p);
  442. if (!IS_ERR(p)) {
  443. char *end = mangle_path(buf, p, esc);
  444. if (end)
  445. res = end - buf;
  446. else
  447. res = -ENAMETOOLONG;
  448. }
  449. }
  450. seq_commit(m, res);
  451. return res < 0 ? res : 0;
  452. }
  453. /*
  454. * returns the path of the 'dentry' from the root of its filesystem.
  455. */
  456. int seq_dentry(struct seq_file *m, struct dentry *dentry, char *esc)
  457. {
  458. char *buf;
  459. size_t size = seq_get_buf(m, &buf);
  460. int res = -1;
  461. if (size) {
  462. char *p = dentry_path(dentry, buf, size);
  463. if (!IS_ERR(p)) {
  464. char *end = mangle_path(buf, p, esc);
  465. if (end)
  466. res = end - buf;
  467. }
  468. }
  469. seq_commit(m, res);
  470. return res;
  471. }
  472. int seq_bitmap(struct seq_file *m, const unsigned long *bits,
  473. unsigned int nr_bits)
  474. {
  475. if (m->count < m->size) {
  476. int len = bitmap_scnprintf(m->buf + m->count,
  477. m->size - m->count, bits, nr_bits);
  478. if (m->count + len < m->size) {
  479. m->count += len;
  480. return 0;
  481. }
  482. }
  483. m->count = m->size;
  484. return -1;
  485. }
  486. EXPORT_SYMBOL(seq_bitmap);
  487. int seq_bitmap_list(struct seq_file *m, const unsigned long *bits,
  488. unsigned int nr_bits)
  489. {
  490. if (m->count < m->size) {
  491. int len = bitmap_scnlistprintf(m->buf + m->count,
  492. m->size - m->count, bits, nr_bits);
  493. if (m->count + len < m->size) {
  494. m->count += len;
  495. return 0;
  496. }
  497. }
  498. m->count = m->size;
  499. return -1;
  500. }
  501. EXPORT_SYMBOL(seq_bitmap_list);
  502. static void *single_start(struct seq_file *p, loff_t *pos)
  503. {
  504. return NULL + (*pos == 0);
  505. }
  506. static void *single_next(struct seq_file *p, void *v, loff_t *pos)
  507. {
  508. ++*pos;
  509. return NULL;
  510. }
  511. static void single_stop(struct seq_file *p, void *v)
  512. {
  513. }
  514. int single_open(struct file *file, int (*show)(struct seq_file *, void *),
  515. void *data)
  516. {
  517. struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
  518. int res = -ENOMEM;
  519. if (op) {
  520. op->start = single_start;
  521. op->next = single_next;
  522. op->stop = single_stop;
  523. op->show = show;
  524. res = seq_open(file, op);
  525. if (!res)
  526. ((struct seq_file *)file->private_data)->private = data;
  527. else
  528. kfree(op);
  529. }
  530. return res;
  531. }
  532. EXPORT_SYMBOL(single_open);
  533. int single_release(struct inode *inode, struct file *file)
  534. {
  535. const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
  536. int res = seq_release(inode, file);
  537. kfree(op);
  538. return res;
  539. }
  540. EXPORT_SYMBOL(single_release);
  541. int seq_release_private(struct inode *inode, struct file *file)
  542. {
  543. struct seq_file *seq = file->private_data;
  544. kfree(seq->private);
  545. seq->private = NULL;
  546. return seq_release(inode, file);
  547. }
  548. EXPORT_SYMBOL(seq_release_private);
  549. void *__seq_open_private(struct file *f, const struct seq_operations *ops,
  550. int psize)
  551. {
  552. int rc;
  553. void *private;
  554. struct seq_file *seq;
  555. private = kzalloc(psize, GFP_KERNEL);
  556. if (private == NULL)
  557. goto out;
  558. rc = seq_open(f, ops);
  559. if (rc < 0)
  560. goto out_free;
  561. seq = f->private_data;
  562. seq->private = private;
  563. return private;
  564. out_free:
  565. kfree(private);
  566. out:
  567. return NULL;
  568. }
  569. EXPORT_SYMBOL(__seq_open_private);
  570. int seq_open_private(struct file *filp, const struct seq_operations *ops,
  571. int psize)
  572. {
  573. return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
  574. }
  575. EXPORT_SYMBOL(seq_open_private);
  576. int seq_putc(struct seq_file *m, char c)
  577. {
  578. if (m->count < m->size) {
  579. m->buf[m->count++] = c;
  580. return 0;
  581. }
  582. return -1;
  583. }
  584. EXPORT_SYMBOL(seq_putc);
  585. int seq_puts(struct seq_file *m, const char *s)
  586. {
  587. int len = strlen(s);
  588. if (m->count + len < m->size) {
  589. memcpy(m->buf + m->count, s, len);
  590. m->count += len;
  591. return 0;
  592. }
  593. m->count = m->size;
  594. return -1;
  595. }
  596. EXPORT_SYMBOL(seq_puts);
  597. /**
  598. * seq_write - write arbitrary data to buffer
  599. * @seq: seq_file identifying the buffer to which data should be written
  600. * @data: data address
  601. * @len: number of bytes
  602. *
  603. * Return 0 on success, non-zero otherwise.
  604. */
  605. int seq_write(struct seq_file *seq, const void *data, size_t len)
  606. {
  607. if (seq->count + len < seq->size) {
  608. memcpy(seq->buf + seq->count, data, len);
  609. seq->count += len;
  610. return 0;
  611. }
  612. seq->count = seq->size;
  613. return -1;
  614. }
  615. EXPORT_SYMBOL(seq_write);
  616. struct list_head *seq_list_start(struct list_head *head, loff_t pos)
  617. {
  618. struct list_head *lh;
  619. list_for_each(lh, head)
  620. if (pos-- == 0)
  621. return lh;
  622. return NULL;
  623. }
  624. EXPORT_SYMBOL(seq_list_start);
  625. struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
  626. {
  627. if (!pos)
  628. return head;
  629. return seq_list_start(head, pos - 1);
  630. }
  631. EXPORT_SYMBOL(seq_list_start_head);
  632. struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
  633. {
  634. struct list_head *lh;
  635. lh = ((struct list_head *)v)->next;
  636. ++*ppos;
  637. return lh == head ? NULL : lh;
  638. }
  639. EXPORT_SYMBOL(seq_list_next);
  640. /**
  641. * seq_hlist_start - start an iteration of a hlist
  642. * @head: the head of the hlist
  643. * @pos: the start position of the sequence
  644. *
  645. * Called at seq_file->op->start().
  646. */
  647. struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
  648. {
  649. struct hlist_node *node;
  650. hlist_for_each(node, head)
  651. if (pos-- == 0)
  652. return node;
  653. return NULL;
  654. }
  655. EXPORT_SYMBOL(seq_hlist_start);
  656. /**
  657. * seq_hlist_start_head - start an iteration of a hlist
  658. * @head: the head of the hlist
  659. * @pos: the start position of the sequence
  660. *
  661. * Called at seq_file->op->start(). Call this function if you want to
  662. * print a header at the top of the output.
  663. */
  664. struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
  665. {
  666. if (!pos)
  667. return SEQ_START_TOKEN;
  668. return seq_hlist_start(head, pos - 1);
  669. }
  670. EXPORT_SYMBOL(seq_hlist_start_head);
  671. /**
  672. * seq_hlist_next - move to the next position of the hlist
  673. * @v: the current iterator
  674. * @head: the head of the hlist
  675. * @ppos: the current position
  676. *
  677. * Called at seq_file->op->next().
  678. */
  679. struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
  680. loff_t *ppos)
  681. {
  682. struct hlist_node *node = v;
  683. ++*ppos;
  684. if (v == SEQ_START_TOKEN)
  685. return head->first;
  686. else
  687. return node->next;
  688. }
  689. EXPORT_SYMBOL(seq_hlist_next);
  690. /**
  691. * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
  692. * @head: the head of the hlist
  693. * @pos: the start position of the sequence
  694. *
  695. * Called at seq_file->op->start().
  696. *
  697. * This list-traversal primitive may safely run concurrently with
  698. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  699. * as long as the traversal is guarded by rcu_read_lock().
  700. */
  701. struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
  702. loff_t pos)
  703. {
  704. struct hlist_node *node;
  705. __hlist_for_each_rcu(node, head)
  706. if (pos-- == 0)
  707. return node;
  708. return NULL;
  709. }
  710. EXPORT_SYMBOL(seq_hlist_start_rcu);
  711. /**
  712. * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
  713. * @head: the head of the hlist
  714. * @pos: the start position of the sequence
  715. *
  716. * Called at seq_file->op->start(). Call this function if you want to
  717. * print a header at the top of the output.
  718. *
  719. * This list-traversal primitive may safely run concurrently with
  720. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  721. * as long as the traversal is guarded by rcu_read_lock().
  722. */
  723. struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
  724. loff_t pos)
  725. {
  726. if (!pos)
  727. return SEQ_START_TOKEN;
  728. return seq_hlist_start_rcu(head, pos - 1);
  729. }
  730. EXPORT_SYMBOL(seq_hlist_start_head_rcu);
  731. /**
  732. * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
  733. * @v: the current iterator
  734. * @head: the head of the hlist
  735. * @ppos: the current position
  736. *
  737. * Called at seq_file->op->next().
  738. *
  739. * This list-traversal primitive may safely run concurrently with
  740. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  741. * as long as the traversal is guarded by rcu_read_lock().
  742. */
  743. struct hlist_node *seq_hlist_next_rcu(void *v,
  744. struct hlist_head *head,
  745. loff_t *ppos)
  746. {
  747. struct hlist_node *node = v;
  748. ++*ppos;
  749. if (v == SEQ_START_TOKEN)
  750. return rcu_dereference(head->first);
  751. else
  752. return rcu_dereference(node->next);
  753. }
  754. EXPORT_SYMBOL(seq_hlist_next_rcu);