seq_file.c 21 KB

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