GNU Linux-libre 4.14.290-gnu1
[releases.git] / fs / nfs / nfs3xdr.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * linux/fs/nfs/nfs3xdr.c
4  *
5  * XDR functions to encode/decode NFSv3 RPC arguments and results.
6  *
7  * Copyright (C) 1996, 1997 Olaf Kirch
8  */
9
10 #include <linux/param.h>
11 #include <linux/time.h>
12 #include <linux/mm.h>
13 #include <linux/errno.h>
14 #include <linux/string.h>
15 #include <linux/in.h>
16 #include <linux/pagemap.h>
17 #include <linux/proc_fs.h>
18 #include <linux/kdev_t.h>
19 #include <linux/sunrpc/clnt.h>
20 #include <linux/nfs.h>
21 #include <linux/nfs3.h>
22 #include <linux/nfs_fs.h>
23 #include <linux/nfsacl.h>
24 #include "internal.h"
25
26 #define NFSDBG_FACILITY         NFSDBG_XDR
27
28 /* Mapping from NFS error code to "errno" error code. */
29 #define errno_NFSERR_IO         EIO
30
31 /*
32  * Declare the space requirements for NFS arguments and replies as
33  * number of 32bit-words
34  */
35 #define NFS3_fhandle_sz         (1+16)
36 #define NFS3_fh_sz              (NFS3_fhandle_sz)       /* shorthand */
37 #define NFS3_post_op_fh_sz      (1+NFS3_fh_sz)
38 #define NFS3_sattr_sz           (15)
39 #define NFS3_filename_sz        (1+(NFS3_MAXNAMLEN>>2))
40 #define NFS3_path_sz            (1+(NFS3_MAXPATHLEN>>2))
41 #define NFS3_fattr_sz           (21)
42 #define NFS3_cookieverf_sz      (NFS3_COOKIEVERFSIZE>>2)
43 #define NFS3_wcc_attr_sz        (6)
44 #define NFS3_pre_op_attr_sz     (1+NFS3_wcc_attr_sz)
45 #define NFS3_post_op_attr_sz    (1+NFS3_fattr_sz)
46 #define NFS3_wcc_data_sz        (NFS3_pre_op_attr_sz+NFS3_post_op_attr_sz)
47 #define NFS3_diropargs_sz       (NFS3_fh_sz+NFS3_filename_sz)
48
49 #define NFS3_getattrargs_sz     (NFS3_fh_sz)
50 #define NFS3_setattrargs_sz     (NFS3_fh_sz+NFS3_sattr_sz+3)
51 #define NFS3_lookupargs_sz      (NFS3_fh_sz+NFS3_filename_sz)
52 #define NFS3_accessargs_sz      (NFS3_fh_sz+1)
53 #define NFS3_readlinkargs_sz    (NFS3_fh_sz)
54 #define NFS3_readargs_sz        (NFS3_fh_sz+3)
55 #define NFS3_writeargs_sz       (NFS3_fh_sz+5)
56 #define NFS3_createargs_sz      (NFS3_diropargs_sz+NFS3_sattr_sz)
57 #define NFS3_mkdirargs_sz       (NFS3_diropargs_sz+NFS3_sattr_sz)
58 #define NFS3_symlinkargs_sz     (NFS3_diropargs_sz+1+NFS3_sattr_sz)
59 #define NFS3_mknodargs_sz       (NFS3_diropargs_sz+2+NFS3_sattr_sz)
60 #define NFS3_removeargs_sz      (NFS3_fh_sz+NFS3_filename_sz)
61 #define NFS3_renameargs_sz      (NFS3_diropargs_sz+NFS3_diropargs_sz)
62 #define NFS3_linkargs_sz                (NFS3_fh_sz+NFS3_diropargs_sz)
63 #define NFS3_readdirargs_sz     (NFS3_fh_sz+NFS3_cookieverf_sz+3)
64 #define NFS3_readdirplusargs_sz (NFS3_fh_sz+NFS3_cookieverf_sz+4)
65 #define NFS3_commitargs_sz      (NFS3_fh_sz+3)
66
67 #define NFS3_getattrres_sz      (1+NFS3_fattr_sz)
68 #define NFS3_setattrres_sz      (1+NFS3_wcc_data_sz)
69 #define NFS3_removeres_sz       (NFS3_setattrres_sz)
70 #define NFS3_lookupres_sz       (1+NFS3_fh_sz+(2 * NFS3_post_op_attr_sz))
71 #define NFS3_accessres_sz       (1+NFS3_post_op_attr_sz+1)
72 #define NFS3_readlinkres_sz     (1+NFS3_post_op_attr_sz+1)
73 #define NFS3_readres_sz         (1+NFS3_post_op_attr_sz+3)
74 #define NFS3_writeres_sz        (1+NFS3_wcc_data_sz+4)
75 #define NFS3_createres_sz       (1+NFS3_post_op_fh_sz+NFS3_post_op_attr_sz+NFS3_wcc_data_sz)
76 #define NFS3_renameres_sz       (1+(2 * NFS3_wcc_data_sz))
77 #define NFS3_linkres_sz         (1+NFS3_post_op_attr_sz+NFS3_wcc_data_sz)
78 #define NFS3_readdirres_sz      (1+NFS3_post_op_attr_sz+2)
79 #define NFS3_fsstatres_sz       (1+NFS3_post_op_attr_sz+13)
80 #define NFS3_fsinfores_sz       (1+NFS3_post_op_attr_sz+12)
81 #define NFS3_pathconfres_sz     (1+NFS3_post_op_attr_sz+6)
82 #define NFS3_commitres_sz       (1+NFS3_wcc_data_sz+2)
83
84 #define ACL3_getaclargs_sz      (NFS3_fh_sz+1)
85 #define ACL3_setaclargs_sz      (NFS3_fh_sz+1+ \
86                                 XDR_QUADLEN(NFS_ACL_INLINE_BUFSIZE))
87 #define ACL3_getaclres_sz       (1+NFS3_post_op_attr_sz+1+ \
88                                 XDR_QUADLEN(NFS_ACL_INLINE_BUFSIZE))
89 #define ACL3_setaclres_sz       (1+NFS3_post_op_attr_sz)
90
91 static int nfs3_stat_to_errno(enum nfs_stat);
92
93 /*
94  * Map file type to S_IFMT bits
95  */
96 static const umode_t nfs_type2fmt[] = {
97         [NF3BAD] = 0,
98         [NF3REG] = S_IFREG,
99         [NF3DIR] = S_IFDIR,
100         [NF3BLK] = S_IFBLK,
101         [NF3CHR] = S_IFCHR,
102         [NF3LNK] = S_IFLNK,
103         [NF3SOCK] = S_IFSOCK,
104         [NF3FIFO] = S_IFIFO,
105 };
106
107 /*
108  * While encoding arguments, set up the reply buffer in advance to
109  * receive reply data directly into the page cache.
110  */
111 static void prepare_reply_buffer(struct rpc_rqst *req, struct page **pages,
112                                  unsigned int base, unsigned int len,
113                                  unsigned int bufsize)
114 {
115         struct rpc_auth *auth = req->rq_cred->cr_auth;
116         unsigned int replen;
117
118         replen = RPC_REPHDRSIZE + auth->au_rslack + bufsize;
119         xdr_inline_pages(&req->rq_rcv_buf, replen << 2, pages, base, len);
120 }
121
122 /*
123  * Handle decode buffer overflows out-of-line.
124  */
125 static void print_overflow_msg(const char *func, const struct xdr_stream *xdr)
126 {
127         dprintk("NFS: %s prematurely hit the end of our receive buffer. "
128                 "Remaining buffer length is %tu words.\n",
129                 func, xdr->end - xdr->p);
130 }
131
132
133 /*
134  * Encode/decode NFSv3 basic data types
135  *
136  * Basic NFSv3 data types are defined in section 2.5 of RFC 1813:
137  * "NFS Version 3 Protocol Specification".
138  *
139  * Not all basic data types have their own encoding and decoding
140  * functions.  For run-time efficiency, some data types are encoded
141  * or decoded inline.
142  */
143
144 static void encode_uint32(struct xdr_stream *xdr, u32 value)
145 {
146         __be32 *p = xdr_reserve_space(xdr, 4);
147         *p = cpu_to_be32(value);
148 }
149
150 static int decode_uint32(struct xdr_stream *xdr, u32 *value)
151 {
152         __be32 *p;
153
154         p = xdr_inline_decode(xdr, 4);
155         if (unlikely(p == NULL))
156                 goto out_overflow;
157         *value = be32_to_cpup(p);
158         return 0;
159 out_overflow:
160         print_overflow_msg(__func__, xdr);
161         return -EIO;
162 }
163
164 static int decode_uint64(struct xdr_stream *xdr, u64 *value)
165 {
166         __be32 *p;
167
168         p = xdr_inline_decode(xdr, 8);
169         if (unlikely(p == NULL))
170                 goto out_overflow;
171         xdr_decode_hyper(p, value);
172         return 0;
173 out_overflow:
174         print_overflow_msg(__func__, xdr);
175         return -EIO;
176 }
177
178 /*
179  * fileid3
180  *
181  *      typedef uint64 fileid3;
182  */
183 static __be32 *xdr_decode_fileid3(__be32 *p, u64 *fileid)
184 {
185         return xdr_decode_hyper(p, fileid);
186 }
187
188 static int decode_fileid3(struct xdr_stream *xdr, u64 *fileid)
189 {
190         return decode_uint64(xdr, fileid);
191 }
192
193 /*
194  * filename3
195  *
196  *      typedef string filename3<>;
197  */
198 static void encode_filename3(struct xdr_stream *xdr,
199                              const char *name, u32 length)
200 {
201         __be32 *p;
202
203         WARN_ON_ONCE(length > NFS3_MAXNAMLEN);
204         p = xdr_reserve_space(xdr, 4 + length);
205         xdr_encode_opaque(p, name, length);
206 }
207
208 static int decode_inline_filename3(struct xdr_stream *xdr,
209                                    const char **name, u32 *length)
210 {
211         __be32 *p;
212         u32 count;
213
214         p = xdr_inline_decode(xdr, 4);
215         if (unlikely(p == NULL))
216                 goto out_overflow;
217         count = be32_to_cpup(p);
218         if (count > NFS3_MAXNAMLEN)
219                 goto out_nametoolong;
220         p = xdr_inline_decode(xdr, count);
221         if (unlikely(p == NULL))
222                 goto out_overflow;
223         *name = (const char *)p;
224         *length = count;
225         return 0;
226
227 out_nametoolong:
228         dprintk("NFS: returned filename too long: %u\n", count);
229         return -ENAMETOOLONG;
230 out_overflow:
231         print_overflow_msg(__func__, xdr);
232         return -EIO;
233 }
234
235 /*
236  * nfspath3
237  *
238  *      typedef string nfspath3<>;
239  */
240 static void encode_nfspath3(struct xdr_stream *xdr, struct page **pages,
241                             const u32 length)
242 {
243         encode_uint32(xdr, length);
244         xdr_write_pages(xdr, pages, 0, length);
245 }
246
247 static int decode_nfspath3(struct xdr_stream *xdr)
248 {
249         u32 recvd, count;
250         __be32 *p;
251
252         p = xdr_inline_decode(xdr, 4);
253         if (unlikely(p == NULL))
254                 goto out_overflow;
255         count = be32_to_cpup(p);
256         if (unlikely(count >= xdr->buf->page_len || count > NFS3_MAXPATHLEN))
257                 goto out_nametoolong;
258         recvd = xdr_read_pages(xdr, count);
259         if (unlikely(count > recvd))
260                 goto out_cheating;
261         xdr_terminate_string(xdr->buf, count);
262         return 0;
263
264 out_nametoolong:
265         dprintk("NFS: returned pathname too long: %u\n", count);
266         return -ENAMETOOLONG;
267 out_cheating:
268         dprintk("NFS: server cheating in pathname result: "
269                 "count %u > recvd %u\n", count, recvd);
270         return -EIO;
271 out_overflow:
272         print_overflow_msg(__func__, xdr);
273         return -EIO;
274 }
275
276 /*
277  * cookie3
278  *
279  *      typedef uint64 cookie3
280  */
281 static __be32 *xdr_encode_cookie3(__be32 *p, u64 cookie)
282 {
283         return xdr_encode_hyper(p, cookie);
284 }
285
286 static int decode_cookie3(struct xdr_stream *xdr, u64 *cookie)
287 {
288         return decode_uint64(xdr, cookie);
289 }
290
291 /*
292  * cookieverf3
293  *
294  *      typedef opaque cookieverf3[NFS3_COOKIEVERFSIZE];
295  */
296 static __be32 *xdr_encode_cookieverf3(__be32 *p, const __be32 *verifier)
297 {
298         memcpy(p, verifier, NFS3_COOKIEVERFSIZE);
299         return p + XDR_QUADLEN(NFS3_COOKIEVERFSIZE);
300 }
301
302 static int decode_cookieverf3(struct xdr_stream *xdr, __be32 *verifier)
303 {
304         __be32 *p;
305
306         p = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE);
307         if (unlikely(p == NULL))
308                 goto out_overflow;
309         memcpy(verifier, p, NFS3_COOKIEVERFSIZE);
310         return 0;
311 out_overflow:
312         print_overflow_msg(__func__, xdr);
313         return -EIO;
314 }
315
316 /*
317  * createverf3
318  *
319  *      typedef opaque createverf3[NFS3_CREATEVERFSIZE];
320  */
321 static void encode_createverf3(struct xdr_stream *xdr, const __be32 *verifier)
322 {
323         __be32 *p;
324
325         p = xdr_reserve_space(xdr, NFS3_CREATEVERFSIZE);
326         memcpy(p, verifier, NFS3_CREATEVERFSIZE);
327 }
328
329 static int decode_writeverf3(struct xdr_stream *xdr, struct nfs_write_verifier *verifier)
330 {
331         __be32 *p;
332
333         p = xdr_inline_decode(xdr, NFS3_WRITEVERFSIZE);
334         if (unlikely(p == NULL))
335                 goto out_overflow;
336         memcpy(verifier->data, p, NFS3_WRITEVERFSIZE);
337         return 0;
338 out_overflow:
339         print_overflow_msg(__func__, xdr);
340         return -EIO;
341 }
342
343 /*
344  * size3
345  *
346  *      typedef uint64 size3;
347  */
348 static __be32 *xdr_decode_size3(__be32 *p, u64 *size)
349 {
350         return xdr_decode_hyper(p, size);
351 }
352
353 /*
354  * nfsstat3
355  *
356  *      enum nfsstat3 {
357  *              NFS3_OK = 0,
358  *              ...
359  *      }
360  */
361 #define NFS3_OK         NFS_OK
362
363 static int decode_nfsstat3(struct xdr_stream *xdr, enum nfs_stat *status)
364 {
365         __be32 *p;
366
367         p = xdr_inline_decode(xdr, 4);
368         if (unlikely(p == NULL))
369                 goto out_overflow;
370         *status = be32_to_cpup(p);
371         return 0;
372 out_overflow:
373         print_overflow_msg(__func__, xdr);
374         return -EIO;
375 }
376
377 /*
378  * ftype3
379  *
380  *      enum ftype3 {
381  *              NF3REG  = 1,
382  *              NF3DIR  = 2,
383  *              NF3BLK  = 3,
384  *              NF3CHR  = 4,
385  *              NF3LNK  = 5,
386  *              NF3SOCK = 6,
387  *              NF3FIFO = 7
388  *      };
389  */
390 static void encode_ftype3(struct xdr_stream *xdr, const u32 type)
391 {
392         encode_uint32(xdr, type);
393 }
394
395 static __be32 *xdr_decode_ftype3(__be32 *p, umode_t *mode)
396 {
397         u32 type;
398
399         type = be32_to_cpup(p++);
400         if (type > NF3FIFO)
401                 type = NF3NON;
402         *mode = nfs_type2fmt[type];
403         return p;
404 }
405
406 /*
407  * specdata3
408  *
409  *     struct specdata3 {
410  *             uint32  specdata1;
411  *             uint32  specdata2;
412  *     };
413  */
414 static void encode_specdata3(struct xdr_stream *xdr, const dev_t rdev)
415 {
416         __be32 *p;
417
418         p = xdr_reserve_space(xdr, 8);
419         *p++ = cpu_to_be32(MAJOR(rdev));
420         *p = cpu_to_be32(MINOR(rdev));
421 }
422
423 static __be32 *xdr_decode_specdata3(__be32 *p, dev_t *rdev)
424 {
425         unsigned int major, minor;
426
427         major = be32_to_cpup(p++);
428         minor = be32_to_cpup(p++);
429         *rdev = MKDEV(major, minor);
430         if (MAJOR(*rdev) != major || MINOR(*rdev) != minor)
431                 *rdev = 0;
432         return p;
433 }
434
435 /*
436  * nfs_fh3
437  *
438  *      struct nfs_fh3 {
439  *              opaque       data<NFS3_FHSIZE>;
440  *      };
441  */
442 static void encode_nfs_fh3(struct xdr_stream *xdr, const struct nfs_fh *fh)
443 {
444         __be32 *p;
445
446         WARN_ON_ONCE(fh->size > NFS3_FHSIZE);
447         p = xdr_reserve_space(xdr, 4 + fh->size);
448         xdr_encode_opaque(p, fh->data, fh->size);
449 }
450
451 static int decode_nfs_fh3(struct xdr_stream *xdr, struct nfs_fh *fh)
452 {
453         u32 length;
454         __be32 *p;
455
456         p = xdr_inline_decode(xdr, 4);
457         if (unlikely(p == NULL))
458                 goto out_overflow;
459         length = be32_to_cpup(p++);
460         if (unlikely(length > NFS3_FHSIZE))
461                 goto out_toobig;
462         p = xdr_inline_decode(xdr, length);
463         if (unlikely(p == NULL))
464                 goto out_overflow;
465         fh->size = length;
466         memcpy(fh->data, p, length);
467         return 0;
468 out_toobig:
469         dprintk("NFS: file handle size (%u) too big\n", length);
470         return -E2BIG;
471 out_overflow:
472         print_overflow_msg(__func__, xdr);
473         return -EIO;
474 }
475
476 static void zero_nfs_fh3(struct nfs_fh *fh)
477 {
478         memset(fh, 0, sizeof(*fh));
479 }
480
481 /*
482  * nfstime3
483  *
484  *      struct nfstime3 {
485  *              uint32  seconds;
486  *              uint32  nseconds;
487  *      };
488  */
489 static __be32 *xdr_encode_nfstime3(__be32 *p, const struct timespec *timep)
490 {
491         *p++ = cpu_to_be32(timep->tv_sec);
492         *p++ = cpu_to_be32(timep->tv_nsec);
493         return p;
494 }
495
496 static __be32 *xdr_decode_nfstime3(__be32 *p, struct timespec *timep)
497 {
498         timep->tv_sec = be32_to_cpup(p++);
499         timep->tv_nsec = be32_to_cpup(p++);
500         return p;
501 }
502
503 /*
504  * sattr3
505  *
506  *      enum time_how {
507  *              DONT_CHANGE             = 0,
508  *              SET_TO_SERVER_TIME      = 1,
509  *              SET_TO_CLIENT_TIME      = 2
510  *      };
511  *
512  *      union set_mode3 switch (bool set_it) {
513  *      case TRUE:
514  *              mode3   mode;
515  *      default:
516  *              void;
517  *      };
518  *
519  *      union set_uid3 switch (bool set_it) {
520  *      case TRUE:
521  *              uid3    uid;
522  *      default:
523  *              void;
524  *      };
525  *
526  *      union set_gid3 switch (bool set_it) {
527  *      case TRUE:
528  *              gid3    gid;
529  *      default:
530  *              void;
531  *      };
532  *
533  *      union set_size3 switch (bool set_it) {
534  *      case TRUE:
535  *              size3   size;
536  *      default:
537  *              void;
538  *      };
539  *
540  *      union set_atime switch (time_how set_it) {
541  *      case SET_TO_CLIENT_TIME:
542  *              nfstime3        atime;
543  *      default:
544  *              void;
545  *      };
546  *
547  *      union set_mtime switch (time_how set_it) {
548  *      case SET_TO_CLIENT_TIME:
549  *              nfstime3  mtime;
550  *      default:
551  *              void;
552  *      };
553  *
554  *      struct sattr3 {
555  *              set_mode3       mode;
556  *              set_uid3        uid;
557  *              set_gid3        gid;
558  *              set_size3       size;
559  *              set_atime       atime;
560  *              set_mtime       mtime;
561  *      };
562  */
563 static void encode_sattr3(struct xdr_stream *xdr, const struct iattr *attr)
564 {
565         u32 nbytes;
566         __be32 *p;
567
568         /*
569          * In order to make only a single xdr_reserve_space() call,
570          * pre-compute the total number of bytes to be reserved.
571          * Six boolean values, one for each set_foo field, are always
572          * present in the encoded result, so start there.
573          */
574         nbytes = 6 * 4;
575         if (attr->ia_valid & ATTR_MODE)
576                 nbytes += 4;
577         if (attr->ia_valid & ATTR_UID)
578                 nbytes += 4;
579         if (attr->ia_valid & ATTR_GID)
580                 nbytes += 4;
581         if (attr->ia_valid & ATTR_SIZE)
582                 nbytes += 8;
583         if (attr->ia_valid & ATTR_ATIME_SET)
584                 nbytes += 8;
585         if (attr->ia_valid & ATTR_MTIME_SET)
586                 nbytes += 8;
587         p = xdr_reserve_space(xdr, nbytes);
588
589         if (attr->ia_valid & ATTR_MODE) {
590                 *p++ = xdr_one;
591                 *p++ = cpu_to_be32(attr->ia_mode & S_IALLUGO);
592         } else
593                 *p++ = xdr_zero;
594
595         if (attr->ia_valid & ATTR_UID) {
596                 *p++ = xdr_one;
597                 *p++ = cpu_to_be32(from_kuid(&init_user_ns, attr->ia_uid));
598         } else
599                 *p++ = xdr_zero;
600
601         if (attr->ia_valid & ATTR_GID) {
602                 *p++ = xdr_one;
603                 *p++ = cpu_to_be32(from_kgid(&init_user_ns, attr->ia_gid));
604         } else
605                 *p++ = xdr_zero;
606
607         if (attr->ia_valid & ATTR_SIZE) {
608                 *p++ = xdr_one;
609                 p = xdr_encode_hyper(p, (u64)attr->ia_size);
610         } else
611                 *p++ = xdr_zero;
612
613         if (attr->ia_valid & ATTR_ATIME_SET) {
614                 *p++ = xdr_two;
615                 p = xdr_encode_nfstime3(p, &attr->ia_atime);
616         } else if (attr->ia_valid & ATTR_ATIME) {
617                 *p++ = xdr_one;
618         } else
619                 *p++ = xdr_zero;
620
621         if (attr->ia_valid & ATTR_MTIME_SET) {
622                 *p++ = xdr_two;
623                 xdr_encode_nfstime3(p, &attr->ia_mtime);
624         } else if (attr->ia_valid & ATTR_MTIME) {
625                 *p = xdr_one;
626         } else
627                 *p = xdr_zero;
628 }
629
630 /*
631  * fattr3
632  *
633  *      struct fattr3 {
634  *              ftype3          type;
635  *              mode3           mode;
636  *              uint32          nlink;
637  *              uid3            uid;
638  *              gid3            gid;
639  *              size3           size;
640  *              size3           used;
641  *              specdata3       rdev;
642  *              uint64          fsid;
643  *              fileid3         fileid;
644  *              nfstime3        atime;
645  *              nfstime3        mtime;
646  *              nfstime3        ctime;
647  *      };
648  */
649 static int decode_fattr3(struct xdr_stream *xdr, struct nfs_fattr *fattr)
650 {
651         umode_t fmode;
652         __be32 *p;
653
654         p = xdr_inline_decode(xdr, NFS3_fattr_sz << 2);
655         if (unlikely(p == NULL))
656                 goto out_overflow;
657
658         p = xdr_decode_ftype3(p, &fmode);
659
660         fattr->mode = (be32_to_cpup(p++) & ~S_IFMT) | fmode;
661         fattr->nlink = be32_to_cpup(p++);
662         fattr->uid = make_kuid(&init_user_ns, be32_to_cpup(p++));
663         if (!uid_valid(fattr->uid))
664                 goto out_uid;
665         fattr->gid = make_kgid(&init_user_ns, be32_to_cpup(p++));
666         if (!gid_valid(fattr->gid))
667                 goto out_gid;
668
669         p = xdr_decode_size3(p, &fattr->size);
670         p = xdr_decode_size3(p, &fattr->du.nfs3.used);
671         p = xdr_decode_specdata3(p, &fattr->rdev);
672
673         p = xdr_decode_hyper(p, &fattr->fsid.major);
674         fattr->fsid.minor = 0;
675
676         p = xdr_decode_fileid3(p, &fattr->fileid);
677         p = xdr_decode_nfstime3(p, &fattr->atime);
678         p = xdr_decode_nfstime3(p, &fattr->mtime);
679         xdr_decode_nfstime3(p, &fattr->ctime);
680         fattr->change_attr = nfs_timespec_to_change_attr(&fattr->ctime);
681
682         fattr->valid |= NFS_ATTR_FATTR_V3;
683         return 0;
684 out_uid:
685         dprintk("NFS: returned invalid uid\n");
686         return -EINVAL;
687 out_gid:
688         dprintk("NFS: returned invalid gid\n");
689         return -EINVAL;
690 out_overflow:
691         print_overflow_msg(__func__, xdr);
692         return -EIO;
693 }
694
695 /*
696  * post_op_attr
697  *
698  *      union post_op_attr switch (bool attributes_follow) {
699  *      case TRUE:
700  *              fattr3  attributes;
701  *      case FALSE:
702  *              void;
703  *      };
704  */
705 static int decode_post_op_attr(struct xdr_stream *xdr, struct nfs_fattr *fattr)
706 {
707         __be32 *p;
708
709         p = xdr_inline_decode(xdr, 4);
710         if (unlikely(p == NULL))
711                 goto out_overflow;
712         if (*p != xdr_zero)
713                 return decode_fattr3(xdr, fattr);
714         return 0;
715 out_overflow:
716         print_overflow_msg(__func__, xdr);
717         return -EIO;
718 }
719
720 /*
721  * wcc_attr
722  *      struct wcc_attr {
723  *              size3           size;
724  *              nfstime3        mtime;
725  *              nfstime3        ctime;
726  *      };
727  */
728 static int decode_wcc_attr(struct xdr_stream *xdr, struct nfs_fattr *fattr)
729 {
730         __be32 *p;
731
732         p = xdr_inline_decode(xdr, NFS3_wcc_attr_sz << 2);
733         if (unlikely(p == NULL))
734                 goto out_overflow;
735
736         fattr->valid |= NFS_ATTR_FATTR_PRESIZE
737                 | NFS_ATTR_FATTR_PRECHANGE
738                 | NFS_ATTR_FATTR_PREMTIME
739                 | NFS_ATTR_FATTR_PRECTIME;
740
741         p = xdr_decode_size3(p, &fattr->pre_size);
742         p = xdr_decode_nfstime3(p, &fattr->pre_mtime);
743         xdr_decode_nfstime3(p, &fattr->pre_ctime);
744         fattr->pre_change_attr = nfs_timespec_to_change_attr(&fattr->pre_ctime);
745
746         return 0;
747 out_overflow:
748         print_overflow_msg(__func__, xdr);
749         return -EIO;
750 }
751
752 /*
753  * pre_op_attr
754  *      union pre_op_attr switch (bool attributes_follow) {
755  *      case TRUE:
756  *              wcc_attr        attributes;
757  *      case FALSE:
758  *              void;
759  *      };
760  *
761  * wcc_data
762  *
763  *      struct wcc_data {
764  *              pre_op_attr     before;
765  *              post_op_attr    after;
766  *      };
767  */
768 static int decode_pre_op_attr(struct xdr_stream *xdr, struct nfs_fattr *fattr)
769 {
770         __be32 *p;
771
772         p = xdr_inline_decode(xdr, 4);
773         if (unlikely(p == NULL))
774                 goto out_overflow;
775         if (*p != xdr_zero)
776                 return decode_wcc_attr(xdr, fattr);
777         return 0;
778 out_overflow:
779         print_overflow_msg(__func__, xdr);
780         return -EIO;
781 }
782
783 static int decode_wcc_data(struct xdr_stream *xdr, struct nfs_fattr *fattr)
784 {
785         int error;
786
787         error = decode_pre_op_attr(xdr, fattr);
788         if (unlikely(error))
789                 goto out;
790         error = decode_post_op_attr(xdr, fattr);
791 out:
792         return error;
793 }
794
795 /*
796  * post_op_fh3
797  *
798  *      union post_op_fh3 switch (bool handle_follows) {
799  *      case TRUE:
800  *              nfs_fh3  handle;
801  *      case FALSE:
802  *              void;
803  *      };
804  */
805 static int decode_post_op_fh3(struct xdr_stream *xdr, struct nfs_fh *fh)
806 {
807         __be32 *p = xdr_inline_decode(xdr, 4);
808         if (unlikely(p == NULL))
809                 goto out_overflow;
810         if (*p != xdr_zero)
811                 return decode_nfs_fh3(xdr, fh);
812         zero_nfs_fh3(fh);
813         return 0;
814 out_overflow:
815         print_overflow_msg(__func__, xdr);
816         return -EIO;
817 }
818
819 /*
820  * diropargs3
821  *
822  *      struct diropargs3 {
823  *              nfs_fh3         dir;
824  *              filename3       name;
825  *      };
826  */
827 static void encode_diropargs3(struct xdr_stream *xdr, const struct nfs_fh *fh,
828                               const char *name, u32 length)
829 {
830         encode_nfs_fh3(xdr, fh);
831         encode_filename3(xdr, name, length);
832 }
833
834
835 /*
836  * NFSv3 XDR encode functions
837  *
838  * NFSv3 argument types are defined in section 3.3 of RFC 1813:
839  * "NFS Version 3 Protocol Specification".
840  */
841
842 /*
843  * 3.3.1  GETATTR3args
844  *
845  *      struct GETATTR3args {
846  *              nfs_fh3  object;
847  *      };
848  */
849 static void nfs3_xdr_enc_getattr3args(struct rpc_rqst *req,
850                                       struct xdr_stream *xdr,
851                                       const void *data)
852 {
853         const struct nfs_fh *fh = data;
854
855         encode_nfs_fh3(xdr, fh);
856 }
857
858 /*
859  * 3.3.2  SETATTR3args
860  *
861  *      union sattrguard3 switch (bool check) {
862  *      case TRUE:
863  *              nfstime3  obj_ctime;
864  *      case FALSE:
865  *              void;
866  *      };
867  *
868  *      struct SETATTR3args {
869  *              nfs_fh3         object;
870  *              sattr3          new_attributes;
871  *              sattrguard3     guard;
872  *      };
873  */
874 static void encode_sattrguard3(struct xdr_stream *xdr,
875                                const struct nfs3_sattrargs *args)
876 {
877         __be32 *p;
878
879         if (args->guard) {
880                 p = xdr_reserve_space(xdr, 4 + 8);
881                 *p++ = xdr_one;
882                 xdr_encode_nfstime3(p, &args->guardtime);
883         } else {
884                 p = xdr_reserve_space(xdr, 4);
885                 *p = xdr_zero;
886         }
887 }
888
889 static void nfs3_xdr_enc_setattr3args(struct rpc_rqst *req,
890                                       struct xdr_stream *xdr,
891                                       const void *data)
892 {
893         const struct nfs3_sattrargs *args = data;
894         encode_nfs_fh3(xdr, args->fh);
895         encode_sattr3(xdr, args->sattr);
896         encode_sattrguard3(xdr, args);
897 }
898
899 /*
900  * 3.3.3  LOOKUP3args
901  *
902  *      struct LOOKUP3args {
903  *              diropargs3  what;
904  *      };
905  */
906 static void nfs3_xdr_enc_lookup3args(struct rpc_rqst *req,
907                                      struct xdr_stream *xdr,
908                                      const void *data)
909 {
910         const struct nfs3_diropargs *args = data;
911
912         encode_diropargs3(xdr, args->fh, args->name, args->len);
913 }
914
915 /*
916  * 3.3.4  ACCESS3args
917  *
918  *      struct ACCESS3args {
919  *              nfs_fh3         object;
920  *              uint32          access;
921  *      };
922  */
923 static void encode_access3args(struct xdr_stream *xdr,
924                                const struct nfs3_accessargs *args)
925 {
926         encode_nfs_fh3(xdr, args->fh);
927         encode_uint32(xdr, args->access);
928 }
929
930 static void nfs3_xdr_enc_access3args(struct rpc_rqst *req,
931                                      struct xdr_stream *xdr,
932                                      const void *data)
933 {
934         const struct nfs3_accessargs *args = data;
935
936         encode_access3args(xdr, args);
937 }
938
939 /*
940  * 3.3.5  READLINK3args
941  *
942  *      struct READLINK3args {
943  *              nfs_fh3 symlink;
944  *      };
945  */
946 static void nfs3_xdr_enc_readlink3args(struct rpc_rqst *req,
947                                        struct xdr_stream *xdr,
948                                        const void *data)
949 {
950         const struct nfs3_readlinkargs *args = data;
951
952         encode_nfs_fh3(xdr, args->fh);
953         prepare_reply_buffer(req, args->pages, args->pgbase,
954                                         args->pglen, NFS3_readlinkres_sz);
955 }
956
957 /*
958  * 3.3.6  READ3args
959  *
960  *      struct READ3args {
961  *              nfs_fh3         file;
962  *              offset3         offset;
963  *              count3          count;
964  *      };
965  */
966 static void encode_read3args(struct xdr_stream *xdr,
967                              const struct nfs_pgio_args *args)
968 {
969         __be32 *p;
970
971         encode_nfs_fh3(xdr, args->fh);
972
973         p = xdr_reserve_space(xdr, 8 + 4);
974         p = xdr_encode_hyper(p, args->offset);
975         *p = cpu_to_be32(args->count);
976 }
977
978 static void nfs3_xdr_enc_read3args(struct rpc_rqst *req,
979                                    struct xdr_stream *xdr,
980                                    const void *data)
981 {
982         const struct nfs_pgio_args *args = data;
983
984         encode_read3args(xdr, args);
985         prepare_reply_buffer(req, args->pages, args->pgbase,
986                                         args->count, NFS3_readres_sz);
987         req->rq_rcv_buf.flags |= XDRBUF_READ;
988 }
989
990 /*
991  * 3.3.7  WRITE3args
992  *
993  *      enum stable_how {
994  *              UNSTABLE  = 0,
995  *              DATA_SYNC = 1,
996  *              FILE_SYNC = 2
997  *      };
998  *
999  *      struct WRITE3args {
1000  *              nfs_fh3         file;
1001  *              offset3         offset;
1002  *              count3          count;
1003  *              stable_how      stable;
1004  *              opaque          data<>;
1005  *      };
1006  */
1007 static void encode_write3args(struct xdr_stream *xdr,
1008                               const struct nfs_pgio_args *args)
1009 {
1010         __be32 *p;
1011
1012         encode_nfs_fh3(xdr, args->fh);
1013
1014         p = xdr_reserve_space(xdr, 8 + 4 + 4 + 4);
1015         p = xdr_encode_hyper(p, args->offset);
1016         *p++ = cpu_to_be32(args->count);
1017         *p++ = cpu_to_be32(args->stable);
1018         *p = cpu_to_be32(args->count);
1019         xdr_write_pages(xdr, args->pages, args->pgbase, args->count);
1020 }
1021
1022 static void nfs3_xdr_enc_write3args(struct rpc_rqst *req,
1023                                     struct xdr_stream *xdr,
1024                                     const void *data)
1025 {
1026         const struct nfs_pgio_args *args = data;
1027
1028         encode_write3args(xdr, args);
1029         xdr->buf->flags |= XDRBUF_WRITE;
1030 }
1031
1032 /*
1033  * 3.3.8  CREATE3args
1034  *
1035  *      enum createmode3 {
1036  *              UNCHECKED = 0,
1037  *              GUARDED   = 1,
1038  *              EXCLUSIVE = 2
1039  *      };
1040  *
1041  *      union createhow3 switch (createmode3 mode) {
1042  *      case UNCHECKED:
1043  *      case GUARDED:
1044  *              sattr3       obj_attributes;
1045  *      case EXCLUSIVE:
1046  *              createverf3  verf;
1047  *      };
1048  *
1049  *      struct CREATE3args {
1050  *              diropargs3      where;
1051  *              createhow3      how;
1052  *      };
1053  */
1054 static void encode_createhow3(struct xdr_stream *xdr,
1055                               const struct nfs3_createargs *args)
1056 {
1057         encode_uint32(xdr, args->createmode);
1058         switch (args->createmode) {
1059         case NFS3_CREATE_UNCHECKED:
1060         case NFS3_CREATE_GUARDED:
1061                 encode_sattr3(xdr, args->sattr);
1062                 break;
1063         case NFS3_CREATE_EXCLUSIVE:
1064                 encode_createverf3(xdr, args->verifier);
1065                 break;
1066         default:
1067                 BUG();
1068         }
1069 }
1070
1071 static void nfs3_xdr_enc_create3args(struct rpc_rqst *req,
1072                                      struct xdr_stream *xdr,
1073                                      const void *data)
1074 {
1075         const struct nfs3_createargs *args = data;
1076
1077         encode_diropargs3(xdr, args->fh, args->name, args->len);
1078         encode_createhow3(xdr, args);
1079 }
1080
1081 /*
1082  * 3.3.9  MKDIR3args
1083  *
1084  *      struct MKDIR3args {
1085  *              diropargs3      where;
1086  *              sattr3          attributes;
1087  *      };
1088  */
1089 static void nfs3_xdr_enc_mkdir3args(struct rpc_rqst *req,
1090                                     struct xdr_stream *xdr,
1091                                     const void *data)
1092 {
1093         const struct nfs3_mkdirargs *args = data;
1094
1095         encode_diropargs3(xdr, args->fh, args->name, args->len);
1096         encode_sattr3(xdr, args->sattr);
1097 }
1098
1099 /*
1100  * 3.3.10  SYMLINK3args
1101  *
1102  *      struct symlinkdata3 {
1103  *              sattr3          symlink_attributes;
1104  *              nfspath3        symlink_data;
1105  *      };
1106  *
1107  *      struct SYMLINK3args {
1108  *              diropargs3      where;
1109  *              symlinkdata3    symlink;
1110  *      };
1111  */
1112 static void encode_symlinkdata3(struct xdr_stream *xdr,
1113                                 const void *data)
1114 {
1115         const struct nfs3_symlinkargs *args = data;
1116
1117         encode_sattr3(xdr, args->sattr);
1118         encode_nfspath3(xdr, args->pages, args->pathlen);
1119 }
1120
1121 static void nfs3_xdr_enc_symlink3args(struct rpc_rqst *req,
1122                                       struct xdr_stream *xdr,
1123                                       const void *data)
1124 {
1125         const struct nfs3_symlinkargs *args = data;
1126
1127         encode_diropargs3(xdr, args->fromfh, args->fromname, args->fromlen);
1128         encode_symlinkdata3(xdr, args);
1129         xdr->buf->flags |= XDRBUF_WRITE;
1130 }
1131
1132 /*
1133  * 3.3.11  MKNOD3args
1134  *
1135  *      struct devicedata3 {
1136  *              sattr3          dev_attributes;
1137  *              specdata3       spec;
1138  *      };
1139  *
1140  *      union mknoddata3 switch (ftype3 type) {
1141  *      case NF3CHR:
1142  *      case NF3BLK:
1143  *              devicedata3     device;
1144  *      case NF3SOCK:
1145  *      case NF3FIFO:
1146  *              sattr3          pipe_attributes;
1147  *      default:
1148  *              void;
1149  *      };
1150  *
1151  *      struct MKNOD3args {
1152  *              diropargs3      where;
1153  *              mknoddata3      what;
1154  *      };
1155  */
1156 static void encode_devicedata3(struct xdr_stream *xdr,
1157                                const struct nfs3_mknodargs *args)
1158 {
1159         encode_sattr3(xdr, args->sattr);
1160         encode_specdata3(xdr, args->rdev);
1161 }
1162
1163 static void encode_mknoddata3(struct xdr_stream *xdr,
1164                               const struct nfs3_mknodargs *args)
1165 {
1166         encode_ftype3(xdr, args->type);
1167         switch (args->type) {
1168         case NF3CHR:
1169         case NF3BLK:
1170                 encode_devicedata3(xdr, args);
1171                 break;
1172         case NF3SOCK:
1173         case NF3FIFO:
1174                 encode_sattr3(xdr, args->sattr);
1175                 break;
1176         case NF3REG:
1177         case NF3DIR:
1178                 break;
1179         default:
1180                 BUG();
1181         }
1182 }
1183
1184 static void nfs3_xdr_enc_mknod3args(struct rpc_rqst *req,
1185                                     struct xdr_stream *xdr,
1186                                     const void *data)
1187 {
1188         const struct nfs3_mknodargs *args = data;
1189
1190         encode_diropargs3(xdr, args->fh, args->name, args->len);
1191         encode_mknoddata3(xdr, args);
1192 }
1193
1194 /*
1195  * 3.3.12  REMOVE3args
1196  *
1197  *      struct REMOVE3args {
1198  *              diropargs3  object;
1199  *      };
1200  */
1201 static void nfs3_xdr_enc_remove3args(struct rpc_rqst *req,
1202                                      struct xdr_stream *xdr,
1203                                      const void *data)
1204 {
1205         const struct nfs_removeargs *args = data;
1206
1207         encode_diropargs3(xdr, args->fh, args->name.name, args->name.len);
1208 }
1209
1210 /*
1211  * 3.3.14  RENAME3args
1212  *
1213  *      struct RENAME3args {
1214  *              diropargs3      from;
1215  *              diropargs3      to;
1216  *      };
1217  */
1218 static void nfs3_xdr_enc_rename3args(struct rpc_rqst *req,
1219                                      struct xdr_stream *xdr,
1220                                      const void *data)
1221 {
1222         const struct nfs_renameargs *args = data;
1223         const struct qstr *old = args->old_name;
1224         const struct qstr *new = args->new_name;
1225
1226         encode_diropargs3(xdr, args->old_dir, old->name, old->len);
1227         encode_diropargs3(xdr, args->new_dir, new->name, new->len);
1228 }
1229
1230 /*
1231  * 3.3.15  LINK3args
1232  *
1233  *      struct LINK3args {
1234  *              nfs_fh3         file;
1235  *              diropargs3      link;
1236  *      };
1237  */
1238 static void nfs3_xdr_enc_link3args(struct rpc_rqst *req,
1239                                    struct xdr_stream *xdr,
1240                                    const void *data)
1241 {
1242         const struct nfs3_linkargs *args = data;
1243
1244         encode_nfs_fh3(xdr, args->fromfh);
1245         encode_diropargs3(xdr, args->tofh, args->toname, args->tolen);
1246 }
1247
1248 /*
1249  * 3.3.16  READDIR3args
1250  *
1251  *      struct READDIR3args {
1252  *              nfs_fh3         dir;
1253  *              cookie3         cookie;
1254  *              cookieverf3     cookieverf;
1255  *              count3          count;
1256  *      };
1257  */
1258 static void encode_readdir3args(struct xdr_stream *xdr,
1259                                 const struct nfs3_readdirargs *args)
1260 {
1261         __be32 *p;
1262
1263         encode_nfs_fh3(xdr, args->fh);
1264
1265         p = xdr_reserve_space(xdr, 8 + NFS3_COOKIEVERFSIZE + 4);
1266         p = xdr_encode_cookie3(p, args->cookie);
1267         p = xdr_encode_cookieverf3(p, args->verf);
1268         *p = cpu_to_be32(args->count);
1269 }
1270
1271 static void nfs3_xdr_enc_readdir3args(struct rpc_rqst *req,
1272                                       struct xdr_stream *xdr,
1273                                       const void *data)
1274 {
1275         const struct nfs3_readdirargs *args = data;
1276
1277         encode_readdir3args(xdr, args);
1278         prepare_reply_buffer(req, args->pages, 0,
1279                                 args->count, NFS3_readdirres_sz);
1280 }
1281
1282 /*
1283  * 3.3.17  READDIRPLUS3args
1284  *
1285  *      struct READDIRPLUS3args {
1286  *              nfs_fh3         dir;
1287  *              cookie3         cookie;
1288  *              cookieverf3     cookieverf;
1289  *              count3          dircount;
1290  *              count3          maxcount;
1291  *      };
1292  */
1293 static void encode_readdirplus3args(struct xdr_stream *xdr,
1294                                     const struct nfs3_readdirargs *args)
1295 {
1296         __be32 *p;
1297
1298         encode_nfs_fh3(xdr, args->fh);
1299
1300         p = xdr_reserve_space(xdr, 8 + NFS3_COOKIEVERFSIZE + 4 + 4);
1301         p = xdr_encode_cookie3(p, args->cookie);
1302         p = xdr_encode_cookieverf3(p, args->verf);
1303
1304         /*
1305          * readdirplus: need dircount + buffer size.
1306          * We just make sure we make dircount big enough
1307          */
1308         *p++ = cpu_to_be32(args->count >> 3);
1309
1310         *p = cpu_to_be32(args->count);
1311 }
1312
1313 static void nfs3_xdr_enc_readdirplus3args(struct rpc_rqst *req,
1314                                           struct xdr_stream *xdr,
1315                                           const void *data)
1316 {
1317         const struct nfs3_readdirargs *args = data;
1318
1319         encode_readdirplus3args(xdr, args);
1320         prepare_reply_buffer(req, args->pages, 0,
1321                                 args->count, NFS3_readdirres_sz);
1322 }
1323
1324 /*
1325  * 3.3.21  COMMIT3args
1326  *
1327  *      struct COMMIT3args {
1328  *              nfs_fh3         file;
1329  *              offset3         offset;
1330  *              count3          count;
1331  *      };
1332  */
1333 static void encode_commit3args(struct xdr_stream *xdr,
1334                                const struct nfs_commitargs *args)
1335 {
1336         __be32 *p;
1337
1338         encode_nfs_fh3(xdr, args->fh);
1339
1340         p = xdr_reserve_space(xdr, 8 + 4);
1341         p = xdr_encode_hyper(p, args->offset);
1342         *p = cpu_to_be32(args->count);
1343 }
1344
1345 static void nfs3_xdr_enc_commit3args(struct rpc_rqst *req,
1346                                      struct xdr_stream *xdr,
1347                                      const void *data)
1348 {
1349         const struct nfs_commitargs *args = data;
1350
1351         encode_commit3args(xdr, args);
1352 }
1353
1354 #ifdef CONFIG_NFS_V3_ACL
1355
1356 static void nfs3_xdr_enc_getacl3args(struct rpc_rqst *req,
1357                                      struct xdr_stream *xdr,
1358                                      const void *data)
1359 {
1360         const struct nfs3_getaclargs *args = data;
1361
1362         encode_nfs_fh3(xdr, args->fh);
1363         encode_uint32(xdr, args->mask);
1364         if (args->mask & (NFS_ACL | NFS_DFACL))
1365                 prepare_reply_buffer(req, args->pages, 0,
1366                                         NFSACL_MAXPAGES << PAGE_SHIFT,
1367                                         ACL3_getaclres_sz);
1368 }
1369
1370 static void nfs3_xdr_enc_setacl3args(struct rpc_rqst *req,
1371                                      struct xdr_stream *xdr,
1372                                      const void *data)
1373 {
1374         const struct nfs3_setaclargs *args = data;
1375         unsigned int base;
1376         int error;
1377
1378         encode_nfs_fh3(xdr, NFS_FH(args->inode));
1379         encode_uint32(xdr, args->mask);
1380
1381         base = req->rq_slen;
1382         if (args->npages != 0)
1383                 xdr_write_pages(xdr, args->pages, 0, args->len);
1384         else
1385                 xdr_reserve_space(xdr, args->len);
1386
1387         error = nfsacl_encode(xdr->buf, base, args->inode,
1388                             (args->mask & NFS_ACL) ?
1389                             args->acl_access : NULL, 1, 0);
1390         /* FIXME: this is just broken */
1391         BUG_ON(error < 0);
1392         error = nfsacl_encode(xdr->buf, base + error, args->inode,
1393                             (args->mask & NFS_DFACL) ?
1394                             args->acl_default : NULL, 1,
1395                             NFS_ACL_DEFAULT);
1396         BUG_ON(error < 0);
1397 }
1398
1399 #endif  /* CONFIG_NFS_V3_ACL */
1400
1401 /*
1402  * NFSv3 XDR decode functions
1403  *
1404  * NFSv3 result types are defined in section 3.3 of RFC 1813:
1405  * "NFS Version 3 Protocol Specification".
1406  */
1407
1408 /*
1409  * 3.3.1  GETATTR3res
1410  *
1411  *      struct GETATTR3resok {
1412  *              fattr3          obj_attributes;
1413  *      };
1414  *
1415  *      union GETATTR3res switch (nfsstat3 status) {
1416  *      case NFS3_OK:
1417  *              GETATTR3resok  resok;
1418  *      default:
1419  *              void;
1420  *      };
1421  */
1422 static int nfs3_xdr_dec_getattr3res(struct rpc_rqst *req,
1423                                     struct xdr_stream *xdr,
1424                                     void *result)
1425 {
1426         enum nfs_stat status;
1427         int error;
1428
1429         error = decode_nfsstat3(xdr, &status);
1430         if (unlikely(error))
1431                 goto out;
1432         if (status != NFS3_OK)
1433                 goto out_default;
1434         error = decode_fattr3(xdr, result);
1435 out:
1436         return error;
1437 out_default:
1438         return nfs3_stat_to_errno(status);
1439 }
1440
1441 /*
1442  * 3.3.2  SETATTR3res
1443  *
1444  *      struct SETATTR3resok {
1445  *              wcc_data  obj_wcc;
1446  *      };
1447  *
1448  *      struct SETATTR3resfail {
1449  *              wcc_data  obj_wcc;
1450  *      };
1451  *
1452  *      union SETATTR3res switch (nfsstat3 status) {
1453  *      case NFS3_OK:
1454  *              SETATTR3resok   resok;
1455  *      default:
1456  *              SETATTR3resfail resfail;
1457  *      };
1458  */
1459 static int nfs3_xdr_dec_setattr3res(struct rpc_rqst *req,
1460                                     struct xdr_stream *xdr,
1461                                     void *result)
1462 {
1463         enum nfs_stat status;
1464         int error;
1465
1466         error = decode_nfsstat3(xdr, &status);
1467         if (unlikely(error))
1468                 goto out;
1469         error = decode_wcc_data(xdr, result);
1470         if (unlikely(error))
1471                 goto out;
1472         if (status != NFS3_OK)
1473                 goto out_status;
1474 out:
1475         return error;
1476 out_status:
1477         return nfs3_stat_to_errno(status);
1478 }
1479
1480 /*
1481  * 3.3.3  LOOKUP3res
1482  *
1483  *      struct LOOKUP3resok {
1484  *              nfs_fh3         object;
1485  *              post_op_attr    obj_attributes;
1486  *              post_op_attr    dir_attributes;
1487  *      };
1488  *
1489  *      struct LOOKUP3resfail {
1490  *              post_op_attr    dir_attributes;
1491  *      };
1492  *
1493  *      union LOOKUP3res switch (nfsstat3 status) {
1494  *      case NFS3_OK:
1495  *              LOOKUP3resok    resok;
1496  *      default:
1497  *              LOOKUP3resfail  resfail;
1498  *      };
1499  */
1500 static int nfs3_xdr_dec_lookup3res(struct rpc_rqst *req,
1501                                    struct xdr_stream *xdr,
1502                                    void *data)
1503 {
1504         struct nfs3_diropres *result = data;
1505         enum nfs_stat status;
1506         int error;
1507
1508         error = decode_nfsstat3(xdr, &status);
1509         if (unlikely(error))
1510                 goto out;
1511         if (status != NFS3_OK)
1512                 goto out_default;
1513         error = decode_nfs_fh3(xdr, result->fh);
1514         if (unlikely(error))
1515                 goto out;
1516         error = decode_post_op_attr(xdr, result->fattr);
1517         if (unlikely(error))
1518                 goto out;
1519         error = decode_post_op_attr(xdr, result->dir_attr);
1520 out:
1521         return error;
1522 out_default:
1523         error = decode_post_op_attr(xdr, result->dir_attr);
1524         if (unlikely(error))
1525                 goto out;
1526         return nfs3_stat_to_errno(status);
1527 }
1528
1529 /*
1530  * 3.3.4  ACCESS3res
1531  *
1532  *      struct ACCESS3resok {
1533  *              post_op_attr    obj_attributes;
1534  *              uint32          access;
1535  *      };
1536  *
1537  *      struct ACCESS3resfail {
1538  *              post_op_attr    obj_attributes;
1539  *      };
1540  *
1541  *      union ACCESS3res switch (nfsstat3 status) {
1542  *      case NFS3_OK:
1543  *              ACCESS3resok    resok;
1544  *      default:
1545  *              ACCESS3resfail  resfail;
1546  *      };
1547  */
1548 static int nfs3_xdr_dec_access3res(struct rpc_rqst *req,
1549                                    struct xdr_stream *xdr,
1550                                    void *data)
1551 {
1552         struct nfs3_accessres *result = data;
1553         enum nfs_stat status;
1554         int error;
1555
1556         error = decode_nfsstat3(xdr, &status);
1557         if (unlikely(error))
1558                 goto out;
1559         error = decode_post_op_attr(xdr, result->fattr);
1560         if (unlikely(error))
1561                 goto out;
1562         if (status != NFS3_OK)
1563                 goto out_default;
1564         error = decode_uint32(xdr, &result->access);
1565 out:
1566         return error;
1567 out_default:
1568         return nfs3_stat_to_errno(status);
1569 }
1570
1571 /*
1572  * 3.3.5  READLINK3res
1573  *
1574  *      struct READLINK3resok {
1575  *              post_op_attr    symlink_attributes;
1576  *              nfspath3        data;
1577  *      };
1578  *
1579  *      struct READLINK3resfail {
1580  *              post_op_attr    symlink_attributes;
1581  *      };
1582  *
1583  *      union READLINK3res switch (nfsstat3 status) {
1584  *      case NFS3_OK:
1585  *              READLINK3resok  resok;
1586  *      default:
1587  *              READLINK3resfail resfail;
1588  *      };
1589  */
1590 static int nfs3_xdr_dec_readlink3res(struct rpc_rqst *req,
1591                                      struct xdr_stream *xdr,
1592                                      void *result)
1593 {
1594         enum nfs_stat status;
1595         int error;
1596
1597         error = decode_nfsstat3(xdr, &status);
1598         if (unlikely(error))
1599                 goto out;
1600         error = decode_post_op_attr(xdr, result);
1601         if (unlikely(error))
1602                 goto out;
1603         if (status != NFS3_OK)
1604                 goto out_default;
1605         error = decode_nfspath3(xdr);
1606 out:
1607         return error;
1608 out_default:
1609         return nfs3_stat_to_errno(status);
1610 }
1611
1612 /*
1613  * 3.3.6  READ3res
1614  *
1615  *      struct READ3resok {
1616  *              post_op_attr    file_attributes;
1617  *              count3          count;
1618  *              bool            eof;
1619  *              opaque          data<>;
1620  *      };
1621  *
1622  *      struct READ3resfail {
1623  *              post_op_attr    file_attributes;
1624  *      };
1625  *
1626  *      union READ3res switch (nfsstat3 status) {
1627  *      case NFS3_OK:
1628  *              READ3resok      resok;
1629  *      default:
1630  *              READ3resfail    resfail;
1631  *      };
1632  */
1633 static int decode_read3resok(struct xdr_stream *xdr,
1634                              struct nfs_pgio_res *result)
1635 {
1636         u32 eof, count, ocount, recvd;
1637         __be32 *p;
1638
1639         p = xdr_inline_decode(xdr, 4 + 4 + 4);
1640         if (unlikely(p == NULL))
1641                 goto out_overflow;
1642         count = be32_to_cpup(p++);
1643         eof = be32_to_cpup(p++);
1644         ocount = be32_to_cpup(p++);
1645         if (unlikely(ocount != count))
1646                 goto out_mismatch;
1647         recvd = xdr_read_pages(xdr, count);
1648         if (unlikely(count > recvd))
1649                 goto out_cheating;
1650 out:
1651         result->eof = eof;
1652         result->count = count;
1653         return count;
1654 out_mismatch:
1655         dprintk("NFS: READ count doesn't match length of opaque: "
1656                 "count %u != ocount %u\n", count, ocount);
1657         return -EIO;
1658 out_cheating:
1659         dprintk("NFS: server cheating in read result: "
1660                 "count %u > recvd %u\n", count, recvd);
1661         count = recvd;
1662         eof = 0;
1663         goto out;
1664 out_overflow:
1665         print_overflow_msg(__func__, xdr);
1666         return -EIO;
1667 }
1668
1669 static int nfs3_xdr_dec_read3res(struct rpc_rqst *req, struct xdr_stream *xdr,
1670                                  void *data)
1671 {
1672         struct nfs_pgio_res *result = data;
1673         enum nfs_stat status;
1674         int error;
1675
1676         error = decode_nfsstat3(xdr, &status);
1677         if (unlikely(error))
1678                 goto out;
1679         error = decode_post_op_attr(xdr, result->fattr);
1680         if (unlikely(error))
1681                 goto out;
1682         result->op_status = status;
1683         if (status != NFS3_OK)
1684                 goto out_status;
1685         error = decode_read3resok(xdr, result);
1686 out:
1687         return error;
1688 out_status:
1689         return nfs3_stat_to_errno(status);
1690 }
1691
1692 /*
1693  * 3.3.7  WRITE3res
1694  *
1695  *      enum stable_how {
1696  *              UNSTABLE  = 0,
1697  *              DATA_SYNC = 1,
1698  *              FILE_SYNC = 2
1699  *      };
1700  *
1701  *      struct WRITE3resok {
1702  *              wcc_data        file_wcc;
1703  *              count3          count;
1704  *              stable_how      committed;
1705  *              writeverf3      verf;
1706  *      };
1707  *
1708  *      struct WRITE3resfail {
1709  *              wcc_data        file_wcc;
1710  *      };
1711  *
1712  *      union WRITE3res switch (nfsstat3 status) {
1713  *      case NFS3_OK:
1714  *              WRITE3resok     resok;
1715  *      default:
1716  *              WRITE3resfail   resfail;
1717  *      };
1718  */
1719 static int decode_write3resok(struct xdr_stream *xdr,
1720                               struct nfs_pgio_res *result)
1721 {
1722         __be32 *p;
1723
1724         p = xdr_inline_decode(xdr, 4 + 4);
1725         if (unlikely(p == NULL))
1726                 goto out_overflow;
1727         result->count = be32_to_cpup(p++);
1728         result->verf->committed = be32_to_cpup(p++);
1729         if (unlikely(result->verf->committed > NFS_FILE_SYNC))
1730                 goto out_badvalue;
1731         if (decode_writeverf3(xdr, &result->verf->verifier))
1732                 goto out_eio;
1733         return result->count;
1734 out_badvalue:
1735         dprintk("NFS: bad stable_how value: %u\n", result->verf->committed);
1736         return -EIO;
1737 out_overflow:
1738         print_overflow_msg(__func__, xdr);
1739 out_eio:
1740         return -EIO;
1741 }
1742
1743 static int nfs3_xdr_dec_write3res(struct rpc_rqst *req, struct xdr_stream *xdr,
1744                                   void *data)
1745 {
1746         struct nfs_pgio_res *result = data;
1747         enum nfs_stat status;
1748         int error;
1749
1750         error = decode_nfsstat3(xdr, &status);
1751         if (unlikely(error))
1752                 goto out;
1753         error = decode_wcc_data(xdr, result->fattr);
1754         if (unlikely(error))
1755                 goto out;
1756         result->op_status = status;
1757         if (status != NFS3_OK)
1758                 goto out_status;
1759         error = decode_write3resok(xdr, result);
1760 out:
1761         return error;
1762 out_status:
1763         return nfs3_stat_to_errno(status);
1764 }
1765
1766 /*
1767  * 3.3.8  CREATE3res
1768  *
1769  *      struct CREATE3resok {
1770  *              post_op_fh3     obj;
1771  *              post_op_attr    obj_attributes;
1772  *              wcc_data        dir_wcc;
1773  *      };
1774  *
1775  *      struct CREATE3resfail {
1776  *              wcc_data        dir_wcc;
1777  *      };
1778  *
1779  *      union CREATE3res switch (nfsstat3 status) {
1780  *      case NFS3_OK:
1781  *              CREATE3resok    resok;
1782  *      default:
1783  *              CREATE3resfail  resfail;
1784  *      };
1785  */
1786 static int decode_create3resok(struct xdr_stream *xdr,
1787                                struct nfs3_diropres *result)
1788 {
1789         int error;
1790
1791         error = decode_post_op_fh3(xdr, result->fh);
1792         if (unlikely(error))
1793                 goto out;
1794         error = decode_post_op_attr(xdr, result->fattr);
1795         if (unlikely(error))
1796                 goto out;
1797         /* The server isn't required to return a file handle.
1798          * If it didn't, force the client to perform a LOOKUP
1799          * to determine the correct file handle and attribute
1800          * values for the new object. */
1801         if (result->fh->size == 0)
1802                 result->fattr->valid = 0;
1803         error = decode_wcc_data(xdr, result->dir_attr);
1804 out:
1805         return error;
1806 }
1807
1808 static int nfs3_xdr_dec_create3res(struct rpc_rqst *req,
1809                                    struct xdr_stream *xdr,
1810                                    void *data)
1811 {
1812         struct nfs3_diropres *result = data;
1813         enum nfs_stat status;
1814         int error;
1815
1816         error = decode_nfsstat3(xdr, &status);
1817         if (unlikely(error))
1818                 goto out;
1819         if (status != NFS3_OK)
1820                 goto out_default;
1821         error = decode_create3resok(xdr, result);
1822 out:
1823         return error;
1824 out_default:
1825         error = decode_wcc_data(xdr, result->dir_attr);
1826         if (unlikely(error))
1827                 goto out;
1828         return nfs3_stat_to_errno(status);
1829 }
1830
1831 /*
1832  * 3.3.12  REMOVE3res
1833  *
1834  *      struct REMOVE3resok {
1835  *              wcc_data    dir_wcc;
1836  *      };
1837  *
1838  *      struct REMOVE3resfail {
1839  *              wcc_data    dir_wcc;
1840  *      };
1841  *
1842  *      union REMOVE3res switch (nfsstat3 status) {
1843  *      case NFS3_OK:
1844  *              REMOVE3resok   resok;
1845  *      default:
1846  *              REMOVE3resfail resfail;
1847  *      };
1848  */
1849 static int nfs3_xdr_dec_remove3res(struct rpc_rqst *req,
1850                                    struct xdr_stream *xdr,
1851                                    void *data)
1852 {
1853         struct nfs_removeres *result = data;
1854         enum nfs_stat status;
1855         int error;
1856
1857         error = decode_nfsstat3(xdr, &status);
1858         if (unlikely(error))
1859                 goto out;
1860         error = decode_wcc_data(xdr, result->dir_attr);
1861         if (unlikely(error))
1862                 goto out;
1863         if (status != NFS3_OK)
1864                 goto out_status;
1865 out:
1866         return error;
1867 out_status:
1868         return nfs3_stat_to_errno(status);
1869 }
1870
1871 /*
1872  * 3.3.14  RENAME3res
1873  *
1874  *      struct RENAME3resok {
1875  *              wcc_data        fromdir_wcc;
1876  *              wcc_data        todir_wcc;
1877  *      };
1878  *
1879  *      struct RENAME3resfail {
1880  *              wcc_data        fromdir_wcc;
1881  *              wcc_data        todir_wcc;
1882  *      };
1883  *
1884  *      union RENAME3res switch (nfsstat3 status) {
1885  *      case NFS3_OK:
1886  *              RENAME3resok   resok;
1887  *      default:
1888  *              RENAME3resfail resfail;
1889  *      };
1890  */
1891 static int nfs3_xdr_dec_rename3res(struct rpc_rqst *req,
1892                                    struct xdr_stream *xdr,
1893                                    void *data)
1894 {
1895         struct nfs_renameres *result = data;
1896         enum nfs_stat status;
1897         int error;
1898
1899         error = decode_nfsstat3(xdr, &status);
1900         if (unlikely(error))
1901                 goto out;
1902         error = decode_wcc_data(xdr, result->old_fattr);
1903         if (unlikely(error))
1904                 goto out;
1905         error = decode_wcc_data(xdr, result->new_fattr);
1906         if (unlikely(error))
1907                 goto out;
1908         if (status != NFS3_OK)
1909                 goto out_status;
1910 out:
1911         return error;
1912 out_status:
1913         return nfs3_stat_to_errno(status);
1914 }
1915
1916 /*
1917  * 3.3.15  LINK3res
1918  *
1919  *      struct LINK3resok {
1920  *              post_op_attr    file_attributes;
1921  *              wcc_data        linkdir_wcc;
1922  *      };
1923  *
1924  *      struct LINK3resfail {
1925  *              post_op_attr    file_attributes;
1926  *              wcc_data        linkdir_wcc;
1927  *      };
1928  *
1929  *      union LINK3res switch (nfsstat3 status) {
1930  *      case NFS3_OK:
1931  *              LINK3resok      resok;
1932  *      default:
1933  *              LINK3resfail    resfail;
1934  *      };
1935  */
1936 static int nfs3_xdr_dec_link3res(struct rpc_rqst *req, struct xdr_stream *xdr,
1937                                  void *data)
1938 {
1939         struct nfs3_linkres *result = data;
1940         enum nfs_stat status;
1941         int error;
1942
1943         error = decode_nfsstat3(xdr, &status);
1944         if (unlikely(error))
1945                 goto out;
1946         error = decode_post_op_attr(xdr, result->fattr);
1947         if (unlikely(error))
1948                 goto out;
1949         error = decode_wcc_data(xdr, result->dir_attr);
1950         if (unlikely(error))
1951                 goto out;
1952         if (status != NFS3_OK)
1953                 goto out_status;
1954 out:
1955         return error;
1956 out_status:
1957         return nfs3_stat_to_errno(status);
1958 }
1959
1960 /**
1961  * nfs3_decode_dirent - Decode a single NFSv3 directory entry stored in
1962  *                      the local page cache
1963  * @xdr: XDR stream where entry resides
1964  * @entry: buffer to fill in with entry data
1965  * @plus: boolean indicating whether this should be a readdirplus entry
1966  *
1967  * Returns zero if successful, otherwise a negative errno value is
1968  * returned.
1969  *
1970  * This function is not invoked during READDIR reply decoding, but
1971  * rather whenever an application invokes the getdents(2) system call
1972  * on a directory already in our cache.
1973  *
1974  * 3.3.16  entry3
1975  *
1976  *      struct entry3 {
1977  *              fileid3         fileid;
1978  *              filename3       name;
1979  *              cookie3         cookie;
1980  *              fhandle3        filehandle;
1981  *              post_op_attr3   attributes;
1982  *              entry3          *nextentry;
1983  *      };
1984  *
1985  * 3.3.17  entryplus3
1986  *      struct entryplus3 {
1987  *              fileid3         fileid;
1988  *              filename3       name;
1989  *              cookie3         cookie;
1990  *              post_op_attr    name_attributes;
1991  *              post_op_fh3     name_handle;
1992  *              entryplus3      *nextentry;
1993  *      };
1994  */
1995 int nfs3_decode_dirent(struct xdr_stream *xdr, struct nfs_entry *entry,
1996                        bool plus)
1997 {
1998         struct nfs_entry old = *entry;
1999         __be32 *p;
2000         int error;
2001
2002         p = xdr_inline_decode(xdr, 4);
2003         if (unlikely(p == NULL))
2004                 goto out_overflow;
2005         if (*p == xdr_zero) {
2006                 p = xdr_inline_decode(xdr, 4);
2007                 if (unlikely(p == NULL))
2008                         goto out_overflow;
2009                 if (*p == xdr_zero)
2010                         return -EAGAIN;
2011                 entry->eof = 1;
2012                 return -EBADCOOKIE;
2013         }
2014
2015         error = decode_fileid3(xdr, &entry->ino);
2016         if (unlikely(error))
2017                 return error;
2018
2019         error = decode_inline_filename3(xdr, &entry->name, &entry->len);
2020         if (unlikely(error))
2021                 return error;
2022
2023         entry->prev_cookie = entry->cookie;
2024         error = decode_cookie3(xdr, &entry->cookie);
2025         if (unlikely(error))
2026                 return error;
2027
2028         entry->d_type = DT_UNKNOWN;
2029
2030         if (plus) {
2031                 entry->fattr->valid = 0;
2032                 error = decode_post_op_attr(xdr, entry->fattr);
2033                 if (unlikely(error))
2034                         return error;
2035                 if (entry->fattr->valid & NFS_ATTR_FATTR_V3)
2036                         entry->d_type = nfs_umode_to_dtype(entry->fattr->mode);
2037
2038                 if (entry->fattr->fileid != entry->ino) {
2039                         entry->fattr->mounted_on_fileid = entry->ino;
2040                         entry->fattr->valid |= NFS_ATTR_FATTR_MOUNTED_ON_FILEID;
2041                 }
2042
2043                 /* In fact, a post_op_fh3: */
2044                 p = xdr_inline_decode(xdr, 4);
2045                 if (unlikely(p == NULL))
2046                         goto out_overflow;
2047                 if (*p != xdr_zero) {
2048                         error = decode_nfs_fh3(xdr, entry->fh);
2049                         if (unlikely(error)) {
2050                                 if (error == -E2BIG)
2051                                         goto out_truncated;
2052                                 return error;
2053                         }
2054                 } else
2055                         zero_nfs_fh3(entry->fh);
2056         }
2057
2058         return 0;
2059
2060 out_overflow:
2061         print_overflow_msg(__func__, xdr);
2062         return -EAGAIN;
2063 out_truncated:
2064         dprintk("NFS: directory entry contains invalid file handle\n");
2065         *entry = old;
2066         return -EAGAIN;
2067 }
2068
2069 /*
2070  * 3.3.16  READDIR3res
2071  *
2072  *      struct dirlist3 {
2073  *              entry3          *entries;
2074  *              bool            eof;
2075  *      };
2076  *
2077  *      struct READDIR3resok {
2078  *              post_op_attr    dir_attributes;
2079  *              cookieverf3     cookieverf;
2080  *              dirlist3        reply;
2081  *      };
2082  *
2083  *      struct READDIR3resfail {
2084  *              post_op_attr    dir_attributes;
2085  *      };
2086  *
2087  *      union READDIR3res switch (nfsstat3 status) {
2088  *      case NFS3_OK:
2089  *              READDIR3resok   resok;
2090  *      default:
2091  *              READDIR3resfail resfail;
2092  *      };
2093  *
2094  * Read the directory contents into the page cache, but otherwise
2095  * don't touch them.  The actual decoding is done by nfs3_decode_entry()
2096  * during subsequent nfs_readdir() calls.
2097  */
2098 static int decode_dirlist3(struct xdr_stream *xdr)
2099 {
2100         return xdr_read_pages(xdr, xdr->buf->page_len);
2101 }
2102
2103 static int decode_readdir3resok(struct xdr_stream *xdr,
2104                                 struct nfs3_readdirres *result)
2105 {
2106         int error;
2107
2108         error = decode_post_op_attr(xdr, result->dir_attr);
2109         if (unlikely(error))
2110                 goto out;
2111         /* XXX: do we need to check if result->verf != NULL ? */
2112         error = decode_cookieverf3(xdr, result->verf);
2113         if (unlikely(error))
2114                 goto out;
2115         error = decode_dirlist3(xdr);
2116 out:
2117         return error;
2118 }
2119
2120 static int nfs3_xdr_dec_readdir3res(struct rpc_rqst *req,
2121                                     struct xdr_stream *xdr,
2122                                     void *data)
2123 {
2124         struct nfs3_readdirres *result = data;
2125         enum nfs_stat status;
2126         int error;
2127
2128         error = decode_nfsstat3(xdr, &status);
2129         if (unlikely(error))
2130                 goto out;
2131         if (status != NFS3_OK)
2132                 goto out_default;
2133         error = decode_readdir3resok(xdr, result);
2134 out:
2135         return error;
2136 out_default:
2137         error = decode_post_op_attr(xdr, result->dir_attr);
2138         if (unlikely(error))
2139                 goto out;
2140         return nfs3_stat_to_errno(status);
2141 }
2142
2143 /*
2144  * 3.3.18  FSSTAT3res
2145  *
2146  *      struct FSSTAT3resok {
2147  *              post_op_attr    obj_attributes;
2148  *              size3           tbytes;
2149  *              size3           fbytes;
2150  *              size3           abytes;
2151  *              size3           tfiles;
2152  *              size3           ffiles;
2153  *              size3           afiles;
2154  *              uint32          invarsec;
2155  *      };
2156  *
2157  *      struct FSSTAT3resfail {
2158  *              post_op_attr    obj_attributes;
2159  *      };
2160  *
2161  *      union FSSTAT3res switch (nfsstat3 status) {
2162  *      case NFS3_OK:
2163  *              FSSTAT3resok    resok;
2164  *      default:
2165  *              FSSTAT3resfail  resfail;
2166  *      };
2167  */
2168 static int decode_fsstat3resok(struct xdr_stream *xdr,
2169                                struct nfs_fsstat *result)
2170 {
2171         __be32 *p;
2172
2173         p = xdr_inline_decode(xdr, 8 * 6 + 4);
2174         if (unlikely(p == NULL))
2175                 goto out_overflow;
2176         p = xdr_decode_size3(p, &result->tbytes);
2177         p = xdr_decode_size3(p, &result->fbytes);
2178         p = xdr_decode_size3(p, &result->abytes);
2179         p = xdr_decode_size3(p, &result->tfiles);
2180         p = xdr_decode_size3(p, &result->ffiles);
2181         xdr_decode_size3(p, &result->afiles);
2182         /* ignore invarsec */
2183         return 0;
2184 out_overflow:
2185         print_overflow_msg(__func__, xdr);
2186         return -EIO;
2187 }
2188
2189 static int nfs3_xdr_dec_fsstat3res(struct rpc_rqst *req,
2190                                    struct xdr_stream *xdr,
2191                                    void *data)
2192 {
2193         struct nfs_fsstat *result = data;
2194         enum nfs_stat status;
2195         int error;
2196
2197         error = decode_nfsstat3(xdr, &status);
2198         if (unlikely(error))
2199                 goto out;
2200         error = decode_post_op_attr(xdr, result->fattr);
2201         if (unlikely(error))
2202                 goto out;
2203         if (status != NFS3_OK)
2204                 goto out_status;
2205         error = decode_fsstat3resok(xdr, result);
2206 out:
2207         return error;
2208 out_status:
2209         return nfs3_stat_to_errno(status);
2210 }
2211
2212 /*
2213  * 3.3.19  FSINFO3res
2214  *
2215  *      struct FSINFO3resok {
2216  *              post_op_attr    obj_attributes;
2217  *              uint32          rtmax;
2218  *              uint32          rtpref;
2219  *              uint32          rtmult;
2220  *              uint32          wtmax;
2221  *              uint32          wtpref;
2222  *              uint32          wtmult;
2223  *              uint32          dtpref;
2224  *              size3           maxfilesize;
2225  *              nfstime3        time_delta;
2226  *              uint32          properties;
2227  *      };
2228  *
2229  *      struct FSINFO3resfail {
2230  *              post_op_attr    obj_attributes;
2231  *      };
2232  *
2233  *      union FSINFO3res switch (nfsstat3 status) {
2234  *      case NFS3_OK:
2235  *              FSINFO3resok    resok;
2236  *      default:
2237  *              FSINFO3resfail  resfail;
2238  *      };
2239  */
2240 static int decode_fsinfo3resok(struct xdr_stream *xdr,
2241                                struct nfs_fsinfo *result)
2242 {
2243         __be32 *p;
2244
2245         p = xdr_inline_decode(xdr, 4 * 7 + 8 + 8 + 4);
2246         if (unlikely(p == NULL))
2247                 goto out_overflow;
2248         result->rtmax  = be32_to_cpup(p++);
2249         result->rtpref = be32_to_cpup(p++);
2250         result->rtmult = be32_to_cpup(p++);
2251         result->wtmax  = be32_to_cpup(p++);
2252         result->wtpref = be32_to_cpup(p++);
2253         result->wtmult = be32_to_cpup(p++);
2254         result->dtpref = be32_to_cpup(p++);
2255         p = xdr_decode_size3(p, &result->maxfilesize);
2256         xdr_decode_nfstime3(p, &result->time_delta);
2257
2258         /* ignore properties */
2259         result->lease_time = 0;
2260         return 0;
2261 out_overflow:
2262         print_overflow_msg(__func__, xdr);
2263         return -EIO;
2264 }
2265
2266 static int nfs3_xdr_dec_fsinfo3res(struct rpc_rqst *req,
2267                                    struct xdr_stream *xdr,
2268                                    void *data)
2269 {
2270         struct nfs_fsinfo *result = data;
2271         enum nfs_stat status;
2272         int error;
2273
2274         error = decode_nfsstat3(xdr, &status);
2275         if (unlikely(error))
2276                 goto out;
2277         error = decode_post_op_attr(xdr, result->fattr);
2278         if (unlikely(error))
2279                 goto out;
2280         if (status != NFS3_OK)
2281                 goto out_status;
2282         error = decode_fsinfo3resok(xdr, result);
2283 out:
2284         return error;
2285 out_status:
2286         return nfs3_stat_to_errno(status);
2287 }
2288
2289 /*
2290  * 3.3.20  PATHCONF3res
2291  *
2292  *      struct PATHCONF3resok {
2293  *              post_op_attr    obj_attributes;
2294  *              uint32          linkmax;
2295  *              uint32          name_max;
2296  *              bool            no_trunc;
2297  *              bool            chown_restricted;
2298  *              bool            case_insensitive;
2299  *              bool            case_preserving;
2300  *      };
2301  *
2302  *      struct PATHCONF3resfail {
2303  *              post_op_attr    obj_attributes;
2304  *      };
2305  *
2306  *      union PATHCONF3res switch (nfsstat3 status) {
2307  *      case NFS3_OK:
2308  *              PATHCONF3resok  resok;
2309  *      default:
2310  *              PATHCONF3resfail resfail;
2311  *      };
2312  */
2313 static int decode_pathconf3resok(struct xdr_stream *xdr,
2314                                  struct nfs_pathconf *result)
2315 {
2316         __be32 *p;
2317
2318         p = xdr_inline_decode(xdr, 4 * 6);
2319         if (unlikely(p == NULL))
2320                 goto out_overflow;
2321         result->max_link = be32_to_cpup(p++);
2322         result->max_namelen = be32_to_cpup(p);
2323         /* ignore remaining fields */
2324         return 0;
2325 out_overflow:
2326         print_overflow_msg(__func__, xdr);
2327         return -EIO;
2328 }
2329
2330 static int nfs3_xdr_dec_pathconf3res(struct rpc_rqst *req,
2331                                      struct xdr_stream *xdr,
2332                                      void *data)
2333 {
2334         struct nfs_pathconf *result = data;
2335         enum nfs_stat status;
2336         int error;
2337
2338         error = decode_nfsstat3(xdr, &status);
2339         if (unlikely(error))
2340                 goto out;
2341         error = decode_post_op_attr(xdr, result->fattr);
2342         if (unlikely(error))
2343                 goto out;
2344         if (status != NFS3_OK)
2345                 goto out_status;
2346         error = decode_pathconf3resok(xdr, result);
2347 out:
2348         return error;
2349 out_status:
2350         return nfs3_stat_to_errno(status);
2351 }
2352
2353 /*
2354  * 3.3.21  COMMIT3res
2355  *
2356  *      struct COMMIT3resok {
2357  *              wcc_data        file_wcc;
2358  *              writeverf3      verf;
2359  *      };
2360  *
2361  *      struct COMMIT3resfail {
2362  *              wcc_data        file_wcc;
2363  *      };
2364  *
2365  *      union COMMIT3res switch (nfsstat3 status) {
2366  *      case NFS3_OK:
2367  *              COMMIT3resok    resok;
2368  *      default:
2369  *              COMMIT3resfail  resfail;
2370  *      };
2371  */
2372 static int nfs3_xdr_dec_commit3res(struct rpc_rqst *req,
2373                                    struct xdr_stream *xdr,
2374                                    void *data)
2375 {
2376         struct nfs_commitres *result = data;
2377         struct nfs_writeverf *verf = result->verf;
2378         enum nfs_stat status;
2379         int error;
2380
2381         error = decode_nfsstat3(xdr, &status);
2382         if (unlikely(error))
2383                 goto out;
2384         error = decode_wcc_data(xdr, result->fattr);
2385         if (unlikely(error))
2386                 goto out;
2387         result->op_status = status;
2388         if (status != NFS3_OK)
2389                 goto out_status;
2390         error = decode_writeverf3(xdr, &verf->verifier);
2391         if (!error)
2392                 verf->committed = NFS_FILE_SYNC;
2393 out:
2394         return error;
2395 out_status:
2396         return nfs3_stat_to_errno(status);
2397 }
2398
2399 #ifdef CONFIG_NFS_V3_ACL
2400
2401 static inline int decode_getacl3resok(struct xdr_stream *xdr,
2402                                       struct nfs3_getaclres *result)
2403 {
2404         struct posix_acl **acl;
2405         unsigned int *aclcnt;
2406         size_t hdrlen;
2407         int error;
2408
2409         error = decode_post_op_attr(xdr, result->fattr);
2410         if (unlikely(error))
2411                 goto out;
2412         error = decode_uint32(xdr, &result->mask);
2413         if (unlikely(error))
2414                 goto out;
2415         error = -EINVAL;
2416         if (result->mask & ~(NFS_ACL|NFS_ACLCNT|NFS_DFACL|NFS_DFACLCNT))
2417                 goto out;
2418
2419         hdrlen = xdr_stream_pos(xdr);
2420
2421         acl = NULL;
2422         if (result->mask & NFS_ACL)
2423                 acl = &result->acl_access;
2424         aclcnt = NULL;
2425         if (result->mask & NFS_ACLCNT)
2426                 aclcnt = &result->acl_access_count;
2427         error = nfsacl_decode(xdr->buf, hdrlen, aclcnt, acl);
2428         if (unlikely(error <= 0))
2429                 goto out;
2430
2431         acl = NULL;
2432         if (result->mask & NFS_DFACL)
2433                 acl = &result->acl_default;
2434         aclcnt = NULL;
2435         if (result->mask & NFS_DFACLCNT)
2436                 aclcnt = &result->acl_default_count;
2437         error = nfsacl_decode(xdr->buf, hdrlen + error, aclcnt, acl);
2438         if (unlikely(error <= 0))
2439                 return error;
2440         error = 0;
2441 out:
2442         return error;
2443 }
2444
2445 static int nfs3_xdr_dec_getacl3res(struct rpc_rqst *req,
2446                                    struct xdr_stream *xdr,
2447                                    void *result)
2448 {
2449         enum nfs_stat status;
2450         int error;
2451
2452         error = decode_nfsstat3(xdr, &status);
2453         if (unlikely(error))
2454                 goto out;
2455         if (status != NFS3_OK)
2456                 goto out_default;
2457         error = decode_getacl3resok(xdr, result);
2458 out:
2459         return error;
2460 out_default:
2461         return nfs3_stat_to_errno(status);
2462 }
2463
2464 static int nfs3_xdr_dec_setacl3res(struct rpc_rqst *req,
2465                                    struct xdr_stream *xdr,
2466                                    void *result)
2467 {
2468         enum nfs_stat status;
2469         int error;
2470
2471         error = decode_nfsstat3(xdr, &status);
2472         if (unlikely(error))
2473                 goto out;
2474         if (status != NFS3_OK)
2475                 goto out_default;
2476         error = decode_post_op_attr(xdr, result);
2477 out:
2478         return error;
2479 out_default:
2480         return nfs3_stat_to_errno(status);
2481 }
2482
2483 #endif  /* CONFIG_NFS_V3_ACL */
2484
2485
2486 /*
2487  * We need to translate between nfs status return values and
2488  * the local errno values which may not be the same.
2489  */
2490 static const struct {
2491         int stat;
2492         int errno;
2493 } nfs_errtbl[] = {
2494         { NFS_OK,               0               },
2495         { NFSERR_PERM,          -EPERM          },
2496         { NFSERR_NOENT,         -ENOENT         },
2497         { NFSERR_IO,            -errno_NFSERR_IO},
2498         { NFSERR_NXIO,          -ENXIO          },
2499 /*      { NFSERR_EAGAIN,        -EAGAIN         }, */
2500         { NFSERR_ACCES,         -EACCES         },
2501         { NFSERR_EXIST,         -EEXIST         },
2502         { NFSERR_XDEV,          -EXDEV          },
2503         { NFSERR_NODEV,         -ENODEV         },
2504         { NFSERR_NOTDIR,        -ENOTDIR        },
2505         { NFSERR_ISDIR,         -EISDIR         },
2506         { NFSERR_INVAL,         -EINVAL         },
2507         { NFSERR_FBIG,          -EFBIG          },
2508         { NFSERR_NOSPC,         -ENOSPC         },
2509         { NFSERR_ROFS,          -EROFS          },
2510         { NFSERR_MLINK,         -EMLINK         },
2511         { NFSERR_NAMETOOLONG,   -ENAMETOOLONG   },
2512         { NFSERR_NOTEMPTY,      -ENOTEMPTY      },
2513         { NFSERR_DQUOT,         -EDQUOT         },
2514         { NFSERR_STALE,         -ESTALE         },
2515         { NFSERR_REMOTE,        -EREMOTE        },
2516 #ifdef EWFLUSH
2517         { NFSERR_WFLUSH,        -EWFLUSH        },
2518 #endif
2519         { NFSERR_BADHANDLE,     -EBADHANDLE     },
2520         { NFSERR_NOT_SYNC,      -ENOTSYNC       },
2521         { NFSERR_BAD_COOKIE,    -EBADCOOKIE     },
2522         { NFSERR_NOTSUPP,       -ENOTSUPP       },
2523         { NFSERR_TOOSMALL,      -ETOOSMALL      },
2524         { NFSERR_SERVERFAULT,   -EREMOTEIO      },
2525         { NFSERR_BADTYPE,       -EBADTYPE       },
2526         { NFSERR_JUKEBOX,       -EJUKEBOX       },
2527         { -1,                   -EIO            }
2528 };
2529
2530 /**
2531  * nfs3_stat_to_errno - convert an NFS status code to a local errno
2532  * @status: NFS status code to convert
2533  *
2534  * Returns a local errno value, or -EIO if the NFS status code is
2535  * not recognized.  This function is used jointly by NFSv2 and NFSv3.
2536  */
2537 static int nfs3_stat_to_errno(enum nfs_stat status)
2538 {
2539         int i;
2540
2541         for (i = 0; nfs_errtbl[i].stat != -1; i++) {
2542                 if (nfs_errtbl[i].stat == (int)status)
2543                         return nfs_errtbl[i].errno;
2544         }
2545         dprintk("NFS: Unrecognized nfs status value: %u\n", status);
2546         return nfs_errtbl[i].errno;
2547 }
2548
2549
2550 #define PROC(proc, argtype, restype, timer)                             \
2551 [NFS3PROC_##proc] = {                                                   \
2552         .p_proc      = NFS3PROC_##proc,                                 \
2553         .p_encode    = nfs3_xdr_enc_##argtype##3args,                   \
2554         .p_decode    = nfs3_xdr_dec_##restype##3res,                    \
2555         .p_arglen    = NFS3_##argtype##args_sz,                         \
2556         .p_replen    = NFS3_##restype##res_sz,                          \
2557         .p_timer     = timer,                                           \
2558         .p_statidx   = NFS3PROC_##proc,                                 \
2559         .p_name      = #proc,                                           \
2560         }
2561
2562 const struct rpc_procinfo nfs3_procedures[] = {
2563         PROC(GETATTR,           getattr,        getattr,        1),
2564         PROC(SETATTR,           setattr,        setattr,        0),
2565         PROC(LOOKUP,            lookup,         lookup,         2),
2566         PROC(ACCESS,            access,         access,         1),
2567         PROC(READLINK,          readlink,       readlink,       3),
2568         PROC(READ,              read,           read,           3),
2569         PROC(WRITE,             write,          write,          4),
2570         PROC(CREATE,            create,         create,         0),
2571         PROC(MKDIR,             mkdir,          create,         0),
2572         PROC(SYMLINK,           symlink,        create,         0),
2573         PROC(MKNOD,             mknod,          create,         0),
2574         PROC(REMOVE,            remove,         remove,         0),
2575         PROC(RMDIR,             lookup,         setattr,        0),
2576         PROC(RENAME,            rename,         rename,         0),
2577         PROC(LINK,              link,           link,           0),
2578         PROC(READDIR,           readdir,        readdir,        3),
2579         PROC(READDIRPLUS,       readdirplus,    readdir,        3),
2580         PROC(FSSTAT,            getattr,        fsstat,         0),
2581         PROC(FSINFO,            getattr,        fsinfo,         0),
2582         PROC(PATHCONF,          getattr,        pathconf,       0),
2583         PROC(COMMIT,            commit,         commit,         5),
2584 };
2585
2586 static unsigned int nfs_version3_counts[ARRAY_SIZE(nfs3_procedures)];
2587 const struct rpc_version nfs_version3 = {
2588         .number                 = 3,
2589         .nrprocs                = ARRAY_SIZE(nfs3_procedures),
2590         .procs                  = nfs3_procedures,
2591         .counts                 = nfs_version3_counts,
2592 };
2593
2594 #ifdef CONFIG_NFS_V3_ACL
2595 static const struct rpc_procinfo nfs3_acl_procedures[] = {
2596         [ACLPROC3_GETACL] = {
2597                 .p_proc = ACLPROC3_GETACL,
2598                 .p_encode = nfs3_xdr_enc_getacl3args,
2599                 .p_decode = nfs3_xdr_dec_getacl3res,
2600                 .p_arglen = ACL3_getaclargs_sz,
2601                 .p_replen = ACL3_getaclres_sz,
2602                 .p_timer = 1,
2603                 .p_name = "GETACL",
2604         },
2605         [ACLPROC3_SETACL] = {
2606                 .p_proc = ACLPROC3_SETACL,
2607                 .p_encode = nfs3_xdr_enc_setacl3args,
2608                 .p_decode = nfs3_xdr_dec_setacl3res,
2609                 .p_arglen = ACL3_setaclargs_sz,
2610                 .p_replen = ACL3_setaclres_sz,
2611                 .p_timer = 0,
2612                 .p_name = "SETACL",
2613         },
2614 };
2615
2616 static unsigned int nfs3_acl_counts[ARRAY_SIZE(nfs3_acl_procedures)];
2617 const struct rpc_version nfsacl_version3 = {
2618         .number                 = 3,
2619         .nrprocs                = ARRAY_SIZE(nfs3_acl_procedures),
2620         .procs                  = nfs3_acl_procedures,
2621         .counts                 = nfs3_acl_counts,
2622 };
2623 #endif  /* CONFIG_NFS_V3_ACL */