seq_file.c 23 KB

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