seq_file.c 11 KB

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