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