proc_namespace.c 7.7 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 proc_mounts *p = proc_mounts(file->private_data);
  19. struct mnt_namespace *ns = p->ns;
  20. unsigned res = POLLIN | POLLRDNORM;
  21. poll_wait(file, &p->ns->poll, wait);
  22. br_read_lock(&vfsmount_lock);
  23. if (p->m.poll_event != ns->event) {
  24. p->m.poll_event = ns->event;
  25. res |= POLLERR | POLLPRI;
  26. }
  27. br_read_unlock(&vfsmount_lock);
  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 mount *r = real_mount(mnt);
  81. int err = 0;
  82. struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
  83. struct super_block *sb = mnt_path.dentry->d_sb;
  84. if (sb->s_op->show_devname) {
  85. err = sb->s_op->show_devname(m, mnt_path.dentry);
  86. if (err)
  87. goto out;
  88. } else {
  89. mangle(m, r->mnt_devname ? r->mnt_devname : "none");
  90. }
  91. seq_putc(m, ' ');
  92. seq_path(m, &mnt_path, " \t\n\\");
  93. seq_putc(m, ' ');
  94. show_type(m, sb);
  95. seq_puts(m, __mnt_is_readonly(mnt) ? " ro" : " rw");
  96. err = show_sb_opts(m, sb);
  97. if (err)
  98. goto out;
  99. show_mnt_opts(m, mnt);
  100. if (sb->s_op->show_options2)
  101. err = sb->s_op->show_options2(mnt, m, mnt_path.dentry);
  102. else if (sb->s_op->show_options)
  103. err = sb->s_op->show_options(m, mnt_path.dentry);
  104. seq_puts(m, " 0 0\n");
  105. out:
  106. return err;
  107. }
  108. static int show_mountinfo(struct seq_file *m, struct vfsmount *mnt)
  109. {
  110. struct proc_mounts *p = proc_mounts(m);
  111. struct mount *r = real_mount(mnt);
  112. struct super_block *sb = mnt->mnt_sb;
  113. struct path mnt_path = { .dentry = mnt->mnt_root, .mnt = mnt };
  114. struct path root = p->root;
  115. int err = 0;
  116. seq_printf(m, "%i %i %u:%u ", r->mnt_id, r->mnt_parent->mnt_id,
  117. MAJOR(sb->s_dev), MINOR(sb->s_dev));
  118. if (sb->s_op->show_path)
  119. err = sb->s_op->show_path(m, mnt->mnt_root);
  120. else
  121. seq_dentry(m, mnt->mnt_root, " \t\n\\");
  122. if (err)
  123. goto out;
  124. seq_putc(m, ' ');
  125. /* mountpoints outside of chroot jail will give SEQ_SKIP on this */
  126. err = seq_path_root(m, &mnt_path, &root, " \t\n\\");
  127. if (err)
  128. goto out;
  129. seq_puts(m, mnt->mnt_flags & MNT_READONLY ? " ro" : " rw");
  130. show_mnt_opts(m, mnt);
  131. /* Tagged fields ("foo:X" or "bar") */
  132. if (IS_MNT_SHARED(r))
  133. seq_printf(m, " shared:%i", r->mnt_group_id);
  134. if (IS_MNT_SLAVE(r)) {
  135. int master = r->mnt_master->mnt_group_id;
  136. int dom = get_dominating_id(r, &p->root);
  137. seq_printf(m, " master:%i", master);
  138. if (dom && dom != master)
  139. seq_printf(m, " propagate_from:%i", dom);
  140. }
  141. if (IS_MNT_UNBINDABLE(r))
  142. seq_puts(m, " unbindable");
  143. /* Filesystem specific data */
  144. seq_puts(m, " - ");
  145. show_type(m, sb);
  146. seq_putc(m, ' ');
  147. if (sb->s_op->show_devname)
  148. err = sb->s_op->show_devname(m, mnt->mnt_root);
  149. else
  150. mangle(m, r->mnt_devname ? r->mnt_devname : "none");
  151. if (err)
  152. goto out;
  153. seq_puts(m, sb->s_flags & MS_RDONLY ? " ro" : " rw");
  154. err = show_sb_opts(m, sb);
  155. if (err)
  156. goto out;
  157. if (sb->s_op->show_options2) {
  158. err = sb->s_op->show_options2(mnt, m, mnt->mnt_root);
  159. } else 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 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. seq_path(m, &mnt_path, " \t\n\\");
  185. seq_putc(m, ' ');
  186. /* file system type */
  187. seq_puts(m, "with fstype ");
  188. show_type(m, sb);
  189. /* optional statistics */
  190. if (sb->s_op->show_stats) {
  191. seq_putc(m, ' ');
  192. if (!err)
  193. err = sb->s_op->show_stats(m, mnt_path.dentry);
  194. }
  195. seq_putc(m, '\n');
  196. return err;
  197. }
  198. static int mounts_open_common(struct inode *inode, struct file *file,
  199. int (*show)(struct seq_file *, struct vfsmount *))
  200. {
  201. struct task_struct *task = get_proc_task(inode);
  202. struct nsproxy *nsp;
  203. struct mnt_namespace *ns = NULL;
  204. struct path root;
  205. struct proc_mounts *p;
  206. int ret = -EINVAL;
  207. if (!task)
  208. goto err;
  209. rcu_read_lock();
  210. nsp = task_nsproxy(task);
  211. if (!nsp) {
  212. rcu_read_unlock();
  213. put_task_struct(task);
  214. goto err;
  215. }
  216. ns = nsp->mnt_ns;
  217. if (!ns) {
  218. rcu_read_unlock();
  219. put_task_struct(task);
  220. goto err;
  221. }
  222. get_mnt_ns(ns);
  223. rcu_read_unlock();
  224. task_lock(task);
  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 = -ENOMEM;
  235. p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
  236. if (!p)
  237. goto err_put_path;
  238. file->private_data = &p->m;
  239. ret = seq_open(file, &mounts_op);
  240. if (ret)
  241. goto err_free;
  242. p->ns = ns;
  243. p->root = root;
  244. p->m.poll_event = ns->event;
  245. p->show = show;
  246. return 0;
  247. err_free:
  248. kfree(p);
  249. err_put_path:
  250. path_put(&root);
  251. err_put_ns:
  252. put_mnt_ns(ns);
  253. err:
  254. return ret;
  255. }
  256. static int mounts_release(struct inode *inode, struct file *file)
  257. {
  258. struct proc_mounts *p = proc_mounts(file->private_data);
  259. path_put(&p->root);
  260. put_mnt_ns(p->ns);
  261. return seq_release(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. };