1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Process version 2 NFS requests.
4  *
5  * Copyright (C) 1995-1997 Olaf Kirch <okir@monad.swb.de>
6  */
7 
8 #include <linux/namei.h>
9 
10 #include "cache.h"
11 #include "xdr.h"
12 #include "vfs.h"
13 
14 typedef struct svc_rqst	svc_rqst;
15 typedef struct svc_buf	svc_buf;
16 
17 #define NFSDDBG_FACILITY		NFSDDBG_PROC
18 
19 
20 static __be32
nfsd_proc_null(struct svc_rqst * rqstp)21 nfsd_proc_null(struct svc_rqst *rqstp)
22 {
23 	return nfs_ok;
24 }
25 
26 static __be32
nfsd_return_attrs(__be32 err,struct nfsd_attrstat * resp)27 nfsd_return_attrs(__be32 err, struct nfsd_attrstat *resp)
28 {
29 	if (err) return err;
30 	return fh_getattr(&resp->fh, &resp->stat);
31 }
32 static __be32
nfsd_return_dirop(__be32 err,struct nfsd_diropres * resp)33 nfsd_return_dirop(__be32 err, struct nfsd_diropres *resp)
34 {
35 	if (err) return err;
36 	return fh_getattr(&resp->fh, &resp->stat);
37 }
38 /*
39  * Get a file's attributes
40  * N.B. After this call resp->fh needs an fh_put
41  */
42 static __be32
nfsd_proc_getattr(struct svc_rqst * rqstp)43 nfsd_proc_getattr(struct svc_rqst *rqstp)
44 {
45 	struct nfsd_fhandle *argp = rqstp->rq_argp;
46 	struct nfsd_attrstat *resp = rqstp->rq_resp;
47 	__be32 nfserr;
48 	dprintk("nfsd: GETATTR  %s\n", SVCFH_fmt(&argp->fh));
49 
50 	fh_copy(&resp->fh, &argp->fh);
51 	nfserr = fh_verify(rqstp, &resp->fh, 0,
52 			NFSD_MAY_NOP | NFSD_MAY_BYPASS_GSS_ON_ROOT);
53 	return nfsd_return_attrs(nfserr, resp);
54 }
55 
56 /*
57  * Set a file's attributes
58  * N.B. After this call resp->fh needs an fh_put
59  */
60 static __be32
nfsd_proc_setattr(struct svc_rqst * rqstp)61 nfsd_proc_setattr(struct svc_rqst *rqstp)
62 {
63 	struct nfsd_sattrargs *argp = rqstp->rq_argp;
64 	struct nfsd_attrstat *resp = rqstp->rq_resp;
65 	struct iattr *iap = &argp->attrs;
66 	struct svc_fh *fhp;
67 	__be32 nfserr;
68 
69 	dprintk("nfsd: SETATTR  %s, valid=%x, size=%ld\n",
70 		SVCFH_fmt(&argp->fh),
71 		argp->attrs.ia_valid, (long) argp->attrs.ia_size);
72 
73 	fhp = fh_copy(&resp->fh, &argp->fh);
74 
75 	/*
76 	 * NFSv2 does not differentiate between "set-[ac]time-to-now"
77 	 * which only requires access, and "set-[ac]time-to-X" which
78 	 * requires ownership.
79 	 * So if it looks like it might be "set both to the same time which
80 	 * is close to now", and if setattr_prepare fails, then we
81 	 * convert to "set to now" instead of "set to explicit time"
82 	 *
83 	 * We only call setattr_prepare as the last test as technically
84 	 * it is not an interface that we should be using.
85 	 */
86 #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
87 #define	MAX_TOUCH_TIME_ERROR (30*60)
88 	if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET &&
89 	    iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec) {
90 		/*
91 		 * Looks probable.
92 		 *
93 		 * Now just make sure time is in the right ballpark.
94 		 * Solaris, at least, doesn't seem to care what the time
95 		 * request is.  We require it be within 30 minutes of now.
96 		 */
97 		time_t delta = iap->ia_atime.tv_sec - get_seconds();
98 
99 		nfserr = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
100 		if (nfserr)
101 			goto done;
102 
103 		if (delta < 0)
104 			delta = -delta;
105 		if (delta < MAX_TOUCH_TIME_ERROR &&
106 		    setattr_prepare(fhp->fh_dentry, iap) != 0) {
107 			/*
108 			 * Turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME.
109 			 * This will cause notify_change to set these times
110 			 * to "now"
111 			 */
112 			iap->ia_valid &= ~BOTH_TIME_SET;
113 		}
114 	}
115 
116 	nfserr = nfsd_setattr(rqstp, fhp, iap, 0, (time_t)0);
117 done:
118 	return nfsd_return_attrs(nfserr, resp);
119 }
120 
121 /* Obsolete, replaced by MNTPROC_MNT. */
122 static __be32
nfsd_proc_root(struct svc_rqst * rqstp)123 nfsd_proc_root(struct svc_rqst *rqstp)
124 {
125 	return nfs_ok;
126 }
127 
128 /*
129  * Look up a path name component
130  * Note: the dentry in the resp->fh may be negative if the file
131  * doesn't exist yet.
132  * N.B. After this call resp->fh needs an fh_put
133  */
134 static __be32
nfsd_proc_lookup(struct svc_rqst * rqstp)135 nfsd_proc_lookup(struct svc_rqst *rqstp)
136 {
137 	struct nfsd_diropargs *argp = rqstp->rq_argp;
138 	struct nfsd_diropres *resp = rqstp->rq_resp;
139 	__be32	nfserr;
140 
141 	dprintk("nfsd: LOOKUP   %s %.*s\n",
142 		SVCFH_fmt(&argp->fh), argp->len, argp->name);
143 
144 	fh_init(&resp->fh, NFS_FHSIZE);
145 	nfserr = nfsd_lookup(rqstp, &argp->fh, argp->name, argp->len,
146 				 &resp->fh);
147 
148 	fh_put(&argp->fh);
149 	return nfsd_return_dirop(nfserr, resp);
150 }
151 
152 /*
153  * Read a symlink.
154  */
155 static __be32
nfsd_proc_readlink(struct svc_rqst * rqstp)156 nfsd_proc_readlink(struct svc_rqst *rqstp)
157 {
158 	struct nfsd_readlinkargs *argp = rqstp->rq_argp;
159 	struct nfsd_readlinkres *resp = rqstp->rq_resp;
160 	__be32	nfserr;
161 
162 	dprintk("nfsd: READLINK %s\n", SVCFH_fmt(&argp->fh));
163 
164 	/* Read the symlink. */
165 	resp->len = NFS_MAXPATHLEN;
166 	nfserr = nfsd_readlink(rqstp, &argp->fh, argp->buffer, &resp->len);
167 
168 	fh_put(&argp->fh);
169 	return nfserr;
170 }
171 
172 /*
173  * Read a portion of a file.
174  * N.B. After this call resp->fh needs an fh_put
175  */
176 static __be32
nfsd_proc_read(struct svc_rqst * rqstp)177 nfsd_proc_read(struct svc_rqst *rqstp)
178 {
179 	struct nfsd_readargs *argp = rqstp->rq_argp;
180 	struct nfsd_readres *resp = rqstp->rq_resp;
181 	__be32	nfserr;
182 
183 	dprintk("nfsd: READ    %s %d bytes at %d\n",
184 		SVCFH_fmt(&argp->fh),
185 		argp->count, argp->offset);
186 
187 	/* Obtain buffer pointer for payload. 19 is 1 word for
188 	 * status, 17 words for fattr, and 1 word for the byte count.
189 	 */
190 
191 	if (NFSSVC_MAXBLKSIZE_V2 < argp->count) {
192 		char buf[RPC_MAX_ADDRBUFLEN];
193 		printk(KERN_NOTICE
194 			"oversized read request from %s (%d bytes)\n",
195 				svc_print_addr(rqstp, buf, sizeof(buf)),
196 				argp->count);
197 		argp->count = NFSSVC_MAXBLKSIZE_V2;
198 	}
199 	svc_reserve_auth(rqstp, (19<<2) + argp->count + 4);
200 
201 	resp->count = argp->count;
202 	nfserr = nfsd_read(rqstp, fh_copy(&resp->fh, &argp->fh),
203 				  argp->offset,
204 			   	  rqstp->rq_vec, argp->vlen,
205 				  &resp->count);
206 
207 	if (nfserr) return nfserr;
208 	return fh_getattr(&resp->fh, &resp->stat);
209 }
210 
211 /* Reserved */
212 static __be32
nfsd_proc_writecache(struct svc_rqst * rqstp)213 nfsd_proc_writecache(struct svc_rqst *rqstp)
214 {
215 	return nfs_ok;
216 }
217 
218 /*
219  * Write data to a file
220  * N.B. After this call resp->fh needs an fh_put
221  */
222 static __be32
nfsd_proc_write(struct svc_rqst * rqstp)223 nfsd_proc_write(struct svc_rqst *rqstp)
224 {
225 	struct nfsd_writeargs *argp = rqstp->rq_argp;
226 	struct nfsd_attrstat *resp = rqstp->rq_resp;
227 	__be32	nfserr;
228 	unsigned long cnt = argp->len;
229 	unsigned int nvecs;
230 
231 	dprintk("nfsd: WRITE    %s %u bytes at %d\n",
232 		SVCFH_fmt(&argp->fh),
233 		argp->len, argp->offset);
234 
235 	nvecs = svc_fill_write_vector(rqstp, rqstp->rq_arg.pages,
236 				      &argp->first, cnt);
237 	if (!nvecs)
238 		return nfserr_io;
239 	nfserr = nfsd_write(rqstp, fh_copy(&resp->fh, &argp->fh),
240 			    argp->offset, rqstp->rq_vec, nvecs,
241 			    &cnt, NFS_DATA_SYNC);
242 	return nfsd_return_attrs(nfserr, resp);
243 }
244 
245 /*
246  * CREATE processing is complicated. The keyword here is `overloaded.'
247  * The parent directory is kept locked between the check for existence
248  * and the actual create() call in compliance with VFS protocols.
249  * N.B. After this call _both_ argp->fh and resp->fh need an fh_put
250  */
251 static __be32
nfsd_proc_create(struct svc_rqst * rqstp)252 nfsd_proc_create(struct svc_rqst *rqstp)
253 {
254 	struct nfsd_createargs *argp = rqstp->rq_argp;
255 	struct nfsd_diropres *resp = rqstp->rq_resp;
256 	svc_fh		*dirfhp = &argp->fh;
257 	svc_fh		*newfhp = &resp->fh;
258 	struct iattr	*attr = &argp->attrs;
259 	struct inode	*inode;
260 	struct dentry	*dchild;
261 	int		type, mode;
262 	__be32		nfserr;
263 	int		hosterr;
264 	dev_t		rdev = 0, wanted = new_decode_dev(attr->ia_size);
265 
266 	dprintk("nfsd: CREATE   %s %.*s\n",
267 		SVCFH_fmt(dirfhp), argp->len, argp->name);
268 
269 	/* First verify the parent file handle */
270 	nfserr = fh_verify(rqstp, dirfhp, S_IFDIR, NFSD_MAY_EXEC);
271 	if (nfserr)
272 		goto done; /* must fh_put dirfhp even on error */
273 
274 	/* Check for NFSD_MAY_WRITE in nfsd_create if necessary */
275 
276 	nfserr = nfserr_exist;
277 	if (isdotent(argp->name, argp->len))
278 		goto done;
279 	hosterr = fh_want_write(dirfhp);
280 	if (hosterr) {
281 		nfserr = nfserrno(hosterr);
282 		goto done;
283 	}
284 
285 	fh_lock_nested(dirfhp, I_MUTEX_PARENT);
286 	dchild = lookup_one_len(argp->name, dirfhp->fh_dentry, argp->len);
287 	if (IS_ERR(dchild)) {
288 		nfserr = nfserrno(PTR_ERR(dchild));
289 		goto out_unlock;
290 	}
291 	fh_init(newfhp, NFS_FHSIZE);
292 	nfserr = fh_compose(newfhp, dirfhp->fh_export, dchild, dirfhp);
293 	if (!nfserr && d_really_is_negative(dchild))
294 		nfserr = nfserr_noent;
295 	dput(dchild);
296 	if (nfserr) {
297 		if (nfserr != nfserr_noent)
298 			goto out_unlock;
299 		/*
300 		 * If the new file handle wasn't verified, we can't tell
301 		 * whether the file exists or not. Time to bail ...
302 		 */
303 		nfserr = nfserr_acces;
304 		if (!newfhp->fh_dentry) {
305 			printk(KERN_WARNING
306 				"nfsd_proc_create: file handle not verified\n");
307 			goto out_unlock;
308 		}
309 	}
310 
311 	inode = d_inode(newfhp->fh_dentry);
312 
313 	/* Unfudge the mode bits */
314 	if (attr->ia_valid & ATTR_MODE) {
315 		type = attr->ia_mode & S_IFMT;
316 		mode = attr->ia_mode & ~S_IFMT;
317 		if (!type) {
318 			/* no type, so if target exists, assume same as that,
319 			 * else assume a file */
320 			if (inode) {
321 				type = inode->i_mode & S_IFMT;
322 				switch(type) {
323 				case S_IFCHR:
324 				case S_IFBLK:
325 					/* reserve rdev for later checking */
326 					rdev = inode->i_rdev;
327 					attr->ia_valid |= ATTR_SIZE;
328 
329 					/* FALLTHROUGH */
330 				case S_IFIFO:
331 					/* this is probably a permission check..
332 					 * at least IRIX implements perm checking on
333 					 *   echo thing > device-special-file-or-pipe
334 					 * by doing a CREATE with type==0
335 					 */
336 					nfserr = nfsd_permission(rqstp,
337 								 newfhp->fh_export,
338 								 newfhp->fh_dentry,
339 								 NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS);
340 					if (nfserr && nfserr != nfserr_rofs)
341 						goto out_unlock;
342 				}
343 			} else
344 				type = S_IFREG;
345 		}
346 	} else if (inode) {
347 		type = inode->i_mode & S_IFMT;
348 		mode = inode->i_mode & ~S_IFMT;
349 	} else {
350 		type = S_IFREG;
351 		mode = 0;	/* ??? */
352 	}
353 
354 	attr->ia_valid |= ATTR_MODE;
355 	attr->ia_mode = mode;
356 
357 	/* Special treatment for non-regular files according to the
358 	 * gospel of sun micro
359 	 */
360 	if (type != S_IFREG) {
361 		if (type != S_IFBLK && type != S_IFCHR) {
362 			rdev = 0;
363 		} else if (type == S_IFCHR && !(attr->ia_valid & ATTR_SIZE)) {
364 			/* If you think you've seen the worst, grok this. */
365 			type = S_IFIFO;
366 		} else {
367 			/* Okay, char or block special */
368 			if (!rdev)
369 				rdev = wanted;
370 		}
371 
372 		/* we've used the SIZE information, so discard it */
373 		attr->ia_valid &= ~ATTR_SIZE;
374 
375 		/* Make sure the type and device matches */
376 		nfserr = nfserr_exist;
377 		if (inode && type != (inode->i_mode & S_IFMT))
378 			goto out_unlock;
379 	}
380 
381 	nfserr = 0;
382 	if (!inode) {
383 		/* File doesn't exist. Create it and set attrs */
384 		nfserr = nfsd_create_locked(rqstp, dirfhp, argp->name,
385 					argp->len, attr, type, rdev, newfhp);
386 	} else if (type == S_IFREG) {
387 		dprintk("nfsd:   existing %s, valid=%x, size=%ld\n",
388 			argp->name, attr->ia_valid, (long) attr->ia_size);
389 		/* File already exists. We ignore all attributes except
390 		 * size, so that creat() behaves exactly like
391 		 * open(..., O_CREAT|O_TRUNC|O_WRONLY).
392 		 */
393 		attr->ia_valid &= ATTR_SIZE;
394 		if (attr->ia_valid)
395 			nfserr = nfsd_setattr(rqstp, newfhp, attr, 0, (time_t)0);
396 	}
397 
398 out_unlock:
399 	/* We don't really need to unlock, as fh_put does it. */
400 	fh_unlock(dirfhp);
401 	fh_drop_write(dirfhp);
402 done:
403 	fh_put(dirfhp);
404 	return nfsd_return_dirop(nfserr, resp);
405 }
406 
407 static __be32
nfsd_proc_remove(struct svc_rqst * rqstp)408 nfsd_proc_remove(struct svc_rqst *rqstp)
409 {
410 	struct nfsd_diropargs *argp = rqstp->rq_argp;
411 	__be32	nfserr;
412 
413 	dprintk("nfsd: REMOVE   %s %.*s\n", SVCFH_fmt(&argp->fh),
414 		argp->len, argp->name);
415 
416 	/* Unlink. -SIFDIR means file must not be a directory */
417 	nfserr = nfsd_unlink(rqstp, &argp->fh, -S_IFDIR, argp->name, argp->len);
418 	fh_put(&argp->fh);
419 	return nfserr;
420 }
421 
422 static __be32
nfsd_proc_rename(struct svc_rqst * rqstp)423 nfsd_proc_rename(struct svc_rqst *rqstp)
424 {
425 	struct nfsd_renameargs *argp = rqstp->rq_argp;
426 	__be32	nfserr;
427 
428 	dprintk("nfsd: RENAME   %s %.*s -> \n",
429 		SVCFH_fmt(&argp->ffh), argp->flen, argp->fname);
430 	dprintk("nfsd:        ->  %s %.*s\n",
431 		SVCFH_fmt(&argp->tfh), argp->tlen, argp->tname);
432 
433 	nfserr = nfsd_rename(rqstp, &argp->ffh, argp->fname, argp->flen,
434 				    &argp->tfh, argp->tname, argp->tlen);
435 	fh_put(&argp->ffh);
436 	fh_put(&argp->tfh);
437 	return nfserr;
438 }
439 
440 static __be32
nfsd_proc_link(struct svc_rqst * rqstp)441 nfsd_proc_link(struct svc_rqst *rqstp)
442 {
443 	struct nfsd_linkargs *argp = rqstp->rq_argp;
444 	__be32	nfserr;
445 
446 	dprintk("nfsd: LINK     %s ->\n",
447 		SVCFH_fmt(&argp->ffh));
448 	dprintk("nfsd:    %s %.*s\n",
449 		SVCFH_fmt(&argp->tfh),
450 		argp->tlen,
451 		argp->tname);
452 
453 	nfserr = nfsd_link(rqstp, &argp->tfh, argp->tname, argp->tlen,
454 				  &argp->ffh);
455 	fh_put(&argp->ffh);
456 	fh_put(&argp->tfh);
457 	return nfserr;
458 }
459 
460 static __be32
nfsd_proc_symlink(struct svc_rqst * rqstp)461 nfsd_proc_symlink(struct svc_rqst *rqstp)
462 {
463 	struct nfsd_symlinkargs *argp = rqstp->rq_argp;
464 	struct svc_fh	newfh;
465 	__be32		nfserr;
466 
467 	if (argp->tlen > NFS_MAXPATHLEN)
468 		return nfserr_nametoolong;
469 
470 	argp->tname = svc_fill_symlink_pathname(rqstp, &argp->first,
471 						page_address(rqstp->rq_arg.pages[0]),
472 						argp->tlen);
473 	if (IS_ERR(argp->tname))
474 		return nfserrno(PTR_ERR(argp->tname));
475 
476 	dprintk("nfsd: SYMLINK  %s %.*s -> %.*s\n",
477 		SVCFH_fmt(&argp->ffh), argp->flen, argp->fname,
478 		argp->tlen, argp->tname);
479 
480 	fh_init(&newfh, NFS_FHSIZE);
481 	nfserr = nfsd_symlink(rqstp, &argp->ffh, argp->fname, argp->flen,
482 						 argp->tname, &newfh);
483 
484 	kfree(argp->tname);
485 	fh_put(&argp->ffh);
486 	fh_put(&newfh);
487 	return nfserr;
488 }
489 
490 /*
491  * Make directory. This operation is not idempotent.
492  * N.B. After this call resp->fh needs an fh_put
493  */
494 static __be32
nfsd_proc_mkdir(struct svc_rqst * rqstp)495 nfsd_proc_mkdir(struct svc_rqst *rqstp)
496 {
497 	struct nfsd_createargs *argp = rqstp->rq_argp;
498 	struct nfsd_diropres *resp = rqstp->rq_resp;
499 	__be32	nfserr;
500 
501 	dprintk("nfsd: MKDIR    %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
502 
503 	if (resp->fh.fh_dentry) {
504 		printk(KERN_WARNING
505 			"nfsd_proc_mkdir: response already verified??\n");
506 	}
507 
508 	argp->attrs.ia_valid &= ~ATTR_SIZE;
509 	fh_init(&resp->fh, NFS_FHSIZE);
510 	nfserr = nfsd_create(rqstp, &argp->fh, argp->name, argp->len,
511 				    &argp->attrs, S_IFDIR, 0, &resp->fh);
512 	fh_put(&argp->fh);
513 	return nfsd_return_dirop(nfserr, resp);
514 }
515 
516 /*
517  * Remove a directory
518  */
519 static __be32
nfsd_proc_rmdir(struct svc_rqst * rqstp)520 nfsd_proc_rmdir(struct svc_rqst *rqstp)
521 {
522 	struct nfsd_diropargs *argp = rqstp->rq_argp;
523 	__be32	nfserr;
524 
525 	dprintk("nfsd: RMDIR    %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
526 
527 	nfserr = nfsd_unlink(rqstp, &argp->fh, S_IFDIR, argp->name, argp->len);
528 	fh_put(&argp->fh);
529 	return nfserr;
530 }
531 
532 /*
533  * Read a portion of a directory.
534  */
535 static __be32
nfsd_proc_readdir(struct svc_rqst * rqstp)536 nfsd_proc_readdir(struct svc_rqst *rqstp)
537 {
538 	struct nfsd_readdirargs *argp = rqstp->rq_argp;
539 	struct nfsd_readdirres *resp = rqstp->rq_resp;
540 	int		count;
541 	__be32		nfserr;
542 	loff_t		offset;
543 
544 	dprintk("nfsd: READDIR  %s %d bytes at %d\n",
545 		SVCFH_fmt(&argp->fh),
546 		argp->count, argp->cookie);
547 
548 	/* Shrink to the client read size */
549 	count = (argp->count >> 2) - 2;
550 
551 	/* Make sure we've room for the NULL ptr & eof flag */
552 	count -= 2;
553 	if (count < 0)
554 		count = 0;
555 
556 	resp->buffer = argp->buffer;
557 	resp->offset = NULL;
558 	resp->buflen = count;
559 	resp->common.err = nfs_ok;
560 	/* Read directory and encode entries on the fly */
561 	offset = argp->cookie;
562 	nfserr = nfsd_readdir(rqstp, &argp->fh, &offset,
563 			      &resp->common, nfssvc_encode_entry);
564 
565 	resp->count = resp->buffer - argp->buffer;
566 	if (resp->offset)
567 		*resp->offset = htonl(offset);
568 
569 	fh_put(&argp->fh);
570 	return nfserr;
571 }
572 
573 /*
574  * Get file system info
575  */
576 static __be32
nfsd_proc_statfs(struct svc_rqst * rqstp)577 nfsd_proc_statfs(struct svc_rqst *rqstp)
578 {
579 	struct nfsd_fhandle *argp = rqstp->rq_argp;
580 	struct nfsd_statfsres *resp = rqstp->rq_resp;
581 	__be32	nfserr;
582 
583 	dprintk("nfsd: STATFS   %s\n", SVCFH_fmt(&argp->fh));
584 
585 	nfserr = nfsd_statfs(rqstp, &argp->fh, &resp->stats,
586 			NFSD_MAY_BYPASS_GSS_ON_ROOT);
587 	fh_put(&argp->fh);
588 	return nfserr;
589 }
590 
591 /*
592  * NFSv2 Server procedures.
593  * Only the results of non-idempotent operations are cached.
594  */
595 struct nfsd_void { int dummy; };
596 
597 #define ST 1		/* status */
598 #define FH 8		/* filehandle */
599 #define	AT 18		/* attributes */
600 
601 static const struct svc_procedure nfsd_procedures2[18] = {
602 	[NFSPROC_NULL] = {
603 		.pc_func = nfsd_proc_null,
604 		.pc_decode = nfssvc_decode_void,
605 		.pc_encode = nfssvc_encode_void,
606 		.pc_argsize = sizeof(struct nfsd_void),
607 		.pc_ressize = sizeof(struct nfsd_void),
608 		.pc_cachetype = RC_NOCACHE,
609 		.pc_xdrressize = ST,
610 	},
611 	[NFSPROC_GETATTR] = {
612 		.pc_func = nfsd_proc_getattr,
613 		.pc_decode = nfssvc_decode_fhandle,
614 		.pc_encode = nfssvc_encode_attrstat,
615 		.pc_release = nfssvc_release_fhandle,
616 		.pc_argsize = sizeof(struct nfsd_fhandle),
617 		.pc_ressize = sizeof(struct nfsd_attrstat),
618 		.pc_cachetype = RC_NOCACHE,
619 		.pc_xdrressize = ST+AT,
620 	},
621 	[NFSPROC_SETATTR] = {
622 		.pc_func = nfsd_proc_setattr,
623 		.pc_decode = nfssvc_decode_sattrargs,
624 		.pc_encode = nfssvc_encode_attrstat,
625 		.pc_release = nfssvc_release_fhandle,
626 		.pc_argsize = sizeof(struct nfsd_sattrargs),
627 		.pc_ressize = sizeof(struct nfsd_attrstat),
628 		.pc_cachetype = RC_REPLBUFF,
629 		.pc_xdrressize = ST+AT,
630 	},
631 	[NFSPROC_ROOT] = {
632 		.pc_func = nfsd_proc_root,
633 		.pc_decode = nfssvc_decode_void,
634 		.pc_encode = nfssvc_encode_void,
635 		.pc_argsize = sizeof(struct nfsd_void),
636 		.pc_ressize = sizeof(struct nfsd_void),
637 		.pc_cachetype = RC_NOCACHE,
638 		.pc_xdrressize = ST,
639 	},
640 	[NFSPROC_LOOKUP] = {
641 		.pc_func = nfsd_proc_lookup,
642 		.pc_decode = nfssvc_decode_diropargs,
643 		.pc_encode = nfssvc_encode_diropres,
644 		.pc_release = nfssvc_release_fhandle,
645 		.pc_argsize = sizeof(struct nfsd_diropargs),
646 		.pc_ressize = sizeof(struct nfsd_diropres),
647 		.pc_cachetype = RC_NOCACHE,
648 		.pc_xdrressize = ST+FH+AT,
649 	},
650 	[NFSPROC_READLINK] = {
651 		.pc_func = nfsd_proc_readlink,
652 		.pc_decode = nfssvc_decode_readlinkargs,
653 		.pc_encode = nfssvc_encode_readlinkres,
654 		.pc_argsize = sizeof(struct nfsd_readlinkargs),
655 		.pc_ressize = sizeof(struct nfsd_readlinkres),
656 		.pc_cachetype = RC_NOCACHE,
657 		.pc_xdrressize = ST+1+NFS_MAXPATHLEN/4,
658 	},
659 	[NFSPROC_READ] = {
660 		.pc_func = nfsd_proc_read,
661 		.pc_decode = nfssvc_decode_readargs,
662 		.pc_encode = nfssvc_encode_readres,
663 		.pc_release = nfssvc_release_fhandle,
664 		.pc_argsize = sizeof(struct nfsd_readargs),
665 		.pc_ressize = sizeof(struct nfsd_readres),
666 		.pc_cachetype = RC_NOCACHE,
667 		.pc_xdrressize = ST+AT+1+NFSSVC_MAXBLKSIZE_V2/4,
668 	},
669 	[NFSPROC_WRITECACHE] = {
670 		.pc_func = nfsd_proc_writecache,
671 		.pc_decode = nfssvc_decode_void,
672 		.pc_encode = nfssvc_encode_void,
673 		.pc_argsize = sizeof(struct nfsd_void),
674 		.pc_ressize = sizeof(struct nfsd_void),
675 		.pc_cachetype = RC_NOCACHE,
676 		.pc_xdrressize = ST,
677 	},
678 	[NFSPROC_WRITE] = {
679 		.pc_func = nfsd_proc_write,
680 		.pc_decode = nfssvc_decode_writeargs,
681 		.pc_encode = nfssvc_encode_attrstat,
682 		.pc_release = nfssvc_release_fhandle,
683 		.pc_argsize = sizeof(struct nfsd_writeargs),
684 		.pc_ressize = sizeof(struct nfsd_attrstat),
685 		.pc_cachetype = RC_REPLBUFF,
686 		.pc_xdrressize = ST+AT,
687 	},
688 	[NFSPROC_CREATE] = {
689 		.pc_func = nfsd_proc_create,
690 		.pc_decode = nfssvc_decode_createargs,
691 		.pc_encode = nfssvc_encode_diropres,
692 		.pc_release = nfssvc_release_fhandle,
693 		.pc_argsize = sizeof(struct nfsd_createargs),
694 		.pc_ressize = sizeof(struct nfsd_diropres),
695 		.pc_cachetype = RC_REPLBUFF,
696 		.pc_xdrressize = ST+FH+AT,
697 	},
698 	[NFSPROC_REMOVE] = {
699 		.pc_func = nfsd_proc_remove,
700 		.pc_decode = nfssvc_decode_diropargs,
701 		.pc_encode = nfssvc_encode_void,
702 		.pc_argsize = sizeof(struct nfsd_diropargs),
703 		.pc_ressize = sizeof(struct nfsd_void),
704 		.pc_cachetype = RC_REPLSTAT,
705 		.pc_xdrressize = ST,
706 	},
707 	[NFSPROC_RENAME] = {
708 		.pc_func = nfsd_proc_rename,
709 		.pc_decode = nfssvc_decode_renameargs,
710 		.pc_encode = nfssvc_encode_void,
711 		.pc_argsize = sizeof(struct nfsd_renameargs),
712 		.pc_ressize = sizeof(struct nfsd_void),
713 		.pc_cachetype = RC_REPLSTAT,
714 		.pc_xdrressize = ST,
715 	},
716 	[NFSPROC_LINK] = {
717 		.pc_func = nfsd_proc_link,
718 		.pc_decode = nfssvc_decode_linkargs,
719 		.pc_encode = nfssvc_encode_void,
720 		.pc_argsize = sizeof(struct nfsd_linkargs),
721 		.pc_ressize = sizeof(struct nfsd_void),
722 		.pc_cachetype = RC_REPLSTAT,
723 		.pc_xdrressize = ST,
724 	},
725 	[NFSPROC_SYMLINK] = {
726 		.pc_func = nfsd_proc_symlink,
727 		.pc_decode = nfssvc_decode_symlinkargs,
728 		.pc_encode = nfssvc_encode_void,
729 		.pc_argsize = sizeof(struct nfsd_symlinkargs),
730 		.pc_ressize = sizeof(struct nfsd_void),
731 		.pc_cachetype = RC_REPLSTAT,
732 		.pc_xdrressize = ST,
733 	},
734 	[NFSPROC_MKDIR] = {
735 		.pc_func = nfsd_proc_mkdir,
736 		.pc_decode = nfssvc_decode_createargs,
737 		.pc_encode = nfssvc_encode_diropres,
738 		.pc_release = nfssvc_release_fhandle,
739 		.pc_argsize = sizeof(struct nfsd_createargs),
740 		.pc_ressize = sizeof(struct nfsd_diropres),
741 		.pc_cachetype = RC_REPLBUFF,
742 		.pc_xdrressize = ST+FH+AT,
743 	},
744 	[NFSPROC_RMDIR] = {
745 		.pc_func = nfsd_proc_rmdir,
746 		.pc_decode = nfssvc_decode_diropargs,
747 		.pc_encode = nfssvc_encode_void,
748 		.pc_argsize = sizeof(struct nfsd_diropargs),
749 		.pc_ressize = sizeof(struct nfsd_void),
750 		.pc_cachetype = RC_REPLSTAT,
751 		.pc_xdrressize = ST,
752 	},
753 	[NFSPROC_READDIR] = {
754 		.pc_func = nfsd_proc_readdir,
755 		.pc_decode = nfssvc_decode_readdirargs,
756 		.pc_encode = nfssvc_encode_readdirres,
757 		.pc_argsize = sizeof(struct nfsd_readdirargs),
758 		.pc_ressize = sizeof(struct nfsd_readdirres),
759 		.pc_cachetype = RC_NOCACHE,
760 	},
761 	[NFSPROC_STATFS] = {
762 		.pc_func = nfsd_proc_statfs,
763 		.pc_decode = nfssvc_decode_fhandle,
764 		.pc_encode = nfssvc_encode_statfsres,
765 		.pc_argsize = sizeof(struct nfsd_fhandle),
766 		.pc_ressize = sizeof(struct nfsd_statfsres),
767 		.pc_cachetype = RC_NOCACHE,
768 		.pc_xdrressize = ST+5,
769 	},
770 };
771 
772 
773 static unsigned int nfsd_count2[ARRAY_SIZE(nfsd_procedures2)];
774 const struct svc_version nfsd_version2 = {
775 	.vs_vers	= 2,
776 	.vs_nproc	= 18,
777 	.vs_proc	= nfsd_procedures2,
778 	.vs_count	= nfsd_count2,
779 	.vs_dispatch	= nfsd_dispatch,
780 	.vs_xdrsize	= NFS2_SVC_XDRSIZE,
781 };
782 
783 /*
784  * Map errnos to NFS errnos.
785  */
786 __be32
nfserrno(int errno)787 nfserrno (int errno)
788 {
789 	static struct {
790 		__be32	nfserr;
791 		int	syserr;
792 	} nfs_errtbl[] = {
793 		{ nfs_ok, 0 },
794 		{ nfserr_perm, -EPERM },
795 		{ nfserr_noent, -ENOENT },
796 		{ nfserr_io, -EIO },
797 		{ nfserr_nxio, -ENXIO },
798 		{ nfserr_fbig, -E2BIG },
799 		{ nfserr_acces, -EACCES },
800 		{ nfserr_exist, -EEXIST },
801 		{ nfserr_xdev, -EXDEV },
802 		{ nfserr_mlink, -EMLINK },
803 		{ nfserr_nodev, -ENODEV },
804 		{ nfserr_notdir, -ENOTDIR },
805 		{ nfserr_isdir, -EISDIR },
806 		{ nfserr_inval, -EINVAL },
807 		{ nfserr_fbig, -EFBIG },
808 		{ nfserr_nospc, -ENOSPC },
809 		{ nfserr_rofs, -EROFS },
810 		{ nfserr_mlink, -EMLINK },
811 		{ nfserr_nametoolong, -ENAMETOOLONG },
812 		{ nfserr_notempty, -ENOTEMPTY },
813 #ifdef EDQUOT
814 		{ nfserr_dquot, -EDQUOT },
815 #endif
816 		{ nfserr_stale, -ESTALE },
817 		{ nfserr_jukebox, -ETIMEDOUT },
818 		{ nfserr_jukebox, -ERESTARTSYS },
819 		{ nfserr_jukebox, -EAGAIN },
820 		{ nfserr_jukebox, -EWOULDBLOCK },
821 		{ nfserr_jukebox, -ENOMEM },
822 		{ nfserr_io, -ETXTBSY },
823 		{ nfserr_notsupp, -EOPNOTSUPP },
824 		{ nfserr_toosmall, -ETOOSMALL },
825 		{ nfserr_serverfault, -ESERVERFAULT },
826 		{ nfserr_serverfault, -ENFILE },
827 		{ nfserr_io, -EUCLEAN },
828 		{ nfserr_perm, -ENOKEY },
829 	};
830 	int	i;
831 
832 	for (i = 0; i < ARRAY_SIZE(nfs_errtbl); i++) {
833 		if (nfs_errtbl[i].syserr == errno)
834 			return nfs_errtbl[i].nfserr;
835 	}
836 	WARN_ONCE(1, "nfsd: non-standard errno: %d\n", errno);
837 	return nfserr_io;
838 }
839 
840