Merge branch 'master' of /home/trondmy/kernel/linux-2.6/
[powerpc.git] / fs / nfs / nfs2xdr.c
1 /*
2  * linux/fs/nfs/nfs2xdr.c
3  *
4  * XDR functions to encode/decode NFS RPC arguments and results.
5  *
6  * Copyright (C) 1992, 1993, 1994  Rick Sladkey
7  * Copyright (C) 1996 Olaf Kirch
8  * 04 Aug 1998  Ion Badulescu <ionut@cs.columbia.edu>
9  *              FIFO's need special handling in NFSv2
10  */
11
12 #include <linux/param.h>
13 #include <linux/time.h>
14 #include <linux/mm.h>
15 #include <linux/slab.h>
16 #include <linux/utsname.h>
17 #include <linux/errno.h>
18 #include <linux/string.h>
19 #include <linux/in.h>
20 #include <linux/pagemap.h>
21 #include <linux/proc_fs.h>
22 #include <linux/sunrpc/clnt.h>
23 #include <linux/nfs.h>
24 #include <linux/nfs2.h>
25 #include <linux/nfs_fs.h>
26
27 #define NFSDBG_FACILITY         NFSDBG_XDR
28 /* #define NFS_PARANOIA 1 */
29
30 /* Mapping from NFS error code to "errno" error code. */
31 #define errno_NFSERR_IO         EIO
32
33 /*
34  * Declare the space requirements for NFS arguments and replies as
35  * number of 32bit-words
36  */
37 #define NFS_fhandle_sz          (8)
38 #define NFS_sattr_sz            (8)
39 #define NFS_filename_sz         (1+(NFS2_MAXNAMLEN>>2))
40 #define NFS_path_sz             (1+(NFS2_MAXPATHLEN>>2))
41 #define NFS_fattr_sz            (17)
42 #define NFS_info_sz             (5)
43 #define NFS_entry_sz            (NFS_filename_sz+3)
44
45 #define NFS_diropargs_sz        (NFS_fhandle_sz+NFS_filename_sz)
46 #define NFS_sattrargs_sz        (NFS_fhandle_sz+NFS_sattr_sz)
47 #define NFS_readlinkargs_sz     (NFS_fhandle_sz)
48 #define NFS_readargs_sz         (NFS_fhandle_sz+3)
49 #define NFS_writeargs_sz        (NFS_fhandle_sz+4)
50 #define NFS_createargs_sz       (NFS_diropargs_sz+NFS_sattr_sz)
51 #define NFS_renameargs_sz       (NFS_diropargs_sz+NFS_diropargs_sz)
52 #define NFS_linkargs_sz         (NFS_fhandle_sz+NFS_diropargs_sz)
53 #define NFS_symlinkargs_sz      (NFS_diropargs_sz+NFS_path_sz+NFS_sattr_sz)
54 #define NFS_readdirargs_sz      (NFS_fhandle_sz+2)
55
56 #define NFS_attrstat_sz         (1+NFS_fattr_sz)
57 #define NFS_diropres_sz         (1+NFS_fhandle_sz+NFS_fattr_sz)
58 #define NFS_readlinkres_sz      (2)
59 #define NFS_readres_sz          (1+NFS_fattr_sz+1)
60 #define NFS_writeres_sz         (NFS_attrstat_sz)
61 #define NFS_stat_sz             (1)
62 #define NFS_readdirres_sz       (1)
63 #define NFS_statfsres_sz        (1+NFS_info_sz)
64
65 /*
66  * Common NFS XDR functions as inlines
67  */
68 static inline u32 *
69 xdr_encode_fhandle(u32 *p, struct nfs_fh *fhandle)
70 {
71         memcpy(p, fhandle->data, NFS2_FHSIZE);
72         return p + XDR_QUADLEN(NFS2_FHSIZE);
73 }
74
75 static inline u32 *
76 xdr_decode_fhandle(u32 *p, struct nfs_fh *fhandle)
77 {
78         /* NFSv2 handles have a fixed length */
79         fhandle->size = NFS2_FHSIZE;
80         memcpy(fhandle->data, p, NFS2_FHSIZE);
81         return p + XDR_QUADLEN(NFS2_FHSIZE);
82 }
83
84 static inline u32*
85 xdr_encode_time(u32 *p, struct timespec *timep)
86 {
87         *p++ = htonl(timep->tv_sec);
88         /* Convert nanoseconds into microseconds */
89         *p++ = htonl(timep->tv_nsec ? timep->tv_nsec / 1000 : 0);
90         return p;
91 }
92
93 static inline u32*
94 xdr_encode_current_server_time(u32 *p, struct timespec *timep)
95 {
96         /*
97          * Passing the invalid value useconds=1000000 is a
98          * Sun convention for "set to current server time".
99          * It's needed to make permissions checks for the
100          * "touch" program across v2 mounts to Solaris and
101          * Irix boxes work correctly. See description of
102          * sattr in section 6.1 of "NFS Illustrated" by
103          * Brent Callaghan, Addison-Wesley, ISBN 0-201-32750-5
104          */
105         *p++ = htonl(timep->tv_sec);
106         *p++ = htonl(1000000);
107         return p;
108 }
109
110 static inline u32*
111 xdr_decode_time(u32 *p, struct timespec *timep)
112 {
113         timep->tv_sec = ntohl(*p++);
114         /* Convert microseconds into nanoseconds */
115         timep->tv_nsec = ntohl(*p++) * 1000;
116         return p;
117 }
118
119 static u32 *
120 xdr_decode_fattr(u32 *p, struct nfs_fattr *fattr)
121 {
122         u32 rdev;
123         fattr->type = (enum nfs_ftype) ntohl(*p++);
124         fattr->mode = ntohl(*p++);
125         fattr->nlink = ntohl(*p++);
126         fattr->uid = ntohl(*p++);
127         fattr->gid = ntohl(*p++);
128         fattr->size = ntohl(*p++);
129         fattr->du.nfs2.blocksize = ntohl(*p++);
130         rdev = ntohl(*p++);
131         fattr->du.nfs2.blocks = ntohl(*p++);
132         fattr->fsid.major = ntohl(*p++);
133         fattr->fsid.minor = 0;
134         fattr->fileid = ntohl(*p++);
135         p = xdr_decode_time(p, &fattr->atime);
136         p = xdr_decode_time(p, &fattr->mtime);
137         p = xdr_decode_time(p, &fattr->ctime);
138         fattr->valid |= NFS_ATTR_FATTR;
139         fattr->rdev = new_decode_dev(rdev);
140         if (fattr->type == NFCHR && rdev == NFS2_FIFO_DEV) {
141                 fattr->type = NFFIFO;
142                 fattr->mode = (fattr->mode & ~S_IFMT) | S_IFIFO;
143                 fattr->rdev = 0;
144         }
145         return p;
146 }
147
148 static inline u32 *
149 xdr_encode_sattr(u32 *p, struct iattr *attr)
150 {
151         const u32 not_set = __constant_htonl(0xFFFFFFFF);
152
153         *p++ = (attr->ia_valid & ATTR_MODE) ? htonl(attr->ia_mode) : not_set;
154         *p++ = (attr->ia_valid & ATTR_UID) ? htonl(attr->ia_uid) : not_set;
155         *p++ = (attr->ia_valid & ATTR_GID) ? htonl(attr->ia_gid) : not_set;
156         *p++ = (attr->ia_valid & ATTR_SIZE) ? htonl(attr->ia_size) : not_set;
157
158         if (attr->ia_valid & ATTR_ATIME_SET) {
159                 p = xdr_encode_time(p, &attr->ia_atime);
160         } else if (attr->ia_valid & ATTR_ATIME) {
161                 p = xdr_encode_current_server_time(p, &attr->ia_atime);
162         } else {
163                 *p++ = not_set;
164                 *p++ = not_set;
165         }
166
167         if (attr->ia_valid & ATTR_MTIME_SET) {
168                 p = xdr_encode_time(p, &attr->ia_mtime);
169         } else if (attr->ia_valid & ATTR_MTIME) {
170                 p = xdr_encode_current_server_time(p, &attr->ia_mtime);
171         } else {
172                 *p++ = not_set; 
173                 *p++ = not_set;
174         }
175         return p;
176 }
177
178 /*
179  * NFS encode functions
180  */
181 /*
182  * Encode file handle argument
183  * GETATTR, READLINK, STATFS
184  */
185 static int
186 nfs_xdr_fhandle(struct rpc_rqst *req, u32 *p, struct nfs_fh *fh)
187 {
188         p = xdr_encode_fhandle(p, fh);
189         req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
190         return 0;
191 }
192
193 /*
194  * Encode SETATTR arguments
195  */
196 static int
197 nfs_xdr_sattrargs(struct rpc_rqst *req, u32 *p, struct nfs_sattrargs *args)
198 {
199         p = xdr_encode_fhandle(p, args->fh);
200         p = xdr_encode_sattr(p, args->sattr);
201         req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
202         return 0;
203 }
204
205 /*
206  * Encode directory ops argument
207  * LOOKUP, REMOVE, RMDIR
208  */
209 static int
210 nfs_xdr_diropargs(struct rpc_rqst *req, u32 *p, struct nfs_diropargs *args)
211 {
212         p = xdr_encode_fhandle(p, args->fh);
213         p = xdr_encode_array(p, args->name, args->len);
214         req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
215         return 0;
216 }
217
218 /*
219  * Arguments to a READ call. Since we read data directly into the page
220  * cache, we also set up the reply iovec here so that iov[1] points
221  * exactly to the page we want to fetch.
222  */
223 static int
224 nfs_xdr_readargs(struct rpc_rqst *req, u32 *p, struct nfs_readargs *args)
225 {
226         struct rpc_auth *auth = req->rq_task->tk_auth;
227         unsigned int replen;
228         u32 offset = (u32)args->offset;
229         u32 count = args->count;
230
231         p = xdr_encode_fhandle(p, args->fh);
232         *p++ = htonl(offset);
233         *p++ = htonl(count);
234         *p++ = htonl(count);
235         req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
236
237         /* Inline the page array */
238         replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS_readres_sz) << 2;
239         xdr_inline_pages(&req->rq_rcv_buf, replen,
240                          args->pages, args->pgbase, count);
241         return 0;
242 }
243
244 /*
245  * Decode READ reply
246  */
247 static int
248 nfs_xdr_readres(struct rpc_rqst *req, u32 *p, struct nfs_readres *res)
249 {
250         struct kvec *iov = req->rq_rcv_buf.head;
251         int     status, count, recvd, hdrlen;
252
253         if ((status = ntohl(*p++)))
254                 return -nfs_stat_to_errno(status);
255         p = xdr_decode_fattr(p, res->fattr);
256
257         count = ntohl(*p++);
258         res->eof = 0;
259         hdrlen = (u8 *) p - (u8 *) iov->iov_base;
260         if (iov->iov_len < hdrlen) {
261                 printk(KERN_WARNING "NFS: READ reply header overflowed:"
262                                 "length %d > %Zu\n", hdrlen, iov->iov_len);
263                 return -errno_NFSERR_IO;
264         } else if (iov->iov_len != hdrlen) {
265                 dprintk("NFS: READ header is short. iovec will be shifted.\n");
266                 xdr_shift_buf(&req->rq_rcv_buf, iov->iov_len - hdrlen);
267         }
268
269         recvd = req->rq_rcv_buf.len - hdrlen;
270         if (count > recvd) {
271                 printk(KERN_WARNING "NFS: server cheating in read reply: "
272                         "count %d > recvd %d\n", count, recvd);
273                 count = recvd;
274         }
275
276         dprintk("RPC:      readres OK count %d\n", count);
277         if (count < res->count)
278                 res->count = count;
279
280         return count;
281 }
282
283
284 /*
285  * Write arguments. Splice the buffer to be written into the iovec.
286  */
287 static int
288 nfs_xdr_writeargs(struct rpc_rqst *req, u32 *p, struct nfs_writeargs *args)
289 {
290         struct xdr_buf *sndbuf = &req->rq_snd_buf;
291         u32 offset = (u32)args->offset;
292         u32 count = args->count;
293
294         p = xdr_encode_fhandle(p, args->fh);
295         *p++ = htonl(offset);
296         *p++ = htonl(offset);
297         *p++ = htonl(count);
298         *p++ = htonl(count);
299         sndbuf->len = xdr_adjust_iovec(sndbuf->head, p);
300
301         /* Copy the page array */
302         xdr_encode_pages(sndbuf, args->pages, args->pgbase, count);
303         return 0;
304 }
305
306 /*
307  * Encode create arguments
308  * CREATE, MKDIR
309  */
310 static int
311 nfs_xdr_createargs(struct rpc_rqst *req, u32 *p, struct nfs_createargs *args)
312 {
313         p = xdr_encode_fhandle(p, args->fh);
314         p = xdr_encode_array(p, args->name, args->len);
315         p = xdr_encode_sattr(p, args->sattr);
316         req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
317         return 0;
318 }
319
320 /*
321  * Encode RENAME arguments
322  */
323 static int
324 nfs_xdr_renameargs(struct rpc_rqst *req, u32 *p, struct nfs_renameargs *args)
325 {
326         p = xdr_encode_fhandle(p, args->fromfh);
327         p = xdr_encode_array(p, args->fromname, args->fromlen);
328         p = xdr_encode_fhandle(p, args->tofh);
329         p = xdr_encode_array(p, args->toname, args->tolen);
330         req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
331         return 0;
332 }
333
334 /*
335  * Encode LINK arguments
336  */
337 static int
338 nfs_xdr_linkargs(struct rpc_rqst *req, u32 *p, struct nfs_linkargs *args)
339 {
340         p = xdr_encode_fhandle(p, args->fromfh);
341         p = xdr_encode_fhandle(p, args->tofh);
342         p = xdr_encode_array(p, args->toname, args->tolen);
343         req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
344         return 0;
345 }
346
347 /*
348  * Encode SYMLINK arguments
349  */
350 static int
351 nfs_xdr_symlinkargs(struct rpc_rqst *req, u32 *p, struct nfs_symlinkargs *args)
352 {
353         p = xdr_encode_fhandle(p, args->fromfh);
354         p = xdr_encode_array(p, args->fromname, args->fromlen);
355         p = xdr_encode_array(p, args->topath, args->tolen);
356         p = xdr_encode_sattr(p, args->sattr);
357         req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
358         return 0;
359 }
360
361 /*
362  * Encode arguments to readdir call
363  */
364 static int
365 nfs_xdr_readdirargs(struct rpc_rqst *req, u32 *p, struct nfs_readdirargs *args)
366 {
367         struct rpc_task *task = req->rq_task;
368         struct rpc_auth *auth = task->tk_auth;
369         unsigned int replen;
370         u32 count = args->count;
371
372         p = xdr_encode_fhandle(p, args->fh);
373         *p++ = htonl(args->cookie);
374         *p++ = htonl(count); /* see above */
375         req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
376
377         /* Inline the page array */
378         replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS_readdirres_sz) << 2;
379         xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages, 0, count);
380         return 0;
381 }
382
383 /*
384  * Decode the result of a readdir call.
385  * We're not really decoding anymore, we just leave the buffer untouched
386  * and only check that it is syntactically correct.
387  * The real decoding happens in nfs_decode_entry below, called directly
388  * from nfs_readdir for each entry.
389  */
390 static int
391 nfs_xdr_readdirres(struct rpc_rqst *req, u32 *p, void *dummy)
392 {
393         struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
394         struct kvec *iov = rcvbuf->head;
395         struct page **page;
396         int hdrlen, recvd;
397         int status, nr;
398         unsigned int len, pglen;
399         u32 *end, *entry, *kaddr;
400
401         if ((status = ntohl(*p++)))
402                 return -nfs_stat_to_errno(status);
403
404         hdrlen = (u8 *) p - (u8 *) iov->iov_base;
405         if (iov->iov_len < hdrlen) {
406                 printk(KERN_WARNING "NFS: READDIR reply header overflowed:"
407                                 "length %d > %Zu\n", hdrlen, iov->iov_len);
408                 return -errno_NFSERR_IO;
409         } else if (iov->iov_len != hdrlen) {
410                 dprintk("NFS: READDIR header is short. iovec will be shifted.\n");
411                 xdr_shift_buf(rcvbuf, iov->iov_len - hdrlen);
412         }
413
414         pglen = rcvbuf->page_len;
415         recvd = rcvbuf->len - hdrlen;
416         if (pglen > recvd)
417                 pglen = recvd;
418         page = rcvbuf->pages;
419         kaddr = p = (u32 *)kmap_atomic(*page, KM_USER0);
420         end = (u32 *)((char *)p + pglen);
421         entry = p;
422         for (nr = 0; *p++; nr++) {
423                 if (p + 2 > end)
424                         goto short_pkt;
425                 p++; /* fileid */
426                 len = ntohl(*p++);
427                 p += XDR_QUADLEN(len) + 1;      /* name plus cookie */
428                 if (len > NFS2_MAXNAMLEN) {
429                         printk(KERN_WARNING "NFS: giant filename in readdir (len 0x%x)!\n",
430                                                 len);
431                         goto err_unmap;
432                 }
433                 if (p + 2 > end)
434                         goto short_pkt;
435                 entry = p;
436         }
437         if (!nr && (entry[0] != 0 || entry[1] == 0))
438                 goto short_pkt;
439  out:
440         kunmap_atomic(kaddr, KM_USER0);
441         return nr;
442  short_pkt:
443         entry[0] = entry[1] = 0;
444         /* truncate listing ? */
445         if (!nr) {
446                 printk(KERN_NOTICE "NFS: readdir reply truncated!\n");
447                 entry[1] = 1;
448         }
449         goto out;
450 err_unmap:
451         nr = -errno_NFSERR_IO;
452         goto out;
453 }
454
455 u32 *
456 nfs_decode_dirent(u32 *p, struct nfs_entry *entry, int plus)
457 {
458         if (!*p++) {
459                 if (!*p)
460                         return ERR_PTR(-EAGAIN);
461                 entry->eof = 1;
462                 return ERR_PTR(-EBADCOOKIE);
463         }
464
465         entry->ino        = ntohl(*p++);
466         entry->len        = ntohl(*p++);
467         entry->name       = (const char *) p;
468         p                += XDR_QUADLEN(entry->len);
469         entry->prev_cookie        = entry->cookie;
470         entry->cookie     = ntohl(*p++);
471         entry->eof        = !p[0] && p[1];
472
473         return p;
474 }
475
476 /*
477  * NFS XDR decode functions
478  */
479 /*
480  * Decode simple status reply
481  */
482 static int
483 nfs_xdr_stat(struct rpc_rqst *req, u32 *p, void *dummy)
484 {
485         int     status;
486
487         if ((status = ntohl(*p++)) != 0)
488                 status = -nfs_stat_to_errno(status);
489         return status;
490 }
491
492 /*
493  * Decode attrstat reply
494  * GETATTR, SETATTR, WRITE
495  */
496 static int
497 nfs_xdr_attrstat(struct rpc_rqst *req, u32 *p, struct nfs_fattr *fattr)
498 {
499         int     status;
500
501         if ((status = ntohl(*p++)))
502                 return -nfs_stat_to_errno(status);
503         xdr_decode_fattr(p, fattr);
504         return 0;
505 }
506
507 /*
508  * Decode diropres reply
509  * LOOKUP, CREATE, MKDIR
510  */
511 static int
512 nfs_xdr_diropres(struct rpc_rqst *req, u32 *p, struct nfs_diropok *res)
513 {
514         int     status;
515
516         if ((status = ntohl(*p++)))
517                 return -nfs_stat_to_errno(status);
518         p = xdr_decode_fhandle(p, res->fh);
519         xdr_decode_fattr(p, res->fattr);
520         return 0;
521 }
522
523 /*
524  * Encode READLINK args
525  */
526 static int
527 nfs_xdr_readlinkargs(struct rpc_rqst *req, u32 *p, struct nfs_readlinkargs *args)
528 {
529         struct rpc_auth *auth = req->rq_task->tk_auth;
530         unsigned int replen;
531
532         p = xdr_encode_fhandle(p, args->fh);
533         req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
534
535         /* Inline the page array */
536         replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS_readlinkres_sz) << 2;
537         xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages, args->pgbase, args->pglen);
538         return 0;
539 }
540
541 /*
542  * Decode READLINK reply
543  */
544 static int
545 nfs_xdr_readlinkres(struct rpc_rqst *req, u32 *p, void *dummy)
546 {
547         struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
548         struct kvec *iov = rcvbuf->head;
549         int hdrlen, len, recvd;
550         char    *kaddr;
551         int     status;
552
553         if ((status = ntohl(*p++)))
554                 return -nfs_stat_to_errno(status);
555         /* Convert length of symlink */
556         len = ntohl(*p++);
557         if (len >= rcvbuf->page_len || len <= 0) {
558                 dprintk(KERN_WARNING "nfs: server returned giant symlink!\n");
559                 return -ENAMETOOLONG;
560         }
561         hdrlen = (u8 *) p - (u8 *) iov->iov_base;
562         if (iov->iov_len < hdrlen) {
563                 printk(KERN_WARNING "NFS: READLINK reply header overflowed:"
564                                 "length %d > %Zu\n", hdrlen, iov->iov_len);
565                 return -errno_NFSERR_IO;
566         } else if (iov->iov_len != hdrlen) {
567                 dprintk("NFS: READLINK header is short. iovec will be shifted.\n");
568                 xdr_shift_buf(rcvbuf, iov->iov_len - hdrlen);
569         }
570         recvd = req->rq_rcv_buf.len - hdrlen;
571         if (recvd < len) {
572                 printk(KERN_WARNING "NFS: server cheating in readlink reply: "
573                                 "count %u > recvd %u\n", len, recvd);
574                 return -EIO;
575         }
576
577         /* NULL terminate the string we got */
578         kaddr = (char *)kmap_atomic(rcvbuf->pages[0], KM_USER0);
579         kaddr[len+rcvbuf->page_base] = '\0';
580         kunmap_atomic(kaddr, KM_USER0);
581         return 0;
582 }
583
584 /*
585  * Decode WRITE reply
586  */
587 static int
588 nfs_xdr_writeres(struct rpc_rqst *req, u32 *p, struct nfs_writeres *res)
589 {
590         res->verf->committed = NFS_FILE_SYNC;
591         return nfs_xdr_attrstat(req, p, res->fattr);
592 }
593
594 /*
595  * Decode STATFS reply
596  */
597 static int
598 nfs_xdr_statfsres(struct rpc_rqst *req, u32 *p, struct nfs2_fsstat *res)
599 {
600         int     status;
601
602         if ((status = ntohl(*p++)))
603                 return -nfs_stat_to_errno(status);
604
605         res->tsize  = ntohl(*p++);
606         res->bsize  = ntohl(*p++);
607         res->blocks = ntohl(*p++);
608         res->bfree  = ntohl(*p++);
609         res->bavail = ntohl(*p++);
610         return 0;
611 }
612
613 /*
614  * We need to translate between nfs status return values and
615  * the local errno values which may not be the same.
616  */
617 static struct {
618         int stat;
619         int errno;
620 } nfs_errtbl[] = {
621         { NFS_OK,               0               },
622         { NFSERR_PERM,          EPERM           },
623         { NFSERR_NOENT,         ENOENT          },
624         { NFSERR_IO,            errno_NFSERR_IO },
625         { NFSERR_NXIO,          ENXIO           },
626 /*      { NFSERR_EAGAIN,        EAGAIN          }, */
627         { NFSERR_ACCES,         EACCES          },
628         { NFSERR_EXIST,         EEXIST          },
629         { NFSERR_XDEV,          EXDEV           },
630         { NFSERR_NODEV,         ENODEV          },
631         { NFSERR_NOTDIR,        ENOTDIR         },
632         { NFSERR_ISDIR,         EISDIR          },
633         { NFSERR_INVAL,         EINVAL          },
634         { NFSERR_FBIG,          EFBIG           },
635         { NFSERR_NOSPC,         ENOSPC          },
636         { NFSERR_ROFS,          EROFS           },
637         { NFSERR_MLINK,         EMLINK          },
638         { NFSERR_NAMETOOLONG,   ENAMETOOLONG    },
639         { NFSERR_NOTEMPTY,      ENOTEMPTY       },
640         { NFSERR_DQUOT,         EDQUOT          },
641         { NFSERR_STALE,         ESTALE          },
642         { NFSERR_REMOTE,        EREMOTE         },
643 #ifdef EWFLUSH
644         { NFSERR_WFLUSH,        EWFLUSH         },
645 #endif
646         { NFSERR_BADHANDLE,     EBADHANDLE      },
647         { NFSERR_NOT_SYNC,      ENOTSYNC        },
648         { NFSERR_BAD_COOKIE,    EBADCOOKIE      },
649         { NFSERR_NOTSUPP,       ENOTSUPP        },
650         { NFSERR_TOOSMALL,      ETOOSMALL       },
651         { NFSERR_SERVERFAULT,   ESERVERFAULT    },
652         { NFSERR_BADTYPE,       EBADTYPE        },
653         { NFSERR_JUKEBOX,       EJUKEBOX        },
654         { -1,                   EIO             }
655 };
656
657 /*
658  * Convert an NFS error code to a local one.
659  * This one is used jointly by NFSv2 and NFSv3.
660  */
661 int
662 nfs_stat_to_errno(int stat)
663 {
664         int i;
665
666         for (i = 0; nfs_errtbl[i].stat != -1; i++) {
667                 if (nfs_errtbl[i].stat == stat)
668                         return nfs_errtbl[i].errno;
669         }
670         printk(KERN_ERR "nfs_stat_to_errno: bad nfs status return value: %d\n", stat);
671         return nfs_errtbl[i].errno;
672 }
673
674 #ifndef MAX
675 # define MAX(a, b)      (((a) > (b))? (a) : (b))
676 #endif
677
678 #define PROC(proc, argtype, restype, timer)                             \
679 [NFSPROC_##proc] = {                                                    \
680         .p_proc     =  NFSPROC_##proc,                                  \
681         .p_encode   =  (kxdrproc_t) nfs_xdr_##argtype,                  \
682         .p_decode   =  (kxdrproc_t) nfs_xdr_##restype,                  \
683         .p_bufsiz   =  MAX(NFS_##argtype##_sz,NFS_##restype##_sz) << 2, \
684         .p_timer    =  timer,                                           \
685         .p_statidx  =  NFSPROC_##proc,                                  \
686         .p_name     =  #proc,                                           \
687         }
688 struct rpc_procinfo     nfs_procedures[] = {
689     PROC(GETATTR,       fhandle,        attrstat, 1),
690     PROC(SETATTR,       sattrargs,      attrstat, 0),
691     PROC(LOOKUP,        diropargs,      diropres, 2),
692     PROC(READLINK,      readlinkargs,   readlinkres, 3),
693     PROC(READ,          readargs,       readres, 3),
694     PROC(WRITE,         writeargs,      writeres, 4),
695     PROC(CREATE,        createargs,     diropres, 0),
696     PROC(REMOVE,        diropargs,      stat, 0),
697     PROC(RENAME,        renameargs,     stat, 0),
698     PROC(LINK,          linkargs,       stat, 0),
699     PROC(SYMLINK,       symlinkargs,    stat, 0),
700     PROC(MKDIR,         createargs,     diropres, 0),
701     PROC(RMDIR,         diropargs,      stat, 0),
702     PROC(READDIR,       readdirargs,    readdirres, 3),
703     PROC(STATFS,        fhandle,        statfsres, 0),
704 };
705
706 struct rpc_version              nfs_version2 = {
707         .number                 = 2,
708         .nrprocs                = ARRAY_SIZE(nfs_procedures),
709         .procs                  = nfs_procedures
710 };