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