proc_namespace.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
  85. struct super_block *sb = mnt_path.dentry->d_sb;
  86. int err;
  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;
  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. if (err)
  124. goto out;
  125. } else {
  126. seq_dentry(m, mnt->mnt_root, " \t\n\\");
  127. }
  128. seq_putc(m, ' ');
  129. /* mountpoints outside of chroot jail will give SEQ_SKIP on this */
  130. err = seq_path_root(m, &mnt_path, &p->root, " \t\n\\");
  131. if (err)
  132. goto out;
  133. seq_puts(m, mnt->mnt_flags & MNT_READONLY ? " ro" : " rw");
  134. show_mnt_opts(m, mnt);
  135. /* Tagged fields ("foo:X" or "bar") */
  136. if (IS_MNT_SHARED(r))
  137. seq_printf(m, " shared:%i", r->mnt_group_id);
  138. if (IS_MNT_SLAVE(r)) {
  139. int master = r->mnt_master->mnt_group_id;
  140. int dom = get_dominating_id(r, &p->root);
  141. seq_printf(m, " master:%i", master);
  142. if (dom && dom != master)
  143. seq_printf(m, " propagate_from:%i", dom);
  144. }
  145. if (IS_MNT_UNBINDABLE(r))
  146. seq_puts(m, " unbindable");
  147. /* Filesystem specific data */
  148. seq_puts(m, " - ");
  149. show_type(m, sb);
  150. seq_putc(m, ' ');
  151. if (sb->s_op->show_devname) {
  152. err = sb->s_op->show_devname(m, mnt->mnt_root);
  153. if (err)
  154. goto out;
  155. } else {
  156. mangle(m, r->mnt_devname ? r->mnt_devname : "none");
  157. }
  158. seq_puts(m, sb->s_flags & MS_RDONLY ? " ro" : " rw");
  159. err = show_sb_opts(m, sb);
  160. if (err)
  161. goto out;
  162. if (sb->s_op->show_options)
  163. err = sb->s_op->show_options(m, mnt->mnt_root);
  164. seq_putc(m, '\n');
  165. out:
  166. return err;
  167. }
  168. static int show_vfsstat(struct seq_file *m, struct vfsmount *mnt)
  169. {
  170. struct proc_mounts *p = m->private;
  171. struct mount *r = real_mount(mnt);
  172. struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
  173. struct super_block *sb = mnt_path.dentry->d_sb;
  174. int err;
  175. /* device */
  176. if (sb->s_op->show_devname) {
  177. seq_puts(m, "device ");
  178. err = sb->s_op->show_devname(m, mnt_path.dentry);
  179. if (err)
  180. goto out;
  181. } else {
  182. if (r->mnt_devname) {
  183. seq_puts(m, "device ");
  184. mangle(m, r->mnt_devname);
  185. } else
  186. seq_puts(m, "no device");
  187. }
  188. /* mount point */
  189. seq_puts(m, " mounted on ");
  190. /* mountpoints outside of chroot jail will give SEQ_SKIP on this */
  191. err = seq_path_root(m, &mnt_path, &p->root, " \t\n\\");
  192. if (err)
  193. goto out;
  194. seq_putc(m, ' ');
  195. /* file system type */
  196. seq_puts(m, "with fstype ");
  197. show_type(m, sb);
  198. /* optional statistics */
  199. if (sb->s_op->show_stats) {
  200. seq_putc(m, ' ');
  201. err = sb->s_op->show_stats(m, mnt_path.dentry);
  202. }
  203. seq_putc(m, '\n');
  204. out:
  205. return err;
  206. }
  207. static int mounts_open_common(struct inode *inode, struct file *file,
  208. int (*show)(struct seq_file *, struct vfsmount *))
  209. {
  210. struct task_struct *task = get_proc_task(inode);
  211. struct nsproxy *nsp;
  212. struct mnt_namespace *ns = NULL;
  213. struct path root;
  214. struct proc_mounts *p;
  215. struct seq_file *m;
  216. int ret = -EINVAL;
  217. if (!task)
  218. goto err;
  219. task_lock(task);
  220. nsp = task->nsproxy;
  221. if (!nsp || !nsp->mnt_ns) {
  222. task_unlock(task);
  223. put_task_struct(task);
  224. goto err;
  225. }
  226. ns = nsp->mnt_ns;
  227. get_mnt_ns(ns);
  228. if (!task->fs) {
  229. task_unlock(task);
  230. put_task_struct(task);
  231. ret = -ENOENT;
  232. goto err_put_ns;
  233. }
  234. get_fs_root(task->fs, &root);
  235. task_unlock(task);
  236. put_task_struct(task);
  237. ret = seq_open_private(file, &mounts_op, sizeof(struct proc_mounts));
  238. if (ret)
  239. goto err_put_path;
  240. m = file->private_data;
  241. m->poll_event = ns->event;
  242. p = m->private;
  243. p->ns = ns;
  244. p->root = root;
  245. p->show = show;
  246. p->cached_event = ~0ULL;
  247. return 0;
  248. err_put_path:
  249. path_put(&root);
  250. err_put_ns:
  251. put_mnt_ns(ns);
  252. err:
  253. return ret;
  254. }
  255. static int mounts_release(struct inode *inode, struct file *file)
  256. {
  257. struct seq_file *m = file->private_data;
  258. struct proc_mounts *p = m->private;
  259. path_put(&p->root);
  260. put_mnt_ns(p->ns);
  261. return seq_release_private(inode, file);
  262. }
  263. static int mounts_open(struct inode *inode, struct file *file)
  264. {
  265. return mounts_open_common(inode, file, show_vfsmnt);
  266. }
  267. static int mountinfo_open(struct inode *inode, struct file *file)
  268. {
  269. return mounts_open_common(inode, file, show_mountinfo);
  270. }
  271. static int mountstats_open(struct inode *inode, struct file *file)
  272. {
  273. return mounts_open_common(inode, file, show_vfsstat);
  274. }
  275. const struct file_operations proc_mounts_operations = {
  276. .open = mounts_open,
  277. .read = seq_read,
  278. .llseek = seq_lseek,
  279. .release = mounts_release,
  280. .poll = mounts_poll,
  281. };
  282. const struct file_operations proc_mountinfo_operations = {
  283. .open = mountinfo_open,
  284. .read = seq_read,
  285. .llseek = seq_lseek,
  286. .release = mounts_release,
  287. .poll = mounts_poll,
  288. };
  289. const struct file_operations proc_mountstats_operations = {
  290. .open = mountstats_open,
  291. .read = seq_read,
  292. .llseek = seq_lseek,
  293. .release = mounts_release,
  294. };