Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (c) 2012 Bryan Schumaker <bjschuma@netapp.com>
4 */
5#include <linux/init.h>
6#include <linux/module.h>
7#include <linux/mount.h>
8#include <linux/nfs4_mount.h>
9#include <linux/nfs_fs.h>
10#include <linux/nfs_ssc.h>
11#include "delegation.h"
12#include "internal.h"
13#include "nfs4_fs.h"
14#include "nfs4idmap.h"
15#include "dns_resolve.h"
16#include "pnfs.h"
17#include "nfs.h"
18
19#define NFSDBG_FACILITY NFSDBG_VFS
20
21static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc);
22static void nfs4_evict_inode(struct inode *inode);
23
24static const struct super_operations nfs4_sops = {
25 .alloc_inode = nfs_alloc_inode,
26 .free_inode = nfs_free_inode,
27 .write_inode = nfs4_write_inode,
28 .drop_inode = nfs_drop_inode,
29 .statfs = nfs_statfs,
30 .evict_inode = nfs4_evict_inode,
31 .umount_begin = nfs_umount_begin,
32 .show_options = nfs_show_options,
33 .show_devname = nfs_show_devname,
34 .show_path = nfs_show_path,
35 .show_stats = nfs_show_stats,
36};
37
38struct nfs_subversion nfs_v4 = {
39 .owner = THIS_MODULE,
40 .nfs_fs = &nfs4_fs_type,
41 .rpc_vers = &nfs_version4,
42 .rpc_ops = &nfs_v4_clientops,
43 .sops = &nfs4_sops,
44 .xattr = nfs4_xattr_handlers,
45};
46
47static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc)
48{
49 int ret = nfs_write_inode(inode, wbc);
50
51 if (ret == 0)
52 ret = pnfs_layoutcommit_inode(inode,
53 wbc->sync_mode == WB_SYNC_ALL);
54 return ret;
55}
56
57/*
58 * Clean out any remaining NFSv4 state that might be left over due
59 * to open() calls that passed nfs_atomic_lookup, but failed to call
60 * nfs_open().
61 */
62static void nfs4_evict_inode(struct inode *inode)
63{
64 truncate_inode_pages_final(&inode->i_data);
65 clear_inode(inode);
66 /* If we are holding a delegation, return and free it */
67 nfs_inode_evict_delegation(inode);
68 /* Note that above delegreturn would trigger pnfs return-on-close */
69 pnfs_return_layout(inode);
70 pnfs_destroy_layout_final(NFS_I(inode));
71 /* First call standard NFS clear_inode() code */
72 nfs_clear_inode(inode);
73 nfs4_xattr_cache_zap(inode);
74}
75
76struct nfs_referral_count {
77 struct list_head list;
78 const struct task_struct *task;
79 unsigned int referral_count;
80};
81
82static LIST_HEAD(nfs_referral_count_list);
83static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
84
85static struct nfs_referral_count *nfs_find_referral_count(void)
86{
87 struct nfs_referral_count *p;
88
89 list_for_each_entry(p, &nfs_referral_count_list, list) {
90 if (p->task == current)
91 return p;
92 }
93 return NULL;
94}
95
96#define NFS_MAX_NESTED_REFERRALS 2
97
98static int nfs_referral_loop_protect(void)
99{
100 struct nfs_referral_count *p, *new;
101 int ret = -ENOMEM;
102
103 new = kmalloc(sizeof(*new), GFP_KERNEL);
104 if (!new)
105 goto out;
106 new->task = current;
107 new->referral_count = 1;
108
109 ret = 0;
110 spin_lock(&nfs_referral_count_list_lock);
111 p = nfs_find_referral_count();
112 if (p != NULL) {
113 if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
114 ret = -ELOOP;
115 else
116 p->referral_count++;
117 } else {
118 list_add(&new->list, &nfs_referral_count_list);
119 new = NULL;
120 }
121 spin_unlock(&nfs_referral_count_list_lock);
122 kfree(new);
123out:
124 return ret;
125}
126
127static void nfs_referral_loop_unprotect(void)
128{
129 struct nfs_referral_count *p;
130
131 spin_lock(&nfs_referral_count_list_lock);
132 p = nfs_find_referral_count();
133 p->referral_count--;
134 if (p->referral_count == 0)
135 list_del(&p->list);
136 else
137 p = NULL;
138 spin_unlock(&nfs_referral_count_list_lock);
139 kfree(p);
140}
141
142static int do_nfs4_mount(struct nfs_server *server,
143 struct fs_context *fc,
144 const char *hostname,
145 const char *export_path)
146{
147 struct nfs_fs_context *root_ctx;
148 struct fs_context *root_fc;
149 struct vfsmount *root_mnt;
150 struct dentry *dentry;
151 size_t len;
152 int ret;
153
154 struct fs_parameter param = {
155 .key = "source",
156 .type = fs_value_is_string,
157 .dirfd = -1,
158 };
159
160 if (IS_ERR(server))
161 return PTR_ERR(server);
162
163 root_fc = vfs_dup_fs_context(fc);
164 if (IS_ERR(root_fc)) {
165 nfs_free_server(server);
166 return PTR_ERR(root_fc);
167 }
168 kfree(root_fc->source);
169 root_fc->source = NULL;
170
171 root_ctx = nfs_fc2context(root_fc);
172 root_ctx->internal = true;
173 root_ctx->server = server;
174 /* We leave export_path unset as it's not used to find the root. */
175
176 len = strlen(hostname) + 5;
177 param.string = kmalloc(len, GFP_KERNEL);
178 if (param.string == NULL) {
179 put_fs_context(root_fc);
180 return -ENOMEM;
181 }
182
183 /* Does hostname needs to be enclosed in brackets? */
184 if (strchr(hostname, ':'))
185 param.size = snprintf(param.string, len, "[%s]:/", hostname);
186 else
187 param.size = snprintf(param.string, len, "%s:/", hostname);
188 ret = vfs_parse_fs_param(root_fc, ¶m);
189 kfree(param.string);
190 if (ret < 0) {
191 put_fs_context(root_fc);
192 return ret;
193 }
194 root_mnt = fc_mount(root_fc);
195 put_fs_context(root_fc);
196
197 if (IS_ERR(root_mnt))
198 return PTR_ERR(root_mnt);
199
200 ret = nfs_referral_loop_protect();
201 if (ret) {
202 mntput(root_mnt);
203 return ret;
204 }
205
206 dentry = mount_subtree(root_mnt, export_path);
207 nfs_referral_loop_unprotect();
208
209 if (IS_ERR(dentry))
210 return PTR_ERR(dentry);
211
212 fc->root = dentry;
213 return 0;
214}
215
216int nfs4_try_get_tree(struct fs_context *fc)
217{
218 struct nfs_fs_context *ctx = nfs_fc2context(fc);
219 int err;
220
221 dfprintk(MOUNT, "--> nfs4_try_get_tree()\n");
222
223 /* We create a mount for the server's root, walk to the requested
224 * location and then create another mount for that.
225 */
226 err= do_nfs4_mount(nfs4_create_server(fc),
227 fc, ctx->nfs_server.hostname,
228 ctx->nfs_server.export_path);
229 if (err) {
230 nfs_ferrorf(fc, MOUNT, "NFS4: Couldn't follow remote path");
231 dfprintk(MOUNT, "<-- nfs4_try_get_tree() = %d [error]\n", err);
232 } else {
233 dfprintk(MOUNT, "<-- nfs4_try_get_tree() = 0\n");
234 }
235 return err;
236}
237
238/*
239 * Create an NFS4 server record on referral traversal
240 */
241int nfs4_get_referral_tree(struct fs_context *fc)
242{
243 struct nfs_fs_context *ctx = nfs_fc2context(fc);
244 int err;
245
246 dprintk("--> nfs4_referral_mount()\n");
247
248 /* create a new volume representation */
249 err = do_nfs4_mount(nfs4_create_referral_server(fc),
250 fc, ctx->nfs_server.hostname,
251 ctx->nfs_server.export_path);
252 if (err) {
253 nfs_ferrorf(fc, MOUNT, "NFS4: Couldn't follow remote path");
254 dfprintk(MOUNT, "<-- nfs4_get_referral_tree() = %d [error]\n", err);
255 } else {
256 dfprintk(MOUNT, "<-- nfs4_get_referral_tree() = 0\n");
257 }
258 return err;
259}
260
261static int __init init_nfs_v4(void)
262{
263 int err;
264
265 err = nfs_dns_resolver_init();
266 if (err)
267 goto out;
268
269 err = nfs_idmap_init();
270 if (err)
271 goto out1;
272
273#ifdef CONFIG_NFS_V4_2
274 err = nfs4_xattr_cache_init();
275 if (err)
276 goto out2;
277#endif
278
279 err = nfs4_register_sysctl();
280 if (err)
281 goto out2;
282
283#ifdef CONFIG_NFS_V4_2
284 nfs42_ssc_register_ops();
285#endif
286 register_nfs_version(&nfs_v4);
287 return 0;
288out2:
289 nfs_idmap_quit();
290out1:
291 nfs_dns_resolver_destroy();
292out:
293 return err;
294}
295
296static void __exit exit_nfs_v4(void)
297{
298 /* Not called in the _init(), conditionally loaded */
299 nfs4_pnfs_v3_ds_connect_unload();
300
301 unregister_nfs_version(&nfs_v4);
302#ifdef CONFIG_NFS_V4_2
303 nfs4_xattr_cache_exit();
304 nfs42_ssc_unregister_ops();
305#endif
306 nfs4_unregister_sysctl();
307 nfs_idmap_quit();
308 nfs_dns_resolver_destroy();
309}
310
311MODULE_LICENSE("GPL");
312
313module_init(init_nfs_v4);
314module_exit(exit_nfs_v4);
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (c) 2012 Bryan Schumaker <bjschuma@netapp.com>
4 */
5#include <linux/init.h>
6#include <linux/module.h>
7#include <linux/mount.h>
8#include <linux/nfs4_mount.h>
9#include <linux/nfs_fs.h>
10#include "delegation.h"
11#include "internal.h"
12#include "nfs4_fs.h"
13#include "nfs4idmap.h"
14#include "dns_resolve.h"
15#include "pnfs.h"
16#include "nfs.h"
17
18#define NFSDBG_FACILITY NFSDBG_VFS
19
20static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc);
21static void nfs4_evict_inode(struct inode *inode);
22
23static const struct super_operations nfs4_sops = {
24 .alloc_inode = nfs_alloc_inode,
25 .free_inode = nfs_free_inode,
26 .write_inode = nfs4_write_inode,
27 .drop_inode = nfs_drop_inode,
28 .statfs = nfs_statfs,
29 .evict_inode = nfs4_evict_inode,
30 .umount_begin = nfs_umount_begin,
31 .show_options = nfs_show_options,
32 .show_devname = nfs_show_devname,
33 .show_path = nfs_show_path,
34 .show_stats = nfs_show_stats,
35};
36
37struct nfs_subversion nfs_v4 = {
38 .owner = THIS_MODULE,
39 .nfs_fs = &nfs4_fs_type,
40 .rpc_vers = &nfs_version4,
41 .rpc_ops = &nfs_v4_clientops,
42 .sops = &nfs4_sops,
43 .xattr = nfs4_xattr_handlers,
44};
45
46static int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc)
47{
48 int ret = nfs_write_inode(inode, wbc);
49
50 if (ret == 0)
51 ret = pnfs_layoutcommit_inode(inode,
52 wbc->sync_mode == WB_SYNC_ALL);
53 return ret;
54}
55
56/*
57 * Clean out any remaining NFSv4 state that might be left over due
58 * to open() calls that passed nfs_atomic_lookup, but failed to call
59 * nfs_open().
60 */
61static void nfs4_evict_inode(struct inode *inode)
62{
63 truncate_inode_pages_final(&inode->i_data);
64 clear_inode(inode);
65 /* If we are holding a delegation, return and free it */
66 nfs_inode_evict_delegation(inode);
67 /* Note that above delegreturn would trigger pnfs return-on-close */
68 pnfs_return_layout(inode);
69 pnfs_destroy_layout(NFS_I(inode));
70 /* First call standard NFS clear_inode() code */
71 nfs_clear_inode(inode);
72 nfs4_xattr_cache_zap(inode);
73}
74
75struct nfs_referral_count {
76 struct list_head list;
77 const struct task_struct *task;
78 unsigned int referral_count;
79};
80
81static LIST_HEAD(nfs_referral_count_list);
82static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
83
84static struct nfs_referral_count *nfs_find_referral_count(void)
85{
86 struct nfs_referral_count *p;
87
88 list_for_each_entry(p, &nfs_referral_count_list, list) {
89 if (p->task == current)
90 return p;
91 }
92 return NULL;
93}
94
95#define NFS_MAX_NESTED_REFERRALS 2
96
97static int nfs_referral_loop_protect(void)
98{
99 struct nfs_referral_count *p, *new;
100 int ret = -ENOMEM;
101
102 new = kmalloc(sizeof(*new), GFP_KERNEL);
103 if (!new)
104 goto out;
105 new->task = current;
106 new->referral_count = 1;
107
108 ret = 0;
109 spin_lock(&nfs_referral_count_list_lock);
110 p = nfs_find_referral_count();
111 if (p != NULL) {
112 if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
113 ret = -ELOOP;
114 else
115 p->referral_count++;
116 } else {
117 list_add(&new->list, &nfs_referral_count_list);
118 new = NULL;
119 }
120 spin_unlock(&nfs_referral_count_list_lock);
121 kfree(new);
122out:
123 return ret;
124}
125
126static void nfs_referral_loop_unprotect(void)
127{
128 struct nfs_referral_count *p;
129
130 spin_lock(&nfs_referral_count_list_lock);
131 p = nfs_find_referral_count();
132 p->referral_count--;
133 if (p->referral_count == 0)
134 list_del(&p->list);
135 else
136 p = NULL;
137 spin_unlock(&nfs_referral_count_list_lock);
138 kfree(p);
139}
140
141static int do_nfs4_mount(struct nfs_server *server,
142 struct fs_context *fc,
143 const char *hostname,
144 const char *export_path)
145{
146 struct nfs_fs_context *root_ctx;
147 struct fs_context *root_fc;
148 struct vfsmount *root_mnt;
149 struct dentry *dentry;
150 size_t len;
151 int ret;
152
153 struct fs_parameter param = {
154 .key = "source",
155 .type = fs_value_is_string,
156 .dirfd = -1,
157 };
158
159 if (IS_ERR(server))
160 return PTR_ERR(server);
161
162 root_fc = vfs_dup_fs_context(fc);
163 if (IS_ERR(root_fc)) {
164 nfs_free_server(server);
165 return PTR_ERR(root_fc);
166 }
167 kfree(root_fc->source);
168 root_fc->source = NULL;
169
170 root_ctx = nfs_fc2context(root_fc);
171 root_ctx->internal = true;
172 root_ctx->server = server;
173 /* We leave export_path unset as it's not used to find the root. */
174
175 len = strlen(hostname) + 5;
176 param.string = kmalloc(len, GFP_KERNEL);
177 if (param.string == NULL) {
178 put_fs_context(root_fc);
179 return -ENOMEM;
180 }
181
182 /* Does hostname needs to be enclosed in brackets? */
183 if (strchr(hostname, ':'))
184 param.size = snprintf(param.string, len, "[%s]:/", hostname);
185 else
186 param.size = snprintf(param.string, len, "%s:/", hostname);
187 ret = vfs_parse_fs_param(root_fc, ¶m);
188 kfree(param.string);
189 if (ret < 0) {
190 put_fs_context(root_fc);
191 return ret;
192 }
193 root_mnt = fc_mount(root_fc);
194 put_fs_context(root_fc);
195
196 if (IS_ERR(root_mnt))
197 return PTR_ERR(root_mnt);
198
199 ret = nfs_referral_loop_protect();
200 if (ret) {
201 mntput(root_mnt);
202 return ret;
203 }
204
205 dentry = mount_subtree(root_mnt, export_path);
206 nfs_referral_loop_unprotect();
207
208 if (IS_ERR(dentry))
209 return PTR_ERR(dentry);
210
211 fc->root = dentry;
212 return 0;
213}
214
215int nfs4_try_get_tree(struct fs_context *fc)
216{
217 struct nfs_fs_context *ctx = nfs_fc2context(fc);
218 int err;
219
220 dfprintk(MOUNT, "--> nfs4_try_get_tree()\n");
221
222 /* We create a mount for the server's root, walk to the requested
223 * location and then create another mount for that.
224 */
225 err= do_nfs4_mount(nfs4_create_server(fc),
226 fc, ctx->nfs_server.hostname,
227 ctx->nfs_server.export_path);
228 if (err) {
229 nfs_errorf(fc, "NFS4: Couldn't follow remote path");
230 dfprintk(MOUNT, "<-- nfs4_try_get_tree() = %d [error]\n", err);
231 } else {
232 dfprintk(MOUNT, "<-- nfs4_try_get_tree() = 0\n");
233 }
234 return err;
235}
236
237/*
238 * Create an NFS4 server record on referral traversal
239 */
240int nfs4_get_referral_tree(struct fs_context *fc)
241{
242 struct nfs_fs_context *ctx = nfs_fc2context(fc);
243 int err;
244
245 dprintk("--> nfs4_referral_mount()\n");
246
247 /* create a new volume representation */
248 err = do_nfs4_mount(nfs4_create_referral_server(fc),
249 fc, ctx->nfs_server.hostname,
250 ctx->nfs_server.export_path);
251 if (err) {
252 nfs_errorf(fc, "NFS4: Couldn't follow remote path");
253 dfprintk(MOUNT, "<-- nfs4_get_referral_tree() = %d [error]\n", err);
254 } else {
255 dfprintk(MOUNT, "<-- nfs4_get_referral_tree() = 0\n");
256 }
257 return err;
258}
259
260static int __init init_nfs_v4(void)
261{
262 int err;
263
264 err = nfs_dns_resolver_init();
265 if (err)
266 goto out;
267
268 err = nfs_idmap_init();
269 if (err)
270 goto out1;
271
272#ifdef CONFIG_NFS_V4_2
273 err = nfs4_xattr_cache_init();
274 if (err)
275 goto out2;
276#endif
277
278 err = nfs4_register_sysctl();
279 if (err)
280 goto out2;
281
282 register_nfs_version(&nfs_v4);
283 return 0;
284out2:
285 nfs_idmap_quit();
286out1:
287 nfs_dns_resolver_destroy();
288out:
289 return err;
290}
291
292static void __exit exit_nfs_v4(void)
293{
294 /* Not called in the _init(), conditionally loaded */
295 nfs4_pnfs_v3_ds_connect_unload();
296
297 unregister_nfs_version(&nfs_v4);
298#ifdef CONFIG_NFS_V4_2
299 nfs4_xattr_cache_exit();
300#endif
301 nfs4_unregister_sysctl();
302 nfs_idmap_quit();
303 nfs_dns_resolver_destroy();
304}
305
306MODULE_LICENSE("GPL");
307
308module_init(init_nfs_v4);
309module_exit(exit_nfs_v4);