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