]> err.no Git - linux-2.6/blob - fs/nfs/nfs4xdr.c
NFSv4: Fix a potential CLOSE race
[linux-2.6] / fs / nfs / nfs4xdr.c
1 /*
2  *  fs/nfs/nfs4xdr.c
3  *
4  *  Client-side XDR for NFSv4.
5  *
6  *  Copyright (c) 2002 The Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  Kendrick Smith <kmsmith@umich.edu>
10  *  Andy Adamson   <andros@umich.edu>
11  * 
12  *  Redistribution and use in source and binary forms, with or without
13  *  modification, are permitted provided that the following conditions
14  *  are met:
15  *
16  *  1. Redistributions of source code must retain the above copyright
17  *     notice, this list of conditions and the following disclaimer.
18  *  2. Redistributions in binary form must reproduce the above copyright
19  *     notice, this list of conditions and the following disclaimer in the
20  *     documentation and/or other materials provided with the distribution.
21  *  3. Neither the name of the University nor the names of its
22  *     contributors may be used to endorse or promote products derived
23  *     from this software without specific prior written permission.
24  *
25  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37
38 #include <linux/param.h>
39 #include <linux/time.h>
40 #include <linux/mm.h>
41 #include <linux/slab.h>
42 #include <linux/utsname.h>
43 #include <linux/errno.h>
44 #include <linux/string.h>
45 #include <linux/in.h>
46 #include <linux/pagemap.h>
47 #include <linux/proc_fs.h>
48 #include <linux/kdev_t.h>
49 #include <linux/sunrpc/clnt.h>
50 #include <linux/nfs.h>
51 #include <linux/nfs4.h>
52 #include <linux/nfs_fs.h>
53 #include <linux/nfs_idmap.h>
54 #include "nfs4_fs.h"
55
56 #define NFSDBG_FACILITY         NFSDBG_XDR
57
58 /* Mapping from NFS error code to "errno" error code. */
59 #define errno_NFSERR_IO         EIO
60
61 static int nfs_stat_to_errno(int);
62
63 /* NFSv4 COMPOUND tags are only wanted for debugging purposes */
64 #ifdef DEBUG
65 #define NFS4_MAXTAGLEN          20
66 #else
67 #define NFS4_MAXTAGLEN          0
68 #endif
69
70 /* lock,open owner id: 
71  * we currently use size 1 (u32) out of (NFS4_OPAQUE_LIMIT  >> 2)
72  */
73 #define owner_id_maxsz          (1 + 1)
74 #define compound_encode_hdr_maxsz       (3 + (NFS4_MAXTAGLEN >> 2))
75 #define compound_decode_hdr_maxsz       (3 + (NFS4_MAXTAGLEN >> 2))
76 #define op_encode_hdr_maxsz     (1)
77 #define op_decode_hdr_maxsz     (2)
78 #define encode_putfh_maxsz      (op_encode_hdr_maxsz + 1 + \
79                                 (NFS4_FHSIZE >> 2))
80 #define decode_putfh_maxsz      (op_decode_hdr_maxsz)
81 #define encode_putrootfh_maxsz  (op_encode_hdr_maxsz)
82 #define decode_putrootfh_maxsz  (op_decode_hdr_maxsz)
83 #define encode_getfh_maxsz      (op_encode_hdr_maxsz)
84 #define decode_getfh_maxsz      (op_decode_hdr_maxsz + 1 + \
85                                 ((3+NFS4_FHSIZE) >> 2))
86 #define nfs4_fattr_bitmap_maxsz 3
87 #define encode_getattr_maxsz    (op_encode_hdr_maxsz + nfs4_fattr_bitmap_maxsz)
88 #define nfs4_name_maxsz         (1 + ((3 + NFS4_MAXNAMLEN) >> 2))
89 #define nfs4_path_maxsz         (1 + ((3 + NFS4_MAXPATHLEN) >> 2))
90 /* This is based on getfattr, which uses the most attributes: */
91 #define nfs4_fattr_value_maxsz  (1 + (1 + 2 + 2 + 4 + 2 + 1 + 1 + 2 + 2 + \
92                                 3 + 3 + 3 + 2 * nfs4_name_maxsz))
93 #define nfs4_fattr_maxsz        (nfs4_fattr_bitmap_maxsz + \
94                                 nfs4_fattr_value_maxsz)
95 #define decode_getattr_maxsz    (op_decode_hdr_maxsz + nfs4_fattr_maxsz)
96 #define encode_savefh_maxsz     (op_encode_hdr_maxsz)
97 #define decode_savefh_maxsz     (op_decode_hdr_maxsz)
98 #define encode_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->sequence->counter);
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->sequence->counter);
736                 WRITEMEM(&ol->open_stateid, sizeof(ol->open_stateid));
737                 WRITE32(ol->lock_seqid->sequence->counter);
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->sequence->counter);
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->sequence->counter);
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->sequence->counter);
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->sequence->counter);
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->sequence->counter);
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         uint32_t attrs[2] = {
1018                 FATTR4_WORD0_RDATTR_ERROR|FATTR4_WORD0_FILEID,
1019                 FATTR4_WORD1_MOUNTED_ON_FILEID,
1020         };
1021         int replen;
1022         uint32_t *p;
1023
1024         RESERVE_SPACE(32+sizeof(nfs4_verifier));
1025         WRITE32(OP_READDIR);
1026         WRITE64(readdir->cookie);
1027         WRITEMEM(readdir->verifier.data, sizeof(readdir->verifier.data));
1028         WRITE32(readdir->count >> 1);  /* We're not doing readdirplus */
1029         WRITE32(readdir->count);
1030         WRITE32(2);
1031         /* Switch to mounted_on_fileid if the server supports it */
1032         if (readdir->bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
1033                 attrs[0] &= ~FATTR4_WORD0_FILEID;
1034         else
1035                 attrs[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
1036         WRITE32(attrs[0] & readdir->bitmask[0]);
1037         WRITE32(attrs[1] & readdir->bitmask[1]);
1038         dprintk("%s: cookie = %Lu, verifier = 0x%x%x, bitmap = 0x%x%x\n",
1039                         __FUNCTION__,
1040                         (unsigned long long)readdir->cookie,
1041                         ((u32 *)readdir->verifier.data)[0],
1042                         ((u32 *)readdir->verifier.data)[1],
1043                         attrs[0] & readdir->bitmask[0],
1044                         attrs[1] & readdir->bitmask[1]);
1045
1046         /* set up reply kvec
1047          *    toplevel_status + taglen + rescount + OP_PUTFH + status
1048          *      + OP_READDIR + status + verifer(2)  = 9
1049          */
1050         replen = (RPC_REPHDRSIZE + auth->au_rslack + 9) << 2;
1051         xdr_inline_pages(&req->rq_rcv_buf, replen, readdir->pages,
1052                          readdir->pgbase, readdir->count);
1053         dprintk("%s: inlined page args = (%u, %p, %u, %u)\n",
1054                         __FUNCTION__, replen, readdir->pages,
1055                         readdir->pgbase, readdir->count);
1056
1057         return 0;
1058 }
1059
1060 static int encode_readlink(struct xdr_stream *xdr, const struct nfs4_readlink *readlink, struct rpc_rqst *req)
1061 {
1062         struct rpc_auth *auth = req->rq_task->tk_auth;
1063         unsigned int replen;
1064         uint32_t *p;
1065
1066         RESERVE_SPACE(4);
1067         WRITE32(OP_READLINK);
1068
1069         /* set up reply kvec
1070          *    toplevel_status + taglen + rescount + OP_PUTFH + status
1071          *      + OP_READLINK + status + string length = 8
1072          */
1073         replen = (RPC_REPHDRSIZE + auth->au_rslack + 8) << 2;
1074         xdr_inline_pages(&req->rq_rcv_buf, replen, readlink->pages,
1075                         readlink->pgbase, readlink->pglen);
1076         
1077         return 0;
1078 }
1079
1080 static int encode_remove(struct xdr_stream *xdr, const struct qstr *name)
1081 {
1082         uint32_t *p;
1083
1084         RESERVE_SPACE(8 + name->len);
1085         WRITE32(OP_REMOVE);
1086         WRITE32(name->len);
1087         WRITEMEM(name->name, name->len);
1088
1089         return 0;
1090 }
1091
1092 static int encode_rename(struct xdr_stream *xdr, const struct qstr *oldname, const struct qstr *newname)
1093 {
1094         uint32_t *p;
1095
1096         RESERVE_SPACE(8 + oldname->len);
1097         WRITE32(OP_RENAME);
1098         WRITE32(oldname->len);
1099         WRITEMEM(oldname->name, oldname->len);
1100         
1101         RESERVE_SPACE(4 + newname->len);
1102         WRITE32(newname->len);
1103         WRITEMEM(newname->name, newname->len);
1104
1105         return 0;
1106 }
1107
1108 static int encode_renew(struct xdr_stream *xdr, const struct nfs4_client *client_stateid)
1109 {
1110         uint32_t *p;
1111
1112         RESERVE_SPACE(12);
1113         WRITE32(OP_RENEW);
1114         WRITE64(client_stateid->cl_clientid);
1115
1116         return 0;
1117 }
1118
1119 static int
1120 encode_setacl(struct xdr_stream *xdr, struct nfs_setaclargs *arg)
1121 {
1122         uint32_t *p;
1123
1124         RESERVE_SPACE(4+sizeof(zero_stateid.data));
1125         WRITE32(OP_SETATTR);
1126         WRITEMEM(zero_stateid.data, sizeof(zero_stateid.data));
1127         RESERVE_SPACE(2*4);
1128         WRITE32(1);
1129         WRITE32(FATTR4_WORD0_ACL);
1130         if (arg->acl_len % 4)
1131                 return -EINVAL;
1132         RESERVE_SPACE(4);
1133         WRITE32(arg->acl_len);
1134         xdr_write_pages(xdr, arg->acl_pages, arg->acl_pgbase, arg->acl_len);
1135         return 0;
1136 }
1137
1138 static int
1139 encode_savefh(struct xdr_stream *xdr)
1140 {
1141         uint32_t *p;
1142
1143         RESERVE_SPACE(4);
1144         WRITE32(OP_SAVEFH);
1145
1146         return 0;
1147 }
1148
1149 static int encode_setattr(struct xdr_stream *xdr, const struct nfs_setattrargs *arg, const struct nfs_server *server)
1150 {
1151         int status;
1152         uint32_t *p;
1153         
1154         RESERVE_SPACE(4+sizeof(arg->stateid.data));
1155         WRITE32(OP_SETATTR);
1156         WRITEMEM(arg->stateid.data, sizeof(arg->stateid.data));
1157
1158         if ((status = encode_attrs(xdr, arg->iap, server)))
1159                 return status;
1160
1161         return 0;
1162 }
1163
1164 static int encode_setclientid(struct xdr_stream *xdr, const struct nfs4_setclientid *setclientid)
1165 {
1166         uint32_t *p;
1167
1168         RESERVE_SPACE(4 + sizeof(setclientid->sc_verifier->data));
1169         WRITE32(OP_SETCLIENTID);
1170         WRITEMEM(setclientid->sc_verifier->data, sizeof(setclientid->sc_verifier->data));
1171
1172         encode_string(xdr, setclientid->sc_name_len, setclientid->sc_name);
1173         RESERVE_SPACE(4);
1174         WRITE32(setclientid->sc_prog);
1175         encode_string(xdr, setclientid->sc_netid_len, setclientid->sc_netid);
1176         encode_string(xdr, setclientid->sc_uaddr_len, setclientid->sc_uaddr);
1177         RESERVE_SPACE(4);
1178         WRITE32(setclientid->sc_cb_ident);
1179
1180         return 0;
1181 }
1182
1183 static int encode_setclientid_confirm(struct xdr_stream *xdr, const struct nfs4_client *client_state)
1184 {
1185         uint32_t *p;
1186
1187         RESERVE_SPACE(12 + sizeof(client_state->cl_confirm.data));
1188         WRITE32(OP_SETCLIENTID_CONFIRM);
1189         WRITE64(client_state->cl_clientid);
1190         WRITEMEM(client_state->cl_confirm.data, sizeof(client_state->cl_confirm.data));
1191
1192         return 0;
1193 }
1194
1195 static int encode_write(struct xdr_stream *xdr, const struct nfs_writeargs *args)
1196 {
1197         uint32_t *p;
1198
1199         RESERVE_SPACE(4);
1200         WRITE32(OP_WRITE);
1201
1202         encode_stateid(xdr, args->context);
1203
1204         RESERVE_SPACE(16);
1205         WRITE64(args->offset);
1206         WRITE32(args->stable);
1207         WRITE32(args->count);
1208
1209         xdr_write_pages(xdr, args->pages, args->pgbase, args->count);
1210
1211         return 0;
1212 }
1213
1214 static int encode_delegreturn(struct xdr_stream *xdr, const nfs4_stateid *stateid)
1215 {
1216         uint32_t *p;
1217
1218         RESERVE_SPACE(20);
1219
1220         WRITE32(OP_DELEGRETURN);
1221         WRITEMEM(stateid->data, sizeof(stateid->data));
1222         return 0;
1223
1224 }
1225 /*
1226  * END OF "GENERIC" ENCODE ROUTINES.
1227  */
1228
1229 /*
1230  * Encode an ACCESS request
1231  */
1232 static int nfs4_xdr_enc_access(struct rpc_rqst *req, uint32_t *p, const struct nfs4_accessargs *args)
1233 {
1234         struct xdr_stream xdr;
1235         struct compound_hdr hdr = {
1236                 .nops = 2,
1237         };
1238         int status;
1239
1240         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1241         encode_compound_hdr(&xdr, &hdr);
1242         if ((status = encode_putfh(&xdr, args->fh)) == 0)
1243                 status = encode_access(&xdr, args->access);
1244         return status;
1245 }
1246
1247 /*
1248  * Encode LOOKUP request
1249  */
1250 static int nfs4_xdr_enc_lookup(struct rpc_rqst *req, uint32_t *p, const struct nfs4_lookup_arg *args)
1251 {
1252         struct xdr_stream xdr;
1253         struct compound_hdr hdr = {
1254                 .nops = 4,
1255         };
1256         int status;
1257
1258         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1259         encode_compound_hdr(&xdr, &hdr);
1260         if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1261                 goto out;
1262         if ((status = encode_lookup(&xdr, args->name)) != 0)
1263                 goto out;
1264         if ((status = encode_getfh(&xdr)) != 0)
1265                 goto out;
1266         status = encode_getfattr(&xdr, args->bitmask);
1267 out:
1268         return status;
1269 }
1270
1271 /*
1272  * Encode LOOKUP_ROOT request
1273  */
1274 static int nfs4_xdr_enc_lookup_root(struct rpc_rqst *req, uint32_t *p, const struct nfs4_lookup_root_arg *args)
1275 {
1276         struct xdr_stream xdr;
1277         struct compound_hdr hdr = {
1278                 .nops = 3,
1279         };
1280         int status;
1281
1282         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1283         encode_compound_hdr(&xdr, &hdr);
1284         if ((status = encode_putrootfh(&xdr)) != 0)
1285                 goto out;
1286         if ((status = encode_getfh(&xdr)) == 0)
1287                 status = encode_getfattr(&xdr, args->bitmask);
1288 out:
1289         return status;
1290 }
1291
1292 /*
1293  * Encode REMOVE request
1294  */
1295 static int nfs4_xdr_enc_remove(struct rpc_rqst *req, uint32_t *p, const struct nfs4_remove_arg *args)
1296 {
1297         struct xdr_stream xdr;
1298         struct compound_hdr hdr = {
1299                 .nops = 2,
1300         };
1301         int status;
1302
1303         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1304         encode_compound_hdr(&xdr, &hdr);
1305         if ((status = encode_putfh(&xdr, args->fh)) == 0)
1306                 status = encode_remove(&xdr, args->name);
1307         return status;
1308 }
1309
1310 /*
1311  * Encode RENAME request
1312  */
1313 static int nfs4_xdr_enc_rename(struct rpc_rqst *req, uint32_t *p, const struct nfs4_rename_arg *args)
1314 {
1315         struct xdr_stream xdr;
1316         struct compound_hdr hdr = {
1317                 .nops = 4,
1318         };
1319         int status;
1320
1321         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1322         encode_compound_hdr(&xdr, &hdr);
1323         if ((status = encode_putfh(&xdr, args->old_dir)) != 0)
1324                 goto out;
1325         if ((status = encode_savefh(&xdr)) != 0)
1326                 goto out;
1327         if ((status = encode_putfh(&xdr, args->new_dir)) != 0)
1328                 goto out;
1329         status = encode_rename(&xdr, args->old_name, args->new_name);
1330 out:
1331         return status;
1332 }
1333
1334 /*
1335  * Encode LINK request
1336  */
1337 static int nfs4_xdr_enc_link(struct rpc_rqst *req, uint32_t *p, const struct nfs4_link_arg *args)
1338 {
1339         struct xdr_stream xdr;
1340         struct compound_hdr hdr = {
1341                 .nops = 4,
1342         };
1343         int status;
1344
1345         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1346         encode_compound_hdr(&xdr, &hdr);
1347         if ((status = encode_putfh(&xdr, args->fh)) != 0)
1348                 goto out;
1349         if ((status = encode_savefh(&xdr)) != 0)
1350                 goto out;
1351         if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1352                 goto out;
1353         status = encode_link(&xdr, args->name);
1354 out:
1355         return status;
1356 }
1357
1358 /*
1359  * Encode CREATE request
1360  */
1361 static int nfs4_xdr_enc_create(struct rpc_rqst *req, uint32_t *p, const struct nfs4_create_arg *args)
1362 {
1363         struct xdr_stream xdr;
1364         struct compound_hdr hdr = {
1365                 .nops = 4,
1366         };
1367         int status;
1368
1369         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1370         encode_compound_hdr(&xdr, &hdr);
1371         if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1372                 goto out;
1373         if ((status = encode_create(&xdr, args)) != 0)
1374                 goto out;
1375         if ((status = encode_getfh(&xdr)) != 0)
1376                 goto out;
1377         status = encode_getfattr(&xdr, args->bitmask);
1378 out:
1379         return status;
1380 }
1381
1382 /*
1383  * Encode SYMLINK request
1384  */
1385 static int nfs4_xdr_enc_symlink(struct rpc_rqst *req, uint32_t *p, const struct nfs4_create_arg *args)
1386 {
1387         return nfs4_xdr_enc_create(req, p, args);
1388 }
1389
1390 /*
1391  * Encode GETATTR request
1392  */
1393 static int nfs4_xdr_enc_getattr(struct rpc_rqst *req, uint32_t *p, const struct nfs4_getattr_arg *args)
1394 {
1395         struct xdr_stream xdr;
1396         struct compound_hdr hdr = {
1397                 .nops = 2,
1398         };
1399         int status;
1400
1401         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1402         encode_compound_hdr(&xdr, &hdr);
1403         if ((status = encode_putfh(&xdr, args->fh)) == 0)
1404                 status = encode_getfattr(&xdr, args->bitmask);
1405         return status;
1406 }
1407
1408 /*
1409  * Encode a CLOSE request
1410  */
1411 static int nfs4_xdr_enc_close(struct rpc_rqst *req, uint32_t *p, struct nfs_closeargs *args)
1412 {
1413         struct xdr_stream xdr;
1414         struct compound_hdr hdr = {
1415                 .nops   = 2,
1416         };
1417         int status;
1418
1419         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1420         encode_compound_hdr(&xdr, &hdr);
1421         status = encode_putfh(&xdr, args->fh);
1422         if(status)
1423                 goto out;
1424         status = encode_close(&xdr, args);
1425 out:
1426         return status;
1427 }
1428
1429 /*
1430  * Encode an OPEN request
1431  */
1432 static int nfs4_xdr_enc_open(struct rpc_rqst *req, uint32_t *p, struct nfs_openargs *args)
1433 {
1434         struct xdr_stream xdr;
1435         struct compound_hdr hdr = {
1436                 .nops = 4,
1437         };
1438         int status;
1439
1440         status = nfs_wait_on_sequence(args->seqid, req->rq_task);
1441         if (status != 0)
1442                 goto out;
1443         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1444         encode_compound_hdr(&xdr, &hdr);
1445         status = encode_putfh(&xdr, args->fh);
1446         if (status)
1447                 goto out;
1448         status = encode_open(&xdr, args);
1449         if (status)
1450                 goto out;
1451         status = encode_getfh(&xdr);
1452         if (status)
1453                 goto out;
1454         status = encode_getfattr(&xdr, args->bitmask);
1455 out:
1456         return status;
1457 }
1458
1459 /*
1460  * Encode an OPEN_CONFIRM request
1461  */
1462 static int nfs4_xdr_enc_open_confirm(struct rpc_rqst *req, uint32_t *p, struct nfs_open_confirmargs *args)
1463 {
1464         struct xdr_stream xdr;
1465         struct compound_hdr hdr = {
1466                 .nops   = 2,
1467         };
1468         int status;
1469
1470         status = nfs_wait_on_sequence(args->seqid, req->rq_task);
1471         if (status != 0)
1472                 goto out;
1473         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1474         encode_compound_hdr(&xdr, &hdr);
1475         status = encode_putfh(&xdr, args->fh);
1476         if(status)
1477                 goto out;
1478         status = encode_open_confirm(&xdr, args);
1479 out:
1480         return status;
1481 }
1482
1483 /*
1484  * Encode an OPEN request with no attributes.
1485  */
1486 static int nfs4_xdr_enc_open_noattr(struct rpc_rqst *req, uint32_t *p, struct nfs_openargs *args)
1487 {
1488         struct xdr_stream xdr;
1489         struct compound_hdr hdr = {
1490                 .nops   = 2,
1491         };
1492         int status;
1493
1494         status = nfs_wait_on_sequence(args->seqid, req->rq_task);
1495         if (status != 0)
1496                 goto out;
1497         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1498         encode_compound_hdr(&xdr, &hdr);
1499         status = encode_putfh(&xdr, args->fh);
1500         if (status)
1501                 goto out;
1502         status = encode_open(&xdr, args);
1503 out:
1504         return status;
1505 }
1506
1507 /*
1508  * Encode an OPEN_DOWNGRADE request
1509  */
1510 static int nfs4_xdr_enc_open_downgrade(struct rpc_rqst *req, uint32_t *p, struct nfs_closeargs *args)
1511 {
1512         struct xdr_stream xdr;
1513         struct compound_hdr hdr = {
1514                 .nops   = 2,
1515         };
1516         int status;
1517
1518         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1519         encode_compound_hdr(&xdr, &hdr);
1520         status = encode_putfh(&xdr, args->fh);
1521         if (status)
1522                 goto out;
1523         status = encode_open_downgrade(&xdr, args);
1524 out:
1525         return status;
1526 }
1527
1528 /*
1529  * Encode a LOCK request
1530  */
1531 static int nfs4_xdr_enc_lock(struct rpc_rqst *req, uint32_t *p, struct nfs_lockargs *args)
1532 {
1533         struct xdr_stream xdr;
1534         struct compound_hdr hdr = {
1535                 .nops   = 2,
1536         };
1537         struct nfs_lock_opargs *opargs = args->u.lock;
1538         struct nfs_seqid *seqid;
1539         int status;
1540
1541         if (opargs->new_lock_owner)
1542                 seqid = opargs->u.open_lock->lock_seqid;
1543         else
1544                 seqid = opargs->u.exist_lock->seqid;
1545         status = nfs_wait_on_sequence(seqid, req->rq_task);
1546         if (status != 0)
1547                 goto out;
1548         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1549         encode_compound_hdr(&xdr, &hdr);
1550         status = encode_putfh(&xdr, args->fh);
1551         if(status)
1552                 goto out;
1553         status = encode_lock(&xdr, args);
1554 out:
1555         return status;
1556 }
1557
1558 /*
1559  * Encode a LOCKT request
1560  */
1561 static int nfs4_xdr_enc_lockt(struct rpc_rqst *req, uint32_t *p, struct nfs_lockargs *args)
1562 {
1563         struct xdr_stream xdr;
1564         struct compound_hdr hdr = {
1565                 .nops   = 2,
1566         };
1567         int status;
1568
1569         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1570         encode_compound_hdr(&xdr, &hdr);
1571         status = encode_putfh(&xdr, args->fh);
1572         if(status)
1573                 goto out;
1574         status = encode_lockt(&xdr, args);
1575 out:
1576         return status;
1577 }
1578
1579 /*
1580  * Encode a LOCKU request
1581  */
1582 static int nfs4_xdr_enc_locku(struct rpc_rqst *req, uint32_t *p, struct nfs_lockargs *args)
1583 {
1584         struct xdr_stream xdr;
1585         struct compound_hdr hdr = {
1586                 .nops   = 2,
1587         };
1588         int status;
1589
1590         status = nfs_wait_on_sequence(args->u.locku->seqid, req->rq_task);
1591         if (status != 0)
1592                 goto out;
1593         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1594         encode_compound_hdr(&xdr, &hdr);
1595         status = encode_putfh(&xdr, args->fh);
1596         if(status)
1597                 goto out;
1598         status = encode_locku(&xdr, args);
1599 out:
1600         return status;
1601 }
1602
1603 /*
1604  * Encode a READLINK request
1605  */
1606 static int nfs4_xdr_enc_readlink(struct rpc_rqst *req, uint32_t *p, const struct nfs4_readlink *args)
1607 {
1608         struct xdr_stream xdr;
1609         struct compound_hdr hdr = {
1610                 .nops = 2,
1611         };
1612         int status;
1613
1614         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1615         encode_compound_hdr(&xdr, &hdr);
1616         status = encode_putfh(&xdr, args->fh);
1617         if(status)
1618                 goto out;
1619         status = encode_readlink(&xdr, args, req);
1620 out:
1621         return status;
1622 }
1623
1624 /*
1625  * Encode a READDIR request
1626  */
1627 static int nfs4_xdr_enc_readdir(struct rpc_rqst *req, uint32_t *p, const struct nfs4_readdir_arg *args)
1628 {
1629         struct xdr_stream xdr;
1630         struct compound_hdr hdr = {
1631                 .nops = 2,
1632         };
1633         int status;
1634
1635         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1636         encode_compound_hdr(&xdr, &hdr);
1637         status = encode_putfh(&xdr, args->fh);
1638         if(status)
1639                 goto out;
1640         status = encode_readdir(&xdr, args, req);
1641 out:
1642         return status;
1643 }
1644
1645 /*
1646  * Encode a READ request
1647  */
1648 static int nfs4_xdr_enc_read(struct rpc_rqst *req, uint32_t *p, struct nfs_readargs *args)
1649 {
1650         struct rpc_auth *auth = req->rq_task->tk_auth;
1651         struct xdr_stream xdr;
1652         struct compound_hdr hdr = {
1653                 .nops = 2,
1654         };
1655         int replen, status;
1656
1657         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1658         encode_compound_hdr(&xdr, &hdr);
1659         status = encode_putfh(&xdr, args->fh);
1660         if (status)
1661                 goto out;
1662         status = encode_read(&xdr, args);
1663         if (status)
1664                 goto out;
1665
1666         /* set up reply kvec
1667          *    toplevel status + taglen=0 + rescount + OP_PUTFH + status
1668          *       + OP_READ + status + eof + datalen = 9
1669          */
1670         replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_read_sz) << 2;
1671         xdr_inline_pages(&req->rq_rcv_buf, replen,
1672                          args->pages, args->pgbase, args->count);
1673 out:
1674         return status;
1675 }
1676
1677 /*
1678  * Encode an SETATTR request
1679  */
1680 static int nfs4_xdr_enc_setattr(struct rpc_rqst *req, uint32_t *p, struct nfs_setattrargs *args)
1681
1682 {
1683         struct xdr_stream xdr;
1684         struct compound_hdr hdr = {
1685                 .nops   = 3,
1686         };
1687         int status;
1688
1689         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1690         encode_compound_hdr(&xdr, &hdr);
1691         status = encode_putfh(&xdr, args->fh);
1692         if(status)
1693                 goto out;
1694         status = encode_setattr(&xdr, args, args->server);
1695         if(status)
1696                 goto out;
1697         status = encode_getfattr(&xdr, args->bitmask);
1698 out:
1699         return status;
1700 }
1701
1702 /*
1703  * Encode a GETACL request
1704  */
1705 static int
1706 nfs4_xdr_enc_getacl(struct rpc_rqst *req, uint32_t *p,
1707                 struct nfs_getaclargs *args)
1708 {
1709         struct xdr_stream xdr;
1710         struct rpc_auth *auth = req->rq_task->tk_auth;
1711         struct compound_hdr hdr = {
1712                 .nops   = 2,
1713         };
1714         int replen, status;
1715
1716         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1717         encode_compound_hdr(&xdr, &hdr);
1718         status = encode_putfh(&xdr, args->fh);
1719         if (status)
1720                 goto out;
1721         status = encode_getattr_two(&xdr, FATTR4_WORD0_ACL, 0);
1722         /* set up reply buffer: */
1723         replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_getacl_sz) << 2;
1724         xdr_inline_pages(&req->rq_rcv_buf, replen,
1725                 args->acl_pages, args->acl_pgbase, args->acl_len);
1726 out:
1727         return status;
1728 }
1729
1730 /*
1731  * Encode a WRITE request
1732  */
1733 static int nfs4_xdr_enc_write(struct rpc_rqst *req, uint32_t *p, struct nfs_writeargs *args)
1734 {
1735         struct xdr_stream xdr;
1736         struct compound_hdr hdr = {
1737                 .nops = 2,
1738         };
1739         int status;
1740
1741         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1742         encode_compound_hdr(&xdr, &hdr);
1743         status = encode_putfh(&xdr, args->fh);
1744         if (status)
1745                 goto out;
1746         status = encode_write(&xdr, args);
1747 out:
1748         return status;
1749 }
1750
1751 /*
1752  *  a COMMIT request
1753  */
1754 static int nfs4_xdr_enc_commit(struct rpc_rqst *req, uint32_t *p, struct nfs_writeargs *args)
1755 {
1756         struct xdr_stream xdr;
1757         struct compound_hdr hdr = {
1758                 .nops = 2,
1759         };
1760         int status;
1761
1762         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1763         encode_compound_hdr(&xdr, &hdr);
1764         status = encode_putfh(&xdr, args->fh);
1765         if (status)
1766                 goto out;
1767         status = encode_commit(&xdr, args);
1768 out:
1769         return status;
1770 }
1771
1772 /*
1773  * FSINFO request
1774  */
1775 static int nfs4_xdr_enc_fsinfo(struct rpc_rqst *req, uint32_t *p, struct nfs4_fsinfo_arg *args)
1776 {
1777         struct xdr_stream xdr;
1778         struct compound_hdr hdr = {
1779                 .nops   = 2,
1780         };
1781         int status;
1782
1783         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1784         encode_compound_hdr(&xdr, &hdr);
1785         status = encode_putfh(&xdr, args->fh);
1786         if (!status)
1787                 status = encode_fsinfo(&xdr, args->bitmask);
1788         return status;
1789 }
1790
1791 /*
1792  * a PATHCONF request
1793  */
1794 static int nfs4_xdr_enc_pathconf(struct rpc_rqst *req, uint32_t *p, const struct nfs4_pathconf_arg *args)
1795 {
1796         struct xdr_stream xdr;
1797         struct compound_hdr hdr = {
1798                 .nops = 2,
1799         };
1800         int status;
1801
1802         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1803         encode_compound_hdr(&xdr, &hdr);
1804         status = encode_putfh(&xdr, args->fh);
1805         if (!status)
1806                 status = encode_getattr_one(&xdr,
1807                                 args->bitmask[0] & nfs4_pathconf_bitmap[0]);
1808         return status;
1809 }
1810
1811 /*
1812  * a STATFS request
1813  */
1814 static int nfs4_xdr_enc_statfs(struct rpc_rqst *req, uint32_t *p, const struct nfs4_statfs_arg *args)
1815 {
1816         struct xdr_stream xdr;
1817         struct compound_hdr hdr = {
1818                 .nops = 2,
1819         };
1820         int status;
1821
1822         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1823         encode_compound_hdr(&xdr, &hdr);
1824         status = encode_putfh(&xdr, args->fh);
1825         if (status == 0)
1826                 status = encode_getattr_two(&xdr,
1827                                 args->bitmask[0] & nfs4_statfs_bitmap[0],
1828                                 args->bitmask[1] & nfs4_statfs_bitmap[1]);
1829         return status;
1830 }
1831
1832 /*
1833  * GETATTR_BITMAP request
1834  */
1835 static int nfs4_xdr_enc_server_caps(struct rpc_rqst *req, uint32_t *p, const struct nfs_fh *fhandle)
1836 {
1837         struct xdr_stream xdr;
1838         struct compound_hdr hdr = {
1839                 .nops = 2,
1840         };
1841         int status;
1842
1843         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1844         encode_compound_hdr(&xdr, &hdr);
1845         status = encode_putfh(&xdr, fhandle);
1846         if (status == 0)
1847                 status = encode_getattr_one(&xdr, FATTR4_WORD0_SUPPORTED_ATTRS|
1848                                 FATTR4_WORD0_LINK_SUPPORT|
1849                                 FATTR4_WORD0_SYMLINK_SUPPORT|
1850                                 FATTR4_WORD0_ACLSUPPORT);
1851         return status;
1852 }
1853
1854 /*
1855  * a RENEW request
1856  */
1857 static int nfs4_xdr_enc_renew(struct rpc_rqst *req, uint32_t *p, struct nfs4_client *clp)
1858 {
1859         struct xdr_stream xdr;
1860         struct compound_hdr hdr = {
1861                 .nops   = 1,
1862         };
1863
1864         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1865         encode_compound_hdr(&xdr, &hdr);
1866         return encode_renew(&xdr, clp);
1867 }
1868
1869 /*
1870  * a SETCLIENTID request
1871  */
1872 static int nfs4_xdr_enc_setclientid(struct rpc_rqst *req, uint32_t *p, struct nfs4_setclientid *sc)
1873 {
1874         struct xdr_stream xdr;
1875         struct compound_hdr hdr = {
1876                 .nops   = 1,
1877         };
1878
1879         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1880         encode_compound_hdr(&xdr, &hdr);
1881         return encode_setclientid(&xdr, sc);
1882 }
1883
1884 /*
1885  * a SETCLIENTID_CONFIRM request
1886  */
1887 static int nfs4_xdr_enc_setclientid_confirm(struct rpc_rqst *req, uint32_t *p, struct nfs4_client *clp)
1888 {
1889         struct xdr_stream xdr;
1890         struct compound_hdr hdr = {
1891                 .nops   = 3,
1892         };
1893         const u32 lease_bitmap[2] = { FATTR4_WORD0_LEASE_TIME, 0 };
1894         int status;
1895
1896         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1897         encode_compound_hdr(&xdr, &hdr);
1898         status = encode_setclientid_confirm(&xdr, clp);
1899         if (!status)
1900                 status = encode_putrootfh(&xdr);
1901         if (!status)
1902                 status = encode_fsinfo(&xdr, lease_bitmap);
1903         return status;
1904 }
1905
1906 /*
1907  * DELEGRETURN request
1908  */
1909 static int nfs4_xdr_enc_delegreturn(struct rpc_rqst *req, uint32_t *p, const struct nfs4_delegreturnargs *args)
1910 {
1911         struct xdr_stream xdr;
1912         struct compound_hdr hdr = {
1913                 .nops = 2,
1914         };
1915         int status;
1916
1917         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1918         encode_compound_hdr(&xdr, &hdr);
1919         if ((status = encode_putfh(&xdr, args->fhandle)) == 0)
1920                 status = encode_delegreturn(&xdr, args->stateid);
1921         return status;
1922 }
1923
1924 /*
1925  * START OF "GENERIC" DECODE ROUTINES.
1926  *   These may look a little ugly since they are imported from a "generic"
1927  * set of XDR encode/decode routines which are intended to be shared by
1928  * all of our NFSv4 implementations (OpenBSD, MacOS X...).
1929  *
1930  * If the pain of reading these is too great, it should be a straightforward
1931  * task to translate them into Linux-specific versions which are more
1932  * consistent with the style used in NFSv2/v3...
1933  */
1934 #define READ32(x)         (x) = ntohl(*p++)
1935 #define READ64(x)         do {                  \
1936         (x) = (u64)ntohl(*p++) << 32;           \
1937         (x) |= ntohl(*p++);                     \
1938 } while (0)
1939 #define READTIME(x)       do {                  \
1940         p++;                                    \
1941         (x.tv_sec) = ntohl(*p++);               \
1942         (x.tv_nsec) = ntohl(*p++);              \
1943 } while (0)
1944 #define COPYMEM(x,nbytes) do {                  \
1945         memcpy((x), p, nbytes);                 \
1946         p += XDR_QUADLEN(nbytes);               \
1947 } while (0)
1948
1949 #define READ_BUF(nbytes)  do { \
1950         p = xdr_inline_decode(xdr, nbytes); \
1951         if (!p) { \
1952                 printk(KERN_WARNING "%s: reply buffer overflowed in line %d.", \
1953                                 __FUNCTION__, __LINE__); \
1954                 return -EIO; \
1955         } \
1956 } while (0)
1957
1958 static int decode_opaque_inline(struct xdr_stream *xdr, uint32_t *len, char **string)
1959 {
1960         uint32_t *p;
1961
1962         READ_BUF(4);
1963         READ32(*len);
1964         READ_BUF(*len);
1965         *string = (char *)p;
1966         return 0;
1967 }
1968
1969 static int decode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
1970 {
1971         uint32_t *p;
1972
1973         READ_BUF(8);
1974         READ32(hdr->status);
1975         READ32(hdr->taglen);
1976         
1977         READ_BUF(hdr->taglen + 4);
1978         hdr->tag = (char *)p;
1979         p += XDR_QUADLEN(hdr->taglen);
1980         READ32(hdr->nops);
1981         return 0;
1982 }
1983
1984 static int decode_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
1985 {
1986         uint32_t *p;
1987         uint32_t opnum;
1988         int32_t nfserr;
1989
1990         READ_BUF(8);
1991         READ32(opnum);
1992         if (opnum != expected) {
1993                 printk(KERN_NOTICE
1994                                 "nfs4_decode_op_hdr: Server returned operation"
1995                                 " %d but we issued a request for %d\n",
1996                                 opnum, expected);
1997                 return -EIO;
1998         }
1999         READ32(nfserr);
2000         if (nfserr != NFS_OK)
2001                 return -nfs_stat_to_errno(nfserr);
2002         return 0;
2003 }
2004
2005 /* Dummy routine */
2006 static int decode_ace(struct xdr_stream *xdr, void *ace, struct nfs4_client *clp)
2007 {
2008         uint32_t *p;
2009         uint32_t strlen;
2010         char *str;
2011
2012         READ_BUF(12);
2013         return decode_opaque_inline(xdr, &strlen, &str);
2014 }
2015
2016 static int decode_attr_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
2017 {
2018         uint32_t bmlen, *p;
2019
2020         READ_BUF(4);
2021         READ32(bmlen);
2022
2023         bitmap[0] = bitmap[1] = 0;
2024         READ_BUF((bmlen << 2));
2025         if (bmlen > 0) {
2026                 READ32(bitmap[0]);
2027                 if (bmlen > 1)
2028                         READ32(bitmap[1]);
2029         }
2030         return 0;
2031 }
2032
2033 static inline int decode_attr_length(struct xdr_stream *xdr, uint32_t *attrlen, uint32_t **savep)
2034 {
2035         uint32_t *p;
2036
2037         READ_BUF(4);
2038         READ32(*attrlen);
2039         *savep = xdr->p;
2040         return 0;
2041 }
2042
2043 static int decode_attr_supported(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *bitmask)
2044 {
2045         if (likely(bitmap[0] & FATTR4_WORD0_SUPPORTED_ATTRS)) {
2046                 decode_attr_bitmap(xdr, bitmask);
2047                 bitmap[0] &= ~FATTR4_WORD0_SUPPORTED_ATTRS;
2048         } else
2049                 bitmask[0] = bitmask[1] = 0;
2050         dprintk("%s: bitmask=0x%x%x\n", __FUNCTION__, bitmask[0], bitmask[1]);
2051         return 0;
2052 }
2053
2054 static int decode_attr_type(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *type)
2055 {
2056         uint32_t *p;
2057
2058         *type = 0;
2059         if (unlikely(bitmap[0] & (FATTR4_WORD0_TYPE - 1U)))
2060                 return -EIO;
2061         if (likely(bitmap[0] & FATTR4_WORD0_TYPE)) {
2062                 READ_BUF(4);
2063                 READ32(*type);
2064                 if (*type < NF4REG || *type > NF4NAMEDATTR) {
2065                         dprintk("%s: bad type %d\n", __FUNCTION__, *type);
2066                         return -EIO;
2067                 }
2068                 bitmap[0] &= ~FATTR4_WORD0_TYPE;
2069         }
2070         dprintk("%s: type=0%o\n", __FUNCTION__, nfs_type2fmt[*type].nfs2type);
2071         return 0;
2072 }
2073
2074 static int decode_attr_change(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *change)
2075 {
2076         uint32_t *p;
2077
2078         *change = 0;
2079         if (unlikely(bitmap[0] & (FATTR4_WORD0_CHANGE - 1U)))
2080                 return -EIO;
2081         if (likely(bitmap[0] & FATTR4_WORD0_CHANGE)) {
2082                 READ_BUF(8);
2083                 READ64(*change);
2084                 bitmap[0] &= ~FATTR4_WORD0_CHANGE;
2085         }
2086         dprintk("%s: change attribute=%Lu\n", __FUNCTION__,
2087                         (unsigned long long)*change);
2088         return 0;
2089 }
2090
2091 static int decode_attr_size(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *size)
2092 {
2093         uint32_t *p;
2094
2095         *size = 0;
2096         if (unlikely(bitmap[0] & (FATTR4_WORD0_SIZE - 1U)))
2097                 return -EIO;
2098         if (likely(bitmap[0] & FATTR4_WORD0_SIZE)) {
2099                 READ_BUF(8);
2100                 READ64(*size);
2101                 bitmap[0] &= ~FATTR4_WORD0_SIZE;
2102         }
2103         dprintk("%s: file size=%Lu\n", __FUNCTION__, (unsigned long long)*size);
2104         return 0;
2105 }
2106
2107 static int decode_attr_link_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2108 {
2109         uint32_t *p;
2110
2111         *res = 0;
2112         if (unlikely(bitmap[0] & (FATTR4_WORD0_LINK_SUPPORT - 1U)))
2113                 return -EIO;
2114         if (likely(bitmap[0] & FATTR4_WORD0_LINK_SUPPORT)) {
2115                 READ_BUF(4);
2116                 READ32(*res);
2117                 bitmap[0] &= ~FATTR4_WORD0_LINK_SUPPORT;
2118         }
2119         dprintk("%s: link support=%s\n", __FUNCTION__, *res == 0 ? "false" : "true");
2120         return 0;
2121 }
2122
2123 static int decode_attr_symlink_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2124 {
2125         uint32_t *p;
2126
2127         *res = 0;
2128         if (unlikely(bitmap[0] & (FATTR4_WORD0_SYMLINK_SUPPORT - 1U)))
2129                 return -EIO;
2130         if (likely(bitmap[0] & FATTR4_WORD0_SYMLINK_SUPPORT)) {
2131                 READ_BUF(4);
2132                 READ32(*res);
2133                 bitmap[0] &= ~FATTR4_WORD0_SYMLINK_SUPPORT;
2134         }
2135         dprintk("%s: symlink support=%s\n", __FUNCTION__, *res == 0 ? "false" : "true");
2136         return 0;
2137 }
2138
2139 static int decode_attr_fsid(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_fsid *fsid)
2140 {
2141         uint32_t *p;
2142
2143         fsid->major = 0;
2144         fsid->minor = 0;
2145         if (unlikely(bitmap[0] & (FATTR4_WORD0_FSID - 1U)))
2146                 return -EIO;
2147         if (likely(bitmap[0] & FATTR4_WORD0_FSID)) {
2148                 READ_BUF(16);
2149                 READ64(fsid->major);
2150                 READ64(fsid->minor);
2151                 bitmap[0] &= ~FATTR4_WORD0_FSID;
2152         }
2153         dprintk("%s: fsid=(0x%Lx/0x%Lx)\n", __FUNCTION__,
2154                         (unsigned long long)fsid->major,
2155                         (unsigned long long)fsid->minor);
2156         return 0;
2157 }
2158
2159 static int decode_attr_lease_time(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2160 {
2161         uint32_t *p;
2162
2163         *res = 60;
2164         if (unlikely(bitmap[0] & (FATTR4_WORD0_LEASE_TIME - 1U)))
2165                 return -EIO;
2166         if (likely(bitmap[0] & FATTR4_WORD0_LEASE_TIME)) {
2167                 READ_BUF(4);
2168                 READ32(*res);
2169                 bitmap[0] &= ~FATTR4_WORD0_LEASE_TIME;
2170         }
2171         dprintk("%s: file size=%u\n", __FUNCTION__, (unsigned int)*res);
2172         return 0;
2173 }
2174
2175 static int decode_attr_aclsupport(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2176 {
2177         uint32_t *p;
2178
2179         *res = ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL;
2180         if (unlikely(bitmap[0] & (FATTR4_WORD0_ACLSUPPORT - 1U)))
2181                 return -EIO;
2182         if (likely(bitmap[0] & FATTR4_WORD0_ACLSUPPORT)) {
2183                 READ_BUF(4);
2184                 READ32(*res);
2185                 bitmap[0] &= ~FATTR4_WORD0_ACLSUPPORT;
2186         }
2187         dprintk("%s: ACLs supported=%u\n", __FUNCTION__, (unsigned int)*res);
2188         return 0;
2189 }
2190
2191 static int decode_attr_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
2192 {
2193         uint32_t *p;
2194
2195         *fileid = 0;
2196         if (unlikely(bitmap[0] & (FATTR4_WORD0_FILEID - 1U)))
2197                 return -EIO;
2198         if (likely(bitmap[0] & FATTR4_WORD0_FILEID)) {
2199                 READ_BUF(8);
2200                 READ64(*fileid);
2201                 bitmap[0] &= ~FATTR4_WORD0_FILEID;
2202         }
2203         dprintk("%s: fileid=%Lu\n", __FUNCTION__, (unsigned long long)*fileid);
2204         return 0;
2205 }
2206
2207 static int decode_attr_files_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2208 {
2209         uint32_t *p;
2210         int status = 0;
2211
2212         *res = 0;
2213         if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_AVAIL - 1U)))
2214                 return -EIO;
2215         if (likely(bitmap[0] & FATTR4_WORD0_FILES_AVAIL)) {
2216                 READ_BUF(8);
2217                 READ64(*res);
2218                 bitmap[0] &= ~FATTR4_WORD0_FILES_AVAIL;
2219         }
2220         dprintk("%s: files avail=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2221         return status;
2222 }
2223
2224 static int decode_attr_files_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2225 {
2226         uint32_t *p;
2227         int status = 0;
2228
2229         *res = 0;
2230         if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_FREE - 1U)))
2231                 return -EIO;
2232         if (likely(bitmap[0] & FATTR4_WORD0_FILES_FREE)) {
2233                 READ_BUF(8);
2234                 READ64(*res);
2235                 bitmap[0] &= ~FATTR4_WORD0_FILES_FREE;
2236         }
2237         dprintk("%s: files free=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2238         return status;
2239 }
2240
2241 static int decode_attr_files_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2242 {
2243         uint32_t *p;
2244         int status = 0;
2245
2246         *res = 0;
2247         if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_TOTAL - 1U)))
2248                 return -EIO;
2249         if (likely(bitmap[0] & FATTR4_WORD0_FILES_TOTAL)) {
2250                 READ_BUF(8);
2251                 READ64(*res);
2252                 bitmap[0] &= ~FATTR4_WORD0_FILES_TOTAL;
2253         }
2254         dprintk("%s: files total=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2255         return status;
2256 }
2257
2258 static int decode_attr_maxfilesize(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2259 {
2260         uint32_t *p;
2261         int status = 0;
2262
2263         *res = 0;
2264         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXFILESIZE - 1U)))
2265                 return -EIO;
2266         if (likely(bitmap[0] & FATTR4_WORD0_MAXFILESIZE)) {
2267                 READ_BUF(8);
2268                 READ64(*res);
2269                 bitmap[0] &= ~FATTR4_WORD0_MAXFILESIZE;
2270         }
2271         dprintk("%s: maxfilesize=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2272         return status;
2273 }
2274
2275 static int decode_attr_maxlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxlink)
2276 {
2277         uint32_t *p;
2278         int status = 0;
2279
2280         *maxlink = 1;
2281         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXLINK - 1U)))
2282                 return -EIO;
2283         if (likely(bitmap[0] & FATTR4_WORD0_MAXLINK)) {
2284                 READ_BUF(4);
2285                 READ32(*maxlink);
2286                 bitmap[0] &= ~FATTR4_WORD0_MAXLINK;
2287         }
2288         dprintk("%s: maxlink=%u\n", __FUNCTION__, *maxlink);
2289         return status;
2290 }
2291
2292 static int decode_attr_maxname(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxname)
2293 {
2294         uint32_t *p;
2295         int status = 0;
2296
2297         *maxname = 1024;
2298         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXNAME - 1U)))
2299                 return -EIO;
2300         if (likely(bitmap[0] & FATTR4_WORD0_MAXNAME)) {
2301                 READ_BUF(4);
2302                 READ32(*maxname);
2303                 bitmap[0] &= ~FATTR4_WORD0_MAXNAME;
2304         }
2305         dprintk("%s: maxname=%u\n", __FUNCTION__, *maxname);
2306         return status;
2307 }
2308
2309 static int decode_attr_maxread(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2310 {
2311         uint32_t *p;
2312         int status = 0;
2313
2314         *res = 1024;
2315         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXREAD - 1U)))
2316                 return -EIO;
2317         if (likely(bitmap[0] & FATTR4_WORD0_MAXREAD)) {
2318                 uint64_t maxread;
2319                 READ_BUF(8);
2320                 READ64(maxread);
2321                 if (maxread > 0x7FFFFFFF)
2322                         maxread = 0x7FFFFFFF;
2323                 *res = (uint32_t)maxread;
2324                 bitmap[0] &= ~FATTR4_WORD0_MAXREAD;
2325         }
2326         dprintk("%s: maxread=%lu\n", __FUNCTION__, (unsigned long)*res);
2327         return status;
2328 }
2329
2330 static int decode_attr_maxwrite(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2331 {
2332         uint32_t *p;
2333         int status = 0;
2334
2335         *res = 1024;
2336         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXWRITE - 1U)))
2337                 return -EIO;
2338         if (likely(bitmap[0] & FATTR4_WORD0_MAXWRITE)) {
2339                 uint64_t maxwrite;
2340                 READ_BUF(8);
2341                 READ64(maxwrite);
2342                 if (maxwrite > 0x7FFFFFFF)
2343                         maxwrite = 0x7FFFFFFF;
2344                 *res = (uint32_t)maxwrite;
2345                 bitmap[0] &= ~FATTR4_WORD0_MAXWRITE;
2346         }
2347         dprintk("%s: maxwrite=%lu\n", __FUNCTION__, (unsigned long)*res);
2348         return status;
2349 }
2350
2351 static int decode_attr_mode(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *mode)
2352 {
2353         uint32_t *p;
2354
2355         *mode = 0;
2356         if (unlikely(bitmap[1] & (FATTR4_WORD1_MODE - 1U)))
2357                 return -EIO;
2358         if (likely(bitmap[1] & FATTR4_WORD1_MODE)) {
2359                 READ_BUF(4);
2360                 READ32(*mode);
2361                 *mode &= ~S_IFMT;
2362                 bitmap[1] &= ~FATTR4_WORD1_MODE;
2363         }
2364         dprintk("%s: file mode=0%o\n", __FUNCTION__, (unsigned int)*mode);
2365         return 0;
2366 }
2367
2368 static int decode_attr_nlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *nlink)
2369 {
2370         uint32_t *p;
2371
2372         *nlink = 1;
2373         if (unlikely(bitmap[1] & (FATTR4_WORD1_NUMLINKS - 1U)))
2374                 return -EIO;
2375         if (likely(bitmap[1] & FATTR4_WORD1_NUMLINKS)) {
2376                 READ_BUF(4);
2377                 READ32(*nlink);
2378                 bitmap[1] &= ~FATTR4_WORD1_NUMLINKS;
2379         }
2380         dprintk("%s: nlink=%u\n", __FUNCTION__, (unsigned int)*nlink);
2381         return 0;
2382 }
2383
2384 static int decode_attr_owner(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_client *clp, int32_t *uid)
2385 {
2386         uint32_t len, *p;
2387
2388         *uid = -2;
2389         if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER - 1U)))
2390                 return -EIO;
2391         if (likely(bitmap[1] & FATTR4_WORD1_OWNER)) {
2392                 READ_BUF(4);
2393                 READ32(len);
2394                 READ_BUF(len);
2395                 if (len < XDR_MAX_NETOBJ) {
2396                         if (nfs_map_name_to_uid(clp, (char *)p, len, uid) != 0)
2397                                 dprintk("%s: nfs_map_name_to_uid failed!\n",
2398                                                 __FUNCTION__);
2399                 } else
2400                         printk(KERN_WARNING "%s: name too long (%u)!\n",
2401                                         __FUNCTION__, len);
2402                 bitmap[1] &= ~FATTR4_WORD1_OWNER;
2403         }
2404         dprintk("%s: uid=%d\n", __FUNCTION__, (int)*uid);
2405         return 0;
2406 }
2407
2408 static int decode_attr_group(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_client *clp, int32_t *gid)
2409 {
2410         uint32_t len, *p;
2411
2412         *gid = -2;
2413         if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER_GROUP - 1U)))
2414                 return -EIO;
2415         if (likely(bitmap[1] & FATTR4_WORD1_OWNER_GROUP)) {
2416                 READ_BUF(4);
2417                 READ32(len);
2418                 READ_BUF(len);
2419                 if (len < XDR_MAX_NETOBJ) {
2420                         if (nfs_map_group_to_gid(clp, (char *)p, len, gid) != 0)
2421                                 dprintk("%s: nfs_map_group_to_gid failed!\n",
2422                                                 __FUNCTION__);
2423                 } else
2424                         printk(KERN_WARNING "%s: name too long (%u)!\n",
2425                                         __FUNCTION__, len);
2426                 bitmap[1] &= ~FATTR4_WORD1_OWNER_GROUP;
2427         }
2428         dprintk("%s: gid=%d\n", __FUNCTION__, (int)*gid);
2429         return 0;
2430 }
2431
2432 static int decode_attr_rdev(struct xdr_stream *xdr, uint32_t *bitmap, dev_t *rdev)
2433 {
2434         uint32_t major = 0, minor = 0, *p;
2435
2436         *rdev = MKDEV(0,0);
2437         if (unlikely(bitmap[1] & (FATTR4_WORD1_RAWDEV - 1U)))
2438                 return -EIO;
2439         if (likely(bitmap[1] & FATTR4_WORD1_RAWDEV)) {
2440                 dev_t tmp;
2441
2442                 READ_BUF(8);
2443                 READ32(major);
2444                 READ32(minor);
2445                 tmp = MKDEV(major, minor);
2446                 if (MAJOR(tmp) == major && MINOR(tmp) == minor)
2447                         *rdev = tmp;
2448                 bitmap[1] &= ~ FATTR4_WORD1_RAWDEV;
2449         }
2450         dprintk("%s: rdev=(0x%x:0x%x)\n", __FUNCTION__, major, minor);
2451         return 0;
2452 }
2453
2454 static int decode_attr_space_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2455 {
2456         uint32_t *p;
2457         int status = 0;
2458
2459         *res = 0;
2460         if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_AVAIL - 1U)))
2461                 return -EIO;
2462         if (likely(bitmap[1] & FATTR4_WORD1_SPACE_AVAIL)) {
2463                 READ_BUF(8);
2464                 READ64(*res);
2465                 bitmap[1] &= ~FATTR4_WORD1_SPACE_AVAIL;
2466         }
2467         dprintk("%s: space avail=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2468         return status;
2469 }
2470
2471 static int decode_attr_space_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2472 {
2473         uint32_t *p;
2474         int status = 0;
2475
2476         *res = 0;
2477         if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_FREE - 1U)))
2478                 return -EIO;
2479         if (likely(bitmap[1] & FATTR4_WORD1_SPACE_FREE)) {
2480                 READ_BUF(8);
2481                 READ64(*res);
2482                 bitmap[1] &= ~FATTR4_WORD1_SPACE_FREE;
2483         }
2484         dprintk("%s: space free=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2485         return status;
2486 }
2487
2488 static int decode_attr_space_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2489 {
2490         uint32_t *p;
2491         int status = 0;
2492
2493         *res = 0;
2494         if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_TOTAL - 1U)))
2495                 return -EIO;
2496         if (likely(bitmap[1] & FATTR4_WORD1_SPACE_TOTAL)) {
2497                 READ_BUF(8);
2498                 READ64(*res);
2499                 bitmap[1] &= ~FATTR4_WORD1_SPACE_TOTAL;
2500         }
2501         dprintk("%s: space total=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2502         return status;
2503 }
2504
2505 static int decode_attr_space_used(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *used)
2506 {
2507         uint32_t *p;
2508
2509         *used = 0;
2510         if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_USED - 1U)))
2511                 return -EIO;
2512         if (likely(bitmap[1] & FATTR4_WORD1_SPACE_USED)) {
2513                 READ_BUF(8);
2514                 READ64(*used);
2515                 bitmap[1] &= ~FATTR4_WORD1_SPACE_USED;
2516         }
2517         dprintk("%s: space used=%Lu\n", __FUNCTION__,
2518                         (unsigned long long)*used);
2519         return 0;
2520 }
2521
2522 static int decode_attr_time(struct xdr_stream *xdr, struct timespec *time)
2523 {
2524         uint32_t *p;
2525         uint64_t sec;
2526         uint32_t nsec;
2527
2528         READ_BUF(12);
2529         READ64(sec);
2530         READ32(nsec);
2531         time->tv_sec = (time_t)sec;
2532         time->tv_nsec = (long)nsec;
2533         return 0;
2534 }
2535
2536 static int decode_attr_time_access(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2537 {
2538         int status = 0;
2539
2540         time->tv_sec = 0;
2541         time->tv_nsec = 0;
2542         if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_ACCESS - 1U)))
2543                 return -EIO;
2544         if (likely(bitmap[1] & FATTR4_WORD1_TIME_ACCESS)) {
2545                 status = decode_attr_time(xdr, time);
2546                 bitmap[1] &= ~FATTR4_WORD1_TIME_ACCESS;
2547         }
2548         dprintk("%s: atime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2549         return status;
2550 }
2551
2552 static int decode_attr_time_metadata(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2553 {
2554         int status = 0;
2555
2556         time->tv_sec = 0;
2557         time->tv_nsec = 0;
2558         if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_METADATA - 1U)))
2559                 return -EIO;
2560         if (likely(bitmap[1] & FATTR4_WORD1_TIME_METADATA)) {
2561                 status = decode_attr_time(xdr, time);
2562                 bitmap[1] &= ~FATTR4_WORD1_TIME_METADATA;
2563         }
2564         dprintk("%s: ctime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2565         return status;
2566 }
2567
2568 static int decode_attr_time_modify(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2569 {
2570         int status = 0;
2571
2572         time->tv_sec = 0;
2573         time->tv_nsec = 0;
2574         if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_MODIFY - 1U)))
2575                 return -EIO;
2576         if (likely(bitmap[1] & FATTR4_WORD1_TIME_MODIFY)) {
2577                 status = decode_attr_time(xdr, time);
2578                 bitmap[1] &= ~FATTR4_WORD1_TIME_MODIFY;
2579         }
2580         dprintk("%s: mtime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2581         return status;
2582 }
2583
2584 static int verify_attr_len(struct xdr_stream *xdr, uint32_t *savep, uint32_t attrlen)
2585 {
2586         unsigned int attrwords = XDR_QUADLEN(attrlen);
2587         unsigned int nwords = xdr->p - savep;
2588
2589         if (unlikely(attrwords != nwords)) {
2590                 printk(KERN_WARNING "%s: server returned incorrect attribute length: %u %c %u\n",
2591                                 __FUNCTION__,
2592                                 attrwords << 2,
2593                                 (attrwords < nwords) ? '<' : '>',
2594                                 nwords << 2);
2595                 return -EIO;
2596         }
2597         return 0;
2598 }
2599
2600 static int decode_change_info(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
2601 {
2602         uint32_t *p;
2603
2604         READ_BUF(20);
2605         READ32(cinfo->atomic);
2606         READ64(cinfo->before);
2607         READ64(cinfo->after);
2608         return 0;
2609 }
2610
2611 static int decode_access(struct xdr_stream *xdr, struct nfs4_accessres *access)
2612 {
2613         uint32_t *p;
2614         uint32_t supp, acc;
2615         int status;
2616
2617         status = decode_op_hdr(xdr, OP_ACCESS);
2618         if (status)
2619                 return status;
2620         READ_BUF(8);
2621         READ32(supp);
2622         READ32(acc);
2623         access->supported = supp;
2624         access->access = acc;
2625         return 0;
2626 }
2627
2628 static int decode_close(struct xdr_stream *xdr, struct nfs_closeres *res)
2629 {
2630         uint32_t *p;
2631         int status;
2632
2633         status = decode_op_hdr(xdr, OP_CLOSE);
2634         if (status)
2635                 return status;
2636         READ_BUF(sizeof(res->stateid.data));
2637         COPYMEM(res->stateid.data, sizeof(res->stateid.data));
2638         return 0;
2639 }
2640
2641 static int decode_commit(struct xdr_stream *xdr, struct nfs_writeres *res)
2642 {
2643         uint32_t *p;
2644         int status;
2645
2646         status = decode_op_hdr(xdr, OP_COMMIT);
2647         if (status)
2648                 return status;
2649         READ_BUF(8);
2650         COPYMEM(res->verf->verifier, 8);
2651         return 0;
2652 }
2653
2654 static int decode_create(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
2655 {
2656         uint32_t *p;
2657         uint32_t bmlen;
2658         int status;
2659
2660         status = decode_op_hdr(xdr, OP_CREATE);
2661         if (status)
2662                 return status;
2663         if ((status = decode_change_info(xdr, cinfo)))
2664                 return status;
2665         READ_BUF(4);
2666         READ32(bmlen);
2667         READ_BUF(bmlen << 2);
2668         return 0;
2669 }
2670
2671 static int decode_server_caps(struct xdr_stream *xdr, struct nfs4_server_caps_res *res)
2672 {
2673         uint32_t *savep;
2674         uint32_t attrlen, 
2675                  bitmap[2] = {0};
2676         int status;
2677
2678         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2679                 goto xdr_error;
2680         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2681                 goto xdr_error;
2682         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2683                 goto xdr_error;
2684         if ((status = decode_attr_supported(xdr, bitmap, res->attr_bitmask)) != 0)
2685                 goto xdr_error;
2686         if ((status = decode_attr_link_support(xdr, bitmap, &res->has_links)) != 0)
2687                 goto xdr_error;
2688         if ((status = decode_attr_symlink_support(xdr, bitmap, &res->has_symlinks)) != 0)
2689                 goto xdr_error;
2690         if ((status = decode_attr_aclsupport(xdr, bitmap, &res->acl_bitmask)) != 0)
2691                 goto xdr_error;
2692         status = verify_attr_len(xdr, savep, attrlen);
2693 xdr_error:
2694         if (status != 0)
2695                 printk(KERN_NOTICE "%s: xdr error %d!\n", __FUNCTION__, -status);
2696         return status;
2697 }
2698         
2699 static int decode_statfs(struct xdr_stream *xdr, struct nfs_fsstat *fsstat)
2700 {
2701         uint32_t *savep;
2702         uint32_t attrlen, 
2703                  bitmap[2] = {0};
2704         int status;
2705         
2706         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2707                 goto xdr_error;
2708         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2709                 goto xdr_error;
2710         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2711                 goto xdr_error;
2712
2713         if ((status = decode_attr_files_avail(xdr, bitmap, &fsstat->afiles)) != 0)
2714                 goto xdr_error;
2715         if ((status = decode_attr_files_free(xdr, bitmap, &fsstat->ffiles)) != 0)
2716                 goto xdr_error;
2717         if ((status = decode_attr_files_total(xdr, bitmap, &fsstat->tfiles)) != 0)
2718                 goto xdr_error;
2719         if ((status = decode_attr_space_avail(xdr, bitmap, &fsstat->abytes)) != 0)
2720                 goto xdr_error;
2721         if ((status = decode_attr_space_free(xdr, bitmap, &fsstat->fbytes)) != 0)
2722                 goto xdr_error;
2723         if ((status = decode_attr_space_total(xdr, bitmap, &fsstat->tbytes)) != 0)
2724                 goto xdr_error;
2725
2726         status = verify_attr_len(xdr, savep, attrlen);
2727 xdr_error:
2728         if (status != 0)
2729                 printk(KERN_NOTICE "%s: xdr error %d!\n", __FUNCTION__, -status);
2730         return status;
2731 }
2732
2733 static int decode_pathconf(struct xdr_stream *xdr, struct nfs_pathconf *pathconf)
2734 {
2735         uint32_t *savep;
2736         uint32_t attrlen, 
2737                  bitmap[2] = {0};
2738         int status;
2739         
2740         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2741                 goto xdr_error;
2742         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2743                 goto xdr_error;
2744         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2745                 goto xdr_error;
2746
2747         if ((status = decode_attr_maxlink(xdr, bitmap, &pathconf->max_link)) != 0)
2748                 goto xdr_error;
2749         if ((status = decode_attr_maxname(xdr, bitmap, &pathconf->max_namelen)) != 0)
2750                 goto xdr_error;
2751
2752         status = verify_attr_len(xdr, savep, attrlen);
2753 xdr_error:
2754         if (status != 0)
2755                 printk(KERN_NOTICE "%s: xdr error %d!\n", __FUNCTION__, -status);
2756         return status;
2757 }
2758
2759 static int decode_getfattr(struct xdr_stream *xdr, struct nfs_fattr *fattr, const struct nfs_server *server)
2760 {
2761         uint32_t *savep;
2762         uint32_t attrlen,
2763                  bitmap[2] = {0},
2764                  type;
2765         int status, fmode = 0;
2766
2767         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2768                 goto xdr_error;
2769         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2770                 goto xdr_error;
2771
2772         fattr->bitmap[0] = bitmap[0];
2773         fattr->bitmap[1] = bitmap[1];
2774
2775         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2776                 goto xdr_error;
2777
2778
2779         if ((status = decode_attr_type(xdr, bitmap, &type)) != 0)
2780                 goto xdr_error;
2781         fattr->type = nfs_type2fmt[type].nfs2type;
2782         fmode = nfs_type2fmt[type].mode;
2783
2784         if ((status = decode_attr_change(xdr, bitmap, &fattr->change_attr)) != 0)
2785                 goto xdr_error;
2786         if ((status = decode_attr_size(xdr, bitmap, &fattr->size)) != 0)
2787                 goto xdr_error;
2788         if ((status = decode_attr_fsid(xdr, bitmap, &fattr->fsid_u.nfs4)) != 0)
2789                 goto xdr_error;
2790         if ((status = decode_attr_fileid(xdr, bitmap, &fattr->fileid)) != 0)
2791                 goto xdr_error;
2792         if ((status = decode_attr_mode(xdr, bitmap, &fattr->mode)) != 0)
2793                 goto xdr_error;
2794         fattr->mode |= fmode;
2795         if ((status = decode_attr_nlink(xdr, bitmap, &fattr->nlink)) != 0)
2796                 goto xdr_error;
2797         if ((status = decode_attr_owner(xdr, bitmap, server->nfs4_state, &fattr->uid)) != 0)
2798                 goto xdr_error;
2799         if ((status = decode_attr_group(xdr, bitmap, server->nfs4_state, &fattr->gid)) != 0)
2800                 goto xdr_error;
2801         if ((status = decode_attr_rdev(xdr, bitmap, &fattr->rdev)) != 0)
2802                 goto xdr_error;
2803         if ((status = decode_attr_space_used(xdr, bitmap, &fattr->du.nfs3.used)) != 0)
2804                 goto xdr_error;
2805         if ((status = decode_attr_time_access(xdr, bitmap, &fattr->atime)) != 0)
2806                 goto xdr_error;
2807         if ((status = decode_attr_time_metadata(xdr, bitmap, &fattr->ctime)) != 0)
2808                 goto xdr_error;
2809         if ((status = decode_attr_time_modify(xdr, bitmap, &fattr->mtime)) != 0)
2810                 goto xdr_error;
2811         if ((status = verify_attr_len(xdr, savep, attrlen)) == 0) {
2812                 fattr->valid = NFS_ATTR_FATTR | NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4;
2813                 fattr->timestamp = jiffies;
2814         }
2815 xdr_error:
2816         if (status != 0)
2817                 printk(KERN_NOTICE "%s: xdr error %d!\n", __FUNCTION__, -status);
2818         return status;
2819 }
2820
2821
2822 static int decode_fsinfo(struct xdr_stream *xdr, struct nfs_fsinfo *fsinfo)
2823 {
2824         uint32_t *savep;
2825         uint32_t attrlen, bitmap[2];
2826         int status;
2827
2828         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2829                 goto xdr_error;
2830         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2831                 goto xdr_error;
2832         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2833                 goto xdr_error;
2834
2835         fsinfo->rtmult = fsinfo->wtmult = 512;  /* ??? */
2836
2837         if ((status = decode_attr_lease_time(xdr, bitmap, &fsinfo->lease_time)) != 0)
2838                 goto xdr_error;
2839         if ((status = decode_attr_maxfilesize(xdr, bitmap, &fsinfo->maxfilesize)) != 0)
2840                 goto xdr_error;
2841         if ((status = decode_attr_maxread(xdr, bitmap, &fsinfo->rtmax)) != 0)
2842                 goto xdr_error;
2843         fsinfo->rtpref = fsinfo->dtpref = fsinfo->rtmax;
2844         if ((status = decode_attr_maxwrite(xdr, bitmap, &fsinfo->wtmax)) != 0)
2845                 goto xdr_error;
2846         fsinfo->wtpref = fsinfo->wtmax;
2847
2848         status = verify_attr_len(xdr, savep, attrlen);
2849 xdr_error:
2850         if (status != 0)
2851                 printk(KERN_NOTICE "%s: xdr error %d!\n", __FUNCTION__, -status);
2852         return status;
2853 }
2854
2855 static int decode_getfh(struct xdr_stream *xdr, struct nfs_fh *fh)
2856 {
2857         uint32_t *p;
2858         uint32_t len;
2859         int status;
2860
2861         status = decode_op_hdr(xdr, OP_GETFH);
2862         if (status)
2863                 return status;
2864         /* Zero handle first to allow comparisons */
2865         memset(fh, 0, sizeof(*fh));
2866
2867         READ_BUF(4);
2868         READ32(len);
2869         if (len > NFS4_FHSIZE)
2870                 return -EIO;
2871         fh->size = len;
2872         READ_BUF(len);
2873         COPYMEM(fh->data, len);
2874         return 0;
2875 }
2876
2877 static int decode_link(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
2878 {
2879         int status;
2880         
2881         status = decode_op_hdr(xdr, OP_LINK);
2882         if (status)
2883                 return status;
2884         return decode_change_info(xdr, cinfo);
2885 }
2886
2887 /*
2888  * We create the owner, so we know a proper owner.id length is 4.
2889  */
2890 static int decode_lock_denied (struct xdr_stream *xdr, struct nfs_lock_denied *denied)
2891 {
2892         uint32_t *p;
2893         uint32_t namelen;
2894
2895         READ_BUF(32);
2896         READ64(denied->offset);
2897         READ64(denied->length);
2898         READ32(denied->type);
2899         READ64(denied->owner.clientid);
2900         READ32(namelen);
2901         READ_BUF(namelen);
2902         if (namelen == 4)
2903                 READ32(denied->owner.id);
2904         return -NFS4ERR_DENIED;
2905 }
2906
2907 static int decode_lock(struct xdr_stream *xdr, struct nfs_lockres *res)
2908 {
2909         uint32_t *p;
2910         int status;
2911
2912         status = decode_op_hdr(xdr, OP_LOCK);
2913         if (status == 0) {
2914                 READ_BUF(sizeof(nfs4_stateid));
2915                 COPYMEM(&res->u.stateid, sizeof(res->u.stateid));
2916         } else if (status == -NFS4ERR_DENIED)
2917                 return decode_lock_denied(xdr, &res->u.denied);
2918         return status;
2919 }
2920
2921 static int decode_lockt(struct xdr_stream *xdr, struct nfs_lockres *res)
2922 {
2923         int status;
2924         status = decode_op_hdr(xdr, OP_LOCKT);
2925         if (status == -NFS4ERR_DENIED)
2926                 return decode_lock_denied(xdr, &res->u.denied);
2927         return status;
2928 }
2929
2930 static int decode_locku(struct xdr_stream *xdr, struct nfs_lockres *res)
2931 {
2932         uint32_t *p;
2933         int status;
2934
2935         status = decode_op_hdr(xdr, OP_LOCKU);
2936         if (status == 0) {
2937                 READ_BUF(sizeof(nfs4_stateid));
2938                 COPYMEM(&res->u.stateid, sizeof(res->u.stateid));
2939         }
2940         return status;
2941 }
2942
2943 static int decode_lookup(struct xdr_stream *xdr)
2944 {
2945         return decode_op_hdr(xdr, OP_LOOKUP);
2946 }
2947
2948 /* This is too sick! */
2949 static int decode_space_limit(struct xdr_stream *xdr, u64 *maxsize)
2950 {
2951         uint32_t *p;
2952         uint32_t limit_type, nblocks, blocksize;
2953
2954         READ_BUF(12);
2955         READ32(limit_type);
2956         switch (limit_type) {
2957                 case 1:
2958                         READ64(*maxsize);
2959                         break;
2960                 case 2:
2961                         READ32(nblocks);
2962                         READ32(blocksize);
2963                         *maxsize = (uint64_t)nblocks * (uint64_t)blocksize;
2964         }
2965         return 0;
2966 }
2967
2968 static int decode_delegation(struct xdr_stream *xdr, struct nfs_openres *res)
2969 {
2970         uint32_t *p;
2971         uint32_t delegation_type;
2972
2973         READ_BUF(4);
2974         READ32(delegation_type);
2975         if (delegation_type == NFS4_OPEN_DELEGATE_NONE) {
2976                 res->delegation_type = 0;
2977                 return 0;
2978         }
2979         READ_BUF(20);
2980         COPYMEM(res->delegation.data, sizeof(res->delegation.data));
2981         READ32(res->do_recall);
2982         switch (delegation_type) {
2983                 case NFS4_OPEN_DELEGATE_READ:
2984                         res->delegation_type = FMODE_READ;
2985                         break;
2986                 case NFS4_OPEN_DELEGATE_WRITE:
2987                         res->delegation_type = FMODE_WRITE|FMODE_READ;
2988                         if (decode_space_limit(xdr, &res->maxsize) < 0)
2989                                 return -EIO;
2990         }
2991         return decode_ace(xdr, NULL, res->server->nfs4_state);
2992 }
2993
2994 static int decode_open(struct xdr_stream *xdr, struct nfs_openres *res)
2995 {
2996         uint32_t *p;
2997         uint32_t bmlen;
2998         int status;
2999
3000         status = decode_op_hdr(xdr, OP_OPEN);
3001         if (status)
3002                 return status;
3003         READ_BUF(sizeof(res->stateid.data));
3004         COPYMEM(res->stateid.data, sizeof(res->stateid.data));
3005
3006         decode_change_info(xdr, &res->cinfo);
3007
3008         READ_BUF(8);
3009         READ32(res->rflags);
3010         READ32(bmlen);
3011         if (bmlen > 10)
3012                 goto xdr_error;
3013
3014         READ_BUF(bmlen << 2);
3015         p += bmlen;
3016         return decode_delegation(xdr, res);
3017 xdr_error:
3018         printk(KERN_NOTICE "%s: xdr error!\n", __FUNCTION__);
3019         return -EIO;
3020 }
3021
3022 static int decode_open_confirm(struct xdr_stream *xdr, struct nfs_open_confirmres *res)
3023 {
3024         uint32_t *p;
3025         int status;
3026
3027         status = decode_op_hdr(xdr, OP_OPEN_CONFIRM);
3028         if (status)
3029                 return status;
3030         READ_BUF(sizeof(res->stateid.data));
3031         COPYMEM(res->stateid.data, sizeof(res->stateid.data));
3032         return 0;
3033 }
3034
3035 static int decode_open_downgrade(struct xdr_stream *xdr, struct nfs_closeres *res)
3036 {
3037         uint32_t *p;
3038         int status;
3039
3040         status = decode_op_hdr(xdr, OP_OPEN_DOWNGRADE);
3041         if (status)
3042                 return status;
3043         READ_BUF(sizeof(res->stateid.data));
3044         COPYMEM(res->stateid.data, sizeof(res->stateid.data));
3045         return 0;
3046 }
3047
3048 static int decode_putfh(struct xdr_stream *xdr)
3049 {
3050         return decode_op_hdr(xdr, OP_PUTFH);
3051 }
3052
3053 static int decode_putrootfh(struct xdr_stream *xdr)
3054 {
3055         return decode_op_hdr(xdr, OP_PUTROOTFH);
3056 }
3057
3058 static int decode_read(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs_readres *res)
3059 {
3060         struct kvec *iov = req->rq_rcv_buf.head;
3061         uint32_t *p;
3062         uint32_t count, eof, recvd, hdrlen;
3063         int status;
3064
3065         status = decode_op_hdr(xdr, OP_READ);
3066         if (status)
3067                 return status;
3068         READ_BUF(8);
3069         READ32(eof);
3070         READ32(count);
3071         hdrlen = (u8 *) p - (u8 *) iov->iov_base;
3072         recvd = req->rq_rcv_buf.len - hdrlen;
3073         if (count > recvd) {
3074                 printk(KERN_WARNING "NFS: server cheating in read reply: "
3075                                 "count %u > recvd %u\n", count, recvd);
3076                 count = recvd;
3077                 eof = 0;
3078         }
3079         xdr_read_pages(xdr, count);
3080         res->eof = eof;
3081         res->count = count;
3082         return 0;
3083 }
3084
3085 static int decode_readdir(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs4_readdir_res *readdir)
3086 {
3087         struct xdr_buf  *rcvbuf = &req->rq_rcv_buf;
3088         struct page     *page = *rcvbuf->pages;
3089         struct kvec     *iov = rcvbuf->head;
3090         unsigned int    nr, pglen = rcvbuf->page_len;
3091         uint32_t        *end, *entry, *p, *kaddr;
3092         uint32_t        len, attrlen;
3093         int             hdrlen, recvd, status;
3094
3095         status = decode_op_hdr(xdr, OP_READDIR);
3096         if (status)
3097                 return status;
3098         READ_BUF(8);
3099         COPYMEM(readdir->verifier.data, 8);
3100         dprintk("%s: verifier = 0x%x%x\n",
3101                         __FUNCTION__,
3102                         ((u32 *)readdir->verifier.data)[0],
3103                         ((u32 *)readdir->verifier.data)[1]);
3104
3105
3106         hdrlen = (char *) p - (char *) iov->iov_base;
3107         recvd = rcvbuf->len - hdrlen;
3108         if (pglen > recvd)
3109                 pglen = recvd;
3110         xdr_read_pages(xdr, pglen);
3111
3112         BUG_ON(pglen + readdir->pgbase > PAGE_CACHE_SIZE);
3113         kaddr = p = (uint32_t *) kmap_atomic(page, KM_USER0);
3114         end = (uint32_t *) ((char *)p + pglen + readdir->pgbase);
3115         entry = p;
3116         for (nr = 0; *p++; nr++) {
3117                 if (p + 3 > end)
3118                         goto short_pkt;
3119                 dprintk("cookie = %Lu, ", *((unsigned long long *)p));
3120                 p += 2;                 /* cookie */
3121                 len = ntohl(*p++);      /* filename length */
3122                 if (len > NFS4_MAXNAMLEN) {
3123                         printk(KERN_WARNING "NFS: giant filename in readdir (len 0x%x)\n", len);
3124                         goto err_unmap;
3125                 }
3126                 dprintk("filename = %*s\n", len, (char *)p);
3127                 p += XDR_QUADLEN(len);
3128                 if (p + 1 > end)
3129                         goto short_pkt;
3130                 len = ntohl(*p++);      /* bitmap length */
3131                 p += len;
3132                 if (p + 1 > end)
3133                         goto short_pkt;
3134                 attrlen = XDR_QUADLEN(ntohl(*p++));
3135                 p += attrlen;           /* attributes */
3136                 if (p + 2 > end)
3137                         goto short_pkt;
3138                 entry = p;
3139         }
3140         if (!nr && (entry[0] != 0 || entry[1] == 0))
3141                 goto short_pkt;
3142 out:    
3143         kunmap_atomic(kaddr, KM_USER0);
3144         return 0;
3145 short_pkt:
3146         dprintk("%s: short packet at entry %d\n", __FUNCTION__, nr);
3147         entry[0] = entry[1] = 0;
3148         /* truncate listing ? */
3149         if (!nr) {
3150                 printk(KERN_NOTICE "NFS: readdir reply truncated!\n");
3151                 entry[1] = 1;
3152         }
3153         goto out;
3154 err_unmap:
3155         kunmap_atomic(kaddr, KM_USER0);
3156         return -errno_NFSERR_IO;
3157 }
3158
3159 static int decode_readlink(struct xdr_stream *xdr, struct rpc_rqst *req)
3160 {
3161         struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
3162         struct kvec *iov = rcvbuf->head;
3163         int hdrlen, len, recvd;
3164         uint32_t *p;
3165         char *kaddr;
3166         int status;
3167
3168         status = decode_op_hdr(xdr, OP_READLINK);
3169         if (status)
3170                 return status;
3171
3172         /* Convert length of symlink */
3173         READ_BUF(4);
3174         READ32(len);
3175         if (len >= rcvbuf->page_len || len <= 0) {
3176                 dprintk(KERN_WARNING "nfs: server returned giant symlink!\n");
3177                 return -ENAMETOOLONG;
3178         }
3179         hdrlen = (char *) xdr->p - (char *) iov->iov_base;
3180         recvd = req->rq_rcv_buf.len - hdrlen;
3181         if (recvd < len) {
3182                 printk(KERN_WARNING "NFS: server cheating in readlink reply: "
3183                                 "count %u > recvd %u\n", len, recvd);
3184                 return -EIO;
3185         }
3186         xdr_read_pages(xdr, len);
3187         /*
3188          * The XDR encode routine has set things up so that
3189          * the link text will be copied directly into the
3190          * buffer.  We just have to do overflow-checking,
3191          * and and null-terminate the text (the VFS expects
3192          * null-termination).
3193          */
3194         kaddr = (char *)kmap_atomic(rcvbuf->pages[0], KM_USER0);
3195         kaddr[len+rcvbuf->page_base] = '\0';
3196         kunmap_atomic(kaddr, KM_USER0);
3197         return 0;
3198 }
3199
3200 static int decode_remove(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
3201 {
3202         int status;
3203
3204         status = decode_op_hdr(xdr, OP_REMOVE);
3205         if (status)
3206                 goto out;
3207         status = decode_change_info(xdr, cinfo);
3208 out:
3209         return status;
3210 }
3211
3212 static int decode_rename(struct xdr_stream *xdr, struct nfs4_change_info *old_cinfo,
3213               struct nfs4_change_info *new_cinfo)
3214 {
3215         int status;
3216
3217         status = decode_op_hdr(xdr, OP_RENAME);
3218         if (status)
3219                 goto out;
3220         if ((status = decode_change_info(xdr, old_cinfo)))
3221                 goto out;
3222         status = decode_change_info(xdr, new_cinfo);
3223 out:
3224         return status;
3225 }
3226
3227 static int decode_renew(struct xdr_stream *xdr)
3228 {
3229         return decode_op_hdr(xdr, OP_RENEW);
3230 }
3231
3232 static int decode_getacl(struct xdr_stream *xdr, struct rpc_rqst *req,
3233                 size_t *acl_len)
3234 {
3235         uint32_t *savep;
3236         uint32_t attrlen,
3237                  bitmap[2] = {0};
3238         struct kvec *iov = req->rq_rcv_buf.head;
3239         int status;
3240
3241         *acl_len = 0;
3242         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3243                 goto out;
3244         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3245                 goto out;
3246         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3247                 goto out;
3248
3249         if (unlikely(bitmap[0] & (FATTR4_WORD0_ACL - 1U)))
3250                 return -EIO;
3251         if (likely(bitmap[0] & FATTR4_WORD0_ACL)) {
3252                 int hdrlen, recvd;
3253
3254                 /* We ignore &savep and don't do consistency checks on
3255                  * the attr length.  Let userspace figure it out.... */
3256                 hdrlen = (u8 *)xdr->p - (u8 *)iov->iov_base;
3257                 recvd = req->rq_rcv_buf.len - hdrlen;
3258                 if (attrlen > recvd) {
3259                         printk(KERN_WARNING "NFS: server cheating in getattr"
3260                                         " acl reply: attrlen %u > recvd %u\n",
3261                                         attrlen, recvd);
3262                         return -EINVAL;
3263                 }
3264                 if (attrlen <= *acl_len)
3265                         xdr_read_pages(xdr, attrlen);
3266                 *acl_len = attrlen;
3267         }
3268
3269 out:
3270         return status;
3271 }
3272
3273 static int
3274 decode_savefh(struct xdr_stream *xdr)
3275 {
3276         return decode_op_hdr(xdr, OP_SAVEFH);
3277 }
3278
3279 static int decode_setattr(struct xdr_stream *xdr, struct nfs_setattrres *res)
3280 {
3281         uint32_t *p;
3282         uint32_t bmlen;
3283         int status;
3284
3285         
3286         status = decode_op_hdr(xdr, OP_SETATTR);
3287         if (status)
3288                 return status;
3289         READ_BUF(4);
3290         READ32(bmlen);
3291         READ_BUF(bmlen << 2);
3292         return 0;
3293 }
3294
3295 static int decode_setclientid(struct xdr_stream *xdr, struct nfs4_client *clp)
3296 {
3297         uint32_t *p;
3298         uint32_t opnum;
3299         int32_t nfserr;
3300
3301         READ_BUF(8);
3302         READ32(opnum);
3303         if (opnum != OP_SETCLIENTID) {
3304                 printk(KERN_NOTICE
3305                                 "nfs4_decode_setclientid: Server returned operation"
3306                                 " %d\n", opnum);
3307                 return -EIO;
3308         }
3309         READ32(nfserr);
3310         if (nfserr == NFS_OK) {
3311                 READ_BUF(8 + sizeof(clp->cl_confirm.data));
3312                 READ64(clp->cl_clientid);
3313                 COPYMEM(clp->cl_confirm.data, sizeof(clp->cl_confirm.data));
3314         } else if (nfserr == NFSERR_CLID_INUSE) {
3315                 uint32_t len;
3316
3317                 /* skip netid string */
3318                 READ_BUF(4);
3319                 READ32(len);
3320                 READ_BUF(len);
3321
3322                 /* skip uaddr string */
3323                 READ_BUF(4);
3324                 READ32(len);
3325                 READ_BUF(len);
3326                 return -NFSERR_CLID_INUSE;
3327         } else
3328                 return -nfs_stat_to_errno(nfserr);
3329
3330         return 0;
3331 }
3332
3333 static int decode_setclientid_confirm(struct xdr_stream *xdr)
3334 {
3335         return decode_op_hdr(xdr, OP_SETCLIENTID_CONFIRM);
3336 }
3337
3338 static int decode_write(struct xdr_stream *xdr, struct nfs_writeres *res)
3339 {
3340         uint32_t *p;
3341         int status;
3342
3343         status = decode_op_hdr(xdr, OP_WRITE);
3344         if (status)
3345                 return status;
3346
3347         READ_BUF(16);
3348         READ32(res->count);
3349         READ32(res->verf->committed);
3350         COPYMEM(res->verf->verifier, 8);
3351         return 0;
3352 }
3353
3354 static int decode_delegreturn(struct xdr_stream *xdr)
3355 {
3356         return decode_op_hdr(xdr, OP_DELEGRETURN);
3357 }
3358
3359 /*
3360  * Decode OPEN_DOWNGRADE response
3361  */
3362 static int nfs4_xdr_dec_open_downgrade(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_closeres *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         status = decode_compound_hdr(&xdr, &hdr);
3370         if (status)
3371                 goto out;
3372         status = decode_putfh(&xdr);
3373         if (status)
3374                 goto out;
3375         status = decode_open_downgrade(&xdr, res);
3376 out:
3377         return status;
3378 }
3379
3380 /*
3381  * END OF "GENERIC" DECODE ROUTINES.
3382  */
3383
3384 /*
3385  * Decode ACCESS response
3386  */
3387 static int nfs4_xdr_dec_access(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_accessres *res)
3388 {
3389         struct xdr_stream xdr;
3390         struct compound_hdr hdr;
3391         int status;
3392         
3393         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3394         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3395                 goto out;
3396         if ((status = decode_putfh(&xdr)) == 0)
3397                 status = decode_access(&xdr, res);
3398 out:
3399         return status;
3400 }
3401
3402 /*
3403  * Decode LOOKUP response
3404  */
3405 static int nfs4_xdr_dec_lookup(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_lookup_res *res)
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                 goto out;
3416         if ((status = decode_lookup(&xdr)) != 0)
3417                 goto out;
3418         if ((status = decode_getfh(&xdr, res->fh)) != 0)
3419                 goto out;
3420         status = decode_getfattr(&xdr, res->fattr, res->server);
3421 out:
3422         return status;
3423 }
3424
3425 /*
3426  * Decode LOOKUP_ROOT response
3427  */
3428 static int nfs4_xdr_dec_lookup_root(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_lookup_res *res)
3429 {
3430         struct xdr_stream xdr;
3431         struct compound_hdr hdr;
3432         int status;
3433         
3434         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3435         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3436                 goto out;
3437         if ((status = decode_putrootfh(&xdr)) != 0)
3438                 goto out;
3439         if ((status = decode_getfh(&xdr, res->fh)) == 0)
3440                 status = decode_getfattr(&xdr, res->fattr, res->server);
3441 out:
3442         return status;
3443 }
3444
3445 /*
3446  * Decode REMOVE response
3447  */
3448 static int nfs4_xdr_dec_remove(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_change_info *cinfo)
3449 {
3450         struct xdr_stream xdr;
3451         struct compound_hdr hdr;
3452         int status;
3453         
3454         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3455         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3456                 goto out;
3457         if ((status = decode_putfh(&xdr)) == 0)
3458                 status = decode_remove(&xdr, cinfo);
3459 out:
3460         return status;
3461 }
3462
3463 /*
3464  * Decode RENAME response
3465  */
3466 static int nfs4_xdr_dec_rename(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_rename_res *res)
3467 {
3468         struct xdr_stream xdr;
3469         struct compound_hdr hdr;
3470         int status;
3471         
3472         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3473         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3474                 goto out;
3475         if ((status = decode_putfh(&xdr)) != 0)
3476                 goto out;
3477         if ((status = decode_savefh(&xdr)) != 0)
3478                 goto out;
3479         if ((status = decode_putfh(&xdr)) != 0)
3480                 goto out;
3481         status = decode_rename(&xdr, &res->old_cinfo, &res->new_cinfo);
3482 out:
3483         return status;
3484 }
3485
3486 /*
3487  * Decode LINK response
3488  */
3489 static int nfs4_xdr_dec_link(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_change_info *cinfo)
3490 {
3491         struct xdr_stream xdr;
3492         struct compound_hdr hdr;
3493         int status;
3494         
3495         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3496         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3497                 goto out;
3498         if ((status = decode_putfh(&xdr)) != 0)
3499                 goto out;
3500         if ((status = decode_savefh(&xdr)) != 0)
3501                 goto out;
3502         if ((status = decode_putfh(&xdr)) != 0)
3503                 goto out;
3504         status = decode_link(&xdr, cinfo);
3505 out:
3506         return status;
3507 }
3508
3509 /*
3510  * Decode CREATE response
3511  */
3512 static int nfs4_xdr_dec_create(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_create_res *res)
3513 {
3514         struct xdr_stream xdr;
3515         struct compound_hdr hdr;
3516         int status;
3517         
3518         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3519         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3520                 goto out;
3521         if ((status = decode_putfh(&xdr)) != 0)
3522                 goto out;
3523         if ((status = decode_create(&xdr,&res->dir_cinfo)) != 0)
3524                 goto out;
3525         if ((status = decode_getfh(&xdr, res->fh)) != 0)
3526                 goto out;
3527         status = decode_getfattr(&xdr, res->fattr, res->server);
3528         if (status == NFS4ERR_DELAY)
3529                 status = 0;
3530 out:
3531         return status;
3532 }
3533
3534 /*
3535  * Decode SYMLINK response
3536  */
3537 static int nfs4_xdr_dec_symlink(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_create_res *res)
3538 {
3539         return nfs4_xdr_dec_create(rqstp, p, res);
3540 }
3541
3542 /*
3543  * Decode GETATTR response
3544  */
3545 static int nfs4_xdr_dec_getattr(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_getattr_res *res)
3546 {
3547         struct xdr_stream xdr;
3548         struct compound_hdr hdr;
3549         int status;
3550         
3551         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3552         status = decode_compound_hdr(&xdr, &hdr);
3553         if (status)
3554                 goto out;
3555         status = decode_putfh(&xdr);
3556         if (status)
3557                 goto out;
3558         status = decode_getfattr(&xdr, res->fattr, res->server);
3559 out:
3560         return status;
3561
3562 }
3563
3564 /*
3565  * Encode an SETACL request
3566  */
3567 static int
3568 nfs4_xdr_enc_setacl(struct rpc_rqst *req, uint32_t *p, struct nfs_setaclargs *args)
3569 {
3570         struct xdr_stream xdr;
3571         struct compound_hdr hdr = {
3572                 .nops   = 2,
3573         };
3574         int status;
3575
3576         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
3577         encode_compound_hdr(&xdr, &hdr);
3578         status = encode_putfh(&xdr, args->fh);
3579         if (status)
3580                 goto out;
3581         status = encode_setacl(&xdr, args);
3582 out:
3583         return status;
3584 }
3585 /*
3586  * Decode SETACL response
3587  */
3588 static int
3589 nfs4_xdr_dec_setacl(struct rpc_rqst *rqstp, uint32_t *p, void *res)
3590 {
3591         struct xdr_stream xdr;
3592         struct compound_hdr hdr;
3593         int status;
3594
3595         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3596         status = decode_compound_hdr(&xdr, &hdr);
3597         if (status)
3598                 goto out;
3599         status = decode_putfh(&xdr);
3600         if (status)
3601                 goto out;
3602         status = decode_setattr(&xdr, res);
3603 out:
3604         return status;
3605 }
3606
3607 /*
3608  * Decode GETACL response
3609  */
3610 static int
3611 nfs4_xdr_dec_getacl(struct rpc_rqst *rqstp, uint32_t *p, size_t *acl_len)
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_getacl(&xdr, rqstp, acl_len);
3625
3626 out:
3627         return status;
3628 }
3629
3630 /*
3631  * Decode CLOSE response
3632  */
3633 static int nfs4_xdr_dec_close(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_closeres *res)
3634 {
3635         struct xdr_stream xdr;
3636         struct compound_hdr hdr;
3637         int status;
3638
3639         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3640         status = decode_compound_hdr(&xdr, &hdr);
3641         if (status)
3642                 goto out;
3643         status = decode_putfh(&xdr);
3644         if (status)
3645                 goto out;
3646         status = decode_close(&xdr, res);
3647 out:
3648         return status;
3649 }
3650
3651 /*
3652  * Decode OPEN response
3653  */
3654 static int nfs4_xdr_dec_open(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_openres *res)
3655 {
3656         struct xdr_stream xdr;
3657         struct compound_hdr hdr;
3658         int status;
3659
3660         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3661         status = decode_compound_hdr(&xdr, &hdr);
3662         if (status)
3663                 goto out;
3664         status = decode_putfh(&xdr);
3665         if (status)
3666                 goto out;
3667         status = decode_open(&xdr, res);
3668         if (status)
3669                 goto out;
3670         status = decode_getfh(&xdr, &res->fh);
3671         if (status)
3672                 goto out;
3673         status = decode_getfattr(&xdr, res->f_attr, res->server);
3674         if (status == NFS4ERR_DELAY)
3675                 status = 0;
3676 out:
3677         return status;
3678 }
3679
3680 /*
3681  * Decode OPEN_CONFIRM response
3682  */
3683 static int nfs4_xdr_dec_open_confirm(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_open_confirmres *res)
3684 {
3685         struct xdr_stream xdr;
3686         struct compound_hdr hdr;
3687         int status;
3688
3689         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3690         status = decode_compound_hdr(&xdr, &hdr);
3691         if (status)
3692                 goto out;
3693         status = decode_putfh(&xdr);
3694         if (status)
3695                 goto out;
3696         status = decode_open_confirm(&xdr, res);
3697 out:
3698         return status;
3699 }
3700
3701 /*
3702  * Decode OPEN response
3703  */
3704 static int nfs4_xdr_dec_open_noattr(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_openres *res)
3705 {
3706         struct xdr_stream xdr;
3707         struct compound_hdr hdr;
3708         int status;
3709
3710         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3711         status = decode_compound_hdr(&xdr, &hdr);
3712         if (status)
3713                 goto out;
3714         status = decode_putfh(&xdr);
3715         if (status)
3716                 goto out;
3717         status = decode_open(&xdr, res);
3718 out:
3719         return status;
3720 }
3721
3722 /*
3723  * Decode SETATTR response
3724  */
3725 static int nfs4_xdr_dec_setattr(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_setattrres *res)
3726 {
3727         struct xdr_stream xdr;
3728         struct compound_hdr hdr;
3729         int status;
3730
3731         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3732         status = decode_compound_hdr(&xdr, &hdr);
3733         if (status)
3734                 goto out;
3735         status = decode_putfh(&xdr);
3736         if (status)
3737                 goto out;
3738         status = decode_setattr(&xdr, res);
3739         if (status)
3740                 goto out;
3741         status = decode_getfattr(&xdr, res->fattr, res->server);
3742         if (status == NFS4ERR_DELAY)
3743                 status = 0;
3744 out:
3745         return status;
3746 }
3747
3748 /*
3749  * Decode LOCK response
3750  */
3751 static int nfs4_xdr_dec_lock(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_lockres *res)
3752 {
3753         struct xdr_stream xdr;
3754         struct compound_hdr hdr;
3755         int status;
3756
3757         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3758         status = decode_compound_hdr(&xdr, &hdr);
3759         if (status)
3760                 goto out;
3761         status = decode_putfh(&xdr);
3762         if (status)
3763                 goto out;
3764         status = decode_lock(&xdr, res);
3765 out:
3766         return status;
3767 }
3768
3769 /*
3770  * Decode LOCKT response
3771  */
3772 static int nfs4_xdr_dec_lockt(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_lockres *res)
3773 {
3774         struct xdr_stream xdr;
3775         struct compound_hdr hdr;
3776         int status;
3777
3778         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3779         status = decode_compound_hdr(&xdr, &hdr);
3780         if (status)
3781                 goto out;
3782         status = decode_putfh(&xdr);
3783         if (status)
3784                 goto out;
3785         status = decode_lockt(&xdr, res);
3786 out:
3787         return status;
3788 }
3789
3790 /*
3791  * Decode LOCKU response
3792  */
3793 static int nfs4_xdr_dec_locku(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_lockres *res)
3794 {
3795         struct xdr_stream xdr;
3796         struct compound_hdr hdr;
3797         int status;
3798
3799         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3800         status = decode_compound_hdr(&xdr, &hdr);
3801         if (status)
3802                 goto out;
3803         status = decode_putfh(&xdr);
3804         if (status)
3805                 goto out;
3806         status = decode_locku(&xdr, res);
3807 out:
3808         return status;
3809 }
3810
3811 /*
3812  * Decode READLINK response
3813  */
3814 static int nfs4_xdr_dec_readlink(struct rpc_rqst *rqstp, uint32_t *p, void *res)
3815 {
3816         struct xdr_stream xdr;
3817         struct compound_hdr hdr;
3818         int status;
3819
3820         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3821         status = decode_compound_hdr(&xdr, &hdr);
3822         if (status)
3823                 goto out;
3824         status = decode_putfh(&xdr);
3825         if (status)
3826                 goto out;
3827         status = decode_readlink(&xdr, rqstp);
3828 out:
3829         return status;
3830 }
3831
3832 /*
3833  * Decode READDIR response
3834  */
3835 static int nfs4_xdr_dec_readdir(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_readdir_res *res)
3836 {
3837         struct xdr_stream xdr;
3838         struct compound_hdr hdr;
3839         int status;
3840
3841         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3842         status = decode_compound_hdr(&xdr, &hdr);
3843         if (status)
3844                 goto out;
3845         status = decode_putfh(&xdr);
3846         if (status)
3847                 goto out;
3848         status = decode_readdir(&xdr, rqstp, res);
3849 out:
3850         return status;
3851 }
3852
3853 /*
3854  * Decode Read response
3855  */
3856 static int nfs4_xdr_dec_read(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_readres *res)
3857 {
3858         struct xdr_stream xdr;
3859         struct compound_hdr hdr;
3860         int status;
3861
3862         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3863         status = decode_compound_hdr(&xdr, &hdr);
3864         if (status)
3865                 goto out;
3866         status = decode_putfh(&xdr);
3867         if (status)
3868                 goto out;
3869         status = decode_read(&xdr, rqstp, res);
3870         if (!status)
3871                 status = res->count;
3872 out:
3873         return status;
3874 }
3875
3876 /*
3877  * Decode WRITE response
3878  */
3879 static int nfs4_xdr_dec_write(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_writeres *res)
3880 {
3881         struct xdr_stream xdr;
3882         struct compound_hdr hdr;
3883         int status;
3884
3885         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3886         status = decode_compound_hdr(&xdr, &hdr);
3887         if (status)
3888                 goto out;
3889         status = decode_putfh(&xdr);
3890         if (status)
3891                 goto out;
3892         status = decode_write(&xdr, res);
3893         if (!status)
3894                 status = res->count;
3895 out:
3896         return status;
3897 }
3898
3899 /*
3900  * Decode COMMIT response
3901  */
3902 static int nfs4_xdr_dec_commit(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_writeres *res)
3903 {
3904         struct xdr_stream xdr;
3905         struct compound_hdr hdr;
3906         int status;
3907
3908         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3909         status = decode_compound_hdr(&xdr, &hdr);
3910         if (status)
3911                 goto out;
3912         status = decode_putfh(&xdr);
3913         if (status)
3914                 goto out;
3915         status = decode_commit(&xdr, res);
3916 out:
3917         return status;
3918 }
3919
3920 /*
3921  * FSINFO request
3922  */
3923 static int nfs4_xdr_dec_fsinfo(struct rpc_rqst *req, uint32_t *p, struct nfs_fsinfo *fsinfo)
3924 {
3925         struct xdr_stream xdr;
3926         struct compound_hdr hdr;
3927         int status;
3928
3929         xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
3930         status = decode_compound_hdr(&xdr, &hdr);
3931         if (!status)
3932                 status = decode_putfh(&xdr);
3933         if (!status)
3934                 status = decode_fsinfo(&xdr, fsinfo);
3935         if (!status)
3936                 status = -nfs_stat_to_errno(hdr.status);
3937         return status;
3938 }
3939
3940 /*
3941  * PATHCONF request
3942  */
3943 static int nfs4_xdr_dec_pathconf(struct rpc_rqst *req, uint32_t *p, struct nfs_pathconf *pathconf)
3944 {
3945         struct xdr_stream xdr;
3946         struct compound_hdr hdr;
3947         int status;
3948
3949         xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
3950         status = decode_compound_hdr(&xdr, &hdr);
3951         if (!status)
3952                 status = decode_putfh(&xdr);
3953         if (!status)
3954                 status = decode_pathconf(&xdr, pathconf);
3955         return status;
3956 }
3957
3958 /*
3959  * STATFS request
3960  */
3961 static int nfs4_xdr_dec_statfs(struct rpc_rqst *req, uint32_t *p, struct nfs_fsstat *fsstat)
3962 {
3963         struct xdr_stream xdr;
3964         struct compound_hdr hdr;
3965         int status;
3966
3967         xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
3968         status = decode_compound_hdr(&xdr, &hdr);
3969         if (!status)
3970                 status = decode_putfh(&xdr);
3971         if (!status)
3972                 status = decode_statfs(&xdr, fsstat);
3973         return status;
3974 }
3975
3976 /*
3977  * GETATTR_BITMAP request
3978  */
3979 static int nfs4_xdr_dec_server_caps(struct rpc_rqst *req, uint32_t *p, struct nfs4_server_caps_res *res)
3980 {
3981         struct xdr_stream xdr;
3982         struct compound_hdr hdr;
3983         int status;
3984
3985         xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
3986         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3987                 goto out;
3988         if ((status = decode_putfh(&xdr)) != 0)
3989                 goto out;
3990         status = decode_server_caps(&xdr, res);
3991 out:
3992         return status;
3993 }
3994
3995 /*
3996  * Decode RENEW response
3997  */
3998 static int nfs4_xdr_dec_renew(struct rpc_rqst *rqstp, uint32_t *p, void *dummy)
3999 {
4000         struct xdr_stream xdr;
4001         struct compound_hdr hdr;
4002         int status;
4003
4004         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4005         status = decode_compound_hdr(&xdr, &hdr);
4006         if (!status)
4007                 status = decode_renew(&xdr);
4008         return status;
4009 }
4010
4011 /*
4012  * a SETCLIENTID request
4013  */
4014 static int nfs4_xdr_dec_setclientid(struct rpc_rqst *req, uint32_t *p,
4015                 struct nfs4_client *clp)
4016 {
4017         struct xdr_stream xdr;
4018         struct compound_hdr hdr;
4019         int status;
4020
4021         xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4022         status = decode_compound_hdr(&xdr, &hdr);
4023         if (!status)
4024                 status = decode_setclientid(&xdr, clp);
4025         if (!status)
4026                 status = -nfs_stat_to_errno(hdr.status);
4027         return status;
4028 }
4029
4030 /*
4031  * a SETCLIENTID_CONFIRM request
4032  */
4033 static int nfs4_xdr_dec_setclientid_confirm(struct rpc_rqst *req, uint32_t *p, struct nfs_fsinfo *fsinfo)
4034 {
4035         struct xdr_stream xdr;
4036         struct compound_hdr hdr;
4037         int status;
4038
4039         xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4040         status = decode_compound_hdr(&xdr, &hdr);
4041         if (!status)
4042                 status = decode_setclientid_confirm(&xdr);
4043         if (!status)
4044                 status = decode_putrootfh(&xdr);
4045         if (!status)
4046                 status = decode_fsinfo(&xdr, fsinfo);
4047         if (!status)
4048                 status = -nfs_stat_to_errno(hdr.status);
4049         return status;
4050 }
4051
4052 /*
4053  * DELEGRETURN request
4054  */
4055 static int nfs4_xdr_dec_delegreturn(struct rpc_rqst *rqstp, uint32_t *p, void *dummy)
4056 {
4057         struct xdr_stream xdr;
4058         struct compound_hdr hdr;
4059         int status;
4060
4061         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4062         status = decode_compound_hdr(&xdr, &hdr);
4063         if (status == 0) {
4064                 status = decode_putfh(&xdr);
4065                 if (status == 0)
4066                         status = decode_delegreturn(&xdr);
4067         }
4068         return status;
4069 }
4070
4071 uint32_t *nfs4_decode_dirent(uint32_t *p, struct nfs_entry *entry, int plus)
4072 {
4073         uint32_t bitmap[2] = {0};
4074         uint32_t len;
4075
4076         if (!*p++) {
4077                 if (!*p)
4078                         return ERR_PTR(-EAGAIN);
4079                 entry->eof = 1;
4080                 return ERR_PTR(-EBADCOOKIE);
4081         }
4082
4083         entry->prev_cookie = entry->cookie;
4084         p = xdr_decode_hyper(p, &entry->cookie);
4085         entry->len = ntohl(*p++);
4086         entry->name = (const char *) p;
4087         p += XDR_QUADLEN(entry->len);
4088
4089         /*
4090          * In case the server doesn't return an inode number,
4091          * we fake one here.  (We don't use inode number 0,
4092          * since glibc seems to choke on it...)
4093          */
4094         entry->ino = 1;
4095
4096         len = ntohl(*p++);              /* bitmap length */
4097         if (len-- > 0) {
4098                 bitmap[0] = ntohl(*p++);
4099                 if (len-- > 0) {
4100                         bitmap[1] = ntohl(*p++);
4101                         p += len;
4102                 }
4103         }
4104         len = XDR_QUADLEN(ntohl(*p++)); /* attribute buffer length */
4105         if (len > 0) {
4106                 if (bitmap[0] & FATTR4_WORD0_RDATTR_ERROR) {
4107                         bitmap[0] &= ~FATTR4_WORD0_RDATTR_ERROR;
4108                         /* Ignore the return value of rdattr_error for now */
4109                         p++;
4110                         len--;
4111                 }
4112                 if (bitmap[0] == 0 && bitmap[1] == FATTR4_WORD1_MOUNTED_ON_FILEID)
4113                         xdr_decode_hyper(p, &entry->ino);
4114                 else if (bitmap[0] == FATTR4_WORD0_FILEID)
4115                         xdr_decode_hyper(p, &entry->ino);
4116                 p += len;
4117         }
4118
4119         entry->eof = !p[0] && p[1];
4120         return p;
4121 }
4122
4123 /*
4124  * We need to translate between nfs status return values and
4125  * the local errno values which may not be the same.
4126  */
4127 static struct {
4128         int stat;
4129         int errno;
4130 } nfs_errtbl[] = {
4131         { NFS4_OK,              0               },
4132         { NFS4ERR_PERM,         EPERM           },
4133         { NFS4ERR_NOENT,        ENOENT          },
4134         { NFS4ERR_IO,           errno_NFSERR_IO },
4135         { NFS4ERR_NXIO,         ENXIO           },
4136         { NFS4ERR_ACCESS,       EACCES          },
4137         { NFS4ERR_EXIST,        EEXIST          },
4138         { NFS4ERR_XDEV,         EXDEV           },
4139         { NFS4ERR_NOTDIR,       ENOTDIR         },
4140         { NFS4ERR_ISDIR,        EISDIR          },
4141         { NFS4ERR_INVAL,        EINVAL          },
4142         { NFS4ERR_FBIG,         EFBIG           },
4143         { NFS4ERR_NOSPC,        ENOSPC          },
4144         { NFS4ERR_ROFS,         EROFS           },
4145         { NFS4ERR_MLINK,        EMLINK          },
4146         { NFS4ERR_NAMETOOLONG,  ENAMETOOLONG    },
4147         { NFS4ERR_NOTEMPTY,     ENOTEMPTY       },
4148         { NFS4ERR_DQUOT,        EDQUOT          },
4149         { NFS4ERR_STALE,        ESTALE          },
4150         { NFS4ERR_BADHANDLE,    EBADHANDLE      },
4151         { NFS4ERR_BADOWNER,     EINVAL          },
4152         { NFS4ERR_BADNAME,      EINVAL          },
4153         { NFS4ERR_BAD_COOKIE,   EBADCOOKIE      },
4154         { NFS4ERR_NOTSUPP,      ENOTSUPP        },
4155         { NFS4ERR_TOOSMALL,     ETOOSMALL       },
4156         { NFS4ERR_SERVERFAULT,  ESERVERFAULT    },
4157         { NFS4ERR_BADTYPE,      EBADTYPE        },
4158         { NFS4ERR_LOCKED,       EAGAIN          },
4159         { NFS4ERR_RESOURCE,     EREMOTEIO       },
4160         { NFS4ERR_SYMLINK,      ELOOP           },
4161         { NFS4ERR_OP_ILLEGAL,   EOPNOTSUPP      },
4162         { NFS4ERR_DEADLOCK,     EDEADLK         },
4163         { NFS4ERR_WRONGSEC,     EPERM           }, /* FIXME: this needs
4164                                                     * to be handled by a
4165                                                     * middle-layer.
4166                                                     */
4167         { -1,                   EIO             }
4168 };
4169
4170 /*
4171  * Convert an NFS error code to a local one.
4172  * This one is used jointly by NFSv2 and NFSv3.
4173  */
4174 static int
4175 nfs_stat_to_errno(int stat)
4176 {
4177         int i;
4178         for (i = 0; nfs_errtbl[i].stat != -1; i++) {
4179                 if (nfs_errtbl[i].stat == stat)
4180                         return nfs_errtbl[i].errno;
4181         }
4182         if (stat <= 10000 || stat > 10100) {
4183                 /* The server is looney tunes. */
4184                 return ESERVERFAULT;
4185         }
4186         /* If we cannot translate the error, the recovery routines should
4187          * handle it.
4188          * Note: remaining NFSv4 error codes have values > 10000, so should
4189          * not conflict with native Linux error codes.
4190          */
4191         return stat;
4192 }
4193
4194 #ifndef MAX
4195 # define MAX(a, b)      (((a) > (b))? (a) : (b))
4196 #endif
4197
4198 #define PROC(proc, argtype, restype)                            \
4199 [NFSPROC4_CLNT_##proc] = {                                      \
4200         .p_proc   = NFSPROC4_COMPOUND,                          \
4201         .p_encode = (kxdrproc_t) nfs4_xdr_##argtype,            \
4202         .p_decode = (kxdrproc_t) nfs4_xdr_##restype,            \
4203         .p_bufsiz = MAX(NFS4_##argtype##_sz,NFS4_##restype##_sz) << 2,  \
4204     }
4205
4206 struct rpc_procinfo     nfs4_procedures[] = {
4207   PROC(READ,            enc_read,       dec_read),
4208   PROC(WRITE,           enc_write,      dec_write),
4209   PROC(COMMIT,          enc_commit,     dec_commit),
4210   PROC(OPEN,            enc_open,       dec_open),
4211   PROC(OPEN_CONFIRM,    enc_open_confirm,       dec_open_confirm),
4212   PROC(OPEN_NOATTR,     enc_open_noattr,        dec_open_noattr),
4213   PROC(OPEN_DOWNGRADE,  enc_open_downgrade,     dec_open_downgrade),
4214   PROC(CLOSE,           enc_close,      dec_close),
4215   PROC(SETATTR,         enc_setattr,    dec_setattr),
4216   PROC(FSINFO,          enc_fsinfo,     dec_fsinfo),
4217   PROC(RENEW,           enc_renew,      dec_renew),
4218   PROC(SETCLIENTID,     enc_setclientid,        dec_setclientid),
4219   PROC(SETCLIENTID_CONFIRM,     enc_setclientid_confirm,        dec_setclientid_confirm),
4220   PROC(LOCK,            enc_lock,       dec_lock),
4221   PROC(LOCKT,           enc_lockt,      dec_lockt),
4222   PROC(LOCKU,           enc_locku,      dec_locku),
4223   PROC(ACCESS,          enc_access,     dec_access),
4224   PROC(GETATTR,         enc_getattr,    dec_getattr),
4225   PROC(LOOKUP,          enc_lookup,     dec_lookup),
4226   PROC(LOOKUP_ROOT,     enc_lookup_root,        dec_lookup_root),
4227   PROC(REMOVE,          enc_remove,     dec_remove),
4228   PROC(RENAME,          enc_rename,     dec_rename),
4229   PROC(LINK,            enc_link,       dec_link),
4230   PROC(SYMLINK,         enc_symlink,    dec_symlink),
4231   PROC(CREATE,          enc_create,     dec_create),
4232   PROC(PATHCONF,        enc_pathconf,   dec_pathconf),
4233   PROC(STATFS,          enc_statfs,     dec_statfs),
4234   PROC(READLINK,        enc_readlink,   dec_readlink),
4235   PROC(READDIR,         enc_readdir,    dec_readdir),
4236   PROC(SERVER_CAPS,     enc_server_caps, dec_server_caps),
4237   PROC(DELEGRETURN,     enc_delegreturn, dec_delegreturn),
4238   PROC(GETACL,          enc_getacl,     dec_getacl),
4239   PROC(SETACL,          enc_setacl,     dec_setacl),
4240 };
4241
4242 struct rpc_version              nfs_version4 = {
4243         .number                 = 4,
4244         .nrprocs                = sizeof(nfs4_procedures)/sizeof(nfs4_procedures[0]),
4245         .procs                  = nfs4_procedures
4246 };
4247
4248 /*
4249  * Local variables:
4250  *  c-basic-offset: 8
4251  * End:
4252  */