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 "delegation.h"
13 #include "nfs4idmap.h"
14 #include "dns_resolve.h"
18 #define NFSDBG_FACILITY NFSDBG_VFS
20 static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc);
21 static void nfs4_evict_inode(struct inode *inode);
23 static const struct super_operations nfs4_sops = {
24 .alloc_inode = nfs_alloc_inode,
25 .free_inode = nfs_free_inode,
26 .write_inode = nfs4_write_inode,
27 .drop_inode = nfs_drop_inode,
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,
37 struct nfs_subversion nfs_v4 = {
39 .nfs_fs = &nfs4_fs_type,
40 .rpc_vers = &nfs_version4,
41 .rpc_ops = &nfs_v4_clientops,
43 .xattr = nfs4_xattr_handlers,
46 static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc)
48 int ret = nfs_write_inode(inode, wbc);
51 ret = pnfs_layoutcommit_inode(inode,
52 wbc->sync_mode == WB_SYNC_ALL);
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
61 static void nfs4_evict_inode(struct inode *inode)
63 truncate_inode_pages_final(&inode->i_data);
65 /* If we are holding a delegation, return and free it */
66 nfs_inode_evict_delegation(inode);
67 /* Note that above delegreturn would trigger pnfs return-on-close */
68 pnfs_return_layout(inode);
69 pnfs_destroy_layout(NFS_I(inode));
70 /* First call standard NFS clear_inode() code */
71 nfs_clear_inode(inode);
74 struct nfs_referral_count {
75 struct list_head list;
76 const struct task_struct *task;
77 unsigned int referral_count;
80 static LIST_HEAD(nfs_referral_count_list);
81 static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
83 static struct nfs_referral_count *nfs_find_referral_count(void)
85 struct nfs_referral_count *p;
87 list_for_each_entry(p, &nfs_referral_count_list, list) {
88 if (p->task == current)
94 #define NFS_MAX_NESTED_REFERRALS 2
96 static int nfs_referral_loop_protect(void)
98 struct nfs_referral_count *p, *new;
101 new = kmalloc(sizeof(*new), GFP_KERNEL);
105 new->referral_count = 1;
108 spin_lock(&nfs_referral_count_list_lock);
109 p = nfs_find_referral_count();
111 if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
116 list_add(&new->list, &nfs_referral_count_list);
119 spin_unlock(&nfs_referral_count_list_lock);
125 static void nfs_referral_loop_unprotect(void)
127 struct nfs_referral_count *p;
129 spin_lock(&nfs_referral_count_list_lock);
130 p = nfs_find_referral_count();
132 if (p->referral_count == 0)
136 spin_unlock(&nfs_referral_count_list_lock);
140 static int do_nfs4_mount(struct nfs_server *server,
141 struct fs_context *fc,
142 const char *hostname,
143 const char *export_path)
145 struct nfs_fs_context *root_ctx;
146 struct fs_context *root_fc;
147 struct vfsmount *root_mnt;
148 struct dentry *dentry;
152 struct fs_parameter param = {
154 .type = fs_value_is_string,
159 return PTR_ERR(server);
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);
166 kfree(root_fc->source);
167 root_fc->source = NULL;
169 root_ctx = nfs_fc2context(root_fc);
170 root_ctx->internal = true;
171 root_ctx->server = server;
172 /* We leave export_path unset as it's not used to find the root. */
174 len = strlen(hostname) + 5;
175 param.string = kmalloc(len, GFP_KERNEL);
176 if (param.string == NULL) {
177 put_fs_context(root_fc);
181 /* Does hostname needs to be enclosed in brackets? */
182 if (strchr(hostname, ':'))
183 param.size = snprintf(param.string, len, "[%s]:/", hostname);
185 param.size = snprintf(param.string, len, "%s:/", hostname);
186 ret = vfs_parse_fs_param(root_fc, ¶m);
189 put_fs_context(root_fc);
192 root_mnt = fc_mount(root_fc);
193 put_fs_context(root_fc);
195 if (IS_ERR(root_mnt))
196 return PTR_ERR(root_mnt);
198 ret = nfs_referral_loop_protect();
204 dentry = mount_subtree(root_mnt, export_path);
205 nfs_referral_loop_unprotect();
208 return PTR_ERR(dentry);
214 int nfs4_try_get_tree(struct fs_context *fc)
216 struct nfs_fs_context *ctx = nfs_fc2context(fc);
219 dfprintk(MOUNT, "--> nfs4_try_get_tree()\n");
221 /* We create a mount for the server's root, walk to the requested
222 * location and then create another mount for that.
224 err= do_nfs4_mount(nfs4_create_server(fc),
225 fc, ctx->nfs_server.hostname,
226 ctx->nfs_server.export_path);
228 nfs_errorf(fc, "NFS4: Couldn't follow remote path");
229 dfprintk(MOUNT, "<-- nfs4_try_get_tree() = %d [error]\n", err);
231 dfprintk(MOUNT, "<-- nfs4_try_get_tree() = 0\n");
237 * Create an NFS4 server record on referral traversal
239 int nfs4_get_referral_tree(struct fs_context *fc)
241 struct nfs_fs_context *ctx = nfs_fc2context(fc);
244 dprintk("--> nfs4_referral_mount()\n");
246 /* create a new volume representation */
247 err = do_nfs4_mount(nfs4_create_referral_server(fc),
248 fc, ctx->nfs_server.hostname,
249 ctx->nfs_server.export_path);
251 nfs_errorf(fc, "NFS4: Couldn't follow remote path");
252 dfprintk(MOUNT, "<-- nfs4_get_referral_tree() = %d [error]\n", err);
254 dfprintk(MOUNT, "<-- nfs4_get_referral_tree() = 0\n");
259 static int __init init_nfs_v4(void)
263 err = nfs_dns_resolver_init();
267 err = nfs_idmap_init();
271 err = nfs4_register_sysctl();
275 register_nfs_version(&nfs_v4);
280 nfs_dns_resolver_destroy();
285 static void __exit exit_nfs_v4(void)
287 /* Not called in the _init(), conditionally loaded */
288 nfs4_pnfs_v3_ds_connect_unload();
290 unregister_nfs_version(&nfs_v4);
291 nfs4_unregister_sysctl();
293 nfs_dns_resolver_destroy();
296 MODULE_LICENSE("GPL");
298 module_init(init_nfs_v4);
299 module_exit(exit_nfs_v4);