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