seq_file.c 25 KB

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