seq_file.c 13 KB

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