1129d1977SBryan Schumaker /* 2129d1977SBryan Schumaker * Copyright (c) 2012 Bryan Schumaker <bjschuma@netapp.com> 3129d1977SBryan Schumaker */ 4129d1977SBryan Schumaker #include <linux/init.h> 5fbdefd64SBryan Schumaker #include <linux/module.h> 6129d1977SBryan Schumaker #include <linux/nfs_idmap.h> 7fbdefd64SBryan Schumaker #include <linux/nfs4_mount.h> 8466bfe7fSBryan Schumaker #include <linux/nfs_fs.h> 919d87ca3SBryan Schumaker #include "delegation.h" 10fbdefd64SBryan Schumaker #include "internal.h" 11466bfe7fSBryan Schumaker #include "nfs4_fs.h" 12c8d74d9bSTrond Myklebust #include "dns_resolve.h" 1319d87ca3SBryan Schumaker #include "pnfs.h" 14ab7017a3SBryan Schumaker #include "nfs.h" 15129d1977SBryan Schumaker 16fbdefd64SBryan Schumaker #define NFSDBG_FACILITY NFSDBG_VFS 17fbdefd64SBryan Schumaker 1819d87ca3SBryan Schumaker static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc); 1919d87ca3SBryan Schumaker static void nfs4_evict_inode(struct inode *inode); 20fbdefd64SBryan Schumaker static struct dentry *nfs4_remote_mount(struct file_system_type *fs_type, 21fbdefd64SBryan Schumaker int flags, const char *dev_name, void *raw_data); 22fbdefd64SBryan Schumaker static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type, 23fbdefd64SBryan Schumaker int flags, const char *dev_name, void *raw_data); 24fbdefd64SBryan Schumaker static struct dentry *nfs4_remote_referral_mount(struct file_system_type *fs_type, 25fbdefd64SBryan Schumaker int flags, const char *dev_name, void *raw_data); 26fbdefd64SBryan Schumaker 27fbdefd64SBryan Schumaker static struct file_system_type nfs4_remote_fs_type = { 28fbdefd64SBryan Schumaker .owner = THIS_MODULE, 29fbdefd64SBryan Schumaker .name = "nfs4", 30fbdefd64SBryan Schumaker .mount = nfs4_remote_mount, 31fbdefd64SBryan Schumaker .kill_sb = nfs_kill_super, 32ecf3d1f1SJeff Layton .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA, 33fbdefd64SBryan Schumaker }; 34fbdefd64SBryan Schumaker 35fbdefd64SBryan Schumaker static struct file_system_type nfs4_remote_referral_fs_type = { 36fbdefd64SBryan Schumaker .owner = THIS_MODULE, 37fbdefd64SBryan Schumaker .name = "nfs4", 38fbdefd64SBryan Schumaker .mount = nfs4_remote_referral_mount, 39fbdefd64SBryan Schumaker .kill_sb = nfs_kill_super, 40ecf3d1f1SJeff Layton .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA, 41fbdefd64SBryan Schumaker }; 42fbdefd64SBryan Schumaker 43fbdefd64SBryan Schumaker struct file_system_type nfs4_referral_fs_type = { 44fbdefd64SBryan Schumaker .owner = THIS_MODULE, 45fbdefd64SBryan Schumaker .name = "nfs4", 46fbdefd64SBryan Schumaker .mount = nfs4_referral_mount, 47fbdefd64SBryan Schumaker .kill_sb = nfs_kill_super, 48ecf3d1f1SJeff Layton .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA, 49fbdefd64SBryan Schumaker }; 50fbdefd64SBryan Schumaker 51fbdefd64SBryan Schumaker static const struct super_operations nfs4_sops = { 52fbdefd64SBryan Schumaker .alloc_inode = nfs_alloc_inode, 53fbdefd64SBryan Schumaker .destroy_inode = nfs_destroy_inode, 54fbdefd64SBryan Schumaker .write_inode = nfs4_write_inode, 55eed99357STrond Myklebust .drop_inode = nfs_drop_inode, 56fbdefd64SBryan Schumaker .put_super = nfs_put_super, 57fbdefd64SBryan Schumaker .statfs = nfs_statfs, 58fbdefd64SBryan Schumaker .evict_inode = nfs4_evict_inode, 59fbdefd64SBryan Schumaker .umount_begin = nfs_umount_begin, 60fbdefd64SBryan Schumaker .show_options = nfs_show_options, 61fbdefd64SBryan Schumaker .show_devname = nfs_show_devname, 62fbdefd64SBryan Schumaker .show_path = nfs_show_path, 63fbdefd64SBryan Schumaker .show_stats = nfs_show_stats, 64fbdefd64SBryan Schumaker .remount_fs = nfs_remount, 65fbdefd64SBryan Schumaker }; 66fbdefd64SBryan Schumaker 67ab7017a3SBryan Schumaker struct nfs_subversion nfs_v4 = { 68ab7017a3SBryan Schumaker .owner = THIS_MODULE, 69ab7017a3SBryan Schumaker .nfs_fs = &nfs4_fs_type, 70ab7017a3SBryan Schumaker .rpc_vers = &nfs_version4, 71ab7017a3SBryan Schumaker .rpc_ops = &nfs_v4_clientops, 726a74490dSBryan Schumaker .sops = &nfs4_sops, 736a74490dSBryan Schumaker .xattr = nfs4_xattr_handlers, 74ab7017a3SBryan Schumaker }; 75ab7017a3SBryan Schumaker 7619d87ca3SBryan Schumaker static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc) 7719d87ca3SBryan Schumaker { 7819d87ca3SBryan Schumaker int ret = nfs_write_inode(inode, wbc); 7919d87ca3SBryan Schumaker 8071244d9bSTrond Myklebust if (ret == 0) 8171244d9bSTrond Myklebust ret = pnfs_layoutcommit_inode(inode, 8271244d9bSTrond Myklebust wbc->sync_mode == WB_SYNC_ALL); 8319d87ca3SBryan Schumaker return ret; 8419d87ca3SBryan Schumaker } 8519d87ca3SBryan Schumaker 8619d87ca3SBryan Schumaker /* 8719d87ca3SBryan Schumaker * Clean out any remaining NFSv4 state that might be left over due 8819d87ca3SBryan Schumaker * to open() calls that passed nfs_atomic_lookup, but failed to call 8919d87ca3SBryan Schumaker * nfs_open(). 9019d87ca3SBryan Schumaker */ 9119d87ca3SBryan Schumaker static void nfs4_evict_inode(struct inode *inode) 9219d87ca3SBryan Schumaker { 9391b0abe3SJohannes Weiner truncate_inode_pages_final(&inode->i_data); 9419d87ca3SBryan Schumaker clear_inode(inode); 9519d87ca3SBryan Schumaker pnfs_return_layout(inode); 9619d87ca3SBryan Schumaker pnfs_destroy_layout(NFS_I(inode)); 9719d87ca3SBryan Schumaker /* If we are holding a delegation, return it! */ 9819d87ca3SBryan Schumaker nfs_inode_return_delegation_noreclaim(inode); 9919d87ca3SBryan Schumaker /* First call standard NFS clear_inode() code */ 10019d87ca3SBryan Schumaker nfs_clear_inode(inode); 10119d87ca3SBryan Schumaker } 10219d87ca3SBryan Schumaker 103fbdefd64SBryan Schumaker /* 104fbdefd64SBryan Schumaker * Get the superblock for the NFS4 root partition 105fbdefd64SBryan Schumaker */ 106fbdefd64SBryan Schumaker static struct dentry * 107fbdefd64SBryan Schumaker nfs4_remote_mount(struct file_system_type *fs_type, int flags, 108fbdefd64SBryan Schumaker const char *dev_name, void *info) 109fbdefd64SBryan Schumaker { 110fbdefd64SBryan Schumaker struct nfs_mount_info *mount_info = info; 111fbdefd64SBryan Schumaker struct nfs_server *server; 112fbdefd64SBryan Schumaker struct dentry *mntroot = ERR_PTR(-ENOMEM); 113fbdefd64SBryan Schumaker 114fbdefd64SBryan Schumaker mount_info->set_security = nfs_set_sb_security; 115fbdefd64SBryan Schumaker 116fbdefd64SBryan Schumaker /* Get a volume representation */ 1171179acc6SBryan Schumaker server = nfs4_create_server(mount_info, &nfs_v4); 118fbdefd64SBryan Schumaker if (IS_ERR(server)) { 119fbdefd64SBryan Schumaker mntroot = ERR_CAST(server); 120fbdefd64SBryan Schumaker goto out; 121fbdefd64SBryan Schumaker } 122fbdefd64SBryan Schumaker 123ab7017a3SBryan Schumaker mntroot = nfs_fs_mount_common(server, flags, dev_name, mount_info, &nfs_v4); 124fbdefd64SBryan Schumaker 125fbdefd64SBryan Schumaker out: 126fbdefd64SBryan Schumaker return mntroot; 127fbdefd64SBryan Schumaker } 128fbdefd64SBryan Schumaker 129fbdefd64SBryan Schumaker static struct vfsmount *nfs_do_root_mount(struct file_system_type *fs_type, 130fbdefd64SBryan Schumaker int flags, void *data, const char *hostname) 131fbdefd64SBryan Schumaker { 132fbdefd64SBryan Schumaker struct vfsmount *root_mnt; 133fbdefd64SBryan Schumaker char *root_devname; 134fbdefd64SBryan Schumaker size_t len; 135fbdefd64SBryan Schumaker 136fbdefd64SBryan Schumaker len = strlen(hostname) + 5; 137fbdefd64SBryan Schumaker root_devname = kmalloc(len, GFP_KERNEL); 138fbdefd64SBryan Schumaker if (root_devname == NULL) 139fbdefd64SBryan Schumaker return ERR_PTR(-ENOMEM); 140fbdefd64SBryan Schumaker /* Does hostname needs to be enclosed in brackets? */ 141fbdefd64SBryan Schumaker if (strchr(hostname, ':')) 142fbdefd64SBryan Schumaker snprintf(root_devname, len, "[%s]:/", hostname); 143fbdefd64SBryan Schumaker else 144fbdefd64SBryan Schumaker snprintf(root_devname, len, "%s:/", hostname); 145fbdefd64SBryan Schumaker root_mnt = vfs_kern_mount(fs_type, flags, root_devname, data); 146fbdefd64SBryan Schumaker kfree(root_devname); 147fbdefd64SBryan Schumaker return root_mnt; 148fbdefd64SBryan Schumaker } 149fbdefd64SBryan Schumaker 150fbdefd64SBryan Schumaker struct nfs_referral_count { 151fbdefd64SBryan Schumaker struct list_head list; 152fbdefd64SBryan Schumaker const struct task_struct *task; 153fbdefd64SBryan Schumaker unsigned int referral_count; 154fbdefd64SBryan Schumaker }; 155fbdefd64SBryan Schumaker 156fbdefd64SBryan Schumaker static LIST_HEAD(nfs_referral_count_list); 157fbdefd64SBryan Schumaker static DEFINE_SPINLOCK(nfs_referral_count_list_lock); 158fbdefd64SBryan Schumaker 159fbdefd64SBryan Schumaker static struct nfs_referral_count *nfs_find_referral_count(void) 160fbdefd64SBryan Schumaker { 161fbdefd64SBryan Schumaker struct nfs_referral_count *p; 162fbdefd64SBryan Schumaker 163fbdefd64SBryan Schumaker list_for_each_entry(p, &nfs_referral_count_list, list) { 164fbdefd64SBryan Schumaker if (p->task == current) 165fbdefd64SBryan Schumaker return p; 166fbdefd64SBryan Schumaker } 167fbdefd64SBryan Schumaker return NULL; 168fbdefd64SBryan Schumaker } 169fbdefd64SBryan Schumaker 170fbdefd64SBryan Schumaker #define NFS_MAX_NESTED_REFERRALS 2 171fbdefd64SBryan Schumaker 172fbdefd64SBryan Schumaker static int nfs_referral_loop_protect(void) 173fbdefd64SBryan Schumaker { 174fbdefd64SBryan Schumaker struct nfs_referral_count *p, *new; 175fbdefd64SBryan Schumaker int ret = -ENOMEM; 176fbdefd64SBryan Schumaker 177fbdefd64SBryan Schumaker new = kmalloc(sizeof(*new), GFP_KERNEL); 178fbdefd64SBryan Schumaker if (!new) 179fbdefd64SBryan Schumaker goto out; 180fbdefd64SBryan Schumaker new->task = current; 181fbdefd64SBryan Schumaker new->referral_count = 1; 182fbdefd64SBryan Schumaker 183fbdefd64SBryan Schumaker ret = 0; 184fbdefd64SBryan Schumaker spin_lock(&nfs_referral_count_list_lock); 185fbdefd64SBryan Schumaker p = nfs_find_referral_count(); 186fbdefd64SBryan Schumaker if (p != NULL) { 187fbdefd64SBryan Schumaker if (p->referral_count >= NFS_MAX_NESTED_REFERRALS) 188fbdefd64SBryan Schumaker ret = -ELOOP; 189fbdefd64SBryan Schumaker else 190fbdefd64SBryan Schumaker p->referral_count++; 191fbdefd64SBryan Schumaker } else { 192fbdefd64SBryan Schumaker list_add(&new->list, &nfs_referral_count_list); 193fbdefd64SBryan Schumaker new = NULL; 194fbdefd64SBryan Schumaker } 195fbdefd64SBryan Schumaker spin_unlock(&nfs_referral_count_list_lock); 196fbdefd64SBryan Schumaker kfree(new); 197fbdefd64SBryan Schumaker out: 198fbdefd64SBryan Schumaker return ret; 199fbdefd64SBryan Schumaker } 200fbdefd64SBryan Schumaker 201fbdefd64SBryan Schumaker static void nfs_referral_loop_unprotect(void) 202fbdefd64SBryan Schumaker { 203fbdefd64SBryan Schumaker struct nfs_referral_count *p; 204fbdefd64SBryan Schumaker 205fbdefd64SBryan Schumaker spin_lock(&nfs_referral_count_list_lock); 206fbdefd64SBryan Schumaker p = nfs_find_referral_count(); 207fbdefd64SBryan Schumaker p->referral_count--; 208fbdefd64SBryan Schumaker if (p->referral_count == 0) 209fbdefd64SBryan Schumaker list_del(&p->list); 210fbdefd64SBryan Schumaker else 211fbdefd64SBryan Schumaker p = NULL; 212fbdefd64SBryan Schumaker spin_unlock(&nfs_referral_count_list_lock); 213fbdefd64SBryan Schumaker kfree(p); 214fbdefd64SBryan Schumaker } 215fbdefd64SBryan Schumaker 216fbdefd64SBryan Schumaker static struct dentry *nfs_follow_remote_path(struct vfsmount *root_mnt, 217fbdefd64SBryan Schumaker const char *export_path) 218fbdefd64SBryan Schumaker { 219fbdefd64SBryan Schumaker struct dentry *dentry; 220fbdefd64SBryan Schumaker int err; 221fbdefd64SBryan Schumaker 222fbdefd64SBryan Schumaker if (IS_ERR(root_mnt)) 223fbdefd64SBryan Schumaker return ERR_CAST(root_mnt); 224fbdefd64SBryan Schumaker 225fbdefd64SBryan Schumaker err = nfs_referral_loop_protect(); 226fbdefd64SBryan Schumaker if (err) { 227fbdefd64SBryan Schumaker mntput(root_mnt); 228fbdefd64SBryan Schumaker return ERR_PTR(err); 229fbdefd64SBryan Schumaker } 230fbdefd64SBryan Schumaker 231fbdefd64SBryan Schumaker dentry = mount_subtree(root_mnt, export_path); 232fbdefd64SBryan Schumaker nfs_referral_loop_unprotect(); 233fbdefd64SBryan Schumaker 234fbdefd64SBryan Schumaker return dentry; 235fbdefd64SBryan Schumaker } 236fbdefd64SBryan Schumaker 237fbdefd64SBryan Schumaker struct dentry *nfs4_try_mount(int flags, const char *dev_name, 238ff9099f2SBryan Schumaker struct nfs_mount_info *mount_info, 239ff9099f2SBryan Schumaker struct nfs_subversion *nfs_mod) 240fbdefd64SBryan Schumaker { 241fbdefd64SBryan Schumaker char *export_path; 242fbdefd64SBryan Schumaker struct vfsmount *root_mnt; 243fbdefd64SBryan Schumaker struct dentry *res; 244fbdefd64SBryan Schumaker struct nfs_parsed_mount_data *data = mount_info->parsed; 245fbdefd64SBryan Schumaker 246fbdefd64SBryan Schumaker dfprintk(MOUNT, "--> nfs4_try_mount()\n"); 247fbdefd64SBryan Schumaker 248fbdefd64SBryan Schumaker export_path = data->nfs_server.export_path; 249fbdefd64SBryan Schumaker data->nfs_server.export_path = "/"; 250fbdefd64SBryan Schumaker root_mnt = nfs_do_root_mount(&nfs4_remote_fs_type, flags, mount_info, 251fbdefd64SBryan Schumaker data->nfs_server.hostname); 252fbdefd64SBryan Schumaker data->nfs_server.export_path = export_path; 253fbdefd64SBryan Schumaker 254fbdefd64SBryan Schumaker res = nfs_follow_remote_path(root_mnt, export_path); 255fbdefd64SBryan Schumaker 2566706246bSGeyslan G. Bem dfprintk(MOUNT, "<-- nfs4_try_mount() = %d%s\n", 2576706246bSGeyslan G. Bem PTR_ERR_OR_ZERO(res), 258fbdefd64SBryan Schumaker IS_ERR(res) ? " [error]" : ""); 259fbdefd64SBryan Schumaker return res; 260fbdefd64SBryan Schumaker } 261fbdefd64SBryan Schumaker 262fbdefd64SBryan Schumaker static struct dentry * 263fbdefd64SBryan Schumaker nfs4_remote_referral_mount(struct file_system_type *fs_type, int flags, 264fbdefd64SBryan Schumaker const char *dev_name, void *raw_data) 265fbdefd64SBryan Schumaker { 266fbdefd64SBryan Schumaker struct nfs_mount_info mount_info = { 2676a74490dSBryan Schumaker .fill_super = nfs_fill_super, 268fbdefd64SBryan Schumaker .set_security = nfs_clone_sb_security, 269fbdefd64SBryan Schumaker .cloned = raw_data, 270fbdefd64SBryan Schumaker }; 271fbdefd64SBryan Schumaker struct nfs_server *server; 272fbdefd64SBryan Schumaker struct dentry *mntroot = ERR_PTR(-ENOMEM); 273fbdefd64SBryan Schumaker 274fbdefd64SBryan Schumaker dprintk("--> nfs4_referral_get_sb()\n"); 275fbdefd64SBryan Schumaker 276fbdefd64SBryan Schumaker mount_info.mntfh = nfs_alloc_fhandle(); 277fbdefd64SBryan Schumaker if (mount_info.cloned == NULL || mount_info.mntfh == NULL) 278fbdefd64SBryan Schumaker goto out; 279fbdefd64SBryan Schumaker 280fbdefd64SBryan Schumaker /* create a new volume representation */ 281fbdefd64SBryan Schumaker server = nfs4_create_referral_server(mount_info.cloned, mount_info.mntfh); 282fbdefd64SBryan Schumaker if (IS_ERR(server)) { 283fbdefd64SBryan Schumaker mntroot = ERR_CAST(server); 284fbdefd64SBryan Schumaker goto out; 285fbdefd64SBryan Schumaker } 286fbdefd64SBryan Schumaker 287ab7017a3SBryan Schumaker mntroot = nfs_fs_mount_common(server, flags, dev_name, &mount_info, &nfs_v4); 288fbdefd64SBryan Schumaker out: 289fbdefd64SBryan Schumaker nfs_free_fhandle(mount_info.mntfh); 290fbdefd64SBryan Schumaker return mntroot; 291fbdefd64SBryan Schumaker } 292fbdefd64SBryan Schumaker 293fbdefd64SBryan Schumaker /* 294fbdefd64SBryan Schumaker * Create an NFS4 server record on referral traversal 295fbdefd64SBryan Schumaker */ 296fbdefd64SBryan Schumaker static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type, 297fbdefd64SBryan Schumaker int flags, const char *dev_name, void *raw_data) 298fbdefd64SBryan Schumaker { 299fbdefd64SBryan Schumaker struct nfs_clone_mount *data = raw_data; 300fbdefd64SBryan Schumaker char *export_path; 301fbdefd64SBryan Schumaker struct vfsmount *root_mnt; 302fbdefd64SBryan Schumaker struct dentry *res; 303fbdefd64SBryan Schumaker 304fbdefd64SBryan Schumaker dprintk("--> nfs4_referral_mount()\n"); 305fbdefd64SBryan Schumaker 306fbdefd64SBryan Schumaker export_path = data->mnt_path; 307fbdefd64SBryan Schumaker data->mnt_path = "/"; 308fbdefd64SBryan Schumaker 309fbdefd64SBryan Schumaker root_mnt = nfs_do_root_mount(&nfs4_remote_referral_fs_type, 310fbdefd64SBryan Schumaker flags, data, data->hostname); 311fbdefd64SBryan Schumaker data->mnt_path = export_path; 312fbdefd64SBryan Schumaker 313fbdefd64SBryan Schumaker res = nfs_follow_remote_path(root_mnt, export_path); 3146706246bSGeyslan G. Bem dprintk("<-- nfs4_referral_mount() = %d%s\n", 3156706246bSGeyslan G. Bem PTR_ERR_OR_ZERO(res), 316fbdefd64SBryan Schumaker IS_ERR(res) ? " [error]" : ""); 317fbdefd64SBryan Schumaker return res; 318fbdefd64SBryan Schumaker } 319fbdefd64SBryan Schumaker 320fbdefd64SBryan Schumaker 32189d77c8fSBryan Schumaker static int __init init_nfs_v4(void) 322129d1977SBryan Schumaker { 323129d1977SBryan Schumaker int err; 324129d1977SBryan Schumaker 325c8d74d9bSTrond Myklebust err = nfs_dns_resolver_init(); 326129d1977SBryan Schumaker if (err) 327129d1977SBryan Schumaker goto out; 328129d1977SBryan Schumaker 329c8d74d9bSTrond Myklebust err = nfs_idmap_init(); 330466bfe7fSBryan Schumaker if (err) 331466bfe7fSBryan Schumaker goto out1; 332466bfe7fSBryan Schumaker 333c8d74d9bSTrond Myklebust err = nfs4_register_sysctl(); 334c8d74d9bSTrond Myklebust if (err) 335c8d74d9bSTrond Myklebust goto out2; 336c8d74d9bSTrond Myklebust 337ab7017a3SBryan Schumaker register_nfs_version(&nfs_v4); 338129d1977SBryan Schumaker return 0; 339c8d74d9bSTrond Myklebust out2: 340466bfe7fSBryan Schumaker nfs_idmap_quit(); 341c8d74d9bSTrond Myklebust out1: 342c8d74d9bSTrond Myklebust nfs_dns_resolver_destroy(); 343129d1977SBryan Schumaker out: 344129d1977SBryan Schumaker return err; 345129d1977SBryan Schumaker } 346129d1977SBryan Schumaker 34789d77c8fSBryan Schumaker static void __exit exit_nfs_v4(void) 348129d1977SBryan Schumaker { 349*5f01d953SPeng Tao /* Not called in the _init(), conditionally loaded */ 350*5f01d953SPeng Tao nfs4_pnfs_v3_ds_connect_unload(); 351*5f01d953SPeng Tao 352ab7017a3SBryan Schumaker unregister_nfs_version(&nfs_v4); 353466bfe7fSBryan Schumaker nfs4_unregister_sysctl(); 354129d1977SBryan Schumaker nfs_idmap_quit(); 355c8d74d9bSTrond Myklebust nfs_dns_resolver_destroy(); 356129d1977SBryan Schumaker } 35789d77c8fSBryan Schumaker 35889d77c8fSBryan Schumaker MODULE_LICENSE("GPL"); 35989d77c8fSBryan Schumaker 36089d77c8fSBryan Schumaker module_init(init_nfs_v4); 36189d77c8fSBryan Schumaker module_exit(exit_nfs_v4); 362