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