add filesystem subtype support
[linux-2.6/openmoko-kernel.git] / fs / nfs / nfs4xdr.c
blobb8c28f2380a5bcf2aacb48cb45294df65379d21d
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 nfs4_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 1 + \
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_putfh_maxsz + \
391 encode_getattr_maxsz)
392 #define NFS4_dec_server_caps_sz (compound_decode_hdr_maxsz + \
393 decode_putfh_maxsz + \
394 decode_getattr_maxsz)
395 #define NFS4_enc_delegreturn_sz (compound_encode_hdr_maxsz + \
396 encode_putfh_maxsz + \
397 encode_delegreturn_maxsz + \
398 encode_getattr_maxsz)
399 #define NFS4_dec_delegreturn_sz (compound_decode_hdr_maxsz + \
400 decode_delegreturn_maxsz + \
401 decode_getattr_maxsz)
402 #define NFS4_enc_getacl_sz (compound_encode_hdr_maxsz + \
403 encode_putfh_maxsz + \
404 encode_getattr_maxsz)
405 #define NFS4_dec_getacl_sz (compound_decode_hdr_maxsz + \
406 decode_putfh_maxsz + \
407 op_decode_hdr_maxsz + \
408 nfs4_fattr_bitmap_maxsz + 1)
409 #define NFS4_enc_setacl_sz (compound_encode_hdr_maxsz + \
410 encode_putfh_maxsz + \
411 op_encode_hdr_maxsz + 4 + \
412 nfs4_fattr_bitmap_maxsz + 1)
413 #define NFS4_dec_setacl_sz (compound_decode_hdr_maxsz + \
414 decode_putfh_maxsz + \
415 op_decode_hdr_maxsz + nfs4_fattr_bitmap_maxsz)
416 #define NFS4_enc_fs_locations_sz \
417 (compound_encode_hdr_maxsz + \
418 encode_putfh_maxsz + \
419 encode_getattr_maxsz)
420 #define NFS4_dec_fs_locations_sz \
421 (compound_decode_hdr_maxsz + \
422 decode_putfh_maxsz + \
423 op_decode_hdr_maxsz + \
424 nfs4_fattr_bitmap_maxsz)
426 static struct {
427 unsigned int mode;
428 unsigned int nfs2type;
429 } nfs_type2fmt[] = {
430 { 0, NFNON },
431 { S_IFREG, NFREG },
432 { S_IFDIR, NFDIR },
433 { S_IFBLK, NFBLK },
434 { S_IFCHR, NFCHR },
435 { S_IFLNK, NFLNK },
436 { S_IFSOCK, NFSOCK },
437 { S_IFIFO, NFFIFO },
438 { 0, NFNON },
439 { 0, NFNON },
442 struct compound_hdr {
443 int32_t status;
444 uint32_t nops;
445 uint32_t taglen;
446 char * tag;
450 * START OF "GENERIC" ENCODE ROUTINES.
451 * These may look a little ugly since they are imported from a "generic"
452 * set of XDR encode/decode routines which are intended to be shared by
453 * all of our NFSv4 implementations (OpenBSD, MacOS X...).
455 * If the pain of reading these is too great, it should be a straightforward
456 * task to translate them into Linux-specific versions which are more
457 * consistent with the style used in NFSv2/v3...
459 #define WRITE32(n) *p++ = htonl(n)
460 #define WRITE64(n) do { \
461 *p++ = htonl((uint32_t)((n) >> 32)); \
462 *p++ = htonl((uint32_t)(n)); \
463 } while (0)
464 #define WRITEMEM(ptr,nbytes) do { \
465 p = xdr_encode_opaque_fixed(p, ptr, nbytes); \
466 } while (0)
468 #define RESERVE_SPACE(nbytes) do { \
469 p = xdr_reserve_space(xdr, nbytes); \
470 if (!p) printk("RESERVE_SPACE(%d) failed in function %s\n", (int) (nbytes), __FUNCTION__); \
471 BUG_ON(!p); \
472 } while (0)
474 static void encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
476 __be32 *p;
478 p = xdr_reserve_space(xdr, 4 + len);
479 BUG_ON(p == NULL);
480 xdr_encode_opaque(p, str, len);
483 static int encode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
485 __be32 *p;
487 dprintk("encode_compound: tag=%.*s\n", (int)hdr->taglen, hdr->tag);
488 BUG_ON(hdr->taglen > NFS4_MAXTAGLEN);
489 RESERVE_SPACE(12+(XDR_QUADLEN(hdr->taglen)<<2));
490 WRITE32(hdr->taglen);
491 WRITEMEM(hdr->tag, hdr->taglen);
492 WRITE32(NFS4_MINOR_VERSION);
493 WRITE32(hdr->nops);
494 return 0;
497 static void encode_nfs4_verifier(struct xdr_stream *xdr, const nfs4_verifier *verf)
499 __be32 *p;
501 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
502 BUG_ON(p == NULL);
503 xdr_encode_opaque_fixed(p, verf->data, NFS4_VERIFIER_SIZE);
506 static int encode_attrs(struct xdr_stream *xdr, const struct iattr *iap, const struct nfs_server *server)
508 char owner_name[IDMAP_NAMESZ];
509 char owner_group[IDMAP_NAMESZ];
510 int owner_namelen = 0;
511 int owner_grouplen = 0;
512 __be32 *p;
513 __be32 *q;
514 int len;
515 uint32_t bmval0 = 0;
516 uint32_t bmval1 = 0;
517 int status;
520 * We reserve enough space to write the entire attribute buffer at once.
521 * In the worst-case, this would be
522 * 12(bitmap) + 4(attrlen) + 8(size) + 4(mode) + 4(atime) + 4(mtime)
523 * = 36 bytes, plus any contribution from variable-length fields
524 * such as owner/group.
526 len = 16;
528 /* Sigh */
529 if (iap->ia_valid & ATTR_SIZE)
530 len += 8;
531 if (iap->ia_valid & ATTR_MODE)
532 len += 4;
533 if (iap->ia_valid & ATTR_UID) {
534 owner_namelen = nfs_map_uid_to_name(server->nfs_client, iap->ia_uid, owner_name);
535 if (owner_namelen < 0) {
536 printk(KERN_WARNING "nfs: couldn't resolve uid %d to string\n",
537 iap->ia_uid);
538 /* XXX */
539 strcpy(owner_name, "nobody");
540 owner_namelen = sizeof("nobody") - 1;
541 /* goto out; */
543 len += 4 + (XDR_QUADLEN(owner_namelen) << 2);
545 if (iap->ia_valid & ATTR_GID) {
546 owner_grouplen = nfs_map_gid_to_group(server->nfs_client, iap->ia_gid, owner_group);
547 if (owner_grouplen < 0) {
548 printk(KERN_WARNING "nfs4: couldn't resolve gid %d to string\n",
549 iap->ia_gid);
550 strcpy(owner_group, "nobody");
551 owner_grouplen = sizeof("nobody") - 1;
552 /* goto out; */
554 len += 4 + (XDR_QUADLEN(owner_grouplen) << 2);
556 if (iap->ia_valid & ATTR_ATIME_SET)
557 len += 16;
558 else if (iap->ia_valid & ATTR_ATIME)
559 len += 4;
560 if (iap->ia_valid & ATTR_MTIME_SET)
561 len += 16;
562 else if (iap->ia_valid & ATTR_MTIME)
563 len += 4;
564 RESERVE_SPACE(len);
567 * We write the bitmap length now, but leave the bitmap and the attribute
568 * buffer length to be backfilled at the end of this routine.
570 WRITE32(2);
571 q = p;
572 p += 3;
574 if (iap->ia_valid & ATTR_SIZE) {
575 bmval0 |= FATTR4_WORD0_SIZE;
576 WRITE64(iap->ia_size);
578 if (iap->ia_valid & ATTR_MODE) {
579 bmval1 |= FATTR4_WORD1_MODE;
580 WRITE32(iap->ia_mode & S_IALLUGO);
582 if (iap->ia_valid & ATTR_UID) {
583 bmval1 |= FATTR4_WORD1_OWNER;
584 WRITE32(owner_namelen);
585 WRITEMEM(owner_name, owner_namelen);
587 if (iap->ia_valid & ATTR_GID) {
588 bmval1 |= FATTR4_WORD1_OWNER_GROUP;
589 WRITE32(owner_grouplen);
590 WRITEMEM(owner_group, owner_grouplen);
592 if (iap->ia_valid & ATTR_ATIME_SET) {
593 bmval1 |= FATTR4_WORD1_TIME_ACCESS_SET;
594 WRITE32(NFS4_SET_TO_CLIENT_TIME);
595 WRITE32(0);
596 WRITE32(iap->ia_mtime.tv_sec);
597 WRITE32(iap->ia_mtime.tv_nsec);
599 else if (iap->ia_valid & ATTR_ATIME) {
600 bmval1 |= FATTR4_WORD1_TIME_ACCESS_SET;
601 WRITE32(NFS4_SET_TO_SERVER_TIME);
603 if (iap->ia_valid & ATTR_MTIME_SET) {
604 bmval1 |= FATTR4_WORD1_TIME_MODIFY_SET;
605 WRITE32(NFS4_SET_TO_CLIENT_TIME);
606 WRITE32(0);
607 WRITE32(iap->ia_mtime.tv_sec);
608 WRITE32(iap->ia_mtime.tv_nsec);
610 else if (iap->ia_valid & ATTR_MTIME) {
611 bmval1 |= FATTR4_WORD1_TIME_MODIFY_SET;
612 WRITE32(NFS4_SET_TO_SERVER_TIME);
616 * Now we backfill the bitmap and the attribute buffer length.
618 if (len != ((char *)p - (char *)q) + 4) {
619 printk ("encode_attr: Attr length calculation error! %u != %Zu\n",
620 len, ((char *)p - (char *)q) + 4);
621 BUG();
623 len = (char *)p - (char *)q - 12;
624 *q++ = htonl(bmval0);
625 *q++ = htonl(bmval1);
626 *q++ = htonl(len);
628 status = 0;
629 /* out: */
630 return status;
633 static int encode_access(struct xdr_stream *xdr, u32 access)
635 __be32 *p;
637 RESERVE_SPACE(8);
638 WRITE32(OP_ACCESS);
639 WRITE32(access);
641 return 0;
644 static int encode_close(struct xdr_stream *xdr, const struct nfs_closeargs *arg)
646 __be32 *p;
648 RESERVE_SPACE(8+sizeof(arg->stateid->data));
649 WRITE32(OP_CLOSE);
650 WRITE32(arg->seqid->sequence->counter);
651 WRITEMEM(arg->stateid->data, sizeof(arg->stateid->data));
653 return 0;
656 static int encode_commit(struct xdr_stream *xdr, const struct nfs_writeargs *args)
658 __be32 *p;
660 RESERVE_SPACE(16);
661 WRITE32(OP_COMMIT);
662 WRITE64(args->offset);
663 WRITE32(args->count);
665 return 0;
668 static int encode_create(struct xdr_stream *xdr, const struct nfs4_create_arg *create)
670 __be32 *p;
672 RESERVE_SPACE(8);
673 WRITE32(OP_CREATE);
674 WRITE32(create->ftype);
676 switch (create->ftype) {
677 case NF4LNK:
678 RESERVE_SPACE(4);
679 WRITE32(create->u.symlink.len);
680 xdr_write_pages(xdr, create->u.symlink.pages, 0, create->u.symlink.len);
681 break;
683 case NF4BLK: case NF4CHR:
684 RESERVE_SPACE(8);
685 WRITE32(create->u.device.specdata1);
686 WRITE32(create->u.device.specdata2);
687 break;
689 default:
690 break;
693 RESERVE_SPACE(4 + create->name->len);
694 WRITE32(create->name->len);
695 WRITEMEM(create->name->name, create->name->len);
697 return encode_attrs(xdr, create->attrs, create->server);
700 static int encode_getattr_one(struct xdr_stream *xdr, uint32_t bitmap)
702 __be32 *p;
704 RESERVE_SPACE(12);
705 WRITE32(OP_GETATTR);
706 WRITE32(1);
707 WRITE32(bitmap);
708 return 0;
711 static int encode_getattr_two(struct xdr_stream *xdr, uint32_t bm0, uint32_t bm1)
713 __be32 *p;
715 RESERVE_SPACE(16);
716 WRITE32(OP_GETATTR);
717 WRITE32(2);
718 WRITE32(bm0);
719 WRITE32(bm1);
720 return 0;
723 static int encode_getfattr(struct xdr_stream *xdr, const u32* bitmask)
725 return encode_getattr_two(xdr,
726 bitmask[0] & nfs4_fattr_bitmap[0],
727 bitmask[1] & nfs4_fattr_bitmap[1]);
730 static int encode_fsinfo(struct xdr_stream *xdr, const u32* bitmask)
732 return encode_getattr_two(xdr, bitmask[0] & nfs4_fsinfo_bitmap[0],
733 bitmask[1] & nfs4_fsinfo_bitmap[1]);
736 static int encode_fs_locations(struct xdr_stream *xdr, const u32* bitmask)
738 return encode_getattr_two(xdr,
739 bitmask[0] & nfs4_fs_locations_bitmap[0],
740 bitmask[1] & nfs4_fs_locations_bitmap[1]);
743 static int encode_getfh(struct xdr_stream *xdr)
745 __be32 *p;
747 RESERVE_SPACE(4);
748 WRITE32(OP_GETFH);
750 return 0;
753 static int encode_link(struct xdr_stream *xdr, const struct qstr *name)
755 __be32 *p;
757 RESERVE_SPACE(8 + name->len);
758 WRITE32(OP_LINK);
759 WRITE32(name->len);
760 WRITEMEM(name->name, name->len);
762 return 0;
765 static inline int nfs4_lock_type(struct file_lock *fl, int block)
767 if ((fl->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) == F_RDLCK)
768 return block ? NFS4_READW_LT : NFS4_READ_LT;
769 return block ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
772 static inline uint64_t nfs4_lock_length(struct file_lock *fl)
774 if (fl->fl_end == OFFSET_MAX)
775 return ~(uint64_t)0;
776 return fl->fl_end - fl->fl_start + 1;
780 * opcode,type,reclaim,offset,length,new_lock_owner = 32
781 * open_seqid,open_stateid,lock_seqid,lock_owner.clientid, lock_owner.id = 40
783 static int encode_lock(struct xdr_stream *xdr, const struct nfs_lock_args *args)
785 __be32 *p;
787 RESERVE_SPACE(32);
788 WRITE32(OP_LOCK);
789 WRITE32(nfs4_lock_type(args->fl, args->block));
790 WRITE32(args->reclaim);
791 WRITE64(args->fl->fl_start);
792 WRITE64(nfs4_lock_length(args->fl));
793 WRITE32(args->new_lock_owner);
794 if (args->new_lock_owner){
795 RESERVE_SPACE(40);
796 WRITE32(args->open_seqid->sequence->counter);
797 WRITEMEM(args->open_stateid->data, sizeof(args->open_stateid->data));
798 WRITE32(args->lock_seqid->sequence->counter);
799 WRITE64(args->lock_owner.clientid);
800 WRITE32(4);
801 WRITE32(args->lock_owner.id);
803 else {
804 RESERVE_SPACE(20);
805 WRITEMEM(args->lock_stateid->data, sizeof(args->lock_stateid->data));
806 WRITE32(args->lock_seqid->sequence->counter);
809 return 0;
812 static int encode_lockt(struct xdr_stream *xdr, const struct nfs_lockt_args *args)
814 __be32 *p;
816 RESERVE_SPACE(40);
817 WRITE32(OP_LOCKT);
818 WRITE32(nfs4_lock_type(args->fl, 0));
819 WRITE64(args->fl->fl_start);
820 WRITE64(nfs4_lock_length(args->fl));
821 WRITE64(args->lock_owner.clientid);
822 WRITE32(4);
823 WRITE32(args->lock_owner.id);
825 return 0;
828 static int encode_locku(struct xdr_stream *xdr, const struct nfs_locku_args *args)
830 __be32 *p;
832 RESERVE_SPACE(44);
833 WRITE32(OP_LOCKU);
834 WRITE32(nfs4_lock_type(args->fl, 0));
835 WRITE32(args->seqid->sequence->counter);
836 WRITEMEM(args->stateid->data, sizeof(args->stateid->data));
837 WRITE64(args->fl->fl_start);
838 WRITE64(nfs4_lock_length(args->fl));
840 return 0;
843 static int encode_lookup(struct xdr_stream *xdr, const struct qstr *name)
845 int len = name->len;
846 __be32 *p;
848 RESERVE_SPACE(8 + len);
849 WRITE32(OP_LOOKUP);
850 WRITE32(len);
851 WRITEMEM(name->name, len);
853 return 0;
856 static void encode_share_access(struct xdr_stream *xdr, int open_flags)
858 __be32 *p;
860 RESERVE_SPACE(8);
861 switch (open_flags & (FMODE_READ|FMODE_WRITE)) {
862 case FMODE_READ:
863 WRITE32(NFS4_SHARE_ACCESS_READ);
864 break;
865 case FMODE_WRITE:
866 WRITE32(NFS4_SHARE_ACCESS_WRITE);
867 break;
868 case FMODE_READ|FMODE_WRITE:
869 WRITE32(NFS4_SHARE_ACCESS_BOTH);
870 break;
871 default:
872 BUG();
874 WRITE32(0); /* for linux, share_deny = 0 always */
877 static inline void encode_openhdr(struct xdr_stream *xdr, const struct nfs_openargs *arg)
879 __be32 *p;
881 * opcode 4, seqid 4, share_access 4, share_deny 4, clientid 8, ownerlen 4,
882 * owner 4 = 32
884 RESERVE_SPACE(8);
885 WRITE32(OP_OPEN);
886 WRITE32(arg->seqid->sequence->counter);
887 encode_share_access(xdr, arg->open_flags);
888 RESERVE_SPACE(16);
889 WRITE64(arg->clientid);
890 WRITE32(4);
891 WRITE32(arg->id);
894 static inline void encode_createmode(struct xdr_stream *xdr, const struct nfs_openargs *arg)
896 __be32 *p;
898 RESERVE_SPACE(4);
899 switch(arg->open_flags & O_EXCL) {
900 case 0:
901 WRITE32(NFS4_CREATE_UNCHECKED);
902 encode_attrs(xdr, arg->u.attrs, arg->server);
903 break;
904 default:
905 WRITE32(NFS4_CREATE_EXCLUSIVE);
906 encode_nfs4_verifier(xdr, &arg->u.verifier);
910 static void encode_opentype(struct xdr_stream *xdr, const struct nfs_openargs *arg)
912 __be32 *p;
914 RESERVE_SPACE(4);
915 switch (arg->open_flags & O_CREAT) {
916 case 0:
917 WRITE32(NFS4_OPEN_NOCREATE);
918 break;
919 default:
920 BUG_ON(arg->claim != NFS4_OPEN_CLAIM_NULL);
921 WRITE32(NFS4_OPEN_CREATE);
922 encode_createmode(xdr, arg);
926 static inline void encode_delegation_type(struct xdr_stream *xdr, int delegation_type)
928 __be32 *p;
930 RESERVE_SPACE(4);
931 switch (delegation_type) {
932 case 0:
933 WRITE32(NFS4_OPEN_DELEGATE_NONE);
934 break;
935 case FMODE_READ:
936 WRITE32(NFS4_OPEN_DELEGATE_READ);
937 break;
938 case FMODE_WRITE|FMODE_READ:
939 WRITE32(NFS4_OPEN_DELEGATE_WRITE);
940 break;
941 default:
942 BUG();
946 static inline void encode_claim_null(struct xdr_stream *xdr, const struct qstr *name)
948 __be32 *p;
950 RESERVE_SPACE(4);
951 WRITE32(NFS4_OPEN_CLAIM_NULL);
952 encode_string(xdr, name->len, name->name);
955 static inline void encode_claim_previous(struct xdr_stream *xdr, int type)
957 __be32 *p;
959 RESERVE_SPACE(4);
960 WRITE32(NFS4_OPEN_CLAIM_PREVIOUS);
961 encode_delegation_type(xdr, type);
964 static inline void encode_claim_delegate_cur(struct xdr_stream *xdr, const struct qstr *name, const nfs4_stateid *stateid)
966 __be32 *p;
968 RESERVE_SPACE(4+sizeof(stateid->data));
969 WRITE32(NFS4_OPEN_CLAIM_DELEGATE_CUR);
970 WRITEMEM(stateid->data, sizeof(stateid->data));
971 encode_string(xdr, name->len, name->name);
974 static int encode_open(struct xdr_stream *xdr, const struct nfs_openargs *arg)
976 encode_openhdr(xdr, arg);
977 encode_opentype(xdr, arg);
978 switch (arg->claim) {
979 case NFS4_OPEN_CLAIM_NULL:
980 encode_claim_null(xdr, arg->name);
981 break;
982 case NFS4_OPEN_CLAIM_PREVIOUS:
983 encode_claim_previous(xdr, arg->u.delegation_type);
984 break;
985 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
986 encode_claim_delegate_cur(xdr, arg->name, &arg->u.delegation);
987 break;
988 default:
989 BUG();
991 return 0;
994 static int encode_open_confirm(struct xdr_stream *xdr, const struct nfs_open_confirmargs *arg)
996 __be32 *p;
998 RESERVE_SPACE(8+sizeof(arg->stateid->data));
999 WRITE32(OP_OPEN_CONFIRM);
1000 WRITEMEM(arg->stateid->data, sizeof(arg->stateid->data));
1001 WRITE32(arg->seqid->sequence->counter);
1003 return 0;
1006 static int encode_open_downgrade(struct xdr_stream *xdr, const struct nfs_closeargs *arg)
1008 __be32 *p;
1010 RESERVE_SPACE(8+sizeof(arg->stateid->data));
1011 WRITE32(OP_OPEN_DOWNGRADE);
1012 WRITEMEM(arg->stateid->data, sizeof(arg->stateid->data));
1013 WRITE32(arg->seqid->sequence->counter);
1014 encode_share_access(xdr, arg->open_flags);
1015 return 0;
1018 static int
1019 encode_putfh(struct xdr_stream *xdr, const struct nfs_fh *fh)
1021 int len = fh->size;
1022 __be32 *p;
1024 RESERVE_SPACE(8 + len);
1025 WRITE32(OP_PUTFH);
1026 WRITE32(len);
1027 WRITEMEM(fh->data, len);
1029 return 0;
1032 static int encode_putrootfh(struct xdr_stream *xdr)
1034 __be32 *p;
1036 RESERVE_SPACE(4);
1037 WRITE32(OP_PUTROOTFH);
1039 return 0;
1042 static void encode_stateid(struct xdr_stream *xdr, const struct nfs_open_context *ctx)
1044 nfs4_stateid stateid;
1045 __be32 *p;
1047 RESERVE_SPACE(16);
1048 if (ctx->state != NULL) {
1049 nfs4_copy_stateid(&stateid, ctx->state, ctx->lockowner);
1050 WRITEMEM(stateid.data, sizeof(stateid.data));
1051 } else
1052 WRITEMEM(zero_stateid.data, sizeof(zero_stateid.data));
1055 static int encode_read(struct xdr_stream *xdr, const struct nfs_readargs *args)
1057 __be32 *p;
1059 RESERVE_SPACE(4);
1060 WRITE32(OP_READ);
1062 encode_stateid(xdr, args->context);
1064 RESERVE_SPACE(12);
1065 WRITE64(args->offset);
1066 WRITE32(args->count);
1068 return 0;
1071 static int encode_readdir(struct xdr_stream *xdr, const struct nfs4_readdir_arg *readdir, struct rpc_rqst *req)
1073 struct rpc_auth *auth = req->rq_task->tk_auth;
1074 uint32_t attrs[2] = {
1075 FATTR4_WORD0_RDATTR_ERROR|FATTR4_WORD0_FILEID,
1076 FATTR4_WORD1_MOUNTED_ON_FILEID,
1078 int replen;
1079 __be32 *p;
1081 RESERVE_SPACE(32+sizeof(nfs4_verifier));
1082 WRITE32(OP_READDIR);
1083 WRITE64(readdir->cookie);
1084 WRITEMEM(readdir->verifier.data, sizeof(readdir->verifier.data));
1085 WRITE32(readdir->count >> 1); /* We're not doing readdirplus */
1086 WRITE32(readdir->count);
1087 WRITE32(2);
1088 /* Switch to mounted_on_fileid if the server supports it */
1089 if (readdir->bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
1090 attrs[0] &= ~FATTR4_WORD0_FILEID;
1091 else
1092 attrs[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
1093 WRITE32(attrs[0] & readdir->bitmask[0]);
1094 WRITE32(attrs[1] & readdir->bitmask[1]);
1095 dprintk("%s: cookie = %Lu, verifier = 0x%x%x, bitmap = 0x%x%x\n",
1096 __FUNCTION__,
1097 (unsigned long long)readdir->cookie,
1098 ((u32 *)readdir->verifier.data)[0],
1099 ((u32 *)readdir->verifier.data)[1],
1100 attrs[0] & readdir->bitmask[0],
1101 attrs[1] & readdir->bitmask[1]);
1103 /* set up reply kvec
1104 * toplevel_status + taglen + rescount + OP_PUTFH + status
1105 * + OP_READDIR + status + verifer(2) = 9
1107 replen = (RPC_REPHDRSIZE + auth->au_rslack + 9) << 2;
1108 xdr_inline_pages(&req->rq_rcv_buf, replen, readdir->pages,
1109 readdir->pgbase, readdir->count);
1110 dprintk("%s: inlined page args = (%u, %p, %u, %u)\n",
1111 __FUNCTION__, replen, readdir->pages,
1112 readdir->pgbase, readdir->count);
1114 return 0;
1117 static int encode_readlink(struct xdr_stream *xdr, const struct nfs4_readlink *readlink, struct rpc_rqst *req)
1119 struct rpc_auth *auth = req->rq_task->tk_auth;
1120 unsigned int replen;
1121 __be32 *p;
1123 RESERVE_SPACE(4);
1124 WRITE32(OP_READLINK);
1126 /* set up reply kvec
1127 * toplevel_status + taglen + rescount + OP_PUTFH + status
1128 * + OP_READLINK + status + string length = 8
1130 replen = (RPC_REPHDRSIZE + auth->au_rslack + 8) << 2;
1131 xdr_inline_pages(&req->rq_rcv_buf, replen, readlink->pages,
1132 readlink->pgbase, readlink->pglen);
1134 return 0;
1137 static int encode_remove(struct xdr_stream *xdr, const struct qstr *name)
1139 __be32 *p;
1141 RESERVE_SPACE(8 + name->len);
1142 WRITE32(OP_REMOVE);
1143 WRITE32(name->len);
1144 WRITEMEM(name->name, name->len);
1146 return 0;
1149 static int encode_rename(struct xdr_stream *xdr, const struct qstr *oldname, const struct qstr *newname)
1151 __be32 *p;
1153 RESERVE_SPACE(8 + oldname->len);
1154 WRITE32(OP_RENAME);
1155 WRITE32(oldname->len);
1156 WRITEMEM(oldname->name, oldname->len);
1158 RESERVE_SPACE(4 + newname->len);
1159 WRITE32(newname->len);
1160 WRITEMEM(newname->name, newname->len);
1162 return 0;
1165 static int encode_renew(struct xdr_stream *xdr, const struct nfs_client *client_stateid)
1167 __be32 *p;
1169 RESERVE_SPACE(12);
1170 WRITE32(OP_RENEW);
1171 WRITE64(client_stateid->cl_clientid);
1173 return 0;
1176 static int
1177 encode_restorefh(struct xdr_stream *xdr)
1179 __be32 *p;
1181 RESERVE_SPACE(4);
1182 WRITE32(OP_RESTOREFH);
1184 return 0;
1187 static int
1188 encode_setacl(struct xdr_stream *xdr, struct nfs_setaclargs *arg)
1190 __be32 *p;
1192 RESERVE_SPACE(4+sizeof(zero_stateid.data));
1193 WRITE32(OP_SETATTR);
1194 WRITEMEM(zero_stateid.data, sizeof(zero_stateid.data));
1195 RESERVE_SPACE(2*4);
1196 WRITE32(1);
1197 WRITE32(FATTR4_WORD0_ACL);
1198 if (arg->acl_len % 4)
1199 return -EINVAL;
1200 RESERVE_SPACE(4);
1201 WRITE32(arg->acl_len);
1202 xdr_write_pages(xdr, arg->acl_pages, arg->acl_pgbase, arg->acl_len);
1203 return 0;
1206 static int
1207 encode_savefh(struct xdr_stream *xdr)
1209 __be32 *p;
1211 RESERVE_SPACE(4);
1212 WRITE32(OP_SAVEFH);
1214 return 0;
1217 static int encode_setattr(struct xdr_stream *xdr, const struct nfs_setattrargs *arg, const struct nfs_server *server)
1219 int status;
1220 __be32 *p;
1222 RESERVE_SPACE(4+sizeof(arg->stateid.data));
1223 WRITE32(OP_SETATTR);
1224 WRITEMEM(arg->stateid.data, sizeof(arg->stateid.data));
1226 if ((status = encode_attrs(xdr, arg->iap, server)))
1227 return status;
1229 return 0;
1232 static int encode_setclientid(struct xdr_stream *xdr, const struct nfs4_setclientid *setclientid)
1234 __be32 *p;
1236 RESERVE_SPACE(4 + sizeof(setclientid->sc_verifier->data));
1237 WRITE32(OP_SETCLIENTID);
1238 WRITEMEM(setclientid->sc_verifier->data, sizeof(setclientid->sc_verifier->data));
1240 encode_string(xdr, setclientid->sc_name_len, setclientid->sc_name);
1241 RESERVE_SPACE(4);
1242 WRITE32(setclientid->sc_prog);
1243 encode_string(xdr, setclientid->sc_netid_len, setclientid->sc_netid);
1244 encode_string(xdr, setclientid->sc_uaddr_len, setclientid->sc_uaddr);
1245 RESERVE_SPACE(4);
1246 WRITE32(setclientid->sc_cb_ident);
1248 return 0;
1251 static int encode_setclientid_confirm(struct xdr_stream *xdr, const struct nfs_client *client_state)
1253 __be32 *p;
1255 RESERVE_SPACE(12 + sizeof(client_state->cl_confirm.data));
1256 WRITE32(OP_SETCLIENTID_CONFIRM);
1257 WRITE64(client_state->cl_clientid);
1258 WRITEMEM(client_state->cl_confirm.data, sizeof(client_state->cl_confirm.data));
1260 return 0;
1263 static int encode_write(struct xdr_stream *xdr, const struct nfs_writeargs *args)
1265 __be32 *p;
1267 RESERVE_SPACE(4);
1268 WRITE32(OP_WRITE);
1270 encode_stateid(xdr, args->context);
1272 RESERVE_SPACE(16);
1273 WRITE64(args->offset);
1274 WRITE32(args->stable);
1275 WRITE32(args->count);
1277 xdr_write_pages(xdr, args->pages, args->pgbase, args->count);
1279 return 0;
1282 static int encode_delegreturn(struct xdr_stream *xdr, const nfs4_stateid *stateid)
1284 __be32 *p;
1286 RESERVE_SPACE(20);
1288 WRITE32(OP_DELEGRETURN);
1289 WRITEMEM(stateid->data, sizeof(stateid->data));
1290 return 0;
1294 * END OF "GENERIC" ENCODE ROUTINES.
1298 * Encode an ACCESS request
1300 static int nfs4_xdr_enc_access(struct rpc_rqst *req, __be32 *p, const struct nfs4_accessargs *args)
1302 struct xdr_stream xdr;
1303 struct compound_hdr hdr = {
1304 .nops = 2,
1306 int status;
1308 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1309 encode_compound_hdr(&xdr, &hdr);
1310 if ((status = encode_putfh(&xdr, args->fh)) == 0)
1311 status = encode_access(&xdr, args->access);
1312 return status;
1316 * Encode LOOKUP request
1318 static int nfs4_xdr_enc_lookup(struct rpc_rqst *req, __be32 *p, const struct nfs4_lookup_arg *args)
1320 struct xdr_stream xdr;
1321 struct compound_hdr hdr = {
1322 .nops = 4,
1324 int status;
1326 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1327 encode_compound_hdr(&xdr, &hdr);
1328 if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1329 goto out;
1330 if ((status = encode_lookup(&xdr, args->name)) != 0)
1331 goto out;
1332 if ((status = encode_getfh(&xdr)) != 0)
1333 goto out;
1334 status = encode_getfattr(&xdr, args->bitmask);
1335 out:
1336 return status;
1340 * Encode LOOKUP_ROOT request
1342 static int nfs4_xdr_enc_lookup_root(struct rpc_rqst *req, __be32 *p, const struct nfs4_lookup_root_arg *args)
1344 struct xdr_stream xdr;
1345 struct compound_hdr hdr = {
1346 .nops = 3,
1348 int status;
1350 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1351 encode_compound_hdr(&xdr, &hdr);
1352 if ((status = encode_putrootfh(&xdr)) != 0)
1353 goto out;
1354 if ((status = encode_getfh(&xdr)) == 0)
1355 status = encode_getfattr(&xdr, args->bitmask);
1356 out:
1357 return status;
1361 * Encode REMOVE request
1363 static int nfs4_xdr_enc_remove(struct rpc_rqst *req, __be32 *p, const struct nfs4_remove_arg *args)
1365 struct xdr_stream xdr;
1366 struct compound_hdr hdr = {
1367 .nops = 3,
1369 int status;
1371 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1372 encode_compound_hdr(&xdr, &hdr);
1373 if ((status = encode_putfh(&xdr, args->fh)) != 0)
1374 goto out;
1375 if ((status = encode_remove(&xdr, args->name)) != 0)
1376 goto out;
1377 status = encode_getfattr(&xdr, args->bitmask);
1378 out:
1379 return status;
1383 * Encode RENAME request
1385 static int nfs4_xdr_enc_rename(struct rpc_rqst *req, __be32 *p, const struct nfs4_rename_arg *args)
1387 struct xdr_stream xdr;
1388 struct compound_hdr hdr = {
1389 .nops = 7,
1391 int status;
1393 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1394 encode_compound_hdr(&xdr, &hdr);
1395 if ((status = encode_putfh(&xdr, args->old_dir)) != 0)
1396 goto out;
1397 if ((status = encode_savefh(&xdr)) != 0)
1398 goto out;
1399 if ((status = encode_putfh(&xdr, args->new_dir)) != 0)
1400 goto out;
1401 if ((status = encode_rename(&xdr, args->old_name, args->new_name)) != 0)
1402 goto out;
1403 if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
1404 goto out;
1405 if ((status = encode_restorefh(&xdr)) != 0)
1406 goto out;
1407 status = encode_getfattr(&xdr, args->bitmask);
1408 out:
1409 return status;
1413 * Encode LINK request
1415 static int nfs4_xdr_enc_link(struct rpc_rqst *req, __be32 *p, const struct nfs4_link_arg *args)
1417 struct xdr_stream xdr;
1418 struct compound_hdr hdr = {
1419 .nops = 7,
1421 int status;
1423 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1424 encode_compound_hdr(&xdr, &hdr);
1425 if ((status = encode_putfh(&xdr, args->fh)) != 0)
1426 goto out;
1427 if ((status = encode_savefh(&xdr)) != 0)
1428 goto out;
1429 if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1430 goto out;
1431 if ((status = encode_link(&xdr, args->name)) != 0)
1432 goto out;
1433 if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
1434 goto out;
1435 if ((status = encode_restorefh(&xdr)) != 0)
1436 goto out;
1437 status = encode_getfattr(&xdr, args->bitmask);
1438 out:
1439 return status;
1443 * Encode CREATE request
1445 static int nfs4_xdr_enc_create(struct rpc_rqst *req, __be32 *p, const struct nfs4_create_arg *args)
1447 struct xdr_stream xdr;
1448 struct compound_hdr hdr = {
1449 .nops = 7,
1451 int status;
1453 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1454 encode_compound_hdr(&xdr, &hdr);
1455 if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1456 goto out;
1457 if ((status = encode_savefh(&xdr)) != 0)
1458 goto out;
1459 if ((status = encode_create(&xdr, args)) != 0)
1460 goto out;
1461 if ((status = encode_getfh(&xdr)) != 0)
1462 goto out;
1463 if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
1464 goto out;
1465 if ((status = encode_restorefh(&xdr)) != 0)
1466 goto out;
1467 status = encode_getfattr(&xdr, args->bitmask);
1468 out:
1469 return status;
1473 * Encode SYMLINK request
1475 static int nfs4_xdr_enc_symlink(struct rpc_rqst *req, __be32 *p, const struct nfs4_create_arg *args)
1477 return nfs4_xdr_enc_create(req, p, args);
1481 * Encode GETATTR request
1483 static int nfs4_xdr_enc_getattr(struct rpc_rqst *req, __be32 *p, const struct nfs4_getattr_arg *args)
1485 struct xdr_stream xdr;
1486 struct compound_hdr hdr = {
1487 .nops = 2,
1489 int status;
1491 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1492 encode_compound_hdr(&xdr, &hdr);
1493 if ((status = encode_putfh(&xdr, args->fh)) == 0)
1494 status = encode_getfattr(&xdr, args->bitmask);
1495 return status;
1499 * Encode a CLOSE request
1501 static int nfs4_xdr_enc_close(struct rpc_rqst *req, __be32 *p, struct nfs_closeargs *args)
1503 struct xdr_stream xdr;
1504 struct compound_hdr hdr = {
1505 .nops = 3,
1507 int status;
1509 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1510 encode_compound_hdr(&xdr, &hdr);
1511 status = encode_putfh(&xdr, args->fh);
1512 if(status)
1513 goto out;
1514 status = encode_close(&xdr, args);
1515 if (status != 0)
1516 goto out;
1517 status = encode_getfattr(&xdr, args->bitmask);
1518 out:
1519 return status;
1523 * Encode an OPEN request
1525 static int nfs4_xdr_enc_open(struct rpc_rqst *req, __be32 *p, struct nfs_openargs *args)
1527 struct xdr_stream xdr;
1528 struct compound_hdr hdr = {
1529 .nops = 7,
1531 int status;
1533 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1534 encode_compound_hdr(&xdr, &hdr);
1535 status = encode_putfh(&xdr, args->fh);
1536 if (status)
1537 goto out;
1538 status = encode_savefh(&xdr);
1539 if (status)
1540 goto out;
1541 status = encode_open(&xdr, args);
1542 if (status)
1543 goto out;
1544 status = encode_getfh(&xdr);
1545 if (status)
1546 goto out;
1547 status = encode_getfattr(&xdr, args->bitmask);
1548 if (status)
1549 goto out;
1550 status = encode_restorefh(&xdr);
1551 if (status)
1552 goto out;
1553 status = encode_getfattr(&xdr, args->bitmask);
1554 out:
1555 return status;
1559 * Encode an OPEN_CONFIRM request
1561 static int nfs4_xdr_enc_open_confirm(struct rpc_rqst *req, __be32 *p, struct nfs_open_confirmargs *args)
1563 struct xdr_stream xdr;
1564 struct compound_hdr hdr = {
1565 .nops = 2,
1567 int status;
1569 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1570 encode_compound_hdr(&xdr, &hdr);
1571 status = encode_putfh(&xdr, args->fh);
1572 if(status)
1573 goto out;
1574 status = encode_open_confirm(&xdr, args);
1575 out:
1576 return status;
1580 * Encode an OPEN request with no attributes.
1582 static int nfs4_xdr_enc_open_noattr(struct rpc_rqst *req, __be32 *p, struct nfs_openargs *args)
1584 struct xdr_stream xdr;
1585 struct compound_hdr hdr = {
1586 .nops = 3,
1588 int status;
1590 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1591 encode_compound_hdr(&xdr, &hdr);
1592 status = encode_putfh(&xdr, args->fh);
1593 if (status)
1594 goto out;
1595 status = encode_open(&xdr, args);
1596 if (status)
1597 goto out;
1598 status = encode_getfattr(&xdr, args->bitmask);
1599 out:
1600 return status;
1604 * Encode an OPEN_DOWNGRADE request
1606 static int nfs4_xdr_enc_open_downgrade(struct rpc_rqst *req, __be32 *p, struct nfs_closeargs *args)
1608 struct xdr_stream xdr;
1609 struct compound_hdr hdr = {
1610 .nops = 3,
1612 int status;
1614 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1615 encode_compound_hdr(&xdr, &hdr);
1616 status = encode_putfh(&xdr, args->fh);
1617 if (status)
1618 goto out;
1619 status = encode_open_downgrade(&xdr, args);
1620 if (status != 0)
1621 goto out;
1622 status = encode_getfattr(&xdr, args->bitmask);
1623 out:
1624 return status;
1628 * Encode a LOCK request
1630 static int nfs4_xdr_enc_lock(struct rpc_rqst *req, __be32 *p, struct nfs_lock_args *args)
1632 struct xdr_stream xdr;
1633 struct compound_hdr hdr = {
1634 .nops = 2,
1636 int status;
1638 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1639 encode_compound_hdr(&xdr, &hdr);
1640 status = encode_putfh(&xdr, args->fh);
1641 if(status)
1642 goto out;
1643 status = encode_lock(&xdr, args);
1644 out:
1645 return status;
1649 * Encode a LOCKT request
1651 static int nfs4_xdr_enc_lockt(struct rpc_rqst *req, __be32 *p, struct nfs_lockt_args *args)
1653 struct xdr_stream xdr;
1654 struct compound_hdr hdr = {
1655 .nops = 2,
1657 int status;
1659 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1660 encode_compound_hdr(&xdr, &hdr);
1661 status = encode_putfh(&xdr, args->fh);
1662 if(status)
1663 goto out;
1664 status = encode_lockt(&xdr, args);
1665 out:
1666 return status;
1670 * Encode a LOCKU request
1672 static int nfs4_xdr_enc_locku(struct rpc_rqst *req, __be32 *p, struct nfs_locku_args *args)
1674 struct xdr_stream xdr;
1675 struct compound_hdr hdr = {
1676 .nops = 2,
1678 int status;
1680 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1681 encode_compound_hdr(&xdr, &hdr);
1682 status = encode_putfh(&xdr, args->fh);
1683 if(status)
1684 goto out;
1685 status = encode_locku(&xdr, args);
1686 out:
1687 return status;
1691 * Encode a READLINK request
1693 static int nfs4_xdr_enc_readlink(struct rpc_rqst *req, __be32 *p, const struct nfs4_readlink *args)
1695 struct xdr_stream xdr;
1696 struct compound_hdr hdr = {
1697 .nops = 2,
1699 int status;
1701 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1702 encode_compound_hdr(&xdr, &hdr);
1703 status = encode_putfh(&xdr, args->fh);
1704 if(status)
1705 goto out;
1706 status = encode_readlink(&xdr, args, req);
1707 out:
1708 return status;
1712 * Encode a READDIR request
1714 static int nfs4_xdr_enc_readdir(struct rpc_rqst *req, __be32 *p, const struct nfs4_readdir_arg *args)
1716 struct xdr_stream xdr;
1717 struct compound_hdr hdr = {
1718 .nops = 2,
1720 int status;
1722 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1723 encode_compound_hdr(&xdr, &hdr);
1724 status = encode_putfh(&xdr, args->fh);
1725 if(status)
1726 goto out;
1727 status = encode_readdir(&xdr, args, req);
1728 out:
1729 return status;
1733 * Encode a READ request
1735 static int nfs4_xdr_enc_read(struct rpc_rqst *req, __be32 *p, struct nfs_readargs *args)
1737 struct rpc_auth *auth = req->rq_task->tk_auth;
1738 struct xdr_stream xdr;
1739 struct compound_hdr hdr = {
1740 .nops = 2,
1742 int replen, status;
1744 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1745 encode_compound_hdr(&xdr, &hdr);
1746 status = encode_putfh(&xdr, args->fh);
1747 if (status)
1748 goto out;
1749 status = encode_read(&xdr, args);
1750 if (status)
1751 goto out;
1753 /* set up reply kvec
1754 * toplevel status + taglen=0 + rescount + OP_PUTFH + status
1755 * + OP_READ + status + eof + datalen = 9
1757 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_read_sz) << 2;
1758 xdr_inline_pages(&req->rq_rcv_buf, replen,
1759 args->pages, args->pgbase, args->count);
1760 out:
1761 return status;
1765 * Encode an SETATTR request
1767 static int nfs4_xdr_enc_setattr(struct rpc_rqst *req, __be32 *p, struct nfs_setattrargs *args)
1770 struct xdr_stream xdr;
1771 struct compound_hdr hdr = {
1772 .nops = 3,
1774 int status;
1776 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1777 encode_compound_hdr(&xdr, &hdr);
1778 status = encode_putfh(&xdr, args->fh);
1779 if(status)
1780 goto out;
1781 status = encode_setattr(&xdr, args, args->server);
1782 if(status)
1783 goto out;
1784 status = encode_getfattr(&xdr, args->bitmask);
1785 out:
1786 return status;
1790 * Encode a GETACL request
1792 static int
1793 nfs4_xdr_enc_getacl(struct rpc_rqst *req, __be32 *p,
1794 struct nfs_getaclargs *args)
1796 struct xdr_stream xdr;
1797 struct rpc_auth *auth = req->rq_task->tk_auth;
1798 struct compound_hdr hdr = {
1799 .nops = 2,
1801 int replen, status;
1803 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1804 encode_compound_hdr(&xdr, &hdr);
1805 status = encode_putfh(&xdr, args->fh);
1806 if (status)
1807 goto out;
1808 status = encode_getattr_two(&xdr, FATTR4_WORD0_ACL, 0);
1809 /* set up reply buffer: */
1810 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_getacl_sz) << 2;
1811 xdr_inline_pages(&req->rq_rcv_buf, replen,
1812 args->acl_pages, args->acl_pgbase, args->acl_len);
1813 out:
1814 return status;
1818 * Encode a WRITE request
1820 static int nfs4_xdr_enc_write(struct rpc_rqst *req, __be32 *p, struct nfs_writeargs *args)
1822 struct xdr_stream xdr;
1823 struct compound_hdr hdr = {
1824 .nops = 3,
1826 int status;
1828 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1829 encode_compound_hdr(&xdr, &hdr);
1830 status = encode_putfh(&xdr, args->fh);
1831 if (status)
1832 goto out;
1833 status = encode_write(&xdr, args);
1834 if (status)
1835 goto out;
1836 status = encode_getfattr(&xdr, args->bitmask);
1837 out:
1838 return status;
1842 * a COMMIT request
1844 static int nfs4_xdr_enc_commit(struct rpc_rqst *req, __be32 *p, struct nfs_writeargs *args)
1846 struct xdr_stream xdr;
1847 struct compound_hdr hdr = {
1848 .nops = 3,
1850 int status;
1852 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1853 encode_compound_hdr(&xdr, &hdr);
1854 status = encode_putfh(&xdr, args->fh);
1855 if (status)
1856 goto out;
1857 status = encode_commit(&xdr, args);
1858 if (status)
1859 goto out;
1860 status = encode_getfattr(&xdr, args->bitmask);
1861 out:
1862 return status;
1866 * FSINFO request
1868 static int nfs4_xdr_enc_fsinfo(struct rpc_rqst *req, __be32 *p, struct nfs4_fsinfo_arg *args)
1870 struct xdr_stream xdr;
1871 struct compound_hdr hdr = {
1872 .nops = 2,
1874 int status;
1876 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1877 encode_compound_hdr(&xdr, &hdr);
1878 status = encode_putfh(&xdr, args->fh);
1879 if (!status)
1880 status = encode_fsinfo(&xdr, args->bitmask);
1881 return status;
1885 * a PATHCONF request
1887 static int nfs4_xdr_enc_pathconf(struct rpc_rqst *req, __be32 *p, const struct nfs4_pathconf_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)
1899 status = encode_getattr_one(&xdr,
1900 args->bitmask[0] & nfs4_pathconf_bitmap[0]);
1901 return status;
1905 * a STATFS request
1907 static int nfs4_xdr_enc_statfs(struct rpc_rqst *req, __be32 *p, const struct nfs4_statfs_arg *args)
1909 struct xdr_stream xdr;
1910 struct compound_hdr hdr = {
1911 .nops = 2,
1913 int status;
1915 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1916 encode_compound_hdr(&xdr, &hdr);
1917 status = encode_putfh(&xdr, args->fh);
1918 if (status == 0)
1919 status = encode_getattr_two(&xdr,
1920 args->bitmask[0] & nfs4_statfs_bitmap[0],
1921 args->bitmask[1] & nfs4_statfs_bitmap[1]);
1922 return status;
1926 * GETATTR_BITMAP request
1928 static int nfs4_xdr_enc_server_caps(struct rpc_rqst *req, __be32 *p, const struct nfs_fh *fhandle)
1930 struct xdr_stream xdr;
1931 struct compound_hdr hdr = {
1932 .nops = 2,
1934 int status;
1936 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1937 encode_compound_hdr(&xdr, &hdr);
1938 status = encode_putfh(&xdr, fhandle);
1939 if (status == 0)
1940 status = encode_getattr_one(&xdr, FATTR4_WORD0_SUPPORTED_ATTRS|
1941 FATTR4_WORD0_LINK_SUPPORT|
1942 FATTR4_WORD0_SYMLINK_SUPPORT|
1943 FATTR4_WORD0_ACLSUPPORT);
1944 return status;
1948 * a RENEW request
1950 static int nfs4_xdr_enc_renew(struct rpc_rqst *req, __be32 *p, struct nfs_client *clp)
1952 struct xdr_stream xdr;
1953 struct compound_hdr hdr = {
1954 .nops = 1,
1957 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1958 encode_compound_hdr(&xdr, &hdr);
1959 return encode_renew(&xdr, clp);
1963 * a SETCLIENTID request
1965 static int nfs4_xdr_enc_setclientid(struct rpc_rqst *req, __be32 *p, struct nfs4_setclientid *sc)
1967 struct xdr_stream xdr;
1968 struct compound_hdr hdr = {
1969 .nops = 1,
1972 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1973 encode_compound_hdr(&xdr, &hdr);
1974 return encode_setclientid(&xdr, sc);
1978 * a SETCLIENTID_CONFIRM request
1980 static int nfs4_xdr_enc_setclientid_confirm(struct rpc_rqst *req, __be32 *p, struct nfs_client *clp)
1982 struct xdr_stream xdr;
1983 struct compound_hdr hdr = {
1984 .nops = 3,
1986 const u32 lease_bitmap[2] = { FATTR4_WORD0_LEASE_TIME, 0 };
1987 int status;
1989 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1990 encode_compound_hdr(&xdr, &hdr);
1991 status = encode_setclientid_confirm(&xdr, clp);
1992 if (!status)
1993 status = encode_putrootfh(&xdr);
1994 if (!status)
1995 status = encode_fsinfo(&xdr, lease_bitmap);
1996 return status;
2000 * DELEGRETURN request
2002 static int nfs4_xdr_enc_delegreturn(struct rpc_rqst *req, __be32 *p, const struct nfs4_delegreturnargs *args)
2004 struct xdr_stream xdr;
2005 struct compound_hdr hdr = {
2006 .nops = 3,
2008 int status;
2010 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2011 encode_compound_hdr(&xdr, &hdr);
2012 status = encode_putfh(&xdr, args->fhandle);
2013 if (status != 0)
2014 goto out;
2015 status = encode_delegreturn(&xdr, args->stateid);
2016 if (status != 0)
2017 goto out;
2018 status = encode_getfattr(&xdr, args->bitmask);
2019 out:
2020 return status;
2024 * Encode FS_LOCATIONS request
2026 static int nfs4_xdr_enc_fs_locations(struct rpc_rqst *req, __be32 *p, struct nfs4_fs_locations_arg *args)
2028 struct xdr_stream xdr;
2029 struct compound_hdr hdr = {
2030 .nops = 3,
2032 struct rpc_auth *auth = req->rq_task->tk_auth;
2033 int replen;
2034 int status;
2036 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2037 encode_compound_hdr(&xdr, &hdr);
2038 if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
2039 goto out;
2040 if ((status = encode_lookup(&xdr, args->name)) != 0)
2041 goto out;
2042 if ((status = encode_fs_locations(&xdr, args->bitmask)) != 0)
2043 goto out;
2044 /* set up reply
2045 * toplevel_status + OP_PUTFH + status
2046 * + OP_LOOKUP + status + OP_GETATTR + status = 7
2048 replen = (RPC_REPHDRSIZE + auth->au_rslack + 7) << 2;
2049 xdr_inline_pages(&req->rq_rcv_buf, replen, &args->page,
2050 0, PAGE_SIZE);
2051 out:
2052 return status;
2056 * START OF "GENERIC" DECODE ROUTINES.
2057 * These may look a little ugly since they are imported from a "generic"
2058 * set of XDR encode/decode routines which are intended to be shared by
2059 * all of our NFSv4 implementations (OpenBSD, MacOS X...).
2061 * If the pain of reading these is too great, it should be a straightforward
2062 * task to translate them into Linux-specific versions which are more
2063 * consistent with the style used in NFSv2/v3...
2065 #define READ32(x) (x) = ntohl(*p++)
2066 #define READ64(x) do { \
2067 (x) = (u64)ntohl(*p++) << 32; \
2068 (x) |= ntohl(*p++); \
2069 } while (0)
2070 #define READTIME(x) do { \
2071 p++; \
2072 (x.tv_sec) = ntohl(*p++); \
2073 (x.tv_nsec) = ntohl(*p++); \
2074 } while (0)
2075 #define COPYMEM(x,nbytes) do { \
2076 memcpy((x), p, nbytes); \
2077 p += XDR_QUADLEN(nbytes); \
2078 } while (0)
2080 #define READ_BUF(nbytes) do { \
2081 p = xdr_inline_decode(xdr, nbytes); \
2082 if (!p) { \
2083 printk(KERN_WARNING "%s: reply buffer overflowed in line %d.", \
2084 __FUNCTION__, __LINE__); \
2085 return -EIO; \
2087 } while (0)
2089 static int decode_opaque_inline(struct xdr_stream *xdr, unsigned int *len, char **string)
2091 __be32 *p;
2093 READ_BUF(4);
2094 READ32(*len);
2095 READ_BUF(*len);
2096 *string = (char *)p;
2097 return 0;
2100 static int decode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
2102 __be32 *p;
2104 READ_BUF(8);
2105 READ32(hdr->status);
2106 READ32(hdr->taglen);
2108 READ_BUF(hdr->taglen + 4);
2109 hdr->tag = (char *)p;
2110 p += XDR_QUADLEN(hdr->taglen);
2111 READ32(hdr->nops);
2112 return 0;
2115 static int decode_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
2117 __be32 *p;
2118 uint32_t opnum;
2119 int32_t nfserr;
2121 READ_BUF(8);
2122 READ32(opnum);
2123 if (opnum != expected) {
2124 printk(KERN_NOTICE
2125 "nfs4_decode_op_hdr: Server returned operation"
2126 " %d but we issued a request for %d\n",
2127 opnum, expected);
2128 return -EIO;
2130 READ32(nfserr);
2131 if (nfserr != NFS_OK)
2132 return -nfs4_stat_to_errno(nfserr);
2133 return 0;
2136 /* Dummy routine */
2137 static int decode_ace(struct xdr_stream *xdr, void *ace, struct nfs_client *clp)
2139 __be32 *p;
2140 unsigned int strlen;
2141 char *str;
2143 READ_BUF(12);
2144 return decode_opaque_inline(xdr, &strlen, &str);
2147 static int decode_attr_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
2149 uint32_t bmlen;
2150 __be32 *p;
2152 READ_BUF(4);
2153 READ32(bmlen);
2155 bitmap[0] = bitmap[1] = 0;
2156 READ_BUF((bmlen << 2));
2157 if (bmlen > 0) {
2158 READ32(bitmap[0]);
2159 if (bmlen > 1)
2160 READ32(bitmap[1]);
2162 return 0;
2165 static inline int decode_attr_length(struct xdr_stream *xdr, uint32_t *attrlen, __be32 **savep)
2167 __be32 *p;
2169 READ_BUF(4);
2170 READ32(*attrlen);
2171 *savep = xdr->p;
2172 return 0;
2175 static int decode_attr_supported(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *bitmask)
2177 if (likely(bitmap[0] & FATTR4_WORD0_SUPPORTED_ATTRS)) {
2178 decode_attr_bitmap(xdr, bitmask);
2179 bitmap[0] &= ~FATTR4_WORD0_SUPPORTED_ATTRS;
2180 } else
2181 bitmask[0] = bitmask[1] = 0;
2182 dprintk("%s: bitmask=0x%x%x\n", __FUNCTION__, bitmask[0], bitmask[1]);
2183 return 0;
2186 static int decode_attr_type(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *type)
2188 __be32 *p;
2190 *type = 0;
2191 if (unlikely(bitmap[0] & (FATTR4_WORD0_TYPE - 1U)))
2192 return -EIO;
2193 if (likely(bitmap[0] & FATTR4_WORD0_TYPE)) {
2194 READ_BUF(4);
2195 READ32(*type);
2196 if (*type < NF4REG || *type > NF4NAMEDATTR) {
2197 dprintk("%s: bad type %d\n", __FUNCTION__, *type);
2198 return -EIO;
2200 bitmap[0] &= ~FATTR4_WORD0_TYPE;
2202 dprintk("%s: type=0%o\n", __FUNCTION__, nfs_type2fmt[*type].nfs2type);
2203 return 0;
2206 static int decode_attr_change(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *change)
2208 __be32 *p;
2210 *change = 0;
2211 if (unlikely(bitmap[0] & (FATTR4_WORD0_CHANGE - 1U)))
2212 return -EIO;
2213 if (likely(bitmap[0] & FATTR4_WORD0_CHANGE)) {
2214 READ_BUF(8);
2215 READ64(*change);
2216 bitmap[0] &= ~FATTR4_WORD0_CHANGE;
2218 dprintk("%s: change attribute=%Lu\n", __FUNCTION__,
2219 (unsigned long long)*change);
2220 return 0;
2223 static int decode_attr_size(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *size)
2225 __be32 *p;
2227 *size = 0;
2228 if (unlikely(bitmap[0] & (FATTR4_WORD0_SIZE - 1U)))
2229 return -EIO;
2230 if (likely(bitmap[0] & FATTR4_WORD0_SIZE)) {
2231 READ_BUF(8);
2232 READ64(*size);
2233 bitmap[0] &= ~FATTR4_WORD0_SIZE;
2235 dprintk("%s: file size=%Lu\n", __FUNCTION__, (unsigned long long)*size);
2236 return 0;
2239 static int decode_attr_link_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2241 __be32 *p;
2243 *res = 0;
2244 if (unlikely(bitmap[0] & (FATTR4_WORD0_LINK_SUPPORT - 1U)))
2245 return -EIO;
2246 if (likely(bitmap[0] & FATTR4_WORD0_LINK_SUPPORT)) {
2247 READ_BUF(4);
2248 READ32(*res);
2249 bitmap[0] &= ~FATTR4_WORD0_LINK_SUPPORT;
2251 dprintk("%s: link support=%s\n", __FUNCTION__, *res == 0 ? "false" : "true");
2252 return 0;
2255 static int decode_attr_symlink_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2257 __be32 *p;
2259 *res = 0;
2260 if (unlikely(bitmap[0] & (FATTR4_WORD0_SYMLINK_SUPPORT - 1U)))
2261 return -EIO;
2262 if (likely(bitmap[0] & FATTR4_WORD0_SYMLINK_SUPPORT)) {
2263 READ_BUF(4);
2264 READ32(*res);
2265 bitmap[0] &= ~FATTR4_WORD0_SYMLINK_SUPPORT;
2267 dprintk("%s: symlink support=%s\n", __FUNCTION__, *res == 0 ? "false" : "true");
2268 return 0;
2271 static int decode_attr_fsid(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs_fsid *fsid)
2273 __be32 *p;
2275 fsid->major = 0;
2276 fsid->minor = 0;
2277 if (unlikely(bitmap[0] & (FATTR4_WORD0_FSID - 1U)))
2278 return -EIO;
2279 if (likely(bitmap[0] & FATTR4_WORD0_FSID)) {
2280 READ_BUF(16);
2281 READ64(fsid->major);
2282 READ64(fsid->minor);
2283 bitmap[0] &= ~FATTR4_WORD0_FSID;
2285 dprintk("%s: fsid=(0x%Lx/0x%Lx)\n", __FUNCTION__,
2286 (unsigned long long)fsid->major,
2287 (unsigned long long)fsid->minor);
2288 return 0;
2291 static int decode_attr_lease_time(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2293 __be32 *p;
2295 *res = 60;
2296 if (unlikely(bitmap[0] & (FATTR4_WORD0_LEASE_TIME - 1U)))
2297 return -EIO;
2298 if (likely(bitmap[0] & FATTR4_WORD0_LEASE_TIME)) {
2299 READ_BUF(4);
2300 READ32(*res);
2301 bitmap[0] &= ~FATTR4_WORD0_LEASE_TIME;
2303 dprintk("%s: file size=%u\n", __FUNCTION__, (unsigned int)*res);
2304 return 0;
2307 static int decode_attr_aclsupport(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2309 __be32 *p;
2311 *res = ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL;
2312 if (unlikely(bitmap[0] & (FATTR4_WORD0_ACLSUPPORT - 1U)))
2313 return -EIO;
2314 if (likely(bitmap[0] & FATTR4_WORD0_ACLSUPPORT)) {
2315 READ_BUF(4);
2316 READ32(*res);
2317 bitmap[0] &= ~FATTR4_WORD0_ACLSUPPORT;
2319 dprintk("%s: ACLs supported=%u\n", __FUNCTION__, (unsigned int)*res);
2320 return 0;
2323 static int decode_attr_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
2325 __be32 *p;
2327 *fileid = 0;
2328 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILEID - 1U)))
2329 return -EIO;
2330 if (likely(bitmap[0] & FATTR4_WORD0_FILEID)) {
2331 READ_BUF(8);
2332 READ64(*fileid);
2333 bitmap[0] &= ~FATTR4_WORD0_FILEID;
2335 dprintk("%s: fileid=%Lu\n", __FUNCTION__, (unsigned long long)*fileid);
2336 return 0;
2339 static int decode_attr_mounted_on_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
2341 __be32 *p;
2343 *fileid = 0;
2344 if (unlikely(bitmap[1] & (FATTR4_WORD1_MOUNTED_ON_FILEID - 1U)))
2345 return -EIO;
2346 if (likely(bitmap[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)) {
2347 READ_BUF(8);
2348 READ64(*fileid);
2349 bitmap[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
2351 dprintk("%s: fileid=%Lu\n", __FUNCTION__, (unsigned long long)*fileid);
2352 return 0;
2355 static int decode_attr_files_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2357 __be32 *p;
2358 int status = 0;
2360 *res = 0;
2361 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_AVAIL - 1U)))
2362 return -EIO;
2363 if (likely(bitmap[0] & FATTR4_WORD0_FILES_AVAIL)) {
2364 READ_BUF(8);
2365 READ64(*res);
2366 bitmap[0] &= ~FATTR4_WORD0_FILES_AVAIL;
2368 dprintk("%s: files avail=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2369 return status;
2372 static int decode_attr_files_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2374 __be32 *p;
2375 int status = 0;
2377 *res = 0;
2378 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_FREE - 1U)))
2379 return -EIO;
2380 if (likely(bitmap[0] & FATTR4_WORD0_FILES_FREE)) {
2381 READ_BUF(8);
2382 READ64(*res);
2383 bitmap[0] &= ~FATTR4_WORD0_FILES_FREE;
2385 dprintk("%s: files free=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2386 return status;
2389 static int decode_attr_files_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2391 __be32 *p;
2392 int status = 0;
2394 *res = 0;
2395 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_TOTAL - 1U)))
2396 return -EIO;
2397 if (likely(bitmap[0] & FATTR4_WORD0_FILES_TOTAL)) {
2398 READ_BUF(8);
2399 READ64(*res);
2400 bitmap[0] &= ~FATTR4_WORD0_FILES_TOTAL;
2402 dprintk("%s: files total=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2403 return status;
2406 static int decode_pathname(struct xdr_stream *xdr, struct nfs4_pathname *path)
2408 int n;
2409 __be32 *p;
2410 int status = 0;
2412 READ_BUF(4);
2413 READ32(n);
2414 if (n < 0)
2415 goto out_eio;
2416 if (n == 0)
2417 goto root_path;
2418 dprintk("path ");
2419 path->ncomponents = 0;
2420 while (path->ncomponents < n) {
2421 struct nfs4_string *component = &path->components[path->ncomponents];
2422 status = decode_opaque_inline(xdr, &component->len, &component->data);
2423 if (unlikely(status != 0))
2424 goto out_eio;
2425 if (path->ncomponents != n)
2426 dprintk("/");
2427 dprintk("%s", component->data);
2428 if (path->ncomponents < NFS4_PATHNAME_MAXCOMPONENTS)
2429 path->ncomponents++;
2430 else {
2431 dprintk("cannot parse %d components in path\n", n);
2432 goto out_eio;
2435 out:
2436 dprintk("\n");
2437 return status;
2438 root_path:
2439 /* a root pathname is sent as a zero component4 */
2440 path->ncomponents = 1;
2441 path->components[0].len=0;
2442 path->components[0].data=NULL;
2443 dprintk("path /\n");
2444 goto out;
2445 out_eio:
2446 dprintk(" status %d", status);
2447 status = -EIO;
2448 goto out;
2451 static int decode_attr_fs_locations(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_fs_locations *res)
2453 int n;
2454 __be32 *p;
2455 int status = -EIO;
2457 if (unlikely(bitmap[0] & (FATTR4_WORD0_FS_LOCATIONS -1U)))
2458 goto out;
2459 status = 0;
2460 if (unlikely(!(bitmap[0] & FATTR4_WORD0_FS_LOCATIONS)))
2461 goto out;
2462 dprintk("%s: fsroot ", __FUNCTION__);
2463 status = decode_pathname(xdr, &res->fs_path);
2464 if (unlikely(status != 0))
2465 goto out;
2466 READ_BUF(4);
2467 READ32(n);
2468 if (n <= 0)
2469 goto out_eio;
2470 res->nlocations = 0;
2471 while (res->nlocations < n) {
2472 int m;
2473 struct nfs4_fs_location *loc = &res->locations[res->nlocations];
2475 READ_BUF(4);
2476 READ32(m);
2477 if (m <= 0)
2478 goto out_eio;
2480 loc->nservers = 0;
2481 dprintk("%s: servers ", __FUNCTION__);
2482 while (loc->nservers < m) {
2483 struct nfs4_string *server = &loc->servers[loc->nservers];
2484 status = decode_opaque_inline(xdr, &server->len, &server->data);
2485 if (unlikely(status != 0))
2486 goto out_eio;
2487 dprintk("%s ", server->data);
2488 if (loc->nservers < NFS4_FS_LOCATION_MAXSERVERS)
2489 loc->nservers++;
2490 else {
2491 int i;
2492 dprintk("%s: using first %d of %d servers returned for location %d\n", __FUNCTION__, NFS4_FS_LOCATION_MAXSERVERS, m, res->nlocations);
2493 for (i = loc->nservers; i < m; i++) {
2494 int len;
2495 char *data;
2496 status = decode_opaque_inline(xdr, &len, &data);
2497 if (unlikely(status != 0))
2498 goto out_eio;
2502 status = decode_pathname(xdr, &loc->rootpath);
2503 if (unlikely(status != 0))
2504 goto out_eio;
2505 if (res->nlocations < NFS4_FS_LOCATIONS_MAXENTRIES)
2506 res->nlocations++;
2508 out:
2509 dprintk("%s: fs_locations done, error = %d\n", __FUNCTION__, status);
2510 return status;
2511 out_eio:
2512 status = -EIO;
2513 goto out;
2516 static int decode_attr_maxfilesize(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2518 __be32 *p;
2519 int status = 0;
2521 *res = 0;
2522 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXFILESIZE - 1U)))
2523 return -EIO;
2524 if (likely(bitmap[0] & FATTR4_WORD0_MAXFILESIZE)) {
2525 READ_BUF(8);
2526 READ64(*res);
2527 bitmap[0] &= ~FATTR4_WORD0_MAXFILESIZE;
2529 dprintk("%s: maxfilesize=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2530 return status;
2533 static int decode_attr_maxlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxlink)
2535 __be32 *p;
2536 int status = 0;
2538 *maxlink = 1;
2539 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXLINK - 1U)))
2540 return -EIO;
2541 if (likely(bitmap[0] & FATTR4_WORD0_MAXLINK)) {
2542 READ_BUF(4);
2543 READ32(*maxlink);
2544 bitmap[0] &= ~FATTR4_WORD0_MAXLINK;
2546 dprintk("%s: maxlink=%u\n", __FUNCTION__, *maxlink);
2547 return status;
2550 static int decode_attr_maxname(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxname)
2552 __be32 *p;
2553 int status = 0;
2555 *maxname = 1024;
2556 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXNAME - 1U)))
2557 return -EIO;
2558 if (likely(bitmap[0] & FATTR4_WORD0_MAXNAME)) {
2559 READ_BUF(4);
2560 READ32(*maxname);
2561 bitmap[0] &= ~FATTR4_WORD0_MAXNAME;
2563 dprintk("%s: maxname=%u\n", __FUNCTION__, *maxname);
2564 return status;
2567 static int decode_attr_maxread(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2569 __be32 *p;
2570 int status = 0;
2572 *res = 1024;
2573 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXREAD - 1U)))
2574 return -EIO;
2575 if (likely(bitmap[0] & FATTR4_WORD0_MAXREAD)) {
2576 uint64_t maxread;
2577 READ_BUF(8);
2578 READ64(maxread);
2579 if (maxread > 0x7FFFFFFF)
2580 maxread = 0x7FFFFFFF;
2581 *res = (uint32_t)maxread;
2582 bitmap[0] &= ~FATTR4_WORD0_MAXREAD;
2584 dprintk("%s: maxread=%lu\n", __FUNCTION__, (unsigned long)*res);
2585 return status;
2588 static int decode_attr_maxwrite(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2590 __be32 *p;
2591 int status = 0;
2593 *res = 1024;
2594 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXWRITE - 1U)))
2595 return -EIO;
2596 if (likely(bitmap[0] & FATTR4_WORD0_MAXWRITE)) {
2597 uint64_t maxwrite;
2598 READ_BUF(8);
2599 READ64(maxwrite);
2600 if (maxwrite > 0x7FFFFFFF)
2601 maxwrite = 0x7FFFFFFF;
2602 *res = (uint32_t)maxwrite;
2603 bitmap[0] &= ~FATTR4_WORD0_MAXWRITE;
2605 dprintk("%s: maxwrite=%lu\n", __FUNCTION__, (unsigned long)*res);
2606 return status;
2609 static int decode_attr_mode(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *mode)
2611 __be32 *p;
2613 *mode = 0;
2614 if (unlikely(bitmap[1] & (FATTR4_WORD1_MODE - 1U)))
2615 return -EIO;
2616 if (likely(bitmap[1] & FATTR4_WORD1_MODE)) {
2617 READ_BUF(4);
2618 READ32(*mode);
2619 *mode &= ~S_IFMT;
2620 bitmap[1] &= ~FATTR4_WORD1_MODE;
2622 dprintk("%s: file mode=0%o\n", __FUNCTION__, (unsigned int)*mode);
2623 return 0;
2626 static int decode_attr_nlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *nlink)
2628 __be32 *p;
2630 *nlink = 1;
2631 if (unlikely(bitmap[1] & (FATTR4_WORD1_NUMLINKS - 1U)))
2632 return -EIO;
2633 if (likely(bitmap[1] & FATTR4_WORD1_NUMLINKS)) {
2634 READ_BUF(4);
2635 READ32(*nlink);
2636 bitmap[1] &= ~FATTR4_WORD1_NUMLINKS;
2638 dprintk("%s: nlink=%u\n", __FUNCTION__, (unsigned int)*nlink);
2639 return 0;
2642 static int decode_attr_owner(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs_client *clp, int32_t *uid)
2644 uint32_t len;
2645 __be32 *p;
2647 *uid = -2;
2648 if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER - 1U)))
2649 return -EIO;
2650 if (likely(bitmap[1] & FATTR4_WORD1_OWNER)) {
2651 READ_BUF(4);
2652 READ32(len);
2653 READ_BUF(len);
2654 if (len < XDR_MAX_NETOBJ) {
2655 if (nfs_map_name_to_uid(clp, (char *)p, len, uid) != 0)
2656 dprintk("%s: nfs_map_name_to_uid failed!\n",
2657 __FUNCTION__);
2658 } else
2659 printk(KERN_WARNING "%s: name too long (%u)!\n",
2660 __FUNCTION__, len);
2661 bitmap[1] &= ~FATTR4_WORD1_OWNER;
2663 dprintk("%s: uid=%d\n", __FUNCTION__, (int)*uid);
2664 return 0;
2667 static int decode_attr_group(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs_client *clp, int32_t *gid)
2669 uint32_t len;
2670 __be32 *p;
2672 *gid = -2;
2673 if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER_GROUP - 1U)))
2674 return -EIO;
2675 if (likely(bitmap[1] & FATTR4_WORD1_OWNER_GROUP)) {
2676 READ_BUF(4);
2677 READ32(len);
2678 READ_BUF(len);
2679 if (len < XDR_MAX_NETOBJ) {
2680 if (nfs_map_group_to_gid(clp, (char *)p, len, gid) != 0)
2681 dprintk("%s: nfs_map_group_to_gid failed!\n",
2682 __FUNCTION__);
2683 } else
2684 printk(KERN_WARNING "%s: name too long (%u)!\n",
2685 __FUNCTION__, len);
2686 bitmap[1] &= ~FATTR4_WORD1_OWNER_GROUP;
2688 dprintk("%s: gid=%d\n", __FUNCTION__, (int)*gid);
2689 return 0;
2692 static int decode_attr_rdev(struct xdr_stream *xdr, uint32_t *bitmap, dev_t *rdev)
2694 uint32_t major = 0, minor = 0;
2695 __be32 *p;
2697 *rdev = MKDEV(0,0);
2698 if (unlikely(bitmap[1] & (FATTR4_WORD1_RAWDEV - 1U)))
2699 return -EIO;
2700 if (likely(bitmap[1] & FATTR4_WORD1_RAWDEV)) {
2701 dev_t tmp;
2703 READ_BUF(8);
2704 READ32(major);
2705 READ32(minor);
2706 tmp = MKDEV(major, minor);
2707 if (MAJOR(tmp) == major && MINOR(tmp) == minor)
2708 *rdev = tmp;
2709 bitmap[1] &= ~ FATTR4_WORD1_RAWDEV;
2711 dprintk("%s: rdev=(0x%x:0x%x)\n", __FUNCTION__, major, minor);
2712 return 0;
2715 static int decode_attr_space_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2717 __be32 *p;
2718 int status = 0;
2720 *res = 0;
2721 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_AVAIL - 1U)))
2722 return -EIO;
2723 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_AVAIL)) {
2724 READ_BUF(8);
2725 READ64(*res);
2726 bitmap[1] &= ~FATTR4_WORD1_SPACE_AVAIL;
2728 dprintk("%s: space avail=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2729 return status;
2732 static int decode_attr_space_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2734 __be32 *p;
2735 int status = 0;
2737 *res = 0;
2738 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_FREE - 1U)))
2739 return -EIO;
2740 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_FREE)) {
2741 READ_BUF(8);
2742 READ64(*res);
2743 bitmap[1] &= ~FATTR4_WORD1_SPACE_FREE;
2745 dprintk("%s: space free=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2746 return status;
2749 static int decode_attr_space_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2751 __be32 *p;
2752 int status = 0;
2754 *res = 0;
2755 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_TOTAL - 1U)))
2756 return -EIO;
2757 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_TOTAL)) {
2758 READ_BUF(8);
2759 READ64(*res);
2760 bitmap[1] &= ~FATTR4_WORD1_SPACE_TOTAL;
2762 dprintk("%s: space total=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2763 return status;
2766 static int decode_attr_space_used(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *used)
2768 __be32 *p;
2770 *used = 0;
2771 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_USED - 1U)))
2772 return -EIO;
2773 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_USED)) {
2774 READ_BUF(8);
2775 READ64(*used);
2776 bitmap[1] &= ~FATTR4_WORD1_SPACE_USED;
2778 dprintk("%s: space used=%Lu\n", __FUNCTION__,
2779 (unsigned long long)*used);
2780 return 0;
2783 static int decode_attr_time(struct xdr_stream *xdr, struct timespec *time)
2785 __be32 *p;
2786 uint64_t sec;
2787 uint32_t nsec;
2789 READ_BUF(12);
2790 READ64(sec);
2791 READ32(nsec);
2792 time->tv_sec = (time_t)sec;
2793 time->tv_nsec = (long)nsec;
2794 return 0;
2797 static int decode_attr_time_access(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2799 int status = 0;
2801 time->tv_sec = 0;
2802 time->tv_nsec = 0;
2803 if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_ACCESS - 1U)))
2804 return -EIO;
2805 if (likely(bitmap[1] & FATTR4_WORD1_TIME_ACCESS)) {
2806 status = decode_attr_time(xdr, time);
2807 bitmap[1] &= ~FATTR4_WORD1_TIME_ACCESS;
2809 dprintk("%s: atime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2810 return status;
2813 static int decode_attr_time_metadata(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2815 int status = 0;
2817 time->tv_sec = 0;
2818 time->tv_nsec = 0;
2819 if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_METADATA - 1U)))
2820 return -EIO;
2821 if (likely(bitmap[1] & FATTR4_WORD1_TIME_METADATA)) {
2822 status = decode_attr_time(xdr, time);
2823 bitmap[1] &= ~FATTR4_WORD1_TIME_METADATA;
2825 dprintk("%s: ctime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2826 return status;
2829 static int decode_attr_time_modify(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2831 int status = 0;
2833 time->tv_sec = 0;
2834 time->tv_nsec = 0;
2835 if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_MODIFY - 1U)))
2836 return -EIO;
2837 if (likely(bitmap[1] & FATTR4_WORD1_TIME_MODIFY)) {
2838 status = decode_attr_time(xdr, time);
2839 bitmap[1] &= ~FATTR4_WORD1_TIME_MODIFY;
2841 dprintk("%s: mtime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2842 return status;
2845 static int verify_attr_len(struct xdr_stream *xdr, __be32 *savep, uint32_t attrlen)
2847 unsigned int attrwords = XDR_QUADLEN(attrlen);
2848 unsigned int nwords = xdr->p - savep;
2850 if (unlikely(attrwords != nwords)) {
2851 printk(KERN_WARNING "%s: server returned incorrect attribute length: %u %c %u\n",
2852 __FUNCTION__,
2853 attrwords << 2,
2854 (attrwords < nwords) ? '<' : '>',
2855 nwords << 2);
2856 return -EIO;
2858 return 0;
2861 static int decode_change_info(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
2863 __be32 *p;
2865 READ_BUF(20);
2866 READ32(cinfo->atomic);
2867 READ64(cinfo->before);
2868 READ64(cinfo->after);
2869 return 0;
2872 static int decode_access(struct xdr_stream *xdr, struct nfs4_accessres *access)
2874 __be32 *p;
2875 uint32_t supp, acc;
2876 int status;
2878 status = decode_op_hdr(xdr, OP_ACCESS);
2879 if (status)
2880 return status;
2881 READ_BUF(8);
2882 READ32(supp);
2883 READ32(acc);
2884 access->supported = supp;
2885 access->access = acc;
2886 return 0;
2889 static int decode_close(struct xdr_stream *xdr, struct nfs_closeres *res)
2891 __be32 *p;
2892 int status;
2894 status = decode_op_hdr(xdr, OP_CLOSE);
2895 if (status)
2896 return status;
2897 READ_BUF(sizeof(res->stateid.data));
2898 COPYMEM(res->stateid.data, sizeof(res->stateid.data));
2899 return 0;
2902 static int decode_commit(struct xdr_stream *xdr, struct nfs_writeres *res)
2904 __be32 *p;
2905 int status;
2907 status = decode_op_hdr(xdr, OP_COMMIT);
2908 if (status)
2909 return status;
2910 READ_BUF(8);
2911 COPYMEM(res->verf->verifier, 8);
2912 return 0;
2915 static int decode_create(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
2917 __be32 *p;
2918 uint32_t bmlen;
2919 int status;
2921 status = decode_op_hdr(xdr, OP_CREATE);
2922 if (status)
2923 return status;
2924 if ((status = decode_change_info(xdr, cinfo)))
2925 return status;
2926 READ_BUF(4);
2927 READ32(bmlen);
2928 READ_BUF(bmlen << 2);
2929 return 0;
2932 static int decode_server_caps(struct xdr_stream *xdr, struct nfs4_server_caps_res *res)
2934 __be32 *savep;
2935 uint32_t attrlen,
2936 bitmap[2] = {0};
2937 int status;
2939 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2940 goto xdr_error;
2941 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2942 goto xdr_error;
2943 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2944 goto xdr_error;
2945 if ((status = decode_attr_supported(xdr, bitmap, res->attr_bitmask)) != 0)
2946 goto xdr_error;
2947 if ((status = decode_attr_link_support(xdr, bitmap, &res->has_links)) != 0)
2948 goto xdr_error;
2949 if ((status = decode_attr_symlink_support(xdr, bitmap, &res->has_symlinks)) != 0)
2950 goto xdr_error;
2951 if ((status = decode_attr_aclsupport(xdr, bitmap, &res->acl_bitmask)) != 0)
2952 goto xdr_error;
2953 status = verify_attr_len(xdr, savep, attrlen);
2954 xdr_error:
2955 dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
2956 return status;
2959 static int decode_statfs(struct xdr_stream *xdr, struct nfs_fsstat *fsstat)
2961 __be32 *savep;
2962 uint32_t attrlen,
2963 bitmap[2] = {0};
2964 int status;
2966 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2967 goto xdr_error;
2968 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2969 goto xdr_error;
2970 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2971 goto xdr_error;
2973 if ((status = decode_attr_files_avail(xdr, bitmap, &fsstat->afiles)) != 0)
2974 goto xdr_error;
2975 if ((status = decode_attr_files_free(xdr, bitmap, &fsstat->ffiles)) != 0)
2976 goto xdr_error;
2977 if ((status = decode_attr_files_total(xdr, bitmap, &fsstat->tfiles)) != 0)
2978 goto xdr_error;
2979 if ((status = decode_attr_space_avail(xdr, bitmap, &fsstat->abytes)) != 0)
2980 goto xdr_error;
2981 if ((status = decode_attr_space_free(xdr, bitmap, &fsstat->fbytes)) != 0)
2982 goto xdr_error;
2983 if ((status = decode_attr_space_total(xdr, bitmap, &fsstat->tbytes)) != 0)
2984 goto xdr_error;
2986 status = verify_attr_len(xdr, savep, attrlen);
2987 xdr_error:
2988 dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
2989 return status;
2992 static int decode_pathconf(struct xdr_stream *xdr, struct nfs_pathconf *pathconf)
2994 __be32 *savep;
2995 uint32_t attrlen,
2996 bitmap[2] = {0};
2997 int status;
2999 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3000 goto xdr_error;
3001 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3002 goto xdr_error;
3003 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3004 goto xdr_error;
3006 if ((status = decode_attr_maxlink(xdr, bitmap, &pathconf->max_link)) != 0)
3007 goto xdr_error;
3008 if ((status = decode_attr_maxname(xdr, bitmap, &pathconf->max_namelen)) != 0)
3009 goto xdr_error;
3011 status = verify_attr_len(xdr, savep, attrlen);
3012 xdr_error:
3013 dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
3014 return status;
3017 static int decode_getfattr(struct xdr_stream *xdr, struct nfs_fattr *fattr, const struct nfs_server *server)
3019 __be32 *savep;
3020 uint32_t attrlen,
3021 bitmap[2] = {0},
3022 type;
3023 int status, fmode = 0;
3024 uint64_t fileid;
3026 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3027 goto xdr_error;
3028 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3029 goto xdr_error;
3031 fattr->bitmap[0] = bitmap[0];
3032 fattr->bitmap[1] = bitmap[1];
3034 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3035 goto xdr_error;
3038 if ((status = decode_attr_type(xdr, bitmap, &type)) != 0)
3039 goto xdr_error;
3040 fattr->type = nfs_type2fmt[type].nfs2type;
3041 fmode = nfs_type2fmt[type].mode;
3043 if ((status = decode_attr_change(xdr, bitmap, &fattr->change_attr)) != 0)
3044 goto xdr_error;
3045 if ((status = decode_attr_size(xdr, bitmap, &fattr->size)) != 0)
3046 goto xdr_error;
3047 if ((status = decode_attr_fsid(xdr, bitmap, &fattr->fsid)) != 0)
3048 goto xdr_error;
3049 if ((status = decode_attr_fileid(xdr, bitmap, &fattr->fileid)) != 0)
3050 goto xdr_error;
3051 if ((status = decode_attr_fs_locations(xdr, bitmap, container_of(fattr,
3052 struct nfs4_fs_locations,
3053 fattr))) != 0)
3054 goto xdr_error;
3055 if ((status = decode_attr_mode(xdr, bitmap, &fattr->mode)) != 0)
3056 goto xdr_error;
3057 fattr->mode |= fmode;
3058 if ((status = decode_attr_nlink(xdr, bitmap, &fattr->nlink)) != 0)
3059 goto xdr_error;
3060 if ((status = decode_attr_owner(xdr, bitmap, server->nfs_client, &fattr->uid)) != 0)
3061 goto xdr_error;
3062 if ((status = decode_attr_group(xdr, bitmap, server->nfs_client, &fattr->gid)) != 0)
3063 goto xdr_error;
3064 if ((status = decode_attr_rdev(xdr, bitmap, &fattr->rdev)) != 0)
3065 goto xdr_error;
3066 if ((status = decode_attr_space_used(xdr, bitmap, &fattr->du.nfs3.used)) != 0)
3067 goto xdr_error;
3068 if ((status = decode_attr_time_access(xdr, bitmap, &fattr->atime)) != 0)
3069 goto xdr_error;
3070 if ((status = decode_attr_time_metadata(xdr, bitmap, &fattr->ctime)) != 0)
3071 goto xdr_error;
3072 if ((status = decode_attr_time_modify(xdr, bitmap, &fattr->mtime)) != 0)
3073 goto xdr_error;
3074 if ((status = decode_attr_mounted_on_fileid(xdr, bitmap, &fileid)) != 0)
3075 goto xdr_error;
3076 if (fattr->fileid == 0 && fileid != 0)
3077 fattr->fileid = fileid;
3078 if ((status = verify_attr_len(xdr, savep, attrlen)) == 0)
3079 fattr->valid = NFS_ATTR_FATTR | NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4;
3080 xdr_error:
3081 dprintk("%s: xdr returned %d\n", __FUNCTION__, -status);
3082 return status;
3086 static int decode_fsinfo(struct xdr_stream *xdr, struct nfs_fsinfo *fsinfo)
3088 __be32 *savep;
3089 uint32_t attrlen, bitmap[2];
3090 int status;
3092 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3093 goto xdr_error;
3094 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3095 goto xdr_error;
3096 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3097 goto xdr_error;
3099 fsinfo->rtmult = fsinfo->wtmult = 512; /* ??? */
3101 if ((status = decode_attr_lease_time(xdr, bitmap, &fsinfo->lease_time)) != 0)
3102 goto xdr_error;
3103 if ((status = decode_attr_maxfilesize(xdr, bitmap, &fsinfo->maxfilesize)) != 0)
3104 goto xdr_error;
3105 if ((status = decode_attr_maxread(xdr, bitmap, &fsinfo->rtmax)) != 0)
3106 goto xdr_error;
3107 fsinfo->rtpref = fsinfo->dtpref = fsinfo->rtmax;
3108 if ((status = decode_attr_maxwrite(xdr, bitmap, &fsinfo->wtmax)) != 0)
3109 goto xdr_error;
3110 fsinfo->wtpref = fsinfo->wtmax;
3112 status = verify_attr_len(xdr, savep, attrlen);
3113 xdr_error:
3114 dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
3115 return status;
3118 static int decode_getfh(struct xdr_stream *xdr, struct nfs_fh *fh)
3120 __be32 *p;
3121 uint32_t len;
3122 int status;
3124 status = decode_op_hdr(xdr, OP_GETFH);
3125 if (status)
3126 return status;
3127 /* Zero handle first to allow comparisons */
3128 memset(fh, 0, sizeof(*fh));
3130 READ_BUF(4);
3131 READ32(len);
3132 if (len > NFS4_FHSIZE)
3133 return -EIO;
3134 fh->size = len;
3135 READ_BUF(len);
3136 COPYMEM(fh->data, len);
3137 return 0;
3140 static int decode_link(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
3142 int status;
3144 status = decode_op_hdr(xdr, OP_LINK);
3145 if (status)
3146 return status;
3147 return decode_change_info(xdr, cinfo);
3151 * We create the owner, so we know a proper owner.id length is 4.
3153 static int decode_lock_denied (struct xdr_stream *xdr, struct file_lock *fl)
3155 uint64_t offset, length, clientid;
3156 __be32 *p;
3157 uint32_t namelen, type;
3159 READ_BUF(32);
3160 READ64(offset);
3161 READ64(length);
3162 READ32(type);
3163 if (fl != NULL) {
3164 fl->fl_start = (loff_t)offset;
3165 fl->fl_end = fl->fl_start + (loff_t)length - 1;
3166 if (length == ~(uint64_t)0)
3167 fl->fl_end = OFFSET_MAX;
3168 fl->fl_type = F_WRLCK;
3169 if (type & 1)
3170 fl->fl_type = F_RDLCK;
3171 fl->fl_pid = 0;
3173 READ64(clientid);
3174 READ32(namelen);
3175 READ_BUF(namelen);
3176 return -NFS4ERR_DENIED;
3179 static int decode_lock(struct xdr_stream *xdr, struct nfs_lock_res *res)
3181 __be32 *p;
3182 int status;
3184 status = decode_op_hdr(xdr, OP_LOCK);
3185 if (status == 0) {
3186 READ_BUF(sizeof(res->stateid.data));
3187 COPYMEM(res->stateid.data, sizeof(res->stateid.data));
3188 } else if (status == -NFS4ERR_DENIED)
3189 return decode_lock_denied(xdr, NULL);
3190 return status;
3193 static int decode_lockt(struct xdr_stream *xdr, struct nfs_lockt_res *res)
3195 int status;
3196 status = decode_op_hdr(xdr, OP_LOCKT);
3197 if (status == -NFS4ERR_DENIED)
3198 return decode_lock_denied(xdr, res->denied);
3199 return status;
3202 static int decode_locku(struct xdr_stream *xdr, struct nfs_locku_res *res)
3204 __be32 *p;
3205 int status;
3207 status = decode_op_hdr(xdr, OP_LOCKU);
3208 if (status == 0) {
3209 READ_BUF(sizeof(res->stateid.data));
3210 COPYMEM(res->stateid.data, sizeof(res->stateid.data));
3212 return status;
3215 static int decode_lookup(struct xdr_stream *xdr)
3217 return decode_op_hdr(xdr, OP_LOOKUP);
3220 /* This is too sick! */
3221 static int decode_space_limit(struct xdr_stream *xdr, u64 *maxsize)
3223 __be32 *p;
3224 uint32_t limit_type, nblocks, blocksize;
3226 READ_BUF(12);
3227 READ32(limit_type);
3228 switch (limit_type) {
3229 case 1:
3230 READ64(*maxsize);
3231 break;
3232 case 2:
3233 READ32(nblocks);
3234 READ32(blocksize);
3235 *maxsize = (uint64_t)nblocks * (uint64_t)blocksize;
3237 return 0;
3240 static int decode_delegation(struct xdr_stream *xdr, struct nfs_openres *res)
3242 __be32 *p;
3243 uint32_t delegation_type;
3245 READ_BUF(4);
3246 READ32(delegation_type);
3247 if (delegation_type == NFS4_OPEN_DELEGATE_NONE) {
3248 res->delegation_type = 0;
3249 return 0;
3251 READ_BUF(20);
3252 COPYMEM(res->delegation.data, sizeof(res->delegation.data));
3253 READ32(res->do_recall);
3254 switch (delegation_type) {
3255 case NFS4_OPEN_DELEGATE_READ:
3256 res->delegation_type = FMODE_READ;
3257 break;
3258 case NFS4_OPEN_DELEGATE_WRITE:
3259 res->delegation_type = FMODE_WRITE|FMODE_READ;
3260 if (decode_space_limit(xdr, &res->maxsize) < 0)
3261 return -EIO;
3263 return decode_ace(xdr, NULL, res->server->nfs_client);
3266 static int decode_open(struct xdr_stream *xdr, struct nfs_openres *res)
3268 __be32 *p;
3269 uint32_t bmlen;
3270 int status;
3272 status = decode_op_hdr(xdr, OP_OPEN);
3273 if (status)
3274 return status;
3275 READ_BUF(sizeof(res->stateid.data));
3276 COPYMEM(res->stateid.data, sizeof(res->stateid.data));
3278 decode_change_info(xdr, &res->cinfo);
3280 READ_BUF(8);
3281 READ32(res->rflags);
3282 READ32(bmlen);
3283 if (bmlen > 10)
3284 goto xdr_error;
3286 READ_BUF(bmlen << 2);
3287 p += bmlen;
3288 return decode_delegation(xdr, res);
3289 xdr_error:
3290 dprintk("%s: Bitmap too large! Length = %u\n", __FUNCTION__, bmlen);
3291 return -EIO;
3294 static int decode_open_confirm(struct xdr_stream *xdr, struct nfs_open_confirmres *res)
3296 __be32 *p;
3297 int status;
3299 status = decode_op_hdr(xdr, OP_OPEN_CONFIRM);
3300 if (status)
3301 return status;
3302 READ_BUF(sizeof(res->stateid.data));
3303 COPYMEM(res->stateid.data, sizeof(res->stateid.data));
3304 return 0;
3307 static int decode_open_downgrade(struct xdr_stream *xdr, struct nfs_closeres *res)
3309 __be32 *p;
3310 int status;
3312 status = decode_op_hdr(xdr, OP_OPEN_DOWNGRADE);
3313 if (status)
3314 return status;
3315 READ_BUF(sizeof(res->stateid.data));
3316 COPYMEM(res->stateid.data, sizeof(res->stateid.data));
3317 return 0;
3320 static int decode_putfh(struct xdr_stream *xdr)
3322 return decode_op_hdr(xdr, OP_PUTFH);
3325 static int decode_putrootfh(struct xdr_stream *xdr)
3327 return decode_op_hdr(xdr, OP_PUTROOTFH);
3330 static int decode_read(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs_readres *res)
3332 struct kvec *iov = req->rq_rcv_buf.head;
3333 __be32 *p;
3334 uint32_t count, eof, recvd, hdrlen;
3335 int status;
3337 status = decode_op_hdr(xdr, OP_READ);
3338 if (status)
3339 return status;
3340 READ_BUF(8);
3341 READ32(eof);
3342 READ32(count);
3343 hdrlen = (u8 *) p - (u8 *) iov->iov_base;
3344 recvd = req->rq_rcv_buf.len - hdrlen;
3345 if (count > recvd) {
3346 printk(KERN_WARNING "NFS: server cheating in read reply: "
3347 "count %u > recvd %u\n", count, recvd);
3348 count = recvd;
3349 eof = 0;
3351 xdr_read_pages(xdr, count);
3352 res->eof = eof;
3353 res->count = count;
3354 return 0;
3357 static int decode_readdir(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs4_readdir_res *readdir)
3359 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
3360 struct page *page = *rcvbuf->pages;
3361 struct kvec *iov = rcvbuf->head;
3362 unsigned int nr, pglen = rcvbuf->page_len;
3363 __be32 *end, *entry, *p, *kaddr;
3364 uint32_t len, attrlen, xlen;
3365 int hdrlen, recvd, status;
3367 status = decode_op_hdr(xdr, OP_READDIR);
3368 if (status)
3369 return status;
3370 READ_BUF(8);
3371 COPYMEM(readdir->verifier.data, 8);
3372 dprintk("%s: verifier = 0x%x%x\n",
3373 __FUNCTION__,
3374 ((u32 *)readdir->verifier.data)[0],
3375 ((u32 *)readdir->verifier.data)[1]);
3378 hdrlen = (char *) p - (char *) iov->iov_base;
3379 recvd = rcvbuf->len - hdrlen;
3380 if (pglen > recvd)
3381 pglen = recvd;
3382 xdr_read_pages(xdr, pglen);
3384 BUG_ON(pglen + readdir->pgbase > PAGE_CACHE_SIZE);
3385 kaddr = p = kmap_atomic(page, KM_USER0);
3386 end = p + ((pglen + readdir->pgbase) >> 2);
3387 entry = p;
3388 for (nr = 0; *p++; nr++) {
3389 if (end - p < 3)
3390 goto short_pkt;
3391 dprintk("cookie = %Lu, ", *((unsigned long long *)p));
3392 p += 2; /* cookie */
3393 len = ntohl(*p++); /* filename length */
3394 if (len > NFS4_MAXNAMLEN) {
3395 printk(KERN_WARNING "NFS: giant filename in readdir (len 0x%x)\n", len);
3396 goto err_unmap;
3398 xlen = XDR_QUADLEN(len);
3399 if (end - p < xlen + 1)
3400 goto short_pkt;
3401 dprintk("filename = %*s\n", len, (char *)p);
3402 p += xlen;
3403 len = ntohl(*p++); /* bitmap length */
3404 if (end - p < len + 1)
3405 goto short_pkt;
3406 p += len;
3407 attrlen = XDR_QUADLEN(ntohl(*p++));
3408 if (end - p < attrlen + 2)
3409 goto short_pkt;
3410 p += attrlen; /* attributes */
3411 entry = p;
3413 if (!nr && (entry[0] != 0 || entry[1] == 0))
3414 goto short_pkt;
3415 out:
3416 kunmap_atomic(kaddr, KM_USER0);
3417 return 0;
3418 short_pkt:
3419 dprintk("%s: short packet at entry %d\n", __FUNCTION__, nr);
3420 entry[0] = entry[1] = 0;
3421 /* truncate listing ? */
3422 if (!nr) {
3423 printk(KERN_NOTICE "NFS: readdir reply truncated!\n");
3424 entry[1] = 1;
3426 goto out;
3427 err_unmap:
3428 kunmap_atomic(kaddr, KM_USER0);
3429 return -errno_NFSERR_IO;
3432 static int decode_readlink(struct xdr_stream *xdr, struct rpc_rqst *req)
3434 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
3435 struct kvec *iov = rcvbuf->head;
3436 int hdrlen, len, recvd;
3437 __be32 *p;
3438 char *kaddr;
3439 int status;
3441 status = decode_op_hdr(xdr, OP_READLINK);
3442 if (status)
3443 return status;
3445 /* Convert length of symlink */
3446 READ_BUF(4);
3447 READ32(len);
3448 if (len >= rcvbuf->page_len || len <= 0) {
3449 dprintk(KERN_WARNING "nfs: server returned giant symlink!\n");
3450 return -ENAMETOOLONG;
3452 hdrlen = (char *) xdr->p - (char *) iov->iov_base;
3453 recvd = req->rq_rcv_buf.len - hdrlen;
3454 if (recvd < len) {
3455 printk(KERN_WARNING "NFS: server cheating in readlink reply: "
3456 "count %u > recvd %u\n", len, recvd);
3457 return -EIO;
3459 xdr_read_pages(xdr, len);
3461 * The XDR encode routine has set things up so that
3462 * the link text will be copied directly into the
3463 * buffer. We just have to do overflow-checking,
3464 * and and null-terminate the text (the VFS expects
3465 * null-termination).
3467 kaddr = (char *)kmap_atomic(rcvbuf->pages[0], KM_USER0);
3468 kaddr[len+rcvbuf->page_base] = '\0';
3469 kunmap_atomic(kaddr, KM_USER0);
3470 return 0;
3473 static int decode_remove(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
3475 int status;
3477 status = decode_op_hdr(xdr, OP_REMOVE);
3478 if (status)
3479 goto out;
3480 status = decode_change_info(xdr, cinfo);
3481 out:
3482 return status;
3485 static int decode_rename(struct xdr_stream *xdr, struct nfs4_change_info *old_cinfo,
3486 struct nfs4_change_info *new_cinfo)
3488 int status;
3490 status = decode_op_hdr(xdr, OP_RENAME);
3491 if (status)
3492 goto out;
3493 if ((status = decode_change_info(xdr, old_cinfo)))
3494 goto out;
3495 status = decode_change_info(xdr, new_cinfo);
3496 out:
3497 return status;
3500 static int decode_renew(struct xdr_stream *xdr)
3502 return decode_op_hdr(xdr, OP_RENEW);
3505 static int
3506 decode_restorefh(struct xdr_stream *xdr)
3508 return decode_op_hdr(xdr, OP_RESTOREFH);
3511 static int decode_getacl(struct xdr_stream *xdr, struct rpc_rqst *req,
3512 size_t *acl_len)
3514 __be32 *savep;
3515 uint32_t attrlen,
3516 bitmap[2] = {0};
3517 struct kvec *iov = req->rq_rcv_buf.head;
3518 int status;
3520 *acl_len = 0;
3521 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3522 goto out;
3523 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3524 goto out;
3525 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3526 goto out;
3528 if (unlikely(bitmap[0] & (FATTR4_WORD0_ACL - 1U)))
3529 return -EIO;
3530 if (likely(bitmap[0] & FATTR4_WORD0_ACL)) {
3531 int hdrlen, recvd;
3533 /* We ignore &savep and don't do consistency checks on
3534 * the attr length. Let userspace figure it out.... */
3535 hdrlen = (u8 *)xdr->p - (u8 *)iov->iov_base;
3536 recvd = req->rq_rcv_buf.len - hdrlen;
3537 if (attrlen > recvd) {
3538 printk(KERN_WARNING "NFS: server cheating in getattr"
3539 " acl reply: attrlen %u > recvd %u\n",
3540 attrlen, recvd);
3541 return -EINVAL;
3543 xdr_read_pages(xdr, attrlen);
3544 *acl_len = attrlen;
3545 } else
3546 status = -EOPNOTSUPP;
3548 out:
3549 return status;
3552 static int
3553 decode_savefh(struct xdr_stream *xdr)
3555 return decode_op_hdr(xdr, OP_SAVEFH);
3558 static int decode_setattr(struct xdr_stream *xdr, struct nfs_setattrres *res)
3560 __be32 *p;
3561 uint32_t bmlen;
3562 int status;
3565 status = decode_op_hdr(xdr, OP_SETATTR);
3566 if (status)
3567 return status;
3568 READ_BUF(4);
3569 READ32(bmlen);
3570 READ_BUF(bmlen << 2);
3571 return 0;
3574 static int decode_setclientid(struct xdr_stream *xdr, struct nfs_client *clp)
3576 __be32 *p;
3577 uint32_t opnum;
3578 int32_t nfserr;
3580 READ_BUF(8);
3581 READ32(opnum);
3582 if (opnum != OP_SETCLIENTID) {
3583 printk(KERN_NOTICE
3584 "nfs4_decode_setclientid: Server returned operation"
3585 " %d\n", opnum);
3586 return -EIO;
3588 READ32(nfserr);
3589 if (nfserr == NFS_OK) {
3590 READ_BUF(8 + sizeof(clp->cl_confirm.data));
3591 READ64(clp->cl_clientid);
3592 COPYMEM(clp->cl_confirm.data, sizeof(clp->cl_confirm.data));
3593 } else if (nfserr == NFSERR_CLID_INUSE) {
3594 uint32_t len;
3596 /* skip netid string */
3597 READ_BUF(4);
3598 READ32(len);
3599 READ_BUF(len);
3601 /* skip uaddr string */
3602 READ_BUF(4);
3603 READ32(len);
3604 READ_BUF(len);
3605 return -NFSERR_CLID_INUSE;
3606 } else
3607 return -nfs4_stat_to_errno(nfserr);
3609 return 0;
3612 static int decode_setclientid_confirm(struct xdr_stream *xdr)
3614 return decode_op_hdr(xdr, OP_SETCLIENTID_CONFIRM);
3617 static int decode_write(struct xdr_stream *xdr, struct nfs_writeres *res)
3619 __be32 *p;
3620 int status;
3622 status = decode_op_hdr(xdr, OP_WRITE);
3623 if (status)
3624 return status;
3626 READ_BUF(16);
3627 READ32(res->count);
3628 READ32(res->verf->committed);
3629 COPYMEM(res->verf->verifier, 8);
3630 return 0;
3633 static int decode_delegreturn(struct xdr_stream *xdr)
3635 return decode_op_hdr(xdr, OP_DELEGRETURN);
3639 * Decode OPEN_DOWNGRADE response
3641 static int nfs4_xdr_dec_open_downgrade(struct rpc_rqst *rqstp, __be32 *p, struct nfs_closeres *res)
3643 struct xdr_stream xdr;
3644 struct compound_hdr hdr;
3645 int status;
3647 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3648 status = decode_compound_hdr(&xdr, &hdr);
3649 if (status)
3650 goto out;
3651 status = decode_putfh(&xdr);
3652 if (status)
3653 goto out;
3654 status = decode_open_downgrade(&xdr, res);
3655 if (status != 0)
3656 goto out;
3657 decode_getfattr(&xdr, res->fattr, res->server);
3658 out:
3659 return status;
3663 * END OF "GENERIC" DECODE ROUTINES.
3667 * Decode ACCESS response
3669 static int nfs4_xdr_dec_access(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_accessres *res)
3671 struct xdr_stream xdr;
3672 struct compound_hdr hdr;
3673 int status;
3675 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3676 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3677 goto out;
3678 if ((status = decode_putfh(&xdr)) == 0)
3679 status = decode_access(&xdr, res);
3680 out:
3681 return status;
3685 * Decode LOOKUP response
3687 static int nfs4_xdr_dec_lookup(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_lookup_res *res)
3689 struct xdr_stream xdr;
3690 struct compound_hdr hdr;
3691 int status;
3693 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3694 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3695 goto out;
3696 if ((status = decode_putfh(&xdr)) != 0)
3697 goto out;
3698 if ((status = decode_lookup(&xdr)) != 0)
3699 goto out;
3700 if ((status = decode_getfh(&xdr, res->fh)) != 0)
3701 goto out;
3702 status = decode_getfattr(&xdr, res->fattr, res->server);
3703 out:
3704 return status;
3708 * Decode LOOKUP_ROOT response
3710 static int nfs4_xdr_dec_lookup_root(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_lookup_res *res)
3712 struct xdr_stream xdr;
3713 struct compound_hdr hdr;
3714 int status;
3716 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3717 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3718 goto out;
3719 if ((status = decode_putrootfh(&xdr)) != 0)
3720 goto out;
3721 if ((status = decode_getfh(&xdr, res->fh)) == 0)
3722 status = decode_getfattr(&xdr, res->fattr, res->server);
3723 out:
3724 return status;
3728 * Decode REMOVE response
3730 static int nfs4_xdr_dec_remove(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_remove_res *res)
3732 struct xdr_stream xdr;
3733 struct compound_hdr hdr;
3734 int status;
3736 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3737 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3738 goto out;
3739 if ((status = decode_putfh(&xdr)) != 0)
3740 goto out;
3741 if ((status = decode_remove(&xdr, &res->cinfo)) != 0)
3742 goto out;
3743 decode_getfattr(&xdr, res->dir_attr, res->server);
3744 out:
3745 return status;
3749 * Decode RENAME response
3751 static int nfs4_xdr_dec_rename(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_rename_res *res)
3753 struct xdr_stream xdr;
3754 struct compound_hdr hdr;
3755 int status;
3757 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3758 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3759 goto out;
3760 if ((status = decode_putfh(&xdr)) != 0)
3761 goto out;
3762 if ((status = decode_savefh(&xdr)) != 0)
3763 goto out;
3764 if ((status = decode_putfh(&xdr)) != 0)
3765 goto out;
3766 if ((status = decode_rename(&xdr, &res->old_cinfo, &res->new_cinfo)) != 0)
3767 goto out;
3768 /* Current FH is target directory */
3769 if (decode_getfattr(&xdr, res->new_fattr, res->server) != 0)
3770 goto out;
3771 if ((status = decode_restorefh(&xdr)) != 0)
3772 goto out;
3773 decode_getfattr(&xdr, res->old_fattr, res->server);
3774 out:
3775 return status;
3779 * Decode LINK response
3781 static int nfs4_xdr_dec_link(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_link_res *res)
3783 struct xdr_stream xdr;
3784 struct compound_hdr hdr;
3785 int status;
3787 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3788 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3789 goto out;
3790 if ((status = decode_putfh(&xdr)) != 0)
3791 goto out;
3792 if ((status = decode_savefh(&xdr)) != 0)
3793 goto out;
3794 if ((status = decode_putfh(&xdr)) != 0)
3795 goto out;
3796 if ((status = decode_link(&xdr, &res->cinfo)) != 0)
3797 goto out;
3799 * Note order: OP_LINK leaves the directory as the current
3800 * filehandle.
3802 if (decode_getfattr(&xdr, res->dir_attr, res->server) != 0)
3803 goto out;
3804 if ((status = decode_restorefh(&xdr)) != 0)
3805 goto out;
3806 decode_getfattr(&xdr, res->fattr, res->server);
3807 out:
3808 return status;
3812 * Decode CREATE response
3814 static int nfs4_xdr_dec_create(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_create_res *res)
3816 struct xdr_stream xdr;
3817 struct compound_hdr hdr;
3818 int status;
3820 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3821 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3822 goto out;
3823 if ((status = decode_putfh(&xdr)) != 0)
3824 goto out;
3825 if ((status = decode_savefh(&xdr)) != 0)
3826 goto out;
3827 if ((status = decode_create(&xdr,&res->dir_cinfo)) != 0)
3828 goto out;
3829 if ((status = decode_getfh(&xdr, res->fh)) != 0)
3830 goto out;
3831 if (decode_getfattr(&xdr, res->fattr, res->server) != 0)
3832 goto out;
3833 if ((status = decode_restorefh(&xdr)) != 0)
3834 goto out;
3835 decode_getfattr(&xdr, res->dir_fattr, res->server);
3836 out:
3837 return status;
3841 * Decode SYMLINK response
3843 static int nfs4_xdr_dec_symlink(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_create_res *res)
3845 return nfs4_xdr_dec_create(rqstp, p, res);
3849 * Decode GETATTR response
3851 static int nfs4_xdr_dec_getattr(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_getattr_res *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_getfattr(&xdr, res->fattr, res->server);
3865 out:
3866 return status;
3871 * Encode an SETACL request
3873 static int
3874 nfs4_xdr_enc_setacl(struct rpc_rqst *req, __be32 *p, struct nfs_setaclargs *args)
3876 struct xdr_stream xdr;
3877 struct compound_hdr hdr = {
3878 .nops = 2,
3880 int status;
3882 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
3883 encode_compound_hdr(&xdr, &hdr);
3884 status = encode_putfh(&xdr, args->fh);
3885 if (status)
3886 goto out;
3887 status = encode_setacl(&xdr, args);
3888 out:
3889 return status;
3892 * Decode SETACL response
3894 static int
3895 nfs4_xdr_dec_setacl(struct rpc_rqst *rqstp, __be32 *p, void *res)
3897 struct xdr_stream xdr;
3898 struct compound_hdr hdr;
3899 int status;
3901 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3902 status = decode_compound_hdr(&xdr, &hdr);
3903 if (status)
3904 goto out;
3905 status = decode_putfh(&xdr);
3906 if (status)
3907 goto out;
3908 status = decode_setattr(&xdr, res);
3909 out:
3910 return status;
3914 * Decode GETACL response
3916 static int
3917 nfs4_xdr_dec_getacl(struct rpc_rqst *rqstp, __be32 *p, size_t *acl_len)
3919 struct xdr_stream xdr;
3920 struct compound_hdr hdr;
3921 int status;
3923 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3924 status = decode_compound_hdr(&xdr, &hdr);
3925 if (status)
3926 goto out;
3927 status = decode_putfh(&xdr);
3928 if (status)
3929 goto out;
3930 status = decode_getacl(&xdr, rqstp, acl_len);
3932 out:
3933 return status;
3937 * Decode CLOSE response
3939 static int nfs4_xdr_dec_close(struct rpc_rqst *rqstp, __be32 *p, struct nfs_closeres *res)
3941 struct xdr_stream xdr;
3942 struct compound_hdr hdr;
3943 int status;
3945 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3946 status = decode_compound_hdr(&xdr, &hdr);
3947 if (status)
3948 goto out;
3949 status = decode_putfh(&xdr);
3950 if (status)
3951 goto out;
3952 status = decode_close(&xdr, res);
3953 if (status != 0)
3954 goto out;
3956 * Note: Server may do delete on close for this file
3957 * in which case the getattr call will fail with
3958 * an ESTALE error. Shouldn't be a problem,
3959 * though, since fattr->valid will remain unset.
3961 decode_getfattr(&xdr, res->fattr, res->server);
3962 out:
3963 return status;
3967 * Decode OPEN response
3969 static int nfs4_xdr_dec_open(struct rpc_rqst *rqstp, __be32 *p, struct nfs_openres *res)
3971 struct xdr_stream xdr;
3972 struct compound_hdr hdr;
3973 int status;
3975 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3976 status = decode_compound_hdr(&xdr, &hdr);
3977 if (status)
3978 goto out;
3979 status = decode_putfh(&xdr);
3980 if (status)
3981 goto out;
3982 status = decode_savefh(&xdr);
3983 if (status)
3984 goto out;
3985 status = decode_open(&xdr, res);
3986 if (status)
3987 goto out;
3988 status = decode_getfh(&xdr, &res->fh);
3989 if (status)
3990 goto out;
3991 if (decode_getfattr(&xdr, res->f_attr, res->server) != 0)
3992 goto out;
3993 if ((status = decode_restorefh(&xdr)) != 0)
3994 goto out;
3995 decode_getfattr(&xdr, res->dir_attr, res->server);
3996 out:
3997 return status;
4001 * Decode OPEN_CONFIRM response
4003 static int nfs4_xdr_dec_open_confirm(struct rpc_rqst *rqstp, __be32 *p, struct nfs_open_confirmres *res)
4005 struct xdr_stream xdr;
4006 struct compound_hdr hdr;
4007 int status;
4009 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4010 status = decode_compound_hdr(&xdr, &hdr);
4011 if (status)
4012 goto out;
4013 status = decode_putfh(&xdr);
4014 if (status)
4015 goto out;
4016 status = decode_open_confirm(&xdr, res);
4017 out:
4018 return status;
4022 * Decode OPEN response
4024 static int nfs4_xdr_dec_open_noattr(struct rpc_rqst *rqstp, __be32 *p, struct nfs_openres *res)
4026 struct xdr_stream xdr;
4027 struct compound_hdr hdr;
4028 int status;
4030 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4031 status = decode_compound_hdr(&xdr, &hdr);
4032 if (status)
4033 goto out;
4034 status = decode_putfh(&xdr);
4035 if (status)
4036 goto out;
4037 status = decode_open(&xdr, res);
4038 if (status)
4039 goto out;
4040 decode_getfattr(&xdr, res->f_attr, res->server);
4041 out:
4042 return status;
4046 * Decode SETATTR response
4048 static int nfs4_xdr_dec_setattr(struct rpc_rqst *rqstp, __be32 *p, struct nfs_setattrres *res)
4050 struct xdr_stream xdr;
4051 struct compound_hdr hdr;
4052 int status;
4054 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4055 status = decode_compound_hdr(&xdr, &hdr);
4056 if (status)
4057 goto out;
4058 status = decode_putfh(&xdr);
4059 if (status)
4060 goto out;
4061 status = decode_setattr(&xdr, res);
4062 if (status)
4063 goto out;
4064 status = decode_getfattr(&xdr, res->fattr, res->server);
4065 if (status == NFS4ERR_DELAY)
4066 status = 0;
4067 out:
4068 return status;
4072 * Decode LOCK response
4074 static int nfs4_xdr_dec_lock(struct rpc_rqst *rqstp, __be32 *p, struct nfs_lock_res *res)
4076 struct xdr_stream xdr;
4077 struct compound_hdr hdr;
4078 int status;
4080 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4081 status = decode_compound_hdr(&xdr, &hdr);
4082 if (status)
4083 goto out;
4084 status = decode_putfh(&xdr);
4085 if (status)
4086 goto out;
4087 status = decode_lock(&xdr, res);
4088 out:
4089 return status;
4093 * Decode LOCKT response
4095 static int nfs4_xdr_dec_lockt(struct rpc_rqst *rqstp, __be32 *p, struct nfs_lockt_res *res)
4097 struct xdr_stream xdr;
4098 struct compound_hdr hdr;
4099 int status;
4101 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4102 status = decode_compound_hdr(&xdr, &hdr);
4103 if (status)
4104 goto out;
4105 status = decode_putfh(&xdr);
4106 if (status)
4107 goto out;
4108 status = decode_lockt(&xdr, res);
4109 out:
4110 return status;
4114 * Decode LOCKU response
4116 static int nfs4_xdr_dec_locku(struct rpc_rqst *rqstp, __be32 *p, struct nfs_locku_res *res)
4118 struct xdr_stream xdr;
4119 struct compound_hdr hdr;
4120 int status;
4122 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4123 status = decode_compound_hdr(&xdr, &hdr);
4124 if (status)
4125 goto out;
4126 status = decode_putfh(&xdr);
4127 if (status)
4128 goto out;
4129 status = decode_locku(&xdr, res);
4130 out:
4131 return status;
4135 * Decode READLINK response
4137 static int nfs4_xdr_dec_readlink(struct rpc_rqst *rqstp, __be32 *p, void *res)
4139 struct xdr_stream xdr;
4140 struct compound_hdr hdr;
4141 int status;
4143 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4144 status = decode_compound_hdr(&xdr, &hdr);
4145 if (status)
4146 goto out;
4147 status = decode_putfh(&xdr);
4148 if (status)
4149 goto out;
4150 status = decode_readlink(&xdr, rqstp);
4151 out:
4152 return status;
4156 * Decode READDIR response
4158 static int nfs4_xdr_dec_readdir(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_readdir_res *res)
4160 struct xdr_stream xdr;
4161 struct compound_hdr hdr;
4162 int status;
4164 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4165 status = decode_compound_hdr(&xdr, &hdr);
4166 if (status)
4167 goto out;
4168 status = decode_putfh(&xdr);
4169 if (status)
4170 goto out;
4171 status = decode_readdir(&xdr, rqstp, res);
4172 out:
4173 return status;
4177 * Decode Read response
4179 static int nfs4_xdr_dec_read(struct rpc_rqst *rqstp, __be32 *p, struct nfs_readres *res)
4181 struct xdr_stream xdr;
4182 struct compound_hdr hdr;
4183 int status;
4185 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4186 status = decode_compound_hdr(&xdr, &hdr);
4187 if (status)
4188 goto out;
4189 status = decode_putfh(&xdr);
4190 if (status)
4191 goto out;
4192 status = decode_read(&xdr, rqstp, res);
4193 if (!status)
4194 status = res->count;
4195 out:
4196 return status;
4200 * Decode WRITE response
4202 static int nfs4_xdr_dec_write(struct rpc_rqst *rqstp, __be32 *p, struct nfs_writeres *res)
4204 struct xdr_stream xdr;
4205 struct compound_hdr hdr;
4206 int status;
4208 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4209 status = decode_compound_hdr(&xdr, &hdr);
4210 if (status)
4211 goto out;
4212 status = decode_putfh(&xdr);
4213 if (status)
4214 goto out;
4215 status = decode_write(&xdr, res);
4216 if (status)
4217 goto out;
4218 decode_getfattr(&xdr, res->fattr, res->server);
4219 if (!status)
4220 status = res->count;
4221 out:
4222 return status;
4226 * Decode COMMIT response
4228 static int nfs4_xdr_dec_commit(struct rpc_rqst *rqstp, __be32 *p, struct nfs_writeres *res)
4230 struct xdr_stream xdr;
4231 struct compound_hdr hdr;
4232 int status;
4234 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4235 status = decode_compound_hdr(&xdr, &hdr);
4236 if (status)
4237 goto out;
4238 status = decode_putfh(&xdr);
4239 if (status)
4240 goto out;
4241 status = decode_commit(&xdr, res);
4242 if (status)
4243 goto out;
4244 decode_getfattr(&xdr, res->fattr, res->server);
4245 out:
4246 return status;
4250 * FSINFO request
4252 static int nfs4_xdr_dec_fsinfo(struct rpc_rqst *req, __be32 *p, struct nfs_fsinfo *fsinfo)
4254 struct xdr_stream xdr;
4255 struct compound_hdr hdr;
4256 int status;
4258 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4259 status = decode_compound_hdr(&xdr, &hdr);
4260 if (!status)
4261 status = decode_putfh(&xdr);
4262 if (!status)
4263 status = decode_fsinfo(&xdr, fsinfo);
4264 if (!status)
4265 status = -nfs4_stat_to_errno(hdr.status);
4266 return status;
4270 * PATHCONF request
4272 static int nfs4_xdr_dec_pathconf(struct rpc_rqst *req, __be32 *p, struct nfs_pathconf *pathconf)
4274 struct xdr_stream xdr;
4275 struct compound_hdr hdr;
4276 int status;
4278 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4279 status = decode_compound_hdr(&xdr, &hdr);
4280 if (!status)
4281 status = decode_putfh(&xdr);
4282 if (!status)
4283 status = decode_pathconf(&xdr, pathconf);
4284 return status;
4288 * STATFS request
4290 static int nfs4_xdr_dec_statfs(struct rpc_rqst *req, __be32 *p, struct nfs_fsstat *fsstat)
4292 struct xdr_stream xdr;
4293 struct compound_hdr hdr;
4294 int status;
4296 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4297 status = decode_compound_hdr(&xdr, &hdr);
4298 if (!status)
4299 status = decode_putfh(&xdr);
4300 if (!status)
4301 status = decode_statfs(&xdr, fsstat);
4302 return status;
4306 * GETATTR_BITMAP request
4308 static int nfs4_xdr_dec_server_caps(struct rpc_rqst *req, __be32 *p, struct nfs4_server_caps_res *res)
4310 struct xdr_stream xdr;
4311 struct compound_hdr hdr;
4312 int status;
4314 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4315 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
4316 goto out;
4317 if ((status = decode_putfh(&xdr)) != 0)
4318 goto out;
4319 status = decode_server_caps(&xdr, res);
4320 out:
4321 return status;
4325 * Decode RENEW response
4327 static int nfs4_xdr_dec_renew(struct rpc_rqst *rqstp, __be32 *p, void *dummy)
4329 struct xdr_stream xdr;
4330 struct compound_hdr hdr;
4331 int status;
4333 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4334 status = decode_compound_hdr(&xdr, &hdr);
4335 if (!status)
4336 status = decode_renew(&xdr);
4337 return status;
4341 * a SETCLIENTID request
4343 static int nfs4_xdr_dec_setclientid(struct rpc_rqst *req, __be32 *p,
4344 struct nfs_client *clp)
4346 struct xdr_stream xdr;
4347 struct compound_hdr hdr;
4348 int status;
4350 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4351 status = decode_compound_hdr(&xdr, &hdr);
4352 if (!status)
4353 status = decode_setclientid(&xdr, clp);
4354 if (!status)
4355 status = -nfs4_stat_to_errno(hdr.status);
4356 return status;
4360 * a SETCLIENTID_CONFIRM request
4362 static int nfs4_xdr_dec_setclientid_confirm(struct rpc_rqst *req, __be32 *p, struct nfs_fsinfo *fsinfo)
4364 struct xdr_stream xdr;
4365 struct compound_hdr hdr;
4366 int status;
4368 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4369 status = decode_compound_hdr(&xdr, &hdr);
4370 if (!status)
4371 status = decode_setclientid_confirm(&xdr);
4372 if (!status)
4373 status = decode_putrootfh(&xdr);
4374 if (!status)
4375 status = decode_fsinfo(&xdr, fsinfo);
4376 if (!status)
4377 status = -nfs4_stat_to_errno(hdr.status);
4378 return status;
4382 * DELEGRETURN request
4384 static int nfs4_xdr_dec_delegreturn(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_delegreturnres *res)
4386 struct xdr_stream xdr;
4387 struct compound_hdr hdr;
4388 int status;
4390 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4391 status = decode_compound_hdr(&xdr, &hdr);
4392 if (status != 0)
4393 goto out;
4394 status = decode_putfh(&xdr);
4395 if (status != 0)
4396 goto out;
4397 status = decode_delegreturn(&xdr);
4398 decode_getfattr(&xdr, res->fattr, res->server);
4399 out:
4400 return status;
4404 * FS_LOCATIONS request
4406 static int nfs4_xdr_dec_fs_locations(struct rpc_rqst *req, __be32 *p, struct nfs4_fs_locations *res)
4408 struct xdr_stream xdr;
4409 struct compound_hdr hdr;
4410 int status;
4412 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4413 status = decode_compound_hdr(&xdr, &hdr);
4414 if (status != 0)
4415 goto out;
4416 if ((status = decode_putfh(&xdr)) != 0)
4417 goto out;
4418 if ((status = decode_lookup(&xdr)) != 0)
4419 goto out;
4420 xdr_enter_page(&xdr, PAGE_SIZE);
4421 status = decode_getfattr(&xdr, &res->fattr, res->server);
4422 out:
4423 return status;
4426 __be32 *nfs4_decode_dirent(__be32 *p, struct nfs_entry *entry, int plus)
4428 uint32_t bitmap[2] = {0};
4429 uint32_t len;
4431 if (!*p++) {
4432 if (!*p)
4433 return ERR_PTR(-EAGAIN);
4434 entry->eof = 1;
4435 return ERR_PTR(-EBADCOOKIE);
4438 entry->prev_cookie = entry->cookie;
4439 p = xdr_decode_hyper(p, &entry->cookie);
4440 entry->len = ntohl(*p++);
4441 entry->name = (const char *) p;
4442 p += XDR_QUADLEN(entry->len);
4445 * In case the server doesn't return an inode number,
4446 * we fake one here. (We don't use inode number 0,
4447 * since glibc seems to choke on it...)
4449 entry->ino = 1;
4451 len = ntohl(*p++); /* bitmap length */
4452 if (len-- > 0) {
4453 bitmap[0] = ntohl(*p++);
4454 if (len-- > 0) {
4455 bitmap[1] = ntohl(*p++);
4456 p += len;
4459 len = XDR_QUADLEN(ntohl(*p++)); /* attribute buffer length */
4460 if (len > 0) {
4461 if (bitmap[0] & FATTR4_WORD0_RDATTR_ERROR) {
4462 bitmap[0] &= ~FATTR4_WORD0_RDATTR_ERROR;
4463 /* Ignore the return value of rdattr_error for now */
4464 p++;
4465 len--;
4467 if (bitmap[0] == 0 && bitmap[1] == FATTR4_WORD1_MOUNTED_ON_FILEID)
4468 xdr_decode_hyper(p, &entry->ino);
4469 else if (bitmap[0] == FATTR4_WORD0_FILEID)
4470 xdr_decode_hyper(p, &entry->ino);
4471 p += len;
4474 entry->eof = !p[0] && p[1];
4475 return p;
4479 * We need to translate between nfs status return values and
4480 * the local errno values which may not be the same.
4482 static struct {
4483 int stat;
4484 int errno;
4485 } nfs_errtbl[] = {
4486 { NFS4_OK, 0 },
4487 { NFS4ERR_PERM, EPERM },
4488 { NFS4ERR_NOENT, ENOENT },
4489 { NFS4ERR_IO, errno_NFSERR_IO },
4490 { NFS4ERR_NXIO, ENXIO },
4491 { NFS4ERR_ACCESS, EACCES },
4492 { NFS4ERR_EXIST, EEXIST },
4493 { NFS4ERR_XDEV, EXDEV },
4494 { NFS4ERR_NOTDIR, ENOTDIR },
4495 { NFS4ERR_ISDIR, EISDIR },
4496 { NFS4ERR_INVAL, EINVAL },
4497 { NFS4ERR_FBIG, EFBIG },
4498 { NFS4ERR_NOSPC, ENOSPC },
4499 { NFS4ERR_ROFS, EROFS },
4500 { NFS4ERR_MLINK, EMLINK },
4501 { NFS4ERR_NAMETOOLONG, ENAMETOOLONG },
4502 { NFS4ERR_NOTEMPTY, ENOTEMPTY },
4503 { NFS4ERR_DQUOT, EDQUOT },
4504 { NFS4ERR_STALE, ESTALE },
4505 { NFS4ERR_BADHANDLE, EBADHANDLE },
4506 { NFS4ERR_BADOWNER, EINVAL },
4507 { NFS4ERR_BADNAME, EINVAL },
4508 { NFS4ERR_BAD_COOKIE, EBADCOOKIE },
4509 { NFS4ERR_NOTSUPP, ENOTSUPP },
4510 { NFS4ERR_TOOSMALL, ETOOSMALL },
4511 { NFS4ERR_SERVERFAULT, ESERVERFAULT },
4512 { NFS4ERR_BADTYPE, EBADTYPE },
4513 { NFS4ERR_LOCKED, EAGAIN },
4514 { NFS4ERR_RESOURCE, EREMOTEIO },
4515 { NFS4ERR_SYMLINK, ELOOP },
4516 { NFS4ERR_OP_ILLEGAL, EOPNOTSUPP },
4517 { NFS4ERR_DEADLOCK, EDEADLK },
4518 { NFS4ERR_WRONGSEC, EPERM }, /* FIXME: this needs
4519 * to be handled by a
4520 * middle-layer.
4522 { -1, EIO }
4526 * Convert an NFS error code to a local one.
4527 * This one is used jointly by NFSv2 and NFSv3.
4529 static int
4530 nfs4_stat_to_errno(int stat)
4532 int i;
4533 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
4534 if (nfs_errtbl[i].stat == stat)
4535 return nfs_errtbl[i].errno;
4537 if (stat <= 10000 || stat > 10100) {
4538 /* The server is looney tunes. */
4539 return ESERVERFAULT;
4541 /* If we cannot translate the error, the recovery routines should
4542 * handle it.
4543 * Note: remaining NFSv4 error codes have values > 10000, so should
4544 * not conflict with native Linux error codes.
4546 return stat;
4549 #define PROC(proc, argtype, restype) \
4550 [NFSPROC4_CLNT_##proc] = { \
4551 .p_proc = NFSPROC4_COMPOUND, \
4552 .p_encode = (kxdrproc_t) nfs4_xdr_##argtype, \
4553 .p_decode = (kxdrproc_t) nfs4_xdr_##restype, \
4554 .p_arglen = NFS4_##argtype##_sz, \
4555 .p_replen = NFS4_##restype##_sz, \
4556 .p_statidx = NFSPROC4_CLNT_##proc, \
4557 .p_name = #proc, \
4560 struct rpc_procinfo nfs4_procedures[] = {
4561 PROC(READ, enc_read, dec_read),
4562 PROC(WRITE, enc_write, dec_write),
4563 PROC(COMMIT, enc_commit, dec_commit),
4564 PROC(OPEN, enc_open, dec_open),
4565 PROC(OPEN_CONFIRM, enc_open_confirm, dec_open_confirm),
4566 PROC(OPEN_NOATTR, enc_open_noattr, dec_open_noattr),
4567 PROC(OPEN_DOWNGRADE, enc_open_downgrade, dec_open_downgrade),
4568 PROC(CLOSE, enc_close, dec_close),
4569 PROC(SETATTR, enc_setattr, dec_setattr),
4570 PROC(FSINFO, enc_fsinfo, dec_fsinfo),
4571 PROC(RENEW, enc_renew, dec_renew),
4572 PROC(SETCLIENTID, enc_setclientid, dec_setclientid),
4573 PROC(SETCLIENTID_CONFIRM, enc_setclientid_confirm, dec_setclientid_confirm),
4574 PROC(LOCK, enc_lock, dec_lock),
4575 PROC(LOCKT, enc_lockt, dec_lockt),
4576 PROC(LOCKU, enc_locku, dec_locku),
4577 PROC(ACCESS, enc_access, dec_access),
4578 PROC(GETATTR, enc_getattr, dec_getattr),
4579 PROC(LOOKUP, enc_lookup, dec_lookup),
4580 PROC(LOOKUP_ROOT, enc_lookup_root, dec_lookup_root),
4581 PROC(REMOVE, enc_remove, dec_remove),
4582 PROC(RENAME, enc_rename, dec_rename),
4583 PROC(LINK, enc_link, dec_link),
4584 PROC(SYMLINK, enc_symlink, dec_symlink),
4585 PROC(CREATE, enc_create, dec_create),
4586 PROC(PATHCONF, enc_pathconf, dec_pathconf),
4587 PROC(STATFS, enc_statfs, dec_statfs),
4588 PROC(READLINK, enc_readlink, dec_readlink),
4589 PROC(READDIR, enc_readdir, dec_readdir),
4590 PROC(SERVER_CAPS, enc_server_caps, dec_server_caps),
4591 PROC(DELEGRETURN, enc_delegreturn, dec_delegreturn),
4592 PROC(GETACL, enc_getacl, dec_getacl),
4593 PROC(SETACL, enc_setacl, dec_setacl),
4594 PROC(FS_LOCATIONS, enc_fs_locations, dec_fs_locations),
4597 struct rpc_version nfs_version4 = {
4598 .number = 4,
4599 .nrprocs = ARRAY_SIZE(nfs4_procedures),
4600 .procs = nfs4_procedures
4604 * Local variables:
4605 * c-basic-offset: 8
4606 * End: