seq_file.c 9.5 KB

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