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