proc_namespace.c 7.5 KB

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