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