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