blob: ddcc18adfeb1acb1fefbca33705150bd232ca305 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070012/*
13 * Mapping of S_IF* types to NFS file types
14 */
Chuck Lever92b54a42020-10-23 15:28:59 -040015static const u32 nfs_ftypes[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 NFNON, NFCHR, NFCHR, NFBAD,
17 NFDIR, NFBAD, NFBLK, NFBAD,
18 NFREG, NFBAD, NFLNK, NFBAD,
19 NFSOCK, NFBAD, NFLNK, NFBAD,
20};
21
22
23/*
Chuck Leverebcd8e82020-10-21 12:14:23 -040024 * Basic NFSv2 data types (RFC 1094 Section 2.3)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
Chuck Leverebcd8e82020-10-21 12:14:23 -040026
Chuck Leverf8cba472020-11-18 14:38:47 -050027/**
28 * svcxdr_encode_stat - Encode an NFSv2 status code
29 * @xdr: XDR stream
30 * @status: status value to encode
31 *
32 * Return values:
33 * %false: Send buffer space was exhausted
34 * %true: Success
35 */
36bool
Chuck Levera887eae2020-10-23 11:08:02 -040037svcxdr_encode_stat(struct xdr_stream *xdr, __be32 status)
38{
39 __be32 *p;
40
41 p = xdr_reserve_space(xdr, sizeof(status));
42 if (!p)
43 return false;
44 *p = status;
45
46 return true;
47}
48
Chuck Lever635a45d2020-11-17 11:32:04 -050049/**
50 * svcxdr_decode_fhandle - Decode an NFSv2 file handle
51 * @xdr: XDR stream positioned at an encoded NFSv2 FH
52 * @fhp: OUT: filled-in server file handle
53 *
54 * Return values:
55 * %false: The encoded file handle was not valid
56 * %true: @fhp has been initialized
57 */
58bool
Chuck Leverebcd8e82020-10-21 12:14:23 -040059svcxdr_decode_fhandle(struct xdr_stream *xdr, struct svc_fh *fhp)
60{
61 __be32 *p;
62
63 p = xdr_inline_decode(xdr, NFS_FHSIZE);
64 if (!p)
65 return false;
66 fh_init(fhp, NFS_FHSIZE);
NeilBrownd8b26072021-09-02 11:16:32 +100067 memcpy(&fhp->fh_handle.fh_raw, p, NFS_FHSIZE);
Chuck Leverebcd8e82020-10-21 12:14:23 -040068 fhp->fh_handle.fh_size = NFS_FHSIZE;
69
70 return true;
71}
72
Chuck Levere3b4ef22020-10-23 16:44:16 -040073static bool
74svcxdr_encode_fhandle(struct xdr_stream *xdr, const struct svc_fh *fhp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Chuck Levere3b4ef22020-10-23 16:44:16 -040076 __be32 *p;
77
78 p = xdr_reserve_space(xdr, NFS_FHSIZE);
79 if (!p)
80 return false;
NeilBrownd8b26072021-09-02 11:16:32 +100081 memcpy(p, &fhp->fh_handle.fh_raw, NFS_FHSIZE);
Chuck Levere3b4ef22020-10-23 16:44:16 -040082
83 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
Chuck Lever92b54a42020-10-23 15:28:59 -040086static __be32 *
87encode_timeval(__be32 *p, const struct timespec64 *time)
88{
89 *p++ = cpu_to_be32((u32)time->tv_sec);
90 if (time->tv_nsec)
91 *p++ = cpu_to_be32(time->tv_nsec / NSEC_PER_USEC);
92 else
93 *p++ = xdr_zero;
94 return p;
95}
96
Chuck Lever6d742c12020-10-19 14:33:24 -040097static bool
98svcxdr_decode_filename(struct xdr_stream *xdr, char **name, unsigned int *len)
99{
100 u32 size, i;
101 __be32 *p;
102 char *c;
103
104 if (xdr_stream_decode_u32(xdr, &size) < 0)
105 return false;
106 if (size == 0 || size > NFS_MAXNAMLEN)
107 return false;
108 p = xdr_inline_decode(xdr, size);
109 if (!p)
110 return false;
111
112 *len = size;
113 *name = (char *)p;
114 for (i = 0, c = *name; i < size; i++, c++)
115 if (*c == '\0' || *c == '/')
116 return false;
117
118 return true;
119}
120
121static bool
122svcxdr_decode_diropargs(struct xdr_stream *xdr, struct svc_fh *fhp,
123 char **name, unsigned int *len)
124{
125 return svcxdr_decode_fhandle(xdr, fhp) &&
126 svcxdr_decode_filename(xdr, name, len);
127}
128
Chuck Lever2fdd6bd2020-10-21 12:39:06 -0400129static bool
130svcxdr_decode_sattr(struct svc_rqst *rqstp, struct xdr_stream *xdr,
131 struct iattr *iap)
132{
133 u32 tmp1, tmp2;
134 __be32 *p;
135
136 p = xdr_inline_decode(xdr, XDR_UNIT * 8);
137 if (!p)
138 return false;
139
140 iap->ia_valid = 0;
141
142 /*
143 * Some Sun clients put 0xffff in the mode field when they
144 * mean 0xffffffff.
145 */
146 tmp1 = be32_to_cpup(p++);
147 if (tmp1 != (u32)-1 && tmp1 != 0xffff) {
148 iap->ia_valid |= ATTR_MODE;
149 iap->ia_mode = tmp1;
150 }
151
152 tmp1 = be32_to_cpup(p++);
153 if (tmp1 != (u32)-1) {
154 iap->ia_uid = make_kuid(nfsd_user_namespace(rqstp), tmp1);
155 if (uid_valid(iap->ia_uid))
156 iap->ia_valid |= ATTR_UID;
157 }
158
159 tmp1 = be32_to_cpup(p++);
160 if (tmp1 != (u32)-1) {
161 iap->ia_gid = make_kgid(nfsd_user_namespace(rqstp), tmp1);
162 if (gid_valid(iap->ia_gid))
163 iap->ia_valid |= ATTR_GID;
164 }
165
166 tmp1 = be32_to_cpup(p++);
167 if (tmp1 != (u32)-1) {
168 iap->ia_valid |= ATTR_SIZE;
169 iap->ia_size = tmp1;
170 }
171
172 tmp1 = be32_to_cpup(p++);
173 tmp2 = be32_to_cpup(p++);
174 if (tmp1 != (u32)-1 && tmp2 != (u32)-1) {
175 iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET;
176 iap->ia_atime.tv_sec = tmp1;
177 iap->ia_atime.tv_nsec = tmp2 * NSEC_PER_USEC;
178 }
179
180 tmp1 = be32_to_cpup(p++);
181 tmp2 = be32_to_cpup(p++);
182 if (tmp1 != (u32)-1 && tmp2 != (u32)-1) {
183 iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET;
184 iap->ia_mtime.tv_sec = tmp1;
185 iap->ia_mtime.tv_nsec = tmp2 * NSEC_PER_USEC;
186 /*
187 * Passing the invalid value useconds=1000000 for mtime
188 * is a Sun convention for "set both mtime and atime to
189 * current server time". It's needed to make permissions
190 * checks for the "touch" program across v2 mounts to
191 * Solaris and Irix boxes work correctly. See description of
192 * sattr in section 6.1 of "NFS Illustrated" by
193 * Brent Callaghan, Addison-Wesley, ISBN 0-201-32750-5
194 */
195 if (tmp2 == 1000000)
196 iap->ia_valid &= ~(ATTR_ATIME_SET|ATTR_MTIME_SET);
197 }
198
199 return true;
200}
201
Chuck Leverf8cba472020-11-18 14:38:47 -0500202/**
203 * svcxdr_encode_fattr - Encode NFSv2 file attributes
204 * @rqstp: Context of a completed RPC transaction
205 * @xdr: XDR stream
206 * @fhp: File handle to encode
207 * @stat: Attributes to encode
208 *
209 * Return values:
210 * %false: Send buffer space was exhausted
211 * %true: Success
212 */
213bool
Chuck Lever92b54a42020-10-23 15:28:59 -0400214svcxdr_encode_fattr(struct svc_rqst *rqstp, struct xdr_stream *xdr,
215 const struct svc_fh *fhp, const struct kstat *stat)
216{
217 struct user_namespace *userns = nfsd_user_namespace(rqstp);
218 struct dentry *dentry = fhp->fh_dentry;
219 int type = stat->mode & S_IFMT;
220 struct timespec64 time;
221 __be32 *p;
222 u32 fsid;
223
224 p = xdr_reserve_space(xdr, XDR_UNIT * 17);
225 if (!p)
Chuck Levere3b4ef22020-10-23 16:44:16 -0400226 return false;
Chuck Lever92b54a42020-10-23 15:28:59 -0400227
228 *p++ = cpu_to_be32(nfs_ftypes[type >> 12]);
229 *p++ = cpu_to_be32((u32)stat->mode);
230 *p++ = cpu_to_be32((u32)stat->nlink);
231 *p++ = cpu_to_be32((u32)from_kuid_munged(userns, stat->uid));
232 *p++ = cpu_to_be32((u32)from_kgid_munged(userns, stat->gid));
233
234 if (S_ISLNK(type) && stat->size > NFS_MAXPATHLEN)
235 *p++ = cpu_to_be32(NFS_MAXPATHLEN);
236 else
237 *p++ = cpu_to_be32((u32) stat->size);
238 *p++ = cpu_to_be32((u32) stat->blksize);
239 if (S_ISCHR(type) || S_ISBLK(type))
240 *p++ = cpu_to_be32(new_encode_dev(stat->rdev));
241 else
242 *p++ = cpu_to_be32(0xffffffff);
243 *p++ = cpu_to_be32((u32)stat->blocks);
244
245 switch (fsid_source(fhp)) {
246 case FSIDSOURCE_FSID:
247 fsid = (u32)fhp->fh_export->ex_fsid;
248 break;
249 case FSIDSOURCE_UUID:
250 fsid = ((u32 *)fhp->fh_export->ex_uuid)[0];
251 fsid ^= ((u32 *)fhp->fh_export->ex_uuid)[1];
252 fsid ^= ((u32 *)fhp->fh_export->ex_uuid)[2];
253 fsid ^= ((u32 *)fhp->fh_export->ex_uuid)[3];
254 break;
255 default:
256 fsid = new_encode_dev(stat->dev);
257 break;
258 }
259 *p++ = cpu_to_be32(fsid);
260
261 *p++ = cpu_to_be32((u32)stat->ino);
262 p = encode_timeval(p, &stat->atime);
263 time = stat->mtime;
264 lease_get_mtime(d_inode(dentry), &time);
265 p = encode_timeval(p, &time);
266 encode_timeval(p, &stat->ctime);
267
Chuck Levere3b4ef22020-10-23 16:44:16 -0400268 return true;
Chuck Lever92b54a42020-10-23 15:28:59 -0400269}
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271/*
272 * XDR decode functions
273 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275int
Chuck Leverebcd8e82020-10-21 12:14:23 -0400276nfssvc_decode_fhandleargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Chuck Leverebcd8e82020-10-21 12:14:23 -0400278 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200279 struct nfsd_fhandle *args = rqstp->rq_argp;
280
Chuck Leverebcd8e82020-10-21 12:14:23 -0400281 return svcxdr_decode_fhandle(xdr, &args->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
284int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200285nfssvc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Chuck Lever2fdd6bd2020-10-21 12:39:06 -0400287 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200288 struct nfsd_sattrargs *args = rqstp->rq_argp;
289
Chuck Lever2fdd6bd2020-10-21 12:39:06 -0400290 return svcxdr_decode_fhandle(xdr, &args->fh) &&
291 svcxdr_decode_sattr(rqstp, xdr, &args->attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
294int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200295nfssvc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Chuck Lever6d742c12020-10-19 14:33:24 -0400297 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200298 struct nfsd_diropargs *args = rqstp->rq_argp;
299
Chuck Lever6d742c12020-10-19 14:33:24 -0400300 return svcxdr_decode_diropargs(xdr, &args->fh, &args->name, &args->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
303int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200304nfssvc_decode_readargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Chuck Lever8c293ef2020-10-21 12:15:51 -0400306 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200307 struct nfsd_readargs *args = rqstp->rq_argp;
Chuck Lever8c293ef2020-10-21 12:15:51 -0400308 u32 totalcount;
309
310 if (!svcxdr_decode_fhandle(xdr, &args->fh))
311 return 0;
312 if (xdr_stream_decode_u32(xdr, &args->offset) < 0)
313 return 0;
314 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
315 return 0;
316 /* totalcount is ignored */
317 if (xdr_stream_decode_u32(xdr, &totalcount) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return 0;
319
Chuck Lever8c293ef2020-10-21 12:15:51 -0400320 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
323int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200324nfssvc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Chuck Levera51b5b72020-10-21 12:18:36 -0400326 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200327 struct nfsd_writeargs *args = rqstp->rq_argp;
Chuck Levera51b5b72020-10-21 12:18:36 -0400328 u32 beginoffset, totalcount;
Peter Staubachf34b95682007-05-09 02:34:48 -0700329
Chuck Levera51b5b72020-10-21 12:18:36 -0400330 if (!svcxdr_decode_fhandle(xdr, &args->fh))
331 return 0;
332 /* beginoffset is ignored */
333 if (xdr_stream_decode_u32(xdr, &beginoffset) < 0)
334 return 0;
335 if (xdr_stream_decode_u32(xdr, &args->offset) < 0)
336 return 0;
337 /* totalcount is ignored */
338 if (xdr_stream_decode_u32(xdr, &totalcount) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return 0;
340
Chuck Levera51b5b72020-10-21 12:18:36 -0400341 /* opaque data */
342 if (xdr_stream_decode_u32(xdr, &args->len) < 0)
Peter Staubachf34b95682007-05-09 02:34:48 -0700343 return 0;
Chuck Levera51b5b72020-10-21 12:18:36 -0400344 if (args->len > NFSSVC_MAXBLKSIZE_V2)
J. Bruce Fields13bf9fb2017-04-21 15:26:30 -0400345 return 0;
Chuck Leverdae9a6c2021-09-30 17:06:21 -0400346 if (!xdr_stream_subsegment(xdr, &args->payload, args->len))
Peter Staubachf34b95682007-05-09 02:34:48 -0700347 return 0;
348
Peter Staubachf34b95682007-05-09 02:34:48 -0700349 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
352int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200353nfssvc_decode_createargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Chuck Lever7dcf65b2020-10-21 12:43:58 -0400355 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200356 struct nfsd_createargs *args = rqstp->rq_argp;
357
Chuck Lever7dcf65b2020-10-21 12:43:58 -0400358 return svcxdr_decode_diropargs(xdr, &args->fh,
359 &args->name, &args->len) &&
360 svcxdr_decode_sattr(rqstp, xdr, &args->attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200364nfssvc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Chuck Lever62aa5572020-10-21 12:35:41 -0400366 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200367 struct nfsd_renameargs *args = rqstp->rq_argp;
368
Chuck Lever62aa5572020-10-21 12:35:41 -0400369 return svcxdr_decode_diropargs(xdr, &args->ffh,
370 &args->fname, &args->flen) &&
371 svcxdr_decode_diropargs(xdr, &args->tfh,
372 &args->tname, &args->tlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
375int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200376nfssvc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Chuck Lever77edcdf2020-10-21 12:34:24 -0400378 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200379 struct nfsd_linkargs *args = rqstp->rq_argp;
380
Chuck Lever77edcdf2020-10-21 12:34:24 -0400381 return svcxdr_decode_fhandle(xdr, &args->ffh) &&
382 svcxdr_decode_diropargs(xdr, &args->tfh,
383 &args->tname, &args->tlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
386int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200387nfssvc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Chuck Lever09f75a532020-10-21 12:46:03 -0400389 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200390 struct nfsd_symlinkargs *args = rqstp->rq_argp;
Chuck Lever09f75a532020-10-21 12:46:03 -0400391 struct kvec *head = rqstp->rq_arg.head;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200392
Chuck Lever09f75a532020-10-21 12:46:03 -0400393 if (!svcxdr_decode_diropargs(xdr, &args->ffh, &args->fname, &args->flen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return 0;
Chuck Lever09f75a532020-10-21 12:46:03 -0400395 if (xdr_stream_decode_u32(xdr, &args->tlen) < 0)
396 return 0;
Chuck Lever38a70312018-03-27 10:54:21 -0400397 if (args->tlen == 0)
398 return 0;
399
Chuck Lever09f75a532020-10-21 12:46:03 -0400400 args->first.iov_len = head->iov_len - xdr_stream_pos(xdr);
401 args->first.iov_base = xdr_inline_decode(xdr, args->tlen);
402 if (!args->first.iov_base)
403 return 0;
404 return svcxdr_decode_sattr(rqstp, xdr, &args->attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
407int
Christoph Hellwig026fec72017-05-08 19:01:48 +0200408nfssvc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Chuck Lever86883612020-10-19 14:15:51 -0400410 struct xdr_stream *xdr = &rqstp->rq_arg_stream;
Christoph Hellwig026fec72017-05-08 19:01:48 +0200411 struct nfsd_readdirargs *args = rqstp->rq_argp;
412
Chuck Lever86883612020-10-19 14:15:51 -0400413 if (!svcxdr_decode_fhandle(xdr, &args->fh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return 0;
Chuck Lever86883612020-10-19 14:15:51 -0400415 if (xdr_stream_decode_u32(xdr, &args->cookie) < 0)
416 return 0;
417 if (xdr_stream_decode_u32(xdr, &args->count) < 0)
418 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Chuck Lever86883612020-10-19 14:15:51 -0400420 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
423/*
424 * XDR encode functions
425 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427int
Chuck Levera887eae2020-10-23 11:08:02 -0400428nfssvc_encode_statres(struct svc_rqst *rqstp, __be32 *p)
Chuck Levercc028a12020-10-02 15:52:44 -0400429{
Chuck Levera887eae2020-10-23 11:08:02 -0400430 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Chuck Levercc028a12020-10-02 15:52:44 -0400431 struct nfsd_stat *resp = rqstp->rq_resp;
432
Chuck Levera887eae2020-10-23 11:08:02 -0400433 return svcxdr_encode_stat(xdr, resp->status);
Chuck Levercc028a12020-10-02 15:52:44 -0400434}
435
436int
Chuck Lever92b54a42020-10-23 15:28:59 -0400437nfssvc_encode_attrstatres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Chuck Lever92b54a42020-10-23 15:28:59 -0400439 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200440 struct nfsd_attrstat *resp = rqstp->rq_resp;
441
Chuck Lever92b54a42020-10-23 15:28:59 -0400442 if (!svcxdr_encode_stat(xdr, resp->status))
443 return 0;
444 switch (resp->status) {
445 case nfs_ok:
446 if (!svcxdr_encode_fattr(rqstp, xdr, &resp->fh, &resp->stat))
447 return 0;
448 break;
449 }
450
451 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
454int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200455nfssvc_encode_diropres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Chuck Levere3b4ef22020-10-23 16:44:16 -0400457 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200458 struct nfsd_diropres *resp = rqstp->rq_resp;
459
Chuck Levere3b4ef22020-10-23 16:44:16 -0400460 if (!svcxdr_encode_stat(xdr, resp->status))
461 return 0;
462 switch (resp->status) {
463 case nfs_ok:
464 if (!svcxdr_encode_fhandle(xdr, &resp->fh))
465 return 0;
466 if (!svcxdr_encode_fattr(rqstp, xdr, &resp->fh, &resp->stat))
467 return 0;
468 break;
469 }
470
471 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
474int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200475nfssvc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Chuck Leverd9014b02020-10-23 15:41:09 -0400477 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200478 struct nfsd_readlinkres *resp = rqstp->rq_resp;
Chuck Lever76e54922020-11-05 10:24:19 -0500479 struct kvec *head = rqstp->rq_res.head;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200480
Chuck Leverd9014b02020-10-23 15:41:09 -0400481 if (!svcxdr_encode_stat(xdr, resp->status))
Chuck Lever76e54922020-11-05 10:24:19 -0500482 return 0;
Chuck Leverd9014b02020-10-23 15:41:09 -0400483 switch (resp->status) {
484 case nfs_ok:
485 if (xdr_stream_encode_u32(xdr, resp->len) < 0)
486 return 0;
487 xdr_write_pages(xdr, &resp->page, 0, resp->len);
488 if (svc_encode_result_payload(rqstp, head->iov_len, resp->len) < 0)
489 return 0;
490 break;
491 }
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return 1;
494}
495
496int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200497nfssvc_encode_readres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Chuck Levera6f8d9d2020-10-23 16:40:11 -0400499 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200500 struct nfsd_readres *resp = rqstp->rq_resp;
Chuck Lever76e54922020-11-05 10:24:19 -0500501 struct kvec *head = rqstp->rq_res.head;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200502
Chuck Levera6f8d9d2020-10-23 16:40:11 -0400503 if (!svcxdr_encode_stat(xdr, resp->status))
Chuck Lever76e54922020-11-05 10:24:19 -0500504 return 0;
Chuck Levera6f8d9d2020-10-23 16:40:11 -0400505 switch (resp->status) {
506 case nfs_ok:
507 if (!svcxdr_encode_fattr(rqstp, xdr, &resp->fh, &resp->stat))
508 return 0;
509 if (xdr_stream_encode_u32(xdr, resp->count) < 0)
510 return 0;
511 xdr_write_pages(xdr, resp->pages, rqstp->rq_res.page_base,
512 resp->count);
513 if (svc_encode_result_payload(rqstp, head->iov_len, resp->count) < 0)
514 return 0;
515 break;
516 }
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return 1;
519}
520
521int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200522nfssvc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Chuck Lever94c8f8c2020-10-23 16:49:01 -0400524 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200525 struct nfsd_readdirres *resp = rqstp->rq_resp;
Chuck Leverf5dcccd2020-11-14 13:45:35 -0500526 struct xdr_buf *dirlist = &resp->dirlist;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200527
Chuck Lever94c8f8c2020-10-23 16:49:01 -0400528 if (!svcxdr_encode_stat(xdr, resp->status))
529 return 0;
530 switch (resp->status) {
531 case nfs_ok:
Chuck Leverf5dcccd2020-11-14 13:45:35 -0500532 xdr_write_pages(xdr, dirlist->pages, 0, dirlist->len);
Chuck Lever94c8f8c2020-10-23 16:49:01 -0400533 /* no more entries */
534 if (xdr_stream_encode_item_absent(xdr) < 0)
535 return 0;
536 if (xdr_stream_encode_bool(xdr, resp->common.err == nfserr_eof) < 0)
537 return 0;
538 break;
539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 return 1;
542}
543
544int
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200545nfssvc_encode_statfsres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Chuck Leverbf152292020-10-23 19:01:38 -0400547 struct xdr_stream *xdr = &rqstp->rq_res_stream;
Christoph Hellwig63f8de32017-05-08 19:42:02 +0200548 struct nfsd_statfsres *resp = rqstp->rq_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 struct kstatfs *stat = &resp->stats;
550
Chuck Leverbf152292020-10-23 19:01:38 -0400551 if (!svcxdr_encode_stat(xdr, resp->status))
552 return 0;
553 switch (resp->status) {
554 case nfs_ok:
555 p = xdr_reserve_space(xdr, XDR_UNIT * 5);
556 if (!p)
557 return 0;
558 *p++ = cpu_to_be32(NFSSVC_MAXBLKSIZE_V2);
559 *p++ = cpu_to_be32(stat->f_bsize);
560 *p++ = cpu_to_be32(stat->f_blocks);
561 *p++ = cpu_to_be32(stat->f_bfree);
562 *p = cpu_to_be32(stat->f_bavail);
563 break;
564 }
Chuck Leverf0af2212020-10-01 18:59:49 -0400565
Chuck Leverbf152292020-10-23 19:01:38 -0400566 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
Chuck Leverd52532002020-11-13 16:53:17 -0500569/**
570 * nfssvc_encode_nfscookie - Encode a directory offset cookie
571 * @resp: readdir result context
572 * @offset: offset cookie to encode
573 *
Chuck Leverf5dcccd2020-11-14 13:45:35 -0500574 * The buffer space for the offset cookie has already been reserved
575 * by svcxdr_encode_entry_common().
Chuck Leverd52532002020-11-13 16:53:17 -0500576 */
577void nfssvc_encode_nfscookie(struct nfsd_readdirres *resp, u32 offset)
578{
Chuck Leverf5dcccd2020-11-14 13:45:35 -0500579 __be32 cookie = cpu_to_be32(offset);
580
581 if (!resp->cookie_offset)
Chuck Leverd52532002020-11-13 16:53:17 -0500582 return;
583
Chuck Leverf5dcccd2020-11-14 13:45:35 -0500584 write_bytes_to_xdr_buf(&resp->dirlist, resp->cookie_offset, &cookie,
585 sizeof(cookie));
586 resp->cookie_offset = 0;
587}
588
589static bool
590svcxdr_encode_entry_common(struct nfsd_readdirres *resp, const char *name,
591 int namlen, loff_t offset, u64 ino)
592{
593 struct xdr_buf *dirlist = &resp->dirlist;
594 struct xdr_stream *xdr = &resp->xdr;
595
596 if (xdr_stream_encode_item_present(xdr) < 0)
597 return false;
598 /* fileid */
599 if (xdr_stream_encode_u32(xdr, (u32)ino) < 0)
600 return false;
601 /* name */
602 if (xdr_stream_encode_opaque(xdr, name, min(namlen, NFS2_MAXNAMLEN)) < 0)
603 return false;
604 /* cookie */
605 resp->cookie_offset = dirlist->len;
606 if (xdr_stream_encode_u32(xdr, ~0U) < 0)
607 return false;
608
609 return true;
610}
611
612/**
Chuck Lever8a2cf9f2020-11-15 14:30:13 -0500613 * nfssvc_encode_entry - encode one NFSv2 READDIR entry
Chuck Leverf5dcccd2020-11-14 13:45:35 -0500614 * @data: directory context
615 * @name: name of the object to be encoded
616 * @namlen: length of that name, in bytes
617 * @offset: the offset of the previous entry
618 * @ino: the fileid of this entry
619 * @d_type: unused
620 *
621 * Return values:
622 * %0: Entry was successfully encoded.
623 * %-EINVAL: An encoding problem occured, secondary status code in resp->common.err
624 *
625 * On exit, the following fields are updated:
626 * - resp->xdr
627 * - resp->common.err
628 * - resp->cookie_offset
629 */
Chuck Lever8a2cf9f2020-11-15 14:30:13 -0500630int nfssvc_encode_entry(void *data, const char *name, int namlen,
631 loff_t offset, u64 ino, unsigned int d_type)
Chuck Leverf5dcccd2020-11-14 13:45:35 -0500632{
633 struct readdir_cd *ccd = data;
634 struct nfsd_readdirres *resp = container_of(ccd,
635 struct nfsd_readdirres,
636 common);
637 unsigned int starting_length = resp->dirlist.len;
638
639 /* The offset cookie for the previous entry */
640 nfssvc_encode_nfscookie(resp, offset);
641
642 if (!svcxdr_encode_entry_common(resp, name, namlen, offset, ino))
643 goto out_toosmall;
644
645 xdr_commit_encode(&resp->xdr);
646 resp->common.err = nfs_ok;
647 return 0;
648
649out_toosmall:
650 resp->cookie_offset = 0;
651 resp->common.err = nfserr_toosmall;
652 resp->dirlist.len = starting_length;
653 return -EINVAL;
Chuck Leverd52532002020-11-13 16:53:17 -0500654}
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656/*
657 * XDR release functions
658 */
Chuck Lever1841b9b2020-10-01 18:59:44 -0400659void nfssvc_release_attrstat(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Chuck Lever1841b9b2020-10-01 18:59:44 -0400661 struct nfsd_attrstat *resp = rqstp->rq_resp;
662
663 fh_put(&resp->fh);
664}
665
666void nfssvc_release_diropres(struct svc_rqst *rqstp)
667{
668 struct nfsd_diropres *resp = rqstp->rq_resp;
669
670 fh_put(&resp->fh);
671}
672
673void nfssvc_release_readres(struct svc_rqst *rqstp)
674{
675 struct nfsd_readres *resp = rqstp->rq_resp;
Christoph Hellwig85374882017-05-08 18:48:24 +0200676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 fh_put(&resp->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}