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, &nfs_v4);
104 static struct vfsmount *nfs_do_root_mount(struct nfs_server *server, int flags,
105 struct nfs_mount_info *info,
106 const char *hostname)
108 struct vfsmount *root_mnt;
113 return ERR_CAST(server);
115 len = strlen(hostname) + 5;
116 root_devname = kmalloc(len, GFP_KERNEL);
117 if (root_devname == NULL) {
118 nfs_free_server(server);
119 return ERR_PTR(-ENOMEM);
121 /* Does hostname needs to be enclosed in brackets? */
122 if (strchr(hostname, ':'))
123 snprintf(root_devname, len, "[%s]:/", hostname);
125 snprintf(root_devname, len, "%s:/", hostname);
126 info->server = server;
127 root_mnt = vfs_kern_mount(&nfs4_remote_fs_type, flags, root_devname, info);
129 nfs_free_server(info->server);
135 struct nfs_referral_count {
136 struct list_head list;
137 const struct task_struct *task;
138 unsigned int referral_count;
141 static LIST_HEAD(nfs_referral_count_list);
142 static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
144 static struct nfs_referral_count *nfs_find_referral_count(void)
146 struct nfs_referral_count *p;
148 list_for_each_entry(p, &nfs_referral_count_list, list) {
149 if (p->task == current)
155 #define NFS_MAX_NESTED_REFERRALS 2
157 static int nfs_referral_loop_protect(void)
159 struct nfs_referral_count *p, *new;
162 new = kmalloc(sizeof(*new), GFP_KERNEL);
166 new->referral_count = 1;
169 spin_lock(&nfs_referral_count_list_lock);
170 p = nfs_find_referral_count();
172 if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
177 list_add(&new->list, &nfs_referral_count_list);
180 spin_unlock(&nfs_referral_count_list_lock);
186 static void nfs_referral_loop_unprotect(void)
188 struct nfs_referral_count *p;
190 spin_lock(&nfs_referral_count_list_lock);
191 p = nfs_find_referral_count();
193 if (p->referral_count == 0)
197 spin_unlock(&nfs_referral_count_list_lock);
201 static struct dentry *nfs_follow_remote_path(struct vfsmount *root_mnt,
202 const char *export_path)
204 struct dentry *dentry;
207 if (IS_ERR(root_mnt))
208 return ERR_CAST(root_mnt);
210 err = nfs_referral_loop_protect();
216 dentry = mount_subtree(root_mnt, export_path);
217 nfs_referral_loop_unprotect();
222 struct dentry *nfs4_try_mount(int flags, const char *dev_name,
223 struct nfs_mount_info *mount_info,
224 struct nfs_subversion *nfs_mod)
227 struct vfsmount *root_mnt;
229 struct nfs_parsed_mount_data *data = mount_info->parsed;
231 mount_info->set_security = nfs_set_sb_security;
233 dfprintk(MOUNT, "--> nfs4_try_mount()\n");
235 export_path = data->nfs_server.export_path;
236 root_mnt = nfs_do_root_mount(
237 nfs4_create_server(mount_info, &nfs_v4),
239 data->nfs_server.hostname);
241 res = nfs_follow_remote_path(root_mnt, export_path);
243 dfprintk(MOUNT, "<-- nfs4_try_mount() = %d%s\n",
244 PTR_ERR_OR_ZERO(res),
245 IS_ERR(res) ? " [error]" : "");
250 * Create an NFS4 server record on referral traversal
252 static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
253 int flags, const char *dev_name, void *raw_data)
255 struct nfs_clone_mount *data = raw_data;
256 struct nfs_mount_info mount_info = {
257 .fill_super = nfs_fill_super,
258 .set_security = nfs_clone_sb_security,
262 struct vfsmount *root_mnt;
265 dprintk("--> nfs4_referral_mount()\n");
267 mount_info.mntfh = nfs_alloc_fhandle();
268 if (!mount_info.mntfh)
269 return ERR_PTR(-ENOMEM);
271 export_path = data->mnt_path;
272 root_mnt = nfs_do_root_mount(
273 nfs4_create_referral_server(mount_info.cloned,
275 flags, &mount_info, data->hostname);
277 res = nfs_follow_remote_path(root_mnt, export_path);
278 dprintk("<-- nfs4_referral_mount() = %d%s\n",
279 PTR_ERR_OR_ZERO(res),
280 IS_ERR(res) ? " [error]" : "");
282 nfs_free_fhandle(mount_info.mntfh);
287 static int __init init_nfs_v4(void)
291 err = nfs_dns_resolver_init();
295 err = nfs_idmap_init();
299 err = nfs4_register_sysctl();
303 register_nfs_version(&nfs_v4);
308 nfs_dns_resolver_destroy();
313 static void __exit exit_nfs_v4(void)
315 /* Not called in the _init(), conditionally loaded */
316 nfs4_pnfs_v3_ds_connect_unload();
318 unregister_nfs_version(&nfs_v4);
319 nfs4_unregister_sysctl();
321 nfs_dns_resolver_destroy();
324 MODULE_LICENSE("GPL");
326 module_init(init_nfs_v4);
327 module_exit(exit_nfs_v4);