NFSv4: Convert LOCK rpc call into an asynchronous RPC call
[linux-2.6/verdex.git] / fs / nfs / nfs4xdr.c
blob5d6bda43dfaa3260066663f16478cc3cced882b2
1 /*
2 * fs/nfs/nfs4xdr.c
4 * Client-side XDR for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
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.
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.
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"
56 #define NFSDBG_FACILITY NFSDBG_XDR
58 /* Mapping from NFS error code to "errno" error code. */
59 #define errno_NFSERR_IO EIO
61 static int nfs_stat_to_errno(int);
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
70 /* lock,open owner id:
71 * we currently use size 1 (u32) out of (NFS4_OPAQUE_LIMIT >> 2)
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_restorefh_maxsz (op_encode_hdr_maxsz)
99 #define decode_restorefh_maxsz (op_decode_hdr_maxsz)
100 #define encode_fsinfo_maxsz (op_encode_hdr_maxsz + 2)
101 #define decode_fsinfo_maxsz (op_decode_hdr_maxsz + 11)
102 #define encode_renew_maxsz (op_encode_hdr_maxsz + 3)
103 #define decode_renew_maxsz (op_decode_hdr_maxsz)
104 #define encode_setclientid_maxsz \
105 (op_encode_hdr_maxsz + \
106 4 /*server->ip_addr*/ + \
107 1 /*Netid*/ + \
108 6 /*uaddr*/ + \
109 6 + (NFS4_VERIFIER_SIZE >> 2))
110 #define decode_setclientid_maxsz \
111 (op_decode_hdr_maxsz + \
112 2 + \
113 1024) /* large value for CLID_INUSE */
114 #define encode_setclientid_confirm_maxsz \
115 (op_encode_hdr_maxsz + \
116 3 + (NFS4_VERIFIER_SIZE >> 2))
117 #define decode_setclientid_confirm_maxsz \
118 (op_decode_hdr_maxsz)
119 #define encode_lookup_maxsz (op_encode_hdr_maxsz + \
120 1 + ((3 + NFS4_FHSIZE) >> 2))
121 #define encode_remove_maxsz (op_encode_hdr_maxsz + \
122 nfs4_name_maxsz)
123 #define encode_rename_maxsz (op_encode_hdr_maxsz + \
124 2 * nfs4_name_maxsz)
125 #define decode_rename_maxsz (op_decode_hdr_maxsz + 5 + 5)
126 #define encode_link_maxsz (op_encode_hdr_maxsz + \
127 nfs4_name_maxsz)
128 #define decode_link_maxsz (op_decode_hdr_maxsz + 5)
129 #define encode_symlink_maxsz (op_encode_hdr_maxsz + \
130 1 + nfs4_name_maxsz + \
131 nfs4_path_maxsz + \
132 nfs4_fattr_maxsz)
133 #define decode_symlink_maxsz (op_decode_hdr_maxsz + 8)
134 #define encode_create_maxsz (op_encode_hdr_maxsz + \
135 2 + nfs4_name_maxsz + \
136 nfs4_fattr_maxsz)
137 #define decode_create_maxsz (op_decode_hdr_maxsz + 8)
138 #define encode_delegreturn_maxsz (op_encode_hdr_maxsz + 4)
139 #define decode_delegreturn_maxsz (op_decode_hdr_maxsz)
140 #define NFS4_enc_compound_sz (1024) /* XXX: large enough? */
141 #define NFS4_dec_compound_sz (1024) /* XXX: large enough? */
142 #define NFS4_enc_read_sz (compound_encode_hdr_maxsz + \
143 encode_putfh_maxsz + \
144 op_encode_hdr_maxsz + 7)
145 #define NFS4_dec_read_sz (compound_decode_hdr_maxsz + \
146 decode_putfh_maxsz + \
147 op_decode_hdr_maxsz + 2)
148 #define NFS4_enc_readlink_sz (compound_encode_hdr_maxsz + \
149 encode_putfh_maxsz + \
150 op_encode_hdr_maxsz)
151 #define NFS4_dec_readlink_sz (compound_decode_hdr_maxsz + \
152 decode_putfh_maxsz + \
153 op_decode_hdr_maxsz)
154 #define NFS4_enc_readdir_sz (compound_encode_hdr_maxsz + \
155 encode_putfh_maxsz + \
156 op_encode_hdr_maxsz + 9)
157 #define NFS4_dec_readdir_sz (compound_decode_hdr_maxsz + \
158 decode_putfh_maxsz + \
159 op_decode_hdr_maxsz + 2)
160 #define NFS4_enc_write_sz (compound_encode_hdr_maxsz + \
161 encode_putfh_maxsz + \
162 op_encode_hdr_maxsz + 8 + \
163 encode_getattr_maxsz)
164 #define NFS4_dec_write_sz (compound_decode_hdr_maxsz + \
165 decode_putfh_maxsz + \
166 op_decode_hdr_maxsz + 4 + \
167 decode_getattr_maxsz)
168 #define NFS4_enc_commit_sz (compound_encode_hdr_maxsz + \
169 encode_putfh_maxsz + \
170 op_encode_hdr_maxsz + 3 + \
171 encode_getattr_maxsz)
172 #define NFS4_dec_commit_sz (compound_decode_hdr_maxsz + \
173 decode_putfh_maxsz + \
174 op_decode_hdr_maxsz + 2 + \
175 decode_getattr_maxsz)
176 #define NFS4_enc_open_sz (compound_encode_hdr_maxsz + \
177 encode_putfh_maxsz + \
178 op_encode_hdr_maxsz + \
179 13 + 3 + 2 + 64 + \
180 encode_getattr_maxsz + \
181 encode_getfh_maxsz)
182 #define NFS4_dec_open_sz (compound_decode_hdr_maxsz + \
183 decode_putfh_maxsz + \
184 op_decode_hdr_maxsz + 4 + 5 + 2 + 3 + \
185 decode_getattr_maxsz + \
186 decode_getfh_maxsz)
187 #define NFS4_enc_open_confirm_sz \
188 (compound_encode_hdr_maxsz + \
189 encode_putfh_maxsz + \
190 op_encode_hdr_maxsz + 5)
191 #define NFS4_dec_open_confirm_sz (compound_decode_hdr_maxsz + \
192 decode_putfh_maxsz + \
193 op_decode_hdr_maxsz + 4)
194 #define NFS4_enc_open_noattr_sz (compound_encode_hdr_maxsz + \
195 encode_putfh_maxsz + \
196 op_encode_hdr_maxsz + \
198 #define NFS4_dec_open_noattr_sz (compound_decode_hdr_maxsz + \
199 decode_putfh_maxsz + \
200 op_decode_hdr_maxsz + \
201 4 + 5 + 2 + 3)
202 #define NFS4_enc_open_downgrade_sz \
203 (compound_encode_hdr_maxsz + \
204 encode_putfh_maxsz + \
205 op_encode_hdr_maxsz + 7 + \
206 encode_getattr_maxsz)
207 #define NFS4_dec_open_downgrade_sz \
208 (compound_decode_hdr_maxsz + \
209 decode_putfh_maxsz + \
210 op_decode_hdr_maxsz + 4 + \
211 decode_getattr_maxsz)
212 #define NFS4_enc_close_sz (compound_encode_hdr_maxsz + \
213 encode_putfh_maxsz + \
214 op_encode_hdr_maxsz + 5 + \
215 encode_getattr_maxsz)
216 #define NFS4_dec_close_sz (compound_decode_hdr_maxsz + \
217 decode_putfh_maxsz + \
218 op_decode_hdr_maxsz + 4 + \
219 decode_getattr_maxsz)
220 #define NFS4_enc_setattr_sz (compound_encode_hdr_maxsz + \
221 encode_putfh_maxsz + \
222 op_encode_hdr_maxsz + 4 + \
223 nfs4_fattr_maxsz + \
224 encode_getattr_maxsz)
225 #define NFS4_dec_setattr_sz (compound_decode_hdr_maxsz + \
226 decode_putfh_maxsz + \
227 op_decode_hdr_maxsz + 3)
228 #define NFS4_enc_fsinfo_sz (compound_encode_hdr_maxsz + \
229 encode_putfh_maxsz + \
230 encode_fsinfo_maxsz)
231 #define NFS4_dec_fsinfo_sz (compound_decode_hdr_maxsz + \
232 decode_putfh_maxsz + \
233 decode_fsinfo_maxsz)
234 #define NFS4_enc_renew_sz (compound_encode_hdr_maxsz + \
235 encode_renew_maxsz)
236 #define NFS4_dec_renew_sz (compound_decode_hdr_maxsz + \
237 decode_renew_maxsz)
238 #define NFS4_enc_setclientid_sz (compound_encode_hdr_maxsz + \
239 encode_setclientid_maxsz)
240 #define NFS4_dec_setclientid_sz (compound_decode_hdr_maxsz + \
241 decode_setclientid_maxsz)
242 #define NFS4_enc_setclientid_confirm_sz \
243 (compound_encode_hdr_maxsz + \
244 encode_setclientid_confirm_maxsz + \
245 encode_putrootfh_maxsz + \
246 encode_fsinfo_maxsz)
247 #define NFS4_dec_setclientid_confirm_sz \
248 (compound_decode_hdr_maxsz + \
249 decode_setclientid_confirm_maxsz + \
250 decode_putrootfh_maxsz + \
251 decode_fsinfo_maxsz)
252 #define NFS4_enc_lock_sz (compound_encode_hdr_maxsz + \
253 encode_putfh_maxsz + \
254 encode_getattr_maxsz + \
255 op_encode_hdr_maxsz + \
256 1 + 1 + 2 + 2 + \
257 1 + 4 + 1 + 2 + \
258 owner_id_maxsz)
259 #define NFS4_dec_lock_sz (compound_decode_hdr_maxsz + \
260 decode_putfh_maxsz + \
261 decode_getattr_maxsz + \
262 op_decode_hdr_maxsz + \
263 2 + 2 + 1 + 2 + \
264 owner_id_maxsz)
265 #define NFS4_enc_lockt_sz (compound_encode_hdr_maxsz + \
266 encode_putfh_maxsz + \
267 encode_getattr_maxsz + \
268 op_encode_hdr_maxsz + \
269 1 + 2 + 2 + 2 + \
270 owner_id_maxsz)
271 #define NFS4_dec_lockt_sz (NFS4_dec_lock_sz)
272 #define NFS4_enc_locku_sz (compound_encode_hdr_maxsz + \
273 encode_putfh_maxsz + \
274 encode_getattr_maxsz + \
275 op_encode_hdr_maxsz + \
276 1 + 1 + 4 + 2 + 2)
277 #define NFS4_dec_locku_sz (compound_decode_hdr_maxsz + \
278 decode_putfh_maxsz + \
279 decode_getattr_maxsz + \
280 op_decode_hdr_maxsz + 4)
281 #define NFS4_enc_access_sz (compound_encode_hdr_maxsz + \
282 encode_putfh_maxsz + \
283 op_encode_hdr_maxsz + 1)
284 #define NFS4_dec_access_sz (compound_decode_hdr_maxsz + \
285 decode_putfh_maxsz + \
286 op_decode_hdr_maxsz + 2)
287 #define NFS4_enc_getattr_sz (compound_encode_hdr_maxsz + \
288 encode_putfh_maxsz + \
289 encode_getattr_maxsz)
290 #define NFS4_dec_getattr_sz (compound_decode_hdr_maxsz + \
291 decode_putfh_maxsz + \
292 decode_getattr_maxsz)
293 #define NFS4_enc_lookup_sz (compound_encode_hdr_maxsz + \
294 encode_putfh_maxsz + \
295 encode_lookup_maxsz + \
296 encode_getattr_maxsz + \
297 encode_getfh_maxsz)
298 #define NFS4_dec_lookup_sz (compound_decode_hdr_maxsz + \
299 decode_putfh_maxsz + \
300 op_decode_hdr_maxsz + \
301 decode_getattr_maxsz + \
302 decode_getfh_maxsz)
303 #define NFS4_enc_lookup_root_sz (compound_encode_hdr_maxsz + \
304 encode_putrootfh_maxsz + \
305 encode_getattr_maxsz + \
306 encode_getfh_maxsz)
307 #define NFS4_dec_lookup_root_sz (compound_decode_hdr_maxsz + \
308 decode_putrootfh_maxsz + \
309 decode_getattr_maxsz + \
310 decode_getfh_maxsz)
311 #define NFS4_enc_remove_sz (compound_encode_hdr_maxsz + \
312 encode_putfh_maxsz + \
313 encode_remove_maxsz + \
314 encode_getattr_maxsz)
315 #define NFS4_dec_remove_sz (compound_decode_hdr_maxsz + \
316 decode_putfh_maxsz + \
317 op_decode_hdr_maxsz + 5 + \
318 decode_getattr_maxsz)
319 #define NFS4_enc_rename_sz (compound_encode_hdr_maxsz + \
320 encode_putfh_maxsz + \
321 encode_savefh_maxsz + \
322 encode_putfh_maxsz + \
323 encode_rename_maxsz + \
324 encode_getattr_maxsz + \
325 encode_restorefh_maxsz + \
326 encode_getattr_maxsz)
327 #define NFS4_dec_rename_sz (compound_decode_hdr_maxsz + \
328 decode_putfh_maxsz + \
329 decode_savefh_maxsz + \
330 decode_putfh_maxsz + \
331 decode_rename_maxsz + \
332 decode_getattr_maxsz + \
333 decode_restorefh_maxsz + \
334 decode_getattr_maxsz)
335 #define NFS4_enc_link_sz (compound_encode_hdr_maxsz + \
336 encode_putfh_maxsz + \
337 encode_savefh_maxsz + \
338 encode_putfh_maxsz + \
339 encode_link_maxsz + \
340 decode_getattr_maxsz + \
341 encode_restorefh_maxsz + \
342 decode_getattr_maxsz)
343 #define NFS4_dec_link_sz (compound_decode_hdr_maxsz + \
344 decode_putfh_maxsz + \
345 decode_savefh_maxsz + \
346 decode_putfh_maxsz + \
347 decode_link_maxsz + \
348 decode_getattr_maxsz + \
349 decode_restorefh_maxsz + \
350 decode_getattr_maxsz)
351 #define NFS4_enc_symlink_sz (compound_encode_hdr_maxsz + \
352 encode_putfh_maxsz + \
353 encode_symlink_maxsz + \
354 encode_getattr_maxsz + \
355 encode_getfh_maxsz)
356 #define NFS4_dec_symlink_sz (compound_decode_hdr_maxsz + \
357 decode_putfh_maxsz + \
358 decode_symlink_maxsz + \
359 decode_getattr_maxsz + \
360 decode_getfh_maxsz)
361 #define NFS4_enc_create_sz (compound_encode_hdr_maxsz + \
362 encode_putfh_maxsz + \
363 encode_savefh_maxsz + \
364 encode_create_maxsz + \
365 encode_getfh_maxsz + \
366 encode_getattr_maxsz + \
367 encode_restorefh_maxsz + \
368 encode_getattr_maxsz)
369 #define NFS4_dec_create_sz (compound_decode_hdr_maxsz + \
370 decode_putfh_maxsz + \
371 decode_savefh_maxsz + \
372 decode_create_maxsz + \
373 decode_getfh_maxsz + \
374 decode_getattr_maxsz + \
375 decode_restorefh_maxsz + \
376 decode_getattr_maxsz)
377 #define NFS4_enc_pathconf_sz (compound_encode_hdr_maxsz + \
378 encode_putfh_maxsz + \
379 encode_getattr_maxsz)
380 #define NFS4_dec_pathconf_sz (compound_decode_hdr_maxsz + \
381 decode_putfh_maxsz + \
382 decode_getattr_maxsz)
383 #define NFS4_enc_statfs_sz (compound_encode_hdr_maxsz + \
384 encode_putfh_maxsz + \
385 encode_getattr_maxsz)
386 #define NFS4_dec_statfs_sz (compound_decode_hdr_maxsz + \
387 decode_putfh_maxsz + \
388 op_decode_hdr_maxsz + 12)
389 #define NFS4_enc_server_caps_sz (compound_encode_hdr_maxsz + \
390 encode_getattr_maxsz)
391 #define NFS4_dec_server_caps_sz (compound_decode_hdr_maxsz + \
392 decode_getattr_maxsz)
393 #define NFS4_enc_delegreturn_sz (compound_encode_hdr_maxsz + \
394 encode_putfh_maxsz + \
395 encode_delegreturn_maxsz)
396 #define NFS4_dec_delegreturn_sz (compound_decode_hdr_maxsz + \
397 decode_delegreturn_maxsz)
398 #define NFS4_enc_getacl_sz (compound_encode_hdr_maxsz + \
399 encode_putfh_maxsz + \
400 encode_getattr_maxsz)
401 #define NFS4_dec_getacl_sz (compound_decode_hdr_maxsz + \
402 decode_putfh_maxsz + \
403 op_decode_hdr_maxsz + \
404 nfs4_fattr_bitmap_maxsz + 1)
405 #define NFS4_enc_setacl_sz (compound_encode_hdr_maxsz + \
406 encode_putfh_maxsz + \
407 op_encode_hdr_maxsz + 4 + \
408 nfs4_fattr_bitmap_maxsz + 1)
409 #define NFS4_dec_setacl_sz (compound_decode_hdr_maxsz + \
410 decode_putfh_maxsz + \
411 op_decode_hdr_maxsz + nfs4_fattr_bitmap_maxsz)
413 static struct {
414 unsigned int mode;
415 unsigned int nfs2type;
416 } nfs_type2fmt[] = {
417 { 0, NFNON },
418 { S_IFREG, NFREG },
419 { S_IFDIR, NFDIR },
420 { S_IFBLK, NFBLK },
421 { S_IFCHR, NFCHR },
422 { S_IFLNK, NFLNK },
423 { S_IFSOCK, NFSOCK },
424 { S_IFIFO, NFFIFO },
425 { 0, NFNON },
426 { 0, NFNON },
429 struct compound_hdr {
430 int32_t status;
431 uint32_t nops;
432 uint32_t taglen;
433 char * tag;
437 * START OF "GENERIC" ENCODE ROUTINES.
438 * These may look a little ugly since they are imported from a "generic"
439 * set of XDR encode/decode routines which are intended to be shared by
440 * all of our NFSv4 implementations (OpenBSD, MacOS X...).
442 * If the pain of reading these is too great, it should be a straightforward
443 * task to translate them into Linux-specific versions which are more
444 * consistent with the style used in NFSv2/v3...
446 #define WRITE32(n) *p++ = htonl(n)
447 #define WRITE64(n) do { \
448 *p++ = htonl((uint32_t)((n) >> 32)); \
449 *p++ = htonl((uint32_t)(n)); \
450 } while (0)
451 #define WRITEMEM(ptr,nbytes) do { \
452 p = xdr_encode_opaque_fixed(p, ptr, nbytes); \
453 } while (0)
455 #define RESERVE_SPACE(nbytes) do { \
456 p = xdr_reserve_space(xdr, nbytes); \
457 if (!p) printk("RESERVE_SPACE(%d) failed in function %s\n", (int) (nbytes), __FUNCTION__); \
458 BUG_ON(!p); \
459 } while (0)
461 static void encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
463 uint32_t *p;
465 p = xdr_reserve_space(xdr, 4 + len);
466 BUG_ON(p == NULL);
467 xdr_encode_opaque(p, str, len);
470 static int encode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
472 uint32_t *p;
474 dprintk("encode_compound: tag=%.*s\n", (int)hdr->taglen, hdr->tag);
475 BUG_ON(hdr->taglen > NFS4_MAXTAGLEN);
476 RESERVE_SPACE(12+(XDR_QUADLEN(hdr->taglen)<<2));
477 WRITE32(hdr->taglen);
478 WRITEMEM(hdr->tag, hdr->taglen);
479 WRITE32(NFS4_MINOR_VERSION);
480 WRITE32(hdr->nops);
481 return 0;
484 static void encode_nfs4_verifier(struct xdr_stream *xdr, const nfs4_verifier *verf)
486 uint32_t *p;
488 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
489 BUG_ON(p == NULL);
490 xdr_encode_opaque_fixed(p, verf->data, NFS4_VERIFIER_SIZE);
493 static int encode_attrs(struct xdr_stream *xdr, const struct iattr *iap, const struct nfs_server *server)
495 char owner_name[IDMAP_NAMESZ];
496 char owner_group[IDMAP_NAMESZ];
497 int owner_namelen = 0;
498 int owner_grouplen = 0;
499 uint32_t *p;
500 uint32_t *q;
501 int len;
502 uint32_t bmval0 = 0;
503 uint32_t bmval1 = 0;
504 int status;
507 * We reserve enough space to write the entire attribute buffer at once.
508 * In the worst-case, this would be
509 * 12(bitmap) + 4(attrlen) + 8(size) + 4(mode) + 4(atime) + 4(mtime)
510 * = 36 bytes, plus any contribution from variable-length fields
511 * such as owner/group.
513 len = 16;
515 /* Sigh */
516 if (iap->ia_valid & ATTR_SIZE)
517 len += 8;
518 if (iap->ia_valid & ATTR_MODE)
519 len += 4;
520 if (iap->ia_valid & ATTR_UID) {
521 owner_namelen = nfs_map_uid_to_name(server->nfs4_state, iap->ia_uid, owner_name);
522 if (owner_namelen < 0) {
523 printk(KERN_WARNING "nfs: couldn't resolve uid %d to string\n",
524 iap->ia_uid);
525 /* XXX */
526 strcpy(owner_name, "nobody");
527 owner_namelen = sizeof("nobody") - 1;
528 /* goto out; */
530 len += 4 + (XDR_QUADLEN(owner_namelen) << 2);
532 if (iap->ia_valid & ATTR_GID) {
533 owner_grouplen = nfs_map_gid_to_group(server->nfs4_state, iap->ia_gid, owner_group);
534 if (owner_grouplen < 0) {
535 printk(KERN_WARNING "nfs4: couldn't resolve gid %d to string\n",
536 iap->ia_gid);
537 strcpy(owner_group, "nobody");
538 owner_grouplen = sizeof("nobody") - 1;
539 /* goto out; */
541 len += 4 + (XDR_QUADLEN(owner_grouplen) << 2);
543 if (iap->ia_valid & ATTR_ATIME_SET)
544 len += 16;
545 else if (iap->ia_valid & ATTR_ATIME)
546 len += 4;
547 if (iap->ia_valid & ATTR_MTIME_SET)
548 len += 16;
549 else if (iap->ia_valid & ATTR_MTIME)
550 len += 4;
551 RESERVE_SPACE(len);
554 * We write the bitmap length now, but leave the bitmap and the attribute
555 * buffer length to be backfilled at the end of this routine.
557 WRITE32(2);
558 q = p;
559 p += 3;
561 if (iap->ia_valid & ATTR_SIZE) {
562 bmval0 |= FATTR4_WORD0_SIZE;
563 WRITE64(iap->ia_size);
565 if (iap->ia_valid & ATTR_MODE) {
566 bmval1 |= FATTR4_WORD1_MODE;
567 WRITE32(iap->ia_mode);
569 if (iap->ia_valid & ATTR_UID) {
570 bmval1 |= FATTR4_WORD1_OWNER;
571 WRITE32(owner_namelen);
572 WRITEMEM(owner_name, owner_namelen);
574 if (iap->ia_valid & ATTR_GID) {
575 bmval1 |= FATTR4_WORD1_OWNER_GROUP;
576 WRITE32(owner_grouplen);
577 WRITEMEM(owner_group, owner_grouplen);
579 if (iap->ia_valid & ATTR_ATIME_SET) {
580 bmval1 |= FATTR4_WORD1_TIME_ACCESS_SET;
581 WRITE32(NFS4_SET_TO_CLIENT_TIME);
582 WRITE32(0);
583 WRITE32(iap->ia_mtime.tv_sec);
584 WRITE32(iap->ia_mtime.tv_nsec);
586 else if (iap->ia_valid & ATTR_ATIME) {
587 bmval1 |= FATTR4_WORD1_TIME_ACCESS_SET;
588 WRITE32(NFS4_SET_TO_SERVER_TIME);
590 if (iap->ia_valid & ATTR_MTIME_SET) {
591 bmval1 |= FATTR4_WORD1_TIME_MODIFY_SET;
592 WRITE32(NFS4_SET_TO_CLIENT_TIME);
593 WRITE32(0);
594 WRITE32(iap->ia_mtime.tv_sec);
595 WRITE32(iap->ia_mtime.tv_nsec);
597 else if (iap->ia_valid & ATTR_MTIME) {
598 bmval1 |= FATTR4_WORD1_TIME_MODIFY_SET;
599 WRITE32(NFS4_SET_TO_SERVER_TIME);
603 * Now we backfill the bitmap and the attribute buffer length.
605 if (len != ((char *)p - (char *)q) + 4) {
606 printk ("encode_attr: Attr length calculation error! %u != %Zu\n",
607 len, ((char *)p - (char *)q) + 4);
608 BUG();
610 len = (char *)p - (char *)q - 12;
611 *q++ = htonl(bmval0);
612 *q++ = htonl(bmval1);
613 *q++ = htonl(len);
615 status = 0;
616 /* out: */
617 return status;
620 static int encode_access(struct xdr_stream *xdr, u32 access)
622 uint32_t *p;
624 RESERVE_SPACE(8);
625 WRITE32(OP_ACCESS);
626 WRITE32(access);
628 return 0;
631 static int encode_close(struct xdr_stream *xdr, const struct nfs_closeargs *arg)
633 uint32_t *p;
635 RESERVE_SPACE(8+sizeof(arg->stateid->data));
636 WRITE32(OP_CLOSE);
637 WRITE32(arg->seqid->sequence->counter);
638 WRITEMEM(arg->stateid->data, sizeof(arg->stateid->data));
640 return 0;
643 static int encode_commit(struct xdr_stream *xdr, const struct nfs_writeargs *args)
645 uint32_t *p;
647 RESERVE_SPACE(16);
648 WRITE32(OP_COMMIT);
649 WRITE64(args->offset);
650 WRITE32(args->count);
652 return 0;
655 static int encode_create(struct xdr_stream *xdr, const struct nfs4_create_arg *create)
657 uint32_t *p;
659 RESERVE_SPACE(8);
660 WRITE32(OP_CREATE);
661 WRITE32(create->ftype);
663 switch (create->ftype) {
664 case NF4LNK:
665 RESERVE_SPACE(4 + create->u.symlink->len);
666 WRITE32(create->u.symlink->len);
667 WRITEMEM(create->u.symlink->name, create->u.symlink->len);
668 break;
670 case NF4BLK: case NF4CHR:
671 RESERVE_SPACE(8);
672 WRITE32(create->u.device.specdata1);
673 WRITE32(create->u.device.specdata2);
674 break;
676 default:
677 break;
680 RESERVE_SPACE(4 + create->name->len);
681 WRITE32(create->name->len);
682 WRITEMEM(create->name->name, create->name->len);
684 return encode_attrs(xdr, create->attrs, create->server);
687 static int encode_getattr_one(struct xdr_stream *xdr, uint32_t bitmap)
689 uint32_t *p;
691 RESERVE_SPACE(12);
692 WRITE32(OP_GETATTR);
693 WRITE32(1);
694 WRITE32(bitmap);
695 return 0;
698 static int encode_getattr_two(struct xdr_stream *xdr, uint32_t bm0, uint32_t bm1)
700 uint32_t *p;
702 RESERVE_SPACE(16);
703 WRITE32(OP_GETATTR);
704 WRITE32(2);
705 WRITE32(bm0);
706 WRITE32(bm1);
707 return 0;
710 static int encode_getfattr(struct xdr_stream *xdr, const u32* bitmask)
712 return encode_getattr_two(xdr,
713 bitmask[0] & nfs4_fattr_bitmap[0],
714 bitmask[1] & nfs4_fattr_bitmap[1]);
717 static int encode_fsinfo(struct xdr_stream *xdr, const u32* bitmask)
719 return encode_getattr_two(xdr, bitmask[0] & nfs4_fsinfo_bitmap[0],
720 bitmask[1] & nfs4_fsinfo_bitmap[1]);
723 static int encode_getfh(struct xdr_stream *xdr)
725 uint32_t *p;
727 RESERVE_SPACE(4);
728 WRITE32(OP_GETFH);
730 return 0;
733 static int encode_link(struct xdr_stream *xdr, const struct qstr *name)
735 uint32_t *p;
737 RESERVE_SPACE(8 + name->len);
738 WRITE32(OP_LINK);
739 WRITE32(name->len);
740 WRITEMEM(name->name, name->len);
742 return 0;
745 static inline int nfs4_lock_type(struct file_lock *fl, int block)
747 if ((fl->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) == F_RDLCK)
748 return block ? NFS4_READW_LT : NFS4_READ_LT;
749 return block ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
752 static inline uint64_t nfs4_lock_length(struct file_lock *fl)
754 if (fl->fl_end == OFFSET_MAX)
755 return ~(uint64_t)0;
756 return fl->fl_end - fl->fl_start + 1;
760 * opcode,type,reclaim,offset,length,new_lock_owner = 32
761 * open_seqid,open_stateid,lock_seqid,lock_owner.clientid, lock_owner.id = 40
763 static int encode_lock(struct xdr_stream *xdr, const struct nfs_lock_args *args)
765 uint32_t *p;
767 RESERVE_SPACE(32);
768 WRITE32(OP_LOCK);
769 WRITE32(nfs4_lock_type(args->fl, args->block));
770 WRITE32(args->reclaim);
771 WRITE64(args->fl->fl_start);
772 WRITE64(nfs4_lock_length(args->fl));
773 WRITE32(args->new_lock_owner);
774 if (args->new_lock_owner){
775 RESERVE_SPACE(40);
776 WRITE32(args->open_seqid->sequence->counter);
777 WRITEMEM(args->open_stateid->data, sizeof(args->open_stateid->data));
778 WRITE32(args->lock_seqid->sequence->counter);
779 WRITE64(args->lock_owner.clientid);
780 WRITE32(4);
781 WRITE32(args->lock_owner.id);
783 else {
784 RESERVE_SPACE(20);
785 WRITEMEM(args->lock_stateid->data, sizeof(args->lock_stateid->data));
786 WRITE32(args->lock_seqid->sequence->counter);
789 return 0;
792 static int encode_lockt(struct xdr_stream *xdr, const struct nfs_lockt_args *args)
794 uint32_t *p;
796 RESERVE_SPACE(40);
797 WRITE32(OP_LOCKT);
798 WRITE32(nfs4_lock_type(args->fl, 0));
799 WRITE64(args->fl->fl_start);
800 WRITE64(nfs4_lock_length(args->fl));
801 WRITE64(args->lock_owner.clientid);
802 WRITE32(4);
803 WRITE32(args->lock_owner.id);
805 return 0;
808 static int encode_locku(struct xdr_stream *xdr, const struct nfs_locku_args *args)
810 uint32_t *p;
812 RESERVE_SPACE(44);
813 WRITE32(OP_LOCKU);
814 WRITE32(nfs4_lock_type(args->fl, 0));
815 WRITE32(args->seqid->sequence->counter);
816 WRITEMEM(args->stateid->data, sizeof(args->stateid->data));
817 WRITE64(args->fl->fl_start);
818 WRITE64(nfs4_lock_length(args->fl));
820 return 0;
823 static int encode_lookup(struct xdr_stream *xdr, const struct qstr *name)
825 int len = name->len;
826 uint32_t *p;
828 RESERVE_SPACE(8 + len);
829 WRITE32(OP_LOOKUP);
830 WRITE32(len);
831 WRITEMEM(name->name, len);
833 return 0;
836 static void encode_share_access(struct xdr_stream *xdr, int open_flags)
838 uint32_t *p;
840 RESERVE_SPACE(8);
841 switch (open_flags & (FMODE_READ|FMODE_WRITE)) {
842 case FMODE_READ:
843 WRITE32(NFS4_SHARE_ACCESS_READ);
844 break;
845 case FMODE_WRITE:
846 WRITE32(NFS4_SHARE_ACCESS_WRITE);
847 break;
848 case FMODE_READ|FMODE_WRITE:
849 WRITE32(NFS4_SHARE_ACCESS_BOTH);
850 break;
851 default:
852 BUG();
854 WRITE32(0); /* for linux, share_deny = 0 always */
857 static inline void encode_openhdr(struct xdr_stream *xdr, const struct nfs_openargs *arg)
859 uint32_t *p;
861 * opcode 4, seqid 4, share_access 4, share_deny 4, clientid 8, ownerlen 4,
862 * owner 4 = 32
864 RESERVE_SPACE(8);
865 WRITE32(OP_OPEN);
866 WRITE32(arg->seqid->sequence->counter);
867 encode_share_access(xdr, arg->open_flags);
868 RESERVE_SPACE(16);
869 WRITE64(arg->clientid);
870 WRITE32(4);
871 WRITE32(arg->id);
874 static inline void encode_createmode(struct xdr_stream *xdr, const struct nfs_openargs *arg)
876 uint32_t *p;
878 RESERVE_SPACE(4);
879 switch(arg->open_flags & O_EXCL) {
880 case 0:
881 WRITE32(NFS4_CREATE_UNCHECKED);
882 encode_attrs(xdr, arg->u.attrs, arg->server);
883 break;
884 default:
885 WRITE32(NFS4_CREATE_EXCLUSIVE);
886 encode_nfs4_verifier(xdr, &arg->u.verifier);
890 static void encode_opentype(struct xdr_stream *xdr, const struct nfs_openargs *arg)
892 uint32_t *p;
894 RESERVE_SPACE(4);
895 switch (arg->open_flags & O_CREAT) {
896 case 0:
897 WRITE32(NFS4_OPEN_NOCREATE);
898 break;
899 default:
900 BUG_ON(arg->claim != NFS4_OPEN_CLAIM_NULL);
901 WRITE32(NFS4_OPEN_CREATE);
902 encode_createmode(xdr, arg);
906 static inline void encode_delegation_type(struct xdr_stream *xdr, int delegation_type)
908 uint32_t *p;
910 RESERVE_SPACE(4);
911 switch (delegation_type) {
912 case 0:
913 WRITE32(NFS4_OPEN_DELEGATE_NONE);
914 break;
915 case FMODE_READ:
916 WRITE32(NFS4_OPEN_DELEGATE_READ);
917 break;
918 case FMODE_WRITE|FMODE_READ:
919 WRITE32(NFS4_OPEN_DELEGATE_WRITE);
920 break;
921 default:
922 BUG();
926 static inline void encode_claim_null(struct xdr_stream *xdr, const struct qstr *name)
928 uint32_t *p;
930 RESERVE_SPACE(4);
931 WRITE32(NFS4_OPEN_CLAIM_NULL);
932 encode_string(xdr, name->len, name->name);
935 static inline void encode_claim_previous(struct xdr_stream *xdr, int type)
937 uint32_t *p;
939 RESERVE_SPACE(4);
940 WRITE32(NFS4_OPEN_CLAIM_PREVIOUS);
941 encode_delegation_type(xdr, type);
944 static inline void encode_claim_delegate_cur(struct xdr_stream *xdr, const struct qstr *name, const nfs4_stateid *stateid)
946 uint32_t *p;
948 RESERVE_SPACE(4+sizeof(stateid->data));
949 WRITE32(NFS4_OPEN_CLAIM_DELEGATE_CUR);
950 WRITEMEM(stateid->data, sizeof(stateid->data));
951 encode_string(xdr, name->len, name->name);
954 static int encode_open(struct xdr_stream *xdr, const struct nfs_openargs *arg)
956 encode_openhdr(xdr, arg);
957 encode_opentype(xdr, arg);
958 switch (arg->claim) {
959 case NFS4_OPEN_CLAIM_NULL:
960 encode_claim_null(xdr, arg->name);
961 break;
962 case NFS4_OPEN_CLAIM_PREVIOUS:
963 encode_claim_previous(xdr, arg->u.delegation_type);
964 break;
965 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
966 encode_claim_delegate_cur(xdr, arg->name, &arg->u.delegation);
967 break;
968 default:
969 BUG();
971 return 0;
974 static int encode_open_confirm(struct xdr_stream *xdr, const struct nfs_open_confirmargs *arg)
976 uint32_t *p;
978 RESERVE_SPACE(8+sizeof(arg->stateid->data));
979 WRITE32(OP_OPEN_CONFIRM);
980 WRITEMEM(arg->stateid->data, sizeof(arg->stateid->data));
981 WRITE32(arg->seqid->sequence->counter);
983 return 0;
986 static int encode_open_downgrade(struct xdr_stream *xdr, const struct nfs_closeargs *arg)
988 uint32_t *p;
990 RESERVE_SPACE(8+sizeof(arg->stateid->data));
991 WRITE32(OP_OPEN_DOWNGRADE);
992 WRITEMEM(arg->stateid->data, sizeof(arg->stateid->data));
993 WRITE32(arg->seqid->sequence->counter);
994 encode_share_access(xdr, arg->open_flags);
995 return 0;
998 static int
999 encode_putfh(struct xdr_stream *xdr, const struct nfs_fh *fh)
1001 int len = fh->size;
1002 uint32_t *p;
1004 RESERVE_SPACE(8 + len);
1005 WRITE32(OP_PUTFH);
1006 WRITE32(len);
1007 WRITEMEM(fh->data, len);
1009 return 0;
1012 static int encode_putrootfh(struct xdr_stream *xdr)
1014 uint32_t *p;
1016 RESERVE_SPACE(4);
1017 WRITE32(OP_PUTROOTFH);
1019 return 0;
1022 static void encode_stateid(struct xdr_stream *xdr, const struct nfs_open_context *ctx)
1024 nfs4_stateid stateid;
1025 uint32_t *p;
1027 RESERVE_SPACE(16);
1028 if (ctx->state != NULL) {
1029 nfs4_copy_stateid(&stateid, ctx->state, ctx->lockowner);
1030 WRITEMEM(stateid.data, sizeof(stateid.data));
1031 } else
1032 WRITEMEM(zero_stateid.data, sizeof(zero_stateid.data));
1035 static int encode_read(struct xdr_stream *xdr, const struct nfs_readargs *args)
1037 uint32_t *p;
1039 RESERVE_SPACE(4);
1040 WRITE32(OP_READ);
1042 encode_stateid(xdr, args->context);
1044 RESERVE_SPACE(12);
1045 WRITE64(args->offset);
1046 WRITE32(args->count);
1048 return 0;
1051 static int encode_readdir(struct xdr_stream *xdr, const struct nfs4_readdir_arg *readdir, struct rpc_rqst *req)
1053 struct rpc_auth *auth = req->rq_task->tk_auth;
1054 uint32_t attrs[2] = {
1055 FATTR4_WORD0_RDATTR_ERROR|FATTR4_WORD0_FILEID,
1056 FATTR4_WORD1_MOUNTED_ON_FILEID,
1058 int replen;
1059 uint32_t *p;
1061 RESERVE_SPACE(32+sizeof(nfs4_verifier));
1062 WRITE32(OP_READDIR);
1063 WRITE64(readdir->cookie);
1064 WRITEMEM(readdir->verifier.data, sizeof(readdir->verifier.data));
1065 WRITE32(readdir->count >> 1); /* We're not doing readdirplus */
1066 WRITE32(readdir->count);
1067 WRITE32(2);
1068 /* Switch to mounted_on_fileid if the server supports it */
1069 if (readdir->bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
1070 attrs[0] &= ~FATTR4_WORD0_FILEID;
1071 else
1072 attrs[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
1073 WRITE32(attrs[0] & readdir->bitmask[0]);
1074 WRITE32(attrs[1] & readdir->bitmask[1]);
1075 dprintk("%s: cookie = %Lu, verifier = 0x%x%x, bitmap = 0x%x%x\n",
1076 __FUNCTION__,
1077 (unsigned long long)readdir->cookie,
1078 ((u32 *)readdir->verifier.data)[0],
1079 ((u32 *)readdir->verifier.data)[1],
1080 attrs[0] & readdir->bitmask[0],
1081 attrs[1] & readdir->bitmask[1]);
1083 /* set up reply kvec
1084 * toplevel_status + taglen + rescount + OP_PUTFH + status
1085 * + OP_READDIR + status + verifer(2) = 9
1087 replen = (RPC_REPHDRSIZE + auth->au_rslack + 9) << 2;
1088 xdr_inline_pages(&req->rq_rcv_buf, replen, readdir->pages,
1089 readdir->pgbase, readdir->count);
1090 dprintk("%s: inlined page args = (%u, %p, %u, %u)\n",
1091 __FUNCTION__, replen, readdir->pages,
1092 readdir->pgbase, readdir->count);
1094 return 0;
1097 static int encode_readlink(struct xdr_stream *xdr, const struct nfs4_readlink *readlink, struct rpc_rqst *req)
1099 struct rpc_auth *auth = req->rq_task->tk_auth;
1100 unsigned int replen;
1101 uint32_t *p;
1103 RESERVE_SPACE(4);
1104 WRITE32(OP_READLINK);
1106 /* set up reply kvec
1107 * toplevel_status + taglen + rescount + OP_PUTFH + status
1108 * + OP_READLINK + status + string length = 8
1110 replen = (RPC_REPHDRSIZE + auth->au_rslack + 8) << 2;
1111 xdr_inline_pages(&req->rq_rcv_buf, replen, readlink->pages,
1112 readlink->pgbase, readlink->pglen);
1114 return 0;
1117 static int encode_remove(struct xdr_stream *xdr, const struct qstr *name)
1119 uint32_t *p;
1121 RESERVE_SPACE(8 + name->len);
1122 WRITE32(OP_REMOVE);
1123 WRITE32(name->len);
1124 WRITEMEM(name->name, name->len);
1126 return 0;
1129 static int encode_rename(struct xdr_stream *xdr, const struct qstr *oldname, const struct qstr *newname)
1131 uint32_t *p;
1133 RESERVE_SPACE(8 + oldname->len);
1134 WRITE32(OP_RENAME);
1135 WRITE32(oldname->len);
1136 WRITEMEM(oldname->name, oldname->len);
1138 RESERVE_SPACE(4 + newname->len);
1139 WRITE32(newname->len);
1140 WRITEMEM(newname->name, newname->len);
1142 return 0;
1145 static int encode_renew(struct xdr_stream *xdr, const struct nfs4_client *client_stateid)
1147 uint32_t *p;
1149 RESERVE_SPACE(12);
1150 WRITE32(OP_RENEW);
1151 WRITE64(client_stateid->cl_clientid);
1153 return 0;
1156 static int
1157 encode_restorefh(struct xdr_stream *xdr)
1159 uint32_t *p;
1161 RESERVE_SPACE(4);
1162 WRITE32(OP_RESTOREFH);
1164 return 0;
1167 static int
1168 encode_setacl(struct xdr_stream *xdr, struct nfs_setaclargs *arg)
1170 uint32_t *p;
1172 RESERVE_SPACE(4+sizeof(zero_stateid.data));
1173 WRITE32(OP_SETATTR);
1174 WRITEMEM(zero_stateid.data, sizeof(zero_stateid.data));
1175 RESERVE_SPACE(2*4);
1176 WRITE32(1);
1177 WRITE32(FATTR4_WORD0_ACL);
1178 if (arg->acl_len % 4)
1179 return -EINVAL;
1180 RESERVE_SPACE(4);
1181 WRITE32(arg->acl_len);
1182 xdr_write_pages(xdr, arg->acl_pages, arg->acl_pgbase, arg->acl_len);
1183 return 0;
1186 static int
1187 encode_savefh(struct xdr_stream *xdr)
1189 uint32_t *p;
1191 RESERVE_SPACE(4);
1192 WRITE32(OP_SAVEFH);
1194 return 0;
1197 static int encode_setattr(struct xdr_stream *xdr, const struct nfs_setattrargs *arg, const struct nfs_server *server)
1199 int status;
1200 uint32_t *p;
1202 RESERVE_SPACE(4+sizeof(arg->stateid.data));
1203 WRITE32(OP_SETATTR);
1204 WRITEMEM(arg->stateid.data, sizeof(arg->stateid.data));
1206 if ((status = encode_attrs(xdr, arg->iap, server)))
1207 return status;
1209 return 0;
1212 static int encode_setclientid(struct xdr_stream *xdr, const struct nfs4_setclientid *setclientid)
1214 uint32_t *p;
1216 RESERVE_SPACE(4 + sizeof(setclientid->sc_verifier->data));
1217 WRITE32(OP_SETCLIENTID);
1218 WRITEMEM(setclientid->sc_verifier->data, sizeof(setclientid->sc_verifier->data));
1220 encode_string(xdr, setclientid->sc_name_len, setclientid->sc_name);
1221 RESERVE_SPACE(4);
1222 WRITE32(setclientid->sc_prog);
1223 encode_string(xdr, setclientid->sc_netid_len, setclientid->sc_netid);
1224 encode_string(xdr, setclientid->sc_uaddr_len, setclientid->sc_uaddr);
1225 RESERVE_SPACE(4);
1226 WRITE32(setclientid->sc_cb_ident);
1228 return 0;
1231 static int encode_setclientid_confirm(struct xdr_stream *xdr, const struct nfs4_client *client_state)
1233 uint32_t *p;
1235 RESERVE_SPACE(12 + sizeof(client_state->cl_confirm.data));
1236 WRITE32(OP_SETCLIENTID_CONFIRM);
1237 WRITE64(client_state->cl_clientid);
1238 WRITEMEM(client_state->cl_confirm.data, sizeof(client_state->cl_confirm.data));
1240 return 0;
1243 static int encode_write(struct xdr_stream *xdr, const struct nfs_writeargs *args)
1245 uint32_t *p;
1247 RESERVE_SPACE(4);
1248 WRITE32(OP_WRITE);
1250 encode_stateid(xdr, args->context);
1252 RESERVE_SPACE(16);
1253 WRITE64(args->offset);
1254 WRITE32(args->stable);
1255 WRITE32(args->count);
1257 xdr_write_pages(xdr, args->pages, args->pgbase, args->count);
1259 return 0;
1262 static int encode_delegreturn(struct xdr_stream *xdr, const nfs4_stateid *stateid)
1264 uint32_t *p;
1266 RESERVE_SPACE(20);
1268 WRITE32(OP_DELEGRETURN);
1269 WRITEMEM(stateid->data, sizeof(stateid->data));
1270 return 0;
1274 * END OF "GENERIC" ENCODE ROUTINES.
1278 * Encode an ACCESS request
1280 static int nfs4_xdr_enc_access(struct rpc_rqst *req, uint32_t *p, const struct nfs4_accessargs *args)
1282 struct xdr_stream xdr;
1283 struct compound_hdr hdr = {
1284 .nops = 2,
1286 int status;
1288 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1289 encode_compound_hdr(&xdr, &hdr);
1290 if ((status = encode_putfh(&xdr, args->fh)) == 0)
1291 status = encode_access(&xdr, args->access);
1292 return status;
1296 * Encode LOOKUP request
1298 static int nfs4_xdr_enc_lookup(struct rpc_rqst *req, uint32_t *p, const struct nfs4_lookup_arg *args)
1300 struct xdr_stream xdr;
1301 struct compound_hdr hdr = {
1302 .nops = 4,
1304 int status;
1306 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1307 encode_compound_hdr(&xdr, &hdr);
1308 if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1309 goto out;
1310 if ((status = encode_lookup(&xdr, args->name)) != 0)
1311 goto out;
1312 if ((status = encode_getfh(&xdr)) != 0)
1313 goto out;
1314 status = encode_getfattr(&xdr, args->bitmask);
1315 out:
1316 return status;
1320 * Encode LOOKUP_ROOT request
1322 static int nfs4_xdr_enc_lookup_root(struct rpc_rqst *req, uint32_t *p, const struct nfs4_lookup_root_arg *args)
1324 struct xdr_stream xdr;
1325 struct compound_hdr hdr = {
1326 .nops = 3,
1328 int status;
1330 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1331 encode_compound_hdr(&xdr, &hdr);
1332 if ((status = encode_putrootfh(&xdr)) != 0)
1333 goto out;
1334 if ((status = encode_getfh(&xdr)) == 0)
1335 status = encode_getfattr(&xdr, args->bitmask);
1336 out:
1337 return status;
1341 * Encode REMOVE request
1343 static int nfs4_xdr_enc_remove(struct rpc_rqst *req, uint32_t *p, const struct nfs4_remove_arg *args)
1345 struct xdr_stream xdr;
1346 struct compound_hdr hdr = {
1347 .nops = 3,
1349 int status;
1351 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1352 encode_compound_hdr(&xdr, &hdr);
1353 if ((status = encode_putfh(&xdr, args->fh)) != 0)
1354 goto out;
1355 if ((status = encode_remove(&xdr, args->name)) != 0)
1356 goto out;
1357 status = encode_getfattr(&xdr, args->bitmask);
1358 out:
1359 return status;
1363 * Encode RENAME request
1365 static int nfs4_xdr_enc_rename(struct rpc_rqst *req, uint32_t *p, const struct nfs4_rename_arg *args)
1367 struct xdr_stream xdr;
1368 struct compound_hdr hdr = {
1369 .nops = 7,
1371 int status;
1373 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1374 encode_compound_hdr(&xdr, &hdr);
1375 if ((status = encode_putfh(&xdr, args->old_dir)) != 0)
1376 goto out;
1377 if ((status = encode_savefh(&xdr)) != 0)
1378 goto out;
1379 if ((status = encode_putfh(&xdr, args->new_dir)) != 0)
1380 goto out;
1381 if ((status = encode_rename(&xdr, args->old_name, args->new_name)) != 0)
1382 goto out;
1383 if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
1384 goto out;
1385 if ((status = encode_restorefh(&xdr)) != 0)
1386 goto out;
1387 status = encode_getfattr(&xdr, args->bitmask);
1388 out:
1389 return status;
1393 * Encode LINK request
1395 static int nfs4_xdr_enc_link(struct rpc_rqst *req, uint32_t *p, const struct nfs4_link_arg *args)
1397 struct xdr_stream xdr;
1398 struct compound_hdr hdr = {
1399 .nops = 7,
1401 int status;
1403 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1404 encode_compound_hdr(&xdr, &hdr);
1405 if ((status = encode_putfh(&xdr, args->fh)) != 0)
1406 goto out;
1407 if ((status = encode_savefh(&xdr)) != 0)
1408 goto out;
1409 if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1410 goto out;
1411 if ((status = encode_link(&xdr, args->name)) != 0)
1412 goto out;
1413 if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
1414 goto out;
1415 if ((status = encode_restorefh(&xdr)) != 0)
1416 goto out;
1417 status = encode_getfattr(&xdr, args->bitmask);
1418 out:
1419 return status;
1423 * Encode CREATE request
1425 static int nfs4_xdr_enc_create(struct rpc_rqst *req, uint32_t *p, const struct nfs4_create_arg *args)
1427 struct xdr_stream xdr;
1428 struct compound_hdr hdr = {
1429 .nops = 7,
1431 int status;
1433 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1434 encode_compound_hdr(&xdr, &hdr);
1435 if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1436 goto out;
1437 if ((status = encode_savefh(&xdr)) != 0)
1438 goto out;
1439 if ((status = encode_create(&xdr, args)) != 0)
1440 goto out;
1441 if ((status = encode_getfh(&xdr)) != 0)
1442 goto out;
1443 if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
1444 goto out;
1445 if ((status = encode_restorefh(&xdr)) != 0)
1446 goto out;
1447 status = encode_getfattr(&xdr, args->bitmask);
1448 out:
1449 return status;
1453 * Encode SYMLINK request
1455 static int nfs4_xdr_enc_symlink(struct rpc_rqst *req, uint32_t *p, const struct nfs4_create_arg *args)
1457 return nfs4_xdr_enc_create(req, p, args);
1461 * Encode GETATTR request
1463 static int nfs4_xdr_enc_getattr(struct rpc_rqst *req, uint32_t *p, const struct nfs4_getattr_arg *args)
1465 struct xdr_stream xdr;
1466 struct compound_hdr hdr = {
1467 .nops = 2,
1469 int status;
1471 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1472 encode_compound_hdr(&xdr, &hdr);
1473 if ((status = encode_putfh(&xdr, args->fh)) == 0)
1474 status = encode_getfattr(&xdr, args->bitmask);
1475 return status;
1479 * Encode a CLOSE request
1481 static int nfs4_xdr_enc_close(struct rpc_rqst *req, uint32_t *p, struct nfs_closeargs *args)
1483 struct xdr_stream xdr;
1484 struct compound_hdr hdr = {
1485 .nops = 3,
1487 int status;
1489 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1490 encode_compound_hdr(&xdr, &hdr);
1491 status = encode_putfh(&xdr, args->fh);
1492 if(status)
1493 goto out;
1494 status = encode_close(&xdr, args);
1495 if (status != 0)
1496 goto out;
1497 status = encode_getfattr(&xdr, args->bitmask);
1498 out:
1499 return status;
1503 * Encode an OPEN request
1505 static int nfs4_xdr_enc_open(struct rpc_rqst *req, uint32_t *p, struct nfs_openargs *args)
1507 struct xdr_stream xdr;
1508 struct compound_hdr hdr = {
1509 .nops = 7,
1511 int status;
1513 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1514 encode_compound_hdr(&xdr, &hdr);
1515 status = encode_putfh(&xdr, args->fh);
1516 if (status)
1517 goto out;
1518 status = encode_savefh(&xdr);
1519 if (status)
1520 goto out;
1521 status = encode_open(&xdr, args);
1522 if (status)
1523 goto out;
1524 status = encode_getfh(&xdr);
1525 if (status)
1526 goto out;
1527 status = encode_getfattr(&xdr, args->bitmask);
1528 if (status)
1529 goto out;
1530 status = encode_restorefh(&xdr);
1531 if (status)
1532 goto out;
1533 status = encode_getfattr(&xdr, args->bitmask);
1534 out:
1535 return status;
1539 * Encode an OPEN_CONFIRM request
1541 static int nfs4_xdr_enc_open_confirm(struct rpc_rqst *req, uint32_t *p, struct nfs_open_confirmargs *args)
1543 struct xdr_stream xdr;
1544 struct compound_hdr hdr = {
1545 .nops = 2,
1547 int status;
1549 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1550 encode_compound_hdr(&xdr, &hdr);
1551 status = encode_putfh(&xdr, args->fh);
1552 if(status)
1553 goto out;
1554 status = encode_open_confirm(&xdr, args);
1555 out:
1556 return status;
1560 * Encode an OPEN request with no attributes.
1562 static int nfs4_xdr_enc_open_noattr(struct rpc_rqst *req, uint32_t *p, struct nfs_openargs *args)
1564 struct xdr_stream xdr;
1565 struct compound_hdr hdr = {
1566 .nops = 3,
1568 int status;
1570 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1571 encode_compound_hdr(&xdr, &hdr);
1572 status = encode_putfh(&xdr, args->fh);
1573 if (status)
1574 goto out;
1575 status = encode_open(&xdr, args);
1576 if (status)
1577 goto out;
1578 status = encode_getfattr(&xdr, args->bitmask);
1579 out:
1580 return status;
1584 * Encode an OPEN_DOWNGRADE request
1586 static int nfs4_xdr_enc_open_downgrade(struct rpc_rqst *req, uint32_t *p, struct nfs_closeargs *args)
1588 struct xdr_stream xdr;
1589 struct compound_hdr hdr = {
1590 .nops = 3,
1592 int status;
1594 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1595 encode_compound_hdr(&xdr, &hdr);
1596 status = encode_putfh(&xdr, args->fh);
1597 if (status)
1598 goto out;
1599 status = encode_open_downgrade(&xdr, args);
1600 if (status != 0)
1601 goto out;
1602 status = encode_getfattr(&xdr, args->bitmask);
1603 out:
1604 return status;
1608 * Encode a LOCK request
1610 static int nfs4_xdr_enc_lock(struct rpc_rqst *req, uint32_t *p, struct nfs_lock_args *args)
1612 struct xdr_stream xdr;
1613 struct compound_hdr hdr = {
1614 .nops = 2,
1616 int status;
1618 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1619 encode_compound_hdr(&xdr, &hdr);
1620 status = encode_putfh(&xdr, args->fh);
1621 if(status)
1622 goto out;
1623 status = encode_lock(&xdr, args);
1624 out:
1625 return status;
1629 * Encode a LOCKT request
1631 static int nfs4_xdr_enc_lockt(struct rpc_rqst *req, uint32_t *p, struct nfs_lockt_args *args)
1633 struct xdr_stream xdr;
1634 struct compound_hdr hdr = {
1635 .nops = 2,
1637 int status;
1639 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1640 encode_compound_hdr(&xdr, &hdr);
1641 status = encode_putfh(&xdr, args->fh);
1642 if(status)
1643 goto out;
1644 status = encode_lockt(&xdr, args);
1645 out:
1646 return status;
1650 * Encode a LOCKU request
1652 static int nfs4_xdr_enc_locku(struct rpc_rqst *req, uint32_t *p, struct nfs_locku_args *args)
1654 struct xdr_stream xdr;
1655 struct compound_hdr hdr = {
1656 .nops = 2,
1658 int status;
1660 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1661 encode_compound_hdr(&xdr, &hdr);
1662 status = encode_putfh(&xdr, args->fh);
1663 if(status)
1664 goto out;
1665 status = encode_locku(&xdr, args);
1666 out:
1667 return status;
1671 * Encode a READLINK request
1673 static int nfs4_xdr_enc_readlink(struct rpc_rqst *req, uint32_t *p, const struct nfs4_readlink *args)
1675 struct xdr_stream xdr;
1676 struct compound_hdr hdr = {
1677 .nops = 2,
1679 int status;
1681 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1682 encode_compound_hdr(&xdr, &hdr);
1683 status = encode_putfh(&xdr, args->fh);
1684 if(status)
1685 goto out;
1686 status = encode_readlink(&xdr, args, req);
1687 out:
1688 return status;
1692 * Encode a READDIR request
1694 static int nfs4_xdr_enc_readdir(struct rpc_rqst *req, uint32_t *p, const struct nfs4_readdir_arg *args)
1696 struct xdr_stream xdr;
1697 struct compound_hdr hdr = {
1698 .nops = 2,
1700 int status;
1702 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1703 encode_compound_hdr(&xdr, &hdr);
1704 status = encode_putfh(&xdr, args->fh);
1705 if(status)
1706 goto out;
1707 status = encode_readdir(&xdr, args, req);
1708 out:
1709 return status;
1713 * Encode a READ request
1715 static int nfs4_xdr_enc_read(struct rpc_rqst *req, uint32_t *p, struct nfs_readargs *args)
1717 struct rpc_auth *auth = req->rq_task->tk_auth;
1718 struct xdr_stream xdr;
1719 struct compound_hdr hdr = {
1720 .nops = 2,
1722 int replen, status;
1724 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1725 encode_compound_hdr(&xdr, &hdr);
1726 status = encode_putfh(&xdr, args->fh);
1727 if (status)
1728 goto out;
1729 status = encode_read(&xdr, args);
1730 if (status)
1731 goto out;
1733 /* set up reply kvec
1734 * toplevel status + taglen=0 + rescount + OP_PUTFH + status
1735 * + OP_READ + status + eof + datalen = 9
1737 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_read_sz) << 2;
1738 xdr_inline_pages(&req->rq_rcv_buf, replen,
1739 args->pages, args->pgbase, args->count);
1740 out:
1741 return status;
1745 * Encode an SETATTR request
1747 static int nfs4_xdr_enc_setattr(struct rpc_rqst *req, uint32_t *p, struct nfs_setattrargs *args)
1750 struct xdr_stream xdr;
1751 struct compound_hdr hdr = {
1752 .nops = 3,
1754 int status;
1756 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1757 encode_compound_hdr(&xdr, &hdr);
1758 status = encode_putfh(&xdr, args->fh);
1759 if(status)
1760 goto out;
1761 status = encode_setattr(&xdr, args, args->server);
1762 if(status)
1763 goto out;
1764 status = encode_getfattr(&xdr, args->bitmask);
1765 out:
1766 return status;
1770 * Encode a GETACL request
1772 static int
1773 nfs4_xdr_enc_getacl(struct rpc_rqst *req, uint32_t *p,
1774 struct nfs_getaclargs *args)
1776 struct xdr_stream xdr;
1777 struct rpc_auth *auth = req->rq_task->tk_auth;
1778 struct compound_hdr hdr = {
1779 .nops = 2,
1781 int replen, status;
1783 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1784 encode_compound_hdr(&xdr, &hdr);
1785 status = encode_putfh(&xdr, args->fh);
1786 if (status)
1787 goto out;
1788 status = encode_getattr_two(&xdr, FATTR4_WORD0_ACL, 0);
1789 /* set up reply buffer: */
1790 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_getacl_sz) << 2;
1791 xdr_inline_pages(&req->rq_rcv_buf, replen,
1792 args->acl_pages, args->acl_pgbase, args->acl_len);
1793 out:
1794 return status;
1798 * Encode a WRITE request
1800 static int nfs4_xdr_enc_write(struct rpc_rqst *req, uint32_t *p, struct nfs_writeargs *args)
1802 struct xdr_stream xdr;
1803 struct compound_hdr hdr = {
1804 .nops = 3,
1806 int status;
1808 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1809 encode_compound_hdr(&xdr, &hdr);
1810 status = encode_putfh(&xdr, args->fh);
1811 if (status)
1812 goto out;
1813 status = encode_write(&xdr, args);
1814 if (status)
1815 goto out;
1816 status = encode_getfattr(&xdr, args->bitmask);
1817 out:
1818 return status;
1822 * a COMMIT request
1824 static int nfs4_xdr_enc_commit(struct rpc_rqst *req, uint32_t *p, struct nfs_writeargs *args)
1826 struct xdr_stream xdr;
1827 struct compound_hdr hdr = {
1828 .nops = 3,
1830 int status;
1832 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1833 encode_compound_hdr(&xdr, &hdr);
1834 status = encode_putfh(&xdr, args->fh);
1835 if (status)
1836 goto out;
1837 status = encode_commit(&xdr, args);
1838 if (status)
1839 goto out;
1840 status = encode_getfattr(&xdr, args->bitmask);
1841 out:
1842 return status;
1846 * FSINFO request
1848 static int nfs4_xdr_enc_fsinfo(struct rpc_rqst *req, uint32_t *p, struct nfs4_fsinfo_arg *args)
1850 struct xdr_stream xdr;
1851 struct compound_hdr hdr = {
1852 .nops = 2,
1854 int status;
1856 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1857 encode_compound_hdr(&xdr, &hdr);
1858 status = encode_putfh(&xdr, args->fh);
1859 if (!status)
1860 status = encode_fsinfo(&xdr, args->bitmask);
1861 return status;
1865 * a PATHCONF request
1867 static int nfs4_xdr_enc_pathconf(struct rpc_rqst *req, uint32_t *p, const struct nfs4_pathconf_arg *args)
1869 struct xdr_stream xdr;
1870 struct compound_hdr hdr = {
1871 .nops = 2,
1873 int status;
1875 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1876 encode_compound_hdr(&xdr, &hdr);
1877 status = encode_putfh(&xdr, args->fh);
1878 if (!status)
1879 status = encode_getattr_one(&xdr,
1880 args->bitmask[0] & nfs4_pathconf_bitmap[0]);
1881 return status;
1885 * a STATFS request
1887 static int nfs4_xdr_enc_statfs(struct rpc_rqst *req, uint32_t *p, const struct nfs4_statfs_arg *args)
1889 struct xdr_stream xdr;
1890 struct compound_hdr hdr = {
1891 .nops = 2,
1893 int status;
1895 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1896 encode_compound_hdr(&xdr, &hdr);
1897 status = encode_putfh(&xdr, args->fh);
1898 if (status == 0)
1899 status = encode_getattr_two(&xdr,
1900 args->bitmask[0] & nfs4_statfs_bitmap[0],
1901 args->bitmask[1] & nfs4_statfs_bitmap[1]);
1902 return status;
1906 * GETATTR_BITMAP request
1908 static int nfs4_xdr_enc_server_caps(struct rpc_rqst *req, uint32_t *p, const struct nfs_fh *fhandle)
1910 struct xdr_stream xdr;
1911 struct compound_hdr hdr = {
1912 .nops = 2,
1914 int status;
1916 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1917 encode_compound_hdr(&xdr, &hdr);
1918 status = encode_putfh(&xdr, fhandle);
1919 if (status == 0)
1920 status = encode_getattr_one(&xdr, FATTR4_WORD0_SUPPORTED_ATTRS|
1921 FATTR4_WORD0_LINK_SUPPORT|
1922 FATTR4_WORD0_SYMLINK_SUPPORT|
1923 FATTR4_WORD0_ACLSUPPORT);
1924 return status;
1928 * a RENEW request
1930 static int nfs4_xdr_enc_renew(struct rpc_rqst *req, uint32_t *p, struct nfs4_client *clp)
1932 struct xdr_stream xdr;
1933 struct compound_hdr hdr = {
1934 .nops = 1,
1937 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1938 encode_compound_hdr(&xdr, &hdr);
1939 return encode_renew(&xdr, clp);
1943 * a SETCLIENTID request
1945 static int nfs4_xdr_enc_setclientid(struct rpc_rqst *req, uint32_t *p, struct nfs4_setclientid *sc)
1947 struct xdr_stream xdr;
1948 struct compound_hdr hdr = {
1949 .nops = 1,
1952 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1953 encode_compound_hdr(&xdr, &hdr);
1954 return encode_setclientid(&xdr, sc);
1958 * a SETCLIENTID_CONFIRM request
1960 static int nfs4_xdr_enc_setclientid_confirm(struct rpc_rqst *req, uint32_t *p, struct nfs4_client *clp)
1962 struct xdr_stream xdr;
1963 struct compound_hdr hdr = {
1964 .nops = 3,
1966 const u32 lease_bitmap[2] = { FATTR4_WORD0_LEASE_TIME, 0 };
1967 int status;
1969 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1970 encode_compound_hdr(&xdr, &hdr);
1971 status = encode_setclientid_confirm(&xdr, clp);
1972 if (!status)
1973 status = encode_putrootfh(&xdr);
1974 if (!status)
1975 status = encode_fsinfo(&xdr, lease_bitmap);
1976 return status;
1980 * DELEGRETURN request
1982 static int nfs4_xdr_enc_delegreturn(struct rpc_rqst *req, uint32_t *p, const struct nfs4_delegreturnargs *args)
1984 struct xdr_stream xdr;
1985 struct compound_hdr hdr = {
1986 .nops = 2,
1988 int status;
1990 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1991 encode_compound_hdr(&xdr, &hdr);
1992 if ((status = encode_putfh(&xdr, args->fhandle)) == 0)
1993 status = encode_delegreturn(&xdr, args->stateid);
1994 return status;
1998 * START OF "GENERIC" DECODE ROUTINES.
1999 * These may look a little ugly since they are imported from a "generic"
2000 * set of XDR encode/decode routines which are intended to be shared by
2001 * all of our NFSv4 implementations (OpenBSD, MacOS X...).
2003 * If the pain of reading these is too great, it should be a straightforward
2004 * task to translate them into Linux-specific versions which are more
2005 * consistent with the style used in NFSv2/v3...
2007 #define READ32(x) (x) = ntohl(*p++)
2008 #define READ64(x) do { \
2009 (x) = (u64)ntohl(*p++) << 32; \
2010 (x) |= ntohl(*p++); \
2011 } while (0)
2012 #define READTIME(x) do { \
2013 p++; \
2014 (x.tv_sec) = ntohl(*p++); \
2015 (x.tv_nsec) = ntohl(*p++); \
2016 } while (0)
2017 #define COPYMEM(x,nbytes) do { \
2018 memcpy((x), p, nbytes); \
2019 p += XDR_QUADLEN(nbytes); \
2020 } while (0)
2022 #define READ_BUF(nbytes) do { \
2023 p = xdr_inline_decode(xdr, nbytes); \
2024 if (!p) { \
2025 printk(KERN_WARNING "%s: reply buffer overflowed in line %d.", \
2026 __FUNCTION__, __LINE__); \
2027 return -EIO; \
2029 } while (0)
2031 static int decode_opaque_inline(struct xdr_stream *xdr, uint32_t *len, char **string)
2033 uint32_t *p;
2035 READ_BUF(4);
2036 READ32(*len);
2037 READ_BUF(*len);
2038 *string = (char *)p;
2039 return 0;
2042 static int decode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
2044 uint32_t *p;
2046 READ_BUF(8);
2047 READ32(hdr->status);
2048 READ32(hdr->taglen);
2050 READ_BUF(hdr->taglen + 4);
2051 hdr->tag = (char *)p;
2052 p += XDR_QUADLEN(hdr->taglen);
2053 READ32(hdr->nops);
2054 return 0;
2057 static int decode_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
2059 uint32_t *p;
2060 uint32_t opnum;
2061 int32_t nfserr;
2063 READ_BUF(8);
2064 READ32(opnum);
2065 if (opnum != expected) {
2066 printk(KERN_NOTICE
2067 "nfs4_decode_op_hdr: Server returned operation"
2068 " %d but we issued a request for %d\n",
2069 opnum, expected);
2070 return -EIO;
2072 READ32(nfserr);
2073 if (nfserr != NFS_OK)
2074 return -nfs_stat_to_errno(nfserr);
2075 return 0;
2078 /* Dummy routine */
2079 static int decode_ace(struct xdr_stream *xdr, void *ace, struct nfs4_client *clp)
2081 uint32_t *p;
2082 uint32_t strlen;
2083 char *str;
2085 READ_BUF(12);
2086 return decode_opaque_inline(xdr, &strlen, &str);
2089 static int decode_attr_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
2091 uint32_t bmlen, *p;
2093 READ_BUF(4);
2094 READ32(bmlen);
2096 bitmap[0] = bitmap[1] = 0;
2097 READ_BUF((bmlen << 2));
2098 if (bmlen > 0) {
2099 READ32(bitmap[0]);
2100 if (bmlen > 1)
2101 READ32(bitmap[1]);
2103 return 0;
2106 static inline int decode_attr_length(struct xdr_stream *xdr, uint32_t *attrlen, uint32_t **savep)
2108 uint32_t *p;
2110 READ_BUF(4);
2111 READ32(*attrlen);
2112 *savep = xdr->p;
2113 return 0;
2116 static int decode_attr_supported(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *bitmask)
2118 if (likely(bitmap[0] & FATTR4_WORD0_SUPPORTED_ATTRS)) {
2119 decode_attr_bitmap(xdr, bitmask);
2120 bitmap[0] &= ~FATTR4_WORD0_SUPPORTED_ATTRS;
2121 } else
2122 bitmask[0] = bitmask[1] = 0;
2123 dprintk("%s: bitmask=0x%x%x\n", __FUNCTION__, bitmask[0], bitmask[1]);
2124 return 0;
2127 static int decode_attr_type(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *type)
2129 uint32_t *p;
2131 *type = 0;
2132 if (unlikely(bitmap[0] & (FATTR4_WORD0_TYPE - 1U)))
2133 return -EIO;
2134 if (likely(bitmap[0] & FATTR4_WORD0_TYPE)) {
2135 READ_BUF(4);
2136 READ32(*type);
2137 if (*type < NF4REG || *type > NF4NAMEDATTR) {
2138 dprintk("%s: bad type %d\n", __FUNCTION__, *type);
2139 return -EIO;
2141 bitmap[0] &= ~FATTR4_WORD0_TYPE;
2143 dprintk("%s: type=0%o\n", __FUNCTION__, nfs_type2fmt[*type].nfs2type);
2144 return 0;
2147 static int decode_attr_change(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *change)
2149 uint32_t *p;
2151 *change = 0;
2152 if (unlikely(bitmap[0] & (FATTR4_WORD0_CHANGE - 1U)))
2153 return -EIO;
2154 if (likely(bitmap[0] & FATTR4_WORD0_CHANGE)) {
2155 READ_BUF(8);
2156 READ64(*change);
2157 bitmap[0] &= ~FATTR4_WORD0_CHANGE;
2159 dprintk("%s: change attribute=%Lu\n", __FUNCTION__,
2160 (unsigned long long)*change);
2161 return 0;
2164 static int decode_attr_size(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *size)
2166 uint32_t *p;
2168 *size = 0;
2169 if (unlikely(bitmap[0] & (FATTR4_WORD0_SIZE - 1U)))
2170 return -EIO;
2171 if (likely(bitmap[0] & FATTR4_WORD0_SIZE)) {
2172 READ_BUF(8);
2173 READ64(*size);
2174 bitmap[0] &= ~FATTR4_WORD0_SIZE;
2176 dprintk("%s: file size=%Lu\n", __FUNCTION__, (unsigned long long)*size);
2177 return 0;
2180 static int decode_attr_link_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2182 uint32_t *p;
2184 *res = 0;
2185 if (unlikely(bitmap[0] & (FATTR4_WORD0_LINK_SUPPORT - 1U)))
2186 return -EIO;
2187 if (likely(bitmap[0] & FATTR4_WORD0_LINK_SUPPORT)) {
2188 READ_BUF(4);
2189 READ32(*res);
2190 bitmap[0] &= ~FATTR4_WORD0_LINK_SUPPORT;
2192 dprintk("%s: link support=%s\n", __FUNCTION__, *res == 0 ? "false" : "true");
2193 return 0;
2196 static int decode_attr_symlink_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2198 uint32_t *p;
2200 *res = 0;
2201 if (unlikely(bitmap[0] & (FATTR4_WORD0_SYMLINK_SUPPORT - 1U)))
2202 return -EIO;
2203 if (likely(bitmap[0] & FATTR4_WORD0_SYMLINK_SUPPORT)) {
2204 READ_BUF(4);
2205 READ32(*res);
2206 bitmap[0] &= ~FATTR4_WORD0_SYMLINK_SUPPORT;
2208 dprintk("%s: symlink support=%s\n", __FUNCTION__, *res == 0 ? "false" : "true");
2209 return 0;
2212 static int decode_attr_fsid(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_fsid *fsid)
2214 uint32_t *p;
2216 fsid->major = 0;
2217 fsid->minor = 0;
2218 if (unlikely(bitmap[0] & (FATTR4_WORD0_FSID - 1U)))
2219 return -EIO;
2220 if (likely(bitmap[0] & FATTR4_WORD0_FSID)) {
2221 READ_BUF(16);
2222 READ64(fsid->major);
2223 READ64(fsid->minor);
2224 bitmap[0] &= ~FATTR4_WORD0_FSID;
2226 dprintk("%s: fsid=(0x%Lx/0x%Lx)\n", __FUNCTION__,
2227 (unsigned long long)fsid->major,
2228 (unsigned long long)fsid->minor);
2229 return 0;
2232 static int decode_attr_lease_time(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2234 uint32_t *p;
2236 *res = 60;
2237 if (unlikely(bitmap[0] & (FATTR4_WORD0_LEASE_TIME - 1U)))
2238 return -EIO;
2239 if (likely(bitmap[0] & FATTR4_WORD0_LEASE_TIME)) {
2240 READ_BUF(4);
2241 READ32(*res);
2242 bitmap[0] &= ~FATTR4_WORD0_LEASE_TIME;
2244 dprintk("%s: file size=%u\n", __FUNCTION__, (unsigned int)*res);
2245 return 0;
2248 static int decode_attr_aclsupport(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2250 uint32_t *p;
2252 *res = ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL;
2253 if (unlikely(bitmap[0] & (FATTR4_WORD0_ACLSUPPORT - 1U)))
2254 return -EIO;
2255 if (likely(bitmap[0] & FATTR4_WORD0_ACLSUPPORT)) {
2256 READ_BUF(4);
2257 READ32(*res);
2258 bitmap[0] &= ~FATTR4_WORD0_ACLSUPPORT;
2260 dprintk("%s: ACLs supported=%u\n", __FUNCTION__, (unsigned int)*res);
2261 return 0;
2264 static int decode_attr_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
2266 uint32_t *p;
2268 *fileid = 0;
2269 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILEID - 1U)))
2270 return -EIO;
2271 if (likely(bitmap[0] & FATTR4_WORD0_FILEID)) {
2272 READ_BUF(8);
2273 READ64(*fileid);
2274 bitmap[0] &= ~FATTR4_WORD0_FILEID;
2276 dprintk("%s: fileid=%Lu\n", __FUNCTION__, (unsigned long long)*fileid);
2277 return 0;
2280 static int decode_attr_files_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2282 uint32_t *p;
2283 int status = 0;
2285 *res = 0;
2286 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_AVAIL - 1U)))
2287 return -EIO;
2288 if (likely(bitmap[0] & FATTR4_WORD0_FILES_AVAIL)) {
2289 READ_BUF(8);
2290 READ64(*res);
2291 bitmap[0] &= ~FATTR4_WORD0_FILES_AVAIL;
2293 dprintk("%s: files avail=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2294 return status;
2297 static int decode_attr_files_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2299 uint32_t *p;
2300 int status = 0;
2302 *res = 0;
2303 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_FREE - 1U)))
2304 return -EIO;
2305 if (likely(bitmap[0] & FATTR4_WORD0_FILES_FREE)) {
2306 READ_BUF(8);
2307 READ64(*res);
2308 bitmap[0] &= ~FATTR4_WORD0_FILES_FREE;
2310 dprintk("%s: files free=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2311 return status;
2314 static int decode_attr_files_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2316 uint32_t *p;
2317 int status = 0;
2319 *res = 0;
2320 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_TOTAL - 1U)))
2321 return -EIO;
2322 if (likely(bitmap[0] & FATTR4_WORD0_FILES_TOTAL)) {
2323 READ_BUF(8);
2324 READ64(*res);
2325 bitmap[0] &= ~FATTR4_WORD0_FILES_TOTAL;
2327 dprintk("%s: files total=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2328 return status;
2331 static int decode_attr_maxfilesize(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2333 uint32_t *p;
2334 int status = 0;
2336 *res = 0;
2337 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXFILESIZE - 1U)))
2338 return -EIO;
2339 if (likely(bitmap[0] & FATTR4_WORD0_MAXFILESIZE)) {
2340 READ_BUF(8);
2341 READ64(*res);
2342 bitmap[0] &= ~FATTR4_WORD0_MAXFILESIZE;
2344 dprintk("%s: maxfilesize=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2345 return status;
2348 static int decode_attr_maxlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxlink)
2350 uint32_t *p;
2351 int status = 0;
2353 *maxlink = 1;
2354 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXLINK - 1U)))
2355 return -EIO;
2356 if (likely(bitmap[0] & FATTR4_WORD0_MAXLINK)) {
2357 READ_BUF(4);
2358 READ32(*maxlink);
2359 bitmap[0] &= ~FATTR4_WORD0_MAXLINK;
2361 dprintk("%s: maxlink=%u\n", __FUNCTION__, *maxlink);
2362 return status;
2365 static int decode_attr_maxname(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxname)
2367 uint32_t *p;
2368 int status = 0;
2370 *maxname = 1024;
2371 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXNAME - 1U)))
2372 return -EIO;
2373 if (likely(bitmap[0] & FATTR4_WORD0_MAXNAME)) {
2374 READ_BUF(4);
2375 READ32(*maxname);
2376 bitmap[0] &= ~FATTR4_WORD0_MAXNAME;
2378 dprintk("%s: maxname=%u\n", __FUNCTION__, *maxname);
2379 return status;
2382 static int decode_attr_maxread(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2384 uint32_t *p;
2385 int status = 0;
2387 *res = 1024;
2388 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXREAD - 1U)))
2389 return -EIO;
2390 if (likely(bitmap[0] & FATTR4_WORD0_MAXREAD)) {
2391 uint64_t maxread;
2392 READ_BUF(8);
2393 READ64(maxread);
2394 if (maxread > 0x7FFFFFFF)
2395 maxread = 0x7FFFFFFF;
2396 *res = (uint32_t)maxread;
2397 bitmap[0] &= ~FATTR4_WORD0_MAXREAD;
2399 dprintk("%s: maxread=%lu\n", __FUNCTION__, (unsigned long)*res);
2400 return status;
2403 static int decode_attr_maxwrite(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2405 uint32_t *p;
2406 int status = 0;
2408 *res = 1024;
2409 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXWRITE - 1U)))
2410 return -EIO;
2411 if (likely(bitmap[0] & FATTR4_WORD0_MAXWRITE)) {
2412 uint64_t maxwrite;
2413 READ_BUF(8);
2414 READ64(maxwrite);
2415 if (maxwrite > 0x7FFFFFFF)
2416 maxwrite = 0x7FFFFFFF;
2417 *res = (uint32_t)maxwrite;
2418 bitmap[0] &= ~FATTR4_WORD0_MAXWRITE;
2420 dprintk("%s: maxwrite=%lu\n", __FUNCTION__, (unsigned long)*res);
2421 return status;
2424 static int decode_attr_mode(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *mode)
2426 uint32_t *p;
2428 *mode = 0;
2429 if (unlikely(bitmap[1] & (FATTR4_WORD1_MODE - 1U)))
2430 return -EIO;
2431 if (likely(bitmap[1] & FATTR4_WORD1_MODE)) {
2432 READ_BUF(4);
2433 READ32(*mode);
2434 *mode &= ~S_IFMT;
2435 bitmap[1] &= ~FATTR4_WORD1_MODE;
2437 dprintk("%s: file mode=0%o\n", __FUNCTION__, (unsigned int)*mode);
2438 return 0;
2441 static int decode_attr_nlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *nlink)
2443 uint32_t *p;
2445 *nlink = 1;
2446 if (unlikely(bitmap[1] & (FATTR4_WORD1_NUMLINKS - 1U)))
2447 return -EIO;
2448 if (likely(bitmap[1] & FATTR4_WORD1_NUMLINKS)) {
2449 READ_BUF(4);
2450 READ32(*nlink);
2451 bitmap[1] &= ~FATTR4_WORD1_NUMLINKS;
2453 dprintk("%s: nlink=%u\n", __FUNCTION__, (unsigned int)*nlink);
2454 return 0;
2457 static int decode_attr_owner(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_client *clp, int32_t *uid)
2459 uint32_t len, *p;
2461 *uid = -2;
2462 if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER - 1U)))
2463 return -EIO;
2464 if (likely(bitmap[1] & FATTR4_WORD1_OWNER)) {
2465 READ_BUF(4);
2466 READ32(len);
2467 READ_BUF(len);
2468 if (len < XDR_MAX_NETOBJ) {
2469 if (nfs_map_name_to_uid(clp, (char *)p, len, uid) != 0)
2470 dprintk("%s: nfs_map_name_to_uid failed!\n",
2471 __FUNCTION__);
2472 } else
2473 printk(KERN_WARNING "%s: name too long (%u)!\n",
2474 __FUNCTION__, len);
2475 bitmap[1] &= ~FATTR4_WORD1_OWNER;
2477 dprintk("%s: uid=%d\n", __FUNCTION__, (int)*uid);
2478 return 0;
2481 static int decode_attr_group(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_client *clp, int32_t *gid)
2483 uint32_t len, *p;
2485 *gid = -2;
2486 if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER_GROUP - 1U)))
2487 return -EIO;
2488 if (likely(bitmap[1] & FATTR4_WORD1_OWNER_GROUP)) {
2489 READ_BUF(4);
2490 READ32(len);
2491 READ_BUF(len);
2492 if (len < XDR_MAX_NETOBJ) {
2493 if (nfs_map_group_to_gid(clp, (char *)p, len, gid) != 0)
2494 dprintk("%s: nfs_map_group_to_gid failed!\n",
2495 __FUNCTION__);
2496 } else
2497 printk(KERN_WARNING "%s: name too long (%u)!\n",
2498 __FUNCTION__, len);
2499 bitmap[1] &= ~FATTR4_WORD1_OWNER_GROUP;
2501 dprintk("%s: gid=%d\n", __FUNCTION__, (int)*gid);
2502 return 0;
2505 static int decode_attr_rdev(struct xdr_stream *xdr, uint32_t *bitmap, dev_t *rdev)
2507 uint32_t major = 0, minor = 0, *p;
2509 *rdev = MKDEV(0,0);
2510 if (unlikely(bitmap[1] & (FATTR4_WORD1_RAWDEV - 1U)))
2511 return -EIO;
2512 if (likely(bitmap[1] & FATTR4_WORD1_RAWDEV)) {
2513 dev_t tmp;
2515 READ_BUF(8);
2516 READ32(major);
2517 READ32(minor);
2518 tmp = MKDEV(major, minor);
2519 if (MAJOR(tmp) == major && MINOR(tmp) == minor)
2520 *rdev = tmp;
2521 bitmap[1] &= ~ FATTR4_WORD1_RAWDEV;
2523 dprintk("%s: rdev=(0x%x:0x%x)\n", __FUNCTION__, major, minor);
2524 return 0;
2527 static int decode_attr_space_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2529 uint32_t *p;
2530 int status = 0;
2532 *res = 0;
2533 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_AVAIL - 1U)))
2534 return -EIO;
2535 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_AVAIL)) {
2536 READ_BUF(8);
2537 READ64(*res);
2538 bitmap[1] &= ~FATTR4_WORD1_SPACE_AVAIL;
2540 dprintk("%s: space avail=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2541 return status;
2544 static int decode_attr_space_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2546 uint32_t *p;
2547 int status = 0;
2549 *res = 0;
2550 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_FREE - 1U)))
2551 return -EIO;
2552 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_FREE)) {
2553 READ_BUF(8);
2554 READ64(*res);
2555 bitmap[1] &= ~FATTR4_WORD1_SPACE_FREE;
2557 dprintk("%s: space free=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2558 return status;
2561 static int decode_attr_space_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2563 uint32_t *p;
2564 int status = 0;
2566 *res = 0;
2567 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_TOTAL - 1U)))
2568 return -EIO;
2569 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_TOTAL)) {
2570 READ_BUF(8);
2571 READ64(*res);
2572 bitmap[1] &= ~FATTR4_WORD1_SPACE_TOTAL;
2574 dprintk("%s: space total=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2575 return status;
2578 static int decode_attr_space_used(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *used)
2580 uint32_t *p;
2582 *used = 0;
2583 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_USED - 1U)))
2584 return -EIO;
2585 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_USED)) {
2586 READ_BUF(8);
2587 READ64(*used);
2588 bitmap[1] &= ~FATTR4_WORD1_SPACE_USED;
2590 dprintk("%s: space used=%Lu\n", __FUNCTION__,
2591 (unsigned long long)*used);
2592 return 0;
2595 static int decode_attr_time(struct xdr_stream *xdr, struct timespec *time)
2597 uint32_t *p;
2598 uint64_t sec;
2599 uint32_t nsec;
2601 READ_BUF(12);
2602 READ64(sec);
2603 READ32(nsec);
2604 time->tv_sec = (time_t)sec;
2605 time->tv_nsec = (long)nsec;
2606 return 0;
2609 static int decode_attr_time_access(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2611 int status = 0;
2613 time->tv_sec = 0;
2614 time->tv_nsec = 0;
2615 if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_ACCESS - 1U)))
2616 return -EIO;
2617 if (likely(bitmap[1] & FATTR4_WORD1_TIME_ACCESS)) {
2618 status = decode_attr_time(xdr, time);
2619 bitmap[1] &= ~FATTR4_WORD1_TIME_ACCESS;
2621 dprintk("%s: atime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2622 return status;
2625 static int decode_attr_time_metadata(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2627 int status = 0;
2629 time->tv_sec = 0;
2630 time->tv_nsec = 0;
2631 if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_METADATA - 1U)))
2632 return -EIO;
2633 if (likely(bitmap[1] & FATTR4_WORD1_TIME_METADATA)) {
2634 status = decode_attr_time(xdr, time);
2635 bitmap[1] &= ~FATTR4_WORD1_TIME_METADATA;
2637 dprintk("%s: ctime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2638 return status;
2641 static int decode_attr_time_modify(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2643 int status = 0;
2645 time->tv_sec = 0;
2646 time->tv_nsec = 0;
2647 if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_MODIFY - 1U)))
2648 return -EIO;
2649 if (likely(bitmap[1] & FATTR4_WORD1_TIME_MODIFY)) {
2650 status = decode_attr_time(xdr, time);
2651 bitmap[1] &= ~FATTR4_WORD1_TIME_MODIFY;
2653 dprintk("%s: mtime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2654 return status;
2657 static int verify_attr_len(struct xdr_stream *xdr, uint32_t *savep, uint32_t attrlen)
2659 unsigned int attrwords = XDR_QUADLEN(attrlen);
2660 unsigned int nwords = xdr->p - savep;
2662 if (unlikely(attrwords != nwords)) {
2663 printk(KERN_WARNING "%s: server returned incorrect attribute length: %u %c %u\n",
2664 __FUNCTION__,
2665 attrwords << 2,
2666 (attrwords < nwords) ? '<' : '>',
2667 nwords << 2);
2668 return -EIO;
2670 return 0;
2673 static int decode_change_info(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
2675 uint32_t *p;
2677 READ_BUF(20);
2678 READ32(cinfo->atomic);
2679 READ64(cinfo->before);
2680 READ64(cinfo->after);
2681 return 0;
2684 static int decode_access(struct xdr_stream *xdr, struct nfs4_accessres *access)
2686 uint32_t *p;
2687 uint32_t supp, acc;
2688 int status;
2690 status = decode_op_hdr(xdr, OP_ACCESS);
2691 if (status)
2692 return status;
2693 READ_BUF(8);
2694 READ32(supp);
2695 READ32(acc);
2696 access->supported = supp;
2697 access->access = acc;
2698 return 0;
2701 static int decode_close(struct xdr_stream *xdr, struct nfs_closeres *res)
2703 uint32_t *p;
2704 int status;
2706 status = decode_op_hdr(xdr, OP_CLOSE);
2707 if (status)
2708 return status;
2709 READ_BUF(sizeof(res->stateid.data));
2710 COPYMEM(res->stateid.data, sizeof(res->stateid.data));
2711 return 0;
2714 static int decode_commit(struct xdr_stream *xdr, struct nfs_writeres *res)
2716 uint32_t *p;
2717 int status;
2719 status = decode_op_hdr(xdr, OP_COMMIT);
2720 if (status)
2721 return status;
2722 READ_BUF(8);
2723 COPYMEM(res->verf->verifier, 8);
2724 return 0;
2727 static int decode_create(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
2729 uint32_t *p;
2730 uint32_t bmlen;
2731 int status;
2733 status = decode_op_hdr(xdr, OP_CREATE);
2734 if (status)
2735 return status;
2736 if ((status = decode_change_info(xdr, cinfo)))
2737 return status;
2738 READ_BUF(4);
2739 READ32(bmlen);
2740 READ_BUF(bmlen << 2);
2741 return 0;
2744 static int decode_server_caps(struct xdr_stream *xdr, struct nfs4_server_caps_res *res)
2746 uint32_t *savep;
2747 uint32_t attrlen,
2748 bitmap[2] = {0};
2749 int status;
2751 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2752 goto xdr_error;
2753 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2754 goto xdr_error;
2755 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2756 goto xdr_error;
2757 if ((status = decode_attr_supported(xdr, bitmap, res->attr_bitmask)) != 0)
2758 goto xdr_error;
2759 if ((status = decode_attr_link_support(xdr, bitmap, &res->has_links)) != 0)
2760 goto xdr_error;
2761 if ((status = decode_attr_symlink_support(xdr, bitmap, &res->has_symlinks)) != 0)
2762 goto xdr_error;
2763 if ((status = decode_attr_aclsupport(xdr, bitmap, &res->acl_bitmask)) != 0)
2764 goto xdr_error;
2765 status = verify_attr_len(xdr, savep, attrlen);
2766 xdr_error:
2767 dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
2768 return status;
2771 static int decode_statfs(struct xdr_stream *xdr, struct nfs_fsstat *fsstat)
2773 uint32_t *savep;
2774 uint32_t attrlen,
2775 bitmap[2] = {0};
2776 int status;
2778 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2779 goto xdr_error;
2780 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2781 goto xdr_error;
2782 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2783 goto xdr_error;
2785 if ((status = decode_attr_files_avail(xdr, bitmap, &fsstat->afiles)) != 0)
2786 goto xdr_error;
2787 if ((status = decode_attr_files_free(xdr, bitmap, &fsstat->ffiles)) != 0)
2788 goto xdr_error;
2789 if ((status = decode_attr_files_total(xdr, bitmap, &fsstat->tfiles)) != 0)
2790 goto xdr_error;
2791 if ((status = decode_attr_space_avail(xdr, bitmap, &fsstat->abytes)) != 0)
2792 goto xdr_error;
2793 if ((status = decode_attr_space_free(xdr, bitmap, &fsstat->fbytes)) != 0)
2794 goto xdr_error;
2795 if ((status = decode_attr_space_total(xdr, bitmap, &fsstat->tbytes)) != 0)
2796 goto xdr_error;
2798 status = verify_attr_len(xdr, savep, attrlen);
2799 xdr_error:
2800 dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
2801 return status;
2804 static int decode_pathconf(struct xdr_stream *xdr, struct nfs_pathconf *pathconf)
2806 uint32_t *savep;
2807 uint32_t attrlen,
2808 bitmap[2] = {0};
2809 int status;
2811 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2812 goto xdr_error;
2813 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2814 goto xdr_error;
2815 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2816 goto xdr_error;
2818 if ((status = decode_attr_maxlink(xdr, bitmap, &pathconf->max_link)) != 0)
2819 goto xdr_error;
2820 if ((status = decode_attr_maxname(xdr, bitmap, &pathconf->max_namelen)) != 0)
2821 goto xdr_error;
2823 status = verify_attr_len(xdr, savep, attrlen);
2824 xdr_error:
2825 dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
2826 return status;
2829 static int decode_getfattr(struct xdr_stream *xdr, struct nfs_fattr *fattr, const struct nfs_server *server)
2831 uint32_t *savep;
2832 uint32_t attrlen,
2833 bitmap[2] = {0},
2834 type;
2835 int status, fmode = 0;
2837 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2838 goto xdr_error;
2839 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2840 goto xdr_error;
2842 fattr->bitmap[0] = bitmap[0];
2843 fattr->bitmap[1] = bitmap[1];
2845 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2846 goto xdr_error;
2849 if ((status = decode_attr_type(xdr, bitmap, &type)) != 0)
2850 goto xdr_error;
2851 fattr->type = nfs_type2fmt[type].nfs2type;
2852 fmode = nfs_type2fmt[type].mode;
2854 if ((status = decode_attr_change(xdr, bitmap, &fattr->change_attr)) != 0)
2855 goto xdr_error;
2856 if ((status = decode_attr_size(xdr, bitmap, &fattr->size)) != 0)
2857 goto xdr_error;
2858 if ((status = decode_attr_fsid(xdr, bitmap, &fattr->fsid_u.nfs4)) != 0)
2859 goto xdr_error;
2860 if ((status = decode_attr_fileid(xdr, bitmap, &fattr->fileid)) != 0)
2861 goto xdr_error;
2862 if ((status = decode_attr_mode(xdr, bitmap, &fattr->mode)) != 0)
2863 goto xdr_error;
2864 fattr->mode |= fmode;
2865 if ((status = decode_attr_nlink(xdr, bitmap, &fattr->nlink)) != 0)
2866 goto xdr_error;
2867 if ((status = decode_attr_owner(xdr, bitmap, server->nfs4_state, &fattr->uid)) != 0)
2868 goto xdr_error;
2869 if ((status = decode_attr_group(xdr, bitmap, server->nfs4_state, &fattr->gid)) != 0)
2870 goto xdr_error;
2871 if ((status = decode_attr_rdev(xdr, bitmap, &fattr->rdev)) != 0)
2872 goto xdr_error;
2873 if ((status = decode_attr_space_used(xdr, bitmap, &fattr->du.nfs3.used)) != 0)
2874 goto xdr_error;
2875 if ((status = decode_attr_time_access(xdr, bitmap, &fattr->atime)) != 0)
2876 goto xdr_error;
2877 if ((status = decode_attr_time_metadata(xdr, bitmap, &fattr->ctime)) != 0)
2878 goto xdr_error;
2879 if ((status = decode_attr_time_modify(xdr, bitmap, &fattr->mtime)) != 0)
2880 goto xdr_error;
2881 if ((status = verify_attr_len(xdr, savep, attrlen)) == 0)
2882 fattr->valid = NFS_ATTR_FATTR | NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4;
2883 xdr_error:
2884 dprintk("%s: xdr returned %d\n", __FUNCTION__, -status);
2885 return status;
2889 static int decode_fsinfo(struct xdr_stream *xdr, struct nfs_fsinfo *fsinfo)
2891 uint32_t *savep;
2892 uint32_t attrlen, bitmap[2];
2893 int status;
2895 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2896 goto xdr_error;
2897 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2898 goto xdr_error;
2899 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2900 goto xdr_error;
2902 fsinfo->rtmult = fsinfo->wtmult = 512; /* ??? */
2904 if ((status = decode_attr_lease_time(xdr, bitmap, &fsinfo->lease_time)) != 0)
2905 goto xdr_error;
2906 if ((status = decode_attr_maxfilesize(xdr, bitmap, &fsinfo->maxfilesize)) != 0)
2907 goto xdr_error;
2908 if ((status = decode_attr_maxread(xdr, bitmap, &fsinfo->rtmax)) != 0)
2909 goto xdr_error;
2910 fsinfo->rtpref = fsinfo->dtpref = fsinfo->rtmax;
2911 if ((status = decode_attr_maxwrite(xdr, bitmap, &fsinfo->wtmax)) != 0)
2912 goto xdr_error;
2913 fsinfo->wtpref = fsinfo->wtmax;
2915 status = verify_attr_len(xdr, savep, attrlen);
2916 xdr_error:
2917 dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
2918 return status;
2921 static int decode_getfh(struct xdr_stream *xdr, struct nfs_fh *fh)
2923 uint32_t *p;
2924 uint32_t len;
2925 int status;
2927 status = decode_op_hdr(xdr, OP_GETFH);
2928 if (status)
2929 return status;
2930 /* Zero handle first to allow comparisons */
2931 memset(fh, 0, sizeof(*fh));
2933 READ_BUF(4);
2934 READ32(len);
2935 if (len > NFS4_FHSIZE)
2936 return -EIO;
2937 fh->size = len;
2938 READ_BUF(len);
2939 COPYMEM(fh->data, len);
2940 return 0;
2943 static int decode_link(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
2945 int status;
2947 status = decode_op_hdr(xdr, OP_LINK);
2948 if (status)
2949 return status;
2950 return decode_change_info(xdr, cinfo);
2954 * We create the owner, so we know a proper owner.id length is 4.
2956 static int decode_lock_denied (struct xdr_stream *xdr, struct file_lock *fl)
2958 uint64_t offset, length, clientid;
2959 uint32_t *p;
2960 uint32_t namelen, type;
2962 READ_BUF(32);
2963 READ64(offset);
2964 READ64(length);
2965 READ32(type);
2966 if (fl != NULL) {
2967 fl->fl_start = (loff_t)offset;
2968 fl->fl_end = fl->fl_start + (loff_t)length - 1;
2969 if (length == ~(uint64_t)0)
2970 fl->fl_end = OFFSET_MAX;
2971 fl->fl_type = F_WRLCK;
2972 if (type & 1)
2973 fl->fl_type = F_RDLCK;
2974 fl->fl_pid = 0;
2976 READ64(clientid);
2977 READ32(namelen);
2978 READ_BUF(namelen);
2979 return -NFS4ERR_DENIED;
2982 static int decode_lock(struct xdr_stream *xdr, struct nfs_lock_res *res)
2984 uint32_t *p;
2985 int status;
2987 status = decode_op_hdr(xdr, OP_LOCK);
2988 if (status == 0) {
2989 READ_BUF(sizeof(res->stateid.data));
2990 COPYMEM(res->stateid.data, sizeof(res->stateid.data));
2991 } else if (status == -NFS4ERR_DENIED)
2992 return decode_lock_denied(xdr, NULL);
2993 return status;
2996 static int decode_lockt(struct xdr_stream *xdr, struct nfs_lockt_res *res)
2998 int status;
2999 status = decode_op_hdr(xdr, OP_LOCKT);
3000 if (status == -NFS4ERR_DENIED)
3001 return decode_lock_denied(xdr, res->denied);
3002 return status;
3005 static int decode_locku(struct xdr_stream *xdr, struct nfs_locku_res *res)
3007 uint32_t *p;
3008 int status;
3010 status = decode_op_hdr(xdr, OP_LOCKU);
3011 if (status == 0) {
3012 READ_BUF(sizeof(res->stateid.data));
3013 COPYMEM(res->stateid.data, sizeof(res->stateid.data));
3015 return status;
3018 static int decode_lookup(struct xdr_stream *xdr)
3020 return decode_op_hdr(xdr, OP_LOOKUP);
3023 /* This is too sick! */
3024 static int decode_space_limit(struct xdr_stream *xdr, u64 *maxsize)
3026 uint32_t *p;
3027 uint32_t limit_type, nblocks, blocksize;
3029 READ_BUF(12);
3030 READ32(limit_type);
3031 switch (limit_type) {
3032 case 1:
3033 READ64(*maxsize);
3034 break;
3035 case 2:
3036 READ32(nblocks);
3037 READ32(blocksize);
3038 *maxsize = (uint64_t)nblocks * (uint64_t)blocksize;
3040 return 0;
3043 static int decode_delegation(struct xdr_stream *xdr, struct nfs_openres *res)
3045 uint32_t *p;
3046 uint32_t delegation_type;
3048 READ_BUF(4);
3049 READ32(delegation_type);
3050 if (delegation_type == NFS4_OPEN_DELEGATE_NONE) {
3051 res->delegation_type = 0;
3052 return 0;
3054 READ_BUF(20);
3055 COPYMEM(res->delegation.data, sizeof(res->delegation.data));
3056 READ32(res->do_recall);
3057 switch (delegation_type) {
3058 case NFS4_OPEN_DELEGATE_READ:
3059 res->delegation_type = FMODE_READ;
3060 break;
3061 case NFS4_OPEN_DELEGATE_WRITE:
3062 res->delegation_type = FMODE_WRITE|FMODE_READ;
3063 if (decode_space_limit(xdr, &res->maxsize) < 0)
3064 return -EIO;
3066 return decode_ace(xdr, NULL, res->server->nfs4_state);
3069 static int decode_open(struct xdr_stream *xdr, struct nfs_openres *res)
3071 uint32_t *p;
3072 uint32_t bmlen;
3073 int status;
3075 status = decode_op_hdr(xdr, OP_OPEN);
3076 if (status)
3077 return status;
3078 READ_BUF(sizeof(res->stateid.data));
3079 COPYMEM(res->stateid.data, sizeof(res->stateid.data));
3081 decode_change_info(xdr, &res->cinfo);
3083 READ_BUF(8);
3084 READ32(res->rflags);
3085 READ32(bmlen);
3086 if (bmlen > 10)
3087 goto xdr_error;
3089 READ_BUF(bmlen << 2);
3090 p += bmlen;
3091 return decode_delegation(xdr, res);
3092 xdr_error:
3093 dprintk("%s: Bitmap too large! Length = %u\n", __FUNCTION__, bmlen);
3094 return -EIO;
3097 static int decode_open_confirm(struct xdr_stream *xdr, struct nfs_open_confirmres *res)
3099 uint32_t *p;
3100 int status;
3102 status = decode_op_hdr(xdr, OP_OPEN_CONFIRM);
3103 if (status)
3104 return status;
3105 READ_BUF(sizeof(res->stateid.data));
3106 COPYMEM(res->stateid.data, sizeof(res->stateid.data));
3107 return 0;
3110 static int decode_open_downgrade(struct xdr_stream *xdr, struct nfs_closeres *res)
3112 uint32_t *p;
3113 int status;
3115 status = decode_op_hdr(xdr, OP_OPEN_DOWNGRADE);
3116 if (status)
3117 return status;
3118 READ_BUF(sizeof(res->stateid.data));
3119 COPYMEM(res->stateid.data, sizeof(res->stateid.data));
3120 return 0;
3123 static int decode_putfh(struct xdr_stream *xdr)
3125 return decode_op_hdr(xdr, OP_PUTFH);
3128 static int decode_putrootfh(struct xdr_stream *xdr)
3130 return decode_op_hdr(xdr, OP_PUTROOTFH);
3133 static int decode_read(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs_readres *res)
3135 struct kvec *iov = req->rq_rcv_buf.head;
3136 uint32_t *p;
3137 uint32_t count, eof, recvd, hdrlen;
3138 int status;
3140 status = decode_op_hdr(xdr, OP_READ);
3141 if (status)
3142 return status;
3143 READ_BUF(8);
3144 READ32(eof);
3145 READ32(count);
3146 hdrlen = (u8 *) p - (u8 *) iov->iov_base;
3147 recvd = req->rq_rcv_buf.len - hdrlen;
3148 if (count > recvd) {
3149 printk(KERN_WARNING "NFS: server cheating in read reply: "
3150 "count %u > recvd %u\n", count, recvd);
3151 count = recvd;
3152 eof = 0;
3154 xdr_read_pages(xdr, count);
3155 res->eof = eof;
3156 res->count = count;
3157 return 0;
3160 static int decode_readdir(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs4_readdir_res *readdir)
3162 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
3163 struct page *page = *rcvbuf->pages;
3164 struct kvec *iov = rcvbuf->head;
3165 unsigned int nr, pglen = rcvbuf->page_len;
3166 uint32_t *end, *entry, *p, *kaddr;
3167 uint32_t len, attrlen;
3168 int hdrlen, recvd, status;
3170 status = decode_op_hdr(xdr, OP_READDIR);
3171 if (status)
3172 return status;
3173 READ_BUF(8);
3174 COPYMEM(readdir->verifier.data, 8);
3175 dprintk("%s: verifier = 0x%x%x\n",
3176 __FUNCTION__,
3177 ((u32 *)readdir->verifier.data)[0],
3178 ((u32 *)readdir->verifier.data)[1]);
3181 hdrlen = (char *) p - (char *) iov->iov_base;
3182 recvd = rcvbuf->len - hdrlen;
3183 if (pglen > recvd)
3184 pglen = recvd;
3185 xdr_read_pages(xdr, pglen);
3187 BUG_ON(pglen + readdir->pgbase > PAGE_CACHE_SIZE);
3188 kaddr = p = (uint32_t *) kmap_atomic(page, KM_USER0);
3189 end = (uint32_t *) ((char *)p + pglen + readdir->pgbase);
3190 entry = p;
3191 for (nr = 0; *p++; nr++) {
3192 if (p + 3 > end)
3193 goto short_pkt;
3194 dprintk("cookie = %Lu, ", *((unsigned long long *)p));
3195 p += 2; /* cookie */
3196 len = ntohl(*p++); /* filename length */
3197 if (len > NFS4_MAXNAMLEN) {
3198 printk(KERN_WARNING "NFS: giant filename in readdir (len 0x%x)\n", len);
3199 goto err_unmap;
3201 dprintk("filename = %*s\n", len, (char *)p);
3202 p += XDR_QUADLEN(len);
3203 if (p + 1 > end)
3204 goto short_pkt;
3205 len = ntohl(*p++); /* bitmap length */
3206 p += len;
3207 if (p + 1 > end)
3208 goto short_pkt;
3209 attrlen = XDR_QUADLEN(ntohl(*p++));
3210 p += attrlen; /* attributes */
3211 if (p + 2 > end)
3212 goto short_pkt;
3213 entry = p;
3215 if (!nr && (entry[0] != 0 || entry[1] == 0))
3216 goto short_pkt;
3217 out:
3218 kunmap_atomic(kaddr, KM_USER0);
3219 return 0;
3220 short_pkt:
3221 dprintk("%s: short packet at entry %d\n", __FUNCTION__, nr);
3222 entry[0] = entry[1] = 0;
3223 /* truncate listing ? */
3224 if (!nr) {
3225 printk(KERN_NOTICE "NFS: readdir reply truncated!\n");
3226 entry[1] = 1;
3228 goto out;
3229 err_unmap:
3230 kunmap_atomic(kaddr, KM_USER0);
3231 return -errno_NFSERR_IO;
3234 static int decode_readlink(struct xdr_stream *xdr, struct rpc_rqst *req)
3236 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
3237 struct kvec *iov = rcvbuf->head;
3238 int hdrlen, len, recvd;
3239 uint32_t *p;
3240 char *kaddr;
3241 int status;
3243 status = decode_op_hdr(xdr, OP_READLINK);
3244 if (status)
3245 return status;
3247 /* Convert length of symlink */
3248 READ_BUF(4);
3249 READ32(len);
3250 if (len >= rcvbuf->page_len || len <= 0) {
3251 dprintk(KERN_WARNING "nfs: server returned giant symlink!\n");
3252 return -ENAMETOOLONG;
3254 hdrlen = (char *) xdr->p - (char *) iov->iov_base;
3255 recvd = req->rq_rcv_buf.len - hdrlen;
3256 if (recvd < len) {
3257 printk(KERN_WARNING "NFS: server cheating in readlink reply: "
3258 "count %u > recvd %u\n", len, recvd);
3259 return -EIO;
3261 xdr_read_pages(xdr, len);
3263 * The XDR encode routine has set things up so that
3264 * the link text will be copied directly into the
3265 * buffer. We just have to do overflow-checking,
3266 * and and null-terminate the text (the VFS expects
3267 * null-termination).
3269 kaddr = (char *)kmap_atomic(rcvbuf->pages[0], KM_USER0);
3270 kaddr[len+rcvbuf->page_base] = '\0';
3271 kunmap_atomic(kaddr, KM_USER0);
3272 return 0;
3275 static int decode_remove(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
3277 int status;
3279 status = decode_op_hdr(xdr, OP_REMOVE);
3280 if (status)
3281 goto out;
3282 status = decode_change_info(xdr, cinfo);
3283 out:
3284 return status;
3287 static int decode_rename(struct xdr_stream *xdr, struct nfs4_change_info *old_cinfo,
3288 struct nfs4_change_info *new_cinfo)
3290 int status;
3292 status = decode_op_hdr(xdr, OP_RENAME);
3293 if (status)
3294 goto out;
3295 if ((status = decode_change_info(xdr, old_cinfo)))
3296 goto out;
3297 status = decode_change_info(xdr, new_cinfo);
3298 out:
3299 return status;
3302 static int decode_renew(struct xdr_stream *xdr)
3304 return decode_op_hdr(xdr, OP_RENEW);
3307 static int
3308 decode_restorefh(struct xdr_stream *xdr)
3310 return decode_op_hdr(xdr, OP_RESTOREFH);
3313 static int decode_getacl(struct xdr_stream *xdr, struct rpc_rqst *req,
3314 size_t *acl_len)
3316 uint32_t *savep;
3317 uint32_t attrlen,
3318 bitmap[2] = {0};
3319 struct kvec *iov = req->rq_rcv_buf.head;
3320 int status;
3322 *acl_len = 0;
3323 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3324 goto out;
3325 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3326 goto out;
3327 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3328 goto out;
3330 if (unlikely(bitmap[0] & (FATTR4_WORD0_ACL - 1U)))
3331 return -EIO;
3332 if (likely(bitmap[0] & FATTR4_WORD0_ACL)) {
3333 int hdrlen, recvd;
3335 /* We ignore &savep and don't do consistency checks on
3336 * the attr length. Let userspace figure it out.... */
3337 hdrlen = (u8 *)xdr->p - (u8 *)iov->iov_base;
3338 recvd = req->rq_rcv_buf.len - hdrlen;
3339 if (attrlen > recvd) {
3340 printk(KERN_WARNING "NFS: server cheating in getattr"
3341 " acl reply: attrlen %u > recvd %u\n",
3342 attrlen, recvd);
3343 return -EINVAL;
3345 if (attrlen <= *acl_len)
3346 xdr_read_pages(xdr, attrlen);
3347 *acl_len = attrlen;
3348 } else
3349 status = -EOPNOTSUPP;
3351 out:
3352 return status;
3355 static int
3356 decode_savefh(struct xdr_stream *xdr)
3358 return decode_op_hdr(xdr, OP_SAVEFH);
3361 static int decode_setattr(struct xdr_stream *xdr, struct nfs_setattrres *res)
3363 uint32_t *p;
3364 uint32_t bmlen;
3365 int status;
3368 status = decode_op_hdr(xdr, OP_SETATTR);
3369 if (status)
3370 return status;
3371 READ_BUF(4);
3372 READ32(bmlen);
3373 READ_BUF(bmlen << 2);
3374 return 0;
3377 static int decode_setclientid(struct xdr_stream *xdr, struct nfs4_client *clp)
3379 uint32_t *p;
3380 uint32_t opnum;
3381 int32_t nfserr;
3383 READ_BUF(8);
3384 READ32(opnum);
3385 if (opnum != OP_SETCLIENTID) {
3386 printk(KERN_NOTICE
3387 "nfs4_decode_setclientid: Server returned operation"
3388 " %d\n", opnum);
3389 return -EIO;
3391 READ32(nfserr);
3392 if (nfserr == NFS_OK) {
3393 READ_BUF(8 + sizeof(clp->cl_confirm.data));
3394 READ64(clp->cl_clientid);
3395 COPYMEM(clp->cl_confirm.data, sizeof(clp->cl_confirm.data));
3396 } else if (nfserr == NFSERR_CLID_INUSE) {
3397 uint32_t len;
3399 /* skip netid string */
3400 READ_BUF(4);
3401 READ32(len);
3402 READ_BUF(len);
3404 /* skip uaddr string */
3405 READ_BUF(4);
3406 READ32(len);
3407 READ_BUF(len);
3408 return -NFSERR_CLID_INUSE;
3409 } else
3410 return -nfs_stat_to_errno(nfserr);
3412 return 0;
3415 static int decode_setclientid_confirm(struct xdr_stream *xdr)
3417 return decode_op_hdr(xdr, OP_SETCLIENTID_CONFIRM);
3420 static int decode_write(struct xdr_stream *xdr, struct nfs_writeres *res)
3422 uint32_t *p;
3423 int status;
3425 status = decode_op_hdr(xdr, OP_WRITE);
3426 if (status)
3427 return status;
3429 READ_BUF(16);
3430 READ32(res->count);
3431 READ32(res->verf->committed);
3432 COPYMEM(res->verf->verifier, 8);
3433 return 0;
3436 static int decode_delegreturn(struct xdr_stream *xdr)
3438 return decode_op_hdr(xdr, OP_DELEGRETURN);
3442 * Decode OPEN_DOWNGRADE response
3444 static int nfs4_xdr_dec_open_downgrade(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_closeres *res)
3446 struct xdr_stream xdr;
3447 struct compound_hdr hdr;
3448 int status;
3450 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3451 status = decode_compound_hdr(&xdr, &hdr);
3452 if (status)
3453 goto out;
3454 status = decode_putfh(&xdr);
3455 if (status)
3456 goto out;
3457 status = decode_open_downgrade(&xdr, res);
3458 if (status != 0)
3459 goto out;
3460 decode_getfattr(&xdr, res->fattr, res->server);
3461 out:
3462 return status;
3466 * END OF "GENERIC" DECODE ROUTINES.
3470 * Decode ACCESS response
3472 static int nfs4_xdr_dec_access(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_accessres *res)
3474 struct xdr_stream xdr;
3475 struct compound_hdr hdr;
3476 int status;
3478 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3479 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3480 goto out;
3481 if ((status = decode_putfh(&xdr)) == 0)
3482 status = decode_access(&xdr, res);
3483 out:
3484 return status;
3488 * Decode LOOKUP response
3490 static int nfs4_xdr_dec_lookup(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_lookup_res *res)
3492 struct xdr_stream xdr;
3493 struct compound_hdr hdr;
3494 int status;
3496 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3497 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3498 goto out;
3499 if ((status = decode_putfh(&xdr)) != 0)
3500 goto out;
3501 if ((status = decode_lookup(&xdr)) != 0)
3502 goto out;
3503 if ((status = decode_getfh(&xdr, res->fh)) != 0)
3504 goto out;
3505 status = decode_getfattr(&xdr, res->fattr, res->server);
3506 out:
3507 return status;
3511 * Decode LOOKUP_ROOT response
3513 static int nfs4_xdr_dec_lookup_root(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_lookup_res *res)
3515 struct xdr_stream xdr;
3516 struct compound_hdr hdr;
3517 int status;
3519 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3520 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3521 goto out;
3522 if ((status = decode_putrootfh(&xdr)) != 0)
3523 goto out;
3524 if ((status = decode_getfh(&xdr, res->fh)) == 0)
3525 status = decode_getfattr(&xdr, res->fattr, res->server);
3526 out:
3527 return status;
3531 * Decode REMOVE response
3533 static int nfs4_xdr_dec_remove(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_remove_res *res)
3535 struct xdr_stream xdr;
3536 struct compound_hdr hdr;
3537 int status;
3539 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3540 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3541 goto out;
3542 if ((status = decode_putfh(&xdr)) != 0)
3543 goto out;
3544 if ((status = decode_remove(&xdr, &res->cinfo)) != 0)
3545 goto out;
3546 decode_getfattr(&xdr, res->dir_attr, res->server);
3547 out:
3548 return status;
3552 * Decode RENAME response
3554 static int nfs4_xdr_dec_rename(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_rename_res *res)
3556 struct xdr_stream xdr;
3557 struct compound_hdr hdr;
3558 int status;
3560 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3561 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3562 goto out;
3563 if ((status = decode_putfh(&xdr)) != 0)
3564 goto out;
3565 if ((status = decode_savefh(&xdr)) != 0)
3566 goto out;
3567 if ((status = decode_putfh(&xdr)) != 0)
3568 goto out;
3569 if ((status = decode_rename(&xdr, &res->old_cinfo, &res->new_cinfo)) != 0)
3570 goto out;
3571 /* Current FH is target directory */
3572 if (decode_getfattr(&xdr, res->new_fattr, res->server) != 0)
3573 goto out;
3574 if ((status = decode_restorefh(&xdr)) != 0)
3575 goto out;
3576 decode_getfattr(&xdr, res->old_fattr, res->server);
3577 out:
3578 return status;
3582 * Decode LINK response
3584 static int nfs4_xdr_dec_link(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_link_res *res)
3586 struct xdr_stream xdr;
3587 struct compound_hdr hdr;
3588 int status;
3590 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3591 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3592 goto out;
3593 if ((status = decode_putfh(&xdr)) != 0)
3594 goto out;
3595 if ((status = decode_savefh(&xdr)) != 0)
3596 goto out;
3597 if ((status = decode_putfh(&xdr)) != 0)
3598 goto out;
3599 if ((status = decode_link(&xdr, &res->cinfo)) != 0)
3600 goto out;
3602 * Note order: OP_LINK leaves the directory as the current
3603 * filehandle.
3605 if (decode_getfattr(&xdr, res->dir_attr, res->server) != 0)
3606 goto out;
3607 if ((status = decode_restorefh(&xdr)) != 0)
3608 goto out;
3609 decode_getfattr(&xdr, res->fattr, res->server);
3610 out:
3611 return status;
3615 * Decode CREATE response
3617 static int nfs4_xdr_dec_create(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_create_res *res)
3619 struct xdr_stream xdr;
3620 struct compound_hdr hdr;
3621 int status;
3623 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3624 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3625 goto out;
3626 if ((status = decode_putfh(&xdr)) != 0)
3627 goto out;
3628 if ((status = decode_savefh(&xdr)) != 0)
3629 goto out;
3630 if ((status = decode_create(&xdr,&res->dir_cinfo)) != 0)
3631 goto out;
3632 if ((status = decode_getfh(&xdr, res->fh)) != 0)
3633 goto out;
3634 if (decode_getfattr(&xdr, res->fattr, res->server) != 0)
3635 goto out;
3636 if ((status = decode_restorefh(&xdr)) != 0)
3637 goto out;
3638 decode_getfattr(&xdr, res->dir_fattr, res->server);
3639 out:
3640 return status;
3644 * Decode SYMLINK response
3646 static int nfs4_xdr_dec_symlink(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_create_res *res)
3648 return nfs4_xdr_dec_create(rqstp, p, res);
3652 * Decode GETATTR response
3654 static int nfs4_xdr_dec_getattr(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_getattr_res *res)
3656 struct xdr_stream xdr;
3657 struct compound_hdr hdr;
3658 int status;
3660 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3661 status = decode_compound_hdr(&xdr, &hdr);
3662 if (status)
3663 goto out;
3664 status = decode_putfh(&xdr);
3665 if (status)
3666 goto out;
3667 status = decode_getfattr(&xdr, res->fattr, res->server);
3668 out:
3669 return status;
3674 * Encode an SETACL request
3676 static int
3677 nfs4_xdr_enc_setacl(struct rpc_rqst *req, uint32_t *p, struct nfs_setaclargs *args)
3679 struct xdr_stream xdr;
3680 struct compound_hdr hdr = {
3681 .nops = 2,
3683 int status;
3685 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
3686 encode_compound_hdr(&xdr, &hdr);
3687 status = encode_putfh(&xdr, args->fh);
3688 if (status)
3689 goto out;
3690 status = encode_setacl(&xdr, args);
3691 out:
3692 return status;
3695 * Decode SETACL response
3697 static int
3698 nfs4_xdr_dec_setacl(struct rpc_rqst *rqstp, uint32_t *p, void *res)
3700 struct xdr_stream xdr;
3701 struct compound_hdr hdr;
3702 int status;
3704 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3705 status = decode_compound_hdr(&xdr, &hdr);
3706 if (status)
3707 goto out;
3708 status = decode_putfh(&xdr);
3709 if (status)
3710 goto out;
3711 status = decode_setattr(&xdr, res);
3712 out:
3713 return status;
3717 * Decode GETACL response
3719 static int
3720 nfs4_xdr_dec_getacl(struct rpc_rqst *rqstp, uint32_t *p, size_t *acl_len)
3722 struct xdr_stream xdr;
3723 struct compound_hdr hdr;
3724 int status;
3726 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3727 status = decode_compound_hdr(&xdr, &hdr);
3728 if (status)
3729 goto out;
3730 status = decode_putfh(&xdr);
3731 if (status)
3732 goto out;
3733 status = decode_getacl(&xdr, rqstp, acl_len);
3735 out:
3736 return status;
3740 * Decode CLOSE response
3742 static int nfs4_xdr_dec_close(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_closeres *res)
3744 struct xdr_stream xdr;
3745 struct compound_hdr hdr;
3746 int status;
3748 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3749 status = decode_compound_hdr(&xdr, &hdr);
3750 if (status)
3751 goto out;
3752 status = decode_putfh(&xdr);
3753 if (status)
3754 goto out;
3755 status = decode_close(&xdr, res);
3756 if (status != 0)
3757 goto out;
3759 * Note: Server may do delete on close for this file
3760 * in which case the getattr call will fail with
3761 * an ESTALE error. Shouldn't be a problem,
3762 * though, since fattr->valid will remain unset.
3764 decode_getfattr(&xdr, res->fattr, res->server);
3765 out:
3766 return status;
3770 * Decode OPEN response
3772 static int nfs4_xdr_dec_open(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_openres *res)
3774 struct xdr_stream xdr;
3775 struct compound_hdr hdr;
3776 int status;
3778 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3779 status = decode_compound_hdr(&xdr, &hdr);
3780 if (status)
3781 goto out;
3782 status = decode_putfh(&xdr);
3783 if (status)
3784 goto out;
3785 status = decode_savefh(&xdr);
3786 if (status)
3787 goto out;
3788 status = decode_open(&xdr, res);
3789 if (status)
3790 goto out;
3791 status = decode_getfh(&xdr, &res->fh);
3792 if (status)
3793 goto out;
3794 if (decode_getfattr(&xdr, res->f_attr, res->server) != 0)
3795 goto out;
3796 if ((status = decode_restorefh(&xdr)) != 0)
3797 goto out;
3798 decode_getfattr(&xdr, res->dir_attr, res->server);
3799 out:
3800 return status;
3804 * Decode OPEN_CONFIRM response
3806 static int nfs4_xdr_dec_open_confirm(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_open_confirmres *res)
3808 struct xdr_stream xdr;
3809 struct compound_hdr hdr;
3810 int status;
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_open_confirm(&xdr, res);
3820 out:
3821 return status;
3825 * Decode OPEN response
3827 static int nfs4_xdr_dec_open_noattr(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_openres *res)
3829 struct xdr_stream xdr;
3830 struct compound_hdr hdr;
3831 int status;
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_open(&xdr, res);
3841 if (status)
3842 goto out;
3843 decode_getfattr(&xdr, res->f_attr, res->server);
3844 out:
3845 return status;
3849 * Decode SETATTR response
3851 static int nfs4_xdr_dec_setattr(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_setattrres *res)
3853 struct xdr_stream xdr;
3854 struct compound_hdr hdr;
3855 int status;
3857 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3858 status = decode_compound_hdr(&xdr, &hdr);
3859 if (status)
3860 goto out;
3861 status = decode_putfh(&xdr);
3862 if (status)
3863 goto out;
3864 status = decode_setattr(&xdr, res);
3865 if (status)
3866 goto out;
3867 status = decode_getfattr(&xdr, res->fattr, res->server);
3868 if (status == NFS4ERR_DELAY)
3869 status = 0;
3870 out:
3871 return status;
3875 * Decode LOCK response
3877 static int nfs4_xdr_dec_lock(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_lock_res *res)
3879 struct xdr_stream xdr;
3880 struct compound_hdr hdr;
3881 int status;
3883 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3884 status = decode_compound_hdr(&xdr, &hdr);
3885 if (status)
3886 goto out;
3887 status = decode_putfh(&xdr);
3888 if (status)
3889 goto out;
3890 status = decode_lock(&xdr, res);
3891 out:
3892 return status;
3896 * Decode LOCKT response
3898 static int nfs4_xdr_dec_lockt(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_lockt_res *res)
3900 struct xdr_stream xdr;
3901 struct compound_hdr hdr;
3902 int status;
3904 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3905 status = decode_compound_hdr(&xdr, &hdr);
3906 if (status)
3907 goto out;
3908 status = decode_putfh(&xdr);
3909 if (status)
3910 goto out;
3911 status = decode_lockt(&xdr, res);
3912 out:
3913 return status;
3917 * Decode LOCKU response
3919 static int nfs4_xdr_dec_locku(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_locku_res *res)
3921 struct xdr_stream xdr;
3922 struct compound_hdr hdr;
3923 int status;
3925 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3926 status = decode_compound_hdr(&xdr, &hdr);
3927 if (status)
3928 goto out;
3929 status = decode_putfh(&xdr);
3930 if (status)
3931 goto out;
3932 status = decode_locku(&xdr, res);
3933 out:
3934 return status;
3938 * Decode READLINK response
3940 static int nfs4_xdr_dec_readlink(struct rpc_rqst *rqstp, uint32_t *p, void *res)
3942 struct xdr_stream xdr;
3943 struct compound_hdr hdr;
3944 int status;
3946 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3947 status = decode_compound_hdr(&xdr, &hdr);
3948 if (status)
3949 goto out;
3950 status = decode_putfh(&xdr);
3951 if (status)
3952 goto out;
3953 status = decode_readlink(&xdr, rqstp);
3954 out:
3955 return status;
3959 * Decode READDIR response
3961 static int nfs4_xdr_dec_readdir(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_readdir_res *res)
3963 struct xdr_stream xdr;
3964 struct compound_hdr hdr;
3965 int status;
3967 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3968 status = decode_compound_hdr(&xdr, &hdr);
3969 if (status)
3970 goto out;
3971 status = decode_putfh(&xdr);
3972 if (status)
3973 goto out;
3974 status = decode_readdir(&xdr, rqstp, res);
3975 out:
3976 return status;
3980 * Decode Read response
3982 static int nfs4_xdr_dec_read(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_readres *res)
3984 struct xdr_stream xdr;
3985 struct compound_hdr hdr;
3986 int status;
3988 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3989 status = decode_compound_hdr(&xdr, &hdr);
3990 if (status)
3991 goto out;
3992 status = decode_putfh(&xdr);
3993 if (status)
3994 goto out;
3995 status = decode_read(&xdr, rqstp, res);
3996 if (!status)
3997 status = res->count;
3998 out:
3999 return status;
4003 * Decode WRITE response
4005 static int nfs4_xdr_dec_write(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_writeres *res)
4007 struct xdr_stream xdr;
4008 struct compound_hdr hdr;
4009 int status;
4011 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4012 status = decode_compound_hdr(&xdr, &hdr);
4013 if (status)
4014 goto out;
4015 status = decode_putfh(&xdr);
4016 if (status)
4017 goto out;
4018 status = decode_write(&xdr, res);
4019 if (status)
4020 goto out;
4021 decode_getfattr(&xdr, res->fattr, res->server);
4022 if (!status)
4023 status = res->count;
4024 out:
4025 return status;
4029 * Decode COMMIT response
4031 static int nfs4_xdr_dec_commit(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_writeres *res)
4033 struct xdr_stream xdr;
4034 struct compound_hdr hdr;
4035 int status;
4037 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4038 status = decode_compound_hdr(&xdr, &hdr);
4039 if (status)
4040 goto out;
4041 status = decode_putfh(&xdr);
4042 if (status)
4043 goto out;
4044 status = decode_commit(&xdr, res);
4045 if (status)
4046 goto out;
4047 decode_getfattr(&xdr, res->fattr, res->server);
4048 out:
4049 return status;
4053 * FSINFO request
4055 static int nfs4_xdr_dec_fsinfo(struct rpc_rqst *req, uint32_t *p, struct nfs_fsinfo *fsinfo)
4057 struct xdr_stream xdr;
4058 struct compound_hdr hdr;
4059 int status;
4061 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4062 status = decode_compound_hdr(&xdr, &hdr);
4063 if (!status)
4064 status = decode_putfh(&xdr);
4065 if (!status)
4066 status = decode_fsinfo(&xdr, fsinfo);
4067 if (!status)
4068 status = -nfs_stat_to_errno(hdr.status);
4069 return status;
4073 * PATHCONF request
4075 static int nfs4_xdr_dec_pathconf(struct rpc_rqst *req, uint32_t *p, struct nfs_pathconf *pathconf)
4077 struct xdr_stream xdr;
4078 struct compound_hdr hdr;
4079 int status;
4081 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4082 status = decode_compound_hdr(&xdr, &hdr);
4083 if (!status)
4084 status = decode_putfh(&xdr);
4085 if (!status)
4086 status = decode_pathconf(&xdr, pathconf);
4087 return status;
4091 * STATFS request
4093 static int nfs4_xdr_dec_statfs(struct rpc_rqst *req, uint32_t *p, struct nfs_fsstat *fsstat)
4095 struct xdr_stream xdr;
4096 struct compound_hdr hdr;
4097 int status;
4099 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4100 status = decode_compound_hdr(&xdr, &hdr);
4101 if (!status)
4102 status = decode_putfh(&xdr);
4103 if (!status)
4104 status = decode_statfs(&xdr, fsstat);
4105 return status;
4109 * GETATTR_BITMAP request
4111 static int nfs4_xdr_dec_server_caps(struct rpc_rqst *req, uint32_t *p, struct nfs4_server_caps_res *res)
4113 struct xdr_stream xdr;
4114 struct compound_hdr hdr;
4115 int status;
4117 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4118 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
4119 goto out;
4120 if ((status = decode_putfh(&xdr)) != 0)
4121 goto out;
4122 status = decode_server_caps(&xdr, res);
4123 out:
4124 return status;
4128 * Decode RENEW response
4130 static int nfs4_xdr_dec_renew(struct rpc_rqst *rqstp, uint32_t *p, void *dummy)
4132 struct xdr_stream xdr;
4133 struct compound_hdr hdr;
4134 int status;
4136 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4137 status = decode_compound_hdr(&xdr, &hdr);
4138 if (!status)
4139 status = decode_renew(&xdr);
4140 return status;
4144 * a SETCLIENTID request
4146 static int nfs4_xdr_dec_setclientid(struct rpc_rqst *req, uint32_t *p,
4147 struct nfs4_client *clp)
4149 struct xdr_stream xdr;
4150 struct compound_hdr hdr;
4151 int status;
4153 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4154 status = decode_compound_hdr(&xdr, &hdr);
4155 if (!status)
4156 status = decode_setclientid(&xdr, clp);
4157 if (!status)
4158 status = -nfs_stat_to_errno(hdr.status);
4159 return status;
4163 * a SETCLIENTID_CONFIRM request
4165 static int nfs4_xdr_dec_setclientid_confirm(struct rpc_rqst *req, uint32_t *p, struct nfs_fsinfo *fsinfo)
4167 struct xdr_stream xdr;
4168 struct compound_hdr hdr;
4169 int status;
4171 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4172 status = decode_compound_hdr(&xdr, &hdr);
4173 if (!status)
4174 status = decode_setclientid_confirm(&xdr);
4175 if (!status)
4176 status = decode_putrootfh(&xdr);
4177 if (!status)
4178 status = decode_fsinfo(&xdr, fsinfo);
4179 if (!status)
4180 status = -nfs_stat_to_errno(hdr.status);
4181 return status;
4185 * DELEGRETURN request
4187 static int nfs4_xdr_dec_delegreturn(struct rpc_rqst *rqstp, uint32_t *p, void *dummy)
4189 struct xdr_stream xdr;
4190 struct compound_hdr hdr;
4191 int status;
4193 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4194 status = decode_compound_hdr(&xdr, &hdr);
4195 if (status == 0) {
4196 status = decode_putfh(&xdr);
4197 if (status == 0)
4198 status = decode_delegreturn(&xdr);
4200 return status;
4203 uint32_t *nfs4_decode_dirent(uint32_t *p, struct nfs_entry *entry, int plus)
4205 uint32_t bitmap[2] = {0};
4206 uint32_t len;
4208 if (!*p++) {
4209 if (!*p)
4210 return ERR_PTR(-EAGAIN);
4211 entry->eof = 1;
4212 return ERR_PTR(-EBADCOOKIE);
4215 entry->prev_cookie = entry->cookie;
4216 p = xdr_decode_hyper(p, &entry->cookie);
4217 entry->len = ntohl(*p++);
4218 entry->name = (const char *) p;
4219 p += XDR_QUADLEN(entry->len);
4222 * In case the server doesn't return an inode number,
4223 * we fake one here. (We don't use inode number 0,
4224 * since glibc seems to choke on it...)
4226 entry->ino = 1;
4228 len = ntohl(*p++); /* bitmap length */
4229 if (len-- > 0) {
4230 bitmap[0] = ntohl(*p++);
4231 if (len-- > 0) {
4232 bitmap[1] = ntohl(*p++);
4233 p += len;
4236 len = XDR_QUADLEN(ntohl(*p++)); /* attribute buffer length */
4237 if (len > 0) {
4238 if (bitmap[0] & FATTR4_WORD0_RDATTR_ERROR) {
4239 bitmap[0] &= ~FATTR4_WORD0_RDATTR_ERROR;
4240 /* Ignore the return value of rdattr_error for now */
4241 p++;
4242 len--;
4244 if (bitmap[0] == 0 && bitmap[1] == FATTR4_WORD1_MOUNTED_ON_FILEID)
4245 xdr_decode_hyper(p, &entry->ino);
4246 else if (bitmap[0] == FATTR4_WORD0_FILEID)
4247 xdr_decode_hyper(p, &entry->ino);
4248 p += len;
4251 entry->eof = !p[0] && p[1];
4252 return p;
4256 * We need to translate between nfs status return values and
4257 * the local errno values which may not be the same.
4259 static struct {
4260 int stat;
4261 int errno;
4262 } nfs_errtbl[] = {
4263 { NFS4_OK, 0 },
4264 { NFS4ERR_PERM, EPERM },
4265 { NFS4ERR_NOENT, ENOENT },
4266 { NFS4ERR_IO, errno_NFSERR_IO },
4267 { NFS4ERR_NXIO, ENXIO },
4268 { NFS4ERR_ACCESS, EACCES },
4269 { NFS4ERR_EXIST, EEXIST },
4270 { NFS4ERR_XDEV, EXDEV },
4271 { NFS4ERR_NOTDIR, ENOTDIR },
4272 { NFS4ERR_ISDIR, EISDIR },
4273 { NFS4ERR_INVAL, EINVAL },
4274 { NFS4ERR_FBIG, EFBIG },
4275 { NFS4ERR_NOSPC, ENOSPC },
4276 { NFS4ERR_ROFS, EROFS },
4277 { NFS4ERR_MLINK, EMLINK },
4278 { NFS4ERR_NAMETOOLONG, ENAMETOOLONG },
4279 { NFS4ERR_NOTEMPTY, ENOTEMPTY },
4280 { NFS4ERR_DQUOT, EDQUOT },
4281 { NFS4ERR_STALE, ESTALE },
4282 { NFS4ERR_BADHANDLE, EBADHANDLE },
4283 { NFS4ERR_BADOWNER, EINVAL },
4284 { NFS4ERR_BADNAME, EINVAL },
4285 { NFS4ERR_BAD_COOKIE, EBADCOOKIE },
4286 { NFS4ERR_NOTSUPP, ENOTSUPP },
4287 { NFS4ERR_TOOSMALL, ETOOSMALL },
4288 { NFS4ERR_SERVERFAULT, ESERVERFAULT },
4289 { NFS4ERR_BADTYPE, EBADTYPE },
4290 { NFS4ERR_LOCKED, EAGAIN },
4291 { NFS4ERR_RESOURCE, EREMOTEIO },
4292 { NFS4ERR_SYMLINK, ELOOP },
4293 { NFS4ERR_OP_ILLEGAL, EOPNOTSUPP },
4294 { NFS4ERR_DEADLOCK, EDEADLK },
4295 { NFS4ERR_WRONGSEC, EPERM }, /* FIXME: this needs
4296 * to be handled by a
4297 * middle-layer.
4299 { -1, EIO }
4303 * Convert an NFS error code to a local one.
4304 * This one is used jointly by NFSv2 and NFSv3.
4306 static int
4307 nfs_stat_to_errno(int stat)
4309 int i;
4310 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
4311 if (nfs_errtbl[i].stat == stat)
4312 return nfs_errtbl[i].errno;
4314 if (stat <= 10000 || stat > 10100) {
4315 /* The server is looney tunes. */
4316 return ESERVERFAULT;
4318 /* If we cannot translate the error, the recovery routines should
4319 * handle it.
4320 * Note: remaining NFSv4 error codes have values > 10000, so should
4321 * not conflict with native Linux error codes.
4323 return stat;
4326 #ifndef MAX
4327 # define MAX(a, b) (((a) > (b))? (a) : (b))
4328 #endif
4330 #define PROC(proc, argtype, restype) \
4331 [NFSPROC4_CLNT_##proc] = { \
4332 .p_proc = NFSPROC4_COMPOUND, \
4333 .p_encode = (kxdrproc_t) nfs4_xdr_##argtype, \
4334 .p_decode = (kxdrproc_t) nfs4_xdr_##restype, \
4335 .p_bufsiz = MAX(NFS4_##argtype##_sz,NFS4_##restype##_sz) << 2, \
4338 struct rpc_procinfo nfs4_procedures[] = {
4339 PROC(READ, enc_read, dec_read),
4340 PROC(WRITE, enc_write, dec_write),
4341 PROC(COMMIT, enc_commit, dec_commit),
4342 PROC(OPEN, enc_open, dec_open),
4343 PROC(OPEN_CONFIRM, enc_open_confirm, dec_open_confirm),
4344 PROC(OPEN_NOATTR, enc_open_noattr, dec_open_noattr),
4345 PROC(OPEN_DOWNGRADE, enc_open_downgrade, dec_open_downgrade),
4346 PROC(CLOSE, enc_close, dec_close),
4347 PROC(SETATTR, enc_setattr, dec_setattr),
4348 PROC(FSINFO, enc_fsinfo, dec_fsinfo),
4349 PROC(RENEW, enc_renew, dec_renew),
4350 PROC(SETCLIENTID, enc_setclientid, dec_setclientid),
4351 PROC(SETCLIENTID_CONFIRM, enc_setclientid_confirm, dec_setclientid_confirm),
4352 PROC(LOCK, enc_lock, dec_lock),
4353 PROC(LOCKT, enc_lockt, dec_lockt),
4354 PROC(LOCKU, enc_locku, dec_locku),
4355 PROC(ACCESS, enc_access, dec_access),
4356 PROC(GETATTR, enc_getattr, dec_getattr),
4357 PROC(LOOKUP, enc_lookup, dec_lookup),
4358 PROC(LOOKUP_ROOT, enc_lookup_root, dec_lookup_root),
4359 PROC(REMOVE, enc_remove, dec_remove),
4360 PROC(RENAME, enc_rename, dec_rename),
4361 PROC(LINK, enc_link, dec_link),
4362 PROC(SYMLINK, enc_symlink, dec_symlink),
4363 PROC(CREATE, enc_create, dec_create),
4364 PROC(PATHCONF, enc_pathconf, dec_pathconf),
4365 PROC(STATFS, enc_statfs, dec_statfs),
4366 PROC(READLINK, enc_readlink, dec_readlink),
4367 PROC(READDIR, enc_readdir, dec_readdir),
4368 PROC(SERVER_CAPS, enc_server_caps, dec_server_caps),
4369 PROC(DELEGRETURN, enc_delegreturn, dec_delegreturn),
4370 PROC(GETACL, enc_getacl, dec_getacl),
4371 PROC(SETACL, enc_setacl, dec_setacl),
4374 struct rpc_version nfs_version4 = {
4375 .number = 4,
4376 .nrprocs = sizeof(nfs4_procedures)/sizeof(nfs4_procedures[0]),
4377 .procs = nfs4_procedures
4381 * Local variables:
4382 * c-basic-offset: 8
4383 * End: