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