seq_file.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. */
  26. int seq_open(struct file *file, struct seq_operations *op)
  27. {
  28. struct seq_file *p = kmalloc(sizeof(*p), GFP_KERNEL);
  29. if (!p)
  30. return -ENOMEM;
  31. memset(p, 0, sizeof(*p));
  32. sema_init(&p->sem, 1);
  33. p->op = op;
  34. file->private_data = p;
  35. /*
  36. * Wrappers around seq_open(e.g. swaps_open) need to be
  37. * aware of this. If they set f_version themselves, they
  38. * should call seq_open first and then set f_version.
  39. */
  40. file->f_version = 0;
  41. /* SEQ files support lseek, but not pread/pwrite */
  42. file->f_mode &= ~(FMODE_PREAD | FMODE_PWRITE);
  43. return 0;
  44. }
  45. EXPORT_SYMBOL(seq_open);
  46. /**
  47. * seq_read - ->read() method for sequential files.
  48. * @file, @buf, @size, @ppos: see file_operations method
  49. *
  50. * Ready-made ->f_op->read()
  51. */
  52. ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
  53. {
  54. struct seq_file *m = (struct seq_file *)file->private_data;
  55. size_t copied = 0;
  56. loff_t pos;
  57. size_t n;
  58. void *p;
  59. int err = 0;
  60. down(&m->sem);
  61. /*
  62. * seq_file->op->..m_start/m_stop/m_next may do special actions
  63. * or optimisations based on the file->f_version, so we want to
  64. * pass the file->f_version to those methods.
  65. *
  66. * seq_file->version is just copy of f_version, and seq_file
  67. * methods can treat it simply as file version.
  68. * It is copied in first and copied out after all operations.
  69. * It is convenient to have it as part of structure to avoid the
  70. * need of passing another argument to all the seq_file methods.
  71. */
  72. m->version = file->f_version;
  73. /* grab buffer if we didn't have one */
  74. if (!m->buf) {
  75. m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
  76. if (!m->buf)
  77. goto Enomem;
  78. }
  79. /* if not empty - flush it first */
  80. if (m->count) {
  81. n = min(m->count, size);
  82. err = copy_to_user(buf, m->buf + m->from, n);
  83. if (err)
  84. goto Efault;
  85. m->count -= n;
  86. m->from += n;
  87. size -= n;
  88. buf += n;
  89. copied += n;
  90. if (!m->count)
  91. m->index++;
  92. if (!size)
  93. goto Done;
  94. }
  95. /* we need at least one record in buffer */
  96. while (1) {
  97. pos = m->index;
  98. p = m->op->start(m, &pos);
  99. err = PTR_ERR(p);
  100. if (!p || IS_ERR(p))
  101. break;
  102. err = m->op->show(m, p);
  103. if (err)
  104. break;
  105. if (m->count < m->size)
  106. goto Fill;
  107. m->op->stop(m, p);
  108. kfree(m->buf);
  109. m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
  110. if (!m->buf)
  111. goto Enomem;
  112. m->count = 0;
  113. m->version = 0;
  114. }
  115. m->op->stop(m, p);
  116. m->count = 0;
  117. goto Done;
  118. Fill:
  119. /* they want more? let's try to get some more */
  120. while (m->count < size) {
  121. size_t offs = m->count;
  122. loff_t next = pos;
  123. p = m->op->next(m, p, &next);
  124. if (!p || IS_ERR(p)) {
  125. err = PTR_ERR(p);
  126. break;
  127. }
  128. err = m->op->show(m, p);
  129. if (err || m->count == m->size) {
  130. m->count = offs;
  131. break;
  132. }
  133. pos = next;
  134. }
  135. m->op->stop(m, p);
  136. n = min(m->count, size);
  137. err = copy_to_user(buf, m->buf, n);
  138. if (err)
  139. goto Efault;
  140. copied += n;
  141. m->count -= n;
  142. if (m->count)
  143. m->from = n;
  144. else
  145. pos++;
  146. m->index = pos;
  147. Done:
  148. if (!copied)
  149. copied = err;
  150. else
  151. *ppos += copied;
  152. file->f_version = m->version;
  153. up(&m->sem);
  154. return copied;
  155. Enomem:
  156. err = -ENOMEM;
  157. goto Done;
  158. Efault:
  159. err = -EFAULT;
  160. goto Done;
  161. }
  162. EXPORT_SYMBOL(seq_read);
  163. static int traverse(struct seq_file *m, loff_t offset)
  164. {
  165. loff_t pos = 0;
  166. int error = 0;
  167. void *p;
  168. m->version = 0;
  169. m->index = 0;
  170. m->count = m->from = 0;
  171. if (!offset)
  172. return 0;
  173. if (!m->buf) {
  174. m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
  175. if (!m->buf)
  176. return -ENOMEM;
  177. }
  178. p = m->op->start(m, &m->index);
  179. while (p) {
  180. error = PTR_ERR(p);
  181. if (IS_ERR(p))
  182. break;
  183. error = m->op->show(m, p);
  184. if (error)
  185. break;
  186. if (m->count == m->size)
  187. goto Eoverflow;
  188. if (pos + m->count > offset) {
  189. m->from = offset - pos;
  190. m->count -= m->from;
  191. break;
  192. }
  193. pos += m->count;
  194. m->count = 0;
  195. if (pos == offset) {
  196. m->index++;
  197. break;
  198. }
  199. p = m->op->next(m, p, &m->index);
  200. }
  201. m->op->stop(m, p);
  202. return error;
  203. Eoverflow:
  204. m->op->stop(m, p);
  205. kfree(m->buf);
  206. m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
  207. return !m->buf ? -ENOMEM : -EAGAIN;
  208. }
  209. /**
  210. * seq_lseek - ->llseek() method for sequential files.
  211. * @file, @offset, @origin: see file_operations method
  212. *
  213. * Ready-made ->f_op->llseek()
  214. */
  215. loff_t seq_lseek(struct file *file, loff_t offset, int origin)
  216. {
  217. struct seq_file *m = (struct seq_file *)file->private_data;
  218. long long retval = -EINVAL;
  219. down(&m->sem);
  220. m->version = file->f_version;
  221. switch (origin) {
  222. case 1:
  223. offset += file->f_pos;
  224. case 0:
  225. if (offset < 0)
  226. break;
  227. retval = offset;
  228. if (offset != file->f_pos) {
  229. while ((retval=traverse(m, offset)) == -EAGAIN)
  230. ;
  231. if (retval) {
  232. /* with extreme prejudice... */
  233. file->f_pos = 0;
  234. m->version = 0;
  235. m->index = 0;
  236. m->count = 0;
  237. } else {
  238. retval = file->f_pos = offset;
  239. }
  240. }
  241. }
  242. up(&m->sem);
  243. file->f_version = m->version;
  244. return retval;
  245. }
  246. EXPORT_SYMBOL(seq_lseek);
  247. /**
  248. * seq_release - free the structures associated with sequential file.
  249. * @file: file in question
  250. * @inode: file->f_dentry->d_inode
  251. *
  252. * Frees the structures associated with sequential file; can be used
  253. * as ->f_op->release() if you don't have private data to destroy.
  254. */
  255. int seq_release(struct inode *inode, struct file *file)
  256. {
  257. struct seq_file *m = (struct seq_file *)file->private_data;
  258. kfree(m->buf);
  259. kfree(m);
  260. return 0;
  261. }
  262. EXPORT_SYMBOL(seq_release);
  263. /**
  264. * seq_escape - print string into buffer, escaping some characters
  265. * @m: target buffer
  266. * @s: string
  267. * @esc: set of characters that need escaping
  268. *
  269. * Puts string into buffer, replacing each occurrence of character from
  270. * @esc with usual octal escape. Returns 0 in case of success, -1 - in
  271. * case of overflow.
  272. */
  273. int seq_escape(struct seq_file *m, const char *s, const char *esc)
  274. {
  275. char *end = m->buf + m->size;
  276. char *p;
  277. char c;
  278. for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
  279. if (!strchr(esc, c)) {
  280. *p++ = c;
  281. continue;
  282. }
  283. if (p + 3 < end) {
  284. *p++ = '\\';
  285. *p++ = '0' + ((c & 0300) >> 6);
  286. *p++ = '0' + ((c & 070) >> 3);
  287. *p++ = '0' + (c & 07);
  288. continue;
  289. }
  290. m->count = m->size;
  291. return -1;
  292. }
  293. m->count = p - m->buf;
  294. return 0;
  295. }
  296. EXPORT_SYMBOL(seq_escape);
  297. int seq_printf(struct seq_file *m, const char *f, ...)
  298. {
  299. va_list args;
  300. int len;
  301. if (m->count < m->size) {
  302. va_start(args, f);
  303. len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
  304. va_end(args);
  305. if (m->count + len < m->size) {
  306. m->count += len;
  307. return 0;
  308. }
  309. }
  310. m->count = m->size;
  311. return -1;
  312. }
  313. EXPORT_SYMBOL(seq_printf);
  314. int seq_path(struct seq_file *m,
  315. struct vfsmount *mnt, struct dentry *dentry,
  316. char *esc)
  317. {
  318. if (m->count < m->size) {
  319. char *s = m->buf + m->count;
  320. char *p = d_path(dentry, mnt, s, m->size - m->count);
  321. if (!IS_ERR(p)) {
  322. while (s <= p) {
  323. char c = *p++;
  324. if (!c) {
  325. p = m->buf + m->count;
  326. m->count = s - m->buf;
  327. return s - p;
  328. } else if (!strchr(esc, c)) {
  329. *s++ = c;
  330. } else if (s + 4 > p) {
  331. break;
  332. } else {
  333. *s++ = '\\';
  334. *s++ = '0' + ((c & 0300) >> 6);
  335. *s++ = '0' + ((c & 070) >> 3);
  336. *s++ = '0' + (c & 07);
  337. }
  338. }
  339. }
  340. }
  341. m->count = m->size;
  342. return -1;
  343. }
  344. EXPORT_SYMBOL(seq_path);
  345. static void *single_start(struct seq_file *p, loff_t *pos)
  346. {
  347. return NULL + (*pos == 0);
  348. }
  349. static void *single_next(struct seq_file *p, void *v, loff_t *pos)
  350. {
  351. ++*pos;
  352. return NULL;
  353. }
  354. static void single_stop(struct seq_file *p, void *v)
  355. {
  356. }
  357. int single_open(struct file *file, int (*show)(struct seq_file *, void *),
  358. void *data)
  359. {
  360. struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
  361. int res = -ENOMEM;
  362. if (op) {
  363. op->start = single_start;
  364. op->next = single_next;
  365. op->stop = single_stop;
  366. op->show = show;
  367. res = seq_open(file, op);
  368. if (!res)
  369. ((struct seq_file *)file->private_data)->private = data;
  370. else
  371. kfree(op);
  372. }
  373. return res;
  374. }
  375. EXPORT_SYMBOL(single_open);
  376. int single_release(struct inode *inode, struct file *file)
  377. {
  378. struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
  379. int res = seq_release(inode, file);
  380. kfree(op);
  381. return res;
  382. }
  383. EXPORT_SYMBOL(single_release);
  384. int seq_release_private(struct inode *inode, struct file *file)
  385. {
  386. struct seq_file *seq = file->private_data;
  387. kfree(seq->private);
  388. seq->private = NULL;
  389. return seq_release(inode, file);
  390. }
  391. EXPORT_SYMBOL(seq_release_private);
  392. int seq_putc(struct seq_file *m, char c)
  393. {
  394. if (m->count < m->size) {
  395. m->buf[m->count++] = c;
  396. return 0;
  397. }
  398. return -1;
  399. }
  400. EXPORT_SYMBOL(seq_putc);
  401. int seq_puts(struct seq_file *m, const char *s)
  402. {
  403. int len = strlen(s);
  404. if (m->count + len < m->size) {
  405. memcpy(m->buf + m->count, s, len);
  406. m->count += len;
  407. return 0;
  408. }
  409. m->count = m->size;
  410. return -1;
  411. }
  412. EXPORT_SYMBOL(seq_puts);