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/fs.h> |
| 16 | #include <linux/pagemap.h> |
| 17 | #include <linux/mount.h> |
| 18 | #include <linux/namei.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/gfp.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, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 25 | unsigned int flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | static int afs_mntpt_open(struct inode *inode, struct file *file); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 27 | static void afs_mntpt_expiry_timed_out(struct work_struct *work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 29 | const struct file_operations afs_mntpt_file_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | .open = afs_mntpt_open, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 31 | .llseek = noop_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | .readlink = page_readlink, |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 37 | .getattr = afs_getattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 40 | const struct inode_operations afs_autocell_inode_operations = { |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 41 | .getattr = afs_getattr, |
| 42 | }; |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | static LIST_HEAD(afs_vfsmounts); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 45 | 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] | 46 | |
Adrian Bunk | c1206a2 | 2007-10-16 23:26:41 -0700 | [diff] [blame] | 47 | static unsigned long afs_mntpt_expiry_timeout = 10 * 60; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | /* |
| 50 | * check a symbolic link to see whether it actually encodes a mountpoint |
| 51 | * - sets the AFS_VNODE_MOUNTPOINT flag on the vnode appropriately |
| 52 | */ |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 53 | int afs_mntpt_check_symlink(struct afs_vnode *vnode, struct key *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | { |
| 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 */ |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 64 | page = read_cache_page(AFS_VNODE_TO_I(vnode)->i_mapping, 0, |
| 65 | afs_page_filler, key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | if (IS_ERR(page)) { |
| 67 | ret = PTR_ERR(page); |
| 68 | goto out; |
| 69 | } |
| 70 | |
| 71 | ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | if (PageError(page)) |
| 73 | goto out_free; |
| 74 | |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 75 | buf = kmap(page); |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | /* examine the symlink's contents */ |
| 78 | size = vnode->status.size; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 79 | _debug("symlink to %*.*s", (int) size, (int) size, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | if (size > 2 && |
| 82 | (buf[0] == '%' || buf[0] == '#') && |
| 83 | buf[size - 1] == '.' |
| 84 | ) { |
| 85 | _debug("symlink is a mountpoint"); |
| 86 | spin_lock(&vnode->lock); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 87 | set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags); |
David Howells | d18610b | 2011-01-14 19:04:05 +0000 | [diff] [blame] | 88 | vnode->vfs_inode.i_flags |= S_AUTOMOUNT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | spin_unlock(&vnode->lock); |
| 90 | } |
| 91 | |
| 92 | ret = 0; |
| 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | kunmap(page); |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 95 | out_free: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 107 | unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 109 | _enter("%p,%p{%pd2}", dir, dentry, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | return ERR_PTR(-EREMOTE); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 111 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | /* |
| 114 | * no valid open procedure on this sort of dir |
| 115 | */ |
| 116 | static int afs_mntpt_open(struct inode *inode, struct file *file) |
| 117 | { |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 118 | _enter("%p,%p{%pD2}", inode, file, file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | return -EREMOTE; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 120 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | /* |
| 123 | * create a vfsmount to be automounted |
| 124 | */ |
| 125 | static struct vfsmount *afs_mntpt_do_automount(struct dentry *mntpt) |
| 126 | { |
| 127 | struct afs_super_info *super; |
| 128 | struct vfsmount *mnt; |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 129 | struct afs_vnode *vnode; |
David Howells | 083fd8b | 2010-04-21 12:01:23 +0100 | [diff] [blame] | 130 | struct page *page; |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 131 | char *devname, *options; |
| 132 | bool rwpath = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | int ret; |
| 134 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 135 | _enter("{%pd}", mntpt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 137 | BUG_ON(!d_inode(mntpt)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | ret = -ENOMEM; |
| 140 | devname = (char *) get_zeroed_page(GFP_KERNEL); |
| 141 | if (!devname) |
David Howells | 083fd8b | 2010-04-21 12:01:23 +0100 | [diff] [blame] | 142 | goto error_no_devname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
| 144 | options = (char *) get_zeroed_page(GFP_KERNEL); |
| 145 | if (!options) |
David Howells | 083fd8b | 2010-04-21 12:01:23 +0100 | [diff] [blame] | 146 | goto error_no_options; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 148 | vnode = AFS_FS_I(d_inode(mntpt)); |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 149 | if (test_bit(AFS_VNODE_PSEUDODIR, &vnode->flags)) { |
| 150 | /* if the directory is a pseudo directory, use the d_name */ |
| 151 | static const char afs_root_cell[] = ":root.cell."; |
| 152 | unsigned size = mntpt->d_name.len; |
| 153 | |
| 154 | ret = -ENOENT; |
| 155 | if (size < 2 || size > AFS_MAXCELLNAME) |
| 156 | goto error_no_page; |
| 157 | |
| 158 | if (mntpt->d_name.name[0] == '.') { |
| 159 | devname[0] = '#'; |
| 160 | memcpy(devname + 1, mntpt->d_name.name, size - 1); |
| 161 | memcpy(devname + size, afs_root_cell, |
| 162 | sizeof(afs_root_cell)); |
| 163 | rwpath = true; |
| 164 | } else { |
| 165 | devname[0] = '%'; |
| 166 | memcpy(devname + 1, mntpt->d_name.name, size); |
| 167 | memcpy(devname + size + 1, afs_root_cell, |
| 168 | sizeof(afs_root_cell)); |
| 169 | } |
| 170 | } else { |
| 171 | /* read the contents of the AFS special symlink */ |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 172 | loff_t size = i_size_read(d_inode(mntpt)); |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 173 | char *buf; |
| 174 | |
| 175 | ret = -EINVAL; |
| 176 | if (size > PAGE_SIZE - 1) |
| 177 | goto error_no_page; |
| 178 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 179 | page = read_mapping_page(d_inode(mntpt)->i_mapping, 0, NULL); |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 180 | if (IS_ERR(page)) { |
| 181 | ret = PTR_ERR(page); |
| 182 | goto error_no_page; |
| 183 | } |
| 184 | |
| 185 | ret = -EIO; |
| 186 | if (PageError(page)) |
| 187 | goto error; |
| 188 | |
Cong Wang | da4aa36 | 2011-11-25 23:14:26 +0800 | [diff] [blame] | 189 | buf = kmap_atomic(page); |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 190 | memcpy(devname, buf, size); |
Cong Wang | da4aa36 | 2011-11-25 23:14:26 +0800 | [diff] [blame] | 191 | kunmap_atomic(buf); |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 192 | page_cache_release(page); |
| 193 | page = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | /* work out what options we want */ |
| 197 | super = AFS_FS_S(mntpt->d_sb); |
| 198 | memcpy(options, "cell=", 5); |
| 199 | strcpy(options + 5, super->volume->cell->name); |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 200 | if (super->volume->type == AFSVL_RWVOL || rwpath) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | strcat(options, ",rwpath"); |
| 202 | |
| 203 | /* try and do the mount */ |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 204 | _debug("--- attempting mount %s -o %s ---", devname, options); |
Trond Myklebust | 1f5ce9e | 2006-06-09 09:34:16 -0400 | [diff] [blame] | 205 | mnt = vfs_kern_mount(&afs_fs_type, 0, devname, options); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 206 | _debug("--- mount result %p ---", mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
| 208 | free_page((unsigned long) devname); |
| 209 | free_page((unsigned long) options); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 210 | _leave(" = %p", mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | return mnt; |
| 212 | |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 213 | error: |
David Howells | 083fd8b | 2010-04-21 12:01:23 +0100 | [diff] [blame] | 214 | page_cache_release(page); |
| 215 | error_no_page: |
| 216 | free_page((unsigned long) options); |
| 217 | error_no_options: |
| 218 | free_page((unsigned long) devname); |
| 219 | error_no_devname: |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 220 | _leave(" = %d", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | return ERR_PTR(ret); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 222 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | /* |
David Howells | d18610b | 2011-01-14 19:04:05 +0000 | [diff] [blame] | 225 | * handle an automount point |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | */ |
David Howells | d18610b | 2011-01-14 19:04:05 +0000 | [diff] [blame] | 227 | struct vfsmount *afs_d_automount(struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | { |
| 229 | struct vfsmount *newmnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 231 | _enter("{%pd}", path->dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
David Howells | d18610b | 2011-01-14 19:04:05 +0000 | [diff] [blame] | 233 | newmnt = afs_mntpt_do_automount(path->dentry); |
| 234 | if (IS_ERR(newmnt)) |
| 235 | return newmnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
David Howells | ea5b778 | 2011-01-14 19:10:03 +0000 | [diff] [blame] | 237 | mntget(newmnt); /* prevent immediate expiration */ |
| 238 | mnt_set_expiry(newmnt, &afs_vfsmounts); |
| 239 | queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer, |
| 240 | afs_mntpt_expiry_timeout * HZ); |
Al Viro | 5ffc283 | 2011-11-25 02:22:06 -0500 | [diff] [blame] | 241 | _leave(" = %p", newmnt); |
David Howells | ea5b778 | 2011-01-14 19:10:03 +0000 | [diff] [blame] | 242 | return newmnt; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 243 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | /* |
| 246 | * handle mountpoint expiry timer going off |
| 247 | */ |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 248 | static void afs_mntpt_expiry_timed_out(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 250 | _enter(""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 252 | if (!list_empty(&afs_vfsmounts)) { |
| 253 | mark_mounts_for_expiry(&afs_vfsmounts); |
Tejun Heo | 0ad53ee | 2011-01-14 15:56:37 +0000 | [diff] [blame] | 254 | queue_delayed_work(afs_wq, &afs_mntpt_expiry_timer, |
| 255 | afs_mntpt_expiry_timeout * HZ); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 256 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 258 | _leave(""); |
| 259 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 261 | /* |
| 262 | * kill the AFS mountpoint timer if it's still running |
| 263 | */ |
| 264 | void afs_mntpt_kill_timer(void) |
| 265 | { |
| 266 | _enter(""); |
| 267 | |
| 268 | ASSERT(list_empty(&afs_vfsmounts)); |
Tejun Heo | 0ad53ee | 2011-01-14 15:56:37 +0000 | [diff] [blame] | 269 | cancel_delayed_work_sync(&afs_mntpt_expiry_timer); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 270 | } |