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