seq_file.c 24 KB

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