blob: eab14b52db202b0b48b02a15ad456cfc2667df87 [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 Lever2c42f802020-10-21 11:58:41 -0400110static bool
111svcxdr_encode_nfsstat3(struct xdr_stream *xdr, __be32 status)
112{
113 __be32 *p;
114
115 p = xdr_reserve_space(xdr, sizeof(status));
116 if (!p)
117 return false;
118 *p = status;
119
120 return true;
121}
122
Chuck Lever5cf35332020-10-22 14:46:58 -0400123static bool
124svcxdr_encode_nfs_fh3(struct xdr_stream *xdr, const struct svc_fh *fhp)
125{
126 u32 size = fhp->fh_handle.fh_size;
127 __be32 *p;
128
129 p = xdr_reserve_space(xdr, XDR_UNIT + size);
130 if (!p)
131 return false;
132 *p++ = cpu_to_be32(size);
133 if (size)
134 p[XDR_QUADLEN(size) - 1] = 0;
135 memcpy(p, &fhp->fh_handle.fh_base, size);
136
137 return true;
138}
139
Chuck Lever78315b32020-10-22 15:27:23 -0400140static bool
141svcxdr_encode_post_op_fh3(struct xdr_stream *xdr, const struct svc_fh *fhp)
142{
143 if (xdr_stream_encode_item_present(xdr) < 0)
144 return false;
145 if (!svcxdr_encode_nfs_fh3(xdr, fhp))
146 return false;
147
148 return true;
149}
150
Adrian Bunk3ee6f612006-12-06 20:40:23 -0800151static __be32 *
Al Viro91f07162006-10-19 23:28:57 -0700152encode_fh(__be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 unsigned int size = fhp->fh_handle.fh_size;
155 *p++ = htonl(size);
156 if (size) p[XDR_QUADLEN(size)-1]=0;
157 memcpy(p, &fhp->fh_handle.fh_base, size);
158 return p + XDR_QUADLEN(size);
159}
160
Chuck Lever54d1d432020-10-20 15:42:33 -0400161static bool
Chuck Leverecb7a082020-10-22 15:26:31 -0400162svcxdr_encode_writeverf3(struct xdr_stream *xdr, const __be32 *verf)
163{
164 __be32 *p;
165
166 p = xdr_reserve_space(xdr, NFS3_WRITEVERFSIZE);
167 if (!p)
168 return false;
169 memcpy(p, verf, NFS3_WRITEVERFSIZE);
170
171 return true;
172}
173
174static bool
Chuck Lever54d1d432020-10-20 15:42:33 -0400175svcxdr_decode_filename3(struct xdr_stream *xdr, char **name, unsigned int *len)
176{
177 u32 size, i;
178 __be32 *p;
179 char *c;
180
181 if (xdr_stream_decode_u32(xdr, &size) < 0)
182 return false;
183 if (size == 0 || size > NFS3_MAXNAMLEN)
184 return false;
185 p = xdr_inline_decode(xdr, size);
186 if (!p)
187 return false;
188
189 *len = size;
190 *name = (char *)p;
191 for (i = 0, c = *name; i < size; i++, c++) {
192 if (*c == '\0' || *c == '/')
193 return false;
194 }
195
196 return true;
197}
198
199static bool
200svcxdr_decode_diropargs3(struct xdr_stream *xdr, struct svc_fh *fhp,
201 char **name, unsigned int *len)
202{
203 return svcxdr_decode_nfs_fh3(xdr, fhp) &&
204 svcxdr_decode_filename3(xdr, name, len);
205}
206
Chuck Lever9cde9362020-10-20 15:48:22 -0400207static bool
208svcxdr_decode_sattr3(struct svc_rqst *rqstp, struct xdr_stream *xdr,
209 struct iattr *iap)
210{
211 u32 set_it;
212
213 iap->ia_valid = 0;
214
215 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
216 return false;
217 if (set_it) {
218 u32 mode;
219
220 if (xdr_stream_decode_u32(xdr, &mode) < 0)
221 return false;
222 iap->ia_valid |= ATTR_MODE;
223 iap->ia_mode = mode;
224 }
225 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
226 return false;
227 if (set_it) {
228 u32 uid;
229
230 if (xdr_stream_decode_u32(xdr, &uid) < 0)
231 return false;
232 iap->ia_uid = make_kuid(nfsd_user_namespace(rqstp), uid);
233 if (uid_valid(iap->ia_uid))
234 iap->ia_valid |= ATTR_UID;
235 }
236 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
237 return false;
238 if (set_it) {
239 u32 gid;
240
241 if (xdr_stream_decode_u32(xdr, &gid) < 0)
242 return false;
243 iap->ia_gid = make_kgid(nfsd_user_namespace(rqstp), gid);
244 if (gid_valid(iap->ia_gid))
245 iap->ia_valid |= ATTR_GID;
246 }
247 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
248 return false;
249 if (set_it) {
250 u64 newsize;
251
252 if (xdr_stream_decode_u64(xdr, &newsize) < 0)
253 return false;
254 iap->ia_valid |= ATTR_SIZE;
255 iap->ia_size = min_t(u64, newsize, NFS_OFFSET_MAX);
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_ATIME;
264 break;
265 case SET_TO_CLIENT_TIME:
266 if (!svcxdr_decode_nfstime3(xdr, &iap->ia_atime))
267 return false;
268 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
269 break;
270 default:
271 return false;
272 }
273 if (xdr_stream_decode_u32(xdr, &set_it) < 0)
274 return false;
275 switch (set_it) {
276 case DONT_CHANGE:
277 break;
278 case SET_TO_SERVER_TIME:
279 iap->ia_valid |= ATTR_MTIME;
280 break;
281 case SET_TO_CLIENT_TIME:
282 if (!svcxdr_decode_nfstime3(xdr, &iap->ia_mtime))
283 return false;
284 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
285 break;
286 default:
287 return false;
288 }
289
290 return true;
291}
292
293static bool
294svcxdr_decode_sattrguard3(struct xdr_stream *xdr, struct nfsd3_sattrargs *args)
295{
296 __be32 *p;
297 u32 check;
298
299 if (xdr_stream_decode_bool(xdr, &check) < 0)
300 return false;
301 if (check) {
302 p = xdr_inline_decode(xdr, XDR_UNIT * 2);
303 if (!p)
304 return false;
305 args->check_guard = 1;
306 args->guardtime = be32_to_cpup(p);
307 } else
308 args->check_guard = 0;
309
310 return true;
311}
312
Chuck Leverf8a38e22020-10-20 17:04:03 -0400313static bool
314svcxdr_decode_specdata3(struct xdr_stream *xdr, struct nfsd3_mknodargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Chuck Leverf8a38e22020-10-20 17:04:03 -0400316 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Chuck Leverf8a38e22020-10-20 17:04:03 -0400318 p = xdr_inline_decode(xdr, XDR_UNIT * 2);
319 if (!p)
320 return false;
321 args->major = be32_to_cpup(p++);
322 args->minor = be32_to_cpup(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Chuck Leverf8a38e22020-10-20 17:04:03 -0400324 return true;
325}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Chuck Leverf8a38e22020-10-20 17:04:03 -0400327static bool
328svcxdr_decode_devicedata3(struct svc_rqst *rqstp, struct xdr_stream *xdr,
329 struct nfsd3_mknodargs *args)
330{
331 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs) &&
332 svcxdr_decode_specdata3(xdr, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
Chuck Lever2c42f802020-10-21 11:58:41 -0400335static bool
336svcxdr_encode_fattr3(struct svc_rqst *rqstp, struct xdr_stream *xdr,
337 const struct svc_fh *fhp, const struct kstat *stat)
338{
339 struct user_namespace *userns = nfsd_user_namespace(rqstp);
340 __be32 *p;
341 u64 fsid;
342
343 p = xdr_reserve_space(xdr, XDR_UNIT * 21);
344 if (!p)
345 return false;
346
347 *p++ = cpu_to_be32(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]);
348 *p++ = cpu_to_be32((u32)(stat->mode & S_IALLUGO));
349 *p++ = cpu_to_be32((u32)stat->nlink);
350 *p++ = cpu_to_be32((u32)from_kuid_munged(userns, stat->uid));
351 *p++ = cpu_to_be32((u32)from_kgid_munged(userns, stat->gid));
352 if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN)
353 p = xdr_encode_hyper(p, (u64)NFS3_MAXPATHLEN);
354 else
355 p = xdr_encode_hyper(p, (u64)stat->size);
356
357 /* used */
358 p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
359
360 /* rdev */
361 *p++ = cpu_to_be32((u32)MAJOR(stat->rdev));
362 *p++ = cpu_to_be32((u32)MINOR(stat->rdev));
363
364 switch(fsid_source(fhp)) {
365 case FSIDSOURCE_FSID:
366 fsid = (u64)fhp->fh_export->ex_fsid;
367 break;
368 case FSIDSOURCE_UUID:
369 fsid = ((u64 *)fhp->fh_export->ex_uuid)[0];
370 fsid ^= ((u64 *)fhp->fh_export->ex_uuid)[1];
371 break;
372 default:
373 fsid = (u64)huge_encode_dev(fhp->fh_dentry->d_sb->s_dev);
374 }
375 p = xdr_encode_hyper(p, fsid);
376
377 /* fileid */
378 p = xdr_encode_hyper(p, stat->ino);
379
380 p = encode_nfstime3(p, &stat->atime);
381 p = encode_nfstime3(p, &stat->mtime);
382 encode_nfstime3(p, &stat->ctime);
383
384 return true;
385}
386
NeilBrownaf6a4e22007-02-14 00:33:12 -0800387static __be32 *encode_fsid(__be32 *p, struct svc_fh *fhp)
388{
389 u64 f;
390 switch(fsid_source(fhp)) {
391 default:
392 case FSIDSOURCE_DEV:
393 p = xdr_encode_hyper(p, (u64)huge_encode_dev
Al Virofc640052016-04-10 01:33:30 -0400394 (fhp->fh_dentry->d_sb->s_dev));
NeilBrownaf6a4e22007-02-14 00:33:12 -0800395 break;
396 case FSIDSOURCE_FSID:
397 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
398 break;
399 case FSIDSOURCE_UUID:
400 f = ((u64*)fhp->fh_export->ex_uuid)[0];
401 f ^= ((u64*)fhp->fh_export->ex_uuid)[1];
402 p = xdr_encode_hyper(p, f);
403 break;
404 }
405 return p;
406}
407
Adrian Bunk3ee6f612006-12-06 20:40:23 -0800408static __be32 *
Al Viro91f07162006-10-19 23:28:57 -0700409encode_fattr3(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp,
David Shawa334de22006-01-06 00:19:58 -0800410 struct kstat *stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400412 struct user_namespace *userns = nfsd_user_namespace(rqstp);
David Shawa334de22006-01-06 00:19:58 -0800413 *p++ = htonl(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]);
Albert Fluegel6e14b462013-11-18 12:18:01 -0500414 *p++ = htonl((u32) (stat->mode & S_IALLUGO));
David Shawa334de22006-01-06 00:19:58 -0800415 *p++ = htonl((u32) stat->nlink);
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400416 *p++ = htonl((u32) from_kuid_munged(userns, stat->uid));
417 *p++ = htonl((u32) from_kgid_munged(userns, stat->gid));
David Shawa334de22006-01-06 00:19:58 -0800418 if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
420 } else {
David Shawa334de22006-01-06 00:19:58 -0800421 p = xdr_encode_hyper(p, (u64) stat->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
David Shawa334de22006-01-06 00:19:58 -0800423 p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
424 *p++ = htonl((u32) MAJOR(stat->rdev));
425 *p++ = htonl((u32) MINOR(stat->rdev));
NeilBrownaf6a4e22007-02-14 00:33:12 -0800426 p = encode_fsid(p, fhp);
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400427 p = xdr_encode_hyper(p, stat->ino);
Arnd Bergmann92c5e462019-10-31 14:55:32 +0100428 p = encode_time3(p, &stat->atime);
429 p = encode_time3(p, &stat->mtime);
430 p = encode_time3(p, &stat->ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 return p;
433}
434
Chuck Lever907c3822020-10-22 13:56:58 -0400435static bool
Chuck Lever70f8e832020-10-22 15:12:38 -0400436svcxdr_encode_wcc_attr(struct xdr_stream *xdr, const struct svc_fh *fhp)
437{
438 __be32 *p;
439
440 p = xdr_reserve_space(xdr, XDR_UNIT * 6);
441 if (!p)
442 return false;
443 p = xdr_encode_hyper(p, (u64)fhp->fh_pre_size);
444 p = encode_nfstime3(p, &fhp->fh_pre_mtime);
445 encode_nfstime3(p, &fhp->fh_pre_ctime);
446
447 return true;
448}
449
450static bool
451svcxdr_encode_pre_op_attr(struct xdr_stream *xdr, const struct svc_fh *fhp)
452{
453 if (!fhp->fh_pre_saved) {
454 if (xdr_stream_encode_item_absent(xdr) < 0)
455 return false;
456 return true;
457 }
458
459 if (xdr_stream_encode_item_present(xdr) < 0)
460 return false;
461 return svcxdr_encode_wcc_attr(xdr, fhp);
462}
463
464static bool
Chuck Lever907c3822020-10-22 13:56:58 -0400465svcxdr_encode_post_op_attr(struct svc_rqst *rqstp, struct xdr_stream *xdr,
466 const struct svc_fh *fhp)
467{
468 struct dentry *dentry = fhp->fh_dentry;
469 struct kstat stat;
470
471 /*
472 * The inode may be NULL if the call failed because of a
473 * stale file handle. In this case, no attributes are
474 * returned.
475 */
476 if (fhp->fh_no_wcc || !dentry || !d_really_is_positive(dentry))
477 goto no_post_op_attrs;
478 if (fh_getattr(fhp, &stat) != nfs_ok)
479 goto no_post_op_attrs;
480
481 if (xdr_stream_encode_item_present(xdr) < 0)
482 return false;
483 lease_get_mtime(d_inode(dentry), &stat.mtime);
484 if (!svcxdr_encode_fattr3(rqstp, xdr, fhp, &stat))
485 return false;
486
487 return true;
488
489no_post_op_attrs:
490 return xdr_stream_encode_item_absent(xdr) > 0;
491}
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493/*
494 * Encode post-operation attributes.
495 * The inode may be NULL if the call failed because of a stale file
496 * handle. In this case, no attributes are returned.
497 */
Al Viro91f07162006-10-19 23:28:57 -0700498static __be32 *
499encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 struct dentry *dentry = fhp->fh_dentry;
Jeff Laytondaab1102020-11-30 17:03:14 -0500502 if (!fhp->fh_no_wcc && dentry && d_really_is_positive(dentry)) {
Al Viro3dadecc2013-01-24 02:18:08 -0500503 __be32 err;
David Shawa334de22006-01-06 00:19:58 -0800504 struct kstat stat;
505
Al Viro3dadecc2013-01-24 02:18:08 -0500506 err = fh_getattr(fhp, &stat);
David Shawa334de22006-01-06 00:19:58 -0800507 if (!err) {
508 *p++ = xdr_one; /* attributes follow */
David Howells2b0143b2015-03-17 22:25:59 +0000509 lease_get_mtime(d_inode(dentry), &stat.mtime);
David Shawa334de22006-01-06 00:19:58 -0800510 return encode_fattr3(rqstp, p, fhp, &stat);
511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513 *p++ = xdr_zero;
514 return p;
515}
516
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000517/* Helper for NFSv3 ACLs */
Al Viro91f07162006-10-19 23:28:57 -0700518__be32 *
519nfs3svc_encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000520{
521 return encode_post_op_attr(rqstp, p, fhp);
522}
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524/*
Chuck Lever70f8e832020-10-22 15:12:38 -0400525 * Encode weak cache consistency data
526 */
527static bool
528svcxdr_encode_wcc_data(struct svc_rqst *rqstp, struct xdr_stream *xdr,
529 const struct svc_fh *fhp)
530{
531 struct dentry *dentry = fhp->fh_dentry;
532
533 if (!dentry || !d_really_is_positive(dentry) || !fhp->fh_post_saved)
534 goto neither;
535
536 /* before */
537 if (!svcxdr_encode_pre_op_attr(xdr, fhp))
538 return false;
539
540 /* after */
541 if (xdr_stream_encode_item_present(xdr) < 0)
542 return false;
543 if (!svcxdr_encode_fattr3(rqstp, xdr, fhp, &fhp->fh_post_attr))
544 return false;
545
546 return true;
547
548neither:
549 if (xdr_stream_encode_item_absent(xdr) < 0)
550 return false;
551 if (!svcxdr_encode_post_op_attr(rqstp, xdr, fhp))
552 return false;
553
554 return true;
555}
556
J. Bruce Fields428a23d2021-01-29 14:27:01 -0500557static bool fs_supports_change_attribute(struct super_block *sb)
558{
559 return sb->s_flags & SB_I_VERSION || sb->s_export_op->fetch_iversion;
560}
561
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400562/*
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200563 * Fill in the pre_op attr for the wcc data
564 */
565void fill_pre_wcc(struct svc_fh *fhp)
566{
567 struct inode *inode;
568 struct kstat stat;
J. Bruce Fields942b20dc2020-11-30 17:46:17 -0500569 bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200570
Jeff Laytondaab1102020-11-30 17:03:14 -0500571 if (fhp->fh_no_wcc || fhp->fh_pre_saved)
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200572 return;
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200573 inode = d_inode(fhp->fh_dentry);
J. Bruce Fields428a23d2021-01-29 14:27:01 -0500574 if (fs_supports_change_attribute(inode->i_sb) || !v4) {
575 __be32 err = fh_getattr(fhp, &stat);
576
577 if (err) {
578 /* Grab the times from inode anyway */
579 stat.mtime = inode->i_mtime;
580 stat.ctime = inode->i_ctime;
581 stat.size = inode->i_size;
582 }
583 fhp->fh_pre_mtime = stat.mtime;
584 fhp->fh_pre_ctime = stat.ctime;
585 fhp->fh_pre_size = stat.size;
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200586 }
J. Bruce Fields942b20dc2020-11-30 17:46:17 -0500587 if (v4)
588 fhp->fh_pre_change = nfsd4_change_attribute(&stat, inode);
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200589
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200590 fhp->fh_pre_saved = true;
591}
592
593/*
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400594 * Fill in the post_op attr for the wcc data
595 */
596void fill_post_wcc(struct svc_fh *fhp)
597{
J. Bruce Fields942b20dc2020-11-30 17:46:17 -0500598 bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
599 struct inode *inode = d_inode(fhp->fh_dentry);
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400600
Jeff Laytondaab1102020-11-30 17:03:14 -0500601 if (fhp->fh_no_wcc)
602 return;
603
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400604 if (fhp->fh_post_saved)
605 printk("nfsd: inode locked twice during operation.\n");
606
J. Bruce Fields428a23d2021-01-29 14:27:01 -0500607 fhp->fh_post_saved = true;
608
609 if (fs_supports_change_attribute(inode->i_sb) || !v4) {
610 __be32 err = fh_getattr(fhp, &fhp->fh_post_attr);
611
612 if (err) {
613 fhp->fh_post_saved = false;
614 fhp->fh_post_attr.ctime = inode->i_ctime;
615 }
616 }
J. Bruce Fields942b20dc2020-11-30 17:46:17 -0500617 if (v4)
618 fhp->fh_post_change =
619 nfsd4_change_attribute(&fhp->fh_post_attr, inode);
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400620}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622/*
623 * XDR decode functions
624 */
Chuck Leverdcc46992020-10-01 18:59:12 -0400625
626int
Chuck Lever95753632020-10-20 14:30:02 -0400627nfs3svc_decode_fhandleargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Chuck Lever95753632020-10-20 14:30:02 -0400629 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200630 struct nfsd_fhandle *args = rqstp->rq_argp;
631
Chuck Lever95753632020-10-20 14:30:02 -0400632 return svcxdr_decode_nfs_fh3(xdr, &args->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
635int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200636nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Chuck Lever9cde9362020-10-20 15:48:22 -0400638 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200639 struct nfsd3_sattrargs *args = rqstp->rq_argp;
640
Chuck Lever9cde9362020-10-20 15:48:22 -0400641 return svcxdr_decode_nfs_fh3(xdr, &args->fh) &&
642 svcxdr_decode_sattr3(rqstp, xdr, &args->attrs) &&
643 svcxdr_decode_sattrguard3(xdr, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
646int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200647nfs3svc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Chuck Lever54d1d432020-10-20 15:42:33 -0400649 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200650 struct nfsd3_diropargs *args = rqstp->rq_argp;
651
Chuck Lever54d1d432020-10-20 15:42:33 -0400652 return svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
655int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200656nfs3svc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Chuck Lever3b921a22020-10-20 14:32:04 -0400658 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200659 struct nfsd3_accessargs *args = rqstp->rq_argp;
660
Chuck Lever3b921a22020-10-20 14:32:04 -0400661 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return 0;
Chuck Lever3b921a22020-10-20 14:32:04 -0400663 if (xdr_stream_decode_u32(xdr, &args->access) < 0)
664 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Chuck Lever3b921a22020-10-20 14:32:04 -0400666 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
669int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200670nfs3svc_decode_readargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Chuck Leverbe63bd22020-10-20 14:34:40 -0400672 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200673 struct nfsd3_readargs *args = rqstp->rq_argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Chuck Leverbe63bd22020-10-20 14:34:40 -0400675 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return 0;
Chuck Leverbe63bd22020-10-20 14:34:40 -0400677 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
678 return 0;
679 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
680 return 0;
J. Bruce Fields9512a162017-05-16 15:57:42 -0400681
Chuck Leverbe63bd22020-10-20 14:34:40 -0400682 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
685int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200686nfs3svc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Chuck Leverc43b2f22020-10-22 11:14:55 -0400688 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200689 struct nfsd3_writeargs *args = rqstp->rq_argp;
Greg Banks7adae482006-10-04 02:15:47 -0700690 u32 max_blocksize = svc_max_payload(rqstp);
J. Bruce Fieldsdb44bac2017-04-25 16:21:34 -0400691 struct kvec *head = rqstp->rq_arg.head;
692 struct kvec *tail = rqstp->rq_arg.tail;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400693 size_t remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Chuck Leverc43b2f22020-10-22 11:14:55 -0400695 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return 0;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400697 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
698 return 0;
699 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
700 return 0;
701 if (xdr_stream_decode_u32(xdr, &args->stable) < 0)
702 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Chuck Leverc43b2f22020-10-22 11:14:55 -0400704 /* opaque data */
705 if (xdr_stream_decode_u32(xdr, &args->len) < 0)
J. Bruce Fields13bf9fb2017-04-21 15:26:30 -0400706 return 0;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400707
708 /* request sanity */
Peter Staubachf34b95682007-05-09 02:34:48 -0700709 if (args->count != args->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return 0;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400711 remaining = head->iov_len + rqstp->rq_arg.page_len + tail->iov_len;
712 remaining -= xdr_stream_pos(xdr);
713 if (remaining < xdr_align_size(args->len))
Peter Staubachf34b95682007-05-09 02:34:48 -0700714 return 0;
Peter Staubachf34b95682007-05-09 02:34:48 -0700715 if (args->count > max_blocksize) {
716 args->count = max_blocksize;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400717 args->len = max_blocksize;
Peter Staubachf34b95682007-05-09 02:34:48 -0700718 }
Chuck Lever8154ef22018-03-27 10:54:07 -0400719
Chuck Leverc43b2f22020-10-22 11:14:55 -0400720 args->first.iov_base = xdr->p;
721 args->first.iov_len = head->iov_len - xdr_stream_pos(xdr);
722
Peter Staubachf34b95682007-05-09 02:34:48 -0700723 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
726int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200727nfs3svc_decode_createargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Chuck Lever6b3a1192020-10-20 15:56:11 -0400729 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200730 struct nfsd3_createargs *args = rqstp->rq_argp;
731
Chuck Lever6b3a1192020-10-20 15:56:11 -0400732 if (!svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return 0;
Chuck Lever6b3a1192020-10-20 15:56:11 -0400734 if (xdr_stream_decode_u32(xdr, &args->createmode) < 0)
735 return 0;
736 switch (args->createmode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 case NFS3_CREATE_UNCHECKED:
738 case NFS3_CREATE_GUARDED:
Chuck Lever6b3a1192020-10-20 15:56:11 -0400739 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 case NFS3_CREATE_EXCLUSIVE:
Chuck Lever6b3a1192020-10-20 15:56:11 -0400741 args->verf = xdr_inline_decode(xdr, NFS3_CREATEVERFSIZE);
742 if (!args->verf)
743 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 break;
745 default:
746 return 0;
747 }
Chuck Lever6b3a1192020-10-20 15:56:11 -0400748 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
Christoph Hellwig026fec72017-05-08 19:01:48 +0200750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200752nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Chuck Lever83374c22020-10-20 17:02:16 -0400754 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200755 struct nfsd3_createargs *args = rqstp->rq_argp;
756
Chuck Lever83374c22020-10-20 17:02:16 -0400757 return svcxdr_decode_diropargs3(xdr, &args->fh,
758 &args->name, &args->len) &&
759 svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
762int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200763nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Chuck Leverda392012020-10-20 16:01:16 -0400765 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200766 struct nfsd3_symlinkargs *args = rqstp->rq_argp;
Chuck Leverda392012020-10-20 16:01:16 -0400767 struct kvec *head = rqstp->rq_arg.head;
768 struct kvec *tail = rqstp->rq_arg.tail;
769 size_t remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Chuck Leverda392012020-10-20 16:01:16 -0400771 if (!svcxdr_decode_diropargs3(xdr, &args->ffh, &args->fname, &args->flen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return 0;
Chuck Leverda392012020-10-20 16:01:16 -0400773 if (!svcxdr_decode_sattr3(rqstp, xdr, &args->attrs))
Chuck Lever38a70312018-03-27 10:54:21 -0400774 return 0;
Chuck Leverda392012020-10-20 16:01:16 -0400775 if (xdr_stream_decode_u32(xdr, &args->tlen) < 0)
776 return 0;
777
778 /* request sanity */
779 remaining = head->iov_len + rqstp->rq_arg.page_len + tail->iov_len;
780 remaining -= xdr_stream_pos(xdr);
781 if (remaining < xdr_align_size(args->tlen))
782 return 0;
783
784 args->first.iov_base = xdr->p;
785 args->first.iov_len = head->iov_len - xdr_stream_pos(xdr);
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return 1;
788}
789
790int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200791nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Chuck Leverf8a38e22020-10-20 17:04:03 -0400793 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200794 struct nfsd3_mknodargs *args = rqstp->rq_argp;
795
Chuck Leverf8a38e22020-10-20 17:04:03 -0400796 if (!svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return 0;
Chuck Leverf8a38e22020-10-20 17:04:03 -0400798 if (xdr_stream_decode_u32(xdr, &args->ftype) < 0)
799 return 0;
800 switch (args->ftype) {
801 case NF3CHR:
802 case NF3BLK:
803 return svcxdr_decode_devicedata3(rqstp, xdr, args);
804 case NF3SOCK:
805 case NF3FIFO:
806 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
807 case NF3REG:
808 case NF3DIR:
809 case NF3LNK:
810 /* Valid XDR but illegal file types */
811 break;
812 default:
813 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815
Chuck Leverf8a38e22020-10-20 17:04:03 -0400816 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
819int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200820nfs3svc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Chuck Leverd181e0a2020-10-20 15:44:12 -0400822 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200823 struct nfsd3_renameargs *args = rqstp->rq_argp;
824
Chuck Leverd181e0a2020-10-20 15:44:12 -0400825 return svcxdr_decode_diropargs3(xdr, &args->ffh,
826 &args->fname, &args->flen) &&
827 svcxdr_decode_diropargs3(xdr, &args->tfh,
828 &args->tname, &args->tlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
831int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200832nfs3svc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Chuck Leverefaa1e72020-10-19 13:26:32 -0400834 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200835 struct nfsd3_linkargs *args = rqstp->rq_argp;
836
Chuck Leverefaa1e72020-10-19 13:26:32 -0400837 return svcxdr_decode_nfs_fh3(xdr, &args->ffh) &&
838 svcxdr_decode_diropargs3(xdr, &args->tfh,
839 &args->tname, &args->tlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841
842int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200843nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400845 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200846 struct nfsd3_readdirargs *args = rqstp->rq_argp;
NeilBrownf875a792019-03-07 09:49:46 +1100847
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400848 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return 0;
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400850 if (xdr_stream_decode_u64(xdr, &args->cookie) < 0)
851 return 0;
852 args->verf = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE);
853 if (!args->verf)
854 return 0;
855 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
856 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400858 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
861int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200862nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400864 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200865 struct nfsd3_readdirargs *args = rqstp->rq_argp;
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400866 u32 dircount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400868 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return 0;
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400870 if (xdr_stream_decode_u64(xdr, &args->cookie) < 0)
871 return 0;
872 args->verf = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE);
873 if (!args->verf)
874 return 0;
875 /* dircount is ignored */
876 if (xdr_stream_decode_u32(xdr, &dircount) < 0)
877 return 0;
878 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
879 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400881 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883
884int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200885nfs3svc_decode_commitargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Chuck Leverc8d26a02020-10-20 14:41:56 -0400887 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200888 struct nfsd3_commitargs *args = rqstp->rq_argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Chuck Leverc8d26a02020-10-20 14:41:56 -0400890 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
891 return 0;
892 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
893 return 0;
894 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
895 return 0;
896
897 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
900/*
901 * XDR encode functions
902 */
Chuck Levercc028a12020-10-02 15:52:44 -0400903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904/* GETATTR */
905int
Chuck Lever2c42f802020-10-21 11:58:41 -0400906nfs3svc_encode_getattrres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Chuck Lever2c42f802020-10-21 11:58:41 -0400908 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200909 struct nfsd3_attrstat *resp = rqstp->rq_resp;
910
Chuck Lever2c42f802020-10-21 11:58:41 -0400911 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
912 return 0;
913 switch (resp->status) {
914 case nfs_ok:
915 lease_get_mtime(d_inode(resp->fh.fh_dentry), &resp->stat.mtime);
916 if (!svcxdr_encode_fattr3(rqstp, xdr, &resp->fh, &resp->stat))
917 return 0;
918 break;
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400919 }
Chuck Lever2c42f802020-10-21 11:58:41 -0400920
921 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
924/* SETATTR, REMOVE, RMDIR */
925int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200926nfs3svc_encode_wccstat(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Chuck Lever70f8e832020-10-22 15:12:38 -0400928 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200929 struct nfsd3_attrstat *resp = rqstp->rq_resp;
930
Chuck Lever70f8e832020-10-22 15:12:38 -0400931 return svcxdr_encode_nfsstat3(xdr, resp->status) &&
932 svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
935/* LOOKUP */
Chuck Lever5cf35332020-10-22 14:46:58 -0400936int nfs3svc_encode_lookupres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Chuck Lever5cf35332020-10-22 14:46:58 -0400938 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200939 struct nfsd3_diropres *resp = rqstp->rq_resp;
940
Chuck Lever5cf35332020-10-22 14:46:58 -0400941 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
942 return 0;
943 switch (resp->status) {
944 case nfs_ok:
945 if (!svcxdr_encode_nfs_fh3(xdr, &resp->fh))
946 return 0;
947 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
948 return 0;
949 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->dirfh))
950 return 0;
951 break;
952 default:
953 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->dirfh))
954 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
Chuck Lever5cf35332020-10-22 14:46:58 -0400956
957 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
960/* ACCESS */
961int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200962nfs3svc_encode_accessres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Chuck Lever907c3822020-10-22 13:56:58 -0400964 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200965 struct nfsd3_accessres *resp = rqstp->rq_resp;
966
Chuck Lever907c3822020-10-22 13:56:58 -0400967 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
968 return 0;
969 switch (resp->status) {
970 case nfs_ok:
971 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
972 return 0;
973 if (xdr_stream_encode_u32(xdr, resp->access) < 0)
974 return 0;
975 break;
976 default:
977 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
978 return 0;
979 }
980
981 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
984/* READLINK */
985int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200986nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Chuck Lever9a9c8922020-10-22 15:18:40 -0400988 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200989 struct nfsd3_readlinkres *resp = rqstp->rq_resp;
Chuck Lever76e54922020-11-05 10:24:19 -0500990 struct kvec *head = rqstp->rq_res.head;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200991
Chuck Lever9a9c8922020-10-22 15:18:40 -0400992 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
993 return 0;
994 switch (resp->status) {
995 case nfs_ok:
996 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
Chuck Lever76e54922020-11-05 10:24:19 -0500997 return 0;
Chuck Lever9a9c8922020-10-22 15:18:40 -0400998 if (xdr_stream_encode_u32(xdr, resp->len) < 0)
999 return 0;
1000 xdr_write_pages(xdr, resp->pages, 0, resp->len);
1001 if (svc_encode_result_payload(rqstp, head->iov_len, resp->len) < 0)
1002 return 0;
1003 break;
1004 default:
1005 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
1006 return 0;
1007 }
1008
1009 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
1011
1012/* READ */
1013int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001014nfs3svc_encode_readres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
Chuck Levercc9bcda2020-10-22 15:23:50 -04001016 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001017 struct nfsd3_readres *resp = rqstp->rq_resp;
Chuck Lever76e54922020-11-05 10:24:19 -05001018 struct kvec *head = rqstp->rq_res.head;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001019
Chuck Levercc9bcda2020-10-22 15:23:50 -04001020 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1021 return 0;
1022 switch (resp->status) {
1023 case nfs_ok:
1024 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
Chuck Lever76e54922020-11-05 10:24:19 -05001025 return 0;
Chuck Levercc9bcda2020-10-22 15:23:50 -04001026 if (xdr_stream_encode_u32(xdr, resp->count) < 0)
1027 return 0;
1028 if (xdr_stream_encode_bool(xdr, resp->eof) < 0)
1029 return 0;
1030 if (xdr_stream_encode_u32(xdr, resp->count) < 0)
1031 return 0;
1032 xdr_write_pages(xdr, resp->pages, rqstp->rq_res.page_base,
1033 resp->count);
1034 if (svc_encode_result_payload(rqstp, head->iov_len, resp->count) < 0)
1035 return 0;
1036 break;
1037 default:
1038 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
1039 return 0;
1040 }
1041
1042 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
1045/* WRITE */
1046int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001047nfs3svc_encode_writeres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Chuck Leverecb7a082020-10-22 15:26:31 -04001049 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001050 struct nfsd3_writeres *resp = rqstp->rq_resp;
Stanislav Kinsburskyb9c0ef82012-12-06 14:23:19 +03001051
Chuck Leverecb7a082020-10-22 15:26:31 -04001052 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1053 return 0;
1054 switch (resp->status) {
1055 case nfs_ok:
1056 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
1057 return 0;
1058 if (xdr_stream_encode_u32(xdr, resp->count) < 0)
1059 return 0;
1060 if (xdr_stream_encode_u32(xdr, resp->committed) < 0)
1061 return 0;
1062 if (!svcxdr_encode_writeverf3(xdr, resp->verf))
1063 return 0;
1064 break;
1065 default:
1066 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
1067 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
Chuck Leverecb7a082020-10-22 15:26:31 -04001069
1070 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
1073/* CREATE, MKDIR, SYMLINK, MKNOD */
1074int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001075nfs3svc_encode_createres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Chuck Lever78315b32020-10-22 15:27:23 -04001077 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001078 struct nfsd3_diropres *resp = rqstp->rq_resp;
1079
Chuck Lever78315b32020-10-22 15:27:23 -04001080 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1081 return 0;
1082 switch (resp->status) {
1083 case nfs_ok:
1084 if (!svcxdr_encode_post_op_fh3(xdr, &resp->fh))
1085 return 0;
1086 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
1087 return 0;
1088 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->dirfh))
1089 return 0;
1090 break;
1091 default:
1092 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->dirfh))
1093 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
Chuck Lever78315b32020-10-22 15:27:23 -04001095
1096 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
1098
1099/* RENAME */
1100int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001101nfs3svc_encode_renameres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Chuck Lever89d79e92020-10-22 15:33:05 -04001103 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001104 struct nfsd3_renameres *resp = rqstp->rq_resp;
1105
Chuck Lever89d79e92020-10-22 15:33:05 -04001106 return svcxdr_encode_nfsstat3(xdr, resp->status) &&
1107 svcxdr_encode_wcc_data(rqstp, xdr, &resp->ffh) &&
1108 svcxdr_encode_wcc_data(rqstp, xdr, &resp->tfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109}
1110
1111/* LINK */
1112int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001113nfs3svc_encode_linkres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Chuck Lever4d743802020-10-22 15:08:29 -04001115 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001116 struct nfsd3_linkres *resp = rqstp->rq_resp;
1117
Chuck Lever4d743802020-10-22 15:08:29 -04001118 return svcxdr_encode_nfsstat3(xdr, resp->status) &&
1119 svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh) &&
1120 svcxdr_encode_wcc_data(rqstp, xdr, &resp->tfh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
1123/* READDIR */
1124int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001125nfs3svc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001127 struct nfsd3_readdirres *resp = rqstp->rq_resp;
1128
Chuck Levercc028a12020-10-02 15:52:44 -04001129 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 p = encode_post_op_attr(rqstp, p, &resp->fh);
1131
1132 if (resp->status == 0) {
1133 /* stupid readdir cookie */
1134 memcpy(p, resp->verf, 8); p += 2;
1135 xdr_ressize_check(rqstp, p);
1136 if (rqstp->rq_res.head[0].iov_len + (2<<2) > PAGE_SIZE)
1137 return 1; /*No room for trailer */
1138 rqstp->rq_res.page_len = (resp->count) << 2;
1139
1140 /* add the 'tail' to the end of the 'head' page - page 0. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 rqstp->rq_res.tail[0].iov_base = p;
1142 *p++ = 0; /* no more entries */
1143 *p++ = htonl(resp->common.err == nfserr_eof);
1144 rqstp->rq_res.tail[0].iov_len = 2<<2;
1145 return 1;
1146 } else
1147 return xdr_ressize_check(rqstp, p);
1148}
1149
Adrian Bunk3ee6f612006-12-06 20:40:23 -08001150static __be32 *
Al Viro91f07162006-10-19 23:28:57 -07001151encode_entry_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name,
Peter Staubach40ee5dc2007-08-16 12:10:07 -04001152 int namlen, u64 ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
1154 *p++ = xdr_one; /* mark entry present */
1155 p = xdr_encode_hyper(p, ino); /* file id */
1156 p = xdr_encode_array(p, name, namlen);/* name length & name */
1157
1158 cd->offset = p; /* remember pointer */
1159 p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
1160
1161 return p;
1162}
1163
Al Viroefe39652012-04-13 00:32:14 -04001164static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
NeilBrown43b0e7e2015-05-03 09:16:53 +10001166 const char *name, int namlen, u64 ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
1168 struct svc_export *exp;
1169 struct dentry *dparent, *dchild;
Al Viroefe39652012-04-13 00:32:14 -04001170 __be32 rv = nfserr_noent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 dparent = cd->fh.fh_dentry;
1173 exp = cd->fh.fh_export;
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (isdotent(name, namlen)) {
1176 if (namlen == 2) {
1177 dchild = dget_parent(dparent);
J. Bruce Fields51b2ee72021-01-11 16:01:29 -05001178 /*
1179 * Don't return filehandle for ".." if we're at
1180 * the filesystem or export root:
1181 */
Al Viroefe39652012-04-13 00:32:14 -04001182 if (dchild == dparent)
1183 goto out;
J. Bruce Fields51b2ee72021-01-11 16:01:29 -05001184 if (dparent == exp->ex_path.dentry)
1185 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 } else
1187 dchild = dget(dparent);
1188 } else
Al Viro6c2d47982019-10-31 01:21:58 -04001189 dchild = lookup_positive_unlocked(name, dparent, namlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if (IS_ERR(dchild))
Al Viroefe39652012-04-13 00:32:14 -04001191 return rv;
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001192 if (d_mountpoint(dchild))
1193 goto out;
NeilBrown43b0e7e2015-05-03 09:16:53 +10001194 if (dchild->d_inode->i_ino != ino)
1195 goto out;
Al Viroefe39652012-04-13 00:32:14 -04001196 rv = fh_compose(fhp, exp, dchild, &cd->fh);
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001197out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 dput(dchild);
1199 return rv;
1200}
1201
NeilBrown43b0e7e2015-05-03 09:16:53 +10001202static __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 -04001203{
J. Bruce Fields068c34c2014-01-09 16:24:35 -05001204 struct svc_fh *fh = &cd->scratch;
Al Viroefe39652012-04-13 00:32:14 -04001205 __be32 err;
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001206
J. Bruce Fields068c34c2014-01-09 16:24:35 -05001207 fh_init(fh, NFS3_FHSIZE);
NeilBrown43b0e7e2015-05-03 09:16:53 +10001208 err = compose_entry_fh(cd, fh, name, namlen, ino);
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001209 if (err) {
1210 *p++ = 0;
1211 *p++ = 0;
J. Bruce Fieldsaed100f2009-09-04 14:40:36 -04001212 goto out;
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001213 }
J. Bruce Fields068c34c2014-01-09 16:24:35 -05001214 p = encode_post_op_attr(cd->rqstp, p, fh);
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001215 *p++ = xdr_one; /* yes, a file handle follows */
J. Bruce Fields068c34c2014-01-09 16:24:35 -05001216 p = encode_fh(p, fh);
J. Bruce Fieldsaed100f2009-09-04 14:40:36 -04001217out:
J. Bruce Fields068c34c2014-01-09 16:24:35 -05001218 fh_put(fh);
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001219 return p;
1220}
1221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222/*
1223 * Encode a directory entry. This one works for both normal readdir
1224 * and readdirplus.
1225 * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
1226 * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
1227 *
1228 * The readdirplus baggage is 1+21 words for post_op_attr, plus the
1229 * file handle.
1230 */
1231
1232#define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1)
1233#define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
1234static int
NeilBrown598b9a52007-03-26 21:32:08 -08001235encode_entry(struct readdir_cd *ccd, const char *name, int namlen,
Peter Staubach40ee5dc2007-08-16 12:10:07 -04001236 loff_t offset, u64 ino, unsigned int d_type, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
1238 struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
1239 common);
Al Viro91f07162006-10-19 23:28:57 -07001240 __be32 *p = cd->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 caddr_t curr_page_addr = NULL;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05001242 struct page ** page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 int slen; /* string (name) length */
1244 int elen; /* estimated entry length in words */
1245 int num_entry_words = 0; /* actual number of words */
1246
1247 if (cd->offset) {
1248 u64 offset64 = offset;
1249
1250 if (unlikely(cd->offset1)) {
1251 /* we ended up with offset on a page boundary */
1252 *cd->offset = htonl(offset64 >> 32);
1253 *cd->offset1 = htonl(offset64 & 0xffffffff);
1254 cd->offset1 = NULL;
1255 } else {
NeilBrown598b9a52007-03-26 21:32:08 -08001256 xdr_encode_hyper(cd->offset, offset64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
NeilBrownb6023452019-03-04 14:08:22 +11001258 cd->offset = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
1260
1261 /*
1262 dprintk("encode_entry(%.*s @%ld%s)\n",
1263 namlen, name, (long) offset, plus? " plus" : "");
1264 */
1265
1266 /* truncate filename if too long */
Kinglong Mee3c7aa152014-06-10 18:08:19 +08001267 namlen = min(namlen, NFS3_MAXNAMLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 slen = XDR_QUADLEN(namlen);
1270 elen = slen + NFS3_ENTRY_BAGGAGE
1271 + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
1272
1273 if (cd->buflen < elen) {
1274 cd->common.err = nfserr_toosmall;
1275 return -EINVAL;
1276 }
1277
1278 /* determine which page in rq_respages[] we are currently filling */
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05001279 for (page = cd->rqstp->rq_respages + 1;
1280 page < cd->rqstp->rq_next_page; page++) {
1281 curr_page_addr = page_address(*page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 if (((caddr_t)cd->buffer >= curr_page_addr) &&
1284 ((caddr_t)cd->buffer < curr_page_addr + PAGE_SIZE))
1285 break;
1286 }
1287
1288 if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
1289 /* encode entry in current page */
1290
1291 p = encode_entry_baggage(cd, p, name, namlen, ino);
1292
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001293 if (plus)
NeilBrown43b0e7e2015-05-03 09:16:53 +10001294 p = encode_entryplus_baggage(cd, p, name, namlen, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 num_entry_words = p - cd->buffer;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05001296 } else if (*(page+1) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 /* temporarily encode entry into next page, then move back to
1298 * current and next page in rq_respages[] */
Al Viro91f07162006-10-19 23:28:57 -07001299 __be32 *p1, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 int len1, len2;
1301
1302 /* grab next page for temporary storage of entry */
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05001303 p1 = tmp = page_address(*(page+1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
1306
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001307 if (plus)
NeilBrown43b0e7e2015-05-03 09:16:53 +10001308 p1 = encode_entryplus_baggage(cd, p1, name, namlen, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 /* determine entry word length and lengths to go in pages */
1311 num_entry_words = p1 - tmp;
1312 len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer;
1313 if ((num_entry_words << 2) < len1) {
1314 /* the actual number of words in the entry is less
1315 * than elen and can still fit in the current page
1316 */
1317 memmove(p, tmp, num_entry_words << 2);
1318 p += num_entry_words;
1319
1320 /* update offset */
1321 cd->offset = cd->buffer + (cd->offset - tmp);
1322 } else {
1323 unsigned int offset_r = (cd->offset - tmp) << 2;
1324
1325 /* update pointer to offset location.
1326 * This is a 64bit quantity, so we need to
1327 * deal with 3 cases:
1328 * - entirely in first page
1329 * - entirely in second page
1330 * - 4 bytes in each page
1331 */
1332 if (offset_r + 8 <= len1) {
1333 cd->offset = p + (cd->offset - tmp);
1334 } else if (offset_r >= len1) {
1335 cd->offset -= len1 >> 2;
1336 } else {
1337 /* sitting on the fence */
1338 BUG_ON(offset_r != len1 - 4);
1339 cd->offset = p + (cd->offset - tmp);
1340 cd->offset1 = tmp;
1341 }
1342
1343 len2 = (num_entry_words << 2) - len1;
1344
1345 /* move from temp page to current and next pages */
1346 memmove(p, tmp, len1);
1347 memmove(tmp, (caddr_t)tmp+len1, len2);
1348
1349 p = tmp + (len2 >> 2);
1350 }
1351 }
1352 else {
1353 cd->common.err = nfserr_toosmall;
1354 return -EINVAL;
1355 }
1356
1357 cd->buflen -= num_entry_words;
1358 cd->buffer = p;
1359 cd->common.err = nfs_ok;
1360 return 0;
1361
1362}
1363
1364int
NeilBrowna0ad13e2007-01-26 00:57:10 -08001365nfs3svc_encode_entry(void *cd, const char *name,
1366 int namlen, loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
1368 return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
1369}
1370
1371int
NeilBrowna0ad13e2007-01-26 00:57:10 -08001372nfs3svc_encode_entry_plus(void *cd, const char *name,
1373 int namlen, loff_t offset, u64 ino,
1374 unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
1376 return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
1377}
1378
Chuck Lever8b704492020-11-06 13:08:45 -05001379static bool
1380svcxdr_encode_fsstat3resok(struct xdr_stream *xdr,
1381 const struct nfsd3_fsstatres *resp)
1382{
1383 const struct kstatfs *s = &resp->stats;
1384 u64 bs = s->f_bsize;
1385 __be32 *p;
1386
1387 p = xdr_reserve_space(xdr, XDR_UNIT * 13);
1388 if (!p)
1389 return false;
1390 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1391 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1392 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1393 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1394 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1395 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1396 *p = cpu_to_be32(resp->invarsec); /* mean unchanged time */
1397
1398 return true;
1399}
1400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401/* FSSTAT */
1402int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001403nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Chuck Lever8b704492020-11-06 13:08:45 -05001405 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001406 struct nfsd3_fsstatres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Chuck Lever8b704492020-11-06 13:08:45 -05001408 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1409 return 0;
1410 switch (resp->status) {
1411 case nfs_ok:
1412 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1413 return 0;
1414 if (!svcxdr_encode_fsstat3resok(xdr, resp))
1415 return 0;
1416 break;
1417 default:
1418 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1419 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
Chuck Lever8b704492020-11-06 13:08:45 -05001421
1422 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423}
1424
Chuck Lever0a139d12020-10-22 13:42:13 -04001425static bool
1426svcxdr_encode_fsinfo3resok(struct xdr_stream *xdr,
1427 const struct nfsd3_fsinfores *resp)
1428{
1429 __be32 *p;
1430
1431 p = xdr_reserve_space(xdr, XDR_UNIT * 12);
1432 if (!p)
1433 return false;
1434 *p++ = cpu_to_be32(resp->f_rtmax);
1435 *p++ = cpu_to_be32(resp->f_rtpref);
1436 *p++ = cpu_to_be32(resp->f_rtmult);
1437 *p++ = cpu_to_be32(resp->f_wtmax);
1438 *p++ = cpu_to_be32(resp->f_wtpref);
1439 *p++ = cpu_to_be32(resp->f_wtmult);
1440 *p++ = cpu_to_be32(resp->f_dtpref);
1441 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1442 p = encode_nfstime3(p, &nfs3svc_time_delta);
1443 *p = cpu_to_be32(resp->f_properties);
1444
1445 return true;
1446}
1447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448/* FSINFO */
1449int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001450nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
Chuck Lever0a139d12020-10-22 13:42:13 -04001452 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001453 struct nfsd3_fsinfores *resp = rqstp->rq_resp;
1454
Chuck Lever0a139d12020-10-22 13:42:13 -04001455 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1456 return 0;
1457 switch (resp->status) {
1458 case nfs_ok:
1459 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1460 return 0;
1461 if (!svcxdr_encode_fsinfo3resok(xdr, resp))
1462 return 0;
1463 break;
1464 default:
1465 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1466 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 }
1468
Chuck Lever0a139d12020-10-22 13:42:13 -04001469 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470}
1471
Chuck Leverded04a52020-11-06 13:15:09 -05001472static bool
1473svcxdr_encode_pathconf3resok(struct xdr_stream *xdr,
1474 const struct nfsd3_pathconfres *resp)
1475{
1476 __be32 *p;
1477
1478 p = xdr_reserve_space(xdr, XDR_UNIT * 6);
1479 if (!p)
1480 return false;
1481 *p++ = cpu_to_be32(resp->p_link_max);
1482 *p++ = cpu_to_be32(resp->p_name_max);
1483 p = xdr_encode_bool(p, resp->p_no_trunc);
1484 p = xdr_encode_bool(p, resp->p_chown_restricted);
1485 p = xdr_encode_bool(p, resp->p_case_insensitive);
1486 xdr_encode_bool(p, resp->p_case_preserving);
1487
1488 return true;
1489}
1490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491/* PATHCONF */
1492int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001493nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
Chuck Leverded04a52020-11-06 13:15:09 -05001495 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001496 struct nfsd3_pathconfres *resp = rqstp->rq_resp;
1497
Chuck Leverded04a52020-11-06 13:15:09 -05001498 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1499 return 0;
1500 switch (resp->status) {
1501 case nfs_ok:
1502 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1503 return 0;
1504 if (!svcxdr_encode_pathconf3resok(xdr, resp))
1505 return 0;
1506 break;
1507 default:
1508 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh))
1509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 }
1511
Chuck Leverded04a52020-11-06 13:15:09 -05001512 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
1515/* COMMIT */
1516int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001517nfs3svc_encode_commitres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
Chuck Lever5ef28262020-10-22 15:35:46 -04001519 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001520 struct nfsd3_commitres *resp = rqstp->rq_resp;
Stanislav Kinsburskyb9c0ef82012-12-06 14:23:19 +03001521
Chuck Lever5ef28262020-10-22 15:35:46 -04001522 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
1523 return 0;
1524 switch (resp->status) {
1525 case nfs_ok:
1526 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
1527 return 0;
1528 if (!svcxdr_encode_writeverf3(xdr, resp->verf))
1529 return 0;
1530 break;
1531 default:
1532 if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh))
1533 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
Chuck Lever5ef28262020-10-22 15:35:46 -04001535
1536 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
1538
1539/*
1540 * XDR release functions
1541 */
Christoph Hellwig85374882017-05-08 18:48:24 +02001542void
1543nfs3svc_release_fhandle(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Christoph Hellwig85374882017-05-08 18:48:24 +02001545 struct nfsd3_attrstat *resp = rqstp->rq_resp;
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 fh_put(&resp->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
Christoph Hellwig85374882017-05-08 18:48:24 +02001550void
1551nfs3svc_release_fhandle2(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Christoph Hellwig85374882017-05-08 18:48:24 +02001553 struct nfsd3_fhandle_pair *resp = rqstp->rq_resp;
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 fh_put(&resp->fh1);
1556 fh_put(&resp->fh2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}