xref: /linux/fs/nfs/nfs3proc.c (revision 9e4e86a604dfd06402933467578c4b79f5412b2c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/fs/nfs/nfs3proc.c
4  *
5  *  Client-side NFSv3 procedures stubs.
6  *
7  *  Copyright (C) 1997, Olaf Kirch
8  */
9 
10 #include <linux/mm.h>
11 #include <linux/errno.h>
12 #include <linux/string.h>
13 #include <linux/sunrpc/clnt.h>
14 #include <linux/slab.h>
15 #include <linux/nfs.h>
16 #include <linux/nfs3.h>
17 #include <linux/nfs_fs.h>
18 #include <linux/nfs_page.h>
19 #include <linux/filelock.h>
20 #include <linux/lockd/bind.h>
21 #include <linux/nfs_mount.h>
22 #include <linux/freezer.h>
23 #include <linux/xattr.h>
24 
25 #include "iostat.h"
26 #include "internal.h"
27 #include "nfs3_fs.h"
28 
29 #define NFSDBG_FACILITY		NFSDBG_PROC
30 
31 /* A wrapper to handle the EJUKEBOX error messages */
32 static int
nfs3_rpc_wrapper(struct rpc_clnt * clnt,struct rpc_message * msg,int flags)33 nfs3_rpc_wrapper(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
34 {
35 	int res;
36 	do {
37 		res = rpc_call_sync(clnt, msg, flags);
38 		if (res != -EJUKEBOX)
39 			break;
40 		__set_current_state(TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
41 		schedule_timeout(NFS_JUKEBOX_RETRY_TIME);
42 		res = -ERESTARTSYS;
43 	} while (!fatal_signal_pending(current) && !nfs_current_task_exiting());
44 	return res;
45 }
46 
47 #define rpc_call_sync(clnt, msg, flags)	nfs3_rpc_wrapper(clnt, msg, flags)
48 
49 static int
nfs3_async_handle_jukebox(struct rpc_task * task,struct inode * inode)50 nfs3_async_handle_jukebox(struct rpc_task *task, struct inode *inode)
51 {
52 	if (task->tk_status != -EJUKEBOX)
53 		return 0;
54 	nfs_inc_stats(inode, NFSIOS_DELAY);
55 	task->tk_status = 0;
56 	rpc_restart_call(task);
57 	rpc_delay(task, NFS_JUKEBOX_RETRY_TIME);
58 	return 1;
59 }
60 
61 static int
do_proc_get_root(struct rpc_clnt * client,struct nfs_fh * fhandle,struct nfs_fsinfo * info)62 do_proc_get_root(struct rpc_clnt *client, struct nfs_fh *fhandle,
63 		 struct nfs_fsinfo *info)
64 {
65 	struct rpc_message msg = {
66 		.rpc_proc	= &nfs3_procedures[NFS3PROC_FSINFO],
67 		.rpc_argp	= fhandle,
68 		.rpc_resp	= info,
69 	};
70 	int	status;
71 
72 	dprintk("%s: call  fsinfo\n", __func__);
73 	nfs_fattr_init(info->fattr);
74 	status = rpc_call_sync(client, &msg, 0);
75 	dprintk("%s: reply fsinfo: %d\n", __func__, status);
76 	if (status == 0 && !(info->fattr->valid & NFS_ATTR_FATTR)) {
77 		msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
78 		msg.rpc_resp = info->fattr;
79 		status = rpc_call_sync(client, &msg, 0);
80 		dprintk("%s: reply getattr: %d\n", __func__, status);
81 	}
82 	return status;
83 }
84 
85 /*
86  * Bare-bones access to getattr: this is for nfs_get_root/nfs_get_sb
87  */
88 static int
nfs3_proc_get_root(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsinfo * info)89 nfs3_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
90 		   struct nfs_fsinfo *info)
91 {
92 	int	status;
93 
94 	status = do_proc_get_root(server->client, fhandle, info);
95 	if (status && server->nfs_client->cl_rpcclient != server->client)
96 		status = do_proc_get_root(server->nfs_client->cl_rpcclient, fhandle, info);
97 	return status;
98 }
99 
100 /*
101  * One function for each procedure in the NFS protocol.
102  */
103 static int
nfs3_proc_getattr(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fattr * fattr,struct inode * inode)104 nfs3_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
105 		struct nfs_fattr *fattr, struct inode *inode)
106 {
107 	struct rpc_message msg = {
108 		.rpc_proc	= &nfs3_procedures[NFS3PROC_GETATTR],
109 		.rpc_argp	= fhandle,
110 		.rpc_resp	= fattr,
111 	};
112 	int	status;
113 	unsigned short task_flags = 0;
114 
115 	/* Is this is an attribute revalidation, subject to softreval? */
116 	if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
117 		task_flags |= RPC_TASK_TIMEOUT;
118 
119 	dprintk("NFS call  getattr\n");
120 	nfs_fattr_init(fattr);
121 	status = rpc_call_sync(server->client, &msg, task_flags);
122 	dprintk("NFS reply getattr: %d\n", status);
123 	return status;
124 }
125 
126 static int
nfs3_proc_setattr(struct dentry * dentry,struct nfs_fattr * fattr,struct iattr * sattr)127 nfs3_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
128 			struct iattr *sattr)
129 {
130 	struct inode *inode = d_inode(dentry);
131 	struct nfs3_sattrargs	arg = {
132 		.fh		= NFS_FH(inode),
133 		.sattr		= sattr,
134 	};
135 	struct rpc_message msg = {
136 		.rpc_proc	= &nfs3_procedures[NFS3PROC_SETATTR],
137 		.rpc_argp	= &arg,
138 		.rpc_resp	= fattr,
139 	};
140 	int	status;
141 
142 	dprintk("NFS call  setattr\n");
143 	if (sattr->ia_valid & ATTR_FILE)
144 		msg.rpc_cred = nfs_file_cred(sattr->ia_file);
145 	nfs_fattr_init(fattr);
146 	status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
147 	if (status == 0) {
148 		nfs_setattr_update_inode(inode, sattr, fattr);
149 		if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
150 			nfs_zap_acl_cache(inode);
151 	}
152 	dprintk("NFS reply setattr: %d\n", status);
153 	return status;
154 }
155 
156 static int
__nfs3_proc_lookup(struct inode * dir,const char * name,size_t len,struct nfs_fh * fhandle,struct nfs_fattr * fattr,unsigned short task_flags)157 __nfs3_proc_lookup(struct inode *dir, const char *name, size_t len,
158 		   struct nfs_fh *fhandle, struct nfs_fattr *fattr,
159 		   unsigned short task_flags)
160 {
161 	struct nfs3_diropargs	arg = {
162 		.fh		= NFS_FH(dir),
163 		.name		= name,
164 		.len		= len
165 	};
166 	struct nfs3_diropres	res = {
167 		.fh		= fhandle,
168 		.fattr		= fattr
169 	};
170 	struct rpc_message msg = {
171 		.rpc_proc	= &nfs3_procedures[NFS3PROC_LOOKUP],
172 		.rpc_argp	= &arg,
173 		.rpc_resp	= &res,
174 	};
175 	int			status;
176 
177 	res.dir_attr = nfs_alloc_fattr();
178 	if (res.dir_attr == NULL)
179 		return -ENOMEM;
180 
181 	nfs_fattr_init(fattr);
182 	status = rpc_call_sync(NFS_CLIENT(dir), &msg, task_flags);
183 	nfs_refresh_inode(dir, res.dir_attr);
184 	if (status >= 0 && !(fattr->valid & NFS_ATTR_FATTR)) {
185 		msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
186 		msg.rpc_argp = fhandle;
187 		msg.rpc_resp = fattr;
188 		status = rpc_call_sync(NFS_CLIENT(dir), &msg, task_flags);
189 	}
190 	nfs_free_fattr(res.dir_attr);
191 	dprintk("NFS reply lookup: %d\n", status);
192 	return status;
193 }
194 
195 static int
nfs3_proc_lookup(struct inode * dir,struct dentry * dentry,const struct qstr * name,struct nfs_fh * fhandle,struct nfs_fattr * fattr)196 nfs3_proc_lookup(struct inode *dir, struct dentry *dentry, const struct qstr *name,
197 		 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
198 {
199 	unsigned short task_flags = 0;
200 
201 	/* Is this is an attribute revalidation, subject to softreval? */
202 	if (nfs_lookup_is_soft_revalidate(dentry))
203 		task_flags |= RPC_TASK_TIMEOUT;
204 
205 	dprintk("NFS call  lookup %pd2\n", dentry);
206 	return __nfs3_proc_lookup(dir, name->name, name->len, fhandle, fattr,
207 				  task_flags);
208 }
209 
nfs3_proc_lookupp(struct inode * inode,struct nfs_fh * fhandle,struct nfs_fattr * fattr)210 static int nfs3_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
211 			     struct nfs_fattr *fattr)
212 {
213 	const char dotdot[] = "..";
214 	const size_t len = strlen(dotdot);
215 	unsigned short task_flags = 0;
216 
217 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
218 		task_flags |= RPC_TASK_TIMEOUT;
219 
220 	return __nfs3_proc_lookup(inode, dotdot, len, fhandle, fattr,
221 				  task_flags);
222 }
223 
nfs3_proc_access(struct inode * inode,struct nfs_access_entry * entry,const struct cred * cred)224 static int nfs3_proc_access(struct inode *inode, struct nfs_access_entry *entry,
225 			    const struct cred *cred)
226 {
227 	struct nfs3_accessargs	arg = {
228 		.fh		= NFS_FH(inode),
229 		.access		= entry->mask,
230 	};
231 	struct nfs3_accessres	res;
232 	struct rpc_message msg = {
233 		.rpc_proc	= &nfs3_procedures[NFS3PROC_ACCESS],
234 		.rpc_argp	= &arg,
235 		.rpc_resp	= &res,
236 		.rpc_cred	= cred,
237 	};
238 	int status = -ENOMEM;
239 
240 	dprintk("NFS call  access\n");
241 	res.fattr = nfs_alloc_fattr();
242 	if (res.fattr == NULL)
243 		goto out;
244 
245 	status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
246 	nfs_refresh_inode(inode, res.fattr);
247 	if (status == 0)
248 		nfs_access_set_mask(entry, res.access);
249 	nfs_free_fattr(res.fattr);
250 out:
251 	dprintk("NFS reply access: %d\n", status);
252 	return status;
253 }
254 
nfs3_proc_readlink(struct inode * inode,struct page * page,unsigned int pgbase,unsigned int pglen)255 static int nfs3_proc_readlink(struct inode *inode, struct page *page,
256 		unsigned int pgbase, unsigned int pglen)
257 {
258 	struct nfs_fattr	*fattr;
259 	struct nfs3_readlinkargs args = {
260 		.fh		= NFS_FH(inode),
261 		.pgbase		= pgbase,
262 		.pglen		= pglen,
263 		.pages		= &page
264 	};
265 	struct rpc_message msg = {
266 		.rpc_proc	= &nfs3_procedures[NFS3PROC_READLINK],
267 		.rpc_argp	= &args,
268 	};
269 	int status = -ENOMEM;
270 
271 	dprintk("NFS call  readlink\n");
272 	fattr = nfs_alloc_fattr();
273 	if (fattr == NULL)
274 		goto out;
275 	msg.rpc_resp = fattr;
276 
277 	status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
278 	nfs_refresh_inode(inode, fattr);
279 	nfs_free_fattr(fattr);
280 out:
281 	dprintk("NFS reply readlink: %d\n", status);
282 	return status;
283 }
284 
285 struct nfs3_createdata {
286 	struct rpc_message msg;
287 	union {
288 		struct nfs3_createargs create;
289 		struct nfs3_mkdirargs mkdir;
290 		struct nfs3_symlinkargs symlink;
291 		struct nfs3_mknodargs mknod;
292 	} arg;
293 	struct nfs3_diropres res;
294 	struct nfs_fh fh;
295 	struct nfs_fattr fattr;
296 	struct nfs_fattr dir_attr;
297 };
298 
nfs3_alloc_createdata(void)299 static struct nfs3_createdata *nfs3_alloc_createdata(void)
300 {
301 	struct nfs3_createdata *data;
302 
303 	data = kzalloc_obj(*data);
304 	if (data != NULL) {
305 		data->msg.rpc_argp = &data->arg;
306 		data->msg.rpc_resp = &data->res;
307 		data->res.fh = &data->fh;
308 		data->res.fattr = &data->fattr;
309 		data->res.dir_attr = &data->dir_attr;
310 		nfs_fattr_init(data->res.fattr);
311 		nfs_fattr_init(data->res.dir_attr);
312 	}
313 	return data;
314 }
315 
316 static struct dentry *
nfs3_do_create(struct inode * dir,struct dentry * dentry,struct nfs3_createdata * data)317 nfs3_do_create(struct inode *dir, struct dentry *dentry, struct nfs3_createdata *data)
318 {
319 	int status;
320 
321 	status = rpc_call_sync(NFS_CLIENT(dir), &data->msg, 0);
322 	nfs_post_op_update_inode(dir, data->res.dir_attr);
323 	if (status != 0)
324 		return ERR_PTR(status);
325 
326 	return nfs_add_or_obtain(dentry, data->res.fh, data->res.fattr);
327 }
328 
nfs3_free_createdata(struct nfs3_createdata * data)329 static void nfs3_free_createdata(struct nfs3_createdata *data)
330 {
331 	kfree(data);
332 }
333 
334 /*
335  * Create a regular file.
336  */
337 static int
nfs3_proc_create(struct inode * dir,struct dentry * dentry,struct iattr * sattr,int flags)338 nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
339 		 int flags)
340 {
341 	struct posix_acl *default_acl, *acl;
342 	struct nfs3_createdata *data;
343 	struct dentry *d_alias;
344 	int status = -ENOMEM;
345 
346 	dprintk("NFS call  create %pd\n", dentry);
347 
348 	data = nfs3_alloc_createdata();
349 	if (data == NULL)
350 		goto out;
351 
352 	data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_CREATE];
353 	data->arg.create.fh = NFS_FH(dir);
354 	data->arg.create.name = dentry->d_name.name;
355 	data->arg.create.len = dentry->d_name.len;
356 	data->arg.create.sattr = sattr;
357 
358 	data->arg.create.createmode = NFS3_CREATE_UNCHECKED;
359 	if (flags & O_EXCL) {
360 		data->arg.create.createmode  = NFS3_CREATE_EXCLUSIVE;
361 		data->arg.create.verifier[0] = cpu_to_be32(jiffies);
362 		data->arg.create.verifier[1] = cpu_to_be32(current->pid);
363 	}
364 
365 	status = posix_acl_create(dir, &sattr->ia_mode, &default_acl, &acl);
366 	if (status)
367 		goto out;
368 
369 	for (;;) {
370 		d_alias = nfs3_do_create(dir, dentry, data);
371 		status = PTR_ERR_OR_ZERO(d_alias);
372 
373 		if (status != -ENOTSUPP)
374 			break;
375 		/* If the server doesn't support the exclusive creation
376 		 * semantics, try again with simple 'guarded' mode. */
377 		switch (data->arg.create.createmode) {
378 			case NFS3_CREATE_EXCLUSIVE:
379 				data->arg.create.createmode = NFS3_CREATE_GUARDED;
380 				break;
381 
382 			case NFS3_CREATE_GUARDED:
383 				data->arg.create.createmode = NFS3_CREATE_UNCHECKED;
384 				break;
385 
386 			case NFS3_CREATE_UNCHECKED:
387 				goto out_release_acls;
388 		}
389 		nfs_fattr_init(data->res.dir_attr);
390 		nfs_fattr_init(data->res.fattr);
391 	}
392 
393 	if (status != 0)
394 		goto out_release_acls;
395 
396 	if (d_alias) {
397 		if (d_is_dir(d_alias)) {
398 			status = -EISDIR;
399 			goto out_dput;
400 		}
401 		dentry = d_alias;
402 	}
403 
404 	/* When we created the file with exclusive semantics, make
405 	 * sure we set the attributes afterwards. */
406 	if (data->arg.create.createmode == NFS3_CREATE_EXCLUSIVE) {
407 		dprintk("NFS call  setattr (post-create)\n");
408 
409 		if (!(sattr->ia_valid & ATTR_ATIME_SET))
410 			sattr->ia_valid |= ATTR_ATIME;
411 		if (!(sattr->ia_valid & ATTR_MTIME_SET))
412 			sattr->ia_valid |= ATTR_MTIME;
413 
414 		/* Note: we could use a guarded setattr here, but I'm
415 		 * not sure this buys us anything (and I'd have
416 		 * to revamp the NFSv3 XDR code) */
417 		status = nfs3_proc_setattr(dentry, data->res.fattr, sattr);
418 		nfs_post_op_update_inode(d_inode(dentry), data->res.fattr);
419 		dprintk("NFS reply setattr (post-create): %d\n", status);
420 		if (status != 0)
421 			goto out_dput;
422 	}
423 
424 	status = nfs3_proc_setacls(d_inode(dentry), acl, default_acl);
425 
426 out_dput:
427 	dput(d_alias);
428 out_release_acls:
429 	posix_acl_release(acl);
430 	posix_acl_release(default_acl);
431 out:
432 	nfs3_free_createdata(data);
433 	dprintk("NFS reply create: %d\n", status);
434 	return status;
435 }
436 
437 static int
nfs3_proc_remove(struct inode * dir,struct dentry * dentry)438 nfs3_proc_remove(struct inode *dir, struct dentry *dentry)
439 {
440 	struct nfs_removeargs arg = {
441 		.fh = NFS_FH(dir),
442 		.name = dentry->d_name,
443 	};
444 	struct nfs_removeres res;
445 	struct rpc_message msg = {
446 		.rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE],
447 		.rpc_argp = &arg,
448 		.rpc_resp = &res,
449 	};
450 	int status = -ENOMEM;
451 
452 	dprintk("NFS call  remove %pd2\n", dentry);
453 	res.dir_attr = nfs_alloc_fattr();
454 	if (res.dir_attr == NULL)
455 		goto out;
456 
457 	status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
458 	nfs_post_op_update_inode(dir, res.dir_attr);
459 	nfs_free_fattr(res.dir_attr);
460 out:
461 	dprintk("NFS reply remove: %d\n", status);
462 	return status;
463 }
464 
465 static void
nfs3_proc_unlink_setup(struct rpc_message * msg,struct dentry * dentry,struct inode * inode)466 nfs3_proc_unlink_setup(struct rpc_message *msg,
467 		struct dentry *dentry,
468 		struct inode *inode)
469 {
470 	msg->rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE];
471 }
472 
nfs3_proc_unlink_rpc_prepare(struct rpc_task * task,struct nfs_unlinkdata * data)473 static void nfs3_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
474 {
475 	rpc_call_start(task);
476 }
477 
478 static int
nfs3_proc_unlink_done(struct rpc_task * task,struct inode * dir)479 nfs3_proc_unlink_done(struct rpc_task *task, struct inode *dir)
480 {
481 	struct nfs_removeres *res;
482 	if (nfs3_async_handle_jukebox(task, dir))
483 		return 0;
484 	res = task->tk_msg.rpc_resp;
485 	nfs_post_op_update_inode(dir, res->dir_attr);
486 	return 1;
487 }
488 
489 static void
nfs3_proc_rename_setup(struct rpc_message * msg,struct dentry * old_dentry,struct dentry * new_dentry,struct inode * same_parent)490 nfs3_proc_rename_setup(struct rpc_message *msg,
491 		struct dentry *old_dentry,
492 		struct dentry *new_dentry,
493 		struct inode *same_parent)
494 {
495 	msg->rpc_proc = &nfs3_procedures[NFS3PROC_RENAME];
496 }
497 
nfs3_proc_rename_rpc_prepare(struct rpc_task * task,struct nfs_renamedata * data)498 static void nfs3_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
499 {
500 	rpc_call_start(task);
501 }
502 
503 static int
nfs3_proc_rename_done(struct rpc_task * task,struct inode * old_dir,struct inode * new_dir)504 nfs3_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
505 		      struct inode *new_dir)
506 {
507 	struct nfs_renameres *res;
508 
509 	if (nfs3_async_handle_jukebox(task, old_dir))
510 		return 0;
511 	res = task->tk_msg.rpc_resp;
512 
513 	nfs_post_op_update_inode(old_dir, res->old_fattr);
514 	nfs_post_op_update_inode(new_dir, res->new_fattr);
515 	return 1;
516 }
517 
518 static int
nfs3_proc_link(struct inode * inode,struct inode * dir,const struct qstr * name)519 nfs3_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
520 {
521 	struct nfs3_linkargs	arg = {
522 		.fromfh		= NFS_FH(inode),
523 		.tofh		= NFS_FH(dir),
524 		.toname		= name->name,
525 		.tolen		= name->len
526 	};
527 	struct nfs3_linkres	res;
528 	struct rpc_message msg = {
529 		.rpc_proc	= &nfs3_procedures[NFS3PROC_LINK],
530 		.rpc_argp	= &arg,
531 		.rpc_resp	= &res,
532 	};
533 	int status = -ENOMEM;
534 
535 	dprintk("NFS call  link %s\n", name->name);
536 	res.fattr = nfs_alloc_fattr();
537 	res.dir_attr = nfs_alloc_fattr();
538 	if (res.fattr == NULL || res.dir_attr == NULL)
539 		goto out;
540 
541 	status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
542 	nfs_post_op_update_inode(dir, res.dir_attr);
543 	nfs_post_op_update_inode(inode, res.fattr);
544 out:
545 	nfs_free_fattr(res.dir_attr);
546 	nfs_free_fattr(res.fattr);
547 	dprintk("NFS reply link: %d\n", status);
548 	return status;
549 }
550 
551 static int
nfs3_proc_symlink(struct inode * dir,struct dentry * dentry,struct folio * folio,unsigned int len,struct iattr * sattr)552 nfs3_proc_symlink(struct inode *dir, struct dentry *dentry, struct folio *folio,
553 		  unsigned int len, struct iattr *sattr)
554 {
555 	struct page *page = &folio->page;
556 	struct nfs3_createdata *data;
557 	struct dentry *d_alias;
558 	int status = -ENOMEM;
559 
560 	if (len > NFS3_MAXPATHLEN)
561 		return -ENAMETOOLONG;
562 
563 	dprintk("NFS call  symlink %pd\n", dentry);
564 
565 	data = nfs3_alloc_createdata();
566 	if (data == NULL)
567 		goto out;
568 	data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_SYMLINK];
569 	data->arg.symlink.fromfh = NFS_FH(dir);
570 	data->arg.symlink.fromname = dentry->d_name.name;
571 	data->arg.symlink.fromlen = dentry->d_name.len;
572 	data->arg.symlink.pages = &page;
573 	data->arg.symlink.pathlen = len;
574 	data->arg.symlink.sattr = sattr;
575 
576 	d_alias = nfs3_do_create(dir, dentry, data);
577 	status = PTR_ERR_OR_ZERO(d_alias);
578 
579 	if (status == 0)
580 		dput(d_alias);
581 
582 	nfs3_free_createdata(data);
583 out:
584 	dprintk("NFS reply symlink: %d\n", status);
585 	return status;
586 }
587 
588 static struct dentry *
nfs3_proc_mkdir(struct inode * dir,struct dentry * dentry,struct iattr * sattr)589 nfs3_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
590 {
591 	struct posix_acl *default_acl, *acl;
592 	struct nfs3_createdata *data;
593 	struct dentry *ret = ERR_PTR(-ENOMEM);
594 	int status;
595 
596 	dprintk("NFS call  mkdir %pd\n", dentry);
597 
598 	data = nfs3_alloc_createdata();
599 	if (data == NULL)
600 		goto out;
601 
602 	ret = ERR_PTR(posix_acl_create(dir, &sattr->ia_mode,
603 				       &default_acl, &acl));
604 	if (IS_ERR(ret))
605 		goto out;
606 
607 	data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_MKDIR];
608 	data->arg.mkdir.fh = NFS_FH(dir);
609 	data->arg.mkdir.name = dentry->d_name.name;
610 	data->arg.mkdir.len = dentry->d_name.len;
611 	data->arg.mkdir.sattr = sattr;
612 
613 	ret = nfs3_do_create(dir, dentry, data);
614 
615 	if (IS_ERR(ret))
616 		goto out_release_acls;
617 
618 	if (ret)
619 		dentry = ret;
620 
621 	status = nfs3_proc_setacls(d_inode(dentry), acl, default_acl);
622 	if (status) {
623 		dput(ret);
624 		ret = ERR_PTR(status);
625 	}
626 
627 out_release_acls:
628 	posix_acl_release(acl);
629 	posix_acl_release(default_acl);
630 out:
631 	nfs3_free_createdata(data);
632 	dprintk("NFS reply mkdir: %d\n", PTR_ERR_OR_ZERO(ret));
633 	return ret;
634 }
635 
636 static int
nfs3_proc_rmdir(struct inode * dir,const struct qstr * name)637 nfs3_proc_rmdir(struct inode *dir, const struct qstr *name)
638 {
639 	struct nfs_fattr	*dir_attr;
640 	struct nfs3_diropargs	arg = {
641 		.fh		= NFS_FH(dir),
642 		.name		= name->name,
643 		.len		= name->len
644 	};
645 	struct rpc_message msg = {
646 		.rpc_proc	= &nfs3_procedures[NFS3PROC_RMDIR],
647 		.rpc_argp	= &arg,
648 	};
649 	int status = -ENOMEM;
650 
651 	dprintk("NFS call  rmdir %s\n", name->name);
652 	dir_attr = nfs_alloc_fattr();
653 	if (dir_attr == NULL)
654 		goto out;
655 
656 	msg.rpc_resp = dir_attr;
657 	status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
658 	nfs_post_op_update_inode(dir, dir_attr);
659 	nfs_free_fattr(dir_attr);
660 out:
661 	dprintk("NFS reply rmdir: %d\n", status);
662 	return status;
663 }
664 
665 /*
666  * The READDIR implementation is somewhat hackish - we pass the user buffer
667  * to the encode function, which installs it in the receive iovec.
668  * The decode function itself doesn't perform any decoding, it just makes
669  * sure the reply is syntactically correct.
670  *
671  * Also note that this implementation handles both plain readdir and
672  * readdirplus.
673  */
nfs3_proc_readdir(struct nfs_readdir_arg * nr_arg,struct nfs_readdir_res * nr_res)674 static int nfs3_proc_readdir(struct nfs_readdir_arg *nr_arg,
675 			     struct nfs_readdir_res *nr_res)
676 {
677 	struct inode		*dir = d_inode(nr_arg->dentry);
678 	struct nfs3_readdirargs	arg = {
679 		.fh		= NFS_FH(dir),
680 		.cookie		= nr_arg->cookie,
681 		.plus		= nr_arg->plus,
682 		.count		= nr_arg->page_len,
683 		.pages		= nr_arg->pages
684 	};
685 	struct nfs3_readdirres	res = {
686 		.verf		= nr_res->verf,
687 		.plus		= nr_arg->plus,
688 	};
689 	struct rpc_message	msg = {
690 		.rpc_proc	= &nfs3_procedures[NFS3PROC_READDIR],
691 		.rpc_argp	= &arg,
692 		.rpc_resp	= &res,
693 		.rpc_cred	= nr_arg->cred,
694 	};
695 	int status = -ENOMEM;
696 
697 	if (nr_arg->plus)
698 		msg.rpc_proc = &nfs3_procedures[NFS3PROC_READDIRPLUS];
699 	if (arg.cookie)
700 		memcpy(arg.verf, nr_arg->verf, sizeof(arg.verf));
701 
702 	dprintk("NFS call  readdir%s %llu\n", nr_arg->plus ? "plus" : "",
703 		(unsigned long long)nr_arg->cookie);
704 
705 	res.dir_attr = nfs_alloc_fattr();
706 	if (res.dir_attr == NULL)
707 		goto out;
708 
709 	status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
710 
711 	nfs_invalidate_atime(dir);
712 	nfs_refresh_inode(dir, res.dir_attr);
713 
714 	nfs_free_fattr(res.dir_attr);
715 out:
716 	dprintk("NFS reply readdir%s: %d\n", nr_arg->plus ? "plus" : "",
717 		status);
718 	return status;
719 }
720 
721 static int
nfs3_proc_mknod(struct inode * dir,struct dentry * dentry,struct iattr * sattr,dev_t rdev)722 nfs3_proc_mknod(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
723 		dev_t rdev)
724 {
725 	struct posix_acl *default_acl, *acl;
726 	struct nfs3_createdata *data;
727 	struct dentry *d_alias;
728 	int status = -ENOMEM;
729 
730 	dprintk("NFS call  mknod %pd %u:%u\n", dentry,
731 			MAJOR(rdev), MINOR(rdev));
732 
733 	data = nfs3_alloc_createdata();
734 	if (data == NULL)
735 		goto out;
736 
737 	status = posix_acl_create(dir, &sattr->ia_mode, &default_acl, &acl);
738 	if (status)
739 		goto out;
740 
741 	data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_MKNOD];
742 	data->arg.mknod.fh = NFS_FH(dir);
743 	data->arg.mknod.name = dentry->d_name.name;
744 	data->arg.mknod.len = dentry->d_name.len;
745 	data->arg.mknod.sattr = sattr;
746 	data->arg.mknod.rdev = rdev;
747 
748 	switch (sattr->ia_mode & S_IFMT) {
749 	case S_IFBLK:
750 		data->arg.mknod.type = NF3BLK;
751 		break;
752 	case S_IFCHR:
753 		data->arg.mknod.type = NF3CHR;
754 		break;
755 	case S_IFIFO:
756 		data->arg.mknod.type = NF3FIFO;
757 		break;
758 	case S_IFSOCK:
759 		data->arg.mknod.type = NF3SOCK;
760 		break;
761 	default:
762 		status = -EINVAL;
763 		goto out_release_acls;
764 	}
765 
766 	d_alias = nfs3_do_create(dir, dentry, data);
767 	status = PTR_ERR_OR_ZERO(d_alias);
768 	if (status != 0)
769 		goto out_release_acls;
770 
771 	if (d_alias)
772 		dentry = d_alias;
773 
774 	status = nfs3_proc_setacls(d_inode(dentry), acl, default_acl);
775 
776 	dput(d_alias);
777 out_release_acls:
778 	posix_acl_release(acl);
779 	posix_acl_release(default_acl);
780 out:
781 	nfs3_free_createdata(data);
782 	dprintk("NFS reply mknod: %d\n", status);
783 	return status;
784 }
785 
786 static int
nfs3_proc_statfs(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsstat * stat)787 nfs3_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
788 		 struct nfs_fsstat *stat)
789 {
790 	struct rpc_message msg = {
791 		.rpc_proc	= &nfs3_procedures[NFS3PROC_FSSTAT],
792 		.rpc_argp	= fhandle,
793 		.rpc_resp	= stat,
794 	};
795 	int	status;
796 
797 	dprintk("NFS call  fsstat\n");
798 	nfs_fattr_init(stat->fattr);
799 	status = rpc_call_sync(server->client, &msg, 0);
800 	dprintk("NFS reply fsstat: %d\n", status);
801 	return status;
802 }
803 
804 static int
do_proc_fsinfo(struct rpc_clnt * client,struct nfs_fh * fhandle,struct nfs_fsinfo * info)805 do_proc_fsinfo(struct rpc_clnt *client, struct nfs_fh *fhandle,
806 		 struct nfs_fsinfo *info)
807 {
808 	struct rpc_message msg = {
809 		.rpc_proc	= &nfs3_procedures[NFS3PROC_FSINFO],
810 		.rpc_argp	= fhandle,
811 		.rpc_resp	= info,
812 	};
813 	int	status;
814 
815 	dprintk("NFS call  fsinfo\n");
816 	nfs_fattr_init(info->fattr);
817 	status = rpc_call_sync(client, &msg, 0);
818 	dprintk("NFS reply fsinfo: %d\n", status);
819 	return status;
820 }
821 
822 /*
823  * Bare-bones access to fsinfo: this is for nfs_get_root/nfs_get_sb via
824  * nfs_create_server
825  */
826 static int
nfs3_proc_fsinfo(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsinfo * info)827 nfs3_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
828 		   struct nfs_fsinfo *info)
829 {
830 	int	status;
831 
832 	status = do_proc_fsinfo(server->client, fhandle, info);
833 	if (status && server->nfs_client->cl_rpcclient != server->client)
834 		status = do_proc_fsinfo(server->nfs_client->cl_rpcclient, fhandle, info);
835 	return status;
836 }
837 
838 static int
nfs3_proc_pathconf(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_pathconf * info)839 nfs3_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
840 		   struct nfs_pathconf *info)
841 {
842 	struct rpc_message msg = {
843 		.rpc_proc	= &nfs3_procedures[NFS3PROC_PATHCONF],
844 		.rpc_argp	= fhandle,
845 		.rpc_resp	= info,
846 	};
847 	int	status;
848 
849 	dprintk("NFS call  pathconf\n");
850 	nfs_fattr_init(info->fattr);
851 	status = rpc_call_sync(server->client, &msg, 0);
852 	dprintk("NFS reply pathconf: %d\n", status);
853 	return status;
854 }
855 
856 #if IS_ENABLED(CONFIG_NFS_LOCALIO)
857 
858 static unsigned nfs3_localio_probe_throttle __read_mostly = 0;
859 module_param(nfs3_localio_probe_throttle, uint, 0644);
860 MODULE_PARM_DESC(nfs3_localio_probe_throttle,
861 		 "Probe for NFSv3 LOCALIO every N IO requests. Must be power-of-2, defaults to 0 (probing disabled).");
862 
nfs3_localio_probe(struct nfs_server * server)863 static void nfs3_localio_probe(struct nfs_server *server)
864 {
865 	struct nfs_client *clp = server->nfs_client;
866 
867 	/* Throttled to reduce nfs_local_probe_async() frequency */
868 	if (!nfs3_localio_probe_throttle || nfs_server_is_local(clp))
869 		return;
870 
871 	/*
872 	 * Try (re)enabling LOCALIO if isn't enabled -- admin deems
873 	 * it worthwhile to periodically check if LOCALIO possible by
874 	 * setting the 'nfs3_localio_probe_throttle' module parameter.
875 	 *
876 	 * This is useful if LOCALIO was previously enabled, but was
877 	 * disabled due to server restart, and IO has successfully
878 	 * completed in terms of normal RPC.
879 	 */
880 	if ((clp->cl_uuid.nfs3_localio_probe_count++ &
881 	     (nfs3_localio_probe_throttle - 1)) == 0) {
882 		if (!nfs_server_is_local(clp))
883 			nfs_local_probe_async(clp);
884 	}
885 }
886 
887 #else
nfs3_localio_probe(struct nfs_server * server)888 static void nfs3_localio_probe(struct nfs_server *server) {}
889 #endif
890 
nfs3_read_done(struct rpc_task * task,struct nfs_pgio_header * hdr)891 static int nfs3_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
892 {
893 	struct inode *inode = hdr->inode;
894 	struct nfs_server *server = NFS_SERVER(inode);
895 
896 	if (hdr->pgio_done_cb != NULL)
897 		return hdr->pgio_done_cb(task, hdr);
898 
899 	if (nfs3_async_handle_jukebox(task, inode))
900 		return -EAGAIN;
901 
902 	if (task->tk_status >= 0) {
903 		if (!server->read_hdrsize)
904 			cmpxchg(&server->read_hdrsize, 0, hdr->res.replen);
905 		nfs3_localio_probe(server);
906 	}
907 
908 	nfs_invalidate_atime(inode);
909 	nfs_refresh_inode(inode, &hdr->fattr);
910 	return 0;
911 }
912 
nfs3_proc_read_setup(struct nfs_pgio_header * hdr,struct rpc_message * msg)913 static void nfs3_proc_read_setup(struct nfs_pgio_header *hdr,
914 				 struct rpc_message *msg)
915 {
916 	msg->rpc_proc = &nfs3_procedures[NFS3PROC_READ];
917 	hdr->args.replen = NFS_SERVER(hdr->inode)->read_hdrsize;
918 }
919 
nfs3_proc_pgio_rpc_prepare(struct rpc_task * task,struct nfs_pgio_header * hdr)920 static int nfs3_proc_pgio_rpc_prepare(struct rpc_task *task,
921 				      struct nfs_pgio_header *hdr)
922 {
923 	rpc_call_start(task);
924 	return 0;
925 }
926 
nfs3_write_done(struct rpc_task * task,struct nfs_pgio_header * hdr)927 static int nfs3_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
928 {
929 	struct inode *inode = hdr->inode;
930 
931 	if (hdr->pgio_done_cb != NULL)
932 		return hdr->pgio_done_cb(task, hdr);
933 
934 	if (nfs3_async_handle_jukebox(task, inode))
935 		return -EAGAIN;
936 	if (task->tk_status >= 0) {
937 		nfs_writeback_update_inode(hdr);
938 		nfs3_localio_probe(NFS_SERVER(inode));
939 	}
940 	return 0;
941 }
942 
nfs3_proc_write_setup(struct nfs_pgio_header * hdr,struct rpc_message * msg,struct rpc_clnt ** clnt)943 static void nfs3_proc_write_setup(struct nfs_pgio_header *hdr,
944 				  struct rpc_message *msg,
945 				  struct rpc_clnt **clnt)
946 {
947 	msg->rpc_proc = &nfs3_procedures[NFS3PROC_WRITE];
948 }
949 
nfs3_proc_commit_rpc_prepare(struct rpc_task * task,struct nfs_commit_data * data)950 static void nfs3_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
951 {
952 	rpc_call_start(task);
953 }
954 
nfs3_commit_done(struct rpc_task * task,struct nfs_commit_data * data)955 static int nfs3_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
956 {
957 	if (data->commit_done_cb != NULL)
958 		return data->commit_done_cb(task, data);
959 
960 	if (nfs3_async_handle_jukebox(task, data->inode))
961 		return -EAGAIN;
962 	nfs_refresh_inode(data->inode, data->res.fattr);
963 	return 0;
964 }
965 
nfs3_proc_commit_setup(struct nfs_commit_data * data,struct rpc_message * msg,struct rpc_clnt ** clnt)966 static void nfs3_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
967 				   struct rpc_clnt **clnt)
968 {
969 	msg->rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT];
970 }
971 
nfs3_nlm_alloc_call(void * data)972 static void nfs3_nlm_alloc_call(void *data)
973 {
974 	struct nfs_lock_context *l_ctx = data;
975 	if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags)) {
976 		get_nfs_open_context(l_ctx->open_context);
977 		nfs_get_lock_context(l_ctx->open_context);
978 	}
979 }
980 
nfs3_nlm_unlock_prepare(struct rpc_task * task,void * data)981 static bool nfs3_nlm_unlock_prepare(struct rpc_task *task, void *data)
982 {
983 	struct nfs_lock_context *l_ctx = data;
984 	if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags))
985 		return nfs_async_iocounter_wait(task, l_ctx);
986 	return false;
987 
988 }
989 
nfs3_nlm_release_call(void * data)990 static void nfs3_nlm_release_call(void *data)
991 {
992 	struct nfs_lock_context *l_ctx = data;
993 	struct nfs_open_context *ctx;
994 	if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags)) {
995 		ctx = l_ctx->open_context;
996 		nfs_put_lock_context(l_ctx);
997 		put_nfs_open_context(ctx);
998 	}
999 }
1000 
1001 static const struct nlmclnt_operations nlmclnt_fl_close_lock_ops = {
1002 	.nlmclnt_alloc_call = nfs3_nlm_alloc_call,
1003 	.nlmclnt_unlock_prepare = nfs3_nlm_unlock_prepare,
1004 	.nlmclnt_release_call = nfs3_nlm_release_call,
1005 };
1006 
1007 static int
nfs3_proc_lock(struct file * filp,int cmd,struct file_lock * fl)1008 nfs3_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
1009 {
1010 	struct inode *inode = file_inode(filp);
1011 	struct nfs_lock_context *l_ctx = NULL;
1012 	struct nfs_open_context *ctx = nfs_file_open_context(filp);
1013 	int status;
1014 
1015 	if (fl->c.flc_flags & FL_CLOSE) {
1016 		l_ctx = nfs_get_lock_context(ctx);
1017 		if (IS_ERR(l_ctx))
1018 			l_ctx = NULL;
1019 		else
1020 			set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
1021 	}
1022 
1023 	status = nlmclnt_proc(NFS_SERVER(inode)->nlm_host, cmd, fl, l_ctx);
1024 
1025 	if (l_ctx)
1026 		nfs_put_lock_context(l_ctx);
1027 
1028 	return status;
1029 }
1030 
nfs3_have_delegation(struct inode * inode,fmode_t type,int flags)1031 static int nfs3_have_delegation(struct inode *inode, fmode_t type, int flags)
1032 {
1033 	return 0;
1034 }
1035 
nfs3_return_delegation(struct inode * inode)1036 static void nfs3_return_delegation(struct inode *inode)
1037 {
1038 	if (S_ISREG(inode->i_mode))
1039 		nfs_wb_all(inode);
1040 }
1041 
1042 static const struct inode_operations nfs3_dir_inode_operations = {
1043 	.create		= nfs_create,
1044 	.atomic_open	= nfs_atomic_open_v23,
1045 	.lookup		= nfs_lookup,
1046 	.link		= nfs_link,
1047 	.unlink		= nfs_unlink,
1048 	.symlink	= nfs_symlink,
1049 	.mkdir		= nfs_mkdir,
1050 	.rmdir		= nfs_rmdir,
1051 	.mknod		= nfs_mknod,
1052 	.rename		= nfs_rename,
1053 	.permission	= nfs_permission,
1054 	.getattr	= nfs_getattr,
1055 	.setattr	= nfs_setattr,
1056 #ifdef CONFIG_NFS_V3_ACL
1057 	.listxattr	= nfs3_listxattr,
1058 	.get_inode_acl	= nfs3_get_acl,
1059 	.set_acl	= nfs3_set_acl,
1060 #endif
1061 };
1062 
1063 static const struct inode_operations nfs3_file_inode_operations = {
1064 	.permission	= nfs_permission,
1065 	.getattr	= nfs_getattr,
1066 	.setattr	= nfs_setattr,
1067 #ifdef CONFIG_NFS_V3_ACL
1068 	.listxattr	= nfs3_listxattr,
1069 	.get_inode_acl	= nfs3_get_acl,
1070 	.set_acl	= nfs3_set_acl,
1071 #endif
1072 };
1073 
1074 const struct nfs_rpc_ops nfs_v3_clientops = {
1075 	.version	= 3,			/* protocol version */
1076 	.dentry_ops	= &nfs_dentry_operations,
1077 	.dir_inode_ops	= &nfs3_dir_inode_operations,
1078 	.file_inode_ops	= &nfs3_file_inode_operations,
1079 	.file_ops	= &nfs_file_operations,
1080 	.nlmclnt_ops	= &nlmclnt_fl_close_lock_ops,
1081 	.getroot	= nfs3_proc_get_root,
1082 	.submount	= nfs_submount,
1083 	.try_get_tree	= nfs_try_get_tree,
1084 	.getattr	= nfs3_proc_getattr,
1085 	.setattr	= nfs3_proc_setattr,
1086 	.lookup		= nfs3_proc_lookup,
1087 	.lookupp	= nfs3_proc_lookupp,
1088 	.access		= nfs3_proc_access,
1089 	.readlink	= nfs3_proc_readlink,
1090 	.create		= nfs3_proc_create,
1091 	.remove		= nfs3_proc_remove,
1092 	.unlink_setup	= nfs3_proc_unlink_setup,
1093 	.unlink_rpc_prepare = nfs3_proc_unlink_rpc_prepare,
1094 	.unlink_done	= nfs3_proc_unlink_done,
1095 	.rename_setup	= nfs3_proc_rename_setup,
1096 	.rename_rpc_prepare = nfs3_proc_rename_rpc_prepare,
1097 	.rename_done	= nfs3_proc_rename_done,
1098 	.link		= nfs3_proc_link,
1099 	.symlink	= nfs3_proc_symlink,
1100 	.mkdir		= nfs3_proc_mkdir,
1101 	.rmdir		= nfs3_proc_rmdir,
1102 	.readdir	= nfs3_proc_readdir,
1103 	.mknod		= nfs3_proc_mknod,
1104 	.statfs		= nfs3_proc_statfs,
1105 	.fsinfo		= nfs3_proc_fsinfo,
1106 	.pathconf	= nfs3_proc_pathconf,
1107 	.decode_dirent	= nfs3_decode_dirent,
1108 	.pgio_rpc_prepare = nfs3_proc_pgio_rpc_prepare,
1109 	.read_setup	= nfs3_proc_read_setup,
1110 	.read_done	= nfs3_read_done,
1111 	.write_setup	= nfs3_proc_write_setup,
1112 	.write_done	= nfs3_write_done,
1113 	.commit_setup	= nfs3_proc_commit_setup,
1114 	.commit_rpc_prepare = nfs3_proc_commit_rpc_prepare,
1115 	.commit_done	= nfs3_commit_done,
1116 	.lock		= nfs3_proc_lock,
1117 	.clear_acl_cache = forget_all_cached_acls,
1118 	.close_context	= nfs_close_context,
1119 	.have_delegation = nfs3_have_delegation,
1120 	.return_delegation = nfs3_return_delegation,
1121 	.alloc_client	= nfs_alloc_client,
1122 	.init_client	= nfs_init_client,
1123 	.free_client	= nfs_free_client,
1124 	.create_server	= nfs3_create_server,
1125 	.clone_server	= nfs3_clone_server,
1126 };
1127