blob: 29b923a497f487d4422ecc503570b4ce8330524f [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
Adrian Bunk3ee6f612006-12-06 20:40:23 -0800124static __be32 *
Al Viro91f07162006-10-19 23:28:57 -0700125encode_fh(__be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 unsigned int size = fhp->fh_handle.fh_size;
128 *p++ = htonl(size);
129 if (size) p[XDR_QUADLEN(size)-1]=0;
130 memcpy(p, &fhp->fh_handle.fh_base, size);
131 return p + XDR_QUADLEN(size);
132}
133
Chuck Lever54d1d432020-10-20 15:42:33 -0400134static bool
135svcxdr_decode_filename3(struct xdr_stream *xdr, char **name, unsigned int *len)
136{
137 u32 size, i;
138 __be32 *p;
139 char *c;
140
141 if (xdr_stream_decode_u32(xdr, &size) < 0)
142 return false;
143 if (size == 0 || size > NFS3_MAXNAMLEN)
144 return false;
145 p = xdr_inline_decode(xdr, size);
146 if (!p)
147 return false;
148
149 *len = size;
150 *name = (char *)p;
151 for (i = 0, c = *name; i < size; i++, c++) {
152 if (*c == '\0' || *c == '/')
153 return false;
154 }
155
156 return true;
157}
158
159static bool
160svcxdr_decode_diropargs3(struct xdr_stream *xdr, struct svc_fh *fhp,
161 char **name, unsigned int *len)
162{
163 return svcxdr_decode_nfs_fh3(xdr, fhp) &&
164 svcxdr_decode_filename3(xdr, name, len);
165}
166
Chuck Lever9cde9362020-10-20 15:48:22 -0400167static bool
168svcxdr_decode_sattr3(struct svc_rqst *rqstp, struct xdr_stream *xdr,
169 struct iattr *iap)
170{
171 u32 set_it;
172
173 iap->ia_valid = 0;
174
175 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
176 return false;
177 if (set_it) {
178 u32 mode;
179
180 if (xdr_stream_decode_u32(xdr, &mode) < 0)
181 return false;
182 iap->ia_valid |= ATTR_MODE;
183 iap->ia_mode = mode;
184 }
185 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
186 return false;
187 if (set_it) {
188 u32 uid;
189
190 if (xdr_stream_decode_u32(xdr, &uid) < 0)
191 return false;
192 iap->ia_uid = make_kuid(nfsd_user_namespace(rqstp), uid);
193 if (uid_valid(iap->ia_uid))
194 iap->ia_valid |= ATTR_UID;
195 }
196 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
197 return false;
198 if (set_it) {
199 u32 gid;
200
201 if (xdr_stream_decode_u32(xdr, &gid) < 0)
202 return false;
203 iap->ia_gid = make_kgid(nfsd_user_namespace(rqstp), gid);
204 if (gid_valid(iap->ia_gid))
205 iap->ia_valid |= ATTR_GID;
206 }
207 if (xdr_stream_decode_bool(xdr, &set_it) < 0)
208 return false;
209 if (set_it) {
210 u64 newsize;
211
212 if (xdr_stream_decode_u64(xdr, &newsize) < 0)
213 return false;
214 iap->ia_valid |= ATTR_SIZE;
215 iap->ia_size = min_t(u64, newsize, NFS_OFFSET_MAX);
216 }
217 if (xdr_stream_decode_u32(xdr, &set_it) < 0)
218 return false;
219 switch (set_it) {
220 case DONT_CHANGE:
221 break;
222 case SET_TO_SERVER_TIME:
223 iap->ia_valid |= ATTR_ATIME;
224 break;
225 case SET_TO_CLIENT_TIME:
226 if (!svcxdr_decode_nfstime3(xdr, &iap->ia_atime))
227 return false;
228 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
229 break;
230 default:
231 return false;
232 }
233 if (xdr_stream_decode_u32(xdr, &set_it) < 0)
234 return false;
235 switch (set_it) {
236 case DONT_CHANGE:
237 break;
238 case SET_TO_SERVER_TIME:
239 iap->ia_valid |= ATTR_MTIME;
240 break;
241 case SET_TO_CLIENT_TIME:
242 if (!svcxdr_decode_nfstime3(xdr, &iap->ia_mtime))
243 return false;
244 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
245 break;
246 default:
247 return false;
248 }
249
250 return true;
251}
252
253static bool
254svcxdr_decode_sattrguard3(struct xdr_stream *xdr, struct nfsd3_sattrargs *args)
255{
256 __be32 *p;
257 u32 check;
258
259 if (xdr_stream_decode_bool(xdr, &check) < 0)
260 return false;
261 if (check) {
262 p = xdr_inline_decode(xdr, XDR_UNIT * 2);
263 if (!p)
264 return false;
265 args->check_guard = 1;
266 args->guardtime = be32_to_cpup(p);
267 } else
268 args->check_guard = 0;
269
270 return true;
271}
272
Chuck Leverf8a38e22020-10-20 17:04:03 -0400273static bool
274svcxdr_decode_specdata3(struct xdr_stream *xdr, struct nfsd3_mknodargs *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Chuck Leverf8a38e22020-10-20 17:04:03 -0400276 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Chuck Leverf8a38e22020-10-20 17:04:03 -0400278 p = xdr_inline_decode(xdr, XDR_UNIT * 2);
279 if (!p)
280 return false;
281 args->major = be32_to_cpup(p++);
282 args->minor = be32_to_cpup(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Chuck Leverf8a38e22020-10-20 17:04:03 -0400284 return true;
285}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Chuck Leverf8a38e22020-10-20 17:04:03 -0400287static bool
288svcxdr_decode_devicedata3(struct svc_rqst *rqstp, struct xdr_stream *xdr,
289 struct nfsd3_mknodargs *args)
290{
291 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs) &&
292 svcxdr_decode_specdata3(xdr, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
Chuck Lever2c42f802020-10-21 11:58:41 -0400295static bool
296svcxdr_encode_fattr3(struct svc_rqst *rqstp, struct xdr_stream *xdr,
297 const struct svc_fh *fhp, const struct kstat *stat)
298{
299 struct user_namespace *userns = nfsd_user_namespace(rqstp);
300 __be32 *p;
301 u64 fsid;
302
303 p = xdr_reserve_space(xdr, XDR_UNIT * 21);
304 if (!p)
305 return false;
306
307 *p++ = cpu_to_be32(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]);
308 *p++ = cpu_to_be32((u32)(stat->mode & S_IALLUGO));
309 *p++ = cpu_to_be32((u32)stat->nlink);
310 *p++ = cpu_to_be32((u32)from_kuid_munged(userns, stat->uid));
311 *p++ = cpu_to_be32((u32)from_kgid_munged(userns, stat->gid));
312 if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN)
313 p = xdr_encode_hyper(p, (u64)NFS3_MAXPATHLEN);
314 else
315 p = xdr_encode_hyper(p, (u64)stat->size);
316
317 /* used */
318 p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
319
320 /* rdev */
321 *p++ = cpu_to_be32((u32)MAJOR(stat->rdev));
322 *p++ = cpu_to_be32((u32)MINOR(stat->rdev));
323
324 switch(fsid_source(fhp)) {
325 case FSIDSOURCE_FSID:
326 fsid = (u64)fhp->fh_export->ex_fsid;
327 break;
328 case FSIDSOURCE_UUID:
329 fsid = ((u64 *)fhp->fh_export->ex_uuid)[0];
330 fsid ^= ((u64 *)fhp->fh_export->ex_uuid)[1];
331 break;
332 default:
333 fsid = (u64)huge_encode_dev(fhp->fh_dentry->d_sb->s_dev);
334 }
335 p = xdr_encode_hyper(p, fsid);
336
337 /* fileid */
338 p = xdr_encode_hyper(p, stat->ino);
339
340 p = encode_nfstime3(p, &stat->atime);
341 p = encode_nfstime3(p, &stat->mtime);
342 encode_nfstime3(p, &stat->ctime);
343
344 return true;
345}
346
NeilBrownaf6a4e22007-02-14 00:33:12 -0800347static __be32 *encode_fsid(__be32 *p, struct svc_fh *fhp)
348{
349 u64 f;
350 switch(fsid_source(fhp)) {
351 default:
352 case FSIDSOURCE_DEV:
353 p = xdr_encode_hyper(p, (u64)huge_encode_dev
Al Virofc640052016-04-10 01:33:30 -0400354 (fhp->fh_dentry->d_sb->s_dev));
NeilBrownaf6a4e22007-02-14 00:33:12 -0800355 break;
356 case FSIDSOURCE_FSID:
357 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
358 break;
359 case FSIDSOURCE_UUID:
360 f = ((u64*)fhp->fh_export->ex_uuid)[0];
361 f ^= ((u64*)fhp->fh_export->ex_uuid)[1];
362 p = xdr_encode_hyper(p, f);
363 break;
364 }
365 return p;
366}
367
Adrian Bunk3ee6f612006-12-06 20:40:23 -0800368static __be32 *
Al Viro91f07162006-10-19 23:28:57 -0700369encode_fattr3(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp,
David Shawa334de22006-01-06 00:19:58 -0800370 struct kstat *stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400372 struct user_namespace *userns = nfsd_user_namespace(rqstp);
David Shawa334de22006-01-06 00:19:58 -0800373 *p++ = htonl(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]);
Albert Fluegel6e14b462013-11-18 12:18:01 -0500374 *p++ = htonl((u32) (stat->mode & S_IALLUGO));
David Shawa334de22006-01-06 00:19:58 -0800375 *p++ = htonl((u32) stat->nlink);
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400376 *p++ = htonl((u32) from_kuid_munged(userns, stat->uid));
377 *p++ = htonl((u32) from_kgid_munged(userns, stat->gid));
David Shawa334de22006-01-06 00:19:58 -0800378 if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
380 } else {
David Shawa334de22006-01-06 00:19:58 -0800381 p = xdr_encode_hyper(p, (u64) stat->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
David Shawa334de22006-01-06 00:19:58 -0800383 p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
384 *p++ = htonl((u32) MAJOR(stat->rdev));
385 *p++ = htonl((u32) MINOR(stat->rdev));
NeilBrownaf6a4e22007-02-14 00:33:12 -0800386 p = encode_fsid(p, fhp);
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400387 p = xdr_encode_hyper(p, stat->ino);
Arnd Bergmann92c5e462019-10-31 14:55:32 +0100388 p = encode_time3(p, &stat->atime);
389 p = encode_time3(p, &stat->mtime);
390 p = encode_time3(p, &stat->ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 return p;
393}
394
Adrian Bunk3ee6f612006-12-06 20:40:23 -0800395static __be32 *
Al Viro91f07162006-10-19 23:28:57 -0700396encode_saved_post_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /* Attributes to follow */
399 *p++ = xdr_one;
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400400 return encode_fattr3(rqstp, p, fhp, &fhp->fh_post_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
Chuck Lever907c3822020-10-22 13:56:58 -0400403static bool
Chuck Lever70f8e832020-10-22 15:12:38 -0400404svcxdr_encode_wcc_attr(struct xdr_stream *xdr, const struct svc_fh *fhp)
405{
406 __be32 *p;
407
408 p = xdr_reserve_space(xdr, XDR_UNIT * 6);
409 if (!p)
410 return false;
411 p = xdr_encode_hyper(p, (u64)fhp->fh_pre_size);
412 p = encode_nfstime3(p, &fhp->fh_pre_mtime);
413 encode_nfstime3(p, &fhp->fh_pre_ctime);
414
415 return true;
416}
417
418static bool
419svcxdr_encode_pre_op_attr(struct xdr_stream *xdr, const struct svc_fh *fhp)
420{
421 if (!fhp->fh_pre_saved) {
422 if (xdr_stream_encode_item_absent(xdr) < 0)
423 return false;
424 return true;
425 }
426
427 if (xdr_stream_encode_item_present(xdr) < 0)
428 return false;
429 return svcxdr_encode_wcc_attr(xdr, fhp);
430}
431
432static bool
Chuck Lever907c3822020-10-22 13:56:58 -0400433svcxdr_encode_post_op_attr(struct svc_rqst *rqstp, struct xdr_stream *xdr,
434 const struct svc_fh *fhp)
435{
436 struct dentry *dentry = fhp->fh_dentry;
437 struct kstat stat;
438
439 /*
440 * The inode may be NULL if the call failed because of a
441 * stale file handle. In this case, no attributes are
442 * returned.
443 */
444 if (fhp->fh_no_wcc || !dentry || !d_really_is_positive(dentry))
445 goto no_post_op_attrs;
446 if (fh_getattr(fhp, &stat) != nfs_ok)
447 goto no_post_op_attrs;
448
449 if (xdr_stream_encode_item_present(xdr) < 0)
450 return false;
451 lease_get_mtime(d_inode(dentry), &stat.mtime);
452 if (!svcxdr_encode_fattr3(rqstp, xdr, fhp, &stat))
453 return false;
454
455 return true;
456
457no_post_op_attrs:
458 return xdr_stream_encode_item_absent(xdr) > 0;
459}
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461/*
462 * Encode post-operation attributes.
463 * The inode may be NULL if the call failed because of a stale file
464 * handle. In this case, no attributes are returned.
465 */
Al Viro91f07162006-10-19 23:28:57 -0700466static __be32 *
467encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 struct dentry *dentry = fhp->fh_dentry;
Jeff Laytondaab1102020-11-30 17:03:14 -0500470 if (!fhp->fh_no_wcc && dentry && d_really_is_positive(dentry)) {
Al Viro3dadecc2013-01-24 02:18:08 -0500471 __be32 err;
David Shawa334de22006-01-06 00:19:58 -0800472 struct kstat stat;
473
Al Viro3dadecc2013-01-24 02:18:08 -0500474 err = fh_getattr(fhp, &stat);
David Shawa334de22006-01-06 00:19:58 -0800475 if (!err) {
476 *p++ = xdr_one; /* attributes follow */
David Howells2b0143b2015-03-17 22:25:59 +0000477 lease_get_mtime(d_inode(dentry), &stat.mtime);
David Shawa334de22006-01-06 00:19:58 -0800478 return encode_fattr3(rqstp, p, fhp, &stat);
479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481 *p++ = xdr_zero;
482 return p;
483}
484
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000485/* Helper for NFSv3 ACLs */
Al Viro91f07162006-10-19 23:28:57 -0700486__be32 *
487nfs3svc_encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000488{
489 return encode_post_op_attr(rqstp, p, fhp);
490}
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492/*
Chuck Lever70f8e832020-10-22 15:12:38 -0400493 * Encode weak cache consistency data
494 */
495static bool
496svcxdr_encode_wcc_data(struct svc_rqst *rqstp, struct xdr_stream *xdr,
497 const struct svc_fh *fhp)
498{
499 struct dentry *dentry = fhp->fh_dentry;
500
501 if (!dentry || !d_really_is_positive(dentry) || !fhp->fh_post_saved)
502 goto neither;
503
504 /* before */
505 if (!svcxdr_encode_pre_op_attr(xdr, fhp))
506 return false;
507
508 /* after */
509 if (xdr_stream_encode_item_present(xdr) < 0)
510 return false;
511 if (!svcxdr_encode_fattr3(rqstp, xdr, fhp, &fhp->fh_post_attr))
512 return false;
513
514 return true;
515
516neither:
517 if (xdr_stream_encode_item_absent(xdr) < 0)
518 return false;
519 if (!svcxdr_encode_post_op_attr(rqstp, xdr, fhp))
520 return false;
521
522 return true;
523}
524
525/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 * Enocde weak cache consistency data
527 */
Al Viro91f07162006-10-19 23:28:57 -0700528static __be32 *
529encode_wcc_data(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
531 struct dentry *dentry = fhp->fh_dentry;
532
David Howells2b0143b2015-03-17 22:25:59 +0000533 if (dentry && d_really_is_positive(dentry) && fhp->fh_post_saved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (fhp->fh_pre_saved) {
535 *p++ = xdr_one;
536 p = xdr_encode_hyper(p, (u64) fhp->fh_pre_size);
537 p = encode_time3(p, &fhp->fh_pre_mtime);
538 p = encode_time3(p, &fhp->fh_pre_ctime);
539 } else {
540 *p++ = xdr_zero;
541 }
542 return encode_saved_post_attr(rqstp, p, fhp);
543 }
544 /* no pre- or post-attrs */
545 *p++ = xdr_zero;
546 return encode_post_op_attr(rqstp, p, fhp);
547}
548
J. Bruce Fields428a23d2021-01-29 14:27:01 -0500549static bool fs_supports_change_attribute(struct super_block *sb)
550{
551 return sb->s_flags & SB_I_VERSION || sb->s_export_op->fetch_iversion;
552}
553
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400554/*
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200555 * Fill in the pre_op attr for the wcc data
556 */
557void fill_pre_wcc(struct svc_fh *fhp)
558{
559 struct inode *inode;
560 struct kstat stat;
J. Bruce Fields942b20dc2020-11-30 17:46:17 -0500561 bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200562
Jeff Laytondaab1102020-11-30 17:03:14 -0500563 if (fhp->fh_no_wcc || fhp->fh_pre_saved)
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200564 return;
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200565 inode = d_inode(fhp->fh_dentry);
J. Bruce Fields428a23d2021-01-29 14:27:01 -0500566 if (fs_supports_change_attribute(inode->i_sb) || !v4) {
567 __be32 err = fh_getattr(fhp, &stat);
568
569 if (err) {
570 /* Grab the times from inode anyway */
571 stat.mtime = inode->i_mtime;
572 stat.ctime = inode->i_ctime;
573 stat.size = inode->i_size;
574 }
575 fhp->fh_pre_mtime = stat.mtime;
576 fhp->fh_pre_ctime = stat.ctime;
577 fhp->fh_pre_size = stat.size;
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200578 }
J. Bruce Fields942b20dc2020-11-30 17:46:17 -0500579 if (v4)
580 fhp->fh_pre_change = nfsd4_change_attribute(&stat, inode);
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200581
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200582 fhp->fh_pre_saved = true;
583}
584
585/*
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400586 * Fill in the post_op attr for the wcc data
587 */
588void fill_post_wcc(struct svc_fh *fhp)
589{
J. Bruce Fields942b20dc2020-11-30 17:46:17 -0500590 bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE);
591 struct inode *inode = d_inode(fhp->fh_dentry);
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400592
Jeff Laytondaab1102020-11-30 17:03:14 -0500593 if (fhp->fh_no_wcc)
594 return;
595
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400596 if (fhp->fh_post_saved)
597 printk("nfsd: inode locked twice during operation.\n");
598
J. Bruce Fields428a23d2021-01-29 14:27:01 -0500599 fhp->fh_post_saved = true;
600
601 if (fs_supports_change_attribute(inode->i_sb) || !v4) {
602 __be32 err = fh_getattr(fhp, &fhp->fh_post_attr);
603
604 if (err) {
605 fhp->fh_post_saved = false;
606 fhp->fh_post_attr.ctime = inode->i_ctime;
607 }
608 }
J. Bruce Fields942b20dc2020-11-30 17:46:17 -0500609 if (v4)
610 fhp->fh_post_change =
611 nfsd4_change_attribute(&fhp->fh_post_attr, inode);
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400612}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614/*
615 * XDR decode functions
616 */
Chuck Leverdcc46992020-10-01 18:59:12 -0400617
618int
Chuck Lever95753632020-10-20 14:30:02 -0400619nfs3svc_decode_fhandleargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Chuck Lever95753632020-10-20 14:30:02 -0400621 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200622 struct nfsd_fhandle *args = rqstp->rq_argp;
623
Chuck Lever95753632020-10-20 14:30:02 -0400624 return svcxdr_decode_nfs_fh3(xdr, &args->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
627int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200628nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Chuck Lever9cde9362020-10-20 15:48:22 -0400630 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200631 struct nfsd3_sattrargs *args = rqstp->rq_argp;
632
Chuck Lever9cde9362020-10-20 15:48:22 -0400633 return svcxdr_decode_nfs_fh3(xdr, &args->fh) &&
634 svcxdr_decode_sattr3(rqstp, xdr, &args->attrs) &&
635 svcxdr_decode_sattrguard3(xdr, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
638int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200639nfs3svc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Chuck Lever54d1d432020-10-20 15:42:33 -0400641 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200642 struct nfsd3_diropargs *args = rqstp->rq_argp;
643
Chuck Lever54d1d432020-10-20 15:42:33 -0400644 return svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
647int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200648nfs3svc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Chuck Lever3b921a22020-10-20 14:32:04 -0400650 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200651 struct nfsd3_accessargs *args = rqstp->rq_argp;
652
Chuck Lever3b921a22020-10-20 14:32:04 -0400653 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return 0;
Chuck Lever3b921a22020-10-20 14:32:04 -0400655 if (xdr_stream_decode_u32(xdr, &args->access) < 0)
656 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Chuck Lever3b921a22020-10-20 14:32:04 -0400658 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
661int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200662nfs3svc_decode_readargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Chuck Leverbe63bd22020-10-20 14:34:40 -0400664 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200665 struct nfsd3_readargs *args = rqstp->rq_argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Chuck Leverbe63bd22020-10-20 14:34:40 -0400667 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return 0;
Chuck Leverbe63bd22020-10-20 14:34:40 -0400669 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
670 return 0;
671 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
672 return 0;
J. Bruce Fields9512a162017-05-16 15:57:42 -0400673
Chuck Leverbe63bd22020-10-20 14:34:40 -0400674 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
677int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200678nfs3svc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Chuck Leverc43b2f22020-10-22 11:14:55 -0400680 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200681 struct nfsd3_writeargs *args = rqstp->rq_argp;
Greg Banks7adae482006-10-04 02:15:47 -0700682 u32 max_blocksize = svc_max_payload(rqstp);
J. Bruce Fieldsdb44bac2017-04-25 16:21:34 -0400683 struct kvec *head = rqstp->rq_arg.head;
684 struct kvec *tail = rqstp->rq_arg.tail;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400685 size_t remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Chuck Leverc43b2f22020-10-22 11:14:55 -0400687 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return 0;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400689 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
690 return 0;
691 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
692 return 0;
693 if (xdr_stream_decode_u32(xdr, &args->stable) < 0)
694 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Chuck Leverc43b2f22020-10-22 11:14:55 -0400696 /* opaque data */
697 if (xdr_stream_decode_u32(xdr, &args->len) < 0)
J. Bruce Fields13bf9fb2017-04-21 15:26:30 -0400698 return 0;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400699
700 /* request sanity */
Peter Staubachf34b95682007-05-09 02:34:48 -0700701 if (args->count != args->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return 0;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400703 remaining = head->iov_len + rqstp->rq_arg.page_len + tail->iov_len;
704 remaining -= xdr_stream_pos(xdr);
705 if (remaining < xdr_align_size(args->len))
Peter Staubachf34b95682007-05-09 02:34:48 -0700706 return 0;
Peter Staubachf34b95682007-05-09 02:34:48 -0700707 if (args->count > max_blocksize) {
708 args->count = max_blocksize;
Chuck Leverc43b2f22020-10-22 11:14:55 -0400709 args->len = max_blocksize;
Peter Staubachf34b95682007-05-09 02:34:48 -0700710 }
Chuck Lever8154ef22018-03-27 10:54:07 -0400711
Chuck Leverc43b2f22020-10-22 11:14:55 -0400712 args->first.iov_base = xdr->p;
713 args->first.iov_len = head->iov_len - xdr_stream_pos(xdr);
714
Peter Staubachf34b95682007-05-09 02:34:48 -0700715 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
718int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200719nfs3svc_decode_createargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Chuck Lever6b3a1192020-10-20 15:56:11 -0400721 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200722 struct nfsd3_createargs *args = rqstp->rq_argp;
723
Chuck Lever6b3a1192020-10-20 15:56:11 -0400724 if (!svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return 0;
Chuck Lever6b3a1192020-10-20 15:56:11 -0400726 if (xdr_stream_decode_u32(xdr, &args->createmode) < 0)
727 return 0;
728 switch (args->createmode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 case NFS3_CREATE_UNCHECKED:
730 case NFS3_CREATE_GUARDED:
Chuck Lever6b3a1192020-10-20 15:56:11 -0400731 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 case NFS3_CREATE_EXCLUSIVE:
Chuck Lever6b3a1192020-10-20 15:56:11 -0400733 args->verf = xdr_inline_decode(xdr, NFS3_CREATEVERFSIZE);
734 if (!args->verf)
735 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 break;
737 default:
738 return 0;
739 }
Chuck Lever6b3a1192020-10-20 15:56:11 -0400740 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
Christoph Hellwig026fec72017-05-08 19:01:48 +0200742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200744nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Chuck Lever83374c22020-10-20 17:02:16 -0400746 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200747 struct nfsd3_createargs *args = rqstp->rq_argp;
748
Chuck Lever83374c22020-10-20 17:02:16 -0400749 return svcxdr_decode_diropargs3(xdr, &args->fh,
750 &args->name, &args->len) &&
751 svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
754int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200755nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Chuck Leverda392012020-10-20 16:01:16 -0400757 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200758 struct nfsd3_symlinkargs *args = rqstp->rq_argp;
Chuck Leverda392012020-10-20 16:01:16 -0400759 struct kvec *head = rqstp->rq_arg.head;
760 struct kvec *tail = rqstp->rq_arg.tail;
761 size_t remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Chuck Leverda392012020-10-20 16:01:16 -0400763 if (!svcxdr_decode_diropargs3(xdr, &args->ffh, &args->fname, &args->flen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return 0;
Chuck Leverda392012020-10-20 16:01:16 -0400765 if (!svcxdr_decode_sattr3(rqstp, xdr, &args->attrs))
Chuck Lever38a70312018-03-27 10:54:21 -0400766 return 0;
Chuck Leverda392012020-10-20 16:01:16 -0400767 if (xdr_stream_decode_u32(xdr, &args->tlen) < 0)
768 return 0;
769
770 /* request sanity */
771 remaining = head->iov_len + rqstp->rq_arg.page_len + tail->iov_len;
772 remaining -= xdr_stream_pos(xdr);
773 if (remaining < xdr_align_size(args->tlen))
774 return 0;
775
776 args->first.iov_base = xdr->p;
777 args->first.iov_len = head->iov_len - xdr_stream_pos(xdr);
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return 1;
780}
781
782int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200783nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Chuck Leverf8a38e22020-10-20 17:04:03 -0400785 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200786 struct nfsd3_mknodargs *args = rqstp->rq_argp;
787
Chuck Leverf8a38e22020-10-20 17:04:03 -0400788 if (!svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return 0;
Chuck Leverf8a38e22020-10-20 17:04:03 -0400790 if (xdr_stream_decode_u32(xdr, &args->ftype) < 0)
791 return 0;
792 switch (args->ftype) {
793 case NF3CHR:
794 case NF3BLK:
795 return svcxdr_decode_devicedata3(rqstp, xdr, args);
796 case NF3SOCK:
797 case NF3FIFO:
798 return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs);
799 case NF3REG:
800 case NF3DIR:
801 case NF3LNK:
802 /* Valid XDR but illegal file types */
803 break;
804 default:
805 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807
Chuck Leverf8a38e22020-10-20 17:04:03 -0400808 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
811int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200812nfs3svc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Chuck Leverd181e0a2020-10-20 15:44:12 -0400814 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200815 struct nfsd3_renameargs *args = rqstp->rq_argp;
816
Chuck Leverd181e0a2020-10-20 15:44:12 -0400817 return svcxdr_decode_diropargs3(xdr, &args->ffh,
818 &args->fname, &args->flen) &&
819 svcxdr_decode_diropargs3(xdr, &args->tfh,
820 &args->tname, &args->tlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
823int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200824nfs3svc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Chuck Leverefaa1e72020-10-19 13:26:32 -0400826 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200827 struct nfsd3_linkargs *args = rqstp->rq_argp;
828
Chuck Leverefaa1e72020-10-19 13:26:32 -0400829 return svcxdr_decode_nfs_fh3(xdr, &args->ffh) &&
830 svcxdr_decode_diropargs3(xdr, &args->tfh,
831 &args->tname, &args->tlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
834int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200835nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400837 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200838 struct nfsd3_readdirargs *args = rqstp->rq_argp;
NeilBrownf875a792019-03-07 09:49:46 +1100839
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400840 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return 0;
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400842 if (xdr_stream_decode_u64(xdr, &args->cookie) < 0)
843 return 0;
844 args->verf = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE);
845 if (!args->verf)
846 return 0;
847 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
848 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400850 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
853int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200854nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400856 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200857 struct nfsd3_readdirargs *args = rqstp->rq_argp;
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400858 u32 dircount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400860 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return 0;
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400862 if (xdr_stream_decode_u64(xdr, &args->cookie) < 0)
863 return 0;
864 args->verf = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE);
865 if (!args->verf)
866 return 0;
867 /* dircount is ignored */
868 if (xdr_stream_decode_u32(xdr, &dircount) < 0)
869 return 0;
870 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
871 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Chuck Lever9cedc2e2020-10-19 13:23:52 -0400873 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
876int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200877nfs3svc_decode_commitargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Chuck Leverc8d26a02020-10-20 14:41:56 -0400879 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200880 struct nfsd3_commitargs *args = rqstp->rq_argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Chuck Leverc8d26a02020-10-20 14:41:56 -0400882 if (!svcxdr_decode_nfs_fh3(xdr, &args->fh))
883 return 0;
884 if (xdr_stream_decode_u64(xdr, &args->offset) < 0)
885 return 0;
886 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
887 return 0;
888
889 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
892/*
893 * XDR encode functions
894 */
Chuck Levercc028a12020-10-02 15:52:44 -0400895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896/* GETATTR */
897int
Chuck Lever2c42f802020-10-21 11:58:41 -0400898nfs3svc_encode_getattrres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Chuck Lever2c42f802020-10-21 11:58:41 -0400900 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200901 struct nfsd3_attrstat *resp = rqstp->rq_resp;
902
Chuck Lever2c42f802020-10-21 11:58:41 -0400903 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
904 return 0;
905 switch (resp->status) {
906 case nfs_ok:
907 lease_get_mtime(d_inode(resp->fh.fh_dentry), &resp->stat.mtime);
908 if (!svcxdr_encode_fattr3(rqstp, xdr, &resp->fh, &resp->stat))
909 return 0;
910 break;
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400911 }
Chuck Lever2c42f802020-10-21 11:58:41 -0400912
913 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
916/* SETATTR, REMOVE, RMDIR */
917int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200918nfs3svc_encode_wccstat(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Chuck Lever70f8e832020-10-22 15:12:38 -0400920 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200921 struct nfsd3_attrstat *resp = rqstp->rq_resp;
922
Chuck Lever70f8e832020-10-22 15:12:38 -0400923 return svcxdr_encode_nfsstat3(xdr, resp->status) &&
924 svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
927/* LOOKUP */
Chuck Lever5cf35332020-10-22 14:46:58 -0400928int nfs3svc_encode_lookupres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Chuck Lever5cf35332020-10-22 14:46:58 -0400930 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200931 struct nfsd3_diropres *resp = rqstp->rq_resp;
932
Chuck Lever5cf35332020-10-22 14:46:58 -0400933 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
934 return 0;
935 switch (resp->status) {
936 case nfs_ok:
937 if (!svcxdr_encode_nfs_fh3(xdr, &resp->fh))
938 return 0;
939 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
940 return 0;
941 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->dirfh))
942 return 0;
943 break;
944 default:
945 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->dirfh))
946 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
Chuck Lever5cf35332020-10-22 14:46:58 -0400948
949 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
951
952/* ACCESS */
953int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200954nfs3svc_encode_accessres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Chuck Lever907c3822020-10-22 13:56:58 -0400956 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200957 struct nfsd3_accessres *resp = rqstp->rq_resp;
958
Chuck Lever907c3822020-10-22 13:56:58 -0400959 if (!svcxdr_encode_nfsstat3(xdr, resp->status))
960 return 0;
961 switch (resp->status) {
962 case nfs_ok:
963 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
964 return 0;
965 if (xdr_stream_encode_u32(xdr, resp->access) < 0)
966 return 0;
967 break;
968 default:
969 if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh))
970 return 0;
971 }
972
973 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
976/* READLINK */
977int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200978nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200980 struct nfsd3_readlinkres *resp = rqstp->rq_resp;
Chuck Lever76e54922020-11-05 10:24:19 -0500981 struct kvec *head = rqstp->rq_res.head;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200982
Chuck Levercc028a12020-10-02 15:52:44 -0400983 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 p = encode_post_op_attr(rqstp, p, &resp->fh);
985 if (resp->status == 0) {
986 *p++ = htonl(resp->len);
987 xdr_ressize_check(rqstp, p);
988 rqstp->rq_res.page_len = resp->len;
989 if (resp->len & 3) {
990 /* need to pad the tail */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 rqstp->rq_res.tail[0].iov_base = p;
992 *p = 0;
993 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
994 }
Chuck Lever76e54922020-11-05 10:24:19 -0500995 if (svc_encode_result_payload(rqstp, head->iov_len, resp->len))
996 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return 1;
998 } else
999 return xdr_ressize_check(rqstp, p);
1000}
1001
1002/* READ */
1003int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001004nfs3svc_encode_readres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001006 struct nfsd3_readres *resp = rqstp->rq_resp;
Chuck Lever76e54922020-11-05 10:24:19 -05001007 struct kvec *head = rqstp->rq_res.head;
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001008
Chuck Levercc028a12020-10-02 15:52:44 -04001009 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 p = encode_post_op_attr(rqstp, p, &resp->fh);
1011 if (resp->status == 0) {
1012 *p++ = htonl(resp->count);
1013 *p++ = htonl(resp->eof);
1014 *p++ = htonl(resp->count); /* xdr opaque count */
1015 xdr_ressize_check(rqstp, p);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001016 /* now update rqstp->rq_res to reflect data as well */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 rqstp->rq_res.page_len = resp->count;
1018 if (resp->count & 3) {
1019 /* need to pad the tail */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 rqstp->rq_res.tail[0].iov_base = p;
1021 *p = 0;
1022 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count & 3);
1023 }
Chuck Lever76e54922020-11-05 10:24:19 -05001024 if (svc_encode_result_payload(rqstp, head->iov_len,
1025 resp->count))
1026 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return 1;
1028 } else
1029 return xdr_ressize_check(rqstp, p);
1030}
1031
1032/* WRITE */
1033int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001034nfs3svc_encode_writeres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001036 struct nfsd3_writeres *resp = rqstp->rq_resp;
Stanislav Kinsburskyb9c0ef82012-12-06 14:23:19 +03001037
Chuck Levercc028a12020-10-02 15:52:44 -04001038 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 p = encode_wcc_data(rqstp, p, &resp->fh);
1040 if (resp->status == 0) {
1041 *p++ = htonl(resp->count);
1042 *p++ = htonl(resp->committed);
Trond Myklebust19e06632020-01-06 13:40:37 -05001043 *p++ = resp->verf[0];
1044 *p++ = resp->verf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
1046 return xdr_ressize_check(rqstp, p);
1047}
1048
1049/* CREATE, MKDIR, SYMLINK, MKNOD */
1050int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001051nfs3svc_encode_createres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001053 struct nfsd3_diropres *resp = rqstp->rq_resp;
1054
Chuck Levercc028a12020-10-02 15:52:44 -04001055 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (resp->status == 0) {
1057 *p++ = xdr_one;
1058 p = encode_fh(p, &resp->fh);
1059 p = encode_post_op_attr(rqstp, p, &resp->fh);
1060 }
1061 p = encode_wcc_data(rqstp, p, &resp->dirfh);
1062 return xdr_ressize_check(rqstp, p);
1063}
1064
1065/* RENAME */
1066int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001067nfs3svc_encode_renameres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001069 struct nfsd3_renameres *resp = rqstp->rq_resp;
1070
Chuck Levercc028a12020-10-02 15:52:44 -04001071 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 p = encode_wcc_data(rqstp, p, &resp->ffh);
1073 p = encode_wcc_data(rqstp, p, &resp->tfh);
1074 return xdr_ressize_check(rqstp, p);
1075}
1076
1077/* LINK */
1078int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001079nfs3svc_encode_linkres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001081 struct nfsd3_linkres *resp = rqstp->rq_resp;
1082
Chuck Levercc028a12020-10-02 15:52:44 -04001083 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 p = encode_post_op_attr(rqstp, p, &resp->fh);
1085 p = encode_wcc_data(rqstp, p, &resp->tfh);
1086 return xdr_ressize_check(rqstp, p);
1087}
1088
1089/* READDIR */
1090int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001091nfs3svc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001093 struct nfsd3_readdirres *resp = rqstp->rq_resp;
1094
Chuck Levercc028a12020-10-02 15:52:44 -04001095 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 p = encode_post_op_attr(rqstp, p, &resp->fh);
1097
1098 if (resp->status == 0) {
1099 /* stupid readdir cookie */
1100 memcpy(p, resp->verf, 8); p += 2;
1101 xdr_ressize_check(rqstp, p);
1102 if (rqstp->rq_res.head[0].iov_len + (2<<2) > PAGE_SIZE)
1103 return 1; /*No room for trailer */
1104 rqstp->rq_res.page_len = (resp->count) << 2;
1105
1106 /* add the 'tail' to the end of the 'head' page - page 0. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 rqstp->rq_res.tail[0].iov_base = p;
1108 *p++ = 0; /* no more entries */
1109 *p++ = htonl(resp->common.err == nfserr_eof);
1110 rqstp->rq_res.tail[0].iov_len = 2<<2;
1111 return 1;
1112 } else
1113 return xdr_ressize_check(rqstp, p);
1114}
1115
Adrian Bunk3ee6f612006-12-06 20:40:23 -08001116static __be32 *
Al Viro91f07162006-10-19 23:28:57 -07001117encode_entry_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name,
Peter Staubach40ee5dc2007-08-16 12:10:07 -04001118 int namlen, u64 ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
1120 *p++ = xdr_one; /* mark entry present */
1121 p = xdr_encode_hyper(p, ino); /* file id */
1122 p = xdr_encode_array(p, name, namlen);/* name length & name */
1123
1124 cd->offset = p; /* remember pointer */
1125 p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
1126
1127 return p;
1128}
1129
Al Viroefe39652012-04-13 00:32:14 -04001130static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
NeilBrown43b0e7e2015-05-03 09:16:53 +10001132 const char *name, int namlen, u64 ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
1134 struct svc_export *exp;
1135 struct dentry *dparent, *dchild;
Al Viroefe39652012-04-13 00:32:14 -04001136 __be32 rv = nfserr_noent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 dparent = cd->fh.fh_dentry;
1139 exp = cd->fh.fh_export;
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (isdotent(name, namlen)) {
1142 if (namlen == 2) {
1143 dchild = dget_parent(dparent);
J. Bruce Fields51b2ee72021-01-11 16:01:29 -05001144 /*
1145 * Don't return filehandle for ".." if we're at
1146 * the filesystem or export root:
1147 */
Al Viroefe39652012-04-13 00:32:14 -04001148 if (dchild == dparent)
1149 goto out;
J. Bruce Fields51b2ee72021-01-11 16:01:29 -05001150 if (dparent == exp->ex_path.dentry)
1151 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 } else
1153 dchild = dget(dparent);
1154 } else
Al Viro6c2d47982019-10-31 01:21:58 -04001155 dchild = lookup_positive_unlocked(name, dparent, namlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (IS_ERR(dchild))
Al Viroefe39652012-04-13 00:32:14 -04001157 return rv;
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001158 if (d_mountpoint(dchild))
1159 goto out;
NeilBrown43b0e7e2015-05-03 09:16:53 +10001160 if (dchild->d_inode->i_ino != ino)
1161 goto out;
Al Viroefe39652012-04-13 00:32:14 -04001162 rv = fh_compose(fhp, exp, dchild, &cd->fh);
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001163out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 dput(dchild);
1165 return rv;
1166}
1167
NeilBrown43b0e7e2015-05-03 09:16:53 +10001168static __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 -04001169{
J. Bruce Fields068c34c2014-01-09 16:24:35 -05001170 struct svc_fh *fh = &cd->scratch;
Al Viroefe39652012-04-13 00:32:14 -04001171 __be32 err;
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001172
J. Bruce Fields068c34c2014-01-09 16:24:35 -05001173 fh_init(fh, NFS3_FHSIZE);
NeilBrown43b0e7e2015-05-03 09:16:53 +10001174 err = compose_entry_fh(cd, fh, name, namlen, ino);
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001175 if (err) {
1176 *p++ = 0;
1177 *p++ = 0;
J. Bruce Fieldsaed100f2009-09-04 14:40:36 -04001178 goto out;
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001179 }
J. Bruce Fields068c34c2014-01-09 16:24:35 -05001180 p = encode_post_op_attr(cd->rqstp, p, fh);
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001181 *p++ = xdr_one; /* yes, a file handle follows */
J. Bruce Fields068c34c2014-01-09 16:24:35 -05001182 p = encode_fh(p, fh);
J. Bruce Fieldsaed100f2009-09-04 14:40:36 -04001183out:
J. Bruce Fields068c34c2014-01-09 16:24:35 -05001184 fh_put(fh);
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001185 return p;
1186}
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188/*
1189 * Encode a directory entry. This one works for both normal readdir
1190 * and readdirplus.
1191 * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
1192 * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
1193 *
1194 * The readdirplus baggage is 1+21 words for post_op_attr, plus the
1195 * file handle.
1196 */
1197
1198#define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1)
1199#define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
1200static int
NeilBrown598b9a52007-03-26 21:32:08 -08001201encode_entry(struct readdir_cd *ccd, const char *name, int namlen,
Peter Staubach40ee5dc2007-08-16 12:10:07 -04001202 loff_t offset, u64 ino, unsigned int d_type, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
1205 common);
Al Viro91f07162006-10-19 23:28:57 -07001206 __be32 *p = cd->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 caddr_t curr_page_addr = NULL;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05001208 struct page ** page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 int slen; /* string (name) length */
1210 int elen; /* estimated entry length in words */
1211 int num_entry_words = 0; /* actual number of words */
1212
1213 if (cd->offset) {
1214 u64 offset64 = offset;
1215
1216 if (unlikely(cd->offset1)) {
1217 /* we ended up with offset on a page boundary */
1218 *cd->offset = htonl(offset64 >> 32);
1219 *cd->offset1 = htonl(offset64 & 0xffffffff);
1220 cd->offset1 = NULL;
1221 } else {
NeilBrown598b9a52007-03-26 21:32:08 -08001222 xdr_encode_hyper(cd->offset, offset64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
NeilBrownb6023452019-03-04 14:08:22 +11001224 cd->offset = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
1226
1227 /*
1228 dprintk("encode_entry(%.*s @%ld%s)\n",
1229 namlen, name, (long) offset, plus? " plus" : "");
1230 */
1231
1232 /* truncate filename if too long */
Kinglong Mee3c7aa152014-06-10 18:08:19 +08001233 namlen = min(namlen, NFS3_MAXNAMLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 slen = XDR_QUADLEN(namlen);
1236 elen = slen + NFS3_ENTRY_BAGGAGE
1237 + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
1238
1239 if (cd->buflen < elen) {
1240 cd->common.err = nfserr_toosmall;
1241 return -EINVAL;
1242 }
1243
1244 /* determine which page in rq_respages[] we are currently filling */
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05001245 for (page = cd->rqstp->rq_respages + 1;
1246 page < cd->rqstp->rq_next_page; page++) {
1247 curr_page_addr = page_address(*page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 if (((caddr_t)cd->buffer >= curr_page_addr) &&
1250 ((caddr_t)cd->buffer < curr_page_addr + PAGE_SIZE))
1251 break;
1252 }
1253
1254 if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
1255 /* encode entry in current page */
1256
1257 p = encode_entry_baggage(cd, p, name, namlen, ino);
1258
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001259 if (plus)
NeilBrown43b0e7e2015-05-03 09:16:53 +10001260 p = encode_entryplus_baggage(cd, p, name, namlen, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 num_entry_words = p - cd->buffer;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05001262 } else if (*(page+1) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 /* temporarily encode entry into next page, then move back to
1264 * current and next page in rq_respages[] */
Al Viro91f07162006-10-19 23:28:57 -07001265 __be32 *p1, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 int len1, len2;
1267
1268 /* grab next page for temporary storage of entry */
J. Bruce Fieldsafc59402012-12-10 18:01:37 -05001269 p1 = tmp = page_address(*(page+1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
1272
J. Bruce Fields8177e6d2009-09-04 14:13:09 -04001273 if (plus)
NeilBrown43b0e7e2015-05-03 09:16:53 +10001274 p1 = encode_entryplus_baggage(cd, p1, name, namlen, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 /* determine entry word length and lengths to go in pages */
1277 num_entry_words = p1 - tmp;
1278 len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer;
1279 if ((num_entry_words << 2) < len1) {
1280 /* the actual number of words in the entry is less
1281 * than elen and can still fit in the current page
1282 */
1283 memmove(p, tmp, num_entry_words << 2);
1284 p += num_entry_words;
1285
1286 /* update offset */
1287 cd->offset = cd->buffer + (cd->offset - tmp);
1288 } else {
1289 unsigned int offset_r = (cd->offset - tmp) << 2;
1290
1291 /* update pointer to offset location.
1292 * This is a 64bit quantity, so we need to
1293 * deal with 3 cases:
1294 * - entirely in first page
1295 * - entirely in second page
1296 * - 4 bytes in each page
1297 */
1298 if (offset_r + 8 <= len1) {
1299 cd->offset = p + (cd->offset - tmp);
1300 } else if (offset_r >= len1) {
1301 cd->offset -= len1 >> 2;
1302 } else {
1303 /* sitting on the fence */
1304 BUG_ON(offset_r != len1 - 4);
1305 cd->offset = p + (cd->offset - tmp);
1306 cd->offset1 = tmp;
1307 }
1308
1309 len2 = (num_entry_words << 2) - len1;
1310
1311 /* move from temp page to current and next pages */
1312 memmove(p, tmp, len1);
1313 memmove(tmp, (caddr_t)tmp+len1, len2);
1314
1315 p = tmp + (len2 >> 2);
1316 }
1317 }
1318 else {
1319 cd->common.err = nfserr_toosmall;
1320 return -EINVAL;
1321 }
1322
1323 cd->buflen -= num_entry_words;
1324 cd->buffer = p;
1325 cd->common.err = nfs_ok;
1326 return 0;
1327
1328}
1329
1330int
NeilBrowna0ad13e2007-01-26 00:57:10 -08001331nfs3svc_encode_entry(void *cd, const char *name,
1332 int namlen, loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
1335}
1336
1337int
NeilBrowna0ad13e2007-01-26 00:57:10 -08001338nfs3svc_encode_entry_plus(void *cd, const char *name,
1339 int namlen, loff_t offset, u64 ino,
1340 unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
1342 return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
1343}
1344
1345/* FSSTAT */
1346int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001347nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001349 struct nfsd3_fsstatres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 struct kstatfs *s = &resp->stats;
1351 u64 bs = s->f_bsize;
1352
Chuck Levercc028a12020-10-02 15:52:44 -04001353 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 *p++ = xdr_zero; /* no post_op_attr */
1355
1356 if (resp->status == 0) {
1357 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1358 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1359 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1360 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1361 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1362 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1363 *p++ = htonl(resp->invarsec); /* mean unchanged time */
1364 }
1365 return xdr_ressize_check(rqstp, p);
1366}
1367
1368/* FSINFO */
1369int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001370nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001372 struct nfsd3_fsinfores *resp = rqstp->rq_resp;
1373
Chuck Levercc028a12020-10-02 15:52:44 -04001374 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 *p++ = xdr_zero; /* no post_op_attr */
1376
1377 if (resp->status == 0) {
1378 *p++ = htonl(resp->f_rtmax);
1379 *p++ = htonl(resp->f_rtpref);
1380 *p++ = htonl(resp->f_rtmult);
1381 *p++ = htonl(resp->f_wtmax);
1382 *p++ = htonl(resp->f_wtpref);
1383 *p++ = htonl(resp->f_wtmult);
1384 *p++ = htonl(resp->f_dtpref);
1385 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1386 *p++ = xdr_one;
1387 *p++ = xdr_zero;
1388 *p++ = htonl(resp->f_properties);
1389 }
1390
1391 return xdr_ressize_check(rqstp, p);
1392}
1393
1394/* PATHCONF */
1395int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001396nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001398 struct nfsd3_pathconfres *resp = rqstp->rq_resp;
1399
Chuck Lever1905cac2020-10-23 10:41:01 -04001400 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 *p++ = xdr_zero; /* no post_op_attr */
1402
1403 if (resp->status == 0) {
1404 *p++ = htonl(resp->p_link_max);
1405 *p++ = htonl(resp->p_name_max);
1406 *p++ = htonl(resp->p_no_trunc);
1407 *p++ = htonl(resp->p_chown_restricted);
1408 *p++ = htonl(resp->p_case_insensitive);
1409 *p++ = htonl(resp->p_case_preserving);
1410 }
1411
1412 return xdr_ressize_check(rqstp, p);
1413}
1414
1415/* COMMIT */
1416int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001417nfs3svc_encode_commitres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001419 struct nfsd3_commitres *resp = rqstp->rq_resp;
Stanislav Kinsburskyb9c0ef82012-12-06 14:23:19 +03001420
Chuck Levercc028a12020-10-02 15:52:44 -04001421 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 p = encode_wcc_data(rqstp, p, &resp->fh);
1423 /* Write verifier */
1424 if (resp->status == 0) {
Trond Myklebust524ff1a2020-01-06 13:40:36 -05001425 *p++ = resp->verf[0];
1426 *p++ = resp->verf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428 return xdr_ressize_check(rqstp, p);
1429}
1430
1431/*
1432 * XDR release functions
1433 */
Christoph Hellwig85374882017-05-08 18:48:24 +02001434void
1435nfs3svc_release_fhandle(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
Christoph Hellwig85374882017-05-08 18:48:24 +02001437 struct nfsd3_attrstat *resp = rqstp->rq_resp;
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 fh_put(&resp->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440}
1441
Christoph Hellwig85374882017-05-08 18:48:24 +02001442void
1443nfs3svc_release_fhandle2(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
Christoph Hellwig85374882017-05-08 18:48:24 +02001445 struct nfsd3_fhandle_pair *resp = rqstp->rq_resp;
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 fh_put(&resp->fh1);
1448 fh_put(&resp->fh2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}