7d914d9e7584d593eac7cea963df027bc8c62daa
[linux-2.6.git] / fs / nfs / nfs4xdr.c
1 /*
2  *  fs/nfs/nfs4xdr.c
3  *
4  *  Client-side XDR for NFSv4.
5  *
6  *  Copyright (c) 2002 The Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  Kendrick Smith <kmsmith@umich.edu>
10  *  Andy Adamson   <andros@umich.edu>
11  *
12  *  Redistribution and use in source and binary forms, with or without
13  *  modification, are permitted provided that the following conditions
14  *  are met:
15  *
16  *  1. Redistributions of source code must retain the above copyright
17  *     notice, this list of conditions and the following disclaimer.
18  *  2. Redistributions in binary form must reproduce the above copyright
19  *     notice, this list of conditions and the following disclaimer in the
20  *     documentation and/or other materials provided with the distribution.
21  *  3. Neither the name of the University nor the names of its
22  *     contributors may be used to endorse or promote products derived
23  *     from this software without specific prior written permission.
24  *
25  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37
38 #include <linux/param.h>
39 #include <linux/time.h>
40 #include <linux/mm.h>
41 #include <linux/errno.h>
42 #include <linux/string.h>
43 #include <linux/in.h>
44 #include <linux/pagemap.h>
45 #include <linux/proc_fs.h>
46 #include <linux/kdev_t.h>
47 #include <linux/sunrpc/clnt.h>
48 #include <linux/sunrpc/msg_prot.h>
49 #include <linux/sunrpc/gss_api.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 #include "internal.h"
56 #include "pnfs.h"
57
58 #define NFSDBG_FACILITY         NFSDBG_XDR
59
60 /* Mapping from NFS error code to "errno" error code. */
61 #define errno_NFSERR_IO         EIO
62
63 static int nfs4_stat_to_errno(int);
64
65 /* NFSv4 COMPOUND tags are only wanted for debugging purposes */
66 #ifdef DEBUG
67 #define NFS4_MAXTAGLEN          20
68 #else
69 #define NFS4_MAXTAGLEN          0
70 #endif
71
72 /* lock,open owner id:
73  * we currently use size 2 (u64) out of (NFS4_OPAQUE_LIMIT  >> 2)
74  */
75 #define open_owner_id_maxsz     (1 + 1 + 4)
76 #define lock_owner_id_maxsz     (1 + 1 + 4)
77 #define decode_lockowner_maxsz  (1 + XDR_QUADLEN(IDMAP_NAMESZ))
78 #define compound_encode_hdr_maxsz       (3 + (NFS4_MAXTAGLEN >> 2))
79 #define compound_decode_hdr_maxsz       (3 + (NFS4_MAXTAGLEN >> 2))
80 #define op_encode_hdr_maxsz     (1)
81 #define op_decode_hdr_maxsz     (2)
82 #define encode_stateid_maxsz    (XDR_QUADLEN(NFS4_STATEID_SIZE))
83 #define decode_stateid_maxsz    (XDR_QUADLEN(NFS4_STATEID_SIZE))
84 #define encode_verifier_maxsz   (XDR_QUADLEN(NFS4_VERIFIER_SIZE))
85 #define decode_verifier_maxsz   (XDR_QUADLEN(NFS4_VERIFIER_SIZE))
86 #define encode_putfh_maxsz      (op_encode_hdr_maxsz + 1 + \
87                                 (NFS4_FHSIZE >> 2))
88 #define decode_putfh_maxsz      (op_decode_hdr_maxsz)
89 #define encode_putrootfh_maxsz  (op_encode_hdr_maxsz)
90 #define decode_putrootfh_maxsz  (op_decode_hdr_maxsz)
91 #define encode_getfh_maxsz      (op_encode_hdr_maxsz)
92 #define decode_getfh_maxsz      (op_decode_hdr_maxsz + 1 + \
93                                 ((3+NFS4_FHSIZE) >> 2))
94 #define nfs4_fattr_bitmap_maxsz 4
95 #define encode_getattr_maxsz    (op_encode_hdr_maxsz + nfs4_fattr_bitmap_maxsz)
96 #define nfs4_name_maxsz         (1 + ((3 + NFS4_MAXNAMLEN) >> 2))
97 #define nfs4_path_maxsz         (1 + ((3 + NFS4_MAXPATHLEN) >> 2))
98 #define nfs4_owner_maxsz        (1 + XDR_QUADLEN(IDMAP_NAMESZ))
99 #define nfs4_group_maxsz        (1 + XDR_QUADLEN(IDMAP_NAMESZ))
100 /* This is based on getfattr, which uses the most attributes: */
101 #define nfs4_fattr_value_maxsz  (1 + (1 + 2 + 2 + 4 + 2 + 1 + 1 + 2 + 2 + \
102                                 3 + 3 + 3 + nfs4_owner_maxsz + nfs4_group_maxsz))
103 #define nfs4_fattr_maxsz        (nfs4_fattr_bitmap_maxsz + \
104                                 nfs4_fattr_value_maxsz)
105 #define decode_getattr_maxsz    (op_decode_hdr_maxsz + nfs4_fattr_maxsz)
106 #define encode_attrs_maxsz      (nfs4_fattr_bitmap_maxsz + \
107                                  1 + 2 + 1 + \
108                                 nfs4_owner_maxsz + \
109                                 nfs4_group_maxsz + \
110                                 4 + 4)
111 #define encode_savefh_maxsz     (op_encode_hdr_maxsz)
112 #define decode_savefh_maxsz     (op_decode_hdr_maxsz)
113 #define encode_restorefh_maxsz  (op_encode_hdr_maxsz)
114 #define decode_restorefh_maxsz  (op_decode_hdr_maxsz)
115 #define encode_fsinfo_maxsz     (encode_getattr_maxsz)
116 #define decode_fsinfo_maxsz     (op_decode_hdr_maxsz + 15)
117 #define encode_renew_maxsz      (op_encode_hdr_maxsz + 3)
118 #define decode_renew_maxsz      (op_decode_hdr_maxsz)
119 #define encode_setclientid_maxsz \
120                                 (op_encode_hdr_maxsz + \
121                                 XDR_QUADLEN(NFS4_VERIFIER_SIZE) + \
122                                 XDR_QUADLEN(NFS4_SETCLIENTID_NAMELEN) + \
123                                 1 /* sc_prog */ + \
124                                 XDR_QUADLEN(RPCBIND_MAXNETIDLEN) + \
125                                 XDR_QUADLEN(RPCBIND_MAXUADDRLEN) + \
126                                 1) /* sc_cb_ident */
127 #define decode_setclientid_maxsz \
128                                 (op_decode_hdr_maxsz + \
129                                 2 + \
130                                 1024) /* large value for CLID_INUSE */
131 #define encode_setclientid_confirm_maxsz \
132                                 (op_encode_hdr_maxsz + \
133                                 3 + (NFS4_VERIFIER_SIZE >> 2))
134 #define decode_setclientid_confirm_maxsz \
135                                 (op_decode_hdr_maxsz)
136 #define encode_lookup_maxsz     (op_encode_hdr_maxsz + nfs4_name_maxsz)
137 #define decode_lookup_maxsz     (op_decode_hdr_maxsz)
138 #define encode_share_access_maxsz \
139                                 (2)
140 #define encode_createmode_maxsz (1 + encode_attrs_maxsz + encode_verifier_maxsz)
141 #define encode_opentype_maxsz   (1 + encode_createmode_maxsz)
142 #define encode_claim_null_maxsz (1 + nfs4_name_maxsz)
143 #define encode_open_maxsz       (op_encode_hdr_maxsz + \
144                                 2 + encode_share_access_maxsz + 2 + \
145                                 open_owner_id_maxsz + \
146                                 encode_opentype_maxsz + \
147                                 encode_claim_null_maxsz)
148 #define decode_ace_maxsz        (3 + nfs4_owner_maxsz)
149 #define decode_delegation_maxsz (1 + decode_stateid_maxsz + 1 + \
150                                 decode_ace_maxsz)
151 #define decode_change_info_maxsz        (5)
152 #define decode_open_maxsz       (op_decode_hdr_maxsz + \
153                                 decode_stateid_maxsz + \
154                                 decode_change_info_maxsz + 1 + \
155                                 nfs4_fattr_bitmap_maxsz + \
156                                 decode_delegation_maxsz)
157 #define encode_open_confirm_maxsz \
158                                 (op_encode_hdr_maxsz + \
159                                  encode_stateid_maxsz + 1)
160 #define decode_open_confirm_maxsz \
161                                 (op_decode_hdr_maxsz + \
162                                  decode_stateid_maxsz)
163 #define encode_open_downgrade_maxsz \
164                                 (op_encode_hdr_maxsz + \
165                                  encode_stateid_maxsz + 1 + \
166                                  encode_share_access_maxsz)
167 #define decode_open_downgrade_maxsz \
168                                 (op_decode_hdr_maxsz + \
169                                  decode_stateid_maxsz)
170 #define encode_close_maxsz      (op_encode_hdr_maxsz + \
171                                  1 + encode_stateid_maxsz)
172 #define decode_close_maxsz      (op_decode_hdr_maxsz + \
173                                  decode_stateid_maxsz)
174 #define encode_setattr_maxsz    (op_encode_hdr_maxsz + \
175                                  encode_stateid_maxsz + \
176                                  encode_attrs_maxsz)
177 #define decode_setattr_maxsz    (op_decode_hdr_maxsz + \
178                                  nfs4_fattr_bitmap_maxsz)
179 #define encode_read_maxsz       (op_encode_hdr_maxsz + \
180                                  encode_stateid_maxsz + 3)
181 #define decode_read_maxsz       (op_decode_hdr_maxsz + 2)
182 #define encode_readdir_maxsz    (op_encode_hdr_maxsz + \
183                                  2 + encode_verifier_maxsz + 5)
184 #define decode_readdir_maxsz    (op_decode_hdr_maxsz + \
185                                  decode_verifier_maxsz)
186 #define encode_readlink_maxsz   (op_encode_hdr_maxsz)
187 #define decode_readlink_maxsz   (op_decode_hdr_maxsz + 1)
188 #define encode_write_maxsz      (op_encode_hdr_maxsz + \
189                                  encode_stateid_maxsz + 4)
190 #define decode_write_maxsz      (op_decode_hdr_maxsz + \
191                                  2 + decode_verifier_maxsz)
192 #define encode_commit_maxsz     (op_encode_hdr_maxsz + 3)
193 #define decode_commit_maxsz     (op_decode_hdr_maxsz + \
194                                  decode_verifier_maxsz)
195 #define encode_remove_maxsz     (op_encode_hdr_maxsz + \
196                                 nfs4_name_maxsz)
197 #define decode_remove_maxsz     (op_decode_hdr_maxsz + \
198                                  decode_change_info_maxsz)
199 #define encode_rename_maxsz     (op_encode_hdr_maxsz + \
200                                 2 * nfs4_name_maxsz)
201 #define decode_rename_maxsz     (op_decode_hdr_maxsz + \
202                                  decode_change_info_maxsz + \
203                                  decode_change_info_maxsz)
204 #define encode_link_maxsz       (op_encode_hdr_maxsz + \
205                                 nfs4_name_maxsz)
206 #define decode_link_maxsz       (op_decode_hdr_maxsz + decode_change_info_maxsz)
207 #define encode_lockowner_maxsz  (7)
208 #define encode_lock_maxsz       (op_encode_hdr_maxsz + \
209                                  7 + \
210                                  1 + encode_stateid_maxsz + 1 + \
211                                  encode_lockowner_maxsz)
212 #define decode_lock_denied_maxsz \
213                                 (8 + decode_lockowner_maxsz)
214 #define decode_lock_maxsz       (op_decode_hdr_maxsz + \
215                                  decode_lock_denied_maxsz)
216 #define encode_lockt_maxsz      (op_encode_hdr_maxsz + 5 + \
217                                 encode_lockowner_maxsz)
218 #define decode_lockt_maxsz      (op_decode_hdr_maxsz + \
219                                  decode_lock_denied_maxsz)
220 #define encode_locku_maxsz      (op_encode_hdr_maxsz + 3 + \
221                                  encode_stateid_maxsz + \
222                                  4)
223 #define decode_locku_maxsz      (op_decode_hdr_maxsz + \
224                                  decode_stateid_maxsz)
225 #define encode_release_lockowner_maxsz \
226                                 (op_encode_hdr_maxsz + \
227                                  encode_lockowner_maxsz)
228 #define decode_release_lockowner_maxsz \
229                                 (op_decode_hdr_maxsz)
230 #define encode_access_maxsz     (op_encode_hdr_maxsz + 1)
231 #define decode_access_maxsz     (op_decode_hdr_maxsz + 2)
232 #define encode_symlink_maxsz    (op_encode_hdr_maxsz + \
233                                 1 + nfs4_name_maxsz + \
234                                 1 + \
235                                 nfs4_fattr_maxsz)
236 #define decode_symlink_maxsz    (op_decode_hdr_maxsz + 8)
237 #define encode_create_maxsz     (op_encode_hdr_maxsz + \
238                                 1 + 2 + nfs4_name_maxsz + \
239                                 encode_attrs_maxsz)
240 #define decode_create_maxsz     (op_decode_hdr_maxsz + \
241                                 decode_change_info_maxsz + \
242                                 nfs4_fattr_bitmap_maxsz)
243 #define encode_statfs_maxsz     (encode_getattr_maxsz)
244 #define decode_statfs_maxsz     (decode_getattr_maxsz)
245 #define encode_delegreturn_maxsz (op_encode_hdr_maxsz + 4)
246 #define decode_delegreturn_maxsz (op_decode_hdr_maxsz)
247 #define encode_getacl_maxsz     (encode_getattr_maxsz)
248 #define decode_getacl_maxsz     (op_decode_hdr_maxsz + \
249                                  nfs4_fattr_bitmap_maxsz + 1)
250 #define encode_setacl_maxsz     (op_encode_hdr_maxsz + \
251                                  encode_stateid_maxsz + 3)
252 #define decode_setacl_maxsz     (decode_setattr_maxsz)
253 #define encode_fs_locations_maxsz \
254                                 (encode_getattr_maxsz)
255 #define decode_fs_locations_maxsz \
256                                 (0)
257 #define encode_secinfo_maxsz    (op_encode_hdr_maxsz + nfs4_name_maxsz)
258 #define decode_secinfo_maxsz    (op_decode_hdr_maxsz + 1 + ((NFS_MAX_SECFLAVORS * (16 + GSS_OID_MAX_LEN)) / 4))
259
260 #if defined(CONFIG_NFS_V4_1)
261 #define NFS4_MAX_MACHINE_NAME_LEN (64)
262
263 #define encode_exchange_id_maxsz (op_encode_hdr_maxsz + \
264                                 encode_verifier_maxsz + \
265                                 1 /* co_ownerid.len */ + \
266                                 XDR_QUADLEN(NFS4_EXCHANGE_ID_LEN) + \
267                                 1 /* flags */ + \
268                                 1 /* spa_how */ + \
269                                 0 /* SP4_NONE (for now) */ + \
270                                 1 /* zero implemetation id array */)
271 #define decode_exchange_id_maxsz (op_decode_hdr_maxsz + \
272                                 2 /* eir_clientid */ + \
273                                 1 /* eir_sequenceid */ + \
274                                 1 /* eir_flags */ + \
275                                 1 /* spr_how */ + \
276                                 0 /* SP4_NONE (for now) */ + \
277                                 2 /* eir_server_owner.so_minor_id */ + \
278                                 /* eir_server_owner.so_major_id<> */ \
279                                 XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 + \
280                                 /* eir_server_scope<> */ \
281                                 XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 + \
282                                 1 /* eir_server_impl_id array length */ + \
283                                 0 /* ignored eir_server_impl_id contents */)
284 #define encode_channel_attrs_maxsz  (6 + 1 /* ca_rdma_ird.len (0) */)
285 #define decode_channel_attrs_maxsz  (6 + \
286                                      1 /* ca_rdma_ird.len */ + \
287                                      1 /* ca_rdma_ird */)
288 #define encode_create_session_maxsz  (op_encode_hdr_maxsz + \
289                                      2 /* csa_clientid */ + \
290                                      1 /* csa_sequence */ + \
291                                      1 /* csa_flags */ + \
292                                      encode_channel_attrs_maxsz + \
293                                      encode_channel_attrs_maxsz + \
294                                      1 /* csa_cb_program */ + \
295                                      1 /* csa_sec_parms.len (1) */ + \
296                                      1 /* cb_secflavor (AUTH_SYS) */ + \
297                                      1 /* stamp */ + \
298                                      1 /* machinename.len */ + \
299                                      XDR_QUADLEN(NFS4_MAX_MACHINE_NAME_LEN) + \
300                                      1 /* uid */ + \
301                                      1 /* gid */ + \
302                                      1 /* gids.len (0) */)
303 #define decode_create_session_maxsz  (op_decode_hdr_maxsz +     \
304                                      XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
305                                      1 /* csr_sequence */ + \
306                                      1 /* csr_flags */ + \
307                                      decode_channel_attrs_maxsz + \
308                                      decode_channel_attrs_maxsz)
309 #define encode_destroy_session_maxsz    (op_encode_hdr_maxsz + 4)
310 #define decode_destroy_session_maxsz    (op_decode_hdr_maxsz)
311 #define encode_sequence_maxsz   (op_encode_hdr_maxsz + \
312                                 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + 4)
313 #define decode_sequence_maxsz   (op_decode_hdr_maxsz + \
314                                 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + 5)
315 #define encode_reclaim_complete_maxsz   (op_encode_hdr_maxsz + 4)
316 #define decode_reclaim_complete_maxsz   (op_decode_hdr_maxsz + 4)
317 #define encode_getdeviceinfo_maxsz (op_encode_hdr_maxsz + 4 + \
318                                 XDR_QUADLEN(NFS4_DEVICEID4_SIZE))
319 #define decode_getdeviceinfo_maxsz (op_decode_hdr_maxsz + \
320                                 1 /* layout type */ + \
321                                 1 /* opaque devaddr4 length */ + \
322                                   /* devaddr4 payload is read into page */ \
323                                 1 /* notification bitmap length */ + \
324                                 1 /* notification bitmap */)
325 #define encode_layoutget_maxsz  (op_encode_hdr_maxsz + 10 + \
326                                 encode_stateid_maxsz)
327 #define decode_layoutget_maxsz  (op_decode_hdr_maxsz + 8 + \
328                                 decode_stateid_maxsz + \
329                                 XDR_QUADLEN(PNFS_LAYOUT_MAXSIZE))
330 #define encode_layoutcommit_maxsz (op_encode_hdr_maxsz +          \
331                                 2 /* offset */ + \
332                                 2 /* length */ + \
333                                 1 /* reclaim */ + \
334                                 encode_stateid_maxsz + \
335                                 1 /* new offset (true) */ + \
336                                 2 /* last byte written */ + \
337                                 1 /* nt_timechanged (false) */ + \
338                                 1 /* layoutupdate4 layout type */ + \
339                                 1 /* NULL filelayout layoutupdate4 payload */)
340 #define decode_layoutcommit_maxsz (op_decode_hdr_maxsz + 3)
341 #define encode_layoutreturn_maxsz (8 + op_encode_hdr_maxsz + \
342                                 encode_stateid_maxsz + \
343                                 1 /* FIXME: opaque lrf_body always empty at the moment */)
344 #define decode_layoutreturn_maxsz (op_decode_hdr_maxsz + \
345                                 1 + decode_stateid_maxsz)
346 #define encode_secinfo_no_name_maxsz (op_encode_hdr_maxsz + 1)
347 #define decode_secinfo_no_name_maxsz decode_secinfo_maxsz
348 #define encode_test_stateid_maxsz       (op_encode_hdr_maxsz + 2 + \
349                                          XDR_QUADLEN(NFS4_STATEID_SIZE))
350 #define decode_test_stateid_maxsz       (op_decode_hdr_maxsz + 2 + 1)
351 #else /* CONFIG_NFS_V4_1 */
352 #define encode_sequence_maxsz   0
353 #define decode_sequence_maxsz   0
354 #endif /* CONFIG_NFS_V4_1 */
355
356 #define NFS4_enc_compound_sz    (1024)  /* XXX: large enough? */
357 #define NFS4_dec_compound_sz    (1024)  /* XXX: large enough? */
358 #define NFS4_enc_read_sz        (compound_encode_hdr_maxsz + \
359                                 encode_sequence_maxsz + \
360                                 encode_putfh_maxsz + \
361                                 encode_read_maxsz)
362 #define NFS4_dec_read_sz        (compound_decode_hdr_maxsz + \
363                                 decode_sequence_maxsz + \
364                                 decode_putfh_maxsz + \
365                                 decode_read_maxsz)
366 #define NFS4_enc_readlink_sz    (compound_encode_hdr_maxsz + \
367                                 encode_sequence_maxsz + \
368                                 encode_putfh_maxsz + \
369                                 encode_readlink_maxsz)
370 #define NFS4_dec_readlink_sz    (compound_decode_hdr_maxsz + \
371                                 decode_sequence_maxsz + \
372                                 decode_putfh_maxsz + \
373                                 decode_readlink_maxsz)
374 #define NFS4_enc_readdir_sz     (compound_encode_hdr_maxsz + \
375                                 encode_sequence_maxsz + \
376                                 encode_putfh_maxsz + \
377                                 encode_readdir_maxsz)
378 #define NFS4_dec_readdir_sz     (compound_decode_hdr_maxsz + \
379                                 decode_sequence_maxsz + \
380                                 decode_putfh_maxsz + \
381                                 decode_readdir_maxsz)
382 #define NFS4_enc_write_sz       (compound_encode_hdr_maxsz + \
383                                 encode_sequence_maxsz + \
384                                 encode_putfh_maxsz + \
385                                 encode_write_maxsz + \
386                                 encode_getattr_maxsz)
387 #define NFS4_dec_write_sz       (compound_decode_hdr_maxsz + \
388                                 decode_sequence_maxsz + \
389                                 decode_putfh_maxsz + \
390                                 decode_write_maxsz + \
391                                 decode_getattr_maxsz)
392 #define NFS4_enc_commit_sz      (compound_encode_hdr_maxsz + \
393                                 encode_sequence_maxsz + \
394                                 encode_putfh_maxsz + \
395                                 encode_commit_maxsz + \
396                                 encode_getattr_maxsz)
397 #define NFS4_dec_commit_sz      (compound_decode_hdr_maxsz + \
398                                 decode_sequence_maxsz + \
399                                 decode_putfh_maxsz + \
400                                 decode_commit_maxsz + \
401                                 decode_getattr_maxsz)
402 #define NFS4_enc_open_sz        (compound_encode_hdr_maxsz + \
403                                 encode_sequence_maxsz + \
404                                 encode_putfh_maxsz + \
405                                 encode_savefh_maxsz + \
406                                 encode_open_maxsz + \
407                                 encode_getfh_maxsz + \
408                                 encode_getattr_maxsz + \
409                                 encode_restorefh_maxsz + \
410                                 encode_getattr_maxsz)
411 #define NFS4_dec_open_sz        (compound_decode_hdr_maxsz + \
412                                 decode_sequence_maxsz + \
413                                 decode_putfh_maxsz + \
414                                 decode_savefh_maxsz + \
415                                 decode_open_maxsz + \
416                                 decode_getfh_maxsz + \
417                                 decode_getattr_maxsz + \
418                                 decode_restorefh_maxsz + \
419                                 decode_getattr_maxsz)
420 #define NFS4_enc_open_confirm_sz \
421                                 (compound_encode_hdr_maxsz + \
422                                  encode_putfh_maxsz + \
423                                  encode_open_confirm_maxsz)
424 #define NFS4_dec_open_confirm_sz \
425                                 (compound_decode_hdr_maxsz + \
426                                  decode_putfh_maxsz + \
427                                  decode_open_confirm_maxsz)
428 #define NFS4_enc_open_noattr_sz (compound_encode_hdr_maxsz + \
429                                         encode_sequence_maxsz + \
430                                         encode_putfh_maxsz + \
431                                         encode_open_maxsz + \
432                                         encode_getattr_maxsz)
433 #define NFS4_dec_open_noattr_sz (compound_decode_hdr_maxsz + \
434                                         decode_sequence_maxsz + \
435                                         decode_putfh_maxsz + \
436                                         decode_open_maxsz + \
437                                         decode_getattr_maxsz)
438 #define NFS4_enc_open_downgrade_sz \
439                                 (compound_encode_hdr_maxsz + \
440                                  encode_sequence_maxsz + \
441                                  encode_putfh_maxsz + \
442                                  encode_open_downgrade_maxsz + \
443                                  encode_getattr_maxsz)
444 #define NFS4_dec_open_downgrade_sz \
445                                 (compound_decode_hdr_maxsz + \
446                                  decode_sequence_maxsz + \
447                                  decode_putfh_maxsz + \
448                                  decode_open_downgrade_maxsz + \
449                                  decode_getattr_maxsz)
450 #define NFS4_enc_close_sz       (compound_encode_hdr_maxsz + \
451                                  encode_sequence_maxsz + \
452                                  encode_putfh_maxsz + \
453                                  encode_close_maxsz + \
454                                  encode_getattr_maxsz)
455 #define NFS4_dec_close_sz       (compound_decode_hdr_maxsz + \
456                                  decode_sequence_maxsz + \
457                                  decode_putfh_maxsz + \
458                                  decode_close_maxsz + \
459                                  decode_getattr_maxsz)
460 #define NFS4_enc_setattr_sz     (compound_encode_hdr_maxsz + \
461                                  encode_sequence_maxsz + \
462                                  encode_putfh_maxsz + \
463                                  encode_setattr_maxsz + \
464                                  encode_getattr_maxsz)
465 #define NFS4_dec_setattr_sz     (compound_decode_hdr_maxsz + \
466                                  decode_sequence_maxsz + \
467                                  decode_putfh_maxsz + \
468                                  decode_setattr_maxsz + \
469                                  decode_getattr_maxsz)
470 #define NFS4_enc_fsinfo_sz      (compound_encode_hdr_maxsz + \
471                                 encode_sequence_maxsz + \
472                                 encode_putfh_maxsz + \
473                                 encode_fsinfo_maxsz)
474 #define NFS4_dec_fsinfo_sz      (compound_decode_hdr_maxsz + \
475                                 decode_sequence_maxsz + \
476                                 decode_putfh_maxsz + \
477                                 decode_fsinfo_maxsz)
478 #define NFS4_enc_renew_sz       (compound_encode_hdr_maxsz + \
479                                 encode_renew_maxsz)
480 #define NFS4_dec_renew_sz       (compound_decode_hdr_maxsz + \
481                                 decode_renew_maxsz)
482 #define NFS4_enc_setclientid_sz (compound_encode_hdr_maxsz + \
483                                 encode_setclientid_maxsz)
484 #define NFS4_dec_setclientid_sz (compound_decode_hdr_maxsz + \
485                                 decode_setclientid_maxsz)
486 #define NFS4_enc_setclientid_confirm_sz \
487                                 (compound_encode_hdr_maxsz + \
488                                 encode_setclientid_confirm_maxsz + \
489                                 encode_putrootfh_maxsz + \
490                                 encode_fsinfo_maxsz)
491 #define NFS4_dec_setclientid_confirm_sz \
492                                 (compound_decode_hdr_maxsz + \
493                                 decode_setclientid_confirm_maxsz + \
494                                 decode_putrootfh_maxsz + \
495                                 decode_fsinfo_maxsz)
496 #define NFS4_enc_lock_sz        (compound_encode_hdr_maxsz + \
497                                 encode_sequence_maxsz + \
498                                 encode_putfh_maxsz + \
499                                 encode_lock_maxsz)
500 #define NFS4_dec_lock_sz        (compound_decode_hdr_maxsz + \
501                                 decode_sequence_maxsz + \
502                                 decode_putfh_maxsz + \
503                                 decode_lock_maxsz)
504 #define NFS4_enc_lockt_sz       (compound_encode_hdr_maxsz + \
505                                 encode_sequence_maxsz + \
506                                 encode_putfh_maxsz + \
507                                 encode_lockt_maxsz)
508 #define NFS4_dec_lockt_sz       (compound_decode_hdr_maxsz + \
509                                  decode_sequence_maxsz + \
510                                  decode_putfh_maxsz + \
511                                  decode_lockt_maxsz)
512 #define NFS4_enc_locku_sz       (compound_encode_hdr_maxsz + \
513                                 encode_sequence_maxsz + \
514                                 encode_putfh_maxsz + \
515                                 encode_locku_maxsz)
516 #define NFS4_dec_locku_sz       (compound_decode_hdr_maxsz + \
517                                 decode_sequence_maxsz + \
518                                 decode_putfh_maxsz + \
519                                 decode_locku_maxsz)
520 #define NFS4_enc_release_lockowner_sz \
521                                 (compound_encode_hdr_maxsz + \
522                                  encode_lockowner_maxsz)
523 #define NFS4_dec_release_lockowner_sz \
524                                 (compound_decode_hdr_maxsz + \
525                                  decode_lockowner_maxsz)
526 #define NFS4_enc_access_sz      (compound_encode_hdr_maxsz + \
527                                 encode_sequence_maxsz + \
528                                 encode_putfh_maxsz + \
529                                 encode_access_maxsz + \
530                                 encode_getattr_maxsz)
531 #define NFS4_dec_access_sz      (compound_decode_hdr_maxsz + \
532                                 decode_sequence_maxsz + \
533                                 decode_putfh_maxsz + \
534                                 decode_access_maxsz + \
535                                 decode_getattr_maxsz)
536 #define NFS4_enc_getattr_sz     (compound_encode_hdr_maxsz + \
537                                 encode_sequence_maxsz + \
538                                 encode_putfh_maxsz + \
539                                 encode_getattr_maxsz)
540 #define NFS4_dec_getattr_sz     (compound_decode_hdr_maxsz + \
541                                 decode_sequence_maxsz + \
542                                 decode_putfh_maxsz + \
543                                 decode_getattr_maxsz)
544 #define NFS4_enc_lookup_sz      (compound_encode_hdr_maxsz + \
545                                 encode_sequence_maxsz + \
546                                 encode_putfh_maxsz + \
547                                 encode_lookup_maxsz + \
548                                 encode_getattr_maxsz + \
549                                 encode_getfh_maxsz)
550 #define NFS4_dec_lookup_sz      (compound_decode_hdr_maxsz + \
551                                 decode_sequence_maxsz + \
552                                 decode_putfh_maxsz + \
553                                 decode_lookup_maxsz + \
554                                 decode_getattr_maxsz + \
555                                 decode_getfh_maxsz)
556 #define NFS4_enc_lookup_root_sz (compound_encode_hdr_maxsz + \
557                                 encode_sequence_maxsz + \
558                                 encode_putrootfh_maxsz + \
559                                 encode_getattr_maxsz + \
560                                 encode_getfh_maxsz)
561 #define NFS4_dec_lookup_root_sz (compound_decode_hdr_maxsz + \
562                                 decode_sequence_maxsz + \
563                                 decode_putrootfh_maxsz + \
564                                 decode_getattr_maxsz + \
565                                 decode_getfh_maxsz)
566 #define NFS4_enc_remove_sz      (compound_encode_hdr_maxsz + \
567                                 encode_sequence_maxsz + \
568                                 encode_putfh_maxsz + \
569                                 encode_remove_maxsz + \
570                                 encode_getattr_maxsz)
571 #define NFS4_dec_remove_sz      (compound_decode_hdr_maxsz + \
572                                 decode_sequence_maxsz + \
573                                 decode_putfh_maxsz + \
574                                 decode_remove_maxsz + \
575                                 decode_getattr_maxsz)
576 #define NFS4_enc_rename_sz      (compound_encode_hdr_maxsz + \
577                                 encode_sequence_maxsz + \
578                                 encode_putfh_maxsz + \
579                                 encode_savefh_maxsz + \
580                                 encode_putfh_maxsz + \
581                                 encode_rename_maxsz + \
582                                 encode_getattr_maxsz + \
583                                 encode_restorefh_maxsz + \
584                                 encode_getattr_maxsz)
585 #define NFS4_dec_rename_sz      (compound_decode_hdr_maxsz + \
586                                 decode_sequence_maxsz + \
587                                 decode_putfh_maxsz + \
588                                 decode_savefh_maxsz + \
589                                 decode_putfh_maxsz + \
590                                 decode_rename_maxsz + \
591                                 decode_getattr_maxsz + \
592                                 decode_restorefh_maxsz + \
593                                 decode_getattr_maxsz)
594 #define NFS4_enc_link_sz        (compound_encode_hdr_maxsz + \
595                                 encode_sequence_maxsz + \
596                                 encode_putfh_maxsz + \
597                                 encode_savefh_maxsz + \
598                                 encode_putfh_maxsz + \
599                                 encode_link_maxsz + \
600                                 decode_getattr_maxsz + \
601                                 encode_restorefh_maxsz + \
602                                 decode_getattr_maxsz)
603 #define NFS4_dec_link_sz        (compound_decode_hdr_maxsz + \
604                                 decode_sequence_maxsz + \
605                                 decode_putfh_maxsz + \
606                                 decode_savefh_maxsz + \
607                                 decode_putfh_maxsz + \
608                                 decode_link_maxsz + \
609                                 decode_getattr_maxsz + \
610                                 decode_restorefh_maxsz + \
611                                 decode_getattr_maxsz)
612 #define NFS4_enc_symlink_sz     (compound_encode_hdr_maxsz + \
613                                 encode_sequence_maxsz + \
614                                 encode_putfh_maxsz + \
615                                 encode_symlink_maxsz + \
616                                 encode_getattr_maxsz + \
617                                 encode_getfh_maxsz)
618 #define NFS4_dec_symlink_sz     (compound_decode_hdr_maxsz + \
619                                 decode_sequence_maxsz + \
620                                 decode_putfh_maxsz + \
621                                 decode_symlink_maxsz + \
622                                 decode_getattr_maxsz + \
623                                 decode_getfh_maxsz)
624 #define NFS4_enc_create_sz      (compound_encode_hdr_maxsz + \
625                                 encode_sequence_maxsz + \
626                                 encode_putfh_maxsz + \
627                                 encode_savefh_maxsz + \
628                                 encode_create_maxsz + \
629                                 encode_getfh_maxsz + \
630                                 encode_getattr_maxsz + \
631                                 encode_restorefh_maxsz + \
632                                 encode_getattr_maxsz)
633 #define NFS4_dec_create_sz      (compound_decode_hdr_maxsz + \
634                                 decode_sequence_maxsz + \
635                                 decode_putfh_maxsz + \
636                                 decode_savefh_maxsz + \
637                                 decode_create_maxsz + \
638                                 decode_getfh_maxsz + \
639                                 decode_getattr_maxsz + \
640                                 decode_restorefh_maxsz + \
641                                 decode_getattr_maxsz)
642 #define NFS4_enc_pathconf_sz    (compound_encode_hdr_maxsz + \
643                                 encode_sequence_maxsz + \
644                                 encode_putfh_maxsz + \
645                                 encode_getattr_maxsz)
646 #define NFS4_dec_pathconf_sz    (compound_decode_hdr_maxsz + \
647                                 decode_sequence_maxsz + \
648                                 decode_putfh_maxsz + \
649                                 decode_getattr_maxsz)
650 #define NFS4_enc_statfs_sz      (compound_encode_hdr_maxsz + \
651                                 encode_sequence_maxsz + \
652                                 encode_putfh_maxsz + \
653                                 encode_statfs_maxsz)
654 #define NFS4_dec_statfs_sz      (compound_decode_hdr_maxsz + \
655                                 decode_sequence_maxsz + \
656                                 decode_putfh_maxsz + \
657                                 decode_statfs_maxsz)
658 #define NFS4_enc_server_caps_sz (compound_encode_hdr_maxsz + \
659                                 encode_sequence_maxsz + \
660                                 encode_putfh_maxsz + \
661                                 encode_getattr_maxsz)
662 #define NFS4_dec_server_caps_sz (compound_decode_hdr_maxsz + \
663                                 decode_sequence_maxsz + \
664                                 decode_putfh_maxsz + \
665                                 decode_getattr_maxsz)
666 #define NFS4_enc_delegreturn_sz (compound_encode_hdr_maxsz + \
667                                 encode_sequence_maxsz + \
668                                 encode_putfh_maxsz + \
669                                 encode_delegreturn_maxsz + \
670                                 encode_getattr_maxsz)
671 #define NFS4_dec_delegreturn_sz (compound_decode_hdr_maxsz + \
672                                 decode_sequence_maxsz + \
673                                 decode_delegreturn_maxsz + \
674                                 decode_getattr_maxsz)
675 #define NFS4_enc_getacl_sz      (compound_encode_hdr_maxsz + \
676                                 encode_sequence_maxsz + \
677                                 encode_putfh_maxsz + \
678                                 encode_getacl_maxsz)
679 #define NFS4_dec_getacl_sz      (compound_decode_hdr_maxsz + \
680                                 decode_sequence_maxsz + \
681                                 decode_putfh_maxsz + \
682                                 decode_getacl_maxsz)
683 #define NFS4_enc_setacl_sz      (compound_encode_hdr_maxsz + \
684                                 encode_sequence_maxsz + \
685                                 encode_putfh_maxsz + \
686                                 encode_setacl_maxsz)
687 #define NFS4_dec_setacl_sz      (compound_decode_hdr_maxsz + \
688                                 decode_sequence_maxsz + \
689                                 decode_putfh_maxsz + \
690                                 decode_setacl_maxsz)
691 #define NFS4_enc_fs_locations_sz \
692                                 (compound_encode_hdr_maxsz + \
693                                  encode_sequence_maxsz + \
694                                  encode_putfh_maxsz + \
695                                  encode_lookup_maxsz + \
696                                  encode_fs_locations_maxsz)
697 #define NFS4_dec_fs_locations_sz \
698                                 (compound_decode_hdr_maxsz + \
699                                  decode_sequence_maxsz + \
700                                  decode_putfh_maxsz + \
701                                  decode_lookup_maxsz + \
702                                  decode_fs_locations_maxsz)
703 #define NFS4_enc_secinfo_sz     (compound_encode_hdr_maxsz + \
704                                 encode_sequence_maxsz + \
705                                 encode_putfh_maxsz + \
706                                 encode_secinfo_maxsz)
707 #define NFS4_dec_secinfo_sz     (compound_decode_hdr_maxsz + \
708                                 decode_sequence_maxsz + \
709                                 decode_putfh_maxsz + \
710                                 decode_secinfo_maxsz)
711 #if defined(CONFIG_NFS_V4_1)
712 #define NFS4_enc_exchange_id_sz \
713                                 (compound_encode_hdr_maxsz + \
714                                  encode_exchange_id_maxsz)
715 #define NFS4_dec_exchange_id_sz \
716                                 (compound_decode_hdr_maxsz + \
717                                  decode_exchange_id_maxsz)
718 #define NFS4_enc_create_session_sz \
719                                 (compound_encode_hdr_maxsz + \
720                                  encode_create_session_maxsz)
721 #define NFS4_dec_create_session_sz \
722                                 (compound_decode_hdr_maxsz + \
723                                  decode_create_session_maxsz)
724 #define NFS4_enc_destroy_session_sz     (compound_encode_hdr_maxsz + \
725                                          encode_destroy_session_maxsz)
726 #define NFS4_dec_destroy_session_sz     (compound_decode_hdr_maxsz + \
727                                          decode_destroy_session_maxsz)
728 #define NFS4_enc_sequence_sz \
729                                 (compound_decode_hdr_maxsz + \
730                                  encode_sequence_maxsz)
731 #define NFS4_dec_sequence_sz \
732                                 (compound_decode_hdr_maxsz + \
733                                  decode_sequence_maxsz)
734 #define NFS4_enc_get_lease_time_sz      (compound_encode_hdr_maxsz + \
735                                          encode_sequence_maxsz + \
736                                          encode_putrootfh_maxsz + \
737                                          encode_fsinfo_maxsz)
738 #define NFS4_dec_get_lease_time_sz      (compound_decode_hdr_maxsz + \
739                                          decode_sequence_maxsz + \
740                                          decode_putrootfh_maxsz + \
741                                          decode_fsinfo_maxsz)
742 #define NFS4_enc_reclaim_complete_sz    (compound_encode_hdr_maxsz + \
743                                          encode_sequence_maxsz + \
744                                          encode_reclaim_complete_maxsz)
745 #define NFS4_dec_reclaim_complete_sz    (compound_decode_hdr_maxsz + \
746                                          decode_sequence_maxsz + \
747                                          decode_reclaim_complete_maxsz)
748 #define NFS4_enc_getdeviceinfo_sz (compound_encode_hdr_maxsz +    \
749                                 encode_sequence_maxsz +\
750                                 encode_getdeviceinfo_maxsz)
751 #define NFS4_dec_getdeviceinfo_sz (compound_decode_hdr_maxsz +    \
752                                 decode_sequence_maxsz + \
753                                 decode_getdeviceinfo_maxsz)
754 #define NFS4_enc_layoutget_sz   (compound_encode_hdr_maxsz + \
755                                 encode_sequence_maxsz + \
756                                 encode_putfh_maxsz +        \
757                                 encode_layoutget_maxsz)
758 #define NFS4_dec_layoutget_sz   (compound_decode_hdr_maxsz + \
759                                 decode_sequence_maxsz + \
760                                 decode_putfh_maxsz +        \
761                                 decode_layoutget_maxsz)
762 #define NFS4_enc_layoutcommit_sz (compound_encode_hdr_maxsz + \
763                                 encode_sequence_maxsz +\
764                                 encode_putfh_maxsz + \
765                                 encode_layoutcommit_maxsz + \
766                                 encode_getattr_maxsz)
767 #define NFS4_dec_layoutcommit_sz (compound_decode_hdr_maxsz + \
768                                 decode_sequence_maxsz + \
769                                 decode_putfh_maxsz + \
770                                 decode_layoutcommit_maxsz + \
771                                 decode_getattr_maxsz)
772 #define NFS4_enc_layoutreturn_sz (compound_encode_hdr_maxsz + \
773                                 encode_sequence_maxsz + \
774                                 encode_putfh_maxsz + \
775                                 encode_layoutreturn_maxsz)
776 #define NFS4_dec_layoutreturn_sz (compound_decode_hdr_maxsz + \
777                                 decode_sequence_maxsz + \
778                                 decode_putfh_maxsz + \
779                                 decode_layoutreturn_maxsz)
780 #define NFS4_enc_secinfo_no_name_sz     (compound_encode_hdr_maxsz + \
781                                         encode_sequence_maxsz + \
782                                         encode_putrootfh_maxsz +\
783                                         encode_secinfo_no_name_maxsz)
784 #define NFS4_dec_secinfo_no_name_sz     (compound_decode_hdr_maxsz + \
785                                         decode_sequence_maxsz + \
786                                         decode_putrootfh_maxsz + \
787                                         decode_secinfo_no_name_maxsz)
788 #define NFS4_enc_test_stateid_sz        (compound_encode_hdr_maxsz + \
789                                          encode_sequence_maxsz + \
790                                          encode_test_stateid_maxsz)
791 #define NFS4_dec_test_stateid_sz        (compound_decode_hdr_maxsz + \
792                                          decode_sequence_maxsz + \
793                                          decode_test_stateid_maxsz)
794
795 const u32 nfs41_maxwrite_overhead = ((RPC_MAX_HEADER_WITH_AUTH +
796                                       compound_encode_hdr_maxsz +
797                                       encode_sequence_maxsz +
798                                       encode_putfh_maxsz +
799                                       encode_getattr_maxsz) *
800                                      XDR_UNIT);
801
802 const u32 nfs41_maxread_overhead = ((RPC_MAX_HEADER_WITH_AUTH +
803                                      compound_decode_hdr_maxsz +
804                                      decode_sequence_maxsz +
805                                      decode_putfh_maxsz) *
806                                     XDR_UNIT);
807 #endif /* CONFIG_NFS_V4_1 */
808
809 static const umode_t nfs_type2fmt[] = {
810         [NF4BAD] = 0,
811         [NF4REG] = S_IFREG,
812         [NF4DIR] = S_IFDIR,
813         [NF4BLK] = S_IFBLK,
814         [NF4CHR] = S_IFCHR,
815         [NF4LNK] = S_IFLNK,
816         [NF4SOCK] = S_IFSOCK,
817         [NF4FIFO] = S_IFIFO,
818         [NF4ATTRDIR] = 0,
819         [NF4NAMEDATTR] = 0,
820 };
821
822 struct compound_hdr {
823         int32_t         status;
824         uint32_t        nops;
825         __be32 *        nops_p;
826         uint32_t        taglen;
827         char *          tag;
828         uint32_t        replen;         /* expected reply words */
829         u32             minorversion;
830 };
831
832 static __be32 *reserve_space(struct xdr_stream *xdr, size_t nbytes)
833 {
834         __be32 *p = xdr_reserve_space(xdr, nbytes);
835         BUG_ON(!p);
836         return p;
837 }
838
839 static void encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
840 {
841         __be32 *p;
842
843         p = xdr_reserve_space(xdr, 4 + len);
844         BUG_ON(p == NULL);
845         xdr_encode_opaque(p, str, len);
846 }
847
848 static void encode_compound_hdr(struct xdr_stream *xdr,
849                                 struct rpc_rqst *req,
850                                 struct compound_hdr *hdr)
851 {
852         __be32 *p;
853         struct rpc_auth *auth = req->rq_cred->cr_auth;
854
855         /* initialize running count of expected bytes in reply.
856          * NOTE: the replied tag SHOULD be the same is the one sent,
857          * but this is not required as a MUST for the server to do so. */
858         hdr->replen = RPC_REPHDRSIZE + auth->au_rslack + 3 + hdr->taglen;
859
860         dprintk("encode_compound: tag=%.*s\n", (int)hdr->taglen, hdr->tag);
861         BUG_ON(hdr->taglen > NFS4_MAXTAGLEN);
862         p = reserve_space(xdr, 4 + hdr->taglen + 8);
863         p = xdr_encode_opaque(p, hdr->tag, hdr->taglen);
864         *p++ = cpu_to_be32(hdr->minorversion);
865         hdr->nops_p = p;
866         *p = cpu_to_be32(hdr->nops);
867 }
868
869 static void encode_nops(struct compound_hdr *hdr)
870 {
871         BUG_ON(hdr->nops > NFS4_MAX_OPS);
872         *hdr->nops_p = htonl(hdr->nops);
873 }
874
875 static void encode_nfs4_verifier(struct xdr_stream *xdr, const nfs4_verifier *verf)
876 {
877         __be32 *p;
878
879         p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
880         BUG_ON(p == NULL);
881         xdr_encode_opaque_fixed(p, verf->data, NFS4_VERIFIER_SIZE);
882 }
883
884 static void encode_attrs(struct xdr_stream *xdr, const struct iattr *iap, const struct nfs_server *server)
885 {
886         char owner_name[IDMAP_NAMESZ];
887         char owner_group[IDMAP_NAMESZ];
888         int owner_namelen = 0;
889         int owner_grouplen = 0;
890         __be32 *p;
891         __be32 *q;
892         int len;
893         uint32_t bmval0 = 0;
894         uint32_t bmval1 = 0;
895
896         /*
897          * We reserve enough space to write the entire attribute buffer at once.
898          * In the worst-case, this would be
899          *   12(bitmap) + 4(attrlen) + 8(size) + 4(mode) + 4(atime) + 4(mtime)
900          *          = 36 bytes, plus any contribution from variable-length fields
901          *            such as owner/group.
902          */
903         len = 16;
904
905         /* Sigh */
906         if (iap->ia_valid & ATTR_SIZE)
907                 len += 8;
908         if (iap->ia_valid & ATTR_MODE)
909                 len += 4;
910         if (iap->ia_valid & ATTR_UID) {
911                 owner_namelen = nfs_map_uid_to_name(server, iap->ia_uid, owner_name, IDMAP_NAMESZ);
912                 if (owner_namelen < 0) {
913                         dprintk("nfs: couldn't resolve uid %d to string\n",
914                                         iap->ia_uid);
915                         /* XXX */
916                         strcpy(owner_name, "nobody");
917                         owner_namelen = sizeof("nobody") - 1;
918                         /* goto out; */
919                 }
920                 len += 4 + (XDR_QUADLEN(owner_namelen) << 2);
921         }
922         if (iap->ia_valid & ATTR_GID) {
923                 owner_grouplen = nfs_map_gid_to_group(server, iap->ia_gid, owner_group, IDMAP_NAMESZ);
924                 if (owner_grouplen < 0) {
925                         dprintk("nfs: couldn't resolve gid %d to string\n",
926                                         iap->ia_gid);
927                         strcpy(owner_group, "nobody");
928                         owner_grouplen = sizeof("nobody") - 1;
929                         /* goto out; */
930                 }
931                 len += 4 + (XDR_QUADLEN(owner_grouplen) << 2);
932         }
933         if (iap->ia_valid & ATTR_ATIME_SET)
934                 len += 16;
935         else if (iap->ia_valid & ATTR_ATIME)
936                 len += 4;
937         if (iap->ia_valid & ATTR_MTIME_SET)
938                 len += 16;
939         else if (iap->ia_valid & ATTR_MTIME)
940                 len += 4;
941         p = reserve_space(xdr, len);
942
943         /*
944          * We write the bitmap length now, but leave the bitmap and the attribute
945          * buffer length to be backfilled at the end of this routine.
946          */
947         *p++ = cpu_to_be32(2);
948         q = p;
949         p += 3;
950
951         if (iap->ia_valid & ATTR_SIZE) {
952                 bmval0 |= FATTR4_WORD0_SIZE;
953                 p = xdr_encode_hyper(p, iap->ia_size);
954         }
955         if (iap->ia_valid & ATTR_MODE) {
956                 bmval1 |= FATTR4_WORD1_MODE;
957                 *p++ = cpu_to_be32(iap->ia_mode & S_IALLUGO);
958         }
959         if (iap->ia_valid & ATTR_UID) {
960                 bmval1 |= FATTR4_WORD1_OWNER;
961                 p = xdr_encode_opaque(p, owner_name, owner_namelen);
962         }
963         if (iap->ia_valid & ATTR_GID) {
964                 bmval1 |= FATTR4_WORD1_OWNER_GROUP;
965                 p = xdr_encode_opaque(p, owner_group, owner_grouplen);
966         }
967         if (iap->ia_valid & ATTR_ATIME_SET) {
968                 bmval1 |= FATTR4_WORD1_TIME_ACCESS_SET;
969                 *p++ = cpu_to_be32(NFS4_SET_TO_CLIENT_TIME);
970                 *p++ = cpu_to_be32(0);
971                 *p++ = cpu_to_be32(iap->ia_atime.tv_sec);
972                 *p++ = cpu_to_be32(iap->ia_atime.tv_nsec);
973         }
974         else if (iap->ia_valid & ATTR_ATIME) {
975                 bmval1 |= FATTR4_WORD1_TIME_ACCESS_SET;
976                 *p++ = cpu_to_be32(NFS4_SET_TO_SERVER_TIME);
977         }
978         if (iap->ia_valid & ATTR_MTIME_SET) {
979                 bmval1 |= FATTR4_WORD1_TIME_MODIFY_SET;
980                 *p++ = cpu_to_be32(NFS4_SET_TO_CLIENT_TIME);
981                 *p++ = cpu_to_be32(0);
982                 *p++ = cpu_to_be32(iap->ia_mtime.tv_sec);
983                 *p++ = cpu_to_be32(iap->ia_mtime.tv_nsec);
984         }
985         else if (iap->ia_valid & ATTR_MTIME) {
986                 bmval1 |= FATTR4_WORD1_TIME_MODIFY_SET;
987                 *p++ = cpu_to_be32(NFS4_SET_TO_SERVER_TIME);
988         }
989
990         /*
991          * Now we backfill the bitmap and the attribute buffer length.
992          */
993         if (len != ((char *)p - (char *)q) + 4) {
994                 printk(KERN_ERR "nfs: Attr length error, %u != %Zu\n",
995                                 len, ((char *)p - (char *)q) + 4);
996                 BUG();
997         }
998         len = (char *)p - (char *)q - 12;
999         *q++ = htonl(bmval0);
1000         *q++ = htonl(bmval1);
1001         *q = htonl(len);
1002
1003 /* out: */
1004 }
1005
1006 static void encode_access(struct xdr_stream *xdr, u32 access, struct compound_hdr *hdr)
1007 {
1008         __be32 *p;
1009
1010         p = reserve_space(xdr, 8);
1011         *p++ = cpu_to_be32(OP_ACCESS);
1012         *p = cpu_to_be32(access);
1013         hdr->nops++;
1014         hdr->replen += decode_access_maxsz;
1015 }
1016
1017 static void encode_close(struct xdr_stream *xdr, const struct nfs_closeargs *arg, struct compound_hdr *hdr)
1018 {
1019         __be32 *p;
1020
1021         p = reserve_space(xdr, 8+NFS4_STATEID_SIZE);
1022         *p++ = cpu_to_be32(OP_CLOSE);
1023         *p++ = cpu_to_be32(arg->seqid->sequence->counter);
1024         xdr_encode_opaque_fixed(p, arg->stateid->data, NFS4_STATEID_SIZE);
1025         hdr->nops++;
1026         hdr->replen += decode_close_maxsz;
1027 }
1028
1029 static void encode_commit(struct xdr_stream *xdr, const struct nfs_writeargs *args, struct compound_hdr *hdr)
1030 {
1031         __be32 *p;
1032
1033         p = reserve_space(xdr, 16);
1034         *p++ = cpu_to_be32(OP_COMMIT);
1035         p = xdr_encode_hyper(p, args->offset);
1036         *p = cpu_to_be32(args->count);
1037         hdr->nops++;
1038         hdr->replen += decode_commit_maxsz;
1039 }
1040
1041 static void encode_create(struct xdr_stream *xdr, const struct nfs4_create_arg *create, struct compound_hdr *hdr)
1042 {
1043         __be32 *p;
1044
1045         p = reserve_space(xdr, 8);
1046         *p++ = cpu_to_be32(OP_CREATE);
1047         *p = cpu_to_be32(create->ftype);
1048
1049         switch (create->ftype) {
1050         case NF4LNK:
1051                 p = reserve_space(xdr, 4);
1052                 *p = cpu_to_be32(create->u.symlink.len);
1053                 xdr_write_pages(xdr, create->u.symlink.pages, 0, create->u.symlink.len);
1054                 break;
1055
1056         case NF4BLK: case NF4CHR:
1057                 p = reserve_space(xdr, 8);
1058                 *p++ = cpu_to_be32(create->u.device.specdata1);
1059                 *p = cpu_to_be32(create->u.device.specdata2);
1060                 break;
1061
1062         default:
1063                 break;
1064         }
1065
1066         encode_string(xdr, create->name->len, create->name->name);
1067         hdr->nops++;
1068         hdr->replen += decode_create_maxsz;
1069
1070         encode_attrs(xdr, create->attrs, create->server);
1071 }
1072
1073 static void encode_getattr_one(struct xdr_stream *xdr, uint32_t bitmap, struct compound_hdr *hdr)
1074 {
1075         __be32 *p;
1076
1077         p = reserve_space(xdr, 12);
1078         *p++ = cpu_to_be32(OP_GETATTR);
1079         *p++ = cpu_to_be32(1);
1080         *p = cpu_to_be32(bitmap);
1081         hdr->nops++;
1082         hdr->replen += decode_getattr_maxsz;
1083 }
1084
1085 static void encode_getattr_two(struct xdr_stream *xdr, uint32_t bm0, uint32_t bm1, struct compound_hdr *hdr)
1086 {
1087         __be32 *p;
1088
1089         p = reserve_space(xdr, 16);
1090         *p++ = cpu_to_be32(OP_GETATTR);
1091         *p++ = cpu_to_be32(2);
1092         *p++ = cpu_to_be32(bm0);
1093         *p = cpu_to_be32(bm1);
1094         hdr->nops++;
1095         hdr->replen += decode_getattr_maxsz;
1096 }
1097
1098 static void encode_getfattr(struct xdr_stream *xdr, const u32* bitmask, struct compound_hdr *hdr)
1099 {
1100         encode_getattr_two(xdr, bitmask[0] & nfs4_fattr_bitmap[0],
1101                            bitmask[1] & nfs4_fattr_bitmap[1], hdr);
1102 }
1103
1104 static void encode_fsinfo(struct xdr_stream *xdr, const u32* bitmask, struct compound_hdr *hdr)
1105 {
1106         encode_getattr_two(xdr, bitmask[0] & nfs4_fsinfo_bitmap[0],
1107                            bitmask[1] & nfs4_fsinfo_bitmap[1], hdr);
1108 }
1109
1110 static void encode_fs_locations(struct xdr_stream *xdr, const u32* bitmask, struct compound_hdr *hdr)
1111 {
1112         encode_getattr_two(xdr, bitmask[0] & nfs4_fs_locations_bitmap[0],
1113                            bitmask[1] & nfs4_fs_locations_bitmap[1], hdr);
1114 }
1115
1116 static void encode_getfh(struct xdr_stream *xdr, struct compound_hdr *hdr)
1117 {
1118         __be32 *p;
1119
1120         p = reserve_space(xdr, 4);
1121         *p = cpu_to_be32(OP_GETFH);
1122         hdr->nops++;
1123         hdr->replen += decode_getfh_maxsz;
1124 }
1125
1126 static void encode_link(struct xdr_stream *xdr, const struct qstr *name, struct compound_hdr *hdr)
1127 {
1128         __be32 *p;
1129
1130         p = reserve_space(xdr, 8 + name->len);
1131         *p++ = cpu_to_be32(OP_LINK);
1132         xdr_encode_opaque(p, name->name, name->len);
1133         hdr->nops++;
1134         hdr->replen += decode_link_maxsz;
1135 }
1136
1137 static inline int nfs4_lock_type(struct file_lock *fl, int block)
1138 {
1139         if ((fl->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) == F_RDLCK)
1140                 return block ? NFS4_READW_LT : NFS4_READ_LT;
1141         return block ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
1142 }
1143
1144 static inline uint64_t nfs4_lock_length(struct file_lock *fl)
1145 {
1146         if (fl->fl_end == OFFSET_MAX)
1147                 return ~(uint64_t)0;
1148         return fl->fl_end - fl->fl_start + 1;
1149 }
1150
1151 static void encode_lockowner(struct xdr_stream *xdr, const struct nfs_lowner *lowner)
1152 {
1153         __be32 *p;
1154
1155         p = reserve_space(xdr, 32);
1156         p = xdr_encode_hyper(p, lowner->clientid);
1157         *p++ = cpu_to_be32(20);
1158         p = xdr_encode_opaque_fixed(p, "lock id:", 8);
1159         *p++ = cpu_to_be32(lowner->s_dev);
1160         xdr_encode_hyper(p, lowner->id);
1161 }
1162
1163 /*
1164  * opcode,type,reclaim,offset,length,new_lock_owner = 32
1165  * open_seqid,open_stateid,lock_seqid,lock_owner.clientid, lock_owner.id = 40
1166  */
1167 static void encode_lock(struct xdr_stream *xdr, const struct nfs_lock_args *args, struct compound_hdr *hdr)
1168 {
1169         __be32 *p;
1170
1171         p = reserve_space(xdr, 32);
1172         *p++ = cpu_to_be32(OP_LOCK);
1173         *p++ = cpu_to_be32(nfs4_lock_type(args->fl, args->block));
1174         *p++ = cpu_to_be32(args->reclaim);
1175         p = xdr_encode_hyper(p, args->fl->fl_start);
1176         p = xdr_encode_hyper(p, nfs4_lock_length(args->fl));
1177         *p = cpu_to_be32(args->new_lock_owner);
1178         if (args->new_lock_owner){
1179                 p = reserve_space(xdr, 4+NFS4_STATEID_SIZE+4);
1180                 *p++ = cpu_to_be32(args->open_seqid->sequence->counter);
1181                 p = xdr_encode_opaque_fixed(p, args->open_stateid->data, NFS4_STATEID_SIZE);
1182                 *p++ = cpu_to_be32(args->lock_seqid->sequence->counter);
1183                 encode_lockowner(xdr, &args->lock_owner);
1184         }
1185         else {
1186                 p = reserve_space(xdr, NFS4_STATEID_SIZE+4);
1187                 p = xdr_encode_opaque_fixed(p, args->lock_stateid->data, NFS4_STATEID_SIZE);
1188                 *p = cpu_to_be32(args->lock_seqid->sequence->counter);
1189         }
1190         hdr->nops++;
1191         hdr->replen += decode_lock_maxsz;
1192 }
1193
1194 static void encode_lockt(struct xdr_stream *xdr, const struct nfs_lockt_args *args, struct compound_hdr *hdr)
1195 {
1196         __be32 *p;
1197
1198         p = reserve_space(xdr, 24);
1199         *p++ = cpu_to_be32(OP_LOCKT);
1200         *p++ = cpu_to_be32(nfs4_lock_type(args->fl, 0));
1201         p = xdr_encode_hyper(p, args->fl->fl_start);
1202         p = xdr_encode_hyper(p, nfs4_lock_length(args->fl));
1203         encode_lockowner(xdr, &args->lock_owner);
1204         hdr->nops++;
1205         hdr->replen += decode_lockt_maxsz;
1206 }
1207
1208 static void encode_locku(struct xdr_stream *xdr, const struct nfs_locku_args *args, struct compound_hdr *hdr)
1209 {
1210         __be32 *p;
1211
1212         p = reserve_space(xdr, 12+NFS4_STATEID_SIZE+16);
1213         *p++ = cpu_to_be32(OP_LOCKU);
1214         *p++ = cpu_to_be32(nfs4_lock_type(args->fl, 0));
1215         *p++ = cpu_to_be32(args->seqid->sequence->counter);
1216         p = xdr_encode_opaque_fixed(p, args->stateid->data, NFS4_STATEID_SIZE);
1217         p = xdr_encode_hyper(p, args->fl->fl_start);
1218         xdr_encode_hyper(p, nfs4_lock_length(args->fl));
1219         hdr->nops++;
1220         hdr->replen += decode_locku_maxsz;
1221 }
1222
1223 static void encode_release_lockowner(struct xdr_stream *xdr, const struct nfs_lowner *lowner, struct compound_hdr *hdr)
1224 {
1225         __be32 *p;
1226
1227         p = reserve_space(xdr, 4);
1228         *p = cpu_to_be32(OP_RELEASE_LOCKOWNER);
1229         encode_lockowner(xdr, lowner);
1230         hdr->nops++;
1231         hdr->replen += decode_release_lockowner_maxsz;
1232 }
1233
1234 static void encode_lookup(struct xdr_stream *xdr, const struct qstr *name, struct compound_hdr *hdr)
1235 {
1236         int len = name->len;
1237         __be32 *p;
1238
1239         p = reserve_space(xdr, 8 + len);
1240         *p++ = cpu_to_be32(OP_LOOKUP);
1241         xdr_encode_opaque(p, name->name, len);
1242         hdr->nops++;
1243         hdr->replen += decode_lookup_maxsz;
1244 }
1245
1246 static void encode_share_access(struct xdr_stream *xdr, fmode_t fmode)
1247 {
1248         __be32 *p;
1249
1250         p = reserve_space(xdr, 8);
1251         switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1252         case FMODE_READ:
1253                 *p++ = cpu_to_be32(NFS4_SHARE_ACCESS_READ);
1254                 break;
1255         case FMODE_WRITE:
1256                 *p++ = cpu_to_be32(NFS4_SHARE_ACCESS_WRITE);
1257                 break;
1258         case FMODE_READ|FMODE_WRITE:
1259                 *p++ = cpu_to_be32(NFS4_SHARE_ACCESS_BOTH);
1260                 break;
1261         default:
1262                 *p++ = cpu_to_be32(0);
1263         }
1264         *p = cpu_to_be32(0);            /* for linux, share_deny = 0 always */
1265 }
1266
1267 static inline void encode_openhdr(struct xdr_stream *xdr, const struct nfs_openargs *arg)
1268 {
1269         __be32 *p;
1270  /*
1271  * opcode 4, seqid 4, share_access 4, share_deny 4, clientid 8, ownerlen 4,
1272  * owner 4 = 32
1273  */
1274         p = reserve_space(xdr, 8);
1275         *p++ = cpu_to_be32(OP_OPEN);
1276         *p = cpu_to_be32(arg->seqid->sequence->counter);
1277         encode_share_access(xdr, arg->fmode);
1278         p = reserve_space(xdr, 32);
1279         p = xdr_encode_hyper(p, arg->clientid);
1280         *p++ = cpu_to_be32(20);
1281         p = xdr_encode_opaque_fixed(p, "open id:", 8);
1282         *p++ = cpu_to_be32(arg->server->s_dev);
1283         xdr_encode_hyper(p, arg->id);
1284 }
1285
1286 static inline void encode_createmode(struct xdr_stream *xdr, const struct nfs_openargs *arg)
1287 {
1288         __be32 *p;
1289         struct nfs_client *clp;
1290
1291         p = reserve_space(xdr, 4);
1292         switch(arg->open_flags & O_EXCL) {
1293         case 0:
1294                 *p = cpu_to_be32(NFS4_CREATE_UNCHECKED);
1295                 encode_attrs(xdr, arg->u.attrs, arg->server);
1296                 break;
1297         default:
1298                 clp = arg->server->nfs_client;
1299                 if (clp->cl_mvops->minor_version > 0) {
1300                         if (nfs4_has_persistent_session(clp)) {
1301                                 *p = cpu_to_be32(NFS4_CREATE_GUARDED);
1302                                 encode_attrs(xdr, arg->u.attrs, arg->server);
1303                         } else {
1304                                 struct iattr dummy;
1305
1306                                 *p = cpu_to_be32(NFS4_CREATE_EXCLUSIVE4_1);
1307                                 encode_nfs4_verifier(xdr, &arg->u.verifier);
1308                                 dummy.ia_valid = 0;
1309                                 encode_attrs(xdr, &dummy, arg->server);
1310                         }
1311                 } else {
1312                         *p = cpu_to_be32(NFS4_CREATE_EXCLUSIVE);
1313                         encode_nfs4_verifier(xdr, &arg->u.verifier);
1314                 }
1315         }
1316 }
1317
1318 static void encode_opentype(struct xdr_stream *xdr, const struct nfs_openargs *arg)
1319 {
1320         __be32 *p;
1321
1322         p = reserve_space(xdr, 4);
1323         switch (arg->open_flags & O_CREAT) {
1324         case 0:
1325                 *p = cpu_to_be32(NFS4_OPEN_NOCREATE);
1326                 break;
1327         default:
1328                 BUG_ON(arg->claim != NFS4_OPEN_CLAIM_NULL);
1329                 *p = cpu_to_be32(NFS4_OPEN_CREATE);
1330                 encode_createmode(xdr, arg);
1331         }
1332 }
1333
1334 static inline void encode_delegation_type(struct xdr_stream *xdr, fmode_t delegation_type)
1335 {
1336         __be32 *p;
1337
1338         p = reserve_space(xdr, 4);
1339         switch (delegation_type) {
1340         case 0:
1341                 *p = cpu_to_be32(NFS4_OPEN_DELEGATE_NONE);
1342                 break;
1343         case FMODE_READ:
1344                 *p = cpu_to_be32(NFS4_OPEN_DELEGATE_READ);
1345                 break;
1346         case FMODE_WRITE|FMODE_READ:
1347                 *p = cpu_to_be32(NFS4_OPEN_DELEGATE_WRITE);
1348                 break;
1349         default:
1350                 BUG();
1351         }
1352 }
1353
1354 static inline void encode_claim_null(struct xdr_stream *xdr, const struct qstr *name)
1355 {
1356         __be32 *p;
1357
1358         p = reserve_space(xdr, 4);
1359         *p = cpu_to_be32(NFS4_OPEN_CLAIM_NULL);
1360         encode_string(xdr, name->len, name->name);
1361 }
1362
1363 static inline void encode_claim_previous(struct xdr_stream *xdr, fmode_t type)
1364 {
1365         __be32 *p;
1366
1367         p = reserve_space(xdr, 4);
1368         *p = cpu_to_be32(NFS4_OPEN_CLAIM_PREVIOUS);
1369         encode_delegation_type(xdr, type);
1370 }
1371
1372 static inline void encode_claim_delegate_cur(struct xdr_stream *xdr, const struct qstr *name, const nfs4_stateid *stateid)
1373 {
1374         __be32 *p;
1375
1376         p = reserve_space(xdr, 4+NFS4_STATEID_SIZE);
1377         *p++ = cpu_to_be32(NFS4_OPEN_CLAIM_DELEGATE_CUR);
1378         xdr_encode_opaque_fixed(p, stateid->data, NFS4_STATEID_SIZE);
1379         encode_string(xdr, name->len, name->name);
1380 }
1381
1382 static void encode_open(struct xdr_stream *xdr, const struct nfs_openargs *arg, struct compound_hdr *hdr)
1383 {
1384         encode_openhdr(xdr, arg);
1385         encode_opentype(xdr, arg);
1386         switch (arg->claim) {
1387         case NFS4_OPEN_CLAIM_NULL:
1388                 encode_claim_null(xdr, arg->name);
1389                 break;
1390         case NFS4_OPEN_CLAIM_PREVIOUS:
1391                 encode_claim_previous(xdr, arg->u.delegation_type);
1392                 break;
1393         case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1394                 encode_claim_delegate_cur(xdr, arg->name, &arg->u.delegation);
1395                 break;
1396         default:
1397                 BUG();
1398         }
1399         hdr->nops++;
1400         hdr->replen += decode_open_maxsz;
1401 }
1402
1403 static void encode_open_confirm(struct xdr_stream *xdr, const struct nfs_open_confirmargs *arg, struct compound_hdr *hdr)
1404 {
1405         __be32 *p;
1406
1407         p = reserve_space(xdr, 4+NFS4_STATEID_SIZE+4);
1408         *p++ = cpu_to_be32(OP_OPEN_CONFIRM);
1409         p = xdr_encode_opaque_fixed(p, arg->stateid->data, NFS4_STATEID_SIZE);
1410         *p = cpu_to_be32(arg->seqid->sequence->counter);
1411         hdr->nops++;
1412         hdr->replen += decode_open_confirm_maxsz;
1413 }
1414
1415 static void encode_open_downgrade(struct xdr_stream *xdr, const struct nfs_closeargs *arg, struct compound_hdr *hdr)
1416 {
1417         __be32 *p;
1418
1419         p = reserve_space(xdr, 4+NFS4_STATEID_SIZE+4);
1420         *p++ = cpu_to_be32(OP_OPEN_DOWNGRADE);
1421         p = xdr_encode_opaque_fixed(p, arg->stateid->data, NFS4_STATEID_SIZE);
1422         *p = cpu_to_be32(arg->seqid->sequence->counter);
1423         encode_share_access(xdr, arg->fmode);
1424         hdr->nops++;
1425         hdr->replen += decode_open_downgrade_maxsz;
1426 }
1427
1428 static void
1429 encode_putfh(struct xdr_stream *xdr, const struct nfs_fh *fh, struct compound_hdr *hdr)
1430 {
1431         int len = fh->size;
1432         __be32 *p;
1433
1434         p = reserve_space(xdr, 8 + len);
1435         *p++ = cpu_to_be32(OP_PUTFH);
1436         xdr_encode_opaque(p, fh->data, len);
1437         hdr->nops++;
1438         hdr->replen += decode_putfh_maxsz;
1439 }
1440
1441 static void encode_putrootfh(struct xdr_stream *xdr, struct compound_hdr *hdr)
1442 {
1443         __be32 *p;
1444
1445         p = reserve_space(xdr, 4);
1446         *p = cpu_to_be32(OP_PUTROOTFH);
1447         hdr->nops++;
1448         hdr->replen += decode_putrootfh_maxsz;
1449 }
1450
1451 static void encode_stateid(struct xdr_stream *xdr, const struct nfs_open_context *ctx, const struct nfs_lock_context *l_ctx, int zero_seqid)
1452 {
1453         nfs4_stateid stateid;
1454         __be32 *p;
1455
1456         p = reserve_space(xdr, NFS4_STATEID_SIZE);
1457         if (ctx->state != NULL) {
1458                 nfs4_copy_stateid(&stateid, ctx->state, l_ctx->lockowner, l_ctx->pid);
1459                 if (zero_seqid)
1460                         stateid.stateid.seqid = 0;
1461                 xdr_encode_opaque_fixed(p, stateid.data, NFS4_STATEID_SIZE);
1462         } else
1463                 xdr_encode_opaque_fixed(p, zero_stateid.data, NFS4_STATEID_SIZE);
1464 }
1465
1466 static void encode_read(struct xdr_stream *xdr, const struct nfs_readargs *args, struct compound_hdr *hdr)
1467 {
1468         __be32 *p;
1469
1470         p = reserve_space(xdr, 4);
1471         *p = cpu_to_be32(OP_READ);
1472
1473         encode_stateid(xdr, args->context, args->lock_context,
1474                        hdr->minorversion);
1475
1476         p = reserve_space(xdr, 12);
1477         p = xdr_encode_hyper(p, args->offset);
1478         *p = cpu_to_be32(args->count);
1479         hdr->nops++;
1480         hdr->replen += decode_read_maxsz;
1481 }
1482
1483 static void encode_readdir(struct xdr_stream *xdr, const struct nfs4_readdir_arg *readdir, struct rpc_rqst *req, struct compound_hdr *hdr)
1484 {
1485         uint32_t attrs[2] = {
1486                 FATTR4_WORD0_RDATTR_ERROR,
1487                 FATTR4_WORD1_MOUNTED_ON_FILEID,
1488         };
1489         uint32_t dircount = readdir->count >> 1;
1490         __be32 *p;
1491
1492         if (readdir->plus) {
1493                 attrs[0] |= FATTR4_WORD0_TYPE|FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE|
1494                         FATTR4_WORD0_FSID|FATTR4_WORD0_FILEHANDLE|FATTR4_WORD0_FILEID;
1495                 attrs[1] |= FATTR4_WORD1_MODE|FATTR4_WORD1_NUMLINKS|FATTR4_WORD1_OWNER|
1496                         FATTR4_WORD1_OWNER_GROUP|FATTR4_WORD1_RAWDEV|
1497                         FATTR4_WORD1_SPACE_USED|FATTR4_WORD1_TIME_ACCESS|
1498                         FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
1499                 dircount >>= 1;
1500         }
1501         /* Use mounted_on_fileid only if the server supports it */
1502         if (!(readdir->bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID))
1503                 attrs[0] |= FATTR4_WORD0_FILEID;
1504
1505         p = reserve_space(xdr, 12+NFS4_VERIFIER_SIZE+20);
1506         *p++ = cpu_to_be32(OP_READDIR);
1507         p = xdr_encode_hyper(p, readdir->cookie);
1508         p = xdr_encode_opaque_fixed(p, readdir->verifier.data, NFS4_VERIFIER_SIZE);
1509         *p++ = cpu_to_be32(dircount);
1510         *p++ = cpu_to_be32(readdir->count);
1511         *p++ = cpu_to_be32(2);
1512
1513         *p++ = cpu_to_be32(attrs[0] & readdir->bitmask[0]);
1514         *p = cpu_to_be32(attrs[1] & readdir->bitmask[1]);
1515         hdr->nops++;
1516         hdr->replen += decode_readdir_maxsz;
1517         dprintk("%s: cookie = %Lu, verifier = %08x:%08x, bitmap = %08x:%08x\n",
1518                         __func__,
1519                         (unsigned long long)readdir->cookie,
1520                         ((u32 *)readdir->verifier.data)[0],
1521                         ((u32 *)readdir->verifier.data)[1],
1522                         attrs[0] & readdir->bitmask[0],
1523                         attrs[1] & readdir->bitmask[1]);
1524 }
1525
1526 static void encode_readlink(struct xdr_stream *xdr, const struct nfs4_readlink *readlink, struct rpc_rqst *req, struct compound_hdr *hdr)
1527 {
1528         __be32 *p;
1529
1530         p = reserve_space(xdr, 4);
1531         *p = cpu_to_be32(OP_READLINK);
1532         hdr->nops++;
1533         hdr->replen += decode_readlink_maxsz;
1534 }
1535
1536 static void encode_remove(struct xdr_stream *xdr, const struct qstr *name, struct compound_hdr *hdr)
1537 {
1538         __be32 *p;
1539
1540         p = reserve_space(xdr, 8 + name->len);
1541         *p++ = cpu_to_be32(OP_REMOVE);
1542         xdr_encode_opaque(p, name->name, name->len);
1543         hdr->nops++;
1544         hdr->replen += decode_remove_maxsz;
1545 }
1546
1547 static void encode_rename(struct xdr_stream *xdr, const struct qstr *oldname, const struct qstr *newname, struct compound_hdr *hdr)
1548 {
1549         __be32 *p;
1550
1551         p = reserve_space(xdr, 4);
1552         *p = cpu_to_be32(OP_RENAME);
1553         encode_string(xdr, oldname->len, oldname->name);
1554         encode_string(xdr, newname->len, newname->name);
1555         hdr->nops++;
1556         hdr->replen += decode_rename_maxsz;
1557 }
1558
1559 static void encode_renew(struct xdr_stream *xdr, const struct nfs_client *client_stateid, struct compound_hdr *hdr)
1560 {
1561         __be32 *p;
1562
1563         p = reserve_space(xdr, 12);
1564         *p++ = cpu_to_be32(OP_RENEW);
1565         xdr_encode_hyper(p, client_stateid->cl_clientid);
1566         hdr->nops++;
1567         hdr->replen += decode_renew_maxsz;
1568 }
1569
1570 static void
1571 encode_restorefh(struct xdr_stream *xdr, struct compound_hdr *hdr)
1572 {
1573         __be32 *p;
1574
1575         p = reserve_space(xdr, 4);
1576         *p = cpu_to_be32(OP_RESTOREFH);
1577         hdr->nops++;
1578         hdr->replen += decode_restorefh_maxsz;
1579 }
1580
1581 static void
1582 encode_setacl(struct xdr_stream *xdr, struct nfs_setaclargs *arg, struct compound_hdr *hdr)
1583 {
1584         __be32 *p;
1585
1586         p = reserve_space(xdr, 4+NFS4_STATEID_SIZE);
1587         *p++ = cpu_to_be32(OP_SETATTR);
1588         xdr_encode_opaque_fixed(p, zero_stateid.data, NFS4_STATEID_SIZE);
1589         p = reserve_space(xdr, 2*4);
1590         *p++ = cpu_to_be32(1);
1591         *p = cpu_to_be32(FATTR4_WORD0_ACL);
1592         BUG_ON(arg->acl_len % 4);
1593         p = reserve_space(xdr, 4);
1594         *p = cpu_to_be32(arg->acl_len);
1595         xdr_write_pages(xdr, arg->acl_pages, arg->acl_pgbase, arg->acl_len);
1596         hdr->nops++;
1597         hdr->replen += decode_setacl_maxsz;
1598 }
1599
1600 static void
1601 encode_savefh(struct xdr_stream *xdr, struct compound_hdr *hdr)
1602 {
1603         __be32 *p;
1604
1605         p = reserve_space(xdr, 4);
1606         *p = cpu_to_be32(OP_SAVEFH);
1607         hdr->nops++;
1608         hdr->replen += decode_savefh_maxsz;
1609 }
1610
1611 static void encode_setattr(struct xdr_stream *xdr, const struct nfs_setattrargs *arg, const struct nfs_server *server, struct compound_hdr *hdr)
1612 {
1613         __be32 *p;
1614
1615         p = reserve_space(xdr, 4+NFS4_STATEID_SIZE);
1616         *p++ = cpu_to_be32(OP_SETATTR);
1617         xdr_encode_opaque_fixed(p, arg->stateid.data, NFS4_STATEID_SIZE);
1618         hdr->nops++;
1619         hdr->replen += decode_setattr_maxsz;
1620         encode_attrs(xdr, arg->iap, server);
1621 }
1622
1623 static void encode_setclientid(struct xdr_stream *xdr, const struct nfs4_setclientid *setclientid, struct compound_hdr *hdr)
1624 {
1625         __be32 *p;
1626
1627         p = reserve_space(xdr, 4 + NFS4_VERIFIER_SIZE);
1628         *p++ = cpu_to_be32(OP_SETCLIENTID);
1629         xdr_encode_opaque_fixed(p, setclientid->sc_verifier->data, NFS4_VERIFIER_SIZE);
1630
1631         encode_string(xdr, setclientid->sc_name_len, setclientid->sc_name);
1632         p = reserve_space(xdr, 4);
1633         *p = cpu_to_be32(setclientid->sc_prog);
1634         encode_string(xdr, setclientid->sc_netid_len, setclientid->sc_netid);
1635         encode_string(xdr, setclientid->sc_uaddr_len, setclientid->sc_uaddr);
1636         p = reserve_space(xdr, 4);
1637         *p = cpu_to_be32(setclientid->sc_cb_ident);
1638         hdr->nops++;
1639         hdr->replen += decode_setclientid_maxsz;
1640 }
1641
1642 static void encode_setclientid_confirm(struct xdr_stream *xdr, const struct nfs4_setclientid_res *arg, struct compound_hdr *hdr)
1643 {
1644         __be32 *p;
1645
1646         p = reserve_space(xdr, 12 + NFS4_VERIFIER_SIZE);
1647         *p++ = cpu_to_be32(OP_SETCLIENTID_CONFIRM);
1648         p = xdr_encode_hyper(p, arg->clientid);
1649         xdr_encode_opaque_fixed(p, arg->confirm.data, NFS4_VERIFIER_SIZE);
1650         hdr->nops++;
1651         hdr->replen += decode_setclientid_confirm_maxsz;
1652 }
1653
1654 static void encode_write(struct xdr_stream *xdr, const struct nfs_writeargs *args, struct compound_hdr *hdr)
1655 {
1656         __be32 *p;
1657
1658         p = reserve_space(xdr, 4);
1659         *p = cpu_to_be32(OP_WRITE);
1660
1661         encode_stateid(xdr, args->context, args->lock_context,
1662                        hdr->minorversion);
1663
1664         p = reserve_space(xdr, 16);
1665         p = xdr_encode_hyper(p, args->offset);
1666         *p++ = cpu_to_be32(args->stable);
1667         *p = cpu_to_be32(args->count);
1668
1669         xdr_write_pages(xdr, args->pages, args->pgbase, args->count);
1670         hdr->nops++;
1671         hdr->replen += decode_write_maxsz;
1672 }
1673
1674 static void encode_delegreturn(struct xdr_stream *xdr, const nfs4_stateid *stateid, struct compound_hdr *hdr)
1675 {
1676         __be32 *p;
1677
1678         p = reserve_space(xdr, 4+NFS4_STATEID_SIZE);
1679
1680         *p++ = cpu_to_be32(OP_DELEGRETURN);
1681         xdr_encode_opaque_fixed(p, stateid->data, NFS4_STATEID_SIZE);
1682         hdr->nops++;
1683         hdr->replen += decode_delegreturn_maxsz;
1684 }
1685
1686 static void encode_secinfo(struct xdr_stream *xdr, const struct qstr *name, struct compound_hdr *hdr)
1687 {
1688         int len = name->len;
1689         __be32 *p;
1690
1691         p = reserve_space(xdr, 8 + len);
1692         *p++ = cpu_to_be32(OP_SECINFO);
1693         xdr_encode_opaque(p, name->name, len);
1694         hdr->nops++;
1695         hdr->replen += decode_secinfo_maxsz;
1696 }
1697
1698 #if defined(CONFIG_NFS_V4_1)
1699 /* NFSv4.1 operations */
1700 static void encode_exchange_id(struct xdr_stream *xdr,
1701                                struct nfs41_exchange_id_args *args,
1702                                struct compound_hdr *hdr)
1703 {
1704         __be32 *p;
1705
1706         p = reserve_space(xdr, 4 + sizeof(args->verifier->data));
1707         *p++ = cpu_to_be32(OP_EXCHANGE_ID);
1708         xdr_encode_opaque_fixed(p, args->verifier->data, sizeof(args->verifier->data));
1709
1710         encode_string(xdr, args->id_len, args->id);
1711
1712         p = reserve_space(xdr, 12);
1713         *p++ = cpu_to_be32(args->flags);
1714         *p++ = cpu_to_be32(0);  /* zero length state_protect4_a */
1715         *p = cpu_to_be32(0);    /* zero length implementation id array */
1716         hdr->nops++;
1717         hdr->replen += decode_exchange_id_maxsz;
1718 }
1719
1720 static void encode_create_session(struct xdr_stream *xdr,
1721                                   struct nfs41_create_session_args *args,
1722                                   struct compound_hdr *hdr)
1723 {
1724         __be32 *p;
1725         char machine_name[NFS4_MAX_MACHINE_NAME_LEN];
1726         uint32_t len;
1727         struct nfs_client *clp = args->client;
1728         u32 max_resp_sz_cached;
1729
1730         /*
1731          * Assumes OPEN is the biggest non-idempotent compound.
1732          * 2 is the verifier.
1733          */
1734         max_resp_sz_cached = (NFS4_dec_open_sz + RPC_REPHDRSIZE +
1735                               RPC_MAX_AUTH_SIZE + 2) * XDR_UNIT;
1736
1737         len = scnprintf(machine_name, sizeof(machine_name), "%s",
1738                         clp->cl_ipaddr);
1739
1740         p = reserve_space(xdr, 20 + 2*28 + 20 + len + 12);
1741         *p++ = cpu_to_be32(OP_CREATE_SESSION);
1742         p = xdr_encode_hyper(p, clp->cl_clientid);
1743         *p++ = cpu_to_be32(clp->cl_seqid);                      /*Sequence id */
1744         *p++ = cpu_to_be32(args->flags);                        /*flags */
1745
1746         /* Fore Channel */
1747         *p++ = cpu_to_be32(0);                          /* header padding size */
1748         *p++ = cpu_to_be32(args->fc_attrs.max_rqst_sz); /* max req size */
1749         *p++ = cpu_to_be32(args->fc_attrs.max_resp_sz); /* max resp size */
1750         *p++ = cpu_to_be32(max_resp_sz_cached);         /* Max resp sz cached */
1751         *p++ = cpu_to_be32(args->fc_attrs.max_ops);     /* max operations */
1752         *p++ = cpu_to_be32(args->fc_attrs.max_reqs);    /* max requests */
1753         *p++ = cpu_to_be32(0);                          /* rdmachannel_attrs */
1754
1755         /* Back Channel */
1756         *p++ = cpu_to_be32(0);                          /* header padding size */
1757         *p++ = cpu_to_be32(args->bc_attrs.max_rqst_sz); /* max req size */
1758         *p++ = cpu_to_be32(args->bc_attrs.max_resp_sz); /* max resp size */
1759         *p++ = cpu_to_be32(args->bc_attrs.max_resp_sz_cached);  /* Max resp sz cached */
1760         *p++ = cpu_to_be32(args->bc_attrs.max_ops);     /* max operations */
1761         *p++ = cpu_to_be32(args->bc_attrs.max_reqs);    /* max requests */
1762         *p++ = cpu_to_be32(0);                          /* rdmachannel_attrs */
1763
1764         *p++ = cpu_to_be32(args->cb_program);           /* cb_program */
1765         *p++ = cpu_to_be32(1);
1766         *p++ = cpu_to_be32(RPC_AUTH_UNIX);                      /* auth_sys */
1767
1768         /* authsys_parms rfc1831 */
1769         *p++ = cpu_to_be32((u32)clp->cl_boot_time.tv_nsec);     /* stamp */
1770         p = xdr_encode_opaque(p, machine_name, len);
1771         *p++ = cpu_to_be32(0);                          /* UID */
1772         *p++ = cpu_to_be32(0);                          /* GID */
1773         *p = cpu_to_be32(0);                            /* No more gids */
1774         hdr->nops++;
1775         hdr->replen += decode_create_session_maxsz;
1776 }
1777
1778 static void encode_destroy_session(struct xdr_stream *xdr,
1779                                    struct nfs4_session *session,
1780                                    struct compound_hdr *hdr)
1781 {
1782         __be32 *p;
1783         p = reserve_space(xdr, 4 + NFS4_MAX_SESSIONID_LEN);
1784         *p++ = cpu_to_be32(OP_DESTROY_SESSION);
1785         xdr_encode_opaque_fixed(p, session->sess_id.data, NFS4_MAX_SESSIONID_LEN);
1786         hdr->nops++;
1787         hdr->replen += decode_destroy_session_maxsz;
1788 }
1789
1790 static void encode_reclaim_complete(struct xdr_stream *xdr,
1791                                     struct nfs41_reclaim_complete_args *args,
1792                                     struct compound_hdr *hdr)
1793 {
1794         __be32 *p;
1795
1796         p = reserve_space(xdr, 8);
1797         *p++ = cpu_to_be32(OP_RECLAIM_COMPLETE);
1798         *p++ = cpu_to_be32(args->one_fs);
1799         hdr->nops++;
1800         hdr->replen += decode_reclaim_complete_maxsz;
1801 }
1802 #endif /* CONFIG_NFS_V4_1 */
1803
1804 static void encode_sequence(struct xdr_stream *xdr,
1805                             const struct nfs4_sequence_args *args,
1806                             struct compound_hdr *hdr)
1807 {
1808 #if defined(CONFIG_NFS_V4_1)
1809         struct nfs4_session *session = args->sa_session;
1810         struct nfs4_slot_table *tp;
1811         struct nfs4_slot *slot;
1812         __be32 *p;
1813
1814         if (!session)
1815                 return;
1816
1817         tp = &session->fc_slot_table;
1818
1819         WARN_ON(args->sa_slotid == NFS4_MAX_SLOT_TABLE);
1820         slot = tp->slots + args->sa_slotid;
1821
1822         p = reserve_space(xdr, 4 + NFS4_MAX_SESSIONID_LEN + 16);
1823         *p++ = cpu_to_be32(OP_SEQUENCE);
1824
1825         /*
1826          * Sessionid + seqid + slotid + max slotid + cache_this
1827          */
1828         dprintk("%s: sessionid=%u:%u:%u:%u seqid=%d slotid=%d "
1829                 "max_slotid=%d cache_this=%d\n",
1830                 __func__,
1831                 ((u32 *)session->sess_id.data)[0],
1832                 ((u32 *)session->sess_id.data)[1],
1833                 ((u32 *)session->sess_id.data)[2],
1834                 ((u32 *)session->sess_id.data)[3],
1835                 slot->seq_nr, args->sa_slotid,
1836                 tp->highest_used_slotid, args->sa_cache_this);
1837         p = xdr_encode_opaque_fixed(p, session->sess_id.data, NFS4_MAX_SESSIONID_LEN);
1838         *p++ = cpu_to_be32(slot->seq_nr);
1839         *p++ = cpu_to_be32(args->sa_slotid);
1840         *p++ = cpu_to_be32(tp->highest_used_slotid);
1841         *p = cpu_to_be32(args->sa_cache_this);
1842         hdr->nops++;
1843         hdr->replen += decode_sequence_maxsz;
1844 #endif /* CONFIG_NFS_V4_1 */
1845 }
1846
1847 #ifdef CONFIG_NFS_V4_1
1848 static void
1849 encode_getdeviceinfo(struct xdr_stream *xdr,
1850                      const struct nfs4_getdeviceinfo_args *args,
1851                      struct compound_hdr *hdr)
1852 {
1853         __be32 *p;
1854
1855         p = reserve_space(xdr, 16 + NFS4_DEVICEID4_SIZE);
1856         *p++ = cpu_to_be32(OP_GETDEVICEINFO);
1857         p = xdr_encode_opaque_fixed(p, args->pdev->dev_id.data,
1858                                     NFS4_DEVICEID4_SIZE);
1859         *p++ = cpu_to_be32(args->pdev->layout_type);
1860         *p++ = cpu_to_be32(args->pdev->pglen);          /* gdia_maxcount */
1861         *p++ = cpu_to_be32(0);                          /* bitmap length 0 */
1862         hdr->nops++;
1863         hdr->replen += decode_getdeviceinfo_maxsz;
1864 }
1865
1866 static void
1867 encode_layoutget(struct xdr_stream *xdr,
1868                       const struct nfs4_layoutget_args *args,
1869                       struct compound_hdr *hdr)
1870 {
1871         __be32 *p;
1872
1873         p = reserve_space(xdr, 44 + NFS4_STATEID_SIZE);
1874         *p++ = cpu_to_be32(OP_LAYOUTGET);
1875         *p++ = cpu_to_be32(0);     /* Signal layout available */
1876         *p++ = cpu_to_be32(args->type);
1877         *p++ = cpu_to_be32(args->range.iomode);
1878         p = xdr_encode_hyper(p, args->range.offset);
1879         p = xdr_encode_hyper(p, args->range.length);
1880         p = xdr_encode_hyper(p, args->minlength);
1881         p = xdr_encode_opaque_fixed(p, &args->stateid.data, NFS4_STATEID_SIZE);
1882         *p = cpu_to_be32(args->maxcount);
1883
1884         dprintk("%s: 1st type:0x%x iomode:%d off:%lu len:%lu mc:%d\n",
1885                 __func__,
1886                 args->type,
1887                 args->range.iomode,
1888                 (unsigned long)args->range.offset,
1889                 (unsigned long)args->range.length,
1890                 args->maxcount);
1891         hdr->nops++;
1892         hdr->replen += decode_layoutget_maxsz;
1893 }
1894
1895 static int
1896 encode_layoutcommit(struct xdr_stream *xdr,
1897                     struct inode *inode,
1898                     const struct nfs4_layoutcommit_args *args,
1899                     struct compound_hdr *hdr)
1900 {
1901         __be32 *p;
1902
1903         dprintk("%s: lbw: %llu type: %d\n", __func__, args->lastbytewritten,
1904                 NFS_SERVER(args->inode)->pnfs_curr_ld->id);
1905
1906         p = reserve_space(xdr, 44 + NFS4_STATEID_SIZE);
1907         *p++ = cpu_to_be32(OP_LAYOUTCOMMIT);
1908         /* Only whole file layouts */
1909         p = xdr_encode_hyper(p, 0); /* offset */
1910         p = xdr_encode_hyper(p, NFS4_MAX_UINT64); /* length */
1911         *p++ = cpu_to_be32(0); /* reclaim */
1912         p = xdr_encode_opaque_fixed(p, args->stateid.data, NFS4_STATEID_SIZE);
1913         *p++ = cpu_to_be32(1); /* newoffset = TRUE */
1914         p = xdr_encode_hyper(p, args->lastbytewritten);
1915         *p++ = cpu_to_be32(0); /* Never send time_modify_changed */
1916         *p++ = cpu_to_be32(NFS_SERVER(args->inode)->pnfs_curr_ld->id);/* type */
1917
1918         if (NFS_SERVER(inode)->pnfs_curr_ld->encode_layoutcommit)
1919                 NFS_SERVER(inode)->pnfs_curr_ld->encode_layoutcommit(
1920                         NFS_I(inode)->layout, xdr, args);
1921         else {
1922                 p = reserve_space(xdr, 4);
1923                 *p = cpu_to_be32(0); /* no layout-type payload */
1924         }
1925
1926         hdr->nops++;
1927         hdr->replen += decode_layoutcommit_maxsz;
1928         return 0;
1929 }
1930
1931 static void
1932 encode_layoutreturn(struct xdr_stream *xdr,
1933                     const struct nfs4_layoutreturn_args *args,
1934                     struct compound_hdr *hdr)
1935 {
1936         __be32 *p;
1937
1938         p = reserve_space(xdr, 20);
1939         *p++ = cpu_to_be32(OP_LAYOUTRETURN);
1940         *p++ = cpu_to_be32(0);          /* reclaim. always 0 for now */
1941         *p++ = cpu_to_be32(args->layout_type);
1942         *p++ = cpu_to_be32(IOMODE_ANY);
1943         *p = cpu_to_be32(RETURN_FILE);
1944         p = reserve_space(xdr, 16 + NFS4_STATEID_SIZE);
1945         p = xdr_encode_hyper(p, 0);
1946         p = xdr_encode_hyper(p, NFS4_MAX_UINT64);
1947         spin_lock(&args->inode->i_lock);
1948         xdr_encode_opaque_fixed(p, &args->stateid.data, NFS4_STATEID_SIZE);
1949         spin_unlock(&args->inode->i_lock);
1950         if (NFS_SERVER(args->inode)->pnfs_curr_ld->encode_layoutreturn) {
1951                 NFS_SERVER(args->inode)->pnfs_curr_ld->encode_layoutreturn(
1952                         NFS_I(args->inode)->layout, xdr, args);
1953         } else {
1954                 p = reserve_space(xdr, 4);
1955                 *p = cpu_to_be32(0);
1956         }
1957         hdr->nops++;
1958         hdr->replen += decode_layoutreturn_maxsz;
1959 }
1960
1961 static int
1962 encode_secinfo_no_name(struct xdr_stream *xdr,
1963                        const struct nfs41_secinfo_no_name_args *args,
1964                        struct compound_hdr *hdr)
1965 {
1966         __be32 *p;
1967         p = reserve_space(xdr, 8);
1968         *p++ = cpu_to_be32(OP_SECINFO_NO_NAME);
1969         *p++ = cpu_to_be32(args->style);
1970         hdr->nops++;
1971         hdr->replen += decode_secinfo_no_name_maxsz;
1972         return 0;
1973 }
1974
1975 static void encode_test_stateid(struct xdr_stream *xdr,
1976                                 struct nfs41_test_stateid_args *args,
1977                                 struct compound_hdr *hdr)
1978 {
1979         __be32 *p;
1980
1981         p = reserve_space(xdr, 8 + NFS4_STATEID_SIZE);
1982         *p++ = cpu_to_be32(OP_TEST_STATEID);
1983         *p++ = cpu_to_be32(1);
1984         xdr_encode_opaque_fixed(p, args->stateid->data, NFS4_STATEID_SIZE);
1985         hdr->nops++;
1986         hdr->replen += decode_test_stateid_maxsz;
1987 }
1988 #endif /* CONFIG_NFS_V4_1 */
1989
1990 /*
1991  * END OF "GENERIC" ENCODE ROUTINES.
1992  */
1993
1994 static u32 nfs4_xdr_minorversion(const struct nfs4_sequence_args *args)
1995 {
1996 #if defined(CONFIG_NFS_V4_1)
1997         if (args->sa_session)
1998                 return args->sa_session->clp->cl_mvops->minor_version;
1999 #endif /* CONFIG_NFS_V4_1 */
2000         return 0;
2001 }
2002
2003 /*
2004  * Encode an ACCESS request
2005  */
2006 static void nfs4_xdr_enc_access(struct rpc_rqst *req, struct xdr_stream *xdr,
2007                                 const struct nfs4_accessargs *args)
2008 {
2009         struct compound_hdr hdr = {
2010                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2011         };
2012
2013         encode_compound_hdr(xdr, req, &hdr);
2014         encode_sequence(xdr, &args->seq_args, &hdr);
2015         encode_putfh(xdr, args->fh, &hdr);
2016         encode_access(xdr, args->access, &hdr);
2017         encode_getfattr(xdr, args->bitmask, &hdr);
2018         encode_nops(&hdr);
2019 }
2020
2021 /*
2022  * Encode LOOKUP request
2023  */
2024 static void nfs4_xdr_enc_lookup(struct rpc_rqst *req, struct xdr_stream *xdr,
2025                                 const struct nfs4_lookup_arg *args)
2026 {
2027         struct compound_hdr hdr = {
2028                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2029         };
2030
2031         encode_compound_hdr(xdr, req, &hdr);
2032         encode_sequence(xdr, &args->seq_args, &hdr);
2033         encode_putfh(xdr, args->dir_fh, &hdr);
2034         encode_lookup(xdr, args->name, &hdr);
2035         encode_getfh(xdr, &hdr);
2036         encode_getfattr(xdr, args->bitmask, &hdr);
2037         encode_nops(&hdr);
2038 }
2039
2040 /*
2041  * Encode LOOKUP_ROOT request
2042  */
2043 static void nfs4_xdr_enc_lookup_root(struct rpc_rqst *req,
2044                                      struct xdr_stream *xdr,
2045                                      const struct nfs4_lookup_root_arg *args)
2046 {
2047         struct compound_hdr hdr = {
2048                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2049         };
2050
2051         encode_compound_hdr(xdr, req, &hdr);
2052         encode_sequence(xdr, &args->seq_args, &hdr);
2053         encode_putrootfh(xdr, &hdr);
2054         encode_getfh(xdr, &hdr);
2055         encode_getfattr(xdr, args->bitmask, &hdr);
2056         encode_nops(&hdr);
2057 }
2058
2059 /*
2060  * Encode REMOVE request
2061  */
2062 static void nfs4_xdr_enc_remove(struct rpc_rqst *req, struct xdr_stream *xdr,
2063                                 const struct nfs_removeargs *args)
2064 {
2065         struct compound_hdr hdr = {
2066                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2067         };
2068
2069         encode_compound_hdr(xdr, req, &hdr);
2070         encode_sequence(xdr, &args->seq_args, &hdr);
2071         encode_putfh(xdr, args->fh, &hdr);
2072         encode_remove(xdr, &args->name, &hdr);
2073         encode_getfattr(xdr, args->bitmask, &hdr);
2074         encode_nops(&hdr);
2075 }
2076
2077 /*
2078  * Encode RENAME request
2079  */
2080 static void nfs4_xdr_enc_rename(struct rpc_rqst *req, struct xdr_stream *xdr,
2081                                 const struct nfs_renameargs *args)
2082 {
2083         struct compound_hdr hdr = {
2084                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2085         };
2086
2087         encode_compound_hdr(xdr, req, &hdr);
2088         encode_sequence(xdr, &args->seq_args, &hdr);
2089         encode_putfh(xdr, args->old_dir, &hdr);
2090         encode_savefh(xdr, &hdr);
2091         encode_putfh(xdr, args->new_dir, &hdr);
2092         encode_rename(xdr, args->old_name, args->new_name, &hdr);
2093         encode_getfattr(xdr, args->bitmask, &hdr);
2094         encode_restorefh(xdr, &hdr);
2095         encode_getfattr(xdr, args->bitmask, &hdr);
2096         encode_nops(&hdr);
2097 }
2098
2099 /*
2100  * Encode LINK request
2101  */
2102 static void nfs4_xdr_enc_link(struct rpc_rqst *req, struct xdr_stream *xdr,
2103                              const struct nfs4_link_arg *args)
2104 {
2105         struct compound_hdr hdr = {
2106                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2107         };
2108
2109         encode_compound_hdr(xdr, req, &hdr);
2110         encode_sequence(xdr, &args->seq_args, &hdr);
2111         encode_putfh(xdr, args->fh, &hdr);
2112         encode_savefh(xdr, &hdr);
2113         encode_putfh(xdr, args->dir_fh, &hdr);
2114         encode_link(xdr, args->name, &hdr);
2115         encode_getfattr(xdr, args->bitmask, &hdr);
2116         encode_restorefh(xdr, &hdr);
2117         encode_getfattr(xdr, args->bitmask, &hdr);
2118         encode_nops(&hdr);
2119 }
2120
2121 /*
2122  * Encode CREATE request
2123  */
2124 static void nfs4_xdr_enc_create(struct rpc_rqst *req, struct xdr_stream *xdr,
2125                                 const struct nfs4_create_arg *args)
2126 {
2127         struct compound_hdr hdr = {
2128                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2129         };
2130
2131         encode_compound_hdr(xdr, req, &hdr);
2132         encode_sequence(xdr, &args->seq_args, &hdr);
2133         encode_putfh(xdr, args->dir_fh, &hdr);
2134         encode_savefh(xdr, &hdr);
2135         encode_create(xdr, args, &hdr);
2136         encode_getfh(xdr, &hdr);
2137         encode_getfattr(xdr, args->bitmask, &hdr);
2138         encode_restorefh(xdr, &hdr);
2139         encode_getfattr(xdr, args->bitmask, &hdr);
2140         encode_nops(&hdr);
2141 }
2142
2143 /*
2144  * Encode SYMLINK request
2145  */
2146 static void nfs4_xdr_enc_symlink(struct rpc_rqst *req, struct xdr_stream *xdr,
2147                                  const struct nfs4_create_arg *args)
2148 {
2149         nfs4_xdr_enc_create(req, xdr, args);
2150 }
2151
2152 /*
2153  * Encode GETATTR request
2154  */
2155 static void nfs4_xdr_enc_getattr(struct rpc_rqst *req, struct xdr_stream *xdr,
2156                                  const struct nfs4_getattr_arg *args)
2157 {
2158         struct compound_hdr hdr = {
2159                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2160         };
2161
2162         encode_compound_hdr(xdr, req, &hdr);
2163         encode_sequence(xdr, &args->seq_args, &hdr);
2164         encode_putfh(xdr, args->fh, &hdr);
2165         encode_getfattr(xdr, args->bitmask, &hdr);
2166         encode_nops(&hdr);
2167 }
2168
2169 /*
2170  * Encode a CLOSE request
2171  */
2172 static void nfs4_xdr_enc_close(struct rpc_rqst *req, struct xdr_stream *xdr,
2173                                struct nfs_closeargs *args)
2174 {
2175         struct compound_hdr hdr = {
2176                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2177         };
2178
2179         encode_compound_hdr(xdr, req, &hdr);
2180         encode_sequence(xdr, &args->seq_args, &hdr);
2181         encode_putfh(xdr, args->fh, &hdr);
2182         encode_close(xdr, args, &hdr);
2183         encode_getfattr(xdr, args->bitmask, &hdr);
2184         encode_nops(&hdr);
2185 }
2186
2187 /*
2188  * Encode an OPEN request
2189  */
2190 static void nfs4_xdr_enc_open(struct rpc_rqst *req, struct xdr_stream *xdr,
2191                               struct nfs_openargs *args)
2192 {
2193         struct compound_hdr hdr = {
2194                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2195         };
2196
2197         encode_compound_hdr(xdr, req, &hdr);
2198         encode_sequence(xdr, &args->seq_args, &hdr);
2199         encode_putfh(xdr, args->fh, &hdr);
2200         encode_savefh(xdr, &hdr);
2201         encode_open(xdr, args, &hdr);
2202         encode_getfh(xdr, &hdr);
2203         encode_getfattr(xdr, args->bitmask, &hdr);
2204         encode_restorefh(xdr, &hdr);
2205         encode_getfattr(xdr, args->bitmask, &hdr);
2206         encode_nops(&hdr);
2207 }
2208
2209 /*
2210  * Encode an OPEN_CONFIRM request
2211  */
2212 static void nfs4_xdr_enc_open_confirm(struct rpc_rqst *req,
2213                                       struct xdr_stream *xdr,
2214                                       struct nfs_open_confirmargs *args)
2215 {
2216         struct compound_hdr hdr = {
2217                 .nops   = 0,
2218         };
2219
2220         encode_compound_hdr(xdr, req, &hdr);
2221         encode_putfh(xdr, args->fh, &hdr);
2222         encode_open_confirm(xdr, args, &hdr);
2223         encode_nops(&hdr);
2224 }
2225
2226 /*
2227  * Encode an OPEN request with no attributes.
2228  */
2229 static void nfs4_xdr_enc_open_noattr(struct rpc_rqst *req,
2230                                      struct xdr_stream *xdr,
2231                                      struct nfs_openargs *args)
2232 {
2233         struct compound_hdr hdr = {
2234                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2235         };
2236
2237         encode_compound_hdr(xdr, req, &hdr);
2238         encode_sequence(xdr, &args->seq_args, &hdr);
2239         encode_putfh(xdr, args->fh, &hdr);
2240         encode_open(xdr, args, &hdr);
2241         encode_getfattr(xdr, args->bitmask, &hdr);
2242         encode_nops(&hdr);
2243 }
2244
2245 /*
2246  * Encode an OPEN_DOWNGRADE request
2247  */
2248 static void nfs4_xdr_enc_open_downgrade(struct rpc_rqst *req,
2249                                         struct xdr_stream *xdr,
2250                                         struct nfs_closeargs *args)
2251 {
2252         struct compound_hdr hdr = {
2253                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2254         };
2255
2256         encode_compound_hdr(xdr, req, &hdr);
2257         encode_sequence(xdr, &args->seq_args, &hdr);
2258         encode_putfh(xdr, args->fh, &hdr);
2259         encode_open_downgrade(xdr, args, &hdr);
2260         encode_getfattr(xdr, args->bitmask, &hdr);
2261         encode_nops(&hdr);
2262 }
2263
2264 /*
2265  * Encode a LOCK request
2266  */
2267 static void nfs4_xdr_enc_lock(struct rpc_rqst *req, struct xdr_stream *xdr,
2268                               struct nfs_lock_args *args)
2269 {
2270         struct compound_hdr hdr = {
2271                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2272         };
2273
2274         encode_compound_hdr(xdr, req, &hdr);
2275         encode_sequence(xdr, &args->seq_args, &hdr);
2276         encode_putfh(xdr, args->fh, &hdr);
2277         encode_lock(xdr, args, &hdr);
2278         encode_nops(&hdr);
2279 }
2280
2281 /*
2282  * Encode a LOCKT request
2283  */
2284 static void nfs4_xdr_enc_lockt(struct rpc_rqst *req, struct xdr_stream *xdr,
2285                                struct nfs_lockt_args *args)
2286 {
2287         struct compound_hdr hdr = {
2288                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2289         };
2290
2291         encode_compound_hdr(xdr, req, &hdr);
2292         encode_sequence(xdr, &args->seq_args, &hdr);
2293         encode_putfh(xdr, args->fh, &hdr);
2294         encode_lockt(xdr, args, &hdr);
2295         encode_nops(&hdr);
2296 }
2297
2298 /*
2299  * Encode a LOCKU request
2300  */
2301 static void nfs4_xdr_enc_locku(struct rpc_rqst *req, struct xdr_stream *xdr,
2302                                struct nfs_locku_args *args)
2303 {
2304         struct compound_hdr hdr = {
2305                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2306         };
2307
2308         encode_compound_hdr(xdr, req, &hdr);
2309         encode_sequence(xdr, &args->seq_args, &hdr);
2310         encode_putfh(xdr, args->fh, &hdr);
2311         encode_locku(xdr, args, &hdr);
2312         encode_nops(&hdr);
2313 }
2314
2315 static void nfs4_xdr_enc_release_lockowner(struct rpc_rqst *req,
2316                                            struct xdr_stream *xdr,
2317                                         struct nfs_release_lockowner_args *args)
2318 {
2319         struct compound_hdr hdr = {
2320                 .minorversion = 0,
2321         };
2322
2323         encode_compound_hdr(xdr, req, &hdr);
2324         encode_release_lockowner(xdr, &args->lock_owner, &hdr);
2325         encode_nops(&hdr);
2326 }
2327
2328 /*
2329  * Encode a READLINK request
2330  */
2331 static void nfs4_xdr_enc_readlink(struct rpc_rqst *req, struct xdr_stream *xdr,
2332                                   const struct nfs4_readlink *args)
2333 {
2334         struct compound_hdr hdr = {
2335                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2336         };
2337
2338         encode_compound_hdr(xdr, req, &hdr);
2339         encode_sequence(xdr, &args->seq_args, &hdr);
2340         encode_putfh(xdr, args->fh, &hdr);
2341         encode_readlink(xdr, args, req, &hdr);
2342
2343         xdr_inline_pages(&req->rq_rcv_buf, hdr.replen << 2, args->pages,
2344                         args->pgbase, args->pglen);
2345         encode_nops(&hdr);
2346 }
2347
2348 /*
2349  * Encode a READDIR request
2350  */
2351 static void nfs4_xdr_enc_readdir(struct rpc_rqst *req, struct xdr_stream *xdr,
2352                                  const struct nfs4_readdir_arg *args)
2353 {
2354         struct compound_hdr hdr = {
2355                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2356         };
2357
2358         encode_compound_hdr(xdr, req, &hdr);
2359         encode_sequence(xdr, &args->seq_args, &hdr);
2360         encode_putfh(xdr, args->fh, &hdr);
2361         encode_readdir(xdr, args, req, &hdr);
2362
2363         xdr_inline_pages(&req->rq_rcv_buf, hdr.replen << 2, args->pages,
2364                          args->pgbase, args->count);
2365         dprintk("%s: inlined page args = (%u, %p, %u, %u)\n",
2366                         __func__, hdr.replen << 2, args->pages,
2367                         args->pgbase, args->count);
2368         encode_nops(&hdr);
2369 }
2370
2371 /*
2372  * Encode a READ request
2373  */
2374 static void nfs4_xdr_enc_read(struct rpc_rqst *req, struct xdr_stream *xdr,
2375                               struct nfs_readargs *args)
2376 {
2377         struct compound_hdr hdr = {
2378                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2379         };
2380
2381         encode_compound_hdr(xdr, req, &hdr);
2382         encode_sequence(xdr, &args->seq_args, &hdr);
2383         encode_putfh(xdr, args->fh, &hdr);
2384         encode_read(xdr, args, &hdr);
2385
2386         xdr_inline_pages(&req->rq_rcv_buf, hdr.replen << 2,
2387                          args->pages, args->pgbase, args->count);
2388         req->rq_rcv_buf.flags |= XDRBUF_READ;
2389         encode_nops(&hdr);
2390 }
2391
2392 /*
2393  * Encode an SETATTR request
2394  */
2395 static void nfs4_xdr_enc_setattr(struct rpc_rqst *req, struct xdr_stream *xdr,
2396                                  struct nfs_setattrargs *args)
2397 {
2398         struct compound_hdr hdr = {
2399                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2400         };
2401
2402         encode_compound_hdr(xdr, req, &hdr);
2403         encode_sequence(xdr, &args->seq_args, &hdr);
2404         encode_putfh(xdr, args->fh, &hdr);
2405         encode_setattr(xdr, args, args->server, &hdr);
2406         encode_getfattr(xdr, args->bitmask, &hdr);
2407         encode_nops(&hdr);
2408 }
2409
2410 /*
2411  * Encode a GETACL request
2412  */
2413 static void nfs4_xdr_enc_getacl(struct rpc_rqst *req, struct xdr_stream *xdr,
2414                                 struct nfs_getaclargs *args)
2415 {
2416         struct compound_hdr hdr = {
2417                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2418         };
2419         uint32_t replen;
2420
2421         encode_compound_hdr(xdr, req, &hdr);
2422         encode_sequence(xdr, &args->seq_args, &hdr);
2423         encode_putfh(xdr, args->fh, &hdr);
2424         replen = hdr.replen + op_decode_hdr_maxsz + nfs4_fattr_bitmap_maxsz + 1;
2425         encode_getattr_two(xdr, FATTR4_WORD0_ACL, 0, &hdr);
2426
2427         xdr_inline_pages(&req->rq_rcv_buf, replen << 2,
2428                 args->acl_pages, args->acl_pgbase, args->acl_len);
2429         encode_nops(&hdr);
2430 }
2431
2432 /*
2433  * Encode a WRITE request
2434  */
2435 static void nfs4_xdr_enc_write(struct rpc_rqst *req, struct xdr_stream *xdr,
2436                                struct nfs_writeargs *args)
2437 {
2438         struct compound_hdr hdr = {
2439                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2440         };
2441
2442         encode_compound_hdr(xdr, req, &hdr);
2443         encode_sequence(xdr, &args->seq_args, &hdr);
2444         encode_putfh(xdr, args->fh, &hdr);
2445         encode_write(xdr, args, &hdr);
2446         req->rq_snd_buf.flags |= XDRBUF_WRITE;
2447         if (args->bitmask)
2448                 encode_getfattr(xdr, args->bitmask, &hdr);
2449         encode_nops(&hdr);
2450 }
2451
2452 /*
2453  *  a COMMIT request
2454  */
2455 static void nfs4_xdr_enc_commit(struct rpc_rqst *req, struct xdr_stream *xdr,
2456                                 struct nfs_writeargs *args)
2457 {
2458         struct compound_hdr hdr = {
2459                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2460         };
2461
2462         encode_compound_hdr(xdr, req, &hdr);
2463         encode_sequence(xdr, &args->seq_args, &hdr);
2464         encode_putfh(xdr, args->fh, &hdr);
2465         encode_commit(xdr, args, &hdr);
2466         if (args->bitmask)
2467                 encode_getfattr(xdr, args->bitmask, &hdr);
2468         encode_nops(&hdr);
2469 }
2470
2471 /*
2472  * FSINFO request
2473  */
2474 static void nfs4_xdr_enc_fsinfo(struct rpc_rqst *req, struct xdr_stream *xdr,
2475                                 struct nfs4_fsinfo_arg *args)
2476 {
2477         struct compound_hdr hdr = {
2478                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2479         };
2480
2481         encode_compound_hdr(xdr, req, &hdr);
2482         encode_sequence(xdr, &args->seq_args, &hdr);
2483         encode_putfh(xdr, args->fh, &hdr);
2484         encode_fsinfo(xdr, args->bitmask, &hdr);
2485         encode_nops(&hdr);
2486 }
2487
2488 /*
2489  * a PATHCONF request
2490  */
2491 static void nfs4_xdr_enc_pathconf(struct rpc_rqst *req, struct xdr_stream *xdr,
2492                                   const struct nfs4_pathconf_arg *args)
2493 {
2494         struct compound_hdr hdr = {
2495                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2496         };
2497
2498         encode_compound_hdr(xdr, req, &hdr);
2499         encode_sequence(xdr, &args->seq_args, &hdr);
2500         encode_putfh(xdr, args->fh, &hdr);
2501         encode_getattr_one(xdr, args->bitmask[0] & nfs4_pathconf_bitmap[0],
2502                            &hdr);
2503         encode_nops(&hdr);
2504 }
2505
2506 /*
2507  * a STATFS request
2508  */
2509 static void nfs4_xdr_enc_statfs(struct rpc_rqst *req, struct xdr_stream *xdr,
2510                                 const struct nfs4_statfs_arg *args)
2511 {
2512         struct compound_hdr hdr = {
2513                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2514         };
2515
2516         encode_compound_hdr(xdr, req, &hdr);
2517         encode_sequence(xdr, &args->seq_args, &hdr);
2518         encode_putfh(xdr, args->fh, &hdr);
2519         encode_getattr_two(xdr, args->bitmask[0] & nfs4_statfs_bitmap[0],
2520                            args->bitmask[1] & nfs4_statfs_bitmap[1], &hdr);
2521         encode_nops(&hdr);
2522 }
2523
2524 /*
2525  * GETATTR_BITMAP request
2526  */
2527 static void nfs4_xdr_enc_server_caps(struct rpc_rqst *req,
2528                                      struct xdr_stream *xdr,
2529                                      struct nfs4_server_caps_arg *args)
2530 {
2531         struct compound_hdr hdr = {
2532                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2533         };
2534
2535         encode_compound_hdr(xdr, req, &hdr);
2536         encode_sequence(xdr, &args->seq_args, &hdr);
2537         encode_putfh(xdr, args->fhandle, &hdr);
2538         encode_getattr_one(xdr, FATTR4_WORD0_SUPPORTED_ATTRS|
2539                            FATTR4_WORD0_LINK_SUPPORT|
2540                            FATTR4_WORD0_SYMLINK_SUPPORT|
2541                            FATTR4_WORD0_ACLSUPPORT, &hdr);
2542         encode_nops(&hdr);
2543 }
2544
2545 /*
2546  * a RENEW request
2547  */
2548 static void nfs4_xdr_enc_renew(struct rpc_rqst *req, struct xdr_stream *xdr,
2549                                struct nfs_client *clp)
2550 {
2551         struct compound_hdr hdr = {
2552                 .nops   = 0,
2553         };
2554
2555         encode_compound_hdr(xdr, req, &hdr);
2556         encode_renew(xdr, clp, &hdr);
2557         encode_nops(&hdr);
2558 }
2559
2560 /*
2561  * a SETCLIENTID request
2562  */
2563 static void nfs4_xdr_enc_setclientid(struct rpc_rqst *req,
2564                                      struct xdr_stream *xdr,
2565                                      struct nfs4_setclientid *sc)
2566 {
2567         struct compound_hdr hdr = {
2568                 .nops   = 0,
2569         };
2570
2571         encode_compound_hdr(xdr, req, &hdr);
2572         encode_setclientid(xdr, sc, &hdr);
2573         encode_nops(&hdr);
2574 }
2575
2576 /*
2577  * a SETCLIENTID_CONFIRM request
2578  */
2579 static void nfs4_xdr_enc_setclientid_confirm(struct rpc_rqst *req,
2580                                              struct xdr_stream *xdr,
2581                                              struct nfs4_setclientid_res *arg)
2582 {
2583         struct compound_hdr hdr = {
2584                 .nops   = 0,
2585         };
2586         const u32 lease_bitmap[2] = { FATTR4_WORD0_LEASE_TIME, 0 };
2587
2588         encode_compound_hdr(xdr, req, &hdr);
2589         encode_setclientid_confirm(xdr, arg, &hdr);
2590         encode_putrootfh(xdr, &hdr);
2591         encode_fsinfo(xdr, lease_bitmap, &hdr);
2592         encode_nops(&hdr);
2593 }
2594
2595 /*
2596  * DELEGRETURN request
2597  */
2598 static void nfs4_xdr_enc_delegreturn(struct rpc_rqst *req,
2599                                      struct xdr_stream *xdr,
2600                                      const struct nfs4_delegreturnargs *args)
2601 {
2602         struct compound_hdr hdr = {
2603                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2604         };
2605
2606         encode_compound_hdr(xdr, req, &hdr);
2607         encode_sequence(xdr, &args->seq_args, &hdr);
2608         encode_putfh(xdr, args->fhandle, &hdr);
2609         encode_delegreturn(xdr, args->stateid, &hdr);
2610         encode_getfattr(xdr, args->bitmask, &hdr);
2611         encode_nops(&hdr);
2612 }
2613
2614 /*
2615  * Encode FS_LOCATIONS request
2616  */
2617 static void nfs4_xdr_enc_fs_locations(struct rpc_rqst *req,
2618                                       struct xdr_stream *xdr,
2619                                       struct nfs4_fs_locations_arg *args)
2620 {
2621         struct compound_hdr hdr = {
2622                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2623         };
2624         uint32_t replen;
2625
2626         encode_compound_hdr(xdr, req, &hdr);
2627         encode_sequence(xdr, &args->seq_args, &hdr);
2628         encode_putfh(xdr, args->dir_fh, &hdr);
2629         encode_lookup(xdr, args->name, &hdr);
2630         replen = hdr.replen;    /* get the attribute into args->page */
2631         encode_fs_locations(xdr, args->bitmask, &hdr);
2632
2633         xdr_inline_pages(&req->rq_rcv_buf, replen << 2, &args->page,
2634                         0, PAGE_SIZE);
2635         encode_nops(&hdr);
2636 }
2637
2638 /*
2639  * Encode SECINFO request
2640  */
2641 static void nfs4_xdr_enc_secinfo(struct rpc_rqst *req,
2642                                 struct xdr_stream *xdr,
2643                                 struct nfs4_secinfo_arg *args)
2644 {
2645         struct compound_hdr hdr = {
2646                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2647         };
2648
2649         encode_compound_hdr(xdr, req, &hdr);
2650         encode_sequence(xdr, &args->seq_args, &hdr);
2651         encode_putfh(xdr, args->dir_fh, &hdr);
2652         encode_secinfo(xdr, args->name, &hdr);
2653         encode_nops(&hdr);
2654 }
2655
2656 #if defined(CONFIG_NFS_V4_1)
2657 /*
2658  * EXCHANGE_ID request
2659  */
2660 static void nfs4_xdr_enc_exchange_id(struct rpc_rqst *req,
2661                                      struct xdr_stream *xdr,
2662                                      struct nfs41_exchange_id_args *args)
2663 {
2664         struct compound_hdr hdr = {
2665                 .minorversion = args->client->cl_mvops->minor_version,
2666         };
2667
2668         encode_compound_hdr(xdr, req, &hdr);
2669         encode_exchange_id(xdr, args, &hdr);
2670         encode_nops(&hdr);
2671 }
2672
2673 /*
2674  * a CREATE_SESSION request
2675  */
2676 static void nfs4_xdr_enc_create_session(struct rpc_rqst *req,
2677                                         struct xdr_stream *xdr,
2678                                         struct nfs41_create_session_args *args)
2679 {
2680         struct compound_hdr hdr = {
2681                 .minorversion = args->client->cl_mvops->minor_version,
2682         };
2683
2684         encode_compound_hdr(xdr, req, &hdr);
2685         encode_create_session(xdr, args, &hdr);
2686         encode_nops(&hdr);
2687 }
2688
2689 /*
2690  * a DESTROY_SESSION request
2691  */
2692 static void nfs4_xdr_enc_destroy_session(struct rpc_rqst *req,
2693                                          struct xdr_stream *xdr,
2694                                          struct nfs4_session *session)
2695 {
2696         struct compound_hdr hdr = {
2697                 .minorversion = session->clp->cl_mvops->minor_version,
2698         };
2699
2700         encode_compound_hdr(xdr, req, &hdr);
2701         encode_destroy_session(xdr, session, &hdr);
2702         encode_nops(&hdr);
2703 }
2704
2705 /*
2706  * a SEQUENCE request
2707  */
2708 static void nfs4_xdr_enc_sequence(struct rpc_rqst *req, struct xdr_stream *xdr,
2709                                   struct nfs4_sequence_args *args)
2710 {
2711         struct compound_hdr hdr = {
2712                 .minorversion = nfs4_xdr_minorversion(args),
2713         };
2714
2715         encode_compound_hdr(xdr, req, &hdr);
2716         encode_sequence(xdr, args, &hdr);
2717         encode_nops(&hdr);
2718 }
2719
2720 /*
2721  * a GET_LEASE_TIME request
2722  */
2723 static void nfs4_xdr_enc_get_lease_time(struct rpc_rqst *req,
2724                                         struct xdr_stream *xdr,
2725                                         struct nfs4_get_lease_time_args *args)
2726 {
2727         struct compound_hdr hdr = {
2728                 .minorversion = nfs4_xdr_minorversion(&args->la_seq_args),
2729         };
2730         const u32 lease_bitmap[2] = { FATTR4_WORD0_LEASE_TIME, 0 };
2731
2732         encode_compound_hdr(xdr, req, &hdr);
2733         encode_sequence(xdr, &args->la_seq_args, &hdr);
2734         encode_putrootfh(xdr, &hdr);
2735         encode_fsinfo(xdr, lease_bitmap, &hdr);
2736         encode_nops(&hdr);
2737 }
2738
2739 /*
2740  * a RECLAIM_COMPLETE request
2741  */
2742 static void nfs4_xdr_enc_reclaim_complete(struct rpc_rqst *req,
2743                                           struct xdr_stream *xdr,
2744                                 struct nfs41_reclaim_complete_args *args)
2745 {
2746         struct compound_hdr hdr = {
2747                 .minorversion = nfs4_xdr_minorversion(&args->seq_args)
2748         };
2749
2750         encode_compound_hdr(xdr, req, &hdr);
2751         encode_sequence(xdr, &args->seq_args, &hdr);
2752         encode_reclaim_complete(xdr, args, &hdr);
2753         encode_nops(&hdr);
2754 }
2755
2756 /*
2757  * Encode GETDEVICEINFO request
2758  */
2759 static void nfs4_xdr_enc_getdeviceinfo(struct rpc_rqst *req,
2760                                        struct xdr_stream *xdr,
2761                                        struct nfs4_getdeviceinfo_args *args)
2762 {
2763         struct compound_hdr hdr = {
2764                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2765         };
2766
2767         encode_compound_hdr(xdr, req, &hdr);
2768         encode_sequence(xdr, &args->seq_args, &hdr);
2769         encode_getdeviceinfo(xdr, args, &hdr);
2770
2771         /* set up reply kvec. Subtract notification bitmap max size (2)
2772          * so that notification bitmap is put in xdr_buf tail */
2773         xdr_inline_pages(&req->rq_rcv_buf, (hdr.replen - 2) << 2,
2774                          args->pdev->pages, args->pdev->pgbase,
2775                          args->pdev->pglen);
2776
2777         encode_nops(&hdr);
2778 }
2779
2780 /*
2781  *  Encode LAYOUTGET request
2782  */
2783 static void nfs4_xdr_enc_layoutget(struct rpc_rqst *req,
2784                                    struct xdr_stream *xdr,
2785                                    struct nfs4_layoutget_args *args)
2786 {
2787         struct compound_hdr hdr = {
2788                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2789         };
2790
2791         encode_compound_hdr(xdr, req, &hdr);
2792         encode_sequence(xdr, &args->seq_args, &hdr);
2793         encode_putfh(xdr, NFS_FH(args->inode), &hdr);
2794         encode_layoutget(xdr, args, &hdr);
2795
2796         xdr_inline_pages(&req->rq_rcv_buf, hdr.replen << 2,
2797             args->layout.pages, 0, args->layout.pglen);
2798
2799         encode_nops(&hdr);
2800 }
2801
2802 /*
2803  *  Encode LAYOUTCOMMIT request
2804  */
2805 static void nfs4_xdr_enc_layoutcommit(struct rpc_rqst *req,
2806                                       struct xdr_stream *xdr,
2807                                       struct nfs4_layoutcommit_args *args)
2808 {
2809         struct nfs4_layoutcommit_data *data =
2810                 container_of(args, struct nfs4_layoutcommit_data, args);
2811         struct compound_hdr hdr = {
2812                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2813         };
2814
2815         encode_compound_hdr(xdr, req, &hdr);
2816         encode_sequence(xdr, &args->seq_args, &hdr);
2817         encode_putfh(xdr, NFS_FH(args->inode), &hdr);
2818         encode_layoutcommit(xdr, data->args.inode, args, &hdr);
2819         encode_getfattr(xdr, args->bitmask, &hdr);
2820         encode_nops(&hdr);
2821 }
2822
2823 /*
2824  * Encode LAYOUTRETURN request
2825  */
2826 static void nfs4_xdr_enc_layoutreturn(struct rpc_rqst *req,
2827                                       struct xdr_stream *xdr,
2828                                       struct nfs4_layoutreturn_args *args)
2829 {
2830         struct compound_hdr hdr = {
2831                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2832         };
2833
2834         encode_compound_hdr(xdr, req, &hdr);
2835         encode_sequence(xdr, &args->seq_args, &hdr);
2836         encode_putfh(xdr, NFS_FH(args->inode), &hdr);
2837         encode_layoutreturn(xdr, args, &hdr);
2838         encode_nops(&hdr);
2839 }
2840
2841 /*
2842  * Encode SECINFO_NO_NAME request
2843  */
2844 static int nfs4_xdr_enc_secinfo_no_name(struct rpc_rqst *req,
2845                                         struct xdr_stream *xdr,
2846                                         struct nfs41_secinfo_no_name_args *args)
2847 {
2848         struct compound_hdr hdr = {
2849                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2850         };
2851
2852         encode_compound_hdr(xdr, req, &hdr);
2853         encode_sequence(xdr, &args->seq_args, &hdr);
2854         encode_putrootfh(xdr, &hdr);
2855         encode_secinfo_no_name(xdr, args, &hdr);
2856         encode_nops(&hdr);
2857         return 0;
2858 }
2859
2860 /*
2861  *  Encode TEST_STATEID request
2862  */
2863 static void nfs4_xdr_enc_test_stateid(struct rpc_rqst *req,
2864                                       struct xdr_stream *xdr,
2865                                       struct nfs41_test_stateid_args *args)
2866 {
2867         struct compound_hdr hdr = {
2868                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2869         };
2870
2871         encode_compound_hdr(xdr, req, &hdr);
2872         encode_sequence(xdr, &args->seq_args, &hdr);
2873         encode_test_stateid(xdr, args, &hdr);
2874         encode_nops(&hdr);
2875 }
2876 #endif /* CONFIG_NFS_V4_1 */
2877
2878 static void print_overflow_msg(const char *func, const struct xdr_stream *xdr)
2879 {
2880         dprintk("nfs: %s: prematurely hit end of receive buffer. "
2881                 "Remaining buffer length is %tu words.\n",
2882                 func, xdr->end - xdr->p);
2883 }
2884
2885 static int decode_opaque_inline(struct xdr_stream *xdr, unsigned int *len, char **string)
2886 {
2887         __be32 *p;
2888
2889         p = xdr_inline_decode(xdr, 4);
2890         if (unlikely(!p))
2891                 goto out_overflow;
2892         *len = be32_to_cpup(p);
2893         p = xdr_inline_decode(xdr, *len);
2894         if (unlikely(!p))
2895                 goto out_overflow;
2896         *string = (char *)p;
2897         return 0;
2898 out_overflow:
2899         print_overflow_msg(__func__, xdr);
2900         return -EIO;
2901 }
2902
2903 static int decode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
2904 {
2905         __be32 *p;
2906
2907         p = xdr_inline_decode(xdr, 8);
2908         if (unlikely(!p))
2909                 goto out_overflow;
2910         hdr->status = be32_to_cpup(p++);
2911         hdr->taglen = be32_to_cpup(p);
2912
2913         p = xdr_inline_decode(xdr, hdr->taglen + 4);
2914         if (unlikely(!p))
2915                 goto out_overflow;
2916         hdr->tag = (char *)p;
2917         p += XDR_QUADLEN(hdr->taglen);
2918         hdr->nops = be32_to_cpup(p);
2919         if (unlikely(hdr->nops < 1))
2920                 return nfs4_stat_to_errno(hdr->status);
2921         return 0;
2922 out_overflow:
2923         print_overflow_msg(__func__, xdr);
2924         return -EIO;
2925 }
2926
2927 static int decode_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
2928 {
2929         __be32 *p;
2930         uint32_t opnum;
2931         int32_t nfserr;
2932
2933         p = xdr_inline_decode(xdr, 8);
2934         if (unlikely(!p))
2935                 goto out_overflow;
2936         opnum = be32_to_cpup(p++);
2937         if (opnum != expected) {
2938                 dprintk("nfs: Server returned operation"
2939                         " %d but we issued a request for %d\n",
2940                                 opnum, expected);
2941                 return -EIO;
2942         }
2943         nfserr = be32_to_cpup(p);
2944         if (nfserr != NFS_OK)
2945                 return nfs4_stat_to_errno(nfserr);
2946         return 0;
2947 out_overflow:
2948         print_overflow_msg(__func__, xdr);
2949         return -EIO;
2950 }
2951
2952 /* Dummy routine */
2953 static int decode_ace(struct xdr_stream *xdr, void *ace, struct nfs_client *clp)
2954 {
2955         __be32 *p;
2956         unsigned int strlen;
2957         char *str;
2958
2959         p = xdr_inline_decode(xdr, 12);
2960         if (likely(p))
2961                 return decode_opaque_inline(xdr, &strlen, &str);
2962         print_overflow_msg(__func__, xdr);
2963         return -EIO;
2964 }
2965
2966 static int decode_attr_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
2967 {
2968         uint32_t bmlen;
2969         __be32 *p;
2970
2971         p = xdr_inline_decode(xdr, 4);
2972         if (unlikely(!p))
2973                 goto out_overflow;
2974         bmlen = be32_to_cpup(p);
2975
2976         bitmap[0] = bitmap[1] = 0;
2977         p = xdr_inline_decode(xdr, (bmlen << 2));
2978         if (unlikely(!p))
2979                 goto out_overflow;
2980         if (bmlen > 0) {
2981                 bitmap[0] = be32_to_cpup(p++);
2982                 if (bmlen > 1)
2983                         bitmap[1] = be32_to_cpup(p);
2984         }
2985         return 0;
2986 out_overflow:
2987         print_overflow_msg(__func__, xdr);
2988         return -EIO;
2989 }
2990
2991 static inline int decode_attr_length(struct xdr_stream *xdr, uint32_t *attrlen, __be32 **savep)
2992 {
2993         __be32 *p;
2994
2995         p = xdr_inline_decode(xdr, 4);
2996         if (unlikely(!p))
2997                 goto out_overflow;
2998         *attrlen = be32_to_cpup(p);
2999         *savep = xdr->p;
3000         return 0;
3001 out_overflow:
3002         print_overflow_msg(__func__, xdr);
3003         return -EIO;
3004 }
3005
3006 static int decode_attr_supported(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *bitmask)
3007 {
3008         if (likely(bitmap[0] & FATTR4_WORD0_SUPPORTED_ATTRS)) {
3009                 int ret;
3010                 ret = decode_attr_bitmap(xdr, bitmask);
3011                 if (unlikely(ret < 0))
3012                         return ret;
3013                 bitmap[0] &= ~FATTR4_WORD0_SUPPORTED_ATTRS;
3014         } else
3015                 bitmask[0] = bitmask[1] = 0;
3016         dprintk("%s: bitmask=%08x:%08x\n", __func__, bitmask[0], bitmask[1]);
3017         return 0;
3018 }
3019
3020 static int decode_attr_type(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *type)
3021 {
3022         __be32 *p;
3023         int ret = 0;
3024
3025         *type = 0;
3026         if (unlikely(bitmap[0] & (FATTR4_WORD0_TYPE - 1U)))
3027                 return -EIO;
3028         if (likely(bitmap[0] & FATTR4_WORD0_TYPE)) {
3029                 p = xdr_inline_decode(xdr, 4);
3030                 if (unlikely(!p))
3031                         goto out_overflow;
3032                 *type = be32_to_cpup(p);
3033                 if (*type < NF4REG || *type > NF4NAMEDATTR) {
3034                         dprintk("%s: bad type %d\n", __func__, *type);
3035                         return -EIO;
3036                 }
3037                 bitmap[0] &= ~FATTR4_WORD0_TYPE;
3038                 ret = NFS_ATTR_FATTR_TYPE;
3039         }
3040         dprintk("%s: type=0%o\n", __func__, nfs_type2fmt[*type]);
3041         return ret;
3042 out_overflow:
3043         print_overflow_msg(__func__, xdr);
3044         return -EIO;
3045 }
3046
3047 static int decode_attr_change(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *change)
3048 {
3049         __be32 *p;
3050         int ret = 0;
3051
3052         *change = 0;
3053         if (unlikely(bitmap[0] & (FATTR4_WORD0_CHANGE - 1U)))
3054                 return -EIO;
3055         if (likely(bitmap[0] & FATTR4_WORD0_CHANGE)) {
3056                 p = xdr_inline_decode(xdr, 8);
3057                 if (unlikely(!p))
3058                         goto out_overflow;
3059                 xdr_decode_hyper(p, change);
3060                 bitmap[0] &= ~FATTR4_WORD0_CHANGE;
3061                 ret = NFS_ATTR_FATTR_CHANGE;
3062         }
3063         dprintk("%s: change attribute=%Lu\n", __func__,
3064                         (unsigned long long)*change);
3065         return ret;
3066 out_overflow:
3067         print_overflow_msg(__func__, xdr);
3068         return -EIO;
3069 }
3070
3071 static int decode_attr_size(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *size)
3072 {
3073         __be32 *p;
3074         int ret = 0;
3075
3076         *size = 0;
3077         if (unlikely(bitmap[0] & (FATTR4_WORD0_SIZE - 1U)))
3078                 return -EIO;
3079         if (likely(bitmap[0] & FATTR4_WORD0_SIZE)) {
3080                 p = xdr_inline_decode(xdr, 8);
3081                 if (unlikely(!p))
3082                         goto out_overflow;
3083                 xdr_decode_hyper(p, size);
3084                 bitmap[0] &= ~FATTR4_WORD0_SIZE;
3085                 ret = NFS_ATTR_FATTR_SIZE;
3086         }
3087         dprintk("%s: file size=%Lu\n", __func__, (unsigned long long)*size);
3088         return ret;
3089 out_overflow:
3090         print_overflow_msg(__func__, xdr);
3091         return -EIO;
3092 }
3093
3094 static int decode_attr_link_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
3095 {
3096         __be32 *p;
3097
3098         *res = 0;
3099         if (unlikely(bitmap[0] & (FATTR4_WORD0_LINK_SUPPORT - 1U)))
3100                 return -EIO;
3101         if (likely(bitmap[0] & FATTR4_WORD0_LINK_SUPPORT)) {
3102                 p = xdr_inline_decode(xdr, 4);
3103                 if (unlikely(!p))
3104                         goto out_overflow;
3105                 *res = be32_to_cpup(p);
3106                 bitmap[0] &= ~FATTR4_WORD0_LINK_SUPPORT;
3107         }
3108         dprintk("%s: link support=%s\n", __func__, *res == 0 ? "false" : "true");
3109         return 0;
3110 out_overflow:
3111         print_overflow_msg(__func__, xdr);
3112         return -EIO;
3113 }
3114
3115 static int decode_attr_symlink_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
3116 {
3117         __be32 *p;
3118
3119         *res = 0;
3120         if (unlikely(bitmap[0] & (FATTR4_WORD0_SYMLINK_SUPPORT - 1U)))
3121                 return -EIO;
3122         if (likely(bitmap[0] & FATTR4_WORD0_SYMLINK_SUPPORT)) {
3123                 p = xdr_inline_decode(xdr, 4);
3124                 if (unlikely(!p))
3125                         goto out_overflow;
3126                 *res = be32_to_cpup(p);
3127                 bitmap[0] &= ~FATTR4_WORD0_SYMLINK_SUPPORT;
3128         }
3129         dprintk("%s: symlink support=%s\n", __func__, *res == 0 ? "false" : "true");
3130         return 0;
3131 out_overflow:
3132         print_overflow_msg(__func__, xdr);
3133         return -EIO;
3134 }
3135
3136 static int decode_attr_fsid(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs_fsid *fsid)
3137 {
3138         __be32 *p;
3139         int ret = 0;
3140
3141         fsid->major = 0;
3142         fsid->minor = 0;
3143         if (unlikely(bitmap[0] & (FATTR4_WORD0_FSID - 1U)))
3144                 return -EIO;
3145         if (likely(bitmap[0] & FATTR4_WORD0_FSID)) {
3146                 p = xdr_inline_decode(xdr, 16);
3147                 if (unlikely(!p))
3148                         goto out_overflow;
3149                 p = xdr_decode_hyper(p, &fsid->major);
3150                 xdr_decode_hyper(p, &fsid->minor);
3151                 bitmap[0] &= ~FATTR4_WORD0_FSID;
3152                 ret = NFS_ATTR_FATTR_FSID;
3153         }
3154         dprintk("%s: fsid=(0x%Lx/0x%Lx)\n", __func__,
3155                         (unsigned long long)fsid->major,
3156                         (unsigned long long)fsid->minor);
3157         return ret;
3158 out_overflow:
3159         print_overflow_msg(__func__, xdr);
3160         return -EIO;
3161 }
3162
3163 static int decode_attr_lease_time(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
3164 {
3165         __be32 *p;
3166
3167         *res = 60;
3168         if (unlikely(bitmap[0] & (FATTR4_WORD0_LEASE_TIME - 1U)))
3169                 return -EIO;
3170         if (likely(bitmap[0] & FATTR4_WORD0_LEASE_TIME)) {
3171                 p = xdr_inline_decode(xdr, 4);
3172                 if (unlikely(!p))
3173                         goto out_overflow;
3174                 *res = be32_to_cpup(p);
3175                 bitmap[0] &= ~FATTR4_WORD0_LEASE_TIME;
3176         }
3177         dprintk("%s: file size=%u\n", __func__, (unsigned int)*res);
3178         return 0;
3179 out_overflow:
3180         print_overflow_msg(__func__, xdr);
3181         return -EIO;
3182 }
3183
3184 static int decode_attr_error(struct xdr_stream *xdr, uint32_t *bitmap, int32_t *res)
3185 {
3186         __be32 *p;
3187
3188         if (unlikely(bitmap[0] & (FATTR4_WORD0_RDATTR_ERROR - 1U)))
3189                 return -EIO;
3190         if (likely(bitmap[0] & FATTR4_WORD0_RDATTR_ERROR)) {
3191                 p = xdr_inline_decode(xdr, 4);
3192                 if (unlikely(!p))
3193                         goto out_overflow;
3194                 bitmap[0] &= ~FATTR4_WORD0_RDATTR_ERROR;
3195                 *res = -be32_to_cpup(p);
3196         }
3197         return 0;
3198 out_overflow:
3199         print_overflow_msg(__func__, xdr);
3200         return -EIO;
3201 }
3202
3203 static int decode_attr_filehandle(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs_fh *fh)
3204 {
3205         __be32 *p;
3206         int len;
3207
3208         if (fh != NULL)
3209                 memset(fh, 0, sizeof(*fh));
3210
3211         if (unlikely(bitmap[0] & (FATTR4_WORD0_FILEHANDLE - 1U)))
3212                 return -EIO;
3213         if (likely(bitmap[0] & FATTR4_WORD0_FILEHANDLE)) {
3214                 p = xdr_inline_decode(xdr, 4);
3215                 if (unlikely(!p))
3216                         goto out_overflow;
3217                 len = be32_to_cpup(p);
3218                 if (len > NFS4_FHSIZE)
3219                         return -EIO;
3220                 p = xdr_inline_decode(xdr, len);
3221                 if (unlikely(!p))
3222                         goto out_overflow;
3223                 if (fh != NULL) {
3224                         memcpy(fh->data, p, len);
3225                         fh->size = len;
3226                 }
3227                 bitmap[0] &= ~FATTR4_WORD0_FILEHANDLE;
3228         }
3229         return 0;
3230 out_overflow:
3231         print_overflow_msg(__func__, xdr);
3232         return -EIO;
3233 }
3234
3235 static int decode_attr_aclsupport(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
3236 {
3237         __be32 *p;
3238
3239         *res = ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL;
3240         if (unlikely(bitmap[0] & (FATTR4_WORD0_ACLSUPPORT - 1U)))
3241                 return -EIO;
3242         if (likely(bitmap[0] & FATTR4_WORD0_ACLSUPPORT)) {
3243                 p = xdr_inline_decode(xdr, 4);
3244                 if (unlikely(!p))
3245                         goto out_overflow;
3246                 *res = be32_to_cpup(p);
3247                 bitmap[0] &= ~FATTR4_WORD0_ACLSUPPORT;
3248         }
3249         dprintk("%s: ACLs supported=%u\n", __func__, (unsigned int)*res);
3250         return 0;
3251 out_overflow:
3252         print_overflow_msg(__func__, xdr);
3253         return -EIO;
3254 }
3255
3256 static int decode_attr_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
3257 {
3258         __be32 *p;
3259         int ret = 0;
3260
3261         *fileid = 0;
3262         if (unlikely(bitmap[0] & (FATTR4_WORD0_FILEID - 1U)))
3263                 return -EIO;
3264         if (likely(bitmap[0] & FATTR4_WORD0_FILEID)) {
3265                 p = xdr_inline_decode(xdr, 8);
3266                 if (unlikely(!p))
3267                         goto out_overflow;
3268                 xdr_decode_hyper(p, fileid);
3269                 bitmap[0] &= ~FATTR4_WORD0_FILEID;
3270                 ret = NFS_ATTR_FATTR_FILEID;
3271         }
3272         dprintk("%s: fileid=%Lu\n", __func__, (unsigned long long)*fileid);
3273         return ret;
3274 out_overflow:
3275         print_overflow_msg(__func__, xdr);
3276         return -EIO;
3277 }
3278
3279 static int decode_attr_mounted_on_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
3280 {
3281         __be32 *p;
3282         int ret = 0;
3283
3284         *fileid = 0;
3285         if (unlikely(bitmap[1] & (FATTR4_WORD1_MOUNTED_ON_FILEID - 1U)))
3286                 return -EIO;
3287         if (likely(bitmap[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)) {
3288                 p = xdr_inline_decode(xdr, 8);
3289                 if (unlikely(!p))
3290                         goto out_overflow;
3291                 xdr_decode_hyper(p, fileid);
3292                 bitmap[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
3293                 ret = NFS_ATTR_FATTR_MOUNTED_ON_FILEID;
3294         }
3295         dprintk("%s: fileid=%Lu\n", __func__, (unsigned long long)*fileid);
3296         return ret;
3297 out_overflow:
3298         print_overflow_msg(__func__, xdr);
3299         return -EIO;
3300 }
3301
3302 static int decode_attr_files_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
3303 {
3304         __be32 *p;
3305         int status = 0;
3306
3307         *res = 0;
3308         if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_AVAIL - 1U)))
3309                 return -EIO;
3310         if (likely(bitmap[0] & FATTR4_WORD0_FILES_AVAIL)) {
3311                 p = xdr_inline_decode(xdr, 8);
3312                 if (unlikely(!p))
3313                         goto out_overflow;
3314                 xdr_decode_hyper(p, res);
3315                 bitmap[0] &= ~FATTR4_WORD0_FILES_AVAIL;
3316         }
3317         dprintk("%s: files avail=%Lu\n", __func__, (unsigned long long)*res);
3318         return status;
3319 out_overflow:
3320         print_overflow_msg(__func__, xdr);
3321         return -EIO;
3322 }
3323
3324 static int decode_attr_files_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
3325 {
3326         __be32 *p;
3327         int status = 0;
3328
3329         *res = 0;
3330         if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_FREE - 1U)))
3331                 return -EIO;
3332         if (likely(bitmap[0] & FATTR4_WORD0_FILES_FREE)) {
3333                 p = xdr_inline_decode(xdr, 8);
3334                 if (unlikely(!p))
3335                         goto out_overflow;
3336                 xdr_decode_hyper(p, res);
3337                 bitmap[0] &= ~FATTR4_WORD0_FILES_FREE;
3338         }
3339         dprintk("%s: files free=%Lu\n", __func__, (unsigned long long)*res);
3340         return status;
3341 out_overflow:
3342         print_overflow_msg(__func__, xdr);
3343         return -EIO;
3344 }
3345
3346 static int decode_attr_files_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
3347 {
3348         __be32 *p;
3349         int status = 0;
3350
3351         *res = 0;
3352         if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_TOTAL - 1U)))
3353                 return -EIO;
3354         if (likely(bitmap[0] & FATTR4_WORD0_FILES_TOTAL)) {
3355                 p = xdr_inline_decode(xdr, 8);
3356                 if (unlikely(!p))
3357                         goto out_overflow;
3358                 xdr_decode_hyper(p, res);
3359                 bitmap[0] &= ~FATTR4_WORD0_FILES_TOTAL;
3360         }
3361         dprintk("%s: files total=%Lu\n", __func__, (unsigned long long)*res);
3362         return status;
3363 out_overflow:
3364         print_overflow_msg(__func__, xdr);
3365         return -EIO;
3366 }
3367
3368 static int decode_pathname(struct xdr_stream *xdr, struct nfs4_pathname *path)
3369 {
3370         u32 n;
3371         __be32 *p;
3372         int status = 0;
3373
3374         p = xdr_inline_decode(xdr, 4);
3375         if (unlikely(!p))
3376                 goto out_overflow;
3377         n = be32_to_cpup(p);
3378         if (n == 0)
3379                 goto root_path;
3380         dprintk("path ");
3381         path->ncomponents = 0;
3382         while (path->ncomponents < n) {
3383                 struct nfs4_string *component = &path->components[path->ncomponents];
3384                 status = decode_opaque_inline(xdr, &component->len, &component->data);
3385                 if (unlikely(status != 0))
3386                         goto out_eio;
3387                 if (path->ncomponents != n)
3388                         dprintk("/");
3389                 dprintk("%s", component->data);
3390                 if (path->ncomponents < NFS4_PATHNAME_MAXCOMPONENTS)
3391                         path->ncomponents++;
3392                 else {
3393                         dprintk("cannot parse %d components in path\n", n);
3394                         goto out_eio;
3395                 }
3396         }
3397 out:
3398         dprintk("\n");
3399         return status;
3400 root_path:
3401 /* a root pathname is sent as a zero component4 */
3402         path->ncomponents = 1;
3403         path->components[0].len=0;
3404         path->components[0].data=NULL;
3405         dprintk("path /\n");
3406         goto out;
3407 out_eio:
3408         dprintk(" status %d", status);
3409         status = -EIO;
3410         goto out;
3411 out_overflow:
3412         print_overflow_msg(__func__, xdr);
3413         return -EIO;
3414 }
3415
3416 static int decode_attr_fs_locations(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_fs_locations *res)
3417 {
3418         int n;
3419         __be32 *p;
3420         int status = -EIO;
3421
3422         if (unlikely(bitmap[0] & (FATTR4_WORD0_FS_LOCATIONS -1U)))
3423                 goto out;
3424         status = 0;
3425         if (unlikely(!(bitmap[0] & FATTR4_WORD0_FS_LOCATIONS)))
3426                 goto out;
3427         dprintk("%s: fsroot ", __func__);
3428         status = decode_pathname(xdr, &res->fs_path);
3429         if (unlikely(status != 0))
3430                 goto out;
3431         p = xdr_inline_decode(xdr, 4);
3432         if (unlikely(!p))
3433                 goto out_overflow;
3434         n = be32_to_cpup(p);
3435         if (n <= 0)
3436                 goto out_eio;
3437         res->nlocations = 0;
3438         while (res->nlocations < n) {
3439                 u32 m;
3440                 struct nfs4_fs_location *loc = &res->locations[res->nlocations];
3441
3442                 p = xdr_inline_decode(xdr, 4);
3443                 if (unlikely(!p))
3444                         goto out_overflow;
3445                 m = be32_to_cpup(p);
3446
3447                 loc->nservers = 0;
3448                 dprintk("%s: servers ", __func__);
3449                 while (loc->nservers < m) {
3450                         struct nfs4_string *server = &loc->servers[loc->nservers];
3451                         status = decode_opaque_inline(xdr, &server->len, &server->data);
3452                         if (unlikely(status != 0))
3453                                 goto out_eio;
3454                         dprintk("%s ", server->data);
3455                         if (loc->nservers < NFS4_FS_LOCATION_MAXSERVERS)
3456                                 loc->nservers++;
3457                         else {
3458                                 unsigned int i;
3459                                 dprintk("%s: using first %u of %u servers "
3460                                         "returned for location %u\n",
3461                                                 __func__,
3462                                                 NFS4_FS_LOCATION_MAXSERVERS,
3463                                                 m, res->nlocations);
3464                                 for (i = loc->nservers; i < m; i++) {
3465                                         unsigned int len;
3466                                         char *data;
3467                                         status = decode_opaque_inline(xdr, &len, &data);
3468                                         if (unlikely(status != 0))
3469                                                 goto out_eio;
3470                                 }
3471                         }
3472                 }
3473                 status = decode_pathname(xdr, &loc->rootpath);
3474                 if (unlikely(status != 0))
3475                         goto out_eio;
3476                 if (res->nlocations < NFS4_FS_LOCATIONS_MAXENTRIES)
3477                         res->nlocations++;
3478         }
3479         if (res->nlocations != 0)
3480                 status = NFS_ATTR_FATTR_V4_REFERRAL;
3481 out:
3482         dprintk("%s: fs_locations done, error = %d\n", __func__, status);
3483         return status;
3484 out_overflow:
3485         print_overflow_msg(__func__, xdr);
3486 out_eio:
3487         status = -EIO;
3488         goto out;
3489 }
3490
3491 static int decode_attr_maxfilesize(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
3492 {
3493         __be32 *p;
3494         int status = 0;
3495
3496         *res = 0;
3497         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXFILESIZE - 1U)))
3498                 return -EIO;
3499         if (likely(bitmap[0] & FATTR4_WORD0_MAXFILESIZE)) {
3500                 p = xdr_inline_decode(xdr, 8);
3501                 if (unlikely(!p))
3502                         goto out_overflow;
3503                 xdr_decode_hyper(p, res);
3504                 bitmap[0] &= ~FATTR4_WORD0_MAXFILESIZE;
3505         }
3506         dprintk("%s: maxfilesize=%Lu\n", __func__, (unsigned long long)*res);
3507         return status;
3508 out_overflow:
3509         print_overflow_msg(__func__, xdr);
3510         return -EIO;
3511 }
3512
3513 static int decode_attr_maxlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxlink)
3514 {
3515         __be32 *p;
3516         int status = 0;
3517
3518         *maxlink = 1;
3519         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXLINK - 1U)))
3520                 return -EIO;
3521         if (likely(bitmap[0] & FATTR4_WORD0_MAXLINK)) {
3522                 p = xdr_inline_decode(xdr, 4);
3523                 if (unlikely(!p))
3524                         goto out_overflow;
3525                 *maxlink = be32_to_cpup(p);
3526                 bitmap[0] &= ~FATTR4_WORD0_MAXLINK;
3527         }
3528         dprintk("%s: maxlink=%u\n", __func__, *maxlink);
3529         return status;
3530 out_overflow:
3531         print_overflow_msg(__func__, xdr);
3532         return -EIO;
3533 }
3534
3535 static int decode_attr_maxname(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxname)
3536 {
3537         __be32 *p;
3538         int status = 0;
3539
3540         *maxname = 1024;
3541         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXNAME - 1U)))
3542                 return -EIO;
3543         if (likely(bitmap[0] & FATTR4_WORD0_MAXNAME)) {
3544                 p = xdr_inline_decode(xdr, 4);
3545                 if (unlikely(!p))
3546                         goto out_overflow;
3547                 *maxname = be32_to_cpup(p);
3548                 bitmap[0] &= ~FATTR4_WORD0_MAXNAME;
3549         }
3550         dprintk("%s: maxname=%u\n", __func__, *maxname);
3551         return status;
3552 out_overflow:
3553         print_overflow_msg(__func__, xdr);
3554         return -EIO;
3555 }
3556
3557 static int decode_attr_maxread(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
3558 {
3559         __be32 *p;
3560         int status = 0;
3561
3562         *res = 1024;
3563         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXREAD - 1U)))
3564                 return -EIO;
3565         if (likely(bitmap[0] & FATTR4_WORD0_MAXREAD)) {
3566                 uint64_t maxread;
3567                 p = xdr_inline_decode(xdr, 8);
3568                 if (unlikely(!p))
3569                         goto out_overflow;
3570                 xdr_decode_hyper(p, &maxread);
3571                 if (maxread > 0x7FFFFFFF)
3572                         maxread = 0x7FFFFFFF;
3573                 *res = (uint32_t)maxread;
3574                 bitmap[0] &= ~FATTR4_WORD0_MAXREAD;
3575         }
3576         dprintk("%s: maxread=%lu\n", __func__, (unsigned long)*res);
3577         return status;
3578 out_overflow:
3579         print_overflow_msg(__func__, xdr);
3580         return -EIO;
3581 }
3582
3583 static int decode_attr_maxwrite(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
3584 {
3585         __be32 *p;
3586         int status = 0;
3587
3588         *res = 1024;
3589         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXWRITE - 1U)))
3590                 return -EIO;
3591         if (likely(bitmap[0] & FATTR4_WORD0_MAXWRITE)) {
3592                 uint64_t maxwrite;
3593                 p = xdr_inline_decode(xdr, 8);
3594                 if (unlikely(!p))
3595                         goto out_overflow;
3596                 xdr_decode_hyper(p, &maxwrite);
3597                 if (maxwrite > 0x7FFFFFFF)
3598                         maxwrite = 0x7FFFFFFF;
3599                 *res = (uint32_t)maxwrite;
3600                 bitmap[0] &= ~FATTR4_WORD0_MAXWRITE;
3601         }
3602         dprintk("%s: maxwrite=%lu\n", __func__, (unsigned long)*res);
3603         return status;
3604 out_overflow:
3605         print_overflow_msg(__func__, xdr);
3606         return -EIO;
3607 }
3608
3609 static int decode_attr_mode(struct xdr_stream *xdr, uint32_t *bitmap, umode_t *mode)
3610 {
3611         uint32_t tmp;
3612         __be32 *p;
3613         int ret = 0;
3614
3615         *mode = 0;
3616         if (unlikely(bitmap[1] & (FATTR4_WORD1_MODE - 1U)))
3617                 return -EIO;
3618         if (likely(bitmap[1] & FATTR4_WORD1_MODE)) {
3619                 p = xdr_inline_decode(xdr, 4);
3620                 if (unlikely(!p))
3621                         goto out_overflow;
3622                 tmp = be32_to_cpup(p);
3623                 *mode = tmp & ~S_IFMT;
3624                 bitmap[1] &= ~FATTR4_WORD1_MODE;
3625                 ret = NFS_ATTR_FATTR_MODE;
3626         }
3627         dprintk("%s: file mode=0%o\n", __func__, (unsigned int)*mode);
3628         return ret;
3629 out_overflow:
3630         print_overflow_msg(__func__, xdr);
3631         return -EIO;
3632 }
3633
3634 static int decode_attr_nlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *nlink)
3635 {
3636         __be32 *p;
3637         int ret = 0;
3638
3639         *nlink = 1;
3640         if (unlikely(bitmap[1] & (FATTR4_WORD1_NUMLINKS - 1U)))
3641                 return -EIO;
3642         if (likely(bitmap[1] & FATTR4_WORD1_NUMLINKS)) {
3643                 p = xdr_inline_decode(xdr, 4);
3644                 if (unlikely(!p))
3645                         goto out_overflow;
3646                 *nlink = be32_to_cpup(p);
3647                 bitmap[1] &= ~FATTR4_WORD1_NUMLINKS;
3648                 ret = NFS_ATTR_FATTR_NLINK;
3649         }
3650         dprintk("%s: nlink=%u\n", __func__, (unsigned int)*nlink);
3651         return ret;
3652 out_overflow:
3653         print_overflow_msg(__func__, xdr);
3654         return -EIO;
3655 }
3656
3657 static int decode_attr_owner(struct xdr_stream *xdr, uint32_t *bitmap,
3658                 const struct nfs_server *server, uint32_t *uid, int may_sleep)
3659 {
3660         uint32_t len;
3661         __be32 *p;
3662         int ret = 0;
3663
3664         *uid = -2;
3665         if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER - 1U)))
3666                 return -EIO;
3667         if (likely(bitmap[1] & FATTR4_WORD1_OWNER)) {
3668                 p = xdr_inline_decode(xdr, 4);
3669                 if (unlikely(!p))
3670                         goto out_overflow;
3671                 len = be32_to_cpup(p);
3672                 p = xdr_inline_decode(xdr, len);
3673                 if (unlikely(!p))
3674                         goto out_overflow;
3675                 if (!may_sleep) {
3676                         /* do nothing */
3677                 } else if (len < XDR_MAX_NETOBJ) {
3678                         if (nfs_map_name_to_uid(server, (char *)p, len, uid) == 0)
3679                                 ret = NFS_ATTR_FATTR_OWNER;
3680                         else
3681                                 dprintk("%s: nfs_map_name_to_uid failed!\n",
3682                                                 __func__);
3683                 } else
3684                         dprintk("%s: name too long (%u)!\n",
3685                                         __func__, len);
3686                 bitmap[1] &= ~FATTR4_WORD1_OWNER;
3687         }
3688         dprintk("%s: uid=%d\n", __func__, (int)*uid);
3689         return ret;
3690 out_overflow:
3691         print_overflow_msg(__func__, xdr);
3692         return -EIO;
3693 }
3694
3695 static int decode_attr_group(struct xdr_stream *xdr, uint32_t *bitmap,
3696                 const struct nfs_server *server, uint32_t *gid, int may_sleep)
3697 {
3698         uint32_t len;
3699         __be32 *p;
3700         int ret = 0;
3701
3702         *gid = -2;
3703         if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER_GROUP - 1U)))
3704                 return -EIO;
3705         if (likely(bitmap[1] & FATTR4_WORD1_OWNER_GROUP)) {
3706                 p = xdr_inline_decode(xdr, 4);
3707                 if (unlikely(!p))
3708                         goto out_overflow;
3709                 len = be32_to_cpup(p);
3710                 p = xdr_inline_decode(xdr, len);
3711                 if (unlikely(!p))
3712                         goto out_overflow;
3713                 if (!may_sleep) {
3714                         /* do nothing */
3715                 } else if (len < XDR_MAX_NETOBJ) {
3716                         if (nfs_map_group_to_gid(server, (char *)p, len, gid) == 0)
3717                                 ret = NFS_ATTR_FATTR_GROUP;
3718                         else
3719                                 dprintk("%s: nfs_map_group_to_gid failed!\n",
3720                                                 __func__);
3721                 } else
3722                         dprintk("%s: name too long (%u)!\n",
3723                                         __func__, len);
3724                 bitmap[1] &= ~FATTR4_WORD1_OWNER_GROUP;
3725         }
3726         dprintk("%s: gid=%d\n", __func__, (int)*gid);
3727         return ret;
3728 out_overflow:
3729         print_overflow_msg(__func__, xdr);
3730         return -EIO;
3731 }
3732
3733 static int decode_attr_rdev(struct xdr_stream *xdr, uint32_t *bitmap, dev_t *rdev)
3734 {
3735         uint32_t major = 0, minor = 0;
3736         __be32 *p;
3737         int ret = 0;
3738
3739         *rdev = MKDEV(0,0);
3740         if (unlikely(bitmap[1] & (FATTR4_WORD1_RAWDEV - 1U)))
3741                 return -EIO;
3742         if (likely(bitmap[1] & FATTR4_WORD1_RAWDEV)) {
3743                 dev_t tmp;
3744
3745                 p = xdr_inline_decode(xdr, 8);
3746                 if (unlikely(!p))
3747                         goto out_overflow;
3748                 major = be32_to_cpup(p++);
3749                 minor = be32_to_cpup(p);
3750                 tmp = MKDEV(major, minor);
3751                 if (MAJOR(tmp) == major && MINOR(tmp) == minor)
3752                         *rdev = tmp;
3753                 bitmap[1] &= ~ FATTR4_WORD1_RAWDEV;
3754                 ret = NFS_ATTR_FATTR_RDEV;
3755         }
3756         dprintk("%s: rdev=(0x%x:0x%x)\n", __func__, major, minor);
3757         return ret;
3758 out_overflow:
3759         print_overflow_msg(__func__, xdr);
3760         return -EIO;
3761 }
3762
3763 static int decode_attr_space_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
3764 {
3765         __be32 *p;
3766         int status = 0;
3767
3768         *res = 0;
3769         if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_AVAIL - 1U)))
3770                 return -EIO;
3771         if (likely(bitmap[1] & FATTR4_WORD1_SPACE_AVAIL)) {
3772                 p = xdr_inline_decode(xdr, 8);
3773                 if (unlikely(!p))
3774                         goto out_overflow;
3775                 xdr_decode_hyper(p, res);
3776                 bitmap[1] &= ~FATTR4_WORD1_SPACE_AVAIL;
3777         }
3778         dprintk("%s: space avail=%Lu\n", __func__, (unsigned long long)*res);
3779         return status;
3780 out_overflow:
3781         print_overflow_msg(__func__, xdr);
3782         return -EIO;
3783 }
3784
3785 static int decode_attr_space_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
3786 {
3787         __be32 *p;
3788         int status = 0;
3789
3790         *res = 0;
3791         if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_FREE - 1U)))
3792                 return -EIO;
3793         if (likely(bitmap[1] & FATTR4_WORD1_SPACE_FREE)) {
3794                 p = xdr_inline_decode(xdr, 8);
3795                 if (unlikely(!p))
3796                         goto out_overflow;
3797                 xdr_decode_hyper(p, res);
3798                 bitmap[1] &= ~FATTR4_WORD1_SPACE_FREE;
3799         }
3800         dprintk("%s: space free=%Lu\n", __func__, (unsigned long long)*res);
3801         return status;
3802 out_overflow:
3803         print_overflow_msg(__func__, xdr);
3804         return -EIO;
3805 }
3806
3807 static int decode_attr_space_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
3808 {
3809         __be32 *p;
3810         int status = 0;
3811
3812         *res = 0;
3813         if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_TOTAL - 1U)))
3814                 return -EIO;
3815         if (likely(bitmap[1] & FATTR4_WORD1_SPACE_TOTAL)) {
3816                 p = xdr_inline_decode(xdr, 8);
3817                 if (unlikely(!p))
3818                         goto out_overflow;
3819                 xdr_decode_hyper(p, res);
3820                 bitmap[1] &= ~FATTR4_WORD1_SPACE_TOTAL;
3821         }
3822         dprintk("%s: space total=%Lu\n", __func__, (unsigned long long)*res);
3823         return status;
3824 out_overflow:
3825         print_overflow_msg(__func__, xdr);
3826         return -EIO;
3827 }
3828
3829 static int decode_attr_space_used(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *used)
3830 {
3831         __be32 *p;
3832         int ret = 0;
3833
3834         *used = 0;
3835         if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_USED - 1U)))
3836                 return -EIO;
3837         if (likely(bitmap[1] & FATTR4_WORD1_SPACE_USED)) {
3838                 p = xdr_inline_decode(xdr, 8);
3839                 if (unlikely(!p))
3840                         goto out_overflow;
3841                 xdr_decode_hyper(p, used);
3842                 bitmap[1] &= ~FATTR4_WORD1_SPACE_USED;
3843                 ret = NFS_ATTR_FATTR_SPACE_USED;
3844         }
3845         dprintk("%s: space used=%Lu\n", __func__,
3846                         (unsigned long long)*used);
3847         return ret;
3848 out_overflow:
3849         print_overflow_msg(__func__, xdr);
3850         return -EIO;
3851 }
3852
3853 static int decode_attr_time(struct xdr_stream *xdr, struct timespec *time)
3854 {
3855         __be32 *p;
3856         uint64_t sec;
3857         uint32_t nsec;
3858
3859         p = xdr_inline_decode(xdr, 12);
3860         if (unlikely(!p))
3861                 goto out_overflow;
3862         p = xdr_decode_hyper(p, &sec);
3863         nsec = be32_to_cpup(p);
3864         time->tv_sec = (time_t)sec;
3865         time->tv_nsec = (long)nsec;
3866         return 0;
3867 out_overflow:
3868         print_overflow_msg(__func__, xdr);
3869         return -EIO;
3870 }
3871
3872 static int decode_attr_time_access(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
3873 {
3874         int status = 0;
3875
3876         time->tv_sec = 0;
3877         time->tv_nsec = 0;
3878         if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_ACCESS - 1U)))
3879                 return -EIO;
3880         if (likely(bitmap[1] & FATTR4_WORD1_TIME_ACCESS)) {
3881                 status = decode_attr_time(xdr, time);
3882                 if (status == 0)
3883                         status = NFS_ATTR_FATTR_ATIME;
3884                 bitmap[1] &= ~FATTR4_WORD1_TIME_ACCESS;
3885         }
3886         dprintk("%s: atime=%ld\n", __func__, (long)time->tv_sec);
3887         return status;
3888 }
3889
3890 static int decode_attr_time_metadata(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
3891 {
3892         int status = 0;
3893
3894         time->tv_sec = 0;
3895         time->tv_nsec = 0;
3896         if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_METADATA - 1U)))
3897                 return -EIO;
3898         if (likely(bitmap[1] & FATTR4_WORD1_TIME_METADATA)) {
3899                 status = decode_attr_time(xdr, time);
3900                 if (status == 0)
3901                         status = NFS_ATTR_FATTR_CTIME;
3902                 bitmap[1] &= ~FATTR4_WORD1_TIME_METADATA;
3903         }
3904         dprintk("%s: ctime=%ld\n", __func__, (long)time->tv_sec);
3905         return status;
3906 }
3907
3908 static int decode_attr_time_delta(struct xdr_stream *xdr, uint32_t *bitmap,
3909                                   struct timespec *time)
3910 {
3911         int status = 0;
3912
3913         time->tv_sec = 0;
3914         time->tv_nsec = 0;
3915         if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_DELTA - 1U)))
3916                 return -EIO;
3917         if (likely(bitmap[1] & FATTR4_WORD1_TIME_DELTA)) {
3918                 status = decode_attr_time(xdr, time);
3919                 bitmap[1] &= ~FATTR4_WORD1_TIME_DELTA;
3920         }
3921         dprintk("%s: time_delta=%ld %ld\n", __func__, (long)time->tv_sec,
3922                 (long)time->tv_nsec);
3923         return status;
3924 }
3925
3926 static int decode_attr_time_modify(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
3927 {
3928         int status = 0;
3929
3930         time->tv_sec = 0;
3931         time->tv_nsec = 0;
3932         if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_MODIFY - 1U)))
3933                 return -EIO;
3934         if (likely(bitmap[1] & FATTR4_WORD1_TIME_MODIFY)) {
3935                 status = decode_attr_time(xdr, time);
3936                 if (status == 0)
3937                         status = NFS_ATTR_FATTR_MTIME;
3938                 bitmap[1] &= ~FATTR4_WORD1_TIME_MODIFY;
3939         }
3940         dprintk("%s: mtime=%ld\n", __func__, (long)time->tv_sec);
3941         return status;
3942 }
3943
3944 static int verify_attr_len(struct xdr_stream *xdr, __be32 *savep, uint32_t attrlen)
3945 {
3946         unsigned int attrwords = XDR_QUADLEN(attrlen);
3947         unsigned int nwords = xdr->p - savep;
3948
3949         if (unlikely(attrwords != nwords)) {
3950                 dprintk("%s: server returned incorrect attribute length: "
3951                         "%u %c %u\n",
3952                                 __func__,
3953                                 attrwords << 2,
3954                                 (attrwords < nwords) ? '<' : '>',
3955                                 nwords << 2);
3956                 return -EIO;
3957         }
3958         return 0;
3959 }
3960
3961 static int decode_change_info(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
3962 {
3963         __be32 *p;
3964
3965         p = xdr_inline_decode(xdr, 20);
3966         if (unlikely(!p))
3967                 goto out_overflow;
3968         cinfo->atomic = be32_to_cpup(p++);
3969         p = xdr_decode_hyper(p, &cinfo->before);
3970         xdr_decode_hyper(p, &cinfo->after);
3971         return 0;
3972 out_overflow:
3973         print_overflow_msg(__func__, xdr);
3974         return -EIO;
3975 }
3976
3977 static int decode_access(struct xdr_stream *xdr, struct nfs4_accessres *access)
3978 {
3979         __be32 *p;
3980         uint32_t supp, acc;
3981         int status;
3982
3983         status = decode_op_hdr(xdr, OP_ACCESS);
3984         if (status)
3985                 return status;
3986         p = xdr_inline_decode(xdr, 8);
3987         if (unlikely(!p))
3988                 goto out_overflow;
3989         supp = be32_to_cpup(p++);
3990         acc = be32_to_cpup(p);
3991         access->supported = supp;
3992         access->access = acc;
3993         return 0;
3994 out_overflow:
3995         print_overflow_msg(__func__, xdr);
3996         return -EIO;
3997 }
3998
3999 static int decode_opaque_fixed(struct xdr_stream *xdr, void *buf, size_t len)
4000 {
4001         __be32 *p;
4002
4003         p = xdr_inline_decode(xdr, len);
4004         if (likely(p)) {
4005                 memcpy(buf, p, len);
4006                 return 0;
4007         }
4008         print_overflow_msg(__func__, xdr);
4009         return -EIO;
4010 }
4011
4012 static int decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
4013 {
4014         return decode_opaque_fixed(xdr, stateid->data, NFS4_STATEID_SIZE);
4015 }
4016
4017 static int decode_close(struct xdr_stream *xdr, struct nfs_closeres *res)
4018 {
4019         int status;
4020
4021         status = decode_op_hdr(xdr, OP_CLOSE);
4022         if (status != -EIO)
4023                 nfs_increment_open_seqid(status, res->seqid);
4024         if (!status)
4025                 status = decode_stateid(xdr, &res->stateid);
4026         return status;
4027 }
4028
4029 static int decode_verifier(struct xdr_stream *xdr, void *verifier)
4030 {
4031         return decode_opaque_fixed(xdr, verifier, 8);
4032 }
4033
4034 static int decode_commit(struct xdr_stream *xdr, struct nfs_writeres *res)
4035 {
4036         int status;
4037
4038         status = decode_op_hdr(xdr, OP_COMMIT);
4039         if (!status)
4040                 status = decode_verifier(xdr, res->verf->verifier);
4041         return status;
4042 }
4043
4044 static int decode_create(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
4045 {
4046         __be32 *p;
4047         uint32_t bmlen;
4048         int status;
4049
4050         status = decode_op_hdr(xdr, OP_CREATE);
4051         if (status)
4052                 return status;
4053         if ((status = decode_change_info(xdr, cinfo)))
4054                 return status;
4055         p = xdr_inline_decode(xdr, 4);
4056         if (unlikely(!p))
4057                 goto out_overflow;
4058         bmlen = be32_to_cpup(p);
4059         p = xdr_inline_decode(xdr, bmlen << 2);
4060         if (likely(p))
4061                 return 0;
4062 out_overflow:
4063         print_overflow_msg(__func__, xdr);
4064         return -EIO;
4065 }
4066
4067 static int decode_server_caps(struct xdr_stream *xdr, struct nfs4_server_caps_res *res)
4068 {
4069         __be32 *savep;
4070         uint32_t attrlen, bitmap[2] = {0};
4071         int status;
4072
4073         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
4074                 goto xdr_error;
4075         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
4076                 goto xdr_error;
4077         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
4078                 goto xdr_error;
4079         if ((status = decode_attr_supported(xdr, bitmap, res->attr_bitmask)) != 0)
4080                 goto xdr_error;
4081         if ((status = decode_attr_link_support(xdr, bitmap, &res->has_links)) != 0)
4082                 goto xdr_error;
4083         if ((status = decode_attr_symlink_support(xdr, bitmap, &res->has_symlinks)) != 0)
4084                 goto xdr_error;
4085         if ((status = decode_attr_aclsupport(xdr, bitmap, &res->acl_bitmask)) != 0)
4086                 goto xdr_error;
4087         status = verify_attr_len(xdr, savep, attrlen);
4088 xdr_error:
4089         dprintk("%s: xdr returned %d!\n", __func__, -status);
4090         return status;
4091 }
4092
4093 static int decode_statfs(struct xdr_stream *xdr, struct nfs_fsstat *fsstat)
4094 {
4095         __be32 *savep;
4096         uint32_t attrlen, bitmap[2] = {0};
4097         int status;
4098
4099         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
4100                 goto xdr_error;
4101         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
4102                 goto xdr_error;
4103         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
4104                 goto xdr_error;
4105
4106         if ((status = decode_attr_files_avail(xdr, bitmap, &fsstat->afiles)) != 0)
4107                 goto xdr_error;
4108         if ((status = decode_attr_files_free(xdr, bitmap, &fsstat->ffiles)) != 0)
4109                 goto xdr_error;
4110         if ((status = decode_attr_files_total(xdr, bitmap, &fsstat->tfiles)) != 0)
4111                 goto xdr_error;
4112         if ((status = decode_attr_space_avail(xdr, bitmap, &fsstat->abytes)) != 0)
4113                 goto xdr_error;
4114         if ((status = decode_attr_space_free(xdr, bitmap, &fsstat->fbytes)) != 0)
4115                 goto xdr_error;
4116         if ((status = decode_attr_space_total(xdr, bitmap, &fsstat->tbytes)) != 0)
4117                 goto xdr_error;
4118
4119         status = verify_attr_len(xdr, savep, attrlen);
4120 xdr_error:
4121         dprintk("%s: xdr returned %d!\n", __func__, -status);
4122         return status;
4123 }
4124
4125 static int decode_pathconf(struct xdr_stream *xdr, struct nfs_pathconf *pathconf)
4126 {
4127         __be32 *savep;
4128         uint32_t attrlen, bitmap[2] = {0};
4129         int status;
4130
4131         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
4132                 goto xdr_error;
4133         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
4134                 goto xdr_error;
4135         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
4136                 goto xdr_error;
4137
4138         if ((status = decode_attr_maxlink(xdr, bitmap, &pathconf->max_link)) != 0)
4139                 goto xdr_error;
4140         if ((status = decode_attr_maxname(xdr, bitmap, &pathconf->max_namelen)) != 0)
4141                 goto xdr_error;
4142
4143         status = verify_attr_len(xdr, savep, attrlen);
4144 xdr_error:
4145         dprintk("%s: xdr returned %d!\n", __func__, -status);
4146         return status;
4147 }
4148
4149 static int decode_getfattr_attrs(struct xdr_stream *xdr, uint32_t *bitmap,
4150                 struct nfs_fattr *fattr, struct nfs_fh *fh,
4151                 const struct nfs_server *server, int may_sleep)
4152 {
4153         int status;
4154         umode_t fmode = 0;
4155         uint32_t type;
4156         int32_t err;
4157
4158         status = decode_attr_type(xdr, bitmap, &type);
4159         if (status < 0)
4160                 goto xdr_error;
4161         fattr->mode = 0;
4162         if (status != 0) {
4163                 fattr->mode |= nfs_type2fmt[type];
4164                 fattr->valid |= status;
4165         }
4166
4167         status = decode_attr_change(xdr, bitmap, &fattr->change_attr);
4168         if (status < 0)
4169                 goto xdr_error;
4170         fattr->valid |= status;
4171
4172         status = decode_attr_size(xdr, bitmap, &fattr->size);
4173         if (status < 0)
4174                 goto xdr_error;
4175         fattr->valid |= status;
4176
4177         status = decode_attr_fsid(xdr, bitmap, &fattr->fsid);
4178         if (status < 0)
4179                 goto xdr_error;
4180         fattr->valid |= status;
4181
4182         err = 0;
4183         status = decode_attr_error(xdr, bitmap, &err);
4184         if (status < 0)
4185                 goto xdr_error;
4186         if (err == -NFS4ERR_WRONGSEC)
4187                 nfs_fixup_secinfo_attributes(fattr, fh);
4188
4189         status = decode_attr_filehandle(xdr, bitmap, fh);
4190         if (status < 0)
4191                 goto xdr_error;
4192
4193         status = decode_attr_fileid(xdr, bitmap, &fattr->fileid);
4194         if (status < 0)
4195                 goto xdr_error;
4196         fattr->valid |= status;
4197
4198         status = decode_attr_fs_locations(xdr, bitmap, container_of(fattr,
4199                                                 struct nfs4_fs_locations,
4200                                                 fattr));
4201         if (status < 0)
4202                 goto xdr_error;
4203         fattr->valid |= status;
4204
4205         status = decode_attr_mode(xdr, bitmap, &fmode);
4206         if (status < 0)
4207                 goto xdr_error;
4208         if (status != 0) {
4209                 fattr->mode |= fmode;
4210                 fattr->valid |= status;
4211         }
4212
4213         status = decode_attr_nlink(xdr, bitmap, &fattr->nlink);
4214         if (status < 0)
4215                 goto xdr_error;
4216         fattr->valid |= status;
4217
4218         status = decode_attr_owner(xdr, bitmap, server, &fattr->uid, may_sleep);
4219         if (status < 0)
4220                 goto xdr_error;
4221         fattr->valid |= status;
4222
4223         status = decode_attr_group(xdr, bitmap, server, &fattr->gid, may_sleep);
4224         if (status < 0)
4225                 goto xdr_error;
4226         fattr->valid |= status;
4227
4228         status = decode_attr_rdev(xdr, bitmap, &fattr->rdev);
4229         if (status < 0)
4230                 goto xdr_error;
4231         fattr->valid |= status;
4232
4233         status = decode_attr_space_used(xdr, bitmap, &fattr->du.nfs3.used);
4234         if (status < 0)
4235                 goto xdr_error;
4236         fattr->valid |= status;
4237
4238         status = decode_attr_time_access(xdr, bitmap, &fattr->atime);
4239         if (status < 0)
4240                 goto xdr_error;
4241         fattr->valid |= status;
4242
4243         status = decode_attr_time_metadata(xdr, bitmap, &fattr->ctime);
4244         if (status < 0)
4245                 goto xdr_error;
4246         fattr->valid |= status;
4247
4248         status = decode_attr_time_modify(xdr, bitmap, &fattr->mtime);
4249         if (status < 0)
4250                 goto xdr_error;
4251         fattr->valid |= status;
4252
4253         status = decode_attr_mounted_on_fileid(xdr, bitmap, &fattr->mounted_on_fileid);
4254         if (status < 0)
4255                 goto xdr_error;
4256         fattr->valid |= status;
4257
4258 xdr_error:
4259         dprintk("%s: xdr returned %d\n", __func__, -status);
4260         return status;
4261 }
4262
4263 static int decode_getfattr_generic(struct xdr_stream *xdr, struct nfs_fattr *fattr,
4264                 struct nfs_fh *fh, const struct nfs_server *server, int may_sleep)
4265 {
4266         __be32 *savep;
4267         uint32_t attrlen,
4268                  bitmap[2] = {0};
4269         int status;
4270
4271         status = decode_op_hdr(xdr, OP_GETATTR);
4272         if (status < 0)
4273                 goto xdr_error;
4274
4275         status = decode_attr_bitmap(xdr, bitmap);
4276         if (status < 0)
4277                 goto xdr_error;
4278
4279         status = decode_attr_length(xdr, &attrlen, &savep);
4280         if (status < 0)
4281                 goto xdr_error;
4282
4283         status = decode_getfattr_attrs(xdr, bitmap, fattr, fh, server, may_sleep);
4284         if (status < 0)
4285                 goto xdr_error;
4286
4287         status = verify_attr_len(xdr, savep, attrlen);
4288 xdr_error:
4289         dprintk("%s: xdr returned %d\n", __func__, -status);
4290         return status;
4291 }
4292
4293 static int decode_getfattr(struct xdr_stream *xdr, struct nfs_fattr *fattr,
4294                 const struct nfs_server *server, int may_sleep)
4295 {
4296         return decode_getfattr_generic(xdr, fattr, NULL, server, may_sleep);
4297 }
4298
4299 /*
4300  * Decode potentially multiple layout types. Currently we only support
4301  * one layout driver per file system.
4302  */
4303 static int decode_first_pnfs_layout_type(struct xdr_stream *xdr,
4304                                          uint32_t *layouttype)
4305 {
4306         uint32_t *p;
4307         int num;
4308
4309         p = xdr_inline_decode(xdr, 4);
4310         if (unlikely(!p))
4311                 goto out_overflow;
4312         num = be32_to_cpup(p);
4313
4314         /* pNFS is not supported by the underlying file system */
4315         if (num == 0) {
4316                 *layouttype = 0;
4317                 return 0;
4318         }
4319         if (num > 1)
4320                 printk(KERN_INFO "%s: Warning: Multiple pNFS layout drivers "
4321                         "per filesystem not supported\n", __func__);
4322
4323         /* Decode and set first layout type, move xdr->p past unused types */
4324         p = xdr_inline_decode(xdr, num * 4);
4325         if (unlikely(!p))
4326                 goto out_overflow;
4327         *layouttype = be32_to_cpup(p);
4328         return 0;
4329 out_overflow:
4330         print_overflow_msg(__func__, xdr);
4331         return -EIO;
4332 }
4333
4334 /*
4335  * The type of file system exported.
4336  * Note we must ensure that layouttype is set in any non-error case.
4337  */
4338 static int decode_attr_pnfstype(struct xdr_stream *xdr, uint32_t *bitmap,
4339                                 uint32_t *layouttype)
4340 {
4341         int status = 0;
4342
4343         dprintk("%s: bitmap is %x\n", __func__, bitmap[1]);
4344         if (unlikely(bitmap[1] & (FATTR4_WORD1_FS_LAYOUT_TYPES - 1U)))
4345                 return -EIO;
4346         if (bitmap[1] & FATTR4_WORD1_FS_LAYOUT_TYPES) {
4347                 status = decode_first_pnfs_layout_type(xdr, layouttype);
4348                 bitmap[1] &= ~FATTR4_WORD1_FS_LAYOUT_TYPES;
4349         } else
4350                 *layouttype = 0;
4351         return status;
4352 }
4353
4354 static int decode_fsinfo(struct xdr_stream *xdr, struct nfs_fsinfo *fsinfo)
4355 {
4356         __be32 *savep;
4357         uint32_t attrlen, bitmap[2];
4358         int status;
4359
4360         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
4361                 goto xdr_error;
4362         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
4363                 goto xdr_error;
4364         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
4365                 goto xdr_error;
4366
4367         fsinfo->rtmult = fsinfo->wtmult = 512;  /* ??? */
4368
4369         if ((status = decode_attr_lease_time(xdr, bitmap, &fsinfo->lease_time)) != 0)
4370                 goto xdr_error;
4371         if ((status = decode_attr_maxfilesize(xdr, bitmap, &fsinfo->maxfilesize)) != 0)
4372                 goto xdr_error;
4373         if ((status = decode_attr_maxread(xdr, bitmap, &fsinfo->rtmax)) != 0)
4374                 goto xdr_error;
4375         fsinfo->rtpref = fsinfo->dtpref = fsinfo->rtmax;
4376         if ((status = decode_attr_maxwrite(xdr, bitmap, &fsinfo->wtmax)) != 0)
4377                 goto xdr_error;
4378         fsinfo->wtpref = fsinfo->wtmax;
4379         status = decode_attr_time_delta(xdr, bitmap, &fsinfo->time_delta);
4380         if (status != 0)
4381                 goto xdr_error;
4382         status = decode_attr_pnfstype(xdr, bitmap, &fsinfo->layouttype);
4383         if (status != 0)
4384                 goto xdr_error;
4385
4386         status = verify_attr_len(xdr, savep, attrlen);
4387 xdr_error:
4388         dprintk("%s: xdr returned %d!\n", __func__, -status);
4389         return status;
4390 }
4391
4392 static int decode_getfh(struct xdr_stream *xdr, struct nfs_fh *fh)
4393 {
4394         __be32 *p;
4395         uint32_t len;
4396         int status;
4397
4398         /* Zero handle first to allow comparisons */
4399         memset(fh, 0, sizeof(*fh));
4400
4401         status = decode_op_hdr(xdr, OP_GETFH);
4402         if (status)
4403                 return status;
4404
4405         p = xdr_inline_decode(xdr, 4);
4406         if (unlikely(!p))
4407                 goto out_overflow;
4408         len = be32_to_cpup(p);
4409         if (len > NFS4_FHSIZE)
4410                 return -EIO;
4411         fh->size = len;
4412         p = xdr_inline_decode(xdr, len);
4413         if (unlikely(!p))
4414                 goto out_overflow;
4415         memcpy(fh->data, p, len);
4416         return 0;
4417 out_overflow:
4418         print_overflow_msg(__func__, xdr);
4419         return -EIO;
4420 }
4421
4422 static int decode_link(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
4423 {
4424         int status;
4425
4426         status = decode_op_hdr(xdr, OP_LINK);
4427         if (status)
4428                 return status;
4429         return decode_change_info(xdr, cinfo);
4430 }
4431
4432 /*
4433  * We create the owner, so we know a proper owner.id length is 4.
4434  */
4435 static int decode_lock_denied (struct xdr_stream *xdr, struct file_lock *fl)
4436 {
4437         uint64_t offset, length, clientid;
4438         __be32 *p;
4439         uint32_t namelen, type;
4440
4441         p = xdr_inline_decode(xdr, 32); /* read 32 bytes */
4442         if (unlikely(!p))
4443                 goto out_overflow;
4444         p = xdr_decode_hyper(p, &offset); /* read 2 8-byte long words */
4445         p = xdr_decode_hyper(p, &length);
4446         type = be32_to_cpup(p++); /* 4 byte read */
4447         if (fl != NULL) { /* manipulate file lock */
4448                 fl->fl_start = (loff_t)offset;
4449                 fl->fl_end = fl->fl_start + (loff_t)length - 1;
4450                 if (length == ~(uint64_t)0)
4451                         fl->fl_end = OFFSET_MAX;
4452                 fl->fl_type = F_WRLCK;
4453                 if (type & 1)
4454                         fl->fl_type = F_RDLCK;
4455                 fl->fl_pid = 0;
4456         }
4457         p = xdr_decode_hyper(p, &clientid); /* read 8 bytes */
4458         namelen = be32_to_cpup(p); /* read 4 bytes */  /* have read all 32 bytes now */
4459         p = xdr_inline_decode(xdr, namelen); /* variable size field */
4460         if (likely(p))
4461                 return -NFS4ERR_DENIED;
4462 out_overflow:
4463         print_overflow_msg(__func__, xdr);
4464         return -EIO;
4465 }
4466
4467 static int decode_lock(struct xdr_stream *xdr, struct nfs_lock_res *res)
4468 {
4469         int status;
4470
4471         status = decode_op_hdr(xdr, OP_LOCK);
4472         if (status == -EIO)
4473                 goto out;
4474         if (status == 0) {
4475                 status = decode_stateid(xdr, &res->stateid);
4476                 if (unlikely(status))
4477                         goto out;
4478         } else if (status == -NFS4ERR_DENIED)
4479                 status = decode_lock_denied(xdr, NULL);
4480         if (res->open_seqid != NULL)
4481                 nfs_increment_open_seqid(status, res->open_seqid);
4482         nfs_increment_lock_seqid(status, res->lock_seqid);
4483 out:
4484         return status;
4485 }
4486
4487 static int decode_lockt(struct xdr_stream *xdr, struct nfs_lockt_res *res)
4488 {
4489         int status;
4490         status = decode_op_hdr(xdr, OP_LOCKT);
4491         if (status == -NFS4ERR_DENIED)
4492                 return decode_lock_denied(xdr, res->denied);
4493         return status;
4494 }
4495
4496 static int decode_locku(struct xdr_stream *xdr, struct nfs_locku_res *res)
4497 {
4498         int status;
4499
4500         status = decode_op_hdr(xdr, OP_LOCKU);
4501         if (status != -EIO)
4502                 nfs_increment_lock_seqid(status, res->seqid);
4503         if (status == 0)
4504                 status = decode_stateid(xdr, &res->stateid);
4505         return status;
4506 }
4507
4508 static int decode_release_lockowner(struct xdr_stream *xdr)
4509 {
4510         return decode_op_hdr(xdr, OP_RELEASE_LOCKOWNER);
4511 }
4512
4513 static int decode_lookup(struct xdr_stream *xdr)
4514 {
4515         return decode_op_hdr(xdr, OP_LOOKUP);
4516 }
4517
4518 /* This is too sick! */
4519 static int decode_space_limit(struct xdr_stream *xdr, u64 *maxsize)
4520 {
4521         __be32 *p;
4522         uint32_t limit_type, nblocks, blocksize;
4523
4524         p = xdr_inline_decode(xdr, 12);
4525         if (unlikely(!p))
4526                 goto out_overflow;
4527         limit_type = be32_to_cpup(p++);
4528         switch (limit_type) {
4529         case 1:
4530                 xdr_decode_hyper(p, maxsize);
4531                 break;
4532         case 2:
4533                 nblocks = be32_to_cpup(p++);
4534                 blocksize = be32_to_cpup(p);
4535                 *maxsize = (uint64_t)nblocks * (uint64_t)blocksize;
4536         }
4537         return 0;
4538 out_overflow:
4539         print_overflow_msg(__func__, xdr);
4540         return -EIO;
4541 }
4542
4543 static int decode_delegation(struct xdr_stream *xdr, struct nfs_openres *res)
4544 {
4545         __be32 *p;
4546         uint32_t delegation_type;
4547         int status;
4548
4549         p = xdr_inline_decode(xdr, 4);
4550         if (unlikely(!p))
4551                 goto out_overflow;
4552         delegation_type = be32_to_cpup(p);
4553         if (delegation_type == NFS4_OPEN_DELEGATE_NONE) {
4554                 res->delegation_type = 0;
4555                 return 0;
4556         }
4557         status = decode_stateid(xdr, &res->delegation);
4558         if (unlikely(status))
4559                 return status;
4560         p = xdr_inline_decode(xdr, 4);
4561         if (unlikely(!p))
4562                 goto out_overflow;
4563         res->do_recall = be32_to_cpup(p);
4564
4565         switch (delegation_type) {
4566         case NFS4_OPEN_DELEGATE_READ:
4567                 res->delegation_type = FMODE_READ;
4568                 break;
4569         case NFS4_OPEN_DELEGATE_WRITE:
4570                 res->delegation_type = FMODE_WRITE|FMODE_READ;
4571                 if (decode_space_limit(xdr, &res->maxsize) < 0)
4572                                 return -EIO;
4573         }
4574         return decode_ace(xdr, NULL, res->server->nfs_client);
4575 out_overflow:
4576         print_overflow_msg(__func__, xdr);
4577         return -EIO;
4578 }
4579
4580 static int decode_open(struct xdr_stream *xdr, struct nfs_openres *res)
4581 {
4582         __be32 *p;
4583         uint32_t savewords, bmlen, i;
4584         int status;
4585
4586         status = decode_op_hdr(xdr, OP_OPEN);
4587         if (status != -EIO)
4588                 nfs_increment_open_seqid(status, res->seqid);
4589         if (!status)
4590                 status = decode_stateid(xdr, &res->stateid);
4591         if (unlikely(status))
4592                 return status;
4593
4594         decode_change_info(xdr, &res->cinfo);
4595
4596         p = xdr_inline_decode(xdr, 8);
4597         if (unlikely(!p))
4598                 goto out_overflow;
4599         res->rflags = be32_to_cpup(p++);
4600         bmlen = be32_to_cpup(p);
4601         if (bmlen > 10)
4602                 goto xdr_error;
4603
4604         p = xdr_inline_decode(xdr, bmlen << 2);
4605         if (unlikely(!p))
4606                 goto out_overflow;
4607         savewords = min_t(uint32_t, bmlen, NFS4_BITMAP_SIZE);
4608         for (i = 0; i < savewords; ++i)
4609                 res->attrset[i] = be32_to_cpup(p++);
4610         for (; i < NFS4_BITMAP_SIZE; i++)
4611                 res->attrset[i] = 0;
4612
4613         return decode_delegation(xdr, res);
4614 xdr_error:
4615         dprintk("%s: Bitmap too large! Length = %u\n", __func__, bmlen);
4616         return -EIO;
4617 out_overflow:
4618         print_overflow_msg(__func__, xdr);
4619         return -EIO;
4620 }
4621
4622 static int decode_open_confirm(struct xdr_stream *xdr, struct nfs_open_confirmres *res)
4623 {
4624         int status;
4625
4626         status = decode_op_hdr(xdr, OP_OPEN_CONFIRM);
4627         if (status != -EIO)
4628                 nfs_increment_open_seqid(status, res->seqid);
4629         if (!status)
4630                 status = decode_stateid(xdr, &res->stateid);
4631         return status;
4632 }
4633
4634 static int decode_open_downgrade(struct xdr_stream *xdr, struct nfs_closeres *res)
4635 {
4636         int status;
4637
4638         status = decode_op_hdr(xdr, OP_OPEN_DOWNGRADE);
4639         if (status != -EIO)
4640                 nfs_increment_open_seqid(status, res->seqid);
4641         if (!status)
4642                 status = decode_stateid(xdr, &res->stateid);
4643         return status;
4644 }
4645
4646 static int decode_putfh(struct xdr_stream *xdr)
4647 {
4648         return decode_op_hdr(xdr, OP_PUTFH);
4649 }
4650
4651 static int decode_putrootfh(struct xdr_stream *xdr)
4652 {
4653         return decode_op_hdr(xdr, OP_PUTROOTFH);
4654 }
4655
4656 static int decode_read(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs_readres *res)
4657 {
4658         struct kvec *iov = req->rq_rcv_buf.head;
4659         __be32 *p;
4660         uint32_t count, eof, recvd, hdrlen;
4661         int status;
4662
4663         status = decode_op_hdr(xdr, OP_READ);
4664         if (status)
4665                 return status;
4666         p = xdr_inline_decode(xdr, 8);
4667         if (unlikely(!p))
4668                 goto out_overflow;
4669         eof = be32_to_cpup(p++);
4670         count = be32_to_cpup(p);
4671         hdrlen = (u8 *) xdr->p - (u8 *) iov->iov_base;
4672         recvd = req->rq_rcv_buf.len - hdrlen;
4673         if (count > recvd) {
4674                 dprintk("NFS: server cheating in read reply: "
4675                                 "count %u > recvd %u\n", count, recvd);
4676                 count = recvd;
4677                 eof = 0;
4678         }
4679         xdr_read_pages(xdr, count);
4680         res->eof = eof;
4681         res->count = count;
4682         return 0;
4683 out_overflow:
4684         print_overflow_msg(__func__, xdr);
4685         return -EIO;
4686 }
4687
4688 static int decode_readdir(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs4_readdir_res *readdir)
4689 {
4690         struct xdr_buf  *rcvbuf = &req->rq_rcv_buf;
4691         struct kvec     *iov = rcvbuf->head;
4692         size_t          hdrlen;
4693         u32             recvd, pglen = rcvbuf->page_len;
4694         int             status;
4695
4696         status = decode_op_hdr(xdr, OP_READDIR);
4697         if (!status)
4698                 status = decode_verifier(xdr, readdir->verifier.data);
4699         if (unlikely(status))
4700                 return status;
4701         dprintk("%s: verifier = %08x:%08x\n",
4702                         __func__,
4703                         ((u32 *)readdir->verifier.data)[0],
4704                         ((u32 *)readdir->verifier.data)[1]);
4705
4706
4707         hdrlen = (char *) xdr->p - (char *) iov->iov_base;
4708         recvd = rcvbuf->len - hdrlen;
4709         if (pglen > recvd)
4710                 pglen = recvd;
4711         xdr_read_pages(xdr, pglen);
4712
4713
4714         return pglen;
4715 }
4716
4717 static int decode_readlink(struct xdr_stream *xdr, struct rpc_rqst *req)
4718 {
4719         struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
4720         struct kvec *iov = rcvbuf->head;
4721         size_t hdrlen;
4722         u32 len, recvd;
4723         __be32 *p;
4724         int status;
4725
4726         status = decode_op_hdr(xdr, OP_READLINK);
4727         if (status)
4728                 return status;
4729
4730         /* Convert length of symlink */
4731         p = xdr_inline_decode(xdr, 4);
4732         if (unlikely(!p))
4733                 goto out_overflow;
4734         len = be32_to_cpup(p);
4735         if (len >= rcvbuf->page_len || len <= 0) {
4736                 dprintk("nfs: server returned giant symlink!\n");
4737                 return -ENAMETOOLONG;
4738         }
4739         hdrlen = (char *) xdr->p - (char *) iov->iov_base;
4740         recvd = req->rq_rcv_buf.len - hdrlen;
4741         if (recvd < len) {
4742                 dprintk("NFS: server cheating in readlink reply: "
4743                                 "count %u > recvd %u\n", len, recvd);
4744                 return -EIO;
4745         }
4746         xdr_read_pages(xdr, len);
4747         /*
4748          * The XDR encode routine has set things up so that
4749          * the link text will be copied directly into the
4750          * buffer.  We just have to do overflow-checking,
4751          * and and null-terminate the text (the VFS expects
4752          * null-termination).
4753          */
4754         xdr_terminate_string(rcvbuf, len);
4755         return 0;
4756 out_overflow:
4757         print_overflow_msg(__func__, xdr);
4758         return -EIO;
4759 }
4760
4761 static int decode_remove(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
4762 {
4763         int status;
4764
4765         status = decode_op_hdr(xdr, OP_REMOVE);
4766         if (status)
4767                 goto out;
4768         status = decode_change_info(xdr, cinfo);
4769 out:
4770         return status;
4771 }
4772
4773 static int decode_rename(struct xdr_stream *xdr, struct nfs4_change_info *old_cinfo,
4774               struct nfs4_change_info *new_cinfo)
4775 {
4776         int status;
4777
4778         status = decode_op_hdr(xdr, OP_RENAME);
4779         if (status)
4780                 goto out;
4781         if ((status = decode_change_info(xdr, old_cinfo)))
4782                 goto out;
4783         status = decode_change_info(xdr, new_cinfo);
4784 out:
4785         return status;
4786 }
4787
4788 static int decode_renew(struct xdr_stream *xdr)
4789 {
4790         return decode_op_hdr(xdr, OP_RENEW);
4791 }
4792
4793 static int
4794 decode_restorefh(struct xdr_stream *xdr)
4795 {
4796         return decode_op_hdr(xdr, OP_RESTOREFH);
4797 }
4798
4799 static int decode_getacl(struct xdr_stream *xdr, struct rpc_rqst *req,
4800                 size_t *acl_len)
4801 {
4802         __be32 *savep;
4803         uint32_t attrlen,
4804                  bitmap[2] = {0};
4805         struct kvec *iov = req->rq_rcv_buf.head;
4806         int status;
4807
4808         *acl_len = 0;
4809         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
4810                 goto out;
4811         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
4812                 goto out;
4813         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
4814                 goto out;
4815
4816         if (unlikely(bitmap[0] & (FATTR4_WORD0_ACL - 1U)))
4817                 return -EIO;
4818         if (likely(bitmap[0] & FATTR4_WORD0_ACL)) {
4819                 size_t hdrlen;
4820                 u32 recvd;
4821
4822                 /* We ignore &savep and don't do consistency checks on
4823                  * the attr length.  Let userspace figure it out.... */
4824                 hdrlen = (u8 *)xdr->p - (u8 *)iov->iov_base;
4825                 recvd = req->rq_rcv_buf.len - hdrlen;
4826                 if (attrlen > recvd) {
4827                         dprintk("NFS: server cheating in getattr"
4828                                         " acl reply: attrlen %u > recvd %u\n",
4829                                         attrlen, recvd);
4830                         return -EINVAL;
4831                 }
4832                 xdr_read_pages(xdr, attrlen);
4833                 *acl_len = attrlen;
4834         } else
4835                 status = -EOPNOTSUPP;
4836
4837 out:
4838         return status;
4839 }
4840
4841 static int
4842 decode_savefh(struct xdr_stream *xdr)
4843 {
4844         return decode_op_hdr(xdr, OP_SAVEFH);
4845 }
4846
4847 static int decode_setattr(struct xdr_stream *xdr)
4848 {
4849         __be32 *p;
4850         uint32_t bmlen;
4851         int status;
4852
4853         status = decode_op_hdr(xdr, OP_SETATTR);
4854         if (status)
4855                 return status;
4856         p = xdr_inline_decode(xdr, 4);
4857         if (unlikely(!p))
4858                 goto out_overflow;
4859         bmlen = be32_to_cpup(p);
4860         p = xdr_inline_decode(xdr, bmlen << 2);
4861         if (likely(p))
4862                 return 0;
4863 out_overflow:
4864         print_overflow_msg(__func__, xdr);
4865         return -EIO;
4866 }
4867
4868 static int decode_setclientid(struct xdr_stream *xdr, struct nfs4_setclientid_res *res)
4869 {
4870         __be32 *p;
4871         uint32_t opnum;
4872         int32_t nfserr;
4873
4874         p = xdr_inline_decode(xdr, 8);
4875         if (unlikely(!p))
4876                 goto out_overflow;
4877         opnum = be32_to_cpup(p++);
4878         if (opnum != OP_SETCLIENTID) {
4879                 dprintk("nfs: decode_setclientid: Server returned operation"
4880                         " %d\n", opnum);
4881                 return -EIO;
4882         }
4883         nfserr = be32_to_cpup(p);
4884         if (nfserr == NFS_OK) {
4885                 p = xdr_inline_decode(xdr, 8 + NFS4_VERIFIER_SIZE);
4886                 if (unlikely(!p))
4887                         goto out_overflow;
4888                 p = xdr_decode_hyper(p, &res->clientid);
4889                 memcpy(res->confirm.data, p, NFS4_VERIFIER_SIZE);
4890         } else if (nfserr == NFSERR_CLID_INUSE) {
4891                 uint32_t len;
4892
4893                 /* skip netid string */
4894                 p = xdr_inline_decode(xdr, 4);
4895                 if (unlikely(!p))
4896                         goto out_overflow;
4897                 len = be32_to_cpup(p);
4898                 p = xdr_inline_decode(xdr, len);
4899                 if (unlikely(!p))
4900                         goto out_overflow;
4901
4902                 /* skip uaddr string */
4903                 p = xdr_inline_decode(xdr, 4);
4904                 if (unlikely(!p))
4905                         goto out_overflow;
4906                 len = be32_to_cpup(p);
4907                 p = xdr_inline_decode(xdr, len);
4908                 if (unlikely(!p))
4909                         goto out_overflow;
4910                 return -NFSERR_CLID_INUSE;
4911         } else
4912                 return nfs4_stat_to_errno(nfserr);
4913
4914         return 0;
4915 out_overflow:
4916         print_overflow_msg(__func__, xdr);
4917         return -EIO;
4918 }
4919
4920 static int decode_setclientid_confirm(struct xdr_stream *xdr)
4921 {
4922         return decode_op_hdr(xdr, OP_SETCLIENTID_CONFIRM);
4923 }
4924
4925 static int decode_write(struct xdr_stream *xdr, struct nfs_writeres *res)
4926 {
4927         __be32 *p;
4928         int status;
4929
4930         status = decode_op_hdr(xdr, OP_WRITE);
4931         if (status)
4932                 return status;
4933
4934         p = xdr_inline_decode(xdr, 16);
4935         if (unlikely(!p))
4936                 goto out_overflow;
4937         res->count = be32_to_cpup(p++);
4938         res->verf->committed = be32_to_cpup(p++);
4939         memcpy(res->verf->verifier, p, 8);
4940         return 0;
4941 out_overflow:
4942         print_overflow_msg(__func__, xdr);
4943         return -EIO;
4944 }
4945
4946 static int decode_delegreturn(struct xdr_stream *xdr)
4947 {
4948         return decode_op_hdr(xdr, OP_DELEGRETURN);
4949 }
4950
4951 static int decode_secinfo_gss(struct xdr_stream *xdr, struct nfs4_secinfo_flavor *flavor)
4952 {
4953         __be32 *p;
4954
4955         p = xdr_inline_decode(xdr, 4);
4956         if (unlikely(!p))
4957                 goto out_overflow;
4958         flavor->gss.sec_oid4.len = be32_to_cpup(p);
4959         if (flavor->gss.sec_oid4.len > GSS_OID_MAX_LEN)
4960                 goto out_err;
4961
4962         p = xdr_inline_decode(xdr, flavor->gss.sec_oid4.len);
4963         if (unlikely(!p))
4964                 goto out_overflow;
4965         memcpy(flavor->gss.sec_oid4.data, p, flavor->gss.sec_oid4.len);
4966
4967         p = xdr_inline_decode(xdr, 8);
4968         if (unlikely(!p))
4969                 goto out_overflow;
4970         flavor->gss.qop4 = be32_to_cpup(p++);
4971         flavor->gss.service = be32_to_cpup(p);
4972
4973         return 0;
4974
4975 out_overflow:
4976         print_overflow_msg(__func__, xdr);
4977         return -EIO;
4978 out_err:
4979         return -EINVAL;
4980 }
4981
4982 static int decode_secinfo(struct xdr_stream *xdr, struct nfs4_secinfo_res *res)
4983 {
4984         struct nfs4_secinfo_flavor *sec_flavor;
4985         int status;
4986         __be32 *p;
4987         int i, num_flavors;
4988
4989         status = decode_op_hdr(xdr, OP_SECINFO);
4990         if (status)
4991                 goto out;
4992         p = xdr_inline_decode(xdr, 4);
4993         if (unlikely(!p))
4994                 goto out_overflow;
4995
4996         res->flavors->num_flavors = 0;
4997         num_flavors = be32_to_cpup(p);
4998
4999         for (i = 0; i < num_flavors; i++) {
5000                 sec_flavor = &res->flavors->flavors[i];
5001                 if ((char *)&sec_flavor[1] - (char *)res->flavors > PAGE_SIZE)
5002                         break;
5003
5004                 p = xdr_inline_decode(xdr, 4);
5005                 if (unlikely(!p))
5006                         goto out_overflow;
5007                 sec_flavor->flavor = be32_to_cpup(p);
5008
5009                 if (sec_flavor->flavor == RPC_AUTH_GSS) {
5010                         status = decode_secinfo_gss(xdr, sec_flavor);
5011                         if (status)
5012                                 goto out;
5013                 }
5014                 res->flavors->num_flavors++;
5015         }
5016
5017 out:
5018         return status;
5019 out_overflow:
5020         print_overflow_msg(__func__, xdr);
5021         return -EIO;
5022 }
5023
5024 #if defined(CONFIG_NFS_V4_1)
5025 static int decode_exchange_id(struct xdr_stream *xdr,
5026                               struct nfs41_exchange_id_res *res)
5027 {
5028         __be32 *p;
5029         uint32_t dummy;
5030         char *dummy_str;
5031         int status;
5032         struct nfs_client *clp = res->client;
5033
5034         status = decode_op_hdr(xdr, OP_EXCHANGE_ID);
5035         if (status)
5036                 return status;
5037
5038         p = xdr_inline_decode(xdr, 8);
5039         if (unlikely(!p))
5040                 goto out_overflow;
5041         xdr_decode_hyper(p, &clp->cl_clientid);
5042         p = xdr_inline_decode(xdr, 12);
5043         if (unlikely(!p))
5044                 goto out_overflow;
5045         clp->cl_seqid = be32_to_cpup(p++);
5046         clp->cl_exchange_flags = be32_to_cpup(p++);
5047
5048         /* We ask for SP4_NONE */
5049         dummy = be32_to_cpup(p);
5050         if (dummy != SP4_NONE)
5051                 return -EIO;
5052
5053         /* Throw away minor_id */
5054         p = xdr_inline_decode(xdr, 8);
5055         if (unlikely(!p))
5056                 goto out_overflow;
5057
5058         /* Throw away Major id */
5059         status = decode_opaque_inline(xdr, &dummy, &dummy_str);
5060         if (unlikely(status))
5061                 return status;
5062
5063         /* Save server_scope */
5064         status = decode_opaque_inline(xdr, &dummy, &dummy_str);
5065         if (unlikely(status))
5066                 return status;
5067
5068         if (unlikely(dummy > NFS4_OPAQUE_LIMIT))
5069                 return -EIO;
5070
5071         memcpy(res->server_scope->server_scope, dummy_str, dummy);
5072         res->server_scope->server_scope_sz = dummy;
5073
5074         /* Throw away Implementation id array */
5075         status = decode_opaque_inline(xdr, &dummy, &dummy_str);
5076         if (unlikely(status))
5077                 return status;
5078
5079         return 0;
5080 out_overflow:
5081         print_overflow_msg(__func__, xdr);
5082         return -EIO;
5083 }
5084
5085 static int decode_chan_attrs(struct xdr_stream *xdr,
5086                              struct nfs4_channel_attrs *attrs)
5087 {
5088         __be32 *p;
5089         u32 nr_attrs, val;
5090
5091         p = xdr_inline_decode(xdr, 28);
5092         if (unlikely(!p))
5093                 goto out_overflow;
5094         val = be32_to_cpup(p++);        /* headerpadsz */
5095         if (val)
5096                 return -EINVAL;         /* no support for header padding yet */
5097         attrs->max_rqst_sz = be32_to_cpup(p++);
5098         attrs->max_resp_sz = be32_to_cpup(p++);
5099         attrs->max_resp_sz_cached = be32_to_cpup(p++);
5100         attrs->max_ops = be32_to_cpup(p++);
5101         attrs->max_reqs = be32_to_cpup(p++);
5102         nr_attrs = be32_to_cpup(p);
5103         if (unlikely(nr_attrs > 1)) {
5104                 printk(KERN_WARNING "%s: Invalid rdma channel attrs count %u\n",
5105                         __func__, nr_attrs);
5106                 return -EINVAL;
5107         }
5108         if (nr_attrs == 1) {
5109                 p = xdr_inline_decode(xdr, 4); /* skip rdma_attrs */
5110                 if (unlikely(!p))
5111                         goto out_overflow;
5112         }
5113         return 0;
5114 out_overflow:
5115         print_overflow_msg(__func__, xdr);
5116         return -EIO;
5117 }
5118
5119 static int decode_sessionid(struct xdr_stream *xdr, struct nfs4_sessionid *sid)
5120 {
5121         return decode_opaque_fixed(xdr, sid->data, NFS4_MAX_SESSIONID_LEN);
5122 }
5123
5124 static int decode_create_session(struct xdr_stream *xdr,
5125                                  struct nfs41_create_session_res *res)
5126 {
5127         __be32 *p;
5128         int status;
5129         struct nfs_client *clp = res->client;
5130         struct nfs4_session *session = clp->cl_session;
5131
5132         status = decode_op_hdr(xdr, OP_CREATE_SESSION);
5133         if (!status)
5134                 status = decode_sessionid(xdr, &session->sess_id);
5135         if (unlikely(status))
5136                 return status;
5137
5138         /* seqid, flags */
5139         p = xdr_inline_decode(xdr, 8);
5140         if (unlikely(!p))
5141                 goto out_overflow;
5142         clp->cl_seqid = be32_to_cpup(p++);
5143         session->flags = be32_to_cpup(p);
5144
5145         /* Channel attributes */
5146         status = decode_chan_attrs(xdr, &session->fc_attrs);
5147         if (!status)
5148                 status = decode_chan_attrs(xdr, &session->bc_attrs);
5149         return status;
5150 out_overflow:
5151         print_overflow_msg(__func__, xdr);
5152         return -EIO;
5153 }
5154
5155 static int decode_destroy_session(struct xdr_stream *xdr, void *dummy)
5156 {
5157         return decode_op_hdr(xdr, OP_DESTROY_SESSION);
5158 }
5159
5160 static int decode_reclaim_complete(struct xdr_stream *xdr, void *dummy)
5161 {
5162         return decode_op_hdr(xdr, OP_RECLAIM_COMPLETE);
5163 }
5164 #endif /* CONFIG_NFS_V4_1 */
5165
5166 static int decode_sequence(struct xdr_stream *xdr,
5167                            struct nfs4_sequence_res *res,
5168                            struct rpc_rqst *rqstp)
5169 {
5170 #if defined(CONFIG_NFS_V4_1)
5171         struct nfs4_sessionid id;
5172         u32 dummy;
5173         int status;
5174         __be32 *p;
5175
5176         if (!res->sr_session)
5177                 return 0;
5178
5179         status = decode_op_hdr(xdr, OP_SEQUENCE);
5180         if (!status)
5181                 status = decode_sessionid(xdr, &id);
5182         if (unlikely(status))
5183                 goto out_err;
5184
5185         /*
5186          * If the server returns different values for sessionID, slotID or
5187          * sequence number, the server is looney tunes.
5188          */
5189         status = -EREMOTEIO;
5190
5191         if (memcmp(id.data, res->sr_session->sess_id.data,
5192                    NFS4_MAX_SESSIONID_LEN)) {
5193                 dprintk("%s Invalid session id\n", __func__);
5194                 goto out_err;
5195         }
5196
5197         p = xdr_inline_decode(xdr, 20);
5198         if (unlikely(!p))
5199                 goto out_overflow;
5200
5201         /* seqid */
5202         dummy = be32_to_cpup(p++);
5203         if (dummy != res->sr_slot->seq_nr) {
5204                 dprintk("%s Invalid sequence number\n", __func__);
5205                 goto out_err;
5206         }
5207         /* slot id */
5208         dummy = be32_to_cpup(p++);
5209         if (dummy != res->sr_slot - res->sr_session->fc_slot_table.slots) {
5210                 dprintk("%s Invalid slot id\n", __func__);
5211                 goto out_err;
5212         }
5213         /* highest slot id - currently not processed */
5214         dummy = be32_to_cpup(p++);
5215         /* target highest slot id - currently not processed */
5216         dummy = be32_to_cpup(p++);
5217         /* result flags */
5218         res->sr_status_flags = be32_to_cpup(p);
5219         status = 0;
5220 out_err:
5221         res->sr_status = status;
5222         return status;
5223 out_overflow:
5224         print_overflow_msg(__func__, xdr);
5225         status = -EIO;
5226         goto out_err;
5227 #else  /* CONFIG_NFS_V4_1 */
5228         return 0;
5229 #endif /* CONFIG_NFS_V4_1 */
5230 }
5231
5232 #if defined(CONFIG_NFS_V4_1)
5233
5234 static int decode_getdeviceinfo(struct xdr_stream *xdr,
5235                                 struct pnfs_device *pdev)
5236 {
5237         __be32 *p;
5238         uint32_t len, type;
5239         int status;
5240
5241         status = decode_op_hdr(xdr, OP_GETDEVICEINFO);
5242         if (status) {
5243                 if (status == -ETOOSMALL) {
5244                         p = xdr_inline_decode(xdr, 4);
5245                         if (unlikely(!p))
5246                                 goto out_overflow;
5247                         pdev->mincount = be32_to_cpup(p);
5248                         dprintk("%s: Min count too small. mincnt = %u\n",
5249                                 __func__, pdev->mincount);
5250                 }
5251                 return status;
5252         }
5253
5254         p = xdr_inline_decode(xdr, 8);
5255         if (unlikely(!p))
5256                 goto out_overflow;
5257         type = be32_to_cpup(p++);
5258         if (type != pdev->layout_type) {
5259                 dprintk("%s: layout mismatch req: %u pdev: %u\n",
5260                         __func__, pdev->layout_type, type);
5261                 return -EINVAL;
5262         }
5263         /*
5264          * Get the length of the opaque device_addr4. xdr_read_pages places
5265          * the opaque device_addr4 in the xdr_buf->pages (pnfs_device->pages)
5266          * and places the remaining xdr data in xdr_buf->tail
5267          */
5268         pdev->mincount = be32_to_cpup(p);
5269         xdr_read_pages(xdr, pdev->mincount); /* include space for the length */
5270
5271         /* Parse notification bitmap, verifying that it is zero. */
5272         p = xdr_inline_decode(xdr, 4);
5273         if (unlikely(!p))
5274                 goto out_overflow;
5275         len = be32_to_cpup(p);
5276         if (len) {
5277                 uint32_t i;
5278
5279                 p = xdr_inline_decode(xdr, 4 * len);
5280                 if (unlikely(!p))
5281                         goto out_overflow;
5282                 for (i = 0; i < len; i++, p++) {
5283                         if (be32_to_cpup(p)) {
5284                                 dprintk("%s: notifications not supported\n",
5285                                         __func__);
5286                                 return -EIO;
5287                         }
5288                 }
5289         }
5290         return 0;
5291 out_overflow:
5292         print_overflow_msg(__func__, xdr);
5293         return -EIO;
5294 }
5295
5296 static int decode_layoutget(struct xdr_stream *xdr, struct rpc_rqst *req,
5297                             struct nfs4_layoutget_res *res)
5298 {
5299         __be32 *p;
5300         int status;
5301         u32 layout_count;
5302         struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
5303         struct kvec *iov = rcvbuf->head;
5304         u32 hdrlen, recvd;
5305
5306         status = decode_op_hdr(xdr, OP_LAYOUTGET);
5307         if (status)
5308                 return status;
5309         p = xdr_inline_decode(xdr, 8 + NFS4_STATEID_SIZE);
5310         if (unlikely(!p))
5311                 goto out_overflow;
5312         res->return_on_close = be32_to_cpup(p++);
5313         p = xdr_decode_opaque_fixed(p, res->stateid.data, NFS4_STATEID_SIZE);
5314         layout_count = be32_to_cpup(p);
5315         if (!layout_count) {
5316                 dprintk("%s: server responded with empty layout array\n",
5317                         __func__);
5318                 return -EINVAL;
5319         }
5320
5321         p = xdr_inline_decode(xdr, 28);
5322         if (unlikely(!p))
5323                 goto out_overflow;
5324         p = xdr_decode_hyper(p, &res->range.offset);
5325         p = xdr_decode_hyper(p, &res->range.length);
5326         res->range.iomode = be32_to_cpup(p++);
5327         res->type = be32_to_cpup(p++);
5328         res->layoutp->len = be32_to_cpup(p);
5329
5330         dprintk("%s roff:%lu rlen:%lu riomode:%d, lo_type:0x%x, lo.len:%d\n",
5331                 __func__,
5332                 (unsigned long)res->range.offset,
5333                 (unsigned long)res->range.length,
5334                 res->range.iomode,
5335                 res->type,
5336                 res->layoutp->len);
5337
5338         hdrlen = (u8 *) xdr->p - (u8 *) iov->iov_base;
5339         recvd = req->rq_rcv_buf.len - hdrlen;
5340         if (res->layoutp->len > recvd) {
5341                 dprintk("NFS: server cheating in layoutget reply: "
5342                                 "layout len %u > recvd %u\n",
5343                                 res->layoutp->len, recvd);
5344                 return -EINVAL;
5345         }
5346
5347         xdr_read_pages(xdr, res->layoutp->len);
5348
5349         if (layout_count > 1) {
5350                 /* We only handle a length one array at the moment.  Any
5351                  * further entries are just ignored.  Note that this means
5352                  * the client may see a response that is less than the
5353                  * minimum it requested.
5354                  */
5355                 dprintk("%s: server responded with %d layouts, dropping tail\n",
5356                         __func__, layout_count);
5357         }
5358
5359         return 0;
5360 out_overflow:
5361         print_overflow_msg(__func__, xdr);
5362         return -EIO;
5363 }
5364
5365 static int decode_layoutreturn(struct xdr_stream *xdr,
5366                                struct nfs4_layoutreturn_res *res)
5367 {
5368         __be32 *p;
5369         int status;
5370
5371         status = decode_op_hdr(xdr, OP_LAYOUTRETURN);
5372         if (status)
5373                 return status;
5374         p = xdr_inline_decode(xdr, 4);
5375         if (unlikely(!p))
5376                 goto out_overflow;
5377         res->lrs_present = be32_to_cpup(p);
5378         if (res->lrs_present)
5379                 status = decode_stateid(xdr, &res->stateid);
5380         return status;
5381 out_overflow:
5382         print_overflow_msg(__func__, xdr);
5383         return -EIO;
5384 }
5385
5386 static int decode_layoutcommit(struct xdr_stream *xdr,
5387                                struct rpc_rqst *req,
5388                                struct nfs4_layoutcommit_res *res)
5389 {
5390         __be32 *p;
5391         __u32 sizechanged;
5392         int status;
5393
5394         status = decode_op_hdr(xdr, OP_LAYOUTCOMMIT);
5395         if (status)
5396                 return status;
5397
5398         p = xdr_inline_decode(xdr, 4);
5399         if (unlikely(!p))
5400                 goto out_overflow;
5401         sizechanged = be32_to_cpup(p);
5402
5403         if (sizechanged) {
5404                 /* throw away new size */
5405                 p = xdr_inline_decode(xdr, 8);
5406                 if (unlikely(!p))
5407                         goto out_overflow;
5408         }
5409         return 0;
5410 out_overflow:
5411         print_overflow_msg(__func__, xdr);
5412         return -EIO;
5413 }
5414
5415 static int decode_test_stateid(struct xdr_stream *xdr,
5416                                struct nfs41_test_stateid_res *res)
5417 {
5418         __be32 *p;
5419         int status;
5420         int num_res;
5421
5422         status = decode_op_hdr(xdr, OP_TEST_STATEID);
5423         if (status)
5424                 return status;
5425
5426         p = xdr_inline_decode(xdr, 4);
5427         if (unlikely(!p))
5428                 goto out_overflow;
5429         num_res = be32_to_cpup(p++);
5430         if (num_res != 1)
5431                 goto out;
5432
5433         p = xdr_inline_decode(xdr, 4);
5434         if (unlikely(!p))
5435                 goto out_overflow;
5436         res->status = be32_to_cpup(p++);
5437         return res->status;
5438 out_overflow:
5439         print_overflow_msg(__func__, xdr);
5440 out:
5441         return -EIO;
5442 }
5443 #endif /* CONFIG_NFS_V4_1 */
5444
5445 /*
5446  * END OF "GENERIC" DECODE ROUTINES.
5447  */
5448
5449 /*
5450  * Decode OPEN_DOWNGRADE response
5451  */
5452 static int nfs4_xdr_dec_open_downgrade(struct rpc_rqst *rqstp,
5453                                        struct xdr_stream *xdr,
5454                                        struct nfs_closeres *res)
5455 {
5456         struct compound_hdr hdr;
5457         int status;
5458
5459         status = decode_compound_hdr(xdr, &hdr);
5460         if (status)
5461                 goto out;
5462         status = decode_sequence(xdr, &res->seq_res, rqstp);
5463         if (status)
5464                 goto out;
5465         status = decode_putfh(xdr);
5466         if (status)
5467                 goto out;
5468         status = decode_open_downgrade(xdr, res);
5469         if (status != 0)
5470                 goto out;
5471         decode_getfattr(xdr, res->fattr, res->server,
5472                         !RPC_IS_ASYNC(rqstp->rq_task));
5473 out:
5474         return status;
5475 }
5476
5477 /*
5478  * Decode ACCESS response
5479  */
5480 static int nfs4_xdr_dec_access(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
5481                                struct nfs4_accessres *res)
5482 {
5483         struct compound_hdr hdr;
5484         int status;
5485
5486         status = decode_compound_hdr(xdr, &hdr);
5487         if (status)
5488                 goto out;
5489         status = decode_sequence(xdr, &res->seq_res, rqstp);
5490         if (status)
5491                 goto out;
5492         status = decode_putfh(xdr);
5493         if (status != 0)
5494                 goto out;
5495         status = decode_access(xdr, res);
5496         if (status != 0)
5497                 goto out;
5498         decode_getfattr(xdr, res->fattr, res->server,
5499                         !RPC_IS_ASYNC(rqstp->rq_task));
5500 out:
5501         return status;
5502 }
5503
5504 /*
5505  * Decode LOOKUP response
5506  */
5507 static int nfs4_xdr_dec_lookup(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
5508                                struct nfs4_lookup_res *res)
5509 {
5510         struct compound_hdr hdr;
5511         int status;
5512
5513         status = decode_compound_hdr(xdr, &hdr);
5514         if (status)
5515                 goto out;
5516         status = decode_sequence(xdr, &res->seq_res, rqstp);
5517         if (status)
5518                 goto out;
5519         status = decode_putfh(xdr);
5520         if (status)
5521                 goto out;
5522         status = decode_lookup(xdr);
5523         if (status)
5524                 goto out;
5525         status = decode_getfh(xdr, res->fh);
5526         if (status)
5527                 goto out;
5528         status = decode_getfattr(xdr, res->fattr, res->server
5529                         ,!RPC_IS_ASYNC(rqstp->rq_task));
5530 out:
5531         return status;
5532 }
5533
5534 /*
5535  * Decode LOOKUP_ROOT response
5536  */
5537 static int nfs4_xdr_dec_lookup_root(struct rpc_rqst *rqstp,
5538                                     struct xdr_stream *xdr,
5539                                     struct nfs4_lookup_res *res)
5540 {
5541         struct compound_hdr hdr;
5542         int status;
5543
5544         status = decode_compound_hdr(xdr, &hdr);
5545         if (status)
5546                 goto out;
5547         status = decode_sequence(xdr, &res->seq_res, rqstp);
5548         if (status)
5549                 goto out;
5550         status = decode_putrootfh(xdr);
5551         if (status)
5552                 goto out;
5553         status = decode_getfh(xdr, res->fh);
5554         if (status == 0)
5555                 status = decode_getfattr(xdr, res->fattr, res->server,
5556                                 !RPC_IS_ASYNC(rqstp->rq_task));
5557 out:
5558         return status;
5559 }
5560
5561 /*
5562  * Decode REMOVE response
5563  */
5564 static int nfs4_xdr_dec_remove(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
5565                                struct nfs_removeres *res)
5566 {
5567         struct compound_hdr hdr;
5568         int status;
5569
5570         status = decode_compound_hdr(xdr, &hdr);
5571         if (status)
5572                 goto out;
5573         status = decode_sequence(xdr, &res->seq_res, rqstp);
5574         if (status)
5575                 goto out;
5576         status = decode_putfh(xdr);
5577         if (status)
5578                 goto out;
5579         status = decode_remove(xdr, &res->cinfo);
5580         if (status)
5581                 goto out;
5582         decode_getfattr(xdr, res->dir_attr, res->server,
5583                         !RPC_IS_ASYNC(rqstp->rq_task));
5584 out:
5585         return status;
5586 }
5587
5588 /*
5589  * Decode RENAME response
5590  */
5591 static int nfs4_xdr_dec_rename(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
5592                                struct nfs_renameres *res)
5593 {
5594         struct compound_hdr hdr;
5595         int status;
5596
5597         status = decode_compound_hdr(xdr, &hdr);
5598         if (status)
5599                 goto out;
5600         status = decode_sequence(xdr, &res->seq_res, rqstp);
5601         if (status)
5602                 goto out;
5603         status = decode_putfh(xdr);
5604         if (status)
5605                 goto out;
5606         status = decode_savefh(xdr);
5607         if (status)
5608                 goto out;
5609         status = decode_putfh(xdr);
5610         if (status)
5611                 goto out;
5612         status = decode_rename(xdr, &res->old_cinfo, &res->new_cinfo);
5613         if (status)
5614                 goto out;
5615         /* Current FH is target directory */
5616         if (decode_getfattr(xdr, res->new_fattr, res->server,
5617                                 !RPC_IS_ASYNC(rqstp->rq_task)) != 0)
5618                 goto out;
5619         status = decode_restorefh(xdr);
5620         if (status)
5621                 goto out;
5622         decode_getfattr(xdr, res->old_fattr, res->server,
5623                         !RPC_IS_ASYNC(rqstp->rq_task));
5624 out:
5625         return status;
5626 }
5627
5628 /*
5629  * Decode LINK response
5630  */
5631 static int nfs4_xdr_dec_link(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
5632                              struct nfs4_link_res *res)
5633 {
5634         struct compound_hdr hdr;
5635         int status;
5636
5637         status = decode_compound_hdr(xdr, &hdr);
5638         if (status)
5639                 goto out;
5640         status = decode_sequence(xdr, &res->seq_res, rqstp);
5641         if (status)
5642                 goto out;
5643         status = decode_putfh(xdr);
5644         if (status)
5645                 goto out;
5646         status = decode_savefh(xdr);
5647         if (status)
5648                 goto out;
5649         status = decode_putfh(xdr);
5650         if (status)
5651                 goto out;
5652         status = decode_link(xdr, &res->cinfo);
5653         if (status)
5654                 goto out;
5655         /*
5656          * Note order: OP_LINK leaves the directory as the current
5657          *             filehandle.
5658          */
5659         if (decode_getfattr(xdr, res->dir_attr, res->server,
5660                                 !RPC_IS_ASYNC(rqstp->rq_task)) != 0)
5661                 goto out;
5662         status = decode_restorefh(xdr);
5663         if (status)
5664                 goto out;
5665         decode_getfattr(xdr, res->fattr, res->server,
5666                         !RPC_IS_ASYNC(rqstp->rq_task));
5667 out:
5668         return status;
5669 }
5670
5671 /*
5672  * Decode CREATE response
5673  */
5674 static int nfs4_xdr_dec_create(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
5675                                struct nfs4_create_res *res)
5676 {
5677         struct compound_hdr hdr;
5678         int status;
5679
5680         status = decode_compound_hdr(xdr, &hdr);
5681         if (status)
5682                 goto out;
5683         status = decode_sequence(xdr, &res->seq_res, rqstp);
5684         if (status)
5685                 goto out;
5686         status = decode_putfh(xdr);
5687         if (status)
5688                 goto out;
5689         status = decode_savefh(xdr);
5690         if (status)
5691                 goto out;
5692         status = decode_create(xdr, &res->dir_cinfo);
5693         if (status)
5694                 goto out;
5695         status = decode_getfh(xdr, res->fh);
5696         if (status)
5697                 goto out;
5698         if (decode_getfattr(xdr, res->fattr, res->server,
5699                                 !RPC_IS_ASYNC(rqstp->rq_task)) != 0)
5700                 goto out;
5701         status = decode_restorefh(xdr);
5702         if (status)
5703                 goto out;
5704         decode_getfattr(xdr, res->dir_fattr, res->server,
5705                         !RPC_IS_ASYNC(rqstp->rq_task));
5706 out:
5707         return status;
5708 }
5709
5710 /*
5711  * Decode SYMLINK response
5712  */
5713 static int nfs4_xdr_dec_symlink(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
5714                                 struct nfs4_create_res *res)
5715 {
5716         return nfs4_xdr_dec_create(rqstp, xdr, res);
5717 }
5718
5719 /*
5720  * Decode GETATTR response
5721  */
5722 static int nfs4_xdr_dec_getattr(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
5723                                 struct nfs4_getattr_res *res)
5724 {
5725         struct compound_hdr hdr;
5726         int status;
5727
5728         status = decode_compound_hdr(xdr, &hdr);
5729         if (status)
5730                 goto out;
5731         status = decode_sequence(xdr, &res->seq_res, rqstp);
5732         if (status)
5733                 goto out;
5734         status = decode_putfh(xdr);
5735         if (status)
5736                 goto out;
5737         status = decode_getfattr(xdr, res->fattr, res->server,
5738                         !RPC_IS_ASYNC(rqstp->rq_task));
5739 out:
5740         return status;
5741 }
5742
5743 /*
5744  * Encode an SETACL request
5745  */
5746 static void nfs4_xdr_enc_setacl(struct rpc_rqst *req, struct xdr_stream *xdr,
5747                                 struct nfs_setaclargs *args)
5748 {
5749         struct compound_hdr hdr = {
5750                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
5751         };
5752
5753         encode_compound_hdr(xdr, req, &hdr);
5754         encode_sequence(xdr, &args->seq_args, &hdr);
5755         encode_putfh(xdr, args->fh, &hdr);
5756         encode_setacl(xdr, args, &hdr);
5757         encode_nops(&hdr);
5758 }
5759
5760 /*
5761  * Decode SETACL response
5762  */
5763 static int
5764 nfs4_xdr_dec_setacl(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
5765                     struct nfs_setaclres *res)
5766 {
5767         struct compound_hdr hdr;
5768         int status;
5769
5770         status = decode_compound_hdr(xdr, &hdr);
5771         if (status)
5772                 goto out;
5773         status = decode_sequence(xdr, &res->seq_res, rqstp);
5774         if (status)
5775                 goto out;
5776         status = decode_putfh(xdr);
5777         if (status)
5778                 goto out;
5779         status = decode_setattr(xdr);
5780 out:
5781         return status;
5782 }
5783
5784 /*
5785  * Decode GETACL response
5786  */
5787 static int
5788 nfs4_xdr_dec_getacl(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
5789                     struct nfs_getaclres *res)
5790 {
5791         struct compound_hdr hdr;
5792         int status;
5793
5794         status = decode_compound_hdr(xdr, &hdr);
5795         if (status)
5796                 goto out;
5797         status = decode_sequence(xdr, &res->seq_res, rqstp);
5798         if (status)
5799                 goto out;
5800         status = decode_putfh(xdr);
5801         if (status)
5802                 goto out;
5803         status = decode_getacl(xdr, rqstp, &res->acl_len);
5804
5805 out:
5806         return status;
5807 }
5808
5809 /*
5810  * Decode CLOSE response
5811  */
5812 static int nfs4_xdr_dec_close(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
5813                               struct nfs_closeres *res)
5814 {
5815         struct compound_hdr hdr;
5816         int status;
5817
5818         status = decode_compound_hdr(xdr, &hdr);
5819         if (status)
5820                 goto out;
5821         status = decode_sequence(xdr, &res->seq_res, rqstp);
5822         if (status)
5823                 goto out;
5824         status = decode_putfh(xdr);
5825         if (status)
5826                 goto out;
5827         status = decode_close(xdr, res);
5828         if (status != 0)
5829                 goto out;
5830         /*
5831          * Note: Server may do delete on close for this file
5832          *      in which case the getattr call will fail with
5833          *      an ESTALE error. Shouldn't be a problem,
5834          *      though, since fattr->valid will remain unset.
5835          */
5836         decode_getfattr(xdr, res->fattr, res->server,
5837                         !RPC_IS_ASYNC(rqstp->rq_task));
5838 out:
5839         return status;
5840 }
5841
5842 /*
5843  * Decode OPEN response
5844  */
5845 static int nfs4_xdr_dec_open(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
5846                              struct nfs_openres *res)
5847 {
5848         struct compound_hdr hdr;
5849         int status;
5850
5851         status = decode_compound_hdr(xdr, &hdr);
5852         if (status)
5853                 goto out;
5854         status = decode_sequence(xdr, &res->seq_res, rqstp);
5855         if (status)
5856                 goto out;
5857         status = decode_putfh(xdr);
5858         if (status)
5859                 goto out;
5860         status = decode_savefh(xdr);
5861         if (status)
5862                 goto out;
5863         status = decode_open(xdr, res);
5864         if (status)
5865                 goto out;
5866         if (decode_getfh(xdr, &res->fh) != 0)
5867                 goto out;
5868         if (decode_getfattr(xdr, res->f_attr, res->server,
5869                                 !RPC_IS_ASYNC(rqstp->rq_task)) != 0)
5870                 goto out;
5871         if (decode_restorefh(xdr) != 0)
5872                 goto out;
5873         decode_getfattr(xdr, res->dir_attr, res->server,
5874                         !RPC_IS_ASYNC(rqstp->rq_task));
5875 out:
5876         return status;
5877 }
5878
5879 /*
5880  * Decode OPEN_CONFIRM response
5881  */
5882 static int nfs4_xdr_dec_open_confirm(struct rpc_rqst *rqstp,
5883                                      struct xdr_stream *xdr,
5884                                      struct nfs_open_confirmres *res)
5885 {
5886         struct compound_hdr hdr;
5887         int status;
5888
5889         status = decode_compound_hdr(xdr, &hdr);
5890         if (status)
5891                 goto out;
5892         status = decode_putfh(xdr);
5893         if (status)
5894                 goto out;
5895         status = decode_open_confirm(xdr, res);
5896 out:
5897         return status;
5898 }
5899
5900 /*
5901  * Decode OPEN response
5902  */
5903 static int nfs4_xdr_dec_open_noattr(struct rpc_rqst *rqstp,
5904                                     struct xdr_stream *xdr,
5905                                     struct nfs_openres *res)
5906 {
5907         struct compound_hdr hdr;
5908         int status;
5909
5910         status = decode_compound_hdr(xdr, &hdr);
5911         if (status)
5912                 goto out;
5913         status = decode_sequence(xdr, &res->seq_res, rqstp);
5914         if (status)
5915                 goto out;
5916         status = decode_putfh(xdr);
5917         if (status)
5918                 goto out;
5919         status = decode_open(xdr, res);
5920         if (status)
5921                 goto out;
5922         decode_getfattr(xdr, res->f_attr, res->server,
5923                         !RPC_IS_ASYNC(rqstp->rq_task));
5924 out:
5925         return status;
5926 }
5927
5928 /*
5929  * Decode SETATTR response
5930  */
5931 static int nfs4_xdr_dec_setattr(struct rpc_rqst *rqstp,
5932                                 struct xdr_stream *xdr,
5933                                 struct nfs_setattrres *res)
5934 {
5935         struct compound_hdr hdr;
5936         int status;
5937
5938         status = decode_compound_hdr(xdr, &hdr);
5939         if (status)
5940                 goto out;
5941         status = decode_sequence(xdr, &res->seq_res, rqstp);
5942         if (status)
5943                 goto out;
5944         status = decode_putfh(xdr);
5945         if (status)
5946                 goto out;
5947         status = decode_setattr(xdr);
5948         if (status)
5949                 goto out;
5950         decode_getfattr(xdr, res->fattr, res->server,
5951                         !RPC_IS_ASYNC(rqstp->rq_task));
5952 out:
5953         return status;
5954 }
5955
5956 /*
5957  * Decode LOCK response
5958  */
5959 static int nfs4_xdr_dec_lock(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
5960                              struct nfs_lock_res *res)
5961 {
5962         struct compound_hdr hdr;
5963         int status;
5964
5965         status = decode_compound_hdr(xdr, &hdr);
5966         if (status)
5967                 goto out;
5968         status = decode_sequence(xdr, &res->seq_res, rqstp);
5969         if (status)
5970                 goto out;
5971         status = decode_putfh(xdr);
5972         if (status)
5973                 goto out;
5974         status = decode_lock(xdr, res);
5975 out:
5976         return status;
5977 }
5978
5979 /*
5980  * Decode LOCKT response
5981  */
5982 static int nfs4_xdr_dec_lockt(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
5983                               struct nfs_lockt_res *res)
5984 {
5985         struct compound_hdr hdr;
5986         int status;
5987
5988         status = decode_compound_hdr(xdr, &hdr);
5989         if (status)
5990                 goto out;
5991         status = decode_sequence(xdr, &res->seq_res, rqstp);
5992         if (status)
5993                 goto out;
5994         status = decode_putfh(xdr);
5995         if (status)
5996                 goto out;
5997         status = decode_lockt(xdr, res);
5998 out:
5999         return status;
6000 }
6001
6002 /*
6003  * Decode LOCKU response
6004  */
6005 static int nfs4_xdr_dec_locku(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6006                               struct nfs_locku_res *res)
6007 {
6008         struct compound_hdr hdr;
6009         int status;
6010
6011         status = decode_compound_hdr(xdr, &hdr);
6012         if (status)
6013                 goto out;
6014         status = decode_sequence(xdr, &res->seq_res, rqstp);
6015         if (status)
6016                 goto out;
6017         status = decode_putfh(xdr);
6018         if (status)
6019                 goto out;
6020         status = decode_locku(xdr, res);
6021 out:
6022         return status;
6023 }
6024
6025 static int nfs4_xdr_dec_release_lockowner(struct rpc_rqst *rqstp,
6026                                           struct xdr_stream *xdr, void *dummy)
6027 {
6028         struct compound_hdr hdr;
6029         int status;
6030
6031         status = decode_compound_hdr(xdr, &hdr);
6032         if (!status)
6033                 status = decode_release_lockowner(xdr);
6034         return status;
6035 }
6036
6037 /*
6038  * Decode READLINK response
6039  */
6040 static int nfs4_xdr_dec_readlink(struct rpc_rqst *rqstp,
6041                                  struct xdr_stream *xdr,
6042                                  struct nfs4_readlink_res *res)
6043 {
6044         struct compound_hdr hdr;
6045         int status;
6046
6047         status = decode_compound_hdr(xdr, &hdr);
6048         if (status)
6049                 goto out;
6050         status = decode_sequence(xdr, &res->seq_res, rqstp);
6051         if (status)
6052                 goto out;
6053         status = decode_putfh(xdr);
6054         if (status)
6055                 goto out;
6056         status = decode_readlink(xdr, rqstp);
6057 out:
6058         return status;
6059 }
6060
6061 /*
6062  * Decode READDIR response
6063  */
6064 static int nfs4_xdr_dec_readdir(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6065                                 struct nfs4_readdir_res *res)
6066 {
6067         struct compound_hdr hdr;
6068         int status;
6069
6070         status = decode_compound_hdr(xdr, &hdr);
6071         if (status)
6072                 goto out;
6073         status = decode_sequence(xdr, &res->seq_res, rqstp);
6074         if (status)
6075                 goto out;
6076         status = decode_putfh(xdr);
6077         if (status)
6078                 goto out;
6079         status = decode_readdir(xdr, rqstp, res);
6080 out:
6081         return status;
6082 }
6083
6084 /*
6085  * Decode Read response
6086  */
6087 static int nfs4_xdr_dec_read(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6088                              struct nfs_readres *res)
6089 {
6090         struct compound_hdr hdr;
6091         int status;
6092
6093         status = decode_compound_hdr(xdr, &hdr);
6094         if (status)
6095                 goto out;
6096         status = decode_sequence(xdr, &res->seq_res, rqstp);
6097         if (status)
6098                 goto out;
6099         status = decode_putfh(xdr);
6100         if (status)
6101                 goto out;
6102         status = decode_read(xdr, rqstp, res);
6103         if (!status)
6104                 status = res->count;
6105 out:
6106         return status;
6107 }
6108
6109 /*
6110  * Decode WRITE response
6111  */
6112 static int nfs4_xdr_dec_write(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6113                               struct nfs_writeres *res)
6114 {
6115         struct compound_hdr hdr;
6116         int status;
6117
6118         status = decode_compound_hdr(xdr, &hdr);
6119         if (status)
6120                 goto out;
6121         status = decode_sequence(xdr, &res->seq_res, rqstp);
6122         if (status)
6123                 goto out;
6124         status = decode_putfh(xdr);
6125         if (status)
6126                 goto out;
6127         status = decode_write(xdr, res);
6128         if (status)
6129               &nbs