blob: 7142b0501451e56bd79f086b8888d44073536202 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Server-side XDR for NFSv4
3 *
4 * Copyright (c) 2002 The Regents of the University of Michigan.
5 * All rights reserved.
6 *
7 * Kendrick Smith <kmsmith@umich.edu>
8 * Andy Adamson <andros@umich.edu>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 */
35
Christoph Hellwig96bcad52015-06-18 16:45:01 +020036#include <linux/file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/namei.h>
Boaz Harrosh341eb182009-12-03 20:29:12 +020039#include <linux/statfs.h>
Andy Adamson0733d212009-04-03 08:28:01 +030040#include <linux/utsname.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040041#include <linux/pagemap.h>
J. Bruce Fields4796f452007-07-17 04:04:51 -070042#include <linux/sunrpc/svcauth_gss.h>
Olga Kornievskaia84e1b212019-09-13 14:00:57 -040043#include <linux/sunrpc/addr.h>
Frank van der Linden23e50fe2020-06-23 22:39:26 +000044#include <linux/xattr.h>
45#include <uapi/linux/xattr.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020046
J. Bruce Fields2ca72e12011-01-04 17:37:15 -050047#include "idmap.h"
48#include "acl.h"
Boaz Harrosh9a74af22009-12-03 20:30:56 +020049#include "xdr4.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050050#include "vfs.h"
Bryan Schumaker17456802011-07-13 10:50:48 -040051#include "state.h"
J. Bruce Fields10910062011-01-24 12:11:02 -050052#include "cache.h"
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +030053#include "netns.h"
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020054#include "pnfs.h"
Jeff Layton5c4583b2019-08-18 14:18:54 -040055#include "filecache.h"
J. Bruce Fields2ca72e12011-01-04 17:37:15 -050056
Chuck Lever08281342020-11-21 11:36:42 -050057#include "trace.h"
58
David Quigley18032ca2013-05-02 13:19:10 -040059#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
60#include <linux/security.h>
61#endif
62
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#define NFSDDBG_FACILITY NFSDDBG_XDR
65
J. Bruce Fields5cf23db2017-01-11 20:40:36 -050066const u32 nfsd_suppattrs[3][3] = {
J. Bruce Fields916d2d82016-10-18 14:18:40 -040067 {NFSD4_SUPPORTED_ATTRS_WORD0,
68 NFSD4_SUPPORTED_ATTRS_WORD1,
69 NFSD4_SUPPORTED_ATTRS_WORD2},
70
71 {NFSD4_1_SUPPORTED_ATTRS_WORD0,
72 NFSD4_1_SUPPORTED_ATTRS_WORD1,
73 NFSD4_1_SUPPORTED_ATTRS_WORD2},
74
75 {NFSD4_1_SUPPORTED_ATTRS_WORD0,
76 NFSD4_1_SUPPORTED_ATTRS_WORD1,
77 NFSD4_2_SUPPORTED_ATTRS_WORD2},
78};
79
J.Bruce Fields42ca0992006-10-04 02:16:20 -070080/*
81 * As per referral draft, the fsid for a referral MUST be different from the fsid of the containing
82 * directory in order to indicate to the client that a filesystem boundary is present
83 * We use a fixed fsid for a referral
84 */
85#define NFS4_REFERRAL_FSID_MAJOR 0x8000000ULL
86#define NFS4_REFERRAL_FSID_MINOR 0x8000000ULL
87
Al Virob37ad282006-10-19 23:28:59 -070088static __be32
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -050089check_filename(char *str, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 int i;
92
93 if (len == 0)
94 return nfserr_inval;
Chuck Lever000dfa12020-11-03 13:24:10 -050095 if (len > NFS4_MAXNAMLEN)
96 return nfserr_nametoolong;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (isdotent(str, len))
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -050098 return nfserr_badname;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 for (i = 0; i < len; i++)
100 if (str[i] == '/')
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500101 return nfserr_badname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return 0;
103}
104
Andy Adamson60adfc52009-04-03 08:28:50 +0300105static int zero_clientid(clientid_t *clid)
106{
107 return (clid->cl_boot == 0) && (clid->cl_id == 0);
108}
109
Christoph Hellwig2d8498d2013-11-20 00:24:11 -0800110/**
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400111 * svcxdr_tmpalloc - allocate memory to be freed after compound processing
J. Bruce Fieldsce043ac2014-06-24 16:51:12 -0400112 * @argp: NFSv4 compound argument structure
Trond Myklebusted992752019-08-18 14:18:59 -0400113 * @len: length of buffer to allocate
Christoph Hellwig2d8498d2013-11-20 00:24:11 -0800114 *
Trond Myklebusted992752019-08-18 14:18:59 -0400115 * Allocates a buffer of size @len to be freed when processing the compound
116 * operation described in @argp finishes.
Christoph Hellwig2d8498d2013-11-20 00:24:11 -0800117 */
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400118static void *
119svcxdr_tmpalloc(struct nfsd4_compoundargs *argp, u32 len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400121 struct svcxdr_tmpbuf *tb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400123 tb = kmalloc(sizeof(*tb) + len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (!tb)
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400125 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 tb->next = argp->to_free;
127 argp->to_free = tb;
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400128 return tb->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
J. Bruce Fields29c353b2014-06-24 17:06:51 -0400131/*
132 * For xdr strings that need to be passed to other kernel api's
133 * as null-terminated strings.
134 *
135 * Note null-terminating in place usually isn't safe since the
136 * buffer might end on a page boundary.
137 */
138static char *
139svcxdr_dupstr(struct nfsd4_compoundargs *argp, void *buf, u32 len)
140{
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400141 char *p = svcxdr_tmpalloc(argp, len + 1);
J. Bruce Fields29c353b2014-06-24 17:06:51 -0400142
143 if (!p)
144 return NULL;
145 memcpy(p, buf, len);
146 p[len] = '\0';
J. Bruce Fields29c353b2014-06-24 17:06:51 -0400147 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Chuck Lever5dcbfab2020-11-04 11:41:55 -0500150/*
151 * NFSv4 basic data type decoders
152 */
153
Chuck Lever1a994402020-11-03 13:09:34 -0500154/*
155 * This helper handles variable-length opaques which belong to protocol
156 * elements that this implementation does not support.
157 */
158static __be32
159nfsd4_decode_ignored_string(struct nfsd4_compoundargs *argp, u32 maxlen)
160{
161 u32 len;
162
163 if (xdr_stream_decode_u32(argp->xdr, &len) < 0)
164 return nfserr_bad_xdr;
165 if (maxlen && len > maxlen)
166 return nfserr_bad_xdr;
167 if (!xdr_inline_decode(argp->xdr, len))
168 return nfserr_bad_xdr;
169
170 return nfs_ok;
171}
172
Chuck Lever5dcbfab2020-11-04 11:41:55 -0500173static __be32
174nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
175{
176 __be32 *p;
177 u32 len;
178
179 if (xdr_stream_decode_u32(argp->xdr, &len) < 0)
180 return nfserr_bad_xdr;
181 if (len == 0 || len > NFS4_OPAQUE_LIMIT)
182 return nfserr_bad_xdr;
183 p = xdr_inline_decode(argp->xdr, len);
184 if (!p)
185 return nfserr_bad_xdr;
186 o->data = svcxdr_tmpalloc(argp, len);
187 if (!o->data)
188 return nfserr_jukebox;
189 o->len = len;
190 memcpy(o->data, p, len);
191
192 return nfs_ok;
193}
194
Christoph Hellwig4c94e132015-01-22 12:09:50 +0100195static __be32
Chuck Lever000dfa12020-11-03 13:24:10 -0500196nfsd4_decode_component4(struct nfsd4_compoundargs *argp, char **namp, u32 *lenp)
197{
198 __be32 *p, status;
199
200 if (xdr_stream_decode_u32(argp->xdr, lenp) < 0)
201 return nfserr_bad_xdr;
202 p = xdr_inline_decode(argp->xdr, *lenp);
203 if (!p)
204 return nfserr_bad_xdr;
205 status = check_filename((char *)p, *lenp);
206 if (status)
207 return status;
208 *namp = svcxdr_tmpalloc(argp, *lenp);
209 if (!*namp)
210 return nfserr_jukebox;
211 memcpy(*namp, p, *lenp);
212
213 return nfs_ok;
214}
215
216static __be32
Chuck Lever1c3eff72020-11-19 14:01:08 -0500217nfsd4_decode_nfstime4(struct nfsd4_compoundargs *argp, struct timespec64 *tv)
218{
219 __be32 *p;
220
221 p = xdr_inline_decode(argp->xdr, XDR_UNIT * 3);
222 if (!p)
223 return nfserr_bad_xdr;
224 p = xdr_decode_hyper(p, &tv->tv_sec);
225 tv->tv_nsec = be32_to_cpup(p++);
226 if (tv->tv_nsec >= (u32)1000000000)
227 return nfserr_inval;
228 return nfs_ok;
229}
230
231static __be32
Chuck Lever796dd1c2020-11-16 17:34:01 -0500232nfsd4_decode_verifier4(struct nfsd4_compoundargs *argp, nfs4_verifier *verf)
233{
234 __be32 *p;
235
236 p = xdr_inline_decode(argp->xdr, NFS4_VERIFIER_SIZE);
237 if (!p)
238 return nfserr_bad_xdr;
239 memcpy(verf->data, p, sizeof(verf->data));
240 return nfs_ok;
241}
242
Chuck Leverd1c263a2020-11-03 12:56:05 -0500243/**
244 * nfsd4_decode_bitmap4 - Decode an NFSv4 bitmap4
245 * @argp: NFSv4 compound argument structure
246 * @bmval: pointer to an array of u32's to decode into
247 * @bmlen: size of the @bmval array
248 *
249 * The server needs to return nfs_ok rather than nfserr_bad_xdr when
250 * encountering bitmaps containing bits it does not recognize. This
251 * includes bits in bitmap words past WORDn, where WORDn is the last
252 * bitmap WORD the implementation currently supports. Thus we are
253 * careful here to simply ignore bits in bitmap words that this
254 * implementation has yet to support explicitly.
255 *
256 * Return values:
257 * %nfs_ok: @bmval populated successfully
258 * %nfserr_bad_xdr: the encoded bitmap was invalid
259 */
260static __be32
261nfsd4_decode_bitmap4(struct nfsd4_compoundargs *argp, u32 *bmval, u32 bmlen)
262{
263 u32 i, count;
264 __be32 *p;
265
266 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
267 return nfserr_bad_xdr;
268 /* request sanity */
269 if (count > 1000)
270 return nfserr_bad_xdr;
271 p = xdr_inline_decode(argp->xdr, count << 2);
272 if (!p)
273 return nfserr_bad_xdr;
274 i = 0;
275 while (i < count)
276 bmval[i++] = be32_to_cpup(p++);
277 while (i < bmlen)
278 bmval[i++] = 0;
279
280 return nfs_ok;
281}
282
Al Virob37ad282006-10-19 23:28:59 -0700283static __be32
Chuck Leverc941a962020-11-19 13:02:54 -0500284nfsd4_decode_nfsace4(struct nfsd4_compoundargs *argp, struct nfs4_ace *ace)
285{
286 __be32 *p, status;
287 u32 length;
288
289 if (xdr_stream_decode_u32(argp->xdr, &ace->type) < 0)
290 return nfserr_bad_xdr;
291 if (xdr_stream_decode_u32(argp->xdr, &ace->flag) < 0)
292 return nfserr_bad_xdr;
293 if (xdr_stream_decode_u32(argp->xdr, &ace->access_mask) < 0)
294 return nfserr_bad_xdr;
295
296 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
297 return nfserr_bad_xdr;
298 p = xdr_inline_decode(argp->xdr, length);
299 if (!p)
300 return nfserr_bad_xdr;
301 ace->whotype = nfs4_acl_get_whotype((char *)p, length);
302 if (ace->whotype != NFS4_ACL_WHO_NAMED)
303 status = nfs_ok;
304 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
305 status = nfsd_map_name_to_gid(argp->rqstp,
306 (char *)p, length, &ace->who_gid);
307 else
308 status = nfsd_map_name_to_uid(argp->rqstp,
309 (char *)p, length, &ace->who_uid);
310
311 return status;
312}
313
314/* A counted array of nfsace4's */
315static noinline __be32
316nfsd4_decode_acl(struct nfsd4_compoundargs *argp, struct nfs4_acl **acl)
317{
318 struct nfs4_ace *ace;
319 __be32 status;
320 u32 count;
321
322 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
323 return nfserr_bad_xdr;
324
325 if (count > xdr_stream_remaining(argp->xdr) / 20)
326 /*
327 * Even with 4-byte names there wouldn't be
328 * space for that many aces; something fishy is
329 * going on:
330 */
331 return nfserr_fbig;
332
333 *acl = svcxdr_tmpalloc(argp, nfs4_acl_bytes(count));
334 if (*acl == NULL)
335 return nfserr_jukebox;
336
337 (*acl)->naces = count;
338 for (ace = (*acl)->aces; ace < (*acl)->aces + count; ace++) {
339 status = nfsd4_decode_nfsace4(argp, ace);
340 if (status)
341 return status;
342 }
343
344 return nfs_ok;
345}
346
Chuck Leverdabe9182020-11-19 14:05:51 -0500347static noinline __be32
348nfsd4_decode_security_label(struct nfsd4_compoundargs *argp,
349 struct xdr_netobj *label)
350{
351 u32 lfs, pi, length;
352 __be32 *p;
353
354 if (xdr_stream_decode_u32(argp->xdr, &lfs) < 0)
355 return nfserr_bad_xdr;
356 if (xdr_stream_decode_u32(argp->xdr, &pi) < 0)
357 return nfserr_bad_xdr;
358
359 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
360 return nfserr_bad_xdr;
361 if (length > NFS4_MAXLABELLEN)
362 return nfserr_badlabel;
363 p = xdr_inline_decode(argp->xdr, length);
364 if (!p)
365 return nfserr_bad_xdr;
366 label->len = length;
367 label->data = svcxdr_dupstr(argp, p, length);
368 if (!label->data)
369 return nfserr_jukebox;
370
371 return nfs_ok;
372}
373
Chuck Leverc941a962020-11-19 13:02:54 -0500374static __be32
Chuck Leverd1c263a2020-11-03 12:56:05 -0500375nfsd4_decode_fattr4(struct nfsd4_compoundargs *argp, u32 *bmval, u32 bmlen,
376 struct iattr *iattr, struct nfs4_acl **acl,
377 struct xdr_netobj *label, int *umask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Chuck Lever081d53f2020-11-19 13:09:13 -0500379 unsigned int starting_pos;
380 u32 attrlist4_count;
Chuck Leverd1c263a2020-11-03 12:56:05 -0500381 __be32 *p, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 iattr->ia_valid = 0;
Chuck Leverd1c263a2020-11-03 12:56:05 -0500384 status = nfsd4_decode_bitmap4(argp, bmval, bmlen);
385 if (status)
386 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
J. Bruce Fieldse864c182016-06-10 16:56:05 -0400388 if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
389 || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
390 || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2) {
391 if (nfsd_attrs_supported(argp->minorversion, bmval))
392 return nfserr_inval;
393 return nfserr_attrnotsupp;
394 }
395
Chuck Lever081d53f2020-11-19 13:09:13 -0500396 if (xdr_stream_decode_u32(argp->xdr, &attrlist4_count) < 0)
397 return nfserr_bad_xdr;
398 starting_pos = xdr_stream_pos(argp->xdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 if (bmval[0] & FATTR4_WORD0_SIZE) {
Chuck Lever2ac1b9b2020-11-19 13:47:16 -0500401 u64 size;
402
403 if (xdr_stream_decode_u64(argp->xdr, &size) < 0)
404 return nfserr_bad_xdr;
405 iattr->ia_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 iattr->ia_valid |= ATTR_SIZE;
407 }
408 if (bmval[0] & FATTR4_WORD0_ACL) {
Chuck Leverc941a962020-11-19 13:02:54 -0500409 status = nfsd4_decode_acl(argp, acl);
410 if (status)
411 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 } else
413 *acl = NULL;
414 if (bmval[1] & FATTR4_WORD1_MODE) {
Chuck Lever1c8f0ad2020-11-19 13:54:26 -0500415 u32 mode;
416
417 if (xdr_stream_decode_u32(argp->xdr, &mode) < 0)
418 return nfserr_bad_xdr;
419 iattr->ia_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 iattr->ia_mode &= (S_IFMT | S_IALLUGO);
421 iattr->ia_valid |= ATTR_MODE;
422 }
423 if (bmval[1] & FATTR4_WORD1_OWNER) {
Chuck Lever9853a5a2020-11-19 13:56:42 -0500424 u32 length;
425
426 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
427 return nfserr_bad_xdr;
428 p = xdr_inline_decode(argp->xdr, length);
429 if (!p)
430 return nfserr_bad_xdr;
431 status = nfsd_map_name_to_uid(argp->rqstp, (char *)p, length,
432 &iattr->ia_uid);
433 if (status)
NeilBrown47c85292011-02-16 13:08:35 +1100434 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 iattr->ia_valid |= ATTR_UID;
436 }
437 if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
Chuck Lever393c31d2020-11-19 13:58:18 -0500438 u32 length;
439
440 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
441 return nfserr_bad_xdr;
442 p = xdr_inline_decode(argp->xdr, length);
443 if (!p)
444 return nfserr_bad_xdr;
445 status = nfsd_map_name_to_gid(argp->rqstp, (char *)p, length,
446 &iattr->ia_gid);
447 if (status)
NeilBrown47c85292011-02-16 13:08:35 +1100448 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 iattr->ia_valid |= ATTR_GID;
450 }
451 if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
Chuck Lever1c3eff72020-11-19 14:01:08 -0500452 u32 set_it;
453
454 if (xdr_stream_decode_u32(argp->xdr, &set_it) < 0)
455 return nfserr_bad_xdr;
456 switch (set_it) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 case NFS4_SET_TO_CLIENT_TIME:
Chuck Lever1c3eff72020-11-19 14:01:08 -0500458 status = nfsd4_decode_nfstime4(argp, &iattr->ia_atime);
Christoph Hellwig4c94e132015-01-22 12:09:50 +0100459 if (status)
460 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
462 break;
463 case NFS4_SET_TO_SERVER_TIME:
464 iattr->ia_valid |= ATTR_ATIME;
465 break;
466 default:
Chuck Lever1c3eff72020-11-19 14:01:08 -0500467 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
Chuck Lever1c3eff72020-11-19 14:01:08 -0500471 u32 set_it;
472
473 if (xdr_stream_decode_u32(argp->xdr, &set_it) < 0)
474 return nfserr_bad_xdr;
475 switch (set_it) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 case NFS4_SET_TO_CLIENT_TIME:
Chuck Lever1c3eff72020-11-19 14:01:08 -0500477 status = nfsd4_decode_nfstime4(argp, &iattr->ia_mtime);
Christoph Hellwig4c94e132015-01-22 12:09:50 +0100478 if (status)
479 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
481 break;
482 case NFS4_SET_TO_SERVER_TIME:
483 iattr->ia_valid |= ATTR_MTIME;
484 break;
485 default:
Chuck Lever1c3eff72020-11-19 14:01:08 -0500486 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488 }
David Quigley18032ca2013-05-02 13:19:10 -0400489 label->len = 0;
Arnd Bergmann2285ae72018-01-22 22:09:12 +0100490 if (IS_ENABLED(CONFIG_NFSD_V4_SECURITY_LABEL) &&
491 bmval[2] & FATTR4_WORD2_SECURITY_LABEL) {
Chuck Leverdabe9182020-11-19 14:05:51 -0500492 status = nfsd4_decode_security_label(argp, label);
493 if (status)
494 return status;
David Quigley18032ca2013-05-02 13:19:10 -0400495 }
Andreas Gruenbacher47057ab2016-01-12 20:24:14 +0100496 if (bmval[2] & FATTR4_WORD2_MODE_UMASK) {
Chuck Lever66f04762020-11-19 14:07:43 -0500497 u32 mode, mask;
498
Andreas Gruenbacher47057ab2016-01-12 20:24:14 +0100499 if (!umask)
Chuck Lever66f04762020-11-19 14:07:43 -0500500 return nfserr_bad_xdr;
501 if (xdr_stream_decode_u32(argp->xdr, &mode) < 0)
502 return nfserr_bad_xdr;
503 iattr->ia_mode = mode & (S_IFMT | S_IALLUGO);
504 if (xdr_stream_decode_u32(argp->xdr, &mask) < 0)
505 return nfserr_bad_xdr;
506 *umask = mask & S_IRWXUGO;
Andreas Gruenbacher47057ab2016-01-12 20:24:14 +0100507 iattr->ia_valid |= ATTR_MODE;
508 }
Chuck Lever081d53f2020-11-19 13:09:13 -0500509
510 /* request sanity: did attrlist4 contain the expected number of words? */
511 if (attrlist4_count != xdr_stream_pos(argp->xdr) - starting_pos)
512 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Chuck Leverd1c263a2020-11-03 12:56:05 -0500514 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
Al Virob37ad282006-10-19 23:28:59 -0700517static __be32
Chuck Leverd3d2f382020-11-03 13:18:23 -0500518nfsd4_decode_stateid4(struct nfsd4_compoundargs *argp, stateid_t *sid)
519{
520 __be32 *p;
521
522 p = xdr_inline_decode(argp->xdr, NFS4_STATEID_SIZE);
523 if (!p)
524 return nfserr_bad_xdr;
525 sid->si_generation = be32_to_cpup(p++);
526 memcpy(&sid->si_opaque, p, sizeof(sid->si_opaque));
527 return nfs_ok;
528}
529
Chuck Lever144e8262020-11-16 17:25:02 -0500530static __be32
531nfsd4_decode_clientid4(struct nfsd4_compoundargs *argp, clientid_t *clientid)
532{
533 __be32 *p;
534
535 p = xdr_inline_decode(argp->xdr, sizeof(__be64));
536 if (!p)
537 return nfserr_bad_xdr;
538 memcpy(clientid, p, sizeof(*clientid));
539 return nfs_ok;
540}
541
542static __be32
543nfsd4_decode_state_owner4(struct nfsd4_compoundargs *argp,
544 clientid_t *clientid, struct xdr_netobj *owner)
545{
546 __be32 status;
547
548 status = nfsd4_decode_clientid4(argp, clientid);
549 if (status)
550 return status;
551 return nfsd4_decode_opaque(argp, owner);
552}
553
Chuck Lever04495972020-11-03 15:03:50 -0500554#ifdef CONFIG_NFSD_PNFS
555static __be32
556nfsd4_decode_deviceid4(struct nfsd4_compoundargs *argp,
557 struct nfsd4_deviceid *devid)
558{
559 __be32 *p;
560
561 p = xdr_inline_decode(argp->xdr, NFS4_DEVICEID4_SIZE);
562 if (!p)
563 return nfserr_bad_xdr;
564 memcpy(devid, p, sizeof(*devid));
565 return nfs_ok;
566}
Chuck Lever51859802020-11-04 10:40:07 -0500567
568static __be32
569nfsd4_decode_layoutupdate4(struct nfsd4_compoundargs *argp,
570 struct nfsd4_layoutcommit *lcp)
571{
572 if (xdr_stream_decode_u32(argp->xdr, &lcp->lc_layout_type) < 0)
573 return nfserr_bad_xdr;
574 if (lcp->lc_layout_type < LAYOUT_NFSV4_1_FILES)
575 return nfserr_bad_xdr;
576 if (lcp->lc_layout_type >= LAYOUT_TYPE_MAX)
577 return nfserr_bad_xdr;
578
579 if (xdr_stream_decode_u32(argp->xdr, &lcp->lc_up_len) < 0)
580 return nfserr_bad_xdr;
581 if (lcp->lc_up_len > 0) {
582 lcp->lc_up_layout = xdr_inline_decode(argp->xdr, lcp->lc_up_len);
583 if (!lcp->lc_up_layout)
584 return nfserr_bad_xdr;
585 }
586
587 return nfs_ok;
588}
589
Chuck Lever645fcad2020-11-04 10:42:25 -0500590static __be32
591nfsd4_decode_layoutreturn4(struct nfsd4_compoundargs *argp,
592 struct nfsd4_layoutreturn *lrp)
593{
594 __be32 status;
595
596 if (xdr_stream_decode_u32(argp->xdr, &lrp->lr_return_type) < 0)
597 return nfserr_bad_xdr;
598 switch (lrp->lr_return_type) {
599 case RETURN_FILE:
600 if (xdr_stream_decode_u64(argp->xdr, &lrp->lr_seg.offset) < 0)
601 return nfserr_bad_xdr;
602 if (xdr_stream_decode_u64(argp->xdr, &lrp->lr_seg.length) < 0)
603 return nfserr_bad_xdr;
604 status = nfsd4_decode_stateid4(argp, &lrp->lr_sid);
605 if (status)
606 return status;
607 if (xdr_stream_decode_u32(argp->xdr, &lrp->lrf_body_len) < 0)
608 return nfserr_bad_xdr;
609 if (lrp->lrf_body_len > 0) {
610 lrp->lrf_body = xdr_inline_decode(argp->xdr, lrp->lrf_body_len);
611 if (!lrp->lrf_body)
612 return nfserr_bad_xdr;
613 }
614 break;
615 case RETURN_FSID:
616 case RETURN_ALL:
617 lrp->lr_seg.offset = 0;
618 lrp->lr_seg.length = NFS4_MAX_UINT64;
619 break;
620 default:
621 return nfserr_bad_xdr;
622 }
623
624 return nfs_ok;
625}
626
Chuck Lever04495972020-11-03 15:03:50 -0500627#endif /* CONFIG_NFSD_PNFS */
628
Chuck Lever571e0452020-11-03 13:16:23 -0500629static __be32
630nfsd4_decode_sessionid4(struct nfsd4_compoundargs *argp,
631 struct nfs4_sessionid *sessionid)
632{
633 __be32 *p;
634
635 p = xdr_inline_decode(argp->xdr, NFS4_MAX_SESSIONID_LEN);
636 if (!p)
637 return nfserr_bad_xdr;
638 memcpy(sessionid->data, p, sizeof(sessionid->data));
639 return nfs_ok;
640}
641
Chuck Lever1a994402020-11-03 13:09:34 -0500642/* Defined in Appendix A of RFC 5531 */
643static __be32
644nfsd4_decode_authsys_parms(struct nfsd4_compoundargs *argp,
645 struct nfsd4_cb_sec *cbs)
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400646{
Chuck Lever1a994402020-11-03 13:09:34 -0500647 u32 stamp, gidcount, uid, gid;
648 __be32 *p, status;
649
650 if (xdr_stream_decode_u32(argp->xdr, &stamp) < 0)
651 return nfserr_bad_xdr;
652 /* machine name */
653 status = nfsd4_decode_ignored_string(argp, 255);
654 if (status)
655 return status;
656 if (xdr_stream_decode_u32(argp->xdr, &uid) < 0)
657 return nfserr_bad_xdr;
658 if (xdr_stream_decode_u32(argp->xdr, &gid) < 0)
659 return nfserr_bad_xdr;
660 if (xdr_stream_decode_u32(argp->xdr, &gidcount) < 0)
661 return nfserr_bad_xdr;
662 if (gidcount > 16)
663 return nfserr_bad_xdr;
664 p = xdr_inline_decode(argp->xdr, gidcount << 2);
665 if (!p)
666 return nfserr_bad_xdr;
667 if (cbs->flavor == (u32)(-1)) {
668 struct user_namespace *userns = nfsd_user_namespace(argp->rqstp);
669
670 kuid_t kuid = make_kuid(userns, uid);
671 kgid_t kgid = make_kgid(userns, gid);
672 if (uid_valid(kuid) && gid_valid(kgid)) {
673 cbs->uid = kuid;
674 cbs->gid = kgid;
675 cbs->flavor = RPC_AUTH_UNIX;
676 } else {
677 dprintk("RPC_AUTH_UNIX with invalid uid or gid, ignoring!\n");
678 }
679 }
680
681 return nfs_ok;
682}
683
684static __be32
685nfsd4_decode_gss_cb_handles4(struct nfsd4_compoundargs *argp,
686 struct nfsd4_cb_sec *cbs)
687{
688 __be32 status;
689 u32 service;
690
691 dprintk("RPC_AUTH_GSS callback secflavor not supported!\n");
692
693 if (xdr_stream_decode_u32(argp->xdr, &service) < 0)
694 return nfserr_bad_xdr;
695 if (service < RPC_GSS_SVC_NONE || service > RPC_GSS_SVC_PRIVACY)
696 return nfserr_bad_xdr;
697 /* gcbp_handle_from_server */
698 status = nfsd4_decode_ignored_string(argp, 0);
699 if (status)
700 return status;
701 /* gcbp_handle_from_client */
702 status = nfsd4_decode_ignored_string(argp, 0);
703 if (status)
704 return status;
705
706 return nfs_ok;
707}
708
709/* a counted array of callback_sec_parms4 items */
710static __be32
711nfsd4_decode_cb_sec(struct nfsd4_compoundargs *argp, struct nfsd4_cb_sec *cbs)
712{
713 u32 i, secflavor, nr_secflavs;
714 __be32 status;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400715
716 /* callback_sec_params4 */
Chuck Lever1a994402020-11-03 13:09:34 -0500717 if (xdr_stream_decode_u32(argp->xdr, &nr_secflavs) < 0)
718 return nfserr_bad_xdr;
J. Bruce Fields57569a72013-05-17 16:25:32 -0400719 if (nr_secflavs)
720 cbs->flavor = (u32)(-1);
721 else
722 /* Is this legal? Be generous, take it to mean AUTH_NONE: */
723 cbs->flavor = 0;
Chuck Lever1a994402020-11-03 13:09:34 -0500724
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400725 for (i = 0; i < nr_secflavs; ++i) {
Chuck Lever1a994402020-11-03 13:09:34 -0500726 if (xdr_stream_decode_u32(argp->xdr, &secflavor) < 0)
727 return nfserr_bad_xdr;
728 switch (secflavor) {
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400729 case RPC_AUTH_NULL:
Chuck Lever1a994402020-11-03 13:09:34 -0500730 /* void */
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500731 if (cbs->flavor == (u32)(-1))
732 cbs->flavor = RPC_AUTH_NULL;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400733 break;
734 case RPC_AUTH_UNIX:
Chuck Lever1a994402020-11-03 13:09:34 -0500735 status = nfsd4_decode_authsys_parms(argp, cbs);
736 if (status)
737 return status;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400738 break;
739 case RPC_AUTH_GSS:
Chuck Lever1a994402020-11-03 13:09:34 -0500740 status = nfsd4_decode_gss_cb_handles4(argp, cbs);
741 if (status)
742 return status;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400743 break;
744 default:
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400745 return nfserr_inval;
746 }
747 }
Chuck Lever1a994402020-11-03 13:09:34 -0500748
749 return nfs_ok;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400750}
751
Chuck Lever1a994402020-11-03 13:09:34 -0500752
Chuck Leverd169a6a2020-11-03 13:12:27 -0500753/*
754 * NFSv4 operation argument decoders
755 */
756
757static __be32
758nfsd4_decode_access(struct nfsd4_compoundargs *argp,
759 struct nfsd4_access *access)
760{
761 if (xdr_stream_decode_u32(argp->xdr, &access->ac_req_access) < 0)
762 return nfserr_bad_xdr;
763 return nfs_ok;
764}
765
Al Virob37ad282006-10-19 23:28:59 -0700766static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
768{
Chuck Leverd3d2f382020-11-03 13:18:23 -0500769 if (xdr_stream_decode_u32(argp->xdr, &close->cl_seqid) < 0)
770 return nfserr_bad_xdr;
771 return nfsd4_decode_stateid4(argp, &close->cl_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
774
Al Virob37ad282006-10-19 23:28:59 -0700775static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
777{
Chuck Levercbd9abb2020-11-03 13:19:51 -0500778 if (xdr_stream_decode_u64(argp->xdr, &commit->co_offset) < 0)
779 return nfserr_bad_xdr;
780 if (xdr_stream_decode_u32(argp->xdr, &commit->co_count) < 0)
781 return nfserr_bad_xdr;
782 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
Al Virob37ad282006-10-19 23:28:59 -0700785static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
787{
Chuck Lever000dfa12020-11-03 13:24:10 -0500788 __be32 *p, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Chuck Lever000dfa12020-11-03 13:24:10 -0500790 if (xdr_stream_decode_u32(argp->xdr, &create->cr_type) < 0)
791 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 switch (create->cr_type) {
793 case NF4LNK:
Chuck Lever000dfa12020-11-03 13:24:10 -0500794 if (xdr_stream_decode_u32(argp->xdr, &create->cr_datalen) < 0)
795 return nfserr_bad_xdr;
796 p = xdr_inline_decode(argp->xdr, create->cr_datalen);
797 if (!p)
798 return nfserr_bad_xdr;
J. Bruce Fields29c353b2014-06-24 17:06:51 -0400799 create->cr_data = svcxdr_dupstr(argp, p, create->cr_datalen);
J. Bruce Fields7fb84302014-06-24 15:06:41 -0400800 if (!create->cr_data)
J. Bruce Fields76f471282014-06-19 16:44:48 -0400801 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 break;
803 case NF4BLK:
804 case NF4CHR:
Chuck Lever000dfa12020-11-03 13:24:10 -0500805 if (xdr_stream_decode_u32(argp->xdr, &create->cr_specdata1) < 0)
806 return nfserr_bad_xdr;
807 if (xdr_stream_decode_u32(argp->xdr, &create->cr_specdata2) < 0)
808 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 break;
810 case NF4SOCK:
811 case NF4FIFO:
812 case NF4DIR:
813 default:
814 break;
815 }
Chuck Lever000dfa12020-11-03 13:24:10 -0500816 status = nfsd4_decode_component4(argp, &create->cr_name,
817 &create->cr_namelen);
818 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return status;
Chuck Leverd1c263a2020-11-03 12:56:05 -0500820 status = nfsd4_decode_fattr4(argp, create->cr_bmval,
821 ARRAY_SIZE(create->cr_bmval),
822 &create->cr_iattr, &create->cr_acl,
823 &create->cr_label, &create->cr_umask);
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300824 if (status)
Chuck Lever000dfa12020-11-03 13:24:10 -0500825 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Chuck Lever000dfa12020-11-03 13:24:10 -0500827 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
Al Virob37ad282006-10-19 23:28:59 -0700830static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
832{
Chuck Lever95e64822020-11-21 14:11:58 -0500833 return nfsd4_decode_stateid4(argp, &dr->dr_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
Al Virob37ad282006-10-19 23:28:59 -0700836static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
838{
Chuck Leverf759eff2020-11-19 14:40:20 -0500839 return nfsd4_decode_bitmap4(argp, getattr->ga_bmval,
840 ARRAY_SIZE(getattr->ga_bmval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
Al Virob37ad282006-10-19 23:28:59 -0700843static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
845{
Chuck Lever5c505d12020-11-04 15:01:24 -0500846 return nfsd4_decode_component4(argp, &link->li_name, &link->li_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
Al Virob37ad282006-10-19 23:28:59 -0700849static __be32
Chuck Lever8918cc02020-11-16 17:16:52 -0500850nfsd4_decode_open_to_lock_owner4(struct nfsd4_compoundargs *argp,
851 struct nfsd4_lock *lock)
852{
853 __be32 status;
854
855 if (xdr_stream_decode_u32(argp->xdr, &lock->lk_new_open_seqid) < 0)
856 return nfserr_bad_xdr;
857 status = nfsd4_decode_stateid4(argp, &lock->lk_new_open_stateid);
858 if (status)
859 return status;
860 if (xdr_stream_decode_u32(argp->xdr, &lock->lk_new_lock_seqid) < 0)
861 return nfserr_bad_xdr;
862 return nfsd4_decode_state_owner4(argp, &lock->lk_new_clientid,
863 &lock->lk_new_owner);
864}
865
866static __be32
867nfsd4_decode_exist_lock_owner4(struct nfsd4_compoundargs *argp,
868 struct nfsd4_lock *lock)
869{
870 __be32 status;
871
872 status = nfsd4_decode_stateid4(argp, &lock->lk_old_lock_stateid);
873 if (status)
874 return status;
875 if (xdr_stream_decode_u32(argp->xdr, &lock->lk_old_lock_seqid) < 0)
876 return nfserr_bad_xdr;
877
878 return nfs_ok;
879}
880
881static __be32
882nfsd4_decode_locker4(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
883{
884 if (xdr_stream_decode_bool(argp->xdr, &lock->lk_is_new) < 0)
885 return nfserr_bad_xdr;
886 if (lock->lk_is_new)
887 return nfsd4_decode_open_to_lock_owner4(argp, lock);
888 return nfsd4_decode_exist_lock_owner4(argp, lock);
889}
890
891static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
893{
Chuck Lever7c59dee2020-11-03 13:29:27 -0500894 if (xdr_stream_decode_u32(argp->xdr, &lock->lk_type) < 0)
895 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
Chuck Lever7c59dee2020-11-03 13:29:27 -0500897 return nfserr_bad_xdr;
898 if (xdr_stream_decode_bool(argp->xdr, &lock->lk_reclaim) < 0)
899 return nfserr_bad_xdr;
900 if (xdr_stream_decode_u64(argp->xdr, &lock->lk_offset) < 0)
901 return nfserr_bad_xdr;
902 if (xdr_stream_decode_u64(argp->xdr, &lock->lk_length) < 0)
903 return nfserr_bad_xdr;
904 return nfsd4_decode_locker4(argp, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906
Al Virob37ad282006-10-19 23:28:59 -0700907static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
909{
Chuck Lever0a146f02020-11-03 13:31:44 -0500910 if (xdr_stream_decode_u32(argp->xdr, &lockt->lt_type) < 0)
911 return nfserr_bad_xdr;
912 if ((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
913 return nfserr_bad_xdr;
914 if (xdr_stream_decode_u64(argp->xdr, &lockt->lt_offset) < 0)
915 return nfserr_bad_xdr;
916 if (xdr_stream_decode_u64(argp->xdr, &lockt->lt_length) < 0)
917 return nfserr_bad_xdr;
918 return nfsd4_decode_state_owner4(argp, &lockt->lt_clientid,
919 &lockt->lt_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
Al Virob37ad282006-10-19 23:28:59 -0700922static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
924{
Chuck Leverca9cf9f2020-11-03 13:33:28 -0500925 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Chuck Leverca9cf9f2020-11-03 13:33:28 -0500927 if (xdr_stream_decode_u32(argp->xdr, &locku->lu_type) < 0)
928 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
Chuck Leverca9cf9f2020-11-03 13:33:28 -0500930 return nfserr_bad_xdr;
931 if (xdr_stream_decode_u32(argp->xdr, &locku->lu_seqid) < 0)
932 return nfserr_bad_xdr;
933 status = nfsd4_decode_stateid4(argp, &locku->lu_stateid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300934 if (status)
935 return status;
Chuck Leverca9cf9f2020-11-03 13:33:28 -0500936 if (xdr_stream_decode_u64(argp->xdr, &locku->lu_offset) < 0)
937 return nfserr_bad_xdr;
938 if (xdr_stream_decode_u64(argp->xdr, &locku->lu_length) < 0)
939 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Chuck Leverca9cf9f2020-11-03 13:33:28 -0500941 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
Al Virob37ad282006-10-19 23:28:59 -0700944static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
946{
Chuck Lever3d5877e2020-11-04 15:02:40 -0500947 return nfsd4_decode_component4(argp, &lookup->lo_name, &lookup->lo_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
Chuck Leverbf33bab2020-11-16 17:37:42 -0500950static __be32
951nfsd4_decode_createhow4(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
952{
953 __be32 status;
954
955 if (xdr_stream_decode_u32(argp->xdr, &open->op_createmode) < 0)
956 return nfserr_bad_xdr;
957 switch (open->op_createmode) {
958 case NFS4_CREATE_UNCHECKED:
959 case NFS4_CREATE_GUARDED:
960 status = nfsd4_decode_fattr4(argp, open->op_bmval,
961 ARRAY_SIZE(open->op_bmval),
962 &open->op_iattr, &open->op_acl,
963 &open->op_label, &open->op_umask);
964 if (status)
965 return status;
966 break;
967 case NFS4_CREATE_EXCLUSIVE:
968 status = nfsd4_decode_verifier4(argp, &open->op_verf);
969 if (status)
970 return status;
971 break;
972 case NFS4_CREATE_EXCLUSIVE4_1:
973 if (argp->minorversion < 1)
974 return nfserr_bad_xdr;
975 status = nfsd4_decode_verifier4(argp, &open->op_verf);
976 if (status)
977 return status;
978 status = nfsd4_decode_fattr4(argp, open->op_bmval,
979 ARRAY_SIZE(open->op_bmval),
980 &open->op_iattr, &open->op_acl,
981 &open->op_label, &open->op_umask);
982 if (status)
983 return status;
984 break;
985 default:
986 return nfserr_bad_xdr;
987 }
988
989 return nfs_ok;
990}
991
Chuck Levere6ec04b2020-11-16 17:41:21 -0500992static __be32
993nfsd4_decode_openflag4(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
994{
995 __be32 status;
996
997 if (xdr_stream_decode_u32(argp->xdr, &open->op_create) < 0)
998 return nfserr_bad_xdr;
999 switch (open->op_create) {
1000 case NFS4_OPEN_NOCREATE:
1001 break;
1002 case NFS4_OPEN_CREATE:
1003 status = nfsd4_decode_createhow4(argp, open);
1004 if (status)
1005 return status;
1006 break;
1007 default:
1008 return nfserr_bad_xdr;
1009 }
1010
1011 return nfs_ok;
1012}
1013
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02001014static __be32 nfsd4_decode_share_access(struct nfsd4_compoundargs *argp, u32 *share_access, u32 *deleg_want, u32 *deleg_when)
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001015{
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001016 u32 w;
1017
Chuck Lever9aa62f52020-11-16 17:54:48 -05001018 if (xdr_stream_decode_u32(argp->xdr, &w) < 0)
1019 return nfserr_bad_xdr;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02001020 *share_access = w & NFS4_SHARE_ACCESS_MASK;
1021 *deleg_want = w & NFS4_SHARE_WANT_MASK;
1022 if (deleg_when)
1023 *deleg_when = w & NFS4_SHARE_WHEN_MASK;
1024
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001025 switch (w & NFS4_SHARE_ACCESS_MASK) {
1026 case NFS4_SHARE_ACCESS_READ:
1027 case NFS4_SHARE_ACCESS_WRITE:
1028 case NFS4_SHARE_ACCESS_BOTH:
1029 break;
1030 default:
1031 return nfserr_bad_xdr;
1032 }
Benny Halevyfc0d14f2011-10-27 20:43:01 +02001033 w &= ~NFS4_SHARE_ACCESS_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001034 if (!w)
1035 return nfs_ok;
1036 if (!argp->minorversion)
1037 return nfserr_bad_xdr;
1038 switch (w & NFS4_SHARE_WANT_MASK) {
1039 case NFS4_SHARE_WANT_NO_PREFERENCE:
1040 case NFS4_SHARE_WANT_READ_DELEG:
1041 case NFS4_SHARE_WANT_WRITE_DELEG:
1042 case NFS4_SHARE_WANT_ANY_DELEG:
1043 case NFS4_SHARE_WANT_NO_DELEG:
1044 case NFS4_SHARE_WANT_CANCEL:
1045 break;
1046 default:
1047 return nfserr_bad_xdr;
1048 }
Benny Halevy92bac8c2011-10-19 19:13:29 -07001049 w &= ~NFS4_SHARE_WANT_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001050 if (!w)
1051 return nfs_ok;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02001052
1053 if (!deleg_when) /* open_downgrade */
1054 return nfserr_inval;
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001055 switch (w) {
1056 case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
1057 case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
Benny Halevyc668fc62011-10-19 19:13:13 -07001058 case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
1059 NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001060 return nfs_ok;
1061 }
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001062 return nfserr_bad_xdr;
1063}
1064
1065static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
1066{
Chuck Leverb07bebd2020-11-16 17:56:17 -05001067 if (xdr_stream_decode_u32(argp->xdr, x) < 0)
1068 return nfserr_bad_xdr;
1069 /* Note: unlike access bits, deny bits may be zero. */
Dan Carpenter01cd4af2011-10-17 10:41:17 +03001070 if (*x & ~NFS4_SHARE_DENY_BOTH)
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001071 return nfserr_bad_xdr;
Chuck Leverb07bebd2020-11-16 17:56:17 -05001072
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001073 return nfs_ok;
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001074}
1075
Al Virob37ad282006-10-19 23:28:59 -07001076static __be32
Chuck Lever1708e502020-11-16 17:45:04 -05001077nfsd4_decode_open_claim4(struct nfsd4_compoundargs *argp,
1078 struct nfsd4_open *open)
1079{
1080 __be32 status;
1081
1082 if (xdr_stream_decode_u32(argp->xdr, &open->op_claim_type) < 0)
1083 return nfserr_bad_xdr;
1084 switch (open->op_claim_type) {
1085 case NFS4_OPEN_CLAIM_NULL:
1086 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1087 status = nfsd4_decode_component4(argp, &open->op_fname,
1088 &open->op_fnamelen);
1089 if (status)
1090 return status;
1091 break;
1092 case NFS4_OPEN_CLAIM_PREVIOUS:
1093 if (xdr_stream_decode_u32(argp->xdr, &open->op_delegate_type) < 0)
1094 return nfserr_bad_xdr;
1095 break;
1096 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1097 status = nfsd4_decode_stateid4(argp, &open->op_delegate_stateid);
1098 if (status)
1099 return status;
1100 status = nfsd4_decode_component4(argp, &open->op_fname,
1101 &open->op_fnamelen);
1102 if (status)
1103 return status;
1104 break;
1105 case NFS4_OPEN_CLAIM_FH:
1106 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1107 if (argp->minorversion < 1)
1108 return nfserr_bad_xdr;
1109 /* void */
1110 break;
1111 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1112 if (argp->minorversion < 1)
1113 return nfserr_bad_xdr;
1114 status = nfsd4_decode_stateid4(argp, &open->op_delegate_stateid);
1115 if (status)
1116 return status;
1117 break;
1118 default:
1119 return nfserr_bad_xdr;
1120 }
1121
1122 return nfs_ok;
1123}
1124
1125static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
1127{
Chuck Lever61e5e0b2020-11-01 12:04:06 -05001128 __be32 status;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02001129 u32 dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 memset(open->op_bmval, 0, sizeof(open->op_bmval));
1132 open->op_iattr.ia_valid = 0;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001133 open->op_openowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
J. Bruce Fields9d313b12013-02-28 12:51:49 -08001135 open->op_xdr_error = 0;
Chuck Lever61e5e0b2020-11-01 12:04:06 -05001136 if (xdr_stream_decode_u32(argp->xdr, &open->op_seqid) < 0)
1137 return nfserr_bad_xdr;
1138 /* deleg_want is ignored */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02001139 status = nfsd4_decode_share_access(argp, &open->op_share_access,
1140 &open->op_deleg_want, &dummy);
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001141 if (status)
Chuck Lever61e5e0b2020-11-01 12:04:06 -05001142 return status;
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001143 status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
1144 if (status)
Chuck Lever61e5e0b2020-11-01 12:04:06 -05001145 return status;
1146 status = nfsd4_decode_state_owner4(argp, &open->op_clientid,
1147 &open->op_owner);
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001148 if (status)
Chuck Lever61e5e0b2020-11-01 12:04:06 -05001149 return status;
Chuck Levere6ec04b2020-11-16 17:41:21 -05001150 status = nfsd4_decode_openflag4(argp, open);
1151 if (status)
1152 return status;
Chuck Lever61e5e0b2020-11-01 12:04:06 -05001153 return nfsd4_decode_open_claim4(argp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
Al Virob37ad282006-10-19 23:28:59 -07001156static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
1158{
Chuck Lever06bee692020-11-03 14:18:57 -05001159 __be32 status;
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001160
1161 if (argp->minorversion >= 1)
1162 return nfserr_notsupp;
1163
Chuck Lever06bee692020-11-03 14:18:57 -05001164 status = nfsd4_decode_stateid4(argp, &open_conf->oc_req_stateid);
Benny Halevye31a1b62008-08-12 20:46:18 +03001165 if (status)
1166 return status;
Chuck Lever06bee692020-11-03 14:18:57 -05001167 if (xdr_stream_decode_u32(argp->xdr, &open_conf->oc_seqid) < 0)
1168 return nfserr_bad_xdr;
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001169
Chuck Lever06bee692020-11-03 14:18:57 -05001170 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
1172
Al Virob37ad282006-10-19 23:28:59 -07001173static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
1175{
Chuck Leverdca716512020-11-03 14:21:01 -05001176 __be32 status;
1177
1178 status = nfsd4_decode_stateid4(argp, &open_down->od_stateid);
Benny Halevye31a1b62008-08-12 20:46:18 +03001179 if (status)
1180 return status;
Chuck Leverdca716512020-11-03 14:21:01 -05001181 if (xdr_stream_decode_u32(argp->xdr, &open_down->od_seqid) < 0)
1182 return nfserr_bad_xdr;
1183 /* deleg_want is ignored */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02001184 status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
1185 &open_down->od_deleg_want, NULL);
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001186 if (status)
1187 return status;
Chuck Leverdca716512020-11-03 14:21:01 -05001188 return nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
Al Virob37ad282006-10-19 23:28:59 -07001191static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
1193{
Chuck Levera73bed92020-11-03 14:23:02 -05001194 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Chuck Levera73bed92020-11-03 14:23:02 -05001196 if (xdr_stream_decode_u32(argp->xdr, &putfh->pf_fhlen) < 0)
1197 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 if (putfh->pf_fhlen > NFS4_FHSIZE)
Chuck Levera73bed92020-11-03 14:23:02 -05001199 return nfserr_bad_xdr;
1200 p = xdr_inline_decode(argp->xdr, putfh->pf_fhlen);
1201 if (!p)
1202 return nfserr_bad_xdr;
1203 putfh->pf_fhval = svcxdr_tmpalloc(argp, putfh->pf_fhlen);
1204 if (!putfh->pf_fhval)
1205 return nfserr_jukebox;
1206 memcpy(putfh->pf_fhval, p, putfh->pf_fhlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Chuck Levera73bed92020-11-03 14:23:02 -05001208 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
Al Virob37ad282006-10-19 23:28:59 -07001211static __be32
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001212nfsd4_decode_putpubfh(struct nfsd4_compoundargs *argp, void *p)
1213{
1214 if (argp->minorversion == 0)
1215 return nfs_ok;
1216 return nfserr_notsupp;
1217}
1218
1219static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
1221{
Chuck Lever3909c3b2020-11-03 14:28:24 -05001222 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Chuck Lever3909c3b2020-11-03 14:28:24 -05001224 status = nfsd4_decode_stateid4(argp, &read->rd_stateid);
Benny Halevye31a1b62008-08-12 20:46:18 +03001225 if (status)
1226 return status;
Chuck Lever3909c3b2020-11-03 14:28:24 -05001227 if (xdr_stream_decode_u64(argp->xdr, &read->rd_offset) < 0)
1228 return nfserr_bad_xdr;
1229 if (xdr_stream_decode_u32(argp->xdr, &read->rd_length) < 0)
1230 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Chuck Lever3909c3b2020-11-03 14:28:24 -05001232 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
Al Virob37ad282006-10-19 23:28:59 -07001235static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
1237{
Chuck Lever0dfaf2a2020-11-03 14:30:59 -05001238 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Chuck Lever0dfaf2a2020-11-03 14:30:59 -05001240 if (xdr_stream_decode_u64(argp->xdr, &readdir->rd_cookie) < 0)
1241 return nfserr_bad_xdr;
1242 status = nfsd4_decode_verifier4(argp, &readdir->rd_verf);
1243 if (status)
1244 return status;
1245 if (xdr_stream_decode_u32(argp->xdr, &readdir->rd_dircount) < 0)
1246 return nfserr_bad_xdr;
1247 if (xdr_stream_decode_u32(argp->xdr, &readdir->rd_maxcount) < 0)
1248 return nfserr_bad_xdr;
1249 if (xdr_stream_decode_uint32_array(argp->xdr, readdir->rd_bmval,
1250 ARRAY_SIZE(readdir->rd_bmval)) < 0)
1251 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Chuck Lever0dfaf2a2020-11-03 14:30:59 -05001253 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254}
1255
Al Virob37ad282006-10-19 23:28:59 -07001256static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
1258{
Chuck Leverb7f5fbf2020-11-04 15:04:36 -05001259 return nfsd4_decode_component4(argp, &remove->rm_name, &remove->rm_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260}
1261
Al Virob37ad282006-10-19 23:28:59 -07001262static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
1264{
Chuck Leverba881a02020-11-04 15:05:58 -05001265 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Chuck Leverba881a02020-11-04 15:05:58 -05001267 status = nfsd4_decode_component4(argp, &rename->rn_sname, &rename->rn_snamelen);
1268 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return status;
Chuck Leverba881a02020-11-04 15:05:58 -05001270 return nfsd4_decode_component4(argp, &rename->rn_tname, &rename->rn_tnamelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
Al Virob37ad282006-10-19 23:28:59 -07001273static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
1275{
Chuck Leverd12f9042020-11-04 15:08:50 -05001276 return nfsd4_decode_clientid4(argp, clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
Al Virob37ad282006-10-19 23:28:59 -07001279static __be32
Andy Adamsondcb488a32007-07-17 04:04:51 -07001280nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
1281 struct nfsd4_secinfo *secinfo)
1282{
Chuck Leverd0abdae2020-11-04 15:09:42 -05001283 return nfsd4_decode_component4(argp, &secinfo->si_name, &secinfo->si_namelen);
Andy Adamsondcb488a32007-07-17 04:04:51 -07001284}
1285
1286static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
1288{
Benny Halevye31a1b62008-08-12 20:46:18 +03001289 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Chuck Lever44592fe2020-11-21 14:14:59 -05001291 status = nfsd4_decode_stateid4(argp, &setattr->sa_stateid);
Benny Halevye31a1b62008-08-12 20:46:18 +03001292 if (status)
1293 return status;
Chuck Leverd1c263a2020-11-03 12:56:05 -05001294 return nfsd4_decode_fattr4(argp, setattr->sa_bmval,
1295 ARRAY_SIZE(setattr->sa_bmval),
1296 &setattr->sa_iattr, &setattr->sa_acl,
1297 &setattr->sa_label, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
1299
Al Virob37ad282006-10-19 23:28:59 -07001300static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
1302{
Chuck Lever92fa6c02020-11-03 14:35:02 -05001303 __be32 *p, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001305 if (argp->minorversion >= 1)
1306 return nfserr_notsupp;
1307
Chuck Lever92fa6c02020-11-03 14:35:02 -05001308 status = nfsd4_decode_verifier4(argp, &setclientid->se_verf);
1309 if (status)
1310 return status;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001311 status = nfsd4_decode_opaque(argp, &setclientid->se_name);
1312 if (status)
Chuck Lever92fa6c02020-11-03 14:35:02 -05001313 return status;
1314 if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_prog) < 0)
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001315 return nfserr_bad_xdr;
Chuck Lever92fa6c02020-11-03 14:35:02 -05001316 if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_netid_len) < 0)
1317 return nfserr_bad_xdr;
1318 p = xdr_inline_decode(argp->xdr, setclientid->se_callback_netid_len);
1319 if (!p)
1320 return nfserr_bad_xdr;
1321 setclientid->se_callback_netid_val = svcxdr_tmpalloc(argp,
1322 setclientid->se_callback_netid_len);
1323 if (!setclientid->se_callback_netid_val)
1324 return nfserr_jukebox;
1325 memcpy(setclientid->se_callback_netid_val, p,
1326 setclientid->se_callback_netid_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Chuck Lever92fa6c02020-11-03 14:35:02 -05001328 if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_addr_len) < 0)
1329 return nfserr_bad_xdr;
1330 p = xdr_inline_decode(argp->xdr, setclientid->se_callback_addr_len);
1331 if (!p)
1332 return nfserr_bad_xdr;
1333 setclientid->se_callback_addr_val = svcxdr_tmpalloc(argp,
1334 setclientid->se_callback_addr_len);
1335 if (!setclientid->se_callback_addr_val)
1336 return nfserr_jukebox;
1337 memcpy(setclientid->se_callback_addr_val, p,
1338 setclientid->se_callback_addr_len);
1339 if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_ident) < 0)
1340 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Chuck Lever92fa6c02020-11-03 14:35:02 -05001342 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
Al Virob37ad282006-10-19 23:28:59 -07001345static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
1347{
Chuck Leverd1ca5512020-11-04 15:12:33 -05001348 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001350 if (argp->minorversion >= 1)
1351 return nfserr_notsupp;
1352
Chuck Leverd1ca5512020-11-04 15:12:33 -05001353 status = nfsd4_decode_clientid4(argp, &scd_c->sc_clientid);
1354 if (status)
1355 return status;
1356 return nfsd4_decode_verifier4(argp, &scd_c->sc_confirm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358
1359/* Also used for NVERIFY */
Al Virob37ad282006-10-19 23:28:59 -07001360static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1362{
Chuck Lever67cd4532020-11-03 14:40:32 -05001363 __be32 *p, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Chuck Lever67cd4532020-11-03 14:40:32 -05001365 status = nfsd4_decode_bitmap4(argp, verify->ve_bmval,
1366 ARRAY_SIZE(verify->ve_bmval));
1367 if (status)
1368 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 /* For convenience's sake, we compare raw xdr'd attributes in
J. Bruce Fieldse5f95702012-11-30 17:24:18 -05001371 * nfsd4_proc_verify */
1372
Chuck Lever67cd4532020-11-03 14:40:32 -05001373 if (xdr_stream_decode_u32(argp->xdr, &verify->ve_attrlen) < 0)
1374 return nfserr_bad_xdr;
1375 p = xdr_inline_decode(argp->xdr, verify->ve_attrlen);
1376 if (!p)
1377 return nfserr_bad_xdr;
1378 verify->ve_attrval = svcxdr_tmpalloc(argp, verify->ve_attrlen);
1379 if (!verify->ve_attrval)
1380 return nfserr_jukebox;
1381 memcpy(verify->ve_attrval, p, verify->ve_attrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Chuck Lever67cd4532020-11-03 14:40:32 -05001383 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
Al Virob37ad282006-10-19 23:28:59 -07001386static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1388{
Chuck Lever244e2be2020-11-03 14:44:28 -05001389 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Chuck Lever244e2be2020-11-03 14:44:28 -05001391 status = nfsd4_decode_stateid4(argp, &write->wr_stateid);
Benny Halevye31a1b62008-08-12 20:46:18 +03001392 if (status)
1393 return status;
Chuck Lever244e2be2020-11-03 14:44:28 -05001394 if (xdr_stream_decode_u64(argp->xdr, &write->wr_offset) < 0)
1395 return nfserr_bad_xdr;
1396 if (xdr_stream_decode_u32(argp->xdr, &write->wr_stable_how) < 0)
1397 return nfserr_bad_xdr;
Kinglong Mee54bbb7d2016-12-31 20:59:53 +08001398 if (write->wr_stable_how > NFS_FILE_SYNC)
Chuck Lever244e2be2020-11-03 14:44:28 -05001399 return nfserr_bad_xdr;
1400 if (xdr_stream_decode_u32(argp->xdr, &write->wr_buflen) < 0)
1401 return nfserr_bad_xdr;
Chuck Leverc1346a122020-11-03 11:54:23 -05001402 if (!xdr_stream_subsegment(argp->xdr, &write->wr_payload, write->wr_buflen))
Chuck Lever244e2be2020-11-03 14:44:28 -05001403 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Chuck Lever244e2be2020-11-03 14:44:28 -05001405 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406}
1407
Al Virob37ad282006-10-19 23:28:59 -07001408static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1410{
Chuck Levera4a80c12020-11-04 13:42:25 -05001411 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001413 if (argp->minorversion >= 1)
1414 return nfserr_notsupp;
1415
Chuck Levera4a80c12020-11-04 13:42:25 -05001416 status = nfsd4_decode_state_owner4(argp, &rlockowner->rl_clientid,
1417 &rlockowner->rl_owner);
1418 if (status)
1419 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Andy Adamson60adfc52009-04-03 08:28:50 +03001421 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1422 return nfserr_inval;
Chuck Levera4a80c12020-11-04 13:42:25 -05001423
1424 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425}
1426
Chuck Lever0f81d962020-11-03 13:14:35 -05001427static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp, struct nfsd4_backchannel_ctl *bc)
1428{
1429 if (xdr_stream_decode_u32(argp->xdr, &bc->bc_cb_program) < 0)
1430 return nfserr_bad_xdr;
1431 return nfsd4_decode_cb_sec(argp, &bc->bc_cb_sec);
1432}
1433
Chuck Lever571e0452020-11-03 13:16:23 -05001434static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
1435{
1436 u32 use_conn_in_rdma_mode;
1437 __be32 status;
1438
1439 status = nfsd4_decode_sessionid4(argp, &bcts->sessionid);
1440 if (status)
1441 return status;
1442 if (xdr_stream_decode_u32(argp->xdr, &bcts->dir) < 0)
1443 return nfserr_bad_xdr;
1444 if (xdr_stream_decode_u32(argp->xdr, &use_conn_in_rdma_mode) < 0)
1445 return nfserr_bad_xdr;
1446
1447 return nfs_ok;
1448}
1449
Al Virob37ad282006-10-19 23:28:59 -07001450static __be32
Chuck Lever2548aa72020-11-03 11:13:00 -05001451nfsd4_decode_state_protect_ops(struct nfsd4_compoundargs *argp,
1452 struct nfsd4_exchange_id *exid)
1453{
1454 __be32 status;
1455
1456 status = nfsd4_decode_bitmap4(argp, exid->spo_must_enforce,
1457 ARRAY_SIZE(exid->spo_must_enforce));
1458 if (status)
1459 return nfserr_bad_xdr;
1460 status = nfsd4_decode_bitmap4(argp, exid->spo_must_allow,
1461 ARRAY_SIZE(exid->spo_must_allow));
1462 if (status)
1463 return nfserr_bad_xdr;
1464
1465 return nfs_ok;
1466}
1467
Chuck Lever547bfeb2020-11-03 11:17:50 -05001468/*
1469 * This implementation currently does not support SP4_SSV.
1470 * This decoder simply skips over these arguments.
1471 */
1472static noinline __be32
1473nfsd4_decode_ssv_sp_parms(struct nfsd4_compoundargs *argp,
1474 struct nfsd4_exchange_id *exid)
1475{
1476 u32 count, window, num_gss_handles;
1477 __be32 status;
1478
1479 /* ssp_ops */
1480 status = nfsd4_decode_state_protect_ops(argp, exid);
1481 if (status)
1482 return status;
1483
1484 /* ssp_hash_algs<> */
1485 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
1486 return nfserr_bad_xdr;
1487 while (count--) {
1488 status = nfsd4_decode_ignored_string(argp, 0);
1489 if (status)
1490 return status;
1491 }
1492
1493 /* ssp_encr_algs<> */
1494 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
1495 return nfserr_bad_xdr;
1496 while (count--) {
1497 status = nfsd4_decode_ignored_string(argp, 0);
1498 if (status)
1499 return status;
1500 }
1501
1502 if (xdr_stream_decode_u32(argp->xdr, &window) < 0)
1503 return nfserr_bad_xdr;
1504 if (xdr_stream_decode_u32(argp->xdr, &num_gss_handles) < 0)
1505 return nfserr_bad_xdr;
1506
1507 return nfs_ok;
1508}
1509
Chuck Lever2548aa72020-11-03 11:13:00 -05001510static __be32
Chuck Lever523ec6e2020-11-02 15:19:12 -05001511nfsd4_decode_state_protect4_a(struct nfsd4_compoundargs *argp,
1512 struct nfsd4_exchange_id *exid)
Andy Adamson2db134e2009-04-03 08:27:55 +03001513{
Chuck Lever523ec6e2020-11-02 15:19:12 -05001514 __be32 status;
Andy Adamson0733d212009-04-03 08:28:01 +03001515
Chuck Lever523ec6e2020-11-02 15:19:12 -05001516 if (xdr_stream_decode_u32(argp->xdr, &exid->spa_how) < 0)
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001517 return nfserr_bad_xdr;
Andy Adamson0733d212009-04-03 08:28:01 +03001518 switch (exid->spa_how) {
1519 case SP4_NONE:
1520 break;
1521 case SP4_MACH_CRED:
Chuck Lever2548aa72020-11-03 11:13:00 -05001522 status = nfsd4_decode_state_protect_ops(argp, exid);
Andrew Elbleed941642016-06-15 12:52:09 -04001523 if (status)
Chuck Lever2548aa72020-11-03 11:13:00 -05001524 return status;
Andy Adamson0733d212009-04-03 08:28:01 +03001525 break;
1526 case SP4_SSV:
Chuck Lever547bfeb2020-11-03 11:17:50 -05001527 status = nfsd4_decode_ssv_sp_parms(argp, exid);
Chuck Lever2548aa72020-11-03 11:13:00 -05001528 if (status)
1529 return status;
Andy Adamson0733d212009-04-03 08:28:01 +03001530 break;
1531 default:
Chuck Lever523ec6e2020-11-02 15:19:12 -05001532 return nfserr_bad_xdr;
Andy Adamson0733d212009-04-03 08:28:01 +03001533 }
1534
Chuck Lever523ec6e2020-11-02 15:19:12 -05001535 return nfs_ok;
1536}
1537
1538static __be32
Chuck Lever10ff8422020-11-16 15:21:55 -05001539nfsd4_decode_nfs_impl_id4(struct nfsd4_compoundargs *argp,
1540 struct nfsd4_exchange_id *exid)
1541{
1542 __be32 status;
1543 u32 count;
1544
1545 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
1546 return nfserr_bad_xdr;
1547 switch (count) {
1548 case 0:
1549 break;
1550 case 1:
1551 /* Note that RFC 8881 places no length limit on
1552 * nii_domain, but this implementation permits no
1553 * more than NFS4_OPAQUE_LIMIT bytes */
1554 status = nfsd4_decode_opaque(argp, &exid->nii_domain);
1555 if (status)
1556 return status;
1557 /* Note that RFC 8881 places no length limit on
1558 * nii_name, but this implementation permits no
1559 * more than NFS4_OPAQUE_LIMIT bytes */
1560 status = nfsd4_decode_opaque(argp, &exid->nii_name);
1561 if (status)
1562 return status;
1563 status = nfsd4_decode_nfstime4(argp, &exid->nii_time);
1564 if (status)
1565 return status;
1566 break;
1567 default:
1568 return nfserr_bad_xdr;
1569 }
1570
1571 return nfs_ok;
1572}
1573
1574static __be32
Chuck Lever523ec6e2020-11-02 15:19:12 -05001575nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
1576 struct nfsd4_exchange_id *exid)
1577{
Chuck Lever10ff8422020-11-16 15:21:55 -05001578 __be32 status;
Chuck Lever523ec6e2020-11-02 15:19:12 -05001579
1580 status = nfsd4_decode_verifier4(argp, &exid->verifier);
1581 if (status)
1582 return status;
1583 status = nfsd4_decode_opaque(argp, &exid->clname);
1584 if (status)
1585 return status;
1586 if (xdr_stream_decode_u32(argp->xdr, &exid->flags) < 0)
1587 return nfserr_bad_xdr;
1588 status = nfsd4_decode_state_protect4_a(argp, exid);
1589 if (status)
1590 return status;
Chuck Lever10ff8422020-11-16 15:21:55 -05001591 return nfsd4_decode_nfs_impl_id4(argp, exid);
Andy Adamson2db134e2009-04-03 08:27:55 +03001592}
1593
1594static __be32
Chuck Lever3a3f1fb2020-11-16 15:35:05 -05001595nfsd4_decode_channel_attrs4(struct nfsd4_compoundargs *argp,
1596 struct nfsd4_channel_attrs *ca)
1597{
1598 __be32 *p;
1599
1600 p = xdr_inline_decode(argp->xdr, XDR_UNIT * 7);
1601 if (!p)
1602 return nfserr_bad_xdr;
1603
1604 /* headerpadsz is ignored */
1605 p++;
1606 ca->maxreq_sz = be32_to_cpup(p++);
1607 ca->maxresp_sz = be32_to_cpup(p++);
1608 ca->maxresp_cached = be32_to_cpup(p++);
1609 ca->maxops = be32_to_cpup(p++);
1610 ca->maxreqs = be32_to_cpup(p++);
1611 ca->nr_rdma_attrs = be32_to_cpup(p);
1612 switch (ca->nr_rdma_attrs) {
1613 case 0:
1614 break;
1615 case 1:
1616 if (xdr_stream_decode_u32(argp->xdr, &ca->rdma_attrs) < 0)
1617 return nfserr_bad_xdr;
1618 break;
1619 default:
1620 return nfserr_bad_xdr;
1621 }
1622
1623 return nfs_ok;
1624}
1625
1626static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001627nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1628 struct nfsd4_create_session *sess)
1629{
Chuck Lever81243e32020-11-03 14:52:44 -05001630 __be32 status;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001631
Chuck Lever81243e32020-11-03 14:52:44 -05001632 status = nfsd4_decode_clientid4(argp, &sess->clientid);
1633 if (status)
1634 return status;
1635 if (xdr_stream_decode_u32(argp->xdr, &sess->seqid) < 0)
1636 return nfserr_bad_xdr;
1637 if (xdr_stream_decode_u32(argp->xdr, &sess->flags) < 0)
1638 return nfserr_bad_xdr;
Chuck Lever3a3f1fb2020-11-16 15:35:05 -05001639 status = nfsd4_decode_channel_attrs4(argp, &sess->fore_channel);
1640 if (status)
1641 return status;
1642 status = nfsd4_decode_channel_attrs4(argp, &sess->back_channel);
1643 if (status)
1644 return status;
Chuck Lever81243e32020-11-03 14:52:44 -05001645 if (xdr_stream_decode_u32(argp->xdr, &sess->callback_prog) < 0)
1646 return nfserr_bad_xdr;
1647 status = nfsd4_decode_cb_sec(argp, &sess->cb_sec);
1648 if (status)
1649 return status;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001650
Chuck Lever81243e32020-11-03 14:52:44 -05001651 return nfs_ok;
Andy Adamson2db134e2009-04-03 08:27:55 +03001652}
1653
1654static __be32
1655nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1656 struct nfsd4_destroy_session *destroy_session)
1657{
Chuck Lever94e254a2020-11-04 13:50:55 -05001658 return nfsd4_decode_sessionid4(argp, &destroy_session->sessionid);
Andy Adamson2db134e2009-04-03 08:27:55 +03001659}
1660
1661static __be32
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001662nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1663 struct nfsd4_free_stateid *free_stateid)
1664{
Chuck Leveraec387d2020-11-01 13:38:27 -05001665 return nfsd4_decode_stateid4(argp, &free_stateid->fr_stateid);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001666}
1667
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001668#ifdef CONFIG_NFSD_PNFS
1669static __be32
1670nfsd4_decode_getdeviceinfo(struct nfsd4_compoundargs *argp,
1671 struct nfsd4_getdeviceinfo *gdev)
1672{
Chuck Lever04495972020-11-03 15:03:50 -05001673 __be32 status;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001674
Chuck Lever04495972020-11-03 15:03:50 -05001675 status = nfsd4_decode_deviceid4(argp, &gdev->gd_devid);
1676 if (status)
1677 return status;
1678 if (xdr_stream_decode_u32(argp->xdr, &gdev->gd_layout_type) < 0)
1679 return nfserr_bad_xdr;
1680 if (xdr_stream_decode_u32(argp->xdr, &gdev->gd_maxcount) < 0)
1681 return nfserr_bad_xdr;
1682 if (xdr_stream_decode_uint32_array(argp->xdr,
1683 &gdev->gd_notify_types, 1) < 0)
1684 return nfserr_bad_xdr;
1685
1686 return nfs_ok;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001687}
1688
1689static __be32
Chuck Lever51859802020-11-04 10:40:07 -05001690nfsd4_decode_layoutcommit(struct nfsd4_compoundargs *argp,
1691 struct nfsd4_layoutcommit *lcp)
1692{
1693 __be32 *p, status;
1694
1695 if (xdr_stream_decode_u64(argp->xdr, &lcp->lc_seg.offset) < 0)
1696 return nfserr_bad_xdr;
1697 if (xdr_stream_decode_u64(argp->xdr, &lcp->lc_seg.length) < 0)
1698 return nfserr_bad_xdr;
1699 if (xdr_stream_decode_bool(argp->xdr, &lcp->lc_reclaim) < 0)
1700 return nfserr_bad_xdr;
1701 status = nfsd4_decode_stateid4(argp, &lcp->lc_sid);
1702 if (status)
1703 return status;
1704 if (xdr_stream_decode_u32(argp->xdr, &lcp->lc_newoffset) < 0)
1705 return nfserr_bad_xdr;
1706 if (lcp->lc_newoffset) {
1707 if (xdr_stream_decode_u64(argp->xdr, &lcp->lc_last_wr) < 0)
1708 return nfserr_bad_xdr;
1709 } else
1710 lcp->lc_last_wr = 0;
1711 p = xdr_inline_decode(argp->xdr, XDR_UNIT);
1712 if (!p)
1713 return nfserr_bad_xdr;
1714 if (xdr_item_is_present(p)) {
1715 status = nfsd4_decode_nfstime4(argp, &lcp->lc_mtime);
1716 if (status)
1717 return status;
1718 } else {
1719 lcp->lc_mtime.tv_nsec = UTIME_NOW;
1720 }
1721 return nfsd4_decode_layoutupdate4(argp, lcp);
1722}
1723
1724static __be32
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001725nfsd4_decode_layoutget(struct nfsd4_compoundargs *argp,
1726 struct nfsd4_layoutget *lgp)
1727{
Chuck Leverc8e88e32020-11-03 15:06:04 -05001728 __be32 status;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001729
Chuck Leverc8e88e32020-11-03 15:06:04 -05001730 if (xdr_stream_decode_u32(argp->xdr, &lgp->lg_signal) < 0)
1731 return nfserr_bad_xdr;
1732 if (xdr_stream_decode_u32(argp->xdr, &lgp->lg_layout_type) < 0)
1733 return nfserr_bad_xdr;
1734 if (xdr_stream_decode_u32(argp->xdr, &lgp->lg_seg.iomode) < 0)
1735 return nfserr_bad_xdr;
1736 if (xdr_stream_decode_u64(argp->xdr, &lgp->lg_seg.offset) < 0)
1737 return nfserr_bad_xdr;
1738 if (xdr_stream_decode_u64(argp->xdr, &lgp->lg_seg.length) < 0)
1739 return nfserr_bad_xdr;
1740 if (xdr_stream_decode_u64(argp->xdr, &lgp->lg_minlength) < 0)
1741 return nfserr_bad_xdr;
1742 status = nfsd4_decode_stateid4(argp, &lgp->lg_sid);
Kinglong Meedb59c0e2015-03-19 19:04:41 +08001743 if (status)
1744 return status;
Chuck Leverc8e88e32020-11-03 15:06:04 -05001745 if (xdr_stream_decode_u32(argp->xdr, &lgp->lg_maxcount) < 0)
1746 return nfserr_bad_xdr;
Kinglong Meedb59c0e2015-03-19 19:04:41 +08001747
Chuck Leverc8e88e32020-11-03 15:06:04 -05001748 return nfs_ok;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001749}
1750
1751static __be32
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001752nfsd4_decode_layoutreturn(struct nfsd4_compoundargs *argp,
1753 struct nfsd4_layoutreturn *lrp)
1754{
Chuck Lever645fcad2020-11-04 10:42:25 -05001755 if (xdr_stream_decode_bool(argp->xdr, &lrp->lr_reclaim) < 0)
1756 return nfserr_bad_xdr;
1757 if (xdr_stream_decode_u32(argp->xdr, &lrp->lr_layout_type) < 0)
1758 return nfserr_bad_xdr;
1759 if (xdr_stream_decode_u32(argp->xdr, &lrp->lr_seg.iomode) < 0)
1760 return nfserr_bad_xdr;
1761 return nfsd4_decode_layoutreturn4(argp, lrp);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001762}
1763#endif /* CONFIG_NFSD_PNFS */
1764
Chuck Lever53d70872020-11-03 14:33:12 -05001765static __be32 nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
1766 struct nfsd4_secinfo_no_name *sin)
1767{
1768 if (xdr_stream_decode_u32(argp->xdr, &sin->sin_style) < 0)
1769 return nfserr_bad_xdr;
1770 return nfs_ok;
1771}
1772
Andy Adamson2db134e2009-04-03 08:27:55 +03001773static __be32
Chuck Levercf907b12020-11-03 14:55:19 -05001774nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1775 struct nfsd4_sequence *seq)
1776{
1777 __be32 *p, status;
1778
1779 status = nfsd4_decode_sessionid4(argp, &seq->sessionid);
1780 if (status)
1781 return status;
1782 p = xdr_inline_decode(argp->xdr, XDR_UNIT * 4);
1783 if (!p)
1784 return nfserr_bad_xdr;
1785 seq->seqid = be32_to_cpup(p++);
1786 seq->slotid = be32_to_cpup(p++);
1787 seq->maxslots = be32_to_cpup(p++);
1788 seq->cachethis = be32_to_cpup(p);
1789
1790 return nfs_ok;
1791}
1792
1793static __be32
Chuck Leverb7a0c8f2020-11-03 14:57:44 -05001794nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1795{
1796 struct nfsd4_test_stateid_id *stateid;
1797 __be32 status;
1798 u32 i;
1799
1800 if (xdr_stream_decode_u32(argp->xdr, &test_stateid->ts_num_ids) < 0)
1801 return nfserr_bad_xdr;
1802
1803 INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
1804 for (i = 0; i < test_stateid->ts_num_ids; i++) {
1805 stateid = svcxdr_tmpalloc(argp, sizeof(*stateid));
1806 if (!stateid)
1807 return nfserrno(-ENOMEM); /* XXX: not jukebox? */
1808 INIT_LIST_HEAD(&stateid->ts_id_list);
1809 list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
1810 status = nfsd4_decode_stateid4(argp, &stateid->ts_id_stateid);
1811 if (status)
1812 return status;
1813 }
1814
1815 return nfs_ok;
1816}
1817
Chuck Leverc95f2ec2020-11-04 15:15:09 -05001818static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp,
1819 struct nfsd4_destroy_clientid *dc)
1820{
1821 return nfsd4_decode_clientid4(argp, &dc->clientid);
1822}
1823
Chuck Lever0d646782020-11-03 15:02:11 -05001824static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp,
1825 struct nfsd4_reclaim_complete *rc)
1826{
1827 if (xdr_stream_decode_bool(argp->xdr, &rc->rca_one_fs) < 0)
1828 return nfserr_bad_xdr;
1829 return nfs_ok;
1830}
1831
Chuck Leverb7a0c8f2020-11-03 14:57:44 -05001832static __be32
Anna Schumaker95d871f2014-11-07 14:44:26 -05001833nfsd4_decode_fallocate(struct nfsd4_compoundargs *argp,
1834 struct nfsd4_fallocate *fallocate)
1835{
Chuck Lever6aef27a2020-11-04 10:44:05 -05001836 __be32 status;
Anna Schumaker95d871f2014-11-07 14:44:26 -05001837
Chuck Lever6aef27a2020-11-04 10:44:05 -05001838 status = nfsd4_decode_stateid4(argp, &fallocate->falloc_stateid);
Anna Schumaker95d871f2014-11-07 14:44:26 -05001839 if (status)
1840 return status;
Chuck Lever6aef27a2020-11-04 10:44:05 -05001841 if (xdr_stream_decode_u64(argp->xdr, &fallocate->falloc_offset) < 0)
1842 return nfserr_bad_xdr;
1843 if (xdr_stream_decode_u64(argp->xdr, &fallocate->falloc_length) < 0)
1844 return nfserr_bad_xdr;
Anna Schumaker95d871f2014-11-07 14:44:26 -05001845
Chuck Lever6aef27a2020-11-04 10:44:05 -05001846 return nfs_ok;
Anna Schumaker95d871f2014-11-07 14:44:26 -05001847}
1848
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001849static __be32 nfsd4_decode_nl4_server(struct nfsd4_compoundargs *argp,
1850 struct nl4_server *ns)
1851{
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001852 struct nfs42_netaddr *naddr;
Chuck Leverf49e4b42020-11-16 18:05:06 -05001853 __be32 *p;
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001854
Chuck Leverf49e4b42020-11-16 18:05:06 -05001855 if (xdr_stream_decode_u32(argp->xdr, &ns->nl4_type) < 0)
1856 return nfserr_bad_xdr;
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001857
1858 /* currently support for 1 inter-server source server */
1859 switch (ns->nl4_type) {
1860 case NL4_NETADDR:
1861 naddr = &ns->u.nl4_addr;
1862
Chuck Leverf49e4b42020-11-16 18:05:06 -05001863 if (xdr_stream_decode_u32(argp->xdr, &naddr->netid_len) < 0)
1864 return nfserr_bad_xdr;
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001865 if (naddr->netid_len > RPCBIND_MAXNETIDLEN)
Chuck Leverf49e4b42020-11-16 18:05:06 -05001866 return nfserr_bad_xdr;
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001867
Chuck Leverf49e4b42020-11-16 18:05:06 -05001868 p = xdr_inline_decode(argp->xdr, naddr->netid_len);
1869 if (!p)
1870 return nfserr_bad_xdr;
1871 memcpy(naddr->netid, p, naddr->netid_len);
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001872
Chuck Leverf49e4b42020-11-16 18:05:06 -05001873 if (xdr_stream_decode_u32(argp->xdr, &naddr->addr_len) < 0)
1874 return nfserr_bad_xdr;
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001875 if (naddr->addr_len > RPCBIND_MAXUADDRLEN)
Chuck Leverf49e4b42020-11-16 18:05:06 -05001876 return nfserr_bad_xdr;
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001877
Chuck Leverf49e4b42020-11-16 18:05:06 -05001878 p = xdr_inline_decode(argp->xdr, naddr->addr_len);
1879 if (!p)
1880 return nfserr_bad_xdr;
1881 memcpy(naddr->addr, p, naddr->addr_len);
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001882 break;
1883 default:
Chuck Leverf49e4b42020-11-16 18:05:06 -05001884 return nfserr_bad_xdr;
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001885 }
Chuck Leverf49e4b42020-11-16 18:05:06 -05001886
1887 return nfs_ok;
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001888}
1889
Christoph Hellwigffa01602015-12-03 12:59:52 +01001890static __be32
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001891nfsd4_decode_copy(struct nfsd4_compoundargs *argp, struct nfsd4_copy *copy)
1892{
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001893 struct nl4_server *ns_dummy;
Chuck Levere8febea2020-11-04 10:49:37 -05001894 u32 consecutive, i, count;
1895 __be32 status;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001896
Chuck Levere8febea2020-11-04 10:49:37 -05001897 status = nfsd4_decode_stateid4(argp, &copy->cp_src_stateid);
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001898 if (status)
1899 return status;
Chuck Levere8febea2020-11-04 10:49:37 -05001900 status = nfsd4_decode_stateid4(argp, &copy->cp_dst_stateid);
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001901 if (status)
1902 return status;
Chuck Levere8febea2020-11-04 10:49:37 -05001903 if (xdr_stream_decode_u64(argp->xdr, &copy->cp_src_pos) < 0)
1904 return nfserr_bad_xdr;
1905 if (xdr_stream_decode_u64(argp->xdr, &copy->cp_dst_pos) < 0)
1906 return nfserr_bad_xdr;
1907 if (xdr_stream_decode_u64(argp->xdr, &copy->cp_count) < 0)
1908 return nfserr_bad_xdr;
1909 /* ca_consecutive: we always do consecutive copies */
1910 if (xdr_stream_decode_u32(argp->xdr, &consecutive) < 0)
1911 return nfserr_bad_xdr;
1912 if (xdr_stream_decode_u32(argp->xdr, &copy->cp_synchronous) < 0)
1913 return nfserr_bad_xdr;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001914
Chuck Levere8febea2020-11-04 10:49:37 -05001915 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
1916 return nfserr_bad_xdr;
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001917 copy->cp_intra = false;
1918 if (count == 0) { /* intra-server copy */
1919 copy->cp_intra = true;
Chuck Levere8febea2020-11-04 10:49:37 -05001920 return nfs_ok;
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001921 }
1922
Chuck Levere8febea2020-11-04 10:49:37 -05001923 /* decode all the supplied server addresses but use only the first */
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001924 status = nfsd4_decode_nl4_server(argp, &copy->cp_src);
1925 if (status)
1926 return status;
1927
1928 ns_dummy = kmalloc(sizeof(struct nl4_server), GFP_KERNEL);
1929 if (ns_dummy == NULL)
Chuck Levere8febea2020-11-04 10:49:37 -05001930 return nfserrno(-ENOMEM); /* XXX: jukebox? */
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001931 for (i = 0; i < count - 1; i++) {
1932 status = nfsd4_decode_nl4_server(argp, ns_dummy);
1933 if (status) {
1934 kfree(ns_dummy);
1935 return status;
1936 }
1937 }
1938 kfree(ns_dummy);
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001939
Chuck Levere8febea2020-11-04 10:49:37 -05001940 return nfs_ok;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001941}
1942
1943static __be32
Olga Kornievskaia51911862019-08-08 11:14:59 -04001944nfsd4_decode_copy_notify(struct nfsd4_compoundargs *argp,
1945 struct nfsd4_copy_notify *cn)
1946{
Chuck Lever5aff7d02020-09-11 14:47:59 -04001947 __be32 status;
Olga Kornievskaia51911862019-08-08 11:14:59 -04001948
Chuck Leverf9a953f2020-11-21 14:19:24 -05001949 status = nfsd4_decode_stateid4(argp, &cn->cpn_src_stateid);
Olga Kornievskaia51911862019-08-08 11:14:59 -04001950 if (status)
1951 return status;
1952 return nfsd4_decode_nl4_server(argp, &cn->cpn_dst);
1953}
1954
1955static __be32
Chuck Leverf9a953f2020-11-21 14:19:24 -05001956nfsd4_decode_offload_status(struct nfsd4_compoundargs *argp,
1957 struct nfsd4_offload_status *os)
1958{
Chuck Lever2846bb052020-11-21 14:21:25 -05001959 return nfsd4_decode_stateid4(argp, &os->stateid);
Chuck Leverf9a953f2020-11-21 14:19:24 -05001960}
1961
1962static __be32
Anna Schumaker24bab492014-09-26 13:58:27 -04001963nfsd4_decode_seek(struct nfsd4_compoundargs *argp, struct nfsd4_seek *seek)
1964{
Chuck Lever9d32b412020-11-04 10:54:47 -05001965 __be32 status;
Anna Schumaker24bab492014-09-26 13:58:27 -04001966
Chuck Lever9d32b412020-11-04 10:54:47 -05001967 status = nfsd4_decode_stateid4(argp, &seek->seek_stateid);
Anna Schumaker24bab492014-09-26 13:58:27 -04001968 if (status)
1969 return status;
Chuck Lever9d32b412020-11-04 10:54:47 -05001970 if (xdr_stream_decode_u64(argp->xdr, &seek->seek_offset) < 0)
1971 return nfserr_bad_xdr;
1972 if (xdr_stream_decode_u32(argp->xdr, &seek->seek_whence) < 0)
1973 return nfserr_bad_xdr;
Anna Schumaker24bab492014-09-26 13:58:27 -04001974
Chuck Lever9d32b412020-11-04 10:54:47 -05001975 return nfs_ok;
Anna Schumaker24bab492014-09-26 13:58:27 -04001976}
1977
Chuck Lever3dfd0b02020-11-04 10:46:46 -05001978static __be32
1979nfsd4_decode_clone(struct nfsd4_compoundargs *argp, struct nfsd4_clone *clone)
1980{
1981 __be32 status;
1982
1983 status = nfsd4_decode_stateid4(argp, &clone->cl_src_stateid);
1984 if (status)
1985 return status;
1986 status = nfsd4_decode_stateid4(argp, &clone->cl_dst_stateid);
1987 if (status)
1988 return status;
1989 if (xdr_stream_decode_u64(argp->xdr, &clone->cl_src_pos) < 0)
1990 return nfserr_bad_xdr;
1991 if (xdr_stream_decode_u64(argp->xdr, &clone->cl_dst_pos) < 0)
1992 return nfserr_bad_xdr;
1993 if (xdr_stream_decode_u64(argp->xdr, &clone->cl_count) < 0)
1994 return nfserr_bad_xdr;
1995
1996 return nfs_ok;
1997}
1998
Frank van der Linden23e50fe2020-06-23 22:39:26 +00001999/*
2000 * XDR data that is more than PAGE_SIZE in size is normally part of a
2001 * read or write. However, the size of extended attributes is limited
2002 * by the maximum request size, and then further limited by the underlying
2003 * filesystem limits. This can exceed PAGE_SIZE (currently, XATTR_SIZE_MAX
2004 * is 64k). Since there is no kvec- or page-based interface to xattrs,
2005 * and we're not dealing with contiguous pages, we need to do some copying.
2006 */
2007
2008/*
Chuck Leverc1346a122020-11-03 11:54:23 -05002009 * Decode data into buffer.
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002010 */
2011static __be32
Chuck Leverc1346a122020-11-03 11:54:23 -05002012nfsd4_vbuf_from_vector(struct nfsd4_compoundargs *argp, struct xdr_buf *xdr,
2013 char **bufp, u32 buflen)
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002014{
Chuck Leverc1346a122020-11-03 11:54:23 -05002015 struct page **pages = xdr->pages;
2016 struct kvec *head = xdr->head;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002017 char *tmp, *dp;
2018 u32 len;
2019
2020 if (buflen <= head->iov_len) {
2021 /*
2022 * We're in luck, the head has enough space. Just return
2023 * the head, no need for copying.
2024 */
2025 *bufp = head->iov_base;
2026 return 0;
2027 }
2028
2029 tmp = svcxdr_tmpalloc(argp, buflen);
2030 if (tmp == NULL)
2031 return nfserr_jukebox;
2032
2033 dp = tmp;
2034 memcpy(dp, head->iov_base, head->iov_len);
2035 buflen -= head->iov_len;
2036 dp += head->iov_len;
2037
2038 while (buflen > 0) {
2039 len = min_t(u32, buflen, PAGE_SIZE);
2040 memcpy(dp, page_address(*pages), len);
2041
2042 buflen -= len;
2043 dp += len;
2044 pages++;
2045 }
2046
2047 *bufp = tmp;
2048 return 0;
2049}
2050
2051/*
2052 * Get a user extended attribute name from the XDR buffer.
2053 * It will not have the "user." prefix, so prepend it.
2054 * Lastly, check for nul characters in the name.
2055 */
2056static __be32
2057nfsd4_decode_xattr_name(struct nfsd4_compoundargs *argp, char **namep)
2058{
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002059 char *name, *sp, *dp;
2060 u32 namelen, cnt;
Chuck Lever830c7152020-11-04 10:56:52 -05002061 __be32 *p;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002062
Chuck Lever830c7152020-11-04 10:56:52 -05002063 if (xdr_stream_decode_u32(argp->xdr, &namelen) < 0)
2064 return nfserr_bad_xdr;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002065 if (namelen > (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN))
2066 return nfserr_nametoolong;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002067 if (namelen == 0)
Chuck Lever830c7152020-11-04 10:56:52 -05002068 return nfserr_bad_xdr;
2069 p = xdr_inline_decode(argp->xdr, namelen);
2070 if (!p)
2071 return nfserr_bad_xdr;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002072 name = svcxdr_tmpalloc(argp, namelen + XATTR_USER_PREFIX_LEN + 1);
2073 if (!name)
2074 return nfserr_jukebox;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002075 memcpy(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
2076
2077 /*
2078 * Copy the extended attribute name over while checking for 0
2079 * characters.
2080 */
2081 sp = (char *)p;
2082 dp = name + XATTR_USER_PREFIX_LEN;
2083 cnt = namelen;
2084
2085 while (cnt-- > 0) {
2086 if (*sp == '\0')
Chuck Lever830c7152020-11-04 10:56:52 -05002087 return nfserr_bad_xdr;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002088 *dp++ = *sp++;
2089 }
2090 *dp = '\0';
2091
2092 *namep = name;
2093
Chuck Lever830c7152020-11-04 10:56:52 -05002094 return nfs_ok;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002095}
2096
2097/*
2098 * A GETXATTR op request comes without a length specifier. We just set the
2099 * maximum length for the reply based on XATTR_SIZE_MAX and the maximum
2100 * channel reply size. nfsd_getxattr will probe the length of the xattr,
2101 * check it against getxa_len, and allocate + return the value.
2102 */
2103static __be32
2104nfsd4_decode_getxattr(struct nfsd4_compoundargs *argp,
2105 struct nfsd4_getxattr *getxattr)
2106{
2107 __be32 status;
2108 u32 maxcount;
2109
2110 status = nfsd4_decode_xattr_name(argp, &getxattr->getxa_name);
2111 if (status)
2112 return status;
2113
2114 maxcount = svc_max_payload(argp->rqstp);
2115 maxcount = min_t(u32, XATTR_SIZE_MAX, maxcount);
2116
2117 getxattr->getxa_len = maxcount;
2118
2119 return status;
2120}
2121
2122static __be32
2123nfsd4_decode_setxattr(struct nfsd4_compoundargs *argp,
2124 struct nfsd4_setxattr *setxattr)
2125{
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002126 u32 flags, maxcount, size;
Chuck Lever403366a2020-11-04 10:59:57 -05002127 __be32 status;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002128
Chuck Lever403366a2020-11-04 10:59:57 -05002129 if (xdr_stream_decode_u32(argp->xdr, &flags) < 0)
2130 return nfserr_bad_xdr;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002131
2132 if (flags > SETXATTR4_REPLACE)
2133 return nfserr_inval;
2134 setxattr->setxa_flags = flags;
2135
2136 status = nfsd4_decode_xattr_name(argp, &setxattr->setxa_name);
2137 if (status)
2138 return status;
2139
2140 maxcount = svc_max_payload(argp->rqstp);
2141 maxcount = min_t(u32, XATTR_SIZE_MAX, maxcount);
2142
Chuck Lever403366a2020-11-04 10:59:57 -05002143 if (xdr_stream_decode_u32(argp->xdr, &size) < 0)
2144 return nfserr_bad_xdr;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002145 if (size > maxcount)
2146 return nfserr_xattr2big;
2147
2148 setxattr->setxa_len = size;
2149 if (size > 0) {
Chuck Leverc1346a122020-11-03 11:54:23 -05002150 struct xdr_buf payload;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002151
Chuck Leverc1346a122020-11-03 11:54:23 -05002152 if (!xdr_stream_subsegment(argp->xdr, &payload, size))
Chuck Lever403366a2020-11-04 10:59:57 -05002153 return nfserr_bad_xdr;
Chuck Leverc1346a122020-11-03 11:54:23 -05002154 status = nfsd4_vbuf_from_vector(argp, &payload,
2155 &setxattr->setxa_buf, size);
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002156 }
2157
Chuck Lever403366a2020-11-04 10:59:57 -05002158 return nfs_ok;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002159}
2160
2161static __be32
2162nfsd4_decode_listxattrs(struct nfsd4_compoundargs *argp,
2163 struct nfsd4_listxattrs *listxattrs)
2164{
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002165 u32 maxcount;
2166
Chuck Lever22120362020-11-04 11:04:02 -05002167 if (xdr_stream_decode_u64(argp->xdr, &listxattrs->lsxa_cookie) < 0)
2168 return nfserr_bad_xdr;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002169
2170 /*
2171 * If the cookie is too large to have even one user.x attribute
2172 * plus trailing '\0' left in a maximum size buffer, it's invalid.
2173 */
2174 if (listxattrs->lsxa_cookie >=
2175 (XATTR_LIST_MAX / (XATTR_USER_PREFIX_LEN + 2)))
2176 return nfserr_badcookie;
2177
Chuck Lever22120362020-11-04 11:04:02 -05002178 if (xdr_stream_decode_u32(argp->xdr, &maxcount) < 0)
2179 return nfserr_bad_xdr;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002180 if (maxcount < 8)
2181 /* Always need at least 2 words (length and one character) */
2182 return nfserr_inval;
2183
2184 maxcount = min(maxcount, svc_max_payload(argp->rqstp));
2185 listxattrs->lsxa_maxcount = maxcount;
2186
Chuck Lever22120362020-11-04 11:04:02 -05002187 return nfs_ok;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002188}
2189
2190static __be32
2191nfsd4_decode_removexattr(struct nfsd4_compoundargs *argp,
2192 struct nfsd4_removexattr *removexattr)
2193{
2194 return nfsd4_decode_xattr_name(argp, &removexattr->rmxa_name);
2195}
2196
Anna Schumaker24bab492014-09-26 13:58:27 -04002197static __be32
Benny Halevy347e0ad2008-07-02 11:13:41 +03002198nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
2199{
2200 return nfs_ok;
2201}
2202
Benny Halevy3c375c62008-07-02 11:14:01 +03002203static __be32
2204nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
2205{
Benny Halevy1e685ec2009-03-04 23:06:06 +02002206 return nfserr_notsupp;
Benny Halevy3c375c62008-07-02 11:14:01 +03002207}
2208
Benny Halevy347e0ad2008-07-02 11:13:41 +03002209typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
2210
Chuck Leverc1df6092017-08-01 11:59:58 -04002211static const nfsd4_dec nfsd4_dec_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04002212 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
2213 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
2214 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
2215 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
2216 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
2217 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
2218 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
2219 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
2220 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
2221 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
2222 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
2223 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
2224 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
2225 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
2226 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
2227 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
2228 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
2229 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
2230 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
2231 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002232 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_putpubfh,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04002233 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
2234 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
2235 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
2236 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
2237 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
2238 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
2239 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
2240 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
2241 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
2242 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
2243 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
2244 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
2245 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
2246 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
2247 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
2248 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
Andy Adamson2db134e2009-04-03 08:27:55 +03002249
2250 /* new operations for NFSv4.1 */
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002251 [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002252 [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
Randy Dunlap9064caa2009-04-28 16:48:25 -07002253 [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
2254 [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
2255 [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04002256 [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07002257 [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002258#ifdef CONFIG_NFSD_PNFS
2259 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_getdeviceinfo,
2260 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
2261 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_layoutcommit,
2262 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_layoutget,
2263 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_layoutreturn,
2264#else
Randy Dunlap9064caa2009-04-28 16:48:25 -07002265 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
2266 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
2267 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
2268 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
2269 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002270#endif
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05002271 [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
Randy Dunlap9064caa2009-04-28 16:48:25 -07002272 [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
2273 [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
Bryan Schumaker17456802011-07-13 10:50:48 -04002274 [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07002275 [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
Mi Jinlong345c2842011-10-20 17:51:39 +08002276 [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002277 [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
Anna Schumaker87a15a82014-09-26 13:58:26 -04002278
2279 /* new operations for NFSv4.2 */
Anna Schumaker95d871f2014-11-07 14:44:26 -05002280 [OP_ALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
Anna Schumaker29ae7f92016-09-07 15:57:30 -04002281 [OP_COPY] = (nfsd4_dec)nfsd4_decode_copy,
Olga Kornievskaia51911862019-08-08 11:14:59 -04002282 [OP_COPY_NOTIFY] = (nfsd4_dec)nfsd4_decode_copy_notify,
Anna Schumakerb0cb9082014-11-07 14:44:27 -05002283 [OP_DEALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
Anna Schumaker87a15a82014-09-26 13:58:26 -04002284 [OP_IO_ADVISE] = (nfsd4_dec)nfsd4_decode_notsupp,
2285 [OP_LAYOUTERROR] = (nfsd4_dec)nfsd4_decode_notsupp,
2286 [OP_LAYOUTSTATS] = (nfsd4_dec)nfsd4_decode_notsupp,
Olga Kornievskaia885e2bf2018-07-20 18:19:19 -04002287 [OP_OFFLOAD_CANCEL] = (nfsd4_dec)nfsd4_decode_offload_status,
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04002288 [OP_OFFLOAD_STATUS] = (nfsd4_dec)nfsd4_decode_offload_status,
Anna Schumaker528b8492020-09-28 13:08:58 -04002289 [OP_READ_PLUS] = (nfsd4_dec)nfsd4_decode_read,
Anna Schumaker24bab492014-09-26 13:58:27 -04002290 [OP_SEEK] = (nfsd4_dec)nfsd4_decode_seek,
Anna Schumaker87a15a82014-09-26 13:58:26 -04002291 [OP_WRITE_SAME] = (nfsd4_dec)nfsd4_decode_notsupp,
Christoph Hellwigffa01602015-12-03 12:59:52 +01002292 [OP_CLONE] = (nfsd4_dec)nfsd4_decode_clone,
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002293 /* RFC 8276 extended atributes operations */
2294 [OP_GETXATTR] = (nfsd4_dec)nfsd4_decode_getxattr,
2295 [OP_SETXATTR] = (nfsd4_dec)nfsd4_decode_setxattr,
2296 [OP_LISTXATTRS] = (nfsd4_dec)nfsd4_decode_listxattrs,
2297 [OP_REMOVEXATTR] = (nfsd4_dec)nfsd4_decode_removexattr,
Andy Adamson2db134e2009-04-03 08:27:55 +03002298};
2299
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002300static inline bool
2301nfsd4_opnum_in_range(struct nfsd4_compoundargs *argp, struct nfsd4_op *op)
2302{
Anna Schumaker8217d142013-10-30 13:38:13 -04002303 if (op->opnum < FIRST_NFS4_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002304 return false;
Anna Schumaker8217d142013-10-30 13:38:13 -04002305 else if (argp->minorversion == 0 && op->opnum > LAST_NFS40_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002306 return false;
Anna Schumaker8217d142013-10-30 13:38:13 -04002307 else if (argp->minorversion == 1 && op->opnum > LAST_NFS41_OP)
2308 return false;
2309 else if (argp->minorversion == 2 && op->opnum > LAST_NFS42_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002310 return false;
2311 return true;
2312}
Benny Halevyf2feb962008-07-02 11:14:22 +03002313
Chuck Leverd9b74bd2020-11-04 11:07:06 -05002314static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
2316{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 struct nfsd4_op *op;
J. Bruce Fields10910062011-01-24 12:11:02 -05002318 bool cachethis = false;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002319 int auth_slack= argp->rqstp->rq_auth_slack;
2320 int max_reply = auth_slack + 8; /* opcnt, status */
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05002321 int readcount = 0;
2322 int readbytes = 0;
Chuck Leverd9b74bd2020-11-04 11:07:06 -05002323 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 int i;
2325
Chuck Leverd9b74bd2020-11-04 11:07:06 -05002326 if (xdr_stream_decode_u32(argp->xdr, &argp->taglen) < 0)
2327 return 0;
2328 max_reply += XDR_UNIT;
2329 argp->tag = NULL;
2330 if (unlikely(argp->taglen)) {
2331 if (argp->taglen > NFSD4_MAX_TAGLEN)
2332 return 0;
2333 p = xdr_inline_decode(argp->xdr, argp->taglen);
2334 if (!p)
2335 return 0;
2336 argp->tag = svcxdr_tmpalloc(argp, argp->taglen);
2337 if (!argp->tag)
2338 return 0;
2339 memcpy(argp->tag, p, argp->taglen);
2340 max_reply += xdr_align_size(argp->taglen);
2341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Chuck Leverd9b74bd2020-11-04 11:07:06 -05002343 if (xdr_stream_decode_u32(argp->xdr, &argp->minorversion) < 0)
2344 return 0;
2345 if (xdr_stream_decode_u32(argp->xdr, &argp->opcnt) < 0)
2346 return 0;
2347
J. Bruce Fields00781172017-11-15 12:30:27 -05002348 /*
2349 * NFS4ERR_RESOURCE is a more helpful error than GARBAGE_ARGS
2350 * here, so we return success at the xdr level so that
2351 * nfsd4_proc can handle this is an NFS-level error.
2352 */
2353 if (argp->opcnt > NFSD_MAX_OPS_PER_COMPOUND)
Chuck Leverd9b74bd2020-11-04 11:07:06 -05002354 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
Tobias Klausere8c96f82006-03-24 03:15:34 -08002356 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
J. Bruce Fields5d6031c2014-07-17 16:20:39 -04002357 argp->ops = kzalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 if (!argp->ops) {
2359 argp->ops = argp->iops;
Chuck Lever817cb9d2007-09-11 18:01:20 -04002360 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
Chuck Leverd9b74bd2020-11-04 11:07:06 -05002361 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 }
2363 }
2364
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002365 if (argp->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
Benny Halevy30cff1f2008-07-02 11:13:18 +03002366 argp->opcnt = 0;
2367
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 for (i = 0; i < argp->opcnt; i++) {
2369 op = &argp->ops[i];
2370 op->replay = NULL;
2371
Chuck Lever3a237b42020-11-22 12:49:52 -05002372 if (xdr_stream_decode_u32(argp->xdr, &op->opnum) < 0)
Chuck Leverd9b74bd2020-11-04 11:07:06 -05002373 return 0;
Chuck Lever08281342020-11-21 11:36:42 -05002374 if (nfsd4_opnum_in_range(argp, op)) {
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002375 op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
Chuck Lever08281342020-11-21 11:36:42 -05002376 if (op->status != nfs_ok)
2377 trace_nfsd_compound_decode_err(argp->rqstp,
2378 argp->opcnt, i,
2379 op->opnum,
2380 op->status);
2381 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 op->opnum = OP_ILLEGAL;
2383 op->status = nfserr_op_illegal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 }
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002385 op->opdesc = OPDESC(op);
J. Bruce Fields10910062011-01-24 12:11:02 -05002386 /*
2387 * We'll try to cache the result in the DRC if any one
2388 * op in the compound wants to be cached:
2389 */
2390 cachethis |= nfsd4_cache_this_op(op);
J. Bruce Fields6ff40de2013-11-05 15:07:16 -05002391
Anna Schumaker528b8492020-09-28 13:08:58 -04002392 if (op->opnum == OP_READ || op->opnum == OP_READ_PLUS) {
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05002393 readcount++;
2394 readbytes += nfsd4_max_reply(argp->rqstp, op);
2395 } else
2396 max_reply += nfsd4_max_reply(argp->rqstp, op);
J. Bruce Fieldsf7b43d02014-08-12 11:41:40 -04002397 /*
Kinglong Mee7323f0d2017-02-03 22:51:46 +08002398 * OP_LOCK and OP_LOCKT may return a conflicting lock.
2399 * (Special case because it will just skip encoding this
2400 * if it runs out of xdr buffer space, and it is the only
2401 * operation that behaves this way.)
J. Bruce Fieldsf7b43d02014-08-12 11:41:40 -04002402 */
Kinglong Mee7323f0d2017-02-03 22:51:46 +08002403 if (op->opnum == OP_LOCK || op->opnum == OP_LOCKT)
J. Bruce Fieldsf7b43d02014-08-12 11:41:40 -04002404 max_reply += NFS4_OPAQUE_LIMIT;
J. Bruce Fieldse372ba62014-05-19 12:27:11 -04002405
2406 if (op->status) {
2407 argp->opcnt = i+1;
2408 break;
2409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 }
J. Bruce Fields10910062011-01-24 12:11:02 -05002411 /* Sessions make the DRC unnecessary: */
2412 if (argp->minorversion)
2413 cachethis = false;
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05002414 svc_reserve(argp->rqstp, max_reply + readbytes);
J. Bruce Fields10910062011-01-24 12:11:02 -05002415 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002417 if (readcount > 1 || max_reply > PAGE_SIZE - auth_slack)
Jeff Layton779fb0f2014-11-19 07:51:18 -05002418 clear_bit(RQ_SPLICE_OK, &argp->rqstp->rq_flags);
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05002419
Chuck Leverd9b74bd2020-11-04 11:07:06 -05002420 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
NeilBrownb8800922017-01-30 17:17:00 +11002423static __be32 *encode_change(__be32 *p, struct kstat *stat, struct inode *inode,
2424 struct svc_export *exp)
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002425{
NeilBrownb8800922017-01-30 17:17:00 +11002426 if (exp->ex_flags & NFSEXP_V4ROOT) {
2427 *p++ = cpu_to_be32(convert_to_wallclock(exp->cd->flush_time));
2428 *p++ = 0;
2429 } else if (IS_I_VERSION(inode)) {
Amir Goldstein39ca1bf2018-01-03 17:14:35 +02002430 p = xdr_encode_hyper(p, nfsd4_change_attribute(stat, inode));
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002431 } else {
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002432 *p++ = cpu_to_be32(stat->ctime.tv_sec);
2433 *p++ = cpu_to_be32(stat->ctime.tv_nsec);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002434 }
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002435 return p;
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002436}
2437
J. Bruce Fields16945142018-04-25 14:34:11 -04002438/*
2439 * ctime (in NFSv4, time_metadata) is not writeable, and the client
2440 * doesn't really care what resolution could theoretically be stored by
2441 * the filesystem.
2442 *
2443 * The client cares how close together changes can be while still
2444 * guaranteeing ctime changes. For most filesystems (which have
2445 * timestamps with nanosecond fields) that is limited by the resolution
2446 * of the time returned from current_time() (which I'm assuming to be
2447 * 1/HZ).
2448 */
2449static __be32 *encode_time_delta(__be32 *p, struct inode *inode)
2450{
Arnd Bergmanne4598e32019-10-31 15:14:03 +01002451 struct timespec64 ts;
J. Bruce Fields16945142018-04-25 14:34:11 -04002452 u32 ns;
2453
2454 ns = max_t(u32, NSEC_PER_SEC/HZ, inode->i_sb->s_time_gran);
Arnd Bergmanne4598e32019-10-31 15:14:03 +01002455 ts = ns_to_timespec64(ns);
J. Bruce Fields16945142018-04-25 14:34:11 -04002456
2457 p = xdr_encode_hyper(p, ts.tv_sec);
2458 *p++ = cpu_to_be32(ts.tv_nsec);
2459
2460 return p;
2461}
2462
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002463static __be32 *encode_cinfo(__be32 *p, struct nfsd4_change_info *c)
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002464{
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002465 *p++ = cpu_to_be32(c->atomic);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002466 if (c->change_supported) {
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002467 p = xdr_encode_hyper(p, c->before_change);
2468 p = xdr_encode_hyper(p, c->after_change);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002469 } else {
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002470 *p++ = cpu_to_be32(c->before_ctime_sec);
2471 *p++ = cpu_to_be32(c->before_ctime_nsec);
2472 *p++ = cpu_to_be32(c->after_ctime_sec);
2473 *p++ = cpu_to_be32(c->after_ctime_nsec);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002474 }
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002475 return p;
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002476}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002478/* Encode as an array of strings the string given with components
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002479 * separated @sep, escaped with esc_enter and esc_exit.
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002480 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002481static __be32 nfsd4_encode_components_esc(struct xdr_stream *xdr, char sep,
2482 char *components, char esc_enter,
2483 char esc_exit)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002484{
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002485 __be32 *p;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002486 __be32 pathlen;
2487 int pathlen_offset;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002488 int strlen, count=0;
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002489 char *str, *end, *next;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002490
2491 dprintk("nfsd4_encode_components(%s)\n", components);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002492
2493 pathlen_offset = xdr->buf->len;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002494 p = xdr_reserve_space(xdr, 4);
2495 if (!p)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002496 return nfserr_resource;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002497 p++; /* We will fill this in with @count later */
2498
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002499 end = str = components;
2500 while (*end) {
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002501 bool found_esc = false;
2502
2503 /* try to parse as esc_start, ..., esc_end, sep */
2504 if (*str == esc_enter) {
2505 for (; *end && (*end != esc_exit); end++)
2506 /* find esc_exit or end of string */;
2507 next = end + 1;
2508 if (*end && (!*next || *next == sep)) {
2509 str++;
2510 found_esc = true;
2511 }
2512 }
2513
2514 if (!found_esc)
2515 for (; *end && (*end != sep); end++)
2516 /* find sep or end of string */;
2517
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002518 strlen = end - str;
2519 if (strlen) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002520 p = xdr_reserve_space(xdr, strlen + 4);
2521 if (!p)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002522 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04002523 p = xdr_encode_opaque(p, str, strlen);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002524 count++;
2525 }
2526 else
2527 end++;
Benjamin Coddington5a64e562014-12-07 16:05:47 -05002528 if (found_esc)
2529 end = next;
2530
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002531 str = end;
2532 }
Benjamin Coddingtonbf7491f2014-12-07 16:05:48 -05002533 pathlen = htonl(count);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002534 write_bytes_to_xdr_buf(xdr->buf, pathlen_offset, &pathlen, 4);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002535 return 0;
2536}
2537
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002538/* Encode as an array of strings the string given with components
2539 * separated @sep.
2540 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002541static __be32 nfsd4_encode_components(struct xdr_stream *xdr, char sep,
2542 char *components)
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002543{
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002544 return nfsd4_encode_components_esc(xdr, sep, components, 0, 0);
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002545}
2546
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002547/*
2548 * encode a location element of a fs_locations structure
2549 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002550static __be32 nfsd4_encode_fs_location4(struct xdr_stream *xdr,
2551 struct nfsd4_fs_location *location)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002552{
Al Virob37ad282006-10-19 23:28:59 -07002553 __be32 status;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002554
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002555 status = nfsd4_encode_components_esc(xdr, ':', location->hosts,
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002556 '[', ']');
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002557 if (status)
2558 return status;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002559 status = nfsd4_encode_components(xdr, '/', location->path);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002560 if (status)
2561 return status;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002562 return 0;
2563}
2564
2565/*
Trond Myklebusted748aa2011-09-12 19:37:06 -04002566 * Encode a path in RFC3530 'pathname4' format
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002567 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002568static __be32 nfsd4_encode_path(struct xdr_stream *xdr,
2569 const struct path *root,
2570 const struct path *path)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002571{
Al Viro301f0262013-09-01 16:06:39 -04002572 struct path cur = *path;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002573 __be32 *p;
Trond Myklebusted748aa2011-09-12 19:37:06 -04002574 struct dentry **components = NULL;
2575 unsigned int ncomponents = 0;
2576 __be32 err = nfserr_jukebox;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002577
Trond Myklebusted748aa2011-09-12 19:37:06 -04002578 dprintk("nfsd4_encode_components(");
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002579
Trond Myklebusted748aa2011-09-12 19:37:06 -04002580 path_get(&cur);
2581 /* First walk the path up to the nfsd root, and store the
2582 * dentries/path components in an array.
2583 */
2584 for (;;) {
Kinglong Meeb77a4b22015-03-15 23:13:32 +08002585 if (path_equal(&cur, root))
Trond Myklebusted748aa2011-09-12 19:37:06 -04002586 break;
2587 if (cur.dentry == cur.mnt->mnt_root) {
2588 if (follow_up(&cur))
2589 continue;
2590 goto out_free;
2591 }
2592 if ((ncomponents & 15) == 0) {
2593 struct dentry **new;
2594 new = krealloc(components,
2595 sizeof(*new) * (ncomponents + 16),
2596 GFP_KERNEL);
2597 if (!new)
2598 goto out_free;
2599 components = new;
2600 }
2601 components[ncomponents++] = cur.dentry;
2602 cur.dentry = dget_parent(cur.dentry);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002603 }
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002604 err = nfserr_resource;
2605 p = xdr_reserve_space(xdr, 4);
2606 if (!p)
Trond Myklebusted748aa2011-09-12 19:37:06 -04002607 goto out_free;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002608 *p++ = cpu_to_be32(ncomponents);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002609
2610 while (ncomponents) {
2611 struct dentry *dentry = components[ncomponents - 1];
Al Viro301f0262013-09-01 16:06:39 -04002612 unsigned int len;
Trond Myklebusted748aa2011-09-12 19:37:06 -04002613
Al Viro301f0262013-09-01 16:06:39 -04002614 spin_lock(&dentry->d_lock);
2615 len = dentry->d_name.len;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002616 p = xdr_reserve_space(xdr, len + 4);
2617 if (!p) {
Al Viro301f0262013-09-01 16:06:39 -04002618 spin_unlock(&dentry->d_lock);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002619 goto out_free;
Al Viro301f0262013-09-01 16:06:39 -04002620 }
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04002621 p = xdr_encode_opaque(p, dentry->d_name.name, len);
Al Viroa4555892014-10-21 20:11:25 -04002622 dprintk("/%pd", dentry);
Al Viro301f0262013-09-01 16:06:39 -04002623 spin_unlock(&dentry->d_lock);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002624 dput(dentry);
2625 ncomponents--;
2626 }
2627
Trond Myklebusted748aa2011-09-12 19:37:06 -04002628 err = 0;
2629out_free:
2630 dprintk(")\n");
2631 while (ncomponents)
2632 dput(components[--ncomponents]);
2633 kfree(components);
2634 path_put(&cur);
2635 return err;
2636}
2637
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002638static __be32 nfsd4_encode_fsloc_fsroot(struct xdr_stream *xdr,
2639 struct svc_rqst *rqstp, const struct path *path)
Trond Myklebusted748aa2011-09-12 19:37:06 -04002640{
2641 struct svc_export *exp_ps;
2642 __be32 res;
2643
2644 exp_ps = rqst_find_fsidzero_export(rqstp);
2645 if (IS_ERR(exp_ps))
2646 return nfserrno(PTR_ERR(exp_ps));
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002647 res = nfsd4_encode_path(xdr, &exp_ps->ex_path, path);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002648 exp_put(exp_ps);
2649 return res;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002650}
2651
2652/*
2653 * encode a fs_locations structure
2654 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002655static __be32 nfsd4_encode_fs_locations(struct xdr_stream *xdr,
2656 struct svc_rqst *rqstp, struct svc_export *exp)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002657{
Al Virob37ad282006-10-19 23:28:59 -07002658 __be32 status;
Al Virocc45f012006-10-19 23:28:44 -07002659 int i;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002660 __be32 *p;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002661 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002662
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002663 status = nfsd4_encode_fsloc_fsroot(xdr, rqstp, &exp->ex_path);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002664 if (status)
2665 return status;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002666 p = xdr_reserve_space(xdr, 4);
2667 if (!p)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002668 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002669 *p++ = cpu_to_be32(fslocs->locations_count);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002670 for (i=0; i<fslocs->locations_count; i++) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002671 status = nfsd4_encode_fs_location4(xdr, &fslocs->locations[i]);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002672 if (status)
2673 return status;
2674 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002675 return 0;
2676}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002678static u32 nfs4_file_type(umode_t mode)
2679{
2680 switch (mode & S_IFMT) {
2681 case S_IFIFO: return NF4FIFO;
2682 case S_IFCHR: return NF4CHR;
2683 case S_IFDIR: return NF4DIR;
2684 case S_IFBLK: return NF4BLK;
2685 case S_IFLNK: return NF4LNK;
2686 case S_IFREG: return NF4REG;
2687 case S_IFSOCK: return NF4SOCK;
2688 default: return NF4BAD;
Tom Rix25fef482020-11-01 07:32:34 -08002689 }
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002690}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
Al Virob37ad282006-10-19 23:28:59 -07002692static inline __be32
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002693nfsd4_encode_aclname(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2694 struct nfs4_ace *ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695{
J. Bruce Fields35541162014-01-08 09:49:01 -05002696 if (ace->whotype != NFS4_ACL_WHO_NAMED)
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002697 return nfs4_acl_write_who(xdr, ace->whotype);
J. Bruce Fields35541162014-01-08 09:49:01 -05002698 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002699 return nfsd4_encode_group(xdr, rqstp, ace->who_gid);
J. Bruce Fields35541162014-01-08 09:49:01 -05002700 else
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002701 return nfsd4_encode_user(xdr, rqstp, ace->who_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702}
2703
Kinglong Mee6896f152015-07-30 21:52:44 +08002704static inline __be32
Jeff Layton8a4c3922016-07-10 15:55:58 -04002705nfsd4_encode_layout_types(struct xdr_stream *xdr, u32 layout_types)
Kinglong Mee6896f152015-07-30 21:52:44 +08002706{
Jeff Layton8a4c3922016-07-10 15:55:58 -04002707 __be32 *p;
2708 unsigned long i = hweight_long(layout_types);
Kinglong Mee6896f152015-07-30 21:52:44 +08002709
Jeff Layton8a4c3922016-07-10 15:55:58 -04002710 p = xdr_reserve_space(xdr, 4 + 4 * i);
2711 if (!p)
2712 return nfserr_resource;
2713
2714 *p++ = cpu_to_be32(i);
2715
2716 for (i = LAYOUT_NFSV4_1_FILES; i < LAYOUT_TYPE_MAX; ++i)
2717 if (layout_types & (1 << i))
2718 *p++ = cpu_to_be32(i);
Kinglong Mee6896f152015-07-30 21:52:44 +08002719
2720 return 0;
2721}
2722
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002723#define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
2724 FATTR4_WORD0_RDATTR_ERROR)
2725#define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
Kinglong Meec2227a32015-07-07 10:16:37 +08002726#define WORD2_ABSENT_FS_ATTRS 0
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002727
David Quigley18032ca2013-05-02 13:19:10 -04002728#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2729static inline __be32
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002730nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2731 void *context, int len)
David Quigley18032ca2013-05-02 13:19:10 -04002732{
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002733 __be32 *p;
David Quigley18032ca2013-05-02 13:19:10 -04002734
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002735 p = xdr_reserve_space(xdr, len + 4 + 4 + 4);
2736 if (!p)
David Quigley18032ca2013-05-02 13:19:10 -04002737 return nfserr_resource;
2738
2739 /*
2740 * For now we use a 0 here to indicate the null translation; in
2741 * the future we may place a call to translation code here.
2742 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002743 *p++ = cpu_to_be32(0); /* lfs */
2744 *p++ = cpu_to_be32(0); /* pi */
David Quigley18032ca2013-05-02 13:19:10 -04002745 p = xdr_encode_opaque(p, context, len);
David Quigley18032ca2013-05-02 13:19:10 -04002746 return 0;
2747}
2748#else
2749static inline __be32
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002750nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2751 void *context, int len)
David Quigley18032ca2013-05-02 13:19:10 -04002752{ return 0; }
2753#endif
2754
Kinglong Meec2227a32015-07-07 10:16:37 +08002755static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *bmval2, u32 *rdattr_err)
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002756{
2757 /* As per referral draft: */
2758 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
2759 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
2760 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
2761 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
2762 *rdattr_err = NFSERR_MOVED;
2763 else
2764 return nfserr_moved;
2765 }
2766 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
2767 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
Kinglong Meec2227a32015-07-07 10:16:37 +08002768 *bmval2 &= WORD2_ABSENT_FS_ATTRS;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002769 return 0;
2770}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002772
2773static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
2774{
2775 struct path path = exp->ex_path;
2776 int err;
2777
2778 path_get(&path);
2779 while (follow_up(&path)) {
2780 if (path.dentry != path.mnt->mnt_root)
2781 break;
2782 }
David Howellsa528d352017-01-31 16:46:22 +00002783 err = vfs_getattr(&path, stat, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002784 path_put(&path);
2785 return err;
2786}
2787
Kinglong Mee75976de2015-07-30 21:55:30 +08002788static __be32
2789nfsd4_encode_bitmap(struct xdr_stream *xdr, u32 bmval0, u32 bmval1, u32 bmval2)
2790{
2791 __be32 *p;
2792
2793 if (bmval2) {
2794 p = xdr_reserve_space(xdr, 16);
2795 if (!p)
2796 goto out_resource;
2797 *p++ = cpu_to_be32(3);
2798 *p++ = cpu_to_be32(bmval0);
2799 *p++ = cpu_to_be32(bmval1);
2800 *p++ = cpu_to_be32(bmval2);
2801 } else if (bmval1) {
2802 p = xdr_reserve_space(xdr, 12);
2803 if (!p)
2804 goto out_resource;
2805 *p++ = cpu_to_be32(2);
2806 *p++ = cpu_to_be32(bmval0);
2807 *p++ = cpu_to_be32(bmval1);
2808 } else {
2809 p = xdr_reserve_space(xdr, 8);
2810 if (!p)
2811 goto out_resource;
2812 *p++ = cpu_to_be32(1);
2813 *p++ = cpu_to_be32(bmval0);
2814 }
2815
2816 return 0;
2817out_resource:
2818 return nfserr_resource;
2819}
2820
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821/*
2822 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
2823 * ourselves.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 */
Jeff Laytonda2ebce2014-05-30 09:09:25 -04002825static __be32
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04002826nfsd4_encode_fattr(struct xdr_stream *xdr, struct svc_fh *fhp,
2827 struct svc_export *exp,
2828 struct dentry *dentry, u32 *bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002829 struct svc_rqst *rqstp, int ignore_crossmnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830{
2831 u32 bmval0 = bmval[0];
2832 u32 bmval1 = bmval[1];
Andy Adamson7e705702009-04-03 08:29:11 +03002833 u32 bmval2 = bmval[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 struct kstat stat;
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002835 struct svc_fh *tempfh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 struct kstatfs statfs;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002837 __be32 *p;
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05002838 int starting_len = xdr->buf->len;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002839 int attrlen_offset;
2840 __be32 attrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 u32 dummy;
2842 u64 dummy64;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002843 u32 rdattr_err = 0;
Al Virob37ad282006-10-19 23:28:59 -07002844 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07002845 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 struct nfs4_acl *acl = NULL;
Arnd Bergmann0ab88ca2019-03-22 15:07:11 +01002847#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
David Quigley18032ca2013-05-02 13:19:10 -04002848 void *context = NULL;
2849 int contextlen;
Arnd Bergmann0ab88ca2019-03-22 15:07:11 +01002850#endif
David Quigley18032ca2013-05-02 13:19:10 -04002851 bool contextsupport = false;
Andy Adamson7e705702009-04-03 08:29:11 +03002852 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2853 u32 minorversion = resp->cstate.minorversion;
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002854 struct path path = {
2855 .mnt = exp->ex_path.mnt,
2856 .dentry = dentry,
2857 };
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03002858 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
2860 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002861 BUG_ON(!nfsd_attrs_supported(minorversion, bmval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002863 if (exp->ex_fslocs.migrated) {
Kinglong Meec2227a32015-07-07 10:16:37 +08002864 status = fattr_handle_absent_fs(&bmval0, &bmval1, &bmval2, &rdattr_err);
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002865 if (status)
2866 goto out;
2867 }
2868
David Howellsa528d352017-01-31 16:46:22 +00002869 err = vfs_getattr(&path, &stat, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
Al Virob8dd7b92006-10-19 23:29:01 -07002870 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 goto out_nfserr;
Christoph Hellwig12337902014-05-28 10:46:13 +02002872 if ((bmval0 & (FATTR4_WORD0_FILES_AVAIL | FATTR4_WORD0_FILES_FREE |
2873 FATTR4_WORD0_FILES_TOTAL | FATTR4_WORD0_MAXNAME)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2875 FATTR4_WORD1_SPACE_TOTAL))) {
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002876 err = vfs_statfs(&path, &statfs);
Al Virob8dd7b92006-10-19 23:29:01 -07002877 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 goto out_nfserr;
2879 }
2880 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002881 tempfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
2882 status = nfserr_jukebox;
2883 if (!tempfh)
2884 goto out;
2885 fh_init(tempfh, NFS4_FHSIZE);
2886 status = fh_compose(tempfh, exp, dentry, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 if (status)
2888 goto out;
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002889 fhp = tempfh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 }
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02002891 if (bmval0 & FATTR4_WORD0_ACL) {
Al Virob8dd7b92006-10-19 23:29:01 -07002892 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02002893 if (err == -EOPNOTSUPP)
2894 bmval0 &= ~FATTR4_WORD0_ACL;
2895 else if (err == -EINVAL) {
2896 status = nfserr_attrnotsupp;
2897 goto out;
2898 } else if (err != 0)
2899 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
David Quigley18032ca2013-05-02 13:19:10 -04002902#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
Kinglong Meec2227a32015-07-07 10:16:37 +08002903 if ((bmval2 & FATTR4_WORD2_SECURITY_LABEL) ||
2904 bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
J. Bruce Fields32ddd942017-01-03 12:30:11 -05002905 if (exp->ex_flags & NFSEXP_SECURITY_LABEL)
2906 err = security_inode_getsecctx(d_inode(dentry),
David Quigley18032ca2013-05-02 13:19:10 -04002907 &context, &contextlen);
J. Bruce Fields32ddd942017-01-03 12:30:11 -05002908 else
2909 err = -EOPNOTSUPP;
David Quigley18032ca2013-05-02 13:19:10 -04002910 contextsupport = (err == 0);
2911 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2912 if (err == -EOPNOTSUPP)
2913 bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2914 else if (err)
2915 goto out_nfserr;
2916 }
2917 }
2918#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2919
Kinglong Mee75976de2015-07-30 21:55:30 +08002920 status = nfsd4_encode_bitmap(xdr, bmval0, bmval1, bmval2);
2921 if (status)
2922 goto out;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002923
2924 attrlen_offset = xdr->buf->len;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002925 p = xdr_reserve_space(xdr, 4);
2926 if (!p)
2927 goto out_resource;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002928 p++; /* to be backfilled later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
2930 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
J. Bruce Fieldsdcd208692017-01-11 20:34:50 -05002931 u32 supp[3];
2932
2933 memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
Andy Adamson7e705702009-04-03 08:29:11 +03002934
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02002935 if (!IS_POSIXACL(dentry->d_inode))
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002936 supp[0] &= ~FATTR4_WORD0_ACL;
David Quigley18032ca2013-05-02 13:19:10 -04002937 if (!contextsupport)
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002938 supp[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2939 if (!supp[2]) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002940 p = xdr_reserve_space(xdr, 12);
2941 if (!p)
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002942 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002943 *p++ = cpu_to_be32(2);
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002944 *p++ = cpu_to_be32(supp[0]);
2945 *p++ = cpu_to_be32(supp[1]);
Andy Adamson7e705702009-04-03 08:29:11 +03002946 } else {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002947 p = xdr_reserve_space(xdr, 16);
2948 if (!p)
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002949 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002950 *p++ = cpu_to_be32(3);
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002951 *p++ = cpu_to_be32(supp[0]);
2952 *p++ = cpu_to_be32(supp[1]);
2953 *p++ = cpu_to_be32(supp[2]);
Andy Adamson7e705702009-04-03 08:29:11 +03002954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 }
2956 if (bmval0 & FATTR4_WORD0_TYPE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002957 p = xdr_reserve_space(xdr, 4);
2958 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 goto out_resource;
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002960 dummy = nfs4_file_type(stat.mode);
J. Bruce Fields6b6d81372013-01-16 17:11:11 -05002961 if (dummy == NF4BAD) {
2962 status = nfserr_serverfault;
2963 goto out;
2964 }
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002965 *p++ = cpu_to_be32(dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 }
2967 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002968 p = xdr_reserve_space(xdr, 4);
2969 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 goto out_resource;
NeilBrown49640002005-06-23 22:02:58 -07002971 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002972 *p++ = cpu_to_be32(NFS4_FH_PERSISTENT);
NeilBrown49640002005-06-23 22:02:58 -07002973 else
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002974 *p++ = cpu_to_be32(NFS4_FH_PERSISTENT|
2975 NFS4_FH_VOL_RENAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 }
2977 if (bmval0 & FATTR4_WORD0_CHANGE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002978 p = xdr_reserve_space(xdr, 8);
2979 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 goto out_resource;
NeilBrownb8800922017-01-30 17:17:00 +11002981 p = encode_change(p, &stat, d_inode(dentry), exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 }
2983 if (bmval0 & FATTR4_WORD0_SIZE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002984 p = xdr_reserve_space(xdr, 8);
2985 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002987 p = xdr_encode_hyper(p, stat.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 }
2989 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002990 p = xdr_reserve_space(xdr, 4);
2991 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002993 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 }
2995 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002996 p = xdr_reserve_space(xdr, 4);
2997 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002999 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 }
3001 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003002 p = xdr_reserve_space(xdr, 4);
3003 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003005 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 }
3007 if (bmval0 & FATTR4_WORD0_FSID) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003008 p = xdr_reserve_space(xdr, 16);
3009 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07003011 if (exp->ex_fslocs.migrated) {
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003012 p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MAJOR);
3013 p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MINOR);
NeilBrownaf6a4e22007-02-14 00:33:12 -08003014 } else switch(fsid_source(fhp)) {
3015 case FSIDSOURCE_FSID:
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003016 p = xdr_encode_hyper(p, (u64)exp->ex_fsid);
3017 p = xdr_encode_hyper(p, (u64)0);
NeilBrownaf6a4e22007-02-14 00:33:12 -08003018 break;
3019 case FSIDSOURCE_DEV:
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003020 *p++ = cpu_to_be32(0);
3021 *p++ = cpu_to_be32(MAJOR(stat.dev));
3022 *p++ = cpu_to_be32(0);
3023 *p++ = cpu_to_be32(MINOR(stat.dev));
NeilBrownaf6a4e22007-02-14 00:33:12 -08003024 break;
3025 case FSIDSOURCE_UUID:
Kinglong Mee94eb3682014-05-23 20:00:19 +08003026 p = xdr_encode_opaque_fixed(p, exp->ex_uuid,
3027 EX_UUID_LEN);
NeilBrownaf6a4e22007-02-14 00:33:12 -08003028 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 }
3030 }
3031 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003032 p = xdr_reserve_space(xdr, 4);
3033 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003035 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 }
3037 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003038 p = xdr_reserve_space(xdr, 4);
3039 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003041 *p++ = cpu_to_be32(nn->nfsd4_lease);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 }
3043 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003044 p = xdr_reserve_space(xdr, 4);
3045 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003047 *p++ = cpu_to_be32(rdattr_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 }
3049 if (bmval0 & FATTR4_WORD0_ACL) {
3050 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
3052 if (acl == NULL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003053 p = xdr_reserve_space(xdr, 4);
3054 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 goto out_resource;
3056
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003057 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 goto out_acl;
3059 }
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003060 p = xdr_reserve_space(xdr, 4);
3061 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003063 *p++ = cpu_to_be32(acl->naces);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08003065 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003066 p = xdr_reserve_space(xdr, 4*3);
3067 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003069 *p++ = cpu_to_be32(ace->type);
3070 *p++ = cpu_to_be32(ace->flag);
3071 *p++ = cpu_to_be32(ace->access_mask &
3072 NFS4_ACE_MASK_ALL);
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003073 status = nfsd4_encode_aclname(xdr, rqstp, ace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 if (status)
3075 goto out;
3076 }
3077 }
3078out_acl:
3079 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003080 p = xdr_reserve_space(xdr, 4);
3081 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 goto out_resource;
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02003083 *p++ = cpu_to_be32(IS_POSIXACL(dentry->d_inode) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
3085 }
3086 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003087 p = xdr_reserve_space(xdr, 4);
3088 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003090 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 }
3092 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003093 p = xdr_reserve_space(xdr, 4);
3094 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003096 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 }
3098 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003099 p = xdr_reserve_space(xdr, 4);
3100 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003102 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 }
3104 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003105 p = xdr_reserve_space(xdr, 4);
3106 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003108 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 }
3110 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003111 p = xdr_reserve_space(xdr, fhp->fh_handle.fh_size + 4);
3112 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 goto out_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003114 p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base,
3115 fhp->fh_handle.fh_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 }
3117 if (bmval0 & FATTR4_WORD0_FILEID) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003118 p = xdr_reserve_space(xdr, 8);
3119 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003121 p = xdr_encode_hyper(p, stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 }
3123 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003124 p = xdr_reserve_space(xdr, 8);
3125 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003127 p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 }
3129 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003130 p = xdr_reserve_space(xdr, 8);
3131 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003133 p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 }
3135 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003136 p = xdr_reserve_space(xdr, 8);
3137 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003139 p = xdr_encode_hyper(p, (u64) statfs.f_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07003141 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003142 status = nfsd4_encode_fs_locations(xdr, rqstp, exp);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07003143 if (status)
3144 goto out;
3145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003147 p = xdr_reserve_space(xdr, 4);
3148 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003150 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 }
3152 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003153 p = xdr_reserve_space(xdr, 8);
3154 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003156 p = xdr_encode_hyper(p, exp->ex_path.mnt->mnt_sb->s_maxbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 }
3158 if (bmval0 & FATTR4_WORD0_MAXLINK) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003159 p = xdr_reserve_space(xdr, 4);
3160 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003162 *p++ = cpu_to_be32(255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 }
3164 if (bmval0 & FATTR4_WORD0_MAXNAME) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003165 p = xdr_reserve_space(xdr, 4);
3166 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003168 *p++ = cpu_to_be32(statfs.f_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 }
3170 if (bmval0 & FATTR4_WORD0_MAXREAD) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003171 p = xdr_reserve_space(xdr, 8);
3172 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003174 p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 }
3176 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003177 p = xdr_reserve_space(xdr, 8);
3178 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003180 p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 }
3182 if (bmval1 & FATTR4_WORD1_MODE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003183 p = xdr_reserve_space(xdr, 4);
3184 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003186 *p++ = cpu_to_be32(stat.mode & S_IALLUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 }
3188 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003189 p = xdr_reserve_space(xdr, 4);
3190 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003192 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 }
3194 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003195 p = xdr_reserve_space(xdr, 4);
3196 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003198 *p++ = cpu_to_be32(stat.nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 }
3200 if (bmval1 & FATTR4_WORD1_OWNER) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003201 status = nfsd4_encode_user(xdr, rqstp, stat.uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 if (status)
3203 goto out;
3204 }
3205 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003206 status = nfsd4_encode_group(xdr, rqstp, stat.gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 if (status)
3208 goto out;
3209 }
3210 if (bmval1 & FATTR4_WORD1_RAWDEV) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003211 p = xdr_reserve_space(xdr, 8);
3212 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003214 *p++ = cpu_to_be32((u32) MAJOR(stat.rdev));
3215 *p++ = cpu_to_be32((u32) MINOR(stat.rdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 }
3217 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003218 p = xdr_reserve_space(xdr, 8);
3219 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 goto out_resource;
3221 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003222 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 }
3224 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003225 p = xdr_reserve_space(xdr, 8);
3226 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 goto out_resource;
3228 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003229 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 }
3231 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003232 p = xdr_reserve_space(xdr, 8);
3233 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 goto out_resource;
3235 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003236 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 }
3238 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003239 p = xdr_reserve_space(xdr, 8);
3240 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 goto out_resource;
3242 dummy64 = (u64)stat.blocks << 9;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003243 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 }
3245 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003246 p = xdr_reserve_space(xdr, 12);
3247 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003249 p = xdr_encode_hyper(p, (s64)stat.atime.tv_sec);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003250 *p++ = cpu_to_be32(stat.atime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 }
3252 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003253 p = xdr_reserve_space(xdr, 12);
3254 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 goto out_resource;
J. Bruce Fields16945142018-04-25 14:34:11 -04003256 p = encode_time_delta(p, d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 }
3258 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003259 p = xdr_reserve_space(xdr, 12);
3260 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003262 p = xdr_encode_hyper(p, (s64)stat.ctime.tv_sec);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003263 *p++ = cpu_to_be32(stat.ctime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 }
3265 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003266 p = xdr_reserve_space(xdr, 12);
3267 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003269 p = xdr_encode_hyper(p, (s64)stat.mtime.tv_sec);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003270 *p++ = cpu_to_be32(stat.mtime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 }
3272 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
Kinglong Mee0a2050d2015-07-30 21:51:54 +08003273 struct kstat parent_stat;
3274 u64 ino = stat.ino;
3275
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003276 p = xdr_reserve_space(xdr, 8);
3277 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 goto out_resource;
Frank Filz406a7ea2007-11-27 11:34:05 -08003279 /*
3280 * Get parent's attributes if not ignoring crossmount
3281 * and this is the root of a cross-mounted filesystem.
3282 */
3283 if (ignore_crossmnt == 0 &&
Kinglong Mee0a2050d2015-07-30 21:51:54 +08003284 dentry == exp->ex_path.mnt->mnt_root) {
3285 err = get_parent_attributes(exp, &parent_stat);
3286 if (err)
3287 goto out_nfserr;
3288 ino = parent_stat.ino;
3289 }
3290 p = xdr_encode_hyper(p, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003292#ifdef CONFIG_NFSD_PNFS
Kinglong Mee6896f152015-07-30 21:52:44 +08003293 if (bmval1 & FATTR4_WORD1_FS_LAYOUT_TYPES) {
Jeff Layton8a4c3922016-07-10 15:55:58 -04003294 status = nfsd4_encode_layout_types(xdr, exp->ex_layout_types);
Kinglong Mee6896f152015-07-30 21:52:44 +08003295 if (status)
3296 goto out;
3297 }
3298
3299 if (bmval2 & FATTR4_WORD2_LAYOUT_TYPES) {
Jeff Layton8a4c3922016-07-10 15:55:58 -04003300 status = nfsd4_encode_layout_types(xdr, exp->ex_layout_types);
Kinglong Mee6896f152015-07-30 21:52:44 +08003301 if (status)
3302 goto out;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003303 }
3304
3305 if (bmval2 & FATTR4_WORD2_LAYOUT_BLKSIZE) {
3306 p = xdr_reserve_space(xdr, 4);
3307 if (!p)
3308 goto out_resource;
3309 *p++ = cpu_to_be32(stat.blksize);
3310 }
3311#endif /* CONFIG_NFSD_PNFS */
Benny Halevy8c18f202009-04-03 08:29:14 +03003312 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
Trond Myklebustb26b78c2017-05-09 16:24:59 -04003313 u32 supp[3];
3314
3315 memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
3316 supp[0] &= NFSD_SUPPATTR_EXCLCREAT_WORD0;
3317 supp[1] &= NFSD_SUPPATTR_EXCLCREAT_WORD1;
3318 supp[2] &= NFSD_SUPPATTR_EXCLCREAT_WORD2;
3319
3320 status = nfsd4_encode_bitmap(xdr, supp[0], supp[1], supp[2]);
Kinglong Mee75976de2015-07-30 21:55:30 +08003321 if (status)
3322 goto out;
Benny Halevy8c18f202009-04-03 08:29:14 +03003323 }
Andy Adamson7e705702009-04-03 08:29:11 +03003324
J. Bruce Fieldsa8585762018-04-25 13:26:23 -04003325 if (bmval2 & FATTR4_WORD2_CHANGE_ATTR_TYPE) {
3326 p = xdr_reserve_space(xdr, 4);
3327 if (!p)
3328 goto out_resource;
3329 if (IS_I_VERSION(d_inode(dentry)))
3330 *p++ = cpu_to_be32(NFS4_CHANGE_TYPE_IS_MONOTONIC_INCR);
3331 else
3332 *p++ = cpu_to_be32(NFS4_CHANGE_TYPE_IS_TIME_METADATA);
3333 }
3334
Arnd Bergmann0ab88ca2019-03-22 15:07:11 +01003335#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
Kinglong Mee7d580722015-07-30 21:54:26 +08003336 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
3337 status = nfsd4_encode_security_label(xdr, rqstp, context,
3338 contextlen);
3339 if (status)
3340 goto out;
3341 }
Arnd Bergmann0ab88ca2019-03-22 15:07:11 +01003342#endif
Kinglong Mee7d580722015-07-30 21:54:26 +08003343
Frank van der Linden0e885e82020-06-23 22:39:27 +00003344 if (bmval2 & FATTR4_WORD2_XATTR_SUPPORT) {
3345 p = xdr_reserve_space(xdr, 4);
3346 if (!p)
3347 goto out_resource;
3348 err = xattr_supported_namespace(d_inode(dentry),
3349 XATTR_USER_PREFIX);
3350 *p++ = cpu_to_be32(err == 0);
3351 }
3352
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04003353 attrlen = htonl(xdr->buf->len - attrlen_offset - 4);
3354 write_bytes_to_xdr_buf(xdr->buf, attrlen_offset, &attrlen, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 status = nfs_ok;
3356
3357out:
J. Bruce Fieldsba4e55b2013-05-15 10:27:52 -04003358#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
David Quigley18032ca2013-05-02 13:19:10 -04003359 if (context)
3360 security_release_secctx(context, contextlen);
J. Bruce Fieldsba4e55b2013-05-15 10:27:52 -04003361#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08003362 kfree(acl);
Yan, Zheng18df11d02014-03-10 12:52:07 +08003363 if (tempfh) {
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05003364 fh_put(tempfh);
Yan, Zheng18df11d02014-03-10 12:52:07 +08003365 kfree(tempfh);
3366 }
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05003367 if (status)
3368 xdr_truncate_encode(xdr, starting_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 return status;
3370out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07003371 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 goto out;
3373out_resource:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 status = nfserr_resource;
3375 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376}
3377
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04003378static void svcxdr_init_encode_from_buffer(struct xdr_stream *xdr,
3379 struct xdr_buf *buf, __be32 *p, int bytes)
3380{
3381 xdr->scratch.iov_len = 0;
3382 memset(buf, 0, sizeof(struct xdr_buf));
3383 buf->head[0].iov_base = p;
3384 buf->head[0].iov_len = 0;
3385 buf->len = 0;
3386 xdr->buf = buf;
3387 xdr->iov = buf->head;
3388 xdr->p = p;
3389 xdr->end = (void *)p + bytes;
3390 buf->buflen = bytes;
3391}
3392
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04003393__be32 nfsd4_encode_fattr_to_buf(__be32 **p, int words,
3394 struct svc_fh *fhp, struct svc_export *exp,
3395 struct dentry *dentry, u32 *bmval,
3396 struct svc_rqst *rqstp, int ignore_crossmnt)
3397{
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04003398 struct xdr_buf dummy;
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04003399 struct xdr_stream xdr;
3400 __be32 ret;
3401
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04003402 svcxdr_init_encode_from_buffer(&xdr, &dummy, *p, words << 2);
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04003403 ret = nfsd4_encode_fattr(&xdr, fhp, exp, dentry, bmval, rqstp,
3404 ignore_crossmnt);
3405 *p = xdr.p;
3406 return ret;
3407}
3408
J. Bruce Fieldsc0ce6ec2008-02-11 15:48:47 -05003409static inline int attributes_need_mount(u32 *bmval)
3410{
3411 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
3412 return 1;
3413 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
3414 return 1;
3415 return 0;
3416}
3417
Al Virob37ad282006-10-19 23:28:59 -07003418static __be32
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003419nfsd4_encode_dirent_fattr(struct xdr_stream *xdr, struct nfsd4_readdir *cd,
3420 const char *name, int namlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421{
3422 struct svc_export *exp = cd->rd_fhp->fh_export;
3423 struct dentry *dentry;
Al Virob37ad282006-10-19 23:28:59 -07003424 __be32 nfserr;
Frank Filz406a7ea2007-11-27 11:34:05 -08003425 int ignore_crossmnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
Al Viro6c2d47982019-10-31 01:21:58 -04003427 dentry = lookup_positive_unlocked(name, cd->rd_fhp->fh_dentry, namlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 if (IS_ERR(dentry))
3429 return nfserrno(PTR_ERR(dentry));
3430
3431 exp_get(exp);
Frank Filz406a7ea2007-11-27 11:34:05 -08003432 /*
3433 * In the case of a mountpoint, the client may be asking for
3434 * attributes that are only properties of the underlying filesystem
3435 * as opposed to the cross-mounted file system. In such a case,
3436 * we will not follow the cross mount and will fill the attribtutes
3437 * directly from the mountpoint dentry.
3438 */
J. Bruce Fields3227fa42009-10-25 21:43:01 -04003439 if (nfsd_mountpoint(dentry, exp)) {
J.Bruce Fields021d3a72006-12-13 00:35:24 -08003440 int err;
3441
J. Bruce Fields3227fa42009-10-25 21:43:01 -04003442 if (!(exp->ex_flags & NFSEXP_V4ROOT)
3443 && !attributes_need_mount(cd->rd_bmval)) {
3444 ignore_crossmnt = 1;
3445 goto out_encode;
3446 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07003447 /*
3448 * Why the heck aren't we just using nfsd_lookup??
3449 * Different "."/".." handling? Something else?
3450 * At least, add a comment here to explain....
3451 */
J.Bruce Fields021d3a72006-12-13 00:35:24 -08003452 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
3453 if (err) {
3454 nfserr = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 goto out_put;
3456 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07003457 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
3458 if (nfserr)
3459 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
3461 }
J. Bruce Fields3227fa42009-10-25 21:43:01 -04003462out_encode:
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003463 nfserr = nfsd4_encode_fattr(xdr, NULL, exp, dentry, cd->rd_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08003464 cd->rd_rqstp, ignore_crossmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465out_put:
3466 dput(dentry);
3467 exp_put(exp);
3468 return nfserr;
3469}
3470
Al Viro2ebbc012006-10-19 23:28:58 -07003471static __be32 *
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003472nfsd4_encode_rdattr_error(struct xdr_stream *xdr, __be32 nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473{
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003474 __be32 *p;
3475
Kinglong Meec3a45612014-07-06 11:34:43 +08003476 p = xdr_reserve_space(xdr, 20);
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003477 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 return NULL;
3479 *p++ = htonl(2);
3480 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
3481 *p++ = htonl(0); /* bmval1 */
3482
J. Bruce Fields87915c62013-01-16 17:33:28 -05003483 *p++ = htonl(4); /* attribute length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 *p++ = nfserr; /* no htonl */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 return p;
3486}
3487
3488static int
NeilBrowna0ad13e2007-01-26 00:57:10 -08003489nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
3490 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491{
NeilBrowna0ad13e2007-01-26 00:57:10 -08003492 struct readdir_cd *ccd = ccdv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003494 struct xdr_stream *xdr = cd->xdr;
3495 int start_offset = xdr->buf->len;
3496 int cookie_offset;
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04003497 u32 name_and_cookie;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003498 int entry_bytes;
Al Virob37ad282006-10-19 23:28:59 -07003499 __be32 nfserr = nfserr_toosmall;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003500 __be64 wire_offset;
3501 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502
3503 /* In nfsv4, "." and ".." never make it onto the wire.. */
3504 if (name && isdotent(name, namlen)) {
3505 cd->common.err = nfs_ok;
3506 return 0;
3507 }
3508
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003509 if (cd->cookie_offset) {
3510 wire_offset = cpu_to_be64(offset);
3511 write_bytes_to_xdr_buf(xdr->buf, cd->cookie_offset,
3512 &wire_offset, 8);
3513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003515 p = xdr_reserve_space(xdr, 4);
3516 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 *p++ = xdr_one; /* mark entry present */
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003519 cookie_offset = xdr->buf->len;
3520 p = xdr_reserve_space(xdr, 3*4 + namlen);
3521 if (!p)
3522 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
3524 p = xdr_encode_array(p, name, namlen); /* name length & name */
3525
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003526 nfserr = nfsd4_encode_dirent_fattr(xdr, cd, name, namlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 switch (nfserr) {
3528 case nfs_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 break;
3530 case nfserr_resource:
3531 nfserr = nfserr_toosmall;
3532 goto fail;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04003533 case nfserr_noent:
Kinglong Meef41c5ad2014-06-13 17:32:42 +08003534 xdr_truncate_encode(xdr, start_offset);
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04003535 goto skip_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 default:
3537 /*
3538 * If the client requested the RDATTR_ERROR attribute,
3539 * we stuff the error code into this attribute
3540 * and continue. If this attribute was not requested,
3541 * then in accordance with the spec, we fail the
3542 * entire READDIR operation(!)
3543 */
3544 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
3545 goto fail;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003546 p = nfsd4_encode_rdattr_error(xdr, nfserr);
Fred Isaman34081ef2006-01-18 17:43:40 -08003547 if (p == NULL) {
3548 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 goto fail;
Fred Isaman34081ef2006-01-18 17:43:40 -08003550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 }
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003552 nfserr = nfserr_toosmall;
3553 entry_bytes = xdr->buf->len - start_offset;
3554 if (entry_bytes > cd->rd_maxcount)
3555 goto fail;
3556 cd->rd_maxcount -= entry_bytes;
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04003557 /*
3558 * RFC 3530 14.2.24 describes rd_dircount as only a "hint", so
3559 * let's always let through the first entry, at least:
3560 */
J. Bruce Fields0ec016e2014-12-19 18:01:35 -05003561 if (!cd->rd_dircount)
3562 goto fail;
3563 name_and_cookie = 4 + 4 * XDR_QUADLEN(namlen) + 8;
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04003564 if (name_and_cookie > cd->rd_dircount && cd->cookie_offset)
3565 goto fail;
3566 cd->rd_dircount -= min(cd->rd_dircount, name_and_cookie);
J. Bruce Fields0ec016e2014-12-19 18:01:35 -05003567
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003568 cd->cookie_offset = cookie_offset;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04003569skip_entry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 cd->common.err = nfs_ok;
3571 return 0;
3572fail:
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003573 xdr_truncate_encode(xdr, start_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 cd->common.err = nfserr;
3575 return -EINVAL;
3576}
3577
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003578static __be32
3579nfsd4_encode_stateid(struct xdr_stream *xdr, stateid_t *sid)
Benny Halevye2f282b2008-08-12 20:45:07 +03003580{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003581 __be32 *p;
Benny Halevye2f282b2008-08-12 20:45:07 +03003582
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003583 p = xdr_reserve_space(xdr, sizeof(stateid_t));
3584 if (!p)
3585 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003586 *p++ = cpu_to_be32(sid->si_generation);
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003587 p = xdr_encode_opaque_fixed(p, &sid->si_opaque,
3588 sizeof(stateid_opaque_t));
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003589 return 0;
Benny Halevye2f282b2008-08-12 20:45:07 +03003590}
3591
Benny Halevy695e12f2008-07-04 14:38:33 +03003592static __be32
Al Virob37ad282006-10-19 23:28:59 -07003593nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003595 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003596 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003598 p = xdr_reserve_space(xdr, 8);
3599 if (!p)
3600 return nfserr_resource;
3601 *p++ = cpu_to_be32(access->ac_supported);
3602 *p++ = cpu_to_be32(access->ac_resp_access);
3603 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604}
3605
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003606static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
3607{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003608 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003609 __be32 *p;
3610
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003611 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 8);
3612 if (!p)
3613 return nfserr_resource;
3614 p = xdr_encode_opaque_fixed(p, bcts->sessionid.data,
3615 NFS4_MAX_SESSIONID_LEN);
3616 *p++ = cpu_to_be32(bcts->dir);
3617 /* Upshifting from TCP to RDMA is not supported */
3618 *p++ = cpu_to_be32(0);
3619 return 0;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003620}
3621
Benny Halevy695e12f2008-07-04 14:38:33 +03003622static __be32
Al Virob37ad282006-10-19 23:28:59 -07003623nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003625 struct xdr_stream *xdr = &resp->xdr;
3626
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003627 return nfsd4_encode_stateid(xdr, &close->cl_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628}
3629
3630
Benny Halevy695e12f2008-07-04 14:38:33 +03003631static __be32
Al Virob37ad282006-10-19 23:28:59 -07003632nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003634 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003635 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003637 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
3638 if (!p)
3639 return nfserr_resource;
3640 p = xdr_encode_opaque_fixed(p, commit->co_verf.data,
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003641 NFS4_VERIFIER_SIZE);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003642 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643}
3644
Benny Halevy695e12f2008-07-04 14:38:33 +03003645static __be32
Al Virob37ad282006-10-19 23:28:59 -07003646nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003648 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003649 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003651 p = xdr_reserve_space(xdr, 20);
3652 if (!p)
3653 return nfserr_resource;
3654 encode_cinfo(p, &create->cr_cinfo);
Trond Myklebustb96811c2019-08-18 14:18:58 -04003655 return nfsd4_encode_bitmap(xdr, create->cr_bmval[0],
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003656 create->cr_bmval[1], create->cr_bmval[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657}
3658
Al Virob37ad282006-10-19 23:28:59 -07003659static __be32
3660nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661{
3662 struct svc_fh *fhp = getattr->ga_fhp;
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04003663 struct xdr_stream *xdr = &resp->xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003665 return nfsd4_encode_fattr(xdr, fhp, fhp->fh_export, fhp->fh_dentry,
3666 getattr->ga_bmval, resp->rqstp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667}
3668
Benny Halevy695e12f2008-07-04 14:38:33 +03003669static __be32
3670nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003672 struct xdr_stream *xdr = &resp->xdr;
Benny Halevy695e12f2008-07-04 14:38:33 +03003673 struct svc_fh *fhp = *fhpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 unsigned int len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003675 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003677 len = fhp->fh_handle.fh_size;
3678 p = xdr_reserve_space(xdr, len + 4);
3679 if (!p)
3680 return nfserr_resource;
3681 p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base, len);
3682 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683}
3684
3685/*
3686* Including all fields other than the name, a LOCK4denied structure requires
3687* 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
3688*/
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003689static __be32
3690nfsd4_encode_lock_denied(struct xdr_stream *xdr, struct nfsd4_lock_denied *ld)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691{
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003692 struct xdr_netobj *conf = &ld->ld_owner;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003693 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003695again:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003696 p = xdr_reserve_space(xdr, 32 + XDR_LEN(conf->len));
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003697 if (!p) {
3698 /*
3699 * Don't fail to return the result just because we can't
3700 * return the conflicting open:
3701 */
3702 if (conf->len) {
Kinglong Meef98bac52014-07-07 22:10:56 +08003703 kfree(conf->data);
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003704 conf->len = 0;
3705 conf->data = NULL;
3706 goto again;
3707 }
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003708 return nfserr_resource;
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003709 }
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003710 p = xdr_encode_hyper(p, ld->ld_start);
3711 p = xdr_encode_hyper(p, ld->ld_length);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003712 *p++ = cpu_to_be32(ld->ld_type);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003713 if (conf->len) {
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003714 p = xdr_encode_opaque_fixed(p, &ld->ld_clientid, 8);
3715 p = xdr_encode_opaque(p, conf->data, conf->len);
Kinglong Meef98bac52014-07-07 22:10:56 +08003716 kfree(conf->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003718 p = xdr_encode_hyper(p, (u64)0); /* clientid */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003719 *p++ = cpu_to_be32(0); /* length of owner name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 }
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003721 return nfserr_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722}
3723
Benny Halevy695e12f2008-07-04 14:38:33 +03003724static __be32
Al Virob37ad282006-10-19 23:28:59 -07003725nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003727 struct xdr_stream *xdr = &resp->xdr;
3728
Benny Halevye2f282b2008-08-12 20:45:07 +03003729 if (!nfserr)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003730 nfserr = nfsd4_encode_stateid(xdr, &lock->lk_resp_stateid);
Benny Halevye2f282b2008-08-12 20:45:07 +03003731 else if (nfserr == nfserr_denied)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003732 nfserr = nfsd4_encode_lock_denied(xdr, &lock->lk_denied);
Kinglong Meef98bac52014-07-07 22:10:56 +08003733
Benny Halevy695e12f2008-07-04 14:38:33 +03003734 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735}
3736
Benny Halevy695e12f2008-07-04 14:38:33 +03003737static __be32
Al Virob37ad282006-10-19 23:28:59 -07003738nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003740 struct xdr_stream *xdr = &resp->xdr;
3741
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 if (nfserr == nfserr_denied)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003743 nfsd4_encode_lock_denied(xdr, &lockt->lt_denied);
Benny Halevy695e12f2008-07-04 14:38:33 +03003744 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745}
3746
Benny Halevy695e12f2008-07-04 14:38:33 +03003747static __be32
Al Virob37ad282006-10-19 23:28:59 -07003748nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003750 struct xdr_stream *xdr = &resp->xdr;
3751
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003752 return nfsd4_encode_stateid(xdr, &locku->lu_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753}
3754
3755
Benny Halevy695e12f2008-07-04 14:38:33 +03003756static __be32
Al Virob37ad282006-10-19 23:28:59 -07003757nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003759 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003760 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003762 p = xdr_reserve_space(xdr, 20);
3763 if (!p)
3764 return nfserr_resource;
3765 p = encode_cinfo(p, &link->li_cinfo);
3766 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767}
3768
3769
Benny Halevy695e12f2008-07-04 14:38:33 +03003770static __be32
Al Virob37ad282006-10-19 23:28:59 -07003771nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003773 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003774 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003776 nfserr = nfsd4_encode_stateid(xdr, &open->op_stateid);
3777 if (nfserr)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003778 return nfserr;
Kinglong Mee75976de2015-07-30 21:55:30 +08003779 p = xdr_reserve_space(xdr, 24);
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003780 if (!p)
3781 return nfserr_resource;
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04003782 p = encode_cinfo(p, &open->op_cinfo);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003783 *p++ = cpu_to_be32(open->op_rflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784
Kinglong Mee75976de2015-07-30 21:55:30 +08003785 nfserr = nfsd4_encode_bitmap(xdr, open->op_bmval[0], open->op_bmval[1],
3786 open->op_bmval[2]);
3787 if (nfserr)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003788 return nfserr;
Kinglong Mee75976de2015-07-30 21:55:30 +08003789
3790 p = xdr_reserve_space(xdr, 4);
3791 if (!p)
3792 return nfserr_resource;
3793
3794 *p++ = cpu_to_be32(open->op_delegate_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 switch (open->op_delegate_type) {
3796 case NFS4_OPEN_DELEGATE_NONE:
3797 break;
3798 case NFS4_OPEN_DELEGATE_READ:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003799 nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
3800 if (nfserr)
3801 return nfserr;
3802 p = xdr_reserve_space(xdr, 20);
3803 if (!p)
3804 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003805 *p++ = cpu_to_be32(open->op_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806
3807 /*
3808 * TODO: ACE's in delegations
3809 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003810 *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
3811 *p++ = cpu_to_be32(0);
3812 *p++ = cpu_to_be32(0);
3813 *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 break;
3815 case NFS4_OPEN_DELEGATE_WRITE:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003816 nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
3817 if (nfserr)
3818 return nfserr;
3819 p = xdr_reserve_space(xdr, 32);
3820 if (!p)
3821 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003822 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823
3824 /*
3825 * TODO: space_limit's in delegations
3826 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003827 *p++ = cpu_to_be32(NFS4_LIMIT_SIZE);
3828 *p++ = cpu_to_be32(~(u32)0);
3829 *p++ = cpu_to_be32(~(u32)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830
3831 /*
3832 * TODO: ACE's in delegations
3833 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003834 *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
3835 *p++ = cpu_to_be32(0);
3836 *p++ = cpu_to_be32(0);
3837 *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 break;
Benny Halevyd24433c2012-02-16 20:57:17 +02003839 case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
3840 switch (open->op_why_no_deleg) {
3841 case WND4_CONTENTION:
3842 case WND4_RESOURCE:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003843 p = xdr_reserve_space(xdr, 8);
3844 if (!p)
3845 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003846 *p++ = cpu_to_be32(open->op_why_no_deleg);
3847 /* deleg signaling not supported yet: */
3848 *p++ = cpu_to_be32(0);
Benny Halevyd24433c2012-02-16 20:57:17 +02003849 break;
3850 default:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003851 p = xdr_reserve_space(xdr, 4);
3852 if (!p)
3853 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003854 *p++ = cpu_to_be32(open->op_why_no_deleg);
Benny Halevyd24433c2012-02-16 20:57:17 +02003855 }
Benny Halevyd24433c2012-02-16 20:57:17 +02003856 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 default:
3858 BUG();
3859 }
3860 /* XXX save filehandle here */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003861 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862}
3863
Benny Halevy695e12f2008-07-04 14:38:33 +03003864static __be32
Al Virob37ad282006-10-19 23:28:59 -07003865nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003867 struct xdr_stream *xdr = &resp->xdr;
3868
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003869 return nfsd4_encode_stateid(xdr, &oc->oc_resp_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870}
3871
Benny Halevy695e12f2008-07-04 14:38:33 +03003872static __be32
Al Virob37ad282006-10-19 23:28:59 -07003873nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003875 struct xdr_stream *xdr = &resp->xdr;
3876
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003877 return nfsd4_encode_stateid(xdr, &od->od_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878}
3879
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003880static __be32 nfsd4_encode_splice_read(
3881 struct nfsd4_compoundres *resp,
3882 struct nfsd4_read *read,
3883 struct file *file, unsigned long maxcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884{
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003885 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003886 struct xdr_buf *buf = xdr->buf;
Chuck Lever76e54922020-11-05 10:24:19 -05003887 int status, space_left;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003888 u32 eof;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003889 __be32 nfserr;
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003890 __be32 *p = xdr->p - 2;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003891
Kinglong Meed5d5c302014-07-09 21:51:27 +08003892 /* Make sure there will be room for padding if needed */
3893 if (xdr->end - xdr->p < 1)
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003894 return nfserr_resource;
3895
Chuck Lever87c59422018-03-28 13:29:11 -04003896 nfserr = nfsd_splice_read(read->rd_rqstp, read->rd_fhp,
Trond Myklebust83a63072019-08-26 13:03:11 -04003897 file, read->rd_offset, &maxcount, &eof);
Chuck Lever87c59422018-03-28 13:29:11 -04003898 read->rd_length = maxcount;
Chuck Lever76e54922020-11-05 10:24:19 -05003899 if (nfserr)
3900 goto out_err;
3901 status = svc_encode_result_payload(read->rd_rqstp,
3902 buf->head[0].iov_len, maxcount);
3903 if (status) {
3904 nfserr = nfserrno(status);
3905 goto out_err;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003906 }
3907
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003908 *(p++) = htonl(eof);
3909 *(p++) = htonl(maxcount);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003910
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003911 buf->page_len = maxcount;
3912 buf->len += maxcount;
J. Bruce Fields15b23ef2014-09-24 16:32:34 -04003913 xdr->page_ptr += (buf->page_base + maxcount + PAGE_SIZE - 1)
3914 / PAGE_SIZE;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003915
3916 /* Use rest of head for padding and remaining ops: */
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003917 buf->tail[0].iov_base = xdr->p;
3918 buf->tail[0].iov_len = 0;
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003919 xdr->iov = buf->tail;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003920 if (maxcount&3) {
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003921 int pad = 4 - (maxcount&3);
3922
3923 *(xdr->p++) = 0;
3924
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003925 buf->tail[0].iov_base += maxcount&3;
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003926 buf->tail[0].iov_len = pad;
3927 buf->len += pad;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003928 }
3929
3930 space_left = min_t(int, (void *)xdr->end - (void *)xdr->p,
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003931 buf->buflen - buf->len);
3932 buf->buflen = buf->len + space_left;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003933 xdr->end = (__be32 *)((void *)xdr->end + space_left);
3934
3935 return 0;
Chuck Lever76e54922020-11-05 10:24:19 -05003936
3937out_err:
3938 /*
3939 * nfsd_splice_actor may have already messed with the
3940 * page length; reset it so as not to confuse
3941 * xdr_truncate_encode in our caller.
3942 */
3943 buf->page_len = 0;
3944 return nfserr;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003945}
3946
3947static __be32 nfsd4_encode_readv(struct nfsd4_compoundres *resp,
3948 struct nfsd4_read *read,
3949 struct file *file, unsigned long maxcount)
3950{
3951 struct xdr_stream *xdr = &resp->xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 u32 eof;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003953 int starting_len = xdr->buf->len - 8;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003954 __be32 nfserr;
3955 __be32 tmp;
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003956 int pad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
Anna Schumaker403217f2020-08-17 12:53:06 -04003958 read->rd_vlen = xdr_reserve_space_vec(xdr, resp->rqstp->rq_vec, maxcount);
3959 if (read->rd_vlen < 0)
3960 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961
Chuck Lever87c59422018-03-28 13:29:11 -04003962 nfserr = nfsd_readv(resp->rqstp, read->rd_fhp, file, read->rd_offset,
Trond Myklebust83a63072019-08-26 13:03:11 -04003963 resp->rqstp->rq_vec, read->rd_vlen, &maxcount,
3964 &eof);
Chuck Lever87c59422018-03-28 13:29:11 -04003965 read->rd_length = maxcount;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003966 if (nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 return nfserr;
Chuck Lever03493bc2020-06-10 10:36:42 -04003968 if (svc_encode_result_payload(resp->rqstp, starting_len + 8, maxcount))
Chuck Lever41205532020-03-02 14:45:53 -05003969 return nfserr_io;
Chuck Lever7dcf4ab2020-03-02 15:00:04 -05003970 xdr_truncate_encode(xdr, starting_len + 8 + xdr_align_size(maxcount));
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003971
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003972 tmp = htonl(eof);
3973 write_bytes_to_xdr_buf(xdr->buf, starting_len , &tmp, 4);
3974 tmp = htonl(maxcount);
3975 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp, 4);
3976
Chuck Lever7dcf4ab2020-03-02 15:00:04 -05003977 tmp = xdr_zero;
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003978 pad = (maxcount&3) ? 4 - (maxcount&3) : 0;
3979 write_bytes_to_xdr_buf(xdr->buf, starting_len + 8 + maxcount,
Chuck Lever7dcf4ab2020-03-02 15:00:04 -05003980 &tmp, pad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 return 0;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003982
3983}
3984
3985static __be32
3986nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
3987 struct nfsd4_read *read)
3988{
3989 unsigned long maxcount;
3990 struct xdr_stream *xdr = &resp->xdr;
Jeff Layton5c4583b2019-08-18 14:18:54 -04003991 struct file *file;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003992 int starting_len = xdr->buf->len;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003993 __be32 *p;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003994
Jeff Layton5c4583b2019-08-18 14:18:54 -04003995 if (nfserr)
3996 return nfserr;
3997 file = read->rd_nf->nf_file;
3998
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003999 p = xdr_reserve_space(xdr, 8); /* eof flag and byte count */
4000 if (!p) {
Jeff Layton779fb0f2014-11-19 07:51:18 -05004001 WARN_ON_ONCE(test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags));
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004002 return nfserr_resource;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004003 }
Christoph Hellwig68e8bb02015-06-18 16:45:02 +02004004 if (resp->xdr.buf->page_len &&
4005 test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags)) {
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04004006 WARN_ON_ONCE(1);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004007 return nfserr_resource;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004008 }
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004009 xdr_commit_encode(xdr);
4010
4011 maxcount = svc_max_payload(resp->rqstp);
Christoph Hellwig68e8bb02015-06-18 16:45:02 +02004012 maxcount = min_t(unsigned long, maxcount,
4013 (xdr->buf->buflen - xdr->buf->len));
Kinglong Mee3c7aa152014-06-10 18:08:19 +08004014 maxcount = min_t(unsigned long, maxcount, read->rd_length);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004015
Christoph Hellwig68e8bb02015-06-18 16:45:02 +02004016 if (file->f_op->splice_read &&
4017 test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags))
Christoph Hellwig96bcad52015-06-18 16:45:01 +02004018 nfserr = nfsd4_encode_splice_read(resp, read, file, maxcount);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004019 else
Christoph Hellwig96bcad52015-06-18 16:45:01 +02004020 nfserr = nfsd4_encode_readv(resp, read, file, maxcount);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004021
Christoph Hellwig96bcad52015-06-18 16:45:01 +02004022 if (nfserr)
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004023 xdr_truncate_encode(xdr, starting_len);
Christoph Hellwig96bcad52015-06-18 16:45:01 +02004024
Christoph Hellwig96bcad52015-06-18 16:45:01 +02004025 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026}
4027
Al Virob37ad282006-10-19 23:28:59 -07004028static __be32
4029nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030{
4031 int maxcount;
J. Bruce Fields476a7b12014-01-20 17:08:27 -05004032 __be32 wire_count;
4033 int zero = 0;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04004034 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05004035 int length_offset = xdr->buf->len;
Chuck Lever76e54922020-11-05 10:24:19 -05004036 int status;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004037 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04004039 p = xdr_reserve_space(xdr, 4);
4040 if (!p)
4041 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 maxcount = PAGE_SIZE;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004043
J. Bruce Fields476a7b12014-01-20 17:08:27 -05004044 p = xdr_reserve_space(xdr, maxcount);
4045 if (!p)
J. Bruce Fields4e21ac42014-03-22 15:15:11 -04004046 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 /*
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004048 * XXX: By default, vfs_readlink() will truncate symlinks if they
4049 * would overflow the buffer. Is this kosher in NFSv4? If not, one
4050 * easy fix is: if vfs_readlink() precisely fills the buffer, assume
4051 * that truncation occurred, and return NFS4ERR_RESOURCE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 */
J. Bruce Fields476a7b12014-01-20 17:08:27 -05004053 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp,
4054 (char *)p, &maxcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 if (nfserr == nfserr_isdir)
J. Bruce Fieldsd3f627c2014-02-26 17:00:38 -05004056 nfserr = nfserr_inval;
Chuck Lever76e54922020-11-05 10:24:19 -05004057 if (nfserr)
4058 goto out_err;
4059 status = svc_encode_result_payload(readlink->rl_rqstp, length_offset,
4060 maxcount);
4061 if (status) {
4062 nfserr = nfserrno(status);
4063 goto out_err;
J. Bruce Fieldsd3f627c2014-02-26 17:00:38 -05004064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065
J. Bruce Fields476a7b12014-01-20 17:08:27 -05004066 wire_count = htonl(maxcount);
4067 write_bytes_to_xdr_buf(xdr->buf, length_offset, &wire_count, 4);
Avi Kivity69bbd9c2014-06-26 17:23:12 +03004068 xdr_truncate_encode(xdr, length_offset + 4 + ALIGN(maxcount, 4));
J. Bruce Fields476a7b12014-01-20 17:08:27 -05004069 if (maxcount & 3)
4070 write_bytes_to_xdr_buf(xdr->buf, length_offset + 4 + maxcount,
4071 &zero, 4 - (maxcount&3));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 return 0;
Chuck Lever76e54922020-11-05 10:24:19 -05004073
4074out_err:
4075 xdr_truncate_encode(xdr, length_offset);
4076 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077}
4078
Al Virob37ad282006-10-19 23:28:59 -07004079static __be32
4080nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081{
4082 int maxcount;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004083 int bytes_left;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 loff_t offset;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004085 __be64 wire_offset;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04004086 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05004087 int starting_len = xdr->buf->len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004088 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004090 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
4091 if (!p)
4092 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
4094 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004095 *p++ = cpu_to_be32(0);
4096 *p++ = cpu_to_be32(0);
J. Bruce Fields4aea24b2014-01-15 15:17:58 -05004097 resp->xdr.buf->head[0].iov_len = ((char *)resp->xdr.p)
4098 - (char *)resp->xdr.buf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099
4100 /*
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004101 * Number of bytes left for directory entries allowing for the
4102 * final 8 bytes of the readdir and a following failed op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 */
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004104 bytes_left = xdr->buf->buflen - xdr->buf->len
4105 - COMPOUND_ERR_SLACK_SPACE - 8;
4106 if (bytes_left < 0) {
4107 nfserr = nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 goto err_no_verf;
4109 }
Scott Mayhew9c2ece62018-05-07 09:01:08 -04004110 maxcount = svc_max_payload(resp->rqstp);
4111 maxcount = min_t(u32, readdir->rd_maxcount, maxcount);
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004112 /*
4113 * Note the rfc defines rd_maxcount as the size of the
4114 * READDIR4resok structure, which includes the verifier above
4115 * and the 8 bytes encoded at the end of this function:
4116 */
4117 if (maxcount < 16) {
4118 nfserr = nfserr_toosmall;
4119 goto err_no_verf;
4120 }
4121 maxcount = min_t(int, maxcount-16, bytes_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04004123 /* RFC 3530 14.2.24 allows us to ignore dircount when it's 0: */
4124 if (!readdir->rd_dircount)
Scott Mayhew9c2ece62018-05-07 09:01:08 -04004125 readdir->rd_dircount = svc_max_payload(resp->rqstp);
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04004126
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004127 readdir->xdr = xdr;
4128 readdir->rd_maxcount = maxcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 readdir->common.err = 0;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004130 readdir->cookie_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131
4132 offset = readdir->rd_cookie;
4133 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
4134 &offset,
4135 &readdir->common, nfsd4_encode_dirent);
4136 if (nfserr == nfs_ok &&
4137 readdir->common.err == nfserr_toosmall &&
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004138 xdr->buf->len == starting_len + 8) {
4139 /* nothing encoded; which limit did we hit?: */
4140 if (maxcount - 16 < bytes_left)
4141 /* It was the fault of rd_maxcount: */
4142 nfserr = nfserr_toosmall;
4143 else
4144 /* We ran out of buffer space: */
4145 nfserr = nfserr_resource;
4146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 if (nfserr)
4148 goto err_no_verf;
4149
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004150 if (readdir->cookie_offset) {
4151 wire_offset = cpu_to_be64(offset);
4152 write_bytes_to_xdr_buf(xdr->buf, readdir->cookie_offset,
4153 &wire_offset, 8);
4154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004156 p = xdr_reserve_space(xdr, 8);
4157 if (!p) {
4158 WARN_ON_ONCE(1);
4159 goto err_no_verf;
4160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 *p++ = 0; /* no more entries */
4162 *p++ = htonl(readdir->common.err == nfserr_eof);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
4164 return 0;
4165err_no_verf:
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05004166 xdr_truncate_encode(xdr, starting_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 return nfserr;
4168}
4169
Benny Halevy695e12f2008-07-04 14:38:33 +03004170static __be32
Al Virob37ad282006-10-19 23:28:59 -07004171nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004173 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004174 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004176 p = xdr_reserve_space(xdr, 20);
4177 if (!p)
4178 return nfserr_resource;
4179 p = encode_cinfo(p, &remove->rm_cinfo);
4180 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181}
4182
Benny Halevy695e12f2008-07-04 14:38:33 +03004183static __be32
Al Virob37ad282006-10-19 23:28:59 -07004184nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004186 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004187 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004189 p = xdr_reserve_space(xdr, 40);
4190 if (!p)
4191 return nfserr_resource;
4192 p = encode_cinfo(p, &rename->rn_sinfo);
4193 p = encode_cinfo(p, &rename->rn_tinfo);
4194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195}
4196
Benny Halevy695e12f2008-07-04 14:38:33 +03004197static __be32
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004198nfsd4_do_encode_secinfo(struct xdr_stream *xdr, struct svc_export *exp)
Andy Adamsondcb488a32007-07-17 04:04:51 -07004199{
Chuck Lever676e4eb2013-04-30 18:48:54 -04004200 u32 i, nflavs, supported;
J. Bruce Fields4796f452007-07-17 04:04:51 -07004201 struct exp_flavor_info *flavs;
4202 struct exp_flavor_info def_flavs[2];
Chuck Lever676e4eb2013-04-30 18:48:54 -04004203 __be32 *p, *flavorsp;
4204 static bool report = true;
Andy Adamsondcb488a32007-07-17 04:04:51 -07004205
J. Bruce Fields4796f452007-07-17 04:04:51 -07004206 if (exp->ex_nflavors) {
4207 flavs = exp->ex_flavors;
4208 nflavs = exp->ex_nflavors;
4209 } else { /* Handling of some defaults in absence of real secinfo: */
4210 flavs = def_flavs;
4211 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
4212 nflavs = 2;
4213 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
4214 flavs[1].pseudoflavor = RPC_AUTH_NULL;
4215 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
4216 nflavs = 1;
4217 flavs[0].pseudoflavor
4218 = svcauth_gss_flavor(exp->ex_client);
4219 } else {
4220 nflavs = 1;
4221 flavs[0].pseudoflavor
4222 = exp->ex_client->flavour->flavour;
4223 }
4224 }
4225
Chuck Lever676e4eb2013-04-30 18:48:54 -04004226 supported = 0;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004227 p = xdr_reserve_space(xdr, 4);
4228 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004229 return nfserr_resource;
Chuck Lever676e4eb2013-04-30 18:48:54 -04004230 flavorsp = p++; /* to be backfilled later */
Chuck Lever676e4eb2013-04-30 18:48:54 -04004231
J. Bruce Fields4796f452007-07-17 04:04:51 -07004232 for (i = 0; i < nflavs; i++) {
Chuck Lever676e4eb2013-04-30 18:48:54 -04004233 rpc_authflavor_t pf = flavs[i].pseudoflavor;
Chuck Levera77c8062013-03-16 15:55:10 -04004234 struct rpcsec_gss_info info;
Andy Adamsondcb488a32007-07-17 04:04:51 -07004235
Chuck Lever676e4eb2013-04-30 18:48:54 -04004236 if (rpcauth_get_gssinfo(pf, &info) == 0) {
4237 supported++;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004238 p = xdr_reserve_space(xdr, 4 + 4 +
4239 XDR_LEN(info.oid.len) + 4 + 4);
4240 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004241 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004242 *p++ = cpu_to_be32(RPC_AUTH_GSS);
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004243 p = xdr_encode_opaque(p, info.oid.data, info.oid.len);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004244 *p++ = cpu_to_be32(info.qop);
4245 *p++ = cpu_to_be32(info.service);
Chuck Lever676e4eb2013-04-30 18:48:54 -04004246 } else if (pf < RPC_AUTH_MAXFLAVOR) {
4247 supported++;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004248 p = xdr_reserve_space(xdr, 4);
4249 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004250 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004251 *p++ = cpu_to_be32(pf);
Chuck Lever676e4eb2013-04-30 18:48:54 -04004252 } else {
4253 if (report)
4254 pr_warn("NFS: SECINFO: security flavor %u "
4255 "is not supported\n", pf);
Andy Adamsondcb488a32007-07-17 04:04:51 -07004256 }
4257 }
Chuck Levera77c8062013-03-16 15:55:10 -04004258
Chuck Lever676e4eb2013-04-30 18:48:54 -04004259 if (nflavs != supported)
4260 report = false;
4261 *flavorsp = htonl(supported);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004262 return 0;
Andy Adamsondcb488a32007-07-17 04:04:51 -07004263}
4264
Mi Jinlong22b6dee2010-12-27 14:29:57 +08004265static __be32
4266nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
4267 struct nfsd4_secinfo *secinfo)
4268{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004269 struct xdr_stream *xdr = &resp->xdr;
4270
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004271 return nfsd4_do_encode_secinfo(xdr, secinfo->si_exp);
Mi Jinlong22b6dee2010-12-27 14:29:57 +08004272}
4273
4274static __be32
4275nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
4276 struct nfsd4_secinfo_no_name *secinfo)
4277{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004278 struct xdr_stream *xdr = &resp->xdr;
4279
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004280 return nfsd4_do_encode_secinfo(xdr, secinfo->sin_exp);
Mi Jinlong22b6dee2010-12-27 14:29:57 +08004281}
4282
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283/*
4284 * The SETATTR encode routine is special -- it always encodes a bitmap,
4285 * regardless of the error status.
4286 */
Benny Halevy695e12f2008-07-04 14:38:33 +03004287static __be32
Al Virob37ad282006-10-19 23:28:59 -07004288nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004290 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004291 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004293 p = xdr_reserve_space(xdr, 16);
4294 if (!p)
4295 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 if (nfserr) {
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004297 *p++ = cpu_to_be32(3);
4298 *p++ = cpu_to_be32(0);
4299 *p++ = cpu_to_be32(0);
4300 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 }
4302 else {
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004303 *p++ = cpu_to_be32(3);
4304 *p++ = cpu_to_be32(setattr->sa_bmval[0]);
4305 *p++ = cpu_to_be32(setattr->sa_bmval[1]);
4306 *p++ = cpu_to_be32(setattr->sa_bmval[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 }
Benny Halevy695e12f2008-07-04 14:38:33 +03004308 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309}
4310
Benny Halevy695e12f2008-07-04 14:38:33 +03004311static __be32
Al Virob37ad282006-10-19 23:28:59 -07004312nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004314 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004315 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316
4317 if (!nfserr) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004318 p = xdr_reserve_space(xdr, 8 + NFS4_VERIFIER_SIZE);
4319 if (!p)
4320 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004321 p = xdr_encode_opaque_fixed(p, &scd->se_clientid, 8);
4322 p = xdr_encode_opaque_fixed(p, &scd->se_confirm,
4323 NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 }
4325 else if (nfserr == nfserr_clid_inuse) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004326 p = xdr_reserve_space(xdr, 8);
4327 if (!p)
4328 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004329 *p++ = cpu_to_be32(0);
4330 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 }
Benny Halevy695e12f2008-07-04 14:38:33 +03004332 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333}
4334
Benny Halevy695e12f2008-07-04 14:38:33 +03004335static __be32
Al Virob37ad282006-10-19 23:28:59 -07004336nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004338 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004339 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004341 p = xdr_reserve_space(xdr, 16);
4342 if (!p)
4343 return nfserr_resource;
4344 *p++ = cpu_to_be32(write->wr_bytes_written);
4345 *p++ = cpu_to_be32(write->wr_how_written);
4346 p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
4347 NFS4_VERIFIER_SIZE);
4348 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349}
4350
Benny Halevy695e12f2008-07-04 14:38:33 +03004351static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004352nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03004353 struct nfsd4_exchange_id *exid)
4354{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004355 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004356 __be32 *p;
Andy Adamson0733d212009-04-03 08:28:01 +03004357 char *major_id;
4358 char *server_scope;
4359 int major_id_sz;
4360 int server_scope_sz;
4361 uint64_t minor_id = 0;
Scott Mayhew7627d7d2020-02-19 15:52:15 -05004362 struct nfsd_net *nn = net_generic(SVC_NET(resp->rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03004363
Scott Mayhew7627d7d2020-02-19 15:52:15 -05004364 major_id = nn->nfsd_name;
4365 major_id_sz = strlen(nn->nfsd_name);
4366 server_scope = nn->nfsd_name;
4367 server_scope_sz = strlen(nn->nfsd_name);
Andy Adamson0733d212009-04-03 08:28:01 +03004368
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004369 p = xdr_reserve_space(xdr,
Andy Adamson0733d212009-04-03 08:28:01 +03004370 8 /* eir_clientid */ +
4371 4 /* eir_sequenceid */ +
4372 4 /* eir_flags */ +
Kinglong Meea8bb84b2013-12-10 15:24:36 +08004373 4 /* spr_how */);
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004374 if (!p)
4375 return nfserr_resource;
Andy Adamson0733d212009-04-03 08:28:01 +03004376
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004377 p = xdr_encode_opaque_fixed(p, &exid->clientid, 8);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004378 *p++ = cpu_to_be32(exid->seqid);
4379 *p++ = cpu_to_be32(exid->flags);
Andy Adamson0733d212009-04-03 08:28:01 +03004380
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004381 *p++ = cpu_to_be32(exid->spa_how);
Kinglong Meea8bb84b2013-12-10 15:24:36 +08004382
J. Bruce Fields57266a62013-04-13 14:27:29 -04004383 switch (exid->spa_how) {
4384 case SP4_NONE:
4385 break;
4386 case SP4_MACH_CRED:
4387 /* spo_must_enforce bitmap: */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004388 nfserr = nfsd4_encode_bitmap(xdr,
Andrew Elbleed941642016-06-15 12:52:09 -04004389 exid->spo_must_enforce[0],
4390 exid->spo_must_enforce[1],
4391 exid->spo_must_enforce[2]);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004392 if (nfserr)
4393 return nfserr;
Andrew Elbleed941642016-06-15 12:52:09 -04004394 /* spo_must_allow bitmap: */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004395 nfserr = nfsd4_encode_bitmap(xdr,
Andrew Elbleed941642016-06-15 12:52:09 -04004396 exid->spo_must_allow[0],
4397 exid->spo_must_allow[1],
4398 exid->spo_must_allow[2]);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004399 if (nfserr)
4400 return nfserr;
J. Bruce Fields57266a62013-04-13 14:27:29 -04004401 break;
4402 default:
4403 WARN_ON_ONCE(1);
4404 }
Andy Adamson0733d212009-04-03 08:28:01 +03004405
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004406 p = xdr_reserve_space(xdr,
Kinglong Meea8bb84b2013-12-10 15:24:36 +08004407 8 /* so_minor_id */ +
4408 4 /* so_major_id.len */ +
4409 (XDR_QUADLEN(major_id_sz) * 4) +
4410 4 /* eir_server_scope.len */ +
4411 (XDR_QUADLEN(server_scope_sz) * 4) +
4412 4 /* eir_server_impl_id.count (0) */);
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004413 if (!p)
4414 return nfserr_resource;
Kinglong Meea8bb84b2013-12-10 15:24:36 +08004415
Andy Adamson0733d212009-04-03 08:28:01 +03004416 /* The server_owner struct */
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04004417 p = xdr_encode_hyper(p, minor_id); /* Minor id */
Andy Adamson0733d212009-04-03 08:28:01 +03004418 /* major id */
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004419 p = xdr_encode_opaque(p, major_id, major_id_sz);
Andy Adamson0733d212009-04-03 08:28:01 +03004420
4421 /* Server scope */
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004422 p = xdr_encode_opaque(p, server_scope, server_scope_sz);
Andy Adamson0733d212009-04-03 08:28:01 +03004423
4424 /* Implementation id */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004425 *p++ = cpu_to_be32(0); /* zero length nfs_impl_id4 array */
Andy Adamson0733d212009-04-03 08:28:01 +03004426 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03004427}
4428
4429static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004430nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03004431 struct nfsd4_create_session *sess)
4432{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004433 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004434 __be32 *p;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004435
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004436 p = xdr_reserve_space(xdr, 24);
4437 if (!p)
4438 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004439 p = xdr_encode_opaque_fixed(p, sess->sessionid.data,
4440 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004441 *p++ = cpu_to_be32(sess->seqid);
4442 *p++ = cpu_to_be32(sess->flags);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004443
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004444 p = xdr_reserve_space(xdr, 28);
4445 if (!p)
4446 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004447 *p++ = cpu_to_be32(0); /* headerpadsz */
4448 *p++ = cpu_to_be32(sess->fore_channel.maxreq_sz);
4449 *p++ = cpu_to_be32(sess->fore_channel.maxresp_sz);
4450 *p++ = cpu_to_be32(sess->fore_channel.maxresp_cached);
4451 *p++ = cpu_to_be32(sess->fore_channel.maxops);
4452 *p++ = cpu_to_be32(sess->fore_channel.maxreqs);
4453 *p++ = cpu_to_be32(sess->fore_channel.nr_rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004454
4455 if (sess->fore_channel.nr_rdma_attrs) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004456 p = xdr_reserve_space(xdr, 4);
4457 if (!p)
4458 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004459 *p++ = cpu_to_be32(sess->fore_channel.rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004460 }
4461
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004462 p = xdr_reserve_space(xdr, 28);
4463 if (!p)
4464 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004465 *p++ = cpu_to_be32(0); /* headerpadsz */
4466 *p++ = cpu_to_be32(sess->back_channel.maxreq_sz);
4467 *p++ = cpu_to_be32(sess->back_channel.maxresp_sz);
4468 *p++ = cpu_to_be32(sess->back_channel.maxresp_cached);
4469 *p++ = cpu_to_be32(sess->back_channel.maxops);
4470 *p++ = cpu_to_be32(sess->back_channel.maxreqs);
4471 *p++ = cpu_to_be32(sess->back_channel.nr_rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004472
4473 if (sess->back_channel.nr_rdma_attrs) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004474 p = xdr_reserve_space(xdr, 4);
4475 if (!p)
4476 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004477 *p++ = cpu_to_be32(sess->back_channel.rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004478 }
4479 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03004480}
4481
4482static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004483nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03004484 struct nfsd4_sequence *seq)
4485{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004486 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004487 __be32 *p;
Benny Halevyb85d4c02009-04-03 08:28:08 +03004488
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004489 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 20);
4490 if (!p)
4491 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004492 p = xdr_encode_opaque_fixed(p, seq->sessionid.data,
4493 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004494 *p++ = cpu_to_be32(seq->seqid);
4495 *p++ = cpu_to_be32(seq->slotid);
J. Bruce Fieldsb7d7ca32011-08-31 15:39:30 -04004496 /* Note slotid's are numbered from zero: */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004497 *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_highest_slotid */
4498 *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_target_highest_slotid */
4499 *p++ = cpu_to_be32(seq->status_flags);
Benny Halevyb85d4c02009-04-03 08:28:08 +03004500
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04004501 resp->cstate.data_offset = xdr->buf->len; /* DRC cache data pointer */
Benny Halevyb85d4c02009-04-03 08:28:08 +03004502 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03004503}
4504
J. Bruce Fields2355c592012-04-25 16:56:22 -04004505static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004506nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
Bryan Schumaker17456802011-07-13 10:50:48 -04004507 struct nfsd4_test_stateid *test_stateid)
4508{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004509 struct xdr_stream *xdr = &resp->xdr;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004510 struct nfsd4_test_stateid_id *stateid, *next;
Bryan Schumaker17456802011-07-13 10:50:48 -04004511 __be32 *p;
Bryan Schumaker17456802011-07-13 10:50:48 -04004512
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004513 p = xdr_reserve_space(xdr, 4 + (4 * test_stateid->ts_num_ids));
4514 if (!p)
4515 return nfserr_resource;
Bryan Schumaker17456802011-07-13 10:50:48 -04004516 *p++ = htonl(test_stateid->ts_num_ids);
Bryan Schumaker17456802011-07-13 10:50:48 -04004517
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004518 list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
Al Viro02f5fde2012-04-13 00:10:34 -04004519 *p++ = stateid->ts_id_status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004520 }
Bryan Schumaker17456802011-07-13 10:50:48 -04004521
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004522 return 0;
Bryan Schumaker17456802011-07-13 10:50:48 -04004523}
4524
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004525#ifdef CONFIG_NFSD_PNFS
4526static __be32
4527nfsd4_encode_getdeviceinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
4528 struct nfsd4_getdeviceinfo *gdev)
4529{
4530 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004531 const struct nfsd4_layout_ops *ops;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004532 u32 starting_len = xdr->buf->len, needed_len;
4533 __be32 *p;
4534
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004535 p = xdr_reserve_space(xdr, 4);
4536 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004537 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004538
4539 *p++ = cpu_to_be32(gdev->gd_layout_type);
4540
4541 /* If maxcount is 0 then just update notifications */
4542 if (gdev->gd_maxcount != 0) {
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004543 ops = nfsd4_layout_ops[gdev->gd_layout_type];
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004544 nfserr = ops->encode_getdeviceinfo(xdr, gdev);
4545 if (nfserr) {
4546 /*
4547 * We don't bother to burden the layout drivers with
4548 * enforcing gd_maxcount, just tell the client to
4549 * come back with a bigger buffer if it's not enough.
4550 */
4551 if (xdr->buf->len + 4 > gdev->gd_maxcount)
4552 goto toosmall;
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004553 return nfserr;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004554 }
4555 }
4556
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004557 if (gdev->gd_notify_types) {
4558 p = xdr_reserve_space(xdr, 4 + 4);
4559 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004560 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004561 *p++ = cpu_to_be32(1); /* bitmap length */
4562 *p++ = cpu_to_be32(gdev->gd_notify_types);
4563 } else {
4564 p = xdr_reserve_space(xdr, 4);
4565 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004566 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004567 *p++ = 0;
4568 }
4569
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004570 return 0;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004571toosmall:
4572 dprintk("%s: maxcount too small\n", __func__);
4573 needed_len = xdr->buf->len + 4 /* notifications */;
4574 xdr_truncate_encode(xdr, starting_len);
4575 p = xdr_reserve_space(xdr, 4);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004576 if (!p)
4577 return nfserr_resource;
4578 *p++ = cpu_to_be32(needed_len);
4579 return nfserr_toosmall;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004580}
4581
4582static __be32
4583nfsd4_encode_layoutget(struct nfsd4_compoundres *resp, __be32 nfserr,
4584 struct nfsd4_layoutget *lgp)
4585{
4586 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004587 const struct nfsd4_layout_ops *ops;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004588 __be32 *p;
4589
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004590 p = xdr_reserve_space(xdr, 36 + sizeof(stateid_opaque_t));
4591 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004592 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004593
4594 *p++ = cpu_to_be32(1); /* we always set return-on-close */
4595 *p++ = cpu_to_be32(lgp->lg_sid.si_generation);
4596 p = xdr_encode_opaque_fixed(p, &lgp->lg_sid.si_opaque,
4597 sizeof(stateid_opaque_t));
4598
4599 *p++ = cpu_to_be32(1); /* we always return a single layout */
4600 p = xdr_encode_hyper(p, lgp->lg_seg.offset);
4601 p = xdr_encode_hyper(p, lgp->lg_seg.length);
4602 *p++ = cpu_to_be32(lgp->lg_seg.iomode);
4603 *p++ = cpu_to_be32(lgp->lg_layout_type);
4604
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004605 ops = nfsd4_layout_ops[lgp->lg_layout_type];
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004606 return ops->encode_layoutget(xdr, lgp);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004607}
4608
4609static __be32
4610nfsd4_encode_layoutcommit(struct nfsd4_compoundres *resp, __be32 nfserr,
4611 struct nfsd4_layoutcommit *lcp)
4612{
4613 struct xdr_stream *xdr = &resp->xdr;
4614 __be32 *p;
4615
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004616 p = xdr_reserve_space(xdr, 4);
4617 if (!p)
4618 return nfserr_resource;
4619 *p++ = cpu_to_be32(lcp->lc_size_chg);
4620 if (lcp->lc_size_chg) {
4621 p = xdr_reserve_space(xdr, 8);
4622 if (!p)
4623 return nfserr_resource;
4624 p = xdr_encode_hyper(p, lcp->lc_newsize);
4625 }
4626
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004627 return 0;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004628}
4629
4630static __be32
4631nfsd4_encode_layoutreturn(struct nfsd4_compoundres *resp, __be32 nfserr,
4632 struct nfsd4_layoutreturn *lrp)
4633{
4634 struct xdr_stream *xdr = &resp->xdr;
4635 __be32 *p;
4636
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004637 p = xdr_reserve_space(xdr, 4);
4638 if (!p)
4639 return nfserr_resource;
4640 *p++ = cpu_to_be32(lrp->lrs_present);
4641 if (lrp->lrs_present)
Kinglong Mee376675d2015-03-22 22:17:10 +08004642 return nfsd4_encode_stateid(xdr, &lrp->lr_sid);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004643 return 0;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004644}
4645#endif /* CONFIG_NFSD_PNFS */
4646
Andy Adamson2db134e2009-04-03 08:27:55 +03004647static __be32
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004648nfsd42_encode_write_res(struct nfsd4_compoundres *resp,
4649 struct nfsd42_write_res *write, bool sync)
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004650{
4651 __be32 *p;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004652 p = xdr_reserve_space(&resp->xdr, 4);
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004653 if (!p)
4654 return nfserr_resource;
4655
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004656 if (sync)
4657 *p++ = cpu_to_be32(0);
4658 else {
4659 __be32 nfserr;
4660 *p++ = cpu_to_be32(1);
4661 nfserr = nfsd4_encode_stateid(&resp->xdr, &write->cb_stateid);
4662 if (nfserr)
4663 return nfserr;
4664 }
4665 p = xdr_reserve_space(&resp->xdr, 8 + 4 + NFS4_VERIFIER_SIZE);
4666 if (!p)
4667 return nfserr_resource;
4668
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004669 p = xdr_encode_hyper(p, write->wr_bytes_written);
4670 *p++ = cpu_to_be32(write->wr_stable_how);
4671 p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
4672 NFS4_VERIFIER_SIZE);
4673 return nfs_ok;
4674}
4675
4676static __be32
Olga Kornievskaia51911862019-08-08 11:14:59 -04004677nfsd42_encode_nl4_server(struct nfsd4_compoundres *resp, struct nl4_server *ns)
4678{
4679 struct xdr_stream *xdr = &resp->xdr;
4680 struct nfs42_netaddr *addr;
4681 __be32 *p;
4682
4683 p = xdr_reserve_space(xdr, 4);
4684 *p++ = cpu_to_be32(ns->nl4_type);
4685
4686 switch (ns->nl4_type) {
4687 case NL4_NETADDR:
4688 addr = &ns->u.nl4_addr;
4689
4690 /* netid_len, netid, uaddr_len, uaddr (port included
4691 * in RPCBIND_MAXUADDRLEN)
4692 */
4693 p = xdr_reserve_space(xdr,
4694 4 /* netid len */ +
4695 (XDR_QUADLEN(addr->netid_len) * 4) +
4696 4 /* uaddr len */ +
4697 (XDR_QUADLEN(addr->addr_len) * 4));
4698 if (!p)
4699 return nfserr_resource;
4700
4701 *p++ = cpu_to_be32(addr->netid_len);
4702 p = xdr_encode_opaque_fixed(p, addr->netid,
4703 addr->netid_len);
4704 *p++ = cpu_to_be32(addr->addr_len);
4705 p = xdr_encode_opaque_fixed(p, addr->addr,
4706 addr->addr_len);
4707 break;
4708 default:
4709 WARN_ON_ONCE(ns->nl4_type != NL4_NETADDR);
4710 return nfserr_inval;
4711 }
4712
4713 return 0;
4714}
4715
4716static __be32
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004717nfsd4_encode_copy(struct nfsd4_compoundres *resp, __be32 nfserr,
4718 struct nfsd4_copy *copy)
4719{
4720 __be32 *p;
4721
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004722 nfserr = nfsd42_encode_write_res(resp, &copy->cp_res,
Chuck Levere8febea2020-11-04 10:49:37 -05004723 !!copy->cp_synchronous);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004724 if (nfserr)
4725 return nfserr;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004726
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004727 p = xdr_reserve_space(&resp->xdr, 4 + 4);
J. Bruce Fieldsedcc8452018-03-07 15:37:35 -05004728 *p++ = xdr_one; /* cr_consecutive */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004729 *p++ = cpu_to_be32(copy->cp_synchronous);
4730 return 0;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004731}
4732
4733static __be32
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04004734nfsd4_encode_offload_status(struct nfsd4_compoundres *resp, __be32 nfserr,
4735 struct nfsd4_offload_status *os)
4736{
4737 struct xdr_stream *xdr = &resp->xdr;
4738 __be32 *p;
4739
4740 p = xdr_reserve_space(xdr, 8 + 4);
4741 if (!p)
4742 return nfserr_resource;
4743 p = xdr_encode_hyper(p, os->count);
4744 *p++ = cpu_to_be32(0);
Anna Schumaker528b8492020-09-28 13:08:58 -04004745 return nfserr;
4746}
4747
4748static __be32
4749nfsd4_encode_read_plus_data(struct nfsd4_compoundres *resp,
4750 struct nfsd4_read *read,
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004751 unsigned long *maxcount, u32 *eof,
4752 loff_t *pos)
Anna Schumaker528b8492020-09-28 13:08:58 -04004753{
4754 struct xdr_stream *xdr = &resp->xdr;
4755 struct file *file = read->rd_nf->nf_file;
4756 int starting_len = xdr->buf->len;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004757 loff_t hole_pos;
Anna Schumaker528b8492020-09-28 13:08:58 -04004758 __be32 nfserr;
4759 __be32 *p, tmp;
4760 __be64 tmp64;
4761
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004762 hole_pos = pos ? *pos : vfs_llseek(file, read->rd_offset, SEEK_HOLE);
Anna Schumaker2db27992020-09-28 13:08:59 -04004763 if (hole_pos > read->rd_offset)
Anna Schumaker278765e2020-09-28 13:09:00 -04004764 *maxcount = min_t(unsigned long, *maxcount, hole_pos - read->rd_offset);
4765 *maxcount = min_t(unsigned long, *maxcount, (xdr->buf->buflen - xdr->buf->len));
Anna Schumaker528b8492020-09-28 13:08:58 -04004766
4767 /* Content type, offset, byte count */
4768 p = xdr_reserve_space(xdr, 4 + 8 + 4);
4769 if (!p)
4770 return nfserr_resource;
4771
Anna Schumaker278765e2020-09-28 13:09:00 -04004772 read->rd_vlen = xdr_reserve_space_vec(xdr, resp->rqstp->rq_vec, *maxcount);
Anna Schumaker528b8492020-09-28 13:08:58 -04004773 if (read->rd_vlen < 0)
4774 return nfserr_resource;
4775
4776 nfserr = nfsd_readv(resp->rqstp, read->rd_fhp, file, read->rd_offset,
Anna Schumaker278765e2020-09-28 13:09:00 -04004777 resp->rqstp->rq_vec, read->rd_vlen, maxcount, eof);
Anna Schumaker528b8492020-09-28 13:08:58 -04004778 if (nfserr)
4779 return nfserr;
4780
4781 tmp = htonl(NFS4_CONTENT_DATA);
4782 write_bytes_to_xdr_buf(xdr->buf, starting_len, &tmp, 4);
4783 tmp64 = cpu_to_be64(read->rd_offset);
4784 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp64, 8);
Anna Schumaker278765e2020-09-28 13:09:00 -04004785 tmp = htonl(*maxcount);
Anna Schumaker528b8492020-09-28 13:08:58 -04004786 write_bytes_to_xdr_buf(xdr->buf, starting_len + 12, &tmp, 4);
4787 return nfs_ok;
4788}
4789
4790static __be32
Anna Schumaker2db27992020-09-28 13:08:59 -04004791nfsd4_encode_read_plus_hole(struct nfsd4_compoundres *resp,
4792 struct nfsd4_read *read,
Anna Schumaker278765e2020-09-28 13:09:00 -04004793 unsigned long *maxcount, u32 *eof)
Anna Schumaker2db27992020-09-28 13:08:59 -04004794{
4795 struct file *file = read->rd_nf->nf_file;
Anna Schumaker278765e2020-09-28 13:09:00 -04004796 loff_t data_pos = vfs_llseek(file, read->rd_offset, SEEK_DATA);
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004797 loff_t f_size = i_size_read(file_inode(file));
Anna Schumaker278765e2020-09-28 13:09:00 -04004798 unsigned long count;
Anna Schumaker2db27992020-09-28 13:08:59 -04004799 __be32 *p;
4800
Anna Schumaker278765e2020-09-28 13:09:00 -04004801 if (data_pos == -ENXIO)
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004802 data_pos = f_size;
4803 else if (data_pos <= read->rd_offset || (data_pos < f_size && data_pos % PAGE_SIZE))
4804 return nfsd4_encode_read_plus_data(resp, read, maxcount, eof, &f_size);
Anna Schumaker278765e2020-09-28 13:09:00 -04004805 count = data_pos - read->rd_offset;
4806
Anna Schumaker2db27992020-09-28 13:08:59 -04004807 /* Content type, offset, byte count */
4808 p = xdr_reserve_space(&resp->xdr, 4 + 8 + 8);
4809 if (!p)
4810 return nfserr_resource;
4811
4812 *p++ = htonl(NFS4_CONTENT_HOLE);
4813 p = xdr_encode_hyper(p, read->rd_offset);
Anna Schumaker278765e2020-09-28 13:09:00 -04004814 p = xdr_encode_hyper(p, count);
Anna Schumaker2db27992020-09-28 13:08:59 -04004815
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004816 *eof = (read->rd_offset + count) >= f_size;
Anna Schumaker278765e2020-09-28 13:09:00 -04004817 *maxcount = min_t(unsigned long, count, *maxcount);
Anna Schumaker2db27992020-09-28 13:08:59 -04004818 return nfs_ok;
4819}
4820
4821static __be32
Anna Schumaker528b8492020-09-28 13:08:58 -04004822nfsd4_encode_read_plus(struct nfsd4_compoundres *resp, __be32 nfserr,
4823 struct nfsd4_read *read)
4824{
Anna Schumaker278765e2020-09-28 13:09:00 -04004825 unsigned long maxcount, count;
Anna Schumaker528b8492020-09-28 13:08:58 -04004826 struct xdr_stream *xdr = &resp->xdr;
4827 struct file *file;
4828 int starting_len = xdr->buf->len;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004829 int last_segment = xdr->buf->len;
Anna Schumaker528b8492020-09-28 13:08:58 -04004830 int segments = 0;
4831 __be32 *p, tmp;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004832 bool is_data;
Anna Schumaker2db27992020-09-28 13:08:59 -04004833 loff_t pos;
Anna Schumaker528b8492020-09-28 13:08:58 -04004834 u32 eof;
4835
4836 if (nfserr)
4837 return nfserr;
4838 file = read->rd_nf->nf_file;
4839
4840 /* eof flag, segment count */
4841 p = xdr_reserve_space(xdr, 4 + 4);
4842 if (!p)
4843 return nfserr_resource;
4844 xdr_commit_encode(xdr);
4845
4846 maxcount = svc_max_payload(resp->rqstp);
4847 maxcount = min_t(unsigned long, maxcount,
4848 (xdr->buf->buflen - xdr->buf->len));
4849 maxcount = min_t(unsigned long, maxcount, read->rd_length);
Anna Schumaker278765e2020-09-28 13:09:00 -04004850 count = maxcount;
Anna Schumaker528b8492020-09-28 13:08:58 -04004851
4852 eof = read->rd_offset >= i_size_read(file_inode(file));
Anna Schumaker2db27992020-09-28 13:08:59 -04004853 if (eof)
4854 goto out;
4855
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004856 pos = vfs_llseek(file, read->rd_offset, SEEK_HOLE);
4857 is_data = pos > read->rd_offset;
Anna Schumaker2db27992020-09-28 13:08:59 -04004858
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004859 while (count > 0 && !eof) {
Anna Schumaker278765e2020-09-28 13:09:00 -04004860 maxcount = count;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004861 if (is_data)
4862 nfserr = nfsd4_encode_read_plus_data(resp, read, &maxcount, &eof,
4863 segments == 0 ? &pos : NULL);
4864 else
4865 nfserr = nfsd4_encode_read_plus_hole(resp, read, &maxcount, &eof);
Anna Schumaker278765e2020-09-28 13:09:00 -04004866 if (nfserr)
4867 goto out;
4868 count -= maxcount;
4869 read->rd_offset += maxcount;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004870 is_data = !is_data;
4871 last_segment = xdr->buf->len;
Anna Schumaker528b8492020-09-28 13:08:58 -04004872 segments++;
4873 }
4874
Anna Schumaker2db27992020-09-28 13:08:59 -04004875out:
Anna Schumaker278765e2020-09-28 13:09:00 -04004876 if (nfserr && segments == 0)
Anna Schumaker528b8492020-09-28 13:08:58 -04004877 xdr_truncate_encode(xdr, starting_len);
4878 else {
4879 tmp = htonl(eof);
4880 write_bytes_to_xdr_buf(xdr->buf, starting_len, &tmp, 4);
4881 tmp = htonl(segments);
4882 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp, 4);
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004883 if (nfserr) {
4884 xdr_truncate_encode(xdr, last_segment);
4885 nfserr = nfs_ok;
4886 }
Anna Schumaker528b8492020-09-28 13:08:58 -04004887 }
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04004888
4889 return nfserr;
4890}
4891
4892static __be32
Olga Kornievskaia51911862019-08-08 11:14:59 -04004893nfsd4_encode_copy_notify(struct nfsd4_compoundres *resp, __be32 nfserr,
4894 struct nfsd4_copy_notify *cn)
4895{
4896 struct xdr_stream *xdr = &resp->xdr;
4897 __be32 *p;
4898
4899 if (nfserr)
4900 return nfserr;
4901
4902 /* 8 sec, 4 nsec */
4903 p = xdr_reserve_space(xdr, 12);
4904 if (!p)
4905 return nfserr_resource;
4906
4907 /* cnr_lease_time */
4908 p = xdr_encode_hyper(p, cn->cpn_sec);
4909 *p++ = cpu_to_be32(cn->cpn_nsec);
4910
4911 /* cnr_stateid */
4912 nfserr = nfsd4_encode_stateid(xdr, &cn->cpn_cnr_stateid);
4913 if (nfserr)
4914 return nfserr;
4915
4916 /* cnr_src.nl_nsvr */
4917 p = xdr_reserve_space(xdr, 4);
4918 if (!p)
4919 return nfserr_resource;
4920
4921 *p++ = cpu_to_be32(1);
4922
4923 return nfsd42_encode_nl4_server(resp, &cn->cpn_src);
4924}
4925
4926static __be32
Anna Schumaker24bab492014-09-26 13:58:27 -04004927nfsd4_encode_seek(struct nfsd4_compoundres *resp, __be32 nfserr,
4928 struct nfsd4_seek *seek)
4929{
4930 __be32 *p;
4931
Anna Schumaker24bab492014-09-26 13:58:27 -04004932 p = xdr_reserve_space(&resp->xdr, 4 + 8);
4933 *p++ = cpu_to_be32(seek->seek_eof);
4934 p = xdr_encode_hyper(p, seek->seek_pos);
4935
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004936 return 0;
Anna Schumaker24bab492014-09-26 13:58:27 -04004937}
4938
4939static __be32
Benny Halevy695e12f2008-07-04 14:38:33 +03004940nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
4941{
4942 return nfserr;
4943}
4944
Frank van der Linden23e50fe2020-06-23 22:39:26 +00004945/*
4946 * Encode kmalloc-ed buffer in to XDR stream.
4947 */
Chuck Leverb9a49232020-09-11 14:47:53 -04004948static __be32
Frank van der Linden23e50fe2020-06-23 22:39:26 +00004949nfsd4_vbuf_to_stream(struct xdr_stream *xdr, char *buf, u32 buflen)
4950{
4951 u32 cplen;
4952 __be32 *p;
4953
4954 cplen = min_t(unsigned long, buflen,
4955 ((void *)xdr->end - (void *)xdr->p));
4956 p = xdr_reserve_space(xdr, cplen);
4957 if (!p)
4958 return nfserr_resource;
4959
4960 memcpy(p, buf, cplen);
4961 buf += cplen;
4962 buflen -= cplen;
4963
4964 while (buflen) {
4965 cplen = min_t(u32, buflen, PAGE_SIZE);
4966 p = xdr_reserve_space(xdr, cplen);
4967 if (!p)
4968 return nfserr_resource;
4969
4970 memcpy(p, buf, cplen);
4971
4972 if (cplen < PAGE_SIZE) {
4973 /*
4974 * We're done, with a length that wasn't page
4975 * aligned, so possibly not word aligned. Pad
4976 * any trailing bytes with 0.
4977 */
4978 xdr_encode_opaque_fixed(p, NULL, cplen);
4979 break;
4980 }
4981
4982 buflen -= PAGE_SIZE;
4983 buf += PAGE_SIZE;
4984 }
4985
4986 return 0;
4987}
4988
4989static __be32
4990nfsd4_encode_getxattr(struct nfsd4_compoundres *resp, __be32 nfserr,
4991 struct nfsd4_getxattr *getxattr)
4992{
4993 struct xdr_stream *xdr = &resp->xdr;
4994 __be32 *p, err;
4995
4996 p = xdr_reserve_space(xdr, 4);
4997 if (!p)
4998 return nfserr_resource;
4999
5000 *p = cpu_to_be32(getxattr->getxa_len);
5001
5002 if (getxattr->getxa_len == 0)
5003 return 0;
5004
5005 err = nfsd4_vbuf_to_stream(xdr, getxattr->getxa_buf,
5006 getxattr->getxa_len);
5007
5008 kvfree(getxattr->getxa_buf);
5009
5010 return err;
5011}
5012
5013static __be32
5014nfsd4_encode_setxattr(struct nfsd4_compoundres *resp, __be32 nfserr,
5015 struct nfsd4_setxattr *setxattr)
5016{
5017 struct xdr_stream *xdr = &resp->xdr;
5018 __be32 *p;
5019
5020 p = xdr_reserve_space(xdr, 20);
5021 if (!p)
5022 return nfserr_resource;
5023
5024 encode_cinfo(p, &setxattr->setxa_cinfo);
5025
5026 return 0;
5027}
5028
5029/*
5030 * See if there are cookie values that can be rejected outright.
5031 */
5032static __be32
5033nfsd4_listxattr_validate_cookie(struct nfsd4_listxattrs *listxattrs,
5034 u32 *offsetp)
5035{
5036 u64 cookie = listxattrs->lsxa_cookie;
5037
5038 /*
5039 * If the cookie is larger than the maximum number we can fit
5040 * in either the buffer we just got back from vfs_listxattr, or,
5041 * XDR-encoded, in the return buffer, it's invalid.
5042 */
5043 if (cookie > (listxattrs->lsxa_len) / (XATTR_USER_PREFIX_LEN + 2))
5044 return nfserr_badcookie;
5045
5046 if (cookie > (listxattrs->lsxa_maxcount /
5047 (XDR_QUADLEN(XATTR_USER_PREFIX_LEN + 2) + 4)))
5048 return nfserr_badcookie;
5049
5050 *offsetp = (u32)cookie;
5051 return 0;
5052}
5053
5054static __be32
5055nfsd4_encode_listxattrs(struct nfsd4_compoundres *resp, __be32 nfserr,
5056 struct nfsd4_listxattrs *listxattrs)
5057{
5058 struct xdr_stream *xdr = &resp->xdr;
5059 u32 cookie_offset, count_offset, eof;
5060 u32 left, xdrleft, slen, count;
5061 u32 xdrlen, offset;
5062 u64 cookie;
5063 char *sp;
Chuck Leverb9a49232020-09-11 14:47:53 -04005064 __be32 status, tmp;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005065 __be32 *p;
5066 u32 nuser;
5067
5068 eof = 1;
5069
5070 status = nfsd4_listxattr_validate_cookie(listxattrs, &offset);
5071 if (status)
5072 goto out;
5073
5074 /*
5075 * Reserve space for the cookie and the name array count. Record
5076 * the offsets to save them later.
5077 */
5078 cookie_offset = xdr->buf->len;
5079 count_offset = cookie_offset + 8;
5080 p = xdr_reserve_space(xdr, 12);
5081 if (!p) {
5082 status = nfserr_resource;
5083 goto out;
5084 }
5085
5086 count = 0;
5087 left = listxattrs->lsxa_len;
5088 sp = listxattrs->lsxa_buf;
5089 nuser = 0;
5090
5091 xdrleft = listxattrs->lsxa_maxcount;
5092
5093 while (left > 0 && xdrleft > 0) {
5094 slen = strlen(sp);
5095
5096 /*
Alex Dewar4cce11fa2020-08-17 18:51:26 +01005097 * Check if this is a "user." attribute, skip it if not.
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005098 */
5099 if (strncmp(sp, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
5100 goto contloop;
5101
5102 slen -= XATTR_USER_PREFIX_LEN;
5103 xdrlen = 4 + ((slen + 3) & ~3);
5104 if (xdrlen > xdrleft) {
5105 if (count == 0) {
5106 /*
5107 * Can't even fit the first attribute name.
5108 */
5109 status = nfserr_toosmall;
5110 goto out;
5111 }
5112 eof = 0;
5113 goto wreof;
5114 }
5115
5116 left -= XATTR_USER_PREFIX_LEN;
5117 sp += XATTR_USER_PREFIX_LEN;
5118 if (nuser++ < offset)
5119 goto contloop;
5120
5121
5122 p = xdr_reserve_space(xdr, xdrlen);
5123 if (!p) {
5124 status = nfserr_resource;
5125 goto out;
5126 }
5127
Alex Deware2a18402020-08-12 15:12:51 +01005128 xdr_encode_opaque(p, sp, slen);
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005129
5130 xdrleft -= xdrlen;
5131 count++;
5132contloop:
5133 sp += slen + 1;
5134 left -= slen + 1;
5135 }
5136
5137 /*
5138 * If there were user attributes to copy, but we didn't copy
5139 * any, the offset was too large (e.g. the cookie was invalid).
5140 */
5141 if (nuser > 0 && count == 0) {
5142 status = nfserr_badcookie;
5143 goto out;
5144 }
5145
5146wreof:
5147 p = xdr_reserve_space(xdr, 4);
5148 if (!p) {
5149 status = nfserr_resource;
5150 goto out;
5151 }
5152 *p = cpu_to_be32(eof);
5153
5154 cookie = offset + count;
5155
5156 write_bytes_to_xdr_buf(xdr->buf, cookie_offset, &cookie, 8);
Chuck Leverb9a49232020-09-11 14:47:53 -04005157 tmp = cpu_to_be32(count);
5158 write_bytes_to_xdr_buf(xdr->buf, count_offset, &tmp, 4);
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005159out:
5160 if (listxattrs->lsxa_len)
5161 kvfree(listxattrs->lsxa_buf);
5162 return status;
5163}
5164
5165static __be32
5166nfsd4_encode_removexattr(struct nfsd4_compoundres *resp, __be32 nfserr,
5167 struct nfsd4_removexattr *removexattr)
5168{
5169 struct xdr_stream *xdr = &resp->xdr;
5170 __be32 *p;
5171
5172 p = xdr_reserve_space(xdr, 20);
5173 if (!p)
5174 return nfserr_resource;
5175
5176 p = encode_cinfo(p, &removexattr->rmxa_cinfo);
5177 return 0;
5178}
5179
Benny Halevy695e12f2008-07-04 14:38:33 +03005180typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
5181
Andy Adamson2db134e2009-04-03 08:27:55 +03005182/*
5183 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
5184 * since we don't need to filter out obsolete ops as this is
5185 * done in the decoding phase.
5186 */
Chuck Leverc1df6092017-08-01 11:59:58 -04005187static const nfsd4_enc nfsd4_enc_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04005188 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
5189 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
5190 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
5191 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
5192 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
5193 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
5194 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
5195 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
5196 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
5197 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
5198 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
5199 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
5200 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
5201 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
5202 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
5203 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
Benny Halevy84f09f42009-03-04 23:05:35 +02005204 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04005205 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
5206 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
5207 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
5208 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
5209 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
5210 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
5211 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
5212 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
5213 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
5214 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
5215 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
5216 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
5217 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
5218 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
5219 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
5220 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
5221 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
5222 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
5223 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
5224 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03005225
5226 /* NFSv4.1 operations */
5227 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04005228 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
Andy Adamson2db134e2009-04-03 08:27:55 +03005229 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
5230 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
Kinglong Mee43212cc2013-12-09 19:04:23 +08005231 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_noop,
5232 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03005233 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02005234#ifdef CONFIG_NFSD_PNFS
5235 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_getdeviceinfo,
5236 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
5237 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_layoutcommit,
5238 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_layoutget,
5239 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_layoutreturn,
5240#else
Andy Adamson2db134e2009-04-03 08:27:55 +03005241 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
5242 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
5243 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
5244 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
5245 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02005246#endif
Mi Jinlong22b6dee2010-12-27 14:29:57 +08005247 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
Andy Adamson2db134e2009-04-03 08:27:55 +03005248 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
5249 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
Bryan Schumaker17456802011-07-13 10:50:48 -04005250 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
Andy Adamson2db134e2009-04-03 08:27:55 +03005251 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
5252 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
5253 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
Anna Schumaker87a15a82014-09-26 13:58:26 -04005254
5255 /* NFSv4.2 operations */
5256 [OP_ALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
Anna Schumaker29ae7f92016-09-07 15:57:30 -04005257 [OP_COPY] = (nfsd4_enc)nfsd4_encode_copy,
Olga Kornievskaia51911862019-08-08 11:14:59 -04005258 [OP_COPY_NOTIFY] = (nfsd4_enc)nfsd4_encode_copy_notify,
Anna Schumaker87a15a82014-09-26 13:58:26 -04005259 [OP_DEALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
5260 [OP_IO_ADVISE] = (nfsd4_enc)nfsd4_encode_noop,
5261 [OP_LAYOUTERROR] = (nfsd4_enc)nfsd4_encode_noop,
5262 [OP_LAYOUTSTATS] = (nfsd4_enc)nfsd4_encode_noop,
5263 [OP_OFFLOAD_CANCEL] = (nfsd4_enc)nfsd4_encode_noop,
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04005264 [OP_OFFLOAD_STATUS] = (nfsd4_enc)nfsd4_encode_offload_status,
Anna Schumaker528b8492020-09-28 13:08:58 -04005265 [OP_READ_PLUS] = (nfsd4_enc)nfsd4_encode_read_plus,
Anna Schumaker24bab492014-09-26 13:58:27 -04005266 [OP_SEEK] = (nfsd4_enc)nfsd4_encode_seek,
Anna Schumaker87a15a82014-09-26 13:58:26 -04005267 [OP_WRITE_SAME] = (nfsd4_enc)nfsd4_encode_noop,
Christoph Hellwigffa01602015-12-03 12:59:52 +01005268 [OP_CLONE] = (nfsd4_enc)nfsd4_encode_noop,
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005269
5270 /* RFC 8276 extended atributes operations */
5271 [OP_GETXATTR] = (nfsd4_enc)nfsd4_encode_getxattr,
5272 [OP_SETXATTR] = (nfsd4_enc)nfsd4_encode_setxattr,
5273 [OP_LISTXATTRS] = (nfsd4_enc)nfsd4_encode_listxattrs,
5274 [OP_REMOVEXATTR] = (nfsd4_enc)nfsd4_encode_removexattr,
Benny Halevy695e12f2008-07-04 14:38:33 +03005275};
5276
Andy Adamson496c2622009-04-03 08:28:48 +03005277/*
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04005278 * Calculate whether we still have space to encode repsize bytes.
5279 * There are two considerations:
5280 * - For NFS versions >=4.1, the size of the reply must stay within
5281 * session limits
5282 * - For all NFS versions, we must stay within limited preallocated
5283 * buffer space.
Andy Adamson496c2622009-04-03 08:28:48 +03005284 *
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04005285 * This is called before the operation is processed, so can only provide
5286 * an upper estimate. For some nonidempotent operations (such as
5287 * getattr), it's not necessarily a problem if that estimate is wrong,
5288 * as we can fail it after processing without significant side effects.
Andy Adamson496c2622009-04-03 08:28:48 +03005289 */
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04005290__be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 respsize)
Andy Adamson496c2622009-04-03 08:28:48 +03005291{
J. Bruce Fields67492c92014-03-08 16:42:05 -05005292 struct xdr_buf *buf = &resp->rqstp->rq_res;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04005293 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson496c2622009-04-03 08:28:48 +03005294
J. Bruce Fields47ee5292014-03-12 21:39:35 -04005295 if (buf->len + respsize <= buf->buflen)
5296 return nfs_ok;
5297 if (!nfsd4_has_session(&resp->cstate))
J. Bruce Fieldsea8d7722014-03-08 17:19:55 -05005298 return nfserr_resource;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04005299 if (slot->sl_flags & NFSD4_SLOT_CACHETHIS) {
5300 WARN_ON_ONCE(1);
5301 return nfserr_rep_too_big_to_cache;
J. Bruce Fieldsea8d7722014-03-08 17:19:55 -05005302 }
J. Bruce Fields47ee5292014-03-12 21:39:35 -04005303 return nfserr_rep_too_big;
Andy Adamson496c2622009-04-03 08:28:48 +03005304}
5305
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306void
5307nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
5308{
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005309 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005310 struct nfs4_stateowner *so = resp->cstate.replay_owner;
J. Bruce Fields5f4ab942014-03-07 11:01:37 -05005311 struct svc_rqst *rqstp = resp->rqstp;
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04005312 const struct nfsd4_operation *opdesc = op->opdesc;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005313 int post_err_offset;
J. Bruce Fields07d1f802014-03-06 20:39:29 -05005314 nfsd4_enc encoder;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07005315 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05005317 p = xdr_reserve_space(xdr, 8);
5318 if (!p) {
5319 WARN_ON_ONCE(1);
5320 return;
5321 }
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04005322 *p++ = cpu_to_be32(op->opnum);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005323 post_err_offset = xdr->buf->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324
Benny Halevy695e12f2008-07-04 14:38:33 +03005325 if (op->opnum == OP_ILLEGAL)
5326 goto status;
J. Bruce Fieldsb7571e42017-05-06 10:49:21 -04005327 if (op->status && opdesc &&
5328 !(opdesc->op_flags & OP_NONTRIVIAL_ERROR_ENCODE))
5329 goto status;
Chuck Lever3a237b42020-11-22 12:49:52 -05005330 BUG_ON(op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
Benny Halevy695e12f2008-07-04 14:38:33 +03005331 !nfsd4_enc_ops[op->opnum]);
J. Bruce Fields07d1f802014-03-06 20:39:29 -05005332 encoder = nfsd4_enc_ops[op->opnum];
5333 op->status = encoder(resp, op->status, &op->u);
Chuck Lever08281342020-11-21 11:36:42 -05005334 if (op->status)
5335 trace_nfsd_compound_encode_err(rqstp, op->opnum, op->status);
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04005336 if (opdesc && opdesc->op_release)
5337 opdesc->op_release(&op->u);
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04005338 xdr_commit_encode(xdr);
5339
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04005340 /* nfsd4_check_resp_size guarantees enough room for error status */
J. Bruce Fields5f4ab942014-03-07 11:01:37 -05005341 if (!op->status) {
5342 int space_needed = 0;
5343 if (!nfsd4_last_compound_op(rqstp))
5344 space_needed = COMPOUND_ERR_SLACK_SPACE;
5345 op->status = nfsd4_check_resp_size(resp, space_needed);
5346 }
J. Bruce Fieldsc8f13d92014-05-08 17:38:18 -04005347 if (op->status == nfserr_resource && nfsd4_has_session(&resp->cstate)) {
5348 struct nfsd4_slot *slot = resp->cstate.slot;
5349
5350 if (slot->sl_flags & NFSD4_SLOT_CACHETHIS)
5351 op->status = nfserr_rep_too_big_to_cache;
5352 else
5353 op->status = nfserr_rep_too_big;
5354 }
J. Bruce Fields07d1f802014-03-06 20:39:29 -05005355 if (op->status == nfserr_resource ||
5356 op->status == nfserr_rep_too_big ||
5357 op->status == nfserr_rep_too_big_to_cache) {
5358 /*
5359 * The operation may have already been encoded or
5360 * partially encoded. No op returns anything additional
5361 * in the case of one of these three errors, so we can
5362 * just truncate back to after the status. But it's a
5363 * bug if we had to do this on a non-idempotent op:
5364 */
5365 warn_on_nonidempotent_op(op);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005366 xdr_truncate_encode(xdr, post_err_offset);
J. Bruce Fields07d1f802014-03-06 20:39:29 -05005367 }
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005368 if (so) {
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005369 int len = xdr->buf->len - post_err_offset;
5370
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005371 so->so_replay.rp_status = op->status;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005372 so->so_replay.rp_buflen = len;
5373 read_bytes_from_xdr_buf(xdr->buf, post_err_offset,
5374 so->so_replay.rp_buf, len);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005375 }
Benny Halevy695e12f2008-07-04 14:38:33 +03005376status:
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005377 /* Note that op->status is already in network byte order: */
5378 write_bytes_to_xdr_buf(xdr->buf, post_err_offset - 4, &op->status, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379}
5380
5381/*
5382 * Encode the reply stored in the stateowner reply cache
5383 *
5384 * XDR note: do not encode rp->rp_buflen: the buffer contains the
5385 * previously sent already encoded operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 */
5387void
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05005388nfsd4_encode_replay(struct xdr_stream *xdr, struct nfsd4_op *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07005390 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 struct nfs4_replay *rp = op->replay;
5392
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05005393 p = xdr_reserve_space(xdr, 8 + rp->rp_buflen);
5394 if (!p) {
5395 WARN_ON_ONCE(1);
5396 return;
5397 }
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04005398 *p++ = cpu_to_be32(op->opnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 *p++ = rp->rp_status; /* already xdr'ed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04005401 p = xdr_encode_opaque_fixed(p, rp->rp_buf, rp->rp_buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402}
5403
Christoph Hellwig85374882017-05-08 18:48:24 +02005404void nfsd4_release_compoundargs(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405{
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04005406 struct nfsd4_compoundargs *args = rqstp->rq_argp;
5407
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 if (args->ops != args->iops) {
5409 kfree(args->ops);
5410 args->ops = args->iops;
5411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 while (args->to_free) {
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -04005413 struct svcxdr_tmpbuf *tb = args->to_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 args->to_free = tb->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 kfree(tb);
5416 }
5417}
5418
5419int
Christoph Hellwig026fec72017-05-08 19:01:48 +02005420nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421{
Christoph Hellwig026fec72017-05-08 19:01:48 +02005422 struct nfsd4_compoundargs *args = rqstp->rq_argp;
5423
Chuck Leverc1346a122020-11-03 11:54:23 -05005424 /* svcxdr_tmp_alloc */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 args->to_free = NULL;
Chuck Leverc1346a122020-11-03 11:54:23 -05005426
5427 args->xdr = &rqstp->rq_arg_stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 args->ops = args->iops;
5429 args->rqstp = rqstp;
5430
Chuck Leverd9b74bd2020-11-04 11:07:06 -05005431 return nfsd4_decode_compound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432}
5433
5434int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02005435nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02005437 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields6ac90392014-02-26 17:39:35 -05005438 struct xdr_buf *buf = resp->xdr.buf;
5439
5440 WARN_ON_ONCE(buf->len != buf->head[0].iov_len + buf->page_len +
5441 buf->tail[0].iov_len);
J. Bruce Fieldsdd97fdd2014-02-26 17:16:27 -05005442
Chuck Levercc028a12020-10-02 15:52:44 -04005443 *p = resp->cstate.status;
5444
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04005445 rqstp->rq_next_page = resp->xdr.page_ptr + 1;
5446
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 p = resp->tagp;
5448 *p++ = htonl(resp->taglen);
5449 memcpy(p, resp->tag, resp->taglen);
5450 p += XDR_QUADLEN(resp->taglen);
5451 *p++ = htonl(resp->opcnt);
5452
Trond Myklebustb6076642014-06-30 11:48:35 -04005453 nfsd4_sequence_done(resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 return 1;
5455}
5456
5457/*
5458 * Local variables:
5459 * c-basic-offset: 8
5460 * End:
5461 */