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