seq_file.c 9.6 KB

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