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