proc_namespace.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. if (err)
  178. goto out;
  179. } else {
  180. if (r->mnt_devname) {
  181. seq_puts(m, "device ");
  182. mangle(m, r->mnt_devname);
  183. } else
  184. seq_puts(m, "no device");
  185. }
  186. /* mount point */
  187. seq_puts(m, " mounted on ");
  188. /* mountpoints outside of chroot jail will give SEQ_SKIP on this */
  189. err = seq_path_root(m, &mnt_path, &p->root, " \t\n\\");
  190. if (err)
  191. goto out;
  192. seq_putc(m, ' ');
  193. /* file system type */
  194. seq_puts(m, "with fstype ");
  195. show_type(m, sb);
  196. /* optional statistics */
  197. if (sb->s_op->show_stats) {
  198. seq_putc(m, ' ');
  199. if (!err)
  200. err = sb->s_op->show_stats(m, mnt_path.dentry);
  201. }
  202. seq_putc(m, '\n');
  203. out:
  204. return err;
  205. }
  206. static int mounts_open_common(struct inode *inode, struct file *file,
  207. int (*show)(struct seq_file *, struct vfsmount *))
  208. {
  209. struct task_struct *task = get_proc_task(inode);
  210. struct nsproxy *nsp;
  211. struct mnt_namespace *ns = NULL;
  212. struct path root;
  213. struct proc_mounts *p;
  214. struct seq_file *m;
  215. int ret = -EINVAL;
  216. if (!task)
  217. goto err;
  218. task_lock(task);
  219. nsp = task->nsproxy;
  220. if (!nsp || !nsp->mnt_ns) {
  221. task_unlock(task);
  222. put_task_struct(task);
  223. goto err;
  224. }
  225. ns = nsp->mnt_ns;
  226. get_mnt_ns(ns);
  227. if (!task->fs) {
  228. task_unlock(task);
  229. put_task_struct(task);
  230. ret = -ENOENT;
  231. goto err_put_ns;
  232. }
  233. get_fs_root(task->fs, &root);
  234. task_unlock(task);
  235. put_task_struct(task);
  236. ret = seq_open_private(file, &mounts_op, sizeof(struct proc_mounts));
  237. if (ret)
  238. goto err_put_path;
  239. m = file->private_data;
  240. m->poll_event = ns->event;
  241. p = m->private;
  242. p->ns = ns;
  243. p->root = root;
  244. p->show = show;
  245. p->cached_event = ~0ULL;
  246. return 0;
  247. err_put_path:
  248. path_put(&root);
  249. err_put_ns:
  250. put_mnt_ns(ns);
  251. err:
  252. return ret;
  253. }
  254. static int mounts_release(struct inode *inode, struct file *file)
  255. {
  256. struct seq_file *m = file->private_data;
  257. struct proc_mounts *p = m->private;
  258. path_put(&p->root);
  259. put_mnt_ns(p->ns);
  260. return seq_release_private(inode, file);
  261. }
  262. static int mounts_open(struct inode *inode, struct file *file)
  263. {
  264. return mounts_open_common(inode, file, show_vfsmnt);
  265. }
  266. static int mountinfo_open(struct inode *inode, struct file *file)
  267. {
  268. return mounts_open_common(inode, file, show_mountinfo);
  269. }
  270. static int mountstats_open(struct inode *inode, struct file *file)
  271. {
  272. return mounts_open_common(inode, file, show_vfsstat);
  273. }
  274. const struct file_operations proc_mounts_operations = {
  275. .open = mounts_open,
  276. .read = seq_read,
  277. .llseek = seq_lseek,
  278. .release = mounts_release,
  279. .poll = mounts_poll,
  280. };
  281. const struct file_operations proc_mountinfo_operations = {
  282. .open = mountinfo_open,
  283. .read = seq_read,
  284. .llseek = seq_lseek,
  285. .release = mounts_release,
  286. .poll = mounts_poll,
  287. };
  288. const struct file_operations proc_mountstats_operations = {
  289. .open = mountstats_open,
  290. .read = seq_read,
  291. .llseek = seq_lseek,
  292. .release = mounts_release,
  293. };