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