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