seq_file.c 20 KB

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