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