blob: badd73b7ca124ea19a057f8016c6f54a42cf5e02 [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{
Al Viro8687b632006-10-19 23:28:48 -07001204 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 RESERVE_SPACE(4);
1207 WRITE32(OP_READLINK);
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return 0;
1210}
1211
1212static int encode_remove(struct xdr_stream *xdr, const struct qstr *name)
1213{
Al Viro8687b632006-10-19 23:28:48 -07001214 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 RESERVE_SPACE(8 + name->len);
1217 WRITE32(OP_REMOVE);
1218 WRITE32(name->len);
1219 WRITEMEM(name->name, name->len);
1220
1221 return 0;
1222}
1223
1224static int encode_rename(struct xdr_stream *xdr, const struct qstr *oldname, const struct qstr *newname)
1225{
Al Viro8687b632006-10-19 23:28:48 -07001226 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 RESERVE_SPACE(8 + oldname->len);
1229 WRITE32(OP_RENAME);
1230 WRITE32(oldname->len);
1231 WRITEMEM(oldname->name, oldname->len);
1232
1233 RESERVE_SPACE(4 + newname->len);
1234 WRITE32(newname->len);
1235 WRITEMEM(newname->name, newname->len);
1236
1237 return 0;
1238}
1239
David Howellsadfa6f92006-08-22 20:06:08 -04001240static int encode_renew(struct xdr_stream *xdr, const struct nfs_client *client_stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Al Viro8687b632006-10-19 23:28:48 -07001242 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 RESERVE_SPACE(12);
1245 WRITE32(OP_RENEW);
1246 WRITE64(client_stateid->cl_clientid);
1247
1248 return 0;
1249}
1250
1251static int
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001252encode_restorefh(struct xdr_stream *xdr)
1253{
Al Viro8687b632006-10-19 23:28:48 -07001254 __be32 *p;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001255
1256 RESERVE_SPACE(4);
1257 WRITE32(OP_RESTOREFH);
1258
1259 return 0;
1260}
1261
1262static int
J. Bruce Fields23ec6962005-06-22 17:16:22 +00001263encode_setacl(struct xdr_stream *xdr, struct nfs_setaclargs *arg)
1264{
Al Viro8687b632006-10-19 23:28:48 -07001265 __be32 *p;
J. Bruce Fields23ec6962005-06-22 17:16:22 +00001266
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001267 RESERVE_SPACE(4+NFS4_STATEID_SIZE);
J. Bruce Fields23ec6962005-06-22 17:16:22 +00001268 WRITE32(OP_SETATTR);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001269 WRITEMEM(zero_stateid.data, NFS4_STATEID_SIZE);
J. Bruce Fields23ec6962005-06-22 17:16:22 +00001270 RESERVE_SPACE(2*4);
1271 WRITE32(1);
1272 WRITE32(FATTR4_WORD0_ACL);
1273 if (arg->acl_len % 4)
1274 return -EINVAL;
1275 RESERVE_SPACE(4);
1276 WRITE32(arg->acl_len);
1277 xdr_write_pages(xdr, arg->acl_pages, arg->acl_pgbase, arg->acl_len);
1278 return 0;
1279}
1280
1281static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282encode_savefh(struct xdr_stream *xdr)
1283{
Al Viro8687b632006-10-19 23:28:48 -07001284 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 RESERVE_SPACE(4);
1287 WRITE32(OP_SAVEFH);
1288
1289 return 0;
1290}
1291
1292static int encode_setattr(struct xdr_stream *xdr, const struct nfs_setattrargs *arg, const struct nfs_server *server)
1293{
1294 int status;
Al Viro8687b632006-10-19 23:28:48 -07001295 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001297 RESERVE_SPACE(4+NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 WRITE32(OP_SETATTR);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001299 WRITEMEM(arg->stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 if ((status = encode_attrs(xdr, arg->iap, server)))
1302 return status;
1303
1304 return 0;
1305}
1306
1307static int encode_setclientid(struct xdr_stream *xdr, const struct nfs4_setclientid *setclientid)
1308{
Al Viro8687b632006-10-19 23:28:48 -07001309 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001311 RESERVE_SPACE(4 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 WRITE32(OP_SETCLIENTID);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001313 WRITEMEM(setclientid->sc_verifier->data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 encode_string(xdr, setclientid->sc_name_len, setclientid->sc_name);
1316 RESERVE_SPACE(4);
1317 WRITE32(setclientid->sc_prog);
1318 encode_string(xdr, setclientid->sc_netid_len, setclientid->sc_netid);
1319 encode_string(xdr, setclientid->sc_uaddr_len, setclientid->sc_uaddr);
1320 RESERVE_SPACE(4);
1321 WRITE32(setclientid->sc_cb_ident);
1322
1323 return 0;
1324}
1325
David Howellsadfa6f92006-08-22 20:06:08 -04001326static int encode_setclientid_confirm(struct xdr_stream *xdr, const struct nfs_client *client_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Al Viro8687b632006-10-19 23:28:48 -07001328 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001330 RESERVE_SPACE(12 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 WRITE32(OP_SETCLIENTID_CONFIRM);
1332 WRITE64(client_state->cl_clientid);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001333 WRITEMEM(client_state->cl_confirm.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 return 0;
1336}
1337
1338static int encode_write(struct xdr_stream *xdr, const struct nfs_writeargs *args)
1339{
Al Viro8687b632006-10-19 23:28:48 -07001340 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 RESERVE_SPACE(4);
1343 WRITE32(OP_WRITE);
1344
1345 encode_stateid(xdr, args->context);
1346
1347 RESERVE_SPACE(16);
1348 WRITE64(args->offset);
1349 WRITE32(args->stable);
1350 WRITE32(args->count);
1351
1352 xdr_write_pages(xdr, args->pages, args->pgbase, args->count);
1353
1354 return 0;
1355}
1356
1357static int encode_delegreturn(struct xdr_stream *xdr, const nfs4_stateid *stateid)
1358{
Al Viro8687b632006-10-19 23:28:48 -07001359 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001361 RESERVE_SPACE(4+NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 WRITE32(OP_DELEGRETURN);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04001364 WRITEMEM(stateid->data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return 0;
1366
1367}
1368/*
1369 * END OF "GENERIC" ENCODE ROUTINES.
1370 */
1371
1372/*
1373 * Encode an ACCESS request
1374 */
Al Viro8687b632006-10-19 23:28:48 -07001375static int nfs4_xdr_enc_access(struct rpc_rqst *req, __be32 *p, const struct nfs4_accessargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 struct xdr_stream xdr;
1378 struct compound_hdr hdr = {
1379 .nops = 2,
1380 };
1381 int status;
1382
1383 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1384 encode_compound_hdr(&xdr, &hdr);
1385 if ((status = encode_putfh(&xdr, args->fh)) == 0)
1386 status = encode_access(&xdr, args->access);
1387 return status;
1388}
1389
1390/*
1391 * Encode LOOKUP request
1392 */
Al Viro8687b632006-10-19 23:28:48 -07001393static int nfs4_xdr_enc_lookup(struct rpc_rqst *req, __be32 *p, const struct nfs4_lookup_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
1395 struct xdr_stream xdr;
1396 struct compound_hdr hdr = {
1397 .nops = 4,
1398 };
1399 int status;
1400
1401 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1402 encode_compound_hdr(&xdr, &hdr);
1403 if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1404 goto out;
1405 if ((status = encode_lookup(&xdr, args->name)) != 0)
1406 goto out;
1407 if ((status = encode_getfh(&xdr)) != 0)
1408 goto out;
1409 status = encode_getfattr(&xdr, args->bitmask);
1410out:
1411 return status;
1412}
1413
1414/*
1415 * Encode LOOKUP_ROOT request
1416 */
Al Viro8687b632006-10-19 23:28:48 -07001417static 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 -07001418{
1419 struct xdr_stream xdr;
1420 struct compound_hdr hdr = {
1421 .nops = 3,
1422 };
1423 int status;
1424
1425 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1426 encode_compound_hdr(&xdr, &hdr);
1427 if ((status = encode_putrootfh(&xdr)) != 0)
1428 goto out;
1429 if ((status = encode_getfh(&xdr)) == 0)
1430 status = encode_getfattr(&xdr, args->bitmask);
1431out:
1432 return status;
1433}
1434
1435/*
1436 * Encode REMOVE request
1437 */
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04001438static int nfs4_xdr_enc_remove(struct rpc_rqst *req, __be32 *p, const struct nfs_removeargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
1440 struct xdr_stream xdr;
1441 struct compound_hdr hdr = {
Trond Myklebust16e42952005-10-27 22:12:44 -04001442 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 };
1444 int status;
1445
1446 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1447 encode_compound_hdr(&xdr, &hdr);
Trond Myklebust16e42952005-10-27 22:12:44 -04001448 if ((status = encode_putfh(&xdr, args->fh)) != 0)
1449 goto out;
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04001450 if ((status = encode_remove(&xdr, &args->name)) != 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04001451 goto out;
1452 status = encode_getfattr(&xdr, args->bitmask);
1453out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return status;
1455}
1456
1457/*
1458 * Encode RENAME request
1459 */
Al Viro8687b632006-10-19 23:28:48 -07001460static int nfs4_xdr_enc_rename(struct rpc_rqst *req, __be32 *p, const struct nfs4_rename_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
1462 struct xdr_stream xdr;
1463 struct compound_hdr hdr = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001464 .nops = 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 };
1466 int status;
1467
1468 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1469 encode_compound_hdr(&xdr, &hdr);
1470 if ((status = encode_putfh(&xdr, args->old_dir)) != 0)
1471 goto out;
1472 if ((status = encode_savefh(&xdr)) != 0)
1473 goto out;
1474 if ((status = encode_putfh(&xdr, args->new_dir)) != 0)
1475 goto out;
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001476 if ((status = encode_rename(&xdr, args->old_name, args->new_name)) != 0)
1477 goto out;
1478 if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
1479 goto out;
1480 if ((status = encode_restorefh(&xdr)) != 0)
1481 goto out;
1482 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483out:
1484 return status;
1485}
1486
1487/*
1488 * Encode LINK request
1489 */
Al Viro8687b632006-10-19 23:28:48 -07001490static int nfs4_xdr_enc_link(struct rpc_rqst *req, __be32 *p, const struct nfs4_link_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
1492 struct xdr_stream xdr;
1493 struct compound_hdr hdr = {
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001494 .nops = 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 };
1496 int status;
1497
1498 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1499 encode_compound_hdr(&xdr, &hdr);
1500 if ((status = encode_putfh(&xdr, args->fh)) != 0)
1501 goto out;
1502 if ((status = encode_savefh(&xdr)) != 0)
1503 goto out;
1504 if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1505 goto out;
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001506 if ((status = encode_link(&xdr, args->name)) != 0)
1507 goto out;
1508 if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
1509 goto out;
1510 if ((status = encode_restorefh(&xdr)) != 0)
1511 goto out;
1512 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513out:
1514 return status;
1515}
1516
1517/*
1518 * Encode CREATE request
1519 */
Al Viro8687b632006-10-19 23:28:48 -07001520static int nfs4_xdr_enc_create(struct rpc_rqst *req, __be32 *p, const struct nfs4_create_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
1522 struct xdr_stream xdr;
1523 struct compound_hdr hdr = {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001524 .nops = 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 };
1526 int status;
1527
1528 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1529 encode_compound_hdr(&xdr, &hdr);
1530 if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
1531 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001532 if ((status = encode_savefh(&xdr)) != 0)
1533 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 if ((status = encode_create(&xdr, args)) != 0)
1535 goto out;
1536 if ((status = encode_getfh(&xdr)) != 0)
1537 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001538 if ((status = encode_getfattr(&xdr, args->bitmask)) != 0)
1539 goto out;
1540 if ((status = encode_restorefh(&xdr)) != 0)
1541 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 status = encode_getfattr(&xdr, args->bitmask);
1543out:
1544 return status;
1545}
1546
1547/*
1548 * Encode SYMLINK request
1549 */
Al Viro8687b632006-10-19 23:28:48 -07001550static int nfs4_xdr_enc_symlink(struct rpc_rqst *req, __be32 *p, const struct nfs4_create_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
1552 return nfs4_xdr_enc_create(req, p, args);
1553}
1554
1555/*
1556 * Encode GETATTR request
1557 */
Al Viro8687b632006-10-19 23:28:48 -07001558static int nfs4_xdr_enc_getattr(struct rpc_rqst *req, __be32 *p, const struct nfs4_getattr_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
1560 struct xdr_stream xdr;
1561 struct compound_hdr hdr = {
1562 .nops = 2,
1563 };
1564 int status;
1565
1566 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1567 encode_compound_hdr(&xdr, &hdr);
1568 if ((status = encode_putfh(&xdr, args->fh)) == 0)
1569 status = encode_getfattr(&xdr, args->bitmask);
1570 return status;
1571}
1572
1573/*
1574 * Encode a CLOSE request
1575 */
Al Viro8687b632006-10-19 23:28:48 -07001576static int nfs4_xdr_enc_close(struct rpc_rqst *req, __be32 *p, struct nfs_closeargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
1578 struct xdr_stream xdr;
1579 struct compound_hdr hdr = {
Trond Myklebust516a6af2005-10-27 22:12:41 -04001580 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 };
1582 int status;
1583
1584 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1585 encode_compound_hdr(&xdr, &hdr);
1586 status = encode_putfh(&xdr, args->fh);
1587 if(status)
1588 goto out;
1589 status = encode_close(&xdr, args);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001590 if (status != 0)
1591 goto out;
1592 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593out:
1594 return status;
1595}
1596
1597/*
1598 * Encode an OPEN request
1599 */
Al Viro8687b632006-10-19 23:28:48 -07001600static int nfs4_xdr_enc_open(struct rpc_rqst *req, __be32 *p, struct nfs_openargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
1602 struct xdr_stream xdr;
1603 struct compound_hdr hdr = {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001604 .nops = 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 };
1606 int status;
1607
1608 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1609 encode_compound_hdr(&xdr, &hdr);
1610 status = encode_putfh(&xdr, args->fh);
1611 if (status)
1612 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001613 status = encode_savefh(&xdr);
1614 if (status)
1615 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 status = encode_open(&xdr, args);
1617 if (status)
1618 goto out;
1619 status = encode_getfh(&xdr);
1620 if (status)
1621 goto out;
1622 status = encode_getfattr(&xdr, args->bitmask);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001623 if (status)
1624 goto out;
1625 status = encode_restorefh(&xdr);
1626 if (status)
1627 goto out;
1628 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629out:
1630 return status;
1631}
1632
1633/*
1634 * Encode an OPEN_CONFIRM request
1635 */
Al Viro8687b632006-10-19 23:28:48 -07001636static int nfs4_xdr_enc_open_confirm(struct rpc_rqst *req, __be32 *p, struct nfs_open_confirmargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
1638 struct xdr_stream xdr;
1639 struct compound_hdr hdr = {
1640 .nops = 2,
1641 };
1642 int status;
1643
1644 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1645 encode_compound_hdr(&xdr, &hdr);
1646 status = encode_putfh(&xdr, args->fh);
1647 if(status)
1648 goto out;
1649 status = encode_open_confirm(&xdr, args);
1650out:
1651 return status;
1652}
1653
1654/*
1655 * Encode an OPEN request with no attributes.
1656 */
Al Viro8687b632006-10-19 23:28:48 -07001657static int nfs4_xdr_enc_open_noattr(struct rpc_rqst *req, __be32 *p, struct nfs_openargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
1659 struct xdr_stream xdr;
1660 struct compound_hdr hdr = {
Trond Myklebust864472e2006-01-03 09:55:15 +01001661 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 };
1663 int status;
1664
1665 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1666 encode_compound_hdr(&xdr, &hdr);
1667 status = encode_putfh(&xdr, args->fh);
1668 if (status)
1669 goto out;
1670 status = encode_open(&xdr, args);
Trond Myklebust864472e2006-01-03 09:55:15 +01001671 if (status)
1672 goto out;
1673 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674out:
1675 return status;
1676}
1677
1678/*
1679 * Encode an OPEN_DOWNGRADE request
1680 */
Al Viro8687b632006-10-19 23:28:48 -07001681static int nfs4_xdr_enc_open_downgrade(struct rpc_rqst *req, __be32 *p, struct nfs_closeargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
1683 struct xdr_stream xdr;
1684 struct compound_hdr hdr = {
Trond Myklebust516a6af2005-10-27 22:12:41 -04001685 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 };
1687 int status;
1688
1689 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1690 encode_compound_hdr(&xdr, &hdr);
1691 status = encode_putfh(&xdr, args->fh);
1692 if (status)
1693 goto out;
1694 status = encode_open_downgrade(&xdr, args);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001695 if (status != 0)
1696 goto out;
1697 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698out:
1699 return status;
1700}
1701
1702/*
1703 * Encode a LOCK request
1704 */
Al Viro8687b632006-10-19 23:28:48 -07001705static int nfs4_xdr_enc_lock(struct rpc_rqst *req, __be32 *p, struct nfs_lock_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
1707 struct xdr_stream xdr;
1708 struct compound_hdr hdr = {
1709 .nops = 2,
1710 };
1711 int status;
1712
1713 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1714 encode_compound_hdr(&xdr, &hdr);
1715 status = encode_putfh(&xdr, args->fh);
1716 if(status)
1717 goto out;
1718 status = encode_lock(&xdr, args);
1719out:
1720 return status;
1721}
1722
1723/*
1724 * Encode a LOCKT request
1725 */
Al Viro8687b632006-10-19 23:28:48 -07001726static int nfs4_xdr_enc_lockt(struct rpc_rqst *req, __be32 *p, struct nfs_lockt_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
1728 struct xdr_stream xdr;
1729 struct compound_hdr hdr = {
1730 .nops = 2,
1731 };
1732 int status;
1733
1734 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1735 encode_compound_hdr(&xdr, &hdr);
1736 status = encode_putfh(&xdr, args->fh);
1737 if(status)
1738 goto out;
1739 status = encode_lockt(&xdr, args);
1740out:
1741 return status;
1742}
1743
1744/*
1745 * Encode a LOCKU request
1746 */
Al Viro8687b632006-10-19 23:28:48 -07001747static int nfs4_xdr_enc_locku(struct rpc_rqst *req, __be32 *p, struct nfs_locku_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
1749 struct xdr_stream xdr;
1750 struct compound_hdr hdr = {
1751 .nops = 2,
1752 };
1753 int status;
1754
1755 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1756 encode_compound_hdr(&xdr, &hdr);
1757 status = encode_putfh(&xdr, args->fh);
1758 if(status)
1759 goto out;
1760 status = encode_locku(&xdr, args);
1761out:
1762 return status;
1763}
1764
1765/*
1766 * Encode a READLINK request
1767 */
Al Viro8687b632006-10-19 23:28:48 -07001768static int nfs4_xdr_enc_readlink(struct rpc_rqst *req, __be32 *p, const struct nfs4_readlink *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
1770 struct xdr_stream xdr;
1771 struct compound_hdr hdr = {
1772 .nops = 2,
1773 };
Trond Myklebuste3a535e2007-07-19 10:03:38 -04001774 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
1775 unsigned int replen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 int status;
1777
1778 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1779 encode_compound_hdr(&xdr, &hdr);
1780 status = encode_putfh(&xdr, args->fh);
1781 if(status)
1782 goto out;
1783 status = encode_readlink(&xdr, args, req);
Trond Myklebuste3a535e2007-07-19 10:03:38 -04001784
1785 /* set up reply kvec
1786 * toplevel_status + taglen + rescount + OP_PUTFH + status
1787 * + OP_READLINK + status + string length = 8
1788 */
1789 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_readlink_sz) << 2;
1790 xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages,
1791 args->pgbase, args->pglen);
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793out:
1794 return status;
1795}
1796
1797/*
1798 * Encode a READDIR request
1799 */
Al Viro8687b632006-10-19 23:28:48 -07001800static int nfs4_xdr_enc_readdir(struct rpc_rqst *req, __be32 *p, const struct nfs4_readdir_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
1802 struct xdr_stream xdr;
1803 struct compound_hdr hdr = {
1804 .nops = 2,
1805 };
Trond Myklebustd6ac02d2007-07-19 10:03:37 -04001806 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
1807 int replen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 int status;
1809
1810 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1811 encode_compound_hdr(&xdr, &hdr);
1812 status = encode_putfh(&xdr, args->fh);
1813 if(status)
1814 goto out;
1815 status = encode_readdir(&xdr, args, req);
Trond Myklebustd6ac02d2007-07-19 10:03:37 -04001816
1817 /* set up reply kvec
1818 * toplevel_status + taglen + rescount + OP_PUTFH + status
1819 * + OP_READDIR + status + verifer(2) = 9
1820 */
1821 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_readdir_sz) << 2;
1822 xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages,
1823 args->pgbase, args->count);
1824 dprintk("%s: inlined page args = (%u, %p, %u, %u)\n",
1825 __FUNCTION__, replen, args->pages,
1826 args->pgbase, args->count);
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828out:
1829 return status;
1830}
1831
1832/*
1833 * Encode a READ request
1834 */
Al Viro8687b632006-10-19 23:28:48 -07001835static int nfs4_xdr_enc_read(struct rpc_rqst *req, __be32 *p, struct nfs_readargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
Trond Myklebust1be27f32007-06-27 14:29:04 -04001837 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 struct xdr_stream xdr;
1839 struct compound_hdr hdr = {
1840 .nops = 2,
1841 };
1842 int replen, status;
1843
1844 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1845 encode_compound_hdr(&xdr, &hdr);
1846 status = encode_putfh(&xdr, args->fh);
1847 if (status)
1848 goto out;
1849 status = encode_read(&xdr, args);
1850 if (status)
1851 goto out;
1852
1853 /* set up reply kvec
1854 * toplevel status + taglen=0 + rescount + OP_PUTFH + status
1855 * + OP_READ + status + eof + datalen = 9
1856 */
1857 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_read_sz) << 2;
1858 xdr_inline_pages(&req->rq_rcv_buf, replen,
1859 args->pages, args->pgbase, args->count);
1860out:
1861 return status;
1862}
1863
1864/*
1865 * Encode an SETATTR request
1866 */
Al Viro8687b632006-10-19 23:28:48 -07001867static int nfs4_xdr_enc_setattr(struct rpc_rqst *req, __be32 *p, struct nfs_setattrargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869{
1870 struct xdr_stream xdr;
1871 struct compound_hdr hdr = {
1872 .nops = 3,
1873 };
1874 int status;
1875
1876 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1877 encode_compound_hdr(&xdr, &hdr);
1878 status = encode_putfh(&xdr, args->fh);
1879 if(status)
1880 goto out;
1881 status = encode_setattr(&xdr, args, args->server);
1882 if(status)
1883 goto out;
1884 status = encode_getfattr(&xdr, args->bitmask);
1885out:
1886 return status;
1887}
1888
1889/*
J. Bruce Fields029d1052005-06-22 17:16:22 +00001890 * Encode a GETACL request
1891 */
1892static int
Al Viro8687b632006-10-19 23:28:48 -07001893nfs4_xdr_enc_getacl(struct rpc_rqst *req, __be32 *p,
J. Bruce Fields029d1052005-06-22 17:16:22 +00001894 struct nfs_getaclargs *args)
1895{
1896 struct xdr_stream xdr;
Trond Myklebust1be27f32007-06-27 14:29:04 -04001897 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
J. Bruce Fields029d1052005-06-22 17:16:22 +00001898 struct compound_hdr hdr = {
1899 .nops = 2,
1900 };
1901 int replen, status;
1902
1903 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1904 encode_compound_hdr(&xdr, &hdr);
1905 status = encode_putfh(&xdr, args->fh);
1906 if (status)
1907 goto out;
1908 status = encode_getattr_two(&xdr, FATTR4_WORD0_ACL, 0);
1909 /* set up reply buffer: */
1910 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_getacl_sz) << 2;
1911 xdr_inline_pages(&req->rq_rcv_buf, replen,
1912 args->acl_pages, args->acl_pgbase, args->acl_len);
1913out:
1914 return status;
1915}
1916
1917/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 * Encode a WRITE request
1919 */
Al Viro8687b632006-10-19 23:28:48 -07001920static int nfs4_xdr_enc_write(struct rpc_rqst *req, __be32 *p, struct nfs_writeargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
1922 struct xdr_stream xdr;
1923 struct compound_hdr hdr = {
Trond Myklebust4f9838c2005-10-27 22:12:44 -04001924 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 };
1926 int status;
1927
1928 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1929 encode_compound_hdr(&xdr, &hdr);
1930 status = encode_putfh(&xdr, args->fh);
1931 if (status)
1932 goto out;
1933 status = encode_write(&xdr, args);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04001934 if (status)
1935 goto out;
1936 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937out:
1938 return status;
1939}
1940
1941/*
1942 * a COMMIT request
1943 */
Al Viro8687b632006-10-19 23:28:48 -07001944static int nfs4_xdr_enc_commit(struct rpc_rqst *req, __be32 *p, struct nfs_writeargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
1946 struct xdr_stream xdr;
1947 struct compound_hdr hdr = {
Trond Myklebust4f9838c2005-10-27 22:12:44 -04001948 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 };
1950 int status;
1951
1952 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1953 encode_compound_hdr(&xdr, &hdr);
1954 status = encode_putfh(&xdr, args->fh);
1955 if (status)
1956 goto out;
1957 status = encode_commit(&xdr, args);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04001958 if (status)
1959 goto out;
1960 status = encode_getfattr(&xdr, args->bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961out:
1962 return status;
1963}
1964
1965/*
1966 * FSINFO request
1967 */
Al Viro8687b632006-10-19 23:28:48 -07001968static int nfs4_xdr_enc_fsinfo(struct rpc_rqst *req, __be32 *p, struct nfs4_fsinfo_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
1970 struct xdr_stream xdr;
1971 struct compound_hdr hdr = {
1972 .nops = 2,
1973 };
1974 int status;
1975
1976 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1977 encode_compound_hdr(&xdr, &hdr);
1978 status = encode_putfh(&xdr, args->fh);
1979 if (!status)
1980 status = encode_fsinfo(&xdr, args->bitmask);
1981 return status;
1982}
1983
1984/*
1985 * a PATHCONF request
1986 */
Al Viro8687b632006-10-19 23:28:48 -07001987static int nfs4_xdr_enc_pathconf(struct rpc_rqst *req, __be32 *p, const struct nfs4_pathconf_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 struct xdr_stream xdr;
1990 struct compound_hdr hdr = {
1991 .nops = 2,
1992 };
1993 int status;
1994
1995 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1996 encode_compound_hdr(&xdr, &hdr);
1997 status = encode_putfh(&xdr, args->fh);
1998 if (!status)
1999 status = encode_getattr_one(&xdr,
2000 args->bitmask[0] & nfs4_pathconf_bitmap[0]);
2001 return status;
2002}
2003
2004/*
2005 * a STATFS request
2006 */
Al Viro8687b632006-10-19 23:28:48 -07002007static int nfs4_xdr_enc_statfs(struct rpc_rqst *req, __be32 *p, const struct nfs4_statfs_arg *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 struct xdr_stream xdr;
2010 struct compound_hdr hdr = {
2011 .nops = 2,
2012 };
2013 int status;
2014
2015 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2016 encode_compound_hdr(&xdr, &hdr);
2017 status = encode_putfh(&xdr, args->fh);
2018 if (status == 0)
2019 status = encode_getattr_two(&xdr,
2020 args->bitmask[0] & nfs4_statfs_bitmap[0],
2021 args->bitmask[1] & nfs4_statfs_bitmap[1]);
2022 return status;
2023}
2024
2025/*
2026 * GETATTR_BITMAP request
2027 */
Al Viro8687b632006-10-19 23:28:48 -07002028static int nfs4_xdr_enc_server_caps(struct rpc_rqst *req, __be32 *p, const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
2030 struct xdr_stream xdr;
2031 struct compound_hdr hdr = {
2032 .nops = 2,
2033 };
2034 int status;
2035
2036 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2037 encode_compound_hdr(&xdr, &hdr);
2038 status = encode_putfh(&xdr, fhandle);
2039 if (status == 0)
2040 status = encode_getattr_one(&xdr, FATTR4_WORD0_SUPPORTED_ATTRS|
2041 FATTR4_WORD0_LINK_SUPPORT|
2042 FATTR4_WORD0_SYMLINK_SUPPORT|
2043 FATTR4_WORD0_ACLSUPPORT);
2044 return status;
2045}
2046
2047/*
2048 * a RENEW request
2049 */
Al Viro8687b632006-10-19 23:28:48 -07002050static int nfs4_xdr_enc_renew(struct rpc_rqst *req, __be32 *p, struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051{
2052 struct xdr_stream xdr;
2053 struct compound_hdr hdr = {
2054 .nops = 1,
2055 };
2056
2057 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2058 encode_compound_hdr(&xdr, &hdr);
2059 return encode_renew(&xdr, clp);
2060}
2061
2062/*
2063 * a SETCLIENTID request
2064 */
Al Viro8687b632006-10-19 23:28:48 -07002065static int nfs4_xdr_enc_setclientid(struct rpc_rqst *req, __be32 *p, struct nfs4_setclientid *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
2067 struct xdr_stream xdr;
2068 struct compound_hdr hdr = {
2069 .nops = 1,
2070 };
2071
2072 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2073 encode_compound_hdr(&xdr, &hdr);
2074 return encode_setclientid(&xdr, sc);
2075}
2076
2077/*
2078 * a SETCLIENTID_CONFIRM request
2079 */
Al Viro8687b632006-10-19 23:28:48 -07002080static int nfs4_xdr_enc_setclientid_confirm(struct rpc_rqst *req, __be32 *p, struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
2082 struct xdr_stream xdr;
2083 struct compound_hdr hdr = {
2084 .nops = 3,
2085 };
2086 const u32 lease_bitmap[2] = { FATTR4_WORD0_LEASE_TIME, 0 };
2087 int status;
2088
2089 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2090 encode_compound_hdr(&xdr, &hdr);
2091 status = encode_setclientid_confirm(&xdr, clp);
2092 if (!status)
2093 status = encode_putrootfh(&xdr);
2094 if (!status)
2095 status = encode_fsinfo(&xdr, lease_bitmap);
2096 return status;
2097}
2098
2099/*
2100 * DELEGRETURN request
2101 */
Al Viro8687b632006-10-19 23:28:48 -07002102static int nfs4_xdr_enc_delegreturn(struct rpc_rqst *req, __be32 *p, const struct nfs4_delegreturnargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
2104 struct xdr_stream xdr;
2105 struct compound_hdr hdr = {
Trond Myklebustfa178f22006-01-03 09:55:38 +01002106 .nops = 3,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 };
2108 int status;
2109
2110 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2111 encode_compound_hdr(&xdr, &hdr);
Trond Myklebustfa178f22006-01-03 09:55:38 +01002112 status = encode_putfh(&xdr, args->fhandle);
2113 if (status != 0)
2114 goto out;
2115 status = encode_delegreturn(&xdr, args->stateid);
2116 if (status != 0)
2117 goto out;
2118 status = encode_getfattr(&xdr, args->bitmask);
2119out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 return status;
2121}
2122
2123/*
Trond Myklebust683b57b2006-06-09 09:34:22 -04002124 * Encode FS_LOCATIONS request
2125 */
Al Viro8687b632006-10-19 23:28:48 -07002126static 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 -04002127{
2128 struct xdr_stream xdr;
2129 struct compound_hdr hdr = {
2130 .nops = 3,
2131 };
Trond Myklebust1be27f32007-06-27 14:29:04 -04002132 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
Trond Myklebust683b57b2006-06-09 09:34:22 -04002133 int replen;
2134 int status;
2135
2136 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2137 encode_compound_hdr(&xdr, &hdr);
2138 if ((status = encode_putfh(&xdr, args->dir_fh)) != 0)
2139 goto out;
2140 if ((status = encode_lookup(&xdr, args->name)) != 0)
2141 goto out;
Manoj Naik830b8e32006-06-09 09:34:25 -04002142 if ((status = encode_fs_locations(&xdr, args->bitmask)) != 0)
Trond Myklebust683b57b2006-06-09 09:34:22 -04002143 goto out;
2144 /* set up reply
Manoj Naik830b8e32006-06-09 09:34:25 -04002145 * toplevel_status + OP_PUTFH + status
Trond Myklebust683b57b2006-06-09 09:34:22 -04002146 * + OP_LOOKUP + status + OP_GETATTR + status = 7
2147 */
2148 replen = (RPC_REPHDRSIZE + auth->au_rslack + 7) << 2;
2149 xdr_inline_pages(&req->rq_rcv_buf, replen, &args->page,
2150 0, PAGE_SIZE);
2151out:
2152 return status;
2153}
2154
2155/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 * START OF "GENERIC" DECODE ROUTINES.
2157 * These may look a little ugly since they are imported from a "generic"
2158 * set of XDR encode/decode routines which are intended to be shared by
2159 * all of our NFSv4 implementations (OpenBSD, MacOS X...).
2160 *
2161 * If the pain of reading these is too great, it should be a straightforward
2162 * task to translate them into Linux-specific versions which are more
2163 * consistent with the style used in NFSv2/v3...
2164 */
2165#define READ32(x) (x) = ntohl(*p++)
2166#define READ64(x) do { \
2167 (x) = (u64)ntohl(*p++) << 32; \
2168 (x) |= ntohl(*p++); \
2169} while (0)
2170#define READTIME(x) do { \
2171 p++; \
2172 (x.tv_sec) = ntohl(*p++); \
2173 (x.tv_nsec) = ntohl(*p++); \
2174} while (0)
2175#define COPYMEM(x,nbytes) do { \
2176 memcpy((x), p, nbytes); \
2177 p += XDR_QUADLEN(nbytes); \
2178} while (0)
2179
2180#define READ_BUF(nbytes) do { \
2181 p = xdr_inline_decode(xdr, nbytes); \
Chuck Levere4cc6ee2007-05-08 18:23:28 -04002182 if (unlikely(!p)) { \
2183 printk(KERN_INFO "%s: prematurely hit end of receive" \
2184 " buffer\n", __FUNCTION__); \
2185 printk(KERN_INFO "%s: xdr->p=%p, bytes=%u, xdr->end=%p\n", \
2186 __FUNCTION__, xdr->p, nbytes, xdr->end); \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 return -EIO; \
2188 } \
2189} while (0)
2190
Trond Myklebust683b57b2006-06-09 09:34:22 -04002191static int decode_opaque_inline(struct xdr_stream *xdr, unsigned int *len, char **string)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
Al Viro8687b632006-10-19 23:28:48 -07002193 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
2195 READ_BUF(4);
2196 READ32(*len);
2197 READ_BUF(*len);
2198 *string = (char *)p;
2199 return 0;
2200}
2201
2202static int decode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
2203{
Al Viro8687b632006-10-19 23:28:48 -07002204 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
2206 READ_BUF(8);
2207 READ32(hdr->status);
2208 READ32(hdr->taglen);
2209
2210 READ_BUF(hdr->taglen + 4);
2211 hdr->tag = (char *)p;
2212 p += XDR_QUADLEN(hdr->taglen);
2213 READ32(hdr->nops);
2214 return 0;
2215}
2216
2217static int decode_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
2218{
Al Viro8687b632006-10-19 23:28:48 -07002219 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 uint32_t opnum;
2221 int32_t nfserr;
2222
2223 READ_BUF(8);
2224 READ32(opnum);
2225 if (opnum != expected) {
2226 printk(KERN_NOTICE
2227 "nfs4_decode_op_hdr: Server returned operation"
2228 " %d but we issued a request for %d\n",
2229 opnum, expected);
2230 return -EIO;
2231 }
2232 READ32(nfserr);
2233 if (nfserr != NFS_OK)
David Howells0a8ea432006-08-22 20:06:08 -04002234 return -nfs4_stat_to_errno(nfserr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 return 0;
2236}
2237
2238/* Dummy routine */
David Howellsadfa6f92006-08-22 20:06:08 -04002239static int decode_ace(struct xdr_stream *xdr, void *ace, struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240{
Al Viro8687b632006-10-19 23:28:48 -07002241 __be32 *p;
Trond Myklebust683b57b2006-06-09 09:34:22 -04002242 unsigned int strlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 char *str;
2244
2245 READ_BUF(12);
2246 return decode_opaque_inline(xdr, &strlen, &str);
2247}
2248
2249static int decode_attr_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
2250{
Al Viro8687b632006-10-19 23:28:48 -07002251 uint32_t bmlen;
2252 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
2254 READ_BUF(4);
2255 READ32(bmlen);
2256
2257 bitmap[0] = bitmap[1] = 0;
2258 READ_BUF((bmlen << 2));
2259 if (bmlen > 0) {
2260 READ32(bitmap[0]);
2261 if (bmlen > 1)
2262 READ32(bitmap[1]);
2263 }
2264 return 0;
2265}
2266
Al Viro8687b632006-10-19 23:28:48 -07002267static inline int decode_attr_length(struct xdr_stream *xdr, uint32_t *attrlen, __be32 **savep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268{
Al Viro8687b632006-10-19 23:28:48 -07002269 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
2271 READ_BUF(4);
2272 READ32(*attrlen);
2273 *savep = xdr->p;
2274 return 0;
2275}
2276
2277static int decode_attr_supported(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *bitmask)
2278{
2279 if (likely(bitmap[0] & FATTR4_WORD0_SUPPORTED_ATTRS)) {
2280 decode_attr_bitmap(xdr, bitmask);
2281 bitmap[0] &= ~FATTR4_WORD0_SUPPORTED_ATTRS;
2282 } else
2283 bitmask[0] = bitmask[1] = 0;
2284 dprintk("%s: bitmask=0x%x%x\n", __FUNCTION__, bitmask[0], bitmask[1]);
2285 return 0;
2286}
2287
2288static int decode_attr_type(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *type)
2289{
Al Viro8687b632006-10-19 23:28:48 -07002290 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
2292 *type = 0;
2293 if (unlikely(bitmap[0] & (FATTR4_WORD0_TYPE - 1U)))
2294 return -EIO;
2295 if (likely(bitmap[0] & FATTR4_WORD0_TYPE)) {
2296 READ_BUF(4);
2297 READ32(*type);
2298 if (*type < NF4REG || *type > NF4NAMEDATTR) {
2299 dprintk("%s: bad type %d\n", __FUNCTION__, *type);
2300 return -EIO;
2301 }
2302 bitmap[0] &= ~FATTR4_WORD0_TYPE;
2303 }
2304 dprintk("%s: type=0%o\n", __FUNCTION__, nfs_type2fmt[*type].nfs2type);
2305 return 0;
2306}
2307
2308static int decode_attr_change(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *change)
2309{
Al Viro8687b632006-10-19 23:28:48 -07002310 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
2312 *change = 0;
2313 if (unlikely(bitmap[0] & (FATTR4_WORD0_CHANGE - 1U)))
2314 return -EIO;
2315 if (likely(bitmap[0] & FATTR4_WORD0_CHANGE)) {
2316 READ_BUF(8);
2317 READ64(*change);
2318 bitmap[0] &= ~FATTR4_WORD0_CHANGE;
2319 }
2320 dprintk("%s: change attribute=%Lu\n", __FUNCTION__,
2321 (unsigned long long)*change);
2322 return 0;
2323}
2324
2325static int decode_attr_size(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *size)
2326{
Al Viro8687b632006-10-19 23:28:48 -07002327 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
2329 *size = 0;
2330 if (unlikely(bitmap[0] & (FATTR4_WORD0_SIZE - 1U)))
2331 return -EIO;
2332 if (likely(bitmap[0] & FATTR4_WORD0_SIZE)) {
2333 READ_BUF(8);
2334 READ64(*size);
2335 bitmap[0] &= ~FATTR4_WORD0_SIZE;
2336 }
2337 dprintk("%s: file size=%Lu\n", __FUNCTION__, (unsigned long long)*size);
2338 return 0;
2339}
2340
2341static int decode_attr_link_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2342{
Al Viro8687b632006-10-19 23:28:48 -07002343 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
2345 *res = 0;
2346 if (unlikely(bitmap[0] & (FATTR4_WORD0_LINK_SUPPORT - 1U)))
2347 return -EIO;
2348 if (likely(bitmap[0] & FATTR4_WORD0_LINK_SUPPORT)) {
2349 READ_BUF(4);
2350 READ32(*res);
2351 bitmap[0] &= ~FATTR4_WORD0_LINK_SUPPORT;
2352 }
2353 dprintk("%s: link support=%s\n", __FUNCTION__, *res == 0 ? "false" : "true");
2354 return 0;
2355}
2356
2357static int decode_attr_symlink_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2358{
Al Viro8687b632006-10-19 23:28:48 -07002359 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
2361 *res = 0;
2362 if (unlikely(bitmap[0] & (FATTR4_WORD0_SYMLINK_SUPPORT - 1U)))
2363 return -EIO;
2364 if (likely(bitmap[0] & FATTR4_WORD0_SYMLINK_SUPPORT)) {
2365 READ_BUF(4);
2366 READ32(*res);
2367 bitmap[0] &= ~FATTR4_WORD0_SYMLINK_SUPPORT;
2368 }
2369 dprintk("%s: symlink support=%s\n", __FUNCTION__, *res == 0 ? "false" : "true");
2370 return 0;
2371}
2372
Trond Myklebust8b4bdcf2006-06-09 09:34:19 -04002373static int decode_attr_fsid(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs_fsid *fsid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374{
Al Viro8687b632006-10-19 23:28:48 -07002375 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
2377 fsid->major = 0;
2378 fsid->minor = 0;
2379 if (unlikely(bitmap[0] & (FATTR4_WORD0_FSID - 1U)))
2380 return -EIO;
2381 if (likely(bitmap[0] & FATTR4_WORD0_FSID)) {
2382 READ_BUF(16);
2383 READ64(fsid->major);
2384 READ64(fsid->minor);
2385 bitmap[0] &= ~FATTR4_WORD0_FSID;
2386 }
2387 dprintk("%s: fsid=(0x%Lx/0x%Lx)\n", __FUNCTION__,
2388 (unsigned long long)fsid->major,
2389 (unsigned long long)fsid->minor);
2390 return 0;
2391}
2392
2393static int decode_attr_lease_time(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2394{
Al Viro8687b632006-10-19 23:28:48 -07002395 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
2397 *res = 60;
2398 if (unlikely(bitmap[0] & (FATTR4_WORD0_LEASE_TIME - 1U)))
2399 return -EIO;
2400 if (likely(bitmap[0] & FATTR4_WORD0_LEASE_TIME)) {
2401 READ_BUF(4);
2402 READ32(*res);
2403 bitmap[0] &= ~FATTR4_WORD0_LEASE_TIME;
2404 }
2405 dprintk("%s: file size=%u\n", __FUNCTION__, (unsigned int)*res);
2406 return 0;
2407}
2408
2409static int decode_attr_aclsupport(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2410{
Al Viro8687b632006-10-19 23:28:48 -07002411 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
2413 *res = ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL;
2414 if (unlikely(bitmap[0] & (FATTR4_WORD0_ACLSUPPORT - 1U)))
2415 return -EIO;
2416 if (likely(bitmap[0] & FATTR4_WORD0_ACLSUPPORT)) {
2417 READ_BUF(4);
2418 READ32(*res);
2419 bitmap[0] &= ~FATTR4_WORD0_ACLSUPPORT;
2420 }
2421 dprintk("%s: ACLs supported=%u\n", __FUNCTION__, (unsigned int)*res);
2422 return 0;
2423}
2424
2425static int decode_attr_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
2426{
Al Viro8687b632006-10-19 23:28:48 -07002427 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
2429 *fileid = 0;
2430 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILEID - 1U)))
2431 return -EIO;
2432 if (likely(bitmap[0] & FATTR4_WORD0_FILEID)) {
2433 READ_BUF(8);
2434 READ64(*fileid);
2435 bitmap[0] &= ~FATTR4_WORD0_FILEID;
2436 }
2437 dprintk("%s: fileid=%Lu\n", __FUNCTION__, (unsigned long long)*fileid);
2438 return 0;
2439}
2440
Manoj Naik99baf622006-06-09 09:34:24 -04002441static int decode_attr_mounted_on_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
2442{
Al Viro8687b632006-10-19 23:28:48 -07002443 __be32 *p;
Manoj Naik99baf622006-06-09 09:34:24 -04002444
2445 *fileid = 0;
2446 if (unlikely(bitmap[1] & (FATTR4_WORD1_MOUNTED_ON_FILEID - 1U)))
2447 return -EIO;
2448 if (likely(bitmap[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)) {
2449 READ_BUF(8);
2450 READ64(*fileid);
2451 bitmap[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
2452 }
2453 dprintk("%s: fileid=%Lu\n", __FUNCTION__, (unsigned long long)*fileid);
2454 return 0;
2455}
2456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457static int decode_attr_files_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2458{
Al Viro8687b632006-10-19 23:28:48 -07002459 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 int status = 0;
2461
2462 *res = 0;
2463 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_AVAIL - 1U)))
2464 return -EIO;
2465 if (likely(bitmap[0] & FATTR4_WORD0_FILES_AVAIL)) {
2466 READ_BUF(8);
2467 READ64(*res);
2468 bitmap[0] &= ~FATTR4_WORD0_FILES_AVAIL;
2469 }
2470 dprintk("%s: files avail=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2471 return status;
2472}
2473
2474static int decode_attr_files_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2475{
Al Viro8687b632006-10-19 23:28:48 -07002476 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 int status = 0;
2478
2479 *res = 0;
2480 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_FREE - 1U)))
2481 return -EIO;
2482 if (likely(bitmap[0] & FATTR4_WORD0_FILES_FREE)) {
2483 READ_BUF(8);
2484 READ64(*res);
2485 bitmap[0] &= ~FATTR4_WORD0_FILES_FREE;
2486 }
2487 dprintk("%s: files free=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2488 return status;
2489}
2490
2491static int decode_attr_files_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2492{
Al Viro8687b632006-10-19 23:28:48 -07002493 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 int status = 0;
2495
2496 *res = 0;
2497 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_TOTAL - 1U)))
2498 return -EIO;
2499 if (likely(bitmap[0] & FATTR4_WORD0_FILES_TOTAL)) {
2500 READ_BUF(8);
2501 READ64(*res);
2502 bitmap[0] &= ~FATTR4_WORD0_FILES_TOTAL;
2503 }
2504 dprintk("%s: files total=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2505 return status;
2506}
2507
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002508static int decode_pathname(struct xdr_stream *xdr, struct nfs4_pathname *path)
2509{
2510 int n;
Al Viro8687b632006-10-19 23:28:48 -07002511 __be32 *p;
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002512 int status = 0;
2513
2514 READ_BUF(4);
2515 READ32(n);
Andy Adamson33a43f22006-06-09 09:34:30 -04002516 if (n < 0)
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002517 goto out_eio;
Andy Adamson33a43f22006-06-09 09:34:30 -04002518 if (n == 0)
2519 goto root_path;
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002520 dprintk("path ");
2521 path->ncomponents = 0;
2522 while (path->ncomponents < n) {
2523 struct nfs4_string *component = &path->components[path->ncomponents];
2524 status = decode_opaque_inline(xdr, &component->len, &component->data);
2525 if (unlikely(status != 0))
2526 goto out_eio;
2527 if (path->ncomponents != n)
2528 dprintk("/");
2529 dprintk("%s", component->data);
2530 if (path->ncomponents < NFS4_PATHNAME_MAXCOMPONENTS)
2531 path->ncomponents++;
2532 else {
2533 dprintk("cannot parse %d components in path\n", n);
2534 goto out_eio;
2535 }
2536 }
2537out:
2538 dprintk("\n");
2539 return status;
Andy Adamson33a43f22006-06-09 09:34:30 -04002540root_path:
2541/* a root pathname is sent as a zero component4 */
2542 path->ncomponents = 1;
2543 path->components[0].len=0;
2544 path->components[0].data=NULL;
2545 dprintk("path /\n");
2546 goto out;
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002547out_eio:
2548 dprintk(" status %d", status);
2549 status = -EIO;
2550 goto out;
2551}
2552
2553static int decode_attr_fs_locations(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_fs_locations *res)
Trond Myklebust683b57b2006-06-09 09:34:22 -04002554{
2555 int n;
Al Viro8687b632006-10-19 23:28:48 -07002556 __be32 *p;
Trond Myklebust683b57b2006-06-09 09:34:22 -04002557 int status = -EIO;
2558
2559 if (unlikely(bitmap[0] & (FATTR4_WORD0_FS_LOCATIONS -1U)))
2560 goto out;
2561 status = 0;
2562 if (unlikely(!(bitmap[0] & FATTR4_WORD0_FS_LOCATIONS)))
2563 goto out;
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002564 dprintk("%s: fsroot ", __FUNCTION__);
2565 status = decode_pathname(xdr, &res->fs_path);
Trond Myklebust683b57b2006-06-09 09:34:22 -04002566 if (unlikely(status != 0))
2567 goto out;
2568 READ_BUF(4);
2569 READ32(n);
2570 if (n <= 0)
2571 goto out_eio;
2572 res->nlocations = 0;
2573 while (res->nlocations < n) {
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002574 int m;
2575 struct nfs4_fs_location *loc = &res->locations[res->nlocations];
Trond Myklebust683b57b2006-06-09 09:34:22 -04002576
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002577 READ_BUF(4);
2578 READ32(m);
2579 if (m <= 0)
2580 goto out_eio;
2581
2582 loc->nservers = 0;
2583 dprintk("%s: servers ", __FUNCTION__);
2584 while (loc->nservers < m) {
2585 struct nfs4_string *server = &loc->servers[loc->nservers];
2586 status = decode_opaque_inline(xdr, &server->len, &server->data);
2587 if (unlikely(status != 0))
2588 goto out_eio;
2589 dprintk("%s ", server->data);
2590 if (loc->nservers < NFS4_FS_LOCATION_MAXSERVERS)
2591 loc->nservers++;
2592 else {
2593 int i;
2594 dprintk("%s: using first %d of %d servers returned for location %d\n", __FUNCTION__, NFS4_FS_LOCATION_MAXSERVERS, m, res->nlocations);
2595 for (i = loc->nservers; i < m; i++) {
Trond Myklebust2e42c3e2007-05-14 17:20:41 -04002596 unsigned int len;
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002597 char *data;
2598 status = decode_opaque_inline(xdr, &len, &data);
2599 if (unlikely(status != 0))
2600 goto out_eio;
2601 }
2602 }
2603 }
2604 status = decode_pathname(xdr, &loc->rootpath);
Trond Myklebust683b57b2006-06-09 09:34:22 -04002605 if (unlikely(status != 0))
2606 goto out_eio;
Manoj Naik7aaa0b32006-06-09 09:34:23 -04002607 if (res->nlocations < NFS4_FS_LOCATIONS_MAXENTRIES)
Trond Myklebust683b57b2006-06-09 09:34:22 -04002608 res->nlocations++;
2609 }
2610out:
2611 dprintk("%s: fs_locations done, error = %d\n", __FUNCTION__, status);
2612 return status;
2613out_eio:
2614 status = -EIO;
2615 goto out;
2616}
2617
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618static int decode_attr_maxfilesize(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2619{
Al Viro8687b632006-10-19 23:28:48 -07002620 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 int status = 0;
2622
2623 *res = 0;
2624 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXFILESIZE - 1U)))
2625 return -EIO;
2626 if (likely(bitmap[0] & FATTR4_WORD0_MAXFILESIZE)) {
2627 READ_BUF(8);
2628 READ64(*res);
2629 bitmap[0] &= ~FATTR4_WORD0_MAXFILESIZE;
2630 }
2631 dprintk("%s: maxfilesize=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2632 return status;
2633}
2634
2635static int decode_attr_maxlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxlink)
2636{
Al Viro8687b632006-10-19 23:28:48 -07002637 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 int status = 0;
2639
2640 *maxlink = 1;
2641 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXLINK - 1U)))
2642 return -EIO;
2643 if (likely(bitmap[0] & FATTR4_WORD0_MAXLINK)) {
2644 READ_BUF(4);
2645 READ32(*maxlink);
2646 bitmap[0] &= ~FATTR4_WORD0_MAXLINK;
2647 }
2648 dprintk("%s: maxlink=%u\n", __FUNCTION__, *maxlink);
2649 return status;
2650}
2651
2652static int decode_attr_maxname(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxname)
2653{
Al Viro8687b632006-10-19 23:28:48 -07002654 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 int status = 0;
2656
2657 *maxname = 1024;
2658 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXNAME - 1U)))
2659 return -EIO;
2660 if (likely(bitmap[0] & FATTR4_WORD0_MAXNAME)) {
2661 READ_BUF(4);
2662 READ32(*maxname);
2663 bitmap[0] &= ~FATTR4_WORD0_MAXNAME;
2664 }
2665 dprintk("%s: maxname=%u\n", __FUNCTION__, *maxname);
2666 return status;
2667}
2668
2669static int decode_attr_maxread(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2670{
Al Viro8687b632006-10-19 23:28:48 -07002671 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 int status = 0;
2673
2674 *res = 1024;
2675 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXREAD - 1U)))
2676 return -EIO;
2677 if (likely(bitmap[0] & FATTR4_WORD0_MAXREAD)) {
2678 uint64_t maxread;
2679 READ_BUF(8);
2680 READ64(maxread);
2681 if (maxread > 0x7FFFFFFF)
2682 maxread = 0x7FFFFFFF;
2683 *res = (uint32_t)maxread;
2684 bitmap[0] &= ~FATTR4_WORD0_MAXREAD;
2685 }
2686 dprintk("%s: maxread=%lu\n", __FUNCTION__, (unsigned long)*res);
2687 return status;
2688}
2689
2690static int decode_attr_maxwrite(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2691{
Al Viro8687b632006-10-19 23:28:48 -07002692 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 int status = 0;
2694
2695 *res = 1024;
2696 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXWRITE - 1U)))
2697 return -EIO;
2698 if (likely(bitmap[0] & FATTR4_WORD0_MAXWRITE)) {
2699 uint64_t maxwrite;
2700 READ_BUF(8);
2701 READ64(maxwrite);
2702 if (maxwrite > 0x7FFFFFFF)
2703 maxwrite = 0x7FFFFFFF;
2704 *res = (uint32_t)maxwrite;
2705 bitmap[0] &= ~FATTR4_WORD0_MAXWRITE;
2706 }
2707 dprintk("%s: maxwrite=%lu\n", __FUNCTION__, (unsigned long)*res);
2708 return status;
2709}
2710
2711static int decode_attr_mode(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *mode)
2712{
Al Viro8687b632006-10-19 23:28:48 -07002713 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
2715 *mode = 0;
2716 if (unlikely(bitmap[1] & (FATTR4_WORD1_MODE - 1U)))
2717 return -EIO;
2718 if (likely(bitmap[1] & FATTR4_WORD1_MODE)) {
2719 READ_BUF(4);
2720 READ32(*mode);
2721 *mode &= ~S_IFMT;
2722 bitmap[1] &= ~FATTR4_WORD1_MODE;
2723 }
2724 dprintk("%s: file mode=0%o\n", __FUNCTION__, (unsigned int)*mode);
2725 return 0;
2726}
2727
2728static int decode_attr_nlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *nlink)
2729{
Al Viro8687b632006-10-19 23:28:48 -07002730 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
2732 *nlink = 1;
2733 if (unlikely(bitmap[1] & (FATTR4_WORD1_NUMLINKS - 1U)))
2734 return -EIO;
2735 if (likely(bitmap[1] & FATTR4_WORD1_NUMLINKS)) {
2736 READ_BUF(4);
2737 READ32(*nlink);
2738 bitmap[1] &= ~FATTR4_WORD1_NUMLINKS;
2739 }
2740 dprintk("%s: nlink=%u\n", __FUNCTION__, (unsigned int)*nlink);
2741 return 0;
2742}
2743
Trond Myklebust2e42c3e2007-05-14 17:20:41 -04002744static int decode_attr_owner(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs_client *clp, uint32_t *uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745{
Al Viro8687b632006-10-19 23:28:48 -07002746 uint32_t len;
2747 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
2749 *uid = -2;
2750 if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER - 1U)))
2751 return -EIO;
2752 if (likely(bitmap[1] & FATTR4_WORD1_OWNER)) {
2753 READ_BUF(4);
2754 READ32(len);
2755 READ_BUF(len);
2756 if (len < XDR_MAX_NETOBJ) {
2757 if (nfs_map_name_to_uid(clp, (char *)p, len, uid) != 0)
2758 dprintk("%s: nfs_map_name_to_uid failed!\n",
2759 __FUNCTION__);
2760 } else
2761 printk(KERN_WARNING "%s: name too long (%u)!\n",
2762 __FUNCTION__, len);
2763 bitmap[1] &= ~FATTR4_WORD1_OWNER;
2764 }
2765 dprintk("%s: uid=%d\n", __FUNCTION__, (int)*uid);
2766 return 0;
2767}
2768
Trond Myklebust2e42c3e2007-05-14 17:20:41 -04002769static int decode_attr_group(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs_client *clp, uint32_t *gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770{
Al Viro8687b632006-10-19 23:28:48 -07002771 uint32_t len;
2772 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
2774 *gid = -2;
2775 if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER_GROUP - 1U)))
2776 return -EIO;
2777 if (likely(bitmap[1] & FATTR4_WORD1_OWNER_GROUP)) {
2778 READ_BUF(4);
2779 READ32(len);
2780 READ_BUF(len);
2781 if (len < XDR_MAX_NETOBJ) {
2782 if (nfs_map_group_to_gid(clp, (char *)p, len, gid) != 0)
2783 dprintk("%s: nfs_map_group_to_gid failed!\n",
2784 __FUNCTION__);
2785 } else
2786 printk(KERN_WARNING "%s: name too long (%u)!\n",
2787 __FUNCTION__, len);
2788 bitmap[1] &= ~FATTR4_WORD1_OWNER_GROUP;
2789 }
2790 dprintk("%s: gid=%d\n", __FUNCTION__, (int)*gid);
2791 return 0;
2792}
2793
2794static int decode_attr_rdev(struct xdr_stream *xdr, uint32_t *bitmap, dev_t *rdev)
2795{
Al Viro8687b632006-10-19 23:28:48 -07002796 uint32_t major = 0, minor = 0;
2797 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
2799 *rdev = MKDEV(0,0);
2800 if (unlikely(bitmap[1] & (FATTR4_WORD1_RAWDEV - 1U)))
2801 return -EIO;
2802 if (likely(bitmap[1] & FATTR4_WORD1_RAWDEV)) {
2803 dev_t tmp;
2804
2805 READ_BUF(8);
2806 READ32(major);
2807 READ32(minor);
2808 tmp = MKDEV(major, minor);
2809 if (MAJOR(tmp) == major && MINOR(tmp) == minor)
2810 *rdev = tmp;
2811 bitmap[1] &= ~ FATTR4_WORD1_RAWDEV;
2812 }
2813 dprintk("%s: rdev=(0x%x:0x%x)\n", __FUNCTION__, major, minor);
2814 return 0;
2815}
2816
2817static int decode_attr_space_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2818{
Al Viro8687b632006-10-19 23:28:48 -07002819 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 int status = 0;
2821
2822 *res = 0;
2823 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_AVAIL - 1U)))
2824 return -EIO;
2825 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_AVAIL)) {
2826 READ_BUF(8);
2827 READ64(*res);
2828 bitmap[1] &= ~FATTR4_WORD1_SPACE_AVAIL;
2829 }
2830 dprintk("%s: space avail=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2831 return status;
2832}
2833
2834static int decode_attr_space_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2835{
Al Viro8687b632006-10-19 23:28:48 -07002836 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 int status = 0;
2838
2839 *res = 0;
2840 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_FREE - 1U)))
2841 return -EIO;
2842 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_FREE)) {
2843 READ_BUF(8);
2844 READ64(*res);
2845 bitmap[1] &= ~FATTR4_WORD1_SPACE_FREE;
2846 }
2847 dprintk("%s: space free=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2848 return status;
2849}
2850
2851static int decode_attr_space_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2852{
Al Viro8687b632006-10-19 23:28:48 -07002853 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 int status = 0;
2855
2856 *res = 0;
2857 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_TOTAL - 1U)))
2858 return -EIO;
2859 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_TOTAL)) {
2860 READ_BUF(8);
2861 READ64(*res);
2862 bitmap[1] &= ~FATTR4_WORD1_SPACE_TOTAL;
2863 }
2864 dprintk("%s: space total=%Lu\n", __FUNCTION__, (unsigned long long)*res);
2865 return status;
2866}
2867
2868static int decode_attr_space_used(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *used)
2869{
Al Viro8687b632006-10-19 23:28:48 -07002870 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
2872 *used = 0;
2873 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_USED - 1U)))
2874 return -EIO;
2875 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_USED)) {
2876 READ_BUF(8);
2877 READ64(*used);
2878 bitmap[1] &= ~FATTR4_WORD1_SPACE_USED;
2879 }
2880 dprintk("%s: space used=%Lu\n", __FUNCTION__,
2881 (unsigned long long)*used);
2882 return 0;
2883}
2884
2885static int decode_attr_time(struct xdr_stream *xdr, struct timespec *time)
2886{
Al Viro8687b632006-10-19 23:28:48 -07002887 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 uint64_t sec;
2889 uint32_t nsec;
2890
2891 READ_BUF(12);
2892 READ64(sec);
2893 READ32(nsec);
2894 time->tv_sec = (time_t)sec;
2895 time->tv_nsec = (long)nsec;
2896 return 0;
2897}
2898
2899static int decode_attr_time_access(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2900{
2901 int status = 0;
2902
2903 time->tv_sec = 0;
2904 time->tv_nsec = 0;
2905 if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_ACCESS - 1U)))
2906 return -EIO;
2907 if (likely(bitmap[1] & FATTR4_WORD1_TIME_ACCESS)) {
2908 status = decode_attr_time(xdr, time);
2909 bitmap[1] &= ~FATTR4_WORD1_TIME_ACCESS;
2910 }
2911 dprintk("%s: atime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2912 return status;
2913}
2914
2915static int decode_attr_time_metadata(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2916{
2917 int status = 0;
2918
2919 time->tv_sec = 0;
2920 time->tv_nsec = 0;
2921 if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_METADATA - 1U)))
2922 return -EIO;
2923 if (likely(bitmap[1] & FATTR4_WORD1_TIME_METADATA)) {
2924 status = decode_attr_time(xdr, time);
2925 bitmap[1] &= ~FATTR4_WORD1_TIME_METADATA;
2926 }
2927 dprintk("%s: ctime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2928 return status;
2929}
2930
2931static int decode_attr_time_modify(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2932{
2933 int status = 0;
2934
2935 time->tv_sec = 0;
2936 time->tv_nsec = 0;
2937 if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_MODIFY - 1U)))
2938 return -EIO;
2939 if (likely(bitmap[1] & FATTR4_WORD1_TIME_MODIFY)) {
2940 status = decode_attr_time(xdr, time);
2941 bitmap[1] &= ~FATTR4_WORD1_TIME_MODIFY;
2942 }
2943 dprintk("%s: mtime=%ld\n", __FUNCTION__, (long)time->tv_sec);
2944 return status;
2945}
2946
Al Viro8687b632006-10-19 23:28:48 -07002947static int verify_attr_len(struct xdr_stream *xdr, __be32 *savep, uint32_t attrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948{
2949 unsigned int attrwords = XDR_QUADLEN(attrlen);
2950 unsigned int nwords = xdr->p - savep;
2951
2952 if (unlikely(attrwords != nwords)) {
2953 printk(KERN_WARNING "%s: server returned incorrect attribute length: %u %c %u\n",
2954 __FUNCTION__,
2955 attrwords << 2,
2956 (attrwords < nwords) ? '<' : '>',
2957 nwords << 2);
2958 return -EIO;
2959 }
2960 return 0;
2961}
2962
2963static int decode_change_info(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
2964{
Al Viro8687b632006-10-19 23:28:48 -07002965 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
2967 READ_BUF(20);
2968 READ32(cinfo->atomic);
2969 READ64(cinfo->before);
2970 READ64(cinfo->after);
2971 return 0;
2972}
2973
2974static int decode_access(struct xdr_stream *xdr, struct nfs4_accessres *access)
2975{
Al Viro8687b632006-10-19 23:28:48 -07002976 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 uint32_t supp, acc;
2978 int status;
2979
2980 status = decode_op_hdr(xdr, OP_ACCESS);
2981 if (status)
2982 return status;
2983 READ_BUF(8);
2984 READ32(supp);
2985 READ32(acc);
2986 access->supported = supp;
2987 access->access = acc;
2988 return 0;
2989}
2990
2991static int decode_close(struct xdr_stream *xdr, struct nfs_closeres *res)
2992{
Al Viro8687b632006-10-19 23:28:48 -07002993 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 int status;
2995
2996 status = decode_op_hdr(xdr, OP_CLOSE);
2997 if (status)
2998 return status;
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04002999 READ_BUF(NFS4_STATEID_SIZE);
3000 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 return 0;
3002}
3003
3004static int decode_commit(struct xdr_stream *xdr, struct nfs_writeres *res)
3005{
Al Viro8687b632006-10-19 23:28:48 -07003006 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 int status;
3008
3009 status = decode_op_hdr(xdr, OP_COMMIT);
3010 if (status)
3011 return status;
3012 READ_BUF(8);
3013 COPYMEM(res->verf->verifier, 8);
3014 return 0;
3015}
3016
3017static int decode_create(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
3018{
Al Viro8687b632006-10-19 23:28:48 -07003019 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 uint32_t bmlen;
3021 int status;
3022
3023 status = decode_op_hdr(xdr, OP_CREATE);
3024 if (status)
3025 return status;
3026 if ((status = decode_change_info(xdr, cinfo)))
3027 return status;
3028 READ_BUF(4);
3029 READ32(bmlen);
3030 READ_BUF(bmlen << 2);
3031 return 0;
3032}
3033
3034static int decode_server_caps(struct xdr_stream *xdr, struct nfs4_server_caps_res *res)
3035{
Al Viro8687b632006-10-19 23:28:48 -07003036 __be32 *savep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 uint32_t attrlen,
3038 bitmap[2] = {0};
3039 int status;
3040
3041 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3042 goto xdr_error;
3043 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3044 goto xdr_error;
3045 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3046 goto xdr_error;
3047 if ((status = decode_attr_supported(xdr, bitmap, res->attr_bitmask)) != 0)
3048 goto xdr_error;
3049 if ((status = decode_attr_link_support(xdr, bitmap, &res->has_links)) != 0)
3050 goto xdr_error;
3051 if ((status = decode_attr_symlink_support(xdr, bitmap, &res->has_symlinks)) != 0)
3052 goto xdr_error;
3053 if ((status = decode_attr_aclsupport(xdr, bitmap, &res->acl_bitmask)) != 0)
3054 goto xdr_error;
3055 status = verify_attr_len(xdr, savep, attrlen);
3056xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04003057 dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 return status;
3059}
3060
3061static int decode_statfs(struct xdr_stream *xdr, struct nfs_fsstat *fsstat)
3062{
Al Viro8687b632006-10-19 23:28:48 -07003063 __be32 *savep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 uint32_t attrlen,
3065 bitmap[2] = {0};
3066 int status;
3067
3068 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3069 goto xdr_error;
3070 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3071 goto xdr_error;
3072 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3073 goto xdr_error;
3074
3075 if ((status = decode_attr_files_avail(xdr, bitmap, &fsstat->afiles)) != 0)
3076 goto xdr_error;
3077 if ((status = decode_attr_files_free(xdr, bitmap, &fsstat->ffiles)) != 0)
3078 goto xdr_error;
3079 if ((status = decode_attr_files_total(xdr, bitmap, &fsstat->tfiles)) != 0)
3080 goto xdr_error;
3081 if ((status = decode_attr_space_avail(xdr, bitmap, &fsstat->abytes)) != 0)
3082 goto xdr_error;
3083 if ((status = decode_attr_space_free(xdr, bitmap, &fsstat->fbytes)) != 0)
3084 goto xdr_error;
3085 if ((status = decode_attr_space_total(xdr, bitmap, &fsstat->tbytes)) != 0)
3086 goto xdr_error;
3087
3088 status = verify_attr_len(xdr, savep, attrlen);
3089xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04003090 dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 return status;
3092}
3093
3094static int decode_pathconf(struct xdr_stream *xdr, struct nfs_pathconf *pathconf)
3095{
Al Viro8687b632006-10-19 23:28:48 -07003096 __be32 *savep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 uint32_t attrlen,
3098 bitmap[2] = {0};
3099 int status;
3100
3101 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3102 goto xdr_error;
3103 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3104 goto xdr_error;
3105 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3106 goto xdr_error;
3107
3108 if ((status = decode_attr_maxlink(xdr, bitmap, &pathconf->max_link)) != 0)
3109 goto xdr_error;
3110 if ((status = decode_attr_maxname(xdr, bitmap, &pathconf->max_namelen)) != 0)
3111 goto xdr_error;
3112
3113 status = verify_attr_len(xdr, savep, attrlen);
3114xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04003115 dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 return status;
3117}
3118
3119static int decode_getfattr(struct xdr_stream *xdr, struct nfs_fattr *fattr, const struct nfs_server *server)
3120{
Al Viro8687b632006-10-19 23:28:48 -07003121 __be32 *savep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 uint32_t attrlen,
3123 bitmap[2] = {0},
3124 type;
3125 int status, fmode = 0;
Manoj Naik99baf622006-06-09 09:34:24 -04003126 uint64_t fileid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
3128 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3129 goto xdr_error;
3130 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3131 goto xdr_error;
3132
3133 fattr->bitmap[0] = bitmap[0];
3134 fattr->bitmap[1] = bitmap[1];
3135
3136 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3137 goto xdr_error;
3138
3139
3140 if ((status = decode_attr_type(xdr, bitmap, &type)) != 0)
3141 goto xdr_error;
3142 fattr->type = nfs_type2fmt[type].nfs2type;
3143 fmode = nfs_type2fmt[type].mode;
3144
3145 if ((status = decode_attr_change(xdr, bitmap, &fattr->change_attr)) != 0)
3146 goto xdr_error;
3147 if ((status = decode_attr_size(xdr, bitmap, &fattr->size)) != 0)
3148 goto xdr_error;
Trond Myklebust8b4bdcf2006-06-09 09:34:19 -04003149 if ((status = decode_attr_fsid(xdr, bitmap, &fattr->fsid)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 goto xdr_error;
3151 if ((status = decode_attr_fileid(xdr, bitmap, &fattr->fileid)) != 0)
3152 goto xdr_error;
Trond Myklebust683b57b2006-06-09 09:34:22 -04003153 if ((status = decode_attr_fs_locations(xdr, bitmap, container_of(fattr,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04003154 struct nfs4_fs_locations,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003155 fattr))) != 0)
3156 goto xdr_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 if ((status = decode_attr_mode(xdr, bitmap, &fattr->mode)) != 0)
3158 goto xdr_error;
3159 fattr->mode |= fmode;
3160 if ((status = decode_attr_nlink(xdr, bitmap, &fattr->nlink)) != 0)
3161 goto xdr_error;
David Howells7539bba2006-08-22 20:06:09 -04003162 if ((status = decode_attr_owner(xdr, bitmap, server->nfs_client, &fattr->uid)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 goto xdr_error;
David Howells7539bba2006-08-22 20:06:09 -04003164 if ((status = decode_attr_group(xdr, bitmap, server->nfs_client, &fattr->gid)) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 goto xdr_error;
3166 if ((status = decode_attr_rdev(xdr, bitmap, &fattr->rdev)) != 0)
3167 goto xdr_error;
3168 if ((status = decode_attr_space_used(xdr, bitmap, &fattr->du.nfs3.used)) != 0)
3169 goto xdr_error;
3170 if ((status = decode_attr_time_access(xdr, bitmap, &fattr->atime)) != 0)
3171 goto xdr_error;
3172 if ((status = decode_attr_time_metadata(xdr, bitmap, &fattr->ctime)) != 0)
3173 goto xdr_error;
3174 if ((status = decode_attr_time_modify(xdr, bitmap, &fattr->mtime)) != 0)
3175 goto xdr_error;
Manoj Naik99baf622006-06-09 09:34:24 -04003176 if ((status = decode_attr_mounted_on_fileid(xdr, bitmap, &fileid)) != 0)
3177 goto xdr_error;
3178 if (fattr->fileid == 0 && fileid != 0)
3179 fattr->fileid = fileid;
Trond Myklebust33801142005-10-27 22:12:39 -04003180 if ((status = verify_attr_len(xdr, savep, attrlen)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 fattr->valid = NFS_ATTR_FATTR | NFS_ATTR_FATTR_V3 | NFS_ATTR_FATTR_V4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04003183 dprintk("%s: xdr returned %d\n", __FUNCTION__, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 return status;
3185}
3186
3187
3188static int decode_fsinfo(struct xdr_stream *xdr, struct nfs_fsinfo *fsinfo)
3189{
Al Viro8687b632006-10-19 23:28:48 -07003190 __be32 *savep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 uint32_t attrlen, bitmap[2];
3192 int status;
3193
3194 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3195 goto xdr_error;
3196 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3197 goto xdr_error;
3198 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3199 goto xdr_error;
3200
3201 fsinfo->rtmult = fsinfo->wtmult = 512; /* ??? */
3202
3203 if ((status = decode_attr_lease_time(xdr, bitmap, &fsinfo->lease_time)) != 0)
3204 goto xdr_error;
3205 if ((status = decode_attr_maxfilesize(xdr, bitmap, &fsinfo->maxfilesize)) != 0)
3206 goto xdr_error;
3207 if ((status = decode_attr_maxread(xdr, bitmap, &fsinfo->rtmax)) != 0)
3208 goto xdr_error;
3209 fsinfo->rtpref = fsinfo->dtpref = fsinfo->rtmax;
3210 if ((status = decode_attr_maxwrite(xdr, bitmap, &fsinfo->wtmax)) != 0)
3211 goto xdr_error;
3212 fsinfo->wtpref = fsinfo->wtmax;
3213
3214 status = verify_attr_len(xdr, savep, attrlen);
3215xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04003216 dprintk("%s: xdr returned %d!\n", __FUNCTION__, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 return status;
3218}
3219
3220static int decode_getfh(struct xdr_stream *xdr, struct nfs_fh *fh)
3221{
Al Viro8687b632006-10-19 23:28:48 -07003222 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 uint32_t len;
3224 int status;
3225
Trond Myklebust99367812007-07-17 21:52:41 -04003226 /* Zero handle first to allow comparisons */
3227 memset(fh, 0, sizeof(*fh));
3228
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 status = decode_op_hdr(xdr, OP_GETFH);
3230 if (status)
3231 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
3233 READ_BUF(4);
3234 READ32(len);
3235 if (len > NFS4_FHSIZE)
3236 return -EIO;
3237 fh->size = len;
3238 READ_BUF(len);
3239 COPYMEM(fh->data, len);
3240 return 0;
3241}
3242
3243static int decode_link(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
3244{
3245 int status;
3246
3247 status = decode_op_hdr(xdr, OP_LINK);
3248 if (status)
3249 return status;
3250 return decode_change_info(xdr, cinfo);
3251}
3252
3253/*
3254 * We create the owner, so we know a proper owner.id length is 4.
3255 */
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003256static int decode_lock_denied (struct xdr_stream *xdr, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257{
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003258 uint64_t offset, length, clientid;
Al Viro8687b632006-10-19 23:28:48 -07003259 __be32 *p;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003260 uint32_t namelen, type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
3262 READ_BUF(32);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003263 READ64(offset);
3264 READ64(length);
3265 READ32(type);
3266 if (fl != NULL) {
3267 fl->fl_start = (loff_t)offset;
3268 fl->fl_end = fl->fl_start + (loff_t)length - 1;
3269 if (length == ~(uint64_t)0)
3270 fl->fl_end = OFFSET_MAX;
3271 fl->fl_type = F_WRLCK;
3272 if (type & 1)
3273 fl->fl_type = F_RDLCK;
3274 fl->fl_pid = 0;
3275 }
3276 READ64(clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 READ32(namelen);
3278 READ_BUF(namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 return -NFS4ERR_DENIED;
3280}
3281
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003282static int decode_lock(struct xdr_stream *xdr, struct nfs_lock_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283{
Al Viro8687b632006-10-19 23:28:48 -07003284 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 int status;
3286
3287 status = decode_op_hdr(xdr, OP_LOCK);
3288 if (status == 0) {
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003289 READ_BUF(NFS4_STATEID_SIZE);
3290 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 } else if (status == -NFS4ERR_DENIED)
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003292 return decode_lock_denied(xdr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 return status;
3294}
3295
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003296static int decode_lockt(struct xdr_stream *xdr, struct nfs_lockt_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
3298 int status;
3299 status = decode_op_hdr(xdr, OP_LOCKT);
3300 if (status == -NFS4ERR_DENIED)
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003301 return decode_lock_denied(xdr, res->denied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 return status;
3303}
3304
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003305static int decode_locku(struct xdr_stream *xdr, struct nfs_locku_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306{
Al Viro8687b632006-10-19 23:28:48 -07003307 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 int status;
3309
3310 status = decode_op_hdr(xdr, OP_LOCKU);
3311 if (status == 0) {
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003312 READ_BUF(NFS4_STATEID_SIZE);
3313 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 }
3315 return status;
3316}
3317
3318static int decode_lookup(struct xdr_stream *xdr)
3319{
3320 return decode_op_hdr(xdr, OP_LOOKUP);
3321}
3322
3323/* This is too sick! */
3324static int decode_space_limit(struct xdr_stream *xdr, u64 *maxsize)
3325{
Al Viro8687b632006-10-19 23:28:48 -07003326 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 uint32_t limit_type, nblocks, blocksize;
3328
3329 READ_BUF(12);
3330 READ32(limit_type);
3331 switch (limit_type) {
3332 case 1:
3333 READ64(*maxsize);
3334 break;
3335 case 2:
3336 READ32(nblocks);
3337 READ32(blocksize);
3338 *maxsize = (uint64_t)nblocks * (uint64_t)blocksize;
3339 }
3340 return 0;
3341}
3342
3343static int decode_delegation(struct xdr_stream *xdr, struct nfs_openres *res)
3344{
Al Viro8687b632006-10-19 23:28:48 -07003345 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 uint32_t delegation_type;
3347
3348 READ_BUF(4);
3349 READ32(delegation_type);
3350 if (delegation_type == NFS4_OPEN_DELEGATE_NONE) {
3351 res->delegation_type = 0;
3352 return 0;
3353 }
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003354 READ_BUF(NFS4_STATEID_SIZE+4);
3355 COPYMEM(res->delegation.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 READ32(res->do_recall);
3357 switch (delegation_type) {
3358 case NFS4_OPEN_DELEGATE_READ:
3359 res->delegation_type = FMODE_READ;
3360 break;
3361 case NFS4_OPEN_DELEGATE_WRITE:
3362 res->delegation_type = FMODE_WRITE|FMODE_READ;
3363 if (decode_space_limit(xdr, &res->maxsize) < 0)
3364 return -EIO;
3365 }
David Howells7539bba2006-08-22 20:06:09 -04003366 return decode_ace(xdr, NULL, res->server->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367}
3368
3369static int decode_open(struct xdr_stream *xdr, struct nfs_openres *res)
3370{
Al Viro8687b632006-10-19 23:28:48 -07003371 __be32 *p;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003372 uint32_t savewords, bmlen, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 int status;
3374
3375 status = decode_op_hdr(xdr, OP_OPEN);
3376 if (status)
3377 return status;
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003378 READ_BUF(NFS4_STATEID_SIZE);
3379 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380
3381 decode_change_info(xdr, &res->cinfo);
3382
3383 READ_BUF(8);
3384 READ32(res->rflags);
3385 READ32(bmlen);
3386 if (bmlen > 10)
3387 goto xdr_error;
3388
3389 READ_BUF(bmlen << 2);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003390 savewords = min_t(uint32_t, bmlen, NFS4_BITMAP_SIZE);
3391 for (i = 0; i < savewords; ++i)
3392 READ32(res->attrset[i]);
3393 for (; i < NFS4_BITMAP_SIZE; i++)
3394 res->attrset[i] = 0;
3395
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 return decode_delegation(xdr, res);
3397xdr_error:
Trond Myklebust16c32b72005-10-27 22:12:45 -04003398 dprintk("%s: Bitmap too large! Length = %u\n", __FUNCTION__, bmlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 return -EIO;
3400}
3401
3402static int decode_open_confirm(struct xdr_stream *xdr, struct nfs_open_confirmres *res)
3403{
Al Viro8687b632006-10-19 23:28:48 -07003404 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 int status;
3406
3407 status = decode_op_hdr(xdr, OP_OPEN_CONFIRM);
3408 if (status)
3409 return status;
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003410 READ_BUF(NFS4_STATEID_SIZE);
3411 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 return 0;
3413}
3414
3415static int decode_open_downgrade(struct xdr_stream *xdr, struct nfs_closeres *res)
3416{
Al Viro8687b632006-10-19 23:28:48 -07003417 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 int status;
3419
3420 status = decode_op_hdr(xdr, OP_OPEN_DOWNGRADE);
3421 if (status)
3422 return status;
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003423 READ_BUF(NFS4_STATEID_SIZE);
3424 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 return 0;
3426}
3427
3428static int decode_putfh(struct xdr_stream *xdr)
3429{
3430 return decode_op_hdr(xdr, OP_PUTFH);
3431}
3432
3433static int decode_putrootfh(struct xdr_stream *xdr)
3434{
3435 return decode_op_hdr(xdr, OP_PUTROOTFH);
3436}
3437
3438static int decode_read(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs_readres *res)
3439{
3440 struct kvec *iov = req->rq_rcv_buf.head;
Al Viro8687b632006-10-19 23:28:48 -07003441 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 uint32_t count, eof, recvd, hdrlen;
3443 int status;
3444
3445 status = decode_op_hdr(xdr, OP_READ);
3446 if (status)
3447 return status;
3448 READ_BUF(8);
3449 READ32(eof);
3450 READ32(count);
3451 hdrlen = (u8 *) p - (u8 *) iov->iov_base;
3452 recvd = req->rq_rcv_buf.len - hdrlen;
3453 if (count > recvd) {
3454 printk(KERN_WARNING "NFS: server cheating in read reply: "
3455 "count %u > recvd %u\n", count, recvd);
3456 count = recvd;
3457 eof = 0;
3458 }
3459 xdr_read_pages(xdr, count);
3460 res->eof = eof;
3461 res->count = count;
3462 return 0;
3463}
3464
3465static int decode_readdir(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs4_readdir_res *readdir)
3466{
3467 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
3468 struct page *page = *rcvbuf->pages;
3469 struct kvec *iov = rcvbuf->head;
3470 unsigned int nr, pglen = rcvbuf->page_len;
Al Viro8687b632006-10-19 23:28:48 -07003471 __be32 *end, *entry, *p, *kaddr;
David Howellse8896492006-08-24 15:44:19 -04003472 uint32_t len, attrlen, xlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 int hdrlen, recvd, status;
3474
3475 status = decode_op_hdr(xdr, OP_READDIR);
3476 if (status)
3477 return status;
3478 READ_BUF(8);
3479 COPYMEM(readdir->verifier.data, 8);
Trond Myklebusteadf4592005-06-22 17:16:39 +00003480 dprintk("%s: verifier = 0x%x%x\n",
3481 __FUNCTION__,
3482 ((u32 *)readdir->verifier.data)[0],
3483 ((u32 *)readdir->verifier.data)[1]);
3484
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
3486 hdrlen = (char *) p - (char *) iov->iov_base;
3487 recvd = rcvbuf->len - hdrlen;
3488 if (pglen > recvd)
3489 pglen = recvd;
3490 xdr_read_pages(xdr, pglen);
3491
3492 BUG_ON(pglen + readdir->pgbase > PAGE_CACHE_SIZE);
Al Viro8687b632006-10-19 23:28:48 -07003493 kaddr = p = kmap_atomic(page, KM_USER0);
David Howellse8896492006-08-24 15:44:19 -04003494 end = p + ((pglen + readdir->pgbase) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 entry = p;
3496 for (nr = 0; *p++; nr++) {
David Howellse8896492006-08-24 15:44:19 -04003497 if (end - p < 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 goto short_pkt;
Trond Myklebusteadf4592005-06-22 17:16:39 +00003499 dprintk("cookie = %Lu, ", *((unsigned long long *)p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 p += 2; /* cookie */
3501 len = ntohl(*p++); /* filename length */
3502 if (len > NFS4_MAXNAMLEN) {
3503 printk(KERN_WARNING "NFS: giant filename in readdir (len 0x%x)\n", len);
3504 goto err_unmap;
3505 }
David Howellse8896492006-08-24 15:44:19 -04003506 xlen = XDR_QUADLEN(len);
3507 if (end - p < xlen + 1)
3508 goto short_pkt;
Trond Myklebusteadf4592005-06-22 17:16:39 +00003509 dprintk("filename = %*s\n", len, (char *)p);
David Howellse8896492006-08-24 15:44:19 -04003510 p += xlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 len = ntohl(*p++); /* bitmap length */
David Howellse8896492006-08-24 15:44:19 -04003512 if (end - p < len + 1)
3513 goto short_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 p += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 attrlen = XDR_QUADLEN(ntohl(*p++));
David Howellse8896492006-08-24 15:44:19 -04003516 if (end - p < attrlen + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 goto short_pkt;
David Howellse8896492006-08-24 15:44:19 -04003518 p += attrlen; /* attributes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 entry = p;
3520 }
3521 if (!nr && (entry[0] != 0 || entry[1] == 0))
3522 goto short_pkt;
3523out:
3524 kunmap_atomic(kaddr, KM_USER0);
3525 return 0;
3526short_pkt:
Trond Myklebusteadf4592005-06-22 17:16:39 +00003527 dprintk("%s: short packet at entry %d\n", __FUNCTION__, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 entry[0] = entry[1] = 0;
3529 /* truncate listing ? */
3530 if (!nr) {
3531 printk(KERN_NOTICE "NFS: readdir reply truncated!\n");
3532 entry[1] = 1;
3533 }
3534 goto out;
3535err_unmap:
3536 kunmap_atomic(kaddr, KM_USER0);
3537 return -errno_NFSERR_IO;
3538}
3539
3540static int decode_readlink(struct xdr_stream *xdr, struct rpc_rqst *req)
3541{
3542 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
3543 struct kvec *iov = rcvbuf->head;
3544 int hdrlen, len, recvd;
Al Viro8687b632006-10-19 23:28:48 -07003545 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 char *kaddr;
3547 int status;
3548
3549 status = decode_op_hdr(xdr, OP_READLINK);
3550 if (status)
3551 return status;
3552
3553 /* Convert length of symlink */
3554 READ_BUF(4);
3555 READ32(len);
3556 if (len >= rcvbuf->page_len || len <= 0) {
3557 dprintk(KERN_WARNING "nfs: server returned giant symlink!\n");
3558 return -ENAMETOOLONG;
3559 }
3560 hdrlen = (char *) xdr->p - (char *) iov->iov_base;
3561 recvd = req->rq_rcv_buf.len - hdrlen;
3562 if (recvd < len) {
3563 printk(KERN_WARNING "NFS: server cheating in readlink reply: "
3564 "count %u > recvd %u\n", len, recvd);
3565 return -EIO;
3566 }
3567 xdr_read_pages(xdr, len);
3568 /*
3569 * The XDR encode routine has set things up so that
3570 * the link text will be copied directly into the
3571 * buffer. We just have to do overflow-checking,
3572 * and and null-terminate the text (the VFS expects
3573 * null-termination).
3574 */
3575 kaddr = (char *)kmap_atomic(rcvbuf->pages[0], KM_USER0);
3576 kaddr[len+rcvbuf->page_base] = '\0';
3577 kunmap_atomic(kaddr, KM_USER0);
3578 return 0;
3579}
3580
3581static int decode_remove(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
3582{
3583 int status;
3584
3585 status = decode_op_hdr(xdr, OP_REMOVE);
3586 if (status)
3587 goto out;
3588 status = decode_change_info(xdr, cinfo);
3589out:
3590 return status;
3591}
3592
3593static int decode_rename(struct xdr_stream *xdr, struct nfs4_change_info *old_cinfo,
3594 struct nfs4_change_info *new_cinfo)
3595{
3596 int status;
3597
3598 status = decode_op_hdr(xdr, OP_RENAME);
3599 if (status)
3600 goto out;
3601 if ((status = decode_change_info(xdr, old_cinfo)))
3602 goto out;
3603 status = decode_change_info(xdr, new_cinfo);
3604out:
3605 return status;
3606}
3607
3608static int decode_renew(struct xdr_stream *xdr)
3609{
3610 return decode_op_hdr(xdr, OP_RENEW);
3611}
3612
Trond Myklebust56ae19f2005-10-27 22:12:40 -04003613static int
3614decode_restorefh(struct xdr_stream *xdr)
3615{
3616 return decode_op_hdr(xdr, OP_RESTOREFH);
3617}
3618
J. Bruce Fields029d1052005-06-22 17:16:22 +00003619static int decode_getacl(struct xdr_stream *xdr, struct rpc_rqst *req,
3620 size_t *acl_len)
3621{
Al Viro8687b632006-10-19 23:28:48 -07003622 __be32 *savep;
J. Bruce Fields029d1052005-06-22 17:16:22 +00003623 uint32_t attrlen,
3624 bitmap[2] = {0};
3625 struct kvec *iov = req->rq_rcv_buf.head;
3626 int status;
3627
3628 *acl_len = 0;
3629 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3630 goto out;
3631 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3632 goto out;
3633 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3634 goto out;
3635
3636 if (unlikely(bitmap[0] & (FATTR4_WORD0_ACL - 1U)))
3637 return -EIO;
3638 if (likely(bitmap[0] & FATTR4_WORD0_ACL)) {
3639 int hdrlen, recvd;
3640
3641 /* We ignore &savep and don't do consistency checks on
3642 * the attr length. Let userspace figure it out.... */
3643 hdrlen = (u8 *)xdr->p - (u8 *)iov->iov_base;
3644 recvd = req->rq_rcv_buf.len - hdrlen;
3645 if (attrlen > recvd) {
3646 printk(KERN_WARNING "NFS: server cheating in getattr"
3647 " acl reply: attrlen %u > recvd %u\n",
3648 attrlen, recvd);
3649 return -EINVAL;
3650 }
J. Bruce Fieldsc04871e2006-05-30 16:28:58 -04003651 xdr_read_pages(xdr, attrlen);
J. Bruce Fields029d1052005-06-22 17:16:22 +00003652 *acl_len = attrlen;
J. Bruce Fields8c233cf2005-10-13 16:54:27 -04003653 } else
3654 status = -EOPNOTSUPP;
J. Bruce Fields029d1052005-06-22 17:16:22 +00003655
3656out:
3657 return status;
3658}
3659
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660static int
3661decode_savefh(struct xdr_stream *xdr)
3662{
3663 return decode_op_hdr(xdr, OP_SAVEFH);
3664}
3665
3666static int decode_setattr(struct xdr_stream *xdr, struct nfs_setattrres *res)
3667{
Al Viro8687b632006-10-19 23:28:48 -07003668 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 uint32_t bmlen;
3670 int status;
3671
3672
3673 status = decode_op_hdr(xdr, OP_SETATTR);
3674 if (status)
3675 return status;
3676 READ_BUF(4);
3677 READ32(bmlen);
3678 READ_BUF(bmlen << 2);
3679 return 0;
3680}
3681
David Howellsadfa6f92006-08-22 20:06:08 -04003682static int decode_setclientid(struct xdr_stream *xdr, struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683{
Al Viro8687b632006-10-19 23:28:48 -07003684 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 uint32_t opnum;
3686 int32_t nfserr;
3687
3688 READ_BUF(8);
3689 READ32(opnum);
3690 if (opnum != OP_SETCLIENTID) {
3691 printk(KERN_NOTICE
3692 "nfs4_decode_setclientid: Server returned operation"
3693 " %d\n", opnum);
3694 return -EIO;
3695 }
3696 READ32(nfserr);
3697 if (nfserr == NFS_OK) {
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003698 READ_BUF(8 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 READ64(clp->cl_clientid);
Trond Myklebust8ae20ab2007-05-14 16:50:45 -04003700 COPYMEM(clp->cl_confirm.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 } else if (nfserr == NFSERR_CLID_INUSE) {
3702 uint32_t len;
3703
3704 /* skip netid string */
3705 READ_BUF(4);
3706 READ32(len);
3707 READ_BUF(len);
3708
3709 /* skip uaddr string */
3710 READ_BUF(4);
3711 READ32(len);
3712 READ_BUF(len);
3713 return -NFSERR_CLID_INUSE;
3714 } else
David Howells0a8ea432006-08-22 20:06:08 -04003715 return -nfs4_stat_to_errno(nfserr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
3717 return 0;
3718}
3719
3720static int decode_setclientid_confirm(struct xdr_stream *xdr)
3721{
3722 return decode_op_hdr(xdr, OP_SETCLIENTID_CONFIRM);
3723}
3724
3725static int decode_write(struct xdr_stream *xdr, struct nfs_writeres *res)
3726{
Al Viro8687b632006-10-19 23:28:48 -07003727 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 int status;
3729
3730 status = decode_op_hdr(xdr, OP_WRITE);
3731 if (status)
3732 return status;
3733
3734 READ_BUF(16);
3735 READ32(res->count);
3736 READ32(res->verf->committed);
3737 COPYMEM(res->verf->verifier, 8);
3738 return 0;
3739}
3740
3741static int decode_delegreturn(struct xdr_stream *xdr)
3742{
3743 return decode_op_hdr(xdr, OP_DELEGRETURN);
3744}
3745
3746/*
3747 * Decode OPEN_DOWNGRADE response
3748 */
Al Viro8687b632006-10-19 23:28:48 -07003749static int nfs4_xdr_dec_open_downgrade(struct rpc_rqst *rqstp, __be32 *p, struct nfs_closeres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750{
3751 struct xdr_stream xdr;
3752 struct compound_hdr hdr;
3753 int status;
3754
3755 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3756 status = decode_compound_hdr(&xdr, &hdr);
3757 if (status)
3758 goto out;
3759 status = decode_putfh(&xdr);
3760 if (status)
3761 goto out;
3762 status = decode_open_downgrade(&xdr, res);
Trond Myklebust516a6af2005-10-27 22:12:41 -04003763 if (status != 0)
3764 goto out;
3765 decode_getfattr(&xdr, res->fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766out:
3767 return status;
3768}
3769
3770/*
3771 * END OF "GENERIC" DECODE ROUTINES.
3772 */
3773
3774/*
3775 * Decode ACCESS response
3776 */
Al Viro8687b632006-10-19 23:28:48 -07003777static int nfs4_xdr_dec_access(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_accessres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778{
3779 struct xdr_stream xdr;
3780 struct compound_hdr hdr;
3781 int status;
3782
3783 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3784 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3785 goto out;
3786 if ((status = decode_putfh(&xdr)) == 0)
3787 status = decode_access(&xdr, res);
3788out:
3789 return status;
3790}
3791
3792/*
3793 * Decode LOOKUP response
3794 */
Al Viro8687b632006-10-19 23:28:48 -07003795static int nfs4_xdr_dec_lookup(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_lookup_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796{
3797 struct xdr_stream xdr;
3798 struct compound_hdr hdr;
3799 int status;
3800
3801 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3802 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3803 goto out;
3804 if ((status = decode_putfh(&xdr)) != 0)
3805 goto out;
3806 if ((status = decode_lookup(&xdr)) != 0)
3807 goto out;
3808 if ((status = decode_getfh(&xdr, res->fh)) != 0)
3809 goto out;
3810 status = decode_getfattr(&xdr, res->fattr, res->server);
3811out:
3812 return status;
3813}
3814
3815/*
3816 * Decode LOOKUP_ROOT response
3817 */
Al Viro8687b632006-10-19 23:28:48 -07003818static int nfs4_xdr_dec_lookup_root(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_lookup_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819{
3820 struct xdr_stream xdr;
3821 struct compound_hdr hdr;
3822 int status;
3823
3824 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3825 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3826 goto out;
3827 if ((status = decode_putrootfh(&xdr)) != 0)
3828 goto out;
3829 if ((status = decode_getfh(&xdr, res->fh)) == 0)
3830 status = decode_getfattr(&xdr, res->fattr, res->server);
3831out:
3832 return status;
3833}
3834
3835/*
3836 * Decode REMOVE response
3837 */
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003838static int nfs4_xdr_dec_remove(struct rpc_rqst *rqstp, __be32 *p, struct nfs_removeres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839{
3840 struct xdr_stream xdr;
3841 struct compound_hdr hdr;
3842 int status;
3843
3844 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3845 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3846 goto out;
Trond Myklebust16e42952005-10-27 22:12:44 -04003847 if ((status = decode_putfh(&xdr)) != 0)
3848 goto out;
3849 if ((status = decode_remove(&xdr, &res->cinfo)) != 0)
3850 goto out;
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003851 decode_getfattr(&xdr, &res->dir_attr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852out:
3853 return status;
3854}
3855
3856/*
3857 * Decode RENAME response
3858 */
Al Viro8687b632006-10-19 23:28:48 -07003859static int nfs4_xdr_dec_rename(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_rename_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860{
3861 struct xdr_stream xdr;
3862 struct compound_hdr hdr;
3863 int status;
3864
3865 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3866 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3867 goto out;
3868 if ((status = decode_putfh(&xdr)) != 0)
3869 goto out;
3870 if ((status = decode_savefh(&xdr)) != 0)
3871 goto out;
3872 if ((status = decode_putfh(&xdr)) != 0)
3873 goto out;
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003874 if ((status = decode_rename(&xdr, &res->old_cinfo, &res->new_cinfo)) != 0)
3875 goto out;
3876 /* Current FH is target directory */
3877 if (decode_getfattr(&xdr, res->new_fattr, res->server) != 0)
3878 goto out;
3879 if ((status = decode_restorefh(&xdr)) != 0)
3880 goto out;
3881 decode_getfattr(&xdr, res->old_fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882out:
3883 return status;
3884}
3885
3886/*
3887 * Decode LINK response
3888 */
Al Viro8687b632006-10-19 23:28:48 -07003889static int nfs4_xdr_dec_link(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_link_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890{
3891 struct xdr_stream xdr;
3892 struct compound_hdr hdr;
3893 int status;
3894
3895 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3896 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3897 goto out;
3898 if ((status = decode_putfh(&xdr)) != 0)
3899 goto out;
3900 if ((status = decode_savefh(&xdr)) != 0)
3901 goto out;
3902 if ((status = decode_putfh(&xdr)) != 0)
3903 goto out;
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003904 if ((status = decode_link(&xdr, &res->cinfo)) != 0)
3905 goto out;
3906 /*
3907 * Note order: OP_LINK leaves the directory as the current
3908 * filehandle.
3909 */
3910 if (decode_getfattr(&xdr, res->dir_attr, res->server) != 0)
3911 goto out;
3912 if ((status = decode_restorefh(&xdr)) != 0)
3913 goto out;
3914 decode_getfattr(&xdr, res->fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915out:
3916 return status;
3917}
3918
3919/*
3920 * Decode CREATE response
3921 */
Al Viro8687b632006-10-19 23:28:48 -07003922static int nfs4_xdr_dec_create(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_create_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923{
3924 struct xdr_stream xdr;
3925 struct compound_hdr hdr;
3926 int status;
3927
3928 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3929 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3930 goto out;
3931 if ((status = decode_putfh(&xdr)) != 0)
3932 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04003933 if ((status = decode_savefh(&xdr)) != 0)
3934 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 if ((status = decode_create(&xdr,&res->dir_cinfo)) != 0)
3936 goto out;
3937 if ((status = decode_getfh(&xdr, res->fh)) != 0)
3938 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04003939 if (decode_getfattr(&xdr, res->fattr, res->server) != 0)
3940 goto out;
3941 if ((status = decode_restorefh(&xdr)) != 0)
3942 goto out;
3943 decode_getfattr(&xdr, res->dir_fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944out:
3945 return status;
3946}
3947
3948/*
3949 * Decode SYMLINK response
3950 */
Al Viro8687b632006-10-19 23:28:48 -07003951static int nfs4_xdr_dec_symlink(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_create_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952{
3953 return nfs4_xdr_dec_create(rqstp, p, res);
3954}
3955
3956/*
3957 * Decode GETATTR response
3958 */
Al Viro8687b632006-10-19 23:28:48 -07003959static int nfs4_xdr_dec_getattr(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_getattr_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960{
3961 struct xdr_stream xdr;
3962 struct compound_hdr hdr;
3963 int status;
3964
3965 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3966 status = decode_compound_hdr(&xdr, &hdr);
3967 if (status)
3968 goto out;
3969 status = decode_putfh(&xdr);
3970 if (status)
3971 goto out;
3972 status = decode_getfattr(&xdr, res->fattr, res->server);
3973out:
3974 return status;
3975
3976}
3977
J. Bruce Fields23ec6962005-06-22 17:16:22 +00003978/*
3979 * Encode an SETACL request
3980 */
3981static int
Al Viro8687b632006-10-19 23:28:48 -07003982nfs4_xdr_enc_setacl(struct rpc_rqst *req, __be32 *p, struct nfs_setaclargs *args)
J. Bruce Fields23ec6962005-06-22 17:16:22 +00003983{
3984 struct xdr_stream xdr;
3985 struct compound_hdr hdr = {
3986 .nops = 2,
3987 };
3988 int status;
3989
3990 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
3991 encode_compound_hdr(&xdr, &hdr);
3992 status = encode_putfh(&xdr, args->fh);
3993 if (status)
3994 goto out;
3995 status = encode_setacl(&xdr, args);
3996out:
3997 return status;
3998}
3999/*
4000 * Decode SETACL response
4001 */
4002static int
Al Viro8687b632006-10-19 23:28:48 -07004003nfs4_xdr_dec_setacl(struct rpc_rqst *rqstp, __be32 *p, void *res)
J. Bruce Fields23ec6962005-06-22 17:16:22 +00004004{
4005 struct xdr_stream xdr;
4006 struct compound_hdr hdr;
4007 int status;
4008
4009 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4010 status = decode_compound_hdr(&xdr, &hdr);
4011 if (status)
4012 goto out;
4013 status = decode_putfh(&xdr);
4014 if (status)
4015 goto out;
4016 status = decode_setattr(&xdr, res);
4017out:
4018 return status;
4019}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
4021/*
J. Bruce Fields029d1052005-06-22 17:16:22 +00004022 * Decode GETACL response
4023 */
4024static int
Al Viro8687b632006-10-19 23:28:48 -07004025nfs4_xdr_dec_getacl(struct rpc_rqst *rqstp, __be32 *p, size_t *acl_len)
J. Bruce Fields029d1052005-06-22 17:16:22 +00004026{
4027 struct xdr_stream xdr;
4028 struct compound_hdr hdr;
4029 int status;
4030
4031 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4032 status = decode_compound_hdr(&xdr, &hdr);
4033 if (status)
4034 goto out;
4035 status = decode_putfh(&xdr);
4036 if (status)
4037 goto out;
4038 status = decode_getacl(&xdr, rqstp, acl_len);
4039
4040out:
4041 return status;
4042}
4043
4044/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 * Decode CLOSE response
4046 */
Al Viro8687b632006-10-19 23:28:48 -07004047static int nfs4_xdr_dec_close(struct rpc_rqst *rqstp, __be32 *p, struct nfs_closeres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048{
4049 struct xdr_stream xdr;
4050 struct compound_hdr hdr;
4051 int status;
4052
4053 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4054 status = decode_compound_hdr(&xdr, &hdr);
4055 if (status)
4056 goto out;
4057 status = decode_putfh(&xdr);
4058 if (status)
4059 goto out;
4060 status = decode_close(&xdr, res);
Trond Myklebust516a6af2005-10-27 22:12:41 -04004061 if (status != 0)
4062 goto out;
4063 /*
4064 * Note: Server may do delete on close for this file
4065 * in which case the getattr call will fail with
4066 * an ESTALE error. Shouldn't be a problem,
4067 * though, since fattr->valid will remain unset.
4068 */
4069 decode_getfattr(&xdr, res->fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070out:
4071 return status;
4072}
4073
4074/*
4075 * Decode OPEN response
4076 */
Al Viro8687b632006-10-19 23:28:48 -07004077static int nfs4_xdr_dec_open(struct rpc_rqst *rqstp, __be32 *p, struct nfs_openres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078{
4079 struct xdr_stream xdr;
4080 struct compound_hdr hdr;
4081 int status;
4082
4083 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4084 status = decode_compound_hdr(&xdr, &hdr);
4085 if (status)
4086 goto out;
4087 status = decode_putfh(&xdr);
4088 if (status)
4089 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04004090 status = decode_savefh(&xdr);
4091 if (status)
4092 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 status = decode_open(&xdr, res);
4094 if (status)
4095 goto out;
Trond Myklebust99367812007-07-17 21:52:41 -04004096 if (decode_getfh(&xdr, &res->fh) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 goto out;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04004098 if (decode_getfattr(&xdr, res->f_attr, res->server) != 0)
4099 goto out;
Trond Myklebust365c8f52007-07-17 21:52:37 -04004100 if (decode_restorefh(&xdr) != 0)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04004101 goto out;
4102 decode_getfattr(&xdr, res->dir_attr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103out:
4104 return status;
4105}
4106
4107/*
4108 * Decode OPEN_CONFIRM response
4109 */
Al Viro8687b632006-10-19 23:28:48 -07004110static int nfs4_xdr_dec_open_confirm(struct rpc_rqst *rqstp, __be32 *p, struct nfs_open_confirmres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111{
4112 struct xdr_stream xdr;
4113 struct compound_hdr hdr;
4114 int status;
4115
4116 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4117 status = decode_compound_hdr(&xdr, &hdr);
4118 if (status)
4119 goto out;
4120 status = decode_putfh(&xdr);
4121 if (status)
4122 goto out;
4123 status = decode_open_confirm(&xdr, res);
4124out:
4125 return status;
4126}
4127
4128/*
4129 * Decode OPEN response
4130 */
Al Viro8687b632006-10-19 23:28:48 -07004131static int nfs4_xdr_dec_open_noattr(struct rpc_rqst *rqstp, __be32 *p, struct nfs_openres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132{
4133 struct xdr_stream xdr;
4134 struct compound_hdr hdr;
4135 int status;
4136
4137 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4138 status = decode_compound_hdr(&xdr, &hdr);
4139 if (status)
4140 goto out;
4141 status = decode_putfh(&xdr);
4142 if (status)
4143 goto out;
4144 status = decode_open(&xdr, res);
Trond Myklebust864472e2006-01-03 09:55:15 +01004145 if (status)
4146 goto out;
4147 decode_getfattr(&xdr, res->f_attr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148out:
4149 return status;
4150}
4151
4152/*
4153 * Decode SETATTR response
4154 */
Al Viro8687b632006-10-19 23:28:48 -07004155static int nfs4_xdr_dec_setattr(struct rpc_rqst *rqstp, __be32 *p, struct nfs_setattrres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156{
4157 struct xdr_stream xdr;
4158 struct compound_hdr hdr;
4159 int status;
4160
4161 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4162 status = decode_compound_hdr(&xdr, &hdr);
4163 if (status)
4164 goto out;
4165 status = decode_putfh(&xdr);
4166 if (status)
4167 goto out;
4168 status = decode_setattr(&xdr, res);
4169 if (status)
4170 goto out;
4171 status = decode_getfattr(&xdr, res->fattr, res->server);
4172 if (status == NFS4ERR_DELAY)
4173 status = 0;
4174out:
4175 return status;
4176}
4177
4178/*
4179 * Decode LOCK response
4180 */
Al Viro8687b632006-10-19 23:28:48 -07004181static int nfs4_xdr_dec_lock(struct rpc_rqst *rqstp, __be32 *p, struct nfs_lock_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182{
4183 struct xdr_stream xdr;
4184 struct compound_hdr hdr;
4185 int status;
4186
4187 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4188 status = decode_compound_hdr(&xdr, &hdr);
4189 if (status)
4190 goto out;
4191 status = decode_putfh(&xdr);
4192 if (status)
4193 goto out;
4194 status = decode_lock(&xdr, res);
4195out:
4196 return status;
4197}
4198
4199/*
4200 * Decode LOCKT response
4201 */
Al Viro8687b632006-10-19 23:28:48 -07004202static int nfs4_xdr_dec_lockt(struct rpc_rqst *rqstp, __be32 *p, struct nfs_lockt_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203{
4204 struct xdr_stream xdr;
4205 struct compound_hdr hdr;
4206 int status;
4207
4208 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4209 status = decode_compound_hdr(&xdr, &hdr);
4210 if (status)
4211 goto out;
4212 status = decode_putfh(&xdr);
4213 if (status)
4214 goto out;
4215 status = decode_lockt(&xdr, res);
4216out:
4217 return status;
4218}
4219
4220/*
4221 * Decode LOCKU response
4222 */
Al Viro8687b632006-10-19 23:28:48 -07004223static int nfs4_xdr_dec_locku(struct rpc_rqst *rqstp, __be32 *p, struct nfs_locku_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224{
4225 struct xdr_stream xdr;
4226 struct compound_hdr hdr;
4227 int status;
4228
4229 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4230 status = decode_compound_hdr(&xdr, &hdr);
4231 if (status)
4232 goto out;
4233 status = decode_putfh(&xdr);
4234 if (status)
4235 goto out;
4236 status = decode_locku(&xdr, res);
4237out:
4238 return status;
4239}
4240
4241/*
4242 * Decode READLINK response
4243 */
Al Viro8687b632006-10-19 23:28:48 -07004244static int nfs4_xdr_dec_readlink(struct rpc_rqst *rqstp, __be32 *p, void *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245{
4246 struct xdr_stream xdr;
4247 struct compound_hdr hdr;
4248 int status;
4249
4250 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4251 status = decode_compound_hdr(&xdr, &hdr);
4252 if (status)
4253 goto out;
4254 status = decode_putfh(&xdr);
4255 if (status)
4256 goto out;
4257 status = decode_readlink(&xdr, rqstp);
4258out:
4259 return status;
4260}
4261
4262/*
4263 * Decode READDIR response
4264 */
Al Viro8687b632006-10-19 23:28:48 -07004265static int nfs4_xdr_dec_readdir(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_readdir_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266{
4267 struct xdr_stream xdr;
4268 struct compound_hdr hdr;
4269 int status;
4270
4271 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4272 status = decode_compound_hdr(&xdr, &hdr);
4273 if (status)
4274 goto out;
4275 status = decode_putfh(&xdr);
4276 if (status)
4277 goto out;
4278 status = decode_readdir(&xdr, rqstp, res);
4279out:
4280 return status;
4281}
4282
4283/*
4284 * Decode Read response
4285 */
Al Viro8687b632006-10-19 23:28:48 -07004286static int nfs4_xdr_dec_read(struct rpc_rqst *rqstp, __be32 *p, struct nfs_readres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287{
4288 struct xdr_stream xdr;
4289 struct compound_hdr hdr;
4290 int status;
4291
4292 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4293 status = decode_compound_hdr(&xdr, &hdr);
4294 if (status)
4295 goto out;
4296 status = decode_putfh(&xdr);
4297 if (status)
4298 goto out;
4299 status = decode_read(&xdr, rqstp, res);
4300 if (!status)
4301 status = res->count;
4302out:
4303 return status;
4304}
4305
4306/*
4307 * Decode WRITE response
4308 */
Al Viro8687b632006-10-19 23:28:48 -07004309static int nfs4_xdr_dec_write(struct rpc_rqst *rqstp, __be32 *p, struct nfs_writeres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310{
4311 struct xdr_stream xdr;
4312 struct compound_hdr hdr;
4313 int status;
4314
4315 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4316 status = decode_compound_hdr(&xdr, &hdr);
4317 if (status)
4318 goto out;
4319 status = decode_putfh(&xdr);
4320 if (status)
4321 goto out;
4322 status = decode_write(&xdr, res);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004323 if (status)
4324 goto out;
4325 decode_getfattr(&xdr, res->fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 if (!status)
4327 status = res->count;
4328out:
4329 return status;
4330}
4331
4332/*
4333 * Decode COMMIT response
4334 */
Al Viro8687b632006-10-19 23:28:48 -07004335static int nfs4_xdr_dec_commit(struct rpc_rqst *rqstp, __be32 *p, struct nfs_writeres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336{
4337 struct xdr_stream xdr;
4338 struct compound_hdr hdr;
4339 int status;
4340
4341 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4342 status = decode_compound_hdr(&xdr, &hdr);
4343 if (status)
4344 goto out;
4345 status = decode_putfh(&xdr);
4346 if (status)
4347 goto out;
4348 status = decode_commit(&xdr, res);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004349 if (status)
4350 goto out;
4351 decode_getfattr(&xdr, res->fattr, res->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352out:
4353 return status;
4354}
4355
4356/*
4357 * FSINFO request
4358 */
Al Viro8687b632006-10-19 23:28:48 -07004359static int nfs4_xdr_dec_fsinfo(struct rpc_rqst *req, __be32 *p, struct nfs_fsinfo *fsinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360{
4361 struct xdr_stream xdr;
4362 struct compound_hdr hdr;
4363 int status;
4364
4365 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4366 status = decode_compound_hdr(&xdr, &hdr);
4367 if (!status)
4368 status = decode_putfh(&xdr);
4369 if (!status)
4370 status = decode_fsinfo(&xdr, fsinfo);
4371 if (!status)
David Howells0a8ea432006-08-22 20:06:08 -04004372 status = -nfs4_stat_to_errno(hdr.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 return status;
4374}
4375
4376/*
4377 * PATHCONF request
4378 */
Al Viro8687b632006-10-19 23:28:48 -07004379static int nfs4_xdr_dec_pathconf(struct rpc_rqst *req, __be32 *p, struct nfs_pathconf *pathconf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380{
4381 struct xdr_stream xdr;
4382 struct compound_hdr hdr;
4383 int status;
4384
4385 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4386 status = decode_compound_hdr(&xdr, &hdr);
4387 if (!status)
4388 status = decode_putfh(&xdr);
4389 if (!status)
4390 status = decode_pathconf(&xdr, pathconf);
4391 return status;
4392}
4393
4394/*
4395 * STATFS request
4396 */
Al Viro8687b632006-10-19 23:28:48 -07004397static int nfs4_xdr_dec_statfs(struct rpc_rqst *req, __be32 *p, struct nfs_fsstat *fsstat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398{
4399 struct xdr_stream xdr;
4400 struct compound_hdr hdr;
4401 int status;
4402
4403 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4404 status = decode_compound_hdr(&xdr, &hdr);
4405 if (!status)
4406 status = decode_putfh(&xdr);
4407 if (!status)
4408 status = decode_statfs(&xdr, fsstat);
4409 return status;
4410}
4411
4412/*
4413 * GETATTR_BITMAP request
4414 */
Al Viro8687b632006-10-19 23:28:48 -07004415static 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 -07004416{
4417 struct xdr_stream xdr;
4418 struct compound_hdr hdr;
4419 int status;
4420
4421 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4422 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
4423 goto out;
4424 if ((status = decode_putfh(&xdr)) != 0)
4425 goto out;
4426 status = decode_server_caps(&xdr, res);
4427out:
4428 return status;
4429}
4430
4431/*
4432 * Decode RENEW response
4433 */
Al Viro8687b632006-10-19 23:28:48 -07004434static int nfs4_xdr_dec_renew(struct rpc_rqst *rqstp, __be32 *p, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435{
4436 struct xdr_stream xdr;
4437 struct compound_hdr hdr;
4438 int status;
4439
4440 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4441 status = decode_compound_hdr(&xdr, &hdr);
4442 if (!status)
4443 status = decode_renew(&xdr);
4444 return status;
4445}
4446
4447/*
4448 * a SETCLIENTID request
4449 */
Al Viro8687b632006-10-19 23:28:48 -07004450static int nfs4_xdr_dec_setclientid(struct rpc_rqst *req, __be32 *p,
David Howellsadfa6f92006-08-22 20:06:08 -04004451 struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452{
4453 struct xdr_stream xdr;
4454 struct compound_hdr hdr;
4455 int status;
4456
4457 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4458 status = decode_compound_hdr(&xdr, &hdr);
4459 if (!status)
4460 status = decode_setclientid(&xdr, clp);
4461 if (!status)
David Howells0a8ea432006-08-22 20:06:08 -04004462 status = -nfs4_stat_to_errno(hdr.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 return status;
4464}
4465
4466/*
4467 * a SETCLIENTID_CONFIRM request
4468 */
Al Viro8687b632006-10-19 23:28:48 -07004469static int nfs4_xdr_dec_setclientid_confirm(struct rpc_rqst *req, __be32 *p, struct nfs_fsinfo *fsinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470{
4471 struct xdr_stream xdr;
4472 struct compound_hdr hdr;
4473 int status;
4474
4475 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4476 status = decode_compound_hdr(&xdr, &hdr);
4477 if (!status)
4478 status = decode_setclientid_confirm(&xdr);
4479 if (!status)
4480 status = decode_putrootfh(&xdr);
4481 if (!status)
4482 status = decode_fsinfo(&xdr, fsinfo);
4483 if (!status)
David Howells0a8ea432006-08-22 20:06:08 -04004484 status = -nfs4_stat_to_errno(hdr.status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 return status;
4486}
4487
4488/*
4489 * DELEGRETURN request
4490 */
Al Viro8687b632006-10-19 23:28:48 -07004491static int nfs4_xdr_dec_delegreturn(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_delegreturnres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492{
4493 struct xdr_stream xdr;
4494 struct compound_hdr hdr;
4495 int status;
4496
4497 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4498 status = decode_compound_hdr(&xdr, &hdr);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004499 if (status != 0)
4500 goto out;
4501 status = decode_putfh(&xdr);
4502 if (status != 0)
4503 goto out;
4504 status = decode_delegreturn(&xdr);
4505 decode_getfattr(&xdr, res->fattr, res->server);
4506out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 return status;
4508}
4509
Trond Myklebust683b57b2006-06-09 09:34:22 -04004510/*
4511 * FS_LOCATIONS request
4512 */
Al Viro8687b632006-10-19 23:28:48 -07004513static int nfs4_xdr_dec_fs_locations(struct rpc_rqst *req, __be32 *p, struct nfs4_fs_locations *res)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004514{
4515 struct xdr_stream xdr;
4516 struct compound_hdr hdr;
4517 int status;
4518
4519 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4520 status = decode_compound_hdr(&xdr, &hdr);
4521 if (status != 0)
4522 goto out;
4523 if ((status = decode_putfh(&xdr)) != 0)
4524 goto out;
4525 if ((status = decode_lookup(&xdr)) != 0)
4526 goto out;
4527 xdr_enter_page(&xdr, PAGE_SIZE);
4528 status = decode_getfattr(&xdr, &res->fattr, res->server);
4529out:
4530 return status;
4531}
4532
Al Viro0dbb4c62006-10-19 23:28:49 -07004533__be32 *nfs4_decode_dirent(__be32 *p, struct nfs_entry *entry, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534{
4535 uint32_t bitmap[2] = {0};
4536 uint32_t len;
4537
4538 if (!*p++) {
4539 if (!*p)
4540 return ERR_PTR(-EAGAIN);
4541 entry->eof = 1;
4542 return ERR_PTR(-EBADCOOKIE);
4543 }
4544
4545 entry->prev_cookie = entry->cookie;
4546 p = xdr_decode_hyper(p, &entry->cookie);
4547 entry->len = ntohl(*p++);
4548 entry->name = (const char *) p;
4549 p += XDR_QUADLEN(entry->len);
4550
4551 /*
4552 * In case the server doesn't return an inode number,
4553 * we fake one here. (We don't use inode number 0,
4554 * since glibc seems to choke on it...)
4555 */
4556 entry->ino = 1;
4557
4558 len = ntohl(*p++); /* bitmap length */
4559 if (len-- > 0) {
4560 bitmap[0] = ntohl(*p++);
4561 if (len-- > 0) {
4562 bitmap[1] = ntohl(*p++);
4563 p += len;
4564 }
4565 }
4566 len = XDR_QUADLEN(ntohl(*p++)); /* attribute buffer length */
4567 if (len > 0) {
Manoj Naik97d312d2005-06-22 17:16:39 +00004568 if (bitmap[0] & FATTR4_WORD0_RDATTR_ERROR) {
4569 bitmap[0] &= ~FATTR4_WORD0_RDATTR_ERROR;
4570 /* Ignore the return value of rdattr_error for now */
4571 p++;
4572 len--;
4573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 if (bitmap[0] == 0 && bitmap[1] == FATTR4_WORD1_MOUNTED_ON_FILEID)
4575 xdr_decode_hyper(p, &entry->ino);
4576 else if (bitmap[0] == FATTR4_WORD0_FILEID)
4577 xdr_decode_hyper(p, &entry->ino);
4578 p += len;
4579 }
4580
4581 entry->eof = !p[0] && p[1];
4582 return p;
4583}
4584
4585/*
4586 * We need to translate between nfs status return values and
4587 * the local errno values which may not be the same.
4588 */
4589static struct {
4590 int stat;
4591 int errno;
4592} nfs_errtbl[] = {
4593 { NFS4_OK, 0 },
4594 { NFS4ERR_PERM, EPERM },
4595 { NFS4ERR_NOENT, ENOENT },
4596 { NFS4ERR_IO, errno_NFSERR_IO },
4597 { NFS4ERR_NXIO, ENXIO },
4598 { NFS4ERR_ACCESS, EACCES },
4599 { NFS4ERR_EXIST, EEXIST },
4600 { NFS4ERR_XDEV, EXDEV },
4601 { NFS4ERR_NOTDIR, ENOTDIR },
4602 { NFS4ERR_ISDIR, EISDIR },
4603 { NFS4ERR_INVAL, EINVAL },
4604 { NFS4ERR_FBIG, EFBIG },
4605 { NFS4ERR_NOSPC, ENOSPC },
4606 { NFS4ERR_ROFS, EROFS },
4607 { NFS4ERR_MLINK, EMLINK },
4608 { NFS4ERR_NAMETOOLONG, ENAMETOOLONG },
4609 { NFS4ERR_NOTEMPTY, ENOTEMPTY },
4610 { NFS4ERR_DQUOT, EDQUOT },
4611 { NFS4ERR_STALE, ESTALE },
4612 { NFS4ERR_BADHANDLE, EBADHANDLE },
Manoj Naik6ebf3652005-06-22 17:16:39 +00004613 { NFS4ERR_BADOWNER, EINVAL },
4614 { NFS4ERR_BADNAME, EINVAL },
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 { NFS4ERR_BAD_COOKIE, EBADCOOKIE },
4616 { NFS4ERR_NOTSUPP, ENOTSUPP },
4617 { NFS4ERR_TOOSMALL, ETOOSMALL },
4618 { NFS4ERR_SERVERFAULT, ESERVERFAULT },
4619 { NFS4ERR_BADTYPE, EBADTYPE },
4620 { NFS4ERR_LOCKED, EAGAIN },
4621 { NFS4ERR_RESOURCE, EREMOTEIO },
4622 { NFS4ERR_SYMLINK, ELOOP },
4623 { NFS4ERR_OP_ILLEGAL, EOPNOTSUPP },
4624 { NFS4ERR_DEADLOCK, EDEADLK },
4625 { NFS4ERR_WRONGSEC, EPERM }, /* FIXME: this needs
4626 * to be handled by a
4627 * middle-layer.
4628 */
4629 { -1, EIO }
4630};
4631
4632/*
4633 * Convert an NFS error code to a local one.
4634 * This one is used jointly by NFSv2 and NFSv3.
4635 */
4636static int
David Howells0a8ea432006-08-22 20:06:08 -04004637nfs4_stat_to_errno(int stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638{
4639 int i;
4640 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
4641 if (nfs_errtbl[i].stat == stat)
4642 return nfs_errtbl[i].errno;
4643 }
4644 if (stat <= 10000 || stat > 10100) {
4645 /* The server is looney tunes. */
4646 return ESERVERFAULT;
4647 }
4648 /* If we cannot translate the error, the recovery routines should
4649 * handle it.
4650 * Note: remaining NFSv4 error codes have values > 10000, so should
4651 * not conflict with native Linux error codes.
4652 */
4653 return stat;
4654}
4655
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656#define PROC(proc, argtype, restype) \
4657[NFSPROC4_CLNT_##proc] = { \
4658 .p_proc = NFSPROC4_COMPOUND, \
4659 .p_encode = (kxdrproc_t) nfs4_xdr_##argtype, \
4660 .p_decode = (kxdrproc_t) nfs4_xdr_##restype, \
Chuck Lever2bea90d2007-03-29 16:47:53 -04004661 .p_arglen = NFS4_##argtype##_sz, \
4662 .p_replen = NFS4_##restype##_sz, \
Chuck Levercc0175c2006-03-20 13:44:22 -05004663 .p_statidx = NFSPROC4_CLNT_##proc, \
4664 .p_name = #proc, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 }
4666
4667struct rpc_procinfo nfs4_procedures[] = {
4668 PROC(READ, enc_read, dec_read),
4669 PROC(WRITE, enc_write, dec_write),
4670 PROC(COMMIT, enc_commit, dec_commit),
4671 PROC(OPEN, enc_open, dec_open),
4672 PROC(OPEN_CONFIRM, enc_open_confirm, dec_open_confirm),
4673 PROC(OPEN_NOATTR, enc_open_noattr, dec_open_noattr),
4674 PROC(OPEN_DOWNGRADE, enc_open_downgrade, dec_open_downgrade),
4675 PROC(CLOSE, enc_close, dec_close),
4676 PROC(SETATTR, enc_setattr, dec_setattr),
4677 PROC(FSINFO, enc_fsinfo, dec_fsinfo),
4678 PROC(RENEW, enc_renew, dec_renew),
4679 PROC(SETCLIENTID, enc_setclientid, dec_setclientid),
4680 PROC(SETCLIENTID_CONFIRM, enc_setclientid_confirm, dec_setclientid_confirm),
4681 PROC(LOCK, enc_lock, dec_lock),
4682 PROC(LOCKT, enc_lockt, dec_lockt),
4683 PROC(LOCKU, enc_locku, dec_locku),
4684 PROC(ACCESS, enc_access, dec_access),
4685 PROC(GETATTR, enc_getattr, dec_getattr),
4686 PROC(LOOKUP, enc_lookup, dec_lookup),
4687 PROC(LOOKUP_ROOT, enc_lookup_root, dec_lookup_root),
4688 PROC(REMOVE, enc_remove, dec_remove),
4689 PROC(RENAME, enc_rename, dec_rename),
4690 PROC(LINK, enc_link, dec_link),
4691 PROC(SYMLINK, enc_symlink, dec_symlink),
4692 PROC(CREATE, enc_create, dec_create),
4693 PROC(PATHCONF, enc_pathconf, dec_pathconf),
4694 PROC(STATFS, enc_statfs, dec_statfs),
4695 PROC(READLINK, enc_readlink, dec_readlink),
4696 PROC(READDIR, enc_readdir, dec_readdir),
4697 PROC(SERVER_CAPS, enc_server_caps, dec_server_caps),
4698 PROC(DELEGRETURN, enc_delegreturn, dec_delegreturn),
J. Bruce Fields029d1052005-06-22 17:16:22 +00004699 PROC(GETACL, enc_getacl, dec_getacl),
J. Bruce Fields23ec6962005-06-22 17:16:22 +00004700 PROC(SETACL, enc_setacl, dec_setacl),
Trond Myklebust683b57b2006-06-09 09:34:22 -04004701 PROC(FS_LOCATIONS, enc_fs_locations, dec_fs_locations),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702};
4703
4704struct rpc_version nfs_version4 = {
4705 .number = 4,
Tobias Klausere8c96f82006-03-24 03:15:34 -08004706 .nrprocs = ARRAY_SIZE(nfs4_procedures),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 .procs = nfs4_procedures
4708};
4709
4710/*
4711 * Local variables:
4712 * c-basic-offset: 8
4713 * End:
4714 */