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