blob: 0a5ebc52e6a9ccdc5aaf0beffab7ced35cb3adac [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;
95 memcpy(&fhp->fh_handle.fh_base, p, size);
96
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;
134 memcpy(p, &fhp->fh_handle.fh_base, size);
135
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
559int
Chuck Lever95753632020-10-20 14:30:02 -0400560nfs3svc_decode_fhandleargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Chuck Lever95753632020-10-20 14:30:02 -0400562 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200563 struct nfsd_fhandle *args = rqstp->rq_argp;
564
Chuck Lever95753632020-10-20 14:30:02 -0400565 return svcxdr_decode_nfs_fh3(xdr, &args->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
568int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200569nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Chuck Lever9cde9362020-10-20 15:48:22 -0400571 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200572 struct nfsd3_sattrargs *args = rqstp->rq_argp;
573
Chuck Lever9cde9362020-10-20 15:48:22 -0400574 return svcxdr_decode_nfs_fh3(xdr, &args->fh) &&
575 svcxdr_decode_sattr3(rqstp, xdr, &args->attrs) &&
576 svcxdr_decode_sattrguard3(xdr, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
579int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200580nfs3svc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Chuck Lever54d1d432020-10-20 15:42:33 -0400582 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200583 struct nfsd3_diropargs *args = rqstp->rq_argp;
584
Chuck Lever54d1d432020-10-20 15:42:33 -0400585 return svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
588int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200589nfs3svc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Chuck Lever3b921a22020-10-20 14:32:04 -0400591 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200592 struct nfsd3_accessargs *args = rqstp->rq_argp;
593
Chuck Lever3b921a22020-10-20 14:32:04 -0400594 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return 0;
Chuck Lever3b921a22020-10-20 14:32:04 -0400596 if (xdr_stream_decode_u32(xdr, &args->access) < 0)
597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Chuck Lever3b921a22020-10-20 14:32:04 -0400599 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
602int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200603nfs3svc_decode_readargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Chuck Leverbe63bd22020-10-20 14:34:40 -0400605 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200606 struct nfsd3_readargs *args = rqstp->rq_argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Chuck Leverbe63bd22020-10-20 14:34:40 -0400608 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return 0;
Chuck Leverbe63bd22020-10-20 14:34:40 -0400610 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
611 return 0;
612 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
613 return 0;
J. Bruce Fields9512a162017-05-16 15:57:42 -0400614
Chuck Leverbe63bd22020-10-20 14:34:40 -0400615 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
618int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200619nfs3svc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Chuck Leverc43b2f22020-10-22 11:14:55 -0400621 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200622 struct nfsd3_writeargs *args = rqstp->rq_argp;
Greg Banks7adae482006-10-04 02:15:47 -0700623 u32 max_blocksize = svc_max_payload(rqstp);
J. Bruce Fieldsdb44bac2017-04-25 16:21:34 -0400624 struct kvec *head = rqstp->rq_arg.head;
625 struct kvec *tail = rqstp->rq_arg.tail;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400626 size_t remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Chuck Leverc43b2f22020-10-22 11:14:55 -0400628 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return 0;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400630 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
631 return 0;
632 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
633 return 0;
634 if (xdr_stream_decode_u32(xdr, &args->stable) < 0)
635 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Chuck Leverc43b2f22020-10-22 11:14:55 -0400637 /* opaque data */
638 if (xdr_stream_decode_u32(xdr, &args->len) < 0)
J. Bruce Fields13bf9fb2017-04-21 15:26:30 -0400639 return 0;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400640
641 /* request sanity */
Peter Staubachf34b95682007-05-09 02:34:48 -0700642 if (args->count != args->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return 0;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400644 remaining = head->iov_len + rqstp->rq_arg.page_len + tail->iov_len;
645 remaining -= xdr_stream_pos(xdr);
646 if (remaining < xdr_align_size(args->len))
Peter Staubachf34b95682007-05-09 02:34:48 -0700647 return 0;
Peter Staubachf34b95682007-05-09 02:34:48 -0700648 if (args->count > max_blocksize) {
649 args->count = max_blocksize;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400650 args->len = max_blocksize;
Peter Staubachf34b95682007-05-09 02:34:48 -0700651 }
Chuck Lever8154ef22018-03-27 10:54:07 -0400652
Chuck Leverc43b2f22020-10-22 11:14:55 -0400653 args->first.iov_base = xdr->p;
654 args->first.iov_len = head->iov_len - xdr_stream_pos(xdr);
655
Peter Staubachf34b95682007-05-09 02:34:48 -0700656 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
659int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200660nfs3svc_decode_createargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Chuck Lever6b3a1192020-10-20 15:56:11 -0400662 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200663 struct nfsd3_createargs *args = rqstp->rq_argp;
664
Chuck Lever6b3a1192020-10-20 15:56:11 -0400665 if (!svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return 0;
Chuck Lever6b3a1192020-10-20 15:56:11 -0400667 if (xdr_stream_decode_u32(xdr, &args->createmode) < 0)
668 return 0;
669 switch (args->createmode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 case NFS3_CREATE_UNCHECKED:
671 case NFS3_CREATE_GUARDED:
Chuck Lever6b3a1192020-10-20 15:56:11 -0400672 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 case NFS3_CREATE_EXCLUSIVE:
Chuck Lever6b3a1192020-10-20 15:56:11 -0400674 args->verf = xdr_inline_decode(xdr, NFS3_CREATEVERFSIZE);
675 if (!args->verf)
676 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 break;
678 default:
679 return 0;
680 }
Chuck Lever6b3a1192020-10-20 15:56:11 -0400681 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
Christoph Hellwig026fec72017-05-08 19:01:48 +0200683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200685nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Chuck Lever83374c22020-10-20 17:02:16 -0400687 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200688 struct nfsd3_createargs *args = rqstp->rq_argp;
689
Chuck Lever83374c22020-10-20 17:02:16 -0400690 return svcxdr_decode_diropargs3(xdr, &args->fh,
691 &args->name, &args->len) &&
692 svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
695int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200696nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Chuck Leverda392012020-10-20 16:01:16 -0400698 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200699 struct nfsd3_symlinkargs *args = rqstp->rq_argp;
Chuck Leverda392012020-10-20 16:01:16 -0400700 struct kvec *head = rqstp->rq_arg.head;
701 struct kvec *tail = rqstp->rq_arg.tail;
702 size_t remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Chuck Leverda392012020-10-20 16:01:16 -0400704 if (!svcxdr_decode_diropargs3(xdr, &args->ffh, &args->fname, &args->flen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return 0;
Chuck Leverda392012020-10-20 16:01:16 -0400706 if (!svcxdr_decode_sattr3(rqstp, xdr, &args->attrs))
Chuck Lever38a70312018-03-27 10:54:21 -0400707 return 0;
Chuck Leverda392012020-10-20 16:01:16 -0400708 if (xdr_stream_decode_u32(xdr, &args->tlen) < 0)
709 return 0;
710
711 /* request sanity */
712 remaining = head->iov_len + rqstp->rq_arg.page_len + tail->iov_len;
713 remaining -= xdr_stream_pos(xdr);
714 if (remaining < xdr_align_size(args->tlen))
715 return 0;
716
717 args->first.iov_base = xdr->p;
718 args->first.iov_len = head->iov_len - xdr_stream_pos(xdr);
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return 1;
721}
722
723int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200724nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Chuck Leverf8a38e22020-10-20 17:04:03 -0400726 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200727 struct nfsd3_mknodargs *args = rqstp->rq_argp;
728
Chuck Leverf8a38e22020-10-20 17:04:03 -0400729 if (!svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return 0;
Chuck Leverf8a38e22020-10-20 17:04:03 -0400731 if (xdr_stream_decode_u32(xdr, &args->ftype) < 0)
732 return 0;
733 switch (args->ftype) {
734 case NF3CHR:
735 case NF3BLK:
736 return svcxdr_decode_devicedata3(rqstp, xdr, args);
737 case NF3SOCK:
738 case NF3FIFO:
739 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
740 case NF3REG:
741 case NF3DIR:
742 case NF3LNK:
743 /* Valid XDR but illegal file types */
744 break;
745 default:
746 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748
Chuck Leverf8a38e22020-10-20 17:04:03 -0400749 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
752int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200753nfs3svc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Chuck Leverd181e0a2020-10-20 15:44:12 -0400755 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200756 struct nfsd3_renameargs *args = rqstp->rq_argp;
757
Chuck Leverd181e0a2020-10-20 15:44:12 -0400758 return svcxdr_decode_diropargs3(xdr, &args->ffh,
759 &args->fname, &args->flen) &&
760 svcxdr_decode_diropargs3(xdr, &args->tfh,
761 &args->tname, &args->tlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
764int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200765nfs3svc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Chuck Leverefaa1e72020-10-19 13:26:32 -0400767 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200768 struct nfsd3_linkargs *args = rqstp->rq_argp;
769
Chuck Leverefaa1e72020-10-19 13:26:32 -0400770 return svcxdr_decode_nfs_fh3(xdr, &args->ffh) &&
771 svcxdr_decode_diropargs3(xdr, &args->tfh,
772 &args->tname, &args->tlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
775int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200776nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400778 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200779 struct nfsd3_readdirargs *args = rqstp->rq_argp;
NeilBrownf875a792019-03-07 09:49:46 +1100780
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400781 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return 0;
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400783 if (xdr_stream_decode_u64(xdr, &args->cookie) < 0)
784 return 0;
785 args->verf = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE);
786 if (!args->verf)
787 return 0;
788 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
789 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400791 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
794int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200795nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400797 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200798 struct nfsd3_readdirargs *args = rqstp->rq_argp;
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400799 u32 dircount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400801 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return 0;
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400803 if (xdr_stream_decode_u64(xdr, &args->cookie) < 0)
804 return 0;
805 args->verf = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE);
806 if (!args->verf)
807 return 0;
808 /* dircount is ignored */
809 if (xdr_stream_decode_u32(xdr, &dircount) < 0)
810 return 0;
811 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
812 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400814 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
817int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200818nfs3svc_decode_commitargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Chuck Leverc8d26a02020-10-20 14:41:56 -0400820 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200821 struct nfsd3_commitargs *args = rqstp->rq_argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Chuck Leverc8d26a02020-10-20 14:41:56 -0400823 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
824 return 0;
825 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
826 return 0;
827 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
828 return 0;
829
830 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
833/*
834 * XDR encode functions
835 */
Chuck Levercc028a12020-10-02 15:52:44 -0400836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837/* GETATTR */
838int
Chuck Lever2c42f802020-10-21 11:58:41 -0400839nfs3svc_encode_getattrres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Chuck Lever2c42f802020-10-21 11:58:41 -0400841 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200842 struct nfsd3_attrstat *resp = rqstp->rq_resp;
843
Chuck Lever2c42f802020-10-21 11:58:41 -0400844 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
845 return 0;
846 switch (resp->status) {
847 case nfs_ok:
848 lease_get_mtime(d_inode(resp->fh.fh_dentry), &resp->stat.mtime);
849 if (!svcxdr_encode_fattr3(rqstp, xdr, &resp->fh, &resp->stat))
850 return 0;
851 break;
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400852 }
Chuck Lever2c42f802020-10-21 11:58:41 -0400853
854 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
857/* SETATTR, REMOVE, RMDIR */
858int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200859nfs3svc_encode_wccstat(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Chuck Lever70f8e832020-10-22 15:12:38 -0400861 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200862 struct nfsd3_attrstat *resp = rqstp->rq_resp;
863
Chuck Lever70f8e832020-10-22 15:12:38 -0400864 return svcxdr_encode_nfsstat3(xdr, resp->status) &&
865 svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
868/* LOOKUP */
Chuck Lever5cf35332020-10-22 14:46:58 -0400869int nfs3svc_encode_lookupres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Chuck Lever5cf35332020-10-22 14:46:58 -0400871 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200872 struct nfsd3_diropres *resp = rqstp->rq_resp;
873
Chuck Lever5cf35332020-10-22 14:46:58 -0400874 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
875 return 0;
876 switch (resp->status) {
877 case nfs_ok:
878 if (!svcxdr_encode_nfs_fh3(xdr, &resp->fh))
879 return 0;
880 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
881 return 0;
882 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->dirfh))
883 return 0;
884 break;
885 default:
886 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->dirfh))
887 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
Chuck Lever5cf35332020-10-22 14:46:58 -0400889
890 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
893/* ACCESS */
894int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200895nfs3svc_encode_accessres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Chuck Lever907c3822020-10-22 13:56:58 -0400897 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200898 struct nfsd3_accessres *resp = rqstp->rq_resp;
899
Chuck Lever907c3822020-10-22 13:56:58 -0400900 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
901 return 0;
902 switch (resp->status) {
903 case nfs_ok:
904 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
905 return 0;
906 if (xdr_stream_encode_u32(xdr, resp->access) < 0)
907 return 0;
908 break;
909 default:
910 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
911 return 0;
912 }
913
914 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
917/* READLINK */
918int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200919nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Chuck Lever9a9c8922020-10-22 15:18:40 -0400921 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200922 struct nfsd3_readlinkres *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 Lever9a9c8922020-10-22 15:18:40 -0400925 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
926 return 0;
927 switch (resp->status) {
928 case nfs_ok:
929 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
Chuck Lever76e54922020-11-05 10:24:19 -0500930 return 0;
Chuck Lever9a9c8922020-10-22 15:18:40 -0400931 if (xdr_stream_encode_u32(xdr, resp->len) < 0)
932 return 0;
933 xdr_write_pages(xdr, resp->pages, 0, resp->len);
934 if (svc_encode_result_payload(rqstp, head->iov_len, resp->len) < 0)
935 return 0;
936 break;
937 default:
938 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
939 return 0;
940 }
941
942 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
945/* READ */
946int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200947nfs3svc_encode_readres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Chuck Levercc9bcda2020-10-22 15:23:50 -0400949 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200950 struct nfsd3_readres *resp = rqstp->rq_resp;
Chuck Lever76e54922020-11-05 10:24:19 -0500951 struct kvec *head = rqstp->rq_res.head;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200952
Chuck Levercc9bcda2020-10-22 15:23:50 -0400953 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
954 return 0;
955 switch (resp->status) {
956 case nfs_ok:
957 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
Chuck Lever76e54922020-11-05 10:24:19 -0500958 return 0;
Chuck Levercc9bcda2020-10-22 15:23:50 -0400959 if (xdr_stream_encode_u32(xdr, resp->count) < 0)
960 return 0;
961 if (xdr_stream_encode_bool(xdr, resp->eof) < 0)
962 return 0;
963 if (xdr_stream_encode_u32(xdr, resp->count) < 0)
964 return 0;
965 xdr_write_pages(xdr, resp->pages, rqstp->rq_res.page_base,
966 resp->count);
967 if (svc_encode_result_payload(rqstp, head->iov_len, resp->count) < 0)
968 return 0;
969 break;
970 default:
971 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
972 return 0;
973 }
974
975 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
978/* WRITE */
979int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200980nfs3svc_encode_writeres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Chuck Leverecb7a082020-10-22 15:26:31 -0400982 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200983 struct nfsd3_writeres *resp = rqstp->rq_resp;
Stanislav Kinsburskyb9c0ef82012-12-06 14:23:19 +0300984
Chuck Leverecb7a082020-10-22 15:26:31 -0400985 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
986 return 0;
987 switch (resp->status) {
988 case nfs_ok:
989 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
990 return 0;
991 if (xdr_stream_encode_u32(xdr, resp->count) < 0)
992 return 0;
993 if (xdr_stream_encode_u32(xdr, resp->committed) < 0)
994 return 0;
995 if (!svcxdr_encode_writeverf3(xdr, resp->verf))
996 return 0;
997 break;
998 default:
999 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
1000 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
Chuck Leverecb7a082020-10-22 15:26:31 -04001002
1003 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005
1006/* CREATE, MKDIR, SYMLINK, MKNOD */
1007int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001008nfs3svc_encode_createres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Chuck Lever78315b32020-10-22 15:27:23 -04001010 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001011 struct nfsd3_diropres *resp = rqstp->rq_resp;
1012
Chuck Lever78315b32020-10-22 15:27:23 -04001013 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1014 return 0;
1015 switch (resp->status) {
1016 case nfs_ok:
1017 if (!svcxdr_encode_post_op_fh3(xdr, &resp->fh))
1018 return 0;
1019 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
1020 return 0;
1021 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->dirfh))
1022 return 0;
1023 break;
1024 default:
1025 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->dirfh))
1026 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
Chuck Lever78315b32020-10-22 15:27:23 -04001028
1029 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
1032/* RENAME */
1033int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001034nfs3svc_encode_renameres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
Chuck Lever89d79e92020-10-22 15:33:05 -04001036 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001037 struct nfsd3_renameres *resp = rqstp->rq_resp;
1038
Chuck Lever89d79e92020-10-22 15:33:05 -04001039 return svcxdr_encode_nfsstat3(xdr, resp->status) &&
1040 svcxdr_encode_wcc_data(rqstp, xdr, &resp->ffh) &&
1041 svcxdr_encode_wcc_data(rqstp, xdr, &resp->tfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
1044/* LINK */
1045int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001046nfs3svc_encode_linkres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Chuck Lever4d743802020-10-22 15:08:29 -04001048 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001049 struct nfsd3_linkres *resp = rqstp->rq_resp;
1050
Chuck Lever4d743802020-10-22 15:08:29 -04001051 return svcxdr_encode_nfsstat3(xdr, resp->status) &&
1052 svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh) &&
1053 svcxdr_encode_wcc_data(rqstp, xdr, &resp->tfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
1056/* READDIR */
1057int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001058nfs3svc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Chuck Levere4ccfe32020-10-22 19:31:48 -04001060 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001061 struct nfsd3_readdirres *resp = rqstp->rq_resp;
Chuck Lever7f87fc22020-10-22 19:46:58 -04001062 struct xdr_buf *dirlist = &resp->dirlist;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001063
Chuck Levere4ccfe32020-10-22 19:31:48 -04001064 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1065 return 0;
1066 switch (resp->status) {
1067 case nfs_ok:
1068 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
1069 return 0;
1070 if (!svcxdr_encode_cookieverf3(xdr, resp->verf))
1071 return 0;
Chuck Lever7f87fc22020-10-22 19:46:58 -04001072 xdr_write_pages(xdr, dirlist->pages, 0, dirlist->len);
Chuck Levere4ccfe32020-10-22 19:31:48 -04001073 /* no more entries */
1074 if (xdr_stream_encode_item_absent(xdr) < 0)
1075 return 0;
1076 if (xdr_stream_encode_bool(xdr, resp->common.err == nfserr_eof) < 0)
1077 return 0;
1078 break;
1079 default:
1080 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
1081 return 0;
1082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Chuck Levere4ccfe32020-10-22 19:31:48 -04001084 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
1086
Al Viroefe39652012-04-13 00:32:14 -04001087static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
NeilBrown43b0e7e2015-05-03 09:16:53 +10001089 const char *name, int namlen, u64 ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 struct svc_export *exp;
1092 struct dentry *dparent, *dchild;
Al Viroefe39652012-04-13 00:32:14 -04001093 __be32 rv = nfserr_noent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 dparent = cd->fh.fh_dentry;
1096 exp = cd->fh.fh_export;
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (isdotent(name, namlen)) {
1099 if (namlen == 2) {
1100 dchild = dget_parent(dparent);
J. Bruce Fields51b2ee72021-01-11 16:01:29 -05001101 /*
1102 * Don't return filehandle for ".." if we're at
1103 * the filesystem or export root:
1104 */
Al Viroefe39652012-04-13 00:32:14 -04001105 if (dchild == dparent)
1106 goto out;
J. Bruce Fields51b2ee72021-01-11 16:01:29 -05001107 if (dparent == exp->ex_path.dentry)
1108 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 } else
1110 dchild = dget(dparent);
1111 } else
Al Viro6c2d47982019-10-31 01:21:58 -04001112 dchild = lookup_positive_unlocked(name, dparent, namlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (IS_ERR(dchild))
Al Viroefe39652012-04-13 00:32:14 -04001114 return rv;
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001115 if (d_mountpoint(dchild))
1116 goto out;
NeilBrown43b0e7e2015-05-03 09:16:53 +10001117 if (dchild->d_inode->i_ino != ino)
1118 goto out;
Al Viroefe39652012-04-13 00:32:14 -04001119 rv = fh_compose(fhp, exp, dchild, &cd->fh);
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001120out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 dput(dchild);
1122 return rv;
1123}
1124
Chuck Levera161e6c2020-11-10 09:57:14 -05001125/**
1126 * nfs3svc_encode_cookie3 - Encode a directory offset cookie
1127 * @resp: readdir result context
1128 * @offset: offset cookie to encode
1129 *
Chuck Lever7f87fc22020-10-22 19:46:58 -04001130 * The buffer space for the offset cookie has already been reserved
1131 * by svcxdr_encode_entry3_common().
Chuck Levera161e6c2020-11-10 09:57:14 -05001132 */
1133void nfs3svc_encode_cookie3(struct nfsd3_readdirres *resp, u64 offset)
1134{
Chuck Lever7f87fc22020-10-22 19:46:58 -04001135 __be64 cookie = cpu_to_be64(offset);
Chuck Levera161e6c2020-11-10 09:57:14 -05001136
Chuck Lever7f87fc22020-10-22 19:46:58 -04001137 if (!resp->cookie_offset)
1138 return;
1139 write_bytes_to_xdr_buf(&resp->dirlist, resp->cookie_offset, &cookie,
1140 sizeof(cookie));
1141 resp->cookie_offset = 0;
Chuck Levera161e6c2020-11-10 09:57:14 -05001142}
1143
Chuck Lever8b704492020-11-06 13:08:45 -05001144static bool
Chuck Lever7f87fc22020-10-22 19:46:58 -04001145svcxdr_encode_entry3_common(struct nfsd3_readdirres *resp, const char *name,
1146 int namlen, loff_t offset, u64 ino)
1147{
1148 struct xdr_buf *dirlist = &resp->dirlist;
1149 struct xdr_stream *xdr = &resp->xdr;
1150
1151 if (xdr_stream_encode_item_present(xdr) < 0)
1152 return false;
1153 /* fileid */
1154 if (xdr_stream_encode_u64(xdr, ino) < 0)
1155 return false;
1156 /* name */
1157 if (xdr_stream_encode_opaque(xdr, name, min(namlen, NFS3_MAXNAMLEN)) < 0)
1158 return false;
1159 /* cookie */
1160 resp->cookie_offset = dirlist->len;
1161 if (xdr_stream_encode_u64(xdr, NFS_OFFSET_MAX) < 0)
1162 return false;
1163
1164 return true;
1165}
1166
1167/**
1168 * nfs3svc_encode_entry3 - encode one NFSv3 READDIR entry
1169 * @data: directory context
1170 * @name: name of the object to be encoded
1171 * @namlen: length of that name, in bytes
1172 * @offset: the offset of the previous entry
1173 * @ino: the fileid of this entry
1174 * @d_type: unused
1175 *
1176 * Return values:
1177 * %0: Entry was successfully encoded.
1178 * %-EINVAL: An encoding problem occured, secondary status code in resp->common.err
1179 *
1180 * On exit, the following fields are updated:
1181 * - resp->xdr
1182 * - resp->common.err
1183 * - resp->cookie_offset
1184 */
1185int nfs3svc_encode_entry3(void *data, const char *name, int namlen,
1186 loff_t offset, u64 ino, unsigned int d_type)
1187{
1188 struct readdir_cd *ccd = data;
1189 struct nfsd3_readdirres *resp = container_of(ccd,
1190 struct nfsd3_readdirres,
1191 common);
1192 unsigned int starting_length = resp->dirlist.len;
1193
1194 /* The offset cookie for the previous entry */
1195 nfs3svc_encode_cookie3(resp, offset);
1196
1197 if (!svcxdr_encode_entry3_common(resp, name, namlen, offset, ino))
1198 goto out_toosmall;
1199
1200 xdr_commit_encode(&resp->xdr);
1201 resp->common.err = nfs_ok;
1202 return 0;
1203
1204out_toosmall:
1205 resp->cookie_offset = 0;
1206 resp->common.err = nfserr_toosmall;
1207 resp->dirlist.len = starting_length;
1208 return -EINVAL;
1209}
1210
1211static bool
1212svcxdr_encode_entry3_plus(struct nfsd3_readdirres *resp, const char *name,
1213 int namlen, u64 ino)
1214{
1215 struct xdr_stream *xdr = &resp->xdr;
1216 struct svc_fh *fhp = &resp->scratch;
1217 bool result;
1218
1219 result = false;
1220 fh_init(fhp, NFS3_FHSIZE);
1221 if (compose_entry_fh(resp, fhp, name, namlen, ino) != nfs_ok)
1222 goto out_noattrs;
1223
1224 if (!svcxdr_encode_post_op_attr(resp->rqstp, xdr, fhp))
1225 goto out;
1226 if (!svcxdr_encode_post_op_fh3(xdr, fhp))
1227 goto out;
1228 result = true;
1229
1230out:
1231 fh_put(fhp);
1232 return result;
1233
1234out_noattrs:
1235 if (xdr_stream_encode_item_absent(xdr) < 0)
1236 return false;
1237 if (xdr_stream_encode_item_absent(xdr) < 0)
1238 return false;
1239 return true;
1240}
1241
1242/**
1243 * nfs3svc_encode_entryplus3 - encode one NFSv3 READDIRPLUS entry
1244 * @data: directory context
1245 * @name: name of the object to be encoded
1246 * @namlen: length of that name, in bytes
1247 * @offset: the offset of the previous entry
1248 * @ino: the fileid of this entry
1249 * @d_type: unused
1250 *
1251 * Return values:
1252 * %0: Entry was successfully encoded.
1253 * %-EINVAL: An encoding problem occured, secondary status code in resp->common.err
1254 *
1255 * On exit, the following fields are updated:
1256 * - resp->xdr
1257 * - resp->common.err
1258 * - resp->cookie_offset
1259 */
1260int nfs3svc_encode_entryplus3(void *data, const char *name, int namlen,
1261 loff_t offset, u64 ino, unsigned int d_type)
1262{
1263 struct readdir_cd *ccd = data;
1264 struct nfsd3_readdirres *resp = container_of(ccd,
1265 struct nfsd3_readdirres,
1266 common);
1267 unsigned int starting_length = resp->dirlist.len;
1268
1269 /* The offset cookie for the previous entry */
1270 nfs3svc_encode_cookie3(resp, offset);
1271
1272 if (!svcxdr_encode_entry3_common(resp, name, namlen, offset, ino))
1273 goto out_toosmall;
1274 if (!svcxdr_encode_entry3_plus(resp, name, namlen, ino))
1275 goto out_toosmall;
1276
1277 xdr_commit_encode(&resp->xdr);
1278 resp->common.err = nfs_ok;
1279 return 0;
1280
1281out_toosmall:
1282 resp->cookie_offset = 0;
1283 resp->common.err = nfserr_toosmall;
1284 resp->dirlist.len = starting_length;
1285 return -EINVAL;
1286}
1287
1288static bool
Chuck Lever8b704492020-11-06 13:08:45 -05001289svcxdr_encode_fsstat3resok(struct xdr_stream *xdr,
1290 const struct nfsd3_fsstatres *resp)
1291{
1292 const struct kstatfs *s = &resp->stats;
1293 u64 bs = s->f_bsize;
1294 __be32 *p;
1295
1296 p = xdr_reserve_space(xdr, XDR_UNIT * 13);
1297 if (!p)
1298 return false;
1299 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1300 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1301 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1302 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1303 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1304 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1305 *p = cpu_to_be32(resp->invarsec); /* mean unchanged time */
1306
1307 return true;
1308}
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310/* FSSTAT */
1311int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001312nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
Chuck Lever8b704492020-11-06 13:08:45 -05001314 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001315 struct nfsd3_fsstatres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Chuck Lever8b704492020-11-06 13:08:45 -05001317 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1318 return 0;
1319 switch (resp->status) {
1320 case nfs_ok:
1321 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1322 return 0;
1323 if (!svcxdr_encode_fsstat3resok(xdr, resp))
1324 return 0;
1325 break;
1326 default:
1327 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1328 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 }
Chuck Lever8b704492020-11-06 13:08:45 -05001330
1331 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
Chuck Lever0a139d12020-10-22 13:42:13 -04001334static bool
1335svcxdr_encode_fsinfo3resok(struct xdr_stream *xdr,
1336 const struct nfsd3_fsinfores *resp)
1337{
1338 __be32 *p;
1339
1340 p = xdr_reserve_space(xdr, XDR_UNIT * 12);
1341 if (!p)
1342 return false;
1343 *p++ = cpu_to_be32(resp->f_rtmax);
1344 *p++ = cpu_to_be32(resp->f_rtpref);
1345 *p++ = cpu_to_be32(resp->f_rtmult);
1346 *p++ = cpu_to_be32(resp->f_wtmax);
1347 *p++ = cpu_to_be32(resp->f_wtpref);
1348 *p++ = cpu_to_be32(resp->f_wtmult);
1349 *p++ = cpu_to_be32(resp->f_dtpref);
1350 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1351 p = encode_nfstime3(p, &nfs3svc_time_delta);
1352 *p = cpu_to_be32(resp->f_properties);
1353
1354 return true;
1355}
1356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357/* FSINFO */
1358int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001359nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Chuck Lever0a139d12020-10-22 13:42:13 -04001361 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001362 struct nfsd3_fsinfores *resp = rqstp->rq_resp;
1363
Chuck Lever0a139d12020-10-22 13:42:13 -04001364 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1365 return 0;
1366 switch (resp->status) {
1367 case nfs_ok:
1368 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1369 return 0;
1370 if (!svcxdr_encode_fsinfo3resok(xdr, resp))
1371 return 0;
1372 break;
1373 default:
1374 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1375 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
1377
Chuck Lever0a139d12020-10-22 13:42:13 -04001378 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379}
1380
Chuck Leverded04a52020-11-06 13:15:09 -05001381static bool
1382svcxdr_encode_pathconf3resok(struct xdr_stream *xdr,
1383 const struct nfsd3_pathconfres *resp)
1384{
1385 __be32 *p;
1386
1387 p = xdr_reserve_space(xdr, XDR_UNIT * 6);
1388 if (!p)
1389 return false;
1390 *p++ = cpu_to_be32(resp->p_link_max);
1391 *p++ = cpu_to_be32(resp->p_name_max);
1392 p = xdr_encode_bool(p, resp->p_no_trunc);
1393 p = xdr_encode_bool(p, resp->p_chown_restricted);
1394 p = xdr_encode_bool(p, resp->p_case_insensitive);
1395 xdr_encode_bool(p, resp->p_case_preserving);
1396
1397 return true;
1398}
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400/* PATHCONF */
1401int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001402nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
Chuck Leverded04a52020-11-06 13:15:09 -05001404 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001405 struct nfsd3_pathconfres *resp = rqstp->rq_resp;
1406
Chuck Leverded04a52020-11-06 13:15:09 -05001407 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1408 return 0;
1409 switch (resp->status) {
1410 case nfs_ok:
1411 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1412 return 0;
1413 if (!svcxdr_encode_pathconf3resok(xdr, resp))
1414 return 0;
1415 break;
1416 default:
1417 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1418 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
1420
Chuck Leverded04a52020-11-06 13:15:09 -05001421 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
1424/* COMMIT */
1425int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001426nfs3svc_encode_commitres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
Chuck Lever5ef28262020-10-22 15:35:46 -04001428 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001429 struct nfsd3_commitres *resp = rqstp->rq_resp;
Stanislav Kinsburskyb9c0ef82012-12-06 14:23:19 +03001430
Chuck Lever5ef28262020-10-22 15:35:46 -04001431 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1432 return 0;
1433 switch (resp->status) {
1434 case nfs_ok:
1435 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
1436 return 0;
1437 if (!svcxdr_encode_writeverf3(xdr, resp->verf))
1438 return 0;
1439 break;
1440 default:
1441 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
1442 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
Chuck Lever5ef28262020-10-22 15:35:46 -04001444
1445 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
1448/*
1449 * XDR release functions
1450 */
Christoph Hellwig85374882017-05-08 18:48:24 +02001451void
1452nfs3svc_release_fhandle(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Christoph Hellwig85374882017-05-08 18:48:24 +02001454 struct nfsd3_attrstat *resp = rqstp->rq_resp;
1455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 fh_put(&resp->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
1458
Christoph Hellwig85374882017-05-08 18:48:24 +02001459void
1460nfs3svc_release_fhandle2(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
Christoph Hellwig85374882017-05-08 18:48:24 +02001462 struct nfsd3_fhandle_pair *resp = rqstp->rq_resp;
1463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 fh_put(&resp->fh1);
1465 fh_put(&resp->fh2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466}