]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - fs/nfs/nfs4xdr.c
nfs41: sessions client infrastructure
[mirror_ubuntu-hirsute-kernel.git] / fs / nfs / nfs4xdr.c
CommitLineData
1da177e4
LT
1/*
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>
6c0195a4 11 *
1da177e4
LT
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>
4ce79717 54#include "nfs4_fs.h"
1da177e4
LT
55
56#define NFSDBG_FACILITY NFSDBG_XDR
57
58/* Mapping from NFS error code to "errno" error code. */
59#define errno_NFSERR_IO EIO
60
0a8ea437 61static int nfs4_stat_to_errno(int);
1da177e4
LT
62
63/* NFSv4 COMPOUND tags are only wanted for debugging purposes */
64#ifdef DEBUG
65#define NFS4_MAXTAGLEN 20
66#else
67#define NFS4_MAXTAGLEN 0
68#endif
69
6c0195a4 70/* lock,open owner id:
9f958ab8 71 * we currently use size 2 (u64) out of (NFS4_OPAQUE_LIMIT >> 2)
1da177e4 72 */
9f958ab8
TM
73#define open_owner_id_maxsz (1 + 4)
74#define lock_owner_id_maxsz (1 + 4)
9104a55d 75#define decode_lockowner_maxsz (1 + XDR_QUADLEN(IDMAP_NAMESZ))
1da177e4
LT
76#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)
9104a55d
TM
80#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))
1da177e4
LT
84#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))
96928206
BF
92#define nfs4_fattr_bitmap_maxsz 3
93#define encode_getattr_maxsz (op_encode_hdr_maxsz + nfs4_fattr_bitmap_maxsz)
1da177e4
LT
94#define nfs4_name_maxsz (1 + ((3 + NFS4_MAXNAMLEN) >> 2))
95#define nfs4_path_maxsz (1 + ((3 + NFS4_MAXPATHLEN) >> 2))
bd625ba8
TM
96#define nfs4_owner_maxsz (1 + XDR_QUADLEN(IDMAP_NAMESZ))
97#define nfs4_group_maxsz (1 + XDR_QUADLEN(IDMAP_NAMESZ))
96928206
BF
98/* 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 + \
bd625ba8 100 3 + 3 + 3 + nfs4_owner_maxsz + nfs4_group_maxsz))
96928206
BF
101#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)
9104a55d
TM
104#define encode_attrs_maxsz (nfs4_fattr_bitmap_maxsz + \
105 1 + 2 + 1 + \
106 nfs4_owner_maxsz + \
107 nfs4_group_maxsz + \
108 4 + 4)
1da177e4
LT
109#define encode_savefh_maxsz (op_encode_hdr_maxsz)
110#define decode_savefh_maxsz (op_decode_hdr_maxsz)
56ae19f3
TM
111#define encode_restorefh_maxsz (op_encode_hdr_maxsz)
112#define decode_restorefh_maxsz (op_decode_hdr_maxsz)
2f42b5d0 113#define encode_fsinfo_maxsz (encode_getattr_maxsz)
1da177e4
LT
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 + \
cc38bac3
CL
119 XDR_QUADLEN(NFS4_VERIFIER_SIZE) + \
120 XDR_QUADLEN(NFS4_SETCLIENTID_NAMELEN) + \
121 1 /* sc_prog */ + \
122 XDR_QUADLEN(RPCBIND_MAXNETIDLEN) + \
123 XDR_QUADLEN(RPCBIND_MAXUADDRLEN) + \
124 1) /* sc_cb_ident */
1da177e4
LT
125#define decode_setclientid_maxsz \
126 (op_decode_hdr_maxsz + \
127 2 + \
128 1024) /* large value for CLID_INUSE */
129#define encode_setclientid_confirm_maxsz \
130 (op_encode_hdr_maxsz + \
131 3 + (NFS4_VERIFIER_SIZE >> 2))
132#define decode_setclientid_confirm_maxsz \
133 (op_decode_hdr_maxsz)
e6889620
TM
134#define encode_lookup_maxsz (op_encode_hdr_maxsz + nfs4_name_maxsz)
135#define decode_lookup_maxsz (op_decode_hdr_maxsz)
2cebf828
TM
136#define encode_share_access_maxsz \
137 (2)
9104a55d 138#define encode_createmode_maxsz (1 + encode_attrs_maxsz)
2cebf828
TM
139#define encode_opentype_maxsz (1 + encode_createmode_maxsz)
140#define encode_claim_null_maxsz (1 + nfs4_name_maxsz)
141#define encode_open_maxsz (op_encode_hdr_maxsz + \
142 2 + encode_share_access_maxsz + 2 + \
143 open_owner_id_maxsz + \
144 encode_opentype_maxsz + \
145 encode_claim_null_maxsz)
146#define decode_ace_maxsz (3 + nfs4_owner_maxsz)
9104a55d 147#define decode_delegation_maxsz (1 + decode_stateid_maxsz + 1 + \
2cebf828
TM
148 decode_ace_maxsz)
149#define decode_change_info_maxsz (5)
150#define decode_open_maxsz (op_decode_hdr_maxsz + \
9104a55d 151 decode_stateid_maxsz + \
2cebf828
TM
152 decode_change_info_maxsz + 1 + \
153 nfs4_fattr_bitmap_maxsz + \
154 decode_delegation_maxsz)
9104a55d
TM
155#define encode_open_confirm_maxsz \
156 (op_encode_hdr_maxsz + \
157 encode_stateid_maxsz + 1)
158#define decode_open_confirm_maxsz \
159 (op_decode_hdr_maxsz + \
160 decode_stateid_maxsz)
161#define encode_open_downgrade_maxsz \
162 (op_encode_hdr_maxsz + \
163 encode_stateid_maxsz + 1 + \
164 encode_share_access_maxsz)
165#define decode_open_downgrade_maxsz \
166 (op_decode_hdr_maxsz + \
167 decode_stateid_maxsz)
168#define encode_close_maxsz (op_encode_hdr_maxsz + \
169 1 + encode_stateid_maxsz)
170#define decode_close_maxsz (op_decode_hdr_maxsz + \
171 decode_stateid_maxsz)
172#define encode_setattr_maxsz (op_encode_hdr_maxsz + \
173 encode_stateid_maxsz + \
174 encode_attrs_maxsz)
175#define decode_setattr_maxsz (op_decode_hdr_maxsz + \
176 nfs4_fattr_bitmap_maxsz)
177#define encode_read_maxsz (op_encode_hdr_maxsz + \
178 encode_stateid_maxsz + 3)
179#define decode_read_maxsz (op_decode_hdr_maxsz + 2)
180#define encode_readdir_maxsz (op_encode_hdr_maxsz + \
181 2 + encode_verifier_maxsz + 5)
182#define decode_readdir_maxsz (op_decode_hdr_maxsz + \
183 decode_verifier_maxsz)
184#define encode_readlink_maxsz (op_encode_hdr_maxsz)
185#define decode_readlink_maxsz (op_decode_hdr_maxsz + 1)
186#define encode_write_maxsz (op_encode_hdr_maxsz + \
187 encode_stateid_maxsz + 4)
188#define decode_write_maxsz (op_decode_hdr_maxsz + \
189 2 + decode_verifier_maxsz)
190#define encode_commit_maxsz (op_encode_hdr_maxsz + 3)
191#define decode_commit_maxsz (op_decode_hdr_maxsz + \
192 decode_verifier_maxsz)
1da177e4
LT
193#define encode_remove_maxsz (op_encode_hdr_maxsz + \
194 nfs4_name_maxsz)
195#define encode_rename_maxsz (op_encode_hdr_maxsz + \
196 2 * nfs4_name_maxsz)
197#define decode_rename_maxsz (op_decode_hdr_maxsz + 5 + 5)
198#define encode_link_maxsz (op_encode_hdr_maxsz + \
199 nfs4_name_maxsz)
200#define decode_link_maxsz (op_decode_hdr_maxsz + 5)
9104a55d
TM
201#define encode_lock_maxsz (op_encode_hdr_maxsz + \
202 7 + \
203 1 + encode_stateid_maxsz + 8)
204#define decode_lock_denied_maxsz \
205 (8 + decode_lockowner_maxsz)
206#define decode_lock_maxsz (op_decode_hdr_maxsz + \
207 decode_lock_denied_maxsz)
208#define encode_lockt_maxsz (op_encode_hdr_maxsz + 12)
209#define decode_lockt_maxsz (op_decode_hdr_maxsz + \
210 decode_lock_denied_maxsz)
211#define encode_locku_maxsz (op_encode_hdr_maxsz + 3 + \
212 encode_stateid_maxsz + \
213 4)
214#define decode_locku_maxsz (op_decode_hdr_maxsz + \
215 decode_stateid_maxsz)
216#define encode_access_maxsz (op_encode_hdr_maxsz + 1)
217#define decode_access_maxsz (op_decode_hdr_maxsz + 2)
1da177e4
LT
218#define encode_symlink_maxsz (op_encode_hdr_maxsz + \
219 1 + nfs4_name_maxsz + \
94a6d753 220 1 + \
96928206 221 nfs4_fattr_maxsz)
1da177e4
LT
222#define decode_symlink_maxsz (op_decode_hdr_maxsz + 8)
223#define encode_create_maxsz (op_encode_hdr_maxsz + \
9104a55d
TM
224 1 + 2 + nfs4_name_maxsz + \
225 encode_attrs_maxsz)
2cebf828
TM
226#define decode_create_maxsz (op_decode_hdr_maxsz + \
227 decode_change_info_maxsz + \
228 nfs4_fattr_bitmap_maxsz)
9104a55d
TM
229#define encode_statfs_maxsz (encode_getattr_maxsz)
230#define decode_statfs_maxsz (decode_getattr_maxsz)
1da177e4
LT
231#define encode_delegreturn_maxsz (op_encode_hdr_maxsz + 4)
232#define decode_delegreturn_maxsz (op_decode_hdr_maxsz)
9104a55d
TM
233#define encode_getacl_maxsz (encode_getattr_maxsz)
234#define decode_getacl_maxsz (op_decode_hdr_maxsz + \
235 nfs4_fattr_bitmap_maxsz + 1)
236#define encode_setacl_maxsz (op_encode_hdr_maxsz + \
237 encode_stateid_maxsz + 3)
238#define decode_setacl_maxsz (decode_setattr_maxsz)
e6889620
TM
239#define encode_fs_locations_maxsz \
240 (encode_getattr_maxsz)
241#define decode_fs_locations_maxsz \
242 (0)
1da177e4
LT
243#define NFS4_enc_compound_sz (1024) /* XXX: large enough? */
244#define NFS4_dec_compound_sz (1024) /* XXX: large enough? */
245#define NFS4_enc_read_sz (compound_encode_hdr_maxsz + \
246 encode_putfh_maxsz + \
9104a55d 247 encode_read_maxsz)
1da177e4
LT
248#define NFS4_dec_read_sz (compound_decode_hdr_maxsz + \
249 decode_putfh_maxsz + \
9104a55d 250 decode_read_maxsz)
1da177e4
LT
251#define NFS4_enc_readlink_sz (compound_encode_hdr_maxsz + \
252 encode_putfh_maxsz + \
9104a55d 253 encode_readlink_maxsz)
1da177e4
LT
254#define NFS4_dec_readlink_sz (compound_decode_hdr_maxsz + \
255 decode_putfh_maxsz + \
9104a55d 256 decode_readlink_maxsz)
1da177e4
LT
257#define NFS4_enc_readdir_sz (compound_encode_hdr_maxsz + \
258 encode_putfh_maxsz + \
9104a55d 259 encode_readdir_maxsz)
1da177e4
LT
260#define NFS4_dec_readdir_sz (compound_decode_hdr_maxsz + \
261 decode_putfh_maxsz + \
9104a55d 262 decode_readdir_maxsz)
1da177e4
LT
263#define NFS4_enc_write_sz (compound_encode_hdr_maxsz + \
264 encode_putfh_maxsz + \
9104a55d 265 encode_write_maxsz + \
4f9838c7 266 encode_getattr_maxsz)
1da177e4
LT
267#define NFS4_dec_write_sz (compound_decode_hdr_maxsz + \
268 decode_putfh_maxsz + \
9104a55d 269 decode_write_maxsz + \
4f9838c7 270 decode_getattr_maxsz)
1da177e4
LT
271#define NFS4_enc_commit_sz (compound_encode_hdr_maxsz + \
272 encode_putfh_maxsz + \
9104a55d 273 encode_commit_maxsz + \
4f9838c7 274 encode_getattr_maxsz)
1da177e4
LT
275#define NFS4_dec_commit_sz (compound_decode_hdr_maxsz + \
276 decode_putfh_maxsz + \
9104a55d 277 decode_commit_maxsz + \
4f9838c7 278 decode_getattr_maxsz)
1da177e4 279#define NFS4_enc_open_sz (compound_encode_hdr_maxsz + \
2cebf828
TM
280 encode_putfh_maxsz + \
281 encode_savefh_maxsz + \
282 encode_open_maxsz + \
283 encode_getfh_maxsz + \
284 encode_getattr_maxsz + \
285 encode_restorefh_maxsz + \
286 encode_getattr_maxsz)
1da177e4 287#define NFS4_dec_open_sz (compound_decode_hdr_maxsz + \
2cebf828
TM
288 decode_putfh_maxsz + \
289 decode_savefh_maxsz + \
290 decode_open_maxsz + \
291 decode_getfh_maxsz + \
292 decode_getattr_maxsz + \
293 decode_restorefh_maxsz + \
294 decode_getattr_maxsz)
9104a55d
TM
295#define NFS4_enc_open_confirm_sz \
296 (compound_encode_hdr_maxsz + \
297 encode_putfh_maxsz + \
298 encode_open_confirm_maxsz)
299#define NFS4_dec_open_confirm_sz \
300 (compound_decode_hdr_maxsz + \
301 decode_putfh_maxsz + \
302 decode_open_confirm_maxsz)
1da177e4
LT
303#define NFS4_enc_open_noattr_sz (compound_encode_hdr_maxsz + \
304 encode_putfh_maxsz + \
2cebf828
TM
305 encode_open_maxsz + \
306 encode_getattr_maxsz)
1da177e4
LT
307#define NFS4_dec_open_noattr_sz (compound_decode_hdr_maxsz + \
308 decode_putfh_maxsz + \
2cebf828
TM
309 decode_open_maxsz + \
310 decode_getattr_maxsz)
1da177e4
LT
311#define NFS4_enc_open_downgrade_sz \
312 (compound_encode_hdr_maxsz + \
9104a55d
TM
313 encode_putfh_maxsz + \
314 encode_open_downgrade_maxsz + \
315 encode_getattr_maxsz)
1da177e4
LT
316#define NFS4_dec_open_downgrade_sz \
317 (compound_decode_hdr_maxsz + \
9104a55d
TM
318 decode_putfh_maxsz + \
319 decode_open_downgrade_maxsz + \
320 decode_getattr_maxsz)
321#define NFS4_enc_close_sz (compound_encode_hdr_maxsz + \
322 encode_putfh_maxsz + \
323 encode_close_maxsz + \
324 encode_getattr_maxsz)
325#define NFS4_dec_close_sz (compound_decode_hdr_maxsz + \
326 decode_putfh_maxsz + \
327 decode_close_maxsz + \
328 decode_getattr_maxsz)
329#define NFS4_enc_setattr_sz (compound_encode_hdr_maxsz + \
330 encode_putfh_maxsz + \
331 encode_setattr_maxsz + \
332 encode_getattr_maxsz)
333#define NFS4_dec_setattr_sz (compound_decode_hdr_maxsz + \
334 decode_putfh_maxsz + \
335 decode_setattr_maxsz + \
336 decode_getattr_maxsz)
1da177e4
LT
337#define NFS4_enc_fsinfo_sz (compound_encode_hdr_maxsz + \
338 encode_putfh_maxsz + \
339 encode_fsinfo_maxsz)
340#define NFS4_dec_fsinfo_sz (compound_decode_hdr_maxsz + \
341 decode_putfh_maxsz + \
342 decode_fsinfo_maxsz)
343#define NFS4_enc_renew_sz (compound_encode_hdr_maxsz + \
344 encode_renew_maxsz)
345#define NFS4_dec_renew_sz (compound_decode_hdr_maxsz + \
346 decode_renew_maxsz)
347#define NFS4_enc_setclientid_sz (compound_encode_hdr_maxsz + \
348 encode_setclientid_maxsz)
349#define NFS4_dec_setclientid_sz (compound_decode_hdr_maxsz + \
350 decode_setclientid_maxsz)
351#define NFS4_enc_setclientid_confirm_sz \
352 (compound_encode_hdr_maxsz + \
353 encode_setclientid_confirm_maxsz + \
354 encode_putrootfh_maxsz + \
355 encode_fsinfo_maxsz)
356#define NFS4_dec_setclientid_confirm_sz \
357 (compound_decode_hdr_maxsz + \
358 decode_setclientid_confirm_maxsz + \
359 decode_putrootfh_maxsz + \
360 decode_fsinfo_maxsz)
361#define NFS4_enc_lock_sz (compound_encode_hdr_maxsz + \
362 encode_putfh_maxsz + \
9104a55d 363 encode_lock_maxsz)
1da177e4
LT
364#define NFS4_dec_lock_sz (compound_decode_hdr_maxsz + \
365 decode_putfh_maxsz + \
9104a55d 366 decode_lock_maxsz)
1da177e4
LT
367#define NFS4_enc_lockt_sz (compound_encode_hdr_maxsz + \
368 encode_putfh_maxsz + \
9104a55d
TM
369 encode_lockt_maxsz)
370#define NFS4_dec_lockt_sz (compound_decode_hdr_maxsz + \
371 decode_putfh_maxsz + \
372 decode_lockt_maxsz)
1da177e4
LT
373#define NFS4_enc_locku_sz (compound_encode_hdr_maxsz + \
374 encode_putfh_maxsz + \
9104a55d 375 encode_locku_maxsz)
1da177e4
LT
376#define NFS4_dec_locku_sz (compound_decode_hdr_maxsz + \
377 decode_putfh_maxsz + \
9104a55d 378 decode_locku_maxsz)
1da177e4
LT
379#define NFS4_enc_access_sz (compound_encode_hdr_maxsz + \
380 encode_putfh_maxsz + \
76b32999
TM
381 encode_access_maxsz + \
382 encode_getattr_maxsz)
1da177e4
LT
383#define NFS4_dec_access_sz (compound_decode_hdr_maxsz + \
384 decode_putfh_maxsz + \
76b32999
TM
385 decode_access_maxsz + \
386 decode_getattr_maxsz)
1da177e4
LT
387#define NFS4_enc_getattr_sz (compound_encode_hdr_maxsz + \
388 encode_putfh_maxsz + \
389 encode_getattr_maxsz)
390#define NFS4_dec_getattr_sz (compound_decode_hdr_maxsz + \
391 decode_putfh_maxsz + \
392 decode_getattr_maxsz)
393#define NFS4_enc_lookup_sz (compound_encode_hdr_maxsz + \
394 encode_putfh_maxsz + \
395 encode_lookup_maxsz + \
396 encode_getattr_maxsz + \
397 encode_getfh_maxsz)
398#define NFS4_dec_lookup_sz (compound_decode_hdr_maxsz + \
399 decode_putfh_maxsz + \
e6889620 400 decode_lookup_maxsz + \
1da177e4
LT
401 decode_getattr_maxsz + \
402 decode_getfh_maxsz)
403#define NFS4_enc_lookup_root_sz (compound_encode_hdr_maxsz + \
404 encode_putrootfh_maxsz + \
405 encode_getattr_maxsz + \
406 encode_getfh_maxsz)
407#define NFS4_dec_lookup_root_sz (compound_decode_hdr_maxsz + \
408 decode_putrootfh_maxsz + \
409 decode_getattr_maxsz + \
410 decode_getfh_maxsz)
411#define NFS4_enc_remove_sz (compound_encode_hdr_maxsz + \
412 encode_putfh_maxsz + \
16e42959
TM
413 encode_remove_maxsz + \
414 encode_getattr_maxsz)
1da177e4
LT
415#define NFS4_dec_remove_sz (compound_decode_hdr_maxsz + \
416 decode_putfh_maxsz + \
16e42959
TM
417 op_decode_hdr_maxsz + 5 + \
418 decode_getattr_maxsz)
1da177e4
LT
419#define NFS4_enc_rename_sz (compound_encode_hdr_maxsz + \
420 encode_putfh_maxsz + \
421 encode_savefh_maxsz + \
422 encode_putfh_maxsz + \
6caf2c82
TM
423 encode_rename_maxsz + \
424 encode_getattr_maxsz + \
425 encode_restorefh_maxsz + \
426 encode_getattr_maxsz)
1da177e4
LT
427#define NFS4_dec_rename_sz (compound_decode_hdr_maxsz + \
428 decode_putfh_maxsz + \
429 decode_savefh_maxsz + \
430 decode_putfh_maxsz + \
6caf2c82
TM
431 decode_rename_maxsz + \
432 decode_getattr_maxsz + \
433 decode_restorefh_maxsz + \
434 decode_getattr_maxsz)
1da177e4
LT
435#define NFS4_enc_link_sz (compound_encode_hdr_maxsz + \
436 encode_putfh_maxsz + \
437 encode_savefh_maxsz + \
438 encode_putfh_maxsz + \
91ba2eee
TM
439 encode_link_maxsz + \
440 decode_getattr_maxsz + \
441 encode_restorefh_maxsz + \
442 decode_getattr_maxsz)
1da177e4
LT
443#define NFS4_dec_link_sz (compound_decode_hdr_maxsz + \
444 decode_putfh_maxsz + \
445 decode_savefh_maxsz + \
446 decode_putfh_maxsz + \
91ba2eee
TM
447 decode_link_maxsz + \
448 decode_getattr_maxsz + \
449 decode_restorefh_maxsz + \
450 decode_getattr_maxsz)
1da177e4
LT
451#define NFS4_enc_symlink_sz (compound_encode_hdr_maxsz + \
452 encode_putfh_maxsz + \
453 encode_symlink_maxsz + \
454 encode_getattr_maxsz + \
455 encode_getfh_maxsz)
456#define NFS4_dec_symlink_sz (compound_decode_hdr_maxsz + \
457 decode_putfh_maxsz + \
458 decode_symlink_maxsz + \
459 decode_getattr_maxsz + \
460 decode_getfh_maxsz)
461#define NFS4_enc_create_sz (compound_encode_hdr_maxsz + \
462 encode_putfh_maxsz + \
56ae19f3 463 encode_savefh_maxsz + \
1da177e4 464 encode_create_maxsz + \
56ae19f3 465 encode_getfh_maxsz + \
1da177e4 466 encode_getattr_maxsz + \
56ae19f3
TM
467 encode_restorefh_maxsz + \
468 encode_getattr_maxsz)
1da177e4
LT
469#define NFS4_dec_create_sz (compound_decode_hdr_maxsz + \
470 decode_putfh_maxsz + \
56ae19f3 471 decode_savefh_maxsz + \
1da177e4 472 decode_create_maxsz + \
56ae19f3 473 decode_getfh_maxsz + \
1da177e4 474 decode_getattr_maxsz + \
56ae19f3
TM
475 decode_restorefh_maxsz + \
476 decode_getattr_maxsz)
1da177e4
LT
477#define NFS4_enc_pathconf_sz (compound_encode_hdr_maxsz + \
478 encode_putfh_maxsz + \
479 encode_getattr_maxsz)
480#define NFS4_dec_pathconf_sz (compound_decode_hdr_maxsz + \
481 decode_putfh_maxsz + \
482 decode_getattr_maxsz)
483#define NFS4_enc_statfs_sz (compound_encode_hdr_maxsz + \
484 encode_putfh_maxsz + \
9104a55d 485 encode_statfs_maxsz)
1da177e4
LT
486#define NFS4_dec_statfs_sz (compound_decode_hdr_maxsz + \
487 decode_putfh_maxsz + \
9104a55d 488 decode_statfs_maxsz)
1da177e4 489#define NFS4_enc_server_caps_sz (compound_encode_hdr_maxsz + \
ab91f264 490 encode_putfh_maxsz + \
1da177e4
LT
491 encode_getattr_maxsz)
492#define NFS4_dec_server_caps_sz (compound_decode_hdr_maxsz + \
ab91f264 493 decode_putfh_maxsz + \
1da177e4
LT
494 decode_getattr_maxsz)
495#define NFS4_enc_delegreturn_sz (compound_encode_hdr_maxsz + \
496 encode_putfh_maxsz + \
fa178f29
TM
497 encode_delegreturn_maxsz + \
498 encode_getattr_maxsz)
1da177e4 499#define NFS4_dec_delegreturn_sz (compound_decode_hdr_maxsz + \
fa178f29
TM
500 decode_delegreturn_maxsz + \
501 decode_getattr_maxsz)
029d105e
BF
502#define NFS4_enc_getacl_sz (compound_encode_hdr_maxsz + \
503 encode_putfh_maxsz + \
9104a55d 504 encode_getacl_maxsz)
029d105e
BF
505#define NFS4_dec_getacl_sz (compound_decode_hdr_maxsz + \
506 decode_putfh_maxsz + \
9104a55d 507 decode_getacl_maxsz)
23ec6965
BF
508#define NFS4_enc_setacl_sz (compound_encode_hdr_maxsz + \
509 encode_putfh_maxsz + \
9104a55d 510 encode_setacl_maxsz)
23ec6965
BF
511#define NFS4_dec_setacl_sz (compound_decode_hdr_maxsz + \
512 decode_putfh_maxsz + \
9104a55d 513 decode_setacl_maxsz)
683b57b4
TM
514#define NFS4_enc_fs_locations_sz \
515 (compound_encode_hdr_maxsz + \
516 encode_putfh_maxsz + \
e6889620
TM
517 encode_lookup_maxsz + \
518 encode_fs_locations_maxsz)
683b57b4
TM
519#define NFS4_dec_fs_locations_sz \
520 (compound_decode_hdr_maxsz + \
521 decode_putfh_maxsz + \
e6889620
TM
522 decode_lookup_maxsz + \
523 decode_fs_locations_maxsz)
1da177e4 524
bca79478
TM
525static const umode_t nfs_type2fmt[] = {
526 [NF4BAD] = 0,
527 [NF4REG] = S_IFREG,
528 [NF4DIR] = S_IFDIR,
529 [NF4BLK] = S_IFBLK,
530 [NF4CHR] = S_IFCHR,
531 [NF4LNK] = S_IFLNK,
532 [NF4SOCK] = S_IFSOCK,
533 [NF4FIFO] = S_IFIFO,
534 [NF4ATTRDIR] = 0,
535 [NF4NAMEDATTR] = 0,
1da177e4
LT
536};
537
538struct compound_hdr {
539 int32_t status;
540 uint32_t nops;
d017931c 541 __be32 * nops_p;
1da177e4
LT
542 uint32_t taglen;
543 char * tag;
544};
545
546/*
547 * START OF "GENERIC" ENCODE ROUTINES.
548 * These may look a little ugly since they are imported from a "generic"
549 * set of XDR encode/decode routines which are intended to be shared by
550 * all of our NFSv4 implementations (OpenBSD, MacOS X...).
551 *
552 * If the pain of reading these is too great, it should be a straightforward
553 * task to translate them into Linux-specific versions which are more
554 * consistent with the style used in NFSv2/v3...
555 */
556#define WRITE32(n) *p++ = htonl(n)
557#define WRITE64(n) do { \
558 *p++ = htonl((uint32_t)((n) >> 32)); \
559 *p++ = htonl((uint32_t)(n)); \
560} while (0)
561#define WRITEMEM(ptr,nbytes) do { \
562 p = xdr_encode_opaque_fixed(p, ptr, nbytes); \
563} while (0)
564
565#define RESERVE_SPACE(nbytes) do { \
566 p = xdr_reserve_space(xdr, nbytes); \
1da177e4
LT
567 BUG_ON(!p); \
568} while (0)
569
570static void encode_string(struct xdr_stream *xdr, unsigned int len, const char *str)
571{
8687b63a 572 __be32 *p;
1da177e4
LT
573
574 p = xdr_reserve_space(xdr, 4 + len);
575 BUG_ON(p == NULL);
576 xdr_encode_opaque(p, str, len);
577}
578
d017931c 579static void encode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
1da177e4 580{
8687b63a 581 __be32 *p;
1da177e4
LT
582
583 dprintk("encode_compound: tag=%.*s\n", (int)hdr->taglen, hdr->tag);
584 BUG_ON(hdr->taglen > NFS4_MAXTAGLEN);
585 RESERVE_SPACE(12+(XDR_QUADLEN(hdr->taglen)<<2));
586 WRITE32(hdr->taglen);
587 WRITEMEM(hdr->tag, hdr->taglen);
588 WRITE32(NFS4_MINOR_VERSION);
d017931c 589 hdr->nops_p = p;
1da177e4 590 WRITE32(hdr->nops);
d017931c
AA
591}
592
593static void encode_nops(struct compound_hdr *hdr)
594{
595 *hdr->nops_p = htonl(hdr->nops);
1da177e4
LT
596}
597
598static void encode_nfs4_verifier(struct xdr_stream *xdr, const nfs4_verifier *verf)
599{
8687b63a 600 __be32 *p;
1da177e4
LT
601
602 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
603 BUG_ON(p == NULL);
604 xdr_encode_opaque_fixed(p, verf->data, NFS4_VERIFIER_SIZE);
605}
606
cf8cdbe5 607static void encode_attrs(struct xdr_stream *xdr, const struct iattr *iap, const struct nfs_server *server)
1da177e4
LT
608{
609 char owner_name[IDMAP_NAMESZ];
610 char owner_group[IDMAP_NAMESZ];
611 int owner_namelen = 0;
612 int owner_grouplen = 0;
8687b63a
AV
613 __be32 *p;
614 __be32 *q;
1da177e4
LT
615 int len;
616 uint32_t bmval0 = 0;
617 uint32_t bmval1 = 0;
1da177e4
LT
618
619 /*
620 * We reserve enough space to write the entire attribute buffer at once.
621 * In the worst-case, this would be
622 * 12(bitmap) + 4(attrlen) + 8(size) + 4(mode) + 4(atime) + 4(mtime)
623 * = 36 bytes, plus any contribution from variable-length fields
23ec6965 624 * such as owner/group.
1da177e4
LT
625 */
626 len = 16;
627
628 /* Sigh */
629 if (iap->ia_valid & ATTR_SIZE)
630 len += 8;
631 if (iap->ia_valid & ATTR_MODE)
632 len += 4;
633 if (iap->ia_valid & ATTR_UID) {
7539bbab 634 owner_namelen = nfs_map_uid_to_name(server->nfs_client, iap->ia_uid, owner_name);
1da177e4 635 if (owner_namelen < 0) {
fe82a183
CL
636 dprintk("nfs: couldn't resolve uid %d to string\n",
637 iap->ia_uid);
1da177e4
LT
638 /* XXX */
639 strcpy(owner_name, "nobody");
640 owner_namelen = sizeof("nobody") - 1;
641 /* goto out; */
642 }
643 len += 4 + (XDR_QUADLEN(owner_namelen) << 2);
644 }
645 if (iap->ia_valid & ATTR_GID) {
7539bbab 646 owner_grouplen = nfs_map_gid_to_group(server->nfs_client, iap->ia_gid, owner_group);
1da177e4 647 if (owner_grouplen < 0) {
fe82a183
CL
648 dprintk("nfs: couldn't resolve gid %d to string\n",
649 iap->ia_gid);
1da177e4
LT
650 strcpy(owner_group, "nobody");
651 owner_grouplen = sizeof("nobody") - 1;
652 /* goto out; */
653 }
654 len += 4 + (XDR_QUADLEN(owner_grouplen) << 2);
655 }
656 if (iap->ia_valid & ATTR_ATIME_SET)
657 len += 16;
658 else if (iap->ia_valid & ATTR_ATIME)
659 len += 4;
660 if (iap->ia_valid & ATTR_MTIME_SET)
661 len += 16;
662 else if (iap->ia_valid & ATTR_MTIME)
663 len += 4;
664 RESERVE_SPACE(len);
665
666 /*
667 * We write the bitmap length now, but leave the bitmap and the attribute
668 * buffer length to be backfilled at the end of this routine.
669 */
670 WRITE32(2);
671 q = p;
672 p += 3;
673
674 if (iap->ia_valid & ATTR_SIZE) {
675 bmval0 |= FATTR4_WORD0_SIZE;
676 WRITE64(iap->ia_size);
677 }
678 if (iap->ia_valid & ATTR_MODE) {
679 bmval1 |= FATTR4_WORD1_MODE;
cf3fff54 680 WRITE32(iap->ia_mode & S_IALLUGO);
1da177e4
LT
681 }
682 if (iap->ia_valid & ATTR_UID) {
683 bmval1 |= FATTR4_WORD1_OWNER;
684 WRITE32(owner_namelen);
685 WRITEMEM(owner_name, owner_namelen);
686 }
687 if (iap->ia_valid & ATTR_GID) {
688 bmval1 |= FATTR4_WORD1_OWNER_GROUP;
689 WRITE32(owner_grouplen);
690 WRITEMEM(owner_group, owner_grouplen);
691 }
692 if (iap->ia_valid & ATTR_ATIME_SET) {
693 bmval1 |= FATTR4_WORD1_TIME_ACCESS_SET;
694 WRITE32(NFS4_SET_TO_CLIENT_TIME);
695 WRITE32(0);
696 WRITE32(iap->ia_mtime.tv_sec);
697 WRITE32(iap->ia_mtime.tv_nsec);
698 }
699 else if (iap->ia_valid & ATTR_ATIME) {
700 bmval1 |= FATTR4_WORD1_TIME_ACCESS_SET;
701 WRITE32(NFS4_SET_TO_SERVER_TIME);
702 }
703 if (iap->ia_valid & ATTR_MTIME_SET) {
704 bmval1 |= FATTR4_WORD1_TIME_MODIFY_SET;
705 WRITE32(NFS4_SET_TO_CLIENT_TIME);
706 WRITE32(0);
707 WRITE32(iap->ia_mtime.tv_sec);
708 WRITE32(iap->ia_mtime.tv_nsec);
709 }
710 else if (iap->ia_valid & ATTR_MTIME) {
711 bmval1 |= FATTR4_WORD1_TIME_MODIFY_SET;
712 WRITE32(NFS4_SET_TO_SERVER_TIME);
713 }
6c0195a4 714
1da177e4
LT
715 /*
716 * Now we backfill the bitmap and the attribute buffer length.
717 */
718 if (len != ((char *)p - (char *)q) + 4) {
fe82a183 719 printk(KERN_ERR "nfs: Attr length error, %u != %Zu\n",
1da177e4
LT
720 len, ((char *)p - (char *)q) + 4);
721 BUG();
722 }
723 len = (char *)p - (char *)q - 12;
724 *q++ = htonl(bmval0);
725 *q++ = htonl(bmval1);
726 *q++ = htonl(len);
727
1da177e4 728/* out: */
1da177e4
LT
729}
730
cf8cdbe5 731static void encode_access(struct xdr_stream *xdr, u32 access, struct compound_hdr *hdr)
1da177e4 732{
8687b63a 733 __be32 *p;
1da177e4
LT
734
735 RESERVE_SPACE(8);
736 WRITE32(OP_ACCESS);
737 WRITE32(access);
d017931c 738 hdr->nops++;
1da177e4
LT
739}
740
cf8cdbe5 741static void encode_close(struct xdr_stream *xdr, const struct nfs_closeargs *arg, struct compound_hdr *hdr)
1da177e4 742{
8687b63a 743 __be32 *p;
1da177e4 744
8ae20abd 745 RESERVE_SPACE(8+NFS4_STATEID_SIZE);
1da177e4 746 WRITE32(OP_CLOSE);
cee54fc9 747 WRITE32(arg->seqid->sequence->counter);
8ae20abd 748 WRITEMEM(arg->stateid->data, NFS4_STATEID_SIZE);
d017931c 749 hdr->nops++;
1da177e4
LT
750}
751
cf8cdbe5 752static void encode_commit(struct xdr_stream *xdr, const struct nfs_writeargs *args, struct compound_hdr *hdr)
1da177e4 753{
8687b63a 754 __be32 *p;
6c0195a4 755
05d564fe
AA
756 RESERVE_SPACE(16);
757 WRITE32(OP_COMMIT);
758 WRITE64(args->offset);
759 WRITE32(args->count);
d017931c 760 hdr->nops++;
1da177e4
LT
761}
762
cf8cdbe5 763static void encode_create(struct xdr_stream *xdr, const struct nfs4_create_arg *create, struct compound_hdr *hdr)
1da177e4 764{
8687b63a 765 __be32 *p;
6c0195a4 766
1da177e4
LT
767 RESERVE_SPACE(8);
768 WRITE32(OP_CREATE);
769 WRITE32(create->ftype);
770
771 switch (create->ftype) {
772 case NF4LNK:
94a6d753
CL
773 RESERVE_SPACE(4);
774 WRITE32(create->u.symlink.len);
775 xdr_write_pages(xdr, create->u.symlink.pages, 0, create->u.symlink.len);
1da177e4
LT
776 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);
d017931c 791 hdr->nops++;
1da177e4 792
cf8cdbe5 793 encode_attrs(xdr, create->attrs, create->server);
1da177e4
LT
794}
795
cf8cdbe5 796static void encode_getattr_one(struct xdr_stream *xdr, uint32_t bitmap, struct compound_hdr *hdr)
1da177e4 797{
05d564fe 798 __be32 *p;
1da177e4 799
05d564fe
AA
800 RESERVE_SPACE(12);
801 WRITE32(OP_GETATTR);
802 WRITE32(1);
803 WRITE32(bitmap);
d017931c 804 hdr->nops++;
1da177e4
LT
805}
806
cf8cdbe5 807static void encode_getattr_two(struct xdr_stream *xdr, uint32_t bm0, uint32_t bm1, struct compound_hdr *hdr)
1da177e4 808{
05d564fe 809 __be32 *p;
1da177e4 810
05d564fe
AA
811 RESERVE_SPACE(16);
812 WRITE32(OP_GETATTR);
813 WRITE32(2);
814 WRITE32(bm0);
815 WRITE32(bm1);
d017931c 816 hdr->nops++;
1da177e4
LT
817}
818
cf8cdbe5 819static void encode_getfattr(struct xdr_stream *xdr, const u32* bitmask, struct compound_hdr *hdr)
1da177e4 820{
cf8cdbe5
AA
821 encode_getattr_two(xdr, bitmask[0] & nfs4_fattr_bitmap[0],
822 bitmask[1] & nfs4_fattr_bitmap[1], hdr);
1da177e4
LT
823}
824
cf8cdbe5 825static void encode_fsinfo(struct xdr_stream *xdr, const u32* bitmask, struct compound_hdr *hdr)
1da177e4 826{
cf8cdbe5
AA
827 encode_getattr_two(xdr, bitmask[0] & nfs4_fsinfo_bitmap[0],
828 bitmask[1] & nfs4_fsinfo_bitmap[1], hdr);
1da177e4
LT
829}
830
cf8cdbe5 831static void encode_fs_locations(struct xdr_stream *xdr, const u32* bitmask, struct compound_hdr *hdr)
830b8e33 832{
cf8cdbe5
AA
833 encode_getattr_two(xdr, bitmask[0] & nfs4_fs_locations_bitmap[0],
834 bitmask[1] & nfs4_fs_locations_bitmap[1], hdr);
830b8e33
MN
835}
836
cf8cdbe5 837static void encode_getfh(struct xdr_stream *xdr, struct compound_hdr *hdr)
1da177e4 838{
8687b63a 839 __be32 *p;
1da177e4
LT
840
841 RESERVE_SPACE(4);
842 WRITE32(OP_GETFH);
d017931c 843 hdr->nops++;
1da177e4
LT
844}
845
cf8cdbe5 846static void encode_link(struct xdr_stream *xdr, const struct qstr *name, struct compound_hdr *hdr)
1da177e4 847{
8687b63a 848 __be32 *p;
1da177e4
LT
849
850 RESERVE_SPACE(8 + name->len);
851 WRITE32(OP_LINK);
852 WRITE32(name->len);
853 WRITEMEM(name->name, name->len);
d017931c 854 hdr->nops++;
1da177e4
LT
855}
856
911d1aaf
TM
857static inline int nfs4_lock_type(struct file_lock *fl, int block)
858{
859 if ((fl->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) == F_RDLCK)
860 return block ? NFS4_READW_LT : NFS4_READ_LT;
861 return block ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
862}
863
864static inline uint64_t nfs4_lock_length(struct file_lock *fl)
865{
866 if (fl->fl_end == OFFSET_MAX)
867 return ~(uint64_t)0;
868 return fl->fl_end - fl->fl_start + 1;
869}
870
1da177e4
LT
871/*
872 * opcode,type,reclaim,offset,length,new_lock_owner = 32
873 * open_seqid,open_stateid,lock_seqid,lock_owner.clientid, lock_owner.id = 40
874 */
cf8cdbe5 875static void encode_lock(struct xdr_stream *xdr, const struct nfs_lock_args *args, struct compound_hdr *hdr)
1da177e4 876{
8687b63a 877 __be32 *p;
1da177e4
LT
878
879 RESERVE_SPACE(32);
880 WRITE32(OP_LOCK);
911d1aaf
TM
881 WRITE32(nfs4_lock_type(args->fl, args->block));
882 WRITE32(args->reclaim);
883 WRITE64(args->fl->fl_start);
884 WRITE64(nfs4_lock_length(args->fl));
885 WRITE32(args->new_lock_owner);
886 if (args->new_lock_owner){
9f958ab8 887 RESERVE_SPACE(4+NFS4_STATEID_SIZE+32);
911d1aaf 888 WRITE32(args->open_seqid->sequence->counter);
8ae20abd 889 WRITEMEM(args->open_stateid->data, NFS4_STATEID_SIZE);
911d1aaf
TM
890 WRITE32(args->lock_seqid->sequence->counter);
891 WRITE64(args->lock_owner.clientid);
9f958ab8
TM
892 WRITE32(16);
893 WRITEMEM("lock id:", 8);
894 WRITE64(args->lock_owner.id);
1da177e4
LT
895 }
896 else {
8ae20abd
TM
897 RESERVE_SPACE(NFS4_STATEID_SIZE+4);
898 WRITEMEM(args->lock_stateid->data, NFS4_STATEID_SIZE);
911d1aaf 899 WRITE32(args->lock_seqid->sequence->counter);
1da177e4 900 }
d017931c 901 hdr->nops++;
1da177e4
LT
902}
903
cf8cdbe5 904static void encode_lockt(struct xdr_stream *xdr, const struct nfs_lockt_args *args, struct compound_hdr *hdr)
1da177e4 905{
8687b63a 906 __be32 *p;
1da177e4 907
9f958ab8 908 RESERVE_SPACE(52);
1da177e4 909 WRITE32(OP_LOCKT);
911d1aaf
TM
910 WRITE32(nfs4_lock_type(args->fl, 0));
911 WRITE64(args->fl->fl_start);
912 WRITE64(nfs4_lock_length(args->fl));
913 WRITE64(args->lock_owner.clientid);
9f958ab8
TM
914 WRITE32(16);
915 WRITEMEM("lock id:", 8);
916 WRITE64(args->lock_owner.id);
d017931c 917 hdr->nops++;
1da177e4
LT
918}
919
cf8cdbe5 920static void encode_locku(struct xdr_stream *xdr, const struct nfs_locku_args *args, struct compound_hdr *hdr)
1da177e4 921{
8687b63a 922 __be32 *p;
1da177e4 923
8ae20abd 924 RESERVE_SPACE(12+NFS4_STATEID_SIZE+16);
1da177e4 925 WRITE32(OP_LOCKU);
911d1aaf
TM
926 WRITE32(nfs4_lock_type(args->fl, 0));
927 WRITE32(args->seqid->sequence->counter);
8ae20abd 928 WRITEMEM(args->stateid->data, NFS4_STATEID_SIZE);
911d1aaf
TM
929 WRITE64(args->fl->fl_start);
930 WRITE64(nfs4_lock_length(args->fl));
d017931c 931 hdr->nops++;
1da177e4
LT
932}
933
cf8cdbe5 934static void encode_lookup(struct xdr_stream *xdr, const struct qstr *name, struct compound_hdr *hdr)
1da177e4
LT
935{
936 int len = name->len;
8687b63a 937 __be32 *p;
1da177e4
LT
938
939 RESERVE_SPACE(8 + len);
940 WRITE32(OP_LOOKUP);
941 WRITE32(len);
942 WRITEMEM(name->name, len);
d017931c 943 hdr->nops++;
1da177e4
LT
944}
945
dc0b027d 946static void encode_share_access(struct xdr_stream *xdr, fmode_t fmode)
1da177e4 947{
8687b63a 948 __be32 *p;
1da177e4
LT
949
950 RESERVE_SPACE(8);
dc0b027d 951 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
05d564fe
AA
952 case FMODE_READ:
953 WRITE32(NFS4_SHARE_ACCESS_READ);
954 break;
955 case FMODE_WRITE:
956 WRITE32(NFS4_SHARE_ACCESS_WRITE);
957 break;
958 case FMODE_READ|FMODE_WRITE:
959 WRITE32(NFS4_SHARE_ACCESS_BOTH);
960 break;
961 default:
962 WRITE32(0);
1da177e4
LT
963 }
964 WRITE32(0); /* for linux, share_deny = 0 always */
965}
966
967static inline void encode_openhdr(struct xdr_stream *xdr, const struct nfs_openargs *arg)
968{
8687b63a 969 __be32 *p;
1da177e4
LT
970 /*
971 * opcode 4, seqid 4, share_access 4, share_deny 4, clientid 8, ownerlen 4,
972 * owner 4 = 32
973 */
974 RESERVE_SPACE(8);
975 WRITE32(OP_OPEN);
cee54fc9 976 WRITE32(arg->seqid->sequence->counter);
dc0b027d 977 encode_share_access(xdr, arg->fmode);
9f958ab8 978 RESERVE_SPACE(28);
1da177e4 979 WRITE64(arg->clientid);
9f958ab8
TM
980 WRITE32(16);
981 WRITEMEM("open id:", 8);
982 WRITE64(arg->id);
1da177e4
LT
983}
984
985static inline void encode_createmode(struct xdr_stream *xdr, const struct nfs_openargs *arg)
986{
8687b63a 987 __be32 *p;
1da177e4
LT
988
989 RESERVE_SPACE(4);
990 switch(arg->open_flags & O_EXCL) {
05d564fe
AA
991 case 0:
992 WRITE32(NFS4_CREATE_UNCHECKED);
993 encode_attrs(xdr, arg->u.attrs, arg->server);
994 break;
995 default:
996 WRITE32(NFS4_CREATE_EXCLUSIVE);
997 encode_nfs4_verifier(xdr, &arg->u.verifier);
1da177e4
LT
998 }
999}
1000
1001static void encode_opentype(struct xdr_stream *xdr, const struct nfs_openargs *arg)
1002{
8687b63a 1003 __be32 *p;
1da177e4
LT
1004
1005 RESERVE_SPACE(4);
1006 switch (arg->open_flags & O_CREAT) {
05d564fe
AA
1007 case 0:
1008 WRITE32(NFS4_OPEN_NOCREATE);
1009 break;
1010 default:
1011 BUG_ON(arg->claim != NFS4_OPEN_CLAIM_NULL);
1012 WRITE32(NFS4_OPEN_CREATE);
1013 encode_createmode(xdr, arg);
1da177e4
LT
1014 }
1015}
1016
bd7bf9d5 1017static inline void encode_delegation_type(struct xdr_stream *xdr, fmode_t delegation_type)
1da177e4 1018{
8687b63a 1019 __be32 *p;
1da177e4
LT
1020
1021 RESERVE_SPACE(4);
1022 switch (delegation_type) {
05d564fe
AA
1023 case 0:
1024 WRITE32(NFS4_OPEN_DELEGATE_NONE);
1025 break;
1026 case FMODE_READ:
1027 WRITE32(NFS4_OPEN_DELEGATE_READ);
1028 break;
1029 case FMODE_WRITE|FMODE_READ:
1030 WRITE32(NFS4_OPEN_DELEGATE_WRITE);
1031 break;
1032 default:
1033 BUG();
1da177e4
LT
1034 }
1035}
1036
1037static inline void encode_claim_null(struct xdr_stream *xdr, const struct qstr *name)
1038{
8687b63a 1039 __be32 *p;
1da177e4
LT
1040
1041 RESERVE_SPACE(4);
1042 WRITE32(NFS4_OPEN_CLAIM_NULL);
1043 encode_string(xdr, name->len, name->name);
1044}
1045
bd7bf9d5 1046static inline void encode_claim_previous(struct xdr_stream *xdr, fmode_t type)
1da177e4 1047{
8687b63a 1048 __be32 *p;
1da177e4
LT
1049
1050 RESERVE_SPACE(4);
1051 WRITE32(NFS4_OPEN_CLAIM_PREVIOUS);
1052 encode_delegation_type(xdr, type);
1053}
1054
1055static inline void encode_claim_delegate_cur(struct xdr_stream *xdr, const struct qstr *name, const nfs4_stateid *stateid)
1056{
8687b63a 1057 __be32 *p;
1da177e4 1058
8ae20abd 1059 RESERVE_SPACE(4+NFS4_STATEID_SIZE);
1da177e4 1060 WRITE32(NFS4_OPEN_CLAIM_DELEGATE_CUR);
8ae20abd 1061 WRITEMEM(stateid->data, NFS4_STATEID_SIZE);
1da177e4
LT
1062 encode_string(xdr, name->len, name->name);
1063}
1064
cf8cdbe5 1065static void encode_open(struct xdr_stream *xdr, const struct nfs_openargs *arg, struct compound_hdr *hdr)
1da177e4
LT
1066{
1067 encode_openhdr(xdr, arg);
1068 encode_opentype(xdr, arg);
1069 switch (arg->claim) {
05d564fe
AA
1070 case NFS4_OPEN_CLAIM_NULL:
1071 encode_claim_null(xdr, arg->name);
1072 break;
1073 case NFS4_OPEN_CLAIM_PREVIOUS:
1074 encode_claim_previous(xdr, arg->u.delegation_type);
1075 break;
1076 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1077 encode_claim_delegate_cur(xdr, arg->name, &arg->u.delegation);
1078 break;
1079 default:
1080 BUG();
1da177e4 1081 }
d017931c 1082 hdr->nops++;
1da177e4
LT
1083}
1084
cf8cdbe5 1085static void encode_open_confirm(struct xdr_stream *xdr, const struct nfs_open_confirmargs *arg, struct compound_hdr *hdr)
1da177e4 1086{
8687b63a 1087 __be32 *p;
1da177e4 1088
8ae20abd 1089 RESERVE_SPACE(4+NFS4_STATEID_SIZE+4);
1da177e4 1090 WRITE32(OP_OPEN_CONFIRM);
8ae20abd 1091 WRITEMEM(arg->stateid->data, NFS4_STATEID_SIZE);
cee54fc9 1092 WRITE32(arg->seqid->sequence->counter);
d017931c 1093 hdr->nops++;
1da177e4
LT
1094}
1095
cf8cdbe5 1096static void encode_open_downgrade(struct xdr_stream *xdr, const struct nfs_closeargs *arg, struct compound_hdr *hdr)
1da177e4 1097{
8687b63a 1098 __be32 *p;
1da177e4 1099
8ae20abd 1100 RESERVE_SPACE(4+NFS4_STATEID_SIZE+4);
1da177e4 1101 WRITE32(OP_OPEN_DOWNGRADE);
8ae20abd 1102 WRITEMEM(arg->stateid->data, NFS4_STATEID_SIZE);
cee54fc9 1103 WRITE32(arg->seqid->sequence->counter);
dc0b027d 1104 encode_share_access(xdr, arg->fmode);
d017931c 1105 hdr->nops++;
1da177e4
LT
1106}
1107
cf8cdbe5 1108static void
d017931c 1109encode_putfh(struct xdr_stream *xdr, const struct nfs_fh *fh, struct compound_hdr *hdr)
1da177e4
LT
1110{
1111 int len = fh->size;
8687b63a 1112 __be32 *p;
1da177e4
LT
1113
1114 RESERVE_SPACE(8 + len);
1115 WRITE32(OP_PUTFH);
1116 WRITE32(len);
1117 WRITEMEM(fh->data, len);
d017931c 1118 hdr->nops++;
1da177e4
LT
1119}
1120
cf8cdbe5 1121static void encode_putrootfh(struct xdr_stream *xdr, struct compound_hdr *hdr)
1da177e4 1122{
05d564fe 1123 __be32 *p;
6c0195a4 1124
05d564fe
AA
1125 RESERVE_SPACE(4);
1126 WRITE32(OP_PUTROOTFH);
d017931c 1127 hdr->nops++;
1da177e4
LT
1128}
1129
1130static void encode_stateid(struct xdr_stream *xdr, const struct nfs_open_context *ctx)
1131{
1da177e4 1132 nfs4_stateid stateid;
8687b63a 1133 __be32 *p;
1da177e4 1134
8ae20abd 1135 RESERVE_SPACE(NFS4_STATEID_SIZE);
1da177e4
LT
1136 if (ctx->state != NULL) {
1137 nfs4_copy_stateid(&stateid, ctx->state, ctx->lockowner);
8ae20abd 1138 WRITEMEM(stateid.data, NFS4_STATEID_SIZE);
1da177e4 1139 } else
8ae20abd 1140 WRITEMEM(zero_stateid.data, NFS4_STATEID_SIZE);
1da177e4
LT
1141}
1142
cf8cdbe5 1143static void encode_read(struct xdr_stream *xdr, const struct nfs_readargs *args, struct compound_hdr *hdr)
1da177e4 1144{
8687b63a 1145 __be32 *p;
1da177e4
LT
1146
1147 RESERVE_SPACE(4);
1148 WRITE32(OP_READ);
1149
1150 encode_stateid(xdr, args->context);
1151
1152 RESERVE_SPACE(12);
1153 WRITE64(args->offset);
1154 WRITE32(args->count);
d017931c 1155 hdr->nops++;
1da177e4
LT
1156}
1157
cf8cdbe5 1158static void encode_readdir(struct xdr_stream *xdr, const struct nfs4_readdir_arg *readdir, struct rpc_rqst *req, struct compound_hdr *hdr)
1da177e4 1159{
97d312d0
MN
1160 uint32_t attrs[2] = {
1161 FATTR4_WORD0_RDATTR_ERROR|FATTR4_WORD0_FILEID,
1162 FATTR4_WORD1_MOUNTED_ON_FILEID,
1163 };
8687b63a 1164 __be32 *p;
1da177e4 1165
8ae20abd 1166 RESERVE_SPACE(12+NFS4_VERIFIER_SIZE+20);
1da177e4
LT
1167 WRITE32(OP_READDIR);
1168 WRITE64(readdir->cookie);
8ae20abd 1169 WRITEMEM(readdir->verifier.data, NFS4_VERIFIER_SIZE);
1da177e4
LT
1170 WRITE32(readdir->count >> 1); /* We're not doing readdirplus */
1171 WRITE32(readdir->count);
1172 WRITE32(2);
97d312d0
MN
1173 /* Switch to mounted_on_fileid if the server supports it */
1174 if (readdir->bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
1175 attrs[0] &= ~FATTR4_WORD0_FILEID;
1176 else
1177 attrs[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
1178 WRITE32(attrs[0] & readdir->bitmask[0]);
1179 WRITE32(attrs[1] & readdir->bitmask[1]);
d017931c 1180 hdr->nops++;
44109241
FI
1181 dprintk("%s: cookie = %Lu, verifier = %08x:%08x, bitmap = %08x:%08x\n",
1182 __func__,
eadf4598
TM
1183 (unsigned long long)readdir->cookie,
1184 ((u32 *)readdir->verifier.data)[0],
1185 ((u32 *)readdir->verifier.data)[1],
1186 attrs[0] & readdir->bitmask[0],
1187 attrs[1] & readdir->bitmask[1]);
1da177e4
LT
1188}
1189
cf8cdbe5 1190static void encode_readlink(struct xdr_stream *xdr, const struct nfs4_readlink *readlink, struct rpc_rqst *req, struct compound_hdr *hdr)
1da177e4 1191{
8687b63a 1192 __be32 *p;
1da177e4
LT
1193
1194 RESERVE_SPACE(4);
1195 WRITE32(OP_READLINK);
d017931c 1196 hdr->nops++;
1da177e4
LT
1197}
1198
cf8cdbe5 1199static void encode_remove(struct xdr_stream *xdr, const struct qstr *name, struct compound_hdr *hdr)
1da177e4 1200{
8687b63a 1201 __be32 *p;
1da177e4
LT
1202
1203 RESERVE_SPACE(8 + name->len);
1204 WRITE32(OP_REMOVE);
1205 WRITE32(name->len);
1206 WRITEMEM(name->name, name->len);
d017931c 1207 hdr->nops++;
1da177e4
LT
1208}
1209
cf8cdbe5 1210static void encode_rename(struct xdr_stream *xdr, const struct qstr *oldname, const struct qstr *newname, struct compound_hdr *hdr)
1da177e4 1211{
8687b63a 1212 __be32 *p;
1da177e4
LT
1213
1214 RESERVE_SPACE(8 + oldname->len);
1215 WRITE32(OP_RENAME);
1216 WRITE32(oldname->len);
1217 WRITEMEM(oldname->name, oldname->len);
6c0195a4 1218
1da177e4
LT
1219 RESERVE_SPACE(4 + newname->len);
1220 WRITE32(newname->len);
1221 WRITEMEM(newname->name, newname->len);
d017931c 1222 hdr->nops++;
1da177e4
LT
1223}
1224
cf8cdbe5 1225static void encode_renew(struct xdr_stream *xdr, const struct nfs_client *client_stateid, struct compound_hdr *hdr)
1da177e4 1226{
8687b63a 1227 __be32 *p;
1da177e4
LT
1228
1229 RESERVE_SPACE(12);
1230 WRITE32(OP_RENEW);
1231 WRITE64(client_stateid->cl_clientid);
d017931c 1232 hdr->nops++;
1da177e4
LT
1233}
1234
cf8cdbe5 1235static void
d017931c 1236encode_restorefh(struct xdr_stream *xdr, struct compound_hdr *hdr)
56ae19f3 1237{
8687b63a 1238 __be32 *p;
56ae19f3
TM
1239
1240 RESERVE_SPACE(4);
1241 WRITE32(OP_RESTOREFH);
d017931c 1242 hdr->nops++;
56ae19f3
TM
1243}
1244
23ec6965 1245static int
d017931c 1246encode_setacl(struct xdr_stream *xdr, struct nfs_setaclargs *arg, struct compound_hdr *hdr)
23ec6965 1247{
8687b63a 1248 __be32 *p;
23ec6965 1249
8ae20abd 1250 RESERVE_SPACE(4+NFS4_STATEID_SIZE);
23ec6965 1251 WRITE32(OP_SETATTR);
8ae20abd 1252 WRITEMEM(zero_stateid.data, NFS4_STATEID_SIZE);
23ec6965
BF
1253 RESERVE_SPACE(2*4);
1254 WRITE32(1);
1255 WRITE32(FATTR4_WORD0_ACL);
1256 if (arg->acl_len % 4)
1257 return -EINVAL;
1258 RESERVE_SPACE(4);
1259 WRITE32(arg->acl_len);
1260 xdr_write_pages(xdr, arg->acl_pages, arg->acl_pgbase, arg->acl_len);
d017931c 1261 hdr->nops++;
23ec6965
BF
1262 return 0;
1263}
1264
cf8cdbe5 1265static void
d017931c 1266encode_savefh(struct xdr_stream *xdr, struct compound_hdr *hdr)
1da177e4 1267{
8687b63a 1268 __be32 *p;
1da177e4
LT
1269
1270 RESERVE_SPACE(4);
1271 WRITE32(OP_SAVEFH);
d017931c 1272 hdr->nops++;
1da177e4
LT
1273}
1274
cf8cdbe5 1275static void encode_setattr(struct xdr_stream *xdr, const struct nfs_setattrargs *arg, const struct nfs_server *server, struct compound_hdr *hdr)
1da177e4 1276{
8687b63a 1277 __be32 *p;
6c0195a4 1278
05d564fe
AA
1279 RESERVE_SPACE(4+NFS4_STATEID_SIZE);
1280 WRITE32(OP_SETATTR);
8ae20abd 1281 WRITEMEM(arg->stateid.data, NFS4_STATEID_SIZE);
d017931c 1282 hdr->nops++;
cf8cdbe5 1283 encode_attrs(xdr, arg->iap, server);
1da177e4
LT
1284}
1285
cf8cdbe5 1286static void encode_setclientid(struct xdr_stream *xdr, const struct nfs4_setclientid *setclientid, struct compound_hdr *hdr)
1da177e4 1287{
8687b63a 1288 __be32 *p;
1da177e4 1289
8ae20abd 1290 RESERVE_SPACE(4 + NFS4_VERIFIER_SIZE);
1da177e4 1291 WRITE32(OP_SETCLIENTID);
8ae20abd 1292 WRITEMEM(setclientid->sc_verifier->data, NFS4_VERIFIER_SIZE);
1da177e4
LT
1293
1294 encode_string(xdr, setclientid->sc_name_len, setclientid->sc_name);
1295 RESERVE_SPACE(4);
1296 WRITE32(setclientid->sc_prog);
1297 encode_string(xdr, setclientid->sc_netid_len, setclientid->sc_netid);
1298 encode_string(xdr, setclientid->sc_uaddr_len, setclientid->sc_uaddr);
1299 RESERVE_SPACE(4);
1300 WRITE32(setclientid->sc_cb_ident);
d017931c 1301 hdr->nops++;
1da177e4
LT
1302}
1303
cf8cdbe5 1304static void encode_setclientid_confirm(struct xdr_stream *xdr, const struct nfs_client *client_state, struct compound_hdr *hdr)
1da177e4 1305{
05d564fe 1306 __be32 *p;
1da177e4 1307
05d564fe
AA
1308 RESERVE_SPACE(12 + NFS4_VERIFIER_SIZE);
1309 WRITE32(OP_SETCLIENTID_CONFIRM);
1310 WRITE64(client_state->cl_clientid);
1311 WRITEMEM(client_state->cl_confirm.data, NFS4_VERIFIER_SIZE);
d017931c 1312 hdr->nops++;
1da177e4
LT
1313}
1314
cf8cdbe5 1315static void encode_write(struct xdr_stream *xdr, const struct nfs_writeargs *args, struct compound_hdr *hdr)
1da177e4 1316{
8687b63a 1317 __be32 *p;
1da177e4
LT
1318
1319 RESERVE_SPACE(4);
1320 WRITE32(OP_WRITE);
1321
1322 encode_stateid(xdr, args->context);
1323
1324 RESERVE_SPACE(16);
1325 WRITE64(args->offset);
1326 WRITE32(args->stable);
1327 WRITE32(args->count);
1328
1329 xdr_write_pages(xdr, args->pages, args->pgbase, args->count);
d017931c 1330 hdr->nops++;
1da177e4
LT
1331}
1332
cf8cdbe5 1333static void encode_delegreturn(struct xdr_stream *xdr, const nfs4_stateid *stateid, struct compound_hdr *hdr)
1da177e4 1334{
8687b63a 1335 __be32 *p;
1da177e4 1336
8ae20abd 1337 RESERVE_SPACE(4+NFS4_STATEID_SIZE);
1da177e4
LT
1338
1339 WRITE32(OP_DELEGRETURN);
8ae20abd 1340 WRITEMEM(stateid->data, NFS4_STATEID_SIZE);
d017931c 1341 hdr->nops++;
1da177e4
LT
1342}
1343/*
1344 * END OF "GENERIC" ENCODE ROUTINES.
1345 */
1346
1347/*
1348 * Encode an ACCESS request
1349 */
8687b63a 1350static int nfs4_xdr_enc_access(struct rpc_rqst *req, __be32 *p, const struct nfs4_accessargs *args)
1da177e4
LT
1351{
1352 struct xdr_stream xdr;
1353 struct compound_hdr hdr = {
d017931c 1354 .nops = 0,
1da177e4 1355 };
1da177e4
LT
1356
1357 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1358 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1359 encode_putfh(&xdr, args->fh, &hdr);
1360 encode_access(&xdr, args->access, &hdr);
1361 encode_getfattr(&xdr, args->bitmask, &hdr);
d017931c 1362 encode_nops(&hdr);
cf8cdbe5 1363 return 0;
1da177e4
LT
1364}
1365
1366/*
1367 * Encode LOOKUP request
1368 */
8687b63a 1369static int nfs4_xdr_enc_lookup(struct rpc_rqst *req, __be32 *p, const struct nfs4_lookup_arg *args)
1da177e4
LT
1370{
1371 struct xdr_stream xdr;
1372 struct compound_hdr hdr = {
d017931c 1373 .nops = 0,
1da177e4 1374 };
1da177e4
LT
1375
1376 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1377 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1378 encode_putfh(&xdr, args->dir_fh, &hdr);
1379 encode_lookup(&xdr, args->name, &hdr);
1380 encode_getfh(&xdr, &hdr);
1381 encode_getfattr(&xdr, args->bitmask, &hdr);
d017931c 1382 encode_nops(&hdr);
cf8cdbe5 1383 return 0;
1da177e4
LT
1384}
1385
1386/*
1387 * Encode LOOKUP_ROOT request
1388 */
8687b63a 1389static int nfs4_xdr_enc_lookup_root(struct rpc_rqst *req, __be32 *p, const struct nfs4_lookup_root_arg *args)
1da177e4
LT
1390{
1391 struct xdr_stream xdr;
1392 struct compound_hdr hdr = {
d017931c 1393 .nops = 0,
1da177e4 1394 };
1da177e4
LT
1395
1396 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1397 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1398 encode_putrootfh(&xdr, &hdr);
1399 encode_getfh(&xdr, &hdr);
1400 encode_getfattr(&xdr, args->bitmask, &hdr);
d017931c 1401 encode_nops(&hdr);
cf8cdbe5 1402 return 0;
1da177e4
LT
1403}
1404
1405/*
1406 * Encode REMOVE request
1407 */
4fdc17b2 1408static int nfs4_xdr_enc_remove(struct rpc_rqst *req, __be32 *p, const struct nfs_removeargs *args)
1da177e4
LT
1409{
1410 struct xdr_stream xdr;
1411 struct compound_hdr hdr = {
d017931c 1412 .nops = 0,
1da177e4 1413 };
1da177e4
LT
1414
1415 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1416 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1417 encode_putfh(&xdr, args->fh, &hdr);
1418 encode_remove(&xdr, &args->name, &hdr);
1419 encode_getfattr(&xdr, args->bitmask, &hdr);
d017931c 1420 encode_nops(&hdr);
cf8cdbe5 1421 return 0;
1da177e4
LT
1422}
1423
1424/*
1425 * Encode RENAME request
1426 */
8687b63a 1427static int nfs4_xdr_enc_rename(struct rpc_rqst *req, __be32 *p, const struct nfs4_rename_arg *args)
1da177e4
LT
1428{
1429 struct xdr_stream xdr;
1430 struct compound_hdr hdr = {
d017931c 1431 .nops = 0,
1da177e4 1432 };
1da177e4
LT
1433
1434 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1435 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1436 encode_putfh(&xdr, args->old_dir, &hdr);
1437 encode_savefh(&xdr, &hdr);
1438 encode_putfh(&xdr, args->new_dir, &hdr);
1439 encode_rename(&xdr, args->old_name, args->new_name, &hdr);
1440 encode_getfattr(&xdr, args->bitmask, &hdr);
1441 encode_restorefh(&xdr, &hdr);
1442 encode_getfattr(&xdr, args->bitmask, &hdr);
d017931c 1443 encode_nops(&hdr);
cf8cdbe5 1444 return 0;
1da177e4
LT
1445}
1446
1447/*
1448 * Encode LINK request
1449 */
8687b63a 1450static int nfs4_xdr_enc_link(struct rpc_rqst *req, __be32 *p, const struct nfs4_link_arg *args)
1da177e4
LT
1451{
1452 struct xdr_stream xdr;
1453 struct compound_hdr hdr = {
d017931c 1454 .nops = 0,
1da177e4 1455 };
1da177e4
LT
1456
1457 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1458 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1459 encode_putfh(&xdr, args->fh, &hdr);
1460 encode_savefh(&xdr, &hdr);
1461 encode_putfh(&xdr, args->dir_fh, &hdr);
1462 encode_link(&xdr, args->name, &hdr);
1463 encode_getfattr(&xdr, args->bitmask, &hdr);
1464 encode_restorefh(&xdr, &hdr);
1465 encode_getfattr(&xdr, args->bitmask, &hdr);
d017931c 1466 encode_nops(&hdr);
cf8cdbe5 1467 return 0;
1da177e4
LT
1468}
1469
1470/*
1471 * Encode CREATE request
1472 */
8687b63a 1473static int nfs4_xdr_enc_create(struct rpc_rqst *req, __be32 *p, const struct nfs4_create_arg *args)
1da177e4
LT
1474{
1475 struct xdr_stream xdr;
1476 struct compound_hdr hdr = {
d017931c 1477 .nops = 0,
1da177e4 1478 };
1da177e4
LT
1479
1480 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1481 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1482 encode_putfh(&xdr, args->dir_fh, &hdr);
1483 encode_savefh(&xdr, &hdr);
1484 encode_create(&xdr, args, &hdr);
1485 encode_getfh(&xdr, &hdr);
1486 encode_getfattr(&xdr, args->bitmask, &hdr);
1487 encode_restorefh(&xdr, &hdr);
1488 encode_getfattr(&xdr, args->bitmask, &hdr);
d017931c 1489 encode_nops(&hdr);
cf8cdbe5 1490 return 0;
1da177e4
LT
1491}
1492
1493/*
1494 * Encode SYMLINK request
1495 */
8687b63a 1496static int nfs4_xdr_enc_symlink(struct rpc_rqst *req, __be32 *p, const struct nfs4_create_arg *args)
1da177e4
LT
1497{
1498 return nfs4_xdr_enc_create(req, p, args);
1499}
1500
1501/*
1502 * Encode GETATTR request
1503 */
8687b63a 1504static int nfs4_xdr_enc_getattr(struct rpc_rqst *req, __be32 *p, const struct nfs4_getattr_arg *args)
1da177e4
LT
1505{
1506 struct xdr_stream xdr;
1507 struct compound_hdr hdr = {
d017931c 1508 .nops = 0,
1da177e4 1509 };
1da177e4
LT
1510
1511 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1512 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1513 encode_putfh(&xdr, args->fh, &hdr);
1514 encode_getfattr(&xdr, args->bitmask, &hdr);
d017931c 1515 encode_nops(&hdr);
cf8cdbe5 1516 return 0;
1da177e4
LT
1517}
1518
1519/*
1520 * Encode a CLOSE request
1521 */
8687b63a 1522static int nfs4_xdr_enc_close(struct rpc_rqst *req, __be32 *p, struct nfs_closeargs *args)
1da177e4 1523{
05d564fe
AA
1524 struct xdr_stream xdr;
1525 struct compound_hdr hdr = {
d017931c 1526 .nops = 0,
05d564fe 1527 };
05d564fe
AA
1528
1529 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1530 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1531 encode_putfh(&xdr, args->fh, &hdr);
1532 encode_close(&xdr, args, &hdr);
1533 encode_getfattr(&xdr, args->bitmask, &hdr);
d017931c 1534 encode_nops(&hdr);
cf8cdbe5 1535 return 0;
1da177e4
LT
1536}
1537
1538/*
1539 * Encode an OPEN request
1540 */
8687b63a 1541static int nfs4_xdr_enc_open(struct rpc_rqst *req, __be32 *p, struct nfs_openargs *args)
1da177e4
LT
1542{
1543 struct xdr_stream xdr;
1544 struct compound_hdr hdr = {
d017931c 1545 .nops = 0,
1da177e4 1546 };
1da177e4
LT
1547
1548 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1549 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1550 encode_putfh(&xdr, args->fh, &hdr);
1551 encode_savefh(&xdr, &hdr);
1552 encode_open(&xdr, args, &hdr);
1553 encode_getfh(&xdr, &hdr);
1554 encode_getfattr(&xdr, args->bitmask, &hdr);
1555 encode_restorefh(&xdr, &hdr);
1556 encode_getfattr(&xdr, args->bitmask, &hdr);
d017931c 1557 encode_nops(&hdr);
cf8cdbe5 1558 return 0;
1da177e4
LT
1559}
1560
1561/*
1562 * Encode an OPEN_CONFIRM request
1563 */
8687b63a 1564static int nfs4_xdr_enc_open_confirm(struct rpc_rqst *req, __be32 *p, struct nfs_open_confirmargs *args)
1da177e4
LT
1565{
1566 struct xdr_stream xdr;
1567 struct compound_hdr hdr = {
d017931c 1568 .nops = 0,
1da177e4 1569 };
1da177e4
LT
1570
1571 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1572 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1573 encode_putfh(&xdr, args->fh, &hdr);
1574 encode_open_confirm(&xdr, args, &hdr);
d017931c 1575 encode_nops(&hdr);
cf8cdbe5 1576 return 0;
1da177e4
LT
1577}
1578
1579/*
1580 * Encode an OPEN request with no attributes.
1581 */
8687b63a 1582static int nfs4_xdr_enc_open_noattr(struct rpc_rqst *req, __be32 *p, struct nfs_openargs *args)
1da177e4
LT
1583{
1584 struct xdr_stream xdr;
1585 struct compound_hdr hdr = {
d017931c 1586 .nops = 0,
1da177e4 1587 };
1da177e4
LT
1588
1589 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1590 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1591 encode_putfh(&xdr, args->fh, &hdr);
1592 encode_open(&xdr, args, &hdr);
1593 encode_getfattr(&xdr, args->bitmask, &hdr);
d017931c 1594 encode_nops(&hdr);
cf8cdbe5 1595 return 0;
1da177e4
LT
1596}
1597
1598/*
1599 * Encode an OPEN_DOWNGRADE request
1600 */
8687b63a 1601static int nfs4_xdr_enc_open_downgrade(struct rpc_rqst *req, __be32 *p, struct nfs_closeargs *args)
1da177e4
LT
1602{
1603 struct xdr_stream xdr;
1604 struct compound_hdr hdr = {
d017931c 1605 .nops = 0,
1da177e4 1606 };
1da177e4
LT
1607
1608 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1609 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1610 encode_putfh(&xdr, args->fh, &hdr);
1611 encode_open_downgrade(&xdr, args, &hdr);
1612 encode_getfattr(&xdr, args->bitmask, &hdr);
d017931c 1613 encode_nops(&hdr);
cf8cdbe5 1614 return 0;
1da177e4
LT
1615}
1616
1617/*
1618 * Encode a LOCK request
1619 */
8687b63a 1620static int nfs4_xdr_enc_lock(struct rpc_rqst *req, __be32 *p, struct nfs_lock_args *args)
1da177e4
LT
1621{
1622 struct xdr_stream xdr;
1623 struct compound_hdr hdr = {
d017931c 1624 .nops = 0,
1da177e4 1625 };
1da177e4
LT
1626
1627 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1628 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1629 encode_putfh(&xdr, args->fh, &hdr);
1630 encode_lock(&xdr, args, &hdr);
d017931c 1631 encode_nops(&hdr);
cf8cdbe5 1632 return 0;
1da177e4
LT
1633}
1634
1635/*
1636 * Encode a LOCKT request
1637 */
8687b63a 1638static int nfs4_xdr_enc_lockt(struct rpc_rqst *req, __be32 *p, struct nfs_lockt_args *args)
1da177e4
LT
1639{
1640 struct xdr_stream xdr;
1641 struct compound_hdr hdr = {
d017931c 1642 .nops = 0,
1da177e4 1643 };
1da177e4
LT
1644
1645 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1646 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1647 encode_putfh(&xdr, args->fh, &hdr);
1648 encode_lockt(&xdr, args, &hdr);
d017931c 1649 encode_nops(&hdr);
cf8cdbe5 1650 return 0;
1da177e4
LT
1651}
1652
1653/*
1654 * Encode a LOCKU request
1655 */
8687b63a 1656static int nfs4_xdr_enc_locku(struct rpc_rqst *req, __be32 *p, struct nfs_locku_args *args)
1da177e4
LT
1657{
1658 struct xdr_stream xdr;
1659 struct compound_hdr hdr = {
d017931c 1660 .nops = 0,
1da177e4 1661 };
1da177e4
LT
1662
1663 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1664 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1665 encode_putfh(&xdr, args->fh, &hdr);
1666 encode_locku(&xdr, args, &hdr);
d017931c 1667 encode_nops(&hdr);
cf8cdbe5 1668 return 0;
1da177e4
LT
1669}
1670
1671/*
1672 * Encode a READLINK request
1673 */
8687b63a 1674static int nfs4_xdr_enc_readlink(struct rpc_rqst *req, __be32 *p, const struct nfs4_readlink *args)
1da177e4
LT
1675{
1676 struct xdr_stream xdr;
1677 struct compound_hdr hdr = {
d017931c 1678 .nops = 0,
1da177e4 1679 };
e3a535e1
TM
1680 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
1681 unsigned int replen;
1da177e4
LT
1682
1683 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1684 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1685 encode_putfh(&xdr, args->fh, &hdr);
1686 encode_readlink(&xdr, args, req, &hdr);
e3a535e1
TM
1687
1688 /* set up reply kvec
1689 * toplevel_status + taglen + rescount + OP_PUTFH + status
1690 * + OP_READLINK + status + string length = 8
1691 */
1692 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_readlink_sz) << 2;
1693 xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages,
1694 args->pgbase, args->pglen);
d017931c 1695 encode_nops(&hdr);
cf8cdbe5 1696 return 0;
1da177e4
LT
1697}
1698
1699/*
1700 * Encode a READDIR request
1701 */
8687b63a 1702static int nfs4_xdr_enc_readdir(struct rpc_rqst *req, __be32 *p, const struct nfs4_readdir_arg *args)
1da177e4
LT
1703{
1704 struct xdr_stream xdr;
1705 struct compound_hdr hdr = {
d017931c 1706 .nops = 0,
1da177e4 1707 };
d6ac02df
TM
1708 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
1709 int replen;
1da177e4
LT
1710
1711 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1712 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1713 encode_putfh(&xdr, args->fh, &hdr);
1714 encode_readdir(&xdr, args, req, &hdr);
d6ac02df
TM
1715
1716 /* set up reply kvec
1717 * toplevel_status + taglen + rescount + OP_PUTFH + status
1718 * + OP_READDIR + status + verifer(2) = 9
1719 */
1720 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_readdir_sz) << 2;
1721 xdr_inline_pages(&req->rq_rcv_buf, replen, args->pages,
1722 args->pgbase, args->count);
1723 dprintk("%s: inlined page args = (%u, %p, %u, %u)\n",
3110ff80 1724 __func__, replen, args->pages,
d6ac02df 1725 args->pgbase, args->count);
d017931c 1726 encode_nops(&hdr);
cf8cdbe5 1727 return 0;
1da177e4
LT
1728}
1729
1730/*
1731 * Encode a READ request
1732 */
8687b63a 1733static int nfs4_xdr_enc_read(struct rpc_rqst *req, __be32 *p, struct nfs_readargs *args)
1da177e4 1734{
1be27f36 1735 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
1da177e4
LT
1736 struct xdr_stream xdr;
1737 struct compound_hdr hdr = {
d017931c 1738 .nops = 0,
1da177e4 1739 };
cf8cdbe5 1740 int replen;
1da177e4
LT
1741
1742 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1743 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1744 encode_putfh(&xdr, args->fh, &hdr);
1745 encode_read(&xdr, args, &hdr);
1da177e4
LT
1746
1747 /* set up reply kvec
1748 * toplevel status + taglen=0 + rescount + OP_PUTFH + status
1749 * + OP_READ + status + eof + datalen = 9
1750 */
1751 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_read_sz) << 2;
1752 xdr_inline_pages(&req->rq_rcv_buf, replen,
1753 args->pages, args->pgbase, args->count);
4f22ccc3 1754 req->rq_rcv_buf.flags |= XDRBUF_READ;
d017931c 1755 encode_nops(&hdr);
cf8cdbe5 1756 return 0;
1da177e4
LT
1757}
1758
1759/*
1760 * Encode an SETATTR request
1761 */
8687b63a 1762static int nfs4_xdr_enc_setattr(struct rpc_rqst *req, __be32 *p, struct nfs_setattrargs *args)
1da177e4 1763{
05d564fe
AA
1764 struct xdr_stream xdr;
1765 struct compound_hdr hdr = {
d017931c 1766 .nops = 0,
05d564fe 1767 };
05d564fe
AA
1768
1769 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1770 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1771 encode_putfh(&xdr, args->fh, &hdr);
1772 encode_setattr(&xdr, args, args->server, &hdr);
1773 encode_getfattr(&xdr, args->bitmask, &hdr);
d017931c 1774 encode_nops(&hdr);
cf8cdbe5 1775 return 0;
1da177e4
LT
1776}
1777
029d105e
BF
1778/*
1779 * Encode a GETACL request
1780 */
1781static int
8687b63a 1782nfs4_xdr_enc_getacl(struct rpc_rqst *req, __be32 *p,
029d105e
BF
1783 struct nfs_getaclargs *args)
1784{
1785 struct xdr_stream xdr;
1be27f36 1786 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
029d105e 1787 struct compound_hdr hdr = {
d017931c 1788 .nops = 0,
029d105e 1789 };
cf8cdbe5 1790 int replen;
029d105e
BF
1791
1792 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1793 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1794 encode_putfh(&xdr, args->fh, &hdr);
1795 encode_getattr_two(&xdr, FATTR4_WORD0_ACL, 0, &hdr);
1796
029d105e
BF
1797 /* set up reply buffer: */
1798 replen = (RPC_REPHDRSIZE + auth->au_rslack + NFS4_dec_getacl_sz) << 2;
1799 xdr_inline_pages(&req->rq_rcv_buf, replen,
1800 args->acl_pages, args->acl_pgbase, args->acl_len);
d017931c 1801 encode_nops(&hdr);
cf8cdbe5 1802 return 0;
029d105e
BF
1803}
1804
1da177e4
LT
1805/*
1806 * Encode a WRITE request
1807 */
8687b63a 1808static int nfs4_xdr_enc_write(struct rpc_rqst *req, __be32 *p, struct nfs_writeargs *args)
1da177e4
LT
1809{
1810 struct xdr_stream xdr;
1811 struct compound_hdr hdr = {
d017931c 1812 .nops = 0,
1da177e4 1813 };
1da177e4
LT
1814
1815 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1816 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1817 encode_putfh(&xdr, args->fh, &hdr);
1818 encode_write(&xdr, args, &hdr);
4f22ccc3 1819 req->rq_snd_buf.flags |= XDRBUF_WRITE;
cf8cdbe5 1820 encode_getfattr(&xdr, args->bitmask, &hdr);
d017931c 1821 encode_nops(&hdr);
cf8cdbe5 1822 return 0;
1da177e4
LT
1823}
1824
1825/*
1826 * a COMMIT request
1827 */
8687b63a 1828static int nfs4_xdr_enc_commit(struct rpc_rqst *req, __be32 *p, struct nfs_writeargs *args)
1da177e4
LT
1829{
1830 struct xdr_stream xdr;
1831 struct compound_hdr hdr = {
d017931c 1832 .nops = 0,
1da177e4 1833 };
1da177e4
LT
1834
1835 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1836 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1837 encode_putfh(&xdr, args->fh, &hdr);
1838 encode_commit(&xdr, args, &hdr);
1839 encode_getfattr(&xdr, args->bitmask, &hdr);
d017931c 1840 encode_nops(&hdr);
cf8cdbe5 1841 return 0;
1da177e4
LT
1842}
1843
1844/*
1845 * FSINFO request
1846 */
8687b63a 1847static int nfs4_xdr_enc_fsinfo(struct rpc_rqst *req, __be32 *p, struct nfs4_fsinfo_arg *args)
1da177e4
LT
1848{
1849 struct xdr_stream xdr;
1850 struct compound_hdr hdr = {
d017931c 1851 .nops = 0,
1da177e4 1852 };
1da177e4
LT
1853
1854 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1855 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1856 encode_putfh(&xdr, args->fh, &hdr);
1857 encode_fsinfo(&xdr, args->bitmask, &hdr);
d017931c 1858 encode_nops(&hdr);
cf8cdbe5 1859 return 0;
1da177e4
LT
1860}
1861
1862/*
1863 * a PATHCONF request
1864 */
8687b63a 1865static int nfs4_xdr_enc_pathconf(struct rpc_rqst *req, __be32 *p, const struct nfs4_pathconf_arg *args)
1da177e4 1866{
1da177e4
LT
1867 struct xdr_stream xdr;
1868 struct compound_hdr hdr = {
d017931c 1869 .nops = 0,
1da177e4 1870 };
1da177e4
LT
1871
1872 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1873 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1874 encode_putfh(&xdr, args->fh, &hdr);
1875 encode_getattr_one(&xdr, args->bitmask[0] & nfs4_pathconf_bitmap[0],
1876 &hdr);
d017931c 1877 encode_nops(&hdr);
cf8cdbe5 1878 return 0;
1da177e4
LT
1879}
1880
1881/*
1882 * a STATFS request
1883 */
8687b63a 1884static int nfs4_xdr_enc_statfs(struct rpc_rqst *req, __be32 *p, const struct nfs4_statfs_arg *args)
1da177e4 1885{
1da177e4
LT
1886 struct xdr_stream xdr;
1887 struct compound_hdr hdr = {
d017931c 1888 .nops = 0,
1da177e4 1889 };
1da177e4
LT
1890
1891 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1892 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1893 encode_putfh(&xdr, args->fh, &hdr);
1894 encode_getattr_two(&xdr, args->bitmask[0] & nfs4_statfs_bitmap[0],
1895 args->bitmask[1] & nfs4_statfs_bitmap[1], &hdr);
d017931c 1896 encode_nops(&hdr);
cf8cdbe5 1897 return 0;
1da177e4
LT
1898}
1899
1900/*
1901 * GETATTR_BITMAP request
1902 */
8687b63a 1903static int nfs4_xdr_enc_server_caps(struct rpc_rqst *req, __be32 *p, const struct nfs_fh *fhandle)
1da177e4
LT
1904{
1905 struct xdr_stream xdr;
1906 struct compound_hdr hdr = {
d017931c 1907 .nops = 0,
1da177e4 1908 };
1da177e4
LT
1909
1910 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1911 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1912 encode_putfh(&xdr, fhandle, &hdr);
1913 encode_getattr_one(&xdr, FATTR4_WORD0_SUPPORTED_ATTRS|
1914 FATTR4_WORD0_LINK_SUPPORT|
1915 FATTR4_WORD0_SYMLINK_SUPPORT|
1916 FATTR4_WORD0_ACLSUPPORT, &hdr);
d017931c 1917 encode_nops(&hdr);
cf8cdbe5 1918 return 0;
1da177e4
LT
1919}
1920
1921/*
1922 * a RENEW request
1923 */
8687b63a 1924static int nfs4_xdr_enc_renew(struct rpc_rqst *req, __be32 *p, struct nfs_client *clp)
1da177e4
LT
1925{
1926 struct xdr_stream xdr;
1927 struct compound_hdr hdr = {
d017931c 1928 .nops = 0,
1da177e4
LT
1929 };
1930
1931 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1932 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5 1933 encode_renew(&xdr, clp, &hdr);
d017931c 1934 encode_nops(&hdr);
cf8cdbe5 1935 return 0;
1da177e4
LT
1936}
1937
1938/*
1939 * a SETCLIENTID request
1940 */
8687b63a 1941static int nfs4_xdr_enc_setclientid(struct rpc_rqst *req, __be32 *p, struct nfs4_setclientid *sc)
1da177e4
LT
1942{
1943 struct xdr_stream xdr;
1944 struct compound_hdr hdr = {
d017931c 1945 .nops = 0,
1da177e4
LT
1946 };
1947
1948 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1949 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5 1950 encode_setclientid(&xdr, sc, &hdr);
d017931c 1951 encode_nops(&hdr);
cf8cdbe5 1952 return 0;
1da177e4
LT
1953}
1954
1955/*
1956 * a SETCLIENTID_CONFIRM request
1957 */
8687b63a 1958static int nfs4_xdr_enc_setclientid_confirm(struct rpc_rqst *req, __be32 *p, struct nfs_client *clp)
1da177e4
LT
1959{
1960 struct xdr_stream xdr;
1961 struct compound_hdr hdr = {
d017931c 1962 .nops = 0,
1da177e4
LT
1963 };
1964 const u32 lease_bitmap[2] = { FATTR4_WORD0_LEASE_TIME, 0 };
1da177e4
LT
1965
1966 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1967 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1968 encode_setclientid_confirm(&xdr, clp, &hdr);
1969 encode_putrootfh(&xdr, &hdr);
1970 encode_fsinfo(&xdr, lease_bitmap, &hdr);
d017931c 1971 encode_nops(&hdr);
cf8cdbe5 1972 return 0;
1da177e4
LT
1973}
1974
1975/*
1976 * DELEGRETURN request
1977 */
8687b63a 1978static int nfs4_xdr_enc_delegreturn(struct rpc_rqst *req, __be32 *p, const struct nfs4_delegreturnargs *args)
1da177e4
LT
1979{
1980 struct xdr_stream xdr;
1981 struct compound_hdr hdr = {
d017931c 1982 .nops = 0,
1da177e4 1983 };
1da177e4
LT
1984
1985 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
1986 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
1987 encode_putfh(&xdr, args->fhandle, &hdr);
1988 encode_delegreturn(&xdr, args->stateid, &hdr);
1989 encode_getfattr(&xdr, args->bitmask, &hdr);
d017931c 1990 encode_nops(&hdr);
cf8cdbe5 1991 return 0;
1da177e4
LT
1992}
1993
683b57b4
TM
1994/*
1995 * Encode FS_LOCATIONS request
1996 */
8687b63a 1997static int nfs4_xdr_enc_fs_locations(struct rpc_rqst *req, __be32 *p, struct nfs4_fs_locations_arg *args)
683b57b4
TM
1998{
1999 struct xdr_stream xdr;
2000 struct compound_hdr hdr = {
d017931c 2001 .nops = 0,
683b57b4 2002 };
1be27f36 2003 struct rpc_auth *auth = req->rq_task->tk_msg.rpc_cred->cr_auth;
683b57b4 2004 int replen;
683b57b4
TM
2005
2006 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
2007 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5
AA
2008 encode_putfh(&xdr, args->dir_fh, &hdr);
2009 encode_lookup(&xdr, args->name, &hdr);
2010 encode_fs_locations(&xdr, args->bitmask, &hdr);
2011
683b57b4 2012 /* set up reply
830b8e33 2013 * toplevel_status + OP_PUTFH + status
683b57b4
TM
2014 * + OP_LOOKUP + status + OP_GETATTR + status = 7
2015 */
2016 replen = (RPC_REPHDRSIZE + auth->au_rslack + 7) << 2;
2017 xdr_inline_pages(&req->rq_rcv_buf, replen, &args->page,
2018 0, PAGE_SIZE);
d017931c 2019 encode_nops(&hdr);
cf8cdbe5 2020 return 0;
683b57b4
TM
2021}
2022
1da177e4
LT
2023/*
2024 * START OF "GENERIC" DECODE ROUTINES.
2025 * These may look a little ugly since they are imported from a "generic"
2026 * set of XDR encode/decode routines which are intended to be shared by
2027 * all of our NFSv4 implementations (OpenBSD, MacOS X...).
2028 *
2029 * If the pain of reading these is too great, it should be a straightforward
2030 * task to translate them into Linux-specific versions which are more
2031 * consistent with the style used in NFSv2/v3...
2032 */
2033#define READ32(x) (x) = ntohl(*p++)
2034#define READ64(x) do { \
2035 (x) = (u64)ntohl(*p++) << 32; \
2036 (x) |= ntohl(*p++); \
2037} while (0)
2038#define READTIME(x) do { \
2039 p++; \
2040 (x.tv_sec) = ntohl(*p++); \
2041 (x.tv_nsec) = ntohl(*p++); \
2042} while (0)
2043#define COPYMEM(x,nbytes) do { \
2044 memcpy((x), p, nbytes); \
2045 p += XDR_QUADLEN(nbytes); \
2046} while (0)
2047
2048#define READ_BUF(nbytes) do { \
2049 p = xdr_inline_decode(xdr, nbytes); \
e4cc6ee2 2050 if (unlikely(!p)) { \
fe82a183 2051 dprintk("nfs: %s: prematurely hit end of receive" \
3110ff80 2052 " buffer\n", __func__); \
fe82a183 2053 dprintk("nfs: %s: xdr->p=%p, bytes=%u, xdr->end=%p\n", \
3110ff80 2054 __func__, xdr->p, nbytes, xdr->end); \
1da177e4
LT
2055 return -EIO; \
2056 } \
2057} while (0)
2058
683b57b4 2059static int decode_opaque_inline(struct xdr_stream *xdr, unsigned int *len, char **string)
1da177e4 2060{
8687b63a 2061 __be32 *p;
1da177e4
LT
2062
2063 READ_BUF(4);
2064 READ32(*len);
2065 READ_BUF(*len);
2066 *string = (char *)p;
2067 return 0;
2068}
2069
2070static int decode_compound_hdr(struct xdr_stream *xdr, struct compound_hdr *hdr)
2071{
8687b63a 2072 __be32 *p;
1da177e4
LT
2073
2074 READ_BUF(8);
2075 READ32(hdr->status);
2076 READ32(hdr->taglen);
6c0195a4 2077
1da177e4
LT
2078 READ_BUF(hdr->taglen + 4);
2079 hdr->tag = (char *)p;
2080 p += XDR_QUADLEN(hdr->taglen);
2081 READ32(hdr->nops);
aadf6152
BH
2082 if (unlikely(hdr->nops < 1))
2083 return nfs4_stat_to_errno(hdr->status);
1da177e4
LT
2084 return 0;
2085}
2086
2087static int decode_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
2088{
8687b63a 2089 __be32 *p;
1da177e4
LT
2090 uint32_t opnum;
2091 int32_t nfserr;
2092
2093 READ_BUF(8);
2094 READ32(opnum);
2095 if (opnum != expected) {
fe82a183
CL
2096 dprintk("nfs: Server returned operation"
2097 " %d but we issued a request for %d\n",
1da177e4
LT
2098 opnum, expected);
2099 return -EIO;
2100 }
2101 READ32(nfserr);
2102 if (nfserr != NFS_OK)
856dff3d 2103 return nfs4_stat_to_errno(nfserr);
1da177e4
LT
2104 return 0;
2105}
2106
2107/* Dummy routine */
adfa6f98 2108static int decode_ace(struct xdr_stream *xdr, void *ace, struct nfs_client *clp)
1da177e4 2109{
8687b63a 2110 __be32 *p;
683b57b4 2111 unsigned int strlen;
1da177e4
LT
2112 char *str;
2113
2114 READ_BUF(12);
2115 return decode_opaque_inline(xdr, &strlen, &str);
2116}
2117
2118static int decode_attr_bitmap(struct xdr_stream *xdr, uint32_t *bitmap)
2119{
8687b63a
AV
2120 uint32_t bmlen;
2121 __be32 *p;
1da177e4
LT
2122
2123 READ_BUF(4);
2124 READ32(bmlen);
2125
2126 bitmap[0] = bitmap[1] = 0;
2127 READ_BUF((bmlen << 2));
2128 if (bmlen > 0) {
2129 READ32(bitmap[0]);
2130 if (bmlen > 1)
2131 READ32(bitmap[1]);
2132 }
2133 return 0;
2134}
2135
8687b63a 2136static inline int decode_attr_length(struct xdr_stream *xdr, uint32_t *attrlen, __be32 **savep)
1da177e4 2137{
8687b63a 2138 __be32 *p;
1da177e4
LT
2139
2140 READ_BUF(4);
2141 READ32(*attrlen);
2142 *savep = xdr->p;
2143 return 0;
2144}
2145
2146static int decode_attr_supported(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *bitmask)
2147{
2148 if (likely(bitmap[0] & FATTR4_WORD0_SUPPORTED_ATTRS)) {
2149 decode_attr_bitmap(xdr, bitmask);
2150 bitmap[0] &= ~FATTR4_WORD0_SUPPORTED_ATTRS;
2151 } else
2152 bitmask[0] = bitmask[1] = 0;
44109241 2153 dprintk("%s: bitmask=%08x:%08x\n", __func__, bitmask[0], bitmask[1]);
1da177e4
LT
2154 return 0;
2155}
2156
2157static int decode_attr_type(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *type)
2158{
8687b63a 2159 __be32 *p;
409924e4 2160 int ret = 0;
1da177e4
LT
2161
2162 *type = 0;
2163 if (unlikely(bitmap[0] & (FATTR4_WORD0_TYPE - 1U)))
2164 return -EIO;
2165 if (likely(bitmap[0] & FATTR4_WORD0_TYPE)) {
2166 READ_BUF(4);
2167 READ32(*type);
2168 if (*type < NF4REG || *type > NF4NAMEDATTR) {
3110ff80 2169 dprintk("%s: bad type %d\n", __func__, *type);
1da177e4
LT
2170 return -EIO;
2171 }
2172 bitmap[0] &= ~FATTR4_WORD0_TYPE;
409924e4 2173 ret = NFS_ATTR_FATTR_TYPE;
1da177e4 2174 }
bca79478 2175 dprintk("%s: type=0%o\n", __func__, nfs_type2fmt[*type]);
409924e4 2176 return ret;
1da177e4
LT
2177}
2178
2179static int decode_attr_change(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *change)
2180{
8687b63a 2181 __be32 *p;
409924e4 2182 int ret = 0;
1da177e4
LT
2183
2184 *change = 0;
2185 if (unlikely(bitmap[0] & (FATTR4_WORD0_CHANGE - 1U)))
2186 return -EIO;
2187 if (likely(bitmap[0] & FATTR4_WORD0_CHANGE)) {
2188 READ_BUF(8);
2189 READ64(*change);
2190 bitmap[0] &= ~FATTR4_WORD0_CHANGE;
409924e4 2191 ret = NFS_ATTR_FATTR_CHANGE;
1da177e4 2192 }
3110ff80 2193 dprintk("%s: change attribute=%Lu\n", __func__,
1da177e4 2194 (unsigned long long)*change);
409924e4 2195 return ret;
1da177e4
LT
2196}
2197
2198static int decode_attr_size(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *size)
2199{
8687b63a 2200 __be32 *p;
409924e4 2201 int ret = 0;
1da177e4
LT
2202
2203 *size = 0;
2204 if (unlikely(bitmap[0] & (FATTR4_WORD0_SIZE - 1U)))
2205 return -EIO;
2206 if (likely(bitmap[0] & FATTR4_WORD0_SIZE)) {
2207 READ_BUF(8);
2208 READ64(*size);
2209 bitmap[0] &= ~FATTR4_WORD0_SIZE;
409924e4 2210 ret = NFS_ATTR_FATTR_SIZE;
1da177e4 2211 }
3110ff80 2212 dprintk("%s: file size=%Lu\n", __func__, (unsigned long long)*size);
409924e4 2213 return ret;
1da177e4
LT
2214}
2215
2216static int decode_attr_link_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2217{
8687b63a 2218 __be32 *p;
1da177e4
LT
2219
2220 *res = 0;
2221 if (unlikely(bitmap[0] & (FATTR4_WORD0_LINK_SUPPORT - 1U)))
2222 return -EIO;
2223 if (likely(bitmap[0] & FATTR4_WORD0_LINK_SUPPORT)) {
2224 READ_BUF(4);
2225 READ32(*res);
2226 bitmap[0] &= ~FATTR4_WORD0_LINK_SUPPORT;
2227 }
3110ff80 2228 dprintk("%s: link support=%s\n", __func__, *res == 0 ? "false" : "true");
1da177e4
LT
2229 return 0;
2230}
2231
2232static int decode_attr_symlink_support(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2233{
8687b63a 2234 __be32 *p;
1da177e4
LT
2235
2236 *res = 0;
2237 if (unlikely(bitmap[0] & (FATTR4_WORD0_SYMLINK_SUPPORT - 1U)))
2238 return -EIO;
2239 if (likely(bitmap[0] & FATTR4_WORD0_SYMLINK_SUPPORT)) {
2240 READ_BUF(4);
2241 READ32(*res);
2242 bitmap[0] &= ~FATTR4_WORD0_SYMLINK_SUPPORT;
2243 }
3110ff80 2244 dprintk("%s: symlink support=%s\n", __func__, *res == 0 ? "false" : "true");
1da177e4
LT
2245 return 0;
2246}
2247
8b4bdcf8 2248static int decode_attr_fsid(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs_fsid *fsid)
1da177e4 2249{
8687b63a 2250 __be32 *p;
409924e4 2251 int ret = 0;
1da177e4
LT
2252
2253 fsid->major = 0;
2254 fsid->minor = 0;
2255 if (unlikely(bitmap[0] & (FATTR4_WORD0_FSID - 1U)))
2256 return -EIO;
2257 if (likely(bitmap[0] & FATTR4_WORD0_FSID)) {
2258 READ_BUF(16);
2259 READ64(fsid->major);
2260 READ64(fsid->minor);
2261 bitmap[0] &= ~FATTR4_WORD0_FSID;
409924e4 2262 ret = NFS_ATTR_FATTR_FSID;
1da177e4 2263 }
3110ff80 2264 dprintk("%s: fsid=(0x%Lx/0x%Lx)\n", __func__,
1da177e4
LT
2265 (unsigned long long)fsid->major,
2266 (unsigned long long)fsid->minor);
409924e4 2267 return ret;
1da177e4
LT
2268}
2269
2270static int decode_attr_lease_time(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2271{
8687b63a 2272 __be32 *p;
1da177e4
LT
2273
2274 *res = 60;
2275 if (unlikely(bitmap[0] & (FATTR4_WORD0_LEASE_TIME - 1U)))
2276 return -EIO;
2277 if (likely(bitmap[0] & FATTR4_WORD0_LEASE_TIME)) {
2278 READ_BUF(4);
2279 READ32(*res);
2280 bitmap[0] &= ~FATTR4_WORD0_LEASE_TIME;
2281 }
3110ff80 2282 dprintk("%s: file size=%u\n", __func__, (unsigned int)*res);
1da177e4
LT
2283 return 0;
2284}
2285
2286static int decode_attr_aclsupport(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2287{
8687b63a 2288 __be32 *p;
1da177e4
LT
2289
2290 *res = ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL;
2291 if (unlikely(bitmap[0] & (FATTR4_WORD0_ACLSUPPORT - 1U)))
2292 return -EIO;
2293 if (likely(bitmap[0] & FATTR4_WORD0_ACLSUPPORT)) {
2294 READ_BUF(4);
2295 READ32(*res);
2296 bitmap[0] &= ~FATTR4_WORD0_ACLSUPPORT;
2297 }
3110ff80 2298 dprintk("%s: ACLs supported=%u\n", __func__, (unsigned int)*res);
1da177e4
LT
2299 return 0;
2300}
2301
2302static int decode_attr_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
2303{
8687b63a 2304 __be32 *p;
409924e4 2305 int ret = 0;
1da177e4
LT
2306
2307 *fileid = 0;
2308 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILEID - 1U)))
2309 return -EIO;
2310 if (likely(bitmap[0] & FATTR4_WORD0_FILEID)) {
2311 READ_BUF(8);
2312 READ64(*fileid);
2313 bitmap[0] &= ~FATTR4_WORD0_FILEID;
409924e4 2314 ret = NFS_ATTR_FATTR_FILEID;
1da177e4 2315 }
3110ff80 2316 dprintk("%s: fileid=%Lu\n", __func__, (unsigned long long)*fileid);
409924e4 2317 return ret;
1da177e4
LT
2318}
2319
99baf625
MN
2320static int decode_attr_mounted_on_fileid(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *fileid)
2321{
8687b63a 2322 __be32 *p;
409924e4 2323 int ret = 0;
99baf625
MN
2324
2325 *fileid = 0;
2326 if (unlikely(bitmap[1] & (FATTR4_WORD1_MOUNTED_ON_FILEID - 1U)))
2327 return -EIO;
2328 if (likely(bitmap[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)) {
2329 READ_BUF(8);
2330 READ64(*fileid);
2331 bitmap[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
409924e4 2332 ret = NFS_ATTR_FATTR_FILEID;
99baf625 2333 }
3110ff80 2334 dprintk("%s: fileid=%Lu\n", __func__, (unsigned long long)*fileid);
409924e4 2335 return ret;
99baf625
MN
2336}
2337
1da177e4
LT
2338static int decode_attr_files_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2339{
8687b63a 2340 __be32 *p;
1da177e4
LT
2341 int status = 0;
2342
2343 *res = 0;
2344 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_AVAIL - 1U)))
2345 return -EIO;
2346 if (likely(bitmap[0] & FATTR4_WORD0_FILES_AVAIL)) {
2347 READ_BUF(8);
2348 READ64(*res);
2349 bitmap[0] &= ~FATTR4_WORD0_FILES_AVAIL;
2350 }
3110ff80 2351 dprintk("%s: files avail=%Lu\n", __func__, (unsigned long long)*res);
1da177e4
LT
2352 return status;
2353}
2354
2355static int decode_attr_files_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2356{
8687b63a 2357 __be32 *p;
1da177e4
LT
2358 int status = 0;
2359
2360 *res = 0;
2361 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_FREE - 1U)))
2362 return -EIO;
2363 if (likely(bitmap[0] & FATTR4_WORD0_FILES_FREE)) {
2364 READ_BUF(8);
2365 READ64(*res);
2366 bitmap[0] &= ~FATTR4_WORD0_FILES_FREE;
2367 }
3110ff80 2368 dprintk("%s: files free=%Lu\n", __func__, (unsigned long long)*res);
1da177e4
LT
2369 return status;
2370}
2371
2372static int decode_attr_files_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2373{
8687b63a 2374 __be32 *p;
1da177e4
LT
2375 int status = 0;
2376
2377 *res = 0;
2378 if (unlikely(bitmap[0] & (FATTR4_WORD0_FILES_TOTAL - 1U)))
2379 return -EIO;
2380 if (likely(bitmap[0] & FATTR4_WORD0_FILES_TOTAL)) {
2381 READ_BUF(8);
2382 READ64(*res);
2383 bitmap[0] &= ~FATTR4_WORD0_FILES_TOTAL;
2384 }
3110ff80 2385 dprintk("%s: files total=%Lu\n", __func__, (unsigned long long)*res);
1da177e4
LT
2386 return status;
2387}
2388
7aaa0b3b
MN
2389static int decode_pathname(struct xdr_stream *xdr, struct nfs4_pathname *path)
2390{
464ad6b1 2391 u32 n;
8687b63a 2392 __be32 *p;
7aaa0b3b
MN
2393 int status = 0;
2394
2395 READ_BUF(4);
2396 READ32(n);
33a43f28
AA
2397 if (n == 0)
2398 goto root_path;
7aaa0b3b
MN
2399 dprintk("path ");
2400 path->ncomponents = 0;
2401 while (path->ncomponents < n) {
2402 struct nfs4_string *component = &path->components[path->ncomponents];
2403 status = decode_opaque_inline(xdr, &component->len, &component->data);
2404 if (unlikely(status != 0))
2405 goto out_eio;
2406 if (path->ncomponents != n)
2407 dprintk("/");
2408 dprintk("%s", component->data);
2409 if (path->ncomponents < NFS4_PATHNAME_MAXCOMPONENTS)
2410 path->ncomponents++;
2411 else {
2412 dprintk("cannot parse %d components in path\n", n);
2413 goto out_eio;
2414 }
2415 }
2416out:
2417 dprintk("\n");
2418 return status;
33a43f28
AA
2419root_path:
2420/* a root pathname is sent as a zero component4 */
2421 path->ncomponents = 1;
2422 path->components[0].len=0;
2423 path->components[0].data=NULL;
2424 dprintk("path /\n");
2425 goto out;
7aaa0b3b
MN
2426out_eio:
2427 dprintk(" status %d", status);
2428 status = -EIO;
2429 goto out;
2430}
2431
2432static int decode_attr_fs_locations(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs4_fs_locations *res)
683b57b4
TM
2433{
2434 int n;
8687b63a 2435 __be32 *p;
683b57b4
TM
2436 int status = -EIO;
2437
2438 if (unlikely(bitmap[0] & (FATTR4_WORD0_FS_LOCATIONS -1U)))
2439 goto out;
2440 status = 0;
2441 if (unlikely(!(bitmap[0] & FATTR4_WORD0_FS_LOCATIONS)))
2442 goto out;
3110ff80 2443 dprintk("%s: fsroot ", __func__);
7aaa0b3b 2444 status = decode_pathname(xdr, &res->fs_path);
683b57b4
TM
2445 if (unlikely(status != 0))
2446 goto out;
2447 READ_BUF(4);
2448 READ32(n);
2449 if (n <= 0)
2450 goto out_eio;
2451 res->nlocations = 0;
2452 while (res->nlocations < n) {
464ad6b1 2453 u32 m;
7aaa0b3b 2454 struct nfs4_fs_location *loc = &res->locations[res->nlocations];
683b57b4 2455
7aaa0b3b
MN
2456 READ_BUF(4);
2457 READ32(m);
7aaa0b3b
MN
2458
2459 loc->nservers = 0;
3110ff80 2460 dprintk("%s: servers ", __func__);
7aaa0b3b
MN
2461 while (loc->nservers < m) {
2462 struct nfs4_string *server = &loc->servers[loc->nservers];
2463 status = decode_opaque_inline(xdr, &server->len, &server->data);
2464 if (unlikely(status != 0))
2465 goto out_eio;
2466 dprintk("%s ", server->data);
2467 if (loc->nservers < NFS4_FS_LOCATION_MAXSERVERS)
2468 loc->nservers++;
2469 else {
464ad6b1
CL
2470 unsigned int i;
2471 dprintk("%s: using first %u of %u servers "
2472 "returned for location %u\n",
3110ff80 2473 __func__,
464ad6b1
CL
2474 NFS4_FS_LOCATION_MAXSERVERS,
2475 m, res->nlocations);
7aaa0b3b 2476 for (i = loc->nservers; i < m; i++) {
2e42c3e2 2477 unsigned int len;
7aaa0b3b
MN
2478 char *data;
2479 status = decode_opaque_inline(xdr, &len, &data);
2480 if (unlikely(status != 0))
2481 goto out_eio;
2482 }
2483 }
2484 }
2485 status = decode_pathname(xdr, &loc->rootpath);
683b57b4
TM
2486 if (unlikely(status != 0))
2487 goto out_eio;
7aaa0b3b 2488 if (res->nlocations < NFS4_FS_LOCATIONS_MAXENTRIES)
683b57b4
TM
2489 res->nlocations++;
2490 }
409924e4
TM
2491 if (res->nlocations != 0)
2492 status = NFS_ATTR_FATTR_V4_REFERRAL;
683b57b4 2493out:
3110ff80 2494 dprintk("%s: fs_locations done, error = %d\n", __func__, status);
683b57b4
TM
2495 return status;
2496out_eio:
2497 status = -EIO;
2498 goto out;
2499}
2500
1da177e4
LT
2501static int decode_attr_maxfilesize(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2502{
8687b63a 2503 __be32 *p;
1da177e4
LT
2504 int status = 0;
2505
2506 *res = 0;
2507 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXFILESIZE - 1U)))
2508 return -EIO;
2509 if (likely(bitmap[0] & FATTR4_WORD0_MAXFILESIZE)) {
2510 READ_BUF(8);
2511 READ64(*res);
2512 bitmap[0] &= ~FATTR4_WORD0_MAXFILESIZE;
2513 }
3110ff80 2514 dprintk("%s: maxfilesize=%Lu\n", __func__, (unsigned long long)*res);
1da177e4
LT
2515 return status;
2516}
2517
2518static int decode_attr_maxlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxlink)
2519{
8687b63a 2520 __be32 *p;
1da177e4
LT
2521 int status = 0;
2522
2523 *maxlink = 1;
2524 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXLINK - 1U)))
2525 return -EIO;
2526 if (likely(bitmap[0] & FATTR4_WORD0_MAXLINK)) {
2527 READ_BUF(4);
2528 READ32(*maxlink);
2529 bitmap[0] &= ~FATTR4_WORD0_MAXLINK;
2530 }
3110ff80 2531 dprintk("%s: maxlink=%u\n", __func__, *maxlink);
1da177e4
LT
2532 return status;
2533}
2534
2535static int decode_attr_maxname(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *maxname)
2536{
8687b63a 2537 __be32 *p;
1da177e4
LT
2538 int status = 0;
2539
2540 *maxname = 1024;
2541 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXNAME - 1U)))
2542 return -EIO;
2543 if (likely(bitmap[0] & FATTR4_WORD0_MAXNAME)) {
2544 READ_BUF(4);
2545 READ32(*maxname);
2546 bitmap[0] &= ~FATTR4_WORD0_MAXNAME;
2547 }
3110ff80 2548 dprintk("%s: maxname=%u\n", __func__, *maxname);
1da177e4
LT
2549 return status;
2550}
2551
2552static int decode_attr_maxread(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2553{
8687b63a 2554 __be32 *p;
1da177e4
LT
2555 int status = 0;
2556
2557 *res = 1024;
2558 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXREAD - 1U)))
2559 return -EIO;
2560 if (likely(bitmap[0] & FATTR4_WORD0_MAXREAD)) {
2561 uint64_t maxread;
2562 READ_BUF(8);
2563 READ64(maxread);
2564 if (maxread > 0x7FFFFFFF)
2565 maxread = 0x7FFFFFFF;
2566 *res = (uint32_t)maxread;
2567 bitmap[0] &= ~FATTR4_WORD0_MAXREAD;
2568 }
3110ff80 2569 dprintk("%s: maxread=%lu\n", __func__, (unsigned long)*res);
1da177e4
LT
2570 return status;
2571}
2572
2573static int decode_attr_maxwrite(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *res)
2574{
8687b63a 2575 __be32 *p;
1da177e4
LT
2576 int status = 0;
2577
2578 *res = 1024;
2579 if (unlikely(bitmap[0] & (FATTR4_WORD0_MAXWRITE - 1U)))
2580 return -EIO;
2581 if (likely(bitmap[0] & FATTR4_WORD0_MAXWRITE)) {
2582 uint64_t maxwrite;
2583 READ_BUF(8);
2584 READ64(maxwrite);
2585 if (maxwrite > 0x7FFFFFFF)
2586 maxwrite = 0x7FFFFFFF;
2587 *res = (uint32_t)maxwrite;
2588 bitmap[0] &= ~FATTR4_WORD0_MAXWRITE;
2589 }
3110ff80 2590 dprintk("%s: maxwrite=%lu\n", __func__, (unsigned long)*res);
1da177e4
LT
2591 return status;
2592}
2593
bca79478 2594static int decode_attr_mode(struct xdr_stream *xdr, uint32_t *bitmap, umode_t *mode)
1da177e4 2595{
bca79478 2596 uint32_t tmp;
8687b63a 2597 __be32 *p;
409924e4 2598 int ret = 0;
1da177e4
LT
2599
2600 *mode = 0;
2601 if (unlikely(bitmap[1] & (FATTR4_WORD1_MODE - 1U)))
2602 return -EIO;
2603 if (likely(bitmap[1] & FATTR4_WORD1_MODE)) {
2604 READ_BUF(4);
bca79478
TM
2605 READ32(tmp);
2606 *mode = tmp & ~S_IFMT;
1da177e4 2607 bitmap[1] &= ~FATTR4_WORD1_MODE;
409924e4 2608 ret = NFS_ATTR_FATTR_MODE;
1da177e4 2609 }
3110ff80 2610 dprintk("%s: file mode=0%o\n", __func__, (unsigned int)*mode);
409924e4 2611 return ret;
1da177e4
LT
2612}
2613
2614static int decode_attr_nlink(struct xdr_stream *xdr, uint32_t *bitmap, uint32_t *nlink)
2615{
8687b63a 2616 __be32 *p;
409924e4 2617 int ret = 0;
1da177e4
LT
2618
2619 *nlink = 1;
2620 if (unlikely(bitmap[1] & (FATTR4_WORD1_NUMLINKS - 1U)))
2621 return -EIO;
2622 if (likely(bitmap[1] & FATTR4_WORD1_NUMLINKS)) {
2623 READ_BUF(4);
2624 READ32(*nlink);
2625 bitmap[1] &= ~FATTR4_WORD1_NUMLINKS;
409924e4 2626 ret = NFS_ATTR_FATTR_NLINK;
1da177e4 2627 }
3110ff80 2628 dprintk("%s: nlink=%u\n", __func__, (unsigned int)*nlink);
409924e4 2629 return ret;
1da177e4
LT
2630}
2631
2e42c3e2 2632static int decode_attr_owner(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs_client *clp, uint32_t *uid)
1da177e4 2633{
8687b63a
AV
2634 uint32_t len;
2635 __be32 *p;
409924e4 2636 int ret = 0;
1da177e4
LT
2637
2638 *uid = -2;
2639 if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER - 1U)))
2640 return -EIO;
2641 if (likely(bitmap[1] & FATTR4_WORD1_OWNER)) {
2642 READ_BUF(4);
2643 READ32(len);
2644 READ_BUF(len);
2645 if (len < XDR_MAX_NETOBJ) {
409924e4
TM
2646 if (nfs_map_name_to_uid(clp, (char *)p, len, uid) == 0)
2647 ret = NFS_ATTR_FATTR_OWNER;
2648 else
1da177e4 2649 dprintk("%s: nfs_map_name_to_uid failed!\n",
3110ff80 2650 __func__);
1da177e4 2651 } else
fe82a183 2652 dprintk("%s: name too long (%u)!\n",
3110ff80 2653 __func__, len);
1da177e4
LT
2654 bitmap[1] &= ~FATTR4_WORD1_OWNER;
2655 }
3110ff80 2656 dprintk("%s: uid=%d\n", __func__, (int)*uid);
409924e4 2657 return ret;
1da177e4
LT
2658}
2659
2e42c3e2 2660static int decode_attr_group(struct xdr_stream *xdr, uint32_t *bitmap, struct nfs_client *clp, uint32_t *gid)
1da177e4 2661{
8687b63a
AV
2662 uint32_t len;
2663 __be32 *p;
409924e4 2664 int ret = 0;
1da177e4
LT
2665
2666 *gid = -2;
2667 if (unlikely(bitmap[1] & (FATTR4_WORD1_OWNER_GROUP - 1U)))
2668 return -EIO;
2669 if (likely(bitmap[1] & FATTR4_WORD1_OWNER_GROUP)) {
2670 READ_BUF(4);
2671 READ32(len);
2672 READ_BUF(len);
2673 if (len < XDR_MAX_NETOBJ) {
409924e4
TM
2674 if (nfs_map_group_to_gid(clp, (char *)p, len, gid) == 0)
2675 ret = NFS_ATTR_FATTR_GROUP;
2676 else
1da177e4 2677 dprintk("%s: nfs_map_group_to_gid failed!\n",
3110ff80 2678 __func__);
1da177e4 2679 } else
fe82a183 2680 dprintk("%s: name too long (%u)!\n",
3110ff80 2681 __func__, len);
1da177e4
LT
2682 bitmap[1] &= ~FATTR4_WORD1_OWNER_GROUP;
2683 }
3110ff80 2684 dprintk("%s: gid=%d\n", __func__, (int)*gid);
409924e4 2685 return ret;
1da177e4
LT
2686}
2687
2688static int decode_attr_rdev(struct xdr_stream *xdr, uint32_t *bitmap, dev_t *rdev)
2689{
8687b63a
AV
2690 uint32_t major = 0, minor = 0;
2691 __be32 *p;
409924e4 2692 int ret = 0;
1da177e4
LT
2693
2694 *rdev = MKDEV(0,0);
2695 if (unlikely(bitmap[1] & (FATTR4_WORD1_RAWDEV - 1U)))
2696 return -EIO;
2697 if (likely(bitmap[1] & FATTR4_WORD1_RAWDEV)) {
2698 dev_t tmp;
2699
2700 READ_BUF(8);
2701 READ32(major);
2702 READ32(minor);
2703 tmp = MKDEV(major, minor);
2704 if (MAJOR(tmp) == major && MINOR(tmp) == minor)
2705 *rdev = tmp;
2706 bitmap[1] &= ~ FATTR4_WORD1_RAWDEV;
409924e4 2707 ret = NFS_ATTR_FATTR_RDEV;
1da177e4 2708 }
3110ff80 2709 dprintk("%s: rdev=(0x%x:0x%x)\n", __func__, major, minor);
409924e4 2710 return ret;
1da177e4
LT
2711}
2712
2713static int decode_attr_space_avail(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2714{
8687b63a 2715 __be32 *p;
1da177e4
LT
2716 int status = 0;
2717
2718 *res = 0;
2719 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_AVAIL - 1U)))
2720 return -EIO;
2721 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_AVAIL)) {
2722 READ_BUF(8);
2723 READ64(*res);
2724 bitmap[1] &= ~FATTR4_WORD1_SPACE_AVAIL;
2725 }
3110ff80 2726 dprintk("%s: space avail=%Lu\n", __func__, (unsigned long long)*res);
1da177e4
LT
2727 return status;
2728}
2729
2730static int decode_attr_space_free(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2731{
8687b63a 2732 __be32 *p;
1da177e4
LT
2733 int status = 0;
2734
2735 *res = 0;
2736 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_FREE - 1U)))
2737 return -EIO;
2738 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_FREE)) {
2739 READ_BUF(8);
2740 READ64(*res);
2741 bitmap[1] &= ~FATTR4_WORD1_SPACE_FREE;
2742 }
3110ff80 2743 dprintk("%s: space free=%Lu\n", __func__, (unsigned long long)*res);
1da177e4
LT
2744 return status;
2745}
2746
2747static int decode_attr_space_total(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *res)
2748{
8687b63a 2749 __be32 *p;
1da177e4
LT
2750 int status = 0;
2751
2752 *res = 0;
2753 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_TOTAL - 1U)))
2754 return -EIO;
2755 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_TOTAL)) {
2756 READ_BUF(8);
2757 READ64(*res);
2758 bitmap[1] &= ~FATTR4_WORD1_SPACE_TOTAL;
2759 }
3110ff80 2760 dprintk("%s: space total=%Lu\n", __func__, (unsigned long long)*res);
1da177e4
LT
2761 return status;
2762}
2763
2764static int decode_attr_space_used(struct xdr_stream *xdr, uint32_t *bitmap, uint64_t *used)
2765{
8687b63a 2766 __be32 *p;
409924e4 2767 int ret = 0;
1da177e4
LT
2768
2769 *used = 0;
2770 if (unlikely(bitmap[1] & (FATTR4_WORD1_SPACE_USED - 1U)))
2771 return -EIO;
2772 if (likely(bitmap[1] & FATTR4_WORD1_SPACE_USED)) {
2773 READ_BUF(8);
2774 READ64(*used);
2775 bitmap[1] &= ~FATTR4_WORD1_SPACE_USED;
409924e4 2776 ret = NFS_ATTR_FATTR_SPACE_USED;
1da177e4 2777 }
3110ff80 2778 dprintk("%s: space used=%Lu\n", __func__,
1da177e4 2779 (unsigned long long)*used);
409924e4 2780 return ret;
1da177e4
LT
2781}
2782
2783static int decode_attr_time(struct xdr_stream *xdr, struct timespec *time)
2784{
8687b63a 2785 __be32 *p;
1da177e4
LT
2786 uint64_t sec;
2787 uint32_t nsec;
2788
2789 READ_BUF(12);
2790 READ64(sec);
2791 READ32(nsec);
2792 time->tv_sec = (time_t)sec;
2793 time->tv_nsec = (long)nsec;
2794 return 0;
2795}
2796
2797static int decode_attr_time_access(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2798{
2799 int status = 0;
2800
2801 time->tv_sec = 0;
2802 time->tv_nsec = 0;
2803 if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_ACCESS - 1U)))
2804 return -EIO;
2805 if (likely(bitmap[1] & FATTR4_WORD1_TIME_ACCESS)) {
2806 status = decode_attr_time(xdr, time);
409924e4
TM
2807 if (status == 0)
2808 status = NFS_ATTR_FATTR_ATIME;
1da177e4
LT
2809 bitmap[1] &= ~FATTR4_WORD1_TIME_ACCESS;
2810 }
3110ff80 2811 dprintk("%s: atime=%ld\n", __func__, (long)time->tv_sec);
1da177e4
LT
2812 return status;
2813}
2814
2815static int decode_attr_time_metadata(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2816{
2817 int status = 0;
2818
2819 time->tv_sec = 0;
2820 time->tv_nsec = 0;
2821 if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_METADATA - 1U)))
2822 return -EIO;
2823 if (likely(bitmap[1] & FATTR4_WORD1_TIME_METADATA)) {
2824 status = decode_attr_time(xdr, time);
409924e4
TM
2825 if (status == 0)
2826 status = NFS_ATTR_FATTR_CTIME;
1da177e4
LT
2827 bitmap[1] &= ~FATTR4_WORD1_TIME_METADATA;
2828 }
3110ff80 2829 dprintk("%s: ctime=%ld\n", __func__, (long)time->tv_sec);
1da177e4
LT
2830 return status;
2831}
2832
2833static int decode_attr_time_modify(struct xdr_stream *xdr, uint32_t *bitmap, struct timespec *time)
2834{
2835 int status = 0;
2836
2837 time->tv_sec = 0;
2838 time->tv_nsec = 0;
2839 if (unlikely(bitmap[1] & (FATTR4_WORD1_TIME_MODIFY - 1U)))
2840 return -EIO;
2841 if (likely(bitmap[1] & FATTR4_WORD1_TIME_MODIFY)) {
2842 status = decode_attr_time(xdr, time);
409924e4
TM
2843 if (status == 0)
2844 status = NFS_ATTR_FATTR_MTIME;
1da177e4
LT
2845 bitmap[1] &= ~FATTR4_WORD1_TIME_MODIFY;
2846 }
3110ff80 2847 dprintk("%s: mtime=%ld\n", __func__, (long)time->tv_sec);
1da177e4
LT
2848 return status;
2849}
2850
8687b63a 2851static int verify_attr_len(struct xdr_stream *xdr, __be32 *savep, uint32_t attrlen)
1da177e4
LT
2852{
2853 unsigned int attrwords = XDR_QUADLEN(attrlen);
2854 unsigned int nwords = xdr->p - savep;
2855
2856 if (unlikely(attrwords != nwords)) {
fe82a183
CL
2857 dprintk("%s: server returned incorrect attribute length: "
2858 "%u %c %u\n",
3110ff80 2859 __func__,
1da177e4
LT
2860 attrwords << 2,
2861 (attrwords < nwords) ? '<' : '>',
2862 nwords << 2);
2863 return -EIO;
2864 }
2865 return 0;
2866}
2867
2868static int decode_change_info(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
2869{
8687b63a 2870 __be32 *p;
1da177e4
LT
2871
2872 READ_BUF(20);
2873 READ32(cinfo->atomic);
2874 READ64(cinfo->before);
2875 READ64(cinfo->after);
2876 return 0;
2877}
2878
2879static int decode_access(struct xdr_stream *xdr, struct nfs4_accessres *access)
2880{
8687b63a 2881 __be32 *p;
1da177e4
LT
2882 uint32_t supp, acc;
2883 int status;
2884
2885 status = decode_op_hdr(xdr, OP_ACCESS);
2886 if (status)
2887 return status;
2888 READ_BUF(8);
2889 READ32(supp);
2890 READ32(acc);
2891 access->supported = supp;
2892 access->access = acc;
2893 return 0;
2894}
2895
2896static int decode_close(struct xdr_stream *xdr, struct nfs_closeres *res)
2897{
8687b63a 2898 __be32 *p;
1da177e4
LT
2899 int status;
2900
2901 status = decode_op_hdr(xdr, OP_CLOSE);
c1d51931
TM
2902 if (status != -EIO)
2903 nfs_increment_open_seqid(status, res->seqid);
1da177e4
LT
2904 if (status)
2905 return status;
8ae20abd
TM
2906 READ_BUF(NFS4_STATEID_SIZE);
2907 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
1da177e4
LT
2908 return 0;
2909}
2910
2911static int decode_commit(struct xdr_stream *xdr, struct nfs_writeres *res)
2912{
8687b63a 2913 __be32 *p;
1da177e4
LT
2914 int status;
2915
2916 status = decode_op_hdr(xdr, OP_COMMIT);
2917 if (status)
2918 return status;
2919 READ_BUF(8);
2920 COPYMEM(res->verf->verifier, 8);
2921 return 0;
2922}
2923
2924static int decode_create(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
2925{
8687b63a 2926 __be32 *p;
1da177e4
LT
2927 uint32_t bmlen;
2928 int status;
2929
2930 status = decode_op_hdr(xdr, OP_CREATE);
2931 if (status)
2932 return status;
2933 if ((status = decode_change_info(xdr, cinfo)))
2934 return status;
2935 READ_BUF(4);
2936 READ32(bmlen);
2937 READ_BUF(bmlen << 2);
2938 return 0;
2939}
2940
2941static int decode_server_caps(struct xdr_stream *xdr, struct nfs4_server_caps_res *res)
2942{
8687b63a 2943 __be32 *savep;
6c0195a4 2944 uint32_t attrlen, bitmap[2] = {0};
1da177e4
LT
2945 int status;
2946
2947 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2948 goto xdr_error;
2949 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2950 goto xdr_error;
2951 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2952 goto xdr_error;
2953 if ((status = decode_attr_supported(xdr, bitmap, res->attr_bitmask)) != 0)
2954 goto xdr_error;
2955 if ((status = decode_attr_link_support(xdr, bitmap, &res->has_links)) != 0)
2956 goto xdr_error;
2957 if ((status = decode_attr_symlink_support(xdr, bitmap, &res->has_symlinks)) != 0)
2958 goto xdr_error;
2959 if ((status = decode_attr_aclsupport(xdr, bitmap, &res->acl_bitmask)) != 0)
2960 goto xdr_error;
2961 status = verify_attr_len(xdr, savep, attrlen);
2962xdr_error:
3110ff80 2963 dprintk("%s: xdr returned %d!\n", __func__, -status);
1da177e4
LT
2964 return status;
2965}
6c0195a4 2966
1da177e4
LT
2967static int decode_statfs(struct xdr_stream *xdr, struct nfs_fsstat *fsstat)
2968{
8687b63a 2969 __be32 *savep;
6c0195a4 2970 uint32_t attrlen, bitmap[2] = {0};
1da177e4 2971 int status;
6c0195a4 2972
1da177e4
LT
2973 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
2974 goto xdr_error;
2975 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
2976 goto xdr_error;
2977 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
2978 goto xdr_error;
2979
2980 if ((status = decode_attr_files_avail(xdr, bitmap, &fsstat->afiles)) != 0)
2981 goto xdr_error;
2982 if ((status = decode_attr_files_free(xdr, bitmap, &fsstat->ffiles)) != 0)
2983 goto xdr_error;
2984 if ((status = decode_attr_files_total(xdr, bitmap, &fsstat->tfiles)) != 0)
2985 goto xdr_error;
2986 if ((status = decode_attr_space_avail(xdr, bitmap, &fsstat->abytes)) != 0)
2987 goto xdr_error;
2988 if ((status = decode_attr_space_free(xdr, bitmap, &fsstat->fbytes)) != 0)
2989 goto xdr_error;
2990 if ((status = decode_attr_space_total(xdr, bitmap, &fsstat->tbytes)) != 0)
2991 goto xdr_error;
2992
2993 status = verify_attr_len(xdr, savep, attrlen);
2994xdr_error:
3110ff80 2995 dprintk("%s: xdr returned %d!\n", __func__, -status);
1da177e4
LT
2996 return status;
2997}
2998
2999static int decode_pathconf(struct xdr_stream *xdr, struct nfs_pathconf *pathconf)
3000{
8687b63a 3001 __be32 *savep;
6c0195a4 3002 uint32_t attrlen, bitmap[2] = {0};
1da177e4 3003 int status;
6c0195a4 3004
1da177e4
LT
3005 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3006 goto xdr_error;
3007 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3008 goto xdr_error;
3009 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3010 goto xdr_error;
3011
3012 if ((status = decode_attr_maxlink(xdr, bitmap, &pathconf->max_link)) != 0)
3013 goto xdr_error;
3014 if ((status = decode_attr_maxname(xdr, bitmap, &pathconf->max_namelen)) != 0)
3015 goto xdr_error;
3016
3017 status = verify_attr_len(xdr, savep, attrlen);
3018xdr_error:
3110ff80 3019 dprintk("%s: xdr returned %d!\n", __func__, -status);
1da177e4
LT
3020 return status;
3021}
3022
3023static int decode_getfattr(struct xdr_stream *xdr, struct nfs_fattr *fattr, const struct nfs_server *server)
3024{
8687b63a 3025 __be32 *savep;
1da177e4
LT
3026 uint32_t attrlen,
3027 bitmap[2] = {0},
3028 type;
bca79478
TM
3029 int status;
3030 umode_t fmode = 0;
99baf625 3031 uint64_t fileid;
1da177e4 3032
f26c7a78
TM
3033 status = decode_op_hdr(xdr, OP_GETATTR);
3034 if (status < 0)
1da177e4 3035 goto xdr_error;
f26c7a78
TM
3036
3037 status = decode_attr_bitmap(xdr, bitmap);
3038 if (status < 0)
1da177e4
LT
3039 goto xdr_error;
3040
f26c7a78
TM
3041 status = decode_attr_length(xdr, &attrlen, &savep);
3042 if (status < 0)
1da177e4
LT
3043 goto xdr_error;
3044
3045
f26c7a78
TM
3046 status = decode_attr_type(xdr, bitmap, &type);
3047 if (status < 0)
1da177e4 3048 goto xdr_error;
409924e4
TM
3049 fattr->mode = 0;
3050 if (status != 0) {
3051 fattr->mode |= nfs_type2fmt[type];
3052 fattr->valid |= status;
3053 }
1da177e4 3054
f26c7a78
TM
3055 status = decode_attr_change(xdr, bitmap, &fattr->change_attr);
3056 if (status < 0)
1da177e4 3057 goto xdr_error;
409924e4 3058 fattr->valid |= status;
f26c7a78
TM
3059
3060 status = decode_attr_size(xdr, bitmap, &fattr->size);
3061 if (status < 0)
1da177e4 3062 goto xdr_error;
409924e4 3063 fattr->valid |= status;
f26c7a78
TM
3064
3065 status = decode_attr_fsid(xdr, bitmap, &fattr->fsid);
3066 if (status < 0)
1da177e4 3067 goto xdr_error;
409924e4 3068 fattr->valid |= status;
f26c7a78
TM
3069
3070 status = decode_attr_fileid(xdr, bitmap, &fattr->fileid);
3071 if (status < 0)
1da177e4 3072 goto xdr_error;
409924e4 3073 fattr->valid |= status;
f26c7a78
TM
3074
3075 status = decode_attr_fs_locations(xdr, bitmap, container_of(fattr,
7aaa0b3b 3076 struct nfs4_fs_locations,
f26c7a78
TM
3077 fattr));
3078 if (status < 0)
683b57b4 3079 goto xdr_error;
409924e4 3080 fattr->valid |= status;
f26c7a78
TM
3081
3082 status = decode_attr_mode(xdr, bitmap, &fmode);
3083 if (status < 0)
1da177e4 3084 goto xdr_error;
409924e4
TM
3085 if (status != 0) {
3086 fattr->mode |= fmode;
3087 fattr->valid |= status;
3088 }
f26c7a78
TM
3089
3090 status = decode_attr_nlink(xdr, bitmap, &fattr->nlink);
3091 if (status < 0)
1da177e4 3092 goto xdr_error;
409924e4 3093 fattr->valid |= status;
f26c7a78
TM
3094
3095 status = decode_attr_owner(xdr, bitmap, server->nfs_client, &fattr->uid);
3096 if (status < 0)
1da177e4 3097 goto xdr_error;
409924e4 3098 fattr->valid |= status;
f26c7a78
TM
3099
3100 status = decode_attr_group(xdr, bitmap, server->nfs_client, &fattr->gid);
3101 if (status < 0)
1da177e4 3102 goto xdr_error;
409924e4 3103 fattr->valid |= status;
f26c7a78
TM
3104
3105 status = decode_attr_rdev(xdr, bitmap, &fattr->rdev);
3106 if (status < 0)
1da177e4 3107 goto xdr_error;
409924e4 3108 fattr->valid |= status;
f26c7a78
TM
3109
3110 status = decode_attr_space_used(xdr, bitmap, &fattr->du.nfs3.used);
3111 if (status < 0)
1da177e4 3112 goto xdr_error;
409924e4 3113 fattr->valid |= status;
f26c7a78
TM
3114
3115 status = decode_attr_time_access(xdr, bitmap, &fattr->atime);
3116 if (status < 0)
1da177e4 3117 goto xdr_error;
409924e4 3118 fattr->valid |= status;
f26c7a78
TM
3119
3120 status = decode_attr_time_metadata(xdr, bitmap, &fattr->ctime);
3121 if (status < 0)
1da177e4 3122 goto xdr_error;
409924e4 3123 fattr->valid |= status;
f26c7a78
TM
3124
3125 status = decode_attr_time_modify(xdr, bitmap, &fattr->mtime);
3126 if (status < 0)
1da177e4 3127 goto xdr_error;
409924e4 3128 fattr->valid |= status;
f26c7a78
TM
3129
3130 status = decode_attr_mounted_on_fileid(xdr, bitmap, &fileid);
3131 if (status < 0)
99baf625 3132 goto xdr_error;
409924e4 3133 if (status != 0 && !(fattr->valid & status)) {
99baf625 3134 fattr->fileid = fileid;
409924e4
TM
3135 fattr->valid |= status;
3136 }
f26c7a78
TM
3137
3138 status = verify_attr_len(xdr, savep, attrlen);
1da177e4 3139xdr_error:
3110ff80 3140 dprintk("%s: xdr returned %d\n", __func__, -status);
1da177e4
LT
3141 return status;
3142}
3143
3144
3145static int decode_fsinfo(struct xdr_stream *xdr, struct nfs_fsinfo *fsinfo)
3146{
8687b63a 3147 __be32 *savep;
1da177e4
LT
3148 uint32_t attrlen, bitmap[2];
3149 int status;
3150
3151 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3152 goto xdr_error;
3153 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3154 goto xdr_error;
3155 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3156 goto xdr_error;
3157
3158 fsinfo->rtmult = fsinfo->wtmult = 512; /* ??? */
3159
3160 if ((status = decode_attr_lease_time(xdr, bitmap, &fsinfo->lease_time)) != 0)
3161 goto xdr_error;
3162 if ((status = decode_attr_maxfilesize(xdr, bitmap, &fsinfo->maxfilesize)) != 0)
3163 goto xdr_error;
3164 if ((status = decode_attr_maxread(xdr, bitmap, &fsinfo->rtmax)) != 0)
3165 goto xdr_error;
3166 fsinfo->rtpref = fsinfo->dtpref = fsinfo->rtmax;
3167 if ((status = decode_attr_maxwrite(xdr, bitmap, &fsinfo->wtmax)) != 0)
3168 goto xdr_error;
3169 fsinfo->wtpref = fsinfo->wtmax;
3170
3171 status = verify_attr_len(xdr, savep, attrlen);
3172xdr_error:
3110ff80 3173 dprintk("%s: xdr returned %d!\n", __func__, -status);
1da177e4
LT
3174 return status;
3175}
3176
3177static int decode_getfh(struct xdr_stream *xdr, struct nfs_fh *fh)
3178{
8687b63a 3179 __be32 *p;
1da177e4
LT
3180 uint32_t len;
3181 int status;
3182
9936781d
TM
3183 /* Zero handle first to allow comparisons */
3184 memset(fh, 0, sizeof(*fh));
3185
1da177e4
LT
3186 status = decode_op_hdr(xdr, OP_GETFH);
3187 if (status)
3188 return status;
1da177e4
LT
3189
3190 READ_BUF(4);
3191 READ32(len);
3192 if (len > NFS4_FHSIZE)
3193 return -EIO;
3194 fh->size = len;
3195 READ_BUF(len);
3196 COPYMEM(fh->data, len);
3197 return 0;
3198}
3199
3200static int decode_link(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
3201{
3202 int status;
6c0195a4 3203
1da177e4
LT
3204 status = decode_op_hdr(xdr, OP_LINK);
3205 if (status)
3206 return status;
3207 return decode_change_info(xdr, cinfo);
3208}
3209
3210/*
3211 * We create the owner, so we know a proper owner.id length is 4.
3212 */
911d1aaf 3213static int decode_lock_denied (struct xdr_stream *xdr, struct file_lock *fl)
1da177e4 3214{
911d1aaf 3215 uint64_t offset, length, clientid;
8687b63a 3216 __be32 *p;
911d1aaf 3217 uint32_t namelen, type;
1da177e4
LT
3218
3219 READ_BUF(32);
911d1aaf
TM
3220 READ64(offset);
3221 READ64(length);
3222 READ32(type);
3223 if (fl != NULL) {
3224 fl->fl_start = (loff_t)offset;
3225 fl->fl_end = fl->fl_start + (loff_t)length - 1;
3226 if (length == ~(uint64_t)0)
3227 fl->fl_end = OFFSET_MAX;
3228 fl->fl_type = F_WRLCK;
3229 if (type & 1)
3230 fl->fl_type = F_RDLCK;
3231 fl->fl_pid = 0;
3232 }
3233 READ64(clientid);
1da177e4
LT
3234 READ32(namelen);
3235 READ_BUF(namelen);
1da177e4
LT
3236 return -NFS4ERR_DENIED;
3237}
3238
911d1aaf 3239static int decode_lock(struct xdr_stream *xdr, struct nfs_lock_res *res)
1da177e4 3240{
8687b63a 3241 __be32 *p;
1da177e4
LT
3242 int status;
3243
3244 status = decode_op_hdr(xdr, OP_LOCK);
c1d51931
TM
3245 if (status == -EIO)
3246 goto out;
1da177e4 3247 if (status == 0) {
8ae20abd
TM
3248 READ_BUF(NFS4_STATEID_SIZE);
3249 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
1da177e4 3250 } else if (status == -NFS4ERR_DENIED)
c1d51931
TM
3251 status = decode_lock_denied(xdr, NULL);
3252 if (res->open_seqid != NULL)
3253 nfs_increment_open_seqid(status, res->open_seqid);
3254 nfs_increment_lock_seqid(status, res->lock_seqid);
3255out:
1da177e4
LT
3256 return status;
3257}
3258
911d1aaf 3259static int decode_lockt(struct xdr_stream *xdr, struct nfs_lockt_res *res)
1da177e4
LT
3260{
3261 int status;
3262 status = decode_op_hdr(xdr, OP_LOCKT);
3263 if (status == -NFS4ERR_DENIED)
911d1aaf 3264 return decode_lock_denied(xdr, res->denied);
1da177e4
LT
3265 return status;
3266}
3267
911d1aaf 3268static int decode_locku(struct xdr_stream *xdr, struct nfs_locku_res *res)
1da177e4 3269{
8687b63a 3270 __be32 *p;
1da177e4
LT
3271 int status;
3272
3273 status = decode_op_hdr(xdr, OP_LOCKU);
c1d51931
TM
3274 if (status != -EIO)
3275 nfs_increment_lock_seqid(status, res->seqid);
1da177e4 3276 if (status == 0) {
8ae20abd
TM
3277 READ_BUF(NFS4_STATEID_SIZE);
3278 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
1da177e4
LT
3279 }
3280 return status;
3281}
3282
3283static int decode_lookup(struct xdr_stream *xdr)
3284{
3285 return decode_op_hdr(xdr, OP_LOOKUP);
3286}
3287
3288/* This is too sick! */
3289static int decode_space_limit(struct xdr_stream *xdr, u64 *maxsize)
3290{
05d564fe 3291 __be32 *p;
1da177e4
LT
3292 uint32_t limit_type, nblocks, blocksize;
3293
3294 READ_BUF(12);
3295 READ32(limit_type);
3296 switch (limit_type) {
05d564fe
AA
3297 case 1:
3298 READ64(*maxsize);
3299 break;
3300 case 2:
3301 READ32(nblocks);
3302 READ32(blocksize);
3303 *maxsize = (uint64_t)nblocks * (uint64_t)blocksize;
1da177e4
LT
3304 }
3305 return 0;
3306}
3307
3308static int decode_delegation(struct xdr_stream *xdr, struct nfs_openres *res)
3309{
05d564fe
AA
3310 __be32 *p;
3311 uint32_t delegation_type;
1da177e4
LT
3312
3313 READ_BUF(4);
3314 READ32(delegation_type);
3315 if (delegation_type == NFS4_OPEN_DELEGATE_NONE) {
3316 res->delegation_type = 0;
3317 return 0;
3318 }
8ae20abd
TM
3319 READ_BUF(NFS4_STATEID_SIZE+4);
3320 COPYMEM(res->delegation.data, NFS4_STATEID_SIZE);
1da177e4 3321 READ32(res->do_recall);
05d564fe 3322
1da177e4 3323 switch (delegation_type) {
05d564fe
AA
3324 case NFS4_OPEN_DELEGATE_READ:
3325 res->delegation_type = FMODE_READ;
3326 break;
3327 case NFS4_OPEN_DELEGATE_WRITE:
3328 res->delegation_type = FMODE_WRITE|FMODE_READ;
3329 if (decode_space_limit(xdr, &res->maxsize) < 0)
1da177e4
LT
3330 return -EIO;
3331 }
7539bbab 3332 return decode_ace(xdr, NULL, res->server->nfs_client);
1da177e4
LT
3333}
3334
3335static int decode_open(struct xdr_stream *xdr, struct nfs_openres *res)
3336{
05d564fe 3337 __be32 *p;
aa53ed54 3338 uint32_t savewords, bmlen, i;
05d564fe 3339 int status;
1da177e4 3340
05d564fe 3341 status = decode_op_hdr(xdr, OP_OPEN);
c1d51931
TM
3342 if (status != -EIO)
3343 nfs_increment_open_seqid(status, res->seqid);
05d564fe
AA
3344 if (status)
3345 return status;
3346 READ_BUF(NFS4_STATEID_SIZE);
3347 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
1da177e4 3348
05d564fe 3349 decode_change_info(xdr, &res->cinfo);
1da177e4 3350
05d564fe
AA
3351 READ_BUF(8);
3352 READ32(res->rflags);
3353 READ32(bmlen);
3354 if (bmlen > 10)
3355 goto xdr_error;
1da177e4 3356
05d564fe 3357 READ_BUF(bmlen << 2);
aa53ed54
JL
3358 savewords = min_t(uint32_t, bmlen, NFS4_BITMAP_SIZE);
3359 for (i = 0; i < savewords; ++i)
3360 READ32(res->attrset[i]);
3361 for (; i < NFS4_BITMAP_SIZE; i++)
3362 res->attrset[i] = 0;
3363
1da177e4
LT
3364 return decode_delegation(xdr, res);
3365xdr_error:
3110ff80 3366 dprintk("%s: Bitmap too large! Length = %u\n", __func__, bmlen);
1da177e4
LT
3367 return -EIO;
3368}
3369
3370static int decode_open_confirm(struct xdr_stream *xdr, struct nfs_open_confirmres *res)
3371{
05d564fe 3372 __be32 *p;
1da177e4
LT
3373 int status;
3374
05d564fe 3375 status = decode_op_hdr(xdr, OP_OPEN_CONFIRM);
c1d51931
TM
3376 if (status != -EIO)
3377 nfs_increment_open_seqid(status, res->seqid);
05d564fe
AA
3378 if (status)
3379 return status;
3380 READ_BUF(NFS4_STATEID_SIZE);
3381 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
3382 return 0;
1da177e4
LT
3383}
3384
3385static int decode_open_downgrade(struct xdr_stream *xdr, struct nfs_closeres *res)
3386{
8687b63a 3387 __be32 *p;
1da177e4
LT
3388 int status;
3389
3390 status = decode_op_hdr(xdr, OP_OPEN_DOWNGRADE);
c1d51931
TM
3391 if (status != -EIO)
3392 nfs_increment_open_seqid(status, res->seqid);
1da177e4
LT
3393 if (status)
3394 return status;
8ae20abd
TM
3395 READ_BUF(NFS4_STATEID_SIZE);
3396 COPYMEM(res->stateid.data, NFS4_STATEID_SIZE);
1da177e4
LT
3397 return 0;
3398}
3399
3400static int decode_putfh(struct xdr_stream *xdr)
3401{
3402 return decode_op_hdr(xdr, OP_PUTFH);
3403}
3404
3405static int decode_putrootfh(struct xdr_stream *xdr)
3406{
3407 return decode_op_hdr(xdr, OP_PUTROOTFH);
3408}
3409
3410static int decode_read(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs_readres *res)
3411{
3412 struct kvec *iov = req->rq_rcv_buf.head;
8687b63a 3413 __be32 *p;
1da177e4
LT
3414 uint32_t count, eof, recvd, hdrlen;
3415 int status;
3416
3417 status = decode_op_hdr(xdr, OP_READ);
3418 if (status)
3419 return status;
3420 READ_BUF(8);
3421 READ32(eof);
3422 READ32(count);
3423 hdrlen = (u8 *) p - (u8 *) iov->iov_base;
3424 recvd = req->rq_rcv_buf.len - hdrlen;
3425 if (count > recvd) {
fe82a183 3426 dprintk("NFS: server cheating in read reply: "
1da177e4
LT
3427 "count %u > recvd %u\n", count, recvd);
3428 count = recvd;
3429 eof = 0;
3430 }
3431 xdr_read_pages(xdr, count);
3432 res->eof = eof;
3433 res->count = count;
3434 return 0;
3435}
3436
3437static int decode_readdir(struct xdr_stream *xdr, struct rpc_rqst *req, struct nfs4_readdir_res *readdir)
3438{
3439 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
3440 struct page *page = *rcvbuf->pages;
3441 struct kvec *iov = rcvbuf->head;
bcecff77
CL
3442 size_t hdrlen;
3443 u32 recvd, pglen = rcvbuf->page_len;
8687b63a 3444 __be32 *end, *entry, *p, *kaddr;
7bda2cdf 3445 unsigned int nr = 0;
bcecff77 3446 int status;
1da177e4
LT
3447
3448 status = decode_op_hdr(xdr, OP_READDIR);
3449 if (status)
3450 return status;
3451 READ_BUF(8);
3452 COPYMEM(readdir->verifier.data, 8);
44109241
FI
3453 dprintk("%s: verifier = %08x:%08x\n",
3454 __func__,
eadf4598
TM
3455 ((u32 *)readdir->verifier.data)[0],
3456 ((u32 *)readdir->verifier.data)[1]);
3457
1da177e4
LT
3458
3459 hdrlen = (char *) p - (char *) iov->iov_base;
3460 recvd = rcvbuf->len - hdrlen;
3461 if (pglen > recvd)
3462 pglen = recvd;
3463 xdr_read_pages(xdr, pglen);
3464
3465 BUG_ON(pglen + readdir->pgbase > PAGE_CACHE_SIZE);
8687b63a 3466 kaddr = p = kmap_atomic(page, KM_USER0);
e8896495 3467 end = p + ((pglen + readdir->pgbase) >> 2);
1da177e4 3468 entry = p;
7bda2cdf
JL
3469
3470 /* Make sure the packet actually has a value_follows and EOF entry */
3471 if ((entry + 1) > end)
3472 goto short_pkt;
3473
3474 for (; *p++; nr++) {
bcecff77 3475 u32 len, attrlen, xlen;
e8896495 3476 if (end - p < 3)
1da177e4 3477 goto short_pkt;
eadf4598 3478 dprintk("cookie = %Lu, ", *((unsigned long long *)p));
1da177e4
LT
3479 p += 2; /* cookie */
3480 len = ntohl(*p++); /* filename length */
3481 if (len > NFS4_MAXNAMLEN) {
fe82a183
CL
3482 dprintk("NFS: giant filename in readdir (len 0x%x)\n",
3483 len);
1da177e4
LT
3484 goto err_unmap;
3485 }
e8896495
DH
3486 xlen = XDR_QUADLEN(len);
3487 if (end - p < xlen + 1)
1da177e4 3488 goto short_pkt;
e8896495
DH
3489 dprintk("filename = %*s\n", len, (char *)p);
3490 p += xlen;
1da177e4 3491 len = ntohl(*p++); /* bitmap length */
e8896495 3492 if (end - p < len + 1)
1da177e4 3493 goto short_pkt;
e8896495 3494 p += len;
1da177e4 3495 attrlen = XDR_QUADLEN(ntohl(*p++));
e8896495 3496 if (end - p < attrlen + 2)
1da177e4 3497 goto short_pkt;
e8896495 3498 p += attrlen; /* attributes */
1da177e4
LT
3499 entry = p;
3500 }
7bda2cdf
JL
3501 /*
3502 * Apparently some server sends responses that are a valid size, but
3503 * contain no entries, and have value_follows==0 and EOF==0. For
3504 * those, just set the EOF marker.
3505 */
3506 if (!nr && entry[1] == 0) {
3507 dprintk("NFS: readdir reply truncated!\n");
3508 entry[1] = 1;
3509 }
6c0195a4 3510out:
1da177e4
LT
3511 kunmap_atomic(kaddr, KM_USER0);
3512 return 0;
3513short_pkt:
7bda2cdf
JL
3514 /*
3515 * When we get a short packet there are 2 possibilities. We can
3516 * return an error, or fix up the response to look like a valid
3517 * response and return what we have so far. If there are no
3518 * entries and the packet was short, then return -EIO. If there
3519 * are valid entries in the response, return them and pretend that
3520 * the call was successful, but incomplete. The caller can retry the
3521 * readdir starting at the last cookie.
3522 */
3110ff80 3523 dprintk("%s: short packet at entry %d\n", __func__, nr);
1da177e4 3524 entry[0] = entry[1] = 0;
7bda2cdf
JL
3525 if (nr)
3526 goto out;
1da177e4
LT
3527err_unmap:
3528 kunmap_atomic(kaddr, KM_USER0);
3529 return -errno_NFSERR_IO;
3530}
3531
3532static int decode_readlink(struct xdr_stream *xdr, struct rpc_rqst *req)
3533{
3534 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
3535 struct kvec *iov = rcvbuf->head;
bcecff77
CL
3536 size_t hdrlen;
3537 u32 len, recvd;
8687b63a 3538 __be32 *p;
1da177e4
LT
3539 char *kaddr;
3540 int status;
3541
3542 status = decode_op_hdr(xdr, OP_READLINK);
3543 if (status)
3544 return status;
3545
3546 /* Convert length of symlink */
3547 READ_BUF(4);
3548 READ32(len);
3549 if (len >= rcvbuf->page_len || len <= 0) {
fe82a183 3550 dprintk("nfs: server returned giant symlink!\n");
1da177e4
LT
3551 return -ENAMETOOLONG;
3552 }
3553 hdrlen = (char *) xdr->p - (char *) iov->iov_base;
3554 recvd = req->rq_rcv_buf.len - hdrlen;
3555 if (recvd < len) {
fe82a183 3556 dprintk("NFS: server cheating in readlink reply: "
1da177e4
LT
3557 "count %u > recvd %u\n", len, recvd);
3558 return -EIO;
3559 }
3560 xdr_read_pages(xdr, len);
3561 /*
3562 * The XDR encode routine has set things up so that
3563 * the link text will be copied directly into the
3564 * buffer. We just have to do overflow-checking,
3565 * and and null-terminate the text (the VFS expects
3566 * null-termination).
3567 */
3568 kaddr = (char *)kmap_atomic(rcvbuf->pages[0], KM_USER0);
3569 kaddr[len+rcvbuf->page_base] = '\0';
3570 kunmap_atomic(kaddr, KM_USER0);
3571 return 0;
3572}
3573
3574static int decode_remove(struct xdr_stream *xdr, struct nfs4_change_info *cinfo)
3575{
3576 int status;
3577
3578 status = decode_op_hdr(xdr, OP_REMOVE);
3579 if (status)
3580 goto out;
3581 status = decode_change_info(xdr, cinfo);
3582out:
3583 return status;
3584}
3585
3586static int decode_rename(struct xdr_stream *xdr, struct nfs4_change_info *old_cinfo,
3587 struct nfs4_change_info *new_cinfo)
3588{
3589 int status;
3590
3591 status = decode_op_hdr(xdr, OP_RENAME);
3592 if (status)
3593 goto out;
3594 if ((status = decode_change_info(xdr, old_cinfo)))
3595 goto out;
3596 status = decode_change_info(xdr, new_cinfo);
3597out:
3598 return status;
3599}
3600
3601static int decode_renew(struct xdr_stream *xdr)
3602{
3603 return decode_op_hdr(xdr, OP_RENEW);
3604}
3605
56ae19f3
TM
3606static int
3607decode_restorefh(struct xdr_stream *xdr)
3608{
3609 return decode_op_hdr(xdr, OP_RESTOREFH);
3610}
3611
029d105e
BF
3612static int decode_getacl(struct xdr_stream *xdr, struct rpc_rqst *req,
3613 size_t *acl_len)
3614{
8687b63a 3615 __be32 *savep;
029d105e
BF
3616 uint32_t attrlen,
3617 bitmap[2] = {0};
3618 struct kvec *iov = req->rq_rcv_buf.head;
3619 int status;
3620
3621 *acl_len = 0;
3622 if ((status = decode_op_hdr(xdr, OP_GETATTR)) != 0)
3623 goto out;
3624 if ((status = decode_attr_bitmap(xdr, bitmap)) != 0)
3625 goto out;
3626 if ((status = decode_attr_length(xdr, &attrlen, &savep)) != 0)
3627 goto out;
3628
3629 if (unlikely(bitmap[0] & (FATTR4_WORD0_ACL - 1U)))
3630 return -EIO;
3631 if (likely(bitmap[0] & FATTR4_WORD0_ACL)) {
bcecff77
CL
3632 size_t hdrlen;
3633 u32 recvd;
029d105e
BF
3634
3635 /* We ignore &savep and don't do consistency checks on
3636 * the attr length. Let userspace figure it out.... */
3637 hdrlen = (u8 *)xdr->p - (u8 *)iov->iov_base;
3638 recvd = req->rq_rcv_buf.len - hdrlen;
3639 if (attrlen > recvd) {
fe82a183 3640 dprintk("NFS: server cheating in getattr"
029d105e
BF
3641 " acl reply: attrlen %u > recvd %u\n",
3642 attrlen, recvd);
3643 return -EINVAL;
3644 }
c04871e6 3645 xdr_read_pages(xdr, attrlen);
029d105e 3646 *acl_len = attrlen;
8c233cf9
BF
3647 } else
3648 status = -EOPNOTSUPP;
029d105e
BF
3649
3650out:
3651 return status;
3652}
3653
1da177e4
LT
3654static int
3655decode_savefh(struct xdr_stream *xdr)
3656{
3657 return decode_op_hdr(xdr, OP_SAVEFH);
3658}
3659
3660static int decode_setattr(struct xdr_stream *xdr, struct nfs_setattrres *res)
3661{
8687b63a 3662 __be32 *p;
1da177e4
LT
3663 uint32_t bmlen;
3664 int status;
3665
1da177e4
LT
3666 status = decode_op_hdr(xdr, OP_SETATTR);
3667 if (status)
3668 return status;
3669 READ_BUF(4);
3670 READ32(bmlen);
3671 READ_BUF(bmlen << 2);
3672 return 0;
3673}
3674
adfa6f98 3675static int decode_setclientid(struct xdr_stream *xdr, struct nfs_client *clp)
1da177e4 3676{
8687b63a 3677 __be32 *p;
1da177e4
LT
3678 uint32_t opnum;
3679 int32_t nfserr;
3680
3681 READ_BUF(8);
3682 READ32(opnum);
3683 if (opnum != OP_SETCLIENTID) {
fe82a183 3684 dprintk("nfs: decode_setclientid: Server returned operation"
6c0195a4 3685 " %d\n", opnum);
1da177e4
LT
3686 return -EIO;
3687 }
3688 READ32(nfserr);
3689 if (nfserr == NFS_OK) {
8ae20abd 3690 READ_BUF(8 + NFS4_VERIFIER_SIZE);
1da177e4 3691 READ64(clp->cl_clientid);
8ae20abd 3692 COPYMEM(clp->cl_confirm.data, NFS4_VERIFIER_SIZE);
1da177e4
LT
3693 } else if (nfserr == NFSERR_CLID_INUSE) {
3694 uint32_t len;
3695
3696 /* skip netid string */
3697 READ_BUF(4);
3698 READ32(len);
3699 READ_BUF(len);
3700
3701 /* skip uaddr string */
3702 READ_BUF(4);
3703 READ32(len);
3704 READ_BUF(len);
3705 return -NFSERR_CLID_INUSE;
3706 } else
856dff3d 3707 return nfs4_stat_to_errno(nfserr);
1da177e4
LT
3708
3709 return 0;
3710}
3711
3712static int decode_setclientid_confirm(struct xdr_stream *xdr)
3713{
3714 return decode_op_hdr(xdr, OP_SETCLIENTID_CONFIRM);
3715}
3716
3717static int decode_write(struct xdr_stream *xdr, struct nfs_writeres *res)
3718{
8687b63a 3719 __be32 *p;
1da177e4
LT
3720 int status;
3721
3722 status = decode_op_hdr(xdr, OP_WRITE);
3723 if (status)
3724 return status;
3725
3726 READ_BUF(16);
3727 READ32(res->count);
3728 READ32(res->verf->committed);
3729 COPYMEM(res->verf->verifier, 8);
3730 return 0;
3731}
3732
3733static int decode_delegreturn(struct xdr_stream *xdr)
3734{
3735 return decode_op_hdr(xdr, OP_DELEGRETURN);
3736}
3737
49c2559e
BH
3738/*
3739 * END OF "GENERIC" DECODE ROUTINES.
3740 */
3741
1da177e4
LT
3742/*
3743 * Decode OPEN_DOWNGRADE response
3744 */
8687b63a 3745static int nfs4_xdr_dec_open_downgrade(struct rpc_rqst *rqstp, __be32 *p, struct nfs_closeres *res)
1da177e4 3746{
05d564fe
AA
3747 struct xdr_stream xdr;
3748 struct compound_hdr hdr;
3749 int status;
3750
3751 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3752 status = decode_compound_hdr(&xdr, &hdr);
3753 if (status)
3754 goto out;
3755 status = decode_putfh(&xdr);
3756 if (status)
3757 goto out;
3758 status = decode_open_downgrade(&xdr, res);
516a6af6
TM
3759 if (status != 0)
3760 goto out;
3761 decode_getfattr(&xdr, res->fattr, res->server);
1da177e4 3762out:
05d564fe 3763 return status;
1da177e4
LT
3764}
3765
1da177e4
LT
3766/*
3767 * Decode ACCESS response
3768 */
8687b63a 3769static int nfs4_xdr_dec_access(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_accessres *res)
1da177e4
LT
3770{
3771 struct xdr_stream xdr;
3772 struct compound_hdr hdr;
3773 int status;
6c0195a4 3774
1da177e4
LT
3775 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3776 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3777 goto out;
76b32999
TM
3778 status = decode_putfh(&xdr);
3779 if (status != 0)
3780 goto out;
3781 status = decode_access(&xdr, res);
3782 if (status != 0)
3783 goto out;
3784 decode_getfattr(&xdr, res->fattr, res->server);
1da177e4
LT
3785out:
3786 return status;
3787}
3788
3789/*
3790 * Decode LOOKUP response
3791 */
8687b63a 3792static int nfs4_xdr_dec_lookup(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_lookup_res *res)
1da177e4
LT
3793{
3794 struct xdr_stream xdr;
3795 struct compound_hdr hdr;
3796 int status;
6c0195a4 3797
1da177e4
LT
3798 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3799 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3800 goto out;
3801 if ((status = decode_putfh(&xdr)) != 0)
3802 goto out;
3803 if ((status = decode_lookup(&xdr)) != 0)
3804 goto out;
3805 if ((status = decode_getfh(&xdr, res->fh)) != 0)
3806 goto out;
3807 status = decode_getfattr(&xdr, res->fattr, res->server);
3808out:
3809 return status;
3810}
3811
3812/*
3813 * Decode LOOKUP_ROOT response
3814 */
8687b63a 3815static int nfs4_xdr_dec_lookup_root(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_lookup_res *res)
1da177e4
LT
3816{
3817 struct xdr_stream xdr;
3818 struct compound_hdr hdr;
3819 int status;
6c0195a4 3820
1da177e4
LT
3821 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3822 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3823 goto out;
3824 if ((status = decode_putrootfh(&xdr)) != 0)
3825 goto out;
3826 if ((status = decode_getfh(&xdr, res->fh)) == 0)
3827 status = decode_getfattr(&xdr, res->fattr, res->server);
3828out:
3829 return status;
3830}
3831
3832/*
3833 * Decode REMOVE response
3834 */
4fdc17b2 3835static int nfs4_xdr_dec_remove(struct rpc_rqst *rqstp, __be32 *p, struct nfs_removeres *res)
1da177e4
LT
3836{
3837 struct xdr_stream xdr;
3838 struct compound_hdr hdr;
3839 int status;
6c0195a4 3840
1da177e4
LT
3841 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3842 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3843 goto out;
16e42959
TM
3844 if ((status = decode_putfh(&xdr)) != 0)
3845 goto out;
3846 if ((status = decode_remove(&xdr, &res->cinfo)) != 0)
3847 goto out;
4fdc17b2 3848 decode_getfattr(&xdr, &res->dir_attr, res->server);
1da177e4
LT
3849out:
3850 return status;
3851}
3852
3853/*
3854 * Decode RENAME response
3855 */
8687b63a 3856static int nfs4_xdr_dec_rename(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_rename_res *res)
1da177e4
LT
3857{
3858 struct xdr_stream xdr;
3859 struct compound_hdr hdr;
3860 int status;
6c0195a4 3861
1da177e4
LT
3862 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3863 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3864 goto out;
3865 if ((status = decode_putfh(&xdr)) != 0)
3866 goto out;
3867 if ((status = decode_savefh(&xdr)) != 0)
3868 goto out;
3869 if ((status = decode_putfh(&xdr)) != 0)
3870 goto out;
6caf2c82
TM
3871 if ((status = decode_rename(&xdr, &res->old_cinfo, &res->new_cinfo)) != 0)
3872 goto out;
3873 /* Current FH is target directory */
3874 if (decode_getfattr(&xdr, res->new_fattr, res->server) != 0)
3875 goto out;
3876 if ((status = decode_restorefh(&xdr)) != 0)
3877 goto out;
3878 decode_getfattr(&xdr, res->old_fattr, res->server);
1da177e4
LT
3879out:
3880 return status;
3881}
3882
3883/*
3884 * Decode LINK response
3885 */
8687b63a 3886static int nfs4_xdr_dec_link(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_link_res *res)
1da177e4
LT
3887{
3888 struct xdr_stream xdr;
3889 struct compound_hdr hdr;
3890 int status;
6c0195a4 3891
1da177e4
LT
3892 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3893 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3894 goto out;
3895 if ((status = decode_putfh(&xdr)) != 0)
3896 goto out;
3897 if ((status = decode_savefh(&xdr)) != 0)
3898 goto out;
3899 if ((status = decode_putfh(&xdr)) != 0)
3900 goto out;
91ba2eee
TM
3901 if ((status = decode_link(&xdr, &res->cinfo)) != 0)
3902 goto out;
3903 /*
3904 * Note order: OP_LINK leaves the directory as the current
3905 * filehandle.
3906 */
3907 if (decode_getfattr(&xdr, res->dir_attr, res->server) != 0)
3908 goto out;
3909 if ((status = decode_restorefh(&xdr)) != 0)
3910 goto out;
3911 decode_getfattr(&xdr, res->fattr, res->server);
1da177e4
LT
3912out:
3913 return status;
3914}
3915
3916/*
3917 * Decode CREATE response
3918 */
8687b63a 3919static int nfs4_xdr_dec_create(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_create_res *res)
1da177e4
LT
3920{
3921 struct xdr_stream xdr;
3922 struct compound_hdr hdr;
3923 int status;
6c0195a4 3924
1da177e4
LT
3925 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3926 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
3927 goto out;
3928 if ((status = decode_putfh(&xdr)) != 0)
3929 goto out;
56ae19f3
TM
3930 if ((status = decode_savefh(&xdr)) != 0)
3931 goto out;
1da177e4
LT
3932 if ((status = decode_create(&xdr,&res->dir_cinfo)) != 0)
3933 goto out;
3934 if ((status = decode_getfh(&xdr, res->fh)) != 0)
3935 goto out;
56ae19f3
TM
3936 if (decode_getfattr(&xdr, res->fattr, res->server) != 0)
3937 goto out;
3938 if ((status = decode_restorefh(&xdr)) != 0)
3939 goto out;
3940 decode_getfattr(&xdr, res->dir_fattr, res->server);
1da177e4
LT
3941out:
3942 return status;
3943}
3944
3945/*
3946 * Decode SYMLINK response
3947 */
8687b63a 3948static int nfs4_xdr_dec_symlink(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_create_res *res)
1da177e4
LT
3949{
3950 return nfs4_xdr_dec_create(rqstp, p, res);
3951}
3952
3953/*
3954 * Decode GETATTR response
3955 */
8687b63a 3956static int nfs4_xdr_dec_getattr(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_getattr_res *res)
1da177e4
LT
3957{
3958 struct xdr_stream xdr;
3959 struct compound_hdr hdr;
3960 int status;
6c0195a4 3961
1da177e4
LT
3962 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
3963 status = decode_compound_hdr(&xdr, &hdr);
3964 if (status)
3965 goto out;
3966 status = decode_putfh(&xdr);
3967 if (status)
3968 goto out;
3969 status = decode_getfattr(&xdr, res->fattr, res->server);
3970out:
3971 return status;
1da177e4
LT
3972}
3973
23ec6965
BF
3974/*
3975 * Encode an SETACL request
3976 */
3977static int
8687b63a 3978nfs4_xdr_enc_setacl(struct rpc_rqst *req, __be32 *p, struct nfs_setaclargs *args)
23ec6965 3979{
05d564fe
AA
3980 struct xdr_stream xdr;
3981 struct compound_hdr hdr = {
d017931c 3982 .nops = 0,
05d564fe
AA
3983 };
3984 int status;
3985
3986 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
3987 encode_compound_hdr(&xdr, &hdr);
cf8cdbe5 3988 encode_putfh(&xdr, args->fh, &hdr);
d017931c 3989 status = encode_setacl(&xdr, args, &hdr);
d017931c 3990 encode_nops(&hdr);
05d564fe 3991 return status;
23ec6965 3992}
05d564fe 3993
23ec6965
BF
3994/*
3995 * Decode SETACL response
3996 */
3997static int
8687b63a 3998nfs4_xdr_dec_setacl(struct rpc_rqst *rqstp, __be32 *p, void *res)
23ec6965
BF
3999{
4000 struct xdr_stream xdr;
4001 struct compound_hdr hdr;
4002 int status;
4003
4004 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4005 status = decode_compound_hdr(&xdr, &hdr);
4006 if (status)
4007 goto out;
4008 status = decode_putfh(&xdr);
4009 if (status)
4010 goto out;
4011 status = decode_setattr(&xdr, res);
4012out:
4013 return status;
4014}
1da177e4 4015
029d105e
BF
4016/*
4017 * Decode GETACL response
4018 */
4019static int
8687b63a 4020nfs4_xdr_dec_getacl(struct rpc_rqst *rqstp, __be32 *p, size_t *acl_len)
029d105e
BF
4021{
4022 struct xdr_stream xdr;
4023 struct compound_hdr hdr;
4024 int status;
4025
4026 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4027 status = decode_compound_hdr(&xdr, &hdr);
4028 if (status)
4029 goto out;
4030 status = decode_putfh(&xdr);
4031 if (status)
4032 goto out;
4033 status = decode_getacl(&xdr, rqstp, acl_len);
4034
4035out:
4036 return status;
4037}
4038
1da177e4
LT
4039/*
4040 * Decode CLOSE response
4041 */
8687b63a 4042static int nfs4_xdr_dec_close(struct rpc_rqst *rqstp, __be32 *p, struct nfs_closeres *res)
1da177e4 4043{
05d564fe
AA
4044 struct xdr_stream xdr;
4045 struct compound_hdr hdr;
4046 int status;
4047
4048 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4049 status = decode_compound_hdr(&xdr, &hdr);
4050 if (status)
4051 goto out;
4052 status = decode_putfh(&xdr);
4053 if (status)
4054 goto out;
4055 status = decode_close(&xdr, res);
516a6af6
TM
4056 if (status != 0)
4057 goto out;
4058 /*
4059 * Note: Server may do delete on close for this file
4060 * in which case the getattr call will fail with
4061 * an ESTALE error. Shouldn't be a problem,
4062 * though, since fattr->valid will remain unset.
4063 */
4064 decode_getfattr(&xdr, res->fattr, res->server);
1da177e4 4065out:
05d564fe 4066 return status;
1da177e4
LT
4067}
4068
4069/*
4070 * Decode OPEN response
4071 */
8687b63a 4072static int nfs4_xdr_dec_open(struct rpc_rqst *rqstp, __be32 *p, struct nfs_openres *res)
1da177e4 4073{
05d564fe
AA
4074 struct xdr_stream xdr;
4075 struct compound_hdr hdr;
4076 int status;
4077
4078 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4079 status = decode_compound_hdr(&xdr, &hdr);
4080 if (status)
4081 goto out;
4082 status = decode_putfh(&xdr);
4083 if (status)
4084 goto out;
4085 status = decode_savefh(&xdr);
4086 if (status)
4087 goto out;
4088 status = decode_open(&xdr, res);
56ae19f3
TM
4089 if (status)
4090 goto out;
9936781d 4091 if (decode_getfh(&xdr, &res->fh) != 0)
1da177e4 4092 goto out;
56ae19f3
TM
4093 if (decode_getfattr(&xdr, res->f_attr, res->server) != 0)
4094 goto out;
365c8f58 4095 if (decode_restorefh(&xdr) != 0)
56ae19f3
TM
4096 goto out;
4097 decode_getfattr(&xdr, res->dir_attr, res->server);
1da177e4 4098out:
05d564fe 4099 return status;
1da177e4
LT
4100}
4101
4102/*
4103 * Decode OPEN_CONFIRM response
4104 */
8687b63a 4105static int nfs4_xdr_dec_open_confirm(struct rpc_rqst *rqstp, __be32 *p, struct nfs_open_confirmres *res)
1da177e4 4106{
05d564fe
AA
4107 struct xdr_stream xdr;
4108 struct compound_hdr hdr;
4109 int status;
4110
4111 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4112 status = decode_compound_hdr(&xdr, &hdr);
4113 if (status)
4114 goto out;
4115 status = decode_putfh(&xdr);
4116 if (status)
4117 goto out;
4118 status = decode_open_confirm(&xdr, res);
1da177e4 4119out:
05d564fe 4120 return status;
1da177e4
LT
4121}
4122
4123/*
4124 * Decode OPEN response
4125 */
8687b63a 4126static int nfs4_xdr_dec_open_noattr(struct rpc_rqst *rqstp, __be32 *p, struct nfs_openres *res)
1da177e4 4127{
05d564fe
AA
4128 struct xdr_stream xdr;
4129 struct compound_hdr hdr;
4130 int status;
4131
4132 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4133 status = decode_compound_hdr(&xdr, &hdr);
4134 if (status)
4135 goto out;
4136 status = decode_putfh(&xdr);
4137 if (status)
4138 goto out;
4139 status = decode_open(&xdr, res);
4140 if (status)
4141 goto out;
864472e9 4142 decode_getfattr(&xdr, res->f_attr, res->server);
1da177e4 4143out:
05d564fe 4144 return status;
1da177e4
LT
4145}
4146
4147/*
4148 * Decode SETATTR response
4149 */
8687b63a 4150static int nfs4_xdr_dec_setattr(struct rpc_rqst *rqstp, __be32 *p, struct nfs_setattrres *res)
1da177e4 4151{
05d564fe
AA
4152 struct xdr_stream xdr;
4153 struct compound_hdr hdr;
4154 int status;
4155
4156 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4157 status = decode_compound_hdr(&xdr, &hdr);
4158 if (status)
4159 goto out;
4160 status = decode_putfh(&xdr);
4161 if (status)
4162 goto out;
4163 status = decode_setattr(&xdr, res);
4164 if (status)
4165 goto out;
78f945f8 4166 decode_getfattr(&xdr, res->fattr, res->server);
1da177e4 4167out:
05d564fe 4168 return status;
1da177e4
LT
4169}
4170
4171/*
4172 * Decode LOCK response
4173 */
8687b63a 4174static int nfs4_xdr_dec_lock(struct rpc_rqst *rqstp, __be32 *p, struct nfs_lock_res *res)
1da177e4
LT
4175{
4176 struct xdr_stream xdr;
4177 struct compound_hdr hdr;
4178 int status;
4179
4180 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4181 status = decode_compound_hdr(&xdr, &hdr);
4182 if (status)
4183 goto out;
4184 status = decode_putfh(&xdr);
4185 if (status)
4186 goto out;
4187 status = decode_lock(&xdr, res);
4188out:
4189 return status;
4190}
4191
4192/*
4193 * Decode LOCKT response
4194 */
8687b63a 4195static int nfs4_xdr_dec_lockt(struct rpc_rqst *rqstp, __be32 *p, struct nfs_lockt_res *res)
1da177e4
LT
4196{
4197 struct xdr_stream xdr;
4198 struct compound_hdr hdr;
4199 int status;
4200
4201 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4202 status = decode_compound_hdr(&xdr, &hdr);
4203 if (status)
4204 goto out;
4205 status = decode_putfh(&xdr);
4206 if (status)
4207 goto out;
4208 status = decode_lockt(&xdr, res);
4209out:
4210 return status;
4211}
4212
4213/*
4214 * Decode LOCKU response
4215 */
8687b63a 4216static int nfs4_xdr_dec_locku(struct rpc_rqst *rqstp, __be32 *p, struct nfs_locku_res *res)
1da177e4
LT
4217{
4218 struct xdr_stream xdr;
4219 struct compound_hdr hdr;
4220 int status;
4221
4222 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4223 status = decode_compound_hdr(&xdr, &hdr);
4224 if (status)
4225 goto out;
4226 status = decode_putfh(&xdr);
4227 if (status)
4228 goto out;
4229 status = decode_locku(&xdr, res);
4230out:
4231 return status;
4232}
4233
4234/*
4235 * Decode READLINK response
4236 */
8687b63a 4237static int nfs4_xdr_dec_readlink(struct rpc_rqst *rqstp, __be32 *p, void *res)
1da177e4
LT
4238{
4239 struct xdr_stream xdr;
4240 struct compound_hdr hdr;
4241 int status;
4242
4243 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4244 status = decode_compound_hdr(&xdr, &hdr);
4245 if (status)
4246 goto out;
4247 status = decode_putfh(&xdr);
4248 if (status)
4249 goto out;
4250 status = decode_readlink(&xdr, rqstp);
4251out:
4252 return status;
4253}
4254
4255/*
4256 * Decode READDIR response
4257 */
8687b63a 4258static int nfs4_xdr_dec_readdir(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_readdir_res *res)
1da177e4
LT
4259{
4260 struct xdr_stream xdr;
4261 struct compound_hdr hdr;
4262 int status;
4263
4264 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4265 status = decode_compound_hdr(&xdr, &hdr);
4266 if (status)
4267 goto out;
4268 status = decode_putfh(&xdr);
4269 if (status)
4270 goto out;
4271 status = decode_readdir(&xdr, rqstp, res);
4272out:
4273 return status;
4274}
4275
4276/*
4277 * Decode Read response
4278 */
8687b63a 4279static int nfs4_xdr_dec_read(struct rpc_rqst *rqstp, __be32 *p, struct nfs_readres *res)
1da177e4
LT
4280{
4281 struct xdr_stream xdr;
4282 struct compound_hdr hdr;
4283 int status;
4284
4285 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4286 status = decode_compound_hdr(&xdr, &hdr);
4287 if (status)
4288 goto out;
4289 status = decode_putfh(&xdr);
4290 if (status)
4291 goto out;
4292 status = decode_read(&xdr, rqstp, res);
4293 if (!status)
4294 status = res->count;
4295out:
4296 return status;
4297}
4298
4299/*
4300 * Decode WRITE response
4301 */
8687b63a 4302static int nfs4_xdr_dec_write(struct rpc_rqst *rqstp, __be32 *p, struct nfs_writeres *res)
1da177e4
LT
4303{
4304 struct xdr_stream xdr;
4305 struct compound_hdr hdr;
4306 int status;
4307
4308 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4309 status = decode_compound_hdr(&xdr, &hdr);
4310 if (status)
4311 goto out;
4312 status = decode_putfh(&xdr);
4313 if (status)
4314 goto out;
4315 status = decode_write(&xdr, res);
4f9838c7
TM
4316 if (status)
4317 goto out;
4318 decode_getfattr(&xdr, res->fattr, res->server);
1da177e4
LT
4319 if (!status)
4320 status = res->count;
4321out:
4322 return status;
4323}
4324
4325/*
4326 * Decode COMMIT response
4327 */
8687b63a 4328static int nfs4_xdr_dec_commit(struct rpc_rqst *rqstp, __be32 *p, struct nfs_writeres *res)
1da177e4
LT
4329{
4330 struct xdr_stream xdr;
4331 struct compound_hdr hdr;
4332 int status;
4333
4334 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4335 status = decode_compound_hdr(&xdr, &hdr);
4336 if (status)
4337 goto out;
4338 status = decode_putfh(&xdr);
4339 if (status)
4340 goto out;
4341 status = decode_commit(&xdr, res);
4f9838c7
TM
4342 if (status)
4343 goto out;
4344 decode_getfattr(&xdr, res->fattr, res->server);
1da177e4
LT
4345out:
4346 return status;
4347}
4348
4349/*
4350 * FSINFO request
4351 */
8687b63a 4352static int nfs4_xdr_dec_fsinfo(struct rpc_rqst *req, __be32 *p, struct nfs_fsinfo *fsinfo)
1da177e4
LT
4353{
4354 struct xdr_stream xdr;
4355 struct compound_hdr hdr;
4356 int status;
4357
4358 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4359 status = decode_compound_hdr(&xdr, &hdr);
4360 if (!status)
4361 status = decode_putfh(&xdr);
4362 if (!status)
4363 status = decode_fsinfo(&xdr, fsinfo);
1da177e4
LT
4364 return status;
4365}
4366
4367/*
4368 * PATHCONF request
4369 */
8687b63a 4370static int nfs4_xdr_dec_pathconf(struct rpc_rqst *req, __be32 *p, struct nfs_pathconf *pathconf)
1da177e4
LT
4371{
4372 struct xdr_stream xdr;
4373 struct compound_hdr hdr;
4374 int status;
4375
4376 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4377 status = decode_compound_hdr(&xdr, &hdr);
4378 if (!status)
4379 status = decode_putfh(&xdr);
4380 if (!status)
4381 status = decode_pathconf(&xdr, pathconf);
4382 return status;
4383}
4384
4385/*
4386 * STATFS request
4387 */
8687b63a 4388static int nfs4_xdr_dec_statfs(struct rpc_rqst *req, __be32 *p, struct nfs_fsstat *fsstat)
1da177e4
LT
4389{
4390 struct xdr_stream xdr;
4391 struct compound_hdr hdr;
4392 int status;
4393
4394 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4395 status = decode_compound_hdr(&xdr, &hdr);
4396 if (!status)
4397 status = decode_putfh(&xdr);
4398 if (!status)
4399 status = decode_statfs(&xdr, fsstat);
4400 return status;
4401}
4402
4403/*
4404 * GETATTR_BITMAP request
4405 */
8687b63a 4406static int nfs4_xdr_dec_server_caps(struct rpc_rqst *req, __be32 *p, struct nfs4_server_caps_res *res)
1da177e4
LT
4407{
4408 struct xdr_stream xdr;
4409 struct compound_hdr hdr;
4410 int status;
4411
4412 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4413 if ((status = decode_compound_hdr(&xdr, &hdr)) != 0)
4414 goto out;
4415 if ((status = decode_putfh(&xdr)) != 0)
4416 goto out;
4417 status = decode_server_caps(&xdr, res);
4418out:
4419 return status;
4420}
4421
4422/*
4423 * Decode RENEW response
4424 */
8687b63a 4425static int nfs4_xdr_dec_renew(struct rpc_rqst *rqstp, __be32 *p, void *dummy)
1da177e4
LT
4426{
4427 struct xdr_stream xdr;
4428 struct compound_hdr hdr;
4429 int status;
4430
4431 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4432 status = decode_compound_hdr(&xdr, &hdr);
4433 if (!status)
4434 status = decode_renew(&xdr);
4435 return status;
4436}
4437
4438/*
4439 * a SETCLIENTID request
4440 */
8687b63a 4441static int nfs4_xdr_dec_setclientid(struct rpc_rqst *req, __be32 *p,
adfa6f98 4442 struct nfs_client *clp)
1da177e4
LT
4443{
4444 struct xdr_stream xdr;
4445 struct compound_hdr hdr;
4446 int status;
4447
4448 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4449 status = decode_compound_hdr(&xdr, &hdr);
4450 if (!status)
4451 status = decode_setclientid(&xdr, clp);
1da177e4
LT
4452 return status;
4453}
4454
4455/*
4456 * a SETCLIENTID_CONFIRM request
4457 */
8687b63a 4458static int nfs4_xdr_dec_setclientid_confirm(struct rpc_rqst *req, __be32 *p, struct nfs_fsinfo *fsinfo)
1da177e4
LT
4459{
4460 struct xdr_stream xdr;
4461 struct compound_hdr hdr;
4462 int status;
4463
4464 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4465 status = decode_compound_hdr(&xdr, &hdr);
4466 if (!status)
4467 status = decode_setclientid_confirm(&xdr);
4468 if (!status)
4469 status = decode_putrootfh(&xdr);
4470 if (!status)
4471 status = decode_fsinfo(&xdr, fsinfo);
1da177e4
LT
4472 return status;
4473}
4474
4475/*
4476 * DELEGRETURN request
4477 */
8687b63a 4478static int nfs4_xdr_dec_delegreturn(struct rpc_rqst *rqstp, __be32 *p, struct nfs4_delegreturnres *res)
1da177e4
LT
4479{
4480 struct xdr_stream xdr;
4481 struct compound_hdr hdr;
4482 int status;
4483
4484 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
4485 status = decode_compound_hdr(&xdr, &hdr);
fa178f29
TM
4486 if (status != 0)
4487 goto out;
4488 status = decode_putfh(&xdr);
4489 if (status != 0)
4490 goto out;
4491 status = decode_delegreturn(&xdr);
4492 decode_getfattr(&xdr, res->fattr, res->server);
4493out:
1da177e4
LT
4494 return status;
4495}
4496
683b57b4
TM
4497/*
4498 * FS_LOCATIONS request
4499 */
8687b63a 4500static int nfs4_xdr_dec_fs_locations(struct rpc_rqst *req, __be32 *p, struct nfs4_fs_locations *res)
683b57b4
TM
4501{
4502 struct xdr_stream xdr;
4503 struct compound_hdr hdr;
4504 int status;
4505
4506 xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
4507 status = decode_compound_hdr(&xdr, &hdr);
4508 if (status != 0)
4509 goto out;
4510 if ((status = decode_putfh(&xdr)) != 0)
4511 goto out;
4512 if ((status = decode_lookup(&xdr)) != 0)
4513 goto out;
4514 xdr_enter_page(&xdr, PAGE_SIZE);
4515 status = decode_getfattr(&xdr, &res->fattr, res->server);
4516out:
4517 return status;
4518}
4519
0dbb4c67 4520__be32 *nfs4_decode_dirent(__be32 *p, struct nfs_entry *entry, int plus)
1da177e4
LT
4521{
4522 uint32_t bitmap[2] = {0};
4523 uint32_t len;
4524
4525 if (!*p++) {
4526 if (!*p)
4527 return ERR_PTR(-EAGAIN);
4528 entry->eof = 1;
4529 return ERR_PTR(-EBADCOOKIE);
4530 }
4531
4532 entry->prev_cookie = entry->cookie;
4533 p = xdr_decode_hyper(p, &entry->cookie);
4534 entry->len = ntohl(*p++);
4535 entry->name = (const char *) p;
4536 p += XDR_QUADLEN(entry->len);
4537
4538 /*
4539 * In case the server doesn't return an inode number,
4540 * we fake one here. (We don't use inode number 0,
4541 * since glibc seems to choke on it...)
4542 */
4543 entry->ino = 1;
4544
4545 len = ntohl(*p++); /* bitmap length */
4546 if (len-- > 0) {
4547 bitmap[0] = ntohl(*p++);
4548 if (len-- > 0) {
4549 bitmap[1] = ntohl(*p++);
4550 p += len;
4551 }
4552 }
4553 len = XDR_QUADLEN(ntohl(*p++)); /* attribute buffer length */
4554 if (len > 0) {
97d312d0
MN
4555 if (bitmap[0] & FATTR4_WORD0_RDATTR_ERROR) {
4556 bitmap[0] &= ~FATTR4_WORD0_RDATTR_ERROR;
4557 /* Ignore the return value of rdattr_error for now */
4558 p++;
4559 len--;
4560 }
1da177e4
LT
4561 if (bitmap[0] == 0 && bitmap[1] == FATTR4_WORD1_MOUNTED_ON_FILEID)
4562 xdr_decode_hyper(p, &entry->ino);
4563 else if (bitmap[0] == FATTR4_WORD0_FILEID)
4564 xdr_decode_hyper(p, &entry->ino);
4565 p += len;
4566 }
4567
4568 entry->eof = !p[0] && p[1];
4569 return p;
4570}
4571
4572/*
4573 * We need to translate between nfs status return values and
4574 * the local errno values which may not be the same.
4575 */
4576static struct {
4577 int stat;
4578 int errno;
4579} nfs_errtbl[] = {
4580 { NFS4_OK, 0 },
856dff3d
BH
4581 { NFS4ERR_PERM, -EPERM },
4582 { NFS4ERR_NOENT, -ENOENT },
4583 { NFS4ERR_IO, -errno_NFSERR_IO},
4584 { NFS4ERR_NXIO, -ENXIO },
4585 { NFS4ERR_ACCESS, -EACCES },
4586 { NFS4ERR_EXIST, -EEXIST },
4587 { NFS4ERR_XDEV, -EXDEV },
4588 { NFS4ERR_NOTDIR, -ENOTDIR },
4589 { NFS4ERR_ISDIR, -EISDIR },
4590 { NFS4ERR_INVAL, -EINVAL },
4591 { NFS4ERR_FBIG, -EFBIG },
4592 { NFS4ERR_NOSPC, -ENOSPC },
4593 { NFS4ERR_ROFS, -EROFS },
4594 { NFS4ERR_MLINK, -EMLINK },
4595 { NFS4ERR_NAMETOOLONG, -ENAMETOOLONG },
4596 { NFS4ERR_NOTEMPTY, -ENOTEMPTY },
4597 { NFS4ERR_DQUOT, -EDQUOT },
4598 { NFS4ERR_STALE, -ESTALE },
4599 { NFS4ERR_BADHANDLE, -EBADHANDLE },
4600 { NFS4ERR_BADOWNER, -EINVAL },
4601 { NFS4ERR_BADNAME, -EINVAL },
4602 { NFS4ERR_BAD_COOKIE, -EBADCOOKIE },
4603 { NFS4ERR_NOTSUPP, -ENOTSUPP },
4604 { NFS4ERR_TOOSMALL, -ETOOSMALL },
4605 { NFS4ERR_SERVERFAULT, -ESERVERFAULT },
4606 { NFS4ERR_BADTYPE, -EBADTYPE },
4607 { NFS4ERR_LOCKED, -EAGAIN },
4608 { NFS4ERR_RESOURCE, -EREMOTEIO },
4609 { NFS4ERR_SYMLINK, -ELOOP },
4610 { NFS4ERR_OP_ILLEGAL, -EOPNOTSUPP },
4611 { NFS4ERR_DEADLOCK, -EDEADLK },
4612 { NFS4ERR_WRONGSEC, -EPERM }, /* FIXME: this needs
1da177e4
LT
4613 * to be handled by a
4614 * middle-layer.
4615 */
856dff3d 4616 { -1, -EIO }
1da177e4
LT
4617};
4618
4619/*
4620 * Convert an NFS error code to a local one.
4621 * This one is used jointly by NFSv2 and NFSv3.
4622 */
4623static int
0a8ea437 4624nfs4_stat_to_errno(int stat)
1da177e4
LT
4625{
4626 int i;
4627 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
4628 if (nfs_errtbl[i].stat == stat)
4629 return nfs_errtbl[i].errno;
4630 }
4631 if (stat <= 10000 || stat > 10100) {
4632 /* The server is looney tunes. */
856dff3d 4633 return -ESERVERFAULT;
1da177e4
LT
4634 }
4635 /* If we cannot translate the error, the recovery routines should
4636 * handle it.
4637 * Note: remaining NFSv4 error codes have values > 10000, so should
4638 * not conflict with native Linux error codes.
4639 */
856dff3d 4640 return -stat;
1da177e4
LT
4641}
4642
1da177e4
LT
4643#define PROC(proc, argtype, restype) \
4644[NFSPROC4_CLNT_##proc] = { \
4645 .p_proc = NFSPROC4_COMPOUND, \
4646 .p_encode = (kxdrproc_t) nfs4_xdr_##argtype, \
4647 .p_decode = (kxdrproc_t) nfs4_xdr_##restype, \
2bea90d4
CL
4648 .p_arglen = NFS4_##argtype##_sz, \
4649 .p_replen = NFS4_##restype##_sz, \
cc0175c1
CL
4650 .p_statidx = NFSPROC4_CLNT_##proc, \
4651 .p_name = #proc, \
05d564fe 4652}
1da177e4
LT
4653
4654struct rpc_procinfo nfs4_procedures[] = {
4655 PROC(READ, enc_read, dec_read),
4656 PROC(WRITE, enc_write, dec_write),
4657 PROC(COMMIT, enc_commit, dec_commit),
4658 PROC(OPEN, enc_open, dec_open),
4659 PROC(OPEN_CONFIRM, enc_open_confirm, dec_open_confirm),
4660 PROC(OPEN_NOATTR, enc_open_noattr, dec_open_noattr),
4661 PROC(OPEN_DOWNGRADE, enc_open_downgrade, dec_open_downgrade),
4662 PROC(CLOSE, enc_close, dec_close),
4663 PROC(SETATTR, enc_setattr, dec_setattr),
4664 PROC(FSINFO, enc_fsinfo, dec_fsinfo),
4665 PROC(RENEW, enc_renew, dec_renew),
4666 PROC(SETCLIENTID, enc_setclientid, dec_setclientid),
4667 PROC(SETCLIENTID_CONFIRM, enc_setclientid_confirm, dec_setclientid_confirm),
4668 PROC(LOCK, enc_lock, dec_lock),
4669 PROC(LOCKT, enc_lockt, dec_lockt),
4670 PROC(LOCKU, enc_locku, dec_locku),
4671 PROC(ACCESS, enc_access, dec_access),
4672 PROC(GETATTR, enc_getattr, dec_getattr),
4673 PROC(LOOKUP, enc_lookup, dec_lookup),
4674 PROC(LOOKUP_ROOT, enc_lookup_root, dec_lookup_root),
4675 PROC(REMOVE, enc_remove, dec_remove),
4676 PROC(RENAME, enc_rename, dec_rename),
4677 PROC(LINK, enc_link, dec_link),
4678 PROC(SYMLINK, enc_symlink, dec_symlink),
4679 PROC(CREATE, enc_create, dec_create),
4680 PROC(PATHCONF, enc_pathconf, dec_pathconf),
4681 PROC(STATFS, enc_statfs, dec_statfs),
4682 PROC(READLINK, enc_readlink, dec_readlink),
4683 PROC(READDIR, enc_readdir, dec_readdir),
4684 PROC(SERVER_CAPS, enc_server_caps, dec_server_caps),
4685 PROC(DELEGRETURN, enc_delegreturn, dec_delegreturn),
029d105e 4686 PROC(GETACL, enc_getacl, dec_getacl),
23ec6965 4687 PROC(SETACL, enc_setacl, dec_setacl),
683b57b4 4688 PROC(FS_LOCATIONS, enc_fs_locations, dec_fs_locations),
1da177e4
LT
4689};
4690
4691struct rpc_version nfs_version4 = {
4692 .number = 4,
e8c96f8c 4693 .nrprocs = ARRAY_SIZE(nfs4_procedures),
1da177e4
LT
4694 .procs = nfs4_procedures
4695};
4696
4697/*
4698 * Local variables:
4699 * c-basic-offset: 8
4700 * End:
4701 */