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