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