blob: 2019899f2ab822caec7dd235e8c610096549a93b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Ram Pai07b20882005-11-07 17:19:07 -050027#include "pnode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29extern int __init init_rootfs(void);
30
31#ifdef CONFIG_SYSFS
32extern int __init sysfs_init(void);
33#else
34static inline int sysfs_init(void)
35{
36 return 0;
37}
38#endif
39
40/* spinlock for vfsmount related operations, inplace of dcache_lock */
Al Viro5addc5d2005-11-07 17:15:49 -050041__cacheline_aligned_in_smp DEFINE_SPINLOCK(vfsmount_lock);
42
43static int event;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45static struct list_head *mount_hashtable;
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -070046static int hash_mask __read_mostly, hash_bits __read_mostly;
Ram Paib58fed82005-11-07 17:16:09 -050047static kmem_cache_t *mnt_cache;
Ram Pai390c6842005-11-07 17:17:51 -050048static struct rw_semaphore namespace_sem;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
51{
Ram Paib58fed82005-11-07 17:16:09 -050052 unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
53 tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 tmp = tmp + (tmp >> hash_bits);
55 return tmp & hash_mask;
56}
57
58struct vfsmount *alloc_vfsmnt(const char *name)
59{
Ram Paib58fed82005-11-07 17:16:09 -050060 struct vfsmount *mnt = kmem_cache_alloc(mnt_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 if (mnt) {
62 memset(mnt, 0, sizeof(struct vfsmount));
Ram Paib58fed82005-11-07 17:16:09 -050063 atomic_set(&mnt->mnt_count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 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);
Miklos Szeredi55e700b2005-07-07 17:57:30 -070068 INIT_LIST_HEAD(&mnt->mnt_expire);
Ram Pai03e06e62005-11-07 17:19:33 -050069 INIT_LIST_HEAD(&mnt->mnt_share);
Ram Paia58b0eb2005-11-07 17:20:48 -050070 INIT_LIST_HEAD(&mnt->mnt_slave_list);
71 INIT_LIST_HEAD(&mnt->mnt_slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (name) {
Ram Paib58fed82005-11-07 17:16:09 -050073 int size = strlen(name) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 char *newname = kmalloc(size, GFP_KERNEL);
75 if (newname) {
76 memcpy(newname, name, size);
77 mnt->mnt_devname = newname;
78 }
79 }
80 }
81 return mnt;
82}
83
84void free_vfsmnt(struct vfsmount *mnt)
85{
86 kfree(mnt->mnt_devname);
87 kmem_cache_free(mnt_cache, mnt);
88}
89
90/*
Ram Paia05964f2005-11-07 17:20:17 -050091 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 */
Ram Paia05964f2005-11-07 17:20:17 -050094struct vfsmount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
95 int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Ram Paib58fed82005-11-07 17:16:09 -050097 struct list_head *head = mount_hashtable + hash(mnt, dentry);
98 struct list_head *tmp = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 struct vfsmount *p, *found = NULL;
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 for (;;) {
Ram Paia05964f2005-11-07 17:20:17 -0500102 tmp = dir ? tmp->next : tmp->prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 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) {
Ram Paia05964f2005-11-07 17:20:17 -0500108 found = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 break;
110 }
111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return found;
113}
114
Ram Paia05964f2005-11-07 17:20:17 -0500115/*
116 * lookup_mnt increments the ref count before returning
117 * the vfsmount struct.
118 */
119struct vfsmount *lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
120{
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;
127}
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129static inline int check_mnt(struct vfsmount *mnt)
130{
131 return mnt->mnt_namespace == current->namespace;
132}
133
Al Viro5addc5d2005-11-07 17:15:49 -0500134static void touch_namespace(struct namespace *ns)
135{
136 if (ns) {
137 ns->event = ++event;
138 wake_up_interruptible(&ns->poll);
139 }
140}
141
142static void __touch_namespace(struct namespace *ns)
143{
144 if (ns && ns->event != event) {
145 ns->event = event;
146 wake_up_interruptible(&ns->poll);
147 }
148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static void detach_mnt(struct vfsmount *mnt, struct nameidata *old_nd)
151{
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--;
159}
160
Ram Paib90fa9a2005-11-07 17:19:50 -0500161void mnt_set_mountpoint(struct vfsmount *mnt, struct dentry *dentry,
162 struct vfsmount *child_mnt)
163{
164 child_mnt->mnt_parent = mntget(mnt);
165 child_mnt->mnt_mountpoint = dget(dentry);
166 dentry->d_mounted++;
167}
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169static void attach_mnt(struct vfsmount *mnt, struct nameidata *nd)
170{
Ram Paib90fa9a2005-11-07 17:19:50 -0500171 mnt_set_mountpoint(nd->mnt, nd->dentry, mnt);
172 list_add_tail(&mnt->mnt_hash, mount_hashtable +
173 hash(nd->mnt, nd->dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 list_add_tail(&mnt->mnt_child, &nd->mnt->mnt_mounts);
Ram Paib90fa9a2005-11-07 17:19:50 -0500175}
176
177/*
178 * the caller must hold vfsmount_lock
179 */
180static void commit_tree(struct vfsmount *mnt)
181{
182 struct vfsmount *parent = mnt->mnt_parent;
183 struct vfsmount *m;
184 LIST_HEAD(head);
185 struct namespace *n = parent->mnt_namespace;
186
187 BUG_ON(parent == mnt);
188
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);
193
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
200static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
201{
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;
211 }
212 }
213 return list_entry(next, struct vfsmount, mnt_child);
214}
215
Ram Pai9676f0c2005-11-07 17:21:20 -0500216static struct vfsmount *skip_mnt_tree(struct vfsmount *p)
217{
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;
222 }
223 return p;
224}
225
Ram Pai36341f62005-11-07 17:17:22 -0500226static struct vfsmount *clone_mnt(struct vfsmount *old, struct dentry *root,
227 int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 struct super_block *sb = old->mnt_sb;
230 struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
231
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;
Ram Paib90fa9a2005-11-07 17:19:50 -0500239
Ram Pai5afe0022005-11-07 17:21:01 -0500240 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;
250 }
Ram Paib90fa9a2005-11-07 17:19:50 -0500251 if (flag & CL_MAKE_SHARED)
252 set_mnt_shared(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 /* stick the duplicate mount on the same expiry list
255 * as the original if that was on one */
Ram Pai36341f62005-11-07 17:17:22 -0500256 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);
261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263 return mnt;
264}
265
Al Viro7b7b1ac2005-11-07 17:13:39 -0500266static inline void __mntput(struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 struct super_block *sb = mnt->mnt_sb;
269 dput(mnt->mnt_root);
270 free_vfsmnt(mnt);
271 deactivate_super(sb);
272}
273
Al Viro7b7b1ac2005-11-07 17:13:39 -0500274void mntput_no_expire(struct vfsmount *mnt)
275{
276repeat:
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;
282 }
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;
289 }
290}
291
292EXPORT_SYMBOL(mntput_no_expire);
293
294void mnt_pin(struct vfsmount *mnt)
295{
296 spin_lock(&vfsmount_lock);
297 mnt->mnt_pinned++;
298 spin_unlock(&vfsmount_lock);
299}
300
301EXPORT_SYMBOL(mnt_pin);
302
303void mnt_unpin(struct vfsmount *mnt)
304{
305 spin_lock(&vfsmount_lock);
306 if (mnt->mnt_pinned) {
307 atomic_inc(&mnt->mnt_count);
308 mnt->mnt_pinned--;
309 }
310 spin_unlock(&vfsmount_lock);
311}
312
313EXPORT_SYMBOL(mnt_unpin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315/* iterator */
316static void *m_start(struct seq_file *m, loff_t *pos)
317{
318 struct namespace *n = m->private;
319 struct list_head *p;
320 loff_t l = *pos;
321
Ram Pai390c6842005-11-07 17:17:51 -0500322 down_read(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 list_for_each(p, &n->list)
324 if (!l--)
325 return list_entry(p, struct vfsmount, mnt_list);
326 return NULL;
327}
328
329static void *m_next(struct seq_file *m, void *v, loff_t *pos)
330{
331 struct namespace *n = m->private;
332 struct list_head *p = ((struct vfsmount *)v)->mnt_list.next;
333 (*pos)++;
Ram Paib58fed82005-11-07 17:16:09 -0500334 return p == &n->list ? NULL : list_entry(p, struct vfsmount, mnt_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
337static void m_stop(struct seq_file *m, void *v)
338{
Ram Pai390c6842005-11-07 17:17:51 -0500339 up_read(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
342static inline void mangle(struct seq_file *m, const char *s)
343{
344 seq_escape(m, s, " \t\n\\");
345}
346
347static int show_vfsmnt(struct seq_file *m, void *v)
348{
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 }
361 };
362 static struct proc_fs_info mnt_info[] = {
363 { MNT_NOSUID, ",nosuid" },
364 { MNT_NODEV, ",nodev" },
365 { MNT_NOEXEC, ",noexec" },
366 { 0, NULL }
367 };
368 struct proc_fs_info *fs_infop;
369
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);
379 }
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);
383 }
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;
388}
389
390struct seq_operations mounts_op = {
391 .start = m_start,
392 .next = m_next,
393 .stop = m_stop,
394 .show = show_vfsmnt
395};
396
397/**
398 * may_umount_tree - check if a mount tree is busy
399 * @mnt: root of mount tree
400 *
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.
404 */
405int may_umount_tree(struct vfsmount *mnt)
406{
Ram Pai36341f62005-11-07 17:17:22 -0500407 int actual_refs = 0;
408 int minimum_refs = 0;
409 struct vfsmount *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 spin_lock(&vfsmount_lock);
Ram Pai36341f62005-11-07 17:17:22 -0500412 for (p = mnt; p; p = next_mnt(p, mnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 actual_refs += atomic_read(&p->mnt_count);
414 minimum_refs += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416 spin_unlock(&vfsmount_lock);
417
418 if (actual_refs > minimum_refs)
419 return -EBUSY;
420
421 return 0;
422}
423
424EXPORT_SYMBOL(may_umount_tree);
425
426/**
427 * may_umount - check if a mount point is busy
428 * @mnt: root of mount
429 *
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.
434 *
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.
438 */
439int may_umount(struct vfsmount *mnt)
440{
Ram Paia05964f2005-11-07 17:20:17 -0500441 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
449EXPORT_SYMBOL(may_umount);
450
Ram Paib90fa9a2005-11-07 17:19:50 -0500451void release_mounts(struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Ram Pai70fbcdf2005-11-07 17:17:04 -0500453 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 spin_unlock(&vfsmount_lock);
Ram Pai70fbcdf2005-11-07 17:17:04 -0500466 dput(dentry);
467 mntput(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469 mntput(mnt);
Ram Pai70fbcdf2005-11-07 17:17:04 -0500470 }
471}
472
Ram Paia05964f2005-11-07 17:20:17 -0500473void umount_tree(struct vfsmount *mnt, int propagate, struct list_head *kill)
Ram Pai70fbcdf2005-11-07 17:17:04 -0500474{
475 struct vfsmount *p;
476
477 for (p = mnt; p; p = next_mnt(p, mnt)) {
478 list_del(&p->mnt_hash);
479 list_add(&p->mnt_hash, kill);
480 }
481
Ram Paia05964f2005-11-07 17:20:17 -0500482 if (propagate)
483 propagate_umount(kill);
484
Ram Pai70fbcdf2005-11-07 17:17:04 -0500485 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--;
Ram Paia05964f2005-11-07 17:20:17 -0500493 change_mnt_propagation(p, MS_PRIVATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495}
496
497static int do_umount(struct vfsmount *mnt, int flags)
498{
Ram Paib58fed82005-11-07 17:16:09 -0500499 struct super_block *sb = mnt->mnt_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 int retval;
Ram Pai70fbcdf2005-11-07 17:17:04 -0500501 LIST_HEAD(umount_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 retval = security_sb_umount(mnt, flags);
504 if (retval)
505 return retval;
506
507 /*
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]
512 */
513 if (flags & MNT_EXPIRE) {
514 if (mnt == current->fs->rootmnt ||
515 flags & (MNT_FORCE | MNT_DETACH))
516 return -EINVAL;
517
518 if (atomic_read(&mnt->mnt_count) != 2)
519 return -EBUSY;
520
521 if (!xchg(&mnt->mnt_expiry_mark, 1))
522 return -EAGAIN;
523 }
524
525 /*
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.
533 */
534
535 lock_kernel();
Ram Paib58fed82005-11-07 17:16:09 -0500536 if ((flags & MNT_FORCE) && sb->s_op->umount_begin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 sb->s_op->umount_begin(sb);
538 unlock_kernel();
539
540 /*
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.
548 */
549 if (mnt == current->fs->rootmnt && !(flags & MNT_DETACH)) {
550 /*
551 * Special case for "unmounting" root ...
552 * we just try to remount it readonly.
553 */
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();
560 }
561 up_write(&sb->s_umount);
562 return retval;
563 }
564
Ram Pai390c6842005-11-07 17:17:51 -0500565 down_write(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 spin_lock(&vfsmount_lock);
Al Viro5addc5d2005-11-07 17:15:49 -0500567 event++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 retval = -EBUSY;
Ram Paia05964f2005-11-07 17:20:17 -0500570 if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (!list_empty(&mnt->mnt_list))
Ram Paia05964f2005-11-07 17:20:17 -0500572 umount_tree(mnt, 1, &umount_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 retval = 0;
574 }
575 spin_unlock(&vfsmount_lock);
576 if (retval)
577 security_sb_umount_busy(mnt);
Ram Pai390c6842005-11-07 17:17:51 -0500578 up_write(&namespace_sem);
Ram Pai70fbcdf2005-11-07 17:17:04 -0500579 release_mounts(&umount_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return retval;
581}
582
583/*
584 * Now umount can handle mount points as well as block devices.
585 * This is important for filesystems which use unnamed block devices.
586 *
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
589 */
590
591asmlinkage long sys_umount(char __user * name, int flags)
592{
593 struct nameidata nd;
594 int retval;
595
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;
604
605 retval = -EPERM;
606 if (!capable(CAP_SYS_ADMIN))
607 goto dput_and_out;
608
609 retval = do_umount(nd.mnt, flags);
610dput_and_out:
611 path_release_on_umount(&nd);
612out:
613 return retval;
614}
615
616#ifdef __ARCH_WANT_SYS_OLDUMOUNT
617
618/*
Ram Paib58fed82005-11-07 17:16:09 -0500619 * The 2.0 compatible umount. No flags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621asmlinkage long sys_oldumount(char __user * name)
622{
Ram Paib58fed82005-11-07 17:16:09 -0500623 return sys_umount(name, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
626#endif
627
628static int mount_is_safe(struct nameidata *nd)
629{
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;
639 }
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800640 if (vfs_permission(nd, MAY_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return -EPERM;
642 return 0;
643#endif
644}
645
Ram Paib58fed82005-11-07 17:16:09 -0500646static int lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
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;
654 }
655}
656
Ram Paib90fa9a2005-11-07 17:19:50 -0500657struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry,
Ram Pai36341f62005-11-07 17:17:22 -0500658 int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 struct vfsmount *res, *p, *q, *r, *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 struct nameidata nd;
662
Ram Pai9676f0c2005-11-07 17:21:20 -0500663 if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
664 return NULL;
665
Ram Pai36341f62005-11-07 17:17:22 -0500666 res = q = clone_mnt(mnt, dentry, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (!q)
668 goto Enomem;
669 q->mnt_mountpoint = mnt->mnt_mountpoint;
670
671 p = mnt;
Domen Puncerfdadd652005-09-10 00:27:07 -0700672 list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
674 continue;
675
676 for (s = r; s; s = next_mnt(s, r)) {
Ram Pai9676f0c2005-11-07 17:21:20 -0500677 if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) {
678 s = skip_mnt_tree(s);
679 continue;
680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 while (p != s->mnt_parent) {
682 p = p->mnt_parent;
683 q = q->mnt_parent;
684 }
685 p = s;
686 nd.mnt = q;
687 nd.dentry = p->mnt_mountpoint;
Ram Pai36341f62005-11-07 17:17:22 -0500688 q = clone_mnt(p, p->mnt_root, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 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);
695 }
696 }
697 return res;
Ram Paib58fed82005-11-07 17:16:09 -0500698Enomem:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (res) {
Ram Pai70fbcdf2005-11-07 17:17:04 -0500700 LIST_HEAD(umount_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 spin_lock(&vfsmount_lock);
Ram Paia05964f2005-11-07 17:20:17 -0500702 umount_tree(res, 0, &umount_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 spin_unlock(&vfsmount_lock);
Ram Pai70fbcdf2005-11-07 17:17:04 -0500704 release_mounts(&umount_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706 return NULL;
707}
708
Ram Paib90fa9a2005-11-07 17:19:50 -0500709/*
710 * @source_mnt : mount tree to be attached
Ram Pai21444402005-11-07 17:20:03 -0500711 * @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)
Ram Paib90fa9a2005-11-07 17:19:50 -0500715 *
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.
Ram Pai9676f0c2005-11-07 17:21:20 -0500718 * ---------------------------------------------------------------------------
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 * ***************************************************************************
Ram Paib90fa9a2005-11-07 17:19:50 -0500730 * A bind operation clones the source mount and mounts the clone on the
731 * destination mount.
732 *
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.
Ram Pai5afe0022005-11-07 17:21:01 -0500739 * (+++) 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.
745 *
Ram Pai9676f0c2005-11-07 17:21:20 -0500746 * ---------------------------------------------------------------------------
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 * ***************************************************************************
Ram Pai5afe0022005-11-07 17:21:01 -0500758 *
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.
Ram Pai21444402005-11-07 17:20:03 -0500761 * (+*) the mount is moved to the destination.
Ram Pai5afe0022005-11-07 17:21:01 -0500762 * (+++) 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.
Ram Paib90fa9a2005-11-07 17:19:50 -0500766 *
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.
771 */
772static int attach_recursive_mnt(struct vfsmount *source_mnt,
Ram Pai21444402005-11-07 17:20:03 -0500773 struct nameidata *nd, struct nameidata *parent_nd)
Ram Paib90fa9a2005-11-07 17:19:50 -0500774{
775 LIST_HEAD(tree_list);
776 struct vfsmount *dest_mnt = nd->mnt;
777 struct dentry *dest_dentry = nd->dentry;
778 struct vfsmount *child, *p;
779
780 if (propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list))
781 return -EINVAL;
782
783 if (IS_MNT_SHARED(dest_mnt)) {
784 for (p = source_mnt; p; p = next_mnt(p, source_mnt))
785 set_mnt_shared(p);
786 }
787
788 spin_lock(&vfsmount_lock);
Ram Pai21444402005-11-07 17:20:03 -0500789 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);
796 }
Ram Paib90fa9a2005-11-07 17:19:50 -0500797
798 list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
799 list_del_init(&child->mnt_hash);
800 commit_tree(child);
801 }
802 spin_unlock(&vfsmount_lock);
803 return 0;
804}
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806static int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
807{
808 int err;
809 if (mnt->mnt_sb->s_flags & MS_NOUSER)
810 return -EINVAL;
811
812 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
813 S_ISDIR(mnt->mnt_root->d_inode->i_mode))
814 return -ENOTDIR;
815
816 err = -ENOENT;
817 down(&nd->dentry->d_inode->i_sem);
818 if (IS_DEADDIR(nd->dentry->d_inode))
819 goto out_unlock;
820
821 err = security_sb_check_sb(mnt, nd);
822 if (err)
823 goto out_unlock;
824
825 err = -ENOENT;
Ram Paib90fa9a2005-11-07 17:19:50 -0500826 if (IS_ROOT(nd->dentry) || !d_unhashed(nd->dentry))
Ram Pai21444402005-11-07 17:20:03 -0500827 err = attach_recursive_mnt(mnt, nd, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828out_unlock:
829 up(&nd->dentry->d_inode->i_sem);
830 if (!err)
831 security_sb_post_addmount(mnt, nd);
832 return err;
833}
834
835/*
Ram Pai07b20882005-11-07 17:19:07 -0500836 * recursively change the type of the mountpoint.
837 */
838static int do_change_type(struct nameidata *nd, int flag)
839{
840 struct vfsmount *m, *mnt = nd->mnt;
841 int recurse = flag & MS_REC;
842 int type = flag & ~MS_REC;
843
844 if (nd->dentry != nd->mnt->mnt_root)
845 return -EINVAL;
846
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;
854}
855
856/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 * do loopback mount.
858 */
859static int do_loopback(struct nameidata *nd, char *old_name, int recurse)
860{
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;
871
Ram Pai390c6842005-11-07 17:17:51 -0500872 down_write(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 err = -EINVAL;
Ram Pai9676f0c2005-11-07 17:21:20 -0500874 if (IS_MNT_UNBINDABLE(old_nd.mnt))
875 goto out;
876
Al Viroccd48bc2005-11-07 17:15:04 -0500877 if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
878 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Al Viroccd48bc2005-11-07 17:15:04 -0500880 err = -ENOMEM;
881 if (recurse)
Ram Pai36341f62005-11-07 17:17:22 -0500882 mnt = copy_tree(old_nd.mnt, old_nd.dentry, 0);
Al Viroccd48bc2005-11-07 17:15:04 -0500883 else
Ram Pai36341f62005-11-07 17:17:22 -0500884 mnt = clone_mnt(old_nd.mnt, old_nd.dentry, 0);
Al Viroccd48bc2005-11-07 17:15:04 -0500885
886 if (!mnt)
887 goto out;
888
Al Viroccd48bc2005-11-07 17:15:04 -0500889 err = graft_tree(mnt, nd);
890 if (err) {
Ram Pai70fbcdf2005-11-07 17:17:04 -0500891 LIST_HEAD(umount_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 spin_lock(&vfsmount_lock);
Ram Paia05964f2005-11-07 17:20:17 -0500893 umount_tree(mnt, 0, &umount_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 spin_unlock(&vfsmount_lock);
Ram Pai70fbcdf2005-11-07 17:17:04 -0500895 release_mounts(&umount_list);
Ram Pai5b83d2c2005-11-07 17:16:29 -0500896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Al Viroccd48bc2005-11-07 17:15:04 -0500898out:
Ram Pai390c6842005-11-07 17:17:51 -0500899 up_write(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 path_release(&old_nd);
901 return err;
902}
903
904/*
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.
908 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909static int do_remount(struct nameidata *nd, int flags, int mnt_flags,
910 void *data)
911{
912 int err;
Ram Paib58fed82005-11-07 17:16:09 -0500913 struct super_block *sb = nd->mnt->mnt_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 if (!capable(CAP_SYS_ADMIN))
916 return -EPERM;
917
918 if (!check_mnt(nd->mnt))
919 return -EINVAL;
920
921 if (nd->dentry != nd->mnt->mnt_root)
922 return -EINVAL;
923
924 down_write(&sb->s_umount);
925 err = do_remount_sb(sb, flags, data, 0);
926 if (!err)
Ram Paib58fed82005-11-07 17:16:09 -0500927 nd->mnt->mnt_flags = mnt_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 up_write(&sb->s_umount);
929 if (!err)
930 security_sb_post_remount(nd->mnt, flags, data);
931 return err;
932}
933
Ram Pai9676f0c2005-11-07 17:21:20 -0500934static inline int tree_contains_unbindable(struct vfsmount *mnt)
935{
936 struct vfsmount *p;
937 for (p = mnt; p; p = next_mnt(p, mnt)) {
938 if (IS_MNT_UNBINDABLE(p))
939 return 1;
940 }
941 return 0;
942}
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944static int do_move_mount(struct nameidata *nd, char *old_name)
945{
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;
956
Ram Pai390c6842005-11-07 17:17:51 -0500957 down_write(&namespace_sem);
Ram Paib58fed82005-11-07 17:16:09 -0500958 while (d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 ;
960 err = -EINVAL;
961 if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
962 goto out;
963
964 err = -ENOENT;
965 down(&nd->dentry->d_inode->i_sem);
966 if (IS_DEADDIR(nd->dentry->d_inode))
967 goto out1;
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (!IS_ROOT(nd->dentry) && d_unhashed(nd->dentry))
Ram Pai21444402005-11-07 17:20:03 -0500970 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 err = -EINVAL;
973 if (old_nd.dentry != old_nd.mnt->mnt_root)
Ram Pai21444402005-11-07 17:20:03 -0500974 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 if (old_nd.mnt == old_nd.mnt->mnt_parent)
Ram Pai21444402005-11-07 17:20:03 -0500977 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
980 S_ISDIR(old_nd.dentry->d_inode->i_mode))
Ram Pai21444402005-11-07 17:20:03 -0500981 goto out1;
982 /*
983 * Don't move a mount residing in a shared parent.
984 */
985 if (old_nd.mnt->mnt_parent && IS_MNT_SHARED(old_nd.mnt->mnt_parent))
986 goto out1;
Ram Pai9676f0c2005-11-07 17:21:20 -0500987 /*
988 * Don't move a mount tree containing unbindable mounts to a destination
989 * mount which is shared.
990 */
991 if (IS_MNT_SHARED(nd->mnt) && tree_contains_unbindable(old_nd.mnt))
992 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 err = -ELOOP;
Ram Paib58fed82005-11-07 17:16:09 -0500994 for (p = nd->mnt; p->mnt_parent != p; p = p->mnt_parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (p == old_nd.mnt)
Ram Pai21444402005-11-07 17:20:03 -0500996 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Ram Pai21444402005-11-07 17:20:03 -0500998 if ((err = attach_recursive_mnt(old_nd.mnt, nd, &parent_nd)))
999 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Ram Pai21444402005-11-07 17:20:03 -05001001 spin_lock(&vfsmount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 /* if the mount is moved, it should no longer be expire
1003 * automatically */
Miklos Szeredi55e700b2005-07-07 17:57:30 -07001004 list_del_init(&old_nd.mnt->mnt_expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 spin_unlock(&vfsmount_lock);
1006out1:
1007 up(&nd->dentry->d_inode->i_sem);
1008out:
Ram Pai390c6842005-11-07 17:17:51 -05001009 up_write(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (!err)
1011 path_release(&parent_nd);
1012 path_release(&old_nd);
1013 return err;
1014}
1015
1016/*
1017 * create a new mount for userspace and request it to be added into the
1018 * namespace's tree
1019 */
1020static int do_new_mount(struct nameidata *nd, char *type, int flags,
1021 int mnt_flags, char *name, void *data)
1022{
1023 struct vfsmount *mnt;
1024
1025 if (!type || !memchr(type, 0, PAGE_SIZE))
1026 return -EINVAL;
1027
1028 /* we need capabilities... */
1029 if (!capable(CAP_SYS_ADMIN))
1030 return -EPERM;
1031
1032 mnt = do_kern_mount(type, flags, name, data);
1033 if (IS_ERR(mnt))
1034 return PTR_ERR(mnt);
1035
1036 return do_add_mount(mnt, nd, mnt_flags, NULL);
1037}
1038
1039/*
1040 * add a mount into a namespace's mount tree
1041 * - provide the option of adding the new mount to an expiration list
1042 */
1043int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd,
1044 int mnt_flags, struct list_head *fslist)
1045{
1046 int err;
1047
Ram Pai390c6842005-11-07 17:17:51 -05001048 down_write(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 /* Something was mounted here while we slept */
Ram Paib58fed82005-11-07 17:16:09 -05001050 while (d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 ;
1052 err = -EINVAL;
1053 if (!check_mnt(nd->mnt))
1054 goto unlock;
1055
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;
1061
1062 err = -EINVAL;
1063 if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
1064 goto unlock;
1065
1066 newmnt->mnt_flags = mnt_flags;
Ram Pai5b83d2c2005-11-07 17:16:29 -05001067 if ((err = graft_tree(newmnt, nd)))
1068 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Ram Pai5b83d2c2005-11-07 17:16:29 -05001070 if (fslist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 /* add to the specified expiration list */
1072 spin_lock(&vfsmount_lock);
Miklos Szeredi55e700b2005-07-07 17:57:30 -07001073 list_add_tail(&newmnt->mnt_expire, fslist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 spin_unlock(&vfsmount_lock);
1075 }
Ram Pai390c6842005-11-07 17:17:51 -05001076 up_write(&namespace_sem);
Ram Pai5b83d2c2005-11-07 17:16:29 -05001077 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079unlock:
Ram Pai390c6842005-11-07 17:17:51 -05001080 up_write(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 mntput(newmnt);
1082 return err;
1083}
1084
1085EXPORT_SYMBOL_GPL(do_add_mount);
1086
Ram Pai70fbcdf2005-11-07 17:17:04 -05001087static void expire_mount(struct vfsmount *mnt, struct list_head *mounts,
1088 struct list_head *umounts)
Miklos Szeredi24ca2af2005-07-07 17:57:25 -07001089{
1090 spin_lock(&vfsmount_lock);
1091
1092 /*
Miklos Szeredied42c872005-07-07 17:57:26 -07001093 * Check if mount is still attached, if not, let whoever holds it deal
1094 * with the sucker
1095 */
1096 if (mnt->mnt_parent == mnt) {
1097 spin_unlock(&vfsmount_lock);
1098 return;
1099 }
1100
1101 /*
Miklos Szeredi24ca2af2005-07-07 17:57:25 -07001102 * Check that it is still dead: the count should now be 2 - as
1103 * contributed by the vfsmount parent and the mntget above
1104 */
Ram Paia05964f2005-11-07 17:20:17 -05001105 if (!propagate_mount_busy(mnt, 2)) {
Miklos Szeredi24ca2af2005-07-07 17:57:25 -07001106 /* delete from the namespace */
Al Viro5addc5d2005-11-07 17:15:49 -05001107 touch_namespace(mnt->mnt_namespace);
Miklos Szeredi24ca2af2005-07-07 17:57:25 -07001108 list_del_init(&mnt->mnt_list);
Miklos Szerediac081152005-07-07 17:57:27 -07001109 mnt->mnt_namespace = NULL;
Ram Paia05964f2005-11-07 17:20:17 -05001110 umount_tree(mnt, 1, umounts);
Miklos Szeredi24ca2af2005-07-07 17:57:25 -07001111 spin_unlock(&vfsmount_lock);
Miklos Szeredi24ca2af2005-07-07 17:57:25 -07001112 } else {
1113 /*
1114 * Someone brought it back to life whilst we didn't have any
1115 * locks held so return it to the expiration list
1116 */
Miklos Szeredi55e700b2005-07-07 17:57:30 -07001117 list_add_tail(&mnt->mnt_expire, mounts);
Miklos Szeredi24ca2af2005-07-07 17:57:25 -07001118 spin_unlock(&vfsmount_lock);
1119 }
1120}
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122/*
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
1126 */
1127void mark_mounts_for_expiry(struct list_head *mounts)
1128{
1129 struct namespace *namespace;
1130 struct vfsmount *mnt, *next;
1131 LIST_HEAD(graveyard);
1132
1133 if (list_empty(mounts))
1134 return;
1135
1136 spin_lock(&vfsmount_lock);
1137
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())
1143 */
Miklos Szeredi55e700b2005-07-07 17:57:30 -07001144 list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
1146 atomic_read(&mnt->mnt_count) != 1)
1147 continue;
1148
1149 mntget(mnt);
Miklos Szeredi55e700b2005-07-07 17:57:30 -07001150 list_move(&mnt->mnt_expire, &graveyard);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
1152
1153 /*
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
1158 */
1159 while (!list_empty(&graveyard)) {
Ram Pai70fbcdf2005-11-07 17:17:04 -05001160 LIST_HEAD(umounts);
Miklos Szeredi55e700b2005-07-07 17:57:30 -07001161 mnt = list_entry(graveyard.next, struct vfsmount, mnt_expire);
1162 list_del_init(&mnt->mnt_expire);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
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;
Miklos Szeredi1ce88cf2005-07-07 17:57:24 -07001167 if (!namespace || !namespace->root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 continue;
1169 get_namespace(namespace);
1170
1171 spin_unlock(&vfsmount_lock);
Ram Pai390c6842005-11-07 17:17:51 -05001172 down_write(&namespace_sem);
Ram Pai70fbcdf2005-11-07 17:17:04 -05001173 expire_mount(mnt, mounts, &umounts);
Ram Pai390c6842005-11-07 17:17:51 -05001174 up_write(&namespace_sem);
Ram Pai70fbcdf2005-11-07 17:17:04 -05001175 release_mounts(&umounts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 mntput(mnt);
1177 put_namespace(namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 spin_lock(&vfsmount_lock);
1179 }
1180
1181 spin_unlock(&vfsmount_lock);
1182}
1183
1184EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
1185
1186/*
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.
1191 */
Ram Paib58fed82005-11-07 17:16:09 -05001192static long exact_copy_from_user(void *to, const void __user * from,
1193 unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
1195 char *t = to;
1196 const char __user *f = from;
1197 char c;
1198
1199 if (!access_ok(VERIFY_READ, from, n))
1200 return n;
1201
1202 while (n) {
1203 if (__get_user(c, f)) {
1204 memset(t, 0, n);
1205 break;
1206 }
1207 *t++ = c;
1208 f++;
1209 n--;
1210 }
1211 return n;
1212}
1213
Ram Paib58fed82005-11-07 17:16:09 -05001214int copy_mount_options(const void __user * data, unsigned long *where)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
1216 int i;
1217 unsigned long page;
1218 unsigned long size;
Ram Paib58fed82005-11-07 17:16:09 -05001219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 *where = 0;
1221 if (!data)
1222 return 0;
1223
1224 if (!(page = __get_free_page(GFP_KERNEL)))
1225 return -ENOMEM;
1226
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.
1230 */
1231 /* copy_from_user cannot cross TASK_SIZE ! */
1232 size = TASK_SIZE - (unsigned long)data;
1233 if (size > PAGE_SIZE)
1234 size = PAGE_SIZE;
1235
1236 i = size - exact_copy_from_user((void *)page, data, size);
1237 if (!i) {
Ram Paib58fed82005-11-07 17:16:09 -05001238 free_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return -EFAULT;
1240 }
1241 if (i != PAGE_SIZE)
1242 memset((char *)page + i, 0, PAGE_SIZE - i);
1243 *where = page;
1244 return 0;
1245}
1246
1247/*
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).
1250 *
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).
1254 *
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.
1260 */
Ram Paib58fed82005-11-07 17:16:09 -05001261long do_mount(char *dev_name, char *dir_name, char *type_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 unsigned long flags, void *data_page)
1263{
1264 struct nameidata nd;
1265 int retval = 0;
1266 int mnt_flags = 0;
1267
1268 /* Discard magic */
1269 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
1270 flags &= ~MS_MGC_MSK;
1271
1272 /* Basic sanity checks */
1273
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;
1278
1279 if (data_page)
1280 ((char *)data_page)[PAGE_SIZE - 1] = 0;
1281
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;
Ram Paib58fed82005-11-07 17:16:09 -05001289 flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 /* ... and get the mountpoint */
1292 retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
1293 if (retval)
1294 return retval;
1295
1296 retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
1297 if (retval)
1298 goto dput_out;
1299
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);
Ram Pai9676f0c2005-11-07 17:21:20 -05001305 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
Ram Pai07b20882005-11-07 17:19:07 -05001306 retval = do_change_type(&nd, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 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);
1312dput_out:
1313 path_release(&nd);
1314 return retval;
1315}
1316
1317int copy_namespace(int flags, struct task_struct *tsk)
1318{
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;
1324
1325 if (!namespace)
1326 return 0;
1327
1328 get_namespace(namespace);
1329
1330 if (!(flags & CLONE_NEWNS))
1331 return 0;
1332
1333 if (!capable(CAP_SYS_ADMIN)) {
1334 put_namespace(namespace);
1335 return -EPERM;
1336 }
1337
1338 new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL);
1339 if (!new_ns)
1340 goto out;
1341
1342 atomic_set(&new_ns->count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 INIT_LIST_HEAD(&new_ns->list);
Al Viro5addc5d2005-11-07 17:15:49 -05001344 init_waitqueue_head(&new_ns->poll);
1345 new_ns->event = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Ram Pai390c6842005-11-07 17:17:51 -05001347 down_write(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 /* First pass: copy the tree topology */
Ram Pai36341f62005-11-07 17:17:22 -05001349 new_ns->root = copy_tree(namespace->root, namespace->root->mnt_root,
Ram Pai9676f0c2005-11-07 17:21:20 -05001350 CL_COPY_ALL | CL_EXPIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 if (!new_ns->root) {
Ram Pai390c6842005-11-07 17:17:51 -05001352 up_write(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 kfree(new_ns);
1354 goto out;
1355 }
1356 spin_lock(&vfsmount_lock);
1357 list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
1358 spin_unlock(&vfsmount_lock);
1359
1360 /*
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.
1364 */
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);
1373 }
1374 if (p == fs->pwdmnt) {
1375 pwdmnt = p;
1376 fs->pwdmnt = mntget(q);
1377 }
1378 if (p == fs->altrootmnt) {
1379 altrootmnt = p;
1380 fs->altrootmnt = mntget(q);
1381 }
1382 }
1383 p = next_mnt(p, namespace->root);
1384 q = next_mnt(q, new_ns->root);
1385 }
Ram Pai390c6842005-11-07 17:17:51 -05001386 up_write(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 tsk->namespace = new_ns;
1389
1390 if (rootmnt)
1391 mntput(rootmnt);
1392 if (pwdmnt)
1393 mntput(pwdmnt);
1394 if (altrootmnt)
1395 mntput(altrootmnt);
1396
1397 put_namespace(namespace);
1398 return 0;
1399
1400out:
1401 put_namespace(namespace);
1402 return -ENOMEM;
1403}
1404
1405asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
1406 char __user * type, unsigned long flags,
1407 void __user * data)
1408{
1409 int retval;
1410 unsigned long data_page;
1411 unsigned long type_page;
1412 unsigned long dev_page;
1413 char *dir_page;
1414
Ram Paib58fed82005-11-07 17:16:09 -05001415 retval = copy_mount_options(type, &type_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (retval < 0)
1417 return retval;
1418
1419 dir_page = getname(dir_name);
1420 retval = PTR_ERR(dir_page);
1421 if (IS_ERR(dir_page))
1422 goto out1;
1423
Ram Paib58fed82005-11-07 17:16:09 -05001424 retval = copy_mount_options(dev_name, &dev_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (retval < 0)
1426 goto out2;
1427
Ram Paib58fed82005-11-07 17:16:09 -05001428 retval = copy_mount_options(data, &data_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 if (retval < 0)
1430 goto out3;
1431
1432 lock_kernel();
Ram Paib58fed82005-11-07 17:16:09 -05001433 retval = do_mount((char *)dev_page, dir_page, (char *)type_page,
1434 flags, (void *)data_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 unlock_kernel();
1436 free_page(data_page);
1437
1438out3:
1439 free_page(dev_page);
1440out2:
1441 putname(dir_page);
1442out1:
1443 free_page(type_page);
1444 return retval;
1445}
1446
1447/*
1448 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
1449 * It can block. Requires the big lock held.
1450 */
1451void set_fs_root(struct fs_struct *fs, struct vfsmount *mnt,
1452 struct dentry *dentry)
1453{
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);
1465 }
1466}
1467
1468/*
1469 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
1470 * It can block. Requires the big lock held.
1471 */
1472void set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
1473 struct dentry *dentry)
1474{
1475 struct dentry *old_pwd;
1476 struct vfsmount *old_pwdmnt;
1477
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);
1484
1485 if (old_pwd) {
1486 dput(old_pwd);
1487 mntput(old_pwdmnt);
1488 }
1489}
1490
1491static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
1492{
1493 struct task_struct *g, *p;
1494 struct fs_struct *fs;
1495
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);
Ram Paib58fed82005-11-07 17:16:09 -05001503 if (fs->root == old_nd->dentry
1504 && fs->rootmnt == old_nd->mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 set_fs_root(fs, new_nd->mnt, new_nd->dentry);
Ram Paib58fed82005-11-07 17:16:09 -05001506 if (fs->pwd == old_nd->dentry
1507 && fs->pwdmnt == old_nd->mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 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);
1514}
1515
1516/*
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.
1521 *
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.
1528 *
1529 * Notes:
1530 * - we don't move root/cwd if they are not at the root (reason: if something
1531 * cared enough to change them, it's probably wrong to force them elsewhere)
1532 * - it's okay to pick a root that isn't the root of a file system, e.g.
1533 * /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1534 * though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1535 * first.
1536 */
Ram Paib58fed82005-11-07 17:16:09 -05001537asmlinkage long sys_pivot_root(const char __user * new_root,
1538 const char __user * put_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
1540 struct vfsmount *tmp;
1541 struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd;
1542 int error;
1543
1544 if (!capable(CAP_SYS_ADMIN))
1545 return -EPERM;
1546
1547 lock_kernel();
1548
Ram Paib58fed82005-11-07 17:16:09 -05001549 error = __user_walk(new_root, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
1550 &new_nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if (error)
1552 goto out0;
1553 error = -EINVAL;
1554 if (!check_mnt(new_nd.mnt))
1555 goto out1;
1556
Ram Paib58fed82005-11-07 17:16:09 -05001557 error = __user_walk(put_old, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old_nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if (error)
1559 goto out1;
1560
1561 error = security_sb_pivotroot(&old_nd, &new_nd);
1562 if (error) {
1563 path_release(&old_nd);
1564 goto out1;
1565 }
1566
1567 read_lock(&current->fs->lock);
1568 user_nd.mnt = mntget(current->fs->rootmnt);
1569 user_nd.dentry = dget(current->fs->root);
1570 read_unlock(&current->fs->lock);
Ram Pai390c6842005-11-07 17:17:51 -05001571 down_write(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 down(&old_nd.dentry->d_inode->i_sem);
1573 error = -EINVAL;
Ram Pai21444402005-11-07 17:20:03 -05001574 if (IS_MNT_SHARED(old_nd.mnt) ||
1575 IS_MNT_SHARED(new_nd.mnt->mnt_parent) ||
1576 IS_MNT_SHARED(user_nd.mnt->mnt_parent))
1577 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 if (!check_mnt(user_nd.mnt))
1579 goto out2;
1580 error = -ENOENT;
1581 if (IS_DEADDIR(new_nd.dentry->d_inode))
1582 goto out2;
1583 if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
1584 goto out2;
1585 if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
1586 goto out2;
1587 error = -EBUSY;
1588 if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt)
1589 goto out2; /* loop, on the same file system */
1590 error = -EINVAL;
1591 if (user_nd.mnt->mnt_root != user_nd.dentry)
1592 goto out2; /* not a mountpoint */
Miklos Szeredi0bb6fcc2005-09-06 15:19:36 -07001593 if (user_nd.mnt->mnt_parent == user_nd.mnt)
1594 goto out2; /* not attached */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if (new_nd.mnt->mnt_root != new_nd.dentry)
1596 goto out2; /* not a mountpoint */
Miklos Szeredi0bb6fcc2005-09-06 15:19:36 -07001597 if (new_nd.mnt->mnt_parent == new_nd.mnt)
1598 goto out2; /* not attached */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
1600 spin_lock(&vfsmount_lock);
1601 if (tmp != new_nd.mnt) {
1602 for (;;) {
1603 if (tmp->mnt_parent == tmp)
1604 goto out3; /* already mounted on put_old */
1605 if (tmp->mnt_parent == new_nd.mnt)
1606 break;
1607 tmp = tmp->mnt_parent;
1608 }
1609 if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
1610 goto out3;
1611 } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
1612 goto out3;
1613 detach_mnt(new_nd.mnt, &parent_nd);
1614 detach_mnt(user_nd.mnt, &root_parent);
1615 attach_mnt(user_nd.mnt, &old_nd); /* mount old root on put_old */
1616 attach_mnt(new_nd.mnt, &root_parent); /* mount new_root on / */
Al Viro5addc5d2005-11-07 17:15:49 -05001617 touch_namespace(current->namespace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 spin_unlock(&vfsmount_lock);
1619 chroot_fs_refs(&user_nd, &new_nd);
1620 security_sb_post_pivotroot(&user_nd, &new_nd);
1621 error = 0;
1622 path_release(&root_parent);
1623 path_release(&parent_nd);
1624out2:
1625 up(&old_nd.dentry->d_inode->i_sem);
Ram Pai390c6842005-11-07 17:17:51 -05001626 up_write(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 path_release(&user_nd);
1628 path_release(&old_nd);
1629out1:
1630 path_release(&new_nd);
1631out0:
1632 unlock_kernel();
1633 return error;
1634out3:
1635 spin_unlock(&vfsmount_lock);
1636 goto out2;
1637}
1638
1639static void __init init_mount_tree(void)
1640{
1641 struct vfsmount *mnt;
1642 struct namespace *namespace;
1643 struct task_struct *g, *p;
1644
1645 mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
1646 if (IS_ERR(mnt))
1647 panic("Can't create rootfs");
1648 namespace = kmalloc(sizeof(*namespace), GFP_KERNEL);
1649 if (!namespace)
1650 panic("Can't allocate initial namespace");
1651 atomic_set(&namespace->count, 1);
1652 INIT_LIST_HEAD(&namespace->list);
Al Viro5addc5d2005-11-07 17:15:49 -05001653 init_waitqueue_head(&namespace->poll);
1654 namespace->event = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 list_add(&mnt->mnt_list, &namespace->list);
1656 namespace->root = mnt;
1657 mnt->mnt_namespace = namespace;
1658
1659 init_task.namespace = namespace;
1660 read_lock(&tasklist_lock);
1661 do_each_thread(g, p) {
1662 get_namespace(namespace);
1663 p->namespace = namespace;
1664 } while_each_thread(g, p);
1665 read_unlock(&tasklist_lock);
1666
1667 set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root);
1668 set_fs_root(current->fs, namespace->root, namespace->root->mnt_root);
1669}
1670
1671void __init mnt_init(unsigned long mempages)
1672{
1673 struct list_head *d;
1674 unsigned int nr_hash;
1675 int i;
1676
Ram Pai390c6842005-11-07 17:17:51 -05001677 init_rwsem(&namespace_sem);
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
Ram Paib58fed82005-11-07 17:16:09 -05001680 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Ram Paib58fed82005-11-07 17:16:09 -05001682 mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
1684 if (!mount_hashtable)
1685 panic("Failed to allocate mount hash table\n");
1686
1687 /*
1688 * Find the power-of-two list-heads that can fit into the allocation..
1689 * We don't guarantee that "sizeof(struct list_head)" is necessarily
1690 * a power-of-two.
1691 */
1692 nr_hash = PAGE_SIZE / sizeof(struct list_head);
1693 hash_bits = 0;
1694 do {
1695 hash_bits++;
1696 } while ((nr_hash >> hash_bits) != 0);
1697 hash_bits--;
1698
1699 /*
1700 * Re-calculate the actual number of entries and the mask
1701 * from the number of bits we can fit.
1702 */
1703 nr_hash = 1UL << hash_bits;
Ram Paib58fed82005-11-07 17:16:09 -05001704 hash_mask = nr_hash - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 printk("Mount-cache hash table entries: %d\n", nr_hash);
1707
1708 /* And initialize the newly allocated array */
1709 d = mount_hashtable;
1710 i = nr_hash;
1711 do {
1712 INIT_LIST_HEAD(d);
1713 d++;
1714 i--;
1715 } while (i);
1716 sysfs_init();
1717 init_rootfs();
1718 init_mount_tree();
1719}
1720
1721void __put_namespace(struct namespace *namespace)
1722{
Miklos Szeredi1ce88cf2005-07-07 17:57:24 -07001723 struct vfsmount *root = namespace->root;
Ram Pai70fbcdf2005-11-07 17:17:04 -05001724 LIST_HEAD(umount_list);
Miklos Szeredi1ce88cf2005-07-07 17:57:24 -07001725 namespace->root = NULL;
1726 spin_unlock(&vfsmount_lock);
Ram Pai390c6842005-11-07 17:17:51 -05001727 down_write(&namespace_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 spin_lock(&vfsmount_lock);
Ram Paia05964f2005-11-07 17:20:17 -05001729 umount_tree(root, 0, &umount_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 spin_unlock(&vfsmount_lock);
Ram Pai390c6842005-11-07 17:17:51 -05001731 up_write(&namespace_sem);
Ram Pai70fbcdf2005-11-07 17:17:04 -05001732 release_mounts(&umount_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 kfree(namespace);
1734}