xref: /linux/fs/nfsd/nfs3proc.c (revision 15ecd83dc06277385ad71dc7ea26911d9a79acaf)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Process version 3 NFS requests.
4  *
5  * Copyright (C) 1996, 1997, 1998 Olaf Kirch <okir@monad.swb.de>
6  */
7 
8 #include <linux/fs.h>
9 #include <linux/ext2_fs.h>
10 #include <linux/magic.h>
11 #include <linux/namei.h>
12 
13 #include "cache.h"
14 #include "xdr3.h"
15 #include "vfs.h"
16 #include "filecache.h"
17 
18 #define NFSDDBG_FACILITY		NFSDDBG_PROC
19 
20 static int	nfs3_ftypes[] = {
21 	0,			/* NF3NON */
22 	S_IFREG,		/* NF3REG */
23 	S_IFDIR,		/* NF3DIR */
24 	S_IFBLK,		/* NF3BLK */
25 	S_IFCHR,		/* NF3CHR */
26 	S_IFLNK,		/* NF3LNK */
27 	S_IFSOCK,		/* NF3SOCK */
28 	S_IFIFO,		/* NF3FIFO */
29 };
30 
31 static __be32 nfsd3_map_status(__be32 status)
32 {
33 	switch (status) {
34 	case nfs_ok:
35 		break;
36 	case nfserr_nofilehandle:
37 		status = nfserr_badhandle;
38 		break;
39 	case nfserr_wrongsec:
40 	case nfserr_file_open:
41 		status = nfserr_acces;
42 		break;
43 	case nfserr_symlink_not_dir:
44 		status = nfserr_notdir;
45 		break;
46 	case nfserr_symlink:
47 	case nfserr_wrong_type:
48 		status = nfserr_inval;
49 		break;
50 	}
51 	return status;
52 }
53 
54 /*
55  * NULL call.
56  */
57 static __be32
58 nfsd3_proc_null(struct svc_rqst *rqstp)
59 {
60 	return rpc_success;
61 }
62 
63 /*
64  * Get a file's attributes
65  */
66 static __be32
67 nfsd3_proc_getattr(struct svc_rqst *rqstp)
68 {
69 	struct nfsd_fhandle *argp = rqstp->rq_argp;
70 	struct nfsd3_attrstat *resp = rqstp->rq_resp;
71 
72 	dprintk("nfsd: GETATTR(3)  %s\n",
73 		SVCFH_fmt(&argp->fh));
74 
75 	fh_copy(&resp->fh, &argp->fh);
76 	resp->status = fh_verify(rqstp, &resp->fh, 0,
77 				 NFSD_MAY_NOP | NFSD_MAY_BYPASS_GSS_ON_ROOT);
78 	if (resp->status != nfs_ok)
79 		goto out;
80 
81 	resp->status = fh_getattr(&resp->fh, &resp->stat);
82 out:
83 	resp->status = nfsd3_map_status(resp->status);
84 	return rpc_success;
85 }
86 
87 /*
88  * Set a file's attributes
89  */
90 static __be32
91 nfsd3_proc_setattr(struct svc_rqst *rqstp)
92 {
93 	struct nfsd3_sattrargs *argp = rqstp->rq_argp;
94 	struct nfsd3_attrstat *resp = rqstp->rq_resp;
95 	struct nfsd_attrs attrs = {
96 		.na_iattr	= &argp->attrs,
97 	};
98 	const struct timespec64 *guardtime = NULL;
99 
100 	dprintk("nfsd: SETATTR(3)  %s\n",
101 				SVCFH_fmt(&argp->fh));
102 
103 	fh_copy(&resp->fh, &argp->fh);
104 	if (argp->check_guard)
105 		guardtime = &argp->guardtime;
106 	resp->status = nfsd_setattr(rqstp, &resp->fh, &attrs, guardtime);
107 	resp->status = nfsd3_map_status(resp->status);
108 	return rpc_success;
109 }
110 
111 /*
112  * Look up a path name component
113  */
114 static __be32
115 nfsd3_proc_lookup(struct svc_rqst *rqstp)
116 {
117 	struct nfsd3_diropargs *argp = rqstp->rq_argp;
118 	struct nfsd3_diropres  *resp = rqstp->rq_resp;
119 
120 	dprintk("nfsd: LOOKUP(3)   %s %.*s\n",
121 				SVCFH_fmt(&argp->fh),
122 				argp->len,
123 				argp->name);
124 
125 	fh_copy(&resp->dirfh, &argp->fh);
126 	fh_init(&resp->fh, NFS3_FHSIZE);
127 
128 	resp->status = nfsd_lookup(rqstp, &resp->dirfh,
129 				   argp->name, argp->len,
130 				   &resp->fh);
131 	resp->status = nfsd3_map_status(resp->status);
132 	return rpc_success;
133 }
134 
135 /*
136  * Check file access
137  */
138 static __be32
139 nfsd3_proc_access(struct svc_rqst *rqstp)
140 {
141 	struct nfsd3_accessargs *argp = rqstp->rq_argp;
142 	struct nfsd3_accessres *resp = rqstp->rq_resp;
143 
144 	dprintk("nfsd: ACCESS(3)   %s 0x%x\n",
145 				SVCFH_fmt(&argp->fh),
146 				argp->access);
147 
148 	fh_copy(&resp->fh, &argp->fh);
149 	resp->access = argp->access;
150 	resp->status = nfsd_access(rqstp, &resp->fh, &resp->access, NULL);
151 	resp->status = nfsd3_map_status(resp->status);
152 	return rpc_success;
153 }
154 
155 /*
156  * Read a symlink.
157  */
158 static __be32
159 nfsd3_proc_readlink(struct svc_rqst *rqstp)
160 {
161 	struct nfsd_fhandle *argp = rqstp->rq_argp;
162 	struct nfsd3_readlinkres *resp = rqstp->rq_resp;
163 
164 	dprintk("nfsd: READLINK(3) %s\n", SVCFH_fmt(&argp->fh));
165 
166 	/* Read the symlink. */
167 	fh_copy(&resp->fh, &argp->fh);
168 	resp->len = NFS3_MAXPATHLEN;
169 	resp->pages = rqstp->rq_next_page++;
170 	resp->status = nfsd_readlink(rqstp, &resp->fh,
171 				     page_address(*resp->pages), &resp->len);
172 	resp->status = nfsd3_map_status(resp->status);
173 	return rpc_success;
174 }
175 
176 /*
177  * Read a portion of a file.
178  */
179 static __be32
180 nfsd3_proc_read(struct svc_rqst *rqstp)
181 {
182 	struct nfsd3_readargs *argp = rqstp->rq_argp;
183 	struct nfsd3_readres *resp = rqstp->rq_resp;
184 
185 	dprintk("nfsd: READ(3) %s %lu bytes at %Lu\n",
186 				SVCFH_fmt(&argp->fh),
187 				(unsigned long) argp->count,
188 				(unsigned long long) argp->offset);
189 
190 	argp->count = min_t(u32, argp->count, svc_max_payload(rqstp));
191 	argp->count = min_t(u32, argp->count, rqstp->rq_res.buflen);
192 	if (argp->offset > (u64)OFFSET_MAX)
193 		argp->offset = (u64)OFFSET_MAX;
194 	if (argp->offset + argp->count > (u64)OFFSET_MAX)
195 		argp->count = (u64)OFFSET_MAX - argp->offset;
196 
197 	resp->pages = rqstp->rq_next_page;
198 
199 	/* Obtain buffer pointer for payload.
200 	 * 1 (status) + 22 (post_op_attr) + 1 (count) + 1 (eof)
201 	 * + 1 (xdr opaque byte count) = 26
202 	 */
203 	resp->count = argp->count;
204 	svc_reserve_auth(rqstp, ((1 + NFS3_POST_OP_ATTR_WORDS + 3) << 2) +
205 			 resp->count + 4);
206 
207 	fh_copy(&resp->fh, &argp->fh);
208 	resp->status = nfsd_read(rqstp, &resp->fh, argp->offset,
209 				 &resp->count, &resp->eof);
210 	resp->status = nfsd3_map_status(resp->status);
211 	return rpc_success;
212 }
213 
214 /*
215  * Write data to a file
216  */
217 static __be32
218 nfsd3_proc_write(struct svc_rqst *rqstp)
219 {
220 	struct nfsd3_writeargs *argp = rqstp->rq_argp;
221 	struct nfsd3_writeres *resp = rqstp->rq_resp;
222 	unsigned long cnt = argp->len;
223 	unsigned int nvecs;
224 
225 	dprintk("nfsd: WRITE(3)    %s %d bytes at %Lu%s\n",
226 				SVCFH_fmt(&argp->fh),
227 				argp->len,
228 				(unsigned long long) argp->offset,
229 				argp->stable ? " stable" : "");
230 
231 	resp->status = nfserr_fbig;
232 	if (argp->offset > (u64)OFFSET_MAX ||
233 	    argp->offset + argp->len > (u64)OFFSET_MAX)
234 		return rpc_success;
235 
236 	fh_copy(&resp->fh, &argp->fh);
237 	resp->committed = argp->stable;
238 	nvecs = svc_fill_write_vector(rqstp, &argp->payload);
239 
240 	resp->status = nfsd_write(rqstp, &resp->fh, argp->offset,
241 				  rqstp->rq_vec, nvecs, &cnt,
242 				  resp->committed, resp->verf);
243 	resp->count = cnt;
244 	resp->status = nfsd3_map_status(resp->status);
245 	return rpc_success;
246 }
247 
248 /*
249  * Implement NFSv3's unchecked, guarded, and exclusive CREATE
250  * semantics for regular files. Except for the created file,
251  * this operation is stateless on the server.
252  *
253  * Upon return, caller must release @fhp and @resfhp.
254  */
255 static __be32
256 nfsd3_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
257 		  struct svc_fh *resfhp, struct nfsd3_createargs *argp)
258 {
259 	struct iattr *iap = &argp->attrs;
260 	struct dentry *parent, *child;
261 	struct nfsd_attrs attrs = {
262 		.na_iattr	= iap,
263 	};
264 	__u32 v_mtime, v_atime;
265 	struct inode *inode;
266 	__be32 status;
267 	int host_err;
268 
269 	if (isdotent(argp->name, argp->len))
270 		return nfserr_exist;
271 	if (!(iap->ia_valid & ATTR_MODE))
272 		iap->ia_mode = 0;
273 
274 	status = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
275 	if (status != nfs_ok)
276 		return status;
277 
278 	parent = fhp->fh_dentry;
279 	inode = d_inode(parent);
280 
281 	host_err = fh_want_write(fhp);
282 	if (host_err)
283 		return nfserrno(host_err);
284 
285 	inode_lock_nested(inode, I_MUTEX_PARENT);
286 
287 	child = lookup_one(&nop_mnt_idmap,
288 			   &QSTR_LEN(argp->name, argp->len),
289 			   parent);
290 	if (IS_ERR(child)) {
291 		status = nfserrno(PTR_ERR(child));
292 		goto out;
293 	}
294 
295 	if (d_really_is_negative(child)) {
296 		status = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
297 		if (status != nfs_ok)
298 			goto out;
299 	}
300 
301 	status = fh_compose(resfhp, fhp->fh_export, child, fhp);
302 	if (status != nfs_ok)
303 		goto out;
304 
305 	v_mtime = 0;
306 	v_atime = 0;
307 	if (argp->createmode == NFS3_CREATE_EXCLUSIVE) {
308 		u32 *verifier = (u32 *)argp->verf;
309 
310 		/*
311 		 * Solaris 7 gets confused (bugid 4218508) if these have
312 		 * the high bit set, as do xfs filesystems without the
313 		 * "bigtime" feature. So just clear the high bits.
314 		 */
315 		v_mtime = verifier[0] & 0x7fffffff;
316 		v_atime = verifier[1] & 0x7fffffff;
317 	}
318 
319 	if (d_really_is_positive(child)) {
320 		status = nfs_ok;
321 
322 		switch (argp->createmode) {
323 		case NFS3_CREATE_UNCHECKED:
324 			if (!d_is_reg(child))
325 				break;
326 			iap->ia_valid &= ATTR_SIZE;
327 			goto set_attr;
328 		case NFS3_CREATE_GUARDED:
329 			status = nfserr_exist;
330 			break;
331 		case NFS3_CREATE_EXCLUSIVE:
332 			if (inode_get_mtime_sec(d_inode(child)) == v_mtime &&
333 			    inode_get_atime_sec(d_inode(child)) == v_atime &&
334 			    d_inode(child)->i_size == 0) {
335 				break;
336 			}
337 			status = nfserr_exist;
338 		}
339 		goto out;
340 	}
341 
342 	if (!IS_POSIXACL(inode))
343 		iap->ia_mode &= ~current_umask();
344 
345 	status = fh_fill_pre_attrs(fhp);
346 	if (status != nfs_ok)
347 		goto out;
348 	host_err = vfs_create(&nop_mnt_idmap, inode, child, iap->ia_mode, true);
349 	if (host_err < 0) {
350 		status = nfserrno(host_err);
351 		goto out;
352 	}
353 	fh_fill_post_attrs(fhp);
354 
355 	/* A newly created file already has a file size of zero. */
356 	if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
357 		iap->ia_valid &= ~ATTR_SIZE;
358 	if (argp->createmode == NFS3_CREATE_EXCLUSIVE) {
359 		iap->ia_valid = ATTR_MTIME | ATTR_ATIME |
360 				ATTR_MTIME_SET | ATTR_ATIME_SET;
361 		iap->ia_mtime.tv_sec = v_mtime;
362 		iap->ia_atime.tv_sec = v_atime;
363 		iap->ia_mtime.tv_nsec = 0;
364 		iap->ia_atime.tv_nsec = 0;
365 	}
366 
367 set_attr:
368 	status = nfsd_create_setattr(rqstp, fhp, resfhp, &attrs);
369 
370 out:
371 	inode_unlock(inode);
372 	if (child && !IS_ERR(child))
373 		dput(child);
374 	fh_drop_write(fhp);
375 	return status;
376 }
377 
378 static __be32
379 nfsd3_proc_create(struct svc_rqst *rqstp)
380 {
381 	struct nfsd3_createargs *argp = rqstp->rq_argp;
382 	struct nfsd3_diropres *resp = rqstp->rq_resp;
383 	svc_fh *dirfhp, *newfhp;
384 
385 	dprintk("nfsd: CREATE(3)   %s %.*s\n",
386 				SVCFH_fmt(&argp->fh),
387 				argp->len,
388 				argp->name);
389 
390 	dirfhp = fh_copy(&resp->dirfh, &argp->fh);
391 	newfhp = fh_init(&resp->fh, NFS3_FHSIZE);
392 
393 	resp->status = nfsd3_create_file(rqstp, dirfhp, newfhp, argp);
394 	resp->status = nfsd3_map_status(resp->status);
395 	return rpc_success;
396 }
397 
398 /*
399  * Make directory. This operation is not idempotent.
400  */
401 static __be32
402 nfsd3_proc_mkdir(struct svc_rqst *rqstp)
403 {
404 	struct nfsd3_createargs *argp = rqstp->rq_argp;
405 	struct nfsd3_diropres *resp = rqstp->rq_resp;
406 	struct nfsd_attrs attrs = {
407 		.na_iattr	= &argp->attrs,
408 	};
409 
410 	dprintk("nfsd: MKDIR(3)    %s %.*s\n",
411 				SVCFH_fmt(&argp->fh),
412 				argp->len,
413 				argp->name);
414 
415 	argp->attrs.ia_valid &= ~ATTR_SIZE;
416 	fh_copy(&resp->dirfh, &argp->fh);
417 	fh_init(&resp->fh, NFS3_FHSIZE);
418 	resp->status = nfsd_create(rqstp, &resp->dirfh, argp->name, argp->len,
419 				   &attrs, S_IFDIR, 0, &resp->fh);
420 	resp->status = nfsd3_map_status(resp->status);
421 	return rpc_success;
422 }
423 
424 static __be32
425 nfsd3_proc_symlink(struct svc_rqst *rqstp)
426 {
427 	struct nfsd3_symlinkargs *argp = rqstp->rq_argp;
428 	struct nfsd3_diropres *resp = rqstp->rq_resp;
429 	struct nfsd_attrs attrs = {
430 		.na_iattr	= &argp->attrs,
431 	};
432 
433 	if (argp->tlen == 0) {
434 		resp->status = nfserr_inval;
435 		goto out;
436 	}
437 	if (argp->tlen > NFS3_MAXPATHLEN) {
438 		resp->status = nfserr_nametoolong;
439 		goto out;
440 	}
441 
442 	argp->tname = svc_fill_symlink_pathname(rqstp, &argp->first,
443 						page_address(rqstp->rq_arg.pages[0]),
444 						argp->tlen);
445 	if (IS_ERR(argp->tname)) {
446 		resp->status = nfserrno(PTR_ERR(argp->tname));
447 		goto out;
448 	}
449 
450 	dprintk("nfsd: SYMLINK(3)  %s %.*s -> %.*s\n",
451 				SVCFH_fmt(&argp->ffh),
452 				argp->flen, argp->fname,
453 				argp->tlen, argp->tname);
454 
455 	fh_copy(&resp->dirfh, &argp->ffh);
456 	fh_init(&resp->fh, NFS3_FHSIZE);
457 	resp->status = nfsd_symlink(rqstp, &resp->dirfh, argp->fname,
458 				    argp->flen, argp->tname, &attrs, &resp->fh);
459 	kfree(argp->tname);
460 out:
461 	resp->status = nfsd3_map_status(resp->status);
462 	return rpc_success;
463 }
464 
465 /*
466  * Make socket/fifo/device.
467  */
468 static __be32
469 nfsd3_proc_mknod(struct svc_rqst *rqstp)
470 {
471 	struct nfsd3_mknodargs *argp = rqstp->rq_argp;
472 	struct nfsd3_diropres  *resp = rqstp->rq_resp;
473 	struct nfsd_attrs attrs = {
474 		.na_iattr	= &argp->attrs,
475 	};
476 	int type;
477 	dev_t	rdev = 0;
478 
479 	dprintk("nfsd: MKNOD(3)    %s %.*s\n",
480 				SVCFH_fmt(&argp->fh),
481 				argp->len,
482 				argp->name);
483 
484 	fh_copy(&resp->dirfh, &argp->fh);
485 	fh_init(&resp->fh, NFS3_FHSIZE);
486 
487 	if (argp->ftype == NF3CHR || argp->ftype == NF3BLK) {
488 		rdev = MKDEV(argp->major, argp->minor);
489 		if (MAJOR(rdev) != argp->major ||
490 		    MINOR(rdev) != argp->minor) {
491 			resp->status = nfserr_inval;
492 			goto out;
493 		}
494 	} else if (argp->ftype != NF3SOCK && argp->ftype != NF3FIFO) {
495 		resp->status = nfserr_badtype;
496 		goto out;
497 	}
498 
499 	type = nfs3_ftypes[argp->ftype];
500 	resp->status = nfsd_create(rqstp, &resp->dirfh, argp->name, argp->len,
501 				   &attrs, type, rdev, &resp->fh);
502 out:
503 	resp->status = nfsd3_map_status(resp->status);
504 	return rpc_success;
505 }
506 
507 /*
508  * Remove file/fifo/socket etc.
509  */
510 static __be32
511 nfsd3_proc_remove(struct svc_rqst *rqstp)
512 {
513 	struct nfsd3_diropargs *argp = rqstp->rq_argp;
514 	struct nfsd3_attrstat *resp = rqstp->rq_resp;
515 
516 	dprintk("nfsd: REMOVE(3)   %s %.*s\n",
517 				SVCFH_fmt(&argp->fh),
518 				argp->len,
519 				argp->name);
520 
521 	/* Unlink. -S_IFDIR means file must not be a directory */
522 	fh_copy(&resp->fh, &argp->fh);
523 	resp->status = nfsd_unlink(rqstp, &resp->fh, -S_IFDIR,
524 				   argp->name, argp->len);
525 	resp->status = nfsd3_map_status(resp->status);
526 	return rpc_success;
527 }
528 
529 /*
530  * Remove a directory
531  */
532 static __be32
533 nfsd3_proc_rmdir(struct svc_rqst *rqstp)
534 {
535 	struct nfsd3_diropargs *argp = rqstp->rq_argp;
536 	struct nfsd3_attrstat *resp = rqstp->rq_resp;
537 
538 	dprintk("nfsd: RMDIR(3)    %s %.*s\n",
539 				SVCFH_fmt(&argp->fh),
540 				argp->len,
541 				argp->name);
542 
543 	fh_copy(&resp->fh, &argp->fh);
544 	resp->status = nfsd_unlink(rqstp, &resp->fh, S_IFDIR,
545 				   argp->name, argp->len);
546 	resp->status = nfsd3_map_status(resp->status);
547 	return rpc_success;
548 }
549 
550 static __be32
551 nfsd3_proc_rename(struct svc_rqst *rqstp)
552 {
553 	struct nfsd3_renameargs *argp = rqstp->rq_argp;
554 	struct nfsd3_renameres *resp = rqstp->rq_resp;
555 
556 	dprintk("nfsd: RENAME(3)   %s %.*s ->\n",
557 				SVCFH_fmt(&argp->ffh),
558 				argp->flen,
559 				argp->fname);
560 	dprintk("nfsd: -> %s %.*s\n",
561 				SVCFH_fmt(&argp->tfh),
562 				argp->tlen,
563 				argp->tname);
564 
565 	fh_copy(&resp->ffh, &argp->ffh);
566 	fh_copy(&resp->tfh, &argp->tfh);
567 	resp->status = nfsd_rename(rqstp, &resp->ffh, argp->fname, argp->flen,
568 				   &resp->tfh, argp->tname, argp->tlen);
569 	resp->status = nfsd3_map_status(resp->status);
570 	return rpc_success;
571 }
572 
573 static __be32
574 nfsd3_proc_link(struct svc_rqst *rqstp)
575 {
576 	struct nfsd3_linkargs *argp = rqstp->rq_argp;
577 	struct nfsd3_linkres  *resp = rqstp->rq_resp;
578 
579 	dprintk("nfsd: LINK(3)     %s ->\n",
580 				SVCFH_fmt(&argp->ffh));
581 	dprintk("nfsd:   -> %s %.*s\n",
582 				SVCFH_fmt(&argp->tfh),
583 				argp->tlen,
584 				argp->tname);
585 
586 	fh_copy(&resp->fh,  &argp->ffh);
587 	fh_copy(&resp->tfh, &argp->tfh);
588 	resp->status = nfsd_link(rqstp, &resp->tfh, argp->tname, argp->tlen,
589 				 &resp->fh);
590 	resp->status = nfsd3_map_status(resp->status);
591 	return rpc_success;
592 }
593 
594 static void nfsd3_init_dirlist_pages(struct svc_rqst *rqstp,
595 				     struct nfsd3_readdirres *resp,
596 				     u32 count)
597 {
598 	struct xdr_buf *buf = &resp->dirlist;
599 	struct xdr_stream *xdr = &resp->xdr;
600 	unsigned int sendbuf = min_t(unsigned int, rqstp->rq_res.buflen,
601 				     svc_max_payload(rqstp));
602 
603 	memset(buf, 0, sizeof(*buf));
604 
605 	/* Reserve room for the NULL ptr & eof flag (-2 words) */
606 	buf->buflen = clamp(count, (u32)(XDR_UNIT * 2), sendbuf);
607 	buf->buflen -= XDR_UNIT * 2;
608 	buf->pages = rqstp->rq_next_page;
609 	rqstp->rq_next_page += (buf->buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
610 
611 	xdr_init_encode_pages(xdr, buf, buf->pages,  NULL);
612 }
613 
614 /*
615  * Read a portion of a directory.
616  */
617 static __be32
618 nfsd3_proc_readdir(struct svc_rqst *rqstp)
619 {
620 	struct nfsd3_readdirargs *argp = rqstp->rq_argp;
621 	struct nfsd3_readdirres  *resp = rqstp->rq_resp;
622 	loff_t		offset;
623 
624 	dprintk("nfsd: READDIR(3)  %s %d bytes at %d\n",
625 				SVCFH_fmt(&argp->fh),
626 				argp->count, (u32) argp->cookie);
627 
628 	nfsd3_init_dirlist_pages(rqstp, resp, argp->count);
629 
630 	fh_copy(&resp->fh, &argp->fh);
631 	resp->common.err = nfs_ok;
632 	resp->cookie_offset = 0;
633 	resp->rqstp = rqstp;
634 	offset = argp->cookie;
635 	resp->status = nfsd_readdir(rqstp, &resp->fh, &offset,
636 				    &resp->common, nfs3svc_encode_entry3);
637 	memcpy(resp->verf, argp->verf, 8);
638 	nfs3svc_encode_cookie3(resp, offset);
639 
640 	/* Recycle only pages that were part of the reply */
641 	rqstp->rq_next_page = resp->xdr.page_ptr + 1;
642 
643 	resp->status = nfsd3_map_status(resp->status);
644 	return rpc_success;
645 }
646 
647 /*
648  * Read a portion of a directory, including file handles and attrs.
649  * For now, we choose to ignore the dircount parameter.
650  */
651 static __be32
652 nfsd3_proc_readdirplus(struct svc_rqst *rqstp)
653 {
654 	struct nfsd3_readdirargs *argp = rqstp->rq_argp;
655 	struct nfsd3_readdirres  *resp = rqstp->rq_resp;
656 	loff_t	offset;
657 
658 	dprintk("nfsd: READDIR+(3) %s %d bytes at %d\n",
659 				SVCFH_fmt(&argp->fh),
660 				argp->count, (u32) argp->cookie);
661 
662 	nfsd3_init_dirlist_pages(rqstp, resp, argp->count);
663 
664 	fh_copy(&resp->fh, &argp->fh);
665 	resp->common.err = nfs_ok;
666 	resp->cookie_offset = 0;
667 	resp->rqstp = rqstp;
668 	offset = argp->cookie;
669 
670 	resp->status = fh_verify(rqstp, &resp->fh, S_IFDIR, NFSD_MAY_NOP);
671 	if (resp->status != nfs_ok)
672 		goto out;
673 
674 	if (resp->fh.fh_export->ex_flags & NFSEXP_NOREADDIRPLUS) {
675 		resp->status = nfserr_notsupp;
676 		goto out;
677 	}
678 
679 	resp->status = nfsd_readdir(rqstp, &resp->fh, &offset,
680 				    &resp->common, nfs3svc_encode_entryplus3);
681 	memcpy(resp->verf, argp->verf, 8);
682 	nfs3svc_encode_cookie3(resp, offset);
683 
684 	/* Recycle only pages that were part of the reply */
685 	rqstp->rq_next_page = resp->xdr.page_ptr + 1;
686 
687 out:
688 	resp->status = nfsd3_map_status(resp->status);
689 	return rpc_success;
690 }
691 
692 /*
693  * Get file system stats
694  */
695 static __be32
696 nfsd3_proc_fsstat(struct svc_rqst *rqstp)
697 {
698 	struct nfsd_fhandle *argp = rqstp->rq_argp;
699 	struct nfsd3_fsstatres *resp = rqstp->rq_resp;
700 
701 	dprintk("nfsd: FSSTAT(3)   %s\n",
702 				SVCFH_fmt(&argp->fh));
703 
704 	resp->status = nfsd_statfs(rqstp, &argp->fh, &resp->stats, 0);
705 	fh_put(&argp->fh);
706 	resp->status = nfsd3_map_status(resp->status);
707 	return rpc_success;
708 }
709 
710 /*
711  * Get file system info
712  */
713 static __be32
714 nfsd3_proc_fsinfo(struct svc_rqst *rqstp)
715 {
716 	struct nfsd_fhandle *argp = rqstp->rq_argp;
717 	struct nfsd3_fsinfores *resp = rqstp->rq_resp;
718 	u32	max_blocksize = svc_max_payload(rqstp);
719 
720 	dprintk("nfsd: FSINFO(3)   %s\n",
721 				SVCFH_fmt(&argp->fh));
722 
723 	resp->f_rtmax  = max_blocksize;
724 	resp->f_rtpref = max_blocksize;
725 	resp->f_rtmult = PAGE_SIZE;
726 	resp->f_wtmax  = max_blocksize;
727 	resp->f_wtpref = max_blocksize;
728 	resp->f_wtmult = PAGE_SIZE;
729 	resp->f_dtpref = max_blocksize;
730 	resp->f_maxfilesize = ~(u32) 0;
731 	resp->f_properties = NFS3_FSF_DEFAULT;
732 
733 	resp->status = fh_verify(rqstp, &argp->fh, 0,
734 				 NFSD_MAY_NOP | NFSD_MAY_BYPASS_GSS_ON_ROOT);
735 
736 	/* Check special features of the file system. May request
737 	 * different read/write sizes for file systems known to have
738 	 * problems with large blocks */
739 	if (resp->status == nfs_ok) {
740 		struct super_block *sb = argp->fh.fh_dentry->d_sb;
741 
742 		/* Note that we don't care for remote fs's here */
743 		if (sb->s_magic == MSDOS_SUPER_MAGIC) {
744 			resp->f_properties = NFS3_FSF_BILLYBOY;
745 		}
746 		resp->f_maxfilesize = sb->s_maxbytes;
747 	}
748 
749 	fh_put(&argp->fh);
750 	resp->status = nfsd3_map_status(resp->status);
751 	return rpc_success;
752 }
753 
754 /*
755  * Get pathconf info for the specified file
756  */
757 static __be32
758 nfsd3_proc_pathconf(struct svc_rqst *rqstp)
759 {
760 	struct nfsd_fhandle *argp = rqstp->rq_argp;
761 	struct nfsd3_pathconfres *resp = rqstp->rq_resp;
762 
763 	dprintk("nfsd: PATHCONF(3) %s\n",
764 				SVCFH_fmt(&argp->fh));
765 
766 	/* Set default pathconf */
767 	resp->p_link_max = 255;		/* at least */
768 	resp->p_name_max = 255;		/* at least */
769 	resp->p_no_trunc = 0;
770 	resp->p_chown_restricted = 1;
771 	resp->p_case_insensitive = 0;
772 	resp->p_case_preserving = 1;
773 
774 	resp->status = fh_verify(rqstp, &argp->fh, 0, NFSD_MAY_NOP);
775 
776 	if (resp->status == nfs_ok) {
777 		struct super_block *sb = argp->fh.fh_dentry->d_sb;
778 
779 		/* Note that we don't care for remote fs's here */
780 		switch (sb->s_magic) {
781 		case EXT2_SUPER_MAGIC:
782 			resp->p_link_max = EXT2_LINK_MAX;
783 			resp->p_name_max = EXT2_NAME_LEN;
784 			break;
785 		case MSDOS_SUPER_MAGIC:
786 			resp->p_case_insensitive = 1;
787 			resp->p_case_preserving  = 0;
788 			break;
789 		}
790 	}
791 
792 	fh_put(&argp->fh);
793 	resp->status = nfsd3_map_status(resp->status);
794 	return rpc_success;
795 }
796 
797 /*
798  * Commit a file (range) to stable storage.
799  */
800 static __be32
801 nfsd3_proc_commit(struct svc_rqst *rqstp)
802 {
803 	struct nfsd3_commitargs *argp = rqstp->rq_argp;
804 	struct nfsd3_commitres *resp = rqstp->rq_resp;
805 	struct nfsd_file *nf;
806 
807 	dprintk("nfsd: COMMIT(3)   %s %u@%Lu\n",
808 				SVCFH_fmt(&argp->fh),
809 				argp->count,
810 				(unsigned long long) argp->offset);
811 
812 	fh_copy(&resp->fh, &argp->fh);
813 	resp->status = nfsd_file_acquire_gc(rqstp, &resp->fh, NFSD_MAY_WRITE |
814 					    NFSD_MAY_NOT_BREAK_LEASE, &nf);
815 	if (resp->status)
816 		goto out;
817 	resp->status = nfsd_commit(rqstp, &resp->fh, nf, argp->offset,
818 				   argp->count, resp->verf);
819 	nfsd_file_put(nf);
820 out:
821 	resp->status = nfsd3_map_status(resp->status);
822 	return rpc_success;
823 }
824 
825 
826 /*
827  * NFSv3 Server procedures.
828  * Only the results of non-idempotent operations are cached.
829  */
830 #define nfs3svc_encode_attrstatres	nfs3svc_encode_attrstat
831 #define nfs3svc_encode_wccstatres	nfs3svc_encode_wccstat
832 #define nfsd3_mkdirargs			nfsd3_createargs
833 #define nfsd3_readdirplusargs		nfsd3_readdirargs
834 #define nfsd3_fhandleargs		nfsd_fhandle
835 #define nfsd3_attrstatres		nfsd3_attrstat
836 #define nfsd3_wccstatres		nfsd3_attrstat
837 #define nfsd3_createres			nfsd3_diropres
838 
839 #define ST 1		/* status*/
840 #define FH 17		/* filehandle with length */
841 #define AT 21		/* attributes */
842 #define pAT (1+AT)	/* post attributes - conditional */
843 #define WC (7+pAT)	/* WCC attributes */
844 
845 static const struct svc_procedure nfsd_procedures3[22] = {
846 	[NFS3PROC_NULL] = {
847 		.pc_func = nfsd3_proc_null,
848 		.pc_decode = nfssvc_decode_voidarg,
849 		.pc_encode = nfssvc_encode_voidres,
850 		.pc_argsize = sizeof(struct nfsd_voidargs),
851 		.pc_argzero = sizeof(struct nfsd_voidargs),
852 		.pc_ressize = sizeof(struct nfsd_voidres),
853 		.pc_cachetype = RC_NOCACHE,
854 		.pc_xdrressize = ST,
855 		.pc_name = "NULL",
856 	},
857 	[NFS3PROC_GETATTR] = {
858 		.pc_func = nfsd3_proc_getattr,
859 		.pc_decode = nfs3svc_decode_fhandleargs,
860 		.pc_encode = nfs3svc_encode_getattrres,
861 		.pc_release = nfs3svc_release_fhandle,
862 		.pc_argsize = sizeof(struct nfsd_fhandle),
863 		.pc_argzero = sizeof(struct nfsd_fhandle),
864 		.pc_ressize = sizeof(struct nfsd3_attrstatres),
865 		.pc_cachetype = RC_NOCACHE,
866 		.pc_xdrressize = ST+AT,
867 		.pc_name = "GETATTR",
868 	},
869 	[NFS3PROC_SETATTR] = {
870 		.pc_func = nfsd3_proc_setattr,
871 		.pc_decode = nfs3svc_decode_sattrargs,
872 		.pc_encode = nfs3svc_encode_wccstatres,
873 		.pc_release = nfs3svc_release_fhandle,
874 		.pc_argsize = sizeof(struct nfsd3_sattrargs),
875 		.pc_argzero = sizeof(struct nfsd3_sattrargs),
876 		.pc_ressize = sizeof(struct nfsd3_wccstatres),
877 		.pc_cachetype = RC_REPLBUFF,
878 		.pc_xdrressize = ST+WC,
879 		.pc_name = "SETATTR",
880 	},
881 	[NFS3PROC_LOOKUP] = {
882 		.pc_func = nfsd3_proc_lookup,
883 		.pc_decode = nfs3svc_decode_diropargs,
884 		.pc_encode = nfs3svc_encode_lookupres,
885 		.pc_release = nfs3svc_release_fhandle2,
886 		.pc_argsize = sizeof(struct nfsd3_diropargs),
887 		.pc_argzero = sizeof(struct nfsd3_diropargs),
888 		.pc_ressize = sizeof(struct nfsd3_diropres),
889 		.pc_cachetype = RC_NOCACHE,
890 		.pc_xdrressize = ST+FH+pAT+pAT,
891 		.pc_name = "LOOKUP",
892 	},
893 	[NFS3PROC_ACCESS] = {
894 		.pc_func = nfsd3_proc_access,
895 		.pc_decode = nfs3svc_decode_accessargs,
896 		.pc_encode = nfs3svc_encode_accessres,
897 		.pc_release = nfs3svc_release_fhandle,
898 		.pc_argsize = sizeof(struct nfsd3_accessargs),
899 		.pc_argzero = sizeof(struct nfsd3_accessargs),
900 		.pc_ressize = sizeof(struct nfsd3_accessres),
901 		.pc_cachetype = RC_NOCACHE,
902 		.pc_xdrressize = ST+pAT+1,
903 		.pc_name = "ACCESS",
904 	},
905 	[NFS3PROC_READLINK] = {
906 		.pc_func = nfsd3_proc_readlink,
907 		.pc_decode = nfs3svc_decode_fhandleargs,
908 		.pc_encode = nfs3svc_encode_readlinkres,
909 		.pc_release = nfs3svc_release_fhandle,
910 		.pc_argsize = sizeof(struct nfsd_fhandle),
911 		.pc_argzero = sizeof(struct nfsd_fhandle),
912 		.pc_ressize = sizeof(struct nfsd3_readlinkres),
913 		.pc_cachetype = RC_NOCACHE,
914 		.pc_xdrressize = ST+pAT+1+NFS3_MAXPATHLEN/4,
915 		.pc_name = "READLINK",
916 	},
917 	[NFS3PROC_READ] = {
918 		.pc_func = nfsd3_proc_read,
919 		.pc_decode = nfs3svc_decode_readargs,
920 		.pc_encode = nfs3svc_encode_readres,
921 		.pc_release = nfs3svc_release_fhandle,
922 		.pc_argsize = sizeof(struct nfsd3_readargs),
923 		.pc_argzero = sizeof(struct nfsd3_readargs),
924 		.pc_ressize = sizeof(struct nfsd3_readres),
925 		.pc_cachetype = RC_NOCACHE,
926 		.pc_xdrressize = ST+pAT+4+NFSSVC_MAXBLKSIZE/4,
927 		.pc_name = "READ",
928 	},
929 	[NFS3PROC_WRITE] = {
930 		.pc_func = nfsd3_proc_write,
931 		.pc_decode = nfs3svc_decode_writeargs,
932 		.pc_encode = nfs3svc_encode_writeres,
933 		.pc_release = nfs3svc_release_fhandle,
934 		.pc_argsize = sizeof(struct nfsd3_writeargs),
935 		.pc_argzero = sizeof(struct nfsd3_writeargs),
936 		.pc_ressize = sizeof(struct nfsd3_writeres),
937 		.pc_cachetype = RC_REPLBUFF,
938 		.pc_xdrressize = ST+WC+4,
939 		.pc_name = "WRITE",
940 	},
941 	[NFS3PROC_CREATE] = {
942 		.pc_func = nfsd3_proc_create,
943 		.pc_decode = nfs3svc_decode_createargs,
944 		.pc_encode = nfs3svc_encode_createres,
945 		.pc_release = nfs3svc_release_fhandle2,
946 		.pc_argsize = sizeof(struct nfsd3_createargs),
947 		.pc_argzero = sizeof(struct nfsd3_createargs),
948 		.pc_ressize = sizeof(struct nfsd3_createres),
949 		.pc_cachetype = RC_REPLBUFF,
950 		.pc_xdrressize = ST+(1+FH+pAT)+WC,
951 		.pc_name = "CREATE",
952 	},
953 	[NFS3PROC_MKDIR] = {
954 		.pc_func = nfsd3_proc_mkdir,
955 		.pc_decode = nfs3svc_decode_mkdirargs,
956 		.pc_encode = nfs3svc_encode_createres,
957 		.pc_release = nfs3svc_release_fhandle2,
958 		.pc_argsize = sizeof(struct nfsd3_mkdirargs),
959 		.pc_argzero = sizeof(struct nfsd3_mkdirargs),
960 		.pc_ressize = sizeof(struct nfsd3_createres),
961 		.pc_cachetype = RC_REPLBUFF,
962 		.pc_xdrressize = ST+(1+FH+pAT)+WC,
963 		.pc_name = "MKDIR",
964 	},
965 	[NFS3PROC_SYMLINK] = {
966 		.pc_func = nfsd3_proc_symlink,
967 		.pc_decode = nfs3svc_decode_symlinkargs,
968 		.pc_encode = nfs3svc_encode_createres,
969 		.pc_release = nfs3svc_release_fhandle2,
970 		.pc_argsize = sizeof(struct nfsd3_symlinkargs),
971 		.pc_argzero = sizeof(struct nfsd3_symlinkargs),
972 		.pc_ressize = sizeof(struct nfsd3_createres),
973 		.pc_cachetype = RC_REPLBUFF,
974 		.pc_xdrressize = ST+(1+FH+pAT)+WC,
975 		.pc_name = "SYMLINK",
976 	},
977 	[NFS3PROC_MKNOD] = {
978 		.pc_func = nfsd3_proc_mknod,
979 		.pc_decode = nfs3svc_decode_mknodargs,
980 		.pc_encode = nfs3svc_encode_createres,
981 		.pc_release = nfs3svc_release_fhandle2,
982 		.pc_argsize = sizeof(struct nfsd3_mknodargs),
983 		.pc_argzero = sizeof(struct nfsd3_mknodargs),
984 		.pc_ressize = sizeof(struct nfsd3_createres),
985 		.pc_cachetype = RC_REPLBUFF,
986 		.pc_xdrressize = ST+(1+FH+pAT)+WC,
987 		.pc_name = "MKNOD",
988 	},
989 	[NFS3PROC_REMOVE] = {
990 		.pc_func = nfsd3_proc_remove,
991 		.pc_decode = nfs3svc_decode_diropargs,
992 		.pc_encode = nfs3svc_encode_wccstatres,
993 		.pc_release = nfs3svc_release_fhandle,
994 		.pc_argsize = sizeof(struct nfsd3_diropargs),
995 		.pc_argzero = sizeof(struct nfsd3_diropargs),
996 		.pc_ressize = sizeof(struct nfsd3_wccstatres),
997 		.pc_cachetype = RC_REPLBUFF,
998 		.pc_xdrressize = ST+WC,
999 		.pc_name = "REMOVE",
1000 	},
1001 	[NFS3PROC_RMDIR] = {
1002 		.pc_func = nfsd3_proc_rmdir,
1003 		.pc_decode = nfs3svc_decode_diropargs,
1004 		.pc_encode = nfs3svc_encode_wccstatres,
1005 		.pc_release = nfs3svc_release_fhandle,
1006 		.pc_argsize = sizeof(struct nfsd3_diropargs),
1007 		.pc_argzero = sizeof(struct nfsd3_diropargs),
1008 		.pc_ressize = sizeof(struct nfsd3_wccstatres),
1009 		.pc_cachetype = RC_REPLBUFF,
1010 		.pc_xdrressize = ST+WC,
1011 		.pc_name = "RMDIR",
1012 	},
1013 	[NFS3PROC_RENAME] = {
1014 		.pc_func = nfsd3_proc_rename,
1015 		.pc_decode = nfs3svc_decode_renameargs,
1016 		.pc_encode = nfs3svc_encode_renameres,
1017 		.pc_release = nfs3svc_release_fhandle2,
1018 		.pc_argsize = sizeof(struct nfsd3_renameargs),
1019 		.pc_argzero = sizeof(struct nfsd3_renameargs),
1020 		.pc_ressize = sizeof(struct nfsd3_renameres),
1021 		.pc_cachetype = RC_REPLBUFF,
1022 		.pc_xdrressize = ST+WC+WC,
1023 		.pc_name = "RENAME",
1024 	},
1025 	[NFS3PROC_LINK] = {
1026 		.pc_func = nfsd3_proc_link,
1027 		.pc_decode = nfs3svc_decode_linkargs,
1028 		.pc_encode = nfs3svc_encode_linkres,
1029 		.pc_release = nfs3svc_release_fhandle2,
1030 		.pc_argsize = sizeof(struct nfsd3_linkargs),
1031 		.pc_argzero = sizeof(struct nfsd3_linkargs),
1032 		.pc_ressize = sizeof(struct nfsd3_linkres),
1033 		.pc_cachetype = RC_REPLBUFF,
1034 		.pc_xdrressize = ST+pAT+WC,
1035 		.pc_name = "LINK",
1036 	},
1037 	[NFS3PROC_READDIR] = {
1038 		.pc_func = nfsd3_proc_readdir,
1039 		.pc_decode = nfs3svc_decode_readdirargs,
1040 		.pc_encode = nfs3svc_encode_readdirres,
1041 		.pc_release = nfs3svc_release_fhandle,
1042 		.pc_argsize = sizeof(struct nfsd3_readdirargs),
1043 		.pc_argzero = sizeof(struct nfsd3_readdirargs),
1044 		.pc_ressize = sizeof(struct nfsd3_readdirres),
1045 		.pc_cachetype = RC_NOCACHE,
1046 		.pc_name = "READDIR",
1047 	},
1048 	[NFS3PROC_READDIRPLUS] = {
1049 		.pc_func = nfsd3_proc_readdirplus,
1050 		.pc_decode = nfs3svc_decode_readdirplusargs,
1051 		.pc_encode = nfs3svc_encode_readdirres,
1052 		.pc_release = nfs3svc_release_fhandle,
1053 		.pc_argsize = sizeof(struct nfsd3_readdirplusargs),
1054 		.pc_argzero = sizeof(struct nfsd3_readdirplusargs),
1055 		.pc_ressize = sizeof(struct nfsd3_readdirres),
1056 		.pc_cachetype = RC_NOCACHE,
1057 		.pc_name = "READDIRPLUS",
1058 	},
1059 	[NFS3PROC_FSSTAT] = {
1060 		.pc_func = nfsd3_proc_fsstat,
1061 		.pc_decode = nfs3svc_decode_fhandleargs,
1062 		.pc_encode = nfs3svc_encode_fsstatres,
1063 		.pc_argsize = sizeof(struct nfsd3_fhandleargs),
1064 		.pc_argzero = sizeof(struct nfsd3_fhandleargs),
1065 		.pc_ressize = sizeof(struct nfsd3_fsstatres),
1066 		.pc_cachetype = RC_NOCACHE,
1067 		.pc_xdrressize = ST+pAT+2*6+1,
1068 		.pc_name = "FSSTAT",
1069 	},
1070 	[NFS3PROC_FSINFO] = {
1071 		.pc_func = nfsd3_proc_fsinfo,
1072 		.pc_decode = nfs3svc_decode_fhandleargs,
1073 		.pc_encode = nfs3svc_encode_fsinfores,
1074 		.pc_argsize = sizeof(struct nfsd3_fhandleargs),
1075 		.pc_argzero = sizeof(struct nfsd3_fhandleargs),
1076 		.pc_ressize = sizeof(struct nfsd3_fsinfores),
1077 		.pc_cachetype = RC_NOCACHE,
1078 		.pc_xdrressize = ST+pAT+12,
1079 		.pc_name = "FSINFO",
1080 	},
1081 	[NFS3PROC_PATHCONF] = {
1082 		.pc_func = nfsd3_proc_pathconf,
1083 		.pc_decode = nfs3svc_decode_fhandleargs,
1084 		.pc_encode = nfs3svc_encode_pathconfres,
1085 		.pc_argsize = sizeof(struct nfsd3_fhandleargs),
1086 		.pc_argzero = sizeof(struct nfsd3_fhandleargs),
1087 		.pc_ressize = sizeof(struct nfsd3_pathconfres),
1088 		.pc_cachetype = RC_NOCACHE,
1089 		.pc_xdrressize = ST+pAT+6,
1090 		.pc_name = "PATHCONF",
1091 	},
1092 	[NFS3PROC_COMMIT] = {
1093 		.pc_func = nfsd3_proc_commit,
1094 		.pc_decode = nfs3svc_decode_commitargs,
1095 		.pc_encode = nfs3svc_encode_commitres,
1096 		.pc_release = nfs3svc_release_fhandle,
1097 		.pc_argsize = sizeof(struct nfsd3_commitargs),
1098 		.pc_argzero = sizeof(struct nfsd3_commitargs),
1099 		.pc_ressize = sizeof(struct nfsd3_commitres),
1100 		.pc_cachetype = RC_NOCACHE,
1101 		.pc_xdrressize = ST+WC+2,
1102 		.pc_name = "COMMIT",
1103 	},
1104 };
1105 
1106 static DEFINE_PER_CPU_ALIGNED(unsigned long,
1107 			      nfsd_count3[ARRAY_SIZE(nfsd_procedures3)]);
1108 const struct svc_version nfsd_version3 = {
1109 	.vs_vers	= 3,
1110 	.vs_nproc	= ARRAY_SIZE(nfsd_procedures3),
1111 	.vs_proc	= nfsd_procedures3,
1112 	.vs_dispatch	= nfsd_dispatch,
1113 	.vs_count	= nfsd_count3,
1114 	.vs_xdrsize	= NFS3_SVC_XDRSIZE,
1115 };
1116