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