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