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