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