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