blob: 4a68837e92ea44228d0fb262376ea8f09da69d7e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/nfs3xdr.c
3 *
4 * XDR functions to encode/decode NFSv3 RPC arguments and results.
5 *
6 * Copyright (C) 1996, 1997 Olaf Kirch
7 */
8
9#include <linux/param.h>
10#include <linux/time.h>
11#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/errno.h>
13#include <linux/string.h>
14#include <linux/in.h>
15#include <linux/pagemap.h>
16#include <linux/proc_fs.h>
17#include <linux/kdev_t.h>
18#include <linux/sunrpc/clnt.h>
19#include <linux/nfs.h>
20#include <linux/nfs3.h>
21#include <linux/nfs_fs.h>
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000022#include <linux/nfsacl.h>
David Howellsf7b422b2006-06-09 09:34:33 -040023#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#define NFSDBG_FACILITY NFSDBG_XDR
26
27/* Mapping from NFS error code to "errno" error code. */
28#define errno_NFSERR_IO EIO
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/*
31 * Declare the space requirements for NFS arguments and replies as
32 * number of 32bit-words
33 */
34#define NFS3_fhandle_sz (1+16)
35#define NFS3_fh_sz (NFS3_fhandle_sz) /* shorthand */
Frank Sorenson55ef4f22021-03-08 12:12:13 -060036#define NFS3_post_op_fh_sz (1+NFS3_fh_sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define NFS3_sattr_sz (15)
38#define NFS3_filename_sz (1+(NFS3_MAXNAMLEN>>2))
39#define NFS3_path_sz (1+(NFS3_MAXPATHLEN>>2))
40#define NFS3_fattr_sz (21)
Chuck Leverd9c407b2010-12-14 14:55:50 +000041#define NFS3_cookieverf_sz (NFS3_COOKIEVERFSIZE>>2)
Chuck Leverf5fc3c502010-12-14 14:56:42 +000042#define NFS3_wcc_attr_sz (6)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#define NFS3_pre_op_attr_sz (1+NFS3_wcc_attr_sz)
44#define NFS3_post_op_attr_sz (1+NFS3_fattr_sz)
Chuck Leverf5fc3c502010-12-14 14:56:42 +000045#define NFS3_wcc_data_sz (NFS3_pre_op_attr_sz+NFS3_post_op_attr_sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define NFS3_diropargs_sz (NFS3_fh_sz+NFS3_filename_sz)
Chuck Leverad96b5b2010-12-14 14:56:01 +000047
48#define NFS3_getattrargs_sz (NFS3_fh_sz)
49#define NFS3_setattrargs_sz (NFS3_fh_sz+NFS3_sattr_sz+3)
50#define NFS3_lookupargs_sz (NFS3_fh_sz+NFS3_filename_sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define NFS3_accessargs_sz (NFS3_fh_sz+1)
52#define NFS3_readlinkargs_sz (NFS3_fh_sz)
53#define NFS3_readargs_sz (NFS3_fh_sz+3)
54#define NFS3_writeargs_sz (NFS3_fh_sz+5)
55#define NFS3_createargs_sz (NFS3_diropargs_sz+NFS3_sattr_sz)
56#define NFS3_mkdirargs_sz (NFS3_diropargs_sz+NFS3_sattr_sz)
Chuck Lever94a6d752006-08-22 20:06:23 -040057#define NFS3_symlinkargs_sz (NFS3_diropargs_sz+1+NFS3_sattr_sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#define NFS3_mknodargs_sz (NFS3_diropargs_sz+2+NFS3_sattr_sz)
Chuck Leverad96b5b2010-12-14 14:56:01 +000059#define NFS3_removeargs_sz (NFS3_fh_sz+NFS3_filename_sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define NFS3_renameargs_sz (NFS3_diropargs_sz+NFS3_diropargs_sz)
61#define NFS3_linkargs_sz (NFS3_fh_sz+NFS3_diropargs_sz)
Chuck Leverd9c407b2010-12-14 14:55:50 +000062#define NFS3_readdirargs_sz (NFS3_fh_sz+NFS3_cookieverf_sz+3)
63#define NFS3_readdirplusargs_sz (NFS3_fh_sz+NFS3_cookieverf_sz+4)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#define NFS3_commitargs_sz (NFS3_fh_sz+3)
65
Chuck Leverf5fc3c502010-12-14 14:56:42 +000066#define NFS3_getattrres_sz (1+NFS3_fattr_sz)
67#define NFS3_setattrres_sz (1+NFS3_wcc_data_sz)
68#define NFS3_removeres_sz (NFS3_setattrres_sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#define NFS3_lookupres_sz (1+NFS3_fh_sz+(2 * NFS3_post_op_attr_sz))
70#define NFS3_accessres_sz (1+NFS3_post_op_attr_sz+1)
71#define NFS3_readlinkres_sz (1+NFS3_post_op_attr_sz+1)
72#define NFS3_readres_sz (1+NFS3_post_op_attr_sz+3)
73#define NFS3_writeres_sz (1+NFS3_wcc_data_sz+4)
Frank Sorenson55ef4f22021-03-08 12:12:13 -060074#define NFS3_createres_sz (1+NFS3_post_op_fh_sz+NFS3_post_op_attr_sz+NFS3_wcc_data_sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define NFS3_renameres_sz (1+(2 * NFS3_wcc_data_sz))
76#define NFS3_linkres_sz (1+NFS3_post_op_attr_sz+NFS3_wcc_data_sz)
77#define NFS3_readdirres_sz (1+NFS3_post_op_attr_sz+2)
78#define NFS3_fsstatres_sz (1+NFS3_post_op_attr_sz+13)
79#define NFS3_fsinfores_sz (1+NFS3_post_op_attr_sz+12)
80#define NFS3_pathconfres_sz (1+NFS3_post_op_attr_sz+6)
81#define NFS3_commitres_sz (1+NFS3_wcc_data_sz+2)
82
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000083#define ACL3_getaclargs_sz (NFS3_fh_sz+1)
Trond Myklebustae461412009-03-10 20:33:18 -040084#define ACL3_setaclargs_sz (NFS3_fh_sz+1+ \
85 XDR_QUADLEN(NFS_ACL_INLINE_BUFSIZE))
86#define ACL3_getaclres_sz (1+NFS3_post_op_attr_sz+1+ \
87 XDR_QUADLEN(NFS_ACL_INLINE_BUFSIZE))
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +000088#define ACL3_setaclres_sz (1+NFS3_post_op_attr_sz)
89
Bryan Schumaker5e7e5a02012-05-10 16:47:18 -040090static int nfs3_stat_to_errno(enum nfs_stat);
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/*
93 * Map file type to S_IFMT bits
94 */
Trond Myklebustbca79472009-03-11 14:10:26 -040095static const umode_t nfs_type2fmt[] = {
96 [NF3BAD] = 0,
97 [NF3REG] = S_IFREG,
98 [NF3DIR] = S_IFDIR,
99 [NF3BLK] = S_IFBLK,
100 [NF3CHR] = S_IFCHR,
101 [NF3LNK] = S_IFLNK,
102 [NF3SOCK] = S_IFSOCK,
103 [NF3FIFO] = S_IFIFO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
105
106/*
Chuck Leverd9c407b2010-12-14 14:55:50 +0000107 * While encoding arguments, set up the reply buffer in advance to
108 * receive reply data directly into the page cache.
109 */
110static void prepare_reply_buffer(struct rpc_rqst *req, struct page **pages,
111 unsigned int base, unsigned int len,
112 unsigned int bufsize)
113{
114 struct rpc_auth *auth = req->rq_cred->cr_auth;
115 unsigned int replen;
116
117 replen = RPC_REPHDRSIZE + auth->au_rslack + bufsize;
118 xdr_inline_pages(&req->rq_rcv_buf, replen << 2, pages, base, len);
119}
120
Chuck Levere4f93232010-12-14 14:56:30 +0000121/*
122 * Handle decode buffer overflows out-of-line.
123 */
124static void print_overflow_msg(const char *func, const struct xdr_stream *xdr)
125{
126 dprintk("NFS: %s prematurely hit the end of our receive buffer. "
127 "Remaining buffer length is %tu words.\n",
128 func, xdr->end - xdr->p);
129}
130
Chuck Leverd9c407b2010-12-14 14:55:50 +0000131
132/*
Chuck Leverd9c407b2010-12-14 14:55:50 +0000133 * Encode/decode NFSv3 basic data types
134 *
135 * Basic NFSv3 data types are defined in section 2.5 of RFC 1813:
136 * "NFS Version 3 Protocol Specification".
137 *
138 * Not all basic data types have their own encoding and decoding
139 * functions. For run-time efficiency, some data types are encoded
140 * or decoded inline.
141 */
142
143static void encode_uint32(struct xdr_stream *xdr, u32 value)
144{
145 __be32 *p = xdr_reserve_space(xdr, 4);
146 *p = cpu_to_be32(value);
147}
148
Chuck Levere4f93232010-12-14 14:56:30 +0000149static int decode_uint32(struct xdr_stream *xdr, u32 *value)
150{
151 __be32 *p;
152
153 p = xdr_inline_decode(xdr, 4);
154 if (unlikely(p == NULL))
155 goto out_overflow;
156 *value = be32_to_cpup(p);
157 return 0;
158out_overflow:
159 print_overflow_msg(__func__, xdr);
160 return -EIO;
161}
162
163static int decode_uint64(struct xdr_stream *xdr, u64 *value)
164{
165 __be32 *p;
166
167 p = xdr_inline_decode(xdr, 8);
168 if (unlikely(p == NULL))
169 goto out_overflow;
170 xdr_decode_hyper(p, value);
171 return 0;
172out_overflow:
173 print_overflow_msg(__func__, xdr);
174 return -EIO;
175}
176
177/*
178 * fileid3
179 *
180 * typedef uint64 fileid3;
181 */
Chuck Leverf6048702010-12-14 14:57:02 +0000182static __be32 *xdr_decode_fileid3(__be32 *p, u64 *fileid)
183{
184 return xdr_decode_hyper(p, fileid);
185}
186
Chuck Levere4f93232010-12-14 14:56:30 +0000187static int decode_fileid3(struct xdr_stream *xdr, u64 *fileid)
188{
189 return decode_uint64(xdr, fileid);
190}
191
Chuck Leverd9c407b2010-12-14 14:55:50 +0000192/*
193 * filename3
194 *
195 * typedef string filename3<>;
196 */
197static void encode_filename3(struct xdr_stream *xdr,
198 const char *name, u32 length)
199{
200 __be32 *p;
201
Trond Myklebust7fc38842012-10-15 11:51:21 -0400202 WARN_ON_ONCE(length > NFS3_MAXNAMLEN);
Chuck Leverd9c407b2010-12-14 14:55:50 +0000203 p = xdr_reserve_space(xdr, 4 + length);
204 xdr_encode_opaque(p, name, length);
205}
206
Chuck Levere4f93232010-12-14 14:56:30 +0000207static int decode_inline_filename3(struct xdr_stream *xdr,
208 const char **name, u32 *length)
209{
210 __be32 *p;
211 u32 count;
212
213 p = xdr_inline_decode(xdr, 4);
214 if (unlikely(p == NULL))
215 goto out_overflow;
216 count = be32_to_cpup(p);
217 if (count > NFS3_MAXNAMLEN)
218 goto out_nametoolong;
219 p = xdr_inline_decode(xdr, count);
220 if (unlikely(p == NULL))
221 goto out_overflow;
222 *name = (const char *)p;
223 *length = count;
224 return 0;
225
226out_nametoolong:
227 dprintk("NFS: returned filename too long: %u\n", count);
228 return -ENAMETOOLONG;
229out_overflow:
230 print_overflow_msg(__func__, xdr);
231 return -EIO;
232}
233
Chuck Leverd9c407b2010-12-14 14:55:50 +0000234/*
235 * nfspath3
236 *
237 * typedef string nfspath3<>;
238 */
239static void encode_nfspath3(struct xdr_stream *xdr, struct page **pages,
240 const u32 length)
241{
Chuck Leverd9c407b2010-12-14 14:55:50 +0000242 encode_uint32(xdr, length);
243 xdr_write_pages(xdr, pages, 0, length);
244}
245
Chuck Levere4f93232010-12-14 14:56:30 +0000246static int decode_nfspath3(struct xdr_stream *xdr)
247{
248 u32 recvd, count;
Chuck Levere4f93232010-12-14 14:56:30 +0000249 __be32 *p;
250
251 p = xdr_inline_decode(xdr, 4);
252 if (unlikely(p == NULL))
253 goto out_overflow;
254 count = be32_to_cpup(p);
255 if (unlikely(count >= xdr->buf->page_len || count > NFS3_MAXPATHLEN))
256 goto out_nametoolong;
Trond Myklebust64bd5772012-06-20 22:35:05 -0400257 recvd = xdr_read_pages(xdr, count);
Chuck Levere4f93232010-12-14 14:56:30 +0000258 if (unlikely(count > recvd))
259 goto out_cheating;
Chuck Levere4f93232010-12-14 14:56:30 +0000260 xdr_terminate_string(xdr->buf, count);
261 return 0;
262
263out_nametoolong:
264 dprintk("NFS: returned pathname too long: %u\n", count);
265 return -ENAMETOOLONG;
266out_cheating:
267 dprintk("NFS: server cheating in pathname result: "
268 "count %u > recvd %u\n", count, recvd);
269 return -EIO;
270out_overflow:
271 print_overflow_msg(__func__, xdr);
272 return -EIO;
273}
274
Chuck Leverd9c407b2010-12-14 14:55:50 +0000275/*
276 * cookie3
277 *
278 * typedef uint64 cookie3
279 */
280static __be32 *xdr_encode_cookie3(__be32 *p, u64 cookie)
281{
282 return xdr_encode_hyper(p, cookie);
283}
284
Chuck Levere4f93232010-12-14 14:56:30 +0000285static int decode_cookie3(struct xdr_stream *xdr, u64 *cookie)
286{
287 return decode_uint64(xdr, cookie);
288}
289
Chuck Leverd9c407b2010-12-14 14:55:50 +0000290/*
291 * cookieverf3
292 *
293 * typedef opaque cookieverf3[NFS3_COOKIEVERFSIZE];
294 */
295static __be32 *xdr_encode_cookieverf3(__be32 *p, const __be32 *verifier)
296{
297 memcpy(p, verifier, NFS3_COOKIEVERFSIZE);
298 return p + XDR_QUADLEN(NFS3_COOKIEVERFSIZE);
299}
300
Chuck Levere4f93232010-12-14 14:56:30 +0000301static int decode_cookieverf3(struct xdr_stream *xdr, __be32 *verifier)
302{
303 __be32 *p;
304
305 p = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE);
306 if (unlikely(p == NULL))
307 goto out_overflow;
308 memcpy(verifier, p, NFS3_COOKIEVERFSIZE);
309 return 0;
310out_overflow:
311 print_overflow_msg(__func__, xdr);
312 return -EIO;
313}
314
Chuck Leverd9c407b2010-12-14 14:55:50 +0000315/*
316 * createverf3
317 *
318 * typedef opaque createverf3[NFS3_CREATEVERFSIZE];
319 */
320static void encode_createverf3(struct xdr_stream *xdr, const __be32 *verifier)
321{
322 __be32 *p;
323
324 p = xdr_reserve_space(xdr, NFS3_CREATEVERFSIZE);
325 memcpy(p, verifier, NFS3_CREATEVERFSIZE);
326}
327
Trond Myklebust2f2c63b2012-06-08 11:56:09 -0400328static int decode_writeverf3(struct xdr_stream *xdr, struct nfs_write_verifier *verifier)
Chuck Levere4f93232010-12-14 14:56:30 +0000329{
330 __be32 *p;
331
332 p = xdr_inline_decode(xdr, NFS3_WRITEVERFSIZE);
333 if (unlikely(p == NULL))
334 goto out_overflow;
Trond Myklebust2f2c63b2012-06-08 11:56:09 -0400335 memcpy(verifier->data, p, NFS3_WRITEVERFSIZE);
Chuck Levere4f93232010-12-14 14:56:30 +0000336 return 0;
337out_overflow:
338 print_overflow_msg(__func__, xdr);
339 return -EIO;
340}
341
342/*
343 * size3
344 *
345 * typedef uint64 size3;
346 */
347static __be32 *xdr_decode_size3(__be32 *p, u64 *size)
348{
349 return xdr_decode_hyper(p, size);
350}
351
352/*
353 * nfsstat3
354 *
355 * enum nfsstat3 {
356 * NFS3_OK = 0,
357 * ...
358 * }
359 */
360#define NFS3_OK NFS_OK
361
362static int decode_nfsstat3(struct xdr_stream *xdr, enum nfs_stat *status)
363{
364 __be32 *p;
365
366 p = xdr_inline_decode(xdr, 4);
367 if (unlikely(p == NULL))
368 goto out_overflow;
369 *status = be32_to_cpup(p);
370 return 0;
371out_overflow:
372 print_overflow_msg(__func__, xdr);
373 return -EIO;
374}
375
Chuck Leverd9c407b2010-12-14 14:55:50 +0000376/*
377 * ftype3
378 *
379 * enum ftype3 {
380 * NF3REG = 1,
381 * NF3DIR = 2,
382 * NF3BLK = 3,
383 * NF3CHR = 4,
384 * NF3LNK = 5,
385 * NF3SOCK = 6,
386 * NF3FIFO = 7
387 * };
388 */
389static void encode_ftype3(struct xdr_stream *xdr, const u32 type)
390{
Chuck Leverd9c407b2010-12-14 14:55:50 +0000391 encode_uint32(xdr, type);
392}
393
Chuck Leverf6048702010-12-14 14:57:02 +0000394static __be32 *xdr_decode_ftype3(__be32 *p, umode_t *mode)
395{
396 u32 type;
397
398 type = be32_to_cpup(p++);
399 if (type > NF3FIFO)
400 type = NF3NON;
401 *mode = nfs_type2fmt[type];
402 return p;
403}
404
Chuck Leverd9c407b2010-12-14 14:55:50 +0000405/*
406 * specdata3
407 *
408 * struct specdata3 {
409 * uint32 specdata1;
410 * uint32 specdata2;
411 * };
412 */
413static void encode_specdata3(struct xdr_stream *xdr, const dev_t rdev)
414{
415 __be32 *p;
416
417 p = xdr_reserve_space(xdr, 8);
418 *p++ = cpu_to_be32(MAJOR(rdev));
419 *p = cpu_to_be32(MINOR(rdev));
420}
421
Chuck Leverf6048702010-12-14 14:57:02 +0000422static __be32 *xdr_decode_specdata3(__be32 *p, dev_t *rdev)
423{
424 unsigned int major, minor;
425
426 major = be32_to_cpup(p++);
427 minor = be32_to_cpup(p++);
428 *rdev = MKDEV(major, minor);
429 if (MAJOR(*rdev) != major || MINOR(*rdev) != minor)
430 *rdev = 0;
431 return p;
432}
433
Chuck Leverd9c407b2010-12-14 14:55:50 +0000434/*
435 * nfs_fh3
436 *
437 * struct nfs_fh3 {
438 * opaque data<NFS3_FHSIZE>;
439 * };
440 */
441static void encode_nfs_fh3(struct xdr_stream *xdr, const struct nfs_fh *fh)
442{
443 __be32 *p;
444
Trond Myklebust7fc38842012-10-15 11:51:21 -0400445 WARN_ON_ONCE(fh->size > NFS3_FHSIZE);
Chuck Leverd9c407b2010-12-14 14:55:50 +0000446 p = xdr_reserve_space(xdr, 4 + fh->size);
447 xdr_encode_opaque(p, fh->data, fh->size);
448}
449
Chuck Levere4f93232010-12-14 14:56:30 +0000450static int decode_nfs_fh3(struct xdr_stream *xdr, struct nfs_fh *fh)
451{
452 u32 length;
453 __be32 *p;
454
455 p = xdr_inline_decode(xdr, 4);
456 if (unlikely(p == NULL))
457 goto out_overflow;
458 length = be32_to_cpup(p++);
459 if (unlikely(length > NFS3_FHSIZE))
460 goto out_toobig;
461 p = xdr_inline_decode(xdr, length);
462 if (unlikely(p == NULL))
463 goto out_overflow;
464 fh->size = length;
465 memcpy(fh->data, p, length);
466 return 0;
467out_toobig:
468 dprintk("NFS: file handle size (%u) too big\n", length);
469 return -E2BIG;
470out_overflow:
471 print_overflow_msg(__func__, xdr);
472 return -EIO;
473}
474
475static void zero_nfs_fh3(struct nfs_fh *fh)
476{
477 memset(fh, 0, sizeof(*fh));
478}
479
Chuck Leverd9c407b2010-12-14 14:55:50 +0000480/*
Chuck Lever9d5a6432010-12-14 14:56:20 +0000481 * nfstime3
482 *
483 * struct nfstime3 {
484 * uint32 seconds;
485 * uint32 nseconds;
486 * };
487 */
488static __be32 *xdr_encode_nfstime3(__be32 *p, const struct timespec *timep)
489{
490 *p++ = cpu_to_be32(timep->tv_sec);
491 *p++ = cpu_to_be32(timep->tv_nsec);
492 return p;
493}
494
Chuck Leverf6048702010-12-14 14:57:02 +0000495static __be32 *xdr_decode_nfstime3(__be32 *p, struct timespec *timep)
496{
497 timep->tv_sec = be32_to_cpup(p++);
498 timep->tv_nsec = be32_to_cpup(p++);
499 return p;
500}
501
Chuck Lever9d5a6432010-12-14 14:56:20 +0000502/*
Chuck Leverd9c407b2010-12-14 14:55:50 +0000503 * sattr3
504 *
505 * enum time_how {
506 * DONT_CHANGE = 0,
507 * SET_TO_SERVER_TIME = 1,
508 * SET_TO_CLIENT_TIME = 2
509 * };
510 *
511 * union set_mode3 switch (bool set_it) {
512 * case TRUE:
513 * mode3 mode;
514 * default:
515 * void;
516 * };
517 *
518 * union set_uid3 switch (bool set_it) {
519 * case TRUE:
520 * uid3 uid;
521 * default:
522 * void;
523 * };
524 *
525 * union set_gid3 switch (bool set_it) {
526 * case TRUE:
527 * gid3 gid;
528 * default:
529 * void;
530 * };
531 *
532 * union set_size3 switch (bool set_it) {
533 * case TRUE:
534 * size3 size;
535 * default:
536 * void;
537 * };
538 *
539 * union set_atime switch (time_how set_it) {
540 * case SET_TO_CLIENT_TIME:
541 * nfstime3 atime;
542 * default:
543 * void;
544 * };
545 *
546 * union set_mtime switch (time_how set_it) {
547 * case SET_TO_CLIENT_TIME:
548 * nfstime3 mtime;
549 * default:
550 * void;
551 * };
552 *
553 * struct sattr3 {
554 * set_mode3 mode;
555 * set_uid3 uid;
556 * set_gid3 gid;
557 * set_size3 size;
558 * set_atime atime;
559 * set_mtime mtime;
560 * };
561 */
562static void encode_sattr3(struct xdr_stream *xdr, const struct iattr *attr)
563{
564 u32 nbytes;
565 __be32 *p;
566
567 /*
568 * In order to make only a single xdr_reserve_space() call,
569 * pre-compute the total number of bytes to be reserved.
570 * Six boolean values, one for each set_foo field, are always
571 * present in the encoded result, so start there.
572 */
573 nbytes = 6 * 4;
574 if (attr->ia_valid & ATTR_MODE)
575 nbytes += 4;
576 if (attr->ia_valid & ATTR_UID)
577 nbytes += 4;
578 if (attr->ia_valid & ATTR_GID)
579 nbytes += 4;
580 if (attr->ia_valid & ATTR_SIZE)
581 nbytes += 8;
582 if (attr->ia_valid & ATTR_ATIME_SET)
583 nbytes += 8;
584 if (attr->ia_valid & ATTR_MTIME_SET)
585 nbytes += 8;
586 p = xdr_reserve_space(xdr, nbytes);
587
Chuck Lever9d5a6432010-12-14 14:56:20 +0000588 if (attr->ia_valid & ATTR_MODE) {
589 *p++ = xdr_one;
590 *p++ = cpu_to_be32(attr->ia_mode & S_IALLUGO);
591 } else
592 *p++ = xdr_zero;
593
594 if (attr->ia_valid & ATTR_UID) {
595 *p++ = xdr_one;
Eric W. Biederman57a38da2013-02-01 03:50:00 -0800596 *p++ = cpu_to_be32(from_kuid(&init_user_ns, attr->ia_uid));
Chuck Lever9d5a6432010-12-14 14:56:20 +0000597 } else
598 *p++ = xdr_zero;
599
600 if (attr->ia_valid & ATTR_GID) {
601 *p++ = xdr_one;
Eric W. Biederman57a38da2013-02-01 03:50:00 -0800602 *p++ = cpu_to_be32(from_kgid(&init_user_ns, attr->ia_gid));
Chuck Lever9d5a6432010-12-14 14:56:20 +0000603 } else
604 *p++ = xdr_zero;
605
606 if (attr->ia_valid & ATTR_SIZE) {
607 *p++ = xdr_one;
608 p = xdr_encode_hyper(p, (u64)attr->ia_size);
609 } else
610 *p++ = xdr_zero;
611
612 if (attr->ia_valid & ATTR_ATIME_SET) {
613 *p++ = xdr_two;
614 p = xdr_encode_nfstime3(p, &attr->ia_atime);
615 } else if (attr->ia_valid & ATTR_ATIME) {
616 *p++ = xdr_one;
617 } else
618 *p++ = xdr_zero;
619
620 if (attr->ia_valid & ATTR_MTIME_SET) {
621 *p++ = xdr_two;
622 xdr_encode_nfstime3(p, &attr->ia_mtime);
623 } else if (attr->ia_valid & ATTR_MTIME) {
624 *p = xdr_one;
625 } else
626 *p = xdr_zero;
Chuck Leverd9c407b2010-12-14 14:55:50 +0000627}
628
629/*
Chuck Levere4f93232010-12-14 14:56:30 +0000630 * fattr3
631 *
632 * struct fattr3 {
633 * ftype3 type;
634 * mode3 mode;
635 * uint32 nlink;
636 * uid3 uid;
637 * gid3 gid;
638 * size3 size;
639 * size3 used;
640 * specdata3 rdev;
641 * uint64 fsid;
642 * fileid3 fileid;
643 * nfstime3 atime;
644 * nfstime3 mtime;
645 * nfstime3 ctime;
646 * };
647 */
648static int decode_fattr3(struct xdr_stream *xdr, struct nfs_fattr *fattr)
649{
Chuck Leverf6048702010-12-14 14:57:02 +0000650 umode_t fmode;
Chuck Levere4f93232010-12-14 14:56:30 +0000651 __be32 *p;
652
653 p = xdr_inline_decode(xdr, NFS3_fattr_sz << 2);
654 if (unlikely(p == NULL))
655 goto out_overflow;
Chuck Leverf6048702010-12-14 14:57:02 +0000656
657 p = xdr_decode_ftype3(p, &fmode);
658
659 fattr->mode = (be32_to_cpup(p++) & ~S_IFMT) | fmode;
660 fattr->nlink = be32_to_cpup(p++);
Eric W. Biederman57a38da2013-02-01 03:50:00 -0800661 fattr->uid = make_kuid(&init_user_ns, be32_to_cpup(p++));
662 if (!uid_valid(fattr->uid))
663 goto out_uid;
664 fattr->gid = make_kgid(&init_user_ns, be32_to_cpup(p++));
665 if (!gid_valid(fattr->gid))
666 goto out_gid;
Chuck Leverf6048702010-12-14 14:57:02 +0000667
668 p = xdr_decode_size3(p, &fattr->size);
669 p = xdr_decode_size3(p, &fattr->du.nfs3.used);
670 p = xdr_decode_specdata3(p, &fattr->rdev);
671
672 p = xdr_decode_hyper(p, &fattr->fsid.major);
673 fattr->fsid.minor = 0;
674
675 p = xdr_decode_fileid3(p, &fattr->fileid);
676 p = xdr_decode_nfstime3(p, &fattr->atime);
677 p = xdr_decode_nfstime3(p, &fattr->mtime);
678 xdr_decode_nfstime3(p, &fattr->ctime);
Trond Myklebust3a1556e2012-04-27 13:48:18 -0400679 fattr->change_attr = nfs_timespec_to_change_attr(&fattr->ctime);
Chuck Leverf6048702010-12-14 14:57:02 +0000680
681 fattr->valid |= NFS_ATTR_FATTR_V3;
Chuck Levere4f93232010-12-14 14:56:30 +0000682 return 0;
Eric W. Biederman57a38da2013-02-01 03:50:00 -0800683out_uid:
684 dprintk("NFS: returned invalid uid\n");
685 return -EINVAL;
686out_gid:
687 dprintk("NFS: returned invalid gid\n");
688 return -EINVAL;
Chuck Levere4f93232010-12-14 14:56:30 +0000689out_overflow:
690 print_overflow_msg(__func__, xdr);
691 return -EIO;
692}
693
694/*
695 * post_op_attr
696 *
697 * union post_op_attr switch (bool attributes_follow) {
698 * case TRUE:
699 * fattr3 attributes;
700 * case FALSE:
701 * void;
702 * };
703 */
704static int decode_post_op_attr(struct xdr_stream *xdr, struct nfs_fattr *fattr)
705{
706 __be32 *p;
707
708 p = xdr_inline_decode(xdr, 4);
709 if (unlikely(p == NULL))
710 goto out_overflow;
711 if (*p != xdr_zero)
712 return decode_fattr3(xdr, fattr);
713 return 0;
714out_overflow:
715 print_overflow_msg(__func__, xdr);
716 return -EIO;
717}
718
719/*
720 * wcc_attr
721 * struct wcc_attr {
722 * size3 size;
723 * nfstime3 mtime;
724 * nfstime3 ctime;
725 * };
726 */
727static int decode_wcc_attr(struct xdr_stream *xdr, struct nfs_fattr *fattr)
728{
729 __be32 *p;
730
731 p = xdr_inline_decode(xdr, NFS3_wcc_attr_sz << 2);
732 if (unlikely(p == NULL))
733 goto out_overflow;
Chuck Leverf6048702010-12-14 14:57:02 +0000734
735 fattr->valid |= NFS_ATTR_FATTR_PRESIZE
Trond Myklebust3a1556e2012-04-27 13:48:18 -0400736 | NFS_ATTR_FATTR_PRECHANGE
Chuck Leverf6048702010-12-14 14:57:02 +0000737 | NFS_ATTR_FATTR_PREMTIME
738 | NFS_ATTR_FATTR_PRECTIME;
739
740 p = xdr_decode_size3(p, &fattr->pre_size);
741 p = xdr_decode_nfstime3(p, &fattr->pre_mtime);
742 xdr_decode_nfstime3(p, &fattr->pre_ctime);
Trond Myklebust3a1556e2012-04-27 13:48:18 -0400743 fattr->pre_change_attr = nfs_timespec_to_change_attr(&fattr->pre_ctime);
Chuck Leverf6048702010-12-14 14:57:02 +0000744
Chuck Levere4f93232010-12-14 14:56:30 +0000745 return 0;
746out_overflow:
747 print_overflow_msg(__func__, xdr);
748 return -EIO;
749}
750
751/*
752 * pre_op_attr
753 * union pre_op_attr switch (bool attributes_follow) {
754 * case TRUE:
755 * wcc_attr attributes;
756 * case FALSE:
757 * void;
758 * };
759 *
760 * wcc_data
761 *
762 * struct wcc_data {
763 * pre_op_attr before;
764 * post_op_attr after;
765 * };
766 */
767static int decode_pre_op_attr(struct xdr_stream *xdr, struct nfs_fattr *fattr)
768{
769 __be32 *p;
770
771 p = xdr_inline_decode(xdr, 4);
772 if (unlikely(p == NULL))
773 goto out_overflow;
774 if (*p != xdr_zero)
775 return decode_wcc_attr(xdr, fattr);
776 return 0;
777out_overflow:
778 print_overflow_msg(__func__, xdr);
779 return -EIO;
780}
781
782static int decode_wcc_data(struct xdr_stream *xdr, struct nfs_fattr *fattr)
783{
784 int error;
785
786 error = decode_pre_op_attr(xdr, fattr);
787 if (unlikely(error))
788 goto out;
789 error = decode_post_op_attr(xdr, fattr);
790out:
791 return error;
792}
793
794/*
795 * post_op_fh3
796 *
797 * union post_op_fh3 switch (bool handle_follows) {
798 * case TRUE:
799 * nfs_fh3 handle;
800 * case FALSE:
801 * void;
802 * };
803 */
804static int decode_post_op_fh3(struct xdr_stream *xdr, struct nfs_fh *fh)
805{
806 __be32 *p = xdr_inline_decode(xdr, 4);
807 if (unlikely(p == NULL))
808 goto out_overflow;
809 if (*p != xdr_zero)
810 return decode_nfs_fh3(xdr, fh);
811 zero_nfs_fh3(fh);
812 return 0;
813out_overflow:
814 print_overflow_msg(__func__, xdr);
815 return -EIO;
816}
817
818/*
Chuck Leverd9c407b2010-12-14 14:55:50 +0000819 * diropargs3
820 *
821 * struct diropargs3 {
822 * nfs_fh3 dir;
823 * filename3 name;
824 * };
825 */
826static void encode_diropargs3(struct xdr_stream *xdr, const struct nfs_fh *fh,
827 const char *name, u32 length)
828{
829 encode_nfs_fh3(xdr, fh);
830 encode_filename3(xdr, name, length);
831}
832
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834/*
Chuck Lever499ff712010-12-14 14:56:10 +0000835 * NFSv3 XDR encode functions
836 *
837 * NFSv3 argument types are defined in section 3.3 of RFC 1813:
838 * "NFS Version 3 Protocol Specification".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 */
840
841/*
Chuck Leverd9c407b2010-12-14 14:55:50 +0000842 * 3.3.1 GETATTR3args
843 *
844 * struct GETATTR3args {
845 * nfs_fh3 object;
846 * };
847 */
Chuck Lever9f06c712010-12-14 14:59:18 +0000848static void nfs3_xdr_enc_getattr3args(struct rpc_rqst *req,
849 struct xdr_stream *xdr,
850 const struct nfs_fh *fh)
Chuck Leverd9c407b2010-12-14 14:55:50 +0000851{
Chuck Lever9f06c712010-12-14 14:59:18 +0000852 encode_nfs_fh3(xdr, fh);
Chuck Leverd9c407b2010-12-14 14:55:50 +0000853}
854
855/*
Chuck Leverd9c407b2010-12-14 14:55:50 +0000856 * 3.3.2 SETATTR3args
857 *
858 * union sattrguard3 switch (bool check) {
859 * case TRUE:
860 * nfstime3 obj_ctime;
861 * case FALSE:
862 * void;
863 * };
864 *
865 * struct SETATTR3args {
866 * nfs_fh3 object;
867 * sattr3 new_attributes;
868 * sattrguard3 guard;
869 * };
870 */
871static void encode_sattrguard3(struct xdr_stream *xdr,
872 const struct nfs3_sattrargs *args)
873{
874 __be32 *p;
875
876 if (args->guard) {
877 p = xdr_reserve_space(xdr, 4 + 8);
878 *p++ = xdr_one;
Chuck Lever9d5a6432010-12-14 14:56:20 +0000879 xdr_encode_nfstime3(p, &args->guardtime);
Chuck Leverd9c407b2010-12-14 14:55:50 +0000880 } else {
881 p = xdr_reserve_space(xdr, 4);
882 *p = xdr_zero;
883 }
884}
885
Chuck Lever9f06c712010-12-14 14:59:18 +0000886static void nfs3_xdr_enc_setattr3args(struct rpc_rqst *req,
887 struct xdr_stream *xdr,
888 const struct nfs3_sattrargs *args)
Chuck Leverd9c407b2010-12-14 14:55:50 +0000889{
Chuck Lever9f06c712010-12-14 14:59:18 +0000890 encode_nfs_fh3(xdr, args->fh);
891 encode_sattr3(xdr, args->sattr);
892 encode_sattrguard3(xdr, args);
Chuck Leverd9c407b2010-12-14 14:55:50 +0000893}
894
895/*
Chuck Leverd9c407b2010-12-14 14:55:50 +0000896 * 3.3.3 LOOKUP3args
897 *
898 * struct LOOKUP3args {
899 * diropargs3 what;
900 * };
901 */
Chuck Lever9f06c712010-12-14 14:59:18 +0000902static void nfs3_xdr_enc_lookup3args(struct rpc_rqst *req,
903 struct xdr_stream *xdr,
904 const struct nfs3_diropargs *args)
Chuck Leverd9c407b2010-12-14 14:55:50 +0000905{
Chuck Lever9f06c712010-12-14 14:59:18 +0000906 encode_diropargs3(xdr, args->fh, args->name, args->len);
Chuck Leverd9c407b2010-12-14 14:55:50 +0000907}
908
909/*
Chuck Leverd9c407b2010-12-14 14:55:50 +0000910 * 3.3.4 ACCESS3args
911 *
912 * struct ACCESS3args {
913 * nfs_fh3 object;
914 * uint32 access;
915 * };
916 */
917static void encode_access3args(struct xdr_stream *xdr,
918 const struct nfs3_accessargs *args)
919{
920 encode_nfs_fh3(xdr, args->fh);
921 encode_uint32(xdr, args->access);
922}
923
Chuck Lever9f06c712010-12-14 14:59:18 +0000924static void nfs3_xdr_enc_access3args(struct rpc_rqst *req,
925 struct xdr_stream *xdr,
926 const struct nfs3_accessargs *args)
Chuck Leverd9c407b2010-12-14 14:55:50 +0000927{
Chuck Lever9f06c712010-12-14 14:59:18 +0000928 encode_access3args(xdr, args);
Chuck Leverd9c407b2010-12-14 14:55:50 +0000929}
930
931/*
932 * 3.3.5 READLINK3args
933 *
934 * struct READLINK3args {
935 * nfs_fh3 symlink;
936 * };
937 */
Chuck Lever9f06c712010-12-14 14:59:18 +0000938static void nfs3_xdr_enc_readlink3args(struct rpc_rqst *req,
939 struct xdr_stream *xdr,
940 const struct nfs3_readlinkargs *args)
Chuck Leverd9c407b2010-12-14 14:55:50 +0000941{
Chuck Lever9f06c712010-12-14 14:59:18 +0000942 encode_nfs_fh3(xdr, args->fh);
Chuck Leverd9c407b2010-12-14 14:55:50 +0000943 prepare_reply_buffer(req, args->pages, args->pgbase,
944 args->pglen, NFS3_readlinkres_sz);
Chuck Leverd9c407b2010-12-14 14:55:50 +0000945}
946
947/*
Chuck Leverd9c407b2010-12-14 14:55:50 +0000948 * 3.3.6 READ3args
949 *
950 * struct READ3args {
951 * nfs_fh3 file;
952 * offset3 offset;
953 * count3 count;
954 * };
955 */
956static void encode_read3args(struct xdr_stream *xdr,
Anna Schumaker3c6b8992014-05-06 09:12:24 -0400957 const struct nfs_pgio_args *args)
Chuck Leverd9c407b2010-12-14 14:55:50 +0000958{
959 __be32 *p;
960
961 encode_nfs_fh3(xdr, args->fh);
962
963 p = xdr_reserve_space(xdr, 8 + 4);
964 p = xdr_encode_hyper(p, args->offset);
965 *p = cpu_to_be32(args->count);
966}
967
Chuck Lever9f06c712010-12-14 14:59:18 +0000968static void nfs3_xdr_enc_read3args(struct rpc_rqst *req,
969 struct xdr_stream *xdr,
Anna Schumaker3c6b8992014-05-06 09:12:24 -0400970 const struct nfs_pgio_args *args)
Chuck Leverd9c407b2010-12-14 14:55:50 +0000971{
Chuck Lever9f06c712010-12-14 14:59:18 +0000972 encode_read3args(xdr, args);
Chuck Leverd9c407b2010-12-14 14:55:50 +0000973 prepare_reply_buffer(req, args->pages, args->pgbase,
974 args->count, NFS3_readres_sz);
975 req->rq_rcv_buf.flags |= XDRBUF_READ;
Chuck Leverd9c407b2010-12-14 14:55:50 +0000976}
977
978/*
Chuck Leverd9c407b2010-12-14 14:55:50 +0000979 * 3.3.7 WRITE3args
980 *
981 * enum stable_how {
982 * UNSTABLE = 0,
983 * DATA_SYNC = 1,
984 * FILE_SYNC = 2
985 * };
986 *
987 * struct WRITE3args {
988 * nfs_fh3 file;
989 * offset3 offset;
990 * count3 count;
991 * stable_how stable;
992 * opaque data<>;
993 * };
994 */
995static void encode_write3args(struct xdr_stream *xdr,
Anna Schumaker3c6b8992014-05-06 09:12:24 -0400996 const struct nfs_pgio_args *args)
Chuck Leverd9c407b2010-12-14 14:55:50 +0000997{
998 __be32 *p;
999
1000 encode_nfs_fh3(xdr, args->fh);
1001
1002 p = xdr_reserve_space(xdr, 8 + 4 + 4 + 4);
1003 p = xdr_encode_hyper(p, args->offset);
1004 *p++ = cpu_to_be32(args->count);
Chuck Leverd9c407b2010-12-14 14:55:50 +00001005 *p++ = cpu_to_be32(args->stable);
Chuck Leverd9c407b2010-12-14 14:55:50 +00001006 *p = cpu_to_be32(args->count);
1007 xdr_write_pages(xdr, args->pages, args->pgbase, args->count);
1008}
1009
Chuck Lever9f06c712010-12-14 14:59:18 +00001010static void nfs3_xdr_enc_write3args(struct rpc_rqst *req,
1011 struct xdr_stream *xdr,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04001012 const struct nfs_pgio_args *args)
Chuck Leverd9c407b2010-12-14 14:55:50 +00001013{
Chuck Lever9f06c712010-12-14 14:59:18 +00001014 encode_write3args(xdr, args);
1015 xdr->buf->flags |= XDRBUF_WRITE;
Chuck Leverd9c407b2010-12-14 14:55:50 +00001016}
1017
1018/*
Chuck Leverd9c407b2010-12-14 14:55:50 +00001019 * 3.3.8 CREATE3args
1020 *
1021 * enum createmode3 {
1022 * UNCHECKED = 0,
1023 * GUARDED = 1,
1024 * EXCLUSIVE = 2
1025 * };
1026 *
1027 * union createhow3 switch (createmode3 mode) {
1028 * case UNCHECKED:
1029 * case GUARDED:
1030 * sattr3 obj_attributes;
1031 * case EXCLUSIVE:
1032 * createverf3 verf;
1033 * };
1034 *
1035 * struct CREATE3args {
1036 * diropargs3 where;
1037 * createhow3 how;
1038 * };
1039 */
1040static void encode_createhow3(struct xdr_stream *xdr,
1041 const struct nfs3_createargs *args)
1042{
1043 encode_uint32(xdr, args->createmode);
1044 switch (args->createmode) {
1045 case NFS3_CREATE_UNCHECKED:
1046 case NFS3_CREATE_GUARDED:
1047 encode_sattr3(xdr, args->sattr);
1048 break;
1049 case NFS3_CREATE_EXCLUSIVE:
1050 encode_createverf3(xdr, args->verifier);
1051 break;
1052 default:
1053 BUG();
1054 }
1055}
1056
Chuck Lever9f06c712010-12-14 14:59:18 +00001057static void nfs3_xdr_enc_create3args(struct rpc_rqst *req,
1058 struct xdr_stream *xdr,
1059 const struct nfs3_createargs *args)
Chuck Leverd9c407b2010-12-14 14:55:50 +00001060{
Chuck Lever9f06c712010-12-14 14:59:18 +00001061 encode_diropargs3(xdr, args->fh, args->name, args->len);
1062 encode_createhow3(xdr, args);
Chuck Leverd9c407b2010-12-14 14:55:50 +00001063}
1064
1065/*
Chuck Leverd9c407b2010-12-14 14:55:50 +00001066 * 3.3.9 MKDIR3args
1067 *
1068 * struct MKDIR3args {
1069 * diropargs3 where;
1070 * sattr3 attributes;
1071 * };
1072 */
Chuck Lever9f06c712010-12-14 14:59:18 +00001073static void nfs3_xdr_enc_mkdir3args(struct rpc_rqst *req,
1074 struct xdr_stream *xdr,
1075 const struct nfs3_mkdirargs *args)
Chuck Leverd9c407b2010-12-14 14:55:50 +00001076{
Chuck Lever9f06c712010-12-14 14:59:18 +00001077 encode_diropargs3(xdr, args->fh, args->name, args->len);
1078 encode_sattr3(xdr, args->sattr);
Chuck Leverd9c407b2010-12-14 14:55:50 +00001079}
1080
1081/*
Chuck Leverd9c407b2010-12-14 14:55:50 +00001082 * 3.3.10 SYMLINK3args
1083 *
1084 * struct symlinkdata3 {
1085 * sattr3 symlink_attributes;
1086 * nfspath3 symlink_data;
1087 * };
1088 *
1089 * struct SYMLINK3args {
1090 * diropargs3 where;
1091 * symlinkdata3 symlink;
1092 * };
1093 */
1094static void encode_symlinkdata3(struct xdr_stream *xdr,
1095 const struct nfs3_symlinkargs *args)
1096{
1097 encode_sattr3(xdr, args->sattr);
1098 encode_nfspath3(xdr, args->pages, args->pathlen);
1099}
1100
Chuck Lever9f06c712010-12-14 14:59:18 +00001101static void nfs3_xdr_enc_symlink3args(struct rpc_rqst *req,
1102 struct xdr_stream *xdr,
1103 const struct nfs3_symlinkargs *args)
Chuck Leverd9c407b2010-12-14 14:55:50 +00001104{
Chuck Lever9f06c712010-12-14 14:59:18 +00001105 encode_diropargs3(xdr, args->fromfh, args->fromname, args->fromlen);
1106 encode_symlinkdata3(xdr, args);
Chuck Lever2fcc2132015-08-03 13:04:26 -04001107 xdr->buf->flags |= XDRBUF_WRITE;
Chuck Leverd9c407b2010-12-14 14:55:50 +00001108}
1109
1110/*
Chuck Leverd9c407b2010-12-14 14:55:50 +00001111 * 3.3.11 MKNOD3args
1112 *
1113 * struct devicedata3 {
1114 * sattr3 dev_attributes;
1115 * specdata3 spec;
1116 * };
1117 *
1118 * union mknoddata3 switch (ftype3 type) {
1119 * case NF3CHR:
1120 * case NF3BLK:
1121 * devicedata3 device;
1122 * case NF3SOCK:
1123 * case NF3FIFO:
1124 * sattr3 pipe_attributes;
1125 * default:
1126 * void;
1127 * };
1128 *
1129 * struct MKNOD3args {
1130 * diropargs3 where;
1131 * mknoddata3 what;
1132 * };
1133 */
1134static void encode_devicedata3(struct xdr_stream *xdr,
1135 const struct nfs3_mknodargs *args)
1136{
1137 encode_sattr3(xdr, args->sattr);
1138 encode_specdata3(xdr, args->rdev);
1139}
1140
1141static void encode_mknoddata3(struct xdr_stream *xdr,
1142 const struct nfs3_mknodargs *args)
1143{
1144 encode_ftype3(xdr, args->type);
1145 switch (args->type) {
1146 case NF3CHR:
1147 case NF3BLK:
1148 encode_devicedata3(xdr, args);
1149 break;
1150 case NF3SOCK:
1151 case NF3FIFO:
1152 encode_sattr3(xdr, args->sattr);
1153 break;
1154 case NF3REG:
1155 case NF3DIR:
1156 break;
1157 default:
1158 BUG();
1159 }
1160}
1161
Chuck Lever9f06c712010-12-14 14:59:18 +00001162static void nfs3_xdr_enc_mknod3args(struct rpc_rqst *req,
1163 struct xdr_stream *xdr,
1164 const struct nfs3_mknodargs *args)
Chuck Leverd9c407b2010-12-14 14:55:50 +00001165{
Chuck Lever9f06c712010-12-14 14:59:18 +00001166 encode_diropargs3(xdr, args->fh, args->name, args->len);
1167 encode_mknoddata3(xdr, args);
Chuck Leverd9c407b2010-12-14 14:55:50 +00001168}
1169
1170/*
1171 * 3.3.12 REMOVE3args
1172 *
1173 * struct REMOVE3args {
1174 * diropargs3 object;
1175 * };
1176 */
Chuck Lever9f06c712010-12-14 14:59:18 +00001177static void nfs3_xdr_enc_remove3args(struct rpc_rqst *req,
1178 struct xdr_stream *xdr,
1179 const struct nfs_removeargs *args)
Chuck Leverd9c407b2010-12-14 14:55:50 +00001180{
Chuck Lever9f06c712010-12-14 14:59:18 +00001181 encode_diropargs3(xdr, args->fh, args->name.name, args->name.len);
Chuck Leverd9c407b2010-12-14 14:55:50 +00001182}
1183
1184/*
Chuck Leverd9c407b2010-12-14 14:55:50 +00001185 * 3.3.14 RENAME3args
1186 *
1187 * struct RENAME3args {
1188 * diropargs3 from;
1189 * diropargs3 to;
1190 * };
1191 */
Chuck Lever9f06c712010-12-14 14:59:18 +00001192static void nfs3_xdr_enc_rename3args(struct rpc_rqst *req,
1193 struct xdr_stream *xdr,
1194 const struct nfs_renameargs *args)
Chuck Leverd9c407b2010-12-14 14:55:50 +00001195{
1196 const struct qstr *old = args->old_name;
1197 const struct qstr *new = args->new_name;
Chuck Leverd9c407b2010-12-14 14:55:50 +00001198
Chuck Lever9f06c712010-12-14 14:59:18 +00001199 encode_diropargs3(xdr, args->old_dir, old->name, old->len);
1200 encode_diropargs3(xdr, args->new_dir, new->name, new->len);
Chuck Leverd9c407b2010-12-14 14:55:50 +00001201}
1202
1203/*
Chuck Leverd9c407b2010-12-14 14:55:50 +00001204 * 3.3.15 LINK3args
1205 *
1206 * struct LINK3args {
1207 * nfs_fh3 file;
1208 * diropargs3 link;
1209 * };
1210 */
Chuck Lever9f06c712010-12-14 14:59:18 +00001211static void nfs3_xdr_enc_link3args(struct rpc_rqst *req,
1212 struct xdr_stream *xdr,
1213 const struct nfs3_linkargs *args)
Chuck Leverd9c407b2010-12-14 14:55:50 +00001214{
Chuck Lever9f06c712010-12-14 14:59:18 +00001215 encode_nfs_fh3(xdr, args->fromfh);
1216 encode_diropargs3(xdr, args->tofh, args->toname, args->tolen);
Chuck Leverd9c407b2010-12-14 14:55:50 +00001217}
1218
1219/*
Chuck Leverd9c407b2010-12-14 14:55:50 +00001220 * 3.3.16 READDIR3args
1221 *
1222 * struct READDIR3args {
1223 * nfs_fh3 dir;
1224 * cookie3 cookie;
1225 * cookieverf3 cookieverf;
1226 * count3 count;
1227 * };
1228 */
1229static void encode_readdir3args(struct xdr_stream *xdr,
1230 const struct nfs3_readdirargs *args)
1231{
1232 __be32 *p;
1233
1234 encode_nfs_fh3(xdr, args->fh);
1235
1236 p = xdr_reserve_space(xdr, 8 + NFS3_COOKIEVERFSIZE + 4);
1237 p = xdr_encode_cookie3(p, args->cookie);
1238 p = xdr_encode_cookieverf3(p, args->verf);
1239 *p = cpu_to_be32(args->count);
1240}
1241
Chuck Lever9f06c712010-12-14 14:59:18 +00001242static void nfs3_xdr_enc_readdir3args(struct rpc_rqst *req,
1243 struct xdr_stream *xdr,
1244 const struct nfs3_readdirargs *args)
Chuck Leverd9c407b2010-12-14 14:55:50 +00001245{
Chuck Lever9f06c712010-12-14 14:59:18 +00001246 encode_readdir3args(xdr, args);
Chuck Leverd9c407b2010-12-14 14:55:50 +00001247 prepare_reply_buffer(req, args->pages, 0,
1248 args->count, NFS3_readdirres_sz);
Chuck Leverd9c407b2010-12-14 14:55:50 +00001249}
1250
1251/*
1252 * 3.3.17 READDIRPLUS3args
1253 *
1254 * struct READDIRPLUS3args {
1255 * nfs_fh3 dir;
1256 * cookie3 cookie;
1257 * cookieverf3 cookieverf;
1258 * count3 dircount;
1259 * count3 maxcount;
1260 * };
1261 */
1262static void encode_readdirplus3args(struct xdr_stream *xdr,
1263 const struct nfs3_readdirargs *args)
1264{
1265 __be32 *p;
1266
1267 encode_nfs_fh3(xdr, args->fh);
1268
1269 p = xdr_reserve_space(xdr, 8 + NFS3_COOKIEVERFSIZE + 4 + 4);
1270 p = xdr_encode_cookie3(p, args->cookie);
1271 p = xdr_encode_cookieverf3(p, args->verf);
1272
1273 /*
1274 * readdirplus: need dircount + buffer size.
1275 * We just make sure we make dircount big enough
1276 */
1277 *p++ = cpu_to_be32(args->count >> 3);
1278
1279 *p = cpu_to_be32(args->count);
1280}
1281
Chuck Lever9f06c712010-12-14 14:59:18 +00001282static void nfs3_xdr_enc_readdirplus3args(struct rpc_rqst *req,
1283 struct xdr_stream *xdr,
1284 const struct nfs3_readdirargs *args)
Chuck Leverd9c407b2010-12-14 14:55:50 +00001285{
Chuck Lever9f06c712010-12-14 14:59:18 +00001286 encode_readdirplus3args(xdr, args);
Chuck Leverd9c407b2010-12-14 14:55:50 +00001287 prepare_reply_buffer(req, args->pages, 0,
1288 args->count, NFS3_readdirres_sz);
Chuck Leverd9c407b2010-12-14 14:55:50 +00001289}
1290
1291/*
Chuck Leverd9c407b2010-12-14 14:55:50 +00001292 * 3.3.21 COMMIT3args
1293 *
1294 * struct COMMIT3args {
1295 * nfs_fh3 file;
1296 * offset3 offset;
1297 * count3 count;
1298 * };
1299 */
1300static void encode_commit3args(struct xdr_stream *xdr,
Fred Isaman0b7c0152012-04-20 14:47:39 -04001301 const struct nfs_commitargs *args)
Chuck Leverd9c407b2010-12-14 14:55:50 +00001302{
1303 __be32 *p;
1304
1305 encode_nfs_fh3(xdr, args->fh);
1306
1307 p = xdr_reserve_space(xdr, 8 + 4);
1308 p = xdr_encode_hyper(p, args->offset);
1309 *p = cpu_to_be32(args->count);
1310}
1311
Chuck Lever9f06c712010-12-14 14:59:18 +00001312static void nfs3_xdr_enc_commit3args(struct rpc_rqst *req,
1313 struct xdr_stream *xdr,
Fred Isaman0b7c0152012-04-20 14:47:39 -04001314 const struct nfs_commitargs *args)
Chuck Leverd9c407b2010-12-14 14:55:50 +00001315{
Chuck Lever9f06c712010-12-14 14:59:18 +00001316 encode_commit3args(xdr, args);
Chuck Leverd9c407b2010-12-14 14:55:50 +00001317}
1318
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +00001319#ifdef CONFIG_NFS_V3_ACL
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +00001320
Chuck Lever9f06c712010-12-14 14:59:18 +00001321static void nfs3_xdr_enc_getacl3args(struct rpc_rqst *req,
1322 struct xdr_stream *xdr,
1323 const struct nfs3_getaclargs *args)
Chuck Leverd9c407b2010-12-14 14:55:50 +00001324{
Chuck Lever9f06c712010-12-14 14:59:18 +00001325 encode_nfs_fh3(xdr, args->fh);
1326 encode_uint32(xdr, args->mask);
Chuck Leverd9c407b2010-12-14 14:55:50 +00001327 if (args->mask & (NFS_ACL | NFS_DFACL))
1328 prepare_reply_buffer(req, args->pages, 0,
1329 NFSACL_MAXPAGES << PAGE_SHIFT,
1330 ACL3_getaclres_sz);
Chuck Leverd9c407b2010-12-14 14:55:50 +00001331}
1332
Chuck Lever9f06c712010-12-14 14:59:18 +00001333static void nfs3_xdr_enc_setacl3args(struct rpc_rqst *req,
1334 struct xdr_stream *xdr,
1335 const struct nfs3_setaclargs *args)
Chuck Leverd9c407b2010-12-14 14:55:50 +00001336{
Chuck Leverd9c407b2010-12-14 14:55:50 +00001337 unsigned int base;
1338 int error;
1339
Chuck Lever9f06c712010-12-14 14:59:18 +00001340 encode_nfs_fh3(xdr, NFS_FH(args->inode));
1341 encode_uint32(xdr, args->mask);
Chuck Leverd9c407b2010-12-14 14:55:50 +00001342
1343 base = req->rq_slen;
Chuck Leveree5dc772011-01-21 03:05:18 +00001344 if (args->npages != 0)
1345 xdr_write_pages(xdr, args->pages, 0, args->len);
1346 else
Chuck Leverd683cc42015-05-26 11:53:52 -04001347 xdr_reserve_space(xdr, args->len);
Chuck Leveree5dc772011-01-21 03:05:18 +00001348
Chuck Lever9f06c712010-12-14 14:59:18 +00001349 error = nfsacl_encode(xdr->buf, base, args->inode,
Chuck Leverd9c407b2010-12-14 14:55:50 +00001350 (args->mask & NFS_ACL) ?
1351 args->acl_access : NULL, 1, 0);
Trond Myklebust7fc38842012-10-15 11:51:21 -04001352 /* FIXME: this is just broken */
Chuck Leverd9c407b2010-12-14 14:55:50 +00001353 BUG_ON(error < 0);
Chuck Lever9f06c712010-12-14 14:59:18 +00001354 error = nfsacl_encode(xdr->buf, base + error, args->inode,
Chuck Leverd9c407b2010-12-14 14:55:50 +00001355 (args->mask & NFS_DFACL) ?
1356 args->acl_default : NULL, 1,
1357 NFS_ACL_DEFAULT);
1358 BUG_ON(error < 0);
Chuck Leverd9c407b2010-12-14 14:55:50 +00001359}
1360
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +00001361#endif /* CONFIG_NFS_V3_ACL */
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363/*
Chuck Leverb2cdd9c2010-12-14 14:56:52 +00001364 * NFSv3 XDR decode functions
1365 *
1366 * NFSv3 result types are defined in section 3.3 of RFC 1813:
1367 * "NFS Version 3 Protocol Specification".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 */
1369
1370/*
Chuck Levere4f93232010-12-14 14:56:30 +00001371 * 3.3.1 GETATTR3res
1372 *
1373 * struct GETATTR3resok {
1374 * fattr3 obj_attributes;
1375 * };
1376 *
1377 * union GETATTR3res switch (nfsstat3 status) {
1378 * case NFS3_OK:
1379 * GETATTR3resok resok;
1380 * default:
1381 * void;
1382 * };
1383 */
Chuck Leverbf269552010-12-14 14:59:29 +00001384static int nfs3_xdr_dec_getattr3res(struct rpc_rqst *req,
1385 struct xdr_stream *xdr,
Chuck Levere4f93232010-12-14 14:56:30 +00001386 struct nfs_fattr *result)
1387{
Chuck Levere4f93232010-12-14 14:56:30 +00001388 enum nfs_stat status;
1389 int error;
1390
Chuck Leverbf269552010-12-14 14:59:29 +00001391 error = decode_nfsstat3(xdr, &status);
Chuck Levere4f93232010-12-14 14:56:30 +00001392 if (unlikely(error))
1393 goto out;
1394 if (status != NFS3_OK)
1395 goto out_default;
Chuck Leverbf269552010-12-14 14:59:29 +00001396 error = decode_fattr3(xdr, result);
Chuck Levere4f93232010-12-14 14:56:30 +00001397out:
1398 return error;
1399out_default:
Bryan Schumaker5e7e5a02012-05-10 16:47:18 -04001400 return nfs3_stat_to_errno(status);
Chuck Levere4f93232010-12-14 14:56:30 +00001401}
1402
1403/*
Chuck Levere4f93232010-12-14 14:56:30 +00001404 * 3.3.2 SETATTR3res
1405 *
1406 * struct SETATTR3resok {
1407 * wcc_data obj_wcc;
1408 * };
1409 *
1410 * struct SETATTR3resfail {
1411 * wcc_data obj_wcc;
1412 * };
1413 *
1414 * union SETATTR3res switch (nfsstat3 status) {
1415 * case NFS3_OK:
1416 * SETATTR3resok resok;
1417 * default:
1418 * SETATTR3resfail resfail;
1419 * };
1420 */
Chuck Leverbf269552010-12-14 14:59:29 +00001421static int nfs3_xdr_dec_setattr3res(struct rpc_rqst *req,
1422 struct xdr_stream *xdr,
Chuck Levere4f93232010-12-14 14:56:30 +00001423 struct nfs_fattr *result)
1424{
Chuck Levere4f93232010-12-14 14:56:30 +00001425 enum nfs_stat status;
1426 int error;
1427
Chuck Leverbf269552010-12-14 14:59:29 +00001428 error = decode_nfsstat3(xdr, &status);
Chuck Levere4f93232010-12-14 14:56:30 +00001429 if (unlikely(error))
1430 goto out;
Chuck Leverbf269552010-12-14 14:59:29 +00001431 error = decode_wcc_data(xdr, result);
Chuck Levere4f93232010-12-14 14:56:30 +00001432 if (unlikely(error))
1433 goto out;
1434 if (status != NFS3_OK)
1435 goto out_status;
1436out:
1437 return error;
1438out_status:
Bryan Schumaker5e7e5a02012-05-10 16:47:18 -04001439 return nfs3_stat_to_errno(status);
Chuck Levere4f93232010-12-14 14:56:30 +00001440}
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442/*
Chuck Levere4f93232010-12-14 14:56:30 +00001443 * 3.3.3 LOOKUP3res
1444 *
1445 * struct LOOKUP3resok {
1446 * nfs_fh3 object;
1447 * post_op_attr obj_attributes;
1448 * post_op_attr dir_attributes;
1449 * };
1450 *
1451 * struct LOOKUP3resfail {
1452 * post_op_attr dir_attributes;
1453 * };
1454 *
1455 * union LOOKUP3res switch (nfsstat3 status) {
1456 * case NFS3_OK:
1457 * LOOKUP3resok resok;
1458 * default:
1459 * LOOKUP3resfail resfail;
1460 * };
1461 */
Chuck Leverbf269552010-12-14 14:59:29 +00001462static int nfs3_xdr_dec_lookup3res(struct rpc_rqst *req,
1463 struct xdr_stream *xdr,
Chuck Levere4f93232010-12-14 14:56:30 +00001464 struct nfs3_diropres *result)
1465{
Chuck Levere4f93232010-12-14 14:56:30 +00001466 enum nfs_stat status;
1467 int error;
1468
Chuck Leverbf269552010-12-14 14:59:29 +00001469 error = decode_nfsstat3(xdr, &status);
Chuck Levere4f93232010-12-14 14:56:30 +00001470 if (unlikely(error))
1471 goto out;
1472 if (status != NFS3_OK)
1473 goto out_default;
Chuck Leverbf269552010-12-14 14:59:29 +00001474 error = decode_nfs_fh3(xdr, result->fh);
Chuck Levere4f93232010-12-14 14:56:30 +00001475 if (unlikely(error))
1476 goto out;
Chuck Leverbf269552010-12-14 14:59:29 +00001477 error = decode_post_op_attr(xdr, result->fattr);
Chuck Levere4f93232010-12-14 14:56:30 +00001478 if (unlikely(error))
1479 goto out;
Chuck Leverbf269552010-12-14 14:59:29 +00001480 error = decode_post_op_attr(xdr, result->dir_attr);
Chuck Levere4f93232010-12-14 14:56:30 +00001481out:
1482 return error;
1483out_default:
Chuck Leverbf269552010-12-14 14:59:29 +00001484 error = decode_post_op_attr(xdr, result->dir_attr);
Chuck Levere4f93232010-12-14 14:56:30 +00001485 if (unlikely(error))
1486 goto out;
Bryan Schumaker5e7e5a02012-05-10 16:47:18 -04001487 return nfs3_stat_to_errno(status);
Chuck Levere4f93232010-12-14 14:56:30 +00001488}
1489
1490/*
Chuck Levere4f93232010-12-14 14:56:30 +00001491 * 3.3.4 ACCESS3res
1492 *
1493 * struct ACCESS3resok {
1494 * post_op_attr obj_attributes;
1495 * uint32 access;
1496 * };
1497 *
1498 * struct ACCESS3resfail {
1499 * post_op_attr obj_attributes;
1500 * };
1501 *
1502 * union ACCESS3res switch (nfsstat3 status) {
1503 * case NFS3_OK:
1504 * ACCESS3resok resok;
1505 * default:
1506 * ACCESS3resfail resfail;
1507 * };
1508 */
Chuck Leverbf269552010-12-14 14:59:29 +00001509static int nfs3_xdr_dec_access3res(struct rpc_rqst *req,
1510 struct xdr_stream *xdr,
Chuck Levere4f93232010-12-14 14:56:30 +00001511 struct nfs3_accessres *result)
1512{
Chuck Levere4f93232010-12-14 14:56:30 +00001513 enum nfs_stat status;
1514 int error;
1515
Chuck Leverbf269552010-12-14 14:59:29 +00001516 error = decode_nfsstat3(xdr, &status);
Chuck Levere4f93232010-12-14 14:56:30 +00001517 if (unlikely(error))
1518 goto out;
Chuck Leverbf269552010-12-14 14:59:29 +00001519 error = decode_post_op_attr(xdr, result->fattr);
Chuck Levere4f93232010-12-14 14:56:30 +00001520 if (unlikely(error))
1521 goto out;
1522 if (status != NFS3_OK)
1523 goto out_default;
Chuck Leverbf269552010-12-14 14:59:29 +00001524 error = decode_uint32(xdr, &result->access);
Chuck Levere4f93232010-12-14 14:56:30 +00001525out:
1526 return error;
1527out_default:
Bryan Schumaker5e7e5a02012-05-10 16:47:18 -04001528 return nfs3_stat_to_errno(status);
Chuck Levere4f93232010-12-14 14:56:30 +00001529}
1530
1531/*
Chuck Levere4f93232010-12-14 14:56:30 +00001532 * 3.3.5 READLINK3res
1533 *
1534 * struct READLINK3resok {
1535 * post_op_attr symlink_attributes;
1536 * nfspath3 data;
1537 * };
1538 *
1539 * struct READLINK3resfail {
1540 * post_op_attr symlink_attributes;
1541 * };
1542 *
1543 * union READLINK3res switch (nfsstat3 status) {
1544 * case NFS3_OK:
1545 * READLINK3resok resok;
1546 * default:
1547 * READLINK3resfail resfail;
1548 * };
1549 */
Chuck Leverbf269552010-12-14 14:59:29 +00001550static int nfs3_xdr_dec_readlink3res(struct rpc_rqst *req,
1551 struct xdr_stream *xdr,
Chuck Levere4f93232010-12-14 14:56:30 +00001552 struct nfs_fattr *result)
1553{
Chuck Levere4f93232010-12-14 14:56:30 +00001554 enum nfs_stat status;
1555 int error;
1556
Chuck Leverbf269552010-12-14 14:59:29 +00001557 error = decode_nfsstat3(xdr, &status);
Chuck Levere4f93232010-12-14 14:56:30 +00001558 if (unlikely(error))
1559 goto out;
Chuck Leverbf269552010-12-14 14:59:29 +00001560 error = decode_post_op_attr(xdr, result);
Chuck Levere4f93232010-12-14 14:56:30 +00001561 if (unlikely(error))
1562 goto out;
1563 if (status != NFS3_OK)
1564 goto out_default;
Chuck Leverbf269552010-12-14 14:59:29 +00001565 error = decode_nfspath3(xdr);
Chuck Levere4f93232010-12-14 14:56:30 +00001566out:
1567 return error;
1568out_default:
Bryan Schumaker5e7e5a02012-05-10 16:47:18 -04001569 return nfs3_stat_to_errno(status);
Chuck Levere4f93232010-12-14 14:56:30 +00001570}
1571
1572/*
Chuck Levere4f93232010-12-14 14:56:30 +00001573 * 3.3.6 READ3res
1574 *
1575 * struct READ3resok {
1576 * post_op_attr file_attributes;
1577 * count3 count;
1578 * bool eof;
1579 * opaque data<>;
1580 * };
1581 *
1582 * struct READ3resfail {
1583 * post_op_attr file_attributes;
1584 * };
1585 *
1586 * union READ3res switch (nfsstat3 status) {
1587 * case NFS3_OK:
1588 * READ3resok resok;
1589 * default:
1590 * READ3resfail resfail;
1591 * };
1592 */
1593static int decode_read3resok(struct xdr_stream *xdr,
Anna Schumaker9137bdf2014-05-06 09:12:25 -04001594 struct nfs_pgio_res *result)
Chuck Levere4f93232010-12-14 14:56:30 +00001595{
1596 u32 eof, count, ocount, recvd;
Chuck Levere4f93232010-12-14 14:56:30 +00001597 __be32 *p;
1598
1599 p = xdr_inline_decode(xdr, 4 + 4 + 4);
1600 if (unlikely(p == NULL))
1601 goto out_overflow;
1602 count = be32_to_cpup(p++);
1603 eof = be32_to_cpup(p++);
1604 ocount = be32_to_cpup(p++);
1605 if (unlikely(ocount != count))
1606 goto out_mismatch;
Trond Myklebust64bd5772012-06-20 22:35:05 -04001607 recvd = xdr_read_pages(xdr, count);
Chuck Levere4f93232010-12-14 14:56:30 +00001608 if (unlikely(count > recvd))
1609 goto out_cheating;
Chuck Levere4f93232010-12-14 14:56:30 +00001610out:
Chuck Levere4f93232010-12-14 14:56:30 +00001611 result->eof = eof;
1612 result->count = count;
1613 return count;
1614out_mismatch:
1615 dprintk("NFS: READ count doesn't match length of opaque: "
1616 "count %u != ocount %u\n", count, ocount);
1617 return -EIO;
1618out_cheating:
1619 dprintk("NFS: server cheating in read result: "
1620 "count %u > recvd %u\n", count, recvd);
1621 count = recvd;
1622 eof = 0;
1623 goto out;
1624out_overflow:
1625 print_overflow_msg(__func__, xdr);
1626 return -EIO;
1627}
1628
Chuck Leverbf269552010-12-14 14:59:29 +00001629static int nfs3_xdr_dec_read3res(struct rpc_rqst *req, struct xdr_stream *xdr,
Anna Schumaker9137bdf2014-05-06 09:12:25 -04001630 struct nfs_pgio_res *result)
Chuck Levere4f93232010-12-14 14:56:30 +00001631{
Chuck Levere4f93232010-12-14 14:56:30 +00001632 enum nfs_stat status;
1633 int error;
1634
Chuck Leverbf269552010-12-14 14:59:29 +00001635 error = decode_nfsstat3(xdr, &status);
Chuck Levere4f93232010-12-14 14:56:30 +00001636 if (unlikely(error))
1637 goto out;
Chuck Leverbf269552010-12-14 14:59:29 +00001638 error = decode_post_op_attr(xdr, result->fattr);
Chuck Levere4f93232010-12-14 14:56:30 +00001639 if (unlikely(error))
1640 goto out;
Peng Taoaabff4d2014-08-27 10:47:14 +08001641 result->op_status = status;
Chuck Levere4f93232010-12-14 14:56:30 +00001642 if (status != NFS3_OK)
1643 goto out_status;
Chuck Leverbf269552010-12-14 14:59:29 +00001644 error = decode_read3resok(xdr, result);
Chuck Levere4f93232010-12-14 14:56:30 +00001645out:
1646 return error;
1647out_status:
Bryan Schumaker5e7e5a02012-05-10 16:47:18 -04001648 return nfs3_stat_to_errno(status);
Chuck Levere4f93232010-12-14 14:56:30 +00001649}
1650
1651/*
Chuck Levere4f93232010-12-14 14:56:30 +00001652 * 3.3.7 WRITE3res
1653 *
1654 * enum stable_how {
1655 * UNSTABLE = 0,
1656 * DATA_SYNC = 1,
1657 * FILE_SYNC = 2
1658 * };
1659 *
1660 * struct WRITE3resok {
1661 * wcc_data file_wcc;
1662 * count3 count;
1663 * stable_how committed;
1664 * writeverf3 verf;
1665 * };
1666 *
1667 * struct WRITE3resfail {
1668 * wcc_data file_wcc;
1669 * };
1670 *
1671 * union WRITE3res switch (nfsstat3 status) {
1672 * case NFS3_OK:
1673 * WRITE3resok resok;
1674 * default:
1675 * WRITE3resfail resfail;
1676 * };
1677 */
1678static int decode_write3resok(struct xdr_stream *xdr,
Anna Schumaker9137bdf2014-05-06 09:12:25 -04001679 struct nfs_pgio_res *result)
Chuck Levere4f93232010-12-14 14:56:30 +00001680{
1681 __be32 *p;
1682
Trond Myklebust2f2c63b2012-06-08 11:56:09 -04001683 p = xdr_inline_decode(xdr, 4 + 4);
Chuck Levere4f93232010-12-14 14:56:30 +00001684 if (unlikely(p == NULL))
1685 goto out_overflow;
1686 result->count = be32_to_cpup(p++);
1687 result->verf->committed = be32_to_cpup(p++);
1688 if (unlikely(result->verf->committed > NFS_FILE_SYNC))
1689 goto out_badvalue;
Trond Myklebust2f2c63b2012-06-08 11:56:09 -04001690 if (decode_writeverf3(xdr, &result->verf->verifier))
1691 goto out_eio;
Chuck Levere4f93232010-12-14 14:56:30 +00001692 return result->count;
1693out_badvalue:
1694 dprintk("NFS: bad stable_how value: %u\n", result->verf->committed);
1695 return -EIO;
1696out_overflow:
1697 print_overflow_msg(__func__, xdr);
Trond Myklebust2f2c63b2012-06-08 11:56:09 -04001698out_eio:
Chuck Levere4f93232010-12-14 14:56:30 +00001699 return -EIO;
1700}
1701
Chuck Leverbf269552010-12-14 14:59:29 +00001702static int nfs3_xdr_dec_write3res(struct rpc_rqst *req, struct xdr_stream *xdr,
Anna Schumaker9137bdf2014-05-06 09:12:25 -04001703 struct nfs_pgio_res *result)
Chuck Levere4f93232010-12-14 14:56:30 +00001704{
Chuck Levere4f93232010-12-14 14:56:30 +00001705 enum nfs_stat status;
1706 int error;
1707
Chuck Leverbf269552010-12-14 14:59:29 +00001708 error = decode_nfsstat3(xdr, &status);
Chuck Levere4f93232010-12-14 14:56:30 +00001709 if (unlikely(error))
1710 goto out;
Chuck Leverbf269552010-12-14 14:59:29 +00001711 error = decode_wcc_data(xdr, result->fattr);
Chuck Levere4f93232010-12-14 14:56:30 +00001712 if (unlikely(error))
1713 goto out;
Peng Taoaabff4d2014-08-27 10:47:14 +08001714 result->op_status = status;
Chuck Levere4f93232010-12-14 14:56:30 +00001715 if (status != NFS3_OK)
1716 goto out_status;
Chuck Leverbf269552010-12-14 14:59:29 +00001717 error = decode_write3resok(xdr, result);
Chuck Levere4f93232010-12-14 14:56:30 +00001718out:
1719 return error;
1720out_status:
Bryan Schumaker5e7e5a02012-05-10 16:47:18 -04001721 return nfs3_stat_to_errno(status);
Chuck Levere4f93232010-12-14 14:56:30 +00001722}
1723
1724/*
Chuck Levere4f93232010-12-14 14:56:30 +00001725 * 3.3.8 CREATE3res
1726 *
1727 * struct CREATE3resok {
1728 * post_op_fh3 obj;
1729 * post_op_attr obj_attributes;
1730 * wcc_data dir_wcc;
1731 * };
1732 *
1733 * struct CREATE3resfail {
1734 * wcc_data dir_wcc;
1735 * };
1736 *
1737 * union CREATE3res switch (nfsstat3 status) {
1738 * case NFS3_OK:
1739 * CREATE3resok resok;
1740 * default:
1741 * CREATE3resfail resfail;
1742 * };
1743 */
1744static int decode_create3resok(struct xdr_stream *xdr,
1745 struct nfs3_diropres *result)
1746{
1747 int error;
1748
1749 error = decode_post_op_fh3(xdr, result->fh);
1750 if (unlikely(error))
1751 goto out;
1752 error = decode_post_op_attr(xdr, result->fattr);
1753 if (unlikely(error))
1754 goto out;
1755 /* The server isn't required to return a file handle.
1756 * If it didn't, force the client to perform a LOOKUP
1757 * to determine the correct file handle and attribute
1758 * values for the new object. */
1759 if (result->fh->size == 0)
1760 result->fattr->valid = 0;
1761 error = decode_wcc_data(xdr, result->dir_attr);
1762out:
1763 return error;
1764}
1765
Chuck Leverbf269552010-12-14 14:59:29 +00001766static int nfs3_xdr_dec_create3res(struct rpc_rqst *req,
1767 struct xdr_stream *xdr,
Chuck Levere4f93232010-12-14 14:56:30 +00001768 struct nfs3_diropres *result)
1769{
Chuck Levere4f93232010-12-14 14:56:30 +00001770 enum nfs_stat status;
1771 int error;
1772
Chuck Leverbf269552010-12-14 14:59:29 +00001773 error = decode_nfsstat3(xdr, &status);
Chuck Levere4f93232010-12-14 14:56:30 +00001774 if (unlikely(error))
1775 goto out;
1776 if (status != NFS3_OK)
1777 goto out_default;
Chuck Leverbf269552010-12-14 14:59:29 +00001778 error = decode_create3resok(xdr, result);
Chuck Levere4f93232010-12-14 14:56:30 +00001779out:
1780 return error;
1781out_default:
Chuck Leverbf269552010-12-14 14:59:29 +00001782 error = decode_wcc_data(xdr, result->dir_attr);
Chuck Levere4f93232010-12-14 14:56:30 +00001783 if (unlikely(error))
1784 goto out;
Bryan Schumaker5e7e5a02012-05-10 16:47:18 -04001785 return nfs3_stat_to_errno(status);
Chuck Levere4f93232010-12-14 14:56:30 +00001786}
1787
1788/*
1789 * 3.3.12 REMOVE3res
1790 *
1791 * struct REMOVE3resok {
1792 * wcc_data dir_wcc;
1793 * };
1794 *
1795 * struct REMOVE3resfail {
1796 * wcc_data dir_wcc;
1797 * };
1798 *
1799 * union REMOVE3res switch (nfsstat3 status) {
1800 * case NFS3_OK:
1801 * REMOVE3resok resok;
1802 * default:
1803 * REMOVE3resfail resfail;
1804 * };
1805 */
Chuck Leverbf269552010-12-14 14:59:29 +00001806static int nfs3_xdr_dec_remove3res(struct rpc_rqst *req,
1807 struct xdr_stream *xdr,
Chuck Levere4f93232010-12-14 14:56:30 +00001808 struct nfs_removeres *result)
1809{
Chuck Levere4f93232010-12-14 14:56:30 +00001810 enum nfs_stat status;
1811 int error;
1812
Chuck Leverbf269552010-12-14 14:59:29 +00001813 error = decode_nfsstat3(xdr, &status);
Chuck Levere4f93232010-12-14 14:56:30 +00001814 if (unlikely(error))
1815 goto out;
Chuck Leverbf269552010-12-14 14:59:29 +00001816 error = decode_wcc_data(xdr, result->dir_attr);
Chuck Levere4f93232010-12-14 14:56:30 +00001817 if (unlikely(error))
1818 goto out;
1819 if (status != NFS3_OK)
1820 goto out_status;
1821out:
1822 return error;
1823out_status:
Bryan Schumaker5e7e5a02012-05-10 16:47:18 -04001824 return nfs3_stat_to_errno(status);
Chuck Levere4f93232010-12-14 14:56:30 +00001825}
1826
1827/*
Chuck Levere4f93232010-12-14 14:56:30 +00001828 * 3.3.14 RENAME3res
1829 *
1830 * struct RENAME3resok {
1831 * wcc_data fromdir_wcc;
1832 * wcc_data todir_wcc;
1833 * };
1834 *
1835 * struct RENAME3resfail {
1836 * wcc_data fromdir_wcc;
1837 * wcc_data todir_wcc;
1838 * };
1839 *
1840 * union RENAME3res switch (nfsstat3 status) {
1841 * case NFS3_OK:
1842 * RENAME3resok resok;
1843 * default:
1844 * RENAME3resfail resfail;
1845 * };
1846 */
Chuck Leverbf269552010-12-14 14:59:29 +00001847static int nfs3_xdr_dec_rename3res(struct rpc_rqst *req,
1848 struct xdr_stream *xdr,
Chuck Levere4f93232010-12-14 14:56:30 +00001849 struct nfs_renameres *result)
1850{
Chuck Levere4f93232010-12-14 14:56:30 +00001851 enum nfs_stat status;
1852 int error;
1853
Chuck Leverbf269552010-12-14 14:59:29 +00001854 error = decode_nfsstat3(xdr, &status);
Chuck Levere4f93232010-12-14 14:56:30 +00001855 if (unlikely(error))
1856 goto out;
Chuck Leverbf269552010-12-14 14:59:29 +00001857 error = decode_wcc_data(xdr, result->old_fattr);
Chuck Levere4f93232010-12-14 14:56:30 +00001858 if (unlikely(error))
1859 goto out;
Chuck Leverbf269552010-12-14 14:59:29 +00001860 error = decode_wcc_data(xdr, result->new_fattr);
Chuck Levere4f93232010-12-14 14:56:30 +00001861 if (unlikely(error))
1862 goto out;
1863 if (status != NFS3_OK)
1864 goto out_status;
1865out:
1866 return error;
1867out_status:
Bryan Schumaker5e7e5a02012-05-10 16:47:18 -04001868 return nfs3_stat_to_errno(status);
Chuck Levere4f93232010-12-14 14:56:30 +00001869}
1870
1871/*
Chuck Levere4f93232010-12-14 14:56:30 +00001872 * 3.3.15 LINK3res
1873 *
1874 * struct LINK3resok {
1875 * post_op_attr file_attributes;
1876 * wcc_data linkdir_wcc;
1877 * };
1878 *
1879 * struct LINK3resfail {
1880 * post_op_attr file_attributes;
1881 * wcc_data linkdir_wcc;
1882 * };
1883 *
1884 * union LINK3res switch (nfsstat3 status) {
1885 * case NFS3_OK:
1886 * LINK3resok resok;
1887 * default:
1888 * LINK3resfail resfail;
1889 * };
1890 */
Chuck Leverbf269552010-12-14 14:59:29 +00001891static int nfs3_xdr_dec_link3res(struct rpc_rqst *req, struct xdr_stream *xdr,
Chuck Levere4f93232010-12-14 14:56:30 +00001892 struct nfs3_linkres *result)
1893{
Chuck Levere4f93232010-12-14 14:56:30 +00001894 enum nfs_stat status;
1895 int error;
1896
Chuck Leverbf269552010-12-14 14:59:29 +00001897 error = decode_nfsstat3(xdr, &status);
Chuck Levere4f93232010-12-14 14:56:30 +00001898 if (unlikely(error))
1899 goto out;
Chuck Leverbf269552010-12-14 14:59:29 +00001900 error = decode_post_op_attr(xdr, result->fattr);
Chuck Levere4f93232010-12-14 14:56:30 +00001901 if (unlikely(error))
1902 goto out;
Chuck Leverbf269552010-12-14 14:59:29 +00001903 error = decode_wcc_data(xdr, result->dir_attr);
Chuck Levere4f93232010-12-14 14:56:30 +00001904 if (unlikely(error))
1905 goto out;
1906 if (status != NFS3_OK)
1907 goto out_status;
1908out:
1909 return error;
1910out_status:
Bryan Schumaker5e7e5a02012-05-10 16:47:18 -04001911 return nfs3_stat_to_errno(status);
Chuck Levere4f93232010-12-14 14:56:30 +00001912}
1913
1914/**
1915 * nfs3_decode_dirent - Decode a single NFSv3 directory entry stored in
1916 * the local page cache
1917 * @xdr: XDR stream where entry resides
1918 * @entry: buffer to fill in with entry data
Chuck Levere4f93232010-12-14 14:56:30 +00001919 * @plus: boolean indicating whether this should be a readdirplus entry
1920 *
Chuck Lever573c4e12010-12-14 14:58:11 +00001921 * Returns zero if successful, otherwise a negative errno value is
1922 * returned.
Chuck Levere4f93232010-12-14 14:56:30 +00001923 *
1924 * This function is not invoked during READDIR reply decoding, but
1925 * rather whenever an application invokes the getdents(2) system call
1926 * on a directory already in our cache.
1927 *
1928 * 3.3.16 entry3
1929 *
1930 * struct entry3 {
1931 * fileid3 fileid;
1932 * filename3 name;
1933 * cookie3 cookie;
1934 * fhandle3 filehandle;
1935 * post_op_attr3 attributes;
1936 * entry3 *nextentry;
1937 * };
1938 *
1939 * 3.3.17 entryplus3
1940 * struct entryplus3 {
1941 * fileid3 fileid;
1942 * filename3 name;
1943 * cookie3 cookie;
1944 * post_op_attr name_attributes;
1945 * post_op_fh3 name_handle;
1946 * entryplus3 *nextentry;
1947 * };
1948 */
Chuck Lever573c4e12010-12-14 14:58:11 +00001949int nfs3_decode_dirent(struct xdr_stream *xdr, struct nfs_entry *entry,
1950 int plus)
Chuck Levere4f93232010-12-14 14:56:30 +00001951{
1952 struct nfs_entry old = *entry;
1953 __be32 *p;
1954 int error;
1955
1956 p = xdr_inline_decode(xdr, 4);
1957 if (unlikely(p == NULL))
1958 goto out_overflow;
1959 if (*p == xdr_zero) {
1960 p = xdr_inline_decode(xdr, 4);
1961 if (unlikely(p == NULL))
1962 goto out_overflow;
1963 if (*p == xdr_zero)
Chuck Lever573c4e12010-12-14 14:58:11 +00001964 return -EAGAIN;
Chuck Levere4f93232010-12-14 14:56:30 +00001965 entry->eof = 1;
Chuck Lever573c4e12010-12-14 14:58:11 +00001966 return -EBADCOOKIE;
Chuck Levere4f93232010-12-14 14:56:30 +00001967 }
1968
1969 error = decode_fileid3(xdr, &entry->ino);
1970 if (unlikely(error))
Chuck Lever573c4e12010-12-14 14:58:11 +00001971 return error;
Chuck Levere4f93232010-12-14 14:56:30 +00001972
1973 error = decode_inline_filename3(xdr, &entry->name, &entry->len);
1974 if (unlikely(error))
Chuck Lever573c4e12010-12-14 14:58:11 +00001975 return error;
Chuck Levere4f93232010-12-14 14:56:30 +00001976
1977 entry->prev_cookie = entry->cookie;
1978 error = decode_cookie3(xdr, &entry->cookie);
1979 if (unlikely(error))
Chuck Lever573c4e12010-12-14 14:58:11 +00001980 return error;
Chuck Levere4f93232010-12-14 14:56:30 +00001981
1982 entry->d_type = DT_UNKNOWN;
1983
1984 if (plus) {
1985 entry->fattr->valid = 0;
1986 error = decode_post_op_attr(xdr, entry->fattr);
1987 if (unlikely(error))
Chuck Lever573c4e12010-12-14 14:58:11 +00001988 return error;
Chuck Levere4f93232010-12-14 14:56:30 +00001989 if (entry->fattr->valid & NFS_ATTR_FATTR_V3)
1990 entry->d_type = nfs_umode_to_dtype(entry->fattr->mode);
1991
Trond Myklebust1ae04b22015-02-23 16:15:00 -05001992 if (entry->fattr->fileid != entry->ino) {
1993 entry->fattr->mounted_on_fileid = entry->ino;
1994 entry->fattr->valid |= NFS_ATTR_FATTR_MOUNTED_ON_FILEID;
1995 }
1996
Chuck Levere4f93232010-12-14 14:56:30 +00001997 /* In fact, a post_op_fh3: */
1998 p = xdr_inline_decode(xdr, 4);
1999 if (unlikely(p == NULL))
2000 goto out_overflow;
2001 if (*p != xdr_zero) {
2002 error = decode_nfs_fh3(xdr, entry->fh);
2003 if (unlikely(error)) {
2004 if (error == -E2BIG)
2005 goto out_truncated;
Chuck Lever573c4e12010-12-14 14:58:11 +00002006 return error;
Chuck Levere4f93232010-12-14 14:56:30 +00002007 }
2008 } else
2009 zero_nfs_fh3(entry->fh);
2010 }
2011
Chuck Lever573c4e12010-12-14 14:58:11 +00002012 return 0;
Chuck Levere4f93232010-12-14 14:56:30 +00002013
2014out_overflow:
2015 print_overflow_msg(__func__, xdr);
Chuck Lever573c4e12010-12-14 14:58:11 +00002016 return -EAGAIN;
Chuck Levere4f93232010-12-14 14:56:30 +00002017out_truncated:
2018 dprintk("NFS: directory entry contains invalid file handle\n");
2019 *entry = old;
Chuck Lever573c4e12010-12-14 14:58:11 +00002020 return -EAGAIN;
Chuck Levere4f93232010-12-14 14:56:30 +00002021}
2022
2023/*
2024 * 3.3.16 READDIR3res
2025 *
2026 * struct dirlist3 {
2027 * entry3 *entries;
2028 * bool eof;
2029 * };
2030 *
2031 * struct READDIR3resok {
2032 * post_op_attr dir_attributes;
2033 * cookieverf3 cookieverf;
2034 * dirlist3 reply;
2035 * };
2036 *
2037 * struct READDIR3resfail {
2038 * post_op_attr dir_attributes;
2039 * };
2040 *
2041 * union READDIR3res switch (nfsstat3 status) {
2042 * case NFS3_OK:
2043 * READDIR3resok resok;
2044 * default:
2045 * READDIR3resfail resfail;
2046 * };
2047 *
2048 * Read the directory contents into the page cache, but otherwise
2049 * don't touch them. The actual decoding is done by nfs3_decode_entry()
2050 * during subsequent nfs_readdir() calls.
2051 */
2052static int decode_dirlist3(struct xdr_stream *xdr)
2053{
Trond Myklebust64bd5772012-06-20 22:35:05 -04002054 return xdr_read_pages(xdr, xdr->buf->page_len);
Chuck Levere4f93232010-12-14 14:56:30 +00002055}
2056
2057static int decode_readdir3resok(struct xdr_stream *xdr,
2058 struct nfs3_readdirres *result)
2059{
2060 int error;
2061
2062 error = decode_post_op_attr(xdr, result->dir_attr);
2063 if (unlikely(error))
2064 goto out;
2065 /* XXX: do we need to check if result->verf != NULL ? */
2066 error = decode_cookieverf3(xdr, result->verf);
2067 if (unlikely(error))
2068 goto out;
2069 error = decode_dirlist3(xdr);
2070out:
2071 return error;
2072}
2073
Chuck Leverbf269552010-12-14 14:59:29 +00002074static int nfs3_xdr_dec_readdir3res(struct rpc_rqst *req,
2075 struct xdr_stream *xdr,
Chuck Levere4f93232010-12-14 14:56:30 +00002076 struct nfs3_readdirres *result)
2077{
Chuck Levere4f93232010-12-14 14:56:30 +00002078 enum nfs_stat status;
2079 int error;
2080
Chuck Leverbf269552010-12-14 14:59:29 +00002081 error = decode_nfsstat3(xdr, &status);
Chuck Levere4f93232010-12-14 14:56:30 +00002082 if (unlikely(error))
2083 goto out;
2084 if (status != NFS3_OK)
2085 goto out_default;
Chuck Leverbf269552010-12-14 14:59:29 +00002086 error = decode_readdir3resok(xdr, result);
Chuck Levere4f93232010-12-14 14:56:30 +00002087out:
2088 return error;
2089out_default:
Chuck Leverbf269552010-12-14 14:59:29 +00002090 error = decode_post_op_attr(xdr, result->dir_attr);
Chuck Levere4f93232010-12-14 14:56:30 +00002091 if (unlikely(error))
2092 goto out;
Bryan Schumaker5e7e5a02012-05-10 16:47:18 -04002093 return nfs3_stat_to_errno(status);
Chuck Levere4f93232010-12-14 14:56:30 +00002094}
2095
2096/*
Chuck Levere4f93232010-12-14 14:56:30 +00002097 * 3.3.18 FSSTAT3res
2098 *
2099 * struct FSSTAT3resok {
2100 * post_op_attr obj_attributes;
2101 * size3 tbytes;
2102 * size3 fbytes;
2103 * size3 abytes;
2104 * size3 tfiles;
2105 * size3 ffiles;
2106 * size3 afiles;
2107 * uint32 invarsec;
2108 * };
2109 *
2110 * struct FSSTAT3resfail {
2111 * post_op_attr obj_attributes;
2112 * };
2113 *
2114 * union FSSTAT3res switch (nfsstat3 status) {
2115 * case NFS3_OK:
2116 * FSSTAT3resok resok;
2117 * default:
2118 * FSSTAT3resfail resfail;
2119 * };
2120 */
2121static int decode_fsstat3resok(struct xdr_stream *xdr,
2122 struct nfs_fsstat *result)
2123{
2124 __be32 *p;
2125
2126 p = xdr_inline_decode(xdr, 8 * 6 + 4);
2127 if (unlikely(p == NULL))
2128 goto out_overflow;
2129 p = xdr_decode_size3(p, &result->tbytes);
2130 p = xdr_decode_size3(p, &result->fbytes);
2131 p = xdr_decode_size3(p, &result->abytes);
2132 p = xdr_decode_size3(p, &result->tfiles);
2133 p = xdr_decode_size3(p, &result->ffiles);
2134 xdr_decode_size3(p, &result->afiles);
2135 /* ignore invarsec */
2136 return 0;
2137out_overflow:
2138 print_overflow_msg(__func__, xdr);
2139 return -EIO;
2140}
2141
Chuck Leverbf269552010-12-14 14:59:29 +00002142static int nfs3_xdr_dec_fsstat3res(struct rpc_rqst *req,
2143 struct xdr_stream *xdr,
Chuck Levere4f93232010-12-14 14:56:30 +00002144 struct nfs_fsstat *result)
2145{
Chuck Levere4f93232010-12-14 14:56:30 +00002146 enum nfs_stat status;
2147 int error;
2148
Chuck Leverbf269552010-12-14 14:59:29 +00002149 error = decode_nfsstat3(xdr, &status);
Chuck Levere4f93232010-12-14 14:56:30 +00002150 if (unlikely(error))
2151 goto out;
Chuck Leverbf269552010-12-14 14:59:29 +00002152 error = decode_post_op_attr(xdr, result->fattr);
Chuck Levere4f93232010-12-14 14:56:30 +00002153 if (unlikely(error))
2154 goto out;
2155 if (status != NFS3_OK)
2156 goto out_status;
Chuck Leverbf269552010-12-14 14:59:29 +00002157 error = decode_fsstat3resok(xdr, result);
Chuck Levere4f93232010-12-14 14:56:30 +00002158out:
2159 return error;
2160out_status:
Bryan Schumaker5e7e5a02012-05-10 16:47:18 -04002161 return nfs3_stat_to_errno(status);
Chuck Levere4f93232010-12-14 14:56:30 +00002162}
2163
2164/*
Chuck Levere4f93232010-12-14 14:56:30 +00002165 * 3.3.19 FSINFO3res
2166 *
2167 * struct FSINFO3resok {
2168 * post_op_attr obj_attributes;
2169 * uint32 rtmax;
2170 * uint32 rtpref;
2171 * uint32 rtmult;
2172 * uint32 wtmax;
2173 * uint32 wtpref;
2174 * uint32 wtmult;
2175 * uint32 dtpref;
2176 * size3 maxfilesize;
2177 * nfstime3 time_delta;
2178 * uint32 properties;
2179 * };
2180 *
2181 * struct FSINFO3resfail {
2182 * post_op_attr obj_attributes;
2183 * };
2184 *
2185 * union FSINFO3res switch (nfsstat3 status) {
2186 * case NFS3_OK:
2187 * FSINFO3resok resok;
2188 * default:
2189 * FSINFO3resfail resfail;
2190 * };
2191 */
2192static int decode_fsinfo3resok(struct xdr_stream *xdr,
2193 struct nfs_fsinfo *result)
2194{
2195 __be32 *p;
2196
2197 p = xdr_inline_decode(xdr, 4 * 7 + 8 + 8 + 4);
2198 if (unlikely(p == NULL))
2199 goto out_overflow;
2200 result->rtmax = be32_to_cpup(p++);
2201 result->rtpref = be32_to_cpup(p++);
2202 result->rtmult = be32_to_cpup(p++);
2203 result->wtmax = be32_to_cpup(p++);
2204 result->wtpref = be32_to_cpup(p++);
2205 result->wtmult = be32_to_cpup(p++);
2206 result->dtpref = be32_to_cpup(p++);
2207 p = xdr_decode_size3(p, &result->maxfilesize);
Chuck Leverf6048702010-12-14 14:57:02 +00002208 xdr_decode_nfstime3(p, &result->time_delta);
Chuck Levere4f93232010-12-14 14:56:30 +00002209
2210 /* ignore properties */
2211 result->lease_time = 0;
2212 return 0;
2213out_overflow:
2214 print_overflow_msg(__func__, xdr);
2215 return -EIO;
2216}
2217
Chuck Leverbf269552010-12-14 14:59:29 +00002218static int nfs3_xdr_dec_fsinfo3res(struct rpc_rqst *req,
2219 struct xdr_stream *xdr,
Chuck Levere4f93232010-12-14 14:56:30 +00002220 struct nfs_fsinfo *result)
2221{
Chuck Levere4f93232010-12-14 14:56:30 +00002222 enum nfs_stat status;
2223 int error;
2224
Chuck Leverbf269552010-12-14 14:59:29 +00002225 error = decode_nfsstat3(xdr, &status);
Chuck Levere4f93232010-12-14 14:56:30 +00002226 if (unlikely(error))
2227 goto out;
Chuck Leverbf269552010-12-14 14:59:29 +00002228 error = decode_post_op_attr(xdr, result->fattr);
Chuck Levere4f93232010-12-14 14:56:30 +00002229 if (unlikely(error))
2230 goto out;
2231 if (status != NFS3_OK)
2232 goto out_status;
Chuck Leverbf269552010-12-14 14:59:29 +00002233 error = decode_fsinfo3resok(xdr, result);
Chuck Levere4f93232010-12-14 14:56:30 +00002234out:
2235 return error;
2236out_status:
Bryan Schumaker5e7e5a02012-05-10 16:47:18 -04002237 return nfs3_stat_to_errno(status);
Chuck Levere4f93232010-12-14 14:56:30 +00002238}
2239
2240/*
Chuck Levere4f93232010-12-14 14:56:30 +00002241 * 3.3.20 PATHCONF3res
2242 *
2243 * struct PATHCONF3resok {
2244 * post_op_attr obj_attributes;
2245 * uint32 linkmax;
2246 * uint32 name_max;
2247 * bool no_trunc;
2248 * bool chown_restricted;
2249 * bool case_insensitive;
2250 * bool case_preserving;
2251 * };
2252 *
2253 * struct PATHCONF3resfail {
2254 * post_op_attr obj_attributes;
2255 * };
2256 *
2257 * union PATHCONF3res switch (nfsstat3 status) {
2258 * case NFS3_OK:
2259 * PATHCONF3resok resok;
2260 * default:
2261 * PATHCONF3resfail resfail;
2262 * };
2263 */
2264static int decode_pathconf3resok(struct xdr_stream *xdr,
2265 struct nfs_pathconf *result)
2266{
2267 __be32 *p;
2268
2269 p = xdr_inline_decode(xdr, 4 * 6);
2270 if (unlikely(p == NULL))
2271 goto out_overflow;
2272 result->max_link = be32_to_cpup(p++);
2273 result->max_namelen = be32_to_cpup(p);
2274 /* ignore remaining fields */
2275 return 0;
2276out_overflow:
2277 print_overflow_msg(__func__, xdr);
2278 return -EIO;
2279}
2280
Chuck Leverbf269552010-12-14 14:59:29 +00002281static int nfs3_xdr_dec_pathconf3res(struct rpc_rqst *req,
2282 struct xdr_stream *xdr,
Chuck Levere4f93232010-12-14 14:56:30 +00002283 struct nfs_pathconf *result)
2284{
Chuck Levere4f93232010-12-14 14:56:30 +00002285 enum nfs_stat status;
2286 int error;
2287
Chuck Leverbf269552010-12-14 14:59:29 +00002288 error = decode_nfsstat3(xdr, &status);
Chuck Levere4f93232010-12-14 14:56:30 +00002289 if (unlikely(error))
2290 goto out;
Chuck Leverbf269552010-12-14 14:59:29 +00002291 error = decode_post_op_attr(xdr, result->fattr);
Chuck Levere4f93232010-12-14 14:56:30 +00002292 if (unlikely(error))
2293 goto out;
2294 if (status != NFS3_OK)
2295 goto out_status;
Chuck Leverbf269552010-12-14 14:59:29 +00002296 error = decode_pathconf3resok(xdr, result);
Chuck Levere4f93232010-12-14 14:56:30 +00002297out:
2298 return error;
2299out_status:
Bryan Schumaker5e7e5a02012-05-10 16:47:18 -04002300 return nfs3_stat_to_errno(status);
Chuck Levere4f93232010-12-14 14:56:30 +00002301}
2302
2303/*
Chuck Levere4f93232010-12-14 14:56:30 +00002304 * 3.3.21 COMMIT3res
2305 *
2306 * struct COMMIT3resok {
2307 * wcc_data file_wcc;
2308 * writeverf3 verf;
2309 * };
2310 *
2311 * struct COMMIT3resfail {
2312 * wcc_data file_wcc;
2313 * };
2314 *
2315 * union COMMIT3res switch (nfsstat3 status) {
2316 * case NFS3_OK:
2317 * COMMIT3resok resok;
2318 * default:
2319 * COMMIT3resfail resfail;
2320 * };
2321 */
Chuck Leverbf269552010-12-14 14:59:29 +00002322static int nfs3_xdr_dec_commit3res(struct rpc_rqst *req,
2323 struct xdr_stream *xdr,
Fred Isaman0b7c0152012-04-20 14:47:39 -04002324 struct nfs_commitres *result)
Chuck Levere4f93232010-12-14 14:56:30 +00002325{
Chuck Levere4f93232010-12-14 14:56:30 +00002326 enum nfs_stat status;
2327 int error;
2328
Chuck Leverbf269552010-12-14 14:59:29 +00002329 error = decode_nfsstat3(xdr, &status);
Chuck Levere4f93232010-12-14 14:56:30 +00002330 if (unlikely(error))
2331 goto out;
Chuck Leverbf269552010-12-14 14:59:29 +00002332 error = decode_wcc_data(xdr, result->fattr);
Chuck Levere4f93232010-12-14 14:56:30 +00002333 if (unlikely(error))
2334 goto out;
Peng Taoaabff4d2014-08-27 10:47:14 +08002335 result->op_status = status;
Chuck Levere4f93232010-12-14 14:56:30 +00002336 if (status != NFS3_OK)
2337 goto out_status;
Trond Myklebust2f2c63b2012-06-08 11:56:09 -04002338 error = decode_writeverf3(xdr, &result->verf->verifier);
Chuck Levere4f93232010-12-14 14:56:30 +00002339out:
2340 return error;
2341out_status:
Bryan Schumaker5e7e5a02012-05-10 16:47:18 -04002342 return nfs3_stat_to_errno(status);
Chuck Levere4f93232010-12-14 14:56:30 +00002343}
2344
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +00002345#ifdef CONFIG_NFS_V3_ACL
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +00002346
Chuck Levere4f93232010-12-14 14:56:30 +00002347static inline int decode_getacl3resok(struct xdr_stream *xdr,
2348 struct nfs3_getaclres *result)
2349{
2350 struct posix_acl **acl;
2351 unsigned int *aclcnt;
2352 size_t hdrlen;
2353 int error;
2354
2355 error = decode_post_op_attr(xdr, result->fattr);
2356 if (unlikely(error))
2357 goto out;
2358 error = decode_uint32(xdr, &result->mask);
2359 if (unlikely(error))
2360 goto out;
2361 error = -EINVAL;
2362 if (result->mask & ~(NFS_ACL|NFS_ACLCNT|NFS_DFACL|NFS_DFACLCNT))
2363 goto out;
2364
Trond Myklebust1aecca32012-06-21 11:41:29 -04002365 hdrlen = xdr_stream_pos(xdr);
Chuck Levere4f93232010-12-14 14:56:30 +00002366
2367 acl = NULL;
2368 if (result->mask & NFS_ACL)
2369 acl = &result->acl_access;
2370 aclcnt = NULL;
2371 if (result->mask & NFS_ACLCNT)
2372 aclcnt = &result->acl_access_count;
2373 error = nfsacl_decode(xdr->buf, hdrlen, aclcnt, acl);
2374 if (unlikely(error <= 0))
2375 goto out;
2376
2377 acl = NULL;
2378 if (result->mask & NFS_DFACL)
2379 acl = &result->acl_default;
2380 aclcnt = NULL;
2381 if (result->mask & NFS_DFACLCNT)
2382 aclcnt = &result->acl_default_count;
2383 error = nfsacl_decode(xdr->buf, hdrlen + error, aclcnt, acl);
2384 if (unlikely(error <= 0))
2385 return error;
2386 error = 0;
2387out:
2388 return error;
2389}
2390
Chuck Leverbf269552010-12-14 14:59:29 +00002391static int nfs3_xdr_dec_getacl3res(struct rpc_rqst *req,
2392 struct xdr_stream *xdr,
Chuck Levere4f93232010-12-14 14:56:30 +00002393 struct nfs3_getaclres *result)
2394{
Chuck Levere4f93232010-12-14 14:56:30 +00002395 enum nfs_stat status;
2396 int error;
2397
Chuck Leverbf269552010-12-14 14:59:29 +00002398 error = decode_nfsstat3(xdr, &status);
Chuck Levere4f93232010-12-14 14:56:30 +00002399 if (unlikely(error))
2400 goto out;
2401 if (status != NFS3_OK)
2402 goto out_default;
Chuck Leverbf269552010-12-14 14:59:29 +00002403 error = decode_getacl3resok(xdr, result);
Chuck Levere4f93232010-12-14 14:56:30 +00002404out:
2405 return error;
2406out_default:
Bryan Schumaker5e7e5a02012-05-10 16:47:18 -04002407 return nfs3_stat_to_errno(status);
Chuck Levere4f93232010-12-14 14:56:30 +00002408}
2409
Chuck Leverbf269552010-12-14 14:59:29 +00002410static int nfs3_xdr_dec_setacl3res(struct rpc_rqst *req,
2411 struct xdr_stream *xdr,
Chuck Levere4f93232010-12-14 14:56:30 +00002412 struct nfs_fattr *result)
2413{
Chuck Levere4f93232010-12-14 14:56:30 +00002414 enum nfs_stat status;
2415 int error;
2416
Chuck Leverbf269552010-12-14 14:59:29 +00002417 error = decode_nfsstat3(xdr, &status);
Chuck Levere4f93232010-12-14 14:56:30 +00002418 if (unlikely(error))
2419 goto out;
2420 if (status != NFS3_OK)
2421 goto out_default;
Chuck Leverbf269552010-12-14 14:59:29 +00002422 error = decode_post_op_attr(xdr, result);
Chuck Levere4f93232010-12-14 14:56:30 +00002423out:
2424 return error;
2425out_default:
Bryan Schumaker5e7e5a02012-05-10 16:47:18 -04002426 return nfs3_stat_to_errno(status);
Chuck Levere4f93232010-12-14 14:56:30 +00002427}
2428
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +00002429#endif /* CONFIG_NFS_V3_ACL */
2430
Bryan Schumaker5e7e5a02012-05-10 16:47:18 -04002431
2432/*
2433 * We need to translate between nfs status return values and
2434 * the local errno values which may not be the same.
2435 */
2436static const struct {
2437 int stat;
2438 int errno;
2439} nfs_errtbl[] = {
2440 { NFS_OK, 0 },
2441 { NFSERR_PERM, -EPERM },
2442 { NFSERR_NOENT, -ENOENT },
2443 { NFSERR_IO, -errno_NFSERR_IO},
2444 { NFSERR_NXIO, -ENXIO },
2445/* { NFSERR_EAGAIN, -EAGAIN }, */
2446 { NFSERR_ACCES, -EACCES },
2447 { NFSERR_EXIST, -EEXIST },
2448 { NFSERR_XDEV, -EXDEV },
2449 { NFSERR_NODEV, -ENODEV },
2450 { NFSERR_NOTDIR, -ENOTDIR },
2451 { NFSERR_ISDIR, -EISDIR },
2452 { NFSERR_INVAL, -EINVAL },
2453 { NFSERR_FBIG, -EFBIG },
2454 { NFSERR_NOSPC, -ENOSPC },
2455 { NFSERR_ROFS, -EROFS },
2456 { NFSERR_MLINK, -EMLINK },
2457 { NFSERR_NAMETOOLONG, -ENAMETOOLONG },
2458 { NFSERR_NOTEMPTY, -ENOTEMPTY },
2459 { NFSERR_DQUOT, -EDQUOT },
2460 { NFSERR_STALE, -ESTALE },
2461 { NFSERR_REMOTE, -EREMOTE },
2462#ifdef EWFLUSH
2463 { NFSERR_WFLUSH, -EWFLUSH },
2464#endif
2465 { NFSERR_BADHANDLE, -EBADHANDLE },
2466 { NFSERR_NOT_SYNC, -ENOTSYNC },
2467 { NFSERR_BAD_COOKIE, -EBADCOOKIE },
2468 { NFSERR_NOTSUPP, -ENOTSUPP },
2469 { NFSERR_TOOSMALL, -ETOOSMALL },
2470 { NFSERR_SERVERFAULT, -EREMOTEIO },
2471 { NFSERR_BADTYPE, -EBADTYPE },
2472 { NFSERR_JUKEBOX, -EJUKEBOX },
2473 { -1, -EIO }
2474};
2475
2476/**
2477 * nfs3_stat_to_errno - convert an NFS status code to a local errno
2478 * @status: NFS status code to convert
2479 *
2480 * Returns a local errno value, or -EIO if the NFS status code is
2481 * not recognized. This function is used jointly by NFSv2 and NFSv3.
2482 */
2483static int nfs3_stat_to_errno(enum nfs_stat status)
2484{
2485 int i;
2486
2487 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
2488 if (nfs_errtbl[i].stat == (int)status)
2489 return nfs_errtbl[i].errno;
2490 }
2491 dprintk("NFS: Unrecognized nfs status value: %u\n", status);
2492 return nfs_errtbl[i].errno;
2493}
2494
2495
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496#define PROC(proc, argtype, restype, timer) \
2497[NFS3PROC_##proc] = { \
2498 .p_proc = NFS3PROC_##proc, \
Chuck Lever9f06c712010-12-14 14:59:18 +00002499 .p_encode = (kxdreproc_t)nfs3_xdr_enc_##argtype##3args, \
Chuck Leverbf269552010-12-14 14:59:29 +00002500 .p_decode = (kxdrdproc_t)nfs3_xdr_dec_##restype##3res, \
Chuck Leverad96b5b2010-12-14 14:56:01 +00002501 .p_arglen = NFS3_##argtype##args_sz, \
Chuck Leverf5fc3c502010-12-14 14:56:42 +00002502 .p_replen = NFS3_##restype##res_sz, \
Chuck Levercc0175c2006-03-20 13:44:22 -05002503 .p_timer = timer, \
2504 .p_statidx = NFS3PROC_##proc, \
2505 .p_name = #proc, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 }
2507
2508struct rpc_procinfo nfs3_procedures[] = {
Chuck Leverf5fc3c502010-12-14 14:56:42 +00002509 PROC(GETATTR, getattr, getattr, 1),
2510 PROC(SETATTR, setattr, setattr, 0),
2511 PROC(LOOKUP, lookup, lookup, 2),
2512 PROC(ACCESS, access, access, 1),
2513 PROC(READLINK, readlink, readlink, 3),
2514 PROC(READ, read, read, 3),
2515 PROC(WRITE, write, write, 4),
2516 PROC(CREATE, create, create, 0),
2517 PROC(MKDIR, mkdir, create, 0),
2518 PROC(SYMLINK, symlink, create, 0),
2519 PROC(MKNOD, mknod, create, 0),
2520 PROC(REMOVE, remove, remove, 0),
2521 PROC(RMDIR, lookup, setattr, 0),
2522 PROC(RENAME, rename, rename, 0),
2523 PROC(LINK, link, link, 0),
2524 PROC(READDIR, readdir, readdir, 3),
2525 PROC(READDIRPLUS, readdirplus, readdir, 3),
2526 PROC(FSSTAT, getattr, fsstat, 0),
2527 PROC(FSINFO, getattr, fsinfo, 0),
2528 PROC(PATHCONF, getattr, pathconf, 0),
2529 PROC(COMMIT, commit, commit, 5),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530};
2531
Trond Myklebusta613fa12012-01-20 13:53:56 -05002532const struct rpc_version nfs_version3 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 .number = 3,
Tobias Klausere8c96f82006-03-24 03:15:34 -08002534 .nrprocs = ARRAY_SIZE(nfs3_procedures),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 .procs = nfs3_procedures
2536};
2537
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +00002538#ifdef CONFIG_NFS_V3_ACL
2539static struct rpc_procinfo nfs3_acl_procedures[] = {
2540 [ACLPROC3_GETACL] = {
2541 .p_proc = ACLPROC3_GETACL,
Chuck Lever9f06c712010-12-14 14:59:18 +00002542 .p_encode = (kxdreproc_t)nfs3_xdr_enc_getacl3args,
Chuck Leverbf269552010-12-14 14:59:29 +00002543 .p_decode = (kxdrdproc_t)nfs3_xdr_dec_getacl3res,
Chuck Lever2bea90d2007-03-29 16:47:53 -04002544 .p_arglen = ACL3_getaclargs_sz,
2545 .p_replen = ACL3_getaclres_sz,
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +00002546 .p_timer = 1,
Chuck Levercc0175c2006-03-20 13:44:22 -05002547 .p_name = "GETACL",
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +00002548 },
2549 [ACLPROC3_SETACL] = {
2550 .p_proc = ACLPROC3_SETACL,
Chuck Lever9f06c712010-12-14 14:59:18 +00002551 .p_encode = (kxdreproc_t)nfs3_xdr_enc_setacl3args,
Chuck Leverbf269552010-12-14 14:59:29 +00002552 .p_decode = (kxdrdproc_t)nfs3_xdr_dec_setacl3res,
Chuck Lever2bea90d2007-03-29 16:47:53 -04002553 .p_arglen = ACL3_setaclargs_sz,
2554 .p_replen = ACL3_setaclres_sz,
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +00002555 .p_timer = 0,
Chuck Levercc0175c2006-03-20 13:44:22 -05002556 .p_name = "SETACL",
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +00002557 },
2558};
2559
Trond Myklebusta613fa12012-01-20 13:53:56 -05002560const struct rpc_version nfsacl_version3 = {
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +00002561 .number = 3,
2562 .nrprocs = sizeof(nfs3_acl_procedures)/
2563 sizeof(nfs3_acl_procedures[0]),
2564 .procs = nfs3_acl_procedures,
2565};
2566#endif /* CONFIG_NFS_V3_ACL */