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