seq_file.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. int seq_path(struct seq_file *m, struct path *path, char *esc)
  335. {
  336. if (m->count < m->size) {
  337. char *s = m->buf + m->count;
  338. char *p = d_path(path, s, m->size - m->count);
  339. if (!IS_ERR(p)) {
  340. while (s <= p) {
  341. char c = *p++;
  342. if (!c) {
  343. p = m->buf + m->count;
  344. m->count = s - m->buf;
  345. return s - p;
  346. } else if (!strchr(esc, c)) {
  347. *s++ = c;
  348. } else if (s + 4 > p) {
  349. break;
  350. } else {
  351. *s++ = '\\';
  352. *s++ = '0' + ((c & 0300) >> 6);
  353. *s++ = '0' + ((c & 070) >> 3);
  354. *s++ = '0' + (c & 07);
  355. }
  356. }
  357. }
  358. }
  359. m->count = m->size;
  360. return -1;
  361. }
  362. EXPORT_SYMBOL(seq_path);
  363. static void *single_start(struct seq_file *p, loff_t *pos)
  364. {
  365. return NULL + (*pos == 0);
  366. }
  367. static void *single_next(struct seq_file *p, void *v, loff_t *pos)
  368. {
  369. ++*pos;
  370. return NULL;
  371. }
  372. static void single_stop(struct seq_file *p, void *v)
  373. {
  374. }
  375. int single_open(struct file *file, int (*show)(struct seq_file *, void *),
  376. void *data)
  377. {
  378. struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
  379. int res = -ENOMEM;
  380. if (op) {
  381. op->start = single_start;
  382. op->next = single_next;
  383. op->stop = single_stop;
  384. op->show = show;
  385. res = seq_open(file, op);
  386. if (!res)
  387. ((struct seq_file *)file->private_data)->private = data;
  388. else
  389. kfree(op);
  390. }
  391. return res;
  392. }
  393. EXPORT_SYMBOL(single_open);
  394. int single_release(struct inode *inode, struct file *file)
  395. {
  396. const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
  397. int res = seq_release(inode, file);
  398. kfree(op);
  399. return res;
  400. }
  401. EXPORT_SYMBOL(single_release);
  402. int seq_release_private(struct inode *inode, struct file *file)
  403. {
  404. struct seq_file *seq = file->private_data;
  405. kfree(seq->private);
  406. seq->private = NULL;
  407. return seq_release(inode, file);
  408. }
  409. EXPORT_SYMBOL(seq_release_private);
  410. void *__seq_open_private(struct file *f, const struct seq_operations *ops,
  411. int psize)
  412. {
  413. int rc;
  414. void *private;
  415. struct seq_file *seq;
  416. private = kzalloc(psize, GFP_KERNEL);
  417. if (private == NULL)
  418. goto out;
  419. rc = seq_open(f, ops);
  420. if (rc < 0)
  421. goto out_free;
  422. seq = f->private_data;
  423. seq->private = private;
  424. return private;
  425. out_free:
  426. kfree(private);
  427. out:
  428. return NULL;
  429. }
  430. EXPORT_SYMBOL(__seq_open_private);
  431. int seq_open_private(struct file *filp, const struct seq_operations *ops,
  432. int psize)
  433. {
  434. return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
  435. }
  436. EXPORT_SYMBOL(seq_open_private);
  437. int seq_putc(struct seq_file *m, char c)
  438. {
  439. if (m->count < m->size) {
  440. m->buf[m->count++] = c;
  441. return 0;
  442. }
  443. return -1;
  444. }
  445. EXPORT_SYMBOL(seq_putc);
  446. int seq_puts(struct seq_file *m, const char *s)
  447. {
  448. int len = strlen(s);
  449. if (m->count + len < m->size) {
  450. memcpy(m->buf + m->count, s, len);
  451. m->count += len;
  452. return 0;
  453. }
  454. m->count = m->size;
  455. return -1;
  456. }
  457. EXPORT_SYMBOL(seq_puts);
  458. struct list_head *seq_list_start(struct list_head *head, loff_t pos)
  459. {
  460. struct list_head *lh;
  461. list_for_each(lh, head)
  462. if (pos-- == 0)
  463. return lh;
  464. return NULL;
  465. }
  466. EXPORT_SYMBOL(seq_list_start);
  467. struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
  468. {
  469. if (!pos)
  470. return head;
  471. return seq_list_start(head, pos - 1);
  472. }
  473. EXPORT_SYMBOL(seq_list_start_head);
  474. struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
  475. {
  476. struct list_head *lh;
  477. lh = ((struct list_head *)v)->next;
  478. ++*ppos;
  479. return lh == head ? NULL : lh;
  480. }
  481. EXPORT_SYMBOL(seq_list_next);