original development tree for Linux kernel GTP module; now long in mainline.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

995 lines
22 KiB

fix apparmor dereferencing potentially freed dentry, sanitize __d_path() API __d_path() API is asking for trouble and in case of apparmor d_namespace_path() getting just that. The root cause is that when __d_path() misses the root it had been told to look for, it stores the location of the most remote ancestor in *root. Without grabbing references. Sure, at the moment of call it had been pinned down by what we have in *path. And if we raced with umount -l, we could have very well stopped at vfsmount/dentry that got freed as soon as prepend_path() dropped vfsmount_lock. It is safe to compare these pointers with pre-existing (and known to be still alive) vfsmount and dentry, as long as all we are asking is "is it the same address?". Dereferencing is not safe and apparmor ended up stepping into that. d_namespace_path() really wants to examine the place where we stopped, even if it's not connected to our namespace. As the result, it looked at ->d_sb->s_magic of a dentry that might've been already freed by that point. All other callers had been careful enough to avoid that, but it's really a bad interface - it invites that kind of trouble. The fix is fairly straightforward, even though it's bigger than I'd like: * prepend_path() root argument becomes const. * __d_path() is never called with NULL/NULL root. It was a kludge to start with. Instead, we have an explicit function - d_absolute_root(). Same as __d_path(), except that it doesn't get root passed and stops where it stops. apparmor and tomoyo are using it. * __d_path() returns NULL on path outside of root. The main caller is show_mountinfo() and that's precisely what we pass root for - to skip those outside chroot jail. Those who don't want that can (and do) use d_path(). * __d_path() root argument becomes const. Everyone agrees, I hope. * apparmor does *NOT* try to use __d_path() or any of its variants when it sees that path->mnt is an internal vfsmount. In that case it's definitely not mounted anywhere and dentry_path() is exactly what we want there. Handling of sysctl()-triggered weirdness is moved to that place. * if apparmor is asked to do pathname relative to chroot jail and __d_path() tells it we it's not in that jail, the sucker just calls d_absolute_path() instead. That's the other remaining caller of __d_path(), BTW. * seq_path_root() does _NOT_ return -ENAMETOOLONG (it's stupid anyway - the normal seq_file logics will take care of growing the buffer and redoing the call of ->show() just fine). However, if it gets path not reachable from root, it returns SEQ_SKIP. The only caller adjusted (i.e. stopped ignoring the return value as it used to do). Reviewed-by: John Johansen <john.johansen@canonical.com> ACKed-by: John Johansen <john.johansen@canonical.com> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk> Cc: stable@vger.kernel.org
10 years ago
fix apparmor dereferencing potentially freed dentry, sanitize __d_path() API __d_path() API is asking for trouble and in case of apparmor d_namespace_path() getting just that. The root cause is that when __d_path() misses the root it had been told to look for, it stores the location of the most remote ancestor in *root. Without grabbing references. Sure, at the moment of call it had been pinned down by what we have in *path. And if we raced with umount -l, we could have very well stopped at vfsmount/dentry that got freed as soon as prepend_path() dropped vfsmount_lock. It is safe to compare these pointers with pre-existing (and known to be still alive) vfsmount and dentry, as long as all we are asking is "is it the same address?". Dereferencing is not safe and apparmor ended up stepping into that. d_namespace_path() really wants to examine the place where we stopped, even if it's not connected to our namespace. As the result, it looked at ->d_sb->s_magic of a dentry that might've been already freed by that point. All other callers had been careful enough to avoid that, but it's really a bad interface - it invites that kind of trouble. The fix is fairly straightforward, even though it's bigger than I'd like: * prepend_path() root argument becomes const. * __d_path() is never called with NULL/NULL root. It was a kludge to start with. Instead, we have an explicit function - d_absolute_root(). Same as __d_path(), except that it doesn't get root passed and stops where it stops. apparmor and tomoyo are using it. * __d_path() returns NULL on path outside of root. The main caller is show_mountinfo() and that's precisely what we pass root for - to skip those outside chroot jail. Those who don't want that can (and do) use d_path(). * __d_path() root argument becomes const. Everyone agrees, I hope. * apparmor does *NOT* try to use __d_path() or any of its variants when it sees that path->mnt is an internal vfsmount. In that case it's definitely not mounted anywhere and dentry_path() is exactly what we want there. Handling of sysctl()-triggered weirdness is moved to that place. * if apparmor is asked to do pathname relative to chroot jail and __d_path() tells it we it's not in that jail, the sucker just calls d_absolute_path() instead. That's the other remaining caller of __d_path(), BTW. * seq_path_root() does _NOT_ return -ENAMETOOLONG (it's stupid anyway - the normal seq_file logics will take care of growing the buffer and redoing the call of ->show() just fine). However, if it gets path not reachable from root, it returns SEQ_SKIP. The only caller adjusted (i.e. stopped ignoring the return value as it used to do). Reviewed-by: John Johansen <john.johansen@canonical.com> ACKed-by: John Johansen <john.johansen@canonical.com> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk> Cc: stable@vger.kernel.org
10 years ago
procfs: add num_to_str() to speed up /proc/stat == stat_check.py num = 0 with open("/proc/stat") as f: while num < 1000 : data = f.read() f.seek(0, 0) num = num + 1 == perf shows 20.39% stat_check.py [kernel.kallsyms] [k] format_decode 13.41% stat_check.py [kernel.kallsyms] [k] number 12.61% stat_check.py [kernel.kallsyms] [k] vsnprintf 10.85% stat_check.py [kernel.kallsyms] [k] memcpy 4.85% stat_check.py [kernel.kallsyms] [k] radix_tree_lookup 4.43% stat_check.py [kernel.kallsyms] [k] seq_printf This patch removes most of calls to vsnprintf() by adding num_to_str() and seq_print_decimal_ull(), which prints decimal numbers without rich functions provided by printf(). On my 8cpu box. == Before patch == [root@bluextal test]# time ./stat_check.py real 0m0.150s user 0m0.026s sys 0m0.121s == After patch == [root@bluextal test]# time ./stat_check.py real 0m0.055s user 0m0.022s sys 0m0.030s [akpm@linux-foundation.org: remove incorrect comment, use less statck in num_to_str(), move comment from .h to .c, simplify seq_put_decimal_ull()] [andrea@betterlinux.com: avoid breaking the ABI in /proc/stat] Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Signed-off-by: Andrea Righi <andrea@betterlinux.com> Cc: Eric Dumazet <eric.dumazet@gmail.com> Cc: Glauber Costa <glommer@parallels.com> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Ingo Molnar <mingo@elte.hu> Cc: Paul Turner <pjt@google.com> Cc: Russell King <rmk@arm.linux.org.uk> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
10 years ago
procfs: add num_to_str() to speed up /proc/stat == stat_check.py num = 0 with open("/proc/stat") as f: while num < 1000 : data = f.read() f.seek(0, 0) num = num + 1 == perf shows 20.39% stat_check.py [kernel.kallsyms] [k] format_decode 13.41% stat_check.py [kernel.kallsyms] [k] number 12.61% stat_check.py [kernel.kallsyms] [k] vsnprintf 10.85% stat_check.py [kernel.kallsyms] [k] memcpy 4.85% stat_check.py [kernel.kallsyms] [k] radix_tree_lookup 4.43% stat_check.py [kernel.kallsyms] [k] seq_printf This patch removes most of calls to vsnprintf() by adding num_to_str() and seq_print_decimal_ull(), which prints decimal numbers without rich functions provided by printf(). On my 8cpu box. == Before patch == [root@bluextal test]# time ./stat_check.py real 0m0.150s user 0m0.026s sys 0m0.121s == After patch == [root@bluextal test]# time ./stat_check.py real 0m0.055s user 0m0.022s sys 0m0.030s [akpm@linux-foundation.org: remove incorrect comment, use less statck in num_to_str(), move comment from .h to .c, simplify seq_put_decimal_ull()] [andrea@betterlinux.com: avoid breaking the ABI in /proc/stat] Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Signed-off-by: Andrea Righi <andrea@betterlinux.com> Cc: Eric Dumazet <eric.dumazet@gmail.com> Cc: Glauber Costa <glommer@parallels.com> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Ingo Molnar <mingo@elte.hu> Cc: Paul Turner <pjt@google.com> Cc: Russell King <rmk@arm.linux.org.uk> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
10 years ago
procfs: add num_to_str() to speed up /proc/stat == stat_check.py num = 0 with open("/proc/stat") as f: while num < 1000 : data = f.read() f.seek(0, 0) num = num + 1 == perf shows 20.39% stat_check.py [kernel.kallsyms] [k] format_decode 13.41% stat_check.py [kernel.kallsyms] [k] number 12.61% stat_check.py [kernel.kallsyms] [k] vsnprintf 10.85% stat_check.py [kernel.kallsyms] [k] memcpy 4.85% stat_check.py [kernel.kallsyms] [k] radix_tree_lookup 4.43% stat_check.py [kernel.kallsyms] [k] seq_printf This patch removes most of calls to vsnprintf() by adding num_to_str() and seq_print_decimal_ull(), which prints decimal numbers without rich functions provided by printf(). On my 8cpu box. == Before patch == [root@bluextal test]# time ./stat_check.py real 0m0.150s user 0m0.026s sys 0m0.121s == After patch == [root@bluextal test]# time ./stat_check.py real 0m0.055s user 0m0.022s sys 0m0.030s [akpm@linux-foundation.org: remove incorrect comment, use less statck in num_to_str(), move comment from .h to .c, simplify seq_put_decimal_ull()] [andrea@betterlinux.com: avoid breaking the ABI in /proc/stat] Signed-off-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Signed-off-by: Andrea Righi <andrea@betterlinux.com> Cc: Eric Dumazet <eric.dumazet@gmail.com> Cc: Glauber Costa <glommer@parallels.com> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Ingo Molnar <mingo@elte.hu> Cc: Paul Turner <pjt@google.com> Cc: Russell King <rmk@arm.linux.org.uk> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
10 years ago
  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/slab.h>
  11. #include <linux/cred.h>
  12. #include <asm/uaccess.h>
  13. #include <asm/page.h>
  14. /*
  15. * seq_files have a buffer which can may overflow. When this happens a larger
  16. * buffer is reallocated and all the data will be printed again.
  17. * The overflow state is true when m->count == m->size.
  18. */
  19. static bool seq_overflow(struct seq_file *m)
  20. {
  21. return m->count == m->size;
  22. }
  23. static void seq_set_overflow(struct seq_file *m)
  24. {
  25. m->count = m->size;
  26. }
  27. /**
  28. * seq_open - initialize sequential file
  29. * @file: file we initialize
  30. * @op: method table describing the sequence
  31. *
  32. * seq_open() sets @file, associating it with a sequence described
  33. * by @op. @op->start() sets the iterator up and returns the first
  34. * element of sequence. @op->stop() shuts it down. @op->next()
  35. * returns the next element of sequence. @op->show() prints element
  36. * into the buffer. In case of error ->start() and ->next() return
  37. * ERR_PTR(error). In the end of sequence they return %NULL. ->show()
  38. * returns 0 in case of success and negative number in case of error.
  39. * Returning SEQ_SKIP means "discard this element and move on".
  40. */
  41. int seq_open(struct file *file, const struct seq_operations *op)
  42. {
  43. struct seq_file *p = file->private_data;
  44. if (!p) {
  45. p = kmalloc(sizeof(*p), GFP_KERNEL);
  46. if (!p)
  47. return -ENOMEM;
  48. file->private_data = p;
  49. }
  50. memset(p, 0, sizeof(*p));
  51. mutex_init(&p->lock);
  52. p->op = op;
  53. #ifdef CONFIG_USER_NS
  54. p->user_ns = file->f_cred->user_ns;
  55. #endif
  56. /*
  57. * Wrappers around seq_open(e.g. swaps_open) need to be
  58. * aware of this. If they set f_version themselves, they
  59. * should call seq_open first and then set f_version.
  60. */
  61. file->f_version = 0;
  62. /*
  63. * seq_files support lseek() and pread(). They do not implement
  64. * write() at all, but we clear FMODE_PWRITE here for historical
  65. * reasons.
  66. *
  67. * If a client of seq_files a) implements file.write() and b) wishes to
  68. * support pwrite() then that client will need to implement its own
  69. * file.open() which calls seq_open() and then sets FMODE_PWRITE.
  70. */
  71. file->f_mode &= ~FMODE_PWRITE;
  72. return 0;
  73. }
  74. EXPORT_SYMBOL(seq_open);
  75. static int traverse(struct seq_file *m, loff_t offset)
  76. {
  77. loff_t pos = 0, index;
  78. int error = 0;
  79. void *p;
  80. m->version = 0;
  81. index = 0;
  82. m->count = m->from = 0;
  83. if (!offset) {
  84. m->index = index;
  85. return 0;
  86. }
  87. if (!m->buf) {
  88. m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
  89. if (!m->buf)
  90. return -ENOMEM;
  91. }
  92. p = m->op->start(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_overflow(m))
  105. goto Eoverflow;
  106. if (pos + m->count > offset) {
  107. m->from = offset - pos;
  108. m->count -= m->from;
  109. m->index = index;
  110. break;
  111. }
  112. pos += m->count;
  113. m->count = 0;
  114. if (pos == offset) {
  115. index++;
  116. m->index = index;
  117. break;
  118. }
  119. p = m->op->next(m, p, &index);
  120. }
  121. m->op->stop(m, p);
  122. m->index = index;
  123. return error;
  124. Eoverflow:
  125. m->op->stop(m, p);
  126. kfree(m->buf);
  127. m->count = 0;
  128. m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
  129. return !m->buf ? -ENOMEM : -EAGAIN;
  130. }
  131. /**
  132. * seq_read - ->read() method for sequential files.
  133. * @file: the file to read from
  134. * @buf: the buffer to read to
  135. * @size: the maximum number of bytes to read
  136. * @ppos: the current position in the file
  137. *
  138. * Ready-made ->f_op->read()
  139. */
  140. ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
  141. {
  142. struct seq_file *m = file->private_data;
  143. size_t copied = 0;
  144. loff_t pos;
  145. size_t n;
  146. void *p;
  147. int err = 0;
  148. mutex_lock(&m->lock);
  149. /*
  150. * seq_file->op->..m_start/m_stop/m_next may do special actions
  151. * or optimisations based on the file->f_version, so we want to
  152. * pass the file->f_version to those methods.
  153. *
  154. * seq_file->version is just copy of f_version, and seq_file
  155. * methods can treat it simply as file version.
  156. * It is copied in first and copied out after all operations.
  157. * It is convenient to have it as part of structure to avoid the
  158. * need of passing another argument to all the seq_file methods.
  159. */
  160. m->version = file->f_version;
  161. /* Don't assume *ppos is where we left it */
  162. if (unlikely(*ppos != m->read_pos)) {
  163. while ((err = traverse(m, *ppos)) == -EAGAIN)
  164. ;
  165. if (err) {
  166. /* With prejudice... */
  167. m->read_pos = 0;
  168. m->version = 0;
  169. m->index = 0;
  170. m->count = 0;
  171. goto Done;
  172. } else {
  173. m->read_pos = *ppos;
  174. }
  175. }
  176. /* grab buffer if we didn't have one */
  177. if (!m->buf) {
  178. m->buf = kmalloc(m->size = PAGE_SIZE, GFP_KERNEL);
  179. if (!m->buf)
  180. goto Enomem;
  181. }
  182. /* if not empty - flush it first */
  183. if (m->count) {
  184. n = min(m->count, size);
  185. err = copy_to_user(buf, m->buf + m->from, n);
  186. if (err)
  187. goto Efault;
  188. m->count -= n;
  189. m->from += n;
  190. size -= n;
  191. buf += n;
  192. copied += n;
  193. if (!m->count)
  194. m->index++;
  195. if (!size)
  196. goto Done;
  197. }
  198. /* we need at least one record in buffer */
  199. pos = m->index;
  200. p = m->op->start(m, &pos);
  201. while (1) {
  202. err = PTR_ERR(p);
  203. if (!p || IS_ERR(p))
  204. break;
  205. err = m->op->show(m, p);
  206. if (err < 0)
  207. break;
  208. if (unlikely(err))
  209. m->count = 0;
  210. if (unlikely(!m->count)) {
  211. p = m->op->next(m, p, &pos);
  212. m->index = pos;
  213. continue;
  214. }
  215. if (m->count < m->size)
  216. goto Fill;
  217. m->op->stop(m, p);
  218. kfree(m->buf);
  219. m->count = 0;
  220. m->buf = kmalloc(m->size <<= 1, GFP_KERNEL);
  221. if (!m->buf)
  222. goto Enomem;
  223. m->version = 0;
  224. pos = m->index;
  225. p = m->op->start(m, &pos);
  226. }
  227. m->op->stop(m, p);
  228. m->count = 0;
  229. goto Done;
  230. Fill:
  231. /* they want more? let's try to get some more */
  232. while (m->count < size) {
  233. size_t offs = m->count;
  234. loff_t next = pos;
  235. p = m->op->next(m, p, &next);
  236. if (!p || IS_ERR(p)) {
  237. err = PTR_ERR(p);
  238. break;
  239. }
  240. err = m->op->show(m, p);
  241. if (seq_overflow(m) || err) {
  242. m->count = offs;
  243. if (likely(err <= 0))
  244. break;
  245. }
  246. pos = next;
  247. }
  248. m->op->stop(m, p);
  249. n = min(m->count, size);
  250. err = copy_to_user(buf, m->buf, n);
  251. if (err)
  252. goto Efault;
  253. copied += n;
  254. m->count -= n;
  255. if (m->count)
  256. m->from = n;
  257. else
  258. pos++;
  259. m->index = pos;
  260. Done:
  261. if (!copied)
  262. copied = err;
  263. else {
  264. *ppos += copied;
  265. m->read_pos += copied;
  266. }
  267. file->f_version = m->version;
  268. mutex_unlock(&m->lock);
  269. return copied;
  270. Enomem:
  271. err = -ENOMEM;
  272. goto Done;
  273. Efault:
  274. err = -EFAULT;
  275. goto Done;
  276. }
  277. EXPORT_SYMBOL(seq_read);
  278. /**
  279. * seq_lseek - ->llseek() method for sequential files.
  280. * @file: the file in question
  281. * @offset: new position
  282. * @whence: 0 for absolute, 1 for relative position
  283. *
  284. * Ready-made ->f_op->llseek()
  285. */
  286. loff_t seq_lseek(struct file *file, loff_t offset, int whence)
  287. {
  288. struct seq_file *m = file->private_data;
  289. loff_t retval = -EINVAL;
  290. mutex_lock(&m->lock);
  291. m->version = file->f_version;
  292. switch (whence) {
  293. case SEEK_CUR:
  294. offset += file->f_pos;
  295. case SEEK_SET:
  296. if (offset < 0)
  297. break;
  298. retval = offset;
  299. if (offset != m->read_pos) {
  300. while ((retval = traverse(m, offset)) == -EAGAIN)
  301. ;
  302. if (retval) {
  303. /* with extreme prejudice... */
  304. file->f_pos = 0;
  305. m->read_pos = 0;
  306. m->version = 0;
  307. m->index = 0;
  308. m->count = 0;
  309. } else {
  310. m->read_pos = offset;
  311. retval = file->f_pos = offset;
  312. }
  313. } else {
  314. file->f_pos = offset;
  315. }
  316. }
  317. file->f_version = m->version;
  318. mutex_unlock(&m->lock);
  319. return retval;
  320. }
  321. EXPORT_SYMBOL(seq_lseek);
  322. /**
  323. * seq_release - free the structures associated with sequential file.
  324. * @file: file in question
  325. * @inode: its inode
  326. *
  327. * Frees the structures associated with sequential file; can be used
  328. * as ->f_op->release() if you don't have private data to destroy.
  329. */
  330. int seq_release(struct inode *inode, struct file *file)
  331. {
  332. struct seq_file *m = file->private_data;
  333. kfree(m->buf);
  334. kfree(m);
  335. return 0;
  336. }
  337. EXPORT_SYMBOL(seq_release);
  338. /**
  339. * seq_escape - print string into buffer, escaping some characters
  340. * @m: target buffer
  341. * @s: string
  342. * @esc: set of characters that need escaping
  343. *
  344. * Puts string into buffer, replacing each occurrence of character from
  345. * @esc with usual octal escape. Returns 0 in case of success, -1 - in
  346. * case of overflow.
  347. */
  348. int seq_escape(struct seq_file *m, const char *s, const char *esc)
  349. {
  350. char *end = m->buf + m->size;
  351. char *p;
  352. char c;
  353. for (p = m->buf + m->count; (c = *s) != '\0' && p < end; s++) {
  354. if (!strchr(esc, c)) {
  355. *p++ = c;
  356. continue;
  357. }
  358. if (p + 3 < end) {
  359. *p++ = '\\';
  360. *p++ = '0' + ((c & 0300) >> 6);
  361. *p++ = '0' + ((c & 070) >> 3);
  362. *p++ = '0' + (c & 07);
  363. continue;
  364. }
  365. seq_set_overflow(m);
  366. return -1;
  367. }
  368. m->count = p - m->buf;
  369. return 0;
  370. }
  371. EXPORT_SYMBOL(seq_escape);
  372. int seq_vprintf(struct seq_file *m, const char *f, va_list args)
  373. {
  374. int len;
  375. if (m->count < m->size) {
  376. len = vsnprintf(m->buf + m->count, m->size - m->count, f, args);
  377. if (m->count + len < m->size) {
  378. m->count += len;
  379. return 0;
  380. }
  381. }
  382. seq_set_overflow(m);
  383. return -1;
  384. }
  385. EXPORT_SYMBOL(seq_vprintf);
  386. int seq_printf(struct seq_file *m, const char *f, ...)
  387. {
  388. int ret;
  389. va_list args;
  390. va_start(args, f);
  391. ret = seq_vprintf(m, f, args);
  392. va_end(args);
  393. return ret;
  394. }
  395. EXPORT_SYMBOL(seq_printf);
  396. /**
  397. * mangle_path - mangle and copy path to buffer beginning
  398. * @s: buffer start
  399. * @p: beginning of path in above buffer
  400. * @esc: set of characters that need escaping
  401. *
  402. * Copy the path from @p to @s, replacing each occurrence of character from
  403. * @esc with usual octal escape.
  404. * Returns pointer past last written character in @s, or NULL in case of
  405. * failure.
  406. */
  407. char *mangle_path(char *s, const char *p, const char *esc)
  408. {
  409. while (s <= p) {
  410. char c = *p++;
  411. if (!c) {
  412. return s;
  413. } else if (!strchr(esc, c)) {
  414. *s++ = c;
  415. } else if (s + 4 > p) {
  416. break;
  417. } else {
  418. *s++ = '\\';
  419. *s++ = '0' + ((c & 0300) >> 6);
  420. *s++ = '0' + ((c & 070) >> 3);
  421. *s++ = '0' + (c & 07);
  422. }
  423. }
  424. return NULL;
  425. }
  426. EXPORT_SYMBOL(mangle_path);
  427. /**
  428. * seq_path - seq_file interface to print a pathname
  429. * @m: the seq_file handle
  430. * @path: the struct path to print
  431. * @esc: set of characters to escape in the output
  432. *
  433. * return the absolute path of 'path', as represented by the
  434. * dentry / mnt pair in the path parameter.
  435. */
  436. int seq_path(struct seq_file *m, const struct path *path, const char *esc)
  437. {
  438. char *buf;
  439. size_t size = seq_get_buf(m, &buf);
  440. int res = -1;
  441. if (size) {
  442. char *p = d_path(path, buf, size);
  443. if (!IS_ERR(p)) {
  444. char *end = mangle_path(buf, p, esc);
  445. if (end)
  446. res = end - buf;
  447. }
  448. }
  449. seq_commit(m, res);
  450. return res;
  451. }
  452. EXPORT_SYMBOL(seq_path);
  453. /*
  454. * Same as seq_path, but relative to supplied root.
  455. */
  456. int seq_path_root(struct seq_file *m, const struct path *path,
  457. const struct path *root, const char *esc)
  458. {
  459. char *buf;
  460. size_t size = seq_get_buf(m, &buf);
  461. int res = -ENAMETOOLONG;
  462. if (size) {
  463. char *p;
  464. p = __d_path(path, root, buf, size);
  465. if (!p)
  466. return SEQ_SKIP;
  467. res = PTR_ERR(p);
  468. if (!IS_ERR(p)) {
  469. char *end = mangle_path(buf, p, esc);
  470. if (end)
  471. res = end - buf;
  472. else
  473. res = -ENAMETOOLONG;
  474. }
  475. }
  476. seq_commit(m, res);
  477. return res < 0 && res != -ENAMETOOLONG ? res : 0;
  478. }
  479. /*
  480. * returns the path of the 'dentry' from the root of its filesystem.
  481. */
  482. int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc)
  483. {
  484. char *buf;
  485. size_t size = seq_get_buf(m, &buf);
  486. int res = -1;
  487. if (size) {
  488. char *p = dentry_path(dentry, buf, size);
  489. if (!IS_ERR(p)) {
  490. char *end = mangle_path(buf, p, esc);
  491. if (end)
  492. res = end - buf;
  493. }
  494. }
  495. seq_commit(m, res);
  496. return res;
  497. }
  498. int seq_bitmap(struct seq_file *m, const unsigned long *bits,
  499. unsigned int nr_bits)
  500. {
  501. if (m->count < m->size) {
  502. int len = bitmap_scnprintf(m->buf + m->count,
  503. m->size - m->count, bits, nr_bits);
  504. if (m->count + len < m->size) {
  505. m->count += len;
  506. return 0;
  507. }
  508. }
  509. seq_set_overflow(m);
  510. return -1;
  511. }
  512. EXPORT_SYMBOL(seq_bitmap);
  513. int seq_bitmap_list(struct seq_file *m, const unsigned long *bits,
  514. unsigned int nr_bits)
  515. {
  516. if (m->count < m->size) {
  517. int len = bitmap_scnlistprintf(m->buf + m->count,
  518. m->size - m->count, bits, nr_bits);
  519. if (m->count + len < m->size) {
  520. m->count += len;
  521. return 0;
  522. }
  523. }
  524. seq_set_overflow(m);
  525. return -1;
  526. }
  527. EXPORT_SYMBOL(seq_bitmap_list);
  528. static void *single_start(struct seq_file *p, loff_t *pos)
  529. {
  530. return NULL + (*pos == 0);
  531. }
  532. static void *single_next(struct seq_file *p, void *v, loff_t *pos)
  533. {
  534. ++*pos;
  535. return NULL;
  536. }
  537. static void single_stop(struct seq_file *p, void *v)
  538. {
  539. }
  540. int single_open(struct file *file, int (*show)(struct seq_file *, void *),
  541. void *data)
  542. {
  543. struct seq_operations *op = kmalloc(sizeof(*op), GFP_KERNEL);
  544. int res = -ENOMEM;
  545. if (op) {
  546. op->start = single_start;
  547. op->next = single_next;
  548. op->stop = single_stop;
  549. op->show = show;
  550. res = seq_open(file, op);
  551. if (!res)
  552. ((struct seq_file *)file->private_data)->private = data;
  553. else
  554. kfree(op);
  555. }
  556. return res;
  557. }
  558. EXPORT_SYMBOL(single_open);
  559. int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
  560. void *data, size_t size)
  561. {
  562. char *buf = kmalloc(size, GFP_KERNEL);
  563. int ret;
  564. if (!buf)
  565. return -ENOMEM;
  566. ret = single_open(file, show, data);
  567. if (ret) {
  568. kfree(buf);
  569. return ret;
  570. }
  571. ((struct seq_file *)file->private_data)->buf = buf;
  572. ((struct seq_file *)file->private_data)->size = size;
  573. return 0;
  574. }
  575. EXPORT_SYMBOL(single_open_size);
  576. int single_release(struct inode *inode, struct file *file)
  577. {
  578. const struct seq_operations *op = ((struct seq_file *)file->private_data)->op;
  579. int res = seq_release(inode, file);
  580. kfree(op);
  581. return res;
  582. }
  583. EXPORT_SYMBOL(single_release);
  584. int seq_release_private(struct inode *inode, struct file *file)
  585. {
  586. struct seq_file *seq = file->private_data;
  587. kfree(seq->private);
  588. seq->private = NULL;
  589. return seq_release(inode, file);
  590. }
  591. EXPORT_SYMBOL(seq_release_private);
  592. void *__seq_open_private(struct file *f, const struct seq_operations *ops,
  593. int psize)
  594. {
  595. int rc;
  596. void *private;
  597. struct seq_file *seq;
  598. private = kzalloc(psize, GFP_KERNEL);
  599. if (private == NULL)
  600. goto out;
  601. rc = seq_open(f, ops);
  602. if (rc < 0)
  603. goto out_free;
  604. seq = f->private_data;
  605. seq->private = private;
  606. return private;
  607. out_free:
  608. kfree(private);
  609. out:
  610. return NULL;
  611. }
  612. EXPORT_SYMBOL(__seq_open_private);
  613. int seq_open_private(struct file *filp, const struct seq_operations *ops,
  614. int psize)
  615. {
  616. return __seq_open_private(filp, ops, psize) ? 0 : -ENOMEM;
  617. }
  618. EXPORT_SYMBOL(seq_open_private);
  619. int seq_putc(struct seq_file *m, char c)
  620. {
  621. if (m->count < m->size) {
  622. m->buf[m->count++] = c;
  623. return 0;
  624. }
  625. return -1;
  626. }
  627. EXPORT_SYMBOL(seq_putc);
  628. int seq_puts(struct seq_file *m, const char *s)
  629. {
  630. int len = strlen(s);
  631. if (m->count + len < m->size) {
  632. memcpy(m->buf + m->count, s, len);
  633. m->count += len;
  634. return 0;
  635. }
  636. seq_set_overflow(m);
  637. return -1;
  638. }
  639. EXPORT_SYMBOL(seq_puts);
  640. /*
  641. * A helper routine for putting decimal numbers without rich format of printf().
  642. * only 'unsigned long long' is supported.
  643. * This routine will put one byte delimiter + number into seq_file.
  644. * This routine is very quick when you show lots of numbers.
  645. * In usual cases, it will be better to use seq_printf(). It's easier to read.
  646. */
  647. int seq_put_decimal_ull(struct seq_file *m, char delimiter,
  648. unsigned long long num)
  649. {
  650. int len;
  651. if (m->count + 2 >= m->size) /* we'll write 2 bytes at least */
  652. goto overflow;
  653. if (delimiter)
  654. m->buf[m->count++] = delimiter;
  655. if (num < 10) {
  656. m->buf[m->count++] = num + '0';
  657. return 0;
  658. }
  659. len = num_to_str(m->buf + m->count, m->size - m->count, num);
  660. if (!len)
  661. goto overflow;
  662. m->count += len;
  663. return 0;
  664. overflow:
  665. seq_set_overflow(m);
  666. return -1;
  667. }
  668. EXPORT_SYMBOL(seq_put_decimal_ull);
  669. int seq_put_decimal_ll(struct seq_file *m, char delimiter,
  670. long long num)
  671. {
  672. if (num < 0) {
  673. if (m->count + 3 >= m->size) {
  674. seq_set_overflow(m);
  675. return -1;
  676. }
  677. if (delimiter)
  678. m->buf[m->count++] = delimiter;
  679. num = -num;
  680. delimiter = '-';
  681. }
  682. return seq_put_decimal_ull(m, delimiter, num);
  683. }
  684. EXPORT_SYMBOL(seq_put_decimal_ll);
  685. /**
  686. * seq_write - write arbitrary data to buffer
  687. * @seq: seq_file identifying the buffer to which data should be written
  688. * @data: data address
  689. * @len: number of bytes
  690. *
  691. * Return 0 on success, non-zero otherwise.
  692. */
  693. int seq_write(struct seq_file *seq, const void *data, size_t len)
  694. {
  695. if (seq->count + len < seq->size) {
  696. memcpy(seq->buf + seq->count, data, len);
  697. seq->count += len;
  698. return 0;
  699. }
  700. seq_set_overflow(seq);
  701. return -1;
  702. }
  703. EXPORT_SYMBOL(seq_write);
  704. /**
  705. * seq_pad - write padding spaces to buffer
  706. * @m: seq_file identifying the buffer to which data should be written
  707. * @c: the byte to append after padding if non-zero
  708. */
  709. void seq_pad(struct seq_file *m, char c)
  710. {
  711. int size = m->pad_until - m->count;
  712. if (size > 0)
  713. seq_printf(m, "%*s", size, "");
  714. if (c)
  715. seq_putc(m, c);
  716. }
  717. EXPORT_SYMBOL(seq_pad);
  718. struct list_head *seq_list_start(struct list_head *head, loff_t pos)
  719. {
  720. struct list_head *lh;
  721. list_for_each(lh, head)
  722. if (pos-- == 0)
  723. return lh;
  724. return NULL;
  725. }
  726. EXPORT_SYMBOL(seq_list_start);
  727. struct list_head *seq_list_start_head(struct list_head *head, loff_t pos)
  728. {
  729. if (!pos)
  730. return head;
  731. return seq_list_start(head, pos - 1);
  732. }
  733. EXPORT_SYMBOL(seq_list_start_head);
  734. struct list_head *seq_list_next(void *v, struct list_head *head, loff_t *ppos)
  735. {
  736. struct list_head *lh;
  737. lh = ((struct list_head *)v)->next;
  738. ++*ppos;
  739. return lh == head ? NULL : lh;
  740. }
  741. EXPORT_SYMBOL(seq_list_next);
  742. /**
  743. * seq_hlist_start - start an iteration of a hlist
  744. * @head: the head of the hlist
  745. * @pos: the start position of the sequence
  746. *
  747. * Called at seq_file->op->start().
  748. */
  749. struct hlist_node *seq_hlist_start(struct hlist_head *head, loff_t pos)
  750. {
  751. struct hlist_node *node;
  752. hlist_for_each(node, head)
  753. if (pos-- == 0)
  754. return node;
  755. return NULL;
  756. }
  757. EXPORT_SYMBOL(seq_hlist_start);
  758. /**
  759. * seq_hlist_start_head - start an iteration of a hlist
  760. * @head: the head of the hlist
  761. * @pos: the start position of the sequence
  762. *
  763. * Called at seq_file->op->start(). Call this function if you want to
  764. * print a header at the top of the output.
  765. */
  766. struct hlist_node *seq_hlist_start_head(struct hlist_head *head, loff_t pos)
  767. {
  768. if (!pos)
  769. return SEQ_START_TOKEN;
  770. return seq_hlist_start(head, pos - 1);
  771. }
  772. EXPORT_SYMBOL(seq_hlist_start_head);
  773. /**
  774. * seq_hlist_next - move to the next position of the hlist
  775. * @v: the current iterator
  776. * @head: the head of the hlist
  777. * @ppos: the current position
  778. *
  779. * Called at seq_file->op->next().
  780. */
  781. struct hlist_node *seq_hlist_next(void *v, struct hlist_head *head,
  782. loff_t *ppos)
  783. {
  784. struct hlist_node *node = v;
  785. ++*ppos;
  786. if (v == SEQ_START_TOKEN)
  787. return head->first;
  788. else
  789. return node->next;
  790. }
  791. EXPORT_SYMBOL(seq_hlist_next);
  792. /**
  793. * seq_hlist_start_rcu - start an iteration of a hlist protected by RCU
  794. * @head: the head of the hlist
  795. * @pos: the start position of the sequence
  796. *
  797. * Called at seq_file->op->start().
  798. *
  799. * This list-traversal primitive may safely run concurrently with
  800. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  801. * as long as the traversal is guarded by rcu_read_lock().
  802. */
  803. struct hlist_node *seq_hlist_start_rcu(struct hlist_head *head,
  804. loff_t pos)
  805. {
  806. struct hlist_node *node;
  807. __hlist_for_each_rcu(node, head)
  808. if (pos-- == 0)
  809. return node;
  810. return NULL;
  811. }
  812. EXPORT_SYMBOL(seq_hlist_start_rcu);
  813. /**
  814. * seq_hlist_start_head_rcu - start an iteration of a hlist protected by RCU
  815. * @head: the head of the hlist
  816. * @pos: the start position of the sequence
  817. *
  818. * Called at seq_file->op->start(). Call this function if you want to
  819. * print a header at the top of the output.
  820. *
  821. * This list-traversal primitive may safely run concurrently with
  822. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  823. * as long as the traversal is guarded by rcu_read_lock().
  824. */
  825. struct hlist_node *seq_hlist_start_head_rcu(struct hlist_head *head,
  826. loff_t pos)
  827. {
  828. if (!pos)
  829. return SEQ_START_TOKEN;
  830. return seq_hlist_start_rcu(head, pos - 1);
  831. }
  832. EXPORT_SYMBOL(seq_hlist_start_head_rcu);
  833. /**
  834. * seq_hlist_next_rcu - move to the next position of the hlist protected by RCU
  835. * @v: the current iterator
  836. * @head: the head of the hlist
  837. * @ppos: the current position
  838. *
  839. * Called at seq_file->op->next().
  840. *
  841. * This list-traversal primitive may safely run concurrently with
  842. * the _rcu list-mutation primitives such as hlist_add_head_rcu()
  843. * as long as the traversal is guarded by rcu_read_lock().
  844. */
  845. struct hlist_node *seq_hlist_next_rcu(void *v,
  846. struct hlist_head *head,
  847. loff_t *ppos)
  848. {
  849. struct hlist_node *node = v;
  850. ++*ppos;
  851. if (v == SEQ_START_TOKEN)
  852. return rcu_dereference(head->first);
  853. else
  854. return rcu_dereference(node->next);
  855. }
  856. EXPORT_SYMBOL(seq_hlist_next_rcu);
  857. /**
  858. * seq_hlist_start_precpu - start an iteration of a percpu hlist array
  859. * @head: pointer to percpu array of struct hlist_heads
  860. * @cpu: pointer to cpu "cursor"
  861. * @pos: start position of sequence
  862. *
  863. * Called at seq_file->op->start().
  864. */
  865. struct hlist_node *
  866. seq_hlist_start_percpu(struct hlist_head __percpu *head, int *cpu, loff_t pos)
  867. {
  868. struct hlist_node *node;
  869. for_each_possible_cpu(*cpu) {
  870. hlist_for_each(node, per_cpu_ptr(head, *cpu)) {
  871. if (pos-- == 0)
  872. return node;
  873. }
  874. }
  875. return NULL;
  876. }
  877. EXPORT_SYMBOL(seq_hlist_start_percpu);
  878. /**
  879. * seq_hlist_next_percpu - move to the next position of the percpu hlist array
  880. * @v: pointer to current hlist_node
  881. * @head: pointer to percpu array of struct hlist_heads
  882. * @cpu: pointer to cpu "cursor"
  883. * @pos: start position of sequence
  884. *
  885. * Called at seq_file->op->next().
  886. */
  887. struct hlist_node *
  888. seq_hlist_next_percpu(void *v, struct hlist_head __percpu *head,
  889. int *cpu, loff_t *pos)
  890. {
  891. struct hlist_node *node = v;
  892. ++*pos;
  893. if (node->next)
  894. return node->next;
  895. for (*cpu = cpumask_next(*cpu, cpu_possible_mask); *cpu < nr_cpu_ids;
  896. *cpu = cpumask_next(*cpu, cpu_possible_mask)) {
  897. struct hlist_head *bucket = per_cpu_ptr(head, *cpu);
  898. if (!hlist_empty(bucket))
  899. return bucket->first;
  900. }
  901. return NULL;
  902. }
  903. EXPORT_SYMBOL(seq_hlist_next_percpu);