1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2012 Bryan Schumaker <bjschuma@netapp.com> 4 */ 5 #include <linux/init.h> 6 #include <linux/module.h> 7 #include <linux/mount.h> 8 #include <linux/nfs4_mount.h> 9 #include <linux/nfs_fs.h> 10 #include <linux/nfs_ssc.h> 11 #include "delegation.h" 12 #include "internal.h" 13 #include "nfs4_fs.h" 14 #include "nfs4idmap.h" 15 #include "dns_resolve.h" 16 #include "pnfs.h" 17 #include "nfs.h" 18 19 #define NFSDBG_FACILITY NFSDBG_VFS 20 21 static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc); 22 static void nfs4_evict_inode(struct inode *inode); 23 24 static const struct super_operations nfs4_sops = { 25 .alloc_inode = nfs_alloc_inode, 26 .free_inode = nfs_free_inode, 27 .write_inode = nfs4_write_inode, 28 .drop_inode = nfs_drop_inode, 29 .statfs = nfs_statfs, 30 .evict_inode = nfs4_evict_inode, 31 .umount_begin = nfs_umount_begin, 32 .show_options = nfs_show_options, 33 .show_devname = nfs_show_devname, 34 .show_path = nfs_show_path, 35 .show_stats = nfs_show_stats, 36 }; 37 38 struct nfs_subversion nfs_v4 = { 39 .owner = THIS_MODULE, 40 .nfs_fs = &nfs4_fs_type, 41 .rpc_vers = &nfs_version4, 42 .rpc_ops = &nfs_v4_clientops, 43 .sops = &nfs4_sops, 44 .xattr = nfs4_xattr_handlers, 45 }; 46 47 static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc) 48 { 49 int ret = nfs_write_inode(inode, wbc); 50 51 if (ret == 0) 52 ret = pnfs_layoutcommit_inode(inode, 53 wbc->sync_mode == WB_SYNC_ALL); 54 return ret; 55 } 56 57 /* 58 * Clean out any remaining NFSv4 state that might be left over due 59 * to open() calls that passed nfs_atomic_lookup, but failed to call 60 * nfs_open(). 61 */ 62 static void nfs4_evict_inode(struct inode *inode) 63 { 64 truncate_inode_pages_final(&inode->i_data); 65 clear_inode(inode); 66 /* If we are holding a delegation, return and free it */ 67 nfs_inode_evict_delegation(inode); 68 /* Note that above delegreturn would trigger pnfs return-on-close */ 69 pnfs_return_layout(inode); 70 pnfs_destroy_layout_final(NFS_I(inode)); 71 /* First call standard NFS clear_inode() code */ 72 nfs_clear_inode(inode); 73 nfs4_xattr_cache_zap(inode); 74 } 75 76 struct nfs_referral_count { 77 struct list_head list; 78 const struct task_struct *task; 79 unsigned int referral_count; 80 }; 81 82 static LIST_HEAD(nfs_referral_count_list); 83 static DEFINE_SPINLOCK(nfs_referral_count_list_lock); 84 85 static struct nfs_referral_count *nfs_find_referral_count(void) 86 { 87 struct nfs_referral_count *p; 88 89 list_for_each_entry(p, &nfs_referral_count_list, list) { 90 if (p->task == current) 91 return p; 92 } 93 return NULL; 94 } 95 96 #define NFS_MAX_NESTED_REFERRALS 2 97 98 static int nfs_referral_loop_protect(void) 99 { 100 struct nfs_referral_count *p, *new; 101 int ret = -ENOMEM; 102 103 new = kmalloc(sizeof(*new), GFP_KERNEL); 104 if (!new) 105 goto out; 106 new->task = current; 107 new->referral_count = 1; 108 109 ret = 0; 110 spin_lock(&nfs_referral_count_list_lock); 111 p = nfs_find_referral_count(); 112 if (p != NULL) { 113 if (p->referral_count >= NFS_MAX_NESTED_REFERRALS) 114 ret = -ELOOP; 115 else 116 p->referral_count++; 117 } else { 118 list_add(&new->list, &nfs_referral_count_list); 119 new = NULL; 120 } 121 spin_unlock(&nfs_referral_count_list_lock); 122 kfree(new); 123 out: 124 return ret; 125 } 126 127 static void nfs_referral_loop_unprotect(void) 128 { 129 struct nfs_referral_count *p; 130 131 spin_lock(&nfs_referral_count_list_lock); 132 p = nfs_find_referral_count(); 133 p->referral_count--; 134 if (p->referral_count == 0) 135 list_del(&p->list); 136 else 137 p = NULL; 138 spin_unlock(&nfs_referral_count_list_lock); 139 kfree(p); 140 } 141 142 static int do_nfs4_mount(struct nfs_server *server, 143 struct fs_context *fc, 144 const char *hostname, 145 const char *export_path) 146 { 147 struct nfs_fs_context *root_ctx; 148 struct nfs_fs_context *ctx; 149 struct fs_context *root_fc; 150 struct vfsmount *root_mnt; 151 struct dentry *dentry; 152 char *source; 153 int ret; 154 155 if (IS_ERR(server)) 156 return PTR_ERR(server); 157 158 root_fc = vfs_dup_fs_context(fc); 159 if (IS_ERR(root_fc)) { 160 nfs_free_server(server); 161 return PTR_ERR(root_fc); 162 } 163 kfree(root_fc->source); 164 root_fc->source = NULL; 165 166 ctx = nfs_fc2context(fc); 167 root_ctx = nfs_fc2context(root_fc); 168 root_ctx->internal = true; 169 root_ctx->server = server; 170 171 if (ctx->fscache_uniq) { 172 ret = vfs_parse_fs_string(root_fc, "fsc", ctx->fscache_uniq); 173 if (ret < 0) { 174 put_fs_context(root_fc); 175 return ret; 176 } 177 } 178 /* We leave export_path unset as it's not used to find the root. */ 179 180 /* Does hostname needs to be enclosed in brackets? */ 181 if (strchr(hostname, ':')) 182 source = kasprintf(GFP_KERNEL, "[%s]:/", hostname); 183 else 184 source = kasprintf(GFP_KERNEL, "%s:/", hostname); 185 186 if (!source) { 187 put_fs_context(root_fc); 188 return -ENOMEM; 189 } 190 ret = vfs_parse_fs_string(root_fc, "source", source); 191 kfree(source); 192 if (ret < 0) { 193 put_fs_context(root_fc); 194 return ret; 195 } 196 root_mnt = fc_mount(root_fc); 197 put_fs_context(root_fc); 198 199 if (IS_ERR(root_mnt)) 200 return PTR_ERR(root_mnt); 201 202 ret = nfs_referral_loop_protect(); 203 if (ret) { 204 mntput(root_mnt); 205 return ret; 206 } 207 208 dentry = mount_subtree(root_mnt, export_path); 209 nfs_referral_loop_unprotect(); 210 211 if (IS_ERR(dentry)) 212 return PTR_ERR(dentry); 213 214 fc->root = dentry; 215 return 0; 216 } 217 218 int nfs4_try_get_tree(struct fs_context *fc) 219 { 220 struct nfs_fs_context *ctx = nfs_fc2context(fc); 221 int err; 222 223 dfprintk(MOUNT, "--> nfs4_try_get_tree()\n"); 224 225 /* We create a mount for the server's root, walk to the requested 226 * location and then create another mount for that. 227 */ 228 err= do_nfs4_mount(nfs4_create_server(fc), 229 fc, ctx->nfs_server.hostname, 230 ctx->nfs_server.export_path); 231 if (err) { 232 nfs_ferrorf(fc, MOUNT, "NFS4: Couldn't follow remote path"); 233 dfprintk(MOUNT, "<-- nfs4_try_get_tree() = %d [error]\n", err); 234 } else { 235 dfprintk(MOUNT, "<-- nfs4_try_get_tree() = 0\n"); 236 } 237 return err; 238 } 239 240 /* 241 * Create an NFS4 server record on referral traversal 242 */ 243 int nfs4_get_referral_tree(struct fs_context *fc) 244 { 245 struct nfs_fs_context *ctx = nfs_fc2context(fc); 246 int err; 247 248 dprintk("--> nfs4_referral_mount()\n"); 249 250 /* create a new volume representation */ 251 err = do_nfs4_mount(nfs4_create_referral_server(fc), 252 fc, ctx->nfs_server.hostname, 253 ctx->nfs_server.export_path); 254 if (err) { 255 nfs_ferrorf(fc, MOUNT, "NFS4: Couldn't follow remote path"); 256 dfprintk(MOUNT, "<-- nfs4_get_referral_tree() = %d [error]\n", err); 257 } else { 258 dfprintk(MOUNT, "<-- nfs4_get_referral_tree() = 0\n"); 259 } 260 return err; 261 } 262 263 static int __init init_nfs_v4(void) 264 { 265 int err; 266 267 err = nfs_dns_resolver_init(); 268 if (err) 269 goto out; 270 271 err = nfs_idmap_init(); 272 if (err) 273 goto out1; 274 275 #ifdef CONFIG_NFS_V4_2 276 err = nfs4_xattr_cache_init(); 277 if (err) 278 goto out2; 279 #endif 280 281 err = nfs4_register_sysctl(); 282 if (err) 283 goto out2; 284 285 #ifdef CONFIG_NFS_V4_2 286 nfs42_ssc_register_ops(); 287 #endif 288 register_nfs_version(&nfs_v4); 289 return 0; 290 out2: 291 nfs_idmap_quit(); 292 out1: 293 nfs_dns_resolver_destroy(); 294 out: 295 return err; 296 } 297 298 static void __exit exit_nfs_v4(void) 299 { 300 /* Not called in the _init(), conditionally loaded */ 301 nfs4_pnfs_v3_ds_connect_unload(); 302 303 unregister_nfs_version(&nfs_v4); 304 #ifdef CONFIG_NFS_V4_2 305 nfs4_xattr_cache_exit(); 306 nfs42_ssc_unregister_ops(); 307 #endif 308 nfs4_unregister_sysctl(); 309 nfs_idmap_quit(); 310 nfs_dns_resolver_destroy(); 311 } 312 313 MODULE_DESCRIPTION("NFSv4 client support"); 314 MODULE_LICENSE("GPL"); 315 316 module_init(init_nfs_v4); 317 module_exit(exit_nfs_v4); 318