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