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