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