blob: a7b5de899c6ddf78fecabac6d1a639b3d6578ab8 [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 + \
395 encode_delegreturn_maxsz)
396#define NFS4_dec_delegreturn_sz (compound_decode_hdr_maxsz + \
397 decode_delegreturn_maxsz)
J. Bruce Fields029d1052005-06-22 17:16:22 +0000398#define NFS4_enc_getacl_sz (compound_encode_hdr_maxsz + \
399 encode_putfh_maxsz + \
400 encode_getattr_maxsz)
401#define NFS4_dec_getacl_sz (compound_decode_hdr_maxsz + \
402 decode_putfh_maxsz + \
403 op_decode_hdr_maxsz + \
404 nfs4_fattr_bitmap_maxsz + 1)
J. Bruce Fields23ec6962005-06-22 17:16:22 +0000405#define NFS4_enc_setacl_sz (compound_encode_hdr_maxsz + \
406 encode_putfh_maxsz + \
407 op_encode_hdr_maxsz + 4 + \
408 nfs4_fattr_bitmap_maxsz + 1)
409#define NFS4_dec_setacl_sz (compound_decode_hdr_maxsz + \
410 decode_putfh_maxsz + \
411 op_decode_hdr_maxsz + nfs4_fattr_bitmap_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413static struct {
414 unsigned int mode;
415 unsigned int nfs2type;
416} nfs_type2fmt[] = {
417 { 0, NFNON },
418 { S_IFREG, NFREG },
419 { S_IFDIR, NFDIR },
420 { S_IFBLK, NFBLK },
421 { S_IFCHR, NFCHR },
422 { S_IFLNK, NFLNK },
423 { S_IFSOCK, NFSOCK },
424 { S_IFIFO, NFFIFO },
425 { 0, NFNON },
426 { 0, NFNON },
427};
428
429struct compound_hdr {
430 int32_t status;
431 uint32_t nops;
432 uint32_t taglen;
433 char * tag;
434};
435
436/*
437 * START OF "GENERIC" ENCODE ROUTINES.
438 * These may look a little ugly since they are imported from a "generic"
439 * set of XDR encode/decode routines which are intended to be shared by
440 * all of our NFSv4 implementations (OpenBSD, MacOS X...).
441 *
442 * If the pain of reading these is too great, it should be a straightforward
443 * task to translate them into Linux-specific versions which are more
444 * consistent with the style used in NFSv2/v3...
445 */
446#define WRITE32(n) *p++ = htonl(n)
447#define WRITE64(n) do { \
448 *p++ = htonl((uint32_t)((n) >> 32)); \
449 *p++ = htonl((uint32_t)(n)); \
450} while (0)
451#define WRITEMEM(ptr,nbytes) do { \
452 p = xdr_encode_opaque_fixed(p, ptr, nbytes); \
453} while (0)
454
455#define RESERVE_SPACE(nbytes) do { \
456 p = xdr_reserve_space(xdr, nbytes); \
457 if (!p) printk("RESERVE_SPACE(%d) failed in function %s\n", (int) (nbytes), __FUNCTION__); \
458 BUG_ON(!p); \
459} while (0)
460
461static void encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
462{
463 uint32_t *p;
464
465 p = xdr_reserve_space(xdr, 4 + len);
466 BUG_ON(p == NULL);
467 xdr_encode_opaque(p, str, len);
468}
469
470static int encode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
471{
472 uint32_t *p;
473
474 dprintk("encode_compound: tag=%.*s\n", (int)hdr->taglen, hdr->tag);
475 BUG_ON(hdr->taglen > NFS4_MAXTAGLEN);
476 RESERVE_SPACE(12+(XDR_QUADLEN(hdr->taglen)<<2));
477 WRITE32(hdr->taglen);
478 WRITEMEM(hdr->tag, hdr->taglen);
479 WRITE32(NFS4_MINOR_VERSION);
480 WRITE32(hdr->nops);
481 return 0;
482}
483
484static void encode_nfs4_verifier(struct xdr_stream *xdr, const nfs4_verifier *verf)
485{
486 uint32_t *p;
487
488 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
489 BUG_ON(p == NULL);
490 xdr_encode_opaque_fixed(p, verf->data, NFS4_VERIFIER_SIZE);
491}
492
493static int encode_attrs(struct xdr_stream *xdr, const struct iattr *iap, const struct nfs_server *server)
494{
495 char owner_name[IDMAP_NAMESZ];
496 char owner_group[IDMAP_NAMESZ];
497 int owner_namelen = 0;
498 int owner_grouplen = 0;
499 uint32_t *p;
500 uint32_t *q;
501 int len;
502 uint32_t bmval0 = 0;
503 uint32_t bmval1 = 0;
504 int status;
505
506 /*
507 * We reserve enough space to write the entire attribute buffer at once.
508 * In the worst-case, this would be
509 * 12(bitmap) + 4(attrlen) + 8(size) + 4(mode) + 4(atime) + 4(mtime)
510 * = 36 bytes, plus any contribution from variable-length fields
J. Bruce Fields23ec6962005-06-22 17:16:22 +0000511 * such as owner/group.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 */
513 len = 16;
514
515 /* Sigh */
516 if (iap->ia_valid & ATTR_SIZE)
517 len += 8;
518 if (iap->ia_valid & ATTR_MODE)
519 len += 4;
520 if (iap->ia_valid & ATTR_UID) {
521 owner_namelen = nfs_map_uid_to_name(server->nfs4_state, iap->ia_uid, owner_name);
522 if (owner_namelen < 0) {
523 printk(KERN_WARNING "nfs: couldn't resolve uid %d to string\n",
524 iap->ia_uid);
525 /* XXX */
526 strcpy(owner_name, "nobody");
527 owner_namelen = sizeof("nobody") - 1;
528 /* goto out; */
529 }
530 len += 4 + (XDR_QUADLEN(owner_namelen) << 2);
531 }
532 if (iap->ia_valid & ATTR_GID) {
533 owner_grouplen = nfs_map_gid_to_group(server->nfs4_state, iap->ia_gid, owner_group);
534 if (owner_grouplen < 0) {
535 printk(KERN_WARNING "nfs4: couldn't resolve gid %d to string\n",
536 iap->ia_gid);
537 strcpy(owner_group, "nobody");
538 owner_grouplen = sizeof("nobody") - 1;
539 /* goto out; */
540 }
541 len += 4 + (XDR_QUADLEN(owner_grouplen) << 2);
542 }
543 if (iap->ia_valid & ATTR_ATIME_SET)
544 len += 16;
545 else if (iap->ia_valid & ATTR_ATIME)
546 len += 4;
547 if (iap->ia_valid & ATTR_MTIME_SET)
548 len += 16;
549 else if (iap->ia_valid & ATTR_MTIME)
550 len += 4;
551 RESERVE_SPACE(len);
552
553 /*
554 * We write the bitmap length now, but leave the bitmap and the attribute
555 * buffer length to be backfilled at the end of this routine.
556 */
557 WRITE32(2);
558 q = p;
559 p += 3;
560
561 if (iap->ia_valid & ATTR_SIZE) {
562 bmval0 |= FATTR4_WORD0_SIZE;
563 WRITE64(iap->ia_size);
564 }
565 if (iap->ia_valid & ATTR_MODE) {
566 bmval1 |= FATTR4_WORD1_MODE;
567 WRITE32(iap->ia_mode);
568 }
569 if (iap->ia_valid & ATTR_UID) {
570 bmval1 |= FATTR4_WORD1_OWNER;
571 WRITE32(owner_namelen);
572 WRITEMEM(owner_name, owner_namelen);
573 }
574 if (iap->ia_valid & ATTR_GID) {
575 bmval1 |= FATTR4_WORD1_OWNER_GROUP;
576 WRITE32(owner_grouplen);
577 WRITEMEM(owner_group, owner_grouplen);
578 }
579 if (iap->ia_valid & ATTR_ATIME_SET) {
580 bmval1 |= FATTR4_WORD1_TIME_ACCESS_SET;
581 WRITE32(NFS4_SET_TO_CLIENT_TIME);
582 WRITE32(0);
583 WRITE32(iap->ia_mtime.tv_sec);
584 WRITE32(iap->ia_mtime.tv_nsec);
585 }
586 else if (iap->ia_valid & ATTR_ATIME) {
587 bmval1 |= FATTR4_WORD1_TIME_ACCESS_SET;
588 WRITE32(NFS4_SET_TO_SERVER_TIME);
589 }
590 if (iap->ia_valid & ATTR_MTIME_SET) {
591 bmval1 |= FATTR4_WORD1_TIME_MODIFY_SET;
592 WRITE32(NFS4_SET_TO_CLIENT_TIME);
593 WRITE32(0);
594 WRITE32(iap->ia_mtime.tv_sec);
595 WRITE32(iap->ia_mtime.tv_nsec);
596 }
597 else if (iap->ia_valid & ATTR_MTIME) {
598 bmval1 |= FATTR4_WORD1_TIME_MODIFY_SET;
599 WRITE32(NFS4_SET_TO_SERVER_TIME);
600 }
601
602 /*
603 * Now we backfill the bitmap and the attribute buffer length.
604 */
605 if (len != ((char *)p - (char *)q) + 4) {
606 printk ("encode_attr: Attr length calculation error! %u != %Zu\n",
607 len, ((char *)p - (char *)q) + 4);
608 BUG();
609 }
610 len = (char *)p - (char *)q - 12;
611 *q++ = htonl(bmval0);
612 *q++ = htonl(bmval1);
613 *q++ = htonl(len);
614
615 status = 0;
616/* out: */
617 return status;
618}
619
620static int encode_access(struct xdr_stream *xdr, u32 access)
621{
622 uint32_t *p;
623
624 RESERVE_SPACE(8);
625 WRITE32(OP_ACCESS);
626 WRITE32(access);
627
628 return 0;
629}
630
631static int encode_close(struct xdr_stream *xdr, const struct nfs_closeargs *arg)
632{
633 uint32_t *p;
634
Trond Myklebust95121352005-10-18 14:20:12 -0700635 RESERVE_SPACE(8+sizeof(arg->stateid->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 WRITE32(OP_CLOSE);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700637 WRITE32(arg->seqid->sequence->counter);
Trond Myklebust95121352005-10-18 14:20:12 -0700638 WRITEMEM(arg->stateid->data, sizeof(arg->stateid->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 return 0;
641}
642
643static int encode_commit(struct xdr_stream *xdr, const struct nfs_writeargs *args)
644{
645 uint32_t *p;
646
647 RESERVE_SPACE(16);
648 WRITE32(OP_COMMIT);
649 WRITE64(args->offset);
650 WRITE32(args->count);
651
652 return 0;
653}
654
655static int encode_create(struct xdr_stream *xdr, const struct nfs4_create_arg *create)
656{
657 uint32_t *p;
658
659 RESERVE_SPACE(8);
660 WRITE32(OP_CREATE);
661 WRITE32(create->ftype);
662
663 switch (create->ftype) {
664 case NF4LNK:
665 RESERVE_SPACE(4 + create->u.symlink->len);
666 WRITE32(create->u.symlink->len);
667 WRITEMEM(create->u.symlink->name, create->u.symlink->len);
668 break;
669
670 case NF4BLK: case NF4CHR:
671 RESERVE_SPACE(8);
672 WRITE32(create->u.device.specdata1);
673 WRITE32(create->u.device.specdata2);
674 break;
675
676 default:
677 break;
678 }
679
680 RESERVE_SPACE(4 + create->name->len);
681 WRITE32(create->name->len);
682 WRITEMEM(create->name->name, create->name->len);
683
684 return encode_attrs(xdr, create->attrs, create->server);
685}
686
687static int encode_getattr_one(struct xdr_stream *xdr, uint32_t bitmap)
688{
689 uint32_t *p;
690
691 RESERVE_SPACE(12);
692 WRITE32(OP_GETATTR);
693 WRITE32(1);
694 WRITE32(bitmap);
695 return 0;
696}
697
698static int encode_getattr_two(struct xdr_stream *xdr, uint32_t bm0, uint32_t bm1)
699{
700 uint32_t *p;
701
702 RESERVE_SPACE(16);
703 WRITE32(OP_GETATTR);
704 WRITE32(2);
705 WRITE32(bm0);
706 WRITE32(bm1);
707 return 0;
708}
709
710static int encode_getfattr(struct xdr_stream *xdr, const u32* bitmask)
711{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return encode_getattr_two(xdr,
713 bitmask[0] & nfs4_fattr_bitmap[0],
714 bitmask[1] & nfs4_fattr_bitmap[1]);
715}
716
717static int encode_fsinfo(struct xdr_stream *xdr, const u32* bitmask)
718{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return encode_getattr_two(xdr, bitmask[0] & nfs4_fsinfo_bitmap[0],
720 bitmask[1] & nfs4_fsinfo_bitmap[1]);
721}
722
723static int encode_getfh(struct xdr_stream *xdr)
724{
725 uint32_t *p;
726
727 RESERVE_SPACE(4);
728 WRITE32(OP_GETFH);
729
730 return 0;
731}
732
733static int encode_link(struct xdr_stream *xdr, const struct qstr *name)
734{
735 uint32_t *p;
736
737 RESERVE_SPACE(8 + name->len);
738 WRITE32(OP_LINK);
739 WRITE32(name->len);
740 WRITEMEM(name->name, name->len);
741
742 return 0;
743}
744
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100745static inline int nfs4_lock_type(struct file_lock *fl, int block)
746{
747 if ((fl->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) == F_RDLCK)
748 return block ? NFS4_READW_LT : NFS4_READ_LT;
749 return block ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
750}
751
752static inline uint64_t nfs4_lock_length(struct file_lock *fl)
753{
754 if (fl->fl_end == OFFSET_MAX)
755 return ~(uint64_t)0;
756 return fl->fl_end - fl->fl_start + 1;
757}
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759/*
760 * opcode,type,reclaim,offset,length,new_lock_owner = 32
761 * open_seqid,open_stateid,lock_seqid,lock_owner.clientid, lock_owner.id = 40
762 */
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100763static int encode_lock(struct xdr_stream *xdr, const struct nfs_lock_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
765 uint32_t *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 RESERVE_SPACE(32);
768 WRITE32(OP_LOCK);
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100769 WRITE32(nfs4_lock_type(args->fl, args->block));
770 WRITE32(args->reclaim);
771 WRITE64(args->fl->fl_start);
772 WRITE64(nfs4_lock_length(args->fl));
773 WRITE32(args->new_lock_owner);
774 if (args->new_lock_owner){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 RESERVE_SPACE(40);
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100776 WRITE32(args->open_seqid->sequence->counter);
777 WRITEMEM(args->open_stateid->data, sizeof(args->open_stateid->data));
778 WRITE32(args->lock_seqid->sequence->counter);
779 WRITE64(args->lock_owner.clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 WRITE32(4);
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100781 WRITE32(args->lock_owner.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
783 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 RESERVE_SPACE(20);
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100785 WRITEMEM(args->lock_stateid->data, sizeof(args->lock_stateid->data));
786 WRITE32(args->lock_seqid->sequence->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
788
789 return 0;
790}
791
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100792static int encode_lockt(struct xdr_stream *xdr, const struct nfs_lockt_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
794 uint32_t *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 RESERVE_SPACE(40);
797 WRITE32(OP_LOCKT);
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100798 WRITE32(nfs4_lock_type(args->fl, 0));
799 WRITE64(args->fl->fl_start);
800 WRITE64(nfs4_lock_length(args->fl));
801 WRITE64(args->lock_owner.clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 WRITE32(4);
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100803 WRITE32(args->lock_owner.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 return 0;
806}
807
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100808static int encode_locku(struct xdr_stream *xdr, const struct nfs_locku_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 uint32_t *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 RESERVE_SPACE(44);
813 WRITE32(OP_LOCKU);
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100814 WRITE32(nfs4_lock_type(args->fl, 0));
815 WRITE32(args->seqid->sequence->counter);
816 WRITEMEM(args->stateid->data, sizeof(args->stateid->data));
817 WRITE64(args->fl->fl_start);
818 WRITE64(nfs4_lock_length(args->fl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 return 0;
821}
822
823static int encode_lookup(struct xdr_stream *xdr, const struct qstr *name)
824{
825 int len = name->len;
826 uint32_t *p;
827
828 RESERVE_SPACE(8 + len);
829 WRITE32(OP_LOOKUP);
830 WRITE32(len);
831 WRITEMEM(name->name, len);
832
833 return 0;
834}
835
836static void encode_share_access(struct xdr_stream *xdr, int open_flags)
837{
838 uint32_t *p;
839
840 RESERVE_SPACE(8);
841 switch (open_flags & (FMODE_READ|FMODE_WRITE)) {
842 case FMODE_READ:
843 WRITE32(NFS4_SHARE_ACCESS_READ);
844 break;
845 case FMODE_WRITE:
846 WRITE32(NFS4_SHARE_ACCESS_WRITE);
847 break;
848 case FMODE_READ|FMODE_WRITE:
849 WRITE32(NFS4_SHARE_ACCESS_BOTH);
850 break;
851 default:
852 BUG();
853 }
854 WRITE32(0); /* for linux, share_deny = 0 always */
855}
856
857static inline void encode_openhdr(struct xdr_stream *xdr, const struct nfs_openargs *arg)
858{
859 uint32_t *p;
860 /*
861 * opcode 4, seqid 4, share_access 4, share_deny 4, clientid 8, ownerlen 4,
862 * owner 4 = 32
863 */
864 RESERVE_SPACE(8);
865 WRITE32(OP_OPEN);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700866 WRITE32(arg->seqid->sequence->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 encode_share_access(xdr, arg->open_flags);
868 RESERVE_SPACE(16);
869 WRITE64(arg->clientid);
870 WRITE32(4);
871 WRITE32(arg->id);
872}
873
874static inline void encode_createmode(struct xdr_stream *xdr, const struct nfs_openargs *arg)
875{
876 uint32_t *p;
877
878 RESERVE_SPACE(4);
879 switch(arg->open_flags & O_EXCL) {
880 case 0:
881 WRITE32(NFS4_CREATE_UNCHECKED);
882 encode_attrs(xdr, arg->u.attrs, arg->server);
883 break;
884 default:
885 WRITE32(NFS4_CREATE_EXCLUSIVE);
886 encode_nfs4_verifier(xdr, &arg->u.verifier);
887 }
888}
889
890static void encode_opentype(struct xdr_stream *xdr, const struct nfs_openargs *arg)
891{
892 uint32_t *p;
893
894 RESERVE_SPACE(4);
895 switch (arg->open_flags & O_CREAT) {
896 case 0:
897 WRITE32(NFS4_OPEN_NOCREATE);
898 break;
899 default:
900 BUG_ON(arg->claim != NFS4_OPEN_CLAIM_NULL);
901 WRITE32(NFS4_OPEN_CREATE);
902 encode_createmode(xdr, arg);
903 }
904}
905
906static inline void encode_delegation_type(struct xdr_stream *xdr, int delegation_type)
907{
908 uint32_t *p;
909
910 RESERVE_SPACE(4);
911 switch (delegation_type) {
912 case 0:
913 WRITE32(NFS4_OPEN_DELEGATE_NONE);
914 break;
915 case FMODE_READ:
916 WRITE32(NFS4_OPEN_DELEGATE_READ);
917 break;
918 case FMODE_WRITE|FMODE_READ:
919 WRITE32(NFS4_OPEN_DELEGATE_WRITE);
920 break;
921 default:
922 BUG();
923 }
924}
925
926static inline void encode_claim_null(struct xdr_stream *xdr, const struct qstr *name)
927{
928 uint32_t *p;
929
930 RESERVE_SPACE(4);
931 WRITE32(NFS4_OPEN_CLAIM_NULL);
932 encode_string(xdr, name->len, name->name);
933}
934
935static inline void encode_claim_previous(struct xdr_stream *xdr, int type)
936{
937 uint32_t *p;
938
939 RESERVE_SPACE(4);
940 WRITE32(NFS4_OPEN_CLAIM_PREVIOUS);
941 encode_delegation_type(xdr, type);
942}
943
944static inline void encode_claim_delegate_cur(struct xdr_stream *xdr, const struct qstr *name, const nfs4_stateid *stateid)
945{
946 uint32_t *p;
947
948 RESERVE_SPACE(4+sizeof(stateid->data));
949 WRITE32(NFS4_OPEN_CLAIM_DELEGATE_CUR);
950 WRITEMEM(stateid->data, sizeof(stateid->data));
951 encode_string(xdr, name->len, name->name);
952}
953
954static int encode_open(struct xdr_stream *xdr, const struct nfs_openargs *arg)
955{
956 encode_openhdr(xdr, arg);
957 encode_opentype(xdr, arg);
958 switch (arg->claim) {
959 case NFS4_OPEN_CLAIM_NULL:
960 encode_claim_null(xdr, arg->name);
961 break;
962 case NFS4_OPEN_CLAIM_PREVIOUS:
963 encode_claim_previous(xdr, arg->u.delegation_type);
964 break;
965 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
966 encode_claim_delegate_cur(xdr, arg->name, &arg->u.delegation);
967 break;
968 default:
969 BUG();
970 }
971 return 0;
972}
973
974static int encode_open_confirm(struct xdr_stream *xdr, const struct nfs_open_confirmargs *arg)
975{
976 uint32_t *p;
977
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100978 RESERVE_SPACE(8+sizeof(arg->stateid->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 WRITE32(OP_OPEN_CONFIRM);
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100980 WRITEMEM(arg->stateid->data, sizeof(arg->stateid->data));
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700981 WRITE32(arg->seqid->sequence->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 return 0;
984}
985
986static int encode_open_downgrade(struct xdr_stream *xdr, const struct nfs_closeargs *arg)
987{
988 uint32_t *p;
989
Trond Myklebust95121352005-10-18 14:20:12 -0700990 RESERVE_SPACE(8+sizeof(arg->stateid->data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 WRITE32(OP_OPEN_DOWNGRADE);
Trond Myklebust95121352005-10-18 14:20:12 -0700992 WRITEMEM(arg->stateid->data, sizeof(arg->stateid->data));
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700993 WRITE32(arg->seqid->sequence->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 encode_share_access(xdr, arg->open_flags);
995 return 0;
996}
997
998static int
999encode_putfh(struct xdr_stream *xdr, const struct nfs_fh *fh)
1000{
1001 int len = fh->size;
1002 uint32_t *p;
1003
1004 RESERVE_SPACE(8 + len);
1005 WRITE32(OP_PUTFH);
1006 WRITE32(len);
1007 WRITEMEM(fh->data, len);
1008
1009 return 0;
1010}
1011
1012static int encode_putrootfh(struct xdr_stream *xdr)
1013{
1014 uint32_t *p;
1015
1016 RESERVE_SPACE(4);
1017 WRITE32(OP_PUTROOTFH);
1018
1019 return 0;
1020}
1021
1022static void encode_stateid(struct xdr_stream *xdr, const struct nfs_open_context *ctx)
1023{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 nfs4_stateid stateid;
1025 uint32_t *p;
1026
1027 RESERVE_SPACE(16);
1028 if (ctx->state != NULL) {
1029 nfs4_copy_stateid(&stateid, ctx->state, ctx->lockowner);
1030 WRITEMEM(stateid.data, sizeof(stateid.data));
1031 } else
1032 WRITEMEM(zero_stateid.data, sizeof(zero_stateid.data));
1033}
1034
1035static int encode_read(struct xdr_stream *xdr, const struct nfs_readargs *args)
1036{
1037 uint32_t *p;
1038
1039 RESERVE_SPACE(4);
1040 WRITE32(OP_READ);
1041
1042 encode_stateid(xdr, args->context);
1043
1044 RESERVE_SPACE(12);
1045 WRITE64(args->offset);
1046 WRITE32(args->count);
1047
1048 return 0;
1049}
1050
1051static int encode_readdir(struct xdr_stream *xdr, const struct nfs4_readdir_arg *readdir, struct rpc_rqst *req)
1052{
1053 struct rpc_auth *auth = req->rq_task->tk_auth;
Manoj Naik97d312d2005-06-22 17:16:39 +00001054 uint32_t attrs[2] = {
1055 FATTR4_WORD0_RDATTR_ERROR|FATTR4_WORD0_FILEID,
1056 FATTR4_WORD1_MOUNTED_ON_FILEID,
1057 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 int replen;
1059 uint32_t *p;
1060
1061 RESERVE_SPACE(32+sizeof(nfs4_verifier));
1062 WRITE32(OP_READDIR);
1063 WRITE64(readdir->cookie);
1064 WRITEMEM(readdir->verifier.data, sizeof(readdir->verifier.data));
1065 WRITE32(readdir->count >> 1); /* We're not doing readdirplus */
1066 WRITE32(readdir->count);
1067 WRITE32(2);
Manoj Naik97d312d2005-06-22 17:16:39 +00001068 /* Switch to mounted_on_fileid if the server supports it */
1069 if (readdir->bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
1070 attrs[0] &= ~FATTR4_WORD0_FILEID;
1071 else
1072 attrs[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
1073 WRITE32(attrs[0] & readdir->bitmask[0]);
1074 WRITE32(attrs[1] & readdir->bitmask[1]);
Trond Myklebusteadf4592005-06-22 17:16:39 +00001075 dprintk("%s: cookie = %Lu, verifier = 0x%x%x, bitmap = 0x%x%x\n",
1076 __FUNCTION__,
1077 (unsigned long long)readdir->cookie,
1078 ((u32 *)readdir->verifier.data)[0],
1079 ((u32 *)readdir->verifier.data)[1],
1080 attrs[0] & readdir->bitmask[0],
1081 attrs[1] & readdir->bitmask[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 /* set up reply kvec
1084 * toplevel_status + taglen + rescount + OP_PUTFH + status
1085 * + OP_READDIR + status + verifer(2) = 9
1086 */
1087 replen = (RPC_REPHDRSIZE + auth->au_rslack + 9) << 2;
1088 xdr_inline_pages(&req->rq_rcv_buf, replen, readdir->pages,
1089 readdir->pgbase, readdir->count);
Trond Myklebusteadf4592005-06-22 17:16:39 +00001090 dprintk("%s: inlined page args = (%u, %p, %u, %u)\n",
1091 __FUNCTION__, replen, readdir->pages,
1092 readdir->pgbase, readdir->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 return 0;
1095}
1096
1097static int encode_readlink(struct xdr_stream *xdr, const struct nfs4_readlink *readlink, struct rpc_rqst *req)
1098{
1099 struct rpc_auth *auth = req->rq_task->tk_auth;
1100 unsigned int replen;
1101 uint32_t *p;
1102
1103 RESERVE_SPACE(4);
1104 WRITE32(OP_READLINK);
1105
1106 /* set up reply kvec
1107 * toplevel_status + taglen + rescount + OP_PUTFH + status
1108 * + OP_READLINK + status + string length = 8
1109 */
1110 replen = (RPC_REPHDRSIZE + auth->au_rslack + 8) << 2;
1111 xdr_inline_pages(&req->rq_rcv_buf, replen, readlink->pages,
1112 readlink->pgbase, readlink->pglen);
1113
1114 return 0;
1115}
1116
1117static int encode_remove(struct xdr_stream *xdr, const struct qstr *name)
1118{
1119 uint32_t *p;
1120
1121 RESERVE_SPACE(8 + name->len);
1122 WRITE32(OP_REMOVE);
1123 WRITE32(name->len);
1124 WRITEMEM(name->name, name->len);
1125
1126 return 0;
1127}
1128
1129static int encode_rename(struct xdr_stream *xdr, const struct qstr *oldname, const struct qstr *newname)
1130{
1131 uint32_t *p;
1132
1133 RESERVE_SPACE(8 + oldname->len);
1134 WRITE32(OP_RENAME);
1135 WRITE32(oldname->len);
1136 WRITEMEM(oldname->name, oldname->len);
1137
1138 RESERVE_SPACE(4 + newname->len);
1139 WRITE32(newname->len);
1140 WRITEMEM(newname->name, newname->len);
1141
1142 return 0;
1143}
1144
1145static int encode_renew(struct xdr_stream *xdr, const struct nfs4_client *client_stateid)
1146{
1147 uint32_t *p;
1148
1149 RESERVE_SPACE(12);
1150 WRITE32(OP_RENEW);
1151 WRITE64(client_stateid->cl_clientid);
1152
1153 return 0;
1154}
1155
1156static int
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001157encode_restorefh(struct xdr_stream *xdr)
1158{
1159 uint32_t *p;
1160
1161 RESERVE_SPACE(4);
1162 WRITE32(OP_RESTOREFH);
1163
1164 return 0;
1165}
1166
1167static int
J. Bruce Fields23ec6962005-06-22 17:16:22 +00001168encode_setacl(struct xdr_stream *xdr, struct nfs_setaclargs *arg)
1169{
1170 uint32_t *p;
1171
1172 RESERVE_SPACE(4+sizeof(zero_stateid.data));
1173 WRITE32(OP_SETATTR);
1174 WRITEMEM(zero_stateid.data, sizeof(zero_stateid.data));
1175 RESERVE_SPACE(2*4);
1176 WRITE32(1);
1177 WRITE32(FATTR4_WORD0_ACL);
1178 if (arg->acl_len % 4)
1179 return -EINVAL;
1180 RESERVE_SPACE(4);
1181 WRITE32(arg->acl_len);
1182 xdr_write_pages(xdr, arg->acl_pages, arg->acl_pgbase, arg->acl_len);
1183 return 0;
1184}
1185
1186static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187encode_savefh(struct xdr_stream *xdr)
1188{
1189 uint32_t *p;
1190
1191 RESERVE_SPACE(4);
1192 WRITE32(OP_SAVEFH);
1193
1194 return 0;
1195}
1196
1197static int encode_setattr(struct xdr_stream *xdr, const struct nfs_setattrargs *arg, const struct nfs_server *server)
1198{
1199 int status;
1200 uint32_t *p;
1201
1202 RESERVE_SPACE(4+sizeof(arg->stateid.data));
1203 WRITE32(OP_SETATTR);
1204 WRITEMEM(arg->stateid.data, sizeof(arg->stateid.data));
1205
1206 if ((status = encode_attrs(xdr, arg->iap, server)))
1207 return status;
1208
1209 return 0;
1210}
1211
1212static int encode_setclientid(struct xdr_stream *xdr, const struct nfs4_setclientid *setclientid)
1213{
1214 uint32_t *p;
1215
1216 RESERVE_SPACE(4 + sizeof(setclientid->sc_verifier->data));
1217 WRITE32(OP_SETCLIENTID);
1218 WRITEMEM(setclientid->sc_verifier->data, sizeof(setclientid->sc_verifier->data));
1219
1220 encode_string(xdr, setclientid->sc_name_len, setclientid->sc_name);
1221 RESERVE_SPACE(4);
1222 WRITE32(setclientid->sc_prog);
1223 encode_string(xdr, setclientid->sc_netid_len, setclientid->sc_netid);
1224 encode_string(xdr, setclientid->sc_uaddr_len, setclientid->sc_uaddr);
1225 RESERVE_SPACE(4);
1226 WRITE32(setclientid->sc_cb_ident);
1227
1228 return 0;
1229}
1230
1231static int encode_setclientid_confirm(struct xdr_stream *xdr, const struct nfs4_client *client_state)
1232{
1233 uint32_t *p;
1234
1235 RESERVE_SPACE(12 + sizeof(client_state->cl_confirm.data));
1236 WRITE32(OP_SETCLIENTID_CONFIRM);
1237 WRITE64(client_state->cl_clientid);
1238 WRITEMEM(client_state->cl_confirm.data, sizeof(client_state->cl_confirm.data));
1239
1240 return 0;
1241}
1242
1243static int encode_write(struct xdr_stream *xdr, const struct nfs_writeargs *args)
1244{
1245 uint32_t *p;
1246
1247 RESERVE_SPACE(4);
1248 WRITE32(OP_WRITE);
1249
1250 encode_stateid(xdr, args->context);
1251
1252 RESERVE_SPACE(16);
1253 WRITE64(args->offset);
1254 WRITE32(args->stable);
1255 WRITE32(args->count);
1256
1257 xdr_write_pages(xdr, args->pages, args->pgbase, args->count);
1258
1259 return 0;
1260}
1261
1262static int encode_delegreturn(struct xdr_stream *xdr, const nfs4_stateid *stateid)
1263{
1264 uint32_t *p;
1265
1266 RESERVE_SPACE(20);
1267
1268 WRITE32(OP_DELEGRETURN);
1269 WRITEMEM(stateid->data, sizeof(stateid->data));
1270 return 0;
1271
1272}
1273/*
1274 * END OF "GENERIC" ENCODE ROUTINES.
1275 */
1276
1277/*
1278 * Encode an ACCESS request
1279 */
1280static int nfs4_xdr_enc_access(struct rpc_rqst *req, uint32_t *p, const struct nfs4_accessargs *args)
1281{
1282 struct xdr_stream xdr;
1283 struct compound_hdr hdr = {
1284 .nops = 2,
1285 };
1286 int status;
1287
1288 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1289 encode_compound_hdr(&xdr, &hdr);
1290 if ((status = encode_putfh(&xdr, args->fh)) == 0)
1291 status = encode_access(&xdr, args->access);
1292 return status;
1293}
1294
1295/*
1296 * Encode LOOKUP request
1297 */
1298static int nfs4_xdr_enc_lookup(struct rpc_rqst *req, uint32_t *p, const struct nfs4_lookup_arg *args)
1299{
1300 struct xdr_stream xdr;
1301 struct compound_hdr hdr = {
1302 .nops = 4,
1303 };
1304 int status;
1305
1306 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1307 encode_compound_hdr(&xdr, &hdr);
1308 if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1309 goto out;
1310 if ((status = encode_lookup(&xdr, args->name)) != 0)
1311 goto out;
1312 if ((status = encode_getfh(&xdr)) != 0)
1313 goto out;
1314 status = encode_getfattr(&xdr, args->bitmask);
1315out:
1316 return status;
1317}
1318
1319/*
1320 * Encode LOOKUP_ROOT request
1321 */
1322static int nfs4_xdr_enc_lookup_root(struct rpc_rqst *req, uint32_t *p, const struct nfs4_lookup_root_arg *args)
1323{
1324 struct xdr_stream xdr;
1325 struct compound_hdr hdr = {
1326 .nops = 3,
1327 };
1328 int status;
1329
1330 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1331 encode_compound_hdr(&xdr, &hdr);
1332 if ((status = encode_putrootfh(&xdr)) != 0)
1333 goto out;
1334 if ((status = encode_getfh(&xdr)) == 0)
1335 status = encode_getfattr(&xdr, args->bitmask);
1336out:
1337 return status;
1338}
1339
1340/*
1341 * Encode REMOVE request
1342 */
1343static int nfs4_xdr_enc_remove(struct rpc_rqst *req, uint32_t *p, const struct nfs4_remove_arg *args)
1344{
1345 struct xdr_stream xdr;
1346 struct compound_hdr hdr = {
Trond Myklebust16e42952005-10-27 22:12:44 -04001347 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 };
1349 int status;
1350
1351 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1352 encode_compound_hdr(&xdr, &hdr);
Trond Myklebust16e42952005-10-27 22:12:44 -04001353 if ((status = encode_putfh(&xdr, args->fh)) != 0)
1354 goto out;
1355 if ((status = encode_remove(&xdr, args->name)) != 0)
1356 goto out;
1357 status = encode_getfattr(&xdr, args->bitmask);
1358out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return status;
1360}
1361
1362/*
1363 * Encode RENAME request
1364 */
1365static int nfs4_xdr_enc_rename(struct rpc_rqst *req, uint32_t *p, const struct nfs4_rename_arg *args)
1366{
1367 struct xdr_stream xdr;
1368 struct compound_hdr hdr = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001369 .nops = 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 };
1371 int status;
1372
1373 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1374 encode_compound_hdr(&xdr, &hdr);
1375 if ((status = encode_putfh(&xdr, args->old_dir)) != 0)
1376 goto out;
1377 if ((status = encode_savefh(&xdr)) != 0)
1378 goto out;
1379 if ((status = encode_putfh(&xdr, args->new_dir)) != 0)
1380 goto out;
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001381 if ((status = encode_rename(&xdr, args->old_name, args->new_name)) != 0)
1382 goto out;
1383 if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
1384 goto out;
1385 if ((status = encode_restorefh(&xdr)) != 0)
1386 goto out;
1387 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388out:
1389 return status;
1390}
1391
1392/*
1393 * Encode LINK request
1394 */
1395static int nfs4_xdr_enc_link(struct rpc_rqst *req, uint32_t *p, const struct nfs4_link_arg *args)
1396{
1397 struct xdr_stream xdr;
1398 struct compound_hdr hdr = {
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001399 .nops = 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 };
1401 int status;
1402
1403 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1404 encode_compound_hdr(&xdr, &hdr);
1405 if ((status = encode_putfh(&xdr, args->fh)) != 0)
1406 goto out;
1407 if ((status = encode_savefh(&xdr)) != 0)
1408 goto out;
1409 if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1410 goto out;
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001411 if ((status = encode_link(&xdr, args->name)) != 0)
1412 goto out;
1413 if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
1414 goto out;
1415 if ((status = encode_restorefh(&xdr)) != 0)
1416 goto out;
1417 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418out:
1419 return status;
1420}
1421
1422/*
1423 * Encode CREATE request
1424 */
1425static int nfs4_xdr_enc_create(struct rpc_rqst *req, uint32_t *p, const struct nfs4_create_arg *args)
1426{
1427 struct xdr_stream xdr;
1428 struct compound_hdr hdr = {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001429 .nops = 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 };
1431 int status;
1432
1433 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1434 encode_compound_hdr(&xdr, &hdr);
1435 if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1436 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001437 if ((status = encode_savefh(&xdr)) != 0)
1438 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if ((status = encode_create(&xdr, args)) != 0)
1440 goto out;
1441 if ((status = encode_getfh(&xdr)) != 0)
1442 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001443 if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
1444 goto out;
1445 if ((status = encode_restorefh(&xdr)) != 0)
1446 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 status = encode_getfattr(&xdr, args->bitmask);
1448out:
1449 return status;
1450}
1451
1452/*
1453 * Encode SYMLINK request
1454 */
1455static int nfs4_xdr_enc_symlink(struct rpc_rqst *req, uint32_t *p, const struct nfs4_create_arg *args)
1456{
1457 return nfs4_xdr_enc_create(req, p, args);
1458}
1459
1460/*
1461 * Encode GETATTR request
1462 */
1463static int nfs4_xdr_enc_getattr(struct rpc_rqst *req, uint32_t *p, const struct nfs4_getattr_arg *args)
1464{
1465 struct xdr_stream xdr;
1466 struct compound_hdr hdr = {
1467 .nops = 2,
1468 };
1469 int status;
1470
1471 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1472 encode_compound_hdr(&xdr, &hdr);
1473 if ((status = encode_putfh(&xdr, args->fh)) == 0)
1474 status = encode_getfattr(&xdr, args->bitmask);
1475 return status;
1476}
1477
1478/*
1479 * Encode a CLOSE request
1480 */
1481static int nfs4_xdr_enc_close(struct rpc_rqst *req, uint32_t *p, struct nfs_closeargs *args)
1482{
1483 struct xdr_stream xdr;
1484 struct compound_hdr hdr = {
Trond Myklebust516a6af2005-10-27 22:12:41 -04001485 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 };
1487 int status;
1488
1489 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1490 encode_compound_hdr(&xdr, &hdr);
1491 status = encode_putfh(&xdr, args->fh);
1492 if(status)
1493 goto out;
1494 status = encode_close(&xdr, args);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001495 if (status != 0)
1496 goto out;
1497 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498out:
1499 return status;
1500}
1501
1502/*
1503 * Encode an OPEN request
1504 */
1505static int nfs4_xdr_enc_open(struct rpc_rqst *req, uint32_t *p, struct nfs_openargs *args)
1506{
1507 struct xdr_stream xdr;
1508 struct compound_hdr hdr = {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001509 .nops = 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 };
1511 int status;
1512
1513 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1514 encode_compound_hdr(&xdr, &hdr);
1515 status = encode_putfh(&xdr, args->fh);
1516 if (status)
1517 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001518 status = encode_savefh(&xdr);
1519 if (status)
1520 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 status = encode_open(&xdr, args);
1522 if (status)
1523 goto out;
1524 status = encode_getfh(&xdr);
1525 if (status)
1526 goto out;
1527 status = encode_getfattr(&xdr, args->bitmask);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001528 if (status)
1529 goto out;
1530 status = encode_restorefh(&xdr);
1531 if (status)
1532 goto out;
1533 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534out:
1535 return status;
1536}
1537
1538/*
1539 * Encode an OPEN_CONFIRM request
1540 */
1541static int nfs4_xdr_enc_open_confirm(struct rpc_rqst *req, uint32_t *p, struct nfs_open_confirmargs *args)
1542{
1543 struct xdr_stream xdr;
1544 struct compound_hdr hdr = {
1545 .nops = 2,
1546 };
1547 int status;
1548
1549 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1550 encode_compound_hdr(&xdr, &hdr);
1551 status = encode_putfh(&xdr, args->fh);
1552 if(status)
1553 goto out;
1554 status = encode_open_confirm(&xdr, args);
1555out:
1556 return status;
1557}
1558
1559/*
1560 * Encode an OPEN request with no attributes.
1561 */
1562static int nfs4_xdr_enc_open_noattr(struct rpc_rqst *req, uint32_t *p, struct nfs_openargs *args)
1563{
1564 struct xdr_stream xdr;
1565 struct compound_hdr hdr = {
Trond Myklebust864472e2006-01-03 09:55:15 +01001566 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 };
1568 int status;
1569
1570 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1571 encode_compound_hdr(&xdr, &hdr);
1572 status = encode_putfh(&xdr, args->fh);
1573 if (status)
1574 goto out;
1575 status = encode_open(&xdr, args);
Trond Myklebust864472e2006-01-03 09:55:15 +01001576 if (status)
1577 goto out;
1578 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579out:
1580 return status;
1581}
1582
1583/*
1584 * Encode an OPEN_DOWNGRADE request
1585 */
1586static int nfs4_xdr_enc_open_downgrade(struct rpc_rqst *req, uint32_t *p, struct nfs_closeargs *args)
1587{
1588 struct xdr_stream xdr;
1589 struct compound_hdr hdr = {
Trond Myklebust516a6af2005-10-27 22:12:41 -04001590 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 };
1592 int status;
1593
1594 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1595 encode_compound_hdr(&xdr, &hdr);
1596 status = encode_putfh(&xdr, args->fh);
1597 if (status)
1598 goto out;
1599 status = encode_open_downgrade(&xdr, args);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001600 if (status != 0)
1601 goto out;
1602 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603out:
1604 return status;
1605}
1606
1607/*
1608 * Encode a LOCK request
1609 */
Trond Myklebust911d1aa2006-01-03 09:55:16 +01001610static int nfs4_xdr_enc_lock(struct rpc_rqst *req, uint32_t *p, struct nfs_lock_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
1612 struct xdr_stream xdr;
1613 struct compound_hdr hdr = {
1614 .nops = 2,
1615 };
1616 int status;
1617
Trond Myklebust911d1aa2006-01-03 09:55:16 +01001618 status = nfs_wait_on_sequence(args->lock_seqid, req->rq_task);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001619 if (status != 0)
1620 goto out;
Trond Myklebust06735b32005-10-18 14:20:15 -07001621 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust911d1aa2006-01-03 09:55:16 +01001622 if (args->lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)
1623 args->new_lock_owner = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1625 encode_compound_hdr(&xdr, &hdr);
1626 status = encode_putfh(&xdr, args->fh);
1627 if(status)
1628 goto out;
1629 status = encode_lock(&xdr, args);
1630out:
1631 return status;
1632}
1633
1634/*
1635 * Encode a LOCKT request
1636 */
Trond Myklebust911d1aa2006-01-03 09:55:16 +01001637static int nfs4_xdr_enc_lockt(struct rpc_rqst *req, uint32_t *p, struct nfs_lockt_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
1639 struct xdr_stream xdr;
1640 struct compound_hdr hdr = {
1641 .nops = 2,
1642 };
1643 int status;
1644
1645 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1646 encode_compound_hdr(&xdr, &hdr);
1647 status = encode_putfh(&xdr, args->fh);
1648 if(status)
1649 goto out;
1650 status = encode_lockt(&xdr, args);
1651out:
1652 return status;
1653}
1654
1655/*
1656 * Encode a LOCKU request
1657 */
Trond Myklebust911d1aa2006-01-03 09:55:16 +01001658static int nfs4_xdr_enc_locku(struct rpc_rqst *req, uint32_t *p, struct nfs_locku_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
1660 struct xdr_stream xdr;
1661 struct compound_hdr hdr = {
1662 .nops = 2,
1663 };
1664 int status;
1665
1666 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1667 encode_compound_hdr(&xdr, &hdr);
1668 status = encode_putfh(&xdr, args->fh);
1669 if(status)
1670 goto out;
1671 status = encode_locku(&xdr, args);
1672out:
1673 return status;
1674}
1675
1676/*
1677 * Encode a READLINK request
1678 */
1679static int nfs4_xdr_enc_readlink(struct rpc_rqst *req, uint32_t *p, const struct nfs4_readlink *args)
1680{
1681 struct xdr_stream xdr;
1682 struct compound_hdr hdr = {
1683 .nops = 2,
1684 };
1685 int status;
1686
1687 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1688 encode_compound_hdr(&xdr, &hdr);
1689 status = encode_putfh(&xdr, args->fh);
1690 if(status)
1691 goto out;
1692 status = encode_readlink(&xdr, args, req);
1693out:
1694 return status;
1695}
1696
1697/*
1698 * Encode a READDIR request
1699 */
1700static int nfs4_xdr_enc_readdir(struct rpc_rqst *req, uint32_t *p, const struct nfs4_readdir_arg *args)
1701{
1702 struct xdr_stream xdr;
1703 struct compound_hdr hdr = {
1704 .nops = 2,
1705 };
1706 int status;
1707
1708 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1709 encode_compound_hdr(&xdr, &hdr);
1710 status = encode_putfh(&xdr, args->fh);
1711 if(status)
1712 goto out;
1713 status = encode_readdir(&xdr, args, req);
1714out:
1715 return status;
1716}
1717
1718/*
1719 * Encode a READ request
1720 */
1721static int nfs4_xdr_enc_read(struct rpc_rqst *req, uint32_t *p, struct nfs_readargs *args)
1722{
1723 struct rpc_auth *auth = req->rq_task->tk_auth;
1724 struct xdr_stream xdr;
1725 struct compound_hdr hdr = {
1726 .nops = 2,
1727 };
1728 int replen, status;
1729
1730 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1731 encode_compound_hdr(&xdr, &hdr);
1732 status = encode_putfh(&xdr, args->fh);
1733 if (status)
1734 goto out;
1735 status = encode_read(&xdr, args);
1736 if (status)
1737 goto out;
1738
1739 /* set up reply kvec
1740 * toplevel status + taglen=0 + rescount + OP_PUTFH + status
1741 * + OP_READ + status + eof + datalen = 9
1742 */
1743 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_read_sz) << 2;
1744 xdr_inline_pages(&req->rq_rcv_buf, replen,
1745 args->pages, args->pgbase, args->count);
1746out:
1747 return status;
1748}
1749
1750/*
1751 * Encode an SETATTR request
1752 */
1753static int nfs4_xdr_enc_setattr(struct rpc_rqst *req, uint32_t *p, struct nfs_setattrargs *args)
1754
1755{
1756 struct xdr_stream xdr;
1757 struct compound_hdr hdr = {
1758 .nops = 3,
1759 };
1760 int status;
1761
1762 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1763 encode_compound_hdr(&xdr, &hdr);
1764 status = encode_putfh(&xdr, args->fh);
1765 if(status)
1766 goto out;
1767 status = encode_setattr(&xdr, args, args->server);
1768 if(status)
1769 goto out;
1770 status = encode_getfattr(&xdr, args->bitmask);
1771out:
1772 return status;
1773}
1774
1775/*
J. Bruce Fields029d1052005-06-22 17:16:22 +00001776 * Encode a GETACL request
1777 */
1778static int
1779nfs4_xdr_enc_getacl(struct rpc_rqst *req, uint32_t *p,
1780 struct nfs_getaclargs *args)
1781{
1782 struct xdr_stream xdr;
1783 struct rpc_auth *auth = req->rq_task->tk_auth;
1784 struct compound_hdr hdr = {
1785 .nops = 2,
1786 };
1787 int replen, status;
1788
1789 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1790 encode_compound_hdr(&xdr, &hdr);
1791 status = encode_putfh(&xdr, args->fh);
1792 if (status)
1793 goto out;
1794 status = encode_getattr_two(&xdr, FATTR4_WORD0_ACL, 0);
1795 /* set up reply buffer: */
1796 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_getacl_sz) << 2;
1797 xdr_inline_pages(&req->rq_rcv_buf, replen,
1798 args->acl_pages, args->acl_pgbase, args->acl_len);
1799out:
1800 return status;
1801}
1802
1803/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 * Encode a WRITE request
1805 */
1806static int nfs4_xdr_enc_write(struct rpc_rqst *req, uint32_t *p, struct nfs_writeargs *args)
1807{
1808 struct xdr_stream xdr;
1809 struct compound_hdr hdr = {
Trond Myklebust4f9838c2005-10-27 22:12:44 -04001810 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 };
1812 int status;
1813
1814 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1815 encode_compound_hdr(&xdr, &hdr);
1816 status = encode_putfh(&xdr, args->fh);
1817 if (status)
1818 goto out;
1819 status = encode_write(&xdr, args);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04001820 if (status)
1821 goto out;
1822 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823out:
1824 return status;
1825}
1826
1827/*
1828 * a COMMIT request
1829 */
1830static int nfs4_xdr_enc_commit(struct rpc_rqst *req, uint32_t *p, struct nfs_writeargs *args)
1831{
1832 struct xdr_stream xdr;
1833 struct compound_hdr hdr = {
Trond Myklebust4f9838c2005-10-27 22:12:44 -04001834 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 };
1836 int status;
1837
1838 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1839 encode_compound_hdr(&xdr, &hdr);
1840 status = encode_putfh(&xdr, args->fh);
1841 if (status)
1842 goto out;
1843 status = encode_commit(&xdr, args);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04001844 if (status)
1845 goto out;
1846 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847out:
1848 return status;
1849}
1850
1851/*
1852 * FSINFO request
1853 */
1854static int nfs4_xdr_enc_fsinfo(struct rpc_rqst *req, uint32_t *p, struct nfs4_fsinfo_arg *args)
1855{
1856 struct xdr_stream xdr;
1857 struct compound_hdr hdr = {
1858 .nops = 2,
1859 };
1860 int status;
1861
1862 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1863 encode_compound_hdr(&xdr, &hdr);
1864 status = encode_putfh(&xdr, args->fh);
1865 if (!status)
1866 status = encode_fsinfo(&xdr, args->bitmask);
1867 return status;
1868}
1869
1870/*
1871 * a PATHCONF request
1872 */
1873static int nfs4_xdr_enc_pathconf(struct rpc_rqst *req, uint32_t *p, const struct nfs4_pathconf_arg *args)
1874{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 struct xdr_stream xdr;
1876 struct compound_hdr hdr = {
1877 .nops = 2,
1878 };
1879 int status;
1880
1881 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1882 encode_compound_hdr(&xdr, &hdr);
1883 status = encode_putfh(&xdr, args->fh);
1884 if (!status)
1885 status = encode_getattr_one(&xdr,
1886 args->bitmask[0] & nfs4_pathconf_bitmap[0]);
1887 return status;
1888}
1889
1890/*
1891 * a STATFS request
1892 */
1893static int nfs4_xdr_enc_statfs(struct rpc_rqst *req, uint32_t *p, const struct nfs4_statfs_arg *args)
1894{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 struct xdr_stream xdr;
1896 struct compound_hdr hdr = {
1897 .nops = 2,
1898 };
1899 int status;
1900
1901 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1902 encode_compound_hdr(&xdr, &hdr);
1903 status = encode_putfh(&xdr, args->fh);
1904 if (status == 0)
1905 status = encode_getattr_two(&xdr,
1906 args->bitmask[0] & nfs4_statfs_bitmap[0],
1907 args->bitmask[1] & nfs4_statfs_bitmap[1]);
1908 return status;
1909}
1910
1911/*
1912 * GETATTR_BITMAP request
1913 */
1914static int nfs4_xdr_enc_server_caps(struct rpc_rqst *req, uint32_t *p, const struct nfs_fh *fhandle)
1915{
1916 struct xdr_stream xdr;
1917 struct compound_hdr hdr = {
1918 .nops = 2,
1919 };
1920 int status;
1921
1922 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1923 encode_compound_hdr(&xdr, &hdr);
1924 status = encode_putfh(&xdr, fhandle);
1925 if (status == 0)
1926 status = encode_getattr_one(&xdr, FATTR4_WORD0_SUPPORTED_ATTRS|
1927 FATTR4_WORD0_LINK_SUPPORT|
1928 FATTR4_WORD0_SYMLINK_SUPPORT|
1929 FATTR4_WORD0_ACLSUPPORT);
1930 return status;
1931}
1932
1933/*
1934 * a RENEW request
1935 */
1936static int nfs4_xdr_enc_renew(struct rpc_rqst *req, uint32_t *p, struct nfs4_client *clp)
1937{
1938 struct xdr_stream xdr;
1939 struct compound_hdr hdr = {
1940 .nops = 1,
1941 };
1942
1943 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1944 encode_compound_hdr(&xdr, &hdr);
1945 return encode_renew(&xdr, clp);
1946}
1947
1948/*
1949 * a SETCLIENTID request
1950 */
1951static int nfs4_xdr_enc_setclientid(struct rpc_rqst *req, uint32_t *p, struct nfs4_setclientid *sc)
1952{
1953 struct xdr_stream xdr;
1954 struct compound_hdr hdr = {
1955 .nops = 1,
1956 };
1957
1958 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1959 encode_compound_hdr(&xdr, &hdr);
1960 return encode_setclientid(&xdr, sc);
1961}
1962
1963/*
1964 * a SETCLIENTID_CONFIRM request
1965 */
1966static int nfs4_xdr_enc_setclientid_confirm(struct rpc_rqst *req, uint32_t *p, struct nfs4_client *clp)
1967{
1968 struct xdr_stream xdr;
1969 struct compound_hdr hdr = {
1970 .nops = 3,
1971 };
1972 const u32 lease_bitmap[2] = { FATTR4_WORD0_LEASE_TIME, 0 };
1973 int status;
1974
1975 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1976 encode_compound_hdr(&xdr, &hdr);
1977 status = encode_setclientid_confirm(&xdr, clp);
1978 if (!status)
1979 status = encode_putrootfh(&xdr);
1980 if (!status)
1981 status = encode_fsinfo(&xdr, lease_bitmap);
1982 return status;
1983}
1984
1985/*
1986 * DELEGRETURN request
1987 */
1988static int nfs4_xdr_enc_delegreturn(struct rpc_rqst *req, uint32_t *p, const struct nfs4_delegreturnargs *args)
1989{
1990 struct xdr_stream xdr;
1991 struct compound_hdr hdr = {
1992 .nops = 2,
1993 };
1994 int status;
1995
1996 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1997 encode_compound_hdr(&xdr, &hdr);
1998 if ((status = encode_putfh(&xdr, args->fhandle)) == 0)
1999 status = encode_delegreturn(&xdr, args->stateid);
2000 return status;
2001}
2002
2003/*
2004 * START OF "GENERIC" DECODE ROUTINES.
2005 * These may look a little ugly since they are imported from a "generic"
2006 * set of XDR encode/decode routines which are intended to be shared by
2007 * all of our NFSv4 implementations (OpenBSD, MacOS X...).
2008 *
2009 * If the pain of reading these is too great, it should be a straightforward
2010 * task to translate them into Linux-specific versions which are more
2011 * consistent with the style used in NFSv2/v3...
2012 */
2013#define READ32(x) (x) = ntohl(*p++)
2014#define READ64(x) do { \
2015 (x) = (u64)ntohl(*p++) << 32; \
2016 (x) |= ntohl(*p++); \
2017} while (0)
2018#define READTIME(x) do { \
2019 p++; \
2020 (x.tv_sec) = ntohl(*p++); \
2021 (x.tv_nsec) = ntohl(*p++); \
2022} while (0)
2023#define COPYMEM(x,nbytes) do { \
2024 memcpy((x), p, nbytes); \
2025 p += XDR_QUADLEN(nbytes); \
2026} while (0)
2027
2028#define READ_BUF(nbytes) do { \
2029 p = xdr_inline_decode(xdr, nbytes); \
2030 if (!p) { \
2031 printk(KERN_WARNING "%s: reply buffer overflowed in line %d.", \
2032 __FUNCTION__, __LINE__); \
2033 return -EIO; \
2034 } \
2035} while (0)
2036
2037static int decode_opaque_inline(struct xdr_stream *xdr, uint32_t *len, char **string)
2038{
2039 uint32_t *p;
2040
2041 READ_BUF(4);
2042 READ32(*len);
2043 READ_BUF(*len);
2044 *string = (char *)p;
2045 return 0;
2046}
2047
2048static int decode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
2049{
2050 uint32_t *p;
2051
2052 READ_BUF(8);
2053 READ32(hdr->status);
2054 READ32(hdr->taglen);
2055
2056 READ_BUF(hdr->taglen + 4);
2057 hdr->tag = (char *)p;
2058 p += XDR_QUADLEN(hdr->taglen);
2059 READ32(hdr->nops);
2060 return 0;
2061}
2062
2063static int decode_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
2064{
2065 uint32_t *p;
2066 uint32_t opnum;
2067 int32_t nfserr;
2068
2069 READ_BUF(8);
2070 READ32(opnum);
2071 if (opnum != expected) {
2072 printk(KERN_NOTICE
2073 "nfs4_decode_op_hdr: Server returned operation"
2074 " %d but we issued a request for %d\n",
2075 opnum, expected);
2076 return -EIO;
2077 }
2078 READ32(nfserr);
2079 if (nfserr != NFS_OK)
2080 return -nfs_stat_to_errno(nfserr);
2081 return 0;
2082}
2083
2084/* Dummy routine */
2085static int decode_ace(struct xdr_stream *xdr, void *ace, struct nfs4_client *clp)
2086{
2087 uint32_t *p;
2088 uint32_t strlen;
2089 char *str;
2090
2091 READ_BUF(12);
2092 return decode_opaque_inline(xdr, &strlen, &str);
2093}
2094
2095static int decode_attr_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
2096{
2097 uint32_t bmlen, *p;
2098
2099 READ_BUF(4);
2100 READ32(bmlen);
2101
2102 bitmap[0] = bitmap[1] = 0;
2103 READ_BUF((bmlen << 2));
2104 if (bmlen > 0) {
2105 READ32(bitmap[0]);
2106 if (bmlen > 1)
2107 READ32(bitmap[1]);
2108 }
2109 return 0;
2110}
2111
2112static inline int decode_attr_length(struct xdr_stream *xdr, uint32_t *attrlen, uint32_t **savep)
2113{
2114 uint32_t *p;
2115
2116 READ_BUF(4);
2117 READ32(*attrlen);
2118 *savep = xdr->p;
2119 return 0;
2120}
2121
2122static int decode_attr_supported(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *bitmask)
2123{
2124 if (likely(bitmap[0] & FATTR4_WORD0_SUPPORTED_ATTRS)) {
2125 decode_attr_bitmap(xdr, bitmask);
2126 bitmap[0] &= ~FATTR4_WORD0_SUPPORTED_ATTRS;
2127 } else
2128 bitmask[0] = bitmask[1] = 0;
2129 dprintk("%s: bitmask=0x%x%x\n", __FUNCTION__, bitmask[0], bitmask[1]);
2130 return 0;
2131}
2132
2133static int decode_attr_type(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *type)
2134{
2135 uint32_t *p;
2136
2137 *type = 0;
2138 if (unlikely(bitmap[0] & (FATTR4_WORD0_TYPE - 1U)))
2139 return -EIO;
2140 if (likely(bitmap[0] & FATTR4_WORD0_TYPE)) {
2141 READ_BUF(4);
2142 READ32(*type);
2143 if (*type < NF4REG || *type > NF4NAMEDATTR) {
2144 dprintk("%s: bad type %d\n", __FUNCTION__, *type);
2145 return -EIO;
2146 }
2147 bitmap[0] &= ~FATTR4_WORD0_TYPE;
2148 }
2149 dprintk("%s: type=0%o\n", __FUNCTION__, nfs_type2fmt[*type].nfs2type);
2150 return 0;
2151}
2152
2153static int decode_attr_change(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *change)
2154{
2155 uint32_t *p;
2156
2157 *change = 0;
2158 if (unlikely(bitmap[0] & (FATTR4_WORD0_CHANGE - 1U)))
2159 return -EIO;
2160 if (likely(bitmap[0] & FATTR4_WORD0_CHANGE)) {
2161 READ_BUF(8);
2162 READ64(*change);
2163 bitmap[0] &= ~FATTR4_WORD0_CHANGE;
2164 }
2165 dprintk("%s: change attribute=%Lu\n", __FUNCTION__,
2166 (unsigned long long)*change);
2167 return 0;
2168}
2169
2170static int decode_attr_size(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *size)
2171{
2172 uint32_t *p;
2173
2174 *size = 0;
2175 if (unlikely(bitmap[0] & (FATTR4_WORD0_SIZE - 1U)))
2176 return -EIO;
2177 if (likely(bitmap[0] & FATTR4_WORD0_SIZE)) {
2178 READ_BUF(8);
2179 READ64(*size);
2180 bitmap[0] &= ~FATTR4_WORD0_SIZE;
2181 }
2182 dprintk("%s: file size=%Lu\n", __FUNCTION__, (unsigned long long)*size);
2183 return 0;
2184}
2185
2186static int decode_attr_link_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2187{
2188 uint32_t *p;
2189
2190 *res = 0;
2191 if (unlikely(bitmap[0] & (FATTR4_WORD0_LINK_SUPPORT - 1U)))
2192 return -EIO;
2193 if (likely(bitmap[0] & FATTR4_WORD0_LINK_SUPPORT)) {
2194 READ_BUF(4);
2195 READ32(*res);
2196 bitmap[0] &= ~FATTR4_WORD0_LINK_SUPPORT;
2197 }
2198 dprintk("%s: link support=%s\n", __FUNCTION__, *res == 0 ? "false" : "true");
2199 return 0;
2200}
2201
2202static int decode_attr_symlink_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2203{
2204 uint32_t *p;
2205
2206 *res = 0;
2207 if (unlikely(bitmap[0] & (FATTR4_WORD0_SYMLINK_SUPPORT - 1U)))
2208 return -EIO;
2209 if (likely(bitmap[0] & FATTR4_WORD0_SYMLINK_SUPPORT)) {
2210 READ_BUF(4);
2211 READ32(*res);
2212 bitmap[0] &= ~FATTR4_WORD0_SYMLINK_SUPPORT;
2213 }
2214 dprintk("%s: symlink support=%s\n", __FUNCTION__, *res == 0 ? "false" : "true");
2215 return 0;
2216}
2217
2218static int decode_attr_fsid(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_fsid *fsid)
2219{
2220 uint32_t *p;
2221
2222 fsid->major = 0;
2223 fsid->minor = 0;
2224 if (unlikely(bitmap[0] & (FATTR4_WORD0_FSID - 1U)))
2225 return -EIO;
2226 if (likely(bitmap[0] & FATTR4_WORD0_FSID)) {
2227 READ_BUF(16);
2228 READ64(fsid->major);
2229 READ64(fsid->minor);
2230 bitmap[0] &= ~FATTR4_WORD0_FSID;
2231 }
2232 dprintk("%s: fsid=(0x%Lx/0x%Lx)\n", __FUNCTION__,
2233 (unsigned long long)fsid->major,
2234 (unsigned long long)fsid->minor);
2235 return 0;
2236}
2237
2238static int decode_attr_lease_time(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2239{
2240 uint32_t *p;
2241
2242 *res = 60;
2243 if (unlikely(bitmap[0] & (FATTR4_WORD0_LEASE_TIME - 1U)))
2244 return -EIO;
2245 if (likely(bitmap[0] & FATTR4_WORD0_LEASE_TIME)) {
2246 READ_BUF(4);
2247 READ32(*res);
2248 bitmap[0] &= ~FATTR4_WORD0_LEASE_TIME;
2249 }
2250 dprintk("%s: file size=%u\n", __FUNCTION__, (unsigned int)*res);
2251 return 0;
2252}
2253
2254static int decode_attr_aclsupport(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2255{
2256 uint32_t *p;
2257
2258 *res = ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL;
2259 if (unlikely(bitmap[0] & (FATTR4_WORD0_ACLSUPPORT - 1U)))
2260 return -EIO;
2261 if (likely(bitmap[0] & FATTR4_WORD0_ACLSUPPORT)) {
2262 READ_BUF(4);
2263 READ32(*res);
2264 bitmap[0] &= ~FATTR4_WORD0_ACLSUPPORT;
2265 }
2266 dprintk("%s: ACLs supported=%u\n", __FUNCTION__, (unsigned int)*res);
2267 return 0;
2268}
2269
2270static int decode_attr_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
2271{
2272 uint32_t *p;
2273
2274 *fileid = 0;
2275 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILEID - 1U)))
2276 return -EIO;
2277 if (likely(bitmap[0] & FATTR4_WORD0_FILEID)) {
2278 READ_BUF(8);
2279 READ64(*fileid);
2280 bitmap[0] &= ~FATTR4_WORD0_FILEID;
2281 }
2282 dprintk("%s: fileid=%Lu\n", __FUNCTION__, (unsigned long long)*fileid);
2283 return 0;
2284}
2285
2286static int decode_attr_files_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2287{
2288 uint32_t *p;
2289 int status = 0;
2290
2291 *res = 0;
2292 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_AVAIL - 1U)))
2293 return -EIO;
2294 if (likely(bitmap[0] & FATTR4_WORD0_FILES_AVAIL)) {
2295 READ_BUF(8);
2296 READ64(*res);
2297 bitmap[0] &= ~FATTR4_WORD0_FILES_AVAIL;
2298 }
2299 dprintk("%s: files avail=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2300 return status;
2301}
2302
2303static int decode_attr_files_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2304{
2305 uint32_t *p;
2306 int status = 0;
2307
2308 *res = 0;
2309 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_FREE - 1U)))
2310 return -EIO;
2311 if (likely(bitmap[0] & FATTR4_WORD0_FILES_FREE)) {
2312 READ_BUF(8);
2313 READ64(*res);
2314 bitmap[0] &= ~FATTR4_WORD0_FILES_FREE;
2315 }
2316 dprintk("%s: files free=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2317 return status;
2318}
2319
2320static int decode_attr_files_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2321{
2322 uint32_t *p;
2323 int status = 0;
2324
2325 *res = 0;
2326 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_TOTAL - 1U)))
2327 return -EIO;
2328 if (likely(bitmap[0] & FATTR4_WORD0_FILES_TOTAL)) {
2329 READ_BUF(8);
2330 READ64(*res);
2331 bitmap[0] &= ~FATTR4_WORD0_FILES_TOTAL;
2332 }
2333 dprintk("%s: files total=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2334 return status;
2335}
2336
2337static int decode_attr_maxfilesize(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2338{
2339 uint32_t *p;
2340 int status = 0;
2341
2342 *res = 0;
2343 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXFILESIZE - 1U)))
2344 return -EIO;
2345 if (likely(bitmap[0] & FATTR4_WORD0_MAXFILESIZE)) {
2346 READ_BUF(8);
2347 READ64(*res);
2348 bitmap[0] &= ~FATTR4_WORD0_MAXFILESIZE;
2349 }
2350 dprintk("%s: maxfilesize=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2351 return status;
2352}
2353
2354static int decode_attr_maxlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxlink)
2355{
2356 uint32_t *p;
2357 int status = 0;
2358
2359 *maxlink = 1;
2360 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXLINK - 1U)))
2361 return -EIO;
2362 if (likely(bitmap[0] & FATTR4_WORD0_MAXLINK)) {
2363 READ_BUF(4);
2364 READ32(*maxlink);
2365 bitmap[0] &= ~FATTR4_WORD0_MAXLINK;
2366 }
2367 dprintk("%s: maxlink=%u\n", __FUNCTION__, *maxlink);
2368 return status;
2369}
2370
2371static int decode_attr_maxname(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxname)
2372{
2373 uint32_t *p;
2374 int status = 0;
2375
2376 *maxname = 1024;
2377 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXNAME - 1U)))
2378 return -EIO;
2379 if (likely(bitmap[0] & FATTR4_WORD0_MAXNAME)) {
2380 READ_BUF(4);
2381 READ32(*maxname);
2382 bitmap[0] &= ~FATTR4_WORD0_MAXNAME;
2383 }
2384 dprintk("%s: maxname=%u\n", __FUNCTION__, *maxname);
2385 return status;
2386}
2387
2388static int decode_attr_maxread(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2389{
2390 uint32_t *p;
2391 int status = 0;
2392
2393 *res = 1024;
2394 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXREAD - 1U)))
2395 return -EIO;
2396 if (likely(bitmap[0] & FATTR4_WORD0_MAXREAD)) {
2397 uint64_t maxread;
2398 READ_BUF(8);
2399 READ64(maxread);
2400 if (maxread > 0x7FFFFFFF)
2401 maxread = 0x7FFFFFFF;
2402 *res = (uint32_t)maxread;
2403 bitmap[0] &= ~FATTR4_WORD0_MAXREAD;
2404 }
2405 dprintk("%s: maxread=%lu\n", __FUNCTION__, (unsigned long)*res);
2406 return status;
2407}
2408
2409static int decode_attr_maxwrite(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2410{
2411 uint32_t *p;
2412 int status = 0;
2413
2414 *res = 1024;
2415 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXWRITE - 1U)))
2416 return -EIO;
2417 if (likely(bitmap[0] & FATTR4_WORD0_MAXWRITE)) {
2418 uint64_t maxwrite;
2419 READ_BUF(8);
2420 READ64(maxwrite);
2421 if (maxwrite > 0x7FFFFFFF)
2422 maxwrite = 0x7FFFFFFF;
2423 *res = (uint32_t)maxwrite;
2424 bitmap[0] &= ~FATTR4_WORD0_MAXWRITE;
2425 }
2426 dprintk("%s: maxwrite=%lu\n", __FUNCTION__, (unsigned long)*res);
2427 return status;
2428}
2429
2430static int decode_attr_mode(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *mode)
2431{
2432 uint32_t *p;
2433
2434 *mode = 0;
2435 if (unlikely(bitmap[1] & (FATTR4_WORD1_MODE - 1U)))
2436 return -EIO;
2437 if (likely(bitmap[1] & FATTR4_WORD1_MODE)) {
2438 READ_BUF(4);
2439 READ32(*mode);
2440 *mode &= ~S_IFMT;
2441 bitmap[1] &= ~FATTR4_WORD1_MODE;
2442 }
2443 dprintk("%s: file mode=0%o\n", __FUNCTION__, (unsigned int)*mode);
2444 return 0;
2445}
2446
2447static int decode_attr_nlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *nlink)
2448{
2449 uint32_t *p;
2450
2451 *nlink = 1;
2452 if (unlikely(bitmap[1] & (FATTR4_WORD1_NUMLINKS - 1U)))
2453 return -EIO;
2454 if (likely(bitmap[1] & FATTR4_WORD1_NUMLINKS)) {
2455 READ_BUF(4);
2456 READ32(*nlink);
2457 bitmap[1] &= ~FATTR4_WORD1_NUMLINKS;
2458 }
2459 dprintk("%s: nlink=%u\n", __FUNCTION__, (unsigned int)*nlink);
2460 return 0;
2461}
2462
2463static int decode_attr_owner(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_client *clp, int32_t *uid)
2464{
2465 uint32_t len, *p;
2466
2467 *uid = -2;
2468 if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER - 1U)))
2469 return -EIO;
2470 if (likely(bitmap[1] & FATTR4_WORD1_OWNER)) {
2471 READ_BUF(4);
2472 READ32(len);
2473 READ_BUF(len);
2474 if (len < XDR_MAX_NETOBJ) {
2475 if (nfs_map_name_to_uid(clp, (char *)p, len, uid) != 0)
2476 dprintk("%s: nfs_map_name_to_uid failed!\n",
2477 __FUNCTION__);
2478 } else
2479 printk(KERN_WARNING "%s: name too long (%u)!\n",
2480 __FUNCTION__, len);
2481 bitmap[1] &= ~FATTR4_WORD1_OWNER;
2482 }
2483 dprintk("%s: uid=%d\n", __FUNCTION__, (int)*uid);
2484 return 0;
2485}
2486
2487static int decode_attr_group(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_client *clp, int32_t *gid)
2488{
2489 uint32_t len, *p;
2490
2491 *gid = -2;
2492 if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER_GROUP - 1U)))
2493 return -EIO;
2494 if (likely(bitmap[1] & FATTR4_WORD1_OWNER_GROUP)) {
2495 READ_BUF(4);
2496 READ32(len);
2497 READ_BUF(len);
2498 if (len < XDR_MAX_NETOBJ) {
2499 if (nfs_map_group_to_gid(clp, (char *)p, len, gid) != 0)
2500 dprintk("%s: nfs_map_group_to_gid failed!\n",
2501 __FUNCTION__);
2502 } else
2503 printk(KERN_WARNING "%s: name too long (%u)!\n",
2504 __FUNCTION__, len);
2505 bitmap[1] &= ~FATTR4_WORD1_OWNER_GROUP;
2506 }
2507 dprintk("%s: gid=%d\n", __FUNCTION__, (int)*gid);
2508 return 0;
2509}
2510
2511static int decode_attr_rdev(struct xdr_stream *xdr, uint32_t *bitmap, dev_t *rdev)
2512{
2513 uint32_t major = 0, minor = 0, *p;
2514
2515 *rdev = MKDEV(0,0);
2516 if (unlikely(bitmap[1] & (FATTR4_WORD1_RAWDEV - 1U)))
2517 return -EIO;
2518 if (likely(bitmap[1] & FATTR4_WORD1_RAWDEV)) {
2519 dev_t tmp;
2520
2521 READ_BUF(8);
2522 READ32(major);
2523 READ32(minor);
2524 tmp = MKDEV(major, minor);
2525 if (MAJOR(tmp) == major && MINOR(tmp) == minor)
2526 *rdev = tmp;
2527 bitmap[1] &= ~ FATTR4_WORD1_RAWDEV;
2528 }
2529 dprintk("%s: rdev=(0x%x:0x%x)\n", __FUNCTION__, major, minor);
2530 return 0;
2531}
2532
2533static int decode_attr_space_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2534{
2535 uint32_t *p;
2536 int status = 0;
2537
2538 *res = 0;
2539 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_AVAIL - 1U)))
2540 return -EIO;
2541 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_AVAIL)) {
2542 READ_BUF(8);
2543 READ64(*res);
2544 bitmap[1] &= ~FATTR4_WORD1_SPACE_AVAIL;
2545 }
2546 dprintk("%s: space avail=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2547 return status;
2548}
2549
2550static int decode_attr_space_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2551{
2552 uint32_t *p;
2553 int status = 0;
2554
2555 *res = 0;
2556 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_FREE - 1U)))
2557 return -EIO;
2558 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_FREE)) {
2559 READ_BUF(8);
2560 READ64(*res);
2561 bitmap[1] &= ~FATTR4_WORD1_SPACE_FREE;
2562 }
2563 dprintk("%s: space free=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2564 return status;
2565}
2566
2567static int decode_attr_space_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2568{
2569 uint32_t *p;
2570 int status = 0;
2571
2572 *res = 0;
2573 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_TOTAL - 1U)))
2574 return -EIO;
2575 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_TOTAL)) {
2576 READ_BUF(8);
2577 READ64(*res);
2578 bitmap[1] &= ~FATTR4_WORD1_SPACE_TOTAL;
2579 }
2580 dprintk("%s: space total=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2581 return status;
2582}
2583
2584static int decode_attr_space_used(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *used)
2585{
2586 uint32_t *p;
2587
2588 *used = 0;
2589 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_USED - 1U)))
2590 return -EIO;
2591 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_USED)) {
2592 READ_BUF(8);
2593 READ64(*used);
2594 bitmap[1] &= ~FATTR4_WORD1_SPACE_USED;
2595 }
2596 dprintk("%s: space used=%Lu\n", __FUNCTION__,
2597 (unsigned long long)*used);
2598 return 0;
2599}
2600
2601static int decode_attr_time(struct xdr_stream *xdr, struct timespec *time)
2602{
2603 uint32_t *p;
2604 uint64_t sec;
2605 uint32_t nsec;
2606
2607 READ_BUF(12);
2608 READ64(sec);
2609 READ32(nsec);
2610 time->tv_sec = (time_t)sec;
2611 time->tv_nsec = (long)nsec;
2612 return 0;
2613}
2614
2615static int decode_attr_time_access(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2616{
2617 int status = 0;
2618
2619 time->tv_sec = 0;
2620 time->tv_nsec = 0;
2621 if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_ACCESS - 1U)))
2622 return -EIO;
2623 if (likely(bitmap[1] & FATTR4_WORD1_TIME_ACCESS)) {
2624 status = decode_attr_time(xdr, time);
2625 bitmap[1] &= ~FATTR4_WORD1_TIME_ACCESS;
2626 }
2627 dprintk("%s: atime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2628 return status;
2629}
2630
2631static int decode_attr_time_metadata(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2632{
2633 int status = 0;
2634
2635 time->tv_sec = 0;
2636 time->tv_nsec = 0;
2637 if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_METADATA - 1U)))
2638 return -EIO;
2639 if (likely(bitmap[1] & FATTR4_WORD1_TIME_METADATA)) {
2640 status = decode_attr_time(xdr, time);
2641 bitmap[1] &= ~FATTR4_WORD1_TIME_METADATA;
2642 }
2643 dprintk("%s: ctime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2644 return status;
2645}
2646
2647static int decode_attr_time_modify(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2648{
2649 int status = 0;
2650
2651 time->tv_sec = 0;
2652 time->tv_nsec = 0;
2653 if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_MODIFY - 1U)))
2654 return -EIO;
2655 if (likely(bitmap[1] & FATTR4_WORD1_TIME_MODIFY)) {
2656 status = decode_attr_time(xdr, time);
2657 bitmap[1] &= ~FATTR4_WORD1_TIME_MODIFY;
2658 }
2659 dprintk("%s: mtime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2660 return status;
2661}
2662
2663static int verify_attr_len(struct xdr_stream *xdr, uint32_t *savep, uint32_t attrlen)
2664{
2665 unsigned int attrwords = XDR_QUADLEN(attrlen);
2666 unsigned int nwords = xdr->p - savep;
2667
2668 if (unlikely(attrwords != nwords)) {
2669 printk(KERN_WARNING "%s: server returned incorrect attribute length: %u %c %u\n",
2670 __FUNCTION__,
2671 attrwords << 2,
2672 (attrwords < nwords) ? '<' : '>',
2673 nwords << 2);
2674 return -EIO;
2675 }
2676 return 0;
2677}
2678
2679static int decode_change_info(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
2680{
2681 uint32_t *p;
2682
2683 READ_BUF(20);
2684 READ32(cinfo->atomic);
2685 READ64(cinfo->before);
2686 READ64(cinfo->after);
2687 return 0;
2688}
2689
2690static int decode_access(struct xdr_stream *xdr, struct nfs4_accessres *access)
2691{
2692 uint32_t *p;
2693 uint32_t supp, acc;
2694 int status;
2695
2696 status = decode_op_hdr(xdr, OP_ACCESS);
2697 if (status)
2698 return status;
2699 READ_BUF(8);
2700 READ32(supp);
2701 READ32(acc);
2702 access->supported = supp;
2703 access->access = acc;
2704 return 0;
2705}
2706
2707static int decode_close(struct xdr_stream *xdr, struct nfs_closeres *res)
2708{
2709 uint32_t *p;
2710 int status;
2711
2712 status = decode_op_hdr(xdr, OP_CLOSE);
2713 if (status)
2714 return status;
2715 READ_BUF(sizeof(res->stateid.data));
2716 COPYMEM(res->stateid.data, sizeof(res->stateid.data));
2717 return 0;
2718}
2719
2720static int decode_commit(struct xdr_stream *xdr, struct nfs_writeres *res)
2721{
2722 uint32_t *p;
2723 int status;
2724
2725 status = decode_op_hdr(xdr, OP_COMMIT);
2726 if (status)
2727 return status;
2728 READ_BUF(8);
2729 COPYMEM(res->verf->verifier, 8);
2730 return 0;
2731}
2732
2733static int decode_create(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
2734{
2735 uint32_t *p;
2736 uint32_t bmlen;
2737 int status;
2738
2739 status = decode_op_hdr(xdr, OP_CREATE);
2740 if (status)
2741 return status;
2742 if ((status = decode_change_info(xdr, cinfo)))
2743 return status;
2744 READ_BUF(4);
2745 READ32(bmlen);
2746 READ_BUF(bmlen << 2);
2747 return 0;
2748}
2749
2750static int decode_server_caps(struct xdr_stream *xdr, struct nfs4_server_caps_res *res)
2751{
2752 uint32_t *savep;
2753 uint32_t attrlen,
2754 bitmap[2] = {0};
2755 int status;
2756
2757 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2758 goto xdr_error;
2759 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2760 goto xdr_error;
2761 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2762 goto xdr_error;
2763 if ((status = decode_attr_supported(xdr, bitmap, res->attr_bitmask)) != 0)
2764 goto xdr_error;
2765 if ((status = decode_attr_link_support(xdr, bitmap, &res->has_links)) != 0)
2766 goto xdr_error;
2767 if ((status = decode_attr_symlink_support(xdr, bitmap, &res->has_symlinks)) != 0)
2768 goto xdr_error;
2769 if ((status = decode_attr_aclsupport(xdr, bitmap, &res->acl_bitmask)) != 0)
2770 goto xdr_error;
2771 status = verify_attr_len(xdr, savep, attrlen);
2772xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04002773 dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 return status;
2775}
2776
2777static int decode_statfs(struct xdr_stream *xdr, struct nfs_fsstat *fsstat)
2778{
2779 uint32_t *savep;
2780 uint32_t attrlen,
2781 bitmap[2] = {0};
2782 int status;
2783
2784 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2785 goto xdr_error;
2786 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2787 goto xdr_error;
2788 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2789 goto xdr_error;
2790
2791 if ((status = decode_attr_files_avail(xdr, bitmap, &fsstat->afiles)) != 0)
2792 goto xdr_error;
2793 if ((status = decode_attr_files_free(xdr, bitmap, &fsstat->ffiles)) != 0)
2794 goto xdr_error;
2795 if ((status = decode_attr_files_total(xdr, bitmap, &fsstat->tfiles)) != 0)
2796 goto xdr_error;
2797 if ((status = decode_attr_space_avail(xdr, bitmap, &fsstat->abytes)) != 0)
2798 goto xdr_error;
2799 if ((status = decode_attr_space_free(xdr, bitmap, &fsstat->fbytes)) != 0)
2800 goto xdr_error;
2801 if ((status = decode_attr_space_total(xdr, bitmap, &fsstat->tbytes)) != 0)
2802 goto xdr_error;
2803
2804 status = verify_attr_len(xdr, savep, attrlen);
2805xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04002806 dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 return status;
2808}
2809
2810static int decode_pathconf(struct xdr_stream *xdr, struct nfs_pathconf *pathconf)
2811{
2812 uint32_t *savep;
2813 uint32_t attrlen,
2814 bitmap[2] = {0};
2815 int status;
2816
2817 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2818 goto xdr_error;
2819 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2820 goto xdr_error;
2821 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2822 goto xdr_error;
2823
2824 if ((status = decode_attr_maxlink(xdr, bitmap, &pathconf->max_link)) != 0)
2825 goto xdr_error;
2826 if ((status = decode_attr_maxname(xdr, bitmap, &pathconf->max_namelen)) != 0)
2827 goto xdr_error;
2828
2829 status = verify_attr_len(xdr, savep, attrlen);
2830xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04002831 dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 return status;
2833}
2834
2835static int decode_getfattr(struct xdr_stream *xdr, struct nfs_fattr *fattr, const struct nfs_server *server)
2836{
2837 uint32_t *savep;
2838 uint32_t attrlen,
2839 bitmap[2] = {0},
2840 type;
2841 int status, fmode = 0;
2842
2843 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2844 goto xdr_error;
2845 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2846 goto xdr_error;
2847
2848 fattr->bitmap[0] = bitmap[0];
2849 fattr->bitmap[1] = bitmap[1];
2850
2851 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2852 goto xdr_error;
2853
2854
2855 if ((status = decode_attr_type(xdr, bitmap, &type)) != 0)
2856 goto xdr_error;
2857 fattr->type = nfs_type2fmt[type].nfs2type;
2858 fmode = nfs_type2fmt[type].mode;
2859
2860 if ((status = decode_attr_change(xdr, bitmap, &fattr->change_attr)) != 0)
2861 goto xdr_error;
2862 if ((status = decode_attr_size(xdr, bitmap, &fattr->size)) != 0)
2863 goto xdr_error;
2864 if ((status = decode_attr_fsid(xdr, bitmap, &fattr->fsid_u.nfs4)) != 0)
2865 goto xdr_error;
2866 if ((status = decode_attr_fileid(xdr, bitmap, &fattr->fileid)) != 0)
2867 goto xdr_error;
2868 if ((status = decode_attr_mode(xdr, bitmap, &fattr->mode)) != 0)
2869 goto xdr_error;
2870 fattr->mode |= fmode;
2871 if ((status = decode_attr_nlink(xdr, bitmap, &fattr->nlink)) != 0)
2872 goto xdr_error;
2873 if ((status = decode_attr_owner(xdr, bitmap, server->nfs4_state, &fattr->uid)) != 0)
2874 goto xdr_error;
2875 if ((status = decode_attr_group(xdr, bitmap, server->nfs4_state, &fattr->gid)) != 0)
2876 goto xdr_error;
2877 if ((status = decode_attr_rdev(xdr, bitmap, &fattr->rdev)) != 0)
2878 goto xdr_error;
2879 if ((status = decode_attr_space_used(xdr, bitmap, &fattr->du.nfs3.used)) != 0)
2880 goto xdr_error;
2881 if ((status = decode_attr_time_access(xdr, bitmap, &fattr->atime)) != 0)
2882 goto xdr_error;
2883 if ((status = decode_attr_time_metadata(xdr, bitmap, &fattr->ctime)) != 0)
2884 goto xdr_error;
2885 if ((status = decode_attr_time_modify(xdr, bitmap, &fattr->mtime)) != 0)
2886 goto xdr_error;
Trond Myklebust33801142005-10-27 22:12:39 -04002887 if ((status = verify_attr_len(xdr, savep, attrlen)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 fattr->valid = NFS_ATTR_FATTR | NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04002890 dprintk("%s: xdr returned %d\n", __FUNCTION__, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 return status;
2892}
2893
2894
2895static int decode_fsinfo(struct xdr_stream *xdr, struct nfs_fsinfo *fsinfo)
2896{
2897 uint32_t *savep;
2898 uint32_t attrlen, bitmap[2];
2899 int status;
2900
2901 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2902 goto xdr_error;
2903 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2904 goto xdr_error;
2905 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2906 goto xdr_error;
2907
2908 fsinfo->rtmult = fsinfo->wtmult = 512; /* ??? */
2909
2910 if ((status = decode_attr_lease_time(xdr, bitmap, &fsinfo->lease_time)) != 0)
2911 goto xdr_error;
2912 if ((status = decode_attr_maxfilesize(xdr, bitmap, &fsinfo->maxfilesize)) != 0)
2913 goto xdr_error;
2914 if ((status = decode_attr_maxread(xdr, bitmap, &fsinfo->rtmax)) != 0)
2915 goto xdr_error;
2916 fsinfo->rtpref = fsinfo->dtpref = fsinfo->rtmax;
2917 if ((status = decode_attr_maxwrite(xdr, bitmap, &fsinfo->wtmax)) != 0)
2918 goto xdr_error;
2919 fsinfo->wtpref = fsinfo->wtmax;
2920
2921 status = verify_attr_len(xdr, savep, attrlen);
2922xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04002923 dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 return status;
2925}
2926
2927static int decode_getfh(struct xdr_stream *xdr, struct nfs_fh *fh)
2928{
2929 uint32_t *p;
2930 uint32_t len;
2931 int status;
2932
2933 status = decode_op_hdr(xdr, OP_GETFH);
2934 if (status)
2935 return status;
2936 /* Zero handle first to allow comparisons */
2937 memset(fh, 0, sizeof(*fh));
2938
2939 READ_BUF(4);
2940 READ32(len);
2941 if (len > NFS4_FHSIZE)
2942 return -EIO;
2943 fh->size = len;
2944 READ_BUF(len);
2945 COPYMEM(fh->data, len);
2946 return 0;
2947}
2948
2949static int decode_link(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
2950{
2951 int status;
2952
2953 status = decode_op_hdr(xdr, OP_LINK);
2954 if (status)
2955 return status;
2956 return decode_change_info(xdr, cinfo);
2957}
2958
2959/*
2960 * We create the owner, so we know a proper owner.id length is 4.
2961 */
Trond Myklebust911d1aa2006-01-03 09:55:16 +01002962static int decode_lock_denied (struct xdr_stream *xdr, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963{
Trond Myklebust911d1aa2006-01-03 09:55:16 +01002964 uint64_t offset, length, clientid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 uint32_t *p;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01002966 uint32_t namelen, type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
2968 READ_BUF(32);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01002969 READ64(offset);
2970 READ64(length);
2971 READ32(type);
2972 if (fl != NULL) {
2973 fl->fl_start = (loff_t)offset;
2974 fl->fl_end = fl->fl_start + (loff_t)length - 1;
2975 if (length == ~(uint64_t)0)
2976 fl->fl_end = OFFSET_MAX;
2977 fl->fl_type = F_WRLCK;
2978 if (type & 1)
2979 fl->fl_type = F_RDLCK;
2980 fl->fl_pid = 0;
2981 }
2982 READ64(clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 READ32(namelen);
2984 READ_BUF(namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 return -NFS4ERR_DENIED;
2986}
2987
Trond Myklebust911d1aa2006-01-03 09:55:16 +01002988static int decode_lock(struct xdr_stream *xdr, struct nfs_lock_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989{
2990 uint32_t *p;
2991 int status;
2992
2993 status = decode_op_hdr(xdr, OP_LOCK);
2994 if (status == 0) {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01002995 READ_BUF(sizeof(res->stateid.data));
2996 COPYMEM(res->stateid.data, sizeof(res->stateid.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 } else if (status == -NFS4ERR_DENIED)
Trond Myklebust911d1aa2006-01-03 09:55:16 +01002998 return decode_lock_denied(xdr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 return status;
3000}
3001
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003002static int decode_lockt(struct xdr_stream *xdr, struct nfs_lockt_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003{
3004 int status;
3005 status = decode_op_hdr(xdr, OP_LOCKT);
3006 if (status == -NFS4ERR_DENIED)
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003007 return decode_lock_denied(xdr, res->denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 return status;
3009}
3010
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003011static int decode_locku(struct xdr_stream *xdr, struct nfs_locku_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012{
3013 uint32_t *p;
3014 int status;
3015
3016 status = decode_op_hdr(xdr, OP_LOCKU);
3017 if (status == 0) {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003018 READ_BUF(sizeof(res->stateid.data));
3019 COPYMEM(res->stateid.data, sizeof(res->stateid.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 }
3021 return status;
3022}
3023
3024static int decode_lookup(struct xdr_stream *xdr)
3025{
3026 return decode_op_hdr(xdr, OP_LOOKUP);
3027}
3028
3029/* This is too sick! */
3030static int decode_space_limit(struct xdr_stream *xdr, u64 *maxsize)
3031{
3032 uint32_t *p;
3033 uint32_t limit_type, nblocks, blocksize;
3034
3035 READ_BUF(12);
3036 READ32(limit_type);
3037 switch (limit_type) {
3038 case 1:
3039 READ64(*maxsize);
3040 break;
3041 case 2:
3042 READ32(nblocks);
3043 READ32(blocksize);
3044 *maxsize = (uint64_t)nblocks * (uint64_t)blocksize;
3045 }
3046 return 0;
3047}
3048
3049static int decode_delegation(struct xdr_stream *xdr, struct nfs_openres *res)
3050{
3051 uint32_t *p;
3052 uint32_t delegation_type;
3053
3054 READ_BUF(4);
3055 READ32(delegation_type);
3056 if (delegation_type == NFS4_OPEN_DELEGATE_NONE) {
3057 res->delegation_type = 0;
3058 return 0;
3059 }
3060 READ_BUF(20);
3061 COPYMEM(res->delegation.data, sizeof(res->delegation.data));
3062 READ32(res->do_recall);
3063 switch (delegation_type) {
3064 case NFS4_OPEN_DELEGATE_READ:
3065 res->delegation_type = FMODE_READ;
3066 break;
3067 case NFS4_OPEN_DELEGATE_WRITE:
3068 res->delegation_type = FMODE_WRITE|FMODE_READ;
3069 if (decode_space_limit(xdr, &res->maxsize) < 0)
3070 return -EIO;
3071 }
3072 return decode_ace(xdr, NULL, res->server->nfs4_state);
3073}
3074
3075static int decode_open(struct xdr_stream *xdr, struct nfs_openres *res)
3076{
3077 uint32_t *p;
3078 uint32_t bmlen;
3079 int status;
3080
3081 status = decode_op_hdr(xdr, OP_OPEN);
3082 if (status)
3083 return status;
3084 READ_BUF(sizeof(res->stateid.data));
3085 COPYMEM(res->stateid.data, sizeof(res->stateid.data));
3086
3087 decode_change_info(xdr, &res->cinfo);
3088
3089 READ_BUF(8);
3090 READ32(res->rflags);
3091 READ32(bmlen);
3092 if (bmlen > 10)
3093 goto xdr_error;
3094
3095 READ_BUF(bmlen << 2);
3096 p += bmlen;
3097 return decode_delegation(xdr, res);
3098xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04003099 dprintk("%s: Bitmap too large! Length = %u\n", __FUNCTION__, bmlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 return -EIO;
3101}
3102
3103static int decode_open_confirm(struct xdr_stream *xdr, struct nfs_open_confirmres *res)
3104{
3105 uint32_t *p;
3106 int status;
3107
3108 status = decode_op_hdr(xdr, OP_OPEN_CONFIRM);
3109 if (status)
3110 return status;
3111 READ_BUF(sizeof(res->stateid.data));
3112 COPYMEM(res->stateid.data, sizeof(res->stateid.data));
3113 return 0;
3114}
3115
3116static int decode_open_downgrade(struct xdr_stream *xdr, struct nfs_closeres *res)
3117{
3118 uint32_t *p;
3119 int status;
3120
3121 status = decode_op_hdr(xdr, OP_OPEN_DOWNGRADE);
3122 if (status)
3123 return status;
3124 READ_BUF(sizeof(res->stateid.data));
3125 COPYMEM(res->stateid.data, sizeof(res->stateid.data));
3126 return 0;
3127}
3128
3129static int decode_putfh(struct xdr_stream *xdr)
3130{
3131 return decode_op_hdr(xdr, OP_PUTFH);
3132}
3133
3134static int decode_putrootfh(struct xdr_stream *xdr)
3135{
3136 return decode_op_hdr(xdr, OP_PUTROOTFH);
3137}
3138
3139static int decode_read(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs_readres *res)
3140{
3141 struct kvec *iov = req->rq_rcv_buf.head;
3142 uint32_t *p;
3143 uint32_t count, eof, recvd, hdrlen;
3144 int status;
3145
3146 status = decode_op_hdr(xdr, OP_READ);
3147 if (status)
3148 return status;
3149 READ_BUF(8);
3150 READ32(eof);
3151 READ32(count);
3152 hdrlen = (u8 *) p - (u8 *) iov->iov_base;
3153 recvd = req->rq_rcv_buf.len - hdrlen;
3154 if (count > recvd) {
3155 printk(KERN_WARNING "NFS: server cheating in read reply: "
3156 "count %u > recvd %u\n", count, recvd);
3157 count = recvd;
3158 eof = 0;
3159 }
3160 xdr_read_pages(xdr, count);
3161 res->eof = eof;
3162 res->count = count;
3163 return 0;
3164}
3165
3166static int decode_readdir(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs4_readdir_res *readdir)
3167{
3168 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
3169 struct page *page = *rcvbuf->pages;
3170 struct kvec *iov = rcvbuf->head;
3171 unsigned int nr, pglen = rcvbuf->page_len;
3172 uint32_t *end, *entry, *p, *kaddr;
3173 uint32_t len, attrlen;
3174 int hdrlen, recvd, status;
3175
3176 status = decode_op_hdr(xdr, OP_READDIR);
3177 if (status)
3178 return status;
3179 READ_BUF(8);
3180 COPYMEM(readdir->verifier.data, 8);
Trond Myklebusteadf4592005-06-22 17:16:39 +00003181 dprintk("%s: verifier = 0x%x%x\n",
3182 __FUNCTION__,
3183 ((u32 *)readdir->verifier.data)[0],
3184 ((u32 *)readdir->verifier.data)[1]);
3185
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
3187 hdrlen = (char *) p - (char *) iov->iov_base;
3188 recvd = rcvbuf->len - hdrlen;
3189 if (pglen > recvd)
3190 pglen = recvd;
3191 xdr_read_pages(xdr, pglen);
3192
3193 BUG_ON(pglen + readdir->pgbase > PAGE_CACHE_SIZE);
3194 kaddr = p = (uint32_t *) kmap_atomic(page, KM_USER0);
3195 end = (uint32_t *) ((char *)p + pglen + readdir->pgbase);
3196 entry = p;
3197 for (nr = 0; *p++; nr++) {
3198 if (p + 3 > end)
3199 goto short_pkt;
Trond Myklebusteadf4592005-06-22 17:16:39 +00003200 dprintk("cookie = %Lu, ", *((unsigned long long *)p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 p += 2; /* cookie */
3202 len = ntohl(*p++); /* filename length */
3203 if (len > NFS4_MAXNAMLEN) {
3204 printk(KERN_WARNING "NFS: giant filename in readdir (len 0x%x)\n", len);
3205 goto err_unmap;
3206 }
Trond Myklebusteadf4592005-06-22 17:16:39 +00003207 dprintk("filename = %*s\n", len, (char *)p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 p += XDR_QUADLEN(len);
3209 if (p + 1 > end)
3210 goto short_pkt;
3211 len = ntohl(*p++); /* bitmap length */
3212 p += len;
3213 if (p + 1 > end)
3214 goto short_pkt;
3215 attrlen = XDR_QUADLEN(ntohl(*p++));
3216 p += attrlen; /* attributes */
3217 if (p + 2 > end)
3218 goto short_pkt;
3219 entry = p;
3220 }
3221 if (!nr && (entry[0] != 0 || entry[1] == 0))
3222 goto short_pkt;
3223out:
3224 kunmap_atomic(kaddr, KM_USER0);
3225 return 0;
3226short_pkt:
Trond Myklebusteadf4592005-06-22 17:16:39 +00003227 dprintk("%s: short packet at entry %d\n", __FUNCTION__, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 entry[0] = entry[1] = 0;
3229 /* truncate listing ? */
3230 if (!nr) {
3231 printk(KERN_NOTICE "NFS: readdir reply truncated!\n");
3232 entry[1] = 1;
3233 }
3234 goto out;
3235err_unmap:
3236 kunmap_atomic(kaddr, KM_USER0);
3237 return -errno_NFSERR_IO;
3238}
3239
3240static int decode_readlink(struct xdr_stream *xdr, struct rpc_rqst *req)
3241{
3242 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
3243 struct kvec *iov = rcvbuf->head;
3244 int hdrlen, len, recvd;
3245 uint32_t *p;
3246 char *kaddr;
3247 int status;
3248
3249 status = decode_op_hdr(xdr, OP_READLINK);
3250 if (status)
3251 return status;
3252
3253 /* Convert length of symlink */
3254 READ_BUF(4);
3255 READ32(len);
3256 if (len >= rcvbuf->page_len || len <= 0) {
3257 dprintk(KERN_WARNING "nfs: server returned giant symlink!\n");
3258 return -ENAMETOOLONG;
3259 }
3260 hdrlen = (char *) xdr->p - (char *) iov->iov_base;
3261 recvd = req->rq_rcv_buf.len - hdrlen;
3262 if (recvd < len) {
3263 printk(KERN_WARNING "NFS: server cheating in readlink reply: "
3264 "count %u > recvd %u\n", len, recvd);
3265 return -EIO;
3266 }
3267 xdr_read_pages(xdr, len);
3268 /*
3269 * The XDR encode routine has set things up so that
3270 * the link text will be copied directly into the
3271 * buffer. We just have to do overflow-checking,
3272 * and and null-terminate the text (the VFS expects
3273 * null-termination).
3274 */
3275 kaddr = (char *)kmap_atomic(rcvbuf->pages[0], KM_USER0);
3276 kaddr[len+rcvbuf->page_base] = '\0';
3277 kunmap_atomic(kaddr, KM_USER0);
3278 return 0;
3279}
3280
3281static int decode_remove(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
3282{
3283 int status;
3284
3285 status = decode_op_hdr(xdr, OP_REMOVE);
3286 if (status)
3287 goto out;
3288 status = decode_change_info(xdr, cinfo);
3289out:
3290 return status;
3291}
3292
3293static int decode_rename(struct xdr_stream *xdr, struct nfs4_change_info *old_cinfo,
3294 struct nfs4_change_info *new_cinfo)
3295{
3296 int status;
3297
3298 status = decode_op_hdr(xdr, OP_RENAME);
3299 if (status)
3300 goto out;
3301 if ((status = decode_change_info(xdr, old_cinfo)))
3302 goto out;
3303 status = decode_change_info(xdr, new_cinfo);
3304out:
3305 return status;
3306}
3307
3308static int decode_renew(struct xdr_stream *xdr)
3309{
3310 return decode_op_hdr(xdr, OP_RENEW);
3311}
3312
Trond Myklebust56ae19f2005-10-27 22:12:40 -04003313static int
3314decode_restorefh(struct xdr_stream *xdr)
3315{
3316 return decode_op_hdr(xdr, OP_RESTOREFH);
3317}
3318
J. Bruce Fields029d1052005-06-22 17:16:22 +00003319static int decode_getacl(struct xdr_stream *xdr, struct rpc_rqst *req,
3320 size_t *acl_len)
3321{
3322 uint32_t *savep;
3323 uint32_t attrlen,
3324 bitmap[2] = {0};
3325 struct kvec *iov = req->rq_rcv_buf.head;
3326 int status;
3327
3328 *acl_len = 0;
3329 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3330 goto out;
3331 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3332 goto out;
3333 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3334 goto out;
3335
3336 if (unlikely(bitmap[0] & (FATTR4_WORD0_ACL - 1U)))
3337 return -EIO;
3338 if (likely(bitmap[0] & FATTR4_WORD0_ACL)) {
3339 int hdrlen, recvd;
3340
3341 /* We ignore &savep and don't do consistency checks on
3342 * the attr length. Let userspace figure it out.... */
3343 hdrlen = (u8 *)xdr->p - (u8 *)iov->iov_base;
3344 recvd = req->rq_rcv_buf.len - hdrlen;
3345 if (attrlen > recvd) {
3346 printk(KERN_WARNING "NFS: server cheating in getattr"
3347 " acl reply: attrlen %u > recvd %u\n",
3348 attrlen, recvd);
3349 return -EINVAL;
3350 }
3351 if (attrlen <= *acl_len)
3352 xdr_read_pages(xdr, attrlen);
3353 *acl_len = attrlen;
J. Bruce Fields8c233cf2005-10-13 16:54:27 -04003354 } else
3355 status = -EOPNOTSUPP;
J. Bruce Fields029d1052005-06-22 17:16:22 +00003356
3357out:
3358 return status;
3359}
3360
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361static int
3362decode_savefh(struct xdr_stream *xdr)
3363{
3364 return decode_op_hdr(xdr, OP_SAVEFH);
3365}
3366
3367static int decode_setattr(struct xdr_stream *xdr, struct nfs_setattrres *res)
3368{
3369 uint32_t *p;
3370 uint32_t bmlen;
3371 int status;
3372
3373
3374 status = decode_op_hdr(xdr, OP_SETATTR);
3375 if (status)
3376 return status;
3377 READ_BUF(4);
3378 READ32(bmlen);
3379 READ_BUF(bmlen << 2);
3380 return 0;
3381}
3382
3383static int decode_setclientid(struct xdr_stream *xdr, struct nfs4_client *clp)
3384{
3385 uint32_t *p;
3386 uint32_t opnum;
3387 int32_t nfserr;
3388
3389 READ_BUF(8);
3390 READ32(opnum);
3391 if (opnum != OP_SETCLIENTID) {
3392 printk(KERN_NOTICE
3393 "nfs4_decode_setclientid: Server returned operation"
3394 " %d\n", opnum);
3395 return -EIO;
3396 }
3397 READ32(nfserr);
3398 if (nfserr == NFS_OK) {
3399 READ_BUF(8 + sizeof(clp->cl_confirm.data));
3400 READ64(clp->cl_clientid);
3401 COPYMEM(clp->cl_confirm.data, sizeof(clp->cl_confirm.data));
3402 } else if (nfserr == NFSERR_CLID_INUSE) {
3403 uint32_t len;
3404
3405 /* skip netid string */
3406 READ_BUF(4);
3407 READ32(len);
3408 READ_BUF(len);
3409
3410 /* skip uaddr string */
3411 READ_BUF(4);
3412 READ32(len);
3413 READ_BUF(len);
3414 return -NFSERR_CLID_INUSE;
3415 } else
3416 return -nfs_stat_to_errno(nfserr);
3417
3418 return 0;
3419}
3420
3421static int decode_setclientid_confirm(struct xdr_stream *xdr)
3422{
3423 return decode_op_hdr(xdr, OP_SETCLIENTID_CONFIRM);
3424}
3425
3426static int decode_write(struct xdr_stream *xdr, struct nfs_writeres *res)
3427{
3428 uint32_t *p;
3429 int status;
3430
3431 status = decode_op_hdr(xdr, OP_WRITE);
3432 if (status)
3433 return status;
3434
3435 READ_BUF(16);
3436 READ32(res->count);
3437 READ32(res->verf->committed);
3438 COPYMEM(res->verf->verifier, 8);
3439 return 0;
3440}
3441
3442static int decode_delegreturn(struct xdr_stream *xdr)
3443{
3444 return decode_op_hdr(xdr, OP_DELEGRETURN);
3445}
3446
3447/*
3448 * Decode OPEN_DOWNGRADE response
3449 */
3450static int nfs4_xdr_dec_open_downgrade(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_closeres *res)
3451{
3452 struct xdr_stream xdr;
3453 struct compound_hdr hdr;
3454 int status;
3455
3456 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3457 status = decode_compound_hdr(&xdr, &hdr);
3458 if (status)
3459 goto out;
3460 status = decode_putfh(&xdr);
3461 if (status)
3462 goto out;
3463 status = decode_open_downgrade(&xdr, res);
Trond Myklebust516a6af2005-10-27 22:12:41 -04003464 if (status != 0)
3465 goto out;
3466 decode_getfattr(&xdr, res->fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467out:
3468 return status;
3469}
3470
3471/*
3472 * END OF "GENERIC" DECODE ROUTINES.
3473 */
3474
3475/*
3476 * Decode ACCESS response
3477 */
3478static int nfs4_xdr_dec_access(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_accessres *res)
3479{
3480 struct xdr_stream xdr;
3481 struct compound_hdr hdr;
3482 int status;
3483
3484 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3485 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3486 goto out;
3487 if ((status = decode_putfh(&xdr)) == 0)
3488 status = decode_access(&xdr, res);
3489out:
3490 return status;
3491}
3492
3493/*
3494 * Decode LOOKUP response
3495 */
3496static int nfs4_xdr_dec_lookup(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_lookup_res *res)
3497{
3498 struct xdr_stream xdr;
3499 struct compound_hdr hdr;
3500 int status;
3501
3502 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3503 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3504 goto out;
3505 if ((status = decode_putfh(&xdr)) != 0)
3506 goto out;
3507 if ((status = decode_lookup(&xdr)) != 0)
3508 goto out;
3509 if ((status = decode_getfh(&xdr, res->fh)) != 0)
3510 goto out;
3511 status = decode_getfattr(&xdr, res->fattr, res->server);
3512out:
3513 return status;
3514}
3515
3516/*
3517 * Decode LOOKUP_ROOT response
3518 */
3519static int nfs4_xdr_dec_lookup_root(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_lookup_res *res)
3520{
3521 struct xdr_stream xdr;
3522 struct compound_hdr hdr;
3523 int status;
3524
3525 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3526 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3527 goto out;
3528 if ((status = decode_putrootfh(&xdr)) != 0)
3529 goto out;
3530 if ((status = decode_getfh(&xdr, res->fh)) == 0)
3531 status = decode_getfattr(&xdr, res->fattr, res->server);
3532out:
3533 return status;
3534}
3535
3536/*
3537 * Decode REMOVE response
3538 */
Trond Myklebust16e42952005-10-27 22:12:44 -04003539static int nfs4_xdr_dec_remove(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_remove_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540{
3541 struct xdr_stream xdr;
3542 struct compound_hdr hdr;
3543 int status;
3544
3545 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3546 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3547 goto out;
Trond Myklebust16e42952005-10-27 22:12:44 -04003548 if ((status = decode_putfh(&xdr)) != 0)
3549 goto out;
3550 if ((status = decode_remove(&xdr, &res->cinfo)) != 0)
3551 goto out;
3552 decode_getfattr(&xdr, res->dir_attr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553out:
3554 return status;
3555}
3556
3557/*
3558 * Decode RENAME response
3559 */
3560static int nfs4_xdr_dec_rename(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_rename_res *res)
3561{
3562 struct xdr_stream xdr;
3563 struct compound_hdr hdr;
3564 int status;
3565
3566 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3567 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3568 goto out;
3569 if ((status = decode_putfh(&xdr)) != 0)
3570 goto out;
3571 if ((status = decode_savefh(&xdr)) != 0)
3572 goto out;
3573 if ((status = decode_putfh(&xdr)) != 0)
3574 goto out;
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003575 if ((status = decode_rename(&xdr, &res->old_cinfo, &res->new_cinfo)) != 0)
3576 goto out;
3577 /* Current FH is target directory */
3578 if (decode_getfattr(&xdr, res->new_fattr, res->server) != 0)
3579 goto out;
3580 if ((status = decode_restorefh(&xdr)) != 0)
3581 goto out;
3582 decode_getfattr(&xdr, res->old_fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583out:
3584 return status;
3585}
3586
3587/*
3588 * Decode LINK response
3589 */
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003590static int nfs4_xdr_dec_link(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_link_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591{
3592 struct xdr_stream xdr;
3593 struct compound_hdr hdr;
3594 int status;
3595
3596 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3597 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3598 goto out;
3599 if ((status = decode_putfh(&xdr)) != 0)
3600 goto out;
3601 if ((status = decode_savefh(&xdr)) != 0)
3602 goto out;
3603 if ((status = decode_putfh(&xdr)) != 0)
3604 goto out;
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003605 if ((status = decode_link(&xdr, &res->cinfo)) != 0)
3606 goto out;
3607 /*
3608 * Note order: OP_LINK leaves the directory as the current
3609 * filehandle.
3610 */
3611 if (decode_getfattr(&xdr, res->dir_attr, res->server) != 0)
3612 goto out;
3613 if ((status = decode_restorefh(&xdr)) != 0)
3614 goto out;
3615 decode_getfattr(&xdr, res->fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616out:
3617 return status;
3618}
3619
3620/*
3621 * Decode CREATE response
3622 */
3623static int nfs4_xdr_dec_create(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_create_res *res)
3624{
3625 struct xdr_stream xdr;
3626 struct compound_hdr hdr;
3627 int status;
3628
3629 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3630 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3631 goto out;
3632 if ((status = decode_putfh(&xdr)) != 0)
3633 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04003634 if ((status = decode_savefh(&xdr)) != 0)
3635 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 if ((status = decode_create(&xdr,&res->dir_cinfo)) != 0)
3637 goto out;
3638 if ((status = decode_getfh(&xdr, res->fh)) != 0)
3639 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04003640 if (decode_getfattr(&xdr, res->fattr, res->server) != 0)
3641 goto out;
3642 if ((status = decode_restorefh(&xdr)) != 0)
3643 goto out;
3644 decode_getfattr(&xdr, res->dir_fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645out:
3646 return status;
3647}
3648
3649/*
3650 * Decode SYMLINK response
3651 */
3652static int nfs4_xdr_dec_symlink(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_create_res *res)
3653{
3654 return nfs4_xdr_dec_create(rqstp, p, res);
3655}
3656
3657/*
3658 * Decode GETATTR response
3659 */
3660static int nfs4_xdr_dec_getattr(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_getattr_res *res)
3661{
3662 struct xdr_stream xdr;
3663 struct compound_hdr hdr;
3664 int status;
3665
3666 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3667 status = decode_compound_hdr(&xdr, &hdr);
3668 if (status)
3669 goto out;
3670 status = decode_putfh(&xdr);
3671 if (status)
3672 goto out;
3673 status = decode_getfattr(&xdr, res->fattr, res->server);
3674out:
3675 return status;
3676
3677}
3678
J. Bruce Fields23ec6962005-06-22 17:16:22 +00003679/*
3680 * Encode an SETACL request
3681 */
3682static int
3683nfs4_xdr_enc_setacl(struct rpc_rqst *req, uint32_t *p, struct nfs_setaclargs *args)
3684{
3685 struct xdr_stream xdr;
3686 struct compound_hdr hdr = {
3687 .nops = 2,
3688 };
3689 int status;
3690
3691 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
3692 encode_compound_hdr(&xdr, &hdr);
3693 status = encode_putfh(&xdr, args->fh);
3694 if (status)
3695 goto out;
3696 status = encode_setacl(&xdr, args);
3697out:
3698 return status;
3699}
3700/*
3701 * Decode SETACL response
3702 */
3703static int
3704nfs4_xdr_dec_setacl(struct rpc_rqst *rqstp, uint32_t *p, void *res)
3705{
3706 struct xdr_stream xdr;
3707 struct compound_hdr hdr;
3708 int status;
3709
3710 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3711 status = decode_compound_hdr(&xdr, &hdr);
3712 if (status)
3713 goto out;
3714 status = decode_putfh(&xdr);
3715 if (status)
3716 goto out;
3717 status = decode_setattr(&xdr, res);
3718out:
3719 return status;
3720}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721
3722/*
J. Bruce Fields029d1052005-06-22 17:16:22 +00003723 * Decode GETACL response
3724 */
3725static int
3726nfs4_xdr_dec_getacl(struct rpc_rqst *rqstp, uint32_t *p, size_t *acl_len)
3727{
3728 struct xdr_stream xdr;
3729 struct compound_hdr hdr;
3730 int status;
3731
3732 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3733 status = decode_compound_hdr(&xdr, &hdr);
3734 if (status)
3735 goto out;
3736 status = decode_putfh(&xdr);
3737 if (status)
3738 goto out;
3739 status = decode_getacl(&xdr, rqstp, acl_len);
3740
3741out:
3742 return status;
3743}
3744
3745/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 * Decode CLOSE response
3747 */
3748static int nfs4_xdr_dec_close(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_closeres *res)
3749{
3750 struct xdr_stream xdr;
3751 struct compound_hdr hdr;
3752 int status;
3753
3754 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3755 status = decode_compound_hdr(&xdr, &hdr);
3756 if (status)
3757 goto out;
3758 status = decode_putfh(&xdr);
3759 if (status)
3760 goto out;
3761 status = decode_close(&xdr, res);
Trond Myklebust516a6af2005-10-27 22:12:41 -04003762 if (status != 0)
3763 goto out;
3764 /*
3765 * Note: Server may do delete on close for this file
3766 * in which case the getattr call will fail with
3767 * an ESTALE error. Shouldn't be a problem,
3768 * though, since fattr->valid will remain unset.
3769 */
3770 decode_getfattr(&xdr, res->fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771out:
3772 return status;
3773}
3774
3775/*
3776 * Decode OPEN response
3777 */
3778static int nfs4_xdr_dec_open(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_openres *res)
3779{
3780 struct xdr_stream xdr;
3781 struct compound_hdr hdr;
3782 int status;
3783
3784 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3785 status = decode_compound_hdr(&xdr, &hdr);
3786 if (status)
3787 goto out;
3788 status = decode_putfh(&xdr);
3789 if (status)
3790 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04003791 status = decode_savefh(&xdr);
3792 if (status)
3793 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 status = decode_open(&xdr, res);
3795 if (status)
3796 goto out;
3797 status = decode_getfh(&xdr, &res->fh);
3798 if (status)
3799 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04003800 if (decode_getfattr(&xdr, res->f_attr, res->server) != 0)
3801 goto out;
3802 if ((status = decode_restorefh(&xdr)) != 0)
3803 goto out;
3804 decode_getfattr(&xdr, res->dir_attr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805out:
3806 return status;
3807}
3808
3809/*
3810 * Decode OPEN_CONFIRM response
3811 */
3812static int nfs4_xdr_dec_open_confirm(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_open_confirmres *res)
3813{
3814 struct xdr_stream xdr;
3815 struct compound_hdr hdr;
3816 int status;
3817
3818 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3819 status = decode_compound_hdr(&xdr, &hdr);
3820 if (status)
3821 goto out;
3822 status = decode_putfh(&xdr);
3823 if (status)
3824 goto out;
3825 status = decode_open_confirm(&xdr, res);
3826out:
3827 return status;
3828}
3829
3830/*
3831 * Decode OPEN response
3832 */
3833static int nfs4_xdr_dec_open_noattr(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_openres *res)
3834{
3835 struct xdr_stream xdr;
3836 struct compound_hdr hdr;
3837 int status;
3838
3839 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3840 status = decode_compound_hdr(&xdr, &hdr);
3841 if (status)
3842 goto out;
3843 status = decode_putfh(&xdr);
3844 if (status)
3845 goto out;
3846 status = decode_open(&xdr, res);
Trond Myklebust864472e2006-01-03 09:55:15 +01003847 if (status)
3848 goto out;
3849 decode_getfattr(&xdr, res->f_attr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850out:
3851 return status;
3852}
3853
3854/*
3855 * Decode SETATTR response
3856 */
3857static int nfs4_xdr_dec_setattr(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_setattrres *res)
3858{
3859 struct xdr_stream xdr;
3860 struct compound_hdr hdr;
3861 int status;
3862
3863 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3864 status = decode_compound_hdr(&xdr, &hdr);
3865 if (status)
3866 goto out;
3867 status = decode_putfh(&xdr);
3868 if (status)
3869 goto out;
3870 status = decode_setattr(&xdr, res);
3871 if (status)
3872 goto out;
3873 status = decode_getfattr(&xdr, res->fattr, res->server);
3874 if (status == NFS4ERR_DELAY)
3875 status = 0;
3876out:
3877 return status;
3878}
3879
3880/*
3881 * Decode LOCK response
3882 */
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003883static int nfs4_xdr_dec_lock(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_lock_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884{
3885 struct xdr_stream xdr;
3886 struct compound_hdr hdr;
3887 int status;
3888
3889 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3890 status = decode_compound_hdr(&xdr, &hdr);
3891 if (status)
3892 goto out;
3893 status = decode_putfh(&xdr);
3894 if (status)
3895 goto out;
3896 status = decode_lock(&xdr, res);
3897out:
3898 return status;
3899}
3900
3901/*
3902 * Decode LOCKT response
3903 */
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003904static int nfs4_xdr_dec_lockt(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_lockt_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905{
3906 struct xdr_stream xdr;
3907 struct compound_hdr hdr;
3908 int status;
3909
3910 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3911 status = decode_compound_hdr(&xdr, &hdr);
3912 if (status)
3913 goto out;
3914 status = decode_putfh(&xdr);
3915 if (status)
3916 goto out;
3917 status = decode_lockt(&xdr, res);
3918out:
3919 return status;
3920}
3921
3922/*
3923 * Decode LOCKU response
3924 */
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003925static int nfs4_xdr_dec_locku(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_locku_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926{
3927 struct xdr_stream xdr;
3928 struct compound_hdr hdr;
3929 int status;
3930
3931 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3932 status = decode_compound_hdr(&xdr, &hdr);
3933 if (status)
3934 goto out;
3935 status = decode_putfh(&xdr);
3936 if (status)
3937 goto out;
3938 status = decode_locku(&xdr, res);
3939out:
3940 return status;
3941}
3942
3943/*
3944 * Decode READLINK response
3945 */
3946static int nfs4_xdr_dec_readlink(struct rpc_rqst *rqstp, uint32_t *p, void *res)
3947{
3948 struct xdr_stream xdr;
3949 struct compound_hdr hdr;
3950 int status;
3951
3952 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3953 status = decode_compound_hdr(&xdr, &hdr);
3954 if (status)
3955 goto out;
3956 status = decode_putfh(&xdr);
3957 if (status)
3958 goto out;
3959 status = decode_readlink(&xdr, rqstp);
3960out:
3961 return status;
3962}
3963
3964/*
3965 * Decode READDIR response
3966 */
3967static int nfs4_xdr_dec_readdir(struct rpc_rqst *rqstp, uint32_t *p, struct nfs4_readdir_res *res)
3968{
3969 struct xdr_stream xdr;
3970 struct compound_hdr hdr;
3971 int status;
3972
3973 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3974 status = decode_compound_hdr(&xdr, &hdr);
3975 if (status)
3976 goto out;
3977 status = decode_putfh(&xdr);
3978 if (status)
3979 goto out;
3980 status = decode_readdir(&xdr, rqstp, res);
3981out:
3982 return status;
3983}
3984
3985/*
3986 * Decode Read response
3987 */
3988static int nfs4_xdr_dec_read(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_readres *res)
3989{
3990 struct xdr_stream xdr;
3991 struct compound_hdr hdr;
3992 int status;
3993
3994 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3995 status = decode_compound_hdr(&xdr, &hdr);
3996 if (status)
3997 goto out;
3998 status = decode_putfh(&xdr);
3999 if (status)
4000 goto out;
4001 status = decode_read(&xdr, rqstp, res);
4002 if (!status)
4003 status = res->count;
4004out:
4005 return status;
4006}
4007
4008/*
4009 * Decode WRITE response
4010 */
4011static int nfs4_xdr_dec_write(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_writeres *res)
4012{
4013 struct xdr_stream xdr;
4014 struct compound_hdr hdr;
4015 int status;
4016
4017 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4018 status = decode_compound_hdr(&xdr, &hdr);
4019 if (status)
4020 goto out;
4021 status = decode_putfh(&xdr);
4022 if (status)
4023 goto out;
4024 status = decode_write(&xdr, res);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004025 if (status)
4026 goto out;
4027 decode_getfattr(&xdr, res->fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 if (!status)
4029 status = res->count;
4030out:
4031 return status;
4032}
4033
4034/*
4035 * Decode COMMIT response
4036 */
4037static int nfs4_xdr_dec_commit(struct rpc_rqst *rqstp, uint32_t *p, struct nfs_writeres *res)
4038{
4039 struct xdr_stream xdr;
4040 struct compound_hdr hdr;
4041 int status;
4042
4043 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4044 status = decode_compound_hdr(&xdr, &hdr);
4045 if (status)
4046 goto out;
4047 status = decode_putfh(&xdr);
4048 if (status)
4049 goto out;
4050 status = decode_commit(&xdr, res);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004051 if (status)
4052 goto out;
4053 decode_getfattr(&xdr, res->fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054out:
4055 return status;
4056}
4057
4058/*
4059 * FSINFO request
4060 */
4061static int nfs4_xdr_dec_fsinfo(struct rpc_rqst *req, uint32_t *p, struct nfs_fsinfo *fsinfo)
4062{
4063 struct xdr_stream xdr;
4064 struct compound_hdr hdr;
4065 int status;
4066
4067 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4068 status = decode_compound_hdr(&xdr, &hdr);
4069 if (!status)
4070 status = decode_putfh(&xdr);
4071 if (!status)
4072 status = decode_fsinfo(&xdr, fsinfo);
4073 if (!status)
4074 status = -nfs_stat_to_errno(hdr.status);
4075 return status;
4076}
4077
4078/*
4079 * PATHCONF request
4080 */
4081static int nfs4_xdr_dec_pathconf(struct rpc_rqst *req, uint32_t *p, struct nfs_pathconf *pathconf)
4082{
4083 struct xdr_stream xdr;
4084 struct compound_hdr hdr;
4085 int status;
4086
4087 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4088 status = decode_compound_hdr(&xdr, &hdr);
4089 if (!status)
4090 status = decode_putfh(&xdr);
4091 if (!status)
4092 status = decode_pathconf(&xdr, pathconf);
4093 return status;
4094}
4095
4096/*
4097 * STATFS request
4098 */
4099static int nfs4_xdr_dec_statfs(struct rpc_rqst *req, uint32_t *p, struct nfs_fsstat *fsstat)
4100{
4101 struct xdr_stream xdr;
4102 struct compound_hdr hdr;
4103 int status;
4104
4105 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4106 status = decode_compound_hdr(&xdr, &hdr);
4107 if (!status)
4108 status = decode_putfh(&xdr);
4109 if (!status)
4110 status = decode_statfs(&xdr, fsstat);
4111 return status;
4112}
4113
4114/*
4115 * GETATTR_BITMAP request
4116 */
4117static int nfs4_xdr_dec_server_caps(struct rpc_rqst *req, uint32_t *p, struct nfs4_server_caps_res *res)
4118{
4119 struct xdr_stream xdr;
4120 struct compound_hdr hdr;
4121 int status;
4122
4123 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4124 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
4125 goto out;
4126 if ((status = decode_putfh(&xdr)) != 0)
4127 goto out;
4128 status = decode_server_caps(&xdr, res);
4129out:
4130 return status;
4131}
4132
4133/*
4134 * Decode RENEW response
4135 */
4136static int nfs4_xdr_dec_renew(struct rpc_rqst *rqstp, uint32_t *p, void *dummy)
4137{
4138 struct xdr_stream xdr;
4139 struct compound_hdr hdr;
4140 int status;
4141
4142 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4143 status = decode_compound_hdr(&xdr, &hdr);
4144 if (!status)
4145 status = decode_renew(&xdr);
4146 return status;
4147}
4148
4149/*
4150 * a SETCLIENTID request
4151 */
4152static int nfs4_xdr_dec_setclientid(struct rpc_rqst *req, uint32_t *p,
4153 struct nfs4_client *clp)
4154{
4155 struct xdr_stream xdr;
4156 struct compound_hdr hdr;
4157 int status;
4158
4159 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4160 status = decode_compound_hdr(&xdr, &hdr);
4161 if (!status)
4162 status = decode_setclientid(&xdr, clp);
4163 if (!status)
4164 status = -nfs_stat_to_errno(hdr.status);
4165 return status;
4166}
4167
4168/*
4169 * a SETCLIENTID_CONFIRM request
4170 */
4171static int nfs4_xdr_dec_setclientid_confirm(struct rpc_rqst *req, uint32_t *p, struct nfs_fsinfo *fsinfo)
4172{
4173 struct xdr_stream xdr;
4174 struct compound_hdr hdr;
4175 int status;
4176
4177 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4178 status = decode_compound_hdr(&xdr, &hdr);
4179 if (!status)
4180 status = decode_setclientid_confirm(&xdr);
4181 if (!status)
4182 status = decode_putrootfh(&xdr);
4183 if (!status)
4184 status = decode_fsinfo(&xdr, fsinfo);
4185 if (!status)
4186 status = -nfs_stat_to_errno(hdr.status);
4187 return status;
4188}
4189
4190/*
4191 * DELEGRETURN request
4192 */
4193static int nfs4_xdr_dec_delegreturn(struct rpc_rqst *rqstp, uint32_t *p, void *dummy)
4194{
4195 struct xdr_stream xdr;
4196 struct compound_hdr hdr;
4197 int status;
4198
4199 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4200 status = decode_compound_hdr(&xdr, &hdr);
4201 if (status == 0) {
4202 status = decode_putfh(&xdr);
4203 if (status == 0)
4204 status = decode_delegreturn(&xdr);
4205 }
4206 return status;
4207}
4208
4209uint32_t *nfs4_decode_dirent(uint32_t *p, struct nfs_entry *entry, int plus)
4210{
4211 uint32_t bitmap[2] = {0};
4212 uint32_t len;
4213
4214 if (!*p++) {
4215 if (!*p)
4216 return ERR_PTR(-EAGAIN);
4217 entry->eof = 1;
4218 return ERR_PTR(-EBADCOOKIE);
4219 }
4220
4221 entry->prev_cookie = entry->cookie;
4222 p = xdr_decode_hyper(p, &entry->cookie);
4223 entry->len = ntohl(*p++);
4224 entry->name = (const char *) p;
4225 p += XDR_QUADLEN(entry->len);
4226
4227 /*
4228 * In case the server doesn't return an inode number,
4229 * we fake one here. (We don't use inode number 0,
4230 * since glibc seems to choke on it...)
4231 */
4232 entry->ino = 1;
4233
4234 len = ntohl(*p++); /* bitmap length */
4235 if (len-- > 0) {
4236 bitmap[0] = ntohl(*p++);
4237 if (len-- > 0) {
4238 bitmap[1] = ntohl(*p++);
4239 p += len;
4240 }
4241 }
4242 len = XDR_QUADLEN(ntohl(*p++)); /* attribute buffer length */
4243 if (len > 0) {
Manoj Naik97d312d2005-06-22 17:16:39 +00004244 if (bitmap[0] & FATTR4_WORD0_RDATTR_ERROR) {
4245 bitmap[0] &= ~FATTR4_WORD0_RDATTR_ERROR;
4246 /* Ignore the return value of rdattr_error for now */
4247 p++;
4248 len--;
4249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 if (bitmap[0] == 0 && bitmap[1] == FATTR4_WORD1_MOUNTED_ON_FILEID)
4251 xdr_decode_hyper(p, &entry->ino);
4252 else if (bitmap[0] == FATTR4_WORD0_FILEID)
4253 xdr_decode_hyper(p, &entry->ino);
4254 p += len;
4255 }
4256
4257 entry->eof = !p[0] && p[1];
4258 return p;
4259}
4260
4261/*
4262 * We need to translate between nfs status return values and
4263 * the local errno values which may not be the same.
4264 */
4265static struct {
4266 int stat;
4267 int errno;
4268} nfs_errtbl[] = {
4269 { NFS4_OK, 0 },
4270 { NFS4ERR_PERM, EPERM },
4271 { NFS4ERR_NOENT, ENOENT },
4272 { NFS4ERR_IO, errno_NFSERR_IO },
4273 { NFS4ERR_NXIO, ENXIO },
4274 { NFS4ERR_ACCESS, EACCES },
4275 { NFS4ERR_EXIST, EEXIST },
4276 { NFS4ERR_XDEV, EXDEV },
4277 { NFS4ERR_NOTDIR, ENOTDIR },
4278 { NFS4ERR_ISDIR, EISDIR },
4279 { NFS4ERR_INVAL, EINVAL },
4280 { NFS4ERR_FBIG, EFBIG },
4281 { NFS4ERR_NOSPC, ENOSPC },
4282 { NFS4ERR_ROFS, EROFS },
4283 { NFS4ERR_MLINK, EMLINK },
4284 { NFS4ERR_NAMETOOLONG, ENAMETOOLONG },
4285 { NFS4ERR_NOTEMPTY, ENOTEMPTY },
4286 { NFS4ERR_DQUOT, EDQUOT },
4287 { NFS4ERR_STALE, ESTALE },
4288 { NFS4ERR_BADHANDLE, EBADHANDLE },
Manoj Naik6ebf3652005-06-22 17:16:39 +00004289 { NFS4ERR_BADOWNER, EINVAL },
4290 { NFS4ERR_BADNAME, EINVAL },
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 { NFS4ERR_BAD_COOKIE, EBADCOOKIE },
4292 { NFS4ERR_NOTSUPP, ENOTSUPP },
4293 { NFS4ERR_TOOSMALL, ETOOSMALL },
4294 { NFS4ERR_SERVERFAULT, ESERVERFAULT },
4295 { NFS4ERR_BADTYPE, EBADTYPE },
4296 { NFS4ERR_LOCKED, EAGAIN },
4297 { NFS4ERR_RESOURCE, EREMOTEIO },
4298 { NFS4ERR_SYMLINK, ELOOP },
4299 { NFS4ERR_OP_ILLEGAL, EOPNOTSUPP },
4300 { NFS4ERR_DEADLOCK, EDEADLK },
4301 { NFS4ERR_WRONGSEC, EPERM }, /* FIXME: this needs
4302 * to be handled by a
4303 * middle-layer.
4304 */
4305 { -1, EIO }
4306};
4307
4308/*
4309 * Convert an NFS error code to a local one.
4310 * This one is used jointly by NFSv2 and NFSv3.
4311 */
4312static int
4313nfs_stat_to_errno(int stat)
4314{
4315 int i;
4316 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
4317 if (nfs_errtbl[i].stat == stat)
4318 return nfs_errtbl[i].errno;
4319 }
4320 if (stat <= 10000 || stat > 10100) {
4321 /* The server is looney tunes. */
4322 return ESERVERFAULT;
4323 }
4324 /* If we cannot translate the error, the recovery routines should
4325 * handle it.
4326 * Note: remaining NFSv4 error codes have values > 10000, so should
4327 * not conflict with native Linux error codes.
4328 */
4329 return stat;
4330}
4331
4332#ifndef MAX
4333# define MAX(a, b) (((a) > (b))? (a) : (b))
4334#endif
4335
4336#define PROC(proc, argtype, restype) \
4337[NFSPROC4_CLNT_##proc] = { \
4338 .p_proc = NFSPROC4_COMPOUND, \
4339 .p_encode = (kxdrproc_t) nfs4_xdr_##argtype, \
4340 .p_decode = (kxdrproc_t) nfs4_xdr_##restype, \
4341 .p_bufsiz = MAX(NFS4_##argtype##_sz,NFS4_##restype##_sz) << 2, \
4342 }
4343
4344struct rpc_procinfo nfs4_procedures[] = {
4345 PROC(READ, enc_read, dec_read),
4346 PROC(WRITE, enc_write, dec_write),
4347 PROC(COMMIT, enc_commit, dec_commit),
4348 PROC(OPEN, enc_open, dec_open),
4349 PROC(OPEN_CONFIRM, enc_open_confirm, dec_open_confirm),
4350 PROC(OPEN_NOATTR, enc_open_noattr, dec_open_noattr),
4351 PROC(OPEN_DOWNGRADE, enc_open_downgrade, dec_open_downgrade),
4352 PROC(CLOSE, enc_close, dec_close),
4353 PROC(SETATTR, enc_setattr, dec_setattr),
4354 PROC(FSINFO, enc_fsinfo, dec_fsinfo),
4355 PROC(RENEW, enc_renew, dec_renew),
4356 PROC(SETCLIENTID, enc_setclientid, dec_setclientid),
4357 PROC(SETCLIENTID_CONFIRM, enc_setclientid_confirm, dec_setclientid_confirm),
4358 PROC(LOCK, enc_lock, dec_lock),
4359 PROC(LOCKT, enc_lockt, dec_lockt),
4360 PROC(LOCKU, enc_locku, dec_locku),
4361 PROC(ACCESS, enc_access, dec_access),
4362 PROC(GETATTR, enc_getattr, dec_getattr),
4363 PROC(LOOKUP, enc_lookup, dec_lookup),
4364 PROC(LOOKUP_ROOT, enc_lookup_root, dec_lookup_root),
4365 PROC(REMOVE, enc_remove, dec_remove),
4366 PROC(RENAME, enc_rename, dec_rename),
4367 PROC(LINK, enc_link, dec_link),
4368 PROC(SYMLINK, enc_symlink, dec_symlink),
4369 PROC(CREATE, enc_create, dec_create),
4370 PROC(PATHCONF, enc_pathconf, dec_pathconf),
4371 PROC(STATFS, enc_statfs, dec_statfs),
4372 PROC(READLINK, enc_readlink, dec_readlink),
4373 PROC(READDIR, enc_readdir, dec_readdir),
4374 PROC(SERVER_CAPS, enc_server_caps, dec_server_caps),
4375 PROC(DELEGRETURN, enc_delegreturn, dec_delegreturn),
J. Bruce Fields029d1052005-06-22 17:16:22 +00004376 PROC(GETACL, enc_getacl, dec_getacl),
J. Bruce Fields23ec6962005-06-22 17:16:22 +00004377 PROC(SETACL, enc_setacl, dec_setacl),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378};
4379
4380struct rpc_version nfs_version4 = {
4381 .number = 4,
4382 .nrprocs = sizeof(nfs4_procedures)/sizeof(nfs4_procedures[0]),
4383 .procs = nfs4_procedures
4384};
4385
4386/*
4387 * Local variables:
4388 * c-basic-offset: 8
4389 * End:
4390 */