109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2129d1977SBryan Schumaker /* 3129d1977SBryan Schumaker * Copyright (c) 2012 Bryan Schumaker <bjschuma@netapp.com> 4129d1977SBryan Schumaker */ 5129d1977SBryan Schumaker #include <linux/init.h> 6fbdefd64SBryan Schumaker #include <linux/module.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" 1240c64c26SAnna Schumaker #include "nfs4idmap.h" 13c8d74d9bSTrond Myklebust #include "dns_resolve.h" 1419d87ca3SBryan Schumaker #include "pnfs.h" 15ab7017a3SBryan Schumaker #include "nfs.h" 16129d1977SBryan Schumaker 17fbdefd64SBryan Schumaker #define NFSDBG_FACILITY NFSDBG_VFS 18fbdefd64SBryan Schumaker 1919d87ca3SBryan Schumaker static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc); 2019d87ca3SBryan Schumaker static void nfs4_evict_inode(struct inode *inode); 21fbdefd64SBryan Schumaker static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type, 22fbdefd64SBryan Schumaker int flags, const char *dev_name, void *raw_data); 23fbdefd64SBryan Schumaker 24fbdefd64SBryan Schumaker struct file_system_type nfs4_referral_fs_type = { 25fbdefd64SBryan Schumaker .owner = THIS_MODULE, 26fbdefd64SBryan Schumaker .name = "nfs4", 27fbdefd64SBryan Schumaker .mount = nfs4_referral_mount, 28fbdefd64SBryan Schumaker .kill_sb = nfs_kill_super, 29ecf3d1f1SJeff Layton .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA, 30fbdefd64SBryan Schumaker }; 31fbdefd64SBryan Schumaker 32fbdefd64SBryan Schumaker static const struct super_operations nfs4_sops = { 33fbdefd64SBryan Schumaker .alloc_inode = nfs_alloc_inode, 34ca1a199eSAl Viro .free_inode = nfs_free_inode, 35fbdefd64SBryan Schumaker .write_inode = nfs4_write_inode, 36eed99357STrond Myklebust .drop_inode = nfs_drop_inode, 37fbdefd64SBryan Schumaker .statfs = nfs_statfs, 38fbdefd64SBryan Schumaker .evict_inode = nfs4_evict_inode, 39fbdefd64SBryan Schumaker .umount_begin = nfs_umount_begin, 40fbdefd64SBryan Schumaker .show_options = nfs_show_options, 41fbdefd64SBryan Schumaker .show_devname = nfs_show_devname, 42fbdefd64SBryan Schumaker .show_path = nfs_show_path, 43fbdefd64SBryan Schumaker .show_stats = nfs_show_stats, 44fbdefd64SBryan Schumaker .remount_fs = nfs_remount, 45fbdefd64SBryan Schumaker }; 46fbdefd64SBryan Schumaker 47ab7017a3SBryan Schumaker struct nfs_subversion nfs_v4 = { 48ab7017a3SBryan Schumaker .owner = THIS_MODULE, 49ab7017a3SBryan Schumaker .nfs_fs = &nfs4_fs_type, 50ab7017a3SBryan Schumaker .rpc_vers = &nfs_version4, 51ab7017a3SBryan Schumaker .rpc_ops = &nfs_v4_clientops, 526a74490dSBryan Schumaker .sops = &nfs4_sops, 536a74490dSBryan Schumaker .xattr = nfs4_xattr_handlers, 54ab7017a3SBryan Schumaker }; 55ab7017a3SBryan Schumaker 5619d87ca3SBryan Schumaker static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc) 5719d87ca3SBryan Schumaker { 5819d87ca3SBryan Schumaker int ret = nfs_write_inode(inode, wbc); 5919d87ca3SBryan Schumaker 6071244d9bSTrond Myklebust if (ret == 0) 6171244d9bSTrond Myklebust ret = pnfs_layoutcommit_inode(inode, 6271244d9bSTrond Myklebust wbc->sync_mode == WB_SYNC_ALL); 6319d87ca3SBryan Schumaker return ret; 6419d87ca3SBryan Schumaker } 6519d87ca3SBryan Schumaker 6619d87ca3SBryan Schumaker /* 6719d87ca3SBryan Schumaker * Clean out any remaining NFSv4 state that might be left over due 6819d87ca3SBryan Schumaker * to open() calls that passed nfs_atomic_lookup, but failed to call 6919d87ca3SBryan Schumaker * nfs_open(). 7019d87ca3SBryan Schumaker */ 7119d87ca3SBryan Schumaker static void nfs4_evict_inode(struct inode *inode) 7219d87ca3SBryan Schumaker { 7391b0abe3SJohannes Weiner truncate_inode_pages_final(&inode->i_data); 7419d87ca3SBryan Schumaker clear_inode(inode); 75b47e0e47STrond Myklebust /* If we are holding a delegation, return and free it */ 76b47e0e47STrond Myklebust nfs_inode_evict_delegation(inode); 77415320fcSTrond Myklebust /* Note that above delegreturn would trigger pnfs return-on-close */ 78415320fcSTrond Myklebust pnfs_return_layout(inode); 79415320fcSTrond Myklebust pnfs_destroy_layout(NFS_I(inode)); 8019d87ca3SBryan Schumaker /* First call standard NFS clear_inode() code */ 8119d87ca3SBryan Schumaker nfs_clear_inode(inode); 8219d87ca3SBryan Schumaker } 8319d87ca3SBryan Schumaker 84fbdefd64SBryan Schumaker struct nfs_referral_count { 85fbdefd64SBryan Schumaker struct list_head list; 86fbdefd64SBryan Schumaker const struct task_struct *task; 87fbdefd64SBryan Schumaker unsigned int referral_count; 88fbdefd64SBryan Schumaker }; 89fbdefd64SBryan Schumaker 90fbdefd64SBryan Schumaker static LIST_HEAD(nfs_referral_count_list); 91fbdefd64SBryan Schumaker static DEFINE_SPINLOCK(nfs_referral_count_list_lock); 92fbdefd64SBryan Schumaker 93fbdefd64SBryan Schumaker static struct nfs_referral_count *nfs_find_referral_count(void) 94fbdefd64SBryan Schumaker { 95fbdefd64SBryan Schumaker struct nfs_referral_count *p; 96fbdefd64SBryan Schumaker 97fbdefd64SBryan Schumaker list_for_each_entry(p, &nfs_referral_count_list, list) { 98fbdefd64SBryan Schumaker if (p->task == current) 99fbdefd64SBryan Schumaker return p; 100fbdefd64SBryan Schumaker } 101fbdefd64SBryan Schumaker return NULL; 102fbdefd64SBryan Schumaker } 103fbdefd64SBryan Schumaker 104fbdefd64SBryan Schumaker #define NFS_MAX_NESTED_REFERRALS 2 105fbdefd64SBryan Schumaker 106fbdefd64SBryan Schumaker static int nfs_referral_loop_protect(void) 107fbdefd64SBryan Schumaker { 108fbdefd64SBryan Schumaker struct nfs_referral_count *p, *new; 109fbdefd64SBryan Schumaker int ret = -ENOMEM; 110fbdefd64SBryan Schumaker 111fbdefd64SBryan Schumaker new = kmalloc(sizeof(*new), GFP_KERNEL); 112fbdefd64SBryan Schumaker if (!new) 113fbdefd64SBryan Schumaker goto out; 114fbdefd64SBryan Schumaker new->task = current; 115fbdefd64SBryan Schumaker new->referral_count = 1; 116fbdefd64SBryan Schumaker 117fbdefd64SBryan Schumaker ret = 0; 118fbdefd64SBryan Schumaker spin_lock(&nfs_referral_count_list_lock); 119fbdefd64SBryan Schumaker p = nfs_find_referral_count(); 120fbdefd64SBryan Schumaker if (p != NULL) { 121fbdefd64SBryan Schumaker if (p->referral_count >= NFS_MAX_NESTED_REFERRALS) 122fbdefd64SBryan Schumaker ret = -ELOOP; 123fbdefd64SBryan Schumaker else 124fbdefd64SBryan Schumaker p->referral_count++; 125fbdefd64SBryan Schumaker } else { 126fbdefd64SBryan Schumaker list_add(&new->list, &nfs_referral_count_list); 127fbdefd64SBryan Schumaker new = NULL; 128fbdefd64SBryan Schumaker } 129fbdefd64SBryan Schumaker spin_unlock(&nfs_referral_count_list_lock); 130fbdefd64SBryan Schumaker kfree(new); 131fbdefd64SBryan Schumaker out: 132fbdefd64SBryan Schumaker return ret; 133fbdefd64SBryan Schumaker } 134fbdefd64SBryan Schumaker 135fbdefd64SBryan Schumaker static void nfs_referral_loop_unprotect(void) 136fbdefd64SBryan Schumaker { 137fbdefd64SBryan Schumaker struct nfs_referral_count *p; 138fbdefd64SBryan Schumaker 139fbdefd64SBryan Schumaker spin_lock(&nfs_referral_count_list_lock); 140fbdefd64SBryan Schumaker p = nfs_find_referral_count(); 141fbdefd64SBryan Schumaker p->referral_count--; 142fbdefd64SBryan Schumaker if (p->referral_count == 0) 143fbdefd64SBryan Schumaker list_del(&p->list); 144fbdefd64SBryan Schumaker else 145fbdefd64SBryan Schumaker p = NULL; 146fbdefd64SBryan Schumaker spin_unlock(&nfs_referral_count_list_lock); 147fbdefd64SBryan Schumaker kfree(p); 148fbdefd64SBryan Schumaker } 149fbdefd64SBryan Schumaker 1504e357761SAl Viro static struct dentry *do_nfs4_mount(struct nfs_server *server, int flags, 1514e357761SAl Viro struct nfs_mount_info *info, 1524e357761SAl Viro const char *hostname, 153fbdefd64SBryan Schumaker const char *export_path) 154fbdefd64SBryan Schumaker { 1554e357761SAl Viro struct vfsmount *root_mnt; 156fbdefd64SBryan Schumaker struct dentry *dentry; 1574e357761SAl Viro char *root_devname; 158fbdefd64SBryan Schumaker int err; 1594e357761SAl Viro size_t len; 1604e357761SAl Viro 1614e357761SAl Viro if (IS_ERR(server)) 1624e357761SAl Viro return ERR_CAST(server); 1634e357761SAl Viro 1644e357761SAl Viro len = strlen(hostname) + 5; 1654e357761SAl Viro root_devname = kmalloc(len, GFP_KERNEL); 1664e357761SAl Viro if (root_devname == NULL) { 1674e357761SAl Viro nfs_free_server(server); 1684e357761SAl Viro return ERR_PTR(-ENOMEM); 1694e357761SAl Viro } 1704e357761SAl Viro 1714e357761SAl Viro /* Does hostname needs to be enclosed in brackets? */ 1724e357761SAl Viro if (strchr(hostname, ':')) 1734e357761SAl Viro snprintf(root_devname, len, "[%s]:/", hostname); 1744e357761SAl Viro else 1754e357761SAl Viro snprintf(root_devname, len, "%s:/", hostname); 1764e357761SAl Viro info->server = server; 17782eaed2bSAl Viro root_mnt = vfs_kern_mount(&nfs_prepared_fs_type, flags, root_devname, info); 1784e357761SAl Viro if (info->server) 1794e357761SAl Viro nfs_free_server(info->server); 1804e357761SAl Viro info->server = NULL; 1814e357761SAl Viro kfree(root_devname); 182fbdefd64SBryan Schumaker 183fbdefd64SBryan Schumaker if (IS_ERR(root_mnt)) 184fbdefd64SBryan Schumaker return ERR_CAST(root_mnt); 185fbdefd64SBryan Schumaker 186fbdefd64SBryan Schumaker err = nfs_referral_loop_protect(); 187fbdefd64SBryan Schumaker if (err) { 188fbdefd64SBryan Schumaker mntput(root_mnt); 189fbdefd64SBryan Schumaker return ERR_PTR(err); 190fbdefd64SBryan Schumaker } 191fbdefd64SBryan Schumaker 192fbdefd64SBryan Schumaker dentry = mount_subtree(root_mnt, export_path); 193fbdefd64SBryan Schumaker nfs_referral_loop_unprotect(); 194fbdefd64SBryan Schumaker 195fbdefd64SBryan Schumaker return dentry; 196fbdefd64SBryan Schumaker } 197fbdefd64SBryan Schumaker 198fbdefd64SBryan Schumaker struct dentry *nfs4_try_mount(int flags, const char *dev_name, 199a55d3297SAl Viro struct nfs_mount_info *mount_info) 200fbdefd64SBryan Schumaker { 201*5eb005caSDavid Howells struct nfs_fs_context *ctx = mount_info->ctx; 2024e357761SAl Viro struct dentry *res; 203fbdefd64SBryan Schumaker 204fbdefd64SBryan Schumaker dfprintk(MOUNT, "--> nfs4_try_mount()\n"); 205fbdefd64SBryan Schumaker 2060c38f213SAl Viro res = do_nfs4_mount(nfs4_create_server(mount_info), 2077643c12eSAl Viro flags, mount_info, 208*5eb005caSDavid Howells ctx->nfs_server.hostname, 209*5eb005caSDavid Howells ctx->nfs_server.export_path); 210fbdefd64SBryan Schumaker 2116706246bSGeyslan G. Bem dfprintk(MOUNT, "<-- nfs4_try_mount() = %d%s\n", 2126706246bSGeyslan G. Bem PTR_ERR_OR_ZERO(res), 213fbdefd64SBryan Schumaker IS_ERR(res) ? " [error]" : ""); 214fbdefd64SBryan Schumaker return res; 215fbdefd64SBryan Schumaker } 216fbdefd64SBryan Schumaker 217fbdefd64SBryan Schumaker /* 218fbdefd64SBryan Schumaker * Create an NFS4 server record on referral traversal 219fbdefd64SBryan Schumaker */ 220fbdefd64SBryan Schumaker static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type, 221fbdefd64SBryan Schumaker int flags, const char *dev_name, void *raw_data) 222fbdefd64SBryan Schumaker { 223fbdefd64SBryan Schumaker struct nfs_clone_mount *data = raw_data; 2247643c12eSAl Viro struct nfs_mount_info mount_info = { 2257643c12eSAl Viro .cloned = data, 2266a3f7a39SAl Viro .nfs_mod = &nfs_v4, 2277643c12eSAl Viro }; 228fbdefd64SBryan Schumaker struct dentry *res; 229fbdefd64SBryan Schumaker 230fbdefd64SBryan Schumaker dprintk("--> nfs4_referral_mount()\n"); 231fbdefd64SBryan Schumaker 2327643c12eSAl Viro mount_info.mntfh = nfs_alloc_fhandle(); 2337643c12eSAl Viro if (!mount_info.mntfh) 2347643c12eSAl Viro return ERR_PTR(-ENOMEM); 2357643c12eSAl Viro 2364e357761SAl Viro res = do_nfs4_mount(nfs4_create_referral_server(mount_info.cloned, 2377643c12eSAl Viro mount_info.mntfh), 2384e357761SAl Viro flags, &mount_info, data->hostname, data->mnt_path); 239fbdefd64SBryan Schumaker 2406706246bSGeyslan G. Bem dprintk("<-- nfs4_referral_mount() = %d%s\n", 2416706246bSGeyslan G. Bem PTR_ERR_OR_ZERO(res), 242fbdefd64SBryan Schumaker IS_ERR(res) ? " [error]" : ""); 2437643c12eSAl Viro 2447643c12eSAl Viro nfs_free_fhandle(mount_info.mntfh); 245fbdefd64SBryan Schumaker return res; 246fbdefd64SBryan Schumaker } 247fbdefd64SBryan Schumaker 248fbdefd64SBryan Schumaker 24989d77c8fSBryan Schumaker static int __init init_nfs_v4(void) 250129d1977SBryan Schumaker { 251129d1977SBryan Schumaker int err; 252129d1977SBryan Schumaker 253c8d74d9bSTrond Myklebust err = nfs_dns_resolver_init(); 254129d1977SBryan Schumaker if (err) 255129d1977SBryan Schumaker goto out; 256129d1977SBryan Schumaker 257c8d74d9bSTrond Myklebust err = nfs_idmap_init(); 258466bfe7fSBryan Schumaker if (err) 259466bfe7fSBryan Schumaker goto out1; 260466bfe7fSBryan Schumaker 261c8d74d9bSTrond Myklebust err = nfs4_register_sysctl(); 262c8d74d9bSTrond Myklebust if (err) 263c8d74d9bSTrond Myklebust goto out2; 264c8d74d9bSTrond Myklebust 265ab7017a3SBryan Schumaker register_nfs_version(&nfs_v4); 266129d1977SBryan Schumaker return 0; 267c8d74d9bSTrond Myklebust out2: 268466bfe7fSBryan Schumaker nfs_idmap_quit(); 269c8d74d9bSTrond Myklebust out1: 270c8d74d9bSTrond Myklebust nfs_dns_resolver_destroy(); 271129d1977SBryan Schumaker out: 272129d1977SBryan Schumaker return err; 273129d1977SBryan Schumaker } 274129d1977SBryan Schumaker 27589d77c8fSBryan Schumaker static void __exit exit_nfs_v4(void) 276129d1977SBryan Schumaker { 2775f01d953SPeng Tao /* Not called in the _init(), conditionally loaded */ 2785f01d953SPeng Tao nfs4_pnfs_v3_ds_connect_unload(); 2795f01d953SPeng Tao 280ab7017a3SBryan Schumaker unregister_nfs_version(&nfs_v4); 281466bfe7fSBryan Schumaker nfs4_unregister_sysctl(); 282129d1977SBryan Schumaker nfs_idmap_quit(); 283c8d74d9bSTrond Myklebust nfs_dns_resolver_destroy(); 284129d1977SBryan Schumaker } 28589d77c8fSBryan Schumaker 28689d77c8fSBryan Schumaker MODULE_LICENSE("GPL"); 28789d77c8fSBryan Schumaker 28889d77c8fSBryan Schumaker module_init(init_nfs_v4); 28989d77c8fSBryan Schumaker module_exit(exit_nfs_v4); 290