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