seq_file.c 22 KB

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