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