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