seq_file.c 21 KB

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