blob: 6c87ea8f38769d5bfae1defa0ca3fc8492a2bf75 [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
4 *
5 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
6 */
7
Al Viro3dadecc2013-01-24 02:18:08 -05008#include "vfs.h"
Boaz Harrosh9a74af22009-12-03 20:30:56 +02009#include "xdr.h"
J. Bruce Fields2e8138a2007-11-15 17:05:43 -050010#include "auth.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12#define NFSDDBG_FACILITY NFSDDBG_XDR
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014/*
15 * Mapping of S_IF* types to NFS file types
16 */
17static u32 nfs_ftypes[] = {
18 NFNON, NFCHR, NFCHR, NFBAD,
19 NFDIR, NFBAD, NFBLK, NFBAD,
20 NFREG, NFBAD, NFLNK, NFBAD,
21 NFSOCK, NFBAD, NFLNK, NFBAD,
22};
23
24
25/*
Chuck Leverebcd8e82020-10-21 12:14:23 -040026 * Basic NFSv2 data types (RFC 1094 Section 2.3)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 */
Chuck Leverebcd8e82020-10-21 12:14:23 -040028
Al Viro131a21c2006-10-19 23:28:56 -070029static __be32 *
30decode_fh(__be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
32 fh_init(fhp, NFS_FHSIZE);
33 memcpy(&fhp->fh_handle.fh_base, p, NFS_FHSIZE);
34 fhp->fh_handle.fh_size = NFS_FHSIZE;
35
36 /* FIXME: Look up export pointer here and verify
37 * Sun Secure RPC if requested */
38 return p + (NFS_FHSIZE >> 2);
39}
40
Chuck Leverebcd8e82020-10-21 12:14:23 -040041static bool
42svcxdr_decode_fhandle(struct xdr_stream *xdr, struct svc_fh *fhp)
43{
44 __be32 *p;
45
46 p = xdr_inline_decode(xdr, NFS_FHSIZE);
47 if (!p)
48 return false;
49 fh_init(fhp, NFS_FHSIZE);
50 memcpy(&fhp->fh_handle.fh_base, p, NFS_FHSIZE);
51 fhp->fh_handle.fh_size = NFS_FHSIZE;
52
53 return true;
54}
55
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000056/* Helper function for NFSv2 ACL code */
Al Viro131a21c2006-10-19 23:28:56 -070057__be32 *nfs2svc_decode_fh(__be32 *p, struct svc_fh *fhp)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +000058{
59 return decode_fh(p, fhp);
60}
61
Adrian Bunk3ee6f612006-12-06 20:40:23 -080062static __be32 *
Al Viro131a21c2006-10-19 23:28:56 -070063encode_fh(__be32 *p, struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 memcpy(p, &fhp->fh_handle.fh_base, NFS_FHSIZE);
66 return p + (NFS_FHSIZE>> 2);
67}
68
69/*
70 * Decode a file name and make sure that the path contains
71 * no slashes or null bytes.
72 */
Adrian Bunk3ee6f612006-12-06 20:40:23 -080073static __be32 *
Chuck Leveree1a95b2007-11-01 16:56:58 -040074decode_filename(__be32 *p, char **namp, unsigned int *lenp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 char *name;
Chuck Leveree1a95b2007-11-01 16:56:58 -040077 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS_MAXNAMLEN)) != NULL) {
80 for (i = 0, name = *namp; i < *lenp; i++, name++) {
81 if (*name == '\0' || *name == '/')
82 return NULL;
83 }
84 }
85
86 return p;
87}
88
Chuck Lever6d742c12020-10-19 14:33:24 -040089static bool
90svcxdr_decode_filename(struct xdr_stream *xdr, char **name, unsigned int *len)
91{
92 u32 size, i;
93 __be32 *p;
94 char *c;
95
96 if (xdr_stream_decode_u32(xdr, &size) < 0)
97 return false;
98 if (size == 0 || size > NFS_MAXNAMLEN)
99 return false;
100 p = xdr_inline_decode(xdr, size);
101 if (!p)
102 return false;
103
104 *len = size;
105 *name = (char *)p;
106 for (i = 0, c = *name; i < size; i++, c++)
107 if (*c == '\0' || *c == '/')
108 return false;
109
110 return true;
111}
112
113static bool
114svcxdr_decode_diropargs(struct xdr_stream *xdr, struct svc_fh *fhp,
115 char **name, unsigned int *len)
116{
117 return svcxdr_decode_fhandle(xdr, fhp) &&
118 svcxdr_decode_filename(xdr, name, len);
119}
120
Adrian Bunk3ee6f612006-12-06 20:40:23 -0800121static __be32 *
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400122decode_sattr(__be32 *p, struct iattr *iap, struct user_namespace *userns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 u32 tmp, tmp1;
125
126 iap->ia_valid = 0;
127
128 /* Sun client bug compatibility check: some sun clients seem to
129 * put 0xffff in the mode field when they mean 0xffffffff.
130 * Quoting the 4.4BSD nfs server code: Nah nah nah nah na nah.
131 */
132 if ((tmp = ntohl(*p++)) != (u32)-1 && tmp != 0xffff) {
133 iap->ia_valid |= ATTR_MODE;
134 iap->ia_mode = tmp;
135 }
136 if ((tmp = ntohl(*p++)) != (u32)-1) {
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400137 iap->ia_uid = make_kuid(userns, tmp);
Eric W. Biederman7c197232013-02-02 04:32:35 -0800138 if (uid_valid(iap->ia_uid))
139 iap->ia_valid |= ATTR_UID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 }
141 if ((tmp = ntohl(*p++)) != (u32)-1) {
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400142 iap->ia_gid = make_kgid(userns, tmp);
Eric W. Biederman7c197232013-02-02 04:32:35 -0800143 if (gid_valid(iap->ia_gid))
144 iap->ia_valid |= ATTR_GID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146 if ((tmp = ntohl(*p++)) != (u32)-1) {
147 iap->ia_valid |= ATTR_SIZE;
148 iap->ia_size = tmp;
149 }
150 tmp = ntohl(*p++); tmp1 = ntohl(*p++);
151 if (tmp != (u32)-1 && tmp1 != (u32)-1) {
152 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
153 iap->ia_atime.tv_sec = tmp;
154 iap->ia_atime.tv_nsec = tmp1 * 1000;
155 }
156 tmp = ntohl(*p++); tmp1 = ntohl(*p++);
157 if (tmp != (u32)-1 && tmp1 != (u32)-1) {
158 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
159 iap->ia_mtime.tv_sec = tmp;
160 iap->ia_mtime.tv_nsec = tmp1 * 1000;
161 /*
162 * Passing the invalid value useconds=1000000 for mtime
163 * is a Sun convention for "set both mtime and atime to
164 * current server time". It's needed to make permissions
165 * checks for the "touch" program across v2 mounts to
166 * Solaris and Irix boxes work correctly. See description of
167 * sattr in section 6.1 of "NFS Illustrated" by
168 * Brent Callaghan, Addison-Wesley, ISBN 0-201-32750-5
169 */
170 if (tmp1 == 1000000)
171 iap->ia_valid &= ~(ATTR_ATIME_SET|ATTR_MTIME_SET);
172 }
173 return p;
174}
175
Chuck Lever2fdd6bd2020-10-21 12:39:06 -0400176static bool
177svcxdr_decode_sattr(struct svc_rqst *rqstp, struct xdr_stream *xdr,
178 struct iattr *iap)
179{
180 u32 tmp1, tmp2;
181 __be32 *p;
182
183 p = xdr_inline_decode(xdr, XDR_UNIT * 8);
184 if (!p)
185 return false;
186
187 iap->ia_valid = 0;
188
189 /*
190 * Some Sun clients put 0xffff in the mode field when they
191 * mean 0xffffffff.
192 */
193 tmp1 = be32_to_cpup(p++);
194 if (tmp1 != (u32)-1 && tmp1 != 0xffff) {
195 iap->ia_valid |= ATTR_MODE;
196 iap->ia_mode = tmp1;
197 }
198
199 tmp1 = be32_to_cpup(p++);
200 if (tmp1 != (u32)-1) {
201 iap->ia_uid = make_kuid(nfsd_user_namespace(rqstp), tmp1);
202 if (uid_valid(iap->ia_uid))
203 iap->ia_valid |= ATTR_UID;
204 }
205
206 tmp1 = be32_to_cpup(p++);
207 if (tmp1 != (u32)-1) {
208 iap->ia_gid = make_kgid(nfsd_user_namespace(rqstp), tmp1);
209 if (gid_valid(iap->ia_gid))
210 iap->ia_valid |= ATTR_GID;
211 }
212
213 tmp1 = be32_to_cpup(p++);
214 if (tmp1 != (u32)-1) {
215 iap->ia_valid |= ATTR_SIZE;
216 iap->ia_size = tmp1;
217 }
218
219 tmp1 = be32_to_cpup(p++);
220 tmp2 = be32_to_cpup(p++);
221 if (tmp1 != (u32)-1 && tmp2 != (u32)-1) {
222 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
223 iap->ia_atime.tv_sec = tmp1;
224 iap->ia_atime.tv_nsec = tmp2 * NSEC_PER_USEC;
225 }
226
227 tmp1 = be32_to_cpup(p++);
228 tmp2 = be32_to_cpup(p++);
229 if (tmp1 != (u32)-1 && tmp2 != (u32)-1) {
230 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
231 iap->ia_mtime.tv_sec = tmp1;
232 iap->ia_mtime.tv_nsec = tmp2 * NSEC_PER_USEC;
233 /*
234 * Passing the invalid value useconds=1000000 for mtime
235 * is a Sun convention for "set both mtime and atime to
236 * current server time". It's needed to make permissions
237 * checks for the "touch" program across v2 mounts to
238 * Solaris and Irix boxes work correctly. See description of
239 * sattr in section 6.1 of "NFS Illustrated" by
240 * Brent Callaghan, Addison-Wesley, ISBN 0-201-32750-5
241 */
242 if (tmp2 == 1000000)
243 iap->ia_valid &= ~(ATTR_ATIME_SET|ATTR_MTIME_SET);
244 }
245
246 return true;
247}
248
Al Viro131a21c2006-10-19 23:28:56 -0700249static __be32 *
250encode_fattr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp,
David Shawa334de22006-01-06 00:19:58 -0800251 struct kstat *stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400253 struct user_namespace *userns = nfsd_user_namespace(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 struct dentry *dentry = fhp->fh_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 int type;
Deepa Dinamani95582b02018-05-08 19:36:02 -0700256 struct timespec64 time;
NeilBrownaf6a4e22007-02-14 00:33:12 -0800257 u32 f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
David Shawa334de22006-01-06 00:19:58 -0800259 type = (stat->mode & S_IFMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 *p++ = htonl(nfs_ftypes[type >> 12]);
J. Bruce Fields082f31a2014-04-03 15:10:35 -0400262 *p++ = htonl((u32) stat->mode);
David Shawa334de22006-01-06 00:19:58 -0800263 *p++ = htonl((u32) stat->nlink);
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400264 *p++ = htonl((u32) from_kuid_munged(userns, stat->uid));
265 *p++ = htonl((u32) from_kgid_munged(userns, stat->gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
David Shawa334de22006-01-06 00:19:58 -0800267 if (S_ISLNK(type) && stat->size > NFS_MAXPATHLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 *p++ = htonl(NFS_MAXPATHLEN);
269 } else {
David Shawa334de22006-01-06 00:19:58 -0800270 *p++ = htonl((u32) stat->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
David Shawa334de22006-01-06 00:19:58 -0800272 *p++ = htonl((u32) stat->blksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (S_ISCHR(type) || S_ISBLK(type))
David Shawa334de22006-01-06 00:19:58 -0800274 *p++ = htonl(new_encode_dev(stat->rdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 else
276 *p++ = htonl(0xffffffff);
David Shawa334de22006-01-06 00:19:58 -0800277 *p++ = htonl((u32) stat->blocks);
NeilBrownaf6a4e22007-02-14 00:33:12 -0800278 switch (fsid_source(fhp)) {
279 default:
280 case FSIDSOURCE_DEV:
David Shawa334de22006-01-06 00:19:58 -0800281 *p++ = htonl(new_encode_dev(stat->dev));
NeilBrownaf6a4e22007-02-14 00:33:12 -0800282 break;
283 case FSIDSOURCE_FSID:
284 *p++ = htonl((u32) fhp->fh_export->ex_fsid);
285 break;
286 case FSIDSOURCE_UUID:
287 f = ((u32*)fhp->fh_export->ex_uuid)[0];
288 f ^= ((u32*)fhp->fh_export->ex_uuid)[1];
289 f ^= ((u32*)fhp->fh_export->ex_uuid)[2];
290 f ^= ((u32*)fhp->fh_export->ex_uuid)[3];
291 *p++ = htonl(f);
292 break;
293 }
David Shawa334de22006-01-06 00:19:58 -0800294 *p++ = htonl((u32) stat->ino);
295 *p++ = htonl((u32) stat->atime.tv_sec);
296 *p++ = htonl(stat->atime.tv_nsec ? stat->atime.tv_nsec / 1000 : 0);
Amir Goldstein76c47942018-01-03 17:14:34 +0200297 time = stat->mtime;
David Howells2b0143b2015-03-17 22:25:59 +0000298 lease_get_mtime(d_inode(dentry), &time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 *p++ = htonl((u32) time.tv_sec);
300 *p++ = htonl(time.tv_nsec ? time.tv_nsec / 1000 : 0);
David Shawa334de22006-01-06 00:19:58 -0800301 *p++ = htonl((u32) stat->ctime.tv_sec);
302 *p++ = htonl(stat->ctime.tv_nsec ? stat->ctime.tv_nsec / 1000 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 return p;
305}
306
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000307/* Helper function for NFSv2 ACL code */
J. Bruce Fields4f4a4fa2013-02-01 15:13:04 -0500308__be32 *nfs2svc_encode_fattr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp, struct kstat *stat)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000309{
J. Bruce Fields4f4a4fa2013-02-01 15:13:04 -0500310 return encode_fattr(rqstp, p, fhp, stat);
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000311}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313/*
314 * XDR decode functions
315 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317int
Chuck Leverebcd8e82020-10-21 12:14:23 -0400318nfssvc_decode_fhandleargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Chuck Leverebcd8e82020-10-21 12:14:23 -0400320 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200321 struct nfsd_fhandle *args = rqstp->rq_argp;
322
Chuck Leverebcd8e82020-10-21 12:14:23 -0400323 return svcxdr_decode_fhandle(xdr, &args->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
326int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200327nfssvc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Chuck Lever2fdd6bd2020-10-21 12:39:06 -0400329 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200330 struct nfsd_sattrargs *args = rqstp->rq_argp;
331
Chuck Lever2fdd6bd2020-10-21 12:39:06 -0400332 return svcxdr_decode_fhandle(xdr, &args->fh) &&
333 svcxdr_decode_sattr(rqstp, xdr, &args->attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
336int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200337nfssvc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Chuck Lever6d742c12020-10-19 14:33:24 -0400339 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200340 struct nfsd_diropargs *args = rqstp->rq_argp;
341
Chuck Lever6d742c12020-10-19 14:33:24 -0400342 return svcxdr_decode_diropargs(xdr, &args->fh, &args->name, &args->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
345int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200346nfssvc_decode_readargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Chuck Lever8c293ef2020-10-21 12:15:51 -0400348 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200349 struct nfsd_readargs *args = rqstp->rq_argp;
Chuck Lever8c293ef2020-10-21 12:15:51 -0400350 u32 totalcount;
351
352 if (!svcxdr_decode_fhandle(xdr, &args->fh))
353 return 0;
354 if (xdr_stream_decode_u32(xdr, &args->offset) < 0)
355 return 0;
356 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
357 return 0;
358 /* totalcount is ignored */
359 if (xdr_stream_decode_u32(xdr, &totalcount) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return 0;
361
Chuck Lever8c293ef2020-10-21 12:15:51 -0400362 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
365int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200366nfssvc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Chuck Levera51b5b72020-10-21 12:18:36 -0400368 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200369 struct nfsd_writeargs *args = rqstp->rq_argp;
J. Bruce Fieldsdb44bac2017-04-25 16:21:34 -0400370 struct kvec *head = rqstp->rq_arg.head;
Chuck Levera51b5b72020-10-21 12:18:36 -0400371 struct kvec *tail = rqstp->rq_arg.tail;
372 u32 beginoffset, totalcount;
373 size_t remaining;
Peter Staubachf34b95682007-05-09 02:34:48 -0700374
Chuck Levera51b5b72020-10-21 12:18:36 -0400375 if (!svcxdr_decode_fhandle(xdr, &args->fh))
376 return 0;
377 /* beginoffset is ignored */
378 if (xdr_stream_decode_u32(xdr, &beginoffset) < 0)
379 return 0;
380 if (xdr_stream_decode_u32(xdr, &args->offset) < 0)
381 return 0;
382 /* totalcount is ignored */
383 if (xdr_stream_decode_u32(xdr, &totalcount) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return 0;
385
Chuck Levera51b5b72020-10-21 12:18:36 -0400386 /* opaque data */
387 if (xdr_stream_decode_u32(xdr, &args->len) < 0)
Peter Staubachf34b95682007-05-09 02:34:48 -0700388 return 0;
Chuck Levera51b5b72020-10-21 12:18:36 -0400389 if (args->len > NFSSVC_MAXBLKSIZE_V2)
J. Bruce Fields13bf9fb2017-04-21 15:26:30 -0400390 return 0;
Chuck Levera51b5b72020-10-21 12:18:36 -0400391 remaining = head->iov_len + rqstp->rq_arg.page_len + tail->iov_len;
392 remaining -= xdr_stream_pos(xdr);
393 if (remaining < xdr_align_size(args->len))
Peter Staubachf34b95682007-05-09 02:34:48 -0700394 return 0;
Chuck Levera51b5b72020-10-21 12:18:36 -0400395 args->first.iov_base = xdr->p;
396 args->first.iov_len = head->iov_len - xdr_stream_pos(xdr);
Peter Staubachf34b95682007-05-09 02:34:48 -0700397
Peter Staubachf34b95682007-05-09 02:34:48 -0700398 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
401int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200402nfssvc_decode_createargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200404 struct nfsd_createargs *args = rqstp->rq_argp;
405
NeilBrown072f62e2007-05-09 02:34:57 -0700406 if ( !(p = decode_fh(p, &args->fh))
407 || !(p = decode_filename(p, &args->name, &args->len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return 0;
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400409 p = decode_sattr(p, &args->attrs, nfsd_user_namespace(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 return xdr_argsize_check(rqstp, p);
412}
413
414int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200415nfssvc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Chuck Lever62aa5572020-10-21 12:35:41 -0400417 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200418 struct nfsd_renameargs *args = rqstp->rq_argp;
419
Chuck Lever62aa5572020-10-21 12:35:41 -0400420 return svcxdr_decode_diropargs(xdr, &args->ffh,
421 &args->fname, &args->flen) &&
422 svcxdr_decode_diropargs(xdr, &args->tfh,
423 &args->tname, &args->tlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200427nfssvc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Chuck Lever77edcdf2020-10-21 12:34:24 -0400429 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200430 struct nfsd_linkargs *args = rqstp->rq_argp;
431
Chuck Lever77edcdf2020-10-21 12:34:24 -0400432 return svcxdr_decode_fhandle(xdr, &args->ffh) &&
433 svcxdr_decode_diropargs(xdr, &args->tfh,
434 &args->tname, &args->tlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
437int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200438nfssvc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Christoph Hellwig026fec72017-05-08 19:01:48 +0200440 struct nfsd_symlinkargs *args = rqstp->rq_argp;
Chuck Lever38a70312018-03-27 10:54:21 -0400441 char *base = (char *)p;
442 size_t xdrlen;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200443
NeilBrown072f62e2007-05-09 02:34:57 -0700444 if ( !(p = decode_fh(p, &args->ffh))
Chuck Lever38a70312018-03-27 10:54:21 -0400445 || !(p = decode_filename(p, &args->fname, &args->flen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return 0;
447
Chuck Lever38a70312018-03-27 10:54:21 -0400448 args->tlen = ntohl(*p++);
449 if (args->tlen == 0)
450 return 0;
451
452 args->first.iov_base = p;
453 args->first.iov_len = rqstp->rq_arg.head[0].iov_len;
454 args->first.iov_len -= (char *)p - base;
455
456 /* This request is never larger than a page. Therefore,
457 * transport will deliver either:
458 * 1. pathname in the pagelist -> sattr is in the tail.
459 * 2. everything in the head buffer -> sattr is in the head.
460 */
461 if (rqstp->rq_arg.page_len) {
462 if (args->tlen != rqstp->rq_arg.page_len)
463 return 0;
464 p = rqstp->rq_arg.tail[0].iov_base;
465 } else {
466 xdrlen = XDR_QUADLEN(args->tlen);
467 if (xdrlen > args->first.iov_len - (8 * sizeof(__be32)))
468 return 0;
469 p += xdrlen;
470 }
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400471 decode_sattr(p, &args->attrs, nfsd_user_namespace(rqstp));
Chuck Lever38a70312018-03-27 10:54:21 -0400472
473 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
476int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200477nfssvc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Chuck Lever86883612020-10-19 14:15:51 -0400479 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200480 struct nfsd_readdirargs *args = rqstp->rq_argp;
481
Chuck Lever86883612020-10-19 14:15:51 -0400482 if (!svcxdr_decode_fhandle(xdr, &args->fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return 0;
Chuck Lever86883612020-10-19 14:15:51 -0400484 if (xdr_stream_decode_u32(xdr, &args->cookie) < 0)
485 return 0;
486 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
487 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Chuck Lever86883612020-10-19 14:15:51 -0400489 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492/*
493 * XDR encode functions
494 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496int
Chuck Levercc028a12020-10-02 15:52:44 -0400497nfssvc_encode_stat(struct svc_rqst *rqstp, __be32 *p)
498{
499 struct nfsd_stat *resp = rqstp->rq_resp;
500
501 *p++ = resp->status;
502 return xdr_ressize_check(rqstp, p);
503}
504
505int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200506nfssvc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200508 struct nfsd_attrstat *resp = rqstp->rq_resp;
509
Chuck Levercc028a12020-10-02 15:52:44 -0400510 *p++ = resp->status;
Chuck Leverf0af2212020-10-01 18:59:49 -0400511 if (resp->status != nfs_ok)
Chuck Levercc028a12020-10-02 15:52:44 -0400512 goto out;
David Shawa334de22006-01-06 00:19:58 -0800513 p = encode_fattr(rqstp, p, &resp->fh, &resp->stat);
Chuck Levercc028a12020-10-02 15:52:44 -0400514out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return xdr_ressize_check(rqstp, p);
516}
517
518int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200519nfssvc_encode_diropres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200521 struct nfsd_diropres *resp = rqstp->rq_resp;
522
Chuck Levercc028a12020-10-02 15:52:44 -0400523 *p++ = resp->status;
Chuck Leverf0af2212020-10-01 18:59:49 -0400524 if (resp->status != nfs_ok)
Chuck Levercc028a12020-10-02 15:52:44 -0400525 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 p = encode_fh(p, &resp->fh);
David Shawa334de22006-01-06 00:19:58 -0800527 p = encode_fattr(rqstp, p, &resp->fh, &resp->stat);
Chuck Levercc028a12020-10-02 15:52:44 -0400528out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return xdr_ressize_check(rqstp, p);
530}
531
532int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200533nfssvc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200535 struct nfsd_readlinkres *resp = rqstp->rq_resp;
Chuck Lever76e54922020-11-05 10:24:19 -0500536 struct kvec *head = rqstp->rq_res.head;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200537
Chuck Levercc028a12020-10-02 15:52:44 -0400538 *p++ = resp->status;
Chuck Leverf0af2212020-10-01 18:59:49 -0400539 if (resp->status != nfs_ok)
540 return xdr_ressize_check(rqstp, p);
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 *p++ = htonl(resp->len);
543 xdr_ressize_check(rqstp, p);
544 rqstp->rq_res.page_len = resp->len;
545 if (resp->len & 3) {
546 /* need to pad the tail */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 rqstp->rq_res.tail[0].iov_base = p;
548 *p = 0;
549 rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3);
550 }
Chuck Lever76e54922020-11-05 10:24:19 -0500551 if (svc_encode_result_payload(rqstp, head->iov_len, resp->len))
552 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return 1;
554}
555
556int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200557nfssvc_encode_readres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200559 struct nfsd_readres *resp = rqstp->rq_resp;
Chuck Lever76e54922020-11-05 10:24:19 -0500560 struct kvec *head = rqstp->rq_res.head;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200561
Chuck Levercc028a12020-10-02 15:52:44 -0400562 *p++ = resp->status;
Chuck Leverf0af2212020-10-01 18:59:49 -0400563 if (resp->status != nfs_ok)
564 return xdr_ressize_check(rqstp, p);
565
David Shawa334de22006-01-06 00:19:58 -0800566 p = encode_fattr(rqstp, p, &resp->fh, &resp->stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 *p++ = htonl(resp->count);
568 xdr_ressize_check(rqstp, p);
569
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300570 /* now update rqstp->rq_res to reflect data as well */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 rqstp->rq_res.page_len = resp->count;
572 if (resp->count & 3) {
573 /* need to pad the tail */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 rqstp->rq_res.tail[0].iov_base = p;
575 *p = 0;
576 rqstp->rq_res.tail[0].iov_len = 4 - (resp->count&3);
577 }
Chuck Lever76e54922020-11-05 10:24:19 -0500578 if (svc_encode_result_payload(rqstp, head->iov_len, resp->count))
579 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return 1;
581}
582
583int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200584nfssvc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200586 struct nfsd_readdirres *resp = rqstp->rq_resp;
587
Chuck Levercc028a12020-10-02 15:52:44 -0400588 *p++ = resp->status;
Chuck Leverf0af2212020-10-01 18:59:49 -0400589 if (resp->status != nfs_ok)
590 return xdr_ressize_check(rqstp, p);
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 xdr_ressize_check(rqstp, p);
593 p = resp->buffer;
594 *p++ = 0; /* no more entries */
595 *p++ = htonl((resp->common.err == nfserr_eof));
596 rqstp->rq_res.page_len = (((unsigned long)p-1) & ~PAGE_MASK)+1;
597
598 return 1;
599}
600
601int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200602nfssvc_encode_statfsres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200604 struct nfsd_statfsres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 struct kstatfs *stat = &resp->stats;
606
Chuck Levercc028a12020-10-02 15:52:44 -0400607 *p++ = resp->status;
Chuck Leverf0af2212020-10-01 18:59:49 -0400608 if (resp->status != nfs_ok)
609 return xdr_ressize_check(rqstp, p);
610
Greg Banks7adae482006-10-04 02:15:47 -0700611 *p++ = htonl(NFSSVC_MAXBLKSIZE_V2); /* max transfer size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 *p++ = htonl(stat->f_bsize);
613 *p++ = htonl(stat->f_blocks);
614 *p++ = htonl(stat->f_bfree);
615 *p++ = htonl(stat->f_bavail);
616 return xdr_ressize_check(rqstp, p);
617}
618
619int
NeilBrowna0ad13e2007-01-26 00:57:10 -0800620nfssvc_encode_entry(void *ccdv, const char *name,
621 int namlen, loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
NeilBrowna0ad13e2007-01-26 00:57:10 -0800623 struct readdir_cd *ccd = ccdv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 struct nfsd_readdirres *cd = container_of(ccd, struct nfsd_readdirres, common);
Al Viro131a21c2006-10-19 23:28:56 -0700625 __be32 *p = cd->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 int buflen, slen;
627
628 /*
629 dprintk("nfsd: entry(%.*s off %ld ino %ld)\n",
630 namlen, name, offset, ino);
631 */
632
633 if (offset > ~((u32) 0)) {
634 cd->common.err = nfserr_fbig;
635 return -EINVAL;
636 }
637 if (cd->offset)
638 *cd->offset = htonl(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Kinglong Mee3c7aa152014-06-10 18:08:19 +0800640 /* truncate filename */
641 namlen = min(namlen, NFS2_MAXNAMLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 slen = XDR_QUADLEN(namlen);
Kinglong Mee3c7aa152014-06-10 18:08:19 +0800643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if ((buflen = cd->buflen - slen - 4) < 0) {
645 cd->common.err = nfserr_toosmall;
646 return -EINVAL;
647 }
Peter Staubach40ee5dc2007-08-16 12:10:07 -0400648 if (ino > ~((u32) 0)) {
649 cd->common.err = nfserr_fbig;
650 return -EINVAL;
651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 *p++ = xdr_one; /* mark entry present */
653 *p++ = htonl((u32) ino); /* file id */
654 p = xdr_encode_array(p, name, namlen);/* name length & name */
655 cd->offset = p; /* remember pointer */
Al Viro131a21c2006-10-19 23:28:56 -0700656 *p++ = htonl(~0U); /* offset of next entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 cd->buflen = buflen;
659 cd->buffer = p;
660 cd->common.err = nfs_ok;
661 return 0;
662}
663
664/*
665 * XDR release functions
666 */
Chuck Lever1841b9b2020-10-01 18:59:44 -0400667void nfssvc_release_attrstat(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Chuck Lever1841b9b2020-10-01 18:59:44 -0400669 struct nfsd_attrstat *resp = rqstp->rq_resp;
670
671 fh_put(&resp->fh);
672}
673
674void nfssvc_release_diropres(struct svc_rqst *rqstp)
675{
676 struct nfsd_diropres *resp = rqstp->rq_resp;
677
678 fh_put(&resp->fh);
679}
680
681void nfssvc_release_readres(struct svc_rqst *rqstp)
682{
683 struct nfsd_readres *resp = rqstp->rq_resp;
Christoph Hellwig85374882017-05-08 18:48:24 +0200684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 fh_put(&resp->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}