Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / fs / namespace.c
1 /*
2  *  linux/fs/namespace.c
3  *
4  * (C) Copyright Al Viro 2000, 2001
5  *      Released under GPL v2.
6  *
7  * Based on code from fs/super.c, copyright Linus Torvalds and others.
8  * Heavily rewritten.
9  */
10
11 #include <linux/syscalls.h>
12 #include <linux/slab.h>
13 #include <linux/sched.h>
14 #include <linux/smp_lock.h>
15 #include <linux/init.h>
16 #include <linux/quotaops.h>
17 #include <linux/acct.h>
18 #include <linux/capability.h>
19 #include <linux/module.h>
20 #include <linux/seq_file.h>
21 #include <linux/namespace.h>
22 #include <linux/namei.h>
23 #include <linux/security.h>
24 #include <linux/mount.h>
25 #include <linux/vserver/namespace.h>
26 #include <linux/vserver/xid.h>
27 #include <asm/uaccess.h>
28 #include <asm/unistd.h>
29 #include "pnode.h"
30
31 extern int __init init_rootfs(void);
32
33 #ifdef CONFIG_SYSFS
34 extern int __init sysfs_init(void);
35 #else
36 static inline int sysfs_init(void)
37 {
38         return 0;
39 }
40 #endif
41
42 /* spinlock for vfsmount related operations, inplace of dcache_lock */
43 __cacheline_aligned_in_smp DEFINE_SPINLOCK(vfsmount_lock);
44
45 static int event;
46
47 static struct list_head *mount_hashtable __read_mostly;
48 static int hash_mask __read_mostly, hash_bits __read_mostly;
49 static kmem_cache_t *mnt_cache __read_mostly;
50 static struct rw_semaphore namespace_sem;
51
52 /* /sys/fs */
53 decl_subsys(fs, NULL, NULL);
54 EXPORT_SYMBOL_GPL(fs_subsys);
55
56 static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
57 {
58         unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
59         tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
60         tmp = tmp + (tmp >> hash_bits);
61         return tmp & hash_mask;
62 }
63
64 struct vfsmount *alloc_vfsmnt(const char *name)
65 {
66         struct vfsmount *mnt = kmem_cache_alloc(mnt_cache, GFP_KERNEL);
67         if (mnt) {
68                 memset(mnt, 0, sizeof(struct vfsmount));
69                 atomic_set(&mnt->mnt_count, 1);
70                 INIT_LIST_HEAD(&mnt->mnt_hash);
71                 INIT_LIST_HEAD(&mnt->mnt_child);
72                 INIT_LIST_HEAD(&mnt->mnt_mounts);
73                 INIT_LIST_HEAD(&mnt->mnt_list);
74                 INIT_LIST_HEAD(&mnt->mnt_expire);
75                 INIT_LIST_HEAD(&mnt->mnt_share);
76                 INIT_LIST_HEAD(&mnt->mnt_slave_list);
77                 INIT_LIST_HEAD(&mnt->mnt_slave);
78                 if (name) {
79                         int size = strlen(name) + 1;
80                         char *newname = kmalloc(size, GFP_KERNEL);
81                         if (newname) {
82                                 memcpy(newname, name, size);
83                                 mnt->mnt_devname = newname;
84                         }
85                 }
86         }
87         return mnt;
88 }
89
90 int simple_set_mnt(struct vfsmount *mnt, struct super_block *sb)
91 {
92         mnt->mnt_sb = sb;
93         mnt->mnt_root = dget(sb->s_root);
94         return 0;
95 }
96
97 EXPORT_SYMBOL(simple_set_mnt);
98
99 void free_vfsmnt(struct vfsmount *mnt)
100 {
101         kfree(mnt->mnt_devname);
102         kmem_cache_free(mnt_cache, mnt);
103 }
104
105 /*
106  * find the first or last mount at @dentry on vfsmount @mnt depending on
107  * @dir. If @dir is set return the first mount else return the last mount.
108  */
109 struct vfsmount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
110                               int dir)
111 {
112         struct list_head *head = mount_hashtable + hash(mnt, dentry);
113         struct list_head *tmp = head;
114         struct vfsmount *p, *found = NULL;
115
116         for (;;) {
117                 tmp = dir ? tmp->next : tmp->prev;
118                 p = NULL;
119                 if (tmp == head)
120                         break;
121                 p = list_entry(tmp, struct vfsmount, mnt_hash);
122                 if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
123                         found = p;
124                         break;
125                 }
126         }
127         return found;
128 }
129
130 /*
131  * lookup_mnt increments the ref count before returning
132  * the vfsmount struct.
133  */
134 struct vfsmount *lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
135 {
136         struct vfsmount *child_mnt;
137         spin_lock(&vfsmount_lock);
138         if ((child_mnt = __lookup_mnt(mnt, dentry, 1)))
139                 mntget(child_mnt);
140         spin_unlock(&vfsmount_lock);
141         return child_mnt;
142 }
143
144 static inline int check_mnt(struct vfsmount *mnt)
145 {
146         return mnt->mnt_namespace == current->namespace;
147 }
148
149 static void touch_namespace(struct namespace *ns)
150 {
151         if (ns) {
152                 ns->event = ++event;
153                 wake_up_interruptible(&ns->poll);
154         }
155 }
156
157 static void __touch_namespace(struct namespace *ns)
158 {
159         if (ns && ns->event != event) {
160                 ns->event = event;
161                 wake_up_interruptible(&ns->poll);
162         }
163 }
164
165 static void detach_mnt(struct vfsmount *mnt, struct nameidata *old_nd)
166 {
167         old_nd->dentry = mnt->mnt_mountpoint;
168         old_nd->mnt = mnt->mnt_parent;
169         mnt->mnt_parent = mnt;
170         mnt->mnt_mountpoint = mnt->mnt_root;
171         list_del_init(&mnt->mnt_child);
172         list_del_init(&mnt->mnt_hash);
173         old_nd->dentry->d_mounted--;
174 }
175
176 void mnt_set_mountpoint(struct vfsmount *mnt, struct dentry *dentry,
177                         struct vfsmount *child_mnt)
178 {
179         child_mnt->mnt_parent = mntget(mnt);
180         child_mnt->mnt_mountpoint = dget(dentry);
181         dentry->d_mounted++;
182 }
183
184 static void attach_mnt(struct vfsmount *mnt, struct nameidata *nd)
185 {
186         mnt_set_mountpoint(nd->mnt, nd->dentry, mnt);
187         list_add_tail(&mnt->mnt_hash, mount_hashtable +
188                         hash(nd->mnt, nd->dentry));
189         list_add_tail(&mnt->mnt_child, &nd->mnt->mnt_mounts);
190 }
191
192 /*
193  * the caller must hold vfsmount_lock
194  */
195 static void commit_tree(struct vfsmount *mnt)
196 {
197         struct vfsmount *parent = mnt->mnt_parent;
198         struct vfsmount *m;
199         LIST_HEAD(head);
200         struct namespace *n = parent->mnt_namespace;
201
202         BUG_ON(parent == mnt);
203
204         list_add_tail(&head, &mnt->mnt_list);
205         list_for_each_entry(m, &head, mnt_list)
206                 m->mnt_namespace = n;
207         list_splice(&head, n->list.prev);
208
209         list_add_tail(&mnt->mnt_hash, mount_hashtable +
210                                 hash(parent, mnt->mnt_mountpoint));
211         list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
212         touch_namespace(n);
213 }
214
215 static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
216 {
217         struct list_head *next = p->mnt_mounts.next;
218         if (next == &p->mnt_mounts) {
219                 while (1) {
220                         if (p == root)
221                                 return NULL;
222                         next = p->mnt_child.next;
223                         if (next != &p->mnt_parent->mnt_mounts)
224                                 break;
225                         p = p->mnt_parent;
226                 }
227         }
228         return list_entry(next, struct vfsmount, mnt_child);
229 }
230
231 static struct vfsmount *skip_mnt_tree(struct vfsmount *p)
232 {
233         struct list_head *prev = p->mnt_mounts.prev;
234         while (prev != &p->mnt_mounts) {
235                 p = list_entry(prev, struct vfsmount, mnt_child);
236                 prev = p->mnt_mounts.prev;
237         }
238         return p;
239 }
240
241 static struct vfsmount *clone_mnt(struct vfsmount *old, struct dentry *root,
242                                         int flag)
243 {
244         struct super_block *sb = old->mnt_sb;
245         struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
246
247         if (mnt) {
248                 mnt->mnt_flags = old->mnt_flags;
249                 atomic_inc(&sb->s_active);
250                 mnt->mnt_sb = sb;
251                 mnt->mnt_root = dget(root);
252                 mnt->mnt_mountpoint = mnt->mnt_root;
253                 mnt->mnt_parent = mnt;
254                 mnt->mnt_xid = old->mnt_xid;
255
256                 if (flag & CL_SLAVE) {
257                         list_add(&mnt->mnt_slave, &old->mnt_slave_list);
258                         mnt->mnt_master = old;
259                         CLEAR_MNT_SHARED(mnt);
260                 } else {
261                         if ((flag & CL_PROPAGATION) || IS_MNT_SHARED(old))
262                                 list_add(&mnt->mnt_share, &old->mnt_share);
263                         if (IS_MNT_SLAVE(old))
264                                 list_add(&mnt->mnt_slave, &old->mnt_slave);
265                         mnt->mnt_master = old->mnt_master;
266                 }
267                 if (flag & CL_MAKE_SHARED)
268                         set_mnt_shared(mnt);
269
270                 /* stick the duplicate mount on the same expiry list
271                  * as the original if that was on one */
272                 if (flag & CL_EXPIRE) {
273                         spin_lock(&vfsmount_lock);
274                         if (!list_empty(&old->mnt_expire))
275                                 list_add(&mnt->mnt_expire, &old->mnt_expire);
276                         spin_unlock(&vfsmount_lock);
277                 }
278         }
279         return mnt;
280 }
281
282 static inline void __mntput(struct vfsmount *mnt)
283 {
284         struct super_block *sb = mnt->mnt_sb;
285         dput(mnt->mnt_root);
286         free_vfsmnt(mnt);
287         deactivate_super(sb);
288 }
289
290 void mntput_no_expire(struct vfsmount *mnt)
291 {
292 repeat:
293         if (atomic_dec_and_lock(&mnt->mnt_count, &vfsmount_lock)) {
294                 if (likely(!mnt->mnt_pinned)) {
295                         spin_unlock(&vfsmount_lock);
296                         __mntput(mnt);
297                         return;
298                 }
299                 atomic_add(mnt->mnt_pinned + 1, &mnt->mnt_count);
300                 mnt->mnt_pinned = 0;
301                 spin_unlock(&vfsmount_lock);
302                 acct_auto_close_mnt(mnt);
303                 security_sb_umount_close(mnt);
304                 goto repeat;
305         }
306 }
307
308 EXPORT_SYMBOL(mntput_no_expire);
309
310 void mnt_pin(struct vfsmount *mnt)
311 {
312         spin_lock(&vfsmount_lock);
313         mnt->mnt_pinned++;
314         spin_unlock(&vfsmount_lock);
315 }
316
317 EXPORT_SYMBOL(mnt_pin);
318
319 void mnt_unpin(struct vfsmount *mnt)
320 {
321         spin_lock(&vfsmount_lock);
322         if (mnt->mnt_pinned) {
323                 atomic_inc(&mnt->mnt_count);
324                 mnt->mnt_pinned--;
325         }
326         spin_unlock(&vfsmount_lock);
327 }
328
329 EXPORT_SYMBOL(mnt_unpin);
330
331 /* iterator */
332 static void *m_start(struct seq_file *m, loff_t *pos)
333 {
334         struct namespace *n = m->private;
335         struct list_head *p;
336         loff_t l = *pos;
337
338         down_read(&namespace_sem);
339         list_for_each(p, &n->list)
340                 if (!l--)
341                         return list_entry(p, struct vfsmount, mnt_list);
342         return NULL;
343 }
344
345 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
346 {
347         struct namespace *n = m->private;
348         struct list_head *p = ((struct vfsmount *)v)->mnt_list.next;
349         (*pos)++;
350         return p == &n->list ? NULL : list_entry(p, struct vfsmount, mnt_list);
351 }
352
353 static void m_stop(struct seq_file *m, void *v)
354 {
355         up_read(&namespace_sem);
356 }
357
358 static inline void mangle(struct seq_file *m, const char *s)
359 {
360         seq_escape(m, s, " \t\n\\");
361 }
362
363 static int mnt_is_reachable(struct vfsmount *mnt)
364 {
365         struct vfsmount *root_mnt;
366         struct dentry *root, *point;
367         int ret;
368
369         if (mnt == mnt->mnt_namespace->root)
370                 return 1;
371
372         spin_lock(&dcache_lock);
373         root_mnt = current->fs->rootmnt;
374         root = current->fs->root;
375         point = root;
376
377         while ((mnt != mnt->mnt_parent) && (mnt != root_mnt)) {
378                 point = mnt->mnt_mountpoint;
379                 mnt = mnt->mnt_parent;
380         }
381
382         ret = (mnt == root_mnt) && is_subdir(point, root);
383
384         spin_unlock(&dcache_lock);
385
386         return ret;
387 }
388
389 static int show_vfsmnt(struct seq_file *m, void *v)
390 {
391         struct vfsmount *mnt = v;
392         int err = 0;
393         static struct proc_fs_info {
394                 int s_flag;
395                 int mnt_flag;
396                 char *set_str;
397                 char *unset_str;
398         } fs_info[] = {
399                 { MS_RDONLY, MNT_RDONLY, "ro", "rw" },
400                 { MS_SYNCHRONOUS, 0, ",sync", NULL },
401                 { MS_DIRSYNC, 0, ",dirsync", NULL },
402                 { MS_MANDLOCK, 0, ",mand", NULL },
403                 { MS_TAGXID, 0, ",tagxid", NULL },
404                 { MS_NOATIME, MNT_NOATIME, ",noatime", NULL },
405                 { MS_NODIRATIME, MNT_NODIRATIME, ",nodiratime", NULL },
406                 { 0, MNT_NOSUID, ",nosuid", NULL },
407                 { 0, MNT_NODEV, ",nodev", NULL },
408                 { 0, MNT_NOEXEC, ",noexec", NULL },
409                 { 0, 0, NULL, NULL }
410         };
411         struct proc_fs_info *p;
412         unsigned long s_flags = mnt->mnt_sb->s_flags;
413         int mnt_flags = mnt->mnt_flags;
414
415         if (vx_flags(VXF_HIDE_MOUNT, 0))
416                 return 0;
417         if (!mnt_is_reachable(mnt))
418                 return 0;
419
420         if (!vx_check(0, VX_ADMIN|VX_WATCH) &&
421                 mnt == current->fs->rootmnt) {
422                 seq_puts(m, "/dev/root / ");
423         } else {
424                 mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
425                 seq_putc(m, ' ');
426                 seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
427                 seq_putc(m, ' ');
428         }
429         mangle(m, mnt->mnt_sb->s_type->name);
430         seq_putc(m, ' ');
431         for (p = fs_info; (p->s_flag | p->mnt_flag) ; p++) {
432                 if ((s_flags & p->s_flag) || (mnt_flags & p->mnt_flag)) {
433                         if (p->set_str)
434                                 seq_puts(m, p->set_str);
435                 } else {
436                         if (p->unset_str)
437                                 seq_puts(m, p->unset_str);
438                 }
439         }
440         if (mnt->mnt_flags & MNT_XID)
441                 seq_printf(m, ",xid=%d", mnt->mnt_xid);
442         if (mnt->mnt_sb->s_op->show_options)
443                 err = mnt->mnt_sb->s_op->show_options(m, mnt);
444         seq_puts(m, " 0 0\n");
445         return err;
446 }
447
448 struct seq_operations mounts_op = {
449         .start  = m_start,
450         .next   = m_next,
451         .stop   = m_stop,
452         .show   = show_vfsmnt
453 };
454
455 static int show_vfsstat(struct seq_file *m, void *v)
456 {
457         struct vfsmount *mnt = v;
458         int err = 0;
459
460         if (vx_flags(VXF_HIDE_MOUNT, 0))
461                 return 0;
462         if (!mnt_is_reachable(mnt) && !vx_check(0, VX_WATCH))
463                 return 0;
464
465         if (!vx_check(0, VX_ADMIN|VX_WATCH) &&
466                 mnt == current->fs->rootmnt) {
467                 seq_puts(m, "device /dev/root mounted on / ");
468         } else {
469                 /* device */
470                 if (mnt->mnt_devname) {
471                         seq_puts(m, "device ");
472                         mangle(m, mnt->mnt_devname);
473                 } else
474                         seq_puts(m, "no device");
475
476                 /* mount point */
477                 seq_puts(m, " mounted on ");
478                 seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
479                 seq_putc(m, ' ');
480         }
481
482         /* file system type */
483         seq_puts(m, "with fstype ");
484         mangle(m, mnt->mnt_sb->s_type->name);
485
486         /* optional statistics */
487         if (mnt->mnt_sb->s_op->show_stats) {
488                 seq_putc(m, ' ');
489                 err = mnt->mnt_sb->s_op->show_stats(m, mnt);
490         }
491
492         seq_putc(m, '\n');
493         return err;
494 }
495
496 struct seq_operations mountstats_op = {
497         .start  = m_start,
498         .next   = m_next,
499         .stop   = m_stop,
500         .show   = show_vfsstat,
501 };
502
503 /**
504  * may_umount_tree - check if a mount tree is busy
505  * @mnt: root of mount tree
506  *
507  * This is called to check if a tree of mounts has any
508  * open files, pwds, chroots or sub mounts that are
509  * busy.
510  */
511 int may_umount_tree(struct vfsmount *mnt)
512 {
513         int actual_refs = 0;
514         int minimum_refs = 0;
515         struct vfsmount *p;
516
517         spin_lock(&vfsmount_lock);
518         for (p = mnt; p; p = next_mnt(p, mnt)) {
519                 actual_refs += atomic_read(&p->mnt_count);
520                 minimum_refs += 2;
521         }
522         spin_unlock(&vfsmount_lock);
523
524         if (actual_refs > minimum_refs)
525                 return 0;
526
527         return 1;
528 }
529
530 EXPORT_SYMBOL(may_umount_tree);
531
532 /**
533  * may_umount - check if a mount point is busy
534  * @mnt: root of mount
535  *
536  * This is called to check if a mount point has any
537  * open files, pwds, chroots or sub mounts. If the
538  * mount has sub mounts this will return busy
539  * regardless of whether the sub mounts are busy.
540  *
541  * Doesn't take quota and stuff into account. IOW, in some cases it will
542  * give false negatives. The main reason why it's here is that we need
543  * a non-destructive way to look for easily umountable filesystems.
544  */
545 int may_umount(struct vfsmount *mnt)
546 {
547         int ret = 1;
548         spin_lock(&vfsmount_lock);
549         if (propagate_mount_busy(mnt, 2))
550                 ret = 0;
551         spin_unlock(&vfsmount_lock);
552         return ret;
553 }
554
555 EXPORT_SYMBOL(may_umount);
556
557 void release_mounts(struct list_head *head)
558 {
559         struct vfsmount *mnt;
560         while (!list_empty(head)) {
561                 mnt = list_entry(head->next, struct vfsmount, mnt_hash);
562                 list_del_init(&mnt->mnt_hash);
563                 if (mnt->mnt_parent != mnt) {
564                         struct dentry *dentry;
565                         struct vfsmount *m;
566                         spin_lock(&vfsmount_lock);
567                         dentry = mnt->mnt_mountpoint;
568                         m = mnt->mnt_parent;
569                         mnt->mnt_mountpoint = mnt->mnt_root;
570                         mnt->mnt_parent = mnt;
571                         spin_unlock(&vfsmount_lock);
572                         dput(dentry);
573                         mntput(m);
574                 }
575                 mntput(mnt);
576         }
577 }
578
579 void umount_tree(struct vfsmount *mnt, int propagate, struct list_head *kill)
580 {
581         struct vfsmount *p;
582
583         for (p = mnt; p; p = next_mnt(p, mnt))
584                 list_move(&p->mnt_hash, kill);
585
586         if (propagate)
587                 propagate_umount(kill);
588
589         list_for_each_entry(p, kill, mnt_hash) {
590                 list_del_init(&p->mnt_expire);
591                 list_del_init(&p->mnt_list);
592                 __touch_namespace(p->mnt_namespace);
593                 p->mnt_namespace = NULL;
594                 list_del_init(&p->mnt_child);
595                 if (p->mnt_parent != p)
596                         p->mnt_mountpoint->d_mounted--;
597                 change_mnt_propagation(p, MS_PRIVATE);
598         }
599 }
600
601 static int do_umount(struct vfsmount *mnt, int flags)
602 {
603         struct super_block *sb = mnt->mnt_sb;
604         int retval;
605         LIST_HEAD(umount_list);
606
607         retval = security_sb_umount(mnt, flags);
608         if (retval)
609                 return retval;
610
611         /*
612          * Allow userspace to request a mountpoint be expired rather than
613          * unmounting unconditionally. Unmount only happens if:
614          *  (1) the mark is already set (the mark is cleared by mntput())
615          *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
616          */
617         if (flags & MNT_EXPIRE) {
618                 if (mnt == current->fs->rootmnt ||
619                     flags & (MNT_FORCE | MNT_DETACH))
620                         return -EINVAL;
621
622                 if (atomic_read(&mnt->mnt_count) != 2)
623                         return -EBUSY;
624
625                 if (!xchg(&mnt->mnt_expiry_mark, 1))
626                         return -EAGAIN;
627         }
628
629         /*
630          * If we may have to abort operations to get out of this
631          * mount, and they will themselves hold resources we must
632          * allow the fs to do things. In the Unix tradition of
633          * 'Gee thats tricky lets do it in userspace' the umount_begin
634          * might fail to complete on the first run through as other tasks
635          * must return, and the like. Thats for the mount program to worry
636          * about for the moment.
637          */
638
639         lock_kernel();
640         if (sb->s_op->umount_begin)
641                 sb->s_op->umount_begin(mnt, flags);
642         unlock_kernel();
643
644         /*
645          * No sense to grab the lock for this test, but test itself looks
646          * somewhat bogus. Suggestions for better replacement?
647          * Ho-hum... In principle, we might treat that as umount + switch
648          * to rootfs. GC would eventually take care of the old vfsmount.
649          * Actually it makes sense, especially if rootfs would contain a
650          * /reboot - static binary that would close all descriptors and
651          * call reboot(9). Then init(8) could umount root and exec /reboot.
652          */
653         if (mnt == current->fs->rootmnt && !(flags & MNT_DETACH)) {
654                 /*
655                  * Special case for "unmounting" root ...
656                  * we just try to remount it readonly.
657                  */
658                 down_write(&sb->s_umount);
659                 if (!(sb->s_flags & MS_RDONLY)) {
660                         lock_kernel();
661                         DQUOT_OFF(sb);
662                         retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
663                         unlock_kernel();
664                 }
665                 up_write(&sb->s_umount);
666                 return retval;
667         }
668
669         down_write(&namespace_sem);
670         spin_lock(&vfsmount_lock);
671         event++;
672
673         retval = -EBUSY;
674         if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
675                 if (!list_empty(&mnt->mnt_list))
676                         umount_tree(mnt, 1, &umount_list);
677                 retval = 0;
678         }
679         spin_unlock(&vfsmount_lock);
680         if (retval)
681                 security_sb_umount_busy(mnt);
682         up_write(&namespace_sem);
683         release_mounts(&umount_list);
684         return retval;
685 }
686
687 /*
688  * Now umount can handle mount points as well as block devices.
689  * This is important for filesystems which use unnamed block devices.
690  *
691  * We now support a flag for forced unmount like the other 'big iron'
692  * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
693  */
694
695 asmlinkage long sys_umount(char __user * name, int flags)
696 {
697         struct nameidata nd;
698         int retval;
699
700         retval = __user_walk(name, LOOKUP_FOLLOW, &nd);
701         if (retval)
702                 goto out;
703         retval = -EINVAL;
704         if (nd.dentry != nd.mnt->mnt_root)
705                 goto dput_and_out;
706         if (!check_mnt(nd.mnt))
707                 goto dput_and_out;
708
709         retval = -EPERM;
710         if (!vx_capable(CAP_SYS_ADMIN, VXC_SECURE_MOUNT))
711                 goto dput_and_out;
712
713         retval = do_umount(nd.mnt, flags);
714 dput_and_out:
715         path_release_on_umount(&nd);
716 out:
717         return retval;
718 }
719
720 #ifdef __ARCH_WANT_SYS_OLDUMOUNT
721
722 /*
723  *      The 2.0 compatible umount. No flags.
724  */
725 asmlinkage long sys_oldumount(char __user * name)
726 {
727         return sys_umount(name, 0);
728 }
729
730 #endif
731
732 static int mount_is_safe(struct nameidata *nd)
733 {
734         if (vx_capable(CAP_SYS_ADMIN, VXC_SECURE_MOUNT))
735                 return 0;
736         return -EPERM;
737 #ifdef notyet
738         if (S_ISLNK(nd->dentry->d_inode->i_mode))
739                 return -EPERM;
740         if (nd->dentry->d_inode->i_mode & S_ISVTX) {
741                 if (current->uid != nd->dentry->d_inode->i_uid)
742                         return -EPERM;
743         }
744         if (vfs_permission(nd, MAY_WRITE))
745                 return -EPERM;
746         return 0;
747 #endif
748 }
749
750 static int lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
751 {
752         while (1) {
753                 if (d == dentry)
754                         return 1;
755                 if (d == NULL || d == d->d_parent)
756                         return 0;
757                 d = d->d_parent;
758         }
759 }
760
761 struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry,
762                                         int flag)
763 {
764         struct vfsmount *res, *p, *q, *r, *s;
765         struct nameidata nd;
766
767         if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
768                 return NULL;
769
770         res = q = clone_mnt(mnt, dentry, flag);
771         if (!q)
772                 goto Enomem;
773         q->mnt_mountpoint = mnt->mnt_mountpoint;
774
775         p = mnt;
776         list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
777                 if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
778                         continue;
779
780                 for (s = r; s; s = next_mnt(s, r)) {
781                         if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) {
782                                 s = skip_mnt_tree(s);
783                                 continue;
784                         }
785                         while (p != s->mnt_parent) {
786                                 p = p->mnt_parent;
787                                 q = q->mnt_parent;
788                         }
789                         p = s;
790                         nd.mnt = q;
791                         nd.dentry = p->mnt_mountpoint;
792                         q = clone_mnt(p, p->mnt_root, flag);
793                         if (!q)
794                                 goto Enomem;
795                         spin_lock(&vfsmount_lock);
796                         list_add_tail(&q->mnt_list, &res->mnt_list);
797                         attach_mnt(q, &nd);
798                         spin_unlock(&vfsmount_lock);
799                 }
800         }
801         return res;
802 Enomem:
803         if (res) {
804                 LIST_HEAD(umount_list);
805                 spin_lock(&vfsmount_lock);
806                 umount_tree(res, 0, &umount_list);
807                 spin_unlock(&vfsmount_lock);
808                 release_mounts(&umount_list);
809         }
810         return NULL;
811 }
812
813 /*
814  *  @source_mnt : mount tree to be attached
815  *  @nd         : place the mount tree @source_mnt is attached
816  *  @parent_nd  : if non-null, detach the source_mnt from its parent and
817  *                 store the parent mount and mountpoint dentry.
818  *                 (done when source_mnt is moved)
819  *
820  *  NOTE: in the table below explains the semantics when a source mount
821  *  of a given type is attached to a destination mount of a given type.
822  * ---------------------------------------------------------------------------
823  * |         BIND MOUNT OPERATION                                            |
824  * |**************************************************************************
825  * | source-->| shared        |       private  |       slave    | unbindable |
826  * | dest     |               |                |                |            |
827  * |   |      |               |                |                |            |
828  * |   v      |               |                |                |            |
829  * |**************************************************************************
830  * |  shared  | shared (++)   |     shared (+) |     shared(+++)|  invalid   |
831  * |          |               |                |                |            |
832  * |non-shared| shared (+)    |      private   |      slave (*) |  invalid   |
833  * ***************************************************************************
834  * A bind operation clones the source mount and mounts the clone on the
835  * destination mount.
836  *
837  * (++)  the cloned mount is propagated to all the mounts in the propagation
838  *       tree of the destination mount and the cloned mount is added to
839  *       the peer group of the source mount.
840  * (+)   the cloned mount is created under the destination mount and is marked
841  *       as shared. The cloned mount is added to the peer group of the source
842  *       mount.
843  * (+++) the mount is propagated to all the mounts in the propagation tree
844  *       of the destination mount and the cloned mount is made slave
845  *       of the same master as that of the source mount. The cloned mount
846  *       is marked as 'shared and slave'.
847  * (*)   the cloned mount is made a slave of the same master as that of the
848  *       source mount.
849  *
850  * ---------------------------------------------------------------------------
851  * |                    MOVE MOUNT OPERATION                                 |
852  * |**************************************************************************
853  * | source-->| shared        |       private  |       slave    | unbindable |
854  * | dest     |               |                |                |            |
855  * |   |      |               |                |                |            |
856  * |   v      |               |                |                |            |
857  * |**************************************************************************
858  * |  shared  | shared (+)    |     shared (+) |    shared(+++) |  invalid   |
859  * |          |               |                |                |            |
860  * |non-shared| shared (+*)   |      private   |    slave (*)   | unbindable |
861  * ***************************************************************************
862  *
863  * (+)  the mount is moved to the destination. And is then propagated to
864  *      all the mounts in the propagation tree of the destination mount.
865  * (+*)  the mount is moved to the destination.
866  * (+++)  the mount is moved to the destination and is then propagated to
867  *      all the mounts belonging to the destination mount's propagation tree.
868  *      the mount is marked as 'shared and slave'.
869  * (*)  the mount continues to be a slave at the new location.
870  *
871  * if the source mount is a tree, the operations explained above is
872  * applied to each mount in the tree.
873  * Must be called without spinlocks held, since this function can sleep
874  * in allocations.
875  */
876 static int attach_recursive_mnt(struct vfsmount *source_mnt,
877                         struct nameidata *nd, struct nameidata *parent_nd)
878 {
879         LIST_HEAD(tree_list);
880         struct vfsmount *dest_mnt = nd->mnt;
881         struct dentry *dest_dentry = nd->dentry;
882         struct vfsmount *child, *p;
883
884         if (propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list))
885                 return -EINVAL;
886
887         if (IS_MNT_SHARED(dest_mnt)) {
888                 for (p = source_mnt; p; p = next_mnt(p, source_mnt))
889                         set_mnt_shared(p);
890         }
891
892         spin_lock(&vfsmount_lock);
893         if (parent_nd) {
894                 detach_mnt(source_mnt, parent_nd);
895                 attach_mnt(source_mnt, nd);
896                 touch_namespace(current->namespace);
897         } else {
898                 mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);
899                 commit_tree(source_mnt);
900         }
901
902         list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
903                 list_del_init(&child->mnt_hash);
904                 commit_tree(child);
905         }
906         spin_unlock(&vfsmount_lock);
907         return 0;
908 }
909
910 static int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
911 {
912         int err;
913         if (mnt->mnt_sb->s_flags & MS_NOUSER)
914                 return -EINVAL;
915
916         if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
917               S_ISDIR(mnt->mnt_root->d_inode->i_mode))
918                 return -ENOTDIR;
919
920         err = -ENOENT;
921         mutex_lock(&nd->dentry->d_inode->i_mutex);
922         if (IS_DEADDIR(nd->dentry->d_inode))
923                 goto out_unlock;
924
925         err = security_sb_check_sb(mnt, nd);
926         if (err)
927                 goto out_unlock;
928
929         err = -ENOENT;
930         if (IS_ROOT(nd->dentry) || !d_unhashed(nd->dentry))
931                 err = attach_recursive_mnt(mnt, nd, NULL);
932 out_unlock:
933         mutex_unlock(&nd->dentry->d_inode->i_mutex);
934         if (!err)
935                 security_sb_post_addmount(mnt, nd);
936         return err;
937 }
938
939 /*
940  * recursively change the type of the mountpoint.
941  */
942 static int do_change_type(struct nameidata *nd, int flag)
943 {
944         struct vfsmount *m, *mnt = nd->mnt;
945         int recurse = flag & MS_REC;
946         int type = flag & ~MS_REC;
947
948         if (nd->dentry != nd->mnt->mnt_root)
949                 return -EINVAL;
950
951         down_write(&namespace_sem);
952         spin_lock(&vfsmount_lock);
953         for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
954                 change_mnt_propagation(m, type);
955         spin_unlock(&vfsmount_lock);
956         up_write(&namespace_sem);
957         return 0;
958 }
959
960 /*
961  * do loopback mount.
962  */
963 static int do_loopback(struct nameidata *nd, char *old_name, xid_t xid,
964         unsigned long flags, int mnt_flags)
965 {
966         struct nameidata old_nd;
967         struct vfsmount *mnt = NULL;
968         int err = mount_is_safe(nd);
969         int recurse = flags & MS_REC;
970         if (err)
971                 return err;
972         if (!old_name || !*old_name)
973                 return -EINVAL;
974         err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
975         if (err)
976                 return err;
977
978         down_write(&namespace_sem);
979         err = -EINVAL;
980         if (IS_MNT_UNBINDABLE(old_nd.mnt))
981                 goto out;
982
983         if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
984                 goto out;
985
986         err = -ENOMEM;
987         if (recurse)
988                 mnt = copy_tree(old_nd.mnt, old_nd.dentry, 0);
989         else
990                 mnt = clone_mnt(old_nd.mnt, old_nd.dentry, 0);
991
992         if (!mnt)
993                 goto out;
994
995         mnt->mnt_flags = mnt_flags;
996         if (flags & MS_XID) {
997                 mnt->mnt_xid = xid;
998                 mnt->mnt_flags |= MNT_XID;
999         }
1000
1001         err = graft_tree(mnt, nd);
1002         if (err) {
1003                 LIST_HEAD(umount_list);
1004                 spin_lock(&vfsmount_lock);
1005                 umount_tree(mnt, 0, &umount_list);
1006                 spin_unlock(&vfsmount_lock);
1007                 release_mounts(&umount_list);
1008         }
1009         mnt->mnt_flags = mnt_flags;
1010
1011 out:
1012         up_write(&namespace_sem);
1013         path_release(&old_nd);
1014         return err;
1015 }
1016
1017 /*
1018  * change filesystem flags. dir should be a physical root of filesystem.
1019  * If you've mounted a non-root directory somewhere and want to do remount
1020  * on it - tough luck.
1021  */
1022 static int do_remount(struct nameidata *nd, int flags, int mnt_flags,
1023                       void *data, xid_t xid)
1024 {
1025         int err;
1026         struct super_block *sb = nd->mnt->mnt_sb;
1027
1028         if (!vx_capable(CAP_SYS_ADMIN, VXC_SECURE_REMOUNT))
1029                 return -EPERM;
1030
1031         if (!check_mnt(nd->mnt))
1032                 return -EINVAL;
1033
1034         if (nd->dentry != nd->mnt->mnt_root)
1035                 return -EINVAL;
1036
1037         down_write(&sb->s_umount);
1038         err = do_remount_sb(sb, flags, data, 0);
1039         if (!err)
1040                 nd->mnt->mnt_flags = mnt_flags;
1041         up_write(&sb->s_umount);
1042         if (!err)
1043                 security_sb_post_remount(nd->mnt, flags, data);
1044         return err;
1045 }
1046
1047 static inline int tree_contains_unbindable(struct vfsmount *mnt)
1048 {
1049         struct vfsmount *p;
1050         for (p = mnt; p; p = next_mnt(p, mnt)) {
1051                 if (IS_MNT_UNBINDABLE(p))
1052                         return 1;
1053         }
1054         return 0;
1055 }
1056
1057 static int do_move_mount(struct nameidata *nd, char *old_name)
1058 {
1059         struct nameidata old_nd, parent_nd;
1060         struct vfsmount *p;
1061         int err = 0;
1062         if (!vx_capable(CAP_SYS_ADMIN, VXC_SECURE_MOUNT))
1063                 return -EPERM;
1064         if (!old_name || !*old_name)
1065                 return -EINVAL;
1066         err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
1067         if (err)
1068                 return err;
1069
1070         down_write(&namespace_sem);
1071         while (d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
1072                 ;
1073         err = -EINVAL;
1074         if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
1075                 goto out;
1076
1077         err = -ENOENT;
1078         mutex_lock(&nd->dentry->d_inode->i_mutex);
1079         if (IS_DEADDIR(nd->dentry->d_inode))
1080                 goto out1;
1081
1082         if (!IS_ROOT(nd->dentry) && d_unhashed(nd->dentry))
1083                 goto out1;
1084
1085         err = -EINVAL;
1086         if (old_nd.dentry != old_nd.mnt->mnt_root)
1087                 goto out1;
1088
1089         if (old_nd.mnt == old_nd.mnt->mnt_parent)
1090                 goto out1;
1091
1092         if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
1093               S_ISDIR(old_nd.dentry->d_inode->i_mode))
1094                 goto out1;
1095         /*
1096          * Don't move a mount residing in a shared parent.
1097          */
1098         if (old_nd.mnt->mnt_parent && IS_MNT_SHARED(old_nd.mnt->mnt_parent))
1099                 goto out1;
1100         /*
1101          * Don't move a mount tree containing unbindable mounts to a destination
1102          * mount which is shared.
1103          */
1104         if (IS_MNT_SHARED(nd->mnt) && tree_contains_unbindable(old_nd.mnt))
1105                 goto out1;
1106         err = -ELOOP;
1107         for (p = nd->mnt; p->mnt_parent != p; p = p->mnt_parent)
1108                 if (p == old_nd.mnt)
1109                         goto out1;
1110
1111         if ((err = attach_recursive_mnt(old_nd.mnt, nd, &parent_nd)))
1112                 goto out1;
1113
1114         spin_lock(&vfsmount_lock);
1115         /* if the mount is moved, it should no longer be expire
1116          * automatically */
1117         list_del_init(&old_nd.mnt->mnt_expire);
1118         spin_unlock(&vfsmount_lock);
1119 out1:
1120         mutex_unlock(&nd->dentry->d_inode->i_mutex);
1121 out:
1122         up_write(&namespace_sem);
1123         if (!err)
1124                 path_release(&parent_nd);
1125         path_release(&old_nd);
1126         return err;
1127 }
1128
1129 /*
1130  * create a new mount for userspace and request it to be added into the
1131  * namespace's tree
1132  */
1133 static int do_new_mount(struct nameidata *nd, char *type, int flags,
1134                         int mnt_flags, char *name, void *data)
1135 {
1136         struct vfsmount *mnt;
1137
1138         if (!type || !memchr(type, 0, PAGE_SIZE))
1139                 return -EINVAL;
1140
1141         /* we need capabilities... */
1142         if (!vx_capable(CAP_SYS_ADMIN, VXC_SECURE_MOUNT))
1143                 return -EPERM;
1144
1145         mnt = do_kern_mount(type, flags, name, data);
1146         if (IS_ERR(mnt))
1147                 return PTR_ERR(mnt);
1148
1149         return do_add_mount(mnt, nd, mnt_flags, NULL);
1150 }
1151
1152 /*
1153  * add a mount into a namespace's mount tree
1154  * - provide the option of adding the new mount to an expiration list
1155  */
1156 int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd,
1157                  int mnt_flags, struct list_head *fslist)
1158 {
1159         int err;
1160
1161         down_write(&namespace_sem);
1162         /* Something was mounted here while we slept */
1163         while (d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
1164                 ;
1165         err = -EINVAL;
1166         if (!check_mnt(nd->mnt))
1167                 goto unlock;
1168
1169         /* Refuse the same filesystem on the same mount point */
1170         err = -EBUSY;
1171         if (nd->mnt->mnt_sb == newmnt->mnt_sb &&
1172             nd->mnt->mnt_root == nd->dentry)
1173                 goto unlock;
1174
1175         err = -EINVAL;
1176         if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
1177                 goto unlock;
1178
1179         newmnt->mnt_flags = mnt_flags;
1180         if ((err = graft_tree(newmnt, nd)))
1181                 goto unlock;
1182
1183         if (fslist) {
1184                 /* add to the specified expiration list */
1185                 spin_lock(&vfsmount_lock);
1186                 list_add_tail(&newmnt->mnt_expire, fslist);
1187                 spin_unlock(&vfsmount_lock);
1188         }
1189         up_write(&namespace_sem);
1190         return 0;
1191
1192 unlock:
1193         up_write(&namespace_sem);
1194         mntput(newmnt);
1195         return err;
1196 }
1197
1198 EXPORT_SYMBOL_GPL(do_add_mount);
1199
1200 static void expire_mount(struct vfsmount *mnt, struct list_head *mounts,
1201                                 struct list_head *umounts)
1202 {
1203         spin_lock(&vfsmount_lock);
1204
1205         /*
1206          * Check if mount is still attached, if not, let whoever holds it deal
1207          * with the sucker
1208          */
1209         if (mnt->mnt_parent == mnt) {
1210                 spin_unlock(&vfsmount_lock);
1211                 return;
1212         }
1213
1214         /*
1215          * Check that it is still dead: the count should now be 2 - as
1216          * contributed by the vfsmount parent and the mntget above
1217          */
1218         if (!propagate_mount_busy(mnt, 2)) {
1219                 /* delete from the namespace */
1220                 touch_namespace(mnt->mnt_namespace);
1221                 list_del_init(&mnt->mnt_list);
1222                 mnt->mnt_namespace = NULL;
1223                 umount_tree(mnt, 1, umounts);
1224                 spin_unlock(&vfsmount_lock);
1225         } else {
1226                 /*
1227                  * Someone brought it back to life whilst we didn't have any
1228                  * locks held so return it to the expiration list
1229                  */
1230                 list_add_tail(&mnt->mnt_expire, mounts);
1231                 spin_unlock(&vfsmount_lock);
1232         }
1233 }
1234
1235 /*
1236  * go through the vfsmounts we've just consigned to the graveyard to
1237  * - check that they're still dead
1238  * - delete the vfsmount from the appropriate namespace under lock
1239  * - dispose of the corpse
1240  */
1241 static void expire_mount_list(struct list_head *graveyard, struct list_head *mounts)
1242 {
1243         struct namespace *namespace;
1244         struct vfsmount *mnt;
1245
1246         while (!list_empty(graveyard)) {
1247                 LIST_HEAD(umounts);
1248                 mnt = list_entry(graveyard->next, struct vfsmount, mnt_expire);
1249                 list_del_init(&mnt->mnt_expire);
1250
1251                 /* don't do anything if the namespace is dead - all the
1252                  * vfsmounts from it are going away anyway */
1253                 namespace = mnt->mnt_namespace;
1254                 if (!namespace || !namespace->root)
1255                         continue;
1256                 get_namespace(namespace);
1257
1258                 spin_unlock(&vfsmount_lock);
1259                 down_write(&namespace_sem);
1260                 expire_mount(mnt, mounts, &umounts);
1261                 up_write(&namespace_sem);
1262                 release_mounts(&umounts);
1263                 mntput(mnt);
1264                 put_namespace(namespace);
1265                 spin_lock(&vfsmount_lock);
1266         }
1267 }
1268
1269 /*
1270  * process a list of expirable mountpoints with the intent of discarding any
1271  * mountpoints that aren't in use and haven't been touched since last we came
1272  * here
1273  */
1274 void mark_mounts_for_expiry(struct list_head *mounts)
1275 {
1276         struct vfsmount *mnt, *next;
1277         LIST_HEAD(graveyard);
1278
1279         if (list_empty(mounts))
1280                 return;
1281
1282         spin_lock(&vfsmount_lock);
1283
1284         /* extract from the expiration list every vfsmount that matches the
1285          * following criteria:
1286          * - only referenced by its parent vfsmount
1287          * - still marked for expiry (marked on the last call here; marks are
1288          *   cleared by mntput())
1289          */
1290         list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
1291                 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
1292                     atomic_read(&mnt->mnt_count) != 1)
1293                         continue;
1294
1295                 mntget(mnt);
1296                 list_move(&mnt->mnt_expire, &graveyard);
1297         }
1298
1299         expire_mount_list(&graveyard, mounts);
1300
1301         spin_unlock(&vfsmount_lock);
1302 }
1303
1304 EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
1305
1306 /*
1307  * Ripoff of 'select_parent()'
1308  *
1309  * search the list of submounts for a given mountpoint, and move any
1310  * shrinkable submounts to the 'graveyard' list.
1311  */
1312 static int select_submounts(struct vfsmount *parent, struct list_head *graveyard)
1313 {
1314         struct vfsmount *this_parent = parent;
1315         struct list_head *next;
1316         int found = 0;
1317
1318 repeat:
1319         next = this_parent->mnt_mounts.next;
1320 resume:
1321         while (next != &this_parent->mnt_mounts) {
1322                 struct list_head *tmp = next;
1323                 struct vfsmount *mnt = list_entry(tmp, struct vfsmount, mnt_child);
1324
1325                 next = tmp->next;
1326                 if (!(mnt->mnt_flags & MNT_SHRINKABLE))
1327                         continue;
1328                 /*
1329                  * Descend a level if the d_mounts list is non-empty.
1330                  */
1331                 if (!list_empty(&mnt->mnt_mounts)) {
1332                         this_parent = mnt;
1333                         goto repeat;
1334                 }
1335
1336                 if (!propagate_mount_busy(mnt, 1)) {
1337                         mntget(mnt);
1338                         list_move_tail(&mnt->mnt_expire, graveyard);
1339                         found++;
1340                 }
1341         }
1342         /*
1343          * All done at this level ... ascend and resume the search
1344          */
1345         if (this_parent != parent) {
1346                 next = this_parent->mnt_child.next;
1347                 this_parent = this_parent->mnt_parent;
1348                 goto resume;
1349         }
1350         return found;
1351 }
1352
1353 /*
1354  * process a list of expirable mountpoints with the intent of discarding any
1355  * submounts of a specific parent mountpoint
1356  */
1357 void shrink_submounts(struct vfsmount *mountpoint, struct list_head *mounts)
1358 {
1359         LIST_HEAD(graveyard);
1360         int found;
1361
1362         spin_lock(&vfsmount_lock);
1363
1364         /* extract submounts of 'mountpoint' from the expiration list */
1365         while ((found = select_submounts(mountpoint, &graveyard)) != 0)
1366                 expire_mount_list(&graveyard, mounts);
1367
1368         spin_unlock(&vfsmount_lock);
1369 }
1370
1371 EXPORT_SYMBOL_GPL(shrink_submounts);
1372
1373 /*
1374  * Some copy_from_user() implementations do not return the exact number of
1375  * bytes remaining to copy on a fault.  But copy_mount_options() requires that.
1376  * Note that this function differs from copy_from_user() in that it will oops
1377  * on bad values of `to', rather than returning a short copy.
1378  */
1379 static long exact_copy_from_user(void *to, const void __user * from,
1380                                  unsigned long n)
1381 {
1382         char *t = to;
1383         const char __user *f = from;
1384         char c;
1385
1386         if (!access_ok(VERIFY_READ, from, n))
1387                 return n;
1388
1389         while (n) {
1390                 if (__get_user(c, f)) {
1391                         memset(t, 0, n);
1392                         break;
1393                 }
1394                 *t++ = c;
1395                 f++;
1396                 n--;
1397         }
1398         return n;
1399 }
1400
1401 int copy_mount_options(const void __user * data, unsigned long *where)
1402 {
1403         int i;
1404         unsigned long page;
1405         unsigned long size;
1406
1407         *where = 0;
1408         if (!data)
1409                 return 0;
1410
1411         if (!(page = __get_free_page(GFP_KERNEL)))
1412                 return -ENOMEM;
1413
1414         /* We only care that *some* data at the address the user
1415          * gave us is valid.  Just in case, we'll zero
1416          * the remainder of the page.
1417          */
1418         /* copy_from_user cannot cross TASK_SIZE ! */
1419         size = TASK_SIZE - (unsigned long)data;
1420         if (size > PAGE_SIZE)
1421                 size = PAGE_SIZE;
1422
1423         i = size - exact_copy_from_user((void *)page, data, size);
1424         if (!i) {
1425                 free_page(page);
1426                 return -EFAULT;
1427         }
1428         if (i != PAGE_SIZE)
1429                 memset((char *)page + i, 0, PAGE_SIZE - i);
1430         *where = page;
1431         return 0;
1432 }
1433
1434 /*
1435  * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
1436  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
1437  *
1438  * data is a (void *) that can point to any structure up to
1439  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
1440  * information (or be NULL).
1441  *
1442  * Pre-0.97 versions of mount() didn't have a flags word.
1443  * When the flags word was introduced its top half was required
1444  * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
1445  * Therefore, if this magic number is present, it carries no information
1446  * and must be discarded.
1447  */
1448 long do_mount(char *dev_name, char *dir_name, char *type_page,
1449                   unsigned long flags, void *data_page)
1450 {
1451         struct nameidata nd;
1452         int retval = 0;
1453         int mnt_flags = 0;
1454         xid_t xid = 0;
1455
1456         /* Discard magic */
1457         if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
1458                 flags &= ~MS_MGC_MSK;
1459
1460         /* Basic sanity checks */
1461
1462         if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
1463                 return -EINVAL;
1464         if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
1465                 return -EINVAL;
1466
1467         if (data_page)
1468                 ((char *)data_page)[PAGE_SIZE - 1] = 0;
1469
1470         retval = vx_parse_xid(data_page, &xid, 1);
1471         if (retval) {
1472                 mnt_flags |= MNT_XID;
1473                 /* bind and re-mounts get xid flag */
1474                 if (flags & (MS_BIND|MS_REMOUNT))
1475                         flags |= MS_XID;
1476         }
1477
1478         /* Separate the per-mountpoint flags */
1479         if (flags & MS_RDONLY)
1480                 mnt_flags |= MNT_RDONLY;
1481         if (flags & MS_NOSUID)
1482                 mnt_flags |= MNT_NOSUID;
1483         if (flags & MS_NODEV)
1484                 mnt_flags |= MNT_NODEV;
1485         if (flags & MS_NOEXEC)
1486                 mnt_flags |= MNT_NOEXEC;
1487         if (flags & MS_NOATIME)
1488                 mnt_flags |= MNT_NOATIME;
1489         if (flags & MS_NODIRATIME)
1490                 mnt_flags |= MNT_NODIRATIME;
1491
1492         if (!capable(CAP_SYS_ADMIN))
1493                 mnt_flags |= MNT_NODEV;
1494         flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE |
1495                    MS_NOATIME | MS_NODIRATIME);
1496
1497         /* ... and get the mountpoint */
1498         retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
1499         if (retval)
1500                 return retval;
1501
1502         retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
1503         if (retval)
1504                 goto dput_out;
1505
1506         if (flags & MS_REMOUNT)
1507                 retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
1508                                     data_page, xid);
1509         else if (flags & MS_BIND)
1510                 retval = do_loopback(&nd, dev_name, xid, flags, mnt_flags);
1511         else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
1512                 retval = do_change_type(&nd, flags);
1513         else if (flags & MS_MOVE)
1514                 retval = do_move_mount(&nd, dev_name);
1515         else
1516                 retval = do_new_mount(&nd, type_page, flags, mnt_flags,
1517                                       dev_name, data_page);
1518 dput_out:
1519         path_release(&nd);
1520         return retval;
1521 }
1522
1523 /*
1524  * Allocate a new namespace structure and populate it with contents
1525  * copied from the namespace of the passed in task structure.
1526  */
1527 struct namespace *dup_namespace(struct task_struct *tsk, struct fs_struct *fs)
1528 {
1529         struct namespace *namespace = tsk->namespace;
1530         struct namespace *new_ns;
1531         struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
1532         struct vfsmount *p, *q;
1533
1534         new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL);
1535         if (!new_ns)
1536                 return NULL;
1537
1538         atomic_set(&new_ns->count, 1);
1539         INIT_LIST_HEAD(&new_ns->list);
1540         init_waitqueue_head(&new_ns->poll);
1541         new_ns->event = 0;
1542
1543         down_write(&namespace_sem);
1544         /* First pass: copy the tree topology */
1545         new_ns->root = copy_tree(namespace->root, namespace->root->mnt_root,
1546                                         CL_COPY_ALL | CL_EXPIRE);
1547         if (!new_ns->root) {
1548                 up_write(&namespace_sem);
1549                 kfree(new_ns);
1550                 return NULL;
1551         }
1552         spin_lock(&vfsmount_lock);
1553         list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
1554         spin_unlock(&vfsmount_lock);
1555
1556         /*
1557          * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
1558          * as belonging to new namespace.  We have already acquired a private
1559          * fs_struct, so tsk->fs->lock is not needed.
1560          */
1561         p = namespace->root;
1562         q = new_ns->root;
1563         while (p) {
1564                 q->mnt_namespace = new_ns;
1565                 if (fs) {
1566                         if (p == fs->rootmnt) {
1567                                 rootmnt = p;
1568                                 fs->rootmnt = mntget(q);
1569                         }
1570                         if (p == fs->pwdmnt) {
1571                                 pwdmnt = p;
1572                                 fs->pwdmnt = mntget(q);
1573                         }
1574                         if (p == fs->altrootmnt) {
1575                                 altrootmnt = p;
1576                                 fs->altrootmnt = mntget(q);
1577                         }
1578                 }
1579                 p = next_mnt(p, namespace->root);
1580                 q = next_mnt(q, new_ns->root);
1581         }
1582         up_write(&namespace_sem);
1583
1584         if (rootmnt)
1585                 mntput(rootmnt);
1586         if (pwdmnt)
1587                 mntput(pwdmnt);
1588         if (altrootmnt)
1589                 mntput(altrootmnt);
1590
1591         return new_ns;
1592 }
1593
1594 int copy_namespace(int flags, struct task_struct *tsk)
1595 {
1596         struct namespace *namespace = tsk->namespace;
1597         struct namespace *new_ns;
1598         int err = 0;
1599
1600         if (!namespace)
1601                 return 0;
1602
1603         get_namespace(namespace);
1604
1605         if (!(flags & CLONE_NEWNS))
1606                 return 0;
1607
1608         if (!vx_capable(CAP_SYS_ADMIN, VXC_SECURE_MOUNT)) {
1609                 err = -EPERM;
1610                 goto out;
1611         }
1612
1613         new_ns = dup_namespace(tsk, tsk->fs);
1614         if (!new_ns) {
1615                 err = -ENOMEM;
1616                 goto out;
1617         }
1618
1619         tsk->namespace = new_ns;
1620
1621 out:
1622         put_namespace(namespace);
1623         return err;
1624 }
1625
1626 asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
1627                           char __user * type, unsigned long flags,
1628                           void __user * data)
1629 {
1630         int retval;
1631         unsigned long data_page;
1632         unsigned long type_page;
1633         unsigned long dev_page;
1634         char *dir_page;
1635
1636         retval = copy_mount_options(type, &type_page);
1637         if (retval < 0)
1638                 return retval;
1639
1640         dir_page = getname(dir_name);
1641         retval = PTR_ERR(dir_page);
1642         if (IS_ERR(dir_page))
1643                 goto out1;
1644
1645         retval = copy_mount_options(dev_name, &dev_page);
1646         if (retval < 0)
1647                 goto out2;
1648
1649         retval = copy_mount_options(data, &data_page);
1650         if (retval < 0)
1651                 goto out3;
1652
1653         lock_kernel();
1654         retval = do_mount((char *)dev_page, dir_page, (char *)type_page,
1655                           flags, (void *)data_page);
1656         unlock_kernel();
1657         free_page(data_page);
1658
1659 out3:
1660         free_page(dev_page);
1661 out2:
1662         putname(dir_page);
1663 out1:
1664         free_page(type_page);
1665         return retval;
1666 }
1667
1668 /*
1669  * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
1670  * It can block. Requires the big lock held.
1671  */
1672 void set_fs_root(struct fs_struct *fs, struct vfsmount *mnt,
1673                  struct dentry *dentry)
1674 {
1675         struct dentry *old_root;
1676         struct vfsmount *old_rootmnt;
1677         write_lock(&fs->lock);
1678         old_root = fs->root;
1679         old_rootmnt = fs->rootmnt;
1680         fs->rootmnt = mntget(mnt);
1681         fs->root = dget(dentry);
1682         write_unlock(&fs->lock);
1683         if (old_root) {
1684                 dput(old_root);
1685                 mntput(old_rootmnt);
1686         }
1687 }
1688
1689 EXPORT_SYMBOL_GPL(set_fs_root);
1690
1691 /*
1692  * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
1693  * It can block. Requires the big lock held.
1694  */
1695 void set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
1696                 struct dentry *dentry)
1697 {
1698         struct dentry *old_pwd;
1699         struct vfsmount *old_pwdmnt;
1700
1701         write_lock(&fs->lock);
1702         old_pwd = fs->pwd;
1703         old_pwdmnt = fs->pwdmnt;
1704         fs->pwdmnt = mntget(mnt);
1705         fs->pwd = dget(dentry);
1706         write_unlock(&fs->lock);
1707
1708         if (old_pwd) {
1709                 dput(old_pwd);
1710                 mntput(old_pwdmnt);
1711         }
1712 }
1713
1714 static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
1715 {
1716         struct task_struct *g, *p;
1717         struct fs_struct *fs;
1718
1719         read_lock(&tasklist_lock);
1720         do_each_thread(g, p) {
1721                 task_lock(p);
1722                 fs = p->fs;
1723                 if (fs) {
1724                         atomic_inc(&fs->count);
1725                         task_unlock(p);
1726                         if (fs->root == old_nd->dentry
1727                             && fs->rootmnt == old_nd->mnt)
1728                                 set_fs_root(fs, new_nd->mnt, new_nd->dentry);
1729                         if (fs->pwd == old_nd->dentry
1730                             && fs->pwdmnt == old_nd->mnt)
1731                                 set_fs_pwd(fs, new_nd->mnt, new_nd->dentry);
1732                         put_fs_struct(fs);
1733                 } else
1734                         task_unlock(p);
1735         } while_each_thread(g, p);
1736         read_unlock(&tasklist_lock);
1737 }
1738
1739 /*
1740  * pivot_root Semantics:
1741  * Moves the root file system of the current process to the directory put_old,
1742  * makes new_root as the new root file system of the current process, and sets
1743  * root/cwd of all processes which had them on the current root to new_root.
1744  *
1745  * Restrictions:
1746  * The new_root and put_old must be directories, and  must not be on the
1747  * same file  system as the current process root. The put_old  must  be
1748  * underneath new_root,  i.e. adding a non-zero number of /.. to the string
1749  * pointed to by put_old must yield the same directory as new_root. No other
1750  * file system may be mounted on put_old. After all, new_root is a mountpoint.
1751  *
1752  * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
1753  * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
1754  * in this situation.
1755  *
1756  * Notes:
1757  *  - we don't move root/cwd if they are not at the root (reason: if something
1758  *    cared enough to change them, it's probably wrong to force them elsewhere)
1759  *  - it's okay to pick a root that isn't the root of a file system, e.g.
1760  *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1761  *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1762  *    first.
1763  */
1764 asmlinkage long sys_pivot_root(const char __user * new_root,
1765                                const char __user * put_old)
1766 {
1767         struct vfsmount *tmp;
1768         struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd;
1769         int error;
1770
1771         if (!capable(CAP_SYS_ADMIN))
1772                 return -EPERM;
1773
1774         lock_kernel();
1775
1776         error = __user_walk(new_root, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
1777                             &new_nd);
1778         if (error)
1779                 goto out0;
1780         error = -EINVAL;
1781         if (!check_mnt(new_nd.mnt))
1782                 goto out1;
1783
1784         error = __user_walk(put_old, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old_nd);
1785         if (error)
1786                 goto out1;
1787
1788         error = security_sb_pivotroot(&old_nd, &new_nd);
1789         if (error) {
1790                 path_release(&old_nd);
1791                 goto out1;
1792         }
1793
1794         read_lock(&current->fs->lock);
1795         user_nd.mnt = mntget(current->fs->rootmnt);
1796         user_nd.dentry = dget(current->fs->root);
1797         read_unlock(&current->fs->lock);
1798         down_write(&namespace_sem);
1799         mutex_lock(&old_nd.dentry->d_inode->i_mutex);
1800         error = -EINVAL;
1801         if (IS_MNT_SHARED(old_nd.mnt) ||
1802                 IS_MNT_SHARED(new_nd.mnt->mnt_parent) ||
1803                 IS_MNT_SHARED(user_nd.mnt->mnt_parent))
1804                 goto out2;
1805         if (!check_mnt(user_nd.mnt))
1806                 goto out2;
1807         error = -ENOENT;
1808         if (IS_DEADDIR(new_nd.dentry->d_inode))
1809                 goto out2;
1810         if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
1811                 goto out2;
1812         if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
1813                 goto out2;
1814         error = -EBUSY;
1815         if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt)
1816                 goto out2; /* loop, on the same file system  */
1817         error = -EINVAL;
1818         if (user_nd.mnt->mnt_root != user_nd.dentry)
1819                 goto out2; /* not a mountpoint */
1820         if (user_nd.mnt->mnt_parent == user_nd.mnt)
1821                 goto out2; /* not attached */
1822         if (new_nd.mnt->mnt_root != new_nd.dentry)
1823                 goto out2; /* not a mountpoint */
1824         if (new_nd.mnt->mnt_parent == new_nd.mnt)
1825                 goto out2; /* not attached */
1826         tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
1827         spin_lock(&vfsmount_lock);
1828         if (tmp != new_nd.mnt) {
1829                 for (;;) {
1830                         if (tmp->mnt_parent == tmp)
1831                                 goto out3; /* already mounted on put_old */
1832                         if (tmp->mnt_parent == new_nd.mnt)
1833                                 break;
1834                         tmp = tmp->mnt_parent;
1835                 }
1836                 if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
1837                         goto out3;
1838         } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
1839                 goto out3;
1840         detach_mnt(new_nd.mnt, &parent_nd);
1841         detach_mnt(user_nd.mnt, &root_parent);
1842         attach_mnt(user_nd.mnt, &old_nd);     /* mount old root on put_old */
1843         attach_mnt(new_nd.mnt, &root_parent); /* mount new_root on / */
1844         touch_namespace(current->namespace);
1845         spin_unlock(&vfsmount_lock);
1846         chroot_fs_refs(&user_nd, &new_nd);
1847         security_sb_post_pivotroot(&user_nd, &new_nd);
1848         error = 0;
1849         path_release(&root_parent);
1850         path_release(&parent_nd);
1851 out2:
1852         mutex_unlock(&old_nd.dentry->d_inode->i_mutex);
1853         up_write(&namespace_sem);
1854         path_release(&user_nd);
1855         path_release(&old_nd);
1856 out1:
1857         path_release(&new_nd);
1858 out0:
1859         unlock_kernel();
1860         return error;
1861 out3:
1862         spin_unlock(&vfsmount_lock);
1863         goto out2;
1864 }
1865
1866 static void __init init_mount_tree(void)
1867 {
1868         struct vfsmount *mnt;
1869         struct namespace *namespace;
1870         struct task_struct *g, *p;
1871
1872         mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
1873         if (IS_ERR(mnt))
1874                 panic("Can't create rootfs");
1875         namespace = kmalloc(sizeof(*namespace), GFP_KERNEL);
1876         if (!namespace)
1877                 panic("Can't allocate initial namespace");
1878         atomic_set(&namespace->count, 1);
1879         INIT_LIST_HEAD(&namespace->list);
1880         init_waitqueue_head(&namespace->poll);
1881         namespace->event = 0;
1882         list_add(&mnt->mnt_list, &namespace->list);
1883         namespace->root = mnt;
1884         mnt->mnt_namespace = namespace;
1885
1886         init_task.namespace = namespace;
1887         read_lock(&tasklist_lock);
1888         do_each_thread(g, p) {
1889                 get_namespace(namespace);
1890                 p->namespace = namespace;
1891         } while_each_thread(g, p);
1892         read_unlock(&tasklist_lock);
1893
1894         set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root);
1895         set_fs_root(current->fs, namespace->root, namespace->root->mnt_root);
1896 }
1897
1898 void __init mnt_init(unsigned long mempages)
1899 {
1900         struct list_head *d;
1901         unsigned int nr_hash;
1902         int i;
1903
1904         init_rwsem(&namespace_sem);
1905
1906         mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
1907                         0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL, NULL);
1908
1909         mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
1910
1911         if (!mount_hashtable)
1912                 panic("Failed to allocate mount hash table\n");
1913
1914         /*
1915          * Find the power-of-two list-heads that can fit into the allocation..
1916          * We don't guarantee that "sizeof(struct list_head)" is necessarily
1917          * a power-of-two.
1918          */
1919         nr_hash = PAGE_SIZE / sizeof(struct list_head);
1920         hash_bits = 0;
1921         do {
1922                 hash_bits++;
1923         } while ((nr_hash >> hash_bits) != 0);
1924         hash_bits--;
1925
1926         /*
1927          * Re-calculate the actual number of entries and the mask
1928          * from the number of bits we can fit.
1929          */
1930         nr_hash = 1UL << hash_bits;
1931         hash_mask = nr_hash - 1;
1932
1933         printk("Mount-cache hash table entries: %d\n", nr_hash);
1934
1935         /* And initialize the newly allocated array */
1936         d = mount_hashtable;
1937         i = nr_hash;
1938         do {
1939                 INIT_LIST_HEAD(d);
1940                 d++;
1941                 i--;
1942         } while (i);
1943         sysfs_init();
1944         subsystem_register(&fs_subsys);
1945         init_rootfs();
1946         init_mount_tree();
1947 }
1948
1949 void __put_namespace(struct namespace *namespace)
1950 {
1951         struct vfsmount *root = namespace->root;
1952         LIST_HEAD(umount_list);
1953         namespace->root = NULL;
1954         spin_unlock(&vfsmount_lock);
1955         down_write(&namespace_sem);
1956         spin_lock(&vfsmount_lock);
1957         umount_tree(root, 0, &umount_list);
1958         spin_unlock(&vfsmount_lock);
1959         up_write(&namespace_sem);
1960         release_mounts(&umount_list);
1961         kfree(namespace);
1962 }