1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012 Bryan Schumaker <bjschuma@netapp.com>
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"
14 #include "nfs4idmap.h"
15 #include "dns_resolve.h"
19 #define NFSDBG_FACILITY NFSDBG_VFS
21 static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc);
22 static void nfs4_evict_inode(struct inode *inode);
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,
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,
38 struct nfs_subversion nfs_v4 = {
40 .nfs_fs = &nfs4_fs_type,
41 .rpc_vers = &nfs_version4,
42 .rpc_ops = &nfs_v4_clientops,
44 .xattr = nfs4_xattr_handlers,
47 static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc)
49 int ret = nfs_write_inode(inode, wbc);
52 ret = pnfs_layoutcommit_inode(inode,
53 wbc->sync_mode == WB_SYNC_ALL);
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
62 static void nfs4_evict_inode(struct inode *inode)
64 truncate_inode_pages_final(&inode->i_data);
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);
76 struct nfs_referral_count {
77 struct list_head list;
78 const struct task_struct *task;
79 unsigned int referral_count;
82 static LIST_HEAD(nfs_referral_count_list);
83 static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
85 static struct nfs_referral_count *nfs_find_referral_count(void)
87 struct nfs_referral_count *p;
89 list_for_each_entry(p, &nfs_referral_count_list, list) {
90 if (p->task == current)
96 #define NFS_MAX_NESTED_REFERRALS 2
98 static int nfs_referral_loop_protect(void)
100 struct nfs_referral_count *p, *new;
103 new = kmalloc(sizeof(*new), GFP_KERNEL);
107 new->referral_count = 1;
110 spin_lock(&nfs_referral_count_list_lock);
111 p = nfs_find_referral_count();
113 if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
118 list_add(&new->list, &nfs_referral_count_list);
121 spin_unlock(&nfs_referral_count_list_lock);
127 static void nfs_referral_loop_unprotect(void)
129 struct nfs_referral_count *p;
131 spin_lock(&nfs_referral_count_list_lock);
132 p = nfs_find_referral_count();
134 if (p->referral_count == 0)
138 spin_unlock(&nfs_referral_count_list_lock);
142 static int do_nfs4_mount(struct nfs_server *server,
143 struct fs_context *fc,
144 const char *hostname,
145 const char *export_path)
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;
155 struct fs_parameter param = {
157 .type = fs_value_is_string,
161 struct fs_parameter param_fsc = {
163 .type = fs_value_is_string,
168 return PTR_ERR(server);
170 root_fc = vfs_dup_fs_context(fc);
171 if (IS_ERR(root_fc)) {
172 nfs_free_server(server);
173 return PTR_ERR(root_fc);
175 kfree(root_fc->source);
176 root_fc->source = NULL;
178 ctx = nfs_fc2context(fc);
179 root_ctx = nfs_fc2context(root_fc);
180 root_ctx->internal = true;
181 root_ctx->server = server;
183 if (ctx->fscache_uniq) {
184 len = strlen(ctx->fscache_uniq);
185 param_fsc.size = len;
186 param_fsc.string = kmemdup_nul(ctx->fscache_uniq, len, GFP_KERNEL);
187 if (param_fsc.string == NULL) {
188 put_fs_context(root_fc);
191 ret = vfs_parse_fs_param(root_fc, ¶m_fsc);
192 kfree(param_fsc.string);
194 put_fs_context(root_fc);
198 /* We leave export_path unset as it's not used to find the root. */
200 len = strlen(hostname) + 5;
201 param.string = kmalloc(len, GFP_KERNEL);
202 if (param.string == NULL) {
203 put_fs_context(root_fc);
207 /* Does hostname needs to be enclosed in brackets? */
208 if (strchr(hostname, ':'))
209 param.size = snprintf(param.string, len, "[%s]:/", hostname);
211 param.size = snprintf(param.string, len, "%s:/", hostname);
212 ret = vfs_parse_fs_param(root_fc, ¶m);
215 put_fs_context(root_fc);
218 root_mnt = fc_mount(root_fc);
219 put_fs_context(root_fc);
221 if (IS_ERR(root_mnt))
222 return PTR_ERR(root_mnt);
224 ret = nfs_referral_loop_protect();
230 dentry = mount_subtree(root_mnt, export_path);
231 nfs_referral_loop_unprotect();
234 return PTR_ERR(dentry);
240 int nfs4_try_get_tree(struct fs_context *fc)
242 struct nfs_fs_context *ctx = nfs_fc2context(fc);
245 dfprintk(MOUNT, "--> nfs4_try_get_tree()\n");
247 /* We create a mount for the server's root, walk to the requested
248 * location and then create another mount for that.
250 err= do_nfs4_mount(nfs4_create_server(fc),
251 fc, ctx->nfs_server.hostname,
252 ctx->nfs_server.export_path);
254 nfs_ferrorf(fc, MOUNT, "NFS4: Couldn't follow remote path");
255 dfprintk(MOUNT, "<-- nfs4_try_get_tree() = %d [error]\n", err);
257 dfprintk(MOUNT, "<-- nfs4_try_get_tree() = 0\n");
263 * Create an NFS4 server record on referral traversal
265 int nfs4_get_referral_tree(struct fs_context *fc)
267 struct nfs_fs_context *ctx = nfs_fc2context(fc);
270 dprintk("--> nfs4_referral_mount()\n");
272 /* create a new volume representation */
273 err = do_nfs4_mount(nfs4_create_referral_server(fc),
274 fc, ctx->nfs_server.hostname,
275 ctx->nfs_server.export_path);
277 nfs_ferrorf(fc, MOUNT, "NFS4: Couldn't follow remote path");
278 dfprintk(MOUNT, "<-- nfs4_get_referral_tree() = %d [error]\n", err);
280 dfprintk(MOUNT, "<-- nfs4_get_referral_tree() = 0\n");
285 static int __init init_nfs_v4(void)
289 err = nfs_dns_resolver_init();
293 err = nfs_idmap_init();
297 #ifdef CONFIG_NFS_V4_2
298 err = nfs4_xattr_cache_init();
303 err = nfs4_register_sysctl();
307 #ifdef CONFIG_NFS_V4_2
308 nfs42_ssc_register_ops();
310 register_nfs_version(&nfs_v4);
315 nfs_dns_resolver_destroy();
320 static void __exit exit_nfs_v4(void)
322 /* Not called in the _init(), conditionally loaded */
323 nfs4_pnfs_v3_ds_connect_unload();
325 unregister_nfs_version(&nfs_v4);
326 #ifdef CONFIG_NFS_V4_2
327 nfs4_xattr_cache_exit();
328 nfs42_ssc_unregister_ops();
330 nfs4_unregister_sysctl();
332 nfs_dns_resolver_destroy();
335 MODULE_LICENSE("GPL");
337 module_init(init_nfs_v4);
338 module_exit(exit_nfs_v4);