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);
25 static struct dentry *nfs4_remote_referral_mount(struct file_system_type *fs_type,
26 int flags, const char *dev_name, void *raw_data);
28 static struct file_system_type nfs4_remote_fs_type = {
31 .mount = nfs4_remote_mount,
32 .kill_sb = nfs_kill_super,
33 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
36 static struct file_system_type nfs4_remote_referral_fs_type = {
39 .mount = nfs4_remote_referral_mount,
40 .kill_sb = nfs_kill_super,
41 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
44 struct file_system_type nfs4_referral_fs_type = {
47 .mount = nfs4_referral_mount,
48 .kill_sb = nfs_kill_super,
49 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
52 static const struct super_operations nfs4_sops = {
53 .alloc_inode = nfs_alloc_inode,
54 .free_inode = nfs_free_inode,
55 .write_inode = nfs4_write_inode,
56 .drop_inode = nfs_drop_inode,
58 .evict_inode = nfs4_evict_inode,
59 .umount_begin = nfs_umount_begin,
60 .show_options = nfs_show_options,
61 .show_devname = nfs_show_devname,
62 .show_path = nfs_show_path,
63 .show_stats = nfs_show_stats,
64 .remount_fs = nfs_remount,
67 struct nfs_subversion nfs_v4 = {
69 .nfs_fs = &nfs4_fs_type,
70 .rpc_vers = &nfs_version4,
71 .rpc_ops = &nfs_v4_clientops,
73 .xattr = nfs4_xattr_handlers,
76 static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc)
78 int ret = nfs_write_inode(inode, wbc);
81 ret = pnfs_layoutcommit_inode(inode,
82 wbc->sync_mode == WB_SYNC_ALL);
87 * Clean out any remaining NFSv4 state that might be left over due
88 * to open() calls that passed nfs_atomic_lookup, but failed to call
91 static void nfs4_evict_inode(struct inode *inode)
93 truncate_inode_pages_final(&inode->i_data);
95 /* If we are holding a delegation, return and free it */
96 nfs_inode_evict_delegation(inode);
97 /* Note that above delegreturn would trigger pnfs return-on-close */
98 pnfs_return_layout(inode);
99 pnfs_destroy_layout(NFS_I(inode));
100 /* First call standard NFS clear_inode() code */
101 nfs_clear_inode(inode);
105 * Get the superblock for the NFS4 root partition
107 static struct dentry *
108 nfs4_remote_mount(struct file_system_type *fs_type, int flags,
109 const char *dev_name, void *info)
111 struct nfs_mount_info *mount_info = info;
112 struct nfs_server *server;
114 mount_info->set_security = nfs_set_sb_security;
116 /* Get a volume representation */
117 server = nfs4_create_server(mount_info, &nfs_v4);
118 return nfs_fs_mount_common(server, flags, dev_name, mount_info, &nfs_v4);
121 static struct vfsmount *nfs_do_root_mount(struct file_system_type *fs_type,
122 int flags, void *data, const char *hostname)
124 struct vfsmount *root_mnt;
128 len = strlen(hostname) + 5;
129 root_devname = kmalloc(len, GFP_KERNEL);
130 if (root_devname == NULL)
131 return ERR_PTR(-ENOMEM);
132 /* Does hostname needs to be enclosed in brackets? */
133 if (strchr(hostname, ':'))
134 snprintf(root_devname, len, "[%s]:/", hostname);
136 snprintf(root_devname, len, "%s:/", hostname);
137 root_mnt = vfs_kern_mount(fs_type, flags, root_devname, data);
142 struct nfs_referral_count {
143 struct list_head list;
144 const struct task_struct *task;
145 unsigned int referral_count;
148 static LIST_HEAD(nfs_referral_count_list);
149 static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
151 static struct nfs_referral_count *nfs_find_referral_count(void)
153 struct nfs_referral_count *p;
155 list_for_each_entry(p, &nfs_referral_count_list, list) {
156 if (p->task == current)
162 #define NFS_MAX_NESTED_REFERRALS 2
164 static int nfs_referral_loop_protect(void)
166 struct nfs_referral_count *p, *new;
169 new = kmalloc(sizeof(*new), GFP_KERNEL);
173 new->referral_count = 1;
176 spin_lock(&nfs_referral_count_list_lock);
177 p = nfs_find_referral_count();
179 if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
184 list_add(&new->list, &nfs_referral_count_list);
187 spin_unlock(&nfs_referral_count_list_lock);
193 static void nfs_referral_loop_unprotect(void)
195 struct nfs_referral_count *p;
197 spin_lock(&nfs_referral_count_list_lock);
198 p = nfs_find_referral_count();
200 if (p->referral_count == 0)
204 spin_unlock(&nfs_referral_count_list_lock);
208 static struct dentry *nfs_follow_remote_path(struct vfsmount *root_mnt,
209 const char *export_path)
211 struct dentry *dentry;
214 if (IS_ERR(root_mnt))
215 return ERR_CAST(root_mnt);
217 err = nfs_referral_loop_protect();
223 dentry = mount_subtree(root_mnt, export_path);
224 nfs_referral_loop_unprotect();
229 struct dentry *nfs4_try_mount(int flags, const char *dev_name,
230 struct nfs_mount_info *mount_info,
231 struct nfs_subversion *nfs_mod)
234 struct vfsmount *root_mnt;
236 struct nfs_parsed_mount_data *data = mount_info->parsed;
238 dfprintk(MOUNT, "--> nfs4_try_mount()\n");
240 export_path = data->nfs_server.export_path;
241 data->nfs_server.export_path = "/";
242 root_mnt = nfs_do_root_mount(&nfs4_remote_fs_type, flags, mount_info,
243 data->nfs_server.hostname);
244 data->nfs_server.export_path = export_path;
246 res = nfs_follow_remote_path(root_mnt, export_path);
248 dfprintk(MOUNT, "<-- nfs4_try_mount() = %d%s\n",
249 PTR_ERR_OR_ZERO(res),
250 IS_ERR(res) ? " [error]" : "");
254 static struct dentry *
255 nfs4_remote_referral_mount(struct file_system_type *fs_type, int flags,
256 const char *dev_name, void *raw_data)
258 struct nfs_mount_info mount_info = {
259 .fill_super = nfs_fill_super,
260 .set_security = nfs_clone_sb_security,
263 struct nfs_server *server;
264 struct dentry *mntroot = ERR_PTR(-ENOMEM);
266 dprintk("--> nfs4_referral_get_sb()\n");
268 mount_info.mntfh = nfs_alloc_fhandle();
269 if (mount_info.cloned == NULL || mount_info.mntfh == NULL)
272 /* create a new volume representation */
273 server = nfs4_create_referral_server(mount_info.cloned, mount_info.mntfh);
274 mntroot = nfs_fs_mount_common(server, flags, dev_name, &mount_info, &nfs_v4);
276 nfs_free_fhandle(mount_info.mntfh);
281 * Create an NFS4 server record on referral traversal
283 static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
284 int flags, const char *dev_name, void *raw_data)
286 struct nfs_clone_mount *data = raw_data;
288 struct vfsmount *root_mnt;
291 dprintk("--> nfs4_referral_mount()\n");
293 export_path = data->mnt_path;
294 data->mnt_path = "/";
296 root_mnt = nfs_do_root_mount(&nfs4_remote_referral_fs_type,
297 flags, data, data->hostname);
298 data->mnt_path = export_path;
300 res = nfs_follow_remote_path(root_mnt, export_path);
301 dprintk("<-- nfs4_referral_mount() = %d%s\n",
302 PTR_ERR_OR_ZERO(res),
303 IS_ERR(res) ? " [error]" : "");
308 static int __init init_nfs_v4(void)
312 err = nfs_dns_resolver_init();
316 err = nfs_idmap_init();
320 err = nfs4_register_sysctl();
324 register_nfs_version(&nfs_v4);
329 nfs_dns_resolver_destroy();
334 static void __exit exit_nfs_v4(void)
336 /* Not called in the _init(), conditionally loaded */
337 nfs4_pnfs_v3_ds_connect_unload();
339 unregister_nfs_version(&nfs_v4);
340 nfs4_unregister_sysctl();
342 nfs_dns_resolver_destroy();
345 MODULE_LICENSE("GPL");
347 module_init(init_nfs_v4);
348 module_exit(exit_nfs_v4);