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