]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/nfs/nfs4xdr.c
a2329d69502ba7d51a62103f90c7f67f435f4347
[karo-tx-linux.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/module.h>
48 #include <linux/utsname.h>
49 #include <linux/sunrpc/clnt.h>
50 #include <linux/sunrpc/msg_prot.h>
51 #include <linux/sunrpc/gss_api.h>
52 #include <linux/nfs.h>
53 #include <linux/nfs4.h>
54 #include <linux/nfs_fs.h>
55 #include <linux/nfs_idmap.h>
56
57 #include "nfs4_fs.h"
58 #include "internal.h"
59 #include "nfs4session.h"
60 #include "pnfs.h"
61 #include "netns.h"
62
63 #define NFSDBG_FACILITY         NFSDBG_XDR
64
65 /* Mapping from NFS error code to "errno" error code. */
66 #define errno_NFSERR_IO         EIO
67
68 static int nfs4_stat_to_errno(int);
69
70 /* NFSv4 COMPOUND tags are only wanted for debugging purposes */
71 #ifdef DEBUG
72 #define NFS4_MAXTAGLEN          20
73 #else
74 #define NFS4_MAXTAGLEN          0
75 #endif
76
77 /* lock,open owner id:
78  * we currently use size 2 (u64) out of (NFS4_OPAQUE_LIMIT  >> 2)
79  */
80 #define open_owner_id_maxsz     (1 + 2 + 1 + 1 + 2)
81 #define lock_owner_id_maxsz     (1 + 1 + 4)
82 #define decode_lockowner_maxsz  (1 + XDR_QUADLEN(IDMAP_NAMESZ))
83 #define compound_encode_hdr_maxsz       (3 + (NFS4_MAXTAGLEN >> 2))
84 #define compound_decode_hdr_maxsz       (3 + (NFS4_MAXTAGLEN >> 2))
85 #define op_encode_hdr_maxsz     (1)
86 #define op_decode_hdr_maxsz     (2)
87 #define encode_stateid_maxsz    (XDR_QUADLEN(NFS4_STATEID_SIZE))
88 #define decode_stateid_maxsz    (XDR_QUADLEN(NFS4_STATEID_SIZE))
89 #define encode_verifier_maxsz   (XDR_QUADLEN(NFS4_VERIFIER_SIZE))
90 #define decode_verifier_maxsz   (XDR_QUADLEN(NFS4_VERIFIER_SIZE))
91 #define encode_putfh_maxsz      (op_encode_hdr_maxsz + 1 + \
92                                 (NFS4_FHSIZE >> 2))
93 #define decode_putfh_maxsz      (op_decode_hdr_maxsz)
94 #define encode_putrootfh_maxsz  (op_encode_hdr_maxsz)
95 #define decode_putrootfh_maxsz  (op_decode_hdr_maxsz)
96 #define encode_getfh_maxsz      (op_encode_hdr_maxsz)
97 #define decode_getfh_maxsz      (op_decode_hdr_maxsz + 1 + \
98                                 ((3+NFS4_FHSIZE) >> 2))
99 #define nfs4_fattr_bitmap_maxsz 4
100 #define encode_getattr_maxsz    (op_encode_hdr_maxsz + nfs4_fattr_bitmap_maxsz)
101 #define nfs4_name_maxsz         (1 + ((3 + NFS4_MAXNAMLEN) >> 2))
102 #define nfs4_path_maxsz         (1 + ((3 + NFS4_MAXPATHLEN) >> 2))
103 #define nfs4_owner_maxsz        (1 + XDR_QUADLEN(IDMAP_NAMESZ))
104 #define nfs4_group_maxsz        (1 + XDR_QUADLEN(IDMAP_NAMESZ))
105 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
106 /* PI(4 bytes) + LFS(4 bytes) + 1(for null terminator?) + MAXLABELLEN */
107 #define nfs4_label_maxsz        (4 + 4 + 1 + XDR_QUADLEN(NFS4_MAXLABELLEN))
108 #else
109 #define nfs4_label_maxsz        0
110 #endif
111 /* We support only one layout type per file system */
112 #define decode_mdsthreshold_maxsz (1 + 1 + nfs4_fattr_bitmap_maxsz + 1 + 8)
113 /* This is based on getfattr, which uses the most attributes: */
114 #define nfs4_fattr_value_maxsz  (1 + (1 + 2 + 2 + 4 + 2 + 1 + 1 + 2 + 2 + \
115                                 3 + 3 + 3 + nfs4_owner_maxsz + \
116                                 nfs4_group_maxsz + nfs4_label_maxsz + \
117                                  decode_mdsthreshold_maxsz))
118 #define nfs4_fattr_maxsz        (nfs4_fattr_bitmap_maxsz + \
119                                 nfs4_fattr_value_maxsz)
120 #define decode_getattr_maxsz    (op_decode_hdr_maxsz + nfs4_fattr_maxsz)
121 #define encode_attrs_maxsz      (nfs4_fattr_bitmap_maxsz + \
122                                  1 + 2 + 1 + \
123                                 nfs4_owner_maxsz + \
124                                 nfs4_group_maxsz + \
125                                 nfs4_label_maxsz + \
126                                 4 + 4)
127 #define encode_savefh_maxsz     (op_encode_hdr_maxsz)
128 #define decode_savefh_maxsz     (op_decode_hdr_maxsz)
129 #define encode_restorefh_maxsz  (op_encode_hdr_maxsz)
130 #define decode_restorefh_maxsz  (op_decode_hdr_maxsz)
131 #define encode_fsinfo_maxsz     (encode_getattr_maxsz)
132 /* The 5 accounts for the PNFS attributes, and assumes that at most three
133  * layout types will be returned.
134  */
135 #define decode_fsinfo_maxsz     (op_decode_hdr_maxsz + \
136                                  nfs4_fattr_bitmap_maxsz + 4 + 8 + 5)
137 #define encode_renew_maxsz      (op_encode_hdr_maxsz + 3)
138 #define decode_renew_maxsz      (op_decode_hdr_maxsz)
139 #define encode_setclientid_maxsz \
140                                 (op_encode_hdr_maxsz + \
141                                 XDR_QUADLEN(NFS4_VERIFIER_SIZE) + \
142                                 XDR_QUADLEN(NFS4_SETCLIENTID_NAMELEN) + \
143                                 1 /* sc_prog */ + \
144                                 1 + XDR_QUADLEN(RPCBIND_MAXNETIDLEN) + \
145                                 1 + XDR_QUADLEN(RPCBIND_MAXUADDRLEN) + \
146                                 1) /* sc_cb_ident */
147 #define decode_setclientid_maxsz \
148                                 (op_decode_hdr_maxsz + \
149                                 2 /* clientid */ + \
150                                 XDR_QUADLEN(NFS4_VERIFIER_SIZE) + \
151                                 1 + XDR_QUADLEN(RPCBIND_MAXNETIDLEN) + \
152                                 1 + XDR_QUADLEN(RPCBIND_MAXUADDRLEN))
153 #define encode_setclientid_confirm_maxsz \
154                                 (op_encode_hdr_maxsz + \
155                                 3 + (NFS4_VERIFIER_SIZE >> 2))
156 #define decode_setclientid_confirm_maxsz \
157                                 (op_decode_hdr_maxsz)
158 #define encode_lookup_maxsz     (op_encode_hdr_maxsz + nfs4_name_maxsz)
159 #define decode_lookup_maxsz     (op_decode_hdr_maxsz)
160 #define encode_share_access_maxsz \
161                                 (2)
162 #define encode_createmode_maxsz (1 + encode_attrs_maxsz + encode_verifier_maxsz)
163 #define encode_opentype_maxsz   (1 + encode_createmode_maxsz)
164 #define encode_claim_null_maxsz (1 + nfs4_name_maxsz)
165 #define encode_open_maxsz       (op_encode_hdr_maxsz + \
166                                 2 + encode_share_access_maxsz + 2 + \
167                                 open_owner_id_maxsz + \
168                                 encode_opentype_maxsz + \
169                                 encode_claim_null_maxsz)
170 #define decode_ace_maxsz        (3 + nfs4_owner_maxsz)
171 #define decode_delegation_maxsz (1 + decode_stateid_maxsz + 1 + \
172                                 decode_ace_maxsz)
173 #define decode_change_info_maxsz        (5)
174 #define decode_open_maxsz       (op_decode_hdr_maxsz + \
175                                 decode_stateid_maxsz + \
176                                 decode_change_info_maxsz + 1 + \
177                                 nfs4_fattr_bitmap_maxsz + \
178                                 decode_delegation_maxsz)
179 #define encode_open_confirm_maxsz \
180                                 (op_encode_hdr_maxsz + \
181                                  encode_stateid_maxsz + 1)
182 #define decode_open_confirm_maxsz \
183                                 (op_decode_hdr_maxsz + \
184                                  decode_stateid_maxsz)
185 #define encode_open_downgrade_maxsz \
186                                 (op_encode_hdr_maxsz + \
187                                  encode_stateid_maxsz + 1 + \
188                                  encode_share_access_maxsz)
189 #define decode_open_downgrade_maxsz \
190                                 (op_decode_hdr_maxsz + \
191                                  decode_stateid_maxsz)
192 #define encode_close_maxsz      (op_encode_hdr_maxsz + \
193                                  1 + encode_stateid_maxsz)
194 #define decode_close_maxsz      (op_decode_hdr_maxsz + \
195                                  decode_stateid_maxsz)
196 #define encode_setattr_maxsz    (op_encode_hdr_maxsz + \
197                                  encode_stateid_maxsz + \
198                                  encode_attrs_maxsz)
199 #define decode_setattr_maxsz    (op_decode_hdr_maxsz + \
200                                  nfs4_fattr_bitmap_maxsz)
201 #define encode_read_maxsz       (op_encode_hdr_maxsz + \
202                                  encode_stateid_maxsz + 3)
203 #define decode_read_maxsz       (op_decode_hdr_maxsz + 2)
204 #define encode_readdir_maxsz    (op_encode_hdr_maxsz + \
205                                  2 + encode_verifier_maxsz + 5 + \
206                                 nfs4_label_maxsz)
207 #define decode_readdir_maxsz    (op_decode_hdr_maxsz + \
208                                  decode_verifier_maxsz)
209 #define encode_readlink_maxsz   (op_encode_hdr_maxsz)
210 #define decode_readlink_maxsz   (op_decode_hdr_maxsz + 1)
211 #define encode_write_maxsz      (op_encode_hdr_maxsz + \
212                                  encode_stateid_maxsz + 4)
213 #define decode_write_maxsz      (op_decode_hdr_maxsz + \
214                                  2 + decode_verifier_maxsz)
215 #define encode_commit_maxsz     (op_encode_hdr_maxsz + 3)
216 #define decode_commit_maxsz     (op_decode_hdr_maxsz + \
217                                  decode_verifier_maxsz)
218 #define encode_remove_maxsz     (op_encode_hdr_maxsz + \
219                                 nfs4_name_maxsz)
220 #define decode_remove_maxsz     (op_decode_hdr_maxsz + \
221                                  decode_change_info_maxsz)
222 #define encode_rename_maxsz     (op_encode_hdr_maxsz + \
223                                 2 * nfs4_name_maxsz)
224 #define decode_rename_maxsz     (op_decode_hdr_maxsz + \
225                                  decode_change_info_maxsz + \
226                                  decode_change_info_maxsz)
227 #define encode_link_maxsz       (op_encode_hdr_maxsz + \
228                                 nfs4_name_maxsz)
229 #define decode_link_maxsz       (op_decode_hdr_maxsz + decode_change_info_maxsz)
230 #define encode_lockowner_maxsz  (7)
231 #define encode_lock_maxsz       (op_encode_hdr_maxsz + \
232                                  7 + \
233                                  1 + encode_stateid_maxsz + 1 + \
234                                  encode_lockowner_maxsz)
235 #define decode_lock_denied_maxsz \
236                                 (8 + decode_lockowner_maxsz)
237 #define decode_lock_maxsz       (op_decode_hdr_maxsz + \
238                                  decode_lock_denied_maxsz)
239 #define encode_lockt_maxsz      (op_encode_hdr_maxsz + 5 + \
240                                 encode_lockowner_maxsz)
241 #define decode_lockt_maxsz      (op_decode_hdr_maxsz + \
242                                  decode_lock_denied_maxsz)
243 #define encode_locku_maxsz      (op_encode_hdr_maxsz + 3 + \
244                                  encode_stateid_maxsz + \
245                                  4)
246 #define decode_locku_maxsz      (op_decode_hdr_maxsz + \
247                                  decode_stateid_maxsz)
248 #define encode_release_lockowner_maxsz \
249                                 (op_encode_hdr_maxsz + \
250                                  encode_lockowner_maxsz)
251 #define decode_release_lockowner_maxsz \
252                                 (op_decode_hdr_maxsz)
253 #define encode_access_maxsz     (op_encode_hdr_maxsz + 1)
254 #define decode_access_maxsz     (op_decode_hdr_maxsz + 2)
255 #define encode_symlink_maxsz    (op_encode_hdr_maxsz + \
256                                 1 + nfs4_name_maxsz + \
257                                 1 + \
258                                 nfs4_fattr_maxsz)
259 #define decode_symlink_maxsz    (op_decode_hdr_maxsz + 8)
260 #define encode_create_maxsz     (op_encode_hdr_maxsz + \
261                                 1 + 2 + nfs4_name_maxsz + \
262                                 encode_attrs_maxsz)
263 #define decode_create_maxsz     (op_decode_hdr_maxsz + \
264                                 decode_change_info_maxsz + \
265                                 nfs4_fattr_bitmap_maxsz)
266 #define encode_statfs_maxsz     (encode_getattr_maxsz)
267 #define decode_statfs_maxsz     (decode_getattr_maxsz)
268 #define encode_delegreturn_maxsz (op_encode_hdr_maxsz + 4)
269 #define decode_delegreturn_maxsz (op_decode_hdr_maxsz)
270 #define encode_getacl_maxsz     (encode_getattr_maxsz)
271 #define decode_getacl_maxsz     (op_decode_hdr_maxsz + \
272                                  nfs4_fattr_bitmap_maxsz + 1)
273 #define encode_setacl_maxsz     (op_encode_hdr_maxsz + \
274                                  encode_stateid_maxsz + 3)
275 #define decode_setacl_maxsz     (decode_setattr_maxsz)
276 #define encode_fs_locations_maxsz \
277                                 (encode_getattr_maxsz)
278 #define decode_fs_locations_maxsz \
279                                 (0)
280 #define encode_secinfo_maxsz    (op_encode_hdr_maxsz + nfs4_name_maxsz)
281 #define decode_secinfo_maxsz    (op_decode_hdr_maxsz + 1 + ((NFS_MAX_SECFLAVORS * (16 + GSS_OID_MAX_LEN)) / 4))
282
283 #if defined(CONFIG_NFS_V4_1)
284 #define NFS4_MAX_MACHINE_NAME_LEN (64)
285 #define IMPL_NAME_LIMIT (sizeof(utsname()->sysname) + sizeof(utsname()->release) + \
286                          sizeof(utsname()->version) + sizeof(utsname()->machine) + 8)
287
288 #define encode_exchange_id_maxsz (op_encode_hdr_maxsz + \
289                                 encode_verifier_maxsz + \
290                                 1 /* co_ownerid.len */ + \
291                                 XDR_QUADLEN(NFS4_EXCHANGE_ID_LEN) + \
292                                 1 /* flags */ + \
293                                 1 /* spa_how */ + \
294                                 /* max is SP4_MACH_CRED (for now) */ + \
295                                 1 + NFS4_OP_MAP_NUM_WORDS + \
296                                 1 + NFS4_OP_MAP_NUM_WORDS + \
297                                 1 /* implementation id array of size 1 */ + \
298                                 1 /* nii_domain */ + \
299                                 XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + \
300                                 1 /* nii_name */ + \
301                                 XDR_QUADLEN(IMPL_NAME_LIMIT) + \
302                                 3 /* nii_date */)
303 #define decode_exchange_id_maxsz (op_decode_hdr_maxsz + \
304                                 2 /* eir_clientid */ + \
305                                 1 /* eir_sequenceid */ + \
306                                 1 /* eir_flags */ + \
307                                 1 /* spr_how */ + \
308                                   /* max is SP4_MACH_CRED (for now) */ + \
309                                 1 + NFS4_OP_MAP_NUM_WORDS + \
310                                 1 + NFS4_OP_MAP_NUM_WORDS + \
311                                 2 /* eir_server_owner.so_minor_id */ + \
312                                 /* eir_server_owner.so_major_id<> */ \
313                                 XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 + \
314                                 /* eir_server_scope<> */ \
315                                 XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 + \
316                                 1 /* eir_server_impl_id array length */ + \
317                                 1 /* nii_domain */ + \
318                                 XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + \
319                                 1 /* nii_name */ + \
320                                 XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + \
321                                 3 /* nii_date */)
322 #define encode_channel_attrs_maxsz  (6 + 1 /* ca_rdma_ird.len (0) */)
323 #define decode_channel_attrs_maxsz  (6 + \
324                                      1 /* ca_rdma_ird.len */ + \
325                                      1 /* ca_rdma_ird */)
326 #define encode_create_session_maxsz  (op_encode_hdr_maxsz + \
327                                      2 /* csa_clientid */ + \
328                                      1 /* csa_sequence */ + \
329                                      1 /* csa_flags */ + \
330                                      encode_channel_attrs_maxsz + \
331                                      encode_channel_attrs_maxsz + \
332                                      1 /* csa_cb_program */ + \
333                                      1 /* csa_sec_parms.len (1) */ + \
334                                      1 /* cb_secflavor (AUTH_SYS) */ + \
335                                      1 /* stamp */ + \
336                                      1 /* machinename.len */ + \
337                                      XDR_QUADLEN(NFS4_MAX_MACHINE_NAME_LEN) + \
338                                      1 /* uid */ + \
339                                      1 /* gid */ + \
340                                      1 /* gids.len (0) */)
341 #define decode_create_session_maxsz  (op_decode_hdr_maxsz +     \
342                                      XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
343                                      1 /* csr_sequence */ + \
344                                      1 /* csr_flags */ + \
345                                      decode_channel_attrs_maxsz + \
346                                      decode_channel_attrs_maxsz)
347 #define encode_bind_conn_to_session_maxsz  (op_encode_hdr_maxsz + \
348                                      /* bctsa_sessid */ \
349                                      XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
350                                      1 /* bctsa_dir */ + \
351                                      1 /* bctsa_use_conn_in_rdma_mode */)
352 #define decode_bind_conn_to_session_maxsz  (op_decode_hdr_maxsz +       \
353                                      /* bctsr_sessid */ \
354                                      XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
355                                      1 /* bctsr_dir */ + \
356                                      1 /* bctsr_use_conn_in_rdma_mode */)
357 #define encode_destroy_session_maxsz    (op_encode_hdr_maxsz + 4)
358 #define decode_destroy_session_maxsz    (op_decode_hdr_maxsz)
359 #define encode_destroy_clientid_maxsz   (op_encode_hdr_maxsz + 2)
360 #define decode_destroy_clientid_maxsz   (op_decode_hdr_maxsz)
361 #define encode_sequence_maxsz   (op_encode_hdr_maxsz + \
362                                 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + 4)
363 #define decode_sequence_maxsz   (op_decode_hdr_maxsz + \
364                                 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + 5)
365 #define encode_reclaim_complete_maxsz   (op_encode_hdr_maxsz + 4)
366 #define decode_reclaim_complete_maxsz   (op_decode_hdr_maxsz + 4)
367 #define encode_getdeviceinfo_maxsz (op_encode_hdr_maxsz + \
368                                 XDR_QUADLEN(NFS4_DEVICEID4_SIZE) + \
369                                 1 /* layout type */ + \
370                                 1 /* maxcount */ + \
371                                 1 /* bitmap size */ + \
372                                 1 /* notification bitmap length */ + \
373                                 1 /* notification bitmap, word 0 */)
374 #define decode_getdeviceinfo_maxsz (op_decode_hdr_maxsz + \
375                                 1 /* layout type */ + \
376                                 1 /* opaque devaddr4 length */ + \
377                                   /* devaddr4 payload is read into page */ \
378                                 1 /* notification bitmap length */ + \
379                                 1 /* notification bitmap, word 0 */)
380 #define encode_layoutget_maxsz  (op_encode_hdr_maxsz + 10 + \
381                                 encode_stateid_maxsz)
382 #define decode_layoutget_maxsz  (op_decode_hdr_maxsz + 8 + \
383                                 decode_stateid_maxsz + \
384                                 XDR_QUADLEN(PNFS_LAYOUT_MAXSIZE))
385 #define encode_layoutcommit_maxsz (op_encode_hdr_maxsz +          \
386                                 2 /* offset */ + \
387                                 2 /* length */ + \
388                                 1 /* reclaim */ + \
389                                 encode_stateid_maxsz + \
390                                 1 /* new offset (true) */ + \
391                                 2 /* last byte written */ + \
392                                 1 /* nt_timechanged (false) */ + \
393                                 1 /* layoutupdate4 layout type */ + \
394                                 1 /* layoutupdate4 opaqueue len */)
395                                   /* the actual content of layoutupdate4 should
396                                      be allocated by drivers and spliced in
397                                      using xdr_write_pages */
398 #define decode_layoutcommit_maxsz (op_decode_hdr_maxsz + 3)
399 #define encode_layoutreturn_maxsz (8 + op_encode_hdr_maxsz + \
400                                 encode_stateid_maxsz + \
401                                 1 /* FIXME: opaque lrf_body always empty at the moment */)
402 #define decode_layoutreturn_maxsz (op_decode_hdr_maxsz + \
403                                 1 + decode_stateid_maxsz)
404 #define encode_secinfo_no_name_maxsz (op_encode_hdr_maxsz + 1)
405 #define decode_secinfo_no_name_maxsz decode_secinfo_maxsz
406 #define encode_test_stateid_maxsz       (op_encode_hdr_maxsz + 2 + \
407                                          XDR_QUADLEN(NFS4_STATEID_SIZE))
408 #define decode_test_stateid_maxsz       (op_decode_hdr_maxsz + 2 + 1)
409 #define encode_free_stateid_maxsz       (op_encode_hdr_maxsz + 1 + \
410                                          XDR_QUADLEN(NFS4_STATEID_SIZE))
411 #define decode_free_stateid_maxsz       (op_decode_hdr_maxsz)
412 #else /* CONFIG_NFS_V4_1 */
413 #define encode_sequence_maxsz   0
414 #define decode_sequence_maxsz   0
415 #endif /* CONFIG_NFS_V4_1 */
416
417 #define NFS4_enc_compound_sz    (1024)  /* XXX: large enough? */
418 #define NFS4_dec_compound_sz    (1024)  /* XXX: large enough? */
419 #define NFS4_enc_read_sz        (compound_encode_hdr_maxsz + \
420                                 encode_sequence_maxsz + \
421                                 encode_putfh_maxsz + \
422                                 encode_read_maxsz)
423 #define NFS4_dec_read_sz        (compound_decode_hdr_maxsz + \
424                                 decode_sequence_maxsz + \
425                                 decode_putfh_maxsz + \
426                                 decode_read_maxsz)
427 #define NFS4_enc_readlink_sz    (compound_encode_hdr_maxsz + \
428                                 encode_sequence_maxsz + \
429                                 encode_putfh_maxsz + \
430                                 encode_readlink_maxsz)
431 #define NFS4_dec_readlink_sz    (compound_decode_hdr_maxsz + \
432                                 decode_sequence_maxsz + \
433                                 decode_putfh_maxsz + \
434                                 decode_readlink_maxsz)
435 #define NFS4_enc_readdir_sz     (compound_encode_hdr_maxsz + \
436                                 encode_sequence_maxsz + \
437                                 encode_putfh_maxsz + \
438                                 encode_readdir_maxsz)
439 #define NFS4_dec_readdir_sz     (compound_decode_hdr_maxsz + \
440                                 decode_sequence_maxsz + \
441                                 decode_putfh_maxsz + \
442                                 decode_readdir_maxsz)
443 #define NFS4_enc_write_sz       (compound_encode_hdr_maxsz + \
444                                 encode_sequence_maxsz + \
445                                 encode_putfh_maxsz + \
446                                 encode_write_maxsz + \
447                                 encode_getattr_maxsz)
448 #define NFS4_dec_write_sz       (compound_decode_hdr_maxsz + \
449                                 decode_sequence_maxsz + \
450                                 decode_putfh_maxsz + \
451                                 decode_write_maxsz + \
452                                 decode_getattr_maxsz)
453 #define NFS4_enc_commit_sz      (compound_encode_hdr_maxsz + \
454                                 encode_sequence_maxsz + \
455                                 encode_putfh_maxsz + \
456                                 encode_commit_maxsz)
457 #define NFS4_dec_commit_sz      (compound_decode_hdr_maxsz + \
458                                 decode_sequence_maxsz + \
459                                 decode_putfh_maxsz + \
460                                 decode_commit_maxsz)
461 #define NFS4_enc_open_sz        (compound_encode_hdr_maxsz + \
462                                 encode_sequence_maxsz + \
463                                 encode_putfh_maxsz + \
464                                 encode_open_maxsz + \
465                                 encode_access_maxsz + \
466                                 encode_getfh_maxsz + \
467                                 encode_getattr_maxsz)
468 #define NFS4_dec_open_sz        (compound_decode_hdr_maxsz + \
469                                 decode_sequence_maxsz + \
470                                 decode_putfh_maxsz + \
471                                 decode_open_maxsz + \
472                                 decode_access_maxsz + \
473                                 decode_getfh_maxsz + \
474                                 decode_getattr_maxsz)
475 #define NFS4_enc_open_confirm_sz \
476                                 (compound_encode_hdr_maxsz + \
477                                  encode_putfh_maxsz + \
478                                  encode_open_confirm_maxsz)
479 #define NFS4_dec_open_confirm_sz \
480                                 (compound_decode_hdr_maxsz + \
481                                  decode_putfh_maxsz + \
482                                  decode_open_confirm_maxsz)
483 #define NFS4_enc_open_noattr_sz (compound_encode_hdr_maxsz + \
484                                         encode_sequence_maxsz + \
485                                         encode_putfh_maxsz + \
486                                         encode_open_maxsz + \
487                                         encode_access_maxsz + \
488                                         encode_getattr_maxsz)
489 #define NFS4_dec_open_noattr_sz (compound_decode_hdr_maxsz + \
490                                         decode_sequence_maxsz + \
491                                         decode_putfh_maxsz + \
492                                         decode_open_maxsz + \
493                                         decode_access_maxsz + \
494                                         decode_getattr_maxsz)
495 #define NFS4_enc_open_downgrade_sz \
496                                 (compound_encode_hdr_maxsz + \
497                                  encode_sequence_maxsz + \
498                                  encode_putfh_maxsz + \
499                                  encode_open_downgrade_maxsz + \
500                                  encode_getattr_maxsz)
501 #define NFS4_dec_open_downgrade_sz \
502                                 (compound_decode_hdr_maxsz + \
503                                  decode_sequence_maxsz + \
504                                  decode_putfh_maxsz + \
505                                  decode_open_downgrade_maxsz + \
506                                  decode_getattr_maxsz)
507 #define NFS4_enc_close_sz       (compound_encode_hdr_maxsz + \
508                                  encode_sequence_maxsz + \
509                                  encode_putfh_maxsz + \
510                                  encode_close_maxsz + \
511                                  encode_getattr_maxsz)
512 #define NFS4_dec_close_sz       (compound_decode_hdr_maxsz + \
513                                  decode_sequence_maxsz + \
514                                  decode_putfh_maxsz + \
515                                  decode_close_maxsz + \
516                                  decode_getattr_maxsz)
517 #define NFS4_enc_setattr_sz     (compound_encode_hdr_maxsz + \
518                                  encode_sequence_maxsz + \
519                                  encode_putfh_maxsz + \
520                                  encode_setattr_maxsz + \
521                                  encode_getattr_maxsz)
522 #define NFS4_dec_setattr_sz     (compound_decode_hdr_maxsz + \
523                                  decode_sequence_maxsz + \
524                                  decode_putfh_maxsz + \
525                                  decode_setattr_maxsz + \
526                                  decode_getattr_maxsz)
527 #define NFS4_enc_fsinfo_sz      (compound_encode_hdr_maxsz + \
528                                 encode_sequence_maxsz + \
529                                 encode_putfh_maxsz + \
530                                 encode_fsinfo_maxsz)
531 #define NFS4_dec_fsinfo_sz      (compound_decode_hdr_maxsz + \
532                                 decode_sequence_maxsz + \
533                                 decode_putfh_maxsz + \
534                                 decode_fsinfo_maxsz)
535 #define NFS4_enc_renew_sz       (compound_encode_hdr_maxsz + \
536                                 encode_renew_maxsz)
537 #define NFS4_dec_renew_sz       (compound_decode_hdr_maxsz + \
538                                 decode_renew_maxsz)
539 #define NFS4_enc_setclientid_sz (compound_encode_hdr_maxsz + \
540                                 encode_setclientid_maxsz)
541 #define NFS4_dec_setclientid_sz (compound_decode_hdr_maxsz + \
542                                 decode_setclientid_maxsz)
543 #define NFS4_enc_setclientid_confirm_sz \
544                                 (compound_encode_hdr_maxsz + \
545                                 encode_setclientid_confirm_maxsz)
546 #define NFS4_dec_setclientid_confirm_sz \
547                                 (compound_decode_hdr_maxsz + \
548                                 decode_setclientid_confirm_maxsz)
549 #define NFS4_enc_lock_sz        (compound_encode_hdr_maxsz + \
550                                 encode_sequence_maxsz + \
551                                 encode_putfh_maxsz + \
552                                 encode_lock_maxsz)
553 #define NFS4_dec_lock_sz        (compound_decode_hdr_maxsz + \
554                                 decode_sequence_maxsz + \
555                                 decode_putfh_maxsz + \
556                                 decode_lock_maxsz)
557 #define NFS4_enc_lockt_sz       (compound_encode_hdr_maxsz + \
558                                 encode_sequence_maxsz + \
559                                 encode_putfh_maxsz + \
560                                 encode_lockt_maxsz)
561 #define NFS4_dec_lockt_sz       (compound_decode_hdr_maxsz + \
562                                  decode_sequence_maxsz + \
563                                  decode_putfh_maxsz + \
564                                  decode_lockt_maxsz)
565 #define NFS4_enc_locku_sz       (compound_encode_hdr_maxsz + \
566                                 encode_sequence_maxsz + \
567                                 encode_putfh_maxsz + \
568                                 encode_locku_maxsz)
569 #define NFS4_dec_locku_sz       (compound_decode_hdr_maxsz + \
570                                 decode_sequence_maxsz + \
571                                 decode_putfh_maxsz + \
572                                 decode_locku_maxsz)
573 #define NFS4_enc_release_lockowner_sz \
574                                 (compound_encode_hdr_maxsz + \
575                                  encode_lockowner_maxsz)
576 #define NFS4_dec_release_lockowner_sz \
577                                 (compound_decode_hdr_maxsz + \
578                                  decode_lockowner_maxsz)
579 #define NFS4_enc_access_sz      (compound_encode_hdr_maxsz + \
580                                 encode_sequence_maxsz + \
581                                 encode_putfh_maxsz + \
582                                 encode_access_maxsz + \
583                                 encode_getattr_maxsz)
584 #define NFS4_dec_access_sz      (compound_decode_hdr_maxsz + \
585                                 decode_sequence_maxsz + \
586                                 decode_putfh_maxsz + \
587                                 decode_access_maxsz + \
588                                 decode_getattr_maxsz)
589 #define NFS4_enc_getattr_sz     (compound_encode_hdr_maxsz + \
590                                 encode_sequence_maxsz + \
591                                 encode_putfh_maxsz + \
592                                 encode_getattr_maxsz + \
593                                 encode_renew_maxsz)
594 #define NFS4_dec_getattr_sz     (compound_decode_hdr_maxsz + \
595                                 decode_sequence_maxsz + \
596                                 decode_putfh_maxsz + \
597                                 decode_getattr_maxsz + \
598                                 decode_renew_maxsz)
599 #define NFS4_enc_lookup_sz      (compound_encode_hdr_maxsz + \
600                                 encode_sequence_maxsz + \
601                                 encode_putfh_maxsz + \
602                                 encode_lookup_maxsz + \
603                                 encode_getattr_maxsz + \
604                                 encode_getfh_maxsz)
605 #define NFS4_dec_lookup_sz      (compound_decode_hdr_maxsz + \
606                                 decode_sequence_maxsz + \
607                                 decode_putfh_maxsz + \
608                                 decode_lookup_maxsz + \
609                                 decode_getattr_maxsz + \
610                                 decode_getfh_maxsz)
611 #define NFS4_enc_lookup_root_sz (compound_encode_hdr_maxsz + \
612                                 encode_sequence_maxsz + \
613                                 encode_putrootfh_maxsz + \
614                                 encode_getattr_maxsz + \
615                                 encode_getfh_maxsz)
616 #define NFS4_dec_lookup_root_sz (compound_decode_hdr_maxsz + \
617                                 decode_sequence_maxsz + \
618                                 decode_putrootfh_maxsz + \
619                                 decode_getattr_maxsz + \
620                                 decode_getfh_maxsz)
621 #define NFS4_enc_remove_sz      (compound_encode_hdr_maxsz + \
622                                 encode_sequence_maxsz + \
623                                 encode_putfh_maxsz + \
624                                 encode_remove_maxsz)
625 #define NFS4_dec_remove_sz      (compound_decode_hdr_maxsz + \
626                                 decode_sequence_maxsz + \
627                                 decode_putfh_maxsz + \
628                                 decode_remove_maxsz)
629 #define NFS4_enc_rename_sz      (compound_encode_hdr_maxsz + \
630                                 encode_sequence_maxsz + \
631                                 encode_putfh_maxsz + \
632                                 encode_savefh_maxsz + \
633                                 encode_putfh_maxsz + \
634                                 encode_rename_maxsz)
635 #define NFS4_dec_rename_sz      (compound_decode_hdr_maxsz + \
636                                 decode_sequence_maxsz + \
637                                 decode_putfh_maxsz + \
638                                 decode_savefh_maxsz + \
639                                 decode_putfh_maxsz + \
640                                 decode_rename_maxsz)
641 #define NFS4_enc_link_sz        (compound_encode_hdr_maxsz + \
642                                 encode_sequence_maxsz + \
643                                 encode_putfh_maxsz + \
644                                 encode_savefh_maxsz + \
645                                 encode_putfh_maxsz + \
646                                 encode_link_maxsz + \
647                                 encode_restorefh_maxsz + \
648                                 encode_getattr_maxsz)
649 #define NFS4_dec_link_sz        (compound_decode_hdr_maxsz + \
650                                 decode_sequence_maxsz + \
651                                 decode_putfh_maxsz + \
652                                 decode_savefh_maxsz + \
653                                 decode_putfh_maxsz + \
654                                 decode_link_maxsz + \
655                                 decode_restorefh_maxsz + \
656                                 decode_getattr_maxsz)
657 #define NFS4_enc_symlink_sz     (compound_encode_hdr_maxsz + \
658                                 encode_sequence_maxsz + \
659                                 encode_putfh_maxsz + \
660                                 encode_symlink_maxsz + \
661                                 encode_getattr_maxsz + \
662                                 encode_getfh_maxsz)
663 #define NFS4_dec_symlink_sz     (compound_decode_hdr_maxsz + \
664                                 decode_sequence_maxsz + \
665                                 decode_putfh_maxsz + \
666                                 decode_symlink_maxsz + \
667                                 decode_getattr_maxsz + \
668                                 decode_getfh_maxsz)
669 #define NFS4_enc_create_sz      (compound_encode_hdr_maxsz + \
670                                 encode_sequence_maxsz + \
671                                 encode_putfh_maxsz + \
672                                 encode_create_maxsz + \
673                                 encode_getfh_maxsz + \
674                                 encode_getattr_maxsz)
675 #define NFS4_dec_create_sz      (compound_decode_hdr_maxsz + \
676                                 decode_sequence_maxsz + \
677                                 decode_putfh_maxsz + \
678                                 decode_create_maxsz + \
679                                 decode_getfh_maxsz + \
680                                 decode_getattr_maxsz)
681 #define NFS4_enc_pathconf_sz    (compound_encode_hdr_maxsz + \
682                                 encode_sequence_maxsz + \
683                                 encode_putfh_maxsz + \
684                                 encode_getattr_maxsz)
685 #define NFS4_dec_pathconf_sz    (compound_decode_hdr_maxsz + \
686                                 decode_sequence_maxsz + \
687                                 decode_putfh_maxsz + \
688                                 decode_getattr_maxsz)
689 #define NFS4_enc_statfs_sz      (compound_encode_hdr_maxsz + \
690                                 encode_sequence_maxsz + \
691                                 encode_putfh_maxsz + \
692                                 encode_statfs_maxsz)
693 #define NFS4_dec_statfs_sz      (compound_decode_hdr_maxsz + \
694                                 decode_sequence_maxsz + \
695                                 decode_putfh_maxsz + \
696                                 decode_statfs_maxsz)
697 #define NFS4_enc_server_caps_sz (compound_encode_hdr_maxsz + \
698                                 encode_sequence_maxsz + \
699                                 encode_putfh_maxsz + \
700                                 encode_getattr_maxsz)
701 #define NFS4_dec_server_caps_sz (compound_decode_hdr_maxsz + \
702                                 decode_sequence_maxsz + \
703                                 decode_putfh_maxsz + \
704                                 decode_getattr_maxsz)
705 #define NFS4_enc_delegreturn_sz (compound_encode_hdr_maxsz + \
706                                 encode_sequence_maxsz + \
707                                 encode_putfh_maxsz + \
708                                 encode_delegreturn_maxsz + \
709                                 encode_getattr_maxsz)
710 #define NFS4_dec_delegreturn_sz (compound_decode_hdr_maxsz + \
711                                 decode_sequence_maxsz + \
712                                 decode_delegreturn_maxsz + \
713                                 decode_getattr_maxsz)
714 #define NFS4_enc_getacl_sz      (compound_encode_hdr_maxsz + \
715                                 encode_sequence_maxsz + \
716                                 encode_putfh_maxsz + \
717                                 encode_getacl_maxsz)
718 #define NFS4_dec_getacl_sz      (compound_decode_hdr_maxsz + \
719                                 decode_sequence_maxsz + \
720                                 decode_putfh_maxsz + \
721                                 decode_getacl_maxsz)
722 #define NFS4_enc_setacl_sz      (compound_encode_hdr_maxsz + \
723                                 encode_sequence_maxsz + \
724                                 encode_putfh_maxsz + \
725                                 encode_setacl_maxsz)
726 #define NFS4_dec_setacl_sz      (compound_decode_hdr_maxsz + \
727                                 decode_sequence_maxsz + \
728                                 decode_putfh_maxsz + \
729                                 decode_setacl_maxsz)
730 #define NFS4_enc_fs_locations_sz \
731                                 (compound_encode_hdr_maxsz + \
732                                  encode_sequence_maxsz + \
733                                  encode_putfh_maxsz + \
734                                  encode_lookup_maxsz + \
735                                  encode_fs_locations_maxsz + \
736                                  encode_renew_maxsz)
737 #define NFS4_dec_fs_locations_sz \
738                                 (compound_decode_hdr_maxsz + \
739                                  decode_sequence_maxsz + \
740                                  decode_putfh_maxsz + \
741                                  decode_lookup_maxsz + \
742                                  decode_fs_locations_maxsz + \
743                                  decode_renew_maxsz)
744 #define NFS4_enc_secinfo_sz     (compound_encode_hdr_maxsz + \
745                                 encode_sequence_maxsz + \
746                                 encode_putfh_maxsz + \
747                                 encode_secinfo_maxsz)
748 #define NFS4_dec_secinfo_sz     (compound_decode_hdr_maxsz + \
749                                 decode_sequence_maxsz + \
750                                 decode_putfh_maxsz + \
751                                 decode_secinfo_maxsz)
752 #define NFS4_enc_fsid_present_sz \
753                                 (compound_encode_hdr_maxsz + \
754                                  encode_sequence_maxsz + \
755                                  encode_putfh_maxsz + \
756                                  encode_getfh_maxsz + \
757                                  encode_renew_maxsz)
758 #define NFS4_dec_fsid_present_sz \
759                                 (compound_decode_hdr_maxsz + \
760                                  decode_sequence_maxsz + \
761                                  decode_putfh_maxsz + \
762                                  decode_getfh_maxsz + \
763                                  decode_renew_maxsz)
764 #if defined(CONFIG_NFS_V4_1)
765 #define NFS4_enc_bind_conn_to_session_sz \
766                                 (compound_encode_hdr_maxsz + \
767                                  encode_bind_conn_to_session_maxsz)
768 #define NFS4_dec_bind_conn_to_session_sz \
769                                 (compound_decode_hdr_maxsz + \
770                                  decode_bind_conn_to_session_maxsz)
771 #define NFS4_enc_exchange_id_sz \
772                                 (compound_encode_hdr_maxsz + \
773                                  encode_exchange_id_maxsz)
774 #define NFS4_dec_exchange_id_sz \
775                                 (compound_decode_hdr_maxsz + \
776                                  decode_exchange_id_maxsz)
777 #define NFS4_enc_create_session_sz \
778                                 (compound_encode_hdr_maxsz + \
779                                  encode_create_session_maxsz)
780 #define NFS4_dec_create_session_sz \
781                                 (compound_decode_hdr_maxsz + \
782                                  decode_create_session_maxsz)
783 #define NFS4_enc_destroy_session_sz     (compound_encode_hdr_maxsz + \
784                                          encode_destroy_session_maxsz)
785 #define NFS4_dec_destroy_session_sz     (compound_decode_hdr_maxsz + \
786                                          decode_destroy_session_maxsz)
787 #define NFS4_enc_destroy_clientid_sz    (compound_encode_hdr_maxsz + \
788                                          encode_destroy_clientid_maxsz)
789 #define NFS4_dec_destroy_clientid_sz    (compound_decode_hdr_maxsz + \
790                                          decode_destroy_clientid_maxsz)
791 #define NFS4_enc_sequence_sz \
792                                 (compound_decode_hdr_maxsz + \
793                                  encode_sequence_maxsz)
794 #define NFS4_dec_sequence_sz \
795                                 (compound_decode_hdr_maxsz + \
796                                  decode_sequence_maxsz)
797 #define NFS4_enc_get_lease_time_sz      (compound_encode_hdr_maxsz + \
798                                          encode_sequence_maxsz + \
799                                          encode_putrootfh_maxsz + \
800                                          encode_fsinfo_maxsz)
801 #define NFS4_dec_get_lease_time_sz      (compound_decode_hdr_maxsz + \
802                                          decode_sequence_maxsz + \
803                                          decode_putrootfh_maxsz + \
804                                          decode_fsinfo_maxsz)
805 #define NFS4_enc_reclaim_complete_sz    (compound_encode_hdr_maxsz + \
806                                          encode_sequence_maxsz + \
807                                          encode_reclaim_complete_maxsz)
808 #define NFS4_dec_reclaim_complete_sz    (compound_decode_hdr_maxsz + \
809                                          decode_sequence_maxsz + \
810                                          decode_reclaim_complete_maxsz)
811 #define NFS4_enc_getdeviceinfo_sz (compound_encode_hdr_maxsz +    \
812                                 encode_sequence_maxsz +\
813                                 encode_getdeviceinfo_maxsz)
814 #define NFS4_dec_getdeviceinfo_sz (compound_decode_hdr_maxsz +    \
815                                 decode_sequence_maxsz + \
816                                 decode_getdeviceinfo_maxsz)
817 #define NFS4_enc_layoutget_sz   (compound_encode_hdr_maxsz + \
818                                 encode_sequence_maxsz + \
819                                 encode_putfh_maxsz +        \
820                                 encode_layoutget_maxsz)
821 #define NFS4_dec_layoutget_sz   (compound_decode_hdr_maxsz + \
822                                 decode_sequence_maxsz + \
823                                 decode_putfh_maxsz +        \
824                                 decode_layoutget_maxsz)
825 #define NFS4_enc_layoutcommit_sz (compound_encode_hdr_maxsz + \
826                                 encode_sequence_maxsz +\
827                                 encode_putfh_maxsz + \
828                                 encode_layoutcommit_maxsz + \
829                                 encode_getattr_maxsz)
830 #define NFS4_dec_layoutcommit_sz (compound_decode_hdr_maxsz + \
831                                 decode_sequence_maxsz + \
832                                 decode_putfh_maxsz + \
833                                 decode_layoutcommit_maxsz + \
834                                 decode_getattr_maxsz)
835 #define NFS4_enc_layoutreturn_sz (compound_encode_hdr_maxsz + \
836                                 encode_sequence_maxsz + \
837                                 encode_putfh_maxsz + \
838                                 encode_layoutreturn_maxsz)
839 #define NFS4_dec_layoutreturn_sz (compound_decode_hdr_maxsz + \
840                                 decode_sequence_maxsz + \
841                                 decode_putfh_maxsz + \
842                                 decode_layoutreturn_maxsz)
843 #define NFS4_enc_secinfo_no_name_sz     (compound_encode_hdr_maxsz + \
844                                         encode_sequence_maxsz + \
845                                         encode_putrootfh_maxsz +\
846                                         encode_secinfo_no_name_maxsz)
847 #define NFS4_dec_secinfo_no_name_sz     (compound_decode_hdr_maxsz + \
848                                         decode_sequence_maxsz + \
849                                         decode_putrootfh_maxsz + \
850                                         decode_secinfo_no_name_maxsz)
851 #define NFS4_enc_test_stateid_sz        (compound_encode_hdr_maxsz + \
852                                          encode_sequence_maxsz + \
853                                          encode_test_stateid_maxsz)
854 #define NFS4_dec_test_stateid_sz        (compound_decode_hdr_maxsz + \
855                                          decode_sequence_maxsz + \
856                                          decode_test_stateid_maxsz)
857 #define NFS4_enc_free_stateid_sz        (compound_encode_hdr_maxsz + \
858                                          encode_sequence_maxsz + \
859                                          encode_free_stateid_maxsz)
860 #define NFS4_dec_free_stateid_sz        (compound_decode_hdr_maxsz + \
861                                          decode_sequence_maxsz + \
862                                          decode_free_stateid_maxsz)
863
864 const u32 nfs41_maxwrite_overhead = ((RPC_MAX_HEADER_WITH_AUTH +
865                                       compound_encode_hdr_maxsz +
866                                       encode_sequence_maxsz +
867                                       encode_putfh_maxsz +
868                                       encode_getattr_maxsz) *
869                                      XDR_UNIT);
870
871 const u32 nfs41_maxread_overhead = ((RPC_MAX_HEADER_WITH_AUTH +
872                                      compound_decode_hdr_maxsz +
873                                      decode_sequence_maxsz +
874                                      decode_putfh_maxsz) *
875                                     XDR_UNIT);
876
877 const u32 nfs41_maxgetdevinfo_overhead = ((RPC_MAX_REPHEADER_WITH_AUTH +
878                                            compound_decode_hdr_maxsz +
879                                            decode_sequence_maxsz) *
880                                           XDR_UNIT);
881 EXPORT_SYMBOL_GPL(nfs41_maxgetdevinfo_overhead);
882 #endif /* CONFIG_NFS_V4_1 */
883
884 static const umode_t nfs_type2fmt[] = {
885         [NF4BAD] = 0,
886         [NF4REG] = S_IFREG,
887         [NF4DIR] = S_IFDIR,
888         [NF4BLK] = S_IFBLK,
889         [NF4CHR] = S_IFCHR,
890         [NF4LNK] = S_IFLNK,
891         [NF4SOCK] = S_IFSOCK,
892         [NF4FIFO] = S_IFIFO,
893         [NF4ATTRDIR] = 0,
894         [NF4NAMEDATTR] = 0,
895 };
896
897 struct compound_hdr {
898         int32_t         status;
899         uint32_t        nops;
900         __be32 *        nops_p;
901         uint32_t        taglen;
902         char *          tag;
903         uint32_t        replen;         /* expected reply words */
904         u32             minorversion;
905 };
906
907 static __be32 *reserve_space(struct xdr_stream *xdr, size_t nbytes)
908 {
909         __be32 *p = xdr_reserve_space(xdr, nbytes);
910         BUG_ON(!p);
911         return p;
912 }
913
914 static void encode_opaque_fixed(struct xdr_stream *xdr, const void *buf, size_t len)
915 {
916         __be32 *p;
917
918         p = xdr_reserve_space(xdr, len);
919         xdr_encode_opaque_fixed(p, buf, len);
920 }
921
922 static void encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
923 {
924         __be32 *p;
925
926         p = reserve_space(xdr, 4 + len);
927         xdr_encode_opaque(p, str, len);
928 }
929
930 static void encode_uint32(struct xdr_stream *xdr, u32 n)
931 {
932         __be32 *p;
933
934         p = reserve_space(xdr, 4);
935         *p = cpu_to_be32(n);
936 }
937
938 static void encode_uint64(struct xdr_stream *xdr, u64 n)
939 {
940         __be32 *p;
941
942         p = reserve_space(xdr, 8);
943         xdr_encode_hyper(p, n);
944 }
945
946 static void encode_nfs4_seqid(struct xdr_stream *xdr,
947                 const struct nfs_seqid *seqid)
948 {
949         if (seqid != NULL)
950                 encode_uint32(xdr, seqid->sequence->counter);
951         else
952                 encode_uint32(xdr, 0);
953 }
954
955 static void encode_compound_hdr(struct xdr_stream *xdr,
956                                 struct rpc_rqst *req,
957                                 struct compound_hdr *hdr)
958 {
959         __be32 *p;
960         struct rpc_auth *auth = req->rq_cred->cr_auth;
961
962         /* initialize running count of expected bytes in reply.
963          * NOTE: the replied tag SHOULD be the same is the one sent,
964          * but this is not required as a MUST for the server to do so. */
965         hdr->replen = RPC_REPHDRSIZE + auth->au_rslack + 3 + hdr->taglen;
966
967         WARN_ON_ONCE(hdr->taglen > NFS4_MAXTAGLEN);
968         encode_string(xdr, hdr->taglen, hdr->tag);
969         p = reserve_space(xdr, 8);
970         *p++ = cpu_to_be32(hdr->minorversion);
971         hdr->nops_p = p;
972         *p = cpu_to_be32(hdr->nops);
973 }
974
975 static void encode_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 op,
976                 uint32_t replen,
977                 struct compound_hdr *hdr)
978 {
979         encode_uint32(xdr, op);
980         hdr->nops++;
981         hdr->replen += replen;
982 }
983
984 static void encode_nops(struct compound_hdr *hdr)
985 {
986         WARN_ON_ONCE(hdr->nops > NFS4_MAX_OPS);
987         *hdr->nops_p = htonl(hdr->nops);
988 }
989
990 static void encode_nfs4_stateid(struct xdr_stream *xdr, const nfs4_stateid *stateid)
991 {
992         encode_opaque_fixed(xdr, stateid, NFS4_STATEID_SIZE);
993 }
994
995 static void encode_nfs4_verifier(struct xdr_stream *xdr, const nfs4_verifier *verf)
996 {
997         encode_opaque_fixed(xdr, verf->data, NFS4_VERIFIER_SIZE);
998 }
999
1000 static void encode_attrs(struct xdr_stream *xdr, const struct iattr *iap,
1001                                 const struct nfs4_label *label,
1002                                 const struct nfs_server *server)
1003 {
1004         char owner_name[IDMAP_NAMESZ];
1005         char owner_group[IDMAP_NAMESZ];
1006         int owner_namelen = 0;
1007         int owner_grouplen = 0;
1008         __be32 *p;
1009         unsigned i;
1010         uint32_t len = 0;
1011         uint32_t bmval_len;
1012         uint32_t bmval[3] = { 0 };
1013
1014         /*
1015          * We reserve enough space to write the entire attribute buffer at once.
1016          * In the worst-case, this would be
1017          * 16(bitmap) + 4(attrlen) + 8(size) + 4(mode) + 4(atime) + 4(mtime)
1018          * = 40 bytes, plus any contribution from variable-length fields
1019          *            such as owner/group.
1020          */
1021         if (iap->ia_valid & ATTR_SIZE) {
1022                 bmval[0] |= FATTR4_WORD0_SIZE;
1023                 len += 8;
1024         }
1025         if (iap->ia_valid & ATTR_MODE) {
1026                 bmval[1] |= FATTR4_WORD1_MODE;
1027                 len += 4;
1028         }
1029         if (iap->ia_valid & ATTR_UID) {
1030                 owner_namelen = nfs_map_uid_to_name(server, iap->ia_uid, owner_name, IDMAP_NAMESZ);
1031                 if (owner_namelen < 0) {
1032                         dprintk("nfs: couldn't resolve uid %d to string\n",
1033                                         from_kuid(&init_user_ns, iap->ia_uid));
1034                         /* XXX */
1035                         strcpy(owner_name, "nobody");
1036                         owner_namelen = sizeof("nobody") - 1;
1037                         /* goto out; */
1038                 }
1039                 bmval[1] |= FATTR4_WORD1_OWNER;
1040                 len += 4 + (XDR_QUADLEN(owner_namelen) << 2);
1041         }
1042         if (iap->ia_valid & ATTR_GID) {
1043                 owner_grouplen = nfs_map_gid_to_group(server, iap->ia_gid, owner_group, IDMAP_NAMESZ);
1044                 if (owner_grouplen < 0) {
1045                         dprintk("nfs: couldn't resolve gid %d to string\n",
1046                                         from_kgid(&init_user_ns, iap->ia_gid));
1047                         strcpy(owner_group, "nobody");
1048                         owner_grouplen = sizeof("nobody") - 1;
1049                         /* goto out; */
1050                 }
1051                 bmval[1] |= FATTR4_WORD1_OWNER_GROUP;
1052                 len += 4 + (XDR_QUADLEN(owner_grouplen) << 2);
1053         }
1054         if (iap->ia_valid & ATTR_ATIME_SET) {
1055                 bmval[1] |= FATTR4_WORD1_TIME_ACCESS_SET;
1056                 len += 16;
1057         } else if (iap->ia_valid & ATTR_ATIME) {
1058                 bmval[1] |= FATTR4_WORD1_TIME_ACCESS_SET;
1059                 len += 4;
1060         }
1061         if (iap->ia_valid & ATTR_MTIME_SET) {
1062                 bmval[1] |= FATTR4_WORD1_TIME_MODIFY_SET;
1063                 len += 16;
1064         } else if (iap->ia_valid & ATTR_MTIME) {
1065                 bmval[1] |= FATTR4_WORD1_TIME_MODIFY_SET;
1066                 len += 4;
1067         }
1068         if (label) {
1069                 len += 4 + 4 + 4 + (XDR_QUADLEN(label->len) << 2);
1070                 bmval[2] |= FATTR4_WORD2_SECURITY_LABEL;
1071         }
1072
1073         if (bmval[2] != 0)
1074                 bmval_len = 3;
1075         else if (bmval[1] != 0)
1076                 bmval_len = 2;
1077         else
1078                 bmval_len = 1;
1079
1080         p = reserve_space(xdr, 4 + (bmval_len << 2) + 4 + len);
1081
1082         *p++ = cpu_to_be32(bmval_len);
1083         for (i = 0; i < bmval_len; i++)
1084                 *p++ = cpu_to_be32(bmval[i]);
1085         *p++ = cpu_to_be32(len);
1086
1087         if (bmval[0] & FATTR4_WORD0_SIZE)
1088                 p = xdr_encode_hyper(p, iap->ia_size);
1089         if (bmval[1] & FATTR4_WORD1_MODE)
1090                 *p++ = cpu_to_be32(iap->ia_mode & S_IALLUGO);
1091         if (bmval[1] & FATTR4_WORD1_OWNER)
1092                 p = xdr_encode_opaque(p, owner_name, owner_namelen);
1093         if (bmval[1] & FATTR4_WORD1_OWNER_GROUP)
1094                 p = xdr_encode_opaque(p, owner_group, owner_grouplen);
1095         if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
1096                 if (iap->ia_valid & ATTR_ATIME_SET) {
1097                         *p++ = cpu_to_be32(NFS4_SET_TO_CLIENT_TIME);
1098                         p = xdr_encode_hyper(p, (s64)iap->ia_atime.tv_sec);
1099                         *p++ = cpu_to_be32(iap->ia_atime.tv_nsec);
1100                 } else
1101                         *p++ = cpu_to_be32(NFS4_SET_TO_SERVER_TIME);
1102         }
1103         if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
1104                 if (iap->ia_valid & ATTR_MTIME_SET) {
1105                         *p++ = cpu_to_be32(NFS4_SET_TO_CLIENT_TIME);
1106                         p = xdr_encode_hyper(p, (s64)iap->ia_mtime.tv_sec);
1107                         *p++ = cpu_to_be32(iap->ia_mtime.tv_nsec);
1108                 } else
1109                         *p++ = cpu_to_be32(NFS4_SET_TO_SERVER_TIME);
1110         }
1111         if (bmval[2] & FATTR4_WORD2_SECURITY_LABEL) {
1112                 *p++ = cpu_to_be32(label->lfs);
1113                 *p++ = cpu_to_be32(label->pi);
1114                 *p++ = cpu_to_be32(label->len);
1115                 p = xdr_encode_opaque_fixed(p, label->label, label->len);
1116         }
1117
1118 /* out: */
1119 }
1120
1121 static void encode_access(struct xdr_stream *xdr, u32 access, struct compound_hdr *hdr)
1122 {
1123         encode_op_hdr(xdr, OP_ACCESS, decode_access_maxsz, hdr);
1124         encode_uint32(xdr, access);
1125 }
1126
1127 static void encode_close(struct xdr_stream *xdr, const struct nfs_closeargs *arg, struct compound_hdr *hdr)
1128 {
1129         encode_op_hdr(xdr, OP_CLOSE, decode_close_maxsz, hdr);
1130         encode_nfs4_seqid(xdr, arg->seqid);
1131         encode_nfs4_stateid(xdr, &arg->stateid);
1132 }
1133
1134 static void encode_commit(struct xdr_stream *xdr, const struct nfs_commitargs *args, struct compound_hdr *hdr)
1135 {
1136         __be32 *p;
1137
1138         encode_op_hdr(xdr, OP_COMMIT, decode_commit_maxsz, hdr);
1139         p = reserve_space(xdr, 12);
1140         p = xdr_encode_hyper(p, args->offset);
1141         *p = cpu_to_be32(args->count);
1142 }
1143
1144 static void encode_create(struct xdr_stream *xdr, const struct nfs4_create_arg *create, struct compound_hdr *hdr)
1145 {
1146         __be32 *p;
1147
1148         encode_op_hdr(xdr, OP_CREATE, decode_create_maxsz, hdr);
1149         encode_uint32(xdr, create->ftype);
1150
1151         switch (create->ftype) {
1152         case NF4LNK:
1153                 p = reserve_space(xdr, 4);
1154                 *p = cpu_to_be32(create->u.symlink.len);
1155                 xdr_write_pages(xdr, create->u.symlink.pages, 0, create->u.symlink.len);
1156                 break;
1157
1158         case NF4BLK: case NF4CHR:
1159                 p = reserve_space(xdr, 8);
1160                 *p++ = cpu_to_be32(create->u.device.specdata1);
1161                 *p = cpu_to_be32(create->u.device.specdata2);
1162                 break;
1163
1164         default:
1165                 break;
1166         }
1167
1168         encode_string(xdr, create->name->len, create->name->name);
1169         encode_attrs(xdr, create->attrs, create->label, create->server);
1170 }
1171
1172 static void encode_getattr_one(struct xdr_stream *xdr, uint32_t bitmap, struct compound_hdr *hdr)
1173 {
1174         __be32 *p;
1175
1176         encode_op_hdr(xdr, OP_GETATTR, decode_getattr_maxsz, hdr);
1177         p = reserve_space(xdr, 8);
1178         *p++ = cpu_to_be32(1);
1179         *p = cpu_to_be32(bitmap);
1180 }
1181
1182 static void encode_getattr_two(struct xdr_stream *xdr, uint32_t bm0, uint32_t bm1, struct compound_hdr *hdr)
1183 {
1184         __be32 *p;
1185
1186         encode_op_hdr(xdr, OP_GETATTR, decode_getattr_maxsz, hdr);
1187         p = reserve_space(xdr, 12);
1188         *p++ = cpu_to_be32(2);
1189         *p++ = cpu_to_be32(bm0);
1190         *p = cpu_to_be32(bm1);
1191 }
1192
1193 static void
1194 encode_getattr_three(struct xdr_stream *xdr,
1195                      uint32_t bm0, uint32_t bm1, uint32_t bm2,
1196                      struct compound_hdr *hdr)
1197 {
1198         __be32 *p;
1199
1200         encode_op_hdr(xdr, OP_GETATTR, decode_getattr_maxsz, hdr);
1201         if (bm2) {
1202                 p = reserve_space(xdr, 16);
1203                 *p++ = cpu_to_be32(3);
1204                 *p++ = cpu_to_be32(bm0);
1205                 *p++ = cpu_to_be32(bm1);
1206                 *p = cpu_to_be32(bm2);
1207         } else if (bm1) {
1208                 p = reserve_space(xdr, 12);
1209                 *p++ = cpu_to_be32(2);
1210                 *p++ = cpu_to_be32(bm0);
1211                 *p = cpu_to_be32(bm1);
1212         } else {
1213                 p = reserve_space(xdr, 8);
1214                 *p++ = cpu_to_be32(1);
1215                 *p = cpu_to_be32(bm0);
1216         }
1217 }
1218
1219 static void encode_getfattr(struct xdr_stream *xdr, const u32* bitmask, struct compound_hdr *hdr)
1220 {
1221         encode_getattr_three(xdr, bitmask[0] & nfs4_fattr_bitmap[0],
1222                            bitmask[1] & nfs4_fattr_bitmap[1],
1223                            bitmask[2] & nfs4_fattr_bitmap[2],
1224                            hdr);
1225 }
1226
1227 static void encode_getfattr_open(struct xdr_stream *xdr, const u32 *bitmask,
1228                                  const u32 *open_bitmap,
1229                                  struct compound_hdr *hdr)
1230 {
1231         encode_getattr_three(xdr,
1232                              bitmask[0] & open_bitmap[0],
1233                              bitmask[1] & open_bitmap[1],
1234                              bitmask[2] & open_bitmap[2],
1235                              hdr);
1236 }
1237
1238 static void encode_fsinfo(struct xdr_stream *xdr, const u32* bitmask, struct compound_hdr *hdr)
1239 {
1240         encode_getattr_three(xdr,
1241                              bitmask[0] & nfs4_fsinfo_bitmap[0],
1242                              bitmask[1] & nfs4_fsinfo_bitmap[1],
1243                              bitmask[2] & nfs4_fsinfo_bitmap[2],
1244                              hdr);
1245 }
1246
1247 static void encode_fs_locations(struct xdr_stream *xdr, const u32* bitmask, struct compound_hdr *hdr)
1248 {
1249         encode_getattr_two(xdr, bitmask[0] & nfs4_fs_locations_bitmap[0],
1250                            bitmask[1] & nfs4_fs_locations_bitmap[1], hdr);
1251 }
1252
1253 static void encode_getfh(struct xdr_stream *xdr, struct compound_hdr *hdr)
1254 {
1255         encode_op_hdr(xdr, OP_GETFH, decode_getfh_maxsz, hdr);
1256 }
1257
1258 static void encode_link(struct xdr_stream *xdr, const struct qstr *name, struct compound_hdr *hdr)
1259 {
1260         encode_op_hdr(xdr, OP_LINK, decode_link_maxsz, hdr);
1261         encode_string(xdr, name->len, name->name);
1262 }
1263
1264 static inline int nfs4_lock_type(struct file_lock *fl, int block)
1265 {
1266         if (fl->fl_type == F_RDLCK)
1267                 return block ? NFS4_READW_LT : NFS4_READ_LT;
1268         return block ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
1269 }
1270
1271 static inline uint64_t nfs4_lock_length(struct file_lock *fl)
1272 {
1273         if (fl->fl_end == OFFSET_MAX)
1274                 return ~(uint64_t)0;
1275         return fl->fl_end - fl->fl_start + 1;
1276 }
1277
1278 static void encode_lockowner(struct xdr_stream *xdr, const struct nfs_lowner *lowner)
1279 {
1280         __be32 *p;
1281
1282         p = reserve_space(xdr, 32);
1283         p = xdr_encode_hyper(p, lowner->clientid);
1284         *p++ = cpu_to_be32(20);
1285         p = xdr_encode_opaque_fixed(p, "lock id:", 8);
1286         *p++ = cpu_to_be32(lowner->s_dev);
1287         xdr_encode_hyper(p, lowner->id);
1288 }
1289
1290 /*
1291  * opcode,type,reclaim,offset,length,new_lock_owner = 32
1292  * open_seqid,open_stateid,lock_seqid,lock_owner.clientid, lock_owner.id = 40
1293  */
1294 static void encode_lock(struct xdr_stream *xdr, const struct nfs_lock_args *args, struct compound_hdr *hdr)
1295 {
1296         __be32 *p;
1297
1298         encode_op_hdr(xdr, OP_LOCK, decode_lock_maxsz, hdr);
1299         p = reserve_space(xdr, 28);
1300         *p++ = cpu_to_be32(nfs4_lock_type(args->fl, args->block));
1301         *p++ = cpu_to_be32(args->reclaim);
1302         p = xdr_encode_hyper(p, args->fl->fl_start);
1303         p = xdr_encode_hyper(p, nfs4_lock_length(args->fl));
1304         *p = cpu_to_be32(args->new_lock_owner);
1305         if (args->new_lock_owner){
1306                 encode_nfs4_seqid(xdr, args->open_seqid);
1307                 encode_nfs4_stateid(xdr, &args->open_stateid);
1308                 encode_nfs4_seqid(xdr, args->lock_seqid);
1309                 encode_lockowner(xdr, &args->lock_owner);
1310         }
1311         else {
1312                 encode_nfs4_stateid(xdr, &args->lock_stateid);
1313                 encode_nfs4_seqid(xdr, args->lock_seqid);
1314         }
1315 }
1316
1317 static void encode_lockt(struct xdr_stream *xdr, const struct nfs_lockt_args *args, struct compound_hdr *hdr)
1318 {
1319         __be32 *p;
1320
1321         encode_op_hdr(xdr, OP_LOCKT, decode_lockt_maxsz, hdr);
1322         p = reserve_space(xdr, 20);
1323         *p++ = cpu_to_be32(nfs4_lock_type(args->fl, 0));
1324         p = xdr_encode_hyper(p, args->fl->fl_start);
1325         p = xdr_encode_hyper(p, nfs4_lock_length(args->fl));
1326         encode_lockowner(xdr, &args->lock_owner);
1327 }
1328
1329 static void encode_locku(struct xdr_stream *xdr, const struct nfs_locku_args *args, struct compound_hdr *hdr)
1330 {
1331         __be32 *p;
1332
1333         encode_op_hdr(xdr, OP_LOCKU, decode_locku_maxsz, hdr);
1334         encode_uint32(xdr, nfs4_lock_type(args->fl, 0));
1335         encode_nfs4_seqid(xdr, args->seqid);
1336         encode_nfs4_stateid(xdr, &args->stateid);
1337         p = reserve_space(xdr, 16);
1338         p = xdr_encode_hyper(p, args->fl->fl_start);
1339         xdr_encode_hyper(p, nfs4_lock_length(args->fl));
1340 }
1341
1342 static void encode_release_lockowner(struct xdr_stream *xdr, const struct nfs_lowner *lowner, struct compound_hdr *hdr)
1343 {
1344         encode_op_hdr(xdr, OP_RELEASE_LOCKOWNER, decode_release_lockowner_maxsz, hdr);
1345         encode_lockowner(xdr, lowner);
1346 }
1347
1348 static void encode_lookup(struct xdr_stream *xdr, const struct qstr *name, struct compound_hdr *hdr)
1349 {
1350         encode_op_hdr(xdr, OP_LOOKUP, decode_lookup_maxsz, hdr);
1351         encode_string(xdr, name->len, name->name);
1352 }
1353
1354 static void encode_share_access(struct xdr_stream *xdr, fmode_t fmode)
1355 {
1356         __be32 *p;
1357
1358         p = reserve_space(xdr, 8);
1359         switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1360         case FMODE_READ:
1361                 *p++ = cpu_to_be32(NFS4_SHARE_ACCESS_READ);
1362                 break;
1363         case FMODE_WRITE:
1364                 *p++ = cpu_to_be32(NFS4_SHARE_ACCESS_WRITE);
1365                 break;
1366         case FMODE_READ|FMODE_WRITE:
1367                 *p++ = cpu_to_be32(NFS4_SHARE_ACCESS_BOTH);
1368                 break;
1369         default:
1370                 *p++ = cpu_to_be32(0);
1371         }
1372         *p = cpu_to_be32(0);            /* for linux, share_deny = 0 always */
1373 }
1374
1375 static inline void encode_openhdr(struct xdr_stream *xdr, const struct nfs_openargs *arg)
1376 {
1377         __be32 *p;
1378  /*
1379  * opcode 4, seqid 4, share_access 4, share_deny 4, clientid 8, ownerlen 4,
1380  * owner 4 = 32
1381  */
1382         encode_nfs4_seqid(xdr, arg->seqid);
1383         encode_share_access(xdr, arg->fmode);
1384         p = reserve_space(xdr, 36);
1385         p = xdr_encode_hyper(p, arg->clientid);
1386         *p++ = cpu_to_be32(24);
1387         p = xdr_encode_opaque_fixed(p, "open id:", 8);
1388         *p++ = cpu_to_be32(arg->server->s_dev);
1389         *p++ = cpu_to_be32(arg->id.uniquifier);
1390         xdr_encode_hyper(p, arg->id.create_time);
1391 }
1392
1393 static inline void encode_createmode(struct xdr_stream *xdr, const struct nfs_openargs *arg)
1394 {
1395         struct iattr dummy;
1396         __be32 *p;
1397
1398         p = reserve_space(xdr, 4);
1399         switch(arg->createmode) {
1400         case NFS4_CREATE_UNCHECKED:
1401                 *p = cpu_to_be32(NFS4_CREATE_UNCHECKED);
1402                 encode_attrs(xdr, arg->u.attrs, arg->label, arg->server);
1403                 break;
1404         case NFS4_CREATE_GUARDED:
1405                 *p = cpu_to_be32(NFS4_CREATE_GUARDED);
1406                 encode_attrs(xdr, arg->u.attrs, arg->label, arg->server);
1407                 break;
1408         case NFS4_CREATE_EXCLUSIVE:
1409                 *p = cpu_to_be32(NFS4_CREATE_EXCLUSIVE);
1410                 encode_nfs4_verifier(xdr, &arg->u.verifier);
1411                 break;
1412         case NFS4_CREATE_EXCLUSIVE4_1:
1413                 *p = cpu_to_be32(NFS4_CREATE_EXCLUSIVE4_1);
1414                 encode_nfs4_verifier(xdr, &arg->u.verifier);
1415                 dummy.ia_valid = 0;
1416                 encode_attrs(xdr, &dummy, arg->label, arg->server);
1417         }
1418 }
1419
1420 static void encode_opentype(struct xdr_stream *xdr, const struct nfs_openargs *arg)
1421 {
1422         __be32 *p;
1423
1424         p = reserve_space(xdr, 4);
1425         switch (arg->open_flags & O_CREAT) {
1426         case 0:
1427                 *p = cpu_to_be32(NFS4_OPEN_NOCREATE);
1428                 break;
1429         default:
1430                 *p = cpu_to_be32(NFS4_OPEN_CREATE);
1431                 encode_createmode(xdr, arg);
1432         }
1433 }
1434
1435 static inline void encode_delegation_type(struct xdr_stream *xdr, fmode_t delegation_type)
1436 {
1437         __be32 *p;
1438
1439         p = reserve_space(xdr, 4);
1440         switch (delegation_type) {
1441         case 0:
1442                 *p = cpu_to_be32(NFS4_OPEN_DELEGATE_NONE);
1443                 break;
1444         case FMODE_READ:
1445                 *p = cpu_to_be32(NFS4_OPEN_DELEGATE_READ);
1446                 break;
1447         case FMODE_WRITE|FMODE_READ:
1448                 *p = cpu_to_be32(NFS4_OPEN_DELEGATE_WRITE);
1449                 break;
1450         default:
1451                 BUG();
1452         }
1453 }
1454
1455 static inline void encode_claim_null(struct xdr_stream *xdr, const struct qstr *name)
1456 {
1457         __be32 *p;
1458
1459         p = reserve_space(xdr, 4);
1460         *p = cpu_to_be32(NFS4_OPEN_CLAIM_NULL);
1461         encode_string(xdr, name->len, name->name);
1462 }
1463
1464 static inline void encode_claim_previous(struct xdr_stream *xdr, fmode_t type)
1465 {
1466         __be32 *p;
1467
1468         p = reserve_space(xdr, 4);
1469         *p = cpu_to_be32(NFS4_OPEN_CLAIM_PREVIOUS);
1470         encode_delegation_type(xdr, type);
1471 }
1472
1473 static inline void encode_claim_delegate_cur(struct xdr_stream *xdr, const struct qstr *name, const nfs4_stateid *stateid)
1474 {
1475         __be32 *p;
1476
1477         p = reserve_space(xdr, 4);
1478         *p = cpu_to_be32(NFS4_OPEN_CLAIM_DELEGATE_CUR);
1479         encode_nfs4_stateid(xdr, stateid);
1480         encode_string(xdr, name->len, name->name);
1481 }
1482
1483 static inline void encode_claim_fh(struct xdr_stream *xdr)
1484 {
1485         __be32 *p;
1486
1487         p = reserve_space(xdr, 4);
1488         *p = cpu_to_be32(NFS4_OPEN_CLAIM_FH);
1489 }
1490
1491 static inline void encode_claim_delegate_cur_fh(struct xdr_stream *xdr, const nfs4_stateid *stateid)
1492 {
1493         __be32 *p;
1494
1495         p = reserve_space(xdr, 4);
1496         *p = cpu_to_be32(NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1497         encode_nfs4_stateid(xdr, stateid);
1498 }
1499
1500 static void encode_open(struct xdr_stream *xdr, const struct nfs_openargs *arg, struct compound_hdr *hdr)
1501 {
1502         encode_op_hdr(xdr, OP_OPEN, decode_open_maxsz, hdr);
1503         encode_openhdr(xdr, arg);
1504         encode_opentype(xdr, arg);
1505         switch (arg->claim) {
1506         case NFS4_OPEN_CLAIM_NULL:
1507                 encode_claim_null(xdr, arg->name);
1508                 break;
1509         case NFS4_OPEN_CLAIM_PREVIOUS:
1510                 encode_claim_previous(xdr, arg->u.delegation_type);
1511                 break;
1512         case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1513                 encode_claim_delegate_cur(xdr, arg->name, &arg->u.delegation);
1514                 break;
1515         case NFS4_OPEN_CLAIM_FH:
1516                 encode_claim_fh(xdr);
1517                 break;
1518         case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1519                 encode_claim_delegate_cur_fh(xdr, &arg->u.delegation);
1520                 break;
1521         default:
1522                 BUG();
1523         }
1524 }
1525
1526 static void encode_open_confirm(struct xdr_stream *xdr, const struct nfs_open_confirmargs *arg, struct compound_hdr *hdr)
1527 {
1528         encode_op_hdr(xdr, OP_OPEN_CONFIRM, decode_open_confirm_maxsz, hdr);
1529         encode_nfs4_stateid(xdr, arg->stateid);
1530         encode_nfs4_seqid(xdr, arg->seqid);
1531 }
1532
1533 static void encode_open_downgrade(struct xdr_stream *xdr, const struct nfs_closeargs *arg, struct compound_hdr *hdr)
1534 {
1535         encode_op_hdr(xdr, OP_OPEN_DOWNGRADE, decode_open_downgrade_maxsz, hdr);
1536         encode_nfs4_stateid(xdr, &arg->stateid);
1537         encode_nfs4_seqid(xdr, arg->seqid);
1538         encode_share_access(xdr, arg->fmode);
1539 }
1540
1541 static void
1542 encode_putfh(struct xdr_stream *xdr, const struct nfs_fh *fh, struct compound_hdr *hdr)
1543 {
1544         encode_op_hdr(xdr, OP_PUTFH, decode_putfh_maxsz, hdr);
1545         encode_string(xdr, fh->size, fh->data);
1546 }
1547
1548 static void encode_putrootfh(struct xdr_stream *xdr, struct compound_hdr *hdr)
1549 {
1550         encode_op_hdr(xdr, OP_PUTROOTFH, decode_putrootfh_maxsz, hdr);
1551 }
1552
1553 static void encode_read(struct xdr_stream *xdr, const struct nfs_pgio_args *args,
1554                         struct compound_hdr *hdr)
1555 {
1556         __be32 *p;
1557
1558         encode_op_hdr(xdr, OP_READ, decode_read_maxsz, hdr);
1559         encode_nfs4_stateid(xdr, &args->stateid);
1560
1561         p = reserve_space(xdr, 12);
1562         p = xdr_encode_hyper(p, args->offset);
1563         *p = cpu_to_be32(args->count);
1564 }
1565
1566 static void encode_readdir(struct xdr_stream *xdr, const struct nfs4_readdir_arg *readdir, struct rpc_rqst *req, struct compound_hdr *hdr)
1567 {
1568         uint32_t attrs[3] = {
1569                 FATTR4_WORD0_RDATTR_ERROR,
1570                 FATTR4_WORD1_MOUNTED_ON_FILEID,
1571         };
1572         uint32_t dircount = readdir->count >> 1;
1573         __be32 *p, verf[2];
1574         uint32_t attrlen = 0;
1575         unsigned int i;
1576
1577         if (readdir->plus) {
1578                 attrs[0] |= FATTR4_WORD0_TYPE|FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE|
1579                         FATTR4_WORD0_FSID|FATTR4_WORD0_FILEHANDLE|FATTR4_WORD0_FILEID;
1580                 attrs[1] |= FATTR4_WORD1_MODE|FATTR4_WORD1_NUMLINKS|FATTR4_WORD1_OWNER|
1581                         FATTR4_WORD1_OWNER_GROUP|FATTR4_WORD1_RAWDEV|
1582                         FATTR4_WORD1_SPACE_USED|FATTR4_WORD1_TIME_ACCESS|
1583                         FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
1584                 attrs[2] |= FATTR4_WORD2_SECURITY_LABEL;
1585                 dircount >>= 1;
1586         }
1587         /* Use mounted_on_fileid only if the server supports it */
1588         if (!(readdir->bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID))
1589                 attrs[0] |= FATTR4_WORD0_FILEID;
1590         for (i = 0; i < ARRAY_SIZE(attrs); i++) {
1591                 attrs[i] &= readdir->bitmask[i];
1592                 if (attrs[i] != 0)
1593                         attrlen = i+1;
1594         }
1595
1596         encode_op_hdr(xdr, OP_READDIR, decode_readdir_maxsz, hdr);
1597         encode_uint64(xdr, readdir->cookie);
1598         encode_nfs4_verifier(xdr, &readdir->verifier);
1599         p = reserve_space(xdr, 12 + (attrlen << 2));
1600         *p++ = cpu_to_be32(dircount);
1601         *p++ = cpu_to_be32(readdir->count);
1602         *p++ = cpu_to_be32(attrlen);
1603         for (i = 0; i < attrlen; i++)
1604                 *p++ = cpu_to_be32(attrs[i]);
1605         memcpy(verf, readdir->verifier.data, sizeof(verf));
1606
1607         dprintk("%s: cookie = %llu, verifier = %08x:%08x, bitmap = %08x:%08x:%08x\n",
1608                         __func__,
1609                         (unsigned long long)readdir->cookie,
1610                         verf[0], verf[1],
1611                         attrs[0] & readdir->bitmask[0],
1612                         attrs[1] & readdir->bitmask[1],
1613                         attrs[2] & readdir->bitmask[2]);
1614 }
1615
1616 static void encode_readlink(struct xdr_stream *xdr, const struct nfs4_readlink *readlink, struct rpc_rqst *req, struct compound_hdr *hdr)
1617 {
1618         encode_op_hdr(xdr, OP_READLINK, decode_readlink_maxsz, hdr);
1619 }
1620
1621 static void encode_remove(struct xdr_stream *xdr, const struct qstr *name, struct compound_hdr *hdr)
1622 {
1623         encode_op_hdr(xdr, OP_REMOVE, decode_remove_maxsz, hdr);
1624         encode_string(xdr, name->len, name->name);
1625 }
1626
1627 static void encode_rename(struct xdr_stream *xdr, const struct qstr *oldname, const struct qstr *newname, struct compound_hdr *hdr)
1628 {
1629         encode_op_hdr(xdr, OP_RENAME, decode_rename_maxsz, hdr);
1630         encode_string(xdr, oldname->len, oldname->name);
1631         encode_string(xdr, newname->len, newname->name);
1632 }
1633
1634 static void encode_renew(struct xdr_stream *xdr, clientid4 clid,
1635                          struct compound_hdr *hdr)
1636 {
1637         encode_op_hdr(xdr, OP_RENEW, decode_renew_maxsz, hdr);
1638         encode_uint64(xdr, clid);
1639 }
1640
1641 static void
1642 encode_restorefh(struct xdr_stream *xdr, struct compound_hdr *hdr)
1643 {
1644         encode_op_hdr(xdr, OP_RESTOREFH, decode_restorefh_maxsz, hdr);
1645 }
1646
1647 static void
1648 encode_setacl(struct xdr_stream *xdr, struct nfs_setaclargs *arg, struct compound_hdr *hdr)
1649 {
1650         __be32 *p;
1651
1652         encode_op_hdr(xdr, OP_SETATTR, decode_setacl_maxsz, hdr);
1653         encode_nfs4_stateid(xdr, &zero_stateid);
1654         p = reserve_space(xdr, 2*4);
1655         *p++ = cpu_to_be32(1);
1656         *p = cpu_to_be32(FATTR4_WORD0_ACL);
1657         p = reserve_space(xdr, 4);
1658         *p = cpu_to_be32(arg->acl_len);
1659         xdr_write_pages(xdr, arg->acl_pages, arg->acl_pgbase, arg->acl_len);
1660 }
1661
1662 static void
1663 encode_savefh(struct xdr_stream *xdr, struct compound_hdr *hdr)
1664 {
1665         encode_op_hdr(xdr, OP_SAVEFH, decode_savefh_maxsz, hdr);
1666 }
1667
1668 static void encode_setattr(struct xdr_stream *xdr, const struct nfs_setattrargs *arg, const struct nfs_server *server, struct compound_hdr *hdr)
1669 {
1670         encode_op_hdr(xdr, OP_SETATTR, decode_setattr_maxsz, hdr);
1671         encode_nfs4_stateid(xdr, &arg->stateid);
1672         encode_attrs(xdr, arg->iap, arg->label, server);
1673 }
1674
1675 static void encode_setclientid(struct xdr_stream *xdr, const struct nfs4_setclientid *setclientid, struct compound_hdr *hdr)
1676 {
1677         __be32 *p;
1678
1679         encode_op_hdr(xdr, OP_SETCLIENTID, decode_setclientid_maxsz, hdr);
1680         encode_nfs4_verifier(xdr, setclientid->sc_verifier);
1681
1682         encode_string(xdr, setclientid->sc_name_len, setclientid->sc_name);
1683         p = reserve_space(xdr, 4);
1684         *p = cpu_to_be32(setclientid->sc_prog);
1685         encode_string(xdr, setclientid->sc_netid_len, setclientid->sc_netid);
1686         encode_string(xdr, setclientid->sc_uaddr_len, setclientid->sc_uaddr);
1687         p = reserve_space(xdr, 4);
1688         *p = cpu_to_be32(setclientid->sc_cb_ident);
1689 }
1690
1691 static void encode_setclientid_confirm(struct xdr_stream *xdr, const struct nfs4_setclientid_res *arg, struct compound_hdr *hdr)
1692 {
1693         encode_op_hdr(xdr, OP_SETCLIENTID_CONFIRM,
1694                         decode_setclientid_confirm_maxsz, hdr);
1695         encode_uint64(xdr, arg->clientid);
1696         encode_nfs4_verifier(xdr, &arg->confirm);
1697 }
1698
1699 static void encode_write(struct xdr_stream *xdr, const struct nfs_pgio_args *args,
1700                          struct compound_hdr *hdr)
1701 {
1702         __be32 *p;
1703
1704         encode_op_hdr(xdr, OP_WRITE, decode_write_maxsz, hdr);
1705         encode_nfs4_stateid(xdr, &args->stateid);
1706
1707         p = reserve_space(xdr, 16);
1708         p = xdr_encode_hyper(p, args->offset);
1709         *p++ = cpu_to_be32(args->stable);
1710         *p = cpu_to_be32(args->count);
1711
1712         xdr_write_pages(xdr, args->pages, args->pgbase, args->count);
1713 }
1714
1715 static void encode_delegreturn(struct xdr_stream *xdr, const nfs4_stateid *stateid, struct compound_hdr *hdr)
1716 {
1717         encode_op_hdr(xdr, OP_DELEGRETURN, decode_delegreturn_maxsz, hdr);
1718         encode_nfs4_stateid(xdr, stateid);
1719 }
1720
1721 static void encode_secinfo(struct xdr_stream *xdr, const struct qstr *name, struct compound_hdr *hdr)
1722 {
1723         encode_op_hdr(xdr, OP_SECINFO, decode_secinfo_maxsz, hdr);
1724         encode_string(xdr, name->len, name->name);
1725 }
1726
1727 #if defined(CONFIG_NFS_V4_1)
1728 /* NFSv4.1 operations */
1729 static void encode_bind_conn_to_session(struct xdr_stream *xdr,
1730                                    struct nfs4_session *session,
1731                                    struct compound_hdr *hdr)
1732 {
1733         __be32 *p;
1734
1735         encode_op_hdr(xdr, OP_BIND_CONN_TO_SESSION,
1736                 decode_bind_conn_to_session_maxsz, hdr);
1737         encode_opaque_fixed(xdr, session->sess_id.data, NFS4_MAX_SESSIONID_LEN);
1738         p = xdr_reserve_space(xdr, 8);
1739         *p++ = cpu_to_be32(NFS4_CDFC4_BACK_OR_BOTH);
1740         *p = 0; /* use_conn_in_rdma_mode = False */
1741 }
1742
1743 static void encode_op_map(struct xdr_stream *xdr, struct nfs4_op_map *op_map)
1744 {
1745         unsigned int i;
1746         encode_uint32(xdr, NFS4_OP_MAP_NUM_WORDS);
1747         for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++)
1748                 encode_uint32(xdr, op_map->u.words[i]);
1749 }
1750
1751 static void encode_exchange_id(struct xdr_stream *xdr,
1752                                struct nfs41_exchange_id_args *args,
1753                                struct compound_hdr *hdr)
1754 {
1755         __be32 *p;
1756         char impl_name[IMPL_NAME_LIMIT];
1757         int len = 0;
1758
1759         encode_op_hdr(xdr, OP_EXCHANGE_ID, decode_exchange_id_maxsz, hdr);
1760         encode_nfs4_verifier(xdr, args->verifier);
1761
1762         encode_string(xdr, args->id_len, args->id);
1763
1764         encode_uint32(xdr, args->flags);
1765         encode_uint32(xdr, args->state_protect.how);
1766
1767         switch (args->state_protect.how) {
1768         case SP4_NONE:
1769                 break;
1770         case SP4_MACH_CRED:
1771                 encode_op_map(xdr, &args->state_protect.enforce);
1772                 encode_op_map(xdr, &args->state_protect.allow);
1773                 break;
1774         default:
1775                 WARN_ON_ONCE(1);
1776                 break;
1777         }
1778
1779         if (send_implementation_id &&
1780             sizeof(CONFIG_NFS_V4_1_IMPLEMENTATION_ID_DOMAIN) > 1 &&
1781             sizeof(CONFIG_NFS_V4_1_IMPLEMENTATION_ID_DOMAIN)
1782                 <= sizeof(impl_name) + 1)
1783                 len = snprintf(impl_name, sizeof(impl_name), "%s %s %s %s",
1784                                utsname()->sysname, utsname()->release,
1785                                utsname()->version, utsname()->machine);
1786
1787         if (len > 0) {
1788                 encode_uint32(xdr, 1);  /* implementation id array length=1 */
1789
1790                 encode_string(xdr,
1791                         sizeof(CONFIG_NFS_V4_1_IMPLEMENTATION_ID_DOMAIN) - 1,
1792                         CONFIG_NFS_V4_1_IMPLEMENTATION_ID_DOMAIN);
1793                 encode_string(xdr, len, impl_name);
1794                 /* just send zeros for nii_date - the date is in nii_name */
1795                 p = reserve_space(xdr, 12);
1796                 p = xdr_encode_hyper(p, 0);
1797                 *p = cpu_to_be32(0);
1798         } else
1799                 encode_uint32(xdr, 0);  /* implementation id array length=0 */
1800 }
1801
1802 static void encode_create_session(struct xdr_stream *xdr,
1803                                   struct nfs41_create_session_args *args,
1804                                   struct compound_hdr *hdr)
1805 {
1806         __be32 *p;
1807         struct nfs_client *clp = args->client;
1808         struct rpc_clnt *clnt = clp->cl_rpcclient;
1809         struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
1810         u32 max_resp_sz_cached;
1811
1812         /*
1813          * Assumes OPEN is the biggest non-idempotent compound.
1814          * 2 is the verifier.
1815          */
1816         max_resp_sz_cached = (NFS4_dec_open_sz + RPC_REPHDRSIZE +
1817                               RPC_MAX_AUTH_SIZE + 2) * XDR_UNIT;
1818
1819         encode_op_hdr(xdr, OP_CREATE_SESSION, decode_create_session_maxsz, hdr);
1820         p = reserve_space(xdr, 16 + 2*28 + 20 + clnt->cl_nodelen + 12);
1821         p = xdr_encode_hyper(p, clp->cl_clientid);
1822         *p++ = cpu_to_be32(clp->cl_seqid);                      /*Sequence id */
1823         *p++ = cpu_to_be32(args->flags);                        /*flags */
1824
1825         /* Fore Channel */
1826         *p++ = cpu_to_be32(0);                          /* header padding size */
1827         *p++ = cpu_to_be32(args->fc_attrs.max_rqst_sz); /* max req size */
1828         *p++ = cpu_to_be32(args->fc_attrs.max_resp_sz); /* max resp size */
1829         *p++ = cpu_to_be32(max_resp_sz_cached);         /* Max resp sz cached */
1830         *p++ = cpu_to_be32(args->fc_attrs.max_ops);     /* max operations */
1831         *p++ = cpu_to_be32(args->fc_attrs.max_reqs);    /* max requests */
1832         *p++ = cpu_to_be32(0);                          /* rdmachannel_attrs */
1833
1834         /* Back Channel */
1835         *p++ = cpu_to_be32(0);                          /* header padding size */
1836         *p++ = cpu_to_be32(args->bc_attrs.max_rqst_sz); /* max req size */
1837         *p++ = cpu_to_be32(args->bc_attrs.max_resp_sz); /* max resp size */
1838         *p++ = cpu_to_be32(args->bc_attrs.max_resp_sz_cached);  /* Max resp sz cached */
1839         *p++ = cpu_to_be32(args->bc_attrs.max_ops);     /* max operations */
1840         *p++ = cpu_to_be32(args->bc_attrs.max_reqs);    /* max requests */
1841         *p++ = cpu_to_be32(0);                          /* rdmachannel_attrs */
1842
1843         *p++ = cpu_to_be32(args->cb_program);           /* cb_program */
1844         *p++ = cpu_to_be32(1);
1845         *p++ = cpu_to_be32(RPC_AUTH_UNIX);                      /* auth_sys */
1846
1847         /* authsys_parms rfc1831 */
1848         *p++ = cpu_to_be32(nn->boot_time.tv_nsec);      /* stamp */
1849         p = xdr_encode_array(p, clnt->cl_nodename, clnt->cl_nodelen);
1850         *p++ = cpu_to_be32(0);                          /* UID */
1851         *p++ = cpu_to_be32(0);                          /* GID */
1852         *p = cpu_to_be32(0);                            /* No more gids */
1853 }
1854
1855 static void encode_destroy_session(struct xdr_stream *xdr,
1856                                    struct nfs4_session *session,
1857                                    struct compound_hdr *hdr)
1858 {
1859         encode_op_hdr(xdr, OP_DESTROY_SESSION, decode_destroy_session_maxsz, hdr);
1860         encode_opaque_fixed(xdr, session->sess_id.data, NFS4_MAX_SESSIONID_LEN);
1861 }
1862
1863 static void encode_destroy_clientid(struct xdr_stream *xdr,
1864                                    uint64_t clientid,
1865                                    struct compound_hdr *hdr)
1866 {
1867         encode_op_hdr(xdr, OP_DESTROY_CLIENTID, decode_destroy_clientid_maxsz, hdr);
1868         encode_uint64(xdr, clientid);
1869 }
1870
1871 static void encode_reclaim_complete(struct xdr_stream *xdr,
1872                                     struct nfs41_reclaim_complete_args *args,
1873                                     struct compound_hdr *hdr)
1874 {
1875         encode_op_hdr(xdr, OP_RECLAIM_COMPLETE, decode_reclaim_complete_maxsz, hdr);
1876         encode_uint32(xdr, args->one_fs);
1877 }
1878 #endif /* CONFIG_NFS_V4_1 */
1879
1880 static void encode_sequence(struct xdr_stream *xdr,
1881                             const struct nfs4_sequence_args *args,
1882                             struct compound_hdr *hdr)
1883 {
1884 #if defined(CONFIG_NFS_V4_1)
1885         struct nfs4_session *session;
1886         struct nfs4_slot_table *tp;
1887         struct nfs4_slot *slot = args->sa_slot;
1888         __be32 *p;
1889
1890         tp = slot->table;
1891         session = tp->session;
1892         if (!session)
1893                 return;
1894
1895         encode_op_hdr(xdr, OP_SEQUENCE, decode_sequence_maxsz, hdr);
1896
1897         /*
1898          * Sessionid + seqid + slotid + max slotid + cache_this
1899          */
1900         dprintk("%s: sessionid=%u:%u:%u:%u seqid=%d slotid=%d "
1901                 "max_slotid=%d cache_this=%d\n",
1902                 __func__,
1903                 ((u32 *)session->sess_id.data)[0],
1904                 ((u32 *)session->sess_id.data)[1],
1905                 ((u32 *)session->sess_id.data)[2],
1906                 ((u32 *)session->sess_id.data)[3],
1907                 slot->seq_nr, slot->slot_nr,
1908                 tp->highest_used_slotid, args->sa_cache_this);
1909         p = reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 16);
1910         p = xdr_encode_opaque_fixed(p, session->sess_id.data, NFS4_MAX_SESSIONID_LEN);
1911         *p++ = cpu_to_be32(slot->seq_nr);
1912         *p++ = cpu_to_be32(slot->slot_nr);
1913         *p++ = cpu_to_be32(tp->highest_used_slotid);
1914         *p = cpu_to_be32(args->sa_cache_this);
1915 #endif /* CONFIG_NFS_V4_1 */
1916 }
1917
1918 #ifdef CONFIG_NFS_V4_1
1919 static void
1920 encode_getdeviceinfo(struct xdr_stream *xdr,
1921                      const struct nfs4_getdeviceinfo_args *args,
1922                      struct compound_hdr *hdr)
1923 {
1924         __be32 *p;
1925
1926         encode_op_hdr(xdr, OP_GETDEVICEINFO, decode_getdeviceinfo_maxsz, hdr);
1927         p = reserve_space(xdr, NFS4_DEVICEID4_SIZE + 4 + 4);
1928         p = xdr_encode_opaque_fixed(p, args->pdev->dev_id.data,
1929                                     NFS4_DEVICEID4_SIZE);
1930         *p++ = cpu_to_be32(args->pdev->layout_type);
1931         *p++ = cpu_to_be32(args->pdev->maxcount);       /* gdia_maxcount */
1932
1933         p = reserve_space(xdr, 4 + 4);
1934         *p++ = cpu_to_be32(1);                  /* bitmap length */
1935         *p++ = cpu_to_be32(NOTIFY_DEVICEID4_CHANGE | NOTIFY_DEVICEID4_DELETE);
1936 }
1937
1938 static void
1939 encode_layoutget(struct xdr_stream *xdr,
1940                       const struct nfs4_layoutget_args *args,
1941                       struct compound_hdr *hdr)
1942 {
1943         __be32 *p;
1944
1945         encode_op_hdr(xdr, OP_LAYOUTGET, decode_layoutget_maxsz, hdr);
1946         p = reserve_space(xdr, 36);
1947         *p++ = cpu_to_be32(0);     /* Signal layout available */
1948         *p++ = cpu_to_be32(args->type);
1949         *p++ = cpu_to_be32(args->range.iomode);
1950         p = xdr_encode_hyper(p, args->range.offset);
1951         p = xdr_encode_hyper(p, args->range.length);
1952         p = xdr_encode_hyper(p, args->minlength);
1953         encode_nfs4_stateid(xdr, &args->stateid);
1954         encode_uint32(xdr, args->maxcount);
1955
1956         dprintk("%s: 1st type:0x%x iomode:%d off:%lu len:%lu mc:%d\n",
1957                 __func__,
1958                 args->type,
1959                 args->range.iomode,
1960                 (unsigned long)args->range.offset,
1961                 (unsigned long)args->range.length,
1962                 args->maxcount);
1963 }
1964
1965 static int
1966 encode_layoutcommit(struct xdr_stream *xdr,
1967                     struct inode *inode,
1968                     struct nfs4_layoutcommit_args *args,
1969                     struct compound_hdr *hdr)
1970 {
1971         __be32 *p;
1972
1973         dprintk("%s: lbw: %llu type: %d\n", __func__, args->lastbytewritten,
1974                 NFS_SERVER(args->inode)->pnfs_curr_ld->id);
1975
1976         encode_op_hdr(xdr, OP_LAYOUTCOMMIT, decode_layoutcommit_maxsz, hdr);
1977         p = reserve_space(xdr, 20);
1978         /* Only whole file layouts */
1979         p = xdr_encode_hyper(p, 0); /* offset */
1980         p = xdr_encode_hyper(p, args->lastbytewritten + 1);     /* length */
1981         *p = cpu_to_be32(0); /* reclaim */
1982         encode_nfs4_stateid(xdr, &args->stateid);
1983         p = reserve_space(xdr, 20);
1984         *p++ = cpu_to_be32(1); /* newoffset = TRUE */
1985         p = xdr_encode_hyper(p, args->lastbytewritten);
1986         *p++ = cpu_to_be32(0); /* Never send time_modify_changed */
1987         *p++ = cpu_to_be32(NFS_SERVER(args->inode)->pnfs_curr_ld->id);/* type */
1988
1989         if (NFS_SERVER(inode)->pnfs_curr_ld->encode_layoutcommit) {
1990                 NFS_SERVER(inode)->pnfs_curr_ld->encode_layoutcommit(
1991                         NFS_I(inode)->layout, xdr, args);
1992         } else {
1993                 encode_uint32(xdr, args->layoutupdate_len);
1994                 if (args->layoutupdate_pages) {
1995                         xdr_write_pages(xdr, args->layoutupdate_pages, 0,
1996                                         args->layoutupdate_len);
1997                 }
1998         }
1999
2000         return 0;
2001 }
2002
2003 static void
2004 encode_layoutreturn(struct xdr_stream *xdr,
2005                     const struct nfs4_layoutreturn_args *args,
2006                     struct compound_hdr *hdr)
2007 {
2008         __be32 *p;
2009
2010         encode_op_hdr(xdr, OP_LAYOUTRETURN, decode_layoutreturn_maxsz, hdr);
2011         p = reserve_space(xdr, 16);
2012         *p++ = cpu_to_be32(0);          /* reclaim. always 0 for now */
2013         *p++ = cpu_to_be32(args->layout_type);
2014         *p++ = cpu_to_be32(args->range.iomode);
2015         *p = cpu_to_be32(RETURN_FILE);
2016         p = reserve_space(xdr, 16);
2017         p = xdr_encode_hyper(p, args->range.offset);
2018         p = xdr_encode_hyper(p, args->range.length);
2019         spin_lock(&args->inode->i_lock);
2020         encode_nfs4_stateid(xdr, &args->stateid);
2021         spin_unlock(&args->inode->i_lock);
2022         if (NFS_SERVER(args->inode)->pnfs_curr_ld->encode_layoutreturn) {
2023                 NFS_SERVER(args->inode)->pnfs_curr_ld->encode_layoutreturn(
2024                         NFS_I(args->inode)->layout, xdr, args);
2025         } else
2026                 encode_uint32(xdr, 0);
2027 }
2028
2029 static int
2030 encode_secinfo_no_name(struct xdr_stream *xdr,
2031                        const struct nfs41_secinfo_no_name_args *args,
2032                        struct compound_hdr *hdr)
2033 {
2034         encode_op_hdr(xdr, OP_SECINFO_NO_NAME, decode_secinfo_no_name_maxsz, hdr);
2035         encode_uint32(xdr, args->style);
2036         return 0;
2037 }
2038
2039 static void encode_test_stateid(struct xdr_stream *xdr,
2040                                 struct nfs41_test_stateid_args *args,
2041                                 struct compound_hdr *hdr)
2042 {
2043         encode_op_hdr(xdr, OP_TEST_STATEID, decode_test_stateid_maxsz, hdr);
2044         encode_uint32(xdr, 1);
2045         encode_nfs4_stateid(xdr, args->stateid);
2046 }
2047
2048 static void encode_free_stateid(struct xdr_stream *xdr,
2049                                 struct nfs41_free_stateid_args *args,
2050                                 struct compound_hdr *hdr)
2051 {
2052         encode_op_hdr(xdr, OP_FREE_STATEID, decode_free_stateid_maxsz, hdr);
2053         encode_nfs4_stateid(xdr, &args->stateid);
2054 }
2055 #endif /* CONFIG_NFS_V4_1 */
2056
2057 /*
2058  * END OF "GENERIC" ENCODE ROUTINES.
2059  */
2060
2061 static u32 nfs4_xdr_minorversion(const struct nfs4_sequence_args *args)
2062 {
2063 #if defined(CONFIG_NFS_V4_1)
2064         struct nfs4_session *session = args->sa_slot->table->session;
2065         if (session)
2066                 return session->clp->cl_mvops->minor_version;
2067 #endif /* CONFIG_NFS_V4_1 */
2068         return 0;
2069 }
2070
2071 /*
2072  * Encode an ACCESS request
2073  */
2074 static void nfs4_xdr_enc_access(struct rpc_rqst *req, struct xdr_stream *xdr,
2075                                 const struct nfs4_accessargs *args)
2076 {
2077         struct compound_hdr hdr = {
2078                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2079         };
2080
2081         encode_compound_hdr(xdr, req, &hdr);
2082         encode_sequence(xdr, &args->seq_args, &hdr);
2083         encode_putfh(xdr, args->fh, &hdr);
2084         encode_access(xdr, args->access, &hdr);
2085         encode_getfattr(xdr, args->bitmask, &hdr);
2086         encode_nops(&hdr);
2087 }
2088
2089 /*
2090  * Encode LOOKUP request
2091  */
2092 static void nfs4_xdr_enc_lookup(struct rpc_rqst *req, struct xdr_stream *xdr,
2093                                 const struct nfs4_lookup_arg *args)
2094 {
2095         struct compound_hdr hdr = {
2096                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2097         };
2098
2099         encode_compound_hdr(xdr, req, &hdr);
2100         encode_sequence(xdr, &args->seq_args, &hdr);
2101         encode_putfh(xdr, args->dir_fh, &hdr);
2102         encode_lookup(xdr, args->name, &hdr);
2103         encode_getfh(xdr, &hdr);
2104         encode_getfattr(xdr, args->bitmask, &hdr);
2105         encode_nops(&hdr);
2106 }
2107
2108 /*
2109  * Encode LOOKUP_ROOT request
2110  */
2111 static void nfs4_xdr_enc_lookup_root(struct rpc_rqst *req,
2112                                      struct xdr_stream *xdr,
2113                                      const struct nfs4_lookup_root_arg *args)
2114 {
2115         struct compound_hdr hdr = {
2116                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2117         };
2118
2119         encode_compound_hdr(xdr, req, &hdr);
2120         encode_sequence(xdr, &args->seq_args, &hdr);
2121         encode_putrootfh(xdr, &hdr);
2122         encode_getfh(xdr, &hdr);
2123         encode_getfattr(xdr, args->bitmask, &hdr);
2124         encode_nops(&hdr);
2125 }
2126
2127 /*
2128  * Encode REMOVE request
2129  */
2130 static void nfs4_xdr_enc_remove(struct rpc_rqst *req, struct xdr_stream *xdr,
2131                                 const struct nfs_removeargs *args)
2132 {
2133         struct compound_hdr hdr = {
2134                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2135         };
2136
2137         encode_compound_hdr(xdr, req, &hdr);
2138         encode_sequence(xdr, &args->seq_args, &hdr);
2139         encode_putfh(xdr, args->fh, &hdr);
2140         encode_remove(xdr, &args->name, &hdr);
2141         encode_nops(&hdr);
2142 }
2143
2144 /*
2145  * Encode RENAME request
2146  */
2147 static void nfs4_xdr_enc_rename(struct rpc_rqst *req, struct xdr_stream *xdr,
2148                                 const struct nfs_renameargs *args)
2149 {
2150         struct compound_hdr hdr = {
2151                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2152         };
2153
2154         encode_compound_hdr(xdr, req, &hdr);
2155         encode_sequence(xdr, &args->seq_args, &hdr);
2156         encode_putfh(xdr, args->old_dir, &hdr);
2157         encode_savefh(xdr, &hdr);
2158         encode_putfh(xdr, args->new_dir, &hdr);
2159         encode_rename(xdr, args->old_name, args->new_name, &hdr);
2160         encode_nops(&hdr);
2161 }
2162
2163 /*
2164  * Encode LINK request
2165  */
2166 static void nfs4_xdr_enc_link(struct rpc_rqst *req, struct xdr_stream *xdr,
2167                              const struct nfs4_link_arg *args)
2168 {
2169         struct compound_hdr hdr = {
2170                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2171         };
2172
2173         encode_compound_hdr(xdr, req, &hdr);
2174         encode_sequence(xdr, &args->seq_args, &hdr);
2175         encode_putfh(xdr, args->fh, &hdr);
2176         encode_savefh(xdr, &hdr);
2177         encode_putfh(xdr, args->dir_fh, &hdr);
2178         encode_link(xdr, args->name, &hdr);
2179         encode_restorefh(xdr, &hdr);
2180         encode_getfattr(xdr, args->bitmask, &hdr);
2181         encode_nops(&hdr);
2182 }
2183
2184 /*
2185  * Encode CREATE request
2186  */
2187 static void nfs4_xdr_enc_create(struct rpc_rqst *req, struct xdr_stream *xdr,
2188                                 const struct nfs4_create_arg *args)
2189 {
2190         struct compound_hdr hdr = {
2191                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2192         };
2193
2194         encode_compound_hdr(xdr, req, &hdr);
2195         encode_sequence(xdr, &args->seq_args, &hdr);
2196         encode_putfh(xdr, args->dir_fh, &hdr);
2197         encode_create(xdr, args, &hdr);
2198         encode_getfh(xdr, &hdr);
2199         encode_getfattr(xdr, args->bitmask, &hdr);
2200         encode_nops(&hdr);
2201 }
2202
2203 /*
2204  * Encode SYMLINK request
2205  */
2206 static void nfs4_xdr_enc_symlink(struct rpc_rqst *req, struct xdr_stream *xdr,
2207                                  const struct nfs4_create_arg *args)
2208 {
2209         nfs4_xdr_enc_create(req, xdr, args);
2210 }
2211
2212 /*
2213  * Encode GETATTR request
2214  */
2215 static void nfs4_xdr_enc_getattr(struct rpc_rqst *req, struct xdr_stream *xdr,
2216                                  const struct nfs4_getattr_arg *args)
2217 {
2218         struct compound_hdr hdr = {
2219                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2220         };
2221
2222         encode_compound_hdr(xdr, req, &hdr);
2223         encode_sequence(xdr, &args->seq_args, &hdr);
2224         encode_putfh(xdr, args->fh, &hdr);
2225         encode_getfattr(xdr, args->bitmask, &hdr);
2226         encode_nops(&hdr);
2227 }
2228
2229 /*
2230  * Encode a CLOSE request
2231  */
2232 static void nfs4_xdr_enc_close(struct rpc_rqst *req, struct xdr_stream *xdr,
2233                                struct nfs_closeargs *args)
2234 {
2235         struct compound_hdr hdr = {
2236                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2237         };
2238
2239         encode_compound_hdr(xdr, req, &hdr);
2240         encode_sequence(xdr, &args->seq_args, &hdr);
2241         encode_putfh(xdr, args->fh, &hdr);
2242         encode_close(xdr, args, &hdr);
2243         encode_getfattr(xdr, args->bitmask, &hdr);
2244         encode_nops(&hdr);
2245 }
2246
2247 /*
2248  * Encode an OPEN request
2249  */
2250 static void nfs4_xdr_enc_open(struct rpc_rqst *req, struct xdr_stream *xdr,
2251                               struct nfs_openargs *args)
2252 {
2253         struct compound_hdr hdr = {
2254                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2255         };
2256
2257         encode_compound_hdr(xdr, req, &hdr);
2258         encode_sequence(xdr, &args->seq_args, &hdr);
2259         encode_putfh(xdr, args->fh, &hdr);
2260         encode_open(xdr, args, &hdr);
2261         encode_getfh(xdr, &hdr);
2262         if (args->access)
2263                 encode_access(xdr, args->access, &hdr);
2264         encode_getfattr_open(xdr, args->bitmask, args->open_bitmap, &hdr);
2265         encode_nops(&hdr);
2266 }
2267
2268 /*
2269  * Encode an OPEN_CONFIRM request
2270  */
2271 static void nfs4_xdr_enc_open_confirm(struct rpc_rqst *req,
2272                                       struct xdr_stream *xdr,
2273                                       struct nfs_open_confirmargs *args)
2274 {
2275         struct compound_hdr hdr = {
2276                 .nops   = 0,
2277         };
2278
2279         encode_compound_hdr(xdr, req, &hdr);
2280         encode_putfh(xdr, args->fh, &hdr);
2281         encode_open_confirm(xdr, args, &hdr);
2282         encode_nops(&hdr);
2283 }
2284
2285 /*
2286  * Encode an OPEN request with no attributes.
2287  */
2288 static void nfs4_xdr_enc_open_noattr(struct rpc_rqst *req,
2289                                      struct xdr_stream *xdr,
2290                                      struct nfs_openargs *args)
2291 {
2292         struct compound_hdr hdr = {
2293                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2294         };
2295
2296         encode_compound_hdr(xdr, req, &hdr);
2297         encode_sequence(xdr, &args->seq_args, &hdr);
2298         encode_putfh(xdr, args->fh, &hdr);
2299         encode_open(xdr, args, &hdr);
2300         if (args->access)
2301                 encode_access(xdr, args->access, &hdr);
2302         encode_getfattr_open(xdr, args->bitmask, args->open_bitmap, &hdr);
2303         encode_nops(&hdr);
2304 }
2305
2306 /*
2307  * Encode an OPEN_DOWNGRADE request
2308  */
2309 static void nfs4_xdr_enc_open_downgrade(struct rpc_rqst *req,
2310                                         struct xdr_stream *xdr,
2311                                         struct nfs_closeargs *args)
2312 {
2313         struct compound_hdr hdr = {
2314                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2315         };
2316
2317         encode_compound_hdr(xdr, req, &hdr);
2318         encode_sequence(xdr, &args->seq_args, &hdr);
2319         encode_putfh(xdr, args->fh, &hdr);
2320         encode_open_downgrade(xdr, args, &hdr);
2321         encode_getfattr(xdr, args->bitmask, &hdr);
2322         encode_nops(&hdr);
2323 }
2324
2325 /*
2326  * Encode a LOCK request
2327  */
2328 static void nfs4_xdr_enc_lock(struct rpc_rqst *req, struct xdr_stream *xdr,
2329                               struct nfs_lock_args *args)
2330 {
2331         struct compound_hdr hdr = {
2332                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2333         };
2334
2335         encode_compound_hdr(xdr, req, &hdr);
2336         encode_sequence(xdr, &args->seq_args, &hdr);
2337         encode_putfh(xdr, args->fh, &hdr);
2338         encode_lock(xdr, args, &hdr);
2339         encode_nops(&hdr);
2340 }
2341
2342 /*
2343  * Encode a LOCKT request
2344  */
2345 static void nfs4_xdr_enc_lockt(struct rpc_rqst *req, struct xdr_stream *xdr,
2346                                struct nfs_lockt_args *args)
2347 {
2348         struct compound_hdr hdr = {
2349                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2350         };
2351
2352         encode_compound_hdr(xdr, req, &hdr);
2353         encode_sequence(xdr, &args->seq_args, &hdr);
2354         encode_putfh(xdr, args->fh, &hdr);
2355         encode_lockt(xdr, args, &hdr);
2356         encode_nops(&hdr);
2357 }
2358
2359 /*
2360  * Encode a LOCKU request
2361  */
2362 static void nfs4_xdr_enc_locku(struct rpc_rqst *req, struct xdr_stream *xdr,
2363                                struct nfs_locku_args *args)
2364 {
2365         struct compound_hdr hdr = {
2366                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2367         };
2368
2369         encode_compound_hdr(xdr, req, &hdr);
2370         encode_sequence(xdr, &args->seq_args, &hdr);
2371         encode_putfh(xdr, args->fh, &hdr);
2372         encode_locku(xdr, args, &hdr);
2373         encode_nops(&hdr);
2374 }
2375
2376 static void nfs4_xdr_enc_release_lockowner(struct rpc_rqst *req,
2377                                            struct xdr_stream *xdr,
2378                                         struct nfs_release_lockowner_args *args)
2379 {
2380         struct compound_hdr hdr = {
2381                 .minorversion = 0,
2382         };
2383
2384         encode_compound_hdr(xdr, req, &hdr);
2385         encode_release_lockowner(xdr, &args->lock_owner, &hdr);
2386         encode_nops(&hdr);
2387 }
2388
2389 /*
2390  * Encode a READLINK request
2391  */
2392 static void nfs4_xdr_enc_readlink(struct rpc_rqst *req, struct xdr_stream *xdr,
2393                                   const struct nfs4_readlink *args)
2394 {
2395         struct compound_hdr hdr = {
2396                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2397         };
2398
2399         encode_compound_hdr(xdr, req, &hdr);
2400         encode_sequence(xdr, &args->seq_args, &hdr);
2401         encode_putfh(xdr, args->fh, &hdr);
2402         encode_readlink(xdr, args, req, &hdr);
2403
2404         xdr_inline_pages(&req->rq_rcv_buf, hdr.replen << 2, args->pages,
2405                         args->pgbase, args->pglen);
2406         encode_nops(&hdr);
2407 }
2408
2409 /*
2410  * Encode a READDIR request
2411  */
2412 static void nfs4_xdr_enc_readdir(struct rpc_rqst *req, struct xdr_stream *xdr,
2413                                  const struct nfs4_readdir_arg *args)
2414 {
2415         struct compound_hdr hdr = {
2416                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2417         };
2418
2419         encode_compound_hdr(xdr, req, &hdr);
2420         encode_sequence(xdr, &args->seq_args, &hdr);
2421         encode_putfh(xdr, args->fh, &hdr);
2422         encode_readdir(xdr, args, req, &hdr);
2423
2424         xdr_inline_pages(&req->rq_rcv_buf, hdr.replen << 2, args->pages,
2425                          args->pgbase, args->count);
2426         dprintk("%s: inlined page args = (%u, %p, %u, %u)\n",
2427                         __func__, hdr.replen << 2, args->pages,
2428                         args->pgbase, args->count);
2429         encode_nops(&hdr);
2430 }
2431
2432 /*
2433  * Encode a READ request
2434  */
2435 static void nfs4_xdr_enc_read(struct rpc_rqst *req, struct xdr_stream *xdr,
2436                               struct nfs_pgio_args *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_read(xdr, args, &hdr);
2446
2447         xdr_inline_pages(&req->rq_rcv_buf, hdr.replen << 2,
2448                          args->pages, args->pgbase, args->count);
2449         req->rq_rcv_buf.flags |= XDRBUF_READ;
2450         encode_nops(&hdr);
2451 }
2452
2453 /*
2454  * Encode an SETATTR request
2455  */
2456 static void nfs4_xdr_enc_setattr(struct rpc_rqst *req, struct xdr_stream *xdr,
2457                                  struct nfs_setattrargs *args)
2458 {
2459         struct compound_hdr hdr = {
2460                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2461         };
2462
2463         encode_compound_hdr(xdr, req, &hdr);
2464         encode_sequence(xdr, &args->seq_args, &hdr);
2465         encode_putfh(xdr, args->fh, &hdr);
2466         encode_setattr(xdr, args, args->server, &hdr);
2467         encode_getfattr(xdr, args->bitmask, &hdr);
2468         encode_nops(&hdr);
2469 }
2470
2471 /*
2472  * Encode a GETACL request
2473  */
2474 static void nfs4_xdr_enc_getacl(struct rpc_rqst *req, struct xdr_stream *xdr,
2475                                 struct nfs_getaclargs *args)
2476 {
2477         struct compound_hdr hdr = {
2478                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2479         };
2480         uint32_t replen;
2481
2482         encode_compound_hdr(xdr, req, &hdr);
2483         encode_sequence(xdr, &args->seq_args, &hdr);
2484         encode_putfh(xdr, args->fh, &hdr);
2485         replen = hdr.replen + op_decode_hdr_maxsz + 1;
2486         encode_getattr_two(xdr, FATTR4_WORD0_ACL, 0, &hdr);
2487
2488         xdr_inline_pages(&req->rq_rcv_buf, replen << 2,
2489                 args->acl_pages, args->acl_pgbase, args->acl_len);
2490
2491         encode_nops(&hdr);
2492 }
2493
2494 /*
2495  * Encode a WRITE request
2496  */
2497 static void nfs4_xdr_enc_write(struct rpc_rqst *req, struct xdr_stream *xdr,
2498                                struct nfs_pgio_args *args)
2499 {
2500         struct compound_hdr hdr = {
2501                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2502         };
2503
2504         encode_compound_hdr(xdr, req, &hdr);
2505         encode_sequence(xdr, &args->seq_args, &hdr);
2506         encode_putfh(xdr, args->fh, &hdr);
2507         encode_write(xdr, args, &hdr);
2508         req->rq_snd_buf.flags |= XDRBUF_WRITE;
2509         if (args->bitmask)
2510                 encode_getfattr(xdr, args->bitmask, &hdr);
2511         encode_nops(&hdr);
2512 }
2513
2514 /*
2515  *  a COMMIT request
2516  */
2517 static void nfs4_xdr_enc_commit(struct rpc_rqst *req, struct xdr_stream *xdr,
2518                                 struct nfs_commitargs *args)
2519 {
2520         struct compound_hdr hdr = {
2521                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2522         };
2523
2524         encode_compound_hdr(xdr, req, &hdr);
2525         encode_sequence(xdr, &args->seq_args, &hdr);
2526         encode_putfh(xdr, args->fh, &hdr);
2527         encode_commit(xdr, args, &hdr);
2528         encode_nops(&hdr);
2529 }
2530
2531 /*
2532  * FSINFO request
2533  */
2534 static void nfs4_xdr_enc_fsinfo(struct rpc_rqst *req, struct xdr_stream *xdr,
2535                                 struct nfs4_fsinfo_arg *args)
2536 {
2537         struct compound_hdr hdr = {
2538                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2539         };
2540
2541         encode_compound_hdr(xdr, req, &hdr);
2542         encode_sequence(xdr, &args->seq_args, &hdr);
2543         encode_putfh(xdr, args->fh, &hdr);
2544         encode_fsinfo(xdr, args->bitmask, &hdr);
2545         encode_nops(&hdr);
2546 }
2547
2548 /*
2549  * a PATHCONF request
2550  */
2551 static void nfs4_xdr_enc_pathconf(struct rpc_rqst *req, struct xdr_stream *xdr,
2552                                   const struct nfs4_pathconf_arg *args)
2553 {
2554         struct compound_hdr hdr = {
2555                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2556         };
2557
2558         encode_compound_hdr(xdr, req, &hdr);
2559         encode_sequence(xdr, &args->seq_args, &hdr);
2560         encode_putfh(xdr, args->fh, &hdr);
2561         encode_getattr_one(xdr, args->bitmask[0] & nfs4_pathconf_bitmap[0],
2562                            &hdr);
2563         encode_nops(&hdr);
2564 }
2565
2566 /*
2567  * a STATFS request
2568  */
2569 static void nfs4_xdr_enc_statfs(struct rpc_rqst *req, struct xdr_stream *xdr,
2570                                 const struct nfs4_statfs_arg *args)
2571 {
2572         struct compound_hdr hdr = {
2573                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2574         };
2575
2576         encode_compound_hdr(xdr, req, &hdr);
2577         encode_sequence(xdr, &args->seq_args, &hdr);
2578         encode_putfh(xdr, args->fh, &hdr);
2579         encode_getattr_two(xdr, args->bitmask[0] & nfs4_statfs_bitmap[0],
2580                            args->bitmask[1] & nfs4_statfs_bitmap[1], &hdr);
2581         encode_nops(&hdr);
2582 }
2583
2584 /*
2585  * GETATTR_BITMAP request
2586  */
2587 static void nfs4_xdr_enc_server_caps(struct rpc_rqst *req,
2588                                      struct xdr_stream *xdr,
2589                                      struct nfs4_server_caps_arg *args)
2590 {
2591         struct compound_hdr hdr = {
2592                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2593         };
2594
2595         encode_compound_hdr(xdr, req, &hdr);
2596         encode_sequence(xdr, &args->seq_args, &hdr);
2597         encode_putfh(xdr, args->fhandle, &hdr);
2598         encode_getattr_one(xdr, FATTR4_WORD0_SUPPORTED_ATTRS|
2599                            FATTR4_WORD0_FH_EXPIRE_TYPE|
2600                            FATTR4_WORD0_LINK_SUPPORT|
2601                            FATTR4_WORD0_SYMLINK_SUPPORT|
2602                            FATTR4_WORD0_ACLSUPPORT, &hdr);
2603         encode_nops(&hdr);
2604 }
2605
2606 /*
2607  * a RENEW request
2608  */
2609 static void nfs4_xdr_enc_renew(struct rpc_rqst *req, struct xdr_stream *xdr,
2610                                struct nfs_client *clp)
2611 {
2612         struct compound_hdr hdr = {
2613                 .nops   = 0,
2614         };
2615
2616         encode_compound_hdr(xdr, req, &hdr);
2617         encode_renew(xdr, clp->cl_clientid, &hdr);
2618         encode_nops(&hdr);
2619 }
2620
2621 /*
2622  * a SETCLIENTID request
2623  */
2624 static void nfs4_xdr_enc_setclientid(struct rpc_rqst *req,
2625                                      struct xdr_stream *xdr,
2626                                      struct nfs4_setclientid *sc)
2627 {
2628         struct compound_hdr hdr = {
2629                 .nops   = 0,
2630         };
2631
2632         encode_compound_hdr(xdr, req, &hdr);
2633         encode_setclientid(xdr, sc, &hdr);
2634         encode_nops(&hdr);
2635 }
2636
2637 /*
2638  * a SETCLIENTID_CONFIRM request
2639  */
2640 static void nfs4_xdr_enc_setclientid_confirm(struct rpc_rqst *req,
2641                                              struct xdr_stream *xdr,
2642                                              struct nfs4_setclientid_res *arg)
2643 {
2644         struct compound_hdr hdr = {
2645                 .nops   = 0,
2646         };
2647
2648         encode_compound_hdr(xdr, req, &hdr);
2649         encode_setclientid_confirm(xdr, arg, &hdr);
2650         encode_nops(&hdr);
2651 }
2652
2653 /*
2654  * DELEGRETURN request
2655  */
2656 static void nfs4_xdr_enc_delegreturn(struct rpc_rqst *req,
2657                                      struct xdr_stream *xdr,
2658                                      const struct nfs4_delegreturnargs *args)
2659 {
2660         struct compound_hdr hdr = {
2661                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2662         };
2663
2664         encode_compound_hdr(xdr, req, &hdr);
2665         encode_sequence(xdr, &args->seq_args, &hdr);
2666         encode_putfh(xdr, args->fhandle, &hdr);
2667         encode_getfattr(xdr, args->bitmask, &hdr);
2668         encode_delegreturn(xdr, args->stateid, &hdr);
2669         encode_nops(&hdr);
2670 }
2671
2672 /*
2673  * Encode FS_LOCATIONS request
2674  */
2675 static void nfs4_xdr_enc_fs_locations(struct rpc_rqst *req,
2676                                       struct xdr_stream *xdr,
2677                                       struct nfs4_fs_locations_arg *args)
2678 {
2679         struct compound_hdr hdr = {
2680                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2681         };
2682         uint32_t replen;
2683
2684         encode_compound_hdr(xdr, req, &hdr);
2685         encode_sequence(xdr, &args->seq_args, &hdr);
2686         if (args->migration) {
2687                 encode_putfh(xdr, args->fh, &hdr);
2688                 replen = hdr.replen;
2689                 encode_fs_locations(xdr, args->bitmask, &hdr);
2690                 if (args->renew)
2691                         encode_renew(xdr, args->clientid, &hdr);
2692         } else {
2693                 encode_putfh(xdr, args->dir_fh, &hdr);
2694                 encode_lookup(xdr, args->name, &hdr);
2695                 replen = hdr.replen;
2696                 encode_fs_locations(xdr, args->bitmask, &hdr);
2697         }
2698
2699         /* Set up reply kvec to capture returned fs_locations array. */
2700         xdr_inline_pages(&req->rq_rcv_buf, replen << 2, &args->page,
2701                         0, PAGE_SIZE);
2702         encode_nops(&hdr);
2703 }
2704
2705 /*
2706  * Encode SECINFO request
2707  */
2708 static void nfs4_xdr_enc_secinfo(struct rpc_rqst *req,
2709                                 struct xdr_stream *xdr,
2710                                 struct nfs4_secinfo_arg *args)
2711 {
2712         struct compound_hdr hdr = {
2713                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2714         };
2715
2716         encode_compound_hdr(xdr, req, &hdr);
2717         encode_sequence(xdr, &args->seq_args, &hdr);
2718         encode_putfh(xdr, args->dir_fh, &hdr);
2719         encode_secinfo(xdr, args->name, &hdr);
2720         encode_nops(&hdr);
2721 }
2722
2723 /*
2724  * Encode FSID_PRESENT request
2725  */
2726 static void nfs4_xdr_enc_fsid_present(struct rpc_rqst *req,
2727                                       struct xdr_stream *xdr,
2728                                       struct nfs4_fsid_present_arg *args)
2729 {
2730         struct compound_hdr hdr = {
2731                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2732         };
2733
2734         encode_compound_hdr(xdr, req, &hdr);
2735         encode_sequence(xdr, &args->seq_args, &hdr);
2736         encode_putfh(xdr, args->fh, &hdr);
2737         encode_getfh(xdr, &hdr);
2738         if (args->renew)
2739                 encode_renew(xdr, args->clientid, &hdr);
2740         encode_nops(&hdr);
2741 }
2742
2743 #if defined(CONFIG_NFS_V4_1)
2744 /*
2745  * BIND_CONN_TO_SESSION request
2746  */
2747 static void nfs4_xdr_enc_bind_conn_to_session(struct rpc_rqst *req,
2748                                 struct xdr_stream *xdr,
2749                                 struct nfs_client *clp)
2750 {
2751         struct compound_hdr hdr = {
2752                 .minorversion = clp->cl_mvops->minor_version,
2753         };
2754
2755         encode_compound_hdr(xdr, req, &hdr);
2756         encode_bind_conn_to_session(xdr, clp->cl_session, &hdr);
2757         encode_nops(&hdr);
2758 }
2759
2760 /*
2761  * EXCHANGE_ID request
2762  */
2763 static void nfs4_xdr_enc_exchange_id(struct rpc_rqst *req,
2764                                      struct xdr_stream *xdr,
2765                                      struct nfs41_exchange_id_args *args)
2766 {
2767         struct compound_hdr hdr = {
2768                 .minorversion = args->client->cl_mvops->minor_version,
2769         };
2770
2771         encode_compound_hdr(xdr, req, &hdr);
2772         encode_exchange_id(xdr, args, &hdr);
2773         encode_nops(&hdr);
2774 }
2775
2776 /*
2777  * a CREATE_SESSION request
2778  */
2779 static void nfs4_xdr_enc_create_session(struct rpc_rqst *req,
2780                                         struct xdr_stream *xdr,
2781                                         struct nfs41_create_session_args *args)
2782 {
2783         struct compound_hdr hdr = {
2784                 .minorversion = args->client->cl_mvops->minor_version,
2785         };
2786
2787         encode_compound_hdr(xdr, req, &hdr);
2788         encode_create_session(xdr, args, &hdr);
2789         encode_nops(&hdr);
2790 }
2791
2792 /*
2793  * a DESTROY_SESSION request
2794  */
2795 static void nfs4_xdr_enc_destroy_session(struct rpc_rqst *req,
2796                                          struct xdr_stream *xdr,
2797                                          struct nfs4_session *session)
2798 {
2799         struct compound_hdr hdr = {
2800                 .minorversion = session->clp->cl_mvops->minor_version,
2801         };
2802
2803         encode_compound_hdr(xdr, req, &hdr);
2804         encode_destroy_session(xdr, session, &hdr);
2805         encode_nops(&hdr);
2806 }
2807
2808 /*
2809  * a DESTROY_CLIENTID request
2810  */
2811 static void nfs4_xdr_enc_destroy_clientid(struct rpc_rqst *req,
2812                                          struct xdr_stream *xdr,
2813                                          struct nfs_client *clp)
2814 {
2815         struct compound_hdr hdr = {
2816                 .minorversion = clp->cl_mvops->minor_version,
2817         };
2818
2819         encode_compound_hdr(xdr, req, &hdr);
2820         encode_destroy_clientid(xdr, clp->cl_clientid, &hdr);
2821         encode_nops(&hdr);
2822 }
2823
2824 /*
2825  * a SEQUENCE request
2826  */
2827 static void nfs4_xdr_enc_sequence(struct rpc_rqst *req, struct xdr_stream *xdr,
2828                                   struct nfs4_sequence_args *args)
2829 {
2830         struct compound_hdr hdr = {
2831                 .minorversion = nfs4_xdr_minorversion(args),
2832         };
2833
2834         encode_compound_hdr(xdr, req, &hdr);
2835         encode_sequence(xdr, args, &hdr);
2836         encode_nops(&hdr);
2837 }
2838
2839 /*
2840  * a GET_LEASE_TIME request
2841  */
2842 static void nfs4_xdr_enc_get_lease_time(struct rpc_rqst *req,
2843                                         struct xdr_stream *xdr,
2844                                         struct nfs4_get_lease_time_args *args)
2845 {
2846         struct compound_hdr hdr = {
2847                 .minorversion = nfs4_xdr_minorversion(&args->la_seq_args),
2848         };
2849         const u32 lease_bitmap[3] = { FATTR4_WORD0_LEASE_TIME };
2850
2851         encode_compound_hdr(xdr, req, &hdr);
2852         encode_sequence(xdr, &args->la_seq_args, &hdr);
2853         encode_putrootfh(xdr, &hdr);
2854         encode_fsinfo(xdr, lease_bitmap, &hdr);
2855         encode_nops(&hdr);
2856 }
2857
2858 /*
2859  * a RECLAIM_COMPLETE request
2860  */
2861 static void nfs4_xdr_enc_reclaim_complete(struct rpc_rqst *req,
2862                                           struct xdr_stream *xdr,
2863                                 struct nfs41_reclaim_complete_args *args)
2864 {
2865         struct compound_hdr hdr = {
2866                 .minorversion = nfs4_xdr_minorversion(&args->seq_args)
2867         };
2868
2869         encode_compound_hdr(xdr, req, &hdr);
2870         encode_sequence(xdr, &args->seq_args, &hdr);
2871         encode_reclaim_complete(xdr, args, &hdr);
2872         encode_nops(&hdr);
2873 }
2874
2875 /*
2876  * Encode GETDEVICEINFO request
2877  */
2878 static void nfs4_xdr_enc_getdeviceinfo(struct rpc_rqst *req,
2879                                        struct xdr_stream *xdr,
2880                                        struct nfs4_getdeviceinfo_args *args)
2881 {
2882         struct compound_hdr hdr = {
2883                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2884         };
2885
2886         encode_compound_hdr(xdr, req, &hdr);
2887         encode_sequence(xdr, &args->seq_args, &hdr);
2888         encode_getdeviceinfo(xdr, args, &hdr);
2889
2890         /* set up reply kvec. Subtract notification bitmap max size (2)
2891          * so that notification bitmap is put in xdr_buf tail */
2892         xdr_inline_pages(&req->rq_rcv_buf, (hdr.replen - 2) << 2,
2893                          args->pdev->pages, args->pdev->pgbase,
2894                          args->pdev->pglen);
2895
2896         encode_nops(&hdr);
2897 }
2898
2899 /*
2900  *  Encode LAYOUTGET request
2901  */
2902 static void nfs4_xdr_enc_layoutget(struct rpc_rqst *req,
2903                                    struct xdr_stream *xdr,
2904                                    struct nfs4_layoutget_args *args)
2905 {
2906         struct compound_hdr hdr = {
2907                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2908         };
2909
2910         encode_compound_hdr(xdr, req, &hdr);
2911         encode_sequence(xdr, &args->seq_args, &hdr);
2912         encode_putfh(xdr, NFS_FH(args->inode), &hdr);
2913         encode_layoutget(xdr, args, &hdr);
2914
2915         xdr_inline_pages(&req->rq_rcv_buf, hdr.replen << 2,
2916             args->layout.pages, 0, args->layout.pglen);
2917
2918         encode_nops(&hdr);
2919 }
2920
2921 /*
2922  *  Encode LAYOUTCOMMIT request
2923  */
2924 static void nfs4_xdr_enc_layoutcommit(struct rpc_rqst *req,
2925                                       struct xdr_stream *xdr,
2926                                       struct nfs4_layoutcommit_args *args)
2927 {
2928         struct nfs4_layoutcommit_data *data =
2929                 container_of(args, struct nfs4_layoutcommit_data, args);
2930         struct compound_hdr hdr = {
2931                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2932         };
2933
2934         encode_compound_hdr(xdr, req, &hdr);
2935         encode_sequence(xdr, &args->seq_args, &hdr);
2936         encode_putfh(xdr, NFS_FH(args->inode), &hdr);
2937         encode_layoutcommit(xdr, data->args.inode, args, &hdr);
2938         encode_getfattr(xdr, args->bitmask, &hdr);
2939         encode_nops(&hdr);
2940 }
2941
2942 /*
2943  * Encode LAYOUTRETURN request
2944  */
2945 static void nfs4_xdr_enc_layoutreturn(struct rpc_rqst *req,
2946                                       struct xdr_stream *xdr,
2947                                       struct nfs4_layoutreturn_args *args)
2948 {
2949         struct compound_hdr hdr = {
2950                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2951         };
2952
2953         encode_compound_hdr(xdr, req, &hdr);
2954         encode_sequence(xdr, &args->seq_args, &hdr);
2955         encode_putfh(xdr, NFS_FH(args->inode), &hdr);
2956         encode_layoutreturn(xdr, args, &hdr);
2957         encode_nops(&hdr);
2958 }
2959
2960 /*
2961  * Encode SECINFO_NO_NAME request
2962  */
2963 static int nfs4_xdr_enc_secinfo_no_name(struct rpc_rqst *req,
2964                                         struct xdr_stream *xdr,
2965                                         struct nfs41_secinfo_no_name_args *args)
2966 {
2967         struct compound_hdr hdr = {
2968                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2969         };
2970
2971         encode_compound_hdr(xdr, req, &hdr);
2972         encode_sequence(xdr, &args->seq_args, &hdr);
2973         encode_putrootfh(xdr, &hdr);
2974         encode_secinfo_no_name(xdr, args, &hdr);
2975         encode_nops(&hdr);
2976         return 0;
2977 }
2978
2979 /*
2980  *  Encode TEST_STATEID request
2981  */
2982 static void nfs4_xdr_enc_test_stateid(struct rpc_rqst *req,
2983                                       struct xdr_stream *xdr,
2984                                       struct nfs41_test_stateid_args *args)
2985 {
2986         struct compound_hdr hdr = {
2987                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2988         };
2989
2990         encode_compound_hdr(xdr, req, &hdr);
2991         encode_sequence(xdr, &args->seq_args, &hdr);
2992         encode_test_stateid(xdr, args, &hdr);
2993         encode_nops(&hdr);
2994 }
2995
2996 /*
2997  *  Encode FREE_STATEID request
2998  */
2999 static void nfs4_xdr_enc_free_stateid(struct rpc_rqst *req,
3000                                      struct xdr_stream *xdr,
3001                                      struct nfs41_free_stateid_args *args)
3002 {
3003         struct compound_hdr hdr = {
3004                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
3005         };
3006
3007         encode_compound_hdr(xdr, req, &hdr);
3008         encode_sequence(xdr, &args->seq_args, &hdr);
3009         encode_free_stateid(xdr, args, &hdr);
3010         encode_nops(&hdr);
3011 }
3012 #endif /* CONFIG_NFS_V4_1 */
3013
3014 static void print_overflow_msg(const char *func, const struct xdr_stream *xdr)
3015 {
3016         dprintk("nfs: %s: prematurely hit end of receive buffer. "
3017                 "Remaining buffer length is %tu words.\n",
3018                 func, xdr->end - xdr->p);
3019 }
3020
3021 static int decode_opaque_inline(struct xdr_stream *xdr, unsigned int *len, char **string)
3022 {
3023         __be32 *p;
3024
3025         p = xdr_inline_decode(xdr, 4);
3026         if (unlikely(!p))
3027                 goto out_overflow;
3028         *len = be32_to_cpup(p);
3029         p = xdr_inline_decode(xdr, *len);
3030         if (unlikely(!p))
3031                 goto out_overflow;
3032         *string = (char *)p;
3033         return 0;
3034 out_overflow:
3035         print_overflow_msg(__func__, xdr);
3036         return -EIO;
3037 }
3038
3039 static int decode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
3040 {
3041         __be32 *p;
3042
3043         p = xdr_inline_decode(xdr, 8);
3044         if (unlikely(!p))
3045                 goto out_overflow;
3046         hdr->status = be32_to_cpup(p++);
3047         hdr->taglen = be32_to_cpup(p);
3048
3049         p = xdr_inline_decode(xdr, hdr->taglen + 4);
3050         if (unlikely(!p))
3051                 goto out_overflow;
3052         hdr->tag = (char *)p;
3053         p += XDR_QUADLEN(hdr->taglen);
3054         hdr->nops = be32_to_cpup(p);
3055         if (unlikely(hdr->nops < 1))
3056                 return nfs4_stat_to_errno(hdr->status);
3057         return 0;
3058 out_overflow:
3059         print_overflow_msg(__func__, xdr);
3060         return -EIO;
3061 }
3062
3063 static bool __decode_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected,
3064                 int *nfs_retval)
3065 {
3066         __be32 *p;
3067         uint32_t opnum;
3068         int32_t nfserr;
3069
3070         p = xdr_inline_decode(xdr, 8);
3071         if (unlikely(!p))
3072                 goto out_overflow;
3073         opnum = be32_to_cpup(p++);
3074         if (unlikely(opnum != expected))
3075                 goto out_bad_operation;
3076         nfserr = be32_to_cpup(p);
3077         if (nfserr == NFS_OK)
3078                 *nfs_retval = 0;
3079         else
3080                 *nfs_retval = nfs4_stat_to_errno(nfserr);
3081         return true;
3082 out_bad_operation:
3083         dprintk("nfs: Server returned operation"
3084                 " %d but we issued a request for %d\n",
3085                         opnum, expected);
3086         *nfs_retval = -EREMOTEIO;
3087         return false;
3088 out_overflow:
3089         print_overflow_msg(__func__, xdr);
3090         *nfs_retval = -EIO;
3091         return false;
3092 }
3093
3094 static int decode_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
3095 {
3096         int retval;
3097
3098         __decode_op_hdr(xdr, expected, &retval);
3099         return retval;
3100 }
3101
3102 /* Dummy routine */
3103 static int decode_ace(struct xdr_stream *xdr, void *ace, struct nfs_client *clp)
3104 {
3105         __be32 *p;
3106         unsigned int strlen;
3107         char *str;
3108
3109         p = xdr_inline_decode(xdr, 12);
3110         if (likely(p))
3111                 return decode_opaque_inline(xdr, &strlen, &str);
3112         print_overflow_msg(__func__, xdr);
3113         return -EIO;
3114 }
3115
3116 static int decode_attr_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
3117 {
3118         uint32_t bmlen;
3119         __be32 *p;
3120
3121         p = xdr_inline_decode(xdr, 4);
3122         if (unlikely(!p))
3123                 goto out_overflow;
3124         bmlen = be32_to_cpup(p);
3125
3126         bitmap[0] = bitmap[1] = bitmap[2] = 0;
3127         p = xdr_inline_decode(xdr, (bmlen << 2));
3128         if (unlikely(!p))
3129                 goto out_overflow;
3130         if (bmlen > 0) {
3131                 bitmap[0] = be32_to_cpup(p++);
3132                 if (bmlen > 1) {
3133                         bitmap[1] = be32_to_cpup(p++);
3134                         if (bmlen > 2)
3135                                 bitmap[2] = be32_to_cpup(p);
3136                 }
3137         }
3138         return 0;
3139 out_overflow:
3140         print_overflow_msg(__func__, xdr);
3141         return -EIO;
3142 }
3143
3144 static int decode_attr_length(struct xdr_stream *xdr, uint32_t *attrlen, unsigned int *savep)
3145 {
3146         __be32 *p;
3147
3148         p = xdr_inline_decode(xdr, 4);
3149         if (unlikely(!p))
3150                 goto out_overflow;
3151         *attrlen = be32_to_cpup(p);
3152         *savep = xdr_stream_pos(xdr);
3153         return 0;
3154 out_overflow:
3155         print_overflow_msg(__func__, xdr);
3156         return -EIO;
3157 }
3158
3159 static int decode_attr_supported(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *bitmask)
3160 {
3161         if (likely(bitmap[0] & FATTR4_WORD0_SUPPORTED_ATTRS)) {
3162                 int ret;
3163                 ret = decode_attr_bitmap(xdr, bitmask);
3164                 if (unlikely(ret < 0))
3165                         return ret;
3166                 bitmap[0] &= ~FATTR4_WORD0_SUPPORTED_ATTRS;
3167         } else
3168                 bitmask[0] = bitmask[1] = bitmask[2] = 0;
3169         dprintk("%s: bitmask=%08x:%08x:%08x\n", __func__,
3170                 bitmask[0], bitmask[1], bitmask[2]);
3171         return 0;
3172 }
3173
3174 static int decode_attr_type(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *type)
3175 {
3176         __be32 *p;
3177         int ret = 0;
3178
3179         *type = 0;
3180         if (unlikely(bitmap[0] & (FATTR4_WORD0_TYPE - 1U)))
3181                 return -EIO;
3182         if (likely(bitmap[0] & FATTR4_WORD0_TYPE)) {
3183                 p = xdr_inline_decode(xdr, 4);
3184                 if (unlikely(!p))
3185                         goto out_overflow;
3186                 *type = be32_to_cpup(p);
3187                 if (*type < NF4REG || *type > NF4NAMEDATTR) {
3188                         dprintk("%s: bad type %d\n", __func__, *type);
3189                         return -EIO;
3190                 }
3191                 bitmap[0] &= ~FATTR4_WORD0_TYPE;
3192                 ret = NFS_ATTR_FATTR_TYPE;
3193         }
3194         dprintk("%s: type=0%o\n", __func__, nfs_type2fmt[*type]);
3195         return ret;
3196 out_overflow:
3197         print_overflow_msg(__func__, xdr);
3198         return -EIO;
3199 }
3200
3201 static int decode_attr_fh_expire_type(struct xdr_stream *xdr,
3202                                       uint32_t *bitmap, uint32_t *type)
3203 {
3204         __be32 *p;
3205
3206         *type = 0;
3207         if (unlikely(bitmap[0] & (FATTR4_WORD0_FH_EXPIRE_TYPE - 1U)))
3208                 return -EIO;
3209         if (likely(bitmap[0] & FATTR4_WORD0_FH_EXPIRE_TYPE)) {
3210                 p = xdr_inline_decode(xdr, 4);
3211                 if (unlikely(!p))
3212                         goto out_overflow;
3213                 *type = be32_to_cpup(p);
3214                 bitmap[0] &= ~FATTR4_WORD0_FH_EXPIRE_TYPE;
3215         }
3216         dprintk("%s: expire type=0x%x\n", __func__, *type);
3217         return 0;
3218 out_overflow:
3219         print_overflow_msg(__func__, xdr);
3220         return -EIO;
3221 }
3222
3223 static int decode_attr_change(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *change)
3224 {
3225         __be32 *p;
3226         int ret = 0;
3227
3228         *change = 0;
3229         if (unlikely(bitmap[0] & (FATTR4_WORD0_CHANGE - 1U)))
3230                 return -EIO;
3231         if (likely(bitmap[0] & FATTR4_WORD0_CHANGE)) {
3232                 p = xdr_inline_decode(xdr, 8);
3233                 if (unlikely(!p))
3234                         goto out_overflow;
3235                 xdr_decode_hyper(p, change);
3236                 bitmap[0] &= ~FATTR4_WORD0_CHANGE;
3237                 ret = NFS_ATTR_FATTR_CHANGE;
3238         }
3239         dprintk("%s: change attribute=%Lu\n", __func__,
3240                         (unsigned long long)*change);
3241         return ret;
3242 out_overflow:
3243         print_overflow_msg(__func__, xdr);
3244         return -EIO;
3245 }
3246
3247 static int decode_attr_size(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *size)
3248 {
3249         __be32 *p;
3250         int ret = 0;
3251
3252         *size = 0;
3253         if (unlikely(bitmap[0] & (FATTR4_WORD0_SIZE - 1U)))
3254                 return -EIO;
3255         if (likely(bitmap[0] & FATTR4_WORD0_SIZE)) {
3256                 p = xdr_inline_decode(xdr, 8);
3257                 if (unlikely(!p))
3258                         goto out_overflow;
3259                 xdr_decode_hyper(p, size);
3260                 bitmap[0] &= ~FATTR4_WORD0_SIZE;
3261                 ret = NFS_ATTR_FATTR_SIZE;
3262         }
3263         dprintk("%s: file size=%Lu\n", __func__, (unsigned long long)*size);
3264         return ret;
3265 out_overflow:
3266         print_overflow_msg(__func__, xdr);
3267         return -EIO;
3268 }
3269
3270 static int decode_attr_link_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
3271 {
3272         __be32 *p;
3273
3274         *res = 0;
3275         if (unlikely(bitmap[0] & (FATTR4_WORD0_LINK_SUPPORT - 1U)))
3276                 return -EIO;
3277         if (likely(bitmap[0] & FATTR4_WORD0_LINK_SUPPORT)) {
3278                 p = xdr_inline_decode(xdr, 4);
3279                 if (unlikely(!p))
3280                         goto out_overflow;
3281                 *res = be32_to_cpup(p);
3282                 bitmap[0] &= ~FATTR4_WORD0_LINK_SUPPORT;
3283         }
3284         dprintk("%s: link support=%s\n", __func__, *res == 0 ? "false" : "true");
3285         return 0;
3286 out_overflow:
3287         print_overflow_msg(__func__, xdr);
3288         return -EIO;
3289 }
3290
3291 static int decode_attr_symlink_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
3292 {
3293         __be32 *p;
3294
3295         *res = 0;
3296         if (unlikely(bitmap[0] & (FATTR4_WORD0_SYMLINK_SUPPORT - 1U)))
3297                 return -EIO;
3298         if (likely(bitmap[0] & FATTR4_WORD0_SYMLINK_SUPPORT)) {
3299                 p = xdr_inline_decode(xdr, 4);
3300                 if (unlikely(!p))
3301                         goto out_overflow;
3302                 *res = be32_to_cpup(p);
3303                 bitmap[0] &= ~FATTR4_WORD0_SYMLINK_SUPPORT;
3304         }
3305         dprintk("%s: symlink support=%s\n", __func__, *res == 0 ? "false" : "true");
3306         return 0;
3307 out_overflow:
3308         print_overflow_msg(__func__, xdr);
3309         return -EIO;
3310 }
3311
3312 static int decode_attr_fsid(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs_fsid *fsid)
3313 {
3314         __be32 *p;
3315         int ret = 0;
3316
3317         fsid->major = 0;
3318         fsid->minor = 0;
3319         if (unlikely(bitmap[0] & (FATTR4_WORD0_FSID - 1U)))
3320                 return -EIO;
3321         if (likely(bitmap[0] & FATTR4_WORD0_FSID)) {
3322                 p = xdr_inline_decode(xdr, 16);
3323                 if (unlikely(!p))
3324                         goto out_overflow;
3325                 p = xdr_decode_hyper(p, &fsid->major);
3326                 xdr_decode_hyper(p, &fsid->minor);
3327                 bitmap[0] &= ~FATTR4_WORD0_FSID;
3328                 ret = NFS_ATTR_FATTR_FSID;
3329         }
3330         dprintk("%s: fsid=(0x%Lx/0x%Lx)\n", __func__,
3331                         (unsigned long long)fsid->major,
3332                         (unsigned long long)fsid->minor);
3333         return ret;
3334 out_overflow:
3335         print_overflow_msg(__func__, xdr);
3336         return -EIO;
3337 }
3338
3339 static int decode_attr_lease_time(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
3340 {
3341         __be32 *p;
3342
3343         *res = 60;
3344         if (unlikely(bitmap[0] & (FATTR4_WORD0_LEASE_TIME - 1U)))
3345                 return -EIO;
3346         if (likely(bitmap[0] & FATTR4_WORD0_LEASE_TIME)) {
3347                 p = xdr_inline_decode(xdr, 4);
3348                 if (unlikely(!p))
3349                         goto out_overflow;
3350                 *res = be32_to_cpup(p);
3351                 bitmap[0] &= ~FATTR4_WORD0_LEASE_TIME;
3352         }
3353         dprintk("%s: file size=%u\n", __func__, (unsigned int)*res);
3354         return 0;
3355 out_overflow:
3356         print_overflow_msg(__func__, xdr);
3357         return -EIO;
3358 }
3359
3360 static int decode_attr_error(struct xdr_stream *xdr, uint32_t *bitmap, int32_t *res)
3361 {
3362         __be32 *p;
3363
3364         if (unlikely(bitmap[0] & (FATTR4_WORD0_RDATTR_ERROR - 1U)))
3365                 return -EIO;
3366         if (likely(bitmap[0] & FATTR4_WORD0_RDATTR_ERROR)) {
3367                 p = xdr_inline_decode(xdr, 4);
3368                 if (unlikely(!p))
3369                         goto out_overflow;
3370                 bitmap[0] &= ~FATTR4_WORD0_RDATTR_ERROR;
3371                 *res = -be32_to_cpup(p);
3372         }
3373         return 0;
3374 out_overflow:
3375         print_overflow_msg(__func__, xdr);
3376         return -EIO;
3377 }
3378
3379 static int decode_attr_filehandle(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs_fh *fh)
3380 {
3381         __be32 *p;
3382         int len;
3383
3384         if (fh != NULL)
3385                 memset(fh, 0, sizeof(*fh));
3386
3387         if (unlikely(bitmap[0] & (FATTR4_WORD0_FILEHANDLE - 1U)))
3388                 return -EIO;
3389         if (likely(bitmap[0] & FATTR4_WORD0_FILEHANDLE)) {
3390                 p = xdr_inline_decode(xdr, 4);
3391                 if (unlikely(!p))
3392                         goto out_overflow;
3393                 len = be32_to_cpup(p);
3394                 if (len > NFS4_FHSIZE)
3395                         return -EIO;
3396                 p = xdr_inline_decode(xdr, len);
3397                 if (unlikely(!p))
3398                         goto out_overflow;
3399                 if (fh != NULL) {
3400                         memcpy(fh->data, p, len);
3401                         fh->size = len;
3402                 }
3403                 bitmap[0] &= ~FATTR4_WORD0_FILEHANDLE;
3404         }
3405         return 0;
3406 out_overflow:
3407         print_overflow_msg(__func__, xdr);
3408         return -EIO;
3409 }
3410
3411 static int decode_attr_aclsupport(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
3412 {
3413         __be32 *p;
3414
3415         *res = 0;
3416         if (unlikely(bitmap[0] & (FATTR4_WORD0_ACLSUPPORT - 1U)))
3417                 return -EIO;
3418         if (likely(bitmap[0] & FATTR4_WORD0_ACLSUPPORT)) {
3419                 p = xdr_inline_decode(xdr, 4);
3420                 if (unlikely(!p))
3421                         goto out_overflow;
3422                 *res = be32_to_cpup(p);
3423                 bitmap[0] &= ~FATTR4_WORD0_ACLSUPPORT;
3424         }
3425         dprintk("%s: ACLs supported=%u\n", __func__, (unsigned int)*res);
3426         return 0;
3427 out_overflow:
3428         print_overflow_msg(__func__, xdr);
3429         return -EIO;
3430 }
3431
3432 static int decode_attr_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
3433 {
3434         __be32 *p;
3435         int ret = 0;
3436
3437         *fileid = 0;
3438         if (unlikely(bitmap[0] & (FATTR4_WORD0_FILEID - 1U)))
3439                 return -EIO;
3440         if (likely(bitmap[0] & FATTR4_WORD0_FILEID)) {
3441                 p = xdr_inline_decode(xdr, 8);
3442                 if (unlikely(!p))
3443                         goto out_overflow;
3444                 xdr_decode_hyper(p, fileid);
3445                 bitmap[0] &= ~FATTR4_WORD0_FILEID;
3446                 ret = NFS_ATTR_FATTR_FILEID;
3447         }
3448         dprintk("%s: fileid=%Lu\n", __func__, (unsigned long long)*fileid);
3449         return ret;
3450 out_overflow:
3451         print_overflow_msg(__func__, xdr);
3452         return -EIO;
3453 }
3454
3455 static int decode_attr_mounted_on_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
3456 {
3457         __be32 *p;
3458         int ret = 0;
3459
3460         *fileid = 0;
3461         if (unlikely(bitmap[1] & (FATTR4_WORD1_MOUNTED_ON_FILEID - 1U)))
3462                 return -EIO;
3463         if (likely(bitmap[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)) {
3464                 p = xdr_inline_decode(xdr, 8);
3465                 if (unlikely(!p))
3466                         goto out_overflow;
3467                 xdr_decode_hyper(p, fileid);
3468                 bitmap[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
3469                 ret = NFS_ATTR_FATTR_MOUNTED_ON_FILEID;
3470         }
3471         dprintk("%s: fileid=%Lu\n", __func__, (unsigned long long)*fileid);
3472         return ret;
3473 out_overflow:
3474         print_overflow_msg(__func__, xdr);
3475         return -EIO;
3476 }
3477
3478 static int decode_attr_files_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
3479 {
3480         __be32 *p;
3481         int status = 0;
3482
3483         *res = 0;
3484         if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_AVAIL - 1U)))
3485                 return -EIO;
3486         if (likely(bitmap[0] & FATTR4_WORD0_FILES_AVAIL)) {
3487                 p = xdr_inline_decode(xdr, 8);
3488                 if (unlikely(!p))
3489                         goto out_overflow;
3490                 xdr_decode_hyper(p, res);
3491                 bitmap[0] &= ~FATTR4_WORD0_FILES_AVAIL;
3492         }
3493         dprintk("%s: files avail=%Lu\n", __func__, (unsigned long long)*res);
3494         return status;
3495 out_overflow:
3496         print_overflow_msg(__func__, xdr);
3497         return -EIO;
3498 }
3499
3500 static int decode_attr_files_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
3501 {
3502         __be32 *p;
3503         int status = 0;
3504
3505         *res = 0;
3506         if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_FREE - 1U)))
3507                 return -EIO;
3508         if (likely(bitmap[0] & FATTR4_WORD0_FILES_FREE)) {
3509                 p = xdr_inline_decode(xdr, 8);
3510                 if (unlikely(!p))
3511                         goto out_overflow;
3512                 xdr_decode_hyper(p, res);
3513                 bitmap[0] &= ~FATTR4_WORD0_FILES_FREE;
3514         }
3515         dprintk("%s: files free=%Lu\n", __func__, (unsigned long long)*res);
3516         return status;
3517 out_overflow:
3518         print_overflow_msg(__func__, xdr);
3519         return -EIO;
3520 }
3521
3522 static int decode_attr_files_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
3523 {
3524         __be32 *p;
3525         int status = 0;
3526
3527         *res = 0;
3528         if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_TOTAL - 1U)))
3529                 return -EIO;
3530         if (likely(bitmap[0] & FATTR4_WORD0_FILES_TOTAL)) {
3531                 p = xdr_inline_decode(xdr, 8);
3532                 if (unlikely(!p))
3533                         goto out_overflow;
3534                 xdr_decode_hyper(p, res);
3535                 bitmap[0] &= ~FATTR4_WORD0_FILES_TOTAL;
3536         }
3537         dprintk("%s: files total=%Lu\n", __func__, (unsigned long long)*res);
3538         return status;
3539 out_overflow:
3540         print_overflow_msg(__func__, xdr);
3541         return -EIO;
3542 }
3543
3544 static int decode_pathname(struct xdr_stream *xdr, struct nfs4_pathname *path)
3545 {
3546         u32 n;
3547         __be32 *p;
3548         int status = 0;
3549
3550         p = xdr_inline_decode(xdr, 4);
3551         if (unlikely(!p))
3552                 goto out_overflow;
3553         n = be32_to_cpup(p);
3554         if (n == 0)
3555                 goto root_path;
3556         dprintk("pathname4: ");
3557         if (n > NFS4_PATHNAME_MAXCOMPONENTS) {
3558                 dprintk("cannot parse %d components in path\n", n);
3559                 goto out_eio;
3560         }
3561         for (path->ncomponents = 0; path->ncomponents < n; path->ncomponents++) {
3562                 struct nfs4_string *component = &path->components[path->ncomponents];
3563                 status = decode_opaque_inline(xdr, &component->len, &component->data);
3564                 if (unlikely(status != 0))
3565                         goto out_eio;
3566                 ifdebug (XDR)
3567                         pr_cont("%s%.*s ",
3568                                 (path->ncomponents != n ? "/ " : ""),
3569                                 component->len, component->data);
3570         }
3571 out:
3572         return status;
3573 root_path:
3574 /* a root pathname is sent as a zero component4 */
3575         path->ncomponents = 1;
3576         path->components[0].len=0;
3577         path->components[0].data=NULL;
3578         dprintk("pathname4: /\n");
3579         goto out;
3580 out_eio:
3581         dprintk(" status %d", status);
3582         status = -EIO;
3583         goto out;
3584 out_overflow:
3585         print_overflow_msg(__func__, xdr);
3586         return -EIO;
3587 }
3588
3589 static int decode_attr_fs_locations(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_fs_locations *res)
3590 {
3591         int n;
3592         __be32 *p;
3593         int status = -EIO;
3594
3595         if (unlikely(bitmap[0] & (FATTR4_WORD0_FS_LOCATIONS -1U)))
3596                 goto out;
3597         status = 0;
3598         if (unlikely(!(bitmap[0] & FATTR4_WORD0_FS_LOCATIONS)))
3599                 goto out;
3600         status = -EIO;
3601         /* Ignore borken servers that return unrequested attrs */
3602         if (unlikely(res == NULL))
3603                 goto out;
3604         dprintk("%s: fsroot:\n", __func__);
3605         status = decode_pathname(xdr, &res->fs_path);
3606         if (unlikely(status != 0))
3607                 goto out;
3608         p = xdr_inline_decode(xdr, 4);
3609         if (unlikely(!p))
3610                 goto out_overflow;
3611         n = be32_to_cpup(p);
3612         if (n <= 0)
3613                 goto out_eio;
3614         for (res->nlocations = 0; res->nlocations < n; res->nlocations++) {
3615                 u32 m;
3616                 struct nfs4_fs_location *loc;
3617
3618                 if (res->nlocations == NFS4_FS_LOCATIONS_MAXENTRIES)
3619                         break;
3620                 loc = &res->locations[res->nlocations];
3621                 p = xdr_inline_decode(xdr, 4);
3622                 if (unlikely(!p))
3623                         goto out_overflow;
3624                 m = be32_to_cpup(p);
3625
3626                 dprintk("%s: servers:\n", __func__);
3627                 for (loc->nservers = 0; loc->nservers < m; loc->nservers++) {
3628                         struct nfs4_string *server;
3629
3630                         if (loc->nservers == NFS4_FS_LOCATION_MAXSERVERS) {
3631                                 unsigned int i;
3632                                 dprintk("%s: using first %u of %u servers "
3633                                         "returned for location %u\n",
3634                                                 __func__,
3635                                                 NFS4_FS_LOCATION_MAXSERVERS,
3636                                                 m, res->nlocations);
3637                                 for (i = loc->nservers; i < m; i++) {
3638                                         unsigned int len;
3639                                         char *data;
3640                                         status = decode_opaque_inline(xdr, &len, &data);
3641                                         if (unlikely(status != 0))
3642                                                 goto out_eio;
3643                                 }
3644                                 break;
3645                         }
3646                         server = &loc->servers[loc->nservers];
3647                         status = decode_opaque_inline(xdr, &server->len, &server->data);
3648                         if (unlikely(status != 0))
3649                                 goto out_eio;
3650                         dprintk("%s ", server->data);
3651                 }
3652                 status = decode_pathname(xdr, &loc->rootpath);
3653                 if (unlikely(status != 0))
3654                         goto out_eio;
3655         }
3656         if (res->nlocations != 0)
3657                 status = NFS_ATTR_FATTR_V4_LOCATIONS;
3658 out:
3659         dprintk("%s: fs_locations done, error = %d\n", __func__, status);
3660         return status;
3661 out_overflow:
3662         print_overflow_msg(__func__, xdr);
3663 out_eio:
3664         status = -EIO;
3665         goto out;
3666 }
3667
3668 static int decode_attr_maxfilesize(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
3669 {
3670         __be32 *p;
3671         int status = 0;
3672
3673         *res = 0;
3674         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXFILESIZE - 1U)))
3675                 return -EIO;
3676         if (likely(bitmap[0] & FATTR4_WORD0_MAXFILESIZE)) {
3677                 p = xdr_inline_decode(xdr, 8);
3678                 if (unlikely(!p))
3679                         goto out_overflow;
3680                 xdr_decode_hyper(p, res);
3681                 bitmap[0] &= ~FATTR4_WORD0_MAXFILESIZE;
3682         }
3683         dprintk("%s: maxfilesize=%Lu\n", __func__, (unsigned long long)*res);
3684         return status;
3685 out_overflow:
3686         print_overflow_msg(__func__, xdr);
3687         return -EIO;
3688 }
3689
3690 static int decode_attr_maxlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxlink)
3691 {
3692         __be32 *p;
3693         int status = 0;
3694
3695         *maxlink = 1;
3696         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXLINK - 1U)))
3697                 return -EIO;
3698         if (likely(bitmap[0] & FATTR4_WORD0_MAXLINK)) {
3699                 p = xdr_inline_decode(xdr, 4);
3700                 if (unlikely(!p))
3701                         goto out_overflow;
3702                 *maxlink = be32_to_cpup(p);
3703                 bitmap[0] &= ~FATTR4_WORD0_MAXLINK;
3704         }
3705         dprintk("%s: maxlink=%u\n", __func__, *maxlink);
3706         return status;
3707 out_overflow:
3708         print_overflow_msg(__func__, xdr);
3709         return -EIO;
3710 }
3711
3712 static int decode_attr_maxname(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxname)
3713 {
3714         __be32 *p;
3715         int status = 0;
3716
3717         *maxname = 1024;
3718         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXNAME - 1U)))
3719                 return -EIO;
3720         if (likely(bitmap[0] & FATTR4_WORD0_MAXNAME)) {
3721                 p = xdr_inline_decode(xdr, 4);
3722                 if (unlikely(!p))
3723                         goto out_overflow;
3724                 *maxname = be32_to_cpup(p);
3725                 bitmap[0] &= ~FATTR4_WORD0_MAXNAME;
3726         }
3727         dprintk("%s: maxname=%u\n", __func__, *maxname);
3728         return status;
3729 out_overflow:
3730         print_overflow_msg(__func__, xdr);
3731         return -EIO;
3732 }
3733
3734 static int decode_attr_maxread(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
3735 {
3736         __be32 *p;
3737         int status = 0;
3738
3739         *res = 1024;
3740         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXREAD - 1U)))
3741                 return -EIO;
3742         if (likely(bitmap[0] & FATTR4_WORD0_MAXREAD)) {
3743                 uint64_t maxread;
3744                 p = xdr_inline_decode(xdr, 8);
3745                 if (unlikely(!p))
3746                         goto out_overflow;
3747                 xdr_decode_hyper(p, &maxread);
3748                 if (maxread > 0x7FFFFFFF)
3749                         maxread = 0x7FFFFFFF;
3750                 *res = (uint32_t)maxread;
3751                 bitmap[0] &= ~FATTR4_WORD0_MAXREAD;
3752         }
3753         dprintk("%s: maxread=%lu\n", __func__, (unsigned long)*res);
3754         return status;
3755 out_overflow:
3756         print_overflow_msg(__func__, xdr);
3757         return -EIO;
3758 }
3759
3760 static int decode_attr_maxwrite(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
3761 {
3762         __be32 *p;
3763         int status = 0;
3764
3765         *res = 1024;
3766         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXWRITE - 1U)))
3767                 return -EIO;
3768         if (likely(bitmap[0] & FATTR4_WORD0_MAXWRITE)) {
3769                 uint64_t maxwrite;
3770                 p = xdr_inline_decode(xdr, 8);
3771                 if (unlikely(!p))
3772                         goto out_overflow;
3773                 xdr_decode_hyper(p, &maxwrite);
3774                 if (maxwrite > 0x7FFFFFFF)
3775                         maxwrite = 0x7FFFFFFF;
3776                 *res = (uint32_t)maxwrite;
3777                 bitmap[0] &= ~FATTR4_WORD0_MAXWRITE;
3778         }
3779         dprintk("%s: maxwrite=%lu\n", __func__, (unsigned long)*res);
3780         return status;
3781 out_overflow:
3782         print_overflow_msg(__func__, xdr);
3783         return -EIO;
3784 }
3785
3786 static int decode_attr_mode(struct xdr_stream *xdr, uint32_t *bitmap, umode_t *mode)
3787 {
3788         uint32_t tmp;
3789         __be32 *p;
3790         int ret = 0;
3791
3792         *mode = 0;
3793         if (unlikely(bitmap[1] & (FATTR4_WORD1_MODE - 1U)))
3794                 return -EIO;
3795         if (likely(bitmap[1] & FATTR4_WORD1_MODE)) {
3796                 p = xdr_inline_decode(xdr, 4);
3797                 if (unlikely(!p))
3798                         goto out_overflow;
3799                 tmp = be32_to_cpup(p);
3800                 *mode = tmp & ~S_IFMT;
3801                 bitmap[1] &= ~FATTR4_WORD1_MODE;
3802                 ret = NFS_ATTR_FATTR_MODE;
3803         }
3804         dprintk("%s: file mode=0%o\n", __func__, (unsigned int)*mode);
3805         return ret;
3806 out_overflow:
3807         print_overflow_msg(__func__, xdr);
3808         return -EIO;
3809 }
3810
3811 static int decode_attr_nlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *nlink)
3812 {
3813         __be32 *p;
3814         int ret = 0;
3815
3816         *nlink = 1;
3817         if (unlikely(bitmap[1] & (FATTR4_WORD1_NUMLINKS - 1U)))
3818                 return -EIO;
3819         if (likely(bitmap[1] & FATTR4_WORD1_NUMLINKS)) {
3820                 p = xdr_inline_decode(xdr, 4);
3821                 if (unlikely(!p))
3822                         goto out_overflow;
3823                 *nlink = be32_to_cpup(p);
3824                 bitmap[1] &= ~FATTR4_WORD1_NUMLINKS;
3825                 ret = NFS_ATTR_FATTR_NLINK;
3826         }
3827         dprintk("%s: nlink=%u\n", __func__, (unsigned int)*nlink);
3828         return ret;
3829 out_overflow:
3830         print_overflow_msg(__func__, xdr);
3831         return -EIO;
3832 }
3833
3834 static int decode_attr_owner(struct xdr_stream *xdr, uint32_t *bitmap,
3835                 const struct nfs_server *server, kuid_t *uid,
3836                 struct nfs4_string *owner_name)
3837 {
3838         uint32_t len;
3839         __be32 *p;
3840         int ret = 0;
3841
3842         *uid = make_kuid(&init_user_ns, -2);
3843         if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER - 1U)))
3844                 return -EIO;
3845         if (likely(bitmap[1] & FATTR4_WORD1_OWNER)) {
3846                 p = xdr_inline_decode(xdr, 4);
3847                 if (unlikely(!p))
3848                         goto out_overflow;
3849                 len = be32_to_cpup(p);
3850                 p = xdr_inline_decode(xdr, len);
3851                 if (unlikely(!p))
3852                         goto out_overflow;
3853                 if (owner_name != NULL) {
3854                         owner_name->data = kmemdup(p, len, GFP_NOWAIT);
3855                         if (owner_name->data != NULL) {
3856                                 owner_name->len = len;
3857                                 ret = NFS_ATTR_FATTR_OWNER_NAME;
3858                         }
3859                 } else if (len < XDR_MAX_NETOBJ) {
3860                         if (nfs_map_name_to_uid(server, (char *)p, len, uid) == 0)
3861                                 ret = NFS_ATTR_FATTR_OWNER;
3862                         else
3863                                 dprintk("%s: nfs_map_name_to_uid failed!\n",
3864                                                 __func__);
3865                 } else
3866                         dprintk("%s: name too long (%u)!\n",
3867                                         __func__, len);
3868                 bitmap[1] &= ~FATTR4_WORD1_OWNER;
3869         }
3870         dprintk("%s: uid=%d\n", __func__, (int)from_kuid(&init_user_ns, *uid));
3871         return ret;
3872 out_overflow:
3873         print_overflow_msg(__func__, xdr);
3874         return -EIO;
3875 }
3876
3877 static int decode_attr_group(struct xdr_stream *xdr, uint32_t *bitmap,
3878                 const struct nfs_server *server, kgid_t *gid,
3879                 struct nfs4_string *group_name)
3880 {
3881         uint32_t len;
3882         __be32 *p;
3883         int ret = 0;
3884
3885         *gid = make_kgid(&init_user_ns, -2);
3886         if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER_GROUP - 1U)))
3887                 return -EIO;
3888         if (likely(bitmap[1] & FATTR4_WORD1_OWNER_GROUP)) {
3889                 p = xdr_inline_decode(xdr, 4);
3890                 if (unlikely(!p))
3891                         goto out_overflow;
3892                 len = be32_to_cpup(p);
3893                 p = xdr_inline_decode(xdr, len);
3894                 if (unlikely(!p))
3895                         goto out_overflow;
3896                 if (group_name != NULL) {
3897                         group_name->data = kmemdup(p, len, GFP_NOWAIT);
3898                         if (group_name->data != NULL) {
3899                                 group_name->len = len;
3900                                 ret = NFS_ATTR_FATTR_GROUP_NAME;
3901                         }
3902                 } else if (len < XDR_MAX_NETOBJ) {
3903                         if (nfs_map_group_to_gid(server, (char *)p, len, gid) == 0)
3904                                 ret = NFS_ATTR_FATTR_GROUP;
3905                         else
3906                                 dprintk("%s: nfs_map_group_to_gid failed!\n",
3907                                                 __func__);
3908                 } else
3909                         dprintk("%s: name too long (%u)!\n",
3910                                         __func__, len);
3911                 bitmap[1] &= ~FATTR4_WORD1_OWNER_GROUP;
3912         }
3913         dprintk("%s: gid=%d\n", __func__, (int)from_kgid(&init_user_ns, *gid));
3914         return ret;
3915 out_overflow:
3916         print_overflow_msg(__func__, xdr);
3917         return -EIO;
3918 }
3919
3920 static int decode_attr_rdev(struct xdr_stream *xdr, uint32_t *bitmap, dev_t *rdev)
3921 {
3922         uint32_t major = 0, minor = 0;
3923         __be32 *p;
3924         int ret = 0;
3925
3926         *rdev = MKDEV(0,0);
3927         if (unlikely(bitmap[1] & (FATTR4_WORD1_RAWDEV - 1U)))
3928                 return -EIO;
3929         if (likely(bitmap[1] & FATTR4_WORD1_RAWDEV)) {
3930                 dev_t tmp;
3931
3932                 p = xdr_inline_decode(xdr, 8);
3933                 if (unlikely(!p))
3934                         goto out_overflow;
3935                 major = be32_to_cpup(p++);
3936                 minor = be32_to_cpup(p);
3937                 tmp = MKDEV(major, minor);
3938                 if (MAJOR(tmp) == major && MINOR(tmp) == minor)
3939                         *rdev = tmp;
3940                 bitmap[1] &= ~ FATTR4_WORD1_RAWDEV;
3941                 ret = NFS_ATTR_FATTR_RDEV;
3942         }
3943         dprintk("%s: rdev=(0x%x:0x%x)\n", __func__, major, minor);
3944         return ret;
3945 out_overflow:
3946         print_overflow_msg(__func__, xdr);
3947         return -EIO;
3948 }
3949
3950 static int decode_attr_space_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
3951 {
3952         __be32 *p;
3953         int status = 0;
3954
3955         *res = 0;
3956         if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_AVAIL - 1U)))
3957                 return -EIO;
3958         if (likely(bitmap[1] & FATTR4_WORD1_SPACE_AVAIL)) {
3959                 p = xdr_inline_decode(xdr, 8);
3960                 if (unlikely(!p))
3961                         goto out_overflow;
3962                 xdr_decode_hyper(p, res);
3963                 bitmap[1] &= ~FATTR4_WORD1_SPACE_AVAIL;
3964         }
3965         dprintk("%s: space avail=%Lu\n", __func__, (unsigned long long)*res);
3966         return status;
3967 out_overflow:
3968         print_overflow_msg(__func__, xdr);
3969         return -EIO;
3970 }
3971
3972 static int decode_attr_space_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
3973 {
3974         __be32 *p;
3975         int status = 0;
3976
3977         *res = 0;
3978         if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_FREE - 1U)))
3979                 return -EIO;
3980         if (likely(bitmap[1] & FATTR4_WORD1_SPACE_FREE)) {
3981                 p = xdr_inline_decode(xdr, 8);
3982                 if (unlikely(!p))
3983                         goto out_overflow;
3984                 xdr_decode_hyper(p, res);
3985                 bitmap[1] &= ~FATTR4_WORD1_SPACE_FREE;
3986         }
3987         dprintk("%s: space free=%Lu\n", __func__, (unsigned long long)*res);
3988         return status;
3989 out_overflow:
3990         print_overflow_msg(__func__, xdr);
3991         return -EIO;
3992 }
3993
3994 static int decode_attr_space_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
3995 {
3996         __be32 *p;
3997         int status = 0;
3998
3999         *res = 0;
4000         if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_TOTAL - 1U)))
4001                 return -EIO;
4002         if (likely(bitmap[1] & FATTR4_WORD1_SPACE_TOTAL)) {
4003                 p = xdr_inline_decode(xdr, 8);
4004                 if (unlikely(!p))
4005                         goto out_overflow;
4006                 xdr_decode_hyper(p, res);
4007                 bitmap[1] &= ~FATTR4_WORD1_SPACE_TOTAL;
4008         }
4009         dprintk("%s: space total=%Lu\n", __func__, (unsigned long long)*res);
4010         return status;
4011 out_overflow:
4012         print_overflow_msg(__func__, xdr);
4013         return -EIO;
4014 }
4015
4016 static int decode_attr_space_used(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *used)
4017 {
4018         __be32 *p;
4019         int ret = 0;
4020
4021         *used = 0;
4022         if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_USED - 1U)))
4023                 return -EIO;
4024         if (likely(bitmap[1] & FATTR4_WORD1_SPACE_USED)) {
4025                 p = xdr_inline_decode(xdr, 8);
4026                 if (unlikely(!p))
4027                         goto out_overflow;
4028                 xdr_decode_hyper(p, used);
4029                 bitmap[1] &= ~FATTR4_WORD1_SPACE_USED;
4030                 ret = NFS_ATTR_FATTR_SPACE_USED;
4031         }
4032         dprintk("%s: space used=%Lu\n", __func__,
4033                         (unsigned long long)*used);
4034         return ret;
4035 out_overflow:
4036         print_overflow_msg(__func__, xdr);
4037         return -EIO;
4038 }
4039
4040 static int decode_attr_time(struct xdr_stream *xdr, struct timespec *time)
4041 {
4042         __be32 *p;
4043         uint64_t sec;
4044         uint32_t nsec;
4045
4046         p = xdr_inline_decode(xdr, 12);
4047         if (unlikely(!p))
4048                 goto out_overflow;
4049         p = xdr_decode_hyper(p, &sec);
4050         nsec = be32_to_cpup(p);
4051         time->tv_sec = (time_t)sec;
4052         time->tv_nsec = (long)nsec;
4053         return 0;
4054 out_overflow:
4055         print_overflow_msg(__func__, xdr);
4056         return -EIO;
4057 }
4058
4059 static int decode_attr_time_access(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
4060 {
4061         int status = 0;
4062
4063         time->tv_sec = 0;
4064         time->tv_nsec = 0;
4065         if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_ACCESS - 1U)))
4066                 return -EIO;
4067         if (likely(bitmap[1] & FATTR4_WORD1_TIME_ACCESS)) {
4068                 status = decode_attr_time(xdr, time);
4069                 if (status == 0)
4070                         status = NFS_ATTR_FATTR_ATIME;
4071                 bitmap[1] &= ~FATTR4_WORD1_TIME_ACCESS;
4072         }
4073         dprintk("%s: atime=%ld\n", __func__, (long)time->tv_sec);
4074         return status;
4075 }
4076
4077 static int decode_attr_time_metadata(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
4078 {
4079         int status = 0;
4080
4081         time->tv_sec = 0;
4082         time->tv_nsec = 0;
4083         if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_METADATA - 1U)))
4084                 return -EIO;
4085         if (likely(bitmap[1] & FATTR4_WORD1_TIME_METADATA)) {
4086                 status = decode_attr_time(xdr, time);
4087                 if (status == 0)
4088                         status = NFS_ATTR_FATTR_CTIME;
4089                 bitmap[1] &= ~FATTR4_WORD1_TIME_METADATA;
4090         }
4091         dprintk("%s: ctime=%ld\n", __func__, (long)time->tv_sec);
4092         return status;
4093 }
4094
4095 static int decode_attr_time_delta(struct xdr_stream *xdr, uint32_t *bitmap,
4096                                   struct timespec *time)
4097 {
4098         int status = 0;
4099
4100         time->tv_sec = 0;
4101         time->tv_nsec = 0;
4102         if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_DELTA - 1U)))
4103                 return -EIO;
4104         if (likely(bitmap[1] & FATTR4_WORD1_TIME_DELTA)) {
4105                 status = decode_attr_time(xdr, time);
4106                 bitmap[1] &= ~FATTR4_WORD1_TIME_DELTA;
4107         }
4108         dprintk("%s: time_delta=%ld %ld\n", __func__, (long)time->tv_sec,
4109                 (long)time->tv_nsec);
4110         return status;
4111 }
4112
4113 static int decode_attr_security_label(struct xdr_stream *xdr, uint32_t *bitmap,
4114                                         struct nfs4_label *label)
4115 {
4116         uint32_t pi = 0;
4117         uint32_t lfs = 0;
4118         __u32 len;
4119         __be32 *p;
4120         int status = 0;
4121
4122         if (unlikely(bitmap[2] & (FATTR4_WORD2_SECURITY_LABEL - 1U)))
4123                 return -EIO;
4124         if (likely(bitmap[2] & FATTR4_WORD2_SECURITY_LABEL)) {
4125                 p = xdr_inline_decode(xdr, 4);
4126                 if (unlikely(!p))
4127                         goto out_overflow;
4128                 lfs = be32_to_cpup(p++);
4129                 p = xdr_inline_decode(xdr, 4);
4130                 if (unlikely(!p))
4131                         goto out_overflow;
4132                 pi = be32_to_cpup(p++);
4133                 p = xdr_inline_decode(xdr, 4);
4134                 if (unlikely(!p))
4135                         goto out_overflow;
4136                 len = be32_to_cpup(p++);
4137                 p = xdr_inline_decode(xdr, len);
4138                 if (unlikely(!p))
4139                         goto out_overflow;
4140                 if (len < NFS4_MAXLABELLEN) {
4141                         if (label) {
4142                                 memcpy(label->label, p, len);
4143                                 label->len = len;
4144                                 label->pi = pi;
4145                                 label->lfs = lfs;
4146                                 status = NFS_ATTR_FATTR_V4_SECURITY_LABEL;
4147                         }
4148                         bitmap[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
4149                 } else
4150                         printk(KERN_WARNING "%s: label too long (%u)!\n",
4151                                         __func__, len);
4152         }
4153         if (label && label->label)
4154                 dprintk("%s: label=%s, len=%d, PI=%d, LFS=%d\n", __func__,
4155                         (char *)label->label, label->len, label->pi, label->lfs);
4156         return status;
4157
4158 out_overflow:
4159         print_overflow_msg(__func__, xdr);
4160         return -EIO;
4161 }
4162
4163 static int decode_attr_time_modify(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
4164 {
4165         int status = 0;
4166
4167         time->tv_sec = 0;
4168         time->tv_nsec = 0;
4169         if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_MODIFY - 1U)))
4170                 return -EIO;
4171         if (likely(bitmap[1] & FATTR4_WORD1_TIME_MODIFY)) {
4172                 status = decode_attr_time(xdr, time);
4173                 if (status == 0)
4174                         status = NFS_ATTR_FATTR_MTIME;
4175                 bitmap[1] &= ~FATTR4_WORD1_TIME_MODIFY;
4176         }
4177         dprintk("%s: mtime=%ld\n", __func__, (long)time->tv_sec);
4178         return status;
4179 }
4180
4181 static int verify_attr_len(struct xdr_stream *xdr, unsigned int savep, uint32_t attrlen)
4182 {
4183         unsigned int attrwords = XDR_QUADLEN(attrlen);
4184         unsigned int nwords = (xdr_stream_pos(xdr) - savep) >> 2;
4185
4186         if (unlikely(attrwords != nwords)) {
4187                 dprintk("%s: server returned incorrect attribute length: "
4188                         "%u %c %u\n",
4189                                 __func__,
4190                                 attrwords << 2,
4191                                 (attrwords < nwords) ? '<' : '>',
4192                                 nwords << 2);
4193                 return -EIO;
4194         }
4195         return 0;
4196 }
4197
4198 static int decode_change_info(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
4199 {
4200         __be32 *p;
4201
4202         p = xdr_inline_decode(xdr, 20);
4203         if (unlikely(!p))
4204                 goto out_overflow;
4205         cinfo->atomic = be32_to_cpup(p++);
4206         p = xdr_decode_hyper(p, &cinfo->before);
4207         xdr_decode_hyper(p, &cinfo->after);
4208         return 0;
4209 out_overflow:
4210         print_overflow_msg(__func__, xdr);
4211         return -EIO;
4212 }
4213
4214 static int decode_access(struct xdr_stream *xdr, u32 *supported, u32 *access)
4215 {
4216         __be32 *p;
4217         uint32_t supp, acc;
4218         int status;
4219
4220         status = decode_op_hdr(xdr, OP_ACCESS);
4221         if (status)
4222                 return status;
4223         p = xdr_inline_decode(xdr, 8);
4224         if (unlikely(!p))
4225                 goto out_overflow;
4226         supp = be32_to_cpup(p++);
4227         acc = be32_to_cpup(p);
4228         *supported = supp;
4229         *access = acc;
4230         return 0;
4231 out_overflow:
4232         print_overflow_msg(__func__, xdr);
4233         return -EIO;
4234 }
4235
4236 static int decode_opaque_fixed(struct xdr_stream *xdr, void *buf, size_t len)
4237 {
4238         __be32 *p;
4239
4240         p = xdr_inline_decode(xdr, len);
4241         if (likely(p)) {
4242                 memcpy(buf, p, len);
4243                 return 0;
4244         }
4245         print_overflow_msg(__func__, xdr);
4246         return -EIO;
4247 }
4248
4249 static int decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
4250 {
4251         return decode_opaque_fixed(xdr, stateid, NFS4_STATEID_SIZE);
4252 }
4253
4254 static int decode_close(struct xdr_stream *xdr, struct nfs_closeres *res)
4255 {
4256         int status;
4257
4258         status = decode_op_hdr(xdr, OP_CLOSE);
4259         if (status != -EIO)
4260                 nfs_increment_open_seqid(status, res->seqid);
4261         if (!status)
4262                 status = decode_stateid(xdr, &res->stateid);
4263         return status;
4264 }
4265
4266 static int decode_verifier(struct xdr_stream *xdr, void *verifier)
4267 {
4268         return decode_opaque_fixed(xdr, verifier, NFS4_VERIFIER_SIZE);
4269 }
4270
4271 static int decode_write_verifier(struct xdr_stream *xdr, struct nfs_write_verifier *verifier)
4272 {
4273         return decode_opaque_fixed(xdr, verifier->data, NFS4_VERIFIER_SIZE);
4274 }
4275
4276 static int decode_commit(struct xdr_stream *xdr, struct nfs_commitres *res)
4277 {
4278         int status;
4279
4280         status = decode_op_hdr(xdr, OP_COMMIT);
4281         if (!status)
4282                 status = decode_write_verifier(xdr, &res->verf->verifier);
4283         return status;
4284 }
4285
4286 static int decode_create(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
4287 {
4288         __be32 *p;
4289         uint32_t bmlen;
4290         int status;
4291
4292         status = decode_op_hdr(xdr, OP_CREATE);
4293         if (status)
4294                 return status;
4295         if ((status = decode_change_info(xdr, cinfo)))
4296                 return status;
4297         p = xdr_inline_decode(xdr, 4);
4298         if (unlikely(!p))
4299                 goto out_overflow;
4300         bmlen = be32_to_cpup(p);
4301         p = xdr_inline_decode(xdr, bmlen << 2);
4302         if (likely(p))
4303                 return 0;
4304 out_overflow:
4305         print_overflow_msg(__func__, xdr);
4306         return -EIO;
4307 }
4308
4309 static int decode_server_caps(struct xdr_stream *xdr, struct nfs4_server_caps_res *res)
4310 {
4311         unsigned int savep;
4312         uint32_t attrlen, bitmap[3] = {0};
4313         int status;
4314
4315         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
4316                 goto xdr_error;
4317         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
4318                 goto xdr_error;
4319         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
4320                 goto xdr_error;
4321         if ((status = decode_attr_supported(xdr, bitmap, res->attr_bitmask)) != 0)
4322                 goto xdr_error;
4323         if ((status = decode_attr_fh_expire_type(xdr, bitmap,
4324                                                  &res->fh_expire_type)) != 0)
4325                 goto xdr_error;
4326         if ((status = decode_attr_link_support(xdr, bitmap, &res->has_links)) != 0)
4327                 goto xdr_error;
4328         if ((status = decode_attr_symlink_support(xdr, bitmap, &res->has_symlinks)) != 0)
4329                 goto xdr_error;
4330         if ((status = decode_attr_aclsupport(xdr, bitmap, &res->acl_bitmask)) != 0)
4331                 goto xdr_error;
4332         status = verify_attr_len(xdr, savep, attrlen);
4333 xdr_error:
4334         dprintk("%s: xdr returned %d!\n", __func__, -status);
4335         return status;
4336 }
4337
4338 static int decode_statfs(struct xdr_stream *xdr, struct nfs_fsstat *fsstat)
4339 {
4340         unsigned int savep;
4341         uint32_t attrlen, bitmap[3] = {0};
4342         int status;
4343
4344         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
4345                 goto xdr_error;
4346         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
4347                 goto xdr_error;
4348         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
4349                 goto xdr_error;
4350
4351         if ((status = decode_attr_files_avail(xdr, bitmap, &fsstat->afiles)) != 0)
4352                 goto xdr_error;
4353         if ((status = decode_attr_files_free(xdr, bitmap, &fsstat->ffiles)) != 0)
4354                 goto xdr_error;
4355         if ((status = decode_attr_files_total(xdr, bitmap, &fsstat->tfiles)) != 0)
4356                 goto xdr_error;
4357         if ((status = decode_attr_space_avail(xdr, bitmap, &fsstat->abytes)) != 0)
4358                 goto xdr_error;
4359         if ((status = decode_attr_space_free(xdr, bitmap, &fsstat->fbytes)) != 0)
4360                 goto xdr_error;
4361         if ((status = decode_attr_space_total(xdr, bitmap, &fsstat->tbytes)) != 0)
4362                 goto xdr_error;
4363
4364         status = verify_attr_len(xdr, savep, attrlen);
4365 xdr_error:
4366         dprintk("%s: xdr returned %d!\n", __func__, -status);
4367         return status;
4368 }
4369
4370 static int decode_pathconf(struct xdr_stream *xdr, struct nfs_pathconf *pathconf)
4371 {
4372         unsigned int savep;
4373         uint32_t attrlen, bitmap[3] = {0};
4374         int status;
4375
4376         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
4377                 goto xdr_error;
4378         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
4379                 goto xdr_error;
4380         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
4381                 goto xdr_error;
4382
4383         if ((status = decode_attr_maxlink(xdr, bitmap, &pathconf->max_link)) != 0)
4384                 goto xdr_error;
4385         if ((status = decode_attr_maxname(xdr, bitmap, &pathconf->max_namelen)) != 0)
4386                 goto xdr_error;
4387
4388         status = verify_attr_len(xdr, savep, attrlen);
4389 xdr_error:
4390         dprintk("%s: xdr returned %d!\n", __func__, -status);
4391         return status;
4392 }
4393
4394 static int decode_threshold_hint(struct xdr_stream *xdr,
4395                                   uint32_t *bitmap,
4396                                   uint64_t *res,
4397                                   uint32_t hint_bit)
4398 {
4399         __be32 *p;
4400
4401         *res = 0;
4402         if (likely(bitmap[0] & hint_bit)) {
4403                 p = xdr_inline_decode(xdr, 8);
4404                 if (unlikely(!p))
4405                         goto out_overflow;
4406                 xdr_decode_hyper(p, res);
4407         }
4408         return 0;
4409 out_overflow:
4410         print_overflow_msg(__func__, xdr);
4411         return -EIO;
4412 }
4413
4414 static int decode_first_threshold_item4(struct xdr_stream *xdr,
4415                                         struct nfs4_threshold *res)
4416 {
4417         __be32 *p;
4418         unsigned int savep;
4419         uint32_t bitmap[3] = {0,}, attrlen;
4420         int status;
4421
4422         /* layout type */
4423         p = xdr_inline_decode(xdr, 4);
4424         if (unlikely(!p)) {
4425                 print_overflow_msg(__func__, xdr);
4426                 return -EIO;
4427         }
4428         res->l_type = be32_to_cpup(p);
4429
4430         /* thi_hintset bitmap */
4431         status = decode_attr_bitmap(xdr, bitmap);
4432         if (status < 0)
4433                 goto xdr_error;
4434
4435         /* thi_hintlist length */
4436         status = decode_attr_length(xdr, &attrlen, &savep);
4437         if (status < 0)
4438                 goto xdr_error;
4439         /* thi_hintlist */
4440         status = decode_threshold_hint(xdr, bitmap, &res->rd_sz, THRESHOLD_RD);
4441         if (status < 0)
4442                 goto xdr_error;
4443         status = decode_threshold_hint(xdr, bitmap, &res->wr_sz, THRESHOLD_WR);
4444         if (status < 0)
4445                 goto xdr_error;
4446         status = decode_threshold_hint(xdr, bitmap, &res->rd_io_sz,
4447                                        THRESHOLD_RD_IO);
4448         if (status < 0)
4449                 goto xdr_error;
4450         status = decode_threshold_hint(xdr, bitmap, &res->wr_io_sz,
4451                                        THRESHOLD_WR_IO);
4452         if (status < 0)
4453                 goto xdr_error;
4454
4455         status = verify_attr_len(xdr, savep, attrlen);
4456         res->bm = bitmap[0];
4457
4458         dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
4459                  __func__, res->bm, res->rd_sz, res->wr_sz, res->rd_io_sz,
4460                 res->wr_io_sz);
4461 xdr_error:
4462         dprintk("%s ret=%d!\n", __func__, status);
4463         return status;
4464 }
4465
4466 /*
4467  * Thresholds on pNFS direct I/O vrs MDS I/O
4468  */
4469 static int decode_attr_mdsthreshold(struct xdr_stream *xdr,
4470                                     uint32_t *bitmap,
4471                                     struct nfs4_threshold *res)
4472 {
4473         __be32 *p;
4474         int status = 0;
4475         uint32_t num;
4476
4477         if (unlikely(bitmap[2] & (FATTR4_WORD2_MDSTHRESHOLD - 1U)))
4478                 return -EIO;
4479         if (bitmap[2] & FATTR4_WORD2_MDSTHRESHOLD) {
4480                 /* Did the server return an unrequested attribute? */
4481                 if (unlikely(res == NULL))
4482                         return -EREMOTEIO;
4483                 p = xdr_inline_decode(xdr, 4);
4484                 if (unlikely(!p))
4485                         goto out_overflow;
4486                 num = be32_to_cpup(p);
4487                 if (num == 0)
4488                         return 0;
4489                 if (num > 1)
4490                         printk(KERN_INFO "%s: Warning: Multiple pNFS layout "
4491                                 "drivers per filesystem not supported\n",
4492                                 __func__);
4493
4494                 status = decode_first_threshold_item4(xdr, res);
4495                 bitmap[2] &= ~FATTR4_WORD2_MDSTHRESHOLD;
4496         }
4497         return status;
4498 out_overflow:
4499         print_overflow_msg(__func__, xdr);
4500         return -EIO;
4501 }
4502
4503 static int decode_getfattr_attrs(struct xdr_stream *xdr, uint32_t *bitmap,
4504                 struct nfs_fattr *fattr, struct nfs_fh *fh,
4505                 struct nfs4_fs_locations *fs_loc, struct nfs4_label *label,
4506                 const struct nfs_server *server)
4507 {
4508         int status;
4509         umode_t fmode = 0;
4510         uint32_t type;
4511         int32_t err;
4512
4513         status = decode_attr_type(xdr, bitmap, &type);
4514         if (status < 0)
4515                 goto xdr_error;
4516         fattr->mode = 0;
4517         if (status != 0) {
4518                 fattr->mode |= nfs_type2fmt[type];
4519                 fattr->valid |= status;
4520         }
4521
4522         status = decode_attr_change(xdr, bitmap, &fattr->change_attr);
4523         if (status < 0)
4524                 goto xdr_error;
4525         fattr->valid |= status;
4526
4527         status = decode_attr_size(xdr, bitmap, &fattr->size);
4528         if (status < 0)
4529                 goto xdr_error;
4530         fattr->valid |= status;
4531
4532         status = decode_attr_fsid(xdr, bitmap, &fattr->fsid);
4533         if (status < 0)
4534                 goto xdr_error;
4535         fattr->valid |= status;
4536
4537         err = 0;
4538         status = decode_attr_error(xdr, bitmap, &err);
4539         if (status < 0)
4540                 goto xdr_error;
4541
4542         status = decode_attr_filehandle(xdr, bitmap, fh);
4543         if (status < 0)
4544                 goto xdr_error;
4545
4546         status = decode_attr_fileid(xdr, bitmap, &fattr->fileid);
4547         if (status < 0)
4548                 goto xdr_error;
4549         fattr->valid |= status;
4550
4551         status = decode_attr_fs_locations(xdr, bitmap, fs_loc);
4552         if (status < 0)
4553                 goto xdr_error;
4554         fattr->valid |= status;
4555
4556         status = decode_attr_mode(xdr, bitmap, &fmode);
4557         if (status < 0)
4558                 goto xdr_error;
4559         if (status != 0) {
4560                 fattr->mode |= fmode;
4561                 fattr->valid |= status;
4562         }
4563
4564         status = decode_attr_nlink(xdr, bitmap, &fattr->nlink);
4565         if (status < 0)
4566                 goto xdr_error;
4567         fattr->valid |= status;
4568
4569         status = decode_attr_owner(xdr, bitmap, server, &fattr->uid, fattr->owner_name);
4570         if (status < 0)
4571                 goto xdr_error;
4572         fattr->valid |= status;
4573
4574         status = decode_attr_group(xdr, bitmap, server, &fattr->gid, fattr->group_name);
4575         if (status < 0)
4576                 goto xdr_error;
4577         fattr->valid |= status;
4578
4579         status = decode_attr_rdev(xdr, bitmap, &fattr->rdev);
4580         if (status < 0)
4581                 goto xdr_error;
4582         fattr->valid |= status;
4583
4584         status = decode_attr_space_used(xdr, bitmap, &fattr->du.nfs3.used);
4585         if (status < 0)
4586                 goto xdr_error;
4587         fattr->valid |= status;
4588
4589         status = decode_attr_time_access(xdr, bitmap, &fattr->atime);
4590         if (status < 0)
4591                 goto xdr_error;
4592         fattr->valid |= status;
4593
4594         status = decode_attr_time_metadata(xdr, bitmap, &fattr->ctime);
4595         if (status < 0)
4596                 goto xdr_error;
4597         fattr->valid |= status;
4598
4599         status = decode_attr_time_modify(xdr, bitmap, &fattr->mtime);
4600         if (status < 0)
4601                 goto xdr_error;
4602         fattr->valid |= status;
4603
4604         status = decode_attr_mounted_on_fileid(xdr, bitmap, &fattr->mounted_on_fileid);
4605         if (status < 0)
4606                 goto xdr_error;
4607         fattr->valid |= status;
4608
4609         status = decode_attr_mdsthreshold(xdr, bitmap, fattr->mdsthreshold);
4610         if (status < 0)
4611                 goto xdr_error;
4612
4613         if (label) {
4614                 status = decode_attr_security_label(xdr, bitmap, label);
4615                 if (status < 0)
4616                         goto xdr_error;
4617                 fattr->valid |= status;
4618         }
4619
4620 xdr_error:
4621         dprintk("%s: xdr returned %d\n", __func__, -status);
4622         return status;
4623 }
4624
4625 static int decode_getfattr_generic(struct xdr_stream *xdr, struct nfs_fattr *fattr,
4626                 struct nfs_fh *fh, struct nfs4_fs_locations *fs_loc,
4627                 struct nfs4_label *label, const struct nfs_server *server)
4628 {
4629         unsigned int savep;
4630         uint32_t attrlen,
4631                  bitmap[3] = {0};
4632         int status;
4633
4634         status = decode_op_hdr(xdr, OP_GETATTR);
4635         if (status < 0)
4636                 goto xdr_error;
4637
4638         status = decode_attr_bitmap(xdr, bitmap);
4639         if (status < 0)
4640                 goto xdr_error;
4641
4642         status = decode_attr_length(xdr, &attrlen, &savep);
4643         if (status < 0)
4644                 goto xdr_error;
4645
4646         status = decode_getfattr_attrs(xdr, bitmap, fattr, fh, fs_loc,
4647                                         label, server);
4648         if (status < 0)
4649                 goto xdr_error;
4650
4651         status = verify_attr_len(xdr, savep, attrlen);
4652 xdr_error:
4653         dprintk("%s: xdr returned %d\n", __func__, -status);
4654         return status;
4655 }
4656
4657 static int decode_getfattr_label(struct xdr_stream *xdr, struct nfs_fattr *fattr,
4658                 struct nfs4_label *label, const struct nfs_server *server)
4659 {
4660         return decode_getfattr_generic(xdr, fattr, NULL, NULL, label, server);
4661 }
4662
4663 static int decode_getfattr(struct xdr_stream *xdr, struct nfs_fattr *fattr,
4664                 const struct nfs_server *server)
4665 {
4666         return decode_getfattr_generic(xdr, fattr, NULL, NULL, NULL, server);
4667 }
4668
4669 /*
4670  * Decode potentially multiple layout types. Currently we only support
4671  * one layout driver per file system.
4672  */
4673 static int decode_first_pnfs_layout_type(struct xdr_stream *xdr,
4674                                          uint32_t *layouttype)
4675 {
4676         __be32 *p;
4677         int num;
4678
4679         p = xdr_inline_decode(xdr, 4);
4680         if (unlikely(!p))
4681                 goto out_overflow;
4682         num = be32_to_cpup(p);
4683
4684         /* pNFS is not supported by the underlying file system */
4685         if (num == 0) {
4686                 *layouttype = 0;
4687                 return 0;
4688         }
4689         if (num > 1)
4690                 printk(KERN_INFO "NFS: %s: Warning: Multiple pNFS layout "
4691                         "drivers per filesystem not supported\n", __func__);
4692
4693         /* Decode and set first layout type, move xdr->p past unused types */
4694         p = xdr_inline_decode(xdr, num * 4);
4695         if (unlikely(!p))
4696                 goto out_overflow;
4697         *layouttype = be32_to_cpup(p);
4698         return 0;
4699 out_overflow:
4700         print_overflow_msg(__func__, xdr);
4701         return -EIO;
4702 }
4703
4704 /*
4705  * The type of file system exported.
4706  * Note we must ensure that layouttype is set in any non-error case.
4707  */
4708 static int decode_attr_pnfstype(struct xdr_stream *xdr, uint32_t *bitmap,
4709                                 uint32_t *layouttype)
4710 {
4711         int status = 0;
4712
4713         dprintk("%s: bitmap is %x\n", __func__, bitmap[1]);
4714         if (unlikely(bitmap[1] & (FATTR4_WORD1_FS_LAYOUT_TYPES - 1U)))
4715                 return -EIO;
4716         if (bitmap[1] & FATTR4_WORD1_FS_LAYOUT_TYPES) {
4717                 status = decode_first_pnfs_layout_type(xdr, layouttype);
4718                 bitmap[1] &= ~FATTR4_WORD1_FS_LAYOUT_TYPES;
4719         } else
4720                 *layouttype = 0;
4721         return status;
4722 }
4723
4724 /*
4725  * The prefered block size for layout directed io
4726  */
4727 static int decode_attr_layout_blksize(struct xdr_stream *xdr, uint32_t *bitmap,
4728                                       uint32_t *res)
4729 {
4730         __be32 *p;
4731
4732         dprintk("%s: bitmap is %x\n", __func__, bitmap[2]);
4733         *res = 0;
4734         if (bitmap[2] & FATTR4_WORD2_LAYOUT_BLKSIZE) {
4735                 p = xdr_inline_decode(xdr, 4);
4736                 if (unlikely(!p)) {
4737                         print_overflow_msg(__func__, xdr);
4738                         return -EIO;
4739                 }
4740                 *res = be32_to_cpup(p);
4741                 bitmap[2] &= ~FATTR4_WORD2_LAYOUT_BLKSIZE;
4742         }
4743         return 0;
4744 }
4745
4746 static int decode_fsinfo(struct xdr_stream *xdr, struct nfs_fsinfo *fsinfo)
4747 {
4748         unsigned int savep;
4749         uint32_t attrlen, bitmap[3];
4750         int status;
4751
4752         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
4753                 goto xdr_error;
4754         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
4755                 goto xdr_error;
4756         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
4757                 goto xdr_error;
4758
4759         fsinfo->rtmult = fsinfo->wtmult = 512;  /* ??? */
4760
4761         if ((status = decode_attr_lease_time(xdr, bitmap, &fsinfo->lease_time)) != 0)
4762                 goto xdr_error;
4763         if ((status = decode_attr_maxfilesize(xdr, bitmap, &fsinfo->maxfilesize)) != 0)
4764                 goto xdr_error;
4765         if ((status = decode_attr_maxread(xdr, bitmap, &fsinfo->rtmax)) != 0)
4766                 goto xdr_error;
4767         fsinfo->rtpref = fsinfo->dtpref = fsinfo->rtmax;
4768         if ((status = decode_attr_maxwrite(xdr, bitmap, &fsinfo->wtmax)) != 0)
4769                 goto xdr_error;
4770         fsinfo->wtpref = fsinfo->wtmax;
4771         status = decode_attr_time_delta(xdr, bitmap, &fsinfo->time_delta);
4772         if (status != 0)
4773                 goto xdr_error;
4774         status = decode_attr_pnfstype(xdr, bitmap, &fsinfo->layouttype);
4775         if (status != 0)
4776                 goto xdr_error;
4777         status = decode_attr_layout_blksize(xdr, bitmap, &fsinfo->blksize);
4778         if (status)
4779                 goto xdr_error;
4780
4781         status = verify_attr_len(xdr, savep, attrlen);
4782 xdr_error:
4783         dprintk("%s: xdr returned %d!\n", __func__, -status);
4784         return status;
4785 }
4786
4787 static int decode_getfh(struct xdr_stream *xdr, struct nfs_fh *fh)
4788 {
4789         __be32 *p;
4790         uint32_t len;
4791         int status;
4792
4793         /* Zero handle first to allow comparisons */
4794         memset(fh, 0, sizeof(*fh));
4795
4796         status = decode_op_hdr(xdr, OP_GETFH);
4797         if (status)
4798                 return status;
4799
4800         p = xdr_inline_decode(xdr, 4);
4801         if (unlikely(!p))
4802                 goto out_overflow;
4803         len = be32_to_cpup(p);
4804         if (len > NFS4_FHSIZE)
4805                 return -EIO;
4806         fh->size = len;
4807         p = xdr_inline_decode(xdr, len);
4808         if (unlikely(!p))
4809                 goto out_overflow;
4810         memcpy(fh->data, p, len);
4811         return 0;
4812 out_overflow:
4813         print_overflow_msg(__func__, xdr);
4814         return -EIO;
4815 }
4816
4817 static int decode_link(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
4818 {
4819         int status;
4820
4821         status = decode_op_hdr(xdr, OP_LINK);
4822         if (status)
4823                 return status;
4824         return decode_change_info(xdr, cinfo);
4825 }
4826
4827 /*
4828  * We create the owner, so we know a proper owner.id length is 4.
4829  */
4830 static int decode_lock_denied (struct xdr_stream *xdr, struct file_lock *fl)
4831 {
4832         uint64_t offset, length, clientid;
4833         __be32 *p;
4834         uint32_t namelen, type;
4835
4836         p = xdr_inline_decode(xdr, 32); /* read 32 bytes */
4837         if (unlikely(!p))
4838                 goto out_overflow;
4839         p = xdr_decode_hyper(p, &offset); /* read 2 8-byte long words */
4840         p = xdr_decode_hyper(p, &length);
4841         type = be32_to_cpup(p++); /* 4 byte read */
4842         if (fl != NULL) { /* manipulate file lock */
4843                 fl->fl_start = (loff_t)offset;
4844                 fl->fl_end = fl->fl_start + (loff_t)length - 1;
4845                 if (length == ~(uint64_t)0)
4846                         fl->fl_end = OFFSET_MAX;
4847                 fl->fl_type = F_WRLCK;
4848                 if (type & 1)
4849                         fl->fl_type = F_RDLCK;
4850                 fl->fl_pid = 0;
4851         }
4852         p = xdr_decode_hyper(p, &clientid); /* read 8 bytes */
4853         namelen = be32_to_cpup(p); /* read 4 bytes */  /* have read all 32 bytes now */
4854         p = xdr_inline_decode(xdr, namelen); /* variable size field */
4855         if (likely(p))
4856                 return -NFS4ERR_DENIED;
4857 out_overflow:
4858         print_overflow_msg(__func__, xdr);
4859         return -EIO;
4860 }
4861
4862 static int decode_lock(struct xdr_stream *xdr, struct nfs_lock_res *res)
4863 {
4864         int status;
4865
4866         status = decode_op_hdr(xdr, OP_LOCK);
4867         if (status == -EIO)
4868                 goto out;
4869         if (status == 0) {
4870                 status = decode_stateid(xdr, &res->stateid);
4871                 if (unlikely(status))
4872                         goto out;
4873         } else if (status == -NFS4ERR_DENIED)
4874                 status = decode_lock_denied(xdr, NULL);
4875         if (res->open_seqid != NULL)
4876                 nfs_increment_open_seqid(status, res->open_seqid);
4877         nfs_increment_lock_seqid(status, res->lock_seqid);
4878 out:
4879         return status;
4880 }
4881
4882 static int decode_lockt(struct xdr_stream *xdr, struct nfs_lockt_res *res)
4883 {
4884         int status;
4885         status = decode_op_hdr(xdr, OP_LOCKT);
4886         if (status == -NFS4ERR_DENIED)
4887                 return decode_lock_denied(xdr, res->denied);
4888         return status;
4889 }
4890
4891 static int decode_locku(struct xdr_stream *xdr, struct nfs_locku_res *res)
4892 {
4893         int status;
4894
4895         status = decode_op_hdr(xdr, OP_LOCKU);
4896         if (status != -EIO)
4897                 nfs_increment_lock_seqid(status, res->seqid);
4898         if (status == 0)
4899                 status = decode_stateid(xdr, &res->stateid);
4900         return status;
4901 }
4902
4903 static int decode_release_lockowner(struct xdr_stream *xdr)
4904 {
4905         return decode_op_hdr(xdr, OP_RELEASE_LOCKOWNER);
4906 }
4907
4908 static int decode_lookup(struct xdr_stream *xdr)
4909 {
4910         return decode_op_hdr(xdr, OP_LOOKUP);
4911 }
4912
4913 /* This is too sick! */
4914 static int decode_space_limit(struct xdr_stream *xdr, u64 *maxsize)
4915 {
4916         __be32 *p;
4917         uint32_t limit_type, nblocks, blocksize;
4918
4919         p = xdr_inline_decode(xdr, 12);
4920         if (unlikely(!p))
4921                 goto out_overflow;
4922         limit_type = be32_to_cpup(p++);
4923         switch (limit_type) {
4924         case 1:
4925                 xdr_decode_hyper(p, maxsize);
4926                 break;
4927         case 2:
4928                 nblocks = be32_to_cpup(p++);
4929                 blocksize = be32_to_cpup(p);
4930                 *maxsize = (uint64_t)nblocks * (uint64_t)blocksize;
4931         }
4932         return 0;
4933 out_overflow:
4934         print_overflow_msg(__func__, xdr);
4935         return -EIO;
4936 }
4937
4938 static int decode_delegation(struct xdr_stream *xdr, struct nfs_openres *res)
4939 {
4940         __be32 *p;
4941         uint32_t delegation_type;
4942         int status;
4943
4944         p = xdr_inline_decode(xdr, 4);
4945         if (unlikely(!p))
4946                 goto out_overflow;
4947         delegation_type = be32_to_cpup(p);
4948         if (delegation_type == NFS4_OPEN_DELEGATE_NONE) {
4949                 res->delegation_type = 0;
4950                 return 0;
4951         }
4952         status = decode_stateid(xdr, &res->delegation);
4953         if (unlikely(status))
4954                 return status;
4955         p = xdr_inline_decode(xdr, 4);
4956         if (unlikely(!p))
4957                 goto out_overflow;
4958         res->do_recall = be32_to_cpup(p);
4959
4960         switch (delegation_type) {
4961         case NFS4_OPEN_DELEGATE_READ:
4962                 res->delegation_type = FMODE_READ;
4963                 break;
4964         case NFS4_OPEN_DELEGATE_WRITE:
4965                 res->delegation_type = FMODE_WRITE|FMODE_READ;
4966                 if (decode_space_limit(xdr, &res->maxsize) < 0)
4967                                 return -EIO;
4968         }
4969         return decode_ace(xdr, NULL, res->server->nfs_client);
4970 out_overflow:
4971         print_overflow_msg(__func__, xdr);
4972         return -EIO;
4973 }
4974
4975 static int decode_open(struct xdr_stream *xdr, struct nfs_openres *res)
4976 {
4977         __be32 *p;
4978         uint32_t savewords, bmlen, i;
4979         int status;
4980
4981         if (!__decode_op_hdr(xdr, OP_OPEN, &status))
4982                 return status;
4983         nfs_increment_open_seqid(status, res->seqid);
4984         if (status)
4985                 return status;
4986         status = decode_stateid(xdr, &res->stateid);
4987         if (unlikely(status))
4988                 return status;
4989
4990         decode_change_info(xdr, &res->cinfo);
4991
4992         p = xdr_inline_decode(xdr, 8);
4993         if (unlikely(!p))
4994                 goto out_overflow;
4995         res->rflags = be32_to_cpup(p++);
4996         bmlen = be32_to_cpup(p);
4997         if (bmlen > 10)
4998                 goto xdr_error;
4999
5000         p = xdr_inline_decode(xdr, bmlen << 2);
5001         if (unlikely(!p))
5002                 goto out_overflow;
5003         savewords = min_t(uint32_t, bmlen, NFS4_BITMAP_SIZE);
5004         for (i = 0; i < savewords; ++i)
5005                 res->attrset[i] = be32_to_cpup(p++);
5006         for (; i < NFS4_BITMAP_SIZE; i++)
5007                 res->attrset[i] = 0;
5008
5009         return decode_delegation(xdr, res);
5010 xdr_error:
5011         dprintk("%s: Bitmap too large! Length = %u\n", __func__, bmlen);
5012         return -EIO;
5013 out_overflow:
5014         print_overflow_msg(__func__, xdr);
5015         return -EIO;
5016 }
5017
5018 static int decode_open_confirm(struct xdr_stream *xdr, struct nfs_open_confirmres *res)
5019 {
5020         int status;
5021
5022         status = decode_op_hdr(xdr, OP_OPEN_CONFIRM);
5023         if (status != -EIO)
5024                 nfs_increment_open_seqid(status, res->seqid);
5025         if (!status)
5026                 status = decode_stateid(xdr, &res->stateid);
5027         return status;
5028 }
5029
5030 static int decode_open_downgrade(struct xdr_stream *xdr, struct nfs_closeres *res)
5031 {
5032         int status;
5033
5034         status = decode_op_hdr(xdr, OP_OPEN_DOWNGRADE);
5035         if (status != -EIO)
5036                 nfs_increment_open_seqid(status, res->seqid);
5037         if (!status)
5038                 status = decode_stateid(xdr, &res->stateid);
5039         return status;
5040 }
5041
5042 static int decode_putfh(struct xdr_stream *xdr)
5043 {
5044         return decode_op_hdr(xdr, OP_PUTFH);
5045 }
5046
5047 static int decode_putrootfh(struct xdr_stream *xdr)
5048 {
5049         return decode_op_hdr(xdr, OP_PUTROOTFH);
5050 }
5051
5052 static int decode_read(struct xdr_stream *xdr, struct rpc_rqst *req,
5053                        struct nfs_pgio_res *res)
5054 {
5055         __be32 *p;
5056         uint32_t count, eof, recvd;
5057         int status;
5058
5059         status = decode_op_hdr(xdr, OP_READ);
5060         if (status)
5061                 return status;
5062         p = xdr_inline_decode(xdr, 8);
5063         if (unlikely(!p))
5064                 goto out_overflow;
5065         eof = be32_to_cpup(p++);
5066         count = be32_to_cpup(p);
5067         recvd = xdr_read_pages(xdr, count);
5068         if (count > recvd) {
5069                 dprintk("NFS: server cheating in read reply: "
5070                                 "count %u > recvd %u\n", count, recvd);
5071                 count = recvd;
5072                 eof = 0;
5073         }
5074         res->eof = eof;
5075         res->count = count;
5076         return 0;
5077 out_overflow:
5078         print_overflow_msg(__func__, xdr);
5079         return -EIO;
5080 }
5081
5082 static int decode_readdir(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs4_readdir_res *readdir)
5083 {
5084         int             status;
5085         __be32          verf[2];
5086
5087         status = decode_op_hdr(xdr, OP_READDIR);
5088         if (!status)
5089                 status = decode_verifier(xdr, readdir->verifier.data);
5090         if (unlikely(status))
5091                 return status;
5092         memcpy(verf, readdir->verifier.data, sizeof(verf));
5093         dprintk("%s: verifier = %08x:%08x\n",
5094                         __func__, verf[0], verf[1]);
5095         return xdr_read_pages(xdr, xdr->buf->page_len);
5096 }
5097
5098 static int decode_readlink(struct xdr_stream *xdr, struct rpc_rqst *req)
5099 {
5100         struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
5101         u32 len, recvd;
5102         __be32 *p;
5103         int status;
5104
5105         status = decode_op_hdr(xdr, OP_READLINK);
5106         if (status)
5107                 return status;
5108
5109         /* Convert length of symlink */
5110         p = xdr_inline_decode(xdr, 4);
5111         if (unlikely(!p))
5112                 goto out_overflow;
5113         len = be32_to_cpup(p);
5114         if (len >= rcvbuf->page_len || len <= 0) {
5115                 dprintk("nfs: server returned giant symlink!\n");
5116                 return -ENAMETOOLONG;
5117         }
5118         recvd = xdr_read_pages(xdr, len);
5119         if (recvd < len) {
5120                 dprintk("NFS: server cheating in readlink reply: "
5121                                 "count %u > recvd %u\n", len, recvd);
5122                 return -EIO;
5123         }
5124         /*
5125          * The XDR encode routine has set things up so that
5126          * the link text will be copied directly into the
5127          * buffer.  We just have to do overflow-checking,
5128          * and and null-terminate the text (the VFS expects
5129          * null-termination).
5130          */
5131         xdr_terminate_string(rcvbuf, len);
5132         return 0;
5133 out_overflow:
5134         print_overflow_msg(__func__, xdr);
5135         return -EIO;
5136 }
5137
5138 static int decode_remove(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
5139 {
5140         int status;
5141
5142         status = decode_op_hdr(xdr, OP_REMOVE);
5143         if (status)
5144                 goto out;
5145         status = decode_change_info(xdr, cinfo);
5146 out:
5147         return status;
5148 }
5149
5150 static int decode_rename(struct xdr_stream *xdr, struct nfs4_change_info *old_cinfo,
5151               struct nfs4_change_info *new_cinfo)
5152 {
5153         int status;
5154
5155         status = decode_op_hdr(xdr, OP_RENAME);
5156         if (status)
5157                 goto out;
5158         if ((status = decode_change_info(xdr, old_cinfo)))
5159                 goto out;
5160         status = decode_change_info(xdr, new_cinfo);
5161 out:
5162         return status;
5163 }
5164
5165 static int decode_renew(struct xdr_stream *xdr)
5166 {
5167         return decode_op_hdr(xdr, OP_RENEW);
5168 }
5169
5170 static int
5171 decode_restorefh(struct xdr_stream *xdr)
5172 {
5173         return decode_op_hdr(xdr, OP_RESTOREFH);
5174 }
5175
5176 static int decode_getacl(struct xdr_stream *xdr, struct rpc_rqst *req,
5177                          struct nfs_getaclres *res)
5178 {
5179         unsigned int savep;
5180         uint32_t attrlen,
5181                  bitmap[3] = {0};
5182         int status;
5183         unsigned int pg_offset;
5184
5185         res->acl_len = 0;
5186         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
5187                 goto out;
5188
5189         xdr_enter_page(xdr, xdr->buf->page_len);
5190
5191         /* Calculate the offset of the page data */
5192         pg_offset = xdr->buf->head[0].iov_len;
5193
5194         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
5195                 goto out;
5196         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
5197                 goto out;
5198
5199         if (unlikely(bitmap[0] & (FATTR4_WORD0_ACL - 1U)))
5200                 return -EIO;
5201         if (likely(bitmap[0] & FATTR4_WORD0_ACL)) {
5202
5203                 /* The bitmap (xdr len + bitmaps) and the attr xdr len words
5204                  * are stored with the acl data to handle the problem of
5205                  * variable length bitmaps.*/
5206                 res->acl_data_offset = xdr_stream_pos(xdr) - pg_offset;
5207                 res->acl_len = attrlen;
5208
5209                 /* Check for receive buffer overflow */
5210                 if (res->acl_len > (xdr->nwords << 2) ||
5211                     res->acl_len + res->acl_data_offset > xdr->buf->page_len) {
5212                         res->acl_flags |= NFS4_ACL_TRUNC;
5213                         dprintk("NFS: acl reply: attrlen %u > page_len %u\n",
5214                                         attrlen, xdr->nwords << 2);
5215                 }
5216         } else
5217                 status = -EOPNOTSUPP;
5218
5219 out:
5220         return status;
5221 }
5222
5223 static int
5224 decode_savefh(struct xdr_stream *xdr)
5225 {
5226         return decode_op_hdr(xdr, OP_SAVEFH);
5227 }
5228
5229 static int decode_setattr(struct xdr_stream *xdr)
5230 {
5231         __be32 *p;
5232         uint32_t bmlen;
5233         int status;
5234
5235         status = decode_op_hdr(xdr, OP_SETATTR);
5236         if (status)
5237                 return status;
5238         p = xdr_inline_decode(xdr, 4);
5239         if (unlikely(!p))
5240                 goto out_overflow;
5241         bmlen = be32_to_cpup(p);
5242         p = xdr_inline_decode(xdr, bmlen << 2);
5243         if (likely(p))
5244                 return 0;
5245 out_overflow:
5246         print_overflow_msg(__func__, xdr);
5247         return -EIO;
5248 }
5249
5250 static int decode_setclientid(struct xdr_stream *xdr, struct nfs4_setclientid_res *res)
5251 {
5252         __be32 *p;
5253         uint32_t opnum;
5254         int32_t nfserr;
5255
5256         p = xdr_inline_decode(xdr, 8);
5257         if (unlikely(!p))
5258                 goto out_overflow;
5259         opnum = be32_to_cpup(p++);
5260         if (opnum != OP_SETCLIENTID) {
5261                 dprintk("nfs: decode_setclientid: Server returned operation"
5262                         " %d\n", opnum);
5263                 return -EIO;
5264         }
5265         nfserr = be32_to_cpup(p);
5266         if (nfserr == NFS_OK) {
5267                 p = xdr_inline_decode(xdr, 8 + NFS4_VERIFIER_SIZE);
5268                 if (unlikely(!p))
5269                         goto out_overflow;
5270                 p = xdr_decode_hyper(p, &res->clientid);
5271                 memcpy(res->confirm.data, p, NFS4_VERIFIER_SIZE);
5272         } else if (nfserr == NFSERR_CLID_INUSE) {
5273                 uint32_t len;
5274
5275                 /* skip netid string */
5276                 p = xdr_inline_decode(xdr, 4);
5277                 if (unlikely(!p))
5278                         goto out_overflow;
5279                 len = be32_to_cpup(p);
5280                 p = xdr_inline_decode(xdr, len);
5281                 if (unlikely(!p))
5282                         goto out_overflow;
5283
5284                 /* skip uaddr string */
5285                 p = xdr_inline_decode(xdr, 4);
5286                 if (unlikely(!p))
5287                         goto out_overflow;
5288                 len = be32_to_cpup(p);
5289                 p = xdr_inline_decode(xdr, len);
5290                 if (unlikely(!p))
5291                         goto out_overflow;
5292                 return -NFSERR_CLID_INUSE;
5293         } else
5294                 return nfs4_stat_to_errno(nfserr);
5295
5296         return 0;
5297 out_overflow:
5298         print_overflow_msg(__func__, xdr);
5299         return -EIO;
5300 }
5301
5302 static int decode_setclientid_confirm(struct xdr_stream *xdr)
5303 {
5304         return decode_op_hdr(xdr, OP_SETCLIENTID_CONFIRM);
5305 }
5306
5307 static int decode_write(struct xdr_stream *xdr, struct nfs_pgio_res *res)
5308 {
5309         __be32 *p;
5310         int status;
5311
5312         status = decode_op_hdr(xdr, OP_WRITE);
5313         if (status)
5314                 return status;
5315
5316         p = xdr_inline_decode(xdr, 8);
5317         if (unlikely(!p))
5318                 goto out_overflow;
5319         res->count = be32_to_cpup(p++);
5320         res->verf->committed = be32_to_cpup(p++);
5321         return decode_write_verifier(xdr, &res->verf->verifier);
5322 out_overflow:
5323         print_overflow_msg(__func__, xdr);
5324         return -EIO;
5325 }
5326
5327 static int decode_delegreturn(struct xdr_stream *xdr)
5328 {
5329         return decode_op_hdr(xdr, OP_DELEGRETURN);
5330 }
5331
5332 static int decode_secinfo_gss(struct xdr_stream *xdr,
5333                               struct nfs4_secinfo4 *flavor)
5334 {
5335         u32 oid_len;
5336         __be32 *p;
5337
5338         p = xdr_inline_decode(xdr, 4);
5339         if (unlikely(!p))
5340                 goto out_overflow;
5341         oid_len = be32_to_cpup(p);
5342         if (oid_len > GSS_OID_MAX_LEN)
5343                 goto out_err;
5344
5345         p = xdr_inline_decode(xdr, oid_len);
5346         if (unlikely(!p))
5347                 goto out_overflow;
5348         memcpy(flavor->flavor_info.oid.data, p, oid_len);
5349         flavor->flavor_info.oid.len = oid_len;
5350
5351         p = xdr_inline_decode(xdr, 8);
5352         if (unlikely(!p))
5353                 goto out_overflow;
5354         flavor->flavor_info.qop = be32_to_cpup(p++);
5355         flavor->flavor_info.service = be32_to_cpup(p);
5356
5357         return 0;
5358
5359 out_overflow:
5360         print_overflow_msg(__func__, xdr);
5361         return -EIO;
5362 out_err:
5363         return -EINVAL;
5364 }
5365
5366 static int decode_secinfo_common(struct xdr_stream *xdr, struct nfs4_secinfo_res *res)
5367 {
5368         struct nfs4_secinfo4 *sec_flavor;
5369         unsigned int i, num_flavors;
5370         int status;
5371         __be32 *p;
5372
5373         p = xdr_inline_decode(xdr, 4);
5374         if (unlikely(!p))
5375                 goto out_overflow;
5376
5377         res->flavors->num_flavors = 0;
5378         num_flavors = be32_to_cpup(p);
5379
5380         for (i = 0; i < num_flavors; i++) {
5381                 sec_flavor = &res->flavors->flavors[i];
5382                 if ((char *)&sec_flavor[1] - (char *)res->flavors > PAGE_SIZE)
5383                         break;
5384
5385                 p = xdr_inline_decode(xdr, 4);
5386                 if (unlikely(!p))
5387                         goto out_overflow;
5388                 sec_flavor->flavor = be32_to_cpup(p);
5389
5390                 if (sec_flavor->flavor == RPC_AUTH_GSS) {
5391                         status = decode_secinfo_gss(xdr, sec_flavor);
5392                         if (status)
5393                                 goto out;
5394                 }
5395                 res->flavors->num_flavors++;
5396         }
5397
5398         status = 0;
5399 out:
5400         return status;
5401 out_overflow:
5402         print_overflow_msg(__func__, xdr);
5403         return -EIO;
5404 }
5405
5406 static int decode_secinfo(struct xdr_stream *xdr, struct nfs4_secinfo_res *res)
5407 {
5408         int status = decode_op_hdr(xdr, OP_SECINFO);
5409         if (status)
5410                 return status;
5411         return decode_secinfo_common(xdr, res);
5412 }
5413
5414 #if defined(CONFIG_NFS_V4_1)
5415 static int decode_secinfo_no_name(struct xdr_stream *xdr, struct nfs4_secinfo_res *res)
5416 {
5417         int status = decode_op_hdr(xdr, OP_SECINFO_NO_NAME);
5418         if (status)
5419                 return status;
5420         return decode_secinfo_common(xdr, res);
5421 }
5422
5423 static int decode_op_map(struct xdr_stream *xdr, struct nfs4_op_map *op_map)
5424 {
5425         __be32 *p;
5426         uint32_t bitmap_words;
5427         unsigned int i;
5428
5429         p = xdr_inline_decode(xdr, 4);
5430         bitmap_words = be32_to_cpup(p++);
5431         if (bitmap_words > NFS4_OP_MAP_NUM_WORDS)
5432                 return -EIO;
5433         p = xdr_inline_decode(xdr, 4 * bitmap_words);
5434         for (i = 0; i < bitmap_words; i++)
5435                 op_map->u.words[i] = be32_to_cpup(p++);
5436
5437         return 0;
5438 }
5439
5440 static int decode_exchange_id(struct xdr_stream *xdr,
5441                               struct nfs41_exchange_id_res *res)
5442 {
5443         __be32 *p;
5444         uint32_t dummy;
5445         char *dummy_str;
5446         int status;
5447         uint32_t impl_id_count;
5448
5449         status = decode_op_hdr(xdr, OP_EXCHANGE_ID);
5450         if (status)
5451                 return status;
5452
5453         p = xdr_inline_decode(xdr, 8);
5454         if (unlikely(!p))
5455                 goto out_overflow;
5456         xdr_decode_hyper(p, &res->clientid);
5457         p = xdr_inline_decode(xdr, 12);
5458         if (unlikely(!p))
5459                 goto out_overflow;
5460         res->seqid = be32_to_cpup(p++);
5461         res->flags = be32_to_cpup(p++);
5462
5463         res->state_protect.how = be32_to_cpup(p);
5464         switch (res->state_protect.how) {
5465         case SP4_NONE:
5466                 break;
5467         case SP4_MACH_CRED:
5468                 status = decode_op_map(xdr, &res->state_protect.enforce);
5469                 if (status)
5470                         return status;
5471                 status = decode_op_map(xdr, &res->state_protect.allow);
5472                 if (status)
5473                         return status;
5474                 break;
5475         default:
5476                 WARN_ON_ONCE(1);
5477                 return -EIO;
5478         }
5479
5480         /* server_owner4.so_minor_id */
5481         p = xdr_inline_decode(xdr, 8);
5482         if (unlikely(!p))
5483                 goto out_overflow;
5484         p = xdr_decode_hyper(p, &res->server_owner->minor_id);
5485
5486         /* server_owner4.so_major_id */
5487         status = decode_opaque_inline(xdr, &dummy, &dummy_str);
5488         if (unlikely(status))
5489                 return status;
5490         if (unlikely(dummy > NFS4_OPAQUE_LIMIT))
5491                 return -EIO;
5492         memcpy(res->server_owner->major_id, dummy_str, dummy);
5493         res->server_owner->major_id_sz = dummy;
5494
5495         /* server_scope4 */
5496         status = decode_opaque_inline(xdr, &dummy, &dummy_str);
5497         if (unlikely(status))
5498                 return status;
5499         if (unlikely(dummy > NFS4_OPAQUE_LIMIT))
5500                 return -EIO;
5501         memcpy(res->server_scope->server_scope, dummy_str, dummy);
5502         res->server_scope->server_scope_sz = dummy;
5503
5504         /* Implementation Id */
5505         p = xdr_inline_decode(xdr, 4);
5506         if (unlikely(!p))
5507                 goto out_overflow;
5508         impl_id_count = be32_to_cpup(p++);
5509
5510         if (impl_id_count) {
5511                 /* nii_domain */
5512                 status = decode_opaque_inline(xdr, &dummy, &dummy_str);
5513                 if (unlikely(status))
5514                         return status;
5515                 if (unlikely(dummy > NFS4_OPAQUE_LIMIT))
5516                         return -EIO;
5517                 memcpy(res->impl_id->domain, dummy_str, dummy);
5518
5519                 /* nii_name */
5520                 status = decode_opaque_inline(xdr, &dummy, &dummy_str);
5521                 if (unlikely(status))
5522                         return status;
5523                 if (unlikely(dummy > NFS4_OPAQUE_LIMIT))
5524                         return -EIO;
5525                 memcpy(res->impl_id->name, dummy_str, dummy);
5526
5527                 /* nii_date */
5528                 p = xdr_inline_decode(xdr, 12);
5529                 if (unlikely(!p))
5530                         goto out_overflow;
5531                 p = xdr_decode_hyper(p, &res->impl_id->date.seconds);
5532                 res->impl_id->date.nseconds = be32_to_cpup(p);
5533
5534                 /* if there's more than one entry, ignore the rest */
5535         }
5536         return 0;
5537 out_overflow:
5538         print_overflow_msg(__func__, xdr);
5539         return -EIO;
5540 }
5541
5542 static int decode_chan_attrs(struct xdr_stream *xdr,
5543                              struct nfs4_channel_attrs *attrs)
5544 {
5545         __be32 *p;
5546         u32 nr_attrs, val;
5547
5548         p = xdr_inline_decode(xdr, 28);
5549         if (unlikely(!p))
5550                 goto out_overflow;
5551         val = be32_to_cpup(p++);        /* headerpadsz */
5552         if (val)
5553                 return -EINVAL;         /* no support for header padding yet */
5554         attrs->max_rqst_sz = be32_to_cpup(p++);
5555         attrs->max_resp_sz = be32_to_cpup(p++);
5556         attrs->max_resp_sz_cached = be32_to_cpup(p++);
5557         attrs->max_ops = be32_to_cpup(p++);
5558         attrs->max_reqs = be32_to_cpup(p++);
5559         nr_attrs = be32_to_cpup(p);
5560         if (unlikely(nr_attrs > 1)) {
5561                 printk(KERN_WARNING "NFS: %s: Invalid rdma channel attrs "
5562                         "count %u\n", __func__, nr_attrs);
5563                 return -EINVAL;
5564         }
5565         if (nr_attrs == 1) {
5566                 p = xdr_inline_decode(xdr, 4); /* skip rdma_attrs */
5567                 if (unlikely(!p))
5568                         goto out_overflow;
5569         }
5570         return 0;
5571 out_overflow:
5572         print_overflow_msg(__func__, xdr);
5573         return -EIO;
5574 }
5575
5576 static int decode_sessionid(struct xdr_stream *xdr, struct nfs4_sessionid *sid)
5577 {
5578         return decode_opaque_fixed(xdr, sid->data, NFS4_MAX_SESSIONID_LEN);
5579 }
5580
5581 static int decode_bind_conn_to_session(struct xdr_stream *xdr,
5582                                 struct nfs41_bind_conn_to_session_res *res)
5583 {
5584         __be32 *p;
5585         int status;
5586
5587         status = decode_op_hdr(xdr, OP_BIND_CONN_TO_SESSION);
5588         if (!status)
5589                 status = decode_sessionid(xdr, &res->session->sess_id);
5590         if (unlikely(status))
5591                 return status;
5592
5593         /* dir flags, rdma mode bool */
5594         p = xdr_inline_decode(xdr, 8);
5595         if (unlikely(!p))
5596                 goto out_overflow;
5597
5598         res->dir = be32_to_cpup(p++);
5599         if (res->dir == 0 || res->dir > NFS4_CDFS4_BOTH)
5600                 return -EIO;
5601         if (be32_to_cpup(p) == 0)
5602                 res->use_conn_in_rdma_mode = false;
5603         else
5604                 res->use_conn_in_rdma_mode = true;
5605
5606         return 0;
5607 out_overflow:
5608         print_overflow_msg(__func__, xdr);
5609         return -EIO;
5610 }
5611
5612 static int decode_create_session(struct xdr_stream *xdr,
5613                                  struct nfs41_create_session_res *res)
5614 {
5615         __be32 *p;
5616         int status;
5617         struct nfs_client *clp = res->client;
5618         struct nfs4_session *session = clp->cl_session;
5619
5620         status = decode_op_hdr(xdr, OP_CREATE_SESSION);
5621         if (!status)
5622                 status = decode_sessionid(xdr, &session->sess_id);
5623         if (unlikely(status))
5624                 return status;
5625
5626         /* seqid, flags */
5627         p = xdr_inline_decode(xdr, 8);
5628         if (unlikely(!p))
5629                 goto out_overflow;
5630         clp->cl_seqid = be32_to_cpup(p++);
5631         session->flags = be32_to_cpup(p);
5632
5633         /* Channel attributes */
5634         status = decode_chan_attrs(xdr, &session->fc_attrs);
5635         if (!status)
5636                 status = decode_chan_attrs(xdr, &session->bc_attrs);
5637         return status;
5638 out_overflow:
5639         print_overflow_msg(__func__, xdr);
5640         return -EIO;
5641 }
5642
5643 static int decode_destroy_session(struct xdr_stream *xdr, void *dummy)
5644 {
5645         return decode_op_hdr(xdr, OP_DESTROY_SESSION);
5646 }
5647
5648 static int decode_destroy_clientid(struct xdr_stream *xdr, void *dummy)
5649 {
5650         return decode_op_hdr(xdr, OP_DESTROY_CLIENTID);
5651 }
5652
5653 static int decode_reclaim_complete(struct xdr_stream *xdr, void *dummy)
5654 {
5655         return decode_op_hdr(xdr, OP_RECLAIM_COMPLETE);
5656 }
5657 #endif /* CONFIG_NFS_V4_1 */
5658
5659 static int decode_sequence(struct xdr_stream *xdr,
5660                            struct nfs4_sequence_res *res,
5661                            struct rpc_rqst *rqstp)
5662 {
5663 #if defined(CONFIG_NFS_V4_1)
5664         struct nfs4_session *session;
5665         struct nfs4_sessionid id;
5666         u32 dummy;
5667         int status;
5668         __be32 *p;
5669
5670         if (res->sr_slot == NULL)
5671                 return 0;
5672         if (!res->sr_slot->table->session)
5673                 return 0;
5674
5675         status = decode_op_hdr(xdr, OP_SEQUENCE);
5676         if (!status)
5677                 status = decode_sessionid(xdr, &id);
5678         if (unlikely(status))
5679                 goto out_err;
5680
5681         /*
5682          * If the server returns different values for sessionID, slotID or
5683          * sequence number, the server is looney tunes.
5684          */
5685         status = -EREMOTEIO;
5686         session = res->sr_slot->table->session;
5687
5688         if (memcmp(id.data, session->sess_id.data,
5689                    NFS4_MAX_SESSIONID_LEN)) {
5690                 dprintk("%s Invalid session id\n", __func__);
5691                 goto out_err;
5692         }
5693
5694         p = xdr_inline_decode(xdr, 20);
5695         if (unlikely(!p))
5696                 goto out_overflow;
5697
5698         /* seqid */
5699         dummy = be32_to_cpup(p++);
5700         if (dummy != res->sr_slot->seq_nr) {
5701                 dprintk("%s Invalid sequence number\n", __func__);
5702                 goto out_err;
5703         }
5704         /* slot id */
5705         dummy = be32_to_cpup(p++);
5706         if (dummy != res->sr_slot->slot_nr) {
5707                 dprintk("%s Invalid slot id\n", __func__);
5708                 goto out_err;
5709         }
5710         /* highest slot id */
5711         res->sr_highest_slotid = be32_to_cpup(p++);
5712         /* target highest slot id */
5713         res->sr_target_highest_slotid = be32_to_cpup(p++);
5714         /* result flags */
5715         res->sr_status_flags = be32_to_cpup(p);
5716         status = 0;
5717 out_err:
5718         res->sr_status = status;
5719         return status;
5720 out_overflow:
5721         print_overflow_msg(__func__, xdr);
5722         status = -EIO;
5723         goto out_err;
5724 #else  /* CONFIG_NFS_V4_1 */
5725         return 0;
5726 #endif /* CONFIG_NFS_V4_1 */
5727 }
5728
5729 #if defined(CONFIG_NFS_V4_1)
5730 static int decode_getdeviceinfo(struct xdr_stream *xdr,
5731                                 struct pnfs_device *pdev)
5732 {
5733         __be32 *p;
5734         uint32_t len, type;
5735         int status;
5736
5737         status = decode_op_hdr(xdr, OP_GETDEVICEINFO);
5738         if (status) {
5739                 if (status == -ETOOSMALL) {
5740                         p = xdr_inline_decode(xdr, 4);
5741                         if (unlikely(!p))
5742                                 goto out_overflow;
5743                         pdev->mincount = be32_to_cpup(p);
5744                         dprintk("%s: Min count too small. mincnt = %u\n",
5745                                 __func__, pdev->mincount);
5746                 }
5747                 return status;
5748         }
5749
5750         p = xdr_inline_decode(xdr, 8);
5751         if (unlikely(!p))
5752                 goto out_overflow;
5753         type = be32_to_cpup(p++);
5754         if (type != pdev->layout_type) {
5755                 dprintk("%s: layout mismatch req: %u pdev: %u\n",
5756                         __func__, pdev->layout_type, type);
5757                 return -EINVAL;
5758         }
5759         /*
5760          * Get the length of the opaque device_addr4. xdr_read_pages places
5761          * the opaque device_addr4 in the xdr_buf->pages (pnfs_device->pages)
5762          * and places the remaining xdr data in xdr_buf->tail
5763          */
5764         pdev->mincount = be32_to_cpup(p);
5765         if (xdr_read_pages(xdr, pdev->mincount) != pdev->mincount)
5766                 goto out_overflow;
5767
5768         /* Parse notification bitmap, verifying that it is zero. */
5769         p = xdr_inline_decode(xdr, 4);
5770         if (unlikely(!p))
5771                 goto out_overflow;
5772         len = be32_to_cpup(p);
5773         if (len) {
5774                 uint32_t i;
5775
5776                 p = xdr_inline_decode(xdr, 4 * len);
5777                 if (unlikely(!p))
5778                         goto out_overflow;
5779
5780                 if (be32_to_cpup(p++) &
5781                     ~(NOTIFY_DEVICEID4_CHANGE | NOTIFY_DEVICEID4_DELETE)) {
5782                         dprintk("%s: unsupported notification\n",
5783                                 __func__);
5784                 }
5785
5786                 for (i = 1; i < len; i++) {
5787                         if (be32_to_cpup(p++)) {
5788                                 dprintk("%s: unsupported notification\n",
5789                                         __func__);
5790                                 return -EIO;
5791                         }
5792                 }
5793         }
5794         return 0;
5795 out_overflow:
5796         print_overflow_msg(__func__, xdr);
5797         return -EIO;
5798 }
5799
5800 static int decode_layoutget(struct xdr_stream *xdr, struct rpc_rqst *req,
5801                             struct nfs4_layoutget_res *res)
5802 {
5803         __be32 *p;
5804         int status;
5805         u32 layout_count;
5806         u32 recvd;
5807
5808         status = decode_op_hdr(xdr, OP_LAYOUTGET);
5809         if (status)
5810                 return status;
5811         p = xdr_inline_decode(xdr, 4);
5812         if (unlikely(!p))
5813                 goto out_overflow;
5814         res->return_on_close = be32_to_cpup(p);
5815         decode_stateid(xdr, &res->stateid);
5816         p = xdr_inline_decode(xdr, 4);
5817         if (unlikely(!p))
5818                 goto out_overflow;
5819         layout_count = be32_to_cpup(p);
5820         if (!layout_count) {
5821                 dprintk("%s: server responded with empty layout array\n",
5822                         __func__);
5823                 return -EINVAL;
5824         }
5825
5826         p = xdr_inline_decode(xdr, 28);
5827         if (unlikely(!p))
5828                 goto out_overflow;
5829         p = xdr_decode_hyper(p, &res->range.offset);
5830         p = xdr_decode_hyper(p, &res->range.length);
5831         res->range.iomode = be32_to_cpup(p++);
5832         res->type = be32_to_cpup(p++);
5833         res->layoutp->len = be32_to_cpup(p);
5834
5835         dprintk("%s roff:%lu rlen:%lu riomode:%d, lo_type:0x%x, lo.len:%d\n",
5836                 __func__,
5837                 (unsigned long)res->range.offset,
5838                 (unsigned long)res->range.length,
5839                 res->range.iomode,
5840                 res->type,
5841                 res->layoutp->len);
5842
5843         recvd = xdr_read_pages(xdr, res->layoutp->len);
5844         if (res->layoutp->len > recvd) {
5845                 dprintk("NFS: server cheating in layoutget reply: "
5846                                 "layout len %u > recvd %u\n",
5847                                 res->layoutp->len, recvd);
5848                 return -EINVAL;
5849         }
5850
5851         if (layout_count > 1) {
5852                 /* We only handle a length one array at the moment.  Any
5853                  * further entries are just ignored.  Note that this means
5854                  * the client may see a response that is less than the
5855                  * minimum it requested.
5856                  */
5857                 dprintk("%s: server responded with %d layouts, dropping tail\n",
5858                         __func__, layout_count);
5859         }
5860
5861         return 0;
5862 out_overflow:
5863         print_overflow_msg(__func__, xdr);
5864         return -EIO;
5865 }
5866
5867 static int decode_layoutreturn(struct xdr_stream *xdr,
5868                                struct nfs4_layoutreturn_res *res)
5869 {
5870         __be32 *p;
5871         int status;
5872
5873         status = decode_op_hdr(xdr, OP_LAYOUTRETURN);
5874         if (status)
5875                 return status;
5876         p = xdr_inline_decode(xdr, 4);
5877         if (unlikely(!p))
5878                 goto out_overflow;
5879         res->lrs_present = be32_to_cpup(p);
5880         if (res->lrs_present)
5881                 status = decode_stateid(xdr, &res->stateid);
5882         return status;
5883 out_overflow:
5884         print_overflow_msg(__func__, xdr);
5885         return -EIO;
5886 }
5887
5888 static int decode_layoutcommit(struct xdr_stream *xdr,
5889                                struct rpc_rqst *req,
5890                                struct nfs4_layoutcommit_res *res)
5891 {
5892         __be32 *p;
5893         __u32 sizechanged;
5894         int status;
5895
5896         status = decode_op_hdr(xdr, OP_LAYOUTCOMMIT);
5897         res->status = status;
5898         if (status)
5899                 return status;
5900
5901         p = xdr_inline_decode(xdr, 4);
5902         if (unlikely(!p))
5903                 goto out_overflow;
5904         sizechanged = be32_to_cpup(p);
5905
5906         if (sizechanged) {
5907                 /* throw away new size */
5908                 p = xdr_inline_decode(xdr, 8);
5909                 if (unlikely(!p))
5910                         goto out_overflow;
5911         }
5912         return 0;
5913 out_overflow:
5914         print_overflow_msg(__func__, xdr);
5915         return -EIO;
5916 }
5917
5918 static int decode_test_stateid(struct xdr_stream *xdr,
5919                                struct nfs41_test_stateid_res *res)
5920 {
5921         __be32 *p;
5922         int status;
5923         int num_res;
5924
5925         status = decode_op_hdr(xdr, OP_TEST_STATEID);
5926         if (status)
5927                 return status;
5928
5929         p = xdr_inline_decode(xdr, 4);
5930         if (unlikely(!p))
5931                 goto out_overflow;
5932         num_res = be32_to_cpup(p++);
5933         if (num_res != 1)
5934                 goto out;
5935
5936         p = xdr_inline_decode(xdr, 4);
5937         if (unlikely(!p))
5938                 goto out_overflow;
5939         res->status = be32_to_cpup(p++);
5940
5941         return status;
5942 out_overflow:
5943         print_overflow_msg(__func__, xdr);
5944 out:
5945         return -EIO;
5946 }
5947
5948 static int decode_free_stateid(struct xdr_stream *xdr,
5949                                struct nfs41_free_stateid_res *res)
5950 {
5951         res->status = decode_op_hdr(xdr, OP_FREE_STATEID);
5952         return res->status;
5953 }
5954 #endif /* CONFIG_NFS_V4_1 */
5955
5956 /*
5957  * END OF "GENERIC" DECODE ROUTINES.
5958  */
5959
5960 /*
5961  * Decode OPEN_DOWNGRADE response
5962  */
5963 static int nfs4_xdr_dec_open_downgrade(struct rpc_rqst *rqstp,
5964                                        struct xdr_stream *xdr,
5965                                        struct nfs_closeres *res)
5966 {
5967         struct compound_hdr hdr;
5968         int status;
5969
5970         status = decode_compound_hdr(xdr, &hdr);
5971         if (status)
5972                 goto out;
5973         status = decode_sequence(xdr, &res->seq_res, rqstp);
5974         if (status)
5975                 goto out;
5976         status = decode_putfh(xdr);
5977         if (status)
5978                 goto out;
5979         status = decode_open_downgrade(xdr, res);
5980         if (status != 0)
5981                 goto out;
5982         decode_getfattr(xdr, res->fattr, res->server);
5983 out:
5984         return status;
5985 }
5986
5987 /*
5988  * Decode ACCESS response
5989  */
5990 static int nfs4_xdr_dec_access(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
5991                                struct nfs4_accessres *res)
5992 {
5993         struct compound_hdr hdr;
5994         int status;
5995
5996         status = decode_compound_hdr(xdr, &hdr);
5997         if (status)
5998                 goto out;
5999         status = decode_sequence(xdr, &res->seq_res, rqstp);
6000         if (status)
6001                 goto out;
6002         status = decode_putfh(xdr);
6003         if (status != 0)
6004                 goto out;
6005         status = decode_access(xdr, &res->supported, &res->access);
6006         if (status != 0)
6007                 goto out;
6008         decode_getfattr(xdr, res->fattr, res->server);
6009 out:
6010         return status;
6011 }
6012
6013 /*
6014  * Decode LOOKUP response
6015  */
6016 static int nfs4_xdr_dec_lookup(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6017                                struct nfs4_lookup_res *res)
6018 {
6019         struct compound_hdr hdr;
6020         int status;
6021
6022         status = decode_compound_hdr(xdr, &hdr);
6023         if (status)
6024                 goto out;
6025         status = decode_sequence(xdr, &res->seq_res, rqstp);
6026         if (status)
6027                 goto out;
6028         status = decode_putfh(xdr);
6029         if (status)
6030                 goto out;
6031         status = decode_lookup(xdr);
6032         if (status)
6033                 goto out;
6034         status = decode_getfh(xdr, res->fh);
6035         if (status)
6036                 goto out;
6037         status = decode_getfattr_label(xdr, res->fattr, res->label, res->server);
6038 out:
6039         return status;
6040 }
6041
6042 /*
6043  * Decode LOOKUP_ROOT response
6044  */
6045 static int nfs4_xdr_dec_lookup_root(struct rpc_rqst *rqstp,
6046                                     struct xdr_stream *xdr,
6047                                     struct nfs4_lookup_res *res)
6048 {
6049         struct compound_hdr hdr;
6050         int status;
6051
6052         status = decode_compound_hdr(xdr, &hdr);
6053         if (status)
6054                 goto out;
6055         status = decode_sequence(xdr, &res->seq_res, rqstp);
6056         if (status)
6057                 goto out;
6058         status = decode_putrootfh(xdr);
6059         if (status)
6060                 goto out;
6061         status = decode_getfh(xdr, res->fh);
6062         if (status == 0)
6063                 status = decode_getfattr_label(xdr, res->fattr,
6064                                                 res->label, res->server);
6065 out:
6066         return status;
6067 }
6068
6069 /*
6070  * Decode REMOVE response
6071  */
6072 static int nfs4_xdr_dec_remove(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6073                                struct nfs_removeres *res)
6074 {
6075         struct compound_hdr hdr;
6076         int status;
6077
6078         status = decode_compound_hdr(xdr, &hdr);
6079         if (status)
6080                 goto out;
6081         status = decode_sequence(xdr, &res->seq_res, rqstp);
6082         if (status)
6083                 goto out;
6084         status = decode_putfh(xdr);
6085         if (status)
6086                 goto out;
6087         status = decode_remove(xdr, &res->cinfo);
6088 out:
6089         return status;
6090 }
6091
6092 /*
6093  * Decode RENAME response
6094  */
6095 static int nfs4_xdr_dec_rename(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6096                                struct nfs_renameres *res)
6097 {
6098         struct compound_hdr hdr;
6099         int status;
6100
6101         status = decode_compound_hdr(xdr, &hdr);
6102         if (status)
6103                 goto out;
6104         status = decode_sequence(xdr, &res->seq_res, rqstp);
6105         if (status)
6106                 goto out;
6107         status = decode_putfh(xdr);
6108         if (status)
6109                 goto out;
6110         status = decode_savefh(xdr);
6111         if (status)
6112                 goto out;
6113         status = decode_putfh(xdr);
6114         if (status)
6115                 goto out;
6116         status = decode_rename(xdr, &res->old_cinfo, &res->new_cinfo);
6117 out:
6118         return status;
6119 }
6120
6121 /*
6122  * Decode LINK response
6123  */
6124 static int nfs4_xdr_dec_link(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6125                              struct nfs4_link_res *res)
6126 {
6127         struct compound_hdr hdr;
6128         int status;
6129
6130         status = decode_compound_hdr(xdr, &hdr);
6131         if (status)
6132                 goto out;
6133         status = decode_sequence(xdr, &res->seq_res, rqstp);
6134         if (status)
6135                 goto out;
6136         status = decode_putfh(xdr);
6137         if (status)
6138                 goto out;
6139         status = decode_savefh(xdr);
6140         if (status)
6141                 goto out;
6142         status = decode_putfh(xdr);
6143         if (status)
6144                 goto out;
6145         status = decode_link(xdr, &res->cinfo);
6146         if (status)
6147                 goto out;
6148         /*
6149          * Note order: OP_LINK leaves the directory as the current
6150          *             filehandle.
6151          */
6152         status = decode_restorefh(xdr);
6153         if (status)
6154                 goto out;
6155         decode_getfattr_label(xdr, res->fattr, res->label, res->server);
6156 out:
6157         return status;
6158 }
6159
6160 /*
6161  * Decode CREATE response
6162  */
6163 static int nfs4_xdr_dec_create(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6164                                struct nfs4_create_res *res)
6165 {
6166         struct compound_hdr hdr;
6167         int status;
6168
6169         status = decode_compound_hdr(xdr, &hdr);
6170         if (status)
6171                 goto out;
6172         status = decode_sequence(xdr, &res->seq_res, rqstp);
6173         if (status)
6174                 goto out;
6175         status = decode_putfh(xdr);
6176         if (status)
6177                 goto out;
6178         status = decode_create(xdr, &res->dir_cinfo);
6179         if (status)
6180                 goto out;
6181         status = decode_getfh(xdr, res->fh);
6182         if (status)
6183                 goto out;
6184         decode_getfattr_label(xdr, res->fattr, res->label, res->server);
6185 out:
6186         return status;
6187 }
6188
6189 /*
6190  * Decode SYMLINK response
6191  */
6192 static int nfs4_xdr_dec_symlink(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6193                                 struct nfs4_create_res *res)
6194 {
6195         return nfs4_xdr_dec_create(rqstp, xdr, res);
6196 }
6197
6198 /*
6199  * Decode GETATTR response
6200  */
6201 static int nfs4_xdr_dec_getattr(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6202                                 struct nfs4_getattr_res *res)
6203 {
6204         struct compound_hdr hdr;
6205         int status;
6206
6207         status = decode_compound_hdr(xdr, &hdr);
6208         if (status)
6209                 goto out;
6210         status = decode_sequence(xdr, &res->seq_res, rqstp);
6211         if (status)
6212                 goto out;
6213         status = decode_putfh(xdr);
6214         if (status)
6215                 goto out;
6216         status = decode_getfattr_label(xdr, res->fattr, res->label, res->server);
6217 out:
6218         return status;
6219 }
6220
6221 /*
6222  * Encode an SETACL request
6223  */
6224 static void nfs4_xdr_enc_setacl(struct rpc_rqst *req, struct xdr_stream *xdr,
6225                                 struct nfs_setaclargs *args)
6226 {
6227         struct compound_hdr hdr = {
6228                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
6229         };
6230
6231         encode_compound_hdr(xdr, req, &hdr);
6232         encode_sequence(xdr, &args->seq_args, &hdr);
6233         encode_putfh(xdr, args->fh, &hdr);
6234         encode_setacl(xdr, args, &hdr);
6235         encode_nops(&hdr);
6236 }
6237
6238 /*
6239  * Decode SETACL response
6240  */
6241 static int
6242 nfs4_xdr_dec_setacl(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6243                     struct nfs_setaclres *res)
6244 {
6245         struct compound_hdr hdr;
6246         int status;
6247
6248         status = decode_compound_hdr(xdr, &hdr);
6249         if (status)
6250                 goto out;
6251         status = decode_sequence(xdr, &res->seq_res, rqstp);
6252         if (status)
6253                 goto out;
6254         status = decode_putfh(xdr);
6255         if (status)
6256                 goto out;
6257         status = decode_setattr(xdr);
6258 out:
6259         return status;
6260 }
6261
6262 /*
6263  * Decode GETACL response
6264  */
6265 static int
6266 nfs4_xdr_dec_getacl(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6267                     struct nfs_getaclres *res)
6268 {
6269         struct compound_hdr hdr;
6270         int status;
6271
6272         if (res->acl_scratch != NULL) {
6273                 void *p = page_address(res->acl_scratch);
6274                 xdr_set_scratch_buffer(xdr, p, PAGE_SIZE);
6275         }
6276         status = decode_compound_hdr(xdr, &hdr);
6277         if (status)
6278                 goto out;
6279         status = decode_sequence(xdr, &res->seq_res, rqstp);
6280         if (status)
6281                 goto out;
6282         status = decode_putfh(xdr);
6283         if (status)
6284                 goto out;
6285         status = decode_getacl(xdr, rqstp, res);
6286
6287 out:
6288         return status;
6289 }
6290
6291 /*
6292  * Decode CLOSE response
6293  */
6294 static int nfs4_xdr_dec_close(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6295                               struct nfs_closeres *res)
6296 {
6297         struct compound_hdr hdr;
6298         int status;
6299
6300         status = decode_compound_hdr(xdr, &hdr);
6301         if (status)
6302                 goto out;
6303         status = decode_sequence(xdr, &res->seq_res, rqstp);
6304         if (status)
6305                 goto out;
6306         status = decode_putfh(xdr);
6307         if (status)
6308                 goto out;
6309         status = decode_close(xdr, res);
6310         if (status != 0)
6311                 goto out;
6312         /*
6313          * Note: Server may do delete on close for this file
6314          *      in which case the getattr call will fail with
6315          *      an ESTALE error. Shouldn't be a problem,
6316          *      though, since fattr->valid will remain unset.
6317          */
6318         decode_getfattr(xdr, res->fattr, res->server);
6319 out:
6320         return status;
6321 }
6322
6323 /*
6324  * Decode OPEN response
6325  */
6326 static int nfs4_xdr_dec_open(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6327                              struct nfs_openres *res)
6328 {
6329         struct compound_hdr hdr;
6330         int status;
6331
6332         status = decode_compound_hdr(xdr, &hdr);
6333         if (status)
6334                 goto out;
6335         status = decode_sequence(xdr, &res->seq_res, rqstp);
6336         if (status)
6337                 goto out;
6338         status = decode_putfh(xdr);
6339         if (status)
6340                 goto out;
6341         status = decode_open(xdr, res);
6342         if (status)
6343                 goto out;
6344         status = decode_getfh(xdr, &res->fh);
6345         if (status)
6346                 goto out;
6347         if (res->access_request)
6348                 decode_access(xdr, &res->access_supported, &res->access_result);
6349         decode_getfattr_label(xdr, res->f_attr, res->f_label, res->server);
6350 out:
6351         return status;
6352 }
6353
6354 /*
6355  * Decode OPEN_CONFIRM response
6356  */
6357 static int nfs4_xdr_dec_open_confirm(struct rpc_rqst *rqstp,
6358                                      struct xdr_stream *xdr,
6359                                      struct nfs_open_confirmres *res)
6360 {
6361         struct compound_hdr hdr;
6362         int status;
6363
6364         status = decode_compound_hdr(xdr, &hdr);
6365         if (status)
6366                 goto out;
6367         status = decode_putfh(xdr);
6368         if (status)
6369                 goto out;
6370         status = decode_open_confirm(xdr, res);
6371 out:
6372         return status;
6373 }
6374
6375 /*
6376  * Decode OPEN response
6377  */
6378 static int nfs4_xdr_dec_open_noattr(struct rpc_rqst *rqstp,
6379                                     struct xdr_stream *xdr,
6380                                     struct nfs_openres *res)
6381 {
6382         struct compound_hdr hdr;
6383         int status;
6384
6385         status = decode_compound_hdr(xdr, &hdr);
6386         if (status)
6387                 goto out;
6388         status = decode_sequence(xdr, &res->seq_res, rqstp);
6389         if (status)
6390                 goto out;
6391         status = decode_putfh(xdr);
6392         if (status)
6393                 goto out;
6394         status = decode_open(xdr, res);
6395         if (status)
6396                 goto out;
6397         if (res->access_request)
6398                 decode_access(xdr, &res->access_supported, &res->access_result);
6399         decode_getfattr(xdr, res->f_attr, res->server);
6400 out:
6401         return status;
6402 }
6403
6404 /*
6405  * Decode SETATTR response
6406  */
6407 static int nfs4_xdr_dec_setattr(struct rpc_rqst *rqstp,
6408                                 struct xdr_stream *xdr,
6409                                 struct nfs_setattrres *res)
6410 {
6411         struct compound_hdr hdr;
6412         int status;
6413
6414         status = decode_compound_hdr(xdr, &hdr);
6415         if (status)
6416                 goto out;
6417         status = decode_sequence(xdr, &res->seq_res, rqstp);
6418         if (status)
6419                 goto out;
6420         status = decode_putfh(xdr);
6421         if (status)
6422                 goto out;
6423         status = decode_setattr(xdr);
6424         if (status)
6425                 goto out;
6426         decode_getfattr_label(xdr, res->fattr, res->label, res->server);
6427 out:
6428         return status;
6429 }
6430
6431 /*
6432  * Decode LOCK response
6433  */
6434 static int nfs4_xdr_dec_lock(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6435                              struct nfs_lock_res *res)
6436 {
6437         struct compound_hdr hdr;
6438         int status;
6439
6440         status = decode_compound_hdr(xdr, &hdr);
6441         if (status)
6442                 goto out;
6443         status = decode_sequence(xdr, &res->seq_res, rqstp);
6444         if (status)
6445                 goto out;
6446         status = decode_putfh(xdr);
6447         if (status)
6448                 goto out;
6449         status = decode_lock(xdr, res);
6450 out:
6451         return status;
6452 }
6453
6454 /*
6455  * Decode LOCKT response
6456  */
6457 static int nfs4_xdr_dec_lockt(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6458                               struct nfs_lockt_res *res)
6459 {
6460         struct compound_hdr hdr;
6461         int status;
6462
6463         status = decode_compound_hdr(xdr, &hdr);
6464         if (status)
6465                 goto out;
6466         status = decode_sequence(xdr, &res->seq_res, rqstp);
6467         if (status)
6468                 goto out;
6469         status = decode_putfh(xdr);
6470         if (status)
6471                 goto out;
6472         status = decode_lockt(xdr, res);
6473 out:
6474         return status;
6475 }
6476
6477 /*
6478  * Decode LOCKU response
6479  */
6480 static int nfs4_xdr_dec_locku(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6481                               struct nfs_locku_res *res)
6482 {
6483         struct compound_hdr hdr;
6484         int status;
6485
6486         status = decode_compound_hdr(xdr, &hdr);
6487         if (status)
6488                 goto out;
6489         status = decode_sequence(xdr, &res->seq_res, rqstp);
6490         if (status)
6491                 goto out;
6492         status = decode_putfh(xdr);
6493         if (status)
6494                 goto out;
6495         status = decode_locku(xdr, res);
6496 out:
6497         return status;
6498 }
6499
6500 static int nfs4_xdr_dec_release_lockowner(struct rpc_rqst *rqstp,
6501                                           struct xdr_stream *xdr, void *dummy)
6502 {
6503         struct compound_hdr hdr;
6504         int status;
6505
6506         status = decode_compound_hdr(xdr, &hdr);
6507         if (!status)
6508                 status = decode_release_lockowner(xdr);
6509         return status;
6510 }
6511
6512 /*
6513  * Decode READLINK response
6514  */
6515 static int nfs4_xdr_dec_readlink(struct rpc_rqst *rqstp,
6516                                  struct xdr_stream *xdr,
6517                                  struct nfs4_readlink_res *res)
6518 {
6519         struct compound_hdr hdr;
6520         int status;
6521
6522         status = decode_compound_hdr(xdr, &hdr);
6523         if (status)
6524                 goto out;
6525         status = decode_sequence(xdr, &res->seq_res, rqstp);
6526         if (status)
6527                 goto out;
6528         status = decode_putfh(xdr);
6529         if (status)
6530                 goto out;
6531         status = decode_readlink(xdr, rqstp);
6532 out:
6533         return status;
6534 }
6535
6536 /*
6537  * Decode READDIR response
6538  */
6539 static int nfs4_xdr_dec_readdir(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6540                                 struct nfs4_readdir_res *res)
6541 {
6542         struct compound_hdr hdr;
6543         int status;
6544
6545         status = decode_compound_hdr(xdr, &hdr);
6546         if (status)
6547                 goto out;
6548         status = decode_sequence(xdr, &res->seq_res, rqstp);
6549         if (status)
6550                 goto out;
6551         status = decode_putfh(xdr);
6552         if (status)
6553                 goto out;
6554         status = decode_readdir(xdr, rqstp, res);
6555 out:
6556         return status;
6557 }
6558
6559 /*
6560  * Decode Read response
6561  */
6562 static int nfs4_xdr_dec_read(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6563                              struct nfs_pgio_res *res)
6564 {
6565         struct compound_hdr hdr;
6566         int status;
6567
6568         status = decode_compound_hdr(xdr, &hdr);
6569         res->op_status = hdr.status;
6570         if (status)
6571                 goto out;
6572         status = decode_sequence(xdr, &res->seq_res, rqstp);
6573         if (status)
6574                 goto out;
6575         status = decode_putfh(xdr);
6576         if (status)
6577                 goto out;
6578         status = decode_read(xdr, rqstp, res);
6579         if (!status)
6580                 status = res->count;
6581 out:
6582         return status;
6583 }
6584
6585 /*
6586  * Decode WRITE response
6587  */
6588 static int nfs4_xdr_dec_write(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6589                               struct nfs_pgio_res *res)
6590 {
6591         struct compound_hdr hdr;
6592         int status;
6593
6594         status = decode_compound_hdr(xdr, &hdr);
6595         res->op_status = hdr.status;
6596         if (status)
6597                 goto out;
6598         status = decode_sequence(xdr, &res->seq_res, rqstp);
6599         if (status)
6600                 goto out;
6601         status = decode_putfh(xdr);
6602         if (status)
6603                 goto out;
6604         status = decode_write(xdr, res);
6605         if (status)
6606                 goto out;
6607         if (res->fattr)
6608                 decode_getfattr(xdr, res->fattr, res->server);
6609         if (!status)
6610                 status = res->count;
6611 out:
6612         return status;
6613 }
6614
6615 /*
6616  * Decode COMMIT response
6617  */
6618 static int nfs4_xdr_dec_commit(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6619                                struct nfs_commitres *res)
6620 {
6621         struct compound_hdr hdr;
6622         int status;
6623
6624         status = decode_compound_hdr(xdr, &hdr);
6625         res->op_status = hdr.status;
6626         if (status)
6627                 goto out;
6628         status = decode_sequence(xdr, &res->seq_res, rqstp);
6629         if (status)
6630                 goto out;
6631         status = decode_putfh(xdr);
6632         if (status)
6633                 goto out;
6634         status = decode_commit(xdr, res);
6635 out:
6636         return status;
6637 }
6638
6639 /*
6640  * Decode FSINFO response
6641  */
6642 static int nfs4_xdr_dec_fsinfo(struct rpc_rqst *req, struct xdr_stream *xdr,
6643                                struct nfs4_fsinfo_res *res)
6644 {
6645         struct compound_hdr hdr;
6646         int status;
6647
6648         status = decode_compound_hdr(xdr, &hdr);
6649         if (!status)
6650                 status = decode_sequence(xdr, &res->seq_res, req);
6651         if (!status)
6652                 status = decode_putfh(xdr);
6653         if (!status)
6654                 status = decode_fsinfo(xdr, res->fsinfo);
6655         return status;
6656 }
6657
6658 /*
6659  * Decode PATHCONF response
6660  */
6661 static int nfs4_xdr_dec_pathconf(struct rpc_rqst *req, struct xdr_stream *xdr,
6662                                  struct nfs4_pathconf_res *res)
6663 {
6664         struct compound_hdr hdr;
6665         int status;
6666
6667         status = decode_compound_hdr(xdr, &hdr);
6668         if (!status)
6669                 status = decode_sequence(xdr, &res->seq_res, req);
6670         if (!status)
6671                 status = decode_putfh(xdr);
6672         if (!status)
6673                 status = decode_pathconf(xdr, res->pathconf);
6674         return status;
6675 }
6676
6677 /*
6678  * Decode STATFS response
6679  */
6680 static int nfs4_xdr_dec_statfs(struct rpc_rqst *req, struct xdr_stream *xdr,
6681                                struct nfs4_statfs_res *res)
6682 {
6683         struct compound_hdr hdr;
6684         int status;
6685
6686         status = decode_compound_hdr(xdr, &hdr);
6687         if (!status)
6688                 status = decode_sequence(xdr, &res->seq_res, req);
6689         if (!status)
6690                 status = decode_putfh(xdr);
6691         if (!status)
6692                 status = decode_statfs(xdr, res->fsstat);
6693         return status;
6694 }
6695
6696 /*
6697  * Decode GETATTR_BITMAP response
6698  */
6699 static int nfs4_xdr_dec_server_caps(struct rpc_rqst *req,
6700                                     struct xdr_stream *xdr,
6701                                     struct nfs4_server_caps_res *res)
6702 {
6703         struct compound_hdr hdr;
6704         int status;
6705
6706         status = decode_compound_hdr(xdr, &hdr);
6707         if (status)
6708                 goto out;
6709         status = decode_sequence(xdr, &res->seq_res, req);
6710         if (status)
6711                 goto out;
6712         status = decode_putfh(xdr);
6713         if (status)
6714                 goto out;
6715         status = decode_server_caps(xdr, res);
6716 out:
6717         return status;
6718 }
6719
6720 /*
6721  * Decode RENEW response
6722  */
6723 static int nfs4_xdr_dec_renew(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
6724                               void *__unused)
6725 {
6726         struct compound_hdr hdr;
6727         int status;
6728
6729         status = decode_compound_hdr(xdr, &hdr);
6730         if (!status)
6731                 status = decode_renew(xdr);
6732         return status;
6733 }
6734
6735 /*
6736  * Decode SETCLIENTID response
6737  */
6738 static int nfs4_xdr_dec_setclientid(struct rpc_rqst *req,
6739                                     struct xdr_stream *xdr,
6740                                     struct nfs4_setclientid_res *res)
6741 {
6742         struct compound_hdr hdr;
6743         int status;
6744
6745         status = decode_compound_hdr(xdr, &hdr);
6746         if (!status)
6747                 status = decode_setclientid(xdr, res);
6748         return status;
6749 }
6750
6751 /*
6752  * Decode SETCLIENTID_CONFIRM response
6753  */
6754 static int nfs4_xdr_dec_setclientid_confirm(struct rpc_rqst *req,
6755                                             struct xdr_stream *xdr)
6756 {
6757         struct compound_hdr hdr;
6758         int status;
6759
6760         status = decode_compound_hdr(xdr, &hdr);
6761         if (!status)
6762                 status = decode_setclientid_confirm(xdr);
6763         return status;
6764 }
6765
6766 /*
6767  * Decode DELEGRETURN response
6768  */
6769 static int nfs4_xdr_dec_delegreturn(struct rpc_rqst *rqstp,
6770                                     struct xdr_stream *xdr,
6771                                     struct nfs4_delegreturnres *res)
6772 {
6773         struct compound_hdr hdr;
6774         int status;
6775
6776         status = decode_compound_hdr(xdr, &hdr);
6777         if (status)
6778                 goto out;
6779         status = decode_sequence(xdr, &res->seq_res, rqstp);
6780         if (status)
6781                 goto out;
6782         status = decode_putfh(xdr);
6783         if (status != 0)
6784                 goto out;
6785         status = decode_getfattr(xdr, res->fattr, res->server);
6786         if (status != 0)
6787                 goto out;
6788         status = decode_delegreturn(xdr);
6789 out:
6790         return status;
6791 }
6792
6793 /*
6794  * Decode FS_LOCATIONS response
6795  */
6796 static int nfs4_xdr_dec_fs_locations(struct rpc_rqst *req,
6797                                      struct xdr_stream *xdr,
6798                                      struct nfs4_fs_locations_res *res)
6799 {
6800         struct compound_hdr hdr;
6801         int status;
6802
6803         status = decode_compound_hdr(xdr, &hdr);
6804         if (status)
6805                 goto out;
6806         status = decode_sequence(xdr, &res->seq_res, req);
6807         if (status)
6808                 goto out;
6809         status = decode_putfh(xdr);
6810         if (status)
6811                 goto out;
6812         if (res->migration) {
6813                 xdr_enter_page(xdr, PAGE_SIZE);
6814                 status = decode_getfattr_generic(xdr,
6815                                         &res->fs_locations->fattr,
6816                                          NULL, res->fs_locations,
6817                                          NULL, res->fs_locations->server);
6818                 if (status)
6819                         goto out;
6820                 if (res->renew)
6821                         status = decode_renew(xdr);
6822         } else {
6823                 status = decode_lookup(xdr);
6824                 if (status)
6825                         goto out;
6826                 xdr_enter_page(xdr, PAGE_SIZE);
6827                 status = decode_getfattr_generic(xdr,
6828                                         &res->fs_locations->fattr,
6829                                          NULL, res->fs_locations,
6830                                          NULL, res->fs_locations->server);
6831         }
6832 out:
6833         return status;
6834 }
6835
6836 /*
6837  * Decode SECINFO response
6838  */
6839 static int nfs4_xdr_dec_secinfo(struct rpc_rqst *rqstp,
6840                                 struct xdr_stream *xdr,
6841                                 struct nfs4_secinfo_res *res)
6842 {
6843         struct compound_hdr hdr;
6844         int status;
6845
6846         status = decode_compound_hdr(xdr, &hdr);
6847         if (status)
6848                 goto out;
6849         status = decode_sequence(xdr, &res->seq_res, rqstp);
6850         if (status)
6851                 goto out;
6852         status = decode_putfh(xdr);
6853         if (status)
6854                 goto out;
6855         status = decode_secinfo(xdr, res);
6856 out:
6857         return status;
6858 }
6859
6860 /*
6861  * Decode FSID_PRESENT response
6862  */
6863 static int nfs4_xdr_dec_fsid_present(struct rpc_rqst *rqstp,
6864                                      struct xdr_stream *xdr,
6865                                      struct nfs4_fsid_present_res *res)
6866 {
6867         struct compound_hdr hdr;
6868         int status;
6869
6870         status = decode_compound_hdr(xdr, &hdr);
6871         if (status)
6872                 goto out;
6873         status = decode_sequence(xdr, &res->seq_res, rqstp);
6874         if (status)
6875                 goto out;
6876         status = decode_putfh(xdr);
6877         if (status)
6878                 goto out;
6879         status = decode_getfh(xdr, res->fh);
6880         if (status)
6881                 goto out;
6882         if (res->renew)
6883                 status = decode_renew(xdr);
6884 out:
6885         return status;
6886 }
6887
6888 #if defined(CONFIG_NFS_V4_1)
6889 /*
6890  * Decode BIND_CONN_TO_SESSION response
6891  */
6892 static int nfs4_xdr_dec_bind_conn_to_session(struct rpc_rqst *rqstp,
6893                                         struct xdr_stream *xdr,
6894                                         void *res)
6895 {
6896         struct compound_hdr hdr;
6897         int status;
6898
6899         status = decode_compound_hdr(xdr, &hdr);
6900         if (!status)
6901                 status = decode_bind_conn_to_session(xdr, res);
6902         return status;
6903 }
6904
6905 /*
6906  * Decode EXCHANGE_ID response
6907  */
6908 static int nfs4_xdr_dec_exchange_id(struct rpc_rqst *rqstp,
6909                                     struct xdr_stream *xdr,
6910                                     void *res)
6911 {
6912         struct compound_hdr hdr;
6913         int status;
6914
6915         status = decode_compound_hdr(xdr, &hdr);
6916         if (!status)
6917                 status = decode_exchange_id(xdr, res);
6918         return status;
6919 }
6920
6921 /*
6922  * Decode CREATE_SESSION response
6923  */
6924 static int nfs4_xdr_dec_create_session(struct rpc_rqst *rqstp,
6925                                        struct xdr_stream *xdr,
6926                                        struct nfs41_create_session_res *res)
6927 {
6928         struct compound_hdr hdr;
6929         int status;
6930
6931         status = decode_compound_hdr(xdr, &hdr);
6932         if (!status)
6933                 status = decode_create_session(xdr, res);
6934         return status;
6935 }
6936
6937 /*
6938  * Decode DESTROY_SESSION response
6939  */
6940 static int nfs4_xdr_dec_destroy_session(struct rpc_rqst *rqstp,
6941                                         struct xdr_stream *xdr,
6942                                         void *res)
6943 {
6944         struct compound_hdr hdr;
6945         int status;
6946
6947         status = decode_compound_hdr(xdr, &hdr);
6948         if (!status)
6949                 status = decode_destroy_session(xdr, res);
6950         return status;
6951 }
6952
6953 /*
6954  * Decode DESTROY_CLIENTID response
6955  */
6956 static int nfs4_xdr_dec_destroy_clientid(struct rpc_rqst *rqstp,
6957                                         struct xdr_stream *xdr,
6958                                         void *res)
6959 {
6960         struct compound_hdr hdr;
6961         int status;
6962
6963         status = decode_compound_hdr(xdr, &hdr);
6964         if (!status)
6965                 status = decode_destroy_clientid(xdr, res);
6966         return status;
6967 }
6968
6969 /*
6970  * Decode SEQUENCE response
6971  */
6972 static int nfs4_xdr_dec_sequence(struct rpc_rqst *rqstp,
6973                                  struct xdr_stream *xdr,
6974                                  struct nfs4_sequence_res *res)
6975 {
6976         struct compound_hdr hdr;
6977         int status;
6978
6979         status = decode_compound_hdr(xdr, &hdr);
6980         if (!status)
6981                 status = decode_sequence(xdr, res, rqstp);
6982         return status;
6983 }
6984
6985 /*
6986  * Decode GET_LEASE_TIME response
6987  */
6988 static int nfs4_xdr_dec_get_lease_time(struct rpc_rqst *rqstp,
6989                                        struct xdr_stream *xdr,
6990                                        struct nfs4_get_lease_time_res *res)
6991 {
6992         struct compound_hdr hdr;
6993         int status;
6994
6995         status = decode_compound_hdr(xdr, &hdr);
6996         if (!status)
6997                 status = decode_sequence(xdr, &res->lr_seq_res, rqstp);
6998         if (!status)
6999                 status = decode_putrootfh(xdr);
7000         if (!status)
7001                 status = decode_fsinfo(xdr, res->lr_fsinfo);
7002         return status;
7003 }
7004
7005 /*
7006  * Decode RECLAIM_COMPLETE response
7007  */
7008 static int nfs4_xdr_dec_reclaim_complete(struct rpc_rqst *rqstp,
7009                                          struct xdr_stream *xdr,
7010                                          struct nfs41_reclaim_complete_res *res)
7011 {
7012         struct compound_hdr hdr;
7013         int status;
7014
7015         status = decode_compound_hdr(xdr, &hdr);
7016         if (!status)
7017                 status = decode_sequence(xdr, &res->seq_res, rqstp);
7018         if (!status)
7019                 status = decode_reclaim_complete(xdr, NULL);
7020         return status;
7021 }
7022
7023 /*
7024  * Decode GETDEVINFO response
7025  */
7026 static int nfs4_xdr_dec_getdeviceinfo(struct rpc_rqst *rqstp,
7027                                       struct xdr_stream *xdr,
7028                                       struct nfs4_getdeviceinfo_res *res)
7029 {
7030         struct compound_hdr hdr;
7031         int status;
7032
7033         status = decode_compound_hdr(xdr, &hdr);
7034         if (status != 0)
7035                 goto out;
7036         status = decode_sequence(xdr, &res->seq_res, rqstp);
7037         if (status != 0)
7038                 goto out;
7039         status = decode_getdeviceinfo(xdr, res->pdev);
7040 out:
7041         return status;
7042 }
7043
7044 /*
7045  * Decode LAYOUTGET response
7046  */
7047 static int nfs4_xdr_dec_layoutget(struct rpc_rqst *rqstp,
7048                                   struct xdr_stream *xdr,
7049                                   struct nfs4_layoutget_res *res)
7050 {
7051         struct compound_hdr hdr;
7052         int status;
7053
7054         status = decode_compound_hdr(xdr, &hdr);
7055         if (status)
7056                 goto out;
7057         status = decode_sequence(xdr, &res->seq_res, rqstp);
7058         if (status)
7059                 goto out;
7060         status = decode_putfh(xdr);
7061         if (status)
7062                 goto out;
7063         status = decode_layoutget(xdr, rqstp, res);
7064 out:
7065         return status;
7066 }
7067
7068 /*
7069  * Decode LAYOUTRETURN response
7070  */
7071 static int nfs4_xdr_dec_layoutreturn(struct rpc_rqst *rqstp,
7072                                      struct xdr_stream *xdr,
7073                                      struct nfs4_layoutreturn_res *res)
7074 {
7075         struct compound_hdr hdr;
7076         int status;
7077
7078         status = decode_compound_hdr(xdr, &hdr);
7079         if (status)
7080                 goto out;
7081         status = decode_sequence(xdr, &res->seq_res, rqstp);
7082         if (status)
7083                 goto out;
7084         status = decode_putfh(xdr);
7085         if (status)
7086                 goto out;
7087         status = decode_layoutreturn(xdr, res);
7088 out:
7089         return status;
7090 }
7091
7092 /*
7093  * Decode LAYOUTCOMMIT response
7094  */
7095 static int nfs4_xdr_dec_layoutcommit(struct rpc_rqst *rqstp,
7096                                      struct xdr_stream *xdr,
7097                                      struct nfs4_layoutcommit_res *res)
7098 {
7099         struct compound_hdr hdr;
7100         int status;
7101
7102         status = decode_compound_hdr(xdr, &hdr);
7103         if (status)
7104                 goto out;
7105         status = decode_sequence(xdr, &res->seq_res, rqstp);
7106         if (status)
7107                 goto out;
7108         status = decode_putfh(xdr);
7109         if (status)
7110                 goto out;
7111         status = decode_layoutcommit(xdr, rqstp, res);
7112         if (status)
7113                 goto out;
7114         decode_getfattr(xdr, res->fattr, res->server);
7115 out:
7116         return status;
7117 }
7118
7119 /*
7120  * Decode SECINFO_NO_NAME response
7121  */
7122 static int nfs4_xdr_dec_secinfo_no_name(struct rpc_rqst *rqstp,
7123                                         struct xdr_stream *xdr,
7124                                         struct nfs4_secinfo_res *res)
7125 {
7126         struct compound_hdr hdr;
7127         int status;
7128
7129         status = decode_compound_hdr(xdr, &hdr);
7130         if (status)
7131                 goto out;
7132         status = decode_sequence(xdr, &res->seq_res, rqstp);
7133         if (status)
7134                 goto out;
7135         status = decode_putrootfh(xdr);
7136         if (status)
7137                 goto out;
7138         status = decode_secinfo_no_name(xdr, res);
7139 out:
7140         return status;
7141 }
7142
7143 /*
7144  * Decode TEST_STATEID response
7145  */
7146 static int nfs4_xdr_dec_test_stateid(struct rpc_rqst *rqstp,
7147                                      struct xdr_stream *xdr,
7148                                      struct nfs41_test_stateid_res *res)
7149 {
7150         struct compound_hdr hdr;
7151         int status;
7152
7153         status = decode_compound_hdr(xdr, &hdr);
7154         if (status)
7155                 goto out;
7156         status = decode_sequence(xdr, &res->seq_res, rqstp);
7157         if (status)
7158                 goto out;
7159         status = decode_test_stateid(xdr, res);
7160 out:
7161         return status;
7162 }
7163
7164 /*
7165  * Decode FREE_STATEID response
7166  */
7167 static int nfs4_xdr_dec_free_stateid(struct rpc_rqst *rqstp,
7168                                      struct xdr_stream *xdr,
7169                                      struct nfs41_free_stateid_res *res)
7170 {
7171         struct compound_hdr hdr;
7172         int status;
7173
7174         status = decode_compound_hdr(xdr, &hdr);
7175         if (status)
7176                 goto out;
7177         status = decode_sequence(xdr, &res->seq_res, rqstp);
7178         if (status)
7179                 goto out;
7180         status = decode_free_stateid(xdr, res);
7181 out:
7182         return status;
7183 }
7184 #endif /* CONFIG_NFS_V4_1 */
7185
7186 /**
7187  * nfs4_decode_dirent - Decode a single NFSv4 directory entry stored in
7188  *                      the local page cache.
7189  * @xdr: XDR stream where entry resides
7190  * @entry: buffer to fill in with entry data
7191  * @plus: boolean indicating whether this should be a readdirplus entry
7192  *
7193  * Returns zero if successful, otherwise a negative errno value is
7194  * returned.
7195  *
7196  * This function is not invoked during READDIR reply decoding, but
7197  * rather whenever an application invokes the getdents(2) system call
7198  * on a directory already in our cache.
7199  */
7200 int nfs4_decode_dirent(struct xdr_stream *xdr, struct nfs_entry *entry,
7201                        int plus)
7202 {
7203         unsigned int savep;
7204         uint32_t bitmap[3] = {0};
7205         uint32_t len;
7206         __be32 *p = xdr_inline_decode(xdr, 4);
7207         if (unlikely(!p))
7208                 goto out_overflow;
7209         if (*p == xdr_zero) {
7210                 p = xdr_inline_decode(xdr, 4);
7211                 if (unlikely(!p))
7212                         goto out_overflow;
7213                 if (*p == xdr_zero)
7214                         return -EAGAIN;
7215                 entry->eof = 1;
7216                 return -EBADCOOKIE;
7217         }
7218
7219         p = xdr_inline_decode(xdr, 12);
7220         if (unlikely(!p))
7221                 goto out_overflow;
7222         entry->prev_cookie = entry->cookie;
7223         p = xdr_decode_hyper(p, &entry->cookie);
7224         entry->len = be32_to_cpup(p);
7225
7226         p = xdr_inline_decode(xdr, entry->len);
7227         if (unlikely(!p))
7228                 goto out_overflow;
7229         entry->name = (const char *) p;
7230
7231         /*
7232          * In case the server doesn't return an inode number,
7233          * we fake one here.  (We don't use inode number 0,
7234          * since glibc seems to choke on it...)
7235          */
7236         entry->ino = 1;
7237         entry->fattr->valid = 0;
7238
7239         if (decode_attr_bitmap(xdr, bitmap) < 0)
7240                 goto out_overflow;
7241
7242         if (decode_attr_length(xdr, &len, &savep) < 0)
7243                 goto out_overflow;
7244
7245         if (decode_getfattr_attrs(xdr, bitmap, entry->fattr, entry->fh,
7246                         NULL, entry->label, entry->server) < 0)
7247                 goto out_overflow;
7248         if (entry->fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID)
7249                 entry->ino = entry->fattr->mounted_on_fileid;
7250         else if (entry->fattr->valid & NFS_ATTR_FATTR_FILEID)
7251                 entry->ino = entry->fattr->fileid;
7252
7253         entry->d_type = DT_UNKNOWN;
7254         if (entry->fattr->valid & NFS_ATTR_FATTR_TYPE)
7255                 entry->d_type = nfs_umode_to_dtype(entry->fattr->mode);
7256
7257         return 0;
7258
7259 out_overflow:
7260         print_overflow_msg(__func__, xdr);
7261         return -EAGAIN;
7262 }
7263
7264 /*
7265  * We need to translate between nfs status return values and
7266  * the local errno values which may not be the same.
7267  */
7268 static struct {
7269         int stat;
7270         int errno;
7271 } nfs_errtbl[] = {
7272         { NFS4_OK,              0               },
7273         { NFS4ERR_PERM,         -EPERM          },
7274         { NFS4ERR_NOENT,        -ENOENT         },
7275         { NFS4ERR_IO,           -errno_NFSERR_IO},
7276         { NFS4ERR_NXIO,         -ENXIO          },
7277         { NFS4ERR_ACCESS,       -EACCES         },
7278         { NFS4ERR_EXIST,        -EEXIST         },
7279         { NFS4ERR_XDEV,         -EXDEV          },
7280         { NFS4ERR_NOTDIR,       -ENOTDIR        },
7281         { NFS4ERR_ISDIR,        -EISDIR         },
7282         { NFS4ERR_INVAL,        -EINVAL         },
7283         { NFS4ERR_FBIG,         -EFBIG          },
7284         { NFS4ERR_NOSPC,        -ENOSPC         },
7285         { NFS4ERR_ROFS,         -EROFS          },
7286         { NFS4ERR_MLINK,        -EMLINK         },
7287         { NFS4ERR_NAMETOOLONG,  -ENAMETOOLONG   },
7288         { NFS4ERR_NOTEMPTY,     -ENOTEMPTY      },
7289         { NFS4ERR_DQUOT,        -EDQUOT         },
7290         { NFS4ERR_STALE,        -ESTALE         },
7291         { NFS4ERR_BADHANDLE,    -EBADHANDLE     },
7292         { NFS4ERR_BAD_COOKIE,   -EBADCOOKIE     },
7293         { NFS4ERR_NOTSUPP,      -ENOTSUPP       },
7294         { NFS4ERR_TOOSMALL,     -ETOOSMALL      },
7295         { NFS4ERR_SERVERFAULT,  -EREMOTEIO      },
7296         { NFS4ERR_BADTYPE,      -EBADTYPE       },
7297         { NFS4ERR_LOCKED,       -EAGAIN         },
7298         { NFS4ERR_SYMLINK,      -ELOOP          },
7299         { NFS4ERR_OP_ILLEGAL,   -EOPNOTSUPP     },
7300         { NFS4ERR_DEADLOCK,     -EDEADLK        },
7301         { -1,                   -EIO            }
7302 };
7303
7304 /*
7305  * Convert an NFS error code to a local one.
7306  * This one is used jointly by NFSv2 and NFSv3.
7307  */
7308 static int
7309 nfs4_stat_to_errno(int stat)
7310 {
7311         int i;
7312         for (i = 0; nfs_errtbl[i].stat != -1; i++) {
7313                 if (nfs_errtbl[i].stat == stat)
7314                         return nfs_errtbl[i].errno;
7315         }
7316         if (stat <= 10000 || stat > 10100) {
7317                 /* The server is looney tunes. */
7318                 return -EREMOTEIO;
7319         }
7320         /* If we cannot translate the error, the recovery routines should
7321          * handle it.
7322          * Note: remaining NFSv4 error codes have values > 10000, so should
7323          * not conflict with native Linux error codes.
7324          */
7325         return -stat;
7326 }
7327
7328 #ifdef CONFIG_NFS_V4_2
7329 #include "nfs42xdr.c"
7330 #endif /* CONFIG_NFS_V4_2 */
7331
7332 #define PROC(proc, argtype, restype)                            \
7333 [NFSPROC4_CLNT_##proc] = {                                      \
7334         .p_proc   = NFSPROC4_COMPOUND,                          \
7335         .p_encode = (kxdreproc_t)nfs4_xdr_##argtype,            \
7336         .p_decode = (kxdrdproc_t)nfs4_xdr_##restype,            \
7337         .p_arglen = NFS4_##argtype##_sz,                        \
7338         .p_replen = NFS4_##restype##_sz,                        \
7339         .p_statidx = NFSPROC4_CLNT_##proc,                      \
7340         .p_name   = #proc,                                      \
7341 }
7342
7343 struct rpc_procinfo     nfs4_procedures[] = {
7344         PROC(READ,              enc_read,               dec_read),
7345         PROC(WRITE,             enc_write,              dec_write),
7346         PROC(COMMIT,            enc_commit,             dec_commit),
7347         PROC(OPEN,              enc_open,               dec_open),
7348         PROC(OPEN_CONFIRM,      enc_open_confirm,       dec_open_confirm),
7349         PROC(OPEN_NOATTR,       enc_open_noattr,        dec_open_noattr),
7350         PROC(OPEN_DOWNGRADE,    enc_open_downgrade,     dec_open_downgrade),
7351         PROC(CLOSE,             enc_close,              dec_close),
7352         PROC(SETATTR,           enc_setattr,            dec_setattr),
7353         PROC(FSINFO,            enc_fsinfo,             dec_fsinfo),
7354         PROC(RENEW,             enc_renew,              dec_renew),
7355         PROC(SETCLIENTID,       enc_setclientid,        dec_setclientid),
7356         PROC(SETCLIENTID_CONFIRM, enc_setclientid_confirm, dec_setclientid_confirm),
7357         PROC(LOCK,              enc_lock,               dec_lock),
7358         PROC(LOCKT,             enc_lockt,              dec_lockt),
7359         PROC(LOCKU,             enc_locku,              dec_locku),
7360         PROC(ACCESS,            enc_access,             dec_access),
7361         PROC(GETATTR,           enc_getattr,            dec_getattr),
7362         PROC(LOOKUP,            enc_lookup,             dec_lookup),
7363         PROC(LOOKUP_ROOT,       enc_lookup_root,        dec_lookup_root),
7364         PROC(REMOVE,            enc_remove,             dec_remove),
7365         PROC(RENAME,            enc_rename,             dec_rename),
7366         PROC(LINK,              enc_link,               dec_link),
7367         PROC(SYMLINK,           enc_symlink,            dec_symlink),
7368         PROC(CREATE,            enc_create,             dec_create),
7369         PROC(PATHCONF,          enc_pathconf,           dec_pathconf),
7370         PROC(STATFS,            enc_statfs,             dec_statfs),
7371         PROC(READLINK,          enc_readlink,           dec_readlink),
7372         PROC(READDIR,           enc_readdir,            dec_readdir),
7373         PROC(SERVER_CAPS,       enc_server_caps,        dec_server_caps),
7374         PROC(DELEGRETURN,       enc_delegreturn,        dec_delegreturn),
7375         PROC(GETACL,            enc_getacl,             dec_getacl),
7376         PROC(SETACL,            enc_setacl,             dec_setacl),
7377         PROC(FS_LOCATIONS,      enc_fs_locations,       dec_fs_locations),
7378         PROC(RELEASE_LOCKOWNER, enc_release_lockowner,  dec_release_lockowner),
7379         PROC(SECINFO,           enc_secinfo,            dec_secinfo),
7380         PROC(FSID_PRESENT,      enc_fsid_present,       dec_fsid_present),
7381 #if defined(CONFIG_NFS_V4_1)
7382         PROC(EXCHANGE_ID,       enc_exchange_id,        dec_exchange_id),
7383         PROC(CREATE_SESSION,    enc_create_session,     dec_create_session),
7384         PROC(DESTROY_SESSION,   enc_destroy_session,    dec_destroy_session),
7385         PROC(SEQUENCE,          enc_sequence,           dec_sequence),
7386         PROC(GET_LEASE_TIME,    enc_get_lease_time,     dec_get_lease_time),
7387         PROC(RECLAIM_COMPLETE,  enc_reclaim_complete,   dec_reclaim_complete),
7388         PROC(GETDEVICEINFO,     enc_getdeviceinfo,      dec_getdeviceinfo),
7389         PROC(LAYOUTGET,         enc_layoutget,          dec_layoutget),
7390         PROC(LAYOUTCOMMIT,      enc_layoutcommit,       dec_layoutcommit),
7391         PROC(LAYOUTRETURN,      enc_layoutreturn,       dec_layoutreturn),
7392         PROC(SECINFO_NO_NAME,   enc_secinfo_no_name,    dec_secinfo_no_name),
7393         PROC(TEST_STATEID,      enc_test_stateid,       dec_test_stateid),
7394         PROC(FREE_STATEID,      enc_free_stateid,       dec_free_stateid),
7395         PROC(BIND_CONN_TO_SESSION,
7396                         enc_bind_conn_to_session, dec_bind_conn_to_session),
7397         PROC(DESTROY_CLIENTID,  enc_destroy_clientid,   dec_destroy_clientid),
7398 #endif /* CONFIG_NFS_V4_1 */
7399 #ifdef CONFIG_NFS_V4_2
7400         PROC(SEEK,              enc_seek,               dec_seek),
7401         PROC(ALLOCATE,          enc_allocate,           dec_allocate),
7402         PROC(DEALLOCATE,        enc_deallocate,         dec_deallocate),
7403 #endif /* CONFIG_NFS_V4_2 */
7404 };
7405
7406 const struct rpc_version nfs_version4 = {
7407         .number                 = 4,
7408         .nrprocs                = ARRAY_SIZE(nfs4_procedures),
7409         .procs                  = nfs4_procedures
7410 };
7411
7412 /*
7413  * Local variables:
7414  *  c-basic-offset: 8
7415  * End:
7416  */