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