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