blob: 052376a65f7233f2b2b8733fbb17de18330d69d4 [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/*
21 * Mapping of S_IF* types to NFS file types
22 */
Chuck Lever2c42f802020-10-21 11:58:41 -040023static const u32 nfs3_ftypes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 NF3NON, NF3FIFO, NF3CHR, NF3BAD,
25 NF3DIR, NF3BAD, NF3BLK, NF3BAD,
26 NF3REG, NF3BAD, NF3LNK, NF3BAD,
27 NF3SOCK, NF3BAD, NF3LNK, NF3BAD,
28};
29
Trond Myklebust27c438f2019-09-02 13:02:56 -040030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031/*
Chuck Lever95753632020-10-20 14:30:02 -040032 * Basic NFSv3 data types (RFC 1813 Sections 2.5 and 2.6)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
Chuck Lever95753632020-10-20 14:30:02 -040034
Adrian Bunk3ee6f612006-12-06 20:40:23 -080035static __be32 *
Arnd Bergmann92c5e462019-10-31 14:55:32 +010036encode_time3(__be32 *p, struct timespec64 *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
38 *p++ = htonl((u32) time->tv_sec); *p++ = htonl(time->tv_nsec);
39 return p;
40}
41
Chuck Lever2c42f802020-10-21 11:58:41 -040042static __be32 *
43encode_nfstime3(__be32 *p, const struct timespec64 *time)
44{
45 *p++ = cpu_to_be32((u32)time->tv_sec);
46 *p++ = cpu_to_be32(time->tv_nsec);
47
48 return p;
49}
50
Chuck Lever9cde9362020-10-20 15:48:22 -040051static bool
52svcxdr_decode_nfstime3(struct xdr_stream *xdr, struct timespec64 *timep)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Chuck Lever9cde9362020-10-20 15:48:22 -040054 __be32 *p;
55
56 p = xdr_inline_decode(xdr, XDR_UNIT * 2);
57 if (!p)
58 return false;
59 timep->tv_sec = be32_to_cpup(p++);
60 timep->tv_nsec = be32_to_cpup(p);
61
62 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
Chuck Lever05027ea2020-11-17 11:52:04 -050065/**
66 * svcxdr_decode_nfs_fh3 - Decode an NFSv3 file handle
67 * @xdr: XDR stream positioned at an undecoded NFSv3 FH
68 * @fhp: OUT: filled-in server file handle
69 *
70 * Return values:
71 * %false: The encoded file handle was not valid
72 * %true: @fhp has been initialized
73 */
74bool
Chuck Lever95753632020-10-20 14:30:02 -040075svcxdr_decode_nfs_fh3(struct xdr_stream *xdr, struct svc_fh *fhp)
76{
77 __be32 *p;
78 u32 size;
79
80 if (xdr_stream_decode_u32(xdr, &size) < 0)
81 return false;
82 if (size == 0 || size > NFS3_FHSIZE)
83 return false;
84 p = xdr_inline_decode(xdr, size);
85 if (!p)
86 return false;
87 fh_init(fhp, NFS3_FHSIZE);
88 fhp->fh_handle.fh_size = size;
89 memcpy(&fhp->fh_handle.fh_base, p, size);
90
91 return true;
92}
93
Chuck Lever2c42f802020-10-21 11:58:41 -040094static bool
95svcxdr_encode_nfsstat3(struct xdr_stream *xdr, __be32 status)
96{
97 __be32 *p;
98
99 p = xdr_reserve_space(xdr, sizeof(status));
100 if (!p)
101 return false;
102 *p = status;
103
104 return true;
105}
106
Chuck Lever5cf35332020-10-22 14:46:58 -0400107static bool
108svcxdr_encode_nfs_fh3(struct xdr_stream *xdr, const struct svc_fh *fhp)
109{
110 u32 size = fhp->fh_handle.fh_size;
111 __be32 *p;
112
113 p = xdr_reserve_space(xdr, XDR_UNIT + size);
114 if (!p)
115 return false;
116 *p++ = cpu_to_be32(size);
117 if (size)
118 p[XDR_QUADLEN(size) - 1] = 0;
119 memcpy(p, &fhp->fh_handle.fh_base, size);
120
121 return true;
122}
123
Chuck Lever78315b32020-10-22 15:27:23 -0400124static bool
125svcxdr_encode_post_op_fh3(struct xdr_stream *xdr, const struct svc_fh *fhp)
126{
127 if (xdr_stream_encode_item_present(xdr) < 0)
128 return false;
129 if (!svcxdr_encode_nfs_fh3(xdr, fhp))
130 return false;
131
132 return true;
133}
134
Adrian Bunk3ee6f612006-12-06 20:40:23 -0800135static __be32 *
Al Viro91f07162006-10-19 23:28:57 -0700136encode_fh(__be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 unsigned int size = fhp->fh_handle.fh_size;
139 *p++ = htonl(size);
140 if (size) p[XDR_QUADLEN(size)-1]=0;
141 memcpy(p, &fhp->fh_handle.fh_base, size);
142 return p + XDR_QUADLEN(size);
143}
144
Chuck Lever54d1d432020-10-20 15:42:33 -0400145static bool
Chuck Leverecb7a082020-10-22 15:26:31 -0400146svcxdr_encode_writeverf3(struct xdr_stream *xdr, const __be32 *verf)
147{
148 __be32 *p;
149
150 p = xdr_reserve_space(xdr, NFS3_WRITEVERFSIZE);
151 if (!p)
152 return false;
153 memcpy(p, verf, NFS3_WRITEVERFSIZE);
154
155 return true;
156}
157
158static bool
Chuck Lever54d1d432020-10-20 15:42:33 -0400159svcxdr_decode_filename3(struct xdr_stream *xdr, char **name, unsigned int *len)
160{
161 u32 size, i;
162 __be32 *p;
163 char *c;
164
165 if (xdr_stream_decode_u32(xdr, &size) < 0)
166 return false;
167 if (size == 0 || size > NFS3_MAXNAMLEN)
168 return false;
169 p = xdr_inline_decode(xdr, size);
170 if (!p)
171 return false;
172
173 *len = size;
174 *name = (char *)p;
175 for (i = 0, c = *name; i < size; i++, c++) {
176 if (*c == '\0' || *c == '/')
177 return false;
178 }
179
180 return true;
181}
182
183static bool
184svcxdr_decode_diropargs3(struct xdr_stream *xdr, struct svc_fh *fhp,
185 char **name, unsigned int *len)
186{
187 return svcxdr_decode_nfs_fh3(xdr, fhp) &&
188 svcxdr_decode_filename3(xdr, name, len);
189}
190
Chuck Lever9cde9362020-10-20 15:48:22 -0400191static bool
192svcxdr_decode_sattr3(struct svc_rqst *rqstp, struct xdr_stream *xdr,
193 struct iattr *iap)
194{
195 u32 set_it;
196
197 iap->ia_valid = 0;
198
199 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
200 return false;
201 if (set_it) {
202 u32 mode;
203
204 if (xdr_stream_decode_u32(xdr, &mode) < 0)
205 return false;
206 iap->ia_valid |= ATTR_MODE;
207 iap->ia_mode = mode;
208 }
209 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
210 return false;
211 if (set_it) {
212 u32 uid;
213
214 if (xdr_stream_decode_u32(xdr, &uid) < 0)
215 return false;
216 iap->ia_uid = make_kuid(nfsd_user_namespace(rqstp), uid);
217 if (uid_valid(iap->ia_uid))
218 iap->ia_valid |= ATTR_UID;
219 }
220 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
221 return false;
222 if (set_it) {
223 u32 gid;
224
225 if (xdr_stream_decode_u32(xdr, &gid) < 0)
226 return false;
227 iap->ia_gid = make_kgid(nfsd_user_namespace(rqstp), gid);
228 if (gid_valid(iap->ia_gid))
229 iap->ia_valid |= ATTR_GID;
230 }
231 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
232 return false;
233 if (set_it) {
234 u64 newsize;
235
236 if (xdr_stream_decode_u64(xdr, &newsize) < 0)
237 return false;
238 iap->ia_valid |= ATTR_SIZE;
239 iap->ia_size = min_t(u64, newsize, NFS_OFFSET_MAX);
240 }
241 if (xdr_stream_decode_u32(xdr, &set_it) < 0)
242 return false;
243 switch (set_it) {
244 case DONT_CHANGE:
245 break;
246 case SET_TO_SERVER_TIME:
247 iap->ia_valid |= ATTR_ATIME;
248 break;
249 case SET_TO_CLIENT_TIME:
250 if (!svcxdr_decode_nfstime3(xdr, &iap->ia_atime))
251 return false;
252 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
253 break;
254 default:
255 return false;
256 }
257 if (xdr_stream_decode_u32(xdr, &set_it) < 0)
258 return false;
259 switch (set_it) {
260 case DONT_CHANGE:
261 break;
262 case SET_TO_SERVER_TIME:
263 iap->ia_valid |= ATTR_MTIME;
264 break;
265 case SET_TO_CLIENT_TIME:
266 if (!svcxdr_decode_nfstime3(xdr, &iap->ia_mtime))
267 return false;
268 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
269 break;
270 default:
271 return false;
272 }
273
274 return true;
275}
276
277static bool
278svcxdr_decode_sattrguard3(struct xdr_stream *xdr, struct nfsd3_sattrargs *args)
279{
280 __be32 *p;
281 u32 check;
282
283 if (xdr_stream_decode_bool(xdr, &check) < 0)
284 return false;
285 if (check) {
286 p = xdr_inline_decode(xdr, XDR_UNIT * 2);
287 if (!p)
288 return false;
289 args->check_guard = 1;
290 args->guardtime = be32_to_cpup(p);
291 } else
292 args->check_guard = 0;
293
294 return true;
295}
296
Chuck Leverf8a38e22020-10-20 17:04:03 -0400297static bool
298svcxdr_decode_specdata3(struct xdr_stream *xdr, struct nfsd3_mknodargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Chuck Leverf8a38e22020-10-20 17:04:03 -0400300 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Chuck Leverf8a38e22020-10-20 17:04:03 -0400302 p = xdr_inline_decode(xdr, XDR_UNIT * 2);
303 if (!p)
304 return false;
305 args->major = be32_to_cpup(p++);
306 args->minor = be32_to_cpup(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Chuck Leverf8a38e22020-10-20 17:04:03 -0400308 return true;
309}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Chuck Leverf8a38e22020-10-20 17:04:03 -0400311static bool
312svcxdr_decode_devicedata3(struct svc_rqst *rqstp, struct xdr_stream *xdr,
313 struct nfsd3_mknodargs *args)
314{
315 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs) &&
316 svcxdr_decode_specdata3(xdr, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Chuck Lever2c42f802020-10-21 11:58:41 -0400319static bool
320svcxdr_encode_fattr3(struct svc_rqst *rqstp, struct xdr_stream *xdr,
321 const struct svc_fh *fhp, const struct kstat *stat)
322{
323 struct user_namespace *userns = nfsd_user_namespace(rqstp);
324 __be32 *p;
325 u64 fsid;
326
327 p = xdr_reserve_space(xdr, XDR_UNIT * 21);
328 if (!p)
329 return false;
330
331 *p++ = cpu_to_be32(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]);
332 *p++ = cpu_to_be32((u32)(stat->mode & S_IALLUGO));
333 *p++ = cpu_to_be32((u32)stat->nlink);
334 *p++ = cpu_to_be32((u32)from_kuid_munged(userns, stat->uid));
335 *p++ = cpu_to_be32((u32)from_kgid_munged(userns, stat->gid));
336 if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN)
337 p = xdr_encode_hyper(p, (u64)NFS3_MAXPATHLEN);
338 else
339 p = xdr_encode_hyper(p, (u64)stat->size);
340
341 /* used */
342 p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
343
344 /* rdev */
345 *p++ = cpu_to_be32((u32)MAJOR(stat->rdev));
346 *p++ = cpu_to_be32((u32)MINOR(stat->rdev));
347
348 switch(fsid_source(fhp)) {
349 case FSIDSOURCE_FSID:
350 fsid = (u64)fhp->fh_export->ex_fsid;
351 break;
352 case FSIDSOURCE_UUID:
353 fsid = ((u64 *)fhp->fh_export->ex_uuid)[0];
354 fsid ^= ((u64 *)fhp->fh_export->ex_uuid)[1];
355 break;
356 default:
357 fsid = (u64)huge_encode_dev(fhp->fh_dentry->d_sb->s_dev);
358 }
359 p = xdr_encode_hyper(p, fsid);
360
361 /* fileid */
362 p = xdr_encode_hyper(p, stat->ino);
363
364 p = encode_nfstime3(p, &stat->atime);
365 p = encode_nfstime3(p, &stat->mtime);
366 encode_nfstime3(p, &stat->ctime);
367
368 return true;
369}
370
NeilBrownaf6a4e22007-02-14 00:33:12 -0800371static __be32 *encode_fsid(__be32 *p, struct svc_fh *fhp)
372{
373 u64 f;
374 switch(fsid_source(fhp)) {
375 default:
376 case FSIDSOURCE_DEV:
377 p = xdr_encode_hyper(p, (u64)huge_encode_dev
Al Virofc640052016-04-10 01:33:30 -0400378 (fhp->fh_dentry->d_sb->s_dev));
NeilBrownaf6a4e22007-02-14 00:33:12 -0800379 break;
380 case FSIDSOURCE_FSID:
381 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
382 break;
383 case FSIDSOURCE_UUID:
384 f = ((u64*)fhp->fh_export->ex_uuid)[0];
385 f ^= ((u64*)fhp->fh_export->ex_uuid)[1];
386 p = xdr_encode_hyper(p, f);
387 break;
388 }
389 return p;
390}
391
Adrian Bunk3ee6f612006-12-06 20:40:23 -0800392static __be32 *
Al Viro91f07162006-10-19 23:28:57 -0700393encode_fattr3(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp,
David Shawa334de22006-01-06 00:19:58 -0800394 struct kstat *stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400396 struct user_namespace *userns = nfsd_user_namespace(rqstp);
David Shawa334de22006-01-06 00:19:58 -0800397 *p++ = htonl(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]);
Albert Fluegel6e14b462013-11-18 12:18:01 -0500398 *p++ = htonl((u32) (stat->mode & S_IALLUGO));
David Shawa334de22006-01-06 00:19:58 -0800399 *p++ = htonl((u32) stat->nlink);
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400400 *p++ = htonl((u32) from_kuid_munged(userns, stat->uid));
401 *p++ = htonl((u32) from_kgid_munged(userns, stat->gid));
David Shawa334de22006-01-06 00:19:58 -0800402 if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
404 } else {
David Shawa334de22006-01-06 00:19:58 -0800405 p = xdr_encode_hyper(p, (u64) stat->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
David Shawa334de22006-01-06 00:19:58 -0800407 p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
408 *p++ = htonl((u32) MAJOR(stat->rdev));
409 *p++ = htonl((u32) MINOR(stat->rdev));
NeilBrownaf6a4e22007-02-14 00:33:12 -0800410 p = encode_fsid(p, fhp);
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400411 p = xdr_encode_hyper(p, stat->ino);
Arnd Bergmann92c5e462019-10-31 14:55:32 +0100412 p = encode_time3(p, &stat->atime);
413 p = encode_time3(p, &stat->mtime);
414 p = encode_time3(p, &stat->ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 return p;
417}
418
Adrian Bunk3ee6f612006-12-06 20:40:23 -0800419static __be32 *
Al Viro91f07162006-10-19 23:28:57 -0700420encode_saved_post_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 /* Attributes to follow */
423 *p++ = xdr_one;
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400424 return encode_fattr3(rqstp, p, fhp, &fhp->fh_post_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
Chuck Lever907c3822020-10-22 13:56:58 -0400427static bool
Chuck Lever70f8e832020-10-22 15:12:38 -0400428svcxdr_encode_wcc_attr(struct xdr_stream *xdr, const struct svc_fh *fhp)
429{
430 __be32 *p;
431
432 p = xdr_reserve_space(xdr, XDR_UNIT * 6);
433 if (!p)
434 return false;
435 p = xdr_encode_hyper(p, (u64)fhp->fh_pre_size);
436 p = encode_nfstime3(p, &fhp->fh_pre_mtime);
437 encode_nfstime3(p, &fhp->fh_pre_ctime);
438
439 return true;
440}
441
442static bool
443svcxdr_encode_pre_op_attr(struct xdr_stream *xdr, const struct svc_fh *fhp)
444{
445 if (!fhp->fh_pre_saved) {
446 if (xdr_stream_encode_item_absent(xdr) < 0)
447 return false;
448 return true;
449 }
450
451 if (xdr_stream_encode_item_present(xdr) < 0)
452 return false;
453 return svcxdr_encode_wcc_attr(xdr, fhp);
454}
455
456static bool
Chuck Lever907c3822020-10-22 13:56:58 -0400457svcxdr_encode_post_op_attr(struct svc_rqst *rqstp, struct xdr_stream *xdr,
458 const struct svc_fh *fhp)
459{
460 struct dentry *dentry = fhp->fh_dentry;
461 struct kstat stat;
462
463 /*
464 * The inode may be NULL if the call failed because of a
465 * stale file handle. In this case, no attributes are
466 * returned.
467 */
468 if (fhp->fh_no_wcc || !dentry || !d_really_is_positive(dentry))
469 goto no_post_op_attrs;
470 if (fh_getattr(fhp, &stat) != nfs_ok)
471 goto no_post_op_attrs;
472
473 if (xdr_stream_encode_item_present(xdr) < 0)
474 return false;
475 lease_get_mtime(d_inode(dentry), &stat.mtime);
476 if (!svcxdr_encode_fattr3(rqstp, xdr, fhp, &stat))
477 return false;
478
479 return true;
480
481no_post_op_attrs:
482 return xdr_stream_encode_item_absent(xdr) > 0;
483}
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485/*
486 * Encode post-operation attributes.
487 * The inode may be NULL if the call failed because of a stale file
488 * handle. In this case, no attributes are returned.
489 */
Al Viro91f07162006-10-19 23:28:57 -0700490static __be32 *
491encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 struct dentry *dentry = fhp->fh_dentry;
Jeff Laytondaab1102020-11-30 17:03:14 -0500494 if (!fhp->fh_no_wcc && dentry && d_really_is_positive(dentry)) {
Al Viro3dadecc2013-01-24 02:18:08 -0500495 __be32 err;
David Shawa334de22006-01-06 00:19:58 -0800496 struct kstat stat;
497
Al Viro3dadecc2013-01-24 02:18:08 -0500498 err = fh_getattr(fhp, &stat);
David Shawa334de22006-01-06 00:19:58 -0800499 if (!err) {
500 *p++ = xdr_one; /* attributes follow */
David Howells2b0143b2015-03-17 22:25:59 +0000501 lease_get_mtime(d_inode(dentry), &stat.mtime);
David Shawa334de22006-01-06 00:19:58 -0800502 return encode_fattr3(rqstp, p, fhp, &stat);
503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505 *p++ = xdr_zero;
506 return p;
507}
508
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000509/* Helper for NFSv3 ACLs */
Al Viro91f07162006-10-19 23:28:57 -0700510__be32 *
511nfs3svc_encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000512{
513 return encode_post_op_attr(rqstp, p, fhp);
514}
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516/*
Chuck Lever70f8e832020-10-22 15:12:38 -0400517 * Encode weak cache consistency data
518 */
519static bool
520svcxdr_encode_wcc_data(struct svc_rqst *rqstp, struct xdr_stream *xdr,
521 const struct svc_fh *fhp)
522{
523 struct dentry *dentry = fhp->fh_dentry;
524
525 if (!dentry || !d_really_is_positive(dentry) || !fhp->fh_post_saved)
526 goto neither;
527
528 /* before */
529 if (!svcxdr_encode_pre_op_attr(xdr, fhp))
530 return false;
531
532 /* after */
533 if (xdr_stream_encode_item_present(xdr) < 0)
534 return false;
535 if (!svcxdr_encode_fattr3(rqstp, xdr, fhp, &fhp->fh_post_attr))
536 return false;
537
538 return true;
539
540neither:
541 if (xdr_stream_encode_item_absent(xdr) < 0)
542 return false;
543 if (!svcxdr_encode_post_op_attr(rqstp, xdr, fhp))
544 return false;
545
546 return true;
547}
548
549/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 * Enocde weak cache consistency data
551 */
Al Viro91f07162006-10-19 23:28:57 -0700552static __be32 *
553encode_wcc_data(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
555 struct dentry *dentry = fhp->fh_dentry;
556
David Howells2b0143b2015-03-17 22:25:59 +0000557 if (dentry && d_really_is_positive(dentry) && fhp->fh_post_saved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (fhp->fh_pre_saved) {
559 *p++ = xdr_one;
560 p = xdr_encode_hyper(p, (u64) fhp->fh_pre_size);
561 p = encode_time3(p, &fhp->fh_pre_mtime);
562 p = encode_time3(p, &fhp->fh_pre_ctime);
563 } else {
564 *p++ = xdr_zero;
565 }
566 return encode_saved_post_attr(rqstp, p, fhp);
567 }
568 /* no pre- or post-attrs */
569 *p++ = xdr_zero;
570 return encode_post_op_attr(rqstp, p, fhp);
571}
572
J. Bruce Fields428a23d2021-01-29 14:27:01 -0500573static bool fs_supports_change_attribute(struct super_block *sb)
574{
575 return sb->s_flags & SB_I_VERSION || sb->s_export_op->fetch_iversion;
576}
577
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400578/*
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200579 * Fill in the pre_op attr for the wcc data
580 */
581void fill_pre_wcc(struct svc_fh *fhp)
582{
583 struct inode *inode;
584 struct kstat stat;
J. Bruce Fields942b20dc2020-11-30 17:46:17 -0500585 bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200586
Jeff Laytondaab1102020-11-30 17:03:14 -0500587 if (fhp->fh_no_wcc || fhp->fh_pre_saved)
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200588 return;
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200589 inode = d_inode(fhp->fh_dentry);
J. Bruce Fields428a23d2021-01-29 14:27:01 -0500590 if (fs_supports_change_attribute(inode->i_sb) || !v4) {
591 __be32 err = fh_getattr(fhp, &stat);
592
593 if (err) {
594 /* Grab the times from inode anyway */
595 stat.mtime = inode->i_mtime;
596 stat.ctime = inode->i_ctime;
597 stat.size = inode->i_size;
598 }
599 fhp->fh_pre_mtime = stat.mtime;
600 fhp->fh_pre_ctime = stat.ctime;
601 fhp->fh_pre_size = stat.size;
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200602 }
J. Bruce Fields942b20dc2020-11-30 17:46:17 -0500603 if (v4)
604 fhp->fh_pre_change = nfsd4_change_attribute(&stat, inode);
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200605
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200606 fhp->fh_pre_saved = true;
607}
608
609/*
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400610 * Fill in the post_op attr for the wcc data
611 */
612void fill_post_wcc(struct svc_fh *fhp)
613{
J. Bruce Fields942b20dc2020-11-30 17:46:17 -0500614 bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
615 struct inode *inode = d_inode(fhp->fh_dentry);
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400616
Jeff Laytondaab1102020-11-30 17:03:14 -0500617 if (fhp->fh_no_wcc)
618 return;
619
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400620 if (fhp->fh_post_saved)
621 printk("nfsd: inode locked twice during operation.\n");
622
J. Bruce Fields428a23d2021-01-29 14:27:01 -0500623 fhp->fh_post_saved = true;
624
625 if (fs_supports_change_attribute(inode->i_sb) || !v4) {
626 __be32 err = fh_getattr(fhp, &fhp->fh_post_attr);
627
628 if (err) {
629 fhp->fh_post_saved = false;
630 fhp->fh_post_attr.ctime = inode->i_ctime;
631 }
632 }
J. Bruce Fields942b20dc2020-11-30 17:46:17 -0500633 if (v4)
634 fhp->fh_post_change =
635 nfsd4_change_attribute(&fhp->fh_post_attr, inode);
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400636}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638/*
639 * XDR decode functions
640 */
Chuck Leverdcc46992020-10-01 18:59:12 -0400641
642int
Chuck Lever95753632020-10-20 14:30:02 -0400643nfs3svc_decode_fhandleargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Chuck Lever95753632020-10-20 14:30:02 -0400645 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200646 struct nfsd_fhandle *args = rqstp->rq_argp;
647
Chuck Lever95753632020-10-20 14:30:02 -0400648 return svcxdr_decode_nfs_fh3(xdr, &args->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
651int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200652nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Chuck Lever9cde9362020-10-20 15:48:22 -0400654 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200655 struct nfsd3_sattrargs *args = rqstp->rq_argp;
656
Chuck Lever9cde9362020-10-20 15:48:22 -0400657 return svcxdr_decode_nfs_fh3(xdr, &args->fh) &&
658 svcxdr_decode_sattr3(rqstp, xdr, &args->attrs) &&
659 svcxdr_decode_sattrguard3(xdr, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
662int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200663nfs3svc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Chuck Lever54d1d432020-10-20 15:42:33 -0400665 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200666 struct nfsd3_diropargs *args = rqstp->rq_argp;
667
Chuck Lever54d1d432020-10-20 15:42:33 -0400668 return svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
671int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200672nfs3svc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Chuck Lever3b921a22020-10-20 14:32:04 -0400674 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200675 struct nfsd3_accessargs *args = rqstp->rq_argp;
676
Chuck Lever3b921a22020-10-20 14:32:04 -0400677 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return 0;
Chuck Lever3b921a22020-10-20 14:32:04 -0400679 if (xdr_stream_decode_u32(xdr, &args->access) < 0)
680 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Chuck Lever3b921a22020-10-20 14:32:04 -0400682 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
685int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200686nfs3svc_decode_readargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Chuck Leverbe63bd22020-10-20 14:34:40 -0400688 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200689 struct nfsd3_readargs *args = rqstp->rq_argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Chuck Leverbe63bd22020-10-20 14:34:40 -0400691 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return 0;
Chuck Leverbe63bd22020-10-20 14:34:40 -0400693 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
694 return 0;
695 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
696 return 0;
J. Bruce Fields9512a162017-05-16 15:57:42 -0400697
Chuck Leverbe63bd22020-10-20 14:34:40 -0400698 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
701int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200702nfs3svc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Chuck Leverc43b2f22020-10-22 11:14:55 -0400704 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200705 struct nfsd3_writeargs *args = rqstp->rq_argp;
Greg Banks7adae482006-10-04 02:15:47 -0700706 u32 max_blocksize = svc_max_payload(rqstp);
J. Bruce Fieldsdb44bac2017-04-25 16:21:34 -0400707 struct kvec *head = rqstp->rq_arg.head;
708 struct kvec *tail = rqstp->rq_arg.tail;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400709 size_t remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Chuck Leverc43b2f22020-10-22 11:14:55 -0400711 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return 0;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400713 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
714 return 0;
715 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
716 return 0;
717 if (xdr_stream_decode_u32(xdr, &args->stable) < 0)
718 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Chuck Leverc43b2f22020-10-22 11:14:55 -0400720 /* opaque data */
721 if (xdr_stream_decode_u32(xdr, &args->len) < 0)
J. Bruce Fields13bf9fb2017-04-21 15:26:30 -0400722 return 0;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400723
724 /* request sanity */
Peter Staubachf34b95682007-05-09 02:34:48 -0700725 if (args->count != args->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return 0;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400727 remaining = head->iov_len + rqstp->rq_arg.page_len + tail->iov_len;
728 remaining -= xdr_stream_pos(xdr);
729 if (remaining < xdr_align_size(args->len))
Peter Staubachf34b95682007-05-09 02:34:48 -0700730 return 0;
Peter Staubachf34b95682007-05-09 02:34:48 -0700731 if (args->count > max_blocksize) {
732 args->count = max_blocksize;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400733 args->len = max_blocksize;
Peter Staubachf34b95682007-05-09 02:34:48 -0700734 }
Chuck Lever8154ef22018-03-27 10:54:07 -0400735
Chuck Leverc43b2f22020-10-22 11:14:55 -0400736 args->first.iov_base = xdr->p;
737 args->first.iov_len = head->iov_len - xdr_stream_pos(xdr);
738
Peter Staubachf34b95682007-05-09 02:34:48 -0700739 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
742int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200743nfs3svc_decode_createargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Chuck Lever6b3a1192020-10-20 15:56:11 -0400745 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200746 struct nfsd3_createargs *args = rqstp->rq_argp;
747
Chuck Lever6b3a1192020-10-20 15:56:11 -0400748 if (!svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return 0;
Chuck Lever6b3a1192020-10-20 15:56:11 -0400750 if (xdr_stream_decode_u32(xdr, &args->createmode) < 0)
751 return 0;
752 switch (args->createmode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 case NFS3_CREATE_UNCHECKED:
754 case NFS3_CREATE_GUARDED:
Chuck Lever6b3a1192020-10-20 15:56:11 -0400755 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 case NFS3_CREATE_EXCLUSIVE:
Chuck Lever6b3a1192020-10-20 15:56:11 -0400757 args->verf = xdr_inline_decode(xdr, NFS3_CREATEVERFSIZE);
758 if (!args->verf)
759 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 break;
761 default:
762 return 0;
763 }
Chuck Lever6b3a1192020-10-20 15:56:11 -0400764 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
Christoph Hellwig026fec72017-05-08 19:01:48 +0200766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200768nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Chuck Lever83374c22020-10-20 17:02:16 -0400770 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200771 struct nfsd3_createargs *args = rqstp->rq_argp;
772
Chuck Lever83374c22020-10-20 17:02:16 -0400773 return svcxdr_decode_diropargs3(xdr, &args->fh,
774 &args->name, &args->len) &&
775 svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
778int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200779nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Chuck Leverda392012020-10-20 16:01:16 -0400781 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200782 struct nfsd3_symlinkargs *args = rqstp->rq_argp;
Chuck Leverda392012020-10-20 16:01:16 -0400783 struct kvec *head = rqstp->rq_arg.head;
784 struct kvec *tail = rqstp->rq_arg.tail;
785 size_t remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Chuck Leverda392012020-10-20 16:01:16 -0400787 if (!svcxdr_decode_diropargs3(xdr, &args->ffh, &args->fname, &args->flen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return 0;
Chuck Leverda392012020-10-20 16:01:16 -0400789 if (!svcxdr_decode_sattr3(rqstp, xdr, &args->attrs))
Chuck Lever38a70312018-03-27 10:54:21 -0400790 return 0;
Chuck Leverda392012020-10-20 16:01:16 -0400791 if (xdr_stream_decode_u32(xdr, &args->tlen) < 0)
792 return 0;
793
794 /* request sanity */
795 remaining = head->iov_len + rqstp->rq_arg.page_len + tail->iov_len;
796 remaining -= xdr_stream_pos(xdr);
797 if (remaining < xdr_align_size(args->tlen))
798 return 0;
799
800 args->first.iov_base = xdr->p;
801 args->first.iov_len = head->iov_len - xdr_stream_pos(xdr);
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return 1;
804}
805
806int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200807nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Chuck Leverf8a38e22020-10-20 17:04:03 -0400809 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200810 struct nfsd3_mknodargs *args = rqstp->rq_argp;
811
Chuck Leverf8a38e22020-10-20 17:04:03 -0400812 if (!svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return 0;
Chuck Leverf8a38e22020-10-20 17:04:03 -0400814 if (xdr_stream_decode_u32(xdr, &args->ftype) < 0)
815 return 0;
816 switch (args->ftype) {
817 case NF3CHR:
818 case NF3BLK:
819 return svcxdr_decode_devicedata3(rqstp, xdr, args);
820 case NF3SOCK:
821 case NF3FIFO:
822 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
823 case NF3REG:
824 case NF3DIR:
825 case NF3LNK:
826 /* Valid XDR but illegal file types */
827 break;
828 default:
829 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831
Chuck Leverf8a38e22020-10-20 17:04:03 -0400832 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
835int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200836nfs3svc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Chuck Leverd181e0a2020-10-20 15:44:12 -0400838 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200839 struct nfsd3_renameargs *args = rqstp->rq_argp;
840
Chuck Leverd181e0a2020-10-20 15:44:12 -0400841 return svcxdr_decode_diropargs3(xdr, &args->ffh,
842 &args->fname, &args->flen) &&
843 svcxdr_decode_diropargs3(xdr, &args->tfh,
844 &args->tname, &args->tlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
847int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200848nfs3svc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Chuck Leverefaa1e72020-10-19 13:26:32 -0400850 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200851 struct nfsd3_linkargs *args = rqstp->rq_argp;
852
Chuck Leverefaa1e72020-10-19 13:26:32 -0400853 return svcxdr_decode_nfs_fh3(xdr, &args->ffh) &&
854 svcxdr_decode_diropargs3(xdr, &args->tfh,
855 &args->tname, &args->tlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
858int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200859nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400861 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200862 struct nfsd3_readdirargs *args = rqstp->rq_argp;
NeilBrownf875a792019-03-07 09:49:46 +1100863
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400864 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return 0;
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400866 if (xdr_stream_decode_u64(xdr, &args->cookie) < 0)
867 return 0;
868 args->verf = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE);
869 if (!args->verf)
870 return 0;
871 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
872 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400874 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
876
877int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200878nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400880 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200881 struct nfsd3_readdirargs *args = rqstp->rq_argp;
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400882 u32 dircount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400884 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return 0;
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400886 if (xdr_stream_decode_u64(xdr, &args->cookie) < 0)
887 return 0;
888 args->verf = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE);
889 if (!args->verf)
890 return 0;
891 /* dircount is ignored */
892 if (xdr_stream_decode_u32(xdr, &dircount) < 0)
893 return 0;
894 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
895 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400897 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
900int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200901nfs3svc_decode_commitargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Chuck Leverc8d26a02020-10-20 14:41:56 -0400903 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200904 struct nfsd3_commitargs *args = rqstp->rq_argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Chuck Leverc8d26a02020-10-20 14:41:56 -0400906 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
907 return 0;
908 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
909 return 0;
910 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
911 return 0;
912
913 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
916/*
917 * XDR encode functions
918 */
Chuck Levercc028a12020-10-02 15:52:44 -0400919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920/* GETATTR */
921int
Chuck Lever2c42f802020-10-21 11:58:41 -0400922nfs3svc_encode_getattrres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Chuck Lever2c42f802020-10-21 11:58:41 -0400924 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200925 struct nfsd3_attrstat *resp = rqstp->rq_resp;
926
Chuck Lever2c42f802020-10-21 11:58:41 -0400927 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
928 return 0;
929 switch (resp->status) {
930 case nfs_ok:
931 lease_get_mtime(d_inode(resp->fh.fh_dentry), &resp->stat.mtime);
932 if (!svcxdr_encode_fattr3(rqstp, xdr, &resp->fh, &resp->stat))
933 return 0;
934 break;
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400935 }
Chuck Lever2c42f802020-10-21 11:58:41 -0400936
937 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
940/* SETATTR, REMOVE, RMDIR */
941int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200942nfs3svc_encode_wccstat(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Chuck Lever70f8e832020-10-22 15:12:38 -0400944 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200945 struct nfsd3_attrstat *resp = rqstp->rq_resp;
946
Chuck Lever70f8e832020-10-22 15:12:38 -0400947 return svcxdr_encode_nfsstat3(xdr, resp->status) &&
948 svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
951/* LOOKUP */
Chuck Lever5cf35332020-10-22 14:46:58 -0400952int nfs3svc_encode_lookupres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Chuck Lever5cf35332020-10-22 14:46:58 -0400954 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200955 struct nfsd3_diropres *resp = rqstp->rq_resp;
956
Chuck Lever5cf35332020-10-22 14:46:58 -0400957 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
958 return 0;
959 switch (resp->status) {
960 case nfs_ok:
961 if (!svcxdr_encode_nfs_fh3(xdr, &resp->fh))
962 return 0;
963 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
964 return 0;
965 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->dirfh))
966 return 0;
967 break;
968 default:
969 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->dirfh))
970 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
Chuck Lever5cf35332020-10-22 14:46:58 -0400972
973 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
976/* ACCESS */
977int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200978nfs3svc_encode_accessres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Chuck Lever907c3822020-10-22 13:56:58 -0400980 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200981 struct nfsd3_accessres *resp = rqstp->rq_resp;
982
Chuck Lever907c3822020-10-22 13:56:58 -0400983 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
984 return 0;
985 switch (resp->status) {
986 case nfs_ok:
987 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
988 return 0;
989 if (xdr_stream_encode_u32(xdr, resp->access) < 0)
990 return 0;
991 break;
992 default:
993 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
994 return 0;
995 }
996
997 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
999
1000/* READLINK */
1001int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001002nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Chuck Lever9a9c8922020-10-22 15:18:40 -04001004 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001005 struct nfsd3_readlinkres *resp = rqstp->rq_resp;
Chuck Lever76e54922020-11-05 10:24:19 -05001006 struct kvec *head = rqstp->rq_res.head;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001007
Chuck Lever9a9c8922020-10-22 15:18:40 -04001008 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1009 return 0;
1010 switch (resp->status) {
1011 case nfs_ok:
1012 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
Chuck Lever76e54922020-11-05 10:24:19 -05001013 return 0;
Chuck Lever9a9c8922020-10-22 15:18:40 -04001014 if (xdr_stream_encode_u32(xdr, resp->len) < 0)
1015 return 0;
1016 xdr_write_pages(xdr, resp->pages, 0, resp->len);
1017 if (svc_encode_result_payload(rqstp, head->iov_len, resp->len) < 0)
1018 return 0;
1019 break;
1020 default:
1021 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
1022 return 0;
1023 }
1024
1025 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
1028/* READ */
1029int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001030nfs3svc_encode_readres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Chuck Levercc9bcda2020-10-22 15:23:50 -04001032 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001033 struct nfsd3_readres *resp = rqstp->rq_resp;
Chuck Lever76e54922020-11-05 10:24:19 -05001034 struct kvec *head = rqstp->rq_res.head;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001035
Chuck Levercc9bcda2020-10-22 15:23:50 -04001036 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1037 return 0;
1038 switch (resp->status) {
1039 case nfs_ok:
1040 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
Chuck Lever76e54922020-11-05 10:24:19 -05001041 return 0;
Chuck Levercc9bcda2020-10-22 15:23:50 -04001042 if (xdr_stream_encode_u32(xdr, resp->count) < 0)
1043 return 0;
1044 if (xdr_stream_encode_bool(xdr, resp->eof) < 0)
1045 return 0;
1046 if (xdr_stream_encode_u32(xdr, resp->count) < 0)
1047 return 0;
1048 xdr_write_pages(xdr, resp->pages, rqstp->rq_res.page_base,
1049 resp->count);
1050 if (svc_encode_result_payload(rqstp, head->iov_len, resp->count) < 0)
1051 return 0;
1052 break;
1053 default:
1054 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
1055 return 0;
1056 }
1057
1058 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
1061/* WRITE */
1062int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001063nfs3svc_encode_writeres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Chuck Leverecb7a082020-10-22 15:26:31 -04001065 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001066 struct nfsd3_writeres *resp = rqstp->rq_resp;
Stanislav Kinsburskyb9c0ef82012-12-06 14:23:19 +03001067
Chuck Leverecb7a082020-10-22 15:26:31 -04001068 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1069 return 0;
1070 switch (resp->status) {
1071 case nfs_ok:
1072 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
1073 return 0;
1074 if (xdr_stream_encode_u32(xdr, resp->count) < 0)
1075 return 0;
1076 if (xdr_stream_encode_u32(xdr, resp->committed) < 0)
1077 return 0;
1078 if (!svcxdr_encode_writeverf3(xdr, resp->verf))
1079 return 0;
1080 break;
1081 default:
1082 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
1083 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
Chuck Leverecb7a082020-10-22 15:26:31 -04001085
1086 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
1089/* CREATE, MKDIR, SYMLINK, MKNOD */
1090int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001091nfs3svc_encode_createres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Chuck Lever78315b32020-10-22 15:27:23 -04001093 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001094 struct nfsd3_diropres *resp = rqstp->rq_resp;
1095
Chuck Lever78315b32020-10-22 15:27:23 -04001096 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1097 return 0;
1098 switch (resp->status) {
1099 case nfs_ok:
1100 if (!svcxdr_encode_post_op_fh3(xdr, &resp->fh))
1101 return 0;
1102 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
1103 return 0;
1104 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->dirfh))
1105 return 0;
1106 break;
1107 default:
1108 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->dirfh))
1109 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
Chuck Lever78315b32020-10-22 15:27:23 -04001111
1112 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
1115/* RENAME */
1116int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001117nfs3svc_encode_renameres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001119 struct nfsd3_renameres *resp = rqstp->rq_resp;
1120
Chuck Levercc028a12020-10-02 15:52:44 -04001121 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 p = encode_wcc_data(rqstp, p, &resp->ffh);
1123 p = encode_wcc_data(rqstp, p, &resp->tfh);
1124 return xdr_ressize_check(rqstp, p);
1125}
1126
1127/* LINK */
1128int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001129nfs3svc_encode_linkres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001131 struct nfsd3_linkres *resp = rqstp->rq_resp;
1132
Chuck Levercc028a12020-10-02 15:52:44 -04001133 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 p = encode_post_op_attr(rqstp, p, &resp->fh);
1135 p = encode_wcc_data(rqstp, p, &resp->tfh);
1136 return xdr_ressize_check(rqstp, p);
1137}
1138
1139/* READDIR */
1140int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001141nfs3svc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001143 struct nfsd3_readdirres *resp = rqstp->rq_resp;
1144
Chuck Levercc028a12020-10-02 15:52:44 -04001145 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 p = encode_post_op_attr(rqstp, p, &resp->fh);
1147
1148 if (resp->status == 0) {
1149 /* stupid readdir cookie */
1150 memcpy(p, resp->verf, 8); p += 2;
1151 xdr_ressize_check(rqstp, p);
1152 if (rqstp->rq_res.head[0].iov_len + (2<<2) > PAGE_SIZE)
1153 return 1; /*No room for trailer */
1154 rqstp->rq_res.page_len = (resp->count) << 2;
1155
1156 /* add the 'tail' to the end of the 'head' page - page 0. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 rqstp->rq_res.tail[0].iov_base = p;
1158 *p++ = 0; /* no more entries */
1159 *p++ = htonl(resp->common.err == nfserr_eof);
1160 rqstp->rq_res.tail[0].iov_len = 2<<2;
1161 return 1;
1162 } else
1163 return xdr_ressize_check(rqstp, p);
1164}
1165
Adrian Bunk3ee6f612006-12-06 20:40:23 -08001166static __be32 *
Al Viro91f07162006-10-19 23:28:57 -07001167encode_entry_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name,
Peter Staubach40ee5dc2007-08-16 12:10:07 -04001168 int namlen, u64 ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
1170 *p++ = xdr_one; /* mark entry present */
1171 p = xdr_encode_hyper(p, ino); /* file id */
1172 p = xdr_encode_array(p, name, namlen);/* name length & name */
1173
1174 cd->offset = p; /* remember pointer */
1175 p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
1176
1177 return p;
1178}
1179
Al Viroefe39652012-04-13 00:32:14 -04001180static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
NeilBrown43b0e7e2015-05-03 09:16:53 +10001182 const char *name, int namlen, u64 ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
1184 struct svc_export *exp;
1185 struct dentry *dparent, *dchild;
Al Viroefe39652012-04-13 00:32:14 -04001186 __be32 rv = nfserr_noent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 dparent = cd->fh.fh_dentry;
1189 exp = cd->fh.fh_export;
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 if (isdotent(name, namlen)) {
1192 if (namlen == 2) {
1193 dchild = dget_parent(dparent);
J. Bruce Fields51b2ee72021-01-11 16:01:29 -05001194 /*
1195 * Don't return filehandle for ".." if we're at
1196 * the filesystem or export root:
1197 */
Al Viroefe39652012-04-13 00:32:14 -04001198 if (dchild == dparent)
1199 goto out;
J. Bruce Fields51b2ee72021-01-11 16:01:29 -05001200 if (dparent == exp->ex_path.dentry)
1201 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 } else
1203 dchild = dget(dparent);
1204 } else
Al Viro6c2d47982019-10-31 01:21:58 -04001205 dchild = lookup_positive_unlocked(name, dparent, namlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 if (IS_ERR(dchild))
Al Viroefe39652012-04-13 00:32:14 -04001207 return rv;
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001208 if (d_mountpoint(dchild))
1209 goto out;
NeilBrown43b0e7e2015-05-03 09:16:53 +10001210 if (dchild->d_inode->i_ino != ino)
1211 goto out;
Al Viroefe39652012-04-13 00:32:14 -04001212 rv = fh_compose(fhp, exp, dchild, &cd->fh);
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001213out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 dput(dchild);
1215 return rv;
1216}
1217
NeilBrown43b0e7e2015-05-03 09:16:53 +10001218static __be32 *encode_entryplus_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name, int namlen, u64 ino)
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001219{
J. Bruce Fields068c34c2014-01-09 16:24:35 -05001220 struct svc_fh *fh = &cd->scratch;
Al Viroefe39652012-04-13 00:32:14 -04001221 __be32 err;
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001222
J. Bruce Fields068c34c2014-01-09 16:24:35 -05001223 fh_init(fh, NFS3_FHSIZE);
NeilBrown43b0e7e2015-05-03 09:16:53 +10001224 err = compose_entry_fh(cd, fh, name, namlen, ino);
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001225 if (err) {
1226 *p++ = 0;
1227 *p++ = 0;
J. Bruce Fieldsaed100f2009-09-04 14:40:36 -04001228 goto out;
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001229 }
J. Bruce Fields068c34c2014-01-09 16:24:35 -05001230 p = encode_post_op_attr(cd->rqstp, p, fh);
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001231 *p++ = xdr_one; /* yes, a file handle follows */
J. Bruce Fields068c34c2014-01-09 16:24:35 -05001232 p = encode_fh(p, fh);
J. Bruce Fieldsaed100f2009-09-04 14:40:36 -04001233out:
J. Bruce Fields068c34c2014-01-09 16:24:35 -05001234 fh_put(fh);
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001235 return p;
1236}
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238/*
1239 * Encode a directory entry. This one works for both normal readdir
1240 * and readdirplus.
1241 * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
1242 * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
1243 *
1244 * The readdirplus baggage is 1+21 words for post_op_attr, plus the
1245 * file handle.
1246 */
1247
1248#define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1)
1249#define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
1250static int
NeilBrown598b9a52007-03-26 21:32:08 -08001251encode_entry(struct readdir_cd *ccd, const char *name, int namlen,
Peter Staubach40ee5dc2007-08-16 12:10:07 -04001252 loff_t offset, u64 ino, unsigned int d_type, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
1254 struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
1255 common);
Al Viro91f07162006-10-19 23:28:57 -07001256 __be32 *p = cd->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 caddr_t curr_page_addr = NULL;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05001258 struct page ** page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 int slen; /* string (name) length */
1260 int elen; /* estimated entry length in words */
1261 int num_entry_words = 0; /* actual number of words */
1262
1263 if (cd->offset) {
1264 u64 offset64 = offset;
1265
1266 if (unlikely(cd->offset1)) {
1267 /* we ended up with offset on a page boundary */
1268 *cd->offset = htonl(offset64 >> 32);
1269 *cd->offset1 = htonl(offset64 & 0xffffffff);
1270 cd->offset1 = NULL;
1271 } else {
NeilBrown598b9a52007-03-26 21:32:08 -08001272 xdr_encode_hyper(cd->offset, offset64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
NeilBrownb6023452019-03-04 14:08:22 +11001274 cd->offset = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
1276
1277 /*
1278 dprintk("encode_entry(%.*s @%ld%s)\n",
1279 namlen, name, (long) offset, plus? " plus" : "");
1280 */
1281
1282 /* truncate filename if too long */
Kinglong Mee3c7aa152014-06-10 18:08:19 +08001283 namlen = min(namlen, NFS3_MAXNAMLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 slen = XDR_QUADLEN(namlen);
1286 elen = slen + NFS3_ENTRY_BAGGAGE
1287 + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
1288
1289 if (cd->buflen < elen) {
1290 cd->common.err = nfserr_toosmall;
1291 return -EINVAL;
1292 }
1293
1294 /* determine which page in rq_respages[] we are currently filling */
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05001295 for (page = cd->rqstp->rq_respages + 1;
1296 page < cd->rqstp->rq_next_page; page++) {
1297 curr_page_addr = page_address(*page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 if (((caddr_t)cd->buffer >= curr_page_addr) &&
1300 ((caddr_t)cd->buffer < curr_page_addr + PAGE_SIZE))
1301 break;
1302 }
1303
1304 if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
1305 /* encode entry in current page */
1306
1307 p = encode_entry_baggage(cd, p, name, namlen, ino);
1308
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001309 if (plus)
NeilBrown43b0e7e2015-05-03 09:16:53 +10001310 p = encode_entryplus_baggage(cd, p, name, namlen, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 num_entry_words = p - cd->buffer;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05001312 } else if (*(page+1) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 /* temporarily encode entry into next page, then move back to
1314 * current and next page in rq_respages[] */
Al Viro91f07162006-10-19 23:28:57 -07001315 __be32 *p1, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 int len1, len2;
1317
1318 /* grab next page for temporary storage of entry */
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05001319 p1 = tmp = page_address(*(page+1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
1322
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001323 if (plus)
NeilBrown43b0e7e2015-05-03 09:16:53 +10001324 p1 = encode_entryplus_baggage(cd, p1, name, namlen, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 /* determine entry word length and lengths to go in pages */
1327 num_entry_words = p1 - tmp;
1328 len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer;
1329 if ((num_entry_words << 2) < len1) {
1330 /* the actual number of words in the entry is less
1331 * than elen and can still fit in the current page
1332 */
1333 memmove(p, tmp, num_entry_words << 2);
1334 p += num_entry_words;
1335
1336 /* update offset */
1337 cd->offset = cd->buffer + (cd->offset - tmp);
1338 } else {
1339 unsigned int offset_r = (cd->offset - tmp) << 2;
1340
1341 /* update pointer to offset location.
1342 * This is a 64bit quantity, so we need to
1343 * deal with 3 cases:
1344 * - entirely in first page
1345 * - entirely in second page
1346 * - 4 bytes in each page
1347 */
1348 if (offset_r + 8 <= len1) {
1349 cd->offset = p + (cd->offset - tmp);
1350 } else if (offset_r >= len1) {
1351 cd->offset -= len1 >> 2;
1352 } else {
1353 /* sitting on the fence */
1354 BUG_ON(offset_r != len1 - 4);
1355 cd->offset = p + (cd->offset - tmp);
1356 cd->offset1 = tmp;
1357 }
1358
1359 len2 = (num_entry_words << 2) - len1;
1360
1361 /* move from temp page to current and next pages */
1362 memmove(p, tmp, len1);
1363 memmove(tmp, (caddr_t)tmp+len1, len2);
1364
1365 p = tmp + (len2 >> 2);
1366 }
1367 }
1368 else {
1369 cd->common.err = nfserr_toosmall;
1370 return -EINVAL;
1371 }
1372
1373 cd->buflen -= num_entry_words;
1374 cd->buffer = p;
1375 cd->common.err = nfs_ok;
1376 return 0;
1377
1378}
1379
1380int
NeilBrowna0ad13e2007-01-26 00:57:10 -08001381nfs3svc_encode_entry(void *cd, const char *name,
1382 int namlen, loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
1384 return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
1385}
1386
1387int
NeilBrowna0ad13e2007-01-26 00:57:10 -08001388nfs3svc_encode_entry_plus(void *cd, const char *name,
1389 int namlen, loff_t offset, u64 ino,
1390 unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
1392 return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
1393}
1394
1395/* FSSTAT */
1396int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001397nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001399 struct nfsd3_fsstatres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 struct kstatfs *s = &resp->stats;
1401 u64 bs = s->f_bsize;
1402
Chuck Levercc028a12020-10-02 15:52:44 -04001403 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 *p++ = xdr_zero; /* no post_op_attr */
1405
1406 if (resp->status == 0) {
1407 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1408 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1409 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1410 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1411 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1412 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1413 *p++ = htonl(resp->invarsec); /* mean unchanged time */
1414 }
1415 return xdr_ressize_check(rqstp, p);
1416}
1417
1418/* FSINFO */
1419int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001420nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001422 struct nfsd3_fsinfores *resp = rqstp->rq_resp;
1423
Chuck Levercc028a12020-10-02 15:52:44 -04001424 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 *p++ = xdr_zero; /* no post_op_attr */
1426
1427 if (resp->status == 0) {
1428 *p++ = htonl(resp->f_rtmax);
1429 *p++ = htonl(resp->f_rtpref);
1430 *p++ = htonl(resp->f_rtmult);
1431 *p++ = htonl(resp->f_wtmax);
1432 *p++ = htonl(resp->f_wtpref);
1433 *p++ = htonl(resp->f_wtmult);
1434 *p++ = htonl(resp->f_dtpref);
1435 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1436 *p++ = xdr_one;
1437 *p++ = xdr_zero;
1438 *p++ = htonl(resp->f_properties);
1439 }
1440
1441 return xdr_ressize_check(rqstp, p);
1442}
1443
1444/* PATHCONF */
1445int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001446nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001448 struct nfsd3_pathconfres *resp = rqstp->rq_resp;
1449
Chuck Lever1905cac2020-10-23 10:41:01 -04001450 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 *p++ = xdr_zero; /* no post_op_attr */
1452
1453 if (resp->status == 0) {
1454 *p++ = htonl(resp->p_link_max);
1455 *p++ = htonl(resp->p_name_max);
1456 *p++ = htonl(resp->p_no_trunc);
1457 *p++ = htonl(resp->p_chown_restricted);
1458 *p++ = htonl(resp->p_case_insensitive);
1459 *p++ = htonl(resp->p_case_preserving);
1460 }
1461
1462 return xdr_ressize_check(rqstp, p);
1463}
1464
1465/* COMMIT */
1466int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001467nfs3svc_encode_commitres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001469 struct nfsd3_commitres *resp = rqstp->rq_resp;
Stanislav Kinsburskyb9c0ef82012-12-06 14:23:19 +03001470
Chuck Levercc028a12020-10-02 15:52:44 -04001471 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 p = encode_wcc_data(rqstp, p, &resp->fh);
1473 /* Write verifier */
1474 if (resp->status == 0) {
Trond Myklebust524ff1a2020-01-06 13:40:36 -05001475 *p++ = resp->verf[0];
1476 *p++ = resp->verf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 }
1478 return xdr_ressize_check(rqstp, p);
1479}
1480
1481/*
1482 * XDR release functions
1483 */
Christoph Hellwig85374882017-05-08 18:48:24 +02001484void
1485nfs3svc_release_fhandle(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
Christoph Hellwig85374882017-05-08 18:48:24 +02001487 struct nfsd3_attrstat *resp = rqstp->rq_resp;
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 fh_put(&resp->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
1491
Christoph Hellwig85374882017-05-08 18:48:24 +02001492void
1493nfs3svc_release_fhandle2(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
Christoph Hellwig85374882017-05-08 18:48:24 +02001495 struct nfsd3_fhandle_pair *resp = rqstp->rq_resp;
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 fh_put(&resp->fh1);
1498 fh_put(&resp->fh2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}