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