vserver 1.9.3
[linux-2.6.git] / fs / nfs / nfs4xdr.c
1 /*
2  *  fs/nfs/nfs4xdr.c
3  *
4  *  Client-side XDR for NFSv4.
5  *
6  *  Copyright (c) 2002 The Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  Kendrick Smith <kmsmith@umich.edu>
10  *  Andy Adamson   <andros@umich.edu>
11  * 
12  *  Redistribution and use in source and binary forms, with or without
13  *  modification, are permitted provided that the following conditions
14  *  are met:
15  *
16  *  1. Redistributions of source code must retain the above copyright
17  *     notice, this list of conditions and the following disclaimer.
18  *  2. Redistributions in binary form must reproduce the above copyright
19  *     notice, this list of conditions and the following disclaimer in the
20  *     documentation and/or other materials provided with the distribution.
21  *  3. Neither the name of the University nor the names of its
22  *     contributors may be used to endorse or promote products derived
23  *     from this software without specific prior written permission.
24  *
25  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37
38 #include <linux/param.h>
39 #include <linux/time.h>
40 #include <linux/mm.h>
41 #include <linux/slab.h>
42 #include <linux/utsname.h>
43 #include <linux/errno.h>
44 #include <linux/string.h>
45 #include <linux/in.h>
46 #include <linux/pagemap.h>
47 #include <linux/proc_fs.h>
48 #include <linux/kdev_t.h>
49 #include <linux/sunrpc/clnt.h>
50 #include <linux/nfs.h>
51 #include <linux/nfs4.h>
52 #include <linux/nfs_fs.h>
53 #include <linux/nfs_idmap.h>
54
55 #define NFSDBG_FACILITY         NFSDBG_XDR
56
57 /* Mapping from NFS error code to "errno" error code. */
58 #define errno_NFSERR_IO         EIO
59
60 static int nfs_stat_to_errno(int);
61
62 /* NFSv4 COMPOUND tags are only wanted for debugging purposes */
63 #ifdef DEBUG
64 #define NFS4_MAXTAGLEN          20
65 #else
66 #define NFS4_MAXTAGLEN          0
67 #endif
68
69 /* lock,open owner id: 
70  * we currently use size 1 (u32) out of (NFS4_OPAQUE_LIMIT  >> 2)
71  */
72 #define owner_id_maxsz          (1 + 1)
73 #define compound_encode_hdr_maxsz       (3 + (NFS4_MAXTAGLEN >> 2))
74 #define compound_decode_hdr_maxsz       (3 + (NFS4_MAXTAGLEN >> 2))
75 #define op_encode_hdr_maxsz     (1)
76 #define op_decode_hdr_maxsz     (2)
77 #define encode_putfh_maxsz      (op_encode_hdr_maxsz + 1 + \
78                                 (NFS4_FHSIZE >> 2))
79 #define decode_putfh_maxsz      (op_decode_hdr_maxsz)
80 #define encode_putrootfh_maxsz  (op_encode_hdr_maxsz)
81 #define decode_putrootfh_maxsz  (op_decode_hdr_maxsz)
82 #define encode_getfh_maxsz      (op_encode_hdr_maxsz)
83 #define decode_getfh_maxsz      (op_decode_hdr_maxsz + 1 + \
84                                 ((3+NFS4_FHSIZE) >> 2))
85 #define encode_getattr_maxsz    (op_encode_hdr_maxsz + 3)
86 #define nfs4_name_maxsz         (1 + ((3 + NFS4_MAXNAMLEN) >> 2))
87 #define nfs4_path_maxsz         (1 + ((3 + NFS4_MAXPATHLEN) >> 2))
88 #define nfs4_fattr_bitmap_maxsz (36 + 2 * nfs4_name_maxsz)
89 #define decode_getattr_maxsz    (op_decode_hdr_maxsz + 3 + \
90                                 nfs4_fattr_bitmap_maxsz)
91 #define encode_savefh_maxsz     (op_encode_hdr_maxsz)
92 #define decode_savefh_maxsz     (op_decode_hdr_maxsz)
93 #define encode_fsinfo_maxsz     (op_encode_hdr_maxsz + 2)
94 #define decode_fsinfo_maxsz     (op_decode_hdr_maxsz + 11)
95 #define encode_renew_maxsz      (op_encode_hdr_maxsz + 3)
96 #define decode_renew_maxsz      (op_decode_hdr_maxsz)
97 #define encode_setclientid_maxsz \
98                                 (op_encode_hdr_maxsz + \
99                                 4 /*server->ip_addr*/ + \
100                                 1 /*Netid*/ + \
101                                 6 /*uaddr*/ + \
102                                 6 + (NFS4_VERIFIER_SIZE >> 2))
103 #define decode_setclientid_maxsz \
104                                 (op_decode_hdr_maxsz + \
105                                 2 + \
106                                 1024) /* large value for CLID_INUSE */
107 #define encode_setclientid_confirm_maxsz \
108                                 (op_encode_hdr_maxsz + \
109                                 3 + (NFS4_VERIFIER_SIZE >> 2))
110 #define decode_setclientid_confirm_maxsz \
111                                 (op_decode_hdr_maxsz)
112 #define encode_lookup_maxsz     (op_encode_hdr_maxsz + \
113                                 1 + ((3 + NFS4_FHSIZE) >> 2))
114 #define encode_remove_maxsz     (op_encode_hdr_maxsz + \
115                                 nfs4_name_maxsz)
116 #define encode_rename_maxsz     (op_encode_hdr_maxsz + \
117                                 2 * nfs4_name_maxsz)
118 #define decode_rename_maxsz     (op_decode_hdr_maxsz + 5 + 5)
119 #define encode_link_maxsz       (op_encode_hdr_maxsz + \
120                                 nfs4_name_maxsz)
121 #define decode_link_maxsz       (op_decode_hdr_maxsz + 5)
122 #define encode_symlink_maxsz    (op_encode_hdr_maxsz + \
123                                 1 + nfs4_name_maxsz + \
124                                 nfs4_path_maxsz + \
125                                 nfs4_fattr_bitmap_maxsz)
126 #define decode_symlink_maxsz    (op_decode_hdr_maxsz + 8)
127 #define encode_create_maxsz     (op_encode_hdr_maxsz + \
128                                 2 + nfs4_name_maxsz + \
129                                 nfs4_fattr_bitmap_maxsz)
130 #define decode_create_maxsz     (op_decode_hdr_maxsz + 8)
131 #define encode_delegreturn_maxsz (op_encode_hdr_maxsz + 4)
132 #define decode_delegreturn_maxsz (op_decode_hdr_maxsz)
133 #define NFS4_enc_compound_sz    (1024)  /* XXX: large enough? */
134 #define NFS4_dec_compound_sz    (1024)  /* XXX: large enough? */
135 #define NFS4_enc_read_sz        (compound_encode_hdr_maxsz + \
136                                 encode_putfh_maxsz + \
137                                 op_encode_hdr_maxsz + 7)
138 #define NFS4_dec_read_sz        (compound_decode_hdr_maxsz + \
139                                 decode_putfh_maxsz + \
140                                 op_decode_hdr_maxsz + 2)
141 #define NFS4_enc_readlink_sz    (compound_encode_hdr_maxsz + \
142                                 encode_putfh_maxsz + \
143                                 op_encode_hdr_maxsz)
144 #define NFS4_dec_readlink_sz    (compound_decode_hdr_maxsz + \
145                                 decode_putfh_maxsz + \
146                                 op_decode_hdr_maxsz)
147 #define NFS4_enc_readdir_sz     (compound_encode_hdr_maxsz + \
148                                 encode_putfh_maxsz + \
149                                 op_encode_hdr_maxsz + 9)
150 #define NFS4_dec_readdir_sz     (compound_decode_hdr_maxsz + \
151                                 decode_putfh_maxsz + \
152                                 op_decode_hdr_maxsz + 2)
153 #define NFS4_enc_write_sz       (compound_encode_hdr_maxsz + \
154                                 encode_putfh_maxsz + \
155                                 op_encode_hdr_maxsz + 8)
156 #define NFS4_dec_write_sz       (compound_decode_hdr_maxsz + \
157                                 decode_putfh_maxsz + \
158                                 op_decode_hdr_maxsz + 4)
159 #define NFS4_enc_commit_sz      (compound_encode_hdr_maxsz + \
160                                 encode_putfh_maxsz + \
161                                 op_encode_hdr_maxsz + 3)
162 #define NFS4_dec_commit_sz      (compound_decode_hdr_maxsz + \
163                                 decode_putfh_maxsz + \
164                                 op_decode_hdr_maxsz + 2)
165 #define NFS4_enc_open_sz        (compound_encode_hdr_maxsz + \
166                                 encode_putfh_maxsz + \
167                                 op_encode_hdr_maxsz + \
168                                 13 + 3 + 2 + 64 + \
169                                 encode_getattr_maxsz + \
170                                 encode_getfh_maxsz)
171 #define NFS4_dec_open_sz        (compound_decode_hdr_maxsz + \
172                                 decode_putfh_maxsz + \
173                                 op_decode_hdr_maxsz + 4 + 5 + 2 + 3 + \
174                                 decode_getattr_maxsz + \
175                                 decode_getfh_maxsz)
176 #define NFS4_enc_open_confirm_sz      \
177                                 (compound_encode_hdr_maxsz + \
178                                 encode_putfh_maxsz + \
179                                 op_encode_hdr_maxsz + 5)
180 #define NFS4_dec_open_confirm_sz        (compound_decode_hdr_maxsz + \
181                                         decode_putfh_maxsz + \
182                                         op_decode_hdr_maxsz + 4)
183 #define NFS4_enc_open_noattr_sz (compound_encode_hdr_maxsz + \
184                                         encode_putfh_maxsz + \
185                                         op_encode_hdr_maxsz + \
186                                         11)
187 #define NFS4_dec_open_noattr_sz (compound_decode_hdr_maxsz + \
188                                         decode_putfh_maxsz + \
189                                         op_decode_hdr_maxsz + \
190                                         4 + 5 + 2 + 3)
191 #define NFS4_enc_open_downgrade_sz \
192                                 (compound_encode_hdr_maxsz + \
193                                 encode_putfh_maxsz + \
194                                 op_encode_hdr_maxsz + 7)
195 #define NFS4_dec_open_downgrade_sz \
196                                 (compound_decode_hdr_maxsz + \
197                                 decode_putfh_maxsz + \
198                                 op_decode_hdr_maxsz + 4)
199 #define NFS4_enc_close_sz       (compound_encode_hdr_maxsz + \
200                                 encode_putfh_maxsz + \
201                                 op_encode_hdr_maxsz + 5)
202 #define NFS4_dec_close_sz       (compound_decode_hdr_maxsz + \
203                                 decode_putfh_maxsz + \
204                                 op_decode_hdr_maxsz + 4)
205 #define NFS4_enc_setattr_sz     (compound_encode_hdr_maxsz + \
206                                 encode_putfh_maxsz + \
207                                 op_encode_hdr_maxsz + 4 + \
208                                 nfs4_fattr_bitmap_maxsz + \
209                                 encode_getattr_maxsz)
210 #define NFS4_dec_setattr_sz     (compound_decode_hdr_maxsz + \
211                                 decode_putfh_maxsz + \
212                                 op_decode_hdr_maxsz + 3)
213 #define NFS4_enc_fsinfo_sz      (compound_encode_hdr_maxsz + \
214                                 encode_putfh_maxsz + \
215                                 encode_fsinfo_maxsz)
216 #define NFS4_dec_fsinfo_sz      (compound_decode_hdr_maxsz + \
217                                 decode_putfh_maxsz + \
218                                 decode_fsinfo_maxsz)
219 #define NFS4_enc_renew_sz       (compound_encode_hdr_maxsz + \
220                                 encode_renew_maxsz)
221 #define NFS4_dec_renew_sz       (compound_decode_hdr_maxsz + \
222                                 decode_renew_maxsz)
223 #define NFS4_enc_setclientid_sz (compound_encode_hdr_maxsz + \
224                                 encode_setclientid_maxsz)
225 #define NFS4_dec_setclientid_sz (compound_decode_hdr_maxsz + \
226                                 decode_setclientid_maxsz)
227 #define NFS4_enc_setclientid_confirm_sz \
228                                 (compound_encode_hdr_maxsz + \
229                                 encode_setclientid_confirm_maxsz + \
230                                 encode_putrootfh_maxsz + \
231                                 encode_fsinfo_maxsz)
232 #define NFS4_dec_setclientid_confirm_sz \
233                                 (compound_decode_hdr_maxsz + \
234                                 decode_setclientid_confirm_maxsz + \
235                                 decode_putrootfh_maxsz + \
236                                 decode_fsinfo_maxsz)
237 #define NFS4_enc_lock_sz        (compound_encode_hdr_maxsz + \
238                                 encode_putfh_maxsz + \
239                                 encode_getattr_maxsz + \
240                                 op_encode_hdr_maxsz + \
241                                 1 + 1 + 2 + 2 + \
242                                 1 + 4 + 1 + 2 + \
243                                 owner_id_maxsz)
244 #define NFS4_dec_lock_sz        (compound_decode_hdr_maxsz + \
245                                 decode_putfh_maxsz + \
246                                 decode_getattr_maxsz + \
247                                 op_decode_hdr_maxsz + \
248                                 2 + 2 + 1 + 2 + \
249                                 owner_id_maxsz)
250 #define NFS4_enc_lockt_sz       (compound_encode_hdr_maxsz + \
251                                 encode_putfh_maxsz + \
252                                 encode_getattr_maxsz + \
253                                 op_encode_hdr_maxsz + \
254                                 1 + 2 + 2 + 2 + \
255                                 owner_id_maxsz)
256 #define NFS4_dec_lockt_sz       (NFS4_dec_lock_sz)
257 #define NFS4_enc_locku_sz       (compound_encode_hdr_maxsz + \
258                                 encode_putfh_maxsz + \
259                                 encode_getattr_maxsz + \
260                                 op_encode_hdr_maxsz + \
261                                 1 + 1 + 4 + 2 + 2)
262 #define NFS4_dec_locku_sz       (compound_decode_hdr_maxsz + \
263                                 decode_putfh_maxsz + \
264                                 decode_getattr_maxsz + \
265                                 op_decode_hdr_maxsz + 4)
266 #define NFS4_enc_access_sz      (compound_encode_hdr_maxsz + \
267                                 encode_putfh_maxsz + \
268                                 op_encode_hdr_maxsz + 1)
269 #define NFS4_dec_access_sz      (compound_decode_hdr_maxsz + \
270                                 decode_putfh_maxsz + \
271                                 op_decode_hdr_maxsz + 2)
272 #define NFS4_enc_getattr_sz     (compound_encode_hdr_maxsz + \
273                                 encode_putfh_maxsz + \
274                                 encode_getattr_maxsz)
275 #define NFS4_dec_getattr_sz     (compound_decode_hdr_maxsz + \
276                                 decode_putfh_maxsz + \
277                                 decode_getattr_maxsz)
278 #define NFS4_enc_lookup_sz      (compound_encode_hdr_maxsz + \
279                                 encode_putfh_maxsz + \
280                                 encode_lookup_maxsz + \
281                                 encode_getattr_maxsz + \
282                                 encode_getfh_maxsz)
283 #define NFS4_dec_lookup_sz      (compound_decode_hdr_maxsz + \
284                                 decode_putfh_maxsz + \
285                                 op_decode_hdr_maxsz + \
286                                 decode_getattr_maxsz + \
287                                 decode_getfh_maxsz)
288 #define NFS4_enc_lookup_root_sz (compound_encode_hdr_maxsz + \
289                                 encode_putrootfh_maxsz + \
290                                 encode_getattr_maxsz + \
291                                 encode_getfh_maxsz)
292 #define NFS4_dec_lookup_root_sz (compound_decode_hdr_maxsz + \
293                                 decode_putrootfh_maxsz + \
294                                 decode_getattr_maxsz + \
295                                 decode_getfh_maxsz)
296 #define NFS4_enc_remove_sz      (compound_encode_hdr_maxsz + \
297                                 encode_putfh_maxsz + \
298                                 encode_remove_maxsz)
299 #define NFS4_dec_remove_sz      (compound_decode_hdr_maxsz + \
300                                 decode_putfh_maxsz + \
301                                 op_decode_hdr_maxsz + 5)
302 #define NFS4_enc_rename_sz      (compound_encode_hdr_maxsz + \
303                                 encode_putfh_maxsz + \
304                                 encode_savefh_maxsz + \
305                                 encode_putfh_maxsz + \
306                                 encode_rename_maxsz)
307 #define NFS4_dec_rename_sz      (compound_decode_hdr_maxsz + \
308                                 decode_putfh_maxsz + \
309                                 decode_savefh_maxsz + \
310                                 decode_putfh_maxsz + \
311                                 decode_rename_maxsz)
312 #define NFS4_enc_link_sz        (compound_encode_hdr_maxsz + \
313                                 encode_putfh_maxsz + \
314                                 encode_savefh_maxsz + \
315                                 encode_putfh_maxsz + \
316                                 encode_link_maxsz)
317 #define NFS4_dec_link_sz        (compound_decode_hdr_maxsz + \
318                                 decode_putfh_maxsz + \
319                                 decode_savefh_maxsz + \
320                                 decode_putfh_maxsz + \
321                                 decode_link_maxsz)
322 #define NFS4_enc_symlink_sz     (compound_encode_hdr_maxsz + \
323                                 encode_putfh_maxsz + \
324                                 encode_symlink_maxsz + \
325                                 encode_getattr_maxsz + \
326                                 encode_getfh_maxsz)
327 #define NFS4_dec_symlink_sz     (compound_decode_hdr_maxsz + \
328                                 decode_putfh_maxsz + \
329                                 decode_symlink_maxsz + \
330                                 decode_getattr_maxsz + \
331                                 decode_getfh_maxsz)
332 #define NFS4_enc_create_sz      (compound_encode_hdr_maxsz + \
333                                 encode_putfh_maxsz + \
334                                 encode_create_maxsz + \
335                                 encode_getattr_maxsz + \
336                                 encode_getfh_maxsz)
337 #define NFS4_dec_create_sz      (compound_decode_hdr_maxsz + \
338                                 decode_putfh_maxsz + \
339                                 decode_create_maxsz + \
340                                 decode_getattr_maxsz + \
341                                 decode_getfh_maxsz)
342 #define NFS4_enc_pathconf_sz    (compound_encode_hdr_maxsz + \
343                                 encode_putfh_maxsz + \
344                                 encode_getattr_maxsz)
345 #define NFS4_dec_pathconf_sz    (compound_decode_hdr_maxsz + \
346                                 decode_putfh_maxsz + \
347                                 decode_getattr_maxsz)
348 #define NFS4_enc_statfs_sz      (compound_encode_hdr_maxsz + \
349                                 encode_putfh_maxsz + \
350                                 encode_getattr_maxsz)
351 #define NFS4_dec_statfs_sz      (compound_decode_hdr_maxsz + \
352                                 decode_putfh_maxsz + \
353                                 op_decode_hdr_maxsz + 12)
354 #define NFS4_enc_server_caps_sz (compound_encode_hdr_maxsz + \
355                                 encode_getattr_maxsz)
356 #define NFS4_dec_server_caps_sz (compound_decode_hdr_maxsz + \
357                                 decode_getattr_maxsz)
358 #define NFS4_enc_delegreturn_sz (compound_encode_hdr_maxsz + \
359                                 encode_putfh_maxsz + \
360                                 encode_delegreturn_maxsz)
361 #define NFS4_dec_delegreturn_sz (compound_decode_hdr_maxsz + \
362                                 decode_delegreturn_maxsz)
363
364 static struct {
365         unsigned int    mode;
366         unsigned int    nfs2type;
367 } nfs_type2fmt[] = {
368         { 0,            NFNON        },
369         { S_IFREG,      NFREG        },
370         { S_IFDIR,      NFDIR        },
371         { S_IFBLK,      NFBLK        },
372         { S_IFCHR,      NFCHR        },
373         { S_IFLNK,      NFLNK        },
374         { S_IFSOCK,     NFSOCK       },
375         { S_IFIFO,      NFFIFO       },
376         { 0,            NFNON        },
377         { 0,            NFNON        },
378 };
379
380 struct compound_hdr {
381         int32_t         status;
382         uint32_t        nops;
383         uint32_t        taglen;
384         char *          tag;
385 };
386
387 /*
388  * START OF "GENERIC" ENCODE ROUTINES.
389  *   These may look a little ugly since they are imported from a "generic"
390  * set of XDR encode/decode routines which are intended to be shared by
391  * all of our NFSv4 implementations (OpenBSD, MacOS X...).
392  *
393  * If the pain of reading these is too great, it should be a straightforward
394  * task to translate them into Linux-specific versions which are more
395  * consistent with the style used in NFSv2/v3...
396  */
397 #define WRITE32(n)               *p++ = htonl(n)
398 #define WRITE64(n)               do {                           \
399         *p++ = htonl((uint32_t)((n) >> 32));                            \
400         *p++ = htonl((uint32_t)(n));                                    \
401 } while (0)
402 #define WRITEMEM(ptr,nbytes)     do {                           \
403         p = xdr_encode_opaque_fixed(p, ptr, nbytes);            \
404 } while (0)
405
406 #define RESERVE_SPACE(nbytes)   do {                            \
407         p = xdr_reserve_space(xdr, nbytes);                     \
408         if (!p) printk("RESERVE_SPACE(%d) failed in function %s\n", (int) (nbytes), __FUNCTION__); \
409         BUG_ON(!p);                                             \
410 } while (0)
411
412 static void encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
413 {
414         uint32_t *p;
415
416         p = xdr_reserve_space(xdr, 4 + len);
417         BUG_ON(p == NULL);
418         xdr_encode_opaque(p, str, len);
419 }
420
421 static int encode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
422 {
423         uint32_t *p;
424
425         dprintk("encode_compound: tag=%.*s\n", (int)hdr->taglen, hdr->tag);
426         BUG_ON(hdr->taglen > NFS4_MAXTAGLEN);
427         RESERVE_SPACE(12+(XDR_QUADLEN(hdr->taglen)<<2));
428         WRITE32(hdr->taglen);
429         WRITEMEM(hdr->tag, hdr->taglen);
430         WRITE32(NFS4_MINOR_VERSION);
431         WRITE32(hdr->nops);
432         return 0;
433 }
434
435 static void encode_nfs4_verifier(struct xdr_stream *xdr, const nfs4_verifier *verf)
436 {
437         uint32_t *p;
438
439         p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
440         BUG_ON(p == NULL);
441         xdr_encode_opaque_fixed(p, verf->data, NFS4_VERIFIER_SIZE);
442 }
443
444 static int encode_attrs(struct xdr_stream *xdr, const struct iattr *iap, const struct nfs_server *server)
445 {
446         char owner_name[IDMAP_NAMESZ];
447         char owner_group[IDMAP_NAMESZ];
448         int owner_namelen = 0;
449         int owner_grouplen = 0;
450         uint32_t *p;
451         uint32_t *q;
452         int len;
453         uint32_t bmval0 = 0;
454         uint32_t bmval1 = 0;
455         int status;
456
457         /*
458          * We reserve enough space to write the entire attribute buffer at once.
459          * In the worst-case, this would be
460          *   12(bitmap) + 4(attrlen) + 8(size) + 4(mode) + 4(atime) + 4(mtime)
461          *          = 36 bytes, plus any contribution from variable-length fields
462          *            such as owner/group/acl's.
463          */
464         len = 16;
465
466         /* Sigh */
467         if (iap->ia_valid & ATTR_SIZE)
468                 len += 8;
469         if (iap->ia_valid & ATTR_MODE)
470                 len += 4;
471         if (iap->ia_valid & ATTR_UID) {
472                 owner_namelen = nfs_map_uid_to_name(server->nfs4_state, iap->ia_uid, owner_name);
473                 if (owner_namelen < 0) {
474                         printk(KERN_WARNING "nfs: couldn't resolve uid %d to string\n",
475                                iap->ia_uid);
476                         /* XXX */
477                         strcpy(owner_name, "nobody");
478                         owner_namelen = sizeof("nobody") - 1;
479                         /* goto out; */
480                 }
481                 len += 4 + (XDR_QUADLEN(owner_namelen) << 2);
482         }
483         if (iap->ia_valid & ATTR_GID) {
484                 owner_grouplen = nfs_map_gid_to_group(server->nfs4_state, iap->ia_gid, owner_group);
485                 if (owner_grouplen < 0) {
486                         printk(KERN_WARNING "nfs4: couldn't resolve gid %d to string\n",
487                                iap->ia_gid);
488                         strcpy(owner_group, "nobody");
489                         owner_grouplen = sizeof("nobody") - 1;
490                         /* goto out; */
491                 }
492                 len += 4 + (XDR_QUADLEN(owner_grouplen) << 2);
493         }
494         if (iap->ia_valid & ATTR_ATIME_SET)
495                 len += 16;
496         else if (iap->ia_valid & ATTR_ATIME)
497                 len += 4;
498         if (iap->ia_valid & ATTR_MTIME_SET)
499                 len += 16;
500         else if (iap->ia_valid & ATTR_MTIME)
501                 len += 4;
502         RESERVE_SPACE(len);
503
504         /*
505          * We write the bitmap length now, but leave the bitmap and the attribute
506          * buffer length to be backfilled at the end of this routine.
507          */
508         WRITE32(2);
509         q = p;
510         p += 3;
511
512         if (iap->ia_valid & ATTR_SIZE) {
513                 bmval0 |= FATTR4_WORD0_SIZE;
514                 WRITE64(iap->ia_size);
515         }
516         if (iap->ia_valid & ATTR_MODE) {
517                 bmval1 |= FATTR4_WORD1_MODE;
518                 WRITE32(iap->ia_mode);
519         }
520         if (iap->ia_valid & ATTR_UID) {
521                 bmval1 |= FATTR4_WORD1_OWNER;
522                 WRITE32(owner_namelen);
523                 WRITEMEM(owner_name, owner_namelen);
524         }
525         if (iap->ia_valid & ATTR_GID) {
526                 bmval1 |= FATTR4_WORD1_OWNER_GROUP;
527                 WRITE32(owner_grouplen);
528                 WRITEMEM(owner_group, owner_grouplen);
529         }
530         if (iap->ia_valid & ATTR_ATIME_SET) {
531                 bmval1 |= FATTR4_WORD1_TIME_ACCESS_SET;
532                 WRITE32(NFS4_SET_TO_CLIENT_TIME);
533                 WRITE32(0);
534                 WRITE32(iap->ia_mtime.tv_sec);
535                 WRITE32(iap->ia_mtime.tv_nsec);
536         }
537         else if (iap->ia_valid & ATTR_ATIME) {
538                 bmval1 |= FATTR4_WORD1_TIME_ACCESS_SET;
539                 WRITE32(NFS4_SET_TO_SERVER_TIME);
540         }
541         if (iap->ia_valid & ATTR_MTIME_SET) {
542                 bmval1 |= FATTR4_WORD1_TIME_MODIFY_SET;
543                 WRITE32(NFS4_SET_TO_CLIENT_TIME);
544                 WRITE32(0);
545                 WRITE32(iap->ia_mtime.tv_sec);
546                 WRITE32(iap->ia_mtime.tv_nsec);
547         }
548         else if (iap->ia_valid & ATTR_MTIME) {
549                 bmval1 |= FATTR4_WORD1_TIME_MODIFY_SET;
550                 WRITE32(NFS4_SET_TO_SERVER_TIME);
551         }
552         
553         /*
554          * Now we backfill the bitmap and the attribute buffer length.
555          */
556         if (len != ((char *)p - (char *)q) + 4) {
557                 printk ("encode_attr: Attr length calculation error! %u != %Zu\n",
558                                 len, ((char *)p - (char *)q) + 4);
559                 BUG();
560         }
561         len = (char *)p - (char *)q - 12;
562         *q++ = htonl(bmval0);
563         *q++ = htonl(bmval1);
564         *q++ = htonl(len);
565
566         status = 0;
567 /* out: */
568         return status;
569 }
570
571 static int encode_access(struct xdr_stream *xdr, u32 access)
572 {
573         uint32_t *p;
574
575         RESERVE_SPACE(8);
576         WRITE32(OP_ACCESS);
577         WRITE32(access);
578         
579         return 0;
580 }
581
582 static int encode_close(struct xdr_stream *xdr, const struct nfs_closeargs *arg)
583 {
584         uint32_t *p;
585
586         RESERVE_SPACE(8+sizeof(arg->stateid.data));
587         WRITE32(OP_CLOSE);
588         WRITE32(arg->seqid);
589         WRITEMEM(arg->stateid.data, sizeof(arg->stateid.data));
590         
591         return 0;
592 }
593
594 static int encode_commit(struct xdr_stream *xdr, const struct nfs_writeargs *args)
595 {
596         uint32_t *p;
597         
598         RESERVE_SPACE(16);
599         WRITE32(OP_COMMIT);
600         WRITE64(args->offset);
601         WRITE32(args->count);
602
603         return 0;
604 }
605
606 static int encode_create(struct xdr_stream *xdr, const struct nfs4_create_arg *create)
607 {
608         uint32_t *p;
609         
610         RESERVE_SPACE(8);
611         WRITE32(OP_CREATE);
612         WRITE32(create->ftype);
613
614         switch (create->ftype) {
615         case NF4LNK:
616                 RESERVE_SPACE(4 + create->u.symlink->len);
617                 WRITE32(create->u.symlink->len);
618                 WRITEMEM(create->u.symlink->name, create->u.symlink->len);
619                 break;
620
621         case NF4BLK: case NF4CHR:
622                 RESERVE_SPACE(8);
623                 WRITE32(create->u.device.specdata1);
624                 WRITE32(create->u.device.specdata2);
625                 break;
626
627         default:
628                 break;
629         }
630
631         RESERVE_SPACE(4 + create->name->len);
632         WRITE32(create->name->len);
633         WRITEMEM(create->name->name, create->name->len);
634
635         return encode_attrs(xdr, create->attrs, create->server);
636 }
637
638 static int encode_getattr_one(struct xdr_stream *xdr, uint32_t bitmap)
639 {
640         uint32_t *p;
641
642         RESERVE_SPACE(12);
643         WRITE32(OP_GETATTR);
644         WRITE32(1);
645         WRITE32(bitmap);
646         return 0;
647 }
648
649 static int encode_getattr_two(struct xdr_stream *xdr, uint32_t bm0, uint32_t bm1)
650 {
651         uint32_t *p;
652
653         RESERVE_SPACE(16);
654         WRITE32(OP_GETATTR);
655         WRITE32(2);
656         WRITE32(bm0);
657         WRITE32(bm1);
658         return 0;
659 }
660
661 static int encode_getfattr(struct xdr_stream *xdr, const u32* bitmask)
662 {
663         extern u32 nfs4_fattr_bitmap[];
664
665         return encode_getattr_two(xdr,
666                         bitmask[0] & nfs4_fattr_bitmap[0],
667                         bitmask[1] & nfs4_fattr_bitmap[1]);
668 }
669
670 static int encode_fsinfo(struct xdr_stream *xdr, const u32* bitmask)
671 {
672         extern u32 nfs4_fsinfo_bitmap[];
673
674         return encode_getattr_two(xdr, bitmask[0] & nfs4_fsinfo_bitmap[0],
675                         bitmask[1] & nfs4_fsinfo_bitmap[1]);
676 }
677
678 static int encode_getfh(struct xdr_stream *xdr)
679 {
680         uint32_t *p;
681
682         RESERVE_SPACE(4);
683         WRITE32(OP_GETFH);
684
685         return 0;
686 }
687
688 static int encode_link(struct xdr_stream *xdr, const struct qstr *name)
689 {
690         uint32_t *p;
691
692         RESERVE_SPACE(8 + name->len);
693         WRITE32(OP_LINK);
694         WRITE32(name->len);
695         WRITEMEM(name->name, name->len);
696         
697         return 0;
698 }
699
700 /*
701  * opcode,type,reclaim,offset,length,new_lock_owner = 32
702  * open_seqid,open_stateid,lock_seqid,lock_owner.clientid, lock_owner.id = 40
703  */
704 static int encode_lock(struct xdr_stream *xdr, const struct nfs_lockargs *arg)
705 {
706         uint32_t *p;
707         struct nfs_lock_opargs *opargs = arg->u.lock;
708
709         RESERVE_SPACE(32);
710         WRITE32(OP_LOCK);
711         WRITE32(arg->type); 
712         WRITE32(opargs->reclaim);
713         WRITE64(arg->offset);
714         WRITE64(arg->length);
715         WRITE32(opargs->new_lock_owner);
716         if (opargs->new_lock_owner){
717                 struct nfs_open_to_lock *ol = opargs->u.open_lock;
718
719                 RESERVE_SPACE(40);
720                 WRITE32(ol->open_seqid);
721                 WRITEMEM(&ol->open_stateid, sizeof(ol->open_stateid));
722                 WRITE32(ol->lock_seqid);
723                 WRITE64(ol->lock_owner.clientid);
724                 WRITE32(4);
725                 WRITE32(ol->lock_owner.id);
726         }
727         else {
728                 struct nfs_exist_lock *el = opargs->u.exist_lock;
729
730                 RESERVE_SPACE(20);
731                 WRITEMEM(&el->stateid, sizeof(el->stateid));
732                 WRITE32(el->seqid);
733         }
734
735         return 0;
736 }
737
738 static int encode_lockt(struct xdr_stream *xdr, const struct nfs_lockargs *arg)
739 {
740         uint32_t *p;
741         struct nfs_lowner *opargs = arg->u.lockt;
742
743         RESERVE_SPACE(40);
744         WRITE32(OP_LOCKT);
745         WRITE32(arg->type);
746         WRITE64(arg->offset);
747         WRITE64(arg->length);
748         WRITE64(opargs->clientid);
749         WRITE32(4);
750         WRITE32(opargs->id);
751
752         return 0;
753 }
754
755 static int encode_locku(struct xdr_stream *xdr, const struct nfs_lockargs *arg)
756 {
757         uint32_t *p;
758         struct nfs_locku_opargs *opargs = arg->u.locku;
759
760         RESERVE_SPACE(44);
761         WRITE32(OP_LOCKU);
762         WRITE32(arg->type);
763         WRITE32(opargs->seqid);
764         WRITEMEM(&opargs->stateid, sizeof(opargs->stateid));
765         WRITE64(arg->offset);
766         WRITE64(arg->length);
767
768         return 0;
769 }
770
771 static int encode_lookup(struct xdr_stream *xdr, const struct qstr *name)
772 {
773         int len = name->len;
774         uint32_t *p;
775
776         RESERVE_SPACE(8 + len);
777         WRITE32(OP_LOOKUP);
778         WRITE32(len);
779         WRITEMEM(name->name, len);
780
781         return 0;
782 }
783
784 static void encode_share_access(struct xdr_stream *xdr, int open_flags)
785 {
786         uint32_t *p;
787
788         RESERVE_SPACE(8);
789         switch (open_flags & (FMODE_READ|FMODE_WRITE)) {
790                 case FMODE_READ:
791                         WRITE32(NFS4_SHARE_ACCESS_READ);
792                         break;
793                 case FMODE_WRITE:
794                         WRITE32(NFS4_SHARE_ACCESS_WRITE);
795                         break;
796                 case FMODE_READ|FMODE_WRITE:
797                         WRITE32(NFS4_SHARE_ACCESS_BOTH);
798                         break;
799                 default:
800                         BUG();
801         }
802         WRITE32(0);             /* for linux, share_deny = 0 always */
803 }
804
805 static inline void encode_openhdr(struct xdr_stream *xdr, const struct nfs_openargs *arg)
806 {
807         uint32_t *p;
808  /*
809  * opcode 4, seqid 4, share_access 4, share_deny 4, clientid 8, ownerlen 4,
810  * owner 4 = 32
811  */
812         RESERVE_SPACE(8);
813         WRITE32(OP_OPEN);
814         WRITE32(arg->seqid);
815         encode_share_access(xdr, arg->open_flags);
816         RESERVE_SPACE(16);
817         WRITE64(arg->clientid);
818         WRITE32(4);
819         WRITE32(arg->id);
820 }
821
822 static inline void encode_createmode(struct xdr_stream *xdr, const struct nfs_openargs *arg)
823 {
824         uint32_t *p;
825
826         RESERVE_SPACE(4);
827         switch(arg->open_flags & O_EXCL) {
828                 case 0:
829                         WRITE32(NFS4_CREATE_UNCHECKED);
830                         encode_attrs(xdr, arg->u.attrs, arg->server);
831                         break;
832                 default:
833                         WRITE32(NFS4_CREATE_EXCLUSIVE);
834                         encode_nfs4_verifier(xdr, &arg->u.verifier);
835         }
836 }
837
838 static void encode_opentype(struct xdr_stream *xdr, const struct nfs_openargs *arg)
839 {
840         uint32_t *p;
841
842         RESERVE_SPACE(4);
843         switch (arg->open_flags & O_CREAT) {
844                 case 0:
845                         WRITE32(NFS4_OPEN_NOCREATE);
846                         break;
847                 default:
848                         BUG_ON(arg->claim != NFS4_OPEN_CLAIM_NULL);
849                         WRITE32(NFS4_OPEN_CREATE);
850                         encode_createmode(xdr, arg);
851         }
852 }
853
854 static inline void encode_delegation_type(struct xdr_stream *xdr, int delegation_type)
855 {
856         uint32_t *p;
857
858         RESERVE_SPACE(4);
859         switch (delegation_type) {
860                 case 0:
861                         WRITE32(NFS4_OPEN_DELEGATE_NONE);
862                         break;
863                 case FMODE_READ:
864                         WRITE32(NFS4_OPEN_DELEGATE_READ);
865                         break;
866                 case FMODE_WRITE|FMODE_READ:
867                         WRITE32(NFS4_OPEN_DELEGATE_WRITE);
868                         break;
869                 default:
870                         BUG();
871         }
872 }
873
874 static inline void encode_claim_null(struct xdr_stream *xdr, const struct qstr *name)
875 {
876         uint32_t *p;
877
878         RESERVE_SPACE(4);
879         WRITE32(NFS4_OPEN_CLAIM_NULL);
880         encode_string(xdr, name->len, name->name);
881 }
882
883 static inline void encode_claim_previous(struct xdr_stream *xdr, int type)
884 {
885         uint32_t *p;
886
887         RESERVE_SPACE(4);
888         WRITE32(NFS4_OPEN_CLAIM_PREVIOUS);
889         encode_delegation_type(xdr, type);
890 }
891
892 static inline void encode_claim_delegate_cur(struct xdr_stream *xdr, const struct qstr *name, const nfs4_stateid *stateid)
893 {
894         uint32_t *p;
895
896         RESERVE_SPACE(4+sizeof(stateid->data));
897         WRITE32(NFS4_OPEN_CLAIM_DELEGATE_CUR);
898         WRITEMEM(stateid->data, sizeof(stateid->data));
899         encode_string(xdr, name->len, name->name);
900 }
901
902 static int encode_open(struct xdr_stream *xdr, const struct nfs_openargs *arg)
903 {
904         encode_openhdr(xdr, arg);
905         encode_opentype(xdr, arg);
906         switch (arg->claim) {
907                 case NFS4_OPEN_CLAIM_NULL:
908                         encode_claim_null(xdr, arg->name);
909                         break;
910                 case NFS4_OPEN_CLAIM_PREVIOUS:
911                         encode_claim_previous(xdr, arg->u.delegation_type);
912                         break;
913                 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
914                         encode_claim_delegate_cur(xdr, arg->name, &arg->u.delegation);
915                         break;
916                 default:
917                         BUG();
918         }
919         return 0;
920 }
921
922 static int encode_open_confirm(struct xdr_stream *xdr, const struct nfs_open_confirmargs *arg)
923 {
924         uint32_t *p;
925
926         RESERVE_SPACE(8+sizeof(arg->stateid.data));
927         WRITE32(OP_OPEN_CONFIRM);
928         WRITEMEM(arg->stateid.data, sizeof(arg->stateid.data));
929         WRITE32(arg->seqid);
930
931         return 0;
932 }
933
934 static int encode_open_downgrade(struct xdr_stream *xdr, const struct nfs_closeargs *arg)
935 {
936         uint32_t *p;
937
938         RESERVE_SPACE(8+sizeof(arg->stateid.data));
939         WRITE32(OP_OPEN_DOWNGRADE);
940         WRITEMEM(arg->stateid.data, sizeof(arg->stateid.data));
941         WRITE32(arg->seqid);
942         encode_share_access(xdr, arg->open_flags);
943         return 0;
944 }
945
946 static int
947 encode_putfh(struct xdr_stream *xdr, const struct nfs_fh *fh)
948 {
949         int len = fh->size;
950         uint32_t *p;
951
952         RESERVE_SPACE(8 + len);
953         WRITE32(OP_PUTFH);
954         WRITE32(len);
955         WRITEMEM(fh->data, len);
956
957         return 0;
958 }
959
960 static int encode_putrootfh(struct xdr_stream *xdr)
961 {
962         uint32_t *p;
963         
964         RESERVE_SPACE(4);
965         WRITE32(OP_PUTROOTFH);
966
967         return 0;
968 }
969
970 static void encode_stateid(struct xdr_stream *xdr, const struct nfs_open_context *ctx)
971 {
972         extern nfs4_stateid zero_stateid;
973         nfs4_stateid stateid;
974         uint32_t *p;
975
976         RESERVE_SPACE(16);
977         if (ctx->state != NULL) {
978                 nfs4_copy_stateid(&stateid, ctx->state, ctx->lockowner);
979                 WRITEMEM(stateid.data, sizeof(stateid.data));
980         } else
981                 WRITEMEM(zero_stateid.data, sizeof(zero_stateid.data));
982 }
983
984 static int encode_read(struct xdr_stream *xdr, const struct nfs_readargs *args)
985 {
986         uint32_t *p;
987
988         RESERVE_SPACE(4);
989         WRITE32(OP_READ);
990
991         encode_stateid(xdr, args->context);
992
993         RESERVE_SPACE(12);
994         WRITE64(args->offset);
995         WRITE32(args->count);
996
997         return 0;
998 }
999
1000 static int encode_readdir(struct xdr_stream *xdr, const struct nfs4_readdir_arg *readdir, struct rpc_rqst *req)
1001 {
1002         struct rpc_auth *auth = req->rq_task->tk_auth;
1003         int replen;
1004         uint32_t *p;
1005
1006         RESERVE_SPACE(32+sizeof(nfs4_verifier));
1007         WRITE32(OP_READDIR);
1008         WRITE64(readdir->cookie);
1009         WRITEMEM(readdir->verifier.data, sizeof(readdir->verifier.data));
1010         WRITE32(readdir->count >> 1);  /* We're not doing readdirplus */
1011         WRITE32(readdir->count);
1012         WRITE32(2);
1013         WRITE32(FATTR4_WORD0_FILEID);
1014         WRITE32(0);
1015
1016         /* set up reply kvec
1017          *    toplevel_status + taglen + rescount + OP_PUTFH + status
1018          *      + OP_READDIR + status + verifer(2)  = 9
1019          */
1020         replen = (RPC_REPHDRSIZE + auth->au_rslack + 9) << 2;
1021         xdr_inline_pages(&req->rq_rcv_buf, replen, readdir->pages,
1022                          readdir->pgbase, readdir->count);
1023
1024         return 0;
1025 }
1026
1027 static int encode_readlink(struct xdr_stream *xdr, const struct nfs4_readlink *readlink, struct rpc_rqst *req)
1028 {
1029         struct rpc_auth *auth = req->rq_task->tk_auth;
1030         unsigned int replen;
1031         uint32_t *p;
1032
1033         RESERVE_SPACE(4);
1034         WRITE32(OP_READLINK);
1035
1036         /* set up reply kvec
1037          *    toplevel_status + taglen + rescount + OP_PUTFH + status
1038          *      + OP_READLINK + status + string length = 8
1039          */
1040         replen = (RPC_REPHDRSIZE + auth->au_rslack + 8) << 2;
1041         xdr_inline_pages(&req->rq_rcv_buf, replen, readlink->pages,
1042                         readlink->pgbase, readlink->pglen);
1043         
1044         return 0;
1045 }
1046
1047 static int encode_remove(struct xdr_stream *xdr, const struct qstr *name)
1048 {
1049         uint32_t *p;
1050
1051         RESERVE_SPACE(8 + name->len);
1052         WRITE32(OP_REMOVE);
1053         WRITE32(name->len);
1054         WRITEMEM(name->name, name->len);
1055
1056         return 0;
1057 }
1058
1059 static int encode_rename(struct xdr_stream *xdr, const struct qstr *oldname, const struct qstr *newname)
1060 {
1061         uint32_t *p;
1062
1063         RESERVE_SPACE(8 + oldname->len);
1064         WRITE32(OP_RENAME);
1065         WRITE32(oldname->len);
1066         WRITEMEM(oldname->name, oldname->len);
1067         
1068         RESERVE_SPACE(4 + newname->len);
1069         WRITE32(newname->len);
1070         WRITEMEM(newname->name, newname->len);
1071
1072         return 0;
1073 }
1074
1075 static int encode_renew(struct xdr_stream *xdr, const struct nfs4_client *client_stateid)
1076 {
1077         uint32_t *p;
1078
1079         RESERVE_SPACE(12);
1080         WRITE32(OP_RENEW);
1081         WRITE64(client_stateid->cl_clientid);
1082
1083         return 0;
1084 }
1085
1086 static int
1087 encode_savefh(struct xdr_stream *xdr)
1088 {
1089         uint32_t *p;
1090
1091         RESERVE_SPACE(4);
1092         WRITE32(OP_SAVEFH);
1093
1094         return 0;
1095 }
1096
1097 static int encode_setattr(struct xdr_stream *xdr, const struct nfs_setattrargs *arg, const struct nfs_server *server)
1098 {
1099         int status;
1100         uint32_t *p;
1101         
1102         RESERVE_SPACE(4+sizeof(arg->stateid.data));
1103         WRITE32(OP_SETATTR);
1104         WRITEMEM(arg->stateid.data, sizeof(arg->stateid.data));
1105
1106         if ((status = encode_attrs(xdr, arg->iap, server)))
1107                 return status;
1108
1109         return 0;
1110 }
1111
1112 static int encode_setclientid(struct xdr_stream *xdr, const struct nfs4_setclientid *setclientid)
1113 {
1114         uint32_t *p;
1115
1116         RESERVE_SPACE(4 + sizeof(setclientid->sc_verifier->data));
1117         WRITE32(OP_SETCLIENTID);
1118         WRITEMEM(setclientid->sc_verifier->data, sizeof(setclientid->sc_verifier->data));
1119
1120         encode_string(xdr, setclientid->sc_name_len, setclientid->sc_name);
1121         RESERVE_SPACE(4);
1122         WRITE32(setclientid->sc_prog);
1123         encode_string(xdr, setclientid->sc_netid_len, setclientid->sc_netid);
1124         encode_string(xdr, setclientid->sc_uaddr_len, setclientid->sc_uaddr);
1125         RESERVE_SPACE(4);
1126         WRITE32(setclientid->sc_cb_ident);
1127
1128         return 0;
1129 }
1130
1131 static int encode_setclientid_confirm(struct xdr_stream *xdr, const struct nfs4_client *client_state)
1132 {
1133         uint32_t *p;
1134
1135         RESERVE_SPACE(12 + sizeof(client_state->cl_confirm.data));
1136         WRITE32(OP_SETCLIENTID_CONFIRM);
1137         WRITE64(client_state->cl_clientid);
1138         WRITEMEM(client_state->cl_confirm.data, sizeof(client_state->cl_confirm.data));
1139
1140         return 0;
1141 }
1142
1143 static int encode_write(struct xdr_stream *xdr, const struct nfs_writeargs *args)
1144 {
1145         uint32_t *p;
1146
1147         RESERVE_SPACE(4);
1148         WRITE32(OP_WRITE);
1149
1150         encode_stateid(xdr, args->context);
1151
1152         RESERVE_SPACE(16);
1153         WRITE64(args->offset);
1154         WRITE32(args->stable);
1155         WRITE32(args->count);
1156
1157         xdr_write_pages(xdr, args->pages, args->pgbase, args->count);
1158
1159         return 0;
1160 }
1161
1162 static int encode_delegreturn(struct xdr_stream *xdr, const nfs4_stateid *stateid)
1163 {
1164         uint32_t *p;
1165
1166         RESERVE_SPACE(20);
1167
1168         WRITE32(OP_DELEGRETURN);
1169         WRITEMEM(stateid->data, sizeof(stateid->data));
1170         return 0;
1171
1172 }
1173 /*
1174  * END OF "GENERIC" ENCODE ROUTINES.
1175  */
1176
1177 /*
1178  * Encode an ACCESS request
1179  */
1180 static int nfs4_xdr_enc_access(struct rpc_rqst *req, uint32_t *p, const struct nfs4_accessargs *args)
1181 {
1182         struct xdr_stream xdr;
1183         struct compound_hdr hdr = {
1184                 .nops = 2,
1185         };
1186         int status;
1187
1188         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1189         encode_compound_hdr(&xdr, &hdr);
1190         if ((status = encode_putfh(&xdr, args->fh)) == 0)
1191                 status = encode_access(&xdr, args->access);
1192         return status;
1193 }
1194
1195 /*
1196  * Encode LOOKUP request
1197  */
1198 static int nfs4_xdr_enc_lookup(struct rpc_rqst *req, uint32_t *p, const struct nfs4_lookup_arg *args)
1199 {
1200         struct xdr_stream xdr;
1201         struct compound_hdr hdr = {
1202                 .nops = 4,
1203         };
1204         int status;
1205
1206         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1207         encode_compound_hdr(&xdr, &hdr);
1208         if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1209                 goto out;
1210         if ((status = encode_lookup(&xdr, args->name)) != 0)
1211                 goto out;
1212         if ((status = encode_getfh(&xdr)) != 0)
1213                 goto out;
1214         status = encode_getfattr(&xdr, args->bitmask);
1215 out:
1216         return status;
1217 }
1218
1219 /*
1220  * Encode LOOKUP_ROOT request
1221  */
1222 static int nfs4_xdr_enc_lookup_root(struct rpc_rqst *req, uint32_t *p, const struct nfs4_lookup_root_arg *args)
1223 {
1224         struct xdr_stream xdr;
1225         struct compound_hdr hdr = {
1226                 .nops = 3,
1227         };
1228         int status;
1229
1230         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1231         encode_compound_hdr(&xdr, &hdr);
1232         if ((status = encode_putrootfh(&xdr)) != 0)
1233                 goto out;
1234         if ((status = encode_getfh(&xdr)) == 0)
1235                 status = encode_getfattr(&xdr, args->bitmask);
1236 out:
1237         return status;
1238 }
1239
1240 /*
1241  * Encode REMOVE request
1242  */
1243 static int nfs4_xdr_enc_remove(struct rpc_rqst *req, uint32_t *p, const struct nfs4_remove_arg *args)
1244 {
1245         struct xdr_stream xdr;
1246         struct compound_hdr hdr = {
1247                 .nops = 2,
1248         };
1249         int status;
1250
1251         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1252         encode_compound_hdr(&xdr, &hdr);
1253         if ((status = encode_putfh(&xdr, args->fh)) == 0)
1254                 status = encode_remove(&xdr, args->name);
1255         return status;
1256 }
1257
1258 /*
1259  * Encode RENAME request
1260  */
1261 static int nfs4_xdr_enc_rename(struct rpc_rqst *req, uint32_t *p, const struct nfs4_rename_arg *args)
1262 {
1263         struct xdr_stream xdr;
1264         struct compound_hdr hdr = {
1265                 .nops = 4,
1266         };
1267         int status;
1268
1269         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1270         encode_compound_hdr(&xdr, &hdr);
1271         if ((status = encode_putfh(&xdr, args->old_dir)) != 0)
1272                 goto out;
1273         if ((status = encode_savefh(&xdr)) != 0)
1274                 goto out;
1275         if ((status = encode_putfh(&xdr, args->new_dir)) != 0)
1276                 goto out;
1277         status = encode_rename(&xdr, args->old_name, args->new_name);
1278 out:
1279         return status;
1280 }
1281
1282 /*
1283  * Encode LINK request
1284  */
1285 static int nfs4_xdr_enc_link(struct rpc_rqst *req, uint32_t *p, const struct nfs4_link_arg *args)
1286 {
1287         struct xdr_stream xdr;
1288         struct compound_hdr hdr = {
1289                 .nops = 4,
1290         };
1291         int status;
1292
1293         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1294         encode_compound_hdr(&xdr, &hdr);
1295         if ((status = encode_putfh(&xdr, args->fh)) != 0)
1296                 goto out;
1297         if ((status = encode_savefh(&xdr)) != 0)
1298                 goto out;
1299         if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1300                 goto out;
1301         status = encode_link(&xdr, args->name);
1302 out:
1303         return status;
1304 }
1305
1306 /*
1307  * Encode CREATE request
1308  */
1309 static int nfs4_xdr_enc_create(struct rpc_rqst *req, uint32_t *p, const struct nfs4_create_arg *args)
1310 {
1311         struct xdr_stream xdr;
1312         struct compound_hdr hdr = {
1313                 .nops = 4,
1314         };
1315         int status;
1316
1317         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1318         encode_compound_hdr(&xdr, &hdr);
1319         if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1320                 goto out;
1321         if ((status = encode_create(&xdr, args)) != 0)
1322                 goto out;
1323         if ((status = encode_getfh(&xdr)) != 0)
1324                 goto out;
1325         status = encode_getfattr(&xdr, args->bitmask);
1326 out:
1327         return status;
1328 }
1329
1330 /*
1331  * Encode SYMLINK request
1332  */
1333 static int nfs4_xdr_enc_symlink(struct rpc_rqst *req, uint32_t *p, const struct nfs4_create_arg *args)
1334 {
1335         return nfs4_xdr_enc_create(req, p, args);
1336 }
1337
1338 /*
1339  * Encode GETATTR request
1340  */
1341 static int nfs4_xdr_enc_getattr(struct rpc_rqst *req, uint32_t *p, const struct nfs4_getattr_arg *args)
1342 {
1343         struct xdr_stream xdr;
1344         struct compound_hdr hdr = {
1345                 .nops = 2,
1346         };
1347         int status;
1348
1349         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1350         encode_compound_hdr(&xdr, &hdr);
1351         if ((status = encode_putfh(&xdr, args->fh)) == 0)
1352                 status = encode_getfattr(&xdr, args->bitmask);
1353         return status;
1354 }
1355
1356 /*
1357  * Encode a CLOSE request
1358  */
1359 static int nfs4_xdr_enc_close(struct rpc_rqst *req, uint32_t *p, struct nfs_closeargs *args)
1360 {
1361         struct xdr_stream xdr;
1362         struct compound_hdr hdr = {
1363                 .nops   = 2,
1364         };
1365         int status;
1366
1367         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1368         encode_compound_hdr(&xdr, &hdr);
1369         status = encode_putfh(&xdr, args->fh);
1370         if(status)
1371                 goto out;
1372         status = encode_close(&xdr, args);
1373 out:
1374         return status;
1375 }
1376
1377 /*
1378  * Encode an OPEN request
1379  */
1380 static int nfs4_xdr_enc_open(struct rpc_rqst *req, uint32_t *p, struct nfs_openargs *args)
1381 {
1382         struct xdr_stream xdr;
1383         struct compound_hdr hdr = {
1384                 .nops = 4,
1385         };
1386         int status;
1387
1388         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1389         encode_compound_hdr(&xdr, &hdr);
1390         status = encode_putfh(&xdr, args->fh);
1391         if (status)
1392                 goto out;
1393         status = encode_open(&xdr, args);
1394         if (status)
1395                 goto out;
1396         status = encode_getfh(&xdr);
1397         if (status)
1398                 goto out;
1399         status = encode_getfattr(&xdr, args->bitmask);
1400 out:
1401         return status;
1402 }
1403
1404 /*
1405  * Encode an OPEN_CONFIRM request
1406  */
1407 static int nfs4_xdr_enc_open_confirm(struct rpc_rqst *req, uint32_t *p, struct nfs_open_confirmargs *args)
1408 {
1409         struct xdr_stream xdr;
1410         struct compound_hdr hdr = {
1411                 .nops   = 2,
1412         };
1413         int status;
1414
1415         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1416         encode_compound_hdr(&xdr, &hdr);
1417         status = encode_putfh(&xdr, args->fh);
1418         if(status)
1419                 goto out;
1420         status = encode_open_confirm(&xdr, args);
1421 out:
1422         return status;
1423 }
1424
1425 /*
1426  * Encode an OPEN request with no attributes.
1427  */
1428 static int nfs4_xdr_enc_open_noattr(struct rpc_rqst *req, uint32_t *p, struct nfs_openargs *args)
1429 {
1430         struct xdr_stream xdr;
1431         struct compound_hdr hdr = {
1432                 .nops   = 2,
1433         };
1434         int status;
1435
1436         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1437         encode_compound_hdr(&xdr, &hdr);
1438         status = encode_putfh(&xdr, args->fh);
1439         if (status)
1440                 goto out;
1441         status = encode_open(&xdr, args);
1442 out:
1443         return status;
1444 }
1445
1446 /*
1447  * Encode an OPEN_DOWNGRADE request
1448  */
1449 static int nfs4_xdr_enc_open_downgrade(struct rpc_rqst *req, uint32_t *p, struct nfs_closeargs *args)
1450 {
1451         struct xdr_stream xdr;
1452         struct compound_hdr hdr = {
1453                 .nops   = 2,
1454         };
1455         int status;
1456
1457         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1458         encode_compound_hdr(&xdr, &hdr);
1459         status = encode_putfh(&xdr, args->fh);
1460         if (status)
1461                 goto out;
1462         status = encode_open_downgrade(&xdr, args);
1463 out:
1464         return status;
1465 }
1466
1467 /*
1468  * Encode a LOCK request
1469  */
1470 static int nfs4_xdr_enc_lock(struct rpc_rqst *req, uint32_t *p, struct nfs_lockargs *args)
1471 {
1472         struct xdr_stream xdr;
1473         struct compound_hdr hdr = {
1474                 .nops   = 2,
1475         };
1476         int status;
1477
1478         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1479         encode_compound_hdr(&xdr, &hdr);
1480         status = encode_putfh(&xdr, args->fh);
1481         if(status)
1482                 goto out;
1483         status = encode_lock(&xdr, args);
1484 out:
1485         return status;
1486 }
1487
1488 /*
1489  * Encode a LOCKT request
1490  */
1491 static int nfs4_xdr_enc_lockt(struct rpc_rqst *req, uint32_t *p, struct nfs_lockargs *args)
1492 {
1493         struct xdr_stream xdr;
1494         struct compound_hdr hdr = {
1495                 .nops   = 2,
1496         };
1497         int status;
1498
1499         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1500         encode_compound_hdr(&xdr, &hdr);
1501         status = encode_putfh(&xdr, args->fh);
1502         if(status)
1503                 goto out;
1504         status = encode_lockt(&xdr, args);
1505 out:
1506         return status;
1507 }
1508
1509 /*
1510  * Encode a LOCKU request
1511  */
1512 static int nfs4_xdr_enc_locku(struct rpc_rqst *req, uint32_t *p, struct nfs_lockargs *args)
1513 {
1514         struct xdr_stream xdr;
1515         struct compound_hdr hdr = {
1516                 .nops   = 2,
1517         };
1518         int status;
1519
1520         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1521         encode_compound_hdr(&xdr, &hdr);
1522         status = encode_putfh(&xdr, args->fh);
1523         if(status)
1524                 goto out;
1525         status = encode_locku(&xdr, args);
1526 out:
1527         return status;
1528 }
1529
1530 /*
1531  * Encode a READLINK request
1532  */
1533 static int nfs4_xdr_enc_readlink(struct rpc_rqst *req, uint32_t *p, const struct nfs4_readlink *args)
1534 {
1535         struct xdr_stream xdr;
1536         struct compound_hdr hdr = {
1537                 .nops = 2,
1538         };
1539         int status;
1540
1541         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1542         encode_compound_hdr(&xdr, &hdr);
1543         status = encode_putfh(&xdr, args->fh);
1544         if(status)
1545                 goto out;
1546         status = encode_readlink(&xdr, args, req);
1547 out:
1548         return status;
1549 }
1550
1551 /*
1552  * Encode a READDIR request
1553  */
1554 static int nfs4_xdr_enc_readdir(struct rpc_rqst *req, uint32_t *p, const struct nfs4_readdir_arg *args)
1555 {
1556         struct xdr_stream xdr;
1557         struct compound_hdr hdr = {
1558                 .nops = 2,
1559         };
1560         int status;
1561
1562         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1563         encode_compound_hdr(&xdr, &hdr);
1564         status = encode_putfh(&xdr, args->fh);
1565         if(status)
1566                 goto out;
1567         status = encode_readdir(&xdr, args, req);
1568 out:
1569         return status;
1570 }
1571
1572 /*
1573  * Encode a READ request
1574  */
1575 static int nfs4_xdr_enc_read(struct rpc_rqst *req, uint32_t *p, struct nfs_readargs *args)
1576 {
1577         struct rpc_auth *auth = req->rq_task->tk_auth;
1578         struct xdr_stream xdr;
1579         struct compound_hdr hdr = {
1580                 .nops = 2,
1581         };
1582         int replen, status;
1583
1584         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1585         encode_compound_hdr(&xdr, &hdr);
1586         status = encode_putfh(&xdr, args->fh);
1587         if (status)
1588                 goto out;
1589         status = encode_read(&xdr, args);
1590         if (status)
1591                 goto out;
1592
1593         /* set up reply kvec
1594          *    toplevel status + taglen=0 + rescount + OP_PUTFH + status
1595          *       + OP_READ + status + eof + datalen = 9
1596          */
1597         replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_read_sz) << 2;
1598         xdr_inline_pages(&req->rq_rcv_buf, replen,
1599                          args->pages, args->pgbase, args->count);
1600 out:
1601         return status;
1602 }
1603
1604 /*
1605  * Encode an SETATTR request
1606  */
1607 static int nfs4_xdr_enc_setattr(struct rpc_rqst *req, uint32_t *p, struct nfs_setattrargs *args)
1608
1609 {
1610         struct xdr_stream xdr;
1611         struct compound_hdr hdr = {
1612                 .nops   = 3,
1613         };
1614         int status;
1615
1616         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1617         encode_compound_hdr(&xdr, &hdr);
1618         status = encode_putfh(&xdr, args->fh);
1619         if(status)
1620                 goto out;
1621         status = encode_setattr(&xdr, args, args->server);
1622         if(status)
1623                 goto out;
1624         status = encode_getfattr(&xdr, args->bitmask);
1625 out:
1626         return status;
1627 }
1628
1629 /*
1630  * Encode a WRITE request
1631  */
1632 static int nfs4_xdr_enc_write(struct rpc_rqst *req, uint32_t *p, struct nfs_writeargs *args)
1633 {
1634         struct xdr_stream xdr;
1635         struct compound_hdr hdr = {
1636                 .nops = 2,
1637         };
1638         int status;
1639
1640         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1641         encode_compound_hdr(&xdr, &hdr);
1642         status = encode_putfh(&xdr, args->fh);
1643         if (status)
1644                 goto out;
1645         status = encode_write(&xdr, args);
1646 out:
1647         return status;
1648 }
1649
1650 /*
1651  *  a COMMIT request
1652  */
1653 static int nfs4_xdr_enc_commit(struct rpc_rqst *req, uint32_t *p, struct nfs_writeargs *args)
1654 {
1655         struct xdr_stream xdr;
1656         struct compound_hdr hdr = {
1657                 .nops = 2,
1658         };
1659         int status;
1660
1661         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1662         encode_compound_hdr(&xdr, &hdr);
1663         status = encode_putfh(&xdr, args->fh);
1664         if (status)
1665                 goto out;
1666         status = encode_commit(&xdr, args);
1667 out:
1668         return status;
1669 }
1670
1671 /*
1672  * FSINFO request
1673  */
1674 static int nfs4_xdr_enc_fsinfo(struct rpc_rqst *req, uint32_t *p, struct nfs4_fsinfo_arg *args)
1675 {
1676         struct xdr_stream xdr;
1677         struct compound_hdr hdr = {
1678                 .nops   = 2,
1679         };
1680         int status;
1681
1682         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1683         encode_compound_hdr(&xdr, &hdr);
1684         status = encode_putfh(&xdr, args->fh);
1685         if (!status)
1686                 status = encode_fsinfo(&xdr, args->bitmask);
1687         return status;
1688 }
1689
1690 /*
1691  * a PATHCONF request
1692  */
1693 static int nfs4_xdr_enc_pathconf(struct rpc_rqst *req, uint32_t *p, const struct nfs4_pathconf_arg *args)
1694 {
1695         extern u32 nfs4_pathconf_bitmap[2];
1696         struct xdr_stream xdr;
1697         struct compound_hdr hdr = {
1698                 .nops = 2,
1699         };
1700         int status;
1701
1702         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1703         encode_compound_hdr(&xdr, &hdr);
1704         status = encode_putfh(&xdr, args->fh);
1705         if (!status)
1706                 status = encode_getattr_one(&xdr,
1707                                 args->bitmask[0] & nfs4_pathconf_bitmap[0]);
1708         return status;
1709 }
1710
1711 /*
1712  * a STATFS request
1713  */
1714 static int nfs4_xdr_enc_statfs(struct rpc_rqst *req, uint32_t *p, const struct nfs4_statfs_arg *args)
1715 {
1716         extern u32 nfs4_statfs_bitmap[];
1717         struct xdr_stream xdr;
1718         struct compound_hdr hdr = {
1719                 .nops = 2,
1720         };
1721         int status;
1722
1723         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1724         encode_compound_hdr(&xdr, &hdr);
1725         status = encode_putfh(&xdr, args->fh);
1726         if (status == 0)
1727                 status = encode_getattr_two(&xdr,
1728                                 args->bitmask[0] & nfs4_statfs_bitmap[0],
1729                                 args->bitmask[1] & nfs4_statfs_bitmap[1]);
1730         return status;
1731 }
1732
1733 /*
1734  * GETATTR_BITMAP request
1735  */
1736 static int nfs4_xdr_enc_server_caps(struct rpc_rqst *req, uint32_t *p, const struct nfs_fh *fhandle)
1737 {
1738         struct xdr_stream xdr;
1739         struct compound_hdr hdr = {
1740                 .nops = 2,
1741         };
1742         int status;
1743
1744         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1745         encode_compound_hdr(&xdr, &hdr);
1746         status = encode_putfh(&xdr, fhandle);
1747         if (status == 0)
1748                 status = encode_getattr_one(&xdr, FATTR4_WORD0_SUPPORTED_ATTRS|
1749                                 FATTR4_WORD0_LINK_SUPPORT|
1750                                 FATTR4_WORD0_SYMLINK_SUPPORT|
1751                                 FATTR4_WORD0_ACLSUPPORT);
1752         return status;
1753 }
1754
1755 /*
1756  * a RENEW request
1757  */
1758 static int nfs4_xdr_enc_renew(struct rpc_rqst *req, uint32_t *p, struct nfs4_client *clp)
1759 {
1760         struct xdr_stream xdr;
1761         struct compound_hdr hdr = {
1762                 .nops   = 1,
1763         };
1764
1765         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1766         encode_compound_hdr(&xdr, &hdr);
1767         return encode_renew(&xdr, clp);
1768 }
1769
1770 /*
1771  * a SETCLIENTID request
1772  */
1773 static int nfs4_xdr_enc_setclientid(struct rpc_rqst *req, uint32_t *p, struct nfs4_setclientid *sc)
1774 {
1775         struct xdr_stream xdr;
1776         struct compound_hdr hdr = {
1777                 .nops   = 1,
1778         };
1779
1780         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1781         encode_compound_hdr(&xdr, &hdr);
1782         return encode_setclientid(&xdr, sc);
1783 }
1784
1785 /*
1786  * a SETCLIENTID_CONFIRM request
1787  */
1788 static int nfs4_xdr_enc_setclientid_confirm(struct rpc_rqst *req, uint32_t *p, struct nfs4_client *clp)
1789 {
1790         struct xdr_stream xdr;
1791         struct compound_hdr hdr = {
1792                 .nops   = 3,
1793         };
1794         const u32 lease_bitmap[2] = { FATTR4_WORD0_LEASE_TIME, 0 };
1795         int status;
1796
1797         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1798         encode_compound_hdr(&xdr, &hdr);
1799         status = encode_setclientid_confirm(&xdr, clp);
1800         if (!status)
1801                 status = encode_putrootfh(&xdr);
1802         if (!status)
1803                 status = encode_fsinfo(&xdr, lease_bitmap);
1804         return status;
1805 }
1806
1807 /*
1808  * DELEGRETURN request
1809  */
1810 static int nfs4_xdr_enc_delegreturn(struct rpc_rqst *req, uint32_t *p, const struct nfs4_delegreturnargs *args)
1811 {
1812         struct xdr_stream xdr;
1813         struct compound_hdr hdr = {
1814                 .nops = 2,
1815         };
1816         int status;
1817
1818         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1819         encode_compound_hdr(&xdr, &hdr);
1820         if ((status = encode_putfh(&xdr, args->fhandle)) == 0)
1821                 status = encode_delegreturn(&xdr, args->stateid);
1822         return status;
1823 }
1824
1825 /*
1826  * START OF "GENERIC" DECODE ROUTINES.
1827  *   These may look a little ugly since they are imported from a "generic"
1828  * set of XDR encode/decode routines which are intended to be shared by
1829  * all of our NFSv4 implementations (OpenBSD, MacOS X...).
1830  *
1831  * If the pain of reading these is too great, it should be a straightforward
1832  * task to translate them into Linux-specific versions which are more
1833  * consistent with the style used in NFSv2/v3...
1834  */
1835 #define READ32(x)         (x) = ntohl(*p++)
1836 #define READ64(x)         do {                  \
1837         (x) = (u64)ntohl(*p++) << 32;           \
1838         (x) |= ntohl(*p++);                     \
1839 } while (0)
1840 #define READTIME(x)       do {                  \
1841         p++;                                    \
1842         (x.tv_sec) = ntohl(*p++);               \
1843         (x.tv_nsec) = ntohl(*p++);              \
1844 } while (0)
1845 #define COPYMEM(x,nbytes) do {                  \
1846         memcpy((x), p, nbytes);                 \
1847         p += XDR_QUADLEN(nbytes);               \
1848 } while (0)
1849
1850 #define READ_BUF(nbytes)  do { \
1851         p = xdr_inline_decode(xdr, nbytes); \
1852         if (!p) { \
1853                 printk(KERN_WARNING "%s: reply buffer overflowed in line %d.", \
1854                                 __FUNCTION__, __LINE__); \
1855                 return -EIO; \
1856         } \
1857 } while (0)
1858
1859 static int decode_opaque_inline(struct xdr_stream *xdr, uint32_t *len, char **string)
1860 {
1861         uint32_t *p;
1862
1863         READ_BUF(4);
1864         READ32(*len);
1865         READ_BUF(*len);
1866         *string = (char *)p;
1867         return 0;
1868 }
1869
1870 static int decode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
1871 {
1872         uint32_t *p;
1873
1874         READ_BUF(8);
1875         READ32(hdr->status);
1876         READ32(hdr->taglen);
1877         
1878         READ_BUF(hdr->taglen + 4);
1879         hdr->tag = (char *)p;
1880         p += XDR_QUADLEN(hdr->taglen);
1881         READ32(hdr->nops);
1882         return 0;
1883 }
1884
1885 static int decode_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
1886 {
1887         uint32_t *p;
1888         uint32_t opnum;
1889         int32_t nfserr;
1890
1891         READ_BUF(8);
1892         READ32(opnum);
1893         if (opnum != expected) {
1894                 printk(KERN_NOTICE
1895                                 "nfs4_decode_op_hdr: Server returned operation"
1896                                 " %d but we issued a request for %d\n",
1897                                 opnum, expected);
1898                 return -EIO;
1899         }
1900         READ32(nfserr);
1901         if (nfserr != NFS_OK)
1902                 return -nfs_stat_to_errno(nfserr);
1903         return 0;
1904 }
1905
1906 /* Dummy routine */
1907 static int decode_ace(struct xdr_stream *xdr, void *ace, struct nfs4_client *clp)
1908 {
1909         uint32_t *p;
1910         uint32_t strlen;
1911         char *str;
1912
1913         READ_BUF(12);
1914         return decode_opaque_inline(xdr, &strlen, &str);
1915 }
1916
1917 static int decode_attr_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
1918 {
1919         uint32_t bmlen, *p;
1920
1921         READ_BUF(4);
1922         READ32(bmlen);
1923
1924         bitmap[0] = bitmap[1] = 0;
1925         READ_BUF((bmlen << 2));
1926         if (bmlen > 0) {
1927                 READ32(bitmap[0]);
1928                 if (bmlen > 1)
1929                         READ32(bitmap[1]);
1930         }
1931         return 0;
1932 }
1933
1934 static inline int decode_attr_length(struct xdr_stream *xdr, uint32_t *attrlen, uint32_t **savep)
1935 {
1936         uint32_t *p;
1937
1938         READ_BUF(4);
1939         READ32(*attrlen);
1940         *savep = xdr->p;
1941         return 0;
1942 }
1943
1944 static int decode_attr_supported(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *bitmask)
1945 {
1946         if (likely(bitmap[0] & FATTR4_WORD0_SUPPORTED_ATTRS)) {
1947                 decode_attr_bitmap(xdr, bitmask);
1948                 bitmap[0] &= ~FATTR4_WORD0_SUPPORTED_ATTRS;
1949         } else
1950                 bitmask[0] = bitmask[1] = 0;
1951         dprintk("%s: bitmask=0x%x%x\n", __FUNCTION__, bitmask[0], bitmask[1]);
1952         return 0;
1953 }
1954
1955 static int decode_attr_type(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *type)
1956 {
1957         uint32_t *p;
1958
1959         *type = 0;
1960         if (unlikely(bitmap[0] & (FATTR4_WORD0_TYPE - 1U)))
1961                 return -EIO;
1962         if (likely(bitmap[0] & FATTR4_WORD0_TYPE)) {
1963                 READ_BUF(4);
1964                 READ32(*type);
1965                 if (*type < NF4REG || *type > NF4NAMEDATTR) {
1966                         dprintk("%s: bad type %d\n", __FUNCTION__, *type);
1967                         return -EIO;
1968                 }
1969                 bitmap[0] &= ~FATTR4_WORD0_TYPE;
1970         }
1971         dprintk("%s: type=0%o\n", __FUNCTION__, nfs_type2fmt[*type].nfs2type);
1972         return 0;
1973 }
1974
1975 static int decode_attr_change(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *change)
1976 {
1977         uint32_t *p;
1978
1979         *change = 0;
1980         if (unlikely(bitmap[0] & (FATTR4_WORD0_CHANGE - 1U)))
1981                 return -EIO;
1982         if (likely(bitmap[0] & FATTR4_WORD0_CHANGE)) {
1983                 READ_BUF(8);
1984                 READ64(*change);
1985                 bitmap[0] &= ~FATTR4_WORD0_CHANGE;
1986         }
1987         dprintk("%s: change attribute=%Lu\n", __FUNCTION__,
1988                         (unsigned long long)*change);
1989         return 0;
1990 }
1991
1992 static int decode_attr_size(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *size)
1993 {
1994         uint32_t *p;
1995
1996         *size = 0;
1997         if (unlikely(bitmap[0] & (FATTR4_WORD0_SIZE - 1U)))
1998                 return -EIO;
1999         if (likely(bitmap[0] & FATTR4_WORD0_SIZE)) {
2000                 READ_BUF(8);
2001                 READ64(*size);
2002                 bitmap[0] &= ~FATTR4_WORD0_SIZE;
2003         }
2004         dprintk("%s: file size=%Lu\n", __FUNCTION__, (unsigned long long)*size);
2005         return 0;
2006 }
2007
2008 static int decode_attr_link_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2009 {
2010         uint32_t *p;
2011
2012         *res = 0;
2013         if (unlikely(bitmap[0] & (FATTR4_WORD0_LINK_SUPPORT - 1U)))
2014                 return -EIO;
2015         if (likely(bitmap[0] & FATTR4_WORD0_LINK_SUPPORT)) {
2016                 READ_BUF(4);
2017                 READ32(*res);
2018                 bitmap[0] &= ~FATTR4_WORD0_LINK_SUPPORT;
2019         }
2020         dprintk("%s: link support=%s\n", __FUNCTION__, *res == 0 ? "false" : "true");
2021         return 0;
2022 }
2023
2024 static int decode_attr_symlink_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2025 {
2026         uint32_t *p;
2027
2028         *res = 0;
2029         if (unlikely(bitmap[0] & (FATTR4_WORD0_SYMLINK_SUPPORT - 1U)))
2030                 return -EIO;
2031         if (likely(bitmap[0] & FATTR4_WORD0_SYMLINK_SUPPORT)) {
2032                 READ_BUF(4);
2033                 READ32(*res);
2034                 bitmap[0] &= ~FATTR4_WORD0_SYMLINK_SUPPORT;
2035         }
2036         dprintk("%s: symlink support=%s\n", __FUNCTION__, *res == 0 ? "false" : "true");
2037         return 0;
2038 }
2039
2040 static int decode_attr_fsid(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_fsid *fsid)
2041 {
2042         uint32_t *p;
2043
2044         fsid->major = 0;
2045         fsid->minor = 0;
2046         if (unlikely(bitmap[0] & (FATTR4_WORD0_FSID - 1U)))
2047                 return -EIO;
2048         if (likely(bitmap[0] & FATTR4_WORD0_FSID)) {
2049                 READ_BUF(16);
2050                 READ64(fsid->major);
2051                 READ64(fsid->minor);
2052                 bitmap[0] &= ~FATTR4_WORD0_FSID;
2053         }
2054         dprintk("%s: fsid=(0x%Lx/0x%Lx)\n", __FUNCTION__,
2055                         (unsigned long long)fsid->major,
2056                         (unsigned long long)fsid->minor);
2057         return 0;
2058 }
2059
2060 static int decode_attr_lease_time(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2061 {
2062         uint32_t *p;
2063
2064         *res = 60;
2065         if (unlikely(bitmap[0] & (FATTR4_WORD0_LEASE_TIME - 1U)))
2066                 return -EIO;
2067         if (likely(bitmap[0] & FATTR4_WORD0_LEASE_TIME)) {
2068                 READ_BUF(4);
2069                 READ32(*res);
2070                 bitmap[0] &= ~FATTR4_WORD0_LEASE_TIME;
2071         }
2072         dprintk("%s: file size=%u\n", __FUNCTION__, (unsigned int)*res);
2073         return 0;
2074 }
2075
2076 static int decode_attr_aclsupport(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2077 {
2078         uint32_t *p;
2079
2080         *res = ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL;
2081         if (unlikely(bitmap[0] & (FATTR4_WORD0_ACLSUPPORT - 1U)))
2082                 return -EIO;
2083         if (likely(bitmap[0] & FATTR4_WORD0_ACLSUPPORT)) {
2084                 READ_BUF(4);
2085                 READ32(*res);
2086                 bitmap[0] &= ~FATTR4_WORD0_ACLSUPPORT;
2087         }
2088         dprintk("%s: ACLs supported=%u\n", __FUNCTION__, (unsigned int)*res);
2089         return 0;
2090 }
2091
2092 static int decode_attr_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
2093 {
2094         uint32_t *p;
2095
2096         *fileid = 0;
2097         if (unlikely(bitmap[0] & (FATTR4_WORD0_FILEID - 1U)))
2098                 return -EIO;
2099         if (likely(bitmap[0] & FATTR4_WORD0_FILEID)) {
2100                 READ_BUF(8);
2101                 READ64(*fileid);
2102                 bitmap[0] &= ~FATTR4_WORD0_FILEID;
2103         }
2104         dprintk("%s: fileid=%Lu\n", __FUNCTION__, (unsigned long long)*fileid);
2105         return 0;
2106 }
2107
2108 static int decode_attr_files_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2109 {
2110         uint32_t *p;
2111         int status = 0;
2112
2113         *res = 0;
2114         if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_AVAIL - 1U)))
2115                 return -EIO;
2116         if (likely(bitmap[0] & FATTR4_WORD0_FILES_AVAIL)) {
2117                 READ_BUF(8);
2118                 READ64(*res);
2119                 bitmap[0] &= ~FATTR4_WORD0_FILES_AVAIL;
2120         }
2121         dprintk("%s: files avail=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2122         return status;
2123 }
2124
2125 static int decode_attr_files_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2126 {
2127         uint32_t *p;
2128         int status = 0;
2129
2130         *res = 0;
2131         if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_FREE - 1U)))
2132                 return -EIO;
2133         if (likely(bitmap[0] & FATTR4_WORD0_FILES_FREE)) {
2134                 READ_BUF(8);
2135                 READ64(*res);
2136                 bitmap[0] &= ~FATTR4_WORD0_FILES_FREE;
2137         }
2138         dprintk("%s: files free=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2139         return status;
2140 }
2141
2142 static int decode_attr_files_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2143 {
2144         uint32_t *p;
2145         int status = 0;
2146
2147         *res = 0;
2148         if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_TOTAL - 1U)))
2149                 return -EIO;
2150         if (likely(bitmap[0] & FATTR4_WORD0_FILES_TOTAL)) {
2151                 READ_BUF(8);
2152                 READ64(*res);
2153                 bitmap[0] &= ~FATTR4_WORD0_FILES_TOTAL;
2154         }
2155         dprintk("%s: files total=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2156         return status;
2157 }
2158
2159 static int decode_attr_maxfilesize(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2160 {
2161         uint32_t *p;
2162         int status = 0;
2163
2164         *res = 0;
2165         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXFILESIZE - 1U)))
2166                 return -EIO;
2167         if (likely(bitmap[0] & FATTR4_WORD0_MAXFILESIZE)) {
2168                 READ_BUF(8);
2169                 READ64(*res);
2170                 bitmap[0] &= ~FATTR4_WORD0_MAXFILESIZE;
2171         }
2172         dprintk("%s: maxfilesize=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2173         return status;
2174 }
2175
2176 static int decode_attr_maxlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxlink)
2177 {
2178         uint32_t *p;
2179         int status = 0;
2180
2181         *maxlink = 1;
2182         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXLINK - 1U)))
2183                 return -EIO;
2184         if (likely(bitmap[0] & FATTR4_WORD0_MAXLINK)) {
2185                 READ_BUF(4);
2186                 READ32(*maxlink);
2187                 bitmap[0] &= ~FATTR4_WORD0_MAXLINK;
2188         }
2189         dprintk("%s: maxlink=%u\n", __FUNCTION__, *maxlink);
2190         return status;
2191 }
2192
2193 static int decode_attr_maxname(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxname)
2194 {
2195         uint32_t *p;
2196         int status = 0;
2197
2198         *maxname = 1024;
2199         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXNAME - 1U)))
2200                 return -EIO;
2201         if (likely(bitmap[0] & FATTR4_WORD0_MAXNAME)) {
2202                 READ_BUF(4);
2203                 READ32(*maxname);
2204                 bitmap[0] &= ~FATTR4_WORD0_MAXNAME;
2205         }
2206         dprintk("%s: maxname=%u\n", __FUNCTION__, *maxname);
2207         return status;
2208 }
2209
2210 static int decode_attr_maxread(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2211 {
2212         uint32_t *p;
2213         int status = 0;
2214
2215         *res = 1024;
2216         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXREAD - 1U)))
2217                 return -EIO;
2218         if (likely(bitmap[0] & FATTR4_WORD0_MAXREAD)) {
2219                 uint64_t maxread;
2220                 READ_BUF(8);
2221                 READ64(maxread);
2222                 if (maxread > 0x7FFFFFFF)
2223                         maxread = 0x7FFFFFFF;
2224                 *res = (uint32_t)maxread;
2225                 bitmap[0] &= ~FATTR4_WORD0_MAXREAD;
2226         }
2227         dprintk("%s: maxread=%lu\n", __FUNCTION__, (unsigned long)*res);
2228         return status;
2229 }
2230
2231 static int decode_attr_maxwrite(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2232 {
2233         uint32_t *p;
2234         int status = 0;
2235
2236         *res = 1024;
2237         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXWRITE - 1U)))
2238                 return -EIO;
2239         if (likely(bitmap[0] & FATTR4_WORD0_MAXWRITE)) {
2240                 uint64_t maxwrite;
2241                 READ_BUF(8);
2242                 READ64(maxwrite);
2243                 if (maxwrite > 0x7FFFFFFF)
2244                         maxwrite = 0x7FFFFFFF;
2245                 *res = (uint32_t)maxwrite;
2246                 bitmap[0] &= ~FATTR4_WORD0_MAXWRITE;
2247         }
2248         dprintk("%s: maxwrite=%lu\n", __FUNCTION__, (unsigned long)*res);
2249         return status;
2250 }
2251
2252 static int decode_attr_mode(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *mode)
2253 {
2254         uint32_t *p;
2255
2256         *mode = 0;
2257         if (unlikely(bitmap[1] & (FATTR4_WORD1_MODE - 1U)))
2258                 return -EIO;
2259         if (likely(bitmap[1] & FATTR4_WORD1_MODE)) {
2260                 READ_BUF(4);
2261                 READ32(*mode);
2262                 *mode &= ~S_IFMT;
2263                 bitmap[1] &= ~FATTR4_WORD1_MODE;
2264         }
2265         dprintk("%s: file mode=0%o\n", __FUNCTION__, (unsigned int)*mode);
2266         return 0;
2267 }
2268
2269 static int decode_attr_nlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *nlink)
2270 {
2271         uint32_t *p;
2272
2273         *nlink = 1;
2274         if (unlikely(bitmap[1] & (FATTR4_WORD1_NUMLINKS - 1U)))
2275                 return -EIO;
2276         if (likely(bitmap[1] & FATTR4_WORD1_NUMLINKS)) {
2277                 READ_BUF(4);
2278                 READ32(*nlink);
2279                 bitmap[1] &= ~FATTR4_WORD1_NUMLINKS;
2280         }
2281         dprintk("%s: nlink=%u\n", __FUNCTION__, (unsigned int)*nlink);
2282         return 0;
2283 }
2284
2285 static int decode_attr_owner(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_client *clp, int32_t *uid)
2286 {
2287         uint32_t len, *p;
2288
2289         *uid = -2;
2290         if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER - 1U)))
2291                 return -EIO;
2292         if (likely(bitmap[1] & FATTR4_WORD1_OWNER)) {
2293                 READ_BUF(4);
2294                 READ32(len);
2295                 READ_BUF(len);
2296                 if (len < XDR_MAX_NETOBJ) {
2297                         if (nfs_map_name_to_uid(clp, (char *)p, len, uid) != 0)
2298                                 dprintk("%s: nfs_map_name_to_uid failed!\n",
2299                                                 __FUNCTION__);
2300                 } else
2301                         printk(KERN_WARNING "%s: name too long (%u)!\n",
2302                                         __FUNCTION__, len);
2303                 bitmap[1] &= ~FATTR4_WORD1_OWNER;
2304         }
2305         dprintk("%s: uid=%d\n", __FUNCTION__, (int)*uid);
2306         return 0;
2307 }
2308
2309 static int decode_attr_group(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_client *clp, int32_t *gid)
2310 {
2311         uint32_t len, *p;
2312
2313         *gid = -2;
2314         if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER_GROUP - 1U)))
2315                 return -EIO;
2316         if (likely(bitmap[1] & FATTR4_WORD1_OWNER_GROUP)) {
2317                 READ_BUF(4);
2318                 READ32(len);
2319                 READ_BUF(len);
2320                 if (len < XDR_MAX_NETOBJ) {
2321                         if (nfs_map_group_to_gid(clp, (char *)p, len, gid) != 0)
2322                                 dprintk("%s: nfs_map_group_to_gid failed!\n",
2323                                                 __FUNCTION__);
2324                 } else
2325                         printk(KERN_WARNING "%s: name too long (%u)!\n",
2326                                         __FUNCTION__, len);
2327                 bitmap[1] &= ~FATTR4_WORD1_OWNER_GROUP;
2328         }
2329         dprintk("%s: gid=%d\n", __FUNCTION__, (int)*gid);
2330         return 0;
2331 }
2332
2333 static int decode_attr_rdev(struct xdr_stream *xdr, uint32_t *bitmap, dev_t *rdev)
2334 {
2335         uint32_t major = 0, minor = 0, *p;
2336
2337         *rdev = MKDEV(0,0);
2338         if (unlikely(bitmap[1] & (FATTR4_WORD1_RAWDEV - 1U)))
2339                 return -EIO;
2340         if (likely(bitmap[1] & FATTR4_WORD1_RAWDEV)) {
2341                 dev_t tmp;
2342
2343                 READ_BUF(8);
2344                 READ32(major);
2345                 READ32(minor);
2346                 tmp = MKDEV(major, minor);
2347                 if (MAJOR(tmp) == major && MINOR(tmp) == minor)
2348                         *rdev = tmp;
2349                 bitmap[1] &= ~ FATTR4_WORD1_RAWDEV;
2350         }
2351         dprintk("%s: rdev=(0x%x:0x%x)\n", __FUNCTION__, major, minor);
2352         return 0;
2353 }
2354
2355 static int decode_attr_space_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2356 {
2357         uint32_t *p;
2358         int status = 0;
2359
2360         *res = 0;
2361         if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_AVAIL - 1U)))
2362                 return -EIO;
2363         if (likely(bitmap[1] & FATTR4_WORD1_SPACE_AVAIL)) {
2364                 READ_BUF(8);
2365                 READ64(*res);
2366                 bitmap[1] &= ~FATTR4_WORD1_SPACE_AVAIL;
2367         }
2368         dprintk("%s: space avail=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2369         return status;
2370 }
2371
2372 static int decode_attr_space_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2373 {
2374         uint32_t *p;
2375         int status = 0;
2376
2377         *res = 0;
2378         if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_FREE - 1U)))
2379                 return -EIO;
2380         if (likely(bitmap[1] & FATTR4_WORD1_SPACE_FREE)) {
2381                 READ_BUF(8);
2382                 READ64(*res);
2383                 bitmap[1] &= ~FATTR4_WORD1_SPACE_FREE;
2384         }
2385         dprintk("%s: space free=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2386         return status;
2387 }
2388
2389 static int decode_attr_space_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2390 {
2391         uint32_t *p;
2392         int status = 0;
2393
2394         *res = 0;
2395         if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_TOTAL - 1U)))
2396                 return -EIO;
2397         if (likely(bitmap[1] & FATTR4_WORD1_SPACE_TOTAL)) {
2398                 READ_BUF(8);
2399                 READ64(*res);
2400                 bitmap[1] &= ~FATTR4_WORD1_SPACE_TOTAL;
2401         }
2402         dprintk("%s: space total=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2403         return status;
2404 }
2405
2406 static int decode_attr_space_used(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *used)
2407 {
2408         uint32_t *p;
2409
2410         *used = 0;
2411         if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_USED - 1U)))
2412                 return -EIO;
2413         if (likely(bitmap[1] & FATTR4_WORD1_SPACE_USED)) {
2414                 READ_BUF(8);
2415                 READ64(*used);
2416                 bitmap[1] &= ~FATTR4_WORD1_SPACE_USED;
2417         }
2418         dprintk("%s: space used=%Lu\n", __FUNCTION__,
2419                         (unsigned long long)*used);
2420         return 0;
2421 }
2422
2423 static int decode_attr_time(struct xdr_stream *xdr, struct timespec *time)
2424 {
2425         uint32_t *p;
2426         uint64_t sec;
2427         uint32_t nsec;
2428
2429         READ_BUF(12);
2430         READ64(sec);
2431         READ32(nsec);
2432         time->tv_sec = (time_t)sec;
2433         time->tv_nsec = (long)nsec;
2434         return 0;
2435 }
2436
2437 static int decode_attr_time_access(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2438 {
2439         int status = 0;
2440
2441         time->tv_sec = 0;
2442         time->tv_nsec = 0;
2443         if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_ACCESS - 1U)))
2444                 return -EIO;
2445         if (likely(bitmap[1] & FATTR4_WORD1_TIME_ACCESS)) {
2446                 status = decode_attr_time(xdr, time);
2447                 bitmap[1] &= ~FATTR4_WORD1_TIME_ACCESS;
2448         }
2449         dprintk("%s: atime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2450         return status;
2451 }
2452
2453 static int decode_attr_time_metadata(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2454 {
2455         int status = 0;
2456
2457         time->tv_sec = 0;
2458         time->tv_nsec = 0;
2459         if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_METADATA - 1U)))
2460                 return -EIO;
2461         if (likely(bitmap[1] & FATTR4_WORD1_TIME_METADATA)) {
2462                 status = decode_attr_time(xdr, time);
2463                 bitmap[1] &= ~FATTR4_WORD1_TIME_METADATA;
2464         }
2465         dprintk("%s: ctime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2466         return status;
2467 }
2468
2469 static int decode_attr_time_modify(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2470 {
2471         int status = 0;
2472
2473         time->tv_sec = 0;
2474         time->tv_nsec = 0;
2475         if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_MODIFY - 1U)))
2476                 return -EIO;
2477         if (likely(bitmap[1] & FATTR4_WORD1_TIME_MODIFY)) {
2478                 status = decode_attr_time(xdr, time);
2479                 bitmap[1] &= ~FATTR4_WORD1_TIME_MODIFY;
2480         }
2481         dprintk("%s: mtime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2482         return status;
2483 }
2484
2485 static int verify_attr_len(struct xdr_stream *xdr, uint32_t *savep, uint32_t attrlen)
2486 {
2487         unsigned int attrwords = XDR_QUADLEN(attrlen);
2488         unsigned int nwords = xdr->p - savep;
2489
2490         if (unlikely(attrwords != nwords)) {
2491                 printk(KERN_WARNING "%s: server returned incorrect attribute length: %u %c %u\n",
2492                                 __FUNCTION__,
2493                                 attrwords << 2,
2494                                 (attrwords < nwords) ? '<' : '>',
2495                                 nwords << 2);
2496                 return -EIO;
2497         }
2498         return 0;
2499 }
2500
2501 static int decode_change_info(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
2502 {
2503         uint32_t *p;
2504
2505         READ_BUF(20);
2506         READ32(cinfo->atomic);
2507         READ64(cinfo->before);
2508         READ64(cinfo->after);
2509         return 0;
2510 }
2511
2512 static int decode_access(struct xdr_stream *xdr, struct nfs4_accessres *access)
2513 {
2514         uint32_t *p;
2515         uint32_t supp, acc;
2516         int status;
2517
2518         status = decode_op_hdr(xdr, OP_ACCESS);
2519         if (status)
2520                 return status;
2521         READ_BUF(8);
2522         READ32(supp);
2523         READ32(acc);
2524         access->supported = supp;
2525         access->access = acc;
2526         return 0;
2527 }
2528
2529 static int decode_close(struct xdr_stream *xdr, struct nfs_closeres *res)
2530 {
2531         uint32_t *p;
2532         int status;
2533
2534         status = decode_op_hdr(xdr, OP_CLOSE);
2535         if (status)
2536                 return status;
2537         READ_BUF(sizeof(res->stateid.data));
2538         COPYMEM(res->stateid.data, sizeof(res->stateid.data));
2539         return 0;
2540 }
2541
2542 static int decode_commit(struct xdr_stream *xdr, struct nfs_writeres *res)
2543 {
2544         uint32_t *p;
2545         int status;
2546
2547         status = decode_op_hdr(xdr, OP_COMMIT);
2548         if (status)
2549                 return status;
2550         READ_BUF(8);
2551         COPYMEM(res->verf->verifier, 8);
2552         return 0;
2553 }
2554
2555 static int decode_create(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
2556 {
2557         uint32_t *p;
2558         uint32_t bmlen;
2559         int status;
2560
2561         status = decode_op_hdr(xdr, OP_CREATE);
2562         if (status)
2563                 return status;
2564         if ((status = decode_change_info(xdr, cinfo)))
2565                 return status;
2566         READ_BUF(4);
2567         READ32(bmlen);
2568         READ_BUF(bmlen << 2);
2569         return 0;
2570 }
2571
2572 static int decode_server_caps(struct xdr_stream *xdr, struct nfs4_server_caps_res *res)
2573 {
2574         uint32_t *savep;
2575         uint32_t attrlen, 
2576                  bitmap[2] = {0};
2577         int status;
2578
2579         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2580                 goto xdr_error;
2581         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2582                 goto xdr_error;
2583         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2584                 goto xdr_error;
2585         if ((status = decode_attr_supported(xdr, bitmap, res->attr_bitmask)) != 0)
2586                 goto xdr_error;
2587         if ((status = decode_attr_link_support(xdr, bitmap, &res->has_links)) != 0)
2588                 goto xdr_error;
2589         if ((status = decode_attr_symlink_support(xdr, bitmap, &res->has_symlinks)) != 0)
2590                 goto xdr_error;
2591         if ((status = decode_attr_aclsupport(xdr, bitmap, &res->acl_bitmask)) != 0)
2592                 goto xdr_error;
2593         status = verify_attr_len(xdr, savep, attrlen);
2594 xdr_error:
2595         if (status != 0)
2596                 printk(KERN_NOTICE "%s: xdr error %d!\n", __FUNCTION__, -status);
2597         return status;
2598 }
2599         
2600 static int decode_statfs(struct xdr_stream *xdr, struct nfs_fsstat *fsstat)
2601 {
2602         uint32_t *savep;
2603         uint32_t attrlen, 
2604                  bitmap[2] = {0};
2605         int status;
2606         
2607         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2608                 goto xdr_error;
2609         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2610                 goto xdr_error;
2611         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2612                 goto xdr_error;
2613
2614         if ((status = decode_attr_files_avail(xdr, bitmap, &fsstat->afiles)) != 0)
2615                 goto xdr_error;
2616         if ((status = decode_attr_files_free(xdr, bitmap, &fsstat->ffiles)) != 0)
2617                 goto xdr_error;
2618         if ((status = decode_attr_files_total(xdr, bitmap, &fsstat->tfiles)) != 0)
2619                 goto xdr_error;
2620         if ((status = decode_attr_space_avail(xdr, bitmap, &fsstat->abytes)) != 0)
2621                 goto xdr_error;
2622         if ((status = decode_attr_space_free(xdr, bitmap, &fsstat->fbytes)) != 0)
2623                 goto xdr_error;
2624         if ((status = decode_attr_space_total(xdr, bitmap, &fsstat->tbytes)) != 0)
2625                 goto xdr_error;
2626
2627         status = verify_attr_len(xdr, savep, attrlen);
2628 xdr_error:
2629         if (status != 0)
2630                 printk(KERN_NOTICE "%s: xdr error %d!\n", __FUNCTION__, -status);
2631         return status;
2632 }
2633
2634 static int decode_pathconf(struct xdr_stream *xdr, struct nfs_pathconf *pathconf)
2635 {
2636         uint32_t *savep;
2637         uint32_t attrlen, 
2638                  bitmap[2] = {0};
2639         int status;
2640         
2641         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2642                 goto xdr_error;
2643         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2644                 goto xdr_error;
2645         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2646                 goto xdr_error;
2647
2648         if ((status = decode_attr_maxlink(xdr, bitmap, &pathconf->max_link)) != 0)
2649                 goto xdr_error;
2650         if ((status = decode_attr_maxname(xdr, bitmap, &pathconf->max_namelen)) != 0)
2651                 goto xdr_error;
2652
2653         status = verify_attr_len(xdr, savep, attrlen);
2654 xdr_error:
2655         if (status != 0)
2656                 printk(KERN_NOTICE "%s: xdr error %d!\n", __FUNCTION__, -status);
2657         return status;
2658 }
2659
2660 static int decode_getfattr(struct xdr_stream *xdr, struct nfs_fattr *fattr, const struct nfs_server *server)
2661 {
2662         uint32_t *savep;
2663         uint32_t attrlen,
2664                  bitmap[2] = {0},
2665                  type;
2666         int status, fmode = 0;
2667
2668         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2669                 goto xdr_error;
2670         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2671                 goto xdr_error;
2672
2673         fattr->bitmap[0] = bitmap[0];
2674         fattr->bitmap[1] = bitmap[1];
2675
2676         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2677                 goto xdr_error;
2678
2679
2680         if ((status = decode_attr_type(xdr, bitmap, &type)) != 0)
2681                 goto xdr_error;
2682         fattr->type = nfs_type2fmt[type].nfs2type;
2683         fmode = nfs_type2fmt[type].mode;
2684
2685         if ((status = decode_attr_change(xdr, bitmap, &fattr->change_attr)) != 0)
2686                 goto xdr_error;
2687         if ((status = decode_attr_size(xdr, bitmap, &fattr->size)) != 0)
2688                 goto xdr_error;
2689         if ((status = decode_attr_fsid(xdr, bitmap, &fattr->fsid_u.nfs4)) != 0)
2690                 goto xdr_error;
2691         if ((status = decode_attr_fileid(xdr, bitmap, &fattr->fileid)) != 0)
2692                 goto xdr_error;
2693         if ((status = decode_attr_mode(xdr, bitmap, &fattr->mode)) != 0)
2694                 goto xdr_error;
2695         fattr->mode |= fmode;
2696         if ((status = decode_attr_nlink(xdr, bitmap, &fattr->nlink)) != 0)
2697                 goto xdr_error;
2698         if ((status = decode_attr_owner(xdr, bitmap, server->nfs4_state, &fattr->uid)) != 0)
2699                 goto xdr_error;
2700         if ((status = decode_attr_group(xdr, bitmap, server->nfs4_state, &fattr->gid)) != 0)
2701                 goto xdr_error;
2702         if ((status = decode_attr_rdev(xdr, bitmap, &fattr->rdev)) != 0)
2703                 goto xdr_error;
2704         if ((status = decode_attr_space_used(xdr, bitmap, &fattr->du.nfs3.used)) != 0)
2705                 goto xdr_error;
2706         if ((status = decode_attr_time_access(xdr, bitmap, &fattr->atime)) != 0)
2707                 goto xdr_error;
2708         if ((status = decode_attr_time_metadata(xdr, bitmap, &fattr->ctime)) != 0)
2709                 goto xdr_error;
2710         if ((status = decode_attr_time_modify(xdr, bitmap, &fattr->mtime)) != 0)
2711                 goto xdr_error;
2712         if ((status = verify_attr_len(xdr, savep, attrlen)) == 0) {
2713                 fattr->valid = NFS_ATTR_FATTR | NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4;
2714                 fattr->timestamp = jiffies;
2715         }
2716 xdr_error:
2717         if (status != 0)
2718                 printk(KERN_NOTICE "%s: xdr error %d!\n", __FUNCTION__, -status);
2719         return status;
2720 }
2721
2722
2723 static int decode_fsinfo(struct xdr_stream *xdr, struct nfs_fsinfo *fsinfo)
2724 {
2725         uint32_t *savep;
2726         uint32_t attrlen, bitmap[2];
2727         int status;
2728
2729         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2730                 goto xdr_error;
2731         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2732                 goto xdr_error;
2733         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2734                 goto xdr_error;
2735
2736         fsinfo->rtmult = fsinfo->wtmult = 512;  /* ??? */
2737
2738         if ((status = decode_attr_lease_time(xdr, bitmap, &fsinfo->lease_time)) != 0)
2739                 goto xdr_error;
2740         if ((status = decode_attr_maxfilesize(xdr, bitmap, &fsinfo->maxfilesize)) != 0)
2741                 goto xdr_error;
2742         if ((status = decode_attr_maxread(xdr, bitmap, &fsinfo->rtmax)) != 0)
2743                 goto xdr_error;
2744         fsinfo->rtpref = fsinfo->dtpref = fsinfo->rtmax;
2745         if ((status = decode_attr_maxwrite(xdr, bitmap, &fsinfo->wtmax)) != 0)
2746                 goto xdr_error;
2747         fsinfo->wtpref = fsinfo->wtmax;
2748
2749         status = verify_attr_len(xdr, savep, attrlen);
2750 xdr_error:
2751         if (status != 0)
2752                 printk(KERN_NOTICE "%s: xdr error %d!\n", __FUNCTION__, -status);
2753         return status;
2754 }
2755
2756 static int decode_getfh(struct xdr_stream *xdr, struct nfs_fh *fh)
2757 {
2758         uint32_t *p;
2759         uint32_t len;
2760         int status;
2761
2762         status = decode_op_hdr(xdr, OP_GETFH);
2763         if (status)
2764                 return status;
2765         /* Zero handle first to allow comparisons */
2766         memset(fh, 0, sizeof(*fh));
2767
2768         READ_BUF(4);
2769         READ32(len);
2770         if (len > NFS4_FHSIZE)
2771                 return -EIO;
2772         fh->size = len;
2773         READ_BUF(len);
2774         COPYMEM(fh->data, len);
2775         return 0;
2776 }
2777
2778 static int decode_link(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
2779 {
2780         int status;
2781         
2782         status = decode_op_hdr(xdr, OP_LINK);
2783         if (status)
2784                 return status;
2785         return decode_change_info(xdr, cinfo);
2786 }
2787
2788 /*
2789  * We create the owner, so we know a proper owner.id length is 4.
2790  */
2791 static int decode_lock_denied (struct xdr_stream *xdr, struct nfs_lock_denied *denied)
2792 {
2793         uint32_t *p;
2794         uint32_t namelen;
2795
2796         READ_BUF(32);
2797         READ64(denied->offset);
2798         READ64(denied->length);
2799         READ32(denied->type);
2800         READ64(denied->owner.clientid);
2801         READ32(namelen);
2802         READ_BUF(namelen);
2803         if (namelen == 4)
2804                 READ32(denied->owner.id);
2805         return -NFS4ERR_DENIED;
2806 }
2807
2808 static int decode_lock(struct xdr_stream *xdr, struct nfs_lockres *res)
2809 {
2810         uint32_t *p;
2811         int status;
2812
2813         status = decode_op_hdr(xdr, OP_LOCK);
2814         if (status == 0) {
2815                 READ_BUF(sizeof(nfs4_stateid));
2816                 COPYMEM(&res->u.stateid, sizeof(res->u.stateid));
2817         } else if (status == -NFS4ERR_DENIED)
2818                 return decode_lock_denied(xdr, &res->u.denied);
2819         return status;
2820 }
2821
2822 static int decode_lockt(struct xdr_stream *xdr, struct nfs_lockres *res)
2823 {
2824         int status;
2825         status = decode_op_hdr(xdr, OP_LOCKT);
2826         if (status == -NFS4ERR_DENIED)
2827                 return decode_lock_denied(xdr, &res->u.denied);
2828         return status;
2829 }
2830
2831 static int decode_locku(struct xdr_stream *xdr, struct nfs_lockres *res)
2832 {
2833         uint32_t *p;
2834         int status;
2835
2836         status = decode_op_hdr(xdr, OP_LOCKU);
2837         if (status == 0) {
2838                 READ_BUF(sizeof(nfs4_stateid));
2839                 COPYMEM(&res->u.stateid, sizeof(res->u.stateid));
2840         }
2841         return status;
2842 }
2843
2844 static int decode_lookup(struct xdr_stream *xdr)
2845 {
2846         return decode_op_hdr(xdr, OP_LOOKUP);
2847 }
2848
2849 /* This is too sick! */
2850 static int decode_space_limit(struct xdr_stream *xdr, u64 *maxsize)
2851 {
2852         uint32_t *p;
2853         uint32_t limit_type, nblocks, blocksize;
2854
2855         READ_BUF(12);
2856         READ32(limit_type);
2857         switch (limit_type) {
2858                 case 1:
2859                         READ64(*maxsize);
2860                         break;
2861                 case 2:
2862                         READ32(nblocks);
2863                         READ32(blocksize);
2864                         *maxsize = (uint64_t)nblocks * (uint64_t)blocksize;
2865         }
2866         return 0;
2867 }
2868
2869 static int decode_delegation(struct xdr_stream *xdr, struct nfs_openres *res)
2870 {
2871         uint32_t *p;
2872         uint32_t delegation_type;
2873
2874         READ_BUF(4);
2875         READ32(delegation_type);
2876         if (delegation_type == NFS4_OPEN_DELEGATE_NONE) {
2877                 res->delegation_type = 0;
2878                 return 0;
2879         }
2880         READ_BUF(20);
2881         COPYMEM(res->delegation.data, sizeof(res->delegation.data));
2882         READ32(res->do_recall);
2883         switch (delegation_type) {
2884                 case NFS4_OPEN_DELEGATE_READ:
2885                         res->delegation_type = FMODE_READ;
2886                         break;
2887                 case NFS4_OPEN_DELEGATE_WRITE:
2888                         res->delegation_type = FMODE_WRITE|FMODE_READ;
2889                         if (decode_space_limit(xdr, &res->maxsize) < 0)
2890                                 return -EIO;
2891         }
2892         return decode_ace(xdr, NULL, res->server->nfs4_state);
2893 }
2894
2895 static int decode_open(struct xdr_stream *xdr, struct nfs_openres *res)
2896 {
2897         uint32_t *p;
2898         uint32_t bmlen;
2899         int status;
2900
2901         status = decode_op_hdr(xdr, OP_OPEN);
2902         if (status)
2903                 return status;
2904         READ_BUF(sizeof(res->stateid.data));
2905         COPYMEM(res->stateid.data, sizeof(res->stateid.data));
2906
2907         decode_change_info(xdr, &res->cinfo);
2908
2909         READ_BUF(8);
2910         READ32(res->rflags);
2911         READ32(bmlen);
2912         if (bmlen > 10)
2913                 goto xdr_error;
2914
2915         READ_BUF(bmlen << 2);
2916         p += bmlen;
2917         return decode_delegation(xdr, res);
2918 xdr_error:
2919         printk(KERN_NOTICE "%s: xdr error!\n", __FUNCTION__);
2920         return -EIO;
2921 }
2922
2923 static int decode_open_confirm(struct xdr_stream *xdr, struct nfs_open_confirmres *res)
2924 {
2925         uint32_t *p;
2926         int status;
2927
2928         status = decode_op_hdr(xdr, OP_OPEN_CONFIRM);
2929         if (status)
2930                 return status;
2931         READ_BUF(sizeof(res->stateid.data));
2932         COPYMEM(res->stateid.data, sizeof(res->stateid.data));
2933         return 0;
2934 }
2935
2936 static int decode_open_downgrade(struct xdr_stream *xdr, struct nfs_closeres *res)
2937 {
2938         uint32_t *p;
2939         int status;
2940
2941         status = decode_op_hdr(xdr, OP_OPEN_DOWNGRADE);
2942         if (status)
2943                 return status;
2944         READ_BUF(sizeof(res->stateid.data));
2945         COPYMEM(res->stateid.data, sizeof(res->stateid.data));
2946         return 0;
2947 }
2948
2949 static int decode_putfh(struct xdr_stream *xdr)
2950 {
2951         return decode_op_hdr(xdr, OP_PUTFH);
2952 }
2953
2954 static int decode_putrootfh(struct xdr_stream *xdr)
2955 {
2956         return decode_op_hdr(xdr, OP_PUTROOTFH);
2957 }
2958
2959 static int decode_read(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs_readres *res)
2960 {
2961         struct kvec *iov = req->rq_rcv_buf.head;
2962         uint32_t *p;
2963         uint32_t count, eof, recvd, hdrlen;
2964         int status;
2965
2966         status = decode_op_hdr(xdr, OP_READ);
2967         if (status)
2968                 return status;
2969         READ_BUF(8);
2970         READ32(eof);
2971         READ32(count);
2972         hdrlen = (u8 *) p - (u8 *) iov->iov_base;
2973         recvd = req->rq_rcv_buf.len - hdrlen;
2974         if (count > recvd) {
2975                 printk(KERN_WARNING "NFS: server cheating in read reply: "
2976                                 "count %u > recvd %u\n", count, recvd);
2977                 count = recvd;
2978                 eof = 0;
2979         }
2980         xdr_read_pages(xdr, count);
2981         res->eof = eof;
2982         res->count = count;
2983         return 0;
2984 }
2985
2986 static int decode_readdir(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs4_readdir_res *readdir)
2987 {
2988         struct xdr_buf  *rcvbuf = &req->rq_rcv_buf;
2989         struct page     *page = *rcvbuf->pages;
2990         struct kvec     *iov = rcvbuf->head;
2991         unsigned int    nr, pglen = rcvbuf->page_len;
2992         uint32_t        *end, *entry, *p, *kaddr;
2993         uint32_t        len, attrlen;
2994         int             hdrlen, recvd, status;
2995
2996         status = decode_op_hdr(xdr, OP_READDIR);
2997         if (status)
2998                 return status;
2999         READ_BUF(8);
3000         COPYMEM(readdir->verifier.data, 8);
3001
3002         hdrlen = (char *) p - (char *) iov->iov_base;
3003         recvd = rcvbuf->len - hdrlen;
3004         if (pglen > recvd)
3005                 pglen = recvd;
3006         xdr_read_pages(xdr, pglen);
3007
3008         BUG_ON(pglen + readdir->pgbase > PAGE_CACHE_SIZE);
3009         kaddr = p = (uint32_t *) kmap_atomic(page, KM_USER0);
3010         end = (uint32_t *) ((char *)p + pglen + readdir->pgbase);
3011         entry = p;
3012         for (nr = 0; *p++; nr++) {
3013                 if (p + 3 > end)
3014                         goto short_pkt;
3015                 p += 2;                 /* cookie */
3016                 len = ntohl(*p++);      /* filename length */
3017                 if (len > NFS4_MAXNAMLEN) {
3018                         printk(KERN_WARNING "NFS: giant filename in readdir (len 0x%x)\n", len);
3019                         goto err_unmap;
3020                 }
3021                 p += XDR_QUADLEN(len);
3022                 if (p + 1 > end)
3023                         goto short_pkt;
3024                 len = ntohl(*p++);      /* bitmap length */
3025                 p += len;
3026                 if (p + 1 > end)
3027                         goto short_pkt;
3028                 attrlen = XDR_QUADLEN(ntohl(*p++));
3029                 p += attrlen;           /* attributes */
3030                 if (p + 2 > end)
3031                         goto short_pkt;
3032                 entry = p;
3033         }
3034         if (!nr && (entry[0] != 0 || entry[1] == 0))
3035                 goto short_pkt;
3036 out:    
3037         kunmap_atomic(kaddr, KM_USER0);
3038         return 0;
3039 short_pkt:
3040         entry[0] = entry[1] = 0;
3041         /* truncate listing ? */
3042         if (!nr) {
3043                 printk(KERN_NOTICE "NFS: readdir reply truncated!\n");
3044                 entry[1] = 1;
3045         }
3046         goto out;
3047 err_unmap:
3048         kunmap_atomic(kaddr, KM_USER0);
3049         return -errno_NFSERR_IO;
3050 }
3051
3052 static int decode_readlink(struct xdr_stream *xdr, struct rpc_rqst *req)
3053 {
3054         struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
3055         struct kvec *iov = rcvbuf->head;
3056         int hdrlen, len, recvd;
3057         uint32_t *p;
3058         char *kaddr;
3059         int status;
3060
3061         status = decode_op_hdr(xdr, OP_READLINK);
3062         if (status)
3063                 return status;
3064
3065         /* Convert length of symlink */
3066         READ_BUF(4);
3067         READ32(len);
3068         if (len >= rcvbuf->page_len || len <= 0) {
3069                 dprintk(KERN_WARNING "nfs: server returned giant symlink!\n");
3070                 return -ENAMETOOLONG;
3071         }
3072         hdrlen = (char *) xdr->p - (char *) iov->iov_base;
3073         recvd = req->rq_rcv_buf.len - hdrlen;
3074         if (recvd < len) {
3075                 printk(KERN_WARNING "NFS: server cheating in readlink reply: "
3076                                 "count %u > recvd %u\n", len, recvd);
3077                 return -EIO;
3078         }
3079         xdr_read_pages(xdr, len);
3080         /*
3081          * The XDR encode routine has set things up so that
3082          * the link text will be copied directly into the
3083          * buffer.  We just have to do overflow-checking,
3084          * and and null-terminate the text (the VFS expects
3085          * null-termination).
3086          */
3087         kaddr = (char *)kmap_atomic(rcvbuf->pages[0], KM_USER0);
3088         kaddr[len+rcvbuf->page_base] = '\0';
3089         kunmap_atomic(kaddr, KM_USER0);
3090         return 0;
3091 }
3092
3093 static int decode_remove(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
3094 {
3095         int status;
3096
3097         status = decode_op_hdr(xdr, OP_REMOVE);
3098         if (status)
3099                 goto out;
3100         status = decode_change_info(xdr, cinfo);
3101 out:
3102         return status;
3103 }
3104
3105 static int decode_rename(struct xdr_stream *xdr, struct nfs4_change_info *old_cinfo,
3106               struct nfs4_change_info *new_cinfo)
3107 {
3108         int status;
3109
3110         status = decode_op_hdr(xdr, OP_RENAME);
3111         if (status)
3112                 goto out;
3113         if ((status = decode_change_info(xdr, old_cinfo)))
3114                 goto out;
3115         status = decode_change_info(xdr, new_cinfo);
3116 out:
3117         return status;
3118 }
3119
3120 static int decode_renew(struct xdr_stream *xdr)
3121 {
3122         return decode_op_hdr(xdr, OP_RENEW);
3123 }
3124
3125 static int
3126 decode_savefh(struct xdr_stream *xdr)
3127 {
3128         return decode_op_hdr(xdr, OP_SAVEFH);
3129 }
3130
3131 static int decode_setattr(struct xdr_stream *xdr, struct nfs_setattrres *res)
3132 {
3133         uint32_t *p;
3134         uint32_t bmlen;
3135         int status;
3136
3137         
3138         status = decode_op_hdr(xdr, OP_SETATTR);
3139         if (status)
3140                 return status;
3141         READ_BUF(4);
3142         READ32(bmlen);
3143         READ_BUF(bmlen << 2);
3144         return 0;
3145 }
3146
3147 static int decode_setclientid(struct xdr_stream *xdr, struct nfs4_client *clp)
3148 {
3149         uint32_t *p;
3150         uint32_t opnum;
3151         int32_t nfserr;
3152
3153         READ_BUF(8);
3154         READ32(opnum);
3155         if (opnum != OP_SETCLIENTID) {
3156                 printk(KERN_NOTICE
3157                                 "nfs4_decode_setclientid: Server returned operation"
3158                                 " %d\n", opnum);
3159                 return -EIO;
3160         }
3161         READ32(nfserr);
3162         if (nfserr == NFS_OK) {
3163                 READ_BUF(8 + sizeof(clp->cl_confirm.data));
3164                 READ64(clp->cl_clientid);
3165                 COPYMEM(clp->cl_confirm.data, sizeof(clp->cl_confirm.data));
3166         } else if (nfserr == NFSERR_CLID_INUSE) {
3167                 uint32_t len;
3168
3169                 /* skip netid string */
3170                 READ_BUF(4);
3171                 READ32(len);
3172                 READ_BUF(len);
3173
3174                 /* skip uaddr string */
3175                 READ_BUF(4);
3176                 READ32(len);
3177                 READ_BUF(len);
3178                 return -EEXIST;
3179         } else
3180                 return -nfs_stat_to_errno(nfserr);
3181
3182         return 0;
3183 }
3184
3185 static int decode_setclientid_confirm(struct xdr_stream *xdr)
3186 {
3187         return decode_op_hdr(xdr, OP_SETCLIENTID_CONFIRM);
3188 }
3189
3190 static int decode_write(struct xdr_stream *xdr, struct nfs_writeres *res)
3191 {
3192         uint32_t *p;
3193         int status;
3194
3195         status = decode_op_hdr(xdr, OP_WRITE);
3196         if (status)
3197                 return status;
3198
3199         READ_BUF(16);
3200         READ32(res->count);
3201         READ32(res->verf->committed);
3202         COPYMEM(res->verf->verifier, 8);
3203         return 0;
3204 }
3205
3206 static int decode_delegreturn(struct xdr_stream *xdr)
3207 {
3208         return decode_op_hdr(xdr, OP_DELEGRETURN);
3209 }
3210
3211 /*
3212  * Decode OPEN_DOWNGRADE response
3213  */
3214 static int nfs4_xdr_dec_open_downgrade(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_closeres *res)
3215 {
3216         struct xdr_stream xdr;
3217         struct compound_hdr hdr;
3218         int status;
3219
3220         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3221         status = decode_compound_hdr(&xdr, &hdr);
3222         if (status)
3223                 goto out;
3224         status = decode_putfh(&xdr);
3225         if (status)
3226                 goto out;
3227         status = decode_open_downgrade(&xdr, res);
3228 out:
3229         return status;
3230 }
3231
3232 /*
3233  * END OF "GENERIC" DECODE ROUTINES.
3234  */
3235
3236 /*
3237  * Decode ACCESS response
3238  */
3239 static int nfs4_xdr_dec_access(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_accessres *res)
3240 {
3241         struct xdr_stream xdr;
3242         struct compound_hdr hdr;
3243         int status;
3244         
3245         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3246         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3247                 goto out;
3248         if ((status = decode_putfh(&xdr)) == 0)
3249                 status = decode_access(&xdr, res);
3250 out:
3251         return status;
3252 }
3253
3254 /*
3255  * Decode LOOKUP response
3256  */
3257 static int nfs4_xdr_dec_lookup(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_lookup_res *res)
3258 {
3259         struct xdr_stream xdr;
3260         struct compound_hdr hdr;
3261         int status;
3262         
3263         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3264         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3265                 goto out;
3266         if ((status = decode_putfh(&xdr)) != 0)
3267                 goto out;
3268         if ((status = decode_lookup(&xdr)) != 0)
3269                 goto out;
3270         if ((status = decode_getfh(&xdr, res->fh)) != 0)
3271                 goto out;
3272         status = decode_getfattr(&xdr, res->fattr, res->server);
3273 out:
3274         return status;
3275 }
3276
3277 /*
3278  * Decode LOOKUP_ROOT response
3279  */
3280 static int nfs4_xdr_dec_lookup_root(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_lookup_res *res)
3281 {
3282         struct xdr_stream xdr;
3283         struct compound_hdr hdr;
3284         int status;
3285         
3286         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3287         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3288                 goto out;
3289         if ((status = decode_putrootfh(&xdr)) != 0)
3290                 goto out;
3291         if ((status = decode_getfh(&xdr, res->fh)) == 0)
3292                 status = decode_getfattr(&xdr, res->fattr, res->server);
3293 out:
3294         return status;
3295 }
3296
3297 /*
3298  * Decode REMOVE response
3299  */
3300 static int nfs4_xdr_dec_remove(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_change_info *cinfo)
3301 {
3302         struct xdr_stream xdr;
3303         struct compound_hdr hdr;
3304         int status;
3305         
3306         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3307         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3308                 goto out;
3309         if ((status = decode_putfh(&xdr)) == 0)
3310                 status = decode_remove(&xdr, cinfo);
3311 out:
3312         return status;
3313 }
3314
3315 /*
3316  * Decode RENAME response
3317  */
3318 static int nfs4_xdr_dec_rename(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_rename_res *res)
3319 {
3320         struct xdr_stream xdr;
3321         struct compound_hdr hdr;
3322         int status;
3323         
3324         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3325         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3326                 goto out;
3327         if ((status = decode_putfh(&xdr)) != 0)
3328                 goto out;
3329         if ((status = decode_savefh(&xdr)) != 0)
3330                 goto out;
3331         if ((status = decode_putfh(&xdr)) != 0)
3332                 goto out;
3333         status = decode_rename(&xdr, &res->old_cinfo, &res->new_cinfo);
3334 out:
3335         return status;
3336 }
3337
3338 /*
3339  * Decode LINK response
3340  */
3341 static int nfs4_xdr_dec_link(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_change_info *cinfo)
3342 {
3343         struct xdr_stream xdr;
3344         struct compound_hdr hdr;
3345         int status;
3346         
3347         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3348         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3349                 goto out;
3350         if ((status = decode_putfh(&xdr)) != 0)
3351                 goto out;
3352         if ((status = decode_savefh(&xdr)) != 0)
3353                 goto out;
3354         if ((status = decode_putfh(&xdr)) != 0)
3355                 goto out;
3356         status = decode_link(&xdr, cinfo);
3357 out:
3358         return status;
3359 }
3360
3361 /*
3362  * Decode CREATE response
3363  */
3364 static int nfs4_xdr_dec_create(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_create_res *res)
3365 {
3366         struct xdr_stream xdr;
3367         struct compound_hdr hdr;
3368         int status;
3369         
3370         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3371         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3372                 goto out;
3373         if ((status = decode_putfh(&xdr)) != 0)
3374                 goto out;
3375         if ((status = decode_create(&xdr,&res->dir_cinfo)) != 0)
3376                 goto out;
3377         if ((status = decode_getfh(&xdr, res->fh)) != 0)
3378                 goto out;
3379         status = decode_getfattr(&xdr, res->fattr, res->server);
3380         if (status == NFS4ERR_DELAY)
3381                 status = 0;
3382 out:
3383         return status;
3384 }
3385
3386 /*
3387  * Decode SYMLINK response
3388  */
3389 static int nfs4_xdr_dec_symlink(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_create_res *res)
3390 {
3391         return nfs4_xdr_dec_create(rqstp, p, res);
3392 }
3393
3394 /*
3395  * Decode GETATTR response
3396  */
3397 static int nfs4_xdr_dec_getattr(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_getattr_res *res)
3398 {
3399         struct xdr_stream xdr;
3400         struct compound_hdr hdr;
3401         int status;
3402         
3403         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3404         status = decode_compound_hdr(&xdr, &hdr);
3405         if (status)
3406                 goto out;
3407         status = decode_putfh(&xdr);
3408         if (status)
3409                 goto out;
3410         status = decode_getfattr(&xdr, res->fattr, res->server);
3411 out:
3412         return status;
3413
3414 }
3415
3416
3417 /*
3418  * Decode CLOSE response
3419  */
3420 static int nfs4_xdr_dec_close(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_closeres *res)
3421 {
3422         struct xdr_stream xdr;
3423         struct compound_hdr hdr;
3424         int status;
3425
3426         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3427         status = decode_compound_hdr(&xdr, &hdr);
3428         if (status)
3429                 goto out;
3430         status = decode_putfh(&xdr);
3431         if (status)
3432                 goto out;
3433         status = decode_close(&xdr, res);
3434 out:
3435         return status;
3436 }
3437
3438 /*
3439  * Decode OPEN response
3440  */
3441 static int nfs4_xdr_dec_open(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_openres *res)
3442 {
3443         struct xdr_stream xdr;
3444         struct compound_hdr hdr;
3445         int status;
3446
3447         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3448         status = decode_compound_hdr(&xdr, &hdr);
3449         if (status)
3450                 goto out;
3451         status = decode_putfh(&xdr);
3452         if (status)
3453                 goto out;
3454         status = decode_open(&xdr, res);
3455         if (status)
3456                 goto out;
3457         status = decode_getfh(&xdr, &res->fh);
3458         if (status)
3459                 goto out;
3460         status = decode_getfattr(&xdr, res->f_attr, res->server);
3461         if (status == NFS4ERR_DELAY)
3462                 status = 0;
3463 out:
3464         return status;
3465 }
3466
3467 /*
3468  * Decode OPEN_CONFIRM response
3469  */
3470 static int nfs4_xdr_dec_open_confirm(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_open_confirmres *res)
3471 {
3472         struct xdr_stream xdr;
3473         struct compound_hdr hdr;
3474         int status;
3475
3476         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3477         status = decode_compound_hdr(&xdr, &hdr);
3478         if (status)
3479                 goto out;
3480         status = decode_putfh(&xdr);
3481         if (status)
3482                 goto out;
3483         status = decode_open_confirm(&xdr, res);
3484 out:
3485         return status;
3486 }
3487
3488 /*
3489  * Decode OPEN response
3490  */
3491 static int nfs4_xdr_dec_open_noattr(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_openres *res)
3492 {
3493         struct xdr_stream xdr;
3494         struct compound_hdr hdr;
3495         int status;
3496
3497         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3498         status = decode_compound_hdr(&xdr, &hdr);
3499         if (status)
3500                 goto out;
3501         status = decode_putfh(&xdr);
3502         if (status)
3503                 goto out;
3504         status = decode_open(&xdr, res);
3505 out:
3506         return status;
3507 }
3508
3509 /*
3510  * Decode SETATTR response
3511  */
3512 static int nfs4_xdr_dec_setattr(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_setattrres *res)
3513 {
3514         struct xdr_stream xdr;
3515         struct compound_hdr hdr;
3516         int status;
3517
3518         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3519         status = decode_compound_hdr(&xdr, &hdr);
3520         if (status)
3521                 goto out;
3522         status = decode_putfh(&xdr);
3523         if (status)
3524                 goto out;
3525         status = decode_setattr(&xdr, res);
3526         if (status)
3527                 goto out;
3528         status = decode_getfattr(&xdr, res->fattr, res->server);
3529         if (status == NFS4ERR_DELAY)
3530                 status = 0;
3531 out:
3532         return status;
3533 }
3534
3535 /*
3536  * Decode LOCK response
3537  */
3538 static int nfs4_xdr_dec_lock(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_lockres *res)
3539 {
3540         struct xdr_stream xdr;
3541         struct compound_hdr hdr;
3542         int status;
3543
3544         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3545         status = decode_compound_hdr(&xdr, &hdr);
3546         if (status)
3547                 goto out;
3548         status = decode_putfh(&xdr);
3549         if (status)
3550                 goto out;
3551         status = decode_lock(&xdr, res);
3552 out:
3553         return status;
3554 }
3555
3556 /*
3557  * Decode LOCKT response
3558  */
3559 static int nfs4_xdr_dec_lockt(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_lockres *res)
3560 {
3561         struct xdr_stream xdr;
3562         struct compound_hdr hdr;
3563         int status;
3564
3565         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3566         status = decode_compound_hdr(&xdr, &hdr);
3567         if (status)
3568                 goto out;
3569         status = decode_putfh(&xdr);
3570         if (status)
3571                 goto out;
3572         status = decode_lockt(&xdr, res);
3573 out:
3574         return status;
3575 }
3576
3577 /*
3578  * Decode LOCKU response
3579  */
3580 static int nfs4_xdr_dec_locku(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_lockres *res)
3581 {
3582         struct xdr_stream xdr;
3583         struct compound_hdr hdr;
3584         int status;
3585
3586         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3587         status = decode_compound_hdr(&xdr, &hdr);
3588         if (status)
3589                 goto out;
3590         status = decode_putfh(&xdr);
3591         if (status)
3592                 goto out;
3593         status = decode_locku(&xdr, res);
3594 out:
3595         return status;
3596 }
3597
3598 /*
3599  * Decode READLINK response
3600  */
3601 static int nfs4_xdr_dec_readlink(struct rpc_rqst *rqstp, uint32_t *p, void *res)
3602 {
3603         struct xdr_stream xdr;
3604         struct compound_hdr hdr;
3605         int status;
3606
3607         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3608         status = decode_compound_hdr(&xdr, &hdr);
3609         if (status)
3610                 goto out;
3611         status = decode_putfh(&xdr);
3612         if (status)
3613                 goto out;
3614         status = decode_readlink(&xdr, rqstp);
3615 out:
3616         return status;
3617 }
3618
3619 /*
3620  * Decode READDIR response
3621  */
3622 static int nfs4_xdr_dec_readdir(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_readdir_res *res)
3623 {
3624         struct xdr_stream xdr;
3625         struct compound_hdr hdr;
3626         int status;
3627
3628         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3629         status = decode_compound_hdr(&xdr, &hdr);
3630         if (status)
3631                 goto out;
3632         status = decode_putfh(&xdr);
3633         if (status)
3634                 goto out;
3635         status = decode_readdir(&xdr, rqstp, res);
3636 out:
3637         return status;
3638 }
3639
3640 /*
3641  * Decode Read response
3642  */
3643 static int nfs4_xdr_dec_read(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_readres *res)
3644 {
3645         struct xdr_stream xdr;
3646         struct compound_hdr hdr;
3647         int status;
3648
3649         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3650         status = decode_compound_hdr(&xdr, &hdr);
3651         if (status)
3652                 goto out;
3653         status = decode_putfh(&xdr);
3654         if (status)
3655                 goto out;
3656         status = decode_read(&xdr, rqstp, res);
3657         if (!status)
3658                 status = res->count;
3659 out:
3660         return status;
3661 }
3662
3663 /*
3664  * Decode WRITE response
3665  */
3666 static int nfs4_xdr_dec_write(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_writeres *res)
3667 {
3668         struct xdr_stream xdr;
3669         struct compound_hdr hdr;
3670         int status;
3671
3672         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3673         status = decode_compound_hdr(&xdr, &hdr);
3674         if (status)
3675                 goto out;
3676         status = decode_putfh(&xdr);
3677         if (status)
3678                 goto out;
3679         status = decode_write(&xdr, res);
3680         if (!status)
3681                 status = res->count;
3682 out:
3683         return status;
3684 }
3685
3686 /*
3687  * Decode COMMIT response
3688  */
3689 static int nfs4_xdr_dec_commit(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_writeres *res)
3690 {
3691         struct xdr_stream xdr;
3692         struct compound_hdr hdr;
3693         int status;
3694
3695         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3696         status = decode_compound_hdr(&xdr, &hdr);
3697         if (status)
3698                 goto out;
3699         status = decode_putfh(&xdr);
3700         if (status)
3701                 goto out;
3702         status = decode_commit(&xdr, res);
3703 out:
3704         return status;
3705 }
3706
3707 /*
3708  * FSINFO request
3709  */
3710 static int nfs4_xdr_dec_fsinfo(struct rpc_rqst *req, uint32_t *p, struct nfs_fsinfo *fsinfo)
3711 {
3712         struct xdr_stream xdr;
3713         struct compound_hdr hdr;
3714         int status;
3715
3716         xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
3717         status = decode_compound_hdr(&xdr, &hdr);
3718         if (!status)
3719                 status = decode_putfh(&xdr);
3720         if (!status)
3721                 status = decode_fsinfo(&xdr, fsinfo);
3722         if (!status)
3723                 status = -nfs_stat_to_errno(hdr.status);
3724         return status;
3725 }
3726
3727 /*
3728  * PATHCONF request
3729  */
3730 static int nfs4_xdr_dec_pathconf(struct rpc_rqst *req, uint32_t *p, struct nfs_pathconf *pathconf)
3731 {
3732         struct xdr_stream xdr;
3733         struct compound_hdr hdr;
3734         int status;
3735
3736         xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
3737         status = decode_compound_hdr(&xdr, &hdr);
3738         if (!status)
3739                 status = decode_putfh(&xdr);
3740         if (!status)
3741                 status = decode_pathconf(&xdr, pathconf);
3742         return status;
3743 }
3744
3745 /*
3746  * STATFS request
3747  */
3748 static int nfs4_xdr_dec_statfs(struct rpc_rqst *req, uint32_t *p, struct nfs_fsstat *fsstat)
3749 {
3750         struct xdr_stream xdr;
3751         struct compound_hdr hdr;
3752         int status;
3753
3754         xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
3755         status = decode_compound_hdr(&xdr, &hdr);
3756         if (!status)
3757                 status = decode_putfh(&xdr);
3758         if (!status)
3759                 status = decode_statfs(&xdr, fsstat);
3760         return status;
3761 }
3762
3763 /*
3764  * GETATTR_BITMAP request
3765  */
3766 static int nfs4_xdr_dec_server_caps(struct rpc_rqst *req, uint32_t *p, struct nfs4_server_caps_res *res)
3767 {
3768         struct xdr_stream xdr;
3769         struct compound_hdr hdr;
3770         int status;
3771
3772         xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
3773         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3774                 goto out;
3775         if ((status = decode_putfh(&xdr)) != 0)
3776                 goto out;
3777         status = decode_server_caps(&xdr, res);
3778 out:
3779         return status;
3780 }
3781
3782 /*
3783  * Decode RENEW response
3784  */
3785 static int nfs4_xdr_dec_renew(struct rpc_rqst *rqstp, uint32_t *p, void *dummy)
3786 {
3787         struct xdr_stream xdr;
3788         struct compound_hdr hdr;
3789         int status;
3790
3791         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3792         status = decode_compound_hdr(&xdr, &hdr);
3793         if (!status)
3794                 status = decode_renew(&xdr);
3795         return status;
3796 }
3797
3798 /*
3799  * a SETCLIENTID request
3800  */
3801 static int nfs4_xdr_dec_setclientid(struct rpc_rqst *req, uint32_t *p,
3802                 struct nfs4_client *clp)
3803 {
3804         struct xdr_stream xdr;
3805         struct compound_hdr hdr;
3806         int status;
3807
3808         xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
3809         status = decode_compound_hdr(&xdr, &hdr);
3810         if (!status)
3811                 status = decode_setclientid(&xdr, clp);
3812         if (!status)
3813                 status = -nfs_stat_to_errno(hdr.status);
3814         return status;
3815 }
3816
3817 /*
3818  * a SETCLIENTID_CONFIRM request
3819  */
3820 static int nfs4_xdr_dec_setclientid_confirm(struct rpc_rqst *req, uint32_t *p, struct nfs_fsinfo *fsinfo)
3821 {
3822         struct xdr_stream xdr;
3823         struct compound_hdr hdr;
3824         int status;
3825
3826         xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
3827         status = decode_compound_hdr(&xdr, &hdr);
3828         if (!status)
3829                 status = decode_setclientid_confirm(&xdr);
3830         if (!status)
3831                 status = decode_putrootfh(&xdr);
3832         if (!status)
3833                 status = decode_fsinfo(&xdr, fsinfo);
3834         if (!status)
3835                 status = -nfs_stat_to_errno(hdr.status);
3836         return status;
3837 }
3838
3839 /*
3840  * DELEGRETURN request
3841  */
3842 static int nfs4_xdr_dec_delegreturn(struct rpc_rqst *rqstp, uint32_t *p, void *dummy)
3843 {
3844         struct xdr_stream xdr;
3845         struct compound_hdr hdr;
3846         int status;
3847
3848         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3849         status = decode_compound_hdr(&xdr, &hdr);
3850         if (status == 0) {
3851                 status = decode_putfh(&xdr);
3852                 if (status == 0)
3853                         status = decode_delegreturn(&xdr);
3854         }
3855         return status;
3856 }
3857
3858 uint32_t *nfs4_decode_dirent(uint32_t *p, struct nfs_entry *entry, int plus)
3859 {
3860         uint32_t bitmap[1] = {0};
3861         uint32_t len;
3862
3863         if (!*p++) {
3864                 if (!*p)
3865                         return ERR_PTR(-EAGAIN);
3866                 entry->eof = 1;
3867                 return ERR_PTR(-EBADCOOKIE);
3868         }
3869
3870         entry->prev_cookie = entry->cookie;
3871         p = xdr_decode_hyper(p, &entry->cookie);
3872         entry->len = ntohl(*p++);
3873         entry->name = (const char *) p;
3874         p += XDR_QUADLEN(entry->len);
3875
3876         /*
3877          * In case the server doesn't return an inode number,
3878          * we fake one here.  (We don't use inode number 0,
3879          * since glibc seems to choke on it...)
3880          */
3881         entry->ino = 1;
3882
3883         len = ntohl(*p++);              /* bitmap length */
3884         if (len > 0) {
3885                 bitmap[0] = ntohl(*p);
3886                 p += len;
3887         }
3888         len = XDR_QUADLEN(ntohl(*p++)); /* attribute buffer length */
3889         if (len > 0) {
3890                 if (bitmap[0] == FATTR4_WORD0_FILEID)
3891                         xdr_decode_hyper(p, &entry->ino);
3892                 p += len;
3893         }
3894
3895         entry->eof = !p[0] && p[1];
3896         return p;
3897 }
3898
3899 /*
3900  * We need to translate between nfs status return values and
3901  * the local errno values which may not be the same.
3902  */
3903 static struct {
3904         int stat;
3905         int errno;
3906 } nfs_errtbl[] = {
3907         { NFS4_OK,              0               },
3908         { NFS4ERR_PERM,         EPERM           },
3909         { NFS4ERR_NOENT,        ENOENT          },
3910         { NFS4ERR_IO,           errno_NFSERR_IO },
3911         { NFS4ERR_NXIO,         ENXIO           },
3912         { NFS4ERR_ACCESS,       EACCES          },
3913         { NFS4ERR_EXIST,        EEXIST          },
3914         { NFS4ERR_XDEV,         EXDEV           },
3915         { NFS4ERR_NOTDIR,       ENOTDIR         },
3916         { NFS4ERR_ISDIR,        EISDIR          },
3917         { NFS4ERR_INVAL,        EINVAL          },
3918         { NFS4ERR_FBIG,         EFBIG           },
3919         { NFS4ERR_NOSPC,        ENOSPC          },
3920         { NFS4ERR_ROFS,         EROFS           },
3921         { NFS4ERR_MLINK,        EMLINK          },
3922         { NFS4ERR_NAMETOOLONG,  ENAMETOOLONG    },
3923         { NFS4ERR_NOTEMPTY,     ENOTEMPTY       },
3924         { NFS4ERR_DQUOT,        EDQUOT          },
3925         { NFS4ERR_STALE,        ESTALE          },
3926         { NFS4ERR_BADHANDLE,    EBADHANDLE      },
3927         { NFS4ERR_BAD_COOKIE,   EBADCOOKIE      },
3928         { NFS4ERR_NOTSUPP,      ENOTSUPP        },
3929         { NFS4ERR_TOOSMALL,     ETOOSMALL       },
3930         { NFS4ERR_SERVERFAULT,  ESERVERFAULT    },
3931         { NFS4ERR_BADTYPE,      EBADTYPE        },
3932         { NFS4ERR_LOCKED,       EAGAIN          },
3933         { NFS4ERR_RESOURCE,     EREMOTEIO       },
3934         { NFS4ERR_SYMLINK,      ELOOP           },
3935         { NFS4ERR_OP_ILLEGAL,   EOPNOTSUPP      },
3936         { NFS4ERR_DEADLOCK,     EDEADLK         },
3937         { NFS4ERR_WRONGSEC,     EPERM           }, /* FIXME: this needs
3938                                                     * to be handled by a
3939                                                     * middle-layer.
3940                                                     */
3941         { -1,                   EIO             }
3942 };
3943
3944 /*
3945  * Convert an NFS error code to a local one.
3946  * This one is used jointly by NFSv2 and NFSv3.
3947  */
3948 static int
3949 nfs_stat_to_errno(int stat)
3950 {
3951         int i;
3952         for (i = 0; nfs_errtbl[i].stat != -1; i++) {
3953                 if (nfs_errtbl[i].stat == stat)
3954                         return nfs_errtbl[i].errno;
3955         }
3956         if (stat <= 10000 || stat > 10100) {
3957                 /* The server is looney tunes. */
3958                 return ESERVERFAULT;
3959         }
3960         /* If we cannot translate the error, the recovery routines should
3961          * handle it.
3962          * Note: remaining NFSv4 error codes have values > 10000, so should
3963          * not conflict with native Linux error codes.
3964          */
3965         return stat;
3966 }
3967
3968 #ifndef MAX
3969 # define MAX(a, b)      (((a) > (b))? (a) : (b))
3970 #endif
3971
3972 #define PROC(proc, argtype, restype)                            \
3973 [NFSPROC4_CLNT_##proc] = {                                      \
3974         .p_proc   = NFSPROC4_COMPOUND,                          \
3975         .p_encode = (kxdrproc_t) nfs4_xdr_##argtype,            \
3976         .p_decode = (kxdrproc_t) nfs4_xdr_##restype,            \
3977         .p_bufsiz = MAX(NFS4_##argtype##_sz,NFS4_##restype##_sz) << 2,  \
3978     }
3979
3980 struct rpc_procinfo     nfs4_procedures[] = {
3981   PROC(READ,            enc_read,       dec_read),
3982   PROC(WRITE,           enc_write,      dec_write),
3983   PROC(COMMIT,          enc_commit,     dec_commit),
3984   PROC(OPEN,            enc_open,       dec_open),
3985   PROC(OPEN_CONFIRM,    enc_open_confirm,       dec_open_confirm),
3986   PROC(OPEN_NOATTR,     enc_open_noattr,        dec_open_noattr),
3987   PROC(OPEN_DOWNGRADE,  enc_open_downgrade,     dec_open_downgrade),
3988   PROC(CLOSE,           enc_close,      dec_close),
3989   PROC(SETATTR,         enc_setattr,    dec_setattr),
3990   PROC(FSINFO,          enc_fsinfo,     dec_fsinfo),
3991   PROC(RENEW,           enc_renew,      dec_renew),
3992   PROC(SETCLIENTID,     enc_setclientid,        dec_setclientid),
3993   PROC(SETCLIENTID_CONFIRM,     enc_setclientid_confirm,        dec_setclientid_confirm),
3994   PROC(LOCK,            enc_lock,       dec_lock),
3995   PROC(LOCKT,           enc_lockt,      dec_lockt),
3996   PROC(LOCKU,           enc_locku,      dec_locku),
3997   PROC(ACCESS,          enc_access,     dec_access),
3998   PROC(GETATTR,         enc_getattr,    dec_getattr),
3999   PROC(LOOKUP,          enc_lookup,     dec_lookup),
4000   PROC(LOOKUP_ROOT,     enc_lookup_root,        dec_lookup_root),
4001   PROC(REMOVE,          enc_remove,     dec_remove),
4002   PROC(RENAME,          enc_rename,     dec_rename),
4003   PROC(LINK,            enc_link,       dec_link),
4004   PROC(SYMLINK,         enc_symlink,    dec_symlink),
4005   PROC(CREATE,          enc_create,     dec_create),
4006   PROC(PATHCONF,        enc_pathconf,   dec_pathconf),
4007   PROC(STATFS,          enc_statfs,     dec_statfs),
4008   PROC(READLINK,        enc_readlink,   dec_readlink),
4009   PROC(READDIR,         enc_readdir,    dec_readdir),
4010   PROC(SERVER_CAPS,     enc_server_caps, dec_server_caps),
4011   PROC(DELEGRETURN,     enc_delegreturn, dec_delegreturn),
4012 };
4013
4014 struct rpc_version              nfs_version4 = {
4015         .number                 = 4,
4016         .nrprocs                = sizeof(nfs4_procedures)/sizeof(nfs4_procedures[0]),
4017         .procs                  = nfs4_procedures
4018 };
4019
4020 /*
4021  * Local variables:
4022  *  c-basic-offset: 8
4023  * End:
4024  */