seq_file.c 22 KB

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