| Bryan Schumaker | 129d197 | 2012-07-16 16:39:13 -0400 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (c) 2012 Bryan Schumaker <bjschuma@netapp.com> | 
|  | 3 | */ | 
|  | 4 | #include <linux/init.h> | 
| Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 5 | #include <linux/module.h> | 
| Bryan Schumaker | 129d197 | 2012-07-16 16:39:13 -0400 | [diff] [blame] | 6 | #include <linux/nfs_idmap.h> | 
| Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 7 | #include <linux/nfs4_mount.h> | 
| Bryan Schumaker | 466bfe7 | 2012-07-16 16:39:14 -0400 | [diff] [blame] | 8 | #include <linux/nfs_fs.h> | 
| Bryan Schumaker | 19d87ca | 2012-07-30 16:05:21 -0400 | [diff] [blame] | 9 | #include "delegation.h" | 
| Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 10 | #include "internal.h" | 
| Bryan Schumaker | 466bfe7 | 2012-07-16 16:39:14 -0400 | [diff] [blame] | 11 | #include "nfs4_fs.h" | 
| Bryan Schumaker | 19d87ca | 2012-07-30 16:05:21 -0400 | [diff] [blame] | 12 | #include "pnfs.h" | 
| Bryan Schumaker | ab7017a | 2012-07-30 16:05:16 -0400 | [diff] [blame] | 13 | #include "nfs.h" | 
| Bryan Schumaker | 129d197 | 2012-07-16 16:39:13 -0400 | [diff] [blame] | 14 |  | 
| Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 15 | #define NFSDBG_FACILITY		NFSDBG_VFS | 
|  | 16 |  | 
| Bryan Schumaker | 19d87ca | 2012-07-30 16:05:21 -0400 | [diff] [blame] | 17 | static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc); | 
|  | 18 | static void nfs4_evict_inode(struct inode *inode); | 
| Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 19 | static struct dentry *nfs4_remote_mount(struct file_system_type *fs_type, | 
|  | 20 | int flags, const char *dev_name, void *raw_data); | 
| Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 21 | static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type, | 
|  | 22 | int flags, const char *dev_name, void *raw_data); | 
|  | 23 | static struct dentry *nfs4_remote_referral_mount(struct file_system_type *fs_type, | 
|  | 24 | int flags, const char *dev_name, void *raw_data); | 
|  | 25 |  | 
| Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 26 | static struct file_system_type nfs4_remote_fs_type = { | 
|  | 27 | .owner		= THIS_MODULE, | 
|  | 28 | .name		= "nfs4", | 
|  | 29 | .mount		= nfs4_remote_mount, | 
|  | 30 | .kill_sb	= nfs_kill_super, | 
| Jeff Layton | ecf3d1f | 2013-02-20 11:19:05 -0500 | [diff] [blame] | 31 | .fs_flags	= FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA, | 
| Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 32 | }; | 
|  | 33 |  | 
| Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 34 | static struct file_system_type nfs4_remote_referral_fs_type = { | 
|  | 35 | .owner		= THIS_MODULE, | 
|  | 36 | .name		= "nfs4", | 
|  | 37 | .mount		= nfs4_remote_referral_mount, | 
|  | 38 | .kill_sb	= nfs_kill_super, | 
| Jeff Layton | ecf3d1f | 2013-02-20 11:19:05 -0500 | [diff] [blame] | 39 | .fs_flags	= FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA, | 
| Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 40 | }; | 
|  | 41 |  | 
|  | 42 | struct file_system_type nfs4_referral_fs_type = { | 
|  | 43 | .owner		= THIS_MODULE, | 
|  | 44 | .name		= "nfs4", | 
|  | 45 | .mount		= nfs4_referral_mount, | 
|  | 46 | .kill_sb	= nfs_kill_super, | 
| Jeff Layton | ecf3d1f | 2013-02-20 11:19:05 -0500 | [diff] [blame] | 47 | .fs_flags	= FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA, | 
| Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 48 | }; | 
|  | 49 |  | 
|  | 50 | static const struct super_operations nfs4_sops = { | 
|  | 51 | .alloc_inode	= nfs_alloc_inode, | 
|  | 52 | .destroy_inode	= nfs_destroy_inode, | 
|  | 53 | .write_inode	= nfs4_write_inode, | 
| Trond Myklebust | eed9935 | 2012-12-14 14:36:36 -0500 | [diff] [blame] | 54 | .drop_inode	= nfs_drop_inode, | 
| Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 55 | .put_super	= nfs_put_super, | 
|  | 56 | .statfs		= nfs_statfs, | 
|  | 57 | .evict_inode	= nfs4_evict_inode, | 
|  | 58 | .umount_begin	= nfs_umount_begin, | 
|  | 59 | .show_options	= nfs_show_options, | 
|  | 60 | .show_devname	= nfs_show_devname, | 
|  | 61 | .show_path	= nfs_show_path, | 
|  | 62 | .show_stats	= nfs_show_stats, | 
|  | 63 | .remount_fs	= nfs_remount, | 
|  | 64 | }; | 
|  | 65 |  | 
| Bryan Schumaker | ab7017a | 2012-07-30 16:05:16 -0400 | [diff] [blame] | 66 | struct nfs_subversion nfs_v4 = { | 
|  | 67 | .owner = THIS_MODULE, | 
|  | 68 | .nfs_fs   = &nfs4_fs_type, | 
|  | 69 | .rpc_vers = &nfs_version4, | 
|  | 70 | .rpc_ops  = &nfs_v4_clientops, | 
| Bryan Schumaker | 6a74490 | 2012-07-30 16:05:20 -0400 | [diff] [blame] | 71 | .sops     = &nfs4_sops, | 
|  | 72 | .xattr    = nfs4_xattr_handlers, | 
| Bryan Schumaker | ab7017a | 2012-07-30 16:05:16 -0400 | [diff] [blame] | 73 | }; | 
|  | 74 |  | 
| Bryan Schumaker | 19d87ca | 2012-07-30 16:05:21 -0400 | [diff] [blame] | 75 | static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc) | 
|  | 76 | { | 
|  | 77 | int ret = nfs_write_inode(inode, wbc); | 
|  | 78 |  | 
|  | 79 | if (ret >= 0 && test_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(inode)->flags)) { | 
|  | 80 | int status; | 
|  | 81 | bool sync = true; | 
|  | 82 |  | 
|  | 83 | if (wbc->sync_mode == WB_SYNC_NONE) | 
|  | 84 | sync = false; | 
|  | 85 |  | 
|  | 86 | status = pnfs_layoutcommit_inode(inode, sync); | 
|  | 87 | if (status < 0) | 
|  | 88 | return status; | 
|  | 89 | } | 
|  | 90 | return ret; | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | /* | 
|  | 94 | * Clean out any remaining NFSv4 state that might be left over due | 
|  | 95 | * to open() calls that passed nfs_atomic_lookup, but failed to call | 
|  | 96 | * nfs_open(). | 
|  | 97 | */ | 
|  | 98 | static void nfs4_evict_inode(struct inode *inode) | 
|  | 99 | { | 
|  | 100 | truncate_inode_pages(&inode->i_data, 0); | 
|  | 101 | clear_inode(inode); | 
|  | 102 | pnfs_return_layout(inode); | 
|  | 103 | pnfs_destroy_layout(NFS_I(inode)); | 
|  | 104 | /* If we are holding a delegation, return it! */ | 
|  | 105 | nfs_inode_return_delegation_noreclaim(inode); | 
|  | 106 | /* First call standard NFS clear_inode() code */ | 
|  | 107 | nfs_clear_inode(inode); | 
|  | 108 | } | 
|  | 109 |  | 
| Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 110 | /* | 
| Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 111 | * Get the superblock for the NFS4 root partition | 
|  | 112 | */ | 
|  | 113 | static struct dentry * | 
|  | 114 | nfs4_remote_mount(struct file_system_type *fs_type, int flags, | 
|  | 115 | const char *dev_name, void *info) | 
|  | 116 | { | 
|  | 117 | struct nfs_mount_info *mount_info = info; | 
|  | 118 | struct nfs_server *server; | 
|  | 119 | struct dentry *mntroot = ERR_PTR(-ENOMEM); | 
|  | 120 |  | 
| Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 121 | mount_info->set_security = nfs_set_sb_security; | 
|  | 122 |  | 
|  | 123 | /* Get a volume representation */ | 
| Bryan Schumaker | 1179acc | 2012-07-30 16:05:19 -0400 | [diff] [blame] | 124 | server = nfs4_create_server(mount_info, &nfs_v4); | 
| Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 125 | if (IS_ERR(server)) { | 
|  | 126 | mntroot = ERR_CAST(server); | 
|  | 127 | goto out; | 
|  | 128 | } | 
|  | 129 |  | 
| Bryan Schumaker | ab7017a | 2012-07-30 16:05:16 -0400 | [diff] [blame] | 130 | mntroot = nfs_fs_mount_common(server, flags, dev_name, mount_info, &nfs_v4); | 
| Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 131 |  | 
|  | 132 | out: | 
|  | 133 | return mntroot; | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | static struct vfsmount *nfs_do_root_mount(struct file_system_type *fs_type, | 
|  | 137 | int flags, void *data, const char *hostname) | 
|  | 138 | { | 
|  | 139 | struct vfsmount *root_mnt; | 
|  | 140 | char *root_devname; | 
|  | 141 | size_t len; | 
|  | 142 |  | 
|  | 143 | len = strlen(hostname) + 5; | 
|  | 144 | root_devname = kmalloc(len, GFP_KERNEL); | 
|  | 145 | if (root_devname == NULL) | 
|  | 146 | return ERR_PTR(-ENOMEM); | 
|  | 147 | /* Does hostname needs to be enclosed in brackets? */ | 
|  | 148 | if (strchr(hostname, ':')) | 
|  | 149 | snprintf(root_devname, len, "[%s]:/", hostname); | 
|  | 150 | else | 
|  | 151 | snprintf(root_devname, len, "%s:/", hostname); | 
|  | 152 | root_mnt = vfs_kern_mount(fs_type, flags, root_devname, data); | 
|  | 153 | kfree(root_devname); | 
|  | 154 | return root_mnt; | 
|  | 155 | } | 
|  | 156 |  | 
|  | 157 | struct nfs_referral_count { | 
|  | 158 | struct list_head list; | 
|  | 159 | const struct task_struct *task; | 
|  | 160 | unsigned int referral_count; | 
|  | 161 | }; | 
|  | 162 |  | 
|  | 163 | static LIST_HEAD(nfs_referral_count_list); | 
|  | 164 | static DEFINE_SPINLOCK(nfs_referral_count_list_lock); | 
|  | 165 |  | 
|  | 166 | static struct nfs_referral_count *nfs_find_referral_count(void) | 
|  | 167 | { | 
|  | 168 | struct nfs_referral_count *p; | 
|  | 169 |  | 
|  | 170 | list_for_each_entry(p, &nfs_referral_count_list, list) { | 
|  | 171 | if (p->task == current) | 
|  | 172 | return p; | 
|  | 173 | } | 
|  | 174 | return NULL; | 
|  | 175 | } | 
|  | 176 |  | 
|  | 177 | #define NFS_MAX_NESTED_REFERRALS 2 | 
|  | 178 |  | 
|  | 179 | static int nfs_referral_loop_protect(void) | 
|  | 180 | { | 
|  | 181 | struct nfs_referral_count *p, *new; | 
|  | 182 | int ret = -ENOMEM; | 
|  | 183 |  | 
|  | 184 | new = kmalloc(sizeof(*new), GFP_KERNEL); | 
|  | 185 | if (!new) | 
|  | 186 | goto out; | 
|  | 187 | new->task = current; | 
|  | 188 | new->referral_count = 1; | 
|  | 189 |  | 
|  | 190 | ret = 0; | 
|  | 191 | spin_lock(&nfs_referral_count_list_lock); | 
|  | 192 | p = nfs_find_referral_count(); | 
|  | 193 | if (p != NULL) { | 
|  | 194 | if (p->referral_count >= NFS_MAX_NESTED_REFERRALS) | 
|  | 195 | ret = -ELOOP; | 
|  | 196 | else | 
|  | 197 | p->referral_count++; | 
|  | 198 | } else { | 
|  | 199 | list_add(&new->list, &nfs_referral_count_list); | 
|  | 200 | new = NULL; | 
|  | 201 | } | 
|  | 202 | spin_unlock(&nfs_referral_count_list_lock); | 
|  | 203 | kfree(new); | 
|  | 204 | out: | 
|  | 205 | return ret; | 
|  | 206 | } | 
|  | 207 |  | 
|  | 208 | static void nfs_referral_loop_unprotect(void) | 
|  | 209 | { | 
|  | 210 | struct nfs_referral_count *p; | 
|  | 211 |  | 
|  | 212 | spin_lock(&nfs_referral_count_list_lock); | 
|  | 213 | p = nfs_find_referral_count(); | 
|  | 214 | p->referral_count--; | 
|  | 215 | if (p->referral_count == 0) | 
|  | 216 | list_del(&p->list); | 
|  | 217 | else | 
|  | 218 | p = NULL; | 
|  | 219 | spin_unlock(&nfs_referral_count_list_lock); | 
|  | 220 | kfree(p); | 
|  | 221 | } | 
|  | 222 |  | 
|  | 223 | static struct dentry *nfs_follow_remote_path(struct vfsmount *root_mnt, | 
|  | 224 | const char *export_path) | 
|  | 225 | { | 
|  | 226 | struct dentry *dentry; | 
|  | 227 | int err; | 
|  | 228 |  | 
|  | 229 | if (IS_ERR(root_mnt)) | 
|  | 230 | return ERR_CAST(root_mnt); | 
|  | 231 |  | 
|  | 232 | err = nfs_referral_loop_protect(); | 
|  | 233 | if (err) { | 
|  | 234 | mntput(root_mnt); | 
|  | 235 | return ERR_PTR(err); | 
|  | 236 | } | 
|  | 237 |  | 
|  | 238 | dentry = mount_subtree(root_mnt, export_path); | 
|  | 239 | nfs_referral_loop_unprotect(); | 
|  | 240 |  | 
|  | 241 | return dentry; | 
|  | 242 | } | 
|  | 243 |  | 
|  | 244 | struct dentry *nfs4_try_mount(int flags, const char *dev_name, | 
| Bryan Schumaker | ff9099f2 | 2012-07-30 16:05:18 -0400 | [diff] [blame] | 245 | struct nfs_mount_info *mount_info, | 
|  | 246 | struct nfs_subversion *nfs_mod) | 
| Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 247 | { | 
|  | 248 | char *export_path; | 
|  | 249 | struct vfsmount *root_mnt; | 
|  | 250 | struct dentry *res; | 
|  | 251 | struct nfs_parsed_mount_data *data = mount_info->parsed; | 
|  | 252 |  | 
|  | 253 | dfprintk(MOUNT, "--> nfs4_try_mount()\n"); | 
|  | 254 |  | 
| Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 255 | export_path = data->nfs_server.export_path; | 
|  | 256 | data->nfs_server.export_path = "/"; | 
|  | 257 | root_mnt = nfs_do_root_mount(&nfs4_remote_fs_type, flags, mount_info, | 
|  | 258 | data->nfs_server.hostname); | 
|  | 259 | data->nfs_server.export_path = export_path; | 
|  | 260 |  | 
|  | 261 | res = nfs_follow_remote_path(root_mnt, export_path); | 
|  | 262 |  | 
|  | 263 | dfprintk(MOUNT, "<-- nfs4_try_mount() = %ld%s\n", | 
|  | 264 | IS_ERR(res) ? PTR_ERR(res) : 0, | 
|  | 265 | IS_ERR(res) ? " [error]" : ""); | 
|  | 266 | return res; | 
|  | 267 | } | 
|  | 268 |  | 
| Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 269 | static struct dentry * | 
|  | 270 | nfs4_remote_referral_mount(struct file_system_type *fs_type, int flags, | 
|  | 271 | const char *dev_name, void *raw_data) | 
|  | 272 | { | 
|  | 273 | struct nfs_mount_info mount_info = { | 
| Bryan Schumaker | 6a74490 | 2012-07-30 16:05:20 -0400 | [diff] [blame] | 274 | .fill_super = nfs_fill_super, | 
| Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 275 | .set_security = nfs_clone_sb_security, | 
|  | 276 | .cloned = raw_data, | 
|  | 277 | }; | 
|  | 278 | struct nfs_server *server; | 
|  | 279 | struct dentry *mntroot = ERR_PTR(-ENOMEM); | 
|  | 280 |  | 
|  | 281 | dprintk("--> nfs4_referral_get_sb()\n"); | 
|  | 282 |  | 
|  | 283 | mount_info.mntfh = nfs_alloc_fhandle(); | 
|  | 284 | if (mount_info.cloned == NULL || mount_info.mntfh == NULL) | 
|  | 285 | goto out; | 
|  | 286 |  | 
|  | 287 | /* create a new volume representation */ | 
|  | 288 | server = nfs4_create_referral_server(mount_info.cloned, mount_info.mntfh); | 
|  | 289 | if (IS_ERR(server)) { | 
|  | 290 | mntroot = ERR_CAST(server); | 
|  | 291 | goto out; | 
|  | 292 | } | 
|  | 293 |  | 
| Bryan Schumaker | ab7017a | 2012-07-30 16:05:16 -0400 | [diff] [blame] | 294 | mntroot = nfs_fs_mount_common(server, flags, dev_name, &mount_info, &nfs_v4); | 
| Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 295 | out: | 
|  | 296 | nfs_free_fhandle(mount_info.mntfh); | 
|  | 297 | return mntroot; | 
|  | 298 | } | 
|  | 299 |  | 
|  | 300 | /* | 
|  | 301 | * Create an NFS4 server record on referral traversal | 
|  | 302 | */ | 
|  | 303 | static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type, | 
|  | 304 | int flags, const char *dev_name, void *raw_data) | 
|  | 305 | { | 
|  | 306 | struct nfs_clone_mount *data = raw_data; | 
|  | 307 | char *export_path; | 
|  | 308 | struct vfsmount *root_mnt; | 
|  | 309 | struct dentry *res; | 
|  | 310 |  | 
|  | 311 | dprintk("--> nfs4_referral_mount()\n"); | 
|  | 312 |  | 
|  | 313 | export_path = data->mnt_path; | 
|  | 314 | data->mnt_path = "/"; | 
|  | 315 |  | 
|  | 316 | root_mnt = nfs_do_root_mount(&nfs4_remote_referral_fs_type, | 
|  | 317 | flags, data, data->hostname); | 
|  | 318 | data->mnt_path = export_path; | 
|  | 319 |  | 
|  | 320 | res = nfs_follow_remote_path(root_mnt, export_path); | 
|  | 321 | dprintk("<-- nfs4_referral_mount() = %ld%s\n", | 
|  | 322 | IS_ERR(res) ? PTR_ERR(res) : 0, | 
|  | 323 | IS_ERR(res) ? " [error]" : ""); | 
|  | 324 | return res; | 
|  | 325 | } | 
|  | 326 |  | 
|  | 327 |  | 
| Bryan Schumaker | 89d77c8 | 2012-07-30 16:05:25 -0400 | [diff] [blame] | 328 | static int __init init_nfs_v4(void) | 
| Bryan Schumaker | 129d197 | 2012-07-16 16:39:13 -0400 | [diff] [blame] | 329 | { | 
|  | 330 | int err; | 
|  | 331 |  | 
|  | 332 | err = nfs_idmap_init(); | 
|  | 333 | if (err) | 
|  | 334 | goto out; | 
|  | 335 |  | 
| Bryan Schumaker | 466bfe7 | 2012-07-16 16:39:14 -0400 | [diff] [blame] | 336 | err = nfs4_register_sysctl(); | 
|  | 337 | if (err) | 
|  | 338 | goto out1; | 
|  | 339 |  | 
| Bryan Schumaker | ab7017a | 2012-07-30 16:05:16 -0400 | [diff] [blame] | 340 | register_nfs_version(&nfs_v4); | 
| Bryan Schumaker | 129d197 | 2012-07-16 16:39:13 -0400 | [diff] [blame] | 341 | return 0; | 
| Bryan Schumaker | 466bfe7 | 2012-07-16 16:39:14 -0400 | [diff] [blame] | 342 | out1: | 
|  | 343 | nfs_idmap_quit(); | 
| Bryan Schumaker | 129d197 | 2012-07-16 16:39:13 -0400 | [diff] [blame] | 344 | out: | 
|  | 345 | return err; | 
|  | 346 | } | 
|  | 347 |  | 
| Bryan Schumaker | 89d77c8 | 2012-07-30 16:05:25 -0400 | [diff] [blame] | 348 | static void __exit exit_nfs_v4(void) | 
| Bryan Schumaker | 129d197 | 2012-07-16 16:39:13 -0400 | [diff] [blame] | 349 | { | 
| Bryan Schumaker | ab7017a | 2012-07-30 16:05:16 -0400 | [diff] [blame] | 350 | unregister_nfs_version(&nfs_v4); | 
| Bryan Schumaker | 466bfe7 | 2012-07-16 16:39:14 -0400 | [diff] [blame] | 351 | nfs4_unregister_sysctl(); | 
| Bryan Schumaker | 129d197 | 2012-07-16 16:39:13 -0400 | [diff] [blame] | 352 | nfs_idmap_quit(); | 
|  | 353 | } | 
| Bryan Schumaker | 89d77c8 | 2012-07-30 16:05:25 -0400 | [diff] [blame] | 354 |  | 
|  | 355 | MODULE_LICENSE("GPL"); | 
|  | 356 |  | 
|  | 357 | module_init(init_nfs_v4); | 
|  | 358 | module_exit(exit_nfs_v4); |