NFSv4: Decode mounted_on_fileid attribute in getattr.
[safe/jmp/linux-2.6] / fs / nfs / nfs4xdr.c
1 /*
2  *  fs/nfs/nfs4xdr.c
3  *
4  *  Client-side XDR for NFSv4.
5  *
6  *  Copyright (c) 2002 The Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  Kendrick Smith <kmsmith@umich.edu>
10  *  Andy Adamson   <andros@umich.edu>
11  * 
12  *  Redistribution and use in source and binary forms, with or without
13  *  modification, are permitted provided that the following conditions
14  *  are met:
15  *
16  *  1. Redistributions of source code must retain the above copyright
17  *     notice, this list of conditions and the following disclaimer.
18  *  2. Redistributions in binary form must reproduce the above copyright
19  *     notice, this list of conditions and the following disclaimer in the
20  *     documentation and/or other materials provided with the distribution.
21  *  3. Neither the name of the University nor the names of its
22  *     contributors may be used to endorse or promote products derived
23  *     from this software without specific prior written permission.
24  *
25  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37
38 #include <linux/param.h>
39 #include <linux/time.h>
40 #include <linux/mm.h>
41 #include <linux/slab.h>
42 #include <linux/utsname.h>
43 #include <linux/errno.h>
44 #include <linux/string.h>
45 #include <linux/in.h>
46 #include <linux/pagemap.h>
47 #include <linux/proc_fs.h>
48 #include <linux/kdev_t.h>
49 #include <linux/sunrpc/clnt.h>
50 #include <linux/nfs.h>
51 #include <linux/nfs4.h>
52 #include <linux/nfs_fs.h>
53 #include <linux/nfs_idmap.h>
54 #include "nfs4_fs.h"
55
56 #define NFSDBG_FACILITY         NFSDBG_XDR
57
58 /* Mapping from NFS error code to "errno" error code. */
59 #define errno_NFSERR_IO         EIO
60
61 static int nfs_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 1 (u32) out of (NFS4_OPAQUE_LIMIT  >> 2)
72  */
73 #define owner_id_maxsz          (1 + 1)
74 #define compound_encode_hdr_maxsz       (3 + (NFS4_MAXTAGLEN >> 2))
75 #define compound_decode_hdr_maxsz       (3 + (NFS4_MAXTAGLEN >> 2))
76 #define op_encode_hdr_maxsz     (1)
77 #define op_decode_hdr_maxsz     (2)
78 #define encode_putfh_maxsz      (op_encode_hdr_maxsz + 1 + \
79                                 (NFS4_FHSIZE >> 2))
80 #define decode_putfh_maxsz      (op_decode_hdr_maxsz)
81 #define encode_putrootfh_maxsz  (op_encode_hdr_maxsz)
82 #define decode_putrootfh_maxsz  (op_decode_hdr_maxsz)
83 #define encode_getfh_maxsz      (op_encode_hdr_maxsz)
84 #define decode_getfh_maxsz      (op_decode_hdr_maxsz + 1 + \
85                                 ((3+NFS4_FHSIZE) >> 2))
86 #define nfs4_fattr_bitmap_maxsz 3
87 #define encode_getattr_maxsz    (op_encode_hdr_maxsz + nfs4_fattr_bitmap_maxsz)
88 #define nfs4_name_maxsz         (1 + ((3 + NFS4_MAXNAMLEN) >> 2))
89 #define nfs4_path_maxsz         (1 + ((3 + NFS4_MAXPATHLEN) >> 2))
90 /* This is based on getfattr, which uses the most attributes: */
91 #define nfs4_fattr_value_maxsz  (1 + (1 + 2 + 2 + 4 + 2 + 1 + 1 + 2 + 2 + \
92                                 3 + 3 + 3 + 2 * nfs4_name_maxsz))
93 #define nfs4_fattr_maxsz        (nfs4_fattr_bitmap_maxsz + \
94                                 nfs4_fattr_value_maxsz)
95 #define decode_getattr_maxsz    (op_decode_hdr_maxsz + nfs4_fattr_maxsz)
96 #define encode_savefh_maxsz     (op_encode_hdr_maxsz)
97 #define decode_savefh_maxsz     (op_decode_hdr_maxsz)
98 #define encode_restorefh_maxsz  (op_encode_hdr_maxsz)
99 #define decode_restorefh_maxsz  (op_decode_hdr_maxsz)
100 #define encode_fsinfo_maxsz     (op_encode_hdr_maxsz + 2)
101 #define decode_fsinfo_maxsz     (op_decode_hdr_maxsz + 11)
102 #define encode_renew_maxsz      (op_encode_hdr_maxsz + 3)
103 #define decode_renew_maxsz      (op_decode_hdr_maxsz)
104 #define encode_setclientid_maxsz \
105                                 (op_encode_hdr_maxsz + \
106                                 4 /*server->ip_addr*/ + \
107                                 1 /*Netid*/ + \
108                                 6 /*uaddr*/ + \
109                                 6 + (NFS4_VERIFIER_SIZE >> 2))
110 #define decode_setclientid_maxsz \
111                                 (op_decode_hdr_maxsz + \
112                                 2 + \
113                                 1024) /* large value for CLID_INUSE */
114 #define encode_setclientid_confirm_maxsz \
115                                 (op_encode_hdr_maxsz + \
116                                 3 + (NFS4_VERIFIER_SIZE >> 2))
117 #define decode_setclientid_confirm_maxsz \
118                                 (op_decode_hdr_maxsz)
119 #define encode_lookup_maxsz     (op_encode_hdr_maxsz + \
120                                 1 + ((3 + NFS4_FHSIZE) >> 2))
121 #define encode_remove_maxsz     (op_encode_hdr_maxsz + \
122                                 nfs4_name_maxsz)
123 #define encode_rename_maxsz     (op_encode_hdr_maxsz + \
124                                 2 * nfs4_name_maxsz)
125 #define decode_rename_maxsz     (op_decode_hdr_maxsz + 5 + 5)
126 #define encode_link_maxsz       (op_encode_hdr_maxsz + \
127                                 nfs4_name_maxsz)
128 #define decode_link_maxsz       (op_decode_hdr_maxsz + 5)
129 #define encode_symlink_maxsz    (op_encode_hdr_maxsz + \
130                                 1 + nfs4_name_maxsz + \
131                                 nfs4_path_maxsz + \
132                                 nfs4_fattr_maxsz)
133 #define decode_symlink_maxsz    (op_decode_hdr_maxsz + 8)
134 #define encode_create_maxsz     (op_encode_hdr_maxsz + \
135                                 2 + nfs4_name_maxsz + \
136                                 nfs4_fattr_maxsz)
137 #define decode_create_maxsz     (op_decode_hdr_maxsz + 8)
138 #define encode_delegreturn_maxsz (op_encode_hdr_maxsz + 4)
139 #define decode_delegreturn_maxsz (op_decode_hdr_maxsz)
140 #define NFS4_enc_compound_sz    (1024)  /* XXX: large enough? */
141 #define NFS4_dec_compound_sz    (1024)  /* XXX: large enough? */
142 #define NFS4_enc_read_sz        (compound_encode_hdr_maxsz + \
143                                 encode_putfh_maxsz + \
144                                 op_encode_hdr_maxsz + 7)
145 #define NFS4_dec_read_sz        (compound_decode_hdr_maxsz + \
146                                 decode_putfh_maxsz + \
147                                 op_decode_hdr_maxsz + 2)
148 #define NFS4_enc_readlink_sz    (compound_encode_hdr_maxsz + \
149                                 encode_putfh_maxsz + \
150                                 op_encode_hdr_maxsz)
151 #define NFS4_dec_readlink_sz    (compound_decode_hdr_maxsz + \
152                                 decode_putfh_maxsz + \
153                                 op_decode_hdr_maxsz)
154 #define NFS4_enc_readdir_sz     (compound_encode_hdr_maxsz + \
155                                 encode_putfh_maxsz + \
156                                 op_encode_hdr_maxsz + 9)
157 #define NFS4_dec_readdir_sz     (compound_decode_hdr_maxsz + \
158                                 decode_putfh_maxsz + \
159                                 op_decode_hdr_maxsz + 2)
160 #define NFS4_enc_write_sz       (compound_encode_hdr_maxsz + \
161                                 encode_putfh_maxsz + \
162                                 op_encode_hdr_maxsz + 8 + \
163                                 encode_getattr_maxsz)
164 #define NFS4_dec_write_sz       (compound_decode_hdr_maxsz + \
165                                 decode_putfh_maxsz + \
166                                 op_decode_hdr_maxsz + 4 + \
167                                 decode_getattr_maxsz)
168 #define NFS4_enc_commit_sz      (compound_encode_hdr_maxsz + \
169                                 encode_putfh_maxsz + \
170                                 op_encode_hdr_maxsz + 3 + \
171                                 encode_getattr_maxsz)
172 #define NFS4_dec_commit_sz      (compound_decode_hdr_maxsz + \
173                                 decode_putfh_maxsz + \
174                                 op_decode_hdr_maxsz + 2 + \
175                                 decode_getattr_maxsz)
176 #define NFS4_enc_open_sz        (compound_encode_hdr_maxsz + \
177                                 encode_putfh_maxsz + \
178                                 op_encode_hdr_maxsz + \
179                                 13 + 3 + 2 + 64 + \
180                                 encode_getattr_maxsz + \
181                                 encode_getfh_maxsz)
182 #define NFS4_dec_open_sz        (compound_decode_hdr_maxsz + \
183                                 decode_putfh_maxsz + \
184                                 op_decode_hdr_maxsz + 4 + 5 + 2 + 3 + \
185                                 decode_getattr_maxsz + \
186                                 decode_getfh_maxsz)
187 #define NFS4_enc_open_confirm_sz      \
188                                 (compound_encode_hdr_maxsz + \
189                                 encode_putfh_maxsz + \
190                                 op_encode_hdr_maxsz + 5)
191 #define NFS4_dec_open_confirm_sz        (compound_decode_hdr_maxsz + \
192                                         decode_putfh_maxsz + \
193                                         op_decode_hdr_maxsz + 4)
194 #define NFS4_enc_open_noattr_sz (compound_encode_hdr_maxsz + \
195                                         encode_putfh_maxsz + \
196                                         op_encode_hdr_maxsz + \
197                                         11)
198 #define NFS4_dec_open_noattr_sz (compound_decode_hdr_maxsz + \
199                                         decode_putfh_maxsz + \
200                                         op_decode_hdr_maxsz + \
201                                         4 + 5 + 2 + 3)
202 #define NFS4_enc_open_downgrade_sz \
203                                 (compound_encode_hdr_maxsz + \
204                                 encode_putfh_maxsz + \
205                                 op_encode_hdr_maxsz + 7 + \
206                                 encode_getattr_maxsz)
207 #define NFS4_dec_open_downgrade_sz \
208                                 (compound_decode_hdr_maxsz + \
209                                 decode_putfh_maxsz + \
210                                 op_decode_hdr_maxsz + 4 + \
211                                 decode_getattr_maxsz)
212 #define NFS4_enc_close_sz       (compound_encode_hdr_maxsz + \
213                                 encode_putfh_maxsz + \
214                                 op_encode_hdr_maxsz + 5 + \
215                                 encode_getattr_maxsz)
216 #define NFS4_dec_close_sz       (compound_decode_hdr_maxsz + \
217                                 decode_putfh_maxsz + \
218                                 op_decode_hdr_maxsz + 4 + \
219                                 decode_getattr_maxsz)
220 #define NFS4_enc_setattr_sz     (compound_encode_hdr_maxsz + \
221                                 encode_putfh_maxsz + \
222                                 op_encode_hdr_maxsz + 4 + \
223                                 nfs4_fattr_maxsz + \
224                                 encode_getattr_maxsz)
225 #define NFS4_dec_setattr_sz     (compound_decode_hdr_maxsz + \
226                                 decode_putfh_maxsz + \
227                                 op_decode_hdr_maxsz + 3)
228 #define NFS4_enc_fsinfo_sz      (compound_encode_hdr_maxsz + \
229                                 encode_putfh_maxsz + \
230                                 encode_fsinfo_maxsz)
231 #define NFS4_dec_fsinfo_sz      (compound_decode_hdr_maxsz + \
232                                 decode_putfh_maxsz + \
233                                 decode_fsinfo_maxsz)
234 #define NFS4_enc_renew_sz       (compound_encode_hdr_maxsz + \
235                                 encode_renew_maxsz)
236 #define NFS4_dec_renew_sz       (compound_decode_hdr_maxsz + \
237                                 decode_renew_maxsz)
238 #define NFS4_enc_setclientid_sz (compound_encode_hdr_maxsz + \
239                                 encode_setclientid_maxsz)
240 #define NFS4_dec_setclientid_sz (compound_decode_hdr_maxsz + \
241                                 decode_setclientid_maxsz)
242 #define NFS4_enc_setclientid_confirm_sz \
243                                 (compound_encode_hdr_maxsz + \
244                                 encode_setclientid_confirm_maxsz + \
245                                 encode_putrootfh_maxsz + \
246                                 encode_fsinfo_maxsz)
247 #define NFS4_dec_setclientid_confirm_sz \
248                                 (compound_decode_hdr_maxsz + \
249                                 decode_setclientid_confirm_maxsz + \
250                                 decode_putrootfh_maxsz + \
251                                 decode_fsinfo_maxsz)
252 #define NFS4_enc_lock_sz        (compound_encode_hdr_maxsz + \
253                                 encode_putfh_maxsz + \
254                                 encode_getattr_maxsz + \
255                                 op_encode_hdr_maxsz + \
256                                 1 + 1 + 2 + 2 + \
257                                 1 + 4 + 1 + 2 + \
258                                 owner_id_maxsz)
259 #define NFS4_dec_lock_sz        (compound_decode_hdr_maxsz + \
260                                 decode_putfh_maxsz + \
261                                 decode_getattr_maxsz + \
262                                 op_decode_hdr_maxsz + \
263                                 2 + 2 + 1 + 2 + \
264                                 owner_id_maxsz)
265 #define NFS4_enc_lockt_sz       (compound_encode_hdr_maxsz + \
266                                 encode_putfh_maxsz + \
267                                 encode_getattr_maxsz + \
268                                 op_encode_hdr_maxsz + \
269                                 1 + 2 + 2 + 2 + \
270                                 owner_id_maxsz)
271 #define NFS4_dec_lockt_sz       (NFS4_dec_lock_sz)
272 #define NFS4_enc_locku_sz       (compound_encode_hdr_maxsz + \
273                                 encode_putfh_maxsz + \
274                                 encode_getattr_maxsz + \
275                                 op_encode_hdr_maxsz + \
276                                 1 + 1 + 4 + 2 + 2)
277 #define NFS4_dec_locku_sz       (compound_decode_hdr_maxsz + \
278                                 decode_putfh_maxsz + \
279                                 decode_getattr_maxsz + \
280                                 op_decode_hdr_maxsz + 4)
281 #define NFS4_enc_access_sz      (compound_encode_hdr_maxsz + \
282                                 encode_putfh_maxsz + \
283                                 op_encode_hdr_maxsz + 1)
284 #define NFS4_dec_access_sz      (compound_decode_hdr_maxsz + \
285                                 decode_putfh_maxsz + \
286                                 op_decode_hdr_maxsz + 2)
287 #define NFS4_enc_getattr_sz     (compound_encode_hdr_maxsz + \
288                                 encode_putfh_maxsz + \
289                                 encode_getattr_maxsz)
290 #define NFS4_dec_getattr_sz     (compound_decode_hdr_maxsz + \
291                                 decode_putfh_maxsz + \
292                                 decode_getattr_maxsz)
293 #define NFS4_enc_lookup_sz      (compound_encode_hdr_maxsz + \
294                                 encode_putfh_maxsz + \
295                                 encode_lookup_maxsz + \
296                                 encode_getattr_maxsz + \
297                                 encode_getfh_maxsz)
298 #define NFS4_dec_lookup_sz      (compound_decode_hdr_maxsz + \
299                                 decode_putfh_maxsz + \
300                                 op_decode_hdr_maxsz + \
301                                 decode_getattr_maxsz + \
302                                 decode_getfh_maxsz)
303 #define NFS4_enc_lookup_root_sz (compound_encode_hdr_maxsz + \
304                                 encode_putrootfh_maxsz + \
305                                 encode_getattr_maxsz + \
306                                 encode_getfh_maxsz)
307 #define NFS4_dec_lookup_root_sz (compound_decode_hdr_maxsz + \
308                                 decode_putrootfh_maxsz + \
309                                 decode_getattr_maxsz + \
310                                 decode_getfh_maxsz)
311 #define NFS4_enc_remove_sz      (compound_encode_hdr_maxsz + \
312                                 encode_putfh_maxsz + \
313                                 encode_remove_maxsz + \
314                                 encode_getattr_maxsz)
315 #define NFS4_dec_remove_sz      (compound_decode_hdr_maxsz + \
316                                 decode_putfh_maxsz + \
317                                 op_decode_hdr_maxsz + 5 + \
318                                 decode_getattr_maxsz)
319 #define NFS4_enc_rename_sz      (compound_encode_hdr_maxsz + \
320                                 encode_putfh_maxsz + \
321                                 encode_savefh_maxsz + \
322                                 encode_putfh_maxsz + \
323                                 encode_rename_maxsz + \
324                                 encode_getattr_maxsz + \
325                                 encode_restorefh_maxsz + \
326                                 encode_getattr_maxsz)
327 #define NFS4_dec_rename_sz      (compound_decode_hdr_maxsz + \
328                                 decode_putfh_maxsz + \
329                                 decode_savefh_maxsz + \
330                                 decode_putfh_maxsz + \
331                                 decode_rename_maxsz + \
332                                 decode_getattr_maxsz + \
333                                 decode_restorefh_maxsz + \
334                                 decode_getattr_maxsz)
335 #define NFS4_enc_link_sz        (compound_encode_hdr_maxsz + \
336                                 encode_putfh_maxsz + \
337                                 encode_savefh_maxsz + \
338                                 encode_putfh_maxsz + \
339                                 encode_link_maxsz + \
340                                 decode_getattr_maxsz + \
341                                 encode_restorefh_maxsz + \
342                                 decode_getattr_maxsz)
343 #define NFS4_dec_link_sz        (compound_decode_hdr_maxsz + \
344                                 decode_putfh_maxsz + \
345                                 decode_savefh_maxsz + \
346                                 decode_putfh_maxsz + \
347                                 decode_link_maxsz + \
348                                 decode_getattr_maxsz + \
349                                 decode_restorefh_maxsz + \
350                                 decode_getattr_maxsz)
351 #define NFS4_enc_symlink_sz     (compound_encode_hdr_maxsz + \
352                                 encode_putfh_maxsz + \
353                                 encode_symlink_maxsz + \
354                                 encode_getattr_maxsz + \
355                                 encode_getfh_maxsz)
356 #define NFS4_dec_symlink_sz     (compound_decode_hdr_maxsz + \
357                                 decode_putfh_maxsz + \
358                                 decode_symlink_maxsz + \
359                                 decode_getattr_maxsz + \
360                                 decode_getfh_maxsz)
361 #define NFS4_enc_create_sz      (compound_encode_hdr_maxsz + \
362                                 encode_putfh_maxsz + \
363                                 encode_savefh_maxsz + \
364                                 encode_create_maxsz + \
365                                 encode_getfh_maxsz + \
366                                 encode_getattr_maxsz + \
367                                 encode_restorefh_maxsz + \
368                                 encode_getattr_maxsz)
369 #define NFS4_dec_create_sz      (compound_decode_hdr_maxsz + \
370                                 decode_putfh_maxsz + \
371                                 decode_savefh_maxsz + \
372                                 decode_create_maxsz + \
373                                 decode_getfh_maxsz + \
374                                 decode_getattr_maxsz + \
375                                 decode_restorefh_maxsz + \
376                                 decode_getattr_maxsz)
377 #define NFS4_enc_pathconf_sz    (compound_encode_hdr_maxsz + \
378                                 encode_putfh_maxsz + \
379                                 encode_getattr_maxsz)
380 #define NFS4_dec_pathconf_sz    (compound_decode_hdr_maxsz + \
381                                 decode_putfh_maxsz + \
382                                 decode_getattr_maxsz)
383 #define NFS4_enc_statfs_sz      (compound_encode_hdr_maxsz + \
384                                 encode_putfh_maxsz + \
385                                 encode_getattr_maxsz)
386 #define NFS4_dec_statfs_sz      (compound_decode_hdr_maxsz + \
387                                 decode_putfh_maxsz + \
388                                 op_decode_hdr_maxsz + 12)
389 #define NFS4_enc_server_caps_sz (compound_encode_hdr_maxsz + \
390                                 encode_getattr_maxsz)
391 #define NFS4_dec_server_caps_sz (compound_decode_hdr_maxsz + \
392                                 decode_getattr_maxsz)
393 #define NFS4_enc_delegreturn_sz (compound_encode_hdr_maxsz + \
394                                 encode_putfh_maxsz + \
395                                 encode_delegreturn_maxsz + \
396                                 encode_getattr_maxsz)
397 #define NFS4_dec_delegreturn_sz (compound_decode_hdr_maxsz + \
398                                 decode_delegreturn_maxsz + \
399                                 decode_getattr_maxsz)
400 #define NFS4_enc_getacl_sz      (compound_encode_hdr_maxsz + \
401                                 encode_putfh_maxsz + \
402                                 encode_getattr_maxsz)
403 #define NFS4_dec_getacl_sz      (compound_decode_hdr_maxsz + \
404                                 decode_putfh_maxsz + \
405                                 op_decode_hdr_maxsz + \
406                                 nfs4_fattr_bitmap_maxsz + 1)
407 #define NFS4_enc_setacl_sz      (compound_encode_hdr_maxsz + \
408                                 encode_putfh_maxsz + \
409                                 op_encode_hdr_maxsz + 4 + \
410                                 nfs4_fattr_bitmap_maxsz + 1)
411 #define NFS4_dec_setacl_sz      (compound_decode_hdr_maxsz + \
412                                 decode_putfh_maxsz + \
413                                 op_decode_hdr_maxsz + nfs4_fattr_bitmap_maxsz)
414 #define NFS4_enc_fs_locations_sz \
415                                 (compound_encode_hdr_maxsz + \
416                                  encode_putfh_maxsz + \
417                                  encode_getattr_maxsz)
418 #define NFS4_dec_fs_locations_sz \
419                                 (compound_decode_hdr_maxsz + \
420                                  decode_putfh_maxsz + \
421                                  op_decode_hdr_maxsz + \
422                                  nfs4_fattr_bitmap_maxsz)
423
424 static struct {
425         unsigned int    mode;
426         unsigned int    nfs2type;
427 } nfs_type2fmt[] = {
428         { 0,            NFNON        },
429         { S_IFREG,      NFREG        },
430         { S_IFDIR,      NFDIR        },
431         { S_IFBLK,      NFBLK        },
432         { S_IFCHR,      NFCHR        },
433         { S_IFLNK,      NFLNK        },
434         { S_IFSOCK,     NFSOCK       },
435         { S_IFIFO,      NFFIFO       },
436         { 0,            NFNON        },
437         { 0,            NFNON        },
438 };
439
440 struct compound_hdr {
441         int32_t         status;
442         uint32_t        nops;
443         uint32_t        taglen;
444         char *          tag;
445 };
446
447 /*
448  * START OF "GENERIC" ENCODE ROUTINES.
449  *   These may look a little ugly since they are imported from a "generic"
450  * set of XDR encode/decode routines which are intended to be shared by
451  * all of our NFSv4 implementations (OpenBSD, MacOS X...).
452  *
453  * If the pain of reading these is too great, it should be a straightforward
454  * task to translate them into Linux-specific versions which are more
455  * consistent with the style used in NFSv2/v3...
456  */
457 #define WRITE32(n)               *p++ = htonl(n)
458 #define WRITE64(n)               do {                           \
459         *p++ = htonl((uint32_t)((n) >> 32));                            \
460         *p++ = htonl((uint32_t)(n));                                    \
461 } while (0)
462 #define WRITEMEM(ptr,nbytes)     do {                           \
463         p = xdr_encode_opaque_fixed(p, ptr, nbytes);            \
464 } while (0)
465
466 #define RESERVE_SPACE(nbytes)   do {                            \
467         p = xdr_reserve_space(xdr, nbytes);                     \
468         if (!p) printk("RESERVE_SPACE(%d) failed in function %s\n", (int) (nbytes), __FUNCTION__); \
469         BUG_ON(!p);                                             \
470 } while (0)
471
472 static void encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
473 {
474         uint32_t *p;
475
476         p = xdr_reserve_space(xdr, 4 + len);
477         BUG_ON(p == NULL);
478         xdr_encode_opaque(p, str, len);
479 }
480
481 static int encode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
482 {
483         uint32_t *p;
484
485         dprintk("encode_compound: tag=%.*s\n", (int)hdr->taglen, hdr->tag);
486         BUG_ON(hdr->taglen > NFS4_MAXTAGLEN);
487         RESERVE_SPACE(12+(XDR_QUADLEN(hdr->taglen)<<2));
488         WRITE32(hdr->taglen);
489         WRITEMEM(hdr->tag, hdr->taglen);
490         WRITE32(NFS4_MINOR_VERSION);
491         WRITE32(hdr->nops);
492         return 0;
493 }
494
495 static void encode_nfs4_verifier(struct xdr_stream *xdr, const nfs4_verifier *verf)
496 {
497         uint32_t *p;
498
499         p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
500         BUG_ON(p == NULL);
501         xdr_encode_opaque_fixed(p, verf->data, NFS4_VERIFIER_SIZE);
502 }
503
504 static int encode_attrs(struct xdr_stream *xdr, const struct iattr *iap, const struct nfs_server *server)
505 {
506         char owner_name[IDMAP_NAMESZ];
507         char owner_group[IDMAP_NAMESZ];
508         int owner_namelen = 0;
509         int owner_grouplen = 0;
510         uint32_t *p;
511         uint32_t *q;
512         int len;
513         uint32_t bmval0 = 0;
514         uint32_t bmval1 = 0;
515         int status;
516
517         /*
518          * We reserve enough space to write the entire attribute buffer at once.
519          * In the worst-case, this would be
520          *   12(bitmap) + 4(attrlen) + 8(size) + 4(mode) + 4(atime) + 4(mtime)
521          *          = 36 bytes, plus any contribution from variable-length fields
522          *            such as owner/group.
523          */
524         len = 16;
525
526         /* Sigh */
527         if (iap->ia_valid & ATTR_SIZE)
528                 len += 8;
529         if (iap->ia_valid & ATTR_MODE)
530                 len += 4;
531         if (iap->ia_valid & ATTR_UID) {
532                 owner_namelen = nfs_map_uid_to_name(server->nfs4_state, iap->ia_uid, owner_name);
533                 if (owner_namelen < 0) {
534                         printk(KERN_WARNING "nfs: couldn't resolve uid %d to string\n",
535                                iap->ia_uid);
536                         /* XXX */
537                         strcpy(owner_name, "nobody");
538                         owner_namelen = sizeof("nobody") - 1;
539                         /* goto out; */
540                 }
541                 len += 4 + (XDR_QUADLEN(owner_namelen) << 2);
542         }
543         if (iap->ia_valid & ATTR_GID) {
544                 owner_grouplen = nfs_map_gid_to_group(server->nfs4_state, iap->ia_gid, owner_group);
545                 if (owner_grouplen < 0) {
546                         printk(KERN_WARNING "nfs4: couldn't resolve gid %d to string\n",
547                                iap->ia_gid);
548                         strcpy(owner_group, "nobody");
549                         owner_grouplen = sizeof("nobody") - 1;
550                         /* goto out; */
551                 }
552                 len += 4 + (XDR_QUADLEN(owner_grouplen) << 2);
553         }
554         if (iap->ia_valid & ATTR_ATIME_SET)
555                 len += 16;
556         else if (iap->ia_valid & ATTR_ATIME)
557                 len += 4;
558         if (iap->ia_valid & ATTR_MTIME_SET)
559                 len += 16;
560         else if (iap->ia_valid & ATTR_MTIME)
561                 len += 4;
562         RESERVE_SPACE(len);
563
564         /*
565          * We write the bitmap length now, but leave the bitmap and the attribute
566          * buffer length to be backfilled at the end of this routine.
567          */
568         WRITE32(2);
569         q = p;
570         p += 3;
571
572         if (iap->ia_valid & ATTR_SIZE) {
573                 bmval0 |= FATTR4_WORD0_SIZE;
574                 WRITE64(iap->ia_size);
575         }
576         if (iap->ia_valid & ATTR_MODE) {
577                 bmval1 |= FATTR4_WORD1_MODE;
578                 WRITE32(iap->ia_mode & S_IALLUGO);
579         }
580         if (iap->ia_valid & ATTR_UID) {
581                 bmval1 |= FATTR4_WORD1_OWNER;
582                 WRITE32(owner_namelen);
583                 WRITEMEM(owner_name, owner_namelen);
584         }
585         if (iap->ia_valid & ATTR_GID) {
586                 bmval1 |= FATTR4_WORD1_OWNER_GROUP;
587                 WRITE32(owner_grouplen);
588                 WRITEMEM(owner_group, owner_grouplen);
589         }
590         if (iap->ia_valid & ATTR_ATIME_SET) {
591                 bmval1 |= FATTR4_WORD1_TIME_ACCESS_SET;
592                 WRITE32(NFS4_SET_TO_CLIENT_TIME);
593                 WRITE32(0);
594                 WRITE32(iap->ia_mtime.tv_sec);
595                 WRITE32(iap->ia_mtime.tv_nsec);
596         }
597         else if (iap->ia_valid & ATTR_ATIME) {
598                 bmval1 |= FATTR4_WORD1_TIME_ACCESS_SET;
599                 WRITE32(NFS4_SET_TO_SERVER_TIME);
600         }
601         if (iap->ia_valid & ATTR_MTIME_SET) {
602                 bmval1 |= FATTR4_WORD1_TIME_MODIFY_SET;
603                 WRITE32(NFS4_SET_TO_CLIENT_TIME);
604                 WRITE32(0);
605                 WRITE32(iap->ia_mtime.tv_sec);
606                 WRITE32(iap->ia_mtime.tv_nsec);
607         }
608         else if (iap->ia_valid & ATTR_MTIME) {
609                 bmval1 |= FATTR4_WORD1_TIME_MODIFY_SET;
610                 WRITE32(NFS4_SET_TO_SERVER_TIME);
611         }
612         
613         /*
614          * Now we backfill the bitmap and the attribute buffer length.
615          */
616         if (len != ((char *)p - (char *)q) + 4) {
617                 printk ("encode_attr: Attr length calculation error! %u != %Zu\n",
618                                 len, ((char *)p - (char *)q) + 4);
619                 BUG();
620         }
621         len = (char *)p - (char *)q - 12;
622         *q++ = htonl(bmval0);
623         *q++ = htonl(bmval1);
624         *q++ = htonl(len);
625
626         status = 0;
627 /* out: */
628         return status;
629 }
630
631 static int encode_access(struct xdr_stream *xdr, u32 access)
632 {
633         uint32_t *p;
634
635         RESERVE_SPACE(8);
636         WRITE32(OP_ACCESS);
637         WRITE32(access);
638         
639         return 0;
640 }
641
642 static int encode_close(struct xdr_stream *xdr, const struct nfs_closeargs *arg)
643 {
644         uint32_t *p;
645
646         RESERVE_SPACE(8+sizeof(arg->stateid->data));
647         WRITE32(OP_CLOSE);
648         WRITE32(arg->seqid->sequence->counter);
649         WRITEMEM(arg->stateid->data, sizeof(arg->stateid->data));
650         
651         return 0;
652 }
653
654 static int encode_commit(struct xdr_stream *xdr, const struct nfs_writeargs *args)
655 {
656         uint32_t *p;
657         
658         RESERVE_SPACE(16);
659         WRITE32(OP_COMMIT);
660         WRITE64(args->offset);
661         WRITE32(args->count);
662
663         return 0;
664 }
665
666 static int encode_create(struct xdr_stream *xdr, const struct nfs4_create_arg *create)
667 {
668         uint32_t *p;
669         
670         RESERVE_SPACE(8);
671         WRITE32(OP_CREATE);
672         WRITE32(create->ftype);
673
674         switch (create->ftype) {
675         case NF4LNK:
676                 RESERVE_SPACE(4 + create->u.symlink->len);
677                 WRITE32(create->u.symlink->len);
678                 WRITEMEM(create->u.symlink->name, create->u.symlink->len);
679                 break;
680
681         case NF4BLK: case NF4CHR:
682                 RESERVE_SPACE(8);
683                 WRITE32(create->u.device.specdata1);
684                 WRITE32(create->u.device.specdata2);
685                 break;
686
687         default:
688                 break;
689         }
690
691         RESERVE_SPACE(4 + create->name->len);
692         WRITE32(create->name->len);
693         WRITEMEM(create->name->name, create->name->len);
694
695         return encode_attrs(xdr, create->attrs, create->server);
696 }
697
698 static int encode_getattr_one(struct xdr_stream *xdr, uint32_t bitmap)
699 {
700         uint32_t *p;
701
702         RESERVE_SPACE(12);
703         WRITE32(OP_GETATTR);
704         WRITE32(1);
705         WRITE32(bitmap);
706         return 0;
707 }
708
709 static int encode_getattr_two(struct xdr_stream *xdr, uint32_t bm0, uint32_t bm1)
710 {
711         uint32_t *p;
712
713         RESERVE_SPACE(16);
714         WRITE32(OP_GETATTR);
715         WRITE32(2);
716         WRITE32(bm0);
717         WRITE32(bm1);
718         return 0;
719 }
720
721 static int encode_getfattr(struct xdr_stream *xdr, const u32* bitmask)
722 {
723         return encode_getattr_two(xdr,
724                         bitmask[0] & nfs4_fattr_bitmap[0],
725                         bitmask[1] & nfs4_fattr_bitmap[1]);
726 }
727
728 static int encode_fsinfo(struct xdr_stream *xdr, const u32* bitmask)
729 {
730         return encode_getattr_two(xdr, bitmask[0] & nfs4_fsinfo_bitmap[0],
731                         bitmask[1] & nfs4_fsinfo_bitmap[1]);
732 }
733
734 static int encode_getfh(struct xdr_stream *xdr)
735 {
736         uint32_t *p;
737
738         RESERVE_SPACE(4);
739         WRITE32(OP_GETFH);
740
741         return 0;
742 }
743
744 static int encode_link(struct xdr_stream *xdr, const struct qstr *name)
745 {
746         uint32_t *p;
747
748         RESERVE_SPACE(8 + name->len);
749         WRITE32(OP_LINK);
750         WRITE32(name->len);
751         WRITEMEM(name->name, name->len);
752         
753         return 0;
754 }
755
756 static inline int nfs4_lock_type(struct file_lock *fl, int block)
757 {
758         if ((fl->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) == F_RDLCK)
759                 return block ? NFS4_READW_LT : NFS4_READ_LT;
760         return block ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
761 }
762
763 static inline uint64_t nfs4_lock_length(struct file_lock *fl)
764 {
765         if (fl->fl_end == OFFSET_MAX)
766                 return ~(uint64_t)0;
767         return fl->fl_end - fl->fl_start + 1;
768 }
769
770 /*
771  * opcode,type,reclaim,offset,length,new_lock_owner = 32
772  * open_seqid,open_stateid,lock_seqid,lock_owner.clientid, lock_owner.id = 40
773  */
774 static int encode_lock(struct xdr_stream *xdr, const struct nfs_lock_args *args)
775 {
776         uint32_t *p;
777
778         RESERVE_SPACE(32);
779         WRITE32(OP_LOCK);
780         WRITE32(nfs4_lock_type(args->fl, args->block));
781         WRITE32(args->reclaim);
782         WRITE64(args->fl->fl_start);
783         WRITE64(nfs4_lock_length(args->fl));
784         WRITE32(args->new_lock_owner);
785         if (args->new_lock_owner){
786                 RESERVE_SPACE(40);
787                 WRITE32(args->open_seqid->sequence->counter);
788                 WRITEMEM(args->open_stateid->data, sizeof(args->open_stateid->data));
789                 WRITE32(args->lock_seqid->sequence->counter);
790                 WRITE64(args->lock_owner.clientid);
791                 WRITE32(4);
792                 WRITE32(args->lock_owner.id);
793         }
794         else {
795                 RESERVE_SPACE(20);
796                 WRITEMEM(args->lock_stateid->data, sizeof(args->lock_stateid->data));
797                 WRITE32(args->lock_seqid->sequence->counter);
798         }
799
800         return 0;
801 }
802
803 static int encode_lockt(struct xdr_stream *xdr, const struct nfs_lockt_args *args)
804 {
805         uint32_t *p;
806
807         RESERVE_SPACE(40);
808         WRITE32(OP_LOCKT);
809         WRITE32(nfs4_lock_type(args->fl, 0));
810         WRITE64(args->fl->fl_start);
811         WRITE64(nfs4_lock_length(args->fl));
812         WRITE64(args->lock_owner.clientid);
813         WRITE32(4);
814         WRITE32(args->lock_owner.id);
815
816         return 0;
817 }
818
819 static int encode_locku(struct xdr_stream *xdr, const struct nfs_locku_args *args)
820 {
821         uint32_t *p;
822
823         RESERVE_SPACE(44);
824         WRITE32(OP_LOCKU);
825         WRITE32(nfs4_lock_type(args->fl, 0));
826         WRITE32(args->seqid->sequence->counter);
827         WRITEMEM(args->stateid->data, sizeof(args->stateid->data));
828         WRITE64(args->fl->fl_start);
829         WRITE64(nfs4_lock_length(args->fl));
830
831         return 0;
832 }
833
834 static int encode_lookup(struct xdr_stream *xdr, const struct qstr *name)
835 {
836         int len = name->len;
837         uint32_t *p;
838
839         RESERVE_SPACE(8 + len);
840         WRITE32(OP_LOOKUP);
841         WRITE32(len);
842         WRITEMEM(name->name, len);
843
844         return 0;
845 }
846
847 static void encode_share_access(struct xdr_stream *xdr, int open_flags)
848 {
849         uint32_t *p;
850
851         RESERVE_SPACE(8);
852         switch (open_flags & (FMODE_READ|FMODE_WRITE)) {
853                 case FMODE_READ:
854                         WRITE32(NFS4_SHARE_ACCESS_READ);
855                         break;
856                 case FMODE_WRITE:
857                         WRITE32(NFS4_SHARE_ACCESS_WRITE);
858                         break;
859                 case FMODE_READ|FMODE_WRITE:
860                         WRITE32(NFS4_SHARE_ACCESS_BOTH);
861                         break;
862                 default:
863                         BUG();
864         }
865         WRITE32(0);             /* for linux, share_deny = 0 always */
866 }
867
868 static inline void encode_openhdr(struct xdr_stream *xdr, const struct nfs_openargs *arg)
869 {
870         uint32_t *p;
871  /*
872  * opcode 4, seqid 4, share_access 4, share_deny 4, clientid 8, ownerlen 4,
873  * owner 4 = 32
874  */
875         RESERVE_SPACE(8);
876         WRITE32(OP_OPEN);
877         WRITE32(arg->seqid->sequence->counter);
878         encode_share_access(xdr, arg->open_flags);
879         RESERVE_SPACE(16);
880         WRITE64(arg->clientid);
881         WRITE32(4);
882         WRITE32(arg->id);
883 }
884
885 static inline void encode_createmode(struct xdr_stream *xdr, const struct nfs_openargs *arg)
886 {
887         uint32_t *p;
888
889         RESERVE_SPACE(4);
890         switch(arg->open_flags & O_EXCL) {
891                 case 0:
892                         WRITE32(NFS4_CREATE_UNCHECKED);
893                         encode_attrs(xdr, arg->u.attrs, arg->server);
894                         break;
895                 default:
896                         WRITE32(NFS4_CREATE_EXCLUSIVE);
897                         encode_nfs4_verifier(xdr, &arg->u.verifier);
898         }
899 }
900
901 static void encode_opentype(struct xdr_stream *xdr, const struct nfs_openargs *arg)
902 {
903         uint32_t *p;
904
905         RESERVE_SPACE(4);
906         switch (arg->open_flags & O_CREAT) {
907                 case 0:
908                         WRITE32(NFS4_OPEN_NOCREATE);
909                         break;
910                 default:
911                         BUG_ON(arg->claim != NFS4_OPEN_CLAIM_NULL);
912                         WRITE32(NFS4_OPEN_CREATE);
913                         encode_createmode(xdr, arg);
914         }
915 }
916
917 static inline void encode_delegation_type(struct xdr_stream *xdr, int delegation_type)
918 {
919         uint32_t *p;
920
921         RESERVE_SPACE(4);
922         switch (delegation_type) {
923                 case 0:
924                         WRITE32(NFS4_OPEN_DELEGATE_NONE);
925                         break;
926                 case FMODE_READ:
927                         WRITE32(NFS4_OPEN_DELEGATE_READ);
928                         break;
929                 case FMODE_WRITE|FMODE_READ:
930                         WRITE32(NFS4_OPEN_DELEGATE_WRITE);
931                         break;
932                 default:
933                         BUG();
934         }
935 }
936
937 static inline void encode_claim_null(struct xdr_stream *xdr, const struct qstr *name)
938 {
939         uint32_t *p;
940
941         RESERVE_SPACE(4);
942         WRITE32(NFS4_OPEN_CLAIM_NULL);
943         encode_string(xdr, name->len, name->name);
944 }
945
946 static inline void encode_claim_previous(struct xdr_stream *xdr, int type)
947 {
948         uint32_t *p;
949
950         RESERVE_SPACE(4);
951         WRITE32(NFS4_OPEN_CLAIM_PREVIOUS);
952         encode_delegation_type(xdr, type);
953 }
954
955 static inline void encode_claim_delegate_cur(struct xdr_stream *xdr, const struct qstr *name, const nfs4_stateid *stateid)
956 {
957         uint32_t *p;
958
959         RESERVE_SPACE(4+sizeof(stateid->data));
960         WRITE32(NFS4_OPEN_CLAIM_DELEGATE_CUR);
961         WRITEMEM(stateid->data, sizeof(stateid->data));
962         encode_string(xdr, name->len, name->name);
963 }
964
965 static int encode_open(struct xdr_stream *xdr, const struct nfs_openargs *arg)
966 {
967         encode_openhdr(xdr, arg);
968         encode_opentype(xdr, arg);
969         switch (arg->claim) {
970                 case NFS4_OPEN_CLAIM_NULL:
971                         encode_claim_null(xdr, arg->name);
972                         break;
973                 case NFS4_OPEN_CLAIM_PREVIOUS:
974                         encode_claim_previous(xdr, arg->u.delegation_type);
975                         break;
976                 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
977                         encode_claim_delegate_cur(xdr, arg->name, &arg->u.delegation);
978                         break;
979                 default:
980                         BUG();
981         }
982         return 0;
983 }
984
985 static int encode_open_confirm(struct xdr_stream *xdr, const struct nfs_open_confirmargs *arg)
986 {
987         uint32_t *p;
988
989         RESERVE_SPACE(8+sizeof(arg->stateid->data));
990         WRITE32(OP_OPEN_CONFIRM);
991         WRITEMEM(arg->stateid->data, sizeof(arg->stateid->data));
992         WRITE32(arg->seqid->sequence->counter);
993
994         return 0;
995 }
996
997 static int encode_open_downgrade(struct xdr_stream *xdr, const struct nfs_closeargs *arg)
998 {
999         uint32_t *p;
1000
1001         RESERVE_SPACE(8+sizeof(arg->stateid->data));
1002         WRITE32(OP_OPEN_DOWNGRADE);
1003         WRITEMEM(arg->stateid->data, sizeof(arg->stateid->data));
1004         WRITE32(arg->seqid->sequence->counter);
1005         encode_share_access(xdr, arg->open_flags);
1006         return 0;
1007 }
1008
1009 static int
1010 encode_putfh(struct xdr_stream *xdr, const struct nfs_fh *fh)
1011 {
1012         int len = fh->size;
1013         uint32_t *p;
1014
1015         RESERVE_SPACE(8 + len);
1016         WRITE32(OP_PUTFH);
1017         WRITE32(len);
1018         WRITEMEM(fh->data, len);
1019
1020         return 0;
1021 }
1022
1023 static int encode_putrootfh(struct xdr_stream *xdr)
1024 {
1025         uint32_t *p;
1026         
1027         RESERVE_SPACE(4);
1028         WRITE32(OP_PUTROOTFH);
1029
1030         return 0;
1031 }
1032
1033 static void encode_stateid(struct xdr_stream *xdr, const struct nfs_open_context *ctx)
1034 {
1035         nfs4_stateid stateid;
1036         uint32_t *p;
1037
1038         RESERVE_SPACE(16);
1039         if (ctx->state != NULL) {
1040                 nfs4_copy_stateid(&stateid, ctx->state, ctx->lockowner);
1041                 WRITEMEM(stateid.data, sizeof(stateid.data));
1042         } else
1043                 WRITEMEM(zero_stateid.data, sizeof(zero_stateid.data));
1044 }
1045
1046 static int encode_read(struct xdr_stream *xdr, const struct nfs_readargs *args)
1047 {
1048         uint32_t *p;
1049
1050         RESERVE_SPACE(4);
1051         WRITE32(OP_READ);
1052
1053         encode_stateid(xdr, args->context);
1054
1055         RESERVE_SPACE(12);
1056         WRITE64(args->offset);
1057         WRITE32(args->count);
1058
1059         return 0;
1060 }
1061
1062 static int encode_readdir(struct xdr_stream *xdr, const struct nfs4_readdir_arg *readdir, struct rpc_rqst *req)
1063 {
1064         struct rpc_auth *auth = req->rq_task->tk_auth;
1065         uint32_t attrs[2] = {
1066                 FATTR4_WORD0_RDATTR_ERROR|FATTR4_WORD0_FILEID,
1067                 FATTR4_WORD1_MOUNTED_ON_FILEID,
1068         };
1069         int replen;
1070         uint32_t *p;
1071
1072         RESERVE_SPACE(32+sizeof(nfs4_verifier));
1073         WRITE32(OP_READDIR);
1074         WRITE64(readdir->cookie);
1075         WRITEMEM(readdir->verifier.data, sizeof(readdir->verifier.data));
1076         WRITE32(readdir->count >> 1);  /* We're not doing readdirplus */
1077         WRITE32(readdir->count);
1078         WRITE32(2);
1079         /* Switch to mounted_on_fileid if the server supports it */
1080         if (readdir->bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
1081                 attrs[0] &= ~FATTR4_WORD0_FILEID;
1082         else
1083                 attrs[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
1084         WRITE32(attrs[0] & readdir->bitmask[0]);
1085         WRITE32(attrs[1] & readdir->bitmask[1]);
1086         dprintk("%s: cookie = %Lu, verifier = 0x%x%x, bitmap = 0x%x%x\n",
1087                         __FUNCTION__,
1088                         (unsigned long long)readdir->cookie,
1089                         ((u32 *)readdir->verifier.data)[0],
1090                         ((u32 *)readdir->verifier.data)[1],
1091                         attrs[0] & readdir->bitmask[0],
1092                         attrs[1] & readdir->bitmask[1]);
1093
1094         /* set up reply kvec
1095          *    toplevel_status + taglen + rescount + OP_PUTFH + status
1096          *      + OP_READDIR + status + verifer(2)  = 9
1097          */
1098         replen = (RPC_REPHDRSIZE + auth->au_rslack + 9) << 2;
1099         xdr_inline_pages(&req->rq_rcv_buf, replen, readdir->pages,
1100                          readdir->pgbase, readdir->count);
1101         dprintk("%s: inlined page args = (%u, %p, %u, %u)\n",
1102                         __FUNCTION__, replen, readdir->pages,
1103                         readdir->pgbase, readdir->count);
1104
1105         return 0;
1106 }
1107
1108 static int encode_readlink(struct xdr_stream *xdr, const struct nfs4_readlink *readlink, struct rpc_rqst *req)
1109 {
1110         struct rpc_auth *auth = req->rq_task->tk_auth;
1111         unsigned int replen;
1112         uint32_t *p;
1113
1114         RESERVE_SPACE(4);
1115         WRITE32(OP_READLINK);
1116
1117         /* set up reply kvec
1118          *    toplevel_status + taglen + rescount + OP_PUTFH + status
1119          *      + OP_READLINK + status + string length = 8
1120          */
1121         replen = (RPC_REPHDRSIZE + auth->au_rslack + 8) << 2;
1122         xdr_inline_pages(&req->rq_rcv_buf, replen, readlink->pages,
1123                         readlink->pgbase, readlink->pglen);
1124         
1125         return 0;
1126 }
1127
1128 static int encode_remove(struct xdr_stream *xdr, const struct qstr *name)
1129 {
1130         uint32_t *p;
1131
1132         RESERVE_SPACE(8 + name->len);
1133         WRITE32(OP_REMOVE);
1134         WRITE32(name->len);
1135         WRITEMEM(name->name, name->len);
1136
1137         return 0;
1138 }
1139
1140 static int encode_rename(struct xdr_stream *xdr, const struct qstr *oldname, const struct qstr *newname)
1141 {
1142         uint32_t *p;
1143
1144         RESERVE_SPACE(8 + oldname->len);
1145         WRITE32(OP_RENAME);
1146         WRITE32(oldname->len);
1147         WRITEMEM(oldname->name, oldname->len);
1148         
1149         RESERVE_SPACE(4 + newname->len);
1150         WRITE32(newname->len);
1151         WRITEMEM(newname->name, newname->len);
1152
1153         return 0;
1154 }
1155
1156 static int encode_renew(struct xdr_stream *xdr, const struct nfs4_client *client_stateid)
1157 {
1158         uint32_t *p;
1159
1160         RESERVE_SPACE(12);
1161         WRITE32(OP_RENEW);
1162         WRITE64(client_stateid->cl_clientid);
1163
1164         return 0;
1165 }
1166
1167 static int
1168 encode_restorefh(struct xdr_stream *xdr)
1169 {
1170         uint32_t *p;
1171
1172         RESERVE_SPACE(4);
1173         WRITE32(OP_RESTOREFH);
1174
1175         return 0;
1176 }
1177
1178 static int
1179 encode_setacl(struct xdr_stream *xdr, struct nfs_setaclargs *arg)
1180 {
1181         uint32_t *p;
1182
1183         RESERVE_SPACE(4+sizeof(zero_stateid.data));
1184         WRITE32(OP_SETATTR);
1185         WRITEMEM(zero_stateid.data, sizeof(zero_stateid.data));
1186         RESERVE_SPACE(2*4);
1187         WRITE32(1);
1188         WRITE32(FATTR4_WORD0_ACL);
1189         if (arg->acl_len % 4)
1190                 return -EINVAL;
1191         RESERVE_SPACE(4);
1192         WRITE32(arg->acl_len);
1193         xdr_write_pages(xdr, arg->acl_pages, arg->acl_pgbase, arg->acl_len);
1194         return 0;
1195 }
1196
1197 static int
1198 encode_savefh(struct xdr_stream *xdr)
1199 {
1200         uint32_t *p;
1201
1202         RESERVE_SPACE(4);
1203         WRITE32(OP_SAVEFH);
1204
1205         return 0;
1206 }
1207
1208 static int encode_setattr(struct xdr_stream *xdr, const struct nfs_setattrargs *arg, const struct nfs_server *server)
1209 {
1210         int status;
1211         uint32_t *p;
1212         
1213         RESERVE_SPACE(4+sizeof(arg->stateid.data));
1214         WRITE32(OP_SETATTR);
1215         WRITEMEM(arg->stateid.data, sizeof(arg->stateid.data));
1216
1217         if ((status = encode_attrs(xdr, arg->iap, server)))
1218                 return status;
1219
1220         return 0;
1221 }
1222
1223 static int encode_setclientid(struct xdr_stream *xdr, const struct nfs4_setclientid *setclientid)
1224 {
1225         uint32_t *p;
1226
1227         RESERVE_SPACE(4 + sizeof(setclientid->sc_verifier->data));
1228         WRITE32(OP_SETCLIENTID);
1229         WRITEMEM(setclientid->sc_verifier->data, sizeof(setclientid->sc_verifier->data));
1230
1231         encode_string(xdr, setclientid->sc_name_len, setclientid->sc_name);
1232         RESERVE_SPACE(4);
1233         WRITE32(setclientid->sc_prog);
1234         encode_string(xdr, setclientid->sc_netid_len, setclientid->sc_netid);
1235         encode_string(xdr, setclientid->sc_uaddr_len, setclientid->sc_uaddr);
1236         RESERVE_SPACE(4);
1237         WRITE32(setclientid->sc_cb_ident);
1238
1239         return 0;
1240 }
1241
1242 static int encode_setclientid_confirm(struct xdr_stream *xdr, const struct nfs4_client *client_state)
1243 {
1244         uint32_t *p;
1245
1246         RESERVE_SPACE(12 + sizeof(client_state->cl_confirm.data));
1247         WRITE32(OP_SETCLIENTID_CONFIRM);
1248         WRITE64(client_state->cl_clientid);
1249         WRITEMEM(client_state->cl_confirm.data, sizeof(client_state->cl_confirm.data));
1250
1251         return 0;
1252 }
1253
1254 static int encode_write(struct xdr_stream *xdr, const struct nfs_writeargs *args)
1255 {
1256         uint32_t *p;
1257
1258         RESERVE_SPACE(4);
1259         WRITE32(OP_WRITE);
1260
1261         encode_stateid(xdr, args->context);
1262
1263         RESERVE_SPACE(16);
1264         WRITE64(args->offset);
1265         WRITE32(args->stable);
1266         WRITE32(args->count);
1267
1268         xdr_write_pages(xdr, args->pages, args->pgbase, args->count);
1269
1270         return 0;
1271 }
1272
1273 static int encode_delegreturn(struct xdr_stream *xdr, const nfs4_stateid *stateid)
1274 {
1275         uint32_t *p;
1276
1277         RESERVE_SPACE(20);
1278
1279         WRITE32(OP_DELEGRETURN);
1280         WRITEMEM(stateid->data, sizeof(stateid->data));
1281         return 0;
1282
1283 }
1284 /*
1285  * END OF "GENERIC" ENCODE ROUTINES.
1286  */
1287
1288 /*
1289  * Encode an ACCESS request
1290  */
1291 static int nfs4_xdr_enc_access(struct rpc_rqst *req, uint32_t *p, const struct nfs4_accessargs *args)
1292 {
1293         struct xdr_stream xdr;
1294         struct compound_hdr hdr = {
1295                 .nops = 2,
1296         };
1297         int status;
1298
1299         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1300         encode_compound_hdr(&xdr, &hdr);
1301         if ((status = encode_putfh(&xdr, args->fh)) == 0)
1302                 status = encode_access(&xdr, args->access);
1303         return status;
1304 }
1305
1306 /*
1307  * Encode LOOKUP request
1308  */
1309 static int nfs4_xdr_enc_lookup(struct rpc_rqst *req, uint32_t *p, const struct nfs4_lookup_arg *args)
1310 {
1311         struct xdr_stream xdr;
1312         struct compound_hdr hdr = {
1313                 .nops = 4,
1314         };
1315         int status;
1316
1317         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1318         encode_compound_hdr(&xdr, &hdr);
1319         if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1320                 goto out;
1321         if ((status = encode_lookup(&xdr, args->name)) != 0)
1322                 goto out;
1323         if ((status = encode_getfh(&xdr)) != 0)
1324                 goto out;
1325         status = encode_getfattr(&xdr, args->bitmask);
1326 out:
1327         return status;
1328 }
1329
1330 /*
1331  * Encode LOOKUP_ROOT request
1332  */
1333 static int nfs4_xdr_enc_lookup_root(struct rpc_rqst *req, uint32_t *p, const struct nfs4_lookup_root_arg *args)
1334 {
1335         struct xdr_stream xdr;
1336         struct compound_hdr hdr = {
1337                 .nops = 3,
1338         };
1339         int status;
1340
1341         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1342         encode_compound_hdr(&xdr, &hdr);
1343         if ((status = encode_putrootfh(&xdr)) != 0)
1344                 goto out;
1345         if ((status = encode_getfh(&xdr)) == 0)
1346                 status = encode_getfattr(&xdr, args->bitmask);
1347 out:
1348         return status;
1349 }
1350
1351 /*
1352  * Encode REMOVE request
1353  */
1354 static int nfs4_xdr_enc_remove(struct rpc_rqst *req, uint32_t *p, const struct nfs4_remove_arg *args)
1355 {
1356         struct xdr_stream xdr;
1357         struct compound_hdr hdr = {
1358                 .nops = 3,
1359         };
1360         int status;
1361
1362         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1363         encode_compound_hdr(&xdr, &hdr);
1364         if ((status = encode_putfh(&xdr, args->fh)) != 0)
1365                 goto out;
1366         if ((status = encode_remove(&xdr, args->name)) != 0)
1367                 goto out;
1368         status = encode_getfattr(&xdr, args->bitmask);
1369 out:
1370         return status;
1371 }
1372
1373 /*
1374  * Encode RENAME request
1375  */
1376 static int nfs4_xdr_enc_rename(struct rpc_rqst *req, uint32_t *p, const struct nfs4_rename_arg *args)
1377 {
1378         struct xdr_stream xdr;
1379         struct compound_hdr hdr = {
1380                 .nops = 7,
1381         };
1382         int status;
1383
1384         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1385         encode_compound_hdr(&xdr, &hdr);
1386         if ((status = encode_putfh(&xdr, args->old_dir)) != 0)
1387                 goto out;
1388         if ((status = encode_savefh(&xdr)) != 0)
1389                 goto out;
1390         if ((status = encode_putfh(&xdr, args->new_dir)) != 0)
1391                 goto out;
1392         if ((status = encode_rename(&xdr, args->old_name, args->new_name)) != 0)
1393                 goto out;
1394         if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
1395                 goto out;
1396         if ((status = encode_restorefh(&xdr)) != 0)
1397                 goto out;
1398         status = encode_getfattr(&xdr, args->bitmask);
1399 out:
1400         return status;
1401 }
1402
1403 /*
1404  * Encode LINK request
1405  */
1406 static int nfs4_xdr_enc_link(struct rpc_rqst *req, uint32_t *p, const struct nfs4_link_arg *args)
1407 {
1408         struct xdr_stream xdr;
1409         struct compound_hdr hdr = {
1410                 .nops = 7,
1411         };
1412         int status;
1413
1414         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1415         encode_compound_hdr(&xdr, &hdr);
1416         if ((status = encode_putfh(&xdr, args->fh)) != 0)
1417                 goto out;
1418         if ((status = encode_savefh(&xdr)) != 0)
1419                 goto out;
1420         if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1421                 goto out;
1422         if ((status = encode_link(&xdr, args->name)) != 0)
1423                 goto out;
1424         if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
1425                 goto out;
1426         if ((status = encode_restorefh(&xdr)) != 0)
1427                 goto out;
1428         status = encode_getfattr(&xdr, args->bitmask);
1429 out:
1430         return status;
1431 }
1432
1433 /*
1434  * Encode CREATE request
1435  */
1436 static int nfs4_xdr_enc_create(struct rpc_rqst *req, uint32_t *p, const struct nfs4_create_arg *args)
1437 {
1438         struct xdr_stream xdr;
1439         struct compound_hdr hdr = {
1440                 .nops = 7,
1441         };
1442         int status;
1443
1444         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1445         encode_compound_hdr(&xdr, &hdr);
1446         if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1447                 goto out;
1448         if ((status = encode_savefh(&xdr)) != 0)
1449                 goto out;
1450         if ((status = encode_create(&xdr, args)) != 0)
1451                 goto out;
1452         if ((status = encode_getfh(&xdr)) != 0)
1453                 goto out;
1454         if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
1455                 goto out;
1456         if ((status = encode_restorefh(&xdr)) != 0)
1457                 goto out;
1458         status = encode_getfattr(&xdr, args->bitmask);
1459 out:
1460         return status;
1461 }
1462
1463 /*
1464  * Encode SYMLINK request
1465  */
1466 static int nfs4_xdr_enc_symlink(struct rpc_rqst *req, uint32_t *p, const struct nfs4_create_arg *args)
1467 {
1468         return nfs4_xdr_enc_create(req, p, args);
1469 }
1470
1471 /*
1472  * Encode GETATTR request
1473  */
1474 static int nfs4_xdr_enc_getattr(struct rpc_rqst *req, uint32_t *p, const struct nfs4_getattr_arg *args)
1475 {
1476         struct xdr_stream xdr;
1477         struct compound_hdr hdr = {
1478                 .nops = 2,
1479         };
1480         int status;
1481
1482         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1483         encode_compound_hdr(&xdr, &hdr);
1484         if ((status = encode_putfh(&xdr, args->fh)) == 0)
1485                 status = encode_getfattr(&xdr, args->bitmask);
1486         return status;
1487 }
1488
1489 /*
1490  * Encode a CLOSE request
1491  */
1492 static int nfs4_xdr_enc_close(struct rpc_rqst *req, uint32_t *p, struct nfs_closeargs *args)
1493 {
1494         struct xdr_stream xdr;
1495         struct compound_hdr hdr = {
1496                 .nops   = 3,
1497         };
1498         int status;
1499
1500         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1501         encode_compound_hdr(&xdr, &hdr);
1502         status = encode_putfh(&xdr, args->fh);
1503         if(status)
1504                 goto out;
1505         status = encode_close(&xdr, args);
1506         if (status != 0)
1507                 goto out;
1508         status = encode_getfattr(&xdr, args->bitmask);
1509 out:
1510         return status;
1511 }
1512
1513 /*
1514  * Encode an OPEN request
1515  */
1516 static int nfs4_xdr_enc_open(struct rpc_rqst *req, uint32_t *p, struct nfs_openargs *args)
1517 {
1518         struct xdr_stream xdr;
1519         struct compound_hdr hdr = {
1520                 .nops = 7,
1521         };
1522         int status;
1523
1524         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1525         encode_compound_hdr(&xdr, &hdr);
1526         status = encode_putfh(&xdr, args->fh);
1527         if (status)
1528                 goto out;
1529         status = encode_savefh(&xdr);
1530         if (status)
1531                 goto out;
1532         status = encode_open(&xdr, args);
1533         if (status)
1534                 goto out;
1535         status = encode_getfh(&xdr);
1536         if (status)
1537                 goto out;
1538         status = encode_getfattr(&xdr, args->bitmask);
1539         if (status)
1540                 goto out;
1541         status = encode_restorefh(&xdr);
1542         if (status)
1543                 goto out;
1544         status = encode_getfattr(&xdr, args->bitmask);
1545 out:
1546         return status;
1547 }
1548
1549 /*
1550  * Encode an OPEN_CONFIRM request
1551  */
1552 static int nfs4_xdr_enc_open_confirm(struct rpc_rqst *req, uint32_t *p, struct nfs_open_confirmargs *args)
1553 {
1554         struct xdr_stream xdr;
1555         struct compound_hdr hdr = {
1556                 .nops   = 2,
1557         };
1558         int status;
1559
1560         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1561         encode_compound_hdr(&xdr, &hdr);
1562         status = encode_putfh(&xdr, args->fh);
1563         if(status)
1564                 goto out;
1565         status = encode_open_confirm(&xdr, args);
1566 out:
1567         return status;
1568 }
1569
1570 /*
1571  * Encode an OPEN request with no attributes.
1572  */
1573 static int nfs4_xdr_enc_open_noattr(struct rpc_rqst *req, uint32_t *p, struct nfs_openargs *args)
1574 {
1575         struct xdr_stream xdr;
1576         struct compound_hdr hdr = {
1577                 .nops   = 3,
1578         };
1579         int status;
1580
1581         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1582         encode_compound_hdr(&xdr, &hdr);
1583         status = encode_putfh(&xdr, args->fh);
1584         if (status)
1585                 goto out;
1586         status = encode_open(&xdr, args);
1587         if (status)
1588                 goto out;
1589         status = encode_getfattr(&xdr, args->bitmask);
1590 out:
1591         return status;
1592 }
1593
1594 /*
1595  * Encode an OPEN_DOWNGRADE request
1596  */
1597 static int nfs4_xdr_enc_open_downgrade(struct rpc_rqst *req, uint32_t *p, struct nfs_closeargs *args)
1598 {
1599         struct xdr_stream xdr;
1600         struct compound_hdr hdr = {
1601                 .nops   = 3,
1602         };
1603         int status;
1604
1605         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1606         encode_compound_hdr(&xdr, &hdr);
1607         status = encode_putfh(&xdr, args->fh);
1608         if (status)
1609                 goto out;
1610         status = encode_open_downgrade(&xdr, args);
1611         if (status != 0)
1612                 goto out;
1613         status = encode_getfattr(&xdr, args->bitmask);
1614 out:
1615         return status;
1616 }
1617
1618 /*
1619  * Encode a LOCK request
1620  */
1621 static int nfs4_xdr_enc_lock(struct rpc_rqst *req, uint32_t *p, struct nfs_lock_args *args)
1622 {
1623         struct xdr_stream xdr;
1624         struct compound_hdr hdr = {
1625                 .nops   = 2,
1626         };
1627         int status;
1628
1629         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1630         encode_compound_hdr(&xdr, &hdr);
1631         status = encode_putfh(&xdr, args->fh);
1632         if(status)
1633                 goto out;
1634         status = encode_lock(&xdr, args);
1635 out:
1636         return status;
1637 }
1638
1639 /*
1640  * Encode a LOCKT request
1641  */
1642 static int nfs4_xdr_enc_lockt(struct rpc_rqst *req, uint32_t *p, struct nfs_lockt_args *args)
1643 {
1644         struct xdr_stream xdr;
1645         struct compound_hdr hdr = {
1646                 .nops   = 2,
1647         };
1648         int status;
1649
1650         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1651         encode_compound_hdr(&xdr, &hdr);
1652         status = encode_putfh(&xdr, args->fh);
1653         if(status)
1654                 goto out;
1655         status = encode_lockt(&xdr, args);
1656 out:
1657         return status;
1658 }
1659
1660 /*
1661  * Encode a LOCKU request
1662  */
1663 static int nfs4_xdr_enc_locku(struct rpc_rqst *req, uint32_t *p, struct nfs_locku_args *args)
1664 {
1665         struct xdr_stream xdr;
1666         struct compound_hdr hdr = {
1667                 .nops   = 2,
1668         };
1669         int status;
1670
1671         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1672         encode_compound_hdr(&xdr, &hdr);
1673         status = encode_putfh(&xdr, args->fh);
1674         if(status)
1675                 goto out;
1676         status = encode_locku(&xdr, args);
1677 out:
1678         return status;
1679 }
1680
1681 /*
1682  * Encode a READLINK request
1683  */
1684 static int nfs4_xdr_enc_readlink(struct rpc_rqst *req, uint32_t *p, const struct nfs4_readlink *args)
1685 {
1686         struct xdr_stream xdr;
1687         struct compound_hdr hdr = {
1688                 .nops = 2,
1689         };
1690         int status;
1691
1692         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1693         encode_compound_hdr(&xdr, &hdr);
1694         status = encode_putfh(&xdr, args->fh);
1695         if(status)
1696                 goto out;
1697         status = encode_readlink(&xdr, args, req);
1698 out:
1699         return status;
1700 }
1701
1702 /*
1703  * Encode a READDIR request
1704  */
1705 static int nfs4_xdr_enc_readdir(struct rpc_rqst *req, uint32_t *p, const struct nfs4_readdir_arg *args)
1706 {
1707         struct xdr_stream xdr;
1708         struct compound_hdr hdr = {
1709                 .nops = 2,
1710         };
1711         int status;
1712
1713         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1714         encode_compound_hdr(&xdr, &hdr);
1715         status = encode_putfh(&xdr, args->fh);
1716         if(status)
1717                 goto out;
1718         status = encode_readdir(&xdr, args, req);
1719 out:
1720         return status;
1721 }
1722
1723 /*
1724  * Encode a READ request
1725  */
1726 static int nfs4_xdr_enc_read(struct rpc_rqst *req, uint32_t *p, struct nfs_readargs *args)
1727 {
1728         struct rpc_auth *auth = req->rq_task->tk_auth;
1729         struct xdr_stream xdr;
1730         struct compound_hdr hdr = {
1731                 .nops = 2,
1732         };
1733         int replen, status;
1734
1735         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1736         encode_compound_hdr(&xdr, &hdr);
1737         status = encode_putfh(&xdr, args->fh);
1738         if (status)
1739                 goto out;
1740         status = encode_read(&xdr, args);
1741         if (status)
1742                 goto out;
1743
1744         /* set up reply kvec
1745          *    toplevel status + taglen=0 + rescount + OP_PUTFH + status
1746          *       + OP_READ + status + eof + datalen = 9
1747          */
1748         replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_read_sz) << 2;
1749         xdr_inline_pages(&req->rq_rcv_buf, replen,
1750                          args->pages, args->pgbase, args->count);
1751 out:
1752         return status;
1753 }
1754
1755 /*
1756  * Encode an SETATTR request
1757  */
1758 static int nfs4_xdr_enc_setattr(struct rpc_rqst *req, uint32_t *p, struct nfs_setattrargs *args)
1759
1760 {
1761         struct xdr_stream xdr;
1762         struct compound_hdr hdr = {
1763                 .nops   = 3,
1764         };
1765         int status;
1766
1767         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1768         encode_compound_hdr(&xdr, &hdr);
1769         status = encode_putfh(&xdr, args->fh);
1770         if(status)
1771                 goto out;
1772         status = encode_setattr(&xdr, args, args->server);
1773         if(status)
1774                 goto out;
1775         status = encode_getfattr(&xdr, args->bitmask);
1776 out:
1777         return status;
1778 }
1779
1780 /*
1781  * Encode a GETACL request
1782  */
1783 static int
1784 nfs4_xdr_enc_getacl(struct rpc_rqst *req, uint32_t *p,
1785                 struct nfs_getaclargs *args)
1786 {
1787         struct xdr_stream xdr;
1788         struct rpc_auth *auth = req->rq_task->tk_auth;
1789         struct compound_hdr hdr = {
1790                 .nops   = 2,
1791         };
1792         int replen, status;
1793
1794         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1795         encode_compound_hdr(&xdr, &hdr);
1796         status = encode_putfh(&xdr, args->fh);
1797         if (status)
1798                 goto out;
1799         status = encode_getattr_two(&xdr, FATTR4_WORD0_ACL, 0);
1800         /* set up reply buffer: */
1801         replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_getacl_sz) << 2;
1802         xdr_inline_pages(&req->rq_rcv_buf, replen,
1803                 args->acl_pages, args->acl_pgbase, args->acl_len);
1804 out:
1805         return status;
1806 }
1807
1808 /*
1809  * Encode a WRITE request
1810  */
1811 static int nfs4_xdr_enc_write(struct rpc_rqst *req, uint32_t *p, struct nfs_writeargs *args)
1812 {
1813         struct xdr_stream xdr;
1814         struct compound_hdr hdr = {
1815                 .nops = 3,
1816         };
1817         int status;
1818
1819         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1820         encode_compound_hdr(&xdr, &hdr);
1821         status = encode_putfh(&xdr, args->fh);
1822         if (status)
1823                 goto out;
1824         status = encode_write(&xdr, args);
1825         if (status)
1826                 goto out;
1827         status = encode_getfattr(&xdr, args->bitmask);
1828 out:
1829         return status;
1830 }
1831
1832 /*
1833  *  a COMMIT request
1834  */
1835 static int nfs4_xdr_enc_commit(struct rpc_rqst *req, uint32_t *p, struct nfs_writeargs *args)
1836 {
1837         struct xdr_stream xdr;
1838         struct compound_hdr hdr = {
1839                 .nops = 3,
1840         };
1841         int status;
1842
1843         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1844         encode_compound_hdr(&xdr, &hdr);
1845         status = encode_putfh(&xdr, args->fh);
1846         if (status)
1847                 goto out;
1848         status = encode_commit(&xdr, args);
1849         if (status)
1850                 goto out;
1851         status = encode_getfattr(&xdr, args->bitmask);
1852 out:
1853         return status;
1854 }
1855
1856 /*
1857  * FSINFO request
1858  */
1859 static int nfs4_xdr_enc_fsinfo(struct rpc_rqst *req, uint32_t *p, struct nfs4_fsinfo_arg *args)
1860 {
1861         struct xdr_stream xdr;
1862         struct compound_hdr hdr = {
1863                 .nops   = 2,
1864         };
1865         int status;
1866
1867         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1868         encode_compound_hdr(&xdr, &hdr);
1869         status = encode_putfh(&xdr, args->fh);
1870         if (!status)
1871                 status = encode_fsinfo(&xdr, args->bitmask);
1872         return status;
1873 }
1874
1875 /*
1876  * a PATHCONF request
1877  */
1878 static int nfs4_xdr_enc_pathconf(struct rpc_rqst *req, uint32_t *p, const struct nfs4_pathconf_arg *args)
1879 {
1880         struct xdr_stream xdr;
1881         struct compound_hdr hdr = {
1882                 .nops = 2,
1883         };
1884         int status;
1885
1886         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1887         encode_compound_hdr(&xdr, &hdr);
1888         status = encode_putfh(&xdr, args->fh);
1889         if (!status)
1890                 status = encode_getattr_one(&xdr,
1891                                 args->bitmask[0] & nfs4_pathconf_bitmap[0]);
1892         return status;
1893 }
1894
1895 /*
1896  * a STATFS request
1897  */
1898 static int nfs4_xdr_enc_statfs(struct rpc_rqst *req, uint32_t *p, const struct nfs4_statfs_arg *args)
1899 {
1900         struct xdr_stream xdr;
1901         struct compound_hdr hdr = {
1902                 .nops = 2,
1903         };
1904         int status;
1905
1906         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1907         encode_compound_hdr(&xdr, &hdr);
1908         status = encode_putfh(&xdr, args->fh);
1909         if (status == 0)
1910                 status = encode_getattr_two(&xdr,
1911                                 args->bitmask[0] & nfs4_statfs_bitmap[0],
1912                                 args->bitmask[1] & nfs4_statfs_bitmap[1]);
1913         return status;
1914 }
1915
1916 /*
1917  * GETATTR_BITMAP request
1918  */
1919 static int nfs4_xdr_enc_server_caps(struct rpc_rqst *req, uint32_t *p, const struct nfs_fh *fhandle)
1920 {
1921         struct xdr_stream xdr;
1922         struct compound_hdr hdr = {
1923                 .nops = 2,
1924         };
1925         int status;
1926
1927         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1928         encode_compound_hdr(&xdr, &hdr);
1929         status = encode_putfh(&xdr, fhandle);
1930         if (status == 0)
1931                 status = encode_getattr_one(&xdr, FATTR4_WORD0_SUPPORTED_ATTRS|
1932                                 FATTR4_WORD0_LINK_SUPPORT|
1933                                 FATTR4_WORD0_SYMLINK_SUPPORT|
1934                                 FATTR4_WORD0_ACLSUPPORT);
1935         return status;
1936 }
1937
1938 /*
1939  * a RENEW request
1940  */
1941 static int nfs4_xdr_enc_renew(struct rpc_rqst *req, uint32_t *p, struct nfs4_client *clp)
1942 {
1943         struct xdr_stream xdr;
1944         struct compound_hdr hdr = {
1945                 .nops   = 1,
1946         };
1947
1948         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1949         encode_compound_hdr(&xdr, &hdr);
1950         return encode_renew(&xdr, clp);
1951 }
1952
1953 /*
1954  * a SETCLIENTID request
1955  */
1956 static int nfs4_xdr_enc_setclientid(struct rpc_rqst *req, uint32_t *p, struct nfs4_setclientid *sc)
1957 {
1958         struct xdr_stream xdr;
1959         struct compound_hdr hdr = {
1960                 .nops   = 1,
1961         };
1962
1963         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1964         encode_compound_hdr(&xdr, &hdr);
1965         return encode_setclientid(&xdr, sc);
1966 }
1967
1968 /*
1969  * a SETCLIENTID_CONFIRM request
1970  */
1971 static int nfs4_xdr_enc_setclientid_confirm(struct rpc_rqst *req, uint32_t *p, struct nfs4_client *clp)
1972 {
1973         struct xdr_stream xdr;
1974         struct compound_hdr hdr = {
1975                 .nops   = 3,
1976         };
1977         const u32 lease_bitmap[2] = { FATTR4_WORD0_LEASE_TIME, 0 };
1978         int status;
1979
1980         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1981         encode_compound_hdr(&xdr, &hdr);
1982         status = encode_setclientid_confirm(&xdr, clp);
1983         if (!status)
1984                 status = encode_putrootfh(&xdr);
1985         if (!status)
1986                 status = encode_fsinfo(&xdr, lease_bitmap);
1987         return status;
1988 }
1989
1990 /*
1991  * DELEGRETURN request
1992  */
1993 static int nfs4_xdr_enc_delegreturn(struct rpc_rqst *req, uint32_t *p, const struct nfs4_delegreturnargs *args)
1994 {
1995         struct xdr_stream xdr;
1996         struct compound_hdr hdr = {
1997                 .nops = 3,
1998         };
1999         int status;
2000
2001         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2002         encode_compound_hdr(&xdr, &hdr);
2003         status = encode_putfh(&xdr, args->fhandle);
2004         if (status != 0)
2005                 goto out;
2006         status = encode_delegreturn(&xdr, args->stateid);
2007         if (status != 0)
2008                 goto out;
2009         status = encode_getfattr(&xdr, args->bitmask);
2010 out:
2011         return status;
2012 }
2013
2014 /*
2015  * Encode FS_LOCATIONS request
2016  */
2017 static int nfs4_xdr_enc_fs_locations(struct rpc_rqst *req, uint32_t *p, struct nfs4_fs_locations_arg *args)
2018 {
2019         struct xdr_stream xdr;
2020         struct compound_hdr hdr = {
2021                 .nops = 3,
2022         };
2023         struct rpc_auth *auth = req->rq_task->tk_auth;
2024         int replen;
2025         int status;
2026
2027         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2028         encode_compound_hdr(&xdr, &hdr);
2029         if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
2030                 goto out;
2031         if ((status = encode_lookup(&xdr, args->name)) != 0)
2032                 goto out;
2033         if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
2034                 goto out;
2035         /* set up reply
2036          *   toplevel_status + taglen + rescount + OP_PUTFH + status
2037          *   + OP_LOOKUP + status + OP_GETATTR + status = 7
2038          */
2039         replen = (RPC_REPHDRSIZE + auth->au_rslack + 7) << 2;
2040         xdr_inline_pages(&req->rq_rcv_buf, replen, &args->page,
2041                         0, PAGE_SIZE);
2042 out:
2043         return status;
2044 }
2045
2046 /*
2047  * START OF "GENERIC" DECODE ROUTINES.
2048  *   These may look a little ugly since they are imported from a "generic"
2049  * set of XDR encode/decode routines which are intended to be shared by
2050  * all of our NFSv4 implementations (OpenBSD, MacOS X...).
2051  *
2052  * If the pain of reading these is too great, it should be a straightforward
2053  * task to translate them into Linux-specific versions which are more
2054  * consistent with the style used in NFSv2/v3...
2055  */
2056 #define READ32(x)         (x) = ntohl(*p++)
2057 #define READ64(x)         do {                  \
2058         (x) = (u64)ntohl(*p++) << 32;           \
2059         (x) |= ntohl(*p++);                     \
2060 } while (0)
2061 #define READTIME(x)       do {                  \
2062         p++;                                    \
2063         (x.tv_sec) = ntohl(*p++);               \
2064         (x.tv_nsec) = ntohl(*p++);              \
2065 } while (0)
2066 #define COPYMEM(x,nbytes) do {                  \
2067         memcpy((x), p, nbytes);                 \
2068         p += XDR_QUADLEN(nbytes);               \
2069 } while (0)
2070
2071 #define READ_BUF(nbytes)  do { \
2072         p = xdr_inline_decode(xdr, nbytes); \
2073         if (!p) { \
2074                 printk(KERN_WARNING "%s: reply buffer overflowed in line %d.", \
2075                                 __FUNCTION__, __LINE__); \
2076                 return -EIO; \
2077         } \
2078 } while (0)
2079
2080 static int decode_opaque_inline(struct xdr_stream *xdr, unsigned int *len, char **string)
2081 {
2082         uint32_t *p;
2083
2084         READ_BUF(4);
2085         READ32(*len);
2086         READ_BUF(*len);
2087         *string = (char *)p;
2088         return 0;
2089 }
2090
2091 static int decode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
2092 {
2093         uint32_t *p;
2094
2095         READ_BUF(8);
2096         READ32(hdr->status);
2097         READ32(hdr->taglen);
2098         
2099         READ_BUF(hdr->taglen + 4);
2100         hdr->tag = (char *)p;
2101         p += XDR_QUADLEN(hdr->taglen);
2102         READ32(hdr->nops);
2103         return 0;
2104 }
2105
2106 static int decode_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
2107 {
2108         uint32_t *p;
2109         uint32_t opnum;
2110         int32_t nfserr;
2111
2112         READ_BUF(8);
2113         READ32(opnum);
2114         if (opnum != expected) {
2115                 printk(KERN_NOTICE
2116                                 "nfs4_decode_op_hdr: Server returned operation"
2117                                 " %d but we issued a request for %d\n",
2118                                 opnum, expected);
2119                 return -EIO;
2120         }
2121         READ32(nfserr);
2122         if (nfserr != NFS_OK)
2123                 return -nfs_stat_to_errno(nfserr);
2124         return 0;
2125 }
2126
2127 /* Dummy routine */
2128 static int decode_ace(struct xdr_stream *xdr, void *ace, struct nfs4_client *clp)
2129 {
2130         uint32_t *p;
2131         unsigned int strlen;
2132         char *str;
2133
2134         READ_BUF(12);
2135         return decode_opaque_inline(xdr, &strlen, &str);
2136 }
2137
2138 static int decode_attr_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
2139 {
2140         uint32_t bmlen, *p;
2141
2142         READ_BUF(4);
2143         READ32(bmlen);
2144
2145         bitmap[0] = bitmap[1] = 0;
2146         READ_BUF((bmlen << 2));
2147         if (bmlen > 0) {
2148                 READ32(bitmap[0]);
2149                 if (bmlen > 1)
2150                         READ32(bitmap[1]);
2151         }
2152         return 0;
2153 }
2154
2155 static inline int decode_attr_length(struct xdr_stream *xdr, uint32_t *attrlen, uint32_t **savep)
2156 {
2157         uint32_t *p;
2158
2159         READ_BUF(4);
2160         READ32(*attrlen);
2161         *savep = xdr->p;
2162         return 0;
2163 }
2164
2165 static int decode_attr_supported(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *bitmask)
2166 {
2167         if (likely(bitmap[0] & FATTR4_WORD0_SUPPORTED_ATTRS)) {
2168                 decode_attr_bitmap(xdr, bitmask);
2169                 bitmap[0] &= ~FATTR4_WORD0_SUPPORTED_ATTRS;
2170         } else
2171                 bitmask[0] = bitmask[1] = 0;
2172         dprintk("%s: bitmask=0x%x%x\n", __FUNCTION__, bitmask[0], bitmask[1]);
2173         return 0;
2174 }
2175
2176 static int decode_attr_type(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *type)
2177 {
2178         uint32_t *p;
2179
2180         *type = 0;
2181         if (unlikely(bitmap[0] & (FATTR4_WORD0_TYPE - 1U)))
2182                 return -EIO;
2183         if (likely(bitmap[0] & FATTR4_WORD0_TYPE)) {
2184                 READ_BUF(4);
2185                 READ32(*type);
2186                 if (*type < NF4REG || *type > NF4NAMEDATTR) {
2187                         dprintk("%s: bad type %d\n", __FUNCTION__, *type);
2188                         return -EIO;
2189                 }
2190                 bitmap[0] &= ~FATTR4_WORD0_TYPE;
2191         }
2192         dprintk("%s: type=0%o\n", __FUNCTION__, nfs_type2fmt[*type].nfs2type);
2193         return 0;
2194 }
2195
2196 static int decode_attr_change(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *change)
2197 {
2198         uint32_t *p;
2199
2200         *change = 0;
2201         if (unlikely(bitmap[0] & (FATTR4_WORD0_CHANGE - 1U)))
2202                 return -EIO;
2203         if (likely(bitmap[0] & FATTR4_WORD0_CHANGE)) {
2204                 READ_BUF(8);
2205                 READ64(*change);
2206                 bitmap[0] &= ~FATTR4_WORD0_CHANGE;
2207         }
2208         dprintk("%s: change attribute=%Lu\n", __FUNCTION__,
2209                         (unsigned long long)*change);
2210         return 0;
2211 }
2212
2213 static int decode_attr_size(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *size)
2214 {
2215         uint32_t *p;
2216
2217         *size = 0;
2218         if (unlikely(bitmap[0] & (FATTR4_WORD0_SIZE - 1U)))
2219                 return -EIO;
2220         if (likely(bitmap[0] & FATTR4_WORD0_SIZE)) {
2221                 READ_BUF(8);
2222                 READ64(*size);
2223                 bitmap[0] &= ~FATTR4_WORD0_SIZE;
2224         }
2225         dprintk("%s: file size=%Lu\n", __FUNCTION__, (unsigned long long)*size);
2226         return 0;
2227 }
2228
2229 static int decode_attr_link_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2230 {
2231         uint32_t *p;
2232
2233         *res = 0;
2234         if (unlikely(bitmap[0] & (FATTR4_WORD0_LINK_SUPPORT - 1U)))
2235                 return -EIO;
2236         if (likely(bitmap[0] & FATTR4_WORD0_LINK_SUPPORT)) {
2237                 READ_BUF(4);
2238                 READ32(*res);
2239                 bitmap[0] &= ~FATTR4_WORD0_LINK_SUPPORT;
2240         }
2241         dprintk("%s: link support=%s\n", __FUNCTION__, *res == 0 ? "false" : "true");
2242         return 0;
2243 }
2244
2245 static int decode_attr_symlink_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2246 {
2247         uint32_t *p;
2248
2249         *res = 0;
2250         if (unlikely(bitmap[0] & (FATTR4_WORD0_SYMLINK_SUPPORT - 1U)))
2251                 return -EIO;
2252         if (likely(bitmap[0] & FATTR4_WORD0_SYMLINK_SUPPORT)) {
2253                 READ_BUF(4);
2254                 READ32(*res);
2255                 bitmap[0] &= ~FATTR4_WORD0_SYMLINK_SUPPORT;
2256         }
2257         dprintk("%s: symlink support=%s\n", __FUNCTION__, *res == 0 ? "false" : "true");
2258         return 0;
2259 }
2260
2261 static int decode_attr_fsid(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs_fsid *fsid)
2262 {
2263         uint32_t *p;
2264
2265         fsid->major = 0;
2266         fsid->minor = 0;
2267         if (unlikely(bitmap[0] & (FATTR4_WORD0_FSID - 1U)))
2268                 return -EIO;
2269         if (likely(bitmap[0] & FATTR4_WORD0_FSID)) {
2270                 READ_BUF(16);
2271                 READ64(fsid->major);
2272                 READ64(fsid->minor);
2273                 bitmap[0] &= ~FATTR4_WORD0_FSID;
2274         }
2275         dprintk("%s: fsid=(0x%Lx/0x%Lx)\n", __FUNCTION__,
2276                         (unsigned long long)fsid->major,
2277                         (unsigned long long)fsid->minor);
2278         return 0;
2279 }
2280
2281 static int decode_attr_lease_time(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2282 {
2283         uint32_t *p;
2284
2285         *res = 60;
2286         if (unlikely(bitmap[0] & (FATTR4_WORD0_LEASE_TIME - 1U)))
2287                 return -EIO;
2288         if (likely(bitmap[0] & FATTR4_WORD0_LEASE_TIME)) {
2289                 READ_BUF(4);
2290                 READ32(*res);
2291                 bitmap[0] &= ~FATTR4_WORD0_LEASE_TIME;
2292         }
2293         dprintk("%s: file size=%u\n", __FUNCTION__, (unsigned int)*res);
2294         return 0;
2295 }
2296
2297 static int decode_attr_aclsupport(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2298 {
2299         uint32_t *p;
2300
2301         *res = ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL;
2302         if (unlikely(bitmap[0] & (FATTR4_WORD0_ACLSUPPORT - 1U)))
2303                 return -EIO;
2304         if (likely(bitmap[0] & FATTR4_WORD0_ACLSUPPORT)) {
2305                 READ_BUF(4);
2306                 READ32(*res);
2307                 bitmap[0] &= ~FATTR4_WORD0_ACLSUPPORT;
2308         }
2309         dprintk("%s: ACLs supported=%u\n", __FUNCTION__, (unsigned int)*res);
2310         return 0;
2311 }
2312
2313 static int decode_attr_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
2314 {
2315         uint32_t *p;
2316
2317         *fileid = 0;
2318         if (unlikely(bitmap[0] & (FATTR4_WORD0_FILEID - 1U)))
2319                 return -EIO;
2320         if (likely(bitmap[0] & FATTR4_WORD0_FILEID)) {
2321                 READ_BUF(8);
2322                 READ64(*fileid);
2323                 bitmap[0] &= ~FATTR4_WORD0_FILEID;
2324         }
2325         dprintk("%s: fileid=%Lu\n", __FUNCTION__, (unsigned long long)*fileid);
2326         return 0;
2327 }
2328
2329 static int decode_attr_mounted_on_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
2330 {
2331         uint32_t *p;
2332
2333         *fileid = 0;
2334         if (unlikely(bitmap[1] & (FATTR4_WORD1_MOUNTED_ON_FILEID - 1U)))
2335                 return -EIO;
2336         if (likely(bitmap[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)) {
2337                 READ_BUF(8);
2338                 READ64(*fileid);
2339                 bitmap[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
2340         }
2341         dprintk("%s: fileid=%Lu\n", __FUNCTION__, (unsigned long long)*fileid);
2342         return 0;
2343 }
2344
2345 static int decode_attr_files_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2346 {
2347         uint32_t *p;
2348         int status = 0;
2349
2350         *res = 0;
2351         if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_AVAIL - 1U)))
2352                 return -EIO;
2353         if (likely(bitmap[0] & FATTR4_WORD0_FILES_AVAIL)) {
2354                 READ_BUF(8);
2355                 READ64(*res);
2356                 bitmap[0] &= ~FATTR4_WORD0_FILES_AVAIL;
2357         }
2358         dprintk("%s: files avail=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2359         return status;
2360 }
2361
2362 static int decode_attr_files_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2363 {
2364         uint32_t *p;
2365         int status = 0;
2366
2367         *res = 0;
2368         if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_FREE - 1U)))
2369                 return -EIO;
2370         if (likely(bitmap[0] & FATTR4_WORD0_FILES_FREE)) {
2371                 READ_BUF(8);
2372                 READ64(*res);
2373                 bitmap[0] &= ~FATTR4_WORD0_FILES_FREE;
2374         }
2375         dprintk("%s: files free=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2376         return status;
2377 }
2378
2379 static int decode_attr_files_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2380 {
2381         uint32_t *p;
2382         int status = 0;
2383
2384         *res = 0;
2385         if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_TOTAL - 1U)))
2386                 return -EIO;
2387         if (likely(bitmap[0] & FATTR4_WORD0_FILES_TOTAL)) {
2388                 READ_BUF(8);
2389                 READ64(*res);
2390                 bitmap[0] &= ~FATTR4_WORD0_FILES_TOTAL;
2391         }
2392         dprintk("%s: files total=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2393         return status;
2394 }
2395
2396 static int decode_pathname(struct xdr_stream *xdr, struct nfs4_pathname *path)
2397 {
2398         int n;
2399         uint32_t *p;
2400         int status = 0;
2401
2402         READ_BUF(4);
2403         READ32(n);
2404         if (n <= 0)
2405                 goto out_eio;
2406         dprintk("path ");
2407         path->ncomponents = 0;
2408         while (path->ncomponents < n) {
2409                 struct nfs4_string *component = &path->components[path->ncomponents];
2410                 status = decode_opaque_inline(xdr, &component->len, &component->data);
2411                 if (unlikely(status != 0))
2412                         goto out_eio;
2413                 if (path->ncomponents != n)
2414                         dprintk("/");
2415                 dprintk("%s", component->data);
2416                 if (path->ncomponents < NFS4_PATHNAME_MAXCOMPONENTS)
2417                         path->ncomponents++;
2418                 else {
2419                         dprintk("cannot parse %d components in path\n", n);
2420                         goto out_eio;
2421                 }
2422         }
2423 out:
2424         dprintk("\n");
2425         return status;
2426 out_eio:
2427         dprintk(" status %d", status);
2428         status = -EIO;
2429         goto out;
2430 }
2431
2432 static int decode_attr_fs_locations(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_fs_locations *res)
2433 {
2434         int n;
2435         uint32_t *p;
2436         int status = -EIO;
2437
2438         if (unlikely(bitmap[0] & (FATTR4_WORD0_FS_LOCATIONS -1U)))
2439                 goto out;
2440         status = 0;
2441         if (unlikely(!(bitmap[0] & FATTR4_WORD0_FS_LOCATIONS)))
2442                 goto out;
2443         dprintk("%s: fsroot ", __FUNCTION__);
2444         status = decode_pathname(xdr, &res->fs_path);
2445         if (unlikely(status != 0))
2446                 goto out;
2447         READ_BUF(4);
2448         READ32(n);
2449         if (n <= 0)
2450                 goto out_eio;
2451         res->nlocations = 0;
2452         while (res->nlocations < n) {
2453                 int m;
2454                 struct nfs4_fs_location *loc = &res->locations[res->nlocations];
2455
2456                 READ_BUF(4);
2457                 READ32(m);
2458                 if (m <= 0)
2459                         goto out_eio;
2460
2461                 loc->nservers = 0;
2462                 dprintk("%s: servers ", __FUNCTION__);
2463                 while (loc->nservers < m) {
2464                         struct nfs4_string *server = &loc->servers[loc->nservers];
2465                         status = decode_opaque_inline(xdr, &server->len, &server->data);
2466                         if (unlikely(status != 0))
2467                                 goto out_eio;
2468                         dprintk("%s ", server->data);
2469                         if (loc->nservers < NFS4_FS_LOCATION_MAXSERVERS)
2470                                 loc->nservers++;
2471                         else {
2472                                 int i;
2473                                 dprintk("%s: using first %d of %d servers returned for location %d\n", __FUNCTION__, NFS4_FS_LOCATION_MAXSERVERS, m, res->nlocations);
2474                                 for (i = loc->nservers; i < m; i++) {
2475                                         int len;
2476                                         char *data;
2477                                         status = decode_opaque_inline(xdr, &len, &data);
2478                                         if (unlikely(status != 0))
2479                                                 goto out_eio;
2480                                 }
2481                         }
2482                 }
2483                 status = decode_pathname(xdr, &loc->rootpath);
2484                 if (unlikely(status != 0))
2485                         goto out_eio;
2486                 if (res->nlocations < NFS4_FS_LOCATIONS_MAXENTRIES)
2487                         res->nlocations++;
2488         }
2489 out:
2490         dprintk("%s: fs_locations done, error = %d\n", __FUNCTION__, status);
2491         return status;
2492 out_eio:
2493         status = -EIO;
2494         goto out;
2495 }
2496
2497 static int decode_attr_maxfilesize(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2498 {
2499         uint32_t *p;
2500         int status = 0;
2501
2502         *res = 0;
2503         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXFILESIZE - 1U)))
2504                 return -EIO;
2505         if (likely(bitmap[0] & FATTR4_WORD0_MAXFILESIZE)) {
2506                 READ_BUF(8);
2507                 READ64(*res);
2508                 bitmap[0] &= ~FATTR4_WORD0_MAXFILESIZE;
2509         }
2510         dprintk("%s: maxfilesize=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2511         return status;
2512 }
2513
2514 static int decode_attr_maxlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxlink)
2515 {
2516         uint32_t *p;
2517         int status = 0;
2518
2519         *maxlink = 1;
2520         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXLINK - 1U)))
2521                 return -EIO;
2522         if (likely(bitmap[0] & FATTR4_WORD0_MAXLINK)) {
2523                 READ_BUF(4);
2524                 READ32(*maxlink);
2525                 bitmap[0] &= ~FATTR4_WORD0_MAXLINK;
2526         }
2527         dprintk("%s: maxlink=%u\n", __FUNCTION__, *maxlink);
2528         return status;
2529 }
2530
2531 static int decode_attr_maxname(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxname)
2532 {
2533         uint32_t *p;
2534         int status = 0;
2535
2536         *maxname = 1024;
2537         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXNAME - 1U)))
2538                 return -EIO;
2539         if (likely(bitmap[0] & FATTR4_WORD0_MAXNAME)) {
2540                 READ_BUF(4);
2541                 READ32(*maxname);
2542                 bitmap[0] &= ~FATTR4_WORD0_MAXNAME;
2543         }
2544         dprintk("%s: maxname=%u\n", __FUNCTION__, *maxname);
2545         return status;
2546 }
2547
2548 static int decode_attr_maxread(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2549 {
2550         uint32_t *p;
2551         int status = 0;
2552
2553         *res = 1024;
2554         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXREAD - 1U)))
2555                 return -EIO;
2556         if (likely(bitmap[0] & FATTR4_WORD0_MAXREAD)) {
2557                 uint64_t maxread;
2558                 READ_BUF(8);
2559                 READ64(maxread);
2560                 if (maxread > 0x7FFFFFFF)
2561                         maxread = 0x7FFFFFFF;
2562                 *res = (uint32_t)maxread;
2563                 bitmap[0] &= ~FATTR4_WORD0_MAXREAD;
2564         }
2565         dprintk("%s: maxread=%lu\n", __FUNCTION__, (unsigned long)*res);
2566         return status;
2567 }
2568
2569 static int decode_attr_maxwrite(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2570 {
2571         uint32_t *p;
2572         int status = 0;
2573
2574         *res = 1024;
2575         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXWRITE - 1U)))
2576                 return -EIO;
2577         if (likely(bitmap[0] & FATTR4_WORD0_MAXWRITE)) {
2578                 uint64_t maxwrite;
2579                 READ_BUF(8);
2580                 READ64(maxwrite);
2581                 if (maxwrite > 0x7FFFFFFF)
2582                         maxwrite = 0x7FFFFFFF;
2583                 *res = (uint32_t)maxwrite;
2584                 bitmap[0] &= ~FATTR4_WORD0_MAXWRITE;
2585         }
2586         dprintk("%s: maxwrite=%lu\n", __FUNCTION__, (unsigned long)*res);
2587         return status;
2588 }
2589
2590 static int decode_attr_mode(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *mode)
2591 {
2592         uint32_t *p;
2593
2594         *mode = 0;
2595         if (unlikely(bitmap[1] & (FATTR4_WORD1_MODE - 1U)))
2596                 return -EIO;
2597         if (likely(bitmap[1] & FATTR4_WORD1_MODE)) {
2598                 READ_BUF(4);
2599                 READ32(*mode);
2600                 *mode &= ~S_IFMT;
2601                 bitmap[1] &= ~FATTR4_WORD1_MODE;
2602         }
2603         dprintk("%s: file mode=0%o\n", __FUNCTION__, (unsigned int)*mode);
2604         return 0;
2605 }
2606
2607 static int decode_attr_nlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *nlink)
2608 {
2609         uint32_t *p;
2610
2611         *nlink = 1;
2612         if (unlikely(bitmap[1] & (FATTR4_WORD1_NUMLINKS - 1U)))
2613                 return -EIO;
2614         if (likely(bitmap[1] & FATTR4_WORD1_NUMLINKS)) {
2615                 READ_BUF(4);
2616                 READ32(*nlink);
2617                 bitmap[1] &= ~FATTR4_WORD1_NUMLINKS;
2618         }
2619         dprintk("%s: nlink=%u\n", __FUNCTION__, (unsigned int)*nlink);
2620         return 0;
2621 }
2622
2623 static int decode_attr_owner(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_client *clp, int32_t *uid)
2624 {
2625         uint32_t len, *p;
2626
2627         *uid = -2;
2628         if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER - 1U)))
2629                 return -EIO;
2630         if (likely(bitmap[1] & FATTR4_WORD1_OWNER)) {
2631                 READ_BUF(4);
2632                 READ32(len);
2633                 READ_BUF(len);
2634                 if (len < XDR_MAX_NETOBJ) {
2635                         if (nfs_map_name_to_uid(clp, (char *)p, len, uid) != 0)
2636                                 dprintk("%s: nfs_map_name_to_uid failed!\n",
2637                                                 __FUNCTION__);
2638                 } else
2639                         printk(KERN_WARNING "%s: name too long (%u)!\n",
2640                                         __FUNCTION__, len);
2641                 bitmap[1] &= ~FATTR4_WORD1_OWNER;
2642         }
2643         dprintk("%s: uid=%d\n", __FUNCTION__, (int)*uid);
2644         return 0;
2645 }
2646
2647 static int decode_attr_group(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_client *clp, int32_t *gid)
2648 {
2649         uint32_t len, *p;
2650
2651         *gid = -2;
2652         if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER_GROUP - 1U)))
2653                 return -EIO;
2654         if (likely(bitmap[1] & FATTR4_WORD1_OWNER_GROUP)) {
2655                 READ_BUF(4);
2656                 READ32(len);
2657                 READ_BUF(len);
2658                 if (len < XDR_MAX_NETOBJ) {
2659                         if (nfs_map_group_to_gid(clp, (char *)p, len, gid) != 0)
2660                                 dprintk("%s: nfs_map_group_to_gid failed!\n",
2661                                                 __FUNCTION__);
2662                 } else
2663                         printk(KERN_WARNING "%s: name too long (%u)!\n",
2664                                         __FUNCTION__, len);
2665                 bitmap[1] &= ~FATTR4_WORD1_OWNER_GROUP;
2666         }
2667         dprintk("%s: gid=%d\n", __FUNCTION__, (int)*gid);
2668         return 0;
2669 }
2670
2671 static int decode_attr_rdev(struct xdr_stream *xdr, uint32_t *bitmap, dev_t *rdev)
2672 {
2673         uint32_t major = 0, minor = 0, *p;
2674
2675         *rdev = MKDEV(0,0);
2676         if (unlikely(bitmap[1] & (FATTR4_WORD1_RAWDEV - 1U)))
2677                 return -EIO;
2678         if (likely(bitmap[1] & FATTR4_WORD1_RAWDEV)) {
2679                 dev_t tmp;
2680
2681                 READ_BUF(8);
2682                 READ32(major);
2683                 READ32(minor);
2684                 tmp = MKDEV(major, minor);
2685                 if (MAJOR(tmp) == major && MINOR(tmp) == minor)
2686                         *rdev = tmp;
2687                 bitmap[1] &= ~ FATTR4_WORD1_RAWDEV;
2688         }
2689         dprintk("%s: rdev=(0x%x:0x%x)\n", __FUNCTION__, major, minor);
2690         return 0;
2691 }
2692
2693 static int decode_attr_space_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2694 {
2695         uint32_t *p;
2696         int status = 0;
2697
2698         *res = 0;
2699         if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_AVAIL - 1U)))
2700                 return -EIO;
2701         if (likely(bitmap[1] & FATTR4_WORD1_SPACE_AVAIL)) {
2702                 READ_BUF(8);
2703                 READ64(*res);
2704                 bitmap[1] &= ~FATTR4_WORD1_SPACE_AVAIL;
2705         }
2706         dprintk("%s: space avail=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2707         return status;
2708 }
2709
2710 static int decode_attr_space_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2711 {
2712         uint32_t *p;
2713         int status = 0;
2714
2715         *res = 0;
2716         if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_FREE - 1U)))
2717                 return -EIO;
2718         if (likely(bitmap[1] & FATTR4_WORD1_SPACE_FREE)) {
2719                 READ_BUF(8);
2720                 READ64(*res);
2721                 bitmap[1] &= ~FATTR4_WORD1_SPACE_FREE;
2722         }
2723         dprintk("%s: space free=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2724         return status;
2725 }
2726
2727 static int decode_attr_space_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2728 {
2729         uint32_t *p;
2730         int status = 0;
2731
2732         *res = 0;
2733         if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_TOTAL - 1U)))
2734                 return -EIO;
2735         if (likely(bitmap[1] & FATTR4_WORD1_SPACE_TOTAL)) {
2736                 READ_BUF(8);
2737                 READ64(*res);
2738                 bitmap[1] &= ~FATTR4_WORD1_SPACE_TOTAL;
2739         }
2740         dprintk("%s: space total=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2741         return status;
2742 }
2743
2744 static int decode_attr_space_used(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *used)
2745 {
2746         uint32_t *p;
2747
2748         *used = 0;
2749         if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_USED - 1U)))
2750                 return -EIO;
2751         if (likely(bitmap[1] & FATTR4_WORD1_SPACE_USED)) {
2752                 READ_BUF(8);
2753                 READ64(*used);
2754                 bitmap[1] &= ~FATTR4_WORD1_SPACE_USED;
2755         }
2756         dprintk("%s: space used=%Lu\n", __FUNCTION__,
2757                         (unsigned long long)*used);
2758         return 0;
2759 }
2760
2761 static int decode_attr_time(struct xdr_stream *xdr, struct timespec *time)
2762 {
2763         uint32_t *p;
2764         uint64_t sec;
2765         uint32_t nsec;
2766
2767         READ_BUF(12);
2768         READ64(sec);
2769         READ32(nsec);
2770         time->tv_sec = (time_t)sec;
2771         time->tv_nsec = (long)nsec;
2772         return 0;
2773 }
2774
2775 static int decode_attr_time_access(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2776 {
2777         int status = 0;
2778
2779         time->tv_sec = 0;
2780         time->tv_nsec = 0;
2781         if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_ACCESS - 1U)))
2782                 return -EIO;
2783         if (likely(bitmap[1] & FATTR4_WORD1_TIME_ACCESS)) {
2784                 status = decode_attr_time(xdr, time);
2785                 bitmap[1] &= ~FATTR4_WORD1_TIME_ACCESS;
2786         }
2787         dprintk("%s: atime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2788         return status;
2789 }
2790
2791 static int decode_attr_time_metadata(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2792 {
2793         int status = 0;
2794
2795         time->tv_sec = 0;
2796         time->tv_nsec = 0;
2797         if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_METADATA - 1U)))
2798                 return -EIO;
2799         if (likely(bitmap[1] & FATTR4_WORD1_TIME_METADATA)) {
2800                 status = decode_attr_time(xdr, time);
2801                 bitmap[1] &= ~FATTR4_WORD1_TIME_METADATA;
2802         }
2803         dprintk("%s: ctime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2804         return status;
2805 }
2806
2807 static int decode_attr_time_modify(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2808 {
2809         int status = 0;
2810
2811         time->tv_sec = 0;
2812         time->tv_nsec = 0;
2813         if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_MODIFY - 1U)))
2814                 return -EIO;
2815         if (likely(bitmap[1] & FATTR4_WORD1_TIME_MODIFY)) {
2816                 status = decode_attr_time(xdr, time);
2817                 bitmap[1] &= ~FATTR4_WORD1_TIME_MODIFY;
2818         }
2819         dprintk("%s: mtime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2820         return status;
2821 }
2822
2823 static int verify_attr_len(struct xdr_stream *xdr, uint32_t *savep, uint32_t attrlen)
2824 {
2825         unsigned int attrwords = XDR_QUADLEN(attrlen);
2826         unsigned int nwords = xdr->p - savep;
2827
2828         if (unlikely(attrwords != nwords)) {
2829                 printk(KERN_WARNING "%s: server returned incorrect attribute length: %u %c %u\n",
2830                                 __FUNCTION__,
2831                                 attrwords << 2,
2832                                 (attrwords < nwords) ? '<' : '>',
2833                                 nwords << 2);
2834                 return -EIO;
2835         }
2836         return 0;
2837 }
2838
2839 static int decode_change_info(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
2840 {
2841         uint32_t *p;
2842
2843         READ_BUF(20);
2844         READ32(cinfo->atomic);
2845         READ64(cinfo->before);
2846         READ64(cinfo->after);
2847         return 0;
2848 }
2849
2850 static int decode_access(struct xdr_stream *xdr, struct nfs4_accessres *access)
2851 {
2852         uint32_t *p;
2853         uint32_t supp, acc;
2854         int status;
2855
2856         status = decode_op_hdr(xdr, OP_ACCESS);
2857         if (status)
2858                 return status;
2859         READ_BUF(8);
2860         READ32(supp);
2861         READ32(acc);
2862         access->supported = supp;
2863         access->access = acc;
2864         return 0;
2865 }
2866
2867 static int decode_close(struct xdr_stream *xdr, struct nfs_closeres *res)
2868 {
2869         uint32_t *p;
2870         int status;
2871
2872         status = decode_op_hdr(xdr, OP_CLOSE);
2873         if (status)
2874                 return status;
2875         READ_BUF(sizeof(res->stateid.data));
2876         COPYMEM(res->stateid.data, sizeof(res->stateid.data));
2877         return 0;
2878 }
2879
2880 static int decode_commit(struct xdr_stream *xdr, struct nfs_writeres *res)
2881 {
2882         uint32_t *p;
2883         int status;
2884
2885         status = decode_op_hdr(xdr, OP_COMMIT);
2886         if (status)
2887                 return status;
2888         READ_BUF(8);
2889         COPYMEM(res->verf->verifier, 8);
2890         return 0;
2891 }
2892
2893 static int decode_create(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
2894 {
2895         uint32_t *p;
2896         uint32_t bmlen;
2897         int status;
2898
2899         status = decode_op_hdr(xdr, OP_CREATE);
2900         if (status)
2901                 return status;
2902         if ((status = decode_change_info(xdr, cinfo)))
2903                 return status;
2904         READ_BUF(4);
2905         READ32(bmlen);
2906         READ_BUF(bmlen << 2);
2907         return 0;
2908 }
2909
2910 static int decode_server_caps(struct xdr_stream *xdr, struct nfs4_server_caps_res *res)
2911 {
2912         uint32_t *savep;
2913         uint32_t attrlen, 
2914                  bitmap[2] = {0};
2915         int status;
2916
2917         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2918                 goto xdr_error;
2919         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2920                 goto xdr_error;
2921         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2922                 goto xdr_error;
2923         if ((status = decode_attr_supported(xdr, bitmap, res->attr_bitmask)) != 0)
2924                 goto xdr_error;
2925         if ((status = decode_attr_link_support(xdr, bitmap, &res->has_links)) != 0)
2926                 goto xdr_error;
2927         if ((status = decode_attr_symlink_support(xdr, bitmap, &res->has_symlinks)) != 0)
2928                 goto xdr_error;
2929         if ((status = decode_attr_aclsupport(xdr, bitmap, &res->acl_bitmask)) != 0)
2930                 goto xdr_error;
2931         status = verify_attr_len(xdr, savep, attrlen);
2932 xdr_error:
2933         dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
2934         return status;
2935 }
2936         
2937 static int decode_statfs(struct xdr_stream *xdr, struct nfs_fsstat *fsstat)
2938 {
2939         uint32_t *savep;
2940         uint32_t attrlen, 
2941                  bitmap[2] = {0};
2942         int status;
2943         
2944         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2945                 goto xdr_error;
2946         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2947                 goto xdr_error;
2948         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2949                 goto xdr_error;
2950
2951         if ((status = decode_attr_files_avail(xdr, bitmap, &fsstat->afiles)) != 0)
2952                 goto xdr_error;
2953         if ((status = decode_attr_files_free(xdr, bitmap, &fsstat->ffiles)) != 0)
2954                 goto xdr_error;
2955         if ((status = decode_attr_files_total(xdr, bitmap, &fsstat->tfiles)) != 0)
2956                 goto xdr_error;
2957         if ((status = decode_attr_space_avail(xdr, bitmap, &fsstat->abytes)) != 0)
2958                 goto xdr_error;
2959         if ((status = decode_attr_space_free(xdr, bitmap, &fsstat->fbytes)) != 0)
2960                 goto xdr_error;
2961         if ((status = decode_attr_space_total(xdr, bitmap, &fsstat->tbytes)) != 0)
2962                 goto xdr_error;
2963
2964         status = verify_attr_len(xdr, savep, attrlen);
2965 xdr_error:
2966         dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
2967         return status;
2968 }
2969
2970 static int decode_pathconf(struct xdr_stream *xdr, struct nfs_pathconf *pathconf)
2971 {
2972         uint32_t *savep;
2973         uint32_t attrlen, 
2974                  bitmap[2] = {0};
2975         int status;
2976         
2977         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2978                 goto xdr_error;
2979         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2980                 goto xdr_error;
2981         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2982                 goto xdr_error;
2983
2984         if ((status = decode_attr_maxlink(xdr, bitmap, &pathconf->max_link)) != 0)
2985                 goto xdr_error;
2986         if ((status = decode_attr_maxname(xdr, bitmap, &pathconf->max_namelen)) != 0)
2987                 goto xdr_error;
2988
2989         status = verify_attr_len(xdr, savep, attrlen);
2990 xdr_error:
2991         dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
2992         return status;
2993 }
2994
2995 static int decode_getfattr(struct xdr_stream *xdr, struct nfs_fattr *fattr, const struct nfs_server *server)
2996 {
2997         uint32_t *savep;
2998         uint32_t attrlen,
2999                  bitmap[2] = {0},
3000                  type;
3001         int status, fmode = 0;
3002         uint64_t fileid;
3003
3004         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3005                 goto xdr_error;
3006         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3007                 goto xdr_error;
3008
3009         fattr->bitmap[0] = bitmap[0];
3010         fattr->bitmap[1] = bitmap[1];
3011
3012         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3013                 goto xdr_error;
3014
3015
3016         if ((status = decode_attr_type(xdr, bitmap, &type)) != 0)
3017                 goto xdr_error;
3018         fattr->type = nfs_type2fmt[type].nfs2type;
3019         fmode = nfs_type2fmt[type].mode;
3020
3021         if ((status = decode_attr_change(xdr, bitmap, &fattr->change_attr)) != 0)
3022                 goto xdr_error;
3023         if ((status = decode_attr_size(xdr, bitmap, &fattr->size)) != 0)
3024                 goto xdr_error;
3025         if ((status = decode_attr_fsid(xdr, bitmap, &fattr->fsid)) != 0)
3026                 goto xdr_error;
3027         if ((status = decode_attr_fileid(xdr, bitmap, &fattr->fileid)) != 0)
3028                 goto xdr_error;
3029         if ((status = decode_attr_fs_locations(xdr, bitmap, container_of(fattr,
3030                                                 struct nfs4_fs_locations,
3031                                                 fattr))) != 0)
3032                 goto xdr_error;
3033         if ((status = decode_attr_mode(xdr, bitmap, &fattr->mode)) != 0)
3034                 goto xdr_error;
3035         fattr->mode |= fmode;
3036         if ((status = decode_attr_nlink(xdr, bitmap, &fattr->nlink)) != 0)
3037                 goto xdr_error;
3038         if ((status = decode_attr_owner(xdr, bitmap, server->nfs4_state, &fattr->uid)) != 0)
3039                 goto xdr_error;
3040         if ((status = decode_attr_group(xdr, bitmap, server->nfs4_state, &fattr->gid)) != 0)
3041                 goto xdr_error;
3042         if ((status = decode_attr_rdev(xdr, bitmap, &fattr->rdev)) != 0)
3043                 goto xdr_error;
3044         if ((status = decode_attr_space_used(xdr, bitmap, &fattr->du.nfs3.used)) != 0)
3045                 goto xdr_error;
3046         if ((status = decode_attr_time_access(xdr, bitmap, &fattr->atime)) != 0)
3047                 goto xdr_error;
3048         if ((status = decode_attr_time_metadata(xdr, bitmap, &fattr->ctime)) != 0)
3049                 goto xdr_error;
3050         if ((status = decode_attr_time_modify(xdr, bitmap, &fattr->mtime)) != 0)
3051                 goto xdr_error;
3052         if ((status = decode_attr_mounted_on_fileid(xdr, bitmap, &fileid)) != 0)
3053                 goto xdr_error;
3054         if (fattr->fileid == 0 && fileid != 0)
3055                 fattr->fileid = fileid;
3056         if ((status = verify_attr_len(xdr, savep, attrlen)) == 0)
3057                 fattr->valid = NFS_ATTR_FATTR | NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4;
3058 xdr_error:
3059         dprintk("%s: xdr returned %d\n", __FUNCTION__, -status);
3060         return status;
3061 }
3062
3063
3064 static int decode_fsinfo(struct xdr_stream *xdr, struct nfs_fsinfo *fsinfo)
3065 {
3066         uint32_t *savep;
3067         uint32_t attrlen, bitmap[2];
3068         int status;
3069
3070         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3071                 goto xdr_error;
3072         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3073                 goto xdr_error;
3074         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3075                 goto xdr_error;
3076
3077         fsinfo->rtmult = fsinfo->wtmult = 512;  /* ??? */
3078
3079         if ((status = decode_attr_lease_time(xdr, bitmap, &fsinfo->lease_time)) != 0)
3080                 goto xdr_error;
3081         if ((status = decode_attr_maxfilesize(xdr, bitmap, &fsinfo->maxfilesize)) != 0)
3082                 goto xdr_error;
3083         if ((status = decode_attr_maxread(xdr, bitmap, &fsinfo->rtmax)) != 0)
3084                 goto xdr_error;
3085         fsinfo->rtpref = fsinfo->dtpref = fsinfo->rtmax;
3086         if ((status = decode_attr_maxwrite(xdr, bitmap, &fsinfo->wtmax)) != 0)
3087                 goto xdr_error;
3088         fsinfo->wtpref = fsinfo->wtmax;
3089
3090         status = verify_attr_len(xdr, savep, attrlen);
3091 xdr_error:
3092         dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
3093         return status;
3094 }
3095
3096 static int decode_getfh(struct xdr_stream *xdr, struct nfs_fh *fh)
3097 {
3098         uint32_t *p;
3099         uint32_t len;
3100         int status;
3101
3102         status = decode_op_hdr(xdr, OP_GETFH);
3103         if (status)
3104                 return status;
3105         /* Zero handle first to allow comparisons */
3106         memset(fh, 0, sizeof(*fh));
3107
3108         READ_BUF(4);
3109         READ32(len);
3110         if (len > NFS4_FHSIZE)
3111                 return -EIO;
3112         fh->size = len;
3113         READ_BUF(len);
3114         COPYMEM(fh->data, len);
3115         return 0;
3116 }
3117
3118 static int decode_link(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
3119 {
3120         int status;
3121         
3122         status = decode_op_hdr(xdr, OP_LINK);
3123         if (status)
3124                 return status;
3125         return decode_change_info(xdr, cinfo);
3126 }
3127
3128 /*
3129  * We create the owner, so we know a proper owner.id length is 4.
3130  */
3131 static int decode_lock_denied (struct xdr_stream *xdr, struct file_lock *fl)
3132 {
3133         uint64_t offset, length, clientid;
3134         uint32_t *p;
3135         uint32_t namelen, type;
3136
3137         READ_BUF(32);
3138         READ64(offset);
3139         READ64(length);
3140         READ32(type);
3141         if (fl != NULL) {
3142                 fl->fl_start = (loff_t)offset;
3143                 fl->fl_end = fl->fl_start + (loff_t)length - 1;
3144                 if (length == ~(uint64_t)0)
3145                         fl->fl_end = OFFSET_MAX;
3146                 fl->fl_type = F_WRLCK;
3147                 if (type & 1)
3148                         fl->fl_type = F_RDLCK;
3149                 fl->fl_pid = 0;
3150         }
3151         READ64(clientid);
3152         READ32(namelen);
3153         READ_BUF(namelen);
3154         return -NFS4ERR_DENIED;
3155 }
3156
3157 static int decode_lock(struct xdr_stream *xdr, struct nfs_lock_res *res)
3158 {
3159         uint32_t *p;
3160         int status;
3161
3162         status = decode_op_hdr(xdr, OP_LOCK);
3163         if (status == 0) {
3164                 READ_BUF(sizeof(res->stateid.data));
3165                 COPYMEM(res->stateid.data, sizeof(res->stateid.data));
3166         } else if (status == -NFS4ERR_DENIED)
3167                 return decode_lock_denied(xdr, NULL);
3168         return status;
3169 }
3170
3171 static int decode_lockt(struct xdr_stream *xdr, struct nfs_lockt_res *res)
3172 {
3173         int status;
3174         status = decode_op_hdr(xdr, OP_LOCKT);
3175         if (status == -NFS4ERR_DENIED)
3176                 return decode_lock_denied(xdr, res->denied);
3177         return status;
3178 }
3179
3180 static int decode_locku(struct xdr_stream *xdr, struct nfs_locku_res *res)
3181 {
3182         uint32_t *p;
3183         int status;
3184
3185         status = decode_op_hdr(xdr, OP_LOCKU);
3186         if (status == 0) {
3187                 READ_BUF(sizeof(res->stateid.data));
3188                 COPYMEM(res->stateid.data, sizeof(res->stateid.data));
3189         }
3190         return status;
3191 }
3192
3193 static int decode_lookup(struct xdr_stream *xdr)
3194 {
3195         return decode_op_hdr(xdr, OP_LOOKUP);
3196 }
3197
3198 /* This is too sick! */
3199 static int decode_space_limit(struct xdr_stream *xdr, u64 *maxsize)
3200 {
3201         uint32_t *p;
3202         uint32_t limit_type, nblocks, blocksize;
3203
3204         READ_BUF(12);
3205         READ32(limit_type);
3206         switch (limit_type) {
3207                 case 1:
3208                         READ64(*maxsize);
3209                         break;
3210                 case 2:
3211                         READ32(nblocks);
3212                         READ32(blocksize);
3213                         *maxsize = (uint64_t)nblocks * (uint64_t)blocksize;
3214         }
3215         return 0;
3216 }
3217
3218 static int decode_delegation(struct xdr_stream *xdr, struct nfs_openres *res)
3219 {
3220         uint32_t *p;
3221         uint32_t delegation_type;
3222
3223         READ_BUF(4);
3224         READ32(delegation_type);
3225         if (delegation_type == NFS4_OPEN_DELEGATE_NONE) {
3226                 res->delegation_type = 0;
3227                 return 0;
3228         }
3229         READ_BUF(20);
3230         COPYMEM(res->delegation.data, sizeof(res->delegation.data));
3231         READ32(res->do_recall);
3232         switch (delegation_type) {
3233                 case NFS4_OPEN_DELEGATE_READ:
3234                         res->delegation_type = FMODE_READ;
3235                         break;
3236                 case NFS4_OPEN_DELEGATE_WRITE:
3237                         res->delegation_type = FMODE_WRITE|FMODE_READ;
3238                         if (decode_space_limit(xdr, &res->maxsize) < 0)
3239                                 return -EIO;
3240         }
3241         return decode_ace(xdr, NULL, res->server->nfs4_state);
3242 }
3243
3244 static int decode_open(struct xdr_stream *xdr, struct nfs_openres *res)
3245 {
3246         uint32_t *p;
3247         uint32_t bmlen;
3248         int status;
3249
3250         status = decode_op_hdr(xdr, OP_OPEN);
3251         if (status)
3252                 return status;
3253         READ_BUF(sizeof(res->stateid.data));
3254         COPYMEM(res->stateid.data, sizeof(res->stateid.data));
3255
3256         decode_change_info(xdr, &res->cinfo);
3257
3258         READ_BUF(8);
3259         READ32(res->rflags);
3260         READ32(bmlen);
3261         if (bmlen > 10)
3262                 goto xdr_error;
3263
3264         READ_BUF(bmlen << 2);
3265         p += bmlen;
3266         return decode_delegation(xdr, res);
3267 xdr_error:
3268         dprintk("%s: Bitmap too large! Length = %u\n", __FUNCTION__, bmlen);
3269         return -EIO;
3270 }
3271
3272 static int decode_open_confirm(struct xdr_stream *xdr, struct nfs_open_confirmres *res)
3273 {
3274         uint32_t *p;
3275         int status;
3276
3277         status = decode_op_hdr(xdr, OP_OPEN_CONFIRM);
3278         if (status)
3279                 return status;
3280         READ_BUF(sizeof(res->stateid.data));
3281         COPYMEM(res->stateid.data, sizeof(res->stateid.data));
3282         return 0;
3283 }
3284
3285 static int decode_open_downgrade(struct xdr_stream *xdr, struct nfs_closeres *res)
3286 {
3287         uint32_t *p;
3288         int status;
3289
3290         status = decode_op_hdr(xdr, OP_OPEN_DOWNGRADE);
3291         if (status)
3292                 return status;
3293         READ_BUF(sizeof(res->stateid.data));
3294         COPYMEM(res->stateid.data, sizeof(res->stateid.data));
3295         return 0;
3296 }
3297
3298 static int decode_putfh(struct xdr_stream *xdr)
3299 {
3300         return decode_op_hdr(xdr, OP_PUTFH);
3301 }
3302
3303 static int decode_putrootfh(struct xdr_stream *xdr)
3304 {
3305         return decode_op_hdr(xdr, OP_PUTROOTFH);
3306 }
3307
3308 static int decode_read(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs_readres *res)
3309 {
3310         struct kvec *iov = req->rq_rcv_buf.head;
3311         uint32_t *p;
3312         uint32_t count, eof, recvd, hdrlen;
3313         int status;
3314
3315         status = decode_op_hdr(xdr, OP_READ);
3316         if (status)
3317                 return status;
3318         READ_BUF(8);
3319         READ32(eof);
3320         READ32(count);
3321         hdrlen = (u8 *) p - (u8 *) iov->iov_base;
3322         recvd = req->rq_rcv_buf.len - hdrlen;
3323         if (count > recvd) {
3324                 printk(KERN_WARNING "NFS: server cheating in read reply: "
3325                                 "count %u > recvd %u\n", count, recvd);
3326                 count = recvd;
3327                 eof = 0;
3328         }
3329         xdr_read_pages(xdr, count);
3330         res->eof = eof;
3331         res->count = count;
3332         return 0;
3333 }
3334
3335 static int decode_readdir(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs4_readdir_res *readdir)
3336 {
3337         struct xdr_buf  *rcvbuf = &req->rq_rcv_buf;
3338         struct page     *page = *rcvbuf->pages;
3339         struct kvec     *iov = rcvbuf->head;
3340         unsigned int    nr, pglen = rcvbuf->page_len;
3341         uint32_t        *end, *entry, *p, *kaddr;
3342         uint32_t        len, attrlen;
3343         int             hdrlen, recvd, status;
3344
3345         status = decode_op_hdr(xdr, OP_READDIR);
3346         if (status)
3347                 return status;
3348         READ_BUF(8);
3349         COPYMEM(readdir->verifier.data, 8);
3350         dprintk("%s: verifier = 0x%x%x\n",
3351                         __FUNCTION__,
3352                         ((u32 *)readdir->verifier.data)[0],
3353                         ((u32 *)readdir->verifier.data)[1]);
3354
3355
3356         hdrlen = (char *) p - (char *) iov->iov_base;
3357         recvd = rcvbuf->len - hdrlen;
3358         if (pglen > recvd)
3359                 pglen = recvd;
3360         xdr_read_pages(xdr, pglen);
3361
3362         BUG_ON(pglen + readdir->pgbase > PAGE_CACHE_SIZE);
3363         kaddr = p = (uint32_t *) kmap_atomic(page, KM_USER0);
3364         end = (uint32_t *) ((char *)p + pglen + readdir->pgbase);
3365         entry = p;
3366         for (nr = 0; *p++; nr++) {
3367                 if (p + 3 > end)
3368                         goto short_pkt;
3369                 dprintk("cookie = %Lu, ", *((unsigned long long *)p));
3370                 p += 2;                 /* cookie */
3371                 len = ntohl(*p++);      /* filename length */
3372                 if (len > NFS4_MAXNAMLEN) {
3373                         printk(KERN_WARNING "NFS: giant filename in readdir (len 0x%x)\n", len);
3374                         goto err_unmap;
3375                 }
3376                 dprintk("filename = %*s\n", len, (char *)p);
3377                 p += XDR_QUADLEN(len);
3378                 if (p + 1 > end)
3379                         goto short_pkt;
3380                 len = ntohl(*p++);      /* bitmap length */
3381                 p += len;
3382                 if (p + 1 > end)
3383                         goto short_pkt;
3384                 attrlen = XDR_QUADLEN(ntohl(*p++));
3385                 p += attrlen;           /* attributes */
3386                 if (p + 2 > end)
3387                         goto short_pkt;
3388                 entry = p;
3389         }
3390         if (!nr && (entry[0] != 0 || entry[1] == 0))
3391                 goto short_pkt;
3392 out:    
3393         kunmap_atomic(kaddr, KM_USER0);
3394         return 0;
3395 short_pkt:
3396         dprintk("%s: short packet at entry %d\n", __FUNCTION__, nr);
3397         entry[0] = entry[1] = 0;
3398         /* truncate listing ? */
3399         if (!nr) {
3400                 printk(KERN_NOTICE "NFS: readdir reply truncated!\n");
3401                 entry[1] = 1;
3402         }
3403         goto out;
3404 err_unmap:
3405         kunmap_atomic(kaddr, KM_USER0);
3406         return -errno_NFSERR_IO;
3407 }
3408
3409 static int decode_readlink(struct xdr_stream *xdr, struct rpc_rqst *req)
3410 {
3411         struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
3412         struct kvec *iov = rcvbuf->head;
3413         int hdrlen, len, recvd;
3414         uint32_t *p;
3415         char *kaddr;
3416         int status;
3417
3418         status = decode_op_hdr(xdr, OP_READLINK);
3419         if (status)
3420                 return status;
3421
3422         /* Convert length of symlink */
3423         READ_BUF(4);
3424         READ32(len);
3425         if (len >= rcvbuf->page_len || len <= 0) {
3426                 dprintk(KERN_WARNING "nfs: server returned giant symlink!\n");
3427                 return -ENAMETOOLONG;
3428         }
3429         hdrlen = (char *) xdr->p - (char *) iov->iov_base;
3430         recvd = req->rq_rcv_buf.len - hdrlen;
3431         if (recvd < len) {
3432                 printk(KERN_WARNING "NFS: server cheating in readlink reply: "
3433                                 "count %u > recvd %u\n", len, recvd);
3434                 return -EIO;
3435         }
3436         xdr_read_pages(xdr, len);
3437         /*
3438          * The XDR encode routine has set things up so that
3439          * the link text will be copied directly into the
3440          * buffer.  We just have to do overflow-checking,
3441          * and and null-terminate the text (the VFS expects
3442          * null-termination).
3443          */
3444         kaddr = (char *)kmap_atomic(rcvbuf->pages[0], KM_USER0);
3445         kaddr[len+rcvbuf->page_base] = '\0';
3446         kunmap_atomic(kaddr, KM_USER0);
3447         return 0;
3448 }
3449
3450 static int decode_remove(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
3451 {
3452         int status;
3453
3454         status = decode_op_hdr(xdr, OP_REMOVE);
3455         if (status)
3456                 goto out;
3457         status = decode_change_info(xdr, cinfo);
3458 out:
3459         return status;
3460 }
3461
3462 static int decode_rename(struct xdr_stream *xdr, struct nfs4_change_info *old_cinfo,
3463               struct nfs4_change_info *new_cinfo)
3464 {
3465         int status;
3466
3467         status = decode_op_hdr(xdr, OP_RENAME);
3468         if (status)
3469                 goto out;
3470         if ((status = decode_change_info(xdr, old_cinfo)))
3471                 goto out;
3472         status = decode_change_info(xdr, new_cinfo);
3473 out:
3474         return status;
3475 }
3476
3477 static int decode_renew(struct xdr_stream *xdr)
3478 {
3479         return decode_op_hdr(xdr, OP_RENEW);
3480 }
3481
3482 static int
3483 decode_restorefh(struct xdr_stream *xdr)
3484 {
3485         return decode_op_hdr(xdr, OP_RESTOREFH);
3486 }
3487
3488 static int decode_getacl(struct xdr_stream *xdr, struct rpc_rqst *req,
3489                 size_t *acl_len)
3490 {
3491         uint32_t *savep;
3492         uint32_t attrlen,
3493                  bitmap[2] = {0};
3494         struct kvec *iov = req->rq_rcv_buf.head;
3495         int status;
3496
3497         *acl_len = 0;
3498         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3499                 goto out;
3500         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3501                 goto out;
3502         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3503                 goto out;
3504
3505         if (unlikely(bitmap[0] & (FATTR4_WORD0_ACL - 1U)))
3506                 return -EIO;
3507         if (likely(bitmap[0] & FATTR4_WORD0_ACL)) {
3508                 int hdrlen, recvd;
3509
3510                 /* We ignore &savep and don't do consistency checks on
3511                  * the attr length.  Let userspace figure it out.... */
3512                 hdrlen = (u8 *)xdr->p - (u8 *)iov->iov_base;
3513                 recvd = req->rq_rcv_buf.len - hdrlen;
3514                 if (attrlen > recvd) {
3515                         printk(KERN_WARNING "NFS: server cheating in getattr"
3516                                         " acl reply: attrlen %u > recvd %u\n",
3517                                         attrlen, recvd);
3518                         return -EINVAL;
3519                 }
3520                 xdr_read_pages(xdr, attrlen);
3521                 *acl_len = attrlen;
3522         } else
3523                 status = -EOPNOTSUPP;
3524
3525 out:
3526         return status;
3527 }
3528
3529 static int
3530 decode_savefh(struct xdr_stream *xdr)
3531 {
3532         return decode_op_hdr(xdr, OP_SAVEFH);
3533 }
3534
3535 static int decode_setattr(struct xdr_stream *xdr, struct nfs_setattrres *res)
3536 {
3537         uint32_t *p;
3538         uint32_t bmlen;
3539         int status;
3540
3541         
3542         status = decode_op_hdr(xdr, OP_SETATTR);
3543         if (status)
3544                 return status;
3545         READ_BUF(4);
3546         READ32(bmlen);
3547         READ_BUF(bmlen << 2);
3548         return 0;
3549 }
3550
3551 static int decode_setclientid(struct xdr_stream *xdr, struct nfs4_client *clp)
3552 {
3553         uint32_t *p;
3554         uint32_t opnum;
3555         int32_t nfserr;
3556
3557         READ_BUF(8);
3558         READ32(opnum);
3559         if (opnum != OP_SETCLIENTID) {
3560                 printk(KERN_NOTICE
3561                                 "nfs4_decode_setclientid: Server returned operation"
3562                                 " %d\n", opnum);
3563                 return -EIO;
3564         }
3565         READ32(nfserr);
3566         if (nfserr == NFS_OK) {
3567                 READ_BUF(8 + sizeof(clp->cl_confirm.data));
3568                 READ64(clp->cl_clientid);
3569                 COPYMEM(clp->cl_confirm.data, sizeof(clp->cl_confirm.data));
3570         } else if (nfserr == NFSERR_CLID_INUSE) {
3571                 uint32_t len;
3572
3573                 /* skip netid string */
3574                 READ_BUF(4);
3575                 READ32(len);
3576                 READ_BUF(len);
3577
3578                 /* skip uaddr string */
3579                 READ_BUF(4);
3580                 READ32(len);
3581                 READ_BUF(len);
3582                 return -NFSERR_CLID_INUSE;
3583         } else
3584                 return -nfs_stat_to_errno(nfserr);
3585
3586         return 0;
3587 }
3588
3589 static int decode_setclientid_confirm(struct xdr_stream *xdr)
3590 {
3591         return decode_op_hdr(xdr, OP_SETCLIENTID_CONFIRM);
3592 }
3593
3594 static int decode_write(struct xdr_stream *xdr, struct nfs_writeres *res)
3595 {
3596         uint32_t *p;
3597         int status;
3598
3599         status = decode_op_hdr(xdr, OP_WRITE);
3600         if (status)
3601                 return status;
3602
3603         READ_BUF(16);
3604         READ32(res->count);
3605         READ32(res->verf->committed);
3606         COPYMEM(res->verf->verifier, 8);
3607         return 0;
3608 }
3609
3610 static int decode_delegreturn(struct xdr_stream *xdr)
3611 {
3612         return decode_op_hdr(xdr, OP_DELEGRETURN);
3613 }
3614
3615 /*
3616  * Decode OPEN_DOWNGRADE response
3617  */
3618 static int nfs4_xdr_dec_open_downgrade(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_closeres *res)
3619 {
3620         struct xdr_stream xdr;
3621         struct compound_hdr hdr;
3622         int status;
3623
3624         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3625         status = decode_compound_hdr(&xdr, &hdr);
3626         if (status)
3627                 goto out;
3628         status = decode_putfh(&xdr);
3629         if (status)
3630                 goto out;
3631         status = decode_open_downgrade(&xdr, res);
3632         if (status != 0)
3633                 goto out;
3634         decode_getfattr(&xdr, res->fattr, res->server);
3635 out:
3636         return status;
3637 }
3638
3639 /*
3640  * END OF "GENERIC" DECODE ROUTINES.
3641  */
3642
3643 /*
3644  * Decode ACCESS response
3645  */
3646 static int nfs4_xdr_dec_access(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_accessres *res)
3647 {
3648         struct xdr_stream xdr;
3649         struct compound_hdr hdr;
3650         int status;
3651         
3652         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3653         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3654                 goto out;
3655         if ((status = decode_putfh(&xdr)) == 0)
3656                 status = decode_access(&xdr, res);
3657 out:
3658         return status;
3659 }
3660
3661 /*
3662  * Decode LOOKUP response
3663  */
3664 static int nfs4_xdr_dec_lookup(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_lookup_res *res)
3665 {
3666         struct xdr_stream xdr;
3667         struct compound_hdr hdr;
3668         int status;
3669         
3670         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3671         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3672                 goto out;
3673         if ((status = decode_putfh(&xdr)) != 0)
3674                 goto out;
3675         if ((status = decode_lookup(&xdr)) != 0)
3676                 goto out;
3677         if ((status = decode_getfh(&xdr, res->fh)) != 0)
3678                 goto out;
3679         status = decode_getfattr(&xdr, res->fattr, res->server);
3680 out:
3681         return status;
3682 }
3683
3684 /*
3685  * Decode LOOKUP_ROOT response
3686  */
3687 static int nfs4_xdr_dec_lookup_root(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_lookup_res *res)
3688 {
3689         struct xdr_stream xdr;
3690         struct compound_hdr hdr;
3691         int status;
3692         
3693         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3694         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3695                 goto out;
3696         if ((status = decode_putrootfh(&xdr)) != 0)
3697                 goto out;
3698         if ((status = decode_getfh(&xdr, res->fh)) == 0)
3699                 status = decode_getfattr(&xdr, res->fattr, res->server);
3700 out:
3701         return status;
3702 }
3703
3704 /*
3705  * Decode REMOVE response
3706  */
3707 static int nfs4_xdr_dec_remove(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_remove_res *res)
3708 {
3709         struct xdr_stream xdr;
3710         struct compound_hdr hdr;
3711         int status;
3712         
3713         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3714         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3715                 goto out;
3716         if ((status = decode_putfh(&xdr)) != 0)
3717                 goto out;
3718         if ((status = decode_remove(&xdr, &res->cinfo)) != 0)
3719                 goto out;
3720         decode_getfattr(&xdr, res->dir_attr, res->server);
3721 out:
3722         return status;
3723 }
3724
3725 /*
3726  * Decode RENAME response
3727  */
3728 static int nfs4_xdr_dec_rename(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_rename_res *res)
3729 {
3730         struct xdr_stream xdr;
3731         struct compound_hdr hdr;
3732         int status;
3733         
3734         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3735         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3736                 goto out;
3737         if ((status = decode_putfh(&xdr)) != 0)
3738                 goto out;
3739         if ((status = decode_savefh(&xdr)) != 0)
3740                 goto out;
3741         if ((status = decode_putfh(&xdr)) != 0)
3742                 goto out;
3743         if ((status = decode_rename(&xdr, &res->old_cinfo, &res->new_cinfo)) != 0)
3744                 goto out;
3745         /* Current FH is target directory */
3746         if (decode_getfattr(&xdr, res->new_fattr, res->server) != 0)
3747                 goto out;
3748         if ((status = decode_restorefh(&xdr)) != 0)
3749                 goto out;
3750         decode_getfattr(&xdr, res->old_fattr, res->server);
3751 out:
3752         return status;
3753 }
3754
3755 /*
3756  * Decode LINK response
3757  */
3758 static int nfs4_xdr_dec_link(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_link_res *res)
3759 {
3760         struct xdr_stream xdr;
3761         struct compound_hdr hdr;
3762         int status;
3763         
3764         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3765         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3766                 goto out;
3767         if ((status = decode_putfh(&xdr)) != 0)
3768                 goto out;
3769         if ((status = decode_savefh(&xdr)) != 0)
3770                 goto out;
3771         if ((status = decode_putfh(&xdr)) != 0)
3772                 goto out;
3773         if ((status = decode_link(&xdr, &res->cinfo)) != 0)
3774                 goto out;
3775         /*
3776          * Note order: OP_LINK leaves the directory as the current
3777          *             filehandle.
3778          */
3779         if (decode_getfattr(&xdr, res->dir_attr, res->server) != 0)
3780                 goto out;
3781         if ((status = decode_restorefh(&xdr)) != 0)
3782                 goto out;
3783         decode_getfattr(&xdr, res->fattr, res->server);
3784 out:
3785         return status;
3786 }
3787
3788 /*
3789  * Decode CREATE response
3790  */
3791 static int nfs4_xdr_dec_create(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_create_res *res)
3792 {
3793         struct xdr_stream xdr;
3794         struct compound_hdr hdr;
3795         int status;
3796         
3797         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3798         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3799                 goto out;
3800         if ((status = decode_putfh(&xdr)) != 0)
3801                 goto out;
3802         if ((status = decode_savefh(&xdr)) != 0)
3803                 goto out;
3804         if ((status = decode_create(&xdr,&res->dir_cinfo)) != 0)
3805                 goto out;
3806         if ((status = decode_getfh(&xdr, res->fh)) != 0)
3807                 goto out;
3808         if (decode_getfattr(&xdr, res->fattr, res->server) != 0)
3809                 goto out;
3810         if ((status = decode_restorefh(&xdr)) != 0)
3811                 goto out;
3812         decode_getfattr(&xdr, res->dir_fattr, res->server);
3813 out:
3814         return status;
3815 }
3816
3817 /*
3818  * Decode SYMLINK response
3819  */
3820 static int nfs4_xdr_dec_symlink(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_create_res *res)
3821 {
3822         return nfs4_xdr_dec_create(rqstp, p, res);
3823 }
3824
3825 /*
3826  * Decode GETATTR response
3827  */
3828 static int nfs4_xdr_dec_getattr(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_getattr_res *res)
3829 {
3830         struct xdr_stream xdr;
3831         struct compound_hdr hdr;
3832         int status;
3833         
3834         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3835         status = decode_compound_hdr(&xdr, &hdr);
3836         if (status)
3837                 goto out;
3838         status = decode_putfh(&xdr);
3839         if (status)
3840                 goto out;
3841         status = decode_getfattr(&xdr, res->fattr, res->server);
3842 out:
3843         return status;
3844
3845 }
3846
3847 /*
3848  * Encode an SETACL request
3849  */
3850 static int
3851 nfs4_xdr_enc_setacl(struct rpc_rqst *req, uint32_t *p, struct nfs_setaclargs *args)
3852 {
3853         struct xdr_stream xdr;
3854         struct compound_hdr hdr = {
3855                 .nops   = 2,
3856         };
3857         int status;
3858
3859         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
3860         encode_compound_hdr(&xdr, &hdr);
3861         status = encode_putfh(&xdr, args->fh);
3862         if (status)
3863                 goto out;
3864         status = encode_setacl(&xdr, args);
3865 out:
3866         return status;
3867 }
3868 /*
3869  * Decode SETACL response
3870  */
3871 static int
3872 nfs4_xdr_dec_setacl(struct rpc_rqst *rqstp, uint32_t *p, void *res)
3873 {
3874         struct xdr_stream xdr;
3875         struct compound_hdr hdr;
3876         int status;
3877
3878         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3879         status = decode_compound_hdr(&xdr, &hdr);
3880         if (status)
3881                 goto out;
3882         status = decode_putfh(&xdr);
3883         if (status)
3884                 goto out;
3885         status = decode_setattr(&xdr, res);
3886 out:
3887         return status;
3888 }
3889
3890 /*
3891  * Decode GETACL response
3892  */
3893 static int
3894 nfs4_xdr_dec_getacl(struct rpc_rqst *rqstp, uint32_t *p, size_t *acl_len)
3895 {
3896         struct xdr_stream xdr;
3897         struct compound_hdr hdr;
3898         int status;
3899
3900         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3901         status = decode_compound_hdr(&xdr, &hdr);
3902         if (status)
3903                 goto out;
3904         status = decode_putfh(&xdr);
3905         if (status)
3906                 goto out;
3907         status = decode_getacl(&xdr, rqstp, acl_len);
3908
3909 out:
3910         return status;
3911 }
3912
3913 /*
3914  * Decode CLOSE response
3915  */
3916 static int nfs4_xdr_dec_close(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_closeres *res)
3917 {
3918         struct xdr_stream xdr;
3919         struct compound_hdr hdr;
3920         int status;
3921
3922         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3923         status = decode_compound_hdr(&xdr, &hdr);
3924         if (status)
3925                 goto out;
3926         status = decode_putfh(&xdr);
3927         if (status)
3928                 goto out;
3929         status = decode_close(&xdr, res);
3930         if (status != 0)
3931                 goto out;
3932         /*
3933          * Note: Server may do delete on close for this file
3934          *      in which case the getattr call will fail with
3935          *      an ESTALE error. Shouldn't be a problem,
3936          *      though, since fattr->valid will remain unset.
3937          */
3938         decode_getfattr(&xdr, res->fattr, res->server);
3939 out:
3940         return status;
3941 }
3942
3943 /*
3944  * Decode OPEN response
3945  */
3946 static int nfs4_xdr_dec_open(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_openres *res)
3947 {
3948         struct xdr_stream xdr;
3949         struct compound_hdr hdr;
3950         int status;
3951
3952         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3953         status = decode_compound_hdr(&xdr, &hdr);
3954         if (status)
3955                 goto out;
3956         status = decode_putfh(&xdr);
3957         if (status)
3958                 goto out;
3959         status = decode_savefh(&xdr);
3960         if (status)
3961                 goto out;
3962         status = decode_open(&xdr, res);
3963         if (status)
3964                 goto out;
3965         status = decode_getfh(&xdr, &res->fh);
3966         if (status)
3967                 goto out;
3968         if (decode_getfattr(&xdr, res->f_attr, res->server) != 0)
3969                 goto out;
3970         if ((status = decode_restorefh(&xdr)) != 0)
3971                 goto out;
3972         decode_getfattr(&xdr, res->dir_attr, res->server);
3973 out:
3974         return status;
3975 }
3976
3977 /*
3978  * Decode OPEN_CONFIRM response
3979  */
3980 static int nfs4_xdr_dec_open_confirm(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_open_confirmres *res)
3981 {
3982         struct xdr_stream xdr;
3983         struct compound_hdr hdr;
3984         int status;
3985
3986         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3987         status = decode_compound_hdr(&xdr, &hdr);
3988         if (status)
3989                 goto out;
3990         status = decode_putfh(&xdr);
3991         if (status)
3992                 goto out;
3993         status = decode_open_confirm(&xdr, res);
3994 out:
3995         return status;
3996 }
3997
3998 /*
3999  * Decode OPEN response
4000  */
4001 static int nfs4_xdr_dec_open_noattr(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_openres *res)
4002 {
4003         struct xdr_stream xdr;
4004         struct compound_hdr hdr;
4005         int status;
4006
4007         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4008         status = decode_compound_hdr(&xdr, &hdr);
4009         if (status)
4010                 goto out;
4011         status = decode_putfh(&xdr);
4012         if (status)
4013                 goto out;
4014         status = decode_open(&xdr, res);
4015         if (status)
4016                 goto out;
4017         decode_getfattr(&xdr, res->f_attr, res->server);
4018 out:
4019         return status;
4020 }
4021
4022 /*
4023  * Decode SETATTR response
4024  */
4025 static int nfs4_xdr_dec_setattr(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_setattrres *res)
4026 {
4027         struct xdr_stream xdr;
4028         struct compound_hdr hdr;
4029         int status;
4030
4031         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4032         status = decode_compound_hdr(&xdr, &hdr);
4033         if (status)
4034                 goto out;
4035         status = decode_putfh(&xdr);
4036         if (status)
4037                 goto out;
4038         status = decode_setattr(&xdr, res);
4039         if (status)
4040                 goto out;
4041         status = decode_getfattr(&xdr, res->fattr, res->server);
4042         if (status == NFS4ERR_DELAY)
4043                 status = 0;
4044 out:
4045         return status;
4046 }
4047
4048 /*
4049  * Decode LOCK response
4050  */
4051 static int nfs4_xdr_dec_lock(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_lock_res *res)
4052 {
4053         struct xdr_stream xdr;
4054         struct compound_hdr hdr;
4055         int status;
4056
4057         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4058         status = decode_compound_hdr(&xdr, &hdr);
4059         if (status)
4060                 goto out;
4061         status = decode_putfh(&xdr);
4062         if (status)
4063                 goto out;
4064         status = decode_lock(&xdr, res);
4065 out:
4066         return status;
4067 }
4068
4069 /*
4070  * Decode LOCKT response
4071  */
4072 static int nfs4_xdr_dec_lockt(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_lockt_res *res)
4073 {
4074         struct xdr_stream xdr;
4075         struct compound_hdr hdr;
4076         int status;
4077
4078         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4079         status = decode_compound_hdr(&xdr, &hdr);
4080         if (status)
4081                 goto out;
4082         status = decode_putfh(&xdr);
4083         if (status)
4084                 goto out;
4085         status = decode_lockt(&xdr, res);
4086 out:
4087         return status;
4088 }
4089
4090 /*
4091  * Decode LOCKU response
4092  */
4093 static int nfs4_xdr_dec_locku(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_locku_res *res)
4094 {
4095         struct xdr_stream xdr;
4096         struct compound_hdr hdr;
4097         int status;
4098
4099         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4100         status = decode_compound_hdr(&xdr, &hdr);
4101         if (status)
4102                 goto out;
4103         status = decode_putfh(&xdr);
4104         if (status)
4105                 goto out;
4106         status = decode_locku(&xdr, res);
4107 out:
4108         return status;
4109 }
4110
4111 /*
4112  * Decode READLINK response
4113  */
4114 static int nfs4_xdr_dec_readlink(struct rpc_rqst *rqstp, uint32_t *p, void *res)
4115 {
4116         struct xdr_stream xdr;
4117         struct compound_hdr hdr;
4118         int status;
4119
4120         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4121         status = decode_compound_hdr(&xdr, &hdr);
4122         if (status)
4123                 goto out;
4124         status = decode_putfh(&xdr);
4125         if (status)
4126                 goto out;
4127         status = decode_readlink(&xdr, rqstp);
4128 out:
4129         return status;
4130 }
4131
4132 /*
4133  * Decode READDIR response
4134  */
4135 static int nfs4_xdr_dec_readdir(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_readdir_res *res)
4136 {
4137         struct xdr_stream xdr;
4138         struct compound_hdr hdr;
4139         int status;
4140
4141         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4142         status = decode_compound_hdr(&xdr, &hdr);
4143         if (status)
4144                 goto out;
4145         status = decode_putfh(&xdr);
4146         if (status)
4147                 goto out;
4148         status = decode_readdir(&xdr, rqstp, res);
4149 out:
4150         return status;
4151 }
4152
4153 /*
4154  * Decode Read response
4155  */
4156 static int nfs4_xdr_dec_read(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_readres *res)
4157 {
4158         struct xdr_stream xdr;
4159         struct compound_hdr hdr;
4160         int status;
4161
4162         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4163         status = decode_compound_hdr(&xdr, &hdr);
4164         if (status)
4165                 goto out;
4166         status = decode_putfh(&xdr);
4167         if (status)
4168                 goto out;
4169         status = decode_read(&xdr, rqstp, res);
4170         if (!status)
4171                 status = res->count;
4172 out:
4173         return status;
4174 }
4175
4176 /*
4177  * Decode WRITE response
4178  */
4179 static int nfs4_xdr_dec_write(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_writeres *res)
4180 {
4181         struct xdr_stream xdr;
4182         struct compound_hdr hdr;
4183         int status;
4184
4185         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4186         status = decode_compound_hdr(&xdr, &hdr);
4187         if (status)
4188                 goto out;
4189         status = decode_putfh(&xdr);
4190         if (status)
4191                 goto out;
4192         status = decode_write(&xdr, res);
4193         if (status)
4194                 goto out;
4195         decode_getfattr(&xdr, res->fattr, res->server);
4196         if (!status)
4197                 status = res->count;
4198 out:
4199         return status;
4200 }
4201
4202 /*
4203  * Decode COMMIT response
4204  */
4205 static int nfs4_xdr_dec_commit(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_writeres *res)
4206 {
4207         struct xdr_stream xdr;
4208         struct compound_hdr hdr;
4209         int status;
4210
4211         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4212         status = decode_compound_hdr(&xdr, &hdr);
4213         if (status)
4214                 goto out;
4215         status = decode_putfh(&xdr);
4216         if (status)
4217                 goto out;
4218         status = decode_commit(&xdr, res);
4219         if (status)
4220                 goto out;
4221         decode_getfattr(&xdr, res->fattr, res->server);
4222 out:
4223         return status;
4224 }
4225
4226 /*
4227  * FSINFO request
4228  */
4229 static int nfs4_xdr_dec_fsinfo(struct rpc_rqst *req, uint32_t *p, struct nfs_fsinfo *fsinfo)
4230 {
4231         struct xdr_stream xdr;
4232         struct compound_hdr hdr;
4233         int status;
4234
4235         xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4236         status = decode_compound_hdr(&xdr, &hdr);
4237         if (!status)
4238                 status = decode_putfh(&xdr);
4239         if (!status)
4240                 status = decode_fsinfo(&xdr, fsinfo);
4241         if (!status)
4242                 status = -nfs_stat_to_errno(hdr.status);
4243         return status;
4244 }
4245
4246 /*
4247  * PATHCONF request
4248  */
4249 static int nfs4_xdr_dec_pathconf(struct rpc_rqst *req, uint32_t *p, struct nfs_pathconf *pathconf)
4250 {
4251         struct xdr_stream xdr;
4252         struct compound_hdr hdr;
4253         int status;
4254
4255         xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4256         status = decode_compound_hdr(&xdr, &hdr);
4257         if (!status)
4258                 status = decode_putfh(&xdr);
4259         if (!status)
4260                 status = decode_pathconf(&xdr, pathconf);
4261         return status;
4262 }
4263
4264 /*
4265  * STATFS request
4266  */
4267 static int nfs4_xdr_dec_statfs(struct rpc_rqst *req, uint32_t *p, struct nfs_fsstat *fsstat)
4268 {
4269         struct xdr_stream xdr;
4270         struct compound_hdr hdr;
4271         int status;
4272
4273         xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4274         status = decode_compound_hdr(&xdr, &hdr);
4275         if (!status)
4276                 status = decode_putfh(&xdr);
4277         if (!status)
4278                 status = decode_statfs(&xdr, fsstat);
4279         return status;
4280 }
4281
4282 /*
4283  * GETATTR_BITMAP request
4284  */
4285 static int nfs4_xdr_dec_server_caps(struct rpc_rqst *req, uint32_t *p, struct nfs4_server_caps_res *res)
4286 {
4287         struct xdr_stream xdr;
4288         struct compound_hdr hdr;
4289         int status;
4290
4291         xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4292         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
4293                 goto out;
4294         if ((status = decode_putfh(&xdr)) != 0)
4295                 goto out;
4296         status = decode_server_caps(&xdr, res);
4297 out:
4298         return status;
4299 }
4300
4301 /*
4302  * Decode RENEW response
4303  */
4304 static int nfs4_xdr_dec_renew(struct rpc_rqst *rqstp, uint32_t *p, void *dummy)
4305 {
4306         struct xdr_stream xdr;
4307         struct compound_hdr hdr;
4308         int status;
4309
4310         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4311         status = decode_compound_hdr(&xdr, &hdr);
4312         if (!status)
4313                 status = decode_renew(&xdr);
4314         return status;
4315 }
4316
4317 /*
4318  * a SETCLIENTID request
4319  */
4320 static int nfs4_xdr_dec_setclientid(struct rpc_rqst *req, uint32_t *p,
4321                 struct nfs4_client *clp)
4322 {
4323         struct xdr_stream xdr;
4324         struct compound_hdr hdr;
4325         int status;
4326
4327         xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4328         status = decode_compound_hdr(&xdr, &hdr);
4329         if (!status)
4330                 status = decode_setclientid(&xdr, clp);
4331         if (!status)
4332                 status = -nfs_stat_to_errno(hdr.status);
4333         return status;
4334 }
4335
4336 /*
4337  * a SETCLIENTID_CONFIRM request
4338  */
4339 static int nfs4_xdr_dec_setclientid_confirm(struct rpc_rqst *req, uint32_t *p, struct nfs_fsinfo *fsinfo)
4340 {
4341         struct xdr_stream xdr;
4342         struct compound_hdr hdr;
4343         int status;
4344
4345         xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4346         status = decode_compound_hdr(&xdr, &hdr);
4347         if (!status)
4348                 status = decode_setclientid_confirm(&xdr);
4349         if (!status)
4350                 status = decode_putrootfh(&xdr);
4351         if (!status)
4352                 status = decode_fsinfo(&xdr, fsinfo);
4353         if (!status)
4354                 status = -nfs_stat_to_errno(hdr.status);
4355         return status;
4356 }
4357
4358 /*
4359  * DELEGRETURN request
4360  */
4361 static int nfs4_xdr_dec_delegreturn(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_delegreturnres *res)
4362 {
4363         struct xdr_stream xdr;
4364         struct compound_hdr hdr;
4365         int status;
4366
4367         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4368         status = decode_compound_hdr(&xdr, &hdr);
4369         if (status != 0)
4370                 goto out;
4371         status = decode_putfh(&xdr);
4372         if (status != 0)
4373                 goto out;
4374         status = decode_delegreturn(&xdr);
4375         decode_getfattr(&xdr, res->fattr, res->server);
4376 out:
4377         return status;
4378 }
4379
4380 /*
4381  * FS_LOCATIONS request
4382  */
4383 static int nfs4_xdr_dec_fs_locations(struct rpc_rqst *req, uint32_t *p, struct nfs4_fs_locations *res)
4384 {
4385         struct xdr_stream xdr;
4386         struct compound_hdr hdr;
4387         int status;
4388
4389         xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4390         status = decode_compound_hdr(&xdr, &hdr);
4391         if (status != 0)
4392                 goto out;
4393         if ((status = decode_putfh(&xdr)) != 0)
4394                 goto out;
4395         if ((status = decode_lookup(&xdr)) != 0)
4396                 goto out;
4397         xdr_enter_page(&xdr, PAGE_SIZE);
4398         status = decode_getfattr(&xdr, &res->fattr, res->server);
4399 out:
4400         return status;
4401 }
4402
4403 uint32_t *nfs4_decode_dirent(uint32_t *p, struct nfs_entry *entry, int plus)
4404 {
4405         uint32_t bitmap[2] = {0};
4406         uint32_t len;
4407
4408         if (!*p++) {
4409                 if (!*p)
4410                         return ERR_PTR(-EAGAIN);
4411                 entry->eof = 1;
4412                 return ERR_PTR(-EBADCOOKIE);
4413         }
4414
4415         entry->prev_cookie = entry->cookie;
4416         p = xdr_decode_hyper(p, &entry->cookie);
4417         entry->len = ntohl(*p++);
4418         entry->name = (const char *) p;
4419         p += XDR_QUADLEN(entry->len);
4420
4421         /*
4422          * In case the server doesn't return an inode number,
4423          * we fake one here.  (We don't use inode number 0,
4424          * since glibc seems to choke on it...)
4425          */
4426         entry->ino = 1;
4427
4428         len = ntohl(*p++);              /* bitmap length */
4429         if (len-- > 0) {
4430                 bitmap[0] = ntohl(*p++);
4431                 if (len-- > 0) {
4432                         bitmap[1] = ntohl(*p++);
4433                         p += len;
4434                 }
4435         }
4436         len = XDR_QUADLEN(ntohl(*p++)); /* attribute buffer length */
4437         if (len > 0) {
4438                 if (bitmap[0] & FATTR4_WORD0_RDATTR_ERROR) {
4439                         bitmap[0] &= ~FATTR4_WORD0_RDATTR_ERROR;
4440                         /* Ignore the return value of rdattr_error for now */
4441                         p++;
4442                         len--;
4443                 }
4444                 if (bitmap[0] == 0 && bitmap[1] == FATTR4_WORD1_MOUNTED_ON_FILEID)
4445                         xdr_decode_hyper(p, &entry->ino);
4446                 else if (bitmap[0] == FATTR4_WORD0_FILEID)
4447                         xdr_decode_hyper(p, &entry->ino);
4448                 p += len;
4449         }
4450
4451         entry->eof = !p[0] && p[1];
4452         return p;
4453 }
4454
4455 /*
4456  * We need to translate between nfs status return values and
4457  * the local errno values which may not be the same.
4458  */
4459 static struct {
4460         int stat;
4461         int errno;
4462 } nfs_errtbl[] = {
4463         { NFS4_OK,              0               },
4464         { NFS4ERR_PERM,         EPERM           },
4465         { NFS4ERR_NOENT,        ENOENT          },
4466         { NFS4ERR_IO,           errno_NFSERR_IO },
4467         { NFS4ERR_NXIO,         ENXIO           },
4468         { NFS4ERR_ACCESS,       EACCES          },
4469         { NFS4ERR_EXIST,        EEXIST          },
4470         { NFS4ERR_XDEV,         EXDEV           },
4471         { NFS4ERR_NOTDIR,       ENOTDIR         },
4472         { NFS4ERR_ISDIR,        EISDIR          },
4473         { NFS4ERR_INVAL,        EINVAL          },
4474         { NFS4ERR_FBIG,         EFBIG           },
4475         { NFS4ERR_NOSPC,        ENOSPC          },
4476         { NFS4ERR_ROFS,         EROFS           },
4477         { NFS4ERR_MLINK,        EMLINK          },
4478         { NFS4ERR_NAMETOOLONG,  ENAMETOOLONG    },
4479         { NFS4ERR_NOTEMPTY,     ENOTEMPTY       },
4480         { NFS4ERR_DQUOT,        EDQUOT          },
4481         { NFS4ERR_STALE,        ESTALE          },
4482         { NFS4ERR_BADHANDLE,    EBADHANDLE      },
4483         { NFS4ERR_BADOWNER,     EINVAL          },
4484         { NFS4ERR_BADNAME,      EINVAL          },
4485         { NFS4ERR_BAD_COOKIE,   EBADCOOKIE      },
4486         { NFS4ERR_NOTSUPP,      ENOTSUPP        },
4487         { NFS4ERR_TOOSMALL,     ETOOSMALL       },
4488         { NFS4ERR_SERVERFAULT,  ESERVERFAULT    },
4489         { NFS4ERR_BADTYPE,      EBADTYPE        },
4490         { NFS4ERR_LOCKED,       EAGAIN          },
4491         { NFS4ERR_RESOURCE,     EREMOTEIO       },
4492         { NFS4ERR_SYMLINK,      ELOOP           },
4493         { NFS4ERR_OP_ILLEGAL,   EOPNOTSUPP      },
4494         { NFS4ERR_DEADLOCK,     EDEADLK         },
4495         { NFS4ERR_WRONGSEC,     EPERM           }, /* FIXME: this needs
4496                                                     * to be handled by a
4497                                                     * middle-layer.
4498                                                     */
4499         { -1,                   EIO             }
4500 };
4501
4502 /*
4503  * Convert an NFS error code to a local one.
4504  * This one is used jointly by NFSv2 and NFSv3.
4505  */
4506 static int
4507 nfs_stat_to_errno(int stat)
4508 {
4509         int i;
4510         for (i = 0; nfs_errtbl[i].stat != -1; i++) {
4511                 if (nfs_errtbl[i].stat == stat)
4512                         return nfs_errtbl[i].errno;
4513         }
4514         if (stat <= 10000 || stat > 10100) {
4515                 /* The server is looney tunes. */
4516                 return ESERVERFAULT;
4517         }
4518         /* If we cannot translate the error, the recovery routines should
4519          * handle it.
4520          * Note: remaining NFSv4 error codes have values > 10000, so should
4521          * not conflict with native Linux error codes.
4522          */
4523         return stat;
4524 }
4525
4526 #ifndef MAX
4527 # define MAX(a, b)      (((a) > (b))? (a) : (b))
4528 #endif
4529
4530 #define PROC(proc, argtype, restype)                            \
4531 [NFSPROC4_CLNT_##proc] = {                                      \
4532         .p_proc   = NFSPROC4_COMPOUND,                          \
4533         .p_encode = (kxdrproc_t) nfs4_xdr_##argtype,            \
4534         .p_decode = (kxdrproc_t) nfs4_xdr_##restype,            \
4535         .p_bufsiz = MAX(NFS4_##argtype##_sz,NFS4_##restype##_sz) << 2,  \
4536         .p_statidx = NFSPROC4_CLNT_##proc,                      \
4537         .p_name   = #proc,                                      \
4538     }
4539
4540 struct rpc_procinfo     nfs4_procedures[] = {
4541   PROC(READ,            enc_read,       dec_read),
4542   PROC(WRITE,           enc_write,      dec_write),
4543   PROC(COMMIT,          enc_commit,     dec_commit),
4544   PROC(OPEN,            enc_open,       dec_open),
4545   PROC(OPEN_CONFIRM,    enc_open_confirm,       dec_open_confirm),
4546   PROC(OPEN_NOATTR,     enc_open_noattr,        dec_open_noattr),
4547   PROC(OPEN_DOWNGRADE,  enc_open_downgrade,     dec_open_downgrade),
4548   PROC(CLOSE,           enc_close,      dec_close),
4549   PROC(SETATTR,         enc_setattr,    dec_setattr),
4550   PROC(FSINFO,          enc_fsinfo,     dec_fsinfo),
4551   PROC(RENEW,           enc_renew,      dec_renew),
4552   PROC(SETCLIENTID,     enc_setclientid,        dec_setclientid),
4553   PROC(SETCLIENTID_CONFIRM,     enc_setclientid_confirm,        dec_setclientid_confirm),
4554   PROC(LOCK,            enc_lock,       dec_lock),
4555   PROC(LOCKT,           enc_lockt,      dec_lockt),
4556   PROC(LOCKU,           enc_locku,      dec_locku),
4557   PROC(ACCESS,          enc_access,     dec_access),
4558   PROC(GETATTR,         enc_getattr,    dec_getattr),
4559   PROC(LOOKUP,          enc_lookup,     dec_lookup),
4560   PROC(LOOKUP_ROOT,     enc_lookup_root,        dec_lookup_root),
4561   PROC(REMOVE,          enc_remove,     dec_remove),
4562   PROC(RENAME,          enc_rename,     dec_rename),
4563   PROC(LINK,            enc_link,       dec_link),
4564   PROC(SYMLINK,         enc_symlink,    dec_symlink),
4565   PROC(CREATE,          enc_create,     dec_create),
4566   PROC(PATHCONF,        enc_pathconf,   dec_pathconf),
4567   PROC(STATFS,          enc_statfs,     dec_statfs),
4568   PROC(READLINK,        enc_readlink,   dec_readlink),
4569   PROC(READDIR,         enc_readdir,    dec_readdir),
4570   PROC(SERVER_CAPS,     enc_server_caps, dec_server_caps),
4571   PROC(DELEGRETURN,     enc_delegreturn, dec_delegreturn),
4572   PROC(GETACL,          enc_getacl,     dec_getacl),
4573   PROC(SETACL,          enc_setacl,     dec_setacl),
4574   PROC(FS_LOCATIONS,    enc_fs_locations, dec_fs_locations),
4575 };
4576
4577 struct rpc_version              nfs_version4 = {
4578         .number                 = 4,
4579         .nrprocs                = ARRAY_SIZE(nfs4_procedures),
4580         .procs                  = nfs4_procedures
4581 };
4582
4583 /*
4584  * Local variables:
4585  *  c-basic-offset: 8
4586  * End:
4587  */