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 
12 #include "cache.h"
13 #include "xdr3.h"
14 #include "vfs.h"
15 
16 #define NFSDDBG_FACILITY		NFSDDBG_PROC
17 
18 static int	nfs3_ftypes[] = {
19 	0,			/* NF3NON */
20 	S_IFREG,		/* NF3REG */
21 	S_IFDIR,		/* NF3DIR */
22 	S_IFBLK,		/* NF3BLK */
23 	S_IFCHR,		/* NF3CHR */
24 	S_IFLNK,		/* NF3LNK */
25 	S_IFSOCK,		/* NF3SOCK */
26 	S_IFIFO,		/* NF3FIFO */
27 };
28 
29 /*
30  * NULL call.
31  */
32 static __be32
nfsd3_proc_null(struct svc_rqst * rqstp)33 nfsd3_proc_null(struct svc_rqst *rqstp)
34 {
35 	return rpc_success;
36 }
37 
38 /*
39  * Get a file's attributes
40  */
41 static __be32
nfsd3_proc_getattr(struct svc_rqst * rqstp)42 nfsd3_proc_getattr(struct svc_rqst *rqstp)
43 {
44 	struct nfsd_fhandle *argp = rqstp->rq_argp;
45 	struct nfsd3_attrstat *resp = rqstp->rq_resp;
46 
47 	dprintk("nfsd: GETATTR(3)  %s\n",
48 		SVCFH_fmt(&argp->fh));
49 
50 	fh_copy(&resp->fh, &argp->fh);
51 	resp->status = fh_verify(rqstp, &resp->fh, 0,
52 				 NFSD_MAY_NOP | NFSD_MAY_BYPASS_GSS_ON_ROOT);
53 	if (resp->status != nfs_ok)
54 		goto out;
55 
56 	resp->status = fh_getattr(&resp->fh, &resp->stat);
57 out:
58 	return rpc_success;
59 }
60 
61 /*
62  * Set a file's attributes
63  */
64 static __be32
nfsd3_proc_setattr(struct svc_rqst * rqstp)65 nfsd3_proc_setattr(struct svc_rqst *rqstp)
66 {
67 	struct nfsd3_sattrargs *argp = rqstp->rq_argp;
68 	struct nfsd3_attrstat *resp = rqstp->rq_resp;
69 
70 	dprintk("nfsd: SETATTR(3)  %s\n",
71 				SVCFH_fmt(&argp->fh));
72 
73 	fh_copy(&resp->fh, &argp->fh);
74 	resp->status = nfsd_setattr(rqstp, &resp->fh, &argp->attrs,
75 				    argp->check_guard, argp->guardtime);
76 	return rpc_success;
77 }
78 
79 /*
80  * Look up a path name component
81  */
82 static __be32
nfsd3_proc_lookup(struct svc_rqst * rqstp)83 nfsd3_proc_lookup(struct svc_rqst *rqstp)
84 {
85 	struct nfsd3_diropargs *argp = rqstp->rq_argp;
86 	struct nfsd3_diropres  *resp = rqstp->rq_resp;
87 
88 	dprintk("nfsd: LOOKUP(3)   %s %.*s\n",
89 				SVCFH_fmt(&argp->fh),
90 				argp->len,
91 				argp->name);
92 
93 	fh_copy(&resp->dirfh, &argp->fh);
94 	fh_init(&resp->fh, NFS3_FHSIZE);
95 
96 	resp->status = nfsd_lookup(rqstp, &resp->dirfh,
97 				   argp->name, argp->len,
98 				   &resp->fh);
99 	return rpc_success;
100 }
101 
102 /*
103  * Check file access
104  */
105 static __be32
nfsd3_proc_access(struct svc_rqst * rqstp)106 nfsd3_proc_access(struct svc_rqst *rqstp)
107 {
108 	struct nfsd3_accessargs *argp = rqstp->rq_argp;
109 	struct nfsd3_accessres *resp = rqstp->rq_resp;
110 
111 	dprintk("nfsd: ACCESS(3)   %s 0x%x\n",
112 				SVCFH_fmt(&argp->fh),
113 				argp->access);
114 
115 	fh_copy(&resp->fh, &argp->fh);
116 	resp->access = argp->access;
117 	resp->status = nfsd_access(rqstp, &resp->fh, &resp->access, NULL);
118 	return rpc_success;
119 }
120 
121 /*
122  * Read a symlink.
123  */
124 static __be32
nfsd3_proc_readlink(struct svc_rqst * rqstp)125 nfsd3_proc_readlink(struct svc_rqst *rqstp)
126 {
127 	struct nfsd3_readlinkargs *argp = rqstp->rq_argp;
128 	struct nfsd3_readlinkres *resp = rqstp->rq_resp;
129 
130 	dprintk("nfsd: READLINK(3) %s\n", SVCFH_fmt(&argp->fh));
131 
132 	/* Read the symlink. */
133 	fh_copy(&resp->fh, &argp->fh);
134 	resp->len = NFS3_MAXPATHLEN;
135 	resp->status = nfsd_readlink(rqstp, &resp->fh, argp->buffer, &resp->len);
136 	return rpc_success;
137 }
138 
139 /*
140  * Read a portion of a file.
141  */
142 static __be32
nfsd3_proc_read(struct svc_rqst * rqstp)143 nfsd3_proc_read(struct svc_rqst *rqstp)
144 {
145 	struct nfsd3_readargs *argp = rqstp->rq_argp;
146 	struct nfsd3_readres *resp = rqstp->rq_resp;
147 	u32	max_blocksize = svc_max_payload(rqstp);
148 	unsigned long cnt = min(argp->count, max_blocksize);
149 
150 	dprintk("nfsd: READ(3) %s %lu bytes at %Lu\n",
151 				SVCFH_fmt(&argp->fh),
152 				(unsigned long) argp->count,
153 				(unsigned long long) argp->offset);
154 
155 	/* Obtain buffer pointer for payload.
156 	 * 1 (status) + 22 (post_op_attr) + 1 (count) + 1 (eof)
157 	 * + 1 (xdr opaque byte count) = 26
158 	 */
159 	resp->count = cnt;
160 	svc_reserve_auth(rqstp, ((1 + NFS3_POST_OP_ATTR_WORDS + 3)<<2) + resp->count +4);
161 
162 	fh_copy(&resp->fh, &argp->fh);
163 	resp->status = nfsd_read(rqstp, &resp->fh, argp->offset,
164 				 rqstp->rq_vec, argp->vlen, &resp->count,
165 				 &resp->eof);
166 	return rpc_success;
167 }
168 
169 /*
170  * Write data to a file
171  */
172 static __be32
nfsd3_proc_write(struct svc_rqst * rqstp)173 nfsd3_proc_write(struct svc_rqst *rqstp)
174 {
175 	struct nfsd3_writeargs *argp = rqstp->rq_argp;
176 	struct nfsd3_writeres *resp = rqstp->rq_resp;
177 	unsigned long cnt = argp->len;
178 	unsigned int nvecs;
179 
180 	dprintk("nfsd: WRITE(3)    %s %d bytes at %Lu%s\n",
181 				SVCFH_fmt(&argp->fh),
182 				argp->len,
183 				(unsigned long long) argp->offset,
184 				argp->stable? " stable" : "");
185 
186 	fh_copy(&resp->fh, &argp->fh);
187 	resp->committed = argp->stable;
188 	nvecs = svc_fill_write_vector(rqstp, rqstp->rq_arg.pages,
189 				      &argp->first, cnt);
190 	if (!nvecs) {
191 		resp->status = nfserr_io;
192 		goto out;
193 	}
194 	resp->status = nfsd_write(rqstp, &resp->fh, argp->offset,
195 				  rqstp->rq_vec, nvecs, &cnt,
196 				  resp->committed, resp->verf);
197 	resp->count = cnt;
198 out:
199 	return rpc_success;
200 }
201 
202 /*
203  * With NFSv3, CREATE processing is a lot easier than with NFSv2.
204  * At least in theory; we'll see how it fares in practice when the
205  * first reports about SunOS compatibility problems start to pour in...
206  */
207 static __be32
nfsd3_proc_create(struct svc_rqst * rqstp)208 nfsd3_proc_create(struct svc_rqst *rqstp)
209 {
210 	struct nfsd3_createargs *argp = rqstp->rq_argp;
211 	struct nfsd3_diropres *resp = rqstp->rq_resp;
212 	svc_fh		*dirfhp, *newfhp = NULL;
213 	struct iattr	*attr;
214 
215 	dprintk("nfsd: CREATE(3)   %s %.*s\n",
216 				SVCFH_fmt(&argp->fh),
217 				argp->len,
218 				argp->name);
219 
220 	dirfhp = fh_copy(&resp->dirfh, &argp->fh);
221 	newfhp = fh_init(&resp->fh, NFS3_FHSIZE);
222 	attr   = &argp->attrs;
223 
224 	/* Unfudge the mode bits */
225 	attr->ia_mode &= ~S_IFMT;
226 	if (!(attr->ia_valid & ATTR_MODE)) {
227 		attr->ia_valid |= ATTR_MODE;
228 		attr->ia_mode = S_IFREG;
229 	} else {
230 		attr->ia_mode = (attr->ia_mode & ~S_IFMT) | S_IFREG;
231 	}
232 
233 	/* Now create the file and set attributes */
234 	resp->status = do_nfsd_create(rqstp, dirfhp, argp->name, argp->len,
235 				      attr, newfhp, argp->createmode,
236 				      (u32 *)argp->verf, NULL, NULL);
237 	return rpc_success;
238 }
239 
240 /*
241  * Make directory. This operation is not idempotent.
242  */
243 static __be32
nfsd3_proc_mkdir(struct svc_rqst * rqstp)244 nfsd3_proc_mkdir(struct svc_rqst *rqstp)
245 {
246 	struct nfsd3_createargs *argp = rqstp->rq_argp;
247 	struct nfsd3_diropres *resp = rqstp->rq_resp;
248 
249 	dprintk("nfsd: MKDIR(3)    %s %.*s\n",
250 				SVCFH_fmt(&argp->fh),
251 				argp->len,
252 				argp->name);
253 
254 	argp->attrs.ia_valid &= ~ATTR_SIZE;
255 	fh_copy(&resp->dirfh, &argp->fh);
256 	fh_init(&resp->fh, NFS3_FHSIZE);
257 	resp->status = nfsd_create(rqstp, &resp->dirfh, argp->name, argp->len,
258 				   &argp->attrs, S_IFDIR, 0, &resp->fh);
259 	fh_unlock(&resp->dirfh);
260 	return rpc_success;
261 }
262 
263 static __be32
nfsd3_proc_symlink(struct svc_rqst * rqstp)264 nfsd3_proc_symlink(struct svc_rqst *rqstp)
265 {
266 	struct nfsd3_symlinkargs *argp = rqstp->rq_argp;
267 	struct nfsd3_diropres *resp = rqstp->rq_resp;
268 
269 	if (argp->tlen == 0) {
270 		resp->status = nfserr_inval;
271 		goto out;
272 	}
273 	if (argp->tlen > NFS3_MAXPATHLEN) {
274 		resp->status = nfserr_nametoolong;
275 		goto out;
276 	}
277 
278 	argp->tname = svc_fill_symlink_pathname(rqstp, &argp->first,
279 						page_address(rqstp->rq_arg.pages[0]),
280 						argp->tlen);
281 	if (IS_ERR(argp->tname)) {
282 		resp->status = nfserrno(PTR_ERR(argp->tname));
283 		goto out;
284 	}
285 
286 	dprintk("nfsd: SYMLINK(3)  %s %.*s -> %.*s\n",
287 				SVCFH_fmt(&argp->ffh),
288 				argp->flen, argp->fname,
289 				argp->tlen, argp->tname);
290 
291 	fh_copy(&resp->dirfh, &argp->ffh);
292 	fh_init(&resp->fh, NFS3_FHSIZE);
293 	resp->status = nfsd_symlink(rqstp, &resp->dirfh, argp->fname,
294 				    argp->flen, argp->tname, &resp->fh);
295 	kfree(argp->tname);
296 out:
297 	return rpc_success;
298 }
299 
300 /*
301  * Make socket/fifo/device.
302  */
303 static __be32
nfsd3_proc_mknod(struct svc_rqst * rqstp)304 nfsd3_proc_mknod(struct svc_rqst *rqstp)
305 {
306 	struct nfsd3_mknodargs *argp = rqstp->rq_argp;
307 	struct nfsd3_diropres  *resp = rqstp->rq_resp;
308 	int type;
309 	dev_t	rdev = 0;
310 
311 	dprintk("nfsd: MKNOD(3)    %s %.*s\n",
312 				SVCFH_fmt(&argp->fh),
313 				argp->len,
314 				argp->name);
315 
316 	fh_copy(&resp->dirfh, &argp->fh);
317 	fh_init(&resp->fh, NFS3_FHSIZE);
318 
319 	if (argp->ftype == NF3CHR || argp->ftype == NF3BLK) {
320 		rdev = MKDEV(argp->major, argp->minor);
321 		if (MAJOR(rdev) != argp->major ||
322 		    MINOR(rdev) != argp->minor) {
323 			resp->status = nfserr_inval;
324 			goto out;
325 		}
326 	} else if (argp->ftype != NF3SOCK && argp->ftype != NF3FIFO) {
327 		resp->status = nfserr_badtype;
328 		goto out;
329 	}
330 
331 	type = nfs3_ftypes[argp->ftype];
332 	resp->status = nfsd_create(rqstp, &resp->dirfh, argp->name, argp->len,
333 				   &argp->attrs, type, rdev, &resp->fh);
334 	fh_unlock(&resp->dirfh);
335 out:
336 	return rpc_success;
337 }
338 
339 /*
340  * Remove file/fifo/socket etc.
341  */
342 static __be32
nfsd3_proc_remove(struct svc_rqst * rqstp)343 nfsd3_proc_remove(struct svc_rqst *rqstp)
344 {
345 	struct nfsd3_diropargs *argp = rqstp->rq_argp;
346 	struct nfsd3_attrstat *resp = rqstp->rq_resp;
347 
348 	dprintk("nfsd: REMOVE(3)   %s %.*s\n",
349 				SVCFH_fmt(&argp->fh),
350 				argp->len,
351 				argp->name);
352 
353 	/* Unlink. -S_IFDIR means file must not be a directory */
354 	fh_copy(&resp->fh, &argp->fh);
355 	resp->status = nfsd_unlink(rqstp, &resp->fh, -S_IFDIR,
356 				   argp->name, argp->len);
357 	fh_unlock(&resp->fh);
358 	return rpc_success;
359 }
360 
361 /*
362  * Remove a directory
363  */
364 static __be32
nfsd3_proc_rmdir(struct svc_rqst * rqstp)365 nfsd3_proc_rmdir(struct svc_rqst *rqstp)
366 {
367 	struct nfsd3_diropargs *argp = rqstp->rq_argp;
368 	struct nfsd3_attrstat *resp = rqstp->rq_resp;
369 
370 	dprintk("nfsd: RMDIR(3)    %s %.*s\n",
371 				SVCFH_fmt(&argp->fh),
372 				argp->len,
373 				argp->name);
374 
375 	fh_copy(&resp->fh, &argp->fh);
376 	resp->status = nfsd_unlink(rqstp, &resp->fh, S_IFDIR,
377 				   argp->name, argp->len);
378 	fh_unlock(&resp->fh);
379 	return rpc_success;
380 }
381 
382 static __be32
nfsd3_proc_rename(struct svc_rqst * rqstp)383 nfsd3_proc_rename(struct svc_rqst *rqstp)
384 {
385 	struct nfsd3_renameargs *argp = rqstp->rq_argp;
386 	struct nfsd3_renameres *resp = rqstp->rq_resp;
387 
388 	dprintk("nfsd: RENAME(3)   %s %.*s ->\n",
389 				SVCFH_fmt(&argp->ffh),
390 				argp->flen,
391 				argp->fname);
392 	dprintk("nfsd: -> %s %.*s\n",
393 				SVCFH_fmt(&argp->tfh),
394 				argp->tlen,
395 				argp->tname);
396 
397 	fh_copy(&resp->ffh, &argp->ffh);
398 	fh_copy(&resp->tfh, &argp->tfh);
399 	resp->status = nfsd_rename(rqstp, &resp->ffh, argp->fname, argp->flen,
400 				   &resp->tfh, argp->tname, argp->tlen);
401 	return rpc_success;
402 }
403 
404 static __be32
nfsd3_proc_link(struct svc_rqst * rqstp)405 nfsd3_proc_link(struct svc_rqst *rqstp)
406 {
407 	struct nfsd3_linkargs *argp = rqstp->rq_argp;
408 	struct nfsd3_linkres  *resp = rqstp->rq_resp;
409 
410 	dprintk("nfsd: LINK(3)     %s ->\n",
411 				SVCFH_fmt(&argp->ffh));
412 	dprintk("nfsd:   -> %s %.*s\n",
413 				SVCFH_fmt(&argp->tfh),
414 				argp->tlen,
415 				argp->tname);
416 
417 	fh_copy(&resp->fh,  &argp->ffh);
418 	fh_copy(&resp->tfh, &argp->tfh);
419 	resp->status = nfsd_link(rqstp, &resp->tfh, argp->tname, argp->tlen,
420 				 &resp->fh);
421 	return rpc_success;
422 }
423 
424 /*
425  * Read a portion of a directory.
426  */
427 static __be32
nfsd3_proc_readdir(struct svc_rqst * rqstp)428 nfsd3_proc_readdir(struct svc_rqst *rqstp)
429 {
430 	struct nfsd3_readdirargs *argp = rqstp->rq_argp;
431 	struct nfsd3_readdirres  *resp = rqstp->rq_resp;
432 	int		count = 0;
433 	struct page	**p;
434 	caddr_t		page_addr = NULL;
435 
436 	dprintk("nfsd: READDIR(3)  %s %d bytes at %d\n",
437 				SVCFH_fmt(&argp->fh),
438 				argp->count, (u32) argp->cookie);
439 
440 	/* Make sure we've room for the NULL ptr & eof flag, and shrink to
441 	 * client read size */
442 	count = (argp->count >> 2) - 2;
443 
444 	/* Read directory and encode entries on the fly */
445 	fh_copy(&resp->fh, &argp->fh);
446 
447 	resp->buflen = count;
448 	resp->common.err = nfs_ok;
449 	resp->buffer = argp->buffer;
450 	resp->rqstp = rqstp;
451 	resp->status = nfsd_readdir(rqstp, &resp->fh, (loff_t *)&argp->cookie,
452 				    &resp->common, nfs3svc_encode_entry);
453 	memcpy(resp->verf, argp->verf, 8);
454 	count = 0;
455 	for (p = rqstp->rq_respages + 1; p < rqstp->rq_next_page; p++) {
456 		page_addr = page_address(*p);
457 
458 		if (((caddr_t)resp->buffer >= page_addr) &&
459 		    ((caddr_t)resp->buffer < page_addr + PAGE_SIZE)) {
460 			count += (caddr_t)resp->buffer - page_addr;
461 			break;
462 		}
463 		count += PAGE_SIZE;
464 	}
465 	resp->count = count >> 2;
466 	if (resp->offset) {
467 		loff_t offset = argp->cookie;
468 
469 		if (unlikely(resp->offset1)) {
470 			/* we ended up with offset on a page boundary */
471 			*resp->offset = htonl(offset >> 32);
472 			*resp->offset1 = htonl(offset & 0xffffffff);
473 			resp->offset1 = NULL;
474 		} else {
475 			xdr_encode_hyper(resp->offset, offset);
476 		}
477 		resp->offset = NULL;
478 	}
479 
480 	return rpc_success;
481 }
482 
483 /*
484  * Read a portion of a directory, including file handles and attrs.
485  * For now, we choose to ignore the dircount parameter.
486  */
487 static __be32
nfsd3_proc_readdirplus(struct svc_rqst * rqstp)488 nfsd3_proc_readdirplus(struct svc_rqst *rqstp)
489 {
490 	struct nfsd3_readdirargs *argp = rqstp->rq_argp;
491 	struct nfsd3_readdirres  *resp = rqstp->rq_resp;
492 	int	count = 0;
493 	loff_t	offset;
494 	struct page **p;
495 	caddr_t	page_addr = NULL;
496 
497 	dprintk("nfsd: READDIR+(3) %s %d bytes at %d\n",
498 				SVCFH_fmt(&argp->fh),
499 				argp->count, (u32) argp->cookie);
500 
501 	/* Convert byte count to number of words (i.e. >> 2),
502 	 * and reserve room for the NULL ptr & eof flag (-2 words) */
503 	resp->count = (argp->count >> 2) - 2;
504 
505 	/* Read directory and encode entries on the fly */
506 	fh_copy(&resp->fh, &argp->fh);
507 
508 	resp->common.err = nfs_ok;
509 	resp->buffer = argp->buffer;
510 	resp->buflen = resp->count;
511 	resp->rqstp = rqstp;
512 	offset = argp->cookie;
513 
514 	resp->status = fh_verify(rqstp, &resp->fh, S_IFDIR, NFSD_MAY_NOP);
515 	if (resp->status != nfs_ok)
516 		goto out;
517 
518 	if (resp->fh.fh_export->ex_flags & NFSEXP_NOREADDIRPLUS) {
519 		resp->status = nfserr_notsupp;
520 		goto out;
521 	}
522 
523 	resp->status = nfsd_readdir(rqstp, &resp->fh, &offset,
524 				    &resp->common, nfs3svc_encode_entry_plus);
525 	memcpy(resp->verf, argp->verf, 8);
526 	for (p = rqstp->rq_respages + 1; p < rqstp->rq_next_page; p++) {
527 		page_addr = page_address(*p);
528 
529 		if (((caddr_t)resp->buffer >= page_addr) &&
530 		    ((caddr_t)resp->buffer < page_addr + PAGE_SIZE)) {
531 			count += (caddr_t)resp->buffer - page_addr;
532 			break;
533 		}
534 		count += PAGE_SIZE;
535 	}
536 	resp->count = count >> 2;
537 	if (resp->offset) {
538 		if (unlikely(resp->offset1)) {
539 			/* we ended up with offset on a page boundary */
540 			*resp->offset = htonl(offset >> 32);
541 			*resp->offset1 = htonl(offset & 0xffffffff);
542 			resp->offset1 = NULL;
543 		} else {
544 			xdr_encode_hyper(resp->offset, offset);
545 		}
546 		resp->offset = NULL;
547 	}
548 
549 out:
550 	return rpc_success;
551 }
552 
553 /*
554  * Get file system stats
555  */
556 static __be32
nfsd3_proc_fsstat(struct svc_rqst * rqstp)557 nfsd3_proc_fsstat(struct svc_rqst *rqstp)
558 {
559 	struct nfsd_fhandle *argp = rqstp->rq_argp;
560 	struct nfsd3_fsstatres *resp = rqstp->rq_resp;
561 
562 	dprintk("nfsd: FSSTAT(3)   %s\n",
563 				SVCFH_fmt(&argp->fh));
564 
565 	resp->status = nfsd_statfs(rqstp, &argp->fh, &resp->stats, 0);
566 	fh_put(&argp->fh);
567 	return rpc_success;
568 }
569 
570 /*
571  * Get file system info
572  */
573 static __be32
nfsd3_proc_fsinfo(struct svc_rqst * rqstp)574 nfsd3_proc_fsinfo(struct svc_rqst *rqstp)
575 {
576 	struct nfsd_fhandle *argp = rqstp->rq_argp;
577 	struct nfsd3_fsinfores *resp = rqstp->rq_resp;
578 	u32	max_blocksize = svc_max_payload(rqstp);
579 
580 	dprintk("nfsd: FSINFO(3)   %s\n",
581 				SVCFH_fmt(&argp->fh));
582 
583 	resp->f_rtmax  = max_blocksize;
584 	resp->f_rtpref = max_blocksize;
585 	resp->f_rtmult = PAGE_SIZE;
586 	resp->f_wtmax  = max_blocksize;
587 	resp->f_wtpref = max_blocksize;
588 	resp->f_wtmult = PAGE_SIZE;
589 	resp->f_dtpref = max_blocksize;
590 	resp->f_maxfilesize = ~(u32) 0;
591 	resp->f_properties = NFS3_FSF_DEFAULT;
592 
593 	resp->status = fh_verify(rqstp, &argp->fh, 0,
594 				 NFSD_MAY_NOP | NFSD_MAY_BYPASS_GSS_ON_ROOT);
595 
596 	/* Check special features of the file system. May request
597 	 * different read/write sizes for file systems known to have
598 	 * problems with large blocks */
599 	if (resp->status == nfs_ok) {
600 		struct super_block *sb = argp->fh.fh_dentry->d_sb;
601 
602 		/* Note that we don't care for remote fs's here */
603 		if (sb->s_magic == MSDOS_SUPER_MAGIC) {
604 			resp->f_properties = NFS3_FSF_BILLYBOY;
605 		}
606 		resp->f_maxfilesize = sb->s_maxbytes;
607 	}
608 
609 	fh_put(&argp->fh);
610 	return rpc_success;
611 }
612 
613 /*
614  * Get pathconf info for the specified file
615  */
616 static __be32
nfsd3_proc_pathconf(struct svc_rqst * rqstp)617 nfsd3_proc_pathconf(struct svc_rqst *rqstp)
618 {
619 	struct nfsd_fhandle *argp = rqstp->rq_argp;
620 	struct nfsd3_pathconfres *resp = rqstp->rq_resp;
621 
622 	dprintk("nfsd: PATHCONF(3) %s\n",
623 				SVCFH_fmt(&argp->fh));
624 
625 	/* Set default pathconf */
626 	resp->p_link_max = 255;		/* at least */
627 	resp->p_name_max = 255;		/* at least */
628 	resp->p_no_trunc = 0;
629 	resp->p_chown_restricted = 1;
630 	resp->p_case_insensitive = 0;
631 	resp->p_case_preserving = 1;
632 
633 	resp->status = fh_verify(rqstp, &argp->fh, 0, NFSD_MAY_NOP);
634 
635 	if (resp->status == nfs_ok) {
636 		struct super_block *sb = argp->fh.fh_dentry->d_sb;
637 
638 		/* Note that we don't care for remote fs's here */
639 		switch (sb->s_magic) {
640 		case EXT2_SUPER_MAGIC:
641 			resp->p_link_max = EXT2_LINK_MAX;
642 			resp->p_name_max = EXT2_NAME_LEN;
643 			break;
644 		case MSDOS_SUPER_MAGIC:
645 			resp->p_case_insensitive = 1;
646 			resp->p_case_preserving  = 0;
647 			break;
648 		}
649 	}
650 
651 	fh_put(&argp->fh);
652 	return rpc_success;
653 }
654 
655 /*
656  * Commit a file (range) to stable storage.
657  */
658 static __be32
nfsd3_proc_commit(struct svc_rqst * rqstp)659 nfsd3_proc_commit(struct svc_rqst *rqstp)
660 {
661 	struct nfsd3_commitargs *argp = rqstp->rq_argp;
662 	struct nfsd3_commitres *resp = rqstp->rq_resp;
663 
664 	dprintk("nfsd: COMMIT(3)   %s %u@%Lu\n",
665 				SVCFH_fmt(&argp->fh),
666 				argp->count,
667 				(unsigned long long) argp->offset);
668 
669 	if (argp->offset > NFS_OFFSET_MAX) {
670 		resp->status = nfserr_inval;
671 		goto out;
672 	}
673 
674 	fh_copy(&resp->fh, &argp->fh);
675 	resp->status = nfsd_commit(rqstp, &resp->fh, argp->offset,
676 				   argp->count, resp->verf);
677 out:
678 	return rpc_success;
679 }
680 
681 
682 /*
683  * NFSv3 Server procedures.
684  * Only the results of non-idempotent operations are cached.
685  */
686 #define nfs3svc_decode_fhandleargs	nfs3svc_decode_fhandle
687 #define nfs3svc_encode_attrstatres	nfs3svc_encode_attrstat
688 #define nfs3svc_encode_wccstatres	nfs3svc_encode_wccstat
689 #define nfsd3_mkdirargs			nfsd3_createargs
690 #define nfsd3_readdirplusargs		nfsd3_readdirargs
691 #define nfsd3_fhandleargs		nfsd_fhandle
692 #define nfsd3_fhandleres		nfsd3_attrstat
693 #define nfsd3_attrstatres		nfsd3_attrstat
694 #define nfsd3_wccstatres		nfsd3_attrstat
695 #define nfsd3_createres			nfsd3_diropres
696 #define nfsd3_voidres			nfsd3_voidargs
697 struct nfsd3_voidargs { int dummy; };
698 
699 #define ST 1		/* status*/
700 #define FH 17		/* filehandle with length */
701 #define AT 21		/* attributes */
702 #define pAT (1+AT)	/* post attributes - conditional */
703 #define WC (7+pAT)	/* WCC attributes */
704 
705 static const struct svc_procedure nfsd_procedures3[22] = {
706 	[NFS3PROC_NULL] = {
707 		.pc_func = nfsd3_proc_null,
708 		.pc_decode = nfs3svc_decode_voidarg,
709 		.pc_encode = nfs3svc_encode_voidres,
710 		.pc_argsize = sizeof(struct nfsd3_voidargs),
711 		.pc_ressize = sizeof(struct nfsd3_voidres),
712 		.pc_cachetype = RC_NOCACHE,
713 		.pc_xdrressize = ST,
714 	},
715 	[NFS3PROC_GETATTR] = {
716 		.pc_func = nfsd3_proc_getattr,
717 		.pc_decode = nfs3svc_decode_fhandleargs,
718 		.pc_encode = nfs3svc_encode_attrstatres,
719 		.pc_release = nfs3svc_release_fhandle,
720 		.pc_argsize = sizeof(struct nfsd3_fhandleargs),
721 		.pc_ressize = sizeof(struct nfsd3_attrstatres),
722 		.pc_cachetype = RC_NOCACHE,
723 		.pc_xdrressize = ST+AT,
724 	},
725 	[NFS3PROC_SETATTR] = {
726 		.pc_func = nfsd3_proc_setattr,
727 		.pc_decode = nfs3svc_decode_sattrargs,
728 		.pc_encode = nfs3svc_encode_wccstatres,
729 		.pc_release = nfs3svc_release_fhandle,
730 		.pc_argsize = sizeof(struct nfsd3_sattrargs),
731 		.pc_ressize = sizeof(struct nfsd3_wccstatres),
732 		.pc_cachetype = RC_REPLBUFF,
733 		.pc_xdrressize = ST+WC,
734 	},
735 	[NFS3PROC_LOOKUP] = {
736 		.pc_func = nfsd3_proc_lookup,
737 		.pc_decode = nfs3svc_decode_diropargs,
738 		.pc_encode = nfs3svc_encode_diropres,
739 		.pc_release = nfs3svc_release_fhandle2,
740 		.pc_argsize = sizeof(struct nfsd3_diropargs),
741 		.pc_ressize = sizeof(struct nfsd3_diropres),
742 		.pc_cachetype = RC_NOCACHE,
743 		.pc_xdrressize = ST+FH+pAT+pAT,
744 	},
745 	[NFS3PROC_ACCESS] = {
746 		.pc_func = nfsd3_proc_access,
747 		.pc_decode = nfs3svc_decode_accessargs,
748 		.pc_encode = nfs3svc_encode_accessres,
749 		.pc_release = nfs3svc_release_fhandle,
750 		.pc_argsize = sizeof(struct nfsd3_accessargs),
751 		.pc_ressize = sizeof(struct nfsd3_accessres),
752 		.pc_cachetype = RC_NOCACHE,
753 		.pc_xdrressize = ST+pAT+1,
754 	},
755 	[NFS3PROC_READLINK] = {
756 		.pc_func = nfsd3_proc_readlink,
757 		.pc_decode = nfs3svc_decode_readlinkargs,
758 		.pc_encode = nfs3svc_encode_readlinkres,
759 		.pc_release = nfs3svc_release_fhandle,
760 		.pc_argsize = sizeof(struct nfsd3_readlinkargs),
761 		.pc_ressize = sizeof(struct nfsd3_readlinkres),
762 		.pc_cachetype = RC_NOCACHE,
763 		.pc_xdrressize = ST+pAT+1+NFS3_MAXPATHLEN/4,
764 	},
765 	[NFS3PROC_READ] = {
766 		.pc_func = nfsd3_proc_read,
767 		.pc_decode = nfs3svc_decode_readargs,
768 		.pc_encode = nfs3svc_encode_readres,
769 		.pc_release = nfs3svc_release_fhandle,
770 		.pc_argsize = sizeof(struct nfsd3_readargs),
771 		.pc_ressize = sizeof(struct nfsd3_readres),
772 		.pc_cachetype = RC_NOCACHE,
773 		.pc_xdrressize = ST+pAT+4+NFSSVC_MAXBLKSIZE/4,
774 	},
775 	[NFS3PROC_WRITE] = {
776 		.pc_func = nfsd3_proc_write,
777 		.pc_decode = nfs3svc_decode_writeargs,
778 		.pc_encode = nfs3svc_encode_writeres,
779 		.pc_release = nfs3svc_release_fhandle,
780 		.pc_argsize = sizeof(struct nfsd3_writeargs),
781 		.pc_ressize = sizeof(struct nfsd3_writeres),
782 		.pc_cachetype = RC_REPLBUFF,
783 		.pc_xdrressize = ST+WC+4,
784 	},
785 	[NFS3PROC_CREATE] = {
786 		.pc_func = nfsd3_proc_create,
787 		.pc_decode = nfs3svc_decode_createargs,
788 		.pc_encode = nfs3svc_encode_createres,
789 		.pc_release = nfs3svc_release_fhandle2,
790 		.pc_argsize = sizeof(struct nfsd3_createargs),
791 		.pc_ressize = sizeof(struct nfsd3_createres),
792 		.pc_cachetype = RC_REPLBUFF,
793 		.pc_xdrressize = ST+(1+FH+pAT)+WC,
794 	},
795 	[NFS3PROC_MKDIR] = {
796 		.pc_func = nfsd3_proc_mkdir,
797 		.pc_decode = nfs3svc_decode_mkdirargs,
798 		.pc_encode = nfs3svc_encode_createres,
799 		.pc_release = nfs3svc_release_fhandle2,
800 		.pc_argsize = sizeof(struct nfsd3_mkdirargs),
801 		.pc_ressize = sizeof(struct nfsd3_createres),
802 		.pc_cachetype = RC_REPLBUFF,
803 		.pc_xdrressize = ST+(1+FH+pAT)+WC,
804 	},
805 	[NFS3PROC_SYMLINK] = {
806 		.pc_func = nfsd3_proc_symlink,
807 		.pc_decode = nfs3svc_decode_symlinkargs,
808 		.pc_encode = nfs3svc_encode_createres,
809 		.pc_release = nfs3svc_release_fhandle2,
810 		.pc_argsize = sizeof(struct nfsd3_symlinkargs),
811 		.pc_ressize = sizeof(struct nfsd3_createres),
812 		.pc_cachetype = RC_REPLBUFF,
813 		.pc_xdrressize = ST+(1+FH+pAT)+WC,
814 	},
815 	[NFS3PROC_MKNOD] = {
816 		.pc_func = nfsd3_proc_mknod,
817 		.pc_decode = nfs3svc_decode_mknodargs,
818 		.pc_encode = nfs3svc_encode_createres,
819 		.pc_release = nfs3svc_release_fhandle2,
820 		.pc_argsize = sizeof(struct nfsd3_mknodargs),
821 		.pc_ressize = sizeof(struct nfsd3_createres),
822 		.pc_cachetype = RC_REPLBUFF,
823 		.pc_xdrressize = ST+(1+FH+pAT)+WC,
824 	},
825 	[NFS3PROC_REMOVE] = {
826 		.pc_func = nfsd3_proc_remove,
827 		.pc_decode = nfs3svc_decode_diropargs,
828 		.pc_encode = nfs3svc_encode_wccstatres,
829 		.pc_release = nfs3svc_release_fhandle,
830 		.pc_argsize = sizeof(struct nfsd3_diropargs),
831 		.pc_ressize = sizeof(struct nfsd3_wccstatres),
832 		.pc_cachetype = RC_REPLBUFF,
833 		.pc_xdrressize = ST+WC,
834 	},
835 	[NFS3PROC_RMDIR] = {
836 		.pc_func = nfsd3_proc_rmdir,
837 		.pc_decode = nfs3svc_decode_diropargs,
838 		.pc_encode = nfs3svc_encode_wccstatres,
839 		.pc_release = nfs3svc_release_fhandle,
840 		.pc_argsize = sizeof(struct nfsd3_diropargs),
841 		.pc_ressize = sizeof(struct nfsd3_wccstatres),
842 		.pc_cachetype = RC_REPLBUFF,
843 		.pc_xdrressize = ST+WC,
844 	},
845 	[NFS3PROC_RENAME] = {
846 		.pc_func = nfsd3_proc_rename,
847 		.pc_decode = nfs3svc_decode_renameargs,
848 		.pc_encode = nfs3svc_encode_renameres,
849 		.pc_release = nfs3svc_release_fhandle2,
850 		.pc_argsize = sizeof(struct nfsd3_renameargs),
851 		.pc_ressize = sizeof(struct nfsd3_renameres),
852 		.pc_cachetype = RC_REPLBUFF,
853 		.pc_xdrressize = ST+WC+WC,
854 	},
855 	[NFS3PROC_LINK] = {
856 		.pc_func = nfsd3_proc_link,
857 		.pc_decode = nfs3svc_decode_linkargs,
858 		.pc_encode = nfs3svc_encode_linkres,
859 		.pc_release = nfs3svc_release_fhandle2,
860 		.pc_argsize = sizeof(struct nfsd3_linkargs),
861 		.pc_ressize = sizeof(struct nfsd3_linkres),
862 		.pc_cachetype = RC_REPLBUFF,
863 		.pc_xdrressize = ST+pAT+WC,
864 	},
865 	[NFS3PROC_READDIR] = {
866 		.pc_func = nfsd3_proc_readdir,
867 		.pc_decode = nfs3svc_decode_readdirargs,
868 		.pc_encode = nfs3svc_encode_readdirres,
869 		.pc_release = nfs3svc_release_fhandle,
870 		.pc_argsize = sizeof(struct nfsd3_readdirargs),
871 		.pc_ressize = sizeof(struct nfsd3_readdirres),
872 		.pc_cachetype = RC_NOCACHE,
873 	},
874 	[NFS3PROC_READDIRPLUS] = {
875 		.pc_func = nfsd3_proc_readdirplus,
876 		.pc_decode = nfs3svc_decode_readdirplusargs,
877 		.pc_encode = nfs3svc_encode_readdirres,
878 		.pc_release = nfs3svc_release_fhandle,
879 		.pc_argsize = sizeof(struct nfsd3_readdirplusargs),
880 		.pc_ressize = sizeof(struct nfsd3_readdirres),
881 		.pc_cachetype = RC_NOCACHE,
882 	},
883 	[NFS3PROC_FSSTAT] = {
884 		.pc_func = nfsd3_proc_fsstat,
885 		.pc_decode = nfs3svc_decode_fhandleargs,
886 		.pc_encode = nfs3svc_encode_fsstatres,
887 		.pc_argsize = sizeof(struct nfsd3_fhandleargs),
888 		.pc_ressize = sizeof(struct nfsd3_fsstatres),
889 		.pc_cachetype = RC_NOCACHE,
890 		.pc_xdrressize = ST+pAT+2*6+1,
891 	},
892 	[NFS3PROC_FSINFO] = {
893 		.pc_func = nfsd3_proc_fsinfo,
894 		.pc_decode = nfs3svc_decode_fhandleargs,
895 		.pc_encode = nfs3svc_encode_fsinfores,
896 		.pc_argsize = sizeof(struct nfsd3_fhandleargs),
897 		.pc_ressize = sizeof(struct nfsd3_fsinfores),
898 		.pc_cachetype = RC_NOCACHE,
899 		.pc_xdrressize = ST+pAT+12,
900 	},
901 	[NFS3PROC_PATHCONF] = {
902 		.pc_func = nfsd3_proc_pathconf,
903 		.pc_decode = nfs3svc_decode_fhandleargs,
904 		.pc_encode = nfs3svc_encode_pathconfres,
905 		.pc_argsize = sizeof(struct nfsd3_fhandleargs),
906 		.pc_ressize = sizeof(struct nfsd3_pathconfres),
907 		.pc_cachetype = RC_NOCACHE,
908 		.pc_xdrressize = ST+pAT+6,
909 	},
910 	[NFS3PROC_COMMIT] = {
911 		.pc_func = nfsd3_proc_commit,
912 		.pc_decode = nfs3svc_decode_commitargs,
913 		.pc_encode = nfs3svc_encode_commitres,
914 		.pc_release = nfs3svc_release_fhandle,
915 		.pc_argsize = sizeof(struct nfsd3_commitargs),
916 		.pc_ressize = sizeof(struct nfsd3_commitres),
917 		.pc_cachetype = RC_NOCACHE,
918 		.pc_xdrressize = ST+WC+2,
919 	},
920 };
921 
922 static unsigned int nfsd_count3[ARRAY_SIZE(nfsd_procedures3)];
923 const struct svc_version nfsd_version3 = {
924 	.vs_vers	= 3,
925 	.vs_nproc	= 22,
926 	.vs_proc	= nfsd_procedures3,
927 	.vs_dispatch	= nfsd_dispatch,
928 	.vs_count	= nfsd_count3,
929 	.vs_xdrsize	= NFS3_SVC_XDRSIZE,
930 };
931