Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Bryan Schumaker | 129d197 | 2012-07-16 16:39:13 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012 Bryan Schumaker <bjschuma@netapp.com> |
| 4 | */ |
| 5 | #include <linux/init.h> |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 6 | #include <linux/module.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" |
Anna Schumaker | 40c64c2 | 2015-04-15 13:00:05 -0400 | [diff] [blame] | 12 | #include "nfs4idmap.h" |
Trond Myklebust | c8d74d9 | 2013-06-01 11:50:58 -0400 | [diff] [blame] | 13 | #include "dns_resolve.h" |
Bryan Schumaker | 19d87ca | 2012-07-30 16:05:21 -0400 | [diff] [blame] | 14 | #include "pnfs.h" |
Bryan Schumaker | ab7017a | 2012-07-30 16:05:16 -0400 | [diff] [blame] | 15 | #include "nfs.h" |
Bryan Schumaker | 129d197 | 2012-07-16 16:39:13 -0400 | [diff] [blame] | 16 | |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 17 | #define NFSDBG_FACILITY NFSDBG_VFS |
| 18 | |
Bryan Schumaker | 19d87ca | 2012-07-30 16:05:21 -0400 | [diff] [blame] | 19 | static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc); |
| 20 | static void nfs4_evict_inode(struct inode *inode); |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 21 | static struct dentry *nfs4_remote_mount(struct file_system_type *fs_type, |
| 22 | int flags, const char *dev_name, void *raw_data); |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 23 | static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type, |
| 24 | int flags, const char *dev_name, void *raw_data); |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 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 | struct file_system_type nfs4_referral_fs_type = { |
| 35 | .owner = THIS_MODULE, |
| 36 | .name = "nfs4", |
| 37 | .mount = nfs4_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 | static const struct super_operations nfs4_sops = { |
| 43 | .alloc_inode = nfs_alloc_inode, |
Al Viro | ca1a199 | 2019-04-15 20:19:40 -0400 | [diff] [blame] | 44 | .free_inode = nfs_free_inode, |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 45 | .write_inode = nfs4_write_inode, |
Trond Myklebust | eed9935 | 2012-12-14 14:36:36 -0500 | [diff] [blame] | 46 | .drop_inode = nfs_drop_inode, |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 47 | .statfs = nfs_statfs, |
| 48 | .evict_inode = nfs4_evict_inode, |
| 49 | .umount_begin = nfs_umount_begin, |
| 50 | .show_options = nfs_show_options, |
| 51 | .show_devname = nfs_show_devname, |
| 52 | .show_path = nfs_show_path, |
| 53 | .show_stats = nfs_show_stats, |
| 54 | .remount_fs = nfs_remount, |
| 55 | }; |
| 56 | |
Bryan Schumaker | ab7017a | 2012-07-30 16:05:16 -0400 | [diff] [blame] | 57 | struct nfs_subversion nfs_v4 = { |
| 58 | .owner = THIS_MODULE, |
| 59 | .nfs_fs = &nfs4_fs_type, |
| 60 | .rpc_vers = &nfs_version4, |
| 61 | .rpc_ops = &nfs_v4_clientops, |
Bryan Schumaker | 6a74490 | 2012-07-30 16:05:20 -0400 | [diff] [blame] | 62 | .sops = &nfs4_sops, |
| 63 | .xattr = nfs4_xattr_handlers, |
Bryan Schumaker | ab7017a | 2012-07-30 16:05:16 -0400 | [diff] [blame] | 64 | }; |
| 65 | |
Bryan Schumaker | 19d87ca | 2012-07-30 16:05:21 -0400 | [diff] [blame] | 66 | static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc) |
| 67 | { |
| 68 | int ret = nfs_write_inode(inode, wbc); |
| 69 | |
Trond Myklebust | 71244d9 | 2014-01-13 13:34:36 -0500 | [diff] [blame] | 70 | if (ret == 0) |
| 71 | ret = pnfs_layoutcommit_inode(inode, |
| 72 | wbc->sync_mode == WB_SYNC_ALL); |
Bryan Schumaker | 19d87ca | 2012-07-30 16:05:21 -0400 | [diff] [blame] | 73 | return ret; |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * Clean out any remaining NFSv4 state that might be left over due |
| 78 | * to open() calls that passed nfs_atomic_lookup, but failed to call |
| 79 | * nfs_open(). |
| 80 | */ |
| 81 | static void nfs4_evict_inode(struct inode *inode) |
| 82 | { |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 83 | truncate_inode_pages_final(&inode->i_data); |
Bryan Schumaker | 19d87ca | 2012-07-30 16:05:21 -0400 | [diff] [blame] | 84 | clear_inode(inode); |
Trond Myklebust | b47e0e4 | 2019-10-21 14:04:00 -0400 | [diff] [blame] | 85 | /* If we are holding a delegation, return and free it */ |
| 86 | nfs_inode_evict_delegation(inode); |
Trond Myklebust | 415320f | 2015-03-25 13:26:18 -0400 | [diff] [blame] | 87 | /* Note that above delegreturn would trigger pnfs return-on-close */ |
| 88 | pnfs_return_layout(inode); |
| 89 | pnfs_destroy_layout(NFS_I(inode)); |
Bryan Schumaker | 19d87ca | 2012-07-30 16:05:21 -0400 | [diff] [blame] | 90 | /* First call standard NFS clear_inode() code */ |
| 91 | nfs_clear_inode(inode); |
| 92 | } |
| 93 | |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 94 | /* |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 95 | * Get the superblock for the NFS4 root partition |
| 96 | */ |
| 97 | static struct dentry * |
| 98 | nfs4_remote_mount(struct file_system_type *fs_type, int flags, |
| 99 | const char *dev_name, void *info) |
| 100 | { |
Al Viro | 7643c12 | 2019-12-10 07:30:51 -0500 | [diff] [blame] | 101 | return nfs_fs_mount_common(flags, dev_name, info, &nfs_v4); |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 102 | } |
| 103 | |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 104 | struct nfs_referral_count { |
| 105 | struct list_head list; |
| 106 | const struct task_struct *task; |
| 107 | unsigned int referral_count; |
| 108 | }; |
| 109 | |
| 110 | static LIST_HEAD(nfs_referral_count_list); |
| 111 | static DEFINE_SPINLOCK(nfs_referral_count_list_lock); |
| 112 | |
| 113 | static struct nfs_referral_count *nfs_find_referral_count(void) |
| 114 | { |
| 115 | struct nfs_referral_count *p; |
| 116 | |
| 117 | list_for_each_entry(p, &nfs_referral_count_list, list) { |
| 118 | if (p->task == current) |
| 119 | return p; |
| 120 | } |
| 121 | return NULL; |
| 122 | } |
| 123 | |
| 124 | #define NFS_MAX_NESTED_REFERRALS 2 |
| 125 | |
| 126 | static int nfs_referral_loop_protect(void) |
| 127 | { |
| 128 | struct nfs_referral_count *p, *new; |
| 129 | int ret = -ENOMEM; |
| 130 | |
| 131 | new = kmalloc(sizeof(*new), GFP_KERNEL); |
| 132 | if (!new) |
| 133 | goto out; |
| 134 | new->task = current; |
| 135 | new->referral_count = 1; |
| 136 | |
| 137 | ret = 0; |
| 138 | spin_lock(&nfs_referral_count_list_lock); |
| 139 | p = nfs_find_referral_count(); |
| 140 | if (p != NULL) { |
| 141 | if (p->referral_count >= NFS_MAX_NESTED_REFERRALS) |
| 142 | ret = -ELOOP; |
| 143 | else |
| 144 | p->referral_count++; |
| 145 | } else { |
| 146 | list_add(&new->list, &nfs_referral_count_list); |
| 147 | new = NULL; |
| 148 | } |
| 149 | spin_unlock(&nfs_referral_count_list_lock); |
| 150 | kfree(new); |
| 151 | out: |
| 152 | return ret; |
| 153 | } |
| 154 | |
| 155 | static void nfs_referral_loop_unprotect(void) |
| 156 | { |
| 157 | struct nfs_referral_count *p; |
| 158 | |
| 159 | spin_lock(&nfs_referral_count_list_lock); |
| 160 | p = nfs_find_referral_count(); |
| 161 | p->referral_count--; |
| 162 | if (p->referral_count == 0) |
| 163 | list_del(&p->list); |
| 164 | else |
| 165 | p = NULL; |
| 166 | spin_unlock(&nfs_referral_count_list_lock); |
| 167 | kfree(p); |
| 168 | } |
| 169 | |
Al Viro | 4e35776 | 2019-12-10 07:30:54 -0500 | [diff] [blame^] | 170 | static struct dentry *do_nfs4_mount(struct nfs_server *server, int flags, |
| 171 | struct nfs_mount_info *info, |
| 172 | const char *hostname, |
| 173 | const char *export_path) |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 174 | { |
Al Viro | 4e35776 | 2019-12-10 07:30:54 -0500 | [diff] [blame^] | 175 | struct vfsmount *root_mnt; |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 176 | struct dentry *dentry; |
Al Viro | 4e35776 | 2019-12-10 07:30:54 -0500 | [diff] [blame^] | 177 | char *root_devname; |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 178 | int err; |
Al Viro | 4e35776 | 2019-12-10 07:30:54 -0500 | [diff] [blame^] | 179 | size_t len; |
| 180 | |
| 181 | if (IS_ERR(server)) |
| 182 | return ERR_CAST(server); |
| 183 | |
| 184 | len = strlen(hostname) + 5; |
| 185 | root_devname = kmalloc(len, GFP_KERNEL); |
| 186 | if (root_devname == NULL) { |
| 187 | nfs_free_server(server); |
| 188 | return ERR_PTR(-ENOMEM); |
| 189 | } |
| 190 | |
| 191 | /* Does hostname needs to be enclosed in brackets? */ |
| 192 | if (strchr(hostname, ':')) |
| 193 | snprintf(root_devname, len, "[%s]:/", hostname); |
| 194 | else |
| 195 | snprintf(root_devname, len, "%s:/", hostname); |
| 196 | info->server = server; |
| 197 | root_mnt = vfs_kern_mount(&nfs4_remote_fs_type, flags, root_devname, info); |
| 198 | if (info->server) |
| 199 | nfs_free_server(info->server); |
| 200 | info->server = NULL; |
| 201 | kfree(root_devname); |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 202 | |
| 203 | if (IS_ERR(root_mnt)) |
| 204 | return ERR_CAST(root_mnt); |
| 205 | |
| 206 | err = nfs_referral_loop_protect(); |
| 207 | if (err) { |
| 208 | mntput(root_mnt); |
| 209 | return ERR_PTR(err); |
| 210 | } |
| 211 | |
| 212 | dentry = mount_subtree(root_mnt, export_path); |
| 213 | nfs_referral_loop_unprotect(); |
| 214 | |
| 215 | return dentry; |
| 216 | } |
| 217 | |
| 218 | struct dentry *nfs4_try_mount(int flags, const char *dev_name, |
Bryan Schumaker | ff9099f2 | 2012-07-30 16:05:18 -0400 | [diff] [blame] | 219 | struct nfs_mount_info *mount_info, |
| 220 | struct nfs_subversion *nfs_mod) |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 221 | { |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 222 | struct nfs_parsed_mount_data *data = mount_info->parsed; |
Al Viro | 4e35776 | 2019-12-10 07:30:54 -0500 | [diff] [blame^] | 223 | struct dentry *res; |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 224 | |
Al Viro | 7643c12 | 2019-12-10 07:30:51 -0500 | [diff] [blame] | 225 | mount_info->set_security = nfs_set_sb_security; |
| 226 | |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 227 | dfprintk(MOUNT, "--> nfs4_try_mount()\n"); |
| 228 | |
Al Viro | 4e35776 | 2019-12-10 07:30:54 -0500 | [diff] [blame^] | 229 | res = do_nfs4_mount(nfs4_create_server(mount_info, &nfs_v4), |
| 230 | flags, mount_info, |
| 231 | data->nfs_server.hostname, |
| 232 | data->nfs_server.export_path); |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 233 | |
Geyslan G. Bem | 6706246 | 2013-10-14 17:24:16 -0300 | [diff] [blame] | 234 | dfprintk(MOUNT, "<-- nfs4_try_mount() = %d%s\n", |
| 235 | PTR_ERR_OR_ZERO(res), |
| 236 | IS_ERR(res) ? " [error]" : ""); |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 237 | return res; |
| 238 | } |
| 239 | |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 240 | /* |
| 241 | * Create an NFS4 server record on referral traversal |
| 242 | */ |
| 243 | static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type, |
| 244 | int flags, const char *dev_name, void *raw_data) |
| 245 | { |
| 246 | struct nfs_clone_mount *data = raw_data; |
Al Viro | 7643c12 | 2019-12-10 07:30:51 -0500 | [diff] [blame] | 247 | struct nfs_mount_info mount_info = { |
| 248 | .fill_super = nfs_fill_super, |
| 249 | .set_security = nfs_clone_sb_security, |
| 250 | .cloned = data, |
| 251 | }; |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 252 | struct dentry *res; |
| 253 | |
| 254 | dprintk("--> nfs4_referral_mount()\n"); |
| 255 | |
Al Viro | 7643c12 | 2019-12-10 07:30:51 -0500 | [diff] [blame] | 256 | mount_info.mntfh = nfs_alloc_fhandle(); |
| 257 | if (!mount_info.mntfh) |
| 258 | return ERR_PTR(-ENOMEM); |
| 259 | |
Al Viro | 4e35776 | 2019-12-10 07:30:54 -0500 | [diff] [blame^] | 260 | res = do_nfs4_mount(nfs4_create_referral_server(mount_info.cloned, |
| 261 | mount_info.mntfh), |
| 262 | flags, &mount_info, data->hostname, data->mnt_path); |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 263 | |
Geyslan G. Bem | 6706246 | 2013-10-14 17:24:16 -0300 | [diff] [blame] | 264 | dprintk("<-- nfs4_referral_mount() = %d%s\n", |
| 265 | PTR_ERR_OR_ZERO(res), |
| 266 | IS_ERR(res) ? " [error]" : ""); |
Al Viro | 7643c12 | 2019-12-10 07:30:51 -0500 | [diff] [blame] | 267 | |
| 268 | nfs_free_fhandle(mount_info.mntfh); |
Bryan Schumaker | fbdefd6 | 2012-07-16 16:39:20 -0400 | [diff] [blame] | 269 | return res; |
| 270 | } |
| 271 | |
| 272 | |
Bryan Schumaker | 89d77c8 | 2012-07-30 16:05:25 -0400 | [diff] [blame] | 273 | static int __init init_nfs_v4(void) |
Bryan Schumaker | 129d197 | 2012-07-16 16:39:13 -0400 | [diff] [blame] | 274 | { |
| 275 | int err; |
| 276 | |
Trond Myklebust | c8d74d9 | 2013-06-01 11:50:58 -0400 | [diff] [blame] | 277 | err = nfs_dns_resolver_init(); |
Bryan Schumaker | 129d197 | 2012-07-16 16:39:13 -0400 | [diff] [blame] | 278 | if (err) |
| 279 | goto out; |
| 280 | |
Trond Myklebust | c8d74d9 | 2013-06-01 11:50:58 -0400 | [diff] [blame] | 281 | err = nfs_idmap_init(); |
Bryan Schumaker | 466bfe7 | 2012-07-16 16:39:14 -0400 | [diff] [blame] | 282 | if (err) |
| 283 | goto out1; |
| 284 | |
Trond Myklebust | c8d74d9 | 2013-06-01 11:50:58 -0400 | [diff] [blame] | 285 | err = nfs4_register_sysctl(); |
| 286 | if (err) |
| 287 | goto out2; |
| 288 | |
Bryan Schumaker | ab7017a | 2012-07-30 16:05:16 -0400 | [diff] [blame] | 289 | register_nfs_version(&nfs_v4); |
Bryan Schumaker | 129d197 | 2012-07-16 16:39:13 -0400 | [diff] [blame] | 290 | return 0; |
Trond Myklebust | c8d74d9 | 2013-06-01 11:50:58 -0400 | [diff] [blame] | 291 | out2: |
Bryan Schumaker | 466bfe7 | 2012-07-16 16:39:14 -0400 | [diff] [blame] | 292 | nfs_idmap_quit(); |
Trond Myklebust | c8d74d9 | 2013-06-01 11:50:58 -0400 | [diff] [blame] | 293 | out1: |
| 294 | nfs_dns_resolver_destroy(); |
Bryan Schumaker | 129d197 | 2012-07-16 16:39:13 -0400 | [diff] [blame] | 295 | out: |
| 296 | return err; |
| 297 | } |
| 298 | |
Bryan Schumaker | 89d77c8 | 2012-07-30 16:05:25 -0400 | [diff] [blame] | 299 | static void __exit exit_nfs_v4(void) |
Bryan Schumaker | 129d197 | 2012-07-16 16:39:13 -0400 | [diff] [blame] | 300 | { |
Peng Tao | 5f01d95 | 2014-05-30 18:15:59 +0800 | [diff] [blame] | 301 | /* Not called in the _init(), conditionally loaded */ |
| 302 | nfs4_pnfs_v3_ds_connect_unload(); |
| 303 | |
Bryan Schumaker | ab7017a | 2012-07-30 16:05:16 -0400 | [diff] [blame] | 304 | unregister_nfs_version(&nfs_v4); |
Bryan Schumaker | 466bfe7 | 2012-07-16 16:39:14 -0400 | [diff] [blame] | 305 | nfs4_unregister_sysctl(); |
Bryan Schumaker | 129d197 | 2012-07-16 16:39:13 -0400 | [diff] [blame] | 306 | nfs_idmap_quit(); |
Trond Myklebust | c8d74d9 | 2013-06-01 11:50:58 -0400 | [diff] [blame] | 307 | nfs_dns_resolver_destroy(); |
Bryan Schumaker | 129d197 | 2012-07-16 16:39:13 -0400 | [diff] [blame] | 308 | } |
Bryan Schumaker | 89d77c8 | 2012-07-30 16:05:25 -0400 | [diff] [blame] | 309 | |
| 310 | MODULE_LICENSE("GPL"); |
| 311 | |
| 312 | module_init(init_nfs_v4); |
| 313 | module_exit(exit_nfs_v4); |