blob: 5a93a5db4fb0a5ecf1fa0da4d3e1a011f4dd0de0 [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 Lever7b723002020-12-18 12:28:58 -0500150static void *
151svcxdr_savemem(struct nfsd4_compoundargs *argp, __be32 *p, u32 len)
152{
153 __be32 *tmp;
154
155 /*
156 * The location of the decoded data item is stable,
157 * so @p is OK to use. This is the common case.
158 */
159 if (p != argp->xdr->scratch.iov_base)
160 return p;
161
162 tmp = svcxdr_tmpalloc(argp, len);
163 if (!tmp)
164 return NULL;
165 memcpy(tmp, p, len);
166 return tmp;
167}
168
Chuck Lever5dcbfab2020-11-04 11:41:55 -0500169/*
170 * NFSv4 basic data type decoders
171 */
172
Chuck Lever1a994402020-11-03 13:09:34 -0500173/*
174 * This helper handles variable-length opaques which belong to protocol
175 * elements that this implementation does not support.
176 */
177static __be32
178nfsd4_decode_ignored_string(struct nfsd4_compoundargs *argp, u32 maxlen)
179{
180 u32 len;
181
182 if (xdr_stream_decode_u32(argp->xdr, &len) < 0)
183 return nfserr_bad_xdr;
184 if (maxlen && len > maxlen)
185 return nfserr_bad_xdr;
186 if (!xdr_inline_decode(argp->xdr, len))
187 return nfserr_bad_xdr;
188
189 return nfs_ok;
190}
191
Chuck Lever5dcbfab2020-11-04 11:41:55 -0500192static __be32
193nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
194{
195 __be32 *p;
196 u32 len;
197
198 if (xdr_stream_decode_u32(argp->xdr, &len) < 0)
199 return nfserr_bad_xdr;
200 if (len == 0 || len > NFS4_OPAQUE_LIMIT)
201 return nfserr_bad_xdr;
202 p = xdr_inline_decode(argp->xdr, len);
203 if (!p)
204 return nfserr_bad_xdr;
Chuck Lever7b723002020-12-18 12:28:58 -0500205 o->data = svcxdr_savemem(argp, p, len);
Chuck Lever5dcbfab2020-11-04 11:41:55 -0500206 if (!o->data)
207 return nfserr_jukebox;
208 o->len = len;
Chuck Lever5dcbfab2020-11-04 11:41:55 -0500209
210 return nfs_ok;
211}
212
Christoph Hellwig4c94e132015-01-22 12:09:50 +0100213static __be32
Chuck Lever000dfa12020-11-03 13:24:10 -0500214nfsd4_decode_component4(struct nfsd4_compoundargs *argp, char **namp, u32 *lenp)
215{
216 __be32 *p, status;
217
218 if (xdr_stream_decode_u32(argp->xdr, lenp) < 0)
219 return nfserr_bad_xdr;
220 p = xdr_inline_decode(argp->xdr, *lenp);
221 if (!p)
222 return nfserr_bad_xdr;
223 status = check_filename((char *)p, *lenp);
224 if (status)
225 return status;
Chuck Lever7b723002020-12-18 12:28:58 -0500226 *namp = svcxdr_savemem(argp, p, *lenp);
Chuck Lever000dfa12020-11-03 13:24:10 -0500227 if (!*namp)
228 return nfserr_jukebox;
Chuck Lever000dfa12020-11-03 13:24:10 -0500229
230 return nfs_ok;
231}
232
233static __be32
Chuck Lever1c3eff72020-11-19 14:01:08 -0500234nfsd4_decode_nfstime4(struct nfsd4_compoundargs *argp, struct timespec64 *tv)
235{
236 __be32 *p;
237
238 p = xdr_inline_decode(argp->xdr, XDR_UNIT * 3);
239 if (!p)
240 return nfserr_bad_xdr;
241 p = xdr_decode_hyper(p, &tv->tv_sec);
242 tv->tv_nsec = be32_to_cpup(p++);
243 if (tv->tv_nsec >= (u32)1000000000)
244 return nfserr_inval;
245 return nfs_ok;
246}
247
248static __be32
Chuck Lever796dd1c2020-11-16 17:34:01 -0500249nfsd4_decode_verifier4(struct nfsd4_compoundargs *argp, nfs4_verifier *verf)
250{
251 __be32 *p;
252
253 p = xdr_inline_decode(argp->xdr, NFS4_VERIFIER_SIZE);
254 if (!p)
255 return nfserr_bad_xdr;
256 memcpy(verf->data, p, sizeof(verf->data));
257 return nfs_ok;
258}
259
Chuck Leverd1c263a2020-11-03 12:56:05 -0500260/**
261 * nfsd4_decode_bitmap4 - Decode an NFSv4 bitmap4
262 * @argp: NFSv4 compound argument structure
263 * @bmval: pointer to an array of u32's to decode into
264 * @bmlen: size of the @bmval array
265 *
266 * The server needs to return nfs_ok rather than nfserr_bad_xdr when
267 * encountering bitmaps containing bits it does not recognize. This
268 * includes bits in bitmap words past WORDn, where WORDn is the last
269 * bitmap WORD the implementation currently supports. Thus we are
270 * careful here to simply ignore bits in bitmap words that this
271 * implementation has yet to support explicitly.
272 *
273 * Return values:
274 * %nfs_ok: @bmval populated successfully
275 * %nfserr_bad_xdr: the encoded bitmap was invalid
276 */
277static __be32
278nfsd4_decode_bitmap4(struct nfsd4_compoundargs *argp, u32 *bmval, u32 bmlen)
279{
280 u32 i, count;
281 __be32 *p;
282
283 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
284 return nfserr_bad_xdr;
285 /* request sanity */
286 if (count > 1000)
287 return nfserr_bad_xdr;
288 p = xdr_inline_decode(argp->xdr, count << 2);
289 if (!p)
290 return nfserr_bad_xdr;
Chuck Leverc0019b72021-11-14 15:16:04 -0500291 for (i = 0; i < bmlen; i++)
292 bmval[i] = (i < count) ? be32_to_cpup(p++) : 0;
Chuck Leverd1c263a2020-11-03 12:56:05 -0500293
294 return nfs_ok;
295}
296
Al Virob37ad282006-10-19 23:28:59 -0700297static __be32
Chuck Leverc941a962020-11-19 13:02:54 -0500298nfsd4_decode_nfsace4(struct nfsd4_compoundargs *argp, struct nfs4_ace *ace)
299{
300 __be32 *p, status;
301 u32 length;
302
303 if (xdr_stream_decode_u32(argp->xdr, &ace->type) < 0)
304 return nfserr_bad_xdr;
305 if (xdr_stream_decode_u32(argp->xdr, &ace->flag) < 0)
306 return nfserr_bad_xdr;
307 if (xdr_stream_decode_u32(argp->xdr, &ace->access_mask) < 0)
308 return nfserr_bad_xdr;
309
310 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
311 return nfserr_bad_xdr;
312 p = xdr_inline_decode(argp->xdr, length);
313 if (!p)
314 return nfserr_bad_xdr;
315 ace->whotype = nfs4_acl_get_whotype((char *)p, length);
316 if (ace->whotype != NFS4_ACL_WHO_NAMED)
317 status = nfs_ok;
318 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
319 status = nfsd_map_name_to_gid(argp->rqstp,
320 (char *)p, length, &ace->who_gid);
321 else
322 status = nfsd_map_name_to_uid(argp->rqstp,
323 (char *)p, length, &ace->who_uid);
324
325 return status;
326}
327
328/* A counted array of nfsace4's */
329static noinline __be32
330nfsd4_decode_acl(struct nfsd4_compoundargs *argp, struct nfs4_acl **acl)
331{
332 struct nfs4_ace *ace;
333 __be32 status;
334 u32 count;
335
336 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
337 return nfserr_bad_xdr;
338
339 if (count > xdr_stream_remaining(argp->xdr) / 20)
340 /*
341 * Even with 4-byte names there wouldn't be
342 * space for that many aces; something fishy is
343 * going on:
344 */
345 return nfserr_fbig;
346
347 *acl = svcxdr_tmpalloc(argp, nfs4_acl_bytes(count));
348 if (*acl == NULL)
349 return nfserr_jukebox;
350
351 (*acl)->naces = count;
352 for (ace = (*acl)->aces; ace < (*acl)->aces + count; ace++) {
353 status = nfsd4_decode_nfsace4(argp, ace);
354 if (status)
355 return status;
356 }
357
358 return nfs_ok;
359}
360
Chuck Leverdabe9182020-11-19 14:05:51 -0500361static noinline __be32
362nfsd4_decode_security_label(struct nfsd4_compoundargs *argp,
363 struct xdr_netobj *label)
364{
365 u32 lfs, pi, length;
366 __be32 *p;
367
368 if (xdr_stream_decode_u32(argp->xdr, &lfs) < 0)
369 return nfserr_bad_xdr;
370 if (xdr_stream_decode_u32(argp->xdr, &pi) < 0)
371 return nfserr_bad_xdr;
372
373 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
374 return nfserr_bad_xdr;
375 if (length > NFS4_MAXLABELLEN)
376 return nfserr_badlabel;
377 p = xdr_inline_decode(argp->xdr, length);
378 if (!p)
379 return nfserr_bad_xdr;
380 label->len = length;
381 label->data = svcxdr_dupstr(argp, p, length);
382 if (!label->data)
383 return nfserr_jukebox;
384
385 return nfs_ok;
386}
387
Chuck Leverc941a962020-11-19 13:02:54 -0500388static __be32
Chuck Leverd1c263a2020-11-03 12:56:05 -0500389nfsd4_decode_fattr4(struct nfsd4_compoundargs *argp, u32 *bmval, u32 bmlen,
390 struct iattr *iattr, struct nfs4_acl **acl,
391 struct xdr_netobj *label, int *umask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Chuck Lever081d53f2020-11-19 13:09:13 -0500393 unsigned int starting_pos;
394 u32 attrlist4_count;
Chuck Leverd1c263a2020-11-03 12:56:05 -0500395 __be32 *p, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 iattr->ia_valid = 0;
Chuck Leverd1c263a2020-11-03 12:56:05 -0500398 status = nfsd4_decode_bitmap4(argp, bmval, bmlen);
399 if (status)
400 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
J. Bruce Fieldse864c182016-06-10 16:56:05 -0400402 if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
403 || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
404 || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2) {
405 if (nfsd_attrs_supported(argp->minorversion, bmval))
406 return nfserr_inval;
407 return nfserr_attrnotsupp;
408 }
409
Chuck Lever081d53f2020-11-19 13:09:13 -0500410 if (xdr_stream_decode_u32(argp->xdr, &attrlist4_count) < 0)
411 return nfserr_bad_xdr;
412 starting_pos = xdr_stream_pos(argp->xdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 if (bmval[0] & FATTR4_WORD0_SIZE) {
Chuck Lever2ac1b9b2020-11-19 13:47:16 -0500415 u64 size;
416
417 if (xdr_stream_decode_u64(argp->xdr, &size) < 0)
418 return nfserr_bad_xdr;
419 iattr->ia_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 iattr->ia_valid |= ATTR_SIZE;
421 }
422 if (bmval[0] & FATTR4_WORD0_ACL) {
Chuck Leverc941a962020-11-19 13:02:54 -0500423 status = nfsd4_decode_acl(argp, acl);
424 if (status)
425 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 } else
427 *acl = NULL;
428 if (bmval[1] & FATTR4_WORD1_MODE) {
Chuck Lever1c8f0ad2020-11-19 13:54:26 -0500429 u32 mode;
430
431 if (xdr_stream_decode_u32(argp->xdr, &mode) < 0)
432 return nfserr_bad_xdr;
433 iattr->ia_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 iattr->ia_mode &= (S_IFMT | S_IALLUGO);
435 iattr->ia_valid |= ATTR_MODE;
436 }
437 if (bmval[1] & FATTR4_WORD1_OWNER) {
Chuck Lever9853a5a2020-11-19 13:56:42 -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_uid(argp->rqstp, (char *)p, length,
446 &iattr->ia_uid);
447 if (status)
NeilBrown47c85292011-02-16 13:08:35 +1100448 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 iattr->ia_valid |= ATTR_UID;
450 }
451 if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
Chuck Lever393c31d2020-11-19 13:58:18 -0500452 u32 length;
453
454 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
455 return nfserr_bad_xdr;
456 p = xdr_inline_decode(argp->xdr, length);
457 if (!p)
458 return nfserr_bad_xdr;
459 status = nfsd_map_name_to_gid(argp->rqstp, (char *)p, length,
460 &iattr->ia_gid);
461 if (status)
NeilBrown47c85292011-02-16 13:08:35 +1100462 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 iattr->ia_valid |= ATTR_GID;
464 }
465 if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
Chuck Lever1c3eff72020-11-19 14:01:08 -0500466 u32 set_it;
467
468 if (xdr_stream_decode_u32(argp->xdr, &set_it) < 0)
469 return nfserr_bad_xdr;
470 switch (set_it) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 case NFS4_SET_TO_CLIENT_TIME:
Chuck Lever1c3eff72020-11-19 14:01:08 -0500472 status = nfsd4_decode_nfstime4(argp, &iattr->ia_atime);
Christoph Hellwig4c94e132015-01-22 12:09:50 +0100473 if (status)
474 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
476 break;
477 case NFS4_SET_TO_SERVER_TIME:
478 iattr->ia_valid |= ATTR_ATIME;
479 break;
480 default:
Chuck Lever1c3eff72020-11-19 14:01:08 -0500481 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
Chuck Lever1c3eff72020-11-19 14:01:08 -0500485 u32 set_it;
486
487 if (xdr_stream_decode_u32(argp->xdr, &set_it) < 0)
488 return nfserr_bad_xdr;
489 switch (set_it) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 case NFS4_SET_TO_CLIENT_TIME:
Chuck Lever1c3eff72020-11-19 14:01:08 -0500491 status = nfsd4_decode_nfstime4(argp, &iattr->ia_mtime);
Christoph Hellwig4c94e132015-01-22 12:09:50 +0100492 if (status)
493 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
495 break;
496 case NFS4_SET_TO_SERVER_TIME:
497 iattr->ia_valid |= ATTR_MTIME;
498 break;
499 default:
Chuck Lever1c3eff72020-11-19 14:01:08 -0500500 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502 }
David Quigley18032ca2013-05-02 13:19:10 -0400503 label->len = 0;
Arnd Bergmann2285ae72018-01-22 22:09:12 +0100504 if (IS_ENABLED(CONFIG_NFSD_V4_SECURITY_LABEL) &&
505 bmval[2] & FATTR4_WORD2_SECURITY_LABEL) {
Chuck Leverdabe9182020-11-19 14:05:51 -0500506 status = nfsd4_decode_security_label(argp, label);
507 if (status)
508 return status;
David Quigley18032ca2013-05-02 13:19:10 -0400509 }
Andreas Gruenbacher47057ab2016-01-12 20:24:14 +0100510 if (bmval[2] & FATTR4_WORD2_MODE_UMASK) {
Chuck Lever66f04762020-11-19 14:07:43 -0500511 u32 mode, mask;
512
Andreas Gruenbacher47057ab2016-01-12 20:24:14 +0100513 if (!umask)
Chuck Lever66f04762020-11-19 14:07:43 -0500514 return nfserr_bad_xdr;
515 if (xdr_stream_decode_u32(argp->xdr, &mode) < 0)
516 return nfserr_bad_xdr;
517 iattr->ia_mode = mode & (S_IFMT | S_IALLUGO);
518 if (xdr_stream_decode_u32(argp->xdr, &mask) < 0)
519 return nfserr_bad_xdr;
520 *umask = mask & S_IRWXUGO;
Andreas Gruenbacher47057ab2016-01-12 20:24:14 +0100521 iattr->ia_valid |= ATTR_MODE;
522 }
Chuck Lever081d53f2020-11-19 13:09:13 -0500523
524 /* request sanity: did attrlist4 contain the expected number of words? */
525 if (attrlist4_count != xdr_stream_pos(argp->xdr) - starting_pos)
526 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Chuck Leverd1c263a2020-11-03 12:56:05 -0500528 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
Al Virob37ad282006-10-19 23:28:59 -0700531static __be32
Chuck Leverd3d2f382020-11-03 13:18:23 -0500532nfsd4_decode_stateid4(struct nfsd4_compoundargs *argp, stateid_t *sid)
533{
534 __be32 *p;
535
536 p = xdr_inline_decode(argp->xdr, NFS4_STATEID_SIZE);
537 if (!p)
538 return nfserr_bad_xdr;
539 sid->si_generation = be32_to_cpup(p++);
540 memcpy(&sid->si_opaque, p, sizeof(sid->si_opaque));
541 return nfs_ok;
542}
543
Chuck Lever144e8262020-11-16 17:25:02 -0500544static __be32
545nfsd4_decode_clientid4(struct nfsd4_compoundargs *argp, clientid_t *clientid)
546{
547 __be32 *p;
548
549 p = xdr_inline_decode(argp->xdr, sizeof(__be64));
550 if (!p)
551 return nfserr_bad_xdr;
552 memcpy(clientid, p, sizeof(*clientid));
553 return nfs_ok;
554}
555
556static __be32
557nfsd4_decode_state_owner4(struct nfsd4_compoundargs *argp,
558 clientid_t *clientid, struct xdr_netobj *owner)
559{
560 __be32 status;
561
562 status = nfsd4_decode_clientid4(argp, clientid);
563 if (status)
564 return status;
565 return nfsd4_decode_opaque(argp, owner);
566}
567
Chuck Lever04495972020-11-03 15:03:50 -0500568#ifdef CONFIG_NFSD_PNFS
569static __be32
570nfsd4_decode_deviceid4(struct nfsd4_compoundargs *argp,
571 struct nfsd4_deviceid *devid)
572{
573 __be32 *p;
574
575 p = xdr_inline_decode(argp->xdr, NFS4_DEVICEID4_SIZE);
576 if (!p)
577 return nfserr_bad_xdr;
578 memcpy(devid, p, sizeof(*devid));
579 return nfs_ok;
580}
Chuck Lever51859802020-11-04 10:40:07 -0500581
582static __be32
583nfsd4_decode_layoutupdate4(struct nfsd4_compoundargs *argp,
584 struct nfsd4_layoutcommit *lcp)
585{
586 if (xdr_stream_decode_u32(argp->xdr, &lcp->lc_layout_type) < 0)
587 return nfserr_bad_xdr;
588 if (lcp->lc_layout_type < LAYOUT_NFSV4_1_FILES)
589 return nfserr_bad_xdr;
590 if (lcp->lc_layout_type >= LAYOUT_TYPE_MAX)
591 return nfserr_bad_xdr;
592
593 if (xdr_stream_decode_u32(argp->xdr, &lcp->lc_up_len) < 0)
594 return nfserr_bad_xdr;
595 if (lcp->lc_up_len > 0) {
596 lcp->lc_up_layout = xdr_inline_decode(argp->xdr, lcp->lc_up_len);
597 if (!lcp->lc_up_layout)
598 return nfserr_bad_xdr;
599 }
600
601 return nfs_ok;
602}
603
Chuck Lever645fcad2020-11-04 10:42:25 -0500604static __be32
605nfsd4_decode_layoutreturn4(struct nfsd4_compoundargs *argp,
606 struct nfsd4_layoutreturn *lrp)
607{
608 __be32 status;
609
610 if (xdr_stream_decode_u32(argp->xdr, &lrp->lr_return_type) < 0)
611 return nfserr_bad_xdr;
612 switch (lrp->lr_return_type) {
613 case RETURN_FILE:
614 if (xdr_stream_decode_u64(argp->xdr, &lrp->lr_seg.offset) < 0)
615 return nfserr_bad_xdr;
616 if (xdr_stream_decode_u64(argp->xdr, &lrp->lr_seg.length) < 0)
617 return nfserr_bad_xdr;
618 status = nfsd4_decode_stateid4(argp, &lrp->lr_sid);
619 if (status)
620 return status;
621 if (xdr_stream_decode_u32(argp->xdr, &lrp->lrf_body_len) < 0)
622 return nfserr_bad_xdr;
623 if (lrp->lrf_body_len > 0) {
624 lrp->lrf_body = xdr_inline_decode(argp->xdr, lrp->lrf_body_len);
625 if (!lrp->lrf_body)
626 return nfserr_bad_xdr;
627 }
628 break;
629 case RETURN_FSID:
630 case RETURN_ALL:
631 lrp->lr_seg.offset = 0;
632 lrp->lr_seg.length = NFS4_MAX_UINT64;
633 break;
634 default:
635 return nfserr_bad_xdr;
636 }
637
638 return nfs_ok;
639}
640
Chuck Lever04495972020-11-03 15:03:50 -0500641#endif /* CONFIG_NFSD_PNFS */
642
Chuck Lever571e0452020-11-03 13:16:23 -0500643static __be32
644nfsd4_decode_sessionid4(struct nfsd4_compoundargs *argp,
645 struct nfs4_sessionid *sessionid)
646{
647 __be32 *p;
648
649 p = xdr_inline_decode(argp->xdr, NFS4_MAX_SESSIONID_LEN);
650 if (!p)
651 return nfserr_bad_xdr;
652 memcpy(sessionid->data, p, sizeof(sessionid->data));
653 return nfs_ok;
654}
655
Chuck Lever1a994402020-11-03 13:09:34 -0500656/* Defined in Appendix A of RFC 5531 */
657static __be32
658nfsd4_decode_authsys_parms(struct nfsd4_compoundargs *argp,
659 struct nfsd4_cb_sec *cbs)
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400660{
Chuck Lever1a994402020-11-03 13:09:34 -0500661 u32 stamp, gidcount, uid, gid;
662 __be32 *p, status;
663
664 if (xdr_stream_decode_u32(argp->xdr, &stamp) < 0)
665 return nfserr_bad_xdr;
666 /* machine name */
667 status = nfsd4_decode_ignored_string(argp, 255);
668 if (status)
669 return status;
670 if (xdr_stream_decode_u32(argp->xdr, &uid) < 0)
671 return nfserr_bad_xdr;
672 if (xdr_stream_decode_u32(argp->xdr, &gid) < 0)
673 return nfserr_bad_xdr;
674 if (xdr_stream_decode_u32(argp->xdr, &gidcount) < 0)
675 return nfserr_bad_xdr;
676 if (gidcount > 16)
677 return nfserr_bad_xdr;
678 p = xdr_inline_decode(argp->xdr, gidcount << 2);
679 if (!p)
680 return nfserr_bad_xdr;
681 if (cbs->flavor == (u32)(-1)) {
682 struct user_namespace *userns = nfsd_user_namespace(argp->rqstp);
683
684 kuid_t kuid = make_kuid(userns, uid);
685 kgid_t kgid = make_kgid(userns, gid);
686 if (uid_valid(kuid) && gid_valid(kgid)) {
687 cbs->uid = kuid;
688 cbs->gid = kgid;
689 cbs->flavor = RPC_AUTH_UNIX;
690 } else {
691 dprintk("RPC_AUTH_UNIX with invalid uid or gid, ignoring!\n");
692 }
693 }
694
695 return nfs_ok;
696}
697
698static __be32
699nfsd4_decode_gss_cb_handles4(struct nfsd4_compoundargs *argp,
700 struct nfsd4_cb_sec *cbs)
701{
702 __be32 status;
703 u32 service;
704
705 dprintk("RPC_AUTH_GSS callback secflavor not supported!\n");
706
707 if (xdr_stream_decode_u32(argp->xdr, &service) < 0)
708 return nfserr_bad_xdr;
709 if (service < RPC_GSS_SVC_NONE || service > RPC_GSS_SVC_PRIVACY)
710 return nfserr_bad_xdr;
711 /* gcbp_handle_from_server */
712 status = nfsd4_decode_ignored_string(argp, 0);
713 if (status)
714 return status;
715 /* gcbp_handle_from_client */
716 status = nfsd4_decode_ignored_string(argp, 0);
717 if (status)
718 return status;
719
720 return nfs_ok;
721}
722
723/* a counted array of callback_sec_parms4 items */
724static __be32
725nfsd4_decode_cb_sec(struct nfsd4_compoundargs *argp, struct nfsd4_cb_sec *cbs)
726{
727 u32 i, secflavor, nr_secflavs;
728 __be32 status;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400729
730 /* callback_sec_params4 */
Chuck Lever1a994402020-11-03 13:09:34 -0500731 if (xdr_stream_decode_u32(argp->xdr, &nr_secflavs) < 0)
732 return nfserr_bad_xdr;
J. Bruce Fields57569a72013-05-17 16:25:32 -0400733 if (nr_secflavs)
734 cbs->flavor = (u32)(-1);
735 else
736 /* Is this legal? Be generous, take it to mean AUTH_NONE: */
737 cbs->flavor = 0;
Chuck Lever1a994402020-11-03 13:09:34 -0500738
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400739 for (i = 0; i < nr_secflavs; ++i) {
Chuck Lever1a994402020-11-03 13:09:34 -0500740 if (xdr_stream_decode_u32(argp->xdr, &secflavor) < 0)
741 return nfserr_bad_xdr;
742 switch (secflavor) {
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400743 case RPC_AUTH_NULL:
Chuck Lever1a994402020-11-03 13:09:34 -0500744 /* void */
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500745 if (cbs->flavor == (u32)(-1))
746 cbs->flavor = RPC_AUTH_NULL;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400747 break;
748 case RPC_AUTH_UNIX:
Chuck Lever1a994402020-11-03 13:09:34 -0500749 status = nfsd4_decode_authsys_parms(argp, cbs);
750 if (status)
751 return status;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400752 break;
753 case RPC_AUTH_GSS:
Chuck Lever1a994402020-11-03 13:09:34 -0500754 status = nfsd4_decode_gss_cb_handles4(argp, cbs);
755 if (status)
756 return status;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400757 break;
758 default:
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400759 return nfserr_inval;
760 }
761 }
Chuck Lever1a994402020-11-03 13:09:34 -0500762
763 return nfs_ok;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400764}
765
Chuck Lever1a994402020-11-03 13:09:34 -0500766
Chuck Leverd169a6a2020-11-03 13:12:27 -0500767/*
768 * NFSv4 operation argument decoders
769 */
770
771static __be32
772nfsd4_decode_access(struct nfsd4_compoundargs *argp,
773 struct nfsd4_access *access)
774{
775 if (xdr_stream_decode_u32(argp->xdr, &access->ac_req_access) < 0)
776 return nfserr_bad_xdr;
777 return nfs_ok;
778}
779
Al Virob37ad282006-10-19 23:28:59 -0700780static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
782{
Chuck Leverd3d2f382020-11-03 13:18:23 -0500783 if (xdr_stream_decode_u32(argp->xdr, &close->cl_seqid) < 0)
784 return nfserr_bad_xdr;
785 return nfsd4_decode_stateid4(argp, &close->cl_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
787
788
Al Virob37ad282006-10-19 23:28:59 -0700789static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
791{
Chuck Levercbd9abb2020-11-03 13:19:51 -0500792 if (xdr_stream_decode_u64(argp->xdr, &commit->co_offset) < 0)
793 return nfserr_bad_xdr;
794 if (xdr_stream_decode_u32(argp->xdr, &commit->co_count) < 0)
795 return nfserr_bad_xdr;
796 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
Al Virob37ad282006-10-19 23:28:59 -0700799static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
801{
Chuck Lever000dfa12020-11-03 13:24:10 -0500802 __be32 *p, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Chuck Lever000dfa12020-11-03 13:24:10 -0500804 if (xdr_stream_decode_u32(argp->xdr, &create->cr_type) < 0)
805 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 switch (create->cr_type) {
807 case NF4LNK:
Chuck Lever000dfa12020-11-03 13:24:10 -0500808 if (xdr_stream_decode_u32(argp->xdr, &create->cr_datalen) < 0)
809 return nfserr_bad_xdr;
810 p = xdr_inline_decode(argp->xdr, create->cr_datalen);
811 if (!p)
812 return nfserr_bad_xdr;
J. Bruce Fields29c353b2014-06-24 17:06:51 -0400813 create->cr_data = svcxdr_dupstr(argp, p, create->cr_datalen);
J. Bruce Fields7fb84302014-06-24 15:06:41 -0400814 if (!create->cr_data)
J. Bruce Fields76f471282014-06-19 16:44:48 -0400815 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 break;
817 case NF4BLK:
818 case NF4CHR:
Chuck Lever000dfa12020-11-03 13:24:10 -0500819 if (xdr_stream_decode_u32(argp->xdr, &create->cr_specdata1) < 0)
820 return nfserr_bad_xdr;
821 if (xdr_stream_decode_u32(argp->xdr, &create->cr_specdata2) < 0)
822 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 break;
824 case NF4SOCK:
825 case NF4FIFO:
826 case NF4DIR:
827 default:
828 break;
829 }
Chuck Lever000dfa12020-11-03 13:24:10 -0500830 status = nfsd4_decode_component4(argp, &create->cr_name,
831 &create->cr_namelen);
832 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return status;
Chuck Leverd1c263a2020-11-03 12:56:05 -0500834 status = nfsd4_decode_fattr4(argp, create->cr_bmval,
835 ARRAY_SIZE(create->cr_bmval),
836 &create->cr_iattr, &create->cr_acl,
837 &create->cr_label, &create->cr_umask);
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300838 if (status)
Chuck Lever000dfa12020-11-03 13:24:10 -0500839 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Chuck Lever000dfa12020-11-03 13:24:10 -0500841 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
Al Virob37ad282006-10-19 23:28:59 -0700844static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
846{
Chuck Lever95e64822020-11-21 14:11:58 -0500847 return nfsd4_decode_stateid4(argp, &dr->dr_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
Al Virob37ad282006-10-19 23:28:59 -0700850static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
852{
Chuck Leverf759eff2020-11-19 14:40:20 -0500853 return nfsd4_decode_bitmap4(argp, getattr->ga_bmval,
854 ARRAY_SIZE(getattr->ga_bmval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
Al Virob37ad282006-10-19 23:28:59 -0700857static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
859{
Chuck Lever5c505d12020-11-04 15:01:24 -0500860 return nfsd4_decode_component4(argp, &link->li_name, &link->li_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
862
Al Virob37ad282006-10-19 23:28:59 -0700863static __be32
Chuck Lever8918cc02020-11-16 17:16:52 -0500864nfsd4_decode_open_to_lock_owner4(struct nfsd4_compoundargs *argp,
865 struct nfsd4_lock *lock)
866{
867 __be32 status;
868
869 if (xdr_stream_decode_u32(argp->xdr, &lock->lk_new_open_seqid) < 0)
870 return nfserr_bad_xdr;
871 status = nfsd4_decode_stateid4(argp, &lock->lk_new_open_stateid);
872 if (status)
873 return status;
874 if (xdr_stream_decode_u32(argp->xdr, &lock->lk_new_lock_seqid) < 0)
875 return nfserr_bad_xdr;
876 return nfsd4_decode_state_owner4(argp, &lock->lk_new_clientid,
877 &lock->lk_new_owner);
878}
879
880static __be32
881nfsd4_decode_exist_lock_owner4(struct nfsd4_compoundargs *argp,
882 struct nfsd4_lock *lock)
883{
884 __be32 status;
885
886 status = nfsd4_decode_stateid4(argp, &lock->lk_old_lock_stateid);
887 if (status)
888 return status;
889 if (xdr_stream_decode_u32(argp->xdr, &lock->lk_old_lock_seqid) < 0)
890 return nfserr_bad_xdr;
891
892 return nfs_ok;
893}
894
895static __be32
896nfsd4_decode_locker4(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
897{
898 if (xdr_stream_decode_bool(argp->xdr, &lock->lk_is_new) < 0)
899 return nfserr_bad_xdr;
900 if (lock->lk_is_new)
901 return nfsd4_decode_open_to_lock_owner4(argp, lock);
902 return nfsd4_decode_exist_lock_owner4(argp, lock);
903}
904
905static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
907{
Chuck Lever7c59dee2020-11-03 13:29:27 -0500908 if (xdr_stream_decode_u32(argp->xdr, &lock->lk_type) < 0)
909 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
Chuck Lever7c59dee2020-11-03 13:29:27 -0500911 return nfserr_bad_xdr;
912 if (xdr_stream_decode_bool(argp->xdr, &lock->lk_reclaim) < 0)
913 return nfserr_bad_xdr;
914 if (xdr_stream_decode_u64(argp->xdr, &lock->lk_offset) < 0)
915 return nfserr_bad_xdr;
916 if (xdr_stream_decode_u64(argp->xdr, &lock->lk_length) < 0)
917 return nfserr_bad_xdr;
918 return nfsd4_decode_locker4(argp, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
Al Virob37ad282006-10-19 23:28:59 -0700921static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
923{
Chuck Lever0a146f02020-11-03 13:31:44 -0500924 if (xdr_stream_decode_u32(argp->xdr, &lockt->lt_type) < 0)
925 return nfserr_bad_xdr;
926 if ((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
927 return nfserr_bad_xdr;
928 if (xdr_stream_decode_u64(argp->xdr, &lockt->lt_offset) < 0)
929 return nfserr_bad_xdr;
930 if (xdr_stream_decode_u64(argp->xdr, &lockt->lt_length) < 0)
931 return nfserr_bad_xdr;
932 return nfsd4_decode_state_owner4(argp, &lockt->lt_clientid,
933 &lockt->lt_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
Al Virob37ad282006-10-19 23:28:59 -0700936static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
938{
Chuck Leverca9cf9f2020-11-03 13:33:28 -0500939 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Chuck Leverca9cf9f2020-11-03 13:33:28 -0500941 if (xdr_stream_decode_u32(argp->xdr, &locku->lu_type) < 0)
942 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
Chuck Leverca9cf9f2020-11-03 13:33:28 -0500944 return nfserr_bad_xdr;
945 if (xdr_stream_decode_u32(argp->xdr, &locku->lu_seqid) < 0)
946 return nfserr_bad_xdr;
947 status = nfsd4_decode_stateid4(argp, &locku->lu_stateid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300948 if (status)
949 return status;
Chuck Leverca9cf9f2020-11-03 13:33:28 -0500950 if (xdr_stream_decode_u64(argp->xdr, &locku->lu_offset) < 0)
951 return nfserr_bad_xdr;
952 if (xdr_stream_decode_u64(argp->xdr, &locku->lu_length) < 0)
953 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Chuck Leverca9cf9f2020-11-03 13:33:28 -0500955 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
Al Virob37ad282006-10-19 23:28:59 -0700958static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
960{
Chuck Lever3d5877e2020-11-04 15:02:40 -0500961 return nfsd4_decode_component4(argp, &lookup->lo_name, &lookup->lo_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
963
Chuck Leverbf33bab2020-11-16 17:37:42 -0500964static __be32
965nfsd4_decode_createhow4(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
966{
967 __be32 status;
968
969 if (xdr_stream_decode_u32(argp->xdr, &open->op_createmode) < 0)
970 return nfserr_bad_xdr;
971 switch (open->op_createmode) {
972 case NFS4_CREATE_UNCHECKED:
973 case NFS4_CREATE_GUARDED:
974 status = nfsd4_decode_fattr4(argp, open->op_bmval,
975 ARRAY_SIZE(open->op_bmval),
976 &open->op_iattr, &open->op_acl,
977 &open->op_label, &open->op_umask);
978 if (status)
979 return status;
980 break;
981 case NFS4_CREATE_EXCLUSIVE:
982 status = nfsd4_decode_verifier4(argp, &open->op_verf);
983 if (status)
984 return status;
985 break;
986 case NFS4_CREATE_EXCLUSIVE4_1:
987 if (argp->minorversion < 1)
988 return nfserr_bad_xdr;
989 status = nfsd4_decode_verifier4(argp, &open->op_verf);
990 if (status)
991 return status;
992 status = nfsd4_decode_fattr4(argp, open->op_bmval,
993 ARRAY_SIZE(open->op_bmval),
994 &open->op_iattr, &open->op_acl,
995 &open->op_label, &open->op_umask);
996 if (status)
997 return status;
998 break;
999 default:
1000 return nfserr_bad_xdr;
1001 }
1002
1003 return nfs_ok;
1004}
1005
Chuck Levere6ec04b2020-11-16 17:41:21 -05001006static __be32
1007nfsd4_decode_openflag4(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
1008{
1009 __be32 status;
1010
1011 if (xdr_stream_decode_u32(argp->xdr, &open->op_create) < 0)
1012 return nfserr_bad_xdr;
1013 switch (open->op_create) {
1014 case NFS4_OPEN_NOCREATE:
1015 break;
1016 case NFS4_OPEN_CREATE:
1017 status = nfsd4_decode_createhow4(argp, open);
1018 if (status)
1019 return status;
1020 break;
1021 default:
1022 return nfserr_bad_xdr;
1023 }
1024
1025 return nfs_ok;
1026}
1027
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02001028static __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 -04001029{
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001030 u32 w;
1031
Chuck Lever9aa62f52020-11-16 17:54:48 -05001032 if (xdr_stream_decode_u32(argp->xdr, &w) < 0)
1033 return nfserr_bad_xdr;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02001034 *share_access = w & NFS4_SHARE_ACCESS_MASK;
1035 *deleg_want = w & NFS4_SHARE_WANT_MASK;
1036 if (deleg_when)
1037 *deleg_when = w & NFS4_SHARE_WHEN_MASK;
1038
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001039 switch (w & NFS4_SHARE_ACCESS_MASK) {
1040 case NFS4_SHARE_ACCESS_READ:
1041 case NFS4_SHARE_ACCESS_WRITE:
1042 case NFS4_SHARE_ACCESS_BOTH:
1043 break;
1044 default:
1045 return nfserr_bad_xdr;
1046 }
Benny Halevyfc0d14f2011-10-27 20:43:01 +02001047 w &= ~NFS4_SHARE_ACCESS_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001048 if (!w)
1049 return nfs_ok;
1050 if (!argp->minorversion)
1051 return nfserr_bad_xdr;
1052 switch (w & NFS4_SHARE_WANT_MASK) {
1053 case NFS4_SHARE_WANT_NO_PREFERENCE:
1054 case NFS4_SHARE_WANT_READ_DELEG:
1055 case NFS4_SHARE_WANT_WRITE_DELEG:
1056 case NFS4_SHARE_WANT_ANY_DELEG:
1057 case NFS4_SHARE_WANT_NO_DELEG:
1058 case NFS4_SHARE_WANT_CANCEL:
1059 break;
1060 default:
1061 return nfserr_bad_xdr;
1062 }
Benny Halevy92bac8c2011-10-19 19:13:29 -07001063 w &= ~NFS4_SHARE_WANT_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001064 if (!w)
1065 return nfs_ok;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02001066
1067 if (!deleg_when) /* open_downgrade */
1068 return nfserr_inval;
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001069 switch (w) {
1070 case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
1071 case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
Benny Halevyc668fc62011-10-19 19:13:13 -07001072 case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
1073 NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001074 return nfs_ok;
1075 }
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001076 return nfserr_bad_xdr;
1077}
1078
1079static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
1080{
Chuck Leverb07bebd2020-11-16 17:56:17 -05001081 if (xdr_stream_decode_u32(argp->xdr, x) < 0)
1082 return nfserr_bad_xdr;
1083 /* Note: unlike access bits, deny bits may be zero. */
Dan Carpenter01cd4af2011-10-17 10:41:17 +03001084 if (*x & ~NFS4_SHARE_DENY_BOTH)
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001085 return nfserr_bad_xdr;
Chuck Leverb07bebd2020-11-16 17:56:17 -05001086
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001087 return nfs_ok;
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001088}
1089
Al Virob37ad282006-10-19 23:28:59 -07001090static __be32
Chuck Lever1708e502020-11-16 17:45:04 -05001091nfsd4_decode_open_claim4(struct nfsd4_compoundargs *argp,
1092 struct nfsd4_open *open)
1093{
1094 __be32 status;
1095
1096 if (xdr_stream_decode_u32(argp->xdr, &open->op_claim_type) < 0)
1097 return nfserr_bad_xdr;
1098 switch (open->op_claim_type) {
1099 case NFS4_OPEN_CLAIM_NULL:
1100 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1101 status = nfsd4_decode_component4(argp, &open->op_fname,
1102 &open->op_fnamelen);
1103 if (status)
1104 return status;
1105 break;
1106 case NFS4_OPEN_CLAIM_PREVIOUS:
1107 if (xdr_stream_decode_u32(argp->xdr, &open->op_delegate_type) < 0)
1108 return nfserr_bad_xdr;
1109 break;
1110 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1111 status = nfsd4_decode_stateid4(argp, &open->op_delegate_stateid);
1112 if (status)
1113 return status;
1114 status = nfsd4_decode_component4(argp, &open->op_fname,
1115 &open->op_fnamelen);
1116 if (status)
1117 return status;
1118 break;
1119 case NFS4_OPEN_CLAIM_FH:
1120 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1121 if (argp->minorversion < 1)
1122 return nfserr_bad_xdr;
1123 /* void */
1124 break;
1125 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1126 if (argp->minorversion < 1)
1127 return nfserr_bad_xdr;
1128 status = nfsd4_decode_stateid4(argp, &open->op_delegate_stateid);
1129 if (status)
1130 return status;
1131 break;
1132 default:
1133 return nfserr_bad_xdr;
1134 }
1135
1136 return nfs_ok;
1137}
1138
1139static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
1141{
Chuck Lever61e5e0b2020-11-01 12:04:06 -05001142 __be32 status;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02001143 u32 dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 memset(open->op_bmval, 0, sizeof(open->op_bmval));
1146 open->op_iattr.ia_valid = 0;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001147 open->op_openowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
J. Bruce Fields9d313b12013-02-28 12:51:49 -08001149 open->op_xdr_error = 0;
Chuck Lever61e5e0b2020-11-01 12:04:06 -05001150 if (xdr_stream_decode_u32(argp->xdr, &open->op_seqid) < 0)
1151 return nfserr_bad_xdr;
1152 /* deleg_want is ignored */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02001153 status = nfsd4_decode_share_access(argp, &open->op_share_access,
1154 &open->op_deleg_want, &dummy);
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001155 if (status)
Chuck Lever61e5e0b2020-11-01 12:04:06 -05001156 return status;
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001157 status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
1158 if (status)
Chuck Lever61e5e0b2020-11-01 12:04:06 -05001159 return status;
1160 status = nfsd4_decode_state_owner4(argp, &open->op_clientid,
1161 &open->op_owner);
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001162 if (status)
Chuck Lever61e5e0b2020-11-01 12:04:06 -05001163 return status;
Chuck Levere6ec04b2020-11-16 17:41:21 -05001164 status = nfsd4_decode_openflag4(argp, open);
1165 if (status)
1166 return status;
Chuck Lever61e5e0b2020-11-01 12:04:06 -05001167 return nfsd4_decode_open_claim4(argp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169
Al Virob37ad282006-10-19 23:28:59 -07001170static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
1172{
Chuck Lever06bee692020-11-03 14:18:57 -05001173 __be32 status;
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001174
1175 if (argp->minorversion >= 1)
1176 return nfserr_notsupp;
1177
Chuck Lever06bee692020-11-03 14:18:57 -05001178 status = nfsd4_decode_stateid4(argp, &open_conf->oc_req_stateid);
Benny Halevye31a1b62008-08-12 20:46:18 +03001179 if (status)
1180 return status;
Chuck Lever06bee692020-11-03 14:18:57 -05001181 if (xdr_stream_decode_u32(argp->xdr, &open_conf->oc_seqid) < 0)
1182 return nfserr_bad_xdr;
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001183
Chuck Lever06bee692020-11-03 14:18:57 -05001184 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185}
1186
Al Virob37ad282006-10-19 23:28:59 -07001187static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
1189{
Chuck Leverdca716512020-11-03 14:21:01 -05001190 __be32 status;
1191
1192 status = nfsd4_decode_stateid4(argp, &open_down->od_stateid);
Benny Halevye31a1b62008-08-12 20:46:18 +03001193 if (status)
1194 return status;
Chuck Leverdca716512020-11-03 14:21:01 -05001195 if (xdr_stream_decode_u32(argp->xdr, &open_down->od_seqid) < 0)
1196 return nfserr_bad_xdr;
1197 /* deleg_want is ignored */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02001198 status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
1199 &open_down->od_deleg_want, NULL);
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001200 if (status)
1201 return status;
Chuck Leverdca716512020-11-03 14:21:01 -05001202 return nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
Al Virob37ad282006-10-19 23:28:59 -07001205static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
1207{
Chuck Levera73bed92020-11-03 14:23:02 -05001208 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Chuck Levera73bed92020-11-03 14:23:02 -05001210 if (xdr_stream_decode_u32(argp->xdr, &putfh->pf_fhlen) < 0)
1211 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (putfh->pf_fhlen > NFS4_FHSIZE)
Chuck Levera73bed92020-11-03 14:23:02 -05001213 return nfserr_bad_xdr;
1214 p = xdr_inline_decode(argp->xdr, putfh->pf_fhlen);
1215 if (!p)
1216 return nfserr_bad_xdr;
Chuck Lever7b723002020-12-18 12:28:58 -05001217 putfh->pf_fhval = svcxdr_savemem(argp, p, putfh->pf_fhlen);
Chuck Levera73bed92020-11-03 14:23:02 -05001218 if (!putfh->pf_fhval)
1219 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Chuck Levera73bed92020-11-03 14:23:02 -05001221 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
1223
Al Virob37ad282006-10-19 23:28:59 -07001224static __be32
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001225nfsd4_decode_putpubfh(struct nfsd4_compoundargs *argp, void *p)
1226{
1227 if (argp->minorversion == 0)
1228 return nfs_ok;
1229 return nfserr_notsupp;
1230}
1231
1232static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
1234{
Chuck Lever3909c3b2020-11-03 14:28:24 -05001235 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Chuck Lever3909c3b2020-11-03 14:28:24 -05001237 status = nfsd4_decode_stateid4(argp, &read->rd_stateid);
Benny Halevye31a1b62008-08-12 20:46:18 +03001238 if (status)
1239 return status;
Chuck Lever3909c3b2020-11-03 14:28:24 -05001240 if (xdr_stream_decode_u64(argp->xdr, &read->rd_offset) < 0)
1241 return nfserr_bad_xdr;
1242 if (xdr_stream_decode_u32(argp->xdr, &read->rd_length) < 0)
1243 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Chuck Lever3909c3b2020-11-03 14:28:24 -05001245 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
1247
Al Virob37ad282006-10-19 23:28:59 -07001248static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
1250{
Chuck Lever0dfaf2a2020-11-03 14:30:59 -05001251 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Chuck Lever0dfaf2a2020-11-03 14:30:59 -05001253 if (xdr_stream_decode_u64(argp->xdr, &readdir->rd_cookie) < 0)
1254 return nfserr_bad_xdr;
1255 status = nfsd4_decode_verifier4(argp, &readdir->rd_verf);
1256 if (status)
1257 return status;
1258 if (xdr_stream_decode_u32(argp->xdr, &readdir->rd_dircount) < 0)
1259 return nfserr_bad_xdr;
1260 if (xdr_stream_decode_u32(argp->xdr, &readdir->rd_maxcount) < 0)
1261 return nfserr_bad_xdr;
1262 if (xdr_stream_decode_uint32_array(argp->xdr, readdir->rd_bmval,
1263 ARRAY_SIZE(readdir->rd_bmval)) < 0)
1264 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Chuck Lever0dfaf2a2020-11-03 14:30:59 -05001266 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
1268
Al Virob37ad282006-10-19 23:28:59 -07001269static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
1271{
Chuck Leverb7f5fbf2020-11-04 15:04:36 -05001272 return nfsd4_decode_component4(argp, &remove->rm_name, &remove->rm_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
1274
Al Virob37ad282006-10-19 23:28:59 -07001275static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
1277{
Chuck Leverba881a02020-11-04 15:05:58 -05001278 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Chuck Leverba881a02020-11-04 15:05:58 -05001280 status = nfsd4_decode_component4(argp, &rename->rn_sname, &rename->rn_snamelen);
1281 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 return status;
Chuck Leverba881a02020-11-04 15:05:58 -05001283 return nfsd4_decode_component4(argp, &rename->rn_tname, &rename->rn_tnamelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
Al Virob37ad282006-10-19 23:28:59 -07001286static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
1288{
Chuck Leverd12f9042020-11-04 15:08:50 -05001289 return nfsd4_decode_clientid4(argp, clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290}
1291
Al Virob37ad282006-10-19 23:28:59 -07001292static __be32
Andy Adamsondcb488a32007-07-17 04:04:51 -07001293nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
1294 struct nfsd4_secinfo *secinfo)
1295{
Chuck Leverd0abdae2020-11-04 15:09:42 -05001296 return nfsd4_decode_component4(argp, &secinfo->si_name, &secinfo->si_namelen);
Andy Adamsondcb488a32007-07-17 04:04:51 -07001297}
1298
1299static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
1301{
Benny Halevye31a1b62008-08-12 20:46:18 +03001302 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Chuck Lever44592fe2020-11-21 14:14:59 -05001304 status = nfsd4_decode_stateid4(argp, &setattr->sa_stateid);
Benny Halevye31a1b62008-08-12 20:46:18 +03001305 if (status)
1306 return status;
Chuck Leverd1c263a2020-11-03 12:56:05 -05001307 return nfsd4_decode_fattr4(argp, setattr->sa_bmval,
1308 ARRAY_SIZE(setattr->sa_bmval),
1309 &setattr->sa_iattr, &setattr->sa_acl,
1310 &setattr->sa_label, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312
Al Virob37ad282006-10-19 23:28:59 -07001313static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
1315{
Chuck Lever92fa6c02020-11-03 14:35:02 -05001316 __be32 *p, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001318 if (argp->minorversion >= 1)
1319 return nfserr_notsupp;
1320
Chuck Lever92fa6c02020-11-03 14:35:02 -05001321 status = nfsd4_decode_verifier4(argp, &setclientid->se_verf);
1322 if (status)
1323 return status;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001324 status = nfsd4_decode_opaque(argp, &setclientid->se_name);
1325 if (status)
Chuck Lever92fa6c02020-11-03 14:35:02 -05001326 return status;
1327 if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_prog) < 0)
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001328 return nfserr_bad_xdr;
Chuck Lever92fa6c02020-11-03 14:35:02 -05001329 if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_netid_len) < 0)
1330 return nfserr_bad_xdr;
1331 p = xdr_inline_decode(argp->xdr, setclientid->se_callback_netid_len);
1332 if (!p)
1333 return nfserr_bad_xdr;
Chuck Lever7b723002020-12-18 12:28:58 -05001334 setclientid->se_callback_netid_val = svcxdr_savemem(argp, p,
Chuck Lever92fa6c02020-11-03 14:35:02 -05001335 setclientid->se_callback_netid_len);
1336 if (!setclientid->se_callback_netid_val)
1337 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Chuck Lever92fa6c02020-11-03 14:35:02 -05001339 if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_addr_len) < 0)
1340 return nfserr_bad_xdr;
1341 p = xdr_inline_decode(argp->xdr, setclientid->se_callback_addr_len);
1342 if (!p)
1343 return nfserr_bad_xdr;
Chuck Lever7b723002020-12-18 12:28:58 -05001344 setclientid->se_callback_addr_val = svcxdr_savemem(argp, p,
Chuck Lever92fa6c02020-11-03 14:35:02 -05001345 setclientid->se_callback_addr_len);
1346 if (!setclientid->se_callback_addr_val)
1347 return nfserr_jukebox;
Chuck Lever92fa6c02020-11-03 14:35:02 -05001348 if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_ident) < 0)
1349 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Chuck Lever92fa6c02020-11-03 14:35:02 -05001351 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352}
1353
Al Virob37ad282006-10-19 23:28:59 -07001354static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
1356{
Chuck Leverd1ca5512020-11-04 15:12:33 -05001357 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001359 if (argp->minorversion >= 1)
1360 return nfserr_notsupp;
1361
Chuck Leverd1ca5512020-11-04 15:12:33 -05001362 status = nfsd4_decode_clientid4(argp, &scd_c->sc_clientid);
1363 if (status)
1364 return status;
1365 return nfsd4_decode_verifier4(argp, &scd_c->sc_confirm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366}
1367
1368/* Also used for NVERIFY */
Al Virob37ad282006-10-19 23:28:59 -07001369static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1371{
Chuck Lever67cd4532020-11-03 14:40:32 -05001372 __be32 *p, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Chuck Lever67cd4532020-11-03 14:40:32 -05001374 status = nfsd4_decode_bitmap4(argp, verify->ve_bmval,
1375 ARRAY_SIZE(verify->ve_bmval));
1376 if (status)
1377 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 /* For convenience's sake, we compare raw xdr'd attributes in
J. Bruce Fieldse5f95702012-11-30 17:24:18 -05001380 * nfsd4_proc_verify */
1381
Chuck Lever67cd4532020-11-03 14:40:32 -05001382 if (xdr_stream_decode_u32(argp->xdr, &verify->ve_attrlen) < 0)
1383 return nfserr_bad_xdr;
1384 p = xdr_inline_decode(argp->xdr, verify->ve_attrlen);
1385 if (!p)
1386 return nfserr_bad_xdr;
Chuck Lever7b723002020-12-18 12:28:58 -05001387 verify->ve_attrval = svcxdr_savemem(argp, p, verify->ve_attrlen);
Chuck Lever67cd4532020-11-03 14:40:32 -05001388 if (!verify->ve_attrval)
1389 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Chuck Lever67cd4532020-11-03 14:40:32 -05001391 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392}
1393
Al Virob37ad282006-10-19 23:28:59 -07001394static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1396{
Chuck Lever244e2be2020-11-03 14:44:28 -05001397 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Chuck Lever244e2be2020-11-03 14:44:28 -05001399 status = nfsd4_decode_stateid4(argp, &write->wr_stateid);
Benny Halevye31a1b62008-08-12 20:46:18 +03001400 if (status)
1401 return status;
Chuck Lever244e2be2020-11-03 14:44:28 -05001402 if (xdr_stream_decode_u64(argp->xdr, &write->wr_offset) < 0)
1403 return nfserr_bad_xdr;
1404 if (xdr_stream_decode_u32(argp->xdr, &write->wr_stable_how) < 0)
1405 return nfserr_bad_xdr;
Kinglong Mee54bbb7d2016-12-31 20:59:53 +08001406 if (write->wr_stable_how > NFS_FILE_SYNC)
Chuck Lever244e2be2020-11-03 14:44:28 -05001407 return nfserr_bad_xdr;
1408 if (xdr_stream_decode_u32(argp->xdr, &write->wr_buflen) < 0)
1409 return nfserr_bad_xdr;
Chuck Leverc1346a122020-11-03 11:54:23 -05001410 if (!xdr_stream_subsegment(argp->xdr, &write->wr_payload, write->wr_buflen))
Chuck Lever244e2be2020-11-03 14:44:28 -05001411 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Chuck Lever244e2be2020-11-03 14:44:28 -05001413 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
Al Virob37ad282006-10-19 23:28:59 -07001416static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1418{
Chuck Levera4a80c12020-11-04 13:42:25 -05001419 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001421 if (argp->minorversion >= 1)
1422 return nfserr_notsupp;
1423
Chuck Levera4a80c12020-11-04 13:42:25 -05001424 status = nfsd4_decode_state_owner4(argp, &rlockowner->rl_clientid,
1425 &rlockowner->rl_owner);
1426 if (status)
1427 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Andy Adamson60adfc52009-04-03 08:28:50 +03001429 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1430 return nfserr_inval;
Chuck Levera4a80c12020-11-04 13:42:25 -05001431
1432 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
1434
Chuck Lever0f81d962020-11-03 13:14:35 -05001435static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp, struct nfsd4_backchannel_ctl *bc)
1436{
1437 if (xdr_stream_decode_u32(argp->xdr, &bc->bc_cb_program) < 0)
1438 return nfserr_bad_xdr;
1439 return nfsd4_decode_cb_sec(argp, &bc->bc_cb_sec);
1440}
1441
Chuck Lever571e0452020-11-03 13:16:23 -05001442static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
1443{
1444 u32 use_conn_in_rdma_mode;
1445 __be32 status;
1446
1447 status = nfsd4_decode_sessionid4(argp, &bcts->sessionid);
1448 if (status)
1449 return status;
1450 if (xdr_stream_decode_u32(argp->xdr, &bcts->dir) < 0)
1451 return nfserr_bad_xdr;
1452 if (xdr_stream_decode_u32(argp->xdr, &use_conn_in_rdma_mode) < 0)
1453 return nfserr_bad_xdr;
1454
1455 return nfs_ok;
1456}
1457
Al Virob37ad282006-10-19 23:28:59 -07001458static __be32
Chuck Lever2548aa72020-11-03 11:13:00 -05001459nfsd4_decode_state_protect_ops(struct nfsd4_compoundargs *argp,
1460 struct nfsd4_exchange_id *exid)
1461{
1462 __be32 status;
1463
1464 status = nfsd4_decode_bitmap4(argp, exid->spo_must_enforce,
1465 ARRAY_SIZE(exid->spo_must_enforce));
1466 if (status)
1467 return nfserr_bad_xdr;
1468 status = nfsd4_decode_bitmap4(argp, exid->spo_must_allow,
1469 ARRAY_SIZE(exid->spo_must_allow));
1470 if (status)
1471 return nfserr_bad_xdr;
1472
1473 return nfs_ok;
1474}
1475
Chuck Lever547bfeb2020-11-03 11:17:50 -05001476/*
1477 * This implementation currently does not support SP4_SSV.
1478 * This decoder simply skips over these arguments.
1479 */
1480static noinline __be32
1481nfsd4_decode_ssv_sp_parms(struct nfsd4_compoundargs *argp,
1482 struct nfsd4_exchange_id *exid)
1483{
1484 u32 count, window, num_gss_handles;
1485 __be32 status;
1486
1487 /* ssp_ops */
1488 status = nfsd4_decode_state_protect_ops(argp, exid);
1489 if (status)
1490 return status;
1491
1492 /* ssp_hash_algs<> */
1493 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
1494 return nfserr_bad_xdr;
1495 while (count--) {
1496 status = nfsd4_decode_ignored_string(argp, 0);
1497 if (status)
1498 return status;
1499 }
1500
1501 /* ssp_encr_algs<> */
1502 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
1503 return nfserr_bad_xdr;
1504 while (count--) {
1505 status = nfsd4_decode_ignored_string(argp, 0);
1506 if (status)
1507 return status;
1508 }
1509
1510 if (xdr_stream_decode_u32(argp->xdr, &window) < 0)
1511 return nfserr_bad_xdr;
1512 if (xdr_stream_decode_u32(argp->xdr, &num_gss_handles) < 0)
1513 return nfserr_bad_xdr;
1514
1515 return nfs_ok;
1516}
1517
Chuck Lever2548aa72020-11-03 11:13:00 -05001518static __be32
Chuck Lever523ec6e2020-11-02 15:19:12 -05001519nfsd4_decode_state_protect4_a(struct nfsd4_compoundargs *argp,
1520 struct nfsd4_exchange_id *exid)
Andy Adamson2db134e2009-04-03 08:27:55 +03001521{
Chuck Lever523ec6e2020-11-02 15:19:12 -05001522 __be32 status;
Andy Adamson0733d212009-04-03 08:28:01 +03001523
Chuck Lever523ec6e2020-11-02 15:19:12 -05001524 if (xdr_stream_decode_u32(argp->xdr, &exid->spa_how) < 0)
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001525 return nfserr_bad_xdr;
Andy Adamson0733d212009-04-03 08:28:01 +03001526 switch (exid->spa_how) {
1527 case SP4_NONE:
1528 break;
1529 case SP4_MACH_CRED:
Chuck Lever2548aa72020-11-03 11:13:00 -05001530 status = nfsd4_decode_state_protect_ops(argp, exid);
Andrew Elbleed941642016-06-15 12:52:09 -04001531 if (status)
Chuck Lever2548aa72020-11-03 11:13:00 -05001532 return status;
Andy Adamson0733d212009-04-03 08:28:01 +03001533 break;
1534 case SP4_SSV:
Chuck Lever547bfeb2020-11-03 11:17:50 -05001535 status = nfsd4_decode_ssv_sp_parms(argp, exid);
Chuck Lever2548aa72020-11-03 11:13:00 -05001536 if (status)
1537 return status;
Andy Adamson0733d212009-04-03 08:28:01 +03001538 break;
1539 default:
Chuck Lever523ec6e2020-11-02 15:19:12 -05001540 return nfserr_bad_xdr;
Andy Adamson0733d212009-04-03 08:28:01 +03001541 }
1542
Chuck Lever523ec6e2020-11-02 15:19:12 -05001543 return nfs_ok;
1544}
1545
1546static __be32
Chuck Lever10ff8422020-11-16 15:21:55 -05001547nfsd4_decode_nfs_impl_id4(struct nfsd4_compoundargs *argp,
1548 struct nfsd4_exchange_id *exid)
1549{
1550 __be32 status;
1551 u32 count;
1552
1553 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
1554 return nfserr_bad_xdr;
1555 switch (count) {
1556 case 0:
1557 break;
1558 case 1:
1559 /* Note that RFC 8881 places no length limit on
1560 * nii_domain, but this implementation permits no
1561 * more than NFS4_OPAQUE_LIMIT bytes */
1562 status = nfsd4_decode_opaque(argp, &exid->nii_domain);
1563 if (status)
1564 return status;
1565 /* Note that RFC 8881 places no length limit on
1566 * nii_name, but this implementation permits no
1567 * more than NFS4_OPAQUE_LIMIT bytes */
1568 status = nfsd4_decode_opaque(argp, &exid->nii_name);
1569 if (status)
1570 return status;
1571 status = nfsd4_decode_nfstime4(argp, &exid->nii_time);
1572 if (status)
1573 return status;
1574 break;
1575 default:
1576 return nfserr_bad_xdr;
1577 }
1578
1579 return nfs_ok;
1580}
1581
1582static __be32
Chuck Lever523ec6e2020-11-02 15:19:12 -05001583nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
1584 struct nfsd4_exchange_id *exid)
1585{
Chuck Lever10ff8422020-11-16 15:21:55 -05001586 __be32 status;
Chuck Lever523ec6e2020-11-02 15:19:12 -05001587
1588 status = nfsd4_decode_verifier4(argp, &exid->verifier);
1589 if (status)
1590 return status;
1591 status = nfsd4_decode_opaque(argp, &exid->clname);
1592 if (status)
1593 return status;
1594 if (xdr_stream_decode_u32(argp->xdr, &exid->flags) < 0)
1595 return nfserr_bad_xdr;
1596 status = nfsd4_decode_state_protect4_a(argp, exid);
1597 if (status)
1598 return status;
Chuck Lever10ff8422020-11-16 15:21:55 -05001599 return nfsd4_decode_nfs_impl_id4(argp, exid);
Andy Adamson2db134e2009-04-03 08:27:55 +03001600}
1601
1602static __be32
Chuck Lever3a3f1fb2020-11-16 15:35:05 -05001603nfsd4_decode_channel_attrs4(struct nfsd4_compoundargs *argp,
1604 struct nfsd4_channel_attrs *ca)
1605{
1606 __be32 *p;
1607
1608 p = xdr_inline_decode(argp->xdr, XDR_UNIT * 7);
1609 if (!p)
1610 return nfserr_bad_xdr;
1611
1612 /* headerpadsz is ignored */
1613 p++;
1614 ca->maxreq_sz = be32_to_cpup(p++);
1615 ca->maxresp_sz = be32_to_cpup(p++);
1616 ca->maxresp_cached = be32_to_cpup(p++);
1617 ca->maxops = be32_to_cpup(p++);
1618 ca->maxreqs = be32_to_cpup(p++);
1619 ca->nr_rdma_attrs = be32_to_cpup(p);
1620 switch (ca->nr_rdma_attrs) {
1621 case 0:
1622 break;
1623 case 1:
1624 if (xdr_stream_decode_u32(argp->xdr, &ca->rdma_attrs) < 0)
1625 return nfserr_bad_xdr;
1626 break;
1627 default:
1628 return nfserr_bad_xdr;
1629 }
1630
1631 return nfs_ok;
1632}
1633
1634static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001635nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1636 struct nfsd4_create_session *sess)
1637{
Chuck Lever81243e32020-11-03 14:52:44 -05001638 __be32 status;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001639
Chuck Lever81243e32020-11-03 14:52:44 -05001640 status = nfsd4_decode_clientid4(argp, &sess->clientid);
1641 if (status)
1642 return status;
1643 if (xdr_stream_decode_u32(argp->xdr, &sess->seqid) < 0)
1644 return nfserr_bad_xdr;
1645 if (xdr_stream_decode_u32(argp->xdr, &sess->flags) < 0)
1646 return nfserr_bad_xdr;
Chuck Lever3a3f1fb2020-11-16 15:35:05 -05001647 status = nfsd4_decode_channel_attrs4(argp, &sess->fore_channel);
1648 if (status)
1649 return status;
1650 status = nfsd4_decode_channel_attrs4(argp, &sess->back_channel);
1651 if (status)
1652 return status;
Chuck Lever81243e32020-11-03 14:52:44 -05001653 if (xdr_stream_decode_u32(argp->xdr, &sess->callback_prog) < 0)
1654 return nfserr_bad_xdr;
1655 status = nfsd4_decode_cb_sec(argp, &sess->cb_sec);
1656 if (status)
1657 return status;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001658
Chuck Lever81243e32020-11-03 14:52:44 -05001659 return nfs_ok;
Andy Adamson2db134e2009-04-03 08:27:55 +03001660}
1661
1662static __be32
1663nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1664 struct nfsd4_destroy_session *destroy_session)
1665{
Chuck Lever94e254a2020-11-04 13:50:55 -05001666 return nfsd4_decode_sessionid4(argp, &destroy_session->sessionid);
Andy Adamson2db134e2009-04-03 08:27:55 +03001667}
1668
1669static __be32
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001670nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1671 struct nfsd4_free_stateid *free_stateid)
1672{
Chuck Leveraec387d2020-11-01 13:38:27 -05001673 return nfsd4_decode_stateid4(argp, &free_stateid->fr_stateid);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001674}
1675
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001676#ifdef CONFIG_NFSD_PNFS
1677static __be32
1678nfsd4_decode_getdeviceinfo(struct nfsd4_compoundargs *argp,
1679 struct nfsd4_getdeviceinfo *gdev)
1680{
Chuck Lever04495972020-11-03 15:03:50 -05001681 __be32 status;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001682
Chuck Lever04495972020-11-03 15:03:50 -05001683 status = nfsd4_decode_deviceid4(argp, &gdev->gd_devid);
1684 if (status)
1685 return status;
1686 if (xdr_stream_decode_u32(argp->xdr, &gdev->gd_layout_type) < 0)
1687 return nfserr_bad_xdr;
1688 if (xdr_stream_decode_u32(argp->xdr, &gdev->gd_maxcount) < 0)
1689 return nfserr_bad_xdr;
1690 if (xdr_stream_decode_uint32_array(argp->xdr,
1691 &gdev->gd_notify_types, 1) < 0)
1692 return nfserr_bad_xdr;
1693
1694 return nfs_ok;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001695}
1696
1697static __be32
Chuck Lever51859802020-11-04 10:40:07 -05001698nfsd4_decode_layoutcommit(struct nfsd4_compoundargs *argp,
1699 struct nfsd4_layoutcommit *lcp)
1700{
1701 __be32 *p, status;
1702
1703 if (xdr_stream_decode_u64(argp->xdr, &lcp->lc_seg.offset) < 0)
1704 return nfserr_bad_xdr;
1705 if (xdr_stream_decode_u64(argp->xdr, &lcp->lc_seg.length) < 0)
1706 return nfserr_bad_xdr;
1707 if (xdr_stream_decode_bool(argp->xdr, &lcp->lc_reclaim) < 0)
1708 return nfserr_bad_xdr;
1709 status = nfsd4_decode_stateid4(argp, &lcp->lc_sid);
1710 if (status)
1711 return status;
1712 if (xdr_stream_decode_u32(argp->xdr, &lcp->lc_newoffset) < 0)
1713 return nfserr_bad_xdr;
1714 if (lcp->lc_newoffset) {
1715 if (xdr_stream_decode_u64(argp->xdr, &lcp->lc_last_wr) < 0)
1716 return nfserr_bad_xdr;
1717 } else
1718 lcp->lc_last_wr = 0;
1719 p = xdr_inline_decode(argp->xdr, XDR_UNIT);
1720 if (!p)
1721 return nfserr_bad_xdr;
1722 if (xdr_item_is_present(p)) {
1723 status = nfsd4_decode_nfstime4(argp, &lcp->lc_mtime);
1724 if (status)
1725 return status;
1726 } else {
1727 lcp->lc_mtime.tv_nsec = UTIME_NOW;
1728 }
1729 return nfsd4_decode_layoutupdate4(argp, lcp);
1730}
1731
1732static __be32
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001733nfsd4_decode_layoutget(struct nfsd4_compoundargs *argp,
1734 struct nfsd4_layoutget *lgp)
1735{
Chuck Leverc8e88e32020-11-03 15:06:04 -05001736 __be32 status;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001737
Chuck Leverc8e88e32020-11-03 15:06:04 -05001738 if (xdr_stream_decode_u32(argp->xdr, &lgp->lg_signal) < 0)
1739 return nfserr_bad_xdr;
1740 if (xdr_stream_decode_u32(argp->xdr, &lgp->lg_layout_type) < 0)
1741 return nfserr_bad_xdr;
1742 if (xdr_stream_decode_u32(argp->xdr, &lgp->lg_seg.iomode) < 0)
1743 return nfserr_bad_xdr;
1744 if (xdr_stream_decode_u64(argp->xdr, &lgp->lg_seg.offset) < 0)
1745 return nfserr_bad_xdr;
1746 if (xdr_stream_decode_u64(argp->xdr, &lgp->lg_seg.length) < 0)
1747 return nfserr_bad_xdr;
1748 if (xdr_stream_decode_u64(argp->xdr, &lgp->lg_minlength) < 0)
1749 return nfserr_bad_xdr;
1750 status = nfsd4_decode_stateid4(argp, &lgp->lg_sid);
Kinglong Meedb59c0e2015-03-19 19:04:41 +08001751 if (status)
1752 return status;
Chuck Leverc8e88e32020-11-03 15:06:04 -05001753 if (xdr_stream_decode_u32(argp->xdr, &lgp->lg_maxcount) < 0)
1754 return nfserr_bad_xdr;
Kinglong Meedb59c0e2015-03-19 19:04:41 +08001755
Chuck Leverc8e88e32020-11-03 15:06:04 -05001756 return nfs_ok;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001757}
1758
1759static __be32
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001760nfsd4_decode_layoutreturn(struct nfsd4_compoundargs *argp,
1761 struct nfsd4_layoutreturn *lrp)
1762{
Chuck Lever645fcad2020-11-04 10:42:25 -05001763 if (xdr_stream_decode_bool(argp->xdr, &lrp->lr_reclaim) < 0)
1764 return nfserr_bad_xdr;
1765 if (xdr_stream_decode_u32(argp->xdr, &lrp->lr_layout_type) < 0)
1766 return nfserr_bad_xdr;
1767 if (xdr_stream_decode_u32(argp->xdr, &lrp->lr_seg.iomode) < 0)
1768 return nfserr_bad_xdr;
1769 return nfsd4_decode_layoutreturn4(argp, lrp);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001770}
1771#endif /* CONFIG_NFSD_PNFS */
1772
Chuck Lever53d70872020-11-03 14:33:12 -05001773static __be32 nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
1774 struct nfsd4_secinfo_no_name *sin)
1775{
1776 if (xdr_stream_decode_u32(argp->xdr, &sin->sin_style) < 0)
1777 return nfserr_bad_xdr;
1778 return nfs_ok;
1779}
1780
Andy Adamson2db134e2009-04-03 08:27:55 +03001781static __be32
Chuck Levercf907b12020-11-03 14:55:19 -05001782nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1783 struct nfsd4_sequence *seq)
1784{
1785 __be32 *p, status;
1786
1787 status = nfsd4_decode_sessionid4(argp, &seq->sessionid);
1788 if (status)
1789 return status;
1790 p = xdr_inline_decode(argp->xdr, XDR_UNIT * 4);
1791 if (!p)
1792 return nfserr_bad_xdr;
1793 seq->seqid = be32_to_cpup(p++);
1794 seq->slotid = be32_to_cpup(p++);
1795 seq->maxslots = be32_to_cpup(p++);
1796 seq->cachethis = be32_to_cpup(p);
1797
1798 return nfs_ok;
1799}
1800
1801static __be32
Chuck Leverb7a0c8f2020-11-03 14:57:44 -05001802nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1803{
1804 struct nfsd4_test_stateid_id *stateid;
1805 __be32 status;
1806 u32 i;
1807
1808 if (xdr_stream_decode_u32(argp->xdr, &test_stateid->ts_num_ids) < 0)
1809 return nfserr_bad_xdr;
1810
1811 INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
1812 for (i = 0; i < test_stateid->ts_num_ids; i++) {
1813 stateid = svcxdr_tmpalloc(argp, sizeof(*stateid));
1814 if (!stateid)
1815 return nfserrno(-ENOMEM); /* XXX: not jukebox? */
1816 INIT_LIST_HEAD(&stateid->ts_id_list);
1817 list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
1818 status = nfsd4_decode_stateid4(argp, &stateid->ts_id_stateid);
1819 if (status)
1820 return status;
1821 }
1822
1823 return nfs_ok;
1824}
1825
Chuck Leverc95f2ec2020-11-04 15:15:09 -05001826static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp,
1827 struct nfsd4_destroy_clientid *dc)
1828{
1829 return nfsd4_decode_clientid4(argp, &dc->clientid);
1830}
1831
Chuck Lever0d646782020-11-03 15:02:11 -05001832static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp,
1833 struct nfsd4_reclaim_complete *rc)
1834{
1835 if (xdr_stream_decode_bool(argp->xdr, &rc->rca_one_fs) < 0)
1836 return nfserr_bad_xdr;
1837 return nfs_ok;
1838}
1839
Chuck Leverb7a0c8f2020-11-03 14:57:44 -05001840static __be32
Anna Schumaker95d871f2014-11-07 14:44:26 -05001841nfsd4_decode_fallocate(struct nfsd4_compoundargs *argp,
1842 struct nfsd4_fallocate *fallocate)
1843{
Chuck Lever6aef27a2020-11-04 10:44:05 -05001844 __be32 status;
Anna Schumaker95d871f2014-11-07 14:44:26 -05001845
Chuck Lever6aef27a2020-11-04 10:44:05 -05001846 status = nfsd4_decode_stateid4(argp, &fallocate->falloc_stateid);
Anna Schumaker95d871f2014-11-07 14:44:26 -05001847 if (status)
1848 return status;
Chuck Lever6aef27a2020-11-04 10:44:05 -05001849 if (xdr_stream_decode_u64(argp->xdr, &fallocate->falloc_offset) < 0)
1850 return nfserr_bad_xdr;
1851 if (xdr_stream_decode_u64(argp->xdr, &fallocate->falloc_length) < 0)
1852 return nfserr_bad_xdr;
Anna Schumaker95d871f2014-11-07 14:44:26 -05001853
Chuck Lever6aef27a2020-11-04 10:44:05 -05001854 return nfs_ok;
Anna Schumaker95d871f2014-11-07 14:44:26 -05001855}
1856
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001857static __be32 nfsd4_decode_nl4_server(struct nfsd4_compoundargs *argp,
1858 struct nl4_server *ns)
1859{
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001860 struct nfs42_netaddr *naddr;
Chuck Leverf49e4b42020-11-16 18:05:06 -05001861 __be32 *p;
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001862
Chuck Leverf49e4b42020-11-16 18:05:06 -05001863 if (xdr_stream_decode_u32(argp->xdr, &ns->nl4_type) < 0)
1864 return nfserr_bad_xdr;
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001865
1866 /* currently support for 1 inter-server source server */
1867 switch (ns->nl4_type) {
1868 case NL4_NETADDR:
1869 naddr = &ns->u.nl4_addr;
1870
Chuck Leverf49e4b42020-11-16 18:05:06 -05001871 if (xdr_stream_decode_u32(argp->xdr, &naddr->netid_len) < 0)
1872 return nfserr_bad_xdr;
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001873 if (naddr->netid_len > RPCBIND_MAXNETIDLEN)
Chuck Leverf49e4b42020-11-16 18:05:06 -05001874 return nfserr_bad_xdr;
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001875
Chuck Leverf49e4b42020-11-16 18:05:06 -05001876 p = xdr_inline_decode(argp->xdr, naddr->netid_len);
1877 if (!p)
1878 return nfserr_bad_xdr;
1879 memcpy(naddr->netid, p, naddr->netid_len);
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001880
Chuck Leverf49e4b42020-11-16 18:05:06 -05001881 if (xdr_stream_decode_u32(argp->xdr, &naddr->addr_len) < 0)
1882 return nfserr_bad_xdr;
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001883 if (naddr->addr_len > RPCBIND_MAXUADDRLEN)
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 p = xdr_inline_decode(argp->xdr, naddr->addr_len);
1887 if (!p)
1888 return nfserr_bad_xdr;
1889 memcpy(naddr->addr, p, naddr->addr_len);
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001890 break;
1891 default:
Chuck Leverf49e4b42020-11-16 18:05:06 -05001892 return nfserr_bad_xdr;
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001893 }
Chuck Leverf49e4b42020-11-16 18:05:06 -05001894
1895 return nfs_ok;
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001896}
1897
Christoph Hellwigffa01602015-12-03 12:59:52 +01001898static __be32
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001899nfsd4_decode_copy(struct nfsd4_compoundargs *argp, struct nfsd4_copy *copy)
1900{
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001901 struct nl4_server *ns_dummy;
Chuck Levere8febea2020-11-04 10:49:37 -05001902 u32 consecutive, i, count;
1903 __be32 status;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001904
Chuck Levere8febea2020-11-04 10:49:37 -05001905 status = nfsd4_decode_stateid4(argp, &copy->cp_src_stateid);
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001906 if (status)
1907 return status;
Chuck Levere8febea2020-11-04 10:49:37 -05001908 status = nfsd4_decode_stateid4(argp, &copy->cp_dst_stateid);
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001909 if (status)
1910 return status;
Chuck Levere8febea2020-11-04 10:49:37 -05001911 if (xdr_stream_decode_u64(argp->xdr, &copy->cp_src_pos) < 0)
1912 return nfserr_bad_xdr;
1913 if (xdr_stream_decode_u64(argp->xdr, &copy->cp_dst_pos) < 0)
1914 return nfserr_bad_xdr;
1915 if (xdr_stream_decode_u64(argp->xdr, &copy->cp_count) < 0)
1916 return nfserr_bad_xdr;
1917 /* ca_consecutive: we always do consecutive copies */
1918 if (xdr_stream_decode_u32(argp->xdr, &consecutive) < 0)
1919 return nfserr_bad_xdr;
1920 if (xdr_stream_decode_u32(argp->xdr, &copy->cp_synchronous) < 0)
1921 return nfserr_bad_xdr;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001922
Chuck Levere8febea2020-11-04 10:49:37 -05001923 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
1924 return nfserr_bad_xdr;
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001925 copy->cp_intra = false;
1926 if (count == 0) { /* intra-server copy */
1927 copy->cp_intra = true;
Chuck Levere8febea2020-11-04 10:49:37 -05001928 return nfs_ok;
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001929 }
1930
Chuck Levere8febea2020-11-04 10:49:37 -05001931 /* decode all the supplied server addresses but use only the first */
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001932 status = nfsd4_decode_nl4_server(argp, &copy->cp_src);
1933 if (status)
1934 return status;
1935
1936 ns_dummy = kmalloc(sizeof(struct nl4_server), GFP_KERNEL);
1937 if (ns_dummy == NULL)
Chuck Levere8febea2020-11-04 10:49:37 -05001938 return nfserrno(-ENOMEM); /* XXX: jukebox? */
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001939 for (i = 0; i < count - 1; i++) {
1940 status = nfsd4_decode_nl4_server(argp, ns_dummy);
1941 if (status) {
1942 kfree(ns_dummy);
1943 return status;
1944 }
1945 }
1946 kfree(ns_dummy);
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001947
Chuck Levere8febea2020-11-04 10:49:37 -05001948 return nfs_ok;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001949}
1950
1951static __be32
Olga Kornievskaia51911862019-08-08 11:14:59 -04001952nfsd4_decode_copy_notify(struct nfsd4_compoundargs *argp,
1953 struct nfsd4_copy_notify *cn)
1954{
Chuck Lever5aff7d02020-09-11 14:47:59 -04001955 __be32 status;
Olga Kornievskaia51911862019-08-08 11:14:59 -04001956
Chuck Leverf9a953f2020-11-21 14:19:24 -05001957 status = nfsd4_decode_stateid4(argp, &cn->cpn_src_stateid);
Olga Kornievskaia51911862019-08-08 11:14:59 -04001958 if (status)
1959 return status;
1960 return nfsd4_decode_nl4_server(argp, &cn->cpn_dst);
1961}
1962
1963static __be32
Chuck Leverf9a953f2020-11-21 14:19:24 -05001964nfsd4_decode_offload_status(struct nfsd4_compoundargs *argp,
1965 struct nfsd4_offload_status *os)
1966{
Chuck Lever2846bb052020-11-21 14:21:25 -05001967 return nfsd4_decode_stateid4(argp, &os->stateid);
Chuck Leverf9a953f2020-11-21 14:19:24 -05001968}
1969
1970static __be32
Anna Schumaker24bab492014-09-26 13:58:27 -04001971nfsd4_decode_seek(struct nfsd4_compoundargs *argp, struct nfsd4_seek *seek)
1972{
Chuck Lever9d32b412020-11-04 10:54:47 -05001973 __be32 status;
Anna Schumaker24bab492014-09-26 13:58:27 -04001974
Chuck Lever9d32b412020-11-04 10:54:47 -05001975 status = nfsd4_decode_stateid4(argp, &seek->seek_stateid);
Anna Schumaker24bab492014-09-26 13:58:27 -04001976 if (status)
1977 return status;
Chuck Lever9d32b412020-11-04 10:54:47 -05001978 if (xdr_stream_decode_u64(argp->xdr, &seek->seek_offset) < 0)
1979 return nfserr_bad_xdr;
1980 if (xdr_stream_decode_u32(argp->xdr, &seek->seek_whence) < 0)
1981 return nfserr_bad_xdr;
Anna Schumaker24bab492014-09-26 13:58:27 -04001982
Chuck Lever9d32b412020-11-04 10:54:47 -05001983 return nfs_ok;
Anna Schumaker24bab492014-09-26 13:58:27 -04001984}
1985
Chuck Lever3dfd0b02020-11-04 10:46:46 -05001986static __be32
1987nfsd4_decode_clone(struct nfsd4_compoundargs *argp, struct nfsd4_clone *clone)
1988{
1989 __be32 status;
1990
1991 status = nfsd4_decode_stateid4(argp, &clone->cl_src_stateid);
1992 if (status)
1993 return status;
1994 status = nfsd4_decode_stateid4(argp, &clone->cl_dst_stateid);
1995 if (status)
1996 return status;
1997 if (xdr_stream_decode_u64(argp->xdr, &clone->cl_src_pos) < 0)
1998 return nfserr_bad_xdr;
1999 if (xdr_stream_decode_u64(argp->xdr, &clone->cl_dst_pos) < 0)
2000 return nfserr_bad_xdr;
2001 if (xdr_stream_decode_u64(argp->xdr, &clone->cl_count) < 0)
2002 return nfserr_bad_xdr;
2003
2004 return nfs_ok;
2005}
2006
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002007/*
2008 * XDR data that is more than PAGE_SIZE in size is normally part of a
2009 * read or write. However, the size of extended attributes is limited
2010 * by the maximum request size, and then further limited by the underlying
2011 * filesystem limits. This can exceed PAGE_SIZE (currently, XATTR_SIZE_MAX
2012 * is 64k). Since there is no kvec- or page-based interface to xattrs,
2013 * and we're not dealing with contiguous pages, we need to do some copying.
2014 */
2015
2016/*
Chuck Leverc1346a122020-11-03 11:54:23 -05002017 * Decode data into buffer.
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002018 */
2019static __be32
Chuck Leverc1346a122020-11-03 11:54:23 -05002020nfsd4_vbuf_from_vector(struct nfsd4_compoundargs *argp, struct xdr_buf *xdr,
2021 char **bufp, u32 buflen)
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002022{
Chuck Leverc1346a122020-11-03 11:54:23 -05002023 struct page **pages = xdr->pages;
2024 struct kvec *head = xdr->head;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002025 char *tmp, *dp;
2026 u32 len;
2027
2028 if (buflen <= head->iov_len) {
2029 /*
2030 * We're in luck, the head has enough space. Just return
2031 * the head, no need for copying.
2032 */
2033 *bufp = head->iov_base;
2034 return 0;
2035 }
2036
2037 tmp = svcxdr_tmpalloc(argp, buflen);
2038 if (tmp == NULL)
2039 return nfserr_jukebox;
2040
2041 dp = tmp;
2042 memcpy(dp, head->iov_base, head->iov_len);
2043 buflen -= head->iov_len;
2044 dp += head->iov_len;
2045
2046 while (buflen > 0) {
2047 len = min_t(u32, buflen, PAGE_SIZE);
2048 memcpy(dp, page_address(*pages), len);
2049
2050 buflen -= len;
2051 dp += len;
2052 pages++;
2053 }
2054
2055 *bufp = tmp;
2056 return 0;
2057}
2058
2059/*
2060 * Get a user extended attribute name from the XDR buffer.
2061 * It will not have the "user." prefix, so prepend it.
2062 * Lastly, check for nul characters in the name.
2063 */
2064static __be32
2065nfsd4_decode_xattr_name(struct nfsd4_compoundargs *argp, char **namep)
2066{
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002067 char *name, *sp, *dp;
2068 u32 namelen, cnt;
Chuck Lever830c7152020-11-04 10:56:52 -05002069 __be32 *p;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002070
Chuck Lever830c7152020-11-04 10:56:52 -05002071 if (xdr_stream_decode_u32(argp->xdr, &namelen) < 0)
2072 return nfserr_bad_xdr;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002073 if (namelen > (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN))
2074 return nfserr_nametoolong;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002075 if (namelen == 0)
Chuck Lever830c7152020-11-04 10:56:52 -05002076 return nfserr_bad_xdr;
2077 p = xdr_inline_decode(argp->xdr, namelen);
2078 if (!p)
2079 return nfserr_bad_xdr;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002080 name = svcxdr_tmpalloc(argp, namelen + XATTR_USER_PREFIX_LEN + 1);
2081 if (!name)
2082 return nfserr_jukebox;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002083 memcpy(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
2084
2085 /*
2086 * Copy the extended attribute name over while checking for 0
2087 * characters.
2088 */
2089 sp = (char *)p;
2090 dp = name + XATTR_USER_PREFIX_LEN;
2091 cnt = namelen;
2092
2093 while (cnt-- > 0) {
2094 if (*sp == '\0')
Chuck Lever830c7152020-11-04 10:56:52 -05002095 return nfserr_bad_xdr;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002096 *dp++ = *sp++;
2097 }
2098 *dp = '\0';
2099
2100 *namep = name;
2101
Chuck Lever830c7152020-11-04 10:56:52 -05002102 return nfs_ok;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002103}
2104
2105/*
2106 * A GETXATTR op request comes without a length specifier. We just set the
2107 * maximum length for the reply based on XATTR_SIZE_MAX and the maximum
2108 * channel reply size. nfsd_getxattr will probe the length of the xattr,
2109 * check it against getxa_len, and allocate + return the value.
2110 */
2111static __be32
2112nfsd4_decode_getxattr(struct nfsd4_compoundargs *argp,
2113 struct nfsd4_getxattr *getxattr)
2114{
2115 __be32 status;
2116 u32 maxcount;
2117
2118 status = nfsd4_decode_xattr_name(argp, &getxattr->getxa_name);
2119 if (status)
2120 return status;
2121
2122 maxcount = svc_max_payload(argp->rqstp);
2123 maxcount = min_t(u32, XATTR_SIZE_MAX, maxcount);
2124
2125 getxattr->getxa_len = maxcount;
2126
2127 return status;
2128}
2129
2130static __be32
2131nfsd4_decode_setxattr(struct nfsd4_compoundargs *argp,
2132 struct nfsd4_setxattr *setxattr)
2133{
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002134 u32 flags, maxcount, size;
Chuck Lever403366a2020-11-04 10:59:57 -05002135 __be32 status;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002136
Chuck Lever403366a2020-11-04 10:59:57 -05002137 if (xdr_stream_decode_u32(argp->xdr, &flags) < 0)
2138 return nfserr_bad_xdr;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002139
2140 if (flags > SETXATTR4_REPLACE)
2141 return nfserr_inval;
2142 setxattr->setxa_flags = flags;
2143
2144 status = nfsd4_decode_xattr_name(argp, &setxattr->setxa_name);
2145 if (status)
2146 return status;
2147
2148 maxcount = svc_max_payload(argp->rqstp);
2149 maxcount = min_t(u32, XATTR_SIZE_MAX, maxcount);
2150
Chuck Lever403366a2020-11-04 10:59:57 -05002151 if (xdr_stream_decode_u32(argp->xdr, &size) < 0)
2152 return nfserr_bad_xdr;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002153 if (size > maxcount)
2154 return nfserr_xattr2big;
2155
2156 setxattr->setxa_len = size;
2157 if (size > 0) {
Chuck Leverc1346a122020-11-03 11:54:23 -05002158 struct xdr_buf payload;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002159
Chuck Leverc1346a122020-11-03 11:54:23 -05002160 if (!xdr_stream_subsegment(argp->xdr, &payload, size))
Chuck Lever403366a2020-11-04 10:59:57 -05002161 return nfserr_bad_xdr;
Chuck Leverc1346a122020-11-03 11:54:23 -05002162 status = nfsd4_vbuf_from_vector(argp, &payload,
2163 &setxattr->setxa_buf, size);
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002164 }
2165
Chuck Lever403366a2020-11-04 10:59:57 -05002166 return nfs_ok;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002167}
2168
2169static __be32
2170nfsd4_decode_listxattrs(struct nfsd4_compoundargs *argp,
2171 struct nfsd4_listxattrs *listxattrs)
2172{
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002173 u32 maxcount;
2174
Chuck Lever22120362020-11-04 11:04:02 -05002175 if (xdr_stream_decode_u64(argp->xdr, &listxattrs->lsxa_cookie) < 0)
2176 return nfserr_bad_xdr;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002177
2178 /*
2179 * If the cookie is too large to have even one user.x attribute
2180 * plus trailing '\0' left in a maximum size buffer, it's invalid.
2181 */
2182 if (listxattrs->lsxa_cookie >=
2183 (XATTR_LIST_MAX / (XATTR_USER_PREFIX_LEN + 2)))
2184 return nfserr_badcookie;
2185
Chuck Lever22120362020-11-04 11:04:02 -05002186 if (xdr_stream_decode_u32(argp->xdr, &maxcount) < 0)
2187 return nfserr_bad_xdr;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002188 if (maxcount < 8)
2189 /* Always need at least 2 words (length and one character) */
2190 return nfserr_inval;
2191
2192 maxcount = min(maxcount, svc_max_payload(argp->rqstp));
2193 listxattrs->lsxa_maxcount = maxcount;
2194
Chuck Lever22120362020-11-04 11:04:02 -05002195 return nfs_ok;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002196}
2197
2198static __be32
2199nfsd4_decode_removexattr(struct nfsd4_compoundargs *argp,
2200 struct nfsd4_removexattr *removexattr)
2201{
2202 return nfsd4_decode_xattr_name(argp, &removexattr->rmxa_name);
2203}
2204
Anna Schumaker24bab492014-09-26 13:58:27 -04002205static __be32
Benny Halevy347e0ad2008-07-02 11:13:41 +03002206nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
2207{
2208 return nfs_ok;
2209}
2210
Benny Halevy3c375c62008-07-02 11:14:01 +03002211static __be32
2212nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
2213{
Benny Halevy1e685ec2009-03-04 23:06:06 +02002214 return nfserr_notsupp;
Benny Halevy3c375c62008-07-02 11:14:01 +03002215}
2216
Benny Halevy347e0ad2008-07-02 11:13:41 +03002217typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
2218
Chuck Leverc1df6092017-08-01 11:59:58 -04002219static const nfsd4_dec nfsd4_dec_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04002220 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
2221 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
2222 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
2223 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
2224 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
2225 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
2226 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
2227 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
2228 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
2229 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
2230 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
2231 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
2232 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
2233 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
2234 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
2235 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
2236 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
2237 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
2238 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
2239 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002240 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_putpubfh,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04002241 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
2242 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
2243 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
2244 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
2245 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
2246 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
2247 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
2248 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
2249 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
2250 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
2251 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
2252 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
2253 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
2254 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
2255 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
2256 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
Andy Adamson2db134e2009-04-03 08:27:55 +03002257
2258 /* new operations for NFSv4.1 */
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002259 [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002260 [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
Randy Dunlap9064caa2009-04-28 16:48:25 -07002261 [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
2262 [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
2263 [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04002264 [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07002265 [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002266#ifdef CONFIG_NFSD_PNFS
2267 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_getdeviceinfo,
2268 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
2269 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_layoutcommit,
2270 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_layoutget,
2271 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_layoutreturn,
2272#else
Randy Dunlap9064caa2009-04-28 16:48:25 -07002273 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
2274 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
2275 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
2276 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
2277 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002278#endif
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05002279 [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
Randy Dunlap9064caa2009-04-28 16:48:25 -07002280 [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
2281 [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
Bryan Schumaker17456802011-07-13 10:50:48 -04002282 [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07002283 [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
Mi Jinlong345c2842011-10-20 17:51:39 +08002284 [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002285 [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
Anna Schumaker87a15a82014-09-26 13:58:26 -04002286
2287 /* new operations for NFSv4.2 */
Anna Schumaker95d871f2014-11-07 14:44:26 -05002288 [OP_ALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
Anna Schumaker29ae7f92016-09-07 15:57:30 -04002289 [OP_COPY] = (nfsd4_dec)nfsd4_decode_copy,
Olga Kornievskaia51911862019-08-08 11:14:59 -04002290 [OP_COPY_NOTIFY] = (nfsd4_dec)nfsd4_decode_copy_notify,
Anna Schumakerb0cb9082014-11-07 14:44:27 -05002291 [OP_DEALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
Anna Schumaker87a15a82014-09-26 13:58:26 -04002292 [OP_IO_ADVISE] = (nfsd4_dec)nfsd4_decode_notsupp,
2293 [OP_LAYOUTERROR] = (nfsd4_dec)nfsd4_decode_notsupp,
2294 [OP_LAYOUTSTATS] = (nfsd4_dec)nfsd4_decode_notsupp,
Olga Kornievskaia885e2bf2018-07-20 18:19:19 -04002295 [OP_OFFLOAD_CANCEL] = (nfsd4_dec)nfsd4_decode_offload_status,
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04002296 [OP_OFFLOAD_STATUS] = (nfsd4_dec)nfsd4_decode_offload_status,
Anna Schumaker528b8492020-09-28 13:08:58 -04002297 [OP_READ_PLUS] = (nfsd4_dec)nfsd4_decode_read,
Anna Schumaker24bab492014-09-26 13:58:27 -04002298 [OP_SEEK] = (nfsd4_dec)nfsd4_decode_seek,
Anna Schumaker87a15a82014-09-26 13:58:26 -04002299 [OP_WRITE_SAME] = (nfsd4_dec)nfsd4_decode_notsupp,
Christoph Hellwigffa01602015-12-03 12:59:52 +01002300 [OP_CLONE] = (nfsd4_dec)nfsd4_decode_clone,
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002301 /* RFC 8276 extended atributes operations */
2302 [OP_GETXATTR] = (nfsd4_dec)nfsd4_decode_getxattr,
2303 [OP_SETXATTR] = (nfsd4_dec)nfsd4_decode_setxattr,
2304 [OP_LISTXATTRS] = (nfsd4_dec)nfsd4_decode_listxattrs,
2305 [OP_REMOVEXATTR] = (nfsd4_dec)nfsd4_decode_removexattr,
Andy Adamson2db134e2009-04-03 08:27:55 +03002306};
2307
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002308static inline bool
2309nfsd4_opnum_in_range(struct nfsd4_compoundargs *argp, struct nfsd4_op *op)
2310{
Anna Schumaker8217d142013-10-30 13:38:13 -04002311 if (op->opnum < FIRST_NFS4_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002312 return false;
Anna Schumaker8217d142013-10-30 13:38:13 -04002313 else if (argp->minorversion == 0 && op->opnum > LAST_NFS40_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002314 return false;
Anna Schumaker8217d142013-10-30 13:38:13 -04002315 else if (argp->minorversion == 1 && op->opnum > LAST_NFS41_OP)
2316 return false;
2317 else if (argp->minorversion == 2 && op->opnum > LAST_NFS42_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002318 return false;
2319 return true;
2320}
Benny Halevyf2feb962008-07-02 11:14:22 +03002321
Chuck Leverc44b31c2021-10-12 11:57:28 -04002322static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
2324{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 struct nfsd4_op *op;
J. Bruce Fields10910062011-01-24 12:11:02 -05002326 bool cachethis = false;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002327 int auth_slack= argp->rqstp->rq_auth_slack;
2328 int max_reply = auth_slack + 8; /* opcnt, status */
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05002329 int readcount = 0;
2330 int readbytes = 0;
Chuck Leverd9b74bd2020-11-04 11:07:06 -05002331 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 int i;
2333
Chuck Leverd9b74bd2020-11-04 11:07:06 -05002334 if (xdr_stream_decode_u32(argp->xdr, &argp->taglen) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -04002335 return false;
Chuck Leverd9b74bd2020-11-04 11:07:06 -05002336 max_reply += XDR_UNIT;
2337 argp->tag = NULL;
2338 if (unlikely(argp->taglen)) {
2339 if (argp->taglen > NFSD4_MAX_TAGLEN)
Chuck Leverc44b31c2021-10-12 11:57:28 -04002340 return false;
Chuck Leverd9b74bd2020-11-04 11:07:06 -05002341 p = xdr_inline_decode(argp->xdr, argp->taglen);
2342 if (!p)
Chuck Leverc44b31c2021-10-12 11:57:28 -04002343 return false;
Chuck Lever7b723002020-12-18 12:28:58 -05002344 argp->tag = svcxdr_savemem(argp, p, argp->taglen);
Chuck Leverd9b74bd2020-11-04 11:07:06 -05002345 if (!argp->tag)
Chuck Leverc44b31c2021-10-12 11:57:28 -04002346 return false;
Chuck Leverd9b74bd2020-11-04 11:07:06 -05002347 max_reply += xdr_align_size(argp->taglen);
2348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Chuck Leverd9b74bd2020-11-04 11:07:06 -05002350 if (xdr_stream_decode_u32(argp->xdr, &argp->minorversion) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -04002351 return false;
Chuck Leverd9b74bd2020-11-04 11:07:06 -05002352 if (xdr_stream_decode_u32(argp->xdr, &argp->opcnt) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -04002353 return false;
Chuck Leverd9b74bd2020-11-04 11:07:06 -05002354
J. Bruce Fields00781172017-11-15 12:30:27 -05002355 /*
2356 * NFS4ERR_RESOURCE is a more helpful error than GARBAGE_ARGS
2357 * here, so we return success at the xdr level so that
2358 * nfsd4_proc can handle this is an NFS-level error.
2359 */
2360 if (argp->opcnt > NFSD_MAX_OPS_PER_COMPOUND)
Chuck Leverc44b31c2021-10-12 11:57:28 -04002361 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Tobias Klausere8c96f82006-03-24 03:15:34 -08002363 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
J. Bruce Fields5d6031c2014-07-17 16:20:39 -04002364 argp->ops = kzalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (!argp->ops) {
2366 argp->ops = argp->iops;
Chuck Lever817cb9d2007-09-11 18:01:20 -04002367 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
Chuck Leverc44b31c2021-10-12 11:57:28 -04002368 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 }
2370 }
2371
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002372 if (argp->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
Benny Halevy30cff1f2008-07-02 11:13:18 +03002373 argp->opcnt = 0;
2374
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 for (i = 0; i < argp->opcnt; i++) {
2376 op = &argp->ops[i];
2377 op->replay = NULL;
2378
Chuck Lever3a237b42020-11-22 12:49:52 -05002379 if (xdr_stream_decode_u32(argp->xdr, &op->opnum) < 0)
Chuck Leverc44b31c2021-10-12 11:57:28 -04002380 return false;
Chuck Lever08281342020-11-21 11:36:42 -05002381 if (nfsd4_opnum_in_range(argp, op)) {
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002382 op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
Chuck Lever08281342020-11-21 11:36:42 -05002383 if (op->status != nfs_ok)
2384 trace_nfsd_compound_decode_err(argp->rqstp,
2385 argp->opcnt, i,
2386 op->opnum,
2387 op->status);
2388 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 op->opnum = OP_ILLEGAL;
2390 op->status = nfserr_op_illegal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 }
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002392 op->opdesc = OPDESC(op);
J. Bruce Fields10910062011-01-24 12:11:02 -05002393 /*
2394 * We'll try to cache the result in the DRC if any one
2395 * op in the compound wants to be cached:
2396 */
2397 cachethis |= nfsd4_cache_this_op(op);
J. Bruce Fields6ff40de2013-11-05 15:07:16 -05002398
Anna Schumaker528b8492020-09-28 13:08:58 -04002399 if (op->opnum == OP_READ || op->opnum == OP_READ_PLUS) {
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05002400 readcount++;
2401 readbytes += nfsd4_max_reply(argp->rqstp, op);
2402 } else
2403 max_reply += nfsd4_max_reply(argp->rqstp, op);
J. Bruce Fieldsf7b43d02014-08-12 11:41:40 -04002404 /*
Kinglong Mee7323f0d2017-02-03 22:51:46 +08002405 * OP_LOCK and OP_LOCKT may return a conflicting lock.
2406 * (Special case because it will just skip encoding this
2407 * if it runs out of xdr buffer space, and it is the only
2408 * operation that behaves this way.)
J. Bruce Fieldsf7b43d02014-08-12 11:41:40 -04002409 */
Kinglong Mee7323f0d2017-02-03 22:51:46 +08002410 if (op->opnum == OP_LOCK || op->opnum == OP_LOCKT)
J. Bruce Fieldsf7b43d02014-08-12 11:41:40 -04002411 max_reply += NFS4_OPAQUE_LIMIT;
J. Bruce Fieldse372ba62014-05-19 12:27:11 -04002412
2413 if (op->status) {
2414 argp->opcnt = i+1;
2415 break;
2416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 }
J. Bruce Fields10910062011-01-24 12:11:02 -05002418 /* Sessions make the DRC unnecessary: */
2419 if (argp->minorversion)
2420 cachethis = false;
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05002421 svc_reserve(argp->rqstp, max_reply + readbytes);
J. Bruce Fields10910062011-01-24 12:11:02 -05002422 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002424 if (readcount > 1 || max_reply > PAGE_SIZE - auth_slack)
Jeff Layton779fb0f2014-11-19 07:51:18 -05002425 clear_bit(RQ_SPLICE_OK, &argp->rqstp->rq_flags);
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05002426
Chuck Leverc44b31c2021-10-12 11:57:28 -04002427 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
NeilBrownb8800922017-01-30 17:17:00 +11002430static __be32 *encode_change(__be32 *p, struct kstat *stat, struct inode *inode,
2431 struct svc_export *exp)
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002432{
NeilBrownb8800922017-01-30 17:17:00 +11002433 if (exp->ex_flags & NFSEXP_V4ROOT) {
2434 *p++ = cpu_to_be32(convert_to_wallclock(exp->cd->flush_time));
2435 *p++ = 0;
J. Bruce Fields70b87f72020-11-30 17:46:14 -05002436 } else
Amir Goldstein39ca1bf2018-01-03 17:14:35 +02002437 p = xdr_encode_hyper(p, nfsd4_change_attribute(stat, inode));
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002438 return p;
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002439}
2440
J. Bruce Fields16945142018-04-25 14:34:11 -04002441/*
2442 * ctime (in NFSv4, time_metadata) is not writeable, and the client
2443 * doesn't really care what resolution could theoretically be stored by
2444 * the filesystem.
2445 *
2446 * The client cares how close together changes can be while still
2447 * guaranteeing ctime changes. For most filesystems (which have
2448 * timestamps with nanosecond fields) that is limited by the resolution
2449 * of the time returned from current_time() (which I'm assuming to be
2450 * 1/HZ).
2451 */
2452static __be32 *encode_time_delta(__be32 *p, struct inode *inode)
2453{
Arnd Bergmanne4598e32019-10-31 15:14:03 +01002454 struct timespec64 ts;
J. Bruce Fields16945142018-04-25 14:34:11 -04002455 u32 ns;
2456
2457 ns = max_t(u32, NSEC_PER_SEC/HZ, inode->i_sb->s_time_gran);
Arnd Bergmanne4598e32019-10-31 15:14:03 +01002458 ts = ns_to_timespec64(ns);
J. Bruce Fields16945142018-04-25 14:34:11 -04002459
2460 p = xdr_encode_hyper(p, ts.tv_sec);
2461 *p++ = cpu_to_be32(ts.tv_nsec);
2462
2463 return p;
2464}
2465
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002466static __be32 *encode_cinfo(__be32 *p, struct nfsd4_change_info *c)
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002467{
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002468 *p++ = cpu_to_be32(c->atomic);
J. Bruce Fieldsb2140332020-11-30 17:46:15 -05002469 p = xdr_encode_hyper(p, c->before_change);
2470 p = xdr_encode_hyper(p, c->after_change);
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002471 return p;
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002472}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002474/* Encode as an array of strings the string given with components
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002475 * separated @sep, escaped with esc_enter and esc_exit.
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002476 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002477static __be32 nfsd4_encode_components_esc(struct xdr_stream *xdr, char sep,
2478 char *components, char esc_enter,
2479 char esc_exit)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002480{
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002481 __be32 *p;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002482 __be32 pathlen;
2483 int pathlen_offset;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002484 int strlen, count=0;
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002485 char *str, *end, *next;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002486
2487 dprintk("nfsd4_encode_components(%s)\n", components);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002488
2489 pathlen_offset = xdr->buf->len;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002490 p = xdr_reserve_space(xdr, 4);
2491 if (!p)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002492 return nfserr_resource;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002493 p++; /* We will fill this in with @count later */
2494
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002495 end = str = components;
2496 while (*end) {
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002497 bool found_esc = false;
2498
2499 /* try to parse as esc_start, ..., esc_end, sep */
2500 if (*str == esc_enter) {
2501 for (; *end && (*end != esc_exit); end++)
2502 /* find esc_exit or end of string */;
2503 next = end + 1;
2504 if (*end && (!*next || *next == sep)) {
2505 str++;
2506 found_esc = true;
2507 }
2508 }
2509
2510 if (!found_esc)
2511 for (; *end && (*end != sep); end++)
2512 /* find sep or end of string */;
2513
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002514 strlen = end - str;
2515 if (strlen) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002516 p = xdr_reserve_space(xdr, strlen + 4);
2517 if (!p)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002518 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04002519 p = xdr_encode_opaque(p, str, strlen);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002520 count++;
2521 }
2522 else
2523 end++;
Benjamin Coddington5a64e562014-12-07 16:05:47 -05002524 if (found_esc)
2525 end = next;
2526
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002527 str = end;
2528 }
Benjamin Coddingtonbf7491f2014-12-07 16:05:48 -05002529 pathlen = htonl(count);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002530 write_bytes_to_xdr_buf(xdr->buf, pathlen_offset, &pathlen, 4);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002531 return 0;
2532}
2533
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002534/* Encode as an array of strings the string given with components
2535 * separated @sep.
2536 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002537static __be32 nfsd4_encode_components(struct xdr_stream *xdr, char sep,
2538 char *components)
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002539{
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002540 return nfsd4_encode_components_esc(xdr, sep, components, 0, 0);
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002541}
2542
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002543/*
2544 * encode a location element of a fs_locations structure
2545 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002546static __be32 nfsd4_encode_fs_location4(struct xdr_stream *xdr,
2547 struct nfsd4_fs_location *location)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002548{
Al Virob37ad282006-10-19 23:28:59 -07002549 __be32 status;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002550
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002551 status = nfsd4_encode_components_esc(xdr, ':', location->hosts,
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002552 '[', ']');
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002553 if (status)
2554 return status;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002555 status = nfsd4_encode_components(xdr, '/', location->path);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002556 if (status)
2557 return status;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002558 return 0;
2559}
2560
2561/*
Trond Myklebusted748aa2011-09-12 19:37:06 -04002562 * Encode a path in RFC3530 'pathname4' format
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002563 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002564static __be32 nfsd4_encode_path(struct xdr_stream *xdr,
2565 const struct path *root,
2566 const struct path *path)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002567{
Al Viro301f0262013-09-01 16:06:39 -04002568 struct path cur = *path;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002569 __be32 *p;
Trond Myklebusted748aa2011-09-12 19:37:06 -04002570 struct dentry **components = NULL;
2571 unsigned int ncomponents = 0;
2572 __be32 err = nfserr_jukebox;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002573
Trond Myklebusted748aa2011-09-12 19:37:06 -04002574 dprintk("nfsd4_encode_components(");
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002575
Trond Myklebusted748aa2011-09-12 19:37:06 -04002576 path_get(&cur);
2577 /* First walk the path up to the nfsd root, and store the
2578 * dentries/path components in an array.
2579 */
2580 for (;;) {
Kinglong Meeb77a4b22015-03-15 23:13:32 +08002581 if (path_equal(&cur, root))
Trond Myklebusted748aa2011-09-12 19:37:06 -04002582 break;
2583 if (cur.dentry == cur.mnt->mnt_root) {
2584 if (follow_up(&cur))
2585 continue;
2586 goto out_free;
2587 }
2588 if ((ncomponents & 15) == 0) {
2589 struct dentry **new;
2590 new = krealloc(components,
2591 sizeof(*new) * (ncomponents + 16),
2592 GFP_KERNEL);
2593 if (!new)
2594 goto out_free;
2595 components = new;
2596 }
2597 components[ncomponents++] = cur.dentry;
2598 cur.dentry = dget_parent(cur.dentry);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002599 }
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002600 err = nfserr_resource;
2601 p = xdr_reserve_space(xdr, 4);
2602 if (!p)
Trond Myklebusted748aa2011-09-12 19:37:06 -04002603 goto out_free;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002604 *p++ = cpu_to_be32(ncomponents);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002605
2606 while (ncomponents) {
2607 struct dentry *dentry = components[ncomponents - 1];
Al Viro301f0262013-09-01 16:06:39 -04002608 unsigned int len;
Trond Myklebusted748aa2011-09-12 19:37:06 -04002609
Al Viro301f0262013-09-01 16:06:39 -04002610 spin_lock(&dentry->d_lock);
2611 len = dentry->d_name.len;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002612 p = xdr_reserve_space(xdr, len + 4);
2613 if (!p) {
Al Viro301f0262013-09-01 16:06:39 -04002614 spin_unlock(&dentry->d_lock);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002615 goto out_free;
Al Viro301f0262013-09-01 16:06:39 -04002616 }
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04002617 p = xdr_encode_opaque(p, dentry->d_name.name, len);
Al Viroa4555892014-10-21 20:11:25 -04002618 dprintk("/%pd", dentry);
Al Viro301f0262013-09-01 16:06:39 -04002619 spin_unlock(&dentry->d_lock);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002620 dput(dentry);
2621 ncomponents--;
2622 }
2623
Trond Myklebusted748aa2011-09-12 19:37:06 -04002624 err = 0;
2625out_free:
2626 dprintk(")\n");
2627 while (ncomponents)
2628 dput(components[--ncomponents]);
2629 kfree(components);
2630 path_put(&cur);
2631 return err;
2632}
2633
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002634static __be32 nfsd4_encode_fsloc_fsroot(struct xdr_stream *xdr,
2635 struct svc_rqst *rqstp, const struct path *path)
Trond Myklebusted748aa2011-09-12 19:37:06 -04002636{
2637 struct svc_export *exp_ps;
2638 __be32 res;
2639
2640 exp_ps = rqst_find_fsidzero_export(rqstp);
2641 if (IS_ERR(exp_ps))
2642 return nfserrno(PTR_ERR(exp_ps));
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002643 res = nfsd4_encode_path(xdr, &exp_ps->ex_path, path);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002644 exp_put(exp_ps);
2645 return res;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002646}
2647
2648/*
2649 * encode a fs_locations structure
2650 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002651static __be32 nfsd4_encode_fs_locations(struct xdr_stream *xdr,
2652 struct svc_rqst *rqstp, struct svc_export *exp)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002653{
Al Virob37ad282006-10-19 23:28:59 -07002654 __be32 status;
Al Virocc45f012006-10-19 23:28:44 -07002655 int i;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002656 __be32 *p;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002657 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002658
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002659 status = nfsd4_encode_fsloc_fsroot(xdr, rqstp, &exp->ex_path);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002660 if (status)
2661 return status;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002662 p = xdr_reserve_space(xdr, 4);
2663 if (!p)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002664 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002665 *p++ = cpu_to_be32(fslocs->locations_count);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002666 for (i=0; i<fslocs->locations_count; i++) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002667 status = nfsd4_encode_fs_location4(xdr, &fslocs->locations[i]);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002668 if (status)
2669 return status;
2670 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002671 return 0;
2672}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002674static u32 nfs4_file_type(umode_t mode)
2675{
2676 switch (mode & S_IFMT) {
2677 case S_IFIFO: return NF4FIFO;
2678 case S_IFCHR: return NF4CHR;
2679 case S_IFDIR: return NF4DIR;
2680 case S_IFBLK: return NF4BLK;
2681 case S_IFLNK: return NF4LNK;
2682 case S_IFREG: return NF4REG;
2683 case S_IFSOCK: return NF4SOCK;
2684 default: return NF4BAD;
Tom Rix25fef482020-11-01 07:32:34 -08002685 }
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002686}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
Al Virob37ad282006-10-19 23:28:59 -07002688static inline __be32
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002689nfsd4_encode_aclname(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2690 struct nfs4_ace *ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691{
J. Bruce Fields35541162014-01-08 09:49:01 -05002692 if (ace->whotype != NFS4_ACL_WHO_NAMED)
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002693 return nfs4_acl_write_who(xdr, ace->whotype);
J. Bruce Fields35541162014-01-08 09:49:01 -05002694 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002695 return nfsd4_encode_group(xdr, rqstp, ace->who_gid);
J. Bruce Fields35541162014-01-08 09:49:01 -05002696 else
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002697 return nfsd4_encode_user(xdr, rqstp, ace->who_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698}
2699
Kinglong Mee6896f152015-07-30 21:52:44 +08002700static inline __be32
Jeff Layton8a4c3922016-07-10 15:55:58 -04002701nfsd4_encode_layout_types(struct xdr_stream *xdr, u32 layout_types)
Kinglong Mee6896f152015-07-30 21:52:44 +08002702{
Jeff Layton8a4c3922016-07-10 15:55:58 -04002703 __be32 *p;
2704 unsigned long i = hweight_long(layout_types);
Kinglong Mee6896f152015-07-30 21:52:44 +08002705
Jeff Layton8a4c3922016-07-10 15:55:58 -04002706 p = xdr_reserve_space(xdr, 4 + 4 * i);
2707 if (!p)
2708 return nfserr_resource;
2709
2710 *p++ = cpu_to_be32(i);
2711
2712 for (i = LAYOUT_NFSV4_1_FILES; i < LAYOUT_TYPE_MAX; ++i)
2713 if (layout_types & (1 << i))
2714 *p++ = cpu_to_be32(i);
Kinglong Mee6896f152015-07-30 21:52:44 +08002715
2716 return 0;
2717}
2718
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002719#define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
2720 FATTR4_WORD0_RDATTR_ERROR)
2721#define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
Kinglong Meec2227a32015-07-07 10:16:37 +08002722#define WORD2_ABSENT_FS_ATTRS 0
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002723
David Quigley18032ca2013-05-02 13:19:10 -04002724#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2725static inline __be32
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002726nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2727 void *context, int len)
David Quigley18032ca2013-05-02 13:19:10 -04002728{
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002729 __be32 *p;
David Quigley18032ca2013-05-02 13:19:10 -04002730
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002731 p = xdr_reserve_space(xdr, len + 4 + 4 + 4);
2732 if (!p)
David Quigley18032ca2013-05-02 13:19:10 -04002733 return nfserr_resource;
2734
2735 /*
2736 * For now we use a 0 here to indicate the null translation; in
2737 * the future we may place a call to translation code here.
2738 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002739 *p++ = cpu_to_be32(0); /* lfs */
2740 *p++ = cpu_to_be32(0); /* pi */
David Quigley18032ca2013-05-02 13:19:10 -04002741 p = xdr_encode_opaque(p, context, len);
David Quigley18032ca2013-05-02 13:19:10 -04002742 return 0;
2743}
2744#else
2745static inline __be32
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002746nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2747 void *context, int len)
David Quigley18032ca2013-05-02 13:19:10 -04002748{ return 0; }
2749#endif
2750
Kinglong Meec2227a32015-07-07 10:16:37 +08002751static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *bmval2, u32 *rdattr_err)
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002752{
2753 /* As per referral draft: */
2754 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
2755 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
2756 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
2757 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
2758 *rdattr_err = NFSERR_MOVED;
2759 else
2760 return nfserr_moved;
2761 }
2762 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
2763 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
Kinglong Meec2227a32015-07-07 10:16:37 +08002764 *bmval2 &= WORD2_ABSENT_FS_ATTRS;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002765 return 0;
2766}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002768
2769static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
2770{
2771 struct path path = exp->ex_path;
2772 int err;
2773
2774 path_get(&path);
2775 while (follow_up(&path)) {
2776 if (path.dentry != path.mnt->mnt_root)
2777 break;
2778 }
David Howellsa528d352017-01-31 16:46:22 +00002779 err = vfs_getattr(&path, stat, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002780 path_put(&path);
2781 return err;
2782}
2783
Kinglong Mee75976de2015-07-30 21:55:30 +08002784static __be32
2785nfsd4_encode_bitmap(struct xdr_stream *xdr, u32 bmval0, u32 bmval1, u32 bmval2)
2786{
2787 __be32 *p;
2788
2789 if (bmval2) {
2790 p = xdr_reserve_space(xdr, 16);
2791 if (!p)
2792 goto out_resource;
2793 *p++ = cpu_to_be32(3);
2794 *p++ = cpu_to_be32(bmval0);
2795 *p++ = cpu_to_be32(bmval1);
2796 *p++ = cpu_to_be32(bmval2);
2797 } else if (bmval1) {
2798 p = xdr_reserve_space(xdr, 12);
2799 if (!p)
2800 goto out_resource;
2801 *p++ = cpu_to_be32(2);
2802 *p++ = cpu_to_be32(bmval0);
2803 *p++ = cpu_to_be32(bmval1);
2804 } else {
2805 p = xdr_reserve_space(xdr, 8);
2806 if (!p)
2807 goto out_resource;
2808 *p++ = cpu_to_be32(1);
2809 *p++ = cpu_to_be32(bmval0);
2810 }
2811
2812 return 0;
2813out_resource:
2814 return nfserr_resource;
2815}
2816
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817/*
2818 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
2819 * ourselves.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 */
Jeff Laytonda2ebce2014-05-30 09:09:25 -04002821static __be32
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04002822nfsd4_encode_fattr(struct xdr_stream *xdr, struct svc_fh *fhp,
2823 struct svc_export *exp,
2824 struct dentry *dentry, u32 *bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002825 struct svc_rqst *rqstp, int ignore_crossmnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826{
2827 u32 bmval0 = bmval[0];
2828 u32 bmval1 = bmval[1];
Andy Adamson7e705702009-04-03 08:29:11 +03002829 u32 bmval2 = bmval[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 struct kstat stat;
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002831 struct svc_fh *tempfh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 struct kstatfs statfs;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002833 __be32 *p;
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05002834 int starting_len = xdr->buf->len;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002835 int attrlen_offset;
2836 __be32 attrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 u32 dummy;
2838 u64 dummy64;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002839 u32 rdattr_err = 0;
Al Virob37ad282006-10-19 23:28:59 -07002840 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07002841 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 struct nfs4_acl *acl = NULL;
Arnd Bergmann0ab88ca2019-03-22 15:07:11 +01002843#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
David Quigley18032ca2013-05-02 13:19:10 -04002844 void *context = NULL;
2845 int contextlen;
Arnd Bergmann0ab88ca2019-03-22 15:07:11 +01002846#endif
David Quigley18032ca2013-05-02 13:19:10 -04002847 bool contextsupport = false;
Andy Adamson7e705702009-04-03 08:29:11 +03002848 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2849 u32 minorversion = resp->cstate.minorversion;
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002850 struct path path = {
2851 .mnt = exp->ex_path.mnt,
2852 .dentry = dentry,
2853 };
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03002854 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
2856 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002857 BUG_ON(!nfsd_attrs_supported(minorversion, bmval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002859 if (exp->ex_fslocs.migrated) {
Kinglong Meec2227a32015-07-07 10:16:37 +08002860 status = fattr_handle_absent_fs(&bmval0, &bmval1, &bmval2, &rdattr_err);
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002861 if (status)
2862 goto out;
2863 }
2864
David Howellsa528d352017-01-31 16:46:22 +00002865 err = vfs_getattr(&path, &stat, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
Al Virob8dd7b92006-10-19 23:29:01 -07002866 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 goto out_nfserr;
Christoph Hellwig12337902014-05-28 10:46:13 +02002868 if ((bmval0 & (FATTR4_WORD0_FILES_AVAIL | FATTR4_WORD0_FILES_FREE |
2869 FATTR4_WORD0_FILES_TOTAL | FATTR4_WORD0_MAXNAME)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2871 FATTR4_WORD1_SPACE_TOTAL))) {
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002872 err = vfs_statfs(&path, &statfs);
Al Virob8dd7b92006-10-19 23:29:01 -07002873 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 goto out_nfserr;
2875 }
2876 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002877 tempfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
2878 status = nfserr_jukebox;
2879 if (!tempfh)
2880 goto out;
2881 fh_init(tempfh, NFS4_FHSIZE);
2882 status = fh_compose(tempfh, exp, dentry, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 if (status)
2884 goto out;
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002885 fhp = tempfh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 }
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02002887 if (bmval0 & FATTR4_WORD0_ACL) {
Al Virob8dd7b92006-10-19 23:29:01 -07002888 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02002889 if (err == -EOPNOTSUPP)
2890 bmval0 &= ~FATTR4_WORD0_ACL;
2891 else if (err == -EINVAL) {
2892 status = nfserr_attrnotsupp;
2893 goto out;
2894 } else if (err != 0)
2895 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
David Quigley18032ca2013-05-02 13:19:10 -04002898#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
Kinglong Meec2227a32015-07-07 10:16:37 +08002899 if ((bmval2 & FATTR4_WORD2_SECURITY_LABEL) ||
2900 bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
J. Bruce Fields32ddd942017-01-03 12:30:11 -05002901 if (exp->ex_flags & NFSEXP_SECURITY_LABEL)
2902 err = security_inode_getsecctx(d_inode(dentry),
David Quigley18032ca2013-05-02 13:19:10 -04002903 &context, &contextlen);
J. Bruce Fields32ddd942017-01-03 12:30:11 -05002904 else
2905 err = -EOPNOTSUPP;
David Quigley18032ca2013-05-02 13:19:10 -04002906 contextsupport = (err == 0);
2907 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2908 if (err == -EOPNOTSUPP)
2909 bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2910 else if (err)
2911 goto out_nfserr;
2912 }
2913 }
2914#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2915
Kinglong Mee75976de2015-07-30 21:55:30 +08002916 status = nfsd4_encode_bitmap(xdr, bmval0, bmval1, bmval2);
2917 if (status)
2918 goto out;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002919
2920 attrlen_offset = xdr->buf->len;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002921 p = xdr_reserve_space(xdr, 4);
2922 if (!p)
2923 goto out_resource;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002924 p++; /* to be backfilled later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
2926 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
J. Bruce Fieldsdcd208692017-01-11 20:34:50 -05002927 u32 supp[3];
2928
2929 memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
Andy Adamson7e705702009-04-03 08:29:11 +03002930
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02002931 if (!IS_POSIXACL(dentry->d_inode))
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002932 supp[0] &= ~FATTR4_WORD0_ACL;
David Quigley18032ca2013-05-02 13:19:10 -04002933 if (!contextsupport)
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002934 supp[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2935 if (!supp[2]) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002936 p = xdr_reserve_space(xdr, 12);
2937 if (!p)
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002938 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002939 *p++ = cpu_to_be32(2);
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002940 *p++ = cpu_to_be32(supp[0]);
2941 *p++ = cpu_to_be32(supp[1]);
Andy Adamson7e705702009-04-03 08:29:11 +03002942 } else {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002943 p = xdr_reserve_space(xdr, 16);
2944 if (!p)
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002945 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002946 *p++ = cpu_to_be32(3);
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002947 *p++ = cpu_to_be32(supp[0]);
2948 *p++ = cpu_to_be32(supp[1]);
2949 *p++ = cpu_to_be32(supp[2]);
Andy Adamson7e705702009-04-03 08:29:11 +03002950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 }
2952 if (bmval0 & FATTR4_WORD0_TYPE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002953 p = xdr_reserve_space(xdr, 4);
2954 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 goto out_resource;
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002956 dummy = nfs4_file_type(stat.mode);
J. Bruce Fields6b6d81372013-01-16 17:11:11 -05002957 if (dummy == NF4BAD) {
2958 status = nfserr_serverfault;
2959 goto out;
2960 }
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002961 *p++ = cpu_to_be32(dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 }
2963 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002964 p = xdr_reserve_space(xdr, 4);
2965 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 goto out_resource;
NeilBrown49640002005-06-23 22:02:58 -07002967 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002968 *p++ = cpu_to_be32(NFS4_FH_PERSISTENT);
NeilBrown49640002005-06-23 22:02:58 -07002969 else
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002970 *p++ = cpu_to_be32(NFS4_FH_PERSISTENT|
2971 NFS4_FH_VOL_RENAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 }
2973 if (bmval0 & FATTR4_WORD0_CHANGE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002974 p = xdr_reserve_space(xdr, 8);
2975 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 goto out_resource;
NeilBrownb8800922017-01-30 17:17:00 +11002977 p = encode_change(p, &stat, d_inode(dentry), exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 }
2979 if (bmval0 & FATTR4_WORD0_SIZE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002980 p = xdr_reserve_space(xdr, 8);
2981 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002983 p = xdr_encode_hyper(p, stat.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 }
2985 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002986 p = xdr_reserve_space(xdr, 4);
2987 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002989 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 }
2991 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002992 p = xdr_reserve_space(xdr, 4);
2993 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002995 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 }
2997 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002998 p = xdr_reserve_space(xdr, 4);
2999 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003001 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 }
3003 if (bmval0 & FATTR4_WORD0_FSID) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003004 p = xdr_reserve_space(xdr, 16);
3005 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07003007 if (exp->ex_fslocs.migrated) {
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003008 p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MAJOR);
3009 p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MINOR);
NeilBrownaf6a4e22007-02-14 00:33:12 -08003010 } else switch(fsid_source(fhp)) {
3011 case FSIDSOURCE_FSID:
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003012 p = xdr_encode_hyper(p, (u64)exp->ex_fsid);
3013 p = xdr_encode_hyper(p, (u64)0);
NeilBrownaf6a4e22007-02-14 00:33:12 -08003014 break;
3015 case FSIDSOURCE_DEV:
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003016 *p++ = cpu_to_be32(0);
3017 *p++ = cpu_to_be32(MAJOR(stat.dev));
3018 *p++ = cpu_to_be32(0);
3019 *p++ = cpu_to_be32(MINOR(stat.dev));
NeilBrownaf6a4e22007-02-14 00:33:12 -08003020 break;
3021 case FSIDSOURCE_UUID:
Kinglong Mee94eb3682014-05-23 20:00:19 +08003022 p = xdr_encode_opaque_fixed(p, exp->ex_uuid,
3023 EX_UUID_LEN);
NeilBrownaf6a4e22007-02-14 00:33:12 -08003024 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 }
3026 }
3027 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003028 p = xdr_reserve_space(xdr, 4);
3029 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003031 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 }
3033 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003034 p = xdr_reserve_space(xdr, 4);
3035 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003037 *p++ = cpu_to_be32(nn->nfsd4_lease);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 }
3039 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003040 p = xdr_reserve_space(xdr, 4);
3041 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003043 *p++ = cpu_to_be32(rdattr_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 }
3045 if (bmval0 & FATTR4_WORD0_ACL) {
3046 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
3048 if (acl == NULL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003049 p = xdr_reserve_space(xdr, 4);
3050 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 goto out_resource;
3052
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003053 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 goto out_acl;
3055 }
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003056 p = xdr_reserve_space(xdr, 4);
3057 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003059 *p++ = cpu_to_be32(acl->naces);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08003061 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003062 p = xdr_reserve_space(xdr, 4*3);
3063 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003065 *p++ = cpu_to_be32(ace->type);
3066 *p++ = cpu_to_be32(ace->flag);
3067 *p++ = cpu_to_be32(ace->access_mask &
3068 NFS4_ACE_MASK_ALL);
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003069 status = nfsd4_encode_aclname(xdr, rqstp, ace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 if (status)
3071 goto out;
3072 }
3073 }
3074out_acl:
3075 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003076 p = xdr_reserve_space(xdr, 4);
3077 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 goto out_resource;
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02003079 *p++ = cpu_to_be32(IS_POSIXACL(dentry->d_inode) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
3081 }
3082 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003083 p = xdr_reserve_space(xdr, 4);
3084 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003086 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 }
3088 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003089 p = xdr_reserve_space(xdr, 4);
3090 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003092 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 }
3094 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003095 p = xdr_reserve_space(xdr, 4);
3096 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003098 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 }
3100 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003101 p = xdr_reserve_space(xdr, 4);
3102 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003104 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 }
3106 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003107 p = xdr_reserve_space(xdr, fhp->fh_handle.fh_size + 4);
3108 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 goto out_resource;
NeilBrownd8b26072021-09-02 11:16:32 +10003110 p = xdr_encode_opaque(p, &fhp->fh_handle.fh_raw,
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003111 fhp->fh_handle.fh_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 }
3113 if (bmval0 & FATTR4_WORD0_FILEID) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003114 p = xdr_reserve_space(xdr, 8);
3115 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003117 p = xdr_encode_hyper(p, stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 }
3119 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003120 p = xdr_reserve_space(xdr, 8);
3121 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003123 p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 }
3125 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003126 p = xdr_reserve_space(xdr, 8);
3127 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003129 p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 }
3131 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003132 p = xdr_reserve_space(xdr, 8);
3133 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003135 p = xdr_encode_hyper(p, (u64) statfs.f_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07003137 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003138 status = nfsd4_encode_fs_locations(xdr, rqstp, exp);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07003139 if (status)
3140 goto out;
3141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003143 p = xdr_reserve_space(xdr, 4);
3144 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003146 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 }
3148 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003149 p = xdr_reserve_space(xdr, 8);
3150 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003152 p = xdr_encode_hyper(p, exp->ex_path.mnt->mnt_sb->s_maxbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 }
3154 if (bmval0 & FATTR4_WORD0_MAXLINK) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003155 p = xdr_reserve_space(xdr, 4);
3156 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003158 *p++ = cpu_to_be32(255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 }
3160 if (bmval0 & FATTR4_WORD0_MAXNAME) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003161 p = xdr_reserve_space(xdr, 4);
3162 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003164 *p++ = cpu_to_be32(statfs.f_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 }
3166 if (bmval0 & FATTR4_WORD0_MAXREAD) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003167 p = xdr_reserve_space(xdr, 8);
3168 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003170 p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 }
3172 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003173 p = xdr_reserve_space(xdr, 8);
3174 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003176 p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 }
3178 if (bmval1 & FATTR4_WORD1_MODE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003179 p = xdr_reserve_space(xdr, 4);
3180 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003182 *p++ = cpu_to_be32(stat.mode & S_IALLUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 }
3184 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003185 p = xdr_reserve_space(xdr, 4);
3186 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003188 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 }
3190 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003191 p = xdr_reserve_space(xdr, 4);
3192 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003194 *p++ = cpu_to_be32(stat.nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 }
3196 if (bmval1 & FATTR4_WORD1_OWNER) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003197 status = nfsd4_encode_user(xdr, rqstp, stat.uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 if (status)
3199 goto out;
3200 }
3201 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003202 status = nfsd4_encode_group(xdr, rqstp, stat.gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 if (status)
3204 goto out;
3205 }
3206 if (bmval1 & FATTR4_WORD1_RAWDEV) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003207 p = xdr_reserve_space(xdr, 8);
3208 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003210 *p++ = cpu_to_be32((u32) MAJOR(stat.rdev));
3211 *p++ = cpu_to_be32((u32) MINOR(stat.rdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 }
3213 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003214 p = xdr_reserve_space(xdr, 8);
3215 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 goto out_resource;
3217 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003218 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 }
3220 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003221 p = xdr_reserve_space(xdr, 8);
3222 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 goto out_resource;
3224 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003225 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 }
3227 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003228 p = xdr_reserve_space(xdr, 8);
3229 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 goto out_resource;
3231 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003232 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 }
3234 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003235 p = xdr_reserve_space(xdr, 8);
3236 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 goto out_resource;
3238 dummy64 = (u64)stat.blocks << 9;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003239 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 }
3241 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003242 p = xdr_reserve_space(xdr, 12);
3243 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003245 p = xdr_encode_hyper(p, (s64)stat.atime.tv_sec);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003246 *p++ = cpu_to_be32(stat.atime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 }
3248 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003249 p = xdr_reserve_space(xdr, 12);
3250 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 goto out_resource;
J. Bruce Fields16945142018-04-25 14:34:11 -04003252 p = encode_time_delta(p, d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 }
3254 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003255 p = xdr_reserve_space(xdr, 12);
3256 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003258 p = xdr_encode_hyper(p, (s64)stat.ctime.tv_sec);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003259 *p++ = cpu_to_be32(stat.ctime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 }
3261 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003262 p = xdr_reserve_space(xdr, 12);
3263 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003265 p = xdr_encode_hyper(p, (s64)stat.mtime.tv_sec);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003266 *p++ = cpu_to_be32(stat.mtime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 }
3268 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
Kinglong Mee0a2050d2015-07-30 21:51:54 +08003269 struct kstat parent_stat;
3270 u64 ino = stat.ino;
3271
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003272 p = xdr_reserve_space(xdr, 8);
3273 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 goto out_resource;
Frank Filz406a7ea2007-11-27 11:34:05 -08003275 /*
3276 * Get parent's attributes if not ignoring crossmount
3277 * and this is the root of a cross-mounted filesystem.
3278 */
3279 if (ignore_crossmnt == 0 &&
Kinglong Mee0a2050d2015-07-30 21:51:54 +08003280 dentry == exp->ex_path.mnt->mnt_root) {
3281 err = get_parent_attributes(exp, &parent_stat);
3282 if (err)
3283 goto out_nfserr;
3284 ino = parent_stat.ino;
3285 }
3286 p = xdr_encode_hyper(p, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003288#ifdef CONFIG_NFSD_PNFS
Kinglong Mee6896f152015-07-30 21:52:44 +08003289 if (bmval1 & FATTR4_WORD1_FS_LAYOUT_TYPES) {
Jeff Layton8a4c3922016-07-10 15:55:58 -04003290 status = nfsd4_encode_layout_types(xdr, exp->ex_layout_types);
Kinglong Mee6896f152015-07-30 21:52:44 +08003291 if (status)
3292 goto out;
3293 }
3294
3295 if (bmval2 & FATTR4_WORD2_LAYOUT_TYPES) {
Jeff Layton8a4c3922016-07-10 15:55:58 -04003296 status = nfsd4_encode_layout_types(xdr, exp->ex_layout_types);
Kinglong Mee6896f152015-07-30 21:52:44 +08003297 if (status)
3298 goto out;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003299 }
3300
3301 if (bmval2 & FATTR4_WORD2_LAYOUT_BLKSIZE) {
3302 p = xdr_reserve_space(xdr, 4);
3303 if (!p)
3304 goto out_resource;
3305 *p++ = cpu_to_be32(stat.blksize);
3306 }
3307#endif /* CONFIG_NFSD_PNFS */
Benny Halevy8c18f202009-04-03 08:29:14 +03003308 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
Trond Myklebustb26b78c2017-05-09 16:24:59 -04003309 u32 supp[3];
3310
3311 memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
3312 supp[0] &= NFSD_SUPPATTR_EXCLCREAT_WORD0;
3313 supp[1] &= NFSD_SUPPATTR_EXCLCREAT_WORD1;
3314 supp[2] &= NFSD_SUPPATTR_EXCLCREAT_WORD2;
3315
3316 status = nfsd4_encode_bitmap(xdr, supp[0], supp[1], supp[2]);
Kinglong Mee75976de2015-07-30 21:55:30 +08003317 if (status)
3318 goto out;
Benny Halevy8c18f202009-04-03 08:29:14 +03003319 }
Andy Adamson7e705702009-04-03 08:29:11 +03003320
Arnd Bergmann0ab88ca2019-03-22 15:07:11 +01003321#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
Kinglong Mee7d580722015-07-30 21:54:26 +08003322 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
3323 status = nfsd4_encode_security_label(xdr, rqstp, context,
3324 contextlen);
3325 if (status)
3326 goto out;
3327 }
Arnd Bergmann0ab88ca2019-03-22 15:07:11 +01003328#endif
Kinglong Mee7d580722015-07-30 21:54:26 +08003329
Frank van der Linden0e885e82020-06-23 22:39:27 +00003330 if (bmval2 & FATTR4_WORD2_XATTR_SUPPORT) {
3331 p = xdr_reserve_space(xdr, 4);
3332 if (!p)
3333 goto out_resource;
3334 err = xattr_supported_namespace(d_inode(dentry),
3335 XATTR_USER_PREFIX);
3336 *p++ = cpu_to_be32(err == 0);
3337 }
3338
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04003339 attrlen = htonl(xdr->buf->len - attrlen_offset - 4);
3340 write_bytes_to_xdr_buf(xdr->buf, attrlen_offset, &attrlen, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 status = nfs_ok;
3342
3343out:
J. Bruce Fieldsba4e55b2013-05-15 10:27:52 -04003344#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
David Quigley18032ca2013-05-02 13:19:10 -04003345 if (context)
3346 security_release_secctx(context, contextlen);
J. Bruce Fieldsba4e55b2013-05-15 10:27:52 -04003347#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08003348 kfree(acl);
Yan, Zheng18df11d02014-03-10 12:52:07 +08003349 if (tempfh) {
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05003350 fh_put(tempfh);
Yan, Zheng18df11d02014-03-10 12:52:07 +08003351 kfree(tempfh);
3352 }
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05003353 if (status)
3354 xdr_truncate_encode(xdr, starting_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 return status;
3356out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07003357 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 goto out;
3359out_resource:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 status = nfserr_resource;
3361 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362}
3363
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04003364static void svcxdr_init_encode_from_buffer(struct xdr_stream *xdr,
3365 struct xdr_buf *buf, __be32 *p, int bytes)
3366{
3367 xdr->scratch.iov_len = 0;
3368 memset(buf, 0, sizeof(struct xdr_buf));
3369 buf->head[0].iov_base = p;
3370 buf->head[0].iov_len = 0;
3371 buf->len = 0;
3372 xdr->buf = buf;
3373 xdr->iov = buf->head;
3374 xdr->p = p;
3375 xdr->end = (void *)p + bytes;
3376 buf->buflen = bytes;
3377}
3378
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04003379__be32 nfsd4_encode_fattr_to_buf(__be32 **p, int words,
3380 struct svc_fh *fhp, struct svc_export *exp,
3381 struct dentry *dentry, u32 *bmval,
3382 struct svc_rqst *rqstp, int ignore_crossmnt)
3383{
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04003384 struct xdr_buf dummy;
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04003385 struct xdr_stream xdr;
3386 __be32 ret;
3387
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04003388 svcxdr_init_encode_from_buffer(&xdr, &dummy, *p, words << 2);
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04003389 ret = nfsd4_encode_fattr(&xdr, fhp, exp, dentry, bmval, rqstp,
3390 ignore_crossmnt);
3391 *p = xdr.p;
3392 return ret;
3393}
3394
J. Bruce Fieldsc0ce6ec2008-02-11 15:48:47 -05003395static inline int attributes_need_mount(u32 *bmval)
3396{
3397 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
3398 return 1;
3399 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
3400 return 1;
3401 return 0;
3402}
3403
Al Virob37ad282006-10-19 23:28:59 -07003404static __be32
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003405nfsd4_encode_dirent_fattr(struct xdr_stream *xdr, struct nfsd4_readdir *cd,
3406 const char *name, int namlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407{
3408 struct svc_export *exp = cd->rd_fhp->fh_export;
3409 struct dentry *dentry;
Al Virob37ad282006-10-19 23:28:59 -07003410 __be32 nfserr;
Frank Filz406a7ea2007-11-27 11:34:05 -08003411 int ignore_crossmnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412
Al Viro6c2d47982019-10-31 01:21:58 -04003413 dentry = lookup_positive_unlocked(name, cd->rd_fhp->fh_dentry, namlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 if (IS_ERR(dentry))
3415 return nfserrno(PTR_ERR(dentry));
3416
3417 exp_get(exp);
Frank Filz406a7ea2007-11-27 11:34:05 -08003418 /*
3419 * In the case of a mountpoint, the client may be asking for
3420 * attributes that are only properties of the underlying filesystem
3421 * as opposed to the cross-mounted file system. In such a case,
3422 * we will not follow the cross mount and will fill the attribtutes
3423 * directly from the mountpoint dentry.
3424 */
J. Bruce Fields3227fa42009-10-25 21:43:01 -04003425 if (nfsd_mountpoint(dentry, exp)) {
J.Bruce Fields021d3a72006-12-13 00:35:24 -08003426 int err;
3427
J. Bruce Fields3227fa42009-10-25 21:43:01 -04003428 if (!(exp->ex_flags & NFSEXP_V4ROOT)
3429 && !attributes_need_mount(cd->rd_bmval)) {
3430 ignore_crossmnt = 1;
3431 goto out_encode;
3432 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07003433 /*
3434 * Why the heck aren't we just using nfsd_lookup??
3435 * Different "."/".." handling? Something else?
3436 * At least, add a comment here to explain....
3437 */
J.Bruce Fields021d3a72006-12-13 00:35:24 -08003438 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
3439 if (err) {
3440 nfserr = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 goto out_put;
3442 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07003443 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
3444 if (nfserr)
3445 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446
3447 }
J. Bruce Fields3227fa42009-10-25 21:43:01 -04003448out_encode:
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003449 nfserr = nfsd4_encode_fattr(xdr, NULL, exp, dentry, cd->rd_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08003450 cd->rd_rqstp, ignore_crossmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451out_put:
3452 dput(dentry);
3453 exp_put(exp);
3454 return nfserr;
3455}
3456
Al Viro2ebbc012006-10-19 23:28:58 -07003457static __be32 *
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003458nfsd4_encode_rdattr_error(struct xdr_stream *xdr, __be32 nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459{
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003460 __be32 *p;
3461
Kinglong Meec3a45612014-07-06 11:34:43 +08003462 p = xdr_reserve_space(xdr, 20);
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003463 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 return NULL;
3465 *p++ = htonl(2);
3466 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
3467 *p++ = htonl(0); /* bmval1 */
3468
J. Bruce Fields87915c62013-01-16 17:33:28 -05003469 *p++ = htonl(4); /* attribute length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 *p++ = nfserr; /* no htonl */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 return p;
3472}
3473
3474static int
NeilBrowna0ad13e2007-01-26 00:57:10 -08003475nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
3476 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477{
NeilBrowna0ad13e2007-01-26 00:57:10 -08003478 struct readdir_cd *ccd = ccdv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003480 struct xdr_stream *xdr = cd->xdr;
3481 int start_offset = xdr->buf->len;
3482 int cookie_offset;
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04003483 u32 name_and_cookie;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003484 int entry_bytes;
Al Virob37ad282006-10-19 23:28:59 -07003485 __be32 nfserr = nfserr_toosmall;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003486 __be64 wire_offset;
3487 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488
3489 /* In nfsv4, "." and ".." never make it onto the wire.. */
3490 if (name && isdotent(name, namlen)) {
3491 cd->common.err = nfs_ok;
3492 return 0;
3493 }
3494
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003495 if (cd->cookie_offset) {
3496 wire_offset = cpu_to_be64(offset);
3497 write_bytes_to_xdr_buf(xdr->buf, cd->cookie_offset,
3498 &wire_offset, 8);
3499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003501 p = xdr_reserve_space(xdr, 4);
3502 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 *p++ = xdr_one; /* mark entry present */
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003505 cookie_offset = xdr->buf->len;
3506 p = xdr_reserve_space(xdr, 3*4 + namlen);
3507 if (!p)
3508 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
3510 p = xdr_encode_array(p, name, namlen); /* name length & name */
3511
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003512 nfserr = nfsd4_encode_dirent_fattr(xdr, cd, name, namlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 switch (nfserr) {
3514 case nfs_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 break;
3516 case nfserr_resource:
3517 nfserr = nfserr_toosmall;
3518 goto fail;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04003519 case nfserr_noent:
Kinglong Meef41c5ad2014-06-13 17:32:42 +08003520 xdr_truncate_encode(xdr, start_offset);
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04003521 goto skip_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 default:
3523 /*
3524 * If the client requested the RDATTR_ERROR attribute,
3525 * we stuff the error code into this attribute
3526 * and continue. If this attribute was not requested,
3527 * then in accordance with the spec, we fail the
3528 * entire READDIR operation(!)
3529 */
3530 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
3531 goto fail;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003532 p = nfsd4_encode_rdattr_error(xdr, nfserr);
Fred Isaman34081ef2006-01-18 17:43:40 -08003533 if (p == NULL) {
3534 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 goto fail;
Fred Isaman34081ef2006-01-18 17:43:40 -08003536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 }
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003538 nfserr = nfserr_toosmall;
3539 entry_bytes = xdr->buf->len - start_offset;
3540 if (entry_bytes > cd->rd_maxcount)
3541 goto fail;
3542 cd->rd_maxcount -= entry_bytes;
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04003543 /*
Trond Myklebustf2e717d2021-09-30 15:44:41 -04003544 * RFC 3530 14.2.24 describes rd_dircount as only a "hint", and
3545 * notes that it could be zero. If it is zero, then the server
3546 * should enforce only the rd_maxcount value.
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04003547 */
Trond Myklebustf2e717d2021-09-30 15:44:41 -04003548 if (cd->rd_dircount) {
3549 name_and_cookie = 4 + 4 * XDR_QUADLEN(namlen) + 8;
3550 if (name_and_cookie > cd->rd_dircount && cd->cookie_offset)
3551 goto fail;
3552 cd->rd_dircount -= min(cd->rd_dircount, name_and_cookie);
3553 if (!cd->rd_dircount)
3554 cd->rd_maxcount = 0;
3555 }
J. Bruce Fields0ec016e2014-12-19 18:01:35 -05003556
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003557 cd->cookie_offset = cookie_offset;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04003558skip_entry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 cd->common.err = nfs_ok;
3560 return 0;
3561fail:
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003562 xdr_truncate_encode(xdr, start_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 cd->common.err = nfserr;
3564 return -EINVAL;
3565}
3566
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003567static __be32
3568nfsd4_encode_stateid(struct xdr_stream *xdr, stateid_t *sid)
Benny Halevye2f282b2008-08-12 20:45:07 +03003569{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003570 __be32 *p;
Benny Halevye2f282b2008-08-12 20:45:07 +03003571
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003572 p = xdr_reserve_space(xdr, sizeof(stateid_t));
3573 if (!p)
3574 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003575 *p++ = cpu_to_be32(sid->si_generation);
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003576 p = xdr_encode_opaque_fixed(p, &sid->si_opaque,
3577 sizeof(stateid_opaque_t));
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003578 return 0;
Benny Halevye2f282b2008-08-12 20:45:07 +03003579}
3580
Benny Halevy695e12f2008-07-04 14:38:33 +03003581static __be32
Al Virob37ad282006-10-19 23:28:59 -07003582nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003584 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003585 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003587 p = xdr_reserve_space(xdr, 8);
3588 if (!p)
3589 return nfserr_resource;
3590 *p++ = cpu_to_be32(access->ac_supported);
3591 *p++ = cpu_to_be32(access->ac_resp_access);
3592 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593}
3594
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003595static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
3596{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003597 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003598 __be32 *p;
3599
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003600 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 8);
3601 if (!p)
3602 return nfserr_resource;
3603 p = xdr_encode_opaque_fixed(p, bcts->sessionid.data,
3604 NFS4_MAX_SESSIONID_LEN);
3605 *p++ = cpu_to_be32(bcts->dir);
3606 /* Upshifting from TCP to RDMA is not supported */
3607 *p++ = cpu_to_be32(0);
3608 return 0;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003609}
3610
Benny Halevy695e12f2008-07-04 14:38:33 +03003611static __be32
Al Virob37ad282006-10-19 23:28:59 -07003612nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003614 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003615
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003616 return nfsd4_encode_stateid(xdr, &close->cl_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617}
3618
3619
Benny Halevy695e12f2008-07-04 14:38:33 +03003620static __be32
Al Virob37ad282006-10-19 23:28:59 -07003621nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003623 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003624 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003626 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
3627 if (!p)
3628 return nfserr_resource;
3629 p = xdr_encode_opaque_fixed(p, commit->co_verf.data,
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003630 NFS4_VERIFIER_SIZE);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003631 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632}
3633
Benny Halevy695e12f2008-07-04 14:38:33 +03003634static __be32
Al Virob37ad282006-10-19 23:28:59 -07003635nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003637 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003638 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003640 p = xdr_reserve_space(xdr, 20);
3641 if (!p)
3642 return nfserr_resource;
3643 encode_cinfo(p, &create->cr_cinfo);
Trond Myklebustb96811c2019-08-18 14:18:58 -04003644 return nfsd4_encode_bitmap(xdr, create->cr_bmval[0],
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003645 create->cr_bmval[1], create->cr_bmval[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646}
3647
Al Virob37ad282006-10-19 23:28:59 -07003648static __be32
3649nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650{
3651 struct svc_fh *fhp = getattr->ga_fhp;
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003652 struct xdr_stream *xdr = resp->xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003654 return nfsd4_encode_fattr(xdr, fhp, fhp->fh_export, fhp->fh_dentry,
3655 getattr->ga_bmval, resp->rqstp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656}
3657
Benny Halevy695e12f2008-07-04 14:38:33 +03003658static __be32
3659nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003661 struct xdr_stream *xdr = resp->xdr;
Benny Halevy695e12f2008-07-04 14:38:33 +03003662 struct svc_fh *fhp = *fhpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 unsigned int len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003664 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003666 len = fhp->fh_handle.fh_size;
3667 p = xdr_reserve_space(xdr, len + 4);
3668 if (!p)
3669 return nfserr_resource;
NeilBrownd8b26072021-09-02 11:16:32 +10003670 p = xdr_encode_opaque(p, &fhp->fh_handle.fh_raw, len);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003671 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672}
3673
3674/*
3675* Including all fields other than the name, a LOCK4denied structure requires
3676* 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
3677*/
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003678static __be32
3679nfsd4_encode_lock_denied(struct xdr_stream *xdr, struct nfsd4_lock_denied *ld)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680{
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003681 struct xdr_netobj *conf = &ld->ld_owner;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003682 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003684again:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003685 p = xdr_reserve_space(xdr, 32 + XDR_LEN(conf->len));
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003686 if (!p) {
3687 /*
3688 * Don't fail to return the result just because we can't
3689 * return the conflicting open:
3690 */
3691 if (conf->len) {
Kinglong Meef98bac52014-07-07 22:10:56 +08003692 kfree(conf->data);
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003693 conf->len = 0;
3694 conf->data = NULL;
3695 goto again;
3696 }
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003697 return nfserr_resource;
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003698 }
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003699 p = xdr_encode_hyper(p, ld->ld_start);
3700 p = xdr_encode_hyper(p, ld->ld_length);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003701 *p++ = cpu_to_be32(ld->ld_type);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003702 if (conf->len) {
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003703 p = xdr_encode_opaque_fixed(p, &ld->ld_clientid, 8);
3704 p = xdr_encode_opaque(p, conf->data, conf->len);
Kinglong Meef98bac52014-07-07 22:10:56 +08003705 kfree(conf->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003707 p = xdr_encode_hyper(p, (u64)0); /* clientid */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003708 *p++ = cpu_to_be32(0); /* length of owner name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 }
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003710 return nfserr_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711}
3712
Benny Halevy695e12f2008-07-04 14:38:33 +03003713static __be32
Al Virob37ad282006-10-19 23:28:59 -07003714nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003716 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003717
Benny Halevye2f282b2008-08-12 20:45:07 +03003718 if (!nfserr)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003719 nfserr = nfsd4_encode_stateid(xdr, &lock->lk_resp_stateid);
Benny Halevye2f282b2008-08-12 20:45:07 +03003720 else if (nfserr == nfserr_denied)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003721 nfserr = nfsd4_encode_lock_denied(xdr, &lock->lk_denied);
Kinglong Meef98bac52014-07-07 22:10:56 +08003722
Benny Halevy695e12f2008-07-04 14:38:33 +03003723 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724}
3725
Benny Halevy695e12f2008-07-04 14:38:33 +03003726static __be32
Al Virob37ad282006-10-19 23:28:59 -07003727nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003729 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003730
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 if (nfserr == nfserr_denied)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003732 nfsd4_encode_lock_denied(xdr, &lockt->lt_denied);
Benny Halevy695e12f2008-07-04 14:38:33 +03003733 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734}
3735
Benny Halevy695e12f2008-07-04 14:38:33 +03003736static __be32
Al Virob37ad282006-10-19 23:28:59 -07003737nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003739 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003740
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003741 return nfsd4_encode_stateid(xdr, &locku->lu_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742}
3743
3744
Benny Halevy695e12f2008-07-04 14:38:33 +03003745static __be32
Al Virob37ad282006-10-19 23:28:59 -07003746nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003748 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003749 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003751 p = xdr_reserve_space(xdr, 20);
3752 if (!p)
3753 return nfserr_resource;
3754 p = encode_cinfo(p, &link->li_cinfo);
3755 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756}
3757
3758
Benny Halevy695e12f2008-07-04 14:38:33 +03003759static __be32
Al Virob37ad282006-10-19 23:28:59 -07003760nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003762 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003763 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003765 nfserr = nfsd4_encode_stateid(xdr, &open->op_stateid);
3766 if (nfserr)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003767 return nfserr;
Kinglong Mee75976de2015-07-30 21:55:30 +08003768 p = xdr_reserve_space(xdr, 24);
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003769 if (!p)
3770 return nfserr_resource;
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04003771 p = encode_cinfo(p, &open->op_cinfo);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003772 *p++ = cpu_to_be32(open->op_rflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773
Kinglong Mee75976de2015-07-30 21:55:30 +08003774 nfserr = nfsd4_encode_bitmap(xdr, open->op_bmval[0], open->op_bmval[1],
3775 open->op_bmval[2]);
3776 if (nfserr)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003777 return nfserr;
Kinglong Mee75976de2015-07-30 21:55:30 +08003778
3779 p = xdr_reserve_space(xdr, 4);
3780 if (!p)
3781 return nfserr_resource;
3782
3783 *p++ = cpu_to_be32(open->op_delegate_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 switch (open->op_delegate_type) {
3785 case NFS4_OPEN_DELEGATE_NONE:
3786 break;
3787 case NFS4_OPEN_DELEGATE_READ:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003788 nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
3789 if (nfserr)
3790 return nfserr;
3791 p = xdr_reserve_space(xdr, 20);
3792 if (!p)
3793 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003794 *p++ = cpu_to_be32(open->op_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795
3796 /*
3797 * TODO: ACE's in delegations
3798 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003799 *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
3800 *p++ = cpu_to_be32(0);
3801 *p++ = cpu_to_be32(0);
3802 *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 break;
3804 case NFS4_OPEN_DELEGATE_WRITE:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003805 nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
3806 if (nfserr)
3807 return nfserr;
3808 p = xdr_reserve_space(xdr, 32);
3809 if (!p)
3810 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003811 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812
3813 /*
3814 * TODO: space_limit's in delegations
3815 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003816 *p++ = cpu_to_be32(NFS4_LIMIT_SIZE);
3817 *p++ = cpu_to_be32(~(u32)0);
3818 *p++ = cpu_to_be32(~(u32)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819
3820 /*
3821 * TODO: ACE's in delegations
3822 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003823 *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
3824 *p++ = cpu_to_be32(0);
3825 *p++ = cpu_to_be32(0);
3826 *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 break;
Benny Halevyd24433c2012-02-16 20:57:17 +02003828 case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
3829 switch (open->op_why_no_deleg) {
3830 case WND4_CONTENTION:
3831 case WND4_RESOURCE:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003832 p = xdr_reserve_space(xdr, 8);
3833 if (!p)
3834 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003835 *p++ = cpu_to_be32(open->op_why_no_deleg);
3836 /* deleg signaling not supported yet: */
3837 *p++ = cpu_to_be32(0);
Benny Halevyd24433c2012-02-16 20:57:17 +02003838 break;
3839 default:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003840 p = xdr_reserve_space(xdr, 4);
3841 if (!p)
3842 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003843 *p++ = cpu_to_be32(open->op_why_no_deleg);
Benny Halevyd24433c2012-02-16 20:57:17 +02003844 }
Benny Halevyd24433c2012-02-16 20:57:17 +02003845 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 default:
3847 BUG();
3848 }
3849 /* XXX save filehandle here */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003850 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851}
3852
Benny Halevy695e12f2008-07-04 14:38:33 +03003853static __be32
Al Virob37ad282006-10-19 23:28:59 -07003854nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003856 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003857
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003858 return nfsd4_encode_stateid(xdr, &oc->oc_resp_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859}
3860
Benny Halevy695e12f2008-07-04 14:38:33 +03003861static __be32
Al Virob37ad282006-10-19 23:28:59 -07003862nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003864 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003865
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003866 return nfsd4_encode_stateid(xdr, &od->od_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867}
3868
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003869static __be32 nfsd4_encode_splice_read(
3870 struct nfsd4_compoundres *resp,
3871 struct nfsd4_read *read,
3872 struct file *file, unsigned long maxcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003874 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003875 struct xdr_buf *buf = xdr->buf;
Chuck Lever76e54922020-11-05 10:24:19 -05003876 int status, space_left;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003877 u32 eof;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003878 __be32 nfserr;
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003879 __be32 *p = xdr->p - 2;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003880
Kinglong Meed5d5c302014-07-09 21:51:27 +08003881 /* Make sure there will be room for padding if needed */
3882 if (xdr->end - xdr->p < 1)
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003883 return nfserr_resource;
3884
Chuck Lever87c59422018-03-28 13:29:11 -04003885 nfserr = nfsd_splice_read(read->rd_rqstp, read->rd_fhp,
Trond Myklebust83a63072019-08-26 13:03:11 -04003886 file, read->rd_offset, &maxcount, &eof);
Chuck Lever87c59422018-03-28 13:29:11 -04003887 read->rd_length = maxcount;
Chuck Lever76e54922020-11-05 10:24:19 -05003888 if (nfserr)
3889 goto out_err;
3890 status = svc_encode_result_payload(read->rd_rqstp,
3891 buf->head[0].iov_len, maxcount);
3892 if (status) {
3893 nfserr = nfserrno(status);
3894 goto out_err;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003895 }
3896
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003897 *(p++) = htonl(eof);
3898 *(p++) = htonl(maxcount);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003899
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003900 buf->page_len = maxcount;
3901 buf->len += maxcount;
J. Bruce Fields15b23ef2014-09-24 16:32:34 -04003902 xdr->page_ptr += (buf->page_base + maxcount + PAGE_SIZE - 1)
3903 / PAGE_SIZE;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003904
3905 /* Use rest of head for padding and remaining ops: */
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003906 buf->tail[0].iov_base = xdr->p;
3907 buf->tail[0].iov_len = 0;
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003908 xdr->iov = buf->tail;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003909 if (maxcount&3) {
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003910 int pad = 4 - (maxcount&3);
3911
3912 *(xdr->p++) = 0;
3913
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003914 buf->tail[0].iov_base += maxcount&3;
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003915 buf->tail[0].iov_len = pad;
3916 buf->len += pad;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003917 }
3918
3919 space_left = min_t(int, (void *)xdr->end - (void *)xdr->p,
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003920 buf->buflen - buf->len);
3921 buf->buflen = buf->len + space_left;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003922 xdr->end = (__be32 *)((void *)xdr->end + space_left);
3923
3924 return 0;
Chuck Lever76e54922020-11-05 10:24:19 -05003925
3926out_err:
3927 /*
3928 * nfsd_splice_actor may have already messed with the
3929 * page length; reset it so as not to confuse
3930 * xdr_truncate_encode in our caller.
3931 */
3932 buf->page_len = 0;
3933 return nfserr;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003934}
3935
3936static __be32 nfsd4_encode_readv(struct nfsd4_compoundres *resp,
3937 struct nfsd4_read *read,
3938 struct file *file, unsigned long maxcount)
3939{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003940 struct xdr_stream *xdr = resp->xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 u32 eof;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003942 int starting_len = xdr->buf->len - 8;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003943 __be32 nfserr;
3944 __be32 tmp;
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003945 int pad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946
Anna Schumaker403217f2020-08-17 12:53:06 -04003947 read->rd_vlen = xdr_reserve_space_vec(xdr, resp->rqstp->rq_vec, maxcount);
3948 if (read->rd_vlen < 0)
3949 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950
Chuck Lever87c59422018-03-28 13:29:11 -04003951 nfserr = nfsd_readv(resp->rqstp, read->rd_fhp, file, read->rd_offset,
Trond Myklebust83a63072019-08-26 13:03:11 -04003952 resp->rqstp->rq_vec, read->rd_vlen, &maxcount,
3953 &eof);
Chuck Lever87c59422018-03-28 13:29:11 -04003954 read->rd_length = maxcount;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003955 if (nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 return nfserr;
Chuck Lever03493bc2020-06-10 10:36:42 -04003957 if (svc_encode_result_payload(resp->rqstp, starting_len + 8, maxcount))
Chuck Lever41205532020-03-02 14:45:53 -05003958 return nfserr_io;
Chuck Lever7dcf4ab2020-03-02 15:00:04 -05003959 xdr_truncate_encode(xdr, starting_len + 8 + xdr_align_size(maxcount));
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003960
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003961 tmp = htonl(eof);
3962 write_bytes_to_xdr_buf(xdr->buf, starting_len , &tmp, 4);
3963 tmp = htonl(maxcount);
3964 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp, 4);
3965
Chuck Lever7dcf4ab2020-03-02 15:00:04 -05003966 tmp = xdr_zero;
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003967 pad = (maxcount&3) ? 4 - (maxcount&3) : 0;
3968 write_bytes_to_xdr_buf(xdr->buf, starting_len + 8 + maxcount,
Chuck Lever7dcf4ab2020-03-02 15:00:04 -05003969 &tmp, pad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 return 0;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003971
3972}
3973
3974static __be32
3975nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
3976 struct nfsd4_read *read)
3977{
3978 unsigned long maxcount;
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003979 struct xdr_stream *xdr = resp->xdr;
Jeff Layton5c4583b2019-08-18 14:18:54 -04003980 struct file *file;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003981 int starting_len = xdr->buf->len;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003982 __be32 *p;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003983
Jeff Layton5c4583b2019-08-18 14:18:54 -04003984 if (nfserr)
3985 return nfserr;
3986 file = read->rd_nf->nf_file;
3987
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003988 p = xdr_reserve_space(xdr, 8); /* eof flag and byte count */
3989 if (!p) {
Jeff Layton779fb0f2014-11-19 07:51:18 -05003990 WARN_ON_ONCE(test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags));
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003991 return nfserr_resource;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003992 }
Chuck Leverbddfdbc2020-10-27 15:53:42 -04003993 if (resp->xdr->buf->page_len &&
Christoph Hellwig68e8bb02015-06-18 16:45:02 +02003994 test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags)) {
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003995 WARN_ON_ONCE(1);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003996 return nfserr_resource;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003997 }
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003998 xdr_commit_encode(xdr);
3999
4000 maxcount = svc_max_payload(resp->rqstp);
Christoph Hellwig68e8bb02015-06-18 16:45:02 +02004001 maxcount = min_t(unsigned long, maxcount,
4002 (xdr->buf->buflen - xdr->buf->len));
Kinglong Mee3c7aa152014-06-10 18:08:19 +08004003 maxcount = min_t(unsigned long, maxcount, read->rd_length);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004004
Christoph Hellwig68e8bb02015-06-18 16:45:02 +02004005 if (file->f_op->splice_read &&
4006 test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags))
Christoph Hellwig96bcad52015-06-18 16:45:01 +02004007 nfserr = nfsd4_encode_splice_read(resp, read, file, maxcount);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004008 else
Christoph Hellwig96bcad52015-06-18 16:45:01 +02004009 nfserr = nfsd4_encode_readv(resp, read, file, maxcount);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004010
Christoph Hellwig96bcad52015-06-18 16:45:01 +02004011 if (nfserr)
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004012 xdr_truncate_encode(xdr, starting_len);
Christoph Hellwig96bcad52015-06-18 16:45:01 +02004013
Christoph Hellwig96bcad52015-06-18 16:45:01 +02004014 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015}
4016
Al Virob37ad282006-10-19 23:28:59 -07004017static __be32
4018nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019{
4020 int maxcount;
J. Bruce Fields476a7b12014-01-20 17:08:27 -05004021 __be32 wire_count;
4022 int zero = 0;
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004023 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05004024 int length_offset = xdr->buf->len;
Chuck Lever76e54922020-11-05 10:24:19 -05004025 int status;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004026 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04004028 p = xdr_reserve_space(xdr, 4);
4029 if (!p)
4030 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 maxcount = PAGE_SIZE;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004032
J. Bruce Fields476a7b12014-01-20 17:08:27 -05004033 p = xdr_reserve_space(xdr, maxcount);
4034 if (!p)
J. Bruce Fields4e21ac42014-03-22 15:15:11 -04004035 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 /*
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004037 * XXX: By default, vfs_readlink() will truncate symlinks if they
4038 * would overflow the buffer. Is this kosher in NFSv4? If not, one
4039 * easy fix is: if vfs_readlink() precisely fills the buffer, assume
4040 * that truncation occurred, and return NFS4ERR_RESOURCE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 */
J. Bruce Fields476a7b12014-01-20 17:08:27 -05004042 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp,
4043 (char *)p, &maxcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 if (nfserr == nfserr_isdir)
J. Bruce Fieldsd3f627c2014-02-26 17:00:38 -05004045 nfserr = nfserr_inval;
Chuck Lever76e54922020-11-05 10:24:19 -05004046 if (nfserr)
4047 goto out_err;
4048 status = svc_encode_result_payload(readlink->rl_rqstp, length_offset,
4049 maxcount);
4050 if (status) {
4051 nfserr = nfserrno(status);
4052 goto out_err;
J. Bruce Fieldsd3f627c2014-02-26 17:00:38 -05004053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054
J. Bruce Fields476a7b12014-01-20 17:08:27 -05004055 wire_count = htonl(maxcount);
4056 write_bytes_to_xdr_buf(xdr->buf, length_offset, &wire_count, 4);
Avi Kivity69bbd9c2014-06-26 17:23:12 +03004057 xdr_truncate_encode(xdr, length_offset + 4 + ALIGN(maxcount, 4));
J. Bruce Fields476a7b12014-01-20 17:08:27 -05004058 if (maxcount & 3)
4059 write_bytes_to_xdr_buf(xdr->buf, length_offset + 4 + maxcount,
4060 &zero, 4 - (maxcount&3));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 return 0;
Chuck Lever76e54922020-11-05 10:24:19 -05004062
4063out_err:
4064 xdr_truncate_encode(xdr, length_offset);
4065 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066}
4067
Al Virob37ad282006-10-19 23:28:59 -07004068static __be32
4069nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070{
4071 int maxcount;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004072 int bytes_left;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 loff_t offset;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004074 __be64 wire_offset;
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004075 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05004076 int starting_len = xdr->buf->len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004077 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004079 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
4080 if (!p)
4081 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082
4083 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004084 *p++ = cpu_to_be32(0);
4085 *p++ = cpu_to_be32(0);
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004086 xdr->buf->head[0].iov_len = (char *)xdr->p -
4087 (char *)xdr->buf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088
4089 /*
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004090 * Number of bytes left for directory entries allowing for the
4091 * final 8 bytes of the readdir and a following failed op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 */
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004093 bytes_left = xdr->buf->buflen - xdr->buf->len
4094 - COMPOUND_ERR_SLACK_SPACE - 8;
4095 if (bytes_left < 0) {
4096 nfserr = nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 goto err_no_verf;
4098 }
Scott Mayhew9c2ece62018-05-07 09:01:08 -04004099 maxcount = svc_max_payload(resp->rqstp);
4100 maxcount = min_t(u32, readdir->rd_maxcount, maxcount);
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004101 /*
4102 * Note the rfc defines rd_maxcount as the size of the
4103 * READDIR4resok structure, which includes the verifier above
4104 * and the 8 bytes encoded at the end of this function:
4105 */
4106 if (maxcount < 16) {
4107 nfserr = nfserr_toosmall;
4108 goto err_no_verf;
4109 }
4110 maxcount = min_t(int, maxcount-16, bytes_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04004112 /* RFC 3530 14.2.24 allows us to ignore dircount when it's 0: */
4113 if (!readdir->rd_dircount)
Scott Mayhew9c2ece62018-05-07 09:01:08 -04004114 readdir->rd_dircount = svc_max_payload(resp->rqstp);
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04004115
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004116 readdir->xdr = xdr;
4117 readdir->rd_maxcount = maxcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 readdir->common.err = 0;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004119 readdir->cookie_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120
4121 offset = readdir->rd_cookie;
4122 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
4123 &offset,
4124 &readdir->common, nfsd4_encode_dirent);
4125 if (nfserr == nfs_ok &&
4126 readdir->common.err == nfserr_toosmall &&
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004127 xdr->buf->len == starting_len + 8) {
4128 /* nothing encoded; which limit did we hit?: */
4129 if (maxcount - 16 < bytes_left)
4130 /* It was the fault of rd_maxcount: */
4131 nfserr = nfserr_toosmall;
4132 else
4133 /* We ran out of buffer space: */
4134 nfserr = nfserr_resource;
4135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 if (nfserr)
4137 goto err_no_verf;
4138
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004139 if (readdir->cookie_offset) {
4140 wire_offset = cpu_to_be64(offset);
4141 write_bytes_to_xdr_buf(xdr->buf, readdir->cookie_offset,
4142 &wire_offset, 8);
4143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004145 p = xdr_reserve_space(xdr, 8);
4146 if (!p) {
4147 WARN_ON_ONCE(1);
4148 goto err_no_verf;
4149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 *p++ = 0; /* no more entries */
4151 *p++ = htonl(readdir->common.err == nfserr_eof);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152
4153 return 0;
4154err_no_verf:
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05004155 xdr_truncate_encode(xdr, starting_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 return nfserr;
4157}
4158
Benny Halevy695e12f2008-07-04 14:38:33 +03004159static __be32
Al Virob37ad282006-10-19 23:28:59 -07004160nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004162 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004163 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004165 p = xdr_reserve_space(xdr, 20);
4166 if (!p)
4167 return nfserr_resource;
4168 p = encode_cinfo(p, &remove->rm_cinfo);
4169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170}
4171
Benny Halevy695e12f2008-07-04 14:38:33 +03004172static __be32
Al Virob37ad282006-10-19 23:28:59 -07004173nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004175 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004176 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004178 p = xdr_reserve_space(xdr, 40);
4179 if (!p)
4180 return nfserr_resource;
4181 p = encode_cinfo(p, &rename->rn_sinfo);
4182 p = encode_cinfo(p, &rename->rn_tinfo);
4183 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184}
4185
Benny Halevy695e12f2008-07-04 14:38:33 +03004186static __be32
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004187nfsd4_do_encode_secinfo(struct xdr_stream *xdr, struct svc_export *exp)
Andy Adamsondcb488a32007-07-17 04:04:51 -07004188{
Chuck Lever676e4eb2013-04-30 18:48:54 -04004189 u32 i, nflavs, supported;
J. Bruce Fields4796f452007-07-17 04:04:51 -07004190 struct exp_flavor_info *flavs;
4191 struct exp_flavor_info def_flavs[2];
Chuck Lever676e4eb2013-04-30 18:48:54 -04004192 __be32 *p, *flavorsp;
4193 static bool report = true;
Andy Adamsondcb488a32007-07-17 04:04:51 -07004194
J. Bruce Fields4796f452007-07-17 04:04:51 -07004195 if (exp->ex_nflavors) {
4196 flavs = exp->ex_flavors;
4197 nflavs = exp->ex_nflavors;
4198 } else { /* Handling of some defaults in absence of real secinfo: */
4199 flavs = def_flavs;
4200 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
4201 nflavs = 2;
4202 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
4203 flavs[1].pseudoflavor = RPC_AUTH_NULL;
4204 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
4205 nflavs = 1;
4206 flavs[0].pseudoflavor
4207 = svcauth_gss_flavor(exp->ex_client);
4208 } else {
4209 nflavs = 1;
4210 flavs[0].pseudoflavor
4211 = exp->ex_client->flavour->flavour;
4212 }
4213 }
4214
Chuck Lever676e4eb2013-04-30 18:48:54 -04004215 supported = 0;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004216 p = xdr_reserve_space(xdr, 4);
4217 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004218 return nfserr_resource;
Chuck Lever676e4eb2013-04-30 18:48:54 -04004219 flavorsp = p++; /* to be backfilled later */
Chuck Lever676e4eb2013-04-30 18:48:54 -04004220
J. Bruce Fields4796f452007-07-17 04:04:51 -07004221 for (i = 0; i < nflavs; i++) {
Chuck Lever676e4eb2013-04-30 18:48:54 -04004222 rpc_authflavor_t pf = flavs[i].pseudoflavor;
Chuck Levera77c8062013-03-16 15:55:10 -04004223 struct rpcsec_gss_info info;
Andy Adamsondcb488a32007-07-17 04:04:51 -07004224
Chuck Lever676e4eb2013-04-30 18:48:54 -04004225 if (rpcauth_get_gssinfo(pf, &info) == 0) {
4226 supported++;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004227 p = xdr_reserve_space(xdr, 4 + 4 +
4228 XDR_LEN(info.oid.len) + 4 + 4);
4229 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004230 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004231 *p++ = cpu_to_be32(RPC_AUTH_GSS);
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004232 p = xdr_encode_opaque(p, info.oid.data, info.oid.len);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004233 *p++ = cpu_to_be32(info.qop);
4234 *p++ = cpu_to_be32(info.service);
Chuck Lever676e4eb2013-04-30 18:48:54 -04004235 } else if (pf < RPC_AUTH_MAXFLAVOR) {
4236 supported++;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004237 p = xdr_reserve_space(xdr, 4);
4238 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004239 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004240 *p++ = cpu_to_be32(pf);
Chuck Lever676e4eb2013-04-30 18:48:54 -04004241 } else {
4242 if (report)
4243 pr_warn("NFS: SECINFO: security flavor %u "
4244 "is not supported\n", pf);
Andy Adamsondcb488a32007-07-17 04:04:51 -07004245 }
4246 }
Chuck Levera77c8062013-03-16 15:55:10 -04004247
Chuck Lever676e4eb2013-04-30 18:48:54 -04004248 if (nflavs != supported)
4249 report = false;
4250 *flavorsp = htonl(supported);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004251 return 0;
Andy Adamsondcb488a32007-07-17 04:04:51 -07004252}
4253
Mi Jinlong22b6dee2010-12-27 14:29:57 +08004254static __be32
4255nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
4256 struct nfsd4_secinfo *secinfo)
4257{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004258 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004259
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004260 return nfsd4_do_encode_secinfo(xdr, secinfo->si_exp);
Mi Jinlong22b6dee2010-12-27 14:29:57 +08004261}
4262
4263static __be32
4264nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
4265 struct nfsd4_secinfo_no_name *secinfo)
4266{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004267 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004268
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004269 return nfsd4_do_encode_secinfo(xdr, secinfo->sin_exp);
Mi Jinlong22b6dee2010-12-27 14:29:57 +08004270}
4271
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272/*
4273 * The SETATTR encode routine is special -- it always encodes a bitmap,
4274 * regardless of the error status.
4275 */
Benny Halevy695e12f2008-07-04 14:38:33 +03004276static __be32
Al Virob37ad282006-10-19 23:28:59 -07004277nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004279 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004280 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004282 p = xdr_reserve_space(xdr, 16);
4283 if (!p)
4284 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 if (nfserr) {
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004286 *p++ = cpu_to_be32(3);
4287 *p++ = cpu_to_be32(0);
4288 *p++ = cpu_to_be32(0);
4289 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 }
4291 else {
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004292 *p++ = cpu_to_be32(3);
4293 *p++ = cpu_to_be32(setattr->sa_bmval[0]);
4294 *p++ = cpu_to_be32(setattr->sa_bmval[1]);
4295 *p++ = cpu_to_be32(setattr->sa_bmval[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 }
Benny Halevy695e12f2008-07-04 14:38:33 +03004297 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298}
4299
Benny Halevy695e12f2008-07-04 14:38:33 +03004300static __be32
Al Virob37ad282006-10-19 23:28:59 -07004301nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004303 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004304 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305
4306 if (!nfserr) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004307 p = xdr_reserve_space(xdr, 8 + NFS4_VERIFIER_SIZE);
4308 if (!p)
4309 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004310 p = xdr_encode_opaque_fixed(p, &scd->se_clientid, 8);
4311 p = xdr_encode_opaque_fixed(p, &scd->se_confirm,
4312 NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 }
4314 else if (nfserr == nfserr_clid_inuse) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004315 p = xdr_reserve_space(xdr, 8);
4316 if (!p)
4317 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004318 *p++ = cpu_to_be32(0);
4319 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 }
Benny Halevy695e12f2008-07-04 14:38:33 +03004321 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322}
4323
Benny Halevy695e12f2008-07-04 14:38:33 +03004324static __be32
Al Virob37ad282006-10-19 23:28:59 -07004325nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004327 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004328 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004330 p = xdr_reserve_space(xdr, 16);
4331 if (!p)
4332 return nfserr_resource;
4333 *p++ = cpu_to_be32(write->wr_bytes_written);
4334 *p++ = cpu_to_be32(write->wr_how_written);
4335 p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
4336 NFS4_VERIFIER_SIZE);
4337 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338}
4339
Benny Halevy695e12f2008-07-04 14:38:33 +03004340static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004341nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03004342 struct nfsd4_exchange_id *exid)
4343{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004344 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004345 __be32 *p;
Andy Adamson0733d212009-04-03 08:28:01 +03004346 char *major_id;
4347 char *server_scope;
4348 int major_id_sz;
4349 int server_scope_sz;
4350 uint64_t minor_id = 0;
Scott Mayhew7627d7d2020-02-19 15:52:15 -05004351 struct nfsd_net *nn = net_generic(SVC_NET(resp->rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03004352
Scott Mayhew7627d7d2020-02-19 15:52:15 -05004353 major_id = nn->nfsd_name;
4354 major_id_sz = strlen(nn->nfsd_name);
4355 server_scope = nn->nfsd_name;
4356 server_scope_sz = strlen(nn->nfsd_name);
Andy Adamson0733d212009-04-03 08:28:01 +03004357
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004358 p = xdr_reserve_space(xdr,
Andy Adamson0733d212009-04-03 08:28:01 +03004359 8 /* eir_clientid */ +
4360 4 /* eir_sequenceid */ +
4361 4 /* eir_flags */ +
Kinglong Meea8bb84b2013-12-10 15:24:36 +08004362 4 /* spr_how */);
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004363 if (!p)
4364 return nfserr_resource;
Andy Adamson0733d212009-04-03 08:28:01 +03004365
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004366 p = xdr_encode_opaque_fixed(p, &exid->clientid, 8);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004367 *p++ = cpu_to_be32(exid->seqid);
4368 *p++ = cpu_to_be32(exid->flags);
Andy Adamson0733d212009-04-03 08:28:01 +03004369
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004370 *p++ = cpu_to_be32(exid->spa_how);
Kinglong Meea8bb84b2013-12-10 15:24:36 +08004371
J. Bruce Fields57266a62013-04-13 14:27:29 -04004372 switch (exid->spa_how) {
4373 case SP4_NONE:
4374 break;
4375 case SP4_MACH_CRED:
4376 /* spo_must_enforce bitmap: */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004377 nfserr = nfsd4_encode_bitmap(xdr,
Andrew Elbleed941642016-06-15 12:52:09 -04004378 exid->spo_must_enforce[0],
4379 exid->spo_must_enforce[1],
4380 exid->spo_must_enforce[2]);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004381 if (nfserr)
4382 return nfserr;
Andrew Elbleed941642016-06-15 12:52:09 -04004383 /* spo_must_allow bitmap: */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004384 nfserr = nfsd4_encode_bitmap(xdr,
Andrew Elbleed941642016-06-15 12:52:09 -04004385 exid->spo_must_allow[0],
4386 exid->spo_must_allow[1],
4387 exid->spo_must_allow[2]);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004388 if (nfserr)
4389 return nfserr;
J. Bruce Fields57266a62013-04-13 14:27:29 -04004390 break;
4391 default:
4392 WARN_ON_ONCE(1);
4393 }
Andy Adamson0733d212009-04-03 08:28:01 +03004394
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004395 p = xdr_reserve_space(xdr,
Kinglong Meea8bb84b2013-12-10 15:24:36 +08004396 8 /* so_minor_id */ +
4397 4 /* so_major_id.len */ +
4398 (XDR_QUADLEN(major_id_sz) * 4) +
4399 4 /* eir_server_scope.len */ +
4400 (XDR_QUADLEN(server_scope_sz) * 4) +
4401 4 /* eir_server_impl_id.count (0) */);
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004402 if (!p)
4403 return nfserr_resource;
Kinglong Meea8bb84b2013-12-10 15:24:36 +08004404
Andy Adamson0733d212009-04-03 08:28:01 +03004405 /* The server_owner struct */
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04004406 p = xdr_encode_hyper(p, minor_id); /* Minor id */
Andy Adamson0733d212009-04-03 08:28:01 +03004407 /* major id */
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004408 p = xdr_encode_opaque(p, major_id, major_id_sz);
Andy Adamson0733d212009-04-03 08:28:01 +03004409
4410 /* Server scope */
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004411 p = xdr_encode_opaque(p, server_scope, server_scope_sz);
Andy Adamson0733d212009-04-03 08:28:01 +03004412
4413 /* Implementation id */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004414 *p++ = cpu_to_be32(0); /* zero length nfs_impl_id4 array */
Andy Adamson0733d212009-04-03 08:28:01 +03004415 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03004416}
4417
4418static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004419nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03004420 struct nfsd4_create_session *sess)
4421{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004422 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004423 __be32 *p;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004424
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004425 p = xdr_reserve_space(xdr, 24);
4426 if (!p)
4427 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004428 p = xdr_encode_opaque_fixed(p, sess->sessionid.data,
4429 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004430 *p++ = cpu_to_be32(sess->seqid);
4431 *p++ = cpu_to_be32(sess->flags);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004432
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004433 p = xdr_reserve_space(xdr, 28);
4434 if (!p)
4435 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004436 *p++ = cpu_to_be32(0); /* headerpadsz */
4437 *p++ = cpu_to_be32(sess->fore_channel.maxreq_sz);
4438 *p++ = cpu_to_be32(sess->fore_channel.maxresp_sz);
4439 *p++ = cpu_to_be32(sess->fore_channel.maxresp_cached);
4440 *p++ = cpu_to_be32(sess->fore_channel.maxops);
4441 *p++ = cpu_to_be32(sess->fore_channel.maxreqs);
4442 *p++ = cpu_to_be32(sess->fore_channel.nr_rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004443
4444 if (sess->fore_channel.nr_rdma_attrs) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004445 p = xdr_reserve_space(xdr, 4);
4446 if (!p)
4447 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004448 *p++ = cpu_to_be32(sess->fore_channel.rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004449 }
4450
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004451 p = xdr_reserve_space(xdr, 28);
4452 if (!p)
4453 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004454 *p++ = cpu_to_be32(0); /* headerpadsz */
4455 *p++ = cpu_to_be32(sess->back_channel.maxreq_sz);
4456 *p++ = cpu_to_be32(sess->back_channel.maxresp_sz);
4457 *p++ = cpu_to_be32(sess->back_channel.maxresp_cached);
4458 *p++ = cpu_to_be32(sess->back_channel.maxops);
4459 *p++ = cpu_to_be32(sess->back_channel.maxreqs);
4460 *p++ = cpu_to_be32(sess->back_channel.nr_rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004461
4462 if (sess->back_channel.nr_rdma_attrs) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004463 p = xdr_reserve_space(xdr, 4);
4464 if (!p)
4465 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004466 *p++ = cpu_to_be32(sess->back_channel.rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004467 }
4468 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03004469}
4470
4471static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004472nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03004473 struct nfsd4_sequence *seq)
4474{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004475 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004476 __be32 *p;
Benny Halevyb85d4c02009-04-03 08:28:08 +03004477
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004478 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 20);
4479 if (!p)
4480 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004481 p = xdr_encode_opaque_fixed(p, seq->sessionid.data,
4482 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004483 *p++ = cpu_to_be32(seq->seqid);
4484 *p++ = cpu_to_be32(seq->slotid);
J. Bruce Fieldsb7d7ca32011-08-31 15:39:30 -04004485 /* Note slotid's are numbered from zero: */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004486 *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_highest_slotid */
4487 *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_target_highest_slotid */
4488 *p++ = cpu_to_be32(seq->status_flags);
Benny Halevyb85d4c02009-04-03 08:28:08 +03004489
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04004490 resp->cstate.data_offset = xdr->buf->len; /* DRC cache data pointer */
Benny Halevyb85d4c02009-04-03 08:28:08 +03004491 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03004492}
4493
J. Bruce Fields2355c592012-04-25 16:56:22 -04004494static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004495nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
Bryan Schumaker17456802011-07-13 10:50:48 -04004496 struct nfsd4_test_stateid *test_stateid)
4497{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004498 struct xdr_stream *xdr = resp->xdr;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004499 struct nfsd4_test_stateid_id *stateid, *next;
Bryan Schumaker17456802011-07-13 10:50:48 -04004500 __be32 *p;
Bryan Schumaker17456802011-07-13 10:50:48 -04004501
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004502 p = xdr_reserve_space(xdr, 4 + (4 * test_stateid->ts_num_ids));
4503 if (!p)
4504 return nfserr_resource;
Bryan Schumaker17456802011-07-13 10:50:48 -04004505 *p++ = htonl(test_stateid->ts_num_ids);
Bryan Schumaker17456802011-07-13 10:50:48 -04004506
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004507 list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
Al Viro02f5fde2012-04-13 00:10:34 -04004508 *p++ = stateid->ts_id_status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004509 }
Bryan Schumaker17456802011-07-13 10:50:48 -04004510
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004511 return 0;
Bryan Schumaker17456802011-07-13 10:50:48 -04004512}
4513
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004514#ifdef CONFIG_NFSD_PNFS
4515static __be32
4516nfsd4_encode_getdeviceinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
4517 struct nfsd4_getdeviceinfo *gdev)
4518{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004519 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004520 const struct nfsd4_layout_ops *ops;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004521 u32 starting_len = xdr->buf->len, needed_len;
4522 __be32 *p;
4523
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004524 p = xdr_reserve_space(xdr, 4);
4525 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004526 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004527
4528 *p++ = cpu_to_be32(gdev->gd_layout_type);
4529
4530 /* If maxcount is 0 then just update notifications */
4531 if (gdev->gd_maxcount != 0) {
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004532 ops = nfsd4_layout_ops[gdev->gd_layout_type];
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004533 nfserr = ops->encode_getdeviceinfo(xdr, gdev);
4534 if (nfserr) {
4535 /*
4536 * We don't bother to burden the layout drivers with
4537 * enforcing gd_maxcount, just tell the client to
4538 * come back with a bigger buffer if it's not enough.
4539 */
4540 if (xdr->buf->len + 4 > gdev->gd_maxcount)
4541 goto toosmall;
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004542 return nfserr;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004543 }
4544 }
4545
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004546 if (gdev->gd_notify_types) {
4547 p = xdr_reserve_space(xdr, 4 + 4);
4548 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004549 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004550 *p++ = cpu_to_be32(1); /* bitmap length */
4551 *p++ = cpu_to_be32(gdev->gd_notify_types);
4552 } else {
4553 p = xdr_reserve_space(xdr, 4);
4554 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004555 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004556 *p++ = 0;
4557 }
4558
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004559 return 0;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004560toosmall:
4561 dprintk("%s: maxcount too small\n", __func__);
4562 needed_len = xdr->buf->len + 4 /* notifications */;
4563 xdr_truncate_encode(xdr, starting_len);
4564 p = xdr_reserve_space(xdr, 4);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004565 if (!p)
4566 return nfserr_resource;
4567 *p++ = cpu_to_be32(needed_len);
4568 return nfserr_toosmall;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004569}
4570
4571static __be32
4572nfsd4_encode_layoutget(struct nfsd4_compoundres *resp, __be32 nfserr,
4573 struct nfsd4_layoutget *lgp)
4574{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004575 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004576 const struct nfsd4_layout_ops *ops;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004577 __be32 *p;
4578
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004579 p = xdr_reserve_space(xdr, 36 + sizeof(stateid_opaque_t));
4580 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004581 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004582
4583 *p++ = cpu_to_be32(1); /* we always set return-on-close */
4584 *p++ = cpu_to_be32(lgp->lg_sid.si_generation);
4585 p = xdr_encode_opaque_fixed(p, &lgp->lg_sid.si_opaque,
4586 sizeof(stateid_opaque_t));
4587
4588 *p++ = cpu_to_be32(1); /* we always return a single layout */
4589 p = xdr_encode_hyper(p, lgp->lg_seg.offset);
4590 p = xdr_encode_hyper(p, lgp->lg_seg.length);
4591 *p++ = cpu_to_be32(lgp->lg_seg.iomode);
4592 *p++ = cpu_to_be32(lgp->lg_layout_type);
4593
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004594 ops = nfsd4_layout_ops[lgp->lg_layout_type];
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004595 return ops->encode_layoutget(xdr, lgp);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004596}
4597
4598static __be32
4599nfsd4_encode_layoutcommit(struct nfsd4_compoundres *resp, __be32 nfserr,
4600 struct nfsd4_layoutcommit *lcp)
4601{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004602 struct xdr_stream *xdr = resp->xdr;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004603 __be32 *p;
4604
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004605 p = xdr_reserve_space(xdr, 4);
4606 if (!p)
4607 return nfserr_resource;
4608 *p++ = cpu_to_be32(lcp->lc_size_chg);
4609 if (lcp->lc_size_chg) {
4610 p = xdr_reserve_space(xdr, 8);
4611 if (!p)
4612 return nfserr_resource;
4613 p = xdr_encode_hyper(p, lcp->lc_newsize);
4614 }
4615
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004616 return 0;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004617}
4618
4619static __be32
4620nfsd4_encode_layoutreturn(struct nfsd4_compoundres *resp, __be32 nfserr,
4621 struct nfsd4_layoutreturn *lrp)
4622{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004623 struct xdr_stream *xdr = resp->xdr;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004624 __be32 *p;
4625
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004626 p = xdr_reserve_space(xdr, 4);
4627 if (!p)
4628 return nfserr_resource;
4629 *p++ = cpu_to_be32(lrp->lrs_present);
4630 if (lrp->lrs_present)
Kinglong Mee376675d2015-03-22 22:17:10 +08004631 return nfsd4_encode_stateid(xdr, &lrp->lr_sid);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004632 return 0;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004633}
4634#endif /* CONFIG_NFSD_PNFS */
4635
Andy Adamson2db134e2009-04-03 08:27:55 +03004636static __be32
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004637nfsd42_encode_write_res(struct nfsd4_compoundres *resp,
4638 struct nfsd42_write_res *write, bool sync)
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004639{
4640 __be32 *p;
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004641 p = xdr_reserve_space(resp->xdr, 4);
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004642 if (!p)
4643 return nfserr_resource;
4644
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004645 if (sync)
4646 *p++ = cpu_to_be32(0);
4647 else {
4648 __be32 nfserr;
4649 *p++ = cpu_to_be32(1);
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004650 nfserr = nfsd4_encode_stateid(resp->xdr, &write->cb_stateid);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004651 if (nfserr)
4652 return nfserr;
4653 }
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004654 p = xdr_reserve_space(resp->xdr, 8 + 4 + NFS4_VERIFIER_SIZE);
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004655 if (!p)
4656 return nfserr_resource;
4657
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004658 p = xdr_encode_hyper(p, write->wr_bytes_written);
4659 *p++ = cpu_to_be32(write->wr_stable_how);
4660 p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
4661 NFS4_VERIFIER_SIZE);
4662 return nfs_ok;
4663}
4664
4665static __be32
Olga Kornievskaia51911862019-08-08 11:14:59 -04004666nfsd42_encode_nl4_server(struct nfsd4_compoundres *resp, struct nl4_server *ns)
4667{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004668 struct xdr_stream *xdr = resp->xdr;
Olga Kornievskaia51911862019-08-08 11:14:59 -04004669 struct nfs42_netaddr *addr;
4670 __be32 *p;
4671
4672 p = xdr_reserve_space(xdr, 4);
4673 *p++ = cpu_to_be32(ns->nl4_type);
4674
4675 switch (ns->nl4_type) {
4676 case NL4_NETADDR:
4677 addr = &ns->u.nl4_addr;
4678
4679 /* netid_len, netid, uaddr_len, uaddr (port included
4680 * in RPCBIND_MAXUADDRLEN)
4681 */
4682 p = xdr_reserve_space(xdr,
4683 4 /* netid len */ +
4684 (XDR_QUADLEN(addr->netid_len) * 4) +
4685 4 /* uaddr len */ +
4686 (XDR_QUADLEN(addr->addr_len) * 4));
4687 if (!p)
4688 return nfserr_resource;
4689
4690 *p++ = cpu_to_be32(addr->netid_len);
4691 p = xdr_encode_opaque_fixed(p, addr->netid,
4692 addr->netid_len);
4693 *p++ = cpu_to_be32(addr->addr_len);
4694 p = xdr_encode_opaque_fixed(p, addr->addr,
4695 addr->addr_len);
4696 break;
4697 default:
4698 WARN_ON_ONCE(ns->nl4_type != NL4_NETADDR);
4699 return nfserr_inval;
4700 }
4701
4702 return 0;
4703}
4704
4705static __be32
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004706nfsd4_encode_copy(struct nfsd4_compoundres *resp, __be32 nfserr,
4707 struct nfsd4_copy *copy)
4708{
4709 __be32 *p;
4710
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004711 nfserr = nfsd42_encode_write_res(resp, &copy->cp_res,
Chuck Levere8febea2020-11-04 10:49:37 -05004712 !!copy->cp_synchronous);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004713 if (nfserr)
4714 return nfserr;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004715
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004716 p = xdr_reserve_space(resp->xdr, 4 + 4);
J. Bruce Fieldsedcc8452018-03-07 15:37:35 -05004717 *p++ = xdr_one; /* cr_consecutive */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004718 *p++ = cpu_to_be32(copy->cp_synchronous);
4719 return 0;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004720}
4721
4722static __be32
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04004723nfsd4_encode_offload_status(struct nfsd4_compoundres *resp, __be32 nfserr,
4724 struct nfsd4_offload_status *os)
4725{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004726 struct xdr_stream *xdr = resp->xdr;
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04004727 __be32 *p;
4728
4729 p = xdr_reserve_space(xdr, 8 + 4);
4730 if (!p)
4731 return nfserr_resource;
4732 p = xdr_encode_hyper(p, os->count);
4733 *p++ = cpu_to_be32(0);
Anna Schumaker528b8492020-09-28 13:08:58 -04004734 return nfserr;
4735}
4736
4737static __be32
4738nfsd4_encode_read_plus_data(struct nfsd4_compoundres *resp,
4739 struct nfsd4_read *read,
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004740 unsigned long *maxcount, u32 *eof,
4741 loff_t *pos)
Anna Schumaker528b8492020-09-28 13:08:58 -04004742{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004743 struct xdr_stream *xdr = resp->xdr;
Anna Schumaker528b8492020-09-28 13:08:58 -04004744 struct file *file = read->rd_nf->nf_file;
4745 int starting_len = xdr->buf->len;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004746 loff_t hole_pos;
Anna Schumaker528b8492020-09-28 13:08:58 -04004747 __be32 nfserr;
4748 __be32 *p, tmp;
4749 __be64 tmp64;
4750
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004751 hole_pos = pos ? *pos : vfs_llseek(file, read->rd_offset, SEEK_HOLE);
Anna Schumaker2db27992020-09-28 13:08:59 -04004752 if (hole_pos > read->rd_offset)
Anna Schumaker278765e2020-09-28 13:09:00 -04004753 *maxcount = min_t(unsigned long, *maxcount, hole_pos - read->rd_offset);
4754 *maxcount = min_t(unsigned long, *maxcount, (xdr->buf->buflen - xdr->buf->len));
Anna Schumaker528b8492020-09-28 13:08:58 -04004755
4756 /* Content type, offset, byte count */
4757 p = xdr_reserve_space(xdr, 4 + 8 + 4);
4758 if (!p)
4759 return nfserr_resource;
4760
Anna Schumaker278765e2020-09-28 13:09:00 -04004761 read->rd_vlen = xdr_reserve_space_vec(xdr, resp->rqstp->rq_vec, *maxcount);
Anna Schumaker528b8492020-09-28 13:08:58 -04004762 if (read->rd_vlen < 0)
4763 return nfserr_resource;
4764
4765 nfserr = nfsd_readv(resp->rqstp, read->rd_fhp, file, read->rd_offset,
Anna Schumaker278765e2020-09-28 13:09:00 -04004766 resp->rqstp->rq_vec, read->rd_vlen, maxcount, eof);
Anna Schumaker528b8492020-09-28 13:08:58 -04004767 if (nfserr)
4768 return nfserr;
Trond Myklebust72d78712020-12-11 12:26:14 -05004769 xdr_truncate_encode(xdr, starting_len + 16 + xdr_align_size(*maxcount));
Anna Schumaker528b8492020-09-28 13:08:58 -04004770
4771 tmp = htonl(NFS4_CONTENT_DATA);
4772 write_bytes_to_xdr_buf(xdr->buf, starting_len, &tmp, 4);
4773 tmp64 = cpu_to_be64(read->rd_offset);
4774 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp64, 8);
Anna Schumaker278765e2020-09-28 13:09:00 -04004775 tmp = htonl(*maxcount);
Anna Schumaker528b8492020-09-28 13:08:58 -04004776 write_bytes_to_xdr_buf(xdr->buf, starting_len + 12, &tmp, 4);
Trond Myklebust72d78712020-12-11 12:26:14 -05004777
4778 tmp = xdr_zero;
4779 write_bytes_to_xdr_buf(xdr->buf, starting_len + 16 + *maxcount, &tmp,
4780 xdr_pad_size(*maxcount));
Anna Schumaker528b8492020-09-28 13:08:58 -04004781 return nfs_ok;
4782}
4783
4784static __be32
Anna Schumaker2db27992020-09-28 13:08:59 -04004785nfsd4_encode_read_plus_hole(struct nfsd4_compoundres *resp,
4786 struct nfsd4_read *read,
Anna Schumaker278765e2020-09-28 13:09:00 -04004787 unsigned long *maxcount, u32 *eof)
Anna Schumaker2db27992020-09-28 13:08:59 -04004788{
4789 struct file *file = read->rd_nf->nf_file;
Anna Schumaker278765e2020-09-28 13:09:00 -04004790 loff_t data_pos = vfs_llseek(file, read->rd_offset, SEEK_DATA);
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004791 loff_t f_size = i_size_read(file_inode(file));
Anna Schumaker278765e2020-09-28 13:09:00 -04004792 unsigned long count;
Anna Schumaker2db27992020-09-28 13:08:59 -04004793 __be32 *p;
4794
Anna Schumaker278765e2020-09-28 13:09:00 -04004795 if (data_pos == -ENXIO)
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004796 data_pos = f_size;
4797 else if (data_pos <= read->rd_offset || (data_pos < f_size && data_pos % PAGE_SIZE))
4798 return nfsd4_encode_read_plus_data(resp, read, maxcount, eof, &f_size);
Anna Schumaker278765e2020-09-28 13:09:00 -04004799 count = data_pos - read->rd_offset;
4800
Anna Schumaker2db27992020-09-28 13:08:59 -04004801 /* Content type, offset, byte count */
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004802 p = xdr_reserve_space(resp->xdr, 4 + 8 + 8);
Anna Schumaker2db27992020-09-28 13:08:59 -04004803 if (!p)
4804 return nfserr_resource;
4805
4806 *p++ = htonl(NFS4_CONTENT_HOLE);
4807 p = xdr_encode_hyper(p, read->rd_offset);
Anna Schumaker278765e2020-09-28 13:09:00 -04004808 p = xdr_encode_hyper(p, count);
Anna Schumaker2db27992020-09-28 13:08:59 -04004809
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004810 *eof = (read->rd_offset + count) >= f_size;
Anna Schumaker278765e2020-09-28 13:09:00 -04004811 *maxcount = min_t(unsigned long, count, *maxcount);
Anna Schumaker2db27992020-09-28 13:08:59 -04004812 return nfs_ok;
4813}
4814
4815static __be32
Anna Schumaker528b8492020-09-28 13:08:58 -04004816nfsd4_encode_read_plus(struct nfsd4_compoundres *resp, __be32 nfserr,
4817 struct nfsd4_read *read)
4818{
Anna Schumaker278765e2020-09-28 13:09:00 -04004819 unsigned long maxcount, count;
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004820 struct xdr_stream *xdr = resp->xdr;
Anna Schumaker528b8492020-09-28 13:08:58 -04004821 struct file *file;
4822 int starting_len = xdr->buf->len;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004823 int last_segment = xdr->buf->len;
Anna Schumaker528b8492020-09-28 13:08:58 -04004824 int segments = 0;
4825 __be32 *p, tmp;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004826 bool is_data;
Anna Schumaker2db27992020-09-28 13:08:59 -04004827 loff_t pos;
Anna Schumaker528b8492020-09-28 13:08:58 -04004828 u32 eof;
4829
4830 if (nfserr)
4831 return nfserr;
4832 file = read->rd_nf->nf_file;
4833
4834 /* eof flag, segment count */
4835 p = xdr_reserve_space(xdr, 4 + 4);
4836 if (!p)
4837 return nfserr_resource;
4838 xdr_commit_encode(xdr);
4839
4840 maxcount = svc_max_payload(resp->rqstp);
4841 maxcount = min_t(unsigned long, maxcount,
4842 (xdr->buf->buflen - xdr->buf->len));
4843 maxcount = min_t(unsigned long, maxcount, read->rd_length);
Anna Schumaker278765e2020-09-28 13:09:00 -04004844 count = maxcount;
Anna Schumaker528b8492020-09-28 13:08:58 -04004845
4846 eof = read->rd_offset >= i_size_read(file_inode(file));
Anna Schumaker2db27992020-09-28 13:08:59 -04004847 if (eof)
4848 goto out;
4849
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004850 pos = vfs_llseek(file, read->rd_offset, SEEK_HOLE);
4851 is_data = pos > read->rd_offset;
Anna Schumaker2db27992020-09-28 13:08:59 -04004852
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004853 while (count > 0 && !eof) {
Anna Schumaker278765e2020-09-28 13:09:00 -04004854 maxcount = count;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004855 if (is_data)
4856 nfserr = nfsd4_encode_read_plus_data(resp, read, &maxcount, &eof,
4857 segments == 0 ? &pos : NULL);
4858 else
4859 nfserr = nfsd4_encode_read_plus_hole(resp, read, &maxcount, &eof);
Anna Schumaker278765e2020-09-28 13:09:00 -04004860 if (nfserr)
4861 goto out;
4862 count -= maxcount;
4863 read->rd_offset += maxcount;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004864 is_data = !is_data;
4865 last_segment = xdr->buf->len;
Anna Schumaker528b8492020-09-28 13:08:58 -04004866 segments++;
4867 }
4868
Anna Schumaker2db27992020-09-28 13:08:59 -04004869out:
Anna Schumaker278765e2020-09-28 13:09:00 -04004870 if (nfserr && segments == 0)
Anna Schumaker528b8492020-09-28 13:08:58 -04004871 xdr_truncate_encode(xdr, starting_len);
4872 else {
Trond Myklebustb68f0cb2020-12-11 12:26:15 -05004873 if (nfserr) {
4874 xdr_truncate_encode(xdr, last_segment);
4875 nfserr = nfs_ok;
4876 eof = 0;
4877 }
Anna Schumaker528b8492020-09-28 13:08:58 -04004878 tmp = htonl(eof);
4879 write_bytes_to_xdr_buf(xdr->buf, starting_len, &tmp, 4);
4880 tmp = htonl(segments);
4881 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp, 4);
4882 }
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04004883
4884 return nfserr;
4885}
4886
4887static __be32
Olga Kornievskaia51911862019-08-08 11:14:59 -04004888nfsd4_encode_copy_notify(struct nfsd4_compoundres *resp, __be32 nfserr,
4889 struct nfsd4_copy_notify *cn)
4890{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004891 struct xdr_stream *xdr = resp->xdr;
Olga Kornievskaia51911862019-08-08 11:14:59 -04004892 __be32 *p;
4893
4894 if (nfserr)
4895 return nfserr;
4896
4897 /* 8 sec, 4 nsec */
4898 p = xdr_reserve_space(xdr, 12);
4899 if (!p)
4900 return nfserr_resource;
4901
4902 /* cnr_lease_time */
4903 p = xdr_encode_hyper(p, cn->cpn_sec);
4904 *p++ = cpu_to_be32(cn->cpn_nsec);
4905
4906 /* cnr_stateid */
4907 nfserr = nfsd4_encode_stateid(xdr, &cn->cpn_cnr_stateid);
4908 if (nfserr)
4909 return nfserr;
4910
4911 /* cnr_src.nl_nsvr */
4912 p = xdr_reserve_space(xdr, 4);
4913 if (!p)
4914 return nfserr_resource;
4915
4916 *p++ = cpu_to_be32(1);
4917
4918 return nfsd42_encode_nl4_server(resp, &cn->cpn_src);
4919}
4920
4921static __be32
Anna Schumaker24bab492014-09-26 13:58:27 -04004922nfsd4_encode_seek(struct nfsd4_compoundres *resp, __be32 nfserr,
4923 struct nfsd4_seek *seek)
4924{
4925 __be32 *p;
4926
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004927 p = xdr_reserve_space(resp->xdr, 4 + 8);
Anna Schumaker24bab492014-09-26 13:58:27 -04004928 *p++ = cpu_to_be32(seek->seek_eof);
4929 p = xdr_encode_hyper(p, seek->seek_pos);
4930
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004931 return 0;
Anna Schumaker24bab492014-09-26 13:58:27 -04004932}
4933
4934static __be32
Benny Halevy695e12f2008-07-04 14:38:33 +03004935nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
4936{
4937 return nfserr;
4938}
4939
Frank van der Linden23e50fe2020-06-23 22:39:26 +00004940/*
4941 * Encode kmalloc-ed buffer in to XDR stream.
4942 */
Chuck Leverb9a49232020-09-11 14:47:53 -04004943static __be32
Frank van der Linden23e50fe2020-06-23 22:39:26 +00004944nfsd4_vbuf_to_stream(struct xdr_stream *xdr, char *buf, u32 buflen)
4945{
4946 u32 cplen;
4947 __be32 *p;
4948
4949 cplen = min_t(unsigned long, buflen,
4950 ((void *)xdr->end - (void *)xdr->p));
4951 p = xdr_reserve_space(xdr, cplen);
4952 if (!p)
4953 return nfserr_resource;
4954
4955 memcpy(p, buf, cplen);
4956 buf += cplen;
4957 buflen -= cplen;
4958
4959 while (buflen) {
4960 cplen = min_t(u32, buflen, PAGE_SIZE);
4961 p = xdr_reserve_space(xdr, cplen);
4962 if (!p)
4963 return nfserr_resource;
4964
4965 memcpy(p, buf, cplen);
4966
4967 if (cplen < PAGE_SIZE) {
4968 /*
4969 * We're done, with a length that wasn't page
4970 * aligned, so possibly not word aligned. Pad
4971 * any trailing bytes with 0.
4972 */
4973 xdr_encode_opaque_fixed(p, NULL, cplen);
4974 break;
4975 }
4976
4977 buflen -= PAGE_SIZE;
4978 buf += PAGE_SIZE;
4979 }
4980
4981 return 0;
4982}
4983
4984static __be32
4985nfsd4_encode_getxattr(struct nfsd4_compoundres *resp, __be32 nfserr,
4986 struct nfsd4_getxattr *getxattr)
4987{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04004988 struct xdr_stream *xdr = resp->xdr;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00004989 __be32 *p, err;
4990
4991 p = xdr_reserve_space(xdr, 4);
4992 if (!p)
4993 return nfserr_resource;
4994
4995 *p = cpu_to_be32(getxattr->getxa_len);
4996
4997 if (getxattr->getxa_len == 0)
4998 return 0;
4999
5000 err = nfsd4_vbuf_to_stream(xdr, getxattr->getxa_buf,
5001 getxattr->getxa_len);
5002
5003 kvfree(getxattr->getxa_buf);
5004
5005 return err;
5006}
5007
5008static __be32
5009nfsd4_encode_setxattr(struct nfsd4_compoundres *resp, __be32 nfserr,
5010 struct nfsd4_setxattr *setxattr)
5011{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04005012 struct xdr_stream *xdr = resp->xdr;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005013 __be32 *p;
5014
5015 p = xdr_reserve_space(xdr, 20);
5016 if (!p)
5017 return nfserr_resource;
5018
5019 encode_cinfo(p, &setxattr->setxa_cinfo);
5020
5021 return 0;
5022}
5023
5024/*
5025 * See if there are cookie values that can be rejected outright.
5026 */
5027static __be32
5028nfsd4_listxattr_validate_cookie(struct nfsd4_listxattrs *listxattrs,
5029 u32 *offsetp)
5030{
5031 u64 cookie = listxattrs->lsxa_cookie;
5032
5033 /*
5034 * If the cookie is larger than the maximum number we can fit
5035 * in either the buffer we just got back from vfs_listxattr, or,
5036 * XDR-encoded, in the return buffer, it's invalid.
5037 */
5038 if (cookie > (listxattrs->lsxa_len) / (XATTR_USER_PREFIX_LEN + 2))
5039 return nfserr_badcookie;
5040
5041 if (cookie > (listxattrs->lsxa_maxcount /
5042 (XDR_QUADLEN(XATTR_USER_PREFIX_LEN + 2) + 4)))
5043 return nfserr_badcookie;
5044
5045 *offsetp = (u32)cookie;
5046 return 0;
5047}
5048
5049static __be32
5050nfsd4_encode_listxattrs(struct nfsd4_compoundres *resp, __be32 nfserr,
5051 struct nfsd4_listxattrs *listxattrs)
5052{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04005053 struct xdr_stream *xdr = resp->xdr;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005054 u32 cookie_offset, count_offset, eof;
5055 u32 left, xdrleft, slen, count;
5056 u32 xdrlen, offset;
5057 u64 cookie;
5058 char *sp;
Chuck Leverb9a49232020-09-11 14:47:53 -04005059 __be32 status, tmp;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005060 __be32 *p;
5061 u32 nuser;
5062
5063 eof = 1;
5064
5065 status = nfsd4_listxattr_validate_cookie(listxattrs, &offset);
5066 if (status)
5067 goto out;
5068
5069 /*
5070 * Reserve space for the cookie and the name array count. Record
5071 * the offsets to save them later.
5072 */
5073 cookie_offset = xdr->buf->len;
5074 count_offset = cookie_offset + 8;
5075 p = xdr_reserve_space(xdr, 12);
5076 if (!p) {
5077 status = nfserr_resource;
5078 goto out;
5079 }
5080
5081 count = 0;
5082 left = listxattrs->lsxa_len;
5083 sp = listxattrs->lsxa_buf;
5084 nuser = 0;
5085
5086 xdrleft = listxattrs->lsxa_maxcount;
5087
5088 while (left > 0 && xdrleft > 0) {
5089 slen = strlen(sp);
5090
5091 /*
Alex Dewar4cce11fa2020-08-17 18:51:26 +01005092 * Check if this is a "user." attribute, skip it if not.
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005093 */
5094 if (strncmp(sp, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
5095 goto contloop;
5096
5097 slen -= XATTR_USER_PREFIX_LEN;
5098 xdrlen = 4 + ((slen + 3) & ~3);
5099 if (xdrlen > xdrleft) {
5100 if (count == 0) {
5101 /*
5102 * Can't even fit the first attribute name.
5103 */
5104 status = nfserr_toosmall;
5105 goto out;
5106 }
5107 eof = 0;
5108 goto wreof;
5109 }
5110
5111 left -= XATTR_USER_PREFIX_LEN;
5112 sp += XATTR_USER_PREFIX_LEN;
5113 if (nuser++ < offset)
5114 goto contloop;
5115
5116
5117 p = xdr_reserve_space(xdr, xdrlen);
5118 if (!p) {
5119 status = nfserr_resource;
5120 goto out;
5121 }
5122
Alex Deware2a18402020-08-12 15:12:51 +01005123 xdr_encode_opaque(p, sp, slen);
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005124
5125 xdrleft -= xdrlen;
5126 count++;
5127contloop:
5128 sp += slen + 1;
5129 left -= slen + 1;
5130 }
5131
5132 /*
5133 * If there were user attributes to copy, but we didn't copy
5134 * any, the offset was too large (e.g. the cookie was invalid).
5135 */
5136 if (nuser > 0 && count == 0) {
5137 status = nfserr_badcookie;
5138 goto out;
5139 }
5140
5141wreof:
5142 p = xdr_reserve_space(xdr, 4);
5143 if (!p) {
5144 status = nfserr_resource;
5145 goto out;
5146 }
5147 *p = cpu_to_be32(eof);
5148
5149 cookie = offset + count;
5150
5151 write_bytes_to_xdr_buf(xdr->buf, cookie_offset, &cookie, 8);
Chuck Leverb9a49232020-09-11 14:47:53 -04005152 tmp = cpu_to_be32(count);
5153 write_bytes_to_xdr_buf(xdr->buf, count_offset, &tmp, 4);
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005154out:
5155 if (listxattrs->lsxa_len)
5156 kvfree(listxattrs->lsxa_buf);
5157 return status;
5158}
5159
5160static __be32
5161nfsd4_encode_removexattr(struct nfsd4_compoundres *resp, __be32 nfserr,
5162 struct nfsd4_removexattr *removexattr)
5163{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04005164 struct xdr_stream *xdr = resp->xdr;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005165 __be32 *p;
5166
5167 p = xdr_reserve_space(xdr, 20);
5168 if (!p)
5169 return nfserr_resource;
5170
5171 p = encode_cinfo(p, &removexattr->rmxa_cinfo);
5172 return 0;
5173}
5174
Benny Halevy695e12f2008-07-04 14:38:33 +03005175typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
5176
Andy Adamson2db134e2009-04-03 08:27:55 +03005177/*
5178 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
5179 * since we don't need to filter out obsolete ops as this is
5180 * done in the decoding phase.
5181 */
Chuck Leverc1df6092017-08-01 11:59:58 -04005182static const nfsd4_enc nfsd4_enc_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04005183 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
5184 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
5185 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
5186 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
5187 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
5188 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
5189 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
5190 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
5191 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
5192 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
5193 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
5194 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
5195 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
5196 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
5197 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
5198 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
Benny Halevy84f09f42009-03-04 23:05:35 +02005199 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04005200 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
5201 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
5202 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
5203 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
5204 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
5205 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
5206 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
5207 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
5208 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
5209 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
5210 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
5211 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
5212 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
5213 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
5214 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
5215 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
5216 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
5217 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
5218 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
5219 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03005220
5221 /* NFSv4.1 operations */
5222 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04005223 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
Andy Adamson2db134e2009-04-03 08:27:55 +03005224 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
5225 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
Kinglong Mee43212cc2013-12-09 19:04:23 +08005226 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_noop,
5227 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03005228 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02005229#ifdef CONFIG_NFSD_PNFS
5230 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_getdeviceinfo,
5231 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
5232 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_layoutcommit,
5233 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_layoutget,
5234 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_layoutreturn,
5235#else
Andy Adamson2db134e2009-04-03 08:27:55 +03005236 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
5237 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
5238 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
5239 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
5240 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02005241#endif
Mi Jinlong22b6dee2010-12-27 14:29:57 +08005242 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
Andy Adamson2db134e2009-04-03 08:27:55 +03005243 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
5244 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
Bryan Schumaker17456802011-07-13 10:50:48 -04005245 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
Andy Adamson2db134e2009-04-03 08:27:55 +03005246 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
5247 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
5248 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
Anna Schumaker87a15a82014-09-26 13:58:26 -04005249
5250 /* NFSv4.2 operations */
5251 [OP_ALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
Anna Schumaker29ae7f92016-09-07 15:57:30 -04005252 [OP_COPY] = (nfsd4_enc)nfsd4_encode_copy,
Olga Kornievskaia51911862019-08-08 11:14:59 -04005253 [OP_COPY_NOTIFY] = (nfsd4_enc)nfsd4_encode_copy_notify,
Anna Schumaker87a15a82014-09-26 13:58:26 -04005254 [OP_DEALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
5255 [OP_IO_ADVISE] = (nfsd4_enc)nfsd4_encode_noop,
5256 [OP_LAYOUTERROR] = (nfsd4_enc)nfsd4_encode_noop,
5257 [OP_LAYOUTSTATS] = (nfsd4_enc)nfsd4_encode_noop,
5258 [OP_OFFLOAD_CANCEL] = (nfsd4_enc)nfsd4_encode_noop,
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04005259 [OP_OFFLOAD_STATUS] = (nfsd4_enc)nfsd4_encode_offload_status,
Anna Schumaker528b8492020-09-28 13:08:58 -04005260 [OP_READ_PLUS] = (nfsd4_enc)nfsd4_encode_read_plus,
Anna Schumaker24bab492014-09-26 13:58:27 -04005261 [OP_SEEK] = (nfsd4_enc)nfsd4_encode_seek,
Anna Schumaker87a15a82014-09-26 13:58:26 -04005262 [OP_WRITE_SAME] = (nfsd4_enc)nfsd4_encode_noop,
Christoph Hellwigffa01602015-12-03 12:59:52 +01005263 [OP_CLONE] = (nfsd4_enc)nfsd4_encode_noop,
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005264
5265 /* RFC 8276 extended atributes operations */
5266 [OP_GETXATTR] = (nfsd4_enc)nfsd4_encode_getxattr,
5267 [OP_SETXATTR] = (nfsd4_enc)nfsd4_encode_setxattr,
5268 [OP_LISTXATTRS] = (nfsd4_enc)nfsd4_encode_listxattrs,
5269 [OP_REMOVEXATTR] = (nfsd4_enc)nfsd4_encode_removexattr,
Benny Halevy695e12f2008-07-04 14:38:33 +03005270};
5271
Andy Adamson496c2622009-04-03 08:28:48 +03005272/*
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04005273 * Calculate whether we still have space to encode repsize bytes.
5274 * There are two considerations:
5275 * - For NFS versions >=4.1, the size of the reply must stay within
5276 * session limits
5277 * - For all NFS versions, we must stay within limited preallocated
5278 * buffer space.
Andy Adamson496c2622009-04-03 08:28:48 +03005279 *
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04005280 * This is called before the operation is processed, so can only provide
5281 * an upper estimate. For some nonidempotent operations (such as
5282 * getattr), it's not necessarily a problem if that estimate is wrong,
5283 * as we can fail it after processing without significant side effects.
Andy Adamson496c2622009-04-03 08:28:48 +03005284 */
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04005285__be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 respsize)
Andy Adamson496c2622009-04-03 08:28:48 +03005286{
J. Bruce Fields67492c92014-03-08 16:42:05 -05005287 struct xdr_buf *buf = &resp->rqstp->rq_res;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04005288 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson496c2622009-04-03 08:28:48 +03005289
J. Bruce Fields47ee5292014-03-12 21:39:35 -04005290 if (buf->len + respsize <= buf->buflen)
5291 return nfs_ok;
5292 if (!nfsd4_has_session(&resp->cstate))
J. Bruce Fieldsea8d7722014-03-08 17:19:55 -05005293 return nfserr_resource;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04005294 if (slot->sl_flags & NFSD4_SLOT_CACHETHIS) {
5295 WARN_ON_ONCE(1);
5296 return nfserr_rep_too_big_to_cache;
J. Bruce Fieldsea8d7722014-03-08 17:19:55 -05005297 }
J. Bruce Fields47ee5292014-03-12 21:39:35 -04005298 return nfserr_rep_too_big;
Andy Adamson496c2622009-04-03 08:28:48 +03005299}
5300
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301void
5302nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
5303{
Chuck Leverbddfdbc2020-10-27 15:53:42 -04005304 struct xdr_stream *xdr = resp->xdr;
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005305 struct nfs4_stateowner *so = resp->cstate.replay_owner;
J. Bruce Fields5f4ab942014-03-07 11:01:37 -05005306 struct svc_rqst *rqstp = resp->rqstp;
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04005307 const struct nfsd4_operation *opdesc = op->opdesc;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005308 int post_err_offset;
J. Bruce Fields07d1f802014-03-06 20:39:29 -05005309 nfsd4_enc encoder;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07005310 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05005312 p = xdr_reserve_space(xdr, 8);
5313 if (!p) {
5314 WARN_ON_ONCE(1);
5315 return;
5316 }
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04005317 *p++ = cpu_to_be32(op->opnum);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005318 post_err_offset = xdr->buf->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319
Benny Halevy695e12f2008-07-04 14:38:33 +03005320 if (op->opnum == OP_ILLEGAL)
5321 goto status;
J. Bruce Fieldsb7571e42017-05-06 10:49:21 -04005322 if (op->status && opdesc &&
5323 !(opdesc->op_flags & OP_NONTRIVIAL_ERROR_ENCODE))
5324 goto status;
Chuck Lever3a237b42020-11-22 12:49:52 -05005325 BUG_ON(op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
Benny Halevy695e12f2008-07-04 14:38:33 +03005326 !nfsd4_enc_ops[op->opnum]);
J. Bruce Fields07d1f802014-03-06 20:39:29 -05005327 encoder = nfsd4_enc_ops[op->opnum];
5328 op->status = encoder(resp, op->status, &op->u);
Chuck Lever08281342020-11-21 11:36:42 -05005329 if (op->status)
5330 trace_nfsd_compound_encode_err(rqstp, op->opnum, op->status);
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04005331 if (opdesc && opdesc->op_release)
5332 opdesc->op_release(&op->u);
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04005333 xdr_commit_encode(xdr);
5334
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04005335 /* nfsd4_check_resp_size guarantees enough room for error status */
J. Bruce Fields5f4ab942014-03-07 11:01:37 -05005336 if (!op->status) {
5337 int space_needed = 0;
5338 if (!nfsd4_last_compound_op(rqstp))
5339 space_needed = COMPOUND_ERR_SLACK_SPACE;
5340 op->status = nfsd4_check_resp_size(resp, space_needed);
5341 }
J. Bruce Fieldsc8f13d92014-05-08 17:38:18 -04005342 if (op->status == nfserr_resource && nfsd4_has_session(&resp->cstate)) {
5343 struct nfsd4_slot *slot = resp->cstate.slot;
5344
5345 if (slot->sl_flags & NFSD4_SLOT_CACHETHIS)
5346 op->status = nfserr_rep_too_big_to_cache;
5347 else
5348 op->status = nfserr_rep_too_big;
5349 }
J. Bruce Fields07d1f802014-03-06 20:39:29 -05005350 if (op->status == nfserr_resource ||
5351 op->status == nfserr_rep_too_big ||
5352 op->status == nfserr_rep_too_big_to_cache) {
5353 /*
5354 * The operation may have already been encoded or
5355 * partially encoded. No op returns anything additional
5356 * in the case of one of these three errors, so we can
5357 * just truncate back to after the status. But it's a
5358 * bug if we had to do this on a non-idempotent op:
5359 */
5360 warn_on_nonidempotent_op(op);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005361 xdr_truncate_encode(xdr, post_err_offset);
J. Bruce Fields07d1f802014-03-06 20:39:29 -05005362 }
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005363 if (so) {
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005364 int len = xdr->buf->len - post_err_offset;
5365
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005366 so->so_replay.rp_status = op->status;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005367 so->so_replay.rp_buflen = len;
5368 read_bytes_from_xdr_buf(xdr->buf, post_err_offset,
5369 so->so_replay.rp_buf, len);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005370 }
Benny Halevy695e12f2008-07-04 14:38:33 +03005371status:
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005372 /* Note that op->status is already in network byte order: */
5373 write_bytes_to_xdr_buf(xdr->buf, post_err_offset - 4, &op->status, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374}
5375
5376/*
5377 * Encode the reply stored in the stateowner reply cache
5378 *
5379 * XDR note: do not encode rp->rp_buflen: the buffer contains the
5380 * previously sent already encoded operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 */
5382void
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05005383nfsd4_encode_replay(struct xdr_stream *xdr, struct nfsd4_op *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07005385 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 struct nfs4_replay *rp = op->replay;
5387
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05005388 p = xdr_reserve_space(xdr, 8 + rp->rp_buflen);
5389 if (!p) {
5390 WARN_ON_ONCE(1);
5391 return;
5392 }
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04005393 *p++ = cpu_to_be32(op->opnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 *p++ = rp->rp_status; /* already xdr'ed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04005396 p = xdr_encode_opaque_fixed(p, rp->rp_buf, rp->rp_buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397}
5398
Christoph Hellwig85374882017-05-08 18:48:24 +02005399void nfsd4_release_compoundargs(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400{
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04005401 struct nfsd4_compoundargs *args = rqstp->rq_argp;
5402
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 if (args->ops != args->iops) {
5404 kfree(args->ops);
5405 args->ops = args->iops;
5406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 while (args->to_free) {
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -04005408 struct svcxdr_tmpbuf *tb = args->to_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 args->to_free = tb->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 kfree(tb);
5411 }
5412}
5413
Chuck Leverc44b31c2021-10-12 11:57:28 -04005414bool
Chuck Lever16c66362021-10-12 11:57:22 -04005415nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416{
Christoph Hellwig026fec72017-05-08 19:01:48 +02005417 struct nfsd4_compoundargs *args = rqstp->rq_argp;
5418
Chuck Leverc1346a122020-11-03 11:54:23 -05005419 /* svcxdr_tmp_alloc */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 args->to_free = NULL;
Chuck Leverc1346a122020-11-03 11:54:23 -05005421
Chuck Lever16c66362021-10-12 11:57:22 -04005422 args->xdr = xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 args->ops = args->iops;
5424 args->rqstp = rqstp;
5425
Chuck Leverd9b74bd2020-11-04 11:07:06 -05005426 return nfsd4_decode_compound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427}
5428
Chuck Lever130e2052021-10-13 10:41:13 -04005429bool
Chuck Leverfda49442021-10-13 10:41:06 -04005430nfs4svc_encode_compoundres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02005432 struct nfsd4_compoundres *resp = rqstp->rq_resp;
Chuck Leverfda49442021-10-13 10:41:06 -04005433 struct xdr_buf *buf = xdr->buf;
5434 __be32 *p;
J. Bruce Fields6ac90392014-02-26 17:39:35 -05005435
5436 WARN_ON_ONCE(buf->len != buf->head[0].iov_len + buf->page_len +
5437 buf->tail[0].iov_len);
J. Bruce Fieldsdd97fdd2014-02-26 17:16:27 -05005438
Chuck Lever3b0ebb22021-10-13 10:40:59 -04005439 /*
5440 * Send buffer space for the following items is reserved
5441 * at the top of nfsd4_proc_compound().
5442 */
5443 p = resp->statusp;
Chuck Levercc028a12020-10-02 15:52:44 -04005444
Chuck Lever3b0ebb22021-10-13 10:40:59 -04005445 *p++ = resp->cstate.status;
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04005446
Chuck Leverfda49442021-10-13 10:41:06 -04005447 rqstp->rq_next_page = xdr->page_ptr + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 *p++ = htonl(resp->taglen);
5450 memcpy(p, resp->tag, resp->taglen);
5451 p += XDR_QUADLEN(resp->taglen);
5452 *p++ = htonl(resp->opcnt);
5453
Trond Myklebustb6076642014-06-30 11:48:35 -04005454 nfsd4_sequence_done(resp);
Chuck Lever130e2052021-10-13 10:41:13 -04005455 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456}