blob: c3ac1b6aa3aaa14577d37483c4e5cd6199dcef19 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/namei.h>
Stanislav Kinsburskyb9c0ef82012-12-06 14:23:19 +030011#include <linux/sunrpc/svc_xprt.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020012#include "xdr3.h"
J. Bruce Fields2e8138a2007-11-15 17:05:43 -050013#include "auth.h"
Stanislav Kinsburskyb9c0ef82012-12-06 14:23:19 +030014#include "netns.h"
Al Viro3dadecc2013-01-24 02:18:08 -050015#include "vfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Linus Torvalds1da177e2005-04-16 15:20:36 -070017/*
Chuck Lever8b704492020-11-06 13:08:45 -050018 * Force construction of an empty post-op attr
19 */
20static const struct svc_fh nfs3svc_null_fh = {
21 .fh_no_wcc = true,
22};
23
24/*
Chuck Lever0a139d12020-10-22 13:42:13 -040025 * 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
33/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 * Mapping of S_IF* types to NFS file types
35 */
Chuck Lever2c42f802020-10-21 11:58:41 -040036static const u32 nfs3_ftypes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 NF3NON, NF3FIFO, NF3CHR, NF3BAD,
38 NF3DIR, NF3BAD, NF3BLK, NF3BAD,
39 NF3REG, NF3BAD, NF3LNK, NF3BAD,
40 NF3SOCK, NF3BAD, NF3LNK, NF3BAD,
41};
42
Trond Myklebust27c438f2019-09-02 13:02:56 -040043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
Chuck Lever95753632020-10-20 14:30:02 -040045 * Basic NFSv3 data types (RFC 1813 Sections 2.5 and 2.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 */
Chuck Lever95753632020-10-20 14:30:02 -040047
Adrian Bunk3ee6f612006-12-06 20:40:23 -080048static __be32 *
Chuck Lever2c42f802020-10-21 11:58:41 -040049encode_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
Chuck Lever9cde9362020-10-20 15:48:22 -040057static bool
58svcxdr_decode_nfstime3(struct xdr_stream *xdr, struct timespec64 *timep)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Chuck Lever9cde9362020-10-20 15:48:22 -040060 __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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
Chuck Lever05027ea2020-11-17 11:52:04 -050071/**
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
Chuck Lever95753632020-10-20 14:30:02 -040081svcxdr_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;
NeilBrownd8b26072021-09-02 11:16:32 +100095 memcpy(&fhp->fh_handle.fh_raw, p, size);
Chuck Lever95753632020-10-20 14:30:02 -040096
97 return true;
98}
99
Chuck Lever20798df2020-11-18 16:11:42 -0500100/**
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
Chuck Lever2c42f802020-10-21 11:58:41 -0400110svcxdr_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
Chuck Lever5cf35332020-10-22 14:46:58 -0400122static 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;
NeilBrownd8b26072021-09-02 11:16:32 +1000134 memcpy(p, &fhp->fh_handle.fh_raw, size);
Chuck Lever5cf35332020-10-22 14:46:58 -0400135
136 return true;
137}
138
Chuck Lever78315b32020-10-22 15:27:23 -0400139static 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
Chuck Lever54d1d432020-10-20 15:42:33 -0400150static bool
Chuck Levere4ccfe32020-10-22 19:31:48 -0400151svcxdr_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
163static bool
Chuck Leverecb7a082020-10-22 15:26:31 -0400164svcxdr_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
176static bool
Chuck Lever54d1d432020-10-20 15:42:33 -0400177svcxdr_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
Chuck Lever9cde9362020-10-20 15:48:22 -0400209static 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;
257 iap->ia_size = min_t(u64, newsize, NFS_OFFSET_MAX);
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
Chuck Leverf8a38e22020-10-20 17:04:03 -0400315static bool
316svcxdr_decode_specdata3(struct xdr_stream *xdr, struct nfsd3_mknodargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Chuck Leverf8a38e22020-10-20 17:04:03 -0400318 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Chuck Leverf8a38e22020-10-20 17:04:03 -0400320 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Chuck Leverf8a38e22020-10-20 17:04:03 -0400326 return true;
327}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Chuck Leverf8a38e22020-10-20 17:04:03 -0400329static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
Chuck Lever2c42f802020-10-21 11:58:41 -0400337static 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
Chuck Lever907c3822020-10-22 13:56:58 -0400389static bool
Chuck Lever70f8e832020-10-22 15:12:38 -0400390svcxdr_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
Chuck Lever20798df2020-11-18 16:11:42 -0500418/**
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
Chuck Lever907c3822020-10-22 13:56:58 -0400429svcxdr_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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/*
Chuck Lever70f8e832020-10-22 15:12:38 -0400458 * 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
J. Bruce Fields428a23d2021-01-29 14:27:01 -0500490static bool fs_supports_change_attribute(struct super_block *sb)
491{
492 return sb->s_flags & SB_I_VERSION || sb->s_export_op->fetch_iversion;
493}
494
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400495/*
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200496 * Fill in the pre_op attr for the wcc data
497 */
498void fill_pre_wcc(struct svc_fh *fhp)
499{
500 struct inode *inode;
501 struct kstat stat;
J. Bruce Fields942b20dc2020-11-30 17:46:17 -0500502 bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200503
Jeff Laytondaab1102020-11-30 17:03:14 -0500504 if (fhp->fh_no_wcc || fhp->fh_pre_saved)
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200505 return;
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200506 inode = d_inode(fhp->fh_dentry);
J. Bruce Fields428a23d2021-01-29 14:27:01 -0500507 if (fs_supports_change_attribute(inode->i_sb) || !v4) {
508 __be32 err = fh_getattr(fhp, &stat);
509
510 if (err) {
511 /* Grab the times from inode anyway */
512 stat.mtime = inode->i_mtime;
513 stat.ctime = inode->i_ctime;
514 stat.size = inode->i_size;
515 }
516 fhp->fh_pre_mtime = stat.mtime;
517 fhp->fh_pre_ctime = stat.ctime;
518 fhp->fh_pre_size = stat.size;
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200519 }
J. Bruce Fields942b20dc2020-11-30 17:46:17 -0500520 if (v4)
521 fhp->fh_pre_change = nfsd4_change_attribute(&stat, inode);
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200522
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200523 fhp->fh_pre_saved = true;
524}
525
526/*
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400527 * Fill in the post_op attr for the wcc data
528 */
529void fill_post_wcc(struct svc_fh *fhp)
530{
J. Bruce Fields942b20dc2020-11-30 17:46:17 -0500531 bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
532 struct inode *inode = d_inode(fhp->fh_dentry);
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400533
Jeff Laytondaab1102020-11-30 17:03:14 -0500534 if (fhp->fh_no_wcc)
535 return;
536
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400537 if (fhp->fh_post_saved)
538 printk("nfsd: inode locked twice during operation.\n");
539
J. Bruce Fields428a23d2021-01-29 14:27:01 -0500540 fhp->fh_post_saved = true;
541
542 if (fs_supports_change_attribute(inode->i_sb) || !v4) {
543 __be32 err = fh_getattr(fhp, &fhp->fh_post_attr);
544
545 if (err) {
546 fhp->fh_post_saved = false;
547 fhp->fh_post_attr.ctime = inode->i_ctime;
548 }
549 }
J. Bruce Fields942b20dc2020-11-30 17:46:17 -0500550 if (v4)
551 fhp->fh_post_change =
552 nfsd4_change_attribute(&fhp->fh_post_attr, inode);
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400553}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555/*
556 * XDR decode functions
557 */
Chuck Leverdcc46992020-10-01 18:59:12 -0400558
Chuck Leverc44b31c2021-10-12 11:57:28 -0400559bool
Chuck Lever16c66362021-10-12 11:57:22 -0400560nfs3svc_decode_fhandleargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200562 struct nfsd_fhandle *args = rqstp->rq_argp;
563
Chuck Lever95753632020-10-20 14:30:02 -0400564 return svcxdr_decode_nfs_fh3(xdr, &args->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Chuck Leverc44b31c2021-10-12 11:57:28 -0400567bool
Chuck Lever16c66362021-10-12 11:57:22 -0400568nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200570 struct nfsd3_sattrargs *args = rqstp->rq_argp;
571
Chuck Lever9cde9362020-10-20 15:48:22 -0400572 return svcxdr_decode_nfs_fh3(xdr, &args->fh) &&
573 svcxdr_decode_sattr3(rqstp, xdr, &args->attrs) &&
574 svcxdr_decode_sattrguard3(xdr, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
Chuck Leverc44b31c2021-10-12 11:57:28 -0400577bool
Chuck Lever16c66362021-10-12 11:57:22 -0400578nfs3svc_decode_diropargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200580 struct nfsd3_diropargs *args = rqstp->rq_argp;
581
Chuck Lever54d1d432020-10-20 15:42:33 -0400582 return svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
Chuck Leverc44b31c2021-10-12 11:57:28 -0400585bool
Chuck Lever16c66362021-10-12 11:57:22 -0400586nfs3svc_decode_accessargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200588 struct nfsd3_accessargs *args = rqstp->rq_argp;
589
Chuck Lever3b921a22020-10-20 14:32:04 -0400590 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400591 return false;
Chuck Lever3b921a22020-10-20 14:32:04 -0400592 if (xdr_stream_decode_u32(xdr, &args->access) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400593 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Chuck Leverc44b31c2021-10-12 11:57:28 -0400595 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
Chuck Leverc44b31c2021-10-12 11:57:28 -0400598bool
Chuck Lever16c66362021-10-12 11:57:22 -0400599nfs3svc_decode_readargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200601 struct nfsd3_readargs *args = rqstp->rq_argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Chuck Leverbe63bd22020-10-20 14:34:40 -0400603 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400604 return false;
Chuck Leverbe63bd22020-10-20 14:34:40 -0400605 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400606 return false;
Chuck Leverbe63bd22020-10-20 14:34:40 -0400607 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400608 return false;
J. Bruce Fields9512a162017-05-16 15:57:42 -0400609
Chuck Leverc44b31c2021-10-12 11:57:28 -0400610 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
Chuck Leverc44b31c2021-10-12 11:57:28 -0400613bool
Chuck Lever16c66362021-10-12 11:57:22 -0400614nfs3svc_decode_writeargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200616 struct nfsd3_writeargs *args = rqstp->rq_argp;
Greg Banks7adae482006-10-04 02:15:47 -0700617 u32 max_blocksize = svc_max_payload(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Chuck Leverc43b2f22020-10-22 11:14:55 -0400619 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400620 return false;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400621 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400622 return false;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400623 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400624 return false;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400625 if (xdr_stream_decode_u32(xdr, &args->stable) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400626 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Chuck Leverc43b2f22020-10-22 11:14:55 -0400628 /* opaque data */
629 if (xdr_stream_decode_u32(xdr, &args->len) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400630 return false;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400631
632 /* request sanity */
Peter Staubachf34b95682007-05-09 02:34:48 -0700633 if (args->count != args->len)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400634 return false;
Peter Staubachf34b95682007-05-09 02:34:48 -0700635 if (args->count > max_blocksize) {
636 args->count = max_blocksize;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400637 args->len = max_blocksize;
Peter Staubachf34b95682007-05-09 02:34:48 -0700638 }
Chuck Leverdae9a6c2021-09-30 17:06:21 -0400639 if (!xdr_stream_subsegment(xdr, &args->payload, args->count))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400640 return false;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400641
Chuck Leverc44b31c2021-10-12 11:57:28 -0400642 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Chuck Leverc44b31c2021-10-12 11:57:28 -0400645bool
Chuck Lever16c66362021-10-12 11:57:22 -0400646nfs3svc_decode_createargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200648 struct nfsd3_createargs *args = rqstp->rq_argp;
649
Chuck Lever6b3a1192020-10-20 15:56:11 -0400650 if (!svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400651 return false;
Chuck Lever6b3a1192020-10-20 15:56:11 -0400652 if (xdr_stream_decode_u32(xdr, &args->createmode) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400653 return false;
Chuck Lever6b3a1192020-10-20 15:56:11 -0400654 switch (args->createmode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 case NFS3_CREATE_UNCHECKED:
656 case NFS3_CREATE_GUARDED:
Chuck Lever6b3a1192020-10-20 15:56:11 -0400657 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 case NFS3_CREATE_EXCLUSIVE:
Chuck Lever6b3a1192020-10-20 15:56:11 -0400659 args->verf = xdr_inline_decode(xdr, NFS3_CREATEVERFSIZE);
660 if (!args->verf)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400661 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 break;
663 default:
Chuck Leverc44b31c2021-10-12 11:57:28 -0400664 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
Chuck Leverc44b31c2021-10-12 11:57:28 -0400666 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
Christoph Hellwig026fec72017-05-08 19:01:48 +0200668
Chuck Leverc44b31c2021-10-12 11:57:28 -0400669bool
Chuck Lever16c66362021-10-12 11:57:22 -0400670nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200672 struct nfsd3_createargs *args = rqstp->rq_argp;
673
Chuck Lever83374c22020-10-20 17:02:16 -0400674 return svcxdr_decode_diropargs3(xdr, &args->fh,
675 &args->name, &args->len) &&
676 svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
Chuck Leverc44b31c2021-10-12 11:57:28 -0400679bool
Chuck Lever16c66362021-10-12 11:57:22 -0400680nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200682 struct nfsd3_symlinkargs *args = rqstp->rq_argp;
Chuck Leverda392012020-10-20 16:01:16 -0400683 struct kvec *head = rqstp->rq_arg.head;
684 struct kvec *tail = rqstp->rq_arg.tail;
685 size_t remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Chuck Leverda392012020-10-20 16:01:16 -0400687 if (!svcxdr_decode_diropargs3(xdr, &args->ffh, &args->fname, &args->flen))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400688 return false;
Chuck Leverda392012020-10-20 16:01:16 -0400689 if (!svcxdr_decode_sattr3(rqstp, xdr, &args->attrs))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400690 return false;
Chuck Leverda392012020-10-20 16:01:16 -0400691 if (xdr_stream_decode_u32(xdr, &args->tlen) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400692 return false;
Chuck Leverda392012020-10-20 16:01:16 -0400693
694 /* request sanity */
695 remaining = head->iov_len + rqstp->rq_arg.page_len + tail->iov_len;
696 remaining -= xdr_stream_pos(xdr);
697 if (remaining < xdr_align_size(args->tlen))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400698 return false;
Chuck Leverda392012020-10-20 16:01:16 -0400699
700 args->first.iov_base = xdr->p;
701 args->first.iov_len = head->iov_len - xdr_stream_pos(xdr);
702
Chuck Leverc44b31c2021-10-12 11:57:28 -0400703 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
Chuck Leverc44b31c2021-10-12 11:57:28 -0400706bool
Chuck Lever16c66362021-10-12 11:57:22 -0400707nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200709 struct nfsd3_mknodargs *args = rqstp->rq_argp;
710
Chuck Leverf8a38e22020-10-20 17:04:03 -0400711 if (!svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400712 return false;
Chuck Leverf8a38e22020-10-20 17:04:03 -0400713 if (xdr_stream_decode_u32(xdr, &args->ftype) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400714 return false;
Chuck Leverf8a38e22020-10-20 17:04:03 -0400715 switch (args->ftype) {
716 case NF3CHR:
717 case NF3BLK:
718 return svcxdr_decode_devicedata3(rqstp, xdr, args);
719 case NF3SOCK:
720 case NF3FIFO:
721 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
722 case NF3REG:
723 case NF3DIR:
724 case NF3LNK:
725 /* Valid XDR but illegal file types */
726 break;
727 default:
Chuck Leverc44b31c2021-10-12 11:57:28 -0400728 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730
Chuck Leverc44b31c2021-10-12 11:57:28 -0400731 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
Chuck Leverc44b31c2021-10-12 11:57:28 -0400734bool
Chuck Lever16c66362021-10-12 11:57:22 -0400735nfs3svc_decode_renameargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200737 struct nfsd3_renameargs *args = rqstp->rq_argp;
738
Chuck Leverd181e0a2020-10-20 15:44:12 -0400739 return svcxdr_decode_diropargs3(xdr, &args->ffh,
740 &args->fname, &args->flen) &&
741 svcxdr_decode_diropargs3(xdr, &args->tfh,
742 &args->tname, &args->tlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
Chuck Leverc44b31c2021-10-12 11:57:28 -0400745bool
Chuck Lever16c66362021-10-12 11:57:22 -0400746nfs3svc_decode_linkargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200748 struct nfsd3_linkargs *args = rqstp->rq_argp;
749
Chuck Leverefaa1e72020-10-19 13:26:32 -0400750 return svcxdr_decode_nfs_fh3(xdr, &args->ffh) &&
751 svcxdr_decode_diropargs3(xdr, &args->tfh,
752 &args->tname, &args->tlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
Chuck Leverc44b31c2021-10-12 11:57:28 -0400755bool
Chuck Lever16c66362021-10-12 11:57:22 -0400756nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200758 struct nfsd3_readdirargs *args = rqstp->rq_argp;
NeilBrownf875a792019-03-07 09:49:46 +1100759
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400760 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400761 return false;
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400762 if (xdr_stream_decode_u64(xdr, &args->cookie) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400763 return false;
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400764 args->verf = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE);
765 if (!args->verf)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400766 return false;
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400767 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400768 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Chuck Leverc44b31c2021-10-12 11:57:28 -0400770 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
Chuck Leverc44b31c2021-10-12 11:57:28 -0400773bool
Chuck Lever16c66362021-10-12 11:57:22 -0400774nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200776 struct nfsd3_readdirargs *args = rqstp->rq_argp;
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400777 u32 dircount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400779 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400780 return false;
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400781 if (xdr_stream_decode_u64(xdr, &args->cookie) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400782 return false;
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400783 args->verf = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE);
784 if (!args->verf)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400785 return false;
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400786 /* dircount is ignored */
787 if (xdr_stream_decode_u32(xdr, &dircount) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400788 return false;
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400789 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400790 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Chuck Leverc44b31c2021-10-12 11:57:28 -0400792 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
Chuck Leverc44b31c2021-10-12 11:57:28 -0400795bool
Chuck Lever16c66362021-10-12 11:57:22 -0400796nfs3svc_decode_commitargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200798 struct nfsd3_commitargs *args = rqstp->rq_argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Chuck Leverc8d26a02020-10-20 14:41:56 -0400800 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Chuck Leverc44b31c2021-10-12 11:57:28 -0400801 return false;
Chuck Leverc8d26a02020-10-20 14:41:56 -0400802 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400803 return false;
Chuck Leverc8d26a02020-10-20 14:41:56 -0400804 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -0400805 return false;
Chuck Leverc8d26a02020-10-20 14:41:56 -0400806
Chuck Leverc44b31c2021-10-12 11:57:28 -0400807 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
810/*
811 * XDR encode functions
812 */
Chuck Levercc028a12020-10-02 15:52:44 -0400813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814/* GETATTR */
Chuck Lever130e2052021-10-13 10:41:13 -0400815bool
Chuck Leverfda49442021-10-13 10:41:06 -0400816nfs3svc_encode_getattrres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200818 struct nfsd3_attrstat *resp = rqstp->rq_resp;
819
Chuck Lever2c42f802020-10-21 11:58:41 -0400820 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
Chuck Lever130e2052021-10-13 10:41:13 -0400821 return false;
Chuck Lever2c42f802020-10-21 11:58:41 -0400822 switch (resp->status) {
823 case nfs_ok:
824 lease_get_mtime(d_inode(resp->fh.fh_dentry), &resp->stat.mtime);
825 if (!svcxdr_encode_fattr3(rqstp, xdr, &resp->fh, &resp->stat))
Chuck Lever130e2052021-10-13 10:41:13 -0400826 return false;
Chuck Lever2c42f802020-10-21 11:58:41 -0400827 break;
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400828 }
Chuck Lever2c42f802020-10-21 11:58:41 -0400829
Chuck Lever130e2052021-10-13 10:41:13 -0400830 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
833/* SETATTR, REMOVE, RMDIR */
Chuck Lever130e2052021-10-13 10:41:13 -0400834bool
Chuck Leverfda49442021-10-13 10:41:06 -0400835nfs3svc_encode_wccstat(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200837 struct nfsd3_attrstat *resp = rqstp->rq_resp;
838
Chuck Lever70f8e832020-10-22 15:12:38 -0400839 return svcxdr_encode_nfsstat3(xdr, resp->status) &&
840 svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
843/* LOOKUP */
Chuck Lever130e2052021-10-13 10:41:13 -0400844bool
Chuck Leverfda49442021-10-13 10:41:06 -0400845nfs3svc_encode_lookupres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200847 struct nfsd3_diropres *resp = rqstp->rq_resp;
848
Chuck Lever5cf35332020-10-22 14:46:58 -0400849 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
Chuck Lever130e2052021-10-13 10:41:13 -0400850 return false;
Chuck Lever5cf35332020-10-22 14:46:58 -0400851 switch (resp->status) {
852 case nfs_ok:
853 if (!svcxdr_encode_nfs_fh3(xdr, &resp->fh))
Chuck Lever130e2052021-10-13 10:41:13 -0400854 return false;
Chuck Lever5cf35332020-10-22 14:46:58 -0400855 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
Chuck Lever130e2052021-10-13 10:41:13 -0400856 return false;
Chuck Lever5cf35332020-10-22 14:46:58 -0400857 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->dirfh))
Chuck Lever130e2052021-10-13 10:41:13 -0400858 return false;
Chuck Lever5cf35332020-10-22 14:46:58 -0400859 break;
860 default:
861 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->dirfh))
Chuck Lever130e2052021-10-13 10:41:13 -0400862 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
Chuck Lever5cf35332020-10-22 14:46:58 -0400864
Chuck Lever130e2052021-10-13 10:41:13 -0400865 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
868/* ACCESS */
Chuck Lever130e2052021-10-13 10:41:13 -0400869bool
Chuck Leverfda49442021-10-13 10:41:06 -0400870nfs3svc_encode_accessres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200872 struct nfsd3_accessres *resp = rqstp->rq_resp;
873
Chuck Lever907c3822020-10-22 13:56:58 -0400874 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
Chuck Lever130e2052021-10-13 10:41:13 -0400875 return false;
Chuck Lever907c3822020-10-22 13:56:58 -0400876 switch (resp->status) {
877 case nfs_ok:
878 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
Chuck Lever130e2052021-10-13 10:41:13 -0400879 return false;
Chuck Lever907c3822020-10-22 13:56:58 -0400880 if (xdr_stream_encode_u32(xdr, resp->access) < 0)
Chuck Lever130e2052021-10-13 10:41:13 -0400881 return false;
Chuck Lever907c3822020-10-22 13:56:58 -0400882 break;
883 default:
884 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
Chuck Lever130e2052021-10-13 10:41:13 -0400885 return false;
Chuck Lever907c3822020-10-22 13:56:58 -0400886 }
887
Chuck Lever130e2052021-10-13 10:41:13 -0400888 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
891/* READLINK */
Chuck Lever130e2052021-10-13 10:41:13 -0400892bool
Chuck Leverfda49442021-10-13 10:41:06 -0400893nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200895 struct nfsd3_readlinkres *resp = rqstp->rq_resp;
Chuck Lever76e54922020-11-05 10:24:19 -0500896 struct kvec *head = rqstp->rq_res.head;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200897
Chuck Lever9a9c8922020-10-22 15:18:40 -0400898 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
Chuck Lever130e2052021-10-13 10:41:13 -0400899 return false;
Chuck Lever9a9c8922020-10-22 15:18:40 -0400900 switch (resp->status) {
901 case nfs_ok:
902 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
Chuck Lever130e2052021-10-13 10:41:13 -0400903 return false;
Chuck Lever9a9c8922020-10-22 15:18:40 -0400904 if (xdr_stream_encode_u32(xdr, resp->len) < 0)
Chuck Lever130e2052021-10-13 10:41:13 -0400905 return false;
Chuck Lever9a9c8922020-10-22 15:18:40 -0400906 xdr_write_pages(xdr, resp->pages, 0, resp->len);
907 if (svc_encode_result_payload(rqstp, head->iov_len, resp->len) < 0)
Chuck Lever130e2052021-10-13 10:41:13 -0400908 return false;
Chuck Lever9a9c8922020-10-22 15:18:40 -0400909 break;
910 default:
911 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
Chuck Lever130e2052021-10-13 10:41:13 -0400912 return false;
Chuck Lever9a9c8922020-10-22 15:18:40 -0400913 }
914
Chuck Lever130e2052021-10-13 10:41:13 -0400915 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
918/* READ */
Chuck Lever130e2052021-10-13 10:41:13 -0400919bool
Chuck Leverfda49442021-10-13 10:41:06 -0400920nfs3svc_encode_readres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200922 struct nfsd3_readres *resp = rqstp->rq_resp;
Chuck Lever76e54922020-11-05 10:24:19 -0500923 struct kvec *head = rqstp->rq_res.head;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200924
Chuck Levercc9bcda2020-10-22 15:23:50 -0400925 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
Chuck Lever130e2052021-10-13 10:41:13 -0400926 return false;
Chuck Levercc9bcda2020-10-22 15:23:50 -0400927 switch (resp->status) {
928 case nfs_ok:
929 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
Chuck Lever130e2052021-10-13 10:41:13 -0400930 return false;
Chuck Levercc9bcda2020-10-22 15:23:50 -0400931 if (xdr_stream_encode_u32(xdr, resp->count) < 0)
Chuck Lever130e2052021-10-13 10:41:13 -0400932 return false;
Chuck Levercc9bcda2020-10-22 15:23:50 -0400933 if (xdr_stream_encode_bool(xdr, resp->eof) < 0)
Chuck Lever130e2052021-10-13 10:41:13 -0400934 return false;
Chuck Levercc9bcda2020-10-22 15:23:50 -0400935 if (xdr_stream_encode_u32(xdr, resp->count) < 0)
Chuck Lever130e2052021-10-13 10:41:13 -0400936 return false;
Chuck Levercc9bcda2020-10-22 15:23:50 -0400937 xdr_write_pages(xdr, resp->pages, rqstp->rq_res.page_base,
938 resp->count);
939 if (svc_encode_result_payload(rqstp, head->iov_len, resp->count) < 0)
Chuck Lever130e2052021-10-13 10:41:13 -0400940 return false;
Chuck Levercc9bcda2020-10-22 15:23:50 -0400941 break;
942 default:
943 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
Chuck Lever130e2052021-10-13 10:41:13 -0400944 return false;
Chuck Levercc9bcda2020-10-22 15:23:50 -0400945 }
946
Chuck Lever130e2052021-10-13 10:41:13 -0400947 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
950/* WRITE */
Chuck Lever130e2052021-10-13 10:41:13 -0400951bool
Chuck Leverfda49442021-10-13 10:41:06 -0400952nfs3svc_encode_writeres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200954 struct nfsd3_writeres *resp = rqstp->rq_resp;
Stanislav Kinsburskyb9c0ef82012-12-06 14:23:19 +0300955
Chuck Leverecb7a082020-10-22 15:26:31 -0400956 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
Chuck Lever130e2052021-10-13 10:41:13 -0400957 return false;
Chuck Leverecb7a082020-10-22 15:26:31 -0400958 switch (resp->status) {
959 case nfs_ok:
960 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
Chuck Lever130e2052021-10-13 10:41:13 -0400961 return false;
Chuck Leverecb7a082020-10-22 15:26:31 -0400962 if (xdr_stream_encode_u32(xdr, resp->count) < 0)
Chuck Lever130e2052021-10-13 10:41:13 -0400963 return false;
Chuck Leverecb7a082020-10-22 15:26:31 -0400964 if (xdr_stream_encode_u32(xdr, resp->committed) < 0)
Chuck Lever130e2052021-10-13 10:41:13 -0400965 return false;
Chuck Leverecb7a082020-10-22 15:26:31 -0400966 if (!svcxdr_encode_writeverf3(xdr, resp->verf))
Chuck Lever130e2052021-10-13 10:41:13 -0400967 return false;
Chuck Leverecb7a082020-10-22 15:26:31 -0400968 break;
969 default:
970 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
Chuck Lever130e2052021-10-13 10:41:13 -0400971 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
Chuck Leverecb7a082020-10-22 15:26:31 -0400973
Chuck Lever130e2052021-10-13 10:41:13 -0400974 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
977/* CREATE, MKDIR, SYMLINK, MKNOD */
Chuck Lever130e2052021-10-13 10:41:13 -0400978bool
Chuck Leverfda49442021-10-13 10:41:06 -0400979nfs3svc_encode_createres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200981 struct nfsd3_diropres *resp = rqstp->rq_resp;
982
Chuck Lever78315b32020-10-22 15:27:23 -0400983 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
Chuck Lever130e2052021-10-13 10:41:13 -0400984 return false;
Chuck Lever78315b32020-10-22 15:27:23 -0400985 switch (resp->status) {
986 case nfs_ok:
987 if (!svcxdr_encode_post_op_fh3(xdr, &resp->fh))
Chuck Lever130e2052021-10-13 10:41:13 -0400988 return false;
Chuck Lever78315b32020-10-22 15:27:23 -0400989 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
Chuck Lever130e2052021-10-13 10:41:13 -0400990 return false;
Chuck Lever78315b32020-10-22 15:27:23 -0400991 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->dirfh))
Chuck Lever130e2052021-10-13 10:41:13 -0400992 return false;
Chuck Lever78315b32020-10-22 15:27:23 -0400993 break;
994 default:
995 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->dirfh))
Chuck Lever130e2052021-10-13 10:41:13 -0400996 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
Chuck Lever78315b32020-10-22 15:27:23 -0400998
Chuck Lever130e2052021-10-13 10:41:13 -0400999 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
1002/* RENAME */
Chuck Lever130e2052021-10-13 10:41:13 -04001003bool
Chuck Leverfda49442021-10-13 10:41:06 -04001004nfs3svc_encode_renameres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001006 struct nfsd3_renameres *resp = rqstp->rq_resp;
1007
Chuck Lever89d79e92020-10-22 15:33:05 -04001008 return svcxdr_encode_nfsstat3(xdr, resp->status) &&
1009 svcxdr_encode_wcc_data(rqstp, xdr, &resp->ffh) &&
1010 svcxdr_encode_wcc_data(rqstp, xdr, &resp->tfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
1013/* LINK */
Chuck Lever130e2052021-10-13 10:41:13 -04001014bool
Chuck Leverfda49442021-10-13 10:41:06 -04001015nfs3svc_encode_linkres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001017 struct nfsd3_linkres *resp = rqstp->rq_resp;
1018
Chuck Lever4d743802020-10-22 15:08:29 -04001019 return svcxdr_encode_nfsstat3(xdr, resp->status) &&
1020 svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh) &&
1021 svcxdr_encode_wcc_data(rqstp, xdr, &resp->tfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
1024/* READDIR */
Chuck Lever130e2052021-10-13 10:41:13 -04001025bool
Chuck Leverfda49442021-10-13 10:41:06 -04001026nfs3svc_encode_readdirres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001028 struct nfsd3_readdirres *resp = rqstp->rq_resp;
Chuck Lever7f87fc22020-10-22 19:46:58 -04001029 struct xdr_buf *dirlist = &resp->dirlist;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001030
Chuck Levere4ccfe32020-10-22 19:31:48 -04001031 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
Chuck Lever130e2052021-10-13 10:41:13 -04001032 return false;
Chuck Levere4ccfe32020-10-22 19:31:48 -04001033 switch (resp->status) {
1034 case nfs_ok:
1035 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
Chuck Lever130e2052021-10-13 10:41:13 -04001036 return false;
Chuck Levere4ccfe32020-10-22 19:31:48 -04001037 if (!svcxdr_encode_cookieverf3(xdr, resp->verf))
Chuck Lever130e2052021-10-13 10:41:13 -04001038 return false;
Chuck Lever7f87fc22020-10-22 19:46:58 -04001039 xdr_write_pages(xdr, dirlist->pages, 0, dirlist->len);
Chuck Levere4ccfe32020-10-22 19:31:48 -04001040 /* no more entries */
1041 if (xdr_stream_encode_item_absent(xdr) < 0)
Chuck Lever130e2052021-10-13 10:41:13 -04001042 return false;
Chuck Levere4ccfe32020-10-22 19:31:48 -04001043 if (xdr_stream_encode_bool(xdr, resp->common.err == nfserr_eof) < 0)
Chuck Lever130e2052021-10-13 10:41:13 -04001044 return false;
Chuck Levere4ccfe32020-10-22 19:31:48 -04001045 break;
1046 default:
1047 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
Chuck Lever130e2052021-10-13 10:41:13 -04001048 return false;
Chuck Levere4ccfe32020-10-22 19:31:48 -04001049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Chuck Lever130e2052021-10-13 10:41:13 -04001051 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
Al Viroefe39652012-04-13 00:32:14 -04001054static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
NeilBrown43b0e7e2015-05-03 09:16:53 +10001056 const char *name, int namlen, u64 ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
1058 struct svc_export *exp;
1059 struct dentry *dparent, *dchild;
Al Viroefe39652012-04-13 00:32:14 -04001060 __be32 rv = nfserr_noent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 dparent = cd->fh.fh_dentry;
1063 exp = cd->fh.fh_export;
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (isdotent(name, namlen)) {
1066 if (namlen == 2) {
1067 dchild = dget_parent(dparent);
J. Bruce Fields51b2ee72021-01-11 16:01:29 -05001068 /*
1069 * Don't return filehandle for ".." if we're at
1070 * the filesystem or export root:
1071 */
Al Viroefe39652012-04-13 00:32:14 -04001072 if (dchild == dparent)
1073 goto out;
J. Bruce Fields51b2ee72021-01-11 16:01:29 -05001074 if (dparent == exp->ex_path.dentry)
1075 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 } else
1077 dchild = dget(dparent);
1078 } else
Al Viro6c2d47982019-10-31 01:21:58 -04001079 dchild = lookup_positive_unlocked(name, dparent, namlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (IS_ERR(dchild))
Al Viroefe39652012-04-13 00:32:14 -04001081 return rv;
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001082 if (d_mountpoint(dchild))
1083 goto out;
NeilBrown43b0e7e2015-05-03 09:16:53 +10001084 if (dchild->d_inode->i_ino != ino)
1085 goto out;
Al Viroefe39652012-04-13 00:32:14 -04001086 rv = fh_compose(fhp, exp, dchild, &cd->fh);
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001087out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 dput(dchild);
1089 return rv;
1090}
1091
Chuck Levera161e6c2020-11-10 09:57:14 -05001092/**
1093 * nfs3svc_encode_cookie3 - Encode a directory offset cookie
1094 * @resp: readdir result context
1095 * @offset: offset cookie to encode
1096 *
Chuck Lever7f87fc22020-10-22 19:46:58 -04001097 * The buffer space for the offset cookie has already been reserved
1098 * by svcxdr_encode_entry3_common().
Chuck Levera161e6c2020-11-10 09:57:14 -05001099 */
1100void nfs3svc_encode_cookie3(struct nfsd3_readdirres *resp, u64 offset)
1101{
Chuck Lever7f87fc22020-10-22 19:46:58 -04001102 __be64 cookie = cpu_to_be64(offset);
Chuck Levera161e6c2020-11-10 09:57:14 -05001103
Chuck Lever7f87fc22020-10-22 19:46:58 -04001104 if (!resp->cookie_offset)
1105 return;
1106 write_bytes_to_xdr_buf(&resp->dirlist, resp->cookie_offset, &cookie,
1107 sizeof(cookie));
1108 resp->cookie_offset = 0;
Chuck Levera161e6c2020-11-10 09:57:14 -05001109}
1110
Chuck Lever8b704492020-11-06 13:08:45 -05001111static bool
Chuck Lever7f87fc22020-10-22 19:46:58 -04001112svcxdr_encode_entry3_common(struct nfsd3_readdirres *resp, const char *name,
1113 int namlen, loff_t offset, u64 ino)
1114{
1115 struct xdr_buf *dirlist = &resp->dirlist;
1116 struct xdr_stream *xdr = &resp->xdr;
1117
1118 if (xdr_stream_encode_item_present(xdr) < 0)
1119 return false;
1120 /* fileid */
1121 if (xdr_stream_encode_u64(xdr, ino) < 0)
1122 return false;
1123 /* name */
1124 if (xdr_stream_encode_opaque(xdr, name, min(namlen, NFS3_MAXNAMLEN)) < 0)
1125 return false;
1126 /* cookie */
1127 resp->cookie_offset = dirlist->len;
1128 if (xdr_stream_encode_u64(xdr, NFS_OFFSET_MAX) < 0)
1129 return false;
1130
1131 return true;
1132}
1133
1134/**
1135 * nfs3svc_encode_entry3 - encode one NFSv3 READDIR 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_entry3(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
1167 xdr_commit_encode(&resp->xdr);
1168 resp->common.err = nfs_ok;
1169 return 0;
1170
1171out_toosmall:
1172 resp->cookie_offset = 0;
1173 resp->common.err = nfserr_toosmall;
1174 resp->dirlist.len = starting_length;
1175 return -EINVAL;
1176}
1177
1178static bool
1179svcxdr_encode_entry3_plus(struct nfsd3_readdirres *resp, const char *name,
1180 int namlen, u64 ino)
1181{
1182 struct xdr_stream *xdr = &resp->xdr;
1183 struct svc_fh *fhp = &resp->scratch;
1184 bool result;
1185
1186 result = false;
1187 fh_init(fhp, NFS3_FHSIZE);
1188 if (compose_entry_fh(resp, fhp, name, namlen, ino) != nfs_ok)
1189 goto out_noattrs;
1190
1191 if (!svcxdr_encode_post_op_attr(resp->rqstp, xdr, fhp))
1192 goto out;
1193 if (!svcxdr_encode_post_op_fh3(xdr, fhp))
1194 goto out;
1195 result = true;
1196
1197out:
1198 fh_put(fhp);
1199 return result;
1200
1201out_noattrs:
1202 if (xdr_stream_encode_item_absent(xdr) < 0)
1203 return false;
1204 if (xdr_stream_encode_item_absent(xdr) < 0)
1205 return false;
1206 return true;
1207}
1208
1209/**
1210 * nfs3svc_encode_entryplus3 - encode one NFSv3 READDIRPLUS entry
1211 * @data: directory context
1212 * @name: name of the object to be encoded
1213 * @namlen: length of that name, in bytes
1214 * @offset: the offset of the previous entry
1215 * @ino: the fileid of this entry
1216 * @d_type: unused
1217 *
1218 * Return values:
1219 * %0: Entry was successfully encoded.
1220 * %-EINVAL: An encoding problem occured, secondary status code in resp->common.err
1221 *
1222 * On exit, the following fields are updated:
1223 * - resp->xdr
1224 * - resp->common.err
1225 * - resp->cookie_offset
1226 */
1227int nfs3svc_encode_entryplus3(void *data, const char *name, int namlen,
1228 loff_t offset, u64 ino, unsigned int d_type)
1229{
1230 struct readdir_cd *ccd = data;
1231 struct nfsd3_readdirres *resp = container_of(ccd,
1232 struct nfsd3_readdirres,
1233 common);
1234 unsigned int starting_length = resp->dirlist.len;
1235
1236 /* The offset cookie for the previous entry */
1237 nfs3svc_encode_cookie3(resp, offset);
1238
1239 if (!svcxdr_encode_entry3_common(resp, name, namlen, offset, ino))
1240 goto out_toosmall;
1241 if (!svcxdr_encode_entry3_plus(resp, name, namlen, ino))
1242 goto out_toosmall;
1243
1244 xdr_commit_encode(&resp->xdr);
1245 resp->common.err = nfs_ok;
1246 return 0;
1247
1248out_toosmall:
1249 resp->cookie_offset = 0;
1250 resp->common.err = nfserr_toosmall;
1251 resp->dirlist.len = starting_length;
1252 return -EINVAL;
1253}
1254
1255static bool
Chuck Lever8b704492020-11-06 13:08:45 -05001256svcxdr_encode_fsstat3resok(struct xdr_stream *xdr,
1257 const struct nfsd3_fsstatres *resp)
1258{
1259 const struct kstatfs *s = &resp->stats;
1260 u64 bs = s->f_bsize;
1261 __be32 *p;
1262
1263 p = xdr_reserve_space(xdr, XDR_UNIT * 13);
1264 if (!p)
1265 return false;
1266 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1267 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1268 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1269 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1270 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1271 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1272 *p = cpu_to_be32(resp->invarsec); /* mean unchanged time */
1273
1274 return true;
1275}
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277/* FSSTAT */
Chuck Lever130e2052021-10-13 10:41:13 -04001278bool
Chuck Leverfda49442021-10-13 10:41:06 -04001279nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001281 struct nfsd3_fsstatres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Chuck Lever8b704492020-11-06 13:08:45 -05001283 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
Chuck Lever130e2052021-10-13 10:41:13 -04001284 return false;
Chuck Lever8b704492020-11-06 13:08:45 -05001285 switch (resp->status) {
1286 case nfs_ok:
1287 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
Chuck Lever130e2052021-10-13 10:41:13 -04001288 return false;
Chuck Lever8b704492020-11-06 13:08:45 -05001289 if (!svcxdr_encode_fsstat3resok(xdr, resp))
Chuck Lever130e2052021-10-13 10:41:13 -04001290 return false;
Chuck Lever8b704492020-11-06 13:08:45 -05001291 break;
1292 default:
1293 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
Chuck Lever130e2052021-10-13 10:41:13 -04001294 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
Chuck Lever8b704492020-11-06 13:08:45 -05001296
Chuck Lever130e2052021-10-13 10:41:13 -04001297 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
1299
Chuck Lever0a139d12020-10-22 13:42:13 -04001300static bool
1301svcxdr_encode_fsinfo3resok(struct xdr_stream *xdr,
1302 const struct nfsd3_fsinfores *resp)
1303{
1304 __be32 *p;
1305
1306 p = xdr_reserve_space(xdr, XDR_UNIT * 12);
1307 if (!p)
1308 return false;
1309 *p++ = cpu_to_be32(resp->f_rtmax);
1310 *p++ = cpu_to_be32(resp->f_rtpref);
1311 *p++ = cpu_to_be32(resp->f_rtmult);
1312 *p++ = cpu_to_be32(resp->f_wtmax);
1313 *p++ = cpu_to_be32(resp->f_wtpref);
1314 *p++ = cpu_to_be32(resp->f_wtmult);
1315 *p++ = cpu_to_be32(resp->f_dtpref);
1316 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1317 p = encode_nfstime3(p, &nfs3svc_time_delta);
1318 *p = cpu_to_be32(resp->f_properties);
1319
1320 return true;
1321}
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323/* FSINFO */
Chuck Lever130e2052021-10-13 10:41:13 -04001324bool
Chuck Leverfda49442021-10-13 10:41:06 -04001325nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001327 struct nfsd3_fsinfores *resp = rqstp->rq_resp;
1328
Chuck Lever0a139d12020-10-22 13:42:13 -04001329 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
Chuck Lever130e2052021-10-13 10:41:13 -04001330 return false;
Chuck Lever0a139d12020-10-22 13:42:13 -04001331 switch (resp->status) {
1332 case nfs_ok:
1333 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
Chuck Lever130e2052021-10-13 10:41:13 -04001334 return false;
Chuck Lever0a139d12020-10-22 13:42:13 -04001335 if (!svcxdr_encode_fsinfo3resok(xdr, resp))
Chuck Lever130e2052021-10-13 10:41:13 -04001336 return false;
Chuck Lever0a139d12020-10-22 13:42:13 -04001337 break;
1338 default:
1339 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
Chuck Lever130e2052021-10-13 10:41:13 -04001340 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342
Chuck Lever130e2052021-10-13 10:41:13 -04001343 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
Chuck Leverded04a52020-11-06 13:15:09 -05001346static bool
1347svcxdr_encode_pathconf3resok(struct xdr_stream *xdr,
1348 const struct nfsd3_pathconfres *resp)
1349{
1350 __be32 *p;
1351
1352 p = xdr_reserve_space(xdr, XDR_UNIT * 6);
1353 if (!p)
1354 return false;
1355 *p++ = cpu_to_be32(resp->p_link_max);
1356 *p++ = cpu_to_be32(resp->p_name_max);
1357 p = xdr_encode_bool(p, resp->p_no_trunc);
1358 p = xdr_encode_bool(p, resp->p_chown_restricted);
1359 p = xdr_encode_bool(p, resp->p_case_insensitive);
1360 xdr_encode_bool(p, resp->p_case_preserving);
1361
1362 return true;
1363}
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365/* PATHCONF */
Chuck Lever130e2052021-10-13 10:41:13 -04001366bool
Chuck Leverfda49442021-10-13 10:41:06 -04001367nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001369 struct nfsd3_pathconfres *resp = rqstp->rq_resp;
1370
Chuck Leverded04a52020-11-06 13:15:09 -05001371 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
Chuck Lever130e2052021-10-13 10:41:13 -04001372 return false;
Chuck Leverded04a52020-11-06 13:15:09 -05001373 switch (resp->status) {
1374 case nfs_ok:
1375 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
Chuck Lever130e2052021-10-13 10:41:13 -04001376 return false;
Chuck Leverded04a52020-11-06 13:15:09 -05001377 if (!svcxdr_encode_pathconf3resok(xdr, resp))
Chuck Lever130e2052021-10-13 10:41:13 -04001378 return false;
Chuck Leverded04a52020-11-06 13:15:09 -05001379 break;
1380 default:
1381 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
Chuck Lever130e2052021-10-13 10:41:13 -04001382 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
1384
Chuck Lever130e2052021-10-13 10:41:13 -04001385 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
1388/* COMMIT */
Chuck Lever130e2052021-10-13 10:41:13 -04001389bool
Chuck Leverfda49442021-10-13 10:41:06 -04001390nfs3svc_encode_commitres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001392 struct nfsd3_commitres *resp = rqstp->rq_resp;
Stanislav Kinsburskyb9c0ef82012-12-06 14:23:19 +03001393
Chuck Lever5ef28262020-10-22 15:35:46 -04001394 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
Chuck Lever130e2052021-10-13 10:41:13 -04001395 return false;
Chuck Lever5ef28262020-10-22 15:35:46 -04001396 switch (resp->status) {
1397 case nfs_ok:
1398 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
Chuck Lever130e2052021-10-13 10:41:13 -04001399 return false;
Chuck Lever5ef28262020-10-22 15:35:46 -04001400 if (!svcxdr_encode_writeverf3(xdr, resp->verf))
Chuck Lever130e2052021-10-13 10:41:13 -04001401 return false;
Chuck Lever5ef28262020-10-22 15:35:46 -04001402 break;
1403 default:
1404 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
Chuck Lever130e2052021-10-13 10:41:13 -04001405 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
Chuck Lever5ef28262020-10-22 15:35:46 -04001407
Chuck Lever130e2052021-10-13 10:41:13 -04001408 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409}
1410
1411/*
1412 * XDR release functions
1413 */
Christoph Hellwig85374882017-05-08 18:48:24 +02001414void
1415nfs3svc_release_fhandle(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
Christoph Hellwig85374882017-05-08 18:48:24 +02001417 struct nfsd3_attrstat *resp = rqstp->rq_resp;
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 fh_put(&resp->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
Christoph Hellwig85374882017-05-08 18:48:24 +02001422void
1423nfs3svc_release_fhandle2(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
Christoph Hellwig85374882017-05-08 18:48:24 +02001425 struct nfsd3_fhandle_pair *resp = rqstp->rq_resp;
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 fh_put(&resp->fh1);
1428 fh_put(&resp->fh2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}