seq_file.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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. /* SEQ files support lseek, but not pread/pwrite */
  46. file->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
  47. return 0;
  48. }
  49. EXPORT_SYMBOL(seq_open);
  50. /**
  51. * seq_read - ->read() method for sequential files.
  52. * @file: the file to read from
  53. * @buf: the buffer to read to
  54. * @size: the maximum number of bytes to read
  55. * @ppos: the current position in the file
  56. *
  57. * Ready-made ->f_op->read()
  58. */
  59. ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
  60. {
  61. struct seq_file *m = (struct seq_file *)file->private_data;
  62. size_t copied = 0;
  63. loff_t pos;
  64. size_t n;
  65. void *p;
  66. int err = 0;
  67. mutex_lock(&m->lock);
  68. /*
  69. * seq_file->op->..m_start/m_stop/m_next may do special actions
  70. * or optimisations based on the file->f_version, so we want to
  71. * pass the file->f_version to those methods.
  72. *
  73. * seq_file->version is just copy of f_version, and seq_file
  74. * methods can treat it simply as file version.
  75. * It is copied in first and copied out after all operations.
  76. * It is convenient to have it as part of structure to avoid the
  77. * need of passing another argument to all the seq_file methods.
  78. */
  79. m->version = file->f_version;
  80. /* grab buffer if we didn't have one */
  81. if (!m->buf) {
  82. m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
  83. if (!m->buf)
  84. goto Enomem;
  85. }
  86. /* if not empty - flush it first */
  87. if (m->count) {
  88. n = min(m->count, size);
  89. err = copy_to_user(buf, m->buf + m->from, n);
  90. if (err)
  91. goto Efault;
  92. m->count -= n;
  93. m->from += n;
  94. size -= n;
  95. buf += n;
  96. copied += n;
  97. if (!m->count)
  98. m->index++;
  99. if (!size)
  100. goto Done;
  101. }
  102. /* we need at least one record in buffer */
  103. pos = m->index;
  104. p = m->op->start(m, &pos);
  105. while (1) {
  106. err = PTR_ERR(p);
  107. if (!p || IS_ERR(p))
  108. break;
  109. err = m->op->show(m, p);
  110. if (err < 0)
  111. break;
  112. if (unlikely(err))
  113. m->count = 0;
  114. if (unlikely(!m->count)) {
  115. p = m->op->next(m, p, &pos);
  116. m->index = pos;
  117. continue;
  118. }
  119. if (m->count < m->size)
  120. goto Fill;
  121. m->op->stop(m, p);
  122. kfree(m->buf);
  123. m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
  124. if (!m->buf)
  125. goto Enomem;
  126. m->count = 0;
  127. m->version = 0;
  128. pos = m->index;
  129. p = m->op->start(m, &pos);
  130. }
  131. m->op->stop(m, p);
  132. m->count = 0;
  133. goto Done;
  134. Fill:
  135. /* they want more? let's try to get some more */
  136. while (m->count < size) {
  137. size_t offs = m->count;
  138. loff_t next = pos;
  139. p = m->op->next(m, p, &next);
  140. if (!p || IS_ERR(p)) {
  141. err = PTR_ERR(p);
  142. break;
  143. }
  144. err = m->op->show(m, p);
  145. if (m->count == m->size || err) {
  146. m->count = offs;
  147. if (likely(err <= 0))
  148. break;
  149. }
  150. pos = next;
  151. }
  152. m->op->stop(m, p);
  153. n = min(m->count, size);
  154. err = copy_to_user(buf, m->buf, n);
  155. if (err)
  156. goto Efault;
  157. copied += n;
  158. m->count -= n;
  159. if (m->count)
  160. m->from = n;
  161. else
  162. pos++;
  163. m->index = pos;
  164. Done:
  165. if (!copied)
  166. copied = err;
  167. else
  168. *ppos += copied;
  169. file->f_version = m->version;
  170. mutex_unlock(&m->lock);
  171. return copied;
  172. Enomem:
  173. err = -ENOMEM;
  174. goto Done;
  175. Efault:
  176. err = -EFAULT;
  177. goto Done;
  178. }
  179. EXPORT_SYMBOL(seq_read);
  180. static int traverse(struct seq_file *m, loff_t offset)
  181. {
  182. loff_t pos = 0, index;
  183. int error = 0;
  184. void *p;
  185. m->version = 0;
  186. index = 0;
  187. m->count = m->from = 0;
  188. if (!offset) {
  189. m->index = index;
  190. return 0;
  191. }
  192. if (!m->buf) {
  193. m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
  194. if (!m->buf)
  195. return -ENOMEM;
  196. }
  197. p = m->op->start(m, &index);
  198. while (p) {
  199. error = PTR_ERR(p);
  200. if (IS_ERR(p))
  201. break;
  202. error = m->op->show(m, p);
  203. if (error < 0)
  204. break;
  205. if (unlikely(error)) {
  206. error = 0;
  207. m->count = 0;
  208. }
  209. if (m->count == m->size)
  210. goto Eoverflow;
  211. if (pos + m->count > offset) {
  212. m->from = offset - pos;
  213. m->count -= m->from;
  214. m->index = index;
  215. break;
  216. }
  217. pos += m->count;
  218. m->count = 0;
  219. if (pos == offset) {
  220. index++;
  221. m->index = index;
  222. break;
  223. }
  224. p = m->op->next(m, p, &index);
  225. }
  226. m->op->stop(m, p);
  227. return error;
  228. Eoverflow:
  229. m->op->stop(m, p);
  230. kfree(m->buf);
  231. m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
  232. return !m->buf ? -ENOMEM : -EAGAIN;
  233. }
  234. /**
  235. * seq_lseek - ->llseek() method for sequential files.
  236. * @file: the file in question
  237. * @offset: new position
  238. * @origin: 0 for absolute, 1 for relative position
  239. *
  240. * Ready-made ->f_op->llseek()
  241. */
  242. loff_t seq_lseek(struct file *file, loff_t offset, int origin)
  243. {
  244. struct seq_file *m = (struct seq_file *)file->private_data;
  245. loff_t retval = -EINVAL;
  246. mutex_lock(&m->lock);
  247. m->version = file->f_version;
  248. switch (origin) {
  249. case 1:
  250. offset += file->f_pos;
  251. case 0:
  252. if (offset < 0)
  253. break;
  254. retval = offset;
  255. if (offset != file->f_pos) {
  256. while ((retval=traverse(m, offset)) == -EAGAIN)
  257. ;
  258. if (retval) {
  259. /* with extreme prejudice... */
  260. file->f_pos = 0;
  261. m->version = 0;
  262. m->index = 0;
  263. m->count = 0;
  264. } else {
  265. retval = file->f_pos = offset;
  266. }
  267. }
  268. }
  269. file->f_version = m->version;
  270. mutex_unlock(&m->lock);
  271. return retval;
  272. }
  273. EXPORT_SYMBOL(seq_lseek);
  274. /**
  275. * seq_release - free the structures associated with sequential file.
  276. * @file: file in question
  277. * @inode: file->f_path.dentry->d_inode
  278. *
  279. * Frees the structures associated with sequential file; can be used
  280. * as ->f_op->release() if you don't have private data to destroy.
  281. */
  282. int seq_release(struct inode *inode, struct file *file)
  283. {
  284. struct seq_file *m = (struct seq_file *)file->private_data;
  285. kfree(m->buf);
  286. kfree(m);
  287. return 0;
  288. }
  289. EXPORT_SYMBOL(seq_release);
  290. /**
  291. * seq_escape - print string into buffer, escaping some characters
  292. * @m: target buffer
  293. * @s: string
  294. * @esc: set of characters that need escaping
  295. *
  296. * Puts string into buffer, replacing each occurrence of character from
  297. * @esc with usual octal escape. Returns 0 in case of success, -1 - in
  298. * case of overflow.
  299. */
  300. int seq_escape(struct seq_file *m, const char *s, const char *esc)
  301. {
  302. char *end = m->buf + m->size;
  303. char *p;
  304. char c;
  305. for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
  306. if (!strchr(esc, c)) {
  307. *p++ = c;
  308. continue;
  309. }
  310. if (p + 3 < end) {
  311. *p++ = '\\';
  312. *p++ = '0' + ((c & 0300) >> 6);
  313. *p++ = '0' + ((c & 070) >> 3);
  314. *p++ = '0' + (c & 07);
  315. continue;
  316. }
  317. m->count = m->size;
  318. return -1;
  319. }
  320. m->count = p - m->buf;
  321. return 0;
  322. }
  323. EXPORT_SYMBOL(seq_escape);
  324. int seq_printf(struct seq_file *m, const char *f, ...)
  325. {
  326. va_list args;
  327. int len;
  328. if (m->count < m->size) {
  329. va_start(args, f);
  330. len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
  331. va_end(args);
  332. if (m->count + len < m->size) {
  333. m->count += len;
  334. return 0;
  335. }
  336. }
  337. m->count = m->size;
  338. return -1;
  339. }
  340. EXPORT_SYMBOL(seq_printf);
  341. /**
  342. * mangle_path - mangle and copy path to buffer beginning
  343. * @s: buffer start
  344. * @p: beginning of path in above buffer
  345. * @esc: set of characters that need escaping
  346. *
  347. * Copy the path from @p to @s, replacing each occurrence of character from
  348. * @esc with usual octal escape.
  349. * Returns pointer past last written character in @s, or NULL in case of
  350. * failure.
  351. */
  352. char *mangle_path(char *s, char *p, char *esc)
  353. {
  354. while (s <= p) {
  355. char c = *p++;
  356. if (!c) {
  357. return s;
  358. } else if (!strchr(esc, c)) {
  359. *s++ = c;
  360. } else if (s + 4 > p) {
  361. break;
  362. } else {
  363. *s++ = '\\';
  364. *s++ = '0' + ((c & 0300) >> 6);
  365. *s++ = '0' + ((c & 070) >> 3);
  366. *s++ = '0' + (c & 07);
  367. }
  368. }
  369. return NULL;
  370. }
  371. EXPORT_SYMBOL(mangle_path);
  372. /**
  373. * seq_path - seq_file interface to print a pathname
  374. * @m: the seq_file handle
  375. * @path: the struct path to print
  376. * @esc: set of characters to escape in the output
  377. *
  378. * return the absolute path of 'path', as represented by the
  379. * dentry / mnt pair in the path parameter.
  380. */
  381. int seq_path(struct seq_file *m, struct path *path, char *esc)
  382. {
  383. if (m->count < m->size) {
  384. char *s = m->buf + m->count;
  385. char *p = d_path(path, s, m->size - m->count);
  386. if (!IS_ERR(p)) {
  387. s = mangle_path(s, p, esc);
  388. if (s) {
  389. p = m->buf + m->count;
  390. m->count = s - m->buf;
  391. return s - p;
  392. }
  393. }
  394. }
  395. m->count = m->size;
  396. return -1;
  397. }
  398. EXPORT_SYMBOL(seq_path);
  399. /*
  400. * Same as seq_path, but relative to supplied root.
  401. *
  402. * root may be changed, see __d_path().
  403. */
  404. int seq_path_root(struct seq_file *m, struct path *path, struct path *root,
  405. char *esc)
  406. {
  407. int err = -ENAMETOOLONG;
  408. if (m->count < m->size) {
  409. char *s = m->buf + m->count;
  410. char *p;
  411. spin_lock(&dcache_lock);
  412. p = __d_path(path, root, s, m->size - m->count);
  413. spin_unlock(&dcache_lock);
  414. err = PTR_ERR(p);
  415. if (!IS_ERR(p)) {
  416. s = mangle_path(s, p, esc);
  417. if (s) {
  418. p = m->buf + m->count;
  419. m->count = s - m->buf;
  420. return 0;
  421. }
  422. }
  423. }
  424. m->count = m->size;
  425. return err;
  426. }
  427. /*
  428. * returns the path of the 'dentry' from the root of its filesystem.
  429. */
  430. int seq_dentry(struct seq_file *m, struct dentry *dentry, char *esc)
  431. {
  432. if (m->count < m->size) {
  433. char *s = m->buf + m->count;
  434. char *p = dentry_path(dentry, s, m->size - m->count);
  435. if (!IS_ERR(p)) {
  436. s = mangle_path(s, p, esc);
  437. if (s) {
  438. p = m->buf + m->count;
  439. m->count = s - m->buf;
  440. return s - p;
  441. }
  442. }
  443. }
  444. m->count = m->size;
  445. return -1;
  446. }
  447. int seq_bitmap(struct seq_file *m, const unsigned long *bits,
  448. unsigned int nr_bits)
  449. {
  450. if (m->count < m->size) {
  451. int len = bitmap_scnprintf(m->buf + m->count,
  452. m->size - m->count, bits, nr_bits);
  453. if (m->count + len < m->size) {
  454. m->count += len;
  455. return 0;
  456. }
  457. }
  458. m->count = m->size;
  459. return -1;
  460. }
  461. EXPORT_SYMBOL(seq_bitmap);
  462. int seq_bitmap_list(struct seq_file *m, unsigned long *bits,
  463. unsigned int nr_bits)
  464. {
  465. if (m->count < m->size) {
  466. int len = bitmap_scnlistprintf(m->buf + m->count,
  467. m->size - m->count, bits, nr_bits);
  468. if (m->count + len < m->size) {
  469. m->count += len;
  470. return 0;
  471. }
  472. }
  473. m->count = m->size;
  474. return -1;
  475. }
  476. EXPORT_SYMBOL(seq_bitmap_list);
  477. static void *single_start(struct seq_file *p, loff_t *pos)
  478. {
  479. return NULL + (*pos == 0);
  480. }
  481. static void *single_next(struct seq_file *p, void *v, loff_t *pos)
  482. {
  483. ++*pos;
  484. return NULL;
  485. }
  486. static void single_stop(struct seq_file *p, void *v)
  487. {
  488. }
  489. int single_open(struct file *file, int (*show)(struct seq_file *, void *),
  490. void *data)
  491. {
  492. struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
  493. int res = -ENOMEM;
  494. if (op) {
  495. op->start = single_start;
  496. op->next = single_next;
  497. op->stop = single_stop;
  498. op->show = show;
  499. res = seq_open(file, op);
  500. if (!res)
  501. ((struct seq_file *)file->private_data)->private = data;
  502. else
  503. kfree(op);
  504. }
  505. return res;
  506. }
  507. EXPORT_SYMBOL(single_open);
  508. int single_release(struct inode *inode, struct file *file)
  509. {
  510. const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
  511. int res = seq_release(inode, file);
  512. kfree(op);
  513. return res;
  514. }
  515. EXPORT_SYMBOL(single_release);
  516. int seq_release_private(struct inode *inode, struct file *file)
  517. {
  518. struct seq_file *seq = file->private_data;
  519. kfree(seq->private);
  520. seq->private = NULL;
  521. return seq_release(inode, file);
  522. }
  523. EXPORT_SYMBOL(seq_release_private);
  524. void *__seq_open_private(struct file *f, const struct seq_operations *ops,
  525. int psize)
  526. {
  527. int rc;
  528. void *private;
  529. struct seq_file *seq;
  530. private = kzalloc(psize, GFP_KERNEL);
  531. if (private == NULL)
  532. goto out;
  533. rc = seq_open(f, ops);
  534. if (rc < 0)
  535. goto out_free;
  536. seq = f->private_data;
  537. seq->private = private;
  538. return private;
  539. out_free:
  540. kfree(private);
  541. out:
  542. return NULL;
  543. }
  544. EXPORT_SYMBOL(__seq_open_private);
  545. int seq_open_private(struct file *filp, const struct seq_operations *ops,
  546. int psize)
  547. {
  548. return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
  549. }
  550. EXPORT_SYMBOL(seq_open_private);
  551. int seq_putc(struct seq_file *m, char c)
  552. {
  553. if (m->count < m->size) {
  554. m->buf[m->count++] = c;
  555. return 0;
  556. }
  557. return -1;
  558. }
  559. EXPORT_SYMBOL(seq_putc);
  560. int seq_puts(struct seq_file *m, const char *s)
  561. {
  562. int len = strlen(s);
  563. if (m->count + len < m->size) {
  564. memcpy(m->buf + m->count, s, len);
  565. m->count += len;
  566. return 0;
  567. }
  568. m->count = m->size;
  569. return -1;
  570. }
  571. EXPORT_SYMBOL(seq_puts);
  572. struct list_head *seq_list_start(struct list_head *head, loff_t pos)
  573. {
  574. struct list_head *lh;
  575. list_for_each(lh, head)
  576. if (pos-- == 0)
  577. return lh;
  578. return NULL;
  579. }
  580. EXPORT_SYMBOL(seq_list_start);
  581. struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
  582. {
  583. if (!pos)
  584. return head;
  585. return seq_list_start(head, pos - 1);
  586. }
  587. EXPORT_SYMBOL(seq_list_start_head);
  588. struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
  589. {
  590. struct list_head *lh;
  591. lh = ((struct list_head *)v)->next;
  592. ++*ppos;
  593. return lh == head ? NULL : lh;
  594. }
  595. EXPORT_SYMBOL(seq_list_next);