blob: 0c1ab846b83ddb47accf100ba12eed05c862d91b [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Bryan Schumaker129d1972012-07-16 16:39:13 -04002/*
3 * Copyright (c) 2012 Bryan Schumaker <bjschuma@netapp.com>
4 */
5#include <linux/init.h>
Bryan Schumakerfbdefd62012-07-16 16:39:20 -04006#include <linux/module.h>
David Howellsf2aedb72019-12-10 07:31:13 -05007#include <linux/mount.h>
Bryan Schumakerfbdefd62012-07-16 16:39:20 -04008#include <linux/nfs4_mount.h>
Bryan Schumaker466bfe72012-07-16 16:39:14 -04009#include <linux/nfs_fs.h>
Bryan Schumaker19d87ca2012-07-30 16:05:21 -040010#include "delegation.h"
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040011#include "internal.h"
Bryan Schumaker466bfe72012-07-16 16:39:14 -040012#include "nfs4_fs.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040013#include "nfs4idmap.h"
Trond Myklebustc8d74d92013-06-01 11:50:58 -040014#include "dns_resolve.h"
Bryan Schumaker19d87ca2012-07-30 16:05:21 -040015#include "pnfs.h"
Bryan Schumakerab7017a2012-07-30 16:05:16 -040016#include "nfs.h"
Bryan Schumaker129d1972012-07-16 16:39:13 -040017
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040018#define NFSDBG_FACILITY NFSDBG_VFS
19
Bryan Schumaker19d87ca2012-07-30 16:05:21 -040020static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc);
21static void nfs4_evict_inode(struct inode *inode);
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040022
23static const struct super_operations nfs4_sops = {
24 .alloc_inode = nfs_alloc_inode,
Al Viroca1a1992019-04-15 20:19:40 -040025 .free_inode = nfs_free_inode,
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040026 .write_inode = nfs4_write_inode,
Trond Myklebusteed99352012-12-14 14:36:36 -050027 .drop_inode = nfs_drop_inode,
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040028 .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 Schumakerfbdefd62012-07-16 16:39:20 -040035};
36
Bryan Schumakerab7017a2012-07-30 16:05:16 -040037struct nfs_subversion nfs_v4 = {
Scott Mayhew62a55d02019-12-10 07:31:14 -050038 .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 Schumakerab7017a2012-07-30 16:05:16 -040044};
45
Bryan Schumaker19d87ca2012-07-30 16:05:21 -040046static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc)
47{
48 int ret = nfs_write_inode(inode, wbc);
49
Trond Myklebust71244d92014-01-13 13:34:36 -050050 if (ret == 0)
51 ret = pnfs_layoutcommit_inode(inode,
52 wbc->sync_mode == WB_SYNC_ALL);
Bryan Schumaker19d87ca2012-07-30 16:05:21 -040053 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 */
61static void nfs4_evict_inode(struct inode *inode)
62{
Johannes Weiner91b0abe2014-04-03 14:47:49 -070063 truncate_inode_pages_final(&inode->i_data);
Bryan Schumaker19d87ca2012-07-30 16:05:21 -040064 clear_inode(inode);
Trond Myklebustb47e0e42019-10-21 14:04:00 -040065 /* If we are holding a delegation, return and free it */
66 nfs_inode_evict_delegation(inode);
Trond Myklebust415320f2015-03-25 13:26:18 -040067 /* Note that above delegreturn would trigger pnfs return-on-close */
68 pnfs_return_layout(inode);
69 pnfs_destroy_layout(NFS_I(inode));
Bryan Schumaker19d87ca2012-07-30 16:05:21 -040070 /* First call standard NFS clear_inode() code */
71 nfs_clear_inode(inode);
Frank van der Linden95ad37f2020-06-23 22:39:04 +000072 nfs4_xattr_cache_zap(inode);
Bryan Schumaker19d87ca2012-07-30 16:05:21 -040073}
74
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040075struct nfs_referral_count {
76 struct list_head list;
77 const struct task_struct *task;
78 unsigned int referral_count;
79};
80
81static LIST_HEAD(nfs_referral_count_list);
82static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
83
84static struct nfs_referral_count *nfs_find_referral_count(void)
85{
86 struct nfs_referral_count *p;
87
88 list_for_each_entry(p, &nfs_referral_count_list, list) {
89 if (p->task == current)
90 return p;
91 }
92 return NULL;
93}
94
95#define NFS_MAX_NESTED_REFERRALS 2
96
97static int nfs_referral_loop_protect(void)
98{
99 struct nfs_referral_count *p, *new;
100 int ret = -ENOMEM;
101
102 new = kmalloc(sizeof(*new), GFP_KERNEL);
103 if (!new)
104 goto out;
105 new->task = current;
106 new->referral_count = 1;
107
108 ret = 0;
109 spin_lock(&nfs_referral_count_list_lock);
110 p = nfs_find_referral_count();
111 if (p != NULL) {
112 if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
113 ret = -ELOOP;
114 else
115 p->referral_count++;
116 } else {
117 list_add(&new->list, &nfs_referral_count_list);
118 new = NULL;
119 }
120 spin_unlock(&nfs_referral_count_list_lock);
121 kfree(new);
122out:
123 return ret;
124}
125
126static void nfs_referral_loop_unprotect(void)
127{
128 struct nfs_referral_count *p;
129
130 spin_lock(&nfs_referral_count_list_lock);
131 p = nfs_find_referral_count();
132 p->referral_count--;
133 if (p->referral_count == 0)
134 list_del(&p->list);
135 else
136 p = NULL;
137 spin_unlock(&nfs_referral_count_list_lock);
138 kfree(p);
139}
140
David Howellsf2aedb72019-12-10 07:31:13 -0500141static int do_nfs4_mount(struct nfs_server *server,
142 struct fs_context *fc,
143 const char *hostname,
144 const char *export_path)
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400145{
David Howellsf2aedb72019-12-10 07:31:13 -0500146 struct nfs_fs_context *root_ctx;
147 struct fs_context *root_fc;
Al Viro4e357762019-12-10 07:30:54 -0500148 struct vfsmount *root_mnt;
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400149 struct dentry *dentry;
Al Viro4e357762019-12-10 07:30:54 -0500150 size_t len;
David Howellsf2aedb72019-12-10 07:31:13 -0500151 int ret;
152
153 struct fs_parameter param = {
154 .key = "source",
155 .type = fs_value_is_string,
156 .dirfd = -1,
157 };
Al Viro4e357762019-12-10 07:30:54 -0500158
159 if (IS_ERR(server))
David Howellsf2aedb72019-12-10 07:31:13 -0500160 return PTR_ERR(server);
161
162 root_fc = vfs_dup_fs_context(fc);
163 if (IS_ERR(root_fc)) {
164 nfs_free_server(server);
165 return PTR_ERR(root_fc);
166 }
167 kfree(root_fc->source);
168 root_fc->source = NULL;
169
170 root_ctx = nfs_fc2context(root_fc);
171 root_ctx->internal = true;
Scott Mayhew62a55d02019-12-10 07:31:14 -0500172 root_ctx->server = server;
David Howellsf2aedb72019-12-10 07:31:13 -0500173 /* We leave export_path unset as it's not used to find the root. */
Al Viro4e357762019-12-10 07:30:54 -0500174
175 len = strlen(hostname) + 5;
David Howellsf2aedb72019-12-10 07:31:13 -0500176 param.string = kmalloc(len, GFP_KERNEL);
177 if (param.string == NULL) {
178 put_fs_context(root_fc);
179 return -ENOMEM;
Al Viro4e357762019-12-10 07:30:54 -0500180 }
181
182 /* Does hostname needs to be enclosed in brackets? */
183 if (strchr(hostname, ':'))
David Howellsf2aedb72019-12-10 07:31:13 -0500184 param.size = snprintf(param.string, len, "[%s]:/", hostname);
Al Viro4e357762019-12-10 07:30:54 -0500185 else
David Howellsf2aedb72019-12-10 07:31:13 -0500186 param.size = snprintf(param.string, len, "%s:/", hostname);
187 ret = vfs_parse_fs_param(root_fc, &param);
188 kfree(param.string);
189 if (ret < 0) {
190 put_fs_context(root_fc);
191 return ret;
192 }
193 root_mnt = fc_mount(root_fc);
194 put_fs_context(root_fc);
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400195
196 if (IS_ERR(root_mnt))
David Howellsf2aedb72019-12-10 07:31:13 -0500197 return PTR_ERR(root_mnt);
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400198
David Howellsf2aedb72019-12-10 07:31:13 -0500199 ret = nfs_referral_loop_protect();
200 if (ret) {
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400201 mntput(root_mnt);
David Howellsf2aedb72019-12-10 07:31:13 -0500202 return ret;
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400203 }
204
205 dentry = mount_subtree(root_mnt, export_path);
206 nfs_referral_loop_unprotect();
207
David Howellsf2aedb72019-12-10 07:31:13 -0500208 if (IS_ERR(dentry))
209 return PTR_ERR(dentry);
210
211 fc->root = dentry;
212 return 0;
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400213}
214
David Howellsf2aedb72019-12-10 07:31:13 -0500215int nfs4_try_get_tree(struct fs_context *fc)
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400216{
David Howellsf2aedb72019-12-10 07:31:13 -0500217 struct nfs_fs_context *ctx = nfs_fc2context(fc);
218 int err;
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400219
David Howellsf2aedb72019-12-10 07:31:13 -0500220 dfprintk(MOUNT, "--> nfs4_try_get_tree()\n");
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400221
David Howellsf2aedb72019-12-10 07:31:13 -0500222 /* We create a mount for the server's root, walk to the requested
223 * location and then create another mount for that.
224 */
Scott Mayhew62a55d02019-12-10 07:31:14 -0500225 err= do_nfs4_mount(nfs4_create_server(fc),
David Howellsf2aedb72019-12-10 07:31:13 -0500226 fc, ctx->nfs_server.hostname,
227 ctx->nfs_server.export_path);
228 if (err) {
Scott Mayhewce8866f02019-12-10 07:31:15 -0500229 nfs_errorf(fc, "NFS4: Couldn't follow remote path");
David Howellsf2aedb72019-12-10 07:31:13 -0500230 dfprintk(MOUNT, "<-- nfs4_try_get_tree() = %d [error]\n", err);
231 } else {
232 dfprintk(MOUNT, "<-- nfs4_try_get_tree() = 0\n");
233 }
234 return err;
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400235}
236
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400237/*
238 * Create an NFS4 server record on referral traversal
239 */
David Howellsf2aedb72019-12-10 07:31:13 -0500240int nfs4_get_referral_tree(struct fs_context *fc)
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400241{
David Howellsf2aedb72019-12-10 07:31:13 -0500242 struct nfs_fs_context *ctx = nfs_fc2context(fc);
243 int err;
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400244
245 dprintk("--> nfs4_referral_mount()\n");
246
David Howellsf2aedb72019-12-10 07:31:13 -0500247 /* create a new volume representation */
Scott Mayhew62a55d02019-12-10 07:31:14 -0500248 err = do_nfs4_mount(nfs4_create_referral_server(fc),
David Howellsf2aedb72019-12-10 07:31:13 -0500249 fc, ctx->nfs_server.hostname,
250 ctx->nfs_server.export_path);
251 if (err) {
Scott Mayhewce8866f02019-12-10 07:31:15 -0500252 nfs_errorf(fc, "NFS4: Couldn't follow remote path");
David Howellsf2aedb72019-12-10 07:31:13 -0500253 dfprintk(MOUNT, "<-- nfs4_get_referral_tree() = %d [error]\n", err);
254 } else {
255 dfprintk(MOUNT, "<-- nfs4_get_referral_tree() = 0\n");
256 }
257 return err;
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400258}
259
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400260static int __init init_nfs_v4(void)
Bryan Schumaker129d1972012-07-16 16:39:13 -0400261{
262 int err;
263
Trond Myklebustc8d74d92013-06-01 11:50:58 -0400264 err = nfs_dns_resolver_init();
Bryan Schumaker129d1972012-07-16 16:39:13 -0400265 if (err)
266 goto out;
267
Trond Myklebustc8d74d92013-06-01 11:50:58 -0400268 err = nfs_idmap_init();
Bryan Schumaker466bfe72012-07-16 16:39:14 -0400269 if (err)
270 goto out1;
271
Frank van der Linden95ad37f2020-06-23 22:39:04 +0000272#ifdef CONFIG_NFS_V4_2
273 err = nfs4_xattr_cache_init();
274 if (err)
275 goto out2;
276#endif
277
Trond Myklebustc8d74d92013-06-01 11:50:58 -0400278 err = nfs4_register_sysctl();
279 if (err)
280 goto out2;
281
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400282 register_nfs_version(&nfs_v4);
Bryan Schumaker129d1972012-07-16 16:39:13 -0400283 return 0;
Trond Myklebustc8d74d92013-06-01 11:50:58 -0400284out2:
Bryan Schumaker466bfe72012-07-16 16:39:14 -0400285 nfs_idmap_quit();
Trond Myklebustc8d74d92013-06-01 11:50:58 -0400286out1:
287 nfs_dns_resolver_destroy();
Bryan Schumaker129d1972012-07-16 16:39:13 -0400288out:
289 return err;
290}
291
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400292static void __exit exit_nfs_v4(void)
Bryan Schumaker129d1972012-07-16 16:39:13 -0400293{
Peng Tao5f01d952014-05-30 18:15:59 +0800294 /* Not called in the _init(), conditionally loaded */
295 nfs4_pnfs_v3_ds_connect_unload();
296
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400297 unregister_nfs_version(&nfs_v4);
Frank van der Linden95ad37f2020-06-23 22:39:04 +0000298#ifdef CONFIG_NFS_V4_2
299 nfs4_xattr_cache_exit();
300#endif
Bryan Schumaker466bfe72012-07-16 16:39:14 -0400301 nfs4_unregister_sysctl();
Bryan Schumaker129d1972012-07-16 16:39:13 -0400302 nfs_idmap_quit();
Trond Myklebustc8d74d92013-06-01 11:50:58 -0400303 nfs_dns_resolver_destroy();
Bryan Schumaker129d1972012-07-16 16:39:13 -0400304}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400305
306MODULE_LICENSE("GPL");
307
308module_init(init_nfs_v4);
309module_exit(exit_nfs_v4);