NFSv4: Fix the underestimation of delegation XDR space reservation
[pandora-kernel.git] / fs / nfs / nfs4xdr.c
1 /*
2  *  fs/nfs/nfs4xdr.c
3  *
4  *  Client-side XDR for NFSv4.
5  *
6  *  Copyright (c) 2002 The Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  Kendrick Smith <kmsmith@umich.edu>
10  *  Andy Adamson   <andros@umich.edu>
11  *
12  *  Redistribution and use in source and binary forms, with or without
13  *  modification, are permitted provided that the following conditions
14  *  are met:
15  *
16  *  1. Redistributions of source code must retain the above copyright
17  *     notice, this list of conditions and the following disclaimer.
18  *  2. Redistributions in binary form must reproduce the above copyright
19  *     notice, this list of conditions and the following disclaimer in the
20  *     documentation and/or other materials provided with the distribution.
21  *  3. Neither the name of the University nor the names of its
22  *     contributors may be used to endorse or promote products derived
23  *     from this software without specific prior written permission.
24  *
25  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37
38 #include <linux/param.h>
39 #include <linux/time.h>
40 #include <linux/mm.h>
41 #include <linux/errno.h>
42 #include <linux/string.h>
43 #include <linux/in.h>
44 #include <linux/pagemap.h>
45 #include <linux/proc_fs.h>
46 #include <linux/kdev_t.h>
47 #include <linux/sunrpc/clnt.h>
48 #include <linux/sunrpc/msg_prot.h>
49 #include <linux/sunrpc/gss_api.h>
50 #include <linux/nfs.h>
51 #include <linux/nfs4.h>
52 #include <linux/nfs_fs.h>
53 #include <linux/nfs_idmap.h>
54 #include "nfs4_fs.h"
55 #include "internal.h"
56 #include "pnfs.h"
57
58 #define NFSDBG_FACILITY         NFSDBG_XDR
59
60 /* Mapping from NFS error code to "errno" error code. */
61 #define errno_NFSERR_IO         EIO
62
63 static int nfs4_stat_to_errno(int);
64
65 /* NFSv4 COMPOUND tags are only wanted for debugging purposes */
66 #ifdef DEBUG
67 #define NFS4_MAXTAGLEN          20
68 #else
69 #define NFS4_MAXTAGLEN          0
70 #endif
71
72 /* lock,open owner id:
73  * we currently use size 2 (u64) out of (NFS4_OPAQUE_LIMIT  >> 2)
74  */
75 #define open_owner_id_maxsz     (1 + 1 + 4)
76 #define lock_owner_id_maxsz     (1 + 1 + 4)
77 #define decode_lockowner_maxsz  (1 + XDR_QUADLEN(IDMAP_NAMESZ))
78 #define compound_encode_hdr_maxsz       (3 + (NFS4_MAXTAGLEN >> 2))
79 #define compound_decode_hdr_maxsz       (3 + (NFS4_MAXTAGLEN >> 2))
80 #define op_encode_hdr_maxsz     (1)
81 #define op_decode_hdr_maxsz     (2)
82 #define encode_stateid_maxsz    (XDR_QUADLEN(NFS4_STATEID_SIZE))
83 #define decode_stateid_maxsz    (XDR_QUADLEN(NFS4_STATEID_SIZE))
84 #define encode_verifier_maxsz   (XDR_QUADLEN(NFS4_VERIFIER_SIZE))
85 #define decode_verifier_maxsz   (XDR_QUADLEN(NFS4_VERIFIER_SIZE))
86 #define encode_putfh_maxsz      (op_encode_hdr_maxsz + 1 + \
87                                 (NFS4_FHSIZE >> 2))
88 #define decode_putfh_maxsz      (op_decode_hdr_maxsz)
89 #define encode_putrootfh_maxsz  (op_encode_hdr_maxsz)
90 #define decode_putrootfh_maxsz  (op_decode_hdr_maxsz)
91 #define encode_getfh_maxsz      (op_encode_hdr_maxsz)
92 #define decode_getfh_maxsz      (op_decode_hdr_maxsz + 1 + \
93                                 ((3+NFS4_FHSIZE) >> 2))
94 #define nfs4_fattr_bitmap_maxsz 4
95 #define encode_getattr_maxsz    (op_encode_hdr_maxsz + nfs4_fattr_bitmap_maxsz)
96 #define nfs4_name_maxsz         (1 + ((3 + NFS4_MAXNAMLEN) >> 2))
97 #define nfs4_path_maxsz         (1 + ((3 + NFS4_MAXPATHLEN) >> 2))
98 #define nfs4_owner_maxsz        (1 + XDR_QUADLEN(IDMAP_NAMESZ))
99 #define nfs4_group_maxsz        (1 + XDR_QUADLEN(IDMAP_NAMESZ))
100 /* This is based on getfattr, which uses the most attributes: */
101 #define nfs4_fattr_value_maxsz  (1 + (1 + 2 + 2 + 4 + 2 + 1 + 1 + 2 + 2 + \
102                                 3 + 3 + 3 + nfs4_owner_maxsz + nfs4_group_maxsz))
103 #define nfs4_fattr_maxsz        (nfs4_fattr_bitmap_maxsz + \
104                                 nfs4_fattr_value_maxsz)
105 #define decode_getattr_maxsz    (op_decode_hdr_maxsz + nfs4_fattr_maxsz)
106 #define encode_attrs_maxsz      (nfs4_fattr_bitmap_maxsz + \
107                                  1 + 2 + 1 + \
108                                 nfs4_owner_maxsz + \
109                                 nfs4_group_maxsz + \
110                                 4 + 4)
111 #define encode_savefh_maxsz     (op_encode_hdr_maxsz)
112 #define decode_savefh_maxsz     (op_decode_hdr_maxsz)
113 #define encode_restorefh_maxsz  (op_encode_hdr_maxsz)
114 #define decode_restorefh_maxsz  (op_decode_hdr_maxsz)
115 #define encode_fsinfo_maxsz     (encode_getattr_maxsz)
116 /* The 5 accounts for the PNFS attributes, and assumes that at most three
117  * layout types will be returned.
118  */
119 #define decode_fsinfo_maxsz     (op_decode_hdr_maxsz + \
120                                  nfs4_fattr_bitmap_maxsz + 4 + 8 + 5)
121 #define encode_renew_maxsz      (op_encode_hdr_maxsz + 3)
122 #define decode_renew_maxsz      (op_decode_hdr_maxsz)
123 #define encode_setclientid_maxsz \
124                                 (op_encode_hdr_maxsz + \
125                                 XDR_QUADLEN(NFS4_VERIFIER_SIZE) + \
126                                 XDR_QUADLEN(NFS4_SETCLIENTID_NAMELEN) + \
127                                 1 /* sc_prog */ + \
128                                 XDR_QUADLEN(RPCBIND_MAXNETIDLEN) + \
129                                 XDR_QUADLEN(RPCBIND_MAXUADDRLEN) + \
130                                 1) /* sc_cb_ident */
131 #define decode_setclientid_maxsz \
132                                 (op_decode_hdr_maxsz + \
133                                 2 + \
134                                 1024) /* large value for CLID_INUSE */
135 #define encode_setclientid_confirm_maxsz \
136                                 (op_encode_hdr_maxsz + \
137                                 3 + (NFS4_VERIFIER_SIZE >> 2))
138 #define decode_setclientid_confirm_maxsz \
139                                 (op_decode_hdr_maxsz)
140 #define encode_lookup_maxsz     (op_encode_hdr_maxsz + nfs4_name_maxsz)
141 #define decode_lookup_maxsz     (op_decode_hdr_maxsz)
142 #define encode_share_access_maxsz \
143                                 (2)
144 #define encode_createmode_maxsz (1 + encode_attrs_maxsz + encode_verifier_maxsz)
145 #define encode_opentype_maxsz   (1 + encode_createmode_maxsz)
146 #define encode_claim_null_maxsz (1 + nfs4_name_maxsz)
147 #define encode_open_maxsz       (op_encode_hdr_maxsz + \
148                                 2 + encode_share_access_maxsz + 2 + \
149                                 open_owner_id_maxsz + \
150                                 encode_opentype_maxsz + \
151                                 encode_claim_null_maxsz)
152 #define decode_space_limit_maxsz        (3)
153 #define decode_ace_maxsz        (3 + nfs4_owner_maxsz)
154 #define decode_delegation_maxsz (1 + decode_stateid_maxsz + 1 + \
155                                 decode_space_limit_maxsz + \
156                                 decode_ace_maxsz)
157 #define decode_change_info_maxsz        (5)
158 #define decode_open_maxsz       (op_decode_hdr_maxsz + \
159                                 decode_stateid_maxsz + \
160                                 decode_change_info_maxsz + 1 + \
161                                 nfs4_fattr_bitmap_maxsz + \
162                                 decode_delegation_maxsz)
163 #define encode_open_confirm_maxsz \
164                                 (op_encode_hdr_maxsz + \
165                                  encode_stateid_maxsz + 1)
166 #define decode_open_confirm_maxsz \
167                                 (op_decode_hdr_maxsz + \
168                                  decode_stateid_maxsz)
169 #define encode_open_downgrade_maxsz \
170                                 (op_encode_hdr_maxsz + \
171                                  encode_stateid_maxsz + 1 + \
172                                  encode_share_access_maxsz)
173 #define decode_open_downgrade_maxsz \
174                                 (op_decode_hdr_maxsz + \
175                                  decode_stateid_maxsz)
176 #define encode_close_maxsz      (op_encode_hdr_maxsz + \
177                                  1 + encode_stateid_maxsz)
178 #define decode_close_maxsz      (op_decode_hdr_maxsz + \
179                                  decode_stateid_maxsz)
180 #define encode_setattr_maxsz    (op_encode_hdr_maxsz + \
181                                  encode_stateid_maxsz + \
182                                  encode_attrs_maxsz)
183 #define decode_setattr_maxsz    (op_decode_hdr_maxsz + \
184                                  nfs4_fattr_bitmap_maxsz)
185 #define encode_read_maxsz       (op_encode_hdr_maxsz + \
186                                  encode_stateid_maxsz + 3)
187 #define decode_read_maxsz       (op_decode_hdr_maxsz + 2)
188 #define encode_readdir_maxsz    (op_encode_hdr_maxsz + \
189                                  2 + encode_verifier_maxsz + 5)
190 #define decode_readdir_maxsz    (op_decode_hdr_maxsz + \
191                                  decode_verifier_maxsz)
192 #define encode_readlink_maxsz   (op_encode_hdr_maxsz)
193 #define decode_readlink_maxsz   (op_decode_hdr_maxsz + 1)
194 #define encode_write_maxsz      (op_encode_hdr_maxsz + \
195                                  encode_stateid_maxsz + 4)
196 #define decode_write_maxsz      (op_decode_hdr_maxsz + \
197                                  2 + decode_verifier_maxsz)
198 #define encode_commit_maxsz     (op_encode_hdr_maxsz + 3)
199 #define decode_commit_maxsz     (op_decode_hdr_maxsz + \
200                                  decode_verifier_maxsz)
201 #define encode_remove_maxsz     (op_encode_hdr_maxsz + \
202                                 nfs4_name_maxsz)
203 #define decode_remove_maxsz     (op_decode_hdr_maxsz + \
204                                  decode_change_info_maxsz)
205 #define encode_rename_maxsz     (op_encode_hdr_maxsz + \
206                                 2 * nfs4_name_maxsz)
207 #define decode_rename_maxsz     (op_decode_hdr_maxsz + \
208                                  decode_change_info_maxsz + \
209                                  decode_change_info_maxsz)
210 #define encode_link_maxsz       (op_encode_hdr_maxsz + \
211                                 nfs4_name_maxsz)
212 #define decode_link_maxsz       (op_decode_hdr_maxsz + decode_change_info_maxsz)
213 #define encode_lockowner_maxsz  (7)
214 #define encode_lock_maxsz       (op_encode_hdr_maxsz + \
215                                  7 + \
216                                  1 + encode_stateid_maxsz + 1 + \
217                                  encode_lockowner_maxsz)
218 #define decode_lock_denied_maxsz \
219                                 (8 + decode_lockowner_maxsz)
220 #define decode_lock_maxsz       (op_decode_hdr_maxsz + \
221                                  decode_lock_denied_maxsz)
222 #define encode_lockt_maxsz      (op_encode_hdr_maxsz + 5 + \
223                                 encode_lockowner_maxsz)
224 #define decode_lockt_maxsz      (op_decode_hdr_maxsz + \
225                                  decode_lock_denied_maxsz)
226 #define encode_locku_maxsz      (op_encode_hdr_maxsz + 3 + \
227                                  encode_stateid_maxsz + \
228                                  4)
229 #define decode_locku_maxsz      (op_decode_hdr_maxsz + \
230                                  decode_stateid_maxsz)
231 #define encode_release_lockowner_maxsz \
232                                 (op_encode_hdr_maxsz + \
233                                  encode_lockowner_maxsz)
234 #define decode_release_lockowner_maxsz \
235                                 (op_decode_hdr_maxsz)
236 #define encode_access_maxsz     (op_encode_hdr_maxsz + 1)
237 #define decode_access_maxsz     (op_decode_hdr_maxsz + 2)
238 #define encode_symlink_maxsz    (op_encode_hdr_maxsz + \
239                                 1 + nfs4_name_maxsz + \
240                                 1 + \
241                                 nfs4_fattr_maxsz)
242 #define decode_symlink_maxsz    (op_decode_hdr_maxsz + 8)
243 #define encode_create_maxsz     (op_encode_hdr_maxsz + \
244                                 1 + 2 + nfs4_name_maxsz + \
245                                 encode_attrs_maxsz)
246 #define decode_create_maxsz     (op_decode_hdr_maxsz + \
247                                 decode_change_info_maxsz + \
248                                 nfs4_fattr_bitmap_maxsz)
249 #define encode_statfs_maxsz     (encode_getattr_maxsz)
250 #define decode_statfs_maxsz     (decode_getattr_maxsz)
251 #define encode_delegreturn_maxsz (op_encode_hdr_maxsz + 4)
252 #define decode_delegreturn_maxsz (op_decode_hdr_maxsz)
253 #define encode_getacl_maxsz     (encode_getattr_maxsz)
254 #define decode_getacl_maxsz     (op_decode_hdr_maxsz + \
255                                  nfs4_fattr_bitmap_maxsz + 1)
256 #define encode_setacl_maxsz     (op_encode_hdr_maxsz + \
257                                  encode_stateid_maxsz + 3)
258 #define decode_setacl_maxsz     (decode_setattr_maxsz)
259 #define encode_fs_locations_maxsz \
260                                 (encode_getattr_maxsz)
261 #define decode_fs_locations_maxsz \
262                                 (0)
263 #define encode_secinfo_maxsz    (op_encode_hdr_maxsz + nfs4_name_maxsz)
264 #define decode_secinfo_maxsz    (op_decode_hdr_maxsz + 1 + ((NFS_MAX_SECFLAVORS * (16 + GSS_OID_MAX_LEN)) / 4))
265
266 #if defined(CONFIG_NFS_V4_1)
267 #define NFS4_MAX_MACHINE_NAME_LEN (64)
268
269 #define encode_exchange_id_maxsz (op_encode_hdr_maxsz + \
270                                 encode_verifier_maxsz + \
271                                 1 /* co_ownerid.len */ + \
272                                 XDR_QUADLEN(NFS4_EXCHANGE_ID_LEN) + \
273                                 1 /* flags */ + \
274                                 1 /* spa_how */ + \
275                                 0 /* SP4_NONE (for now) */ + \
276                                 1 /* zero implemetation id array */)
277 #define decode_exchange_id_maxsz (op_decode_hdr_maxsz + \
278                                 2 /* eir_clientid */ + \
279                                 1 /* eir_sequenceid */ + \
280                                 1 /* eir_flags */ + \
281                                 1 /* spr_how */ + \
282                                 0 /* SP4_NONE (for now) */ + \
283                                 2 /* eir_server_owner.so_minor_id */ + \
284                                 /* eir_server_owner.so_major_id<> */ \
285                                 XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 + \
286                                 /* eir_server_scope<> */ \
287                                 XDR_QUADLEN(NFS4_OPAQUE_LIMIT) + 1 + \
288                                 1 /* eir_server_impl_id array length */ + \
289                                 0 /* ignored eir_server_impl_id contents */)
290 #define encode_channel_attrs_maxsz  (6 + 1 /* ca_rdma_ird.len (0) */)
291 #define decode_channel_attrs_maxsz  (6 + \
292                                      1 /* ca_rdma_ird.len */ + \
293                                      1 /* ca_rdma_ird */)
294 #define encode_create_session_maxsz  (op_encode_hdr_maxsz + \
295                                      2 /* csa_clientid */ + \
296                                      1 /* csa_sequence */ + \
297                                      1 /* csa_flags */ + \
298                                      encode_channel_attrs_maxsz + \
299                                      encode_channel_attrs_maxsz + \
300                                      1 /* csa_cb_program */ + \
301                                      1 /* csa_sec_parms.len (1) */ + \
302                                      1 /* cb_secflavor (AUTH_SYS) */ + \
303                                      1 /* stamp */ + \
304                                      1 /* machinename.len */ + \
305                                      XDR_QUADLEN(NFS4_MAX_MACHINE_NAME_LEN) + \
306                                      1 /* uid */ + \
307                                      1 /* gid */ + \
308                                      1 /* gids.len (0) */)
309 #define decode_create_session_maxsz  (op_decode_hdr_maxsz +     \
310                                      XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
311                                      1 /* csr_sequence */ + \
312                                      1 /* csr_flags */ + \
313                                      decode_channel_attrs_maxsz + \
314                                      decode_channel_attrs_maxsz)
315 #define encode_destroy_session_maxsz    (op_encode_hdr_maxsz + 4)
316 #define decode_destroy_session_maxsz    (op_decode_hdr_maxsz)
317 #define encode_sequence_maxsz   (op_encode_hdr_maxsz + \
318                                 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + 4)
319 #define decode_sequence_maxsz   (op_decode_hdr_maxsz + \
320                                 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + 5)
321 #define encode_reclaim_complete_maxsz   (op_encode_hdr_maxsz + 4)
322 #define decode_reclaim_complete_maxsz   (op_decode_hdr_maxsz + 4)
323 #define encode_getdevicelist_maxsz (op_encode_hdr_maxsz + 4 + \
324                                 encode_verifier_maxsz)
325 #define decode_getdevicelist_maxsz (op_decode_hdr_maxsz + \
326                                 2 /* nfs_cookie4 gdlr_cookie */ + \
327                                 decode_verifier_maxsz \
328                                   /* verifier4 gdlr_verifier */ + \
329                                 1 /* gdlr_deviceid_list count */ + \
330                                 XDR_QUADLEN(NFS4_PNFS_GETDEVLIST_MAXNUM * \
331                                             NFS4_DEVICEID4_SIZE) \
332                                   /* gdlr_deviceid_list */ + \
333                                 1 /* bool gdlr_eof */)
334 #define encode_getdeviceinfo_maxsz (op_encode_hdr_maxsz + 4 + \
335                                 XDR_QUADLEN(NFS4_DEVICEID4_SIZE))
336 #define decode_getdeviceinfo_maxsz (op_decode_hdr_maxsz + \
337                                 1 /* layout type */ + \
338                                 1 /* opaque devaddr4 length */ + \
339                                   /* devaddr4 payload is read into page */ \
340                                 1 /* notification bitmap length */ + \
341                                 1 /* notification bitmap */)
342 #define encode_layoutget_maxsz  (op_encode_hdr_maxsz + 10 + \
343                                 encode_stateid_maxsz)
344 #define decode_layoutget_maxsz  (op_decode_hdr_maxsz + 8 + \
345                                 decode_stateid_maxsz + \
346                                 XDR_QUADLEN(PNFS_LAYOUT_MAXSIZE))
347 #define encode_layoutcommit_maxsz (op_encode_hdr_maxsz +          \
348                                 2 /* offset */ + \
349                                 2 /* length */ + \
350                                 1 /* reclaim */ + \
351                                 encode_stateid_maxsz + \
352                                 1 /* new offset (true) */ + \
353                                 2 /* last byte written */ + \
354                                 1 /* nt_timechanged (false) */ + \
355                                 1 /* layoutupdate4 layout type */ + \
356                                 1 /* NULL filelayout layoutupdate4 payload */)
357 #define decode_layoutcommit_maxsz (op_decode_hdr_maxsz + 3)
358 #define encode_layoutreturn_maxsz (8 + op_encode_hdr_maxsz + \
359                                 encode_stateid_maxsz + \
360                                 1 /* FIXME: opaque lrf_body always empty at the moment */)
361 #define decode_layoutreturn_maxsz (op_decode_hdr_maxsz + \
362                                 1 + decode_stateid_maxsz)
363 #define encode_secinfo_no_name_maxsz (op_encode_hdr_maxsz + 1)
364 #define decode_secinfo_no_name_maxsz decode_secinfo_maxsz
365 #define encode_test_stateid_maxsz       (op_encode_hdr_maxsz + 2 + \
366                                          XDR_QUADLEN(NFS4_STATEID_SIZE))
367 #define decode_test_stateid_maxsz       (op_decode_hdr_maxsz + 2 + 1)
368 #define encode_free_stateid_maxsz       (op_encode_hdr_maxsz + 1 + \
369                                          XDR_QUADLEN(NFS4_STATEID_SIZE))
370 #define decode_free_stateid_maxsz       (op_decode_hdr_maxsz + 1)
371 #else /* CONFIG_NFS_V4_1 */
372 #define encode_sequence_maxsz   0
373 #define decode_sequence_maxsz   0
374 #endif /* CONFIG_NFS_V4_1 */
375
376 #define NFS4_enc_compound_sz    (1024)  /* XXX: large enough? */
377 #define NFS4_dec_compound_sz    (1024)  /* XXX: large enough? */
378 #define NFS4_enc_read_sz        (compound_encode_hdr_maxsz + \
379                                 encode_sequence_maxsz + \
380                                 encode_putfh_maxsz + \
381                                 encode_read_maxsz)
382 #define NFS4_dec_read_sz        (compound_decode_hdr_maxsz + \
383                                 decode_sequence_maxsz + \
384                                 decode_putfh_maxsz + \
385                                 decode_read_maxsz)
386 #define NFS4_enc_readlink_sz    (compound_encode_hdr_maxsz + \
387                                 encode_sequence_maxsz + \
388                                 encode_putfh_maxsz + \
389                                 encode_readlink_maxsz)
390 #define NFS4_dec_readlink_sz    (compound_decode_hdr_maxsz + \
391                                 decode_sequence_maxsz + \
392                                 decode_putfh_maxsz + \
393                                 decode_readlink_maxsz)
394 #define NFS4_enc_readdir_sz     (compound_encode_hdr_maxsz + \
395                                 encode_sequence_maxsz + \
396                                 encode_putfh_maxsz + \
397                                 encode_readdir_maxsz)
398 #define NFS4_dec_readdir_sz     (compound_decode_hdr_maxsz + \
399                                 decode_sequence_maxsz + \
400                                 decode_putfh_maxsz + \
401                                 decode_readdir_maxsz)
402 #define NFS4_enc_write_sz       (compound_encode_hdr_maxsz + \
403                                 encode_sequence_maxsz + \
404                                 encode_putfh_maxsz + \
405                                 encode_write_maxsz + \
406                                 encode_getattr_maxsz)
407 #define NFS4_dec_write_sz       (compound_decode_hdr_maxsz + \
408                                 decode_sequence_maxsz + \
409                                 decode_putfh_maxsz + \
410                                 decode_write_maxsz + \
411                                 decode_getattr_maxsz)
412 #define NFS4_enc_commit_sz      (compound_encode_hdr_maxsz + \
413                                 encode_sequence_maxsz + \
414                                 encode_putfh_maxsz + \
415                                 encode_commit_maxsz + \
416                                 encode_getattr_maxsz)
417 #define NFS4_dec_commit_sz      (compound_decode_hdr_maxsz + \
418                                 decode_sequence_maxsz + \
419                                 decode_putfh_maxsz + \
420                                 decode_commit_maxsz + \
421                                 decode_getattr_maxsz)
422 #define NFS4_enc_open_sz        (compound_encode_hdr_maxsz + \
423                                 encode_sequence_maxsz + \
424                                 encode_putfh_maxsz + \
425                                 encode_savefh_maxsz + \
426                                 encode_open_maxsz + \
427                                 encode_getfh_maxsz + \
428                                 encode_getattr_maxsz + \
429                                 encode_restorefh_maxsz + \
430                                 encode_getattr_maxsz)
431 #define NFS4_dec_open_sz        (compound_decode_hdr_maxsz + \
432                                 decode_sequence_maxsz + \
433                                 decode_putfh_maxsz + \
434                                 decode_savefh_maxsz + \
435                                 decode_open_maxsz + \
436                                 decode_getfh_maxsz + \
437                                 decode_getattr_maxsz + \
438                                 decode_restorefh_maxsz + \
439                                 decode_getattr_maxsz)
440 #define NFS4_enc_open_confirm_sz \
441                                 (compound_encode_hdr_maxsz + \
442                                  encode_putfh_maxsz + \
443                                  encode_open_confirm_maxsz)
444 #define NFS4_dec_open_confirm_sz \
445                                 (compound_decode_hdr_maxsz + \
446                                  decode_putfh_maxsz + \
447                                  decode_open_confirm_maxsz)
448 #define NFS4_enc_open_noattr_sz (compound_encode_hdr_maxsz + \
449                                         encode_sequence_maxsz + \
450                                         encode_putfh_maxsz + \
451                                         encode_open_maxsz + \
452                                         encode_getattr_maxsz)
453 #define NFS4_dec_open_noattr_sz (compound_decode_hdr_maxsz + \
454                                         decode_sequence_maxsz + \
455                                         decode_putfh_maxsz + \
456                                         decode_open_maxsz + \
457                                         decode_getattr_maxsz)
458 #define NFS4_enc_open_downgrade_sz \
459                                 (compound_encode_hdr_maxsz + \
460                                  encode_sequence_maxsz + \
461                                  encode_putfh_maxsz + \
462                                  encode_open_downgrade_maxsz + \
463                                  encode_getattr_maxsz)
464 #define NFS4_dec_open_downgrade_sz \
465                                 (compound_decode_hdr_maxsz + \
466                                  decode_sequence_maxsz + \
467                                  decode_putfh_maxsz + \
468                                  decode_open_downgrade_maxsz + \
469                                  decode_getattr_maxsz)
470 #define NFS4_enc_close_sz       (compound_encode_hdr_maxsz + \
471                                  encode_sequence_maxsz + \
472                                  encode_putfh_maxsz + \
473                                  encode_close_maxsz + \
474                                  encode_getattr_maxsz)
475 #define NFS4_dec_close_sz       (compound_decode_hdr_maxsz + \
476                                  decode_sequence_maxsz + \
477                                  decode_putfh_maxsz + \
478                                  decode_close_maxsz + \
479                                  decode_getattr_maxsz)
480 #define NFS4_enc_setattr_sz     (compound_encode_hdr_maxsz + \
481                                  encode_sequence_maxsz + \
482                                  encode_putfh_maxsz + \
483                                  encode_setattr_maxsz + \
484                                  encode_getattr_maxsz)
485 #define NFS4_dec_setattr_sz     (compound_decode_hdr_maxsz + \
486                                  decode_sequence_maxsz + \
487                                  decode_putfh_maxsz + \
488                                  decode_setattr_maxsz + \
489                                  decode_getattr_maxsz)
490 #define NFS4_enc_fsinfo_sz      (compound_encode_hdr_maxsz + \
491                                 encode_sequence_maxsz + \
492                                 encode_putfh_maxsz + \
493                                 encode_fsinfo_maxsz)
494 #define NFS4_dec_fsinfo_sz      (compound_decode_hdr_maxsz + \
495                                 decode_sequence_maxsz + \
496                                 decode_putfh_maxsz + \
497                                 decode_fsinfo_maxsz)
498 #define NFS4_enc_renew_sz       (compound_encode_hdr_maxsz + \
499                                 encode_renew_maxsz)
500 #define NFS4_dec_renew_sz       (compound_decode_hdr_maxsz + \
501                                 decode_renew_maxsz)
502 #define NFS4_enc_setclientid_sz (compound_encode_hdr_maxsz + \
503                                 encode_setclientid_maxsz)
504 #define NFS4_dec_setclientid_sz (compound_decode_hdr_maxsz + \
505                                 decode_setclientid_maxsz)
506 #define NFS4_enc_setclientid_confirm_sz \
507                                 (compound_encode_hdr_maxsz + \
508                                 encode_setclientid_confirm_maxsz + \
509                                 encode_putrootfh_maxsz + \
510                                 encode_fsinfo_maxsz)
511 #define NFS4_dec_setclientid_confirm_sz \
512                                 (compound_decode_hdr_maxsz + \
513                                 decode_setclientid_confirm_maxsz + \
514                                 decode_putrootfh_maxsz + \
515                                 decode_fsinfo_maxsz)
516 #define NFS4_enc_lock_sz        (compound_encode_hdr_maxsz + \
517                                 encode_sequence_maxsz + \
518                                 encode_putfh_maxsz + \
519                                 encode_lock_maxsz)
520 #define NFS4_dec_lock_sz        (compound_decode_hdr_maxsz + \
521                                 decode_sequence_maxsz + \
522                                 decode_putfh_maxsz + \
523                                 decode_lock_maxsz)
524 #define NFS4_enc_lockt_sz       (compound_encode_hdr_maxsz + \
525                                 encode_sequence_maxsz + \
526                                 encode_putfh_maxsz + \
527                                 encode_lockt_maxsz)
528 #define NFS4_dec_lockt_sz       (compound_decode_hdr_maxsz + \
529                                  decode_sequence_maxsz + \
530                                  decode_putfh_maxsz + \
531                                  decode_lockt_maxsz)
532 #define NFS4_enc_locku_sz       (compound_encode_hdr_maxsz + \
533                                 encode_sequence_maxsz + \
534                                 encode_putfh_maxsz + \
535                                 encode_locku_maxsz)
536 #define NFS4_dec_locku_sz       (compound_decode_hdr_maxsz + \
537                                 decode_sequence_maxsz + \
538                                 decode_putfh_maxsz + \
539                                 decode_locku_maxsz)
540 #define NFS4_enc_release_lockowner_sz \
541                                 (compound_encode_hdr_maxsz + \
542                                  encode_lockowner_maxsz)
543 #define NFS4_dec_release_lockowner_sz \
544                                 (compound_decode_hdr_maxsz + \
545                                  decode_lockowner_maxsz)
546 #define NFS4_enc_access_sz      (compound_encode_hdr_maxsz + \
547                                 encode_sequence_maxsz + \
548                                 encode_putfh_maxsz + \
549                                 encode_access_maxsz + \
550                                 encode_getattr_maxsz)
551 #define NFS4_dec_access_sz      (compound_decode_hdr_maxsz + \
552                                 decode_sequence_maxsz + \
553                                 decode_putfh_maxsz + \
554                                 decode_access_maxsz + \
555                                 decode_getattr_maxsz)
556 #define NFS4_enc_getattr_sz     (compound_encode_hdr_maxsz + \
557                                 encode_sequence_maxsz + \
558                                 encode_putfh_maxsz + \
559                                 encode_getattr_maxsz)
560 #define NFS4_dec_getattr_sz     (compound_decode_hdr_maxsz + \
561                                 decode_sequence_maxsz + \
562                                 decode_putfh_maxsz + \
563                                 decode_getattr_maxsz)
564 #define NFS4_enc_lookup_sz      (compound_encode_hdr_maxsz + \
565                                 encode_sequence_maxsz + \
566                                 encode_putfh_maxsz + \
567                                 encode_lookup_maxsz + \
568                                 encode_getattr_maxsz + \
569                                 encode_getfh_maxsz)
570 #define NFS4_dec_lookup_sz      (compound_decode_hdr_maxsz + \
571                                 decode_sequence_maxsz + \
572                                 decode_putfh_maxsz + \
573                                 decode_lookup_maxsz + \
574                                 decode_getattr_maxsz + \
575                                 decode_getfh_maxsz)
576 #define NFS4_enc_lookup_root_sz (compound_encode_hdr_maxsz + \
577                                 encode_sequence_maxsz + \
578                                 encode_putrootfh_maxsz + \
579                                 encode_getattr_maxsz + \
580                                 encode_getfh_maxsz)
581 #define NFS4_dec_lookup_root_sz (compound_decode_hdr_maxsz + \
582                                 decode_sequence_maxsz + \
583                                 decode_putrootfh_maxsz + \
584                                 decode_getattr_maxsz + \
585                                 decode_getfh_maxsz)
586 #define NFS4_enc_remove_sz      (compound_encode_hdr_maxsz + \
587                                 encode_sequence_maxsz + \
588                                 encode_putfh_maxsz + \
589                                 encode_remove_maxsz + \
590                                 encode_getattr_maxsz)
591 #define NFS4_dec_remove_sz      (compound_decode_hdr_maxsz + \
592                                 decode_sequence_maxsz + \
593                                 decode_putfh_maxsz + \
594                                 decode_remove_maxsz + \
595                                 decode_getattr_maxsz)
596 #define NFS4_enc_rename_sz      (compound_encode_hdr_maxsz + \
597                                 encode_sequence_maxsz + \
598                                 encode_putfh_maxsz + \
599                                 encode_savefh_maxsz + \
600                                 encode_putfh_maxsz + \
601                                 encode_rename_maxsz + \
602                                 encode_getattr_maxsz + \
603                                 encode_restorefh_maxsz + \
604                                 encode_getattr_maxsz)
605 #define NFS4_dec_rename_sz      (compound_decode_hdr_maxsz + \
606                                 decode_sequence_maxsz + \
607                                 decode_putfh_maxsz + \
608                                 decode_savefh_maxsz + \
609                                 decode_putfh_maxsz + \
610                                 decode_rename_maxsz + \
611                                 decode_getattr_maxsz + \
612                                 decode_restorefh_maxsz + \
613                                 decode_getattr_maxsz)
614 #define NFS4_enc_link_sz        (compound_encode_hdr_maxsz + \
615                                 encode_sequence_maxsz + \
616                                 encode_putfh_maxsz + \
617                                 encode_savefh_maxsz + \
618                                 encode_putfh_maxsz + \
619                                 encode_link_maxsz + \
620                                 decode_getattr_maxsz + \
621                                 encode_restorefh_maxsz + \
622                                 decode_getattr_maxsz)
623 #define NFS4_dec_link_sz        (compound_decode_hdr_maxsz + \
624                                 decode_sequence_maxsz + \
625                                 decode_putfh_maxsz + \
626                                 decode_savefh_maxsz + \
627                                 decode_putfh_maxsz + \
628                                 decode_link_maxsz + \
629                                 decode_getattr_maxsz + \
630                                 decode_restorefh_maxsz + \
631                                 decode_getattr_maxsz)
632 #define NFS4_enc_symlink_sz     (compound_encode_hdr_maxsz + \
633                                 encode_sequence_maxsz + \
634                                 encode_putfh_maxsz + \
635                                 encode_symlink_maxsz + \
636                                 encode_getattr_maxsz + \
637                                 encode_getfh_maxsz)
638 #define NFS4_dec_symlink_sz     (compound_decode_hdr_maxsz + \
639                                 decode_sequence_maxsz + \
640                                 decode_putfh_maxsz + \
641                                 decode_symlink_maxsz + \
642                                 decode_getattr_maxsz + \
643                                 decode_getfh_maxsz)
644 #define NFS4_enc_create_sz      (compound_encode_hdr_maxsz + \
645                                 encode_sequence_maxsz + \
646                                 encode_putfh_maxsz + \
647                                 encode_savefh_maxsz + \
648                                 encode_create_maxsz + \
649                                 encode_getfh_maxsz + \
650                                 encode_getattr_maxsz + \
651                                 encode_restorefh_maxsz + \
652                                 encode_getattr_maxsz)
653 #define NFS4_dec_create_sz      (compound_decode_hdr_maxsz + \
654                                 decode_sequence_maxsz + \
655                                 decode_putfh_maxsz + \
656                                 decode_savefh_maxsz + \
657                                 decode_create_maxsz + \
658                                 decode_getfh_maxsz + \
659                                 decode_getattr_maxsz + \
660                                 decode_restorefh_maxsz + \
661                                 decode_getattr_maxsz)
662 #define NFS4_enc_pathconf_sz    (compound_encode_hdr_maxsz + \
663                                 encode_sequence_maxsz + \
664                                 encode_putfh_maxsz + \
665                                 encode_getattr_maxsz)
666 #define NFS4_dec_pathconf_sz    (compound_decode_hdr_maxsz + \
667                                 decode_sequence_maxsz + \
668                                 decode_putfh_maxsz + \
669                                 decode_getattr_maxsz)
670 #define NFS4_enc_statfs_sz      (compound_encode_hdr_maxsz + \
671                                 encode_sequence_maxsz + \
672                                 encode_putfh_maxsz + \
673                                 encode_statfs_maxsz)
674 #define NFS4_dec_statfs_sz      (compound_decode_hdr_maxsz + \
675                                 decode_sequence_maxsz + \
676                                 decode_putfh_maxsz + \
677                                 decode_statfs_maxsz)
678 #define NFS4_enc_server_caps_sz (compound_encode_hdr_maxsz + \
679                                 encode_sequence_maxsz + \
680                                 encode_putfh_maxsz + \
681                                 encode_getattr_maxsz)
682 #define NFS4_dec_server_caps_sz (compound_decode_hdr_maxsz + \
683                                 decode_sequence_maxsz + \
684                                 decode_putfh_maxsz + \
685                                 decode_getattr_maxsz)
686 #define NFS4_enc_delegreturn_sz (compound_encode_hdr_maxsz + \
687                                 encode_sequence_maxsz + \
688                                 encode_putfh_maxsz + \
689                                 encode_delegreturn_maxsz + \
690                                 encode_getattr_maxsz)
691 #define NFS4_dec_delegreturn_sz (compound_decode_hdr_maxsz + \
692                                 decode_sequence_maxsz + \
693                                 decode_delegreturn_maxsz + \
694                                 decode_getattr_maxsz)
695 #define NFS4_enc_getacl_sz      (compound_encode_hdr_maxsz + \
696                                 encode_sequence_maxsz + \
697                                 encode_putfh_maxsz + \
698                                 encode_getacl_maxsz)
699 #define NFS4_dec_getacl_sz      (compound_decode_hdr_maxsz + \
700                                 decode_sequence_maxsz + \
701                                 decode_putfh_maxsz + \
702                                 decode_getacl_maxsz)
703 #define NFS4_enc_setacl_sz      (compound_encode_hdr_maxsz + \
704                                 encode_sequence_maxsz + \
705                                 encode_putfh_maxsz + \
706                                 encode_setacl_maxsz)
707 #define NFS4_dec_setacl_sz      (compound_decode_hdr_maxsz + \
708                                 decode_sequence_maxsz + \
709                                 decode_putfh_maxsz + \
710                                 decode_setacl_maxsz)
711 #define NFS4_enc_fs_locations_sz \
712                                 (compound_encode_hdr_maxsz + \
713                                  encode_sequence_maxsz + \
714                                  encode_putfh_maxsz + \
715                                  encode_lookup_maxsz + \
716                                  encode_fs_locations_maxsz)
717 #define NFS4_dec_fs_locations_sz \
718                                 (compound_decode_hdr_maxsz + \
719                                  decode_sequence_maxsz + \
720                                  decode_putfh_maxsz + \
721                                  decode_lookup_maxsz + \
722                                  decode_fs_locations_maxsz)
723 #define NFS4_enc_secinfo_sz     (compound_encode_hdr_maxsz + \
724                                 encode_sequence_maxsz + \
725                                 encode_putfh_maxsz + \
726                                 encode_secinfo_maxsz)
727 #define NFS4_dec_secinfo_sz     (compound_decode_hdr_maxsz + \
728                                 decode_sequence_maxsz + \
729                                 decode_putfh_maxsz + \
730                                 decode_secinfo_maxsz)
731 #if defined(CONFIG_NFS_V4_1)
732 #define NFS4_enc_exchange_id_sz \
733                                 (compound_encode_hdr_maxsz + \
734                                  encode_exchange_id_maxsz)
735 #define NFS4_dec_exchange_id_sz \
736                                 (compound_decode_hdr_maxsz + \
737                                  decode_exchange_id_maxsz)
738 #define NFS4_enc_create_session_sz \
739                                 (compound_encode_hdr_maxsz + \
740                                  encode_create_session_maxsz)
741 #define NFS4_dec_create_session_sz \
742                                 (compound_decode_hdr_maxsz + \
743                                  decode_create_session_maxsz)
744 #define NFS4_enc_destroy_session_sz     (compound_encode_hdr_maxsz + \
745                                          encode_destroy_session_maxsz)
746 #define NFS4_dec_destroy_session_sz     (compound_decode_hdr_maxsz + \
747                                          decode_destroy_session_maxsz)
748 #define NFS4_enc_sequence_sz \
749                                 (compound_decode_hdr_maxsz + \
750                                  encode_sequence_maxsz)
751 #define NFS4_dec_sequence_sz \
752                                 (compound_decode_hdr_maxsz + \
753                                  decode_sequence_maxsz)
754 #define NFS4_enc_get_lease_time_sz      (compound_encode_hdr_maxsz + \
755                                          encode_sequence_maxsz + \
756                                          encode_putrootfh_maxsz + \
757                                          encode_fsinfo_maxsz)
758 #define NFS4_dec_get_lease_time_sz      (compound_decode_hdr_maxsz + \
759                                          decode_sequence_maxsz + \
760                                          decode_putrootfh_maxsz + \
761                                          decode_fsinfo_maxsz)
762 #define NFS4_enc_reclaim_complete_sz    (compound_encode_hdr_maxsz + \
763                                          encode_sequence_maxsz + \
764                                          encode_reclaim_complete_maxsz)
765 #define NFS4_dec_reclaim_complete_sz    (compound_decode_hdr_maxsz + \
766                                          decode_sequence_maxsz + \
767                                          decode_reclaim_complete_maxsz)
768 #define NFS4_enc_getdevicelist_sz (compound_encode_hdr_maxsz + \
769                                 encode_sequence_maxsz + \
770                                 encode_putfh_maxsz + \
771                                 encode_getdevicelist_maxsz)
772 #define NFS4_dec_getdevicelist_sz (compound_decode_hdr_maxsz + \
773                                 decode_sequence_maxsz + \
774                                 decode_putfh_maxsz + \
775                                 decode_getdevicelist_maxsz)
776 #define NFS4_enc_getdeviceinfo_sz (compound_encode_hdr_maxsz +    \
777                                 encode_sequence_maxsz +\
778                                 encode_getdeviceinfo_maxsz)
779 #define NFS4_dec_getdeviceinfo_sz (compound_decode_hdr_maxsz +    \
780                                 decode_sequence_maxsz + \
781                                 decode_getdeviceinfo_maxsz)
782 #define NFS4_enc_layoutget_sz   (compound_encode_hdr_maxsz + \
783                                 encode_sequence_maxsz + \
784                                 encode_putfh_maxsz +        \
785                                 encode_layoutget_maxsz)
786 #define NFS4_dec_layoutget_sz   (compound_decode_hdr_maxsz + \
787                                 decode_sequence_maxsz + \
788                                 decode_putfh_maxsz +        \
789                                 decode_layoutget_maxsz)
790 #define NFS4_enc_layoutcommit_sz (compound_encode_hdr_maxsz + \
791                                 encode_sequence_maxsz +\
792                                 encode_putfh_maxsz + \
793                                 encode_layoutcommit_maxsz + \
794                                 encode_getattr_maxsz)
795 #define NFS4_dec_layoutcommit_sz (compound_decode_hdr_maxsz + \
796                                 decode_sequence_maxsz + \
797                                 decode_putfh_maxsz + \
798                                 decode_layoutcommit_maxsz + \
799                                 decode_getattr_maxsz)
800 #define NFS4_enc_layoutreturn_sz (compound_encode_hdr_maxsz + \
801                                 encode_sequence_maxsz + \
802                                 encode_putfh_maxsz + \
803                                 encode_layoutreturn_maxsz)
804 #define NFS4_dec_layoutreturn_sz (compound_decode_hdr_maxsz + \
805                                 decode_sequence_maxsz + \
806                                 decode_putfh_maxsz + \
807                                 decode_layoutreturn_maxsz)
808 #define NFS4_enc_secinfo_no_name_sz     (compound_encode_hdr_maxsz + \
809                                         encode_sequence_maxsz + \
810                                         encode_putrootfh_maxsz +\
811                                         encode_secinfo_no_name_maxsz)
812 #define NFS4_dec_secinfo_no_name_sz     (compound_decode_hdr_maxsz + \
813                                         decode_sequence_maxsz + \
814                                         decode_putrootfh_maxsz + \
815                                         decode_secinfo_no_name_maxsz)
816 #define NFS4_enc_test_stateid_sz        (compound_encode_hdr_maxsz + \
817                                          encode_sequence_maxsz + \
818                                          encode_test_stateid_maxsz)
819 #define NFS4_dec_test_stateid_sz        (compound_decode_hdr_maxsz + \
820                                          decode_sequence_maxsz + \
821                                          decode_test_stateid_maxsz)
822 #define NFS4_enc_free_stateid_sz        (compound_encode_hdr_maxsz + \
823                                          encode_sequence_maxsz + \
824                                          encode_free_stateid_maxsz)
825 #define NFS4_dec_free_stateid_sz        (compound_decode_hdr_maxsz + \
826                                          decode_sequence_maxsz + \
827                                          decode_free_stateid_maxsz)
828
829 const u32 nfs41_maxwrite_overhead = ((RPC_MAX_HEADER_WITH_AUTH +
830                                       compound_encode_hdr_maxsz +
831                                       encode_sequence_maxsz +
832                                       encode_putfh_maxsz +
833                                       encode_getattr_maxsz) *
834                                      XDR_UNIT);
835
836 const u32 nfs41_maxread_overhead = ((RPC_MAX_HEADER_WITH_AUTH +
837                                      compound_decode_hdr_maxsz +
838                                      decode_sequence_maxsz +
839                                      decode_putfh_maxsz) *
840                                     XDR_UNIT);
841 #endif /* CONFIG_NFS_V4_1 */
842
843 static const umode_t nfs_type2fmt[] = {
844         [NF4BAD] = 0,
845         [NF4REG] = S_IFREG,
846         [NF4DIR] = S_IFDIR,
847         [NF4BLK] = S_IFBLK,
848         [NF4CHR] = S_IFCHR,
849         [NF4LNK] = S_IFLNK,
850         [NF4SOCK] = S_IFSOCK,
851         [NF4FIFO] = S_IFIFO,
852         [NF4ATTRDIR] = 0,
853         [NF4NAMEDATTR] = 0,
854 };
855
856 struct compound_hdr {
857         int32_t         status;
858         uint32_t        nops;
859         __be32 *        nops_p;
860         uint32_t        taglen;
861         char *          tag;
862         uint32_t        replen;         /* expected reply words */
863         u32             minorversion;
864 };
865
866 static __be32 *reserve_space(struct xdr_stream *xdr, size_t nbytes)
867 {
868         __be32 *p = xdr_reserve_space(xdr, nbytes);
869         BUG_ON(!p);
870         return p;
871 }
872
873 static void encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
874 {
875         __be32 *p;
876
877         p = xdr_reserve_space(xdr, 4 + len);
878         BUG_ON(p == NULL);
879         xdr_encode_opaque(p, str, len);
880 }
881
882 static void encode_compound_hdr(struct xdr_stream *xdr,
883                                 struct rpc_rqst *req,
884                                 struct compound_hdr *hdr)
885 {
886         __be32 *p;
887         struct rpc_auth *auth = req->rq_cred->cr_auth;
888
889         /* initialize running count of expected bytes in reply.
890          * NOTE: the replied tag SHOULD be the same is the one sent,
891          * but this is not required as a MUST for the server to do so. */
892         hdr->replen = RPC_REPHDRSIZE + auth->au_rslack + 3 + hdr->taglen;
893
894         dprintk("encode_compound: tag=%.*s\n", (int)hdr->taglen, hdr->tag);
895         BUG_ON(hdr->taglen > NFS4_MAXTAGLEN);
896         p = reserve_space(xdr, 4 + hdr->taglen + 8);
897         p = xdr_encode_opaque(p, hdr->tag, hdr->taglen);
898         *p++ = cpu_to_be32(hdr->minorversion);
899         hdr->nops_p = p;
900         *p = cpu_to_be32(hdr->nops);
901 }
902
903 static void encode_nops(struct compound_hdr *hdr)
904 {
905         BUG_ON(hdr->nops > NFS4_MAX_OPS);
906         *hdr->nops_p = htonl(hdr->nops);
907 }
908
909 static void encode_nfs4_verifier(struct xdr_stream *xdr, const nfs4_verifier *verf)
910 {
911         __be32 *p;
912
913         p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
914         BUG_ON(p == NULL);
915         xdr_encode_opaque_fixed(p, verf->data, NFS4_VERIFIER_SIZE);
916 }
917
918 static void encode_attrs(struct xdr_stream *xdr, const struct iattr *iap, const struct nfs_server *server)
919 {
920         char owner_name[IDMAP_NAMESZ];
921         char owner_group[IDMAP_NAMESZ];
922         int owner_namelen = 0;
923         int owner_grouplen = 0;
924         __be32 *p;
925         __be32 *q;
926         int len;
927         uint32_t bmval0 = 0;
928         uint32_t bmval1 = 0;
929
930         /*
931          * We reserve enough space to write the entire attribute buffer at once.
932          * In the worst-case, this would be
933          *   12(bitmap) + 4(attrlen) + 8(size) + 4(mode) + 4(atime) + 4(mtime)
934          *          = 36 bytes, plus any contribution from variable-length fields
935          *            such as owner/group.
936          */
937         len = 16;
938
939         /* Sigh */
940         if (iap->ia_valid & ATTR_SIZE)
941                 len += 8;
942         if (iap->ia_valid & ATTR_MODE)
943                 len += 4;
944         if (iap->ia_valid & ATTR_UID) {
945                 owner_namelen = nfs_map_uid_to_name(server, iap->ia_uid, owner_name, IDMAP_NAMESZ);
946                 if (owner_namelen < 0) {
947                         dprintk("nfs: couldn't resolve uid %d to string\n",
948                                         iap->ia_uid);
949                         /* XXX */
950                         strcpy(owner_name, "nobody");
951                         owner_namelen = sizeof("nobody") - 1;
952                         /* goto out; */
953                 }
954                 len += 4 + (XDR_QUADLEN(owner_namelen) << 2);
955         }
956         if (iap->ia_valid & ATTR_GID) {
957                 owner_grouplen = nfs_map_gid_to_group(server, iap->ia_gid, owner_group, IDMAP_NAMESZ);
958                 if (owner_grouplen < 0) {
959                         dprintk("nfs: couldn't resolve gid %d to string\n",
960                                         iap->ia_gid);
961                         strcpy(owner_group, "nobody");
962                         owner_grouplen = sizeof("nobody") - 1;
963                         /* goto out; */
964                 }
965                 len += 4 + (XDR_QUADLEN(owner_grouplen) << 2);
966         }
967         if (iap->ia_valid & ATTR_ATIME_SET)
968                 len += 16;
969         else if (iap->ia_valid & ATTR_ATIME)
970                 len += 4;
971         if (iap->ia_valid & ATTR_MTIME_SET)
972                 len += 16;
973         else if (iap->ia_valid & ATTR_MTIME)
974                 len += 4;
975         p = reserve_space(xdr, len);
976
977         /*
978          * We write the bitmap length now, but leave the bitmap and the attribute
979          * buffer length to be backfilled at the end of this routine.
980          */
981         *p++ = cpu_to_be32(2);
982         q = p;
983         p += 3;
984
985         if (iap->ia_valid & ATTR_SIZE) {
986                 bmval0 |= FATTR4_WORD0_SIZE;
987                 p = xdr_encode_hyper(p, iap->ia_size);
988         }
989         if (iap->ia_valid & ATTR_MODE) {
990                 bmval1 |= FATTR4_WORD1_MODE;
991                 *p++ = cpu_to_be32(iap->ia_mode & S_IALLUGO);
992         }
993         if (iap->ia_valid & ATTR_UID) {
994                 bmval1 |= FATTR4_WORD1_OWNER;
995                 p = xdr_encode_opaque(p, owner_name, owner_namelen);
996         }
997         if (iap->ia_valid & ATTR_GID) {
998                 bmval1 |= FATTR4_WORD1_OWNER_GROUP;
999                 p = xdr_encode_opaque(p, owner_group, owner_grouplen);
1000         }
1001         if (iap->ia_valid & ATTR_ATIME_SET) {
1002                 bmval1 |= FATTR4_WORD1_TIME_ACCESS_SET;
1003                 *p++ = cpu_to_be32(NFS4_SET_TO_CLIENT_TIME);
1004                 *p++ = cpu_to_be32(0);
1005                 *p++ = cpu_to_be32(iap->ia_atime.tv_sec);
1006                 *p++ = cpu_to_be32(iap->ia_atime.tv_nsec);
1007         }
1008         else if (iap->ia_valid & ATTR_ATIME) {
1009                 bmval1 |= FATTR4_WORD1_TIME_ACCESS_SET;
1010                 *p++ = cpu_to_be32(NFS4_SET_TO_SERVER_TIME);
1011         }
1012         if (iap->ia_valid & ATTR_MTIME_SET) {
1013                 bmval1 |= FATTR4_WORD1_TIME_MODIFY_SET;
1014                 *p++ = cpu_to_be32(NFS4_SET_TO_CLIENT_TIME);
1015                 *p++ = cpu_to_be32(0);
1016                 *p++ = cpu_to_be32(iap->ia_mtime.tv_sec);
1017                 *p++ = cpu_to_be32(iap->ia_mtime.tv_nsec);
1018         }
1019         else if (iap->ia_valid & ATTR_MTIME) {
1020                 bmval1 |= FATTR4_WORD1_TIME_MODIFY_SET;
1021                 *p++ = cpu_to_be32(NFS4_SET_TO_SERVER_TIME);
1022         }
1023
1024         /*
1025          * Now we backfill the bitmap and the attribute buffer length.
1026          */
1027         if (len != ((char *)p - (char *)q) + 4) {
1028                 printk(KERN_ERR "nfs: Attr length error, %u != %Zu\n",
1029                                 len, ((char *)p - (char *)q) + 4);
1030                 BUG();
1031         }
1032         len = (char *)p - (char *)q - 12;
1033         *q++ = htonl(bmval0);
1034         *q++ = htonl(bmval1);
1035         *q = htonl(len);
1036
1037 /* out: */
1038 }
1039
1040 static void encode_access(struct xdr_stream *xdr, u32 access, struct compound_hdr *hdr)
1041 {
1042         __be32 *p;
1043
1044         p = reserve_space(xdr, 8);
1045         *p++ = cpu_to_be32(OP_ACCESS);
1046         *p = cpu_to_be32(access);
1047         hdr->nops++;
1048         hdr->replen += decode_access_maxsz;
1049 }
1050
1051 static void encode_close(struct xdr_stream *xdr, const struct nfs_closeargs *arg, struct compound_hdr *hdr)
1052 {
1053         __be32 *p;
1054
1055         p = reserve_space(xdr, 8+NFS4_STATEID_SIZE);
1056         *p++ = cpu_to_be32(OP_CLOSE);
1057         *p++ = cpu_to_be32(arg->seqid->sequence->counter);
1058         xdr_encode_opaque_fixed(p, arg->stateid->data, NFS4_STATEID_SIZE);
1059         hdr->nops++;
1060         hdr->replen += decode_close_maxsz;
1061 }
1062
1063 static void encode_commit(struct xdr_stream *xdr, const struct nfs_writeargs *args, struct compound_hdr *hdr)
1064 {
1065         __be32 *p;
1066
1067         p = reserve_space(xdr, 16);
1068         *p++ = cpu_to_be32(OP_COMMIT);
1069         p = xdr_encode_hyper(p, args->offset);
1070         *p = cpu_to_be32(args->count);
1071         hdr->nops++;
1072         hdr->replen += decode_commit_maxsz;
1073 }
1074
1075 static void encode_create(struct xdr_stream *xdr, const struct nfs4_create_arg *create, struct compound_hdr *hdr)
1076 {
1077         __be32 *p;
1078
1079         p = reserve_space(xdr, 8);
1080         *p++ = cpu_to_be32(OP_CREATE);
1081         *p = cpu_to_be32(create->ftype);
1082
1083         switch (create->ftype) {
1084         case NF4LNK:
1085                 p = reserve_space(xdr, 4);
1086                 *p = cpu_to_be32(create->u.symlink.len);
1087                 xdr_write_pages(xdr, create->u.symlink.pages, 0, create->u.symlink.len);
1088                 break;
1089
1090         case NF4BLK: case NF4CHR:
1091                 p = reserve_space(xdr, 8);
1092                 *p++ = cpu_to_be32(create->u.device.specdata1);
1093                 *p = cpu_to_be32(create->u.device.specdata2);
1094                 break;
1095
1096         default:
1097                 break;
1098         }
1099
1100         encode_string(xdr, create->name->len, create->name->name);
1101         hdr->nops++;
1102         hdr->replen += decode_create_maxsz;
1103
1104         encode_attrs(xdr, create->attrs, create->server);
1105 }
1106
1107 static void encode_getattr_one(struct xdr_stream *xdr, uint32_t bitmap, struct compound_hdr *hdr)
1108 {
1109         __be32 *p;
1110
1111         p = reserve_space(xdr, 12);
1112         *p++ = cpu_to_be32(OP_GETATTR);
1113         *p++ = cpu_to_be32(1);
1114         *p = cpu_to_be32(bitmap);
1115         hdr->nops++;
1116         hdr->replen += decode_getattr_maxsz;
1117 }
1118
1119 static void encode_getattr_two(struct xdr_stream *xdr, uint32_t bm0, uint32_t bm1, struct compound_hdr *hdr)
1120 {
1121         __be32 *p;
1122
1123         p = reserve_space(xdr, 16);
1124         *p++ = cpu_to_be32(OP_GETATTR);
1125         *p++ = cpu_to_be32(2);
1126         *p++ = cpu_to_be32(bm0);
1127         *p = cpu_to_be32(bm1);
1128         hdr->nops++;
1129         hdr->replen += decode_getattr_maxsz;
1130 }
1131
1132 static void
1133 encode_getattr_three(struct xdr_stream *xdr,
1134                      uint32_t bm0, uint32_t bm1, uint32_t bm2,
1135                      struct compound_hdr *hdr)
1136 {
1137         __be32 *p;
1138
1139         p = reserve_space(xdr, 4);
1140         *p = cpu_to_be32(OP_GETATTR);
1141         if (bm2) {
1142                 p = reserve_space(xdr, 16);
1143                 *p++ = cpu_to_be32(3);
1144                 *p++ = cpu_to_be32(bm0);
1145                 *p++ = cpu_to_be32(bm1);
1146                 *p = cpu_to_be32(bm2);
1147         } else if (bm1) {
1148                 p = reserve_space(xdr, 12);
1149                 *p++ = cpu_to_be32(2);
1150                 *p++ = cpu_to_be32(bm0);
1151                 *p = cpu_to_be32(bm1);
1152         } else {
1153                 p = reserve_space(xdr, 8);
1154                 *p++ = cpu_to_be32(1);
1155                 *p = cpu_to_be32(bm0);
1156         }
1157         hdr->nops++;
1158         hdr->replen += decode_getattr_maxsz;
1159 }
1160
1161 static void encode_getfattr(struct xdr_stream *xdr, const u32* bitmask, struct compound_hdr *hdr)
1162 {
1163         encode_getattr_two(xdr, bitmask[0] & nfs4_fattr_bitmap[0],
1164                            bitmask[1] & nfs4_fattr_bitmap[1], hdr);
1165 }
1166
1167 static void encode_fsinfo(struct xdr_stream *xdr, const u32* bitmask, struct compound_hdr *hdr)
1168 {
1169         encode_getattr_three(xdr,
1170                              bitmask[0] & nfs4_fsinfo_bitmap[0],
1171                              bitmask[1] & nfs4_fsinfo_bitmap[1],
1172                              bitmask[2] & nfs4_fsinfo_bitmap[2],
1173                              hdr);
1174 }
1175
1176 static void encode_fs_locations(struct xdr_stream *xdr, const u32* bitmask, struct compound_hdr *hdr)
1177 {
1178         encode_getattr_two(xdr, bitmask[0] & nfs4_fs_locations_bitmap[0],
1179                            bitmask[1] & nfs4_fs_locations_bitmap[1], hdr);
1180 }
1181
1182 static void encode_getfh(struct xdr_stream *xdr, struct compound_hdr *hdr)
1183 {
1184         __be32 *p;
1185
1186         p = reserve_space(xdr, 4);
1187         *p = cpu_to_be32(OP_GETFH);
1188         hdr->nops++;
1189         hdr->replen += decode_getfh_maxsz;
1190 }
1191
1192 static void encode_link(struct xdr_stream *xdr, const struct qstr *name, struct compound_hdr *hdr)
1193 {
1194         __be32 *p;
1195
1196         p = reserve_space(xdr, 8 + name->len);
1197         *p++ = cpu_to_be32(OP_LINK);
1198         xdr_encode_opaque(p, name->name, name->len);
1199         hdr->nops++;
1200         hdr->replen += decode_link_maxsz;
1201 }
1202
1203 static inline int nfs4_lock_type(struct file_lock *fl, int block)
1204 {
1205         if ((fl->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) == F_RDLCK)
1206                 return block ? NFS4_READW_LT : NFS4_READ_LT;
1207         return block ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
1208 }
1209
1210 static inline uint64_t nfs4_lock_length(struct file_lock *fl)
1211 {
1212         if (fl->fl_end == OFFSET_MAX)
1213                 return ~(uint64_t)0;
1214         return fl->fl_end - fl->fl_start + 1;
1215 }
1216
1217 static void encode_lockowner(struct xdr_stream *xdr, const struct nfs_lowner *lowner)
1218 {
1219         __be32 *p;
1220
1221         p = reserve_space(xdr, 32);
1222         p = xdr_encode_hyper(p, lowner->clientid);
1223         *p++ = cpu_to_be32(20);
1224         p = xdr_encode_opaque_fixed(p, "lock id:", 8);
1225         *p++ = cpu_to_be32(lowner->s_dev);
1226         xdr_encode_hyper(p, lowner->id);
1227 }
1228
1229 /*
1230  * opcode,type,reclaim,offset,length,new_lock_owner = 32
1231  * open_seqid,open_stateid,lock_seqid,lock_owner.clientid, lock_owner.id = 40
1232  */
1233 static void encode_lock(struct xdr_stream *xdr, const struct nfs_lock_args *args, struct compound_hdr *hdr)
1234 {
1235         __be32 *p;
1236
1237         p = reserve_space(xdr, 32);
1238         *p++ = cpu_to_be32(OP_LOCK);
1239         *p++ = cpu_to_be32(nfs4_lock_type(args->fl, args->block));
1240         *p++ = cpu_to_be32(args->reclaim);
1241         p = xdr_encode_hyper(p, args->fl->fl_start);
1242         p = xdr_encode_hyper(p, nfs4_lock_length(args->fl));
1243         *p = cpu_to_be32(args->new_lock_owner);
1244         if (args->new_lock_owner){
1245                 p = reserve_space(xdr, 4+NFS4_STATEID_SIZE+4);
1246                 *p++ = cpu_to_be32(args->open_seqid->sequence->counter);
1247                 p = xdr_encode_opaque_fixed(p, args->open_stateid->data, NFS4_STATEID_SIZE);
1248                 *p++ = cpu_to_be32(args->lock_seqid->sequence->counter);
1249                 encode_lockowner(xdr, &args->lock_owner);
1250         }
1251         else {
1252                 p = reserve_space(xdr, NFS4_STATEID_SIZE+4);
1253                 p = xdr_encode_opaque_fixed(p, args->lock_stateid->data, NFS4_STATEID_SIZE);
1254                 *p = cpu_to_be32(args->lock_seqid->sequence->counter);
1255         }
1256         hdr->nops++;
1257         hdr->replen += decode_lock_maxsz;
1258 }
1259
1260 static void encode_lockt(struct xdr_stream *xdr, const struct nfs_lockt_args *args, struct compound_hdr *hdr)
1261 {
1262         __be32 *p;
1263
1264         p = reserve_space(xdr, 24);
1265         *p++ = cpu_to_be32(OP_LOCKT);
1266         *p++ = cpu_to_be32(nfs4_lock_type(args->fl, 0));
1267         p = xdr_encode_hyper(p, args->fl->fl_start);
1268         p = xdr_encode_hyper(p, nfs4_lock_length(args->fl));
1269         encode_lockowner(xdr, &args->lock_owner);
1270         hdr->nops++;
1271         hdr->replen += decode_lockt_maxsz;
1272 }
1273
1274 static void encode_locku(struct xdr_stream *xdr, const struct nfs_locku_args *args, struct compound_hdr *hdr)
1275 {
1276         __be32 *p;
1277
1278         p = reserve_space(xdr, 12+NFS4_STATEID_SIZE+16);
1279         *p++ = cpu_to_be32(OP_LOCKU);
1280         *p++ = cpu_to_be32(nfs4_lock_type(args->fl, 0));
1281         *p++ = cpu_to_be32(args->seqid->sequence->counter);
1282         p = xdr_encode_opaque_fixed(p, args->stateid->data, NFS4_STATEID_SIZE);
1283         p = xdr_encode_hyper(p, args->fl->fl_start);
1284         xdr_encode_hyper(p, nfs4_lock_length(args->fl));
1285         hdr->nops++;
1286         hdr->replen += decode_locku_maxsz;
1287 }
1288
1289 static void encode_release_lockowner(struct xdr_stream *xdr, const struct nfs_lowner *lowner, struct compound_hdr *hdr)
1290 {
1291         __be32 *p;
1292
1293         p = reserve_space(xdr, 4);
1294         *p = cpu_to_be32(OP_RELEASE_LOCKOWNER);
1295         encode_lockowner(xdr, lowner);
1296         hdr->nops++;
1297         hdr->replen += decode_release_lockowner_maxsz;
1298 }
1299
1300 static void encode_lookup(struct xdr_stream *xdr, const struct qstr *name, struct compound_hdr *hdr)
1301 {
1302         int len = name->len;
1303         __be32 *p;
1304
1305         p = reserve_space(xdr, 8 + len);
1306         *p++ = cpu_to_be32(OP_LOOKUP);
1307         xdr_encode_opaque(p, name->name, len);
1308         hdr->nops++;
1309         hdr->replen += decode_lookup_maxsz;
1310 }
1311
1312 static void encode_share_access(struct xdr_stream *xdr, fmode_t fmode)
1313 {
1314         __be32 *p;
1315
1316         p = reserve_space(xdr, 8);
1317         switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1318         case FMODE_READ:
1319                 *p++ = cpu_to_be32(NFS4_SHARE_ACCESS_READ);
1320                 break;
1321         case FMODE_WRITE:
1322                 *p++ = cpu_to_be32(NFS4_SHARE_ACCESS_WRITE);
1323                 break;
1324         case FMODE_READ|FMODE_WRITE:
1325                 *p++ = cpu_to_be32(NFS4_SHARE_ACCESS_BOTH);
1326                 break;
1327         default:
1328                 *p++ = cpu_to_be32(0);
1329         }
1330         *p = cpu_to_be32(0);            /* for linux, share_deny = 0 always */
1331 }
1332
1333 static inline void encode_openhdr(struct xdr_stream *xdr, const struct nfs_openargs *arg)
1334 {
1335         __be32 *p;
1336  /*
1337  * opcode 4, seqid 4, share_access 4, share_deny 4, clientid 8, ownerlen 4,
1338  * owner 4 = 32
1339  */
1340         p = reserve_space(xdr, 8);
1341         *p++ = cpu_to_be32(OP_OPEN);
1342         *p = cpu_to_be32(arg->seqid->sequence->counter);
1343         encode_share_access(xdr, arg->fmode);
1344         p = reserve_space(xdr, 32);
1345         p = xdr_encode_hyper(p, arg->clientid);
1346         *p++ = cpu_to_be32(20);
1347         p = xdr_encode_opaque_fixed(p, "open id:", 8);
1348         *p++ = cpu_to_be32(arg->server->s_dev);
1349         xdr_encode_hyper(p, arg->id);
1350 }
1351
1352 static inline void encode_createmode(struct xdr_stream *xdr, const struct nfs_openargs *arg)
1353 {
1354         __be32 *p;
1355         struct nfs_client *clp;
1356
1357         p = reserve_space(xdr, 4);
1358         switch(arg->open_flags & O_EXCL) {
1359         case 0:
1360                 *p = cpu_to_be32(NFS4_CREATE_UNCHECKED);
1361                 encode_attrs(xdr, arg->u.attrs, arg->server);
1362                 break;
1363         default:
1364                 clp = arg->server->nfs_client;
1365                 if (clp->cl_mvops->minor_version > 0) {
1366                         if (nfs4_has_persistent_session(clp)) {
1367                                 *p = cpu_to_be32(NFS4_CREATE_GUARDED);
1368                                 encode_attrs(xdr, arg->u.attrs, arg->server);
1369                         } else {
1370                                 struct iattr dummy;
1371
1372                                 *p = cpu_to_be32(NFS4_CREATE_EXCLUSIVE4_1);
1373                                 encode_nfs4_verifier(xdr, &arg->u.verifier);
1374                                 dummy.ia_valid = 0;
1375                                 encode_attrs(xdr, &dummy, arg->server);
1376                         }
1377                 } else {
1378                         *p = cpu_to_be32(NFS4_CREATE_EXCLUSIVE);
1379                         encode_nfs4_verifier(xdr, &arg->u.verifier);
1380                 }
1381         }
1382 }
1383
1384 static void encode_opentype(struct xdr_stream *xdr, const struct nfs_openargs *arg)
1385 {
1386         __be32 *p;
1387
1388         p = reserve_space(xdr, 4);
1389         switch (arg->open_flags & O_CREAT) {
1390         case 0:
1391                 *p = cpu_to_be32(NFS4_OPEN_NOCREATE);
1392                 break;
1393         default:
1394                 BUG_ON(arg->claim != NFS4_OPEN_CLAIM_NULL);
1395                 *p = cpu_to_be32(NFS4_OPEN_CREATE);
1396                 encode_createmode(xdr, arg);
1397         }
1398 }
1399
1400 static inline void encode_delegation_type(struct xdr_stream *xdr, fmode_t delegation_type)
1401 {
1402         __be32 *p;
1403
1404         p = reserve_space(xdr, 4);
1405         switch (delegation_type) {
1406         case 0:
1407                 *p = cpu_to_be32(NFS4_OPEN_DELEGATE_NONE);
1408                 break;
1409         case FMODE_READ:
1410                 *p = cpu_to_be32(NFS4_OPEN_DELEGATE_READ);
1411                 break;
1412         case FMODE_WRITE|FMODE_READ:
1413                 *p = cpu_to_be32(NFS4_OPEN_DELEGATE_WRITE);
1414                 break;
1415         default:
1416                 BUG();
1417         }
1418 }
1419
1420 static inline void encode_claim_null(struct xdr_stream *xdr, const struct qstr *name)
1421 {
1422         __be32 *p;
1423
1424         p = reserve_space(xdr, 4);
1425         *p = cpu_to_be32(NFS4_OPEN_CLAIM_NULL);
1426         encode_string(xdr, name->len, name->name);
1427 }
1428
1429 static inline void encode_claim_previous(struct xdr_stream *xdr, fmode_t type)
1430 {
1431         __be32 *p;
1432
1433         p = reserve_space(xdr, 4);
1434         *p = cpu_to_be32(NFS4_OPEN_CLAIM_PREVIOUS);
1435         encode_delegation_type(xdr, type);
1436 }
1437
1438 static inline void encode_claim_delegate_cur(struct xdr_stream *xdr, const struct qstr *name, const nfs4_stateid *stateid)
1439 {
1440         __be32 *p;
1441
1442         p = reserve_space(xdr, 4+NFS4_STATEID_SIZE);
1443         *p++ = cpu_to_be32(NFS4_OPEN_CLAIM_DELEGATE_CUR);
1444         xdr_encode_opaque_fixed(p, stateid->data, NFS4_STATEID_SIZE);
1445         encode_string(xdr, name->len, name->name);
1446 }
1447
1448 static void encode_open(struct xdr_stream *xdr, const struct nfs_openargs *arg, struct compound_hdr *hdr)
1449 {
1450         encode_openhdr(xdr, arg);
1451         encode_opentype(xdr, arg);
1452         switch (arg->claim) {
1453         case NFS4_OPEN_CLAIM_NULL:
1454                 encode_claim_null(xdr, arg->name);
1455                 break;
1456         case NFS4_OPEN_CLAIM_PREVIOUS:
1457                 encode_claim_previous(xdr, arg->u.delegation_type);
1458                 break;
1459         case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1460                 encode_claim_delegate_cur(xdr, arg->name, &arg->u.delegation);
1461                 break;
1462         default:
1463                 BUG();
1464         }
1465         hdr->nops++;
1466         hdr->replen += decode_open_maxsz;
1467 }
1468
1469 static void encode_open_confirm(struct xdr_stream *xdr, const struct nfs_open_confirmargs *arg, struct compound_hdr *hdr)
1470 {
1471         __be32 *p;
1472
1473         p = reserve_space(xdr, 4+NFS4_STATEID_SIZE+4);
1474         *p++ = cpu_to_be32(OP_OPEN_CONFIRM);
1475         p = xdr_encode_opaque_fixed(p, arg->stateid->data, NFS4_STATEID_SIZE);
1476         *p = cpu_to_be32(arg->seqid->sequence->counter);
1477         hdr->nops++;
1478         hdr->replen += decode_open_confirm_maxsz;
1479 }
1480
1481 static void encode_open_downgrade(struct xdr_stream *xdr, const struct nfs_closeargs *arg, struct compound_hdr *hdr)
1482 {
1483         __be32 *p;
1484
1485         p = reserve_space(xdr, 4+NFS4_STATEID_SIZE+4);
1486         *p++ = cpu_to_be32(OP_OPEN_DOWNGRADE);
1487         p = xdr_encode_opaque_fixed(p, arg->stateid->data, NFS4_STATEID_SIZE);
1488         *p = cpu_to_be32(arg->seqid->sequence->counter);
1489         encode_share_access(xdr, arg->fmode);
1490         hdr->nops++;
1491         hdr->replen += decode_open_downgrade_maxsz;
1492 }
1493
1494 static void
1495 encode_putfh(struct xdr_stream *xdr, const struct nfs_fh *fh, struct compound_hdr *hdr)
1496 {
1497         int len = fh->size;
1498         __be32 *p;
1499
1500         p = reserve_space(xdr, 8 + len);
1501         *p++ = cpu_to_be32(OP_PUTFH);
1502         xdr_encode_opaque(p, fh->data, len);
1503         hdr->nops++;
1504         hdr->replen += decode_putfh_maxsz;
1505 }
1506
1507 static void encode_putrootfh(struct xdr_stream *xdr, struct compound_hdr *hdr)
1508 {
1509         __be32 *p;
1510
1511         p = reserve_space(xdr, 4);
1512         *p = cpu_to_be32(OP_PUTROOTFH);
1513         hdr->nops++;
1514         hdr->replen += decode_putrootfh_maxsz;
1515 }
1516
1517 static void encode_stateid(struct xdr_stream *xdr, const struct nfs_open_context *ctx, const struct nfs_lock_context *l_ctx, int zero_seqid)
1518 {
1519         nfs4_stateid stateid;
1520         __be32 *p;
1521
1522         p = reserve_space(xdr, NFS4_STATEID_SIZE);
1523         if (ctx->state != NULL) {
1524                 nfs4_copy_stateid(&stateid, ctx->state, l_ctx->lockowner, l_ctx->pid);
1525                 if (zero_seqid)
1526                         stateid.stateid.seqid = 0;
1527                 xdr_encode_opaque_fixed(p, stateid.data, NFS4_STATEID_SIZE);
1528         } else
1529                 xdr_encode_opaque_fixed(p, zero_stateid.data, NFS4_STATEID_SIZE);
1530 }
1531
1532 static void encode_read(struct xdr_stream *xdr, const struct nfs_readargs *args, struct compound_hdr *hdr)
1533 {
1534         __be32 *p;
1535
1536         p = reserve_space(xdr, 4);
1537         *p = cpu_to_be32(OP_READ);
1538
1539         encode_stateid(xdr, args->context, args->lock_context,
1540                        hdr->minorversion);
1541
1542         p = reserve_space(xdr, 12);
1543         p = xdr_encode_hyper(p, args->offset);
1544         *p = cpu_to_be32(args->count);
1545         hdr->nops++;
1546         hdr->replen += decode_read_maxsz;
1547 }
1548
1549 static void encode_readdir(struct xdr_stream *xdr, const struct nfs4_readdir_arg *readdir, struct rpc_rqst *req, struct compound_hdr *hdr)
1550 {
1551         uint32_t attrs[2] = {
1552                 FATTR4_WORD0_RDATTR_ERROR,
1553                 FATTR4_WORD1_MOUNTED_ON_FILEID,
1554         };
1555         uint32_t dircount = readdir->count >> 1;
1556         __be32 *p;
1557
1558         if (readdir->plus) {
1559                 attrs[0] |= FATTR4_WORD0_TYPE|FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE|
1560                         FATTR4_WORD0_FSID|FATTR4_WORD0_FILEHANDLE|FATTR4_WORD0_FILEID;
1561                 attrs[1] |= FATTR4_WORD1_MODE|FATTR4_WORD1_NUMLINKS|FATTR4_WORD1_OWNER|
1562                         FATTR4_WORD1_OWNER_GROUP|FATTR4_WORD1_RAWDEV|
1563                         FATTR4_WORD1_SPACE_USED|FATTR4_WORD1_TIME_ACCESS|
1564                         FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
1565                 dircount >>= 1;
1566         }
1567         /* Use mounted_on_fileid only if the server supports it */
1568         if (!(readdir->bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID))
1569                 attrs[0] |= FATTR4_WORD0_FILEID;
1570
1571         p = reserve_space(xdr, 12+NFS4_VERIFIER_SIZE+20);
1572         *p++ = cpu_to_be32(OP_READDIR);
1573         p = xdr_encode_hyper(p, readdir->cookie);
1574         p = xdr_encode_opaque_fixed(p, readdir->verifier.data, NFS4_VERIFIER_SIZE);
1575         *p++ = cpu_to_be32(dircount);
1576         *p++ = cpu_to_be32(readdir->count);
1577         *p++ = cpu_to_be32(2);
1578
1579         *p++ = cpu_to_be32(attrs[0] & readdir->bitmask[0]);
1580         *p = cpu_to_be32(attrs[1] & readdir->bitmask[1]);
1581         hdr->nops++;
1582         hdr->replen += decode_readdir_maxsz;
1583         dprintk("%s: cookie = %Lu, verifier = %08x:%08x, bitmap = %08x:%08x\n",
1584                         __func__,
1585                         (unsigned long long)readdir->cookie,
1586                         ((u32 *)readdir->verifier.data)[0],
1587                         ((u32 *)readdir->verifier.data)[1],
1588                         attrs[0] & readdir->bitmask[0],
1589                         attrs[1] & readdir->bitmask[1]);
1590 }
1591
1592 static void encode_readlink(struct xdr_stream *xdr, const struct nfs4_readlink *readlink, struct rpc_rqst *req, struct compound_hdr *hdr)
1593 {
1594         __be32 *p;
1595
1596         p = reserve_space(xdr, 4);
1597         *p = cpu_to_be32(OP_READLINK);
1598         hdr->nops++;
1599         hdr->replen += decode_readlink_maxsz;
1600 }
1601
1602 static void encode_remove(struct xdr_stream *xdr, const struct qstr *name, struct compound_hdr *hdr)
1603 {
1604         __be32 *p;
1605
1606         p = reserve_space(xdr, 8 + name->len);
1607         *p++ = cpu_to_be32(OP_REMOVE);
1608         xdr_encode_opaque(p, name->name, name->len);
1609         hdr->nops++;
1610         hdr->replen += decode_remove_maxsz;
1611 }
1612
1613 static void encode_rename(struct xdr_stream *xdr, const struct qstr *oldname, const struct qstr *newname, struct compound_hdr *hdr)
1614 {
1615         __be32 *p;
1616
1617         p = reserve_space(xdr, 4);
1618         *p = cpu_to_be32(OP_RENAME);
1619         encode_string(xdr, oldname->len, oldname->name);
1620         encode_string(xdr, newname->len, newname->name);
1621         hdr->nops++;
1622         hdr->replen += decode_rename_maxsz;
1623 }
1624
1625 static void encode_renew(struct xdr_stream *xdr, const struct nfs_client *client_stateid, struct compound_hdr *hdr)
1626 {
1627         __be32 *p;
1628
1629         p = reserve_space(xdr, 12);
1630         *p++ = cpu_to_be32(OP_RENEW);
1631         xdr_encode_hyper(p, client_stateid->cl_clientid);
1632         hdr->nops++;
1633         hdr->replen += decode_renew_maxsz;
1634 }
1635
1636 static void
1637 encode_restorefh(struct xdr_stream *xdr, struct compound_hdr *hdr)
1638 {
1639         __be32 *p;
1640
1641         p = reserve_space(xdr, 4);
1642         *p = cpu_to_be32(OP_RESTOREFH);
1643         hdr->nops++;
1644         hdr->replen += decode_restorefh_maxsz;
1645 }
1646
1647 static void
1648 encode_setacl(struct xdr_stream *xdr, struct nfs_setaclargs *arg, struct compound_hdr *hdr)
1649 {
1650         __be32 *p;
1651
1652         p = reserve_space(xdr, 4+NFS4_STATEID_SIZE);
1653         *p++ = cpu_to_be32(OP_SETATTR);
1654         xdr_encode_opaque_fixed(p, zero_stateid.data, NFS4_STATEID_SIZE);
1655         p = reserve_space(xdr, 2*4);
1656         *p++ = cpu_to_be32(1);
1657         *p = cpu_to_be32(FATTR4_WORD0_ACL);
1658         BUG_ON(arg->acl_len % 4);
1659         p = reserve_space(xdr, 4);
1660         *p = cpu_to_be32(arg->acl_len);
1661         xdr_write_pages(xdr, arg->acl_pages, arg->acl_pgbase, arg->acl_len);
1662         hdr->nops++;
1663         hdr->replen += decode_setacl_maxsz;
1664 }
1665
1666 static void
1667 encode_savefh(struct xdr_stream *xdr, struct compound_hdr *hdr)
1668 {
1669         __be32 *p;
1670
1671         p = reserve_space(xdr, 4);
1672         *p = cpu_to_be32(OP_SAVEFH);
1673         hdr->nops++;
1674         hdr->replen += decode_savefh_maxsz;
1675 }
1676
1677 static void encode_setattr(struct xdr_stream *xdr, const struct nfs_setattrargs *arg, const struct nfs_server *server, struct compound_hdr *hdr)
1678 {
1679         __be32 *p;
1680
1681         p = reserve_space(xdr, 4+NFS4_STATEID_SIZE);
1682         *p++ = cpu_to_be32(OP_SETATTR);
1683         xdr_encode_opaque_fixed(p, arg->stateid.data, NFS4_STATEID_SIZE);
1684         hdr->nops++;
1685         hdr->replen += decode_setattr_maxsz;
1686         encode_attrs(xdr, arg->iap, server);
1687 }
1688
1689 static void encode_setclientid(struct xdr_stream *xdr, const struct nfs4_setclientid *setclientid, struct compound_hdr *hdr)
1690 {
1691         __be32 *p;
1692
1693         p = reserve_space(xdr, 4 + NFS4_VERIFIER_SIZE);
1694         *p++ = cpu_to_be32(OP_SETCLIENTID);
1695         xdr_encode_opaque_fixed(p, setclientid->sc_verifier->data, NFS4_VERIFIER_SIZE);
1696
1697         encode_string(xdr, setclientid->sc_name_len, setclientid->sc_name);
1698         p = reserve_space(xdr, 4);
1699         *p = cpu_to_be32(setclientid->sc_prog);
1700         encode_string(xdr, setclientid->sc_netid_len, setclientid->sc_netid);
1701         encode_string(xdr, setclientid->sc_uaddr_len, setclientid->sc_uaddr);
1702         p = reserve_space(xdr, 4);
1703         *p = cpu_to_be32(setclientid->sc_cb_ident);
1704         hdr->nops++;
1705         hdr->replen += decode_setclientid_maxsz;
1706 }
1707
1708 static void encode_setclientid_confirm(struct xdr_stream *xdr, const struct nfs4_setclientid_res *arg, struct compound_hdr *hdr)
1709 {
1710         __be32 *p;
1711
1712         p = reserve_space(xdr, 12 + NFS4_VERIFIER_SIZE);
1713         *p++ = cpu_to_be32(OP_SETCLIENTID_CONFIRM);
1714         p = xdr_encode_hyper(p, arg->clientid);
1715         xdr_encode_opaque_fixed(p, arg->confirm.data, NFS4_VERIFIER_SIZE);
1716         hdr->nops++;
1717         hdr->replen += decode_setclientid_confirm_maxsz;
1718 }
1719
1720 static void encode_write(struct xdr_stream *xdr, const struct nfs_writeargs *args, struct compound_hdr *hdr)
1721 {
1722         __be32 *p;
1723
1724         p = reserve_space(xdr, 4);
1725         *p = cpu_to_be32(OP_WRITE);
1726
1727         encode_stateid(xdr, args->context, args->lock_context,
1728                        hdr->minorversion);
1729
1730         p = reserve_space(xdr, 16);
1731         p = xdr_encode_hyper(p, args->offset);
1732         *p++ = cpu_to_be32(args->stable);
1733         *p = cpu_to_be32(args->count);
1734
1735         xdr_write_pages(xdr, args->pages, args->pgbase, args->count);
1736         hdr->nops++;
1737         hdr->replen += decode_write_maxsz;
1738 }
1739
1740 static void encode_delegreturn(struct xdr_stream *xdr, const nfs4_stateid *stateid, struct compound_hdr *hdr)
1741 {
1742         __be32 *p;
1743
1744         p = reserve_space(xdr, 4+NFS4_STATEID_SIZE);
1745
1746         *p++ = cpu_to_be32(OP_DELEGRETURN);
1747         xdr_encode_opaque_fixed(p, stateid->data, NFS4_STATEID_SIZE);
1748         hdr->nops++;
1749         hdr->replen += decode_delegreturn_maxsz;
1750 }
1751
1752 static void encode_secinfo(struct xdr_stream *xdr, const struct qstr *name, struct compound_hdr *hdr)
1753 {
1754         int len = name->len;
1755         __be32 *p;
1756
1757         p = reserve_space(xdr, 8 + len);
1758         *p++ = cpu_to_be32(OP_SECINFO);
1759         xdr_encode_opaque(p, name->name, len);
1760         hdr->nops++;
1761         hdr->replen += decode_secinfo_maxsz;
1762 }
1763
1764 #if defined(CONFIG_NFS_V4_1)
1765 /* NFSv4.1 operations */
1766 static void encode_exchange_id(struct xdr_stream *xdr,
1767                                struct nfs41_exchange_id_args *args,
1768                                struct compound_hdr *hdr)
1769 {
1770         __be32 *p;
1771
1772         p = reserve_space(xdr, 4 + sizeof(args->verifier->data));
1773         *p++ = cpu_to_be32(OP_EXCHANGE_ID);
1774         xdr_encode_opaque_fixed(p, args->verifier->data, sizeof(args->verifier->data));
1775
1776         encode_string(xdr, args->id_len, args->id);
1777
1778         p = reserve_space(xdr, 12);
1779         *p++ = cpu_to_be32(args->flags);
1780         *p++ = cpu_to_be32(0);  /* zero length state_protect4_a */
1781         *p = cpu_to_be32(0);    /* zero length implementation id array */
1782         hdr->nops++;
1783         hdr->replen += decode_exchange_id_maxsz;
1784 }
1785
1786 static void encode_create_session(struct xdr_stream *xdr,
1787                                   struct nfs41_create_session_args *args,
1788                                   struct compound_hdr *hdr)
1789 {
1790         __be32 *p;
1791         char machine_name[NFS4_MAX_MACHINE_NAME_LEN];
1792         uint32_t len;
1793         struct nfs_client *clp = args->client;
1794         u32 max_resp_sz_cached;
1795
1796         /*
1797          * Assumes OPEN is the biggest non-idempotent compound.
1798          * 2 is the verifier.
1799          */
1800         max_resp_sz_cached = (NFS4_dec_open_sz + RPC_REPHDRSIZE +
1801                               RPC_MAX_AUTH_SIZE + 2) * XDR_UNIT;
1802
1803         len = scnprintf(machine_name, sizeof(machine_name), "%s",
1804                         clp->cl_ipaddr);
1805
1806         p = reserve_space(xdr, 20 + 2*28 + 20 + len + 12);
1807         *p++ = cpu_to_be32(OP_CREATE_SESSION);
1808         p = xdr_encode_hyper(p, clp->cl_clientid);
1809         *p++ = cpu_to_be32(clp->cl_seqid);                      /*Sequence id */
1810         *p++ = cpu_to_be32(args->flags);                        /*flags */
1811
1812         /* Fore Channel */
1813         *p++ = cpu_to_be32(0);                          /* header padding size */
1814         *p++ = cpu_to_be32(args->fc_attrs.max_rqst_sz); /* max req size */
1815         *p++ = cpu_to_be32(args->fc_attrs.max_resp_sz); /* max resp size */
1816         *p++ = cpu_to_be32(max_resp_sz_cached);         /* Max resp sz cached */
1817         *p++ = cpu_to_be32(args->fc_attrs.max_ops);     /* max operations */
1818         *p++ = cpu_to_be32(args->fc_attrs.max_reqs);    /* max requests */
1819         *p++ = cpu_to_be32(0);                          /* rdmachannel_attrs */
1820
1821         /* Back Channel */
1822         *p++ = cpu_to_be32(0);                          /* header padding size */
1823         *p++ = cpu_to_be32(args->bc_attrs.max_rqst_sz); /* max req size */
1824         *p++ = cpu_to_be32(args->bc_attrs.max_resp_sz); /* max resp size */
1825         *p++ = cpu_to_be32(args->bc_attrs.max_resp_sz_cached);  /* Max resp sz cached */
1826         *p++ = cpu_to_be32(args->bc_attrs.max_ops);     /* max operations */
1827         *p++ = cpu_to_be32(args->bc_attrs.max_reqs);    /* max requests */
1828         *p++ = cpu_to_be32(0);                          /* rdmachannel_attrs */
1829
1830         *p++ = cpu_to_be32(args->cb_program);           /* cb_program */
1831         *p++ = cpu_to_be32(1);
1832         *p++ = cpu_to_be32(RPC_AUTH_UNIX);                      /* auth_sys */
1833
1834         /* authsys_parms rfc1831 */
1835         *p++ = cpu_to_be32((u32)clp->cl_boot_time.tv_nsec);     /* stamp */
1836         p = xdr_encode_opaque(p, machine_name, len);
1837         *p++ = cpu_to_be32(0);                          /* UID */
1838         *p++ = cpu_to_be32(0);                          /* GID */
1839         *p = cpu_to_be32(0);                            /* No more gids */
1840         hdr->nops++;
1841         hdr->replen += decode_create_session_maxsz;
1842 }
1843
1844 static void encode_destroy_session(struct xdr_stream *xdr,
1845                                    struct nfs4_session *session,
1846                                    struct compound_hdr *hdr)
1847 {
1848         __be32 *p;
1849         p = reserve_space(xdr, 4 + NFS4_MAX_SESSIONID_LEN);
1850         *p++ = cpu_to_be32(OP_DESTROY_SESSION);
1851         xdr_encode_opaque_fixed(p, session->sess_id.data, NFS4_MAX_SESSIONID_LEN);
1852         hdr->nops++;
1853         hdr->replen += decode_destroy_session_maxsz;
1854 }
1855
1856 static void encode_reclaim_complete(struct xdr_stream *xdr,
1857                                     struct nfs41_reclaim_complete_args *args,
1858                                     struct compound_hdr *hdr)
1859 {
1860         __be32 *p;
1861
1862         p = reserve_space(xdr, 8);
1863         *p++ = cpu_to_be32(OP_RECLAIM_COMPLETE);
1864         *p++ = cpu_to_be32(args->one_fs);
1865         hdr->nops++;
1866         hdr->replen += decode_reclaim_complete_maxsz;
1867 }
1868 #endif /* CONFIG_NFS_V4_1 */
1869
1870 static void encode_sequence(struct xdr_stream *xdr,
1871                             const struct nfs4_sequence_args *args,
1872                             struct compound_hdr *hdr)
1873 {
1874 #if defined(CONFIG_NFS_V4_1)
1875         struct nfs4_session *session = args->sa_session;
1876         struct nfs4_slot_table *tp;
1877         struct nfs4_slot *slot;
1878         __be32 *p;
1879
1880         if (!session)
1881                 return;
1882
1883         tp = &session->fc_slot_table;
1884
1885         WARN_ON(args->sa_slotid == NFS4_MAX_SLOT_TABLE);
1886         slot = tp->slots + args->sa_slotid;
1887
1888         p = reserve_space(xdr, 4 + NFS4_MAX_SESSIONID_LEN + 16);
1889         *p++ = cpu_to_be32(OP_SEQUENCE);
1890
1891         /*
1892          * Sessionid + seqid + slotid + max slotid + cache_this
1893          */
1894         dprintk("%s: sessionid=%u:%u:%u:%u seqid=%d slotid=%d "
1895                 "max_slotid=%d cache_this=%d\n",
1896                 __func__,
1897                 ((u32 *)session->sess_id.data)[0],
1898                 ((u32 *)session->sess_id.data)[1],
1899                 ((u32 *)session->sess_id.data)[2],
1900                 ((u32 *)session->sess_id.data)[3],
1901                 slot->seq_nr, args->sa_slotid,
1902                 tp->highest_used_slotid, args->sa_cache_this);
1903         p = xdr_encode_opaque_fixed(p, session->sess_id.data, NFS4_MAX_SESSIONID_LEN);
1904         *p++ = cpu_to_be32(slot->seq_nr);
1905         *p++ = cpu_to_be32(args->sa_slotid);
1906         *p++ = cpu_to_be32(tp->highest_used_slotid);
1907         *p = cpu_to_be32(args->sa_cache_this);
1908         hdr->nops++;
1909         hdr->replen += decode_sequence_maxsz;
1910 #endif /* CONFIG_NFS_V4_1 */
1911 }
1912
1913 #ifdef CONFIG_NFS_V4_1
1914 static void
1915 encode_getdevicelist(struct xdr_stream *xdr,
1916                      const struct nfs4_getdevicelist_args *args,
1917                      struct compound_hdr *hdr)
1918 {
1919         __be32 *p;
1920         nfs4_verifier dummy = {
1921                 .data = "dummmmmy",
1922         };
1923
1924         p = reserve_space(xdr, 20);
1925         *p++ = cpu_to_be32(OP_GETDEVICELIST);
1926         *p++ = cpu_to_be32(args->layoutclass);
1927         *p++ = cpu_to_be32(NFS4_PNFS_GETDEVLIST_MAXNUM);
1928         xdr_encode_hyper(p, 0ULL);                          /* cookie */
1929         encode_nfs4_verifier(xdr, &dummy);
1930         hdr->nops++;
1931         hdr->replen += decode_getdevicelist_maxsz;
1932 }
1933
1934 static void
1935 encode_getdeviceinfo(struct xdr_stream *xdr,
1936                      const struct nfs4_getdeviceinfo_args *args,
1937                      struct compound_hdr *hdr)
1938 {
1939         __be32 *p;
1940
1941         p = reserve_space(xdr, 16 + NFS4_DEVICEID4_SIZE);
1942         *p++ = cpu_to_be32(OP_GETDEVICEINFO);
1943         p = xdr_encode_opaque_fixed(p, args->pdev->dev_id.data,
1944                                     NFS4_DEVICEID4_SIZE);
1945         *p++ = cpu_to_be32(args->pdev->layout_type);
1946         *p++ = cpu_to_be32(args->pdev->pglen);          /* gdia_maxcount */
1947         *p++ = cpu_to_be32(0);                          /* bitmap length 0 */
1948         hdr->nops++;
1949         hdr->replen += decode_getdeviceinfo_maxsz;
1950 }
1951
1952 static void
1953 encode_layoutget(struct xdr_stream *xdr,
1954                       const struct nfs4_layoutget_args *args,
1955                       struct compound_hdr *hdr)
1956 {
1957         __be32 *p;
1958
1959         p = reserve_space(xdr, 44 + NFS4_STATEID_SIZE);
1960         *p++ = cpu_to_be32(OP_LAYOUTGET);
1961         *p++ = cpu_to_be32(0);     /* Signal layout available */
1962         *p++ = cpu_to_be32(args->type);
1963         *p++ = cpu_to_be32(args->range.iomode);
1964         p = xdr_encode_hyper(p, args->range.offset);
1965         p = xdr_encode_hyper(p, args->range.length);
1966         p = xdr_encode_hyper(p, args->minlength);
1967         p = xdr_encode_opaque_fixed(p, &args->stateid.data, NFS4_STATEID_SIZE);
1968         *p = cpu_to_be32(args->maxcount);
1969
1970         dprintk("%s: 1st type:0x%x iomode:%d off:%lu len:%lu mc:%d\n",
1971                 __func__,
1972                 args->type,
1973                 args->range.iomode,
1974                 (unsigned long)args->range.offset,
1975                 (unsigned long)args->range.length,
1976                 args->maxcount);
1977         hdr->nops++;
1978         hdr->replen += decode_layoutget_maxsz;
1979 }
1980
1981 static int
1982 encode_layoutcommit(struct xdr_stream *xdr,
1983                     struct inode *inode,
1984                     const struct nfs4_layoutcommit_args *args,
1985                     struct compound_hdr *hdr)
1986 {
1987         __be32 *p;
1988
1989         dprintk("%s: lbw: %llu type: %d\n", __func__, args->lastbytewritten,
1990                 NFS_SERVER(args->inode)->pnfs_curr_ld->id);
1991
1992         p = reserve_space(xdr, 44 + NFS4_STATEID_SIZE);
1993         *p++ = cpu_to_be32(OP_LAYOUTCOMMIT);
1994         /* Only whole file layouts */
1995         p = xdr_encode_hyper(p, 0); /* offset */
1996         p = xdr_encode_hyper(p, args->lastbytewritten + 1);     /* length */
1997         *p++ = cpu_to_be32(0); /* reclaim */
1998         p = xdr_encode_opaque_fixed(p, args->stateid.data, NFS4_STATEID_SIZE);
1999         *p++ = cpu_to_be32(1); /* newoffset = TRUE */
2000         p = xdr_encode_hyper(p, args->lastbytewritten);
2001         *p++ = cpu_to_be32(0); /* Never send time_modify_changed */
2002         *p++ = cpu_to_be32(NFS_SERVER(args->inode)->pnfs_curr_ld->id);/* type */
2003
2004         if (NFS_SERVER(inode)->pnfs_curr_ld->encode_layoutcommit)
2005                 NFS_SERVER(inode)->pnfs_curr_ld->encode_layoutcommit(
2006                         NFS_I(inode)->layout, xdr, args);
2007         else {
2008                 p = reserve_space(xdr, 4);
2009                 *p = cpu_to_be32(0); /* no layout-type payload */
2010         }
2011
2012         hdr->nops++;
2013         hdr->replen += decode_layoutcommit_maxsz;
2014         return 0;
2015 }
2016
2017 static void
2018 encode_layoutreturn(struct xdr_stream *xdr,
2019                     const struct nfs4_layoutreturn_args *args,
2020                     struct compound_hdr *hdr)
2021 {
2022         __be32 *p;
2023
2024         p = reserve_space(xdr, 20);
2025         *p++ = cpu_to_be32(OP_LAYOUTRETURN);
2026         *p++ = cpu_to_be32(0);          /* reclaim. always 0 for now */
2027         *p++ = cpu_to_be32(args->layout_type);
2028         *p++ = cpu_to_be32(IOMODE_ANY);
2029         *p = cpu_to_be32(RETURN_FILE);
2030         p = reserve_space(xdr, 16 + NFS4_STATEID_SIZE);
2031         p = xdr_encode_hyper(p, 0);
2032         p = xdr_encode_hyper(p, NFS4_MAX_UINT64);
2033         spin_lock(&args->inode->i_lock);
2034         xdr_encode_opaque_fixed(p, &args->stateid.data, NFS4_STATEID_SIZE);
2035         spin_unlock(&args->inode->i_lock);
2036         if (NFS_SERVER(args->inode)->pnfs_curr_ld->encode_layoutreturn) {
2037                 NFS_SERVER(args->inode)->pnfs_curr_ld->encode_layoutreturn(
2038                         NFS_I(args->inode)->layout, xdr, args);
2039         } else {
2040                 p = reserve_space(xdr, 4);
2041                 *p = cpu_to_be32(0);
2042         }
2043         hdr->nops++;
2044         hdr->replen += decode_layoutreturn_maxsz;
2045 }
2046
2047 static int
2048 encode_secinfo_no_name(struct xdr_stream *xdr,
2049                        const struct nfs41_secinfo_no_name_args *args,
2050                        struct compound_hdr *hdr)
2051 {
2052         __be32 *p;
2053         p = reserve_space(xdr, 8);
2054         *p++ = cpu_to_be32(OP_SECINFO_NO_NAME);
2055         *p++ = cpu_to_be32(args->style);
2056         hdr->nops++;
2057         hdr->replen += decode_secinfo_no_name_maxsz;
2058         return 0;
2059 }
2060
2061 static void encode_test_stateid(struct xdr_stream *xdr,
2062                                 struct nfs41_test_stateid_args *args,
2063                                 struct compound_hdr *hdr)
2064 {
2065         __be32 *p;
2066
2067         p = reserve_space(xdr, 8 + NFS4_STATEID_SIZE);
2068         *p++ = cpu_to_be32(OP_TEST_STATEID);
2069         *p++ = cpu_to_be32(1);
2070         xdr_encode_opaque_fixed(p, args->stateid->data, NFS4_STATEID_SIZE);
2071         hdr->nops++;
2072         hdr->replen += decode_test_stateid_maxsz;
2073 }
2074
2075 static void encode_free_stateid(struct xdr_stream *xdr,
2076                                 struct nfs41_free_stateid_args *args,
2077                                 struct compound_hdr *hdr)
2078 {
2079         __be32 *p;
2080         p = reserve_space(xdr, 4 + NFS4_STATEID_SIZE);
2081         *p++ = cpu_to_be32(OP_FREE_STATEID);
2082         xdr_encode_opaque_fixed(p, args->stateid->data, NFS4_STATEID_SIZE);
2083         hdr->nops++;
2084         hdr->replen += decode_free_stateid_maxsz;
2085 }
2086 #endif /* CONFIG_NFS_V4_1 */
2087
2088 /*
2089  * END OF "GENERIC" ENCODE ROUTINES.
2090  */
2091
2092 static u32 nfs4_xdr_minorversion(const struct nfs4_sequence_args *args)
2093 {
2094 #if defined(CONFIG_NFS_V4_1)
2095         if (args->sa_session)
2096                 return args->sa_session->clp->cl_mvops->minor_version;
2097 #endif /* CONFIG_NFS_V4_1 */
2098         return 0;
2099 }
2100
2101 /*
2102  * Encode an ACCESS request
2103  */
2104 static void nfs4_xdr_enc_access(struct rpc_rqst *req, struct xdr_stream *xdr,
2105                                 const struct nfs4_accessargs *args)
2106 {
2107         struct compound_hdr hdr = {
2108                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2109         };
2110
2111         encode_compound_hdr(xdr, req, &hdr);
2112         encode_sequence(xdr, &args->seq_args, &hdr);
2113         encode_putfh(xdr, args->fh, &hdr);
2114         encode_access(xdr, args->access, &hdr);
2115         encode_getfattr(xdr, args->bitmask, &hdr);
2116         encode_nops(&hdr);
2117 }
2118
2119 /*
2120  * Encode LOOKUP request
2121  */
2122 static void nfs4_xdr_enc_lookup(struct rpc_rqst *req, struct xdr_stream *xdr,
2123                                 const struct nfs4_lookup_arg *args)
2124 {
2125         struct compound_hdr hdr = {
2126                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2127         };
2128
2129         encode_compound_hdr(xdr, req, &hdr);
2130         encode_sequence(xdr, &args->seq_args, &hdr);
2131         encode_putfh(xdr, args->dir_fh, &hdr);
2132         encode_lookup(xdr, args->name, &hdr);
2133         encode_getfh(xdr, &hdr);
2134         encode_getfattr(xdr, args->bitmask, &hdr);
2135         encode_nops(&hdr);
2136 }
2137
2138 /*
2139  * Encode LOOKUP_ROOT request
2140  */
2141 static void nfs4_xdr_enc_lookup_root(struct rpc_rqst *req,
2142                                      struct xdr_stream *xdr,
2143                                      const struct nfs4_lookup_root_arg *args)
2144 {
2145         struct compound_hdr hdr = {
2146                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2147         };
2148
2149         encode_compound_hdr(xdr, req, &hdr);
2150         encode_sequence(xdr, &args->seq_args, &hdr);
2151         encode_putrootfh(xdr, &hdr);
2152         encode_getfh(xdr, &hdr);
2153         encode_getfattr(xdr, args->bitmask, &hdr);
2154         encode_nops(&hdr);
2155 }
2156
2157 /*
2158  * Encode REMOVE request
2159  */
2160 static void nfs4_xdr_enc_remove(struct rpc_rqst *req, struct xdr_stream *xdr,
2161                                 const struct nfs_removeargs *args)
2162 {
2163         struct compound_hdr hdr = {
2164                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2165         };
2166
2167         encode_compound_hdr(xdr, req, &hdr);
2168         encode_sequence(xdr, &args->seq_args, &hdr);
2169         encode_putfh(xdr, args->fh, &hdr);
2170         encode_remove(xdr, &args->name, &hdr);
2171         encode_getfattr(xdr, args->bitmask, &hdr);
2172         encode_nops(&hdr);
2173 }
2174
2175 /*
2176  * Encode RENAME request
2177  */
2178 static void nfs4_xdr_enc_rename(struct rpc_rqst *req, struct xdr_stream *xdr,
2179                                 const struct nfs_renameargs *args)
2180 {
2181         struct compound_hdr hdr = {
2182                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2183         };
2184
2185         encode_compound_hdr(xdr, req, &hdr);
2186         encode_sequence(xdr, &args->seq_args, &hdr);
2187         encode_putfh(xdr, args->old_dir, &hdr);
2188         encode_savefh(xdr, &hdr);
2189         encode_putfh(xdr, args->new_dir, &hdr);
2190         encode_rename(xdr, args->old_name, args->new_name, &hdr);
2191         encode_getfattr(xdr, args->bitmask, &hdr);
2192         encode_restorefh(xdr, &hdr);
2193         encode_getfattr(xdr, args->bitmask, &hdr);
2194         encode_nops(&hdr);
2195 }
2196
2197 /*
2198  * Encode LINK request
2199  */
2200 static void nfs4_xdr_enc_link(struct rpc_rqst *req, struct xdr_stream *xdr,
2201                              const struct nfs4_link_arg *args)
2202 {
2203         struct compound_hdr hdr = {
2204                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2205         };
2206
2207         encode_compound_hdr(xdr, req, &hdr);
2208         encode_sequence(xdr, &args->seq_args, &hdr);
2209         encode_putfh(xdr, args->fh, &hdr);
2210         encode_savefh(xdr, &hdr);
2211         encode_putfh(xdr, args->dir_fh, &hdr);
2212         encode_link(xdr, args->name, &hdr);
2213         encode_getfattr(xdr, args->bitmask, &hdr);
2214         encode_restorefh(xdr, &hdr);
2215         encode_getfattr(xdr, args->bitmask, &hdr);
2216         encode_nops(&hdr);
2217 }
2218
2219 /*
2220  * Encode CREATE request
2221  */
2222 static void nfs4_xdr_enc_create(struct rpc_rqst *req, struct xdr_stream *xdr,
2223                                 const struct nfs4_create_arg *args)
2224 {
2225         struct compound_hdr hdr = {
2226                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2227         };
2228
2229         encode_compound_hdr(xdr, req, &hdr);
2230         encode_sequence(xdr, &args->seq_args, &hdr);
2231         encode_putfh(xdr, args->dir_fh, &hdr);
2232         encode_savefh(xdr, &hdr);
2233         encode_create(xdr, args, &hdr);
2234         encode_getfh(xdr, &hdr);
2235         encode_getfattr(xdr, args->bitmask, &hdr);
2236         encode_restorefh(xdr, &hdr);
2237         encode_getfattr(xdr, args->bitmask, &hdr);
2238         encode_nops(&hdr);
2239 }
2240
2241 /*
2242  * Encode SYMLINK request
2243  */
2244 static void nfs4_xdr_enc_symlink(struct rpc_rqst *req, struct xdr_stream *xdr,
2245                                  const struct nfs4_create_arg *args)
2246 {
2247         nfs4_xdr_enc_create(req, xdr, args);
2248 }
2249
2250 /*
2251  * Encode GETATTR request
2252  */
2253 static void nfs4_xdr_enc_getattr(struct rpc_rqst *req, struct xdr_stream *xdr,
2254                                  const struct nfs4_getattr_arg *args)
2255 {
2256         struct compound_hdr hdr = {
2257                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2258         };
2259
2260         encode_compound_hdr(xdr, req, &hdr);
2261         encode_sequence(xdr, &args->seq_args, &hdr);
2262         encode_putfh(xdr, args->fh, &hdr);
2263         encode_getfattr(xdr, args->bitmask, &hdr);
2264         encode_nops(&hdr);
2265 }
2266
2267 /*
2268  * Encode a CLOSE request
2269  */
2270 static void nfs4_xdr_enc_close(struct rpc_rqst *req, struct xdr_stream *xdr,
2271                                struct nfs_closeargs *args)
2272 {
2273         struct compound_hdr hdr = {
2274                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2275         };
2276
2277         encode_compound_hdr(xdr, req, &hdr);
2278         encode_sequence(xdr, &args->seq_args, &hdr);
2279         encode_putfh(xdr, args->fh, &hdr);
2280         encode_close(xdr, args, &hdr);
2281         encode_getfattr(xdr, args->bitmask, &hdr);
2282         encode_nops(&hdr);
2283 }
2284
2285 /*
2286  * Encode an OPEN request
2287  */
2288 static void nfs4_xdr_enc_open(struct rpc_rqst *req, struct xdr_stream *xdr,
2289                               struct nfs_openargs *args)
2290 {
2291         struct compound_hdr hdr = {
2292                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2293         };
2294
2295         encode_compound_hdr(xdr, req, &hdr);
2296         encode_sequence(xdr, &args->seq_args, &hdr);
2297         encode_putfh(xdr, args->fh, &hdr);
2298         encode_savefh(xdr, &hdr);
2299         encode_open(xdr, args, &hdr);
2300         encode_getfh(xdr, &hdr);
2301         encode_getfattr(xdr, args->bitmask, &hdr);
2302         encode_restorefh(xdr, &hdr);
2303         encode_getfattr(xdr, args->bitmask, &hdr);
2304         encode_nops(&hdr);
2305 }
2306
2307 /*
2308  * Encode an OPEN_CONFIRM request
2309  */
2310 static void nfs4_xdr_enc_open_confirm(struct rpc_rqst *req,
2311                                       struct xdr_stream *xdr,
2312                                       struct nfs_open_confirmargs *args)
2313 {
2314         struct compound_hdr hdr = {
2315                 .nops   = 0,
2316         };
2317
2318         encode_compound_hdr(xdr, req, &hdr);
2319         encode_putfh(xdr, args->fh, &hdr);
2320         encode_open_confirm(xdr, args, &hdr);
2321         encode_nops(&hdr);
2322 }
2323
2324 /*
2325  * Encode an OPEN request with no attributes.
2326  */
2327 static void nfs4_xdr_enc_open_noattr(struct rpc_rqst *req,
2328                                      struct xdr_stream *xdr,
2329                                      struct nfs_openargs *args)
2330 {
2331         struct compound_hdr hdr = {
2332                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2333         };
2334
2335         encode_compound_hdr(xdr, req, &hdr);
2336         encode_sequence(xdr, &args->seq_args, &hdr);
2337         encode_putfh(xdr, args->fh, &hdr);
2338         encode_open(xdr, args, &hdr);
2339         encode_getfattr(xdr, args->bitmask, &hdr);
2340         encode_nops(&hdr);
2341 }
2342
2343 /*
2344  * Encode an OPEN_DOWNGRADE request
2345  */
2346 static void nfs4_xdr_enc_open_downgrade(struct rpc_rqst *req,
2347                                         struct xdr_stream *xdr,
2348                                         struct nfs_closeargs *args)
2349 {
2350         struct compound_hdr hdr = {
2351                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2352         };
2353
2354         encode_compound_hdr(xdr, req, &hdr);
2355         encode_sequence(xdr, &args->seq_args, &hdr);
2356         encode_putfh(xdr, args->fh, &hdr);
2357         encode_open_downgrade(xdr, args, &hdr);
2358         encode_getfattr(xdr, args->bitmask, &hdr);
2359         encode_nops(&hdr);
2360 }
2361
2362 /*
2363  * Encode a LOCK request
2364  */
2365 static void nfs4_xdr_enc_lock(struct rpc_rqst *req, struct xdr_stream *xdr,
2366                               struct nfs_lock_args *args)
2367 {
2368         struct compound_hdr hdr = {
2369                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2370         };
2371
2372         encode_compound_hdr(xdr, req, &hdr);
2373         encode_sequence(xdr, &args->seq_args, &hdr);
2374         encode_putfh(xdr, args->fh, &hdr);
2375         encode_lock(xdr, args, &hdr);
2376         encode_nops(&hdr);
2377 }
2378
2379 /*
2380  * Encode a LOCKT request
2381  */
2382 static void nfs4_xdr_enc_lockt(struct rpc_rqst *req, struct xdr_stream *xdr,
2383                                struct nfs_lockt_args *args)
2384 {
2385         struct compound_hdr hdr = {
2386                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2387         };
2388
2389         encode_compound_hdr(xdr, req, &hdr);
2390         encode_sequence(xdr, &args->seq_args, &hdr);
2391         encode_putfh(xdr, args->fh, &hdr);
2392         encode_lockt(xdr, args, &hdr);
2393         encode_nops(&hdr);
2394 }
2395
2396 /*
2397  * Encode a LOCKU request
2398  */
2399 static void nfs4_xdr_enc_locku(struct rpc_rqst *req, struct xdr_stream *xdr,
2400                                struct nfs_locku_args *args)
2401 {
2402         struct compound_hdr hdr = {
2403                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2404         };
2405
2406         encode_compound_hdr(xdr, req, &hdr);
2407         encode_sequence(xdr, &args->seq_args, &hdr);
2408         encode_putfh(xdr, args->fh, &hdr);
2409         encode_locku(xdr, args, &hdr);
2410         encode_nops(&hdr);
2411 }
2412
2413 static void nfs4_xdr_enc_release_lockowner(struct rpc_rqst *req,
2414                                            struct xdr_stream *xdr,
2415                                         struct nfs_release_lockowner_args *args)
2416 {
2417         struct compound_hdr hdr = {
2418                 .minorversion = 0,
2419         };
2420
2421         encode_compound_hdr(xdr, req, &hdr);
2422         encode_release_lockowner(xdr, &args->lock_owner, &hdr);
2423         encode_nops(&hdr);
2424 }
2425
2426 /*
2427  * Encode a READLINK request
2428  */
2429 static void nfs4_xdr_enc_readlink(struct rpc_rqst *req, struct xdr_stream *xdr,
2430                                   const struct nfs4_readlink *args)
2431 {
2432         struct compound_hdr hdr = {
2433                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2434         };
2435
2436         encode_compound_hdr(xdr, req, &hdr);
2437         encode_sequence(xdr, &args->seq_args, &hdr);
2438         encode_putfh(xdr, args->fh, &hdr);
2439         encode_readlink(xdr, args, req, &hdr);
2440
2441         xdr_inline_pages(&req->rq_rcv_buf, hdr.replen << 2, args->pages,
2442                         args->pgbase, args->pglen);
2443         encode_nops(&hdr);
2444 }
2445
2446 /*
2447  * Encode a READDIR request
2448  */
2449 static void nfs4_xdr_enc_readdir(struct rpc_rqst *req, struct xdr_stream *xdr,
2450                                  const struct nfs4_readdir_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_readdir(xdr, args, req, &hdr);
2460
2461         xdr_inline_pages(&req->rq_rcv_buf, hdr.replen << 2, args->pages,
2462                          args->pgbase, args->count);
2463         dprintk("%s: inlined page args = (%u, %p, %u, %u)\n",
2464                         __func__, hdr.replen << 2, args->pages,
2465                         args->pgbase, args->count);
2466         encode_nops(&hdr);
2467 }
2468
2469 /*
2470  * Encode a READ request
2471  */
2472 static void nfs4_xdr_enc_read(struct rpc_rqst *req, struct xdr_stream *xdr,
2473                               struct nfs_readargs *args)
2474 {
2475         struct compound_hdr hdr = {
2476                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2477         };
2478
2479         encode_compound_hdr(xdr, req, &hdr);
2480         encode_sequence(xdr, &args->seq_args, &hdr);
2481         encode_putfh(xdr, args->fh, &hdr);
2482         encode_read(xdr, args, &hdr);
2483
2484         xdr_inline_pages(&req->rq_rcv_buf, hdr.replen << 2,
2485                          args->pages, args->pgbase, args->count);
2486         req->rq_rcv_buf.flags |= XDRBUF_READ;
2487         encode_nops(&hdr);
2488 }
2489
2490 /*
2491  * Encode an SETATTR request
2492  */
2493 static void nfs4_xdr_enc_setattr(struct rpc_rqst *req, struct xdr_stream *xdr,
2494                                  struct nfs_setattrargs *args)
2495 {
2496         struct compound_hdr hdr = {
2497                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2498         };
2499
2500         encode_compound_hdr(xdr, req, &hdr);
2501         encode_sequence(xdr, &args->seq_args, &hdr);
2502         encode_putfh(xdr, args->fh, &hdr);
2503         encode_setattr(xdr, args, args->server, &hdr);
2504         encode_getfattr(xdr, args->bitmask, &hdr);
2505         encode_nops(&hdr);
2506 }
2507
2508 /*
2509  * Encode a GETACL request
2510  */
2511 static void nfs4_xdr_enc_getacl(struct rpc_rqst *req, struct xdr_stream *xdr,
2512                                 struct nfs_getaclargs *args)
2513 {
2514         struct compound_hdr hdr = {
2515                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2516         };
2517         uint32_t replen;
2518
2519         encode_compound_hdr(xdr, req, &hdr);
2520         encode_sequence(xdr, &args->seq_args, &hdr);
2521         encode_putfh(xdr, args->fh, &hdr);
2522         replen = hdr.replen + op_decode_hdr_maxsz + 1;
2523         encode_getattr_two(xdr, FATTR4_WORD0_ACL, 0, &hdr);
2524
2525         xdr_inline_pages(&req->rq_rcv_buf, replen << 2,
2526                 args->acl_pages, args->acl_pgbase, args->acl_len);
2527
2528         encode_nops(&hdr);
2529 }
2530
2531 /*
2532  * Encode a WRITE request
2533  */
2534 static void nfs4_xdr_enc_write(struct rpc_rqst *req, struct xdr_stream *xdr,
2535                                struct nfs_writeargs *args)
2536 {
2537         struct compound_hdr hdr = {
2538                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2539         };
2540
2541         encode_compound_hdr(xdr, req, &hdr);
2542         encode_sequence(xdr, &args->seq_args, &hdr);
2543         encode_putfh(xdr, args->fh, &hdr);
2544         encode_write(xdr, args, &hdr);
2545         req->rq_snd_buf.flags |= XDRBUF_WRITE;
2546         if (args->bitmask)
2547                 encode_getfattr(xdr, args->bitmask, &hdr);
2548         encode_nops(&hdr);
2549 }
2550
2551 /*
2552  *  a COMMIT request
2553  */
2554 static void nfs4_xdr_enc_commit(struct rpc_rqst *req, struct xdr_stream *xdr,
2555                                 struct nfs_writeargs *args)
2556 {
2557         struct compound_hdr hdr = {
2558                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2559         };
2560
2561         encode_compound_hdr(xdr, req, &hdr);
2562         encode_sequence(xdr, &args->seq_args, &hdr);
2563         encode_putfh(xdr, args->fh, &hdr);
2564         encode_commit(xdr, args, &hdr);
2565         if (args->bitmask)
2566                 encode_getfattr(xdr, args->bitmask, &hdr);
2567         encode_nops(&hdr);
2568 }
2569
2570 /*
2571  * FSINFO request
2572  */
2573 static void nfs4_xdr_enc_fsinfo(struct rpc_rqst *req, struct xdr_stream *xdr,
2574                                 struct nfs4_fsinfo_arg *args)
2575 {
2576         struct compound_hdr hdr = {
2577                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2578         };
2579
2580         encode_compound_hdr(xdr, req, &hdr);
2581         encode_sequence(xdr, &args->seq_args, &hdr);
2582         encode_putfh(xdr, args->fh, &hdr);
2583         encode_fsinfo(xdr, args->bitmask, &hdr);
2584         encode_nops(&hdr);
2585 }
2586
2587 /*
2588  * a PATHCONF request
2589  */
2590 static void nfs4_xdr_enc_pathconf(struct rpc_rqst *req, struct xdr_stream *xdr,
2591                                   const struct nfs4_pathconf_arg *args)
2592 {
2593         struct compound_hdr hdr = {
2594                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2595         };
2596
2597         encode_compound_hdr(xdr, req, &hdr);
2598         encode_sequence(xdr, &args->seq_args, &hdr);
2599         encode_putfh(xdr, args->fh, &hdr);
2600         encode_getattr_one(xdr, args->bitmask[0] & nfs4_pathconf_bitmap[0],
2601                            &hdr);
2602         encode_nops(&hdr);
2603 }
2604
2605 /*
2606  * a STATFS request
2607  */
2608 static void nfs4_xdr_enc_statfs(struct rpc_rqst *req, struct xdr_stream *xdr,
2609                                 const struct nfs4_statfs_arg *args)
2610 {
2611         struct compound_hdr hdr = {
2612                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2613         };
2614
2615         encode_compound_hdr(xdr, req, &hdr);
2616         encode_sequence(xdr, &args->seq_args, &hdr);
2617         encode_putfh(xdr, args->fh, &hdr);
2618         encode_getattr_two(xdr, args->bitmask[0] & nfs4_statfs_bitmap[0],
2619                            args->bitmask[1] & nfs4_statfs_bitmap[1], &hdr);
2620         encode_nops(&hdr);
2621 }
2622
2623 /*
2624  * GETATTR_BITMAP request
2625  */
2626 static void nfs4_xdr_enc_server_caps(struct rpc_rqst *req,
2627                                      struct xdr_stream *xdr,
2628                                      struct nfs4_server_caps_arg *args)
2629 {
2630         struct compound_hdr hdr = {
2631                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2632         };
2633
2634         encode_compound_hdr(xdr, req, &hdr);
2635         encode_sequence(xdr, &args->seq_args, &hdr);
2636         encode_putfh(xdr, args->fhandle, &hdr);
2637         encode_getattr_one(xdr, FATTR4_WORD0_SUPPORTED_ATTRS|
2638                            FATTR4_WORD0_LINK_SUPPORT|
2639                            FATTR4_WORD0_SYMLINK_SUPPORT|
2640                            FATTR4_WORD0_ACLSUPPORT, &hdr);
2641         encode_nops(&hdr);
2642 }
2643
2644 /*
2645  * a RENEW request
2646  */
2647 static void nfs4_xdr_enc_renew(struct rpc_rqst *req, struct xdr_stream *xdr,
2648                                struct nfs_client *clp)
2649 {
2650         struct compound_hdr hdr = {
2651                 .nops   = 0,
2652         };
2653
2654         encode_compound_hdr(xdr, req, &hdr);
2655         encode_renew(xdr, clp, &hdr);
2656         encode_nops(&hdr);
2657 }
2658
2659 /*
2660  * a SETCLIENTID request
2661  */
2662 static void nfs4_xdr_enc_setclientid(struct rpc_rqst *req,
2663                                      struct xdr_stream *xdr,
2664                                      struct nfs4_setclientid *sc)
2665 {
2666         struct compound_hdr hdr = {
2667                 .nops   = 0,
2668         };
2669
2670         encode_compound_hdr(xdr, req, &hdr);
2671         encode_setclientid(xdr, sc, &hdr);
2672         encode_nops(&hdr);
2673 }
2674
2675 /*
2676  * a SETCLIENTID_CONFIRM request
2677  */
2678 static void nfs4_xdr_enc_setclientid_confirm(struct rpc_rqst *req,
2679                                              struct xdr_stream *xdr,
2680                                              struct nfs4_setclientid_res *arg)
2681 {
2682         struct compound_hdr hdr = {
2683                 .nops   = 0,
2684         };
2685         const u32 lease_bitmap[3] = { FATTR4_WORD0_LEASE_TIME };
2686
2687         encode_compound_hdr(xdr, req, &hdr);
2688         encode_setclientid_confirm(xdr, arg, &hdr);
2689         encode_putrootfh(xdr, &hdr);
2690         encode_fsinfo(xdr, lease_bitmap, &hdr);
2691         encode_nops(&hdr);
2692 }
2693
2694 /*
2695  * DELEGRETURN request
2696  */
2697 static void nfs4_xdr_enc_delegreturn(struct rpc_rqst *req,
2698                                      struct xdr_stream *xdr,
2699                                      const struct nfs4_delegreturnargs *args)
2700 {
2701         struct compound_hdr hdr = {
2702                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2703         };
2704
2705         encode_compound_hdr(xdr, req, &hdr);
2706         encode_sequence(xdr, &args->seq_args, &hdr);
2707         encode_putfh(xdr, args->fhandle, &hdr);
2708         encode_delegreturn(xdr, args->stateid, &hdr);
2709         encode_getfattr(xdr, args->bitmask, &hdr);
2710         encode_nops(&hdr);
2711 }
2712
2713 /*
2714  * Encode FS_LOCATIONS request
2715  */
2716 static void nfs4_xdr_enc_fs_locations(struct rpc_rqst *req,
2717                                       struct xdr_stream *xdr,
2718                                       struct nfs4_fs_locations_arg *args)
2719 {
2720         struct compound_hdr hdr = {
2721                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2722         };
2723         uint32_t replen;
2724
2725         encode_compound_hdr(xdr, req, &hdr);
2726         encode_sequence(xdr, &args->seq_args, &hdr);
2727         encode_putfh(xdr, args->dir_fh, &hdr);
2728         encode_lookup(xdr, args->name, &hdr);
2729         replen = hdr.replen;    /* get the attribute into args->page */
2730         encode_fs_locations(xdr, args->bitmask, &hdr);
2731
2732         xdr_inline_pages(&req->rq_rcv_buf, replen << 2, &args->page,
2733                         0, PAGE_SIZE);
2734         encode_nops(&hdr);
2735 }
2736
2737 /*
2738  * Encode SECINFO request
2739  */
2740 static void nfs4_xdr_enc_secinfo(struct rpc_rqst *req,
2741                                 struct xdr_stream *xdr,
2742                                 struct nfs4_secinfo_arg *args)
2743 {
2744         struct compound_hdr hdr = {
2745                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2746         };
2747
2748         encode_compound_hdr(xdr, req, &hdr);
2749         encode_sequence(xdr, &args->seq_args, &hdr);
2750         encode_putfh(xdr, args->dir_fh, &hdr);
2751         encode_secinfo(xdr, args->name, &hdr);
2752         encode_nops(&hdr);
2753 }
2754
2755 #if defined(CONFIG_NFS_V4_1)
2756 /*
2757  * EXCHANGE_ID request
2758  */
2759 static void nfs4_xdr_enc_exchange_id(struct rpc_rqst *req,
2760                                      struct xdr_stream *xdr,
2761                                      struct nfs41_exchange_id_args *args)
2762 {
2763         struct compound_hdr hdr = {
2764                 .minorversion = args->client->cl_mvops->minor_version,
2765         };
2766
2767         encode_compound_hdr(xdr, req, &hdr);
2768         encode_exchange_id(xdr, args, &hdr);
2769         encode_nops(&hdr);
2770 }
2771
2772 /*
2773  * a CREATE_SESSION request
2774  */
2775 static void nfs4_xdr_enc_create_session(struct rpc_rqst *req,
2776                                         struct xdr_stream *xdr,
2777                                         struct nfs41_create_session_args *args)
2778 {
2779         struct compound_hdr hdr = {
2780                 .minorversion = args->client->cl_mvops->minor_version,
2781         };
2782
2783         encode_compound_hdr(xdr, req, &hdr);
2784         encode_create_session(xdr, args, &hdr);
2785         encode_nops(&hdr);
2786 }
2787
2788 /*
2789  * a DESTROY_SESSION request
2790  */
2791 static void nfs4_xdr_enc_destroy_session(struct rpc_rqst *req,
2792                                          struct xdr_stream *xdr,
2793                                          struct nfs4_session *session)
2794 {
2795         struct compound_hdr hdr = {
2796                 .minorversion = session->clp->cl_mvops->minor_version,
2797         };
2798
2799         encode_compound_hdr(xdr, req, &hdr);
2800         encode_destroy_session(xdr, session, &hdr);
2801         encode_nops(&hdr);
2802 }
2803
2804 /*
2805  * a SEQUENCE request
2806  */
2807 static void nfs4_xdr_enc_sequence(struct rpc_rqst *req, struct xdr_stream *xdr,
2808                                   struct nfs4_sequence_args *args)
2809 {
2810         struct compound_hdr hdr = {
2811                 .minorversion = nfs4_xdr_minorversion(args),
2812         };
2813
2814         encode_compound_hdr(xdr, req, &hdr);
2815         encode_sequence(xdr, args, &hdr);
2816         encode_nops(&hdr);
2817 }
2818
2819 /*
2820  * a GET_LEASE_TIME request
2821  */
2822 static void nfs4_xdr_enc_get_lease_time(struct rpc_rqst *req,
2823                                         struct xdr_stream *xdr,
2824                                         struct nfs4_get_lease_time_args *args)
2825 {
2826         struct compound_hdr hdr = {
2827                 .minorversion = nfs4_xdr_minorversion(&args->la_seq_args),
2828         };
2829         const u32 lease_bitmap[3] = { FATTR4_WORD0_LEASE_TIME };
2830
2831         encode_compound_hdr(xdr, req, &hdr);
2832         encode_sequence(xdr, &args->la_seq_args, &hdr);
2833         encode_putrootfh(xdr, &hdr);
2834         encode_fsinfo(xdr, lease_bitmap, &hdr);
2835         encode_nops(&hdr);
2836 }
2837
2838 /*
2839  * a RECLAIM_COMPLETE request
2840  */
2841 static void nfs4_xdr_enc_reclaim_complete(struct rpc_rqst *req,
2842                                           struct xdr_stream *xdr,
2843                                 struct nfs41_reclaim_complete_args *args)
2844 {
2845         struct compound_hdr hdr = {
2846                 .minorversion = nfs4_xdr_minorversion(&args->seq_args)
2847         };
2848
2849         encode_compound_hdr(xdr, req, &hdr);
2850         encode_sequence(xdr, &args->seq_args, &hdr);
2851         encode_reclaim_complete(xdr, args, &hdr);
2852         encode_nops(&hdr);
2853 }
2854
2855 /*
2856  * Encode GETDEVICELIST request
2857  */
2858 static void nfs4_xdr_enc_getdevicelist(struct rpc_rqst *req,
2859                                        struct xdr_stream *xdr,
2860                                        struct nfs4_getdevicelist_args *args)
2861 {
2862         struct compound_hdr hdr = {
2863                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2864         };
2865
2866         encode_compound_hdr(xdr, req, &hdr);
2867         encode_sequence(xdr, &args->seq_args, &hdr);
2868         encode_putfh(xdr, args->fh, &hdr);
2869         encode_getdevicelist(xdr, args, &hdr);
2870         encode_nops(&hdr);
2871 }
2872
2873 /*
2874  * Encode GETDEVICEINFO request
2875  */
2876 static void nfs4_xdr_enc_getdeviceinfo(struct rpc_rqst *req,
2877                                        struct xdr_stream *xdr,
2878                                        struct nfs4_getdeviceinfo_args *args)
2879 {
2880         struct compound_hdr hdr = {
2881                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2882         };
2883
2884         encode_compound_hdr(xdr, req, &hdr);
2885         encode_sequence(xdr, &args->seq_args, &hdr);
2886         encode_getdeviceinfo(xdr, args, &hdr);
2887
2888         /* set up reply kvec. Subtract notification bitmap max size (2)
2889          * so that notification bitmap is put in xdr_buf tail */
2890         xdr_inline_pages(&req->rq_rcv_buf, (hdr.replen - 2) << 2,
2891                          args->pdev->pages, args->pdev->pgbase,
2892                          args->pdev->pglen);
2893
2894         encode_nops(&hdr);
2895 }
2896
2897 /*
2898  *  Encode LAYOUTGET request
2899  */
2900 static void nfs4_xdr_enc_layoutget(struct rpc_rqst *req,
2901                                    struct xdr_stream *xdr,
2902                                    struct nfs4_layoutget_args *args)
2903 {
2904         struct compound_hdr hdr = {
2905                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2906         };
2907
2908         encode_compound_hdr(xdr, req, &hdr);
2909         encode_sequence(xdr, &args->seq_args, &hdr);
2910         encode_putfh(xdr, NFS_FH(args->inode), &hdr);
2911         encode_layoutget(xdr, args, &hdr);
2912
2913         xdr_inline_pages(&req->rq_rcv_buf, hdr.replen << 2,
2914             args->layout.pages, 0, args->layout.pglen);
2915
2916         encode_nops(&hdr);
2917 }
2918
2919 /*
2920  *  Encode LAYOUTCOMMIT request
2921  */
2922 static void nfs4_xdr_enc_layoutcommit(struct rpc_rqst *req,
2923                                       struct xdr_stream *xdr,
2924                                       struct nfs4_layoutcommit_args *args)
2925 {
2926         struct nfs4_layoutcommit_data *data =
2927                 container_of(args, struct nfs4_layoutcommit_data, args);
2928         struct compound_hdr hdr = {
2929                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2930         };
2931
2932         encode_compound_hdr(xdr, req, &hdr);
2933         encode_sequence(xdr, &args->seq_args, &hdr);
2934         encode_putfh(xdr, NFS_FH(args->inode), &hdr);
2935         encode_layoutcommit(xdr, data->args.inode, args, &hdr);
2936         encode_getfattr(xdr, args->bitmask, &hdr);
2937         encode_nops(&hdr);
2938 }
2939
2940 /*
2941  * Encode LAYOUTRETURN request
2942  */
2943 static void nfs4_xdr_enc_layoutreturn(struct rpc_rqst *req,
2944                                       struct xdr_stream *xdr,
2945                                       struct nfs4_layoutreturn_args *args)
2946 {
2947         struct compound_hdr hdr = {
2948                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2949         };
2950
2951         encode_compound_hdr(xdr, req, &hdr);
2952         encode_sequence(xdr, &args->seq_args, &hdr);
2953         encode_putfh(xdr, NFS_FH(args->inode), &hdr);
2954         encode_layoutreturn(xdr, args, &hdr);
2955         encode_nops(&hdr);
2956 }
2957
2958 /*
2959  * Encode SECINFO_NO_NAME request
2960  */
2961 static int nfs4_xdr_enc_secinfo_no_name(struct rpc_rqst *req,
2962                                         struct xdr_stream *xdr,
2963                                         struct nfs41_secinfo_no_name_args *args)
2964 {
2965         struct compound_hdr hdr = {
2966                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2967         };
2968
2969         encode_compound_hdr(xdr, req, &hdr);
2970         encode_sequence(xdr, &args->seq_args, &hdr);
2971         encode_putrootfh(xdr, &hdr);
2972         encode_secinfo_no_name(xdr, args, &hdr);
2973         encode_nops(&hdr);
2974         return 0;
2975 }
2976
2977 /*
2978  *  Encode TEST_STATEID request
2979  */
2980 static void nfs4_xdr_enc_test_stateid(struct rpc_rqst *req,
2981                                       struct xdr_stream *xdr,
2982                                       struct nfs41_test_stateid_args *args)
2983 {
2984         struct compound_hdr hdr = {
2985                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
2986         };
2987
2988         encode_compound_hdr(xdr, req, &hdr);
2989         encode_sequence(xdr, &args->seq_args, &hdr);
2990         encode_test_stateid(xdr, args, &hdr);
2991         encode_nops(&hdr);
2992 }
2993
2994 /*
2995  *  Encode FREE_STATEID request
2996  */
2997 static void nfs4_xdr_enc_free_stateid(struct rpc_rqst *req,
2998                                      struct xdr_stream *xdr,
2999                                      struct nfs41_free_stateid_args *args)
3000 {
3001         struct compound_hdr hdr = {
3002                 .minorversion = nfs4_xdr_minorversion(&args->seq_args),
3003         };
3004
3005         encode_compound_hdr(xdr, req, &hdr);
3006         encode_sequence(xdr, &args->seq_args, &hdr);
3007         encode_free_stateid(xdr, args, &hdr);
3008         encode_nops(&hdr);
3009 }
3010 #endif /* CONFIG_NFS_V4_1 */
3011
3012 static void print_overflow_msg(const char *func, const struct xdr_stream *xdr)
3013 {
3014         dprintk("nfs: %s: prematurely hit end of receive buffer. "
3015                 "Remaining buffer length is %tu words.\n",
3016                 func, xdr->end - xdr->p);
3017 }
3018
3019 static int decode_opaque_inline(struct xdr_stream *xdr, unsigned int *len, char **string)
3020 {
3021         __be32 *p;
3022
3023         p = xdr_inline_decode(xdr, 4);
3024         if (unlikely(!p))
3025                 goto out_overflow;
3026         *len = be32_to_cpup(p);
3027         p = xdr_inline_decode(xdr, *len);
3028         if (unlikely(!p))
3029                 goto out_overflow;
3030         *string = (char *)p;
3031         return 0;
3032 out_overflow:
3033         print_overflow_msg(__func__, xdr);
3034         return -EIO;
3035 }
3036
3037 static int decode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
3038 {
3039         __be32 *p;
3040
3041         p = xdr_inline_decode(xdr, 8);
3042         if (unlikely(!p))
3043                 goto out_overflow;
3044         hdr->status = be32_to_cpup(p++);
3045         hdr->taglen = be32_to_cpup(p);
3046
3047         p = xdr_inline_decode(xdr, hdr->taglen + 4);
3048         if (unlikely(!p))
3049                 goto out_overflow;
3050         hdr->tag = (char *)p;
3051         p += XDR_QUADLEN(hdr->taglen);
3052         hdr->nops = be32_to_cpup(p);
3053         if (unlikely(hdr->nops < 1))
3054                 return nfs4_stat_to_errno(hdr->status);
3055         return 0;
3056 out_overflow:
3057         print_overflow_msg(__func__, xdr);
3058         return -EIO;
3059 }
3060
3061 static bool __decode_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected,
3062                 int *nfs_retval)
3063 {
3064         __be32 *p;
3065         uint32_t opnum;
3066         int32_t nfserr;
3067
3068         p = xdr_inline_decode(xdr, 8);
3069         if (unlikely(!p))
3070                 goto out_overflow;
3071         opnum = be32_to_cpup(p++);
3072         if (unlikely(opnum != expected))
3073                 goto out_bad_operation;
3074         nfserr = be32_to_cpup(p);
3075         if (nfserr == NFS_OK)
3076                 *nfs_retval = 0;
3077         else
3078                 *nfs_retval = nfs4_stat_to_errno(nfserr);
3079         return true;
3080 out_bad_operation:
3081         dprintk("nfs: Server returned operation"
3082                 " %d but we issued a request for %d\n",
3083                         opnum, expected);
3084         *nfs_retval = -EREMOTEIO;
3085         return false;
3086 out_overflow:
3087         print_overflow_msg(__func__, xdr);
3088         *nfs_retval = -EIO;
3089         return false;
3090 }
3091
3092 static int decode_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
3093 {
3094         int retval;
3095
3096         __decode_op_hdr(xdr, expected, &retval);
3097         return retval;
3098 }
3099
3100 /* Dummy routine */
3101 static int decode_ace(struct xdr_stream *xdr, void *ace, struct nfs_client *clp)
3102 {
3103         __be32 *p;
3104         unsigned int strlen;
3105         char *str;
3106
3107         p = xdr_inline_decode(xdr, 12);
3108         if (likely(p))
3109                 return decode_opaque_inline(xdr, &strlen, &str);
3110         print_overflow_msg(__func__, xdr);
3111         return -EIO;
3112 }
3113
3114 static int decode_attr_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
3115 {