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