blob: 86e5658651f1066e359949571deb0c8ee9eb0a77 [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 *
Al Viro91f07162006-10-19 23:28:57 -070035encode_time3(__be32 *p, struct timespec *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 *
Al Viro91f07162006-10-19 23:28:57 -070042decode_time3(__be32 *p, struct timespec *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);
Deepa Dinamani95582b02018-05-08 19:36:02 -0700170 struct timespec ts;
David Shawa334de22006-01-06 00:19:58 -0800171 *p++ = htonl(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]);
Albert Fluegel6e14b462013-11-18 12:18:01 -0500172 *p++ = htonl((u32) (stat->mode & S_IALLUGO));
David Shawa334de22006-01-06 00:19:58 -0800173 *p++ = htonl((u32) stat->nlink);
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400174 *p++ = htonl((u32) from_kuid_munged(userns, stat->uid));
175 *p++ = htonl((u32) from_kgid_munged(userns, stat->gid));
David Shawa334de22006-01-06 00:19:58 -0800176 if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN);
178 } else {
David Shawa334de22006-01-06 00:19:58 -0800179 p = xdr_encode_hyper(p, (u64) stat->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
David Shawa334de22006-01-06 00:19:58 -0800181 p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9);
182 *p++ = htonl((u32) MAJOR(stat->rdev));
183 *p++ = htonl((u32) MINOR(stat->rdev));
NeilBrownaf6a4e22007-02-14 00:33:12 -0800184 p = encode_fsid(p, fhp);
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400185 p = xdr_encode_hyper(p, stat->ino);
Deepa Dinamani95582b02018-05-08 19:36:02 -0700186 ts = timespec64_to_timespec(stat->atime);
187 p = encode_time3(p, &ts);
188 ts = timespec64_to_timespec(stat->mtime);
189 p = encode_time3(p, &ts);
190 ts = timespec64_to_timespec(stat->ctime);
191 p = encode_time3(p, &ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 return p;
194}
195
Adrian Bunk3ee6f612006-12-06 20:40:23 -0800196static __be32 *
Al Viro91f07162006-10-19 23:28:57 -0700197encode_saved_post_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /* Attributes to follow */
200 *p++ = xdr_one;
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400201 return encode_fattr3(rqstp, p, fhp, &fhp->fh_post_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
204/*
205 * Encode post-operation attributes.
206 * The inode may be NULL if the call failed because of a stale file
207 * handle. In this case, no attributes are returned.
208 */
Al Viro91f07162006-10-19 23:28:57 -0700209static __be32 *
210encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 struct dentry *dentry = fhp->fh_dentry;
David Howells2b0143b2015-03-17 22:25:59 +0000213 if (dentry && d_really_is_positive(dentry)) {
Al Viro3dadecc2013-01-24 02:18:08 -0500214 __be32 err;
David Shawa334de22006-01-06 00:19:58 -0800215 struct kstat stat;
216
Al Viro3dadecc2013-01-24 02:18:08 -0500217 err = fh_getattr(fhp, &stat);
David Shawa334de22006-01-06 00:19:58 -0800218 if (!err) {
219 *p++ = xdr_one; /* attributes follow */
David Howells2b0143b2015-03-17 22:25:59 +0000220 lease_get_mtime(d_inode(dentry), &stat.mtime);
David Shawa334de22006-01-06 00:19:58 -0800221 return encode_fattr3(rqstp, p, fhp, &stat);
222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224 *p++ = xdr_zero;
225 return p;
226}
227
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000228/* Helper for NFSv3 ACLs */
Al Viro91f07162006-10-19 23:28:57 -0700229__be32 *
230nfs3svc_encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000231{
232 return encode_post_op_attr(rqstp, p, fhp);
233}
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235/*
236 * Enocde weak cache consistency data
237 */
Al Viro91f07162006-10-19 23:28:57 -0700238static __be32 *
239encode_wcc_data(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 struct dentry *dentry = fhp->fh_dentry;
242
David Howells2b0143b2015-03-17 22:25:59 +0000243 if (dentry && d_really_is_positive(dentry) && fhp->fh_post_saved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (fhp->fh_pre_saved) {
245 *p++ = xdr_one;
246 p = xdr_encode_hyper(p, (u64) fhp->fh_pre_size);
247 p = encode_time3(p, &fhp->fh_pre_mtime);
248 p = encode_time3(p, &fhp->fh_pre_ctime);
249 } else {
250 *p++ = xdr_zero;
251 }
252 return encode_saved_post_attr(rqstp, p, fhp);
253 }
254 /* no pre- or post-attrs */
255 *p++ = xdr_zero;
256 return encode_post_op_attr(rqstp, p, fhp);
257}
258
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400259/*
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200260 * Fill in the pre_op attr for the wcc data
261 */
262void fill_pre_wcc(struct svc_fh *fhp)
263{
264 struct inode *inode;
265 struct kstat stat;
266 __be32 err;
267
268 if (fhp->fh_pre_saved)
269 return;
270
271 inode = d_inode(fhp->fh_dentry);
272 err = fh_getattr(fhp, &stat);
273 if (err) {
274 /* Grab the times from inode anyway */
275 stat.mtime = inode->i_mtime;
276 stat.ctime = inode->i_ctime;
277 stat.size = inode->i_size;
278 }
279
Deepa Dinamani95582b02018-05-08 19:36:02 -0700280 fhp->fh_pre_mtime = timespec64_to_timespec(stat.mtime);
281 fhp->fh_pre_ctime = timespec64_to_timespec(stat.ctime);
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200282 fhp->fh_pre_size = stat.size;
283 fhp->fh_pre_change = nfsd4_change_attribute(&stat, inode);
284 fhp->fh_pre_saved = true;
285}
286
287/*
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400288 * Fill in the post_op attr for the wcc data
289 */
290void fill_post_wcc(struct svc_fh *fhp)
291{
Al Viro3dadecc2013-01-24 02:18:08 -0500292 __be32 err;
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400293
294 if (fhp->fh_post_saved)
295 printk("nfsd: inode locked twice during operation.\n");
296
Al Viro3dadecc2013-01-24 02:18:08 -0500297 err = fh_getattr(fhp, &fhp->fh_post_attr);
Amir Goldstein39ca1bf2018-01-03 17:14:35 +0200298 fhp->fh_post_change = nfsd4_change_attribute(&fhp->fh_post_attr,
299 d_inode(fhp->fh_dentry));
Neil Brownc1ac3ff2010-12-02 11:14:30 +1100300 if (err) {
Jeff Laytonaaf91ec2015-09-17 08:28:39 -0400301 fhp->fh_post_saved = false;
Neil Brownc1ac3ff2010-12-02 11:14:30 +1100302 /* Grab the ctime anyway - set_change_info might use it */
David Howells2b0143b2015-03-17 22:25:59 +0000303 fhp->fh_post_attr.ctime = d_inode(fhp->fh_dentry)->i_ctime;
Neil Brownc1ac3ff2010-12-02 11:14:30 +1100304 } else
Jeff Laytonaaf91ec2015-09-17 08:28:39 -0400305 fhp->fh_post_saved = true;
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400306}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308/*
309 * XDR decode functions
310 */
311int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200312nfs3svc_decode_fhandle(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200314 struct nfsd_fhandle *args = rqstp->rq_argp;
315
Benoit Tained40aa332014-05-22 16:32:30 +0200316 p = decode_fh(p, &args->fh);
317 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return 0;
319 return xdr_argsize_check(rqstp, p);
320}
321
322int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200323nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200325 struct nfsd3_sattrargs *args = rqstp->rq_argp;
326
Benoit Tained40aa332014-05-22 16:32:30 +0200327 p = decode_fh(p, &args->fh);
328 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return 0;
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400330 p = decode_sattr3(p, &args->attrs, nfsd_user_namespace(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 if ((args->check_guard = ntohl(*p++)) != 0) {
333 struct timespec time;
334 p = decode_time3(p, &time);
335 args->guardtime = time.tv_sec;
336 }
337
338 return xdr_argsize_check(rqstp, p);
339}
340
341int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200342nfs3svc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200344 struct nfsd3_diropargs *args = rqstp->rq_argp;
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (!(p = decode_fh(p, &args->fh))
347 || !(p = decode_filename(p, &args->name, &args->len)))
348 return 0;
349
350 return xdr_argsize_check(rqstp, p);
351}
352
353int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200354nfs3svc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200356 struct nfsd3_accessargs *args = rqstp->rq_argp;
357
Benoit Tained40aa332014-05-22 16:32:30 +0200358 p = decode_fh(p, &args->fh);
359 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return 0;
361 args->access = ntohl(*p++);
362
363 return xdr_argsize_check(rqstp, p);
364}
365
366int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200367nfs3svc_decode_readargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200369 struct nfsd3_readargs *args = rqstp->rq_argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 unsigned int len;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500371 int v;
Greg Banks7adae482006-10-04 02:15:47 -0700372 u32 max_blocksize = svc_max_payload(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Benoit Tained40aa332014-05-22 16:32:30 +0200374 p = decode_fh(p, &args->fh);
375 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return 0;
NeilBrown072f62e2007-05-09 02:34:57 -0700377 p = xdr_decode_hyper(p, &args->offset);
J. Bruce Fields9512a162017-05-16 15:57:42 -0400378
Kinglong Mee3c7aa152014-06-10 18:08:19 +0800379 args->count = ntohl(*p++);
380 len = min(args->count, max_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 /* set up the kvec */
383 v=0;
384 while (len > 0) {
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500385 struct page *p = *(rqstp->rq_next_page++);
386
387 rqstp->rq_vec[v].iov_base = page_address(p);
Kinglong Mee3c7aa152014-06-10 18:08:19 +0800388 rqstp->rq_vec[v].iov_len = min_t(unsigned int, len, PAGE_SIZE);
NeilBrown3cc03b12006-10-04 02:15:47 -0700389 len -= rqstp->rq_vec[v].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 v++;
391 }
392 args->vlen = v;
J. Bruce Fields9512a162017-05-16 15:57:42 -0400393 return xdr_argsize_check(rqstp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
396int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200397nfs3svc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200399 struct nfsd3_writeargs *args = rqstp->rq_argp;
Chuck Lever8154ef22018-03-27 10:54:07 -0400400 unsigned int len, hdr, dlen;
Greg Banks7adae482006-10-04 02:15:47 -0700401 u32 max_blocksize = svc_max_payload(rqstp);
J. Bruce Fieldsdb44bac42017-04-25 16:21:34 -0400402 struct kvec *head = rqstp->rq_arg.head;
403 struct kvec *tail = rqstp->rq_arg.tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Benoit Tained40aa332014-05-22 16:32:30 +0200405 p = decode_fh(p, &args->fh);
406 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return 0;
NeilBrown072f62e2007-05-09 02:34:57 -0700408 p = xdr_decode_hyper(p, &args->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 args->count = ntohl(*p++);
411 args->stable = ntohl(*p++);
412 len = args->len = ntohl(*p++);
J. Bruce Fields13bf9fb2017-04-21 15:26:30 -0400413 if ((void *)p > head->iov_base + head->iov_len)
414 return 0;
Peter Staubachf34b95682007-05-09 02:34:48 -0700415 /*
416 * The count must equal the amount of data passed.
417 */
418 if (args->count != args->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return 0;
420
Peter Staubachf34b95682007-05-09 02:34:48 -0700421 /*
422 * Check to make sure that we got the right number of
423 * bytes.
Peter Staubachf34b95682007-05-09 02:34:48 -0700424 */
J. Bruce Fieldsdb44bac42017-04-25 16:21:34 -0400425 hdr = (void*)p - head->iov_base;
426 dlen = head->iov_len + rqstp->rq_arg.page_len + tail->iov_len - hdr;
Peter Staubachf34b95682007-05-09 02:34:48 -0700427 /*
428 * Round the length of the data which was specified up to
429 * the next multiple of XDR units and then compare that
430 * against the length which was actually received.
NeilBrownba67a392008-01-11 17:06:52 -0500431 * Note that when RPCSEC/GSS (for example) is used, the
432 * data buffer can be padded so dlen might be larger
433 * than required. It must never be smaller.
Peter Staubachf34b95682007-05-09 02:34:48 -0700434 */
NeilBrownba67a392008-01-11 17:06:52 -0500435 if (dlen < XDR_QUADLEN(len)*4)
Peter Staubachf34b95682007-05-09 02:34:48 -0700436 return 0;
437
438 if (args->count > max_blocksize) {
439 args->count = max_blocksize;
440 len = args->len = max_blocksize;
441 }
Chuck Lever8154ef22018-03-27 10:54:07 -0400442
443 args->first.iov_base = (void *)p;
444 args->first.iov_len = head->iov_len - hdr;
Peter Staubachf34b95682007-05-09 02:34:48 -0700445 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
448int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200449nfs3svc_decode_createargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200451 struct nfsd3_createargs *args = rqstp->rq_argp;
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (!(p = decode_fh(p, &args->fh))
454 || !(p = decode_filename(p, &args->name, &args->len)))
455 return 0;
456
457 switch (args->createmode = ntohl(*p++)) {
458 case NFS3_CREATE_UNCHECKED:
459 case NFS3_CREATE_GUARDED:
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400460 p = decode_sattr3(p, &args->attrs, nfsd_user_namespace(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 break;
462 case NFS3_CREATE_EXCLUSIVE:
463 args->verf = p;
464 p += 2;
465 break;
466 default:
467 return 0;
468 }
469
470 return xdr_argsize_check(rqstp, p);
471}
Christoph Hellwig026fec72017-05-08 19:01:48 +0200472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200474nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200476 struct nfsd3_createargs *args = rqstp->rq_argp;
477
NeilBrown072f62e2007-05-09 02:34:57 -0700478 if (!(p = decode_fh(p, &args->fh)) ||
479 !(p = decode_filename(p, &args->name, &args->len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return 0;
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400481 p = decode_sattr3(p, &args->attrs, nfsd_user_namespace(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 return xdr_argsize_check(rqstp, p);
484}
485
486int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200487nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200489 struct nfsd3_symlinkargs *args = rqstp->rq_argp;
Chuck Lever38a70312018-03-27 10:54:21 -0400490 char *base = (char *)p;
491 size_t dlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
NeilBrown072f62e2007-05-09 02:34:57 -0700493 if (!(p = decode_fh(p, &args->ffh)) ||
Chuck Lever38a70312018-03-27 10:54:21 -0400494 !(p = decode_filename(p, &args->fname, &args->flen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return 0;
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400496 p = decode_sattr3(p, &args->attrs, nfsd_user_namespace(rqstp));
NeilBrown072f62e2007-05-09 02:34:57 -0700497
Chuck Lever38a70312018-03-27 10:54:21 -0400498 args->tlen = ntohl(*p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Chuck Lever38a70312018-03-27 10:54:21 -0400500 args->first.iov_base = p;
501 args->first.iov_len = rqstp->rq_arg.head[0].iov_len;
502 args->first.iov_len -= (char *)p - base;
503
504 dlen = args->first.iov_len + rqstp->rq_arg.page_len +
505 rqstp->rq_arg.tail[0].iov_len;
506 if (dlen < XDR_QUADLEN(args->tlen) << 2)
507 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return 1;
509}
510
511int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200512nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200514 struct nfsd3_mknodargs *args = rqstp->rq_argp;
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (!(p = decode_fh(p, &args->fh))
517 || !(p = decode_filename(p, &args->name, &args->len)))
518 return 0;
519
520 args->ftype = ntohl(*p++);
521
522 if (args->ftype == NF3BLK || args->ftype == NF3CHR
NeilBrown072f62e2007-05-09 02:34:57 -0700523 || args->ftype == NF3SOCK || args->ftype == NF3FIFO)
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400524 p = decode_sattr3(p, &args->attrs, nfsd_user_namespace(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 if (args->ftype == NF3BLK || args->ftype == NF3CHR) {
527 args->major = ntohl(*p++);
528 args->minor = ntohl(*p++);
529 }
530
531 return xdr_argsize_check(rqstp, p);
532}
533
534int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200535nfs3svc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200537 struct nfsd3_renameargs *args = rqstp->rq_argp;
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (!(p = decode_fh(p, &args->ffh))
540 || !(p = decode_filename(p, &args->fname, &args->flen))
541 || !(p = decode_fh(p, &args->tfh))
542 || !(p = decode_filename(p, &args->tname, &args->tlen)))
543 return 0;
544
545 return xdr_argsize_check(rqstp, p);
546}
547
548int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200549nfs3svc_decode_readlinkargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200551 struct nfsd3_readlinkargs *args = rqstp->rq_argp;
552
Benoit Tained40aa332014-05-22 16:32:30 +0200553 p = decode_fh(p, &args->fh);
554 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return 0;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500556 args->buffer = page_address(*(rqstp->rq_next_page++));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
J. Bruce Fields9512a162017-05-16 15:57:42 -0400558 return xdr_argsize_check(rqstp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
561int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200562nfs3svc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200564 struct nfsd3_linkargs *args = rqstp->rq_argp;
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (!(p = decode_fh(p, &args->ffh))
567 || !(p = decode_fh(p, &args->tfh))
568 || !(p = decode_filename(p, &args->tname, &args->tlen)))
569 return 0;
570
571 return xdr_argsize_check(rqstp, p);
572}
573
574int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200575nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200577 struct nfsd3_readdirargs *args = rqstp->rq_argp;
Murphy Zhou3c867942019-04-04 14:57:11 +0800578 int len;
NeilBrownf875a792019-03-07 09:49:46 +1100579 u32 max_blocksize = svc_max_payload(rqstp);
580
Benoit Tained40aa332014-05-22 16:32:30 +0200581 p = decode_fh(p, &args->fh);
582 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return 0;
584 p = xdr_decode_hyper(p, &args->cookie);
585 args->verf = p; p += 2;
586 args->dircount = ~0;
587 args->count = ntohl(*p++);
Murphy Zhou3c867942019-04-04 14:57:11 +0800588 len = args->count = min_t(u32, args->count, max_blocksize);
589
590 while (len > 0) {
591 struct page *p = *(rqstp->rq_next_page++);
592 if (!args->buffer)
593 args->buffer = page_address(p);
594 len -= PAGE_SIZE;
595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
J. Bruce Fields9512a162017-05-16 15:57:42 -0400597 return xdr_argsize_check(rqstp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
600int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200601nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200603 struct nfsd3_readdirargs *args = rqstp->rq_argp;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500604 int len;
Greg Banks7adae482006-10-04 02:15:47 -0700605 u32 max_blocksize = svc_max_payload(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Benoit Tained40aa332014-05-22 16:32:30 +0200607 p = decode_fh(p, &args->fh);
608 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return 0;
610 p = xdr_decode_hyper(p, &args->cookie);
611 args->verf = p; p += 2;
612 args->dircount = ntohl(*p++);
613 args->count = ntohl(*p++);
614
Kinglong Mee3c7aa152014-06-10 18:08:19 +0800615 len = args->count = min(args->count, max_blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 while (len > 0) {
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500617 struct page *p = *(rqstp->rq_next_page++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (!args->buffer)
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500619 args->buffer = page_address(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 len -= PAGE_SIZE;
621 }
J. Bruce Fields9512a162017-05-16 15:57:42 -0400622
623 return xdr_argsize_check(rqstp, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
626int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200627nfs3svc_decode_commitargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200629 struct nfsd3_commitargs *args = rqstp->rq_argp;
Benoit Tained40aa332014-05-22 16:32:30 +0200630 p = decode_fh(p, &args->fh);
631 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return 0;
633 p = xdr_decode_hyper(p, &args->offset);
634 args->count = ntohl(*p++);
635
636 return xdr_argsize_check(rqstp, p);
637}
638
639/*
640 * XDR encode functions
641 */
642/*
643 * There must be an encoding function for void results so svc_process
644 * will work properly.
645 */
646int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200647nfs3svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 return xdr_ressize_check(rqstp, p);
650}
651
652/* GETATTR */
653int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200654nfs3svc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200656 struct nfsd3_attrstat *resp = rqstp->rq_resp;
657
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 p = encode_wcc_data(rqstp, p, &resp->fh);
673 return xdr_ressize_check(rqstp, p);
674}
675
676/* LOOKUP */
677int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200678nfs3svc_encode_diropres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200680 struct nfsd3_diropres *resp = rqstp->rq_resp;
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (resp->status == 0) {
683 p = encode_fh(p, &resp->fh);
684 p = encode_post_op_attr(rqstp, p, &resp->fh);
685 }
686 p = encode_post_op_attr(rqstp, p, &resp->dirfh);
687 return xdr_ressize_check(rqstp, p);
688}
689
690/* ACCESS */
691int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200692nfs3svc_encode_accessres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200694 struct nfsd3_accessres *resp = rqstp->rq_resp;
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 p = encode_post_op_attr(rqstp, p, &resp->fh);
697 if (resp->status == 0)
698 *p++ = htonl(resp->access);
699 return xdr_ressize_check(rqstp, p);
700}
701
702/* READLINK */
703int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200704nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200706 struct nfsd3_readlinkres *resp = rqstp->rq_resp;
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 p = encode_post_op_attr(rqstp, p, &resp->fh);
709 if (resp->status == 0) {
710 *p++ = htonl(resp->len);
711 xdr_ressize_check(rqstp, p);
712 rqstp->rq_res.page_len = resp->len;
713 if (resp->len & 3) {
714 /* need to pad the tail */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 rqstp->rq_res.tail[0].iov_base = p;
716 *p = 0;
717 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
718 }
719 return 1;
720 } else
721 return xdr_ressize_check(rqstp, p);
722}
723
724/* READ */
725int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200726nfs3svc_encode_readres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200728 struct nfsd3_readres *resp = rqstp->rq_resp;
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 p = encode_post_op_attr(rqstp, p, &resp->fh);
731 if (resp->status == 0) {
732 *p++ = htonl(resp->count);
733 *p++ = htonl(resp->eof);
734 *p++ = htonl(resp->count); /* xdr opaque count */
735 xdr_ressize_check(rqstp, p);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300736 /* now update rqstp->rq_res to reflect data as well */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 rqstp->rq_res.page_len = resp->count;
738 if (resp->count & 3) {
739 /* need to pad the tail */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 rqstp->rq_res.tail[0].iov_base = p;
741 *p = 0;
742 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count & 3);
743 }
744 return 1;
745 } else
746 return xdr_ressize_check(rqstp, p);
747}
748
749/* WRITE */
750int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200751nfs3svc_encode_writeres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200753 struct nfsd3_writeres *resp = rqstp->rq_resp;
Stanislav Kinsburskyb9c0ef82012-12-06 14:23:19 +0300754 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebust27c438f2019-09-02 13:02:56 -0400755 __be32 verf[2];
Stanislav Kinsburskyb9c0ef82012-12-06 14:23:19 +0300756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 p = encode_wcc_data(rqstp, p, &resp->fh);
758 if (resp->status == 0) {
759 *p++ = htonl(resp->count);
760 *p++ = htonl(resp->committed);
Arnd Bergmann256a89f2017-10-19 12:04:11 +0200761 /* unique identifier, y2038 overflow can be ignored */
Trond Myklebust27c438f2019-09-02 13:02:56 -0400762 nfsd_copy_boot_verifier(verf, nn);
763 *p++ = verf[0];
764 *p++ = verf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766 return xdr_ressize_check(rqstp, p);
767}
768
769/* CREATE, MKDIR, SYMLINK, MKNOD */
770int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200771nfs3svc_encode_createres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200773 struct nfsd3_diropres *resp = rqstp->rq_resp;
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (resp->status == 0) {
776 *p++ = xdr_one;
777 p = encode_fh(p, &resp->fh);
778 p = encode_post_op_attr(rqstp, p, &resp->fh);
779 }
780 p = encode_wcc_data(rqstp, p, &resp->dirfh);
781 return xdr_ressize_check(rqstp, p);
782}
783
784/* RENAME */
785int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200786nfs3svc_encode_renameres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200788 struct nfsd3_renameres *resp = rqstp->rq_resp;
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 p = encode_wcc_data(rqstp, p, &resp->ffh);
791 p = encode_wcc_data(rqstp, p, &resp->tfh);
792 return xdr_ressize_check(rqstp, p);
793}
794
795/* LINK */
796int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200797nfs3svc_encode_linkres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200799 struct nfsd3_linkres *resp = rqstp->rq_resp;
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 p = encode_post_op_attr(rqstp, p, &resp->fh);
802 p = encode_wcc_data(rqstp, p, &resp->tfh);
803 return xdr_ressize_check(rqstp, p);
804}
805
806/* READDIR */
807int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200808nfs3svc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200810 struct nfsd3_readdirres *resp = rqstp->rq_resp;
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 p = encode_post_op_attr(rqstp, p, &resp->fh);
813
814 if (resp->status == 0) {
815 /* stupid readdir cookie */
816 memcpy(p, resp->verf, 8); p += 2;
817 xdr_ressize_check(rqstp, p);
818 if (rqstp->rq_res.head[0].iov_len + (2<<2) > PAGE_SIZE)
819 return 1; /*No room for trailer */
820 rqstp->rq_res.page_len = (resp->count) << 2;
821
822 /* add the 'tail' to the end of the 'head' page - page 0. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 rqstp->rq_res.tail[0].iov_base = p;
824 *p++ = 0; /* no more entries */
825 *p++ = htonl(resp->common.err == nfserr_eof);
826 rqstp->rq_res.tail[0].iov_len = 2<<2;
827 return 1;
828 } else
829 return xdr_ressize_check(rqstp, p);
830}
831
Adrian Bunk3ee6f612006-12-06 20:40:23 -0800832static __be32 *
Al Viro91f07162006-10-19 23:28:57 -0700833encode_entry_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name,
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400834 int namlen, u64 ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
836 *p++ = xdr_one; /* mark entry present */
837 p = xdr_encode_hyper(p, ino); /* file id */
838 p = xdr_encode_array(p, name, namlen);/* name length & name */
839
840 cd->offset = p; /* remember pointer */
841 p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */
842
843 return p;
844}
845
Al Viroefe39652012-04-13 00:32:14 -0400846static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp,
NeilBrown43b0e7e2015-05-03 09:16:53 +1000848 const char *name, int namlen, u64 ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
850 struct svc_export *exp;
851 struct dentry *dparent, *dchild;
Al Viroefe39652012-04-13 00:32:14 -0400852 __be32 rv = nfserr_noent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 dparent = cd->fh.fh_dentry;
855 exp = cd->fh.fh_export;
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (isdotent(name, namlen)) {
858 if (namlen == 2) {
859 dchild = dget_parent(dparent);
Al Viroefe39652012-04-13 00:32:14 -0400860 /* filesystem root - cannot return filehandle for ".." */
861 if (dchild == dparent)
862 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 } else
864 dchild = dget(dparent);
865 } else
NeilBrownbbddca82016-01-07 16:08:20 -0500866 dchild = lookup_one_len_unlocked(name, dparent, namlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (IS_ERR(dchild))
Al Viroefe39652012-04-13 00:32:14 -0400868 return rv;
J. Bruce Fields8177e6d2009-09-04 14:13:09 -0400869 if (d_mountpoint(dchild))
870 goto out;
David Howells2b0143b2015-03-17 22:25:59 +0000871 if (d_really_is_negative(dchild))
J. Bruce Fields8177e6d2009-09-04 14:13:09 -0400872 goto out;
NeilBrown43b0e7e2015-05-03 09:16:53 +1000873 if (dchild->d_inode->i_ino != ino)
874 goto out;
Al Viroefe39652012-04-13 00:32:14 -0400875 rv = fh_compose(fhp, exp, dchild, &cd->fh);
J. Bruce Fields8177e6d2009-09-04 14:13:09 -0400876out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 dput(dchild);
878 return rv;
879}
880
NeilBrown43b0e7e2015-05-03 09:16:53 +1000881static __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 -0400882{
J. Bruce Fields068c34c2014-01-09 16:24:35 -0500883 struct svc_fh *fh = &cd->scratch;
Al Viroefe39652012-04-13 00:32:14 -0400884 __be32 err;
J. Bruce Fields8177e6d2009-09-04 14:13:09 -0400885
J. Bruce Fields068c34c2014-01-09 16:24:35 -0500886 fh_init(fh, NFS3_FHSIZE);
NeilBrown43b0e7e2015-05-03 09:16:53 +1000887 err = compose_entry_fh(cd, fh, name, namlen, ino);
J. Bruce Fields8177e6d2009-09-04 14:13:09 -0400888 if (err) {
889 *p++ = 0;
890 *p++ = 0;
J. Bruce Fieldsaed100f2009-09-04 14:40:36 -0400891 goto out;
J. Bruce Fields8177e6d2009-09-04 14:13:09 -0400892 }
J. Bruce Fields068c34c2014-01-09 16:24:35 -0500893 p = encode_post_op_attr(cd->rqstp, p, fh);
J. Bruce Fields8177e6d2009-09-04 14:13:09 -0400894 *p++ = xdr_one; /* yes, a file handle follows */
J. Bruce Fields068c34c2014-01-09 16:24:35 -0500895 p = encode_fh(p, fh);
J. Bruce Fieldsaed100f2009-09-04 14:40:36 -0400896out:
J. Bruce Fields068c34c2014-01-09 16:24:35 -0500897 fh_put(fh);
J. Bruce Fields8177e6d2009-09-04 14:13:09 -0400898 return p;
899}
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901/*
902 * Encode a directory entry. This one works for both normal readdir
903 * and readdirplus.
904 * The normal readdir reply requires 2 (fileid) + 1 (stringlen)
905 * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen.
906 *
907 * The readdirplus baggage is 1+21 words for post_op_attr, plus the
908 * file handle.
909 */
910
911#define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1)
912#define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2))
913static int
NeilBrown598b9a52007-03-26 21:32:08 -0800914encode_entry(struct readdir_cd *ccd, const char *name, int namlen,
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400915 loff_t offset, u64 ino, unsigned int d_type, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres,
918 common);
Al Viro91f07162006-10-19 23:28:57 -0700919 __be32 *p = cd->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 caddr_t curr_page_addr = NULL;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500921 struct page ** page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 int slen; /* string (name) length */
923 int elen; /* estimated entry length in words */
924 int num_entry_words = 0; /* actual number of words */
925
926 if (cd->offset) {
927 u64 offset64 = offset;
928
929 if (unlikely(cd->offset1)) {
930 /* we ended up with offset on a page boundary */
931 *cd->offset = htonl(offset64 >> 32);
932 *cd->offset1 = htonl(offset64 & 0xffffffff);
933 cd->offset1 = NULL;
934 } else {
NeilBrown598b9a52007-03-26 21:32:08 -0800935 xdr_encode_hyper(cd->offset, offset64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
NeilBrownb6023452019-03-04 14:08:22 +1100937 cd->offset = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
939
940 /*
941 dprintk("encode_entry(%.*s @%ld%s)\n",
942 namlen, name, (long) offset, plus? " plus" : "");
943 */
944
945 /* truncate filename if too long */
Kinglong Mee3c7aa152014-06-10 18:08:19 +0800946 namlen = min(namlen, NFS3_MAXNAMLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 slen = XDR_QUADLEN(namlen);
949 elen = slen + NFS3_ENTRY_BAGGAGE
950 + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0);
951
952 if (cd->buflen < elen) {
953 cd->common.err = nfserr_toosmall;
954 return -EINVAL;
955 }
956
957 /* determine which page in rq_respages[] we are currently filling */
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500958 for (page = cd->rqstp->rq_respages + 1;
959 page < cd->rqstp->rq_next_page; page++) {
960 curr_page_addr = page_address(*page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 if (((caddr_t)cd->buffer >= curr_page_addr) &&
963 ((caddr_t)cd->buffer < curr_page_addr + PAGE_SIZE))
964 break;
965 }
966
967 if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) {
968 /* encode entry in current page */
969
970 p = encode_entry_baggage(cd, p, name, namlen, ino);
971
J. Bruce Fields8177e6d2009-09-04 14:13:09 -0400972 if (plus)
NeilBrown43b0e7e2015-05-03 09:16:53 +1000973 p = encode_entryplus_baggage(cd, p, name, namlen, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 num_entry_words = p - cd->buffer;
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500975 } else if (*(page+1) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 /* temporarily encode entry into next page, then move back to
977 * current and next page in rq_respages[] */
Al Viro91f07162006-10-19 23:28:57 -0700978 __be32 *p1, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 int len1, len2;
980
981 /* grab next page for temporary storage of entry */
J. Bruce Fieldsafc59402012-12-10 18:01:37 -0500982 p1 = tmp = page_address(*(page+1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 p1 = encode_entry_baggage(cd, p1, name, namlen, ino);
985
J. Bruce Fields8177e6d2009-09-04 14:13:09 -0400986 if (plus)
NeilBrown43b0e7e2015-05-03 09:16:53 +1000987 p1 = encode_entryplus_baggage(cd, p1, name, namlen, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 /* determine entry word length and lengths to go in pages */
990 num_entry_words = p1 - tmp;
991 len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer;
992 if ((num_entry_words << 2) < len1) {
993 /* the actual number of words in the entry is less
994 * than elen and can still fit in the current page
995 */
996 memmove(p, tmp, num_entry_words << 2);
997 p += num_entry_words;
998
999 /* update offset */
1000 cd->offset = cd->buffer + (cd->offset - tmp);
1001 } else {
1002 unsigned int offset_r = (cd->offset - tmp) << 2;
1003
1004 /* update pointer to offset location.
1005 * This is a 64bit quantity, so we need to
1006 * deal with 3 cases:
1007 * - entirely in first page
1008 * - entirely in second page
1009 * - 4 bytes in each page
1010 */
1011 if (offset_r + 8 <= len1) {
1012 cd->offset = p + (cd->offset - tmp);
1013 } else if (offset_r >= len1) {
1014 cd->offset -= len1 >> 2;
1015 } else {
1016 /* sitting on the fence */
1017 BUG_ON(offset_r != len1 - 4);
1018 cd->offset = p + (cd->offset - tmp);
1019 cd->offset1 = tmp;
1020 }
1021
1022 len2 = (num_entry_words << 2) - len1;
1023
1024 /* move from temp page to current and next pages */
1025 memmove(p, tmp, len1);
1026 memmove(tmp, (caddr_t)tmp+len1, len2);
1027
1028 p = tmp + (len2 >> 2);
1029 }
1030 }
1031 else {
1032 cd->common.err = nfserr_toosmall;
1033 return -EINVAL;
1034 }
1035
1036 cd->buflen -= num_entry_words;
1037 cd->buffer = p;
1038 cd->common.err = nfs_ok;
1039 return 0;
1040
1041}
1042
1043int
NeilBrowna0ad13e2007-01-26 00:57:10 -08001044nfs3svc_encode_entry(void *cd, const char *name,
1045 int namlen, loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 return encode_entry(cd, name, namlen, offset, ino, d_type, 0);
1048}
1049
1050int
NeilBrowna0ad13e2007-01-26 00:57:10 -08001051nfs3svc_encode_entry_plus(void *cd, const char *name,
1052 int namlen, loff_t offset, u64 ino,
1053 unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
1055 return encode_entry(cd, name, namlen, offset, ino, d_type, 1);
1056}
1057
1058/* FSSTAT */
1059int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001060nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001062 struct nfsd3_fsstatres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 struct kstatfs *s = &resp->stats;
1064 u64 bs = s->f_bsize;
1065
1066 *p++ = xdr_zero; /* no post_op_attr */
1067
1068 if (resp->status == 0) {
1069 p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */
1070 p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */
1071 p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */
1072 p = xdr_encode_hyper(p, s->f_files); /* total inodes */
1073 p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */
1074 p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */
1075 *p++ = htonl(resp->invarsec); /* mean unchanged time */
1076 }
1077 return xdr_ressize_check(rqstp, p);
1078}
1079
1080/* FSINFO */
1081int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001082nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001084 struct nfsd3_fsinfores *resp = rqstp->rq_resp;
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 *p++ = xdr_zero; /* no post_op_attr */
1087
1088 if (resp->status == 0) {
1089 *p++ = htonl(resp->f_rtmax);
1090 *p++ = htonl(resp->f_rtpref);
1091 *p++ = htonl(resp->f_rtmult);
1092 *p++ = htonl(resp->f_wtmax);
1093 *p++ = htonl(resp->f_wtpref);
1094 *p++ = htonl(resp->f_wtmult);
1095 *p++ = htonl(resp->f_dtpref);
1096 p = xdr_encode_hyper(p, resp->f_maxfilesize);
1097 *p++ = xdr_one;
1098 *p++ = xdr_zero;
1099 *p++ = htonl(resp->f_properties);
1100 }
1101
1102 return xdr_ressize_check(rqstp, p);
1103}
1104
1105/* PATHCONF */
1106int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001107nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001109 struct nfsd3_pathconfres *resp = rqstp->rq_resp;
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 *p++ = xdr_zero; /* no post_op_attr */
1112
1113 if (resp->status == 0) {
1114 *p++ = htonl(resp->p_link_max);
1115 *p++ = htonl(resp->p_name_max);
1116 *p++ = htonl(resp->p_no_trunc);
1117 *p++ = htonl(resp->p_chown_restricted);
1118 *p++ = htonl(resp->p_case_insensitive);
1119 *p++ = htonl(resp->p_case_preserving);
1120 }
1121
1122 return xdr_ressize_check(rqstp, p);
1123}
1124
1125/* COMMIT */
1126int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001127nfs3svc_encode_commitres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02001129 struct nfsd3_commitres *resp = rqstp->rq_resp;
Stanislav Kinsburskyb9c0ef82012-12-06 14:23:19 +03001130 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Trond Myklebust27c438f2019-09-02 13:02:56 -04001131 __be32 verf[2];
Stanislav Kinsburskyb9c0ef82012-12-06 14:23:19 +03001132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 p = encode_wcc_data(rqstp, p, &resp->fh);
1134 /* Write verifier */
1135 if (resp->status == 0) {
Arnd Bergmann256a89f2017-10-19 12:04:11 +02001136 /* unique identifier, y2038 overflow can be ignored */
Trond Myklebust27c438f2019-09-02 13:02:56 -04001137 nfsd_copy_boot_verifier(verf, nn);
1138 *p++ = verf[0];
1139 *p++ = verf[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
1141 return xdr_ressize_check(rqstp, p);
1142}
1143
1144/*
1145 * XDR release functions
1146 */
Christoph Hellwig85374882017-05-08 18:48:24 +02001147void
1148nfs3svc_release_fhandle(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Christoph Hellwig85374882017-05-08 18:48:24 +02001150 struct nfsd3_attrstat *resp = rqstp->rq_resp;
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 fh_put(&resp->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
Christoph Hellwig85374882017-05-08 18:48:24 +02001155void
1156nfs3svc_release_fhandle2(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Christoph Hellwig85374882017-05-08 18:48:24 +02001158 struct nfsd3_fhandle_pair *resp = rqstp->rq_resp;
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 fh_put(&resp->fh1);
1161 fh_put(&resp->fh2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}