proc_namespace.c 7.7 KB

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