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