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