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