seq_file.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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/export.h>
  9. #include <linux/seq_file.h>
  10. #include <linux/vmalloc.h>
  11. #include <linux/slab.h>
  12. #include <linux/cred.h>
  13. #include <linux/mm.h>
  14. #include <asm/uaccess.h>
  15. #include <asm/page.h>
  16. static void seq_set_overflow(struct seq_file *m)
  17. {
  18. m->count = m->size;
  19. }
  20. static void *seq_buf_alloc(unsigned long size)
  21. {
  22. void *buf;
  23. /*
  24. * __GFP_NORETRY to avoid oom-killings with high-order allocations -
  25. * it's better to fall back to vmalloc() than to kill things.
  26. */
  27. buf = kmalloc(size, GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN);
  28. if (!buf && size > PAGE_SIZE)
  29. buf = vmalloc(size);
  30. return buf;
  31. }
  32. /**
  33. * seq_open - initialize sequential file
  34. * @file: file we initialize
  35. * @op: method table describing the sequence
  36. *
  37. * seq_open() sets @file, associating it with a sequence described
  38. * by @op. @op->start() sets the iterator up and returns the first
  39. * element of sequence. @op->stop() shuts it down. @op->next()
  40. * returns the next element of sequence. @op->show() prints element
  41. * into the buffer. In case of error ->start() and ->next() return
  42. * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
  43. * returns 0 in case of success and negative number in case of error.
  44. * Returning SEQ_SKIP means "discard this element and move on".
  45. * Note: seq_open() will allocate a struct seq_file and store its
  46. * pointer in @file->private_data. This pointer should not be modified.
  47. */
  48. int seq_open(struct file *file, const struct seq_operations *op)
  49. {
  50. struct seq_file *p;
  51. WARN_ON(file->private_data);
  52. p = kzalloc(sizeof(*p), GFP_KERNEL);
  53. if (!p)
  54. return -ENOMEM;
  55. file->private_data = p;
  56. mutex_init(&p->lock);
  57. p->op = op;
  58. #ifdef CONFIG_USER_NS
  59. p->user_ns = file->f_cred->user_ns;
  60. #endif
  61. /*
  62. * Wrappers around seq_open(e.g. swaps_open) need to be
  63. * aware of this. If they set f_version themselves, they
  64. * should call seq_open first and then set f_version.
  65. */
  66. file->f_version = 0;
  67. /*
  68. * seq_files support lseek() and pread(). They do not implement
  69. * write() at all, but we clear FMODE_PWRITE here for historical
  70. * reasons.
  71. *
  72. * If a client of seq_files a) implements file.write() and b) wishes to
  73. * support pwrite() then that client will need to implement its own
  74. * file.open() which calls seq_open() and then sets FMODE_PWRITE.
  75. */
  76. file->f_mode &= ~FMODE_PWRITE;
  77. return 0;
  78. }
  79. EXPORT_SYMBOL(seq_open);
  80. static int traverse(struct seq_file *m, loff_t offset)
  81. {
  82. loff_t pos = 0, index;
  83. int error = 0;
  84. void *p;
  85. m->version = 0;
  86. index = 0;
  87. m->count = m->from = 0;
  88. if (!offset) {
  89. m->index = index;
  90. return 0;
  91. }
  92. if (!m->buf) {
  93. m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
  94. if (!m->buf)
  95. return -ENOMEM;
  96. }
  97. p = m->op->start(m, &index);
  98. while (p) {
  99. error = PTR_ERR(p);
  100. if (IS_ERR(p))
  101. break;
  102. error = m->op->show(m, p);
  103. if (error < 0)
  104. break;
  105. if (unlikely(error)) {
  106. error = 0;
  107. m->count = 0;
  108. }
  109. if (seq_has_overflowed(m))
  110. goto Eoverflow;
  111. if (pos + m->count > offset) {
  112. m->from = offset - pos;
  113. m->count -= m->from;
  114. m->index = index;
  115. break;
  116. }
  117. pos += m->count;
  118. m->count = 0;
  119. if (pos == offset) {
  120. index++;
  121. m->index = index;
  122. break;
  123. }
  124. p = m->op->next(m, p, &index);
  125. }
  126. m->op->stop(m, p);
  127. m->index = index;
  128. return error;
  129. Eoverflow:
  130. m->op->stop(m, p);
  131. kvfree(m->buf);
  132. m->count = 0;
  133. m->buf = seq_buf_alloc(m->size <<= 1);
  134. return !m->buf ? -ENOMEM : -EAGAIN;
  135. }
  136. /**
  137. * seq_read - ->read() method for sequential files.
  138. * @file: the file to read from
  139. * @buf: the buffer to read to
  140. * @size: the maximum number of bytes to read
  141. * @ppos: the current position in the file
  142. *
  143. * Ready-made ->f_op->read()
  144. */
  145. ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
  146. {
  147. struct seq_file *m = file->private_data;
  148. size_t copied = 0;
  149. loff_t pos;
  150. size_t n;
  151. void *p;
  152. int err = 0;
  153. mutex_lock(&m->lock);
  154. /*
  155. * seq_file->op->..m_start/m_stop/m_next may do special actions
  156. * or optimisations based on the file->f_version, so we want to
  157. * pass the file->f_version to those methods.
  158. *
  159. * seq_file->version is just copy of f_version, and seq_file
  160. * methods can treat it simply as file version.
  161. * It is copied in first and copied out after all operations.
  162. * It is convenient to have it as part of structure to avoid the
  163. * need of passing another argument to all the seq_file methods.
  164. */
  165. m->version = file->f_version;
  166. /* Don't assume *ppos is where we left it */
  167. if (unlikely(*ppos != m->read_pos)) {
  168. while ((err = traverse(m, *ppos)) == -EAGAIN)
  169. ;
  170. if (err) {
  171. /* With prejudice... */
  172. m->read_pos = 0;
  173. m->version = 0;
  174. m->index = 0;
  175. m->count = 0;
  176. goto Done;
  177. } else {
  178. m->read_pos = *ppos;
  179. }
  180. }
  181. /* grab buffer if we didn't have one */
  182. if (!m->buf) {
  183. m->buf = seq_buf_alloc(m->size = PAGE_SIZE);
  184. if (!m->buf)
  185. goto Enomem;
  186. }
  187. /* if not empty - flush it first */
  188. if (m->count) {
  189. n = min(m->count, size);
  190. err = copy_to_user(buf, m->buf + m->from, n);
  191. if (err)
  192. goto Efault;
  193. m->count -= n;
  194. m->from += n;
  195. size -= n;
  196. buf += n;
  197. copied += n;
  198. if (!m->count)
  199. m->index++;
  200. if (!size)
  201. goto Done;
  202. }
  203. /* we need at least one record in buffer */
  204. pos = m->index;
  205. p = m->op->start(m, &pos);
  206. while (1) {
  207. err = PTR_ERR(p);
  208. if (!p || IS_ERR(p))
  209. break;
  210. err = m->op->show(m, p);
  211. if (err < 0)
  212. break;
  213. if (unlikely(err))
  214. m->count = 0;
  215. if (unlikely(!m->count)) {
  216. p = m->op->next(m, p, &pos);
  217. m->index = pos;
  218. continue;
  219. }
  220. if (m->count < m->size)
  221. goto Fill;
  222. m->op->stop(m, p);
  223. kvfree(m->buf);
  224. m->count = 0;
  225. m->buf = seq_buf_alloc(m->size <<= 1);
  226. if (!m->buf)
  227. goto Enomem;
  228. m->version = 0;
  229. pos = m->index;
  230. p = m->op->start(m, &pos);
  231. }
  232. m->op->stop(m, p);
  233. m->count = 0;
  234. goto Done;
  235. Fill:
  236. /* they want more? let's try to get some more */
  237. while (m->count < size) {
  238. size_t offs = m->count;
  239. loff_t next = pos;
  240. p = m->op->next(m, p, &next);
  241. if (!p || IS_ERR(p)) {
  242. err = PTR_ERR(p);
  243. break;
  244. }
  245. err = m->op->show(m, p);
  246. if (seq_has_overflowed(m) || err) {
  247. m->count = offs;
  248. if (likely(err <= 0))
  249. break;
  250. }
  251. pos = next;
  252. }
  253. m->op->stop(m, p);
  254. n = min(m->count, size);
  255. err = copy_to_user(buf, m->buf, n);
  256. if (err)
  257. goto Efault;
  258. copied += n;
  259. m->count -= n;
  260. if (m->count)
  261. m->from = n;
  262. else
  263. pos++;
  264. m->index = pos;
  265. Done:
  266. if (!copied)
  267. copied = err;
  268. else {
  269. *ppos += copied;
  270. m->read_pos += copied;
  271. }
  272. file->f_version = m->version;
  273. mutex_unlock(&m->lock);
  274. return copied;
  275. Enomem:
  276. err = -ENOMEM;
  277. goto Done;
  278. Efault:
  279. err = -EFAULT;
  280. goto Done;
  281. }
  282. EXPORT_SYMBOL(seq_read);
  283. /**
  284. * seq_lseek - ->llseek() method for sequential files.
  285. * @file: the file in question
  286. * @offset: new position
  287. * @whence: 0 for absolute, 1 for relative position
  288. *
  289. * Ready-made ->f_op->llseek()
  290. */
  291. loff_t seq_lseek(struct file *file, loff_t offset, int whence)
  292. {
  293. struct seq_file *m = file->private_data;
  294. loff_t retval = -EINVAL;
  295. mutex_lock(&m->lock);
  296. m->version = file->f_version;
  297. switch (whence) {
  298. case SEEK_CUR:
  299. offset += file->f_pos;
  300. case SEEK_SET:
  301. if (offset < 0)
  302. break;
  303. retval = offset;
  304. if (offset != m->read_pos) {
  305. while ((retval = traverse(m, offset)) == -EAGAIN)
  306. ;
  307. if (retval) {
  308. /* with extreme prejudice... */
  309. file->f_pos = 0;
  310. m->read_pos = 0;
  311. m->version = 0;
  312. m->index = 0;
  313. m->count = 0;
  314. } else {
  315. m->read_pos = offset;
  316. retval = file->f_pos = offset;
  317. }
  318. } else {
  319. file->f_pos = offset;
  320. }
  321. }
  322. file->f_version = m->version;
  323. mutex_unlock(&m->lock);
  324. return retval;
  325. }
  326. EXPORT_SYMBOL(seq_lseek);
  327. /**
  328. * seq_release - free the structures associated with sequential file.
  329. * @file: file in question
  330. * @inode: its inode
  331. *
  332. * Frees the structures associated with sequential file; can be used
  333. * as ->f_op->release() if you don't have private data to destroy.
  334. */
  335. int seq_release(struct inode *inode, struct file *file)
  336. {
  337. struct seq_file *m = file->private_data;
  338. kvfree(m->buf);
  339. kfree(m);
  340. return 0;
  341. }
  342. EXPORT_SYMBOL(seq_release);
  343. /**
  344. * seq_escape - print string into buffer, escaping some characters
  345. * @m: target buffer
  346. * @s: string
  347. * @esc: set of characters that need escaping
  348. *
  349. * Puts string into buffer, replacing each occurrence of character from
  350. * @esc with usual octal escape. Returns 0 in case of success, -1 - in
  351. * case of overflow.
  352. */
  353. int seq_escape(struct seq_file *m, const char *s, const char *esc)
  354. {
  355. char *end = m->buf + m->size;
  356. char *p;
  357. char c;
  358. for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
  359. if (!strchr(esc, c)) {
  360. *p++ = c;
  361. continue;
  362. }
  363. if (p + 3 < end) {
  364. *p++ = '\\';
  365. *p++ = '0' + ((c & 0300) >> 6);
  366. *p++ = '0' + ((c & 070) >> 3);
  367. *p++ = '0' + (c & 07);
  368. continue;
  369. }
  370. seq_set_overflow(m);
  371. return -1;
  372. }
  373. m->count = p - m->buf;
  374. return 0;
  375. }
  376. EXPORT_SYMBOL(seq_escape);
  377. int seq_vprintf(struct seq_file *m, const char *f, va_list args)
  378. {
  379. int len;
  380. if (m->count < m->size) {
  381. len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
  382. if (m->count + len < m->size) {
  383. m->count += len;
  384. return 0;
  385. }
  386. }
  387. seq_set_overflow(m);
  388. return -1;
  389. }
  390. EXPORT_SYMBOL(seq_vprintf);
  391. int seq_printf(struct seq_file *m, const char *f, ...)
  392. {
  393. int ret;
  394. va_list args;
  395. va_start(args, f);
  396. ret = seq_vprintf(m, f, args);
  397. va_end(args);
  398. return ret;
  399. }
  400. EXPORT_SYMBOL(seq_printf);
  401. /**
  402. * mangle_path - mangle and copy path to buffer beginning
  403. * @s: buffer start
  404. * @p: beginning of path in above buffer
  405. * @esc: set of characters that need escaping
  406. *
  407. * Copy the path from @p to @s, replacing each occurrence of character from
  408. * @esc with usual octal escape.
  409. * Returns pointer past last written character in @s, or NULL in case of
  410. * failure.
  411. */
  412. char *mangle_path(char *s, const char *p, const char *esc)
  413. {
  414. while (s <= p) {
  415. char c = *p++;
  416. if (!c) {
  417. return s;
  418. } else if (!strchr(esc, c)) {
  419. *s++ = c;
  420. } else if (s + 4 > p) {
  421. break;
  422. } else {
  423. *s++ = '\\';
  424. *s++ = '0' + ((c & 0300) >> 6);
  425. *s++ = '0' + ((c & 070) >> 3);
  426. *s++ = '0' + (c & 07);
  427. }
  428. }
  429. return NULL;
  430. }
  431. EXPORT_SYMBOL(mangle_path);
  432. /**
  433. * seq_path - seq_file interface to print a pathname
  434. * @m: the seq_file handle
  435. * @path: the struct path to print
  436. * @esc: set of characters to escape in the output
  437. *
  438. * return the absolute path of 'path', as represented by the
  439. * dentry / mnt pair in the path parameter.
  440. */
  441. int seq_path(struct seq_file *m, const struct path *path, const char *esc)
  442. {
  443. char *buf;
  444. size_t size = seq_get_buf(m, &buf);
  445. int res = -1;
  446. if (size) {
  447. char *p = d_path(path, buf, size);
  448. if (!IS_ERR(p)) {
  449. char *end = mangle_path(buf, p, esc);
  450. if (end)
  451. res = end - buf;
  452. }
  453. }
  454. seq_commit(m, res);
  455. return res;
  456. }
  457. EXPORT_SYMBOL(seq_path);
  458. /**
  459. * seq_file_path - seq_file interface to print a pathname of a file
  460. * @m: the seq_file handle
  461. * @file: the struct file to print
  462. * @esc: set of characters to escape in the output
  463. *
  464. * return the absolute path to the file.
  465. */
  466. int seq_file_path(struct seq_file *m, struct file *file, const char *esc)
  467. {
  468. return seq_path(m, &file->f_path, esc);
  469. }
  470. EXPORT_SYMBOL(seq_file_path);
  471. /*
  472. * Same as seq_path, but relative to supplied root.
  473. */
  474. int seq_path_root(struct seq_file *m, const struct path *path,
  475. const struct path *root, const char *esc)
  476. {
  477. char *buf;
  478. size_t size = seq_get_buf(m, &buf);
  479. int res = -ENAMETOOLONG;
  480. if (size) {
  481. char *p;
  482. p = __d_path(path, root, buf, size);
  483. if (!p)
  484. return SEQ_SKIP;
  485. res = PTR_ERR(p);
  486. if (!IS_ERR(p)) {
  487. char *end = mangle_path(buf, p, esc);
  488. if (end)
  489. res = end - buf;
  490. else
  491. res = -ENAMETOOLONG;
  492. }
  493. }
  494. seq_commit(m, res);
  495. return res < 0 && res != -ENAMETOOLONG ? res : 0;
  496. }
  497. /*
  498. * returns the path of the 'dentry' from the root of its filesystem.
  499. */
  500. int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
  501. {
  502. char *buf;
  503. size_t size = seq_get_buf(m, &buf);
  504. int res = -1;
  505. if (size) {
  506. char *p = dentry_path(dentry, buf, size);
  507. if (!IS_ERR(p)) {
  508. char *end = mangle_path(buf, p, esc);
  509. if (end)
  510. res = end - buf;
  511. }
  512. }
  513. seq_commit(m, res);
  514. return res;
  515. }
  516. EXPORT_SYMBOL(seq_dentry);
  517. static void *single_start(struct seq_file *p, loff_t *pos)
  518. {
  519. return NULL + (*pos == 0);
  520. }
  521. static void *single_next(struct seq_file *p, void *v, loff_t *pos)
  522. {
  523. ++*pos;
  524. return NULL;
  525. }
  526. static void single_stop(struct seq_file *p, void *v)
  527. {
  528. }
  529. int single_open(struct file *file, int (*show)(struct seq_file *, void *),
  530. void *data)
  531. {
  532. struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
  533. int res = -ENOMEM;
  534. if (op) {
  535. op->start = single_start;
  536. op->next = single_next;
  537. op->stop = single_stop;
  538. op->show = show;
  539. res = seq_open(file, op);
  540. if (!res)
  541. ((struct seq_file *)file->private_data)->private = data;
  542. else
  543. kfree(op);
  544. }
  545. return res;
  546. }
  547. EXPORT_SYMBOL(single_open);
  548. int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
  549. void *data, size_t size)
  550. {
  551. char *buf = seq_buf_alloc(size);
  552. int ret;
  553. if (!buf)
  554. return -ENOMEM;
  555. ret = single_open(file, show, data);
  556. if (ret) {
  557. kvfree(buf);
  558. return ret;
  559. }
  560. ((struct seq_file *)file->private_data)->buf = buf;
  561. ((struct seq_file *)file->private_data)->size = size;
  562. return 0;
  563. }
  564. EXPORT_SYMBOL(single_open_size);
  565. int single_release(struct inode *inode, struct file *file)
  566. {
  567. const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
  568. int res = seq_release(inode, file);
  569. kfree(op);
  570. return res;
  571. }
  572. EXPORT_SYMBOL(single_release);
  573. int seq_release_private(struct inode *inode, struct file *file)
  574. {
  575. struct seq_file *seq = file->private_data;
  576. kfree(seq->private);
  577. seq->private = NULL;
  578. return seq_release(inode, file);
  579. }
  580. EXPORT_SYMBOL(seq_release_private);
  581. void *__seq_open_private(struct file *f, const struct seq_operations *ops,
  582. int psize)
  583. {
  584. int rc;
  585. void *private;
  586. struct seq_file *seq;
  587. private = kzalloc(psize, GFP_KERNEL);
  588. if (private == NULL)
  589. goto out;
  590. rc = seq_open(f, ops);
  591. if (rc < 0)
  592. goto out_free;
  593. seq = f->private_data;
  594. seq->private = private;
  595. return private;
  596. out_free:
  597. kfree(private);
  598. out:
  599. return NULL;
  600. }
  601. EXPORT_SYMBOL(__seq_open_private);
  602. int seq_open_private(struct file *filp, const struct seq_operations *ops,
  603. int psize)
  604. {
  605. return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
  606. }
  607. EXPORT_SYMBOL(seq_open_private);
  608. int seq_putc(struct seq_file *m, char c)
  609. {
  610. if (m->count < m->size) {
  611. m->buf[m->count++] = c;
  612. return 0;
  613. }
  614. return -1;
  615. }
  616. EXPORT_SYMBOL(seq_putc);
  617. int seq_puts(struct seq_file *m, const char *s)
  618. {
  619. int len = strlen(s);
  620. if (m->count + len < m->size) {
  621. memcpy(m->buf + m->count, s, len);
  622. m->count += len;
  623. return 0;
  624. }
  625. seq_set_overflow(m);
  626. return -1;
  627. }
  628. EXPORT_SYMBOL(seq_puts);
  629. /*
  630. * A helper routine for putting decimal numbers without rich format of printf().
  631. * only 'unsigned long long' is supported.
  632. * This routine will put one byte delimiter + number into seq_file.
  633. * This routine is very quick when you show lots of numbers.
  634. * In usual cases, it will be better to use seq_printf(). It's easier to read.
  635. */
  636. int seq_put_decimal_ull(struct seq_file *m, char delimiter,
  637. unsigned long long num)
  638. {
  639. int len;
  640. if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
  641. goto overflow;
  642. if (delimiter)
  643. m->buf[m->count++] = delimiter;
  644. if (num < 10) {
  645. m->buf[m->count++] = num + '0';
  646. return 0;
  647. }
  648. len = num_to_str(m->buf + m->count, m->size - m->count, num);
  649. if (!len)
  650. goto overflow;
  651. m->count += len;
  652. return 0;
  653. overflow:
  654. seq_set_overflow(m);
  655. return -1;
  656. }
  657. EXPORT_SYMBOL(seq_put_decimal_ull);
  658. int seq_put_decimal_ll(struct seq_file *m, char delimiter,
  659. long long num)
  660. {
  661. if (num < 0) {
  662. if (m->count + 3 >= m->size) {
  663. seq_set_overflow(m);
  664. return -1;
  665. }
  666. if (delimiter)
  667. m->buf[m->count++] = delimiter;
  668. num = -num;
  669. delimiter = '-';
  670. }
  671. return seq_put_decimal_ull(m, delimiter, num);
  672. }
  673. EXPORT_SYMBOL(seq_put_decimal_ll);
  674. /**
  675. * seq_write - write arbitrary data to buffer
  676. * @seq: seq_file identifying the buffer to which data should be written
  677. * @data: data address
  678. * @len: number of bytes
  679. *
  680. * Return 0 on success, non-zero otherwise.
  681. */
  682. int seq_write(struct seq_file *seq, const void *data, size_t len)
  683. {
  684. if (seq->count + len < seq->size) {
  685. memcpy(seq->buf + seq->count, data, len);
  686. seq->count += len;
  687. return 0;
  688. }
  689. seq_set_overflow(seq);
  690. return -1;
  691. }
  692. EXPORT_SYMBOL(seq_write);
  693. /**
  694. * seq_pad - write padding spaces to buffer
  695. * @m: seq_file identifying the buffer to which data should be written
  696. * @c: the byte to append after padding if non-zero
  697. */
  698. void seq_pad(struct seq_file *m, char c)
  699. {
  700. int size = m->pad_until - m->count;
  701. if (size > 0)
  702. seq_printf(m, "%*s", size, "");
  703. if (c)
  704. seq_putc(m, c);
  705. }
  706. EXPORT_SYMBOL(seq_pad);
  707. struct list_head *seq_list_start(struct list_head *head, loff_t pos)
  708. {
  709. struct list_head *lh;
  710. list_for_each(lh, head)
  711. if (pos-- == 0)
  712. return lh;
  713. return NULL;
  714. }
  715. EXPORT_SYMBOL(seq_list_start);
  716. struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
  717. {
  718. if (!pos)
  719. return head;
  720. return seq_list_start(head, pos - 1);
  721. }
  722. EXPORT_SYMBOL(seq_list_start_head);
  723. struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
  724. {
  725. struct list_head *lh;
  726. lh = ((struct list_head *)v)->next;
  727. ++*ppos;
  728. return lh == head ? NULL : lh;
  729. }
  730. EXPORT_SYMBOL(seq_list_next);
  731. /**
  732. * seq_hlist_start - start an iteration of a hlist
  733. * @head: the head of the hlist
  734. * @pos: the start position of the sequence
  735. *
  736. * Called at seq_file->op->start().
  737. */
  738. struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
  739. {
  740. struct hlist_node *node;
  741. hlist_for_each(node, head)
  742. if (pos-- == 0)
  743. return node;
  744. return NULL;
  745. }
  746. EXPORT_SYMBOL(seq_hlist_start);
  747. /**
  748. * seq_hlist_start_head - start an iteration of a hlist
  749. * @head: the head of the hlist
  750. * @pos: the start position of the sequence
  751. *
  752. * Called at seq_file->op->start(). Call this function if you want to
  753. * print a header at the top of the output.
  754. */
  755. struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
  756. {
  757. if (!pos)
  758. return SEQ_START_TOKEN;
  759. return seq_hlist_start(head, pos - 1);
  760. }
  761. EXPORT_SYMBOL(seq_hlist_start_head);
  762. /**
  763. * seq_hlist_next - move to the next position of the hlist
  764. * @v: the current iterator
  765. * @head: the head of the hlist
  766. * @ppos: the current position
  767. *
  768. * Called at seq_file->op->next().
  769. */
  770. struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
  771. loff_t *ppos)
  772. {
  773. struct hlist_node *node = v;
  774. ++*ppos;
  775. if (v == SEQ_START_TOKEN)
  776. return head->first;
  777. else
  778. return node->next;
  779. }
  780. EXPORT_SYMBOL(seq_hlist_next);
  781. /**
  782. * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
  783. * @head: the head of the hlist
  784. * @pos: the start position of the sequence
  785. *
  786. * Called at seq_file->op->start().
  787. *
  788. * This list-traversal primitive may safely run concurrently with
  789. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  790. * as long as the traversal is guarded by rcu_read_lock().
  791. */
  792. struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
  793. loff_t pos)
  794. {
  795. struct hlist_node *node;
  796. __hlist_for_each_rcu(node, head)
  797. if (pos-- == 0)
  798. return node;
  799. return NULL;
  800. }
  801. EXPORT_SYMBOL(seq_hlist_start_rcu);
  802. /**
  803. * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
  804. * @head: the head of the hlist
  805. * @pos: the start position of the sequence
  806. *
  807. * Called at seq_file->op->start(). Call this function if you want to
  808. * print a header at the top of the output.
  809. *
  810. * This list-traversal primitive may safely run concurrently with
  811. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  812. * as long as the traversal is guarded by rcu_read_lock().
  813. */
  814. struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
  815. loff_t pos)
  816. {
  817. if (!pos)
  818. return SEQ_START_TOKEN;
  819. return seq_hlist_start_rcu(head, pos - 1);
  820. }
  821. EXPORT_SYMBOL(seq_hlist_start_head_rcu);
  822. /**
  823. * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
  824. * @v: the current iterator
  825. * @head: the head of the hlist
  826. * @ppos: the current position
  827. *
  828. * Called at seq_file->op->next().
  829. *
  830. * This list-traversal primitive may safely run concurrently with
  831. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  832. * as long as the traversal is guarded by rcu_read_lock().
  833. */
  834. struct hlist_node *seq_hlist_next_rcu(void *v,
  835. struct hlist_head *head,
  836. loff_t *ppos)
  837. {
  838. struct hlist_node *node = v;
  839. ++*ppos;
  840. if (v == SEQ_START_TOKEN)
  841. return rcu_dereference(head->first);
  842. else
  843. return rcu_dereference(node->next);
  844. }
  845. EXPORT_SYMBOL(seq_hlist_next_rcu);
  846. /**
  847. * seq_hlist_start_precpu - start an iteration of a percpu hlist array
  848. * @head: pointer to percpu array of struct hlist_heads
  849. * @cpu: pointer to cpu "cursor"
  850. * @pos: start position of sequence
  851. *
  852. * Called at seq_file->op->start().
  853. */
  854. struct hlist_node *
  855. seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
  856. {
  857. struct hlist_node *node;
  858. for_each_possible_cpu(*cpu) {
  859. hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
  860. if (pos-- == 0)
  861. return node;
  862. }
  863. }
  864. return NULL;
  865. }
  866. EXPORT_SYMBOL(seq_hlist_start_percpu);
  867. /**
  868. * seq_hlist_next_percpu - move to the next position of the percpu hlist array
  869. * @v: pointer to current hlist_node
  870. * @head: pointer to percpu array of struct hlist_heads
  871. * @cpu: pointer to cpu "cursor"
  872. * @pos: start position of sequence
  873. *
  874. * Called at seq_file->op->next().
  875. */
  876. struct hlist_node *
  877. seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
  878. int *cpu, loff_t *pos)
  879. {
  880. struct hlist_node *node = v;
  881. ++*pos;
  882. if (node->next)
  883. return node->next;
  884. for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
  885. *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
  886. struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
  887. if (!hlist_empty(bucket))
  888. return bucket->first;
  889. }
  890. return NULL;
  891. }
  892. EXPORT_SYMBOL(seq_hlist_next_percpu);