proc_namespace.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * fs/proc_namespace.c - handling of /proc/<pid>/{mounts,mountinfo,mountstats}
  3. *
  4. * In fact, that's a piece of procfs; it's *almost* isolated from
  5. * the rest of fs/proc, but has rather close relationships with
  6. * fs/namespace.c, thus here instead of fs/proc
  7. *
  8. */
  9. #include <linux/mnt_namespace.h>
  10. #include <linux/nsproxy.h>
  11. #include <linux/security.h>
  12. #include <linux/fs_struct.h>
  13. #include "proc/internal.h" /* only for get_proc_task() in ->open() */
  14. #include "pnode.h"
  15. #include "internal.h"
  16. static unsigned mounts_poll(struct file *file, poll_table *wait)
  17. {
  18. struct proc_mounts *p = file->private_data;
  19. struct mnt_namespace *ns = p->ns;
  20. unsigned res = POLLIN | POLLRDNORM;
  21. poll_wait(file, &p->ns->poll, wait);
  22. br_read_lock(vfsmount_lock);
  23. if (p->m.poll_event != ns->event) {
  24. p->m.poll_event = ns->event;
  25. res |= POLLERR | POLLPRI;
  26. }
  27. br_read_unlock(vfsmount_lock);
  28. return res;
  29. }
  30. struct proc_fs_info {
  31. int flag;
  32. const char *str;
  33. };
  34. static int show_sb_opts(struct seq_file *m, struct super_block *sb)
  35. {
  36. static const struct proc_fs_info fs_info[] = {
  37. { MS_SYNCHRONOUS, ",sync" },
  38. { MS_DIRSYNC, ",dirsync" },
  39. { MS_MANDLOCK, ",mand" },
  40. { 0, NULL }
  41. };
  42. const struct proc_fs_info *fs_infop;
  43. for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
  44. if (sb->s_flags & fs_infop->flag)
  45. seq_puts(m, fs_infop->str);
  46. }
  47. return security_sb_show_options(m, sb);
  48. }
  49. static void show_mnt_opts(struct seq_file *m, struct vfsmount *mnt)
  50. {
  51. static const struct proc_fs_info mnt_info[] = {
  52. { MNT_NOSUID, ",nosuid" },
  53. { MNT_NODEV, ",nodev" },
  54. { MNT_NOEXEC, ",noexec" },
  55. { MNT_NOATIME, ",noatime" },
  56. { MNT_NODIRATIME, ",nodiratime" },
  57. { MNT_RELATIME, ",relatime" },
  58. { 0, NULL }
  59. };
  60. const struct proc_fs_info *fs_infop;
  61. for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
  62. if (mnt->mnt_flags & fs_infop->flag)
  63. seq_puts(m, fs_infop->str);
  64. }
  65. }
  66. static inline void mangle(struct seq_file *m, const char *s)
  67. {
  68. seq_escape(m, s, " \t\n\\");
  69. }
  70. static void show_type(struct seq_file *m, struct super_block *sb)
  71. {
  72. mangle(m, sb->s_type->name);
  73. if (sb->s_subtype && sb->s_subtype[0]) {
  74. seq_putc(m, '.');
  75. mangle(m, sb->s_subtype);
  76. }
  77. }
  78. static int show_vfsmnt(struct seq_file *m, struct vfsmount *mnt)
  79. {
  80. struct mount *r = real_mount(mnt);
  81. int err = 0;
  82. struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
  83. if (mnt->mnt_sb->s_op->show_devname) {
  84. err = mnt->mnt_sb->s_op->show_devname(m, mnt);
  85. if (err)
  86. goto out;
  87. } else {
  88. mangle(m, r->mnt_devname ? r->mnt_devname : "none");
  89. }
  90. seq_putc(m, ' ');
  91. seq_path(m, &mnt_path, " \t\n\\");
  92. seq_putc(m, ' ');
  93. show_type(m, mnt->mnt_sb);
  94. seq_puts(m, __mnt_is_readonly(mnt) ? " ro" : " rw");
  95. err = show_sb_opts(m, mnt->mnt_sb);
  96. if (err)
  97. goto out;
  98. show_mnt_opts(m, mnt);
  99. if (mnt->mnt_sb->s_op->show_options)
  100. err = mnt->mnt_sb->s_op->show_options(m, mnt);
  101. seq_puts(m, " 0 0\n");
  102. out:
  103. return err;
  104. }
  105. static int show_mountinfo(struct seq_file *m, struct vfsmount *mnt)
  106. {
  107. struct proc_mounts *p = m->private;
  108. struct mount *r = real_mount(mnt);
  109. struct super_block *sb = mnt->mnt_sb;
  110. struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
  111. struct path root = p->root;
  112. int err = 0;
  113. seq_printf(m, "%i %i %u:%u ", r->mnt_id, r->mnt_parent->mnt_id,
  114. MAJOR(sb->s_dev), MINOR(sb->s_dev));
  115. if (sb->s_op->show_path)
  116. err = sb->s_op->show_path(m, mnt);
  117. else
  118. seq_dentry(m, mnt->mnt_root, " \t\n\\");
  119. if (err)
  120. goto out;
  121. seq_putc(m, ' ');
  122. /* mountpoints outside of chroot jail will give SEQ_SKIP on this */
  123. err = seq_path_root(m, &mnt_path, &root, " \t\n\\");
  124. if (err)
  125. goto out;
  126. seq_puts(m, mnt->mnt_flags & MNT_READONLY ? " ro" : " rw");
  127. show_mnt_opts(m, mnt);
  128. /* Tagged fields ("foo:X" or "bar") */
  129. if (IS_MNT_SHARED(r))
  130. seq_printf(m, " shared:%i", r->mnt_group_id);
  131. if (IS_MNT_SLAVE(r)) {
  132. int master = r->mnt_master->mnt_group_id;
  133. int dom = get_dominating_id(r, &p->root);
  134. seq_printf(m, " master:%i", master);
  135. if (dom && dom != master)
  136. seq_printf(m, " propagate_from:%i", dom);
  137. }
  138. if (IS_MNT_UNBINDABLE(r))
  139. seq_puts(m, " unbindable");
  140. /* Filesystem specific data */
  141. seq_puts(m, " - ");
  142. show_type(m, sb);
  143. seq_putc(m, ' ');
  144. if (sb->s_op->show_devname)
  145. err = sb->s_op->show_devname(m, mnt);
  146. else
  147. mangle(m, r->mnt_devname ? r->mnt_devname : "none");
  148. if (err)
  149. goto out;
  150. seq_puts(m, sb->s_flags & MS_RDONLY ? " ro" : " rw");
  151. err = show_sb_opts(m, sb);
  152. if (err)
  153. goto out;
  154. if (sb->s_op->show_options)
  155. err = sb->s_op->show_options(m, mnt);
  156. seq_putc(m, '\n');
  157. out:
  158. return err;
  159. }
  160. static int show_vfsstat(struct seq_file *m, struct vfsmount *mnt)
  161. {
  162. struct mount *r = real_mount(mnt);
  163. struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
  164. int err = 0;
  165. /* device */
  166. if (mnt->mnt_sb->s_op->show_devname) {
  167. seq_puts(m, "device ");
  168. err = mnt->mnt_sb->s_op->show_devname(m, mnt);
  169. } else {
  170. if (r->mnt_devname) {
  171. seq_puts(m, "device ");
  172. mangle(m, r->mnt_devname);
  173. } else
  174. seq_puts(m, "no device");
  175. }
  176. /* mount point */
  177. seq_puts(m, " mounted on ");
  178. seq_path(m, &mnt_path, " \t\n\\");
  179. seq_putc(m, ' ');
  180. /* file system type */
  181. seq_puts(m, "with fstype ");
  182. show_type(m, mnt->mnt_sb);
  183. /* optional statistics */
  184. if (mnt->mnt_sb->s_op->show_stats) {
  185. seq_putc(m, ' ');
  186. if (!err)
  187. err = mnt->mnt_sb->s_op->show_stats(m, mnt);
  188. }
  189. seq_putc(m, '\n');
  190. return err;
  191. }
  192. static int mounts_open_common(struct inode *inode, struct file *file,
  193. int (*show)(struct seq_file *, struct vfsmount *))
  194. {
  195. struct task_struct *task = get_proc_task(inode);
  196. struct nsproxy *nsp;
  197. struct mnt_namespace *ns = NULL;
  198. struct path root;
  199. struct proc_mounts *p;
  200. int ret = -EINVAL;
  201. if (!task)
  202. goto err;
  203. rcu_read_lock();
  204. nsp = task_nsproxy(task);
  205. if (!nsp) {
  206. rcu_read_unlock();
  207. put_task_struct(task);
  208. goto err;
  209. }
  210. ns = nsp->mnt_ns;
  211. if (!ns) {
  212. rcu_read_unlock();
  213. put_task_struct(task);
  214. goto err;
  215. }
  216. get_mnt_ns(ns);
  217. rcu_read_unlock();
  218. task_lock(task);
  219. if (!task->fs) {
  220. task_unlock(task);
  221. put_task_struct(task);
  222. ret = -ENOENT;
  223. goto err_put_ns;
  224. }
  225. get_fs_root(task->fs, &root);
  226. task_unlock(task);
  227. put_task_struct(task);
  228. ret = -ENOMEM;
  229. p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
  230. if (!p)
  231. goto err_put_path;
  232. file->private_data = &p->m;
  233. ret = seq_open(file, &mounts_op);
  234. if (ret)
  235. goto err_free;
  236. p->m.private = p;
  237. p->ns = ns;
  238. p->root = root;
  239. p->m.poll_event = ns->event;
  240. p->show = show;
  241. return 0;
  242. err_free:
  243. kfree(p);
  244. err_put_path:
  245. path_put(&root);
  246. err_put_ns:
  247. put_mnt_ns(ns);
  248. err:
  249. return ret;
  250. }
  251. static int mounts_release(struct inode *inode, struct file *file)
  252. {
  253. struct proc_mounts *p = file->private_data;
  254. path_put(&p->root);
  255. put_mnt_ns(p->ns);
  256. return seq_release(inode, file);
  257. }
  258. static int mounts_open(struct inode *inode, struct file *file)
  259. {
  260. return mounts_open_common(inode, file, show_vfsmnt);
  261. }
  262. static int mountinfo_open(struct inode *inode, struct file *file)
  263. {
  264. return mounts_open_common(inode, file, show_mountinfo);
  265. }
  266. static int mountstats_open(struct inode *inode, struct file *file)
  267. {
  268. return mounts_open_common(inode, file, show_vfsstat);
  269. }
  270. const struct file_operations proc_mounts_operations = {
  271. .open = mounts_open,
  272. .read = seq_read,
  273. .llseek = seq_lseek,
  274. .release = mounts_release,
  275. .poll = mounts_poll,
  276. };
  277. const struct file_operations proc_mountinfo_operations = {
  278. .open = mountinfo_open,
  279. .read = seq_read,
  280. .llseek = seq_lseek,
  281. .release = mounts_release,
  282. .poll = mounts_poll,
  283. };
  284. const struct file_operations proc_mountstats_operations = {
  285. .open = mountstats_open,
  286. .read = seq_read,
  287. .llseek = seq_lseek,
  288. .release = mounts_release,
  289. };