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