blob: 941740a97f8f57345308832ca40f6148d3974839 [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
17#define NFSDDBG_FACILITY NFSDDBG_XDR
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20/*
Chuck Lever8b704492020-11-06 13:08:45 -050021 * Force construction of an empty post-op attr
22 */
23static const struct svc_fh nfs3svc_null_fh = {
24 .fh_no_wcc = true,
25};
26
27/*
Chuck Lever0a139d12020-10-22 13:42:13 -040028 * time_delta. {1, 0} means the server is accurate only
29 * to the nearest second.
30 */
31static const struct timespec64 nfs3svc_time_delta = {
32 .tv_sec = 1,
33 .tv_nsec = 0,
34};
35
36/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * Mapping of S_IF* types to NFS file types
38 */
Chuck Lever2c42f802020-10-21 11:58:41 -040039static const u32 nfs3_ftypes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 NF3NON, NF3FIFO, NF3CHR, NF3BAD,
41 NF3DIR, NF3BAD, NF3BLK, NF3BAD,
42 NF3REG, NF3BAD, NF3LNK, NF3BAD,
43 NF3SOCK, NF3BAD, NF3LNK, NF3BAD,
44};
45
Trond Myklebust27c438f2019-09-02 13:02:56 -040046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*
Chuck Lever95753632020-10-20 14:30:02 -040048 * Basic NFSv3 data types (RFC 1813 Sections 2.5 and 2.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 */
Chuck Lever95753632020-10-20 14:30:02 -040050
Adrian Bunk3ee6f612006-12-06 20:40:23 -080051static __be32 *
Arnd Bergmann92c5e462019-10-31 14:55:32 +010052encode_time3(__be32 *p, struct timespec64 *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
54 *p++ = htonl((u32) time->tv_sec); *p++ = htonl(time->tv_nsec);
55 return p;
56}
57
Chuck Lever2c42f802020-10-21 11:58:41 -040058static __be32 *
59encode_nfstime3(__be32 *p, const struct timespec64 *time)
60{
61 *p++ = cpu_to_be32((u32)time->tv_sec);
62 *p++ = cpu_to_be32(time->tv_nsec);
63
64 return p;
65}
66
Chuck Lever9cde9362020-10-20 15:48:22 -040067static bool
68svcxdr_decode_nfstime3(struct xdr_stream *xdr, struct timespec64 *timep)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Chuck Lever9cde9362020-10-20 15:48:22 -040070 __be32 *p;
71
72 p = xdr_inline_decode(xdr, XDR_UNIT * 2);
73 if (!p)
74 return false;
75 timep->tv_sec = be32_to_cpup(p++);
76 timep->tv_nsec = be32_to_cpup(p);
77
78 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
Chuck Lever05027ea2020-11-17 11:52:04 -050081/**
82 * svcxdr_decode_nfs_fh3 - Decode an NFSv3 file handle
83 * @xdr: XDR stream positioned at an undecoded NFSv3 FH
84 * @fhp: OUT: filled-in server file handle
85 *
86 * Return values:
87 * %false: The encoded file handle was not valid
88 * %true: @fhp has been initialized
89 */
90bool
Chuck Lever95753632020-10-20 14:30:02 -040091svcxdr_decode_nfs_fh3(struct xdr_stream *xdr, struct svc_fh *fhp)
92{
93 __be32 *p;
94 u32 size;
95
96 if (xdr_stream_decode_u32(xdr, &size) < 0)
97 return false;
98 if (size == 0 || size > NFS3_FHSIZE)
99 return false;
100 p = xdr_inline_decode(xdr, size);
101 if (!p)
102 return false;
103 fh_init(fhp, NFS3_FHSIZE);
104 fhp->fh_handle.fh_size = size;
105 memcpy(&fhp->fh_handle.fh_base, p, size);
106
107 return true;
108}
109
Chuck Lever20798df2020-11-18 16:11:42 -0500110/**
111 * svcxdr_encode_nfsstat3 - Encode an NFSv3 status code
112 * @xdr: XDR stream
113 * @status: status value to encode
114 *
115 * Return values:
116 * %false: Send buffer space was exhausted
117 * %true: Success
118 */
119bool
Chuck Lever2c42f802020-10-21 11:58:41 -0400120svcxdr_encode_nfsstat3(struct xdr_stream *xdr, __be32 status)
121{
122 __be32 *p;
123
124 p = xdr_reserve_space(xdr, sizeof(status));
125 if (!p)
126 return false;
127 *p = status;
128
129 return true;
130}
131
Chuck Lever5cf35332020-10-22 14:46:58 -0400132static bool
133svcxdr_encode_nfs_fh3(struct xdr_stream *xdr, const struct svc_fh *fhp)
134{
135 u32 size = fhp->fh_handle.fh_size;
136 __be32 *p;
137
138 p = xdr_reserve_space(xdr, XDR_UNIT + size);
139 if (!p)
140 return false;
141 *p++ = cpu_to_be32(size);
142 if (size)
143 p[XDR_QUADLEN(size) - 1] = 0;
144 memcpy(p, &fhp->fh_handle.fh_base, size);
145
146 return true;
147}
148
Chuck Lever78315b32020-10-22 15:27:23 -0400149static bool
150svcxdr_encode_post_op_fh3(struct xdr_stream *xdr, const struct svc_fh *fhp)
151{
152 if (xdr_stream_encode_item_present(xdr) < 0)
153 return false;
154 if (!svcxdr_encode_nfs_fh3(xdr, fhp))
155 return false;
156
157 return true;
158}
159
Chuck Lever54d1d432020-10-20 15:42:33 -0400160static bool
Chuck Levere4ccfe32020-10-22 19:31:48 -0400161svcxdr_encode_cookieverf3(struct xdr_stream *xdr, const __be32 *verf)
162{
163 __be32 *p;
164
165 p = xdr_reserve_space(xdr, NFS3_COOKIEVERFSIZE);
166 if (!p)
167 return false;
168 memcpy(p, verf, NFS3_COOKIEVERFSIZE);
169
170 return true;
171}
172
173static bool
Chuck Leverecb7a082020-10-22 15:26:31 -0400174svcxdr_encode_writeverf3(struct xdr_stream *xdr, const __be32 *verf)
175{
176 __be32 *p;
177
178 p = xdr_reserve_space(xdr, NFS3_WRITEVERFSIZE);
179 if (!p)
180 return false;
181 memcpy(p, verf, NFS3_WRITEVERFSIZE);
182
183 return true;
184}
185
186static bool
Chuck Lever54d1d432020-10-20 15:42:33 -0400187svcxdr_decode_filename3(struct xdr_stream *xdr, char **name, unsigned int *len)
188{
189 u32 size, i;
190 __be32 *p;
191 char *c;
192
193 if (xdr_stream_decode_u32(xdr, &size) < 0)
194 return false;
195 if (size == 0 || size > NFS3_MAXNAMLEN)
196 return false;
197 p = xdr_inline_decode(xdr, size);
198 if (!p)
199 return false;
200
201 *len = size;
202 *name = (char *)p;
203 for (i = 0, c = *name; i < size; i++, c++) {
204 if (*c == '\0' || *c == '/')
205 return false;
206 }
207
208 return true;
209}
210
211static bool
212svcxdr_decode_diropargs3(struct xdr_stream *xdr, struct svc_fh *fhp,
213 char **name, unsigned int *len)
214{
215 return svcxdr_decode_nfs_fh3(xdr, fhp) &&
216 svcxdr_decode_filename3(xdr, name, len);
217}
218
Chuck Lever9cde9362020-10-20 15:48:22 -0400219static bool
220svcxdr_decode_sattr3(struct svc_rqst *rqstp, struct xdr_stream *xdr,
221 struct iattr *iap)
222{
223 u32 set_it;
224
225 iap->ia_valid = 0;
226
227 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
228 return false;
229 if (set_it) {
230 u32 mode;
231
232 if (xdr_stream_decode_u32(xdr, &mode) < 0)
233 return false;
234 iap->ia_valid |= ATTR_MODE;
235 iap->ia_mode = mode;
236 }
237 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
238 return false;
239 if (set_it) {
240 u32 uid;
241
242 if (xdr_stream_decode_u32(xdr, &uid) < 0)
243 return false;
244 iap->ia_uid = make_kuid(nfsd_user_namespace(rqstp), uid);
245 if (uid_valid(iap->ia_uid))
246 iap->ia_valid |= ATTR_UID;
247 }
248 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
249 return false;
250 if (set_it) {
251 u32 gid;
252
253 if (xdr_stream_decode_u32(xdr, &gid) < 0)
254 return false;
255 iap->ia_gid = make_kgid(nfsd_user_namespace(rqstp), gid);
256 if (gid_valid(iap->ia_gid))
257 iap->ia_valid |= ATTR_GID;
258 }
259 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
260 return false;
261 if (set_it) {
262 u64 newsize;
263
264 if (xdr_stream_decode_u64(xdr, &newsize) < 0)
265 return false;
266 iap->ia_valid |= ATTR_SIZE;
267 iap->ia_size = min_t(u64, newsize, NFS_OFFSET_MAX);
268 }
269 if (xdr_stream_decode_u32(xdr, &set_it) < 0)
270 return false;
271 switch (set_it) {
272 case DONT_CHANGE:
273 break;
274 case SET_TO_SERVER_TIME:
275 iap->ia_valid |= ATTR_ATIME;
276 break;
277 case SET_TO_CLIENT_TIME:
278 if (!svcxdr_decode_nfstime3(xdr, &iap->ia_atime))
279 return false;
280 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
281 break;
282 default:
283 return false;
284 }
285 if (xdr_stream_decode_u32(xdr, &set_it) < 0)
286 return false;
287 switch (set_it) {
288 case DONT_CHANGE:
289 break;
290 case SET_TO_SERVER_TIME:
291 iap->ia_valid |= ATTR_MTIME;
292 break;
293 case SET_TO_CLIENT_TIME:
294 if (!svcxdr_decode_nfstime3(xdr, &iap->ia_mtime))
295 return false;
296 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
297 break;
298 default:
299 return false;
300 }
301
302 return true;
303}
304
305static bool
306svcxdr_decode_sattrguard3(struct xdr_stream *xdr, struct nfsd3_sattrargs *args)
307{
308 __be32 *p;
309 u32 check;
310
311 if (xdr_stream_decode_bool(xdr, &check) < 0)
312 return false;
313 if (check) {
314 p = xdr_inline_decode(xdr, XDR_UNIT * 2);
315 if (!p)
316 return false;
317 args->check_guard = 1;
318 args->guardtime = be32_to_cpup(p);
319 } else
320 args->check_guard = 0;
321
322 return true;
323}
324
Chuck Leverf8a38e22020-10-20 17:04:03 -0400325static bool
326svcxdr_decode_specdata3(struct xdr_stream *xdr, struct nfsd3_mknodargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Chuck Leverf8a38e22020-10-20 17:04:03 -0400328 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Chuck Leverf8a38e22020-10-20 17:04:03 -0400330 p = xdr_inline_decode(xdr, XDR_UNIT * 2);
331 if (!p)
332 return false;
333 args->major = be32_to_cpup(p++);
334 args->minor = be32_to_cpup(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Chuck Leverf8a38e22020-10-20 17:04:03 -0400336 return true;
337}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Chuck Leverf8a38e22020-10-20 17:04:03 -0400339static bool
340svcxdr_decode_devicedata3(struct svc_rqst *rqstp, struct xdr_stream *xdr,
341 struct nfsd3_mknodargs *args)
342{
343 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs) &&
344 svcxdr_decode_specdata3(xdr, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Chuck Lever2c42f802020-10-21 11:58:41 -0400347static bool
348svcxdr_encode_fattr3(struct svc_rqst *rqstp, struct xdr_stream *xdr,
349 const struct svc_fh *fhp, const struct kstat *stat)
350{
351 struct user_namespace *userns = nfsd_user_namespace(rqstp);
352 __be32 *p;
353 u64 fsid;
354
355 p = xdr_reserve_space(xdr, XDR_UNIT * 21);
356 if (!p)
357 return false;
358
359 *p++ = cpu_to_be32(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]);
360 *p++ = cpu_to_be32((u32)(stat->mode & S_IALLUGO));
361 *p++ = cpu_to_be32((u32)stat->nlink);
362 *p++ = cpu_to_be32((u32)from_kuid_munged(userns, stat->uid));
363 *p++ = cpu_to_be32((u32)from_kgid_munged(userns, stat->gid));
364 if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN)
365 p = xdr_encode_hyper(p, (u64)NFS3_MAXPATHLEN);
366 else
367 p = xdr_encode_hyper(p, (u64)stat->size);
368
369 /* used */
370 p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
371
372 /* rdev */
373 *p++ = cpu_to_be32((u32)MAJOR(stat->rdev));
374 *p++ = cpu_to_be32((u32)MINOR(stat->rdev));
375
376 switch(fsid_source(fhp)) {
377 case FSIDSOURCE_FSID:
378 fsid = (u64)fhp->fh_export->ex_fsid;
379 break;
380 case FSIDSOURCE_UUID:
381 fsid = ((u64 *)fhp->fh_export->ex_uuid)[0];
382 fsid ^= ((u64 *)fhp->fh_export->ex_uuid)[1];
383 break;
384 default:
385 fsid = (u64)huge_encode_dev(fhp->fh_dentry->d_sb->s_dev);
386 }
387 p = xdr_encode_hyper(p, fsid);
388
389 /* fileid */
390 p = xdr_encode_hyper(p, stat->ino);
391
392 p = encode_nfstime3(p, &stat->atime);
393 p = encode_nfstime3(p, &stat->mtime);
394 encode_nfstime3(p, &stat->ctime);
395
396 return true;
397}
398
NeilBrownaf6a4e22007-02-14 00:33:12 -0800399static __be32 *encode_fsid(__be32 *p, struct svc_fh *fhp)
400{
401 u64 f;
402 switch(fsid_source(fhp)) {
403 default:
404 case FSIDSOURCE_DEV:
405 p = xdr_encode_hyper(p, (u64)huge_encode_dev
Al Virofc640052016-04-10 01:33:30 -0400406 (fhp->fh_dentry->d_sb->s_dev));
NeilBrownaf6a4e22007-02-14 00:33:12 -0800407 break;
408 case FSIDSOURCE_FSID:
409 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
410 break;
411 case FSIDSOURCE_UUID:
412 f = ((u64*)fhp->fh_export->ex_uuid)[0];
413 f ^= ((u64*)fhp->fh_export->ex_uuid)[1];
414 p = xdr_encode_hyper(p, f);
415 break;
416 }
417 return p;
418}
419
Adrian Bunk3ee6f612006-12-06 20:40:23 -0800420static __be32 *
Al Viro91f07162006-10-19 23:28:57 -0700421encode_fattr3(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp,
David Shawa334de22006-01-06 00:19:58 -0800422 struct kstat *stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400424 struct user_namespace *userns = nfsd_user_namespace(rqstp);
David Shawa334de22006-01-06 00:19:58 -0800425 *p++ = htonl(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]);
Albert Fluegel6e14b462013-11-18 12:18:01 -0500426 *p++ = htonl((u32) (stat->mode & S_IALLUGO));
David Shawa334de22006-01-06 00:19:58 -0800427 *p++ = htonl((u32) stat->nlink);
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400428 *p++ = htonl((u32) from_kuid_munged(userns, stat->uid));
429 *p++ = htonl((u32) from_kgid_munged(userns, stat->gid));
David Shawa334de22006-01-06 00:19:58 -0800430 if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
432 } else {
David Shawa334de22006-01-06 00:19:58 -0800433 p = xdr_encode_hyper(p, (u64) stat->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
David Shawa334de22006-01-06 00:19:58 -0800435 p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
436 *p++ = htonl((u32) MAJOR(stat->rdev));
437 *p++ = htonl((u32) MINOR(stat->rdev));
NeilBrownaf6a4e22007-02-14 00:33:12 -0800438 p = encode_fsid(p, fhp);
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400439 p = xdr_encode_hyper(p, stat->ino);
Arnd Bergmann92c5e462019-10-31 14:55:32 +0100440 p = encode_time3(p, &stat->atime);
441 p = encode_time3(p, &stat->mtime);
442 p = encode_time3(p, &stat->ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 return p;
445}
446
Chuck Lever907c3822020-10-22 13:56:58 -0400447static bool
Chuck Lever70f8e832020-10-22 15:12:38 -0400448svcxdr_encode_wcc_attr(struct xdr_stream *xdr, const struct svc_fh *fhp)
449{
450 __be32 *p;
451
452 p = xdr_reserve_space(xdr, XDR_UNIT * 6);
453 if (!p)
454 return false;
455 p = xdr_encode_hyper(p, (u64)fhp->fh_pre_size);
456 p = encode_nfstime3(p, &fhp->fh_pre_mtime);
457 encode_nfstime3(p, &fhp->fh_pre_ctime);
458
459 return true;
460}
461
462static bool
463svcxdr_encode_pre_op_attr(struct xdr_stream *xdr, const struct svc_fh *fhp)
464{
465 if (!fhp->fh_pre_saved) {
466 if (xdr_stream_encode_item_absent(xdr) < 0)
467 return false;
468 return true;
469 }
470
471 if (xdr_stream_encode_item_present(xdr) < 0)
472 return false;
473 return svcxdr_encode_wcc_attr(xdr, fhp);
474}
475
Chuck Lever20798df2020-11-18 16:11:42 -0500476/**
477 * svcxdr_encode_post_op_attr - Encode NFSv3 post-op attributes
478 * @rqstp: Context of a completed RPC transaction
479 * @xdr: XDR stream
480 * @fhp: File handle to encode
481 *
482 * Return values:
483 * %false: Send buffer space was exhausted
484 * %true: Success
485 */
486bool
Chuck Lever907c3822020-10-22 13:56:58 -0400487svcxdr_encode_post_op_attr(struct svc_rqst *rqstp, struct xdr_stream *xdr,
488 const struct svc_fh *fhp)
489{
490 struct dentry *dentry = fhp->fh_dentry;
491 struct kstat stat;
492
493 /*
494 * The inode may be NULL if the call failed because of a
495 * stale file handle. In this case, no attributes are
496 * returned.
497 */
498 if (fhp->fh_no_wcc || !dentry || !d_really_is_positive(dentry))
499 goto no_post_op_attrs;
500 if (fh_getattr(fhp, &stat) != nfs_ok)
501 goto no_post_op_attrs;
502
503 if (xdr_stream_encode_item_present(xdr) < 0)
504 return false;
505 lease_get_mtime(d_inode(dentry), &stat.mtime);
506 if (!svcxdr_encode_fattr3(rqstp, xdr, fhp, &stat))
507 return false;
508
509 return true;
510
511no_post_op_attrs:
512 return xdr_stream_encode_item_absent(xdr) > 0;
513}
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515/*
516 * Encode post-operation attributes.
517 * The inode may be NULL if the call failed because of a stale file
518 * handle. In this case, no attributes are returned.
519 */
Al Viro91f07162006-10-19 23:28:57 -0700520static __be32 *
521encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 struct dentry *dentry = fhp->fh_dentry;
Jeff Laytondaab1102020-11-30 17:03:14 -0500524 if (!fhp->fh_no_wcc && dentry && d_really_is_positive(dentry)) {
Al Viro3dadecc2013-01-24 02:18:08 -0500525 __be32 err;
David Shawa334de22006-01-06 00:19:58 -0800526 struct kstat stat;
527
Al Viro3dadecc2013-01-24 02:18:08 -0500528 err = fh_getattr(fhp, &stat);
David Shawa334de22006-01-06 00:19:58 -0800529 if (!err) {
530 *p++ = xdr_one; /* attributes follow */
David Howells2b0143b2015-03-17 22:25:59 +0000531 lease_get_mtime(d_inode(dentry), &stat.mtime);
David Shawa334de22006-01-06 00:19:58 -0800532 return encode_fattr3(rqstp, p, fhp, &stat);
533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535 *p++ = xdr_zero;
536 return p;
537}
538
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000539/* Helper for NFSv3 ACLs */
Al Viro91f07162006-10-19 23:28:57 -0700540__be32 *
541nfs3svc_encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000542{
543 return encode_post_op_attr(rqstp, p, fhp);
544}
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546/*
Chuck Lever70f8e832020-10-22 15:12:38 -0400547 * Encode weak cache consistency data
548 */
549static bool
550svcxdr_encode_wcc_data(struct svc_rqst *rqstp, struct xdr_stream *xdr,
551 const struct svc_fh *fhp)
552{
553 struct dentry *dentry = fhp->fh_dentry;
554
555 if (!dentry || !d_really_is_positive(dentry) || !fhp->fh_post_saved)
556 goto neither;
557
558 /* before */
559 if (!svcxdr_encode_pre_op_attr(xdr, fhp))
560 return false;
561
562 /* after */
563 if (xdr_stream_encode_item_present(xdr) < 0)
564 return false;
565 if (!svcxdr_encode_fattr3(rqstp, xdr, fhp, &fhp->fh_post_attr))
566 return false;
567
568 return true;
569
570neither:
571 if (xdr_stream_encode_item_absent(xdr) < 0)
572 return false;
573 if (!svcxdr_encode_post_op_attr(rqstp, xdr, fhp))
574 return false;
575
576 return true;
577}
578
J. Bruce Fields428a23d2021-01-29 14:27:01 -0500579static bool fs_supports_change_attribute(struct super_block *sb)
580{
581 return sb->s_flags & SB_I_VERSION || sb->s_export_op->fetch_iversion;
582}
583
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400584/*
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200585 * Fill in the pre_op attr for the wcc data
586 */
587void fill_pre_wcc(struct svc_fh *fhp)
588{
589 struct inode *inode;
590 struct kstat stat;
J. Bruce Fields942b20dc2020-11-30 17:46:17 -0500591 bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200592
Jeff Laytondaab1102020-11-30 17:03:14 -0500593 if (fhp->fh_no_wcc || fhp->fh_pre_saved)
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200594 return;
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200595 inode = d_inode(fhp->fh_dentry);
J. Bruce Fields428a23d2021-01-29 14:27:01 -0500596 if (fs_supports_change_attribute(inode->i_sb) || !v4) {
597 __be32 err = fh_getattr(fhp, &stat);
598
599 if (err) {
600 /* Grab the times from inode anyway */
601 stat.mtime = inode->i_mtime;
602 stat.ctime = inode->i_ctime;
603 stat.size = inode->i_size;
604 }
605 fhp->fh_pre_mtime = stat.mtime;
606 fhp->fh_pre_ctime = stat.ctime;
607 fhp->fh_pre_size = stat.size;
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200608 }
J. Bruce Fields942b20dc2020-11-30 17:46:17 -0500609 if (v4)
610 fhp->fh_pre_change = nfsd4_change_attribute(&stat, inode);
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200611
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200612 fhp->fh_pre_saved = true;
613}
614
615/*
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400616 * Fill in the post_op attr for the wcc data
617 */
618void fill_post_wcc(struct svc_fh *fhp)
619{
J. Bruce Fields942b20dc2020-11-30 17:46:17 -0500620 bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
621 struct inode *inode = d_inode(fhp->fh_dentry);
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400622
Jeff Laytondaab1102020-11-30 17:03:14 -0500623 if (fhp->fh_no_wcc)
624 return;
625
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400626 if (fhp->fh_post_saved)
627 printk("nfsd: inode locked twice during operation.\n");
628
J. Bruce Fields428a23d2021-01-29 14:27:01 -0500629 fhp->fh_post_saved = true;
630
631 if (fs_supports_change_attribute(inode->i_sb) || !v4) {
632 __be32 err = fh_getattr(fhp, &fhp->fh_post_attr);
633
634 if (err) {
635 fhp->fh_post_saved = false;
636 fhp->fh_post_attr.ctime = inode->i_ctime;
637 }
638 }
J. Bruce Fields942b20dc2020-11-30 17:46:17 -0500639 if (v4)
640 fhp->fh_post_change =
641 nfsd4_change_attribute(&fhp->fh_post_attr, inode);
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400642}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644/*
645 * XDR decode functions
646 */
Chuck Leverdcc46992020-10-01 18:59:12 -0400647
648int
Chuck Lever95753632020-10-20 14:30:02 -0400649nfs3svc_decode_fhandleargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Chuck Lever95753632020-10-20 14:30:02 -0400651 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200652 struct nfsd_fhandle *args = rqstp->rq_argp;
653
Chuck Lever95753632020-10-20 14:30:02 -0400654 return svcxdr_decode_nfs_fh3(xdr, &args->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
657int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200658nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Chuck Lever9cde9362020-10-20 15:48:22 -0400660 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200661 struct nfsd3_sattrargs *args = rqstp->rq_argp;
662
Chuck Lever9cde9362020-10-20 15:48:22 -0400663 return svcxdr_decode_nfs_fh3(xdr, &args->fh) &&
664 svcxdr_decode_sattr3(rqstp, xdr, &args->attrs) &&
665 svcxdr_decode_sattrguard3(xdr, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
668int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200669nfs3svc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Chuck Lever54d1d432020-10-20 15:42:33 -0400671 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200672 struct nfsd3_diropargs *args = rqstp->rq_argp;
673
Chuck Lever54d1d432020-10-20 15:42:33 -0400674 return svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
677int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200678nfs3svc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Chuck Lever3b921a22020-10-20 14:32:04 -0400680 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200681 struct nfsd3_accessargs *args = rqstp->rq_argp;
682
Chuck Lever3b921a22020-10-20 14:32:04 -0400683 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return 0;
Chuck Lever3b921a22020-10-20 14:32:04 -0400685 if (xdr_stream_decode_u32(xdr, &args->access) < 0)
686 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Chuck Lever3b921a22020-10-20 14:32:04 -0400688 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
691int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200692nfs3svc_decode_readargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Chuck Leverbe63bd22020-10-20 14:34:40 -0400694 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200695 struct nfsd3_readargs *args = rqstp->rq_argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Chuck Leverbe63bd22020-10-20 14:34:40 -0400697 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return 0;
Chuck Leverbe63bd22020-10-20 14:34:40 -0400699 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
700 return 0;
701 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
702 return 0;
J. Bruce Fields9512a162017-05-16 15:57:42 -0400703
Chuck Leverbe63bd22020-10-20 14:34:40 -0400704 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
707int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200708nfs3svc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Chuck Leverc43b2f22020-10-22 11:14:55 -0400710 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200711 struct nfsd3_writeargs *args = rqstp->rq_argp;
Greg Banks7adae482006-10-04 02:15:47 -0700712 u32 max_blocksize = svc_max_payload(rqstp);
J. Bruce Fieldsdb44bac2017-04-25 16:21:34 -0400713 struct kvec *head = rqstp->rq_arg.head;
714 struct kvec *tail = rqstp->rq_arg.tail;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400715 size_t remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Chuck Leverc43b2f22020-10-22 11:14:55 -0400717 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return 0;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400719 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
720 return 0;
721 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
722 return 0;
723 if (xdr_stream_decode_u32(xdr, &args->stable) < 0)
724 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Chuck Leverc43b2f22020-10-22 11:14:55 -0400726 /* opaque data */
727 if (xdr_stream_decode_u32(xdr, &args->len) < 0)
J. Bruce Fields13bf9fb2017-04-21 15:26:30 -0400728 return 0;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400729
730 /* request sanity */
Peter Staubachf34b95682007-05-09 02:34:48 -0700731 if (args->count != args->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return 0;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400733 remaining = head->iov_len + rqstp->rq_arg.page_len + tail->iov_len;
734 remaining -= xdr_stream_pos(xdr);
735 if (remaining < xdr_align_size(args->len))
Peter Staubachf34b95682007-05-09 02:34:48 -0700736 return 0;
Peter Staubachf34b95682007-05-09 02:34:48 -0700737 if (args->count > max_blocksize) {
738 args->count = max_blocksize;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400739 args->len = max_blocksize;
Peter Staubachf34b95682007-05-09 02:34:48 -0700740 }
Chuck Lever8154ef22018-03-27 10:54:07 -0400741
Chuck Leverc43b2f22020-10-22 11:14:55 -0400742 args->first.iov_base = xdr->p;
743 args->first.iov_len = head->iov_len - xdr_stream_pos(xdr);
744
Peter Staubachf34b95682007-05-09 02:34:48 -0700745 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
748int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200749nfs3svc_decode_createargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Chuck Lever6b3a1192020-10-20 15:56:11 -0400751 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200752 struct nfsd3_createargs *args = rqstp->rq_argp;
753
Chuck Lever6b3a1192020-10-20 15:56:11 -0400754 if (!svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return 0;
Chuck Lever6b3a1192020-10-20 15:56:11 -0400756 if (xdr_stream_decode_u32(xdr, &args->createmode) < 0)
757 return 0;
758 switch (args->createmode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 case NFS3_CREATE_UNCHECKED:
760 case NFS3_CREATE_GUARDED:
Chuck Lever6b3a1192020-10-20 15:56:11 -0400761 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 case NFS3_CREATE_EXCLUSIVE:
Chuck Lever6b3a1192020-10-20 15:56:11 -0400763 args->verf = xdr_inline_decode(xdr, NFS3_CREATEVERFSIZE);
764 if (!args->verf)
765 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 break;
767 default:
768 return 0;
769 }
Chuck Lever6b3a1192020-10-20 15:56:11 -0400770 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
Christoph Hellwig026fec72017-05-08 19:01:48 +0200772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200774nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Chuck Lever83374c22020-10-20 17:02:16 -0400776 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200777 struct nfsd3_createargs *args = rqstp->rq_argp;
778
Chuck Lever83374c22020-10-20 17:02:16 -0400779 return svcxdr_decode_diropargs3(xdr, &args->fh,
780 &args->name, &args->len) &&
781 svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
784int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200785nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Chuck Leverda392012020-10-20 16:01:16 -0400787 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200788 struct nfsd3_symlinkargs *args = rqstp->rq_argp;
Chuck Leverda392012020-10-20 16:01:16 -0400789 struct kvec *head = rqstp->rq_arg.head;
790 struct kvec *tail = rqstp->rq_arg.tail;
791 size_t remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Chuck Leverda392012020-10-20 16:01:16 -0400793 if (!svcxdr_decode_diropargs3(xdr, &args->ffh, &args->fname, &args->flen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return 0;
Chuck Leverda392012020-10-20 16:01:16 -0400795 if (!svcxdr_decode_sattr3(rqstp, xdr, &args->attrs))
Chuck Lever38a70312018-03-27 10:54:21 -0400796 return 0;
Chuck Leverda392012020-10-20 16:01:16 -0400797 if (xdr_stream_decode_u32(xdr, &args->tlen) < 0)
798 return 0;
799
800 /* request sanity */
801 remaining = head->iov_len + rqstp->rq_arg.page_len + tail->iov_len;
802 remaining -= xdr_stream_pos(xdr);
803 if (remaining < xdr_align_size(args->tlen))
804 return 0;
805
806 args->first.iov_base = xdr->p;
807 args->first.iov_len = head->iov_len - xdr_stream_pos(xdr);
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return 1;
810}
811
812int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200813nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Chuck Leverf8a38e22020-10-20 17:04:03 -0400815 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200816 struct nfsd3_mknodargs *args = rqstp->rq_argp;
817
Chuck Leverf8a38e22020-10-20 17:04:03 -0400818 if (!svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return 0;
Chuck Leverf8a38e22020-10-20 17:04:03 -0400820 if (xdr_stream_decode_u32(xdr, &args->ftype) < 0)
821 return 0;
822 switch (args->ftype) {
823 case NF3CHR:
824 case NF3BLK:
825 return svcxdr_decode_devicedata3(rqstp, xdr, args);
826 case NF3SOCK:
827 case NF3FIFO:
828 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
829 case NF3REG:
830 case NF3DIR:
831 case NF3LNK:
832 /* Valid XDR but illegal file types */
833 break;
834 default:
835 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
837
Chuck Leverf8a38e22020-10-20 17:04:03 -0400838 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
841int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200842nfs3svc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Chuck Leverd181e0a2020-10-20 15:44:12 -0400844 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200845 struct nfsd3_renameargs *args = rqstp->rq_argp;
846
Chuck Leverd181e0a2020-10-20 15:44:12 -0400847 return svcxdr_decode_diropargs3(xdr, &args->ffh,
848 &args->fname, &args->flen) &&
849 svcxdr_decode_diropargs3(xdr, &args->tfh,
850 &args->tname, &args->tlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
853int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200854nfs3svc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Chuck Leverefaa1e72020-10-19 13:26:32 -0400856 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200857 struct nfsd3_linkargs *args = rqstp->rq_argp;
858
Chuck Leverefaa1e72020-10-19 13:26:32 -0400859 return svcxdr_decode_nfs_fh3(xdr, &args->ffh) &&
860 svcxdr_decode_diropargs3(xdr, &args->tfh,
861 &args->tname, &args->tlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
864int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200865nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400867 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200868 struct nfsd3_readdirargs *args = rqstp->rq_argp;
NeilBrownf875a792019-03-07 09:49:46 +1100869
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400870 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return 0;
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400872 if (xdr_stream_decode_u64(xdr, &args->cookie) < 0)
873 return 0;
874 args->verf = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE);
875 if (!args->verf)
876 return 0;
877 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
878 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400880 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
883int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200884nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400886 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200887 struct nfsd3_readdirargs *args = rqstp->rq_argp;
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400888 u32 dircount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400890 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return 0;
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400892 if (xdr_stream_decode_u64(xdr, &args->cookie) < 0)
893 return 0;
894 args->verf = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE);
895 if (!args->verf)
896 return 0;
897 /* dircount is ignored */
898 if (xdr_stream_decode_u32(xdr, &dircount) < 0)
899 return 0;
900 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
901 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400903 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
906int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200907nfs3svc_decode_commitargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Chuck Leverc8d26a02020-10-20 14:41:56 -0400909 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200910 struct nfsd3_commitargs *args = rqstp->rq_argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Chuck Leverc8d26a02020-10-20 14:41:56 -0400912 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
913 return 0;
914 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
915 return 0;
916 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
917 return 0;
918
919 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
922/*
923 * XDR encode functions
924 */
Chuck Levercc028a12020-10-02 15:52:44 -0400925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926/* GETATTR */
927int
Chuck Lever2c42f802020-10-21 11:58:41 -0400928nfs3svc_encode_getattrres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Chuck Lever2c42f802020-10-21 11:58:41 -0400930 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200931 struct nfsd3_attrstat *resp = rqstp->rq_resp;
932
Chuck Lever2c42f802020-10-21 11:58:41 -0400933 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
934 return 0;
935 switch (resp->status) {
936 case nfs_ok:
937 lease_get_mtime(d_inode(resp->fh.fh_dentry), &resp->stat.mtime);
938 if (!svcxdr_encode_fattr3(rqstp, xdr, &resp->fh, &resp->stat))
939 return 0;
940 break;
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400941 }
Chuck Lever2c42f802020-10-21 11:58:41 -0400942
943 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
946/* SETATTR, REMOVE, RMDIR */
947int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200948nfs3svc_encode_wccstat(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Chuck Lever70f8e832020-10-22 15:12:38 -0400950 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200951 struct nfsd3_attrstat *resp = rqstp->rq_resp;
952
Chuck Lever70f8e832020-10-22 15:12:38 -0400953 return svcxdr_encode_nfsstat3(xdr, resp->status) &&
954 svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
957/* LOOKUP */
Chuck Lever5cf35332020-10-22 14:46:58 -0400958int nfs3svc_encode_lookupres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
Chuck Lever5cf35332020-10-22 14:46:58 -0400960 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200961 struct nfsd3_diropres *resp = rqstp->rq_resp;
962
Chuck Lever5cf35332020-10-22 14:46:58 -0400963 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
964 return 0;
965 switch (resp->status) {
966 case nfs_ok:
967 if (!svcxdr_encode_nfs_fh3(xdr, &resp->fh))
968 return 0;
969 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
970 return 0;
971 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->dirfh))
972 return 0;
973 break;
974 default:
975 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->dirfh))
976 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
Chuck Lever5cf35332020-10-22 14:46:58 -0400978
979 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
982/* ACCESS */
983int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200984nfs3svc_encode_accessres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Chuck Lever907c3822020-10-22 13:56:58 -0400986 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200987 struct nfsd3_accessres *resp = rqstp->rq_resp;
988
Chuck Lever907c3822020-10-22 13:56:58 -0400989 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
990 return 0;
991 switch (resp->status) {
992 case nfs_ok:
993 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
994 return 0;
995 if (xdr_stream_encode_u32(xdr, resp->access) < 0)
996 return 0;
997 break;
998 default:
999 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
1000 return 0;
1001 }
1002
1003 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
1005
1006/* READLINK */
1007int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001008nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Chuck Lever9a9c8922020-10-22 15:18:40 -04001010 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001011 struct nfsd3_readlinkres *resp = rqstp->rq_resp;
Chuck Lever76e54922020-11-05 10:24:19 -05001012 struct kvec *head = rqstp->rq_res.head;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001013
Chuck Lever9a9c8922020-10-22 15:18:40 -04001014 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1015 return 0;
1016 switch (resp->status) {
1017 case nfs_ok:
1018 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
Chuck Lever76e54922020-11-05 10:24:19 -05001019 return 0;
Chuck Lever9a9c8922020-10-22 15:18:40 -04001020 if (xdr_stream_encode_u32(xdr, resp->len) < 0)
1021 return 0;
1022 xdr_write_pages(xdr, resp->pages, 0, resp->len);
1023 if (svc_encode_result_payload(rqstp, head->iov_len, resp->len) < 0)
1024 return 0;
1025 break;
1026 default:
1027 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
1028 return 0;
1029 }
1030
1031 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
1033
1034/* READ */
1035int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001036nfs3svc_encode_readres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Chuck Levercc9bcda2020-10-22 15:23:50 -04001038 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001039 struct nfsd3_readres *resp = rqstp->rq_resp;
Chuck Lever76e54922020-11-05 10:24:19 -05001040 struct kvec *head = rqstp->rq_res.head;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001041
Chuck Levercc9bcda2020-10-22 15:23:50 -04001042 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1043 return 0;
1044 switch (resp->status) {
1045 case nfs_ok:
1046 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
Chuck Lever76e54922020-11-05 10:24:19 -05001047 return 0;
Chuck Levercc9bcda2020-10-22 15:23:50 -04001048 if (xdr_stream_encode_u32(xdr, resp->count) < 0)
1049 return 0;
1050 if (xdr_stream_encode_bool(xdr, resp->eof) < 0)
1051 return 0;
1052 if (xdr_stream_encode_u32(xdr, resp->count) < 0)
1053 return 0;
1054 xdr_write_pages(xdr, resp->pages, rqstp->rq_res.page_base,
1055 resp->count);
1056 if (svc_encode_result_payload(rqstp, head->iov_len, resp->count) < 0)
1057 return 0;
1058 break;
1059 default:
1060 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
1061 return 0;
1062 }
1063
1064 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
1067/* WRITE */
1068int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001069nfs3svc_encode_writeres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Chuck Leverecb7a082020-10-22 15:26:31 -04001071 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001072 struct nfsd3_writeres *resp = rqstp->rq_resp;
Stanislav Kinsburskyb9c0ef82012-12-06 14:23:19 +03001073
Chuck Leverecb7a082020-10-22 15:26:31 -04001074 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1075 return 0;
1076 switch (resp->status) {
1077 case nfs_ok:
1078 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
1079 return 0;
1080 if (xdr_stream_encode_u32(xdr, resp->count) < 0)
1081 return 0;
1082 if (xdr_stream_encode_u32(xdr, resp->committed) < 0)
1083 return 0;
1084 if (!svcxdr_encode_writeverf3(xdr, resp->verf))
1085 return 0;
1086 break;
1087 default:
1088 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
1089 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
Chuck Leverecb7a082020-10-22 15:26:31 -04001091
1092 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
1095/* CREATE, MKDIR, SYMLINK, MKNOD */
1096int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001097nfs3svc_encode_createres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
Chuck Lever78315b32020-10-22 15:27:23 -04001099 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001100 struct nfsd3_diropres *resp = rqstp->rq_resp;
1101
Chuck Lever78315b32020-10-22 15:27:23 -04001102 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1103 return 0;
1104 switch (resp->status) {
1105 case nfs_ok:
1106 if (!svcxdr_encode_post_op_fh3(xdr, &resp->fh))
1107 return 0;
1108 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
1109 return 0;
1110 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->dirfh))
1111 return 0;
1112 break;
1113 default:
1114 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->dirfh))
1115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 }
Chuck Lever78315b32020-10-22 15:27:23 -04001117
1118 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
1120
1121/* RENAME */
1122int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001123nfs3svc_encode_renameres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Chuck Lever89d79e92020-10-22 15:33:05 -04001125 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001126 struct nfsd3_renameres *resp = rqstp->rq_resp;
1127
Chuck Lever89d79e92020-10-22 15:33:05 -04001128 return svcxdr_encode_nfsstat3(xdr, resp->status) &&
1129 svcxdr_encode_wcc_data(rqstp, xdr, &resp->ffh) &&
1130 svcxdr_encode_wcc_data(rqstp, xdr, &resp->tfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
1132
1133/* LINK */
1134int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001135nfs3svc_encode_linkres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Chuck Lever4d743802020-10-22 15:08:29 -04001137 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001138 struct nfsd3_linkres *resp = rqstp->rq_resp;
1139
Chuck Lever4d743802020-10-22 15:08:29 -04001140 return svcxdr_encode_nfsstat3(xdr, resp->status) &&
1141 svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh) &&
1142 svcxdr_encode_wcc_data(rqstp, xdr, &resp->tfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
1145/* READDIR */
1146int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001147nfs3svc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Chuck Levere4ccfe32020-10-22 19:31:48 -04001149 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001150 struct nfsd3_readdirres *resp = rqstp->rq_resp;
Chuck Lever7f87fc22020-10-22 19:46:58 -04001151 struct xdr_buf *dirlist = &resp->dirlist;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001152
Chuck Levere4ccfe32020-10-22 19:31:48 -04001153 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1154 return 0;
1155 switch (resp->status) {
1156 case nfs_ok:
1157 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
1158 return 0;
1159 if (!svcxdr_encode_cookieverf3(xdr, resp->verf))
1160 return 0;
Chuck Lever7f87fc22020-10-22 19:46:58 -04001161 xdr_write_pages(xdr, dirlist->pages, 0, dirlist->len);
Chuck Levere4ccfe32020-10-22 19:31:48 -04001162 /* no more entries */
1163 if (xdr_stream_encode_item_absent(xdr) < 0)
1164 return 0;
1165 if (xdr_stream_encode_bool(xdr, resp->common.err == nfserr_eof) < 0)
1166 return 0;
1167 break;
1168 default:
1169 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
1170 return 0;
1171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Chuck Levere4ccfe32020-10-22 19:31:48 -04001173 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
Al Viroefe39652012-04-13 00:32:14 -04001176static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
NeilBrown43b0e7e2015-05-03 09:16:53 +10001178 const char *name, int namlen, u64 ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
1180 struct svc_export *exp;
1181 struct dentry *dparent, *dchild;
Al Viroefe39652012-04-13 00:32:14 -04001182 __be32 rv = nfserr_noent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 dparent = cd->fh.fh_dentry;
1185 exp = cd->fh.fh_export;
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (isdotent(name, namlen)) {
1188 if (namlen == 2) {
1189 dchild = dget_parent(dparent);
J. Bruce Fields51b2ee72021-01-11 16:01:29 -05001190 /*
1191 * Don't return filehandle for ".." if we're at
1192 * the filesystem or export root:
1193 */
Al Viroefe39652012-04-13 00:32:14 -04001194 if (dchild == dparent)
1195 goto out;
J. Bruce Fields51b2ee72021-01-11 16:01:29 -05001196 if (dparent == exp->ex_path.dentry)
1197 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 } else
1199 dchild = dget(dparent);
1200 } else
Al Viro6c2d47982019-10-31 01:21:58 -04001201 dchild = lookup_positive_unlocked(name, dparent, namlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (IS_ERR(dchild))
Al Viroefe39652012-04-13 00:32:14 -04001203 return rv;
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001204 if (d_mountpoint(dchild))
1205 goto out;
NeilBrown43b0e7e2015-05-03 09:16:53 +10001206 if (dchild->d_inode->i_ino != ino)
1207 goto out;
Al Viroefe39652012-04-13 00:32:14 -04001208 rv = fh_compose(fhp, exp, dchild, &cd->fh);
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001209out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 dput(dchild);
1211 return rv;
1212}
1213
Chuck Levera161e6c2020-11-10 09:57:14 -05001214/**
1215 * nfs3svc_encode_cookie3 - Encode a directory offset cookie
1216 * @resp: readdir result context
1217 * @offset: offset cookie to encode
1218 *
Chuck Lever7f87fc22020-10-22 19:46:58 -04001219 * The buffer space for the offset cookie has already been reserved
1220 * by svcxdr_encode_entry3_common().
Chuck Levera161e6c2020-11-10 09:57:14 -05001221 */
1222void nfs3svc_encode_cookie3(struct nfsd3_readdirres *resp, u64 offset)
1223{
Chuck Lever7f87fc22020-10-22 19:46:58 -04001224 __be64 cookie = cpu_to_be64(offset);
Chuck Levera161e6c2020-11-10 09:57:14 -05001225
Chuck Lever7f87fc22020-10-22 19:46:58 -04001226 if (!resp->cookie_offset)
1227 return;
1228 write_bytes_to_xdr_buf(&resp->dirlist, resp->cookie_offset, &cookie,
1229 sizeof(cookie));
1230 resp->cookie_offset = 0;
Chuck Levera161e6c2020-11-10 09:57:14 -05001231}
1232
Chuck Lever8b704492020-11-06 13:08:45 -05001233static bool
Chuck Lever7f87fc22020-10-22 19:46:58 -04001234svcxdr_encode_entry3_common(struct nfsd3_readdirres *resp, const char *name,
1235 int namlen, loff_t offset, u64 ino)
1236{
1237 struct xdr_buf *dirlist = &resp->dirlist;
1238 struct xdr_stream *xdr = &resp->xdr;
1239
1240 if (xdr_stream_encode_item_present(xdr) < 0)
1241 return false;
1242 /* fileid */
1243 if (xdr_stream_encode_u64(xdr, ino) < 0)
1244 return false;
1245 /* name */
1246 if (xdr_stream_encode_opaque(xdr, name, min(namlen, NFS3_MAXNAMLEN)) < 0)
1247 return false;
1248 /* cookie */
1249 resp->cookie_offset = dirlist->len;
1250 if (xdr_stream_encode_u64(xdr, NFS_OFFSET_MAX) < 0)
1251 return false;
1252
1253 return true;
1254}
1255
1256/**
1257 * nfs3svc_encode_entry3 - encode one NFSv3 READDIR entry
1258 * @data: directory context
1259 * @name: name of the object to be encoded
1260 * @namlen: length of that name, in bytes
1261 * @offset: the offset of the previous entry
1262 * @ino: the fileid of this entry
1263 * @d_type: unused
1264 *
1265 * Return values:
1266 * %0: Entry was successfully encoded.
1267 * %-EINVAL: An encoding problem occured, secondary status code in resp->common.err
1268 *
1269 * On exit, the following fields are updated:
1270 * - resp->xdr
1271 * - resp->common.err
1272 * - resp->cookie_offset
1273 */
1274int nfs3svc_encode_entry3(void *data, const char *name, int namlen,
1275 loff_t offset, u64 ino, unsigned int d_type)
1276{
1277 struct readdir_cd *ccd = data;
1278 struct nfsd3_readdirres *resp = container_of(ccd,
1279 struct nfsd3_readdirres,
1280 common);
1281 unsigned int starting_length = resp->dirlist.len;
1282
1283 /* The offset cookie for the previous entry */
1284 nfs3svc_encode_cookie3(resp, offset);
1285
1286 if (!svcxdr_encode_entry3_common(resp, name, namlen, offset, ino))
1287 goto out_toosmall;
1288
1289 xdr_commit_encode(&resp->xdr);
1290 resp->common.err = nfs_ok;
1291 return 0;
1292
1293out_toosmall:
1294 resp->cookie_offset = 0;
1295 resp->common.err = nfserr_toosmall;
1296 resp->dirlist.len = starting_length;
1297 return -EINVAL;
1298}
1299
1300static bool
1301svcxdr_encode_entry3_plus(struct nfsd3_readdirres *resp, const char *name,
1302 int namlen, u64 ino)
1303{
1304 struct xdr_stream *xdr = &resp->xdr;
1305 struct svc_fh *fhp = &resp->scratch;
1306 bool result;
1307
1308 result = false;
1309 fh_init(fhp, NFS3_FHSIZE);
1310 if (compose_entry_fh(resp, fhp, name, namlen, ino) != nfs_ok)
1311 goto out_noattrs;
1312
1313 if (!svcxdr_encode_post_op_attr(resp->rqstp, xdr, fhp))
1314 goto out;
1315 if (!svcxdr_encode_post_op_fh3(xdr, fhp))
1316 goto out;
1317 result = true;
1318
1319out:
1320 fh_put(fhp);
1321 return result;
1322
1323out_noattrs:
1324 if (xdr_stream_encode_item_absent(xdr) < 0)
1325 return false;
1326 if (xdr_stream_encode_item_absent(xdr) < 0)
1327 return false;
1328 return true;
1329}
1330
1331/**
1332 * nfs3svc_encode_entryplus3 - encode one NFSv3 READDIRPLUS entry
1333 * @data: directory context
1334 * @name: name of the object to be encoded
1335 * @namlen: length of that name, in bytes
1336 * @offset: the offset of the previous entry
1337 * @ino: the fileid of this entry
1338 * @d_type: unused
1339 *
1340 * Return values:
1341 * %0: Entry was successfully encoded.
1342 * %-EINVAL: An encoding problem occured, secondary status code in resp->common.err
1343 *
1344 * On exit, the following fields are updated:
1345 * - resp->xdr
1346 * - resp->common.err
1347 * - resp->cookie_offset
1348 */
1349int nfs3svc_encode_entryplus3(void *data, const char *name, int namlen,
1350 loff_t offset, u64 ino, unsigned int d_type)
1351{
1352 struct readdir_cd *ccd = data;
1353 struct nfsd3_readdirres *resp = container_of(ccd,
1354 struct nfsd3_readdirres,
1355 common);
1356 unsigned int starting_length = resp->dirlist.len;
1357
1358 /* The offset cookie for the previous entry */
1359 nfs3svc_encode_cookie3(resp, offset);
1360
1361 if (!svcxdr_encode_entry3_common(resp, name, namlen, offset, ino))
1362 goto out_toosmall;
1363 if (!svcxdr_encode_entry3_plus(resp, name, namlen, ino))
1364 goto out_toosmall;
1365
1366 xdr_commit_encode(&resp->xdr);
1367 resp->common.err = nfs_ok;
1368 return 0;
1369
1370out_toosmall:
1371 resp->cookie_offset = 0;
1372 resp->common.err = nfserr_toosmall;
1373 resp->dirlist.len = starting_length;
1374 return -EINVAL;
1375}
1376
1377static bool
Chuck Lever8b704492020-11-06 13:08:45 -05001378svcxdr_encode_fsstat3resok(struct xdr_stream *xdr,
1379 const struct nfsd3_fsstatres *resp)
1380{
1381 const struct kstatfs *s = &resp->stats;
1382 u64 bs = s->f_bsize;
1383 __be32 *p;
1384
1385 p = xdr_reserve_space(xdr, XDR_UNIT * 13);
1386 if (!p)
1387 return false;
1388 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1389 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1390 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1391 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1392 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1393 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1394 *p = cpu_to_be32(resp->invarsec); /* mean unchanged time */
1395
1396 return true;
1397}
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399/* FSSTAT */
1400int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001401nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Chuck Lever8b704492020-11-06 13:08:45 -05001403 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001404 struct nfsd3_fsstatres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Chuck Lever8b704492020-11-06 13:08:45 -05001406 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1407 return 0;
1408 switch (resp->status) {
1409 case nfs_ok:
1410 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1411 return 0;
1412 if (!svcxdr_encode_fsstat3resok(xdr, resp))
1413 return 0;
1414 break;
1415 default:
1416 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1417 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
Chuck Lever8b704492020-11-06 13:08:45 -05001419
1420 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421}
1422
Chuck Lever0a139d12020-10-22 13:42:13 -04001423static bool
1424svcxdr_encode_fsinfo3resok(struct xdr_stream *xdr,
1425 const struct nfsd3_fsinfores *resp)
1426{
1427 __be32 *p;
1428
1429 p = xdr_reserve_space(xdr, XDR_UNIT * 12);
1430 if (!p)
1431 return false;
1432 *p++ = cpu_to_be32(resp->f_rtmax);
1433 *p++ = cpu_to_be32(resp->f_rtpref);
1434 *p++ = cpu_to_be32(resp->f_rtmult);
1435 *p++ = cpu_to_be32(resp->f_wtmax);
1436 *p++ = cpu_to_be32(resp->f_wtpref);
1437 *p++ = cpu_to_be32(resp->f_wtmult);
1438 *p++ = cpu_to_be32(resp->f_dtpref);
1439 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1440 p = encode_nfstime3(p, &nfs3svc_time_delta);
1441 *p = cpu_to_be32(resp->f_properties);
1442
1443 return true;
1444}
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446/* FSINFO */
1447int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001448nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
Chuck Lever0a139d12020-10-22 13:42:13 -04001450 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001451 struct nfsd3_fsinfores *resp = rqstp->rq_resp;
1452
Chuck Lever0a139d12020-10-22 13:42:13 -04001453 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1454 return 0;
1455 switch (resp->status) {
1456 case nfs_ok:
1457 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1458 return 0;
1459 if (!svcxdr_encode_fsinfo3resok(xdr, resp))
1460 return 0;
1461 break;
1462 default:
1463 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1464 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
1466
Chuck Lever0a139d12020-10-22 13:42:13 -04001467 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
Chuck Leverded04a52020-11-06 13:15:09 -05001470static bool
1471svcxdr_encode_pathconf3resok(struct xdr_stream *xdr,
1472 const struct nfsd3_pathconfres *resp)
1473{
1474 __be32 *p;
1475
1476 p = xdr_reserve_space(xdr, XDR_UNIT * 6);
1477 if (!p)
1478 return false;
1479 *p++ = cpu_to_be32(resp->p_link_max);
1480 *p++ = cpu_to_be32(resp->p_name_max);
1481 p = xdr_encode_bool(p, resp->p_no_trunc);
1482 p = xdr_encode_bool(p, resp->p_chown_restricted);
1483 p = xdr_encode_bool(p, resp->p_case_insensitive);
1484 xdr_encode_bool(p, resp->p_case_preserving);
1485
1486 return true;
1487}
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489/* PATHCONF */
1490int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001491nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
Chuck Leverded04a52020-11-06 13:15:09 -05001493 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001494 struct nfsd3_pathconfres *resp = rqstp->rq_resp;
1495
Chuck Leverded04a52020-11-06 13:15:09 -05001496 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1497 return 0;
1498 switch (resp->status) {
1499 case nfs_ok:
1500 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1501 return 0;
1502 if (!svcxdr_encode_pathconf3resok(xdr, resp))
1503 return 0;
1504 break;
1505 default:
1506 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1507 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
1509
Chuck Leverded04a52020-11-06 13:15:09 -05001510 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
1512
1513/* COMMIT */
1514int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001515nfs3svc_encode_commitres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Chuck Lever5ef28262020-10-22 15:35:46 -04001517 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001518 struct nfsd3_commitres *resp = rqstp->rq_resp;
Stanislav Kinsburskyb9c0ef82012-12-06 14:23:19 +03001519
Chuck Lever5ef28262020-10-22 15:35:46 -04001520 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1521 return 0;
1522 switch (resp->status) {
1523 case nfs_ok:
1524 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
1525 return 0;
1526 if (!svcxdr_encode_writeverf3(xdr, resp->verf))
1527 return 0;
1528 break;
1529 default:
1530 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
1531 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
Chuck Lever5ef28262020-10-22 15:35:46 -04001533
1534 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535}
1536
1537/*
1538 * XDR release functions
1539 */
Christoph Hellwig85374882017-05-08 18:48:24 +02001540void
1541nfs3svc_release_fhandle(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Christoph Hellwig85374882017-05-08 18:48:24 +02001543 struct nfsd3_attrstat *resp = rqstp->rq_resp;
1544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 fh_put(&resp->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
Christoph Hellwig85374882017-05-08 18:48:24 +02001548void
1549nfs3svc_release_fhandle2(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
Christoph Hellwig85374882017-05-08 18:48:24 +02001551 struct nfsd3_fhandle_pair *resp = rqstp->rq_resp;
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 fh_put(&resp->fh1);
1554 fh_put(&resp->fh2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}