xref: /openbmc/linux/fs/nfsd/nfs3proc.c (revision 2a9eb57e)
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 	u32 max_blocksize = svc_max_payload(rqstp);
154 	unsigned int len;
155 	int v;
156 
157 	dprintk("nfsd: READ(3) %s %lu bytes at %Lu\n",
158 				SVCFH_fmt(&argp->fh),
159 				(unsigned long) argp->count,
160 				(unsigned long long) argp->offset);
161 
162 	argp->count = min_t(u32, argp->count, max_blocksize);
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 
567 	count = clamp(count, (u32)(XDR_UNIT * 2), 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 = count - XDR_UNIT * 2;
573 	buf->pages = rqstp->rq_next_page;
574 	rqstp->rq_next_page += (buf->buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
575 
576 	/* This is xdr_init_encode(), but it assumes that
577 	 * the head kvec has already been consumed. */
578 	xdr_set_scratch_buffer(xdr, NULL, 0);
579 	xdr->buf = buf;
580 	xdr->page_ptr = buf->pages;
581 	xdr->iov = NULL;
582 	xdr->p = page_address(*buf->pages);
583 	xdr->end = (void *)xdr->p + min_t(u32, buf->buflen, PAGE_SIZE);
584 	xdr->rqst = NULL;
585 }
586 
587 /*
588  * Read a portion of a directory.
589  */
590 static __be32
591 nfsd3_proc_readdir(struct svc_rqst *rqstp)
592 {
593 	struct nfsd3_readdirargs *argp = rqstp->rq_argp;
594 	struct nfsd3_readdirres  *resp = rqstp->rq_resp;
595 	loff_t		offset;
596 
597 	dprintk("nfsd: READDIR(3)  %s %d bytes at %d\n",
598 				SVCFH_fmt(&argp->fh),
599 				argp->count, (u32) argp->cookie);
600 
601 	nfsd3_init_dirlist_pages(rqstp, resp, argp->count);
602 
603 	fh_copy(&resp->fh, &argp->fh);
604 	resp->common.err = nfs_ok;
605 	resp->cookie_offset = 0;
606 	resp->rqstp = rqstp;
607 	offset = argp->cookie;
608 	resp->status = nfsd_readdir(rqstp, &resp->fh, &offset,
609 				    &resp->common, nfs3svc_encode_entry3);
610 	memcpy(resp->verf, argp->verf, 8);
611 	nfs3svc_encode_cookie3(resp, offset);
612 
613 	/* Recycle only pages that were part of the reply */
614 	rqstp->rq_next_page = resp->xdr.page_ptr + 1;
615 
616 	return rpc_success;
617 }
618 
619 /*
620  * Read a portion of a directory, including file handles and attrs.
621  * For now, we choose to ignore the dircount parameter.
622  */
623 static __be32
624 nfsd3_proc_readdirplus(struct svc_rqst *rqstp)
625 {
626 	struct nfsd3_readdirargs *argp = rqstp->rq_argp;
627 	struct nfsd3_readdirres  *resp = rqstp->rq_resp;
628 	loff_t	offset;
629 
630 	dprintk("nfsd: READDIR+(3) %s %d bytes at %d\n",
631 				SVCFH_fmt(&argp->fh),
632 				argp->count, (u32) argp->cookie);
633 
634 	nfsd3_init_dirlist_pages(rqstp, resp, argp->count);
635 
636 	fh_copy(&resp->fh, &argp->fh);
637 	resp->common.err = nfs_ok;
638 	resp->cookie_offset = 0;
639 	resp->rqstp = rqstp;
640 	offset = argp->cookie;
641 
642 	resp->status = fh_verify(rqstp, &resp->fh, S_IFDIR, NFSD_MAY_NOP);
643 	if (resp->status != nfs_ok)
644 		goto out;
645 
646 	if (resp->fh.fh_export->ex_flags & NFSEXP_NOREADDIRPLUS) {
647 		resp->status = nfserr_notsupp;
648 		goto out;
649 	}
650 
651 	resp->status = nfsd_readdir(rqstp, &resp->fh, &offset,
652 				    &resp->common, nfs3svc_encode_entryplus3);
653 	memcpy(resp->verf, argp->verf, 8);
654 	nfs3svc_encode_cookie3(resp, offset);
655 
656 	/* Recycle only pages that were part of the reply */
657 	rqstp->rq_next_page = resp->xdr.page_ptr + 1;
658 
659 out:
660 	return rpc_success;
661 }
662 
663 /*
664  * Get file system stats
665  */
666 static __be32
667 nfsd3_proc_fsstat(struct svc_rqst *rqstp)
668 {
669 	struct nfsd_fhandle *argp = rqstp->rq_argp;
670 	struct nfsd3_fsstatres *resp = rqstp->rq_resp;
671 
672 	dprintk("nfsd: FSSTAT(3)   %s\n",
673 				SVCFH_fmt(&argp->fh));
674 
675 	resp->status = nfsd_statfs(rqstp, &argp->fh, &resp->stats, 0);
676 	fh_put(&argp->fh);
677 	return rpc_success;
678 }
679 
680 /*
681  * Get file system info
682  */
683 static __be32
684 nfsd3_proc_fsinfo(struct svc_rqst *rqstp)
685 {
686 	struct nfsd_fhandle *argp = rqstp->rq_argp;
687 	struct nfsd3_fsinfores *resp = rqstp->rq_resp;
688 	u32	max_blocksize = svc_max_payload(rqstp);
689 
690 	dprintk("nfsd: FSINFO(3)   %s\n",
691 				SVCFH_fmt(&argp->fh));
692 
693 	resp->f_rtmax  = max_blocksize;
694 	resp->f_rtpref = max_blocksize;
695 	resp->f_rtmult = PAGE_SIZE;
696 	resp->f_wtmax  = max_blocksize;
697 	resp->f_wtpref = max_blocksize;
698 	resp->f_wtmult = PAGE_SIZE;
699 	resp->f_dtpref = max_blocksize;
700 	resp->f_maxfilesize = ~(u32) 0;
701 	resp->f_properties = NFS3_FSF_DEFAULT;
702 
703 	resp->status = fh_verify(rqstp, &argp->fh, 0,
704 				 NFSD_MAY_NOP | NFSD_MAY_BYPASS_GSS_ON_ROOT);
705 
706 	/* Check special features of the file system. May request
707 	 * different read/write sizes for file systems known to have
708 	 * problems with large blocks */
709 	if (resp->status == nfs_ok) {
710 		struct super_block *sb = argp->fh.fh_dentry->d_sb;
711 
712 		/* Note that we don't care for remote fs's here */
713 		if (sb->s_magic == MSDOS_SUPER_MAGIC) {
714 			resp->f_properties = NFS3_FSF_BILLYBOY;
715 		}
716 		resp->f_maxfilesize = sb->s_maxbytes;
717 	}
718 
719 	fh_put(&argp->fh);
720 	return rpc_success;
721 }
722 
723 /*
724  * Get pathconf info for the specified file
725  */
726 static __be32
727 nfsd3_proc_pathconf(struct svc_rqst *rqstp)
728 {
729 	struct nfsd_fhandle *argp = rqstp->rq_argp;
730 	struct nfsd3_pathconfres *resp = rqstp->rq_resp;
731 
732 	dprintk("nfsd: PATHCONF(3) %s\n",
733 				SVCFH_fmt(&argp->fh));
734 
735 	/* Set default pathconf */
736 	resp->p_link_max = 255;		/* at least */
737 	resp->p_name_max = 255;		/* at least */
738 	resp->p_no_trunc = 0;
739 	resp->p_chown_restricted = 1;
740 	resp->p_case_insensitive = 0;
741 	resp->p_case_preserving = 1;
742 
743 	resp->status = fh_verify(rqstp, &argp->fh, 0, NFSD_MAY_NOP);
744 
745 	if (resp->status == nfs_ok) {
746 		struct super_block *sb = argp->fh.fh_dentry->d_sb;
747 
748 		/* Note that we don't care for remote fs's here */
749 		switch (sb->s_magic) {
750 		case EXT2_SUPER_MAGIC:
751 			resp->p_link_max = EXT2_LINK_MAX;
752 			resp->p_name_max = EXT2_NAME_LEN;
753 			break;
754 		case MSDOS_SUPER_MAGIC:
755 			resp->p_case_insensitive = 1;
756 			resp->p_case_preserving  = 0;
757 			break;
758 		}
759 	}
760 
761 	fh_put(&argp->fh);
762 	return rpc_success;
763 }
764 
765 /*
766  * Commit a file (range) to stable storage.
767  */
768 static __be32
769 nfsd3_proc_commit(struct svc_rqst *rqstp)
770 {
771 	struct nfsd3_commitargs *argp = rqstp->rq_argp;
772 	struct nfsd3_commitres *resp = rqstp->rq_resp;
773 
774 	dprintk("nfsd: COMMIT(3)   %s %u@%Lu\n",
775 				SVCFH_fmt(&argp->fh),
776 				argp->count,
777 				(unsigned long long) argp->offset);
778 
779 	fh_copy(&resp->fh, &argp->fh);
780 	resp->status = nfsd_commit(rqstp, &resp->fh, argp->offset,
781 				   argp->count, resp->verf);
782 	return rpc_success;
783 }
784 
785 
786 /*
787  * NFSv3 Server procedures.
788  * Only the results of non-idempotent operations are cached.
789  */
790 #define nfs3svc_encode_attrstatres	nfs3svc_encode_attrstat
791 #define nfs3svc_encode_wccstatres	nfs3svc_encode_wccstat
792 #define nfsd3_mkdirargs			nfsd3_createargs
793 #define nfsd3_readdirplusargs		nfsd3_readdirargs
794 #define nfsd3_fhandleargs		nfsd_fhandle
795 #define nfsd3_attrstatres		nfsd3_attrstat
796 #define nfsd3_wccstatres		nfsd3_attrstat
797 #define nfsd3_createres			nfsd3_diropres
798 
799 #define ST 1		/* status*/
800 #define FH 17		/* filehandle with length */
801 #define AT 21		/* attributes */
802 #define pAT (1+AT)	/* post attributes - conditional */
803 #define WC (7+pAT)	/* WCC attributes */
804 
805 static const struct svc_procedure nfsd_procedures3[22] = {
806 	[NFS3PROC_NULL] = {
807 		.pc_func = nfsd3_proc_null,
808 		.pc_decode = nfssvc_decode_voidarg,
809 		.pc_encode = nfssvc_encode_voidres,
810 		.pc_argsize = sizeof(struct nfsd_voidargs),
811 		.pc_ressize = sizeof(struct nfsd_voidres),
812 		.pc_cachetype = RC_NOCACHE,
813 		.pc_xdrressize = ST,
814 		.pc_name = "NULL",
815 	},
816 	[NFS3PROC_GETATTR] = {
817 		.pc_func = nfsd3_proc_getattr,
818 		.pc_decode = nfs3svc_decode_fhandleargs,
819 		.pc_encode = nfs3svc_encode_getattrres,
820 		.pc_release = nfs3svc_release_fhandle,
821 		.pc_argsize = sizeof(struct nfsd_fhandle),
822 		.pc_ressize = sizeof(struct nfsd3_attrstatres),
823 		.pc_cachetype = RC_NOCACHE,
824 		.pc_xdrressize = ST+AT,
825 		.pc_name = "GETATTR",
826 	},
827 	[NFS3PROC_SETATTR] = {
828 		.pc_func = nfsd3_proc_setattr,
829 		.pc_decode = nfs3svc_decode_sattrargs,
830 		.pc_encode = nfs3svc_encode_wccstatres,
831 		.pc_release = nfs3svc_release_fhandle,
832 		.pc_argsize = sizeof(struct nfsd3_sattrargs),
833 		.pc_ressize = sizeof(struct nfsd3_wccstatres),
834 		.pc_cachetype = RC_REPLBUFF,
835 		.pc_xdrressize = ST+WC,
836 		.pc_name = "SETATTR",
837 	},
838 	[NFS3PROC_LOOKUP] = {
839 		.pc_func = nfsd3_proc_lookup,
840 		.pc_decode = nfs3svc_decode_diropargs,
841 		.pc_encode = nfs3svc_encode_lookupres,
842 		.pc_release = nfs3svc_release_fhandle2,
843 		.pc_argsize = sizeof(struct nfsd3_diropargs),
844 		.pc_ressize = sizeof(struct nfsd3_diropres),
845 		.pc_cachetype = RC_NOCACHE,
846 		.pc_xdrressize = ST+FH+pAT+pAT,
847 		.pc_name = "LOOKUP",
848 	},
849 	[NFS3PROC_ACCESS] = {
850 		.pc_func = nfsd3_proc_access,
851 		.pc_decode = nfs3svc_decode_accessargs,
852 		.pc_encode = nfs3svc_encode_accessres,
853 		.pc_release = nfs3svc_release_fhandle,
854 		.pc_argsize = sizeof(struct nfsd3_accessargs),
855 		.pc_ressize = sizeof(struct nfsd3_accessres),
856 		.pc_cachetype = RC_NOCACHE,
857 		.pc_xdrressize = ST+pAT+1,
858 		.pc_name = "ACCESS",
859 	},
860 	[NFS3PROC_READLINK] = {
861 		.pc_func = nfsd3_proc_readlink,
862 		.pc_decode = nfs3svc_decode_fhandleargs,
863 		.pc_encode = nfs3svc_encode_readlinkres,
864 		.pc_release = nfs3svc_release_fhandle,
865 		.pc_argsize = sizeof(struct nfsd_fhandle),
866 		.pc_ressize = sizeof(struct nfsd3_readlinkres),
867 		.pc_cachetype = RC_NOCACHE,
868 		.pc_xdrressize = ST+pAT+1+NFS3_MAXPATHLEN/4,
869 		.pc_name = "READLINK",
870 	},
871 	[NFS3PROC_READ] = {
872 		.pc_func = nfsd3_proc_read,
873 		.pc_decode = nfs3svc_decode_readargs,
874 		.pc_encode = nfs3svc_encode_readres,
875 		.pc_release = nfs3svc_release_fhandle,
876 		.pc_argsize = 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_ressize = sizeof(struct nfsd3_writeres),
889 		.pc_cachetype = RC_REPLBUFF,
890 		.pc_xdrressize = ST+WC+4,
891 		.pc_name = "WRITE",
892 	},
893 	[NFS3PROC_CREATE] = {
894 		.pc_func = nfsd3_proc_create,
895 		.pc_decode = nfs3svc_decode_createargs,
896 		.pc_encode = nfs3svc_encode_createres,
897 		.pc_release = nfs3svc_release_fhandle2,
898 		.pc_argsize = sizeof(struct nfsd3_createargs),
899 		.pc_ressize = sizeof(struct nfsd3_createres),
900 		.pc_cachetype = RC_REPLBUFF,
901 		.pc_xdrressize = ST+(1+FH+pAT)+WC,
902 		.pc_name = "CREATE",
903 	},
904 	[NFS3PROC_MKDIR] = {
905 		.pc_func = nfsd3_proc_mkdir,
906 		.pc_decode = nfs3svc_decode_mkdirargs,
907 		.pc_encode = nfs3svc_encode_createres,
908 		.pc_release = nfs3svc_release_fhandle2,
909 		.pc_argsize = sizeof(struct nfsd3_mkdirargs),
910 		.pc_ressize = sizeof(struct nfsd3_createres),
911 		.pc_cachetype = RC_REPLBUFF,
912 		.pc_xdrressize = ST+(1+FH+pAT)+WC,
913 		.pc_name = "MKDIR",
914 	},
915 	[NFS3PROC_SYMLINK] = {
916 		.pc_func = nfsd3_proc_symlink,
917 		.pc_decode = nfs3svc_decode_symlinkargs,
918 		.pc_encode = nfs3svc_encode_createres,
919 		.pc_release = nfs3svc_release_fhandle2,
920 		.pc_argsize = sizeof(struct nfsd3_symlinkargs),
921 		.pc_ressize = sizeof(struct nfsd3_createres),
922 		.pc_cachetype = RC_REPLBUFF,
923 		.pc_xdrressize = ST+(1+FH+pAT)+WC,
924 		.pc_name = "SYMLINK",
925 	},
926 	[NFS3PROC_MKNOD] = {
927 		.pc_func = nfsd3_proc_mknod,
928 		.pc_decode = nfs3svc_decode_mknodargs,
929 		.pc_encode = nfs3svc_encode_createres,
930 		.pc_release = nfs3svc_release_fhandle2,
931 		.pc_argsize = sizeof(struct nfsd3_mknodargs),
932 		.pc_ressize = sizeof(struct nfsd3_createres),
933 		.pc_cachetype = RC_REPLBUFF,
934 		.pc_xdrressize = ST+(1+FH+pAT)+WC,
935 		.pc_name = "MKNOD",
936 	},
937 	[NFS3PROC_REMOVE] = {
938 		.pc_func = nfsd3_proc_remove,
939 		.pc_decode = nfs3svc_decode_diropargs,
940 		.pc_encode = nfs3svc_encode_wccstatres,
941 		.pc_release = nfs3svc_release_fhandle,
942 		.pc_argsize = sizeof(struct nfsd3_diropargs),
943 		.pc_ressize = sizeof(struct nfsd3_wccstatres),
944 		.pc_cachetype = RC_REPLBUFF,
945 		.pc_xdrressize = ST+WC,
946 		.pc_name = "REMOVE",
947 	},
948 	[NFS3PROC_RMDIR] = {
949 		.pc_func = nfsd3_proc_rmdir,
950 		.pc_decode = nfs3svc_decode_diropargs,
951 		.pc_encode = nfs3svc_encode_wccstatres,
952 		.pc_release = nfs3svc_release_fhandle,
953 		.pc_argsize = sizeof(struct nfsd3_diropargs),
954 		.pc_ressize = sizeof(struct nfsd3_wccstatres),
955 		.pc_cachetype = RC_REPLBUFF,
956 		.pc_xdrressize = ST+WC,
957 		.pc_name = "RMDIR",
958 	},
959 	[NFS3PROC_RENAME] = {
960 		.pc_func = nfsd3_proc_rename,
961 		.pc_decode = nfs3svc_decode_renameargs,
962 		.pc_encode = nfs3svc_encode_renameres,
963 		.pc_release = nfs3svc_release_fhandle2,
964 		.pc_argsize = sizeof(struct nfsd3_renameargs),
965 		.pc_ressize = sizeof(struct nfsd3_renameres),
966 		.pc_cachetype = RC_REPLBUFF,
967 		.pc_xdrressize = ST+WC+WC,
968 		.pc_name = "RENAME",
969 	},
970 	[NFS3PROC_LINK] = {
971 		.pc_func = nfsd3_proc_link,
972 		.pc_decode = nfs3svc_decode_linkargs,
973 		.pc_encode = nfs3svc_encode_linkres,
974 		.pc_release = nfs3svc_release_fhandle2,
975 		.pc_argsize = sizeof(struct nfsd3_linkargs),
976 		.pc_ressize = sizeof(struct nfsd3_linkres),
977 		.pc_cachetype = RC_REPLBUFF,
978 		.pc_xdrressize = ST+pAT+WC,
979 		.pc_name = "LINK",
980 	},
981 	[NFS3PROC_READDIR] = {
982 		.pc_func = nfsd3_proc_readdir,
983 		.pc_decode = nfs3svc_decode_readdirargs,
984 		.pc_encode = nfs3svc_encode_readdirres,
985 		.pc_release = nfs3svc_release_fhandle,
986 		.pc_argsize = sizeof(struct nfsd3_readdirargs),
987 		.pc_ressize = sizeof(struct nfsd3_readdirres),
988 		.pc_cachetype = RC_NOCACHE,
989 		.pc_name = "READDIR",
990 	},
991 	[NFS3PROC_READDIRPLUS] = {
992 		.pc_func = nfsd3_proc_readdirplus,
993 		.pc_decode = nfs3svc_decode_readdirplusargs,
994 		.pc_encode = nfs3svc_encode_readdirres,
995 		.pc_release = nfs3svc_release_fhandle,
996 		.pc_argsize = sizeof(struct nfsd3_readdirplusargs),
997 		.pc_ressize = sizeof(struct nfsd3_readdirres),
998 		.pc_cachetype = RC_NOCACHE,
999 		.pc_name = "READDIRPLUS",
1000 	},
1001 	[NFS3PROC_FSSTAT] = {
1002 		.pc_func = nfsd3_proc_fsstat,
1003 		.pc_decode = nfs3svc_decode_fhandleargs,
1004 		.pc_encode = nfs3svc_encode_fsstatres,
1005 		.pc_argsize = sizeof(struct nfsd3_fhandleargs),
1006 		.pc_ressize = sizeof(struct nfsd3_fsstatres),
1007 		.pc_cachetype = RC_NOCACHE,
1008 		.pc_xdrressize = ST+pAT+2*6+1,
1009 		.pc_name = "FSSTAT",
1010 	},
1011 	[NFS3PROC_FSINFO] = {
1012 		.pc_func = nfsd3_proc_fsinfo,
1013 		.pc_decode = nfs3svc_decode_fhandleargs,
1014 		.pc_encode = nfs3svc_encode_fsinfores,
1015 		.pc_argsize = sizeof(struct nfsd3_fhandleargs),
1016 		.pc_ressize = sizeof(struct nfsd3_fsinfores),
1017 		.pc_cachetype = RC_NOCACHE,
1018 		.pc_xdrressize = ST+pAT+12,
1019 		.pc_name = "FSINFO",
1020 	},
1021 	[NFS3PROC_PATHCONF] = {
1022 		.pc_func = nfsd3_proc_pathconf,
1023 		.pc_decode = nfs3svc_decode_fhandleargs,
1024 		.pc_encode = nfs3svc_encode_pathconfres,
1025 		.pc_argsize = sizeof(struct nfsd3_fhandleargs),
1026 		.pc_ressize = sizeof(struct nfsd3_pathconfres),
1027 		.pc_cachetype = RC_NOCACHE,
1028 		.pc_xdrressize = ST+pAT+6,
1029 		.pc_name = "PATHCONF",
1030 	},
1031 	[NFS3PROC_COMMIT] = {
1032 		.pc_func = nfsd3_proc_commit,
1033 		.pc_decode = nfs3svc_decode_commitargs,
1034 		.pc_encode = nfs3svc_encode_commitres,
1035 		.pc_release = nfs3svc_release_fhandle,
1036 		.pc_argsize = sizeof(struct nfsd3_commitargs),
1037 		.pc_ressize = sizeof(struct nfsd3_commitres),
1038 		.pc_cachetype = RC_NOCACHE,
1039 		.pc_xdrressize = ST+WC+2,
1040 		.pc_name = "COMMIT",
1041 	},
1042 };
1043 
1044 static unsigned int nfsd_count3[ARRAY_SIZE(nfsd_procedures3)];
1045 const struct svc_version nfsd_version3 = {
1046 	.vs_vers	= 3,
1047 	.vs_nproc	= 22,
1048 	.vs_proc	= nfsd_procedures3,
1049 	.vs_dispatch	= nfsd_dispatch,
1050 	.vs_count	= nfsd_count3,
1051 	.vs_xdrsize	= NFS3_SVC_XDRSIZE,
1052 };
1053