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