seq_file.c 22 KB

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