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