seq_file.c 22 KB

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