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_remote_mount(struct file_system_type *fs_type,
22 int flags, const char *dev_name, void *raw_data);
23 static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
24 int flags, const char *dev_name, void *raw_data);
26 static struct file_system_type nfs4_remote_fs_type = {
29 .mount = nfs4_remote_mount,
30 .kill_sb = nfs_kill_super,
31 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
34 struct file_system_type nfs4_referral_fs_type = {
37 .mount = nfs4_referral_mount,
38 .kill_sb = nfs_kill_super,
39 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
42 static const struct super_operations nfs4_sops = {
43 .alloc_inode = nfs_alloc_inode,
44 .free_inode = nfs_free_inode,
45 .write_inode = nfs4_write_inode,
46 .drop_inode = nfs_drop_inode,
48 .evict_inode = nfs4_evict_inode,
49 .umount_begin = nfs_umount_begin,
50 .show_options = nfs_show_options,
51 .show_devname = nfs_show_devname,
52 .show_path = nfs_show_path,
53 .show_stats = nfs_show_stats,
54 .remount_fs = nfs_remount,
57 struct nfs_subversion nfs_v4 = {
59 .nfs_fs = &nfs4_fs_type,
60 .rpc_vers = &nfs_version4,
61 .rpc_ops = &nfs_v4_clientops,
63 .xattr = nfs4_xattr_handlers,
66 static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc)
68 int ret = nfs_write_inode(inode, wbc);
71 ret = pnfs_layoutcommit_inode(inode,
72 wbc->sync_mode == WB_SYNC_ALL);
77 * Clean out any remaining NFSv4 state that might be left over due
78 * to open() calls that passed nfs_atomic_lookup, but failed to call
81 static void nfs4_evict_inode(struct inode *inode)
83 truncate_inode_pages_final(&inode->i_data);
85 /* If we are holding a delegation, return and free it */
86 nfs_inode_evict_delegation(inode);
87 /* Note that above delegreturn would trigger pnfs return-on-close */
88 pnfs_return_layout(inode);
89 pnfs_destroy_layout(NFS_I(inode));
90 /* First call standard NFS clear_inode() code */
91 nfs_clear_inode(inode);
95 * Get the superblock for the NFS4 root partition
97 static struct dentry *
98 nfs4_remote_mount(struct file_system_type *fs_type, int flags,
99 const char *dev_name, void *info)
101 return nfs_fs_mount_common(flags, dev_name, info);
104 struct nfs_referral_count {
105 struct list_head list;
106 const struct task_struct *task;
107 unsigned int referral_count;
110 static LIST_HEAD(nfs_referral_count_list);
111 static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
113 static struct nfs_referral_count *nfs_find_referral_count(void)
115 struct nfs_referral_count *p;
117 list_for_each_entry(p, &nfs_referral_count_list, list) {
118 if (p->task == current)
124 #define NFS_MAX_NESTED_REFERRALS 2
126 static int nfs_referral_loop_protect(void)
128 struct nfs_referral_count *p, *new;
131 new = kmalloc(sizeof(*new), GFP_KERNEL);
135 new->referral_count = 1;
138 spin_lock(&nfs_referral_count_list_lock);
139 p = nfs_find_referral_count();
141 if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
146 list_add(&new->list, &nfs_referral_count_list);
149 spin_unlock(&nfs_referral_count_list_lock);
155 static void nfs_referral_loop_unprotect(void)
157 struct nfs_referral_count *p;
159 spin_lock(&nfs_referral_count_list_lock);
160 p = nfs_find_referral_count();
162 if (p->referral_count == 0)
166 spin_unlock(&nfs_referral_count_list_lock);
170 static struct dentry *do_nfs4_mount(struct nfs_server *server, int flags,
171 struct nfs_mount_info *info,
172 const char *hostname,
173 const char *export_path)
175 struct vfsmount *root_mnt;
176 struct dentry *dentry;
182 return ERR_CAST(server);
184 len = strlen(hostname) + 5;
185 root_devname = kmalloc(len, GFP_KERNEL);
186 if (root_devname == NULL) {
187 nfs_free_server(server);
188 return ERR_PTR(-ENOMEM);
191 /* Does hostname needs to be enclosed in brackets? */
192 if (strchr(hostname, ':'))
193 snprintf(root_devname, len, "[%s]:/", hostname);
195 snprintf(root_devname, len, "%s:/", hostname);
196 info->server = server;
197 root_mnt = vfs_kern_mount(&nfs4_remote_fs_type, flags, root_devname, info);
199 nfs_free_server(info->server);
203 if (IS_ERR(root_mnt))
204 return ERR_CAST(root_mnt);
206 err = nfs_referral_loop_protect();
212 dentry = mount_subtree(root_mnt, export_path);
213 nfs_referral_loop_unprotect();
218 struct dentry *nfs4_try_mount(int flags, const char *dev_name,
219 struct nfs_mount_info *mount_info)
221 struct nfs_parsed_mount_data *data = mount_info->parsed;
224 mount_info->set_security = nfs_set_sb_security;
226 dfprintk(MOUNT, "--> nfs4_try_mount()\n");
228 res = do_nfs4_mount(nfs4_create_server(mount_info, &nfs_v4),
230 data->nfs_server.hostname,
231 data->nfs_server.export_path);
233 dfprintk(MOUNT, "<-- nfs4_try_mount() = %d%s\n",
234 PTR_ERR_OR_ZERO(res),
235 IS_ERR(res) ? " [error]" : "");
240 * Create an NFS4 server record on referral traversal
242 static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
243 int flags, const char *dev_name, void *raw_data)
245 struct nfs_clone_mount *data = raw_data;
246 struct nfs_mount_info mount_info = {
247 .fill_super = nfs_fill_super,
248 .set_security = nfs_clone_sb_security,
254 dprintk("--> nfs4_referral_mount()\n");
256 mount_info.mntfh = nfs_alloc_fhandle();
257 if (!mount_info.mntfh)
258 return ERR_PTR(-ENOMEM);
260 res = do_nfs4_mount(nfs4_create_referral_server(mount_info.cloned,
262 flags, &mount_info, data->hostname, data->mnt_path);
264 dprintk("<-- nfs4_referral_mount() = %d%s\n",
265 PTR_ERR_OR_ZERO(res),
266 IS_ERR(res) ? " [error]" : "");
268 nfs_free_fhandle(mount_info.mntfh);
273 static int __init init_nfs_v4(void)
277 err = nfs_dns_resolver_init();
281 err = nfs_idmap_init();
285 err = nfs4_register_sysctl();
289 register_nfs_version(&nfs_v4);
294 nfs_dns_resolver_destroy();
299 static void __exit exit_nfs_v4(void)
301 /* Not called in the _init(), conditionally loaded */
302 nfs4_pnfs_v3_ds_connect_unload();
304 unregister_nfs_version(&nfs_v4);
305 nfs4_unregister_sysctl();
307 nfs_dns_resolver_destroy();
310 MODULE_LICENSE("GPL");
312 module_init(init_nfs_v4);
313 module_exit(exit_nfs_v4);