GNU Linux-libre 4.14.266-gnu1
[releases.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 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
21 nfsd_proc_null(struct svc_rqst *rqstp)
22 {
23         return nfs_ok;
24 }
25
26 static __be32
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
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
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
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
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
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
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
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
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
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
230         dprintk("nfsd: WRITE    %s %d bytes at %d\n",
231                 SVCFH_fmt(&argp->fh),
232                 argp->len, argp->offset);
233
234         nfserr = nfsd_write(rqstp, fh_copy(&resp->fh, &argp->fh), argp->offset,
235                                 rqstp->rq_vec, argp->vlen, &cnt, NFS_DATA_SYNC);
236         return nfsd_return_attrs(nfserr, resp);
237 }
238
239 /*
240  * CREATE processing is complicated. The keyword here is `overloaded.'
241  * The parent directory is kept locked between the check for existence
242  * and the actual create() call in compliance with VFS protocols.
243  * N.B. After this call _both_ argp->fh and resp->fh need an fh_put
244  */
245 static __be32
246 nfsd_proc_create(struct svc_rqst *rqstp)
247 {
248         struct nfsd_createargs *argp = rqstp->rq_argp;
249         struct nfsd_diropres *resp = rqstp->rq_resp;
250         svc_fh          *dirfhp = &argp->fh;
251         svc_fh          *newfhp = &resp->fh;
252         struct iattr    *attr = &argp->attrs;
253         struct inode    *inode;
254         struct dentry   *dchild;
255         int             type, mode;
256         __be32          nfserr;
257         int             hosterr;
258         dev_t           rdev = 0, wanted = new_decode_dev(attr->ia_size);
259
260         dprintk("nfsd: CREATE   %s %.*s\n",
261                 SVCFH_fmt(dirfhp), argp->len, argp->name);
262
263         /* First verify the parent file handle */
264         nfserr = fh_verify(rqstp, dirfhp, S_IFDIR, NFSD_MAY_EXEC);
265         if (nfserr)
266                 goto done; /* must fh_put dirfhp even on error */
267
268         /* Check for NFSD_MAY_WRITE in nfsd_create if necessary */
269
270         nfserr = nfserr_exist;
271         if (isdotent(argp->name, argp->len))
272                 goto done;
273         hosterr = fh_want_write(dirfhp);
274         if (hosterr) {
275                 nfserr = nfserrno(hosterr);
276                 goto done;
277         }
278
279         fh_lock_nested(dirfhp, I_MUTEX_PARENT);
280         dchild = lookup_one_len(argp->name, dirfhp->fh_dentry, argp->len);
281         if (IS_ERR(dchild)) {
282                 nfserr = nfserrno(PTR_ERR(dchild));
283                 goto out_unlock;
284         }
285         fh_init(newfhp, NFS_FHSIZE);
286         nfserr = fh_compose(newfhp, dirfhp->fh_export, dchild, dirfhp);
287         if (!nfserr && d_really_is_negative(dchild))
288                 nfserr = nfserr_noent;
289         dput(dchild);
290         if (nfserr) {
291                 if (nfserr != nfserr_noent)
292                         goto out_unlock;
293                 /*
294                  * If the new file handle wasn't verified, we can't tell
295                  * whether the file exists or not. Time to bail ...
296                  */
297                 nfserr = nfserr_acces;
298                 if (!newfhp->fh_dentry) {
299                         printk(KERN_WARNING 
300                                 "nfsd_proc_create: file handle not verified\n");
301                         goto out_unlock;
302                 }
303         }
304
305         inode = d_inode(newfhp->fh_dentry);
306
307         /* Unfudge the mode bits */
308         if (attr->ia_valid & ATTR_MODE) {
309                 type = attr->ia_mode & S_IFMT;
310                 mode = attr->ia_mode & ~S_IFMT;
311                 if (!type) {
312                         /* no type, so if target exists, assume same as that,
313                          * else assume a file */
314                         if (inode) {
315                                 type = inode->i_mode & S_IFMT;
316                                 switch(type) {
317                                 case S_IFCHR:
318                                 case S_IFBLK:
319                                         /* reserve rdev for later checking */
320                                         rdev = inode->i_rdev;
321                                         attr->ia_valid |= ATTR_SIZE;
322
323                                         /* FALLTHROUGH */
324                                 case S_IFIFO:
325                                         /* this is probably a permission check..
326                                          * at least IRIX implements perm checking on
327                                          *   echo thing > device-special-file-or-pipe
328                                          * by doing a CREATE with type==0
329                                          */
330                                         nfserr = nfsd_permission(rqstp,
331                                                                  newfhp->fh_export,
332                                                                  newfhp->fh_dentry,
333                                                                  NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS);
334                                         if (nfserr && nfserr != nfserr_rofs)
335                                                 goto out_unlock;
336                                 }
337                         } else
338                                 type = S_IFREG;
339                 }
340         } else if (inode) {
341                 type = inode->i_mode & S_IFMT;
342                 mode = inode->i_mode & ~S_IFMT;
343         } else {
344                 type = S_IFREG;
345                 mode = 0;       /* ??? */
346         }
347
348         attr->ia_valid |= ATTR_MODE;
349         attr->ia_mode = mode;
350
351         /* Special treatment for non-regular files according to the
352          * gospel of sun micro
353          */
354         if (type != S_IFREG) {
355                 if (type != S_IFBLK && type != S_IFCHR) {
356                         rdev = 0;
357                 } else if (type == S_IFCHR && !(attr->ia_valid & ATTR_SIZE)) {
358                         /* If you think you've seen the worst, grok this. */
359                         type = S_IFIFO;
360                 } else {
361                         /* Okay, char or block special */
362                         if (!rdev)
363                                 rdev = wanted;
364                 }
365
366                 /* we've used the SIZE information, so discard it */
367                 attr->ia_valid &= ~ATTR_SIZE;
368
369                 /* Make sure the type and device matches */
370                 nfserr = nfserr_exist;
371                 if (inode && type != (inode->i_mode & S_IFMT))
372                         goto out_unlock;
373         }
374
375         nfserr = 0;
376         if (!inode) {
377                 /* File doesn't exist. Create it and set attrs */
378                 nfserr = nfsd_create_locked(rqstp, dirfhp, argp->name,
379                                         argp->len, attr, type, rdev, newfhp);
380         } else if (type == S_IFREG) {
381                 dprintk("nfsd:   existing %s, valid=%x, size=%ld\n",
382                         argp->name, attr->ia_valid, (long) attr->ia_size);
383                 /* File already exists. We ignore all attributes except
384                  * size, so that creat() behaves exactly like
385                  * open(..., O_CREAT|O_TRUNC|O_WRONLY).
386                  */
387                 attr->ia_valid &= ATTR_SIZE;
388                 if (attr->ia_valid)
389                         nfserr = nfsd_setattr(rqstp, newfhp, attr, 0, (time_t)0);
390         }
391
392 out_unlock:
393         /* We don't really need to unlock, as fh_put does it. */
394         fh_unlock(dirfhp);
395         fh_drop_write(dirfhp);
396 done:
397         fh_put(dirfhp);
398         return nfsd_return_dirop(nfserr, resp);
399 }
400
401 static __be32
402 nfsd_proc_remove(struct svc_rqst *rqstp)
403 {
404         struct nfsd_diropargs *argp = rqstp->rq_argp;
405         __be32  nfserr;
406
407         dprintk("nfsd: REMOVE   %s %.*s\n", SVCFH_fmt(&argp->fh),
408                 argp->len, argp->name);
409
410         /* Unlink. -SIFDIR means file must not be a directory */
411         nfserr = nfsd_unlink(rqstp, &argp->fh, -S_IFDIR, argp->name, argp->len);
412         fh_put(&argp->fh);
413         return nfserr;
414 }
415
416 static __be32
417 nfsd_proc_rename(struct svc_rqst *rqstp)
418 {
419         struct nfsd_renameargs *argp = rqstp->rq_argp;
420         __be32  nfserr;
421
422         dprintk("nfsd: RENAME   %s %.*s -> \n",
423                 SVCFH_fmt(&argp->ffh), argp->flen, argp->fname);
424         dprintk("nfsd:        ->  %s %.*s\n",
425                 SVCFH_fmt(&argp->tfh), argp->tlen, argp->tname);
426
427         nfserr = nfsd_rename(rqstp, &argp->ffh, argp->fname, argp->flen,
428                                     &argp->tfh, argp->tname, argp->tlen);
429         fh_put(&argp->ffh);
430         fh_put(&argp->tfh);
431         return nfserr;
432 }
433
434 static __be32
435 nfsd_proc_link(struct svc_rqst *rqstp)
436 {
437         struct nfsd_linkargs *argp = rqstp->rq_argp;
438         __be32  nfserr;
439
440         dprintk("nfsd: LINK     %s ->\n",
441                 SVCFH_fmt(&argp->ffh));
442         dprintk("nfsd:    %s %.*s\n",
443                 SVCFH_fmt(&argp->tfh),
444                 argp->tlen,
445                 argp->tname);
446
447         nfserr = nfsd_link(rqstp, &argp->tfh, argp->tname, argp->tlen,
448                                   &argp->ffh);
449         fh_put(&argp->ffh);
450         fh_put(&argp->tfh);
451         return nfserr;
452 }
453
454 static __be32
455 nfsd_proc_symlink(struct svc_rqst *rqstp)
456 {
457         struct nfsd_symlinkargs *argp = rqstp->rq_argp;
458         struct svc_fh   newfh;
459         __be32          nfserr;
460
461         dprintk("nfsd: SYMLINK  %s %.*s -> %.*s\n",
462                 SVCFH_fmt(&argp->ffh), argp->flen, argp->fname,
463                 argp->tlen, argp->tname);
464
465         fh_init(&newfh, NFS_FHSIZE);
466         /*
467          * Crazy hack: the request fits in a page, and already-decoded
468          * attributes follow argp->tname, so it's safe to just write a
469          * null to ensure it's null-terminated:
470          */
471         argp->tname[argp->tlen] = '\0';
472         nfserr = nfsd_symlink(rqstp, &argp->ffh, argp->fname, argp->flen,
473                                                  argp->tname, &newfh);
474
475         fh_put(&argp->ffh);
476         fh_put(&newfh);
477         return nfserr;
478 }
479
480 /*
481  * Make directory. This operation is not idempotent.
482  * N.B. After this call resp->fh needs an fh_put
483  */
484 static __be32
485 nfsd_proc_mkdir(struct svc_rqst *rqstp)
486 {
487         struct nfsd_createargs *argp = rqstp->rq_argp;
488         struct nfsd_diropres *resp = rqstp->rq_resp;
489         __be32  nfserr;
490
491         dprintk("nfsd: MKDIR    %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
492
493         if (resp->fh.fh_dentry) {
494                 printk(KERN_WARNING
495                         "nfsd_proc_mkdir: response already verified??\n");
496         }
497
498         argp->attrs.ia_valid &= ~ATTR_SIZE;
499         fh_init(&resp->fh, NFS_FHSIZE);
500         nfserr = nfsd_create(rqstp, &argp->fh, argp->name, argp->len,
501                                     &argp->attrs, S_IFDIR, 0, &resp->fh);
502         fh_put(&argp->fh);
503         return nfsd_return_dirop(nfserr, resp);
504 }
505
506 /*
507  * Remove a directory
508  */
509 static __be32
510 nfsd_proc_rmdir(struct svc_rqst *rqstp)
511 {
512         struct nfsd_diropargs *argp = rqstp->rq_argp;
513         __be32  nfserr;
514
515         dprintk("nfsd: RMDIR    %s %.*s\n", SVCFH_fmt(&argp->fh), argp->len, argp->name);
516
517         nfserr = nfsd_unlink(rqstp, &argp->fh, S_IFDIR, argp->name, argp->len);
518         fh_put(&argp->fh);
519         return nfserr;
520 }
521
522 /*
523  * Read a portion of a directory.
524  */
525 static __be32
526 nfsd_proc_readdir(struct svc_rqst *rqstp)
527 {
528         struct nfsd_readdirargs *argp = rqstp->rq_argp;
529         struct nfsd_readdirres *resp = rqstp->rq_resp;
530         int             count;
531         __be32          nfserr;
532         loff_t          offset;
533
534         dprintk("nfsd: READDIR  %s %d bytes at %d\n",
535                 SVCFH_fmt(&argp->fh),           
536                 argp->count, argp->cookie);
537
538         /* Shrink to the client read size */
539         count = (argp->count >> 2) - 2;
540
541         /* Make sure we've room for the NULL ptr & eof flag */
542         count -= 2;
543         if (count < 0)
544                 count = 0;
545
546         resp->buffer = argp->buffer;
547         resp->offset = NULL;
548         resp->buflen = count;
549         resp->common.err = nfs_ok;
550         /* Read directory and encode entries on the fly */
551         offset = argp->cookie;
552         nfserr = nfsd_readdir(rqstp, &argp->fh, &offset, 
553                               &resp->common, nfssvc_encode_entry);
554
555         resp->count = resp->buffer - argp->buffer;
556         if (resp->offset)
557                 *resp->offset = htonl(offset);
558
559         fh_put(&argp->fh);
560         return nfserr;
561 }
562
563 /*
564  * Get file system info
565  */
566 static __be32
567 nfsd_proc_statfs(struct svc_rqst *rqstp)
568 {
569         struct nfsd_fhandle *argp = rqstp->rq_argp;
570         struct nfsd_statfsres *resp = rqstp->rq_resp;
571         __be32  nfserr;
572
573         dprintk("nfsd: STATFS   %s\n", SVCFH_fmt(&argp->fh));
574
575         nfserr = nfsd_statfs(rqstp, &argp->fh, &resp->stats,
576                         NFSD_MAY_BYPASS_GSS_ON_ROOT);
577         fh_put(&argp->fh);
578         return nfserr;
579 }
580
581 /*
582  * NFSv2 Server procedures.
583  * Only the results of non-idempotent operations are cached.
584  */
585 struct nfsd_void { int dummy; };
586
587 #define ST 1            /* status */
588 #define FH 8            /* filehandle */
589 #define AT 18           /* attributes */
590
591 static const struct svc_procedure nfsd_procedures2[18] = {
592         [NFSPROC_NULL] = {
593                 .pc_func = nfsd_proc_null,
594                 .pc_decode = nfssvc_decode_void,
595                 .pc_encode = nfssvc_encode_void,
596                 .pc_argsize = sizeof(struct nfsd_void),
597                 .pc_ressize = sizeof(struct nfsd_void),
598                 .pc_cachetype = RC_NOCACHE,
599                 .pc_xdrressize = ST,
600         },
601         [NFSPROC_GETATTR] = {
602                 .pc_func = nfsd_proc_getattr,
603                 .pc_decode = nfssvc_decode_fhandle,
604                 .pc_encode = nfssvc_encode_attrstat,
605                 .pc_release = nfssvc_release_fhandle,
606                 .pc_argsize = sizeof(struct nfsd_fhandle),
607                 .pc_ressize = sizeof(struct nfsd_attrstat),
608                 .pc_cachetype = RC_NOCACHE,
609                 .pc_xdrressize = ST+AT,
610         },
611         [NFSPROC_SETATTR] = {
612                 .pc_func = nfsd_proc_setattr,
613                 .pc_decode = nfssvc_decode_sattrargs,
614                 .pc_encode = nfssvc_encode_attrstat,
615                 .pc_release = nfssvc_release_fhandle,
616                 .pc_argsize = sizeof(struct nfsd_sattrargs),
617                 .pc_ressize = sizeof(struct nfsd_attrstat),
618                 .pc_cachetype = RC_REPLBUFF,
619                 .pc_xdrressize = ST+AT,
620         },
621         [NFSPROC_ROOT] = {
622                 .pc_func = nfsd_proc_root,
623                 .pc_decode = nfssvc_decode_void,
624                 .pc_encode = nfssvc_encode_void,
625                 .pc_argsize = sizeof(struct nfsd_void),
626                 .pc_ressize = sizeof(struct nfsd_void),
627                 .pc_cachetype = RC_NOCACHE,
628                 .pc_xdrressize = ST,
629         },
630         [NFSPROC_LOOKUP] = {
631                 .pc_func = nfsd_proc_lookup,
632                 .pc_decode = nfssvc_decode_diropargs,
633                 .pc_encode = nfssvc_encode_diropres,
634                 .pc_release = nfssvc_release_fhandle,
635                 .pc_argsize = sizeof(struct nfsd_diropargs),
636                 .pc_ressize = sizeof(struct nfsd_diropres),
637                 .pc_cachetype = RC_NOCACHE,
638                 .pc_xdrressize = ST+FH+AT,
639         },
640         [NFSPROC_READLINK] = {
641                 .pc_func = nfsd_proc_readlink,
642                 .pc_decode = nfssvc_decode_readlinkargs,
643                 .pc_encode = nfssvc_encode_readlinkres,
644                 .pc_argsize = sizeof(struct nfsd_readlinkargs),
645                 .pc_ressize = sizeof(struct nfsd_readlinkres),
646                 .pc_cachetype = RC_NOCACHE,
647                 .pc_xdrressize = ST+1+NFS_MAXPATHLEN/4,
648         },
649         [NFSPROC_READ] = {
650                 .pc_func = nfsd_proc_read,
651                 .pc_decode = nfssvc_decode_readargs,
652                 .pc_encode = nfssvc_encode_readres,
653                 .pc_release = nfssvc_release_fhandle,
654                 .pc_argsize = sizeof(struct nfsd_readargs),
655                 .pc_ressize = sizeof(struct nfsd_readres),
656                 .pc_cachetype = RC_NOCACHE,
657                 .pc_xdrressize = ST+AT+1+NFSSVC_MAXBLKSIZE_V2/4,
658         },
659         [NFSPROC_WRITECACHE] = {
660                 .pc_func = nfsd_proc_writecache,
661                 .pc_decode = nfssvc_decode_void,
662                 .pc_encode = nfssvc_encode_void,
663                 .pc_argsize = sizeof(struct nfsd_void),
664                 .pc_ressize = sizeof(struct nfsd_void),
665                 .pc_cachetype = RC_NOCACHE,
666                 .pc_xdrressize = ST,
667         },
668         [NFSPROC_WRITE] = {
669                 .pc_func = nfsd_proc_write,
670                 .pc_decode = nfssvc_decode_writeargs,
671                 .pc_encode = nfssvc_encode_attrstat,
672                 .pc_release = nfssvc_release_fhandle,
673                 .pc_argsize = sizeof(struct nfsd_writeargs),
674                 .pc_ressize = sizeof(struct nfsd_attrstat),
675                 .pc_cachetype = RC_REPLBUFF,
676                 .pc_xdrressize = ST+AT,
677         },
678         [NFSPROC_CREATE] = {
679                 .pc_func = nfsd_proc_create,
680                 .pc_decode = nfssvc_decode_createargs,
681                 .pc_encode = nfssvc_encode_diropres,
682                 .pc_release = nfssvc_release_fhandle,
683                 .pc_argsize = sizeof(struct nfsd_createargs),
684                 .pc_ressize = sizeof(struct nfsd_diropres),
685                 .pc_cachetype = RC_REPLBUFF,
686                 .pc_xdrressize = ST+FH+AT,
687         },
688         [NFSPROC_REMOVE] = {
689                 .pc_func = nfsd_proc_remove,
690                 .pc_decode = nfssvc_decode_diropargs,
691                 .pc_encode = nfssvc_encode_void,
692                 .pc_argsize = sizeof(struct nfsd_diropargs),
693                 .pc_ressize = sizeof(struct nfsd_void),
694                 .pc_cachetype = RC_REPLSTAT,
695                 .pc_xdrressize = ST,
696         },
697         [NFSPROC_RENAME] = {
698                 .pc_func = nfsd_proc_rename,
699                 .pc_decode = nfssvc_decode_renameargs,
700                 .pc_encode = nfssvc_encode_void,
701                 .pc_argsize = sizeof(struct nfsd_renameargs),
702                 .pc_ressize = sizeof(struct nfsd_void),
703                 .pc_cachetype = RC_REPLSTAT,
704                 .pc_xdrressize = ST,
705         },
706         [NFSPROC_LINK] = {
707                 .pc_func = nfsd_proc_link,
708                 .pc_decode = nfssvc_decode_linkargs,
709                 .pc_encode = nfssvc_encode_void,
710                 .pc_argsize = sizeof(struct nfsd_linkargs),
711                 .pc_ressize = sizeof(struct nfsd_void),
712                 .pc_cachetype = RC_REPLSTAT,
713                 .pc_xdrressize = ST,
714         },
715         [NFSPROC_SYMLINK] = {
716                 .pc_func = nfsd_proc_symlink,
717                 .pc_decode = nfssvc_decode_symlinkargs,
718                 .pc_encode = nfssvc_encode_void,
719                 .pc_argsize = sizeof(struct nfsd_symlinkargs),
720                 .pc_ressize = sizeof(struct nfsd_void),
721                 .pc_cachetype = RC_REPLSTAT,
722                 .pc_xdrressize = ST,
723         },
724         [NFSPROC_MKDIR] = {
725                 .pc_func = nfsd_proc_mkdir,
726                 .pc_decode = nfssvc_decode_createargs,
727                 .pc_encode = nfssvc_encode_diropres,
728                 .pc_release = nfssvc_release_fhandle,
729                 .pc_argsize = sizeof(struct nfsd_createargs),
730                 .pc_ressize = sizeof(struct nfsd_diropres),
731                 .pc_cachetype = RC_REPLBUFF,
732                 .pc_xdrressize = ST+FH+AT,
733         },
734         [NFSPROC_RMDIR] = {
735                 .pc_func = nfsd_proc_rmdir,
736                 .pc_decode = nfssvc_decode_diropargs,
737                 .pc_encode = nfssvc_encode_void,
738                 .pc_argsize = sizeof(struct nfsd_diropargs),
739                 .pc_ressize = sizeof(struct nfsd_void),
740                 .pc_cachetype = RC_REPLSTAT,
741                 .pc_xdrressize = ST,
742         },
743         [NFSPROC_READDIR] = {
744                 .pc_func = nfsd_proc_readdir,
745                 .pc_decode = nfssvc_decode_readdirargs,
746                 .pc_encode = nfssvc_encode_readdirres,
747                 .pc_argsize = sizeof(struct nfsd_readdirargs),
748                 .pc_ressize = sizeof(struct nfsd_readdirres),
749                 .pc_cachetype = RC_NOCACHE,
750         },
751         [NFSPROC_STATFS] = {
752                 .pc_func = nfsd_proc_statfs,
753                 .pc_decode = nfssvc_decode_fhandle,
754                 .pc_encode = nfssvc_encode_statfsres,
755                 .pc_argsize = sizeof(struct nfsd_fhandle),
756                 .pc_ressize = sizeof(struct nfsd_statfsres),
757                 .pc_cachetype = RC_NOCACHE,
758                 .pc_xdrressize = ST+5,
759         },
760 };
761
762
763 static unsigned int nfsd_count2[ARRAY_SIZE(nfsd_procedures2)];
764 const struct svc_version nfsd_version2 = {
765         .vs_vers        = 2,
766         .vs_nproc       = 18,
767         .vs_proc        = nfsd_procedures2,
768         .vs_count       = nfsd_count2,
769         .vs_dispatch    = nfsd_dispatch,
770         .vs_xdrsize     = NFS2_SVC_XDRSIZE,
771 };
772
773 /*
774  * Map errnos to NFS errnos.
775  */
776 __be32
777 nfserrno (int errno)
778 {
779         static struct {
780                 __be32  nfserr;
781                 int     syserr;
782         } nfs_errtbl[] = {
783                 { nfs_ok, 0 },
784                 { nfserr_perm, -EPERM },
785                 { nfserr_noent, -ENOENT },
786                 { nfserr_io, -EIO },
787                 { nfserr_nxio, -ENXIO },
788                 { nfserr_fbig, -E2BIG },
789                 { nfserr_acces, -EACCES },
790                 { nfserr_exist, -EEXIST },
791                 { nfserr_xdev, -EXDEV },
792                 { nfserr_mlink, -EMLINK },
793                 { nfserr_nodev, -ENODEV },
794                 { nfserr_notdir, -ENOTDIR },
795                 { nfserr_isdir, -EISDIR },
796                 { nfserr_inval, -EINVAL },
797                 { nfserr_fbig, -EFBIG },
798                 { nfserr_nospc, -ENOSPC },
799                 { nfserr_rofs, -EROFS },
800                 { nfserr_mlink, -EMLINK },
801                 { nfserr_nametoolong, -ENAMETOOLONG },
802                 { nfserr_notempty, -ENOTEMPTY },
803 #ifdef EDQUOT
804                 { nfserr_dquot, -EDQUOT },
805 #endif
806                 { nfserr_stale, -ESTALE },
807                 { nfserr_jukebox, -ETIMEDOUT },
808                 { nfserr_jukebox, -ERESTARTSYS },
809                 { nfserr_jukebox, -EAGAIN },
810                 { nfserr_jukebox, -EWOULDBLOCK },
811                 { nfserr_jukebox, -ENOMEM },
812                 { nfserr_io, -ETXTBSY },
813                 { nfserr_notsupp, -EOPNOTSUPP },
814                 { nfserr_toosmall, -ETOOSMALL },
815                 { nfserr_serverfault, -ESERVERFAULT },
816                 { nfserr_serverfault, -ENFILE },
817                 { nfserr_io, -EUCLEAN },
818                 { nfserr_perm, -ENOKEY },
819         };
820         int     i;
821
822         for (i = 0; i < ARRAY_SIZE(nfs_errtbl); i++) {
823                 if (nfs_errtbl[i].syserr == errno)
824                         return nfs_errtbl[i].nfserr;
825         }
826         WARN_ONCE(1, "nfsd: non-standard errno: %d\n", errno);
827         return nfserr_io;
828 }
829