blob: 2e1fe171bf73ea9e7b8af75476b4f3a4e53fa919 [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 Myklebust76b32992007-08-10 17:45:11 -0400379 encode_access_maxsz + \
380 encode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381#define NFS4_dec_access_sz (compound_decode_hdr_maxsz + \
382 decode_putfh_maxsz + \
Trond Myklebust76b32992007-08-10 17:45:11 -0400383 decode_access_maxsz + \
384 decode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385#define NFS4_enc_getattr_sz (compound_encode_hdr_maxsz + \
386 encode_putfh_maxsz + \
387 encode_getattr_maxsz)
388#define NFS4_dec_getattr_sz (compound_decode_hdr_maxsz + \
389 decode_putfh_maxsz + \
390 decode_getattr_maxsz)
391#define NFS4_enc_lookup_sz (compound_encode_hdr_maxsz + \
392 encode_putfh_maxsz + \
393 encode_lookup_maxsz + \
394 encode_getattr_maxsz + \
395 encode_getfh_maxsz)
396#define NFS4_dec_lookup_sz (compound_decode_hdr_maxsz + \
397 decode_putfh_maxsz + \
Trond Myklebuste6889622007-07-02 13:58:30 -0400398 decode_lookup_maxsz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 decode_getattr_maxsz + \
400 decode_getfh_maxsz)
401#define NFS4_enc_lookup_root_sz (compound_encode_hdr_maxsz + \
402 encode_putrootfh_maxsz + \
403 encode_getattr_maxsz + \
404 encode_getfh_maxsz)
405#define NFS4_dec_lookup_root_sz (compound_decode_hdr_maxsz + \
406 decode_putrootfh_maxsz + \
407 decode_getattr_maxsz + \
408 decode_getfh_maxsz)
409#define NFS4_enc_remove_sz (compound_encode_hdr_maxsz + \
410 encode_putfh_maxsz + \
Trond Myklebust16e42952005-10-27 22:12:44 -0400411 encode_remove_maxsz + \
412 encode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413#define NFS4_dec_remove_sz (compound_decode_hdr_maxsz + \
414 decode_putfh_maxsz + \
Trond Myklebust16e42952005-10-27 22:12:44 -0400415 op_decode_hdr_maxsz + 5 + \
416 decode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417#define NFS4_enc_rename_sz (compound_encode_hdr_maxsz + \
418 encode_putfh_maxsz + \
419 encode_savefh_maxsz + \
420 encode_putfh_maxsz + \
Trond Myklebust6caf2c82005-10-27 22:12:43 -0400421 encode_rename_maxsz + \
422 encode_getattr_maxsz + \
423 encode_restorefh_maxsz + \
424 encode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425#define NFS4_dec_rename_sz (compound_decode_hdr_maxsz + \
426 decode_putfh_maxsz + \
427 decode_savefh_maxsz + \
428 decode_putfh_maxsz + \
Trond Myklebust6caf2c82005-10-27 22:12:43 -0400429 decode_rename_maxsz + \
430 decode_getattr_maxsz + \
431 decode_restorefh_maxsz + \
432 decode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433#define NFS4_enc_link_sz (compound_encode_hdr_maxsz + \
434 encode_putfh_maxsz + \
435 encode_savefh_maxsz + \
436 encode_putfh_maxsz + \
Trond Myklebust91ba2ee2005-10-27 22:12:42 -0400437 encode_link_maxsz + \
438 decode_getattr_maxsz + \
439 encode_restorefh_maxsz + \
440 decode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441#define NFS4_dec_link_sz (compound_decode_hdr_maxsz + \
442 decode_putfh_maxsz + \
443 decode_savefh_maxsz + \
444 decode_putfh_maxsz + \
Trond Myklebust91ba2ee2005-10-27 22:12:42 -0400445 decode_link_maxsz + \
446 decode_getattr_maxsz + \
447 decode_restorefh_maxsz + \
448 decode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449#define NFS4_enc_symlink_sz (compound_encode_hdr_maxsz + \
450 encode_putfh_maxsz + \
451 encode_symlink_maxsz + \
452 encode_getattr_maxsz + \
453 encode_getfh_maxsz)
454#define NFS4_dec_symlink_sz (compound_decode_hdr_maxsz + \
455 decode_putfh_maxsz + \
456 decode_symlink_maxsz + \
457 decode_getattr_maxsz + \
458 decode_getfh_maxsz)
459#define NFS4_enc_create_sz (compound_encode_hdr_maxsz + \
460 encode_putfh_maxsz + \
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400461 encode_savefh_maxsz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 encode_create_maxsz + \
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400463 encode_getfh_maxsz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 encode_getattr_maxsz + \
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400465 encode_restorefh_maxsz + \
466 encode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467#define NFS4_dec_create_sz (compound_decode_hdr_maxsz + \
468 decode_putfh_maxsz + \
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400469 decode_savefh_maxsz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 decode_create_maxsz + \
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400471 decode_getfh_maxsz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 decode_getattr_maxsz + \
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400473 decode_restorefh_maxsz + \
474 decode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475#define NFS4_enc_pathconf_sz (compound_encode_hdr_maxsz + \
476 encode_putfh_maxsz + \
477 encode_getattr_maxsz)
478#define NFS4_dec_pathconf_sz (compound_decode_hdr_maxsz + \
479 decode_putfh_maxsz + \
480 decode_getattr_maxsz)
481#define NFS4_enc_statfs_sz (compound_encode_hdr_maxsz + \
482 encode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400483 encode_statfs_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484#define NFS4_dec_statfs_sz (compound_decode_hdr_maxsz + \
485 decode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400486 decode_statfs_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487#define NFS4_enc_server_caps_sz (compound_encode_hdr_maxsz + \
Trond Myklebustab91f262007-02-02 14:47:17 -0800488 encode_putfh_maxsz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 encode_getattr_maxsz)
490#define NFS4_dec_server_caps_sz (compound_decode_hdr_maxsz + \
Trond Myklebustab91f262007-02-02 14:47:17 -0800491 decode_putfh_maxsz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 decode_getattr_maxsz)
493#define NFS4_enc_delegreturn_sz (compound_encode_hdr_maxsz + \
494 encode_putfh_maxsz + \
Trond Myklebustfa178f22006-01-03 09:55:38 +0100495 encode_delegreturn_maxsz + \
496 encode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497#define NFS4_dec_delegreturn_sz (compound_decode_hdr_maxsz + \
Trond Myklebustfa178f22006-01-03 09:55:38 +0100498 decode_delegreturn_maxsz + \
499 decode_getattr_maxsz)
J. Bruce Fields029d1052005-06-22 17:16:22 +0000500#define NFS4_enc_getacl_sz (compound_encode_hdr_maxsz + \
501 encode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400502 encode_getacl_maxsz)
J. Bruce Fields029d1052005-06-22 17:16:22 +0000503#define NFS4_dec_getacl_sz (compound_decode_hdr_maxsz + \
504 decode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400505 decode_getacl_maxsz)
J. Bruce Fields23ec6962005-06-22 17:16:22 +0000506#define NFS4_enc_setacl_sz (compound_encode_hdr_maxsz + \
507 encode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400508 encode_setacl_maxsz)
J. Bruce Fields23ec6962005-06-22 17:16:22 +0000509#define NFS4_dec_setacl_sz (compound_decode_hdr_maxsz + \
510 decode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400511 decode_setacl_maxsz)
Trond Myklebust683b57b2006-06-09 09:34:22 -0400512#define NFS4_enc_fs_locations_sz \
513 (compound_encode_hdr_maxsz + \
514 encode_putfh_maxsz + \
Trond Myklebuste6889622007-07-02 13:58:30 -0400515 encode_lookup_maxsz + \
516 encode_fs_locations_maxsz)
Trond Myklebust683b57b2006-06-09 09:34:22 -0400517#define NFS4_dec_fs_locations_sz \
518 (compound_decode_hdr_maxsz + \
519 decode_putfh_maxsz + \
Trond Myklebuste6889622007-07-02 13:58:30 -0400520 decode_lookup_maxsz + \
521 decode_fs_locations_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523static struct {
524 unsigned int mode;
525 unsigned int nfs2type;
526} nfs_type2fmt[] = {
527 { 0, NFNON },
528 { S_IFREG, NFREG },
529 { S_IFDIR, NFDIR },
530 { S_IFBLK, NFBLK },
531 { S_IFCHR, NFCHR },
532 { S_IFLNK, NFLNK },
533 { S_IFSOCK, NFSOCK },
534 { S_IFIFO, NFFIFO },
535 { 0, NFNON },
536 { 0, NFNON },
537};
538
539struct compound_hdr {
540 int32_t status;
541 uint32_t nops;
542 uint32_t taglen;
543 char * tag;
544};
545
546/*
547 * START OF "GENERIC" ENCODE ROUTINES.
548 * These may look a little ugly since they are imported from a "generic"
549 * set of XDR encode/decode routines which are intended to be shared by
550 * all of our NFSv4 implementations (OpenBSD, MacOS X...).
551 *
552 * If the pain of reading these is too great, it should be a straightforward
553 * task to translate them into Linux-specific versions which are more
554 * consistent with the style used in NFSv2/v3...
555 */
556#define WRITE32(n) *p++ = htonl(n)
557#define WRITE64(n) do { \
558 *p++ = htonl((uint32_t)((n) >> 32)); \
559 *p++ = htonl((uint32_t)(n)); \
560} while (0)
561#define WRITEMEM(ptr,nbytes) do { \
562 p = xdr_encode_opaque_fixed(p, ptr, nbytes); \
563} while (0)
564
565#define RESERVE_SPACE(nbytes) do { \
566 p = xdr_reserve_space(xdr, nbytes); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 BUG_ON(!p); \
568} while (0)
569
570static void encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
571{
Al Viro8687b632006-10-19 23:28:48 -0700572 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 p = xdr_reserve_space(xdr, 4 + len);
575 BUG_ON(p == NULL);
576 xdr_encode_opaque(p, str, len);
577}
578
579static int encode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
580{
Al Viro8687b632006-10-19 23:28:48 -0700581 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 dprintk("encode_compound: tag=%.*s\n", (int)hdr->taglen, hdr->tag);
584 BUG_ON(hdr->taglen > NFS4_MAXTAGLEN);
585 RESERVE_SPACE(12+(XDR_QUADLEN(hdr->taglen)<<2));
586 WRITE32(hdr->taglen);
587 WRITEMEM(hdr->tag, hdr->taglen);
588 WRITE32(NFS4_MINOR_VERSION);
589 WRITE32(hdr->nops);
590 return 0;
591}
592
593static void encode_nfs4_verifier(struct xdr_stream *xdr, const nfs4_verifier *verf)
594{
Al Viro8687b632006-10-19 23:28:48 -0700595 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
598 BUG_ON(p == NULL);
599 xdr_encode_opaque_fixed(p, verf->data, NFS4_VERIFIER_SIZE);
600}
601
602static int encode_attrs(struct xdr_stream *xdr, const struct iattr *iap, const struct nfs_server *server)
603{
604 char owner_name[IDMAP_NAMESZ];
605 char owner_group[IDMAP_NAMESZ];
606 int owner_namelen = 0;
607 int owner_grouplen = 0;
Al Viro8687b632006-10-19 23:28:48 -0700608 __be32 *p;
609 __be32 *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 int len;
611 uint32_t bmval0 = 0;
612 uint32_t bmval1 = 0;
613 int status;
614
615 /*
616 * We reserve enough space to write the entire attribute buffer at once.
617 * In the worst-case, this would be
618 * 12(bitmap) + 4(attrlen) + 8(size) + 4(mode) + 4(atime) + 4(mtime)
619 * = 36 bytes, plus any contribution from variable-length fields
J. Bruce Fields23ec6962005-06-22 17:16:22 +0000620 * such as owner/group.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 */
622 len = 16;
623
624 /* Sigh */
625 if (iap->ia_valid & ATTR_SIZE)
626 len += 8;
627 if (iap->ia_valid & ATTR_MODE)
628 len += 4;
629 if (iap->ia_valid & ATTR_UID) {
David Howells7539bba2006-08-22 20:06:09 -0400630 owner_namelen = nfs_map_uid_to_name(server->nfs_client, iap->ia_uid, owner_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (owner_namelen < 0) {
Chuck Leverfe82a182007-09-11 18:01:10 -0400632 dprintk("nfs: couldn't resolve uid %d to string\n",
633 iap->ia_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 /* XXX */
635 strcpy(owner_name, "nobody");
636 owner_namelen = sizeof("nobody") - 1;
637 /* goto out; */
638 }
639 len += 4 + (XDR_QUADLEN(owner_namelen) << 2);
640 }
641 if (iap->ia_valid & ATTR_GID) {
David Howells7539bba2006-08-22 20:06:09 -0400642 owner_grouplen = nfs_map_gid_to_group(server->nfs_client, iap->ia_gid, owner_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (owner_grouplen < 0) {
Chuck Leverfe82a182007-09-11 18:01:10 -0400644 dprintk("nfs: couldn't resolve gid %d to string\n",
645 iap->ia_gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 strcpy(owner_group, "nobody");
647 owner_grouplen = sizeof("nobody") - 1;
648 /* goto out; */
649 }
650 len += 4 + (XDR_QUADLEN(owner_grouplen) << 2);
651 }
652 if (iap->ia_valid & ATTR_ATIME_SET)
653 len += 16;
654 else if (iap->ia_valid & ATTR_ATIME)
655 len += 4;
656 if (iap->ia_valid & ATTR_MTIME_SET)
657 len += 16;
658 else if (iap->ia_valid & ATTR_MTIME)
659 len += 4;
660 RESERVE_SPACE(len);
661
662 /*
663 * We write the bitmap length now, but leave the bitmap and the attribute
664 * buffer length to be backfilled at the end of this routine.
665 */
666 WRITE32(2);
667 q = p;
668 p += 3;
669
670 if (iap->ia_valid & ATTR_SIZE) {
671 bmval0 |= FATTR4_WORD0_SIZE;
672 WRITE64(iap->ia_size);
673 }
674 if (iap->ia_valid & ATTR_MODE) {
675 bmval1 |= FATTR4_WORD1_MODE;
Trond Myklebustcf3fff52006-01-03 09:55:53 +0100676 WRITE32(iap->ia_mode & S_IALLUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678 if (iap->ia_valid & ATTR_UID) {
679 bmval1 |= FATTR4_WORD1_OWNER;
680 WRITE32(owner_namelen);
681 WRITEMEM(owner_name, owner_namelen);
682 }
683 if (iap->ia_valid & ATTR_GID) {
684 bmval1 |= FATTR4_WORD1_OWNER_GROUP;
685 WRITE32(owner_grouplen);
686 WRITEMEM(owner_group, owner_grouplen);
687 }
688 if (iap->ia_valid & ATTR_ATIME_SET) {
689 bmval1 |= FATTR4_WORD1_TIME_ACCESS_SET;
690 WRITE32(NFS4_SET_TO_CLIENT_TIME);
691 WRITE32(0);
692 WRITE32(iap->ia_mtime.tv_sec);
693 WRITE32(iap->ia_mtime.tv_nsec);
694 }
695 else if (iap->ia_valid & ATTR_ATIME) {
696 bmval1 |= FATTR4_WORD1_TIME_ACCESS_SET;
697 WRITE32(NFS4_SET_TO_SERVER_TIME);
698 }
699 if (iap->ia_valid & ATTR_MTIME_SET) {
700 bmval1 |= FATTR4_WORD1_TIME_MODIFY_SET;
701 WRITE32(NFS4_SET_TO_CLIENT_TIME);
702 WRITE32(0);
703 WRITE32(iap->ia_mtime.tv_sec);
704 WRITE32(iap->ia_mtime.tv_nsec);
705 }
706 else if (iap->ia_valid & ATTR_MTIME) {
707 bmval1 |= FATTR4_WORD1_TIME_MODIFY_SET;
708 WRITE32(NFS4_SET_TO_SERVER_TIME);
709 }
710
711 /*
712 * Now we backfill the bitmap and the attribute buffer length.
713 */
714 if (len != ((char *)p - (char *)q) + 4) {
Chuck Leverfe82a182007-09-11 18:01:10 -0400715 printk(KERN_ERR "nfs: Attr length error, %u != %Zu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 len, ((char *)p - (char *)q) + 4);
717 BUG();
718 }
719 len = (char *)p - (char *)q - 12;
720 *q++ = htonl(bmval0);
721 *q++ = htonl(bmval1);
722 *q++ = htonl(len);
723
724 status = 0;
725/* out: */
726 return status;
727}
728
729static int encode_access(struct xdr_stream *xdr, u32 access)
730{
Al Viro8687b632006-10-19 23:28:48 -0700731 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 RESERVE_SPACE(8);
734 WRITE32(OP_ACCESS);
735 WRITE32(access);
736
737 return 0;
738}
739
740static int encode_close(struct xdr_stream *xdr, const struct nfs_closeargs *arg)
741{
Al Viro8687b632006-10-19 23:28:48 -0700742 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Trond Myklebust8ae20ab2007-05-14 16:50:45 -0400744 RESERVE_SPACE(8+NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 WRITE32(OP_CLOSE);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700746 WRITE32(arg->seqid->sequence->counter);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -0400747 WRITEMEM(arg->stateid->data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 return 0;
750}
751
752static int encode_commit(struct xdr_stream *xdr, const struct nfs_writeargs *args)
753{
Al Viro8687b632006-10-19 23:28:48 -0700754 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 RESERVE_SPACE(16);
757 WRITE32(OP_COMMIT);
758 WRITE64(args->offset);
759 WRITE32(args->count);
760
761 return 0;
762}
763
764static int encode_create(struct xdr_stream *xdr, const struct nfs4_create_arg *create)
765{
Al Viro8687b632006-10-19 23:28:48 -0700766 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 RESERVE_SPACE(8);
769 WRITE32(OP_CREATE);
770 WRITE32(create->ftype);
771
772 switch (create->ftype) {
773 case NF4LNK:
Chuck Lever94a6d752006-08-22 20:06:23 -0400774 RESERVE_SPACE(4);
775 WRITE32(create->u.symlink.len);
776 xdr_write_pages(xdr, create->u.symlink.pages, 0, create->u.symlink.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 break;
778
779 case NF4BLK: case NF4CHR:
780 RESERVE_SPACE(8);
781 WRITE32(create->u.device.specdata1);
782 WRITE32(create->u.device.specdata2);
783 break;
784
785 default:
786 break;
787 }
788
789 RESERVE_SPACE(4 + create->name->len);
790 WRITE32(create->name->len);
791 WRITEMEM(create->name->name, create->name->len);
792
793 return encode_attrs(xdr, create->attrs, create->server);
794}
795
796static int encode_getattr_one(struct xdr_stream *xdr, uint32_t bitmap)
797{
Al Viro8687b632006-10-19 23:28:48 -0700798 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 RESERVE_SPACE(12);
801 WRITE32(OP_GETATTR);
802 WRITE32(1);
803 WRITE32(bitmap);
804 return 0;
805}
806
807static int encode_getattr_two(struct xdr_stream *xdr, uint32_t bm0, uint32_t bm1)
808{
Al Viro8687b632006-10-19 23:28:48 -0700809 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 RESERVE_SPACE(16);
812 WRITE32(OP_GETATTR);
813 WRITE32(2);
814 WRITE32(bm0);
815 WRITE32(bm1);
816 return 0;
817}
818
819static int encode_getfattr(struct xdr_stream *xdr, const u32* bitmask)
820{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return encode_getattr_two(xdr,
822 bitmask[0] & nfs4_fattr_bitmap[0],
823 bitmask[1] & nfs4_fattr_bitmap[1]);
824}
825
826static int encode_fsinfo(struct xdr_stream *xdr, const u32* bitmask)
827{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return encode_getattr_two(xdr, bitmask[0] & nfs4_fsinfo_bitmap[0],
829 bitmask[1] & nfs4_fsinfo_bitmap[1]);
830}
831
Manoj Naik830b8e32006-06-09 09:34:25 -0400832static int encode_fs_locations(struct xdr_stream *xdr, const u32* bitmask)
833{
834 return encode_getattr_two(xdr,
835 bitmask[0] & nfs4_fs_locations_bitmap[0],
836 bitmask[1] & nfs4_fs_locations_bitmap[1]);
837}
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839static int encode_getfh(struct xdr_stream *xdr)
840{
Al Viro8687b632006-10-19 23:28:48 -0700841 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 RESERVE_SPACE(4);
844 WRITE32(OP_GETFH);
845
846 return 0;
847}
848
849static int encode_link(struct xdr_stream *xdr, const struct qstr *name)
850{
Al Viro8687b632006-10-19 23:28:48 -0700851 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 RESERVE_SPACE(8 + name->len);
854 WRITE32(OP_LINK);
855 WRITE32(name->len);
856 WRITEMEM(name->name, name->len);
857
858 return 0;
859}
860
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100861static inline int nfs4_lock_type(struct file_lock *fl, int block)
862{
863 if ((fl->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) == F_RDLCK)
864 return block ? NFS4_READW_LT : NFS4_READ_LT;
865 return block ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
866}
867
868static inline uint64_t nfs4_lock_length(struct file_lock *fl)
869{
870 if (fl->fl_end == OFFSET_MAX)
871 return ~(uint64_t)0;
872 return fl->fl_end - fl->fl_start + 1;
873}
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875/*
876 * opcode,type,reclaim,offset,length,new_lock_owner = 32
877 * open_seqid,open_stateid,lock_seqid,lock_owner.clientid, lock_owner.id = 40
878 */
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100879static int encode_lock(struct xdr_stream *xdr, const struct nfs_lock_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
Al Viro8687b632006-10-19 23:28:48 -0700881 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 RESERVE_SPACE(32);
884 WRITE32(OP_LOCK);
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100885 WRITE32(nfs4_lock_type(args->fl, args->block));
886 WRITE32(args->reclaim);
887 WRITE64(args->fl->fl_start);
888 WRITE64(nfs4_lock_length(args->fl));
889 WRITE32(args->new_lock_owner);
890 if (args->new_lock_owner){
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400891 RESERVE_SPACE(4+NFS4_STATEID_SIZE+32);
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100892 WRITE32(args->open_seqid->sequence->counter);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -0400893 WRITEMEM(args->open_stateid->data, NFS4_STATEID_SIZE);
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100894 WRITE32(args->lock_seqid->sequence->counter);
895 WRITE64(args->lock_owner.clientid);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400896 WRITE32(16);
897 WRITEMEM("lock id:", 8);
898 WRITE64(args->lock_owner.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
900 else {
Trond Myklebust8ae20ab2007-05-14 16:50:45 -0400901 RESERVE_SPACE(NFS4_STATEID_SIZE+4);
902 WRITEMEM(args->lock_stateid->data, NFS4_STATEID_SIZE);
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100903 WRITE32(args->lock_seqid->sequence->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905
906 return 0;
907}
908
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100909static int encode_lockt(struct xdr_stream *xdr, const struct nfs_lockt_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Al Viro8687b632006-10-19 23:28:48 -0700911 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400913 RESERVE_SPACE(52);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 WRITE32(OP_LOCKT);
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100915 WRITE32(nfs4_lock_type(args->fl, 0));
916 WRITE64(args->fl->fl_start);
917 WRITE64(nfs4_lock_length(args->fl));
918 WRITE64(args->lock_owner.clientid);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400919 WRITE32(16);
920 WRITEMEM("lock id:", 8);
921 WRITE64(args->lock_owner.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 return 0;
924}
925
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100926static int encode_locku(struct xdr_stream *xdr, const struct nfs_locku_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Al Viro8687b632006-10-19 23:28:48 -0700928 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Trond Myklebust8ae20ab2007-05-14 16:50:45 -0400930 RESERVE_SPACE(12+NFS4_STATEID_SIZE+16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 WRITE32(OP_LOCKU);
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100932 WRITE32(nfs4_lock_type(args->fl, 0));
933 WRITE32(args->seqid->sequence->counter);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -0400934 WRITEMEM(args->stateid->data, NFS4_STATEID_SIZE);
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100935 WRITE64(args->fl->fl_start);
936 WRITE64(nfs4_lock_length(args->fl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 return 0;
939}
940
941static int encode_lookup(struct xdr_stream *xdr, const struct qstr *name)
942{
943 int len = name->len;
Al Viro8687b632006-10-19 23:28:48 -0700944 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 RESERVE_SPACE(8 + len);
947 WRITE32(OP_LOOKUP);
948 WRITE32(len);
949 WRITEMEM(name->name, len);
950
951 return 0;
952}
953
954static void encode_share_access(struct xdr_stream *xdr, int open_flags)
955{
Al Viro8687b632006-10-19 23:28:48 -0700956 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 RESERVE_SPACE(8);
959 switch (open_flags & (FMODE_READ|FMODE_WRITE)) {
960 case FMODE_READ:
961 WRITE32(NFS4_SHARE_ACCESS_READ);
962 break;
963 case FMODE_WRITE:
964 WRITE32(NFS4_SHARE_ACCESS_WRITE);
965 break;
966 case FMODE_READ|FMODE_WRITE:
967 WRITE32(NFS4_SHARE_ACCESS_BOTH);
968 break;
969 default:
970 BUG();
971 }
972 WRITE32(0); /* for linux, share_deny = 0 always */
973}
974
975static inline void encode_openhdr(struct xdr_stream *xdr, const struct nfs_openargs *arg)
976{
Al Viro8687b632006-10-19 23:28:48 -0700977 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 /*
979 * opcode 4, seqid 4, share_access 4, share_deny 4, clientid 8, ownerlen 4,
980 * owner 4 = 32
981 */
982 RESERVE_SPACE(8);
983 WRITE32(OP_OPEN);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700984 WRITE32(arg->seqid->sequence->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 encode_share_access(xdr, arg->open_flags);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400986 RESERVE_SPACE(28);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 WRITE64(arg->clientid);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400988 WRITE32(16);
989 WRITEMEM("open id:", 8);
990 WRITE64(arg->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991}
992
993static inline void encode_createmode(struct xdr_stream *xdr, const struct nfs_openargs *arg)
994{
Al Viro8687b632006-10-19 23:28:48 -0700995 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 RESERVE_SPACE(4);
998 switch(arg->open_flags & O_EXCL) {
999 case 0:
1000 WRITE32(NFS4_CREATE_UNCHECKED);
1001 encode_attrs(xdr, arg->u.attrs, arg->server);
1002 break;
1003 default:
1004 WRITE32(NFS4_CREATE_EXCLUSIVE);
1005 encode_nfs4_verifier(xdr, &arg->u.verifier);
1006 }
1007}
1008
1009static void encode_opentype(struct xdr_stream *xdr, const struct nfs_openargs *arg)
1010{
Al Viro8687b632006-10-19 23:28:48 -07001011 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 RESERVE_SPACE(4);
1014 switch (arg->open_flags & O_CREAT) {
1015 case 0:
1016 WRITE32(NFS4_OPEN_NOCREATE);
1017 break;
1018 default:
1019 BUG_ON(arg->claim != NFS4_OPEN_CLAIM_NULL);
1020 WRITE32(NFS4_OPEN_CREATE);
1021 encode_createmode(xdr, arg);
1022 }
1023}
1024
1025static inline void encode_delegation_type(struct xdr_stream *xdr, int delegation_type)
1026{
Al Viro8687b632006-10-19 23:28:48 -07001027 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 RESERVE_SPACE(4);
1030 switch (delegation_type) {
1031 case 0:
1032 WRITE32(NFS4_OPEN_DELEGATE_NONE);
1033 break;
1034 case FMODE_READ:
1035 WRITE32(NFS4_OPEN_DELEGATE_READ);
1036 break;
1037 case FMODE_WRITE|FMODE_READ:
1038 WRITE32(NFS4_OPEN_DELEGATE_WRITE);
1039 break;
1040 default:
1041 BUG();
1042 }
1043}
1044
1045static inline void encode_claim_null(struct xdr_stream *xdr, const struct qstr *name)
1046{
Al Viro8687b632006-10-19 23:28:48 -07001047 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 RESERVE_SPACE(4);
1050 WRITE32(NFS4_OPEN_CLAIM_NULL);
1051 encode_string(xdr, name->len, name->name);
1052}
1053
1054static inline void encode_claim_previous(struct xdr_stream *xdr, int type)
1055{
Al Viro8687b632006-10-19 23:28:48 -07001056 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 RESERVE_SPACE(4);
1059 WRITE32(NFS4_OPEN_CLAIM_PREVIOUS);
1060 encode_delegation_type(xdr, type);
1061}
1062
1063static inline void encode_claim_delegate_cur(struct xdr_stream *xdr, const struct qstr *name, const nfs4_stateid *stateid)
1064{
Al Viro8687b632006-10-19 23:28:48 -07001065 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001067 RESERVE_SPACE(4+NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 WRITE32(NFS4_OPEN_CLAIM_DELEGATE_CUR);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001069 WRITEMEM(stateid->data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 encode_string(xdr, name->len, name->name);
1071}
1072
1073static int encode_open(struct xdr_stream *xdr, const struct nfs_openargs *arg)
1074{
1075 encode_openhdr(xdr, arg);
1076 encode_opentype(xdr, arg);
1077 switch (arg->claim) {
1078 case NFS4_OPEN_CLAIM_NULL:
1079 encode_claim_null(xdr, arg->name);
1080 break;
1081 case NFS4_OPEN_CLAIM_PREVIOUS:
1082 encode_claim_previous(xdr, arg->u.delegation_type);
1083 break;
1084 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1085 encode_claim_delegate_cur(xdr, arg->name, &arg->u.delegation);
1086 break;
1087 default:
1088 BUG();
1089 }
1090 return 0;
1091}
1092
1093static int encode_open_confirm(struct xdr_stream *xdr, const struct nfs_open_confirmargs *arg)
1094{
Al Viro8687b632006-10-19 23:28:48 -07001095 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001097 RESERVE_SPACE(4+NFS4_STATEID_SIZE+4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 WRITE32(OP_OPEN_CONFIRM);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001099 WRITEMEM(arg->stateid->data, NFS4_STATEID_SIZE);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001100 WRITE32(arg->seqid->sequence->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 return 0;
1103}
1104
1105static int encode_open_downgrade(struct xdr_stream *xdr, const struct nfs_closeargs *arg)
1106{
Al Viro8687b632006-10-19 23:28:48 -07001107 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001109 RESERVE_SPACE(4+NFS4_STATEID_SIZE+4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 WRITE32(OP_OPEN_DOWNGRADE);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001111 WRITEMEM(arg->stateid->data, NFS4_STATEID_SIZE);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001112 WRITE32(arg->seqid->sequence->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 encode_share_access(xdr, arg->open_flags);
1114 return 0;
1115}
1116
1117static int
1118encode_putfh(struct xdr_stream *xdr, const struct nfs_fh *fh)
1119{
1120 int len = fh->size;
Al Viro8687b632006-10-19 23:28:48 -07001121 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 RESERVE_SPACE(8 + len);
1124 WRITE32(OP_PUTFH);
1125 WRITE32(len);
1126 WRITEMEM(fh->data, len);
1127
1128 return 0;
1129}
1130
1131static int encode_putrootfh(struct xdr_stream *xdr)
1132{
Al Viro8687b632006-10-19 23:28:48 -07001133 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 RESERVE_SPACE(4);
1136 WRITE32(OP_PUTROOTFH);
1137
1138 return 0;
1139}
1140
1141static void encode_stateid(struct xdr_stream *xdr, const struct nfs_open_context *ctx)
1142{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 nfs4_stateid stateid;
Al Viro8687b632006-10-19 23:28:48 -07001144 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001146 RESERVE_SPACE(NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (ctx->state != NULL) {
1148 nfs4_copy_stateid(&stateid, ctx->state, ctx->lockowner);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001149 WRITEMEM(stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 } else
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001151 WRITEMEM(zero_stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
1153
1154static int encode_read(struct xdr_stream *xdr, const struct nfs_readargs *args)
1155{
Al Viro8687b632006-10-19 23:28:48 -07001156 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 RESERVE_SPACE(4);
1159 WRITE32(OP_READ);
1160
1161 encode_stateid(xdr, args->context);
1162
1163 RESERVE_SPACE(12);
1164 WRITE64(args->offset);
1165 WRITE32(args->count);
1166
1167 return 0;
1168}
1169
1170static int encode_readdir(struct xdr_stream *xdr, const struct nfs4_readdir_arg *readdir, struct rpc_rqst *req)
1171{
Manoj Naik97d312d2005-06-22 17:16:39 +00001172 uint32_t attrs[2] = {
1173 FATTR4_WORD0_RDATTR_ERROR|FATTR4_WORD0_FILEID,
1174 FATTR4_WORD1_MOUNTED_ON_FILEID,
1175 };
Al Viro8687b632006-10-19 23:28:48 -07001176 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001178 RESERVE_SPACE(12+NFS4_VERIFIER_SIZE+20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 WRITE32(OP_READDIR);
1180 WRITE64(readdir->cookie);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001181 WRITEMEM(readdir->verifier.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 WRITE32(readdir->count >> 1); /* We're not doing readdirplus */
1183 WRITE32(readdir->count);
1184 WRITE32(2);
Manoj Naik97d312d2005-06-22 17:16:39 +00001185 /* Switch to mounted_on_fileid if the server supports it */
1186 if (readdir->bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
1187 attrs[0] &= ~FATTR4_WORD0_FILEID;
1188 else
1189 attrs[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
1190 WRITE32(attrs[0] & readdir->bitmask[0]);
1191 WRITE32(attrs[1] & readdir->bitmask[1]);
Trond Myklebusteadf4592005-06-22 17:16:39 +00001192 dprintk("%s: cookie = %Lu, verifier = 0x%x%x, bitmap = 0x%x%x\n",
1193 __FUNCTION__,
1194 (unsigned long long)readdir->cookie,
1195 ((u32 *)readdir->verifier.data)[0],
1196 ((u32 *)readdir->verifier.data)[1],
1197 attrs[0] & readdir->bitmask[0],
1198 attrs[1] & readdir->bitmask[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return 0;
1201}
1202
1203static int encode_readlink(struct xdr_stream *xdr, const struct nfs4_readlink *readlink, struct rpc_rqst *req)
1204{
Al Viro8687b632006-10-19 23:28:48 -07001205 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 RESERVE_SPACE(4);
1208 WRITE32(OP_READLINK);
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return 0;
1211}
1212
1213static int encode_remove(struct xdr_stream *xdr, const struct qstr *name)
1214{
Al Viro8687b632006-10-19 23:28:48 -07001215 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 RESERVE_SPACE(8 + name->len);
1218 WRITE32(OP_REMOVE);
1219 WRITE32(name->len);
1220 WRITEMEM(name->name, name->len);
1221
1222 return 0;
1223}
1224
1225static int encode_rename(struct xdr_stream *xdr, const struct qstr *oldname, const struct qstr *newname)
1226{
Al Viro8687b632006-10-19 23:28:48 -07001227 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 RESERVE_SPACE(8 + oldname->len);
1230 WRITE32(OP_RENAME);
1231 WRITE32(oldname->len);
1232 WRITEMEM(oldname->name, oldname->len);
1233
1234 RESERVE_SPACE(4 + newname->len);
1235 WRITE32(newname->len);
1236 WRITEMEM(newname->name, newname->len);
1237
1238 return 0;
1239}
1240
David Howellsadfa6f92006-08-22 20:06:08 -04001241static int encode_renew(struct xdr_stream *xdr, const struct nfs_client *client_stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
Al Viro8687b632006-10-19 23:28:48 -07001243 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 RESERVE_SPACE(12);
1246 WRITE32(OP_RENEW);
1247 WRITE64(client_stateid->cl_clientid);
1248
1249 return 0;
1250}
1251
1252static int
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001253encode_restorefh(struct xdr_stream *xdr)
1254{
Al Viro8687b632006-10-19 23:28:48 -07001255 __be32 *p;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001256
1257 RESERVE_SPACE(4);
1258 WRITE32(OP_RESTOREFH);
1259
1260 return 0;
1261}
1262
1263static int
J. Bruce Fields23ec6962005-06-22 17:16:22 +00001264encode_setacl(struct xdr_stream *xdr, struct nfs_setaclargs *arg)
1265{
Al Viro8687b632006-10-19 23:28:48 -07001266 __be32 *p;
J. Bruce Fields23ec6962005-06-22 17:16:22 +00001267
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001268 RESERVE_SPACE(4+NFS4_STATEID_SIZE);
J. Bruce Fields23ec6962005-06-22 17:16:22 +00001269 WRITE32(OP_SETATTR);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001270 WRITEMEM(zero_stateid.data, NFS4_STATEID_SIZE);
J. Bruce Fields23ec6962005-06-22 17:16:22 +00001271 RESERVE_SPACE(2*4);
1272 WRITE32(1);
1273 WRITE32(FATTR4_WORD0_ACL);
1274 if (arg->acl_len % 4)
1275 return -EINVAL;
1276 RESERVE_SPACE(4);
1277 WRITE32(arg->acl_len);
1278 xdr_write_pages(xdr, arg->acl_pages, arg->acl_pgbase, arg->acl_len);
1279 return 0;
1280}
1281
1282static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283encode_savefh(struct xdr_stream *xdr)
1284{
Al Viro8687b632006-10-19 23:28:48 -07001285 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 RESERVE_SPACE(4);
1288 WRITE32(OP_SAVEFH);
1289
1290 return 0;
1291}
1292
1293static int encode_setattr(struct xdr_stream *xdr, const struct nfs_setattrargs *arg, const struct nfs_server *server)
1294{
1295 int status;
Al Viro8687b632006-10-19 23:28:48 -07001296 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001298 RESERVE_SPACE(4+NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 WRITE32(OP_SETATTR);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001300 WRITEMEM(arg->stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 if ((status = encode_attrs(xdr, arg->iap, server)))
1303 return status;
1304
1305 return 0;
1306}
1307
1308static int encode_setclientid(struct xdr_stream *xdr, const struct nfs4_setclientid *setclientid)
1309{
Al Viro8687b632006-10-19 23:28:48 -07001310 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001312 RESERVE_SPACE(4 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 WRITE32(OP_SETCLIENTID);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001314 WRITEMEM(setclientid->sc_verifier->data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 encode_string(xdr, setclientid->sc_name_len, setclientid->sc_name);
1317 RESERVE_SPACE(4);
1318 WRITE32(setclientid->sc_prog);
1319 encode_string(xdr, setclientid->sc_netid_len, setclientid->sc_netid);
1320 encode_string(xdr, setclientid->sc_uaddr_len, setclientid->sc_uaddr);
1321 RESERVE_SPACE(4);
1322 WRITE32(setclientid->sc_cb_ident);
1323
1324 return 0;
1325}
1326
David Howellsadfa6f92006-08-22 20:06:08 -04001327static int encode_setclientid_confirm(struct xdr_stream *xdr, const struct nfs_client *client_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
Al Viro8687b632006-10-19 23:28:48 -07001329 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001331 RESERVE_SPACE(12 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 WRITE32(OP_SETCLIENTID_CONFIRM);
1333 WRITE64(client_state->cl_clientid);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001334 WRITEMEM(client_state->cl_confirm.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 return 0;
1337}
1338
1339static int encode_write(struct xdr_stream *xdr, const struct nfs_writeargs *args)
1340{
Al Viro8687b632006-10-19 23:28:48 -07001341 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 RESERVE_SPACE(4);
1344 WRITE32(OP_WRITE);
1345
1346 encode_stateid(xdr, args->context);
1347
1348 RESERVE_SPACE(16);
1349 WRITE64(args->offset);
1350 WRITE32(args->stable);
1351 WRITE32(args->count);
1352
1353 xdr_write_pages(xdr, args->pages, args->pgbase, args->count);
1354
1355 return 0;
1356}
1357
1358static int encode_delegreturn(struct xdr_stream *xdr, const nfs4_stateid *stateid)
1359{
Al Viro8687b632006-10-19 23:28:48 -07001360 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001362 RESERVE_SPACE(4+NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 WRITE32(OP_DELEGRETURN);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001365 WRITEMEM(stateid->data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return 0;
1367
1368}
1369/*
1370 * END OF "GENERIC" ENCODE ROUTINES.
1371 */
1372
1373/*
1374 * Encode an ACCESS request
1375 */
Al Viro8687b632006-10-19 23:28:48 -07001376static int nfs4_xdr_enc_access(struct rpc_rqst *req, __be32 *p, const struct nfs4_accessargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
1378 struct xdr_stream xdr;
1379 struct compound_hdr hdr = {
Trond Myklebust76b32992007-08-10 17:45:11 -04001380 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 };
1382 int status;
1383
1384 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1385 encode_compound_hdr(&xdr, &hdr);
Trond Myklebust76b32992007-08-10 17:45:11 -04001386 status = encode_putfh(&xdr, args->fh);
1387 if (status != 0)
1388 goto out;
1389 status = encode_access(&xdr, args->access);
1390 if (status != 0)
1391 goto out;
1392 status = encode_getfattr(&xdr, args->bitmask);
1393out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return status;
1395}
1396
1397/*
1398 * Encode LOOKUP request
1399 */
Al Viro8687b632006-10-19 23:28:48 -07001400static int nfs4_xdr_enc_lookup(struct rpc_rqst *req, __be32 *p, const struct nfs4_lookup_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
1402 struct xdr_stream xdr;
1403 struct compound_hdr hdr = {
1404 .nops = 4,
1405 };
1406 int status;
1407
1408 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1409 encode_compound_hdr(&xdr, &hdr);
1410 if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1411 goto out;
1412 if ((status = encode_lookup(&xdr, args->name)) != 0)
1413 goto out;
1414 if ((status = encode_getfh(&xdr)) != 0)
1415 goto out;
1416 status = encode_getfattr(&xdr, args->bitmask);
1417out:
1418 return status;
1419}
1420
1421/*
1422 * Encode LOOKUP_ROOT request
1423 */
Al Viro8687b632006-10-19 23:28:48 -07001424static 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 -07001425{
1426 struct xdr_stream xdr;
1427 struct compound_hdr hdr = {
1428 .nops = 3,
1429 };
1430 int status;
1431
1432 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1433 encode_compound_hdr(&xdr, &hdr);
1434 if ((status = encode_putrootfh(&xdr)) != 0)
1435 goto out;
1436 if ((status = encode_getfh(&xdr)) == 0)
1437 status = encode_getfattr(&xdr, args->bitmask);
1438out:
1439 return status;
1440}
1441
1442/*
1443 * Encode REMOVE request
1444 */
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04001445static int nfs4_xdr_enc_remove(struct rpc_rqst *req, __be32 *p, const struct nfs_removeargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
1447 struct xdr_stream xdr;
1448 struct compound_hdr hdr = {
Trond Myklebust16e42952005-10-27 22:12:44 -04001449 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 };
1451 int status;
1452
1453 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1454 encode_compound_hdr(&xdr, &hdr);
Trond Myklebust16e42952005-10-27 22:12:44 -04001455 if ((status = encode_putfh(&xdr, args->fh)) != 0)
1456 goto out;
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04001457 if ((status = encode_remove(&xdr, &args->name)) != 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04001458 goto out;
1459 status = encode_getfattr(&xdr, args->bitmask);
1460out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return status;
1462}
1463
1464/*
1465 * Encode RENAME request
1466 */
Al Viro8687b632006-10-19 23:28:48 -07001467static int nfs4_xdr_enc_rename(struct rpc_rqst *req, __be32 *p, const struct nfs4_rename_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
1469 struct xdr_stream xdr;
1470 struct compound_hdr hdr = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001471 .nops = 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 };
1473 int status;
1474
1475 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1476 encode_compound_hdr(&xdr, &hdr);
1477 if ((status = encode_putfh(&xdr, args->old_dir)) != 0)
1478 goto out;
1479 if ((status = encode_savefh(&xdr)) != 0)
1480 goto out;
1481 if ((status = encode_putfh(&xdr, args->new_dir)) != 0)
1482 goto out;
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001483 if ((status = encode_rename(&xdr, args->old_name, args->new_name)) != 0)
1484 goto out;
1485 if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
1486 goto out;
1487 if ((status = encode_restorefh(&xdr)) != 0)
1488 goto out;
1489 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490out:
1491 return status;
1492}
1493
1494/*
1495 * Encode LINK request
1496 */
Al Viro8687b632006-10-19 23:28:48 -07001497static int nfs4_xdr_enc_link(struct rpc_rqst *req, __be32 *p, const struct nfs4_link_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
1499 struct xdr_stream xdr;
1500 struct compound_hdr hdr = {
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001501 .nops = 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 };
1503 int status;
1504
1505 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1506 encode_compound_hdr(&xdr, &hdr);
1507 if ((status = encode_putfh(&xdr, args->fh)) != 0)
1508 goto out;
1509 if ((status = encode_savefh(&xdr)) != 0)
1510 goto out;
1511 if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1512 goto out;
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001513 if ((status = encode_link(&xdr, args->name)) != 0)
1514 goto out;
1515 if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
1516 goto out;
1517 if ((status = encode_restorefh(&xdr)) != 0)
1518 goto out;
1519 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520out:
1521 return status;
1522}
1523
1524/*
1525 * Encode CREATE request
1526 */
Al Viro8687b632006-10-19 23:28:48 -07001527static int nfs4_xdr_enc_create(struct rpc_rqst *req, __be32 *p, const struct nfs4_create_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
1529 struct xdr_stream xdr;
1530 struct compound_hdr hdr = {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001531 .nops = 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 };
1533 int status;
1534
1535 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1536 encode_compound_hdr(&xdr, &hdr);
1537 if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1538 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001539 if ((status = encode_savefh(&xdr)) != 0)
1540 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 if ((status = encode_create(&xdr, args)) != 0)
1542 goto out;
1543 if ((status = encode_getfh(&xdr)) != 0)
1544 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001545 if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
1546 goto out;
1547 if ((status = encode_restorefh(&xdr)) != 0)
1548 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 status = encode_getfattr(&xdr, args->bitmask);
1550out:
1551 return status;
1552}
1553
1554/*
1555 * Encode SYMLINK request
1556 */
Al Viro8687b632006-10-19 23:28:48 -07001557static int nfs4_xdr_enc_symlink(struct rpc_rqst *req, __be32 *p, const struct nfs4_create_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
1559 return nfs4_xdr_enc_create(req, p, args);
1560}
1561
1562/*
1563 * Encode GETATTR request
1564 */
Al Viro8687b632006-10-19 23:28:48 -07001565static int nfs4_xdr_enc_getattr(struct rpc_rqst *req, __be32 *p, const struct nfs4_getattr_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
1567 struct xdr_stream xdr;
1568 struct compound_hdr hdr = {
1569 .nops = 2,
1570 };
1571 int status;
1572
1573 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1574 encode_compound_hdr(&xdr, &hdr);
1575 if ((status = encode_putfh(&xdr, args->fh)) == 0)
1576 status = encode_getfattr(&xdr, args->bitmask);
1577 return status;
1578}
1579
1580/*
1581 * Encode a CLOSE request
1582 */
Al Viro8687b632006-10-19 23:28:48 -07001583static int nfs4_xdr_enc_close(struct rpc_rqst *req, __be32 *p, struct nfs_closeargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
1585 struct xdr_stream xdr;
1586 struct compound_hdr hdr = {
Trond Myklebust516a6af2005-10-27 22:12:41 -04001587 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 };
1589 int status;
1590
1591 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1592 encode_compound_hdr(&xdr, &hdr);
1593 status = encode_putfh(&xdr, args->fh);
1594 if(status)
1595 goto out;
1596 status = encode_close(&xdr, args);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001597 if (status != 0)
1598 goto out;
1599 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600out:
1601 return status;
1602}
1603
1604/*
1605 * Encode an OPEN request
1606 */
Al Viro8687b632006-10-19 23:28:48 -07001607static int nfs4_xdr_enc_open(struct rpc_rqst *req, __be32 *p, struct nfs_openargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
1609 struct xdr_stream xdr;
1610 struct compound_hdr hdr = {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001611 .nops = 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 };
1613 int status;
1614
1615 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1616 encode_compound_hdr(&xdr, &hdr);
1617 status = encode_putfh(&xdr, args->fh);
1618 if (status)
1619 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001620 status = encode_savefh(&xdr);
1621 if (status)
1622 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 status = encode_open(&xdr, args);
1624 if (status)
1625 goto out;
1626 status = encode_getfh(&xdr);
1627 if (status)
1628 goto out;
1629 status = encode_getfattr(&xdr, args->bitmask);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001630 if (status)
1631 goto out;
1632 status = encode_restorefh(&xdr);
1633 if (status)
1634 goto out;
1635 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636out:
1637 return status;
1638}
1639
1640/*
1641 * Encode an OPEN_CONFIRM request
1642 */
Al Viro8687b632006-10-19 23:28:48 -07001643static int nfs4_xdr_enc_open_confirm(struct rpc_rqst *req, __be32 *p, struct nfs_open_confirmargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
1645 struct xdr_stream xdr;
1646 struct compound_hdr hdr = {
1647 .nops = 2,
1648 };
1649 int status;
1650
1651 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1652 encode_compound_hdr(&xdr, &hdr);
1653 status = encode_putfh(&xdr, args->fh);
1654 if(status)
1655 goto out;
1656 status = encode_open_confirm(&xdr, args);
1657out:
1658 return status;
1659}
1660
1661/*
1662 * Encode an OPEN request with no attributes.
1663 */
Al Viro8687b632006-10-19 23:28:48 -07001664static int nfs4_xdr_enc_open_noattr(struct rpc_rqst *req, __be32 *p, struct nfs_openargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
1666 struct xdr_stream xdr;
1667 struct compound_hdr hdr = {
Trond Myklebust864472e2006-01-03 09:55:15 +01001668 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 };
1670 int status;
1671
1672 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1673 encode_compound_hdr(&xdr, &hdr);
1674 status = encode_putfh(&xdr, args->fh);
1675 if (status)
1676 goto out;
1677 status = encode_open(&xdr, args);
Trond Myklebust864472e2006-01-03 09:55:15 +01001678 if (status)
1679 goto out;
1680 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681out:
1682 return status;
1683}
1684
1685/*
1686 * Encode an OPEN_DOWNGRADE request
1687 */
Al Viro8687b632006-10-19 23:28:48 -07001688static int nfs4_xdr_enc_open_downgrade(struct rpc_rqst *req, __be32 *p, struct nfs_closeargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
1690 struct xdr_stream xdr;
1691 struct compound_hdr hdr = {
Trond Myklebust516a6af2005-10-27 22:12:41 -04001692 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 };
1694 int status;
1695
1696 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1697 encode_compound_hdr(&xdr, &hdr);
1698 status = encode_putfh(&xdr, args->fh);
1699 if (status)
1700 goto out;
1701 status = encode_open_downgrade(&xdr, args);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001702 if (status != 0)
1703 goto out;
1704 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705out:
1706 return status;
1707}
1708
1709/*
1710 * Encode a LOCK request
1711 */
Al Viro8687b632006-10-19 23:28:48 -07001712static int nfs4_xdr_enc_lock(struct rpc_rqst *req, __be32 *p, struct nfs_lock_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
1714 struct xdr_stream xdr;
1715 struct compound_hdr hdr = {
1716 .nops = 2,
1717 };
1718 int status;
1719
1720 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1721 encode_compound_hdr(&xdr, &hdr);
1722 status = encode_putfh(&xdr, args->fh);
1723 if(status)
1724 goto out;
1725 status = encode_lock(&xdr, args);
1726out:
1727 return status;
1728}
1729
1730/*
1731 * Encode a LOCKT request
1732 */
Al Viro8687b632006-10-19 23:28:48 -07001733static int nfs4_xdr_enc_lockt(struct rpc_rqst *req, __be32 *p, struct nfs_lockt_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
1735 struct xdr_stream xdr;
1736 struct compound_hdr hdr = {
1737 .nops = 2,
1738 };
1739 int status;
1740
1741 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1742 encode_compound_hdr(&xdr, &hdr);
1743 status = encode_putfh(&xdr, args->fh);
1744 if(status)
1745 goto out;
1746 status = encode_lockt(&xdr, args);
1747out:
1748 return status;
1749}
1750
1751/*
1752 * Encode a LOCKU request
1753 */
Al Viro8687b632006-10-19 23:28:48 -07001754static int nfs4_xdr_enc_locku(struct rpc_rqst *req, __be32 *p, struct nfs_locku_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
1756 struct xdr_stream xdr;
1757 struct compound_hdr hdr = {
1758 .nops = 2,
1759 };
1760 int status;
1761
1762 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1763 encode_compound_hdr(&xdr, &hdr);
1764 status = encode_putfh(&xdr, args->fh);
1765 if(status)
1766 goto out;
1767 status = encode_locku(&xdr, args);
1768out:
1769 return status;
1770}
1771
1772/*
1773 * Encode a READLINK request
1774 */
Al Viro8687b632006-10-19 23:28:48 -07001775static int nfs4_xdr_enc_readlink(struct rpc_rqst *req, __be32 *p, const struct nfs4_readlink *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
1777 struct xdr_stream xdr;
1778 struct compound_hdr hdr = {
1779 .nops = 2,
1780 };
Trond Myklebuste3a535e2007-07-19 10:03:38 -04001781 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
1782 unsigned int replen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 int status;
1784
1785 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1786 encode_compound_hdr(&xdr, &hdr);
1787 status = encode_putfh(&xdr, args->fh);
1788 if(status)
1789 goto out;
1790 status = encode_readlink(&xdr, args, req);
Trond Myklebuste3a535e2007-07-19 10:03:38 -04001791
1792 /* set up reply kvec
1793 * toplevel_status + taglen + rescount + OP_PUTFH + status
1794 * + OP_READLINK + status + string length = 8
1795 */
1796 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_readlink_sz) << 2;
1797 xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages,
1798 args->pgbase, args->pglen);
1799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800out:
1801 return status;
1802}
1803
1804/*
1805 * Encode a READDIR request
1806 */
Al Viro8687b632006-10-19 23:28:48 -07001807static int nfs4_xdr_enc_readdir(struct rpc_rqst *req, __be32 *p, const struct nfs4_readdir_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
1809 struct xdr_stream xdr;
1810 struct compound_hdr hdr = {
1811 .nops = 2,
1812 };
Trond Myklebustd6ac02d2007-07-19 10:03:37 -04001813 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
1814 int replen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 int status;
1816
1817 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1818 encode_compound_hdr(&xdr, &hdr);
1819 status = encode_putfh(&xdr, args->fh);
1820 if(status)
1821 goto out;
1822 status = encode_readdir(&xdr, args, req);
Trond Myklebustd6ac02d2007-07-19 10:03:37 -04001823
1824 /* set up reply kvec
1825 * toplevel_status + taglen + rescount + OP_PUTFH + status
1826 * + OP_READDIR + status + verifer(2) = 9
1827 */
1828 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_readdir_sz) << 2;
1829 xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages,
1830 args->pgbase, args->count);
1831 dprintk("%s: inlined page args = (%u, %p, %u, %u)\n",
1832 __FUNCTION__, replen, args->pages,
1833 args->pgbase, args->count);
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835out:
1836 return status;
1837}
1838
1839/*
1840 * Encode a READ request
1841 */
Al Viro8687b632006-10-19 23:28:48 -07001842static int nfs4_xdr_enc_read(struct rpc_rqst *req, __be32 *p, struct nfs_readargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
Trond Myklebust1be27f32007-06-27 14:29:04 -04001844 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 struct xdr_stream xdr;
1846 struct compound_hdr hdr = {
1847 .nops = 2,
1848 };
1849 int replen, status;
1850
1851 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1852 encode_compound_hdr(&xdr, &hdr);
1853 status = encode_putfh(&xdr, args->fh);
1854 if (status)
1855 goto out;
1856 status = encode_read(&xdr, args);
1857 if (status)
1858 goto out;
1859
1860 /* set up reply kvec
1861 * toplevel status + taglen=0 + rescount + OP_PUTFH + status
1862 * + OP_READ + status + eof + datalen = 9
1863 */
1864 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_read_sz) << 2;
1865 xdr_inline_pages(&req->rq_rcv_buf, replen,
1866 args->pages, args->pgbase, args->count);
\"Talpey, Thomas\4f22ccc2007-09-10 13:44:58 -04001867 req->rq_rcv_buf.flags |= XDRBUF_READ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868out:
1869 return status;
1870}
1871
1872/*
1873 * Encode an SETATTR request
1874 */
Al Viro8687b632006-10-19 23:28:48 -07001875static int nfs4_xdr_enc_setattr(struct rpc_rqst *req, __be32 *p, struct nfs_setattrargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877{
1878 struct xdr_stream xdr;
1879 struct compound_hdr hdr = {
1880 .nops = 3,
1881 };
1882 int status;
1883
1884 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1885 encode_compound_hdr(&xdr, &hdr);
1886 status = encode_putfh(&xdr, args->fh);
1887 if(status)
1888 goto out;
1889 status = encode_setattr(&xdr, args, args->server);
1890 if(status)
1891 goto out;
1892 status = encode_getfattr(&xdr, args->bitmask);
1893out:
1894 return status;
1895}
1896
1897/*
J. Bruce Fields029d1052005-06-22 17:16:22 +00001898 * Encode a GETACL request
1899 */
1900static int
Al Viro8687b632006-10-19 23:28:48 -07001901nfs4_xdr_enc_getacl(struct rpc_rqst *req, __be32 *p,
J. Bruce Fields029d1052005-06-22 17:16:22 +00001902 struct nfs_getaclargs *args)
1903{
1904 struct xdr_stream xdr;
Trond Myklebust1be27f32007-06-27 14:29:04 -04001905 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
J. Bruce Fields029d1052005-06-22 17:16:22 +00001906 struct compound_hdr hdr = {
1907 .nops = 2,
1908 };
1909 int replen, status;
1910
1911 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1912 encode_compound_hdr(&xdr, &hdr);
1913 status = encode_putfh(&xdr, args->fh);
1914 if (status)
1915 goto out;
1916 status = encode_getattr_two(&xdr, FATTR4_WORD0_ACL, 0);
1917 /* set up reply buffer: */
1918 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_getacl_sz) << 2;
1919 xdr_inline_pages(&req->rq_rcv_buf, replen,
1920 args->acl_pages, args->acl_pgbase, args->acl_len);
1921out:
1922 return status;
1923}
1924
1925/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 * Encode a WRITE request
1927 */
Al Viro8687b632006-10-19 23:28:48 -07001928static int nfs4_xdr_enc_write(struct rpc_rqst *req, __be32 *p, struct nfs_writeargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
1930 struct xdr_stream xdr;
1931 struct compound_hdr hdr = {
Trond Myklebust4f9838c2005-10-27 22:12:44 -04001932 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 };
1934 int status;
1935
1936 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1937 encode_compound_hdr(&xdr, &hdr);
1938 status = encode_putfh(&xdr, args->fh);
1939 if (status)
1940 goto out;
1941 status = encode_write(&xdr, args);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04001942 if (status)
1943 goto out;
\"Talpey, Thomas\4f22ccc2007-09-10 13:44:58 -04001944 req->rq_snd_buf.flags |= XDRBUF_WRITE;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04001945 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946out:
1947 return status;
1948}
1949
1950/*
1951 * a COMMIT request
1952 */
Al Viro8687b632006-10-19 23:28:48 -07001953static int nfs4_xdr_enc_commit(struct rpc_rqst *req, __be32 *p, struct nfs_writeargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
1955 struct xdr_stream xdr;
1956 struct compound_hdr hdr = {
Trond Myklebust4f9838c2005-10-27 22:12:44 -04001957 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 };
1959 int status;
1960
1961 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1962 encode_compound_hdr(&xdr, &hdr);
1963 status = encode_putfh(&xdr, args->fh);
1964 if (status)
1965 goto out;
1966 status = encode_commit(&xdr, args);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04001967 if (status)
1968 goto out;
1969 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970out:
1971 return status;
1972}
1973
1974/*
1975 * FSINFO request
1976 */
Al Viro8687b632006-10-19 23:28:48 -07001977static int nfs4_xdr_enc_fsinfo(struct rpc_rqst *req, __be32 *p, struct nfs4_fsinfo_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
1979 struct xdr_stream xdr;
1980 struct compound_hdr hdr = {
1981 .nops = 2,
1982 };
1983 int status;
1984
1985 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1986 encode_compound_hdr(&xdr, &hdr);
1987 status = encode_putfh(&xdr, args->fh);
1988 if (!status)
1989 status = encode_fsinfo(&xdr, args->bitmask);
1990 return status;
1991}
1992
1993/*
1994 * a PATHCONF request
1995 */
Al Viro8687b632006-10-19 23:28:48 -07001996static int nfs4_xdr_enc_pathconf(struct rpc_rqst *req, __be32 *p, const struct nfs4_pathconf_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 struct xdr_stream xdr;
1999 struct compound_hdr hdr = {
2000 .nops = 2,
2001 };
2002 int status;
2003
2004 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2005 encode_compound_hdr(&xdr, &hdr);
2006 status = encode_putfh(&xdr, args->fh);
2007 if (!status)
2008 status = encode_getattr_one(&xdr,
2009 args->bitmask[0] & nfs4_pathconf_bitmap[0]);
2010 return status;
2011}
2012
2013/*
2014 * a STATFS request
2015 */
Al Viro8687b632006-10-19 23:28:48 -07002016static int nfs4_xdr_enc_statfs(struct rpc_rqst *req, __be32 *p, const struct nfs4_statfs_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 struct xdr_stream xdr;
2019 struct compound_hdr hdr = {
2020 .nops = 2,
2021 };
2022 int status;
2023
2024 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2025 encode_compound_hdr(&xdr, &hdr);
2026 status = encode_putfh(&xdr, args->fh);
2027 if (status == 0)
2028 status = encode_getattr_two(&xdr,
2029 args->bitmask[0] & nfs4_statfs_bitmap[0],
2030 args->bitmask[1] & nfs4_statfs_bitmap[1]);
2031 return status;
2032}
2033
2034/*
2035 * GETATTR_BITMAP request
2036 */
Al Viro8687b632006-10-19 23:28:48 -07002037static int nfs4_xdr_enc_server_caps(struct rpc_rqst *req, __be32 *p, const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
2039 struct xdr_stream xdr;
2040 struct compound_hdr hdr = {
2041 .nops = 2,
2042 };
2043 int status;
2044
2045 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2046 encode_compound_hdr(&xdr, &hdr);
2047 status = encode_putfh(&xdr, fhandle);
2048 if (status == 0)
2049 status = encode_getattr_one(&xdr, FATTR4_WORD0_SUPPORTED_ATTRS|
2050 FATTR4_WORD0_LINK_SUPPORT|
2051 FATTR4_WORD0_SYMLINK_SUPPORT|
2052 FATTR4_WORD0_ACLSUPPORT);
2053 return status;
2054}
2055
2056/*
2057 * a RENEW request
2058 */
Al Viro8687b632006-10-19 23:28:48 -07002059static int nfs4_xdr_enc_renew(struct rpc_rqst *req, __be32 *p, struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
2061 struct xdr_stream xdr;
2062 struct compound_hdr hdr = {
2063 .nops = 1,
2064 };
2065
2066 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2067 encode_compound_hdr(&xdr, &hdr);
2068 return encode_renew(&xdr, clp);
2069}
2070
2071/*
2072 * a SETCLIENTID request
2073 */
Al Viro8687b632006-10-19 23:28:48 -07002074static int nfs4_xdr_enc_setclientid(struct rpc_rqst *req, __be32 *p, struct nfs4_setclientid *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
2076 struct xdr_stream xdr;
2077 struct compound_hdr hdr = {
2078 .nops = 1,
2079 };
2080
2081 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2082 encode_compound_hdr(&xdr, &hdr);
2083 return encode_setclientid(&xdr, sc);
2084}
2085
2086/*
2087 * a SETCLIENTID_CONFIRM request
2088 */
Al Viro8687b632006-10-19 23:28:48 -07002089static int nfs4_xdr_enc_setclientid_confirm(struct rpc_rqst *req, __be32 *p, struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090{
2091 struct xdr_stream xdr;
2092 struct compound_hdr hdr = {
2093 .nops = 3,
2094 };
2095 const u32 lease_bitmap[2] = { FATTR4_WORD0_LEASE_TIME, 0 };
2096 int status;
2097
2098 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2099 encode_compound_hdr(&xdr, &hdr);
2100 status = encode_setclientid_confirm(&xdr, clp);
2101 if (!status)
2102 status = encode_putrootfh(&xdr);
2103 if (!status)
2104 status = encode_fsinfo(&xdr, lease_bitmap);
2105 return status;
2106}
2107
2108/*
2109 * DELEGRETURN request
2110 */
Al Viro8687b632006-10-19 23:28:48 -07002111static int nfs4_xdr_enc_delegreturn(struct rpc_rqst *req, __be32 *p, const struct nfs4_delegreturnargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
2113 struct xdr_stream xdr;
2114 struct compound_hdr hdr = {
Trond Myklebustfa178f22006-01-03 09:55:38 +01002115 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 };
2117 int status;
2118
2119 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2120 encode_compound_hdr(&xdr, &hdr);
Trond Myklebustfa178f22006-01-03 09:55:38 +01002121 status = encode_putfh(&xdr, args->fhandle);
2122 if (status != 0)
2123 goto out;
2124 status = encode_delegreturn(&xdr, args->stateid);
2125 if (status != 0)
2126 goto out;
2127 status = encode_getfattr(&xdr, args->bitmask);
2128out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 return status;
2130}
2131
2132/*
Trond Myklebust683b57b2006-06-09 09:34:22 -04002133 * Encode FS_LOCATIONS request
2134 */
Al Viro8687b632006-10-19 23:28:48 -07002135static 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 -04002136{
2137 struct xdr_stream xdr;
2138 struct compound_hdr hdr = {
2139 .nops = 3,
2140 };
Trond Myklebust1be27f32007-06-27 14:29:04 -04002141 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
Trond Myklebust683b57b2006-06-09 09:34:22 -04002142 int replen;
2143 int status;
2144
2145 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2146 encode_compound_hdr(&xdr, &hdr);
2147 if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
2148 goto out;
2149 if ((status = encode_lookup(&xdr, args->name)) != 0)
2150 goto out;
Manoj Naik830b8e32006-06-09 09:34:25 -04002151 if ((status = encode_fs_locations(&xdr, args->bitmask)) != 0)
Trond Myklebust683b57b2006-06-09 09:34:22 -04002152 goto out;
2153 /* set up reply
Manoj Naik830b8e32006-06-09 09:34:25 -04002154 * toplevel_status + OP_PUTFH + status
Trond Myklebust683b57b2006-06-09 09:34:22 -04002155 * + OP_LOOKUP + status + OP_GETATTR + status = 7
2156 */
2157 replen = (RPC_REPHDRSIZE + auth->au_rslack + 7) << 2;
2158 xdr_inline_pages(&req->rq_rcv_buf, replen, &args->page,
2159 0, PAGE_SIZE);
2160out:
2161 return status;
2162}
2163
2164/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 * START OF "GENERIC" DECODE ROUTINES.
2166 * These may look a little ugly since they are imported from a "generic"
2167 * set of XDR encode/decode routines which are intended to be shared by
2168 * all of our NFSv4 implementations (OpenBSD, MacOS X...).
2169 *
2170 * If the pain of reading these is too great, it should be a straightforward
2171 * task to translate them into Linux-specific versions which are more
2172 * consistent with the style used in NFSv2/v3...
2173 */
2174#define READ32(x) (x) = ntohl(*p++)
2175#define READ64(x) do { \
2176 (x) = (u64)ntohl(*p++) << 32; \
2177 (x) |= ntohl(*p++); \
2178} while (0)
2179#define READTIME(x) do { \
2180 p++; \
2181 (x.tv_sec) = ntohl(*p++); \
2182 (x.tv_nsec) = ntohl(*p++); \
2183} while (0)
2184#define COPYMEM(x,nbytes) do { \
2185 memcpy((x), p, nbytes); \
2186 p += XDR_QUADLEN(nbytes); \
2187} while (0)
2188
2189#define READ_BUF(nbytes) do { \
2190 p = xdr_inline_decode(xdr, nbytes); \
Chuck Levere4cc6ee2007-05-08 18:23:28 -04002191 if (unlikely(!p)) { \
Chuck Leverfe82a182007-09-11 18:01:10 -04002192 dprintk("nfs: %s: prematurely hit end of receive" \
Chuck Levere4cc6ee2007-05-08 18:23:28 -04002193 " buffer\n", __FUNCTION__); \
Chuck Leverfe82a182007-09-11 18:01:10 -04002194 dprintk("nfs: %s: xdr->p=%p, bytes=%u, xdr->end=%p\n", \
Chuck Levere4cc6ee2007-05-08 18:23:28 -04002195 __FUNCTION__, xdr->p, nbytes, xdr->end); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 return -EIO; \
2197 } \
2198} while (0)
2199
Trond Myklebust683b57b2006-06-09 09:34:22 -04002200static int decode_opaque_inline(struct xdr_stream *xdr, unsigned int *len, char **string)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
Al Viro8687b632006-10-19 23:28:48 -07002202 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204 READ_BUF(4);
2205 READ32(*len);
2206 READ_BUF(*len);
2207 *string = (char *)p;
2208 return 0;
2209}
2210
2211static int decode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
2212{
Al Viro8687b632006-10-19 23:28:48 -07002213 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
2215 READ_BUF(8);
2216 READ32(hdr->status);
2217 READ32(hdr->taglen);
2218
2219 READ_BUF(hdr->taglen + 4);
2220 hdr->tag = (char *)p;
2221 p += XDR_QUADLEN(hdr->taglen);
2222 READ32(hdr->nops);
2223 return 0;
2224}
2225
2226static int decode_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
2227{
Al Viro8687b632006-10-19 23:28:48 -07002228 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 uint32_t opnum;
2230 int32_t nfserr;
2231
2232 READ_BUF(8);
2233 READ32(opnum);
2234 if (opnum != expected) {
Chuck Leverfe82a182007-09-11 18:01:10 -04002235 dprintk("nfs: Server returned operation"
2236 " %d but we issued a request for %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 opnum, expected);
2238 return -EIO;
2239 }
2240 READ32(nfserr);
2241 if (nfserr != NFS_OK)
David Howells0a8ea432006-08-22 20:06:08 -04002242 return -nfs4_stat_to_errno(nfserr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 return 0;
2244}
2245
2246/* Dummy routine */
David Howellsadfa6f92006-08-22 20:06:08 -04002247static int decode_ace(struct xdr_stream *xdr, void *ace, struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
Al Viro8687b632006-10-19 23:28:48 -07002249 __be32 *p;
Trond Myklebust683b57b2006-06-09 09:34:22 -04002250 unsigned int strlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 char *str;
2252
2253 READ_BUF(12);
2254 return decode_opaque_inline(xdr, &strlen, &str);
2255}
2256
2257static int decode_attr_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
2258{
Al Viro8687b632006-10-19 23:28:48 -07002259 uint32_t bmlen;
2260 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
2262 READ_BUF(4);
2263 READ32(bmlen);
2264
2265 bitmap[0] = bitmap[1] = 0;
2266 READ_BUF((bmlen << 2));
2267 if (bmlen > 0) {
2268 READ32(bitmap[0]);
2269 if (bmlen > 1)
2270 READ32(bitmap[1]);
2271 }
2272 return 0;
2273}
2274
Al Viro8687b632006-10-19 23:28:48 -07002275static inline int decode_attr_length(struct xdr_stream *xdr, uint32_t *attrlen, __be32 **savep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276{
Al Viro8687b632006-10-19 23:28:48 -07002277 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
2279 READ_BUF(4);
2280 READ32(*attrlen);
2281 *savep = xdr->p;
2282 return 0;
2283}
2284
2285static int decode_attr_supported(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *bitmask)
2286{
2287 if (likely(bitmap[0] & FATTR4_WORD0_SUPPORTED_ATTRS)) {
2288 decode_attr_bitmap(xdr, bitmask);
2289 bitmap[0] &= ~FATTR4_WORD0_SUPPORTED_ATTRS;
2290 } else
2291 bitmask[0] = bitmask[1] = 0;
2292 dprintk("%s: bitmask=0x%x%x\n", __FUNCTION__, bitmask[0], bitmask[1]);
2293 return 0;
2294}
2295
2296static int decode_attr_type(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *type)
2297{
Al Viro8687b632006-10-19 23:28:48 -07002298 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300 *type = 0;
2301 if (unlikely(bitmap[0] & (FATTR4_WORD0_TYPE - 1U)))
2302 return -EIO;
2303 if (likely(bitmap[0] & FATTR4_WORD0_TYPE)) {
2304 READ_BUF(4);
2305 READ32(*type);
2306 if (*type < NF4REG || *type > NF4NAMEDATTR) {
2307 dprintk("%s: bad type %d\n", __FUNCTION__, *type);
2308 return -EIO;
2309 }
2310 bitmap[0] &= ~FATTR4_WORD0_TYPE;
2311 }
2312 dprintk("%s: type=0%o\n", __FUNCTION__, nfs_type2fmt[*type].nfs2type);
2313 return 0;
2314}
2315
2316static int decode_attr_change(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *change)
2317{
Al Viro8687b632006-10-19 23:28:48 -07002318 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
2320 *change = 0;
2321 if (unlikely(bitmap[0] & (FATTR4_WORD0_CHANGE - 1U)))
2322 return -EIO;
2323 if (likely(bitmap[0] & FATTR4_WORD0_CHANGE)) {
2324 READ_BUF(8);
2325 READ64(*change);
2326 bitmap[0] &= ~FATTR4_WORD0_CHANGE;
2327 }
2328 dprintk("%s: change attribute=%Lu\n", __FUNCTION__,
2329 (unsigned long long)*change);
2330 return 0;
2331}
2332
2333static int decode_attr_size(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *size)
2334{
Al Viro8687b632006-10-19 23:28:48 -07002335 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
2337 *size = 0;
2338 if (unlikely(bitmap[0] & (FATTR4_WORD0_SIZE - 1U)))
2339 return -EIO;
2340 if (likely(bitmap[0] & FATTR4_WORD0_SIZE)) {
2341 READ_BUF(8);
2342 READ64(*size);
2343 bitmap[0] &= ~FATTR4_WORD0_SIZE;
2344 }
2345 dprintk("%s: file size=%Lu\n", __FUNCTION__, (unsigned long long)*size);
2346 return 0;
2347}
2348
2349static int decode_attr_link_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2350{
Al Viro8687b632006-10-19 23:28:48 -07002351 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
2353 *res = 0;
2354 if (unlikely(bitmap[0] & (FATTR4_WORD0_LINK_SUPPORT - 1U)))
2355 return -EIO;
2356 if (likely(bitmap[0] & FATTR4_WORD0_LINK_SUPPORT)) {
2357 READ_BUF(4);
2358 READ32(*res);
2359 bitmap[0] &= ~FATTR4_WORD0_LINK_SUPPORT;
2360 }
2361 dprintk("%s: link support=%s\n", __FUNCTION__, *res == 0 ? "false" : "true");
2362 return 0;
2363}
2364
2365static int decode_attr_symlink_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2366{
Al Viro8687b632006-10-19 23:28:48 -07002367 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
2369 *res = 0;
2370 if (unlikely(bitmap[0] & (FATTR4_WORD0_SYMLINK_SUPPORT - 1U)))
2371 return -EIO;
2372 if (likely(bitmap[0] & FATTR4_WORD0_SYMLINK_SUPPORT)) {
2373 READ_BUF(4);
2374 READ32(*res);
2375 bitmap[0] &= ~FATTR4_WORD0_SYMLINK_SUPPORT;
2376 }
2377 dprintk("%s: symlink support=%s\n", __FUNCTION__, *res == 0 ? "false" : "true");
2378 return 0;
2379}
2380
Trond Myklebust8b4bdcf2006-06-09 09:34:19 -04002381static int decode_attr_fsid(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs_fsid *fsid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382{
Al Viro8687b632006-10-19 23:28:48 -07002383 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
2385 fsid->major = 0;
2386 fsid->minor = 0;
2387 if (unlikely(bitmap[0] & (FATTR4_WORD0_FSID - 1U)))
2388 return -EIO;
2389 if (likely(bitmap[0] & FATTR4_WORD0_FSID)) {
2390 READ_BUF(16);
2391 READ64(fsid->major);
2392 READ64(fsid->minor);
2393 bitmap[0] &= ~FATTR4_WORD0_FSID;
2394 }
2395 dprintk("%s: fsid=(0x%Lx/0x%Lx)\n", __FUNCTION__,
2396 (unsigned long long)fsid->major,
2397 (unsigned long long)fsid->minor);
2398 return 0;
2399}
2400
2401static int decode_attr_lease_time(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2402{
Al Viro8687b632006-10-19 23:28:48 -07002403 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 *res = 60;
2406 if (unlikely(bitmap[0] & (FATTR4_WORD0_LEASE_TIME - 1U)))
2407 return -EIO;
2408 if (likely(bitmap[0] & FATTR4_WORD0_LEASE_TIME)) {
2409 READ_BUF(4);
2410 READ32(*res);
2411 bitmap[0] &= ~FATTR4_WORD0_LEASE_TIME;
2412 }
2413 dprintk("%s: file size=%u\n", __FUNCTION__, (unsigned int)*res);
2414 return 0;
2415}
2416
2417static int decode_attr_aclsupport(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2418{
Al Viro8687b632006-10-19 23:28:48 -07002419 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
2421 *res = ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL;
2422 if (unlikely(bitmap[0] & (FATTR4_WORD0_ACLSUPPORT - 1U)))
2423 return -EIO;
2424 if (likely(bitmap[0] & FATTR4_WORD0_ACLSUPPORT)) {
2425 READ_BUF(4);
2426 READ32(*res);
2427 bitmap[0] &= ~FATTR4_WORD0_ACLSUPPORT;
2428 }
2429 dprintk("%s: ACLs supported=%u\n", __FUNCTION__, (unsigned int)*res);
2430 return 0;
2431}
2432
2433static int decode_attr_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
2434{
Al Viro8687b632006-10-19 23:28:48 -07002435 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
2437 *fileid = 0;
2438 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILEID - 1U)))
2439 return -EIO;
2440 if (likely(bitmap[0] & FATTR4_WORD0_FILEID)) {
2441 READ_BUF(8);
2442 READ64(*fileid);
2443 bitmap[0] &= ~FATTR4_WORD0_FILEID;
2444 }
2445 dprintk("%s: fileid=%Lu\n", __FUNCTION__, (unsigned long long)*fileid);
2446 return 0;
2447}
2448
Manoj Naik99baf622006-06-09 09:34:24 -04002449static int decode_attr_mounted_on_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
2450{
Al Viro8687b632006-10-19 23:28:48 -07002451 __be32 *p;
Manoj Naik99baf622006-06-09 09:34:24 -04002452
2453 *fileid = 0;
2454 if (unlikely(bitmap[1] & (FATTR4_WORD1_MOUNTED_ON_FILEID - 1U)))
2455 return -EIO;
2456 if (likely(bitmap[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)) {
2457 READ_BUF(8);
2458 READ64(*fileid);
2459 bitmap[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
2460 }
2461 dprintk("%s: fileid=%Lu\n", __FUNCTION__, (unsigned long long)*fileid);
2462 return 0;
2463}
2464
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465static int decode_attr_files_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2466{
Al Viro8687b632006-10-19 23:28:48 -07002467 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 int status = 0;
2469
2470 *res = 0;
2471 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_AVAIL - 1U)))
2472 return -EIO;
2473 if (likely(bitmap[0] & FATTR4_WORD0_FILES_AVAIL)) {
2474 READ_BUF(8);
2475 READ64(*res);
2476 bitmap[0] &= ~FATTR4_WORD0_FILES_AVAIL;
2477 }
2478 dprintk("%s: files avail=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2479 return status;
2480}
2481
2482static int decode_attr_files_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2483{
Al Viro8687b632006-10-19 23:28:48 -07002484 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 int status = 0;
2486
2487 *res = 0;
2488 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_FREE - 1U)))
2489 return -EIO;
2490 if (likely(bitmap[0] & FATTR4_WORD0_FILES_FREE)) {
2491 READ_BUF(8);
2492 READ64(*res);
2493 bitmap[0] &= ~FATTR4_WORD0_FILES_FREE;
2494 }
2495 dprintk("%s: files free=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2496 return status;
2497}
2498
2499static int decode_attr_files_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2500{
Al Viro8687b632006-10-19 23:28:48 -07002501 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 int status = 0;
2503
2504 *res = 0;
2505 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_TOTAL - 1U)))
2506 return -EIO;
2507 if (likely(bitmap[0] & FATTR4_WORD0_FILES_TOTAL)) {
2508 READ_BUF(8);
2509 READ64(*res);
2510 bitmap[0] &= ~FATTR4_WORD0_FILES_TOTAL;
2511 }
2512 dprintk("%s: files total=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2513 return status;
2514}
2515
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002516static int decode_pathname(struct xdr_stream *xdr, struct nfs4_pathname *path)
2517{
2518 int n;
Al Viro8687b632006-10-19 23:28:48 -07002519 __be32 *p;
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002520 int status = 0;
2521
2522 READ_BUF(4);
2523 READ32(n);
Andy Adamson33a43f22006-06-09 09:34:30 -04002524 if (n < 0)
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002525 goto out_eio;
Andy Adamson33a43f22006-06-09 09:34:30 -04002526 if (n == 0)
2527 goto root_path;
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002528 dprintk("path ");
2529 path->ncomponents = 0;
2530 while (path->ncomponents < n) {
2531 struct nfs4_string *component = &path->components[path->ncomponents];
2532 status = decode_opaque_inline(xdr, &component->len, &component->data);
2533 if (unlikely(status != 0))
2534 goto out_eio;
2535 if (path->ncomponents != n)
2536 dprintk("/");
2537 dprintk("%s", component->data);
2538 if (path->ncomponents < NFS4_PATHNAME_MAXCOMPONENTS)
2539 path->ncomponents++;
2540 else {
2541 dprintk("cannot parse %d components in path\n", n);
2542 goto out_eio;
2543 }
2544 }
2545out:
2546 dprintk("\n");
2547 return status;
Andy Adamson33a43f22006-06-09 09:34:30 -04002548root_path:
2549/* a root pathname is sent as a zero component4 */
2550 path->ncomponents = 1;
2551 path->components[0].len=0;
2552 path->components[0].data=NULL;
2553 dprintk("path /\n");
2554 goto out;
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002555out_eio:
2556 dprintk(" status %d", status);
2557 status = -EIO;
2558 goto out;
2559}
2560
2561static int decode_attr_fs_locations(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_fs_locations *res)
Trond Myklebust683b57b2006-06-09 09:34:22 -04002562{
2563 int n;
Al Viro8687b632006-10-19 23:28:48 -07002564 __be32 *p;
Trond Myklebust683b57b2006-06-09 09:34:22 -04002565 int status = -EIO;
2566
2567 if (unlikely(bitmap[0] & (FATTR4_WORD0_FS_LOCATIONS -1U)))
2568 goto out;
2569 status = 0;
2570 if (unlikely(!(bitmap[0] & FATTR4_WORD0_FS_LOCATIONS)))
2571 goto out;
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002572 dprintk("%s: fsroot ", __FUNCTION__);
2573 status = decode_pathname(xdr, &res->fs_path);
Trond Myklebust683b57b2006-06-09 09:34:22 -04002574 if (unlikely(status != 0))
2575 goto out;
2576 READ_BUF(4);
2577 READ32(n);
2578 if (n <= 0)
2579 goto out_eio;
2580 res->nlocations = 0;
2581 while (res->nlocations < n) {
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002582 int m;
2583 struct nfs4_fs_location *loc = &res->locations[res->nlocations];
Trond Myklebust683b57b2006-06-09 09:34:22 -04002584
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002585 READ_BUF(4);
2586 READ32(m);
2587 if (m <= 0)
2588 goto out_eio;
2589
2590 loc->nservers = 0;
2591 dprintk("%s: servers ", __FUNCTION__);
2592 while (loc->nservers < m) {
2593 struct nfs4_string *server = &loc->servers[loc->nservers];
2594 status = decode_opaque_inline(xdr, &server->len, &server->data);
2595 if (unlikely(status != 0))
2596 goto out_eio;
2597 dprintk("%s ", server->data);
2598 if (loc->nservers < NFS4_FS_LOCATION_MAXSERVERS)
2599 loc->nservers++;
2600 else {
2601 int i;
2602 dprintk("%s: using first %d of %d servers returned for location %d\n", __FUNCTION__, NFS4_FS_LOCATION_MAXSERVERS, m, res->nlocations);
2603 for (i = loc->nservers; i < m; i++) {
Trond Myklebust2e42c3e2007-05-14 17:20:41 -04002604 unsigned int len;
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002605 char *data;
2606 status = decode_opaque_inline(xdr, &len, &data);
2607 if (unlikely(status != 0))
2608 goto out_eio;
2609 }
2610 }
2611 }
2612 status = decode_pathname(xdr, &loc->rootpath);
Trond Myklebust683b57b2006-06-09 09:34:22 -04002613 if (unlikely(status != 0))
2614 goto out_eio;
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002615 if (res->nlocations < NFS4_FS_LOCATIONS_MAXENTRIES)
Trond Myklebust683b57b2006-06-09 09:34:22 -04002616 res->nlocations++;
2617 }
2618out:
2619 dprintk("%s: fs_locations done, error = %d\n", __FUNCTION__, status);
2620 return status;
2621out_eio:
2622 status = -EIO;
2623 goto out;
2624}
2625
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626static int decode_attr_maxfilesize(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2627{
Al Viro8687b632006-10-19 23:28:48 -07002628 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 int status = 0;
2630
2631 *res = 0;
2632 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXFILESIZE - 1U)))
2633 return -EIO;
2634 if (likely(bitmap[0] & FATTR4_WORD0_MAXFILESIZE)) {
2635 READ_BUF(8);
2636 READ64(*res);
2637 bitmap[0] &= ~FATTR4_WORD0_MAXFILESIZE;
2638 }
2639 dprintk("%s: maxfilesize=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2640 return status;
2641}
2642
2643static int decode_attr_maxlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxlink)
2644{
Al Viro8687b632006-10-19 23:28:48 -07002645 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 int status = 0;
2647
2648 *maxlink = 1;
2649 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXLINK - 1U)))
2650 return -EIO;
2651 if (likely(bitmap[0] & FATTR4_WORD0_MAXLINK)) {
2652 READ_BUF(4);
2653 READ32(*maxlink);
2654 bitmap[0] &= ~FATTR4_WORD0_MAXLINK;
2655 }
2656 dprintk("%s: maxlink=%u\n", __FUNCTION__, *maxlink);
2657 return status;
2658}
2659
2660static int decode_attr_maxname(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxname)
2661{
Al Viro8687b632006-10-19 23:28:48 -07002662 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 int status = 0;
2664
2665 *maxname = 1024;
2666 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXNAME - 1U)))
2667 return -EIO;
2668 if (likely(bitmap[0] & FATTR4_WORD0_MAXNAME)) {
2669 READ_BUF(4);
2670 READ32(*maxname);
2671 bitmap[0] &= ~FATTR4_WORD0_MAXNAME;
2672 }
2673 dprintk("%s: maxname=%u\n", __FUNCTION__, *maxname);
2674 return status;
2675}
2676
2677static int decode_attr_maxread(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2678{
Al Viro8687b632006-10-19 23:28:48 -07002679 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 int status = 0;
2681
2682 *res = 1024;
2683 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXREAD - 1U)))
2684 return -EIO;
2685 if (likely(bitmap[0] & FATTR4_WORD0_MAXREAD)) {
2686 uint64_t maxread;
2687 READ_BUF(8);
2688 READ64(maxread);
2689 if (maxread > 0x7FFFFFFF)
2690 maxread = 0x7FFFFFFF;
2691 *res = (uint32_t)maxread;
2692 bitmap[0] &= ~FATTR4_WORD0_MAXREAD;
2693 }
2694 dprintk("%s: maxread=%lu\n", __FUNCTION__, (unsigned long)*res);
2695 return status;
2696}
2697
2698static int decode_attr_maxwrite(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2699{
Al Viro8687b632006-10-19 23:28:48 -07002700 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 int status = 0;
2702
2703 *res = 1024;
2704 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXWRITE - 1U)))
2705 return -EIO;
2706 if (likely(bitmap[0] & FATTR4_WORD0_MAXWRITE)) {
2707 uint64_t maxwrite;
2708 READ_BUF(8);
2709 READ64(maxwrite);
2710 if (maxwrite > 0x7FFFFFFF)
2711 maxwrite = 0x7FFFFFFF;
2712 *res = (uint32_t)maxwrite;
2713 bitmap[0] &= ~FATTR4_WORD0_MAXWRITE;
2714 }
2715 dprintk("%s: maxwrite=%lu\n", __FUNCTION__, (unsigned long)*res);
2716 return status;
2717}
2718
2719static int decode_attr_mode(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *mode)
2720{
Al Viro8687b632006-10-19 23:28:48 -07002721 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
2723 *mode = 0;
2724 if (unlikely(bitmap[1] & (FATTR4_WORD1_MODE - 1U)))
2725 return -EIO;
2726 if (likely(bitmap[1] & FATTR4_WORD1_MODE)) {
2727 READ_BUF(4);
2728 READ32(*mode);
2729 *mode &= ~S_IFMT;
2730 bitmap[1] &= ~FATTR4_WORD1_MODE;
2731 }
2732 dprintk("%s: file mode=0%o\n", __FUNCTION__, (unsigned int)*mode);
2733 return 0;
2734}
2735
2736static int decode_attr_nlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *nlink)
2737{
Al Viro8687b632006-10-19 23:28:48 -07002738 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
2740 *nlink = 1;
2741 if (unlikely(bitmap[1] & (FATTR4_WORD1_NUMLINKS - 1U)))
2742 return -EIO;
2743 if (likely(bitmap[1] & FATTR4_WORD1_NUMLINKS)) {
2744 READ_BUF(4);
2745 READ32(*nlink);
2746 bitmap[1] &= ~FATTR4_WORD1_NUMLINKS;
2747 }
2748 dprintk("%s: nlink=%u\n", __FUNCTION__, (unsigned int)*nlink);
2749 return 0;
2750}
2751
Trond Myklebust2e42c3e2007-05-14 17:20:41 -04002752static 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 -07002753{
Al Viro8687b632006-10-19 23:28:48 -07002754 uint32_t len;
2755 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
2757 *uid = -2;
2758 if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER - 1U)))
2759 return -EIO;
2760 if (likely(bitmap[1] & FATTR4_WORD1_OWNER)) {
2761 READ_BUF(4);
2762 READ32(len);
2763 READ_BUF(len);
2764 if (len < XDR_MAX_NETOBJ) {
2765 if (nfs_map_name_to_uid(clp, (char *)p, len, uid) != 0)
2766 dprintk("%s: nfs_map_name_to_uid failed!\n",
2767 __FUNCTION__);
2768 } else
Chuck Leverfe82a182007-09-11 18:01:10 -04002769 dprintk("%s: name too long (%u)!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 __FUNCTION__, len);
2771 bitmap[1] &= ~FATTR4_WORD1_OWNER;
2772 }
2773 dprintk("%s: uid=%d\n", __FUNCTION__, (int)*uid);
2774 return 0;
2775}
2776
Trond Myklebust2e42c3e2007-05-14 17:20:41 -04002777static 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 -07002778{
Al Viro8687b632006-10-19 23:28:48 -07002779 uint32_t len;
2780 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
2782 *gid = -2;
2783 if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER_GROUP - 1U)))
2784 return -EIO;
2785 if (likely(bitmap[1] & FATTR4_WORD1_OWNER_GROUP)) {
2786 READ_BUF(4);
2787 READ32(len);
2788 READ_BUF(len);
2789 if (len < XDR_MAX_NETOBJ) {
2790 if (nfs_map_group_to_gid(clp, (char *)p, len, gid) != 0)
2791 dprintk("%s: nfs_map_group_to_gid failed!\n",
2792 __FUNCTION__);
2793 } else
Chuck Leverfe82a182007-09-11 18:01:10 -04002794 dprintk("%s: name too long (%u)!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 __FUNCTION__, len);
2796 bitmap[1] &= ~FATTR4_WORD1_OWNER_GROUP;
2797 }
2798 dprintk("%s: gid=%d\n", __FUNCTION__, (int)*gid);
2799 return 0;
2800}
2801
2802static int decode_attr_rdev(struct xdr_stream *xdr, uint32_t *bitmap, dev_t *rdev)
2803{
Al Viro8687b632006-10-19 23:28:48 -07002804 uint32_t major = 0, minor = 0;
2805 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
2807 *rdev = MKDEV(0,0);
2808 if (unlikely(bitmap[1] & (FATTR4_WORD1_RAWDEV - 1U)))
2809 return -EIO;
2810 if (likely(bitmap[1] & FATTR4_WORD1_RAWDEV)) {
2811 dev_t tmp;
2812
2813 READ_BUF(8);
2814 READ32(major);
2815 READ32(minor);
2816 tmp = MKDEV(major, minor);
2817 if (MAJOR(tmp) == major && MINOR(tmp) == minor)
2818 *rdev = tmp;
2819 bitmap[1] &= ~ FATTR4_WORD1_RAWDEV;
2820 }
2821 dprintk("%s: rdev=(0x%x:0x%x)\n", __FUNCTION__, major, minor);
2822 return 0;
2823}
2824
2825static int decode_attr_space_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2826{
Al Viro8687b632006-10-19 23:28:48 -07002827 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 int status = 0;
2829
2830 *res = 0;
2831 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_AVAIL - 1U)))
2832 return -EIO;
2833 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_AVAIL)) {
2834 READ_BUF(8);
2835 READ64(*res);
2836 bitmap[1] &= ~FATTR4_WORD1_SPACE_AVAIL;
2837 }
2838 dprintk("%s: space avail=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2839 return status;
2840}
2841
2842static int decode_attr_space_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2843{
Al Viro8687b632006-10-19 23:28:48 -07002844 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 int status = 0;
2846
2847 *res = 0;
2848 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_FREE - 1U)))
2849 return -EIO;
2850 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_FREE)) {
2851 READ_BUF(8);
2852 READ64(*res);
2853 bitmap[1] &= ~FATTR4_WORD1_SPACE_FREE;
2854 }
2855 dprintk("%s: space free=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2856 return status;
2857}
2858
2859static int decode_attr_space_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2860{
Al Viro8687b632006-10-19 23:28:48 -07002861 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 int status = 0;
2863
2864 *res = 0;
2865 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_TOTAL - 1U)))
2866 return -EIO;
2867 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_TOTAL)) {
2868 READ_BUF(8);
2869 READ64(*res);
2870 bitmap[1] &= ~FATTR4_WORD1_SPACE_TOTAL;
2871 }
2872 dprintk("%s: space total=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2873 return status;
2874}
2875
2876static int decode_attr_space_used(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *used)
2877{
Al Viro8687b632006-10-19 23:28:48 -07002878 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
2880 *used = 0;
2881 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_USED - 1U)))
2882 return -EIO;
2883 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_USED)) {
2884 READ_BUF(8);
2885 READ64(*used);
2886 bitmap[1] &= ~FATTR4_WORD1_SPACE_USED;
2887 }
2888 dprintk("%s: space used=%Lu\n", __FUNCTION__,
2889 (unsigned long long)*used);
2890 return 0;
2891}
2892
2893static int decode_attr_time(struct xdr_stream *xdr, struct timespec *time)
2894{
Al Viro8687b632006-10-19 23:28:48 -07002895 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 uint64_t sec;
2897 uint32_t nsec;
2898
2899 READ_BUF(12);
2900 READ64(sec);
2901 READ32(nsec);
2902 time->tv_sec = (time_t)sec;
2903 time->tv_nsec = (long)nsec;
2904 return 0;
2905}
2906
2907static int decode_attr_time_access(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2908{
2909 int status = 0;
2910
2911 time->tv_sec = 0;
2912 time->tv_nsec = 0;
2913 if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_ACCESS - 1U)))
2914 return -EIO;
2915 if (likely(bitmap[1] & FATTR4_WORD1_TIME_ACCESS)) {
2916 status = decode_attr_time(xdr, time);
2917 bitmap[1] &= ~FATTR4_WORD1_TIME_ACCESS;
2918 }
2919 dprintk("%s: atime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2920 return status;
2921}
2922
2923static int decode_attr_time_metadata(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2924{
2925 int status = 0;
2926
2927 time->tv_sec = 0;
2928 time->tv_nsec = 0;
2929 if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_METADATA - 1U)))
2930 return -EIO;
2931 if (likely(bitmap[1] & FATTR4_WORD1_TIME_METADATA)) {
2932 status = decode_attr_time(xdr, time);
2933 bitmap[1] &= ~FATTR4_WORD1_TIME_METADATA;
2934 }
2935 dprintk("%s: ctime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2936 return status;
2937}
2938
2939static int decode_attr_time_modify(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2940{
2941 int status = 0;
2942
2943 time->tv_sec = 0;
2944 time->tv_nsec = 0;
2945 if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_MODIFY - 1U)))
2946 return -EIO;
2947 if (likely(bitmap[1] & FATTR4_WORD1_TIME_MODIFY)) {
2948 status = decode_attr_time(xdr, time);
2949 bitmap[1] &= ~FATTR4_WORD1_TIME_MODIFY;
2950 }
2951 dprintk("%s: mtime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2952 return status;
2953}
2954
Al Viro8687b632006-10-19 23:28:48 -07002955static int verify_attr_len(struct xdr_stream *xdr, __be32 *savep, uint32_t attrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956{
2957 unsigned int attrwords = XDR_QUADLEN(attrlen);
2958 unsigned int nwords = xdr->p - savep;
2959
2960 if (unlikely(attrwords != nwords)) {
Chuck Leverfe82a182007-09-11 18:01:10 -04002961 dprintk("%s: server returned incorrect attribute length: "
2962 "%u %c %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 __FUNCTION__,
2964 attrwords << 2,
2965 (attrwords < nwords) ? '<' : '>',
2966 nwords << 2);
2967 return -EIO;
2968 }
2969 return 0;
2970}
2971
2972static int decode_change_info(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
2973{
Al Viro8687b632006-10-19 23:28:48 -07002974 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
2976 READ_BUF(20);
2977 READ32(cinfo->atomic);
2978 READ64(cinfo->before);
2979 READ64(cinfo->after);
2980 return 0;
2981}
2982
2983static int decode_access(struct xdr_stream *xdr, struct nfs4_accessres *access)
2984{
Al Viro8687b632006-10-19 23:28:48 -07002985 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 uint32_t supp, acc;
2987 int status;
2988
2989 status = decode_op_hdr(xdr, OP_ACCESS);
2990 if (status)
2991 return status;
2992 READ_BUF(8);
2993 READ32(supp);
2994 READ32(acc);
2995 access->supported = supp;
2996 access->access = acc;
2997 return 0;
2998}
2999
3000static int decode_close(struct xdr_stream *xdr, struct nfs_closeres *res)
3001{
Al Viro8687b632006-10-19 23:28:48 -07003002 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 int status;
3004
3005 status = decode_op_hdr(xdr, OP_CLOSE);
3006 if (status)
3007 return status;
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003008 READ_BUF(NFS4_STATEID_SIZE);
3009 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 return 0;
3011}
3012
3013static int decode_commit(struct xdr_stream *xdr, struct nfs_writeres *res)
3014{
Al Viro8687b632006-10-19 23:28:48 -07003015 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 int status;
3017
3018 status = decode_op_hdr(xdr, OP_COMMIT);
3019 if (status)
3020 return status;
3021 READ_BUF(8);
3022 COPYMEM(res->verf->verifier, 8);
3023 return 0;
3024}
3025
3026static int decode_create(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
3027{
Al Viro8687b632006-10-19 23:28:48 -07003028 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 uint32_t bmlen;
3030 int status;
3031
3032 status = decode_op_hdr(xdr, OP_CREATE);
3033 if (status)
3034 return status;
3035 if ((status = decode_change_info(xdr, cinfo)))
3036 return status;
3037 READ_BUF(4);
3038 READ32(bmlen);
3039 READ_BUF(bmlen << 2);
3040 return 0;
3041}
3042
3043static int decode_server_caps(struct xdr_stream *xdr, struct nfs4_server_caps_res *res)
3044{
Al Viro8687b632006-10-19 23:28:48 -07003045 __be32 *savep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 uint32_t attrlen,
3047 bitmap[2] = {0};
3048 int status;
3049
3050 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3051 goto xdr_error;
3052 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3053 goto xdr_error;
3054 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3055 goto xdr_error;
3056 if ((status = decode_attr_supported(xdr, bitmap, res->attr_bitmask)) != 0)
3057 goto xdr_error;
3058 if ((status = decode_attr_link_support(xdr, bitmap, &res->has_links)) != 0)
3059 goto xdr_error;
3060 if ((status = decode_attr_symlink_support(xdr, bitmap, &res->has_symlinks)) != 0)
3061 goto xdr_error;
3062 if ((status = decode_attr_aclsupport(xdr, bitmap, &res->acl_bitmask)) != 0)
3063 goto xdr_error;
3064 status = verify_attr_len(xdr, savep, attrlen);
3065xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04003066 dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 return status;
3068}
3069
3070static int decode_statfs(struct xdr_stream *xdr, struct nfs_fsstat *fsstat)
3071{
Al Viro8687b632006-10-19 23:28:48 -07003072 __be32 *savep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 uint32_t attrlen,
3074 bitmap[2] = {0};
3075 int status;
3076
3077 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3078 goto xdr_error;
3079 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3080 goto xdr_error;
3081 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3082 goto xdr_error;
3083
3084 if ((status = decode_attr_files_avail(xdr, bitmap, &fsstat->afiles)) != 0)
3085 goto xdr_error;
3086 if ((status = decode_attr_files_free(xdr, bitmap, &fsstat->ffiles)) != 0)
3087 goto xdr_error;
3088 if ((status = decode_attr_files_total(xdr, bitmap, &fsstat->tfiles)) != 0)
3089 goto xdr_error;
3090 if ((status = decode_attr_space_avail(xdr, bitmap, &fsstat->abytes)) != 0)
3091 goto xdr_error;
3092 if ((status = decode_attr_space_free(xdr, bitmap, &fsstat->fbytes)) != 0)
3093 goto xdr_error;
3094 if ((status = decode_attr_space_total(xdr, bitmap, &fsstat->tbytes)) != 0)
3095 goto xdr_error;
3096
3097 status = verify_attr_len(xdr, savep, attrlen);
3098xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04003099 dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 return status;
3101}
3102
3103static int decode_pathconf(struct xdr_stream *xdr, struct nfs_pathconf *pathconf)
3104{
Al Viro8687b632006-10-19 23:28:48 -07003105 __be32 *savep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 uint32_t attrlen,
3107 bitmap[2] = {0};
3108 int status;
3109
3110 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3111 goto xdr_error;
3112 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3113 goto xdr_error;
3114 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3115 goto xdr_error;
3116
3117 if ((status = decode_attr_maxlink(xdr, bitmap, &pathconf->max_link)) != 0)
3118 goto xdr_error;
3119 if ((status = decode_attr_maxname(xdr, bitmap, &pathconf->max_namelen)) != 0)
3120 goto xdr_error;
3121
3122 status = verify_attr_len(xdr, savep, attrlen);
3123xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04003124 dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 return status;
3126}
3127
3128static int decode_getfattr(struct xdr_stream *xdr, struct nfs_fattr *fattr, const struct nfs_server *server)
3129{
Al Viro8687b632006-10-19 23:28:48 -07003130 __be32 *savep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 uint32_t attrlen,
3132 bitmap[2] = {0},
3133 type;
3134 int status, fmode = 0;
Manoj Naik99baf622006-06-09 09:34:24 -04003135 uint64_t fileid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
3137 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3138 goto xdr_error;
3139 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3140 goto xdr_error;
3141
3142 fattr->bitmap[0] = bitmap[0];
3143 fattr->bitmap[1] = bitmap[1];
3144
3145 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3146 goto xdr_error;
3147
3148
3149 if ((status = decode_attr_type(xdr, bitmap, &type)) != 0)
3150 goto xdr_error;
3151 fattr->type = nfs_type2fmt[type].nfs2type;
3152 fmode = nfs_type2fmt[type].mode;
3153
3154 if ((status = decode_attr_change(xdr, bitmap, &fattr->change_attr)) != 0)
3155 goto xdr_error;
3156 if ((status = decode_attr_size(xdr, bitmap, &fattr->size)) != 0)
3157 goto xdr_error;
Trond Myklebust8b4bdcf2006-06-09 09:34:19 -04003158 if ((status = decode_attr_fsid(xdr, bitmap, &fattr->fsid)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 goto xdr_error;
3160 if ((status = decode_attr_fileid(xdr, bitmap, &fattr->fileid)) != 0)
3161 goto xdr_error;
Trond Myklebust683b57b2006-06-09 09:34:22 -04003162 if ((status = decode_attr_fs_locations(xdr, bitmap, container_of(fattr,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04003163 struct nfs4_fs_locations,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003164 fattr))) != 0)
3165 goto xdr_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 if ((status = decode_attr_mode(xdr, bitmap, &fattr->mode)) != 0)
3167 goto xdr_error;
3168 fattr->mode |= fmode;
3169 if ((status = decode_attr_nlink(xdr, bitmap, &fattr->nlink)) != 0)
3170 goto xdr_error;
David Howells7539bba2006-08-22 20:06:09 -04003171 if ((status = decode_attr_owner(xdr, bitmap, server->nfs_client, &fattr->uid)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 goto xdr_error;
David Howells7539bba2006-08-22 20:06:09 -04003173 if ((status = decode_attr_group(xdr, bitmap, server->nfs_client, &fattr->gid)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 goto xdr_error;
3175 if ((status = decode_attr_rdev(xdr, bitmap, &fattr->rdev)) != 0)
3176 goto xdr_error;
3177 if ((status = decode_attr_space_used(xdr, bitmap, &fattr->du.nfs3.used)) != 0)
3178 goto xdr_error;
3179 if ((status = decode_attr_time_access(xdr, bitmap, &fattr->atime)) != 0)
3180 goto xdr_error;
3181 if ((status = decode_attr_time_metadata(xdr, bitmap, &fattr->ctime)) != 0)
3182 goto xdr_error;
3183 if ((status = decode_attr_time_modify(xdr, bitmap, &fattr->mtime)) != 0)
3184 goto xdr_error;
Manoj Naik99baf622006-06-09 09:34:24 -04003185 if ((status = decode_attr_mounted_on_fileid(xdr, bitmap, &fileid)) != 0)
3186 goto xdr_error;
3187 if (fattr->fileid == 0 && fileid != 0)
3188 fattr->fileid = fileid;
Trond Myklebust33801142005-10-27 22:12:39 -04003189 if ((status = verify_attr_len(xdr, savep, attrlen)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 fattr->valid = NFS_ATTR_FATTR | NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04003192 dprintk("%s: xdr returned %d\n", __FUNCTION__, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 return status;
3194}
3195
3196
3197static int decode_fsinfo(struct xdr_stream *xdr, struct nfs_fsinfo *fsinfo)
3198{
Al Viro8687b632006-10-19 23:28:48 -07003199 __be32 *savep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 uint32_t attrlen, bitmap[2];
3201 int status;
3202
3203 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3204 goto xdr_error;
3205 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3206 goto xdr_error;
3207 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3208 goto xdr_error;
3209
3210 fsinfo->rtmult = fsinfo->wtmult = 512; /* ??? */
3211
3212 if ((status = decode_attr_lease_time(xdr, bitmap, &fsinfo->lease_time)) != 0)
3213 goto xdr_error;
3214 if ((status = decode_attr_maxfilesize(xdr, bitmap, &fsinfo->maxfilesize)) != 0)
3215 goto xdr_error;
3216 if ((status = decode_attr_maxread(xdr, bitmap, &fsinfo->rtmax)) != 0)
3217 goto xdr_error;
3218 fsinfo->rtpref = fsinfo->dtpref = fsinfo->rtmax;
3219 if ((status = decode_attr_maxwrite(xdr, bitmap, &fsinfo->wtmax)) != 0)
3220 goto xdr_error;
3221 fsinfo->wtpref = fsinfo->wtmax;
3222
3223 status = verify_attr_len(xdr, savep, attrlen);
3224xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04003225 dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 return status;
3227}
3228
3229static int decode_getfh(struct xdr_stream *xdr, struct nfs_fh *fh)
3230{
Al Viro8687b632006-10-19 23:28:48 -07003231 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 uint32_t len;
3233 int status;
3234
Trond Myklebust99367812007-07-17 21:52:41 -04003235 /* Zero handle first to allow comparisons */
3236 memset(fh, 0, sizeof(*fh));
3237
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 status = decode_op_hdr(xdr, OP_GETFH);
3239 if (status)
3240 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
3242 READ_BUF(4);
3243 READ32(len);
3244 if (len > NFS4_FHSIZE)
3245 return -EIO;
3246 fh->size = len;
3247 READ_BUF(len);
3248 COPYMEM(fh->data, len);
3249 return 0;
3250}
3251
3252static int decode_link(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
3253{
3254 int status;
3255
3256 status = decode_op_hdr(xdr, OP_LINK);
3257 if (status)
3258 return status;
3259 return decode_change_info(xdr, cinfo);
3260}
3261
3262/*
3263 * We create the owner, so we know a proper owner.id length is 4.
3264 */
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003265static int decode_lock_denied (struct xdr_stream *xdr, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266{
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003267 uint64_t offset, length, clientid;
Al Viro8687b632006-10-19 23:28:48 -07003268 __be32 *p;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003269 uint32_t namelen, type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
3271 READ_BUF(32);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003272 READ64(offset);
3273 READ64(length);
3274 READ32(type);
3275 if (fl != NULL) {
3276 fl->fl_start = (loff_t)offset;
3277 fl->fl_end = fl->fl_start + (loff_t)length - 1;
3278 if (length == ~(uint64_t)0)
3279 fl->fl_end = OFFSET_MAX;
3280 fl->fl_type = F_WRLCK;
3281 if (type & 1)
3282 fl->fl_type = F_RDLCK;
3283 fl->fl_pid = 0;
3284 }
3285 READ64(clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 READ32(namelen);
3287 READ_BUF(namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 return -NFS4ERR_DENIED;
3289}
3290
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003291static int decode_lock(struct xdr_stream *xdr, struct nfs_lock_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292{
Al Viro8687b632006-10-19 23:28:48 -07003293 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 int status;
3295
3296 status = decode_op_hdr(xdr, OP_LOCK);
3297 if (status == 0) {
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003298 READ_BUF(NFS4_STATEID_SIZE);
3299 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 } else if (status == -NFS4ERR_DENIED)
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003301 return decode_lock_denied(xdr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 return status;
3303}
3304
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003305static int decode_lockt(struct xdr_stream *xdr, struct nfs_lockt_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306{
3307 int status;
3308 status = decode_op_hdr(xdr, OP_LOCKT);
3309 if (status == -NFS4ERR_DENIED)
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003310 return decode_lock_denied(xdr, res->denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 return status;
3312}
3313
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003314static int decode_locku(struct xdr_stream *xdr, struct nfs_locku_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315{
Al Viro8687b632006-10-19 23:28:48 -07003316 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 int status;
3318
3319 status = decode_op_hdr(xdr, OP_LOCKU);
3320 if (status == 0) {
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003321 READ_BUF(NFS4_STATEID_SIZE);
3322 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 }
3324 return status;
3325}
3326
3327static int decode_lookup(struct xdr_stream *xdr)
3328{
3329 return decode_op_hdr(xdr, OP_LOOKUP);
3330}
3331
3332/* This is too sick! */
3333static int decode_space_limit(struct xdr_stream *xdr, u64 *maxsize)
3334{
Al Viro8687b632006-10-19 23:28:48 -07003335 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 uint32_t limit_type, nblocks, blocksize;
3337
3338 READ_BUF(12);
3339 READ32(limit_type);
3340 switch (limit_type) {
3341 case 1:
3342 READ64(*maxsize);
3343 break;
3344 case 2:
3345 READ32(nblocks);
3346 READ32(blocksize);
3347 *maxsize = (uint64_t)nblocks * (uint64_t)blocksize;
3348 }
3349 return 0;
3350}
3351
3352static int decode_delegation(struct xdr_stream *xdr, struct nfs_openres *res)
3353{
Al Viro8687b632006-10-19 23:28:48 -07003354 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 uint32_t delegation_type;
3356
3357 READ_BUF(4);
3358 READ32(delegation_type);
3359 if (delegation_type == NFS4_OPEN_DELEGATE_NONE) {
3360 res->delegation_type = 0;
3361 return 0;
3362 }
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003363 READ_BUF(NFS4_STATEID_SIZE+4);
3364 COPYMEM(res->delegation.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 READ32(res->do_recall);
3366 switch (delegation_type) {
3367 case NFS4_OPEN_DELEGATE_READ:
3368 res->delegation_type = FMODE_READ;
3369 break;
3370 case NFS4_OPEN_DELEGATE_WRITE:
3371 res->delegation_type = FMODE_WRITE|FMODE_READ;
3372 if (decode_space_limit(xdr, &res->maxsize) < 0)
3373 return -EIO;
3374 }
David Howells7539bba2006-08-22 20:06:09 -04003375 return decode_ace(xdr, NULL, res->server->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376}
3377
3378static int decode_open(struct xdr_stream *xdr, struct nfs_openres *res)
3379{
Al Viro8687b632006-10-19 23:28:48 -07003380 __be32 *p;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003381 uint32_t savewords, bmlen, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 int status;
3383
3384 status = decode_op_hdr(xdr, OP_OPEN);
3385 if (status)
3386 return status;
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003387 READ_BUF(NFS4_STATEID_SIZE);
3388 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
3390 decode_change_info(xdr, &res->cinfo);
3391
3392 READ_BUF(8);
3393 READ32(res->rflags);
3394 READ32(bmlen);
3395 if (bmlen > 10)
3396 goto xdr_error;
3397
3398 READ_BUF(bmlen << 2);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003399 savewords = min_t(uint32_t, bmlen, NFS4_BITMAP_SIZE);
3400 for (i = 0; i < savewords; ++i)
3401 READ32(res->attrset[i]);
3402 for (; i < NFS4_BITMAP_SIZE; i++)
3403 res->attrset[i] = 0;
3404
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 return decode_delegation(xdr, res);
3406xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04003407 dprintk("%s: Bitmap too large! Length = %u\n", __FUNCTION__, bmlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 return -EIO;
3409}
3410
3411static int decode_open_confirm(struct xdr_stream *xdr, struct nfs_open_confirmres *res)
3412{
Al Viro8687b632006-10-19 23:28:48 -07003413 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 int status;
3415
3416 status = decode_op_hdr(xdr, OP_OPEN_CONFIRM);
3417 if (status)
3418 return status;
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003419 READ_BUF(NFS4_STATEID_SIZE);
3420 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 return 0;
3422}
3423
3424static int decode_open_downgrade(struct xdr_stream *xdr, struct nfs_closeres *res)
3425{
Al Viro8687b632006-10-19 23:28:48 -07003426 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 int status;
3428
3429 status = decode_op_hdr(xdr, OP_OPEN_DOWNGRADE);
3430 if (status)
3431 return status;
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003432 READ_BUF(NFS4_STATEID_SIZE);
3433 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 return 0;
3435}
3436
3437static int decode_putfh(struct xdr_stream *xdr)
3438{
3439 return decode_op_hdr(xdr, OP_PUTFH);
3440}
3441
3442static int decode_putrootfh(struct xdr_stream *xdr)
3443{
3444 return decode_op_hdr(xdr, OP_PUTROOTFH);
3445}
3446
3447static int decode_read(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs_readres *res)
3448{
3449 struct kvec *iov = req->rq_rcv_buf.head;
Al Viro8687b632006-10-19 23:28:48 -07003450 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 uint32_t count, eof, recvd, hdrlen;
3452 int status;
3453
3454 status = decode_op_hdr(xdr, OP_READ);
3455 if (status)
3456 return status;
3457 READ_BUF(8);
3458 READ32(eof);
3459 READ32(count);
3460 hdrlen = (u8 *) p - (u8 *) iov->iov_base;
3461 recvd = req->rq_rcv_buf.len - hdrlen;
3462 if (count > recvd) {
Chuck Leverfe82a182007-09-11 18:01:10 -04003463 dprintk("NFS: server cheating in read reply: "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 "count %u > recvd %u\n", count, recvd);
3465 count = recvd;
3466 eof = 0;
3467 }
3468 xdr_read_pages(xdr, count);
3469 res->eof = eof;
3470 res->count = count;
3471 return 0;
3472}
3473
3474static int decode_readdir(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs4_readdir_res *readdir)
3475{
3476 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
3477 struct page *page = *rcvbuf->pages;
3478 struct kvec *iov = rcvbuf->head;
Chuck Leverbcecff72007-10-26 13:32:03 -04003479 size_t hdrlen;
3480 u32 recvd, pglen = rcvbuf->page_len;
Al Viro8687b632006-10-19 23:28:48 -07003481 __be32 *end, *entry, *p, *kaddr;
Chuck Leverbcecff72007-10-26 13:32:03 -04003482 unsigned int nr;
3483 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484
3485 status = decode_op_hdr(xdr, OP_READDIR);
3486 if (status)
3487 return status;
3488 READ_BUF(8);
3489 COPYMEM(readdir->verifier.data, 8);
Trond Myklebusteadf4592005-06-22 17:16:39 +00003490 dprintk("%s: verifier = 0x%x%x\n",
3491 __FUNCTION__,
3492 ((u32 *)readdir->verifier.data)[0],
3493 ((u32 *)readdir->verifier.data)[1]);
3494
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495
3496 hdrlen = (char *) p - (char *) iov->iov_base;
3497 recvd = rcvbuf->len - hdrlen;
3498 if (pglen > recvd)
3499 pglen = recvd;
3500 xdr_read_pages(xdr, pglen);
3501
3502 BUG_ON(pglen + readdir->pgbase > PAGE_CACHE_SIZE);
Al Viro8687b632006-10-19 23:28:48 -07003503 kaddr = p = kmap_atomic(page, KM_USER0);
David Howellse8896492006-08-24 15:44:19 -04003504 end = p + ((pglen + readdir->pgbase) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 entry = p;
3506 for (nr = 0; *p++; nr++) {
Chuck Leverbcecff72007-10-26 13:32:03 -04003507 u32 len, attrlen, xlen;
David Howellse8896492006-08-24 15:44:19 -04003508 if (end - p < 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 goto short_pkt;
Trond Myklebusteadf4592005-06-22 17:16:39 +00003510 dprintk("cookie = %Lu, ", *((unsigned long long *)p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 p += 2; /* cookie */
3512 len = ntohl(*p++); /* filename length */
3513 if (len > NFS4_MAXNAMLEN) {
Chuck Leverfe82a182007-09-11 18:01:10 -04003514 dprintk("NFS: giant filename in readdir (len 0x%x)\n",
3515 len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 goto err_unmap;
3517 }
David Howellse8896492006-08-24 15:44:19 -04003518 xlen = XDR_QUADLEN(len);
3519 if (end - p < xlen + 1)
3520 goto short_pkt;
Trond Myklebusteadf4592005-06-22 17:16:39 +00003521 dprintk("filename = %*s\n", len, (char *)p);
David Howellse8896492006-08-24 15:44:19 -04003522 p += xlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 len = ntohl(*p++); /* bitmap length */
David Howellse8896492006-08-24 15:44:19 -04003524 if (end - p < len + 1)
3525 goto short_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 p += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 attrlen = XDR_QUADLEN(ntohl(*p++));
David Howellse8896492006-08-24 15:44:19 -04003528 if (end - p < attrlen + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 goto short_pkt;
David Howellse8896492006-08-24 15:44:19 -04003530 p += attrlen; /* attributes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 entry = p;
3532 }
3533 if (!nr && (entry[0] != 0 || entry[1] == 0))
3534 goto short_pkt;
3535out:
3536 kunmap_atomic(kaddr, KM_USER0);
3537 return 0;
3538short_pkt:
Trond Myklebusteadf4592005-06-22 17:16:39 +00003539 dprintk("%s: short packet at entry %d\n", __FUNCTION__, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 entry[0] = entry[1] = 0;
3541 /* truncate listing ? */
3542 if (!nr) {
Chuck Leverfe82a182007-09-11 18:01:10 -04003543 dprintk("NFS: readdir reply truncated!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 entry[1] = 1;
3545 }
3546 goto out;
3547err_unmap:
3548 kunmap_atomic(kaddr, KM_USER0);
3549 return -errno_NFSERR_IO;
3550}
3551
3552static int decode_readlink(struct xdr_stream *xdr, struct rpc_rqst *req)
3553{
3554 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
3555 struct kvec *iov = rcvbuf->head;
Chuck Leverbcecff72007-10-26 13:32:03 -04003556 size_t hdrlen;
3557 u32 len, recvd;
Al Viro8687b632006-10-19 23:28:48 -07003558 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 char *kaddr;
3560 int status;
3561
3562 status = decode_op_hdr(xdr, OP_READLINK);
3563 if (status)
3564 return status;
3565
3566 /* Convert length of symlink */
3567 READ_BUF(4);
3568 READ32(len);
3569 if (len >= rcvbuf->page_len || len <= 0) {
Chuck Leverfe82a182007-09-11 18:01:10 -04003570 dprintk("nfs: server returned giant symlink!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 return -ENAMETOOLONG;
3572 }
3573 hdrlen = (char *) xdr->p - (char *) iov->iov_base;
3574 recvd = req->rq_rcv_buf.len - hdrlen;
3575 if (recvd < len) {
Chuck Leverfe82a182007-09-11 18:01:10 -04003576 dprintk("NFS: server cheating in readlink reply: "
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 "count %u > recvd %u\n", len, recvd);
3578 return -EIO;
3579 }
3580 xdr_read_pages(xdr, len);
3581 /*
3582 * The XDR encode routine has set things up so that
3583 * the link text will be copied directly into the
3584 * buffer. We just have to do overflow-checking,
3585 * and and null-terminate the text (the VFS expects
3586 * null-termination).
3587 */
3588 kaddr = (char *)kmap_atomic(rcvbuf->pages[0], KM_USER0);
3589 kaddr[len+rcvbuf->page_base] = '\0';
3590 kunmap_atomic(kaddr, KM_USER0);
3591 return 0;
3592}
3593
3594static int decode_remove(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
3595{
3596 int status;
3597
3598 status = decode_op_hdr(xdr, OP_REMOVE);
3599 if (status)
3600 goto out;
3601 status = decode_change_info(xdr, cinfo);
3602out:
3603 return status;
3604}
3605
3606static int decode_rename(struct xdr_stream *xdr, struct nfs4_change_info *old_cinfo,
3607 struct nfs4_change_info *new_cinfo)
3608{
3609 int status;
3610
3611 status = decode_op_hdr(xdr, OP_RENAME);
3612 if (status)
3613 goto out;
3614 if ((status = decode_change_info(xdr, old_cinfo)))
3615 goto out;
3616 status = decode_change_info(xdr, new_cinfo);
3617out:
3618 return status;
3619}
3620
3621static int decode_renew(struct xdr_stream *xdr)
3622{
3623 return decode_op_hdr(xdr, OP_RENEW);
3624}
3625
Trond Myklebust56ae19f2005-10-27 22:12:40 -04003626static int
3627decode_restorefh(struct xdr_stream *xdr)
3628{
3629 return decode_op_hdr(xdr, OP_RESTOREFH);
3630}
3631
J. Bruce Fields029d1052005-06-22 17:16:22 +00003632static int decode_getacl(struct xdr_stream *xdr, struct rpc_rqst *req,
3633 size_t *acl_len)
3634{
Al Viro8687b632006-10-19 23:28:48 -07003635 __be32 *savep;
J. Bruce Fields029d1052005-06-22 17:16:22 +00003636 uint32_t attrlen,
3637 bitmap[2] = {0};
3638 struct kvec *iov = req->rq_rcv_buf.head;
3639 int status;
3640
3641 *acl_len = 0;
3642 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3643 goto out;
3644 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3645 goto out;
3646 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3647 goto out;
3648
3649 if (unlikely(bitmap[0] & (FATTR4_WORD0_ACL - 1U)))
3650 return -EIO;
3651 if (likely(bitmap[0] & FATTR4_WORD0_ACL)) {
Chuck Leverbcecff72007-10-26 13:32:03 -04003652 size_t hdrlen;
3653 u32 recvd;
J. Bruce Fields029d1052005-06-22 17:16:22 +00003654
3655 /* We ignore &savep and don't do consistency checks on
3656 * the attr length. Let userspace figure it out.... */
3657 hdrlen = (u8 *)xdr->p - (u8 *)iov->iov_base;
3658 recvd = req->rq_rcv_buf.len - hdrlen;
3659 if (attrlen > recvd) {
Chuck Leverfe82a182007-09-11 18:01:10 -04003660 dprintk("NFS: server cheating in getattr"
J. Bruce Fields029d1052005-06-22 17:16:22 +00003661 " acl reply: attrlen %u > recvd %u\n",
3662 attrlen, recvd);
3663 return -EINVAL;
3664 }
J. Bruce Fieldsc04871e2006-05-30 16:28:58 -04003665 xdr_read_pages(xdr, attrlen);
J. Bruce Fields029d1052005-06-22 17:16:22 +00003666 *acl_len = attrlen;
J. Bruce Fields8c233cf2005-10-13 16:54:27 -04003667 } else
3668 status = -EOPNOTSUPP;
J. Bruce Fields029d1052005-06-22 17:16:22 +00003669
3670out:
3671 return status;
3672}
3673
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674static int
3675decode_savefh(struct xdr_stream *xdr)
3676{
3677 return decode_op_hdr(xdr, OP_SAVEFH);
3678}
3679
3680static int decode_setattr(struct xdr_stream *xdr, struct nfs_setattrres *res)
3681{
Al Viro8687b632006-10-19 23:28:48 -07003682 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 uint32_t bmlen;
3684 int status;
3685
3686
3687 status = decode_op_hdr(xdr, OP_SETATTR);
3688 if (status)
3689 return status;
3690 READ_BUF(4);
3691 READ32(bmlen);
3692 READ_BUF(bmlen << 2);
3693 return 0;
3694}
3695
David Howellsadfa6f92006-08-22 20:06:08 -04003696static int decode_setclientid(struct xdr_stream *xdr, struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697{
Al Viro8687b632006-10-19 23:28:48 -07003698 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 uint32_t opnum;
3700 int32_t nfserr;
3701
3702 READ_BUF(8);
3703 READ32(opnum);
3704 if (opnum != OP_SETCLIENTID) {
Chuck Leverfe82a182007-09-11 18:01:10 -04003705 dprintk("nfs: decode_setclientid: Server returned operation"
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 " %d\n", opnum);
3707 return -EIO;
3708 }
3709 READ32(nfserr);
3710 if (nfserr == NFS_OK) {
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003711 READ_BUF(8 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 READ64(clp->cl_clientid);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003713 COPYMEM(clp->cl_confirm.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 } else if (nfserr == NFSERR_CLID_INUSE) {
3715 uint32_t len;
3716
3717 /* skip netid string */
3718 READ_BUF(4);
3719 READ32(len);
3720 READ_BUF(len);
3721
3722 /* skip uaddr string */
3723 READ_BUF(4);
3724 READ32(len);
3725 READ_BUF(len);
3726 return -NFSERR_CLID_INUSE;
3727 } else
David Howells0a8ea432006-08-22 20:06:08 -04003728 return -nfs4_stat_to_errno(nfserr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
3730 return 0;
3731}
3732
3733static int decode_setclientid_confirm(struct xdr_stream *xdr)
3734{
3735 return decode_op_hdr(xdr, OP_SETCLIENTID_CONFIRM);
3736}
3737
3738static int decode_write(struct xdr_stream *xdr, struct nfs_writeres *res)
3739{
Al Viro8687b632006-10-19 23:28:48 -07003740 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 int status;
3742
3743 status = decode_op_hdr(xdr, OP_WRITE);
3744 if (status)
3745 return status;
3746
3747 READ_BUF(16);
3748 READ32(res->count);
3749 READ32(res->verf->committed);
3750 COPYMEM(res->verf->verifier, 8);
3751 return 0;
3752}
3753
3754static int decode_delegreturn(struct xdr_stream *xdr)
3755{
3756 return decode_op_hdr(xdr, OP_DELEGRETURN);
3757}
3758
3759/*
3760 * Decode OPEN_DOWNGRADE response
3761 */
Al Viro8687b632006-10-19 23:28:48 -07003762static int nfs4_xdr_dec_open_downgrade(struct rpc_rqst *rqstp, __be32 *p, struct nfs_closeres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763{
3764 struct xdr_stream xdr;
3765 struct compound_hdr hdr;
3766 int status;
3767
3768 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3769 status = decode_compound_hdr(&xdr, &hdr);
3770 if (status)
3771 goto out;
3772 status = decode_putfh(&xdr);
3773 if (status)
3774 goto out;
3775 status = decode_open_downgrade(&xdr, res);
Trond Myklebust516a6af2005-10-27 22:12:41 -04003776 if (status != 0)
3777 goto out;
3778 decode_getfattr(&xdr, res->fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779out:
3780 return status;
3781}
3782
3783/*
3784 * END OF "GENERIC" DECODE ROUTINES.
3785 */
3786
3787/*
3788 * Decode ACCESS response
3789 */
Al Viro8687b632006-10-19 23:28:48 -07003790static int nfs4_xdr_dec_access(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_accessres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791{
3792 struct xdr_stream xdr;
3793 struct compound_hdr hdr;
3794 int status;
3795
3796 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3797 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3798 goto out;
Trond Myklebust76b32992007-08-10 17:45:11 -04003799 status = decode_putfh(&xdr);
3800 if (status != 0)
3801 goto out;
3802 status = decode_access(&xdr, res);
3803 if (status != 0)
3804 goto out;
3805 decode_getfattr(&xdr, res->fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806out:
3807 return status;
3808}
3809
3810/*
3811 * Decode LOOKUP response
3812 */
Al Viro8687b632006-10-19 23:28:48 -07003813static int nfs4_xdr_dec_lookup(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_lookup_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814{
3815 struct xdr_stream xdr;
3816 struct compound_hdr hdr;
3817 int status;
3818
3819 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3820 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3821 goto out;
3822 if ((status = decode_putfh(&xdr)) != 0)
3823 goto out;
3824 if ((status = decode_lookup(&xdr)) != 0)
3825 goto out;
3826 if ((status = decode_getfh(&xdr, res->fh)) != 0)
3827 goto out;
3828 status = decode_getfattr(&xdr, res->fattr, res->server);
3829out:
3830 return status;
3831}
3832
3833/*
3834 * Decode LOOKUP_ROOT response
3835 */
Al Viro8687b632006-10-19 23:28:48 -07003836static int nfs4_xdr_dec_lookup_root(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_lookup_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837{
3838 struct xdr_stream xdr;
3839 struct compound_hdr hdr;
3840 int status;
3841
3842 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3843 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3844 goto out;
3845 if ((status = decode_putrootfh(&xdr)) != 0)
3846 goto out;
3847 if ((status = decode_getfh(&xdr, res->fh)) == 0)
3848 status = decode_getfattr(&xdr, res->fattr, res->server);
3849out:
3850 return status;
3851}
3852
3853/*
3854 * Decode REMOVE response
3855 */
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003856static int nfs4_xdr_dec_remove(struct rpc_rqst *rqstp, __be32 *p, struct nfs_removeres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857{
3858 struct xdr_stream xdr;
3859 struct compound_hdr hdr;
3860 int status;
3861
3862 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3863 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3864 goto out;
Trond Myklebust16e42952005-10-27 22:12:44 -04003865 if ((status = decode_putfh(&xdr)) != 0)
3866 goto out;
3867 if ((status = decode_remove(&xdr, &res->cinfo)) != 0)
3868 goto out;
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003869 decode_getfattr(&xdr, &res->dir_attr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870out:
3871 return status;
3872}
3873
3874/*
3875 * Decode RENAME response
3876 */
Al Viro8687b632006-10-19 23:28:48 -07003877static int nfs4_xdr_dec_rename(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_rename_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878{
3879 struct xdr_stream xdr;
3880 struct compound_hdr hdr;
3881 int status;
3882
3883 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3884 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3885 goto out;
3886 if ((status = decode_putfh(&xdr)) != 0)
3887 goto out;
3888 if ((status = decode_savefh(&xdr)) != 0)
3889 goto out;
3890 if ((status = decode_putfh(&xdr)) != 0)
3891 goto out;
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003892 if ((status = decode_rename(&xdr, &res->old_cinfo, &res->new_cinfo)) != 0)
3893 goto out;
3894 /* Current FH is target directory */
3895 if (decode_getfattr(&xdr, res->new_fattr, res->server) != 0)
3896 goto out;
3897 if ((status = decode_restorefh(&xdr)) != 0)
3898 goto out;
3899 decode_getfattr(&xdr, res->old_fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900out:
3901 return status;
3902}
3903
3904/*
3905 * Decode LINK response
3906 */
Al Viro8687b632006-10-19 23:28:48 -07003907static int nfs4_xdr_dec_link(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_link_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908{
3909 struct xdr_stream xdr;
3910 struct compound_hdr hdr;
3911 int status;
3912
3913 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3914 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3915 goto out;
3916 if ((status = decode_putfh(&xdr)) != 0)
3917 goto out;
3918 if ((status = decode_savefh(&xdr)) != 0)
3919 goto out;
3920 if ((status = decode_putfh(&xdr)) != 0)
3921 goto out;
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003922 if ((status = decode_link(&xdr, &res->cinfo)) != 0)
3923 goto out;
3924 /*
3925 * Note order: OP_LINK leaves the directory as the current
3926 * filehandle.
3927 */
3928 if (decode_getfattr(&xdr, res->dir_attr, res->server) != 0)
3929 goto out;
3930 if ((status = decode_restorefh(&xdr)) != 0)
3931 goto out;
3932 decode_getfattr(&xdr, res->fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933out:
3934 return status;
3935}
3936
3937/*
3938 * Decode CREATE response
3939 */
Al Viro8687b632006-10-19 23:28:48 -07003940static int nfs4_xdr_dec_create(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_create_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941{
3942 struct xdr_stream xdr;
3943 struct compound_hdr hdr;
3944 int status;
3945
3946 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3947 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3948 goto out;
3949 if ((status = decode_putfh(&xdr)) != 0)
3950 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04003951 if ((status = decode_savefh(&xdr)) != 0)
3952 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 if ((status = decode_create(&xdr,&res->dir_cinfo)) != 0)
3954 goto out;
3955 if ((status = decode_getfh(&xdr, res->fh)) != 0)
3956 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04003957 if (decode_getfattr(&xdr, res->fattr, res->server) != 0)
3958 goto out;
3959 if ((status = decode_restorefh(&xdr)) != 0)
3960 goto out;
3961 decode_getfattr(&xdr, res->dir_fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962out:
3963 return status;
3964}
3965
3966/*
3967 * Decode SYMLINK response
3968 */
Al Viro8687b632006-10-19 23:28:48 -07003969static int nfs4_xdr_dec_symlink(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_create_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970{
3971 return nfs4_xdr_dec_create(rqstp, p, res);
3972}
3973
3974/*
3975 * Decode GETATTR response
3976 */
Al Viro8687b632006-10-19 23:28:48 -07003977static int nfs4_xdr_dec_getattr(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_getattr_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978{
3979 struct xdr_stream xdr;
3980 struct compound_hdr hdr;
3981 int status;
3982
3983 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3984 status = decode_compound_hdr(&xdr, &hdr);
3985 if (status)
3986 goto out;
3987 status = decode_putfh(&xdr);
3988 if (status)
3989 goto out;
3990 status = decode_getfattr(&xdr, res->fattr, res->server);
3991out:
3992 return status;
3993
3994}
3995
J. Bruce Fields23ec6962005-06-22 17:16:22 +00003996/*
3997 * Encode an SETACL request
3998 */
3999static int
Al Viro8687b632006-10-19 23:28:48 -07004000nfs4_xdr_enc_setacl(struct rpc_rqst *req, __be32 *p, struct nfs_setaclargs *args)
J. Bruce Fields23ec6962005-06-22 17:16:22 +00004001{
4002 struct xdr_stream xdr;
4003 struct compound_hdr hdr = {
4004 .nops = 2,
4005 };
4006 int status;
4007
4008 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
4009 encode_compound_hdr(&xdr, &hdr);
4010 status = encode_putfh(&xdr, args->fh);
4011 if (status)
4012 goto out;
4013 status = encode_setacl(&xdr, args);
4014out:
4015 return status;
4016}
4017/*
4018 * Decode SETACL response
4019 */
4020static int
Al Viro8687b632006-10-19 23:28:48 -07004021nfs4_xdr_dec_setacl(struct rpc_rqst *rqstp, __be32 *p, void *res)
J. Bruce Fields23ec6962005-06-22 17:16:22 +00004022{
4023 struct xdr_stream xdr;
4024 struct compound_hdr hdr;
4025 int status;
4026
4027 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4028 status = decode_compound_hdr(&xdr, &hdr);
4029 if (status)
4030 goto out;
4031 status = decode_putfh(&xdr);
4032 if (status)
4033 goto out;
4034 status = decode_setattr(&xdr, res);
4035out:
4036 return status;
4037}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038
4039/*
J. Bruce Fields029d1052005-06-22 17:16:22 +00004040 * Decode GETACL response
4041 */
4042static int
Al Viro8687b632006-10-19 23:28:48 -07004043nfs4_xdr_dec_getacl(struct rpc_rqst *rqstp, __be32 *p, size_t *acl_len)
J. Bruce Fields029d1052005-06-22 17:16:22 +00004044{
4045 struct xdr_stream xdr;
4046 struct compound_hdr hdr;
4047 int status;
4048
4049 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4050 status = decode_compound_hdr(&xdr, &hdr);
4051 if (status)
4052 goto out;
4053 status = decode_putfh(&xdr);
4054 if (status)
4055 goto out;
4056 status = decode_getacl(&xdr, rqstp, acl_len);
4057
4058out:
4059 return status;
4060}
4061
4062/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 * Decode CLOSE response
4064 */
Al Viro8687b632006-10-19 23:28:48 -07004065static int nfs4_xdr_dec_close(struct rpc_rqst *rqstp, __be32 *p, struct nfs_closeres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066{
4067 struct xdr_stream xdr;
4068 struct compound_hdr hdr;
4069 int status;
4070
4071 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4072 status = decode_compound_hdr(&xdr, &hdr);
4073 if (status)
4074 goto out;
4075 status = decode_putfh(&xdr);
4076 if (status)
4077 goto out;
4078 status = decode_close(&xdr, res);
Trond Myklebust516a6af2005-10-27 22:12:41 -04004079 if (status != 0)
4080 goto out;
4081 /*
4082 * Note: Server may do delete on close for this file
4083 * in which case the getattr call will fail with
4084 * an ESTALE error. Shouldn't be a problem,
4085 * though, since fattr->valid will remain unset.
4086 */
4087 decode_getfattr(&xdr, res->fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088out:
4089 return status;
4090}
4091
4092/*
4093 * Decode OPEN response
4094 */
Al Viro8687b632006-10-19 23:28:48 -07004095static int nfs4_xdr_dec_open(struct rpc_rqst *rqstp, __be32 *p, struct nfs_openres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096{
4097 struct xdr_stream xdr;
4098 struct compound_hdr hdr;
4099 int status;
4100
4101 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4102 status = decode_compound_hdr(&xdr, &hdr);
4103 if (status)
4104 goto out;
4105 status = decode_putfh(&xdr);
4106 if (status)
4107 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04004108 status = decode_savefh(&xdr);
4109 if (status)
4110 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 status = decode_open(&xdr, res);
4112 if (status)
4113 goto out;
Trond Myklebust99367812007-07-17 21:52:41 -04004114 if (decode_getfh(&xdr, &res->fh) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04004116 if (decode_getfattr(&xdr, res->f_attr, res->server) != 0)
4117 goto out;
Trond Myklebust365c8f52007-07-17 21:52:37 -04004118 if (decode_restorefh(&xdr) != 0)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04004119 goto out;
4120 decode_getfattr(&xdr, res->dir_attr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121out:
4122 return status;
4123}
4124
4125/*
4126 * Decode OPEN_CONFIRM response
4127 */
Al Viro8687b632006-10-19 23:28:48 -07004128static int nfs4_xdr_dec_open_confirm(struct rpc_rqst *rqstp, __be32 *p, struct nfs_open_confirmres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129{
4130 struct xdr_stream xdr;
4131 struct compound_hdr hdr;
4132 int status;
4133
4134 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4135 status = decode_compound_hdr(&xdr, &hdr);
4136 if (status)
4137 goto out;
4138 status = decode_putfh(&xdr);
4139 if (status)
4140 goto out;
4141 status = decode_open_confirm(&xdr, res);
4142out:
4143 return status;
4144}
4145
4146/*
4147 * Decode OPEN response
4148 */
Al Viro8687b632006-10-19 23:28:48 -07004149static int nfs4_xdr_dec_open_noattr(struct rpc_rqst *rqstp, __be32 *p, struct nfs_openres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150{
4151 struct xdr_stream xdr;
4152 struct compound_hdr hdr;
4153 int status;
4154
4155 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4156 status = decode_compound_hdr(&xdr, &hdr);
4157 if (status)
4158 goto out;
4159 status = decode_putfh(&xdr);
4160 if (status)
4161 goto out;
4162 status = decode_open(&xdr, res);
Trond Myklebust864472e2006-01-03 09:55:15 +01004163 if (status)
4164 goto out;
4165 decode_getfattr(&xdr, res->f_attr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166out:
4167 return status;
4168}
4169
4170/*
4171 * Decode SETATTR response
4172 */
Al Viro8687b632006-10-19 23:28:48 -07004173static int nfs4_xdr_dec_setattr(struct rpc_rqst *rqstp, __be32 *p, struct nfs_setattrres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174{
4175 struct xdr_stream xdr;
4176 struct compound_hdr hdr;
4177 int status;
4178
4179 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4180 status = decode_compound_hdr(&xdr, &hdr);
4181 if (status)
4182 goto out;
4183 status = decode_putfh(&xdr);
4184 if (status)
4185 goto out;
4186 status = decode_setattr(&xdr, res);
4187 if (status)
4188 goto out;
4189 status = decode_getfattr(&xdr, res->fattr, res->server);
4190 if (status == NFS4ERR_DELAY)
4191 status = 0;
4192out:
4193 return status;
4194}
4195
4196/*
4197 * Decode LOCK response
4198 */
Al Viro8687b632006-10-19 23:28:48 -07004199static int nfs4_xdr_dec_lock(struct rpc_rqst *rqstp, __be32 *p, struct nfs_lock_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200{
4201 struct xdr_stream xdr;
4202 struct compound_hdr hdr;
4203 int status;
4204
4205 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4206 status = decode_compound_hdr(&xdr, &hdr);
4207 if (status)
4208 goto out;
4209 status = decode_putfh(&xdr);
4210 if (status)
4211 goto out;
4212 status = decode_lock(&xdr, res);
4213out:
4214 return status;
4215}
4216
4217/*
4218 * Decode LOCKT response
4219 */
Al Viro8687b632006-10-19 23:28:48 -07004220static int nfs4_xdr_dec_lockt(struct rpc_rqst *rqstp, __be32 *p, struct nfs_lockt_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221{
4222 struct xdr_stream xdr;
4223 struct compound_hdr hdr;
4224 int status;
4225
4226 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4227 status = decode_compound_hdr(&xdr, &hdr);
4228 if (status)
4229 goto out;
4230 status = decode_putfh(&xdr);
4231 if (status)
4232 goto out;
4233 status = decode_lockt(&xdr, res);
4234out:
4235 return status;
4236}
4237
4238/*
4239 * Decode LOCKU response
4240 */
Al Viro8687b632006-10-19 23:28:48 -07004241static int nfs4_xdr_dec_locku(struct rpc_rqst *rqstp, __be32 *p, struct nfs_locku_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242{
4243 struct xdr_stream xdr;
4244 struct compound_hdr hdr;
4245 int status;
4246
4247 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4248 status = decode_compound_hdr(&xdr, &hdr);
4249 if (status)
4250 goto out;
4251 status = decode_putfh(&xdr);
4252 if (status)
4253 goto out;
4254 status = decode_locku(&xdr, res);
4255out:
4256 return status;
4257}
4258
4259/*
4260 * Decode READLINK response
4261 */
Al Viro8687b632006-10-19 23:28:48 -07004262static int nfs4_xdr_dec_readlink(struct rpc_rqst *rqstp, __be32 *p, void *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263{
4264 struct xdr_stream xdr;
4265 struct compound_hdr hdr;
4266 int status;
4267
4268 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4269 status = decode_compound_hdr(&xdr, &hdr);
4270 if (status)
4271 goto out;
4272 status = decode_putfh(&xdr);
4273 if (status)
4274 goto out;
4275 status = decode_readlink(&xdr, rqstp);
4276out:
4277 return status;
4278}
4279
4280/*
4281 * Decode READDIR response
4282 */
Al Viro8687b632006-10-19 23:28:48 -07004283static int nfs4_xdr_dec_readdir(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_readdir_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284{
4285 struct xdr_stream xdr;
4286 struct compound_hdr hdr;
4287 int status;
4288
4289 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4290 status = decode_compound_hdr(&xdr, &hdr);
4291 if (status)
4292 goto out;
4293 status = decode_putfh(&xdr);
4294 if (status)
4295 goto out;
4296 status = decode_readdir(&xdr, rqstp, res);
4297out:
4298 return status;
4299}
4300
4301/*
4302 * Decode Read response
4303 */
Al Viro8687b632006-10-19 23:28:48 -07004304static int nfs4_xdr_dec_read(struct rpc_rqst *rqstp, __be32 *p, struct nfs_readres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305{
4306 struct xdr_stream xdr;
4307 struct compound_hdr hdr;
4308 int status;
4309
4310 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4311 status = decode_compound_hdr(&xdr, &hdr);
4312 if (status)
4313 goto out;
4314 status = decode_putfh(&xdr);
4315 if (status)
4316 goto out;
4317 status = decode_read(&xdr, rqstp, res);
4318 if (!status)
4319 status = res->count;
4320out:
4321 return status;
4322}
4323
4324/*
4325 * Decode WRITE response
4326 */
Al Viro8687b632006-10-19 23:28:48 -07004327static int nfs4_xdr_dec_write(struct rpc_rqst *rqstp, __be32 *p, struct nfs_writeres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328{
4329 struct xdr_stream xdr;
4330 struct compound_hdr hdr;
4331 int status;
4332
4333 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4334 status = decode_compound_hdr(&xdr, &hdr);
4335 if (status)
4336 goto out;
4337 status = decode_putfh(&xdr);
4338 if (status)
4339 goto out;
4340 status = decode_write(&xdr, res);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004341 if (status)
4342 goto out;
4343 decode_getfattr(&xdr, res->fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 if (!status)
4345 status = res->count;
4346out:
4347 return status;
4348}
4349
4350/*
4351 * Decode COMMIT response
4352 */
Al Viro8687b632006-10-19 23:28:48 -07004353static int nfs4_xdr_dec_commit(struct rpc_rqst *rqstp, __be32 *p, struct nfs_writeres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354{
4355 struct xdr_stream xdr;
4356 struct compound_hdr hdr;
4357 int status;
4358
4359 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4360 status = decode_compound_hdr(&xdr, &hdr);
4361 if (status)
4362 goto out;
4363 status = decode_putfh(&xdr);
4364 if (status)
4365 goto out;
4366 status = decode_commit(&xdr, res);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004367 if (status)
4368 goto out;
4369 decode_getfattr(&xdr, res->fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370out:
4371 return status;
4372}
4373
4374/*
4375 * FSINFO request
4376 */
Al Viro8687b632006-10-19 23:28:48 -07004377static int nfs4_xdr_dec_fsinfo(struct rpc_rqst *req, __be32 *p, struct nfs_fsinfo *fsinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378{
4379 struct xdr_stream xdr;
4380 struct compound_hdr hdr;
4381 int status;
4382
4383 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4384 status = decode_compound_hdr(&xdr, &hdr);
4385 if (!status)
4386 status = decode_putfh(&xdr);
4387 if (!status)
4388 status = decode_fsinfo(&xdr, fsinfo);
4389 if (!status)
David Howells0a8ea432006-08-22 20:06:08 -04004390 status = -nfs4_stat_to_errno(hdr.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 return status;
4392}
4393
4394/*
4395 * PATHCONF request
4396 */
Al Viro8687b632006-10-19 23:28:48 -07004397static int nfs4_xdr_dec_pathconf(struct rpc_rqst *req, __be32 *p, struct nfs_pathconf *pathconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398{
4399 struct xdr_stream xdr;
4400 struct compound_hdr hdr;
4401 int status;
4402
4403 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4404 status = decode_compound_hdr(&xdr, &hdr);
4405 if (!status)
4406 status = decode_putfh(&xdr);
4407 if (!status)
4408 status = decode_pathconf(&xdr, pathconf);
4409 return status;
4410}
4411
4412/*
4413 * STATFS request
4414 */
Al Viro8687b632006-10-19 23:28:48 -07004415static int nfs4_xdr_dec_statfs(struct rpc_rqst *req, __be32 *p, struct nfs_fsstat *fsstat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416{
4417 struct xdr_stream xdr;
4418 struct compound_hdr hdr;
4419 int status;
4420
4421 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4422 status = decode_compound_hdr(&xdr, &hdr);
4423 if (!status)
4424 status = decode_putfh(&xdr);
4425 if (!status)
4426 status = decode_statfs(&xdr, fsstat);
4427 return status;
4428}
4429
4430/*
4431 * GETATTR_BITMAP request
4432 */
Al Viro8687b632006-10-19 23:28:48 -07004433static 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 -07004434{
4435 struct xdr_stream xdr;
4436 struct compound_hdr hdr;
4437 int status;
4438
4439 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4440 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
4441 goto out;
4442 if ((status = decode_putfh(&xdr)) != 0)
4443 goto out;
4444 status = decode_server_caps(&xdr, res);
4445out:
4446 return status;
4447}
4448
4449/*
4450 * Decode RENEW response
4451 */
Al Viro8687b632006-10-19 23:28:48 -07004452static int nfs4_xdr_dec_renew(struct rpc_rqst *rqstp, __be32 *p, void *dummy)
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, &rqstp->rq_rcv_buf, p);
4459 status = decode_compound_hdr(&xdr, &hdr);
4460 if (!status)
4461 status = decode_renew(&xdr);
4462 return status;
4463}
4464
4465/*
4466 * a SETCLIENTID request
4467 */
Al Viro8687b632006-10-19 23:28:48 -07004468static int nfs4_xdr_dec_setclientid(struct rpc_rqst *req, __be32 *p,
David Howellsadfa6f92006-08-22 20:06:08 -04004469 struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470{
4471 struct xdr_stream xdr;
4472 struct compound_hdr hdr;
4473 int status;
4474
4475 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4476 status = decode_compound_hdr(&xdr, &hdr);
4477 if (!status)
4478 status = decode_setclientid(&xdr, clp);
4479 if (!status)
David Howells0a8ea432006-08-22 20:06:08 -04004480 status = -nfs4_stat_to_errno(hdr.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 return status;
4482}
4483
4484/*
4485 * a SETCLIENTID_CONFIRM request
4486 */
Al Viro8687b632006-10-19 23:28:48 -07004487static int nfs4_xdr_dec_setclientid_confirm(struct rpc_rqst *req, __be32 *p, struct nfs_fsinfo *fsinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488{
4489 struct xdr_stream xdr;
4490 struct compound_hdr hdr;
4491 int status;
4492
4493 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4494 status = decode_compound_hdr(&xdr, &hdr);
4495 if (!status)
4496 status = decode_setclientid_confirm(&xdr);
4497 if (!status)
4498 status = decode_putrootfh(&xdr);
4499 if (!status)
4500 status = decode_fsinfo(&xdr, fsinfo);
4501 if (!status)
David Howells0a8ea432006-08-22 20:06:08 -04004502 status = -nfs4_stat_to_errno(hdr.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 return status;
4504}
4505
4506/*
4507 * DELEGRETURN request
4508 */
Al Viro8687b632006-10-19 23:28:48 -07004509static int nfs4_xdr_dec_delegreturn(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_delegreturnres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510{
4511 struct xdr_stream xdr;
4512 struct compound_hdr hdr;
4513 int status;
4514
4515 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4516 status = decode_compound_hdr(&xdr, &hdr);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004517 if (status != 0)
4518 goto out;
4519 status = decode_putfh(&xdr);
4520 if (status != 0)
4521 goto out;
4522 status = decode_delegreturn(&xdr);
4523 decode_getfattr(&xdr, res->fattr, res->server);
4524out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 return status;
4526}
4527
Trond Myklebust683b57b2006-06-09 09:34:22 -04004528/*
4529 * FS_LOCATIONS request
4530 */
Al Viro8687b632006-10-19 23:28:48 -07004531static int nfs4_xdr_dec_fs_locations(struct rpc_rqst *req, __be32 *p, struct nfs4_fs_locations *res)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004532{
4533 struct xdr_stream xdr;
4534 struct compound_hdr hdr;
4535 int status;
4536
4537 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4538 status = decode_compound_hdr(&xdr, &hdr);
4539 if (status != 0)
4540 goto out;
4541 if ((status = decode_putfh(&xdr)) != 0)
4542 goto out;
4543 if ((status = decode_lookup(&xdr)) != 0)
4544 goto out;
4545 xdr_enter_page(&xdr, PAGE_SIZE);
4546 status = decode_getfattr(&xdr, &res->fattr, res->server);
4547out:
4548 return status;
4549}
4550
Al Viro0dbb4c62006-10-19 23:28:49 -07004551__be32 *nfs4_decode_dirent(__be32 *p, struct nfs_entry *entry, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552{
4553 uint32_t bitmap[2] = {0};
4554 uint32_t len;
4555
4556 if (!*p++) {
4557 if (!*p)
4558 return ERR_PTR(-EAGAIN);
4559 entry->eof = 1;
4560 return ERR_PTR(-EBADCOOKIE);
4561 }
4562
4563 entry->prev_cookie = entry->cookie;
4564 p = xdr_decode_hyper(p, &entry->cookie);
4565 entry->len = ntohl(*p++);
4566 entry->name = (const char *) p;
4567 p += XDR_QUADLEN(entry->len);
4568
4569 /*
4570 * In case the server doesn't return an inode number,
4571 * we fake one here. (We don't use inode number 0,
4572 * since glibc seems to choke on it...)
4573 */
4574 entry->ino = 1;
4575
4576 len = ntohl(*p++); /* bitmap length */
4577 if (len-- > 0) {
4578 bitmap[0] = ntohl(*p++);
4579 if (len-- > 0) {
4580 bitmap[1] = ntohl(*p++);
4581 p += len;
4582 }
4583 }
4584 len = XDR_QUADLEN(ntohl(*p++)); /* attribute buffer length */
4585 if (len > 0) {
Manoj Naik97d312d2005-06-22 17:16:39 +00004586 if (bitmap[0] & FATTR4_WORD0_RDATTR_ERROR) {
4587 bitmap[0] &= ~FATTR4_WORD0_RDATTR_ERROR;
4588 /* Ignore the return value of rdattr_error for now */
4589 p++;
4590 len--;
4591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 if (bitmap[0] == 0 && bitmap[1] == FATTR4_WORD1_MOUNTED_ON_FILEID)
4593 xdr_decode_hyper(p, &entry->ino);
4594 else if (bitmap[0] == FATTR4_WORD0_FILEID)
4595 xdr_decode_hyper(p, &entry->ino);
4596 p += len;
4597 }
4598
4599 entry->eof = !p[0] && p[1];
4600 return p;
4601}
4602
4603/*
4604 * We need to translate between nfs status return values and
4605 * the local errno values which may not be the same.
4606 */
4607static struct {
4608 int stat;
4609 int errno;
4610} nfs_errtbl[] = {
4611 { NFS4_OK, 0 },
4612 { NFS4ERR_PERM, EPERM },
4613 { NFS4ERR_NOENT, ENOENT },
4614 { NFS4ERR_IO, errno_NFSERR_IO },
4615 { NFS4ERR_NXIO, ENXIO },
4616 { NFS4ERR_ACCESS, EACCES },
4617 { NFS4ERR_EXIST, EEXIST },
4618 { NFS4ERR_XDEV, EXDEV },
4619 { NFS4ERR_NOTDIR, ENOTDIR },
4620 { NFS4ERR_ISDIR, EISDIR },
4621 { NFS4ERR_INVAL, EINVAL },
4622 { NFS4ERR_FBIG, EFBIG },
4623 { NFS4ERR_NOSPC, ENOSPC },
4624 { NFS4ERR_ROFS, EROFS },
4625 { NFS4ERR_MLINK, EMLINK },
4626 { NFS4ERR_NAMETOOLONG, ENAMETOOLONG },
4627 { NFS4ERR_NOTEMPTY, ENOTEMPTY },
4628 { NFS4ERR_DQUOT, EDQUOT },
4629 { NFS4ERR_STALE, ESTALE },
4630 { NFS4ERR_BADHANDLE, EBADHANDLE },
Manoj Naik6ebf3652005-06-22 17:16:39 +00004631 { NFS4ERR_BADOWNER, EINVAL },
4632 { NFS4ERR_BADNAME, EINVAL },
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 { NFS4ERR_BAD_COOKIE, EBADCOOKIE },
4634 { NFS4ERR_NOTSUPP, ENOTSUPP },
4635 { NFS4ERR_TOOSMALL, ETOOSMALL },
4636 { NFS4ERR_SERVERFAULT, ESERVERFAULT },
4637 { NFS4ERR_BADTYPE, EBADTYPE },
4638 { NFS4ERR_LOCKED, EAGAIN },
4639 { NFS4ERR_RESOURCE, EREMOTEIO },
4640 { NFS4ERR_SYMLINK, ELOOP },
4641 { NFS4ERR_OP_ILLEGAL, EOPNOTSUPP },
4642 { NFS4ERR_DEADLOCK, EDEADLK },
4643 { NFS4ERR_WRONGSEC, EPERM }, /* FIXME: this needs
4644 * to be handled by a
4645 * middle-layer.
4646 */
4647 { -1, EIO }
4648};
4649
4650/*
4651 * Convert an NFS error code to a local one.
4652 * This one is used jointly by NFSv2 and NFSv3.
4653 */
4654static int
David Howells0a8ea432006-08-22 20:06:08 -04004655nfs4_stat_to_errno(int stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656{
4657 int i;
4658 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
4659 if (nfs_errtbl[i].stat == stat)
4660 return nfs_errtbl[i].errno;
4661 }
4662 if (stat <= 10000 || stat > 10100) {
4663 /* The server is looney tunes. */
4664 return ESERVERFAULT;
4665 }
4666 /* If we cannot translate the error, the recovery routines should
4667 * handle it.
4668 * Note: remaining NFSv4 error codes have values > 10000, so should
4669 * not conflict with native Linux error codes.
4670 */
4671 return stat;
4672}
4673
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674#define PROC(proc, argtype, restype) \
4675[NFSPROC4_CLNT_##proc] = { \
4676 .p_proc = NFSPROC4_COMPOUND, \
4677 .p_encode = (kxdrproc_t) nfs4_xdr_##argtype, \
4678 .p_decode = (kxdrproc_t) nfs4_xdr_##restype, \
Chuck Lever2bea90d2007-03-29 16:47:53 -04004679 .p_arglen = NFS4_##argtype##_sz, \
4680 .p_replen = NFS4_##restype##_sz, \
Chuck Levercc0175c2006-03-20 13:44:22 -05004681 .p_statidx = NFSPROC4_CLNT_##proc, \
4682 .p_name = #proc, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 }
4684
4685struct rpc_procinfo nfs4_procedures[] = {
4686 PROC(READ, enc_read, dec_read),
4687 PROC(WRITE, enc_write, dec_write),
4688 PROC(COMMIT, enc_commit, dec_commit),
4689 PROC(OPEN, enc_open, dec_open),
4690 PROC(OPEN_CONFIRM, enc_open_confirm, dec_open_confirm),
4691 PROC(OPEN_NOATTR, enc_open_noattr, dec_open_noattr),
4692 PROC(OPEN_DOWNGRADE, enc_open_downgrade, dec_open_downgrade),
4693 PROC(CLOSE, enc_close, dec_close),
4694 PROC(SETATTR, enc_setattr, dec_setattr),
4695 PROC(FSINFO, enc_fsinfo, dec_fsinfo),
4696 PROC(RENEW, enc_renew, dec_renew),
4697 PROC(SETCLIENTID, enc_setclientid, dec_setclientid),
4698 PROC(SETCLIENTID_CONFIRM, enc_setclientid_confirm, dec_setclientid_confirm),
4699 PROC(LOCK, enc_lock, dec_lock),
4700 PROC(LOCKT, enc_lockt, dec_lockt),
4701 PROC(LOCKU, enc_locku, dec_locku),
4702 PROC(ACCESS, enc_access, dec_access),
4703 PROC(GETATTR, enc_getattr, dec_getattr),
4704 PROC(LOOKUP, enc_lookup, dec_lookup),
4705 PROC(LOOKUP_ROOT, enc_lookup_root, dec_lookup_root),
4706 PROC(REMOVE, enc_remove, dec_remove),
4707 PROC(RENAME, enc_rename, dec_rename),
4708 PROC(LINK, enc_link, dec_link),
4709 PROC(SYMLINK, enc_symlink, dec_symlink),
4710 PROC(CREATE, enc_create, dec_create),
4711 PROC(PATHCONF, enc_pathconf, dec_pathconf),
4712 PROC(STATFS, enc_statfs, dec_statfs),
4713 PROC(READLINK, enc_readlink, dec_readlink),
4714 PROC(READDIR, enc_readdir, dec_readdir),
4715 PROC(SERVER_CAPS, enc_server_caps, dec_server_caps),
4716 PROC(DELEGRETURN, enc_delegreturn, dec_delegreturn),
J. Bruce Fields029d1052005-06-22 17:16:22 +00004717 PROC(GETACL, enc_getacl, dec_getacl),
J. Bruce Fields23ec6962005-06-22 17:16:22 +00004718 PROC(SETACL, enc_setacl, dec_setacl),
Trond Myklebust683b57b2006-06-09 09:34:22 -04004719 PROC(FS_LOCATIONS, enc_fs_locations, dec_fs_locations),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720};
4721
4722struct rpc_version nfs_version4 = {
4723 .number = 4,
Tobias Klausere8c96f82006-03-24 03:15:34 -08004724 .nrprocs = ARRAY_SIZE(nfs4_procedures),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 .procs = nfs4_procedures
4726};
4727
4728/*
4729 * Local variables:
4730 * c-basic-offset: 8
4731 * End:
4732 */