seq_file.c 13 KB

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