afs: Provide a splice-read wrapper
[linux-block.git] / fs / nfsd / nfs3xdr.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4 2/*
1da177e4
LT
3 * XDR support for nfsd/protocol version 3.
4 *
5 * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
6 *
7 * 2003-08-09 Jamie Lokier: Use htonl() for nanoseconds, not htons()!
8 */
9
1da177e4 10#include <linux/namei.h>
b9c0ef85 11#include <linux/sunrpc/svc_xprt.h>
9a74af21 12#include "xdr3.h"
2e8138a2 13#include "auth.h"
b9c0ef85 14#include "netns.h"
3dadecce 15#include "vfs.h"
1da177e4 16
8b704498
CL
17/*
18 * Force construction of an empty post-op attr
19 */
20static const struct svc_fh nfs3svc_null_fh = {
21 .fh_no_wcc = true,
22};
23
0a139d1b
CL
24/*
25 * time_delta. {1, 0} means the server is accurate only
26 * to the nearest second.
27 */
28static const struct timespec64 nfs3svc_time_delta = {
29 .tv_sec = 1,
30 .tv_nsec = 0,
31};
32
1da177e4
LT
33/*
34 * Mapping of S_IF* types to NFS file types
35 */
2c42f804 36static const u32 nfs3_ftypes[] = {
1da177e4
LT
37 NF3NON, NF3FIFO, NF3CHR, NF3BAD,
38 NF3DIR, NF3BAD, NF3BLK, NF3BAD,
39 NF3REG, NF3BAD, NF3LNK, NF3BAD,
40 NF3SOCK, NF3BAD, NF3LNK, NF3BAD,
41};
42
27c438f5 43
1da177e4 44/*
9575363a 45 * Basic NFSv3 data types (RFC 1813 Sections 2.5 and 2.6)
1da177e4 46 */
9575363a 47
2c42f804
CL
48static __be32 *
49encode_nfstime3(__be32 *p, const struct timespec64 *time)
50{
51 *p++ = cpu_to_be32((u32)time->tv_sec);
52 *p++ = cpu_to_be32(time->tv_nsec);
53
54 return p;
55}
56
9cde9360
CL
57static bool
58svcxdr_decode_nfstime3(struct xdr_stream *xdr, struct timespec64 *timep)
1da177e4 59{
9cde9360
CL
60 __be32 *p;
61
62 p = xdr_inline_decode(xdr, XDR_UNIT * 2);
63 if (!p)
64 return false;
65 timep->tv_sec = be32_to_cpup(p++);
66 timep->tv_nsec = be32_to_cpup(p);
67
68 return true;
1da177e4
LT
69}
70
05027eaf
CL
71/**
72 * svcxdr_decode_nfs_fh3 - Decode an NFSv3 file handle
73 * @xdr: XDR stream positioned at an undecoded NFSv3 FH
74 * @fhp: OUT: filled-in server file handle
75 *
76 * Return values:
77 * %false: The encoded file handle was not valid
78 * %true: @fhp has been initialized
79 */
80bool
9575363a
CL
81svcxdr_decode_nfs_fh3(struct xdr_stream *xdr, struct svc_fh *fhp)
82{
83 __be32 *p;
84 u32 size;
85
86 if (xdr_stream_decode_u32(xdr, &size) < 0)
87 return false;
88 if (size == 0 || size > NFS3_FHSIZE)
89 return false;
90 p = xdr_inline_decode(xdr, size);
91 if (!p)
92 return false;
93 fh_init(fhp, NFS3_FHSIZE);
94 fhp->fh_handle.fh_size = size;
d8b26071 95 memcpy(&fhp->fh_handle.fh_raw, p, size);
9575363a
CL
96
97 return true;
98}
99
20798dfe
CL
100/**
101 * svcxdr_encode_nfsstat3 - Encode an NFSv3 status code
102 * @xdr: XDR stream
103 * @status: status value to encode
104 *
105 * Return values:
106 * %false: Send buffer space was exhausted
107 * %true: Success
108 */
109bool
2c42f804
CL
110svcxdr_encode_nfsstat3(struct xdr_stream *xdr, __be32 status)
111{
112 __be32 *p;
113
114 p = xdr_reserve_space(xdr, sizeof(status));
115 if (!p)
116 return false;
117 *p = status;
118
119 return true;
120}
121
5cf35335
CL
122static bool
123svcxdr_encode_nfs_fh3(struct xdr_stream *xdr, const struct svc_fh *fhp)
124{
125 u32 size = fhp->fh_handle.fh_size;
126 __be32 *p;
127
128 p = xdr_reserve_space(xdr, XDR_UNIT + size);
129 if (!p)
130 return false;
131 *p++ = cpu_to_be32(size);
132 if (size)
133 p[XDR_QUADLEN(size) - 1] = 0;
d8b26071 134 memcpy(p, &fhp->fh_handle.fh_raw, size);
5cf35335
CL
135
136 return true;
137}
138
78315b36
CL
139static bool
140svcxdr_encode_post_op_fh3(struct xdr_stream *xdr, const struct svc_fh *fhp)
141{
142 if (xdr_stream_encode_item_present(xdr) < 0)
143 return false;
144 if (!svcxdr_encode_nfs_fh3(xdr, fhp))
145 return false;
146
147 return true;
148}
149
e4ccfe30
CL
150static bool
151svcxdr_encode_cookieverf3(struct xdr_stream *xdr, const __be32 *verf)
152{
153 __be32 *p;
154
155 p = xdr_reserve_space(xdr, NFS3_COOKIEVERFSIZE);
156 if (!p)
157 return false;
158 memcpy(p, verf, NFS3_COOKIEVERFSIZE);
159
160 return true;
161}
162
ecb7a085
CL
163static bool
164svcxdr_encode_writeverf3(struct xdr_stream *xdr, const __be32 *verf)
165{
166 __be32 *p;
167
168 p = xdr_reserve_space(xdr, NFS3_WRITEVERFSIZE);
169 if (!p)
170 return false;
171 memcpy(p, verf, NFS3_WRITEVERFSIZE);
172
173 return true;
174}
175
54d1d43d
CL
176static bool
177svcxdr_decode_filename3(struct xdr_stream *xdr, char **name, unsigned int *len)
178{
179 u32 size, i;
180 __be32 *p;
181 char *c;
182
183 if (xdr_stream_decode_u32(xdr, &size) < 0)
184 return false;
185 if (size == 0 || size > NFS3_MAXNAMLEN)
186 return false;
187 p = xdr_inline_decode(xdr, size);
188 if (!p)
189 return false;
190
191 *len = size;
192 *name = (char *)p;
193 for (i = 0, c = *name; i < size; i++, c++) {
194 if (*c == '\0' || *c == '/')
195 return false;
196 }
197
198 return true;
199}
200
201static bool
202svcxdr_decode_diropargs3(struct xdr_stream *xdr, struct svc_fh *fhp,
203 char **name, unsigned int *len)
204{
205 return svcxdr_decode_nfs_fh3(xdr, fhp) &&
206 svcxdr_decode_filename3(xdr, name, len);
207}
208
9cde9360
CL
209static bool
210svcxdr_decode_sattr3(struct svc_rqst *rqstp, struct xdr_stream *xdr,
211 struct iattr *iap)
212{
213 u32 set_it;
214
215 iap->ia_valid = 0;
216
217 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
218 return false;
219 if (set_it) {
220 u32 mode;
221
222 if (xdr_stream_decode_u32(xdr, &mode) < 0)
223 return false;
224 iap->ia_valid |= ATTR_MODE;
225 iap->ia_mode = mode;
226 }
227 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
228 return false;
229 if (set_it) {
230 u32 uid;
231
232 if (xdr_stream_decode_u32(xdr, &uid) < 0)
233 return false;
234 iap->ia_uid = make_kuid(nfsd_user_namespace(rqstp), uid);
235 if (uid_valid(iap->ia_uid))
236 iap->ia_valid |= ATTR_UID;
237 }
238 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
239 return false;
240 if (set_it) {
241 u32 gid;
242
243 if (xdr_stream_decode_u32(xdr, &gid) < 0)
244 return false;
245 iap->ia_gid = make_kgid(nfsd_user_namespace(rqstp), gid);
246 if (gid_valid(iap->ia_gid))
247 iap->ia_valid |= ATTR_GID;
248 }
249 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
250 return false;
251 if (set_it) {
252 u64 newsize;
253
254 if (xdr_stream_decode_u64(xdr, &newsize) < 0)
255 return false;
256 iap->ia_valid |= ATTR_SIZE;
a648fdeb 257 iap->ia_size = newsize;
9cde9360
CL
258 }
259 if (xdr_stream_decode_u32(xdr, &set_it) < 0)
260 return false;
261 switch (set_it) {
262 case DONT_CHANGE:
263 break;
264 case SET_TO_SERVER_TIME:
265 iap->ia_valid |= ATTR_ATIME;
266 break;
267 case SET_TO_CLIENT_TIME:
268 if (!svcxdr_decode_nfstime3(xdr, &iap->ia_atime))
269 return false;
270 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
271 break;
272 default:
273 return false;
274 }
275 if (xdr_stream_decode_u32(xdr, &set_it) < 0)
276 return false;
277 switch (set_it) {
278 case DONT_CHANGE:
279 break;
280 case SET_TO_SERVER_TIME:
281 iap->ia_valid |= ATTR_MTIME;
282 break;
283 case SET_TO_CLIENT_TIME:
284 if (!svcxdr_decode_nfstime3(xdr, &iap->ia_mtime))
285 return false;
286 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
287 break;
288 default:
289 return false;
290 }
291
292 return true;
293}
294
295static bool
296svcxdr_decode_sattrguard3(struct xdr_stream *xdr, struct nfsd3_sattrargs *args)
297{
298 __be32 *p;
299 u32 check;
300
301 if (xdr_stream_decode_bool(xdr, &check) < 0)
302 return false;
303 if (check) {
304 p = xdr_inline_decode(xdr, XDR_UNIT * 2);
305 if (!p)
306 return false;
307 args->check_guard = 1;
308 args->guardtime = be32_to_cpup(p);
309 } else
310 args->check_guard = 0;
311
312 return true;
313}
314
f8a38e2d
CL
315static bool
316svcxdr_decode_specdata3(struct xdr_stream *xdr, struct nfsd3_mknodargs *args)
1da177e4 317{
f8a38e2d 318 __be32 *p;
1da177e4 319
f8a38e2d
CL
320 p = xdr_inline_decode(xdr, XDR_UNIT * 2);
321 if (!p)
322 return false;
323 args->major = be32_to_cpup(p++);
324 args->minor = be32_to_cpup(p);
1da177e4 325
f8a38e2d
CL
326 return true;
327}
1da177e4 328
f8a38e2d
CL
329static bool
330svcxdr_decode_devicedata3(struct svc_rqst *rqstp, struct xdr_stream *xdr,
331 struct nfsd3_mknodargs *args)
332{
333 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs) &&
334 svcxdr_decode_specdata3(xdr, args);
1da177e4
LT
335}
336
2c42f804
CL
337static bool
338svcxdr_encode_fattr3(struct svc_rqst *rqstp, struct xdr_stream *xdr,
339 const struct svc_fh *fhp, const struct kstat *stat)
340{
341 struct user_namespace *userns = nfsd_user_namespace(rqstp);
342 __be32 *p;
343 u64 fsid;
344
345 p = xdr_reserve_space(xdr, XDR_UNIT * 21);
346 if (!p)
347 return false;
348
349 *p++ = cpu_to_be32(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]);
350 *p++ = cpu_to_be32((u32)(stat->mode & S_IALLUGO));
351 *p++ = cpu_to_be32((u32)stat->nlink);
352 *p++ = cpu_to_be32((u32)from_kuid_munged(userns, stat->uid));
353 *p++ = cpu_to_be32((u32)from_kgid_munged(userns, stat->gid));
354 if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN)
355 p = xdr_encode_hyper(p, (u64)NFS3_MAXPATHLEN);
356 else
357 p = xdr_encode_hyper(p, (u64)stat->size);
358
359 /* used */
360 p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
361
362 /* rdev */
363 *p++ = cpu_to_be32((u32)MAJOR(stat->rdev));
364 *p++ = cpu_to_be32((u32)MINOR(stat->rdev));
365
366 switch(fsid_source(fhp)) {
367 case FSIDSOURCE_FSID:
368 fsid = (u64)fhp->fh_export->ex_fsid;
369 break;
370 case FSIDSOURCE_UUID:
371 fsid = ((u64 *)fhp->fh_export->ex_uuid)[0];
372 fsid ^= ((u64 *)fhp->fh_export->ex_uuid)[1];
373 break;
374 default:
375 fsid = (u64)huge_encode_dev(fhp->fh_dentry->d_sb->s_dev);
376 }
377 p = xdr_encode_hyper(p, fsid);
378
379 /* fileid */
380 p = xdr_encode_hyper(p, stat->ino);
381
382 p = encode_nfstime3(p, &stat->atime);
383 p = encode_nfstime3(p, &stat->mtime);
384 encode_nfstime3(p, &stat->ctime);
385
386 return true;
387}
388
70f8e839
CL
389static bool
390svcxdr_encode_wcc_attr(struct xdr_stream *xdr, const struct svc_fh *fhp)
391{
392 __be32 *p;
393
394 p = xdr_reserve_space(xdr, XDR_UNIT * 6);
395 if (!p)
396 return false;
397 p = xdr_encode_hyper(p, (u64)fhp->fh_pre_size);
398 p = encode_nfstime3(p, &fhp->fh_pre_mtime);
399 encode_nfstime3(p, &fhp->fh_pre_ctime);
400
401 return true;
402}
403
404static bool
405svcxdr_encode_pre_op_attr(struct xdr_stream *xdr, const struct svc_fh *fhp)
406{
407 if (!fhp->fh_pre_saved) {
408 if (xdr_stream_encode_item_absent(xdr) < 0)
409 return false;
410 return true;
411 }
412
413 if (xdr_stream_encode_item_present(xdr) < 0)
414 return false;
415 return svcxdr_encode_wcc_attr(xdr, fhp);
416}
417
20798dfe
CL
418/**
419 * svcxdr_encode_post_op_attr - Encode NFSv3 post-op attributes
420 * @rqstp: Context of a completed RPC transaction
421 * @xdr: XDR stream
422 * @fhp: File handle to encode
423 *
424 * Return values:
425 * %false: Send buffer space was exhausted
426 * %true: Success
427 */
428bool
907c3822
CL
429svcxdr_encode_post_op_attr(struct svc_rqst *rqstp, struct xdr_stream *xdr,
430 const struct svc_fh *fhp)
431{
432 struct dentry *dentry = fhp->fh_dentry;
433 struct kstat stat;
434
435 /*
436 * The inode may be NULL if the call failed because of a
437 * stale file handle. In this case, no attributes are
438 * returned.
439 */
440 if (fhp->fh_no_wcc || !dentry || !d_really_is_positive(dentry))
441 goto no_post_op_attrs;
442 if (fh_getattr(fhp, &stat) != nfs_ok)
443 goto no_post_op_attrs;
444
445 if (xdr_stream_encode_item_present(xdr) < 0)
446 return false;
447 lease_get_mtime(d_inode(dentry), &stat.mtime);
448 if (!svcxdr_encode_fattr3(rqstp, xdr, fhp, &stat))
449 return false;
450
451 return true;
452
453no_post_op_attrs:
454 return xdr_stream_encode_item_absent(xdr) > 0;
455}
456
70f8e839
CL
457/*
458 * Encode weak cache consistency data
459 */
460static bool
461svcxdr_encode_wcc_data(struct svc_rqst *rqstp, struct xdr_stream *xdr,
462 const struct svc_fh *fhp)
463{
464 struct dentry *dentry = fhp->fh_dentry;
465
466 if (!dentry || !d_really_is_positive(dentry) || !fhp->fh_post_saved)
467 goto neither;
468
469 /* before */
470 if (!svcxdr_encode_pre_op_attr(xdr, fhp))
471 return false;
472
473 /* after */
474 if (xdr_stream_encode_item_present(xdr) < 0)
475 return false;
476 if (!svcxdr_encode_fattr3(rqstp, xdr, fhp, &fhp->fh_post_attr))
477 return false;
478
479 return true;
480
481neither:
482 if (xdr_stream_encode_item_absent(xdr) < 0)
483 return false;
484 if (!svcxdr_encode_post_op_attr(rqstp, xdr, fhp))
485 return false;
486
487 return true;
488}
489
1da177e4
LT
490/*
491 * XDR decode functions
492 */
dcc46991 493
c44b31c2 494bool
16c66364 495nfs3svc_decode_fhandleargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 496{
026fec7e
CH
497 struct nfsd_fhandle *args = rqstp->rq_argp;
498
9575363a 499 return svcxdr_decode_nfs_fh3(xdr, &args->fh);
1da177e4
LT
500}
501
c44b31c2 502bool
16c66364 503nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 504{
026fec7e
CH
505 struct nfsd3_sattrargs *args = rqstp->rq_argp;
506
9cde9360
CL
507 return svcxdr_decode_nfs_fh3(xdr, &args->fh) &&
508 svcxdr_decode_sattr3(rqstp, xdr, &args->attrs) &&
509 svcxdr_decode_sattrguard3(xdr, args);
1da177e4
LT
510}
511
c44b31c2 512bool
16c66364 513nfs3svc_decode_diropargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 514{
026fec7e
CH
515 struct nfsd3_diropargs *args = rqstp->rq_argp;
516
54d1d43d 517 return svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len);
1da177e4
LT
518}
519
c44b31c2 520bool
16c66364 521nfs3svc_decode_accessargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 522{
026fec7e
CH
523 struct nfsd3_accessargs *args = rqstp->rq_argp;
524
3b921a2b 525 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
c44b31c2 526 return false;
3b921a2b 527 if (xdr_stream_decode_u32(xdr, &args->access) < 0)
c44b31c2 528 return false;
1da177e4 529
c44b31c2 530 return true;
1da177e4
LT
531}
532
c44b31c2 533bool
16c66364 534nfs3svc_decode_readargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 535{
026fec7e 536 struct nfsd3_readargs *args = rqstp->rq_argp;
1da177e4 537
be63bd2a 538 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
c44b31c2 539 return false;
be63bd2a 540 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
c44b31c2 541 return false;
be63bd2a 542 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
c44b31c2 543 return false;
afc59400 544
c44b31c2 545 return true;
1da177e4
LT
546}
547
c44b31c2 548bool
16c66364 549nfs3svc_decode_writeargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 550{
026fec7e 551 struct nfsd3_writeargs *args = rqstp->rq_argp;
7adae489 552 u32 max_blocksize = svc_max_payload(rqstp);
1da177e4 553
c43b2f22 554 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
c44b31c2 555 return false;
c43b2f22 556 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
c44b31c2 557 return false;
c43b2f22 558 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
c44b31c2 559 return false;
c43b2f22 560 if (xdr_stream_decode_u32(xdr, &args->stable) < 0)
c44b31c2 561 return false;
1da177e4 562
c43b2f22
CL
563 /* opaque data */
564 if (xdr_stream_decode_u32(xdr, &args->len) < 0)
c44b31c2 565 return false;
1da177e4 566
c43b2f22
CL
567 /* request sanity */
568 if (args->count != args->len)
c44b31c2 569 return false;
f34b9568
PS
570 if (args->count > max_blocksize) {
571 args->count = max_blocksize;
c43b2f22 572 args->len = max_blocksize;
f34b9568 573 }
c43b2f22 574
d4da5baa 575 return xdr_stream_subsegment(xdr, &args->payload, args->count);
1da177e4
LT
576}
577
c44b31c2 578bool
16c66364 579nfs3svc_decode_createargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 580{
026fec7e
CH
581 struct nfsd3_createargs *args = rqstp->rq_argp;
582
6b3a1196 583 if (!svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len))
c44b31c2 584 return false;
6b3a1196 585 if (xdr_stream_decode_u32(xdr, &args->createmode) < 0)
c44b31c2 586 return false;
6b3a1196 587 switch (args->createmode) {
1da177e4
LT
588 case NFS3_CREATE_UNCHECKED:
589 case NFS3_CREATE_GUARDED:
6b3a1196 590 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
1da177e4 591 case NFS3_CREATE_EXCLUSIVE:
6b3a1196
CL
592 args->verf = xdr_inline_decode(xdr, NFS3_CREATEVERFSIZE);
593 if (!args->verf)
c44b31c2 594 return false;
1da177e4
LT
595 break;
596 default:
c44b31c2 597 return false;
1da177e4 598 }
c44b31c2 599 return true;
1da177e4 600}
026fec7e 601
c44b31c2 602bool
16c66364 603nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 604{
026fec7e
CH
605 struct nfsd3_createargs *args = rqstp->rq_argp;
606
83374c27
CL
607 return svcxdr_decode_diropargs3(xdr, &args->fh,
608 &args->name, &args->len) &&
609 svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
1da177e4
LT
610}
611
c44b31c2 612bool
16c66364 613nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 614{
026fec7e 615 struct nfsd3_symlinkargs *args = rqstp->rq_argp;
da392016 616 struct kvec *head = rqstp->rq_arg.head;
1da177e4 617
da392016 618 if (!svcxdr_decode_diropargs3(xdr, &args->ffh, &args->fname, &args->flen))
c44b31c2 619 return false;
da392016 620 if (!svcxdr_decode_sattr3(rqstp, xdr, &args->attrs))
c44b31c2 621 return false;
da392016 622 if (xdr_stream_decode_u32(xdr, &args->tlen) < 0)
c44b31c2 623 return false;
072f62ed 624
c3d2a04f 625 /* symlink_data */
da392016 626 args->first.iov_len = head->iov_len - xdr_stream_pos(xdr);
c3d2a04f
CL
627 args->first.iov_base = xdr_inline_decode(xdr, args->tlen);
628 return args->first.iov_base != NULL;
1da177e4
LT
629}
630
c44b31c2 631bool
16c66364 632nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 633{
026fec7e
CH
634 struct nfsd3_mknodargs *args = rqstp->rq_argp;
635
f8a38e2d 636 if (!svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len))
c44b31c2 637 return false;
f8a38e2d 638 if (xdr_stream_decode_u32(xdr, &args->ftype) < 0)
c44b31c2 639 return false;
f8a38e2d
CL
640 switch (args->ftype) {
641 case NF3CHR:
642 case NF3BLK:
643 return svcxdr_decode_devicedata3(rqstp, xdr, args);
644 case NF3SOCK:
645 case NF3FIFO:
646 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
647 case NF3REG:
648 case NF3DIR:
649 case NF3LNK:
650 /* Valid XDR but illegal file types */
651 break;
652 default:
c44b31c2 653 return false;
1da177e4
LT
654 }
655
c44b31c2 656 return true;
1da177e4
LT
657}
658
c44b31c2 659bool
16c66364 660nfs3svc_decode_renameargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 661{
026fec7e
CH
662 struct nfsd3_renameargs *args = rqstp->rq_argp;
663
d181e0a4
CL
664 return svcxdr_decode_diropargs3(xdr, &args->ffh,
665 &args->fname, &args->flen) &&
666 svcxdr_decode_diropargs3(xdr, &args->tfh,
667 &args->tname, &args->tlen);
1da177e4
LT
668}
669
c44b31c2 670bool
16c66364 671nfs3svc_decode_linkargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 672{
026fec7e
CH
673 struct nfsd3_linkargs *args = rqstp->rq_argp;
674
efaa1e7c
CL
675 return svcxdr_decode_nfs_fh3(xdr, &args->ffh) &&
676 svcxdr_decode_diropargs3(xdr, &args->tfh,
677 &args->tname, &args->tlen);
1da177e4
LT
678}
679
c44b31c2 680bool
16c66364 681nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 682{
026fec7e 683 struct nfsd3_readdirargs *args = rqstp->rq_argp;
f875a792 684
9cedc2e6 685 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
c44b31c2 686 return false;
9cedc2e6 687 if (xdr_stream_decode_u64(xdr, &args->cookie) < 0)
c44b31c2 688 return false;
9cedc2e6
CL
689 args->verf = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE);
690 if (!args->verf)
c44b31c2 691 return false;
9cedc2e6 692 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
c44b31c2 693 return false;
1da177e4 694
c44b31c2 695 return true;
1da177e4
LT
696}
697
c44b31c2 698bool
16c66364 699nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 700{
026fec7e 701 struct nfsd3_readdirargs *args = rqstp->rq_argp;
9cedc2e6 702 u32 dircount;
1da177e4 703
9cedc2e6 704 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
c44b31c2 705 return false;
9cedc2e6 706 if (xdr_stream_decode_u64(xdr, &args->cookie) < 0)
c44b31c2 707 return false;
9cedc2e6
CL
708 args->verf = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE);
709 if (!args->verf)
c44b31c2 710 return false;
9cedc2e6
CL
711 /* dircount is ignored */
712 if (xdr_stream_decode_u32(xdr, &dircount) < 0)
c44b31c2 713 return false;
9cedc2e6 714 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
c44b31c2 715 return false;
1da177e4 716
c44b31c2 717 return true;
1da177e4
LT
718}
719
c44b31c2 720bool
16c66364 721nfs3svc_decode_commitargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 722{
026fec7e 723 struct nfsd3_commitargs *args = rqstp->rq_argp;
c8d26a0a
CL
724
725 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
c44b31c2 726 return false;
c8d26a0a 727 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
c44b31c2 728 return false;
c8d26a0a 729 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
c44b31c2 730 return false;
1da177e4 731
c44b31c2 732 return true;
1da177e4
LT
733}
734
735/*
736 * XDR encode functions
737 */
cc028a10 738
1da177e4 739/* GETATTR */
130e2054 740bool
fda49441 741nfs3svc_encode_getattrres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 742{
63f8de37
CH
743 struct nfsd3_attrstat *resp = rqstp->rq_resp;
744
2c42f804 745 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
130e2054 746 return false;
2c42f804
CL
747 switch (resp->status) {
748 case nfs_ok:
749 lease_get_mtime(d_inode(resp->fh.fh_dentry), &resp->stat.mtime);
750 if (!svcxdr_encode_fattr3(rqstp, xdr, &resp->fh, &resp->stat))
130e2054 751 return false;
2c42f804 752 break;
40ee5dc6 753 }
2c42f804 754
130e2054 755 return true;
1da177e4
LT
756}
757
758/* SETATTR, REMOVE, RMDIR */
130e2054 759bool
fda49441 760nfs3svc_encode_wccstat(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 761{
63f8de37
CH
762 struct nfsd3_attrstat *resp = rqstp->rq_resp;
763
70f8e839
CL
764 return svcxdr_encode_nfsstat3(xdr, resp->status) &&
765 svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh);
1da177e4
LT
766}
767
768/* LOOKUP */
130e2054 769bool
fda49441 770nfs3svc_encode_lookupres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 771{
63f8de37
CH
772 struct nfsd3_diropres *resp = rqstp->rq_resp;
773
5cf35335 774 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
130e2054 775 return false;
5cf35335
CL
776 switch (resp->status) {
777 case nfs_ok:
778 if (!svcxdr_encode_nfs_fh3(xdr, &resp->fh))
130e2054 779 return false;
5cf35335 780 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
130e2054 781 return false;
5cf35335 782 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->dirfh))
130e2054 783 return false;
5cf35335
CL
784 break;
785 default:
786 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->dirfh))
130e2054 787 return false;
1da177e4 788 }
5cf35335 789
130e2054 790 return true;
1da177e4
LT
791}
792
793/* ACCESS */
130e2054 794bool
fda49441 795nfs3svc_encode_accessres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 796{
63f8de37
CH
797 struct nfsd3_accessres *resp = rqstp->rq_resp;
798
907c3822 799 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
130e2054 800 return false;
907c3822
CL
801 switch (resp->status) {
802 case nfs_ok:
803 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
130e2054 804 return false;
907c3822 805 if (xdr_stream_encode_u32(xdr, resp->access) < 0)
130e2054 806 return false;
907c3822
CL
807 break;
808 default:
809 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
130e2054 810 return false;
907c3822
CL
811 }
812
130e2054 813 return true;
1da177e4
LT
814}
815
816/* READLINK */
130e2054 817bool
fda49441 818nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 819{
63f8de37 820 struct nfsd3_readlinkres *resp = rqstp->rq_resp;
76e5492b 821 struct kvec *head = rqstp->rq_res.head;
63f8de37 822
9a9c8923 823 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
130e2054 824 return false;
9a9c8923
CL
825 switch (resp->status) {
826 case nfs_ok:
827 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
130e2054 828 return false;
9a9c8923 829 if (xdr_stream_encode_u32(xdr, resp->len) < 0)
130e2054 830 return false;
9a9c8923
CL
831 xdr_write_pages(xdr, resp->pages, 0, resp->len);
832 if (svc_encode_result_payload(rqstp, head->iov_len, resp->len) < 0)
130e2054 833 return false;
9a9c8923
CL
834 break;
835 default:
836 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
130e2054 837 return false;
9a9c8923
CL
838 }
839
130e2054 840 return true;
1da177e4
LT
841}
842
843/* READ */
130e2054 844bool
fda49441 845nfs3svc_encode_readres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 846{
63f8de37 847 struct nfsd3_readres *resp = rqstp->rq_resp;
76e5492b 848 struct kvec *head = rqstp->rq_res.head;
63f8de37 849
cc9bcdad 850 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
130e2054 851 return false;
cc9bcdad
CL
852 switch (resp->status) {
853 case nfs_ok:
854 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
130e2054 855 return false;
cc9bcdad 856 if (xdr_stream_encode_u32(xdr, resp->count) < 0)
130e2054 857 return false;
cc9bcdad 858 if (xdr_stream_encode_bool(xdr, resp->eof) < 0)
130e2054 859 return false;
cc9bcdad 860 if (xdr_stream_encode_u32(xdr, resp->count) < 0)
130e2054 861 return false;
cc9bcdad
CL
862 xdr_write_pages(xdr, resp->pages, rqstp->rq_res.page_base,
863 resp->count);
864 if (svc_encode_result_payload(rqstp, head->iov_len, resp->count) < 0)
130e2054 865 return false;
cc9bcdad
CL
866 break;
867 default:
868 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
130e2054 869 return false;
cc9bcdad
CL
870 }
871
130e2054 872 return true;
1da177e4
LT
873}
874
875/* WRITE */
130e2054 876bool
fda49441 877nfs3svc_encode_writeres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 878{
63f8de37 879 struct nfsd3_writeres *resp = rqstp->rq_resp;
b9c0ef85 880
ecb7a085 881 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
130e2054 882 return false;
ecb7a085
CL
883 switch (resp->status) {
884 case nfs_ok:
885 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
130e2054 886 return false;
ecb7a085 887 if (xdr_stream_encode_u32(xdr, resp->count) < 0)
130e2054 888 return false;
ecb7a085 889 if (xdr_stream_encode_u32(xdr, resp->committed) < 0)
130e2054 890 return false;
ecb7a085 891 if (!svcxdr_encode_writeverf3(xdr, resp->verf))
130e2054 892 return false;
ecb7a085
CL
893 break;
894 default:
895 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
130e2054 896 return false;
1da177e4 897 }
ecb7a085 898
130e2054 899 return true;
1da177e4
LT
900}
901
902/* CREATE, MKDIR, SYMLINK, MKNOD */
130e2054 903bool
fda49441 904nfs3svc_encode_createres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 905{
63f8de37
CH
906 struct nfsd3_diropres *resp = rqstp->rq_resp;
907
78315b36 908 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
130e2054 909 return false;
78315b36
CL
910 switch (resp->status) {
911 case nfs_ok:
912 if (!svcxdr_encode_post_op_fh3(xdr, &resp->fh))
130e2054 913 return false;
78315b36 914 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
130e2054 915 return false;
78315b36 916 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->dirfh))
130e2054 917 return false;
78315b36
CL
918 break;
919 default:
920 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->dirfh))
130e2054 921 return false;
1da177e4 922 }
78315b36 923
130e2054 924 return true;
1da177e4
LT
925}
926
927/* RENAME */
130e2054 928bool
fda49441 929nfs3svc_encode_renameres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 930{
63f8de37
CH
931 struct nfsd3_renameres *resp = rqstp->rq_resp;
932
89d79e96
CL
933 return svcxdr_encode_nfsstat3(xdr, resp->status) &&
934 svcxdr_encode_wcc_data(rqstp, xdr, &resp->ffh) &&
935 svcxdr_encode_wcc_data(rqstp, xdr, &resp->tfh);
1da177e4
LT
936}
937
938/* LINK */
130e2054 939bool
fda49441 940nfs3svc_encode_linkres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 941{
63f8de37
CH
942 struct nfsd3_linkres *resp = rqstp->rq_resp;
943
4d74380a
CL
944 return svcxdr_encode_nfsstat3(xdr, resp->status) &&
945 svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh) &&
946 svcxdr_encode_wcc_data(rqstp, xdr, &resp->tfh);
1da177e4
LT
947}
948
949/* READDIR */
130e2054 950bool
fda49441 951nfs3svc_encode_readdirres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 952{
63f8de37 953 struct nfsd3_readdirres *resp = rqstp->rq_resp;
7f87fc2d 954 struct xdr_buf *dirlist = &resp->dirlist;
63f8de37 955
e4ccfe30 956 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
130e2054 957 return false;
e4ccfe30
CL
958 switch (resp->status) {
959 case nfs_ok:
960 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
130e2054 961 return false;
e4ccfe30 962 if (!svcxdr_encode_cookieverf3(xdr, resp->verf))
130e2054 963 return false;
7f87fc2d 964 xdr_write_pages(xdr, dirlist->pages, 0, dirlist->len);
e4ccfe30
CL
965 /* no more entries */
966 if (xdr_stream_encode_item_absent(xdr) < 0)
130e2054 967 return false;
e4ccfe30 968 if (xdr_stream_encode_bool(xdr, resp->common.err == nfserr_eof) < 0)
130e2054 969 return false;
e4ccfe30
CL
970 break;
971 default:
972 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
130e2054 973 return false;
e4ccfe30
CL
974 }
975
130e2054 976 return true;
1da177e4
LT
977}
978
efe39651 979static __be32
1da177e4 980compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
43b0e7ea 981 const char *name, int namlen, u64 ino)
1da177e4
LT
982{
983 struct svc_export *exp;
984 struct dentry *dparent, *dchild;
efe39651 985 __be32 rv = nfserr_noent;
1da177e4
LT
986
987 dparent = cd->fh.fh_dentry;
988 exp = cd->fh.fh_export;
989
1da177e4
LT
990 if (isdotent(name, namlen)) {
991 if (namlen == 2) {
992 dchild = dget_parent(dparent);
51b2ee7d
BF
993 /*
994 * Don't return filehandle for ".." if we're at
995 * the filesystem or export root:
996 */
efe39651
AV
997 if (dchild == dparent)
998 goto out;
51b2ee7d
BF
999 if (dparent == exp->ex_path.dentry)
1000 goto out;
1da177e4
LT
1001 } else
1002 dchild = dget(dparent);
1003 } else
6c2d4798 1004 dchild = lookup_positive_unlocked(name, dparent, namlen);
1da177e4 1005 if (IS_ERR(dchild))
efe39651 1006 return rv;
8177e6d6
BF
1007 if (d_mountpoint(dchild))
1008 goto out;
43b0e7ea
N
1009 if (dchild->d_inode->i_ino != ino)
1010 goto out;
efe39651 1011 rv = fh_compose(fhp, exp, dchild, &cd->fh);
8177e6d6 1012out:
1da177e4
LT
1013 dput(dchild);
1014 return rv;
1015}
1016
a161e6c7
CL
1017/**
1018 * nfs3svc_encode_cookie3 - Encode a directory offset cookie
1019 * @resp: readdir result context
1020 * @offset: offset cookie to encode
1021 *
7f87fc2d
CL
1022 * The buffer space for the offset cookie has already been reserved
1023 * by svcxdr_encode_entry3_common().
a161e6c7
CL
1024 */
1025void nfs3svc_encode_cookie3(struct nfsd3_readdirres *resp, u64 offset)
1026{
7f87fc2d 1027 __be64 cookie = cpu_to_be64(offset);
a161e6c7 1028
7f87fc2d
CL
1029 if (!resp->cookie_offset)
1030 return;
1031 write_bytes_to_xdr_buf(&resp->dirlist, resp->cookie_offset, &cookie,
1032 sizeof(cookie));
1033 resp->cookie_offset = 0;
a161e6c7
CL
1034}
1035
7f87fc2d
CL
1036static bool
1037svcxdr_encode_entry3_common(struct nfsd3_readdirres *resp, const char *name,
1038 int namlen, loff_t offset, u64 ino)
1039{
1040 struct xdr_buf *dirlist = &resp->dirlist;
1041 struct xdr_stream *xdr = &resp->xdr;
1042
1043 if (xdr_stream_encode_item_present(xdr) < 0)
1044 return false;
1045 /* fileid */
1046 if (xdr_stream_encode_u64(xdr, ino) < 0)
1047 return false;
1048 /* name */
1049 if (xdr_stream_encode_opaque(xdr, name, min(namlen, NFS3_MAXNAMLEN)) < 0)
1050 return false;
1051 /* cookie */
1052 resp->cookie_offset = dirlist->len;
c306d737 1053 if (xdr_stream_encode_u64(xdr, OFFSET_MAX) < 0)
7f87fc2d
CL
1054 return false;
1055
1056 return true;
1057}
1058
1059/**
1060 * nfs3svc_encode_entry3 - encode one NFSv3 READDIR entry
1061 * @data: directory context
1062 * @name: name of the object to be encoded
1063 * @namlen: length of that name, in bytes
1064 * @offset: the offset of the previous entry
1065 * @ino: the fileid of this entry
1066 * @d_type: unused
1067 *
1068 * Return values:
1069 * %0: Entry was successfully encoded.
1070 * %-EINVAL: An encoding problem occured, secondary status code in resp->common.err
1071 *
1072 * On exit, the following fields are updated:
1073 * - resp->xdr
1074 * - resp->common.err
1075 * - resp->cookie_offset
1076 */
1077int nfs3svc_encode_entry3(void *data, const char *name, int namlen,
1078 loff_t offset, u64 ino, unsigned int d_type)
1079{
1080 struct readdir_cd *ccd = data;
1081 struct nfsd3_readdirres *resp = container_of(ccd,
1082 struct nfsd3_readdirres,
1083 common);
1084 unsigned int starting_length = resp->dirlist.len;
1085
1086 /* The offset cookie for the previous entry */
1087 nfs3svc_encode_cookie3(resp, offset);
1088
1089 if (!svcxdr_encode_entry3_common(resp, name, namlen, offset, ino))
1090 goto out_toosmall;
1091
1092 xdr_commit_encode(&resp->xdr);
1093 resp->common.err = nfs_ok;
1094 return 0;
1095
1096out_toosmall:
1097 resp->cookie_offset = 0;
1098 resp->common.err = nfserr_toosmall;
1099 resp->dirlist.len = starting_length;
1100 return -EINVAL;
1101}
1102
1103static bool
1104svcxdr_encode_entry3_plus(struct nfsd3_readdirres *resp, const char *name,
1105 int namlen, u64 ino)
1106{
1107 struct xdr_stream *xdr = &resp->xdr;
1108 struct svc_fh *fhp = &resp->scratch;
1109 bool result;
1110
1111 result = false;
1112 fh_init(fhp, NFS3_FHSIZE);
1113 if (compose_entry_fh(resp, fhp, name, namlen, ino) != nfs_ok)
1114 goto out_noattrs;
1115
1116 if (!svcxdr_encode_post_op_attr(resp->rqstp, xdr, fhp))
1117 goto out;
1118 if (!svcxdr_encode_post_op_fh3(xdr, fhp))
1119 goto out;
1120 result = true;
1121
1122out:
1123 fh_put(fhp);
1124 return result;
1125
1126out_noattrs:
1127 if (xdr_stream_encode_item_absent(xdr) < 0)
1128 return false;
1129 if (xdr_stream_encode_item_absent(xdr) < 0)
1130 return false;
1131 return true;
1132}
1133
1134/**
1135 * nfs3svc_encode_entryplus3 - encode one NFSv3 READDIRPLUS entry
1136 * @data: directory context
1137 * @name: name of the object to be encoded
1138 * @namlen: length of that name, in bytes
1139 * @offset: the offset of the previous entry
1140 * @ino: the fileid of this entry
1141 * @d_type: unused
1142 *
1143 * Return values:
1144 * %0: Entry was successfully encoded.
1145 * %-EINVAL: An encoding problem occured, secondary status code in resp->common.err
1146 *
1147 * On exit, the following fields are updated:
1148 * - resp->xdr
1149 * - resp->common.err
1150 * - resp->cookie_offset
1151 */
1152int nfs3svc_encode_entryplus3(void *data, const char *name, int namlen,
1153 loff_t offset, u64 ino, unsigned int d_type)
1154{
1155 struct readdir_cd *ccd = data;
1156 struct nfsd3_readdirres *resp = container_of(ccd,
1157 struct nfsd3_readdirres,
1158 common);
1159 unsigned int starting_length = resp->dirlist.len;
1160
1161 /* The offset cookie for the previous entry */
1162 nfs3svc_encode_cookie3(resp, offset);
1163
1164 if (!svcxdr_encode_entry3_common(resp, name, namlen, offset, ino))
1165 goto out_toosmall;
1166 if (!svcxdr_encode_entry3_plus(resp, name, namlen, ino))
1167 goto out_toosmall;
1168
1169 xdr_commit_encode(&resp->xdr);
1170 resp->common.err = nfs_ok;
1171 return 0;
1172
1173out_toosmall:
1174 resp->cookie_offset = 0;
1175 resp->common.err = nfserr_toosmall;
1176 resp->dirlist.len = starting_length;
1177 return -EINVAL;
1178}
1179
8b704498
CL
1180static bool
1181svcxdr_encode_fsstat3resok(struct xdr_stream *xdr,
1182 const struct nfsd3_fsstatres *resp)
1183{
1184 const struct kstatfs *s = &resp->stats;
1185 u64 bs = s->f_bsize;
1186 __be32 *p;
1187
1188 p = xdr_reserve_space(xdr, XDR_UNIT * 13);
1189 if (!p)
1190 return false;
1191 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1192 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1193 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1194 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1195 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1196 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1197 *p = cpu_to_be32(resp->invarsec); /* mean unchanged time */
1198
1199 return true;
1200}
1201
1da177e4 1202/* FSSTAT */
130e2054 1203bool
fda49441 1204nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 1205{
63f8de37 1206 struct nfsd3_fsstatres *resp = rqstp->rq_resp;
1da177e4 1207
8b704498 1208 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
130e2054 1209 return false;
8b704498
CL
1210 switch (resp->status) {
1211 case nfs_ok:
1212 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
130e2054 1213 return false;
8b704498 1214 if (!svcxdr_encode_fsstat3resok(xdr, resp))
130e2054 1215 return false;
8b704498
CL
1216 break;
1217 default:
1218 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
130e2054 1219 return false;
1da177e4 1220 }
8b704498 1221
130e2054 1222 return true;
1da177e4
LT
1223}
1224
0a139d1b
CL
1225static bool
1226svcxdr_encode_fsinfo3resok(struct xdr_stream *xdr,
1227 const struct nfsd3_fsinfores *resp)
1228{
1229 __be32 *p;
1230
1231 p = xdr_reserve_space(xdr, XDR_UNIT * 12);
1232 if (!p)
1233 return false;
1234 *p++ = cpu_to_be32(resp->f_rtmax);
1235 *p++ = cpu_to_be32(resp->f_rtpref);
1236 *p++ = cpu_to_be32(resp->f_rtmult);
1237 *p++ = cpu_to_be32(resp->f_wtmax);
1238 *p++ = cpu_to_be32(resp->f_wtpref);
1239 *p++ = cpu_to_be32(resp->f_wtmult);
1240 *p++ = cpu_to_be32(resp->f_dtpref);
1241 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1242 p = encode_nfstime3(p, &nfs3svc_time_delta);
1243 *p = cpu_to_be32(resp->f_properties);
1244
1245 return true;
1246}
1247
1da177e4 1248/* FSINFO */
130e2054 1249bool
fda49441 1250nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 1251{
63f8de37
CH
1252 struct nfsd3_fsinfores *resp = rqstp->rq_resp;
1253
0a139d1b 1254 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
130e2054 1255 return false;
0a139d1b
CL
1256 switch (resp->status) {
1257 case nfs_ok:
1258 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
130e2054 1259 return false;
0a139d1b 1260 if (!svcxdr_encode_fsinfo3resok(xdr, resp))
130e2054 1261 return false;
0a139d1b
CL
1262 break;
1263 default:
1264 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
130e2054 1265 return false;
1da177e4
LT
1266 }
1267
130e2054 1268 return true;
1da177e4
LT
1269}
1270
ded04a58
CL
1271static bool
1272svcxdr_encode_pathconf3resok(struct xdr_stream *xdr,
1273 const struct nfsd3_pathconfres *resp)
1274{
1275 __be32 *p;
1276
1277 p = xdr_reserve_space(xdr, XDR_UNIT * 6);
1278 if (!p)
1279 return false;
1280 *p++ = cpu_to_be32(resp->p_link_max);
1281 *p++ = cpu_to_be32(resp->p_name_max);
1282 p = xdr_encode_bool(p, resp->p_no_trunc);
1283 p = xdr_encode_bool(p, resp->p_chown_restricted);
1284 p = xdr_encode_bool(p, resp->p_case_insensitive);
1285 xdr_encode_bool(p, resp->p_case_preserving);
1286
1287 return true;
1288}
1289
1da177e4 1290/* PATHCONF */
130e2054 1291bool
fda49441 1292nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 1293{
63f8de37
CH
1294 struct nfsd3_pathconfres *resp = rqstp->rq_resp;
1295
ded04a58 1296 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
130e2054 1297 return false;
ded04a58
CL
1298 switch (resp->status) {
1299 case nfs_ok:
1300 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
130e2054 1301 return false;
ded04a58 1302 if (!svcxdr_encode_pathconf3resok(xdr, resp))
130e2054 1303 return false;
ded04a58
CL
1304 break;
1305 default:
1306 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
130e2054 1307 return false;
1da177e4
LT
1308 }
1309
130e2054 1310 return true;
1da177e4
LT
1311}
1312
1313/* COMMIT */
130e2054 1314bool
fda49441 1315nfs3svc_encode_commitres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1da177e4 1316{
63f8de37 1317 struct nfsd3_commitres *resp = rqstp->rq_resp;
b9c0ef85 1318
5ef2826c 1319 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
130e2054 1320 return false;
5ef2826c
CL
1321 switch (resp->status) {
1322 case nfs_ok:
1323 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
130e2054 1324 return false;
5ef2826c 1325 if (!svcxdr_encode_writeverf3(xdr, resp->verf))
130e2054 1326 return false;
5ef2826c
CL
1327 break;
1328 default:
1329 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
130e2054 1330 return false;
1da177e4 1331 }
5ef2826c 1332
130e2054 1333 return true;
1da177e4
LT
1334}
1335
1336/*
1337 * XDR release functions
1338 */
8537488b
CH
1339void
1340nfs3svc_release_fhandle(struct svc_rqst *rqstp)
1da177e4 1341{
8537488b
CH
1342 struct nfsd3_attrstat *resp = rqstp->rq_resp;
1343
1da177e4 1344 fh_put(&resp->fh);
1da177e4
LT
1345}
1346
8537488b
CH
1347void
1348nfs3svc_release_fhandle2(struct svc_rqst *rqstp)
1da177e4 1349{
8537488b
CH
1350 struct nfsd3_fhandle_pair *resp = rqstp->rq_resp;
1351
1da177e4
LT
1352 fh_put(&resp->fh1);
1353 fh_put(&resp->fh2);
1da177e4 1354}