blob: b422e8a09d2510445331c20ea838a094a093e036 [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>
Dai Ngo0cfcd402020-10-18 23:42:49 -040010#include <linux/nfs_ssc.h>
Bryan Schumaker19d87ca2012-07-30 16:05:21 -040011#include "delegation.h"
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040012#include "internal.h"
Bryan Schumaker466bfe72012-07-16 16:39:14 -040013#include "nfs4_fs.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040014#include "nfs4idmap.h"
Trond Myklebustc8d74d92013-06-01 11:50:58 -040015#include "dns_resolve.h"
Bryan Schumaker19d87ca2012-07-30 16:05:21 -040016#include "pnfs.h"
Bryan Schumakerab7017a2012-07-30 16:05:16 -040017#include "nfs.h"
Bryan Schumaker129d1972012-07-16 16:39:13 -040018
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040019#define NFSDBG_FACILITY NFSDBG_VFS
20
Bryan Schumaker19d87ca2012-07-30 16:05:21 -040021static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc);
22static void nfs4_evict_inode(struct inode *inode);
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040023
24static const struct super_operations nfs4_sops = {
25 .alloc_inode = nfs_alloc_inode,
Al Viroca1a1992019-04-15 20:19:40 -040026 .free_inode = nfs_free_inode,
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040027 .write_inode = nfs4_write_inode,
Trond Myklebusteed99352012-12-14 14:36:36 -050028 .drop_inode = nfs_drop_inode,
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040029 .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,
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040036};
37
Bryan Schumakerab7017a2012-07-30 16:05:16 -040038struct nfs_subversion nfs_v4 = {
Scott Mayhew62a55d02019-12-10 07:31:14 -050039 .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,
Bryan Schumakerab7017a2012-07-30 16:05:16 -040045};
46
Bryan Schumaker19d87ca2012-07-30 16:05:21 -040047static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc)
48{
49 int ret = nfs_write_inode(inode, wbc);
50
Trond Myklebust71244d92014-01-13 13:34:36 -050051 if (ret == 0)
52 ret = pnfs_layoutcommit_inode(inode,
53 wbc->sync_mode == WB_SYNC_ALL);
Bryan Schumaker19d87ca2012-07-30 16:05:21 -040054 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 */
62static void nfs4_evict_inode(struct inode *inode)
63{
Johannes Weiner91b0abe2014-04-03 14:47:49 -070064 truncate_inode_pages_final(&inode->i_data);
Bryan Schumaker19d87ca2012-07-30 16:05:21 -040065 clear_inode(inode);
Trond Myklebustb47e0e42019-10-21 14:04:00 -040066 /* If we are holding a delegation, return and free it */
67 nfs_inode_evict_delegation(inode);
Trond Myklebust415320f2015-03-25 13:26:18 -040068 /* Note that above delegreturn would trigger pnfs return-on-close */
69 pnfs_return_layout(inode);
Trond Myklebust3c0f0f52020-11-25 12:06:14 -050070 pnfs_destroy_layout_final(NFS_I(inode));
Bryan Schumaker19d87ca2012-07-30 16:05:21 -040071 /* First call standard NFS clear_inode() code */
72 nfs_clear_inode(inode);
Frank van der Linden95ad37f2020-06-23 22:39:04 +000073 nfs4_xattr_cache_zap(inode);
Bryan Schumaker19d87ca2012-07-30 16:05:21 -040074}
75
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040076struct nfs_referral_count {
77 struct list_head list;
78 const struct task_struct *task;
79 unsigned int referral_count;
80};
81
82static LIST_HEAD(nfs_referral_count_list);
83static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
84
85static 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
98static 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);
123out:
124 return ret;
125}
126
127static 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
David Howellsf2aedb72019-12-10 07:31:13 -0500142static int do_nfs4_mount(struct nfs_server *server,
143 struct fs_context *fc,
144 const char *hostname,
145 const char *export_path)
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400146{
David Howellsf2aedb72019-12-10 07:31:13 -0500147 struct nfs_fs_context *root_ctx;
148 struct fs_context *root_fc;
Al Viro4e357762019-12-10 07:30:54 -0500149 struct vfsmount *root_mnt;
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400150 struct dentry *dentry;
Al Viro4e357762019-12-10 07:30:54 -0500151 size_t len;
David Howellsf2aedb72019-12-10 07:31:13 -0500152 int ret;
153
154 struct fs_parameter param = {
155 .key = "source",
156 .type = fs_value_is_string,
157 .dirfd = -1,
158 };
Al Viro4e357762019-12-10 07:30:54 -0500159
160 if (IS_ERR(server))
David Howellsf2aedb72019-12-10 07:31:13 -0500161 return PTR_ERR(server);
162
163 root_fc = vfs_dup_fs_context(fc);
164 if (IS_ERR(root_fc)) {
165 nfs_free_server(server);
166 return PTR_ERR(root_fc);
167 }
168 kfree(root_fc->source);
169 root_fc->source = NULL;
170
171 root_ctx = nfs_fc2context(root_fc);
172 root_ctx->internal = true;
Scott Mayhew62a55d02019-12-10 07:31:14 -0500173 root_ctx->server = server;
David Howellsf2aedb72019-12-10 07:31:13 -0500174 /* We leave export_path unset as it's not used to find the root. */
Al Viro4e357762019-12-10 07:30:54 -0500175
176 len = strlen(hostname) + 5;
David Howellsf2aedb72019-12-10 07:31:13 -0500177 param.string = kmalloc(len, GFP_KERNEL);
178 if (param.string == NULL) {
179 put_fs_context(root_fc);
180 return -ENOMEM;
Al Viro4e357762019-12-10 07:30:54 -0500181 }
182
183 /* Does hostname needs to be enclosed in brackets? */
184 if (strchr(hostname, ':'))
David Howellsf2aedb72019-12-10 07:31:13 -0500185 param.size = snprintf(param.string, len, "[%s]:/", hostname);
Al Viro4e357762019-12-10 07:30:54 -0500186 else
David Howellsf2aedb72019-12-10 07:31:13 -0500187 param.size = snprintf(param.string, len, "%s:/", hostname);
188 ret = vfs_parse_fs_param(root_fc, &param);
189 kfree(param.string);
190 if (ret < 0) {
191 put_fs_context(root_fc);
192 return ret;
193 }
194 root_mnt = fc_mount(root_fc);
195 put_fs_context(root_fc);
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400196
197 if (IS_ERR(root_mnt))
David Howellsf2aedb72019-12-10 07:31:13 -0500198 return PTR_ERR(root_mnt);
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400199
David Howellsf2aedb72019-12-10 07:31:13 -0500200 ret = nfs_referral_loop_protect();
201 if (ret) {
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400202 mntput(root_mnt);
David Howellsf2aedb72019-12-10 07:31:13 -0500203 return ret;
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400204 }
205
206 dentry = mount_subtree(root_mnt, export_path);
207 nfs_referral_loop_unprotect();
208
David Howellsf2aedb72019-12-10 07:31:13 -0500209 if (IS_ERR(dentry))
210 return PTR_ERR(dentry);
211
212 fc->root = dentry;
213 return 0;
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400214}
215
David Howellsf2aedb72019-12-10 07:31:13 -0500216int nfs4_try_get_tree(struct fs_context *fc)
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400217{
David Howellsf2aedb72019-12-10 07:31:13 -0500218 struct nfs_fs_context *ctx = nfs_fc2context(fc);
219 int err;
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400220
David Howellsf2aedb72019-12-10 07:31:13 -0500221 dfprintk(MOUNT, "--> nfs4_try_get_tree()\n");
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400222
David Howellsf2aedb72019-12-10 07:31:13 -0500223 /* We create a mount for the server's root, walk to the requested
224 * location and then create another mount for that.
225 */
Scott Mayhew62a55d02019-12-10 07:31:14 -0500226 err= do_nfs4_mount(nfs4_create_server(fc),
David Howellsf2aedb72019-12-10 07:31:13 -0500227 fc, ctx->nfs_server.hostname,
228 ctx->nfs_server.export_path);
229 if (err) {
Scott Mayhewe6ae1642021-01-05 08:54:32 -0500230 nfs_ferrorf(fc, MOUNT, "NFS4: Couldn't follow remote path");
David Howellsf2aedb72019-12-10 07:31:13 -0500231 dfprintk(MOUNT, "<-- nfs4_try_get_tree() = %d [error]\n", err);
232 } else {
233 dfprintk(MOUNT, "<-- nfs4_try_get_tree() = 0\n");
234 }
235 return err;
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400236}
237
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400238/*
239 * Create an NFS4 server record on referral traversal
240 */
David Howellsf2aedb72019-12-10 07:31:13 -0500241int nfs4_get_referral_tree(struct fs_context *fc)
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400242{
David Howellsf2aedb72019-12-10 07:31:13 -0500243 struct nfs_fs_context *ctx = nfs_fc2context(fc);
244 int err;
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400245
246 dprintk("--> nfs4_referral_mount()\n");
247
David Howellsf2aedb72019-12-10 07:31:13 -0500248 /* create a new volume representation */
Scott Mayhew62a55d02019-12-10 07:31:14 -0500249 err = do_nfs4_mount(nfs4_create_referral_server(fc),
David Howellsf2aedb72019-12-10 07:31:13 -0500250 fc, ctx->nfs_server.hostname,
251 ctx->nfs_server.export_path);
252 if (err) {
Scott Mayhewe6ae1642021-01-05 08:54:32 -0500253 nfs_ferrorf(fc, MOUNT, "NFS4: Couldn't follow remote path");
David Howellsf2aedb72019-12-10 07:31:13 -0500254 dfprintk(MOUNT, "<-- nfs4_get_referral_tree() = %d [error]\n", err);
255 } else {
256 dfprintk(MOUNT, "<-- nfs4_get_referral_tree() = 0\n");
257 }
258 return err;
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400259}
260
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400261static int __init init_nfs_v4(void)
Bryan Schumaker129d1972012-07-16 16:39:13 -0400262{
263 int err;
264
Trond Myklebustc8d74d92013-06-01 11:50:58 -0400265 err = nfs_dns_resolver_init();
Bryan Schumaker129d1972012-07-16 16:39:13 -0400266 if (err)
267 goto out;
268
Trond Myklebustc8d74d92013-06-01 11:50:58 -0400269 err = nfs_idmap_init();
Bryan Schumaker466bfe72012-07-16 16:39:14 -0400270 if (err)
271 goto out1;
272
Frank van der Linden95ad37f2020-06-23 22:39:04 +0000273#ifdef CONFIG_NFS_V4_2
274 err = nfs4_xattr_cache_init();
275 if (err)
276 goto out2;
277#endif
278
Trond Myklebustc8d74d92013-06-01 11:50:58 -0400279 err = nfs4_register_sysctl();
280 if (err)
281 goto out2;
282
Dai Ngo0cfcd402020-10-18 23:42:49 -0400283#ifdef CONFIG_NFS_V4_2
284 nfs42_ssc_register_ops();
285#endif
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400286 register_nfs_version(&nfs_v4);
Bryan Schumaker129d1972012-07-16 16:39:13 -0400287 return 0;
Trond Myklebustc8d74d92013-06-01 11:50:58 -0400288out2:
Bryan Schumaker466bfe72012-07-16 16:39:14 -0400289 nfs_idmap_quit();
Trond Myklebustc8d74d92013-06-01 11:50:58 -0400290out1:
291 nfs_dns_resolver_destroy();
Bryan Schumaker129d1972012-07-16 16:39:13 -0400292out:
293 return err;
294}
295
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400296static void __exit exit_nfs_v4(void)
Bryan Schumaker129d1972012-07-16 16:39:13 -0400297{
Peng Tao5f01d952014-05-30 18:15:59 +0800298 /* Not called in the _init(), conditionally loaded */
299 nfs4_pnfs_v3_ds_connect_unload();
300
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400301 unregister_nfs_version(&nfs_v4);
Frank van der Linden95ad37f2020-06-23 22:39:04 +0000302#ifdef CONFIG_NFS_V4_2
303 nfs4_xattr_cache_exit();
Dai Ngo0cfcd402020-10-18 23:42:49 -0400304 nfs42_ssc_unregister_ops();
Frank van der Linden95ad37f2020-06-23 22:39:04 +0000305#endif
Bryan Schumaker466bfe72012-07-16 16:39:14 -0400306 nfs4_unregister_sysctl();
Bryan Schumaker129d1972012-07-16 16:39:13 -0400307 nfs_idmap_quit();
Trond Myklebustc8d74d92013-06-01 11:50:58 -0400308 nfs_dns_resolver_destroy();
Bryan Schumaker129d1972012-07-16 16:39:13 -0400309}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400310
311MODULE_LICENSE("GPL");
Greg Kroah-Hartmand483eed2020-07-02 12:51:03 +0200312MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400313
314module_init(init_nfs_v4);
315module_exit(exit_nfs_v4);