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