blob: c489942b90695f908176d6130b95f20f162f2c38 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Bryan Schumaker129d1972012-07-16 16:39:13 -04002/*
3 * Copyright (c) 2012 Bryan Schumaker <bjschuma@netapp.com>
4 */
5#include <linux/init.h>
Bryan Schumakerfbdefd62012-07-16 16:39:20 -04006#include <linux/module.h>
Bryan Schumakerfbdefd62012-07-16 16:39:20 -04007#include <linux/nfs4_mount.h>
Bryan Schumaker466bfe72012-07-16 16:39:14 -04008#include <linux/nfs_fs.h>
Bryan Schumaker19d87ca2012-07-30 16:05:21 -04009#include "delegation.h"
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040010#include "internal.h"
Bryan Schumaker466bfe72012-07-16 16:39:14 -040011#include "nfs4_fs.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040012#include "nfs4idmap.h"
Trond Myklebustc8d74d92013-06-01 11:50:58 -040013#include "dns_resolve.h"
Bryan Schumaker19d87ca2012-07-30 16:05:21 -040014#include "pnfs.h"
Bryan Schumakerab7017a2012-07-30 16:05:16 -040015#include "nfs.h"
Bryan Schumaker129d1972012-07-16 16:39:13 -040016
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040017#define NFSDBG_FACILITY NFSDBG_VFS
18
Bryan Schumaker19d87ca2012-07-30 16:05:21 -040019static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc);
20static void nfs4_evict_inode(struct inode *inode);
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040021static struct dentry *nfs4_remote_mount(struct file_system_type *fs_type,
22 int flags, const char *dev_name, void *raw_data);
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040023static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
24 int flags, const char *dev_name, void *raw_data);
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040025
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040026static struct file_system_type nfs4_remote_fs_type = {
27 .owner = THIS_MODULE,
28 .name = "nfs4",
29 .mount = nfs4_remote_mount,
30 .kill_sb = nfs_kill_super,
Jeff Laytonecf3d1f2013-02-20 11:19:05 -050031 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040032};
33
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040034struct file_system_type nfs4_referral_fs_type = {
35 .owner = THIS_MODULE,
36 .name = "nfs4",
37 .mount = nfs4_referral_mount,
38 .kill_sb = nfs_kill_super,
Jeff Laytonecf3d1f2013-02-20 11:19:05 -050039 .fs_flags = FS_RENAME_DOES_D_MOVE|FS_BINARY_MOUNTDATA,
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040040};
41
42static const struct super_operations nfs4_sops = {
43 .alloc_inode = nfs_alloc_inode,
Al Viroca1a1992019-04-15 20:19:40 -040044 .free_inode = nfs_free_inode,
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040045 .write_inode = nfs4_write_inode,
Trond Myklebusteed99352012-12-14 14:36:36 -050046 .drop_inode = nfs_drop_inode,
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040047 .statfs = nfs_statfs,
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,
55};
56
Bryan Schumakerab7017a2012-07-30 16:05:16 -040057struct nfs_subversion nfs_v4 = {
58 .owner = THIS_MODULE,
59 .nfs_fs = &nfs4_fs_type,
60 .rpc_vers = &nfs_version4,
61 .rpc_ops = &nfs_v4_clientops,
Bryan Schumaker6a744902012-07-30 16:05:20 -040062 .sops = &nfs4_sops,
63 .xattr = nfs4_xattr_handlers,
Bryan Schumakerab7017a2012-07-30 16:05:16 -040064};
65
Bryan Schumaker19d87ca2012-07-30 16:05:21 -040066static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc)
67{
68 int ret = nfs_write_inode(inode, wbc);
69
Trond Myklebust71244d92014-01-13 13:34:36 -050070 if (ret == 0)
71 ret = pnfs_layoutcommit_inode(inode,
72 wbc->sync_mode == WB_SYNC_ALL);
Bryan Schumaker19d87ca2012-07-30 16:05:21 -040073 return ret;
74}
75
76/*
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
79 * nfs_open().
80 */
81static void nfs4_evict_inode(struct inode *inode)
82{
Johannes Weiner91b0abe2014-04-03 14:47:49 -070083 truncate_inode_pages_final(&inode->i_data);
Bryan Schumaker19d87ca2012-07-30 16:05:21 -040084 clear_inode(inode);
Trond Myklebustb47e0e42019-10-21 14:04:00 -040085 /* If we are holding a delegation, return and free it */
86 nfs_inode_evict_delegation(inode);
Trond Myklebust415320f2015-03-25 13:26:18 -040087 /* Note that above delegreturn would trigger pnfs return-on-close */
88 pnfs_return_layout(inode);
89 pnfs_destroy_layout(NFS_I(inode));
Bryan Schumaker19d87ca2012-07-30 16:05:21 -040090 /* First call standard NFS clear_inode() code */
91 nfs_clear_inode(inode);
92}
93
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040094/*
Bryan Schumakerfbdefd62012-07-16 16:39:20 -040095 * Get the superblock for the NFS4 root partition
96 */
97static struct dentry *
98nfs4_remote_mount(struct file_system_type *fs_type, int flags,
99 const char *dev_name, void *info)
100{
Al Viro7643c122019-12-10 07:30:51 -0500101 return nfs_fs_mount_common(flags, dev_name, info, &nfs_v4);
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400102}
103
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400104struct nfs_referral_count {
105 struct list_head list;
106 const struct task_struct *task;
107 unsigned int referral_count;
108};
109
110static LIST_HEAD(nfs_referral_count_list);
111static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
112
113static struct nfs_referral_count *nfs_find_referral_count(void)
114{
115 struct nfs_referral_count *p;
116
117 list_for_each_entry(p, &nfs_referral_count_list, list) {
118 if (p->task == current)
119 return p;
120 }
121 return NULL;
122}
123
124#define NFS_MAX_NESTED_REFERRALS 2
125
126static int nfs_referral_loop_protect(void)
127{
128 struct nfs_referral_count *p, *new;
129 int ret = -ENOMEM;
130
131 new = kmalloc(sizeof(*new), GFP_KERNEL);
132 if (!new)
133 goto out;
134 new->task = current;
135 new->referral_count = 1;
136
137 ret = 0;
138 spin_lock(&nfs_referral_count_list_lock);
139 p = nfs_find_referral_count();
140 if (p != NULL) {
141 if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
142 ret = -ELOOP;
143 else
144 p->referral_count++;
145 } else {
146 list_add(&new->list, &nfs_referral_count_list);
147 new = NULL;
148 }
149 spin_unlock(&nfs_referral_count_list_lock);
150 kfree(new);
151out:
152 return ret;
153}
154
155static void nfs_referral_loop_unprotect(void)
156{
157 struct nfs_referral_count *p;
158
159 spin_lock(&nfs_referral_count_list_lock);
160 p = nfs_find_referral_count();
161 p->referral_count--;
162 if (p->referral_count == 0)
163 list_del(&p->list);
164 else
165 p = NULL;
166 spin_unlock(&nfs_referral_count_list_lock);
167 kfree(p);
168}
169
Al Viro4e357762019-12-10 07:30:54 -0500170static 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)
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400174{
Al Viro4e357762019-12-10 07:30:54 -0500175 struct vfsmount *root_mnt;
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400176 struct dentry *dentry;
Al Viro4e357762019-12-10 07:30:54 -0500177 char *root_devname;
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400178 int err;
Al Viro4e357762019-12-10 07:30:54 -0500179 size_t len;
180
181 if (IS_ERR(server))
182 return ERR_CAST(server);
183
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);
189 }
190
191 /* Does hostname needs to be enclosed in brackets? */
192 if (strchr(hostname, ':'))
193 snprintf(root_devname, len, "[%s]:/", hostname);
194 else
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);
198 if (info->server)
199 nfs_free_server(info->server);
200 info->server = NULL;
201 kfree(root_devname);
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400202
203 if (IS_ERR(root_mnt))
204 return ERR_CAST(root_mnt);
205
206 err = nfs_referral_loop_protect();
207 if (err) {
208 mntput(root_mnt);
209 return ERR_PTR(err);
210 }
211
212 dentry = mount_subtree(root_mnt, export_path);
213 nfs_referral_loop_unprotect();
214
215 return dentry;
216}
217
218struct dentry *nfs4_try_mount(int flags, const char *dev_name,
Bryan Schumakerff9099f22012-07-30 16:05:18 -0400219 struct nfs_mount_info *mount_info,
220 struct nfs_subversion *nfs_mod)
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400221{
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400222 struct nfs_parsed_mount_data *data = mount_info->parsed;
Al Viro4e357762019-12-10 07:30:54 -0500223 struct dentry *res;
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400224
Al Viro7643c122019-12-10 07:30:51 -0500225 mount_info->set_security = nfs_set_sb_security;
226
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400227 dfprintk(MOUNT, "--> nfs4_try_mount()\n");
228
Al Viro4e357762019-12-10 07:30:54 -0500229 res = do_nfs4_mount(nfs4_create_server(mount_info, &nfs_v4),
230 flags, mount_info,
231 data->nfs_server.hostname,
232 data->nfs_server.export_path);
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400233
Geyslan G. Bem67062462013-10-14 17:24:16 -0300234 dfprintk(MOUNT, "<-- nfs4_try_mount() = %d%s\n",
235 PTR_ERR_OR_ZERO(res),
236 IS_ERR(res) ? " [error]" : "");
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400237 return res;
238}
239
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400240/*
241 * Create an NFS4 server record on referral traversal
242 */
243static struct dentry *nfs4_referral_mount(struct file_system_type *fs_type,
244 int flags, const char *dev_name, void *raw_data)
245{
246 struct nfs_clone_mount *data = raw_data;
Al Viro7643c122019-12-10 07:30:51 -0500247 struct nfs_mount_info mount_info = {
248 .fill_super = nfs_fill_super,
249 .set_security = nfs_clone_sb_security,
250 .cloned = data,
251 };
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400252 struct dentry *res;
253
254 dprintk("--> nfs4_referral_mount()\n");
255
Al Viro7643c122019-12-10 07:30:51 -0500256 mount_info.mntfh = nfs_alloc_fhandle();
257 if (!mount_info.mntfh)
258 return ERR_PTR(-ENOMEM);
259
Al Viro4e357762019-12-10 07:30:54 -0500260 res = do_nfs4_mount(nfs4_create_referral_server(mount_info.cloned,
261 mount_info.mntfh),
262 flags, &mount_info, data->hostname, data->mnt_path);
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400263
Geyslan G. Bem67062462013-10-14 17:24:16 -0300264 dprintk("<-- nfs4_referral_mount() = %d%s\n",
265 PTR_ERR_OR_ZERO(res),
266 IS_ERR(res) ? " [error]" : "");
Al Viro7643c122019-12-10 07:30:51 -0500267
268 nfs_free_fhandle(mount_info.mntfh);
Bryan Schumakerfbdefd62012-07-16 16:39:20 -0400269 return res;
270}
271
272
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400273static int __init init_nfs_v4(void)
Bryan Schumaker129d1972012-07-16 16:39:13 -0400274{
275 int err;
276
Trond Myklebustc8d74d92013-06-01 11:50:58 -0400277 err = nfs_dns_resolver_init();
Bryan Schumaker129d1972012-07-16 16:39:13 -0400278 if (err)
279 goto out;
280
Trond Myklebustc8d74d92013-06-01 11:50:58 -0400281 err = nfs_idmap_init();
Bryan Schumaker466bfe72012-07-16 16:39:14 -0400282 if (err)
283 goto out1;
284
Trond Myklebustc8d74d92013-06-01 11:50:58 -0400285 err = nfs4_register_sysctl();
286 if (err)
287 goto out2;
288
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400289 register_nfs_version(&nfs_v4);
Bryan Schumaker129d1972012-07-16 16:39:13 -0400290 return 0;
Trond Myklebustc8d74d92013-06-01 11:50:58 -0400291out2:
Bryan Schumaker466bfe72012-07-16 16:39:14 -0400292 nfs_idmap_quit();
Trond Myklebustc8d74d92013-06-01 11:50:58 -0400293out1:
294 nfs_dns_resolver_destroy();
Bryan Schumaker129d1972012-07-16 16:39:13 -0400295out:
296 return err;
297}
298
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400299static void __exit exit_nfs_v4(void)
Bryan Schumaker129d1972012-07-16 16:39:13 -0400300{
Peng Tao5f01d952014-05-30 18:15:59 +0800301 /* Not called in the _init(), conditionally loaded */
302 nfs4_pnfs_v3_ds_connect_unload();
303
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400304 unregister_nfs_version(&nfs_v4);
Bryan Schumaker466bfe72012-07-16 16:39:14 -0400305 nfs4_unregister_sysctl();
Bryan Schumaker129d1972012-07-16 16:39:13 -0400306 nfs_idmap_quit();
Trond Myklebustc8d74d92013-06-01 11:50:58 -0400307 nfs_dns_resolver_destroy();
Bryan Schumaker129d1972012-07-16 16:39:13 -0400308}
Bryan Schumaker89d77c82012-07-30 16:05:25 -0400309
310MODULE_LICENSE("GPL");
311
312module_init(init_nfs_v4);
313module_exit(exit_nfs_v4);