IB/mthca: kzalloc conversions
[linux-2.6/mini2440.git] / fs / namespace.c
blob3e8fb61ad597cfd90164bf40aefdf0a761a02a13
1 /*
2 * linux/fs/namespace.c
4 * (C) Copyright Al Viro 2000, 2001
5 * Released under GPL v2.
7 * Based on code from fs/super.c, copyright Linus Torvalds and others.
8 * Heavily rewritten.
9 */
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 #include "pnode.h"
29 extern int __init init_rootfs(void);
31 #ifdef CONFIG_SYSFS
32 extern int __init sysfs_init(void);
33 #else
34 static inline int sysfs_init(void)
36 return 0;
38 #endif
40 /* spinlock for vfsmount related operations, inplace of dcache_lock */
41 __cacheline_aligned_in_smp DEFINE_SPINLOCK(vfsmount_lock);
43 static int event;
45 static struct list_head *mount_hashtable;
46 static int hash_mask __read_mostly, hash_bits __read_mostly;
47 static kmem_cache_t *mnt_cache;
48 static struct rw_semaphore namespace_sem;
50 static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
52 unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
53 tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
54 tmp = tmp + (tmp >> hash_bits);
55 return tmp & hash_mask;
58 struct vfsmount *alloc_vfsmnt(const char *name)
60 struct vfsmount *mnt = kmem_cache_alloc(mnt_cache, GFP_KERNEL);
61 if (mnt) {
62 memset(mnt, 0, sizeof(struct vfsmount));
63 atomic_set(&mnt->mnt_count, 1);
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);
68 INIT_LIST_HEAD(&mnt->mnt_expire);
69 INIT_LIST_HEAD(&mnt->mnt_share);
70 INIT_LIST_HEAD(&mnt->mnt_slave_list);
71 INIT_LIST_HEAD(&mnt->mnt_slave);
72 if (name) {
73 int size = strlen(name) + 1;
74 char *newname = kmalloc(size, GFP_KERNEL);
75 if (newname) {
76 memcpy(newname, name, size);
77 mnt->mnt_devname = newname;
81 return mnt;
84 void free_vfsmnt(struct vfsmount *mnt)
86 kfree(mnt->mnt_devname);
87 kmem_cache_free(mnt_cache, mnt);
91 * find the first or last mount at @dentry on vfsmount @mnt depending on
92 * @dir. If @dir is set return the first mount else return the last mount.
94 struct vfsmount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
95 int dir)
97 struct list_head *head = mount_hashtable + hash(mnt, dentry);
98 struct list_head *tmp = head;
99 struct vfsmount *p, *found = NULL;
101 for (;;) {
102 tmp = dir ? tmp->next : tmp->prev;
103 p = NULL;
104 if (tmp == head)
105 break;
106 p = list_entry(tmp, struct vfsmount, mnt_hash);
107 if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
108 found = p;
109 break;
112 return found;
116 * lookup_mnt increments the ref count before returning
117 * the vfsmount struct.
119 struct vfsmount *lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
121 struct vfsmount *child_mnt;
122 spin_lock(&vfsmount_lock);
123 if ((child_mnt = __lookup_mnt(mnt, dentry, 1)))
124 mntget(child_mnt);
125 spin_unlock(&vfsmount_lock);
126 return child_mnt;
129 static inline int check_mnt(struct vfsmount *mnt)
131 return mnt->mnt_namespace == current->namespace;
134 static void touch_namespace(struct namespace *ns)
136 if (ns) {
137 ns->event = ++event;
138 wake_up_interruptible(&ns->poll);
142 static void __touch_namespace(struct namespace *ns)
144 if (ns && ns->event != event) {
145 ns->event = event;
146 wake_up_interruptible(&ns->poll);
150 static void detach_mnt(struct vfsmount *mnt, struct nameidata *old_nd)
152 old_nd->dentry = mnt->mnt_mountpoint;
153 old_nd->mnt = mnt->mnt_parent;
154 mnt->mnt_parent = mnt;
155 mnt->mnt_mountpoint = mnt->mnt_root;
156 list_del_init(&mnt->mnt_child);
157 list_del_init(&mnt->mnt_hash);
158 old_nd->dentry->d_mounted--;
161 void mnt_set_mountpoint(struct vfsmount *mnt, struct dentry *dentry,
162 struct vfsmount *child_mnt)
164 child_mnt->mnt_parent = mntget(mnt);
165 child_mnt->mnt_mountpoint = dget(dentry);
166 dentry->d_mounted++;
169 static void attach_mnt(struct vfsmount *mnt, struct nameidata *nd)
171 mnt_set_mountpoint(nd->mnt, nd->dentry, mnt);
172 list_add_tail(&mnt->mnt_hash, mount_hashtable +
173 hash(nd->mnt, nd->dentry));
174 list_add_tail(&mnt->mnt_child, &nd->mnt->mnt_mounts);
178 * the caller must hold vfsmount_lock
180 static void commit_tree(struct vfsmount *mnt)
182 struct vfsmount *parent = mnt->mnt_parent;
183 struct vfsmount *m;
184 LIST_HEAD(head);
185 struct namespace *n = parent->mnt_namespace;
187 BUG_ON(parent == mnt);
189 list_add_tail(&head, &mnt->mnt_list);
190 list_for_each_entry(m, &head, mnt_list)
191 m->mnt_namespace = n;
192 list_splice(&head, n->list.prev);
194 list_add_tail(&mnt->mnt_hash, mount_hashtable +
195 hash(parent, mnt->mnt_mountpoint));
196 list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
197 touch_namespace(n);
200 static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
202 struct list_head *next = p->mnt_mounts.next;
203 if (next == &p->mnt_mounts) {
204 while (1) {
205 if (p == root)
206 return NULL;
207 next = p->mnt_child.next;
208 if (next != &p->mnt_parent->mnt_mounts)
209 break;
210 p = p->mnt_parent;
213 return list_entry(next, struct vfsmount, mnt_child);
216 static struct vfsmount *skip_mnt_tree(struct vfsmount *p)
218 struct list_head *prev = p->mnt_mounts.prev;
219 while (prev != &p->mnt_mounts) {
220 p = list_entry(prev, struct vfsmount, mnt_child);
221 prev = p->mnt_mounts.prev;
223 return p;
226 static struct vfsmount *clone_mnt(struct vfsmount *old, struct dentry *root,
227 int flag)
229 struct super_block *sb = old->mnt_sb;
230 struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
232 if (mnt) {
233 mnt->mnt_flags = old->mnt_flags;
234 atomic_inc(&sb->s_active);
235 mnt->mnt_sb = sb;
236 mnt->mnt_root = dget(root);
237 mnt->mnt_mountpoint = mnt->mnt_root;
238 mnt->mnt_parent = mnt;
240 if (flag & CL_SLAVE) {
241 list_add(&mnt->mnt_slave, &old->mnt_slave_list);
242 mnt->mnt_master = old;
243 CLEAR_MNT_SHARED(mnt);
244 } else {
245 if ((flag & CL_PROPAGATION) || IS_MNT_SHARED(old))
246 list_add(&mnt->mnt_share, &old->mnt_share);
247 if (IS_MNT_SLAVE(old))
248 list_add(&mnt->mnt_slave, &old->mnt_slave);
249 mnt->mnt_master = old->mnt_master;
251 if (flag & CL_MAKE_SHARED)
252 set_mnt_shared(mnt);
254 /* stick the duplicate mount on the same expiry list
255 * as the original if that was on one */
256 if (flag & CL_EXPIRE) {
257 spin_lock(&vfsmount_lock);
258 if (!list_empty(&old->mnt_expire))
259 list_add(&mnt->mnt_expire, &old->mnt_expire);
260 spin_unlock(&vfsmount_lock);
263 return mnt;
266 static inline void __mntput(struct vfsmount *mnt)
268 struct super_block *sb = mnt->mnt_sb;
269 dput(mnt->mnt_root);
270 free_vfsmnt(mnt);
271 deactivate_super(sb);
274 void mntput_no_expire(struct vfsmount *mnt)
276 repeat:
277 if (atomic_dec_and_lock(&mnt->mnt_count, &vfsmount_lock)) {
278 if (likely(!mnt->mnt_pinned)) {
279 spin_unlock(&vfsmount_lock);
280 __mntput(mnt);
281 return;
283 atomic_add(mnt->mnt_pinned + 1, &mnt->mnt_count);
284 mnt->mnt_pinned = 0;
285 spin_unlock(&vfsmount_lock);
286 acct_auto_close_mnt(mnt);
287 security_sb_umount_close(mnt);
288 goto repeat;
292 EXPORT_SYMBOL(mntput_no_expire);
294 void mnt_pin(struct vfsmount *mnt)
296 spin_lock(&vfsmount_lock);
297 mnt->mnt_pinned++;
298 spin_unlock(&vfsmount_lock);
301 EXPORT_SYMBOL(mnt_pin);
303 void mnt_unpin(struct vfsmount *mnt)
305 spin_lock(&vfsmount_lock);
306 if (mnt->mnt_pinned) {
307 atomic_inc(&mnt->mnt_count);
308 mnt->mnt_pinned--;
310 spin_unlock(&vfsmount_lock);
313 EXPORT_SYMBOL(mnt_unpin);
315 /* iterator */
316 static void *m_start(struct seq_file *m, loff_t *pos)
318 struct namespace *n = m->private;
319 struct list_head *p;
320 loff_t l = *pos;
322 down_read(&namespace_sem);
323 list_for_each(p, &n->list)
324 if (!l--)
325 return list_entry(p, struct vfsmount, mnt_list);
326 return NULL;
329 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
331 struct namespace *n = m->private;
332 struct list_head *p = ((struct vfsmount *)v)->mnt_list.next;
333 (*pos)++;
334 return p == &n->list ? NULL : list_entry(p, struct vfsmount, mnt_list);
337 static void m_stop(struct seq_file *m, void *v)
339 up_read(&namespace_sem);
342 static inline void mangle(struct seq_file *m, const char *s)
344 seq_escape(m, s, " \t\n\\");
347 static int show_vfsmnt(struct seq_file *m, void *v)
349 struct vfsmount *mnt = v;
350 int err = 0;
351 static struct proc_fs_info {
352 int flag;
353 char *str;
354 } fs_info[] = {
355 { MS_SYNCHRONOUS, ",sync" },
356 { MS_DIRSYNC, ",dirsync" },
357 { MS_MANDLOCK, ",mand" },
358 { MS_NOATIME, ",noatime" },
359 { MS_NODIRATIME, ",nodiratime" },
360 { 0, NULL }
362 static struct proc_fs_info mnt_info[] = {
363 { MNT_NOSUID, ",nosuid" },
364 { MNT_NODEV, ",nodev" },
365 { MNT_NOEXEC, ",noexec" },
366 { 0, NULL }
368 struct proc_fs_info *fs_infop;
370 mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
371 seq_putc(m, ' ');
372 seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
373 seq_putc(m, ' ');
374 mangle(m, mnt->mnt_sb->s_type->name);
375 seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? " ro" : " rw");
376 for (fs_infop = fs_info; fs_infop->flag; fs_infop++) {
377 if (mnt->mnt_sb->s_flags & fs_infop->flag)
378 seq_puts(m, fs_infop->str);
380 for (fs_infop = mnt_info; fs_infop->flag; fs_infop++) {
381 if (mnt->mnt_flags & fs_infop->flag)
382 seq_puts(m, fs_infop->str);
384 if (mnt->mnt_sb->s_op->show_options)
385 err = mnt->mnt_sb->s_op->show_options(m, mnt);
386 seq_puts(m, " 0 0\n");
387 return err;
390 struct seq_operations mounts_op = {
391 .start = m_start,
392 .next = m_next,
393 .stop = m_stop,
394 .show = show_vfsmnt
398 * may_umount_tree - check if a mount tree is busy
399 * @mnt: root of mount tree
401 * This is called to check if a tree of mounts has any
402 * open files, pwds, chroots or sub mounts that are
403 * busy.
405 int may_umount_tree(struct vfsmount *mnt)
407 int actual_refs = 0;
408 int minimum_refs = 0;
409 struct vfsmount *p;
411 spin_lock(&vfsmount_lock);
412 for (p = mnt; p; p = next_mnt(p, mnt)) {
413 actual_refs += atomic_read(&p->mnt_count);
414 minimum_refs += 2;
416 spin_unlock(&vfsmount_lock);
418 if (actual_refs > minimum_refs)
419 return -EBUSY;
421 return 0;
424 EXPORT_SYMBOL(may_umount_tree);
427 * may_umount - check if a mount point is busy
428 * @mnt: root of mount
430 * This is called to check if a mount point has any
431 * open files, pwds, chroots or sub mounts. If the
432 * mount has sub mounts this will return busy
433 * regardless of whether the sub mounts are busy.
435 * Doesn't take quota and stuff into account. IOW, in some cases it will
436 * give false negatives. The main reason why it's here is that we need
437 * a non-destructive way to look for easily umountable filesystems.
439 int may_umount(struct vfsmount *mnt)
441 int ret = 0;
442 spin_lock(&vfsmount_lock);
443 if (propagate_mount_busy(mnt, 2))
444 ret = -EBUSY;
445 spin_unlock(&vfsmount_lock);
446 return ret;
449 EXPORT_SYMBOL(may_umount);
451 void release_mounts(struct list_head *head)
453 struct vfsmount *mnt;
454 while (!list_empty(head)) {
455 mnt = list_entry(head->next, struct vfsmount, mnt_hash);
456 list_del_init(&mnt->mnt_hash);
457 if (mnt->mnt_parent != mnt) {
458 struct dentry *dentry;
459 struct vfsmount *m;
460 spin_lock(&vfsmount_lock);
461 dentry = mnt->mnt_mountpoint;
462 m = mnt->mnt_parent;
463 mnt->mnt_mountpoint = mnt->mnt_root;
464 mnt->mnt_parent = mnt;
465 spin_unlock(&vfsmount_lock);
466 dput(dentry);
467 mntput(m);
469 mntput(mnt);
473 void umount_tree(struct vfsmount *mnt, int propagate, struct list_head *kill)
475 struct vfsmount *p;
477 for (p = mnt; p; p = next_mnt(p, mnt)) {
478 list_del(&p->mnt_hash);
479 list_add(&p->mnt_hash, kill);
482 if (propagate)
483 propagate_umount(kill);
485 list_for_each_entry(p, kill, mnt_hash) {
486 list_del_init(&p->mnt_expire);
487 list_del_init(&p->mnt_list);
488 __touch_namespace(p->mnt_namespace);
489 p->mnt_namespace = NULL;
490 list_del_init(&p->mnt_child);
491 if (p->mnt_parent != p)
492 mnt->mnt_mountpoint->d_mounted--;
493 change_mnt_propagation(p, MS_PRIVATE);
497 static int do_umount(struct vfsmount *mnt, int flags)
499 struct super_block *sb = mnt->mnt_sb;
500 int retval;
501 LIST_HEAD(umount_list);
503 retval = security_sb_umount(mnt, flags);
504 if (retval)
505 return retval;
508 * Allow userspace to request a mountpoint be expired rather than
509 * unmounting unconditionally. Unmount only happens if:
510 * (1) the mark is already set (the mark is cleared by mntput())
511 * (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
513 if (flags & MNT_EXPIRE) {
514 if (mnt == current->fs->rootmnt ||
515 flags & (MNT_FORCE | MNT_DETACH))
516 return -EINVAL;
518 if (atomic_read(&mnt->mnt_count) != 2)
519 return -EBUSY;
521 if (!xchg(&mnt->mnt_expiry_mark, 1))
522 return -EAGAIN;
526 * If we may have to abort operations to get out of this
527 * mount, and they will themselves hold resources we must
528 * allow the fs to do things. In the Unix tradition of
529 * 'Gee thats tricky lets do it in userspace' the umount_begin
530 * might fail to complete on the first run through as other tasks
531 * must return, and the like. Thats for the mount program to worry
532 * about for the moment.
535 lock_kernel();
536 if ((flags & MNT_FORCE) && sb->s_op->umount_begin)
537 sb->s_op->umount_begin(sb);
538 unlock_kernel();
541 * No sense to grab the lock for this test, but test itself looks
542 * somewhat bogus. Suggestions for better replacement?
543 * Ho-hum... In principle, we might treat that as umount + switch
544 * to rootfs. GC would eventually take care of the old vfsmount.
545 * Actually it makes sense, especially if rootfs would contain a
546 * /reboot - static binary that would close all descriptors and
547 * call reboot(9). Then init(8) could umount root and exec /reboot.
549 if (mnt == current->fs->rootmnt && !(flags & MNT_DETACH)) {
551 * Special case for "unmounting" root ...
552 * we just try to remount it readonly.
554 down_write(&sb->s_umount);
555 if (!(sb->s_flags & MS_RDONLY)) {
556 lock_kernel();
557 DQUOT_OFF(sb);
558 retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
559 unlock_kernel();
561 up_write(&sb->s_umount);
562 return retval;
565 down_write(&namespace_sem);
566 spin_lock(&vfsmount_lock);
567 event++;
569 retval = -EBUSY;
570 if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
571 if (!list_empty(&mnt->mnt_list))
572 umount_tree(mnt, 1, &umount_list);
573 retval = 0;
575 spin_unlock(&vfsmount_lock);
576 if (retval)
577 security_sb_umount_busy(mnt);
578 up_write(&namespace_sem);
579 release_mounts(&umount_list);
580 return retval;
584 * Now umount can handle mount points as well as block devices.
585 * This is important for filesystems which use unnamed block devices.
587 * We now support a flag for forced unmount like the other 'big iron'
588 * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
591 asmlinkage long sys_umount(char __user * name, int flags)
593 struct nameidata nd;
594 int retval;
596 retval = __user_walk(name, LOOKUP_FOLLOW, &nd);
597 if (retval)
598 goto out;
599 retval = -EINVAL;
600 if (nd.dentry != nd.mnt->mnt_root)
601 goto dput_and_out;
602 if (!check_mnt(nd.mnt))
603 goto dput_and_out;
605 retval = -EPERM;
606 if (!capable(CAP_SYS_ADMIN))
607 goto dput_and_out;
609 retval = do_umount(nd.mnt, flags);
610 dput_and_out:
611 path_release_on_umount(&nd);
612 out:
613 return retval;
616 #ifdef __ARCH_WANT_SYS_OLDUMOUNT
619 * The 2.0 compatible umount. No flags.
621 asmlinkage long sys_oldumount(char __user * name)
623 return sys_umount(name, 0);
626 #endif
628 static int mount_is_safe(struct nameidata *nd)
630 if (capable(CAP_SYS_ADMIN))
631 return 0;
632 return -EPERM;
633 #ifdef notyet
634 if (S_ISLNK(nd->dentry->d_inode->i_mode))
635 return -EPERM;
636 if (nd->dentry->d_inode->i_mode & S_ISVTX) {
637 if (current->uid != nd->dentry->d_inode->i_uid)
638 return -EPERM;
640 if (vfs_permission(nd, MAY_WRITE))
641 return -EPERM;
642 return 0;
643 #endif
646 static int lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
648 while (1) {
649 if (d == dentry)
650 return 1;
651 if (d == NULL || d == d->d_parent)
652 return 0;
653 d = d->d_parent;
657 struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry,
658 int flag)
660 struct vfsmount *res, *p, *q, *r, *s;
661 struct nameidata nd;
663 if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
664 return NULL;
666 res = q = clone_mnt(mnt, dentry, flag);
667 if (!q)
668 goto Enomem;
669 q->mnt_mountpoint = mnt->mnt_mountpoint;
671 p = mnt;
672 list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
673 if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
674 continue;
676 for (s = r; s; s = next_mnt(s, r)) {
677 if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) {
678 s = skip_mnt_tree(s);
679 continue;
681 while (p != s->mnt_parent) {
682 p = p->mnt_parent;
683 q = q->mnt_parent;
685 p = s;
686 nd.mnt = q;
687 nd.dentry = p->mnt_mountpoint;
688 q = clone_mnt(p, p->mnt_root, flag);
689 if (!q)
690 goto Enomem;
691 spin_lock(&vfsmount_lock);
692 list_add_tail(&q->mnt_list, &res->mnt_list);
693 attach_mnt(q, &nd);
694 spin_unlock(&vfsmount_lock);
697 return res;
698 Enomem:
699 if (res) {
700 LIST_HEAD(umount_list);
701 spin_lock(&vfsmount_lock);
702 umount_tree(res, 0, &umount_list);
703 spin_unlock(&vfsmount_lock);
704 release_mounts(&umount_list);
706 return NULL;
710 * @source_mnt : mount tree to be attached
711 * @nd : place the mount tree @source_mnt is attached
712 * @parent_nd : if non-null, detach the source_mnt from its parent and
713 * store the parent mount and mountpoint dentry.
714 * (done when source_mnt is moved)
716 * NOTE: in the table below explains the semantics when a source mount
717 * of a given type is attached to a destination mount of a given type.
718 * ---------------------------------------------------------------------------
719 * | BIND MOUNT OPERATION |
720 * |**************************************************************************
721 * | source-->| shared | private | slave | unbindable |
722 * | dest | | | | |
723 * | | | | | | |
724 * | v | | | | |
725 * |**************************************************************************
726 * | shared | shared (++) | shared (+) | shared(+++)| invalid |
727 * | | | | | |
728 * |non-shared| shared (+) | private | slave (*) | invalid |
729 * ***************************************************************************
730 * A bind operation clones the source mount and mounts the clone on the
731 * destination mount.
733 * (++) the cloned mount is propagated to all the mounts in the propagation
734 * tree of the destination mount and the cloned mount is added to
735 * the peer group of the source mount.
736 * (+) the cloned mount is created under the destination mount and is marked
737 * as shared. The cloned mount is added to the peer group of the source
738 * mount.
739 * (+++) the mount is propagated to all the mounts in the propagation tree
740 * of the destination mount and the cloned mount is made slave
741 * of the same master as that of the source mount. The cloned mount
742 * is marked as 'shared and slave'.
743 * (*) the cloned mount is made a slave of the same master as that of the
744 * source mount.
746 * ---------------------------------------------------------------------------
747 * | MOVE MOUNT OPERATION |
748 * |**************************************************************************
749 * | source-->| shared | private | slave | unbindable |
750 * | dest | | | | |
751 * | | | | | | |
752 * | v | | | | |
753 * |**************************************************************************
754 * | shared | shared (+) | shared (+) | shared(+++) | invalid |
755 * | | | | | |
756 * |non-shared| shared (+*) | private | slave (*) | unbindable |
757 * ***************************************************************************
759 * (+) the mount is moved to the destination. And is then propagated to
760 * all the mounts in the propagation tree of the destination mount.
761 * (+*) the mount is moved to the destination.
762 * (+++) the mount is moved to the destination and is then propagated to
763 * all the mounts belonging to the destination mount's propagation tree.
764 * the mount is marked as 'shared and slave'.
765 * (*) the mount continues to be a slave at the new location.
767 * if the source mount is a tree, the operations explained above is
768 * applied to each mount in the tree.
769 * Must be called without spinlocks held, since this function can sleep
770 * in allocations.
772 static int attach_recursive_mnt(struct vfsmount *source_mnt,
773 struct nameidata *nd, struct nameidata *parent_nd)
775 LIST_HEAD(tree_list);
776 struct vfsmount *dest_mnt = nd->mnt;
777 struct dentry *dest_dentry = nd->dentry;
778 struct vfsmount *child, *p;
780 if (propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list))
781 return -EINVAL;
783 if (IS_MNT_SHARED(dest_mnt)) {
784 for (p = source_mnt; p; p = next_mnt(p, source_mnt))
785 set_mnt_shared(p);
788 spin_lock(&vfsmount_lock);
789 if (parent_nd) {
790 detach_mnt(source_mnt, parent_nd);
791 attach_mnt(source_mnt, nd);
792 touch_namespace(current->namespace);
793 } else {
794 mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);
795 commit_tree(source_mnt);
798 list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
799 list_del_init(&child->mnt_hash);
800 commit_tree(child);
802 spin_unlock(&vfsmount_lock);
803 return 0;
806 static int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
808 int err;
809 if (mnt->mnt_sb->s_flags & MS_NOUSER)
810 return -EINVAL;
812 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
813 S_ISDIR(mnt->mnt_root->d_inode->i_mode))
814 return -ENOTDIR;
816 err = -ENOENT;
817 down(&nd->dentry->d_inode->i_sem);
818 if (IS_DEADDIR(nd->dentry->d_inode))
819 goto out_unlock;
821 err = security_sb_check_sb(mnt, nd);
822 if (err)
823 goto out_unlock;
825 err = -ENOENT;
826 if (IS_ROOT(nd->dentry) || !d_unhashed(nd->dentry))
827 err = attach_recursive_mnt(mnt, nd, NULL);
828 out_unlock:
829 up(&nd->dentry->d_inode->i_sem);
830 if (!err)
831 security_sb_post_addmount(mnt, nd);
832 return err;
836 * recursively change the type of the mountpoint.
838 static int do_change_type(struct nameidata *nd, int flag)
840 struct vfsmount *m, *mnt = nd->mnt;
841 int recurse = flag & MS_REC;
842 int type = flag & ~MS_REC;
844 if (nd->dentry != nd->mnt->mnt_root)
845 return -EINVAL;
847 down_write(&namespace_sem);
848 spin_lock(&vfsmount_lock);
849 for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
850 change_mnt_propagation(m, type);
851 spin_unlock(&vfsmount_lock);
852 up_write(&namespace_sem);
853 return 0;
857 * do loopback mount.
859 static int do_loopback(struct nameidata *nd, char *old_name, int recurse)
861 struct nameidata old_nd;
862 struct vfsmount *mnt = NULL;
863 int err = mount_is_safe(nd);
864 if (err)
865 return err;
866 if (!old_name || !*old_name)
867 return -EINVAL;
868 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
869 if (err)
870 return err;
872 down_write(&namespace_sem);
873 err = -EINVAL;
874 if (IS_MNT_UNBINDABLE(old_nd.mnt))
875 goto out;
877 if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
878 goto out;
880 err = -ENOMEM;
881 if (recurse)
882 mnt = copy_tree(old_nd.mnt, old_nd.dentry, 0);
883 else
884 mnt = clone_mnt(old_nd.mnt, old_nd.dentry, 0);
886 if (!mnt)
887 goto out;
889 err = graft_tree(mnt, nd);
890 if (err) {
891 LIST_HEAD(umount_list);
892 spin_lock(&vfsmount_lock);
893 umount_tree(mnt, 0, &umount_list);
894 spin_unlock(&vfsmount_lock);
895 release_mounts(&umount_list);
898 out:
899 up_write(&namespace_sem);
900 path_release(&old_nd);
901 return err;
905 * change filesystem flags. dir should be a physical root of filesystem.
906 * If you've mounted a non-root directory somewhere and want to do remount
907 * on it - tough luck.
909 static int do_remount(struct nameidata *nd, int flags, int mnt_flags,
910 void *data)
912 int err;
913 struct super_block *sb = nd->mnt->mnt_sb;
915 if (!capable(CAP_SYS_ADMIN))
916 return -EPERM;
918 if (!check_mnt(nd->mnt))
919 return -EINVAL;
921 if (nd->dentry != nd->mnt->mnt_root)
922 return -EINVAL;
924 down_write(&sb->s_umount);
925 err = do_remount_sb(sb, flags, data, 0);
926 if (!err)
927 nd->mnt->mnt_flags = mnt_flags;
928 up_write(&sb->s_umount);
929 if (!err)
930 security_sb_post_remount(nd->mnt, flags, data);
931 return err;
934 static inline int tree_contains_unbindable(struct vfsmount *mnt)
936 struct vfsmount *p;
937 for (p = mnt; p; p = next_mnt(p, mnt)) {
938 if (IS_MNT_UNBINDABLE(p))
939 return 1;
941 return 0;
944 static int do_move_mount(struct nameidata *nd, char *old_name)
946 struct nameidata old_nd, parent_nd;
947 struct vfsmount *p;
948 int err = 0;
949 if (!capable(CAP_SYS_ADMIN))
950 return -EPERM;
951 if (!old_name || !*old_name)
952 return -EINVAL;
953 err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
954 if (err)
955 return err;
957 down_write(&namespace_sem);
958 while (d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
960 err = -EINVAL;
961 if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
962 goto out;
964 err = -ENOENT;
965 down(&nd->dentry->d_inode->i_sem);
966 if (IS_DEADDIR(nd->dentry->d_inode))
967 goto out1;
969 if (!IS_ROOT(nd->dentry) && d_unhashed(nd->dentry))
970 goto out1;
972 err = -EINVAL;
973 if (old_nd.dentry != old_nd.mnt->mnt_root)
974 goto out1;
976 if (old_nd.mnt == old_nd.mnt->mnt_parent)
977 goto out1;
979 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
980 S_ISDIR(old_nd.dentry->d_inode->i_mode))
981 goto out1;
983 * Don't move a mount residing in a shared parent.
985 if (old_nd.mnt->mnt_parent && IS_MNT_SHARED(old_nd.mnt->mnt_parent))
986 goto out1;
988 * Don't move a mount tree containing unbindable mounts to a destination
989 * mount which is shared.
991 if (IS_MNT_SHARED(nd->mnt) && tree_contains_unbindable(old_nd.mnt))
992 goto out1;
993 err = -ELOOP;
994 for (p = nd->mnt; p->mnt_parent != p; p = p->mnt_parent)
995 if (p == old_nd.mnt)
996 goto out1;
998 if ((err = attach_recursive_mnt(old_nd.mnt, nd, &parent_nd)))
999 goto out1;
1001 spin_lock(&vfsmount_lock);
1002 /* if the mount is moved, it should no longer be expire
1003 * automatically */
1004 list_del_init(&old_nd.mnt->mnt_expire);
1005 spin_unlock(&vfsmount_lock);
1006 out1:
1007 up(&nd->dentry->d_inode->i_sem);
1008 out:
1009 up_write(&namespace_sem);
1010 if (!err)
1011 path_release(&parent_nd);
1012 path_release(&old_nd);
1013 return err;
1017 * create a new mount for userspace and request it to be added into the
1018 * namespace's tree
1020 static int do_new_mount(struct nameidata *nd, char *type, int flags,
1021 int mnt_flags, char *name, void *data)
1023 struct vfsmount *mnt;
1025 if (!type || !memchr(type, 0, PAGE_SIZE))
1026 return -EINVAL;
1028 /* we need capabilities... */
1029 if (!capable(CAP_SYS_ADMIN))
1030 return -EPERM;
1032 mnt = do_kern_mount(type, flags, name, data);
1033 if (IS_ERR(mnt))
1034 return PTR_ERR(mnt);
1036 return do_add_mount(mnt, nd, mnt_flags, NULL);
1040 * add a mount into a namespace's mount tree
1041 * - provide the option of adding the new mount to an expiration list
1043 int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd,
1044 int mnt_flags, struct list_head *fslist)
1046 int err;
1048 down_write(&namespace_sem);
1049 /* Something was mounted here while we slept */
1050 while (d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
1052 err = -EINVAL;
1053 if (!check_mnt(nd->mnt))
1054 goto unlock;
1056 /* Refuse the same filesystem on the same mount point */
1057 err = -EBUSY;
1058 if (nd->mnt->mnt_sb == newmnt->mnt_sb &&
1059 nd->mnt->mnt_root == nd->dentry)
1060 goto unlock;
1062 err = -EINVAL;
1063 if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
1064 goto unlock;
1066 newmnt->mnt_flags = mnt_flags;
1067 if ((err = graft_tree(newmnt, nd)))
1068 goto unlock;
1070 if (fslist) {
1071 /* add to the specified expiration list */
1072 spin_lock(&vfsmount_lock);
1073 list_add_tail(&newmnt->mnt_expire, fslist);
1074 spin_unlock(&vfsmount_lock);
1076 up_write(&namespace_sem);
1077 return 0;
1079 unlock:
1080 up_write(&namespace_sem);
1081 mntput(newmnt);
1082 return err;
1085 EXPORT_SYMBOL_GPL(do_add_mount);
1087 static void expire_mount(struct vfsmount *mnt, struct list_head *mounts,
1088 struct list_head *umounts)
1090 spin_lock(&vfsmount_lock);
1093 * Check if mount is still attached, if not, let whoever holds it deal
1094 * with the sucker
1096 if (mnt->mnt_parent == mnt) {
1097 spin_unlock(&vfsmount_lock);
1098 return;
1102 * Check that it is still dead: the count should now be 2 - as
1103 * contributed by the vfsmount parent and the mntget above
1105 if (!propagate_mount_busy(mnt, 2)) {
1106 /* delete from the namespace */
1107 touch_namespace(mnt->mnt_namespace);
1108 list_del_init(&mnt->mnt_list);
1109 mnt->mnt_namespace = NULL;
1110 umount_tree(mnt, 1, umounts);
1111 spin_unlock(&vfsmount_lock);
1112 } else {
1114 * Someone brought it back to life whilst we didn't have any
1115 * locks held so return it to the expiration list
1117 list_add_tail(&mnt->mnt_expire, mounts);
1118 spin_unlock(&vfsmount_lock);
1123 * process a list of expirable mountpoints with the intent of discarding any
1124 * mountpoints that aren't in use and haven't been touched since last we came
1125 * here
1127 void mark_mounts_for_expiry(struct list_head *mounts)
1129 struct namespace *namespace;
1130 struct vfsmount *mnt, *next;
1131 LIST_HEAD(graveyard);
1133 if (list_empty(mounts))
1134 return;
1136 spin_lock(&vfsmount_lock);
1138 /* extract from the expiration list every vfsmount that matches the
1139 * following criteria:
1140 * - only referenced by its parent vfsmount
1141 * - still marked for expiry (marked on the last call here; marks are
1142 * cleared by mntput())
1144 list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
1145 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
1146 atomic_read(&mnt->mnt_count) != 1)
1147 continue;
1149 mntget(mnt);
1150 list_move(&mnt->mnt_expire, &graveyard);
1154 * go through the vfsmounts we've just consigned to the graveyard to
1155 * - check that they're still dead
1156 * - delete the vfsmount from the appropriate namespace under lock
1157 * - dispose of the corpse
1159 while (!list_empty(&graveyard)) {
1160 LIST_HEAD(umounts);
1161 mnt = list_entry(graveyard.next, struct vfsmount, mnt_expire);
1162 list_del_init(&mnt->mnt_expire);
1164 /* don't do anything if the namespace is dead - all the
1165 * vfsmounts from it are going away anyway */
1166 namespace = mnt->mnt_namespace;
1167 if (!namespace || !namespace->root)
1168 continue;
1169 get_namespace(namespace);
1171 spin_unlock(&vfsmount_lock);
1172 down_write(&namespace_sem);
1173 expire_mount(mnt, mounts, &umounts);
1174 up_write(&namespace_sem);
1175 release_mounts(&umounts);
1176 mntput(mnt);
1177 put_namespace(namespace);
1178 spin_lock(&vfsmount_lock);
1181 spin_unlock(&vfsmount_lock);
1184 EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
1187 * Some copy_from_user() implementations do not return the exact number of
1188 * bytes remaining to copy on a fault. But copy_mount_options() requires that.
1189 * Note that this function differs from copy_from_user() in that it will oops
1190 * on bad values of `to', rather than returning a short copy.
1192 static long exact_copy_from_user(void *to, const void __user * from,
1193 unsigned long n)
1195 char *t = to;
1196 const char __user *f = from;
1197 char c;
1199 if (!access_ok(VERIFY_READ, from, n))
1200 return n;
1202 while (n) {
1203 if (__get_user(c, f)) {
1204 memset(t, 0, n);
1205 break;
1207 *t++ = c;
1208 f++;
1209 n--;
1211 return n;
1214 int copy_mount_options(const void __user * data, unsigned long *where)
1216 int i;
1217 unsigned long page;
1218 unsigned long size;
1220 *where = 0;
1221 if (!data)
1222 return 0;
1224 if (!(page = __get_free_page(GFP_KERNEL)))
1225 return -ENOMEM;
1227 /* We only care that *some* data at the address the user
1228 * gave us is valid. Just in case, we'll zero
1229 * the remainder of the page.
1231 /* copy_from_user cannot cross TASK_SIZE ! */
1232 size = TASK_SIZE - (unsigned long)data;
1233 if (size > PAGE_SIZE)
1234 size = PAGE_SIZE;
1236 i = size - exact_copy_from_user((void *)page, data, size);
1237 if (!i) {
1238 free_page(page);
1239 return -EFAULT;
1241 if (i != PAGE_SIZE)
1242 memset((char *)page + i, 0, PAGE_SIZE - i);
1243 *where = page;
1244 return 0;
1248 * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
1249 * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
1251 * data is a (void *) that can point to any structure up to
1252 * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
1253 * information (or be NULL).
1255 * Pre-0.97 versions of mount() didn't have a flags word.
1256 * When the flags word was introduced its top half was required
1257 * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
1258 * Therefore, if this magic number is present, it carries no information
1259 * and must be discarded.
1261 long do_mount(char *dev_name, char *dir_name, char *type_page,
1262 unsigned long flags, void *data_page)
1264 struct nameidata nd;
1265 int retval = 0;
1266 int mnt_flags = 0;
1268 /* Discard magic */
1269 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
1270 flags &= ~MS_MGC_MSK;
1272 /* Basic sanity checks */
1274 if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
1275 return -EINVAL;
1276 if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
1277 return -EINVAL;
1279 if (data_page)
1280 ((char *)data_page)[PAGE_SIZE - 1] = 0;
1282 /* Separate the per-mountpoint flags */
1283 if (flags & MS_NOSUID)
1284 mnt_flags |= MNT_NOSUID;
1285 if (flags & MS_NODEV)
1286 mnt_flags |= MNT_NODEV;
1287 if (flags & MS_NOEXEC)
1288 mnt_flags |= MNT_NOEXEC;
1289 flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE);
1291 /* ... and get the mountpoint */
1292 retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
1293 if (retval)
1294 return retval;
1296 retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
1297 if (retval)
1298 goto dput_out;
1300 if (flags & MS_REMOUNT)
1301 retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
1302 data_page);
1303 else if (flags & MS_BIND)
1304 retval = do_loopback(&nd, dev_name, flags & MS_REC);
1305 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
1306 retval = do_change_type(&nd, flags);
1307 else if (flags & MS_MOVE)
1308 retval = do_move_mount(&nd, dev_name);
1309 else
1310 retval = do_new_mount(&nd, type_page, flags, mnt_flags,
1311 dev_name, data_page);
1312 dput_out:
1313 path_release(&nd);
1314 return retval;
1317 int copy_namespace(int flags, struct task_struct *tsk)
1319 struct namespace *namespace = tsk->namespace;
1320 struct namespace *new_ns;
1321 struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
1322 struct fs_struct *fs = tsk->fs;
1323 struct vfsmount *p, *q;
1325 if (!namespace)
1326 return 0;
1328 get_namespace(namespace);
1330 if (!(flags & CLONE_NEWNS))
1331 return 0;
1333 if (!capable(CAP_SYS_ADMIN)) {
1334 put_namespace(namespace);
1335 return -EPERM;
1338 new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL);
1339 if (!new_ns)
1340 goto out;
1342 atomic_set(&new_ns->count, 1);
1343 INIT_LIST_HEAD(&new_ns->list);
1344 init_waitqueue_head(&new_ns->poll);
1345 new_ns->event = 0;
1347 down_write(&namespace_sem);
1348 /* First pass: copy the tree topology */
1349 new_ns->root = copy_tree(namespace->root, namespace->root->mnt_root,
1350 CL_COPY_ALL | CL_EXPIRE);
1351 if (!new_ns->root) {
1352 up_write(&namespace_sem);
1353 kfree(new_ns);
1354 goto out;
1356 spin_lock(&vfsmount_lock);
1357 list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
1358 spin_unlock(&vfsmount_lock);
1361 * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
1362 * as belonging to new namespace. We have already acquired a private
1363 * fs_struct, so tsk->fs->lock is not needed.
1365 p = namespace->root;
1366 q = new_ns->root;
1367 while (p) {
1368 q->mnt_namespace = new_ns;
1369 if (fs) {
1370 if (p == fs->rootmnt) {
1371 rootmnt = p;
1372 fs->rootmnt = mntget(q);
1374 if (p == fs->pwdmnt) {
1375 pwdmnt = p;
1376 fs->pwdmnt = mntget(q);
1378 if (p == fs->altrootmnt) {
1379 altrootmnt = p;
1380 fs->altrootmnt = mntget(q);
1383 p = next_mnt(p, namespace->root);
1384 q = next_mnt(q, new_ns->root);
1386 up_write(&namespace_sem);
1388 tsk->namespace = new_ns;
1390 if (rootmnt)
1391 mntput(rootmnt);
1392 if (pwdmnt)
1393 mntput(pwdmnt);
1394 if (altrootmnt)
1395 mntput(altrootmnt);
1397 put_namespace(namespace);
1398 return 0;
1400 out:
1401 put_namespace(namespace);
1402 return -ENOMEM;
1405 asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
1406 char __user * type, unsigned long flags,
1407 void __user * data)
1409 int retval;
1410 unsigned long data_page;
1411 unsigned long type_page;
1412 unsigned long dev_page;
1413 char *dir_page;
1415 retval = copy_mount_options(type, &type_page);
1416 if (retval < 0)
1417 return retval;
1419 dir_page = getname(dir_name);
1420 retval = PTR_ERR(dir_page);
1421 if (IS_ERR(dir_page))
1422 goto out1;
1424 retval = copy_mount_options(dev_name, &dev_page);
1425 if (retval < 0)
1426 goto out2;
1428 retval = copy_mount_options(data, &data_page);
1429 if (retval < 0)
1430 goto out3;
1432 lock_kernel();
1433 retval = do_mount((char *)dev_page, dir_page, (char *)type_page,
1434 flags, (void *)data_page);
1435 unlock_kernel();
1436 free_page(data_page);
1438 out3:
1439 free_page(dev_page);
1440 out2:
1441 putname(dir_page);
1442 out1:
1443 free_page(type_page);
1444 return retval;
1448 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
1449 * It can block. Requires the big lock held.
1451 void set_fs_root(struct fs_struct *fs, struct vfsmount *mnt,
1452 struct dentry *dentry)
1454 struct dentry *old_root;
1455 struct vfsmount *old_rootmnt;
1456 write_lock(&fs->lock);
1457 old_root = fs->root;
1458 old_rootmnt = fs->rootmnt;
1459 fs->rootmnt = mntget(mnt);
1460 fs->root = dget(dentry);
1461 write_unlock(&fs->lock);
1462 if (old_root) {
1463 dput(old_root);
1464 mntput(old_rootmnt);
1469 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
1470 * It can block. Requires the big lock held.
1472 void set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
1473 struct dentry *dentry)
1475 struct dentry *old_pwd;
1476 struct vfsmount *old_pwdmnt;
1478 write_lock(&fs->lock);
1479 old_pwd = fs->pwd;
1480 old_pwdmnt = fs->pwdmnt;
1481 fs->pwdmnt = mntget(mnt);
1482 fs->pwd = dget(dentry);
1483 write_unlock(&fs->lock);
1485 if (old_pwd) {
1486 dput(old_pwd);
1487 mntput(old_pwdmnt);
1491 static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
1493 struct task_struct *g, *p;
1494 struct fs_struct *fs;
1496 read_lock(&tasklist_lock);
1497 do_each_thread(g, p) {
1498 task_lock(p);
1499 fs = p->fs;
1500 if (fs) {
1501 atomic_inc(&fs->count);
1502 task_unlock(p);
1503 if (fs->root == old_nd->dentry
1504 && fs->rootmnt == old_nd->mnt)
1505 set_fs_root(fs, new_nd->mnt, new_nd->dentry);
1506 if (fs->pwd == old_nd->dentry
1507 && fs->pwdmnt == old_nd->mnt)
1508 set_fs_pwd(fs, new_nd->mnt, new_nd->dentry);
1509 put_fs_struct(fs);
1510 } else
1511 task_unlock(p);
1512 } while_each_thread(g, p);
1513 read_unlock(&tasklist_lock);
1517 * pivot_root Semantics:
1518 * Moves the root file system of the current process to the directory put_old,
1519 * makes new_root as the new root file system of the current process, and sets
1520 * root/cwd of all processes which had them on the current root to new_root.
1522 * Restrictions:
1523 * The new_root and put_old must be directories, and must not be on the
1524 * same file system as the current process root. The put_old must be
1525 * underneath new_root, i.e. adding a non-zero number of /.. to the string
1526 * pointed to by put_old must yield the same directory as new_root. No other
1527 * file system may be mounted on put_old. After all, new_root is a mountpoint.
1529 * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
1530 * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
1531 * in this situation.
1533 * Notes:
1534 * - we don't move root/cwd if they are not at the root (reason: if something
1535 * cared enough to change them, it's probably wrong to force them elsewhere)
1536 * - it's okay to pick a root that isn't the root of a file system, e.g.
1537 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1538 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1539 * first.
1541 asmlinkage long sys_pivot_root(const char __user * new_root,
1542 const char __user * put_old)
1544 struct vfsmount *tmp;
1545 struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd;
1546 int error;
1548 if (!capable(CAP_SYS_ADMIN))
1549 return -EPERM;
1551 lock_kernel();
1553 error = __user_walk(new_root, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
1554 &new_nd);
1555 if (error)
1556 goto out0;
1557 error = -EINVAL;
1558 if (!check_mnt(new_nd.mnt))
1559 goto out1;
1561 error = __user_walk(put_old, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old_nd);
1562 if (error)
1563 goto out1;
1565 error = security_sb_pivotroot(&old_nd, &new_nd);
1566 if (error) {
1567 path_release(&old_nd);
1568 goto out1;
1571 read_lock(&current->fs->lock);
1572 user_nd.mnt = mntget(current->fs->rootmnt);
1573 user_nd.dentry = dget(current->fs->root);
1574 read_unlock(&current->fs->lock);
1575 down_write(&namespace_sem);
1576 down(&old_nd.dentry->d_inode->i_sem);
1577 error = -EINVAL;
1578 if (IS_MNT_SHARED(old_nd.mnt) ||
1579 IS_MNT_SHARED(new_nd.mnt->mnt_parent) ||
1580 IS_MNT_SHARED(user_nd.mnt->mnt_parent))
1581 goto out2;
1582 if (!check_mnt(user_nd.mnt))
1583 goto out2;
1584 error = -ENOENT;
1585 if (IS_DEADDIR(new_nd.dentry->d_inode))
1586 goto out2;
1587 if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
1588 goto out2;
1589 if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
1590 goto out2;
1591 error = -EBUSY;
1592 if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt)
1593 goto out2; /* loop, on the same file system */
1594 error = -EINVAL;
1595 if (user_nd.mnt->mnt_root != user_nd.dentry)
1596 goto out2; /* not a mountpoint */
1597 if (user_nd.mnt->mnt_parent == user_nd.mnt)
1598 goto out2; /* not attached */
1599 if (new_nd.mnt->mnt_root != new_nd.dentry)
1600 goto out2; /* not a mountpoint */
1601 if (new_nd.mnt->mnt_parent == new_nd.mnt)
1602 goto out2; /* not attached */
1603 tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
1604 spin_lock(&vfsmount_lock);
1605 if (tmp != new_nd.mnt) {
1606 for (;;) {
1607 if (tmp->mnt_parent == tmp)
1608 goto out3; /* already mounted on put_old */
1609 if (tmp->mnt_parent == new_nd.mnt)
1610 break;
1611 tmp = tmp->mnt_parent;
1613 if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
1614 goto out3;
1615 } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
1616 goto out3;
1617 detach_mnt(new_nd.mnt, &parent_nd);
1618 detach_mnt(user_nd.mnt, &root_parent);
1619 attach_mnt(user_nd.mnt, &old_nd); /* mount old root on put_old */
1620 attach_mnt(new_nd.mnt, &root_parent); /* mount new_root on / */
1621 touch_namespace(current->namespace);
1622 spin_unlock(&vfsmount_lock);
1623 chroot_fs_refs(&user_nd, &new_nd);
1624 security_sb_post_pivotroot(&user_nd, &new_nd);
1625 error = 0;
1626 path_release(&root_parent);
1627 path_release(&parent_nd);
1628 out2:
1629 up(&old_nd.dentry->d_inode->i_sem);
1630 up_write(&namespace_sem);
1631 path_release(&user_nd);
1632 path_release(&old_nd);
1633 out1:
1634 path_release(&new_nd);
1635 out0:
1636 unlock_kernel();
1637 return error;
1638 out3:
1639 spin_unlock(&vfsmount_lock);
1640 goto out2;
1643 static void __init init_mount_tree(void)
1645 struct vfsmount *mnt;
1646 struct namespace *namespace;
1647 struct task_struct *g, *p;
1649 mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
1650 if (IS_ERR(mnt))
1651 panic("Can't create rootfs");
1652 namespace = kmalloc(sizeof(*namespace), GFP_KERNEL);
1653 if (!namespace)
1654 panic("Can't allocate initial namespace");
1655 atomic_set(&namespace->count, 1);
1656 INIT_LIST_HEAD(&namespace->list);
1657 init_waitqueue_head(&namespace->poll);
1658 namespace->event = 0;
1659 list_add(&mnt->mnt_list, &namespace->list);
1660 namespace->root = mnt;
1661 mnt->mnt_namespace = namespace;
1663 init_task.namespace = namespace;
1664 read_lock(&tasklist_lock);
1665 do_each_thread(g, p) {
1666 get_namespace(namespace);
1667 p->namespace = namespace;
1668 } while_each_thread(g, p);
1669 read_unlock(&tasklist_lock);
1671 set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root);
1672 set_fs_root(current->fs, namespace->root, namespace->root->mnt_root);
1675 void __init mnt_init(unsigned long mempages)
1677 struct list_head *d;
1678 unsigned int nr_hash;
1679 int i;
1681 init_rwsem(&namespace_sem);
1683 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
1684 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL, NULL);
1686 mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
1688 if (!mount_hashtable)
1689 panic("Failed to allocate mount hash table\n");
1692 * Find the power-of-two list-heads that can fit into the allocation..
1693 * We don't guarantee that "sizeof(struct list_head)" is necessarily
1694 * a power-of-two.
1696 nr_hash = PAGE_SIZE / sizeof(struct list_head);
1697 hash_bits = 0;
1698 do {
1699 hash_bits++;
1700 } while ((nr_hash >> hash_bits) != 0);
1701 hash_bits--;
1704 * Re-calculate the actual number of entries and the mask
1705 * from the number of bits we can fit.
1707 nr_hash = 1UL << hash_bits;
1708 hash_mask = nr_hash - 1;
1710 printk("Mount-cache hash table entries: %d\n", nr_hash);
1712 /* And initialize the newly allocated array */
1713 d = mount_hashtable;
1714 i = nr_hash;
1715 do {
1716 INIT_LIST_HEAD(d);
1717 d++;
1718 i--;
1719 } while (i);
1720 sysfs_init();
1721 init_rootfs();
1722 init_mount_tree();
1725 void __put_namespace(struct namespace *namespace)
1727 struct vfsmount *root = namespace->root;
1728 LIST_HEAD(umount_list);
1729 namespace->root = NULL;
1730 spin_unlock(&vfsmount_lock);
1731 down_write(&namespace_sem);
1732 spin_lock(&vfsmount_lock);
1733 umount_tree(root, 0, &umount_list);
1734 spin_unlock(&vfsmount_lock);
1735 up_write(&namespace_sem);
1736 release_mounts(&umount_list);
1737 kfree(namespace);