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