blob: bec171a493a4069704e745a4d05d709ddb16e42f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4xdr.c
3 *
4 * Client-side XDR for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/param.h>
39#include <linux/time.h>
40#include <linux/mm.h>
41#include <linux/slab.h>
42#include <linux/utsname.h>
43#include <linux/errno.h>
44#include <linux/string.h>
45#include <linux/in.h>
46#include <linux/pagemap.h>
47#include <linux/proc_fs.h>
48#include <linux/kdev_t.h>
49#include <linux/sunrpc/clnt.h>
50#include <linux/nfs.h>
51#include <linux/nfs4.h>
52#include <linux/nfs_fs.h>
53#include <linux/nfs_idmap.h>
Trond Myklebust4ce79712005-06-22 17:16:21 +000054#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#define NFSDBG_FACILITY NFSDBG_XDR
57
58/* Mapping from NFS error code to "errno" error code. */
59#define errno_NFSERR_IO EIO
60
David Howells0a8ea432006-08-22 20:06:08 -040061static int nfs4_stat_to_errno(int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/* NFSv4 COMPOUND tags are only wanted for debugging purposes */
64#ifdef DEBUG
65#define NFS4_MAXTAGLEN 20
66#else
67#define NFS4_MAXTAGLEN 0
68#endif
69
70/* lock,open owner id:
Trond Myklebust9f958ab2007-07-02 13:58:33 -040071 * we currently use size 2 (u64) out of (NFS4_OPAQUE_LIMIT >> 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 */
Trond Myklebust9f958ab2007-07-02 13:58:33 -040073#define open_owner_id_maxsz (1 + 4)
74#define lock_owner_id_maxsz (1 + 4)
Trond Myklebust9104a552007-07-17 21:52:42 -040075#define decode_lockowner_maxsz (1 + XDR_QUADLEN(IDMAP_NAMESZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#define compound_encode_hdr_maxsz (3 + (NFS4_MAXTAGLEN >> 2))
77#define compound_decode_hdr_maxsz (3 + (NFS4_MAXTAGLEN >> 2))
78#define op_encode_hdr_maxsz (1)
79#define op_decode_hdr_maxsz (2)
Trond Myklebust9104a552007-07-17 21:52:42 -040080#define encode_stateid_maxsz (XDR_QUADLEN(NFS4_STATEID_SIZE))
81#define decode_stateid_maxsz (XDR_QUADLEN(NFS4_STATEID_SIZE))
82#define encode_verifier_maxsz (XDR_QUADLEN(NFS4_VERIFIER_SIZE))
83#define decode_verifier_maxsz (XDR_QUADLEN(NFS4_VERIFIER_SIZE))
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#define encode_putfh_maxsz (op_encode_hdr_maxsz + 1 + \
85 (NFS4_FHSIZE >> 2))
86#define decode_putfh_maxsz (op_decode_hdr_maxsz)
87#define encode_putrootfh_maxsz (op_encode_hdr_maxsz)
88#define decode_putrootfh_maxsz (op_decode_hdr_maxsz)
89#define encode_getfh_maxsz (op_encode_hdr_maxsz)
90#define decode_getfh_maxsz (op_decode_hdr_maxsz + 1 + \
91 ((3+NFS4_FHSIZE) >> 2))
J. Bruce Fields96928202005-06-22 17:16:22 +000092#define nfs4_fattr_bitmap_maxsz 3
93#define encode_getattr_maxsz (op_encode_hdr_maxsz + nfs4_fattr_bitmap_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#define nfs4_name_maxsz (1 + ((3 + NFS4_MAXNAMLEN) >> 2))
95#define nfs4_path_maxsz (1 + ((3 + NFS4_MAXPATHLEN) >> 2))
Trond Myklebustbd625ba2007-07-08 18:38:23 -040096#define nfs4_owner_maxsz (1 + XDR_QUADLEN(IDMAP_NAMESZ))
97#define nfs4_group_maxsz (1 + XDR_QUADLEN(IDMAP_NAMESZ))
J. Bruce Fields96928202005-06-22 17:16:22 +000098/* This is based on getfattr, which uses the most attributes: */
99#define nfs4_fattr_value_maxsz (1 + (1 + 2 + 2 + 4 + 2 + 1 + 1 + 2 + 2 + \
Trond Myklebustbd625ba2007-07-08 18:38:23 -0400100 3 + 3 + 3 + nfs4_owner_maxsz + nfs4_group_maxsz))
J. Bruce Fields96928202005-06-22 17:16:22 +0000101#define nfs4_fattr_maxsz (nfs4_fattr_bitmap_maxsz + \
102 nfs4_fattr_value_maxsz)
103#define decode_getattr_maxsz (op_decode_hdr_maxsz + nfs4_fattr_maxsz)
Trond Myklebust9104a552007-07-17 21:52:42 -0400104#define encode_attrs_maxsz (nfs4_fattr_bitmap_maxsz + \
105 1 + 2 + 1 + \
106 nfs4_owner_maxsz + \
107 nfs4_group_maxsz + \
108 4 + 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#define encode_savefh_maxsz (op_encode_hdr_maxsz)
110#define decode_savefh_maxsz (op_decode_hdr_maxsz)
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400111#define encode_restorefh_maxsz (op_encode_hdr_maxsz)
112#define decode_restorefh_maxsz (op_decode_hdr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#define encode_fsinfo_maxsz (op_encode_hdr_maxsz + 2)
114#define decode_fsinfo_maxsz (op_decode_hdr_maxsz + 11)
115#define encode_renew_maxsz (op_encode_hdr_maxsz + 3)
116#define decode_renew_maxsz (op_decode_hdr_maxsz)
117#define encode_setclientid_maxsz \
118 (op_encode_hdr_maxsz + \
119 4 /*server->ip_addr*/ + \
120 1 /*Netid*/ + \
121 6 /*uaddr*/ + \
122 6 + (NFS4_VERIFIER_SIZE >> 2))
123#define decode_setclientid_maxsz \
124 (op_decode_hdr_maxsz + \
125 2 + \
126 1024) /* large value for CLID_INUSE */
127#define encode_setclientid_confirm_maxsz \
128 (op_encode_hdr_maxsz + \
129 3 + (NFS4_VERIFIER_SIZE >> 2))
130#define decode_setclientid_confirm_maxsz \
131 (op_decode_hdr_maxsz)
Trond Myklebuste6889622007-07-02 13:58:30 -0400132#define encode_lookup_maxsz (op_encode_hdr_maxsz + nfs4_name_maxsz)
133#define decode_lookup_maxsz (op_decode_hdr_maxsz)
Trond Myklebust2cebf822007-07-02 13:57:28 -0400134#define encode_share_access_maxsz \
135 (2)
Trond Myklebust9104a552007-07-17 21:52:42 -0400136#define encode_createmode_maxsz (1 + encode_attrs_maxsz)
Trond Myklebust2cebf822007-07-02 13:57:28 -0400137#define encode_opentype_maxsz (1 + encode_createmode_maxsz)
138#define encode_claim_null_maxsz (1 + nfs4_name_maxsz)
139#define encode_open_maxsz (op_encode_hdr_maxsz + \
140 2 + encode_share_access_maxsz + 2 + \
141 open_owner_id_maxsz + \
142 encode_opentype_maxsz + \
143 encode_claim_null_maxsz)
144#define decode_ace_maxsz (3 + nfs4_owner_maxsz)
Trond Myklebust9104a552007-07-17 21:52:42 -0400145#define decode_delegation_maxsz (1 + decode_stateid_maxsz + 1 + \
Trond Myklebust2cebf822007-07-02 13:57:28 -0400146 decode_ace_maxsz)
147#define decode_change_info_maxsz (5)
148#define decode_open_maxsz (op_decode_hdr_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400149 decode_stateid_maxsz + \
Trond Myklebust2cebf822007-07-02 13:57:28 -0400150 decode_change_info_maxsz + 1 + \
151 nfs4_fattr_bitmap_maxsz + \
152 decode_delegation_maxsz)
Trond Myklebust9104a552007-07-17 21:52:42 -0400153#define encode_open_confirm_maxsz \
154 (op_encode_hdr_maxsz + \
155 encode_stateid_maxsz + 1)
156#define decode_open_confirm_maxsz \
157 (op_decode_hdr_maxsz + \
158 decode_stateid_maxsz)
159#define encode_open_downgrade_maxsz \
160 (op_encode_hdr_maxsz + \
161 encode_stateid_maxsz + 1 + \
162 encode_share_access_maxsz)
163#define decode_open_downgrade_maxsz \
164 (op_decode_hdr_maxsz + \
165 decode_stateid_maxsz)
166#define encode_close_maxsz (op_encode_hdr_maxsz + \
167 1 + encode_stateid_maxsz)
168#define decode_close_maxsz (op_decode_hdr_maxsz + \
169 decode_stateid_maxsz)
170#define encode_setattr_maxsz (op_encode_hdr_maxsz + \
171 encode_stateid_maxsz + \
172 encode_attrs_maxsz)
173#define decode_setattr_maxsz (op_decode_hdr_maxsz + \
174 nfs4_fattr_bitmap_maxsz)
175#define encode_read_maxsz (op_encode_hdr_maxsz + \
176 encode_stateid_maxsz + 3)
177#define decode_read_maxsz (op_decode_hdr_maxsz + 2)
178#define encode_readdir_maxsz (op_encode_hdr_maxsz + \
179 2 + encode_verifier_maxsz + 5)
180#define decode_readdir_maxsz (op_decode_hdr_maxsz + \
181 decode_verifier_maxsz)
182#define encode_readlink_maxsz (op_encode_hdr_maxsz)
183#define decode_readlink_maxsz (op_decode_hdr_maxsz + 1)
184#define encode_write_maxsz (op_encode_hdr_maxsz + \
185 encode_stateid_maxsz + 4)
186#define decode_write_maxsz (op_decode_hdr_maxsz + \
187 2 + decode_verifier_maxsz)
188#define encode_commit_maxsz (op_encode_hdr_maxsz + 3)
189#define decode_commit_maxsz (op_decode_hdr_maxsz + \
190 decode_verifier_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191#define encode_remove_maxsz (op_encode_hdr_maxsz + \
192 nfs4_name_maxsz)
193#define encode_rename_maxsz (op_encode_hdr_maxsz + \
194 2 * nfs4_name_maxsz)
195#define decode_rename_maxsz (op_decode_hdr_maxsz + 5 + 5)
196#define encode_link_maxsz (op_encode_hdr_maxsz + \
197 nfs4_name_maxsz)
198#define decode_link_maxsz (op_decode_hdr_maxsz + 5)
Trond Myklebust9104a552007-07-17 21:52:42 -0400199#define encode_lock_maxsz (op_encode_hdr_maxsz + \
200 7 + \
201 1 + encode_stateid_maxsz + 8)
202#define decode_lock_denied_maxsz \
203 (8 + decode_lockowner_maxsz)
204#define decode_lock_maxsz (op_decode_hdr_maxsz + \
205 decode_lock_denied_maxsz)
206#define encode_lockt_maxsz (op_encode_hdr_maxsz + 12)
207#define decode_lockt_maxsz (op_decode_hdr_maxsz + \
208 decode_lock_denied_maxsz)
209#define encode_locku_maxsz (op_encode_hdr_maxsz + 3 + \
210 encode_stateid_maxsz + \
211 4)
212#define decode_locku_maxsz (op_decode_hdr_maxsz + \
213 decode_stateid_maxsz)
214#define encode_access_maxsz (op_encode_hdr_maxsz + 1)
215#define decode_access_maxsz (op_decode_hdr_maxsz + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216#define encode_symlink_maxsz (op_encode_hdr_maxsz + \
217 1 + nfs4_name_maxsz + \
Chuck Lever94a6d752006-08-22 20:06:23 -0400218 1 + \
J. Bruce Fields96928202005-06-22 17:16:22 +0000219 nfs4_fattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220#define decode_symlink_maxsz (op_decode_hdr_maxsz + 8)
221#define encode_create_maxsz (op_encode_hdr_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400222 1 + 2 + nfs4_name_maxsz + \
223 encode_attrs_maxsz)
Trond Myklebust2cebf822007-07-02 13:57:28 -0400224#define decode_create_maxsz (op_decode_hdr_maxsz + \
225 decode_change_info_maxsz + \
226 nfs4_fattr_bitmap_maxsz)
Trond Myklebust9104a552007-07-17 21:52:42 -0400227#define encode_statfs_maxsz (encode_getattr_maxsz)
228#define decode_statfs_maxsz (decode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229#define encode_delegreturn_maxsz (op_encode_hdr_maxsz + 4)
230#define decode_delegreturn_maxsz (op_decode_hdr_maxsz)
Trond Myklebust9104a552007-07-17 21:52:42 -0400231#define encode_getacl_maxsz (encode_getattr_maxsz)
232#define decode_getacl_maxsz (op_decode_hdr_maxsz + \
233 nfs4_fattr_bitmap_maxsz + 1)
234#define encode_setacl_maxsz (op_encode_hdr_maxsz + \
235 encode_stateid_maxsz + 3)
236#define decode_setacl_maxsz (decode_setattr_maxsz)
Trond Myklebuste6889622007-07-02 13:58:30 -0400237#define encode_fs_locations_maxsz \
238 (encode_getattr_maxsz)
239#define decode_fs_locations_maxsz \
240 (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241#define NFS4_enc_compound_sz (1024) /* XXX: large enough? */
242#define NFS4_dec_compound_sz (1024) /* XXX: large enough? */
243#define NFS4_enc_read_sz (compound_encode_hdr_maxsz + \
244 encode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400245 encode_read_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246#define NFS4_dec_read_sz (compound_decode_hdr_maxsz + \
247 decode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400248 decode_read_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249#define NFS4_enc_readlink_sz (compound_encode_hdr_maxsz + \
250 encode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400251 encode_readlink_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252#define NFS4_dec_readlink_sz (compound_decode_hdr_maxsz + \
253 decode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400254 decode_readlink_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255#define NFS4_enc_readdir_sz (compound_encode_hdr_maxsz + \
256 encode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400257 encode_readdir_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258#define NFS4_dec_readdir_sz (compound_decode_hdr_maxsz + \
259 decode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400260 decode_readdir_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261#define NFS4_enc_write_sz (compound_encode_hdr_maxsz + \
262 encode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400263 encode_write_maxsz + \
Trond Myklebust4f9838c2005-10-27 22:12:44 -0400264 encode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265#define NFS4_dec_write_sz (compound_decode_hdr_maxsz + \
266 decode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400267 decode_write_maxsz + \
Trond Myklebust4f9838c2005-10-27 22:12:44 -0400268 decode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269#define NFS4_enc_commit_sz (compound_encode_hdr_maxsz + \
270 encode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400271 encode_commit_maxsz + \
Trond Myklebust4f9838c2005-10-27 22:12:44 -0400272 encode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273#define NFS4_dec_commit_sz (compound_decode_hdr_maxsz + \
274 decode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400275 decode_commit_maxsz + \
Trond Myklebust4f9838c2005-10-27 22:12:44 -0400276 decode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277#define NFS4_enc_open_sz (compound_encode_hdr_maxsz + \
Trond Myklebust2cebf822007-07-02 13:57:28 -0400278 encode_putfh_maxsz + \
279 encode_savefh_maxsz + \
280 encode_open_maxsz + \
281 encode_getfh_maxsz + \
282 encode_getattr_maxsz + \
283 encode_restorefh_maxsz + \
284 encode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285#define NFS4_dec_open_sz (compound_decode_hdr_maxsz + \
Trond Myklebust2cebf822007-07-02 13:57:28 -0400286 decode_putfh_maxsz + \
287 decode_savefh_maxsz + \
288 decode_open_maxsz + \
289 decode_getfh_maxsz + \
290 decode_getattr_maxsz + \
291 decode_restorefh_maxsz + \
292 decode_getattr_maxsz)
Trond Myklebust9104a552007-07-17 21:52:42 -0400293#define NFS4_enc_open_confirm_sz \
294 (compound_encode_hdr_maxsz + \
295 encode_putfh_maxsz + \
296 encode_open_confirm_maxsz)
297#define NFS4_dec_open_confirm_sz \
298 (compound_decode_hdr_maxsz + \
299 decode_putfh_maxsz + \
300 decode_open_confirm_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301#define NFS4_enc_open_noattr_sz (compound_encode_hdr_maxsz + \
302 encode_putfh_maxsz + \
Trond Myklebust2cebf822007-07-02 13:57:28 -0400303 encode_open_maxsz + \
304 encode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305#define NFS4_dec_open_noattr_sz (compound_decode_hdr_maxsz + \
306 decode_putfh_maxsz + \
Trond Myklebust2cebf822007-07-02 13:57:28 -0400307 decode_open_maxsz + \
308 decode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309#define NFS4_enc_open_downgrade_sz \
310 (compound_encode_hdr_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400311 encode_putfh_maxsz + \
312 encode_open_downgrade_maxsz + \
313 encode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314#define NFS4_dec_open_downgrade_sz \
315 (compound_decode_hdr_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400316 decode_putfh_maxsz + \
317 decode_open_downgrade_maxsz + \
318 decode_getattr_maxsz)
319#define NFS4_enc_close_sz (compound_encode_hdr_maxsz + \
320 encode_putfh_maxsz + \
321 encode_close_maxsz + \
322 encode_getattr_maxsz)
323#define NFS4_dec_close_sz (compound_decode_hdr_maxsz + \
324 decode_putfh_maxsz + \
325 decode_close_maxsz + \
326 decode_getattr_maxsz)
327#define NFS4_enc_setattr_sz (compound_encode_hdr_maxsz + \
328 encode_putfh_maxsz + \
329 encode_setattr_maxsz + \
330 encode_getattr_maxsz)
331#define NFS4_dec_setattr_sz (compound_decode_hdr_maxsz + \
332 decode_putfh_maxsz + \
333 decode_setattr_maxsz + \
334 decode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335#define NFS4_enc_fsinfo_sz (compound_encode_hdr_maxsz + \
336 encode_putfh_maxsz + \
337 encode_fsinfo_maxsz)
338#define NFS4_dec_fsinfo_sz (compound_decode_hdr_maxsz + \
339 decode_putfh_maxsz + \
340 decode_fsinfo_maxsz)
341#define NFS4_enc_renew_sz (compound_encode_hdr_maxsz + \
342 encode_renew_maxsz)
343#define NFS4_dec_renew_sz (compound_decode_hdr_maxsz + \
344 decode_renew_maxsz)
345#define NFS4_enc_setclientid_sz (compound_encode_hdr_maxsz + \
346 encode_setclientid_maxsz)
347#define NFS4_dec_setclientid_sz (compound_decode_hdr_maxsz + \
348 decode_setclientid_maxsz)
349#define NFS4_enc_setclientid_confirm_sz \
350 (compound_encode_hdr_maxsz + \
351 encode_setclientid_confirm_maxsz + \
352 encode_putrootfh_maxsz + \
353 encode_fsinfo_maxsz)
354#define NFS4_dec_setclientid_confirm_sz \
355 (compound_decode_hdr_maxsz + \
356 decode_setclientid_confirm_maxsz + \
357 decode_putrootfh_maxsz + \
358 decode_fsinfo_maxsz)
359#define NFS4_enc_lock_sz (compound_encode_hdr_maxsz + \
360 encode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400361 encode_lock_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362#define NFS4_dec_lock_sz (compound_decode_hdr_maxsz + \
363 decode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400364 decode_lock_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365#define NFS4_enc_lockt_sz (compound_encode_hdr_maxsz + \
366 encode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400367 encode_lockt_maxsz)
368#define NFS4_dec_lockt_sz (compound_decode_hdr_maxsz + \
369 decode_putfh_maxsz + \
370 decode_lockt_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371#define NFS4_enc_locku_sz (compound_encode_hdr_maxsz + \
372 encode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400373 encode_locku_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374#define NFS4_dec_locku_sz (compound_decode_hdr_maxsz + \
375 decode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400376 decode_locku_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377#define NFS4_enc_access_sz (compound_encode_hdr_maxsz + \
378 encode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400379 encode_access_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380#define NFS4_dec_access_sz (compound_decode_hdr_maxsz + \
381 decode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400382 decode_access_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383#define NFS4_enc_getattr_sz (compound_encode_hdr_maxsz + \
384 encode_putfh_maxsz + \
385 encode_getattr_maxsz)
386#define NFS4_dec_getattr_sz (compound_decode_hdr_maxsz + \
387 decode_putfh_maxsz + \
388 decode_getattr_maxsz)
389#define NFS4_enc_lookup_sz (compound_encode_hdr_maxsz + \
390 encode_putfh_maxsz + \
391 encode_lookup_maxsz + \
392 encode_getattr_maxsz + \
393 encode_getfh_maxsz)
394#define NFS4_dec_lookup_sz (compound_decode_hdr_maxsz + \
395 decode_putfh_maxsz + \
Trond Myklebuste6889622007-07-02 13:58:30 -0400396 decode_lookup_maxsz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 decode_getattr_maxsz + \
398 decode_getfh_maxsz)
399#define NFS4_enc_lookup_root_sz (compound_encode_hdr_maxsz + \
400 encode_putrootfh_maxsz + \
401 encode_getattr_maxsz + \
402 encode_getfh_maxsz)
403#define NFS4_dec_lookup_root_sz (compound_decode_hdr_maxsz + \
404 decode_putrootfh_maxsz + \
405 decode_getattr_maxsz + \
406 decode_getfh_maxsz)
407#define NFS4_enc_remove_sz (compound_encode_hdr_maxsz + \
408 encode_putfh_maxsz + \
Trond Myklebust16e42952005-10-27 22:12:44 -0400409 encode_remove_maxsz + \
410 encode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411#define NFS4_dec_remove_sz (compound_decode_hdr_maxsz + \
412 decode_putfh_maxsz + \
Trond Myklebust16e42952005-10-27 22:12:44 -0400413 op_decode_hdr_maxsz + 5 + \
414 decode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415#define NFS4_enc_rename_sz (compound_encode_hdr_maxsz + \
416 encode_putfh_maxsz + \
417 encode_savefh_maxsz + \
418 encode_putfh_maxsz + \
Trond Myklebust6caf2c82005-10-27 22:12:43 -0400419 encode_rename_maxsz + \
420 encode_getattr_maxsz + \
421 encode_restorefh_maxsz + \
422 encode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423#define NFS4_dec_rename_sz (compound_decode_hdr_maxsz + \
424 decode_putfh_maxsz + \
425 decode_savefh_maxsz + \
426 decode_putfh_maxsz + \
Trond Myklebust6caf2c82005-10-27 22:12:43 -0400427 decode_rename_maxsz + \
428 decode_getattr_maxsz + \
429 decode_restorefh_maxsz + \
430 decode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431#define NFS4_enc_link_sz (compound_encode_hdr_maxsz + \
432 encode_putfh_maxsz + \
433 encode_savefh_maxsz + \
434 encode_putfh_maxsz + \
Trond Myklebust91ba2ee2005-10-27 22:12:42 -0400435 encode_link_maxsz + \
436 decode_getattr_maxsz + \
437 encode_restorefh_maxsz + \
438 decode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439#define NFS4_dec_link_sz (compound_decode_hdr_maxsz + \
440 decode_putfh_maxsz + \
441 decode_savefh_maxsz + \
442 decode_putfh_maxsz + \
Trond Myklebust91ba2ee2005-10-27 22:12:42 -0400443 decode_link_maxsz + \
444 decode_getattr_maxsz + \
445 decode_restorefh_maxsz + \
446 decode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447#define NFS4_enc_symlink_sz (compound_encode_hdr_maxsz + \
448 encode_putfh_maxsz + \
449 encode_symlink_maxsz + \
450 encode_getattr_maxsz + \
451 encode_getfh_maxsz)
452#define NFS4_dec_symlink_sz (compound_decode_hdr_maxsz + \
453 decode_putfh_maxsz + \
454 decode_symlink_maxsz + \
455 decode_getattr_maxsz + \
456 decode_getfh_maxsz)
457#define NFS4_enc_create_sz (compound_encode_hdr_maxsz + \
458 encode_putfh_maxsz + \
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400459 encode_savefh_maxsz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 encode_create_maxsz + \
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400461 encode_getfh_maxsz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 encode_getattr_maxsz + \
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400463 encode_restorefh_maxsz + \
464 encode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465#define NFS4_dec_create_sz (compound_decode_hdr_maxsz + \
466 decode_putfh_maxsz + \
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400467 decode_savefh_maxsz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 decode_create_maxsz + \
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400469 decode_getfh_maxsz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 decode_getattr_maxsz + \
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400471 decode_restorefh_maxsz + \
472 decode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473#define NFS4_enc_pathconf_sz (compound_encode_hdr_maxsz + \
474 encode_putfh_maxsz + \
475 encode_getattr_maxsz)
476#define NFS4_dec_pathconf_sz (compound_decode_hdr_maxsz + \
477 decode_putfh_maxsz + \
478 decode_getattr_maxsz)
479#define NFS4_enc_statfs_sz (compound_encode_hdr_maxsz + \
480 encode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400481 encode_statfs_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482#define NFS4_dec_statfs_sz (compound_decode_hdr_maxsz + \
483 decode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400484 decode_statfs_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485#define NFS4_enc_server_caps_sz (compound_encode_hdr_maxsz + \
Trond Myklebustab91f262007-02-02 14:47:17 -0800486 encode_putfh_maxsz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 encode_getattr_maxsz)
488#define NFS4_dec_server_caps_sz (compound_decode_hdr_maxsz + \
Trond Myklebustab91f262007-02-02 14:47:17 -0800489 decode_putfh_maxsz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 decode_getattr_maxsz)
491#define NFS4_enc_delegreturn_sz (compound_encode_hdr_maxsz + \
492 encode_putfh_maxsz + \
Trond Myklebustfa178f22006-01-03 09:55:38 +0100493 encode_delegreturn_maxsz + \
494 encode_getattr_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495#define NFS4_dec_delegreturn_sz (compound_decode_hdr_maxsz + \
Trond Myklebustfa178f22006-01-03 09:55:38 +0100496 decode_delegreturn_maxsz + \
497 decode_getattr_maxsz)
J. Bruce Fields029d1052005-06-22 17:16:22 +0000498#define NFS4_enc_getacl_sz (compound_encode_hdr_maxsz + \
499 encode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400500 encode_getacl_maxsz)
J. Bruce Fields029d1052005-06-22 17:16:22 +0000501#define NFS4_dec_getacl_sz (compound_decode_hdr_maxsz + \
502 decode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400503 decode_getacl_maxsz)
J. Bruce Fields23ec6962005-06-22 17:16:22 +0000504#define NFS4_enc_setacl_sz (compound_encode_hdr_maxsz + \
505 encode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400506 encode_setacl_maxsz)
J. Bruce Fields23ec6962005-06-22 17:16:22 +0000507#define NFS4_dec_setacl_sz (compound_decode_hdr_maxsz + \
508 decode_putfh_maxsz + \
Trond Myklebust9104a552007-07-17 21:52:42 -0400509 decode_setacl_maxsz)
Trond Myklebust683b57b2006-06-09 09:34:22 -0400510#define NFS4_enc_fs_locations_sz \
511 (compound_encode_hdr_maxsz + \
512 encode_putfh_maxsz + \
Trond Myklebuste6889622007-07-02 13:58:30 -0400513 encode_lookup_maxsz + \
514 encode_fs_locations_maxsz)
Trond Myklebust683b57b2006-06-09 09:34:22 -0400515#define NFS4_dec_fs_locations_sz \
516 (compound_decode_hdr_maxsz + \
517 decode_putfh_maxsz + \
Trond Myklebuste6889622007-07-02 13:58:30 -0400518 decode_lookup_maxsz + \
519 decode_fs_locations_maxsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521static struct {
522 unsigned int mode;
523 unsigned int nfs2type;
524} nfs_type2fmt[] = {
525 { 0, NFNON },
526 { S_IFREG, NFREG },
527 { S_IFDIR, NFDIR },
528 { S_IFBLK, NFBLK },
529 { S_IFCHR, NFCHR },
530 { S_IFLNK, NFLNK },
531 { S_IFSOCK, NFSOCK },
532 { S_IFIFO, NFFIFO },
533 { 0, NFNON },
534 { 0, NFNON },
535};
536
537struct compound_hdr {
538 int32_t status;
539 uint32_t nops;
540 uint32_t taglen;
541 char * tag;
542};
543
544/*
545 * START OF "GENERIC" ENCODE ROUTINES.
546 * These may look a little ugly since they are imported from a "generic"
547 * set of XDR encode/decode routines which are intended to be shared by
548 * all of our NFSv4 implementations (OpenBSD, MacOS X...).
549 *
550 * If the pain of reading these is too great, it should be a straightforward
551 * task to translate them into Linux-specific versions which are more
552 * consistent with the style used in NFSv2/v3...
553 */
554#define WRITE32(n) *p++ = htonl(n)
555#define WRITE64(n) do { \
556 *p++ = htonl((uint32_t)((n) >> 32)); \
557 *p++ = htonl((uint32_t)(n)); \
558} while (0)
559#define WRITEMEM(ptr,nbytes) do { \
560 p = xdr_encode_opaque_fixed(p, ptr, nbytes); \
561} while (0)
562
563#define RESERVE_SPACE(nbytes) do { \
564 p = xdr_reserve_space(xdr, nbytes); \
565 if (!p) printk("RESERVE_SPACE(%d) failed in function %s\n", (int) (nbytes), __FUNCTION__); \
566 BUG_ON(!p); \
567} while (0)
568
569static void encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
570{
Al Viro8687b632006-10-19 23:28:48 -0700571 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 p = xdr_reserve_space(xdr, 4 + len);
574 BUG_ON(p == NULL);
575 xdr_encode_opaque(p, str, len);
576}
577
578static int encode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
579{
Al Viro8687b632006-10-19 23:28:48 -0700580 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 dprintk("encode_compound: tag=%.*s\n", (int)hdr->taglen, hdr->tag);
583 BUG_ON(hdr->taglen > NFS4_MAXTAGLEN);
584 RESERVE_SPACE(12+(XDR_QUADLEN(hdr->taglen)<<2));
585 WRITE32(hdr->taglen);
586 WRITEMEM(hdr->tag, hdr->taglen);
587 WRITE32(NFS4_MINOR_VERSION);
588 WRITE32(hdr->nops);
589 return 0;
590}
591
592static void encode_nfs4_verifier(struct xdr_stream *xdr, const nfs4_verifier *verf)
593{
Al Viro8687b632006-10-19 23:28:48 -0700594 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
597 BUG_ON(p == NULL);
598 xdr_encode_opaque_fixed(p, verf->data, NFS4_VERIFIER_SIZE);
599}
600
601static int encode_attrs(struct xdr_stream *xdr, const struct iattr *iap, const struct nfs_server *server)
602{
603 char owner_name[IDMAP_NAMESZ];
604 char owner_group[IDMAP_NAMESZ];
605 int owner_namelen = 0;
606 int owner_grouplen = 0;
Al Viro8687b632006-10-19 23:28:48 -0700607 __be32 *p;
608 __be32 *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 int len;
610 uint32_t bmval0 = 0;
611 uint32_t bmval1 = 0;
612 int status;
613
614 /*
615 * We reserve enough space to write the entire attribute buffer at once.
616 * In the worst-case, this would be
617 * 12(bitmap) + 4(attrlen) + 8(size) + 4(mode) + 4(atime) + 4(mtime)
618 * = 36 bytes, plus any contribution from variable-length fields
J. Bruce Fields23ec6962005-06-22 17:16:22 +0000619 * such as owner/group.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 */
621 len = 16;
622
623 /* Sigh */
624 if (iap->ia_valid & ATTR_SIZE)
625 len += 8;
626 if (iap->ia_valid & ATTR_MODE)
627 len += 4;
628 if (iap->ia_valid & ATTR_UID) {
David Howells7539bba2006-08-22 20:06:09 -0400629 owner_namelen = nfs_map_uid_to_name(server->nfs_client, iap->ia_uid, owner_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (owner_namelen < 0) {
631 printk(KERN_WARNING "nfs: couldn't resolve uid %d to string\n",
632 iap->ia_uid);
633 /* XXX */
634 strcpy(owner_name, "nobody");
635 owner_namelen = sizeof("nobody") - 1;
636 /* goto out; */
637 }
638 len += 4 + (XDR_QUADLEN(owner_namelen) << 2);
639 }
640 if (iap->ia_valid & ATTR_GID) {
David Howells7539bba2006-08-22 20:06:09 -0400641 owner_grouplen = nfs_map_gid_to_group(server->nfs_client, iap->ia_gid, owner_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (owner_grouplen < 0) {
643 printk(KERN_WARNING "nfs4: couldn't resolve gid %d to string\n",
644 iap->ia_gid);
645 strcpy(owner_group, "nobody");
646 owner_grouplen = sizeof("nobody") - 1;
647 /* goto out; */
648 }
649 len += 4 + (XDR_QUADLEN(owner_grouplen) << 2);
650 }
651 if (iap->ia_valid & ATTR_ATIME_SET)
652 len += 16;
653 else if (iap->ia_valid & ATTR_ATIME)
654 len += 4;
655 if (iap->ia_valid & ATTR_MTIME_SET)
656 len += 16;
657 else if (iap->ia_valid & ATTR_MTIME)
658 len += 4;
659 RESERVE_SPACE(len);
660
661 /*
662 * We write the bitmap length now, but leave the bitmap and the attribute
663 * buffer length to be backfilled at the end of this routine.
664 */
665 WRITE32(2);
666 q = p;
667 p += 3;
668
669 if (iap->ia_valid & ATTR_SIZE) {
670 bmval0 |= FATTR4_WORD0_SIZE;
671 WRITE64(iap->ia_size);
672 }
673 if (iap->ia_valid & ATTR_MODE) {
674 bmval1 |= FATTR4_WORD1_MODE;
Trond Myklebustcf3fff52006-01-03 09:55:53 +0100675 WRITE32(iap->ia_mode & S_IALLUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677 if (iap->ia_valid & ATTR_UID) {
678 bmval1 |= FATTR4_WORD1_OWNER;
679 WRITE32(owner_namelen);
680 WRITEMEM(owner_name, owner_namelen);
681 }
682 if (iap->ia_valid & ATTR_GID) {
683 bmval1 |= FATTR4_WORD1_OWNER_GROUP;
684 WRITE32(owner_grouplen);
685 WRITEMEM(owner_group, owner_grouplen);
686 }
687 if (iap->ia_valid & ATTR_ATIME_SET) {
688 bmval1 |= FATTR4_WORD1_TIME_ACCESS_SET;
689 WRITE32(NFS4_SET_TO_CLIENT_TIME);
690 WRITE32(0);
691 WRITE32(iap->ia_mtime.tv_sec);
692 WRITE32(iap->ia_mtime.tv_nsec);
693 }
694 else if (iap->ia_valid & ATTR_ATIME) {
695 bmval1 |= FATTR4_WORD1_TIME_ACCESS_SET;
696 WRITE32(NFS4_SET_TO_SERVER_TIME);
697 }
698 if (iap->ia_valid & ATTR_MTIME_SET) {
699 bmval1 |= FATTR4_WORD1_TIME_MODIFY_SET;
700 WRITE32(NFS4_SET_TO_CLIENT_TIME);
701 WRITE32(0);
702 WRITE32(iap->ia_mtime.tv_sec);
703 WRITE32(iap->ia_mtime.tv_nsec);
704 }
705 else if (iap->ia_valid & ATTR_MTIME) {
706 bmval1 |= FATTR4_WORD1_TIME_MODIFY_SET;
707 WRITE32(NFS4_SET_TO_SERVER_TIME);
708 }
709
710 /*
711 * Now we backfill the bitmap and the attribute buffer length.
712 */
713 if (len != ((char *)p - (char *)q) + 4) {
714 printk ("encode_attr: Attr length calculation error! %u != %Zu\n",
715 len, ((char *)p - (char *)q) + 4);
716 BUG();
717 }
718 len = (char *)p - (char *)q - 12;
719 *q++ = htonl(bmval0);
720 *q++ = htonl(bmval1);
721 *q++ = htonl(len);
722
723 status = 0;
724/* out: */
725 return status;
726}
727
728static int encode_access(struct xdr_stream *xdr, u32 access)
729{
Al Viro8687b632006-10-19 23:28:48 -0700730 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 RESERVE_SPACE(8);
733 WRITE32(OP_ACCESS);
734 WRITE32(access);
735
736 return 0;
737}
738
739static int encode_close(struct xdr_stream *xdr, const struct nfs_closeargs *arg)
740{
Al Viro8687b632006-10-19 23:28:48 -0700741 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Trond Myklebust8ae20ab2007-05-14 16:50:45 -0400743 RESERVE_SPACE(8+NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 WRITE32(OP_CLOSE);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700745 WRITE32(arg->seqid->sequence->counter);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -0400746 WRITEMEM(arg->stateid->data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 return 0;
749}
750
751static int encode_commit(struct xdr_stream *xdr, const struct nfs_writeargs *args)
752{
Al Viro8687b632006-10-19 23:28:48 -0700753 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 RESERVE_SPACE(16);
756 WRITE32(OP_COMMIT);
757 WRITE64(args->offset);
758 WRITE32(args->count);
759
760 return 0;
761}
762
763static int encode_create(struct xdr_stream *xdr, const struct nfs4_create_arg *create)
764{
Al Viro8687b632006-10-19 23:28:48 -0700765 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 RESERVE_SPACE(8);
768 WRITE32(OP_CREATE);
769 WRITE32(create->ftype);
770
771 switch (create->ftype) {
772 case NF4LNK:
Chuck Lever94a6d752006-08-22 20:06:23 -0400773 RESERVE_SPACE(4);
774 WRITE32(create->u.symlink.len);
775 xdr_write_pages(xdr, create->u.symlink.pages, 0, create->u.symlink.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 break;
777
778 case NF4BLK: case NF4CHR:
779 RESERVE_SPACE(8);
780 WRITE32(create->u.device.specdata1);
781 WRITE32(create->u.device.specdata2);
782 break;
783
784 default:
785 break;
786 }
787
788 RESERVE_SPACE(4 + create->name->len);
789 WRITE32(create->name->len);
790 WRITEMEM(create->name->name, create->name->len);
791
792 return encode_attrs(xdr, create->attrs, create->server);
793}
794
795static int encode_getattr_one(struct xdr_stream *xdr, uint32_t bitmap)
796{
Al Viro8687b632006-10-19 23:28:48 -0700797 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 RESERVE_SPACE(12);
800 WRITE32(OP_GETATTR);
801 WRITE32(1);
802 WRITE32(bitmap);
803 return 0;
804}
805
806static int encode_getattr_two(struct xdr_stream *xdr, uint32_t bm0, uint32_t bm1)
807{
Al Viro8687b632006-10-19 23:28:48 -0700808 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 RESERVE_SPACE(16);
811 WRITE32(OP_GETATTR);
812 WRITE32(2);
813 WRITE32(bm0);
814 WRITE32(bm1);
815 return 0;
816}
817
818static int encode_getfattr(struct xdr_stream *xdr, const u32* bitmask)
819{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return encode_getattr_two(xdr,
821 bitmask[0] & nfs4_fattr_bitmap[0],
822 bitmask[1] & nfs4_fattr_bitmap[1]);
823}
824
825static int encode_fsinfo(struct xdr_stream *xdr, const u32* bitmask)
826{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return encode_getattr_two(xdr, bitmask[0] & nfs4_fsinfo_bitmap[0],
828 bitmask[1] & nfs4_fsinfo_bitmap[1]);
829}
830
Manoj Naik830b8e32006-06-09 09:34:25 -0400831static int encode_fs_locations(struct xdr_stream *xdr, const u32* bitmask)
832{
833 return encode_getattr_two(xdr,
834 bitmask[0] & nfs4_fs_locations_bitmap[0],
835 bitmask[1] & nfs4_fs_locations_bitmap[1]);
836}
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838static int encode_getfh(struct xdr_stream *xdr)
839{
Al Viro8687b632006-10-19 23:28:48 -0700840 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 RESERVE_SPACE(4);
843 WRITE32(OP_GETFH);
844
845 return 0;
846}
847
848static int encode_link(struct xdr_stream *xdr, const struct qstr *name)
849{
Al Viro8687b632006-10-19 23:28:48 -0700850 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 RESERVE_SPACE(8 + name->len);
853 WRITE32(OP_LINK);
854 WRITE32(name->len);
855 WRITEMEM(name->name, name->len);
856
857 return 0;
858}
859
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100860static inline int nfs4_lock_type(struct file_lock *fl, int block)
861{
862 if ((fl->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) == F_RDLCK)
863 return block ? NFS4_READW_LT : NFS4_READ_LT;
864 return block ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
865}
866
867static inline uint64_t nfs4_lock_length(struct file_lock *fl)
868{
869 if (fl->fl_end == OFFSET_MAX)
870 return ~(uint64_t)0;
871 return fl->fl_end - fl->fl_start + 1;
872}
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874/*
875 * opcode,type,reclaim,offset,length,new_lock_owner = 32
876 * open_seqid,open_stateid,lock_seqid,lock_owner.clientid, lock_owner.id = 40
877 */
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100878static int encode_lock(struct xdr_stream *xdr, const struct nfs_lock_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Al Viro8687b632006-10-19 23:28:48 -0700880 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 RESERVE_SPACE(32);
883 WRITE32(OP_LOCK);
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100884 WRITE32(nfs4_lock_type(args->fl, args->block));
885 WRITE32(args->reclaim);
886 WRITE64(args->fl->fl_start);
887 WRITE64(nfs4_lock_length(args->fl));
888 WRITE32(args->new_lock_owner);
889 if (args->new_lock_owner){
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400890 RESERVE_SPACE(4+NFS4_STATEID_SIZE+32);
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100891 WRITE32(args->open_seqid->sequence->counter);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -0400892 WRITEMEM(args->open_stateid->data, NFS4_STATEID_SIZE);
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100893 WRITE32(args->lock_seqid->sequence->counter);
894 WRITE64(args->lock_owner.clientid);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400895 WRITE32(16);
896 WRITEMEM("lock id:", 8);
897 WRITE64(args->lock_owner.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
899 else {
Trond Myklebust8ae20ab2007-05-14 16:50:45 -0400900 RESERVE_SPACE(NFS4_STATEID_SIZE+4);
901 WRITEMEM(args->lock_stateid->data, NFS4_STATEID_SIZE);
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100902 WRITE32(args->lock_seqid->sequence->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
904
905 return 0;
906}
907
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100908static int encode_lockt(struct xdr_stream *xdr, const struct nfs_lockt_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Al Viro8687b632006-10-19 23:28:48 -0700910 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400912 RESERVE_SPACE(52);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 WRITE32(OP_LOCKT);
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100914 WRITE32(nfs4_lock_type(args->fl, 0));
915 WRITE64(args->fl->fl_start);
916 WRITE64(nfs4_lock_length(args->fl));
917 WRITE64(args->lock_owner.clientid);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400918 WRITE32(16);
919 WRITEMEM("lock id:", 8);
920 WRITE64(args->lock_owner.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 return 0;
923}
924
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100925static int encode_locku(struct xdr_stream *xdr, const struct nfs_locku_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Al Viro8687b632006-10-19 23:28:48 -0700927 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Trond Myklebust8ae20ab2007-05-14 16:50:45 -0400929 RESERVE_SPACE(12+NFS4_STATEID_SIZE+16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 WRITE32(OP_LOCKU);
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100931 WRITE32(nfs4_lock_type(args->fl, 0));
932 WRITE32(args->seqid->sequence->counter);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -0400933 WRITEMEM(args->stateid->data, NFS4_STATEID_SIZE);
Trond Myklebust911d1aa2006-01-03 09:55:16 +0100934 WRITE64(args->fl->fl_start);
935 WRITE64(nfs4_lock_length(args->fl));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 return 0;
938}
939
940static int encode_lookup(struct xdr_stream *xdr, const struct qstr *name)
941{
942 int len = name->len;
Al Viro8687b632006-10-19 23:28:48 -0700943 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 RESERVE_SPACE(8 + len);
946 WRITE32(OP_LOOKUP);
947 WRITE32(len);
948 WRITEMEM(name->name, len);
949
950 return 0;
951}
952
953static void encode_share_access(struct xdr_stream *xdr, int open_flags)
954{
Al Viro8687b632006-10-19 23:28:48 -0700955 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 RESERVE_SPACE(8);
958 switch (open_flags & (FMODE_READ|FMODE_WRITE)) {
959 case FMODE_READ:
960 WRITE32(NFS4_SHARE_ACCESS_READ);
961 break;
962 case FMODE_WRITE:
963 WRITE32(NFS4_SHARE_ACCESS_WRITE);
964 break;
965 case FMODE_READ|FMODE_WRITE:
966 WRITE32(NFS4_SHARE_ACCESS_BOTH);
967 break;
968 default:
969 BUG();
970 }
971 WRITE32(0); /* for linux, share_deny = 0 always */
972}
973
974static inline void encode_openhdr(struct xdr_stream *xdr, const struct nfs_openargs *arg)
975{
Al Viro8687b632006-10-19 23:28:48 -0700976 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 /*
978 * opcode 4, seqid 4, share_access 4, share_deny 4, clientid 8, ownerlen 4,
979 * owner 4 = 32
980 */
981 RESERVE_SPACE(8);
982 WRITE32(OP_OPEN);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700983 WRITE32(arg->seqid->sequence->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 encode_share_access(xdr, arg->open_flags);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400985 RESERVE_SPACE(28);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 WRITE64(arg->clientid);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400987 WRITE32(16);
988 WRITEMEM("open id:", 8);
989 WRITE64(arg->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
992static inline void encode_createmode(struct xdr_stream *xdr, const struct nfs_openargs *arg)
993{
Al Viro8687b632006-10-19 23:28:48 -0700994 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 RESERVE_SPACE(4);
997 switch(arg->open_flags & O_EXCL) {
998 case 0:
999 WRITE32(NFS4_CREATE_UNCHECKED);
1000 encode_attrs(xdr, arg->u.attrs, arg->server);
1001 break;
1002 default:
1003 WRITE32(NFS4_CREATE_EXCLUSIVE);
1004 encode_nfs4_verifier(xdr, &arg->u.verifier);
1005 }
1006}
1007
1008static void encode_opentype(struct xdr_stream *xdr, const struct nfs_openargs *arg)
1009{
Al Viro8687b632006-10-19 23:28:48 -07001010 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 RESERVE_SPACE(4);
1013 switch (arg->open_flags & O_CREAT) {
1014 case 0:
1015 WRITE32(NFS4_OPEN_NOCREATE);
1016 break;
1017 default:
1018 BUG_ON(arg->claim != NFS4_OPEN_CLAIM_NULL);
1019 WRITE32(NFS4_OPEN_CREATE);
1020 encode_createmode(xdr, arg);
1021 }
1022}
1023
1024static inline void encode_delegation_type(struct xdr_stream *xdr, int delegation_type)
1025{
Al Viro8687b632006-10-19 23:28:48 -07001026 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 RESERVE_SPACE(4);
1029 switch (delegation_type) {
1030 case 0:
1031 WRITE32(NFS4_OPEN_DELEGATE_NONE);
1032 break;
1033 case FMODE_READ:
1034 WRITE32(NFS4_OPEN_DELEGATE_READ);
1035 break;
1036 case FMODE_WRITE|FMODE_READ:
1037 WRITE32(NFS4_OPEN_DELEGATE_WRITE);
1038 break;
1039 default:
1040 BUG();
1041 }
1042}
1043
1044static inline void encode_claim_null(struct xdr_stream *xdr, const struct qstr *name)
1045{
Al Viro8687b632006-10-19 23:28:48 -07001046 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 RESERVE_SPACE(4);
1049 WRITE32(NFS4_OPEN_CLAIM_NULL);
1050 encode_string(xdr, name->len, name->name);
1051}
1052
1053static inline void encode_claim_previous(struct xdr_stream *xdr, int type)
1054{
Al Viro8687b632006-10-19 23:28:48 -07001055 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 RESERVE_SPACE(4);
1058 WRITE32(NFS4_OPEN_CLAIM_PREVIOUS);
1059 encode_delegation_type(xdr, type);
1060}
1061
1062static inline void encode_claim_delegate_cur(struct xdr_stream *xdr, const struct qstr *name, const nfs4_stateid *stateid)
1063{
Al Viro8687b632006-10-19 23:28:48 -07001064 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001066 RESERVE_SPACE(4+NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 WRITE32(NFS4_OPEN_CLAIM_DELEGATE_CUR);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001068 WRITEMEM(stateid->data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 encode_string(xdr, name->len, name->name);
1070}
1071
1072static int encode_open(struct xdr_stream *xdr, const struct nfs_openargs *arg)
1073{
1074 encode_openhdr(xdr, arg);
1075 encode_opentype(xdr, arg);
1076 switch (arg->claim) {
1077 case NFS4_OPEN_CLAIM_NULL:
1078 encode_claim_null(xdr, arg->name);
1079 break;
1080 case NFS4_OPEN_CLAIM_PREVIOUS:
1081 encode_claim_previous(xdr, arg->u.delegation_type);
1082 break;
1083 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1084 encode_claim_delegate_cur(xdr, arg->name, &arg->u.delegation);
1085 break;
1086 default:
1087 BUG();
1088 }
1089 return 0;
1090}
1091
1092static int encode_open_confirm(struct xdr_stream *xdr, const struct nfs_open_confirmargs *arg)
1093{
Al Viro8687b632006-10-19 23:28:48 -07001094 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001096 RESERVE_SPACE(4+NFS4_STATEID_SIZE+4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 WRITE32(OP_OPEN_CONFIRM);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001098 WRITEMEM(arg->stateid->data, NFS4_STATEID_SIZE);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001099 WRITE32(arg->seqid->sequence->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 return 0;
1102}
1103
1104static int encode_open_downgrade(struct xdr_stream *xdr, const struct nfs_closeargs *arg)
1105{
Al Viro8687b632006-10-19 23:28:48 -07001106 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001108 RESERVE_SPACE(4+NFS4_STATEID_SIZE+4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 WRITE32(OP_OPEN_DOWNGRADE);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001110 WRITEMEM(arg->stateid->data, NFS4_STATEID_SIZE);
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001111 WRITE32(arg->seqid->sequence->counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 encode_share_access(xdr, arg->open_flags);
1113 return 0;
1114}
1115
1116static int
1117encode_putfh(struct xdr_stream *xdr, const struct nfs_fh *fh)
1118{
1119 int len = fh->size;
Al Viro8687b632006-10-19 23:28:48 -07001120 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 RESERVE_SPACE(8 + len);
1123 WRITE32(OP_PUTFH);
1124 WRITE32(len);
1125 WRITEMEM(fh->data, len);
1126
1127 return 0;
1128}
1129
1130static int encode_putrootfh(struct xdr_stream *xdr)
1131{
Al Viro8687b632006-10-19 23:28:48 -07001132 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 RESERVE_SPACE(4);
1135 WRITE32(OP_PUTROOTFH);
1136
1137 return 0;
1138}
1139
1140static void encode_stateid(struct xdr_stream *xdr, const struct nfs_open_context *ctx)
1141{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 nfs4_stateid stateid;
Al Viro8687b632006-10-19 23:28:48 -07001143 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001145 RESERVE_SPACE(NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (ctx->state != NULL) {
1147 nfs4_copy_stateid(&stateid, ctx->state, ctx->lockowner);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001148 WRITEMEM(stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 } else
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001150 WRITEMEM(zero_stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151}
1152
1153static int encode_read(struct xdr_stream *xdr, const struct nfs_readargs *args)
1154{
Al Viro8687b632006-10-19 23:28:48 -07001155 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 RESERVE_SPACE(4);
1158 WRITE32(OP_READ);
1159
1160 encode_stateid(xdr, args->context);
1161
1162 RESERVE_SPACE(12);
1163 WRITE64(args->offset);
1164 WRITE32(args->count);
1165
1166 return 0;
1167}
1168
1169static int encode_readdir(struct xdr_stream *xdr, const struct nfs4_readdir_arg *readdir, struct rpc_rqst *req)
1170{
Manoj Naik97d312d2005-06-22 17:16:39 +00001171 uint32_t attrs[2] = {
1172 FATTR4_WORD0_RDATTR_ERROR|FATTR4_WORD0_FILEID,
1173 FATTR4_WORD1_MOUNTED_ON_FILEID,
1174 };
Al Viro8687b632006-10-19 23:28:48 -07001175 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001177 RESERVE_SPACE(12+NFS4_VERIFIER_SIZE+20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 WRITE32(OP_READDIR);
1179 WRITE64(readdir->cookie);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001180 WRITEMEM(readdir->verifier.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 WRITE32(readdir->count >> 1); /* We're not doing readdirplus */
1182 WRITE32(readdir->count);
1183 WRITE32(2);
Manoj Naik97d312d2005-06-22 17:16:39 +00001184 /* Switch to mounted_on_fileid if the server supports it */
1185 if (readdir->bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
1186 attrs[0] &= ~FATTR4_WORD0_FILEID;
1187 else
1188 attrs[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
1189 WRITE32(attrs[0] & readdir->bitmask[0]);
1190 WRITE32(attrs[1] & readdir->bitmask[1]);
Trond Myklebusteadf4592005-06-22 17:16:39 +00001191 dprintk("%s: cookie = %Lu, verifier = 0x%x%x, bitmap = 0x%x%x\n",
1192 __FUNCTION__,
1193 (unsigned long long)readdir->cookie,
1194 ((u32 *)readdir->verifier.data)[0],
1195 ((u32 *)readdir->verifier.data)[1],
1196 attrs[0] & readdir->bitmask[0],
1197 attrs[1] & readdir->bitmask[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 return 0;
1200}
1201
1202static int encode_readlink(struct xdr_stream *xdr, const struct nfs4_readlink *readlink, struct rpc_rqst *req)
1203{
Trond Myklebust1be27f32007-06-27 14:29:04 -04001204 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 unsigned int replen;
Al Viro8687b632006-10-19 23:28:48 -07001206 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 RESERVE_SPACE(4);
1209 WRITE32(OP_READLINK);
1210
1211 /* set up reply kvec
1212 * toplevel_status + taglen + rescount + OP_PUTFH + status
1213 * + OP_READLINK + status + string length = 8
1214 */
1215 replen = (RPC_REPHDRSIZE + auth->au_rslack + 8) << 2;
1216 xdr_inline_pages(&req->rq_rcv_buf, replen, readlink->pages,
1217 readlink->pgbase, readlink->pglen);
1218
1219 return 0;
1220}
1221
1222static int encode_remove(struct xdr_stream *xdr, const struct qstr *name)
1223{
Al Viro8687b632006-10-19 23:28:48 -07001224 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 RESERVE_SPACE(8 + name->len);
1227 WRITE32(OP_REMOVE);
1228 WRITE32(name->len);
1229 WRITEMEM(name->name, name->len);
1230
1231 return 0;
1232}
1233
1234static int encode_rename(struct xdr_stream *xdr, const struct qstr *oldname, const struct qstr *newname)
1235{
Al Viro8687b632006-10-19 23:28:48 -07001236 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 RESERVE_SPACE(8 + oldname->len);
1239 WRITE32(OP_RENAME);
1240 WRITE32(oldname->len);
1241 WRITEMEM(oldname->name, oldname->len);
1242
1243 RESERVE_SPACE(4 + newname->len);
1244 WRITE32(newname->len);
1245 WRITEMEM(newname->name, newname->len);
1246
1247 return 0;
1248}
1249
David Howellsadfa6f92006-08-22 20:06:08 -04001250static int encode_renew(struct xdr_stream *xdr, const struct nfs_client *client_stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
Al Viro8687b632006-10-19 23:28:48 -07001252 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 RESERVE_SPACE(12);
1255 WRITE32(OP_RENEW);
1256 WRITE64(client_stateid->cl_clientid);
1257
1258 return 0;
1259}
1260
1261static int
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001262encode_restorefh(struct xdr_stream *xdr)
1263{
Al Viro8687b632006-10-19 23:28:48 -07001264 __be32 *p;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001265
1266 RESERVE_SPACE(4);
1267 WRITE32(OP_RESTOREFH);
1268
1269 return 0;
1270}
1271
1272static int
J. Bruce Fields23ec6962005-06-22 17:16:22 +00001273encode_setacl(struct xdr_stream *xdr, struct nfs_setaclargs *arg)
1274{
Al Viro8687b632006-10-19 23:28:48 -07001275 __be32 *p;
J. Bruce Fields23ec6962005-06-22 17:16:22 +00001276
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001277 RESERVE_SPACE(4+NFS4_STATEID_SIZE);
J. Bruce Fields23ec6962005-06-22 17:16:22 +00001278 WRITE32(OP_SETATTR);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001279 WRITEMEM(zero_stateid.data, NFS4_STATEID_SIZE);
J. Bruce Fields23ec6962005-06-22 17:16:22 +00001280 RESERVE_SPACE(2*4);
1281 WRITE32(1);
1282 WRITE32(FATTR4_WORD0_ACL);
1283 if (arg->acl_len % 4)
1284 return -EINVAL;
1285 RESERVE_SPACE(4);
1286 WRITE32(arg->acl_len);
1287 xdr_write_pages(xdr, arg->acl_pages, arg->acl_pgbase, arg->acl_len);
1288 return 0;
1289}
1290
1291static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292encode_savefh(struct xdr_stream *xdr)
1293{
Al Viro8687b632006-10-19 23:28:48 -07001294 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 RESERVE_SPACE(4);
1297 WRITE32(OP_SAVEFH);
1298
1299 return 0;
1300}
1301
1302static int encode_setattr(struct xdr_stream *xdr, const struct nfs_setattrargs *arg, const struct nfs_server *server)
1303{
1304 int status;
Al Viro8687b632006-10-19 23:28:48 -07001305 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001307 RESERVE_SPACE(4+NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 WRITE32(OP_SETATTR);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001309 WRITEMEM(arg->stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 if ((status = encode_attrs(xdr, arg->iap, server)))
1312 return status;
1313
1314 return 0;
1315}
1316
1317static int encode_setclientid(struct xdr_stream *xdr, const struct nfs4_setclientid *setclientid)
1318{
Al Viro8687b632006-10-19 23:28:48 -07001319 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001321 RESERVE_SPACE(4 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 WRITE32(OP_SETCLIENTID);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001323 WRITEMEM(setclientid->sc_verifier->data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 encode_string(xdr, setclientid->sc_name_len, setclientid->sc_name);
1326 RESERVE_SPACE(4);
1327 WRITE32(setclientid->sc_prog);
1328 encode_string(xdr, setclientid->sc_netid_len, setclientid->sc_netid);
1329 encode_string(xdr, setclientid->sc_uaddr_len, setclientid->sc_uaddr);
1330 RESERVE_SPACE(4);
1331 WRITE32(setclientid->sc_cb_ident);
1332
1333 return 0;
1334}
1335
David Howellsadfa6f92006-08-22 20:06:08 -04001336static int encode_setclientid_confirm(struct xdr_stream *xdr, const struct nfs_client *client_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Al Viro8687b632006-10-19 23:28:48 -07001338 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001340 RESERVE_SPACE(12 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 WRITE32(OP_SETCLIENTID_CONFIRM);
1342 WRITE64(client_state->cl_clientid);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001343 WRITEMEM(client_state->cl_confirm.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 return 0;
1346}
1347
1348static int encode_write(struct xdr_stream *xdr, const struct nfs_writeargs *args)
1349{
Al Viro8687b632006-10-19 23:28:48 -07001350 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 RESERVE_SPACE(4);
1353 WRITE32(OP_WRITE);
1354
1355 encode_stateid(xdr, args->context);
1356
1357 RESERVE_SPACE(16);
1358 WRITE64(args->offset);
1359 WRITE32(args->stable);
1360 WRITE32(args->count);
1361
1362 xdr_write_pages(xdr, args->pages, args->pgbase, args->count);
1363
1364 return 0;
1365}
1366
1367static int encode_delegreturn(struct xdr_stream *xdr, const nfs4_stateid *stateid)
1368{
Al Viro8687b632006-10-19 23:28:48 -07001369 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001371 RESERVE_SPACE(4+NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 WRITE32(OP_DELEGRETURN);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001374 WRITEMEM(stateid->data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 return 0;
1376
1377}
1378/*
1379 * END OF "GENERIC" ENCODE ROUTINES.
1380 */
1381
1382/*
1383 * Encode an ACCESS request
1384 */
Al Viro8687b632006-10-19 23:28:48 -07001385static int nfs4_xdr_enc_access(struct rpc_rqst *req, __be32 *p, const struct nfs4_accessargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
1387 struct xdr_stream xdr;
1388 struct compound_hdr hdr = {
1389 .nops = 2,
1390 };
1391 int status;
1392
1393 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1394 encode_compound_hdr(&xdr, &hdr);
1395 if ((status = encode_putfh(&xdr, args->fh)) == 0)
1396 status = encode_access(&xdr, args->access);
1397 return status;
1398}
1399
1400/*
1401 * Encode LOOKUP request
1402 */
Al Viro8687b632006-10-19 23:28:48 -07001403static int nfs4_xdr_enc_lookup(struct rpc_rqst *req, __be32 *p, const struct nfs4_lookup_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
1405 struct xdr_stream xdr;
1406 struct compound_hdr hdr = {
1407 .nops = 4,
1408 };
1409 int status;
1410
1411 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1412 encode_compound_hdr(&xdr, &hdr);
1413 if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1414 goto out;
1415 if ((status = encode_lookup(&xdr, args->name)) != 0)
1416 goto out;
1417 if ((status = encode_getfh(&xdr)) != 0)
1418 goto out;
1419 status = encode_getfattr(&xdr, args->bitmask);
1420out:
1421 return status;
1422}
1423
1424/*
1425 * Encode LOOKUP_ROOT request
1426 */
Al Viro8687b632006-10-19 23:28:48 -07001427static 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 -07001428{
1429 struct xdr_stream xdr;
1430 struct compound_hdr hdr = {
1431 .nops = 3,
1432 };
1433 int status;
1434
1435 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1436 encode_compound_hdr(&xdr, &hdr);
1437 if ((status = encode_putrootfh(&xdr)) != 0)
1438 goto out;
1439 if ((status = encode_getfh(&xdr)) == 0)
1440 status = encode_getfattr(&xdr, args->bitmask);
1441out:
1442 return status;
1443}
1444
1445/*
1446 * Encode REMOVE request
1447 */
Al Viro8687b632006-10-19 23:28:48 -07001448static int nfs4_xdr_enc_remove(struct rpc_rqst *req, __be32 *p, const struct nfs4_remove_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
1450 struct xdr_stream xdr;
1451 struct compound_hdr hdr = {
Trond Myklebust16e42952005-10-27 22:12:44 -04001452 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 };
1454 int status;
1455
1456 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1457 encode_compound_hdr(&xdr, &hdr);
Trond Myklebust16e42952005-10-27 22:12:44 -04001458 if ((status = encode_putfh(&xdr, args->fh)) != 0)
1459 goto out;
1460 if ((status = encode_remove(&xdr, args->name)) != 0)
1461 goto out;
1462 status = encode_getfattr(&xdr, args->bitmask);
1463out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return status;
1465}
1466
1467/*
1468 * Encode RENAME request
1469 */
Al Viro8687b632006-10-19 23:28:48 -07001470static int nfs4_xdr_enc_rename(struct rpc_rqst *req, __be32 *p, const struct nfs4_rename_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
1472 struct xdr_stream xdr;
1473 struct compound_hdr hdr = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001474 .nops = 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 };
1476 int status;
1477
1478 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1479 encode_compound_hdr(&xdr, &hdr);
1480 if ((status = encode_putfh(&xdr, args->old_dir)) != 0)
1481 goto out;
1482 if ((status = encode_savefh(&xdr)) != 0)
1483 goto out;
1484 if ((status = encode_putfh(&xdr, args->new_dir)) != 0)
1485 goto out;
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001486 if ((status = encode_rename(&xdr, args->old_name, args->new_name)) != 0)
1487 goto out;
1488 if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
1489 goto out;
1490 if ((status = encode_restorefh(&xdr)) != 0)
1491 goto out;
1492 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493out:
1494 return status;
1495}
1496
1497/*
1498 * Encode LINK request
1499 */
Al Viro8687b632006-10-19 23:28:48 -07001500static int nfs4_xdr_enc_link(struct rpc_rqst *req, __be32 *p, const struct nfs4_link_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
1502 struct xdr_stream xdr;
1503 struct compound_hdr hdr = {
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001504 .nops = 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 };
1506 int status;
1507
1508 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1509 encode_compound_hdr(&xdr, &hdr);
1510 if ((status = encode_putfh(&xdr, args->fh)) != 0)
1511 goto out;
1512 if ((status = encode_savefh(&xdr)) != 0)
1513 goto out;
1514 if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1515 goto out;
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001516 if ((status = encode_link(&xdr, args->name)) != 0)
1517 goto out;
1518 if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
1519 goto out;
1520 if ((status = encode_restorefh(&xdr)) != 0)
1521 goto out;
1522 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523out:
1524 return status;
1525}
1526
1527/*
1528 * Encode CREATE request
1529 */
Al Viro8687b632006-10-19 23:28:48 -07001530static int nfs4_xdr_enc_create(struct rpc_rqst *req, __be32 *p, const struct nfs4_create_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
1532 struct xdr_stream xdr;
1533 struct compound_hdr hdr = {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001534 .nops = 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 };
1536 int status;
1537
1538 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1539 encode_compound_hdr(&xdr, &hdr);
1540 if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1541 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001542 if ((status = encode_savefh(&xdr)) != 0)
1543 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 if ((status = encode_create(&xdr, args)) != 0)
1545 goto out;
1546 if ((status = encode_getfh(&xdr)) != 0)
1547 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001548 if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
1549 goto out;
1550 if ((status = encode_restorefh(&xdr)) != 0)
1551 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 status = encode_getfattr(&xdr, args->bitmask);
1553out:
1554 return status;
1555}
1556
1557/*
1558 * Encode SYMLINK request
1559 */
Al Viro8687b632006-10-19 23:28:48 -07001560static int nfs4_xdr_enc_symlink(struct rpc_rqst *req, __be32 *p, const struct nfs4_create_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
1562 return nfs4_xdr_enc_create(req, p, args);
1563}
1564
1565/*
1566 * Encode GETATTR request
1567 */
Al Viro8687b632006-10-19 23:28:48 -07001568static int nfs4_xdr_enc_getattr(struct rpc_rqst *req, __be32 *p, const struct nfs4_getattr_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
1570 struct xdr_stream xdr;
1571 struct compound_hdr hdr = {
1572 .nops = 2,
1573 };
1574 int status;
1575
1576 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1577 encode_compound_hdr(&xdr, &hdr);
1578 if ((status = encode_putfh(&xdr, args->fh)) == 0)
1579 status = encode_getfattr(&xdr, args->bitmask);
1580 return status;
1581}
1582
1583/*
1584 * Encode a CLOSE request
1585 */
Al Viro8687b632006-10-19 23:28:48 -07001586static int nfs4_xdr_enc_close(struct rpc_rqst *req, __be32 *p, struct nfs_closeargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
1588 struct xdr_stream xdr;
1589 struct compound_hdr hdr = {
Trond Myklebust516a6af2005-10-27 22:12:41 -04001590 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 };
1592 int status;
1593
1594 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1595 encode_compound_hdr(&xdr, &hdr);
1596 status = encode_putfh(&xdr, args->fh);
1597 if(status)
1598 goto out;
1599 status = encode_close(&xdr, args);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001600 if (status != 0)
1601 goto out;
1602 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603out:
1604 return status;
1605}
1606
1607/*
1608 * Encode an OPEN request
1609 */
Al Viro8687b632006-10-19 23:28:48 -07001610static int nfs4_xdr_enc_open(struct rpc_rqst *req, __be32 *p, struct nfs_openargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
1612 struct xdr_stream xdr;
1613 struct compound_hdr hdr = {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001614 .nops = 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 };
1616 int status;
1617
1618 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1619 encode_compound_hdr(&xdr, &hdr);
1620 status = encode_putfh(&xdr, args->fh);
1621 if (status)
1622 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001623 status = encode_savefh(&xdr);
1624 if (status)
1625 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 status = encode_open(&xdr, args);
1627 if (status)
1628 goto out;
1629 status = encode_getfh(&xdr);
1630 if (status)
1631 goto out;
1632 status = encode_getfattr(&xdr, args->bitmask);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001633 if (status)
1634 goto out;
1635 status = encode_restorefh(&xdr);
1636 if (status)
1637 goto out;
1638 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639out:
1640 return status;
1641}
1642
1643/*
1644 * Encode an OPEN_CONFIRM request
1645 */
Al Viro8687b632006-10-19 23:28:48 -07001646static int nfs4_xdr_enc_open_confirm(struct rpc_rqst *req, __be32 *p, struct nfs_open_confirmargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
1648 struct xdr_stream xdr;
1649 struct compound_hdr hdr = {
1650 .nops = 2,
1651 };
1652 int status;
1653
1654 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1655 encode_compound_hdr(&xdr, &hdr);
1656 status = encode_putfh(&xdr, args->fh);
1657 if(status)
1658 goto out;
1659 status = encode_open_confirm(&xdr, args);
1660out:
1661 return status;
1662}
1663
1664/*
1665 * Encode an OPEN request with no attributes.
1666 */
Al Viro8687b632006-10-19 23:28:48 -07001667static int nfs4_xdr_enc_open_noattr(struct rpc_rqst *req, __be32 *p, struct nfs_openargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668{
1669 struct xdr_stream xdr;
1670 struct compound_hdr hdr = {
Trond Myklebust864472e2006-01-03 09:55:15 +01001671 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 };
1673 int status;
1674
1675 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1676 encode_compound_hdr(&xdr, &hdr);
1677 status = encode_putfh(&xdr, args->fh);
1678 if (status)
1679 goto out;
1680 status = encode_open(&xdr, args);
Trond Myklebust864472e2006-01-03 09:55:15 +01001681 if (status)
1682 goto out;
1683 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684out:
1685 return status;
1686}
1687
1688/*
1689 * Encode an OPEN_DOWNGRADE request
1690 */
Al Viro8687b632006-10-19 23:28:48 -07001691static int nfs4_xdr_enc_open_downgrade(struct rpc_rqst *req, __be32 *p, struct nfs_closeargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
1693 struct xdr_stream xdr;
1694 struct compound_hdr hdr = {
Trond Myklebust516a6af2005-10-27 22:12:41 -04001695 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 };
1697 int status;
1698
1699 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1700 encode_compound_hdr(&xdr, &hdr);
1701 status = encode_putfh(&xdr, args->fh);
1702 if (status)
1703 goto out;
1704 status = encode_open_downgrade(&xdr, args);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001705 if (status != 0)
1706 goto out;
1707 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708out:
1709 return status;
1710}
1711
1712/*
1713 * Encode a LOCK request
1714 */
Al Viro8687b632006-10-19 23:28:48 -07001715static int nfs4_xdr_enc_lock(struct rpc_rqst *req, __be32 *p, struct nfs_lock_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
1717 struct xdr_stream xdr;
1718 struct compound_hdr hdr = {
1719 .nops = 2,
1720 };
1721 int status;
1722
1723 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1724 encode_compound_hdr(&xdr, &hdr);
1725 status = encode_putfh(&xdr, args->fh);
1726 if(status)
1727 goto out;
1728 status = encode_lock(&xdr, args);
1729out:
1730 return status;
1731}
1732
1733/*
1734 * Encode a LOCKT request
1735 */
Al Viro8687b632006-10-19 23:28:48 -07001736static int nfs4_xdr_enc_lockt(struct rpc_rqst *req, __be32 *p, struct nfs_lockt_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 struct xdr_stream xdr;
1739 struct compound_hdr hdr = {
1740 .nops = 2,
1741 };
1742 int status;
1743
1744 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1745 encode_compound_hdr(&xdr, &hdr);
1746 status = encode_putfh(&xdr, args->fh);
1747 if(status)
1748 goto out;
1749 status = encode_lockt(&xdr, args);
1750out:
1751 return status;
1752}
1753
1754/*
1755 * Encode a LOCKU request
1756 */
Al Viro8687b632006-10-19 23:28:48 -07001757static int nfs4_xdr_enc_locku(struct rpc_rqst *req, __be32 *p, struct nfs_locku_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
1759 struct xdr_stream xdr;
1760 struct compound_hdr hdr = {
1761 .nops = 2,
1762 };
1763 int status;
1764
1765 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1766 encode_compound_hdr(&xdr, &hdr);
1767 status = encode_putfh(&xdr, args->fh);
1768 if(status)
1769 goto out;
1770 status = encode_locku(&xdr, args);
1771out:
1772 return status;
1773}
1774
1775/*
1776 * Encode a READLINK request
1777 */
Al Viro8687b632006-10-19 23:28:48 -07001778static int nfs4_xdr_enc_readlink(struct rpc_rqst *req, __be32 *p, const struct nfs4_readlink *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
1780 struct xdr_stream xdr;
1781 struct compound_hdr hdr = {
1782 .nops = 2,
1783 };
1784 int status;
1785
1786 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1787 encode_compound_hdr(&xdr, &hdr);
1788 status = encode_putfh(&xdr, args->fh);
1789 if(status)
1790 goto out;
1791 status = encode_readlink(&xdr, args, req);
1792out:
1793 return status;
1794}
1795
1796/*
1797 * Encode a READDIR request
1798 */
Al Viro8687b632006-10-19 23:28:48 -07001799static int nfs4_xdr_enc_readdir(struct rpc_rqst *req, __be32 *p, const struct nfs4_readdir_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
1801 struct xdr_stream xdr;
1802 struct compound_hdr hdr = {
1803 .nops = 2,
1804 };
Trond Myklebustd6ac02d2007-07-19 10:03:37 -04001805 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
1806 int replen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 int status;
1808
1809 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1810 encode_compound_hdr(&xdr, &hdr);
1811 status = encode_putfh(&xdr, args->fh);
1812 if(status)
1813 goto out;
1814 status = encode_readdir(&xdr, args, req);
Trond Myklebustd6ac02d2007-07-19 10:03:37 -04001815
1816 /* set up reply kvec
1817 * toplevel_status + taglen + rescount + OP_PUTFH + status
1818 * + OP_READDIR + status + verifer(2) = 9
1819 */
1820 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_readdir_sz) << 2;
1821 xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages,
1822 args->pgbase, args->count);
1823 dprintk("%s: inlined page args = (%u, %p, %u, %u)\n",
1824 __FUNCTION__, replen, args->pages,
1825 args->pgbase, args->count);
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827out:
1828 return status;
1829}
1830
1831/*
1832 * Encode a READ request
1833 */
Al Viro8687b632006-10-19 23:28:48 -07001834static int nfs4_xdr_enc_read(struct rpc_rqst *req, __be32 *p, struct nfs_readargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
Trond Myklebust1be27f32007-06-27 14:29:04 -04001836 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 struct xdr_stream xdr;
1838 struct compound_hdr hdr = {
1839 .nops = 2,
1840 };
1841 int replen, status;
1842
1843 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1844 encode_compound_hdr(&xdr, &hdr);
1845 status = encode_putfh(&xdr, args->fh);
1846 if (status)
1847 goto out;
1848 status = encode_read(&xdr, args);
1849 if (status)
1850 goto out;
1851
1852 /* set up reply kvec
1853 * toplevel status + taglen=0 + rescount + OP_PUTFH + status
1854 * + OP_READ + status + eof + datalen = 9
1855 */
1856 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_read_sz) << 2;
1857 xdr_inline_pages(&req->rq_rcv_buf, replen,
1858 args->pages, args->pgbase, args->count);
1859out:
1860 return status;
1861}
1862
1863/*
1864 * Encode an SETATTR request
1865 */
Al Viro8687b632006-10-19 23:28:48 -07001866static int nfs4_xdr_enc_setattr(struct rpc_rqst *req, __be32 *p, struct nfs_setattrargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868{
1869 struct xdr_stream xdr;
1870 struct compound_hdr hdr = {
1871 .nops = 3,
1872 };
1873 int status;
1874
1875 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1876 encode_compound_hdr(&xdr, &hdr);
1877 status = encode_putfh(&xdr, args->fh);
1878 if(status)
1879 goto out;
1880 status = encode_setattr(&xdr, args, args->server);
1881 if(status)
1882 goto out;
1883 status = encode_getfattr(&xdr, args->bitmask);
1884out:
1885 return status;
1886}
1887
1888/*
J. Bruce Fields029d1052005-06-22 17:16:22 +00001889 * Encode a GETACL request
1890 */
1891static int
Al Viro8687b632006-10-19 23:28:48 -07001892nfs4_xdr_enc_getacl(struct rpc_rqst *req, __be32 *p,
J. Bruce Fields029d1052005-06-22 17:16:22 +00001893 struct nfs_getaclargs *args)
1894{
1895 struct xdr_stream xdr;
Trond Myklebust1be27f32007-06-27 14:29:04 -04001896 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
J. Bruce Fields029d1052005-06-22 17:16:22 +00001897 struct compound_hdr hdr = {
1898 .nops = 2,
1899 };
1900 int replen, status;
1901
1902 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1903 encode_compound_hdr(&xdr, &hdr);
1904 status = encode_putfh(&xdr, args->fh);
1905 if (status)
1906 goto out;
1907 status = encode_getattr_two(&xdr, FATTR4_WORD0_ACL, 0);
1908 /* set up reply buffer: */
1909 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_getacl_sz) << 2;
1910 xdr_inline_pages(&req->rq_rcv_buf, replen,
1911 args->acl_pages, args->acl_pgbase, args->acl_len);
1912out:
1913 return status;
1914}
1915
1916/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 * Encode a WRITE request
1918 */
Al Viro8687b632006-10-19 23:28:48 -07001919static int nfs4_xdr_enc_write(struct rpc_rqst *req, __be32 *p, struct nfs_writeargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
1921 struct xdr_stream xdr;
1922 struct compound_hdr hdr = {
Trond Myklebust4f9838c2005-10-27 22:12:44 -04001923 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 };
1925 int status;
1926
1927 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1928 encode_compound_hdr(&xdr, &hdr);
1929 status = encode_putfh(&xdr, args->fh);
1930 if (status)
1931 goto out;
1932 status = encode_write(&xdr, args);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04001933 if (status)
1934 goto out;
1935 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936out:
1937 return status;
1938}
1939
1940/*
1941 * a COMMIT request
1942 */
Al Viro8687b632006-10-19 23:28:48 -07001943static int nfs4_xdr_enc_commit(struct rpc_rqst *req, __be32 *p, struct nfs_writeargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944{
1945 struct xdr_stream xdr;
1946 struct compound_hdr hdr = {
Trond Myklebust4f9838c2005-10-27 22:12:44 -04001947 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 };
1949 int status;
1950
1951 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1952 encode_compound_hdr(&xdr, &hdr);
1953 status = encode_putfh(&xdr, args->fh);
1954 if (status)
1955 goto out;
1956 status = encode_commit(&xdr, args);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04001957 if (status)
1958 goto out;
1959 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960out:
1961 return status;
1962}
1963
1964/*
1965 * FSINFO request
1966 */
Al Viro8687b632006-10-19 23:28:48 -07001967static int nfs4_xdr_enc_fsinfo(struct rpc_rqst *req, __be32 *p, struct nfs4_fsinfo_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
1969 struct xdr_stream xdr;
1970 struct compound_hdr hdr = {
1971 .nops = 2,
1972 };
1973 int status;
1974
1975 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1976 encode_compound_hdr(&xdr, &hdr);
1977 status = encode_putfh(&xdr, args->fh);
1978 if (!status)
1979 status = encode_fsinfo(&xdr, args->bitmask);
1980 return status;
1981}
1982
1983/*
1984 * a PATHCONF request
1985 */
Al Viro8687b632006-10-19 23:28:48 -07001986static int nfs4_xdr_enc_pathconf(struct rpc_rqst *req, __be32 *p, const struct nfs4_pathconf_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 struct xdr_stream xdr;
1989 struct compound_hdr hdr = {
1990 .nops = 2,
1991 };
1992 int status;
1993
1994 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1995 encode_compound_hdr(&xdr, &hdr);
1996 status = encode_putfh(&xdr, args->fh);
1997 if (!status)
1998 status = encode_getattr_one(&xdr,
1999 args->bitmask[0] & nfs4_pathconf_bitmap[0]);
2000 return status;
2001}
2002
2003/*
2004 * a STATFS request
2005 */
Al Viro8687b632006-10-19 23:28:48 -07002006static int nfs4_xdr_enc_statfs(struct rpc_rqst *req, __be32 *p, const struct nfs4_statfs_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 struct xdr_stream xdr;
2009 struct compound_hdr hdr = {
2010 .nops = 2,
2011 };
2012 int status;
2013
2014 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2015 encode_compound_hdr(&xdr, &hdr);
2016 status = encode_putfh(&xdr, args->fh);
2017 if (status == 0)
2018 status = encode_getattr_two(&xdr,
2019 args->bitmask[0] & nfs4_statfs_bitmap[0],
2020 args->bitmask[1] & nfs4_statfs_bitmap[1]);
2021 return status;
2022}
2023
2024/*
2025 * GETATTR_BITMAP request
2026 */
Al Viro8687b632006-10-19 23:28:48 -07002027static int nfs4_xdr_enc_server_caps(struct rpc_rqst *req, __be32 *p, const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
2029 struct xdr_stream xdr;
2030 struct compound_hdr hdr = {
2031 .nops = 2,
2032 };
2033 int status;
2034
2035 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2036 encode_compound_hdr(&xdr, &hdr);
2037 status = encode_putfh(&xdr, fhandle);
2038 if (status == 0)
2039 status = encode_getattr_one(&xdr, FATTR4_WORD0_SUPPORTED_ATTRS|
2040 FATTR4_WORD0_LINK_SUPPORT|
2041 FATTR4_WORD0_SYMLINK_SUPPORT|
2042 FATTR4_WORD0_ACLSUPPORT);
2043 return status;
2044}
2045
2046/*
2047 * a RENEW request
2048 */
Al Viro8687b632006-10-19 23:28:48 -07002049static int nfs4_xdr_enc_renew(struct rpc_rqst *req, __be32 *p, struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
2051 struct xdr_stream xdr;
2052 struct compound_hdr hdr = {
2053 .nops = 1,
2054 };
2055
2056 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2057 encode_compound_hdr(&xdr, &hdr);
2058 return encode_renew(&xdr, clp);
2059}
2060
2061/*
2062 * a SETCLIENTID request
2063 */
Al Viro8687b632006-10-19 23:28:48 -07002064static int nfs4_xdr_enc_setclientid(struct rpc_rqst *req, __be32 *p, struct nfs4_setclientid *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
2066 struct xdr_stream xdr;
2067 struct compound_hdr hdr = {
2068 .nops = 1,
2069 };
2070
2071 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2072 encode_compound_hdr(&xdr, &hdr);
2073 return encode_setclientid(&xdr, sc);
2074}
2075
2076/*
2077 * a SETCLIENTID_CONFIRM request
2078 */
Al Viro8687b632006-10-19 23:28:48 -07002079static int nfs4_xdr_enc_setclientid_confirm(struct rpc_rqst *req, __be32 *p, struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
2081 struct xdr_stream xdr;
2082 struct compound_hdr hdr = {
2083 .nops = 3,
2084 };
2085 const u32 lease_bitmap[2] = { FATTR4_WORD0_LEASE_TIME, 0 };
2086 int status;
2087
2088 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2089 encode_compound_hdr(&xdr, &hdr);
2090 status = encode_setclientid_confirm(&xdr, clp);
2091 if (!status)
2092 status = encode_putrootfh(&xdr);
2093 if (!status)
2094 status = encode_fsinfo(&xdr, lease_bitmap);
2095 return status;
2096}
2097
2098/*
2099 * DELEGRETURN request
2100 */
Al Viro8687b632006-10-19 23:28:48 -07002101static int nfs4_xdr_enc_delegreturn(struct rpc_rqst *req, __be32 *p, const struct nfs4_delegreturnargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
2103 struct xdr_stream xdr;
2104 struct compound_hdr hdr = {
Trond Myklebustfa178f22006-01-03 09:55:38 +01002105 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 };
2107 int status;
2108
2109 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2110 encode_compound_hdr(&xdr, &hdr);
Trond Myklebustfa178f22006-01-03 09:55:38 +01002111 status = encode_putfh(&xdr, args->fhandle);
2112 if (status != 0)
2113 goto out;
2114 status = encode_delegreturn(&xdr, args->stateid);
2115 if (status != 0)
2116 goto out;
2117 status = encode_getfattr(&xdr, args->bitmask);
2118out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 return status;
2120}
2121
2122/*
Trond Myklebust683b57b2006-06-09 09:34:22 -04002123 * Encode FS_LOCATIONS request
2124 */
Al Viro8687b632006-10-19 23:28:48 -07002125static 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 -04002126{
2127 struct xdr_stream xdr;
2128 struct compound_hdr hdr = {
2129 .nops = 3,
2130 };
Trond Myklebust1be27f32007-06-27 14:29:04 -04002131 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
Trond Myklebust683b57b2006-06-09 09:34:22 -04002132 int replen;
2133 int status;
2134
2135 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2136 encode_compound_hdr(&xdr, &hdr);
2137 if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
2138 goto out;
2139 if ((status = encode_lookup(&xdr, args->name)) != 0)
2140 goto out;
Manoj Naik830b8e32006-06-09 09:34:25 -04002141 if ((status = encode_fs_locations(&xdr, args->bitmask)) != 0)
Trond Myklebust683b57b2006-06-09 09:34:22 -04002142 goto out;
2143 /* set up reply
Manoj Naik830b8e32006-06-09 09:34:25 -04002144 * toplevel_status + OP_PUTFH + status
Trond Myklebust683b57b2006-06-09 09:34:22 -04002145 * + OP_LOOKUP + status + OP_GETATTR + status = 7
2146 */
2147 replen = (RPC_REPHDRSIZE + auth->au_rslack + 7) << 2;
2148 xdr_inline_pages(&req->rq_rcv_buf, replen, &args->page,
2149 0, PAGE_SIZE);
2150out:
2151 return status;
2152}
2153
2154/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 * START OF "GENERIC" DECODE ROUTINES.
2156 * These may look a little ugly since they are imported from a "generic"
2157 * set of XDR encode/decode routines which are intended to be shared by
2158 * all of our NFSv4 implementations (OpenBSD, MacOS X...).
2159 *
2160 * If the pain of reading these is too great, it should be a straightforward
2161 * task to translate them into Linux-specific versions which are more
2162 * consistent with the style used in NFSv2/v3...
2163 */
2164#define READ32(x) (x) = ntohl(*p++)
2165#define READ64(x) do { \
2166 (x) = (u64)ntohl(*p++) << 32; \
2167 (x) |= ntohl(*p++); \
2168} while (0)
2169#define READTIME(x) do { \
2170 p++; \
2171 (x.tv_sec) = ntohl(*p++); \
2172 (x.tv_nsec) = ntohl(*p++); \
2173} while (0)
2174#define COPYMEM(x,nbytes) do { \
2175 memcpy((x), p, nbytes); \
2176 p += XDR_QUADLEN(nbytes); \
2177} while (0)
2178
2179#define READ_BUF(nbytes) do { \
2180 p = xdr_inline_decode(xdr, nbytes); \
Chuck Levere4cc6ee2007-05-08 18:23:28 -04002181 if (unlikely(!p)) { \
2182 printk(KERN_INFO "%s: prematurely hit end of receive" \
2183 " buffer\n", __FUNCTION__); \
2184 printk(KERN_INFO "%s: xdr->p=%p, bytes=%u, xdr->end=%p\n", \
2185 __FUNCTION__, xdr->p, nbytes, xdr->end); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 return -EIO; \
2187 } \
2188} while (0)
2189
Trond Myklebust683b57b2006-06-09 09:34:22 -04002190static int decode_opaque_inline(struct xdr_stream *xdr, unsigned int *len, char **string)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
Al Viro8687b632006-10-19 23:28:48 -07002192 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
2194 READ_BUF(4);
2195 READ32(*len);
2196 READ_BUF(*len);
2197 *string = (char *)p;
2198 return 0;
2199}
2200
2201static int decode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
2202{
Al Viro8687b632006-10-19 23:28:48 -07002203 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
2205 READ_BUF(8);
2206 READ32(hdr->status);
2207 READ32(hdr->taglen);
2208
2209 READ_BUF(hdr->taglen + 4);
2210 hdr->tag = (char *)p;
2211 p += XDR_QUADLEN(hdr->taglen);
2212 READ32(hdr->nops);
2213 return 0;
2214}
2215
2216static int decode_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
2217{
Al Viro8687b632006-10-19 23:28:48 -07002218 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 uint32_t opnum;
2220 int32_t nfserr;
2221
2222 READ_BUF(8);
2223 READ32(opnum);
2224 if (opnum != expected) {
2225 printk(KERN_NOTICE
2226 "nfs4_decode_op_hdr: Server returned operation"
2227 " %d but we issued a request for %d\n",
2228 opnum, expected);
2229 return -EIO;
2230 }
2231 READ32(nfserr);
2232 if (nfserr != NFS_OK)
David Howells0a8ea432006-08-22 20:06:08 -04002233 return -nfs4_stat_to_errno(nfserr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 return 0;
2235}
2236
2237/* Dummy routine */
David Howellsadfa6f92006-08-22 20:06:08 -04002238static int decode_ace(struct xdr_stream *xdr, void *ace, struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
Al Viro8687b632006-10-19 23:28:48 -07002240 __be32 *p;
Trond Myklebust683b57b2006-06-09 09:34:22 -04002241 unsigned int strlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 char *str;
2243
2244 READ_BUF(12);
2245 return decode_opaque_inline(xdr, &strlen, &str);
2246}
2247
2248static int decode_attr_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
2249{
Al Viro8687b632006-10-19 23:28:48 -07002250 uint32_t bmlen;
2251 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
2253 READ_BUF(4);
2254 READ32(bmlen);
2255
2256 bitmap[0] = bitmap[1] = 0;
2257 READ_BUF((bmlen << 2));
2258 if (bmlen > 0) {
2259 READ32(bitmap[0]);
2260 if (bmlen > 1)
2261 READ32(bitmap[1]);
2262 }
2263 return 0;
2264}
2265
Al Viro8687b632006-10-19 23:28:48 -07002266static inline int decode_attr_length(struct xdr_stream *xdr, uint32_t *attrlen, __be32 **savep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
Al Viro8687b632006-10-19 23:28:48 -07002268 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
2270 READ_BUF(4);
2271 READ32(*attrlen);
2272 *savep = xdr->p;
2273 return 0;
2274}
2275
2276static int decode_attr_supported(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *bitmask)
2277{
2278 if (likely(bitmap[0] & FATTR4_WORD0_SUPPORTED_ATTRS)) {
2279 decode_attr_bitmap(xdr, bitmask);
2280 bitmap[0] &= ~FATTR4_WORD0_SUPPORTED_ATTRS;
2281 } else
2282 bitmask[0] = bitmask[1] = 0;
2283 dprintk("%s: bitmask=0x%x%x\n", __FUNCTION__, bitmask[0], bitmask[1]);
2284 return 0;
2285}
2286
2287static int decode_attr_type(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *type)
2288{
Al Viro8687b632006-10-19 23:28:48 -07002289 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
2291 *type = 0;
2292 if (unlikely(bitmap[0] & (FATTR4_WORD0_TYPE - 1U)))
2293 return -EIO;
2294 if (likely(bitmap[0] & FATTR4_WORD0_TYPE)) {
2295 READ_BUF(4);
2296 READ32(*type);
2297 if (*type < NF4REG || *type > NF4NAMEDATTR) {
2298 dprintk("%s: bad type %d\n", __FUNCTION__, *type);
2299 return -EIO;
2300 }
2301 bitmap[0] &= ~FATTR4_WORD0_TYPE;
2302 }
2303 dprintk("%s: type=0%o\n", __FUNCTION__, nfs_type2fmt[*type].nfs2type);
2304 return 0;
2305}
2306
2307static int decode_attr_change(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *change)
2308{
Al Viro8687b632006-10-19 23:28:48 -07002309 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
2311 *change = 0;
2312 if (unlikely(bitmap[0] & (FATTR4_WORD0_CHANGE - 1U)))
2313 return -EIO;
2314 if (likely(bitmap[0] & FATTR4_WORD0_CHANGE)) {
2315 READ_BUF(8);
2316 READ64(*change);
2317 bitmap[0] &= ~FATTR4_WORD0_CHANGE;
2318 }
2319 dprintk("%s: change attribute=%Lu\n", __FUNCTION__,
2320 (unsigned long long)*change);
2321 return 0;
2322}
2323
2324static int decode_attr_size(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *size)
2325{
Al Viro8687b632006-10-19 23:28:48 -07002326 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328 *size = 0;
2329 if (unlikely(bitmap[0] & (FATTR4_WORD0_SIZE - 1U)))
2330 return -EIO;
2331 if (likely(bitmap[0] & FATTR4_WORD0_SIZE)) {
2332 READ_BUF(8);
2333 READ64(*size);
2334 bitmap[0] &= ~FATTR4_WORD0_SIZE;
2335 }
2336 dprintk("%s: file size=%Lu\n", __FUNCTION__, (unsigned long long)*size);
2337 return 0;
2338}
2339
2340static int decode_attr_link_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2341{
Al Viro8687b632006-10-19 23:28:48 -07002342 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
2344 *res = 0;
2345 if (unlikely(bitmap[0] & (FATTR4_WORD0_LINK_SUPPORT - 1U)))
2346 return -EIO;
2347 if (likely(bitmap[0] & FATTR4_WORD0_LINK_SUPPORT)) {
2348 READ_BUF(4);
2349 READ32(*res);
2350 bitmap[0] &= ~FATTR4_WORD0_LINK_SUPPORT;
2351 }
2352 dprintk("%s: link support=%s\n", __FUNCTION__, *res == 0 ? "false" : "true");
2353 return 0;
2354}
2355
2356static int decode_attr_symlink_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2357{
Al Viro8687b632006-10-19 23:28:48 -07002358 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
2360 *res = 0;
2361 if (unlikely(bitmap[0] & (FATTR4_WORD0_SYMLINK_SUPPORT - 1U)))
2362 return -EIO;
2363 if (likely(bitmap[0] & FATTR4_WORD0_SYMLINK_SUPPORT)) {
2364 READ_BUF(4);
2365 READ32(*res);
2366 bitmap[0] &= ~FATTR4_WORD0_SYMLINK_SUPPORT;
2367 }
2368 dprintk("%s: symlink support=%s\n", __FUNCTION__, *res == 0 ? "false" : "true");
2369 return 0;
2370}
2371
Trond Myklebust8b4bdcf2006-06-09 09:34:19 -04002372static int decode_attr_fsid(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs_fsid *fsid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
Al Viro8687b632006-10-19 23:28:48 -07002374 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
2376 fsid->major = 0;
2377 fsid->minor = 0;
2378 if (unlikely(bitmap[0] & (FATTR4_WORD0_FSID - 1U)))
2379 return -EIO;
2380 if (likely(bitmap[0] & FATTR4_WORD0_FSID)) {
2381 READ_BUF(16);
2382 READ64(fsid->major);
2383 READ64(fsid->minor);
2384 bitmap[0] &= ~FATTR4_WORD0_FSID;
2385 }
2386 dprintk("%s: fsid=(0x%Lx/0x%Lx)\n", __FUNCTION__,
2387 (unsigned long long)fsid->major,
2388 (unsigned long long)fsid->minor);
2389 return 0;
2390}
2391
2392static int decode_attr_lease_time(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2393{
Al Viro8687b632006-10-19 23:28:48 -07002394 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
2396 *res = 60;
2397 if (unlikely(bitmap[0] & (FATTR4_WORD0_LEASE_TIME - 1U)))
2398 return -EIO;
2399 if (likely(bitmap[0] & FATTR4_WORD0_LEASE_TIME)) {
2400 READ_BUF(4);
2401 READ32(*res);
2402 bitmap[0] &= ~FATTR4_WORD0_LEASE_TIME;
2403 }
2404 dprintk("%s: file size=%u\n", __FUNCTION__, (unsigned int)*res);
2405 return 0;
2406}
2407
2408static int decode_attr_aclsupport(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2409{
Al Viro8687b632006-10-19 23:28:48 -07002410 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
2412 *res = ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL;
2413 if (unlikely(bitmap[0] & (FATTR4_WORD0_ACLSUPPORT - 1U)))
2414 return -EIO;
2415 if (likely(bitmap[0] & FATTR4_WORD0_ACLSUPPORT)) {
2416 READ_BUF(4);
2417 READ32(*res);
2418 bitmap[0] &= ~FATTR4_WORD0_ACLSUPPORT;
2419 }
2420 dprintk("%s: ACLs supported=%u\n", __FUNCTION__, (unsigned int)*res);
2421 return 0;
2422}
2423
2424static int decode_attr_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
2425{
Al Viro8687b632006-10-19 23:28:48 -07002426 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
2428 *fileid = 0;
2429 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILEID - 1U)))
2430 return -EIO;
2431 if (likely(bitmap[0] & FATTR4_WORD0_FILEID)) {
2432 READ_BUF(8);
2433 READ64(*fileid);
2434 bitmap[0] &= ~FATTR4_WORD0_FILEID;
2435 }
2436 dprintk("%s: fileid=%Lu\n", __FUNCTION__, (unsigned long long)*fileid);
2437 return 0;
2438}
2439
Manoj Naik99baf622006-06-09 09:34:24 -04002440static int decode_attr_mounted_on_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
2441{
Al Viro8687b632006-10-19 23:28:48 -07002442 __be32 *p;
Manoj Naik99baf622006-06-09 09:34:24 -04002443
2444 *fileid = 0;
2445 if (unlikely(bitmap[1] & (FATTR4_WORD1_MOUNTED_ON_FILEID - 1U)))
2446 return -EIO;
2447 if (likely(bitmap[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)) {
2448 READ_BUF(8);
2449 READ64(*fileid);
2450 bitmap[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
2451 }
2452 dprintk("%s: fileid=%Lu\n", __FUNCTION__, (unsigned long long)*fileid);
2453 return 0;
2454}
2455
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456static int decode_attr_files_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2457{
Al Viro8687b632006-10-19 23:28:48 -07002458 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 int status = 0;
2460
2461 *res = 0;
2462 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_AVAIL - 1U)))
2463 return -EIO;
2464 if (likely(bitmap[0] & FATTR4_WORD0_FILES_AVAIL)) {
2465 READ_BUF(8);
2466 READ64(*res);
2467 bitmap[0] &= ~FATTR4_WORD0_FILES_AVAIL;
2468 }
2469 dprintk("%s: files avail=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2470 return status;
2471}
2472
2473static int decode_attr_files_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2474{
Al Viro8687b632006-10-19 23:28:48 -07002475 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 int status = 0;
2477
2478 *res = 0;
2479 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_FREE - 1U)))
2480 return -EIO;
2481 if (likely(bitmap[0] & FATTR4_WORD0_FILES_FREE)) {
2482 READ_BUF(8);
2483 READ64(*res);
2484 bitmap[0] &= ~FATTR4_WORD0_FILES_FREE;
2485 }
2486 dprintk("%s: files free=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2487 return status;
2488}
2489
2490static int decode_attr_files_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2491{
Al Viro8687b632006-10-19 23:28:48 -07002492 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 int status = 0;
2494
2495 *res = 0;
2496 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_TOTAL - 1U)))
2497 return -EIO;
2498 if (likely(bitmap[0] & FATTR4_WORD0_FILES_TOTAL)) {
2499 READ_BUF(8);
2500 READ64(*res);
2501 bitmap[0] &= ~FATTR4_WORD0_FILES_TOTAL;
2502 }
2503 dprintk("%s: files total=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2504 return status;
2505}
2506
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002507static int decode_pathname(struct xdr_stream *xdr, struct nfs4_pathname *path)
2508{
2509 int n;
Al Viro8687b632006-10-19 23:28:48 -07002510 __be32 *p;
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002511 int status = 0;
2512
2513 READ_BUF(4);
2514 READ32(n);
Andy Adamson33a43f22006-06-09 09:34:30 -04002515 if (n < 0)
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002516 goto out_eio;
Andy Adamson33a43f22006-06-09 09:34:30 -04002517 if (n == 0)
2518 goto root_path;
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002519 dprintk("path ");
2520 path->ncomponents = 0;
2521 while (path->ncomponents < n) {
2522 struct nfs4_string *component = &path->components[path->ncomponents];
2523 status = decode_opaque_inline(xdr, &component->len, &component->data);
2524 if (unlikely(status != 0))
2525 goto out_eio;
2526 if (path->ncomponents != n)
2527 dprintk("/");
2528 dprintk("%s", component->data);
2529 if (path->ncomponents < NFS4_PATHNAME_MAXCOMPONENTS)
2530 path->ncomponents++;
2531 else {
2532 dprintk("cannot parse %d components in path\n", n);
2533 goto out_eio;
2534 }
2535 }
2536out:
2537 dprintk("\n");
2538 return status;
Andy Adamson33a43f22006-06-09 09:34:30 -04002539root_path:
2540/* a root pathname is sent as a zero component4 */
2541 path->ncomponents = 1;
2542 path->components[0].len=0;
2543 path->components[0].data=NULL;
2544 dprintk("path /\n");
2545 goto out;
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002546out_eio:
2547 dprintk(" status %d", status);
2548 status = -EIO;
2549 goto out;
2550}
2551
2552static int decode_attr_fs_locations(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_fs_locations *res)
Trond Myklebust683b57b2006-06-09 09:34:22 -04002553{
2554 int n;
Al Viro8687b632006-10-19 23:28:48 -07002555 __be32 *p;
Trond Myklebust683b57b2006-06-09 09:34:22 -04002556 int status = -EIO;
2557
2558 if (unlikely(bitmap[0] & (FATTR4_WORD0_FS_LOCATIONS -1U)))
2559 goto out;
2560 status = 0;
2561 if (unlikely(!(bitmap[0] & FATTR4_WORD0_FS_LOCATIONS)))
2562 goto out;
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002563 dprintk("%s: fsroot ", __FUNCTION__);
2564 status = decode_pathname(xdr, &res->fs_path);
Trond Myklebust683b57b2006-06-09 09:34:22 -04002565 if (unlikely(status != 0))
2566 goto out;
2567 READ_BUF(4);
2568 READ32(n);
2569 if (n <= 0)
2570 goto out_eio;
2571 res->nlocations = 0;
2572 while (res->nlocations < n) {
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002573 int m;
2574 struct nfs4_fs_location *loc = &res->locations[res->nlocations];
Trond Myklebust683b57b2006-06-09 09:34:22 -04002575
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002576 READ_BUF(4);
2577 READ32(m);
2578 if (m <= 0)
2579 goto out_eio;
2580
2581 loc->nservers = 0;
2582 dprintk("%s: servers ", __FUNCTION__);
2583 while (loc->nservers < m) {
2584 struct nfs4_string *server = &loc->servers[loc->nservers];
2585 status = decode_opaque_inline(xdr, &server->len, &server->data);
2586 if (unlikely(status != 0))
2587 goto out_eio;
2588 dprintk("%s ", server->data);
2589 if (loc->nservers < NFS4_FS_LOCATION_MAXSERVERS)
2590 loc->nservers++;
2591 else {
2592 int i;
2593 dprintk("%s: using first %d of %d servers returned for location %d\n", __FUNCTION__, NFS4_FS_LOCATION_MAXSERVERS, m, res->nlocations);
2594 for (i = loc->nservers; i < m; i++) {
Trond Myklebust2e42c3e2007-05-14 17:20:41 -04002595 unsigned int len;
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002596 char *data;
2597 status = decode_opaque_inline(xdr, &len, &data);
2598 if (unlikely(status != 0))
2599 goto out_eio;
2600 }
2601 }
2602 }
2603 status = decode_pathname(xdr, &loc->rootpath);
Trond Myklebust683b57b2006-06-09 09:34:22 -04002604 if (unlikely(status != 0))
2605 goto out_eio;
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002606 if (res->nlocations < NFS4_FS_LOCATIONS_MAXENTRIES)
Trond Myklebust683b57b2006-06-09 09:34:22 -04002607 res->nlocations++;
2608 }
2609out:
2610 dprintk("%s: fs_locations done, error = %d\n", __FUNCTION__, status);
2611 return status;
2612out_eio:
2613 status = -EIO;
2614 goto out;
2615}
2616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617static int decode_attr_maxfilesize(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2618{
Al Viro8687b632006-10-19 23:28:48 -07002619 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 int status = 0;
2621
2622 *res = 0;
2623 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXFILESIZE - 1U)))
2624 return -EIO;
2625 if (likely(bitmap[0] & FATTR4_WORD0_MAXFILESIZE)) {
2626 READ_BUF(8);
2627 READ64(*res);
2628 bitmap[0] &= ~FATTR4_WORD0_MAXFILESIZE;
2629 }
2630 dprintk("%s: maxfilesize=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2631 return status;
2632}
2633
2634static int decode_attr_maxlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxlink)
2635{
Al Viro8687b632006-10-19 23:28:48 -07002636 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 int status = 0;
2638
2639 *maxlink = 1;
2640 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXLINK - 1U)))
2641 return -EIO;
2642 if (likely(bitmap[0] & FATTR4_WORD0_MAXLINK)) {
2643 READ_BUF(4);
2644 READ32(*maxlink);
2645 bitmap[0] &= ~FATTR4_WORD0_MAXLINK;
2646 }
2647 dprintk("%s: maxlink=%u\n", __FUNCTION__, *maxlink);
2648 return status;
2649}
2650
2651static int decode_attr_maxname(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxname)
2652{
Al Viro8687b632006-10-19 23:28:48 -07002653 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 int status = 0;
2655
2656 *maxname = 1024;
2657 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXNAME - 1U)))
2658 return -EIO;
2659 if (likely(bitmap[0] & FATTR4_WORD0_MAXNAME)) {
2660 READ_BUF(4);
2661 READ32(*maxname);
2662 bitmap[0] &= ~FATTR4_WORD0_MAXNAME;
2663 }
2664 dprintk("%s: maxname=%u\n", __FUNCTION__, *maxname);
2665 return status;
2666}
2667
2668static int decode_attr_maxread(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2669{
Al Viro8687b632006-10-19 23:28:48 -07002670 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 int status = 0;
2672
2673 *res = 1024;
2674 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXREAD - 1U)))
2675 return -EIO;
2676 if (likely(bitmap[0] & FATTR4_WORD0_MAXREAD)) {
2677 uint64_t maxread;
2678 READ_BUF(8);
2679 READ64(maxread);
2680 if (maxread > 0x7FFFFFFF)
2681 maxread = 0x7FFFFFFF;
2682 *res = (uint32_t)maxread;
2683 bitmap[0] &= ~FATTR4_WORD0_MAXREAD;
2684 }
2685 dprintk("%s: maxread=%lu\n", __FUNCTION__, (unsigned long)*res);
2686 return status;
2687}
2688
2689static int decode_attr_maxwrite(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2690{
Al Viro8687b632006-10-19 23:28:48 -07002691 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 int status = 0;
2693
2694 *res = 1024;
2695 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXWRITE - 1U)))
2696 return -EIO;
2697 if (likely(bitmap[0] & FATTR4_WORD0_MAXWRITE)) {
2698 uint64_t maxwrite;
2699 READ_BUF(8);
2700 READ64(maxwrite);
2701 if (maxwrite > 0x7FFFFFFF)
2702 maxwrite = 0x7FFFFFFF;
2703 *res = (uint32_t)maxwrite;
2704 bitmap[0] &= ~FATTR4_WORD0_MAXWRITE;
2705 }
2706 dprintk("%s: maxwrite=%lu\n", __FUNCTION__, (unsigned long)*res);
2707 return status;
2708}
2709
2710static int decode_attr_mode(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *mode)
2711{
Al Viro8687b632006-10-19 23:28:48 -07002712 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
2714 *mode = 0;
2715 if (unlikely(bitmap[1] & (FATTR4_WORD1_MODE - 1U)))
2716 return -EIO;
2717 if (likely(bitmap[1] & FATTR4_WORD1_MODE)) {
2718 READ_BUF(4);
2719 READ32(*mode);
2720 *mode &= ~S_IFMT;
2721 bitmap[1] &= ~FATTR4_WORD1_MODE;
2722 }
2723 dprintk("%s: file mode=0%o\n", __FUNCTION__, (unsigned int)*mode);
2724 return 0;
2725}
2726
2727static int decode_attr_nlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *nlink)
2728{
Al Viro8687b632006-10-19 23:28:48 -07002729 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
2731 *nlink = 1;
2732 if (unlikely(bitmap[1] & (FATTR4_WORD1_NUMLINKS - 1U)))
2733 return -EIO;
2734 if (likely(bitmap[1] & FATTR4_WORD1_NUMLINKS)) {
2735 READ_BUF(4);
2736 READ32(*nlink);
2737 bitmap[1] &= ~FATTR4_WORD1_NUMLINKS;
2738 }
2739 dprintk("%s: nlink=%u\n", __FUNCTION__, (unsigned int)*nlink);
2740 return 0;
2741}
2742
Trond Myklebust2e42c3e2007-05-14 17:20:41 -04002743static 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 -07002744{
Al Viro8687b632006-10-19 23:28:48 -07002745 uint32_t len;
2746 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
2748 *uid = -2;
2749 if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER - 1U)))
2750 return -EIO;
2751 if (likely(bitmap[1] & FATTR4_WORD1_OWNER)) {
2752 READ_BUF(4);
2753 READ32(len);
2754 READ_BUF(len);
2755 if (len < XDR_MAX_NETOBJ) {
2756 if (nfs_map_name_to_uid(clp, (char *)p, len, uid) != 0)
2757 dprintk("%s: nfs_map_name_to_uid failed!\n",
2758 __FUNCTION__);
2759 } else
2760 printk(KERN_WARNING "%s: name too long (%u)!\n",
2761 __FUNCTION__, len);
2762 bitmap[1] &= ~FATTR4_WORD1_OWNER;
2763 }
2764 dprintk("%s: uid=%d\n", __FUNCTION__, (int)*uid);
2765 return 0;
2766}
2767
Trond Myklebust2e42c3e2007-05-14 17:20:41 -04002768static 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 -07002769{
Al Viro8687b632006-10-19 23:28:48 -07002770 uint32_t len;
2771 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
2773 *gid = -2;
2774 if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER_GROUP - 1U)))
2775 return -EIO;
2776 if (likely(bitmap[1] & FATTR4_WORD1_OWNER_GROUP)) {
2777 READ_BUF(4);
2778 READ32(len);
2779 READ_BUF(len);
2780 if (len < XDR_MAX_NETOBJ) {
2781 if (nfs_map_group_to_gid(clp, (char *)p, len, gid) != 0)
2782 dprintk("%s: nfs_map_group_to_gid failed!\n",
2783 __FUNCTION__);
2784 } else
2785 printk(KERN_WARNING "%s: name too long (%u)!\n",
2786 __FUNCTION__, len);
2787 bitmap[1] &= ~FATTR4_WORD1_OWNER_GROUP;
2788 }
2789 dprintk("%s: gid=%d\n", __FUNCTION__, (int)*gid);
2790 return 0;
2791}
2792
2793static int decode_attr_rdev(struct xdr_stream *xdr, uint32_t *bitmap, dev_t *rdev)
2794{
Al Viro8687b632006-10-19 23:28:48 -07002795 uint32_t major = 0, minor = 0;
2796 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
2798 *rdev = MKDEV(0,0);
2799 if (unlikely(bitmap[1] & (FATTR4_WORD1_RAWDEV - 1U)))
2800 return -EIO;
2801 if (likely(bitmap[1] & FATTR4_WORD1_RAWDEV)) {
2802 dev_t tmp;
2803
2804 READ_BUF(8);
2805 READ32(major);
2806 READ32(minor);
2807 tmp = MKDEV(major, minor);
2808 if (MAJOR(tmp) == major && MINOR(tmp) == minor)
2809 *rdev = tmp;
2810 bitmap[1] &= ~ FATTR4_WORD1_RAWDEV;
2811 }
2812 dprintk("%s: rdev=(0x%x:0x%x)\n", __FUNCTION__, major, minor);
2813 return 0;
2814}
2815
2816static int decode_attr_space_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2817{
Al Viro8687b632006-10-19 23:28:48 -07002818 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 int status = 0;
2820
2821 *res = 0;
2822 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_AVAIL - 1U)))
2823 return -EIO;
2824 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_AVAIL)) {
2825 READ_BUF(8);
2826 READ64(*res);
2827 bitmap[1] &= ~FATTR4_WORD1_SPACE_AVAIL;
2828 }
2829 dprintk("%s: space avail=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2830 return status;
2831}
2832
2833static int decode_attr_space_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2834{
Al Viro8687b632006-10-19 23:28:48 -07002835 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 int status = 0;
2837
2838 *res = 0;
2839 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_FREE - 1U)))
2840 return -EIO;
2841 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_FREE)) {
2842 READ_BUF(8);
2843 READ64(*res);
2844 bitmap[1] &= ~FATTR4_WORD1_SPACE_FREE;
2845 }
2846 dprintk("%s: space free=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2847 return status;
2848}
2849
2850static int decode_attr_space_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2851{
Al Viro8687b632006-10-19 23:28:48 -07002852 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 int status = 0;
2854
2855 *res = 0;
2856 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_TOTAL - 1U)))
2857 return -EIO;
2858 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_TOTAL)) {
2859 READ_BUF(8);
2860 READ64(*res);
2861 bitmap[1] &= ~FATTR4_WORD1_SPACE_TOTAL;
2862 }
2863 dprintk("%s: space total=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2864 return status;
2865}
2866
2867static int decode_attr_space_used(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *used)
2868{
Al Viro8687b632006-10-19 23:28:48 -07002869 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
2871 *used = 0;
2872 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_USED - 1U)))
2873 return -EIO;
2874 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_USED)) {
2875 READ_BUF(8);
2876 READ64(*used);
2877 bitmap[1] &= ~FATTR4_WORD1_SPACE_USED;
2878 }
2879 dprintk("%s: space used=%Lu\n", __FUNCTION__,
2880 (unsigned long long)*used);
2881 return 0;
2882}
2883
2884static int decode_attr_time(struct xdr_stream *xdr, struct timespec *time)
2885{
Al Viro8687b632006-10-19 23:28:48 -07002886 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 uint64_t sec;
2888 uint32_t nsec;
2889
2890 READ_BUF(12);
2891 READ64(sec);
2892 READ32(nsec);
2893 time->tv_sec = (time_t)sec;
2894 time->tv_nsec = (long)nsec;
2895 return 0;
2896}
2897
2898static int decode_attr_time_access(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2899{
2900 int status = 0;
2901
2902 time->tv_sec = 0;
2903 time->tv_nsec = 0;
2904 if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_ACCESS - 1U)))
2905 return -EIO;
2906 if (likely(bitmap[1] & FATTR4_WORD1_TIME_ACCESS)) {
2907 status = decode_attr_time(xdr, time);
2908 bitmap[1] &= ~FATTR4_WORD1_TIME_ACCESS;
2909 }
2910 dprintk("%s: atime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2911 return status;
2912}
2913
2914static int decode_attr_time_metadata(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2915{
2916 int status = 0;
2917
2918 time->tv_sec = 0;
2919 time->tv_nsec = 0;
2920 if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_METADATA - 1U)))
2921 return -EIO;
2922 if (likely(bitmap[1] & FATTR4_WORD1_TIME_METADATA)) {
2923 status = decode_attr_time(xdr, time);
2924 bitmap[1] &= ~FATTR4_WORD1_TIME_METADATA;
2925 }
2926 dprintk("%s: ctime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2927 return status;
2928}
2929
2930static int decode_attr_time_modify(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2931{
2932 int status = 0;
2933
2934 time->tv_sec = 0;
2935 time->tv_nsec = 0;
2936 if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_MODIFY - 1U)))
2937 return -EIO;
2938 if (likely(bitmap[1] & FATTR4_WORD1_TIME_MODIFY)) {
2939 status = decode_attr_time(xdr, time);
2940 bitmap[1] &= ~FATTR4_WORD1_TIME_MODIFY;
2941 }
2942 dprintk("%s: mtime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2943 return status;
2944}
2945
Al Viro8687b632006-10-19 23:28:48 -07002946static int verify_attr_len(struct xdr_stream *xdr, __be32 *savep, uint32_t attrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947{
2948 unsigned int attrwords = XDR_QUADLEN(attrlen);
2949 unsigned int nwords = xdr->p - savep;
2950
2951 if (unlikely(attrwords != nwords)) {
2952 printk(KERN_WARNING "%s: server returned incorrect attribute length: %u %c %u\n",
2953 __FUNCTION__,
2954 attrwords << 2,
2955 (attrwords < nwords) ? '<' : '>',
2956 nwords << 2);
2957 return -EIO;
2958 }
2959 return 0;
2960}
2961
2962static int decode_change_info(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
2963{
Al Viro8687b632006-10-19 23:28:48 -07002964 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
2966 READ_BUF(20);
2967 READ32(cinfo->atomic);
2968 READ64(cinfo->before);
2969 READ64(cinfo->after);
2970 return 0;
2971}
2972
2973static int decode_access(struct xdr_stream *xdr, struct nfs4_accessres *access)
2974{
Al Viro8687b632006-10-19 23:28:48 -07002975 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 uint32_t supp, acc;
2977 int status;
2978
2979 status = decode_op_hdr(xdr, OP_ACCESS);
2980 if (status)
2981 return status;
2982 READ_BUF(8);
2983 READ32(supp);
2984 READ32(acc);
2985 access->supported = supp;
2986 access->access = acc;
2987 return 0;
2988}
2989
2990static int decode_close(struct xdr_stream *xdr, struct nfs_closeres *res)
2991{
Al Viro8687b632006-10-19 23:28:48 -07002992 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 int status;
2994
2995 status = decode_op_hdr(xdr, OP_CLOSE);
2996 if (status)
2997 return status;
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04002998 READ_BUF(NFS4_STATEID_SIZE);
2999 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 return 0;
3001}
3002
3003static int decode_commit(struct xdr_stream *xdr, struct nfs_writeres *res)
3004{
Al Viro8687b632006-10-19 23:28:48 -07003005 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 int status;
3007
3008 status = decode_op_hdr(xdr, OP_COMMIT);
3009 if (status)
3010 return status;
3011 READ_BUF(8);
3012 COPYMEM(res->verf->verifier, 8);
3013 return 0;
3014}
3015
3016static int decode_create(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
3017{
Al Viro8687b632006-10-19 23:28:48 -07003018 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 uint32_t bmlen;
3020 int status;
3021
3022 status = decode_op_hdr(xdr, OP_CREATE);
3023 if (status)
3024 return status;
3025 if ((status = decode_change_info(xdr, cinfo)))
3026 return status;
3027 READ_BUF(4);
3028 READ32(bmlen);
3029 READ_BUF(bmlen << 2);
3030 return 0;
3031}
3032
3033static int decode_server_caps(struct xdr_stream *xdr, struct nfs4_server_caps_res *res)
3034{
Al Viro8687b632006-10-19 23:28:48 -07003035 __be32 *savep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 uint32_t attrlen,
3037 bitmap[2] = {0};
3038 int status;
3039
3040 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3041 goto xdr_error;
3042 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3043 goto xdr_error;
3044 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3045 goto xdr_error;
3046 if ((status = decode_attr_supported(xdr, bitmap, res->attr_bitmask)) != 0)
3047 goto xdr_error;
3048 if ((status = decode_attr_link_support(xdr, bitmap, &res->has_links)) != 0)
3049 goto xdr_error;
3050 if ((status = decode_attr_symlink_support(xdr, bitmap, &res->has_symlinks)) != 0)
3051 goto xdr_error;
3052 if ((status = decode_attr_aclsupport(xdr, bitmap, &res->acl_bitmask)) != 0)
3053 goto xdr_error;
3054 status = verify_attr_len(xdr, savep, attrlen);
3055xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04003056 dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 return status;
3058}
3059
3060static int decode_statfs(struct xdr_stream *xdr, struct nfs_fsstat *fsstat)
3061{
Al Viro8687b632006-10-19 23:28:48 -07003062 __be32 *savep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 uint32_t attrlen,
3064 bitmap[2] = {0};
3065 int status;
3066
3067 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3068 goto xdr_error;
3069 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3070 goto xdr_error;
3071 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3072 goto xdr_error;
3073
3074 if ((status = decode_attr_files_avail(xdr, bitmap, &fsstat->afiles)) != 0)
3075 goto xdr_error;
3076 if ((status = decode_attr_files_free(xdr, bitmap, &fsstat->ffiles)) != 0)
3077 goto xdr_error;
3078 if ((status = decode_attr_files_total(xdr, bitmap, &fsstat->tfiles)) != 0)
3079 goto xdr_error;
3080 if ((status = decode_attr_space_avail(xdr, bitmap, &fsstat->abytes)) != 0)
3081 goto xdr_error;
3082 if ((status = decode_attr_space_free(xdr, bitmap, &fsstat->fbytes)) != 0)
3083 goto xdr_error;
3084 if ((status = decode_attr_space_total(xdr, bitmap, &fsstat->tbytes)) != 0)
3085 goto xdr_error;
3086
3087 status = verify_attr_len(xdr, savep, attrlen);
3088xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04003089 dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 return status;
3091}
3092
3093static int decode_pathconf(struct xdr_stream *xdr, struct nfs_pathconf *pathconf)
3094{
Al Viro8687b632006-10-19 23:28:48 -07003095 __be32 *savep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 uint32_t attrlen,
3097 bitmap[2] = {0};
3098 int status;
3099
3100 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3101 goto xdr_error;
3102 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3103 goto xdr_error;
3104 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3105 goto xdr_error;
3106
3107 if ((status = decode_attr_maxlink(xdr, bitmap, &pathconf->max_link)) != 0)
3108 goto xdr_error;
3109 if ((status = decode_attr_maxname(xdr, bitmap, &pathconf->max_namelen)) != 0)
3110 goto xdr_error;
3111
3112 status = verify_attr_len(xdr, savep, attrlen);
3113xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04003114 dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 return status;
3116}
3117
3118static int decode_getfattr(struct xdr_stream *xdr, struct nfs_fattr *fattr, const struct nfs_server *server)
3119{
Al Viro8687b632006-10-19 23:28:48 -07003120 __be32 *savep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 uint32_t attrlen,
3122 bitmap[2] = {0},
3123 type;
3124 int status, fmode = 0;
Manoj Naik99baf622006-06-09 09:34:24 -04003125 uint64_t fileid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
3127 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3128 goto xdr_error;
3129 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3130 goto xdr_error;
3131
3132 fattr->bitmap[0] = bitmap[0];
3133 fattr->bitmap[1] = bitmap[1];
3134
3135 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3136 goto xdr_error;
3137
3138
3139 if ((status = decode_attr_type(xdr, bitmap, &type)) != 0)
3140 goto xdr_error;
3141 fattr->type = nfs_type2fmt[type].nfs2type;
3142 fmode = nfs_type2fmt[type].mode;
3143
3144 if ((status = decode_attr_change(xdr, bitmap, &fattr->change_attr)) != 0)
3145 goto xdr_error;
3146 if ((status = decode_attr_size(xdr, bitmap, &fattr->size)) != 0)
3147 goto xdr_error;
Trond Myklebust8b4bdcf2006-06-09 09:34:19 -04003148 if ((status = decode_attr_fsid(xdr, bitmap, &fattr->fsid)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 goto xdr_error;
3150 if ((status = decode_attr_fileid(xdr, bitmap, &fattr->fileid)) != 0)
3151 goto xdr_error;
Trond Myklebust683b57b2006-06-09 09:34:22 -04003152 if ((status = decode_attr_fs_locations(xdr, bitmap, container_of(fattr,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04003153 struct nfs4_fs_locations,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003154 fattr))) != 0)
3155 goto xdr_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 if ((status = decode_attr_mode(xdr, bitmap, &fattr->mode)) != 0)
3157 goto xdr_error;
3158 fattr->mode |= fmode;
3159 if ((status = decode_attr_nlink(xdr, bitmap, &fattr->nlink)) != 0)
3160 goto xdr_error;
David Howells7539bba2006-08-22 20:06:09 -04003161 if ((status = decode_attr_owner(xdr, bitmap, server->nfs_client, &fattr->uid)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 goto xdr_error;
David Howells7539bba2006-08-22 20:06:09 -04003163 if ((status = decode_attr_group(xdr, bitmap, server->nfs_client, &fattr->gid)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 goto xdr_error;
3165 if ((status = decode_attr_rdev(xdr, bitmap, &fattr->rdev)) != 0)
3166 goto xdr_error;
3167 if ((status = decode_attr_space_used(xdr, bitmap, &fattr->du.nfs3.used)) != 0)
3168 goto xdr_error;
3169 if ((status = decode_attr_time_access(xdr, bitmap, &fattr->atime)) != 0)
3170 goto xdr_error;
3171 if ((status = decode_attr_time_metadata(xdr, bitmap, &fattr->ctime)) != 0)
3172 goto xdr_error;
3173 if ((status = decode_attr_time_modify(xdr, bitmap, &fattr->mtime)) != 0)
3174 goto xdr_error;
Manoj Naik99baf622006-06-09 09:34:24 -04003175 if ((status = decode_attr_mounted_on_fileid(xdr, bitmap, &fileid)) != 0)
3176 goto xdr_error;
3177 if (fattr->fileid == 0 && fileid != 0)
3178 fattr->fileid = fileid;
Trond Myklebust33801142005-10-27 22:12:39 -04003179 if ((status = verify_attr_len(xdr, savep, attrlen)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 fattr->valid = NFS_ATTR_FATTR | NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04003182 dprintk("%s: xdr returned %d\n", __FUNCTION__, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 return status;
3184}
3185
3186
3187static int decode_fsinfo(struct xdr_stream *xdr, struct nfs_fsinfo *fsinfo)
3188{
Al Viro8687b632006-10-19 23:28:48 -07003189 __be32 *savep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 uint32_t attrlen, bitmap[2];
3191 int status;
3192
3193 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3194 goto xdr_error;
3195 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3196 goto xdr_error;
3197 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3198 goto xdr_error;
3199
3200 fsinfo->rtmult = fsinfo->wtmult = 512; /* ??? */
3201
3202 if ((status = decode_attr_lease_time(xdr, bitmap, &fsinfo->lease_time)) != 0)
3203 goto xdr_error;
3204 if ((status = decode_attr_maxfilesize(xdr, bitmap, &fsinfo->maxfilesize)) != 0)
3205 goto xdr_error;
3206 if ((status = decode_attr_maxread(xdr, bitmap, &fsinfo->rtmax)) != 0)
3207 goto xdr_error;
3208 fsinfo->rtpref = fsinfo->dtpref = fsinfo->rtmax;
3209 if ((status = decode_attr_maxwrite(xdr, bitmap, &fsinfo->wtmax)) != 0)
3210 goto xdr_error;
3211 fsinfo->wtpref = fsinfo->wtmax;
3212
3213 status = verify_attr_len(xdr, savep, attrlen);
3214xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04003215 dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 return status;
3217}
3218
3219static int decode_getfh(struct xdr_stream *xdr, struct nfs_fh *fh)
3220{
Al Viro8687b632006-10-19 23:28:48 -07003221 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 uint32_t len;
3223 int status;
3224
Trond Myklebust99367812007-07-17 21:52:41 -04003225 /* Zero handle first to allow comparisons */
3226 memset(fh, 0, sizeof(*fh));
3227
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 status = decode_op_hdr(xdr, OP_GETFH);
3229 if (status)
3230 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
3232 READ_BUF(4);
3233 READ32(len);
3234 if (len > NFS4_FHSIZE)
3235 return -EIO;
3236 fh->size = len;
3237 READ_BUF(len);
3238 COPYMEM(fh->data, len);
3239 return 0;
3240}
3241
3242static int decode_link(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
3243{
3244 int status;
3245
3246 status = decode_op_hdr(xdr, OP_LINK);
3247 if (status)
3248 return status;
3249 return decode_change_info(xdr, cinfo);
3250}
3251
3252/*
3253 * We create the owner, so we know a proper owner.id length is 4.
3254 */
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003255static int decode_lock_denied (struct xdr_stream *xdr, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256{
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003257 uint64_t offset, length, clientid;
Al Viro8687b632006-10-19 23:28:48 -07003258 __be32 *p;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003259 uint32_t namelen, type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
3261 READ_BUF(32);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003262 READ64(offset);
3263 READ64(length);
3264 READ32(type);
3265 if (fl != NULL) {
3266 fl->fl_start = (loff_t)offset;
3267 fl->fl_end = fl->fl_start + (loff_t)length - 1;
3268 if (length == ~(uint64_t)0)
3269 fl->fl_end = OFFSET_MAX;
3270 fl->fl_type = F_WRLCK;
3271 if (type & 1)
3272 fl->fl_type = F_RDLCK;
3273 fl->fl_pid = 0;
3274 }
3275 READ64(clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 READ32(namelen);
3277 READ_BUF(namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 return -NFS4ERR_DENIED;
3279}
3280
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003281static int decode_lock(struct xdr_stream *xdr, struct nfs_lock_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282{
Al Viro8687b632006-10-19 23:28:48 -07003283 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 int status;
3285
3286 status = decode_op_hdr(xdr, OP_LOCK);
3287 if (status == 0) {
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003288 READ_BUF(NFS4_STATEID_SIZE);
3289 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 } else if (status == -NFS4ERR_DENIED)
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003291 return decode_lock_denied(xdr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 return status;
3293}
3294
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003295static int decode_lockt(struct xdr_stream *xdr, struct nfs_lockt_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296{
3297 int status;
3298 status = decode_op_hdr(xdr, OP_LOCKT);
3299 if (status == -NFS4ERR_DENIED)
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003300 return decode_lock_denied(xdr, res->denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 return status;
3302}
3303
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003304static int decode_locku(struct xdr_stream *xdr, struct nfs_locku_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305{
Al Viro8687b632006-10-19 23:28:48 -07003306 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 int status;
3308
3309 status = decode_op_hdr(xdr, OP_LOCKU);
3310 if (status == 0) {
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003311 READ_BUF(NFS4_STATEID_SIZE);
3312 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 }
3314 return status;
3315}
3316
3317static int decode_lookup(struct xdr_stream *xdr)
3318{
3319 return decode_op_hdr(xdr, OP_LOOKUP);
3320}
3321
3322/* This is too sick! */
3323static int decode_space_limit(struct xdr_stream *xdr, u64 *maxsize)
3324{
Al Viro8687b632006-10-19 23:28:48 -07003325 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 uint32_t limit_type, nblocks, blocksize;
3327
3328 READ_BUF(12);
3329 READ32(limit_type);
3330 switch (limit_type) {
3331 case 1:
3332 READ64(*maxsize);
3333 break;
3334 case 2:
3335 READ32(nblocks);
3336 READ32(blocksize);
3337 *maxsize = (uint64_t)nblocks * (uint64_t)blocksize;
3338 }
3339 return 0;
3340}
3341
3342static int decode_delegation(struct xdr_stream *xdr, struct nfs_openres *res)
3343{
Al Viro8687b632006-10-19 23:28:48 -07003344 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 uint32_t delegation_type;
3346
3347 READ_BUF(4);
3348 READ32(delegation_type);
3349 if (delegation_type == NFS4_OPEN_DELEGATE_NONE) {
3350 res->delegation_type = 0;
3351 return 0;
3352 }
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003353 READ_BUF(NFS4_STATEID_SIZE+4);
3354 COPYMEM(res->delegation.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 READ32(res->do_recall);
3356 switch (delegation_type) {
3357 case NFS4_OPEN_DELEGATE_READ:
3358 res->delegation_type = FMODE_READ;
3359 break;
3360 case NFS4_OPEN_DELEGATE_WRITE:
3361 res->delegation_type = FMODE_WRITE|FMODE_READ;
3362 if (decode_space_limit(xdr, &res->maxsize) < 0)
3363 return -EIO;
3364 }
David Howells7539bba2006-08-22 20:06:09 -04003365 return decode_ace(xdr, NULL, res->server->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366}
3367
3368static int decode_open(struct xdr_stream *xdr, struct nfs_openres *res)
3369{
Al Viro8687b632006-10-19 23:28:48 -07003370 __be32 *p;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003371 uint32_t savewords, bmlen, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 int status;
3373
3374 status = decode_op_hdr(xdr, OP_OPEN);
3375 if (status)
3376 return status;
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003377 READ_BUF(NFS4_STATEID_SIZE);
3378 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379
3380 decode_change_info(xdr, &res->cinfo);
3381
3382 READ_BUF(8);
3383 READ32(res->rflags);
3384 READ32(bmlen);
3385 if (bmlen > 10)
3386 goto xdr_error;
3387
3388 READ_BUF(bmlen << 2);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003389 savewords = min_t(uint32_t, bmlen, NFS4_BITMAP_SIZE);
3390 for (i = 0; i < savewords; ++i)
3391 READ32(res->attrset[i]);
3392 for (; i < NFS4_BITMAP_SIZE; i++)
3393 res->attrset[i] = 0;
3394
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 return decode_delegation(xdr, res);
3396xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04003397 dprintk("%s: Bitmap too large! Length = %u\n", __FUNCTION__, bmlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 return -EIO;
3399}
3400
3401static int decode_open_confirm(struct xdr_stream *xdr, struct nfs_open_confirmres *res)
3402{
Al Viro8687b632006-10-19 23:28:48 -07003403 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 int status;
3405
3406 status = decode_op_hdr(xdr, OP_OPEN_CONFIRM);
3407 if (status)
3408 return status;
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003409 READ_BUF(NFS4_STATEID_SIZE);
3410 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 return 0;
3412}
3413
3414static int decode_open_downgrade(struct xdr_stream *xdr, struct nfs_closeres *res)
3415{
Al Viro8687b632006-10-19 23:28:48 -07003416 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 int status;
3418
3419 status = decode_op_hdr(xdr, OP_OPEN_DOWNGRADE);
3420 if (status)
3421 return status;
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003422 READ_BUF(NFS4_STATEID_SIZE);
3423 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 return 0;
3425}
3426
3427static int decode_putfh(struct xdr_stream *xdr)
3428{
3429 return decode_op_hdr(xdr, OP_PUTFH);
3430}
3431
3432static int decode_putrootfh(struct xdr_stream *xdr)
3433{
3434 return decode_op_hdr(xdr, OP_PUTROOTFH);
3435}
3436
3437static int decode_read(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs_readres *res)
3438{
3439 struct kvec *iov = req->rq_rcv_buf.head;
Al Viro8687b632006-10-19 23:28:48 -07003440 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 uint32_t count, eof, recvd, hdrlen;
3442 int status;
3443
3444 status = decode_op_hdr(xdr, OP_READ);
3445 if (status)
3446 return status;
3447 READ_BUF(8);
3448 READ32(eof);
3449 READ32(count);
3450 hdrlen = (u8 *) p - (u8 *) iov->iov_base;
3451 recvd = req->rq_rcv_buf.len - hdrlen;
3452 if (count > recvd) {
3453 printk(KERN_WARNING "NFS: server cheating in read reply: "
3454 "count %u > recvd %u\n", count, recvd);
3455 count = recvd;
3456 eof = 0;
3457 }
3458 xdr_read_pages(xdr, count);
3459 res->eof = eof;
3460 res->count = count;
3461 return 0;
3462}
3463
3464static int decode_readdir(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs4_readdir_res *readdir)
3465{
3466 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
3467 struct page *page = *rcvbuf->pages;
3468 struct kvec *iov = rcvbuf->head;
3469 unsigned int nr, pglen = rcvbuf->page_len;
Al Viro8687b632006-10-19 23:28:48 -07003470 __be32 *end, *entry, *p, *kaddr;
David Howellse8896492006-08-24 15:44:19 -04003471 uint32_t len, attrlen, xlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 int hdrlen, recvd, status;
3473
3474 status = decode_op_hdr(xdr, OP_READDIR);
3475 if (status)
3476 return status;
3477 READ_BUF(8);
3478 COPYMEM(readdir->verifier.data, 8);
Trond Myklebusteadf4592005-06-22 17:16:39 +00003479 dprintk("%s: verifier = 0x%x%x\n",
3480 __FUNCTION__,
3481 ((u32 *)readdir->verifier.data)[0],
3482 ((u32 *)readdir->verifier.data)[1]);
3483
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484
3485 hdrlen = (char *) p - (char *) iov->iov_base;
3486 recvd = rcvbuf->len - hdrlen;
3487 if (pglen > recvd)
3488 pglen = recvd;
3489 xdr_read_pages(xdr, pglen);
3490
3491 BUG_ON(pglen + readdir->pgbase > PAGE_CACHE_SIZE);
Al Viro8687b632006-10-19 23:28:48 -07003492 kaddr = p = kmap_atomic(page, KM_USER0);
David Howellse8896492006-08-24 15:44:19 -04003493 end = p + ((pglen + readdir->pgbase) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 entry = p;
3495 for (nr = 0; *p++; nr++) {
David Howellse8896492006-08-24 15:44:19 -04003496 if (end - p < 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 goto short_pkt;
Trond Myklebusteadf4592005-06-22 17:16:39 +00003498 dprintk("cookie = %Lu, ", *((unsigned long long *)p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 p += 2; /* cookie */
3500 len = ntohl(*p++); /* filename length */
3501 if (len > NFS4_MAXNAMLEN) {
3502 printk(KERN_WARNING "NFS: giant filename in readdir (len 0x%x)\n", len);
3503 goto err_unmap;
3504 }
David Howellse8896492006-08-24 15:44:19 -04003505 xlen = XDR_QUADLEN(len);
3506 if (end - p < xlen + 1)
3507 goto short_pkt;
Trond Myklebusteadf4592005-06-22 17:16:39 +00003508 dprintk("filename = %*s\n", len, (char *)p);
David Howellse8896492006-08-24 15:44:19 -04003509 p += xlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 len = ntohl(*p++); /* bitmap length */
David Howellse8896492006-08-24 15:44:19 -04003511 if (end - p < len + 1)
3512 goto short_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 p += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 attrlen = XDR_QUADLEN(ntohl(*p++));
David Howellse8896492006-08-24 15:44:19 -04003515 if (end - p < attrlen + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 goto short_pkt;
David Howellse8896492006-08-24 15:44:19 -04003517 p += attrlen; /* attributes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 entry = p;
3519 }
3520 if (!nr && (entry[0] != 0 || entry[1] == 0))
3521 goto short_pkt;
3522out:
3523 kunmap_atomic(kaddr, KM_USER0);
3524 return 0;
3525short_pkt:
Trond Myklebusteadf4592005-06-22 17:16:39 +00003526 dprintk("%s: short packet at entry %d\n", __FUNCTION__, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 entry[0] = entry[1] = 0;
3528 /* truncate listing ? */
3529 if (!nr) {
3530 printk(KERN_NOTICE "NFS: readdir reply truncated!\n");
3531 entry[1] = 1;
3532 }
3533 goto out;
3534err_unmap:
3535 kunmap_atomic(kaddr, KM_USER0);
3536 return -errno_NFSERR_IO;
3537}
3538
3539static int decode_readlink(struct xdr_stream *xdr, struct rpc_rqst *req)
3540{
3541 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
3542 struct kvec *iov = rcvbuf->head;
3543 int hdrlen, len, recvd;
Al Viro8687b632006-10-19 23:28:48 -07003544 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 char *kaddr;
3546 int status;
3547
3548 status = decode_op_hdr(xdr, OP_READLINK);
3549 if (status)
3550 return status;
3551
3552 /* Convert length of symlink */
3553 READ_BUF(4);
3554 READ32(len);
3555 if (len >= rcvbuf->page_len || len <= 0) {
3556 dprintk(KERN_WARNING "nfs: server returned giant symlink!\n");
3557 return -ENAMETOOLONG;
3558 }
3559 hdrlen = (char *) xdr->p - (char *) iov->iov_base;
3560 recvd = req->rq_rcv_buf.len - hdrlen;
3561 if (recvd < len) {
3562 printk(KERN_WARNING "NFS: server cheating in readlink reply: "
3563 "count %u > recvd %u\n", len, recvd);
3564 return -EIO;
3565 }
3566 xdr_read_pages(xdr, len);
3567 /*
3568 * The XDR encode routine has set things up so that
3569 * the link text will be copied directly into the
3570 * buffer. We just have to do overflow-checking,
3571 * and and null-terminate the text (the VFS expects
3572 * null-termination).
3573 */
3574 kaddr = (char *)kmap_atomic(rcvbuf->pages[0], KM_USER0);
3575 kaddr[len+rcvbuf->page_base] = '\0';
3576 kunmap_atomic(kaddr, KM_USER0);
3577 return 0;
3578}
3579
3580static int decode_remove(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
3581{
3582 int status;
3583
3584 status = decode_op_hdr(xdr, OP_REMOVE);
3585 if (status)
3586 goto out;
3587 status = decode_change_info(xdr, cinfo);
3588out:
3589 return status;
3590}
3591
3592static int decode_rename(struct xdr_stream *xdr, struct nfs4_change_info *old_cinfo,
3593 struct nfs4_change_info *new_cinfo)
3594{
3595 int status;
3596
3597 status = decode_op_hdr(xdr, OP_RENAME);
3598 if (status)
3599 goto out;
3600 if ((status = decode_change_info(xdr, old_cinfo)))
3601 goto out;
3602 status = decode_change_info(xdr, new_cinfo);
3603out:
3604 return status;
3605}
3606
3607static int decode_renew(struct xdr_stream *xdr)
3608{
3609 return decode_op_hdr(xdr, OP_RENEW);
3610}
3611
Trond Myklebust56ae19f2005-10-27 22:12:40 -04003612static int
3613decode_restorefh(struct xdr_stream *xdr)
3614{
3615 return decode_op_hdr(xdr, OP_RESTOREFH);
3616}
3617
J. Bruce Fields029d1052005-06-22 17:16:22 +00003618static int decode_getacl(struct xdr_stream *xdr, struct rpc_rqst *req,
3619 size_t *acl_len)
3620{
Al Viro8687b632006-10-19 23:28:48 -07003621 __be32 *savep;
J. Bruce Fields029d1052005-06-22 17:16:22 +00003622 uint32_t attrlen,
3623 bitmap[2] = {0};
3624 struct kvec *iov = req->rq_rcv_buf.head;
3625 int status;
3626
3627 *acl_len = 0;
3628 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3629 goto out;
3630 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3631 goto out;
3632 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3633 goto out;
3634
3635 if (unlikely(bitmap[0] & (FATTR4_WORD0_ACL - 1U)))
3636 return -EIO;
3637 if (likely(bitmap[0] & FATTR4_WORD0_ACL)) {
3638 int hdrlen, recvd;
3639
3640 /* We ignore &savep and don't do consistency checks on
3641 * the attr length. Let userspace figure it out.... */
3642 hdrlen = (u8 *)xdr->p - (u8 *)iov->iov_base;
3643 recvd = req->rq_rcv_buf.len - hdrlen;
3644 if (attrlen > recvd) {
3645 printk(KERN_WARNING "NFS: server cheating in getattr"
3646 " acl reply: attrlen %u > recvd %u\n",
3647 attrlen, recvd);
3648 return -EINVAL;
3649 }
J. Bruce Fieldsc04871e2006-05-30 16:28:58 -04003650 xdr_read_pages(xdr, attrlen);
J. Bruce Fields029d1052005-06-22 17:16:22 +00003651 *acl_len = attrlen;
J. Bruce Fields8c233cf2005-10-13 16:54:27 -04003652 } else
3653 status = -EOPNOTSUPP;
J. Bruce Fields029d1052005-06-22 17:16:22 +00003654
3655out:
3656 return status;
3657}
3658
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659static int
3660decode_savefh(struct xdr_stream *xdr)
3661{
3662 return decode_op_hdr(xdr, OP_SAVEFH);
3663}
3664
3665static int decode_setattr(struct xdr_stream *xdr, struct nfs_setattrres *res)
3666{
Al Viro8687b632006-10-19 23:28:48 -07003667 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 uint32_t bmlen;
3669 int status;
3670
3671
3672 status = decode_op_hdr(xdr, OP_SETATTR);
3673 if (status)
3674 return status;
3675 READ_BUF(4);
3676 READ32(bmlen);
3677 READ_BUF(bmlen << 2);
3678 return 0;
3679}
3680
David Howellsadfa6f92006-08-22 20:06:08 -04003681static int decode_setclientid(struct xdr_stream *xdr, struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682{
Al Viro8687b632006-10-19 23:28:48 -07003683 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 uint32_t opnum;
3685 int32_t nfserr;
3686
3687 READ_BUF(8);
3688 READ32(opnum);
3689 if (opnum != OP_SETCLIENTID) {
3690 printk(KERN_NOTICE
3691 "nfs4_decode_setclientid: Server returned operation"
3692 " %d\n", opnum);
3693 return -EIO;
3694 }
3695 READ32(nfserr);
3696 if (nfserr == NFS_OK) {
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003697 READ_BUF(8 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 READ64(clp->cl_clientid);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003699 COPYMEM(clp->cl_confirm.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 } else if (nfserr == NFSERR_CLID_INUSE) {
3701 uint32_t len;
3702
3703 /* skip netid string */
3704 READ_BUF(4);
3705 READ32(len);
3706 READ_BUF(len);
3707
3708 /* skip uaddr string */
3709 READ_BUF(4);
3710 READ32(len);
3711 READ_BUF(len);
3712 return -NFSERR_CLID_INUSE;
3713 } else
David Howells0a8ea432006-08-22 20:06:08 -04003714 return -nfs4_stat_to_errno(nfserr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715
3716 return 0;
3717}
3718
3719static int decode_setclientid_confirm(struct xdr_stream *xdr)
3720{
3721 return decode_op_hdr(xdr, OP_SETCLIENTID_CONFIRM);
3722}
3723
3724static int decode_write(struct xdr_stream *xdr, struct nfs_writeres *res)
3725{
Al Viro8687b632006-10-19 23:28:48 -07003726 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 int status;
3728
3729 status = decode_op_hdr(xdr, OP_WRITE);
3730 if (status)
3731 return status;
3732
3733 READ_BUF(16);
3734 READ32(res->count);
3735 READ32(res->verf->committed);
3736 COPYMEM(res->verf->verifier, 8);
3737 return 0;
3738}
3739
3740static int decode_delegreturn(struct xdr_stream *xdr)
3741{
3742 return decode_op_hdr(xdr, OP_DELEGRETURN);
3743}
3744
3745/*
3746 * Decode OPEN_DOWNGRADE response
3747 */
Al Viro8687b632006-10-19 23:28:48 -07003748static int nfs4_xdr_dec_open_downgrade(struct rpc_rqst *rqstp, __be32 *p, struct nfs_closeres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749{
3750 struct xdr_stream xdr;
3751 struct compound_hdr hdr;
3752 int status;
3753
3754 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3755 status = decode_compound_hdr(&xdr, &hdr);
3756 if (status)
3757 goto out;
3758 status = decode_putfh(&xdr);
3759 if (status)
3760 goto out;
3761 status = decode_open_downgrade(&xdr, res);
Trond Myklebust516a6af2005-10-27 22:12:41 -04003762 if (status != 0)
3763 goto out;
3764 decode_getfattr(&xdr, res->fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765out:
3766 return status;
3767}
3768
3769/*
3770 * END OF "GENERIC" DECODE ROUTINES.
3771 */
3772
3773/*
3774 * Decode ACCESS response
3775 */
Al Viro8687b632006-10-19 23:28:48 -07003776static int nfs4_xdr_dec_access(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_accessres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777{
3778 struct xdr_stream xdr;
3779 struct compound_hdr hdr;
3780 int status;
3781
3782 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3783 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3784 goto out;
3785 if ((status = decode_putfh(&xdr)) == 0)
3786 status = decode_access(&xdr, res);
3787out:
3788 return status;
3789}
3790
3791/*
3792 * Decode LOOKUP response
3793 */
Al Viro8687b632006-10-19 23:28:48 -07003794static int nfs4_xdr_dec_lookup(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_lookup_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795{
3796 struct xdr_stream xdr;
3797 struct compound_hdr hdr;
3798 int status;
3799
3800 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3801 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3802 goto out;
3803 if ((status = decode_putfh(&xdr)) != 0)
3804 goto out;
3805 if ((status = decode_lookup(&xdr)) != 0)
3806 goto out;
3807 if ((status = decode_getfh(&xdr, res->fh)) != 0)
3808 goto out;
3809 status = decode_getfattr(&xdr, res->fattr, res->server);
3810out:
3811 return status;
3812}
3813
3814/*
3815 * Decode LOOKUP_ROOT response
3816 */
Al Viro8687b632006-10-19 23:28:48 -07003817static int nfs4_xdr_dec_lookup_root(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_lookup_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818{
3819 struct xdr_stream xdr;
3820 struct compound_hdr hdr;
3821 int status;
3822
3823 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3824 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3825 goto out;
3826 if ((status = decode_putrootfh(&xdr)) != 0)
3827 goto out;
3828 if ((status = decode_getfh(&xdr, res->fh)) == 0)
3829 status = decode_getfattr(&xdr, res->fattr, res->server);
3830out:
3831 return status;
3832}
3833
3834/*
3835 * Decode REMOVE response
3836 */
Al Viro8687b632006-10-19 23:28:48 -07003837static int nfs4_xdr_dec_remove(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_remove_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838{
3839 struct xdr_stream xdr;
3840 struct compound_hdr hdr;
3841 int status;
3842
3843 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3844 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3845 goto out;
Trond Myklebust16e42952005-10-27 22:12:44 -04003846 if ((status = decode_putfh(&xdr)) != 0)
3847 goto out;
3848 if ((status = decode_remove(&xdr, &res->cinfo)) != 0)
3849 goto out;
3850 decode_getfattr(&xdr, res->dir_attr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851out:
3852 return status;
3853}
3854
3855/*
3856 * Decode RENAME response
3857 */
Al Viro8687b632006-10-19 23:28:48 -07003858static int nfs4_xdr_dec_rename(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_rename_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859{
3860 struct xdr_stream xdr;
3861 struct compound_hdr hdr;
3862 int status;
3863
3864 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3865 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3866 goto out;
3867 if ((status = decode_putfh(&xdr)) != 0)
3868 goto out;
3869 if ((status = decode_savefh(&xdr)) != 0)
3870 goto out;
3871 if ((status = decode_putfh(&xdr)) != 0)
3872 goto out;
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003873 if ((status = decode_rename(&xdr, &res->old_cinfo, &res->new_cinfo)) != 0)
3874 goto out;
3875 /* Current FH is target directory */
3876 if (decode_getfattr(&xdr, res->new_fattr, res->server) != 0)
3877 goto out;
3878 if ((status = decode_restorefh(&xdr)) != 0)
3879 goto out;
3880 decode_getfattr(&xdr, res->old_fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881out:
3882 return status;
3883}
3884
3885/*
3886 * Decode LINK response
3887 */
Al Viro8687b632006-10-19 23:28:48 -07003888static int nfs4_xdr_dec_link(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_link_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889{
3890 struct xdr_stream xdr;
3891 struct compound_hdr hdr;
3892 int status;
3893
3894 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3895 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3896 goto out;
3897 if ((status = decode_putfh(&xdr)) != 0)
3898 goto out;
3899 if ((status = decode_savefh(&xdr)) != 0)
3900 goto out;
3901 if ((status = decode_putfh(&xdr)) != 0)
3902 goto out;
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003903 if ((status = decode_link(&xdr, &res->cinfo)) != 0)
3904 goto out;
3905 /*
3906 * Note order: OP_LINK leaves the directory as the current
3907 * filehandle.
3908 */
3909 if (decode_getfattr(&xdr, res->dir_attr, res->server) != 0)
3910 goto out;
3911 if ((status = decode_restorefh(&xdr)) != 0)
3912 goto out;
3913 decode_getfattr(&xdr, res->fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914out:
3915 return status;
3916}
3917
3918/*
3919 * Decode CREATE response
3920 */
Al Viro8687b632006-10-19 23:28:48 -07003921static int nfs4_xdr_dec_create(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_create_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922{
3923 struct xdr_stream xdr;
3924 struct compound_hdr hdr;
3925 int status;
3926
3927 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3928 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3929 goto out;
3930 if ((status = decode_putfh(&xdr)) != 0)
3931 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04003932 if ((status = decode_savefh(&xdr)) != 0)
3933 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 if ((status = decode_create(&xdr,&res->dir_cinfo)) != 0)
3935 goto out;
3936 if ((status = decode_getfh(&xdr, res->fh)) != 0)
3937 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04003938 if (decode_getfattr(&xdr, res->fattr, res->server) != 0)
3939 goto out;
3940 if ((status = decode_restorefh(&xdr)) != 0)
3941 goto out;
3942 decode_getfattr(&xdr, res->dir_fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943out:
3944 return status;
3945}
3946
3947/*
3948 * Decode SYMLINK response
3949 */
Al Viro8687b632006-10-19 23:28:48 -07003950static int nfs4_xdr_dec_symlink(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_create_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951{
3952 return nfs4_xdr_dec_create(rqstp, p, res);
3953}
3954
3955/*
3956 * Decode GETATTR response
3957 */
Al Viro8687b632006-10-19 23:28:48 -07003958static int nfs4_xdr_dec_getattr(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_getattr_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959{
3960 struct xdr_stream xdr;
3961 struct compound_hdr hdr;
3962 int status;
3963
3964 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3965 status = decode_compound_hdr(&xdr, &hdr);
3966 if (status)
3967 goto out;
3968 status = decode_putfh(&xdr);
3969 if (status)
3970 goto out;
3971 status = decode_getfattr(&xdr, res->fattr, res->server);
3972out:
3973 return status;
3974
3975}
3976
J. Bruce Fields23ec6962005-06-22 17:16:22 +00003977/*
3978 * Encode an SETACL request
3979 */
3980static int
Al Viro8687b632006-10-19 23:28:48 -07003981nfs4_xdr_enc_setacl(struct rpc_rqst *req, __be32 *p, struct nfs_setaclargs *args)
J. Bruce Fields23ec6962005-06-22 17:16:22 +00003982{
3983 struct xdr_stream xdr;
3984 struct compound_hdr hdr = {
3985 .nops = 2,
3986 };
3987 int status;
3988
3989 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
3990 encode_compound_hdr(&xdr, &hdr);
3991 status = encode_putfh(&xdr, args->fh);
3992 if (status)
3993 goto out;
3994 status = encode_setacl(&xdr, args);
3995out:
3996 return status;
3997}
3998/*
3999 * Decode SETACL response
4000 */
4001static int
Al Viro8687b632006-10-19 23:28:48 -07004002nfs4_xdr_dec_setacl(struct rpc_rqst *rqstp, __be32 *p, void *res)
J. Bruce Fields23ec6962005-06-22 17:16:22 +00004003{
4004 struct xdr_stream xdr;
4005 struct compound_hdr hdr;
4006 int status;
4007
4008 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4009 status = decode_compound_hdr(&xdr, &hdr);
4010 if (status)
4011 goto out;
4012 status = decode_putfh(&xdr);
4013 if (status)
4014 goto out;
4015 status = decode_setattr(&xdr, res);
4016out:
4017 return status;
4018}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019
4020/*
J. Bruce Fields029d1052005-06-22 17:16:22 +00004021 * Decode GETACL response
4022 */
4023static int
Al Viro8687b632006-10-19 23:28:48 -07004024nfs4_xdr_dec_getacl(struct rpc_rqst *rqstp, __be32 *p, size_t *acl_len)
J. Bruce Fields029d1052005-06-22 17:16:22 +00004025{
4026 struct xdr_stream xdr;
4027 struct compound_hdr hdr;
4028 int status;
4029
4030 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4031 status = decode_compound_hdr(&xdr, &hdr);
4032 if (status)
4033 goto out;
4034 status = decode_putfh(&xdr);
4035 if (status)
4036 goto out;
4037 status = decode_getacl(&xdr, rqstp, acl_len);
4038
4039out:
4040 return status;
4041}
4042
4043/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 * Decode CLOSE response
4045 */
Al Viro8687b632006-10-19 23:28:48 -07004046static int nfs4_xdr_dec_close(struct rpc_rqst *rqstp, __be32 *p, struct nfs_closeres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047{
4048 struct xdr_stream xdr;
4049 struct compound_hdr hdr;
4050 int status;
4051
4052 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4053 status = decode_compound_hdr(&xdr, &hdr);
4054 if (status)
4055 goto out;
4056 status = decode_putfh(&xdr);
4057 if (status)
4058 goto out;
4059 status = decode_close(&xdr, res);
Trond Myklebust516a6af2005-10-27 22:12:41 -04004060 if (status != 0)
4061 goto out;
4062 /*
4063 * Note: Server may do delete on close for this file
4064 * in which case the getattr call will fail with
4065 * an ESTALE error. Shouldn't be a problem,
4066 * though, since fattr->valid will remain unset.
4067 */
4068 decode_getfattr(&xdr, res->fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069out:
4070 return status;
4071}
4072
4073/*
4074 * Decode OPEN response
4075 */
Al Viro8687b632006-10-19 23:28:48 -07004076static int nfs4_xdr_dec_open(struct rpc_rqst *rqstp, __be32 *p, struct nfs_openres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077{
4078 struct xdr_stream xdr;
4079 struct compound_hdr hdr;
4080 int status;
4081
4082 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4083 status = decode_compound_hdr(&xdr, &hdr);
4084 if (status)
4085 goto out;
4086 status = decode_putfh(&xdr);
4087 if (status)
4088 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04004089 status = decode_savefh(&xdr);
4090 if (status)
4091 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 status = decode_open(&xdr, res);
4093 if (status)
4094 goto out;
Trond Myklebust99367812007-07-17 21:52:41 -04004095 if (decode_getfh(&xdr, &res->fh) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04004097 if (decode_getfattr(&xdr, res->f_attr, res->server) != 0)
4098 goto out;
Trond Myklebust365c8f52007-07-17 21:52:37 -04004099 if (decode_restorefh(&xdr) != 0)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04004100 goto out;
4101 decode_getfattr(&xdr, res->dir_attr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102out:
4103 return status;
4104}
4105
4106/*
4107 * Decode OPEN_CONFIRM response
4108 */
Al Viro8687b632006-10-19 23:28:48 -07004109static int nfs4_xdr_dec_open_confirm(struct rpc_rqst *rqstp, __be32 *p, struct nfs_open_confirmres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110{
4111 struct xdr_stream xdr;
4112 struct compound_hdr hdr;
4113 int status;
4114
4115 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4116 status = decode_compound_hdr(&xdr, &hdr);
4117 if (status)
4118 goto out;
4119 status = decode_putfh(&xdr);
4120 if (status)
4121 goto out;
4122 status = decode_open_confirm(&xdr, res);
4123out:
4124 return status;
4125}
4126
4127/*
4128 * Decode OPEN response
4129 */
Al Viro8687b632006-10-19 23:28:48 -07004130static int nfs4_xdr_dec_open_noattr(struct rpc_rqst *rqstp, __be32 *p, struct nfs_openres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131{
4132 struct xdr_stream xdr;
4133 struct compound_hdr hdr;
4134 int status;
4135
4136 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4137 status = decode_compound_hdr(&xdr, &hdr);
4138 if (status)
4139 goto out;
4140 status = decode_putfh(&xdr);
4141 if (status)
4142 goto out;
4143 status = decode_open(&xdr, res);
Trond Myklebust864472e2006-01-03 09:55:15 +01004144 if (status)
4145 goto out;
4146 decode_getfattr(&xdr, res->f_attr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147out:
4148 return status;
4149}
4150
4151/*
4152 * Decode SETATTR response
4153 */
Al Viro8687b632006-10-19 23:28:48 -07004154static int nfs4_xdr_dec_setattr(struct rpc_rqst *rqstp, __be32 *p, struct nfs_setattrres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155{
4156 struct xdr_stream xdr;
4157 struct compound_hdr hdr;
4158 int status;
4159
4160 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4161 status = decode_compound_hdr(&xdr, &hdr);
4162 if (status)
4163 goto out;
4164 status = decode_putfh(&xdr);
4165 if (status)
4166 goto out;
4167 status = decode_setattr(&xdr, res);
4168 if (status)
4169 goto out;
4170 status = decode_getfattr(&xdr, res->fattr, res->server);
4171 if (status == NFS4ERR_DELAY)
4172 status = 0;
4173out:
4174 return status;
4175}
4176
4177/*
4178 * Decode LOCK response
4179 */
Al Viro8687b632006-10-19 23:28:48 -07004180static int nfs4_xdr_dec_lock(struct rpc_rqst *rqstp, __be32 *p, struct nfs_lock_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181{
4182 struct xdr_stream xdr;
4183 struct compound_hdr hdr;
4184 int status;
4185
4186 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4187 status = decode_compound_hdr(&xdr, &hdr);
4188 if (status)
4189 goto out;
4190 status = decode_putfh(&xdr);
4191 if (status)
4192 goto out;
4193 status = decode_lock(&xdr, res);
4194out:
4195 return status;
4196}
4197
4198/*
4199 * Decode LOCKT response
4200 */
Al Viro8687b632006-10-19 23:28:48 -07004201static int nfs4_xdr_dec_lockt(struct rpc_rqst *rqstp, __be32 *p, struct nfs_lockt_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202{
4203 struct xdr_stream xdr;
4204 struct compound_hdr hdr;
4205 int status;
4206
4207 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4208 status = decode_compound_hdr(&xdr, &hdr);
4209 if (status)
4210 goto out;
4211 status = decode_putfh(&xdr);
4212 if (status)
4213 goto out;
4214 status = decode_lockt(&xdr, res);
4215out:
4216 return status;
4217}
4218
4219/*
4220 * Decode LOCKU response
4221 */
Al Viro8687b632006-10-19 23:28:48 -07004222static int nfs4_xdr_dec_locku(struct rpc_rqst *rqstp, __be32 *p, struct nfs_locku_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223{
4224 struct xdr_stream xdr;
4225 struct compound_hdr hdr;
4226 int status;
4227
4228 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4229 status = decode_compound_hdr(&xdr, &hdr);
4230 if (status)
4231 goto out;
4232 status = decode_putfh(&xdr);
4233 if (status)
4234 goto out;
4235 status = decode_locku(&xdr, res);
4236out:
4237 return status;
4238}
4239
4240/*
4241 * Decode READLINK response
4242 */
Al Viro8687b632006-10-19 23:28:48 -07004243static int nfs4_xdr_dec_readlink(struct rpc_rqst *rqstp, __be32 *p, void *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244{
4245 struct xdr_stream xdr;
4246 struct compound_hdr hdr;
4247 int status;
4248
4249 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4250 status = decode_compound_hdr(&xdr, &hdr);
4251 if (status)
4252 goto out;
4253 status = decode_putfh(&xdr);
4254 if (status)
4255 goto out;
4256 status = decode_readlink(&xdr, rqstp);
4257out:
4258 return status;
4259}
4260
4261/*
4262 * Decode READDIR response
4263 */
Al Viro8687b632006-10-19 23:28:48 -07004264static int nfs4_xdr_dec_readdir(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_readdir_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265{
4266 struct xdr_stream xdr;
4267 struct compound_hdr hdr;
4268 int status;
4269
4270 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4271 status = decode_compound_hdr(&xdr, &hdr);
4272 if (status)
4273 goto out;
4274 status = decode_putfh(&xdr);
4275 if (status)
4276 goto out;
4277 status = decode_readdir(&xdr, rqstp, res);
4278out:
4279 return status;
4280}
4281
4282/*
4283 * Decode Read response
4284 */
Al Viro8687b632006-10-19 23:28:48 -07004285static int nfs4_xdr_dec_read(struct rpc_rqst *rqstp, __be32 *p, struct nfs_readres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286{
4287 struct xdr_stream xdr;
4288 struct compound_hdr hdr;
4289 int status;
4290
4291 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4292 status = decode_compound_hdr(&xdr, &hdr);
4293 if (status)
4294 goto out;
4295 status = decode_putfh(&xdr);
4296 if (status)
4297 goto out;
4298 status = decode_read(&xdr, rqstp, res);
4299 if (!status)
4300 status = res->count;
4301out:
4302 return status;
4303}
4304
4305/*
4306 * Decode WRITE response
4307 */
Al Viro8687b632006-10-19 23:28:48 -07004308static int nfs4_xdr_dec_write(struct rpc_rqst *rqstp, __be32 *p, struct nfs_writeres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309{
4310 struct xdr_stream xdr;
4311 struct compound_hdr hdr;
4312 int status;
4313
4314 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4315 status = decode_compound_hdr(&xdr, &hdr);
4316 if (status)
4317 goto out;
4318 status = decode_putfh(&xdr);
4319 if (status)
4320 goto out;
4321 status = decode_write(&xdr, res);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004322 if (status)
4323 goto out;
4324 decode_getfattr(&xdr, res->fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 if (!status)
4326 status = res->count;
4327out:
4328 return status;
4329}
4330
4331/*
4332 * Decode COMMIT response
4333 */
Al Viro8687b632006-10-19 23:28:48 -07004334static int nfs4_xdr_dec_commit(struct rpc_rqst *rqstp, __be32 *p, struct nfs_writeres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335{
4336 struct xdr_stream xdr;
4337 struct compound_hdr hdr;
4338 int status;
4339
4340 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4341 status = decode_compound_hdr(&xdr, &hdr);
4342 if (status)
4343 goto out;
4344 status = decode_putfh(&xdr);
4345 if (status)
4346 goto out;
4347 status = decode_commit(&xdr, res);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004348 if (status)
4349 goto out;
4350 decode_getfattr(&xdr, res->fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351out:
4352 return status;
4353}
4354
4355/*
4356 * FSINFO request
4357 */
Al Viro8687b632006-10-19 23:28:48 -07004358static int nfs4_xdr_dec_fsinfo(struct rpc_rqst *req, __be32 *p, struct nfs_fsinfo *fsinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359{
4360 struct xdr_stream xdr;
4361 struct compound_hdr hdr;
4362 int status;
4363
4364 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4365 status = decode_compound_hdr(&xdr, &hdr);
4366 if (!status)
4367 status = decode_putfh(&xdr);
4368 if (!status)
4369 status = decode_fsinfo(&xdr, fsinfo);
4370 if (!status)
David Howells0a8ea432006-08-22 20:06:08 -04004371 status = -nfs4_stat_to_errno(hdr.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 return status;
4373}
4374
4375/*
4376 * PATHCONF request
4377 */
Al Viro8687b632006-10-19 23:28:48 -07004378static int nfs4_xdr_dec_pathconf(struct rpc_rqst *req, __be32 *p, struct nfs_pathconf *pathconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379{
4380 struct xdr_stream xdr;
4381 struct compound_hdr hdr;
4382 int status;
4383
4384 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4385 status = decode_compound_hdr(&xdr, &hdr);
4386 if (!status)
4387 status = decode_putfh(&xdr);
4388 if (!status)
4389 status = decode_pathconf(&xdr, pathconf);
4390 return status;
4391}
4392
4393/*
4394 * STATFS request
4395 */
Al Viro8687b632006-10-19 23:28:48 -07004396static int nfs4_xdr_dec_statfs(struct rpc_rqst *req, __be32 *p, struct nfs_fsstat *fsstat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397{
4398 struct xdr_stream xdr;
4399 struct compound_hdr hdr;
4400 int status;
4401
4402 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4403 status = decode_compound_hdr(&xdr, &hdr);
4404 if (!status)
4405 status = decode_putfh(&xdr);
4406 if (!status)
4407 status = decode_statfs(&xdr, fsstat);
4408 return status;
4409}
4410
4411/*
4412 * GETATTR_BITMAP request
4413 */
Al Viro8687b632006-10-19 23:28:48 -07004414static 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 -07004415{
4416 struct xdr_stream xdr;
4417 struct compound_hdr hdr;
4418 int status;
4419
4420 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4421 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
4422 goto out;
4423 if ((status = decode_putfh(&xdr)) != 0)
4424 goto out;
4425 status = decode_server_caps(&xdr, res);
4426out:
4427 return status;
4428}
4429
4430/*
4431 * Decode RENEW response
4432 */
Al Viro8687b632006-10-19 23:28:48 -07004433static int nfs4_xdr_dec_renew(struct rpc_rqst *rqstp, __be32 *p, void *dummy)
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, &rqstp->rq_rcv_buf, p);
4440 status = decode_compound_hdr(&xdr, &hdr);
4441 if (!status)
4442 status = decode_renew(&xdr);
4443 return status;
4444}
4445
4446/*
4447 * a SETCLIENTID request
4448 */
Al Viro8687b632006-10-19 23:28:48 -07004449static int nfs4_xdr_dec_setclientid(struct rpc_rqst *req, __be32 *p,
David Howellsadfa6f92006-08-22 20:06:08 -04004450 struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451{
4452 struct xdr_stream xdr;
4453 struct compound_hdr hdr;
4454 int status;
4455
4456 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4457 status = decode_compound_hdr(&xdr, &hdr);
4458 if (!status)
4459 status = decode_setclientid(&xdr, clp);
4460 if (!status)
David Howells0a8ea432006-08-22 20:06:08 -04004461 status = -nfs4_stat_to_errno(hdr.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 return status;
4463}
4464
4465/*
4466 * a SETCLIENTID_CONFIRM request
4467 */
Al Viro8687b632006-10-19 23:28:48 -07004468static int nfs4_xdr_dec_setclientid_confirm(struct rpc_rqst *req, __be32 *p, struct nfs_fsinfo *fsinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469{
4470 struct xdr_stream xdr;
4471 struct compound_hdr hdr;
4472 int status;
4473
4474 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4475 status = decode_compound_hdr(&xdr, &hdr);
4476 if (!status)
4477 status = decode_setclientid_confirm(&xdr);
4478 if (!status)
4479 status = decode_putrootfh(&xdr);
4480 if (!status)
4481 status = decode_fsinfo(&xdr, fsinfo);
4482 if (!status)
David Howells0a8ea432006-08-22 20:06:08 -04004483 status = -nfs4_stat_to_errno(hdr.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 return status;
4485}
4486
4487/*
4488 * DELEGRETURN request
4489 */
Al Viro8687b632006-10-19 23:28:48 -07004490static int nfs4_xdr_dec_delegreturn(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_delegreturnres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491{
4492 struct xdr_stream xdr;
4493 struct compound_hdr hdr;
4494 int status;
4495
4496 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4497 status = decode_compound_hdr(&xdr, &hdr);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004498 if (status != 0)
4499 goto out;
4500 status = decode_putfh(&xdr);
4501 if (status != 0)
4502 goto out;
4503 status = decode_delegreturn(&xdr);
4504 decode_getfattr(&xdr, res->fattr, res->server);
4505out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 return status;
4507}
4508
Trond Myklebust683b57b2006-06-09 09:34:22 -04004509/*
4510 * FS_LOCATIONS request
4511 */
Al Viro8687b632006-10-19 23:28:48 -07004512static int nfs4_xdr_dec_fs_locations(struct rpc_rqst *req, __be32 *p, struct nfs4_fs_locations *res)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004513{
4514 struct xdr_stream xdr;
4515 struct compound_hdr hdr;
4516 int status;
4517
4518 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4519 status = decode_compound_hdr(&xdr, &hdr);
4520 if (status != 0)
4521 goto out;
4522 if ((status = decode_putfh(&xdr)) != 0)
4523 goto out;
4524 if ((status = decode_lookup(&xdr)) != 0)
4525 goto out;
4526 xdr_enter_page(&xdr, PAGE_SIZE);
4527 status = decode_getfattr(&xdr, &res->fattr, res->server);
4528out:
4529 return status;
4530}
4531
Al Viro0dbb4c62006-10-19 23:28:49 -07004532__be32 *nfs4_decode_dirent(__be32 *p, struct nfs_entry *entry, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533{
4534 uint32_t bitmap[2] = {0};
4535 uint32_t len;
4536
4537 if (!*p++) {
4538 if (!*p)
4539 return ERR_PTR(-EAGAIN);
4540 entry->eof = 1;
4541 return ERR_PTR(-EBADCOOKIE);
4542 }
4543
4544 entry->prev_cookie = entry->cookie;
4545 p = xdr_decode_hyper(p, &entry->cookie);
4546 entry->len = ntohl(*p++);
4547 entry->name = (const char *) p;
4548 p += XDR_QUADLEN(entry->len);
4549
4550 /*
4551 * In case the server doesn't return an inode number,
4552 * we fake one here. (We don't use inode number 0,
4553 * since glibc seems to choke on it...)
4554 */
4555 entry->ino = 1;
4556
4557 len = ntohl(*p++); /* bitmap length */
4558 if (len-- > 0) {
4559 bitmap[0] = ntohl(*p++);
4560 if (len-- > 0) {
4561 bitmap[1] = ntohl(*p++);
4562 p += len;
4563 }
4564 }
4565 len = XDR_QUADLEN(ntohl(*p++)); /* attribute buffer length */
4566 if (len > 0) {
Manoj Naik97d312d2005-06-22 17:16:39 +00004567 if (bitmap[0] & FATTR4_WORD0_RDATTR_ERROR) {
4568 bitmap[0] &= ~FATTR4_WORD0_RDATTR_ERROR;
4569 /* Ignore the return value of rdattr_error for now */
4570 p++;
4571 len--;
4572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 if (bitmap[0] == 0 && bitmap[1] == FATTR4_WORD1_MOUNTED_ON_FILEID)
4574 xdr_decode_hyper(p, &entry->ino);
4575 else if (bitmap[0] == FATTR4_WORD0_FILEID)
4576 xdr_decode_hyper(p, &entry->ino);
4577 p += len;
4578 }
4579
4580 entry->eof = !p[0] && p[1];
4581 return p;
4582}
4583
4584/*
4585 * We need to translate between nfs status return values and
4586 * the local errno values which may not be the same.
4587 */
4588static struct {
4589 int stat;
4590 int errno;
4591} nfs_errtbl[] = {
4592 { NFS4_OK, 0 },
4593 { NFS4ERR_PERM, EPERM },
4594 { NFS4ERR_NOENT, ENOENT },
4595 { NFS4ERR_IO, errno_NFSERR_IO },
4596 { NFS4ERR_NXIO, ENXIO },
4597 { NFS4ERR_ACCESS, EACCES },
4598 { NFS4ERR_EXIST, EEXIST },
4599 { NFS4ERR_XDEV, EXDEV },
4600 { NFS4ERR_NOTDIR, ENOTDIR },
4601 { NFS4ERR_ISDIR, EISDIR },
4602 { NFS4ERR_INVAL, EINVAL },
4603 { NFS4ERR_FBIG, EFBIG },
4604 { NFS4ERR_NOSPC, ENOSPC },
4605 { NFS4ERR_ROFS, EROFS },
4606 { NFS4ERR_MLINK, EMLINK },
4607 { NFS4ERR_NAMETOOLONG, ENAMETOOLONG },
4608 { NFS4ERR_NOTEMPTY, ENOTEMPTY },
4609 { NFS4ERR_DQUOT, EDQUOT },
4610 { NFS4ERR_STALE, ESTALE },
4611 { NFS4ERR_BADHANDLE, EBADHANDLE },
Manoj Naik6ebf3652005-06-22 17:16:39 +00004612 { NFS4ERR_BADOWNER, EINVAL },
4613 { NFS4ERR_BADNAME, EINVAL },
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 { NFS4ERR_BAD_COOKIE, EBADCOOKIE },
4615 { NFS4ERR_NOTSUPP, ENOTSUPP },
4616 { NFS4ERR_TOOSMALL, ETOOSMALL },
4617 { NFS4ERR_SERVERFAULT, ESERVERFAULT },
4618 { NFS4ERR_BADTYPE, EBADTYPE },
4619 { NFS4ERR_LOCKED, EAGAIN },
4620 { NFS4ERR_RESOURCE, EREMOTEIO },
4621 { NFS4ERR_SYMLINK, ELOOP },
4622 { NFS4ERR_OP_ILLEGAL, EOPNOTSUPP },
4623 { NFS4ERR_DEADLOCK, EDEADLK },
4624 { NFS4ERR_WRONGSEC, EPERM }, /* FIXME: this needs
4625 * to be handled by a
4626 * middle-layer.
4627 */
4628 { -1, EIO }
4629};
4630
4631/*
4632 * Convert an NFS error code to a local one.
4633 * This one is used jointly by NFSv2 and NFSv3.
4634 */
4635static int
David Howells0a8ea432006-08-22 20:06:08 -04004636nfs4_stat_to_errno(int stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637{
4638 int i;
4639 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
4640 if (nfs_errtbl[i].stat == stat)
4641 return nfs_errtbl[i].errno;
4642 }
4643 if (stat <= 10000 || stat > 10100) {
4644 /* The server is looney tunes. */
4645 return ESERVERFAULT;
4646 }
4647 /* If we cannot translate the error, the recovery routines should
4648 * handle it.
4649 * Note: remaining NFSv4 error codes have values > 10000, so should
4650 * not conflict with native Linux error codes.
4651 */
4652 return stat;
4653}
4654
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655#define PROC(proc, argtype, restype) \
4656[NFSPROC4_CLNT_##proc] = { \
4657 .p_proc = NFSPROC4_COMPOUND, \
4658 .p_encode = (kxdrproc_t) nfs4_xdr_##argtype, \
4659 .p_decode = (kxdrproc_t) nfs4_xdr_##restype, \
Chuck Lever2bea90d2007-03-29 16:47:53 -04004660 .p_arglen = NFS4_##argtype##_sz, \
4661 .p_replen = NFS4_##restype##_sz, \
Chuck Levercc0175c2006-03-20 13:44:22 -05004662 .p_statidx = NFSPROC4_CLNT_##proc, \
4663 .p_name = #proc, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 }
4665
4666struct rpc_procinfo nfs4_procedures[] = {
4667 PROC(READ, enc_read, dec_read),
4668 PROC(WRITE, enc_write, dec_write),
4669 PROC(COMMIT, enc_commit, dec_commit),
4670 PROC(OPEN, enc_open, dec_open),
4671 PROC(OPEN_CONFIRM, enc_open_confirm, dec_open_confirm),
4672 PROC(OPEN_NOATTR, enc_open_noattr, dec_open_noattr),
4673 PROC(OPEN_DOWNGRADE, enc_open_downgrade, dec_open_downgrade),
4674 PROC(CLOSE, enc_close, dec_close),
4675 PROC(SETATTR, enc_setattr, dec_setattr),
4676 PROC(FSINFO, enc_fsinfo, dec_fsinfo),
4677 PROC(RENEW, enc_renew, dec_renew),
4678 PROC(SETCLIENTID, enc_setclientid, dec_setclientid),
4679 PROC(SETCLIENTID_CONFIRM, enc_setclientid_confirm, dec_setclientid_confirm),
4680 PROC(LOCK, enc_lock, dec_lock),
4681 PROC(LOCKT, enc_lockt, dec_lockt),
4682 PROC(LOCKU, enc_locku, dec_locku),
4683 PROC(ACCESS, enc_access, dec_access),
4684 PROC(GETATTR, enc_getattr, dec_getattr),
4685 PROC(LOOKUP, enc_lookup, dec_lookup),
4686 PROC(LOOKUP_ROOT, enc_lookup_root, dec_lookup_root),
4687 PROC(REMOVE, enc_remove, dec_remove),
4688 PROC(RENAME, enc_rename, dec_rename),
4689 PROC(LINK, enc_link, dec_link),
4690 PROC(SYMLINK, enc_symlink, dec_symlink),
4691 PROC(CREATE, enc_create, dec_create),
4692 PROC(PATHCONF, enc_pathconf, dec_pathconf),
4693 PROC(STATFS, enc_statfs, dec_statfs),
4694 PROC(READLINK, enc_readlink, dec_readlink),
4695 PROC(READDIR, enc_readdir, dec_readdir),
4696 PROC(SERVER_CAPS, enc_server_caps, dec_server_caps),
4697 PROC(DELEGRETURN, enc_delegreturn, dec_delegreturn),
J. Bruce Fields029d1052005-06-22 17:16:22 +00004698 PROC(GETACL, enc_getacl, dec_getacl),
J. Bruce Fields23ec6962005-06-22 17:16:22 +00004699 PROC(SETACL, enc_setacl, dec_setacl),
Trond Myklebust683b57b2006-06-09 09:34:22 -04004700 PROC(FS_LOCATIONS, enc_fs_locations, dec_fs_locations),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701};
4702
4703struct rpc_version nfs_version4 = {
4704 .number = 4,
Tobias Klausere8c96f82006-03-24 03:15:34 -08004705 .nrprocs = ARRAY_SIZE(nfs4_procedures),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 .procs = nfs4_procedures
4707};
4708
4709/*
4710 * Local variables:
4711 * c-basic-offset: 8
4712 * End:
4713 */