seq_file.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. if (m->count < m->size) {
  411. char *s = m->buf + m->count;
  412. char *p = d_path(path, s, m->size - m->count);
  413. if (!IS_ERR(p)) {
  414. s = mangle_path(s, p, esc);
  415. if (s) {
  416. p = m->buf + m->count;
  417. m->count = s - m->buf;
  418. return s - p;
  419. }
  420. }
  421. }
  422. m->count = m->size;
  423. return -1;
  424. }
  425. EXPORT_SYMBOL(seq_path);
  426. /*
  427. * Same as seq_path, but relative to supplied root.
  428. *
  429. * root may be changed, see __d_path().
  430. */
  431. int seq_path_root(struct seq_file *m, struct path *path, struct path *root,
  432. char *esc)
  433. {
  434. int err = -ENAMETOOLONG;
  435. if (m->count < m->size) {
  436. char *s = m->buf + m->count;
  437. char *p;
  438. spin_lock(&dcache_lock);
  439. p = __d_path(path, root, s, m->size - m->count);
  440. spin_unlock(&dcache_lock);
  441. err = PTR_ERR(p);
  442. if (!IS_ERR(p)) {
  443. s = mangle_path(s, p, esc);
  444. if (s) {
  445. p = m->buf + m->count;
  446. m->count = s - m->buf;
  447. return 0;
  448. }
  449. }
  450. }
  451. m->count = m->size;
  452. return err;
  453. }
  454. /*
  455. * returns the path of the 'dentry' from the root of its filesystem.
  456. */
  457. int seq_dentry(struct seq_file *m, struct dentry *dentry, char *esc)
  458. {
  459. if (m->count < m->size) {
  460. char *s = m->buf + m->count;
  461. char *p = dentry_path(dentry, s, m->size - m->count);
  462. if (!IS_ERR(p)) {
  463. s = mangle_path(s, p, esc);
  464. if (s) {
  465. p = m->buf + m->count;
  466. m->count = s - m->buf;
  467. return s - p;
  468. }
  469. }
  470. }
  471. m->count = m->size;
  472. return -1;
  473. }
  474. int seq_bitmap(struct seq_file *m, const unsigned long *bits,
  475. unsigned int nr_bits)
  476. {
  477. if (m->count < m->size) {
  478. int len = bitmap_scnprintf(m->buf + m->count,
  479. m->size - m->count, bits, nr_bits);
  480. if (m->count + len < m->size) {
  481. m->count += len;
  482. return 0;
  483. }
  484. }
  485. m->count = m->size;
  486. return -1;
  487. }
  488. EXPORT_SYMBOL(seq_bitmap);
  489. int seq_bitmap_list(struct seq_file *m, const unsigned long *bits,
  490. unsigned int nr_bits)
  491. {
  492. if (m->count < m->size) {
  493. int len = bitmap_scnlistprintf(m->buf + m->count,
  494. m->size - m->count, bits, nr_bits);
  495. if (m->count + len < m->size) {
  496. m->count += len;
  497. return 0;
  498. }
  499. }
  500. m->count = m->size;
  501. return -1;
  502. }
  503. EXPORT_SYMBOL(seq_bitmap_list);
  504. static void *single_start(struct seq_file *p, loff_t *pos)
  505. {
  506. return NULL + (*pos == 0);
  507. }
  508. static void *single_next(struct seq_file *p, void *v, loff_t *pos)
  509. {
  510. ++*pos;
  511. return NULL;
  512. }
  513. static void single_stop(struct seq_file *p, void *v)
  514. {
  515. }
  516. int single_open(struct file *file, int (*show)(struct seq_file *, void *),
  517. void *data)
  518. {
  519. struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
  520. int res = -ENOMEM;
  521. if (op) {
  522. op->start = single_start;
  523. op->next = single_next;
  524. op->stop = single_stop;
  525. op->show = show;
  526. res = seq_open(file, op);
  527. if (!res)
  528. ((struct seq_file *)file->private_data)->private = data;
  529. else
  530. kfree(op);
  531. }
  532. return res;
  533. }
  534. EXPORT_SYMBOL(single_open);
  535. int single_release(struct inode *inode, struct file *file)
  536. {
  537. const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
  538. int res = seq_release(inode, file);
  539. kfree(op);
  540. return res;
  541. }
  542. EXPORT_SYMBOL(single_release);
  543. int seq_release_private(struct inode *inode, struct file *file)
  544. {
  545. struct seq_file *seq = file->private_data;
  546. kfree(seq->private);
  547. seq->private = NULL;
  548. return seq_release(inode, file);
  549. }
  550. EXPORT_SYMBOL(seq_release_private);
  551. void *__seq_open_private(struct file *f, const struct seq_operations *ops,
  552. int psize)
  553. {
  554. int rc;
  555. void *private;
  556. struct seq_file *seq;
  557. private = kzalloc(psize, GFP_KERNEL);
  558. if (private == NULL)
  559. goto out;
  560. rc = seq_open(f, ops);
  561. if (rc < 0)
  562. goto out_free;
  563. seq = f->private_data;
  564. seq->private = private;
  565. return private;
  566. out_free:
  567. kfree(private);
  568. out:
  569. return NULL;
  570. }
  571. EXPORT_SYMBOL(__seq_open_private);
  572. int seq_open_private(struct file *filp, const struct seq_operations *ops,
  573. int psize)
  574. {
  575. return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
  576. }
  577. EXPORT_SYMBOL(seq_open_private);
  578. int seq_putc(struct seq_file *m, char c)
  579. {
  580. if (m->count < m->size) {
  581. m->buf[m->count++] = c;
  582. return 0;
  583. }
  584. return -1;
  585. }
  586. EXPORT_SYMBOL(seq_putc);
  587. int seq_puts(struct seq_file *m, const char *s)
  588. {
  589. int len = strlen(s);
  590. if (m->count + len < m->size) {
  591. memcpy(m->buf + m->count, s, len);
  592. m->count += len;
  593. return 0;
  594. }
  595. m->count = m->size;
  596. return -1;
  597. }
  598. EXPORT_SYMBOL(seq_puts);
  599. struct list_head *seq_list_start(struct list_head *head, loff_t pos)
  600. {
  601. struct list_head *lh;
  602. list_for_each(lh, head)
  603. if (pos-- == 0)
  604. return lh;
  605. return NULL;
  606. }
  607. EXPORT_SYMBOL(seq_list_start);
  608. struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
  609. {
  610. if (!pos)
  611. return head;
  612. return seq_list_start(head, pos - 1);
  613. }
  614. EXPORT_SYMBOL(seq_list_start_head);
  615. struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
  616. {
  617. struct list_head *lh;
  618. lh = ((struct list_head *)v)->next;
  619. ++*ppos;
  620. return lh == head ? NULL : lh;
  621. }
  622. EXPORT_SYMBOL(seq_list_next);