seq_file.c 25 KB

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