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