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