seq_file.c 22 KB

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