blob: 186b07a72373e5f01df4b2ad2e960e117605cd42 [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 */
23static u32 nfs3_ftypes[] = {
24 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/*
32 * XDR functions for basic NFS types
33 */
Adrian Bunk3ee6f612006-12-06 20:40:23 -080034static __be32 *
Arnd Bergmann92c5e462019-10-31 14:55:32 +010035encode_time3(__be32 *p, struct timespec64 *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
37 *p++ = htonl((u32) time->tv_sec); *p++ = htonl(time->tv_nsec);
38 return p;
39}
40
Adrian Bunk3ee6f612006-12-06 20:40:23 -080041static __be32 *
Arnd Bergmann92c5e462019-10-31 14:55:32 +010042decode_time3(__be32 *p, struct timespec64 *time)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 time->tv_sec = ntohl(*p++);
45 time->tv_nsec = ntohl(*p++);
46 return p;
47}
48
Adrian Bunk3ee6f612006-12-06 20:40:23 -080049static __be32 *
Al Viro91f07162006-10-19 23:28:57 -070050decode_fh(__be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 unsigned int size;
53 fh_init(fhp, NFS3_FHSIZE);
54 size = ntohl(*p++);
55 if (size > NFS3_FHSIZE)
56 return NULL;
57
58 memcpy(&fhp->fh_handle.fh_base, p, size);
59 fhp->fh_handle.fh_size = size;
60 return p + XDR_QUADLEN(size);
61}
62
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000063/* Helper function for NFSv3 ACL code */
Al Viro91f07162006-10-19 23:28:57 -070064__be32 *nfs3svc_decode_fh(__be32 *p, struct svc_fh *fhp)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000065{
66 return decode_fh(p, fhp);
67}
68
Adrian Bunk3ee6f612006-12-06 20:40:23 -080069static __be32 *
Al Viro91f07162006-10-19 23:28:57 -070070encode_fh(__be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 unsigned int size = fhp->fh_handle.fh_size;
73 *p++ = htonl(size);
74 if (size) p[XDR_QUADLEN(size)-1]=0;
75 memcpy(p, &fhp->fh_handle.fh_base, size);
76 return p + XDR_QUADLEN(size);
77}
78
79/*
80 * Decode a file name and make sure that the path contains
81 * no slashes or null bytes.
82 */
Adrian Bunk3ee6f612006-12-06 20:40:23 -080083static __be32 *
Chuck Leveree1a95b2007-11-01 16:56:58 -040084decode_filename(__be32 *p, char **namp, unsigned int *lenp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 char *name;
Chuck Leveree1a95b2007-11-01 16:56:58 -040087 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS3_MAXNAMLEN)) != NULL) {
90 for (i = 0, name = *namp; i < *lenp; i++, name++) {
91 if (*name == '\0' || *name == '/')
92 return NULL;
93 }
94 }
95
96 return p;
97}
98
Adrian Bunk3ee6f612006-12-06 20:40:23 -080099static __be32 *
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400100decode_sattr3(__be32 *p, struct iattr *iap, struct user_namespace *userns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 u32 tmp;
103
104 iap->ia_valid = 0;
105
106 if (*p++) {
107 iap->ia_valid |= ATTR_MODE;
108 iap->ia_mode = ntohl(*p++);
109 }
110 if (*p++) {
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400111 iap->ia_uid = make_kuid(userns, ntohl(*p++));
Eric W. Biederman458878a2013-02-02 04:16:08 -0800112 if (uid_valid(iap->ia_uid))
113 iap->ia_valid |= ATTR_UID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
115 if (*p++) {
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400116 iap->ia_gid = make_kgid(userns, ntohl(*p++));
Eric W. Biederman458878a2013-02-02 04:16:08 -0800117 if (gid_valid(iap->ia_gid))
118 iap->ia_valid |= ATTR_GID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
120 if (*p++) {
121 u64 newsize;
122
123 iap->ia_valid |= ATTR_SIZE;
124 p = xdr_decode_hyper(p, &newsize);
Kinglong Mee3c7aa152014-06-10 18:08:19 +0800125 iap->ia_size = min_t(u64, newsize, NFS_OFFSET_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
127 if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
128 iap->ia_valid |= ATTR_ATIME;
129 } else if (tmp == 2) { /* set to client time */
130 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
131 iap->ia_atime.tv_sec = ntohl(*p++);
132 iap->ia_atime.tv_nsec = ntohl(*p++);
133 }
134 if ((tmp = ntohl(*p++)) == 1) { /* set to server time */
135 iap->ia_valid |= ATTR_MTIME;
136 } else if (tmp == 2) { /* set to client time */
137 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
138 iap->ia_mtime.tv_sec = ntohl(*p++);
139 iap->ia_mtime.tv_nsec = ntohl(*p++);
140 }
141 return p;
142}
143
NeilBrownaf6a4e22007-02-14 00:33:12 -0800144static __be32 *encode_fsid(__be32 *p, struct svc_fh *fhp)
145{
146 u64 f;
147 switch(fsid_source(fhp)) {
148 default:
149 case FSIDSOURCE_DEV:
150 p = xdr_encode_hyper(p, (u64)huge_encode_dev
Al Virofc640052016-04-10 01:33:30 -0400151 (fhp->fh_dentry->d_sb->s_dev));
NeilBrownaf6a4e22007-02-14 00:33:12 -0800152 break;
153 case FSIDSOURCE_FSID:
154 p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid);
155 break;
156 case FSIDSOURCE_UUID:
157 f = ((u64*)fhp->fh_export->ex_uuid)[0];
158 f ^= ((u64*)fhp->fh_export->ex_uuid)[1];
159 p = xdr_encode_hyper(p, f);
160 break;
161 }
162 return p;
163}
164
Adrian Bunk3ee6f612006-12-06 20:40:23 -0800165static __be32 *
Al Viro91f07162006-10-19 23:28:57 -0700166encode_fattr3(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp,
David Shawa334de22006-01-06 00:19:58 -0800167 struct kstat *stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400169 struct user_namespace *userns = nfsd_user_namespace(rqstp);
David Shawa334de22006-01-06 00:19:58 -0800170 *p++ = htonl(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]);
Albert Fluegel6e14b462013-11-18 12:18:01 -0500171 *p++ = htonl((u32) (stat->mode & S_IALLUGO));
David Shawa334de22006-01-06 00:19:58 -0800172 *p++ = htonl((u32) stat->nlink);
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400173 *p++ = htonl((u32) from_kuid_munged(userns, stat->uid));
174 *p++ = htonl((u32) from_kgid_munged(userns, stat->gid));
David Shawa334de22006-01-06 00:19:58 -0800175 if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
177 } else {
David Shawa334de22006-01-06 00:19:58 -0800178 p = xdr_encode_hyper(p, (u64) stat->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
David Shawa334de22006-01-06 00:19:58 -0800180 p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
181 *p++ = htonl((u32) MAJOR(stat->rdev));
182 *p++ = htonl((u32) MINOR(stat->rdev));
NeilBrownaf6a4e22007-02-14 00:33:12 -0800183 p = encode_fsid(p, fhp);
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400184 p = xdr_encode_hyper(p, stat->ino);
Arnd Bergmann92c5e462019-10-31 14:55:32 +0100185 p = encode_time3(p, &stat->atime);
186 p = encode_time3(p, &stat->mtime);
187 p = encode_time3(p, &stat->ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 return p;
190}
191
Adrian Bunk3ee6f612006-12-06 20:40:23 -0800192static __be32 *
Al Viro91f07162006-10-19 23:28:57 -0700193encode_saved_post_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 /* Attributes to follow */
196 *p++ = xdr_one;
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400197 return encode_fattr3(rqstp, p, fhp, &fhp->fh_post_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
200/*
201 * Encode post-operation attributes.
202 * The inode may be NULL if the call failed because of a stale file
203 * handle. In this case, no attributes are returned.
204 */
Al Viro91f07162006-10-19 23:28:57 -0700205static __be32 *
206encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 struct dentry *dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +0000209 if (dentry && d_really_is_positive(dentry)) {
Al Viro3dadecc2013-01-24 02:18:08 -0500210 __be32 err;
David Shawa334de22006-01-06 00:19:58 -0800211 struct kstat stat;
212
Al Viro3dadecc2013-01-24 02:18:08 -0500213 err = fh_getattr(fhp, &stat);
David Shawa334de22006-01-06 00:19:58 -0800214 if (!err) {
215 *p++ = xdr_one; /* attributes follow */
David Howells2b0143b2015-03-17 22:25:59 +0000216 lease_get_mtime(d_inode(dentry), &stat.mtime);
David Shawa334de22006-01-06 00:19:58 -0800217 return encode_fattr3(rqstp, p, fhp, &stat);
218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220 *p++ = xdr_zero;
221 return p;
222}
223
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000224/* Helper for NFSv3 ACLs */
Al Viro91f07162006-10-19 23:28:57 -0700225__be32 *
226nfs3svc_encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000227{
228 return encode_post_op_attr(rqstp, p, fhp);
229}
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/*
232 * Enocde weak cache consistency data
233 */
Al Viro91f07162006-10-19 23:28:57 -0700234static __be32 *
235encode_wcc_data(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 struct dentry *dentry = fhp->fh_dentry;
238
David Howells2b0143b2015-03-17 22:25:59 +0000239 if (dentry && d_really_is_positive(dentry) && fhp->fh_post_saved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (fhp->fh_pre_saved) {
241 *p++ = xdr_one;
242 p = xdr_encode_hyper(p, (u64) fhp->fh_pre_size);
243 p = encode_time3(p, &fhp->fh_pre_mtime);
244 p = encode_time3(p, &fhp->fh_pre_ctime);
245 } else {
246 *p++ = xdr_zero;
247 }
248 return encode_saved_post_attr(rqstp, p, fhp);
249 }
250 /* no pre- or post-attrs */
251 *p++ = xdr_zero;
252 return encode_post_op_attr(rqstp, p, fhp);
253}
254
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400255/*
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200256 * Fill in the pre_op attr for the wcc data
257 */
258void fill_pre_wcc(struct svc_fh *fhp)
259{
260 struct inode *inode;
261 struct kstat stat;
262 __be32 err;
263
264 if (fhp->fh_pre_saved)
265 return;
266
267 inode = d_inode(fhp->fh_dentry);
268 err = fh_getattr(fhp, &stat);
269 if (err) {
270 /* Grab the times from inode anyway */
271 stat.mtime = inode->i_mtime;
272 stat.ctime = inode->i_ctime;
273 stat.size = inode->i_size;
274 }
275
Arnd Bergmann92c5e462019-10-31 14:55:32 +0100276 fhp->fh_pre_mtime = stat.mtime;
277 fhp->fh_pre_ctime = stat.ctime;
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200278 fhp->fh_pre_size = stat.size;
279 fhp->fh_pre_change = nfsd4_change_attribute(&stat, inode);
280 fhp->fh_pre_saved = true;
281}
282
283/*
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400284 * Fill in the post_op attr for the wcc data
285 */
286void fill_post_wcc(struct svc_fh *fhp)
287{
Al Viro3dadecc2013-01-24 02:18:08 -0500288 __be32 err;
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400289
290 if (fhp->fh_post_saved)
291 printk("nfsd: inode locked twice during operation.\n");
292
Al Viro3dadecc2013-01-24 02:18:08 -0500293 err = fh_getattr(fhp, &fhp->fh_post_attr);
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200294 fhp->fh_post_change = nfsd4_change_attribute(&fhp->fh_post_attr,
295 d_inode(fhp->fh_dentry));
Neil Brownc1ac3ff2010-12-02 11:14:30 +1100296 if (err) {
Jeff Laytonaaf91ec2015-09-17 08:28:39 -0400297 fhp->fh_post_saved = false;
Neil Brownc1ac3ff2010-12-02 11:14:30 +1100298 /* Grab the ctime anyway - set_change_info might use it */
David Howells2b0143b2015-03-17 22:25:59 +0000299 fhp->fh_post_attr.ctime = d_inode(fhp->fh_dentry)->i_ctime;
Neil Brownc1ac3ff2010-12-02 11:14:30 +1100300 } else
Jeff Laytonaaf91ec2015-09-17 08:28:39 -0400301 fhp->fh_post_saved = true;
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400302}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304/*
305 * XDR decode functions
306 */
307int
Chuck Leverdcc46992020-10-01 18:59:12 -0400308nfs3svc_decode_voidarg(struct svc_rqst *rqstp, __be32 *p)
309{
310 return 1;
311}
312
313int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200314nfs3svc_decode_fhandle(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200316 struct nfsd_fhandle *args = rqstp->rq_argp;
317
Benoit Tained40aa332014-05-22 16:32:30 +0200318 p = decode_fh(p, &args->fh);
319 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return 0;
321 return xdr_argsize_check(rqstp, p);
322}
323
324int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200325nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200327 struct nfsd3_sattrargs *args = rqstp->rq_argp;
328
Benoit Tained40aa332014-05-22 16:32:30 +0200329 p = decode_fh(p, &args->fh);
330 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return 0;
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400332 p = decode_sattr3(p, &args->attrs, nfsd_user_namespace(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 if ((args->check_guard = ntohl(*p++)) != 0) {
Arnd Bergmann92c5e462019-10-31 14:55:32 +0100335 struct timespec64 time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 p = decode_time3(p, &time);
337 args->guardtime = time.tv_sec;
338 }
339
340 return xdr_argsize_check(rqstp, p);
341}
342
343int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200344nfs3svc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200346 struct nfsd3_diropargs *args = rqstp->rq_argp;
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (!(p = decode_fh(p, &args->fh))
349 || !(p = decode_filename(p, &args->name, &args->len)))
350 return 0;
351
352 return xdr_argsize_check(rqstp, p);
353}
354
355int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200356nfs3svc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200358 struct nfsd3_accessargs *args = rqstp->rq_argp;
359
Benoit Tained40aa332014-05-22 16:32:30 +0200360 p = decode_fh(p, &args->fh);
361 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return 0;
363 args->access = ntohl(*p++);
364
365 return xdr_argsize_check(rqstp, p);
366}
367
368int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200369nfs3svc_decode_readargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200371 struct nfsd3_readargs *args = rqstp->rq_argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 unsigned int len;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500373 int v;
Greg Banks7adae482006-10-04 02:15:47 -0700374 u32 max_blocksize = svc_max_payload(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Benoit Tained40aa332014-05-22 16:32:30 +0200376 p = decode_fh(p, &args->fh);
377 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return 0;
NeilBrown072f62e2007-05-09 02:34:57 -0700379 p = xdr_decode_hyper(p, &args->offset);
J. Bruce Fields9512a162017-05-16 15:57:42 -0400380
Kinglong Mee3c7aa152014-06-10 18:08:19 +0800381 args->count = ntohl(*p++);
382 len = min(args->count, max_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 /* set up the kvec */
385 v=0;
386 while (len > 0) {
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500387 struct page *p = *(rqstp->rq_next_page++);
388
389 rqstp->rq_vec[v].iov_base = page_address(p);
Kinglong Mee3c7aa152014-06-10 18:08:19 +0800390 rqstp->rq_vec[v].iov_len = min_t(unsigned int, len, PAGE_SIZE);
NeilBrown3cc03b12006-10-04 02:15:47 -0700391 len -= rqstp->rq_vec[v].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 v++;
393 }
394 args->vlen = v;
J. Bruce Fields9512a162017-05-16 15:57:42 -0400395 return xdr_argsize_check(rqstp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
398int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200399nfs3svc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200401 struct nfsd3_writeargs *args = rqstp->rq_argp;
Chuck Lever8154ef22018-03-27 10:54:07 -0400402 unsigned int len, hdr, dlen;
Greg Banks7adae482006-10-04 02:15:47 -0700403 u32 max_blocksize = svc_max_payload(rqstp);
J. Bruce Fieldsdb44bac2017-04-25 16:21:34 -0400404 struct kvec *head = rqstp->rq_arg.head;
405 struct kvec *tail = rqstp->rq_arg.tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Benoit Tained40aa332014-05-22 16:32:30 +0200407 p = decode_fh(p, &args->fh);
408 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return 0;
NeilBrown072f62e2007-05-09 02:34:57 -0700410 p = xdr_decode_hyper(p, &args->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 args->count = ntohl(*p++);
413 args->stable = ntohl(*p++);
414 len = args->len = ntohl(*p++);
J. Bruce Fields13bf9fb2017-04-21 15:26:30 -0400415 if ((void *)p > head->iov_base + head->iov_len)
416 return 0;
Peter Staubachf34b95682007-05-09 02:34:48 -0700417 /*
418 * The count must equal the amount of data passed.
419 */
420 if (args->count != args->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return 0;
422
Peter Staubachf34b95682007-05-09 02:34:48 -0700423 /*
424 * Check to make sure that we got the right number of
425 * bytes.
Peter Staubachf34b95682007-05-09 02:34:48 -0700426 */
J. Bruce Fieldsdb44bac2017-04-25 16:21:34 -0400427 hdr = (void*)p - head->iov_base;
428 dlen = head->iov_len + rqstp->rq_arg.page_len + tail->iov_len - hdr;
Peter Staubachf34b95682007-05-09 02:34:48 -0700429 /*
430 * Round the length of the data which was specified up to
431 * the next multiple of XDR units and then compare that
432 * against the length which was actually received.
NeilBrownba67a392008-01-11 17:06:52 -0500433 * Note that when RPCSEC/GSS (for example) is used, the
434 * data buffer can be padded so dlen might be larger
435 * than required. It must never be smaller.
Peter Staubachf34b95682007-05-09 02:34:48 -0700436 */
NeilBrownba67a392008-01-11 17:06:52 -0500437 if (dlen < XDR_QUADLEN(len)*4)
Peter Staubachf34b95682007-05-09 02:34:48 -0700438 return 0;
439
440 if (args->count > max_blocksize) {
441 args->count = max_blocksize;
442 len = args->len = max_blocksize;
443 }
Chuck Lever8154ef22018-03-27 10:54:07 -0400444
445 args->first.iov_base = (void *)p;
446 args->first.iov_len = head->iov_len - hdr;
Peter Staubachf34b95682007-05-09 02:34:48 -0700447 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
450int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200451nfs3svc_decode_createargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200453 struct nfsd3_createargs *args = rqstp->rq_argp;
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (!(p = decode_fh(p, &args->fh))
456 || !(p = decode_filename(p, &args->name, &args->len)))
457 return 0;
458
459 switch (args->createmode = ntohl(*p++)) {
460 case NFS3_CREATE_UNCHECKED:
461 case NFS3_CREATE_GUARDED:
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400462 p = decode_sattr3(p, &args->attrs, nfsd_user_namespace(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 break;
464 case NFS3_CREATE_EXCLUSIVE:
465 args->verf = p;
466 p += 2;
467 break;
468 default:
469 return 0;
470 }
471
472 return xdr_argsize_check(rqstp, p);
473}
Christoph Hellwig026fec72017-05-08 19:01:48 +0200474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200476nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200478 struct nfsd3_createargs *args = rqstp->rq_argp;
479
NeilBrown072f62e2007-05-09 02:34:57 -0700480 if (!(p = decode_fh(p, &args->fh)) ||
481 !(p = decode_filename(p, &args->name, &args->len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return 0;
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400483 p = decode_sattr3(p, &args->attrs, nfsd_user_namespace(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 return xdr_argsize_check(rqstp, p);
486}
487
488int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200489nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200491 struct nfsd3_symlinkargs *args = rqstp->rq_argp;
Chuck Lever38a70312018-03-27 10:54:21 -0400492 char *base = (char *)p;
493 size_t dlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
NeilBrown072f62e2007-05-09 02:34:57 -0700495 if (!(p = decode_fh(p, &args->ffh)) ||
Chuck Lever38a70312018-03-27 10:54:21 -0400496 !(p = decode_filename(p, &args->fname, &args->flen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return 0;
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400498 p = decode_sattr3(p, &args->attrs, nfsd_user_namespace(rqstp));
NeilBrown072f62e2007-05-09 02:34:57 -0700499
Chuck Lever38a70312018-03-27 10:54:21 -0400500 args->tlen = ntohl(*p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Chuck Lever38a70312018-03-27 10:54:21 -0400502 args->first.iov_base = p;
503 args->first.iov_len = rqstp->rq_arg.head[0].iov_len;
504 args->first.iov_len -= (char *)p - base;
505
506 dlen = args->first.iov_len + rqstp->rq_arg.page_len +
507 rqstp->rq_arg.tail[0].iov_len;
508 if (dlen < XDR_QUADLEN(args->tlen) << 2)
509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return 1;
511}
512
513int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200514nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200516 struct nfsd3_mknodargs *args = rqstp->rq_argp;
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (!(p = decode_fh(p, &args->fh))
519 || !(p = decode_filename(p, &args->name, &args->len)))
520 return 0;
521
522 args->ftype = ntohl(*p++);
523
524 if (args->ftype == NF3BLK || args->ftype == NF3CHR
NeilBrown072f62e2007-05-09 02:34:57 -0700525 || args->ftype == NF3SOCK || args->ftype == NF3FIFO)
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400526 p = decode_sattr3(p, &args->attrs, nfsd_user_namespace(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 if (args->ftype == NF3BLK || args->ftype == NF3CHR) {
529 args->major = ntohl(*p++);
530 args->minor = ntohl(*p++);
531 }
532
533 return xdr_argsize_check(rqstp, p);
534}
535
536int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200537nfs3svc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200539 struct nfsd3_renameargs *args = rqstp->rq_argp;
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (!(p = decode_fh(p, &args->ffh))
542 || !(p = decode_filename(p, &args->fname, &args->flen))
543 || !(p = decode_fh(p, &args->tfh))
544 || !(p = decode_filename(p, &args->tname, &args->tlen)))
545 return 0;
546
547 return xdr_argsize_check(rqstp, p);
548}
549
550int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200551nfs3svc_decode_readlinkargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200553 struct nfsd3_readlinkargs *args = rqstp->rq_argp;
554
Benoit Tained40aa332014-05-22 16:32:30 +0200555 p = decode_fh(p, &args->fh);
556 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return 0;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500558 args->buffer = page_address(*(rqstp->rq_next_page++));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
J. Bruce Fields9512a162017-05-16 15:57:42 -0400560 return xdr_argsize_check(rqstp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
563int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200564nfs3svc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200566 struct nfsd3_linkargs *args = rqstp->rq_argp;
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (!(p = decode_fh(p, &args->ffh))
569 || !(p = decode_fh(p, &args->tfh))
570 || !(p = decode_filename(p, &args->tname, &args->tlen)))
571 return 0;
572
573 return xdr_argsize_check(rqstp, p);
574}
575
576int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200577nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200579 struct nfsd3_readdirargs *args = rqstp->rq_argp;
Murphy Zhou3c867942019-04-04 14:57:11 +0800580 int len;
NeilBrownf875a792019-03-07 09:49:46 +1100581 u32 max_blocksize = svc_max_payload(rqstp);
582
Benoit Tained40aa332014-05-22 16:32:30 +0200583 p = decode_fh(p, &args->fh);
584 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return 0;
586 p = xdr_decode_hyper(p, &args->cookie);
587 args->verf = p; p += 2;
588 args->dircount = ~0;
589 args->count = ntohl(*p++);
Murphy Zhou3c867942019-04-04 14:57:11 +0800590 len = args->count = min_t(u32, args->count, max_blocksize);
591
592 while (len > 0) {
593 struct page *p = *(rqstp->rq_next_page++);
594 if (!args->buffer)
595 args->buffer = page_address(p);
596 len -= PAGE_SIZE;
597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
J. Bruce Fields9512a162017-05-16 15:57:42 -0400599 return xdr_argsize_check(rqstp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
602int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200603nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200605 struct nfsd3_readdirargs *args = rqstp->rq_argp;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500606 int len;
Greg Banks7adae482006-10-04 02:15:47 -0700607 u32 max_blocksize = svc_max_payload(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Benoit Tained40aa332014-05-22 16:32:30 +0200609 p = decode_fh(p, &args->fh);
610 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return 0;
612 p = xdr_decode_hyper(p, &args->cookie);
613 args->verf = p; p += 2;
614 args->dircount = ntohl(*p++);
615 args->count = ntohl(*p++);
616
Kinglong Mee3c7aa152014-06-10 18:08:19 +0800617 len = args->count = min(args->count, max_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 while (len > 0) {
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500619 struct page *p = *(rqstp->rq_next_page++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (!args->buffer)
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500621 args->buffer = page_address(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 len -= PAGE_SIZE;
623 }
J. Bruce Fields9512a162017-05-16 15:57:42 -0400624
625 return xdr_argsize_check(rqstp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
628int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200629nfs3svc_decode_commitargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200631 struct nfsd3_commitargs *args = rqstp->rq_argp;
Benoit Tained40aa332014-05-22 16:32:30 +0200632 p = decode_fh(p, &args->fh);
633 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return 0;
635 p = xdr_decode_hyper(p, &args->offset);
636 args->count = ntohl(*p++);
637
638 return xdr_argsize_check(rqstp, p);
639}
640
641/*
642 * XDR encode functions
643 */
Chuck Levercc028a12020-10-02 15:52:44 -0400644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200646nfs3svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 return xdr_ressize_check(rqstp, p);
649}
650
651/* GETATTR */
652int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200653nfs3svc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200655 struct nfsd3_attrstat *resp = rqstp->rq_resp;
656
Chuck Levercc028a12020-10-02 15:52:44 -0400657 *p++ = resp->status;
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400658 if (resp->status == 0) {
David Howells2b0143b2015-03-17 22:25:59 +0000659 lease_get_mtime(d_inode(resp->fh.fh_dentry),
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400660 &resp->stat.mtime);
David Shawa334de22006-01-06 00:19:58 -0800661 p = encode_fattr3(rqstp, p, &resp->fh, &resp->stat);
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return xdr_ressize_check(rqstp, p);
664}
665
666/* SETATTR, REMOVE, RMDIR */
667int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200668nfs3svc_encode_wccstat(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200670 struct nfsd3_attrstat *resp = rqstp->rq_resp;
671
Chuck Levercc028a12020-10-02 15:52:44 -0400672 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 p = encode_wcc_data(rqstp, p, &resp->fh);
674 return xdr_ressize_check(rqstp, p);
675}
676
677/* LOOKUP */
678int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200679nfs3svc_encode_diropres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200681 struct nfsd3_diropres *resp = rqstp->rq_resp;
682
Chuck Levercc028a12020-10-02 15:52:44 -0400683 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (resp->status == 0) {
685 p = encode_fh(p, &resp->fh);
686 p = encode_post_op_attr(rqstp, p, &resp->fh);
687 }
688 p = encode_post_op_attr(rqstp, p, &resp->dirfh);
689 return xdr_ressize_check(rqstp, p);
690}
691
692/* ACCESS */
693int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200694nfs3svc_encode_accessres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200696 struct nfsd3_accessres *resp = rqstp->rq_resp;
697
Chuck Levercc028a12020-10-02 15:52:44 -0400698 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 p = encode_post_op_attr(rqstp, p, &resp->fh);
700 if (resp->status == 0)
701 *p++ = htonl(resp->access);
702 return xdr_ressize_check(rqstp, p);
703}
704
705/* READLINK */
706int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200707nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200709 struct nfsd3_readlinkres *resp = rqstp->rq_resp;
Chuck Lever76e54922020-11-05 10:24:19 -0500710 struct kvec *head = rqstp->rq_res.head;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200711
Chuck Levercc028a12020-10-02 15:52:44 -0400712 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 p = encode_post_op_attr(rqstp, p, &resp->fh);
714 if (resp->status == 0) {
715 *p++ = htonl(resp->len);
716 xdr_ressize_check(rqstp, p);
717 rqstp->rq_res.page_len = resp->len;
718 if (resp->len & 3) {
719 /* need to pad the tail */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 rqstp->rq_res.tail[0].iov_base = p;
721 *p = 0;
722 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
723 }
Chuck Lever76e54922020-11-05 10:24:19 -0500724 if (svc_encode_result_payload(rqstp, head->iov_len, resp->len))
725 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return 1;
727 } else
728 return xdr_ressize_check(rqstp, p);
729}
730
731/* READ */
732int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200733nfs3svc_encode_readres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200735 struct nfsd3_readres *resp = rqstp->rq_resp;
Chuck Lever76e54922020-11-05 10:24:19 -0500736 struct kvec *head = rqstp->rq_res.head;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200737
Chuck Levercc028a12020-10-02 15:52:44 -0400738 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 p = encode_post_op_attr(rqstp, p, &resp->fh);
740 if (resp->status == 0) {
741 *p++ = htonl(resp->count);
742 *p++ = htonl(resp->eof);
743 *p++ = htonl(resp->count); /* xdr opaque count */
744 xdr_ressize_check(rqstp, p);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300745 /* now update rqstp->rq_res to reflect data as well */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 rqstp->rq_res.page_len = resp->count;
747 if (resp->count & 3) {
748 /* need to pad the tail */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 rqstp->rq_res.tail[0].iov_base = p;
750 *p = 0;
751 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count & 3);
752 }
Chuck Lever76e54922020-11-05 10:24:19 -0500753 if (svc_encode_result_payload(rqstp, head->iov_len,
754 resp->count))
755 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return 1;
757 } else
758 return xdr_ressize_check(rqstp, p);
759}
760
761/* WRITE */
762int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200763nfs3svc_encode_writeres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200765 struct nfsd3_writeres *resp = rqstp->rq_resp;
Stanislav Kinsburskyb9c0ef82012-12-06 14:23:19 +0300766
Chuck Levercc028a12020-10-02 15:52:44 -0400767 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 p = encode_wcc_data(rqstp, p, &resp->fh);
769 if (resp->status == 0) {
770 *p++ = htonl(resp->count);
771 *p++ = htonl(resp->committed);
Trond Myklebust19e06632020-01-06 13:40:37 -0500772 *p++ = resp->verf[0];
773 *p++ = resp->verf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775 return xdr_ressize_check(rqstp, p);
776}
777
778/* CREATE, MKDIR, SYMLINK, MKNOD */
779int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200780nfs3svc_encode_createres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200782 struct nfsd3_diropres *resp = rqstp->rq_resp;
783
Chuck Levercc028a12020-10-02 15:52:44 -0400784 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (resp->status == 0) {
786 *p++ = xdr_one;
787 p = encode_fh(p, &resp->fh);
788 p = encode_post_op_attr(rqstp, p, &resp->fh);
789 }
790 p = encode_wcc_data(rqstp, p, &resp->dirfh);
791 return xdr_ressize_check(rqstp, p);
792}
793
794/* RENAME */
795int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200796nfs3svc_encode_renameres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200798 struct nfsd3_renameres *resp = rqstp->rq_resp;
799
Chuck Levercc028a12020-10-02 15:52:44 -0400800 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 p = encode_wcc_data(rqstp, p, &resp->ffh);
802 p = encode_wcc_data(rqstp, p, &resp->tfh);
803 return xdr_ressize_check(rqstp, p);
804}
805
806/* LINK */
807int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200808nfs3svc_encode_linkres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200810 struct nfsd3_linkres *resp = rqstp->rq_resp;
811
Chuck Levercc028a12020-10-02 15:52:44 -0400812 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 p = encode_post_op_attr(rqstp, p, &resp->fh);
814 p = encode_wcc_data(rqstp, p, &resp->tfh);
815 return xdr_ressize_check(rqstp, p);
816}
817
818/* READDIR */
819int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200820nfs3svc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200822 struct nfsd3_readdirres *resp = rqstp->rq_resp;
823
Chuck Levercc028a12020-10-02 15:52:44 -0400824 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 p = encode_post_op_attr(rqstp, p, &resp->fh);
826
827 if (resp->status == 0) {
828 /* stupid readdir cookie */
829 memcpy(p, resp->verf, 8); p += 2;
830 xdr_ressize_check(rqstp, p);
831 if (rqstp->rq_res.head[0].iov_len + (2<<2) > PAGE_SIZE)
832 return 1; /*No room for trailer */
833 rqstp->rq_res.page_len = (resp->count) << 2;
834
835 /* add the 'tail' to the end of the 'head' page - page 0. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 rqstp->rq_res.tail[0].iov_base = p;
837 *p++ = 0; /* no more entries */
838 *p++ = htonl(resp->common.err == nfserr_eof);
839 rqstp->rq_res.tail[0].iov_len = 2<<2;
840 return 1;
841 } else
842 return xdr_ressize_check(rqstp, p);
843}
844
Adrian Bunk3ee6f612006-12-06 20:40:23 -0800845static __be32 *
Al Viro91f07162006-10-19 23:28:57 -0700846encode_entry_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name,
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400847 int namlen, u64 ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 *p++ = xdr_one; /* mark entry present */
850 p = xdr_encode_hyper(p, ino); /* file id */
851 p = xdr_encode_array(p, name, namlen);/* name length & name */
852
853 cd->offset = p; /* remember pointer */
854 p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
855
856 return p;
857}
858
Al Viroefe39652012-04-13 00:32:14 -0400859static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
NeilBrown43b0e7e2015-05-03 09:16:53 +1000861 const char *name, int namlen, u64 ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 struct svc_export *exp;
864 struct dentry *dparent, *dchild;
Al Viroefe39652012-04-13 00:32:14 -0400865 __be32 rv = nfserr_noent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 dparent = cd->fh.fh_dentry;
868 exp = cd->fh.fh_export;
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (isdotent(name, namlen)) {
871 if (namlen == 2) {
872 dchild = dget_parent(dparent);
Al Viroefe39652012-04-13 00:32:14 -0400873 /* filesystem root - cannot return filehandle for ".." */
874 if (dchild == dparent)
875 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 } else
877 dchild = dget(dparent);
878 } else
Al Viro6c2d47982019-10-31 01:21:58 -0400879 dchild = lookup_positive_unlocked(name, dparent, namlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (IS_ERR(dchild))
Al Viroefe39652012-04-13 00:32:14 -0400881 return rv;
J. Bruce Fields8177e6d2009-09-04 14:13:09 -0400882 if (d_mountpoint(dchild))
883 goto out;
NeilBrown43b0e7e2015-05-03 09:16:53 +1000884 if (dchild->d_inode->i_ino != ino)
885 goto out;
Al Viroefe39652012-04-13 00:32:14 -0400886 rv = fh_compose(fhp, exp, dchild, &cd->fh);
J. Bruce Fields8177e6d2009-09-04 14:13:09 -0400887out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 dput(dchild);
889 return rv;
890}
891
NeilBrown43b0e7e2015-05-03 09:16:53 +1000892static __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 -0400893{
J. Bruce Fields068c34c2014-01-09 16:24:35 -0500894 struct svc_fh *fh = &cd->scratch;
Al Viroefe39652012-04-13 00:32:14 -0400895 __be32 err;
J. Bruce Fields8177e6d2009-09-04 14:13:09 -0400896
J. Bruce Fields068c34c2014-01-09 16:24:35 -0500897 fh_init(fh, NFS3_FHSIZE);
NeilBrown43b0e7e2015-05-03 09:16:53 +1000898 err = compose_entry_fh(cd, fh, name, namlen, ino);
J. Bruce Fields8177e6d2009-09-04 14:13:09 -0400899 if (err) {
900 *p++ = 0;
901 *p++ = 0;
J. Bruce Fieldsaed100f2009-09-04 14:40:36 -0400902 goto out;
J. Bruce Fields8177e6d2009-09-04 14:13:09 -0400903 }
J. Bruce Fields068c34c2014-01-09 16:24:35 -0500904 p = encode_post_op_attr(cd->rqstp, p, fh);
J. Bruce Fields8177e6d2009-09-04 14:13:09 -0400905 *p++ = xdr_one; /* yes, a file handle follows */
J. Bruce Fields068c34c2014-01-09 16:24:35 -0500906 p = encode_fh(p, fh);
J. Bruce Fieldsaed100f2009-09-04 14:40:36 -0400907out:
J. Bruce Fields068c34c2014-01-09 16:24:35 -0500908 fh_put(fh);
J. Bruce Fields8177e6d2009-09-04 14:13:09 -0400909 return p;
910}
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912/*
913 * Encode a directory entry. This one works for both normal readdir
914 * and readdirplus.
915 * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
916 * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
917 *
918 * The readdirplus baggage is 1+21 words for post_op_attr, plus the
919 * file handle.
920 */
921
922#define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1)
923#define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
924static int
NeilBrown598b9a52007-03-26 21:32:08 -0800925encode_entry(struct readdir_cd *ccd, const char *name, int namlen,
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400926 loff_t offset, u64 ino, unsigned int d_type, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
928 struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
929 common);
Al Viro91f07162006-10-19 23:28:57 -0700930 __be32 *p = cd->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 caddr_t curr_page_addr = NULL;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500932 struct page ** page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 int slen; /* string (name) length */
934 int elen; /* estimated entry length in words */
935 int num_entry_words = 0; /* actual number of words */
936
937 if (cd->offset) {
938 u64 offset64 = offset;
939
940 if (unlikely(cd->offset1)) {
941 /* we ended up with offset on a page boundary */
942 *cd->offset = htonl(offset64 >> 32);
943 *cd->offset1 = htonl(offset64 & 0xffffffff);
944 cd->offset1 = NULL;
945 } else {
NeilBrown598b9a52007-03-26 21:32:08 -0800946 xdr_encode_hyper(cd->offset, offset64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
NeilBrownb6023452019-03-04 14:08:22 +1100948 cd->offset = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950
951 /*
952 dprintk("encode_entry(%.*s @%ld%s)\n",
953 namlen, name, (long) offset, plus? " plus" : "");
954 */
955
956 /* truncate filename if too long */
Kinglong Mee3c7aa152014-06-10 18:08:19 +0800957 namlen = min(namlen, NFS3_MAXNAMLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 slen = XDR_QUADLEN(namlen);
960 elen = slen + NFS3_ENTRY_BAGGAGE
961 + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
962
963 if (cd->buflen < elen) {
964 cd->common.err = nfserr_toosmall;
965 return -EINVAL;
966 }
967
968 /* determine which page in rq_respages[] we are currently filling */
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500969 for (page = cd->rqstp->rq_respages + 1;
970 page < cd->rqstp->rq_next_page; page++) {
971 curr_page_addr = page_address(*page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 if (((caddr_t)cd->buffer >= curr_page_addr) &&
974 ((caddr_t)cd->buffer < curr_page_addr + PAGE_SIZE))
975 break;
976 }
977
978 if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
979 /* encode entry in current page */
980
981 p = encode_entry_baggage(cd, p, name, namlen, ino);
982
J. Bruce Fields8177e6d2009-09-04 14:13:09 -0400983 if (plus)
NeilBrown43b0e7e2015-05-03 09:16:53 +1000984 p = encode_entryplus_baggage(cd, p, name, namlen, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 num_entry_words = p - cd->buffer;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500986 } else if (*(page+1) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 /* temporarily encode entry into next page, then move back to
988 * current and next page in rq_respages[] */
Al Viro91f07162006-10-19 23:28:57 -0700989 __be32 *p1, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 int len1, len2;
991
992 /* grab next page for temporary storage of entry */
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500993 p1 = tmp = page_address(*(page+1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
996
J. Bruce Fields8177e6d2009-09-04 14:13:09 -0400997 if (plus)
NeilBrown43b0e7e2015-05-03 09:16:53 +1000998 p1 = encode_entryplus_baggage(cd, p1, name, namlen, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 /* determine entry word length and lengths to go in pages */
1001 num_entry_words = p1 - tmp;
1002 len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer;
1003 if ((num_entry_words << 2) < len1) {
1004 /* the actual number of words in the entry is less
1005 * than elen and can still fit in the current page
1006 */
1007 memmove(p, tmp, num_entry_words << 2);
1008 p += num_entry_words;
1009
1010 /* update offset */
1011 cd->offset = cd->buffer + (cd->offset - tmp);
1012 } else {
1013 unsigned int offset_r = (cd->offset - tmp) << 2;
1014
1015 /* update pointer to offset location.
1016 * This is a 64bit quantity, so we need to
1017 * deal with 3 cases:
1018 * - entirely in first page
1019 * - entirely in second page
1020 * - 4 bytes in each page
1021 */
1022 if (offset_r + 8 <= len1) {
1023 cd->offset = p + (cd->offset - tmp);
1024 } else if (offset_r >= len1) {
1025 cd->offset -= len1 >> 2;
1026 } else {
1027 /* sitting on the fence */
1028 BUG_ON(offset_r != len1 - 4);
1029 cd->offset = p + (cd->offset - tmp);
1030 cd->offset1 = tmp;
1031 }
1032
1033 len2 = (num_entry_words << 2) - len1;
1034
1035 /* move from temp page to current and next pages */
1036 memmove(p, tmp, len1);
1037 memmove(tmp, (caddr_t)tmp+len1, len2);
1038
1039 p = tmp + (len2 >> 2);
1040 }
1041 }
1042 else {
1043 cd->common.err = nfserr_toosmall;
1044 return -EINVAL;
1045 }
1046
1047 cd->buflen -= num_entry_words;
1048 cd->buffer = p;
1049 cd->common.err = nfs_ok;
1050 return 0;
1051
1052}
1053
1054int
NeilBrowna0ad13e2007-01-26 00:57:10 -08001055nfs3svc_encode_entry(void *cd, const char *name,
1056 int namlen, loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
1058 return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
1059}
1060
1061int
NeilBrowna0ad13e2007-01-26 00:57:10 -08001062nfs3svc_encode_entry_plus(void *cd, const char *name,
1063 int namlen, loff_t offset, u64 ino,
1064 unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
1067}
1068
1069/* FSSTAT */
1070int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001071nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001073 struct nfsd3_fsstatres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 struct kstatfs *s = &resp->stats;
1075 u64 bs = s->f_bsize;
1076
Chuck Levercc028a12020-10-02 15:52:44 -04001077 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 *p++ = xdr_zero; /* no post_op_attr */
1079
1080 if (resp->status == 0) {
1081 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1082 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1083 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1084 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1085 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1086 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1087 *p++ = htonl(resp->invarsec); /* mean unchanged time */
1088 }
1089 return xdr_ressize_check(rqstp, p);
1090}
1091
1092/* FSINFO */
1093int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001094nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001096 struct nfsd3_fsinfores *resp = rqstp->rq_resp;
1097
Chuck Levercc028a12020-10-02 15:52:44 -04001098 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 *p++ = xdr_zero; /* no post_op_attr */
1100
1101 if (resp->status == 0) {
1102 *p++ = htonl(resp->f_rtmax);
1103 *p++ = htonl(resp->f_rtpref);
1104 *p++ = htonl(resp->f_rtmult);
1105 *p++ = htonl(resp->f_wtmax);
1106 *p++ = htonl(resp->f_wtpref);
1107 *p++ = htonl(resp->f_wtmult);
1108 *p++ = htonl(resp->f_dtpref);
1109 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1110 *p++ = xdr_one;
1111 *p++ = xdr_zero;
1112 *p++ = htonl(resp->f_properties);
1113 }
1114
1115 return xdr_ressize_check(rqstp, p);
1116}
1117
1118/* PATHCONF */
1119int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001120nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001122 struct nfsd3_pathconfres *resp = rqstp->rq_resp;
1123
Chuck Lever1905cac2020-10-23 10:41:01 -04001124 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 *p++ = xdr_zero; /* no post_op_attr */
1126
1127 if (resp->status == 0) {
1128 *p++ = htonl(resp->p_link_max);
1129 *p++ = htonl(resp->p_name_max);
1130 *p++ = htonl(resp->p_no_trunc);
1131 *p++ = htonl(resp->p_chown_restricted);
1132 *p++ = htonl(resp->p_case_insensitive);
1133 *p++ = htonl(resp->p_case_preserving);
1134 }
1135
1136 return xdr_ressize_check(rqstp, p);
1137}
1138
1139/* COMMIT */
1140int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001141nfs3svc_encode_commitres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001143 struct nfsd3_commitres *resp = rqstp->rq_resp;
Stanislav Kinsburskyb9c0ef82012-12-06 14:23:19 +03001144
Chuck Levercc028a12020-10-02 15:52:44 -04001145 *p++ = resp->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 p = encode_wcc_data(rqstp, p, &resp->fh);
1147 /* Write verifier */
1148 if (resp->status == 0) {
Trond Myklebust524ff1a2020-01-06 13:40:36 -05001149 *p++ = resp->verf[0];
1150 *p++ = resp->verf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
1152 return xdr_ressize_check(rqstp, p);
1153}
1154
1155/*
1156 * XDR release functions
1157 */
Christoph Hellwig85374882017-05-08 18:48:24 +02001158void
1159nfs3svc_release_fhandle(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
Christoph Hellwig85374882017-05-08 18:48:24 +02001161 struct nfsd3_attrstat *resp = rqstp->rq_resp;
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 fh_put(&resp->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164}
1165
Christoph Hellwig85374882017-05-08 18:48:24 +02001166void
1167nfs3svc_release_fhandle2(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
Christoph Hellwig85374882017-05-08 18:48:24 +02001169 struct nfsd3_fhandle_pair *resp = rqstp->rq_resp;
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 fh_put(&resp->fh1);
1172 fh_put(&resp->fh2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}