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