xref: /openbmc/linux/fs/nfs/nfs3proc.c (revision d3964221)
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 {
106 	struct rpc_message msg = {
107 		.rpc_proc	= &nfs3_procedures[NFS3PROC_GETATTR],
108 		.rpc_argp	= fhandle,
109 		.rpc_resp	= fattr,
110 	};
111 	int	status;
112 
113 	dprintk("NFS call  getattr\n");
114 	nfs_fattr_init(fattr);
115 	status = rpc_call_sync(server->client, &msg, 0);
116 	dprintk("NFS reply getattr: %d\n", status);
117 	return status;
118 }
119 
120 static int
121 nfs3_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
122 			struct iattr *sattr)
123 {
124 	struct inode *inode = d_inode(dentry);
125 	struct nfs3_sattrargs	arg = {
126 		.fh		= NFS_FH(inode),
127 		.sattr		= sattr,
128 	};
129 	struct rpc_message msg = {
130 		.rpc_proc	= &nfs3_procedures[NFS3PROC_SETATTR],
131 		.rpc_argp	= &arg,
132 		.rpc_resp	= fattr,
133 	};
134 	int	status;
135 
136 	dprintk("NFS call  setattr\n");
137 	if (sattr->ia_valid & ATTR_FILE)
138 		msg.rpc_cred = nfs_file_cred(sattr->ia_file);
139 	nfs_fattr_init(fattr);
140 	status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
141 	if (status == 0)
142 		nfs_setattr_update_inode(inode, sattr, fattr);
143 	dprintk("NFS reply setattr: %d\n", status);
144 	return status;
145 }
146 
147 static int
148 nfs3_proc_lookup(struct inode *dir, const struct qstr *name,
149 		 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
150 		 struct nfs4_label *label)
151 {
152 	struct nfs3_diropargs	arg = {
153 		.fh		= NFS_FH(dir),
154 		.name		= name->name,
155 		.len		= name->len
156 	};
157 	struct nfs3_diropres	res = {
158 		.fh		= fhandle,
159 		.fattr		= fattr
160 	};
161 	struct rpc_message msg = {
162 		.rpc_proc	= &nfs3_procedures[NFS3PROC_LOOKUP],
163 		.rpc_argp	= &arg,
164 		.rpc_resp	= &res,
165 	};
166 	int			status;
167 
168 	dprintk("NFS call  lookup %s\n", name->name);
169 	res.dir_attr = nfs_alloc_fattr();
170 	if (res.dir_attr == NULL)
171 		return -ENOMEM;
172 
173 	nfs_fattr_init(fattr);
174 	status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
175 	nfs_refresh_inode(dir, res.dir_attr);
176 	if (status >= 0 && !(fattr->valid & NFS_ATTR_FATTR)) {
177 		msg.rpc_proc = &nfs3_procedures[NFS3PROC_GETATTR];
178 		msg.rpc_argp = fhandle;
179 		msg.rpc_resp = fattr;
180 		status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
181 	}
182 	nfs_free_fattr(res.dir_attr);
183 	dprintk("NFS reply lookup: %d\n", status);
184 	return status;
185 }
186 
187 static int nfs3_proc_access(struct inode *inode, struct nfs_access_entry *entry)
188 {
189 	struct nfs3_accessargs	arg = {
190 		.fh		= NFS_FH(inode),
191 	};
192 	struct nfs3_accessres	res;
193 	struct rpc_message msg = {
194 		.rpc_proc	= &nfs3_procedures[NFS3PROC_ACCESS],
195 		.rpc_argp	= &arg,
196 		.rpc_resp	= &res,
197 		.rpc_cred	= entry->cred,
198 	};
199 	int mode = entry->mask;
200 	int status = -ENOMEM;
201 
202 	dprintk("NFS call  access\n");
203 
204 	if (mode & MAY_READ)
205 		arg.access |= NFS3_ACCESS_READ;
206 	if (S_ISDIR(inode->i_mode)) {
207 		if (mode & MAY_WRITE)
208 			arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE;
209 		if (mode & MAY_EXEC)
210 			arg.access |= NFS3_ACCESS_LOOKUP;
211 	} else {
212 		if (mode & MAY_WRITE)
213 			arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND;
214 		if (mode & MAY_EXEC)
215 			arg.access |= NFS3_ACCESS_EXECUTE;
216 	}
217 
218 	res.fattr = nfs_alloc_fattr();
219 	if (res.fattr == NULL)
220 		goto out;
221 
222 	status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
223 	nfs_refresh_inode(inode, res.fattr);
224 	if (status == 0)
225 		nfs_access_set_mask(entry, res.access);
226 	nfs_free_fattr(res.fattr);
227 out:
228 	dprintk("NFS reply access: %d\n", status);
229 	return status;
230 }
231 
232 static int nfs3_proc_readlink(struct inode *inode, struct page *page,
233 		unsigned int pgbase, unsigned int pglen)
234 {
235 	struct nfs_fattr	*fattr;
236 	struct nfs3_readlinkargs args = {
237 		.fh		= NFS_FH(inode),
238 		.pgbase		= pgbase,
239 		.pglen		= pglen,
240 		.pages		= &page
241 	};
242 	struct rpc_message msg = {
243 		.rpc_proc	= &nfs3_procedures[NFS3PROC_READLINK],
244 		.rpc_argp	= &args,
245 	};
246 	int status = -ENOMEM;
247 
248 	dprintk("NFS call  readlink\n");
249 	fattr = nfs_alloc_fattr();
250 	if (fattr == NULL)
251 		goto out;
252 	msg.rpc_resp = fattr;
253 
254 	status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
255 	nfs_refresh_inode(inode, fattr);
256 	nfs_free_fattr(fattr);
257 out:
258 	dprintk("NFS reply readlink: %d\n", status);
259 	return status;
260 }
261 
262 struct nfs3_createdata {
263 	struct rpc_message msg;
264 	union {
265 		struct nfs3_createargs create;
266 		struct nfs3_mkdirargs mkdir;
267 		struct nfs3_symlinkargs symlink;
268 		struct nfs3_mknodargs mknod;
269 	} arg;
270 	struct nfs3_diropres res;
271 	struct nfs_fh fh;
272 	struct nfs_fattr fattr;
273 	struct nfs_fattr dir_attr;
274 };
275 
276 static struct nfs3_createdata *nfs3_alloc_createdata(void)
277 {
278 	struct nfs3_createdata *data;
279 
280 	data = kzalloc(sizeof(*data), GFP_KERNEL);
281 	if (data != NULL) {
282 		data->msg.rpc_argp = &data->arg;
283 		data->msg.rpc_resp = &data->res;
284 		data->res.fh = &data->fh;
285 		data->res.fattr = &data->fattr;
286 		data->res.dir_attr = &data->dir_attr;
287 		nfs_fattr_init(data->res.fattr);
288 		nfs_fattr_init(data->res.dir_attr);
289 	}
290 	return data;
291 }
292 
293 static int nfs3_do_create(struct inode *dir, struct dentry *dentry, struct nfs3_createdata *data)
294 {
295 	int status;
296 
297 	status = rpc_call_sync(NFS_CLIENT(dir), &data->msg, 0);
298 	nfs_post_op_update_inode(dir, data->res.dir_attr);
299 	if (status == 0)
300 		status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, NULL);
301 	return status;
302 }
303 
304 static void nfs3_free_createdata(struct nfs3_createdata *data)
305 {
306 	kfree(data);
307 }
308 
309 /*
310  * Create a regular file.
311  */
312 static int
313 nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
314 		 int flags)
315 {
316 	struct posix_acl *default_acl, *acl;
317 	struct nfs3_createdata *data;
318 	int status = -ENOMEM;
319 
320 	dprintk("NFS call  create %pd\n", dentry);
321 
322 	data = nfs3_alloc_createdata();
323 	if (data == NULL)
324 		goto out;
325 
326 	data->msg.rpc_proc = &nfs3_procedures[NFS3PROC_CREATE];
327 	data->arg.create.fh = NFS_FH(dir);
328 	data->arg.create.name = dentry->d_name.name;
329 	data->arg.create.len = dentry->d_name.len;
330 	data->arg.create.sattr = sattr;
331 
332 	data->arg.create.createmode = NFS3_CREATE_UNCHECKED;
333 	if (flags & O_EXCL) {
334 		data->arg.create.createmode  = NFS3_CREATE_EXCLUSIVE;
335 		data->arg.create.verifier[0] = cpu_to_be32(jiffies);
336 		data->arg.create.verifier[1] = cpu_to_be32(current->pid);
337 	}
338 
339 	status = posix_acl_create(dir, &sattr->ia_mode, &default_acl, &acl);
340 	if (status)
341 		goto out;
342 
343 	for (;;) {
344 		status = nfs3_do_create(dir, dentry, data);
345 
346 		if (status != -ENOTSUPP)
347 			break;
348 		/* If the server doesn't support the exclusive creation
349 		 * semantics, try again with simple 'guarded' mode. */
350 		switch (data->arg.create.createmode) {
351 			case NFS3_CREATE_EXCLUSIVE:
352 				data->arg.create.createmode = NFS3_CREATE_GUARDED;
353 				break;
354 
355 			case NFS3_CREATE_GUARDED:
356 				data->arg.create.createmode = NFS3_CREATE_UNCHECKED;
357 				break;
358 
359 			case NFS3_CREATE_UNCHECKED:
360 				goto out;
361 		}
362 		nfs_fattr_init(data->res.dir_attr);
363 		nfs_fattr_init(data->res.fattr);
364 	}
365 
366 	if (status != 0)
367 		goto out_release_acls;
368 
369 	/* When we created the file with exclusive semantics, make
370 	 * sure we set the attributes afterwards. */
371 	if (data->arg.create.createmode == NFS3_CREATE_EXCLUSIVE) {
372 		dprintk("NFS call  setattr (post-create)\n");
373 
374 		if (!(sattr->ia_valid & ATTR_ATIME_SET))
375 			sattr->ia_valid |= ATTR_ATIME;
376 		if (!(sattr->ia_valid & ATTR_MTIME_SET))
377 			sattr->ia_valid |= ATTR_MTIME;
378 
379 		/* Note: we could use a guarded setattr here, but I'm
380 		 * not sure this buys us anything (and I'd have
381 		 * to revamp the NFSv3 XDR code) */
382 		status = nfs3_proc_setattr(dentry, data->res.fattr, sattr);
383 		nfs_post_op_update_inode(d_inode(dentry), data->res.fattr);
384 		dprintk("NFS reply setattr (post-create): %d\n", status);
385 		if (status != 0)
386 			goto out_release_acls;
387 	}
388 
389 	status = nfs3_proc_setacls(d_inode(dentry), acl, default_acl);
390 
391 out_release_acls:
392 	posix_acl_release(acl);
393 	posix_acl_release(default_acl);
394 out:
395 	nfs3_free_createdata(data);
396 	dprintk("NFS reply create: %d\n", status);
397 	return status;
398 }
399 
400 static int
401 nfs3_proc_remove(struct inode *dir, const struct qstr *name)
402 {
403 	struct nfs_removeargs arg = {
404 		.fh = NFS_FH(dir),
405 		.name = *name,
406 	};
407 	struct nfs_removeres res;
408 	struct rpc_message msg = {
409 		.rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE],
410 		.rpc_argp = &arg,
411 		.rpc_resp = &res,
412 	};
413 	int status = -ENOMEM;
414 
415 	dprintk("NFS call  remove %s\n", name->name);
416 	res.dir_attr = nfs_alloc_fattr();
417 	if (res.dir_attr == NULL)
418 		goto out;
419 
420 	status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
421 	nfs_post_op_update_inode(dir, res.dir_attr);
422 	nfs_free_fattr(res.dir_attr);
423 out:
424 	dprintk("NFS reply remove: %d\n", status);
425 	return status;
426 }
427 
428 static void
429 nfs3_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
430 {
431 	msg->rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE];
432 }
433 
434 static void nfs3_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
435 {
436 	rpc_call_start(task);
437 }
438 
439 static int
440 nfs3_proc_unlink_done(struct rpc_task *task, struct inode *dir)
441 {
442 	struct nfs_removeres *res;
443 	if (nfs3_async_handle_jukebox(task, dir))
444 		return 0;
445 	res = task->tk_msg.rpc_resp;
446 	nfs_post_op_update_inode(dir, res->dir_attr);
447 	return 1;
448 }
449 
450 static void
451 nfs3_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
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 
797 	if (hdr->pgio_done_cb != NULL)
798 		return hdr->pgio_done_cb(task, hdr);
799 
800 	if (nfs3_async_handle_jukebox(task, inode))
801 		return -EAGAIN;
802 
803 	nfs_invalidate_atime(inode);
804 	nfs_refresh_inode(inode, &hdr->fattr);
805 	return 0;
806 }
807 
808 static void nfs3_proc_read_setup(struct nfs_pgio_header *hdr,
809 				 struct rpc_message *msg)
810 {
811 	msg->rpc_proc = &nfs3_procedures[NFS3PROC_READ];
812 }
813 
814 static int nfs3_proc_pgio_rpc_prepare(struct rpc_task *task,
815 				      struct nfs_pgio_header *hdr)
816 {
817 	rpc_call_start(task);
818 	return 0;
819 }
820 
821 static int nfs3_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
822 {
823 	struct inode *inode = hdr->inode;
824 
825 	if (hdr->pgio_done_cb != NULL)
826 		return hdr->pgio_done_cb(task, hdr);
827 
828 	if (nfs3_async_handle_jukebox(task, inode))
829 		return -EAGAIN;
830 	if (task->tk_status >= 0)
831 		nfs_writeback_update_inode(hdr);
832 	return 0;
833 }
834 
835 static void nfs3_proc_write_setup(struct nfs_pgio_header *hdr,
836 				  struct rpc_message *msg)
837 {
838 	msg->rpc_proc = &nfs3_procedures[NFS3PROC_WRITE];
839 }
840 
841 static void nfs3_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
842 {
843 	rpc_call_start(task);
844 }
845 
846 static int nfs3_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
847 {
848 	if (data->commit_done_cb != NULL)
849 		return data->commit_done_cb(task, data);
850 
851 	if (nfs3_async_handle_jukebox(task, data->inode))
852 		return -EAGAIN;
853 	nfs_refresh_inode(data->inode, data->res.fattr);
854 	return 0;
855 }
856 
857 static void nfs3_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
858 {
859 	msg->rpc_proc = &nfs3_procedures[NFS3PROC_COMMIT];
860 }
861 
862 static void nfs3_nlm_alloc_call(void *data)
863 {
864 	struct nfs_lock_context *l_ctx = data;
865 	if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags)) {
866 		get_nfs_open_context(l_ctx->open_context);
867 		nfs_get_lock_context(l_ctx->open_context);
868 	}
869 }
870 
871 static bool nfs3_nlm_unlock_prepare(struct rpc_task *task, void *data)
872 {
873 	struct nfs_lock_context *l_ctx = data;
874 	if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags))
875 		return nfs_async_iocounter_wait(task, l_ctx);
876 	return false;
877 
878 }
879 
880 static void nfs3_nlm_release_call(void *data)
881 {
882 	struct nfs_lock_context *l_ctx = data;
883 	struct nfs_open_context *ctx;
884 	if (l_ctx && test_bit(NFS_CONTEXT_UNLOCK, &l_ctx->open_context->flags)) {
885 		ctx = l_ctx->open_context;
886 		nfs_put_lock_context(l_ctx);
887 		put_nfs_open_context(ctx);
888 	}
889 }
890 
891 const struct nlmclnt_operations nlmclnt_fl_close_lock_ops = {
892 	.nlmclnt_alloc_call = nfs3_nlm_alloc_call,
893 	.nlmclnt_unlock_prepare = nfs3_nlm_unlock_prepare,
894 	.nlmclnt_release_call = nfs3_nlm_release_call,
895 };
896 
897 static int
898 nfs3_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
899 {
900 	struct inode *inode = file_inode(filp);
901 	struct nfs_lock_context *l_ctx = NULL;
902 	struct nfs_open_context *ctx = nfs_file_open_context(filp);
903 	int status;
904 
905 	if (fl->fl_flags & FL_CLOSE) {
906 		l_ctx = nfs_get_lock_context(ctx);
907 		if (IS_ERR(l_ctx))
908 			l_ctx = NULL;
909 		else
910 			set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
911 	}
912 
913 	status = nlmclnt_proc(NFS_SERVER(inode)->nlm_host, cmd, fl, l_ctx);
914 
915 	if (l_ctx)
916 		nfs_put_lock_context(l_ctx);
917 
918 	return status;
919 }
920 
921 static int nfs3_have_delegation(struct inode *inode, fmode_t flags)
922 {
923 	return 0;
924 }
925 
926 static int nfs3_return_delegation(struct inode *inode)
927 {
928 	nfs_wb_all(inode);
929 	return 0;
930 }
931 
932 static const struct inode_operations nfs3_dir_inode_operations = {
933 	.create		= nfs_create,
934 	.lookup		= nfs_lookup,
935 	.link		= nfs_link,
936 	.unlink		= nfs_unlink,
937 	.symlink	= nfs_symlink,
938 	.mkdir		= nfs_mkdir,
939 	.rmdir		= nfs_rmdir,
940 	.mknod		= nfs_mknod,
941 	.rename		= nfs_rename,
942 	.permission	= nfs_permission,
943 	.getattr	= nfs_getattr,
944 	.setattr	= nfs_setattr,
945 #ifdef CONFIG_NFS_V3_ACL
946 	.listxattr	= nfs3_listxattr,
947 	.get_acl	= nfs3_get_acl,
948 	.set_acl	= nfs3_set_acl,
949 #endif
950 };
951 
952 static const struct inode_operations nfs3_file_inode_operations = {
953 	.permission	= nfs_permission,
954 	.getattr	= nfs_getattr,
955 	.setattr	= nfs_setattr,
956 #ifdef CONFIG_NFS_V3_ACL
957 	.listxattr	= nfs3_listxattr,
958 	.get_acl	= nfs3_get_acl,
959 	.set_acl	= nfs3_set_acl,
960 #endif
961 };
962 
963 const struct nfs_rpc_ops nfs_v3_clientops = {
964 	.version	= 3,			/* protocol version */
965 	.dentry_ops	= &nfs_dentry_operations,
966 	.dir_inode_ops	= &nfs3_dir_inode_operations,
967 	.file_inode_ops	= &nfs3_file_inode_operations,
968 	.file_ops	= &nfs_file_operations,
969 	.nlmclnt_ops	= &nlmclnt_fl_close_lock_ops,
970 	.getroot	= nfs3_proc_get_root,
971 	.submount	= nfs_submount,
972 	.try_mount	= nfs_try_mount,
973 	.getattr	= nfs3_proc_getattr,
974 	.setattr	= nfs3_proc_setattr,
975 	.lookup		= nfs3_proc_lookup,
976 	.access		= nfs3_proc_access,
977 	.readlink	= nfs3_proc_readlink,
978 	.create		= nfs3_proc_create,
979 	.remove		= nfs3_proc_remove,
980 	.unlink_setup	= nfs3_proc_unlink_setup,
981 	.unlink_rpc_prepare = nfs3_proc_unlink_rpc_prepare,
982 	.unlink_done	= nfs3_proc_unlink_done,
983 	.rename_setup	= nfs3_proc_rename_setup,
984 	.rename_rpc_prepare = nfs3_proc_rename_rpc_prepare,
985 	.rename_done	= nfs3_proc_rename_done,
986 	.link		= nfs3_proc_link,
987 	.symlink	= nfs3_proc_symlink,
988 	.mkdir		= nfs3_proc_mkdir,
989 	.rmdir		= nfs3_proc_rmdir,
990 	.readdir	= nfs3_proc_readdir,
991 	.mknod		= nfs3_proc_mknod,
992 	.statfs		= nfs3_proc_statfs,
993 	.fsinfo		= nfs3_proc_fsinfo,
994 	.pathconf	= nfs3_proc_pathconf,
995 	.decode_dirent	= nfs3_decode_dirent,
996 	.pgio_rpc_prepare = nfs3_proc_pgio_rpc_prepare,
997 	.read_setup	= nfs3_proc_read_setup,
998 	.read_done	= nfs3_read_done,
999 	.write_setup	= nfs3_proc_write_setup,
1000 	.write_done	= nfs3_write_done,
1001 	.commit_setup	= nfs3_proc_commit_setup,
1002 	.commit_rpc_prepare = nfs3_proc_commit_rpc_prepare,
1003 	.commit_done	= nfs3_commit_done,
1004 	.lock		= nfs3_proc_lock,
1005 	.clear_acl_cache = forget_all_cached_acls,
1006 	.close_context	= nfs_close_context,
1007 	.have_delegation = nfs3_have_delegation,
1008 	.return_delegation = nfs3_return_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