backported vs2.1.x fix to irq handling, which caused incorrect scheduler behavior
[linux-2.6.git] / fs / namespace.c
1 /*
2  *  linux/fs/namespace.c
3  *
4  * (C) Copyright Al Viro 2000, 2001
5  *      Released under GPL v2.
6  *
7  * Based on code from fs/super.c, copyright Linus Torvalds and others.
8  * Heavily rewritten.
9  */
10
11 #include <linux/syscalls.h>
12 #include <linux/slab.h>
13 #include <linux/sched.h>
14 #include <linux/smp_lock.h>
15 #include <linux/init.h>
16 #include <linux/quotaops.h>
17 #include <linux/acct.h>
18 #include <linux/capability.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 <linux/vs_base.h>
26 #include <linux/vserver/namespace.h>
27 #include <linux/vserver/xid.h>
28 #include <asm/uaccess.h>
29 #include <asm/unistd.h>
30 #include "pnode.h"
31
32 extern int __init init_rootfs(void);
33
34 #ifdef CONFIG_SYSFS
35 extern int __init sysfs_init(void);
36 #else
37 static inline int sysfs_init(void)
38 {
39         return 0;
40 }
41 #endif
42
43 /* spinlock for vfsmount related operations, inplace of dcache_lock */
44 __cacheline_aligned_in_smp DEFINE_SPINLOCK(vfsmount_lock);
45
46 static int event;
47
48 static struct list_head *mount_hashtable __read_mostly;
49 static int hash_mask __read_mostly, hash_bits __read_mostly;
50 static kmem_cache_t *mnt_cache __read_mostly;
51 static struct rw_semaphore namespace_sem;
52
53 /* /sys/fs */
54 decl_subsys(fs, NULL, NULL);
55 EXPORT_SYMBOL_GPL(fs_subsys);
56
57 static inline unsigned long hash(struct vfsmount *mnt, struct dentry *dentry)
58 {
59         unsigned long tmp = ((unsigned long)mnt / L1_CACHE_BYTES);
60         tmp += ((unsigned long)dentry / L1_CACHE_BYTES);
61         tmp = tmp + (tmp >> hash_bits);
62         return tmp & hash_mask;
63 }
64
65 struct vfsmount *alloc_vfsmnt(const char *name)
66 {
67         struct vfsmount *mnt = kmem_cache_alloc(mnt_cache, GFP_KERNEL);
68         if (mnt) {
69                 memset(mnt, 0, sizeof(struct vfsmount));
70                 atomic_set(&mnt->mnt_count, 1);
71                 INIT_LIST_HEAD(&mnt->mnt_hash);
72                 INIT_LIST_HEAD(&mnt->mnt_child);
73                 INIT_LIST_HEAD(&mnt->mnt_mounts);
74                 INIT_LIST_HEAD(&mnt->mnt_list);
75                 INIT_LIST_HEAD(&mnt->mnt_expire);
76                 INIT_LIST_HEAD(&mnt->mnt_share);
77                 INIT_LIST_HEAD(&mnt->mnt_slave_list);
78                 INIT_LIST_HEAD(&mnt->mnt_slave);
79                 if (name) {
80                         int size = strlen(name) + 1;
81                         char *newname = kmalloc(size, GFP_KERNEL);
82                         if (newname) {
83                                 memcpy(newname, name, size);
84                                 mnt->mnt_devname = newname;
85                         }
86                 }
87         }
88         return mnt;
89 }
90
91 int simple_set_mnt(struct vfsmount *mnt, struct super_block *sb)
92 {
93         mnt->mnt_sb = sb;
94         mnt->mnt_root = dget(sb->s_root);
95         return 0;
96 }
97
98 EXPORT_SYMBOL(simple_set_mnt);
99
100 void free_vfsmnt(struct vfsmount *mnt)
101 {
102         kfree(mnt->mnt_devname);
103         kmem_cache_free(mnt_cache, mnt);
104 }
105
106 /*
107  * find the first or last mount at @dentry on vfsmount @mnt depending on
108  * @dir. If @dir is set return the first mount else return the last mount.
109  */
110 struct vfsmount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry,
111                               int dir)
112 {
113         struct list_head *head = mount_hashtable + hash(mnt, dentry);
114         struct list_head *tmp = head;
115         struct vfsmount *p, *found = NULL;
116
117         for (;;) {
118                 tmp = dir ? tmp->next : tmp->prev;
119                 p = NULL;
120                 if (tmp == head)
121                         break;
122                 p = list_entry(tmp, struct vfsmount, mnt_hash);
123                 if (p->mnt_parent == mnt && p->mnt_mountpoint == dentry) {
124                         found = p;
125                         break;
126                 }
127         }
128         return found;
129 }
130
131 /*
132  * lookup_mnt increments the ref count before returning
133  * the vfsmount struct.
134  */
135 struct vfsmount *lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
136 {
137         struct vfsmount *child_mnt;
138         spin_lock(&vfsmount_lock);
139         if ((child_mnt = __lookup_mnt(mnt, dentry, 1)))
140                 mntget(child_mnt);
141         spin_unlock(&vfsmount_lock);
142         return child_mnt;
143 }
144
145 static inline int check_mnt(struct vfsmount *mnt)
146 {
147         return mnt->mnt_namespace == current->namespace;
148 }
149
150 static void touch_namespace(struct namespace *ns)
151 {
152         if (ns) {
153                 ns->event = ++event;
154                 wake_up_interruptible(&ns->poll);
155         }
156 }
157
158 static void __touch_namespace(struct namespace *ns)
159 {
160         if (ns && ns->event != event) {
161                 ns->event = event;
162                 wake_up_interruptible(&ns->poll);
163         }
164 }
165
166 static void detach_mnt(struct vfsmount *mnt, struct nameidata *old_nd)
167 {
168         old_nd->dentry = mnt->mnt_mountpoint;
169         old_nd->mnt = mnt->mnt_parent;
170         mnt->mnt_parent = mnt;
171         mnt->mnt_mountpoint = mnt->mnt_root;
172         list_del_init(&mnt->mnt_child);
173         list_del_init(&mnt->mnt_hash);
174         old_nd->dentry->d_mounted--;
175 }
176
177 void mnt_set_mountpoint(struct vfsmount *mnt, struct dentry *dentry,
178                         struct vfsmount *child_mnt)
179 {
180         child_mnt->mnt_parent = mntget(mnt);
181         child_mnt->mnt_mountpoint = dget(dentry);
182         dentry->d_mounted++;
183 }
184
185 static void attach_mnt(struct vfsmount *mnt, struct nameidata *nd)
186 {
187         mnt_set_mountpoint(nd->mnt, nd->dentry, mnt);
188         list_add_tail(&mnt->mnt_hash, mount_hashtable +
189                         hash(nd->mnt, nd->dentry));
190         list_add_tail(&mnt->mnt_child, &nd->mnt->mnt_mounts);
191 }
192
193 /*
194  * the caller must hold vfsmount_lock
195  */
196 static void commit_tree(struct vfsmount *mnt)
197 {
198         struct vfsmount *parent = mnt->mnt_parent;
199         struct vfsmount *m;
200         LIST_HEAD(head);
201         struct namespace *n = parent->mnt_namespace;
202
203         BUG_ON(parent == mnt);
204
205         list_add_tail(&head, &mnt->mnt_list);
206         list_for_each_entry(m, &head, mnt_list)
207                 m->mnt_namespace = n;
208         list_splice(&head, n->list.prev);
209
210         list_add_tail(&mnt->mnt_hash, mount_hashtable +
211                                 hash(parent, mnt->mnt_mountpoint));
212         list_add_tail(&mnt->mnt_child, &parent->mnt_mounts);
213         touch_namespace(n);
214 }
215
216 static struct vfsmount *next_mnt(struct vfsmount *p, struct vfsmount *root)
217 {
218         struct list_head *next = p->mnt_mounts.next;
219         if (next == &p->mnt_mounts) {
220                 while (1) {
221                         if (p == root)
222                                 return NULL;
223                         next = p->mnt_child.next;
224                         if (next != &p->mnt_parent->mnt_mounts)
225                                 break;
226                         p = p->mnt_parent;
227                 }
228         }
229         return list_entry(next, struct vfsmount, mnt_child);
230 }
231
232 static struct vfsmount *skip_mnt_tree(struct vfsmount *p)
233 {
234         struct list_head *prev = p->mnt_mounts.prev;
235         while (prev != &p->mnt_mounts) {
236                 p = list_entry(prev, struct vfsmount, mnt_child);
237                 prev = p->mnt_mounts.prev;
238         }
239         return p;
240 }
241
242 static struct vfsmount *clone_mnt(struct vfsmount *old, struct dentry *root,
243                                         int flag)
244 {
245         struct super_block *sb = old->mnt_sb;
246         struct vfsmount *mnt = alloc_vfsmnt(old->mnt_devname);
247
248         if (mnt) {
249                 mnt->mnt_flags = old->mnt_flags;
250                 atomic_inc(&sb->s_active);
251                 mnt->mnt_sb = sb;
252                 mnt->mnt_root = dget(root);
253                 mnt->mnt_mountpoint = mnt->mnt_root;
254                 mnt->mnt_parent = mnt;
255                 mnt->mnt_xid = old->mnt_xid;
256
257                 if (flag & CL_SLAVE) {
258                         list_add(&mnt->mnt_slave, &old->mnt_slave_list);
259                         mnt->mnt_master = old;
260                         CLEAR_MNT_SHARED(mnt);
261                 } else {
262                         if ((flag & CL_PROPAGATION) || IS_MNT_SHARED(old))
263                                 list_add(&mnt->mnt_share, &old->mnt_share);
264                         if (IS_MNT_SLAVE(old))
265                                 list_add(&mnt->mnt_slave, &old->mnt_slave);
266                         mnt->mnt_master = old->mnt_master;
267                 }
268                 if (flag & CL_MAKE_SHARED)
269                         set_mnt_shared(mnt);
270
271                 /* stick the duplicate mount on the same expiry list
272                  * as the original if that was on one */
273                 if (flag & CL_EXPIRE) {
274                         spin_lock(&vfsmount_lock);
275                         if (!list_empty(&old->mnt_expire))
276                                 list_add(&mnt->mnt_expire, &old->mnt_expire);
277                         spin_unlock(&vfsmount_lock);
278                 }
279         }
280         return mnt;
281 }
282
283 static inline void __mntput(struct vfsmount *mnt)
284 {
285         struct super_block *sb = mnt->mnt_sb;
286         dput(mnt->mnt_root);
287         free_vfsmnt(mnt);
288         deactivate_super(sb);
289 }
290
291 void mntput_no_expire(struct vfsmount *mnt)
292 {
293 repeat:
294         if (atomic_dec_and_lock(&mnt->mnt_count, &vfsmount_lock)) {
295                 if (likely(!mnt->mnt_pinned)) {
296                         spin_unlock(&vfsmount_lock);
297                         __mntput(mnt);
298                         return;
299                 }
300                 atomic_add(mnt->mnt_pinned + 1, &mnt->mnt_count);
301                 mnt->mnt_pinned = 0;
302                 spin_unlock(&vfsmount_lock);
303                 acct_auto_close_mnt(mnt);
304                 security_sb_umount_close(mnt);
305                 goto repeat;
306         }
307 }
308
309 EXPORT_SYMBOL(mntput_no_expire);
310
311 void mnt_pin(struct vfsmount *mnt)
312 {
313         spin_lock(&vfsmount_lock);
314         mnt->mnt_pinned++;
315         spin_unlock(&vfsmount_lock);
316 }
317
318 EXPORT_SYMBOL(mnt_pin);
319
320 void mnt_unpin(struct vfsmount *mnt)
321 {
322         spin_lock(&vfsmount_lock);
323         if (mnt->mnt_pinned) {
324                 atomic_inc(&mnt->mnt_count);
325                 mnt->mnt_pinned--;
326         }
327         spin_unlock(&vfsmount_lock);
328 }
329
330 EXPORT_SYMBOL(mnt_unpin);
331
332 /* iterator */
333 static void *m_start(struct seq_file *m, loff_t *pos)
334 {
335         struct namespace *n = m->private;
336         struct list_head *p;
337         loff_t l = *pos;
338
339         down_read(&namespace_sem);
340         list_for_each(p, &n->list)
341                 if (!l--)
342                         return list_entry(p, struct vfsmount, mnt_list);
343         return NULL;
344 }
345
346 static void *m_next(struct seq_file *m, void *v, loff_t *pos)
347 {
348         struct namespace *n = m->private;
349         struct list_head *p = ((struct vfsmount *)v)->mnt_list.next;
350         (*pos)++;
351         return p == &n->list ? NULL : list_entry(p, struct vfsmount, mnt_list);
352 }
353
354 static void m_stop(struct seq_file *m, void *v)
355 {
356         up_read(&namespace_sem);
357 }
358
359 static inline void mangle(struct seq_file *m, const char *s)
360 {
361         seq_escape(m, s, " \t\n\\");
362 }
363
364 static int mnt_is_reachable(struct vfsmount *mnt)
365 {
366         struct vfsmount *root_mnt;
367         struct dentry *root, *point;
368         int ret;
369
370         if (mnt == mnt->mnt_namespace->root)
371                 return 1;
372
373         spin_lock(&dcache_lock);
374         root_mnt = current->fs->rootmnt;
375         root = current->fs->root;
376         point = root;
377
378         while ((mnt != mnt->mnt_parent) && (mnt != root_mnt)) {
379                 point = mnt->mnt_mountpoint;
380                 mnt = mnt->mnt_parent;
381         }
382
383         ret = (mnt == root_mnt) && is_subdir(point, root);
384
385         spin_unlock(&dcache_lock);
386
387         return ret;
388 }
389
390 static int show_vfsmnt(struct seq_file *m, void *v)
391 {
392         struct vfsmount *mnt = v;
393         int err = 0;
394         static struct proc_fs_info {
395                 int s_flag;
396                 int mnt_flag;
397                 char *set_str;
398                 char *unset_str;
399         } fs_info[] = {
400                 { MS_RDONLY, MNT_RDONLY, "ro", "rw" },
401                 { MS_SYNCHRONOUS, 0, ",sync", NULL },
402                 { MS_DIRSYNC, 0, ",dirsync", NULL },
403                 { MS_MANDLOCK, 0, ",mand", NULL },
404                 { MS_TAGXID, 0, ",tagxid", NULL },
405                 { MS_NOATIME, MNT_NOATIME, ",noatime", NULL },
406                 { MS_NODIRATIME, MNT_NODIRATIME, ",nodiratime", NULL },
407                 { 0, MNT_NOSUID, ",nosuid", NULL },
408                 { 0, MNT_NODEV, ",nodev", NULL },
409                 { 0, MNT_NOEXEC, ",noexec", NULL },
410                 { 0, 0, NULL, NULL }
411         };
412         struct proc_fs_info *p;
413         unsigned long s_flags = mnt->mnt_sb->s_flags;
414         int mnt_flags = mnt->mnt_flags;
415
416         if (vx_flags(VXF_HIDE_MOUNT, 0))
417                 return 0;
418         if (!mnt_is_reachable(mnt))
419                 return 0;
420
421         if (!vx_check(0, VX_ADMIN|VX_WATCH) &&
422                 mnt == current->fs->rootmnt) {
423                 seq_puts(m, "/dev/root / ");
424         } else {
425                 mangle(m, mnt->mnt_devname ? mnt->mnt_devname : "none");
426                 seq_putc(m, ' ');
427                 seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
428                 seq_putc(m, ' ');
429         }
430         mangle(m, mnt->mnt_sb->s_type->name);
431         seq_putc(m, ' ');
432         for (p = fs_info; (p->s_flag | p->mnt_flag) ; p++) {
433                 if ((s_flags & p->s_flag) || (mnt_flags & p->mnt_flag)) {
434                         if (p->set_str)
435                                 seq_puts(m, p->set_str);
436                 } else {
437                         if (p->unset_str)
438                                 seq_puts(m, p->unset_str);
439                 }
440         }
441         if (mnt->mnt_flags & MNT_XID)
442                 seq_printf(m, ",xid=%d", mnt->mnt_xid);
443         if (mnt->mnt_sb->s_op->show_options)
444                 err = mnt->mnt_sb->s_op->show_options(m, mnt);
445         seq_puts(m, " 0 0\n");
446         return err;
447 }
448
449 struct seq_operations mounts_op = {
450         .start  = m_start,
451         .next   = m_next,
452         .stop   = m_stop,
453         .show   = show_vfsmnt
454 };
455
456 static int show_vfsstat(struct seq_file *m, void *v)
457 {
458         struct vfsmount *mnt = v;
459         int err = 0;
460
461         if (vx_flags(VXF_HIDE_MOUNT, 0))
462                 return 0;
463         if (!mnt_is_reachable(mnt) && !vx_check(0, VX_WATCH))
464                 return 0;
465
466         if (!vx_check(0, VX_ADMIN|VX_WATCH) &&
467                 mnt == current->fs->rootmnt) {
468                 seq_puts(m, "device /dev/root mounted on / ");
469         } else {
470                 /* device */
471                 if (mnt->mnt_devname) {
472                         seq_puts(m, "device ");
473                         mangle(m, mnt->mnt_devname);
474                 } else
475                         seq_puts(m, "no device");
476
477                 /* mount point */
478                 seq_puts(m, " mounted on ");
479                 seq_path(m, mnt, mnt->mnt_root, " \t\n\\");
480                 seq_putc(m, ' ');
481         }
482
483         /* file system type */
484         seq_puts(m, "with fstype ");
485         mangle(m, mnt->mnt_sb->s_type->name);
486
487         /* optional statistics */
488         if (mnt->mnt_sb->s_op->show_stats) {
489                 seq_putc(m, ' ');
490                 err = mnt->mnt_sb->s_op->show_stats(m, mnt);
491         }
492
493         seq_putc(m, '\n');
494         return err;
495 }
496
497 struct seq_operations mountstats_op = {
498         .start  = m_start,
499         .next   = m_next,
500         .stop   = m_stop,
501         .show   = show_vfsstat,
502 };
503
504 /**
505  * may_umount_tree - check if a mount tree is busy
506  * @mnt: root of mount tree
507  *
508  * This is called to check if a tree of mounts has any
509  * open files, pwds, chroots or sub mounts that are
510  * busy.
511  */
512 int may_umount_tree(struct vfsmount *mnt)
513 {
514         int actual_refs = 0;
515         int minimum_refs = 0;
516         struct vfsmount *p;
517
518         spin_lock(&vfsmount_lock);
519         for (p = mnt; p; p = next_mnt(p, mnt)) {
520                 actual_refs += atomic_read(&p->mnt_count);
521                 minimum_refs += 2;
522         }
523         spin_unlock(&vfsmount_lock);
524
525         if (actual_refs > minimum_refs)
526                 return 0;
527
528         return 1;
529 }
530
531 EXPORT_SYMBOL(may_umount_tree);
532
533 /**
534  * may_umount - check if a mount point is busy
535  * @mnt: root of mount
536  *
537  * This is called to check if a mount point has any
538  * open files, pwds, chroots or sub mounts. If the
539  * mount has sub mounts this will return busy
540  * regardless of whether the sub mounts are busy.
541  *
542  * Doesn't take quota and stuff into account. IOW, in some cases it will
543  * give false negatives. The main reason why it's here is that we need
544  * a non-destructive way to look for easily umountable filesystems.
545  */
546 int may_umount(struct vfsmount *mnt)
547 {
548         int ret = 1;
549         spin_lock(&vfsmount_lock);
550         if (propagate_mount_busy(mnt, 2))
551                 ret = 0;
552         spin_unlock(&vfsmount_lock);
553         return ret;
554 }
555
556 EXPORT_SYMBOL(may_umount);
557
558 void release_mounts(struct list_head *head)
559 {
560         struct vfsmount *mnt;
561         while (!list_empty(head)) {
562                 mnt = list_entry(head->next, struct vfsmount, mnt_hash);
563                 list_del_init(&mnt->mnt_hash);
564                 if (mnt->mnt_parent != mnt) {
565                         struct dentry *dentry;
566                         struct vfsmount *m;
567                         spin_lock(&vfsmount_lock);
568                         dentry = mnt->mnt_mountpoint;
569                         m = mnt->mnt_parent;
570                         mnt->mnt_mountpoint = mnt->mnt_root;
571                         mnt->mnt_parent = mnt;
572                         spin_unlock(&vfsmount_lock);
573                         dput(dentry);
574                         mntput(m);
575                 }
576                 mntput(mnt);
577         }
578 }
579
580 void umount_tree(struct vfsmount *mnt, int propagate, struct list_head *kill)
581 {
582         struct vfsmount *p;
583
584         for (p = mnt; p; p = next_mnt(p, mnt))
585                 list_move(&p->mnt_hash, kill);
586
587         if (propagate)
588                 propagate_umount(kill);
589
590         list_for_each_entry(p, kill, mnt_hash) {
591                 list_del_init(&p->mnt_expire);
592                 list_del_init(&p->mnt_list);
593                 __touch_namespace(p->mnt_namespace);
594                 p->mnt_namespace = NULL;
595                 list_del_init(&p->mnt_child);
596                 if (p->mnt_parent != p)
597                         p->mnt_mountpoint->d_mounted--;
598                 change_mnt_propagation(p, MS_PRIVATE);
599         }
600 }
601
602 static int do_umount(struct vfsmount *mnt, int flags)
603 {
604         struct super_block *sb = mnt->mnt_sb;
605         int retval;
606         LIST_HEAD(umount_list);
607
608         retval = security_sb_umount(mnt, flags);
609         if (retval)
610                 return retval;
611
612         /*
613          * Allow userspace to request a mountpoint be expired rather than
614          * unmounting unconditionally. Unmount only happens if:
615          *  (1) the mark is already set (the mark is cleared by mntput())
616          *  (2) the usage count == 1 [parent vfsmount] + 1 [sys_umount]
617          */
618         if (flags & MNT_EXPIRE) {
619                 if (mnt == current->fs->rootmnt ||
620                     flags & (MNT_FORCE | MNT_DETACH))
621                         return -EINVAL;
622
623                 if (atomic_read(&mnt->mnt_count) != 2)
624                         return -EBUSY;
625
626                 if (!xchg(&mnt->mnt_expiry_mark, 1))
627                         return -EAGAIN;
628         }
629
630         /*
631          * If we may have to abort operations to get out of this
632          * mount, and they will themselves hold resources we must
633          * allow the fs to do things. In the Unix tradition of
634          * 'Gee thats tricky lets do it in userspace' the umount_begin
635          * might fail to complete on the first run through as other tasks
636          * must return, and the like. Thats for the mount program to worry
637          * about for the moment.
638          */
639
640         lock_kernel();
641         if (sb->s_op->umount_begin)
642                 sb->s_op->umount_begin(mnt, flags);
643         unlock_kernel();
644
645         /*
646          * No sense to grab the lock for this test, but test itself looks
647          * somewhat bogus. Suggestions for better replacement?
648          * Ho-hum... In principle, we might treat that as umount + switch
649          * to rootfs. GC would eventually take care of the old vfsmount.
650          * Actually it makes sense, especially if rootfs would contain a
651          * /reboot - static binary that would close all descriptors and
652          * call reboot(9). Then init(8) could umount root and exec /reboot.
653          */
654         if (mnt == current->fs->rootmnt && !(flags & MNT_DETACH)) {
655                 /*
656                  * Special case for "unmounting" root ...
657                  * we just try to remount it readonly.
658                  */
659                 down_write(&sb->s_umount);
660                 if (!(sb->s_flags & MS_RDONLY)) {
661                         lock_kernel();
662                         DQUOT_OFF(sb);
663                         retval = do_remount_sb(sb, MS_RDONLY, NULL, 0);
664                         unlock_kernel();
665                 }
666                 up_write(&sb->s_umount);
667                 return retval;
668         }
669
670         down_write(&namespace_sem);
671         spin_lock(&vfsmount_lock);
672         event++;
673
674         retval = -EBUSY;
675         if (flags & MNT_DETACH || !propagate_mount_busy(mnt, 2)) {
676                 if (!list_empty(&mnt->mnt_list))
677                         umount_tree(mnt, 1, &umount_list);
678                 retval = 0;
679         }
680         spin_unlock(&vfsmount_lock);
681         if (retval)
682                 security_sb_umount_busy(mnt);
683         up_write(&namespace_sem);
684         release_mounts(&umount_list);
685         return retval;
686 }
687
688 /*
689  * Now umount can handle mount points as well as block devices.
690  * This is important for filesystems which use unnamed block devices.
691  *
692  * We now support a flag for forced unmount like the other 'big iron'
693  * unixes. Our API is identical to OSF/1 to avoid making a mess of AMD
694  */
695
696 asmlinkage long sys_umount(char __user * name, int flags)
697 {
698         struct nameidata nd;
699         int retval;
700
701         retval = __user_walk(name, LOOKUP_FOLLOW, &nd);
702         if (retval)
703                 goto out;
704         retval = -EINVAL;
705         if (nd.dentry != nd.mnt->mnt_root)
706                 goto dput_and_out;
707         if (!check_mnt(nd.mnt))
708                 goto dput_and_out;
709
710         retval = -EPERM;
711         if (!vx_capable(CAP_SYS_ADMIN, VXC_SECURE_MOUNT))
712                 goto dput_and_out;
713
714         retval = do_umount(nd.mnt, flags);
715 dput_and_out:
716         path_release_on_umount(&nd);
717 out:
718         return retval;
719 }
720
721 #ifdef __ARCH_WANT_SYS_OLDUMOUNT
722
723 /*
724  *      The 2.0 compatible umount. No flags.
725  */
726 asmlinkage long sys_oldumount(char __user * name)
727 {
728         return sys_umount(name, 0);
729 }
730
731 #endif
732
733 static int mount_is_safe(struct nameidata *nd)
734 {
735         if (vx_capable(CAP_SYS_ADMIN, VXC_SECURE_MOUNT))
736                 return 0;
737         return -EPERM;
738 #ifdef notyet
739         if (S_ISLNK(nd->dentry->d_inode->i_mode))
740                 return -EPERM;
741         if (nd->dentry->d_inode->i_mode & S_ISVTX) {
742                 if (current->uid != nd->dentry->d_inode->i_uid)
743                         return -EPERM;
744         }
745         if (vfs_permission(nd, MAY_WRITE))
746                 return -EPERM;
747         return 0;
748 #endif
749 }
750
751 static int lives_below_in_same_fs(struct dentry *d, struct dentry *dentry)
752 {
753         while (1) {
754                 if (d == dentry)
755                         return 1;
756                 if (d == NULL || d == d->d_parent)
757                         return 0;
758                 d = d->d_parent;
759         }
760 }
761
762 struct vfsmount *copy_tree(struct vfsmount *mnt, struct dentry *dentry,
763                                         int flag)
764 {
765         struct vfsmount *res, *p, *q, *r, *s;
766         struct nameidata nd;
767
768         if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(mnt))
769                 return NULL;
770
771         res = q = clone_mnt(mnt, dentry, flag);
772         if (!q)
773                 goto Enomem;
774         q->mnt_mountpoint = mnt->mnt_mountpoint;
775
776         p = mnt;
777         list_for_each_entry(r, &mnt->mnt_mounts, mnt_child) {
778                 if (!lives_below_in_same_fs(r->mnt_mountpoint, dentry))
779                         continue;
780
781                 for (s = r; s; s = next_mnt(s, r)) {
782                         if (!(flag & CL_COPY_ALL) && IS_MNT_UNBINDABLE(s)) {
783                                 s = skip_mnt_tree(s);
784                                 continue;
785                         }
786                         while (p != s->mnt_parent) {
787                                 p = p->mnt_parent;
788                                 q = q->mnt_parent;
789                         }
790                         p = s;
791                         nd.mnt = q;
792                         nd.dentry = p->mnt_mountpoint;
793                         q = clone_mnt(p, p->mnt_root, flag);
794                         if (!q)
795                                 goto Enomem;
796                         spin_lock(&vfsmount_lock);
797                         list_add_tail(&q->mnt_list, &res->mnt_list);
798                         attach_mnt(q, &nd);
799                         spin_unlock(&vfsmount_lock);
800                 }
801         }
802         return res;
803 Enomem:
804         if (res) {
805                 LIST_HEAD(umount_list);
806                 spin_lock(&vfsmount_lock);
807                 umount_tree(res, 0, &umount_list);
808                 spin_unlock(&vfsmount_lock);
809                 release_mounts(&umount_list);
810         }
811         return NULL;
812 }
813
814 /*
815  *  @source_mnt : mount tree to be attached
816  *  @nd         : place the mount tree @source_mnt is attached
817  *  @parent_nd  : if non-null, detach the source_mnt from its parent and
818  *                 store the parent mount and mountpoint dentry.
819  *                 (done when source_mnt is moved)
820  *
821  *  NOTE: in the table below explains the semantics when a source mount
822  *  of a given type is attached to a destination mount of a given type.
823  * ---------------------------------------------------------------------------
824  * |         BIND MOUNT OPERATION                                            |
825  * |**************************************************************************
826  * | source-->| shared        |       private  |       slave    | unbindable |
827  * | dest     |               |                |                |            |
828  * |   |      |               |                |                |            |
829  * |   v      |               |                |                |            |
830  * |**************************************************************************
831  * |  shared  | shared (++)   |     shared (+) |     shared(+++)|  invalid   |
832  * |          |               |                |                |            |
833  * |non-shared| shared (+)    |      private   |      slave (*) |  invalid   |
834  * ***************************************************************************
835  * A bind operation clones the source mount and mounts the clone on the
836  * destination mount.
837  *
838  * (++)  the cloned mount is propagated to all the mounts in the propagation
839  *       tree of the destination mount and the cloned mount is added to
840  *       the peer group of the source mount.
841  * (+)   the cloned mount is created under the destination mount and is marked
842  *       as shared. The cloned mount is added to the peer group of the source
843  *       mount.
844  * (+++) the mount is propagated to all the mounts in the propagation tree
845  *       of the destination mount and the cloned mount is made slave
846  *       of the same master as that of the source mount. The cloned mount
847  *       is marked as 'shared and slave'.
848  * (*)   the cloned mount is made a slave of the same master as that of the
849  *       source mount.
850  *
851  * ---------------------------------------------------------------------------
852  * |                    MOVE MOUNT OPERATION                                 |
853  * |**************************************************************************
854  * | source-->| shared        |       private  |       slave    | unbindable |
855  * | dest     |               |                |                |            |
856  * |   |      |               |                |                |            |
857  * |   v      |               |                |                |            |
858  * |**************************************************************************
859  * |  shared  | shared (+)    |     shared (+) |    shared(+++) |  invalid   |
860  * |          |               |                |                |            |
861  * |non-shared| shared (+*)   |      private   |    slave (*)   | unbindable |
862  * ***************************************************************************
863  *
864  * (+)  the mount is moved to the destination. And is then propagated to
865  *      all the mounts in the propagation tree of the destination mount.
866  * (+*)  the mount is moved to the destination.
867  * (+++)  the mount is moved to the destination and is then propagated to
868  *      all the mounts belonging to the destination mount's propagation tree.
869  *      the mount is marked as 'shared and slave'.
870  * (*)  the mount continues to be a slave at the new location.
871  *
872  * if the source mount is a tree, the operations explained above is
873  * applied to each mount in the tree.
874  * Must be called without spinlocks held, since this function can sleep
875  * in allocations.
876  */
877 static int attach_recursive_mnt(struct vfsmount *source_mnt,
878                         struct nameidata *nd, struct nameidata *parent_nd)
879 {
880         LIST_HEAD(tree_list);
881         struct vfsmount *dest_mnt = nd->mnt;
882         struct dentry *dest_dentry = nd->dentry;
883         struct vfsmount *child, *p;
884
885         if (propagate_mnt(dest_mnt, dest_dentry, source_mnt, &tree_list))
886                 return -EINVAL;
887
888         if (IS_MNT_SHARED(dest_mnt)) {
889                 for (p = source_mnt; p; p = next_mnt(p, source_mnt))
890                         set_mnt_shared(p);
891         }
892
893         spin_lock(&vfsmount_lock);
894         if (parent_nd) {
895                 detach_mnt(source_mnt, parent_nd);
896                 attach_mnt(source_mnt, nd);
897                 touch_namespace(current->namespace);
898         } else {
899                 mnt_set_mountpoint(dest_mnt, dest_dentry, source_mnt);
900                 commit_tree(source_mnt);
901         }
902
903         list_for_each_entry_safe(child, p, &tree_list, mnt_hash) {
904                 list_del_init(&child->mnt_hash);
905                 commit_tree(child);
906         }
907         spin_unlock(&vfsmount_lock);
908         return 0;
909 }
910
911 static int graft_tree(struct vfsmount *mnt, struct nameidata *nd)
912 {
913         int err;
914         if (mnt->mnt_sb->s_flags & MS_NOUSER)
915                 return -EINVAL;
916
917         if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
918               S_ISDIR(mnt->mnt_root->d_inode->i_mode))
919                 return -ENOTDIR;
920
921         err = -ENOENT;
922         mutex_lock(&nd->dentry->d_inode->i_mutex);
923         if (IS_DEADDIR(nd->dentry->d_inode))
924                 goto out_unlock;
925
926         err = security_sb_check_sb(mnt, nd);
927         if (err)
928                 goto out_unlock;
929
930         err = -ENOENT;
931         if (IS_ROOT(nd->dentry) || !d_unhashed(nd->dentry))
932                 err = attach_recursive_mnt(mnt, nd, NULL);
933 out_unlock:
934         mutex_unlock(&nd->dentry->d_inode->i_mutex);
935         if (!err)
936                 security_sb_post_addmount(mnt, nd);
937         return err;
938 }
939
940 /*
941  * recursively change the type of the mountpoint.
942  */
943 static int do_change_type(struct nameidata *nd, int flag)
944 {
945         struct vfsmount *m, *mnt = nd->mnt;
946         int recurse = flag & MS_REC;
947         int type = flag & ~MS_REC;
948
949         if (nd->dentry != nd->mnt->mnt_root)
950                 return -EINVAL;
951
952         down_write(&namespace_sem);
953         spin_lock(&vfsmount_lock);
954         for (m = mnt; m; m = (recurse ? next_mnt(m, mnt) : NULL))
955                 change_mnt_propagation(m, type);
956         spin_unlock(&vfsmount_lock);
957         up_write(&namespace_sem);
958         return 0;
959 }
960
961 /*
962  * do loopback mount.
963  */
964 static int do_loopback(struct nameidata *nd, char *old_name, xid_t xid,
965         unsigned long flags, int mnt_flags)
966 {
967         struct nameidata old_nd;
968         struct vfsmount *mnt = NULL;
969         int err = mount_is_safe(nd);
970         int recurse = flags & MS_REC;
971         if (err)
972                 return err;
973         if (!old_name || !*old_name)
974                 return -EINVAL;
975         err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
976         if (err)
977                 return err;
978
979         down_write(&namespace_sem);
980         err = -EINVAL;
981         if (IS_MNT_UNBINDABLE(old_nd.mnt))
982                 goto out;
983
984         if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
985                 goto out;
986
987         err = -ENOMEM;
988         if (recurse)
989                 mnt = copy_tree(old_nd.mnt, old_nd.dentry, 0);
990         else
991                 mnt = clone_mnt(old_nd.mnt, old_nd.dentry, 0);
992
993         if (!mnt)
994                 goto out;
995
996         mnt->mnt_flags = mnt_flags;
997         if (flags & MS_XID) {
998                 mnt->mnt_xid = xid;
999                 mnt->mnt_flags |= MNT_XID;
1000         }
1001
1002         err = graft_tree(mnt, nd);
1003         if (err) {
1004                 LIST_HEAD(umount_list);
1005                 spin_lock(&vfsmount_lock);
1006                 umount_tree(mnt, 0, &umount_list);
1007                 spin_unlock(&vfsmount_lock);
1008                 release_mounts(&umount_list);
1009         }
1010         mnt->mnt_flags = mnt_flags;
1011
1012 out:
1013         up_write(&namespace_sem);
1014         path_release(&old_nd);
1015         return err;
1016 }
1017
1018 /*
1019  * change filesystem flags. dir should be a physical root of filesystem.
1020  * If you've mounted a non-root directory somewhere and want to do remount
1021  * on it - tough luck.
1022  */
1023 static int do_remount(struct nameidata *nd, int flags, int mnt_flags,
1024                       void *data, xid_t xid)
1025 {
1026         int err;
1027         struct super_block *sb = nd->mnt->mnt_sb;
1028
1029         if (!vx_capable(CAP_SYS_ADMIN, VXC_SECURE_REMOUNT))
1030                 return -EPERM;
1031
1032         if (!check_mnt(nd->mnt))
1033                 return -EINVAL;
1034
1035         if (nd->dentry != nd->mnt->mnt_root)
1036                 return -EINVAL;
1037
1038         down_write(&sb->s_umount);
1039         err = do_remount_sb(sb, flags, data, 0);
1040         if (!err)
1041                 nd->mnt->mnt_flags = mnt_flags;
1042         up_write(&sb->s_umount);
1043         if (!err)
1044                 security_sb_post_remount(nd->mnt, flags, data);
1045         return err;
1046 }
1047
1048 static inline int tree_contains_unbindable(struct vfsmount *mnt)
1049 {
1050         struct vfsmount *p;
1051         for (p = mnt; p; p = next_mnt(p, mnt)) {
1052                 if (IS_MNT_UNBINDABLE(p))
1053                         return 1;
1054         }
1055         return 0;
1056 }
1057
1058 static int do_move_mount(struct nameidata *nd, char *old_name)
1059 {
1060         struct nameidata old_nd, parent_nd;
1061         struct vfsmount *p;
1062         int err = 0;
1063         if (!vx_capable(CAP_SYS_ADMIN, VXC_SECURE_MOUNT))
1064                 return -EPERM;
1065         if (!old_name || !*old_name)
1066                 return -EINVAL;
1067         err = path_lookup(old_name, LOOKUP_FOLLOW, &old_nd);
1068         if (err)
1069                 return err;
1070
1071         down_write(&namespace_sem);
1072         while (d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
1073                 ;
1074         err = -EINVAL;
1075         if (!check_mnt(nd->mnt) || !check_mnt(old_nd.mnt))
1076                 goto out;
1077
1078         err = -ENOENT;
1079         mutex_lock(&nd->dentry->d_inode->i_mutex);
1080         if (IS_DEADDIR(nd->dentry->d_inode))
1081                 goto out1;
1082
1083         if (!IS_ROOT(nd->dentry) && d_unhashed(nd->dentry))
1084                 goto out1;
1085
1086         err = -EINVAL;
1087         if (old_nd.dentry != old_nd.mnt->mnt_root)
1088                 goto out1;
1089
1090         if (old_nd.mnt == old_nd.mnt->mnt_parent)
1091                 goto out1;
1092
1093         if (S_ISDIR(nd->dentry->d_inode->i_mode) !=
1094               S_ISDIR(old_nd.dentry->d_inode->i_mode))
1095                 goto out1;
1096         /*
1097          * Don't move a mount residing in a shared parent.
1098          */
1099         if (old_nd.mnt->mnt_parent && IS_MNT_SHARED(old_nd.mnt->mnt_parent))
1100                 goto out1;
1101         /*
1102          * Don't move a mount tree containing unbindable mounts to a destination
1103          * mount which is shared.
1104          */
1105         if (IS_MNT_SHARED(nd->mnt) && tree_contains_unbindable(old_nd.mnt))
1106                 goto out1;
1107         err = -ELOOP;
1108         for (p = nd->mnt; p->mnt_parent != p; p = p->mnt_parent)
1109                 if (p == old_nd.mnt)
1110                         goto out1;
1111
1112         if ((err = attach_recursive_mnt(old_nd.mnt, nd, &parent_nd)))
1113                 goto out1;
1114
1115         spin_lock(&vfsmount_lock);
1116         /* if the mount is moved, it should no longer be expire
1117          * automatically */
1118         list_del_init(&old_nd.mnt->mnt_expire);
1119         spin_unlock(&vfsmount_lock);
1120 out1:
1121         mutex_unlock(&nd->dentry->d_inode->i_mutex);
1122 out:
1123         up_write(&namespace_sem);
1124         if (!err)
1125                 path_release(&parent_nd);
1126         path_release(&old_nd);
1127         return err;
1128 }
1129
1130 /*
1131  * create a new mount for userspace and request it to be added into the
1132  * namespace's tree
1133  */
1134 static int do_new_mount(struct nameidata *nd, char *type, int flags,
1135                         int mnt_flags, char *name, void *data)
1136 {
1137         struct vfsmount *mnt;
1138
1139         if (!type || !memchr(type, 0, PAGE_SIZE))
1140                 return -EINVAL;
1141
1142         /* we need capabilities... */
1143         if (!vx_capable(CAP_SYS_ADMIN, VXC_SECURE_MOUNT))
1144                 return -EPERM;
1145
1146         mnt = do_kern_mount(type, flags, name, data);
1147         if (IS_ERR(mnt))
1148                 return PTR_ERR(mnt);
1149
1150         return do_add_mount(mnt, nd, mnt_flags, NULL);
1151 }
1152
1153 /*
1154  * add a mount into a namespace's mount tree
1155  * - provide the option of adding the new mount to an expiration list
1156  */
1157 int do_add_mount(struct vfsmount *newmnt, struct nameidata *nd,
1158                  int mnt_flags, struct list_head *fslist)
1159 {
1160         int err;
1161
1162         down_write(&namespace_sem);
1163         /* Something was mounted here while we slept */
1164         while (d_mountpoint(nd->dentry) && follow_down(&nd->mnt, &nd->dentry))
1165                 ;
1166         err = -EINVAL;
1167         if (!check_mnt(nd->mnt))
1168                 goto unlock;
1169
1170         /* Refuse the same filesystem on the same mount point */
1171         err = -EBUSY;
1172         if (nd->mnt->mnt_sb == newmnt->mnt_sb &&
1173             nd->mnt->mnt_root == nd->dentry)
1174                 goto unlock;
1175
1176         err = -EINVAL;
1177         if (S_ISLNK(newmnt->mnt_root->d_inode->i_mode))
1178                 goto unlock;
1179
1180         newmnt->mnt_flags = mnt_flags;
1181         if ((err = graft_tree(newmnt, nd)))
1182                 goto unlock;
1183
1184         if (fslist) {
1185                 /* add to the specified expiration list */
1186                 spin_lock(&vfsmount_lock);
1187                 list_add_tail(&newmnt->mnt_expire, fslist);
1188                 spin_unlock(&vfsmount_lock);
1189         }
1190         up_write(&namespace_sem);
1191         return 0;
1192
1193 unlock:
1194         up_write(&namespace_sem);
1195         mntput(newmnt);
1196         return err;
1197 }
1198
1199 EXPORT_SYMBOL_GPL(do_add_mount);
1200
1201 static void expire_mount(struct vfsmount *mnt, struct list_head *mounts,
1202                                 struct list_head *umounts)
1203 {
1204         spin_lock(&vfsmount_lock);
1205
1206         /*
1207          * Check if mount is still attached, if not, let whoever holds it deal
1208          * with the sucker
1209          */
1210         if (mnt->mnt_parent == mnt) {
1211                 spin_unlock(&vfsmount_lock);
1212                 return;
1213         }
1214
1215         /*
1216          * Check that it is still dead: the count should now be 2 - as
1217          * contributed by the vfsmount parent and the mntget above
1218          */
1219         if (!propagate_mount_busy(mnt, 2)) {
1220                 /* delete from the namespace */
1221                 touch_namespace(mnt->mnt_namespace);
1222                 list_del_init(&mnt->mnt_list);
1223                 mnt->mnt_namespace = NULL;
1224                 umount_tree(mnt, 1, umounts);
1225                 spin_unlock(&vfsmount_lock);
1226         } else {
1227                 /*
1228                  * Someone brought it back to life whilst we didn't have any
1229                  * locks held so return it to the expiration list
1230                  */
1231                 list_add_tail(&mnt->mnt_expire, mounts);
1232                 spin_unlock(&vfsmount_lock);
1233         }
1234 }
1235
1236 /*
1237  * go through the vfsmounts we've just consigned to the graveyard to
1238  * - check that they're still dead
1239  * - delete the vfsmount from the appropriate namespace under lock
1240  * - dispose of the corpse
1241  */
1242 static void expire_mount_list(struct list_head *graveyard, struct list_head *mounts)
1243 {
1244         struct namespace *namespace;
1245         struct vfsmount *mnt;
1246
1247         while (!list_empty(graveyard)) {
1248                 LIST_HEAD(umounts);
1249                 mnt = list_entry(graveyard->next, struct vfsmount, mnt_expire);
1250                 list_del_init(&mnt->mnt_expire);
1251
1252                 /* don't do anything if the namespace is dead - all the
1253                  * vfsmounts from it are going away anyway */
1254                 namespace = mnt->mnt_namespace;
1255                 if (!namespace || !namespace->root)
1256                         continue;
1257                 get_namespace(namespace);
1258
1259                 spin_unlock(&vfsmount_lock);
1260                 down_write(&namespace_sem);
1261                 expire_mount(mnt, mounts, &umounts);
1262                 up_write(&namespace_sem);
1263                 release_mounts(&umounts);
1264                 mntput(mnt);
1265                 put_namespace(namespace);
1266                 spin_lock(&vfsmount_lock);
1267         }
1268 }
1269
1270 /*
1271  * process a list of expirable mountpoints with the intent of discarding any
1272  * mountpoints that aren't in use and haven't been touched since last we came
1273  * here
1274  */
1275 void mark_mounts_for_expiry(struct list_head *mounts)
1276 {
1277         struct vfsmount *mnt, *next;
1278         LIST_HEAD(graveyard);
1279
1280         if (list_empty(mounts))
1281                 return;
1282
1283         spin_lock(&vfsmount_lock);
1284
1285         /* extract from the expiration list every vfsmount that matches the
1286          * following criteria:
1287          * - only referenced by its parent vfsmount
1288          * - still marked for expiry (marked on the last call here; marks are
1289          *   cleared by mntput())
1290          */
1291         list_for_each_entry_safe(mnt, next, mounts, mnt_expire) {
1292                 if (!xchg(&mnt->mnt_expiry_mark, 1) ||
1293                     atomic_read(&mnt->mnt_count) != 1)
1294                         continue;
1295
1296                 mntget(mnt);
1297                 list_move(&mnt->mnt_expire, &graveyard);
1298         }
1299
1300         expire_mount_list(&graveyard, mounts);
1301
1302         spin_unlock(&vfsmount_lock);
1303 }
1304
1305 EXPORT_SYMBOL_GPL(mark_mounts_for_expiry);
1306
1307 /*
1308  * Ripoff of 'select_parent()'
1309  *
1310  * search the list of submounts for a given mountpoint, and move any
1311  * shrinkable submounts to the 'graveyard' list.
1312  */
1313 static int select_submounts(struct vfsmount *parent, struct list_head *graveyard)
1314 {
1315         struct vfsmount *this_parent = parent;
1316         struct list_head *next;
1317         int found = 0;
1318
1319 repeat:
1320         next = this_parent->mnt_mounts.next;
1321 resume:
1322         while (next != &this_parent->mnt_mounts) {
1323                 struct list_head *tmp = next;
1324                 struct vfsmount *mnt = list_entry(tmp, struct vfsmount, mnt_child);
1325
1326                 next = tmp->next;
1327                 if (!(mnt->mnt_flags & MNT_SHRINKABLE))
1328                         continue;
1329                 /*
1330                  * Descend a level if the d_mounts list is non-empty.
1331                  */
1332                 if (!list_empty(&mnt->mnt_mounts)) {
1333                         this_parent = mnt;
1334                         goto repeat;
1335                 }
1336
1337                 if (!propagate_mount_busy(mnt, 1)) {
1338                         mntget(mnt);
1339                         list_move_tail(&mnt->mnt_expire, graveyard);
1340                         found++;
1341                 }
1342         }
1343         /*
1344          * All done at this level ... ascend and resume the search
1345          */
1346         if (this_parent != parent) {
1347                 next = this_parent->mnt_child.next;
1348                 this_parent = this_parent->mnt_parent;
1349                 goto resume;
1350         }
1351         return found;
1352 }
1353
1354 /*
1355  * process a list of expirable mountpoints with the intent of discarding any
1356  * submounts of a specific parent mountpoint
1357  */
1358 void shrink_submounts(struct vfsmount *mountpoint, struct list_head *mounts)
1359 {
1360         LIST_HEAD(graveyard);
1361         int found;
1362
1363         spin_lock(&vfsmount_lock);
1364
1365         /* extract submounts of 'mountpoint' from the expiration list */
1366         while ((found = select_submounts(mountpoint, &graveyard)) != 0)
1367                 expire_mount_list(&graveyard, mounts);
1368
1369         spin_unlock(&vfsmount_lock);
1370 }
1371
1372 EXPORT_SYMBOL_GPL(shrink_submounts);
1373
1374 /*
1375  * Some copy_from_user() implementations do not return the exact number of
1376  * bytes remaining to copy on a fault.  But copy_mount_options() requires that.
1377  * Note that this function differs from copy_from_user() in that it will oops
1378  * on bad values of `to', rather than returning a short copy.
1379  */
1380 static long exact_copy_from_user(void *to, const void __user * from,
1381                                  unsigned long n)
1382 {
1383         char *t = to;
1384         const char __user *f = from;
1385         char c;
1386
1387         if (!access_ok(VERIFY_READ, from, n))
1388                 return n;
1389
1390         while (n) {
1391                 if (__get_user(c, f)) {
1392                         memset(t, 0, n);
1393                         break;
1394                 }
1395                 *t++ = c;
1396                 f++;
1397                 n--;
1398         }
1399         return n;
1400 }
1401
1402 int copy_mount_options(const void __user * data, unsigned long *where)
1403 {
1404         int i;
1405         unsigned long page;
1406         unsigned long size;
1407
1408         *where = 0;
1409         if (!data)
1410                 return 0;
1411
1412         if (!(page = __get_free_page(GFP_KERNEL)))
1413                 return -ENOMEM;
1414
1415         /* We only care that *some* data at the address the user
1416          * gave us is valid.  Just in case, we'll zero
1417          * the remainder of the page.
1418          */
1419         /* copy_from_user cannot cross TASK_SIZE ! */
1420         size = TASK_SIZE - (unsigned long)data;
1421         if (size > PAGE_SIZE)
1422                 size = PAGE_SIZE;
1423
1424         i = size - exact_copy_from_user((void *)page, data, size);
1425         if (!i) {
1426                 free_page(page);
1427                 return -EFAULT;
1428         }
1429         if (i != PAGE_SIZE)
1430                 memset((char *)page + i, 0, PAGE_SIZE - i);
1431         *where = page;
1432         return 0;
1433 }
1434
1435 /*
1436  * Flags is a 32-bit value that allows up to 31 non-fs dependent flags to
1437  * be given to the mount() call (ie: read-only, no-dev, no-suid etc).
1438  *
1439  * data is a (void *) that can point to any structure up to
1440  * PAGE_SIZE-1 bytes, which can contain arbitrary fs-dependent
1441  * information (or be NULL).
1442  *
1443  * Pre-0.97 versions of mount() didn't have a flags word.
1444  * When the flags word was introduced its top half was required
1445  * to have the magic value 0xC0ED, and this remained so until 2.4.0-test9.
1446  * Therefore, if this magic number is present, it carries no information
1447  * and must be discarded.
1448  */
1449 long do_mount(char *dev_name, char *dir_name, char *type_page,
1450                   unsigned long flags, void *data_page)
1451 {
1452         struct nameidata nd;
1453         int retval = 0;
1454         int mnt_flags = 0;
1455         xid_t xid = 0;
1456
1457         /* Discard magic */
1458         if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
1459                 flags &= ~MS_MGC_MSK;
1460
1461         /* Basic sanity checks */
1462
1463         if (!dir_name || !*dir_name || !memchr(dir_name, 0, PAGE_SIZE))
1464                 return -EINVAL;
1465         if (dev_name && !memchr(dev_name, 0, PAGE_SIZE))
1466                 return -EINVAL;
1467
1468         if (data_page)
1469                 ((char *)data_page)[PAGE_SIZE - 1] = 0;
1470
1471         retval = vx_parse_xid(data_page, &xid, 1);
1472         if (retval) {
1473                 mnt_flags |= MNT_XID;
1474                 /* bind and re-mounts get xid flag */
1475                 if (flags & (MS_BIND|MS_REMOUNT))
1476                         flags |= MS_XID;
1477         }
1478
1479         /* Separate the per-mountpoint flags */
1480         if (flags & MS_RDONLY)
1481                 mnt_flags |= MNT_RDONLY;
1482         if (flags & MS_NOSUID)
1483                 mnt_flags |= MNT_NOSUID;
1484         if (flags & MS_NODEV)
1485                 mnt_flags |= MNT_NODEV;
1486         if (flags & MS_NOEXEC)
1487                 mnt_flags |= MNT_NOEXEC;
1488         if (flags & MS_NOATIME)
1489                 mnt_flags |= MNT_NOATIME;
1490         if (flags & MS_NODIRATIME)
1491                 mnt_flags |= MNT_NODIRATIME;
1492
1493         if (!capable(CAP_SYS_ADMIN))
1494                 mnt_flags |= MNT_NODEV;
1495         flags &= ~(MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_ACTIVE |
1496                    MS_NOATIME | MS_NODIRATIME);
1497
1498         /* ... and get the mountpoint */
1499         retval = path_lookup(dir_name, LOOKUP_FOLLOW, &nd);
1500         if (retval)
1501                 return retval;
1502
1503         retval = security_sb_mount(dev_name, &nd, type_page, flags, data_page);
1504         if (retval)
1505                 goto dput_out;
1506
1507         if (flags & MS_REMOUNT)
1508                 retval = do_remount(&nd, flags & ~MS_REMOUNT, mnt_flags,
1509                                     data_page, xid);
1510         else if (flags & MS_BIND)
1511                 retval = do_loopback(&nd, dev_name, xid, flags, mnt_flags);
1512         else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
1513                 retval = do_change_type(&nd, flags);
1514         else if (flags & MS_MOVE)
1515                 retval = do_move_mount(&nd, dev_name);
1516         else
1517                 retval = do_new_mount(&nd, type_page, flags, mnt_flags,
1518                                       dev_name, data_page);
1519 dput_out:
1520         path_release(&nd);
1521         return retval;
1522 }
1523
1524 /*
1525  * Allocate a new namespace structure and populate it with contents
1526  * copied from the namespace of the passed in task structure.
1527  */
1528 struct namespace *dup_namespace(struct task_struct *tsk, struct fs_struct *fs)
1529 {
1530         struct namespace *namespace = tsk->namespace;
1531         struct namespace *new_ns;
1532         struct vfsmount *rootmnt = NULL, *pwdmnt = NULL, *altrootmnt = NULL;
1533         struct vfsmount *p, *q;
1534
1535         new_ns = kmalloc(sizeof(struct namespace), GFP_KERNEL);
1536         if (!new_ns)
1537                 return NULL;
1538
1539         atomic_set(&new_ns->count, 1);
1540         INIT_LIST_HEAD(&new_ns->list);
1541         init_waitqueue_head(&new_ns->poll);
1542         new_ns->event = 0;
1543
1544         down_write(&namespace_sem);
1545         /* First pass: copy the tree topology */
1546         new_ns->root = copy_tree(namespace->root, namespace->root->mnt_root,
1547                                         CL_COPY_ALL | CL_EXPIRE);
1548         if (!new_ns->root) {
1549                 up_write(&namespace_sem);
1550                 kfree(new_ns);
1551                 return NULL;
1552         }
1553         spin_lock(&vfsmount_lock);
1554         list_add_tail(&new_ns->list, &new_ns->root->mnt_list);
1555         spin_unlock(&vfsmount_lock);
1556
1557         /*
1558          * Second pass: switch the tsk->fs->* elements and mark new vfsmounts
1559          * as belonging to new namespace.  We have already acquired a private
1560          * fs_struct, so tsk->fs->lock is not needed.
1561          */
1562         p = namespace->root;
1563         q = new_ns->root;
1564         while (p) {
1565                 q->mnt_namespace = new_ns;
1566                 if (fs) {
1567                         if (p == fs->rootmnt) {
1568                                 rootmnt = p;
1569                                 fs->rootmnt = mntget(q);
1570                         }
1571                         if (p == fs->pwdmnt) {
1572                                 pwdmnt = p;
1573                                 fs->pwdmnt = mntget(q);
1574                         }
1575                         if (p == fs->altrootmnt) {
1576                                 altrootmnt = p;
1577                                 fs->altrootmnt = mntget(q);
1578                         }
1579                 }
1580                 p = next_mnt(p, namespace->root);
1581                 q = next_mnt(q, new_ns->root);
1582         }
1583         up_write(&namespace_sem);
1584
1585         if (rootmnt)
1586                 mntput(rootmnt);
1587         if (pwdmnt)
1588                 mntput(pwdmnt);
1589         if (altrootmnt)
1590                 mntput(altrootmnt);
1591
1592         return new_ns;
1593 }
1594
1595 int copy_namespace(int flags, struct task_struct *tsk)
1596 {
1597         struct namespace *namespace = tsk->namespace;
1598         struct namespace *new_ns;
1599         int err = 0;
1600
1601         if (!namespace)
1602                 return 0;
1603
1604         get_namespace(namespace);
1605
1606         if (!(flags & CLONE_NEWNS))
1607                 return 0;
1608
1609         if (!vx_capable(CAP_SYS_ADMIN, VXC_SECURE_MOUNT)) {
1610                 err = -EPERM;
1611                 goto out;
1612         }
1613
1614         new_ns = dup_namespace(tsk, tsk->fs);
1615         if (!new_ns) {
1616                 err = -ENOMEM;
1617                 goto out;
1618         }
1619
1620         tsk->namespace = new_ns;
1621
1622 out:
1623         put_namespace(namespace);
1624         return err;
1625 }
1626
1627 asmlinkage long sys_mount(char __user * dev_name, char __user * dir_name,
1628                           char __user * type, unsigned long flags,
1629                           void __user * data)
1630 {
1631         int retval;
1632         unsigned long data_page;
1633         unsigned long type_page;
1634         unsigned long dev_page;
1635         char *dir_page;
1636
1637         retval = copy_mount_options(type, &type_page);
1638         if (retval < 0)
1639                 return retval;
1640
1641         dir_page = getname(dir_name);
1642         retval = PTR_ERR(dir_page);
1643         if (IS_ERR(dir_page))
1644                 goto out1;
1645
1646         retval = copy_mount_options(dev_name, &dev_page);
1647         if (retval < 0)
1648                 goto out2;
1649
1650         retval = copy_mount_options(data, &data_page);
1651         if (retval < 0)
1652                 goto out3;
1653
1654         lock_kernel();
1655         retval = do_mount((char *)dev_page, dir_page, (char *)type_page,
1656                           flags, (void *)data_page);
1657         unlock_kernel();
1658         free_page(data_page);
1659
1660 out3:
1661         free_page(dev_page);
1662 out2:
1663         putname(dir_page);
1664 out1:
1665         free_page(type_page);
1666         return retval;
1667 }
1668
1669 /*
1670  * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
1671  * It can block. Requires the big lock held.
1672  */
1673 void set_fs_root(struct fs_struct *fs, struct vfsmount *mnt,
1674                  struct dentry *dentry)
1675 {
1676         struct dentry *old_root;
1677         struct vfsmount *old_rootmnt;
1678         write_lock(&fs->lock);
1679         old_root = fs->root;
1680         old_rootmnt = fs->rootmnt;
1681         fs->rootmnt = mntget(mnt);
1682         fs->root = dget(dentry);
1683         write_unlock(&fs->lock);
1684         if (old_root) {
1685                 dput(old_root);
1686                 mntput(old_rootmnt);
1687         }
1688 }
1689
1690 EXPORT_SYMBOL_GPL(set_fs_root);
1691
1692 /*
1693  * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
1694  * It can block. Requires the big lock held.
1695  */
1696 void set_fs_pwd(struct fs_struct *fs, struct vfsmount *mnt,
1697                 struct dentry *dentry)
1698 {
1699         struct dentry *old_pwd;
1700         struct vfsmount *old_pwdmnt;
1701
1702         write_lock(&fs->lock);
1703         old_pwd = fs->pwd;
1704         old_pwdmnt = fs->pwdmnt;
1705         fs->pwdmnt = mntget(mnt);
1706         fs->pwd = dget(dentry);
1707         write_unlock(&fs->lock);
1708
1709         if (old_pwd) {
1710                 dput(old_pwd);
1711                 mntput(old_pwdmnt);
1712         }
1713 }
1714
1715 static void chroot_fs_refs(struct nameidata *old_nd, struct nameidata *new_nd)
1716 {
1717         struct task_struct *g, *p;
1718         struct fs_struct *fs;
1719
1720         read_lock(&tasklist_lock);
1721         do_each_thread(g, p) {
1722                 task_lock(p);
1723                 fs = p->fs;
1724                 if (fs) {
1725                         atomic_inc(&fs->count);
1726                         task_unlock(p);
1727                         if (fs->root == old_nd->dentry
1728                             && fs->rootmnt == old_nd->mnt)
1729                                 set_fs_root(fs, new_nd->mnt, new_nd->dentry);
1730                         if (fs->pwd == old_nd->dentry
1731                             && fs->pwdmnt == old_nd->mnt)
1732                                 set_fs_pwd(fs, new_nd->mnt, new_nd->dentry);
1733                         put_fs_struct(fs);
1734                 } else
1735                         task_unlock(p);
1736         } while_each_thread(g, p);
1737         read_unlock(&tasklist_lock);
1738 }
1739
1740 /*
1741  * pivot_root Semantics:
1742  * Moves the root file system of the current process to the directory put_old,
1743  * makes new_root as the new root file system of the current process, and sets
1744  * root/cwd of all processes which had them on the current root to new_root.
1745  *
1746  * Restrictions:
1747  * The new_root and put_old must be directories, and  must not be on the
1748  * same file  system as the current process root. The put_old  must  be
1749  * underneath new_root,  i.e. adding a non-zero number of /.. to the string
1750  * pointed to by put_old must yield the same directory as new_root. No other
1751  * file system may be mounted on put_old. After all, new_root is a mountpoint.
1752  *
1753  * Also, the current root cannot be on the 'rootfs' (initial ramfs) filesystem.
1754  * See Documentation/filesystems/ramfs-rootfs-initramfs.txt for alternatives
1755  * in this situation.
1756  *
1757  * Notes:
1758  *  - we don't move root/cwd if they are not at the root (reason: if something
1759  *    cared enough to change them, it's probably wrong to force them elsewhere)
1760  *  - it's okay to pick a root that isn't the root of a file system, e.g.
1761  *    /nfs/my_root where /nfs is the mount point. It must be a mountpoint,
1762  *    though, so you may need to say mount --bind /nfs/my_root /nfs/my_root
1763  *    first.
1764  */
1765 asmlinkage long sys_pivot_root(const char __user * new_root,
1766                                const char __user * put_old)
1767 {
1768         struct vfsmount *tmp;
1769         struct nameidata new_nd, old_nd, parent_nd, root_parent, user_nd;
1770         int error;
1771
1772         if (!capable(CAP_SYS_ADMIN))
1773                 return -EPERM;
1774
1775         lock_kernel();
1776
1777         error = __user_walk(new_root, LOOKUP_FOLLOW | LOOKUP_DIRECTORY,
1778                             &new_nd);
1779         if (error)
1780                 goto out0;
1781         error = -EINVAL;
1782         if (!check_mnt(new_nd.mnt))
1783                 goto out1;
1784
1785         error = __user_walk(put_old, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &old_nd);
1786         if (error)
1787                 goto out1;
1788
1789         error = security_sb_pivotroot(&old_nd, &new_nd);
1790         if (error) {
1791                 path_release(&old_nd);
1792                 goto out1;
1793         }
1794
1795         read_lock(&current->fs->lock);
1796         user_nd.mnt = mntget(current->fs->rootmnt);
1797         user_nd.dentry = dget(current->fs->root);
1798         read_unlock(&current->fs->lock);
1799         down_write(&namespace_sem);
1800         mutex_lock(&old_nd.dentry->d_inode->i_mutex);
1801         error = -EINVAL;
1802         if (IS_MNT_SHARED(old_nd.mnt) ||
1803                 IS_MNT_SHARED(new_nd.mnt->mnt_parent) ||
1804                 IS_MNT_SHARED(user_nd.mnt->mnt_parent))
1805                 goto out2;
1806         if (!check_mnt(user_nd.mnt))
1807                 goto out2;
1808         error = -ENOENT;
1809         if (IS_DEADDIR(new_nd.dentry->d_inode))
1810                 goto out2;
1811         if (d_unhashed(new_nd.dentry) && !IS_ROOT(new_nd.dentry))
1812                 goto out2;
1813         if (d_unhashed(old_nd.dentry) && !IS_ROOT(old_nd.dentry))
1814                 goto out2;
1815         error = -EBUSY;
1816         if (new_nd.mnt == user_nd.mnt || old_nd.mnt == user_nd.mnt)
1817                 goto out2; /* loop, on the same file system  */
1818         error = -EINVAL;
1819         if (user_nd.mnt->mnt_root != user_nd.dentry)
1820                 goto out2; /* not a mountpoint */
1821         if (user_nd.mnt->mnt_parent == user_nd.mnt)
1822                 goto out2; /* not attached */
1823         if (new_nd.mnt->mnt_root != new_nd.dentry)
1824                 goto out2; /* not a mountpoint */
1825         if (new_nd.mnt->mnt_parent == new_nd.mnt)
1826                 goto out2; /* not attached */
1827         tmp = old_nd.mnt; /* make sure we can reach put_old from new_root */
1828         spin_lock(&vfsmount_lock);
1829         if (tmp != new_nd.mnt) {
1830                 for (;;) {
1831                         if (tmp->mnt_parent == tmp)
1832                                 goto out3; /* already mounted on put_old */
1833                         if (tmp->mnt_parent == new_nd.mnt)
1834                                 break;
1835                         tmp = tmp->mnt_parent;
1836                 }
1837                 if (!is_subdir(tmp->mnt_mountpoint, new_nd.dentry))
1838                         goto out3;
1839         } else if (!is_subdir(old_nd.dentry, new_nd.dentry))
1840                 goto out3;
1841         detach_mnt(new_nd.mnt, &parent_nd);
1842         detach_mnt(user_nd.mnt, &root_parent);
1843         attach_mnt(user_nd.mnt, &old_nd);     /* mount old root on put_old */
1844         attach_mnt(new_nd.mnt, &root_parent); /* mount new_root on / */
1845         touch_namespace(current->namespace);
1846         spin_unlock(&vfsmount_lock);
1847         chroot_fs_refs(&user_nd, &new_nd);
1848         security_sb_post_pivotroot(&user_nd, &new_nd);
1849         error = 0;
1850         path_release(&root_parent);
1851         path_release(&parent_nd);
1852 out2:
1853         mutex_unlock(&old_nd.dentry->d_inode->i_mutex);
1854         up_write(&namespace_sem);
1855         path_release(&user_nd);
1856         path_release(&old_nd);
1857 out1:
1858         path_release(&new_nd);
1859 out0:
1860         unlock_kernel();
1861         return error;
1862 out3:
1863         spin_unlock(&vfsmount_lock);
1864         goto out2;
1865 }
1866
1867 static void __init init_mount_tree(void)
1868 {
1869         struct vfsmount *mnt;
1870         struct namespace *namespace;
1871         struct task_struct *g, *p;
1872
1873         mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
1874         if (IS_ERR(mnt))
1875                 panic("Can't create rootfs");
1876         namespace = kmalloc(sizeof(*namespace), GFP_KERNEL);
1877         if (!namespace)
1878                 panic("Can't allocate initial namespace");
1879         atomic_set(&namespace->count, 1);
1880         INIT_LIST_HEAD(&namespace->list);
1881         init_waitqueue_head(&namespace->poll);
1882         namespace->event = 0;
1883         list_add(&mnt->mnt_list, &namespace->list);
1884         namespace->root = mnt;
1885         mnt->mnt_namespace = namespace;
1886
1887         init_task.namespace = namespace;
1888         read_lock(&tasklist_lock);
1889         do_each_thread(g, p) {
1890                 get_namespace(namespace);
1891                 p->namespace = namespace;
1892         } while_each_thread(g, p);
1893         read_unlock(&tasklist_lock);
1894
1895         set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root);
1896         set_fs_root(current->fs, namespace->root, namespace->root->mnt_root);
1897 }
1898
1899 void __init mnt_init(unsigned long mempages)
1900 {
1901         struct list_head *d;
1902         unsigned int nr_hash;
1903         int i;
1904
1905         init_rwsem(&namespace_sem);
1906
1907         mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
1908                         0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL, NULL);
1909
1910         mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
1911
1912         if (!mount_hashtable)
1913                 panic("Failed to allocate mount hash table\n");
1914
1915         /*
1916          * Find the power-of-two list-heads that can fit into the allocation..
1917          * We don't guarantee that "sizeof(struct list_head)" is necessarily
1918          * a power-of-two.
1919          */
1920         nr_hash = PAGE_SIZE / sizeof(struct list_head);
1921         hash_bits = 0;
1922         do {
1923                 hash_bits++;
1924         } while ((nr_hash >> hash_bits) != 0);
1925         hash_bits--;
1926
1927         /*
1928          * Re-calculate the actual number of entries and the mask
1929          * from the number of bits we can fit.
1930          */
1931         nr_hash = 1UL << hash_bits;
1932         hash_mask = nr_hash - 1;
1933
1934         printk("Mount-cache hash table entries: %d\n", nr_hash);
1935
1936         /* And initialize the newly allocated array */
1937         d = mount_hashtable;
1938         i = nr_hash;
1939         do {
1940                 INIT_LIST_HEAD(d);
1941                 d++;
1942                 i--;
1943         } while (i);
1944         sysfs_init();
1945         subsystem_register(&fs_subsys);
1946         init_rootfs();
1947         init_mount_tree();
1948 }
1949
1950 void __put_namespace(struct namespace *namespace)
1951 {
1952         struct vfsmount *root = namespace->root;
1953         LIST_HEAD(umount_list);
1954         namespace->root = NULL;
1955         spin_unlock(&vfsmount_lock);
1956         down_write(&namespace_sem);
1957         spin_lock(&vfsmount_lock);
1958         umount_tree(root, 0, &umount_list);
1959         spin_unlock(&vfsmount_lock);
1960         up_write(&namespace_sem);
1961         release_mounts(&umount_list);
1962         kfree(namespace);
1963 }