seq_file.c 23 KB

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