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