seq_file.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. while (1) {
  104. pos = m->index;
  105. p = m->op->start(m, &pos);
  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 (m->count < m->size)
  115. goto Fill;
  116. m->op->stop(m, p);
  117. kfree(m->buf);
  118. m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
  119. if (!m->buf)
  120. goto Enomem;
  121. m->count = 0;
  122. m->version = 0;
  123. }
  124. m->op->stop(m, p);
  125. m->count = 0;
  126. goto Done;
  127. Fill:
  128. /* they want more? let's try to get some more */
  129. while (m->count < size) {
  130. size_t offs = m->count;
  131. loff_t next = pos;
  132. p = m->op->next(m, p, &next);
  133. if (!p || IS_ERR(p)) {
  134. err = PTR_ERR(p);
  135. break;
  136. }
  137. err = m->op->show(m, p);
  138. if (m->count == m->size || err) {
  139. m->count = offs;
  140. if (likely(err <= 0))
  141. break;
  142. }
  143. pos = next;
  144. }
  145. m->op->stop(m, p);
  146. n = min(m->count, size);
  147. err = copy_to_user(buf, m->buf, n);
  148. if (err)
  149. goto Efault;
  150. copied += n;
  151. m->count -= n;
  152. if (m->count)
  153. m->from = n;
  154. else
  155. pos++;
  156. m->index = pos;
  157. Done:
  158. if (!copied)
  159. copied = err;
  160. else
  161. *ppos += copied;
  162. file->f_version = m->version;
  163. mutex_unlock(&m->lock);
  164. return copied;
  165. Enomem:
  166. err = -ENOMEM;
  167. goto Done;
  168. Efault:
  169. err = -EFAULT;
  170. goto Done;
  171. }
  172. EXPORT_SYMBOL(seq_read);
  173. static int traverse(struct seq_file *m, loff_t offset)
  174. {
  175. loff_t pos = 0, index;
  176. int error = 0;
  177. void *p;
  178. m->version = 0;
  179. index = 0;
  180. m->count = m->from = 0;
  181. if (!offset) {
  182. m->index = index;
  183. return 0;
  184. }
  185. if (!m->buf) {
  186. m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
  187. if (!m->buf)
  188. return -ENOMEM;
  189. }
  190. p = m->op->start(m, &index);
  191. while (p) {
  192. error = PTR_ERR(p);
  193. if (IS_ERR(p))
  194. break;
  195. error = m->op->show(m, p);
  196. if (error < 0)
  197. break;
  198. if (unlikely(error)) {
  199. error = 0;
  200. m->count = 0;
  201. }
  202. if (m->count == m->size)
  203. goto Eoverflow;
  204. if (pos + m->count > offset) {
  205. m->from = offset - pos;
  206. m->count -= m->from;
  207. m->index = index;
  208. break;
  209. }
  210. pos += m->count;
  211. m->count = 0;
  212. if (pos == offset) {
  213. index++;
  214. m->index = index;
  215. break;
  216. }
  217. p = m->op->next(m, p, &index);
  218. }
  219. m->op->stop(m, p);
  220. return error;
  221. Eoverflow:
  222. m->op->stop(m, p);
  223. kfree(m->buf);
  224. m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
  225. return !m->buf ? -ENOMEM : -EAGAIN;
  226. }
  227. /**
  228. * seq_lseek - ->llseek() method for sequential files.
  229. * @file: the file in question
  230. * @offset: new position
  231. * @origin: 0 for absolute, 1 for relative position
  232. *
  233. * Ready-made ->f_op->llseek()
  234. */
  235. loff_t seq_lseek(struct file *file, loff_t offset, int origin)
  236. {
  237. struct seq_file *m = (struct seq_file *)file->private_data;
  238. loff_t retval = -EINVAL;
  239. mutex_lock(&m->lock);
  240. m->version = file->f_version;
  241. switch (origin) {
  242. case 1:
  243. offset += file->f_pos;
  244. case 0:
  245. if (offset < 0)
  246. break;
  247. retval = offset;
  248. if (offset != file->f_pos) {
  249. while ((retval=traverse(m, offset)) == -EAGAIN)
  250. ;
  251. if (retval) {
  252. /* with extreme prejudice... */
  253. file->f_pos = 0;
  254. m->version = 0;
  255. m->index = 0;
  256. m->count = 0;
  257. } else {
  258. retval = file->f_pos = offset;
  259. }
  260. }
  261. }
  262. file->f_version = m->version;
  263. mutex_unlock(&m->lock);
  264. return retval;
  265. }
  266. EXPORT_SYMBOL(seq_lseek);
  267. /**
  268. * seq_release - free the structures associated with sequential file.
  269. * @file: file in question
  270. * @inode: file->f_path.dentry->d_inode
  271. *
  272. * Frees the structures associated with sequential file; can be used
  273. * as ->f_op->release() if you don't have private data to destroy.
  274. */
  275. int seq_release(struct inode *inode, struct file *file)
  276. {
  277. struct seq_file *m = (struct seq_file *)file->private_data;
  278. kfree(m->buf);
  279. kfree(m);
  280. return 0;
  281. }
  282. EXPORT_SYMBOL(seq_release);
  283. /**
  284. * seq_escape - print string into buffer, escaping some characters
  285. * @m: target buffer
  286. * @s: string
  287. * @esc: set of characters that need escaping
  288. *
  289. * Puts string into buffer, replacing each occurrence of character from
  290. * @esc with usual octal escape. Returns 0 in case of success, -1 - in
  291. * case of overflow.
  292. */
  293. int seq_escape(struct seq_file *m, const char *s, const char *esc)
  294. {
  295. char *end = m->buf + m->size;
  296. char *p;
  297. char c;
  298. for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
  299. if (!strchr(esc, c)) {
  300. *p++ = c;
  301. continue;
  302. }
  303. if (p + 3 < end) {
  304. *p++ = '\\';
  305. *p++ = '0' + ((c & 0300) >> 6);
  306. *p++ = '0' + ((c & 070) >> 3);
  307. *p++ = '0' + (c & 07);
  308. continue;
  309. }
  310. m->count = m->size;
  311. return -1;
  312. }
  313. m->count = p - m->buf;
  314. return 0;
  315. }
  316. EXPORT_SYMBOL(seq_escape);
  317. int seq_printf(struct seq_file *m, const char *f, ...)
  318. {
  319. va_list args;
  320. int len;
  321. if (m->count < m->size) {
  322. va_start(args, f);
  323. len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
  324. va_end(args);
  325. if (m->count + len < m->size) {
  326. m->count += len;
  327. return 0;
  328. }
  329. }
  330. m->count = m->size;
  331. return -1;
  332. }
  333. EXPORT_SYMBOL(seq_printf);
  334. static char *mangle_path(char *s, char *p, char *esc)
  335. {
  336. while (s <= p) {
  337. char c = *p++;
  338. if (!c) {
  339. return s;
  340. } else if (!strchr(esc, c)) {
  341. *s++ = c;
  342. } else if (s + 4 > p) {
  343. break;
  344. } else {
  345. *s++ = '\\';
  346. *s++ = '0' + ((c & 0300) >> 6);
  347. *s++ = '0' + ((c & 070) >> 3);
  348. *s++ = '0' + (c & 07);
  349. }
  350. }
  351. return NULL;
  352. }
  353. /*
  354. * return the absolute path of 'dentry' residing in mount 'mnt'.
  355. */
  356. int seq_path(struct seq_file *m, struct path *path, char *esc)
  357. {
  358. if (m->count < m->size) {
  359. char *s = m->buf + m->count;
  360. char *p = d_path(path, s, m->size - m->count);
  361. if (!IS_ERR(p)) {
  362. s = mangle_path(s, p, esc);
  363. if (s) {
  364. p = m->buf + m->count;
  365. m->count = s - m->buf;
  366. return s - p;
  367. }
  368. }
  369. }
  370. m->count = m->size;
  371. return -1;
  372. }
  373. EXPORT_SYMBOL(seq_path);
  374. /*
  375. * Same as seq_path, but relative to supplied root.
  376. *
  377. * root may be changed, see __d_path().
  378. */
  379. int seq_path_root(struct seq_file *m, struct path *path, struct path *root,
  380. char *esc)
  381. {
  382. int err = -ENAMETOOLONG;
  383. if (m->count < m->size) {
  384. char *s = m->buf + m->count;
  385. char *p;
  386. spin_lock(&dcache_lock);
  387. p = __d_path(path, root, s, m->size - m->count);
  388. spin_unlock(&dcache_lock);
  389. err = PTR_ERR(p);
  390. if (!IS_ERR(p)) {
  391. s = mangle_path(s, p, esc);
  392. if (s) {
  393. p = m->buf + m->count;
  394. m->count = s - m->buf;
  395. return 0;
  396. }
  397. }
  398. }
  399. m->count = m->size;
  400. return err;
  401. }
  402. /*
  403. * returns the path of the 'dentry' from the root of its filesystem.
  404. */
  405. int seq_dentry(struct seq_file *m, struct dentry *dentry, char *esc)
  406. {
  407. if (m->count < m->size) {
  408. char *s = m->buf + m->count;
  409. char *p = dentry_path(dentry, s, m->size - m->count);
  410. if (!IS_ERR(p)) {
  411. s = mangle_path(s, p, esc);
  412. if (s) {
  413. p = m->buf + m->count;
  414. m->count = s - m->buf;
  415. return s - p;
  416. }
  417. }
  418. }
  419. m->count = m->size;
  420. return -1;
  421. }
  422. static void *single_start(struct seq_file *p, loff_t *pos)
  423. {
  424. return NULL + (*pos == 0);
  425. }
  426. static void *single_next(struct seq_file *p, void *v, loff_t *pos)
  427. {
  428. ++*pos;
  429. return NULL;
  430. }
  431. static void single_stop(struct seq_file *p, void *v)
  432. {
  433. }
  434. int single_open(struct file *file, int (*show)(struct seq_file *, void *),
  435. void *data)
  436. {
  437. struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
  438. int res = -ENOMEM;
  439. if (op) {
  440. op->start = single_start;
  441. op->next = single_next;
  442. op->stop = single_stop;
  443. op->show = show;
  444. res = seq_open(file, op);
  445. if (!res)
  446. ((struct seq_file *)file->private_data)->private = data;
  447. else
  448. kfree(op);
  449. }
  450. return res;
  451. }
  452. EXPORT_SYMBOL(single_open);
  453. int single_release(struct inode *inode, struct file *file)
  454. {
  455. const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
  456. int res = seq_release(inode, file);
  457. kfree(op);
  458. return res;
  459. }
  460. EXPORT_SYMBOL(single_release);
  461. int seq_release_private(struct inode *inode, struct file *file)
  462. {
  463. struct seq_file *seq = file->private_data;
  464. kfree(seq->private);
  465. seq->private = NULL;
  466. return seq_release(inode, file);
  467. }
  468. EXPORT_SYMBOL(seq_release_private);
  469. void *__seq_open_private(struct file *f, const struct seq_operations *ops,
  470. int psize)
  471. {
  472. int rc;
  473. void *private;
  474. struct seq_file *seq;
  475. private = kzalloc(psize, GFP_KERNEL);
  476. if (private == NULL)
  477. goto out;
  478. rc = seq_open(f, ops);
  479. if (rc < 0)
  480. goto out_free;
  481. seq = f->private_data;
  482. seq->private = private;
  483. return private;
  484. out_free:
  485. kfree(private);
  486. out:
  487. return NULL;
  488. }
  489. EXPORT_SYMBOL(__seq_open_private);
  490. int seq_open_private(struct file *filp, const struct seq_operations *ops,
  491. int psize)
  492. {
  493. return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
  494. }
  495. EXPORT_SYMBOL(seq_open_private);
  496. int seq_putc(struct seq_file *m, char c)
  497. {
  498. if (m->count < m->size) {
  499. m->buf[m->count++] = c;
  500. return 0;
  501. }
  502. return -1;
  503. }
  504. EXPORT_SYMBOL(seq_putc);
  505. int seq_puts(struct seq_file *m, const char *s)
  506. {
  507. int len = strlen(s);
  508. if (m->count + len < m->size) {
  509. memcpy(m->buf + m->count, s, len);
  510. m->count += len;
  511. return 0;
  512. }
  513. m->count = m->size;
  514. return -1;
  515. }
  516. EXPORT_SYMBOL(seq_puts);
  517. struct list_head *seq_list_start(struct list_head *head, loff_t pos)
  518. {
  519. struct list_head *lh;
  520. list_for_each(lh, head)
  521. if (pos-- == 0)
  522. return lh;
  523. return NULL;
  524. }
  525. EXPORT_SYMBOL(seq_list_start);
  526. struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
  527. {
  528. if (!pos)
  529. return head;
  530. return seq_list_start(head, pos - 1);
  531. }
  532. EXPORT_SYMBOL(seq_list_start_head);
  533. struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
  534. {
  535. struct list_head *lh;
  536. lh = ((struct list_head *)v)->next;
  537. ++*ppos;
  538. return lh == head ? NULL : lh;
  539. }
  540. EXPORT_SYMBOL(seq_list_next);