patch-2_6_7-vs1_9_1_12
[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         unsigned int count = readlink->count - 5;
951         unsigned int replen;
952         uint32_t *p;
953
954         RESERVE_SPACE(4);
955         WRITE32(OP_READLINK);
956
957         /* set up reply iovec
958          *    toplevel_status + taglen + rescount + OP_PUTFH + status
959          *      + OP_READLINK + status  = 7
960          */
961         replen = (RPC_REPHDRSIZE + auth->au_rslack + 7) << 2;
962         xdr_inline_pages(&req->rq_rcv_buf, replen, readlink->pages, 0, count);
963         
964         return 0;
965 }
966
967 static int encode_remove(struct xdr_stream *xdr, const struct qstr *name)
968 {
969         uint32_t *p;
970
971         RESERVE_SPACE(8 + name->len);
972         WRITE32(OP_REMOVE);
973         WRITE32(name->len);
974         WRITEMEM(name->name, name->len);
975
976         return 0;
977 }
978
979 static int encode_rename(struct xdr_stream *xdr, const struct qstr *oldname, const struct qstr *newname)
980 {
981         uint32_t *p;
982
983         RESERVE_SPACE(8 + oldname->len);
984         WRITE32(OP_RENAME);
985         WRITE32(oldname->len);
986         WRITEMEM(oldname->name, oldname->len);
987         
988         RESERVE_SPACE(4 + newname->len);
989         WRITE32(newname->len);
990         WRITEMEM(newname->name, newname->len);
991
992         return 0;
993 }
994
995 static int encode_renew(struct xdr_stream *xdr, const struct nfs4_client *client_stateid)
996 {
997         uint32_t *p;
998
999         RESERVE_SPACE(12);
1000         WRITE32(OP_RENEW);
1001         WRITE64(client_stateid->cl_clientid);
1002
1003         return 0;
1004 }
1005
1006 static int
1007 encode_savefh(struct xdr_stream *xdr)
1008 {
1009         uint32_t *p;
1010
1011         RESERVE_SPACE(4);
1012         WRITE32(OP_SAVEFH);
1013
1014         return 0;
1015 }
1016
1017 static int encode_setattr(struct xdr_stream *xdr, const struct nfs_setattrargs *arg, const struct nfs_server *server)
1018 {
1019         int status;
1020         uint32_t *p;
1021         
1022         RESERVE_SPACE(4+sizeof(arg->stateid.data));
1023         WRITE32(OP_SETATTR);
1024         WRITEMEM(arg->stateid.data, sizeof(arg->stateid.data));
1025
1026         if ((status = encode_attrs(xdr, arg->iap, server)))
1027                 return status;
1028
1029         return 0;
1030 }
1031
1032 static int encode_setclientid(struct xdr_stream *xdr, const struct nfs4_setclientid *setclientid)
1033 {
1034         uint32_t total_len;
1035         uint32_t len1, len2, len3;
1036         uint32_t *p;
1037
1038         len1 = strlen(setclientid->sc_name);
1039         len2 = strlen(setclientid->sc_netid);
1040         len3 = strlen(setclientid->sc_uaddr);
1041         total_len = XDR_QUADLEN(len1) + XDR_QUADLEN(len2) + XDR_QUADLEN(len3);
1042         total_len = (total_len << 2) + 24 + sizeof(setclientid->sc_verifier.data);
1043
1044         RESERVE_SPACE(total_len);
1045         WRITE32(OP_SETCLIENTID);
1046         WRITEMEM(setclientid->sc_verifier.data, sizeof(setclientid->sc_verifier.data));
1047         WRITE32(len1);
1048         WRITEMEM(setclientid->sc_name, len1);
1049         WRITE32(setclientid->sc_prog);
1050         WRITE32(len2);
1051         WRITEMEM(setclientid->sc_netid, len2);
1052         WRITE32(len3);
1053         WRITEMEM(setclientid->sc_uaddr, len3);
1054         WRITE32(setclientid->sc_cb_ident);
1055
1056         return 0;
1057 }
1058
1059 static int encode_setclientid_confirm(struct xdr_stream *xdr, const struct nfs4_client *client_state)
1060 {
1061         uint32_t *p;
1062
1063         RESERVE_SPACE(12 + sizeof(client_state->cl_confirm.data));
1064         WRITE32(OP_SETCLIENTID_CONFIRM);
1065         WRITE64(client_state->cl_clientid);
1066         WRITEMEM(client_state->cl_confirm.data, sizeof(client_state->cl_confirm.data));
1067
1068         return 0;
1069 }
1070
1071 static int encode_write(struct xdr_stream *xdr, const struct nfs_writeargs *args)
1072 {
1073         uint32_t *p;
1074
1075         RESERVE_SPACE(4);
1076         WRITE32(OP_WRITE);
1077
1078         encode_stateid(xdr, args->state, args->lockowner);
1079
1080         RESERVE_SPACE(16);
1081         WRITE64(args->offset);
1082         WRITE32(args->stable);
1083         WRITE32(args->count);
1084
1085         xdr_write_pages(xdr, args->pages, args->pgbase, args->count);
1086
1087         return 0;
1088 }
1089 /*
1090  * END OF "GENERIC" ENCODE ROUTINES.
1091  */
1092
1093 /*
1094  * Encode an ACCESS request
1095  */
1096 static int nfs4_xdr_enc_access(struct rpc_rqst *req, uint32_t *p, const struct nfs4_accessargs *args)
1097 {
1098         struct xdr_stream xdr;
1099         struct compound_hdr hdr = {
1100                 .nops = 2,
1101         };
1102         int status;
1103
1104         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1105         encode_compound_hdr(&xdr, &hdr);
1106         if ((status = encode_putfh(&xdr, args->fh)) == 0)
1107                 status = encode_access(&xdr, args->access);
1108         return status;
1109 }
1110
1111 /*
1112  * Encode LOOKUP request
1113  */
1114 static int nfs4_xdr_enc_lookup(struct rpc_rqst *req, uint32_t *p, const struct nfs4_lookup_arg *args)
1115 {
1116         struct xdr_stream xdr;
1117         struct compound_hdr hdr = {
1118                 .nops = 4,
1119         };
1120         int status;
1121
1122         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1123         encode_compound_hdr(&xdr, &hdr);
1124         if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1125                 goto out;
1126         if ((status = encode_lookup(&xdr, args->name)) != 0)
1127                 goto out;
1128         if ((status = encode_getfh(&xdr)) != 0)
1129                 goto out;
1130         status = encode_getfattr(&xdr, args->bitmask);
1131 out:
1132         return status;
1133 }
1134
1135 /*
1136  * Encode LOOKUP_ROOT request
1137  */
1138 static int nfs4_xdr_enc_lookup_root(struct rpc_rqst *req, uint32_t *p, const struct nfs4_lookup_root_arg *args)
1139 {
1140         struct xdr_stream xdr;
1141         struct compound_hdr hdr = {
1142                 .nops = 3,
1143         };
1144         int status;
1145
1146         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1147         encode_compound_hdr(&xdr, &hdr);
1148         if ((status = encode_putrootfh(&xdr)) != 0)
1149                 goto out;
1150         if ((status = encode_getfh(&xdr)) == 0)
1151                 status = encode_getfattr(&xdr, args->bitmask);
1152 out:
1153         return status;
1154 }
1155
1156 /*
1157  * Encode REMOVE request
1158  */
1159 static int nfs4_xdr_enc_remove(struct rpc_rqst *req, uint32_t *p, const struct nfs4_remove_arg *args)
1160 {
1161         struct xdr_stream xdr;
1162         struct compound_hdr hdr = {
1163                 .nops = 2,
1164         };
1165         int status;
1166
1167         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1168         encode_compound_hdr(&xdr, &hdr);
1169         if ((status = encode_putfh(&xdr, args->fh)) == 0)
1170                 status = encode_remove(&xdr, args->name);
1171         return status;
1172 }
1173
1174 /*
1175  * Encode RENAME request
1176  */
1177 static int nfs4_xdr_enc_rename(struct rpc_rqst *req, uint32_t *p, const struct nfs4_rename_arg *args)
1178 {
1179         struct xdr_stream xdr;
1180         struct compound_hdr hdr = {
1181                 .nops = 4,
1182         };
1183         int status;
1184
1185         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1186         encode_compound_hdr(&xdr, &hdr);
1187         if ((status = encode_putfh(&xdr, args->old_dir)) != 0)
1188                 goto out;
1189         if ((status = encode_savefh(&xdr)) != 0)
1190                 goto out;
1191         if ((status = encode_putfh(&xdr, args->new_dir)) != 0)
1192                 goto out;
1193         status = encode_rename(&xdr, args->old_name, args->new_name);
1194 out:
1195         return status;
1196 }
1197
1198 /*
1199  * Encode LINK request
1200  */
1201 static int nfs4_xdr_enc_link(struct rpc_rqst *req, uint32_t *p, const struct nfs4_link_arg *args)
1202 {
1203         struct xdr_stream xdr;
1204         struct compound_hdr hdr = {
1205                 .nops = 4,
1206         };
1207         int status;
1208
1209         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1210         encode_compound_hdr(&xdr, &hdr);
1211         if ((status = encode_putfh(&xdr, args->fh)) != 0)
1212                 goto out;
1213         if ((status = encode_savefh(&xdr)) != 0)
1214                 goto out;
1215         if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1216                 goto out;
1217         status = encode_link(&xdr, args->name);
1218 out:
1219         return status;
1220 }
1221
1222 /*
1223  * Encode CREATE request
1224  */
1225 static int nfs4_xdr_enc_create(struct rpc_rqst *req, uint32_t *p, const struct nfs4_create_arg *args)
1226 {
1227         struct xdr_stream xdr;
1228         struct compound_hdr hdr = {
1229                 .nops = 4,
1230         };
1231         int status;
1232
1233         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1234         encode_compound_hdr(&xdr, &hdr);
1235         if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1236                 goto out;
1237         if ((status = encode_create(&xdr, args)) != 0)
1238                 goto out;
1239         if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
1240                 goto out;
1241         status = encode_getfh(&xdr);
1242 out:
1243         return status;
1244 }
1245
1246 /*
1247  * Encode GETATTR request
1248  */
1249 static int nfs4_xdr_enc_getattr(struct rpc_rqst *req, uint32_t *p, const struct nfs4_getattr_arg *args)
1250 {
1251         struct xdr_stream xdr;
1252         struct compound_hdr hdr = {
1253                 .nops = 2,
1254         };
1255         int status;
1256
1257         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1258         encode_compound_hdr(&xdr, &hdr);
1259         if ((status = encode_putfh(&xdr, args->fh)) == 0)
1260                 status = encode_getfattr(&xdr, args->bitmask);
1261         return status;
1262 }
1263
1264 /*
1265  * Encode a CLOSE request
1266  */
1267 static int nfs4_xdr_enc_close(struct rpc_rqst *req, uint32_t *p, struct nfs_closeargs *args)
1268 {
1269         struct xdr_stream xdr;
1270         struct compound_hdr hdr = {
1271                 .nops   = 2,
1272         };
1273         int status;
1274
1275         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1276         encode_compound_hdr(&xdr, &hdr);
1277         status = encode_putfh(&xdr, args->fh);
1278         if(status)
1279                 goto out;
1280         status = encode_close(&xdr, args);
1281 out:
1282         return status;
1283 }
1284
1285 /*
1286  * Encode an OPEN request
1287  */
1288 static int nfs4_xdr_enc_open(struct rpc_rqst *req, uint32_t *p, struct nfs_openargs *args)
1289 {
1290         struct xdr_stream xdr;
1291         struct compound_hdr hdr = {
1292                 .nops = 4,
1293         };
1294         int status;
1295
1296         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1297         encode_compound_hdr(&xdr, &hdr);
1298         status = encode_putfh(&xdr, args->fh);
1299         if (status)
1300                 goto out;
1301         status = encode_open(&xdr, args);
1302         if (status)
1303                 goto out;
1304         status = encode_getfattr(&xdr, args->bitmask);
1305         if (status)
1306                 goto out;
1307         status = encode_getfh(&xdr);
1308 out:
1309         return status;
1310 }
1311
1312 /*
1313  * Encode an OPEN_CONFIRM request
1314  */
1315 static int nfs4_xdr_enc_open_confirm(struct rpc_rqst *req, uint32_t *p, struct nfs_open_confirmargs *args)
1316 {
1317         struct xdr_stream xdr;
1318         struct compound_hdr hdr = {
1319                 .nops   = 2,
1320         };
1321         int status;
1322
1323         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1324         encode_compound_hdr(&xdr, &hdr);
1325         status = encode_putfh(&xdr, args->fh);
1326         if(status)
1327                 goto out;
1328         status = encode_open_confirm(&xdr, args);
1329 out:
1330         return status;
1331 }
1332
1333 /*
1334  * Encode an OPEN request
1335  */
1336 static int nfs4_xdr_enc_open_reclaim(struct rpc_rqst *req, uint32_t *p, struct nfs_open_reclaimargs *args)
1337 {
1338         struct xdr_stream xdr;
1339         struct compound_hdr hdr = {
1340                 .nops   = 3,
1341         };
1342         int status;
1343
1344         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1345         encode_compound_hdr(&xdr, &hdr);
1346         status = encode_putfh(&xdr, args->fh);
1347         if (status)
1348                 goto out;
1349         status = encode_open_reclaim(&xdr, args);
1350         if (status)
1351                 goto out;
1352         status = encode_getfattr(&xdr, args->bitmask);
1353 out:
1354         return status;
1355 }
1356
1357 /*
1358  * Encode an OPEN_DOWNGRADE request
1359  */
1360 static int nfs4_xdr_enc_open_downgrade(struct rpc_rqst *req, uint32_t *p, struct nfs_closeargs *args)
1361 {
1362         struct xdr_stream xdr;
1363         struct compound_hdr hdr = {
1364                 .nops   = 2,
1365         };
1366         int status;
1367
1368         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1369         encode_compound_hdr(&xdr, &hdr);
1370         status = encode_putfh(&xdr, args->fh);
1371         if (status)
1372                 goto out;
1373         status = encode_open_downgrade(&xdr, args);
1374 out:
1375         return status;
1376 }
1377
1378 /*
1379  * Encode a LOCK request
1380  */
1381 static int nfs4_xdr_enc_lock(struct rpc_rqst *req, uint32_t *p, struct nfs_lockargs *args)
1382 {
1383         struct xdr_stream xdr;
1384         struct compound_hdr hdr = {
1385                 .nops   = 2,
1386         };
1387         int status;
1388
1389         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1390         encode_compound_hdr(&xdr, &hdr);
1391         status = encode_putfh(&xdr, args->fh);
1392         if(status)
1393                 goto out;
1394         status = encode_lock(&xdr, args);
1395 out:
1396         return status;
1397 }
1398
1399 /*
1400  * Encode a LOCKT request
1401  */
1402 static int nfs4_xdr_enc_lockt(struct rpc_rqst *req, uint32_t *p, struct nfs_lockargs *args)
1403 {
1404         struct xdr_stream xdr;
1405         struct compound_hdr hdr = {
1406                 .nops   = 2,
1407         };
1408         int status;
1409
1410         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1411         encode_compound_hdr(&xdr, &hdr);
1412         status = encode_putfh(&xdr, args->fh);
1413         if(status)
1414                 goto out;
1415         status = encode_lockt(&xdr, args);
1416 out:
1417         return status;
1418 }
1419
1420 /*
1421  * Encode a LOCKU request
1422  */
1423 static int nfs4_xdr_enc_locku(struct rpc_rqst *req, uint32_t *p, struct nfs_lockargs *args)
1424 {
1425         struct xdr_stream xdr;
1426         struct compound_hdr hdr = {
1427                 .nops   = 2,
1428         };
1429         int status;
1430
1431         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1432         encode_compound_hdr(&xdr, &hdr);
1433         status = encode_putfh(&xdr, args->fh);
1434         if(status)
1435                 goto out;
1436         status = encode_locku(&xdr, args);
1437 out:
1438         return status;
1439 }
1440
1441 /*
1442  * Encode a READLINK request
1443  */
1444 static int nfs4_xdr_enc_readlink(struct rpc_rqst *req, uint32_t *p, const struct nfs4_readlink *args)
1445 {
1446         struct xdr_stream xdr;
1447         struct compound_hdr hdr = {
1448                 .nops = 2,
1449         };
1450         int status;
1451
1452         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1453         encode_compound_hdr(&xdr, &hdr);
1454         status = encode_putfh(&xdr, args->fh);
1455         if(status)
1456                 goto out;
1457         status = encode_readlink(&xdr, args, req);
1458 out:
1459         return status;
1460 }
1461
1462 /*
1463  * Encode a READDIR request
1464  */
1465 static int nfs4_xdr_enc_readdir(struct rpc_rqst *req, uint32_t *p, const struct nfs4_readdir_arg *args)
1466 {
1467         struct xdr_stream xdr;
1468         struct compound_hdr hdr = {
1469                 .nops = 2,
1470         };
1471         int status;
1472
1473         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1474         encode_compound_hdr(&xdr, &hdr);
1475         status = encode_putfh(&xdr, args->fh);
1476         if(status)
1477                 goto out;
1478         status = encode_readdir(&xdr, args, req);
1479 out:
1480         return status;
1481 }
1482
1483 /*
1484  * Encode a READ request
1485  */
1486 static int nfs4_xdr_enc_read(struct rpc_rqst *req, uint32_t *p, struct nfs_readargs *args)
1487 {
1488         struct rpc_auth *auth = req->rq_task->tk_auth;
1489         struct xdr_stream xdr;
1490         struct compound_hdr hdr = {
1491                 .nops = 2,
1492         };
1493         int replen, status;
1494
1495         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1496         encode_compound_hdr(&xdr, &hdr);
1497         status = encode_putfh(&xdr, args->fh);
1498         if (status)
1499                 goto out;
1500         status = encode_read(&xdr, args);
1501         if (status)
1502                 goto out;
1503
1504         /* set up reply iovec
1505          *    toplevel status + taglen=0 + rescount + OP_PUTFH + status
1506          *       + OP_READ + status + eof + datalen = 9
1507          */
1508         replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_read_sz) << 2;
1509         xdr_inline_pages(&req->rq_rcv_buf, replen,
1510                          args->pages, args->pgbase, args->count);
1511 out:
1512         return status;
1513 }
1514
1515 /*
1516  * Encode an SETATTR request
1517  */
1518 static int nfs4_xdr_enc_setattr(struct rpc_rqst *req, uint32_t *p, struct nfs_setattrargs *args)
1519
1520 {
1521         struct xdr_stream xdr;
1522         struct compound_hdr hdr = {
1523                 .nops   = 3,
1524         };
1525         int status;
1526
1527         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1528         encode_compound_hdr(&xdr, &hdr);
1529         status = encode_putfh(&xdr, args->fh);
1530         if(status)
1531                 goto out;
1532         status = encode_setattr(&xdr, args, args->server);
1533         if(status)
1534                 goto out;
1535         status = encode_getfattr(&xdr, args->bitmask);
1536 out:
1537         return status;
1538 }
1539
1540 /*
1541  * Encode a WRITE request
1542  */
1543 static int nfs4_xdr_enc_write(struct rpc_rqst *req, uint32_t *p, struct nfs_writeargs *args)
1544 {
1545         struct xdr_stream xdr;
1546         struct compound_hdr hdr = {
1547                 .nops = 2,
1548         };
1549         int status;
1550
1551         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1552         encode_compound_hdr(&xdr, &hdr);
1553         status = encode_putfh(&xdr, args->fh);
1554         if (status)
1555                 goto out;
1556         status = encode_write(&xdr, args);
1557 out:
1558         return status;
1559 }
1560
1561 /*
1562  *  a COMMIT request
1563  */
1564 static int nfs4_xdr_enc_commit(struct rpc_rqst *req, uint32_t *p, struct nfs_writeargs *args)
1565 {
1566         struct xdr_stream xdr;
1567         struct compound_hdr hdr = {
1568                 .nops = 2,
1569         };
1570         int status;
1571
1572         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1573         encode_compound_hdr(&xdr, &hdr);
1574         status = encode_putfh(&xdr, args->fh);
1575         if (status)
1576                 goto out;
1577         status = encode_commit(&xdr, args);
1578 out:
1579         return status;
1580 }
1581
1582 /*
1583  * FSINFO request
1584  */
1585 static int nfs4_xdr_enc_fsinfo(struct rpc_rqst *req, uint32_t *p, struct nfs4_fsinfo_arg *args)
1586 {
1587         struct xdr_stream xdr;
1588         struct compound_hdr hdr = {
1589                 .nops   = 2,
1590         };
1591         int status;
1592
1593         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1594         encode_compound_hdr(&xdr, &hdr);
1595         status = encode_putfh(&xdr, args->fh);
1596         if (!status)
1597                 status = encode_fsinfo(&xdr, args->bitmask);
1598         return status;
1599 }
1600
1601 /*
1602  * a PATHCONF request
1603  */
1604 static int nfs4_xdr_enc_pathconf(struct rpc_rqst *req, uint32_t *p, const struct nfs4_pathconf_arg *args)
1605 {
1606         extern u32 nfs4_pathconf_bitmap[2];
1607         struct xdr_stream xdr;
1608         struct compound_hdr hdr = {
1609                 .nops = 2,
1610         };
1611         int status;
1612
1613         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1614         encode_compound_hdr(&xdr, &hdr);
1615         status = encode_putfh(&xdr, args->fh);
1616         if (!status)
1617                 status = encode_getattr_one(&xdr,
1618                                 args->bitmask[0] & nfs4_pathconf_bitmap[0]);
1619         return status;
1620 }
1621
1622 /*
1623  * a STATFS request
1624  */
1625 static int nfs4_xdr_enc_statfs(struct rpc_rqst *req, uint32_t *p, const struct nfs4_statfs_arg *args)
1626 {
1627         extern u32 nfs4_statfs_bitmap[];
1628         struct xdr_stream xdr;
1629         struct compound_hdr hdr = {
1630                 .nops = 2,
1631         };
1632         int status;
1633
1634         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1635         encode_compound_hdr(&xdr, &hdr);
1636         status = encode_putfh(&xdr, args->fh);
1637         if (status == 0)
1638                 status = encode_getattr_two(&xdr,
1639                                 args->bitmask[0] & nfs4_statfs_bitmap[0],
1640                                 args->bitmask[1] & nfs4_statfs_bitmap[1]);
1641         return status;
1642 }
1643
1644 /*
1645  * GETATTR_BITMAP request
1646  */
1647 static int nfs4_xdr_enc_server_caps(struct rpc_rqst *req, uint32_t *p, const struct nfs_fh *fhandle)
1648 {
1649         struct xdr_stream xdr;
1650         struct compound_hdr hdr = {
1651                 .nops = 2,
1652         };
1653         int status;
1654
1655         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1656         encode_compound_hdr(&xdr, &hdr);
1657         status = encode_putfh(&xdr, fhandle);
1658         if (status == 0)
1659                 status = encode_getattr_one(&xdr, FATTR4_WORD0_SUPPORTED_ATTRS|
1660                                 FATTR4_WORD0_LINK_SUPPORT|
1661                                 FATTR4_WORD0_SYMLINK_SUPPORT|
1662                                 FATTR4_WORD0_ACLSUPPORT);
1663         return status;
1664 }
1665
1666 /*
1667  * a RENEW request
1668  */
1669 static int nfs4_xdr_enc_renew(struct rpc_rqst *req, uint32_t *p, struct nfs4_client *clp)
1670 {
1671         struct xdr_stream xdr;
1672         struct compound_hdr hdr = {
1673                 .nops   = 1,
1674         };
1675
1676         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1677         encode_compound_hdr(&xdr, &hdr);
1678         return encode_renew(&xdr, clp);
1679 }
1680
1681 /*
1682  * a SETCLIENTID request
1683  */
1684 static int nfs4_xdr_enc_setclientid(struct rpc_rqst *req, uint32_t *p, struct nfs4_setclientid *sc)
1685 {
1686         struct xdr_stream xdr;
1687         struct compound_hdr hdr = {
1688                 .nops   = 1,
1689         };
1690
1691         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1692         encode_compound_hdr(&xdr, &hdr);
1693         return encode_setclientid(&xdr, sc);
1694 }
1695
1696 /*
1697  * a SETCLIENTID_CONFIRM request
1698  */
1699 static int nfs4_xdr_enc_setclientid_confirm(struct rpc_rqst *req, uint32_t *p, struct nfs4_client *clp)
1700 {
1701         struct xdr_stream xdr;
1702         struct compound_hdr hdr = {
1703                 .nops   = 3,
1704         };
1705         const u32 lease_bitmap[2] = { FATTR4_WORD0_LEASE_TIME, 0 };
1706         int status;
1707
1708         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1709         encode_compound_hdr(&xdr, &hdr);
1710         status = encode_setclientid_confirm(&xdr, clp);
1711         if (!status)
1712                 status = encode_putrootfh(&xdr);
1713         if (!status)
1714                 status = encode_fsinfo(&xdr, lease_bitmap);
1715         return status;
1716 }
1717
1718 /*
1719  * START OF "GENERIC" DECODE ROUTINES.
1720  *   These may look a little ugly since they are imported from a "generic"
1721  * set of XDR encode/decode routines which are intended to be shared by
1722  * all of our NFSv4 implementations (OpenBSD, MacOS X...).
1723  *
1724  * If the pain of reading these is too great, it should be a straightforward
1725  * task to translate them into Linux-specific versions which are more
1726  * consistent with the style used in NFSv2/v3...
1727  */
1728 #define READ32(x)         (x) = ntohl(*p++)
1729 #define READ64(x)         do {                  \
1730         (x) = (u64)ntohl(*p++) << 32;           \
1731         (x) |= ntohl(*p++);                     \
1732 } while (0)
1733 #define READTIME(x)       do {                  \
1734         p++;                                    \
1735         (x.tv_sec) = ntohl(*p++);               \
1736         (x.tv_nsec) = ntohl(*p++);              \
1737 } while (0)
1738 #define COPYMEM(x,nbytes) do {                  \
1739         memcpy((x), p, nbytes);                 \
1740         p += XDR_QUADLEN(nbytes);               \
1741 } while (0)
1742
1743 #define READ_BUF(nbytes)  do { \
1744         p = xdr_inline_decode(xdr, nbytes); \
1745         if (!p) { \
1746                 printk(KERN_WARNING "%s: reply buffer overflowed in line %d.", \
1747                                 __FUNCTION__, __LINE__); \
1748                 return -EIO; \
1749         } \
1750 } while (0)
1751
1752 static int decode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
1753 {
1754         uint32_t *p;
1755
1756         READ_BUF(8);
1757         READ32(hdr->status);
1758         READ32(hdr->taglen);
1759         
1760         READ_BUF(hdr->taglen + 4);
1761         hdr->tag = (char *)p;
1762         p += XDR_QUADLEN(hdr->taglen);
1763         READ32(hdr->nops);
1764         return 0;
1765 }
1766
1767 static int decode_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
1768 {
1769         uint32_t *p;
1770         uint32_t opnum;
1771         int32_t nfserr;
1772
1773         READ_BUF(8);
1774         READ32(opnum);
1775         if (opnum != expected) {
1776                 printk(KERN_NOTICE
1777                                 "nfs4_decode_op_hdr: Server returned operation"
1778                                 " %d but we issued a request for %d\n",
1779                                 opnum, expected);
1780                 return -EIO;
1781         }
1782         READ32(nfserr);
1783         if (nfserr != NFS_OK)
1784                 return -nfs_stat_to_errno(nfserr);
1785         return 0;
1786 }
1787
1788 static int decode_attr_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
1789 {
1790         uint32_t bmlen, *p;
1791
1792         READ_BUF(4);
1793         READ32(bmlen);
1794
1795         bitmap[0] = bitmap[1] = 0;
1796         READ_BUF((bmlen << 2));
1797         if (bmlen > 0) {
1798                 READ32(bitmap[0]);
1799                 if (bmlen > 1)
1800                         READ32(bitmap[1]);
1801         }
1802         return 0;
1803 }
1804
1805 static inline int decode_attr_length(struct xdr_stream *xdr, uint32_t *attrlen, uint32_t **savep)
1806 {
1807         uint32_t *p;
1808
1809         READ_BUF(4);
1810         READ32(*attrlen);
1811         *savep = xdr->p;
1812         return 0;
1813 }
1814
1815 static int decode_attr_supported(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *bitmask)
1816 {
1817         if (likely(bitmap[0] & FATTR4_WORD0_SUPPORTED_ATTRS)) {
1818                 decode_attr_bitmap(xdr, bitmask);
1819                 bitmap[0] &= ~FATTR4_WORD0_SUPPORTED_ATTRS;
1820         } else
1821                 bitmask[0] = bitmask[1] = 0;
1822         dprintk("%s: bitmask=0x%x%x\n", __FUNCTION__, bitmask[0], bitmask[1]);
1823         return 0;
1824 }
1825
1826 static int decode_attr_type(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *type)
1827 {
1828         uint32_t *p;
1829
1830         *type = 0;
1831         if (unlikely(bitmap[0] & (FATTR4_WORD0_TYPE - 1U)))
1832                 return -EIO;
1833         if (likely(bitmap[0] & FATTR4_WORD0_TYPE)) {
1834                 READ_BUF(4);
1835                 READ32(*type);
1836                 if (*type < NF4REG || *type > NF4NAMEDATTR) {
1837                         dprintk("%s: bad type %d\n", __FUNCTION__, *type);
1838                         return -EIO;
1839                 }
1840                 bitmap[0] &= ~FATTR4_WORD0_TYPE;
1841         }
1842         dprintk("%s: type=0%o\n", __FUNCTION__, nfs_type2fmt[*type].nfs2type);
1843         return 0;
1844 }
1845
1846 static int decode_attr_change(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *change)
1847 {
1848         uint32_t *p;
1849
1850         *change = 0;
1851         if (unlikely(bitmap[0] & (FATTR4_WORD0_CHANGE - 1U)))
1852                 return -EIO;
1853         if (likely(bitmap[0] & FATTR4_WORD0_CHANGE)) {
1854                 READ_BUF(8);
1855                 READ64(*change);
1856                 bitmap[0] &= ~FATTR4_WORD0_CHANGE;
1857         }
1858         dprintk("%s: change attribute=%Lu\n", __FUNCTION__,
1859                         (unsigned long long)*change);
1860         return 0;
1861 }
1862
1863 static int decode_attr_size(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *size)
1864 {
1865         uint32_t *p;
1866
1867         *size = 0;
1868         if (unlikely(bitmap[0] & (FATTR4_WORD0_SIZE - 1U)))
1869                 return -EIO;
1870         if (likely(bitmap[0] & FATTR4_WORD0_SIZE)) {
1871                 READ_BUF(8);
1872                 READ64(*size);
1873                 bitmap[0] &= ~FATTR4_WORD0_SIZE;
1874         }
1875         dprintk("%s: file size=%Lu\n", __FUNCTION__, (unsigned long long)*size);
1876         return 0;
1877 }
1878
1879 static int decode_attr_link_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
1880 {
1881         uint32_t *p;
1882
1883         *res = 0;
1884         if (unlikely(bitmap[0] & (FATTR4_WORD0_LINK_SUPPORT - 1U)))
1885                 return -EIO;
1886         if (likely(bitmap[0] & FATTR4_WORD0_LINK_SUPPORT)) {
1887                 READ_BUF(4);
1888                 READ32(*res);
1889                 bitmap[0] &= ~FATTR4_WORD0_LINK_SUPPORT;
1890         }
1891         dprintk("%s: link support=%s\n", __FUNCTION__, *res == 0 ? "false" : "true");
1892         return 0;
1893 }
1894
1895 static int decode_attr_symlink_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
1896 {
1897         uint32_t *p;
1898
1899         *res = 0;
1900         if (unlikely(bitmap[0] & (FATTR4_WORD0_SYMLINK_SUPPORT - 1U)))
1901                 return -EIO;
1902         if (likely(bitmap[0] & FATTR4_WORD0_SYMLINK_SUPPORT)) {
1903                 READ_BUF(4);
1904                 READ32(*res);
1905                 bitmap[0] &= ~FATTR4_WORD0_SYMLINK_SUPPORT;
1906         }
1907         dprintk("%s: symlink support=%s\n", __FUNCTION__, *res == 0 ? "false" : "true");
1908         return 0;
1909 }
1910
1911 static int decode_attr_fsid(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_fsid *fsid)
1912 {
1913         uint32_t *p;
1914
1915         fsid->major = 0;
1916         fsid->minor = 0;
1917         if (unlikely(bitmap[0] & (FATTR4_WORD0_FSID - 1U)))
1918                 return -EIO;
1919         if (likely(bitmap[0] & FATTR4_WORD0_FSID)) {
1920                 READ_BUF(16);
1921                 READ64(fsid->major);
1922                 READ64(fsid->minor);
1923                 bitmap[0] &= ~FATTR4_WORD0_FSID;
1924         }
1925         dprintk("%s: fsid=(0x%Lx/0x%Lx)\n", __FUNCTION__,
1926                         (unsigned long long)fsid->major,
1927                         (unsigned long long)fsid->minor);
1928         return 0;
1929 }
1930
1931 static int decode_attr_lease_time(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
1932 {
1933         uint32_t *p;
1934
1935         *res = 60;
1936         if (unlikely(bitmap[0] & (FATTR4_WORD0_LEASE_TIME - 1U)))
1937                 return -EIO;
1938         if (likely(bitmap[0] & FATTR4_WORD0_LEASE_TIME)) {
1939                 READ_BUF(4);
1940                 READ32(*res);
1941                 bitmap[0] &= ~FATTR4_WORD0_LEASE_TIME;
1942         }
1943         dprintk("%s: file size=%u\n", __FUNCTION__, (unsigned int)*res);
1944         return 0;
1945 }
1946
1947 static int decode_attr_aclsupport(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
1948 {
1949         uint32_t *p;
1950
1951         *res = ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL;
1952         if (unlikely(bitmap[0] & (FATTR4_WORD0_ACLSUPPORT - 1U)))
1953                 return -EIO;
1954         if (likely(bitmap[0] & FATTR4_WORD0_ACLSUPPORT)) {
1955                 READ_BUF(4);
1956                 READ32(*res);
1957                 bitmap[0] &= ~FATTR4_WORD0_ACLSUPPORT;
1958         }
1959         dprintk("%s: ACLs supported=%u\n", __FUNCTION__, (unsigned int)*res);
1960         return 0;
1961 }
1962
1963 static int decode_attr_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
1964 {
1965         uint32_t *p;
1966
1967         *fileid = 0;
1968         if (unlikely(bitmap[0] & (FATTR4_WORD0_FILEID - 1U)))
1969                 return -EIO;
1970         if (likely(bitmap[0] & FATTR4_WORD0_FILEID)) {
1971                 READ_BUF(8);
1972                 READ64(*fileid);
1973                 bitmap[0] &= ~FATTR4_WORD0_FILEID;
1974         }
1975         dprintk("%s: fileid=%Lu\n", __FUNCTION__, (unsigned long long)*fileid);
1976         return 0;
1977 }
1978
1979 static int decode_attr_files_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
1980 {
1981         uint32_t *p;
1982         int status = 0;
1983
1984         *res = 0;
1985         if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_AVAIL - 1U)))
1986                 return -EIO;
1987         if (likely(bitmap[0] & FATTR4_WORD0_FILES_AVAIL)) {
1988                 READ_BUF(8);
1989                 READ64(*res);
1990                 bitmap[0] &= ~FATTR4_WORD0_FILES_AVAIL;
1991         }
1992         dprintk("%s: files avail=%Lu\n", __FUNCTION__, (unsigned long long)*res);
1993         return status;
1994 }
1995
1996 static int decode_attr_files_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
1997 {
1998         uint32_t *p;
1999         int status = 0;
2000
2001         *res = 0;
2002         if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_FREE - 1U)))
2003                 return -EIO;
2004         if (likely(bitmap[0] & FATTR4_WORD0_FILES_FREE)) {
2005                 READ_BUF(8);
2006                 READ64(*res);
2007                 bitmap[0] &= ~FATTR4_WORD0_FILES_FREE;
2008         }
2009         dprintk("%s: files free=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2010         return status;
2011 }
2012
2013 static int decode_attr_files_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2014 {
2015         uint32_t *p;
2016         int status = 0;
2017
2018         *res = 0;
2019         if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_TOTAL - 1U)))
2020                 return -EIO;
2021         if (likely(bitmap[0] & FATTR4_WORD0_FILES_TOTAL)) {
2022                 READ_BUF(8);
2023                 READ64(*res);
2024                 bitmap[0] &= ~FATTR4_WORD0_FILES_TOTAL;
2025         }
2026         dprintk("%s: files total=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2027         return status;
2028 }
2029
2030 static int decode_attr_maxfilesize(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2031 {
2032         uint32_t *p;
2033         int status = 0;
2034
2035         *res = 0;
2036         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXFILESIZE - 1U)))
2037                 return -EIO;
2038         if (likely(bitmap[0] & FATTR4_WORD0_MAXFILESIZE)) {
2039                 READ_BUF(8);
2040                 READ64(*res);
2041                 bitmap[0] &= ~FATTR4_WORD0_MAXFILESIZE;
2042         }
2043         dprintk("%s: maxfilesize=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2044         return status;
2045 }
2046
2047 static int decode_attr_maxlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxlink)
2048 {
2049         uint32_t *p;
2050         int status = 0;
2051
2052         *maxlink = 1;
2053         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXLINK - 1U)))
2054                 return -EIO;
2055         if (likely(bitmap[0] & FATTR4_WORD0_MAXLINK)) {
2056                 READ_BUF(4);
2057                 READ32(*maxlink);
2058                 bitmap[0] &= ~FATTR4_WORD0_MAXLINK;
2059         }
2060         dprintk("%s: maxlink=%u\n", __FUNCTION__, *maxlink);
2061         return status;
2062 }
2063
2064 static int decode_attr_maxname(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxname)
2065 {
2066         uint32_t *p;
2067         int status = 0;
2068
2069         *maxname = 1024;
2070         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXNAME - 1U)))
2071                 return -EIO;
2072         if (likely(bitmap[0] & FATTR4_WORD0_MAXNAME)) {
2073                 READ_BUF(4);
2074                 READ32(*maxname);
2075                 bitmap[0] &= ~FATTR4_WORD0_MAXNAME;
2076         }
2077         dprintk("%s: maxname=%u\n", __FUNCTION__, *maxname);
2078         return status;
2079 }
2080
2081 static int decode_attr_maxread(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2082 {
2083         uint32_t *p;
2084         int status = 0;
2085
2086         *res = 1024;
2087         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXREAD - 1U)))
2088                 return -EIO;
2089         if (likely(bitmap[0] & FATTR4_WORD0_MAXREAD)) {
2090                 uint64_t maxread;
2091                 READ_BUF(8);
2092                 READ64(maxread);
2093                 if (maxread > 0x7FFFFFFF)
2094                         maxread = 0x7FFFFFFF;
2095                 *res = (uint32_t)maxread;
2096                 bitmap[0] &= ~FATTR4_WORD0_MAXREAD;
2097         }
2098         dprintk("%s: maxread=%lu\n", __FUNCTION__, (unsigned long)*res);
2099         return status;
2100 }
2101
2102 static int decode_attr_maxwrite(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2103 {
2104         uint32_t *p;
2105         int status = 0;
2106
2107         *res = 1024;
2108         if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXWRITE - 1U)))
2109                 return -EIO;
2110         if (likely(bitmap[0] & FATTR4_WORD0_MAXWRITE)) {
2111                 uint64_t maxwrite;
2112                 READ_BUF(8);
2113                 READ64(maxwrite);
2114                 if (maxwrite > 0x7FFFFFFF)
2115                         maxwrite = 0x7FFFFFFF;
2116                 *res = (uint32_t)maxwrite;
2117                 bitmap[0] &= ~FATTR4_WORD0_MAXWRITE;
2118         }
2119         dprintk("%s: maxwrite=%lu\n", __FUNCTION__, (unsigned long)*res);
2120         return status;
2121 }
2122
2123 static int decode_attr_mode(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *mode)
2124 {
2125         uint32_t *p;
2126
2127         *mode = 0;
2128         if (unlikely(bitmap[1] & (FATTR4_WORD1_MODE - 1U)))
2129                 return -EIO;
2130         if (likely(bitmap[1] & FATTR4_WORD1_MODE)) {
2131                 READ_BUF(4);
2132                 READ32(*mode);
2133                 *mode &= ~S_IFMT;
2134                 bitmap[1] &= ~FATTR4_WORD1_MODE;
2135         }
2136         dprintk("%s: file mode=0%o\n", __FUNCTION__, (unsigned int)*mode);
2137         return 0;
2138 }
2139
2140 static int decode_attr_nlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *nlink)
2141 {
2142         uint32_t *p;
2143
2144         *nlink = 1;
2145         if (unlikely(bitmap[1] & (FATTR4_WORD1_NUMLINKS - 1U)))
2146                 return -EIO;
2147         if (likely(bitmap[1] & FATTR4_WORD1_NUMLINKS)) {
2148                 READ_BUF(4);
2149                 READ32(*nlink);
2150                 bitmap[1] &= ~FATTR4_WORD1_NUMLINKS;
2151         }
2152         dprintk("%s: nlink=%u\n", __FUNCTION__, (unsigned int)*nlink);
2153         return 0;
2154 }
2155
2156 static int decode_attr_owner(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_client *clp, int32_t *uid)
2157 {
2158         uint32_t len, *p;
2159
2160         *uid = -2;
2161         if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER - 1U)))
2162                 return -EIO;
2163         if (likely(bitmap[1] & FATTR4_WORD1_OWNER)) {
2164                 READ_BUF(4);
2165                 READ32(len);
2166                 READ_BUF(len);
2167                 if (len < XDR_MAX_NETOBJ) {
2168                         if (nfs_map_name_to_uid(clp, (char *)p, len, uid) != 0)
2169                                 dprintk("%s: nfs_map_name_to_uid failed!\n",
2170                                                 __FUNCTION__);
2171                 } else
2172                         printk(KERN_WARNING "%s: name too long (%u)!\n",
2173                                         __FUNCTION__, len);
2174                 bitmap[1] &= ~FATTR4_WORD1_OWNER;
2175         }
2176         dprintk("%s: uid=%d\n", __FUNCTION__, (int)*uid);
2177         return 0;
2178 }
2179
2180 static int decode_attr_group(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_client *clp, int32_t *gid)
2181 {
2182         uint32_t len, *p;
2183
2184         *gid = -2;
2185         if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER_GROUP - 1U)))
2186                 return -EIO;
2187         if (likely(bitmap[1] & FATTR4_WORD1_OWNER_GROUP)) {
2188                 READ_BUF(4);
2189                 READ32(len);
2190                 READ_BUF(len);
2191                 if (len < XDR_MAX_NETOBJ) {
2192                         if (nfs_map_group_to_gid(clp, (char *)p, len, gid) != 0)
2193                                 dprintk("%s: nfs_map_group_to_gid failed!\n",
2194                                                 __FUNCTION__);
2195                 } else
2196                         printk(KERN_WARNING "%s: name too long (%u)!\n",
2197                                         __FUNCTION__, len);
2198                 bitmap[1] &= ~FATTR4_WORD1_OWNER_GROUP;
2199         }
2200         dprintk("%s: gid=%d\n", __FUNCTION__, (int)*gid);
2201         return 0;
2202 }
2203
2204 static int decode_attr_rdev(struct xdr_stream *xdr, uint32_t *bitmap, dev_t *rdev)
2205 {
2206         uint32_t major = 0, minor = 0, *p;
2207
2208         *rdev = MKDEV(0,0);
2209         if (unlikely(bitmap[1] & (FATTR4_WORD1_RAWDEV - 1U)))
2210                 return -EIO;
2211         if (likely(bitmap[1] & FATTR4_WORD1_RAWDEV)) {
2212                 dev_t tmp;
2213
2214                 READ_BUF(8);
2215                 READ32(major);
2216                 READ32(minor);
2217                 tmp = MKDEV(major, minor);
2218                 if (MAJOR(tmp) == major && MINOR(tmp) == minor)
2219                         *rdev = tmp;
2220                 bitmap[1] &= ~ FATTR4_WORD1_RAWDEV;
2221         }
2222         dprintk("%s: rdev=(0x%x:0x%x)\n", __FUNCTION__, major, minor);
2223         return 0;
2224 }
2225
2226 static int decode_attr_space_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2227 {
2228         uint32_t *p;
2229         int status = 0;
2230
2231         *res = 0;
2232         if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_AVAIL - 1U)))
2233                 return -EIO;
2234         if (likely(bitmap[1] & FATTR4_WORD1_SPACE_AVAIL)) {
2235                 READ_BUF(8);
2236                 READ64(*res);
2237                 bitmap[1] &= ~FATTR4_WORD1_SPACE_AVAIL;
2238         }
2239         dprintk("%s: space avail=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2240         return status;
2241 }
2242
2243 static int decode_attr_space_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2244 {
2245         uint32_t *p;
2246         int status = 0;
2247
2248         *res = 0;
2249         if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_FREE - 1U)))
2250                 return -EIO;
2251         if (likely(bitmap[1] & FATTR4_WORD1_SPACE_FREE)) {
2252                 READ_BUF(8);
2253                 READ64(*res);
2254                 bitmap[1] &= ~FATTR4_WORD1_SPACE_FREE;
2255         }
2256         dprintk("%s: space free=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2257         return status;
2258 }
2259
2260 static int decode_attr_space_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2261 {
2262         uint32_t *p;
2263         int status = 0;
2264
2265         *res = 0;
2266         if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_TOTAL - 1U)))
2267                 return -EIO;
2268         if (likely(bitmap[1] & FATTR4_WORD1_SPACE_TOTAL)) {
2269                 READ_BUF(8);
2270                 READ64(*res);
2271                 bitmap[1] &= ~FATTR4_WORD1_SPACE_TOTAL;
2272         }
2273         dprintk("%s: space total=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2274         return status;
2275 }
2276
2277 static int decode_attr_space_used(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *used)
2278 {
2279         uint32_t *p;
2280
2281         *used = 0;
2282         if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_USED - 1U)))
2283                 return -EIO;
2284         if (likely(bitmap[1] & FATTR4_WORD1_SPACE_USED)) {
2285                 READ_BUF(8);
2286                 READ64(*used);
2287                 bitmap[1] &= ~FATTR4_WORD1_SPACE_USED;
2288         }
2289         dprintk("%s: space used=%Lu\n", __FUNCTION__,
2290                         (unsigned long long)*used);
2291         return 0;
2292 }
2293
2294 static int decode_attr_time(struct xdr_stream *xdr, struct timespec *time)
2295 {
2296         uint32_t *p;
2297         uint64_t sec;
2298         uint32_t nsec;
2299
2300         READ_BUF(12);
2301         READ64(sec);
2302         READ32(nsec);
2303         time->tv_sec = (time_t)sec;
2304         time->tv_nsec = (long)nsec;
2305         return 0;
2306 }
2307
2308 static int decode_attr_time_access(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2309 {
2310         int status = 0;
2311
2312         time->tv_sec = 0;
2313         time->tv_nsec = 0;
2314         if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_ACCESS - 1U)))
2315                 return -EIO;
2316         if (likely(bitmap[1] & FATTR4_WORD1_TIME_ACCESS)) {
2317                 status = decode_attr_time(xdr, time);
2318                 bitmap[1] &= ~FATTR4_WORD1_TIME_ACCESS;
2319         }
2320         dprintk("%s: atime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2321         return status;
2322 }
2323
2324 static int decode_attr_time_metadata(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2325 {
2326         int status = 0;
2327
2328         time->tv_sec = 0;
2329         time->tv_nsec = 0;
2330         if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_METADATA - 1U)))
2331                 return -EIO;
2332         if (likely(bitmap[1] & FATTR4_WORD1_TIME_METADATA)) {
2333                 status = decode_attr_time(xdr, time);
2334                 bitmap[1] &= ~FATTR4_WORD1_TIME_METADATA;
2335         }
2336         dprintk("%s: ctime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2337         return status;
2338 }
2339
2340 static int decode_attr_time_modify(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2341 {
2342         int status = 0;
2343
2344         time->tv_sec = 0;
2345         time->tv_nsec = 0;
2346         if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_MODIFY - 1U)))
2347                 return -EIO;
2348         if (likely(bitmap[1] & FATTR4_WORD1_TIME_MODIFY)) {
2349                 status = decode_attr_time(xdr, time);
2350                 bitmap[1] &= ~FATTR4_WORD1_TIME_MODIFY;
2351         }
2352         dprintk("%s: mtime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2353         return status;
2354 }
2355
2356 static int verify_attr_len(struct xdr_stream *xdr, uint32_t *savep, uint32_t attrlen)
2357 {
2358         unsigned int attrwords = XDR_QUADLEN(attrlen);
2359         unsigned int nwords = xdr->p - savep;
2360
2361         if (unlikely(attrwords != nwords)) {
2362                 printk(KERN_WARNING "%s: server returned incorrect attribute length: %u %c %u\n",
2363                                 __FUNCTION__,
2364                                 attrwords << 2,
2365                                 (attrwords < nwords) ? '<' : '>',
2366                                 nwords << 2);
2367                 return -EIO;
2368         }
2369         return 0;
2370 }
2371
2372 static int decode_change_info(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
2373 {
2374         uint32_t *p;
2375
2376         READ_BUF(20);
2377         READ32(cinfo->atomic);
2378         READ64(cinfo->before);
2379         READ64(cinfo->after);
2380         return 0;
2381 }
2382
2383 static int decode_access(struct xdr_stream *xdr, struct nfs4_accessres *access)
2384 {
2385         uint32_t *p;
2386         uint32_t supp, acc;
2387         int status;
2388
2389         status = decode_op_hdr(xdr, OP_ACCESS);
2390         if (status)
2391                 return status;
2392         READ_BUF(8);
2393         READ32(supp);
2394         READ32(acc);
2395         access->supported = supp;
2396         access->access = acc;
2397         return 0;
2398 }
2399
2400 static int decode_close(struct xdr_stream *xdr, struct nfs_closeres *res)
2401 {
2402         uint32_t *p;
2403         int status;
2404
2405         status = decode_op_hdr(xdr, OP_CLOSE);
2406         if (status)
2407                 return status;
2408         READ_BUF(sizeof(res->stateid.data));
2409         COPYMEM(res->stateid.data, sizeof(res->stateid.data));
2410         return 0;
2411 }
2412
2413 static int decode_commit(struct xdr_stream *xdr, struct nfs_writeres *res)
2414 {
2415         uint32_t *p;
2416         int status;
2417
2418         status = decode_op_hdr(xdr, OP_COMMIT);
2419         if (status)
2420                 return status;
2421         READ_BUF(8);
2422         COPYMEM(res->verf->verifier, 8);
2423         return 0;
2424 }
2425
2426 static int decode_create(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
2427 {
2428         uint32_t *p;
2429         uint32_t bmlen;
2430         int status;
2431
2432         status = decode_op_hdr(xdr, OP_CREATE);
2433         if (status)
2434                 return status;
2435         if ((status = decode_change_info(xdr, cinfo)))
2436                 return status;
2437         READ_BUF(4);
2438         READ32(bmlen);
2439         READ_BUF(bmlen << 2);
2440         return 0;
2441 }
2442
2443 static int decode_server_caps(struct xdr_stream *xdr, struct nfs4_server_caps_res *res)
2444 {
2445         uint32_t *savep;
2446         uint32_t attrlen, 
2447                  bitmap[2] = {0};
2448         int status;
2449
2450         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2451                 goto xdr_error;
2452         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2453                 goto xdr_error;
2454         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2455                 goto xdr_error;
2456         if ((status = decode_attr_supported(xdr, bitmap, res->attr_bitmask)) != 0)
2457                 goto xdr_error;
2458         if ((status = decode_attr_link_support(xdr, bitmap, &res->has_links)) != 0)
2459                 goto xdr_error;
2460         if ((status = decode_attr_symlink_support(xdr, bitmap, &res->has_symlinks)) != 0)
2461                 goto xdr_error;
2462         if ((status = decode_attr_aclsupport(xdr, bitmap, &res->acl_bitmask)) != 0)
2463                 goto xdr_error;
2464         status = verify_attr_len(xdr, savep, attrlen);
2465 xdr_error:
2466         if (status != 0)
2467                 printk(KERN_NOTICE "%s: xdr error %d!\n", __FUNCTION__, -status);
2468         return status;
2469 }
2470         
2471 static int decode_statfs(struct xdr_stream *xdr, struct nfs_fsstat *fsstat)
2472 {
2473         uint32_t *savep;
2474         uint32_t attrlen, 
2475                  bitmap[2] = {0};
2476         int status;
2477         
2478         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2479                 goto xdr_error;
2480         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2481                 goto xdr_error;
2482         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2483                 goto xdr_error;
2484
2485         if ((status = decode_attr_files_avail(xdr, bitmap, &fsstat->afiles)) != 0)
2486                 goto xdr_error;
2487         if ((status = decode_attr_files_free(xdr, bitmap, &fsstat->ffiles)) != 0)
2488                 goto xdr_error;
2489         if ((status = decode_attr_files_total(xdr, bitmap, &fsstat->tfiles)) != 0)
2490                 goto xdr_error;
2491         if ((status = decode_attr_space_avail(xdr, bitmap, &fsstat->abytes)) != 0)
2492                 goto xdr_error;
2493         if ((status = decode_attr_space_free(xdr, bitmap, &fsstat->fbytes)) != 0)
2494                 goto xdr_error;
2495         if ((status = decode_attr_space_total(xdr, bitmap, &fsstat->tbytes)) != 0)
2496                 goto xdr_error;
2497
2498         status = verify_attr_len(xdr, savep, attrlen);
2499 xdr_error:
2500         if (status != 0)
2501                 printk(KERN_NOTICE "%s: xdr error %d!\n", __FUNCTION__, -status);
2502         return status;
2503 }
2504
2505 static int decode_pathconf(struct xdr_stream *xdr, struct nfs_pathconf *pathconf)
2506 {
2507         uint32_t *savep;
2508         uint32_t attrlen, 
2509                  bitmap[2] = {0};
2510         int status;
2511         
2512         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2513                 goto xdr_error;
2514         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2515                 goto xdr_error;
2516         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2517                 goto xdr_error;
2518
2519         if ((status = decode_attr_maxlink(xdr, bitmap, &pathconf->max_link)) != 0)
2520                 goto xdr_error;
2521         if ((status = decode_attr_maxname(xdr, bitmap, &pathconf->max_namelen)) != 0)
2522                 goto xdr_error;
2523
2524         status = verify_attr_len(xdr, savep, attrlen);
2525 xdr_error:
2526         if (status != 0)
2527                 printk(KERN_NOTICE "%s: xdr error %d!\n", __FUNCTION__, -status);
2528         return status;
2529 }
2530
2531 static int decode_getfattr(struct xdr_stream *xdr, struct nfs_fattr *fattr, const struct nfs_server *server)
2532 {
2533         uint32_t *savep;
2534         uint32_t attrlen,
2535                  bitmap[2] = {0},
2536                  type;
2537         int status, fmode = 0;
2538
2539         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2540                 goto xdr_error;
2541         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2542                 goto xdr_error;
2543
2544         fattr->bitmap[0] = bitmap[0];
2545         fattr->bitmap[1] = bitmap[1];
2546
2547         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2548                 goto xdr_error;
2549
2550
2551         if ((status = decode_attr_type(xdr, bitmap, &type)) != 0)
2552                 goto xdr_error;
2553         fattr->type = nfs_type2fmt[type].nfs2type;
2554         fmode = nfs_type2fmt[type].mode;
2555
2556         if ((status = decode_attr_change(xdr, bitmap, &fattr->change_attr)) != 0)
2557                 goto xdr_error;
2558         if ((status = decode_attr_size(xdr, bitmap, &fattr->size)) != 0)
2559                 goto xdr_error;
2560         if ((status = decode_attr_fsid(xdr, bitmap, &fattr->fsid_u.nfs4)) != 0)
2561                 goto xdr_error;
2562         if ((status = decode_attr_fileid(xdr, bitmap, &fattr->fileid)) != 0)
2563                 goto xdr_error;
2564         if ((status = decode_attr_mode(xdr, bitmap, &fattr->mode)) != 0)
2565                 goto xdr_error;
2566         fattr->mode |= fmode;
2567         if ((status = decode_attr_nlink(xdr, bitmap, &fattr->nlink)) != 0)
2568                 goto xdr_error;
2569         if ((status = decode_attr_owner(xdr, bitmap, server->nfs4_state, &fattr->uid)) != 0)
2570                 goto xdr_error;
2571         if ((status = decode_attr_group(xdr, bitmap, server->nfs4_state, &fattr->gid)) != 0)
2572                 goto xdr_error;
2573         if ((status = decode_attr_rdev(xdr, bitmap, &fattr->rdev)) != 0)
2574                 goto xdr_error;
2575         if ((status = decode_attr_space_used(xdr, bitmap, &fattr->du.nfs3.used)) != 0)
2576                 goto xdr_error;
2577         if ((status = decode_attr_time_access(xdr, bitmap, &fattr->atime)) != 0)
2578                 goto xdr_error;
2579         if ((status = decode_attr_time_metadata(xdr, bitmap, &fattr->ctime)) != 0)
2580                 goto xdr_error;
2581         if ((status = decode_attr_time_modify(xdr, bitmap, &fattr->mtime)) != 0)
2582                 goto xdr_error;
2583         if ((status = verify_attr_len(xdr, savep, attrlen)) == 0) {
2584                 fattr->valid = NFS_ATTR_FATTR | NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4;
2585                 fattr->timestamp = jiffies;
2586         }
2587 xdr_error:
2588         if (status != 0)
2589                 printk(KERN_NOTICE "%s: xdr error %d!\n", __FUNCTION__, -status);
2590         return status;
2591 }
2592
2593
2594 static int decode_fsinfo(struct xdr_stream *xdr, struct nfs_fsinfo *fsinfo)
2595 {
2596         uint32_t *savep;
2597         uint32_t attrlen, bitmap[2];
2598         int status;
2599
2600         if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2601                 goto xdr_error;
2602         if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2603                 goto xdr_error;
2604         if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2605                 goto xdr_error;
2606
2607         fsinfo->rtmult = fsinfo->wtmult = 512;  /* ??? */
2608
2609         if ((status = decode_attr_lease_time(xdr, bitmap, &fsinfo->lease_time)) != 0)
2610                 goto xdr_error;
2611         if ((status = decode_attr_maxfilesize(xdr, bitmap, &fsinfo->maxfilesize)) != 0)
2612                 goto xdr_error;
2613         if ((status = decode_attr_maxread(xdr, bitmap, &fsinfo->rtmax)) != 0)
2614                 goto xdr_error;
2615         fsinfo->rtpref = fsinfo->dtpref = fsinfo->rtmax;
2616         if ((status = decode_attr_maxwrite(xdr, bitmap, &fsinfo->wtmax)) != 0)
2617                 goto xdr_error;
2618         fsinfo->wtpref = fsinfo->wtmax;
2619
2620         status = verify_attr_len(xdr, savep, attrlen);
2621 xdr_error:
2622         if (status != 0)
2623                 printk(KERN_NOTICE "%s: xdr error %d!\n", __FUNCTION__, -status);
2624         return status;
2625 }
2626
2627 static int decode_getfh(struct xdr_stream *xdr, struct nfs_fh *fh)
2628 {
2629         uint32_t *p;
2630         uint32_t len;
2631         int status;
2632
2633         status = decode_op_hdr(xdr, OP_GETFH);
2634         if (status)
2635                 return status;
2636         /* Zero handle first to allow comparisons */
2637         memset(fh, 0, sizeof(*fh));
2638
2639         READ_BUF(4);
2640         READ32(len);
2641         if (len > NFS4_FHSIZE)
2642                 return -EIO;
2643         fh->size = len;
2644         READ_BUF(len);
2645         COPYMEM(fh->data, len);
2646         return 0;
2647 }
2648
2649 static int decode_link(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
2650 {
2651         int status;
2652         
2653         status = decode_op_hdr(xdr, OP_LINK);
2654         if (status)
2655                 return status;
2656         return decode_change_info(xdr, cinfo);
2657 }
2658
2659 /*
2660  * We create the owner, so we know a proper owner.id length is 4.
2661  */
2662 static int decode_lock_denied (struct xdr_stream *xdr, struct nfs_lock_denied *denied)
2663 {
2664         uint32_t *p;
2665         uint32_t namelen;
2666
2667         READ_BUF(32);
2668         READ64(denied->offset);
2669         READ64(denied->length);
2670         READ32(denied->type);
2671         READ64(denied->owner.clientid);
2672         READ32(namelen);
2673         READ_BUF(namelen);
2674         if (namelen == 4)
2675                 READ32(denied->owner.id);
2676         return -NFS4ERR_DENIED;
2677 }
2678
2679 static int decode_lock(struct xdr_stream *xdr, struct nfs_lockres *res)
2680 {
2681         uint32_t *p;
2682         int status;
2683
2684         status = decode_op_hdr(xdr, OP_LOCK);
2685         if (status == 0) {
2686                 READ_BUF(sizeof(nfs4_stateid));
2687                 COPYMEM(&res->u.stateid, sizeof(res->u.stateid));
2688         } else if (status == -NFS4ERR_DENIED)
2689                 return decode_lock_denied(xdr, &res->u.denied);
2690         return status;
2691 }
2692
2693 static int decode_lockt(struct xdr_stream *xdr, struct nfs_lockres *res)
2694 {
2695         int status;
2696         status = decode_op_hdr(xdr, OP_LOCKT);
2697         if (status == -NFS4ERR_DENIED)
2698                 return decode_lock_denied(xdr, &res->u.denied);
2699         return status;
2700 }
2701
2702 static int decode_locku(struct xdr_stream *xdr, struct nfs_lockres *res)
2703 {
2704         uint32_t *p;
2705         int status;
2706
2707         status = decode_op_hdr(xdr, OP_LOCKU);
2708         if (status == 0) {
2709                 READ_BUF(sizeof(nfs4_stateid));
2710                 COPYMEM(&res->u.stateid, sizeof(res->u.stateid));
2711         }
2712         return status;
2713 }
2714
2715 static int decode_lookup(struct xdr_stream *xdr)
2716 {
2717         return decode_op_hdr(xdr, OP_LOOKUP);
2718 }
2719
2720 static int decode_open(struct xdr_stream *xdr, struct nfs_openres *res)
2721 {
2722         uint32_t *p;
2723         uint32_t bmlen, delegation_type;
2724         int status;
2725
2726         status = decode_op_hdr(xdr, OP_OPEN);
2727         if (status)
2728                 return status;
2729         READ_BUF(sizeof(res->stateid.data));
2730         COPYMEM(res->stateid.data, sizeof(res->stateid.data));
2731
2732         decode_change_info(xdr, &res->cinfo);
2733
2734         READ_BUF(8);
2735         READ32(res->rflags);
2736         READ32(bmlen);
2737         if (bmlen > 10)
2738                 goto xdr_error;
2739
2740         READ_BUF((bmlen << 2) + 4);
2741         p += bmlen;
2742         READ32(delegation_type);
2743         if (delegation_type == NFS4_OPEN_DELEGATE_NONE)
2744                 return 0;
2745 xdr_error:
2746         printk(KERN_NOTICE "%s: xdr error!\n", __FUNCTION__);
2747         return -EIO;
2748 }
2749
2750 static int decode_open_confirm(struct xdr_stream *xdr, struct nfs_open_confirmres *res)
2751 {
2752         uint32_t *p;
2753         int status;
2754
2755         status = decode_op_hdr(xdr, OP_OPEN_CONFIRM);
2756         if (status)
2757                 return status;
2758         READ_BUF(sizeof(res->stateid.data));
2759         COPYMEM(res->stateid.data, sizeof(res->stateid.data));
2760         return 0;
2761 }
2762
2763 static int decode_open_downgrade(struct xdr_stream *xdr, struct nfs_closeres *res)
2764 {
2765         uint32_t *p;
2766         int status;
2767
2768         status = decode_op_hdr(xdr, OP_OPEN_DOWNGRADE);
2769         if (status)
2770                 return status;
2771         READ_BUF(sizeof(res->stateid.data));
2772         COPYMEM(res->stateid.data, sizeof(res->stateid.data));
2773         return 0;
2774 }
2775
2776 static int decode_putfh(struct xdr_stream *xdr)
2777 {
2778         return decode_op_hdr(xdr, OP_PUTFH);
2779 }
2780
2781 static int decode_putrootfh(struct xdr_stream *xdr)
2782 {
2783         return decode_op_hdr(xdr, OP_PUTROOTFH);
2784 }
2785
2786 static int decode_read(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs_readres *res)
2787 {
2788         struct iovec *iov = req->rq_rcv_buf.head;
2789         uint32_t *p;
2790         uint32_t count, eof, recvd, hdrlen;
2791         int status;
2792
2793         status = decode_op_hdr(xdr, OP_READ);
2794         if (status)
2795                 return status;
2796         READ_BUF(8);
2797         READ32(eof);
2798         READ32(count);
2799         hdrlen = (u8 *) p - (u8 *) iov->iov_base;
2800         recvd = req->rq_rcv_buf.len - hdrlen;
2801         if (count > recvd) {
2802                 printk(KERN_WARNING "NFS: server cheating in read reply: "
2803                                 "count %u > recvd %u\n", count, recvd);
2804                 count = recvd;
2805                 eof = 0;
2806         }
2807         xdr_read_pages(xdr, count);
2808         res->eof = eof;
2809         res->count = count;
2810         return 0;
2811 }
2812
2813 static int decode_readdir(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs4_readdir_res *readdir)
2814 {
2815         struct xdr_buf  *rcvbuf = &req->rq_rcv_buf;
2816         struct page     *page = *rcvbuf->pages;
2817         struct iovec    *iov = rcvbuf->head;
2818         unsigned int    nr, pglen = rcvbuf->page_len;
2819         uint32_t        *end, *entry, *p, *kaddr;
2820         uint32_t        len, attrlen, word;
2821         int             i, hdrlen, recvd, status;
2822
2823         status = decode_op_hdr(xdr, OP_READDIR);
2824         if (status)
2825                 return status;
2826         READ_BUF(8);
2827         COPYMEM(readdir->verifier.data, 8);
2828
2829         hdrlen = (char *) p - (char *) iov->iov_base;
2830         recvd = rcvbuf->len - hdrlen;
2831         if (pglen > recvd)
2832                 pglen = recvd;
2833         xdr_read_pages(xdr, pglen);
2834
2835         BUG_ON(pglen + readdir->pgbase > PAGE_CACHE_SIZE);
2836         kaddr = p = (uint32_t *) kmap_atomic(page, KM_USER0);
2837         end = (uint32_t *) ((char *)p + pglen + readdir->pgbase);
2838         entry = p;
2839         for (nr = 0; *p++; nr++) {
2840                 if (p + 3 > end)
2841                         goto short_pkt;
2842                 p += 2;     /* cookie */
2843                 len = ntohl(*p++);  /* filename length */
2844                 if (len > NFS4_MAXNAMLEN) {
2845                         printk(KERN_WARNING "NFS: giant filename in readdir (len 0x%x)\n", len);
2846                         goto err_unmap;
2847                 }
2848                         
2849                 p += XDR_QUADLEN(len);
2850                 if (p + 1 > end)
2851                         goto short_pkt;
2852                 len = ntohl(*p++);  /* bitmap length */
2853                 if (len > 10) {
2854                         printk(KERN_WARNING "NFS: giant bitmap in readdir (len 0x%x)\n", len);
2855                         goto err_unmap;
2856                 }
2857                 if (p + len + 1 > end)
2858                         goto short_pkt;
2859                 attrlen = 0;
2860                 for (i = 0; i < len; i++) {
2861                         word = ntohl(*p++);
2862                         if (!word)
2863                                 continue;
2864                         else if (i == 0 && word == FATTR4_WORD0_FILEID) {
2865                                 attrlen = 8;
2866                                 continue;
2867                         }
2868                         printk(KERN_WARNING "NFS: unexpected bitmap word in readdir (0x%x)\n", word);
2869                         goto err_unmap;
2870                 }
2871                 if (ntohl(*p++) != attrlen) {
2872                         printk(KERN_WARNING "NFS: unexpected attrlen in readdir\n");
2873                         goto err_unmap;
2874                 }
2875                 p += XDR_QUADLEN(attrlen);
2876                 if (p + 1 > end)
2877                         goto short_pkt;
2878         }
2879         if (!nr && (entry[0] != 0 || entry[1] == 0))
2880                 goto short_pkt;
2881 out:    
2882         kunmap_atomic(kaddr, KM_USER0);
2883         return 0;
2884 short_pkt:
2885         entry[0] = entry[1] = 0;
2886         /* truncate listing ? */
2887         if (!nr) {
2888                 printk(KERN_NOTICE "NFS: readdir reply truncated!\n");
2889                 entry[1] = 1;
2890         }
2891         goto out;
2892 err_unmap:
2893         kunmap_atomic(kaddr, KM_USER0);
2894         return -errno_NFSERR_IO;
2895 }
2896
2897 static int decode_readlink(struct xdr_stream *xdr, struct rpc_rqst *req)
2898 {
2899         struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
2900         struct iovec *iov = rcvbuf->head;
2901         uint32_t *strlen;
2902         unsigned int hdrlen, len;
2903         char *string;
2904         int status;
2905
2906         status = decode_op_hdr(xdr, OP_READLINK);
2907         if (status)
2908                 return status;
2909
2910         hdrlen = (char *) xdr->p - (char *) iov->iov_base;
2911         if (iov->iov_len > hdrlen) {
2912                 dprintk("NFS: READLINK header is short. iovec will be shifted.\n");
2913                 xdr_shift_buf(rcvbuf, iov->iov_len - hdrlen);
2914
2915         }
2916         /*
2917          * The XDR encode routine has set things up so that
2918          * the link text will be copied directly into the
2919          * buffer.  We just have to do overflow-checking,
2920          * and and null-terminate the text (the VFS expects
2921          * null-termination).
2922          */
2923         strlen = (uint32_t *) kmap_atomic(rcvbuf->pages[0], KM_USER0);
2924         len = ntohl(*strlen);
2925         if (len > rcvbuf->page_len) {
2926                 dprintk(KERN_WARNING "nfs: server returned giant symlink!\n");
2927                 kunmap_atomic(strlen, KM_USER0);
2928                 return -ENAMETOOLONG;
2929         }
2930         *strlen = len;
2931
2932         string = (char *)(strlen + 1);
2933         string[len] = '\0';
2934         kunmap_atomic(strlen, KM_USER0);
2935         return 0;
2936 }
2937
2938 static int decode_remove(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
2939 {
2940         int status;
2941
2942         status = decode_op_hdr(xdr, OP_REMOVE);
2943         if (status)
2944                 goto out;
2945         status = decode_change_info(xdr, cinfo);
2946 out:
2947         return status;
2948 }
2949
2950 static int decode_rename(struct xdr_stream *xdr, struct nfs4_change_info *old_cinfo,
2951               struct nfs4_change_info *new_cinfo)
2952 {
2953         int status;
2954
2955         status = decode_op_hdr(xdr, OP_RENAME);
2956         if (status)
2957                 goto out;
2958         if ((status = decode_change_info(xdr, old_cinfo)))
2959                 goto out;
2960         status = decode_change_info(xdr, new_cinfo);
2961 out:
2962         return status;
2963 }
2964
2965 static int decode_renew(struct xdr_stream *xdr)
2966 {
2967         return decode_op_hdr(xdr, OP_RENEW);
2968 }
2969
2970 static int
2971 decode_savefh(struct xdr_stream *xdr)
2972 {
2973         return decode_op_hdr(xdr, OP_SAVEFH);
2974 }
2975
2976 static int decode_setattr(struct xdr_stream *xdr, struct nfs_setattrres *res)
2977 {
2978         uint32_t *p;
2979         uint32_t bmlen;
2980         int status;
2981
2982         
2983         status = decode_op_hdr(xdr, OP_SETATTR);
2984         if (status)
2985                 return status;
2986         READ_BUF(4);
2987         READ32(bmlen);
2988         READ_BUF(bmlen << 2);
2989         return 0;
2990 }
2991
2992 static int decode_setclientid(struct xdr_stream *xdr, struct nfs4_client *clp)
2993 {
2994         uint32_t *p;
2995         uint32_t opnum;
2996         int32_t nfserr;
2997
2998         READ_BUF(8);
2999         READ32(opnum);
3000         if (opnum != OP_SETCLIENTID) {
3001                 printk(KERN_NOTICE
3002                                 "nfs4_decode_setclientid: Server returned operation"
3003                                 " %d\n", opnum);
3004                 return -EIO;
3005         }
3006         READ32(nfserr);
3007         if (nfserr == NFS_OK) {
3008                 READ_BUF(8 + sizeof(clp->cl_confirm.data));
3009                 READ64(clp->cl_clientid);
3010                 COPYMEM(clp->cl_confirm.data, sizeof(clp->cl_confirm.data));
3011         } else if (nfserr == NFSERR_CLID_INUSE) {
3012                 uint32_t len;
3013
3014                 /* skip netid string */
3015                 READ_BUF(4);
3016                 READ32(len);
3017                 READ_BUF(len);
3018
3019                 /* skip uaddr string */
3020                 READ_BUF(4);
3021                 READ32(len);
3022                 READ_BUF(len);
3023                 return -EEXIST;
3024         } else
3025                 return -nfs_stat_to_errno(nfserr);
3026
3027         return 0;
3028 }
3029
3030 static int decode_setclientid_confirm(struct xdr_stream *xdr)
3031 {
3032         return decode_op_hdr(xdr, OP_SETCLIENTID_CONFIRM);
3033 }
3034
3035 static int decode_write(struct xdr_stream *xdr, struct nfs_writeres *res)
3036 {
3037         uint32_t *p;
3038         int status;
3039
3040         status = decode_op_hdr(xdr, OP_WRITE);
3041         if (status)
3042                 return status;
3043
3044         READ_BUF(16);
3045         READ32(res->count);
3046         READ32(res->verf->committed);
3047         COPYMEM(res->verf->verifier, 8);
3048         return 0;
3049 }
3050
3051 /*
3052  * Decode OPEN_DOWNGRADE response
3053  */
3054 static int nfs4_xdr_dec_open_downgrade(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_closeres *res)
3055 {
3056         struct xdr_stream xdr;
3057         struct compound_hdr hdr;
3058         int status;
3059
3060         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3061         status = decode_compound_hdr(&xdr, &hdr);
3062         if (status)
3063                 goto out;
3064         status = decode_putfh(&xdr);
3065         if (status)
3066                 goto out;
3067         status = decode_open_downgrade(&xdr, res);
3068 out:
3069         return status;
3070 }
3071
3072 /*
3073  * END OF "GENERIC" DECODE ROUTINES.
3074  */
3075
3076 /*
3077  * Decode ACCESS response
3078  */
3079 static int nfs4_xdr_dec_access(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_accessres *res)
3080 {
3081         struct xdr_stream xdr;
3082         struct compound_hdr hdr;
3083         int status;
3084         
3085         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3086         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3087                 goto out;
3088         if ((status = decode_putfh(&xdr)) == 0)
3089                 status = decode_access(&xdr, res);
3090 out:
3091         return status;
3092 }
3093
3094 /*
3095  * Decode LOOKUP response
3096  */
3097 static int nfs4_xdr_dec_lookup(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_lookup_res *res)
3098 {
3099         struct xdr_stream xdr;
3100         struct compound_hdr hdr;
3101         int status;
3102         
3103         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3104         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3105                 goto out;
3106         if ((status = decode_putfh(&xdr)) != 0)
3107                 goto out;
3108         if ((status = decode_lookup(&xdr)) != 0)
3109                 goto out;
3110         if ((status = decode_getfh(&xdr, res->fh)) != 0)
3111                 goto out;
3112         status = decode_getfattr(&xdr, res->fattr, res->server);
3113 out:
3114         return status;
3115 }
3116
3117 /*
3118  * Decode LOOKUP_ROOT response
3119  */
3120 static int nfs4_xdr_dec_lookup_root(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_lookup_res *res)
3121 {
3122         struct xdr_stream xdr;
3123         struct compound_hdr hdr;
3124         int status;
3125         
3126         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3127         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3128                 goto out;
3129         if ((status = decode_putrootfh(&xdr)) != 0)
3130                 goto out;
3131         if ((status = decode_getfh(&xdr, res->fh)) == 0)
3132                 status = decode_getfattr(&xdr, res->fattr, res->server);
3133 out:
3134         return status;
3135 }
3136
3137 /*
3138  * Decode REMOVE response
3139  */
3140 static int nfs4_xdr_dec_remove(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_change_info *cinfo)
3141 {
3142         struct xdr_stream xdr;
3143         struct compound_hdr hdr;
3144         int status;
3145         
3146         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3147         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3148                 goto out;
3149         if ((status = decode_putfh(&xdr)) == 0)
3150                 status = decode_remove(&xdr, cinfo);
3151 out:
3152         return status;
3153 }
3154
3155 /*
3156  * Decode RENAME response
3157  */
3158 static int nfs4_xdr_dec_rename(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_rename_res *res)
3159 {
3160         struct xdr_stream xdr;
3161         struct compound_hdr hdr;
3162         int status;
3163         
3164         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3165         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3166                 goto out;
3167         if ((status = decode_putfh(&xdr)) != 0)
3168                 goto out;
3169         if ((status = decode_savefh(&xdr)) != 0)
3170                 goto out;
3171         if ((status = decode_putfh(&xdr)) != 0)
3172                 goto out;
3173         status = decode_rename(&xdr, &res->old_cinfo, &res->new_cinfo);
3174 out:
3175         return status;
3176 }
3177
3178 /*
3179  * Decode LINK response
3180  */
3181 static int nfs4_xdr_dec_link(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_change_info *cinfo)
3182 {
3183         struct xdr_stream xdr;
3184         struct compound_hdr hdr;
3185         int status;
3186         
3187         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3188         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3189                 goto out;
3190         if ((status = decode_putfh(&xdr)) != 0)
3191                 goto out;
3192         if ((status = decode_savefh(&xdr)) != 0)
3193                 goto out;
3194         if ((status = decode_putfh(&xdr)) != 0)
3195                 goto out;
3196         status = decode_link(&xdr, cinfo);
3197 out:
3198         return status;
3199 }
3200
3201 /*
3202  * Decode CREATE response
3203  */
3204 static int nfs4_xdr_dec_create(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_create_res *res)
3205 {
3206         struct xdr_stream xdr;
3207         struct compound_hdr hdr;
3208         int status;
3209         
3210         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3211         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3212                 goto out;
3213         if ((status = decode_putfh(&xdr)) != 0)
3214                 goto out;
3215         if ((status = decode_create(&xdr,&res->dir_cinfo)) != 0)
3216                 goto out;
3217         if ((status = decode_getfattr(&xdr, res->fattr, res->server)) != 0)
3218                 goto out;
3219         status = decode_getfh(&xdr, res->fh);
3220 out:
3221         return status;
3222 }
3223
3224 /*
3225  * Decode GETATTR response
3226  */
3227 static int nfs4_xdr_dec_getattr(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_getattr_res *res)
3228 {
3229         struct xdr_stream xdr;
3230         struct compound_hdr hdr;
3231         int status;
3232         
3233         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3234         status = decode_compound_hdr(&xdr, &hdr);
3235         if (status)
3236                 goto out;
3237         status = decode_putfh(&xdr);
3238         if (status)
3239                 goto out;
3240         status = decode_getfattr(&xdr, res->fattr, res->server);
3241 out:
3242         return status;
3243
3244 }
3245
3246
3247 /*
3248  * Decode CLOSE response
3249  */
3250 static int nfs4_xdr_dec_close(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_closeres *res)
3251 {
3252         struct xdr_stream xdr;
3253         struct compound_hdr hdr;
3254         int status;
3255
3256         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3257         status = decode_compound_hdr(&xdr, &hdr);
3258         if (status)
3259                 goto out;
3260         status = decode_putfh(&xdr);
3261         if (status)
3262                 goto out;
3263         status = decode_close(&xdr, res);
3264 out:
3265         return status;
3266 }
3267
3268 /*
3269  * Decode OPEN response
3270  */
3271 static int nfs4_xdr_dec_open(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_openres *res)
3272 {
3273         struct xdr_stream xdr;
3274         struct compound_hdr hdr;
3275         int status;
3276
3277         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3278         status = decode_compound_hdr(&xdr, &hdr);
3279         if (status)
3280                 goto out;
3281         status = decode_putfh(&xdr);
3282         if (status)
3283                 goto out;
3284         status = decode_open(&xdr, res);
3285         if (status)
3286                 goto out;
3287         status = decode_getfattr(&xdr, res->f_attr, res->server);
3288         if (status)
3289                 goto out;
3290         status = decode_getfh(&xdr, &res->fh);
3291 out:
3292         return status;
3293 }
3294
3295 /*
3296  * Decode OPEN_CONFIRM response
3297  */
3298 static int nfs4_xdr_dec_open_confirm(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_open_confirmres *res)
3299 {
3300         struct xdr_stream xdr;
3301         struct compound_hdr hdr;
3302         int status;
3303
3304         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3305         status = decode_compound_hdr(&xdr, &hdr);
3306         if (status)
3307                 goto out;
3308         status = decode_putfh(&xdr);
3309         if (status)
3310                 goto out;
3311         status = decode_open_confirm(&xdr, res);
3312 out:
3313         return status;
3314 }
3315
3316 /*
3317  * Decode OPEN_RECLAIM response
3318  */
3319 static int nfs4_xdr_dec_open_reclaim(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_openres *res)
3320 {
3321         struct xdr_stream xdr;
3322         struct compound_hdr hdr;
3323         int status;
3324
3325         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3326         status = decode_compound_hdr(&xdr, &hdr);
3327         if (status)
3328                 goto out;
3329         status = decode_putfh(&xdr);
3330         if (status)
3331                 goto out;
3332         status = decode_open(&xdr, res);
3333         if (status)
3334                 goto out;
3335         status = decode_getfattr(&xdr, res->f_attr, res->server);
3336 out:
3337         return status;
3338 }
3339
3340 /*
3341  * Decode SETATTR response
3342  */
3343 static int nfs4_xdr_dec_setattr(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_setattrres *res)
3344 {
3345         struct xdr_stream xdr;
3346         struct compound_hdr hdr;
3347         int status;
3348
3349         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3350         status = decode_compound_hdr(&xdr, &hdr);
3351         if (status)
3352                 goto out;
3353         status = decode_putfh(&xdr);
3354         if (status)
3355                 goto out;
3356         status = decode_setattr(&xdr, res);
3357         if (status)
3358                 goto out;
3359         status = decode_getfattr(&xdr, res->fattr, res->server);
3360 out:
3361         return status;
3362 }
3363
3364 /*
3365  * Decode LOCK response
3366  */
3367 static int nfs4_xdr_dec_lock(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_lockres *res)
3368 {
3369         struct xdr_stream xdr;
3370         struct compound_hdr hdr;
3371         int status;
3372
3373         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3374         status = decode_compound_hdr(&xdr, &hdr);
3375         if (status)
3376                 goto out;
3377         status = decode_putfh(&xdr);
3378         if (status)
3379                 goto out;
3380         status = decode_lock(&xdr, res);
3381 out:
3382         return status;
3383 }
3384
3385 /*
3386  * Decode LOCKT response
3387  */
3388 static int nfs4_xdr_dec_lockt(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_lockres *res)
3389 {
3390         struct xdr_stream xdr;
3391         struct compound_hdr hdr;
3392         int status;
3393
3394         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3395         status = decode_compound_hdr(&xdr, &hdr);
3396         if (status)
3397                 goto out;
3398         status = decode_putfh(&xdr);
3399         if (status)
3400                 goto out;
3401         status = decode_lockt(&xdr, res);
3402 out:
3403         return status;
3404 }
3405
3406 /*
3407  * Decode LOCKU response
3408  */
3409 static int nfs4_xdr_dec_locku(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_lockres *res)
3410 {
3411         struct xdr_stream xdr;
3412         struct compound_hdr hdr;
3413         int status;
3414
3415         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3416         status = decode_compound_hdr(&xdr, &hdr);
3417         if (status)
3418                 goto out;
3419         status = decode_putfh(&xdr);
3420         if (status)
3421                 goto out;
3422         status = decode_locku(&xdr, res);
3423 out:
3424         return status;
3425 }
3426
3427 /*
3428  * Decode READLINK response
3429  */
3430 static int nfs4_xdr_dec_readlink(struct rpc_rqst *rqstp, uint32_t *p, void *res)
3431 {
3432         struct xdr_stream xdr;
3433         struct compound_hdr hdr;
3434         int status;
3435
3436         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3437         status = decode_compound_hdr(&xdr, &hdr);
3438         if (status)
3439                 goto out;
3440         status = decode_putfh(&xdr);
3441         if (status)
3442                 goto out;
3443         status = decode_readlink(&xdr, rqstp);
3444 out:
3445         return status;
3446 }
3447
3448 /*
3449  * Decode READDIR response
3450  */
3451 static int nfs4_xdr_dec_readdir(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_readdir_res *res)
3452 {
3453         struct xdr_stream xdr;
3454         struct compound_hdr hdr;
3455         int status;
3456
3457         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3458         status = decode_compound_hdr(&xdr, &hdr);
3459         if (status)
3460                 goto out;
3461         status = decode_putfh(&xdr);
3462         if (status)
3463                 goto out;
3464         status = decode_readdir(&xdr, rqstp, res);
3465 out:
3466         return status;
3467 }
3468
3469 /*
3470  * Decode Read response
3471  */
3472 static int nfs4_xdr_dec_read(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_readres *res)
3473 {
3474         struct xdr_stream xdr;
3475         struct compound_hdr hdr;
3476         int status;
3477
3478         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3479         status = decode_compound_hdr(&xdr, &hdr);
3480         if (status)
3481                 goto out;
3482         status = decode_putfh(&xdr);
3483         if (status)
3484                 goto out;
3485         status = decode_read(&xdr, rqstp, res);
3486         if (!status)
3487                 status = res->count;
3488 out:
3489         return status;
3490 }
3491
3492 /*
3493  * Decode WRITE response
3494  */
3495 static int nfs4_xdr_dec_write(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_writeres *res)
3496 {
3497         struct xdr_stream xdr;
3498         struct compound_hdr hdr;
3499         int status;
3500
3501         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3502         status = decode_compound_hdr(&xdr, &hdr);
3503         if (status)
3504                 goto out;
3505         status = decode_putfh(&xdr);
3506         if (status)
3507                 goto out;
3508         status = decode_write(&xdr, res);
3509         if (!status)
3510                 status = res->count;
3511 out:
3512         return status;
3513 }
3514
3515 /*
3516  * Decode COMMIT response
3517  */
3518 static int nfs4_xdr_dec_commit(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_writeres *res)
3519 {
3520         struct xdr_stream xdr;
3521         struct compound_hdr hdr;
3522         int status;
3523
3524         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3525         status = decode_compound_hdr(&xdr, &hdr);
3526         if (status)
3527                 goto out;
3528         status = decode_putfh(&xdr);
3529         if (status)
3530                 goto out;
3531         status = decode_commit(&xdr, res);
3532 out:
3533         return status;
3534 }
3535
3536 /*
3537  * FSINFO request
3538  */
3539 static int nfs4_xdr_dec_fsinfo(struct rpc_rqst *req, uint32_t *p, struct nfs_fsinfo *fsinfo)
3540 {
3541         struct xdr_stream xdr;
3542         struct compound_hdr hdr;
3543         int status;
3544
3545         xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
3546         status = decode_compound_hdr(&xdr, &hdr);
3547         if (!status)
3548                 status = decode_putfh(&xdr);
3549         if (!status)
3550                 status = decode_fsinfo(&xdr, fsinfo);
3551         if (!status)
3552                 status = -nfs_stat_to_errno(hdr.status);
3553         return status;
3554 }
3555
3556 /*
3557  * PATHCONF request
3558  */
3559 static int nfs4_xdr_dec_pathconf(struct rpc_rqst *req, uint32_t *p, struct nfs_pathconf *pathconf)
3560 {
3561         struct xdr_stream xdr;
3562         struct compound_hdr hdr;
3563         int status;
3564
3565         xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
3566         status = decode_compound_hdr(&xdr, &hdr);
3567         if (!status)
3568                 status = decode_putfh(&xdr);
3569         if (!status)
3570                 status = decode_pathconf(&xdr, pathconf);
3571         return status;
3572 }
3573
3574 /*
3575  * STATFS request
3576  */
3577 static int nfs4_xdr_dec_statfs(struct rpc_rqst *req, uint32_t *p, struct nfs_fsstat *fsstat)
3578 {
3579         struct xdr_stream xdr;
3580         struct compound_hdr hdr;
3581         int status;
3582
3583         xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
3584         status = decode_compound_hdr(&xdr, &hdr);
3585         if (!status)
3586                 status = decode_putfh(&xdr);
3587         if (!status)
3588                 status = decode_statfs(&xdr, fsstat);
3589         return status;
3590 }
3591
3592 /*
3593  * GETATTR_BITMAP request
3594  */
3595 static int nfs4_xdr_dec_server_caps(struct rpc_rqst *req, uint32_t *p, struct nfs4_server_caps_res *res)
3596 {
3597         struct xdr_stream xdr;
3598         struct compound_hdr hdr;
3599         int status;
3600
3601         xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
3602         if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3603                 goto out;
3604         if ((status = decode_putfh(&xdr)) != 0)
3605                 goto out;
3606         status = decode_server_caps(&xdr, res);
3607 out:
3608         return status;
3609 }
3610
3611 /*
3612  * Decode RENEW response
3613  */
3614 static int nfs4_xdr_dec_renew(struct rpc_rqst *rqstp, uint32_t *p, void *dummy)
3615 {
3616         struct xdr_stream xdr;
3617         struct compound_hdr hdr;
3618         int status;
3619
3620         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3621         status = decode_compound_hdr(&xdr, &hdr);
3622         if (!status)
3623                 status = decode_renew(&xdr);
3624         return status;
3625 }
3626
3627 /*
3628  * a SETCLIENTID request
3629  */
3630 static int nfs4_xdr_dec_setclientid(struct rpc_rqst *req, uint32_t *p,
3631                 struct nfs4_client *clp)
3632 {
3633         struct xdr_stream xdr;
3634         struct compound_hdr hdr;
3635         int status;
3636
3637         xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
3638         status = decode_compound_hdr(&xdr, &hdr);
3639         if (!status)
3640                 status = decode_setclientid(&xdr, clp);
3641         if (!status)
3642                 status = -nfs_stat_to_errno(hdr.status);
3643         return status;
3644 }
3645
3646 /*
3647  * a SETCLIENTID_CONFIRM request
3648  */
3649 static int nfs4_xdr_dec_setclientid_confirm(struct rpc_rqst *req, uint32_t *p, struct nfs_fsinfo *fsinfo)
3650 {
3651         struct xdr_stream xdr;
3652         struct compound_hdr hdr;
3653         int status;
3654
3655         xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
3656         status = decode_compound_hdr(&xdr, &hdr);
3657         if (!status)
3658                 status = decode_setclientid_confirm(&xdr);
3659         if (!status)
3660                 status = decode_putrootfh(&xdr);
3661         if (!status)
3662                 status = decode_fsinfo(&xdr, fsinfo);
3663         if (!status)
3664                 status = -nfs_stat_to_errno(hdr.status);
3665         return status;
3666 }
3667
3668 uint32_t *nfs4_decode_dirent(uint32_t *p, struct nfs_entry *entry, int plus)
3669 {
3670         uint32_t len;
3671
3672         if (!*p++) {
3673                 if (!*p)
3674                         return ERR_PTR(-EAGAIN);
3675                 entry->eof = 1;
3676                 return ERR_PTR(-EBADCOOKIE);
3677         }
3678
3679         entry->prev_cookie = entry->cookie;
3680         p = xdr_decode_hyper(p, &entry->cookie);
3681         entry->len = ntohl(*p++);
3682         entry->name = (const char *) p;
3683         p += XDR_QUADLEN(entry->len);
3684
3685         /*
3686          * In case the server doesn't return an inode number,
3687          * we fake one here.  (We don't use inode number 0,
3688          * since glibc seems to choke on it...)
3689          */
3690         entry->ino = 1;
3691
3692         len = ntohl(*p++);             /* bitmap length */
3693         p += len;
3694         len = ntohl(*p++);             /* attribute buffer length */
3695         if (len)
3696                 p = xdr_decode_hyper(p, &entry->ino);
3697
3698         entry->eof = !p[0] && p[1];
3699         return p;
3700 }
3701
3702 /*
3703  * We need to translate between nfs status return values and
3704  * the local errno values which may not be the same.
3705  */
3706 static struct {
3707         int stat;
3708         int errno;
3709 } nfs_errtbl[] = {
3710         { NFS4_OK,              0               },
3711         { NFS4ERR_PERM,         EPERM           },
3712         { NFS4ERR_NOENT,        ENOENT          },
3713         { NFS4ERR_IO,           errno_NFSERR_IO },
3714         { NFS4ERR_NXIO,         ENXIO           },
3715         { NFS4ERR_ACCESS,       EACCES          },
3716         { NFS4ERR_EXIST,        EEXIST          },
3717         { NFS4ERR_XDEV,         EXDEV           },
3718         { NFS4ERR_NOTDIR,       ENOTDIR         },
3719         { NFS4ERR_ISDIR,        EISDIR          },
3720         { NFS4ERR_INVAL,        EINVAL          },
3721         { NFS4ERR_FBIG,         EFBIG           },
3722         { NFS4ERR_NOSPC,        ENOSPC          },
3723         { NFS4ERR_ROFS,         EROFS           },
3724         { NFS4ERR_MLINK,        EMLINK          },
3725         { NFS4ERR_NAMETOOLONG,  ENAMETOOLONG    },
3726         { NFS4ERR_NOTEMPTY,     ENOTEMPTY       },
3727         { NFS4ERR_DQUOT,        EDQUOT          },
3728         { NFS4ERR_STALE,        ESTALE          },
3729         { NFS4ERR_BADHANDLE,    EBADHANDLE      },
3730         { NFS4ERR_BAD_COOKIE,   EBADCOOKIE      },
3731         { NFS4ERR_NOTSUPP,      ENOTSUPP        },
3732         { NFS4ERR_TOOSMALL,     ETOOSMALL       },
3733         { NFS4ERR_SERVERFAULT,  ESERVERFAULT    },
3734         { NFS4ERR_BADTYPE,      EBADTYPE        },
3735         { NFS4ERR_LOCKED,       EAGAIN          },
3736         { NFS4ERR_RESOURCE,     EREMOTEIO       },
3737         { NFS4ERR_SYMLINK,      ELOOP           },
3738         { NFS4ERR_OP_ILLEGAL,   EOPNOTSUPP      },
3739         { NFS4ERR_DEADLOCK,     EDEADLK         },
3740         { NFS4ERR_WRONGSEC,     EPERM           }, /* FIXME: this needs
3741                                                     * to be handled by a
3742                                                     * middle-layer.
3743                                                     */
3744         { -1,                   EIO             }
3745 };
3746
3747 /*
3748  * Convert an NFS error code to a local one.
3749  * This one is used jointly by NFSv2 and NFSv3.
3750  */
3751 static int
3752 nfs_stat_to_errno(int stat)
3753 {
3754         int i;
3755         for (i = 0; nfs_errtbl[i].stat != -1; i++) {
3756                 if (nfs_errtbl[i].stat == stat)
3757                         return nfs_errtbl[i].errno;
3758         }
3759         if (stat < 0) {
3760                 /* The server is looney tunes. */
3761                 return ESERVERFAULT;
3762         }
3763         /* If we cannot translate the error, the recovery routines should
3764          * handle it.
3765          * Note: remaining NFSv4 error codes have values > 10000, so should
3766          * not conflict with native Linux error codes.
3767          */
3768         return stat;
3769 }
3770
3771 #ifndef MAX
3772 # define MAX(a, b)      (((a) > (b))? (a) : (b))
3773 #endif
3774
3775 #define PROC(proc, argtype, restype)                            \
3776 [NFSPROC4_CLNT_##proc] = {                                      \
3777         .p_proc   = NFSPROC4_COMPOUND,                          \
3778         .p_encode = (kxdrproc_t) nfs4_xdr_##argtype,            \
3779         .p_decode = (kxdrproc_t) nfs4_xdr_##restype,            \
3780         .p_bufsiz = MAX(NFS4_##argtype##_sz,NFS4_##restype##_sz) << 2,  \
3781     }
3782
3783 struct rpc_procinfo     nfs4_procedures[] = {
3784   PROC(READ,            enc_read,       dec_read),
3785   PROC(WRITE,           enc_write,      dec_write),
3786   PROC(COMMIT,          enc_commit,     dec_commit),
3787   PROC(OPEN,            enc_open,       dec_open),
3788   PROC(OPEN_CONFIRM,    enc_open_confirm,       dec_open_confirm),
3789   PROC(OPEN_RECLAIM,    enc_open_reclaim,       dec_open_reclaim),
3790   PROC(OPEN_DOWNGRADE,  enc_open_downgrade,     dec_open_downgrade),
3791   PROC(CLOSE,           enc_close,      dec_close),
3792   PROC(SETATTR,         enc_setattr,    dec_setattr),
3793   PROC(FSINFO,          enc_fsinfo,     dec_fsinfo),
3794   PROC(RENEW,           enc_renew,      dec_renew),
3795   PROC(SETCLIENTID,     enc_setclientid,        dec_setclientid),
3796   PROC(SETCLIENTID_CONFIRM,     enc_setclientid_confirm,        dec_setclientid_confirm),
3797   PROC(LOCK,            enc_lock,       dec_lock),
3798   PROC(LOCKT,           enc_lockt,      dec_lockt),
3799   PROC(LOCKU,           enc_locku,      dec_locku),
3800   PROC(ACCESS,          enc_access,     dec_access),
3801   PROC(GETATTR,         enc_getattr,    dec_getattr),
3802   PROC(LOOKUP,          enc_lookup,     dec_lookup),
3803   PROC(LOOKUP_ROOT,     enc_lookup_root,        dec_lookup_root),
3804   PROC(REMOVE,          enc_remove,     dec_remove),
3805   PROC(RENAME,          enc_rename,     dec_rename),
3806   PROC(LINK,            enc_link,       dec_link),
3807   PROC(CREATE,          enc_create,     dec_create),
3808   PROC(PATHCONF,        enc_pathconf,   dec_pathconf),
3809   PROC(STATFS,          enc_statfs,     dec_statfs),
3810   PROC(READLINK,        enc_readlink,   dec_readlink),
3811   PROC(READDIR,         enc_readdir,    dec_readdir),
3812   PROC(SERVER_CAPS,     enc_server_caps, dec_server_caps),
3813 };
3814
3815 struct rpc_version              nfs_version4 = {
3816         .number                 = 4,
3817         .nrprocs                = sizeof(nfs4_procedures)/sizeof(nfs4_procedures[0]),
3818         .procs                  = nfs4_procedures
3819 };
3820
3821 /*
3822  * Local variables:
3823  *  c-basic-offset: 8
3824  * End:
3825  */