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