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