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