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