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