blob: 5f353d4686b6bae6e71abcc2b68a0cdfc21ac1d4 [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
David Howells0a8ea432006-08-22 20:06:08 -040061static int nfs4_stat_to_errno(int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
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:
Trond Myklebust9f958ab2007-07-02 13:58:33 -040071 * we currently use size 2 (u64) out of (NFS4_OPAQUE_LIMIT >> 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 */
Trond Myklebust9f958ab2007-07-02 13:58:33 -040073#define open_owner_id_maxsz (1 + 4)
74#define lock_owner_id_maxsz (1 + 4)
Trond Myklebust9104a552007-07-17 21:52:42 -040075#define decode_lockowner_maxsz (1 + XDR_QUADLEN(IDMAP_NAMESZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#define compound_encode_hdr_maxsz (3 + (NFS4_MAXTAGLEN >> 2))
77#define compound_decode_hdr_maxsz (3 + (NFS4_MAXTAGLEN >> 2))
78#define op_encode_hdr_maxsz (1)
79#define op_decode_hdr_maxsz (2)
Trond Myklebust9104a552007-07-17 21:52:42 -040080#define encode_stateid_maxsz (XDR_QUADLEN(NFS4_STATEID_SIZE))
81#define decode_stateid_maxsz (XDR_QUADLEN(NFS4_STATEID_SIZE))
82#define encode_verifier_maxsz (XDR_QUADLEN(NFS4_VERIFIER_SIZE))
83#define decode_verifier_maxsz (XDR_QUADLEN(NFS4_VERIFIER_SIZE))
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#define encode_putfh_maxsz (op_encode_hdr_maxsz + 1 + \
85 (NFS4_FHSIZE >> 2))
86#define decode_putfh_maxsz (op_decode_hdr_maxsz)
87#define encode_putrootfh_maxsz (op_encode_hdr_maxsz)
88#define decode_putrootfh_maxsz (op_decode_hdr_maxsz)
89#define encode_getfh_maxsz (op_encode_hdr_maxsz)
90#define decode_getfh_maxsz (op_decode_hdr_maxsz + 1 + \
91 ((3+NFS4_FHSIZE) >> 2))
J. Bruce Fields96928202005-06-22 17:16:22 +000092#define nfs4_fattr_bitmap_maxsz 3
93#define encode_getattr_maxsz (op_encode_hdr_maxsz + nfs4_fattr_bitmap_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#define nfs4_name_maxsz (1 + ((3 + NFS4_MAXNAMLEN) >> 2))
95#define nfs4_path_maxsz (1 + ((3 + NFS4_MAXPATHLEN) >> 2))
Trond Myklebustbd625ba2007-07-08 18:38:23 -040096#define nfs4_owner_maxsz (1 + XDR_QUADLEN(IDMAP_NAMESZ))
97#define nfs4_group_maxsz (1 + XDR_QUADLEN(IDMAP_NAMESZ))
J. Bruce Fields96928202005-06-22 17:16:22 +000098/* This is based on getfattr, which uses the most attributes: */
99#define nfs4_fattr_value_maxsz (1 + (1 + 2 + 2 + 4 + 2 + 1 + 1 + 2 + 2 + \
Trond Myklebustbd625ba2007-07-08 18:38:23 -0400100 3 + 3 + 3 + nfs4_owner_maxsz + nfs4_group_maxsz))
J. Bruce Fields96928202005-06-22 17:16:22 +0000101#define nfs4_fattr_maxsz (nfs4_fattr_bitmap_maxsz + \
102 nfs4_fattr_value_maxsz)
103#define decode_getattr_maxsz (op_decode_hdr_maxsz + nfs4_fattr_maxsz)
Trond Myklebust9104a552007-07-17 21:52:42 -0400104#define encode_attrs_maxsz (nfs4_fattr_bitmap_maxsz + \
105 1 + 2 + 1 + \
106 nfs4_owner_maxsz + \
107 nfs4_group_maxsz + \
108 4 + 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#define encode_savefh_maxsz (op_encode_hdr_maxsz)
110#define decode_savefh_maxsz (op_decode_hdr_maxsz)
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400111#define encode_restorefh_maxsz (op_encode_hdr_maxsz)
112#define decode_restorefh_maxsz (op_decode_hdr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#define encode_fsinfo_maxsz (op_encode_hdr_maxsz + 2)
114#define decode_fsinfo_maxsz (op_decode_hdr_maxsz + 11)
115#define encode_renew_maxsz (op_encode_hdr_maxsz + 3)
116#define decode_renew_maxsz (op_decode_hdr_maxsz)
117#define encode_setclientid_maxsz \
118 (op_encode_hdr_maxsz + \
119 4 /*server->ip_addr*/ + \
120 1 /*Netid*/ + \
121 6 /*uaddr*/ + \
122 6 + (NFS4_VERIFIER_SIZE >> 2))
123#define decode_setclientid_maxsz \
124 (op_decode_hdr_maxsz + \
125 2 + \
126 1024) /* large value for CLID_INUSE */
127#define encode_setclientid_confirm_maxsz \
128 (op_encode_hdr_maxsz + \
129 3 + (NFS4_VERIFIER_SIZE >> 2))
130#define decode_setclientid_confirm_maxsz \
131 (op_decode_hdr_maxsz)
Trond Myklebuste6889622007-07-02 13:58:30 -0400132#define encode_lookup_maxsz (op_encode_hdr_maxsz + nfs4_name_maxsz)
133#define decode_lookup_maxsz (op_decode_hdr_maxsz)
Trond Myklebust2cebf822007-07-02 13:57:28 -0400134#define encode_share_access_maxsz \
135 (2)
Trond Myklebust9104a552007-07-17 21:52:42 -0400136#define encode_createmode_maxsz (1 + encode_attrs_maxsz)
Trond Myklebust2cebf822007-07-02 13:57:28 -0400137#define encode_opentype_maxsz (1 + encode_createmode_maxsz)
138#define encode_claim_null_maxsz (1 + nfs4_name_maxsz)
139#define encode_open_maxsz (op_encode_hdr_maxsz + \
140 2 + encode_share_access_maxsz + 2 + \
141 open_owner_id_maxsz + \
142 encode_opentype_maxsz + \
143 encode_claim_null_maxsz)
144#define decode_ace_maxsz (3 + nfs4_owner_maxsz)
Trond Myklebust9104a552007-07-17 21:52:42 -0400145#define decode_delegation_maxsz (1 + decode_stateid_maxsz + 1 + \
Trond Myklebust2cebf822007-07-02 13:57:28 -0400146 decode_ace_maxsz)
147#define decode_change_info_maxsz (5)
148#define decode_open_maxsz (op_decode_hdr_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400149 decode_stateid_maxsz + \
Trond Myklebust2cebf822007-07-02 13:57:28 -0400150 decode_change_info_maxsz + 1 + \
151 nfs4_fattr_bitmap_maxsz + \
152 decode_delegation_maxsz)
Trond Myklebust9104a552007-07-17 21:52:42 -0400153#define encode_open_confirm_maxsz \
154 (op_encode_hdr_maxsz + \
155 encode_stateid_maxsz + 1)
156#define decode_open_confirm_maxsz \
157 (op_decode_hdr_maxsz + \
158 decode_stateid_maxsz)
159#define encode_open_downgrade_maxsz \
160 (op_encode_hdr_maxsz + \
161 encode_stateid_maxsz + 1 + \
162 encode_share_access_maxsz)
163#define decode_open_downgrade_maxsz \
164 (op_decode_hdr_maxsz + \
165 decode_stateid_maxsz)
166#define encode_close_maxsz (op_encode_hdr_maxsz + \
167 1 + encode_stateid_maxsz)
168#define decode_close_maxsz (op_decode_hdr_maxsz + \
169 decode_stateid_maxsz)
170#define encode_setattr_maxsz (op_encode_hdr_maxsz + \
171 encode_stateid_maxsz + \
172 encode_attrs_maxsz)
173#define decode_setattr_maxsz (op_decode_hdr_maxsz + \
174 nfs4_fattr_bitmap_maxsz)
175#define encode_read_maxsz (op_encode_hdr_maxsz + \
176 encode_stateid_maxsz + 3)
177#define decode_read_maxsz (op_decode_hdr_maxsz + 2)
178#define encode_readdir_maxsz (op_encode_hdr_maxsz + \
179 2 + encode_verifier_maxsz + 5)
180#define decode_readdir_maxsz (op_decode_hdr_maxsz + \
181 decode_verifier_maxsz)
182#define encode_readlink_maxsz (op_encode_hdr_maxsz)
183#define decode_readlink_maxsz (op_decode_hdr_maxsz + 1)
184#define encode_write_maxsz (op_encode_hdr_maxsz + \
185 encode_stateid_maxsz + 4)
186#define decode_write_maxsz (op_decode_hdr_maxsz + \
187 2 + decode_verifier_maxsz)
188#define encode_commit_maxsz (op_encode_hdr_maxsz + 3)
189#define decode_commit_maxsz (op_decode_hdr_maxsz + \
190 decode_verifier_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191#define encode_remove_maxsz (op_encode_hdr_maxsz + \
192 nfs4_name_maxsz)
193#define encode_rename_maxsz (op_encode_hdr_maxsz + \
194 2 * nfs4_name_maxsz)
195#define decode_rename_maxsz (op_decode_hdr_maxsz + 5 + 5)
196#define encode_link_maxsz (op_encode_hdr_maxsz + \
197 nfs4_name_maxsz)
198#define decode_link_maxsz (op_decode_hdr_maxsz + 5)
Trond Myklebust9104a552007-07-17 21:52:42 -0400199#define encode_lock_maxsz (op_encode_hdr_maxsz + \
200 7 + \
201 1 + encode_stateid_maxsz + 8)
202#define decode_lock_denied_maxsz \
203 (8 + decode_lockowner_maxsz)
204#define decode_lock_maxsz (op_decode_hdr_maxsz + \
205 decode_lock_denied_maxsz)
206#define encode_lockt_maxsz (op_encode_hdr_maxsz + 12)
207#define decode_lockt_maxsz (op_decode_hdr_maxsz + \
208 decode_lock_denied_maxsz)
209#define encode_locku_maxsz (op_encode_hdr_maxsz + 3 + \
210 encode_stateid_maxsz + \
211 4)
212#define decode_locku_maxsz (op_decode_hdr_maxsz + \
213 decode_stateid_maxsz)
214#define encode_access_maxsz (op_encode_hdr_maxsz + 1)
215#define decode_access_maxsz (op_decode_hdr_maxsz + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216#define encode_symlink_maxsz (op_encode_hdr_maxsz + \
217 1 + nfs4_name_maxsz + \
Chuck Lever94a6d752006-08-22 20:06:23 -0400218 1 + \
J. Bruce Fields96928202005-06-22 17:16:22 +0000219 nfs4_fattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220#define decode_symlink_maxsz (op_decode_hdr_maxsz + 8)
221#define encode_create_maxsz (op_encode_hdr_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400222 1 + 2 + nfs4_name_maxsz + \
223 encode_attrs_maxsz)
Trond Myklebust2cebf822007-07-02 13:57:28 -0400224#define decode_create_maxsz (op_decode_hdr_maxsz + \
225 decode_change_info_maxsz + \
226 nfs4_fattr_bitmap_maxsz)
Trond Myklebust9104a552007-07-17 21:52:42 -0400227#define encode_statfs_maxsz (encode_getattr_maxsz)
228#define decode_statfs_maxsz (decode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229#define encode_delegreturn_maxsz (op_encode_hdr_maxsz + 4)
230#define decode_delegreturn_maxsz (op_decode_hdr_maxsz)
Trond Myklebust9104a552007-07-17 21:52:42 -0400231#define encode_getacl_maxsz (encode_getattr_maxsz)
232#define decode_getacl_maxsz (op_decode_hdr_maxsz + \
233 nfs4_fattr_bitmap_maxsz + 1)
234#define encode_setacl_maxsz (op_encode_hdr_maxsz + \
235 encode_stateid_maxsz + 3)
236#define decode_setacl_maxsz (decode_setattr_maxsz)
Trond Myklebuste6889622007-07-02 13:58:30 -0400237#define encode_fs_locations_maxsz \
238 (encode_getattr_maxsz)
239#define decode_fs_locations_maxsz \
240 (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241#define NFS4_enc_compound_sz (1024) /* XXX: large enough? */
242#define NFS4_dec_compound_sz (1024) /* XXX: large enough? */
243#define NFS4_enc_read_sz (compound_encode_hdr_maxsz + \
244 encode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400245 encode_read_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246#define NFS4_dec_read_sz (compound_decode_hdr_maxsz + \
247 decode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400248 decode_read_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249#define NFS4_enc_readlink_sz (compound_encode_hdr_maxsz + \
250 encode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400251 encode_readlink_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252#define NFS4_dec_readlink_sz (compound_decode_hdr_maxsz + \
253 decode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400254 decode_readlink_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255#define NFS4_enc_readdir_sz (compound_encode_hdr_maxsz + \
256 encode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400257 encode_readdir_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258#define NFS4_dec_readdir_sz (compound_decode_hdr_maxsz + \
259 decode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400260 decode_readdir_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261#define NFS4_enc_write_sz (compound_encode_hdr_maxsz + \
262 encode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400263 encode_write_maxsz + \
Trond Myklebust4f9838c2005-10-27 22:12:44 -0400264 encode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265#define NFS4_dec_write_sz (compound_decode_hdr_maxsz + \
266 decode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400267 decode_write_maxsz + \
Trond Myklebust4f9838c2005-10-27 22:12:44 -0400268 decode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269#define NFS4_enc_commit_sz (compound_encode_hdr_maxsz + \
270 encode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400271 encode_commit_maxsz + \
Trond Myklebust4f9838c2005-10-27 22:12:44 -0400272 encode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273#define NFS4_dec_commit_sz (compound_decode_hdr_maxsz + \
274 decode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400275 decode_commit_maxsz + \
Trond Myklebust4f9838c2005-10-27 22:12:44 -0400276 decode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277#define NFS4_enc_open_sz (compound_encode_hdr_maxsz + \
Trond Myklebust2cebf822007-07-02 13:57:28 -0400278 encode_putfh_maxsz + \
279 encode_savefh_maxsz + \
280 encode_open_maxsz + \
281 encode_getfh_maxsz + \
282 encode_getattr_maxsz + \
283 encode_restorefh_maxsz + \
284 encode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285#define NFS4_dec_open_sz (compound_decode_hdr_maxsz + \
Trond Myklebust2cebf822007-07-02 13:57:28 -0400286 decode_putfh_maxsz + \
287 decode_savefh_maxsz + \
288 decode_open_maxsz + \
289 decode_getfh_maxsz + \
290 decode_getattr_maxsz + \
291 decode_restorefh_maxsz + \
292 decode_getattr_maxsz)
Trond Myklebust9104a552007-07-17 21:52:42 -0400293#define NFS4_enc_open_confirm_sz \
294 (compound_encode_hdr_maxsz + \
295 encode_putfh_maxsz + \
296 encode_open_confirm_maxsz)
297#define NFS4_dec_open_confirm_sz \
298 (compound_decode_hdr_maxsz + \
299 decode_putfh_maxsz + \
300 decode_open_confirm_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301#define NFS4_enc_open_noattr_sz (compound_encode_hdr_maxsz + \
302 encode_putfh_maxsz + \
Trond Myklebust2cebf822007-07-02 13:57:28 -0400303 encode_open_maxsz + \
304 encode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305#define NFS4_dec_open_noattr_sz (compound_decode_hdr_maxsz + \
306 decode_putfh_maxsz + \
Trond Myklebust2cebf822007-07-02 13:57:28 -0400307 decode_open_maxsz + \
308 decode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309#define NFS4_enc_open_downgrade_sz \
310 (compound_encode_hdr_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400311 encode_putfh_maxsz + \
312 encode_open_downgrade_maxsz + \
313 encode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314#define NFS4_dec_open_downgrade_sz \
315 (compound_decode_hdr_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400316 decode_putfh_maxsz + \
317 decode_open_downgrade_maxsz + \
318 decode_getattr_maxsz)
319#define NFS4_enc_close_sz (compound_encode_hdr_maxsz + \
320 encode_putfh_maxsz + \
321 encode_close_maxsz + \
322 encode_getattr_maxsz)
323#define NFS4_dec_close_sz (compound_decode_hdr_maxsz + \
324 decode_putfh_maxsz + \
325 decode_close_maxsz + \
326 decode_getattr_maxsz)
327#define NFS4_enc_setattr_sz (compound_encode_hdr_maxsz + \
328 encode_putfh_maxsz + \
329 encode_setattr_maxsz + \
330 encode_getattr_maxsz)
331#define NFS4_dec_setattr_sz (compound_decode_hdr_maxsz + \
332 decode_putfh_maxsz + \
333 decode_setattr_maxsz + \
334 decode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335#define NFS4_enc_fsinfo_sz (compound_encode_hdr_maxsz + \
336 encode_putfh_maxsz + \
337 encode_fsinfo_maxsz)
338#define NFS4_dec_fsinfo_sz (compound_decode_hdr_maxsz + \
339 decode_putfh_maxsz + \
340 decode_fsinfo_maxsz)
341#define NFS4_enc_renew_sz (compound_encode_hdr_maxsz + \
342 encode_renew_maxsz)
343#define NFS4_dec_renew_sz (compound_decode_hdr_maxsz + \
344 decode_renew_maxsz)
345#define NFS4_enc_setclientid_sz (compound_encode_hdr_maxsz + \
346 encode_setclientid_maxsz)
347#define NFS4_dec_setclientid_sz (compound_decode_hdr_maxsz + \
348 decode_setclientid_maxsz)
349#define NFS4_enc_setclientid_confirm_sz \
350 (compound_encode_hdr_maxsz + \
351 encode_setclientid_confirm_maxsz + \
352 encode_putrootfh_maxsz + \
353 encode_fsinfo_maxsz)
354#define NFS4_dec_setclientid_confirm_sz \
355 (compound_decode_hdr_maxsz + \
356 decode_setclientid_confirm_maxsz + \
357 decode_putrootfh_maxsz + \
358 decode_fsinfo_maxsz)
359#define NFS4_enc_lock_sz (compound_encode_hdr_maxsz + \
360 encode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400361 encode_lock_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362#define NFS4_dec_lock_sz (compound_decode_hdr_maxsz + \
363 decode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400364 decode_lock_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365#define NFS4_enc_lockt_sz (compound_encode_hdr_maxsz + \
366 encode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400367 encode_lockt_maxsz)
368#define NFS4_dec_lockt_sz (compound_decode_hdr_maxsz + \
369 decode_putfh_maxsz + \
370 decode_lockt_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371#define NFS4_enc_locku_sz (compound_encode_hdr_maxsz + \
372 encode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400373 encode_locku_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374#define NFS4_dec_locku_sz (compound_decode_hdr_maxsz + \
375 decode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400376 decode_locku_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377#define NFS4_enc_access_sz (compound_encode_hdr_maxsz + \
378 encode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400379 encode_access_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380#define NFS4_dec_access_sz (compound_decode_hdr_maxsz + \
381 decode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400382 decode_access_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383#define NFS4_enc_getattr_sz (compound_encode_hdr_maxsz + \
384 encode_putfh_maxsz + \
385 encode_getattr_maxsz)
386#define NFS4_dec_getattr_sz (compound_decode_hdr_maxsz + \
387 decode_putfh_maxsz + \
388 decode_getattr_maxsz)
389#define NFS4_enc_lookup_sz (compound_encode_hdr_maxsz + \
390 encode_putfh_maxsz + \
391 encode_lookup_maxsz + \
392 encode_getattr_maxsz + \
393 encode_getfh_maxsz)
394#define NFS4_dec_lookup_sz (compound_decode_hdr_maxsz + \
395 decode_putfh_maxsz + \
Trond Myklebuste6889622007-07-02 13:58:30 -0400396 decode_lookup_maxsz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 decode_getattr_maxsz + \
398 decode_getfh_maxsz)
399#define NFS4_enc_lookup_root_sz (compound_encode_hdr_maxsz + \
400 encode_putrootfh_maxsz + \
401 encode_getattr_maxsz + \
402 encode_getfh_maxsz)
403#define NFS4_dec_lookup_root_sz (compound_decode_hdr_maxsz + \
404 decode_putrootfh_maxsz + \
405 decode_getattr_maxsz + \
406 decode_getfh_maxsz)
407#define NFS4_enc_remove_sz (compound_encode_hdr_maxsz + \
408 encode_putfh_maxsz + \
Trond Myklebust16e42952005-10-27 22:12:44 -0400409 encode_remove_maxsz + \
410 encode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411#define NFS4_dec_remove_sz (compound_decode_hdr_maxsz + \
412 decode_putfh_maxsz + \
Trond Myklebust16e42952005-10-27 22:12:44 -0400413 op_decode_hdr_maxsz + 5 + \
414 decode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415#define NFS4_enc_rename_sz (compound_encode_hdr_maxsz + \
416 encode_putfh_maxsz + \
417 encode_savefh_maxsz + \
418 encode_putfh_maxsz + \
Trond Myklebust6caf2c82005-10-27 22:12:43 -0400419 encode_rename_maxsz + \
420 encode_getattr_maxsz + \
421 encode_restorefh_maxsz + \
422 encode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423#define NFS4_dec_rename_sz (compound_decode_hdr_maxsz + \
424 decode_putfh_maxsz + \
425 decode_savefh_maxsz + \
426 decode_putfh_maxsz + \
Trond Myklebust6caf2c82005-10-27 22:12:43 -0400427 decode_rename_maxsz + \
428 decode_getattr_maxsz + \
429 decode_restorefh_maxsz + \
430 decode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431#define NFS4_enc_link_sz (compound_encode_hdr_maxsz + \
432 encode_putfh_maxsz + \
433 encode_savefh_maxsz + \
434 encode_putfh_maxsz + \
Trond Myklebust91ba2ee2005-10-27 22:12:42 -0400435 encode_link_maxsz + \
436 decode_getattr_maxsz + \
437 encode_restorefh_maxsz + \
438 decode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439#define NFS4_dec_link_sz (compound_decode_hdr_maxsz + \
440 decode_putfh_maxsz + \
441 decode_savefh_maxsz + \
442 decode_putfh_maxsz + \
Trond Myklebust91ba2ee2005-10-27 22:12:42 -0400443 decode_link_maxsz + \
444 decode_getattr_maxsz + \
445 decode_restorefh_maxsz + \
446 decode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447#define NFS4_enc_symlink_sz (compound_encode_hdr_maxsz + \
448 encode_putfh_maxsz + \
449 encode_symlink_maxsz + \
450 encode_getattr_maxsz + \
451 encode_getfh_maxsz)
452#define NFS4_dec_symlink_sz (compound_decode_hdr_maxsz + \
453 decode_putfh_maxsz + \
454 decode_symlink_maxsz + \
455 decode_getattr_maxsz + \
456 decode_getfh_maxsz)
457#define NFS4_enc_create_sz (compound_encode_hdr_maxsz + \
458 encode_putfh_maxsz + \
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400459 encode_savefh_maxsz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 encode_create_maxsz + \
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400461 encode_getfh_maxsz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 encode_getattr_maxsz + \
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400463 encode_restorefh_maxsz + \
464 encode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465#define NFS4_dec_create_sz (compound_decode_hdr_maxsz + \
466 decode_putfh_maxsz + \
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400467 decode_savefh_maxsz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 decode_create_maxsz + \
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400469 decode_getfh_maxsz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 decode_getattr_maxsz + \
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400471 decode_restorefh_maxsz + \
472 decode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473#define NFS4_enc_pathconf_sz (compound_encode_hdr_maxsz + \
474 encode_putfh_maxsz + \
475 encode_getattr_maxsz)
476#define NFS4_dec_pathconf_sz (compound_decode_hdr_maxsz + \
477 decode_putfh_maxsz + \
478 decode_getattr_maxsz)
479#define NFS4_enc_statfs_sz (compound_encode_hdr_maxsz + \
480 encode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400481 encode_statfs_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482#define NFS4_dec_statfs_sz (compound_decode_hdr_maxsz + \
483 decode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400484 decode_statfs_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485#define NFS4_enc_server_caps_sz (compound_encode_hdr_maxsz + \
Trond Myklebustab91f262007-02-02 14:47:17 -0800486 encode_putfh_maxsz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 encode_getattr_maxsz)
488#define NFS4_dec_server_caps_sz (compound_decode_hdr_maxsz + \
Trond Myklebustab91f262007-02-02 14:47:17 -0800489 decode_putfh_maxsz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 decode_getattr_maxsz)
491#define NFS4_enc_delegreturn_sz (compound_encode_hdr_maxsz + \
492 encode_putfh_maxsz + \
Trond Myklebustfa178f22006-01-03 09:55:38 +0100493 encode_delegreturn_maxsz + \
494 encode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495#define NFS4_dec_delegreturn_sz (compound_decode_hdr_maxsz + \
Trond Myklebustfa178f22006-01-03 09:55:38 +0100496 decode_delegreturn_maxsz + \
497 decode_getattr_maxsz)
J. Bruce Fields029d1052005-06-22 17:16:22 +0000498#define NFS4_enc_getacl_sz (compound_encode_hdr_maxsz + \
499 encode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400500 encode_getacl_maxsz)
J. Bruce Fields029d1052005-06-22 17:16:22 +0000501#define NFS4_dec_getacl_sz (compound_decode_hdr_maxsz + \
502 decode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400503 decode_getacl_maxsz)
J. Bruce Fields23ec6962005-06-22 17:16:22 +0000504#define NFS4_enc_setacl_sz (compound_encode_hdr_maxsz + \
505 encode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400506 encode_setacl_maxsz)
J. Bruce Fields23ec6962005-06-22 17:16:22 +0000507#define NFS4_dec_setacl_sz (compound_decode_hdr_maxsz + \
508 decode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400509 decode_setacl_maxsz)
Trond Myklebust683b57b2006-06-09 09:34:22 -0400510#define NFS4_enc_fs_locations_sz \
511 (compound_encode_hdr_maxsz + \
512 encode_putfh_maxsz + \
Trond Myklebuste6889622007-07-02 13:58:30 -0400513 encode_lookup_maxsz + \
514 encode_fs_locations_maxsz)
Trond Myklebust683b57b2006-06-09 09:34:22 -0400515#define NFS4_dec_fs_locations_sz \
516 (compound_decode_hdr_maxsz + \
517 decode_putfh_maxsz + \
Trond Myklebuste6889622007-07-02 13:58:30 -0400518 decode_lookup_maxsz + \
519 decode_fs_locations_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521static struct {
522 unsigned int mode;
523 unsigned int nfs2type;
524} nfs_type2fmt[] = {
525 { 0, NFNON },
526 { S_IFREG, NFREG },
527 { S_IFDIR, NFDIR },
528 { S_IFBLK, NFBLK },
529 { S_IFCHR, NFCHR },
530 { S_IFLNK, NFLNK },
531 { S_IFSOCK, NFSOCK },
532 { S_IFIFO, NFFIFO },
533 { 0, NFNON },
534 { 0, NFNON },
535};
536
537struct compound_hdr {
538 int32_t status;
539 uint32_t nops;
540 uint32_t taglen;
541 char * tag;
542};
543
544/*
545 * START OF "GENERIC" ENCODE ROUTINES.
546 * These may look a little ugly since they are imported from a "generic"
547 * set of XDR encode/decode routines which are intended to be shared by
548 * all of our NFSv4 implementations (OpenBSD, MacOS X...).
549 *
550 * If the pain of reading these is too great, it should be a straightforward
551 * task to translate them into Linux-specific versions which are more
552 * consistent with the style used in NFSv2/v3...
553 */
554#define WRITE32(n) *p++ = htonl(n)
555#define WRITE64(n) do { \
556 *p++ = htonl((uint32_t)((n) >> 32)); \
557 *p++ = htonl((uint32_t)(n)); \
558} while (0)
559#define WRITEMEM(ptr,nbytes) do { \
560 p = xdr_encode_opaque_fixed(p, ptr, nbytes); \
561} while (0)
562
563#define RESERVE_SPACE(nbytes) do { \
564 p = xdr_reserve_space(xdr, nbytes); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 BUG_ON(!p); \
566} while (0)
567
568static void encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
569{
Al Viro8687b632006-10-19 23:28:48 -0700570 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 p = xdr_reserve_space(xdr, 4 + len);
573 BUG_ON(p == NULL);
574 xdr_encode_opaque(p, str, len);
575}
576
577static int encode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
578{
Al Viro8687b632006-10-19 23:28:48 -0700579 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 dprintk("encode_compound: tag=%.*s\n", (int)hdr->taglen, hdr->tag);
582 BUG_ON(hdr->taglen > NFS4_MAXTAGLEN);
583 RESERVE_SPACE(12+(XDR_QUADLEN(hdr->taglen)<<2));
584 WRITE32(hdr->taglen);
585 WRITEMEM(hdr->tag, hdr->taglen);
586 WRITE32(NFS4_MINOR_VERSION);
587 WRITE32(hdr->nops);
588 return 0;
589}
590
591static void encode_nfs4_verifier(struct xdr_stream *xdr, const nfs4_verifier *verf)
592{
Al Viro8687b632006-10-19 23:28:48 -0700593 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
596 BUG_ON(p == NULL);
597 xdr_encode_opaque_fixed(p, verf->data, NFS4_VERIFIER_SIZE);
598}
599
600static int encode_attrs(struct xdr_stream *xdr, const struct iattr *iap, const struct nfs_server *server)
601{
602 char owner_name[IDMAP_NAMESZ];
603 char owner_group[IDMAP_NAMESZ];
604 int owner_namelen = 0;
605 int owner_grouplen = 0;
Al Viro8687b632006-10-19 23:28:48 -0700606 __be32 *p;
607 __be32 *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 int len;
609 uint32_t bmval0 = 0;
610 uint32_t bmval1 = 0;
611 int status;
612
613 /*
614 * We reserve enough space to write the entire attribute buffer at once.
615 * In the worst-case, this would be
616 * 12(bitmap) + 4(attrlen) + 8(size) + 4(mode) + 4(atime) + 4(mtime)
617 * = 36 bytes, plus any contribution from variable-length fields
J. Bruce Fields23ec6962005-06-22 17:16:22 +0000618 * such as owner/group.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 */
620 len = 16;
621
622 /* Sigh */
623 if (iap->ia_valid & ATTR_SIZE)
624 len += 8;
625 if (iap->ia_valid & ATTR_MODE)
626 len += 4;
627 if (iap->ia_valid & ATTR_UID) {
David Howells7539bba2006-08-22 20:06:09 -0400628 owner_namelen = nfs_map_uid_to_name(server->nfs_client, iap->ia_uid, owner_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (owner_namelen < 0) {
Chuck Leverfe82a182007-09-11 18:01:10 -0400630 dprintk("nfs: couldn't resolve uid %d to string\n",
631 iap->ia_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /* XXX */
633 strcpy(owner_name, "nobody");
634 owner_namelen = sizeof("nobody") - 1;
635 /* goto out; */
636 }
637 len += 4 + (XDR_QUADLEN(owner_namelen) << 2);
638 }
639 if (iap->ia_valid & ATTR_GID) {
David Howells7539bba2006-08-22 20:06:09 -0400640 owner_grouplen = nfs_map_gid_to_group(server->nfs_client, iap->ia_gid, owner_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (owner_grouplen < 0) {
Chuck Leverfe82a182007-09-11 18:01:10 -0400642 dprintk("nfs: couldn't resolve gid %d to string\n",
643 iap->ia_gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 strcpy(owner_group, "nobody");
645 owner_grouplen = sizeof("nobody") - 1;
646 /* goto out; */
647 }
648 len += 4 + (XDR_QUADLEN(owner_grouplen) << 2);
649 }
650 if (iap->ia_valid & ATTR_ATIME_SET)
651 len += 16;
652 else if (iap->ia_valid & ATTR_ATIME)
653 len += 4;
654 if (iap->ia_valid & ATTR_MTIME_SET)
655 len += 16;
656 else if (iap->ia_valid & ATTR_MTIME)
657 len += 4;
658 RESERVE_SPACE(len);
659
660 /*
661 * We write the bitmap length now, but leave the bitmap and the attribute
662 * buffer length to be backfilled at the end of this routine.
663 */
664 WRITE32(2);
665 q = p;
666 p += 3;
667
668 if (iap->ia_valid & ATTR_SIZE) {
669 bmval0 |= FATTR4_WORD0_SIZE;
670 WRITE64(iap->ia_size);
671 }
672 if (iap->ia_valid & ATTR_MODE) {
673 bmval1 |= FATTR4_WORD1_MODE;
Trond Myklebustcf3fff52006-01-03 09:55:53 +0100674 WRITE32(iap->ia_mode & S_IALLUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676 if (iap->ia_valid & ATTR_UID) {
677 bmval1 |= FATTR4_WORD1_OWNER;
678 WRITE32(owner_namelen);
679 WRITEMEM(owner_name, owner_namelen);
680 }
681 if (iap->ia_valid & ATTR_GID) {
682 bmval1 |= FATTR4_WORD1_OWNER_GROUP;
683 WRITE32(owner_grouplen);
684 WRITEMEM(owner_group, owner_grouplen);
685 }
686 if (iap->ia_valid & ATTR_ATIME_SET) {
687 bmval1 |= FATTR4_WORD1_TIME_ACCESS_SET;
688 WRITE32(NFS4_SET_TO_CLIENT_TIME);
689 WRITE32(0);
690 WRITE32(iap->ia_mtime.tv_sec);
691 WRITE32(iap->ia_mtime.tv_nsec);
692 }
693 else if (iap->ia_valid & ATTR_ATIME) {
694 bmval1 |= FATTR4_WORD1_TIME_ACCESS_SET;
695 WRITE32(NFS4_SET_TO_SERVER_TIME);
696 }
697 if (iap->ia_valid & ATTR_MTIME_SET) {
698 bmval1 |= FATTR4_WORD1_TIME_MODIFY_SET;
699 WRITE32(NFS4_SET_TO_CLIENT_TIME);
700 WRITE32(0);
701 WRITE32(iap->ia_mtime.tv_sec);
702 WRITE32(iap->ia_mtime.tv_nsec);
703 }
704 else if (iap->ia_valid & ATTR_MTIME) {
705 bmval1 |= FATTR4_WORD1_TIME_MODIFY_SET;
706 WRITE32(NFS4_SET_TO_SERVER_TIME);
707 }
708
709 /*
710 * Now we backfill the bitmap and the attribute buffer length.
711 */
712 if (len != ((char *)p - (char *)q) + 4) {
Chuck Leverfe82a182007-09-11 18:01:10 -0400713 printk(KERN_ERR "nfs: Attr length error, %u != %Zu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 len, ((char *)p - (char *)q) + 4);
715 BUG();
716 }
717 len = (char *)p - (char *)q - 12;
718 *q++ = htonl(bmval0);
719 *q++ = htonl(bmval1);
720 *q++ = htonl(len);
721
722 status = 0;
723/* out: */
724 return status;
725}
726
727static int encode_access(struct xdr_stream *xdr, u32 access)
728{
Al Viro8687b632006-10-19 23:28:48 -0700729 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 RESERVE_SPACE(8);
732 WRITE32(OP_ACCESS);
733 WRITE32(access);
734
735 return 0;
736}
737
738static int encode_close(struct xdr_stream *xdr, const struct nfs_closeargs *arg)
739{
Al Viro8687b632006-10-19 23:28:48 -0700740 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Trond Myklebust8ae20ab2007-05-14 16:50:45 -0400742 RESERVE_SPACE(8+NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 WRITE32(OP_CLOSE);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700744 WRITE32(arg->seqid->sequence->counter);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -0400745 WRITEMEM(arg->stateid->data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 return 0;
748}
749
750static int encode_commit(struct xdr_stream *xdr, const struct nfs_writeargs *args)
751{
Al Viro8687b632006-10-19 23:28:48 -0700752 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 RESERVE_SPACE(16);
755 WRITE32(OP_COMMIT);
756 WRITE64(args->offset);
757 WRITE32(args->count);
758
759 return 0;
760}
761
762static int encode_create(struct xdr_stream *xdr, const struct nfs4_create_arg *create)
763{
Al Viro8687b632006-10-19 23:28:48 -0700764 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 RESERVE_SPACE(8);
767 WRITE32(OP_CREATE);
768 WRITE32(create->ftype);
769
770 switch (create->ftype) {
771 case NF4LNK:
Chuck Lever94a6d752006-08-22 20:06:23 -0400772 RESERVE_SPACE(4);
773 WRITE32(create->u.symlink.len);
774 xdr_write_pages(xdr, create->u.symlink.pages, 0, create->u.symlink.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 break;
776
777 case NF4BLK: case NF4CHR:
778 RESERVE_SPACE(8);
779 WRITE32(create->u.device.specdata1);
780 WRITE32(create->u.device.specdata2);
781 break;
782
783 default:
784 break;
785 }
786
787 RESERVE_SPACE(4 + create->name->len);
788 WRITE32(create->name->len);
789 WRITEMEM(create->name->name, create->name->len);
790
791 return encode_attrs(xdr, create->attrs, create->server);
792}
793
794static int encode_getattr_one(struct xdr_stream *xdr, uint32_t bitmap)
795{
Al Viro8687b632006-10-19 23:28:48 -0700796 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 RESERVE_SPACE(12);
799 WRITE32(OP_GETATTR);
800 WRITE32(1);
801 WRITE32(bitmap);
802 return 0;
803}
804
805static int encode_getattr_two(struct xdr_stream *xdr, uint32_t bm0, uint32_t bm1)
806{
Al Viro8687b632006-10-19 23:28:48 -0700807 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 RESERVE_SPACE(16);
810 WRITE32(OP_GETATTR);
811 WRITE32(2);
812 WRITE32(bm0);
813 WRITE32(bm1);
814 return 0;
815}
816
817static int encode_getfattr(struct xdr_stream *xdr, const u32* bitmask)
818{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return encode_getattr_two(xdr,
820 bitmask[0] & nfs4_fattr_bitmap[0],
821 bitmask[1] & nfs4_fattr_bitmap[1]);
822}
823
824static int encode_fsinfo(struct xdr_stream *xdr, const u32* bitmask)
825{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return encode_getattr_two(xdr, bitmask[0] & nfs4_fsinfo_bitmap[0],
827 bitmask[1] & nfs4_fsinfo_bitmap[1]);
828}
829
Manoj Naik830b8e32006-06-09 09:34:25 -0400830static int encode_fs_locations(struct xdr_stream *xdr, const u32* bitmask)
831{
832 return encode_getattr_two(xdr,
833 bitmask[0] & nfs4_fs_locations_bitmap[0],
834 bitmask[1] & nfs4_fs_locations_bitmap[1]);
835}
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837static int encode_getfh(struct xdr_stream *xdr)
838{
Al Viro8687b632006-10-19 23:28:48 -0700839 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 RESERVE_SPACE(4);
842 WRITE32(OP_GETFH);
843
844 return 0;
845}
846
847static int encode_link(struct xdr_stream *xdr, const struct qstr *name)
848{
Al Viro8687b632006-10-19 23:28:48 -0700849 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 RESERVE_SPACE(8 + name->len);
852 WRITE32(OP_LINK);
853 WRITE32(name->len);
854 WRITEMEM(name->name, name->len);
855
856 return 0;
857}
858
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100859static inline int nfs4_lock_type(struct file_lock *fl, int block)
860{
861 if ((fl->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) == F_RDLCK)
862 return block ? NFS4_READW_LT : NFS4_READ_LT;
863 return block ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
864}
865
866static inline uint64_t nfs4_lock_length(struct file_lock *fl)
867{
868 if (fl->fl_end == OFFSET_MAX)
869 return ~(uint64_t)0;
870 return fl->fl_end - fl->fl_start + 1;
871}
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873/*
874 * opcode,type,reclaim,offset,length,new_lock_owner = 32
875 * open_seqid,open_stateid,lock_seqid,lock_owner.clientid, lock_owner.id = 40
876 */
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100877static int encode_lock(struct xdr_stream *xdr, const struct nfs_lock_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Al Viro8687b632006-10-19 23:28:48 -0700879 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 RESERVE_SPACE(32);
882 WRITE32(OP_LOCK);
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100883 WRITE32(nfs4_lock_type(args->fl, args->block));
884 WRITE32(args->reclaim);
885 WRITE64(args->fl->fl_start);
886 WRITE64(nfs4_lock_length(args->fl));
887 WRITE32(args->new_lock_owner);
888 if (args->new_lock_owner){
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400889 RESERVE_SPACE(4+NFS4_STATEID_SIZE+32);
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100890 WRITE32(args->open_seqid->sequence->counter);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -0400891 WRITEMEM(args->open_stateid->data, NFS4_STATEID_SIZE);
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100892 WRITE32(args->lock_seqid->sequence->counter);
893 WRITE64(args->lock_owner.clientid);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400894 WRITE32(16);
895 WRITEMEM("lock id:", 8);
896 WRITE64(args->lock_owner.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898 else {
Trond Myklebust8ae20ab2007-05-14 16:50:45 -0400899 RESERVE_SPACE(NFS4_STATEID_SIZE+4);
900 WRITEMEM(args->lock_stateid->data, NFS4_STATEID_SIZE);
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100901 WRITE32(args->lock_seqid->sequence->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903
904 return 0;
905}
906
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100907static int encode_lockt(struct xdr_stream *xdr, const struct nfs_lockt_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Al Viro8687b632006-10-19 23:28:48 -0700909 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400911 RESERVE_SPACE(52);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 WRITE32(OP_LOCKT);
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100913 WRITE32(nfs4_lock_type(args->fl, 0));
914 WRITE64(args->fl->fl_start);
915 WRITE64(nfs4_lock_length(args->fl));
916 WRITE64(args->lock_owner.clientid);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400917 WRITE32(16);
918 WRITEMEM("lock id:", 8);
919 WRITE64(args->lock_owner.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 return 0;
922}
923
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100924static int encode_locku(struct xdr_stream *xdr, const struct nfs_locku_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Al Viro8687b632006-10-19 23:28:48 -0700926 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Trond Myklebust8ae20ab2007-05-14 16:50:45 -0400928 RESERVE_SPACE(12+NFS4_STATEID_SIZE+16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 WRITE32(OP_LOCKU);
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100930 WRITE32(nfs4_lock_type(args->fl, 0));
931 WRITE32(args->seqid->sequence->counter);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -0400932 WRITEMEM(args->stateid->data, NFS4_STATEID_SIZE);
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100933 WRITE64(args->fl->fl_start);
934 WRITE64(nfs4_lock_length(args->fl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 return 0;
937}
938
939static int encode_lookup(struct xdr_stream *xdr, const struct qstr *name)
940{
941 int len = name->len;
Al Viro8687b632006-10-19 23:28:48 -0700942 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 RESERVE_SPACE(8 + len);
945 WRITE32(OP_LOOKUP);
946 WRITE32(len);
947 WRITEMEM(name->name, len);
948
949 return 0;
950}
951
952static void encode_share_access(struct xdr_stream *xdr, int open_flags)
953{
Al Viro8687b632006-10-19 23:28:48 -0700954 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 RESERVE_SPACE(8);
957 switch (open_flags & (FMODE_READ|FMODE_WRITE)) {
958 case FMODE_READ:
959 WRITE32(NFS4_SHARE_ACCESS_READ);
960 break;
961 case FMODE_WRITE:
962 WRITE32(NFS4_SHARE_ACCESS_WRITE);
963 break;
964 case FMODE_READ|FMODE_WRITE:
965 WRITE32(NFS4_SHARE_ACCESS_BOTH);
966 break;
967 default:
968 BUG();
969 }
970 WRITE32(0); /* for linux, share_deny = 0 always */
971}
972
973static inline void encode_openhdr(struct xdr_stream *xdr, const struct nfs_openargs *arg)
974{
Al Viro8687b632006-10-19 23:28:48 -0700975 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 /*
977 * opcode 4, seqid 4, share_access 4, share_deny 4, clientid 8, ownerlen 4,
978 * owner 4 = 32
979 */
980 RESERVE_SPACE(8);
981 WRITE32(OP_OPEN);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700982 WRITE32(arg->seqid->sequence->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 encode_share_access(xdr, arg->open_flags);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400984 RESERVE_SPACE(28);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 WRITE64(arg->clientid);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400986 WRITE32(16);
987 WRITEMEM("open id:", 8);
988 WRITE64(arg->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
991static inline void encode_createmode(struct xdr_stream *xdr, const struct nfs_openargs *arg)
992{
Al Viro8687b632006-10-19 23:28:48 -0700993 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 RESERVE_SPACE(4);
996 switch(arg->open_flags & O_EXCL) {
997 case 0:
998 WRITE32(NFS4_CREATE_UNCHECKED);
999 encode_attrs(xdr, arg->u.attrs, arg->server);
1000 break;
1001 default:
1002 WRITE32(NFS4_CREATE_EXCLUSIVE);
1003 encode_nfs4_verifier(xdr, &arg->u.verifier);
1004 }
1005}
1006
1007static void encode_opentype(struct xdr_stream *xdr, const struct nfs_openargs *arg)
1008{
Al Viro8687b632006-10-19 23:28:48 -07001009 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 RESERVE_SPACE(4);
1012 switch (arg->open_flags & O_CREAT) {
1013 case 0:
1014 WRITE32(NFS4_OPEN_NOCREATE);
1015 break;
1016 default:
1017 BUG_ON(arg->claim != NFS4_OPEN_CLAIM_NULL);
1018 WRITE32(NFS4_OPEN_CREATE);
1019 encode_createmode(xdr, arg);
1020 }
1021}
1022
1023static inline void encode_delegation_type(struct xdr_stream *xdr, int delegation_type)
1024{
Al Viro8687b632006-10-19 23:28:48 -07001025 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 RESERVE_SPACE(4);
1028 switch (delegation_type) {
1029 case 0:
1030 WRITE32(NFS4_OPEN_DELEGATE_NONE);
1031 break;
1032 case FMODE_READ:
1033 WRITE32(NFS4_OPEN_DELEGATE_READ);
1034 break;
1035 case FMODE_WRITE|FMODE_READ:
1036 WRITE32(NFS4_OPEN_DELEGATE_WRITE);
1037 break;
1038 default:
1039 BUG();
1040 }
1041}
1042
1043static inline void encode_claim_null(struct xdr_stream *xdr, const struct qstr *name)
1044{
Al Viro8687b632006-10-19 23:28:48 -07001045 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 RESERVE_SPACE(4);
1048 WRITE32(NFS4_OPEN_CLAIM_NULL);
1049 encode_string(xdr, name->len, name->name);
1050}
1051
1052static inline void encode_claim_previous(struct xdr_stream *xdr, int type)
1053{
Al Viro8687b632006-10-19 23:28:48 -07001054 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 RESERVE_SPACE(4);
1057 WRITE32(NFS4_OPEN_CLAIM_PREVIOUS);
1058 encode_delegation_type(xdr, type);
1059}
1060
1061static inline void encode_claim_delegate_cur(struct xdr_stream *xdr, const struct qstr *name, const nfs4_stateid *stateid)
1062{
Al Viro8687b632006-10-19 23:28:48 -07001063 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001065 RESERVE_SPACE(4+NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 WRITE32(NFS4_OPEN_CLAIM_DELEGATE_CUR);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001067 WRITEMEM(stateid->data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 encode_string(xdr, name->len, name->name);
1069}
1070
1071static int encode_open(struct xdr_stream *xdr, const struct nfs_openargs *arg)
1072{
1073 encode_openhdr(xdr, arg);
1074 encode_opentype(xdr, arg);
1075 switch (arg->claim) {
1076 case NFS4_OPEN_CLAIM_NULL:
1077 encode_claim_null(xdr, arg->name);
1078 break;
1079 case NFS4_OPEN_CLAIM_PREVIOUS:
1080 encode_claim_previous(xdr, arg->u.delegation_type);
1081 break;
1082 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1083 encode_claim_delegate_cur(xdr, arg->name, &arg->u.delegation);
1084 break;
1085 default:
1086 BUG();
1087 }
1088 return 0;
1089}
1090
1091static int encode_open_confirm(struct xdr_stream *xdr, const struct nfs_open_confirmargs *arg)
1092{
Al Viro8687b632006-10-19 23:28:48 -07001093 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001095 RESERVE_SPACE(4+NFS4_STATEID_SIZE+4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 WRITE32(OP_OPEN_CONFIRM);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001097 WRITEMEM(arg->stateid->data, NFS4_STATEID_SIZE);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001098 WRITE32(arg->seqid->sequence->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 return 0;
1101}
1102
1103static int encode_open_downgrade(struct xdr_stream *xdr, const struct nfs_closeargs *arg)
1104{
Al Viro8687b632006-10-19 23:28:48 -07001105 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001107 RESERVE_SPACE(4+NFS4_STATEID_SIZE+4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 WRITE32(OP_OPEN_DOWNGRADE);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001109 WRITEMEM(arg->stateid->data, NFS4_STATEID_SIZE);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001110 WRITE32(arg->seqid->sequence->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 encode_share_access(xdr, arg->open_flags);
1112 return 0;
1113}
1114
1115static int
1116encode_putfh(struct xdr_stream *xdr, const struct nfs_fh *fh)
1117{
1118 int len = fh->size;
Al Viro8687b632006-10-19 23:28:48 -07001119 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 RESERVE_SPACE(8 + len);
1122 WRITE32(OP_PUTFH);
1123 WRITE32(len);
1124 WRITEMEM(fh->data, len);
1125
1126 return 0;
1127}
1128
1129static int encode_putrootfh(struct xdr_stream *xdr)
1130{
Al Viro8687b632006-10-19 23:28:48 -07001131 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 RESERVE_SPACE(4);
1134 WRITE32(OP_PUTROOTFH);
1135
1136 return 0;
1137}
1138
1139static void encode_stateid(struct xdr_stream *xdr, const struct nfs_open_context *ctx)
1140{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 nfs4_stateid stateid;
Al Viro8687b632006-10-19 23:28:48 -07001142 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001144 RESERVE_SPACE(NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (ctx->state != NULL) {
1146 nfs4_copy_stateid(&stateid, ctx->state, ctx->lockowner);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001147 WRITEMEM(stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 } else
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001149 WRITEMEM(zero_stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
1151
1152static int encode_read(struct xdr_stream *xdr, const struct nfs_readargs *args)
1153{
Al Viro8687b632006-10-19 23:28:48 -07001154 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 RESERVE_SPACE(4);
1157 WRITE32(OP_READ);
1158
1159 encode_stateid(xdr, args->context);
1160
1161 RESERVE_SPACE(12);
1162 WRITE64(args->offset);
1163 WRITE32(args->count);
1164
1165 return 0;
1166}
1167
1168static int encode_readdir(struct xdr_stream *xdr, const struct nfs4_readdir_arg *readdir, struct rpc_rqst *req)
1169{
Manoj Naik97d312d2005-06-22 17:16:39 +00001170 uint32_t attrs[2] = {
1171 FATTR4_WORD0_RDATTR_ERROR|FATTR4_WORD0_FILEID,
1172 FATTR4_WORD1_MOUNTED_ON_FILEID,
1173 };
Al Viro8687b632006-10-19 23:28:48 -07001174 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001176 RESERVE_SPACE(12+NFS4_VERIFIER_SIZE+20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 WRITE32(OP_READDIR);
1178 WRITE64(readdir->cookie);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001179 WRITEMEM(readdir->verifier.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 WRITE32(readdir->count >> 1); /* We're not doing readdirplus */
1181 WRITE32(readdir->count);
1182 WRITE32(2);
Manoj Naik97d312d2005-06-22 17:16:39 +00001183 /* Switch to mounted_on_fileid if the server supports it */
1184 if (readdir->bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
1185 attrs[0] &= ~FATTR4_WORD0_FILEID;
1186 else
1187 attrs[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
1188 WRITE32(attrs[0] & readdir->bitmask[0]);
1189 WRITE32(attrs[1] & readdir->bitmask[1]);
Trond Myklebusteadf4592005-06-22 17:16:39 +00001190 dprintk("%s: cookie = %Lu, verifier = 0x%x%x, bitmap = 0x%x%x\n",
1191 __FUNCTION__,
1192 (unsigned long long)readdir->cookie,
1193 ((u32 *)readdir->verifier.data)[0],
1194 ((u32 *)readdir->verifier.data)[1],
1195 attrs[0] & readdir->bitmask[0],
1196 attrs[1] & readdir->bitmask[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return 0;
1199}
1200
1201static int encode_readlink(struct xdr_stream *xdr, const struct nfs4_readlink *readlink, struct rpc_rqst *req)
1202{
Al Viro8687b632006-10-19 23:28:48 -07001203 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 RESERVE_SPACE(4);
1206 WRITE32(OP_READLINK);
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return 0;
1209}
1210
1211static int encode_remove(struct xdr_stream *xdr, const struct qstr *name)
1212{
Al Viro8687b632006-10-19 23:28:48 -07001213 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 RESERVE_SPACE(8 + name->len);
1216 WRITE32(OP_REMOVE);
1217 WRITE32(name->len);
1218 WRITEMEM(name->name, name->len);
1219
1220 return 0;
1221}
1222
1223static int encode_rename(struct xdr_stream *xdr, const struct qstr *oldname, const struct qstr *newname)
1224{
Al Viro8687b632006-10-19 23:28:48 -07001225 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 RESERVE_SPACE(8 + oldname->len);
1228 WRITE32(OP_RENAME);
1229 WRITE32(oldname->len);
1230 WRITEMEM(oldname->name, oldname->len);
1231
1232 RESERVE_SPACE(4 + newname->len);
1233 WRITE32(newname->len);
1234 WRITEMEM(newname->name, newname->len);
1235
1236 return 0;
1237}
1238
David Howellsadfa6f92006-08-22 20:06:08 -04001239static int encode_renew(struct xdr_stream *xdr, const struct nfs_client *client_stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
Al Viro8687b632006-10-19 23:28:48 -07001241 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 RESERVE_SPACE(12);
1244 WRITE32(OP_RENEW);
1245 WRITE64(client_stateid->cl_clientid);
1246
1247 return 0;
1248}
1249
1250static int
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001251encode_restorefh(struct xdr_stream *xdr)
1252{
Al Viro8687b632006-10-19 23:28:48 -07001253 __be32 *p;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001254
1255 RESERVE_SPACE(4);
1256 WRITE32(OP_RESTOREFH);
1257
1258 return 0;
1259}
1260
1261static int
J. Bruce Fields23ec6962005-06-22 17:16:22 +00001262encode_setacl(struct xdr_stream *xdr, struct nfs_setaclargs *arg)
1263{
Al Viro8687b632006-10-19 23:28:48 -07001264 __be32 *p;
J. Bruce Fields23ec6962005-06-22 17:16:22 +00001265
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001266 RESERVE_SPACE(4+NFS4_STATEID_SIZE);
J. Bruce Fields23ec6962005-06-22 17:16:22 +00001267 WRITE32(OP_SETATTR);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001268 WRITEMEM(zero_stateid.data, NFS4_STATEID_SIZE);
J. Bruce Fields23ec6962005-06-22 17:16:22 +00001269 RESERVE_SPACE(2*4);
1270 WRITE32(1);
1271 WRITE32(FATTR4_WORD0_ACL);
1272 if (arg->acl_len % 4)
1273 return -EINVAL;
1274 RESERVE_SPACE(4);
1275 WRITE32(arg->acl_len);
1276 xdr_write_pages(xdr, arg->acl_pages, arg->acl_pgbase, arg->acl_len);
1277 return 0;
1278}
1279
1280static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281encode_savefh(struct xdr_stream *xdr)
1282{
Al Viro8687b632006-10-19 23:28:48 -07001283 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 RESERVE_SPACE(4);
1286 WRITE32(OP_SAVEFH);
1287
1288 return 0;
1289}
1290
1291static int encode_setattr(struct xdr_stream *xdr, const struct nfs_setattrargs *arg, const struct nfs_server *server)
1292{
1293 int status;
Al Viro8687b632006-10-19 23:28:48 -07001294 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001296 RESERVE_SPACE(4+NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 WRITE32(OP_SETATTR);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001298 WRITEMEM(arg->stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 if ((status = encode_attrs(xdr, arg->iap, server)))
1301 return status;
1302
1303 return 0;
1304}
1305
1306static int encode_setclientid(struct xdr_stream *xdr, const struct nfs4_setclientid *setclientid)
1307{
Al Viro8687b632006-10-19 23:28:48 -07001308 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001310 RESERVE_SPACE(4 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 WRITE32(OP_SETCLIENTID);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001312 WRITEMEM(setclientid->sc_verifier->data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 encode_string(xdr, setclientid->sc_name_len, setclientid->sc_name);
1315 RESERVE_SPACE(4);
1316 WRITE32(setclientid->sc_prog);
1317 encode_string(xdr, setclientid->sc_netid_len, setclientid->sc_netid);
1318 encode_string(xdr, setclientid->sc_uaddr_len, setclientid->sc_uaddr);
1319 RESERVE_SPACE(4);
1320 WRITE32(setclientid->sc_cb_ident);
1321
1322 return 0;
1323}
1324
David Howellsadfa6f92006-08-22 20:06:08 -04001325static int encode_setclientid_confirm(struct xdr_stream *xdr, const struct nfs_client *client_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Al Viro8687b632006-10-19 23:28:48 -07001327 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001329 RESERVE_SPACE(12 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 WRITE32(OP_SETCLIENTID_CONFIRM);
1331 WRITE64(client_state->cl_clientid);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001332 WRITEMEM(client_state->cl_confirm.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 return 0;
1335}
1336
1337static int encode_write(struct xdr_stream *xdr, const struct nfs_writeargs *args)
1338{
Al Viro8687b632006-10-19 23:28:48 -07001339 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 RESERVE_SPACE(4);
1342 WRITE32(OP_WRITE);
1343
1344 encode_stateid(xdr, args->context);
1345
1346 RESERVE_SPACE(16);
1347 WRITE64(args->offset);
1348 WRITE32(args->stable);
1349 WRITE32(args->count);
1350
1351 xdr_write_pages(xdr, args->pages, args->pgbase, args->count);
1352
1353 return 0;
1354}
1355
1356static int encode_delegreturn(struct xdr_stream *xdr, const nfs4_stateid *stateid)
1357{
Al Viro8687b632006-10-19 23:28:48 -07001358 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001360 RESERVE_SPACE(4+NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 WRITE32(OP_DELEGRETURN);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001363 WRITEMEM(stateid->data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return 0;
1365
1366}
1367/*
1368 * END OF "GENERIC" ENCODE ROUTINES.
1369 */
1370
1371/*
1372 * Encode an ACCESS request
1373 */
Al Viro8687b632006-10-19 23:28:48 -07001374static int nfs4_xdr_enc_access(struct rpc_rqst *req, __be32 *p, const struct nfs4_accessargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
1376 struct xdr_stream xdr;
1377 struct compound_hdr hdr = {
1378 .nops = 2,
1379 };
1380 int status;
1381
1382 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1383 encode_compound_hdr(&xdr, &hdr);
1384 if ((status = encode_putfh(&xdr, args->fh)) == 0)
1385 status = encode_access(&xdr, args->access);
1386 return status;
1387}
1388
1389/*
1390 * Encode LOOKUP request
1391 */
Al Viro8687b632006-10-19 23:28:48 -07001392static int nfs4_xdr_enc_lookup(struct rpc_rqst *req, __be32 *p, const struct nfs4_lookup_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
1394 struct xdr_stream xdr;
1395 struct compound_hdr hdr = {
1396 .nops = 4,
1397 };
1398 int status;
1399
1400 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1401 encode_compound_hdr(&xdr, &hdr);
1402 if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1403 goto out;
1404 if ((status = encode_lookup(&xdr, args->name)) != 0)
1405 goto out;
1406 if ((status = encode_getfh(&xdr)) != 0)
1407 goto out;
1408 status = encode_getfattr(&xdr, args->bitmask);
1409out:
1410 return status;
1411}
1412
1413/*
1414 * Encode LOOKUP_ROOT request
1415 */
Al Viro8687b632006-10-19 23:28:48 -07001416static int nfs4_xdr_enc_lookup_root(struct rpc_rqst *req, __be32 *p, const struct nfs4_lookup_root_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
1418 struct xdr_stream xdr;
1419 struct compound_hdr hdr = {
1420 .nops = 3,
1421 };
1422 int status;
1423
1424 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1425 encode_compound_hdr(&xdr, &hdr);
1426 if ((status = encode_putrootfh(&xdr)) != 0)
1427 goto out;
1428 if ((status = encode_getfh(&xdr)) == 0)
1429 status = encode_getfattr(&xdr, args->bitmask);
1430out:
1431 return status;
1432}
1433
1434/*
1435 * Encode REMOVE request
1436 */
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04001437static int nfs4_xdr_enc_remove(struct rpc_rqst *req, __be32 *p, const struct nfs_removeargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
1439 struct xdr_stream xdr;
1440 struct compound_hdr hdr = {
Trond Myklebust16e42952005-10-27 22:12:44 -04001441 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 };
1443 int status;
1444
1445 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1446 encode_compound_hdr(&xdr, &hdr);
Trond Myklebust16e42952005-10-27 22:12:44 -04001447 if ((status = encode_putfh(&xdr, args->fh)) != 0)
1448 goto out;
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04001449 if ((status = encode_remove(&xdr, &args->name)) != 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04001450 goto out;
1451 status = encode_getfattr(&xdr, args->bitmask);
1452out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 return status;
1454}
1455
1456/*
1457 * Encode RENAME request
1458 */
Al Viro8687b632006-10-19 23:28:48 -07001459static int nfs4_xdr_enc_rename(struct rpc_rqst *req, __be32 *p, const struct nfs4_rename_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
1461 struct xdr_stream xdr;
1462 struct compound_hdr hdr = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001463 .nops = 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 };
1465 int status;
1466
1467 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1468 encode_compound_hdr(&xdr, &hdr);
1469 if ((status = encode_putfh(&xdr, args->old_dir)) != 0)
1470 goto out;
1471 if ((status = encode_savefh(&xdr)) != 0)
1472 goto out;
1473 if ((status = encode_putfh(&xdr, args->new_dir)) != 0)
1474 goto out;
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001475 if ((status = encode_rename(&xdr, args->old_name, args->new_name)) != 0)
1476 goto out;
1477 if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
1478 goto out;
1479 if ((status = encode_restorefh(&xdr)) != 0)
1480 goto out;
1481 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482out:
1483 return status;
1484}
1485
1486/*
1487 * Encode LINK request
1488 */
Al Viro8687b632006-10-19 23:28:48 -07001489static int nfs4_xdr_enc_link(struct rpc_rqst *req, __be32 *p, const struct nfs4_link_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
1491 struct xdr_stream xdr;
1492 struct compound_hdr hdr = {
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001493 .nops = 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 };
1495 int status;
1496
1497 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1498 encode_compound_hdr(&xdr, &hdr);
1499 if ((status = encode_putfh(&xdr, args->fh)) != 0)
1500 goto out;
1501 if ((status = encode_savefh(&xdr)) != 0)
1502 goto out;
1503 if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1504 goto out;
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001505 if ((status = encode_link(&xdr, args->name)) != 0)
1506 goto out;
1507 if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
1508 goto out;
1509 if ((status = encode_restorefh(&xdr)) != 0)
1510 goto out;
1511 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512out:
1513 return status;
1514}
1515
1516/*
1517 * Encode CREATE request
1518 */
Al Viro8687b632006-10-19 23:28:48 -07001519static int nfs4_xdr_enc_create(struct rpc_rqst *req, __be32 *p, const struct nfs4_create_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
1521 struct xdr_stream xdr;
1522 struct compound_hdr hdr = {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001523 .nops = 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 };
1525 int status;
1526
1527 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1528 encode_compound_hdr(&xdr, &hdr);
1529 if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1530 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001531 if ((status = encode_savefh(&xdr)) != 0)
1532 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 if ((status = encode_create(&xdr, args)) != 0)
1534 goto out;
1535 if ((status = encode_getfh(&xdr)) != 0)
1536 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001537 if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
1538 goto out;
1539 if ((status = encode_restorefh(&xdr)) != 0)
1540 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 status = encode_getfattr(&xdr, args->bitmask);
1542out:
1543 return status;
1544}
1545
1546/*
1547 * Encode SYMLINK request
1548 */
Al Viro8687b632006-10-19 23:28:48 -07001549static int nfs4_xdr_enc_symlink(struct rpc_rqst *req, __be32 *p, const struct nfs4_create_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
1551 return nfs4_xdr_enc_create(req, p, args);
1552}
1553
1554/*
1555 * Encode GETATTR request
1556 */
Al Viro8687b632006-10-19 23:28:48 -07001557static int nfs4_xdr_enc_getattr(struct rpc_rqst *req, __be32 *p, const struct nfs4_getattr_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
1559 struct xdr_stream xdr;
1560 struct compound_hdr hdr = {
1561 .nops = 2,
1562 };
1563 int status;
1564
1565 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1566 encode_compound_hdr(&xdr, &hdr);
1567 if ((status = encode_putfh(&xdr, args->fh)) == 0)
1568 status = encode_getfattr(&xdr, args->bitmask);
1569 return status;
1570}
1571
1572/*
1573 * Encode a CLOSE request
1574 */
Al Viro8687b632006-10-19 23:28:48 -07001575static int nfs4_xdr_enc_close(struct rpc_rqst *req, __be32 *p, struct nfs_closeargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
1577 struct xdr_stream xdr;
1578 struct compound_hdr hdr = {
Trond Myklebust516a6af2005-10-27 22:12:41 -04001579 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 };
1581 int status;
1582
1583 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1584 encode_compound_hdr(&xdr, &hdr);
1585 status = encode_putfh(&xdr, args->fh);
1586 if(status)
1587 goto out;
1588 status = encode_close(&xdr, args);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001589 if (status != 0)
1590 goto out;
1591 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592out:
1593 return status;
1594}
1595
1596/*
1597 * Encode an OPEN request
1598 */
Al Viro8687b632006-10-19 23:28:48 -07001599static int nfs4_xdr_enc_open(struct rpc_rqst *req, __be32 *p, struct nfs_openargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
1601 struct xdr_stream xdr;
1602 struct compound_hdr hdr = {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001603 .nops = 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 };
1605 int status;
1606
1607 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1608 encode_compound_hdr(&xdr, &hdr);
1609 status = encode_putfh(&xdr, args->fh);
1610 if (status)
1611 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001612 status = encode_savefh(&xdr);
1613 if (status)
1614 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 status = encode_open(&xdr, args);
1616 if (status)
1617 goto out;
1618 status = encode_getfh(&xdr);
1619 if (status)
1620 goto out;
1621 status = encode_getfattr(&xdr, args->bitmask);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001622 if (status)
1623 goto out;
1624 status = encode_restorefh(&xdr);
1625 if (status)
1626 goto out;
1627 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628out:
1629 return status;
1630}
1631
1632/*
1633 * Encode an OPEN_CONFIRM request
1634 */
Al Viro8687b632006-10-19 23:28:48 -07001635static int nfs4_xdr_enc_open_confirm(struct rpc_rqst *req, __be32 *p, struct nfs_open_confirmargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
1637 struct xdr_stream xdr;
1638 struct compound_hdr hdr = {
1639 .nops = 2,
1640 };
1641 int status;
1642
1643 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1644 encode_compound_hdr(&xdr, &hdr);
1645 status = encode_putfh(&xdr, args->fh);
1646 if(status)
1647 goto out;
1648 status = encode_open_confirm(&xdr, args);
1649out:
1650 return status;
1651}
1652
1653/*
1654 * Encode an OPEN request with no attributes.
1655 */
Al Viro8687b632006-10-19 23:28:48 -07001656static int nfs4_xdr_enc_open_noattr(struct rpc_rqst *req, __be32 *p, struct nfs_openargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
1658 struct xdr_stream xdr;
1659 struct compound_hdr hdr = {
Trond Myklebust864472e2006-01-03 09:55:15 +01001660 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 };
1662 int status;
1663
1664 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1665 encode_compound_hdr(&xdr, &hdr);
1666 status = encode_putfh(&xdr, args->fh);
1667 if (status)
1668 goto out;
1669 status = encode_open(&xdr, args);
Trond Myklebust864472e2006-01-03 09:55:15 +01001670 if (status)
1671 goto out;
1672 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673out:
1674 return status;
1675}
1676
1677/*
1678 * Encode an OPEN_DOWNGRADE request
1679 */
Al Viro8687b632006-10-19 23:28:48 -07001680static int nfs4_xdr_enc_open_downgrade(struct rpc_rqst *req, __be32 *p, struct nfs_closeargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
1682 struct xdr_stream xdr;
1683 struct compound_hdr hdr = {
Trond Myklebust516a6af2005-10-27 22:12:41 -04001684 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 };
1686 int status;
1687
1688 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1689 encode_compound_hdr(&xdr, &hdr);
1690 status = encode_putfh(&xdr, args->fh);
1691 if (status)
1692 goto out;
1693 status = encode_open_downgrade(&xdr, args);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001694 if (status != 0)
1695 goto out;
1696 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697out:
1698 return status;
1699}
1700
1701/*
1702 * Encode a LOCK request
1703 */
Al Viro8687b632006-10-19 23:28:48 -07001704static int nfs4_xdr_enc_lock(struct rpc_rqst *req, __be32 *p, struct nfs_lock_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
1706 struct xdr_stream xdr;
1707 struct compound_hdr hdr = {
1708 .nops = 2,
1709 };
1710 int status;
1711
1712 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1713 encode_compound_hdr(&xdr, &hdr);
1714 status = encode_putfh(&xdr, args->fh);
1715 if(status)
1716 goto out;
1717 status = encode_lock(&xdr, args);
1718out:
1719 return status;
1720}
1721
1722/*
1723 * Encode a LOCKT request
1724 */
Al Viro8687b632006-10-19 23:28:48 -07001725static int nfs4_xdr_enc_lockt(struct rpc_rqst *req, __be32 *p, struct nfs_lockt_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
1727 struct xdr_stream xdr;
1728 struct compound_hdr hdr = {
1729 .nops = 2,
1730 };
1731 int status;
1732
1733 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1734 encode_compound_hdr(&xdr, &hdr);
1735 status = encode_putfh(&xdr, args->fh);
1736 if(status)
1737 goto out;
1738 status = encode_lockt(&xdr, args);
1739out:
1740 return status;
1741}
1742
1743/*
1744 * Encode a LOCKU request
1745 */
Al Viro8687b632006-10-19 23:28:48 -07001746static int nfs4_xdr_enc_locku(struct rpc_rqst *req, __be32 *p, struct nfs_locku_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
1748 struct xdr_stream xdr;
1749 struct compound_hdr hdr = {
1750 .nops = 2,
1751 };
1752 int status;
1753
1754 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1755 encode_compound_hdr(&xdr, &hdr);
1756 status = encode_putfh(&xdr, args->fh);
1757 if(status)
1758 goto out;
1759 status = encode_locku(&xdr, args);
1760out:
1761 return status;
1762}
1763
1764/*
1765 * Encode a READLINK request
1766 */
Al Viro8687b632006-10-19 23:28:48 -07001767static int nfs4_xdr_enc_readlink(struct rpc_rqst *req, __be32 *p, const struct nfs4_readlink *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
1769 struct xdr_stream xdr;
1770 struct compound_hdr hdr = {
1771 .nops = 2,
1772 };
Trond Myklebuste3a535e2007-07-19 10:03:38 -04001773 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
1774 unsigned int replen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 int status;
1776
1777 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1778 encode_compound_hdr(&xdr, &hdr);
1779 status = encode_putfh(&xdr, args->fh);
1780 if(status)
1781 goto out;
1782 status = encode_readlink(&xdr, args, req);
Trond Myklebuste3a535e2007-07-19 10:03:38 -04001783
1784 /* set up reply kvec
1785 * toplevel_status + taglen + rescount + OP_PUTFH + status
1786 * + OP_READLINK + status + string length = 8
1787 */
1788 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_readlink_sz) << 2;
1789 xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages,
1790 args->pgbase, args->pglen);
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792out:
1793 return status;
1794}
1795
1796/*
1797 * Encode a READDIR request
1798 */
Al Viro8687b632006-10-19 23:28:48 -07001799static int nfs4_xdr_enc_readdir(struct rpc_rqst *req, __be32 *p, const struct nfs4_readdir_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
1801 struct xdr_stream xdr;
1802 struct compound_hdr hdr = {
1803 .nops = 2,
1804 };
Trond Myklebustd6ac02d2007-07-19 10:03:37 -04001805 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
1806 int replen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 int status;
1808
1809 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1810 encode_compound_hdr(&xdr, &hdr);
1811 status = encode_putfh(&xdr, args->fh);
1812 if(status)
1813 goto out;
1814 status = encode_readdir(&xdr, args, req);
Trond Myklebustd6ac02d2007-07-19 10:03:37 -04001815
1816 /* set up reply kvec
1817 * toplevel_status + taglen + rescount + OP_PUTFH + status
1818 * + OP_READDIR + status + verifer(2) = 9
1819 */
1820 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_readdir_sz) << 2;
1821 xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages,
1822 args->pgbase, args->count);
1823 dprintk("%s: inlined page args = (%u, %p, %u, %u)\n",
1824 __FUNCTION__, replen, args->pages,
1825 args->pgbase, args->count);
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827out:
1828 return status;
1829}
1830
1831/*
1832 * Encode a READ request
1833 */
Al Viro8687b632006-10-19 23:28:48 -07001834static int nfs4_xdr_enc_read(struct rpc_rqst *req, __be32 *p, struct nfs_readargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
Trond Myklebust1be27f32007-06-27 14:29:04 -04001836 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 struct xdr_stream xdr;
1838 struct compound_hdr hdr = {
1839 .nops = 2,
1840 };
1841 int replen, status;
1842
1843 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1844 encode_compound_hdr(&xdr, &hdr);
1845 status = encode_putfh(&xdr, args->fh);
1846 if (status)
1847 goto out;
1848 status = encode_read(&xdr, args);
1849 if (status)
1850 goto out;
1851
1852 /* set up reply kvec
1853 * toplevel status + taglen=0 + rescount + OP_PUTFH + status
1854 * + OP_READ + status + eof + datalen = 9
1855 */
1856 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_read_sz) << 2;
1857 xdr_inline_pages(&req->rq_rcv_buf, replen,
1858 args->pages, args->pgbase, args->count);
\"Talpey, Thomas\4f22ccc2007-09-10 13:44:58 -04001859 req->rq_rcv_buf.flags |= XDRBUF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860out:
1861 return status;
1862}
1863
1864/*
1865 * Encode an SETATTR request
1866 */
Al Viro8687b632006-10-19 23:28:48 -07001867static int nfs4_xdr_enc_setattr(struct rpc_rqst *req, __be32 *p, struct nfs_setattrargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869{
1870 struct xdr_stream xdr;
1871 struct compound_hdr hdr = {
1872 .nops = 3,
1873 };
1874 int status;
1875
1876 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1877 encode_compound_hdr(&xdr, &hdr);
1878 status = encode_putfh(&xdr, args->fh);
1879 if(status)
1880 goto out;
1881 status = encode_setattr(&xdr, args, args->server);
1882 if(status)
1883 goto out;
1884 status = encode_getfattr(&xdr, args->bitmask);
1885out:
1886 return status;
1887}
1888
1889/*
J. Bruce Fields029d1052005-06-22 17:16:22 +00001890 * Encode a GETACL request
1891 */
1892static int
Al Viro8687b632006-10-19 23:28:48 -07001893nfs4_xdr_enc_getacl(struct rpc_rqst *req, __be32 *p,
J. Bruce Fields029d1052005-06-22 17:16:22 +00001894 struct nfs_getaclargs *args)
1895{
1896 struct xdr_stream xdr;
Trond Myklebust1be27f32007-06-27 14:29:04 -04001897 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
J. Bruce Fields029d1052005-06-22 17:16:22 +00001898 struct compound_hdr hdr = {
1899 .nops = 2,
1900 };
1901 int replen, status;
1902
1903 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1904 encode_compound_hdr(&xdr, &hdr);
1905 status = encode_putfh(&xdr, args->fh);
1906 if (status)
1907 goto out;
1908 status = encode_getattr_two(&xdr, FATTR4_WORD0_ACL, 0);
1909 /* set up reply buffer: */
1910 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_getacl_sz) << 2;
1911 xdr_inline_pages(&req->rq_rcv_buf, replen,
1912 args->acl_pages, args->acl_pgbase, args->acl_len);
1913out:
1914 return status;
1915}
1916
1917/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 * Encode a WRITE request
1919 */
Al Viro8687b632006-10-19 23:28:48 -07001920static int nfs4_xdr_enc_write(struct rpc_rqst *req, __be32 *p, struct nfs_writeargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
1922 struct xdr_stream xdr;
1923 struct compound_hdr hdr = {
Trond Myklebust4f9838c2005-10-27 22:12:44 -04001924 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 };
1926 int status;
1927
1928 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1929 encode_compound_hdr(&xdr, &hdr);
1930 status = encode_putfh(&xdr, args->fh);
1931 if (status)
1932 goto out;
1933 status = encode_write(&xdr, args);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04001934 if (status)
1935 goto out;
\"Talpey, Thomas\4f22ccc2007-09-10 13:44:58 -04001936 req->rq_snd_buf.flags |= XDRBUF_WRITE;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04001937 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938out:
1939 return status;
1940}
1941
1942/*
1943 * a COMMIT request
1944 */
Al Viro8687b632006-10-19 23:28:48 -07001945static int nfs4_xdr_enc_commit(struct rpc_rqst *req, __be32 *p, struct nfs_writeargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
1947 struct xdr_stream xdr;
1948 struct compound_hdr hdr = {
Trond Myklebust4f9838c2005-10-27 22:12:44 -04001949 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 };
1951 int status;
1952
1953 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1954 encode_compound_hdr(&xdr, &hdr);
1955 status = encode_putfh(&xdr, args->fh);
1956 if (status)
1957 goto out;
1958 status = encode_commit(&xdr, args);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04001959 if (status)
1960 goto out;
1961 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962out:
1963 return status;
1964}
1965
1966/*
1967 * FSINFO request
1968 */
Al Viro8687b632006-10-19 23:28:48 -07001969static int nfs4_xdr_enc_fsinfo(struct rpc_rqst *req, __be32 *p, struct nfs4_fsinfo_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
1971 struct xdr_stream xdr;
1972 struct compound_hdr hdr = {
1973 .nops = 2,
1974 };
1975 int status;
1976
1977 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1978 encode_compound_hdr(&xdr, &hdr);
1979 status = encode_putfh(&xdr, args->fh);
1980 if (!status)
1981 status = encode_fsinfo(&xdr, args->bitmask);
1982 return status;
1983}
1984
1985/*
1986 * a PATHCONF request
1987 */
Al Viro8687b632006-10-19 23:28:48 -07001988static int nfs4_xdr_enc_pathconf(struct rpc_rqst *req, __be32 *p, const struct nfs4_pathconf_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 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 status = encode_putfh(&xdr, args->fh);
1999 if (!status)
2000 status = encode_getattr_one(&xdr,
2001 args->bitmask[0] & nfs4_pathconf_bitmap[0]);
2002 return status;
2003}
2004
2005/*
2006 * a STATFS request
2007 */
Al Viro8687b632006-10-19 23:28:48 -07002008static int nfs4_xdr_enc_statfs(struct rpc_rqst *req, __be32 *p, const struct nfs4_statfs_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 struct xdr_stream xdr;
2011 struct compound_hdr hdr = {
2012 .nops = 2,
2013 };
2014 int status;
2015
2016 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2017 encode_compound_hdr(&xdr, &hdr);
2018 status = encode_putfh(&xdr, args->fh);
2019 if (status == 0)
2020 status = encode_getattr_two(&xdr,
2021 args->bitmask[0] & nfs4_statfs_bitmap[0],
2022 args->bitmask[1] & nfs4_statfs_bitmap[1]);
2023 return status;
2024}
2025
2026/*
2027 * GETATTR_BITMAP request
2028 */
Al Viro8687b632006-10-19 23:28:48 -07002029static int nfs4_xdr_enc_server_caps(struct rpc_rqst *req, __be32 *p, const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
2031 struct xdr_stream xdr;
2032 struct compound_hdr hdr = {
2033 .nops = 2,
2034 };
2035 int status;
2036
2037 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2038 encode_compound_hdr(&xdr, &hdr);
2039 status = encode_putfh(&xdr, fhandle);
2040 if (status == 0)
2041 status = encode_getattr_one(&xdr, FATTR4_WORD0_SUPPORTED_ATTRS|
2042 FATTR4_WORD0_LINK_SUPPORT|
2043 FATTR4_WORD0_SYMLINK_SUPPORT|
2044 FATTR4_WORD0_ACLSUPPORT);
2045 return status;
2046}
2047
2048/*
2049 * a RENEW request
2050 */
Al Viro8687b632006-10-19 23:28:48 -07002051static int nfs4_xdr_enc_renew(struct rpc_rqst *req, __be32 *p, struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
2053 struct xdr_stream xdr;
2054 struct compound_hdr hdr = {
2055 .nops = 1,
2056 };
2057
2058 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2059 encode_compound_hdr(&xdr, &hdr);
2060 return encode_renew(&xdr, clp);
2061}
2062
2063/*
2064 * a SETCLIENTID request
2065 */
Al Viro8687b632006-10-19 23:28:48 -07002066static int nfs4_xdr_enc_setclientid(struct rpc_rqst *req, __be32 *p, struct nfs4_setclientid *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
2068 struct xdr_stream xdr;
2069 struct compound_hdr hdr = {
2070 .nops = 1,
2071 };
2072
2073 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2074 encode_compound_hdr(&xdr, &hdr);
2075 return encode_setclientid(&xdr, sc);
2076}
2077
2078/*
2079 * a SETCLIENTID_CONFIRM request
2080 */
Al Viro8687b632006-10-19 23:28:48 -07002081static int nfs4_xdr_enc_setclientid_confirm(struct rpc_rqst *req, __be32 *p, struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{
2083 struct xdr_stream xdr;
2084 struct compound_hdr hdr = {
2085 .nops = 3,
2086 };
2087 const u32 lease_bitmap[2] = { FATTR4_WORD0_LEASE_TIME, 0 };
2088 int status;
2089
2090 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2091 encode_compound_hdr(&xdr, &hdr);
2092 status = encode_setclientid_confirm(&xdr, clp);
2093 if (!status)
2094 status = encode_putrootfh(&xdr);
2095 if (!status)
2096 status = encode_fsinfo(&xdr, lease_bitmap);
2097 return status;
2098}
2099
2100/*
2101 * DELEGRETURN request
2102 */
Al Viro8687b632006-10-19 23:28:48 -07002103static int nfs4_xdr_enc_delegreturn(struct rpc_rqst *req, __be32 *p, const struct nfs4_delegreturnargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
2105 struct xdr_stream xdr;
2106 struct compound_hdr hdr = {
Trond Myklebustfa178f22006-01-03 09:55:38 +01002107 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 };
2109 int status;
2110
2111 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2112 encode_compound_hdr(&xdr, &hdr);
Trond Myklebustfa178f22006-01-03 09:55:38 +01002113 status = encode_putfh(&xdr, args->fhandle);
2114 if (status != 0)
2115 goto out;
2116 status = encode_delegreturn(&xdr, args->stateid);
2117 if (status != 0)
2118 goto out;
2119 status = encode_getfattr(&xdr, args->bitmask);
2120out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 return status;
2122}
2123
2124/*
Trond Myklebust683b57b2006-06-09 09:34:22 -04002125 * Encode FS_LOCATIONS request
2126 */
Al Viro8687b632006-10-19 23:28:48 -07002127static int nfs4_xdr_enc_fs_locations(struct rpc_rqst *req, __be32 *p, struct nfs4_fs_locations_arg *args)
Trond Myklebust683b57b2006-06-09 09:34:22 -04002128{
2129 struct xdr_stream xdr;
2130 struct compound_hdr hdr = {
2131 .nops = 3,
2132 };
Trond Myklebust1be27f32007-06-27 14:29:04 -04002133 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
Trond Myklebust683b57b2006-06-09 09:34:22 -04002134 int replen;
2135 int status;
2136
2137 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2138 encode_compound_hdr(&xdr, &hdr);
2139 if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
2140 goto out;
2141 if ((status = encode_lookup(&xdr, args->name)) != 0)
2142 goto out;
Manoj Naik830b8e32006-06-09 09:34:25 -04002143 if ((status = encode_fs_locations(&xdr, args->bitmask)) != 0)
Trond Myklebust683b57b2006-06-09 09:34:22 -04002144 goto out;
2145 /* set up reply
Manoj Naik830b8e32006-06-09 09:34:25 -04002146 * toplevel_status + OP_PUTFH + status
Trond Myklebust683b57b2006-06-09 09:34:22 -04002147 * + OP_LOOKUP + status + OP_GETATTR + status = 7
2148 */
2149 replen = (RPC_REPHDRSIZE + auth->au_rslack + 7) << 2;
2150 xdr_inline_pages(&req->rq_rcv_buf, replen, &args->page,
2151 0, PAGE_SIZE);
2152out:
2153 return status;
2154}
2155
2156/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 * START OF "GENERIC" DECODE ROUTINES.
2158 * These may look a little ugly since they are imported from a "generic"
2159 * set of XDR encode/decode routines which are intended to be shared by
2160 * all of our NFSv4 implementations (OpenBSD, MacOS X...).
2161 *
2162 * If the pain of reading these is too great, it should be a straightforward
2163 * task to translate them into Linux-specific versions which are more
2164 * consistent with the style used in NFSv2/v3...
2165 */
2166#define READ32(x) (x) = ntohl(*p++)
2167#define READ64(x) do { \
2168 (x) = (u64)ntohl(*p++) << 32; \
2169 (x) |= ntohl(*p++); \
2170} while (0)
2171#define READTIME(x) do { \
2172 p++; \
2173 (x.tv_sec) = ntohl(*p++); \
2174 (x.tv_nsec) = ntohl(*p++); \
2175} while (0)
2176#define COPYMEM(x,nbytes) do { \
2177 memcpy((x), p, nbytes); \
2178 p += XDR_QUADLEN(nbytes); \
2179} while (0)
2180
2181#define READ_BUF(nbytes) do { \
2182 p = xdr_inline_decode(xdr, nbytes); \
Chuck Levere4cc6ee2007-05-08 18:23:28 -04002183 if (unlikely(!p)) { \
Chuck Leverfe82a182007-09-11 18:01:10 -04002184 dprintk("nfs: %s: prematurely hit end of receive" \
Chuck Levere4cc6ee2007-05-08 18:23:28 -04002185 " buffer\n", __FUNCTION__); \
Chuck Leverfe82a182007-09-11 18:01:10 -04002186 dprintk("nfs: %s: xdr->p=%p, bytes=%u, xdr->end=%p\n", \
Chuck Levere4cc6ee2007-05-08 18:23:28 -04002187 __FUNCTION__, xdr->p, nbytes, xdr->end); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 return -EIO; \
2189 } \
2190} while (0)
2191
Trond Myklebust683b57b2006-06-09 09:34:22 -04002192static int decode_opaque_inline(struct xdr_stream *xdr, unsigned int *len, char **string)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
Al Viro8687b632006-10-19 23:28:48 -07002194 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196 READ_BUF(4);
2197 READ32(*len);
2198 READ_BUF(*len);
2199 *string = (char *)p;
2200 return 0;
2201}
2202
2203static int decode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
2204{
Al Viro8687b632006-10-19 23:28:48 -07002205 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
2207 READ_BUF(8);
2208 READ32(hdr->status);
2209 READ32(hdr->taglen);
2210
2211 READ_BUF(hdr->taglen + 4);
2212 hdr->tag = (char *)p;
2213 p += XDR_QUADLEN(hdr->taglen);
2214 READ32(hdr->nops);
2215 return 0;
2216}
2217
2218static int decode_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
2219{
Al Viro8687b632006-10-19 23:28:48 -07002220 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 uint32_t opnum;
2222 int32_t nfserr;
2223
2224 READ_BUF(8);
2225 READ32(opnum);
2226 if (opnum != expected) {
Chuck Leverfe82a182007-09-11 18:01:10 -04002227 dprintk("nfs: Server returned operation"
2228 " %d but we issued a request for %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 opnum, expected);
2230 return -EIO;
2231 }
2232 READ32(nfserr);
2233 if (nfserr != NFS_OK)
David Howells0a8ea432006-08-22 20:06:08 -04002234 return -nfs4_stat_to_errno(nfserr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 return 0;
2236}
2237
2238/* Dummy routine */
David Howellsadfa6f92006-08-22 20:06:08 -04002239static int decode_ace(struct xdr_stream *xdr, void *ace, struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240{
Al Viro8687b632006-10-19 23:28:48 -07002241 __be32 *p;
Trond Myklebust683b57b2006-06-09 09:34:22 -04002242 unsigned int strlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 char *str;
2244
2245 READ_BUF(12);
2246 return decode_opaque_inline(xdr, &strlen, &str);
2247}
2248
2249static int decode_attr_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
2250{
Al Viro8687b632006-10-19 23:28:48 -07002251 uint32_t bmlen;
2252 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
2254 READ_BUF(4);
2255 READ32(bmlen);
2256
2257 bitmap[0] = bitmap[1] = 0;
2258 READ_BUF((bmlen << 2));
2259 if (bmlen > 0) {
2260 READ32(bitmap[0]);
2261 if (bmlen > 1)
2262 READ32(bitmap[1]);
2263 }
2264 return 0;
2265}
2266
Al Viro8687b632006-10-19 23:28:48 -07002267static inline int decode_attr_length(struct xdr_stream *xdr, uint32_t *attrlen, __be32 **savep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268{
Al Viro8687b632006-10-19 23:28:48 -07002269 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
2271 READ_BUF(4);
2272 READ32(*attrlen);
2273 *savep = xdr->p;
2274 return 0;
2275}
2276
2277static int decode_attr_supported(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *bitmask)
2278{
2279 if (likely(bitmap[0] & FATTR4_WORD0_SUPPORTED_ATTRS)) {
2280 decode_attr_bitmap(xdr, bitmask);
2281 bitmap[0] &= ~FATTR4_WORD0_SUPPORTED_ATTRS;
2282 } else
2283 bitmask[0] = bitmask[1] = 0;
2284 dprintk("%s: bitmask=0x%x%x\n", __FUNCTION__, bitmask[0], bitmask[1]);
2285 return 0;
2286}
2287
2288static int decode_attr_type(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *type)
2289{
Al Viro8687b632006-10-19 23:28:48 -07002290 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
2292 *type = 0;
2293 if (unlikely(bitmap[0] & (FATTR4_WORD0_TYPE - 1U)))
2294 return -EIO;
2295 if (likely(bitmap[0] & FATTR4_WORD0_TYPE)) {
2296 READ_BUF(4);
2297 READ32(*type);
2298 if (*type < NF4REG || *type > NF4NAMEDATTR) {
2299 dprintk("%s: bad type %d\n", __FUNCTION__, *type);
2300 return -EIO;
2301 }
2302 bitmap[0] &= ~FATTR4_WORD0_TYPE;
2303 }
2304 dprintk("%s: type=0%o\n", __FUNCTION__, nfs_type2fmt[*type].nfs2type);
2305 return 0;
2306}
2307
2308static int decode_attr_change(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *change)
2309{
Al Viro8687b632006-10-19 23:28:48 -07002310 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
2312 *change = 0;
2313 if (unlikely(bitmap[0] & (FATTR4_WORD0_CHANGE - 1U)))
2314 return -EIO;
2315 if (likely(bitmap[0] & FATTR4_WORD0_CHANGE)) {
2316 READ_BUF(8);
2317 READ64(*change);
2318 bitmap[0] &= ~FATTR4_WORD0_CHANGE;
2319 }
2320 dprintk("%s: change attribute=%Lu\n", __FUNCTION__,
2321 (unsigned long long)*change);
2322 return 0;
2323}
2324
2325static int decode_attr_size(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *size)
2326{
Al Viro8687b632006-10-19 23:28:48 -07002327 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
2329 *size = 0;
2330 if (unlikely(bitmap[0] & (FATTR4_WORD0_SIZE - 1U)))
2331 return -EIO;
2332 if (likely(bitmap[0] & FATTR4_WORD0_SIZE)) {
2333 READ_BUF(8);
2334 READ64(*size);
2335 bitmap[0] &= ~FATTR4_WORD0_SIZE;
2336 }
2337 dprintk("%s: file size=%Lu\n", __FUNCTION__, (unsigned long long)*size);
2338 return 0;
2339}
2340
2341static int decode_attr_link_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2342{
Al Viro8687b632006-10-19 23:28:48 -07002343 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
2345 *res = 0;
2346 if (unlikely(bitmap[0] & (FATTR4_WORD0_LINK_SUPPORT - 1U)))
2347 return -EIO;
2348 if (likely(bitmap[0] & FATTR4_WORD0_LINK_SUPPORT)) {
2349 READ_BUF(4);
2350 READ32(*res);
2351 bitmap[0] &= ~FATTR4_WORD0_LINK_SUPPORT;
2352 }
2353 dprintk("%s: link support=%s\n", __FUNCTION__, *res == 0 ? "false" : "true");
2354 return 0;
2355}
2356
2357static int decode_attr_symlink_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2358{
Al Viro8687b632006-10-19 23:28:48 -07002359 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
2361 *res = 0;
2362 if (unlikely(bitmap[0] & (FATTR4_WORD0_SYMLINK_SUPPORT - 1U)))
2363 return -EIO;
2364 if (likely(bitmap[0] & FATTR4_WORD0_SYMLINK_SUPPORT)) {
2365 READ_BUF(4);
2366 READ32(*res);
2367 bitmap[0] &= ~FATTR4_WORD0_SYMLINK_SUPPORT;
2368 }
2369 dprintk("%s: symlink support=%s\n", __FUNCTION__, *res == 0 ? "false" : "true");
2370 return 0;
2371}
2372
Trond Myklebust8b4bdcf2006-06-09 09:34:19 -04002373static int decode_attr_fsid(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs_fsid *fsid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374{
Al Viro8687b632006-10-19 23:28:48 -07002375 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
2377 fsid->major = 0;
2378 fsid->minor = 0;
2379 if (unlikely(bitmap[0] & (FATTR4_WORD0_FSID - 1U)))
2380 return -EIO;
2381 if (likely(bitmap[0] & FATTR4_WORD0_FSID)) {
2382 READ_BUF(16);
2383 READ64(fsid->major);
2384 READ64(fsid->minor);
2385 bitmap[0] &= ~FATTR4_WORD0_FSID;
2386 }
2387 dprintk("%s: fsid=(0x%Lx/0x%Lx)\n", __FUNCTION__,
2388 (unsigned long long)fsid->major,
2389 (unsigned long long)fsid->minor);
2390 return 0;
2391}
2392
2393static int decode_attr_lease_time(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2394{
Al Viro8687b632006-10-19 23:28:48 -07002395 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
2397 *res = 60;
2398 if (unlikely(bitmap[0] & (FATTR4_WORD0_LEASE_TIME - 1U)))
2399 return -EIO;
2400 if (likely(bitmap[0] & FATTR4_WORD0_LEASE_TIME)) {
2401 READ_BUF(4);
2402 READ32(*res);
2403 bitmap[0] &= ~FATTR4_WORD0_LEASE_TIME;
2404 }
2405 dprintk("%s: file size=%u\n", __FUNCTION__, (unsigned int)*res);
2406 return 0;
2407}
2408
2409static int decode_attr_aclsupport(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2410{
Al Viro8687b632006-10-19 23:28:48 -07002411 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
2413 *res = ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL;
2414 if (unlikely(bitmap[0] & (FATTR4_WORD0_ACLSUPPORT - 1U)))
2415 return -EIO;
2416 if (likely(bitmap[0] & FATTR4_WORD0_ACLSUPPORT)) {
2417 READ_BUF(4);
2418 READ32(*res);
2419 bitmap[0] &= ~FATTR4_WORD0_ACLSUPPORT;
2420 }
2421 dprintk("%s: ACLs supported=%u\n", __FUNCTION__, (unsigned int)*res);
2422 return 0;
2423}
2424
2425static int decode_attr_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
2426{
Al Viro8687b632006-10-19 23:28:48 -07002427 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
2429 *fileid = 0;
2430 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILEID - 1U)))
2431 return -EIO;
2432 if (likely(bitmap[0] & FATTR4_WORD0_FILEID)) {
2433 READ_BUF(8);
2434 READ64(*fileid);
2435 bitmap[0] &= ~FATTR4_WORD0_FILEID;
2436 }
2437 dprintk("%s: fileid=%Lu\n", __FUNCTION__, (unsigned long long)*fileid);
2438 return 0;
2439}
2440
Manoj Naik99baf622006-06-09 09:34:24 -04002441static int decode_attr_mounted_on_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
2442{
Al Viro8687b632006-10-19 23:28:48 -07002443 __be32 *p;
Manoj Naik99baf622006-06-09 09:34:24 -04002444
2445 *fileid = 0;
2446 if (unlikely(bitmap[1] & (FATTR4_WORD1_MOUNTED_ON_FILEID - 1U)))
2447 return -EIO;
2448 if (likely(bitmap[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)) {
2449 READ_BUF(8);
2450 READ64(*fileid);
2451 bitmap[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
2452 }
2453 dprintk("%s: fileid=%Lu\n", __FUNCTION__, (unsigned long long)*fileid);
2454 return 0;
2455}
2456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457static int decode_attr_files_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2458{
Al Viro8687b632006-10-19 23:28:48 -07002459 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 int status = 0;
2461
2462 *res = 0;
2463 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_AVAIL - 1U)))
2464 return -EIO;
2465 if (likely(bitmap[0] & FATTR4_WORD0_FILES_AVAIL)) {
2466 READ_BUF(8);
2467 READ64(*res);
2468 bitmap[0] &= ~FATTR4_WORD0_FILES_AVAIL;
2469 }
2470 dprintk("%s: files avail=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2471 return status;
2472}
2473
2474static int decode_attr_files_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2475{
Al Viro8687b632006-10-19 23:28:48 -07002476 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 int status = 0;
2478
2479 *res = 0;
2480 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_FREE - 1U)))
2481 return -EIO;
2482 if (likely(bitmap[0] & FATTR4_WORD0_FILES_FREE)) {
2483 READ_BUF(8);
2484 READ64(*res);
2485 bitmap[0] &= ~FATTR4_WORD0_FILES_FREE;
2486 }
2487 dprintk("%s: files free=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2488 return status;
2489}
2490
2491static int decode_attr_files_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2492{
Al Viro8687b632006-10-19 23:28:48 -07002493 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 int status = 0;
2495
2496 *res = 0;
2497 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_TOTAL - 1U)))
2498 return -EIO;
2499 if (likely(bitmap[0] & FATTR4_WORD0_FILES_TOTAL)) {
2500 READ_BUF(8);
2501 READ64(*res);
2502 bitmap[0] &= ~FATTR4_WORD0_FILES_TOTAL;
2503 }
2504 dprintk("%s: files total=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2505 return status;
2506}
2507
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002508static int decode_pathname(struct xdr_stream *xdr, struct nfs4_pathname *path)
2509{
2510 int n;
Al Viro8687b632006-10-19 23:28:48 -07002511 __be32 *p;
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002512 int status = 0;
2513
2514 READ_BUF(4);
2515 READ32(n);
Andy Adamson33a43f22006-06-09 09:34:30 -04002516 if (n < 0)
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002517 goto out_eio;
Andy Adamson33a43f22006-06-09 09:34:30 -04002518 if (n == 0)
2519 goto root_path;
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002520 dprintk("path ");
2521 path->ncomponents = 0;
2522 while (path->ncomponents < n) {
2523 struct nfs4_string *component = &path->components[path->ncomponents];
2524 status = decode_opaque_inline(xdr, &component->len, &component->data);
2525 if (unlikely(status != 0))
2526 goto out_eio;
2527 if (path->ncomponents != n)
2528 dprintk("/");
2529 dprintk("%s", component->data);
2530 if (path->ncomponents < NFS4_PATHNAME_MAXCOMPONENTS)
2531 path->ncomponents++;
2532 else {
2533 dprintk("cannot parse %d components in path\n", n);
2534 goto out_eio;
2535 }
2536 }
2537out:
2538 dprintk("\n");
2539 return status;
Andy Adamson33a43f22006-06-09 09:34:30 -04002540root_path:
2541/* a root pathname is sent as a zero component4 */
2542 path->ncomponents = 1;
2543 path->components[0].len=0;
2544 path->components[0].data=NULL;
2545 dprintk("path /\n");
2546 goto out;
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002547out_eio:
2548 dprintk(" status %d", status);
2549 status = -EIO;
2550 goto out;
2551}
2552
2553static int decode_attr_fs_locations(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_fs_locations *res)
Trond Myklebust683b57b2006-06-09 09:34:22 -04002554{
2555 int n;
Al Viro8687b632006-10-19 23:28:48 -07002556 __be32 *p;
Trond Myklebust683b57b2006-06-09 09:34:22 -04002557 int status = -EIO;
2558
2559 if (unlikely(bitmap[0] & (FATTR4_WORD0_FS_LOCATIONS -1U)))
2560 goto out;
2561 status = 0;
2562 if (unlikely(!(bitmap[0] & FATTR4_WORD0_FS_LOCATIONS)))
2563 goto out;
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002564 dprintk("%s: fsroot ", __FUNCTION__);
2565 status = decode_pathname(xdr, &res->fs_path);
Trond Myklebust683b57b2006-06-09 09:34:22 -04002566 if (unlikely(status != 0))
2567 goto out;
2568 READ_BUF(4);
2569 READ32(n);
2570 if (n <= 0)
2571 goto out_eio;
2572 res->nlocations = 0;
2573 while (res->nlocations < n) {
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002574 int m;
2575 struct nfs4_fs_location *loc = &res->locations[res->nlocations];
Trond Myklebust683b57b2006-06-09 09:34:22 -04002576
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002577 READ_BUF(4);
2578 READ32(m);
2579 if (m <= 0)
2580 goto out_eio;
2581
2582 loc->nservers = 0;
2583 dprintk("%s: servers ", __FUNCTION__);
2584 while (loc->nservers < m) {
2585 struct nfs4_string *server = &loc->servers[loc->nservers];
2586 status = decode_opaque_inline(xdr, &server->len, &server->data);
2587 if (unlikely(status != 0))
2588 goto out_eio;
2589 dprintk("%s ", server->data);
2590 if (loc->nservers < NFS4_FS_LOCATION_MAXSERVERS)
2591 loc->nservers++;
2592 else {
2593 int i;
2594 dprintk("%s: using first %d of %d servers returned for location %d\n", __FUNCTION__, NFS4_FS_LOCATION_MAXSERVERS, m, res->nlocations);
2595 for (i = loc->nservers; i < m; i++) {
Trond Myklebust2e42c3e2007-05-14 17:20:41 -04002596 unsigned int len;
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002597 char *data;
2598 status = decode_opaque_inline(xdr, &len, &data);
2599 if (unlikely(status != 0))
2600 goto out_eio;
2601 }
2602 }
2603 }
2604 status = decode_pathname(xdr, &loc->rootpath);
Trond Myklebust683b57b2006-06-09 09:34:22 -04002605 if (unlikely(status != 0))
2606 goto out_eio;
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002607 if (res->nlocations < NFS4_FS_LOCATIONS_MAXENTRIES)
Trond Myklebust683b57b2006-06-09 09:34:22 -04002608 res->nlocations++;
2609 }
2610out:
2611 dprintk("%s: fs_locations done, error = %d\n", __FUNCTION__, status);
2612 return status;
2613out_eio:
2614 status = -EIO;
2615 goto out;
2616}
2617
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618static int decode_attr_maxfilesize(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2619{
Al Viro8687b632006-10-19 23:28:48 -07002620 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 int status = 0;
2622
2623 *res = 0;
2624 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXFILESIZE - 1U)))
2625 return -EIO;
2626 if (likely(bitmap[0] & FATTR4_WORD0_MAXFILESIZE)) {
2627 READ_BUF(8);
2628 READ64(*res);
2629 bitmap[0] &= ~FATTR4_WORD0_MAXFILESIZE;
2630 }
2631 dprintk("%s: maxfilesize=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2632 return status;
2633}
2634
2635static int decode_attr_maxlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxlink)
2636{
Al Viro8687b632006-10-19 23:28:48 -07002637 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 int status = 0;
2639
2640 *maxlink = 1;
2641 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXLINK - 1U)))
2642 return -EIO;
2643 if (likely(bitmap[0] & FATTR4_WORD0_MAXLINK)) {
2644 READ_BUF(4);
2645 READ32(*maxlink);
2646 bitmap[0] &= ~FATTR4_WORD0_MAXLINK;
2647 }
2648 dprintk("%s: maxlink=%u\n", __FUNCTION__, *maxlink);
2649 return status;
2650}
2651
2652static int decode_attr_maxname(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxname)
2653{
Al Viro8687b632006-10-19 23:28:48 -07002654 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 int status = 0;
2656
2657 *maxname = 1024;
2658 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXNAME - 1U)))
2659 return -EIO;
2660 if (likely(bitmap[0] & FATTR4_WORD0_MAXNAME)) {
2661 READ_BUF(4);
2662 READ32(*maxname);
2663 bitmap[0] &= ~FATTR4_WORD0_MAXNAME;
2664 }
2665 dprintk("%s: maxname=%u\n", __FUNCTION__, *maxname);
2666 return status;
2667}
2668
2669static int decode_attr_maxread(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2670{
Al Viro8687b632006-10-19 23:28:48 -07002671 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 int status = 0;
2673
2674 *res = 1024;
2675 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXREAD - 1U)))
2676 return -EIO;
2677 if (likely(bitmap[0] & FATTR4_WORD0_MAXREAD)) {
2678 uint64_t maxread;
2679 READ_BUF(8);
2680 READ64(maxread);
2681 if (maxread > 0x7FFFFFFF)
2682 maxread = 0x7FFFFFFF;
2683 *res = (uint32_t)maxread;
2684 bitmap[0] &= ~FATTR4_WORD0_MAXREAD;
2685 }
2686 dprintk("%s: maxread=%lu\n", __FUNCTION__, (unsigned long)*res);
2687 return status;
2688}
2689
2690static int decode_attr_maxwrite(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2691{
Al Viro8687b632006-10-19 23:28:48 -07002692 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 int status = 0;
2694
2695 *res = 1024;
2696 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXWRITE - 1U)))
2697 return -EIO;
2698 if (likely(bitmap[0] & FATTR4_WORD0_MAXWRITE)) {
2699 uint64_t maxwrite;
2700 READ_BUF(8);
2701 READ64(maxwrite);
2702 if (maxwrite > 0x7FFFFFFF)
2703 maxwrite = 0x7FFFFFFF;
2704 *res = (uint32_t)maxwrite;
2705 bitmap[0] &= ~FATTR4_WORD0_MAXWRITE;
2706 }
2707 dprintk("%s: maxwrite=%lu\n", __FUNCTION__, (unsigned long)*res);
2708 return status;
2709}
2710
2711static int decode_attr_mode(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *mode)
2712{
Al Viro8687b632006-10-19 23:28:48 -07002713 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
2715 *mode = 0;
2716 if (unlikely(bitmap[1] & (FATTR4_WORD1_MODE - 1U)))
2717 return -EIO;
2718 if (likely(bitmap[1] & FATTR4_WORD1_MODE)) {
2719 READ_BUF(4);
2720 READ32(*mode);
2721 *mode &= ~S_IFMT;
2722 bitmap[1] &= ~FATTR4_WORD1_MODE;
2723 }
2724 dprintk("%s: file mode=0%o\n", __FUNCTION__, (unsigned int)*mode);
2725 return 0;
2726}
2727
2728static int decode_attr_nlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *nlink)
2729{
Al Viro8687b632006-10-19 23:28:48 -07002730 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
2732 *nlink = 1;
2733 if (unlikely(bitmap[1] & (FATTR4_WORD1_NUMLINKS - 1U)))
2734 return -EIO;
2735 if (likely(bitmap[1] & FATTR4_WORD1_NUMLINKS)) {
2736 READ_BUF(4);
2737 READ32(*nlink);
2738 bitmap[1] &= ~FATTR4_WORD1_NUMLINKS;
2739 }
2740 dprintk("%s: nlink=%u\n", __FUNCTION__, (unsigned int)*nlink);
2741 return 0;
2742}
2743
Trond Myklebust2e42c3e2007-05-14 17:20:41 -04002744static int decode_attr_owner(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs_client *clp, uint32_t *uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745{
Al Viro8687b632006-10-19 23:28:48 -07002746 uint32_t len;
2747 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
2749 *uid = -2;
2750 if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER - 1U)))
2751 return -EIO;
2752 if (likely(bitmap[1] & FATTR4_WORD1_OWNER)) {
2753 READ_BUF(4);
2754 READ32(len);
2755 READ_BUF(len);
2756 if (len < XDR_MAX_NETOBJ) {
2757 if (nfs_map_name_to_uid(clp, (char *)p, len, uid) != 0)
2758 dprintk("%s: nfs_map_name_to_uid failed!\n",
2759 __FUNCTION__);
2760 } else
Chuck Leverfe82a182007-09-11 18:01:10 -04002761 dprintk("%s: name too long (%u)!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 __FUNCTION__, len);
2763 bitmap[1] &= ~FATTR4_WORD1_OWNER;
2764 }
2765 dprintk("%s: uid=%d\n", __FUNCTION__, (int)*uid);
2766 return 0;
2767}
2768
Trond Myklebust2e42c3e2007-05-14 17:20:41 -04002769static int decode_attr_group(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs_client *clp, uint32_t *gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770{
Al Viro8687b632006-10-19 23:28:48 -07002771 uint32_t len;
2772 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
2774 *gid = -2;
2775 if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER_GROUP - 1U)))
2776 return -EIO;
2777 if (likely(bitmap[1] & FATTR4_WORD1_OWNER_GROUP)) {
2778 READ_BUF(4);
2779 READ32(len);
2780 READ_BUF(len);
2781 if (len < XDR_MAX_NETOBJ) {
2782 if (nfs_map_group_to_gid(clp, (char *)p, len, gid) != 0)
2783 dprintk("%s: nfs_map_group_to_gid failed!\n",
2784 __FUNCTION__);
2785 } else
Chuck Leverfe82a182007-09-11 18:01:10 -04002786 dprintk("%s: name too long (%u)!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 __FUNCTION__, len);
2788 bitmap[1] &= ~FATTR4_WORD1_OWNER_GROUP;
2789 }
2790 dprintk("%s: gid=%d\n", __FUNCTION__, (int)*gid);
2791 return 0;
2792}
2793
2794static int decode_attr_rdev(struct xdr_stream *xdr, uint32_t *bitmap, dev_t *rdev)
2795{
Al Viro8687b632006-10-19 23:28:48 -07002796 uint32_t major = 0, minor = 0;
2797 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
2799 *rdev = MKDEV(0,0);
2800 if (unlikely(bitmap[1] & (FATTR4_WORD1_RAWDEV - 1U)))
2801 return -EIO;
2802 if (likely(bitmap[1] & FATTR4_WORD1_RAWDEV)) {
2803 dev_t tmp;
2804
2805 READ_BUF(8);
2806 READ32(major);
2807 READ32(minor);
2808 tmp = MKDEV(major, minor);
2809 if (MAJOR(tmp) == major && MINOR(tmp) == minor)
2810 *rdev = tmp;
2811 bitmap[1] &= ~ FATTR4_WORD1_RAWDEV;
2812 }
2813 dprintk("%s: rdev=(0x%x:0x%x)\n", __FUNCTION__, major, minor);
2814 return 0;
2815}
2816
2817static int decode_attr_space_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2818{
Al Viro8687b632006-10-19 23:28:48 -07002819 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 int status = 0;
2821
2822 *res = 0;
2823 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_AVAIL - 1U)))
2824 return -EIO;
2825 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_AVAIL)) {
2826 READ_BUF(8);
2827 READ64(*res);
2828 bitmap[1] &= ~FATTR4_WORD1_SPACE_AVAIL;
2829 }
2830 dprintk("%s: space avail=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2831 return status;
2832}
2833
2834static int decode_attr_space_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2835{
Al Viro8687b632006-10-19 23:28:48 -07002836 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 int status = 0;
2838
2839 *res = 0;
2840 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_FREE - 1U)))
2841 return -EIO;
2842 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_FREE)) {
2843 READ_BUF(8);
2844 READ64(*res);
2845 bitmap[1] &= ~FATTR4_WORD1_SPACE_FREE;
2846 }
2847 dprintk("%s: space free=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2848 return status;
2849}
2850
2851static int decode_attr_space_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2852{
Al Viro8687b632006-10-19 23:28:48 -07002853 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 int status = 0;
2855
2856 *res = 0;
2857 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_TOTAL - 1U)))
2858 return -EIO;
2859 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_TOTAL)) {
2860 READ_BUF(8);
2861 READ64(*res);
2862 bitmap[1] &= ~FATTR4_WORD1_SPACE_TOTAL;
2863 }
2864 dprintk("%s: space total=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2865 return status;
2866}
2867
2868static int decode_attr_space_used(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *used)
2869{
Al Viro8687b632006-10-19 23:28:48 -07002870 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
2872 *used = 0;
2873 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_USED - 1U)))
2874 return -EIO;
2875 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_USED)) {
2876 READ_BUF(8);
2877 READ64(*used);
2878 bitmap[1] &= ~FATTR4_WORD1_SPACE_USED;
2879 }
2880 dprintk("%s: space used=%Lu\n", __FUNCTION__,
2881 (unsigned long long)*used);
2882 return 0;
2883}
2884
2885static int decode_attr_time(struct xdr_stream *xdr, struct timespec *time)
2886{
Al Viro8687b632006-10-19 23:28:48 -07002887 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 uint64_t sec;
2889 uint32_t nsec;
2890
2891 READ_BUF(12);
2892 READ64(sec);
2893 READ32(nsec);
2894 time->tv_sec = (time_t)sec;
2895 time->tv_nsec = (long)nsec;
2896 return 0;
2897}
2898
2899static int decode_attr_time_access(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2900{
2901 int status = 0;
2902
2903 time->tv_sec = 0;
2904 time->tv_nsec = 0;
2905 if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_ACCESS - 1U)))
2906 return -EIO;
2907 if (likely(bitmap[1] & FATTR4_WORD1_TIME_ACCESS)) {
2908 status = decode_attr_time(xdr, time);
2909 bitmap[1] &= ~FATTR4_WORD1_TIME_ACCESS;
2910 }
2911 dprintk("%s: atime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2912 return status;
2913}
2914
2915static int decode_attr_time_metadata(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2916{
2917 int status = 0;
2918
2919 time->tv_sec = 0;
2920 time->tv_nsec = 0;
2921 if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_METADATA - 1U)))
2922 return -EIO;
2923 if (likely(bitmap[1] & FATTR4_WORD1_TIME_METADATA)) {
2924 status = decode_attr_time(xdr, time);
2925 bitmap[1] &= ~FATTR4_WORD1_TIME_METADATA;
2926 }
2927 dprintk("%s: ctime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2928 return status;
2929}
2930
2931static int decode_attr_time_modify(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2932{
2933 int status = 0;
2934
2935 time->tv_sec = 0;
2936 time->tv_nsec = 0;
2937 if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_MODIFY - 1U)))
2938 return -EIO;
2939 if (likely(bitmap[1] & FATTR4_WORD1_TIME_MODIFY)) {
2940 status = decode_attr_time(xdr, time);
2941 bitmap[1] &= ~FATTR4_WORD1_TIME_MODIFY;
2942 }
2943 dprintk("%s: mtime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2944 return status;
2945}
2946
Al Viro8687b632006-10-19 23:28:48 -07002947static int verify_attr_len(struct xdr_stream *xdr, __be32 *savep, uint32_t attrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948{
2949 unsigned int attrwords = XDR_QUADLEN(attrlen);
2950 unsigned int nwords = xdr->p - savep;
2951
2952 if (unlikely(attrwords != nwords)) {
Chuck Leverfe82a182007-09-11 18:01:10 -04002953 dprintk("%s: server returned incorrect attribute length: "
2954 "%u %c %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 __FUNCTION__,
2956 attrwords << 2,
2957 (attrwords < nwords) ? '<' : '>',
2958 nwords << 2);
2959 return -EIO;
2960 }
2961 return 0;
2962}
2963
2964static int decode_change_info(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
2965{
Al Viro8687b632006-10-19 23:28:48 -07002966 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
2968 READ_BUF(20);
2969 READ32(cinfo->atomic);
2970 READ64(cinfo->before);
2971 READ64(cinfo->after);
2972 return 0;
2973}
2974
2975static int decode_access(struct xdr_stream *xdr, struct nfs4_accessres *access)
2976{
Al Viro8687b632006-10-19 23:28:48 -07002977 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 uint32_t supp, acc;
2979 int status;
2980
2981 status = decode_op_hdr(xdr, OP_ACCESS);
2982 if (status)
2983 return status;
2984 READ_BUF(8);
2985 READ32(supp);
2986 READ32(acc);
2987 access->supported = supp;
2988 access->access = acc;
2989 return 0;
2990}
2991
2992static int decode_close(struct xdr_stream *xdr, struct nfs_closeres *res)
2993{
Al Viro8687b632006-10-19 23:28:48 -07002994 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 int status;
2996
2997 status = decode_op_hdr(xdr, OP_CLOSE);
2998 if (status)
2999 return status;
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003000 READ_BUF(NFS4_STATEID_SIZE);
3001 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 return 0;
3003}
3004
3005static int decode_commit(struct xdr_stream *xdr, struct nfs_writeres *res)
3006{
Al Viro8687b632006-10-19 23:28:48 -07003007 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 int status;
3009
3010 status = decode_op_hdr(xdr, OP_COMMIT);
3011 if (status)
3012 return status;
3013 READ_BUF(8);
3014 COPYMEM(res->verf->verifier, 8);
3015 return 0;
3016}
3017
3018static int decode_create(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
3019{
Al Viro8687b632006-10-19 23:28:48 -07003020 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 uint32_t bmlen;
3022 int status;
3023
3024 status = decode_op_hdr(xdr, OP_CREATE);
3025 if (status)
3026 return status;
3027 if ((status = decode_change_info(xdr, cinfo)))
3028 return status;
3029 READ_BUF(4);
3030 READ32(bmlen);
3031 READ_BUF(bmlen << 2);
3032 return 0;
3033}
3034
3035static int decode_server_caps(struct xdr_stream *xdr, struct nfs4_server_caps_res *res)
3036{
Al Viro8687b632006-10-19 23:28:48 -07003037 __be32 *savep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 uint32_t attrlen,
3039 bitmap[2] = {0};
3040 int status;
3041
3042 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3043 goto xdr_error;
3044 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3045 goto xdr_error;
3046 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3047 goto xdr_error;
3048 if ((status = decode_attr_supported(xdr, bitmap, res->attr_bitmask)) != 0)
3049 goto xdr_error;
3050 if ((status = decode_attr_link_support(xdr, bitmap, &res->has_links)) != 0)
3051 goto xdr_error;
3052 if ((status = decode_attr_symlink_support(xdr, bitmap, &res->has_symlinks)) != 0)
3053 goto xdr_error;
3054 if ((status = decode_attr_aclsupport(xdr, bitmap, &res->acl_bitmask)) != 0)
3055 goto xdr_error;
3056 status = verify_attr_len(xdr, savep, attrlen);
3057xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04003058 dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 return status;
3060}
3061
3062static int decode_statfs(struct xdr_stream *xdr, struct nfs_fsstat *fsstat)
3063{
Al Viro8687b632006-10-19 23:28:48 -07003064 __be32 *savep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 uint32_t attrlen,
3066 bitmap[2] = {0};
3067 int status;
3068
3069 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3070 goto xdr_error;
3071 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3072 goto xdr_error;
3073 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3074 goto xdr_error;
3075
3076 if ((status = decode_attr_files_avail(xdr, bitmap, &fsstat->afiles)) != 0)
3077 goto xdr_error;
3078 if ((status = decode_attr_files_free(xdr, bitmap, &fsstat->ffiles)) != 0)
3079 goto xdr_error;
3080 if ((status = decode_attr_files_total(xdr, bitmap, &fsstat->tfiles)) != 0)
3081 goto xdr_error;
3082 if ((status = decode_attr_space_avail(xdr, bitmap, &fsstat->abytes)) != 0)
3083 goto xdr_error;
3084 if ((status = decode_attr_space_free(xdr, bitmap, &fsstat->fbytes)) != 0)
3085 goto xdr_error;
3086 if ((status = decode_attr_space_total(xdr, bitmap, &fsstat->tbytes)) != 0)
3087 goto xdr_error;
3088
3089 status = verify_attr_len(xdr, savep, attrlen);
3090xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04003091 dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 return status;
3093}
3094
3095static int decode_pathconf(struct xdr_stream *xdr, struct nfs_pathconf *pathconf)
3096{
Al Viro8687b632006-10-19 23:28:48 -07003097 __be32 *savep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 uint32_t attrlen,
3099 bitmap[2] = {0};
3100 int status;
3101
3102 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3103 goto xdr_error;
3104 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3105 goto xdr_error;
3106 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3107 goto xdr_error;
3108
3109 if ((status = decode_attr_maxlink(xdr, bitmap, &pathconf->max_link)) != 0)
3110 goto xdr_error;
3111 if ((status = decode_attr_maxname(xdr, bitmap, &pathconf->max_namelen)) != 0)
3112 goto xdr_error;
3113
3114 status = verify_attr_len(xdr, savep, attrlen);
3115xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04003116 dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 return status;
3118}
3119
3120static int decode_getfattr(struct xdr_stream *xdr, struct nfs_fattr *fattr, const struct nfs_server *server)
3121{
Al Viro8687b632006-10-19 23:28:48 -07003122 __be32 *savep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 uint32_t attrlen,
3124 bitmap[2] = {0},
3125 type;
3126 int status, fmode = 0;
Manoj Naik99baf622006-06-09 09:34:24 -04003127 uint64_t fileid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
3129 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3130 goto xdr_error;
3131 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3132 goto xdr_error;
3133
3134 fattr->bitmap[0] = bitmap[0];
3135 fattr->bitmap[1] = bitmap[1];
3136
3137 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3138 goto xdr_error;
3139
3140
3141 if ((status = decode_attr_type(xdr, bitmap, &type)) != 0)
3142 goto xdr_error;
3143 fattr->type = nfs_type2fmt[type].nfs2type;
3144 fmode = nfs_type2fmt[type].mode;
3145
3146 if ((status = decode_attr_change(xdr, bitmap, &fattr->change_attr)) != 0)
3147 goto xdr_error;
3148 if ((status = decode_attr_size(xdr, bitmap, &fattr->size)) != 0)
3149 goto xdr_error;
Trond Myklebust8b4bdcf2006-06-09 09:34:19 -04003150 if ((status = decode_attr_fsid(xdr, bitmap, &fattr->fsid)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 goto xdr_error;
3152 if ((status = decode_attr_fileid(xdr, bitmap, &fattr->fileid)) != 0)
3153 goto xdr_error;
Trond Myklebust683b57b2006-06-09 09:34:22 -04003154 if ((status = decode_attr_fs_locations(xdr, bitmap, container_of(fattr,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04003155 struct nfs4_fs_locations,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003156 fattr))) != 0)
3157 goto xdr_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 if ((status = decode_attr_mode(xdr, bitmap, &fattr->mode)) != 0)
3159 goto xdr_error;
3160 fattr->mode |= fmode;
3161 if ((status = decode_attr_nlink(xdr, bitmap, &fattr->nlink)) != 0)
3162 goto xdr_error;
David Howells7539bba2006-08-22 20:06:09 -04003163 if ((status = decode_attr_owner(xdr, bitmap, server->nfs_client, &fattr->uid)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 goto xdr_error;
David Howells7539bba2006-08-22 20:06:09 -04003165 if ((status = decode_attr_group(xdr, bitmap, server->nfs_client, &fattr->gid)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 goto xdr_error;
3167 if ((status = decode_attr_rdev(xdr, bitmap, &fattr->rdev)) != 0)
3168 goto xdr_error;
3169 if ((status = decode_attr_space_used(xdr, bitmap, &fattr->du.nfs3.used)) != 0)
3170 goto xdr_error;
3171 if ((status = decode_attr_time_access(xdr, bitmap, &fattr->atime)) != 0)
3172 goto xdr_error;
3173 if ((status = decode_attr_time_metadata(xdr, bitmap, &fattr->ctime)) != 0)
3174 goto xdr_error;
3175 if ((status = decode_attr_time_modify(xdr, bitmap, &fattr->mtime)) != 0)
3176 goto xdr_error;
Manoj Naik99baf622006-06-09 09:34:24 -04003177 if ((status = decode_attr_mounted_on_fileid(xdr, bitmap, &fileid)) != 0)
3178 goto xdr_error;
3179 if (fattr->fileid == 0 && fileid != 0)
3180 fattr->fileid = fileid;
Trond Myklebust33801142005-10-27 22:12:39 -04003181 if ((status = verify_attr_len(xdr, savep, attrlen)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 fattr->valid = NFS_ATTR_FATTR | NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04003184 dprintk("%s: xdr returned %d\n", __FUNCTION__, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 return status;
3186}
3187
3188
3189static int decode_fsinfo(struct xdr_stream *xdr, struct nfs_fsinfo *fsinfo)
3190{
Al Viro8687b632006-10-19 23:28:48 -07003191 __be32 *savep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 uint32_t attrlen, bitmap[2];
3193 int status;
3194
3195 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3196 goto xdr_error;
3197 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3198 goto xdr_error;
3199 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3200 goto xdr_error;
3201
3202 fsinfo->rtmult = fsinfo->wtmult = 512; /* ??? */
3203
3204 if ((status = decode_attr_lease_time(xdr, bitmap, &fsinfo->lease_time)) != 0)
3205 goto xdr_error;
3206 if ((status = decode_attr_maxfilesize(xdr, bitmap, &fsinfo->maxfilesize)) != 0)
3207 goto xdr_error;
3208 if ((status = decode_attr_maxread(xdr, bitmap, &fsinfo->rtmax)) != 0)
3209 goto xdr_error;
3210 fsinfo->rtpref = fsinfo->dtpref = fsinfo->rtmax;
3211 if ((status = decode_attr_maxwrite(xdr, bitmap, &fsinfo->wtmax)) != 0)
3212 goto xdr_error;
3213 fsinfo->wtpref = fsinfo->wtmax;
3214
3215 status = verify_attr_len(xdr, savep, attrlen);
3216xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04003217 dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 return status;
3219}
3220
3221static int decode_getfh(struct xdr_stream *xdr, struct nfs_fh *fh)
3222{
Al Viro8687b632006-10-19 23:28:48 -07003223 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 uint32_t len;
3225 int status;
3226
Trond Myklebust99367812007-07-17 21:52:41 -04003227 /* Zero handle first to allow comparisons */
3228 memset(fh, 0, sizeof(*fh));
3229
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 status = decode_op_hdr(xdr, OP_GETFH);
3231 if (status)
3232 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
3234 READ_BUF(4);
3235 READ32(len);
3236 if (len > NFS4_FHSIZE)
3237 return -EIO;
3238 fh->size = len;
3239 READ_BUF(len);
3240 COPYMEM(fh->data, len);
3241 return 0;
3242}
3243
3244static int decode_link(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
3245{
3246 int status;
3247
3248 status = decode_op_hdr(xdr, OP_LINK);
3249 if (status)
3250 return status;
3251 return decode_change_info(xdr, cinfo);
3252}
3253
3254/*
3255 * We create the owner, so we know a proper owner.id length is 4.
3256 */
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003257static int decode_lock_denied (struct xdr_stream *xdr, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258{
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003259 uint64_t offset, length, clientid;
Al Viro8687b632006-10-19 23:28:48 -07003260 __be32 *p;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003261 uint32_t namelen, type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
3263 READ_BUF(32);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003264 READ64(offset);
3265 READ64(length);
3266 READ32(type);
3267 if (fl != NULL) {
3268 fl->fl_start = (loff_t)offset;
3269 fl->fl_end = fl->fl_start + (loff_t)length - 1;
3270 if (length == ~(uint64_t)0)
3271 fl->fl_end = OFFSET_MAX;
3272 fl->fl_type = F_WRLCK;
3273 if (type & 1)
3274 fl->fl_type = F_RDLCK;
3275 fl->fl_pid = 0;
3276 }
3277 READ64(clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 READ32(namelen);
3279 READ_BUF(namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 return -NFS4ERR_DENIED;
3281}
3282
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003283static int decode_lock(struct xdr_stream *xdr, struct nfs_lock_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284{
Al Viro8687b632006-10-19 23:28:48 -07003285 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 int status;
3287
3288 status = decode_op_hdr(xdr, OP_LOCK);
3289 if (status == 0) {
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003290 READ_BUF(NFS4_STATEID_SIZE);
3291 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 } else if (status == -NFS4ERR_DENIED)
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003293 return decode_lock_denied(xdr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 return status;
3295}
3296
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003297static int decode_lockt(struct xdr_stream *xdr, struct nfs_lockt_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298{
3299 int status;
3300 status = decode_op_hdr(xdr, OP_LOCKT);
3301 if (status == -NFS4ERR_DENIED)
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003302 return decode_lock_denied(xdr, res->denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 return status;
3304}
3305
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003306static int decode_locku(struct xdr_stream *xdr, struct nfs_locku_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307{
Al Viro8687b632006-10-19 23:28:48 -07003308 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 int status;
3310
3311 status = decode_op_hdr(xdr, OP_LOCKU);
3312 if (status == 0) {
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003313 READ_BUF(NFS4_STATEID_SIZE);
3314 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 }
3316 return status;
3317}
3318
3319static int decode_lookup(struct xdr_stream *xdr)
3320{
3321 return decode_op_hdr(xdr, OP_LOOKUP);
3322}
3323
3324/* This is too sick! */
3325static int decode_space_limit(struct xdr_stream *xdr, u64 *maxsize)
3326{
Al Viro8687b632006-10-19 23:28:48 -07003327 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 uint32_t limit_type, nblocks, blocksize;
3329
3330 READ_BUF(12);
3331 READ32(limit_type);
3332 switch (limit_type) {
3333 case 1:
3334 READ64(*maxsize);
3335 break;
3336 case 2:
3337 READ32(nblocks);
3338 READ32(blocksize);
3339 *maxsize = (uint64_t)nblocks * (uint64_t)blocksize;
3340 }
3341 return 0;
3342}
3343
3344static int decode_delegation(struct xdr_stream *xdr, struct nfs_openres *res)
3345{
Al Viro8687b632006-10-19 23:28:48 -07003346 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 uint32_t delegation_type;
3348
3349 READ_BUF(4);
3350 READ32(delegation_type);
3351 if (delegation_type == NFS4_OPEN_DELEGATE_NONE) {
3352 res->delegation_type = 0;
3353 return 0;
3354 }
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003355 READ_BUF(NFS4_STATEID_SIZE+4);
3356 COPYMEM(res->delegation.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 READ32(res->do_recall);
3358 switch (delegation_type) {
3359 case NFS4_OPEN_DELEGATE_READ:
3360 res->delegation_type = FMODE_READ;
3361 break;
3362 case NFS4_OPEN_DELEGATE_WRITE:
3363 res->delegation_type = FMODE_WRITE|FMODE_READ;
3364 if (decode_space_limit(xdr, &res->maxsize) < 0)
3365 return -EIO;
3366 }
David Howells7539bba2006-08-22 20:06:09 -04003367 return decode_ace(xdr, NULL, res->server->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368}
3369
3370static int decode_open(struct xdr_stream *xdr, struct nfs_openres *res)
3371{
Al Viro8687b632006-10-19 23:28:48 -07003372 __be32 *p;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003373 uint32_t savewords, bmlen, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 int status;
3375
3376 status = decode_op_hdr(xdr, OP_OPEN);
3377 if (status)
3378 return status;
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003379 READ_BUF(NFS4_STATEID_SIZE);
3380 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
3382 decode_change_info(xdr, &res->cinfo);
3383
3384 READ_BUF(8);
3385 READ32(res->rflags);
3386 READ32(bmlen);
3387 if (bmlen > 10)
3388 goto xdr_error;
3389
3390 READ_BUF(bmlen << 2);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003391 savewords = min_t(uint32_t, bmlen, NFS4_BITMAP_SIZE);
3392 for (i = 0; i < savewords; ++i)
3393 READ32(res->attrset[i]);
3394 for (; i < NFS4_BITMAP_SIZE; i++)
3395 res->attrset[i] = 0;
3396
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 return decode_delegation(xdr, res);
3398xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04003399 dprintk("%s: Bitmap too large! Length = %u\n", __FUNCTION__, bmlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 return -EIO;
3401}
3402
3403static int decode_open_confirm(struct xdr_stream *xdr, struct nfs_open_confirmres *res)
3404{
Al Viro8687b632006-10-19 23:28:48 -07003405 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 int status;
3407
3408 status = decode_op_hdr(xdr, OP_OPEN_CONFIRM);
3409 if (status)
3410 return status;
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003411 READ_BUF(NFS4_STATEID_SIZE);
3412 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 return 0;
3414}
3415
3416static int decode_open_downgrade(struct xdr_stream *xdr, struct nfs_closeres *res)
3417{
Al Viro8687b632006-10-19 23:28:48 -07003418 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 int status;
3420
3421 status = decode_op_hdr(xdr, OP_OPEN_DOWNGRADE);
3422 if (status)
3423 return status;
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003424 READ_BUF(NFS4_STATEID_SIZE);
3425 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 return 0;
3427}
3428
3429static int decode_putfh(struct xdr_stream *xdr)
3430{
3431 return decode_op_hdr(xdr, OP_PUTFH);
3432}
3433
3434static int decode_putrootfh(struct xdr_stream *xdr)
3435{
3436 return decode_op_hdr(xdr, OP_PUTROOTFH);
3437}
3438
3439static int decode_read(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs_readres *res)
3440{
3441 struct kvec *iov = req->rq_rcv_buf.head;
Al Viro8687b632006-10-19 23:28:48 -07003442 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 uint32_t count, eof, recvd, hdrlen;
3444 int status;
3445
3446 status = decode_op_hdr(xdr, OP_READ);
3447 if (status)
3448 return status;
3449 READ_BUF(8);
3450 READ32(eof);
3451 READ32(count);
3452 hdrlen = (u8 *) p - (u8 *) iov->iov_base;
3453 recvd = req->rq_rcv_buf.len - hdrlen;
3454 if (count > recvd) {
Chuck Leverfe82a182007-09-11 18:01:10 -04003455 dprintk("NFS: server cheating in read reply: "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 "count %u > recvd %u\n", count, recvd);
3457 count = recvd;
3458 eof = 0;
3459 }
3460 xdr_read_pages(xdr, count);
3461 res->eof = eof;
3462 res->count = count;
3463 return 0;
3464}
3465
3466static int decode_readdir(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs4_readdir_res *readdir)
3467{
3468 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
3469 struct page *page = *rcvbuf->pages;
3470 struct kvec *iov = rcvbuf->head;
3471 unsigned int nr, pglen = rcvbuf->page_len;
Al Viro8687b632006-10-19 23:28:48 -07003472 __be32 *end, *entry, *p, *kaddr;
David Howellse8896492006-08-24 15:44:19 -04003473 uint32_t len, attrlen, xlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 int hdrlen, recvd, status;
3475
3476 status = decode_op_hdr(xdr, OP_READDIR);
3477 if (status)
3478 return status;
3479 READ_BUF(8);
3480 COPYMEM(readdir->verifier.data, 8);
Trond Myklebusteadf4592005-06-22 17:16:39 +00003481 dprintk("%s: verifier = 0x%x%x\n",
3482 __FUNCTION__,
3483 ((u32 *)readdir->verifier.data)[0],
3484 ((u32 *)readdir->verifier.data)[1]);
3485
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486
3487 hdrlen = (char *) p - (char *) iov->iov_base;
3488 recvd = rcvbuf->len - hdrlen;
3489 if (pglen > recvd)
3490 pglen = recvd;
3491 xdr_read_pages(xdr, pglen);
3492
3493 BUG_ON(pglen + readdir->pgbase > PAGE_CACHE_SIZE);
Al Viro8687b632006-10-19 23:28:48 -07003494 kaddr = p = kmap_atomic(page, KM_USER0);
David Howellse8896492006-08-24 15:44:19 -04003495 end = p + ((pglen + readdir->pgbase) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 entry = p;
3497 for (nr = 0; *p++; nr++) {
David Howellse8896492006-08-24 15:44:19 -04003498 if (end - p < 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 goto short_pkt;
Trond Myklebusteadf4592005-06-22 17:16:39 +00003500 dprintk("cookie = %Lu, ", *((unsigned long long *)p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 p += 2; /* cookie */
3502 len = ntohl(*p++); /* filename length */
3503 if (len > NFS4_MAXNAMLEN) {
Chuck Leverfe82a182007-09-11 18:01:10 -04003504 dprintk("NFS: giant filename in readdir (len 0x%x)\n",
3505 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 goto err_unmap;
3507 }
David Howellse8896492006-08-24 15:44:19 -04003508 xlen = XDR_QUADLEN(len);
3509 if (end - p < xlen + 1)
3510 goto short_pkt;
Trond Myklebusteadf4592005-06-22 17:16:39 +00003511 dprintk("filename = %*s\n", len, (char *)p);
David Howellse8896492006-08-24 15:44:19 -04003512 p += xlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 len = ntohl(*p++); /* bitmap length */
David Howellse8896492006-08-24 15:44:19 -04003514 if (end - p < len + 1)
3515 goto short_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 p += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 attrlen = XDR_QUADLEN(ntohl(*p++));
David Howellse8896492006-08-24 15:44:19 -04003518 if (end - p < attrlen + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 goto short_pkt;
David Howellse8896492006-08-24 15:44:19 -04003520 p += attrlen; /* attributes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 entry = p;
3522 }
3523 if (!nr && (entry[0] != 0 || entry[1] == 0))
3524 goto short_pkt;
3525out:
3526 kunmap_atomic(kaddr, KM_USER0);
3527 return 0;
3528short_pkt:
Trond Myklebusteadf4592005-06-22 17:16:39 +00003529 dprintk("%s: short packet at entry %d\n", __FUNCTION__, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 entry[0] = entry[1] = 0;
3531 /* truncate listing ? */
3532 if (!nr) {
Chuck Leverfe82a182007-09-11 18:01:10 -04003533 dprintk("NFS: readdir reply truncated!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 entry[1] = 1;
3535 }
3536 goto out;
3537err_unmap:
3538 kunmap_atomic(kaddr, KM_USER0);
3539 return -errno_NFSERR_IO;
3540}
3541
3542static int decode_readlink(struct xdr_stream *xdr, struct rpc_rqst *req)
3543{
3544 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
3545 struct kvec *iov = rcvbuf->head;
3546 int hdrlen, len, recvd;
Al Viro8687b632006-10-19 23:28:48 -07003547 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 char *kaddr;
3549 int status;
3550
3551 status = decode_op_hdr(xdr, OP_READLINK);
3552 if (status)
3553 return status;
3554
3555 /* Convert length of symlink */
3556 READ_BUF(4);
3557 READ32(len);
3558 if (len >= rcvbuf->page_len || len <= 0) {
Chuck Leverfe82a182007-09-11 18:01:10 -04003559 dprintk("nfs: server returned giant symlink!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 return -ENAMETOOLONG;
3561 }
3562 hdrlen = (char *) xdr->p - (char *) iov->iov_base;
3563 recvd = req->rq_rcv_buf.len - hdrlen;
3564 if (recvd < len) {
Chuck Leverfe82a182007-09-11 18:01:10 -04003565 dprintk("NFS: server cheating in readlink reply: "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 "count %u > recvd %u\n", len, recvd);
3567 return -EIO;
3568 }
3569 xdr_read_pages(xdr, len);
3570 /*
3571 * The XDR encode routine has set things up so that
3572 * the link text will be copied directly into the
3573 * buffer. We just have to do overflow-checking,
3574 * and and null-terminate the text (the VFS expects
3575 * null-termination).
3576 */
3577 kaddr = (char *)kmap_atomic(rcvbuf->pages[0], KM_USER0);
3578 kaddr[len+rcvbuf->page_base] = '\0';
3579 kunmap_atomic(kaddr, KM_USER0);
3580 return 0;
3581}
3582
3583static int decode_remove(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
3584{
3585 int status;
3586
3587 status = decode_op_hdr(xdr, OP_REMOVE);
3588 if (status)
3589 goto out;
3590 status = decode_change_info(xdr, cinfo);
3591out:
3592 return status;
3593}
3594
3595static int decode_rename(struct xdr_stream *xdr, struct nfs4_change_info *old_cinfo,
3596 struct nfs4_change_info *new_cinfo)
3597{
3598 int status;
3599
3600 status = decode_op_hdr(xdr, OP_RENAME);
3601 if (status)
3602 goto out;
3603 if ((status = decode_change_info(xdr, old_cinfo)))
3604 goto out;
3605 status = decode_change_info(xdr, new_cinfo);
3606out:
3607 return status;
3608}
3609
3610static int decode_renew(struct xdr_stream *xdr)
3611{
3612 return decode_op_hdr(xdr, OP_RENEW);
3613}
3614
Trond Myklebust56ae19f2005-10-27 22:12:40 -04003615static int
3616decode_restorefh(struct xdr_stream *xdr)
3617{
3618 return decode_op_hdr(xdr, OP_RESTOREFH);
3619}
3620
J. Bruce Fields029d1052005-06-22 17:16:22 +00003621static int decode_getacl(struct xdr_stream *xdr, struct rpc_rqst *req,
3622 size_t *acl_len)
3623{
Al Viro8687b632006-10-19 23:28:48 -07003624 __be32 *savep;
J. Bruce Fields029d1052005-06-22 17:16:22 +00003625 uint32_t attrlen,
3626 bitmap[2] = {0};
3627 struct kvec *iov = req->rq_rcv_buf.head;
3628 int status;
3629
3630 *acl_len = 0;
3631 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3632 goto out;
3633 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3634 goto out;
3635 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3636 goto out;
3637
3638 if (unlikely(bitmap[0] & (FATTR4_WORD0_ACL - 1U)))
3639 return -EIO;
3640 if (likely(bitmap[0] & FATTR4_WORD0_ACL)) {
3641 int hdrlen, recvd;
3642
3643 /* We ignore &savep and don't do consistency checks on
3644 * the attr length. Let userspace figure it out.... */
3645 hdrlen = (u8 *)xdr->p - (u8 *)iov->iov_base;
3646 recvd = req->rq_rcv_buf.len - hdrlen;
3647 if (attrlen > recvd) {
Chuck Leverfe82a182007-09-11 18:01:10 -04003648 dprintk("NFS: server cheating in getattr"
J. Bruce Fields029d1052005-06-22 17:16:22 +00003649 " acl reply: attrlen %u > recvd %u\n",
3650 attrlen, recvd);
3651 return -EINVAL;
3652 }
J. Bruce Fieldsc04871e2006-05-30 16:28:58 -04003653 xdr_read_pages(xdr, attrlen);
J. Bruce Fields029d1052005-06-22 17:16:22 +00003654 *acl_len = attrlen;
J. Bruce Fields8c233cf2005-10-13 16:54:27 -04003655 } else
3656 status = -EOPNOTSUPP;
J. Bruce Fields029d1052005-06-22 17:16:22 +00003657
3658out:
3659 return status;
3660}
3661
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662static int
3663decode_savefh(struct xdr_stream *xdr)
3664{
3665 return decode_op_hdr(xdr, OP_SAVEFH);
3666}
3667
3668static int decode_setattr(struct xdr_stream *xdr, struct nfs_setattrres *res)
3669{
Al Viro8687b632006-10-19 23:28:48 -07003670 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 uint32_t bmlen;
3672 int status;
3673
3674
3675 status = decode_op_hdr(xdr, OP_SETATTR);
3676 if (status)
3677 return status;
3678 READ_BUF(4);
3679 READ32(bmlen);
3680 READ_BUF(bmlen << 2);
3681 return 0;
3682}
3683
David Howellsadfa6f92006-08-22 20:06:08 -04003684static int decode_setclientid(struct xdr_stream *xdr, struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685{
Al Viro8687b632006-10-19 23:28:48 -07003686 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 uint32_t opnum;
3688 int32_t nfserr;
3689
3690 READ_BUF(8);
3691 READ32(opnum);
3692 if (opnum != OP_SETCLIENTID) {
Chuck Leverfe82a182007-09-11 18:01:10 -04003693 dprintk("nfs: decode_setclientid: Server returned operation"
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 " %d\n", opnum);
3695 return -EIO;
3696 }
3697 READ32(nfserr);
3698 if (nfserr == NFS_OK) {
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003699 READ_BUF(8 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 READ64(clp->cl_clientid);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003701 COPYMEM(clp->cl_confirm.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 } else if (nfserr == NFSERR_CLID_INUSE) {
3703 uint32_t len;
3704
3705 /* skip netid string */
3706 READ_BUF(4);
3707 READ32(len);
3708 READ_BUF(len);
3709
3710 /* skip uaddr string */
3711 READ_BUF(4);
3712 READ32(len);
3713 READ_BUF(len);
3714 return -NFSERR_CLID_INUSE;
3715 } else
David Howells0a8ea432006-08-22 20:06:08 -04003716 return -nfs4_stat_to_errno(nfserr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717
3718 return 0;
3719}
3720
3721static int decode_setclientid_confirm(struct xdr_stream *xdr)
3722{
3723 return decode_op_hdr(xdr, OP_SETCLIENTID_CONFIRM);
3724}
3725
3726static int decode_write(struct xdr_stream *xdr, struct nfs_writeres *res)
3727{
Al Viro8687b632006-10-19 23:28:48 -07003728 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 int status;
3730
3731 status = decode_op_hdr(xdr, OP_WRITE);
3732 if (status)
3733 return status;
3734
3735 READ_BUF(16);
3736 READ32(res->count);
3737 READ32(res->verf->committed);
3738 COPYMEM(res->verf->verifier, 8);
3739 return 0;
3740}
3741
3742static int decode_delegreturn(struct xdr_stream *xdr)
3743{
3744 return decode_op_hdr(xdr, OP_DELEGRETURN);
3745}
3746
3747/*
3748 * Decode OPEN_DOWNGRADE response
3749 */
Al Viro8687b632006-10-19 23:28:48 -07003750static int nfs4_xdr_dec_open_downgrade(struct rpc_rqst *rqstp, __be32 *p, struct nfs_closeres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751{
3752 struct xdr_stream xdr;
3753 struct compound_hdr hdr;
3754 int status;
3755
3756 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3757 status = decode_compound_hdr(&xdr, &hdr);
3758 if (status)
3759 goto out;
3760 status = decode_putfh(&xdr);
3761 if (status)
3762 goto out;
3763 status = decode_open_downgrade(&xdr, res);
Trond Myklebust516a6af2005-10-27 22:12:41 -04003764 if (status != 0)
3765 goto out;
3766 decode_getfattr(&xdr, res->fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767out:
3768 return status;
3769}
3770
3771/*
3772 * END OF "GENERIC" DECODE ROUTINES.
3773 */
3774
3775/*
3776 * Decode ACCESS response
3777 */
Al Viro8687b632006-10-19 23:28:48 -07003778static int nfs4_xdr_dec_access(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_accessres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779{
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 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3786 goto out;
3787 if ((status = decode_putfh(&xdr)) == 0)
3788 status = decode_access(&xdr, res);
3789out:
3790 return status;
3791}
3792
3793/*
3794 * Decode LOOKUP response
3795 */
Al Viro8687b632006-10-19 23:28:48 -07003796static int nfs4_xdr_dec_lookup(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_lookup_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797{
3798 struct xdr_stream xdr;
3799 struct compound_hdr hdr;
3800 int status;
3801
3802 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3803 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3804 goto out;
3805 if ((status = decode_putfh(&xdr)) != 0)
3806 goto out;
3807 if ((status = decode_lookup(&xdr)) != 0)
3808 goto out;
3809 if ((status = decode_getfh(&xdr, res->fh)) != 0)
3810 goto out;
3811 status = decode_getfattr(&xdr, res->fattr, res->server);
3812out:
3813 return status;
3814}
3815
3816/*
3817 * Decode LOOKUP_ROOT response
3818 */
Al Viro8687b632006-10-19 23:28:48 -07003819static int nfs4_xdr_dec_lookup_root(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_lookup_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820{
3821 struct xdr_stream xdr;
3822 struct compound_hdr hdr;
3823 int status;
3824
3825 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3826 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3827 goto out;
3828 if ((status = decode_putrootfh(&xdr)) != 0)
3829 goto out;
3830 if ((status = decode_getfh(&xdr, res->fh)) == 0)
3831 status = decode_getfattr(&xdr, res->fattr, res->server);
3832out:
3833 return status;
3834}
3835
3836/*
3837 * Decode REMOVE response
3838 */
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003839static int nfs4_xdr_dec_remove(struct rpc_rqst *rqstp, __be32 *p, struct nfs_removeres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840{
3841 struct xdr_stream xdr;
3842 struct compound_hdr hdr;
3843 int status;
3844
3845 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3846 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3847 goto out;
Trond Myklebust16e42952005-10-27 22:12:44 -04003848 if ((status = decode_putfh(&xdr)) != 0)
3849 goto out;
3850 if ((status = decode_remove(&xdr, &res->cinfo)) != 0)
3851 goto out;
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003852 decode_getfattr(&xdr, &res->dir_attr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853out:
3854 return status;
3855}
3856
3857/*
3858 * Decode RENAME response
3859 */
Al Viro8687b632006-10-19 23:28:48 -07003860static int nfs4_xdr_dec_rename(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_rename_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861{
3862 struct xdr_stream xdr;
3863 struct compound_hdr hdr;
3864 int status;
3865
3866 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3867 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3868 goto out;
3869 if ((status = decode_putfh(&xdr)) != 0)
3870 goto out;
3871 if ((status = decode_savefh(&xdr)) != 0)
3872 goto out;
3873 if ((status = decode_putfh(&xdr)) != 0)
3874 goto out;
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003875 if ((status = decode_rename(&xdr, &res->old_cinfo, &res->new_cinfo)) != 0)
3876 goto out;
3877 /* Current FH is target directory */
3878 if (decode_getfattr(&xdr, res->new_fattr, res->server) != 0)
3879 goto out;
3880 if ((status = decode_restorefh(&xdr)) != 0)
3881 goto out;
3882 decode_getfattr(&xdr, res->old_fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883out:
3884 return status;
3885}
3886
3887/*
3888 * Decode LINK response
3889 */
Al Viro8687b632006-10-19 23:28:48 -07003890static int nfs4_xdr_dec_link(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_link_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891{
3892 struct xdr_stream xdr;
3893 struct compound_hdr hdr;
3894 int status;
3895
3896 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3897 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3898 goto out;
3899 if ((status = decode_putfh(&xdr)) != 0)
3900 goto out;
3901 if ((status = decode_savefh(&xdr)) != 0)
3902 goto out;
3903 if ((status = decode_putfh(&xdr)) != 0)
3904 goto out;
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003905 if ((status = decode_link(&xdr, &res->cinfo)) != 0)
3906 goto out;
3907 /*
3908 * Note order: OP_LINK leaves the directory as the current
3909 * filehandle.
3910 */
3911 if (decode_getfattr(&xdr, res->dir_attr, res->server) != 0)
3912 goto out;
3913 if ((status = decode_restorefh(&xdr)) != 0)
3914 goto out;
3915 decode_getfattr(&xdr, res->fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916out:
3917 return status;
3918}
3919
3920/*
3921 * Decode CREATE response
3922 */
Al Viro8687b632006-10-19 23:28:48 -07003923static int nfs4_xdr_dec_create(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_create_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924{
3925 struct xdr_stream xdr;
3926 struct compound_hdr hdr;
3927 int status;
3928
3929 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3930 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3931 goto out;
3932 if ((status = decode_putfh(&xdr)) != 0)
3933 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04003934 if ((status = decode_savefh(&xdr)) != 0)
3935 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 if ((status = decode_create(&xdr,&res->dir_cinfo)) != 0)
3937 goto out;
3938 if ((status = decode_getfh(&xdr, res->fh)) != 0)
3939 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04003940 if (decode_getfattr(&xdr, res->fattr, res->server) != 0)
3941 goto out;
3942 if ((status = decode_restorefh(&xdr)) != 0)
3943 goto out;
3944 decode_getfattr(&xdr, res->dir_fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945out:
3946 return status;
3947}
3948
3949/*
3950 * Decode SYMLINK response
3951 */
Al Viro8687b632006-10-19 23:28:48 -07003952static int nfs4_xdr_dec_symlink(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_create_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953{
3954 return nfs4_xdr_dec_create(rqstp, p, res);
3955}
3956
3957/*
3958 * Decode GETATTR response
3959 */
Al Viro8687b632006-10-19 23:28:48 -07003960static int nfs4_xdr_dec_getattr(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_getattr_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961{
3962 struct xdr_stream xdr;
3963 struct compound_hdr hdr;
3964 int status;
3965
3966 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3967 status = decode_compound_hdr(&xdr, &hdr);
3968 if (status)
3969 goto out;
3970 status = decode_putfh(&xdr);
3971 if (status)
3972 goto out;
3973 status = decode_getfattr(&xdr, res->fattr, res->server);
3974out:
3975 return status;
3976
3977}
3978
J. Bruce Fields23ec6962005-06-22 17:16:22 +00003979/*
3980 * Encode an SETACL request
3981 */
3982static int
Al Viro8687b632006-10-19 23:28:48 -07003983nfs4_xdr_enc_setacl(struct rpc_rqst *req, __be32 *p, struct nfs_setaclargs *args)
J. Bruce Fields23ec6962005-06-22 17:16:22 +00003984{
3985 struct xdr_stream xdr;
3986 struct compound_hdr hdr = {
3987 .nops = 2,
3988 };
3989 int status;
3990
3991 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
3992 encode_compound_hdr(&xdr, &hdr);
3993 status = encode_putfh(&xdr, args->fh);
3994 if (status)
3995 goto out;
3996 status = encode_setacl(&xdr, args);
3997out:
3998 return status;
3999}
4000/*
4001 * Decode SETACL response
4002 */
4003static int
Al Viro8687b632006-10-19 23:28:48 -07004004nfs4_xdr_dec_setacl(struct rpc_rqst *rqstp, __be32 *p, void *res)
J. Bruce Fields23ec6962005-06-22 17:16:22 +00004005{
4006 struct xdr_stream xdr;
4007 struct compound_hdr hdr;
4008 int status;
4009
4010 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4011 status = decode_compound_hdr(&xdr, &hdr);
4012 if (status)
4013 goto out;
4014 status = decode_putfh(&xdr);
4015 if (status)
4016 goto out;
4017 status = decode_setattr(&xdr, res);
4018out:
4019 return status;
4020}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021
4022/*
J. Bruce Fields029d1052005-06-22 17:16:22 +00004023 * Decode GETACL response
4024 */
4025static int
Al Viro8687b632006-10-19 23:28:48 -07004026nfs4_xdr_dec_getacl(struct rpc_rqst *rqstp, __be32 *p, size_t *acl_len)
J. Bruce Fields029d1052005-06-22 17:16:22 +00004027{
4028 struct xdr_stream xdr;
4029 struct compound_hdr hdr;
4030 int status;
4031
4032 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4033 status = decode_compound_hdr(&xdr, &hdr);
4034 if (status)
4035 goto out;
4036 status = decode_putfh(&xdr);
4037 if (status)
4038 goto out;
4039 status = decode_getacl(&xdr, rqstp, acl_len);
4040
4041out:
4042 return status;
4043}
4044
4045/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 * Decode CLOSE response
4047 */
Al Viro8687b632006-10-19 23:28:48 -07004048static int nfs4_xdr_dec_close(struct rpc_rqst *rqstp, __be32 *p, struct nfs_closeres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049{
4050 struct xdr_stream xdr;
4051 struct compound_hdr hdr;
4052 int status;
4053
4054 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4055 status = decode_compound_hdr(&xdr, &hdr);
4056 if (status)
4057 goto out;
4058 status = decode_putfh(&xdr);
4059 if (status)
4060 goto out;
4061 status = decode_close(&xdr, res);
Trond Myklebust516a6af2005-10-27 22:12:41 -04004062 if (status != 0)
4063 goto out;
4064 /*
4065 * Note: Server may do delete on close for this file
4066 * in which case the getattr call will fail with
4067 * an ESTALE error. Shouldn't be a problem,
4068 * though, since fattr->valid will remain unset.
4069 */
4070 decode_getfattr(&xdr, res->fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071out:
4072 return status;
4073}
4074
4075/*
4076 * Decode OPEN response
4077 */
Al Viro8687b632006-10-19 23:28:48 -07004078static int nfs4_xdr_dec_open(struct rpc_rqst *rqstp, __be32 *p, struct nfs_openres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079{
4080 struct xdr_stream xdr;
4081 struct compound_hdr hdr;
4082 int status;
4083
4084 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4085 status = decode_compound_hdr(&xdr, &hdr);
4086 if (status)
4087 goto out;
4088 status = decode_putfh(&xdr);
4089 if (status)
4090 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04004091 status = decode_savefh(&xdr);
4092 if (status)
4093 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 status = decode_open(&xdr, res);
4095 if (status)
4096 goto out;
Trond Myklebust99367812007-07-17 21:52:41 -04004097 if (decode_getfh(&xdr, &res->fh) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04004099 if (decode_getfattr(&xdr, res->f_attr, res->server) != 0)
4100 goto out;
Trond Myklebust365c8f52007-07-17 21:52:37 -04004101 if (decode_restorefh(&xdr) != 0)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04004102 goto out;
4103 decode_getfattr(&xdr, res->dir_attr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104out:
4105 return status;
4106}
4107
4108/*
4109 * Decode OPEN_CONFIRM response
4110 */
Al Viro8687b632006-10-19 23:28:48 -07004111static int nfs4_xdr_dec_open_confirm(struct rpc_rqst *rqstp, __be32 *p, struct nfs_open_confirmres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112{
4113 struct xdr_stream xdr;
4114 struct compound_hdr hdr;
4115 int status;
4116
4117 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4118 status = decode_compound_hdr(&xdr, &hdr);
4119 if (status)
4120 goto out;
4121 status = decode_putfh(&xdr);
4122 if (status)
4123 goto out;
4124 status = decode_open_confirm(&xdr, res);
4125out:
4126 return status;
4127}
4128
4129/*
4130 * Decode OPEN response
4131 */
Al Viro8687b632006-10-19 23:28:48 -07004132static int nfs4_xdr_dec_open_noattr(struct rpc_rqst *rqstp, __be32 *p, struct nfs_openres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133{
4134 struct xdr_stream xdr;
4135 struct compound_hdr hdr;
4136 int status;
4137
4138 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4139 status = decode_compound_hdr(&xdr, &hdr);
4140 if (status)
4141 goto out;
4142 status = decode_putfh(&xdr);
4143 if (status)
4144 goto out;
4145 status = decode_open(&xdr, res);
Trond Myklebust864472e2006-01-03 09:55:15 +01004146 if (status)
4147 goto out;
4148 decode_getfattr(&xdr, res->f_attr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149out:
4150 return status;
4151}
4152
4153/*
4154 * Decode SETATTR response
4155 */
Al Viro8687b632006-10-19 23:28:48 -07004156static int nfs4_xdr_dec_setattr(struct rpc_rqst *rqstp, __be32 *p, struct nfs_setattrres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157{
4158 struct xdr_stream xdr;
4159 struct compound_hdr hdr;
4160 int status;
4161
4162 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4163 status = decode_compound_hdr(&xdr, &hdr);
4164 if (status)
4165 goto out;
4166 status = decode_putfh(&xdr);
4167 if (status)
4168 goto out;
4169 status = decode_setattr(&xdr, res);
4170 if (status)
4171 goto out;
4172 status = decode_getfattr(&xdr, res->fattr, res->server);
4173 if (status == NFS4ERR_DELAY)
4174 status = 0;
4175out:
4176 return status;
4177}
4178
4179/*
4180 * Decode LOCK response
4181 */
Al Viro8687b632006-10-19 23:28:48 -07004182static int nfs4_xdr_dec_lock(struct rpc_rqst *rqstp, __be32 *p, struct nfs_lock_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183{
4184 struct xdr_stream xdr;
4185 struct compound_hdr hdr;
4186 int status;
4187
4188 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4189 status = decode_compound_hdr(&xdr, &hdr);
4190 if (status)
4191 goto out;
4192 status = decode_putfh(&xdr);
4193 if (status)
4194 goto out;
4195 status = decode_lock(&xdr, res);
4196out:
4197 return status;
4198}
4199
4200/*
4201 * Decode LOCKT response
4202 */
Al Viro8687b632006-10-19 23:28:48 -07004203static int nfs4_xdr_dec_lockt(struct rpc_rqst *rqstp, __be32 *p, struct nfs_lockt_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204{
4205 struct xdr_stream xdr;
4206 struct compound_hdr hdr;
4207 int status;
4208
4209 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4210 status = decode_compound_hdr(&xdr, &hdr);
4211 if (status)
4212 goto out;
4213 status = decode_putfh(&xdr);
4214 if (status)
4215 goto out;
4216 status = decode_lockt(&xdr, res);
4217out:
4218 return status;
4219}
4220
4221/*
4222 * Decode LOCKU response
4223 */
Al Viro8687b632006-10-19 23:28:48 -07004224static int nfs4_xdr_dec_locku(struct rpc_rqst *rqstp, __be32 *p, struct nfs_locku_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225{
4226 struct xdr_stream xdr;
4227 struct compound_hdr hdr;
4228 int status;
4229
4230 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4231 status = decode_compound_hdr(&xdr, &hdr);
4232 if (status)
4233 goto out;
4234 status = decode_putfh(&xdr);
4235 if (status)
4236 goto out;
4237 status = decode_locku(&xdr, res);
4238out:
4239 return status;
4240}
4241
4242/*
4243 * Decode READLINK response
4244 */
Al Viro8687b632006-10-19 23:28:48 -07004245static int nfs4_xdr_dec_readlink(struct rpc_rqst *rqstp, __be32 *p, void *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246{
4247 struct xdr_stream xdr;
4248 struct compound_hdr hdr;
4249 int status;
4250
4251 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4252 status = decode_compound_hdr(&xdr, &hdr);
4253 if (status)
4254 goto out;
4255 status = decode_putfh(&xdr);
4256 if (status)
4257 goto out;
4258 status = decode_readlink(&xdr, rqstp);
4259out:
4260 return status;
4261}
4262
4263/*
4264 * Decode READDIR response
4265 */
Al Viro8687b632006-10-19 23:28:48 -07004266static int nfs4_xdr_dec_readdir(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_readdir_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267{
4268 struct xdr_stream xdr;
4269 struct compound_hdr hdr;
4270 int status;
4271
4272 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4273 status = decode_compound_hdr(&xdr, &hdr);
4274 if (status)
4275 goto out;
4276 status = decode_putfh(&xdr);
4277 if (status)
4278 goto out;
4279 status = decode_readdir(&xdr, rqstp, res);
4280out:
4281 return status;
4282}
4283
4284/*
4285 * Decode Read response
4286 */
Al Viro8687b632006-10-19 23:28:48 -07004287static int nfs4_xdr_dec_read(struct rpc_rqst *rqstp, __be32 *p, struct nfs_readres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288{
4289 struct xdr_stream xdr;
4290 struct compound_hdr hdr;
4291 int status;
4292
4293 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4294 status = decode_compound_hdr(&xdr, &hdr);
4295 if (status)
4296 goto out;
4297 status = decode_putfh(&xdr);
4298 if (status)
4299 goto out;
4300 status = decode_read(&xdr, rqstp, res);
4301 if (!status)
4302 status = res->count;
4303out:
4304 return status;
4305}
4306
4307/*
4308 * Decode WRITE response
4309 */
Al Viro8687b632006-10-19 23:28:48 -07004310static int nfs4_xdr_dec_write(struct rpc_rqst *rqstp, __be32 *p, struct nfs_writeres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311{
4312 struct xdr_stream xdr;
4313 struct compound_hdr hdr;
4314 int status;
4315
4316 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4317 status = decode_compound_hdr(&xdr, &hdr);
4318 if (status)
4319 goto out;
4320 status = decode_putfh(&xdr);
4321 if (status)
4322 goto out;
4323 status = decode_write(&xdr, res);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004324 if (status)
4325 goto out;
4326 decode_getfattr(&xdr, res->fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 if (!status)
4328 status = res->count;
4329out:
4330 return status;
4331}
4332
4333/*
4334 * Decode COMMIT response
4335 */
Al Viro8687b632006-10-19 23:28:48 -07004336static int nfs4_xdr_dec_commit(struct rpc_rqst *rqstp, __be32 *p, struct nfs_writeres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337{
4338 struct xdr_stream xdr;
4339 struct compound_hdr hdr;
4340 int status;
4341
4342 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4343 status = decode_compound_hdr(&xdr, &hdr);
4344 if (status)
4345 goto out;
4346 status = decode_putfh(&xdr);
4347 if (status)
4348 goto out;
4349 status = decode_commit(&xdr, res);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004350 if (status)
4351 goto out;
4352 decode_getfattr(&xdr, res->fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353out:
4354 return status;
4355}
4356
4357/*
4358 * FSINFO request
4359 */
Al Viro8687b632006-10-19 23:28:48 -07004360static int nfs4_xdr_dec_fsinfo(struct rpc_rqst *req, __be32 *p, struct nfs_fsinfo *fsinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361{
4362 struct xdr_stream xdr;
4363 struct compound_hdr hdr;
4364 int status;
4365
4366 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4367 status = decode_compound_hdr(&xdr, &hdr);
4368 if (!status)
4369 status = decode_putfh(&xdr);
4370 if (!status)
4371 status = decode_fsinfo(&xdr, fsinfo);
4372 if (!status)
David Howells0a8ea432006-08-22 20:06:08 -04004373 status = -nfs4_stat_to_errno(hdr.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 return status;
4375}
4376
4377/*
4378 * PATHCONF request
4379 */
Al Viro8687b632006-10-19 23:28:48 -07004380static int nfs4_xdr_dec_pathconf(struct rpc_rqst *req, __be32 *p, struct nfs_pathconf *pathconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381{
4382 struct xdr_stream xdr;
4383 struct compound_hdr hdr;
4384 int status;
4385
4386 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4387 status = decode_compound_hdr(&xdr, &hdr);
4388 if (!status)
4389 status = decode_putfh(&xdr);
4390 if (!status)
4391 status = decode_pathconf(&xdr, pathconf);
4392 return status;
4393}
4394
4395/*
4396 * STATFS request
4397 */
Al Viro8687b632006-10-19 23:28:48 -07004398static int nfs4_xdr_dec_statfs(struct rpc_rqst *req, __be32 *p, struct nfs_fsstat *fsstat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399{
4400 struct xdr_stream xdr;
4401 struct compound_hdr hdr;
4402 int status;
4403
4404 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4405 status = decode_compound_hdr(&xdr, &hdr);
4406 if (!status)
4407 status = decode_putfh(&xdr);
4408 if (!status)
4409 status = decode_statfs(&xdr, fsstat);
4410 return status;
4411}
4412
4413/*
4414 * GETATTR_BITMAP request
4415 */
Al Viro8687b632006-10-19 23:28:48 -07004416static int nfs4_xdr_dec_server_caps(struct rpc_rqst *req, __be32 *p, struct nfs4_server_caps_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417{
4418 struct xdr_stream xdr;
4419 struct compound_hdr hdr;
4420 int status;
4421
4422 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4423 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
4424 goto out;
4425 if ((status = decode_putfh(&xdr)) != 0)
4426 goto out;
4427 status = decode_server_caps(&xdr, res);
4428out:
4429 return status;
4430}
4431
4432/*
4433 * Decode RENEW response
4434 */
Al Viro8687b632006-10-19 23:28:48 -07004435static int nfs4_xdr_dec_renew(struct rpc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436{
4437 struct xdr_stream xdr;
4438 struct compound_hdr hdr;
4439 int status;
4440
4441 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4442 status = decode_compound_hdr(&xdr, &hdr);
4443 if (!status)
4444 status = decode_renew(&xdr);
4445 return status;
4446}
4447
4448/*
4449 * a SETCLIENTID request
4450 */
Al Viro8687b632006-10-19 23:28:48 -07004451static int nfs4_xdr_dec_setclientid(struct rpc_rqst *req, __be32 *p,
David Howellsadfa6f92006-08-22 20:06:08 -04004452 struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453{
4454 struct xdr_stream xdr;
4455 struct compound_hdr hdr;
4456 int status;
4457
4458 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4459 status = decode_compound_hdr(&xdr, &hdr);
4460 if (!status)
4461 status = decode_setclientid(&xdr, clp);
4462 if (!status)
David Howells0a8ea432006-08-22 20:06:08 -04004463 status = -nfs4_stat_to_errno(hdr.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 return status;
4465}
4466
4467/*
4468 * a SETCLIENTID_CONFIRM request
4469 */
Al Viro8687b632006-10-19 23:28:48 -07004470static int nfs4_xdr_dec_setclientid_confirm(struct rpc_rqst *req, __be32 *p, struct nfs_fsinfo *fsinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471{
4472 struct xdr_stream xdr;
4473 struct compound_hdr hdr;
4474 int status;
4475
4476 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4477 status = decode_compound_hdr(&xdr, &hdr);
4478 if (!status)
4479 status = decode_setclientid_confirm(&xdr);
4480 if (!status)
4481 status = decode_putrootfh(&xdr);
4482 if (!status)
4483 status = decode_fsinfo(&xdr, fsinfo);
4484 if (!status)
David Howells0a8ea432006-08-22 20:06:08 -04004485 status = -nfs4_stat_to_errno(hdr.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 return status;
4487}
4488
4489/*
4490 * DELEGRETURN request
4491 */
Al Viro8687b632006-10-19 23:28:48 -07004492static int nfs4_xdr_dec_delegreturn(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_delegreturnres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493{
4494 struct xdr_stream xdr;
4495 struct compound_hdr hdr;
4496 int status;
4497
4498 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4499 status = decode_compound_hdr(&xdr, &hdr);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004500 if (status != 0)
4501 goto out;
4502 status = decode_putfh(&xdr);
4503 if (status != 0)
4504 goto out;
4505 status = decode_delegreturn(&xdr);
4506 decode_getfattr(&xdr, res->fattr, res->server);
4507out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 return status;
4509}
4510
Trond Myklebust683b57b2006-06-09 09:34:22 -04004511/*
4512 * FS_LOCATIONS request
4513 */
Al Viro8687b632006-10-19 23:28:48 -07004514static int nfs4_xdr_dec_fs_locations(struct rpc_rqst *req, __be32 *p, struct nfs4_fs_locations *res)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004515{
4516 struct xdr_stream xdr;
4517 struct compound_hdr hdr;
4518 int status;
4519
4520 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4521 status = decode_compound_hdr(&xdr, &hdr);
4522 if (status != 0)
4523 goto out;
4524 if ((status = decode_putfh(&xdr)) != 0)
4525 goto out;
4526 if ((status = decode_lookup(&xdr)) != 0)
4527 goto out;
4528 xdr_enter_page(&xdr, PAGE_SIZE);
4529 status = decode_getfattr(&xdr, &res->fattr, res->server);
4530out:
4531 return status;
4532}
4533
Al Viro0dbb4c62006-10-19 23:28:49 -07004534__be32 *nfs4_decode_dirent(__be32 *p, struct nfs_entry *entry, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535{
4536 uint32_t bitmap[2] = {0};
4537 uint32_t len;
4538
4539 if (!*p++) {
4540 if (!*p)
4541 return ERR_PTR(-EAGAIN);
4542 entry->eof = 1;
4543 return ERR_PTR(-EBADCOOKIE);
4544 }
4545
4546 entry->prev_cookie = entry->cookie;
4547 p = xdr_decode_hyper(p, &entry->cookie);
4548 entry->len = ntohl(*p++);
4549 entry->name = (const char *) p;
4550 p += XDR_QUADLEN(entry->len);
4551
4552 /*
4553 * In case the server doesn't return an inode number,
4554 * we fake one here. (We don't use inode number 0,
4555 * since glibc seems to choke on it...)
4556 */
4557 entry->ino = 1;
4558
4559 len = ntohl(*p++); /* bitmap length */
4560 if (len-- > 0) {
4561 bitmap[0] = ntohl(*p++);
4562 if (len-- > 0) {
4563 bitmap[1] = ntohl(*p++);
4564 p += len;
4565 }
4566 }
4567 len = XDR_QUADLEN(ntohl(*p++)); /* attribute buffer length */
4568 if (len > 0) {
Manoj Naik97d312d2005-06-22 17:16:39 +00004569 if (bitmap[0] & FATTR4_WORD0_RDATTR_ERROR) {
4570 bitmap[0] &= ~FATTR4_WORD0_RDATTR_ERROR;
4571 /* Ignore the return value of rdattr_error for now */
4572 p++;
4573 len--;
4574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 if (bitmap[0] == 0 && bitmap[1] == FATTR4_WORD1_MOUNTED_ON_FILEID)
4576 xdr_decode_hyper(p, &entry->ino);
4577 else if (bitmap[0] == FATTR4_WORD0_FILEID)
4578 xdr_decode_hyper(p, &entry->ino);
4579 p += len;
4580 }
4581
4582 entry->eof = !p[0] && p[1];
4583 return p;
4584}
4585
4586/*
4587 * We need to translate between nfs status return values and
4588 * the local errno values which may not be the same.
4589 */
4590static struct {
4591 int stat;
4592 int errno;
4593} nfs_errtbl[] = {
4594 { NFS4_OK, 0 },
4595 { NFS4ERR_PERM, EPERM },
4596 { NFS4ERR_NOENT, ENOENT },
4597 { NFS4ERR_IO, errno_NFSERR_IO },
4598 { NFS4ERR_NXIO, ENXIO },
4599 { NFS4ERR_ACCESS, EACCES },
4600 { NFS4ERR_EXIST, EEXIST },
4601 { NFS4ERR_XDEV, EXDEV },
4602 { NFS4ERR_NOTDIR, ENOTDIR },
4603 { NFS4ERR_ISDIR, EISDIR },
4604 { NFS4ERR_INVAL, EINVAL },
4605 { NFS4ERR_FBIG, EFBIG },
4606 { NFS4ERR_NOSPC, ENOSPC },
4607 { NFS4ERR_ROFS, EROFS },
4608 { NFS4ERR_MLINK, EMLINK },
4609 { NFS4ERR_NAMETOOLONG, ENAMETOOLONG },
4610 { NFS4ERR_NOTEMPTY, ENOTEMPTY },
4611 { NFS4ERR_DQUOT, EDQUOT },
4612 { NFS4ERR_STALE, ESTALE },
4613 { NFS4ERR_BADHANDLE, EBADHANDLE },
Manoj Naik6ebf3652005-06-22 17:16:39 +00004614 { NFS4ERR_BADOWNER, EINVAL },
4615 { NFS4ERR_BADNAME, EINVAL },
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 { NFS4ERR_BAD_COOKIE, EBADCOOKIE },
4617 { NFS4ERR_NOTSUPP, ENOTSUPP },
4618 { NFS4ERR_TOOSMALL, ETOOSMALL },
4619 { NFS4ERR_SERVERFAULT, ESERVERFAULT },
4620 { NFS4ERR_BADTYPE, EBADTYPE },
4621 { NFS4ERR_LOCKED, EAGAIN },
4622 { NFS4ERR_RESOURCE, EREMOTEIO },
4623 { NFS4ERR_SYMLINK, ELOOP },
4624 { NFS4ERR_OP_ILLEGAL, EOPNOTSUPP },
4625 { NFS4ERR_DEADLOCK, EDEADLK },
4626 { NFS4ERR_WRONGSEC, EPERM }, /* FIXME: this needs
4627 * to be handled by a
4628 * middle-layer.
4629 */
4630 { -1, EIO }
4631};
4632
4633/*
4634 * Convert an NFS error code to a local one.
4635 * This one is used jointly by NFSv2 and NFSv3.
4636 */
4637static int
David Howells0a8ea432006-08-22 20:06:08 -04004638nfs4_stat_to_errno(int stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639{
4640 int i;
4641 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
4642 if (nfs_errtbl[i].stat == stat)
4643 return nfs_errtbl[i].errno;
4644 }
4645 if (stat <= 10000 || stat > 10100) {
4646 /* The server is looney tunes. */
4647 return ESERVERFAULT;
4648 }
4649 /* If we cannot translate the error, the recovery routines should
4650 * handle it.
4651 * Note: remaining NFSv4 error codes have values > 10000, so should
4652 * not conflict with native Linux error codes.
4653 */
4654 return stat;
4655}
4656
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657#define PROC(proc, argtype, restype) \
4658[NFSPROC4_CLNT_##proc] = { \
4659 .p_proc = NFSPROC4_COMPOUND, \
4660 .p_encode = (kxdrproc_t) nfs4_xdr_##argtype, \
4661 .p_decode = (kxdrproc_t) nfs4_xdr_##restype, \
Chuck Lever2bea90d2007-03-29 16:47:53 -04004662 .p_arglen = NFS4_##argtype##_sz, \
4663 .p_replen = NFS4_##restype##_sz, \
Chuck Levercc0175c2006-03-20 13:44:22 -05004664 .p_statidx = NFSPROC4_CLNT_##proc, \
4665 .p_name = #proc, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 }
4667
4668struct rpc_procinfo nfs4_procedures[] = {
4669 PROC(READ, enc_read, dec_read),
4670 PROC(WRITE, enc_write, dec_write),
4671 PROC(COMMIT, enc_commit, dec_commit),
4672 PROC(OPEN, enc_open, dec_open),
4673 PROC(OPEN_CONFIRM, enc_open_confirm, dec_open_confirm),
4674 PROC(OPEN_NOATTR, enc_open_noattr, dec_open_noattr),
4675 PROC(OPEN_DOWNGRADE, enc_open_downgrade, dec_open_downgrade),
4676 PROC(CLOSE, enc_close, dec_close),
4677 PROC(SETATTR, enc_setattr, dec_setattr),
4678 PROC(FSINFO, enc_fsinfo, dec_fsinfo),
4679 PROC(RENEW, enc_renew, dec_renew),
4680 PROC(SETCLIENTID, enc_setclientid, dec_setclientid),
4681 PROC(SETCLIENTID_CONFIRM, enc_setclientid_confirm, dec_setclientid_confirm),
4682 PROC(LOCK, enc_lock, dec_lock),
4683 PROC(LOCKT, enc_lockt, dec_lockt),
4684 PROC(LOCKU, enc_locku, dec_locku),
4685 PROC(ACCESS, enc_access, dec_access),
4686 PROC(GETATTR, enc_getattr, dec_getattr),
4687 PROC(LOOKUP, enc_lookup, dec_lookup),
4688 PROC(LOOKUP_ROOT, enc_lookup_root, dec_lookup_root),
4689 PROC(REMOVE, enc_remove, dec_remove),
4690 PROC(RENAME, enc_rename, dec_rename),
4691 PROC(LINK, enc_link, dec_link),
4692 PROC(SYMLINK, enc_symlink, dec_symlink),
4693 PROC(CREATE, enc_create, dec_create),
4694 PROC(PATHCONF, enc_pathconf, dec_pathconf),
4695 PROC(STATFS, enc_statfs, dec_statfs),
4696 PROC(READLINK, enc_readlink, dec_readlink),
4697 PROC(READDIR, enc_readdir, dec_readdir),
4698 PROC(SERVER_CAPS, enc_server_caps, dec_server_caps),
4699 PROC(DELEGRETURN, enc_delegreturn, dec_delegreturn),
J. Bruce Fields029d1052005-06-22 17:16:22 +00004700 PROC(GETACL, enc_getacl, dec_getacl),
J. Bruce Fields23ec6962005-06-22 17:16:22 +00004701 PROC(SETACL, enc_setacl, dec_setacl),
Trond Myklebust683b57b2006-06-09 09:34:22 -04004702 PROC(FS_LOCATIONS, enc_fs_locations, dec_fs_locations),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703};
4704
4705struct rpc_version nfs_version4 = {
4706 .number = 4,
Tobias Klausere8c96f82006-03-24 03:15:34 -08004707 .nrprocs = ARRAY_SIZE(nfs4_procedures),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 .procs = nfs4_procedures
4709};
4710
4711/*
4712 * Local variables:
4713 * c-basic-offset: 8
4714 * End:
4715 */