David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 1 | /* mountpoint management |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * |
| 3 | * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved. |
| 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/slab.h> |
| 16 | #include <linux/fs.h> |
| 17 | #include <linux/pagemap.h> |
| 18 | #include <linux/mount.h> |
| 19 | #include <linux/namei.h> |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 20 | #include <linux/mnt_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "internal.h" |
| 22 | |
| 23 | |
| 24 | static struct dentry *afs_mntpt_lookup(struct inode *dir, |
| 25 | struct dentry *dentry, |
| 26 | struct nameidata *nd); |
| 27 | static int afs_mntpt_open(struct inode *inode, struct file *file); |
Al Viro | 008b150 | 2005-08-20 00:17:39 +0100 | [diff] [blame] | 28 | static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 29 | static void afs_mntpt_expiry_timed_out(struct work_struct *work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 31 | const struct file_operations afs_mntpt_file_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | .open = afs_mntpt_open, |
| 33 | }; |
| 34 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 35 | const struct inode_operations afs_mntpt_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | .lookup = afs_mntpt_lookup, |
| 37 | .follow_link = afs_mntpt_follow_link, |
| 38 | .readlink = page_readlink, |
| 39 | .getattr = afs_inode_getattr, |
| 40 | }; |
| 41 | |
| 42 | static LIST_HEAD(afs_vfsmounts); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 43 | static DECLARE_DELAYED_WORK(afs_mntpt_expiry_timer, afs_mntpt_expiry_timed_out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 45 | unsigned long afs_mntpt_expiry_timeout = 10 * 60; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | /* |
| 48 | * check a symbolic link to see whether it actually encodes a mountpoint |
| 49 | * - sets the AFS_VNODE_MOUNTPOINT flag on the vnode appropriately |
| 50 | */ |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame^] | 51 | int afs_mntpt_check_symlink(struct afs_vnode *vnode, struct key *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame^] | 53 | struct file file = { |
| 54 | .private_data = key, |
| 55 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | size_t size; |
| 58 | char *buf; |
| 59 | int ret; |
| 60 | |
| 61 | _enter("{%u,%u}", vnode->fid.vnode, vnode->fid.unique); |
| 62 | |
| 63 | /* read the contents of the symlink into the pagecache */ |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame^] | 64 | page = read_mapping_page(AFS_VNODE_TO_I(vnode)->i_mapping, 0, &file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | if (IS_ERR(page)) { |
| 66 | ret = PTR_ERR(page); |
| 67 | goto out; |
| 68 | } |
| 69 | |
| 70 | ret = -EIO; |
| 71 | wait_on_page_locked(page); |
| 72 | buf = kmap(page); |
| 73 | if (!PageUptodate(page)) |
| 74 | goto out_free; |
| 75 | if (PageError(page)) |
| 76 | goto out_free; |
| 77 | |
| 78 | /* examine the symlink's contents */ |
| 79 | size = vnode->status.size; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 80 | _debug("symlink to %*.*s", (int) size, (int) size, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
| 82 | if (size > 2 && |
| 83 | (buf[0] == '%' || buf[0] == '#') && |
| 84 | buf[size - 1] == '.' |
| 85 | ) { |
| 86 | _debug("symlink is a mountpoint"); |
| 87 | spin_lock(&vnode->lock); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 88 | set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | spin_unlock(&vnode->lock); |
| 90 | } |
| 91 | |
| 92 | ret = 0; |
| 93 | |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 94 | out_free: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | kunmap(page); |
| 96 | page_cache_release(page); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 97 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | _leave(" = %d", ret); |
| 99 | return ret; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 100 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | /* |
| 103 | * no valid lookup procedure on this sort of dir |
| 104 | */ |
| 105 | static struct dentry *afs_mntpt_lookup(struct inode *dir, |
| 106 | struct dentry *dentry, |
| 107 | struct nameidata *nd) |
| 108 | { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 109 | _enter("%p,%p{%p{%s},%s}", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | dir, |
| 111 | dentry, |
| 112 | dentry->d_parent, |
| 113 | dentry->d_parent ? |
| 114 | dentry->d_parent->d_name.name : (const unsigned char *) "", |
| 115 | dentry->d_name.name); |
| 116 | |
| 117 | return ERR_PTR(-EREMOTE); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 118 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | /* |
| 121 | * no valid open procedure on this sort of dir |
| 122 | */ |
| 123 | static int afs_mntpt_open(struct inode *inode, struct file *file) |
| 124 | { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 125 | _enter("%p,%p{%p{%s},%s}", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | inode, file, |
Josef Sipek | 1d56a96 | 2006-12-08 02:36:50 -0800 | [diff] [blame] | 127 | file->f_path.dentry->d_parent, |
| 128 | file->f_path.dentry->d_parent ? |
| 129 | file->f_path.dentry->d_parent->d_name.name : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | (const unsigned char *) "", |
Josef Sipek | 1d56a96 | 2006-12-08 02:36:50 -0800 | [diff] [blame] | 131 | file->f_path.dentry->d_name.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
| 133 | return -EREMOTE; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 134 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | /* |
| 137 | * create a vfsmount to be automounted |
| 138 | */ |
| 139 | static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt) |
| 140 | { |
| 141 | struct afs_super_info *super; |
| 142 | struct vfsmount *mnt; |
| 143 | struct page *page = NULL; |
| 144 | size_t size; |
| 145 | char *buf, *devname = NULL, *options = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | int ret; |
| 147 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 148 | _enter("{%s}", mntpt->d_name.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
| 150 | BUG_ON(!mntpt->d_inode); |
| 151 | |
| 152 | ret = -EINVAL; |
| 153 | size = mntpt->d_inode->i_size; |
| 154 | if (size > PAGE_SIZE - 1) |
| 155 | goto error; |
| 156 | |
| 157 | ret = -ENOMEM; |
| 158 | devname = (char *) get_zeroed_page(GFP_KERNEL); |
| 159 | if (!devname) |
| 160 | goto error; |
| 161 | |
| 162 | options = (char *) get_zeroed_page(GFP_KERNEL); |
| 163 | if (!options) |
| 164 | goto error; |
| 165 | |
| 166 | /* read the contents of the AFS special symlink */ |
Pekka Enberg | 090d2b1 | 2006-06-23 02:05:08 -0700 | [diff] [blame] | 167 | page = read_mapping_page(mntpt->d_inode->i_mapping, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | if (IS_ERR(page)) { |
| 169 | ret = PTR_ERR(page); |
| 170 | goto error; |
| 171 | } |
| 172 | |
| 173 | ret = -EIO; |
| 174 | wait_on_page_locked(page); |
| 175 | if (!PageUptodate(page) || PageError(page)) |
| 176 | goto error; |
| 177 | |
| 178 | buf = kmap(page); |
| 179 | memcpy(devname, buf, size); |
| 180 | kunmap(page); |
| 181 | page_cache_release(page); |
| 182 | page = NULL; |
| 183 | |
| 184 | /* work out what options we want */ |
| 185 | super = AFS_FS_S(mntpt->d_sb); |
| 186 | memcpy(options, "cell=", 5); |
| 187 | strcpy(options + 5, super->volume->cell->name); |
| 188 | if (super->volume->type == AFSVL_RWVOL) |
| 189 | strcat(options, ",rwpath"); |
| 190 | |
| 191 | /* try and do the mount */ |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 192 | _debug("--- attempting mount %s -o %s ---", devname, options); |
Trond Myklebust | 1f5ce9e | 2006-06-09 09:34:16 -0400 | [diff] [blame] | 193 | mnt = vfs_kern_mount(&afs_fs_type, 0, devname, options); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 194 | _debug("--- mount result %p ---", mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
| 196 | free_page((unsigned long) devname); |
| 197 | free_page((unsigned long) options); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 198 | _leave(" = %p", mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | return mnt; |
| 200 | |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 201 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | if (page) |
| 203 | page_cache_release(page); |
| 204 | if (devname) |
| 205 | free_page((unsigned long) devname); |
| 206 | if (options) |
| 207 | free_page((unsigned long) options); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 208 | _leave(" = %d", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | return ERR_PTR(ret); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 210 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | /* |
| 213 | * follow a link from a mountpoint directory, thus causing it to be mounted |
| 214 | */ |
Al Viro | 008b150 | 2005-08-20 00:17:39 +0100 | [diff] [blame] | 215 | static void *afs_mntpt_follow_link(struct dentry *dentry, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | { |
| 217 | struct vfsmount *newmnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | int err; |
| 219 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame^] | 220 | _enter("%p{%s},{%s:%p{%s},}", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | dentry, |
| 222 | dentry->d_name.name, |
| 223 | nd->mnt->mnt_devname, |
| 224 | dentry, |
| 225 | nd->dentry->d_name.name); |
| 226 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 227 | dput(nd->dentry); |
| 228 | nd->dentry = dget(dentry); |
| 229 | |
| 230 | newmnt = afs_mntpt_do_automount(nd->dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | if (IS_ERR(newmnt)) { |
| 232 | path_release(nd); |
Al Viro | 008b150 | 2005-08-20 00:17:39 +0100 | [diff] [blame] | 233 | return (void *)newmnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | } |
| 235 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 236 | mntget(newmnt); |
| 237 | err = do_add_mount(newmnt, nd, MNT_SHRINKABLE, &afs_vfsmounts); |
| 238 | switch (err) { |
| 239 | case 0: |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame^] | 240 | mntput(nd->mnt); |
| 241 | dput(nd->dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | nd->mnt = newmnt; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 243 | nd->dentry = dget(newmnt->mnt_root); |
| 244 | schedule_delayed_work(&afs_mntpt_expiry_timer, |
| 245 | afs_mntpt_expiry_timeout * HZ); |
| 246 | break; |
| 247 | case -EBUSY: |
| 248 | /* someone else made a mount here whilst we were busy */ |
| 249 | while (d_mountpoint(nd->dentry) && |
| 250 | follow_down(&nd->mnt, &nd->dentry)) |
| 251 | ; |
| 252 | err = 0; |
| 253 | default: |
| 254 | mntput(newmnt); |
| 255 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
| 257 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 258 | _leave(" = %d", err); |
Al Viro | 008b150 | 2005-08-20 00:17:39 +0100 | [diff] [blame] | 259 | return ERR_PTR(err); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 260 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | /* |
| 263 | * handle mountpoint expiry timer going off |
| 264 | */ |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 265 | static void afs_mntpt_expiry_timed_out(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 267 | _enter(""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 269 | if (!list_empty(&afs_vfsmounts)) { |
| 270 | mark_mounts_for_expiry(&afs_vfsmounts); |
| 271 | schedule_delayed_work(&afs_mntpt_expiry_timer, |
| 272 | afs_mntpt_expiry_timeout * HZ); |
| 273 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 275 | _leave(""); |
| 276 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 278 | /* |
| 279 | * kill the AFS mountpoint timer if it's still running |
| 280 | */ |
| 281 | void afs_mntpt_kill_timer(void) |
| 282 | { |
| 283 | _enter(""); |
| 284 | |
| 285 | ASSERT(list_empty(&afs_vfsmounts)); |
| 286 | cancel_delayed_work(&afs_mntpt_expiry_timer); |
| 287 | flush_scheduled_work(); |
| 288 | } |
| 289 | |
| 290 | /* |
| 291 | * begin unmount by attempting to remove all automounted mountpoints we added |
| 292 | */ |
| 293 | void afs_umount_begin(struct vfsmount *vfsmnt, int flags) |
| 294 | { |
| 295 | shrink_submounts(vfsmnt, &afs_vfsmounts); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 296 | } |