GNU Linux-libre 4.19.264-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         struct timespec ts;
566         u32 nbytes;
567         __be32 *p;
568
569         /*
570          * In order to make only a single xdr_reserve_space() call,
571          * pre-compute the total number of bytes to be reserved.
572          * Six boolean values, one for each set_foo field, are always
573          * present in the encoded result, so start there.
574          */
575         nbytes = 6 * 4;
576         if (attr->ia_valid & ATTR_MODE)
577                 nbytes += 4;
578         if (attr->ia_valid & ATTR_UID)
579                 nbytes += 4;
580         if (attr->ia_valid & ATTR_GID)
581                 nbytes += 4;
582         if (attr->ia_valid & ATTR_SIZE)
583                 nbytes += 8;
584         if (attr->ia_valid & ATTR_ATIME_SET)
585                 nbytes += 8;
586         if (attr->ia_valid & ATTR_MTIME_SET)
587                 nbytes += 8;
588         p = xdr_reserve_space(xdr, nbytes);
589
590         if (attr->ia_valid & ATTR_MODE) {
591                 *p++ = xdr_one;
592                 *p++ = cpu_to_be32(attr->ia_mode & S_IALLUGO);
593         } else
594                 *p++ = xdr_zero;
595
596         if (attr->ia_valid & ATTR_UID) {
597                 *p++ = xdr_one;
598                 *p++ = cpu_to_be32(from_kuid(&init_user_ns, attr->ia_uid));
599         } else
600                 *p++ = xdr_zero;
601
602         if (attr->ia_valid & ATTR_GID) {
603                 *p++ = xdr_one;
604                 *p++ = cpu_to_be32(from_kgid(&init_user_ns, attr->ia_gid));
605         } else
606                 *p++ = xdr_zero;
607
608         if (attr->ia_valid & ATTR_SIZE) {
609                 *p++ = xdr_one;
610                 p = xdr_encode_hyper(p, (u64)attr->ia_size);
611         } else
612                 *p++ = xdr_zero;
613
614         if (attr->ia_valid & ATTR_ATIME_SET) {
615                 struct timespec ts;
616                 *p++ = xdr_two;
617                 ts = timespec64_to_timespec(attr->ia_atime);
618                 p = xdr_encode_nfstime3(p, &ts);
619         } else if (attr->ia_valid & ATTR_ATIME) {
620                 *p++ = xdr_one;
621         } else
622                 *p++ = xdr_zero;
623
624         if (attr->ia_valid & ATTR_MTIME_SET) {
625                 *p++ = xdr_two;
626                 ts = timespec64_to_timespec(attr->ia_mtime);
627                 xdr_encode_nfstime3(p, &ts);
628         } else if (attr->ia_valid & ATTR_MTIME) {
629                 *p = xdr_one;
630         } else
631                 *p = xdr_zero;
632 }
633
634 /*
635  * fattr3
636  *
637  *      struct fattr3 {
638  *              ftype3          type;
639  *              mode3           mode;
640  *              uint32          nlink;
641  *              uid3            uid;
642  *              gid3            gid;
643  *              size3           size;
644  *              size3           used;
645  *              specdata3       rdev;
646  *              uint64          fsid;
647  *              fileid3         fileid;
648  *              nfstime3        atime;
649  *              nfstime3        mtime;
650  *              nfstime3        ctime;
651  *      };
652  */
653 static int decode_fattr3(struct xdr_stream *xdr, struct nfs_fattr *fattr)
654 {
655         umode_t fmode;
656         __be32 *p;
657
658         p = xdr_inline_decode(xdr, NFS3_fattr_sz << 2);
659         if (unlikely(p == NULL))
660                 goto out_overflow;
661
662         p = xdr_decode_ftype3(p, &fmode);
663
664         fattr->mode = (be32_to_cpup(p++) & ~S_IFMT) | fmode;
665         fattr->nlink = be32_to_cpup(p++);
666         fattr->uid = make_kuid(&init_user_ns, be32_to_cpup(p++));
667         if (!uid_valid(fattr->uid))
668                 goto out_uid;
669         fattr->gid = make_kgid(&init_user_ns, be32_to_cpup(p++));
670         if (!gid_valid(fattr->gid))
671                 goto out_gid;
672
673         p = xdr_decode_size3(p, &fattr->size);
674         p = xdr_decode_size3(p, &fattr->du.nfs3.used);
675         p = xdr_decode_specdata3(p, &fattr->rdev);
676
677         p = xdr_decode_hyper(p, &fattr->fsid.major);
678         fattr->fsid.minor = 0;
679
680         p = xdr_decode_fileid3(p, &fattr->fileid);
681         p = xdr_decode_nfstime3(p, &fattr->atime);
682         p = xdr_decode_nfstime3(p, &fattr->mtime);
683         xdr_decode_nfstime3(p, &fattr->ctime);
684         fattr->change_attr = nfs_timespec_to_change_attr(&fattr->ctime);
685
686         fattr->valid |= NFS_ATTR_FATTR_V3;
687         return 0;
688 out_uid:
689         dprintk("NFS: returned invalid uid\n");
690         return -EINVAL;
691 out_gid:
692         dprintk("NFS: returned invalid gid\n");
693         return -EINVAL;
694 out_overflow:
695         print_overflow_msg(__func__, xdr);
696         return -EIO;
697 }
698
699 /*
700  * post_op_attr
701  *
702  *      union post_op_attr switch (bool attributes_follow) {
703  *      case TRUE:
704  *              fattr3  attributes;
705  *      case FALSE:
706  *              void;
707  *      };
708  */
709 static int decode_post_op_attr(struct xdr_stream *xdr, struct nfs_fattr *fattr)
710 {
711         __be32 *p;
712
713         p = xdr_inline_decode(xdr, 4);
714         if (unlikely(p == NULL))
715                 goto out_overflow;
716         if (*p != xdr_zero)
717                 return decode_fattr3(xdr, fattr);
718         return 0;
719 out_overflow:
720         print_overflow_msg(__func__, xdr);
721         return -EIO;
722 }
723
724 /*
725  * wcc_attr
726  *      struct wcc_attr {
727  *              size3           size;
728  *              nfstime3        mtime;
729  *              nfstime3        ctime;
730  *      };
731  */
732 static int decode_wcc_attr(struct xdr_stream *xdr, struct nfs_fattr *fattr)
733 {
734         __be32 *p;
735
736         p = xdr_inline_decode(xdr, NFS3_wcc_attr_sz << 2);
737         if (unlikely(p == NULL))
738                 goto out_overflow;
739
740         fattr->valid |= NFS_ATTR_FATTR_PRESIZE
741                 | NFS_ATTR_FATTR_PRECHANGE
742                 | NFS_ATTR_FATTR_PREMTIME
743                 | NFS_ATTR_FATTR_PRECTIME;
744
745         p = xdr_decode_size3(p, &fattr->pre_size);
746         p = xdr_decode_nfstime3(p, &fattr->pre_mtime);
747         xdr_decode_nfstime3(p, &fattr->pre_ctime);
748         fattr->pre_change_attr = nfs_timespec_to_change_attr(&fattr->pre_ctime);
749
750         return 0;
751 out_overflow:
752         print_overflow_msg(__func__, xdr);
753         return -EIO;
754 }
755
756 /*
757  * pre_op_attr
758  *      union pre_op_attr switch (bool attributes_follow) {
759  *      case TRUE:
760  *              wcc_attr        attributes;
761  *      case FALSE:
762  *              void;
763  *      };
764  *
765  * wcc_data
766  *
767  *      struct wcc_data {
768  *              pre_op_attr     before;
769  *              post_op_attr    after;
770  *      };
771  */
772 static int decode_pre_op_attr(struct xdr_stream *xdr, struct nfs_fattr *fattr)
773 {
774         __be32 *p;
775
776         p = xdr_inline_decode(xdr, 4);
777         if (unlikely(p == NULL))
778                 goto out_overflow;
779         if (*p != xdr_zero)
780                 return decode_wcc_attr(xdr, fattr);
781         return 0;
782 out_overflow:
783         print_overflow_msg(__func__, xdr);
784         return -EIO;
785 }
786
787 static int decode_wcc_data(struct xdr_stream *xdr, struct nfs_fattr *fattr)
788 {
789         int error;
790
791         error = decode_pre_op_attr(xdr, fattr);
792         if (unlikely(error))
793                 goto out;
794         error = decode_post_op_attr(xdr, fattr);
795 out:
796         return error;
797 }
798
799 /*
800  * post_op_fh3
801  *
802  *      union post_op_fh3 switch (bool handle_follows) {
803  *      case TRUE:
804  *              nfs_fh3  handle;
805  *      case FALSE:
806  *              void;
807  *      };
808  */
809 static int decode_post_op_fh3(struct xdr_stream *xdr, struct nfs_fh *fh)
810 {
811         __be32 *p = xdr_inline_decode(xdr, 4);
812         if (unlikely(p == NULL))
813                 goto out_overflow;
814         if (*p != xdr_zero)
815                 return decode_nfs_fh3(xdr, fh);
816         zero_nfs_fh3(fh);
817         return 0;
818 out_overflow:
819         print_overflow_msg(__func__, xdr);
820         return -EIO;
821 }
822
823 /*
824  * diropargs3
825  *
826  *      struct diropargs3 {
827  *              nfs_fh3         dir;
828  *              filename3       name;
829  *      };
830  */
831 static void encode_diropargs3(struct xdr_stream *xdr, const struct nfs_fh *fh,
832                               const char *name, u32 length)
833 {
834         encode_nfs_fh3(xdr, fh);
835         encode_filename3(xdr, name, length);
836 }
837
838
839 /*
840  * NFSv3 XDR encode functions
841  *
842  * NFSv3 argument types are defined in section 3.3 of RFC 1813:
843  * "NFS Version 3 Protocol Specification".
844  */
845
846 /*
847  * 3.3.1  GETATTR3args
848  *
849  *      struct GETATTR3args {
850  *              nfs_fh3  object;
851  *      };
852  */
853 static void nfs3_xdr_enc_getattr3args(struct rpc_rqst *req,
854                                       struct xdr_stream *xdr,
855                                       const void *data)
856 {
857         const struct nfs_fh *fh = data;
858
859         encode_nfs_fh3(xdr, fh);
860 }
861
862 /*
863  * 3.3.2  SETATTR3args
864  *
865  *      union sattrguard3 switch (bool check) {
866  *      case TRUE:
867  *              nfstime3  obj_ctime;
868  *      case FALSE:
869  *              void;
870  *      };
871  *
872  *      struct SETATTR3args {
873  *              nfs_fh3         object;
874  *              sattr3          new_attributes;
875  *              sattrguard3     guard;
876  *      };
877  */
878 static void encode_sattrguard3(struct xdr_stream *xdr,
879                                const struct nfs3_sattrargs *args)
880 {
881         __be32 *p;
882
883         if (args->guard) {
884                 p = xdr_reserve_space(xdr, 4 + 8);
885                 *p++ = xdr_one;
886                 xdr_encode_nfstime3(p, &args->guardtime);
887         } else {
888                 p = xdr_reserve_space(xdr, 4);
889                 *p = xdr_zero;
890         }
891 }
892
893 static void nfs3_xdr_enc_setattr3args(struct rpc_rqst *req,
894                                       struct xdr_stream *xdr,
895                                       const void *data)
896 {
897         const struct nfs3_sattrargs *args = data;
898         encode_nfs_fh3(xdr, args->fh);
899         encode_sattr3(xdr, args->sattr);
900         encode_sattrguard3(xdr, args);
901 }
902
903 /*
904  * 3.3.3  LOOKUP3args
905  *
906  *      struct LOOKUP3args {
907  *              diropargs3  what;
908  *      };
909  */
910 static void nfs3_xdr_enc_lookup3args(struct rpc_rqst *req,
911                                      struct xdr_stream *xdr,
912                                      const void *data)
913 {
914         const struct nfs3_diropargs *args = data;
915
916         encode_diropargs3(xdr, args->fh, args->name, args->len);
917 }
918
919 /*
920  * 3.3.4  ACCESS3args
921  *
922  *      struct ACCESS3args {
923  *              nfs_fh3         object;
924  *              uint32          access;
925  *      };
926  */
927 static void encode_access3args(struct xdr_stream *xdr,
928                                const struct nfs3_accessargs *args)
929 {
930         encode_nfs_fh3(xdr, args->fh);
931         encode_uint32(xdr, args->access);
932 }
933
934 static void nfs3_xdr_enc_access3args(struct rpc_rqst *req,
935                                      struct xdr_stream *xdr,
936                                      const void *data)
937 {
938         const struct nfs3_accessargs *args = data;
939
940         encode_access3args(xdr, args);
941 }
942
943 /*
944  * 3.3.5  READLINK3args
945  *
946  *      struct READLINK3args {
947  *              nfs_fh3 symlink;
948  *      };
949  */
950 static void nfs3_xdr_enc_readlink3args(struct rpc_rqst *req,
951                                        struct xdr_stream *xdr,
952                                        const void *data)
953 {
954         const struct nfs3_readlinkargs *args = data;
955
956         encode_nfs_fh3(xdr, args->fh);
957         prepare_reply_buffer(req, args->pages, args->pgbase,
958                                         args->pglen, NFS3_readlinkres_sz);
959 }
960
961 /*
962  * 3.3.6  READ3args
963  *
964  *      struct READ3args {
965  *              nfs_fh3         file;
966  *              offset3         offset;
967  *              count3          count;
968  *      };
969  */
970 static void encode_read3args(struct xdr_stream *xdr,
971                              const struct nfs_pgio_args *args)
972 {
973         __be32 *p;
974
975         encode_nfs_fh3(xdr, args->fh);
976
977         p = xdr_reserve_space(xdr, 8 + 4);
978         p = xdr_encode_hyper(p, args->offset);
979         *p = cpu_to_be32(args->count);
980 }
981
982 static void nfs3_xdr_enc_read3args(struct rpc_rqst *req,
983                                    struct xdr_stream *xdr,
984                                    const void *data)
985 {
986         const struct nfs_pgio_args *args = data;
987
988         encode_read3args(xdr, args);
989         prepare_reply_buffer(req, args->pages, args->pgbase,
990                                         args->count, NFS3_readres_sz);
991         req->rq_rcv_buf.flags |= XDRBUF_READ;
992 }
993
994 /*
995  * 3.3.7  WRITE3args
996  *
997  *      enum stable_how {
998  *              UNSTABLE  = 0,
999  *              DATA_SYNC = 1,
1000  *              FILE_SYNC = 2
1001  *      };
1002  *
1003  *      struct WRITE3args {
1004  *              nfs_fh3         file;
1005  *              offset3         offset;
1006  *              count3          count;
1007  *              stable_how      stable;
1008  *              opaque          data<>;
1009  *      };
1010  */
1011 static void encode_write3args(struct xdr_stream *xdr,
1012                               const struct nfs_pgio_args *args)
1013 {
1014         __be32 *p;
1015
1016         encode_nfs_fh3(xdr, args->fh);
1017
1018         p = xdr_reserve_space(xdr, 8 + 4 + 4 + 4);
1019         p = xdr_encode_hyper(p, args->offset);
1020         *p++ = cpu_to_be32(args->count);
1021         *p++ = cpu_to_be32(args->stable);
1022         *p = cpu_to_be32(args->count);
1023         xdr_write_pages(xdr, args->pages, args->pgbase, args->count);
1024 }
1025
1026 static void nfs3_xdr_enc_write3args(struct rpc_rqst *req,
1027                                     struct xdr_stream *xdr,
1028                                     const void *data)
1029 {
1030         const struct nfs_pgio_args *args = data;
1031
1032         encode_write3args(xdr, args);
1033         xdr->buf->flags |= XDRBUF_WRITE;
1034 }
1035
1036 /*
1037  * 3.3.8  CREATE3args
1038  *
1039  *      enum createmode3 {
1040  *              UNCHECKED = 0,
1041  *              GUARDED   = 1,
1042  *              EXCLUSIVE = 2
1043  *      };
1044  *
1045  *      union createhow3 switch (createmode3 mode) {
1046  *      case UNCHECKED:
1047  *      case GUARDED:
1048  *              sattr3       obj_attributes;
1049  *      case EXCLUSIVE:
1050  *              createverf3  verf;
1051  *      };
1052  *
1053  *      struct CREATE3args {
1054  *              diropargs3      where;
1055  *              createhow3      how;
1056  *      };
1057  */
1058 static void encode_createhow3(struct xdr_stream *xdr,
1059                               const struct nfs3_createargs *args)
1060 {
1061         encode_uint32(xdr, args->createmode);
1062         switch (args->createmode) {
1063         case NFS3_CREATE_UNCHECKED:
1064         case NFS3_CREATE_GUARDED:
1065                 encode_sattr3(xdr, args->sattr);
1066                 break;
1067         case NFS3_CREATE_EXCLUSIVE:
1068                 encode_createverf3(xdr, args->verifier);
1069                 break;
1070         default:
1071                 BUG();
1072         }
1073 }
1074
1075 static void nfs3_xdr_enc_create3args(struct rpc_rqst *req,
1076                                      struct xdr_stream *xdr,
1077                                      const void *data)
1078 {
1079         const struct nfs3_createargs *args = data;
1080
1081         encode_diropargs3(xdr, args->fh, args->name, args->len);
1082         encode_createhow3(xdr, args);
1083 }
1084
1085 /*
1086  * 3.3.9  MKDIR3args
1087  *
1088  *      struct MKDIR3args {
1089  *              diropargs3      where;
1090  *              sattr3          attributes;
1091  *      };
1092  */
1093 static void nfs3_xdr_enc_mkdir3args(struct rpc_rqst *req,
1094                                     struct xdr_stream *xdr,
1095                                     const void *data)
1096 {
1097         const struct nfs3_mkdirargs *args = data;
1098
1099         encode_diropargs3(xdr, args->fh, args->name, args->len);
1100         encode_sattr3(xdr, args->sattr);
1101 }
1102
1103 /*
1104  * 3.3.10  SYMLINK3args
1105  *
1106  *      struct symlinkdata3 {
1107  *              sattr3          symlink_attributes;
1108  *              nfspath3        symlink_data;
1109  *      };
1110  *
1111  *      struct SYMLINK3args {
1112  *              diropargs3      where;
1113  *              symlinkdata3    symlink;
1114  *      };
1115  */
1116 static void encode_symlinkdata3(struct xdr_stream *xdr,
1117                                 const void *data)
1118 {
1119         const struct nfs3_symlinkargs *args = data;
1120
1121         encode_sattr3(xdr, args->sattr);
1122         encode_nfspath3(xdr, args->pages, args->pathlen);
1123 }
1124
1125 static void nfs3_xdr_enc_symlink3args(struct rpc_rqst *req,
1126                                       struct xdr_stream *xdr,
1127                                       const void *data)
1128 {
1129         const struct nfs3_symlinkargs *args = data;
1130
1131         encode_diropargs3(xdr, args->fromfh, args->fromname, args->fromlen);
1132         encode_symlinkdata3(xdr, args);
1133         xdr->buf->flags |= XDRBUF_WRITE;
1134 }
1135
1136 /*
1137  * 3.3.11  MKNOD3args
1138  *
1139  *      struct devicedata3 {
1140  *              sattr3          dev_attributes;
1141  *              specdata3       spec;
1142  *      };
1143  *
1144  *      union mknoddata3 switch (ftype3 type) {
1145  *      case NF3CHR:
1146  *      case NF3BLK:
1147  *              devicedata3     device;
1148  *      case NF3SOCK:
1149  *      case NF3FIFO:
1150  *              sattr3          pipe_attributes;
1151  *      default:
1152  *              void;
1153  *      };
1154  *
1155  *      struct MKNOD3args {
1156  *              diropargs3      where;
1157  *              mknoddata3      what;
1158  *      };
1159  */
1160 static void encode_devicedata3(struct xdr_stream *xdr,
1161                                const struct nfs3_mknodargs *args)
1162 {
1163         encode_sattr3(xdr, args->sattr);
1164         encode_specdata3(xdr, args->rdev);
1165 }
1166
1167 static void encode_mknoddata3(struct xdr_stream *xdr,
1168                               const struct nfs3_mknodargs *args)
1169 {
1170         encode_ftype3(xdr, args->type);
1171         switch (args->type) {
1172         case NF3CHR:
1173         case NF3BLK:
1174                 encode_devicedata3(xdr, args);
1175                 break;
1176         case NF3SOCK:
1177         case NF3FIFO:
1178                 encode_sattr3(xdr, args->sattr);
1179                 break;
1180         case NF3REG:
1181         case NF3DIR:
1182                 break;
1183         default:
1184                 BUG();
1185         }
1186 }
1187
1188 static void nfs3_xdr_enc_mknod3args(struct rpc_rqst *req,
1189                                     struct xdr_stream *xdr,
1190                                     const void *data)
1191 {
1192         const struct nfs3_mknodargs *args = data;
1193
1194         encode_diropargs3(xdr, args->fh, args->name, args->len);
1195         encode_mknoddata3(xdr, args);
1196 }
1197
1198 /*
1199  * 3.3.12  REMOVE3args
1200  *
1201  *      struct REMOVE3args {
1202  *              diropargs3  object;
1203  *      };
1204  */
1205 static void nfs3_xdr_enc_remove3args(struct rpc_rqst *req,
1206                                      struct xdr_stream *xdr,
1207                                      const void *data)
1208 {
1209         const struct nfs_removeargs *args = data;
1210
1211         encode_diropargs3(xdr, args->fh, args->name.name, args->name.len);
1212 }
1213
1214 /*
1215  * 3.3.14  RENAME3args
1216  *
1217  *      struct RENAME3args {
1218  *              diropargs3      from;
1219  *              diropargs3      to;
1220  *      };
1221  */
1222 static void nfs3_xdr_enc_rename3args(struct rpc_rqst *req,
1223                                      struct xdr_stream *xdr,
1224                                      const void *data)
1225 {
1226         const struct nfs_renameargs *args = data;
1227         const struct qstr *old = args->old_name;
1228         const struct qstr *new = args->new_name;
1229
1230         encode_diropargs3(xdr, args->old_dir, old->name, old->len);
1231         encode_diropargs3(xdr, args->new_dir, new->name, new->len);
1232 }
1233
1234 /*
1235  * 3.3.15  LINK3args
1236  *
1237  *      struct LINK3args {
1238  *              nfs_fh3         file;
1239  *              diropargs3      link;
1240  *      };
1241  */
1242 static void nfs3_xdr_enc_link3args(struct rpc_rqst *req,
1243                                    struct xdr_stream *xdr,
1244                                    const void *data)
1245 {
1246         const struct nfs3_linkargs *args = data;
1247
1248         encode_nfs_fh3(xdr, args->fromfh);
1249         encode_diropargs3(xdr, args->tofh, args->toname, args->tolen);
1250 }
1251
1252 /*
1253  * 3.3.16  READDIR3args
1254  *
1255  *      struct READDIR3args {
1256  *              nfs_fh3         dir;
1257  *              cookie3         cookie;
1258  *              cookieverf3     cookieverf;
1259  *              count3          count;
1260  *      };
1261  */
1262 static void encode_readdir3args(struct xdr_stream *xdr,
1263                                 const struct nfs3_readdirargs *args)
1264 {
1265         __be32 *p;
1266
1267         encode_nfs_fh3(xdr, args->fh);
1268
1269         p = xdr_reserve_space(xdr, 8 + NFS3_COOKIEVERFSIZE + 4);
1270         p = xdr_encode_cookie3(p, args->cookie);
1271         p = xdr_encode_cookieverf3(p, args->verf);
1272         *p = cpu_to_be32(args->count);
1273 }
1274
1275 static void nfs3_xdr_enc_readdir3args(struct rpc_rqst *req,
1276                                       struct xdr_stream *xdr,
1277                                       const void *data)
1278 {
1279         const struct nfs3_readdirargs *args = data;
1280
1281         encode_readdir3args(xdr, args);
1282         prepare_reply_buffer(req, args->pages, 0,
1283                                 args->count, NFS3_readdirres_sz);
1284 }
1285
1286 /*
1287  * 3.3.17  READDIRPLUS3args
1288  *
1289  *      struct READDIRPLUS3args {
1290  *              nfs_fh3         dir;
1291  *              cookie3         cookie;
1292  *              cookieverf3     cookieverf;
1293  *              count3          dircount;
1294  *              count3          maxcount;
1295  *      };
1296  */
1297 static void encode_readdirplus3args(struct xdr_stream *xdr,
1298                                     const struct nfs3_readdirargs *args)
1299 {
1300         __be32 *p;
1301
1302         encode_nfs_fh3(xdr, args->fh);
1303
1304         p = xdr_reserve_space(xdr, 8 + NFS3_COOKIEVERFSIZE + 4 + 4);
1305         p = xdr_encode_cookie3(p, args->cookie);
1306         p = xdr_encode_cookieverf3(p, args->verf);
1307
1308         /*
1309          * readdirplus: need dircount + buffer size.
1310          * We just make sure we make dircount big enough
1311          */
1312         *p++ = cpu_to_be32(args->count >> 3);
1313
1314         *p = cpu_to_be32(args->count);
1315 }
1316
1317 static void nfs3_xdr_enc_readdirplus3args(struct rpc_rqst *req,
1318                                           struct xdr_stream *xdr,
1319                                           const void *data)
1320 {
1321         const struct nfs3_readdirargs *args = data;
1322
1323         encode_readdirplus3args(xdr, args);
1324         prepare_reply_buffer(req, args->pages, 0,
1325                                 args->count, NFS3_readdirres_sz);
1326 }
1327
1328 /*
1329  * 3.3.21  COMMIT3args
1330  *
1331  *      struct COMMIT3args {
1332  *              nfs_fh3         file;
1333  *              offset3         offset;
1334  *              count3          count;
1335  *      };
1336  */
1337 static void encode_commit3args(struct xdr_stream *xdr,
1338                                const struct nfs_commitargs *args)
1339 {
1340         __be32 *p;
1341
1342         encode_nfs_fh3(xdr, args->fh);
1343
1344         p = xdr_reserve_space(xdr, 8 + 4);
1345         p = xdr_encode_hyper(p, args->offset);
1346         *p = cpu_to_be32(args->count);
1347 }
1348
1349 static void nfs3_xdr_enc_commit3args(struct rpc_rqst *req,
1350                                      struct xdr_stream *xdr,
1351                                      const void *data)
1352 {
1353         const struct nfs_commitargs *args = data;
1354
1355         encode_commit3args(xdr, args);
1356 }
1357
1358 #ifdef CONFIG_NFS_V3_ACL
1359
1360 static void nfs3_xdr_enc_getacl3args(struct rpc_rqst *req,
1361                                      struct xdr_stream *xdr,
1362                                      const void *data)
1363 {
1364         const struct nfs3_getaclargs *args = data;
1365
1366         encode_nfs_fh3(xdr, args->fh);
1367         encode_uint32(xdr, args->mask);
1368         if (args->mask & (NFS_ACL | NFS_DFACL))
1369                 prepare_reply_buffer(req, args->pages, 0,
1370                                         NFSACL_MAXPAGES << PAGE_SHIFT,
1371                                         ACL3_getaclres_sz);
1372 }
1373
1374 static void nfs3_xdr_enc_setacl3args(struct rpc_rqst *req,
1375                                      struct xdr_stream *xdr,
1376                                      const void *data)
1377 {
1378         const struct nfs3_setaclargs *args = data;
1379         unsigned int base;
1380         int error;
1381
1382         encode_nfs_fh3(xdr, NFS_FH(args->inode));
1383         encode_uint32(xdr, args->mask);
1384
1385         base = req->rq_slen;
1386         if (args->npages != 0)
1387                 xdr_write_pages(xdr, args->pages, 0, args->len);
1388         else
1389                 xdr_reserve_space(xdr, args->len);
1390
1391         error = nfsacl_encode(xdr->buf, base, args->inode,
1392                             (args->mask & NFS_ACL) ?
1393                             args->acl_access : NULL, 1, 0);
1394         /* FIXME: this is just broken */
1395         BUG_ON(error < 0);
1396         error = nfsacl_encode(xdr->buf, base + error, args->inode,
1397                             (args->mask & NFS_DFACL) ?
1398                             args->acl_default : NULL, 1,
1399                             NFS_ACL_DEFAULT);
1400         BUG_ON(error < 0);
1401 }
1402
1403 #endif  /* CONFIG_NFS_V3_ACL */
1404
1405 /*
1406  * NFSv3 XDR decode functions
1407  *
1408  * NFSv3 result types are defined in section 3.3 of RFC 1813:
1409  * "NFS Version 3 Protocol Specification".
1410  */
1411
1412 /*
1413  * 3.3.1  GETATTR3res
1414  *
1415  *      struct GETATTR3resok {
1416  *              fattr3          obj_attributes;
1417  *      };
1418  *
1419  *      union GETATTR3res switch (nfsstat3 status) {
1420  *      case NFS3_OK:
1421  *              GETATTR3resok  resok;
1422  *      default:
1423  *              void;
1424  *      };
1425  */
1426 static int nfs3_xdr_dec_getattr3res(struct rpc_rqst *req,
1427                                     struct xdr_stream *xdr,
1428                                     void *result)
1429 {
1430         enum nfs_stat status;
1431         int error;
1432
1433         error = decode_nfsstat3(xdr, &status);
1434         if (unlikely(error))
1435                 goto out;
1436         if (status != NFS3_OK)
1437                 goto out_default;
1438         error = decode_fattr3(xdr, result);
1439 out:
1440         return error;
1441 out_default:
1442         return nfs3_stat_to_errno(status);
1443 }
1444
1445 /*
1446  * 3.3.2  SETATTR3res
1447  *
1448  *      struct SETATTR3resok {
1449  *              wcc_data  obj_wcc;
1450  *      };
1451  *
1452  *      struct SETATTR3resfail {
1453  *              wcc_data  obj_wcc;
1454  *      };
1455  *
1456  *      union SETATTR3res switch (nfsstat3 status) {
1457  *      case NFS3_OK:
1458  *              SETATTR3resok   resok;
1459  *      default:
1460  *              SETATTR3resfail resfail;
1461  *      };
1462  */
1463 static int nfs3_xdr_dec_setattr3res(struct rpc_rqst *req,
1464                                     struct xdr_stream *xdr,
1465                                     void *result)
1466 {
1467         enum nfs_stat status;
1468         int error;
1469
1470         error = decode_nfsstat3(xdr, &status);
1471         if (unlikely(error))
1472                 goto out;
1473         error = decode_wcc_data(xdr, result);
1474         if (unlikely(error))
1475                 goto out;
1476         if (status != NFS3_OK)
1477                 goto out_status;
1478 out:
1479         return error;
1480 out_status:
1481         return nfs3_stat_to_errno(status);
1482 }
1483
1484 /*
1485  * 3.3.3  LOOKUP3res
1486  *
1487  *      struct LOOKUP3resok {
1488  *              nfs_fh3         object;
1489  *              post_op_attr    obj_attributes;
1490  *              post_op_attr    dir_attributes;
1491  *      };
1492  *
1493  *      struct LOOKUP3resfail {
1494  *              post_op_attr    dir_attributes;
1495  *      };
1496  *
1497  *      union LOOKUP3res switch (nfsstat3 status) {
1498  *      case NFS3_OK:
1499  *              LOOKUP3resok    resok;
1500  *      default:
1501  *              LOOKUP3resfail  resfail;
1502  *      };
1503  */
1504 static int nfs3_xdr_dec_lookup3res(struct rpc_rqst *req,
1505                                    struct xdr_stream *xdr,
1506                                    void *data)
1507 {
1508         struct nfs3_diropres *result = data;
1509         enum nfs_stat status;
1510         int error;
1511
1512         error = decode_nfsstat3(xdr, &status);
1513         if (unlikely(error))
1514                 goto out;
1515         if (status != NFS3_OK)
1516                 goto out_default;
1517         error = decode_nfs_fh3(xdr, result->fh);
1518         if (unlikely(error))
1519                 goto out;
1520         error = decode_post_op_attr(xdr, result->fattr);
1521         if (unlikely(error))
1522                 goto out;
1523         error = decode_post_op_attr(xdr, result->dir_attr);
1524 out:
1525         return error;
1526 out_default:
1527         error = decode_post_op_attr(xdr, result->dir_attr);
1528         if (unlikely(error))
1529                 goto out;
1530         return nfs3_stat_to_errno(status);
1531 }
1532
1533 /*
1534  * 3.3.4  ACCESS3res
1535  *
1536  *      struct ACCESS3resok {
1537  *              post_op_attr    obj_attributes;
1538  *              uint32          access;
1539  *      };
1540  *
1541  *      struct ACCESS3resfail {
1542  *              post_op_attr    obj_attributes;
1543  *      };
1544  *
1545  *      union ACCESS3res switch (nfsstat3 status) {
1546  *      case NFS3_OK:
1547  *              ACCESS3resok    resok;
1548  *      default:
1549  *              ACCESS3resfail  resfail;
1550  *      };
1551  */
1552 static int nfs3_xdr_dec_access3res(struct rpc_rqst *req,
1553                                    struct xdr_stream *xdr,
1554                                    void *data)
1555 {
1556         struct nfs3_accessres *result = data;
1557         enum nfs_stat status;
1558         int error;
1559
1560         error = decode_nfsstat3(xdr, &status);
1561         if (unlikely(error))
1562                 goto out;
1563         error = decode_post_op_attr(xdr, result->fattr);
1564         if (unlikely(error))
1565                 goto out;
1566         if (status != NFS3_OK)
1567                 goto out_default;
1568         error = decode_uint32(xdr, &result->access);
1569 out:
1570         return error;
1571 out_default:
1572         return nfs3_stat_to_errno(status);
1573 }
1574
1575 /*
1576  * 3.3.5  READLINK3res
1577  *
1578  *      struct READLINK3resok {
1579  *              post_op_attr    symlink_attributes;
1580  *              nfspath3        data;
1581  *      };
1582  *
1583  *      struct READLINK3resfail {
1584  *              post_op_attr    symlink_attributes;
1585  *      };
1586  *
1587  *      union READLINK3res switch (nfsstat3 status) {
1588  *      case NFS3_OK:
1589  *              READLINK3resok  resok;
1590  *      default:
1591  *              READLINK3resfail resfail;
1592  *      };
1593  */
1594 static int nfs3_xdr_dec_readlink3res(struct rpc_rqst *req,
1595                                      struct xdr_stream *xdr,
1596                                      void *result)
1597 {
1598         enum nfs_stat status;
1599         int error;
1600
1601         error = decode_nfsstat3(xdr, &status);
1602         if (unlikely(error))
1603                 goto out;
1604         error = decode_post_op_attr(xdr, result);
1605         if (unlikely(error))
1606                 goto out;
1607         if (status != NFS3_OK)
1608                 goto out_default;
1609         error = decode_nfspath3(xdr);
1610 out:
1611         return error;
1612 out_default:
1613         return nfs3_stat_to_errno(status);
1614 }
1615
1616 /*
1617  * 3.3.6  READ3res
1618  *
1619  *      struct READ3resok {
1620  *              post_op_attr    file_attributes;
1621  *              count3          count;
1622  *              bool            eof;
1623  *              opaque          data<>;
1624  *      };
1625  *
1626  *      struct READ3resfail {
1627  *              post_op_attr    file_attributes;
1628  *      };
1629  *
1630  *      union READ3res switch (nfsstat3 status) {
1631  *      case NFS3_OK:
1632  *              READ3resok      resok;
1633  *      default:
1634  *              READ3resfail    resfail;
1635  *      };
1636  */
1637 static int decode_read3resok(struct xdr_stream *xdr,
1638                              struct nfs_pgio_res *result)
1639 {
1640         u32 eof, count, ocount, recvd;
1641         __be32 *p;
1642
1643         p = xdr_inline_decode(xdr, 4 + 4 + 4);
1644         if (unlikely(p == NULL))
1645                 goto out_overflow;
1646         count = be32_to_cpup(p++);
1647         eof = be32_to_cpup(p++);
1648         ocount = be32_to_cpup(p++);
1649         if (unlikely(ocount != count))
1650                 goto out_mismatch;
1651         recvd = xdr_read_pages(xdr, count);
1652         if (unlikely(count > recvd))
1653                 goto out_cheating;
1654 out:
1655         result->eof = eof;
1656         result->count = count;
1657         return count;
1658 out_mismatch:
1659         dprintk("NFS: READ count doesn't match length of opaque: "
1660                 "count %u != ocount %u\n", count, ocount);
1661         return -EIO;
1662 out_cheating:
1663         dprintk("NFS: server cheating in read result: "
1664                 "count %u > recvd %u\n", count, recvd);
1665         count = recvd;
1666         eof = 0;
1667         goto out;
1668 out_overflow:
1669         print_overflow_msg(__func__, xdr);
1670         return -EIO;
1671 }
1672
1673 static int nfs3_xdr_dec_read3res(struct rpc_rqst *req, struct xdr_stream *xdr,
1674                                  void *data)
1675 {
1676         struct nfs_pgio_res *result = data;
1677         enum nfs_stat status;
1678         int error;
1679
1680         error = decode_nfsstat3(xdr, &status);
1681         if (unlikely(error))
1682                 goto out;
1683         error = decode_post_op_attr(xdr, result->fattr);
1684         if (unlikely(error))
1685                 goto out;
1686         result->op_status = status;
1687         if (status != NFS3_OK)
1688                 goto out_status;
1689         error = decode_read3resok(xdr, result);
1690 out:
1691         return error;
1692 out_status:
1693         return nfs3_stat_to_errno(status);
1694 }
1695
1696 /*
1697  * 3.3.7  WRITE3res
1698  *
1699  *      enum stable_how {
1700  *              UNSTABLE  = 0,
1701  *              DATA_SYNC = 1,
1702  *              FILE_SYNC = 2
1703  *      };
1704  *
1705  *      struct WRITE3resok {
1706  *              wcc_data        file_wcc;
1707  *              count3          count;
1708  *              stable_how      committed;
1709  *              writeverf3      verf;
1710  *      };
1711  *
1712  *      struct WRITE3resfail {
1713  *              wcc_data        file_wcc;
1714  *      };
1715  *
1716  *      union WRITE3res switch (nfsstat3 status) {
1717  *      case NFS3_OK:
1718  *              WRITE3resok     resok;
1719  *      default:
1720  *              WRITE3resfail   resfail;
1721  *      };
1722  */
1723 static int decode_write3resok(struct xdr_stream *xdr,
1724                               struct nfs_pgio_res *result)
1725 {
1726         __be32 *p;
1727
1728         p = xdr_inline_decode(xdr, 4 + 4);
1729         if (unlikely(p == NULL))
1730                 goto out_overflow;
1731         result->count = be32_to_cpup(p++);
1732         result->verf->committed = be32_to_cpup(p++);
1733         if (unlikely(result->verf->committed > NFS_FILE_SYNC))
1734                 goto out_badvalue;
1735         if (decode_writeverf3(xdr, &result->verf->verifier))
1736                 goto out_eio;
1737         return result->count;
1738 out_badvalue:
1739         dprintk("NFS: bad stable_how value: %u\n", result->verf->committed);
1740         return -EIO;
1741 out_overflow:
1742         print_overflow_msg(__func__, xdr);
1743 out_eio:
1744         return -EIO;
1745 }
1746
1747 static int nfs3_xdr_dec_write3res(struct rpc_rqst *req, struct xdr_stream *xdr,
1748                                   void *data)
1749 {
1750         struct nfs_pgio_res *result = data;
1751         enum nfs_stat status;
1752         int error;
1753
1754         error = decode_nfsstat3(xdr, &status);
1755         if (unlikely(error))
1756                 goto out;
1757         error = decode_wcc_data(xdr, result->fattr);
1758         if (unlikely(error))
1759                 goto out;
1760         result->op_status = status;
1761         if (status != NFS3_OK)
1762                 goto out_status;
1763         error = decode_write3resok(xdr, result);
1764 out:
1765         return error;
1766 out_status:
1767         return nfs3_stat_to_errno(status);
1768 }
1769
1770 /*
1771  * 3.3.8  CREATE3res
1772  *
1773  *      struct CREATE3resok {
1774  *              post_op_fh3     obj;
1775  *              post_op_attr    obj_attributes;
1776  *              wcc_data        dir_wcc;
1777  *      };
1778  *
1779  *      struct CREATE3resfail {
1780  *              wcc_data        dir_wcc;
1781  *      };
1782  *
1783  *      union CREATE3res switch (nfsstat3 status) {
1784  *      case NFS3_OK:
1785  *              CREATE3resok    resok;
1786  *      default:
1787  *              CREATE3resfail  resfail;
1788  *      };
1789  */
1790 static int decode_create3resok(struct xdr_stream *xdr,
1791                                struct nfs3_diropres *result)
1792 {
1793         int error;
1794
1795         error = decode_post_op_fh3(xdr, result->fh);
1796         if (unlikely(error))
1797                 goto out;
1798         error = decode_post_op_attr(xdr, result->fattr);
1799         if (unlikely(error))
1800                 goto out;
1801         /* The server isn't required to return a file handle.
1802          * If it didn't, force the client to perform a LOOKUP
1803          * to determine the correct file handle and attribute
1804          * values for the new object. */
1805         if (result->fh->size == 0)
1806                 result->fattr->valid = 0;
1807         error = decode_wcc_data(xdr, result->dir_attr);
1808 out:
1809         return error;
1810 }
1811
1812 static int nfs3_xdr_dec_create3res(struct rpc_rqst *req,
1813                                    struct xdr_stream *xdr,
1814                                    void *data)
1815 {
1816         struct nfs3_diropres *result = data;
1817         enum nfs_stat status;
1818         int error;
1819
1820         error = decode_nfsstat3(xdr, &status);
1821         if (unlikely(error))
1822                 goto out;
1823         if (status != NFS3_OK)
1824                 goto out_default;
1825         error = decode_create3resok(xdr, result);
1826 out:
1827         return error;
1828 out_default:
1829         error = decode_wcc_data(xdr, result->dir_attr);
1830         if (unlikely(error))
1831                 goto out;
1832         return nfs3_stat_to_errno(status);
1833 }
1834
1835 /*
1836  * 3.3.12  REMOVE3res
1837  *
1838  *      struct REMOVE3resok {
1839  *              wcc_data    dir_wcc;
1840  *      };
1841  *
1842  *      struct REMOVE3resfail {
1843  *              wcc_data    dir_wcc;
1844  *      };
1845  *
1846  *      union REMOVE3res switch (nfsstat3 status) {
1847  *      case NFS3_OK:
1848  *              REMOVE3resok   resok;
1849  *      default:
1850  *              REMOVE3resfail resfail;
1851  *      };
1852  */
1853 static int nfs3_xdr_dec_remove3res(struct rpc_rqst *req,
1854                                    struct xdr_stream *xdr,
1855                                    void *data)
1856 {
1857         struct nfs_removeres *result = data;
1858         enum nfs_stat status;
1859         int error;
1860
1861         error = decode_nfsstat3(xdr, &status);
1862         if (unlikely(error))
1863                 goto out;
1864         error = decode_wcc_data(xdr, result->dir_attr);
1865         if (unlikely(error))
1866                 goto out;
1867         if (status != NFS3_OK)
1868                 goto out_status;
1869 out:
1870         return error;
1871 out_status:
1872         return nfs3_stat_to_errno(status);
1873 }
1874
1875 /*
1876  * 3.3.14  RENAME3res
1877  *
1878  *      struct RENAME3resok {
1879  *              wcc_data        fromdir_wcc;
1880  *              wcc_data        todir_wcc;
1881  *      };
1882  *
1883  *      struct RENAME3resfail {
1884  *              wcc_data        fromdir_wcc;
1885  *              wcc_data        todir_wcc;
1886  *      };
1887  *
1888  *      union RENAME3res switch (nfsstat3 status) {
1889  *      case NFS3_OK:
1890  *              RENAME3resok   resok;
1891  *      default:
1892  *              RENAME3resfail resfail;
1893  *      };
1894  */
1895 static int nfs3_xdr_dec_rename3res(struct rpc_rqst *req,
1896                                    struct xdr_stream *xdr,
1897                                    void *data)
1898 {
1899         struct nfs_renameres *result = data;
1900         enum nfs_stat status;
1901         int error;
1902
1903         error = decode_nfsstat3(xdr, &status);
1904         if (unlikely(error))
1905                 goto out;
1906         error = decode_wcc_data(xdr, result->old_fattr);
1907         if (unlikely(error))
1908                 goto out;
1909         error = decode_wcc_data(xdr, result->new_fattr);
1910         if (unlikely(error))
1911                 goto out;
1912         if (status != NFS3_OK)
1913                 goto out_status;
1914 out:
1915         return error;
1916 out_status:
1917         return nfs3_stat_to_errno(status);
1918 }
1919
1920 /*
1921  * 3.3.15  LINK3res
1922  *
1923  *      struct LINK3resok {
1924  *              post_op_attr    file_attributes;
1925  *              wcc_data        linkdir_wcc;
1926  *      };
1927  *
1928  *      struct LINK3resfail {
1929  *              post_op_attr    file_attributes;
1930  *              wcc_data        linkdir_wcc;
1931  *      };
1932  *
1933  *      union LINK3res switch (nfsstat3 status) {
1934  *      case NFS3_OK:
1935  *              LINK3resok      resok;
1936  *      default:
1937  *              LINK3resfail    resfail;
1938  *      };
1939  */
1940 static int nfs3_xdr_dec_link3res(struct rpc_rqst *req, struct xdr_stream *xdr,
1941                                  void *data)
1942 {
1943         struct nfs3_linkres *result = data;
1944         enum nfs_stat status;
1945         int error;
1946
1947         error = decode_nfsstat3(xdr, &status);
1948         if (unlikely(error))
1949                 goto out;
1950         error = decode_post_op_attr(xdr, result->fattr);
1951         if (unlikely(error))
1952                 goto out;
1953         error = decode_wcc_data(xdr, result->dir_attr);
1954         if (unlikely(error))
1955                 goto out;
1956         if (status != NFS3_OK)
1957                 goto out_status;
1958 out:
1959         return error;
1960 out_status:
1961         return nfs3_stat_to_errno(status);
1962 }
1963
1964 /**
1965  * nfs3_decode_dirent - Decode a single NFSv3 directory entry stored in
1966  *                      the local page cache
1967  * @xdr: XDR stream where entry resides
1968  * @entry: buffer to fill in with entry data
1969  * @plus: boolean indicating whether this should be a readdirplus entry
1970  *
1971  * Returns zero if successful, otherwise a negative errno value is
1972  * returned.
1973  *
1974  * This function is not invoked during READDIR reply decoding, but
1975  * rather whenever an application invokes the getdents(2) system call
1976  * on a directory already in our cache.
1977  *
1978  * 3.3.16  entry3
1979  *
1980  *      struct entry3 {
1981  *              fileid3         fileid;
1982  *              filename3       name;
1983  *              cookie3         cookie;
1984  *              fhandle3        filehandle;
1985  *              post_op_attr3   attributes;
1986  *              entry3          *nextentry;
1987  *      };
1988  *
1989  * 3.3.17  entryplus3
1990  *      struct entryplus3 {
1991  *              fileid3         fileid;
1992  *              filename3       name;
1993  *              cookie3         cookie;
1994  *              post_op_attr    name_attributes;
1995  *              post_op_fh3     name_handle;
1996  *              entryplus3      *nextentry;
1997  *      };
1998  */
1999 int nfs3_decode_dirent(struct xdr_stream *xdr, struct nfs_entry *entry,
2000                        bool plus)
2001 {
2002         struct nfs_entry old = *entry;
2003         __be32 *p;
2004         int error;
2005         u64 new_cookie;
2006
2007         p = xdr_inline_decode(xdr, 4);
2008         if (unlikely(p == NULL))
2009                 goto out_overflow;
2010         if (*p == xdr_zero) {
2011                 p = xdr_inline_decode(xdr, 4);
2012                 if (unlikely(p == NULL))
2013                         goto out_overflow;
2014                 if (*p == xdr_zero)
2015                         return -EAGAIN;
2016                 entry->eof = 1;
2017                 return -EBADCOOKIE;
2018         }
2019
2020         error = decode_fileid3(xdr, &entry->ino);
2021         if (unlikely(error))
2022                 return error;
2023
2024         error = decode_inline_filename3(xdr, &entry->name, &entry->len);
2025         if (unlikely(error))
2026                 return error;
2027
2028         error = decode_cookie3(xdr, &new_cookie);
2029         if (unlikely(error))
2030                 return error;
2031
2032         entry->d_type = DT_UNKNOWN;
2033
2034         if (plus) {
2035                 entry->fattr->valid = 0;
2036                 error = decode_post_op_attr(xdr, entry->fattr);
2037                 if (unlikely(error))
2038                         return error;
2039                 if (entry->fattr->valid & NFS_ATTR_FATTR_V3)
2040                         entry->d_type = nfs_umode_to_dtype(entry->fattr->mode);
2041
2042                 if (entry->fattr->fileid != entry->ino) {
2043                         entry->fattr->mounted_on_fileid = entry->ino;
2044                         entry->fattr->valid |= NFS_ATTR_FATTR_MOUNTED_ON_FILEID;
2045                 }
2046
2047                 /* In fact, a post_op_fh3: */
2048                 p = xdr_inline_decode(xdr, 4);
2049                 if (unlikely(p == NULL))
2050                         goto out_overflow;
2051                 if (*p != xdr_zero) {
2052                         error = decode_nfs_fh3(xdr, entry->fh);
2053                         if (unlikely(error)) {
2054                                 if (error == -E2BIG)
2055                                         goto out_truncated;
2056                                 return error;
2057                         }
2058                 } else
2059                         zero_nfs_fh3(entry->fh);
2060         }
2061
2062         entry->prev_cookie = entry->cookie;
2063         entry->cookie = new_cookie;
2064
2065         return 0;
2066
2067 out_overflow:
2068         print_overflow_msg(__func__, xdr);
2069         return -EAGAIN;
2070 out_truncated:
2071         dprintk("NFS: directory entry contains invalid file handle\n");
2072         *entry = old;
2073         return -EAGAIN;
2074 }
2075
2076 /*
2077  * 3.3.16  READDIR3res
2078  *
2079  *      struct dirlist3 {
2080  *              entry3          *entries;
2081  *              bool            eof;
2082  *      };
2083  *
2084  *      struct READDIR3resok {
2085  *              post_op_attr    dir_attributes;
2086  *              cookieverf3     cookieverf;
2087  *              dirlist3        reply;
2088  *      };
2089  *
2090  *      struct READDIR3resfail {
2091  *              post_op_attr    dir_attributes;
2092  *      };
2093  *
2094  *      union READDIR3res switch (nfsstat3 status) {
2095  *      case NFS3_OK:
2096  *              READDIR3resok   resok;
2097  *      default:
2098  *              READDIR3resfail resfail;
2099  *      };
2100  *
2101  * Read the directory contents into the page cache, but otherwise
2102  * don't touch them.  The actual decoding is done by nfs3_decode_entry()
2103  * during subsequent nfs_readdir() calls.
2104  */
2105 static int decode_dirlist3(struct xdr_stream *xdr)
2106 {
2107         return xdr_read_pages(xdr, xdr->buf->page_len);
2108 }
2109
2110 static int decode_readdir3resok(struct xdr_stream *xdr,
2111                                 struct nfs3_readdirres *result)
2112 {
2113         int error;
2114
2115         error = decode_post_op_attr(xdr, result->dir_attr);
2116         if (unlikely(error))
2117                 goto out;
2118         /* XXX: do we need to check if result->verf != NULL ? */
2119         error = decode_cookieverf3(xdr, result->verf);
2120         if (unlikely(error))
2121                 goto out;
2122         error = decode_dirlist3(xdr);
2123 out:
2124         return error;
2125 }
2126
2127 static int nfs3_xdr_dec_readdir3res(struct rpc_rqst *req,
2128                                     struct xdr_stream *xdr,
2129                                     void *data)
2130 {
2131         struct nfs3_readdirres *result = data;
2132         enum nfs_stat status;
2133         int error;
2134
2135         error = decode_nfsstat3(xdr, &status);
2136         if (unlikely(error))
2137                 goto out;
2138         if (status != NFS3_OK)
2139                 goto out_default;
2140         error = decode_readdir3resok(xdr, result);
2141 out:
2142         return error;
2143 out_default:
2144         error = decode_post_op_attr(xdr, result->dir_attr);
2145         if (unlikely(error))
2146                 goto out;
2147         return nfs3_stat_to_errno(status);
2148 }
2149
2150 /*
2151  * 3.3.18  FSSTAT3res
2152  *
2153  *      struct FSSTAT3resok {
2154  *              post_op_attr    obj_attributes;
2155  *              size3           tbytes;
2156  *              size3           fbytes;
2157  *              size3           abytes;
2158  *              size3           tfiles;
2159  *              size3           ffiles;
2160  *              size3           afiles;
2161  *              uint32          invarsec;
2162  *      };
2163  *
2164  *      struct FSSTAT3resfail {
2165  *              post_op_attr    obj_attributes;
2166  *      };
2167  *
2168  *      union FSSTAT3res switch (nfsstat3 status) {
2169  *      case NFS3_OK:
2170  *              FSSTAT3resok    resok;
2171  *      default:
2172  *              FSSTAT3resfail  resfail;
2173  *      };
2174  */
2175 static int decode_fsstat3resok(struct xdr_stream *xdr,
2176                                struct nfs_fsstat *result)
2177 {
2178         __be32 *p;
2179
2180         p = xdr_inline_decode(xdr, 8 * 6 + 4);
2181         if (unlikely(p == NULL))
2182                 goto out_overflow;
2183         p = xdr_decode_size3(p, &result->tbytes);
2184         p = xdr_decode_size3(p, &result->fbytes);
2185         p = xdr_decode_size3(p, &result->abytes);
2186         p = xdr_decode_size3(p, &result->tfiles);
2187         p = xdr_decode_size3(p, &result->ffiles);
2188         xdr_decode_size3(p, &result->afiles);
2189         /* ignore invarsec */
2190         return 0;
2191 out_overflow:
2192         print_overflow_msg(__func__, xdr);
2193         return -EIO;
2194 }
2195
2196 static int nfs3_xdr_dec_fsstat3res(struct rpc_rqst *req,
2197                                    struct xdr_stream *xdr,
2198                                    void *data)
2199 {
2200         struct nfs_fsstat *result = data;
2201         enum nfs_stat status;
2202         int error;
2203
2204         error = decode_nfsstat3(xdr, &status);
2205         if (unlikely(error))
2206                 goto out;
2207         error = decode_post_op_attr(xdr, result->fattr);
2208         if (unlikely(error))
2209                 goto out;
2210         if (status != NFS3_OK)
2211                 goto out_status;
2212         error = decode_fsstat3resok(xdr, result);
2213 out:
2214         return error;
2215 out_status:
2216         return nfs3_stat_to_errno(status);
2217 }
2218
2219 /*
2220  * 3.3.19  FSINFO3res
2221  *
2222  *      struct FSINFO3resok {
2223  *              post_op_attr    obj_attributes;
2224  *              uint32          rtmax;
2225  *              uint32          rtpref;
2226  *              uint32          rtmult;
2227  *              uint32          wtmax;
2228  *              uint32          wtpref;
2229  *              uint32          wtmult;
2230  *              uint32          dtpref;
2231  *              size3           maxfilesize;
2232  *              nfstime3        time_delta;
2233  *              uint32          properties;
2234  *      };
2235  *
2236  *      struct FSINFO3resfail {
2237  *              post_op_attr    obj_attributes;
2238  *      };
2239  *
2240  *      union FSINFO3res switch (nfsstat3 status) {
2241  *      case NFS3_OK:
2242  *              FSINFO3resok    resok;
2243  *      default:
2244  *              FSINFO3resfail  resfail;
2245  *      };
2246  */
2247 static int decode_fsinfo3resok(struct xdr_stream *xdr,
2248                                struct nfs_fsinfo *result)
2249 {
2250         __be32 *p;
2251
2252         p = xdr_inline_decode(xdr, 4 * 7 + 8 + 8 + 4);
2253         if (unlikely(p == NULL))
2254                 goto out_overflow;
2255         result->rtmax  = be32_to_cpup(p++);
2256         result->rtpref = be32_to_cpup(p++);
2257         result->rtmult = be32_to_cpup(p++);
2258         result->wtmax  = be32_to_cpup(p++);
2259         result->wtpref = be32_to_cpup(p++);
2260         result->wtmult = be32_to_cpup(p++);
2261         result->dtpref = be32_to_cpup(p++);
2262         p = xdr_decode_size3(p, &result->maxfilesize);
2263         xdr_decode_nfstime3(p, &result->time_delta);
2264
2265         /* ignore properties */
2266         result->lease_time = 0;
2267         return 0;
2268 out_overflow:
2269         print_overflow_msg(__func__, xdr);
2270         return -EIO;
2271 }
2272
2273 static int nfs3_xdr_dec_fsinfo3res(struct rpc_rqst *req,
2274                                    struct xdr_stream *xdr,
2275                                    void *data)
2276 {
2277         struct nfs_fsinfo *result = data;
2278         enum nfs_stat status;
2279         int error;
2280
2281         error = decode_nfsstat3(xdr, &status);
2282         if (unlikely(error))
2283                 goto out;
2284         error = decode_post_op_attr(xdr, result->fattr);
2285         if (unlikely(error))
2286                 goto out;
2287         if (status != NFS3_OK)
2288                 goto out_status;
2289         error = decode_fsinfo3resok(xdr, result);
2290 out:
2291         return error;
2292 out_status:
2293         return nfs3_stat_to_errno(status);
2294 }
2295
2296 /*
2297  * 3.3.20  PATHCONF3res
2298  *
2299  *      struct PATHCONF3resok {
2300  *              post_op_attr    obj_attributes;
2301  *              uint32          linkmax;
2302  *              uint32          name_max;
2303  *              bool            no_trunc;
2304  *              bool            chown_restricted;
2305  *              bool            case_insensitive;
2306  *              bool            case_preserving;
2307  *      };
2308  *
2309  *      struct PATHCONF3resfail {
2310  *              post_op_attr    obj_attributes;
2311  *      };
2312  *
2313  *      union PATHCONF3res switch (nfsstat3 status) {
2314  *      case NFS3_OK:
2315  *              PATHCONF3resok  resok;
2316  *      default:
2317  *              PATHCONF3resfail resfail;
2318  *      };
2319  */
2320 static int decode_pathconf3resok(struct xdr_stream *xdr,
2321                                  struct nfs_pathconf *result)
2322 {
2323         __be32 *p;
2324
2325         p = xdr_inline_decode(xdr, 4 * 6);
2326         if (unlikely(p == NULL))
2327                 goto out_overflow;
2328         result->max_link = be32_to_cpup(p++);
2329         result->max_namelen = be32_to_cpup(p);
2330         /* ignore remaining fields */
2331         return 0;
2332 out_overflow:
2333         print_overflow_msg(__func__, xdr);
2334         return -EIO;
2335 }
2336
2337 static int nfs3_xdr_dec_pathconf3res(struct rpc_rqst *req,
2338                                      struct xdr_stream *xdr,
2339                                      void *data)
2340 {
2341         struct nfs_pathconf *result = data;
2342         enum nfs_stat status;
2343         int error;
2344
2345         error = decode_nfsstat3(xdr, &status);
2346         if (unlikely(error))
2347                 goto out;
2348         error = decode_post_op_attr(xdr, result->fattr);
2349         if (unlikely(error))
2350                 goto out;
2351         if (status != NFS3_OK)
2352                 goto out_status;
2353         error = decode_pathconf3resok(xdr, result);
2354 out:
2355         return error;
2356 out_status:
2357         return nfs3_stat_to_errno(status);
2358 }
2359
2360 /*
2361  * 3.3.21  COMMIT3res
2362  *
2363  *      struct COMMIT3resok {
2364  *              wcc_data        file_wcc;
2365  *              writeverf3      verf;
2366  *      };
2367  *
2368  *      struct COMMIT3resfail {
2369  *              wcc_data        file_wcc;
2370  *      };
2371  *
2372  *      union COMMIT3res switch (nfsstat3 status) {
2373  *      case NFS3_OK:
2374  *              COMMIT3resok    resok;
2375  *      default:
2376  *              COMMIT3resfail  resfail;
2377  *      };
2378  */
2379 static int nfs3_xdr_dec_commit3res(struct rpc_rqst *req,
2380                                    struct xdr_stream *xdr,
2381                                    void *data)
2382 {
2383         struct nfs_commitres *result = data;
2384         struct nfs_writeverf *verf = result->verf;
2385         enum nfs_stat status;
2386         int error;
2387
2388         error = decode_nfsstat3(xdr, &status);
2389         if (unlikely(error))
2390                 goto out;
2391         error = decode_wcc_data(xdr, result->fattr);
2392         if (unlikely(error))
2393                 goto out;
2394         result->op_status = status;
2395         if (status != NFS3_OK)
2396                 goto out_status;
2397         error = decode_writeverf3(xdr, &verf->verifier);
2398         if (!error)
2399                 verf->committed = NFS_FILE_SYNC;
2400 out:
2401         return error;
2402 out_status:
2403         return nfs3_stat_to_errno(status);
2404 }
2405
2406 #ifdef CONFIG_NFS_V3_ACL
2407
2408 static inline int decode_getacl3resok(struct xdr_stream *xdr,
2409                                       struct nfs3_getaclres *result)
2410 {
2411         struct posix_acl **acl;
2412         unsigned int *aclcnt;
2413         size_t hdrlen;
2414         int error;
2415
2416         error = decode_post_op_attr(xdr, result->fattr);
2417         if (unlikely(error))
2418                 goto out;
2419         error = decode_uint32(xdr, &result->mask);
2420         if (unlikely(error))
2421                 goto out;
2422         error = -EINVAL;
2423         if (result->mask & ~(NFS_ACL|NFS_ACLCNT|NFS_DFACL|NFS_DFACLCNT))
2424                 goto out;
2425
2426         hdrlen = xdr_stream_pos(xdr);
2427
2428         acl = NULL;
2429         if (result->mask & NFS_ACL)
2430                 acl = &result->acl_access;
2431         aclcnt = NULL;
2432         if (result->mask & NFS_ACLCNT)
2433                 aclcnt = &result->acl_access_count;
2434         error = nfsacl_decode(xdr->buf, hdrlen, aclcnt, acl);
2435         if (unlikely(error <= 0))
2436                 goto out;
2437
2438         acl = NULL;
2439         if (result->mask & NFS_DFACL)
2440                 acl = &result->acl_default;
2441         aclcnt = NULL;
2442         if (result->mask & NFS_DFACLCNT)
2443                 aclcnt = &result->acl_default_count;
2444         error = nfsacl_decode(xdr->buf, hdrlen + error, aclcnt, acl);
2445         if (unlikely(error <= 0))
2446                 return error;
2447         error = 0;
2448 out:
2449         return error;
2450 }
2451
2452 static int nfs3_xdr_dec_getacl3res(struct rpc_rqst *req,
2453                                    struct xdr_stream *xdr,
2454                                    void *result)
2455 {
2456         enum nfs_stat status;
2457         int error;
2458
2459         error = decode_nfsstat3(xdr, &status);
2460         if (unlikely(error))
2461                 goto out;
2462         if (status != NFS3_OK)
2463                 goto out_default;
2464         error = decode_getacl3resok(xdr, result);
2465 out:
2466         return error;
2467 out_default:
2468         return nfs3_stat_to_errno(status);
2469 }
2470
2471 static int nfs3_xdr_dec_setacl3res(struct rpc_rqst *req,
2472                                    struct xdr_stream *xdr,
2473                                    void *result)
2474 {
2475         enum nfs_stat status;
2476         int error;
2477
2478         error = decode_nfsstat3(xdr, &status);
2479         if (unlikely(error))
2480                 goto out;
2481         if (status != NFS3_OK)
2482                 goto out_default;
2483         error = decode_post_op_attr(xdr, result);
2484 out:
2485         return error;
2486 out_default:
2487         return nfs3_stat_to_errno(status);
2488 }
2489
2490 #endif  /* CONFIG_NFS_V3_ACL */
2491
2492
2493 /*
2494  * We need to translate between nfs status return values and
2495  * the local errno values which may not be the same.
2496  */
2497 static const struct {
2498         int stat;
2499         int errno;
2500 } nfs_errtbl[] = {
2501         { NFS_OK,               0               },
2502         { NFSERR_PERM,          -EPERM          },
2503         { NFSERR_NOENT,         -ENOENT         },
2504         { NFSERR_IO,            -errno_NFSERR_IO},
2505         { NFSERR_NXIO,          -ENXIO          },
2506 /*      { NFSERR_EAGAIN,        -EAGAIN         }, */
2507         { NFSERR_ACCES,         -EACCES         },
2508         { NFSERR_EXIST,         -EEXIST         },
2509         { NFSERR_XDEV,          -EXDEV          },
2510         { NFSERR_NODEV,         -ENODEV         },
2511         { NFSERR_NOTDIR,        -ENOTDIR        },
2512         { NFSERR_ISDIR,         -EISDIR         },
2513         { NFSERR_INVAL,         -EINVAL         },
2514         { NFSERR_FBIG,          -EFBIG          },
2515         { NFSERR_NOSPC,         -ENOSPC         },
2516         { NFSERR_ROFS,          -EROFS          },
2517         { NFSERR_MLINK,         -EMLINK         },
2518         { NFSERR_NAMETOOLONG,   -ENAMETOOLONG   },
2519         { NFSERR_NOTEMPTY,      -ENOTEMPTY      },
2520         { NFSERR_DQUOT,         -EDQUOT         },
2521         { NFSERR_STALE,         -ESTALE         },
2522         { NFSERR_REMOTE,        -EREMOTE        },
2523 #ifdef EWFLUSH
2524         { NFSERR_WFLUSH,        -EWFLUSH        },
2525 #endif
2526         { NFSERR_BADHANDLE,     -EBADHANDLE     },
2527         { NFSERR_NOT_SYNC,      -ENOTSYNC       },
2528         { NFSERR_BAD_COOKIE,    -EBADCOOKIE     },
2529         { NFSERR_NOTSUPP,       -ENOTSUPP       },
2530         { NFSERR_TOOSMALL,      -ETOOSMALL      },
2531         { NFSERR_SERVERFAULT,   -EREMOTEIO      },
2532         { NFSERR_BADTYPE,       -EBADTYPE       },
2533         { NFSERR_JUKEBOX,       -EJUKEBOX       },
2534         { -1,                   -EIO            }
2535 };
2536
2537 /**
2538  * nfs3_stat_to_errno - convert an NFS status code to a local errno
2539  * @status: NFS status code to convert
2540  *
2541  * Returns a local errno value, or -EIO if the NFS status code is
2542  * not recognized.  This function is used jointly by NFSv2 and NFSv3.
2543  */
2544 static int nfs3_stat_to_errno(enum nfs_stat status)
2545 {
2546         int i;
2547
2548         for (i = 0; nfs_errtbl[i].stat != -1; i++) {
2549                 if (nfs_errtbl[i].stat == (int)status)
2550                         return nfs_errtbl[i].errno;
2551         }
2552         dprintk("NFS: Unrecognized nfs status value: %u\n", status);
2553         return nfs_errtbl[i].errno;
2554 }
2555
2556
2557 #define PROC(proc, argtype, restype, timer)                             \
2558 [NFS3PROC_##proc] = {                                                   \
2559         .p_proc      = NFS3PROC_##proc,                                 \
2560         .p_encode    = nfs3_xdr_enc_##argtype##3args,                   \
2561         .p_decode    = nfs3_xdr_dec_##restype##3res,                    \
2562         .p_arglen    = NFS3_##argtype##args_sz,                         \
2563         .p_replen    = NFS3_##restype##res_sz,                          \
2564         .p_timer     = timer,                                           \
2565         .p_statidx   = NFS3PROC_##proc,                                 \
2566         .p_name      = #proc,                                           \
2567         }
2568
2569 const struct rpc_procinfo nfs3_procedures[] = {
2570         PROC(GETATTR,           getattr,        getattr,        1),
2571         PROC(SETATTR,           setattr,        setattr,        0),
2572         PROC(LOOKUP,            lookup,         lookup,         2),
2573         PROC(ACCESS,            access,         access,         1),
2574         PROC(READLINK,          readlink,       readlink,       3),
2575         PROC(READ,              read,           read,           3),
2576         PROC(WRITE,             write,          write,          4),
2577         PROC(CREATE,            create,         create,         0),
2578         PROC(MKDIR,             mkdir,          create,         0),
2579         PROC(SYMLINK,           symlink,        create,         0),
2580         PROC(MKNOD,             mknod,          create,         0),
2581         PROC(REMOVE,            remove,         remove,         0),
2582         PROC(RMDIR,             lookup,         setattr,        0),
2583         PROC(RENAME,            rename,         rename,         0),
2584         PROC(LINK,              link,           link,           0),
2585         PROC(READDIR,           readdir,        readdir,        3),
2586         PROC(READDIRPLUS,       readdirplus,    readdir,        3),
2587         PROC(FSSTAT,            getattr,        fsstat,         0),
2588         PROC(FSINFO,            getattr,        fsinfo,         0),
2589         PROC(PATHCONF,          getattr,        pathconf,       0),
2590         PROC(COMMIT,            commit,         commit,         5),
2591 };
2592
2593 static unsigned int nfs_version3_counts[ARRAY_SIZE(nfs3_procedures)];
2594 const struct rpc_version nfs_version3 = {
2595         .number                 = 3,
2596         .nrprocs                = ARRAY_SIZE(nfs3_procedures),
2597         .procs                  = nfs3_procedures,
2598         .counts                 = nfs_version3_counts,
2599 };
2600
2601 #ifdef CONFIG_NFS_V3_ACL
2602 static const struct rpc_procinfo nfs3_acl_procedures[] = {
2603         [ACLPROC3_GETACL] = {
2604                 .p_proc = ACLPROC3_GETACL,
2605                 .p_encode = nfs3_xdr_enc_getacl3args,
2606                 .p_decode = nfs3_xdr_dec_getacl3res,
2607                 .p_arglen = ACL3_getaclargs_sz,
2608                 .p_replen = ACL3_getaclres_sz,
2609                 .p_timer = 1,
2610                 .p_name = "GETACL",
2611         },
2612         [ACLPROC3_SETACL] = {
2613                 .p_proc = ACLPROC3_SETACL,
2614                 .p_encode = nfs3_xdr_enc_setacl3args,
2615                 .p_decode = nfs3_xdr_dec_setacl3res,
2616                 .p_arglen = ACL3_setaclargs_sz,
2617                 .p_replen = ACL3_setaclres_sz,
2618                 .p_timer = 0,
2619                 .p_name = "SETACL",
2620         },
2621 };
2622
2623 static unsigned int nfs3_acl_counts[ARRAY_SIZE(nfs3_acl_procedures)];
2624 const struct rpc_version nfsacl_version3 = {
2625         .number                 = 3,
2626         .nrprocs                = ARRAY_SIZE(nfs3_acl_procedures),
2627         .procs                  = nfs3_acl_procedures,
2628         .counts                 = nfs3_acl_counts,
2629 };
2630 #endif  /* CONFIG_NFS_V3_ACL */