seq_file.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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, index;
  172. int error = 0;
  173. void *p;
  174. m->version = 0;
  175. index = 0;
  176. m->count = m->from = 0;
  177. if (!offset) {
  178. m->index = index;
  179. return 0;
  180. }
  181. if (!m->buf) {
  182. m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
  183. if (!m->buf)
  184. return -ENOMEM;
  185. }
  186. p = m->op->start(m, &index);
  187. while (p) {
  188. error = PTR_ERR(p);
  189. if (IS_ERR(p))
  190. break;
  191. error = m->op->show(m, p);
  192. if (error)
  193. break;
  194. if (m->count == m->size)
  195. goto Eoverflow;
  196. if (pos + m->count > offset) {
  197. m->from = offset - pos;
  198. m->count -= m->from;
  199. m->index = index;
  200. break;
  201. }
  202. pos += m->count;
  203. m->count = 0;
  204. if (pos == offset) {
  205. index++;
  206. m->index = index;
  207. break;
  208. }
  209. p = m->op->next(m, p, &index);
  210. }
  211. m->op->stop(m, p);
  212. return error;
  213. Eoverflow:
  214. m->op->stop(m, p);
  215. kfree(m->buf);
  216. m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
  217. return !m->buf ? -ENOMEM : -EAGAIN;
  218. }
  219. /**
  220. * seq_lseek - ->llseek() method for sequential files.
  221. * @file: the file in question
  222. * @offset: new position
  223. * @origin: 0 for absolute, 1 for relative position
  224. *
  225. * Ready-made ->f_op->llseek()
  226. */
  227. loff_t seq_lseek(struct file *file, loff_t offset, int origin)
  228. {
  229. struct seq_file *m = (struct seq_file *)file->private_data;
  230. long long retval = -EINVAL;
  231. mutex_lock(&m->lock);
  232. m->version = file->f_version;
  233. switch (origin) {
  234. case 1:
  235. offset += file->f_pos;
  236. case 0:
  237. if (offset < 0)
  238. break;
  239. retval = offset;
  240. if (offset != file->f_pos) {
  241. while ((retval=traverse(m, offset)) == -EAGAIN)
  242. ;
  243. if (retval) {
  244. /* with extreme prejudice... */
  245. file->f_pos = 0;
  246. m->version = 0;
  247. m->index = 0;
  248. m->count = 0;
  249. } else {
  250. retval = file->f_pos = offset;
  251. }
  252. }
  253. }
  254. file->f_version = m->version;
  255. mutex_unlock(&m->lock);
  256. return retval;
  257. }
  258. EXPORT_SYMBOL(seq_lseek);
  259. /**
  260. * seq_release - free the structures associated with sequential file.
  261. * @file: file in question
  262. * @inode: file->f_path.dentry->d_inode
  263. *
  264. * Frees the structures associated with sequential file; can be used
  265. * as ->f_op->release() if you don't have private data to destroy.
  266. */
  267. int seq_release(struct inode *inode, struct file *file)
  268. {
  269. struct seq_file *m = (struct seq_file *)file->private_data;
  270. kfree(m->buf);
  271. kfree(m);
  272. return 0;
  273. }
  274. EXPORT_SYMBOL(seq_release);
  275. /**
  276. * seq_escape - print string into buffer, escaping some characters
  277. * @m: target buffer
  278. * @s: string
  279. * @esc: set of characters that need escaping
  280. *
  281. * Puts string into buffer, replacing each occurrence of character from
  282. * @esc with usual octal escape. Returns 0 in case of success, -1 - in
  283. * case of overflow.
  284. */
  285. int seq_escape(struct seq_file *m, const char *s, const char *esc)
  286. {
  287. char *end = m->buf + m->size;
  288. char *p;
  289. char c;
  290. for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
  291. if (!strchr(esc, c)) {
  292. *p++ = c;
  293. continue;
  294. }
  295. if (p + 3 < end) {
  296. *p++ = '\\';
  297. *p++ = '0' + ((c & 0300) >> 6);
  298. *p++ = '0' + ((c & 070) >> 3);
  299. *p++ = '0' + (c & 07);
  300. continue;
  301. }
  302. m->count = m->size;
  303. return -1;
  304. }
  305. m->count = p - m->buf;
  306. return 0;
  307. }
  308. EXPORT_SYMBOL(seq_escape);
  309. int seq_printf(struct seq_file *m, const char *f, ...)
  310. {
  311. va_list args;
  312. int len;
  313. if (m->count < m->size) {
  314. va_start(args, f);
  315. len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
  316. va_end(args);
  317. if (m->count + len < m->size) {
  318. m->count += len;
  319. return 0;
  320. }
  321. }
  322. m->count = m->size;
  323. return -1;
  324. }
  325. EXPORT_SYMBOL(seq_printf);
  326. int seq_path(struct seq_file *m,
  327. struct vfsmount *mnt, struct dentry *dentry,
  328. char *esc)
  329. {
  330. if (m->count < m->size) {
  331. char *s = m->buf + m->count;
  332. char *p = d_path(dentry, mnt, s, m->size - m->count);
  333. if (!IS_ERR(p)) {
  334. while (s <= p) {
  335. char c = *p++;
  336. if (!c) {
  337. p = m->buf + m->count;
  338. m->count = s - m->buf;
  339. return s - p;
  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. }
  352. }
  353. m->count = m->size;
  354. return -1;
  355. }
  356. EXPORT_SYMBOL(seq_path);
  357. static void *single_start(struct seq_file *p, loff_t *pos)
  358. {
  359. return NULL + (*pos == 0);
  360. }
  361. static void *single_next(struct seq_file *p, void *v, loff_t *pos)
  362. {
  363. ++*pos;
  364. return NULL;
  365. }
  366. static void single_stop(struct seq_file *p, void *v)
  367. {
  368. }
  369. int single_open(struct file *file, int (*show)(struct seq_file *, void *),
  370. void *data)
  371. {
  372. struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
  373. int res = -ENOMEM;
  374. if (op) {
  375. op->start = single_start;
  376. op->next = single_next;
  377. op->stop = single_stop;
  378. op->show = show;
  379. res = seq_open(file, op);
  380. if (!res)
  381. ((struct seq_file *)file->private_data)->private = data;
  382. else
  383. kfree(op);
  384. }
  385. return res;
  386. }
  387. EXPORT_SYMBOL(single_open);
  388. int single_release(struct inode *inode, struct file *file)
  389. {
  390. const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
  391. int res = seq_release(inode, file);
  392. kfree(op);
  393. return res;
  394. }
  395. EXPORT_SYMBOL(single_release);
  396. int seq_release_private(struct inode *inode, struct file *file)
  397. {
  398. struct seq_file *seq = file->private_data;
  399. kfree(seq->private);
  400. seq->private = NULL;
  401. return seq_release(inode, file);
  402. }
  403. EXPORT_SYMBOL(seq_release_private);
  404. void *__seq_open_private(struct file *f, const struct seq_operations *ops,
  405. int psize)
  406. {
  407. int rc;
  408. void *private;
  409. struct seq_file *seq;
  410. private = kzalloc(psize, GFP_KERNEL);
  411. if (private == NULL)
  412. goto out;
  413. rc = seq_open(f, ops);
  414. if (rc < 0)
  415. goto out_free;
  416. seq = f->private_data;
  417. seq->private = private;
  418. return private;
  419. out_free:
  420. kfree(private);
  421. out:
  422. return NULL;
  423. }
  424. EXPORT_SYMBOL(__seq_open_private);
  425. int seq_open_private(struct file *filp, const struct seq_operations *ops,
  426. int psize)
  427. {
  428. return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
  429. }
  430. EXPORT_SYMBOL(seq_open_private);
  431. int seq_putc(struct seq_file *m, char c)
  432. {
  433. if (m->count < m->size) {
  434. m->buf[m->count++] = c;
  435. return 0;
  436. }
  437. return -1;
  438. }
  439. EXPORT_SYMBOL(seq_putc);
  440. int seq_puts(struct seq_file *m, const char *s)
  441. {
  442. int len = strlen(s);
  443. if (m->count + len < m->size) {
  444. memcpy(m->buf + m->count, s, len);
  445. m->count += len;
  446. return 0;
  447. }
  448. m->count = m->size;
  449. return -1;
  450. }
  451. EXPORT_SYMBOL(seq_puts);
  452. struct list_head *seq_list_start(struct list_head *head, loff_t pos)
  453. {
  454. struct list_head *lh;
  455. list_for_each(lh, head)
  456. if (pos-- == 0)
  457. return lh;
  458. return NULL;
  459. }
  460. EXPORT_SYMBOL(seq_list_start);
  461. struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
  462. {
  463. if (!pos)
  464. return head;
  465. return seq_list_start(head, pos - 1);
  466. }
  467. EXPORT_SYMBOL(seq_list_start_head);
  468. struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
  469. {
  470. struct list_head *lh;
  471. lh = ((struct list_head *)v)->next;
  472. ++*ppos;
  473. return lh == head ? NULL : lh;
  474. }
  475. EXPORT_SYMBOL(seq_list_next);