]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/namespace.c
[PATCH] make /proc/mounts pollable
[mirror_ubuntu-artful-kernel.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;
1da177e4
LT
46static kmem_cache_t *mnt_cache;
47
48static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
49{
50 unsigned long tmp = ((unsigned long) mnt / L1_CACHE_BYTES);
51 tmp += ((unsigned long) dentry / L1_CACHE_BYTES);
52 tmp = tmp + (tmp >> hash_bits);
53 return tmp & hash_mask;
54}
55
56struct vfsmount *alloc_vfsmnt(const char *name)
57{
58 struct vfsmount *mnt = kmem_cache_alloc(mnt_cache, GFP_KERNEL);
59 if (mnt) {
60 memset(mnt, 0, sizeof(struct vfsmount));
61 atomic_set(&mnt->mnt_count,1);
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
LT
67 if (name) {
68 int size = strlen(name)+1;
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{
91 struct list_head * head = mount_hashtable + hash(mnt, dentry);
92 struct list_head * tmp = head;
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);
147 list_add(&mnt->mnt_hash, mount_hashtable+hash(nd->mnt, nd->dentry));
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
168static struct vfsmount *
169clone_mnt(struct vfsmount *old, struct dentry *root)
170{
171 struct super_block *sb = old->mnt_sb;
172 struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
173
174 if (mnt) {
175 mnt->mnt_flags = old->mnt_flags;
176 atomic_inc(&sb->s_active);
177 mnt->mnt_sb = sb;
178 mnt->mnt_root = dget(root);
179 mnt->mnt_mountpoint = mnt->mnt_root;
180 mnt->mnt_parent = mnt;
68b47139 181 mnt->mnt_namespace = current->namespace;
1da177e4
LT
182
183 /* stick the duplicate mount on the same expiry list
184 * as the original if that was on one */
185 spin_lock(&vfsmount_lock);
55e700b9
MS
186 if (!list_empty(&old->mnt_expire))
187 list_add(&mnt->mnt_expire, &old->mnt_expire);
1da177e4
LT
188 spin_unlock(&vfsmount_lock);
189 }
190 return mnt;
191}
192
7b7b1ace 193static inline void __mntput(struct vfsmount *mnt)
1da177e4
LT
194{
195 struct super_block *sb = mnt->mnt_sb;
196 dput(mnt->mnt_root);
197 free_vfsmnt(mnt);
198 deactivate_super(sb);
199}
200
7b7b1ace
AV
201void mntput_no_expire(struct vfsmount *mnt)
202{
203repeat:
204 if (atomic_dec_and_lock(&mnt->mnt_count, &vfsmount_lock)) {
205 if (likely(!mnt->mnt_pinned)) {
206 spin_unlock(&vfsmount_lock);
207 __mntput(mnt);
208 return;
209 }
210 atomic_add(mnt->mnt_pinned + 1, &mnt->mnt_count);
211 mnt->mnt_pinned = 0;
212 spin_unlock(&vfsmount_lock);
213 acct_auto_close_mnt(mnt);
214 security_sb_umount_close(mnt);
215 goto repeat;
216 }
217}
218
219EXPORT_SYMBOL(mntput_no_expire);
220
221void mnt_pin(struct vfsmount *mnt)
222{
223 spin_lock(&vfsmount_lock);
224 mnt->mnt_pinned++;
225 spin_unlock(&vfsmount_lock);
226}
227
228EXPORT_SYMBOL(mnt_pin);
229
230void mnt_unpin(struct vfsmount *mnt)
231{
232 spin_lock(&vfsmount_lock);
233 if (mnt->mnt_pinned) {
234 atomic_inc(&mnt->mnt_count);
235 mnt->mnt_pinned--;
236 }
237 spin_unlock(&vfsmount_lock);
238}
239
240EXPORT_SYMBOL(mnt_unpin);
1da177e4
LT
241
242/* iterator */
243static void *m_start(struct seq_file *m, loff_t *pos)
244{
245 struct namespace *n = m->private;
246 struct list_head *p;
247 loff_t l = *pos;
248
249 down_read(&n->sem);
250 list_for_each(p, &n->list)
251 if (!l--)
252 return list_entry(p, struct vfsmount, mnt_list);
253 return NULL;
254}
255
256static void *m_next(struct seq_file *m, void *v, loff_t *pos)
257{
258 struct namespace *n = m->private;
259 struct list_head *p = ((struct vfsmount *)v)->mnt_list.next;
260 (*pos)++;
261 return p==&n->list ? NULL : list_entry(p, struct vfsmount, mnt_list);
262}
263
264static void m_stop(struct seq_file *m, void *v)
265{
266 struct namespace *n = m->private;
267 up_read(&n->sem);
268}
269
270static inline void mangle(struct seq_file *m, const char *s)
271{
272 seq_escape(m, s, " \t\n\\");
273}
274
275static int show_vfsmnt(struct seq_file *m, void *v)
276{
277 struct vfsmount *mnt = v;
278 int err = 0;
279 static struct proc_fs_info {
280 int flag;
281 char *str;
282 } fs_info[] = {
283 { MS_SYNCHRONOUS, ",sync" },
284 { MS_DIRSYNC, ",dirsync" },
285 { MS_MANDLOCK, ",mand" },
286 { MS_NOATIME, ",noatime" },
287 { MS_NODIRATIME, ",nodiratime" },
288 { 0, NULL }
289 };
290 static struct proc_fs_info mnt_info[] = {
291 { MNT_NOSUID, ",nosuid" },
292 { MNT_NODEV, ",nodev" },
293 { MNT_NOEXEC, ",noexec" },
294 { 0, NULL }
295 };
296 struct proc_fs_info *fs_infop;
297
298 mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
299 seq_putc(m, ' ');
300 seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
301 seq_putc(m, ' ');
302 mangle(m, mnt->mnt_sb->s_type->name);
303 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? " ro" : " rw");
304 for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
305 if (mnt->mnt_sb->s_flags & fs_infop->flag)
306 seq_puts(m, fs_infop->str);
307 }
308 for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
309 if (mnt->mnt_flags & fs_infop->flag)
310 seq_puts(m, fs_infop->str);
311 }
312 if (mnt->mnt_sb->s_op->show_options)
313 err = mnt->mnt_sb->s_op->show_options(m, mnt);
314 seq_puts(m, " 0 0\n");
315 return err;
316}
317
318struct seq_operations mounts_op = {
319 .start = m_start,
320 .next = m_next,
321 .stop = m_stop,
322 .show = show_vfsmnt
323};
324
325/**
326 * may_umount_tree - check if a mount tree is busy
327 * @mnt: root of mount tree
328 *
329 * This is called to check if a tree of mounts has any
330 * open files, pwds, chroots or sub mounts that are
331 * busy.
332 */
333int may_umount_tree(struct vfsmount *mnt)
334{
335 struct list_head *next;
336 struct vfsmount *this_parent = mnt;
337 int actual_refs;
338 int minimum_refs;
339
340 spin_lock(&vfsmount_lock);
341 actual_refs = atomic_read(&mnt->mnt_count);
342 minimum_refs = 2;
343repeat:
344 next = this_parent->mnt_mounts.next;
345resume:
346 while (next != &this_parent->mnt_mounts) {
347 struct vfsmount *p = list_entry(next, struct vfsmount, mnt_child);
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{
428 struct super_block * sb = mnt->mnt_sb;
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();
464 if( (flags&MNT_FORCE) && sb->s_op->umount_begin)
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/*
546 * The 2.0 compatible umount. No flags.
547 */
548
549asmlinkage long sys_oldumount(char __user * name)
550{
551 return sys_umount(name,0);
552}
553
554#endif
555
556static int mount_is_safe(struct nameidata *nd)
557{
558 if (capable(CAP_SYS_ADMIN))
559 return 0;
560 return -EPERM;
561#ifdef notyet
562 if (S_ISLNK(nd->dentry->d_inode->i_mode))
563 return -EPERM;
564 if (nd->dentry->d_inode->i_mode & S_ISVTX) {
565 if (current->uid != nd->dentry->d_inode->i_uid)
566 return -EPERM;
567 }
568 if (permission(nd->dentry->d_inode, MAY_WRITE, nd))
569 return -EPERM;
570 return 0;
571#endif
572}
573
574static int
575lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
576{
577 while (1) {
578 if (d == dentry)
579 return 1;
580 if (d == NULL || d == d->d_parent)
581 return 0;
582 d = d->d_parent;
583 }
584}
585
586static struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry)
587{
588 struct vfsmount *res, *p, *q, *r, *s;
1da177e4
LT
589 struct nameidata nd;
590
591 res = q = clone_mnt(mnt, dentry);
592 if (!q)
593 goto Enomem;
594 q->mnt_mountpoint = mnt->mnt_mountpoint;
595
596 p = mnt;
fdadd65f 597 list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
1da177e4
LT
598 if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
599 continue;
600
601 for (s = r; s; s = next_mnt(s, r)) {
602 while (p != s->mnt_parent) {
603 p = p->mnt_parent;
604 q = q->mnt_parent;
605 }
606 p = s;
607 nd.mnt = q;
608 nd.dentry = p->mnt_mountpoint;
609 q = clone_mnt(p, p->mnt_root);
610 if (!q)
611 goto Enomem;
612 spin_lock(&vfsmount_lock);
613 list_add_tail(&q->mnt_list, &res->mnt_list);
614 attach_mnt(q, &nd);
615 spin_unlock(&vfsmount_lock);
616 }
617 }
618 return res;
619 Enomem:
620 if (res) {
621 spin_lock(&vfsmount_lock);
622 umount_tree(res);
623 spin_unlock(&vfsmount_lock);
624 }
625 return NULL;
626}
627
628static int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
629{
630 int err;
631 if (mnt->mnt_sb->s_flags & MS_NOUSER)
632 return -EINVAL;
633
634 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
635 S_ISDIR(mnt->mnt_root->d_inode->i_mode))
636 return -ENOTDIR;
637
638 err = -ENOENT;
639 down(&nd->dentry->d_inode->i_sem);
640 if (IS_DEADDIR(nd->dentry->d_inode))
641 goto out_unlock;
642
643 err = security_sb_check_sb(mnt, nd);
644 if (err)
645 goto out_unlock;
646
647 err = -ENOENT;
648 spin_lock(&vfsmount_lock);
649 if (IS_ROOT(nd->dentry) || !d_unhashed(nd->dentry)) {
650 struct list_head head;
651
652 attach_mnt(mnt, nd);
653 list_add_tail(&head, &mnt->mnt_list);
654 list_splice(&head, current->namespace->list.prev);
655 mntget(mnt);
656 err = 0;
5addc5dd 657 touch_namespace(current->namespace);
1da177e4
LT
658 }
659 spin_unlock(&vfsmount_lock);
660out_unlock:
661 up(&nd->dentry->d_inode->i_sem);
662 if (!err)
663 security_sb_post_addmount(mnt, nd);
664 return err;
665}
666
667/*
668 * do loopback mount.
669 */
670static int do_loopback(struct nameidata *nd, char *old_name, int recurse)
671{
672 struct nameidata old_nd;
673 struct vfsmount *mnt = NULL;
674 int err = mount_is_safe(nd);
675 if (err)
676 return err;
677 if (!old_name || !*old_name)
678 return -EINVAL;
679 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
680 if (err)
681 return err;
682
683 down_write(&current->namespace->sem);
684 err = -EINVAL;
ccd48bc7
AV
685 if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
686 goto out;
1da177e4 687
ccd48bc7
AV
688 err = -ENOMEM;
689 if (recurse)
690 mnt = copy_tree(old_nd.mnt, old_nd.dentry);
691 else
692 mnt = clone_mnt(old_nd.mnt, old_nd.dentry);
693
694 if (!mnt)
695 goto out;
696
697 /* stop bind mounts from expiring */
698 spin_lock(&vfsmount_lock);
699 list_del_init(&mnt->mnt_expire);
700 spin_unlock(&vfsmount_lock);
701
702 err = graft_tree(mnt, nd);
703 if (err) {
1da177e4 704 spin_lock(&vfsmount_lock);
ccd48bc7 705 umount_tree(mnt);
1da177e4 706 spin_unlock(&vfsmount_lock);
ccd48bc7
AV
707 } else
708 mntput(mnt);
1da177e4 709
ccd48bc7 710out:
1da177e4
LT
711 up_write(&current->namespace->sem);
712 path_release(&old_nd);
713 return err;
714}
715
716/*
717 * change filesystem flags. dir should be a physical root of filesystem.
718 * If you've mounted a non-root directory somewhere and want to do remount
719 * on it - tough luck.
720 */
721
722static int do_remount(struct nameidata *nd, int flags, int mnt_flags,
723 void *data)
724{
725 int err;
726 struct super_block * sb = nd->mnt->mnt_sb;
727
728 if (!capable(CAP_SYS_ADMIN))
729 return -EPERM;
730
731 if (!check_mnt(nd->mnt))
732 return -EINVAL;
733
734 if (nd->dentry != nd->mnt->mnt_root)
735 return -EINVAL;
736
737 down_write(&sb->s_umount);
738 err = do_remount_sb(sb, flags, data, 0);
739 if (!err)
740 nd->mnt->mnt_flags=mnt_flags;
741 up_write(&sb->s_umount);
742 if (!err)
743 security_sb_post_remount(nd->mnt, flags, data);
744 return err;
745}
746
747static int do_move_mount(struct nameidata *nd, char *old_name)
748{
749 struct nameidata old_nd, parent_nd;
750 struct vfsmount *p;
751 int err = 0;
752 if (!capable(CAP_SYS_ADMIN))
753 return -EPERM;
754 if (!old_name || !*old_name)
755 return -EINVAL;
756 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
757 if (err)
758 return err;
759
760 down_write(&current->namespace->sem);
761 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
762 ;
763 err = -EINVAL;
764 if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
765 goto out;
766
767 err = -ENOENT;
768 down(&nd->dentry->d_inode->i_sem);
769 if (IS_DEADDIR(nd->dentry->d_inode))
770 goto out1;
771
772 spin_lock(&vfsmount_lock);
773 if (!IS_ROOT(nd->dentry) && d_unhashed(nd->dentry))
774 goto out2;
775
776 err = -EINVAL;
777 if (old_nd.dentry != old_nd.mnt->mnt_root)
778 goto out2;
779
780 if (old_nd.mnt == old_nd.mnt->mnt_parent)
781 goto out2;
782
783 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
784 S_ISDIR(old_nd.dentry->d_inode->i_mode))
785 goto out2;
786
787 err = -ELOOP;
788 for (p = nd->mnt; p->mnt_parent!=p; p = p->mnt_parent)
789 if (p == old_nd.mnt)
790 goto out2;
791 err = 0;
792
793 detach_mnt(old_nd.mnt, &parent_nd);
794 attach_mnt(old_nd.mnt, nd);
5addc5dd 795 touch_namespace(current->namespace);
1da177e4
LT
796
797 /* if the mount is moved, it should no longer be expire
798 * automatically */
55e700b9 799 list_del_init(&old_nd.mnt->mnt_expire);
1da177e4
LT
800out2:
801 spin_unlock(&vfsmount_lock);
802out1:
803 up(&nd->dentry->d_inode->i_sem);
804out:
805 up_write(&current->namespace->sem);
806 if (!err)
807 path_release(&parent_nd);
808 path_release(&old_nd);
809 return err;
810}
811
812/*
813 * create a new mount for userspace and request it to be added into the
814 * namespace's tree
815 */
816static int do_new_mount(struct nameidata *nd, char *type, int flags,
817 int mnt_flags, char *name, void *data)
818{
819 struct vfsmount *mnt;
820
821 if (!type || !memchr(type, 0, PAGE_SIZE))
822 return -EINVAL;
823
824 /* we need capabilities... */
825 if (!capable(CAP_SYS_ADMIN))
826 return -EPERM;
827
828 mnt = do_kern_mount(type, flags, name, data);
829 if (IS_ERR(mnt))
830 return PTR_ERR(mnt);
831
832 return do_add_mount(mnt, nd, mnt_flags, NULL);
833}
834
835/*
836 * add a mount into a namespace's mount tree
837 * - provide the option of adding the new mount to an expiration list
838 */
839int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd,
840 int mnt_flags, struct list_head *fslist)
841{
842 int err;
843
844 down_write(&current->namespace->sem);
845 /* Something was mounted here while we slept */
846 while(d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
847 ;
848 err = -EINVAL;
849 if (!check_mnt(nd->mnt))
850 goto unlock;
851
852 /* Refuse the same filesystem on the same mount point */
853 err = -EBUSY;
854 if (nd->mnt->mnt_sb == newmnt->mnt_sb &&
855 nd->mnt->mnt_root == nd->dentry)
856 goto unlock;
857
858 err = -EINVAL;
859 if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
860 goto unlock;
861
862 newmnt->mnt_flags = mnt_flags;
484e389c 863 newmnt->mnt_namespace = current->namespace;
1da177e4
LT
864 err = graft_tree(newmnt, nd);
865
866 if (err == 0 && fslist) {
867 /* add to the specified expiration list */
868 spin_lock(&vfsmount_lock);
55e700b9 869 list_add_tail(&newmnt->mnt_expire, fslist);
1da177e4
LT
870 spin_unlock(&vfsmount_lock);
871 }
872
873unlock:
874 up_write(&current->namespace->sem);
875 mntput(newmnt);
876 return err;
877}
878
879EXPORT_SYMBOL_GPL(do_add_mount);
880
24ca2af1
MS
881static void expire_mount(struct vfsmount *mnt, struct list_head *mounts)
882{
883 spin_lock(&vfsmount_lock);
884
ed42c879
MS
885 /*
886 * Check if mount is still attached, if not, let whoever holds it deal
887 * with the sucker
888 */
889 if (mnt->mnt_parent == mnt) {
890 spin_unlock(&vfsmount_lock);
891 return;
892 }
893
24ca2af1
MS
894 /*
895 * Check that it is still dead: the count should now be 2 - as
896 * contributed by the vfsmount parent and the mntget above
897 */
898 if (atomic_read(&mnt->mnt_count) == 2) {
899 struct nameidata old_nd;
900
901 /* delete from the namespace */
5addc5dd 902 touch_namespace(mnt->mnt_namespace);
24ca2af1 903 list_del_init(&mnt->mnt_list);
ac081153 904 mnt->mnt_namespace = NULL;
24ca2af1
MS
905 detach_mnt(mnt, &old_nd);
906 spin_unlock(&vfsmount_lock);
907 path_release(&old_nd);
24ca2af1
MS
908 mntput(mnt);
909 } else {
910 /*
911 * Someone brought it back to life whilst we didn't have any
912 * locks held so return it to the expiration list
913 */
55e700b9 914 list_add_tail(&mnt->mnt_expire, mounts);
24ca2af1
MS
915 spin_unlock(&vfsmount_lock);
916 }
917}
918
1da177e4
LT
919/*
920 * process a list of expirable mountpoints with the intent of discarding any
921 * mountpoints that aren't in use and haven't been touched since last we came
922 * here
923 */
924void mark_mounts_for_expiry(struct list_head *mounts)
925{
926 struct namespace *namespace;
927 struct vfsmount *mnt, *next;
928 LIST_HEAD(graveyard);
929
930 if (list_empty(mounts))
931 return;
932
933 spin_lock(&vfsmount_lock);
934
935 /* extract from the expiration list every vfsmount that matches the
936 * following criteria:
937 * - only referenced by its parent vfsmount
938 * - still marked for expiry (marked on the last call here; marks are
939 * cleared by mntput())
940 */
55e700b9 941 list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
1da177e4
LT
942 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
943 atomic_read(&mnt->mnt_count) != 1)
944 continue;
945
946 mntget(mnt);
55e700b9 947 list_move(&mnt->mnt_expire, &graveyard);
1da177e4
LT
948 }
949
950 /*
951 * go through the vfsmounts we've just consigned to the graveyard to
952 * - check that they're still dead
953 * - delete the vfsmount from the appropriate namespace under lock
954 * - dispose of the corpse
955 */
956 while (!list_empty(&graveyard)) {
55e700b9
MS
957 mnt = list_entry(graveyard.next, struct vfsmount, mnt_expire);
958 list_del_init(&mnt->mnt_expire);
1da177e4
LT
959
960 /* don't do anything if the namespace is dead - all the
961 * vfsmounts from it are going away anyway */
962 namespace = mnt->mnt_namespace;
1ce88cf4 963 if (!namespace || !namespace->root)
1da177e4
LT
964 continue;
965 get_namespace(namespace);
966
967 spin_unlock(&vfsmount_lock);
968 down_write(&namespace->sem);
24ca2af1 969 expire_mount(mnt, mounts);
1da177e4
LT
970 up_write(&namespace->sem);
971
972 mntput(mnt);
973 put_namespace(namespace);
974
975 spin_lock(&vfsmount_lock);
976 }
977
978 spin_unlock(&vfsmount_lock);
979}
980
981EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
982
983/*
984 * Some copy_from_user() implementations do not return the exact number of
985 * bytes remaining to copy on a fault. But copy_mount_options() requires that.
986 * Note that this function differs from copy_from_user() in that it will oops
987 * on bad values of `to', rather than returning a short copy.
988 */
989static long
990exact_copy_from_user(void *to, const void __user *from, unsigned long n)
991{
992 char *t = to;
993 const char __user *f = from;
994 char c;
995
996 if (!access_ok(VERIFY_READ, from, n))
997 return n;
998
999 while (n) {
1000 if (__get_user(c, f)) {
1001 memset(t, 0, n);
1002 break;
1003 }
1004 *t++ = c;
1005 f++;
1006 n--;
1007 }
1008 return n;
1009}
1010
1011int copy_mount_options(const void __user *data, unsigned long *where)
1012{
1013 int i;
1014 unsigned long page;
1015 unsigned long size;
1016
1017 *where = 0;
1018 if (!data)
1019 return 0;
1020
1021 if (!(page = __get_free_page(GFP_KERNEL)))
1022 return -ENOMEM;
1023
1024 /* We only care that *some* data at the address the user
1025 * gave us is valid. Just in case, we'll zero
1026 * the remainder of the page.
1027 */
1028 /* copy_from_user cannot cross TASK_SIZE ! */
1029 size = TASK_SIZE - (unsigned long)data;
1030 if (size > PAGE_SIZE)
1031 size = PAGE_SIZE;
1032
1033 i = size - exact_copy_from_user((void *)page, data, size);
1034 if (!i) {
1035 free_page(page);
1036 return -EFAULT;
1037 }
1038 if (i != PAGE_SIZE)
1039 memset((char *)page + i, 0, PAGE_SIZE - i);
1040 *where = page;
1041 return 0;
1042}
1043
1044/*
1045 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
1046 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
1047 *
1048 * data is a (void *) that can point to any structure up to
1049 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
1050 * information (or be NULL).
1051 *
1052 * Pre-0.97 versions of mount() didn't have a flags word.
1053 * When the flags word was introduced its top half was required
1054 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
1055 * Therefore, if this magic number is present, it carries no information
1056 * and must be discarded.
1057 */
1058long do_mount(char * dev_name, char * dir_name, char *type_page,
1059 unsigned long flags, void *data_page)
1060{
1061 struct nameidata nd;
1062 int retval = 0;
1063 int mnt_flags = 0;
1064
1065 /* Discard magic */
1066 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
1067 flags &= ~MS_MGC_MSK;
1068
1069 /* Basic sanity checks */
1070
1071 if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
1072 return -EINVAL;
1073 if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
1074 return -EINVAL;
1075
1076 if (data_page)
1077 ((char *)data_page)[PAGE_SIZE - 1] = 0;
1078
1079 /* Separate the per-mountpoint flags */
1080 if (flags & MS_NOSUID)
1081 mnt_flags |= MNT_NOSUID;
1082 if (flags & MS_NODEV)
1083 mnt_flags |= MNT_NODEV;
1084 if (flags & MS_NOEXEC)
1085 mnt_flags |= MNT_NOEXEC;
1086 flags &= ~(MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_ACTIVE);
1087
1088 /* ... and get the mountpoint */
1089 retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
1090 if (retval)
1091 return retval;
1092
1093 retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
1094 if (retval)
1095 goto dput_out;
1096
1097 if (flags & MS_REMOUNT)
1098 retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
1099 data_page);
1100 else if (flags & MS_BIND)
1101 retval = do_loopback(&nd, dev_name, flags & MS_REC);
1102 else if (flags & MS_MOVE)
1103 retval = do_move_mount(&nd, dev_name);
1104 else
1105 retval = do_new_mount(&nd, type_page, flags, mnt_flags,
1106 dev_name, data_page);
1107dput_out:
1108 path_release(&nd);
1109 return retval;
1110}
1111
1112int copy_namespace(int flags, struct task_struct *tsk)
1113{
1114 struct namespace *namespace = tsk->namespace;
1115 struct namespace *new_ns;
1116 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
1117 struct fs_struct *fs = tsk->fs;
1118 struct vfsmount *p, *q;
1119
1120 if (!namespace)
1121 return 0;
1122
1123 get_namespace(namespace);
1124
1125 if (!(flags & CLONE_NEWNS))
1126 return 0;
1127
1128 if (!capable(CAP_SYS_ADMIN)) {
1129 put_namespace(namespace);
1130 return -EPERM;
1131 }
1132
1133 new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL);
1134 if (!new_ns)
1135 goto out;
1136
1137 atomic_set(&new_ns->count, 1);
1138 init_rwsem(&new_ns->sem);
1139 INIT_LIST_HEAD(&new_ns->list);
5addc5dd
AV
1140 init_waitqueue_head(&new_ns->poll);
1141 new_ns->event = 0;
1da177e4
LT
1142
1143 down_write(&tsk->namespace->sem);
1144 /* First pass: copy the tree topology */
1145 new_ns->root = copy_tree(namespace->root, namespace->root->mnt_root);
1146 if (!new_ns->root) {
1147 up_write(&tsk->namespace->sem);
1148 kfree(new_ns);
1149 goto out;
1150 }
1151 spin_lock(&vfsmount_lock);
1152 list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
1153 spin_unlock(&vfsmount_lock);
1154
1155 /*
1156 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
1157 * as belonging to new namespace. We have already acquired a private
1158 * fs_struct, so tsk->fs->lock is not needed.
1159 */
1160 p = namespace->root;
1161 q = new_ns->root;
1162 while (p) {
1163 q->mnt_namespace = new_ns;
1164 if (fs) {
1165 if (p == fs->rootmnt) {
1166 rootmnt = p;
1167 fs->rootmnt = mntget(q);
1168 }
1169 if (p == fs->pwdmnt) {
1170 pwdmnt = p;
1171 fs->pwdmnt = mntget(q);
1172 }
1173 if (p == fs->altrootmnt) {
1174 altrootmnt = p;
1175 fs->altrootmnt = mntget(q);
1176 }
1177 }
1178 p = next_mnt(p, namespace->root);
1179 q = next_mnt(q, new_ns->root);
1180 }
1181 up_write(&tsk->namespace->sem);
1182
1183 tsk->namespace = new_ns;
1184
1185 if (rootmnt)
1186 mntput(rootmnt);
1187 if (pwdmnt)
1188 mntput(pwdmnt);
1189 if (altrootmnt)
1190 mntput(altrootmnt);
1191
1192 put_namespace(namespace);
1193 return 0;
1194
1195out:
1196 put_namespace(namespace);
1197 return -ENOMEM;
1198}
1199
1200asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
1201 char __user * type, unsigned long flags,
1202 void __user * data)
1203{
1204 int retval;
1205 unsigned long data_page;
1206 unsigned long type_page;
1207 unsigned long dev_page;
1208 char *dir_page;
1209
1210 retval = copy_mount_options (type, &type_page);
1211 if (retval < 0)
1212 return retval;
1213
1214 dir_page = getname(dir_name);
1215 retval = PTR_ERR(dir_page);
1216 if (IS_ERR(dir_page))
1217 goto out1;
1218
1219 retval = copy_mount_options (dev_name, &dev_page);
1220 if (retval < 0)
1221 goto out2;
1222
1223 retval = copy_mount_options (data, &data_page);
1224 if (retval < 0)
1225 goto out3;
1226
1227 lock_kernel();
1228 retval = do_mount((char*)dev_page, dir_page, (char*)type_page,
1229 flags, (void*)data_page);
1230 unlock_kernel();
1231 free_page(data_page);
1232
1233out3:
1234 free_page(dev_page);
1235out2:
1236 putname(dir_page);
1237out1:
1238 free_page(type_page);
1239 return retval;
1240}
1241
1242/*
1243 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
1244 * It can block. Requires the big lock held.
1245 */
1246void set_fs_root(struct fs_struct *fs, struct vfsmount *mnt,
1247 struct dentry *dentry)
1248{
1249 struct dentry *old_root;
1250 struct vfsmount *old_rootmnt;
1251 write_lock(&fs->lock);
1252 old_root = fs->root;
1253 old_rootmnt = fs->rootmnt;
1254 fs->rootmnt = mntget(mnt);
1255 fs->root = dget(dentry);
1256 write_unlock(&fs->lock);
1257 if (old_root) {
1258 dput(old_root);
1259 mntput(old_rootmnt);
1260 }
1261}
1262
1263/*
1264 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
1265 * It can block. Requires the big lock held.
1266 */
1267void set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
1268 struct dentry *dentry)
1269{
1270 struct dentry *old_pwd;
1271 struct vfsmount *old_pwdmnt;
1272
1273 write_lock(&fs->lock);
1274 old_pwd = fs->pwd;
1275 old_pwdmnt = fs->pwdmnt;
1276 fs->pwdmnt = mntget(mnt);
1277 fs->pwd = dget(dentry);
1278 write_unlock(&fs->lock);
1279
1280 if (old_pwd) {
1281 dput(old_pwd);
1282 mntput(old_pwdmnt);
1283 }
1284}
1285
1286static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
1287{
1288 struct task_struct *g, *p;
1289 struct fs_struct *fs;
1290
1291 read_lock(&tasklist_lock);
1292 do_each_thread(g, p) {
1293 task_lock(p);
1294 fs = p->fs;
1295 if (fs) {
1296 atomic_inc(&fs->count);
1297 task_unlock(p);
1298 if (fs->root==old_nd->dentry&&fs->rootmnt==old_nd->mnt)
1299 set_fs_root(fs, new_nd->mnt, new_nd->dentry);
1300 if (fs->pwd==old_nd->dentry&&fs->pwdmnt==old_nd->mnt)
1301 set_fs_pwd(fs, new_nd->mnt, new_nd->dentry);
1302 put_fs_struct(fs);
1303 } else
1304 task_unlock(p);
1305 } while_each_thread(g, p);
1306 read_unlock(&tasklist_lock);
1307}
1308
1309/*
1310 * pivot_root Semantics:
1311 * Moves the root file system of the current process to the directory put_old,
1312 * makes new_root as the new root file system of the current process, and sets
1313 * root/cwd of all processes which had them on the current root to new_root.
1314 *
1315 * Restrictions:
1316 * The new_root and put_old must be directories, and must not be on the
1317 * same file system as the current process root. The put_old must be
1318 * underneath new_root, i.e. adding a non-zero number of /.. to the string
1319 * pointed to by put_old must yield the same directory as new_root. No other
1320 * file system may be mounted on put_old. After all, new_root is a mountpoint.
1321 *
1322 * Notes:
1323 * - we don't move root/cwd if they are not at the root (reason: if something
1324 * cared enough to change them, it's probably wrong to force them elsewhere)
1325 * - it's okay to pick a root that isn't the root of a file system, e.g.
1326 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1327 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1328 * first.
1329 */
1330
1331asmlinkage long sys_pivot_root(const char __user *new_root, const char __user *put_old)
1332{
1333 struct vfsmount *tmp;
1334 struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd;
1335 int error;
1336
1337 if (!capable(CAP_SYS_ADMIN))
1338 return -EPERM;
1339
1340 lock_kernel();
1341
1342 error = __user_walk(new_root, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &new_nd);
1343 if (error)
1344 goto out0;
1345 error = -EINVAL;
1346 if (!check_mnt(new_nd.mnt))
1347 goto out1;
1348
1349 error = __user_walk(put_old, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &old_nd);
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),
1467 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1468
1469 mount_hashtable = (struct list_head *)
1470 __get_free_page(GFP_ATOMIC);
1471
1472 if (!mount_hashtable)
1473 panic("Failed to allocate mount hash table\n");
1474
1475 /*
1476 * Find the power-of-two list-heads that can fit into the allocation..
1477 * We don't guarantee that "sizeof(struct list_head)" is necessarily
1478 * a power-of-two.
1479 */
1480 nr_hash = PAGE_SIZE / sizeof(struct list_head);
1481 hash_bits = 0;
1482 do {
1483 hash_bits++;
1484 } while ((nr_hash >> hash_bits) != 0);
1485 hash_bits--;
1486
1487 /*
1488 * Re-calculate the actual number of entries and the mask
1489 * from the number of bits we can fit.
1490 */
1491 nr_hash = 1UL << hash_bits;
1492 hash_mask = nr_hash-1;
1493
1494 printk("Mount-cache hash table entries: %d\n", nr_hash);
1495
1496 /* And initialize the newly allocated array */
1497 d = mount_hashtable;
1498 i = nr_hash;
1499 do {
1500 INIT_LIST_HEAD(d);
1501 d++;
1502 i--;
1503 } while (i);
1504 sysfs_init();
1505 init_rootfs();
1506 init_mount_tree();
1507}
1508
1509void __put_namespace(struct namespace *namespace)
1510{
1ce88cf4
MS
1511 struct vfsmount *root = namespace->root;
1512 namespace->root = NULL;
1513 spin_unlock(&vfsmount_lock);
1da177e4
LT
1514 down_write(&namespace->sem);
1515 spin_lock(&vfsmount_lock);
1ce88cf4 1516 umount_tree(root);
1da177e4
LT
1517 spin_unlock(&vfsmount_lock);
1518 up_write(&namespace->sem);
1519 kfree(namespace);
1520}