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/nfs4_mount.h>
8 #include <linux/nfs_fs.h>
9 #include "delegation.h"
12 #include "nfs4idmap.h"
13 #include "dns_resolve.h"
17 #define NFSDBG_FACILITY NFSDBG_VFS
19 static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc);
20 static void nfs4_evict_inode(struct inode *inode);
21 static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
22 int flags, const char *dev_name, void *raw_data);
24 struct file_system_type nfs4_referral_fs_type = {
27 .mount = nfs4_referral_mount,
28 .kill_sb = nfs_kill_super,
29 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
32 static const struct super_operations nfs4_sops = {
33 .alloc_inode = nfs_alloc_inode,
34 .free_inode = nfs_free_inode,
35 .write_inode = nfs4_write_inode,
36 .drop_inode = nfs_drop_inode,
38 .evict_inode = nfs4_evict_inode,
39 .umount_begin = nfs_umount_begin,
40 .show_options = nfs_show_options,
41 .show_devname = nfs_show_devname,
42 .show_path = nfs_show_path,
43 .show_stats = nfs_show_stats,
44 .remount_fs = nfs_remount,
47 struct nfs_subversion nfs_v4 = {
49 .nfs_fs = &nfs4_fs_type,
50 .rpc_vers = &nfs_version4,
51 .rpc_ops = &nfs_v4_clientops,
53 .xattr = nfs4_xattr_handlers,
56 static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc)
58 int ret = nfs_write_inode(inode, wbc);
61 ret = pnfs_layoutcommit_inode(inode,
62 wbc->sync_mode == WB_SYNC_ALL);
67 * Clean out any remaining NFSv4 state that might be left over due
68 * to open() calls that passed nfs_atomic_lookup, but failed to call
71 static void nfs4_evict_inode(struct inode *inode)
73 truncate_inode_pages_final(&inode->i_data);
75 /* If we are holding a delegation, return and free it */
76 nfs_inode_evict_delegation(inode);
77 /* Note that above delegreturn would trigger pnfs return-on-close */
78 pnfs_return_layout(inode);
79 pnfs_destroy_layout(NFS_I(inode));
80 /* First call standard NFS clear_inode() code */
81 nfs_clear_inode(inode);
84 struct nfs_referral_count {
85 struct list_head list;
86 const struct task_struct *task;
87 unsigned int referral_count;
90 static LIST_HEAD(nfs_referral_count_list);
91 static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
93 static struct nfs_referral_count *nfs_find_referral_count(void)
95 struct nfs_referral_count *p;
97 list_for_each_entry(p, &nfs_referral_count_list, list) {
98 if (p->task == current)
104 #define NFS_MAX_NESTED_REFERRALS 2
106 static int nfs_referral_loop_protect(void)
108 struct nfs_referral_count *p, *new;
111 new = kmalloc(sizeof(*new), GFP_KERNEL);
115 new->referral_count = 1;
118 spin_lock(&nfs_referral_count_list_lock);
119 p = nfs_find_referral_count();
121 if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
126 list_add(&new->list, &nfs_referral_count_list);
129 spin_unlock(&nfs_referral_count_list_lock);
135 static void nfs_referral_loop_unprotect(void)
137 struct nfs_referral_count *p;
139 spin_lock(&nfs_referral_count_list_lock);
140 p = nfs_find_referral_count();
142 if (p->referral_count == 0)
146 spin_unlock(&nfs_referral_count_list_lock);
150 static struct dentry *do_nfs4_mount(struct nfs_server *server, int flags,
151 struct nfs_mount_info *info,
152 const char *hostname,
153 const char *export_path)
155 struct vfsmount *root_mnt;
156 struct dentry *dentry;
162 return ERR_CAST(server);
164 len = strlen(hostname) + 5;
165 root_devname = kmalloc(len, GFP_KERNEL);
166 if (root_devname == NULL) {
167 nfs_free_server(server);
168 return ERR_PTR(-ENOMEM);
171 /* Does hostname needs to be enclosed in brackets? */
172 if (strchr(hostname, ':'))
173 snprintf(root_devname, len, "[%s]:/", hostname);
175 snprintf(root_devname, len, "%s:/", hostname);
176 info->server = server;
177 root_mnt = vfs_kern_mount(&nfs_prepared_fs_type, flags, root_devname, info);
179 nfs_free_server(info->server);
183 if (IS_ERR(root_mnt))
184 return ERR_CAST(root_mnt);
186 err = nfs_referral_loop_protect();
192 dentry = mount_subtree(root_mnt, export_path);
193 nfs_referral_loop_unprotect();
198 struct dentry *nfs4_try_mount(int flags, const char *dev_name,
199 struct nfs_mount_info *mount_info)
201 struct nfs_parsed_mount_data *data = mount_info->parsed;
204 mount_info->set_security = nfs_set_sb_security;
206 dfprintk(MOUNT, "--> nfs4_try_mount()\n");
208 res = do_nfs4_mount(nfs4_create_server(mount_info),
210 data->nfs_server.hostname,
211 data->nfs_server.export_path);
213 dfprintk(MOUNT, "<-- nfs4_try_mount() = %d%s\n",
214 PTR_ERR_OR_ZERO(res),
215 IS_ERR(res) ? " [error]" : "");
220 * Create an NFS4 server record on referral traversal
222 static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
223 int flags, const char *dev_name, void *raw_data)
225 struct nfs_clone_mount *data = raw_data;
226 struct nfs_mount_info mount_info = {
227 .fill_super = nfs_fill_super,
228 .set_security = nfs_clone_sb_security,
234 dprintk("--> nfs4_referral_mount()\n");
236 mount_info.mntfh = nfs_alloc_fhandle();
237 if (!mount_info.mntfh)
238 return ERR_PTR(-ENOMEM);
240 res = do_nfs4_mount(nfs4_create_referral_server(mount_info.cloned,
242 flags, &mount_info, data->hostname, data->mnt_path);
244 dprintk("<-- nfs4_referral_mount() = %d%s\n",
245 PTR_ERR_OR_ZERO(res),
246 IS_ERR(res) ? " [error]" : "");
248 nfs_free_fhandle(mount_info.mntfh);
253 static int __init init_nfs_v4(void)
257 err = nfs_dns_resolver_init();
261 err = nfs_idmap_init();
265 err = nfs4_register_sysctl();
269 register_nfs_version(&nfs_v4);
274 nfs_dns_resolver_destroy();
279 static void __exit exit_nfs_v4(void)
281 /* Not called in the _init(), conditionally loaded */
282 nfs4_pnfs_v3_ds_connect_unload();
284 unregister_nfs_version(&nfs_v4);
285 nfs4_unregister_sysctl();
287 nfs_dns_resolver_destroy();
290 MODULE_LICENSE("GPL");
292 module_init(init_nfs_v4);
293 module_exit(exit_nfs_v4);