xref: /linux/fs/nfs/nfs3proc.c (revision d8327c784b51b57dac2c26cfad87dce0d68dfd98)
1 /*
2  *  linux/fs/nfs/nfs3proc.c
3  *
4  *  Client-side NFSv3 procedures stubs.
5  *
6  *  Copyright (C) 1997, Olaf Kirch
7  */
8 
9 #include <linux/mm.h>
10 #include <linux/utsname.h>
11 #include <linux/errno.h>
12 #include <linux/string.h>
13 #include <linux/sunrpc/clnt.h>
14 #include <linux/nfs.h>
15 #include <linux/nfs3.h>
16 #include <linux/nfs_fs.h>
17 #include <linux/nfs_page.h>
18 #include <linux/lockd/bind.h>
19 #include <linux/smp_lock.h>
20 #include <linux/nfs_mount.h>
21 
22 #define NFSDBG_FACILITY		NFSDBG_PROC
23 
24 extern struct rpc_procinfo nfs3_procedures[];
25 
26 /* A wrapper to handle the EJUKEBOX error message */
27 static int
28 nfs3_rpc_wrapper(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
29 {
30 	sigset_t oldset;
31 	int res;
32 	rpc_clnt_sigmask(clnt, &oldset);
33 	do {
34 		res = rpc_call_sync(clnt, msg, flags);
35 		if (res != -EJUKEBOX)
36 			break;
37 		schedule_timeout_interruptible(NFS_JUKEBOX_RETRY_TIME);
38 		res = -ERESTARTSYS;
39 	} while (!signalled());
40 	rpc_clnt_sigunmask(clnt, &oldset);
41 	return res;
42 }
43 
44 static inline int
45 nfs3_rpc_call_wrapper(struct rpc_clnt *clnt, u32 proc, void *argp, void *resp, int flags)
46 {
47 	struct rpc_message msg = {
48 		.rpc_proc	= &clnt->cl_procinfo[proc],
49 		.rpc_argp	= argp,
50 		.rpc_resp	= resp,
51 	};
52 	return nfs3_rpc_wrapper(clnt, &msg, flags);
53 }
54 
55 #define rpc_call(clnt, proc, argp, resp, flags) \
56 		nfs3_rpc_call_wrapper(clnt, proc, argp, resp, flags)
57 #define rpc_call_sync(clnt, msg, flags) \
58 		nfs3_rpc_wrapper(clnt, msg, flags)
59 
60 static int
61 nfs3_async_handle_jukebox(struct rpc_task *task)
62 {
63 	if (task->tk_status != -EJUKEBOX)
64 		return 0;
65 	task->tk_status = 0;
66 	rpc_restart_call(task);
67 	rpc_delay(task, NFS_JUKEBOX_RETRY_TIME);
68 	return 1;
69 }
70 
71 static int
72 do_proc_get_root(struct rpc_clnt *client, struct nfs_fh *fhandle,
73 		 struct nfs_fsinfo *info)
74 {
75 	int	status;
76 
77 	dprintk("%s: call  fsinfo\n", __FUNCTION__);
78 	nfs_fattr_init(info->fattr);
79 	status = rpc_call(client, NFS3PROC_FSINFO, fhandle, info, 0);
80 	dprintk("%s: reply fsinfo: %d\n", __FUNCTION__, status);
81 	if (!(info->fattr->valid & NFS_ATTR_FATTR)) {
82 		status = rpc_call(client, NFS3PROC_GETATTR, fhandle, info->fattr, 0);
83 		dprintk("%s: reply getattr: %d\n", __FUNCTION__, status);
84 	}
85 	return status;
86 }
87 
88 /*
89  * Bare-bones access to getattr: this is for nfs_read_super.
90  */
91 static int
92 nfs3_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
93 		   struct nfs_fsinfo *info)
94 {
95 	int	status;
96 
97 	status = do_proc_get_root(server->client, fhandle, info);
98 	if (status && server->client_sys != server->client)
99 		status = do_proc_get_root(server->client_sys, fhandle, info);
100 	return status;
101 }
102 
103 /*
104  * One function for each procedure in the NFS protocol.
105  */
106 static int
107 nfs3_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
108 		struct nfs_fattr *fattr)
109 {
110 	int	status;
111 
112 	dprintk("NFS call  getattr\n");
113 	nfs_fattr_init(fattr);
114 	status = rpc_call(server->client, NFS3PROC_GETATTR,
115 			  fhandle, fattr, 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 = dentry->d_inode;
125 	struct nfs3_sattrargs	arg = {
126 		.fh		= NFS_FH(inode),
127 		.sattr		= sattr,
128 	};
129 	int	status;
130 
131 	dprintk("NFS call  setattr\n");
132 	nfs_fattr_init(fattr);
133 	status = rpc_call(NFS_CLIENT(inode), NFS3PROC_SETATTR, &arg, fattr, 0);
134 	if (status == 0)
135 		nfs_setattr_update_inode(inode, sattr);
136 	dprintk("NFS reply setattr: %d\n", status);
137 	return status;
138 }
139 
140 static int
141 nfs3_proc_lookup(struct inode *dir, struct qstr *name,
142 		 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
143 {
144 	struct nfs_fattr	dir_attr;
145 	struct nfs3_diropargs	arg = {
146 		.fh		= NFS_FH(dir),
147 		.name		= name->name,
148 		.len		= name->len
149 	};
150 	struct nfs3_diropres	res = {
151 		.dir_attr	= &dir_attr,
152 		.fh		= fhandle,
153 		.fattr		= fattr
154 	};
155 	int			status;
156 
157 	dprintk("NFS call  lookup %s\n", name->name);
158 	nfs_fattr_init(&dir_attr);
159 	nfs_fattr_init(fattr);
160 	status = rpc_call(NFS_CLIENT(dir), NFS3PROC_LOOKUP, &arg, &res, 0);
161 	if (status >= 0 && !(fattr->valid & NFS_ATTR_FATTR))
162 		status = rpc_call(NFS_CLIENT(dir), NFS3PROC_GETATTR,
163 			 fhandle, fattr, 0);
164 	dprintk("NFS reply lookup: %d\n", status);
165 	if (status >= 0)
166 		status = nfs_refresh_inode(dir, &dir_attr);
167 	return status;
168 }
169 
170 static int nfs3_proc_access(struct inode *inode, struct nfs_access_entry *entry)
171 {
172 	struct nfs_fattr	fattr;
173 	struct nfs3_accessargs	arg = {
174 		.fh		= NFS_FH(inode),
175 	};
176 	struct nfs3_accessres	res = {
177 		.fattr		= &fattr,
178 	};
179 	struct rpc_message msg = {
180 		.rpc_proc	= &nfs3_procedures[NFS3PROC_ACCESS],
181 		.rpc_argp	= &arg,
182 		.rpc_resp	= &res,
183 		.rpc_cred	= entry->cred
184 	};
185 	int mode = entry->mask;
186 	int status;
187 
188 	dprintk("NFS call  access\n");
189 
190 	if (mode & MAY_READ)
191 		arg.access |= NFS3_ACCESS_READ;
192 	if (S_ISDIR(inode->i_mode)) {
193 		if (mode & MAY_WRITE)
194 			arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE;
195 		if (mode & MAY_EXEC)
196 			arg.access |= NFS3_ACCESS_LOOKUP;
197 	} else {
198 		if (mode & MAY_WRITE)
199 			arg.access |= NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND;
200 		if (mode & MAY_EXEC)
201 			arg.access |= NFS3_ACCESS_EXECUTE;
202 	}
203 	nfs_fattr_init(&fattr);
204 	status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
205 	nfs_refresh_inode(inode, &fattr);
206 	if (status == 0) {
207 		entry->mask = 0;
208 		if (res.access & NFS3_ACCESS_READ)
209 			entry->mask |= MAY_READ;
210 		if (res.access & (NFS3_ACCESS_MODIFY | NFS3_ACCESS_EXTEND | NFS3_ACCESS_DELETE))
211 			entry->mask |= MAY_WRITE;
212 		if (res.access & (NFS3_ACCESS_LOOKUP|NFS3_ACCESS_EXECUTE))
213 			entry->mask |= MAY_EXEC;
214 	}
215 	dprintk("NFS reply access: %d\n", status);
216 	return status;
217 }
218 
219 static int nfs3_proc_readlink(struct inode *inode, struct page *page,
220 		unsigned int pgbase, unsigned int pglen)
221 {
222 	struct nfs_fattr	fattr;
223 	struct nfs3_readlinkargs args = {
224 		.fh		= NFS_FH(inode),
225 		.pgbase		= pgbase,
226 		.pglen		= pglen,
227 		.pages		= &page
228 	};
229 	int			status;
230 
231 	dprintk("NFS call  readlink\n");
232 	nfs_fattr_init(&fattr);
233 	status = rpc_call(NFS_CLIENT(inode), NFS3PROC_READLINK,
234 			  &args, &fattr, 0);
235 	nfs_refresh_inode(inode, &fattr);
236 	dprintk("NFS reply readlink: %d\n", status);
237 	return status;
238 }
239 
240 static int nfs3_proc_read(struct nfs_read_data *rdata)
241 {
242 	int			flags = rdata->flags;
243 	struct inode *		inode = rdata->inode;
244 	struct nfs_fattr *	fattr = rdata->res.fattr;
245 	struct rpc_message	msg = {
246 		.rpc_proc	= &nfs3_procedures[NFS3PROC_READ],
247 		.rpc_argp	= &rdata->args,
248 		.rpc_resp	= &rdata->res,
249 		.rpc_cred	= rdata->cred,
250 	};
251 	int			status;
252 
253 	dprintk("NFS call  read %d @ %Ld\n", rdata->args.count,
254 			(long long) rdata->args.offset);
255 	nfs_fattr_init(fattr);
256 	status = rpc_call_sync(NFS_CLIENT(inode), &msg, flags);
257 	if (status >= 0)
258 		nfs_refresh_inode(inode, fattr);
259 	dprintk("NFS reply read: %d\n", status);
260 	return status;
261 }
262 
263 static int nfs3_proc_write(struct nfs_write_data *wdata)
264 {
265 	int			rpcflags = wdata->flags;
266 	struct inode *		inode = wdata->inode;
267 	struct nfs_fattr *	fattr = wdata->res.fattr;
268 	struct rpc_message	msg = {
269 		.rpc_proc	= &nfs3_procedures[NFS3PROC_WRITE],
270 		.rpc_argp	= &wdata->args,
271 		.rpc_resp	= &wdata->res,
272 		.rpc_cred	= wdata->cred,
273 	};
274 	int			status;
275 
276 	dprintk("NFS call  write %d @ %Ld\n", wdata->args.count,
277 			(long long) wdata->args.offset);
278 	nfs_fattr_init(fattr);
279 	status = rpc_call_sync(NFS_CLIENT(inode), &msg, rpcflags);
280 	if (status >= 0)
281 		nfs_post_op_update_inode(inode, fattr);
282 	dprintk("NFS reply write: %d\n", status);
283 	return status < 0? status : wdata->res.count;
284 }
285 
286 static int nfs3_proc_commit(struct nfs_write_data *cdata)
287 {
288 	struct inode *		inode = cdata->inode;
289 	struct nfs_fattr *	fattr = cdata->res.fattr;
290 	struct rpc_message	msg = {
291 		.rpc_proc	= &nfs3_procedures[NFS3PROC_COMMIT],
292 		.rpc_argp	= &cdata->args,
293 		.rpc_resp	= &cdata->res,
294 		.rpc_cred	= cdata->cred,
295 	};
296 	int			status;
297 
298 	dprintk("NFS call  commit %d @ %Ld\n", cdata->args.count,
299 			(long long) cdata->args.offset);
300 	nfs_fattr_init(fattr);
301 	status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
302 	if (status >= 0)
303 		nfs_post_op_update_inode(inode, fattr);
304 	dprintk("NFS reply commit: %d\n", status);
305 	return status;
306 }
307 
308 /*
309  * Create a regular file.
310  * For now, we don't implement O_EXCL.
311  */
312 static int
313 nfs3_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
314 		 int flags, struct nameidata *nd)
315 {
316 	struct nfs_fh		fhandle;
317 	struct nfs_fattr	fattr;
318 	struct nfs_fattr	dir_attr;
319 	struct nfs3_createargs	arg = {
320 		.fh		= NFS_FH(dir),
321 		.name		= dentry->d_name.name,
322 		.len		= dentry->d_name.len,
323 		.sattr		= sattr,
324 	};
325 	struct nfs3_diropres	res = {
326 		.dir_attr	= &dir_attr,
327 		.fh		= &fhandle,
328 		.fattr		= &fattr
329 	};
330 	mode_t mode = sattr->ia_mode;
331 	int status;
332 
333 	dprintk("NFS call  create %s\n", dentry->d_name.name);
334 	arg.createmode = NFS3_CREATE_UNCHECKED;
335 	if (flags & O_EXCL) {
336 		arg.createmode  = NFS3_CREATE_EXCLUSIVE;
337 		arg.verifier[0] = jiffies;
338 		arg.verifier[1] = current->pid;
339 	}
340 
341 	sattr->ia_mode &= ~current->fs->umask;
342 
343 again:
344 	nfs_fattr_init(&dir_attr);
345 	nfs_fattr_init(&fattr);
346 	status = rpc_call(NFS_CLIENT(dir), NFS3PROC_CREATE, &arg, &res, 0);
347 	nfs_post_op_update_inode(dir, &dir_attr);
348 
349 	/* If the server doesn't support the exclusive creation semantics,
350 	 * try again with simple 'guarded' mode. */
351 	if (status == NFSERR_NOTSUPP) {
352 		switch (arg.createmode) {
353 			case NFS3_CREATE_EXCLUSIVE:
354 				arg.createmode = NFS3_CREATE_GUARDED;
355 				break;
356 
357 			case NFS3_CREATE_GUARDED:
358 				arg.createmode = NFS3_CREATE_UNCHECKED;
359 				break;
360 
361 			case NFS3_CREATE_UNCHECKED:
362 				goto out;
363 		}
364 		goto again;
365 	}
366 
367 	if (status == 0)
368 		status = nfs_instantiate(dentry, &fhandle, &fattr);
369 	if (status != 0)
370 		goto out;
371 
372 	/* When we created the file with exclusive semantics, make
373 	 * sure we set the attributes afterwards. */
374 	if (arg.createmode == NFS3_CREATE_EXCLUSIVE) {
375 		dprintk("NFS call  setattr (post-create)\n");
376 
377 		if (!(sattr->ia_valid & ATTR_ATIME_SET))
378 			sattr->ia_valid |= ATTR_ATIME;
379 		if (!(sattr->ia_valid & ATTR_MTIME_SET))
380 			sattr->ia_valid |= ATTR_MTIME;
381 
382 		/* Note: we could use a guarded setattr here, but I'm
383 		 * not sure this buys us anything (and I'd have
384 		 * to revamp the NFSv3 XDR code) */
385 		status = nfs3_proc_setattr(dentry, &fattr, sattr);
386 		if (status == 0)
387 			nfs_setattr_update_inode(dentry->d_inode, sattr);
388 		nfs_refresh_inode(dentry->d_inode, &fattr);
389 		dprintk("NFS reply setattr (post-create): %d\n", status);
390 	}
391 	if (status != 0)
392 		goto out;
393 	status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
394 out:
395 	dprintk("NFS reply create: %d\n", status);
396 	return status;
397 }
398 
399 static int
400 nfs3_proc_remove(struct inode *dir, struct qstr *name)
401 {
402 	struct nfs_fattr	dir_attr;
403 	struct nfs3_diropargs	arg = {
404 		.fh		= NFS_FH(dir),
405 		.name		= name->name,
406 		.len		= name->len
407 	};
408 	struct rpc_message	msg = {
409 		.rpc_proc	= &nfs3_procedures[NFS3PROC_REMOVE],
410 		.rpc_argp	= &arg,
411 		.rpc_resp	= &dir_attr,
412 	};
413 	int			status;
414 
415 	dprintk("NFS call  remove %s\n", name->name);
416 	nfs_fattr_init(&dir_attr);
417 	status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
418 	nfs_post_op_update_inode(dir, &dir_attr);
419 	dprintk("NFS reply remove: %d\n", status);
420 	return status;
421 }
422 
423 static int
424 nfs3_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir, struct qstr *name)
425 {
426 	struct unlinkxdr {
427 		struct nfs3_diropargs arg;
428 		struct nfs_fattr res;
429 	} *ptr;
430 
431 	ptr = (struct unlinkxdr *)kmalloc(sizeof(*ptr), GFP_KERNEL);
432 	if (!ptr)
433 		return -ENOMEM;
434 	ptr->arg.fh = NFS_FH(dir->d_inode);
435 	ptr->arg.name = name->name;
436 	ptr->arg.len = name->len;
437 	nfs_fattr_init(&ptr->res);
438 	msg->rpc_proc = &nfs3_procedures[NFS3PROC_REMOVE];
439 	msg->rpc_argp = &ptr->arg;
440 	msg->rpc_resp = &ptr->res;
441 	return 0;
442 }
443 
444 static int
445 nfs3_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
446 {
447 	struct rpc_message *msg = &task->tk_msg;
448 	struct nfs_fattr	*dir_attr;
449 
450 	if (nfs3_async_handle_jukebox(task))
451 		return 1;
452 	if (msg->rpc_argp) {
453 		dir_attr = (struct nfs_fattr*)msg->rpc_resp;
454 		nfs_post_op_update_inode(dir->d_inode, dir_attr);
455 		kfree(msg->rpc_argp);
456 	}
457 	return 0;
458 }
459 
460 static int
461 nfs3_proc_rename(struct inode *old_dir, struct qstr *old_name,
462 		 struct inode *new_dir, struct qstr *new_name)
463 {
464 	struct nfs_fattr	old_dir_attr, new_dir_attr;
465 	struct nfs3_renameargs	arg = {
466 		.fromfh		= NFS_FH(old_dir),
467 		.fromname	= old_name->name,
468 		.fromlen	= old_name->len,
469 		.tofh		= NFS_FH(new_dir),
470 		.toname		= new_name->name,
471 		.tolen		= new_name->len
472 	};
473 	struct nfs3_renameres	res = {
474 		.fromattr	= &old_dir_attr,
475 		.toattr		= &new_dir_attr
476 	};
477 	int			status;
478 
479 	dprintk("NFS call  rename %s -> %s\n", old_name->name, new_name->name);
480 	nfs_fattr_init(&old_dir_attr);
481 	nfs_fattr_init(&new_dir_attr);
482 	status = rpc_call(NFS_CLIENT(old_dir), NFS3PROC_RENAME, &arg, &res, 0);
483 	nfs_post_op_update_inode(old_dir, &old_dir_attr);
484 	nfs_post_op_update_inode(new_dir, &new_dir_attr);
485 	dprintk("NFS reply rename: %d\n", status);
486 	return status;
487 }
488 
489 static int
490 nfs3_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
491 {
492 	struct nfs_fattr	dir_attr, fattr;
493 	struct nfs3_linkargs	arg = {
494 		.fromfh		= NFS_FH(inode),
495 		.tofh		= NFS_FH(dir),
496 		.toname		= name->name,
497 		.tolen		= name->len
498 	};
499 	struct nfs3_linkres	res = {
500 		.dir_attr	= &dir_attr,
501 		.fattr		= &fattr
502 	};
503 	int			status;
504 
505 	dprintk("NFS call  link %s\n", name->name);
506 	nfs_fattr_init(&dir_attr);
507 	nfs_fattr_init(&fattr);
508 	status = rpc_call(NFS_CLIENT(inode), NFS3PROC_LINK, &arg, &res, 0);
509 	nfs_post_op_update_inode(dir, &dir_attr);
510 	nfs_post_op_update_inode(inode, &fattr);
511 	dprintk("NFS reply link: %d\n", status);
512 	return status;
513 }
514 
515 static int
516 nfs3_proc_symlink(struct inode *dir, struct qstr *name, struct qstr *path,
517 		  struct iattr *sattr, struct nfs_fh *fhandle,
518 		  struct nfs_fattr *fattr)
519 {
520 	struct nfs_fattr	dir_attr;
521 	struct nfs3_symlinkargs	arg = {
522 		.fromfh		= NFS_FH(dir),
523 		.fromname	= name->name,
524 		.fromlen	= name->len,
525 		.topath		= path->name,
526 		.tolen		= path->len,
527 		.sattr		= sattr
528 	};
529 	struct nfs3_diropres	res = {
530 		.dir_attr	= &dir_attr,
531 		.fh		= fhandle,
532 		.fattr		= fattr
533 	};
534 	int			status;
535 
536 	if (path->len > NFS3_MAXPATHLEN)
537 		return -ENAMETOOLONG;
538 	dprintk("NFS call  symlink %s -> %s\n", name->name, path->name);
539 	nfs_fattr_init(&dir_attr);
540 	nfs_fattr_init(fattr);
541 	status = rpc_call(NFS_CLIENT(dir), NFS3PROC_SYMLINK, &arg, &res, 0);
542 	nfs_post_op_update_inode(dir, &dir_attr);
543 	dprintk("NFS reply symlink: %d\n", status);
544 	return status;
545 }
546 
547 static int
548 nfs3_proc_mkdir(struct inode *dir, struct dentry *dentry, struct iattr *sattr)
549 {
550 	struct nfs_fh fhandle;
551 	struct nfs_fattr fattr, dir_attr;
552 	struct nfs3_mkdirargs	arg = {
553 		.fh		= NFS_FH(dir),
554 		.name		= dentry->d_name.name,
555 		.len		= dentry->d_name.len,
556 		.sattr		= sattr
557 	};
558 	struct nfs3_diropres	res = {
559 		.dir_attr	= &dir_attr,
560 		.fh		= &fhandle,
561 		.fattr		= &fattr
562 	};
563 	int mode = sattr->ia_mode;
564 	int status;
565 
566 	dprintk("NFS call  mkdir %s\n", dentry->d_name.name);
567 
568 	sattr->ia_mode &= ~current->fs->umask;
569 
570 	nfs_fattr_init(&dir_attr);
571 	nfs_fattr_init(&fattr);
572 	status = rpc_call(NFS_CLIENT(dir), NFS3PROC_MKDIR, &arg, &res, 0);
573 	nfs_post_op_update_inode(dir, &dir_attr);
574 	if (status != 0)
575 		goto out;
576 	status = nfs_instantiate(dentry, &fhandle, &fattr);
577 	if (status != 0)
578 		goto out;
579 	status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
580 out:
581 	dprintk("NFS reply mkdir: %d\n", status);
582 	return status;
583 }
584 
585 static int
586 nfs3_proc_rmdir(struct inode *dir, struct qstr *name)
587 {
588 	struct nfs_fattr	dir_attr;
589 	struct nfs3_diropargs	arg = {
590 		.fh		= NFS_FH(dir),
591 		.name		= name->name,
592 		.len		= name->len
593 	};
594 	int			status;
595 
596 	dprintk("NFS call  rmdir %s\n", name->name);
597 	nfs_fattr_init(&dir_attr);
598 	status = rpc_call(NFS_CLIENT(dir), NFS3PROC_RMDIR, &arg, &dir_attr, 0);
599 	nfs_post_op_update_inode(dir, &dir_attr);
600 	dprintk("NFS reply rmdir: %d\n", status);
601 	return status;
602 }
603 
604 /*
605  * The READDIR implementation is somewhat hackish - we pass the user buffer
606  * to the encode function, which installs it in the receive iovec.
607  * The decode function itself doesn't perform any decoding, it just makes
608  * sure the reply is syntactically correct.
609  *
610  * Also note that this implementation handles both plain readdir and
611  * readdirplus.
612  */
613 static int
614 nfs3_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
615 		  u64 cookie, struct page *page, unsigned int count, int plus)
616 {
617 	struct inode		*dir = dentry->d_inode;
618 	struct nfs_fattr	dir_attr;
619 	u32			*verf = NFS_COOKIEVERF(dir);
620 	struct nfs3_readdirargs	arg = {
621 		.fh		= NFS_FH(dir),
622 		.cookie		= cookie,
623 		.verf		= {verf[0], verf[1]},
624 		.plus		= plus,
625 		.count		= count,
626 		.pages		= &page
627 	};
628 	struct nfs3_readdirres	res = {
629 		.dir_attr	= &dir_attr,
630 		.verf		= verf,
631 		.plus		= plus
632 	};
633 	struct rpc_message	msg = {
634 		.rpc_proc	= &nfs3_procedures[NFS3PROC_READDIR],
635 		.rpc_argp	= &arg,
636 		.rpc_resp	= &res,
637 		.rpc_cred	= cred
638 	};
639 	int			status;
640 
641 	lock_kernel();
642 
643 	if (plus)
644 		msg.rpc_proc = &nfs3_procedures[NFS3PROC_READDIRPLUS];
645 
646 	dprintk("NFS call  readdir%s %d\n",
647 			plus? "plus" : "", (unsigned int) cookie);
648 
649 	nfs_fattr_init(&dir_attr);
650 	status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
651 	nfs_refresh_inode(dir, &dir_attr);
652 	dprintk("NFS reply readdir: %d\n", status);
653 	unlock_kernel();
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 nfs_fh fh;
662 	struct nfs_fattr fattr, dir_attr;
663 	struct nfs3_mknodargs	arg = {
664 		.fh		= NFS_FH(dir),
665 		.name		= dentry->d_name.name,
666 		.len		= dentry->d_name.len,
667 		.sattr		= sattr,
668 		.rdev		= rdev
669 	};
670 	struct nfs3_diropres	res = {
671 		.dir_attr	= &dir_attr,
672 		.fh		= &fh,
673 		.fattr		= &fattr
674 	};
675 	mode_t mode = sattr->ia_mode;
676 	int status;
677 
678 	switch (sattr->ia_mode & S_IFMT) {
679 	case S_IFBLK:	arg.type = NF3BLK;  break;
680 	case S_IFCHR:	arg.type = NF3CHR;  break;
681 	case S_IFIFO:	arg.type = NF3FIFO; break;
682 	case S_IFSOCK:	arg.type = NF3SOCK; break;
683 	default:	return -EINVAL;
684 	}
685 
686 	dprintk("NFS call  mknod %s %u:%u\n", dentry->d_name.name,
687 			MAJOR(rdev), MINOR(rdev));
688 
689 	sattr->ia_mode &= ~current->fs->umask;
690 
691 	nfs_fattr_init(&dir_attr);
692 	nfs_fattr_init(&fattr);
693 	status = rpc_call(NFS_CLIENT(dir), NFS3PROC_MKNOD, &arg, &res, 0);
694 	nfs_post_op_update_inode(dir, &dir_attr);
695 	if (status != 0)
696 		goto out;
697 	status = nfs_instantiate(dentry, &fh, &fattr);
698 	if (status != 0)
699 		goto out;
700 	status = nfs3_proc_set_default_acl(dir, dentry->d_inode, mode);
701 out:
702 	dprintk("NFS reply mknod: %d\n", status);
703 	return status;
704 }
705 
706 static int
707 nfs3_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
708 		 struct nfs_fsstat *stat)
709 {
710 	int	status;
711 
712 	dprintk("NFS call  fsstat\n");
713 	nfs_fattr_init(stat->fattr);
714 	status = rpc_call(server->client, NFS3PROC_FSSTAT, fhandle, stat, 0);
715 	dprintk("NFS reply statfs: %d\n", status);
716 	return status;
717 }
718 
719 static int
720 nfs3_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
721 		 struct nfs_fsinfo *info)
722 {
723 	int	status;
724 
725 	dprintk("NFS call  fsinfo\n");
726 	nfs_fattr_init(info->fattr);
727 	status = rpc_call(server->client_sys, NFS3PROC_FSINFO, fhandle, info, 0);
728 	dprintk("NFS reply fsinfo: %d\n", status);
729 	return status;
730 }
731 
732 static int
733 nfs3_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
734 		   struct nfs_pathconf *info)
735 {
736 	int	status;
737 
738 	dprintk("NFS call  pathconf\n");
739 	nfs_fattr_init(info->fattr);
740 	status = rpc_call(server->client, NFS3PROC_PATHCONF, fhandle, info, 0);
741 	dprintk("NFS reply pathconf: %d\n", status);
742 	return status;
743 }
744 
745 extern u32 *nfs3_decode_dirent(u32 *, struct nfs_entry *, int);
746 
747 static void nfs3_read_done(struct rpc_task *task, void *calldata)
748 {
749 	struct nfs_read_data *data = calldata;
750 
751 	if (nfs3_async_handle_jukebox(task))
752 		return;
753 	/* Call back common NFS readpage processing */
754 	if (task->tk_status >= 0)
755 		nfs_refresh_inode(data->inode, &data->fattr);
756 	nfs_readpage_result(task, calldata);
757 }
758 
759 static const struct rpc_call_ops nfs3_read_ops = {
760 	.rpc_call_done = nfs3_read_done,
761 	.rpc_release = nfs_readdata_release,
762 };
763 
764 static void
765 nfs3_proc_read_setup(struct nfs_read_data *data)
766 {
767 	struct rpc_task		*task = &data->task;
768 	struct inode		*inode = data->inode;
769 	int			flags;
770 	struct rpc_message	msg = {
771 		.rpc_proc	= &nfs3_procedures[NFS3PROC_READ],
772 		.rpc_argp	= &data->args,
773 		.rpc_resp	= &data->res,
774 		.rpc_cred	= data->cred,
775 	};
776 
777 	/* N.B. Do we need to test? Never called for swapfile inode */
778 	flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
779 
780 	/* Finalize the task. */
781 	rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs3_read_ops, data);
782 	rpc_call_setup(task, &msg, 0);
783 }
784 
785 static void nfs3_write_done(struct rpc_task *task, void *calldata)
786 {
787 	struct nfs_write_data *data = calldata;
788 
789 	if (nfs3_async_handle_jukebox(task))
790 		return;
791 	if (task->tk_status >= 0)
792 		nfs_post_op_update_inode(data->inode, data->res.fattr);
793 	nfs_writeback_done(task, calldata);
794 }
795 
796 static const struct rpc_call_ops nfs3_write_ops = {
797 	.rpc_call_done = nfs3_write_done,
798 	.rpc_release = nfs_writedata_release,
799 };
800 
801 static void
802 nfs3_proc_write_setup(struct nfs_write_data *data, int how)
803 {
804 	struct rpc_task		*task = &data->task;
805 	struct inode		*inode = data->inode;
806 	int			stable;
807 	int			flags;
808 	struct rpc_message	msg = {
809 		.rpc_proc	= &nfs3_procedures[NFS3PROC_WRITE],
810 		.rpc_argp	= &data->args,
811 		.rpc_resp	= &data->res,
812 		.rpc_cred	= data->cred,
813 	};
814 
815 	if (how & FLUSH_STABLE) {
816 		if (!NFS_I(inode)->ncommit)
817 			stable = NFS_FILE_SYNC;
818 		else
819 			stable = NFS_DATA_SYNC;
820 	} else
821 		stable = NFS_UNSTABLE;
822 	data->args.stable = stable;
823 
824 	/* Set the initial flags for the task.  */
825 	flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
826 
827 	/* Finalize the task. */
828 	rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs3_write_ops, data);
829 	rpc_call_setup(task, &msg, 0);
830 }
831 
832 static void nfs3_commit_done(struct rpc_task *task, void *calldata)
833 {
834 	struct nfs_write_data *data = calldata;
835 
836 	if (nfs3_async_handle_jukebox(task))
837 		return;
838 	if (task->tk_status >= 0)
839 		nfs_post_op_update_inode(data->inode, data->res.fattr);
840 	nfs_commit_done(task, calldata);
841 }
842 
843 static const struct rpc_call_ops nfs3_commit_ops = {
844 	.rpc_call_done = nfs3_commit_done,
845 	.rpc_release = nfs_commit_release,
846 };
847 
848 static void
849 nfs3_proc_commit_setup(struct nfs_write_data *data, int how)
850 {
851 	struct rpc_task		*task = &data->task;
852 	struct inode		*inode = data->inode;
853 	int			flags;
854 	struct rpc_message	msg = {
855 		.rpc_proc	= &nfs3_procedures[NFS3PROC_COMMIT],
856 		.rpc_argp	= &data->args,
857 		.rpc_resp	= &data->res,
858 		.rpc_cred	= data->cred,
859 	};
860 
861 	/* Set the initial flags for the task.  */
862 	flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
863 
864 	/* Finalize the task. */
865 	rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs3_commit_ops, data);
866 	rpc_call_setup(task, &msg, 0);
867 }
868 
869 static int
870 nfs3_proc_lock(struct file *filp, int cmd, struct file_lock *fl)
871 {
872 	return nlmclnt_proc(filp->f_dentry->d_inode, cmd, fl);
873 }
874 
875 struct nfs_rpc_ops	nfs_v3_clientops = {
876 	.version	= 3,			/* protocol version */
877 	.dentry_ops	= &nfs_dentry_operations,
878 	.dir_inode_ops	= &nfs3_dir_inode_operations,
879 	.file_inode_ops	= &nfs3_file_inode_operations,
880 	.getroot	= nfs3_proc_get_root,
881 	.getattr	= nfs3_proc_getattr,
882 	.setattr	= nfs3_proc_setattr,
883 	.lookup		= nfs3_proc_lookup,
884 	.access		= nfs3_proc_access,
885 	.readlink	= nfs3_proc_readlink,
886 	.read		= nfs3_proc_read,
887 	.write		= nfs3_proc_write,
888 	.commit		= nfs3_proc_commit,
889 	.create		= nfs3_proc_create,
890 	.remove		= nfs3_proc_remove,
891 	.unlink_setup	= nfs3_proc_unlink_setup,
892 	.unlink_done	= nfs3_proc_unlink_done,
893 	.rename		= nfs3_proc_rename,
894 	.link		= nfs3_proc_link,
895 	.symlink	= nfs3_proc_symlink,
896 	.mkdir		= nfs3_proc_mkdir,
897 	.rmdir		= nfs3_proc_rmdir,
898 	.readdir	= nfs3_proc_readdir,
899 	.mknod		= nfs3_proc_mknod,
900 	.statfs		= nfs3_proc_statfs,
901 	.fsinfo		= nfs3_proc_fsinfo,
902 	.pathconf	= nfs3_proc_pathconf,
903 	.decode_dirent	= nfs3_decode_dirent,
904 	.read_setup	= nfs3_proc_read_setup,
905 	.write_setup	= nfs3_proc_write_setup,
906 	.commit_setup	= nfs3_proc_commit_setup,
907 	.file_open	= nfs_open,
908 	.file_release	= nfs_release,
909 	.lock		= nfs3_proc_lock,
910 	.clear_acl_cache = nfs3_forget_cached_acls,
911 };
912