blob: eae416eef7cdce2223d08292ac9b7d8de69c60be [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#define DECODE_HEAD \
Al Viro2ebbc012006-10-19 23:28:58 -0700106 __be32 *p; \
Al Virob37ad282006-10-19 23:28:59 -0700107 __be32 status
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#define DECODE_TAIL \
109 status = 0; \
110out: \
111 return status; \
112xdr_error: \
Chuck Lever817cb9d2007-09-11 18:01:20 -0400113 dprintk("NFSD: xdr error (%s:%d)\n", \
114 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 status = nfserr_bad_xdr; \
116 goto out
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#define READMEM(x,nbytes) do { \
119 x = (char *)p; \
120 p += XDR_QUADLEN(nbytes); \
121} while (0)
122#define SAVEMEM(x,nbytes) do { \
123 if (!(x = (p==argp->tmp || p == argp->tmpp) ? \
124 savemem(argp, p, nbytes) : \
125 (char *)p)) { \
Chuck Lever817cb9d2007-09-11 18:01:20 -0400126 dprintk("NFSD: xdr error (%s:%d)\n", \
127 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 goto xdr_error; \
129 } \
130 p += XDR_QUADLEN(nbytes); \
131} while (0)
132#define COPYMEM(x,nbytes) do { \
133 memcpy((x), p, nbytes); \
134 p += XDR_QUADLEN(nbytes); \
135} while (0)
Chuck Leverc1346a122020-11-03 11:54:23 -0500136#define READ_BUF(nbytes) \
137 do { \
138 p = xdr_inline_decode(argp->xdr,\
139 nbytes); \
140 if (!p) \
141 goto xdr_error; \
142 } while (0)
J. Bruce Fields2b86e3a2019-08-28 21:13:45 -0400143
Andy Adamson60adfc52009-04-03 08:28:50 +0300144static int zero_clientid(clientid_t *clid)
145{
146 return (clid->cl_boot == 0) && (clid->cl_id == 0);
147}
148
Christoph Hellwig2d8498d2013-11-20 00:24:11 -0800149/**
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400150 * svcxdr_tmpalloc - allocate memory to be freed after compound processing
J. Bruce Fieldsce043ac2014-06-24 16:51:12 -0400151 * @argp: NFSv4 compound argument structure
Trond Myklebusted992752019-08-18 14:18:59 -0400152 * @len: length of buffer to allocate
Christoph Hellwig2d8498d2013-11-20 00:24:11 -0800153 *
Trond Myklebusted992752019-08-18 14:18:59 -0400154 * Allocates a buffer of size @len to be freed when processing the compound
155 * operation described in @argp finishes.
Christoph Hellwig2d8498d2013-11-20 00:24:11 -0800156 */
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400157static void *
158svcxdr_tmpalloc(struct nfsd4_compoundargs *argp, u32 len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400160 struct svcxdr_tmpbuf *tb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400162 tb = kmalloc(sizeof(*tb) + len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (!tb)
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400164 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 tb->next = argp->to_free;
166 argp->to_free = tb;
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400167 return tb->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
J. Bruce Fields29c353b2014-06-24 17:06:51 -0400170/*
171 * For xdr strings that need to be passed to other kernel api's
172 * as null-terminated strings.
173 *
174 * Note null-terminating in place usually isn't safe since the
175 * buffer might end on a page boundary.
176 */
177static char *
178svcxdr_dupstr(struct nfsd4_compoundargs *argp, void *buf, u32 len)
179{
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400180 char *p = svcxdr_tmpalloc(argp, len + 1);
J. Bruce Fields29c353b2014-06-24 17:06:51 -0400181
182 if (!p)
183 return NULL;
184 memcpy(p, buf, len);
185 p[len] = '\0';
J. Bruce Fields29c353b2014-06-24 17:06:51 -0400186 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Christoph Hellwig2d8498d2013-11-20 00:24:11 -0800189/**
190 * savemem - duplicate a chunk of memory for later processing
191 * @argp: NFSv4 compound argument structure to be freed with
192 * @p: pointer to be duplicated
193 * @nbytes: length to be duplicated
194 *
195 * Returns a pointer to a copy of @nbytes bytes of memory at @p
196 * that are preserved until processing of the NFSv4 compound
197 * operation described by @argp finishes.
198 */
Al Viro2ebbc012006-10-19 23:28:58 -0700199static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400201 void *ret;
202
203 ret = svcxdr_tmpalloc(argp, nbytes);
204 if (!ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return NULL;
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400206 memcpy(ret, p, nbytes);
207 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
Chuck Lever5dcbfab2020-11-04 11:41:55 -0500210
211/*
212 * NFSv4 basic data type decoders
213 */
214
Chuck Lever1a994402020-11-03 13:09:34 -0500215/*
216 * This helper handles variable-length opaques which belong to protocol
217 * elements that this implementation does not support.
218 */
219static __be32
220nfsd4_decode_ignored_string(struct nfsd4_compoundargs *argp, u32 maxlen)
221{
222 u32 len;
223
224 if (xdr_stream_decode_u32(argp->xdr, &len) < 0)
225 return nfserr_bad_xdr;
226 if (maxlen && len > maxlen)
227 return nfserr_bad_xdr;
228 if (!xdr_inline_decode(argp->xdr, len))
229 return nfserr_bad_xdr;
230
231 return nfs_ok;
232}
233
Chuck Lever5dcbfab2020-11-04 11:41:55 -0500234static __be32
235nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
236{
237 __be32 *p;
238 u32 len;
239
240 if (xdr_stream_decode_u32(argp->xdr, &len) < 0)
241 return nfserr_bad_xdr;
242 if (len == 0 || len > NFS4_OPAQUE_LIMIT)
243 return nfserr_bad_xdr;
244 p = xdr_inline_decode(argp->xdr, len);
245 if (!p)
246 return nfserr_bad_xdr;
247 o->data = svcxdr_tmpalloc(argp, len);
248 if (!o->data)
249 return nfserr_jukebox;
250 o->len = len;
251 memcpy(o->data, p, len);
252
253 return nfs_ok;
254}
255
Christoph Hellwig4c94e132015-01-22 12:09:50 +0100256static __be32
Chuck Lever000dfa12020-11-03 13:24:10 -0500257nfsd4_decode_component4(struct nfsd4_compoundargs *argp, char **namp, u32 *lenp)
258{
259 __be32 *p, status;
260
261 if (xdr_stream_decode_u32(argp->xdr, lenp) < 0)
262 return nfserr_bad_xdr;
263 p = xdr_inline_decode(argp->xdr, *lenp);
264 if (!p)
265 return nfserr_bad_xdr;
266 status = check_filename((char *)p, *lenp);
267 if (status)
268 return status;
269 *namp = svcxdr_tmpalloc(argp, *lenp);
270 if (!*namp)
271 return nfserr_jukebox;
272 memcpy(*namp, p, *lenp);
273
274 return nfs_ok;
275}
276
277static __be32
Chuck Lever1c3eff72020-11-19 14:01:08 -0500278nfsd4_decode_nfstime4(struct nfsd4_compoundargs *argp, struct timespec64 *tv)
279{
280 __be32 *p;
281
282 p = xdr_inline_decode(argp->xdr, XDR_UNIT * 3);
283 if (!p)
284 return nfserr_bad_xdr;
285 p = xdr_decode_hyper(p, &tv->tv_sec);
286 tv->tv_nsec = be32_to_cpup(p++);
287 if (tv->tv_nsec >= (u32)1000000000)
288 return nfserr_inval;
289 return nfs_ok;
290}
291
292static __be32
Chuck Lever796dd1c2020-11-16 17:34:01 -0500293nfsd4_decode_verifier4(struct nfsd4_compoundargs *argp, nfs4_verifier *verf)
294{
295 __be32 *p;
296
297 p = xdr_inline_decode(argp->xdr, NFS4_VERIFIER_SIZE);
298 if (!p)
299 return nfserr_bad_xdr;
300 memcpy(verf->data, p, sizeof(verf->data));
301 return nfs_ok;
302}
303
Chuck Leverd1c263a2020-11-03 12:56:05 -0500304/**
305 * nfsd4_decode_bitmap4 - Decode an NFSv4 bitmap4
306 * @argp: NFSv4 compound argument structure
307 * @bmval: pointer to an array of u32's to decode into
308 * @bmlen: size of the @bmval array
309 *
310 * The server needs to return nfs_ok rather than nfserr_bad_xdr when
311 * encountering bitmaps containing bits it does not recognize. This
312 * includes bits in bitmap words past WORDn, where WORDn is the last
313 * bitmap WORD the implementation currently supports. Thus we are
314 * careful here to simply ignore bits in bitmap words that this
315 * implementation has yet to support explicitly.
316 *
317 * Return values:
318 * %nfs_ok: @bmval populated successfully
319 * %nfserr_bad_xdr: the encoded bitmap was invalid
320 */
321static __be32
322nfsd4_decode_bitmap4(struct nfsd4_compoundargs *argp, u32 *bmval, u32 bmlen)
323{
324 u32 i, count;
325 __be32 *p;
326
327 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
328 return nfserr_bad_xdr;
329 /* request sanity */
330 if (count > 1000)
331 return nfserr_bad_xdr;
332 p = xdr_inline_decode(argp->xdr, count << 2);
333 if (!p)
334 return nfserr_bad_xdr;
335 i = 0;
336 while (i < count)
337 bmval[i++] = be32_to_cpup(p++);
338 while (i < bmlen)
339 bmval[i++] = 0;
340
341 return nfs_ok;
342}
343
Al Virob37ad282006-10-19 23:28:59 -0700344static __be32
Chuck Leverc941a962020-11-19 13:02:54 -0500345nfsd4_decode_nfsace4(struct nfsd4_compoundargs *argp, struct nfs4_ace *ace)
346{
347 __be32 *p, status;
348 u32 length;
349
350 if (xdr_stream_decode_u32(argp->xdr, &ace->type) < 0)
351 return nfserr_bad_xdr;
352 if (xdr_stream_decode_u32(argp->xdr, &ace->flag) < 0)
353 return nfserr_bad_xdr;
354 if (xdr_stream_decode_u32(argp->xdr, &ace->access_mask) < 0)
355 return nfserr_bad_xdr;
356
357 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
358 return nfserr_bad_xdr;
359 p = xdr_inline_decode(argp->xdr, length);
360 if (!p)
361 return nfserr_bad_xdr;
362 ace->whotype = nfs4_acl_get_whotype((char *)p, length);
363 if (ace->whotype != NFS4_ACL_WHO_NAMED)
364 status = nfs_ok;
365 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
366 status = nfsd_map_name_to_gid(argp->rqstp,
367 (char *)p, length, &ace->who_gid);
368 else
369 status = nfsd_map_name_to_uid(argp->rqstp,
370 (char *)p, length, &ace->who_uid);
371
372 return status;
373}
374
375/* A counted array of nfsace4's */
376static noinline __be32
377nfsd4_decode_acl(struct nfsd4_compoundargs *argp, struct nfs4_acl **acl)
378{
379 struct nfs4_ace *ace;
380 __be32 status;
381 u32 count;
382
383 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
384 return nfserr_bad_xdr;
385
386 if (count > xdr_stream_remaining(argp->xdr) / 20)
387 /*
388 * Even with 4-byte names there wouldn't be
389 * space for that many aces; something fishy is
390 * going on:
391 */
392 return nfserr_fbig;
393
394 *acl = svcxdr_tmpalloc(argp, nfs4_acl_bytes(count));
395 if (*acl == NULL)
396 return nfserr_jukebox;
397
398 (*acl)->naces = count;
399 for (ace = (*acl)->aces; ace < (*acl)->aces + count; ace++) {
400 status = nfsd4_decode_nfsace4(argp, ace);
401 if (status)
402 return status;
403 }
404
405 return nfs_ok;
406}
407
Chuck Leverdabe9182020-11-19 14:05:51 -0500408static noinline __be32
409nfsd4_decode_security_label(struct nfsd4_compoundargs *argp,
410 struct xdr_netobj *label)
411{
412 u32 lfs, pi, length;
413 __be32 *p;
414
415 if (xdr_stream_decode_u32(argp->xdr, &lfs) < 0)
416 return nfserr_bad_xdr;
417 if (xdr_stream_decode_u32(argp->xdr, &pi) < 0)
418 return nfserr_bad_xdr;
419
420 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
421 return nfserr_bad_xdr;
422 if (length > NFS4_MAXLABELLEN)
423 return nfserr_badlabel;
424 p = xdr_inline_decode(argp->xdr, length);
425 if (!p)
426 return nfserr_bad_xdr;
427 label->len = length;
428 label->data = svcxdr_dupstr(argp, p, length);
429 if (!label->data)
430 return nfserr_jukebox;
431
432 return nfs_ok;
433}
434
Chuck Leverc941a962020-11-19 13:02:54 -0500435static __be32
Chuck Leverd1c263a2020-11-03 12:56:05 -0500436nfsd4_decode_fattr4(struct nfsd4_compoundargs *argp, u32 *bmval, u32 bmlen,
437 struct iattr *iattr, struct nfs4_acl **acl,
438 struct xdr_netobj *label, int *umask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Chuck Lever081d53f2020-11-19 13:09:13 -0500440 unsigned int starting_pos;
441 u32 attrlist4_count;
Chuck Leverd1c263a2020-11-03 12:56:05 -0500442 __be32 *p, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 iattr->ia_valid = 0;
Chuck Leverd1c263a2020-11-03 12:56:05 -0500445 status = nfsd4_decode_bitmap4(argp, bmval, bmlen);
446 if (status)
447 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
J. Bruce Fieldse864c182016-06-10 16:56:05 -0400449 if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
450 || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
451 || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2) {
452 if (nfsd_attrs_supported(argp->minorversion, bmval))
453 return nfserr_inval;
454 return nfserr_attrnotsupp;
455 }
456
Chuck Lever081d53f2020-11-19 13:09:13 -0500457 if (xdr_stream_decode_u32(argp->xdr, &attrlist4_count) < 0)
458 return nfserr_bad_xdr;
459 starting_pos = xdr_stream_pos(argp->xdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 if (bmval[0] & FATTR4_WORD0_SIZE) {
Chuck Lever2ac1b9b2020-11-19 13:47:16 -0500462 u64 size;
463
464 if (xdr_stream_decode_u64(argp->xdr, &size) < 0)
465 return nfserr_bad_xdr;
466 iattr->ia_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 iattr->ia_valid |= ATTR_SIZE;
468 }
469 if (bmval[0] & FATTR4_WORD0_ACL) {
Chuck Leverc941a962020-11-19 13:02:54 -0500470 status = nfsd4_decode_acl(argp, acl);
471 if (status)
472 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 } else
474 *acl = NULL;
475 if (bmval[1] & FATTR4_WORD1_MODE) {
Chuck Lever1c8f0ad2020-11-19 13:54:26 -0500476 u32 mode;
477
478 if (xdr_stream_decode_u32(argp->xdr, &mode) < 0)
479 return nfserr_bad_xdr;
480 iattr->ia_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 iattr->ia_mode &= (S_IFMT | S_IALLUGO);
482 iattr->ia_valid |= ATTR_MODE;
483 }
484 if (bmval[1] & FATTR4_WORD1_OWNER) {
Chuck Lever9853a5a2020-11-19 13:56:42 -0500485 u32 length;
486
487 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
488 return nfserr_bad_xdr;
489 p = xdr_inline_decode(argp->xdr, length);
490 if (!p)
491 return nfserr_bad_xdr;
492 status = nfsd_map_name_to_uid(argp->rqstp, (char *)p, length,
493 &iattr->ia_uid);
494 if (status)
NeilBrown47c85292011-02-16 13:08:35 +1100495 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 iattr->ia_valid |= ATTR_UID;
497 }
498 if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
Chuck Lever393c31d2020-11-19 13:58:18 -0500499 u32 length;
500
501 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
502 return nfserr_bad_xdr;
503 p = xdr_inline_decode(argp->xdr, length);
504 if (!p)
505 return nfserr_bad_xdr;
506 status = nfsd_map_name_to_gid(argp->rqstp, (char *)p, length,
507 &iattr->ia_gid);
508 if (status)
NeilBrown47c85292011-02-16 13:08:35 +1100509 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 iattr->ia_valid |= ATTR_GID;
511 }
512 if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
Chuck Lever1c3eff72020-11-19 14:01:08 -0500513 u32 set_it;
514
515 if (xdr_stream_decode_u32(argp->xdr, &set_it) < 0)
516 return nfserr_bad_xdr;
517 switch (set_it) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 case NFS4_SET_TO_CLIENT_TIME:
Chuck Lever1c3eff72020-11-19 14:01:08 -0500519 status = nfsd4_decode_nfstime4(argp, &iattr->ia_atime);
Christoph Hellwig4c94e132015-01-22 12:09:50 +0100520 if (status)
521 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
523 break;
524 case NFS4_SET_TO_SERVER_TIME:
525 iattr->ia_valid |= ATTR_ATIME;
526 break;
527 default:
Chuck Lever1c3eff72020-11-19 14:01:08 -0500528 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
Chuck Lever1c3eff72020-11-19 14:01:08 -0500532 u32 set_it;
533
534 if (xdr_stream_decode_u32(argp->xdr, &set_it) < 0)
535 return nfserr_bad_xdr;
536 switch (set_it) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 case NFS4_SET_TO_CLIENT_TIME:
Chuck Lever1c3eff72020-11-19 14:01:08 -0500538 status = nfsd4_decode_nfstime4(argp, &iattr->ia_mtime);
Christoph Hellwig4c94e132015-01-22 12:09:50 +0100539 if (status)
540 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
542 break;
543 case NFS4_SET_TO_SERVER_TIME:
544 iattr->ia_valid |= ATTR_MTIME;
545 break;
546 default:
Chuck Lever1c3eff72020-11-19 14:01:08 -0500547 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549 }
David Quigley18032ca2013-05-02 13:19:10 -0400550 label->len = 0;
Arnd Bergmann2285ae72018-01-22 22:09:12 +0100551 if (IS_ENABLED(CONFIG_NFSD_V4_SECURITY_LABEL) &&
552 bmval[2] & FATTR4_WORD2_SECURITY_LABEL) {
Chuck Leverdabe9182020-11-19 14:05:51 -0500553 status = nfsd4_decode_security_label(argp, label);
554 if (status)
555 return status;
David Quigley18032ca2013-05-02 13:19:10 -0400556 }
Andreas Gruenbacher47057ab2016-01-12 20:24:14 +0100557 if (bmval[2] & FATTR4_WORD2_MODE_UMASK) {
Chuck Lever66f04762020-11-19 14:07:43 -0500558 u32 mode, mask;
559
Andreas Gruenbacher47057ab2016-01-12 20:24:14 +0100560 if (!umask)
Chuck Lever66f04762020-11-19 14:07:43 -0500561 return nfserr_bad_xdr;
562 if (xdr_stream_decode_u32(argp->xdr, &mode) < 0)
563 return nfserr_bad_xdr;
564 iattr->ia_mode = mode & (S_IFMT | S_IALLUGO);
565 if (xdr_stream_decode_u32(argp->xdr, &mask) < 0)
566 return nfserr_bad_xdr;
567 *umask = mask & S_IRWXUGO;
Andreas Gruenbacher47057ab2016-01-12 20:24:14 +0100568 iattr->ia_valid |= ATTR_MODE;
569 }
Chuck Lever081d53f2020-11-19 13:09:13 -0500570
571 /* request sanity: did attrlist4 contain the expected number of words? */
572 if (attrlist4_count != xdr_stream_pos(argp->xdr) - starting_pos)
573 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Chuck Leverd1c263a2020-11-03 12:56:05 -0500575 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
Al Virob37ad282006-10-19 23:28:59 -0700578static __be32
Benny Halevye31a1b62008-08-12 20:46:18 +0300579nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
580{
581 DECODE_HEAD;
582
583 READ_BUF(sizeof(stateid_t));
J. Bruce Fields06553992014-06-02 12:32:51 -0400584 sid->si_generation = be32_to_cpup(p++);
Benny Halevye31a1b62008-08-12 20:46:18 +0300585 COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
586
587 DECODE_TAIL;
588}
589
Chuck Leverd3d2f382020-11-03 13:18:23 -0500590static __be32
591nfsd4_decode_stateid4(struct nfsd4_compoundargs *argp, stateid_t *sid)
592{
593 __be32 *p;
594
595 p = xdr_inline_decode(argp->xdr, NFS4_STATEID_SIZE);
596 if (!p)
597 return nfserr_bad_xdr;
598 sid->si_generation = be32_to_cpup(p++);
599 memcpy(&sid->si_opaque, p, sizeof(sid->si_opaque));
600 return nfs_ok;
601}
602
Chuck Lever144e8262020-11-16 17:25:02 -0500603static __be32
604nfsd4_decode_clientid4(struct nfsd4_compoundargs *argp, clientid_t *clientid)
605{
606 __be32 *p;
607
608 p = xdr_inline_decode(argp->xdr, sizeof(__be64));
609 if (!p)
610 return nfserr_bad_xdr;
611 memcpy(clientid, p, sizeof(*clientid));
612 return nfs_ok;
613}
614
615static __be32
616nfsd4_decode_state_owner4(struct nfsd4_compoundargs *argp,
617 clientid_t *clientid, struct xdr_netobj *owner)
618{
619 __be32 status;
620
621 status = nfsd4_decode_clientid4(argp, clientid);
622 if (status)
623 return status;
624 return nfsd4_decode_opaque(argp, owner);
625}
626
Chuck Lever04495972020-11-03 15:03:50 -0500627#ifdef CONFIG_NFSD_PNFS
628static __be32
629nfsd4_decode_deviceid4(struct nfsd4_compoundargs *argp,
630 struct nfsd4_deviceid *devid)
631{
632 __be32 *p;
633
634 p = xdr_inline_decode(argp->xdr, NFS4_DEVICEID4_SIZE);
635 if (!p)
636 return nfserr_bad_xdr;
637 memcpy(devid, p, sizeof(*devid));
638 return nfs_ok;
639}
Chuck Lever51859802020-11-04 10:40:07 -0500640
641static __be32
642nfsd4_decode_layoutupdate4(struct nfsd4_compoundargs *argp,
643 struct nfsd4_layoutcommit *lcp)
644{
645 if (xdr_stream_decode_u32(argp->xdr, &lcp->lc_layout_type) < 0)
646 return nfserr_bad_xdr;
647 if (lcp->lc_layout_type < LAYOUT_NFSV4_1_FILES)
648 return nfserr_bad_xdr;
649 if (lcp->lc_layout_type >= LAYOUT_TYPE_MAX)
650 return nfserr_bad_xdr;
651
652 if (xdr_stream_decode_u32(argp->xdr, &lcp->lc_up_len) < 0)
653 return nfserr_bad_xdr;
654 if (lcp->lc_up_len > 0) {
655 lcp->lc_up_layout = xdr_inline_decode(argp->xdr, lcp->lc_up_len);
656 if (!lcp->lc_up_layout)
657 return nfserr_bad_xdr;
658 }
659
660 return nfs_ok;
661}
662
Chuck Lever645fcad2020-11-04 10:42:25 -0500663static __be32
664nfsd4_decode_layoutreturn4(struct nfsd4_compoundargs *argp,
665 struct nfsd4_layoutreturn *lrp)
666{
667 __be32 status;
668
669 if (xdr_stream_decode_u32(argp->xdr, &lrp->lr_return_type) < 0)
670 return nfserr_bad_xdr;
671 switch (lrp->lr_return_type) {
672 case RETURN_FILE:
673 if (xdr_stream_decode_u64(argp->xdr, &lrp->lr_seg.offset) < 0)
674 return nfserr_bad_xdr;
675 if (xdr_stream_decode_u64(argp->xdr, &lrp->lr_seg.length) < 0)
676 return nfserr_bad_xdr;
677 status = nfsd4_decode_stateid4(argp, &lrp->lr_sid);
678 if (status)
679 return status;
680 if (xdr_stream_decode_u32(argp->xdr, &lrp->lrf_body_len) < 0)
681 return nfserr_bad_xdr;
682 if (lrp->lrf_body_len > 0) {
683 lrp->lrf_body = xdr_inline_decode(argp->xdr, lrp->lrf_body_len);
684 if (!lrp->lrf_body)
685 return nfserr_bad_xdr;
686 }
687 break;
688 case RETURN_FSID:
689 case RETURN_ALL:
690 lrp->lr_seg.offset = 0;
691 lrp->lr_seg.length = NFS4_MAX_UINT64;
692 break;
693 default:
694 return nfserr_bad_xdr;
695 }
696
697 return nfs_ok;
698}
699
Chuck Lever04495972020-11-03 15:03:50 -0500700#endif /* CONFIG_NFSD_PNFS */
701
Chuck Lever571e0452020-11-03 13:16:23 -0500702static __be32
703nfsd4_decode_sessionid4(struct nfsd4_compoundargs *argp,
704 struct nfs4_sessionid *sessionid)
705{
706 __be32 *p;
707
708 p = xdr_inline_decode(argp->xdr, NFS4_MAX_SESSIONID_LEN);
709 if (!p)
710 return nfserr_bad_xdr;
711 memcpy(sessionid->data, p, sizeof(sessionid->data));
712 return nfs_ok;
713}
714
Chuck Lever1a994402020-11-03 13:09:34 -0500715/* Defined in Appendix A of RFC 5531 */
716static __be32
717nfsd4_decode_authsys_parms(struct nfsd4_compoundargs *argp,
718 struct nfsd4_cb_sec *cbs)
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400719{
Chuck Lever1a994402020-11-03 13:09:34 -0500720 u32 stamp, gidcount, uid, gid;
721 __be32 *p, status;
722
723 if (xdr_stream_decode_u32(argp->xdr, &stamp) < 0)
724 return nfserr_bad_xdr;
725 /* machine name */
726 status = nfsd4_decode_ignored_string(argp, 255);
727 if (status)
728 return status;
729 if (xdr_stream_decode_u32(argp->xdr, &uid) < 0)
730 return nfserr_bad_xdr;
731 if (xdr_stream_decode_u32(argp->xdr, &gid) < 0)
732 return nfserr_bad_xdr;
733 if (xdr_stream_decode_u32(argp->xdr, &gidcount) < 0)
734 return nfserr_bad_xdr;
735 if (gidcount > 16)
736 return nfserr_bad_xdr;
737 p = xdr_inline_decode(argp->xdr, gidcount << 2);
738 if (!p)
739 return nfserr_bad_xdr;
740 if (cbs->flavor == (u32)(-1)) {
741 struct user_namespace *userns = nfsd_user_namespace(argp->rqstp);
742
743 kuid_t kuid = make_kuid(userns, uid);
744 kgid_t kgid = make_kgid(userns, gid);
745 if (uid_valid(kuid) && gid_valid(kgid)) {
746 cbs->uid = kuid;
747 cbs->gid = kgid;
748 cbs->flavor = RPC_AUTH_UNIX;
749 } else {
750 dprintk("RPC_AUTH_UNIX with invalid uid or gid, ignoring!\n");
751 }
752 }
753
754 return nfs_ok;
755}
756
757static __be32
758nfsd4_decode_gss_cb_handles4(struct nfsd4_compoundargs *argp,
759 struct nfsd4_cb_sec *cbs)
760{
761 __be32 status;
762 u32 service;
763
764 dprintk("RPC_AUTH_GSS callback secflavor not supported!\n");
765
766 if (xdr_stream_decode_u32(argp->xdr, &service) < 0)
767 return nfserr_bad_xdr;
768 if (service < RPC_GSS_SVC_NONE || service > RPC_GSS_SVC_PRIVACY)
769 return nfserr_bad_xdr;
770 /* gcbp_handle_from_server */
771 status = nfsd4_decode_ignored_string(argp, 0);
772 if (status)
773 return status;
774 /* gcbp_handle_from_client */
775 status = nfsd4_decode_ignored_string(argp, 0);
776 if (status)
777 return status;
778
779 return nfs_ok;
780}
781
782/* a counted array of callback_sec_parms4 items */
783static __be32
784nfsd4_decode_cb_sec(struct nfsd4_compoundargs *argp, struct nfsd4_cb_sec *cbs)
785{
786 u32 i, secflavor, nr_secflavs;
787 __be32 status;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400788
789 /* callback_sec_params4 */
Chuck Lever1a994402020-11-03 13:09:34 -0500790 if (xdr_stream_decode_u32(argp->xdr, &nr_secflavs) < 0)
791 return nfserr_bad_xdr;
J. Bruce Fields57569a72013-05-17 16:25:32 -0400792 if (nr_secflavs)
793 cbs->flavor = (u32)(-1);
794 else
795 /* Is this legal? Be generous, take it to mean AUTH_NONE: */
796 cbs->flavor = 0;
Chuck Lever1a994402020-11-03 13:09:34 -0500797
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400798 for (i = 0; i < nr_secflavs; ++i) {
Chuck Lever1a994402020-11-03 13:09:34 -0500799 if (xdr_stream_decode_u32(argp->xdr, &secflavor) < 0)
800 return nfserr_bad_xdr;
801 switch (secflavor) {
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400802 case RPC_AUTH_NULL:
Chuck Lever1a994402020-11-03 13:09:34 -0500803 /* void */
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500804 if (cbs->flavor == (u32)(-1))
805 cbs->flavor = RPC_AUTH_NULL;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400806 break;
807 case RPC_AUTH_UNIX:
Chuck Lever1a994402020-11-03 13:09:34 -0500808 status = nfsd4_decode_authsys_parms(argp, cbs);
809 if (status)
810 return status;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400811 break;
812 case RPC_AUTH_GSS:
Chuck Lever1a994402020-11-03 13:09:34 -0500813 status = nfsd4_decode_gss_cb_handles4(argp, cbs);
814 if (status)
815 return status;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400816 break;
817 default:
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400818 return nfserr_inval;
819 }
820 }
Chuck Lever1a994402020-11-03 13:09:34 -0500821
822 return nfs_ok;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400823}
824
Chuck Lever1a994402020-11-03 13:09:34 -0500825
Chuck Leverd169a6a2020-11-03 13:12:27 -0500826/*
827 * NFSv4 operation argument decoders
828 */
829
830static __be32
831nfsd4_decode_access(struct nfsd4_compoundargs *argp,
832 struct nfsd4_access *access)
833{
834 if (xdr_stream_decode_u32(argp->xdr, &access->ac_req_access) < 0)
835 return nfserr_bad_xdr;
836 return nfs_ok;
837}
838
Al Virob37ad282006-10-19 23:28:59 -0700839static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
841{
Chuck Leverd3d2f382020-11-03 13:18:23 -0500842 if (xdr_stream_decode_u32(argp->xdr, &close->cl_seqid) < 0)
843 return nfserr_bad_xdr;
844 return nfsd4_decode_stateid4(argp, &close->cl_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
847
Al Virob37ad282006-10-19 23:28:59 -0700848static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
850{
Chuck Levercbd9abb2020-11-03 13:19:51 -0500851 if (xdr_stream_decode_u64(argp->xdr, &commit->co_offset) < 0)
852 return nfserr_bad_xdr;
853 if (xdr_stream_decode_u32(argp->xdr, &commit->co_count) < 0)
854 return nfserr_bad_xdr;
855 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
Al Virob37ad282006-10-19 23:28:59 -0700858static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
860{
Chuck Lever000dfa12020-11-03 13:24:10 -0500861 __be32 *p, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Chuck Lever000dfa12020-11-03 13:24:10 -0500863 if (xdr_stream_decode_u32(argp->xdr, &create->cr_type) < 0)
864 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 switch (create->cr_type) {
866 case NF4LNK:
Chuck Lever000dfa12020-11-03 13:24:10 -0500867 if (xdr_stream_decode_u32(argp->xdr, &create->cr_datalen) < 0)
868 return nfserr_bad_xdr;
869 p = xdr_inline_decode(argp->xdr, create->cr_datalen);
870 if (!p)
871 return nfserr_bad_xdr;
J. Bruce Fields29c353b2014-06-24 17:06:51 -0400872 create->cr_data = svcxdr_dupstr(argp, p, create->cr_datalen);
J. Bruce Fields7fb84302014-06-24 15:06:41 -0400873 if (!create->cr_data)
J. Bruce Fields76f471282014-06-19 16:44:48 -0400874 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 break;
876 case NF4BLK:
877 case NF4CHR:
Chuck Lever000dfa12020-11-03 13:24:10 -0500878 if (xdr_stream_decode_u32(argp->xdr, &create->cr_specdata1) < 0)
879 return nfserr_bad_xdr;
880 if (xdr_stream_decode_u32(argp->xdr, &create->cr_specdata2) < 0)
881 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 break;
883 case NF4SOCK:
884 case NF4FIFO:
885 case NF4DIR:
886 default:
887 break;
888 }
Chuck Lever000dfa12020-11-03 13:24:10 -0500889 status = nfsd4_decode_component4(argp, &create->cr_name,
890 &create->cr_namelen);
891 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return status;
Chuck Leverd1c263a2020-11-03 12:56:05 -0500893 status = nfsd4_decode_fattr4(argp, create->cr_bmval,
894 ARRAY_SIZE(create->cr_bmval),
895 &create->cr_iattr, &create->cr_acl,
896 &create->cr_label, &create->cr_umask);
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300897 if (status)
Chuck Lever000dfa12020-11-03 13:24:10 -0500898 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Chuck Lever000dfa12020-11-03 13:24:10 -0500900 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
Al Virob37ad282006-10-19 23:28:59 -0700903static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
905{
Chuck Lever95e64822020-11-21 14:11:58 -0500906 return nfsd4_decode_stateid4(argp, &dr->dr_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
Al Virob37ad282006-10-19 23:28:59 -0700909static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
911{
Chuck Leverf759eff2020-11-19 14:40:20 -0500912 return nfsd4_decode_bitmap4(argp, getattr->ga_bmval,
913 ARRAY_SIZE(getattr->ga_bmval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
Al Virob37ad282006-10-19 23:28:59 -0700916static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
918{
Chuck Lever5c505d12020-11-04 15:01:24 -0500919 return nfsd4_decode_component4(argp, &link->li_name, &link->li_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
Al Virob37ad282006-10-19 23:28:59 -0700922static __be32
Chuck Lever8918cc02020-11-16 17:16:52 -0500923nfsd4_decode_open_to_lock_owner4(struct nfsd4_compoundargs *argp,
924 struct nfsd4_lock *lock)
925{
926 __be32 status;
927
928 if (xdr_stream_decode_u32(argp->xdr, &lock->lk_new_open_seqid) < 0)
929 return nfserr_bad_xdr;
930 status = nfsd4_decode_stateid4(argp, &lock->lk_new_open_stateid);
931 if (status)
932 return status;
933 if (xdr_stream_decode_u32(argp->xdr, &lock->lk_new_lock_seqid) < 0)
934 return nfserr_bad_xdr;
935 return nfsd4_decode_state_owner4(argp, &lock->lk_new_clientid,
936 &lock->lk_new_owner);
937}
938
939static __be32
940nfsd4_decode_exist_lock_owner4(struct nfsd4_compoundargs *argp,
941 struct nfsd4_lock *lock)
942{
943 __be32 status;
944
945 status = nfsd4_decode_stateid4(argp, &lock->lk_old_lock_stateid);
946 if (status)
947 return status;
948 if (xdr_stream_decode_u32(argp->xdr, &lock->lk_old_lock_seqid) < 0)
949 return nfserr_bad_xdr;
950
951 return nfs_ok;
952}
953
954static __be32
955nfsd4_decode_locker4(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
956{
957 if (xdr_stream_decode_bool(argp->xdr, &lock->lk_is_new) < 0)
958 return nfserr_bad_xdr;
959 if (lock->lk_is_new)
960 return nfsd4_decode_open_to_lock_owner4(argp, lock);
961 return nfsd4_decode_exist_lock_owner4(argp, lock);
962}
963
964static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
966{
Chuck Lever7c59dee2020-11-03 13:29:27 -0500967 if (xdr_stream_decode_u32(argp->xdr, &lock->lk_type) < 0)
968 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
Chuck Lever7c59dee2020-11-03 13:29:27 -0500970 return nfserr_bad_xdr;
971 if (xdr_stream_decode_bool(argp->xdr, &lock->lk_reclaim) < 0)
972 return nfserr_bad_xdr;
973 if (xdr_stream_decode_u64(argp->xdr, &lock->lk_offset) < 0)
974 return nfserr_bad_xdr;
975 if (xdr_stream_decode_u64(argp->xdr, &lock->lk_length) < 0)
976 return nfserr_bad_xdr;
977 return nfsd4_decode_locker4(argp, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
Al Virob37ad282006-10-19 23:28:59 -0700980static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
982{
Chuck Lever0a146f02020-11-03 13:31:44 -0500983 if (xdr_stream_decode_u32(argp->xdr, &lockt->lt_type) < 0)
984 return nfserr_bad_xdr;
985 if ((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
986 return nfserr_bad_xdr;
987 if (xdr_stream_decode_u64(argp->xdr, &lockt->lt_offset) < 0)
988 return nfserr_bad_xdr;
989 if (xdr_stream_decode_u64(argp->xdr, &lockt->lt_length) < 0)
990 return nfserr_bad_xdr;
991 return nfsd4_decode_state_owner4(argp, &lockt->lt_clientid,
992 &lockt->lt_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
994
Al Virob37ad282006-10-19 23:28:59 -0700995static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
997{
Chuck Leverca9cf9f2020-11-03 13:33:28 -0500998 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Chuck Leverca9cf9f2020-11-03 13:33:28 -05001000 if (xdr_stream_decode_u32(argp->xdr, &locku->lu_type) < 0)
1001 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
Chuck Leverca9cf9f2020-11-03 13:33:28 -05001003 return nfserr_bad_xdr;
1004 if (xdr_stream_decode_u32(argp->xdr, &locku->lu_seqid) < 0)
1005 return nfserr_bad_xdr;
1006 status = nfsd4_decode_stateid4(argp, &locku->lu_stateid);
Benny Halevye31a1b62008-08-12 20:46:18 +03001007 if (status)
1008 return status;
Chuck Leverca9cf9f2020-11-03 13:33:28 -05001009 if (xdr_stream_decode_u64(argp->xdr, &locku->lu_offset) < 0)
1010 return nfserr_bad_xdr;
1011 if (xdr_stream_decode_u64(argp->xdr, &locku->lu_length) < 0)
1012 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Chuck Leverca9cf9f2020-11-03 13:33:28 -05001014 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
Al Virob37ad282006-10-19 23:28:59 -07001017static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
1019{
Chuck Lever3d5877e2020-11-04 15:02:40 -05001020 return nfsd4_decode_component4(argp, &lookup->lo_name, &lookup->lo_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
1022
Chuck Leverbf33bab2020-11-16 17:37:42 -05001023static __be32
1024nfsd4_decode_createhow4(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
1025{
1026 __be32 status;
1027
1028 if (xdr_stream_decode_u32(argp->xdr, &open->op_createmode) < 0)
1029 return nfserr_bad_xdr;
1030 switch (open->op_createmode) {
1031 case NFS4_CREATE_UNCHECKED:
1032 case NFS4_CREATE_GUARDED:
1033 status = nfsd4_decode_fattr4(argp, open->op_bmval,
1034 ARRAY_SIZE(open->op_bmval),
1035 &open->op_iattr, &open->op_acl,
1036 &open->op_label, &open->op_umask);
1037 if (status)
1038 return status;
1039 break;
1040 case NFS4_CREATE_EXCLUSIVE:
1041 status = nfsd4_decode_verifier4(argp, &open->op_verf);
1042 if (status)
1043 return status;
1044 break;
1045 case NFS4_CREATE_EXCLUSIVE4_1:
1046 if (argp->minorversion < 1)
1047 return nfserr_bad_xdr;
1048 status = nfsd4_decode_verifier4(argp, &open->op_verf);
1049 if (status)
1050 return status;
1051 status = nfsd4_decode_fattr4(argp, open->op_bmval,
1052 ARRAY_SIZE(open->op_bmval),
1053 &open->op_iattr, &open->op_acl,
1054 &open->op_label, &open->op_umask);
1055 if (status)
1056 return status;
1057 break;
1058 default:
1059 return nfserr_bad_xdr;
1060 }
1061
1062 return nfs_ok;
1063}
1064
Chuck Levere6ec04b2020-11-16 17:41:21 -05001065static __be32
1066nfsd4_decode_openflag4(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
1067{
1068 __be32 status;
1069
1070 if (xdr_stream_decode_u32(argp->xdr, &open->op_create) < 0)
1071 return nfserr_bad_xdr;
1072 switch (open->op_create) {
1073 case NFS4_OPEN_NOCREATE:
1074 break;
1075 case NFS4_OPEN_CREATE:
1076 status = nfsd4_decode_createhow4(argp, open);
1077 if (status)
1078 return status;
1079 break;
1080 default:
1081 return nfserr_bad_xdr;
1082 }
1083
1084 return nfs_ok;
1085}
1086
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02001087static __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 -04001088{
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001089 u32 w;
1090
Chuck Lever9aa62f52020-11-16 17:54:48 -05001091 if (xdr_stream_decode_u32(argp->xdr, &w) < 0)
1092 return nfserr_bad_xdr;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02001093 *share_access = w & NFS4_SHARE_ACCESS_MASK;
1094 *deleg_want = w & NFS4_SHARE_WANT_MASK;
1095 if (deleg_when)
1096 *deleg_when = w & NFS4_SHARE_WHEN_MASK;
1097
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001098 switch (w & NFS4_SHARE_ACCESS_MASK) {
1099 case NFS4_SHARE_ACCESS_READ:
1100 case NFS4_SHARE_ACCESS_WRITE:
1101 case NFS4_SHARE_ACCESS_BOTH:
1102 break;
1103 default:
1104 return nfserr_bad_xdr;
1105 }
Benny Halevyfc0d14f2011-10-27 20:43:01 +02001106 w &= ~NFS4_SHARE_ACCESS_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001107 if (!w)
1108 return nfs_ok;
1109 if (!argp->minorversion)
1110 return nfserr_bad_xdr;
1111 switch (w & NFS4_SHARE_WANT_MASK) {
1112 case NFS4_SHARE_WANT_NO_PREFERENCE:
1113 case NFS4_SHARE_WANT_READ_DELEG:
1114 case NFS4_SHARE_WANT_WRITE_DELEG:
1115 case NFS4_SHARE_WANT_ANY_DELEG:
1116 case NFS4_SHARE_WANT_NO_DELEG:
1117 case NFS4_SHARE_WANT_CANCEL:
1118 break;
1119 default:
1120 return nfserr_bad_xdr;
1121 }
Benny Halevy92bac8c2011-10-19 19:13:29 -07001122 w &= ~NFS4_SHARE_WANT_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001123 if (!w)
1124 return nfs_ok;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02001125
1126 if (!deleg_when) /* open_downgrade */
1127 return nfserr_inval;
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001128 switch (w) {
1129 case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
1130 case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
Benny Halevyc668fc62011-10-19 19:13:13 -07001131 case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
1132 NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001133 return nfs_ok;
1134 }
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001135 return nfserr_bad_xdr;
1136}
1137
1138static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
1139{
Chuck Leverb07bebd2020-11-16 17:56:17 -05001140 if (xdr_stream_decode_u32(argp->xdr, x) < 0)
1141 return nfserr_bad_xdr;
1142 /* Note: unlike access bits, deny bits may be zero. */
Dan Carpenter01cd4af2011-10-17 10:41:17 +03001143 if (*x & ~NFS4_SHARE_DENY_BOTH)
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001144 return nfserr_bad_xdr;
Chuck Leverb07bebd2020-11-16 17:56:17 -05001145
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001146 return nfs_ok;
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001147}
1148
Al Virob37ad282006-10-19 23:28:59 -07001149static __be32
Chuck Lever1708e502020-11-16 17:45:04 -05001150nfsd4_decode_open_claim4(struct nfsd4_compoundargs *argp,
1151 struct nfsd4_open *open)
1152{
1153 __be32 status;
1154
1155 if (xdr_stream_decode_u32(argp->xdr, &open->op_claim_type) < 0)
1156 return nfserr_bad_xdr;
1157 switch (open->op_claim_type) {
1158 case NFS4_OPEN_CLAIM_NULL:
1159 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1160 status = nfsd4_decode_component4(argp, &open->op_fname,
1161 &open->op_fnamelen);
1162 if (status)
1163 return status;
1164 break;
1165 case NFS4_OPEN_CLAIM_PREVIOUS:
1166 if (xdr_stream_decode_u32(argp->xdr, &open->op_delegate_type) < 0)
1167 return nfserr_bad_xdr;
1168 break;
1169 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1170 status = nfsd4_decode_stateid4(argp, &open->op_delegate_stateid);
1171 if (status)
1172 return status;
1173 status = nfsd4_decode_component4(argp, &open->op_fname,
1174 &open->op_fnamelen);
1175 if (status)
1176 return status;
1177 break;
1178 case NFS4_OPEN_CLAIM_FH:
1179 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1180 if (argp->minorversion < 1)
1181 return nfserr_bad_xdr;
1182 /* void */
1183 break;
1184 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1185 if (argp->minorversion < 1)
1186 return nfserr_bad_xdr;
1187 status = nfsd4_decode_stateid4(argp, &open->op_delegate_stateid);
1188 if (status)
1189 return status;
1190 break;
1191 default:
1192 return nfserr_bad_xdr;
1193 }
1194
1195 return nfs_ok;
1196}
1197
1198static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
1200{
Chuck Lever61e5e0b2020-11-01 12:04:06 -05001201 __be32 status;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02001202 u32 dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 memset(open->op_bmval, 0, sizeof(open->op_bmval));
1205 open->op_iattr.ia_valid = 0;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001206 open->op_openowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
J. Bruce Fields9d313b12013-02-28 12:51:49 -08001208 open->op_xdr_error = 0;
Chuck Lever61e5e0b2020-11-01 12:04:06 -05001209 if (xdr_stream_decode_u32(argp->xdr, &open->op_seqid) < 0)
1210 return nfserr_bad_xdr;
1211 /* deleg_want is ignored */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02001212 status = nfsd4_decode_share_access(argp, &open->op_share_access,
1213 &open->op_deleg_want, &dummy);
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001214 if (status)
Chuck Lever61e5e0b2020-11-01 12:04:06 -05001215 return status;
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001216 status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
1217 if (status)
Chuck Lever61e5e0b2020-11-01 12:04:06 -05001218 return status;
1219 status = nfsd4_decode_state_owner4(argp, &open->op_clientid,
1220 &open->op_owner);
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001221 if (status)
Chuck Lever61e5e0b2020-11-01 12:04:06 -05001222 return status;
Chuck Levere6ec04b2020-11-16 17:41:21 -05001223 status = nfsd4_decode_openflag4(argp, open);
1224 if (status)
1225 return status;
Chuck Lever61e5e0b2020-11-01 12:04:06 -05001226 return nfsd4_decode_open_claim4(argp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
Al Virob37ad282006-10-19 23:28:59 -07001229static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
1231{
Chuck Lever06bee692020-11-03 14:18:57 -05001232 __be32 status;
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001233
1234 if (argp->minorversion >= 1)
1235 return nfserr_notsupp;
1236
Chuck Lever06bee692020-11-03 14:18:57 -05001237 status = nfsd4_decode_stateid4(argp, &open_conf->oc_req_stateid);
Benny Halevye31a1b62008-08-12 20:46:18 +03001238 if (status)
1239 return status;
Chuck Lever06bee692020-11-03 14:18:57 -05001240 if (xdr_stream_decode_u32(argp->xdr, &open_conf->oc_seqid) < 0)
1241 return nfserr_bad_xdr;
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001242
Chuck Lever06bee692020-11-03 14:18:57 -05001243 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
Al Virob37ad282006-10-19 23:28:59 -07001246static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
1248{
Chuck Leverdca716512020-11-03 14:21:01 -05001249 __be32 status;
1250
1251 status = nfsd4_decode_stateid4(argp, &open_down->od_stateid);
Benny Halevye31a1b62008-08-12 20:46:18 +03001252 if (status)
1253 return status;
Chuck Leverdca716512020-11-03 14:21:01 -05001254 if (xdr_stream_decode_u32(argp->xdr, &open_down->od_seqid) < 0)
1255 return nfserr_bad_xdr;
1256 /* deleg_want is ignored */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02001257 status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
1258 &open_down->od_deleg_want, NULL);
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001259 if (status)
1260 return status;
Chuck Leverdca716512020-11-03 14:21:01 -05001261 return nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
1263
Al Virob37ad282006-10-19 23:28:59 -07001264static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
1266{
Chuck Levera73bed92020-11-03 14:23:02 -05001267 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Chuck Levera73bed92020-11-03 14:23:02 -05001269 if (xdr_stream_decode_u32(argp->xdr, &putfh->pf_fhlen) < 0)
1270 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (putfh->pf_fhlen > NFS4_FHSIZE)
Chuck Levera73bed92020-11-03 14:23:02 -05001272 return nfserr_bad_xdr;
1273 p = xdr_inline_decode(argp->xdr, putfh->pf_fhlen);
1274 if (!p)
1275 return nfserr_bad_xdr;
1276 putfh->pf_fhval = svcxdr_tmpalloc(argp, putfh->pf_fhlen);
1277 if (!putfh->pf_fhval)
1278 return nfserr_jukebox;
1279 memcpy(putfh->pf_fhval, p, putfh->pf_fhlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Chuck Levera73bed92020-11-03 14:23:02 -05001281 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
1283
Al Virob37ad282006-10-19 23:28:59 -07001284static __be32
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001285nfsd4_decode_putpubfh(struct nfsd4_compoundargs *argp, void *p)
1286{
1287 if (argp->minorversion == 0)
1288 return nfs_ok;
1289 return nfserr_notsupp;
1290}
1291
1292static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
1294{
Chuck Lever3909c3b2020-11-03 14:28:24 -05001295 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Chuck Lever3909c3b2020-11-03 14:28:24 -05001297 status = nfsd4_decode_stateid4(argp, &read->rd_stateid);
Benny Halevye31a1b62008-08-12 20:46:18 +03001298 if (status)
1299 return status;
Chuck Lever3909c3b2020-11-03 14:28:24 -05001300 if (xdr_stream_decode_u64(argp->xdr, &read->rd_offset) < 0)
1301 return nfserr_bad_xdr;
1302 if (xdr_stream_decode_u32(argp->xdr, &read->rd_length) < 0)
1303 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Chuck Lever3909c3b2020-11-03 14:28:24 -05001305 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
Al Virob37ad282006-10-19 23:28:59 -07001308static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
1310{
Chuck Lever0dfaf2a2020-11-03 14:30:59 -05001311 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Chuck Lever0dfaf2a2020-11-03 14:30:59 -05001313 if (xdr_stream_decode_u64(argp->xdr, &readdir->rd_cookie) < 0)
1314 return nfserr_bad_xdr;
1315 status = nfsd4_decode_verifier4(argp, &readdir->rd_verf);
1316 if (status)
1317 return status;
1318 if (xdr_stream_decode_u32(argp->xdr, &readdir->rd_dircount) < 0)
1319 return nfserr_bad_xdr;
1320 if (xdr_stream_decode_u32(argp->xdr, &readdir->rd_maxcount) < 0)
1321 return nfserr_bad_xdr;
1322 if (xdr_stream_decode_uint32_array(argp->xdr, readdir->rd_bmval,
1323 ARRAY_SIZE(readdir->rd_bmval)) < 0)
1324 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Chuck Lever0dfaf2a2020-11-03 14:30:59 -05001326 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
Al Virob37ad282006-10-19 23:28:59 -07001329static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
1331{
Chuck Leverb7f5fbf2020-11-04 15:04:36 -05001332 return nfsd4_decode_component4(argp, &remove->rm_name, &remove->rm_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
1334
Al Virob37ad282006-10-19 23:28:59 -07001335static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
1337{
Chuck Leverba881a02020-11-04 15:05:58 -05001338 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Chuck Leverba881a02020-11-04 15:05:58 -05001340 status = nfsd4_decode_component4(argp, &rename->rn_sname, &rename->rn_snamelen);
1341 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return status;
Chuck Leverba881a02020-11-04 15:05:58 -05001343 return nfsd4_decode_component4(argp, &rename->rn_tname, &rename->rn_tnamelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
Al Virob37ad282006-10-19 23:28:59 -07001346static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
1348{
Chuck Leverd12f9042020-11-04 15:08:50 -05001349 return nfsd4_decode_clientid4(argp, clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
1351
Al Virob37ad282006-10-19 23:28:59 -07001352static __be32
Andy Adamsondcb488a32007-07-17 04:04:51 -07001353nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
1354 struct nfsd4_secinfo *secinfo)
1355{
Chuck Leverd0abdae2020-11-04 15:09:42 -05001356 return nfsd4_decode_component4(argp, &secinfo->si_name, &secinfo->si_namelen);
Andy Adamsondcb488a32007-07-17 04:04:51 -07001357}
1358
1359static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
1361{
Benny Halevye31a1b62008-08-12 20:46:18 +03001362 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Chuck Lever44592fe2020-11-21 14:14:59 -05001364 status = nfsd4_decode_stateid4(argp, &setattr->sa_stateid);
Benny Halevye31a1b62008-08-12 20:46:18 +03001365 if (status)
1366 return status;
Chuck Leverd1c263a2020-11-03 12:56:05 -05001367 return nfsd4_decode_fattr4(argp, setattr->sa_bmval,
1368 ARRAY_SIZE(setattr->sa_bmval),
1369 &setattr->sa_iattr, &setattr->sa_acl,
1370 &setattr->sa_label, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
1372
Al Virob37ad282006-10-19 23:28:59 -07001373static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
1375{
Chuck Lever92fa6c02020-11-03 14:35:02 -05001376 __be32 *p, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001378 if (argp->minorversion >= 1)
1379 return nfserr_notsupp;
1380
Chuck Lever92fa6c02020-11-03 14:35:02 -05001381 status = nfsd4_decode_verifier4(argp, &setclientid->se_verf);
1382 if (status)
1383 return status;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001384 status = nfsd4_decode_opaque(argp, &setclientid->se_name);
1385 if (status)
Chuck Lever92fa6c02020-11-03 14:35:02 -05001386 return status;
1387 if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_prog) < 0)
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001388 return nfserr_bad_xdr;
Chuck Lever92fa6c02020-11-03 14:35:02 -05001389 if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_netid_len) < 0)
1390 return nfserr_bad_xdr;
1391 p = xdr_inline_decode(argp->xdr, setclientid->se_callback_netid_len);
1392 if (!p)
1393 return nfserr_bad_xdr;
1394 setclientid->se_callback_netid_val = svcxdr_tmpalloc(argp,
1395 setclientid->se_callback_netid_len);
1396 if (!setclientid->se_callback_netid_val)
1397 return nfserr_jukebox;
1398 memcpy(setclientid->se_callback_netid_val, p,
1399 setclientid->se_callback_netid_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Chuck Lever92fa6c02020-11-03 14:35:02 -05001401 if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_addr_len) < 0)
1402 return nfserr_bad_xdr;
1403 p = xdr_inline_decode(argp->xdr, setclientid->se_callback_addr_len);
1404 if (!p)
1405 return nfserr_bad_xdr;
1406 setclientid->se_callback_addr_val = svcxdr_tmpalloc(argp,
1407 setclientid->se_callback_addr_len);
1408 if (!setclientid->se_callback_addr_val)
1409 return nfserr_jukebox;
1410 memcpy(setclientid->se_callback_addr_val, p,
1411 setclientid->se_callback_addr_len);
1412 if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_ident) < 0)
1413 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Chuck Lever92fa6c02020-11-03 14:35:02 -05001415 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416}
1417
Al Virob37ad282006-10-19 23:28:59 -07001418static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
1420{
Chuck Leverd1ca5512020-11-04 15:12:33 -05001421 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001423 if (argp->minorversion >= 1)
1424 return nfserr_notsupp;
1425
Chuck Leverd1ca5512020-11-04 15:12:33 -05001426 status = nfsd4_decode_clientid4(argp, &scd_c->sc_clientid);
1427 if (status)
1428 return status;
1429 return nfsd4_decode_verifier4(argp, &scd_c->sc_confirm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
1432/* Also used for NVERIFY */
Al Virob37ad282006-10-19 23:28:59 -07001433static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1435{
Chuck Lever67cd4532020-11-03 14:40:32 -05001436 __be32 *p, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Chuck Lever67cd4532020-11-03 14:40:32 -05001438 status = nfsd4_decode_bitmap4(argp, verify->ve_bmval,
1439 ARRAY_SIZE(verify->ve_bmval));
1440 if (status)
1441 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 /* For convenience's sake, we compare raw xdr'd attributes in
J. Bruce Fieldse5f95702012-11-30 17:24:18 -05001444 * nfsd4_proc_verify */
1445
Chuck Lever67cd4532020-11-03 14:40:32 -05001446 if (xdr_stream_decode_u32(argp->xdr, &verify->ve_attrlen) < 0)
1447 return nfserr_bad_xdr;
1448 p = xdr_inline_decode(argp->xdr, verify->ve_attrlen);
1449 if (!p)
1450 return nfserr_bad_xdr;
1451 verify->ve_attrval = svcxdr_tmpalloc(argp, verify->ve_attrlen);
1452 if (!verify->ve_attrval)
1453 return nfserr_jukebox;
1454 memcpy(verify->ve_attrval, p, verify->ve_attrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Chuck Lever67cd4532020-11-03 14:40:32 -05001456 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
1458
Al Virob37ad282006-10-19 23:28:59 -07001459static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1461{
Chuck Lever244e2be2020-11-03 14:44:28 -05001462 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Chuck Lever244e2be2020-11-03 14:44:28 -05001464 status = nfsd4_decode_stateid4(argp, &write->wr_stateid);
Benny Halevye31a1b62008-08-12 20:46:18 +03001465 if (status)
1466 return status;
Chuck Lever244e2be2020-11-03 14:44:28 -05001467 if (xdr_stream_decode_u64(argp->xdr, &write->wr_offset) < 0)
1468 return nfserr_bad_xdr;
1469 if (xdr_stream_decode_u32(argp->xdr, &write->wr_stable_how) < 0)
1470 return nfserr_bad_xdr;
Kinglong Mee54bbb7d2016-12-31 20:59:53 +08001471 if (write->wr_stable_how > NFS_FILE_SYNC)
Chuck Lever244e2be2020-11-03 14:44:28 -05001472 return nfserr_bad_xdr;
1473 if (xdr_stream_decode_u32(argp->xdr, &write->wr_buflen) < 0)
1474 return nfserr_bad_xdr;
Chuck Leverc1346a122020-11-03 11:54:23 -05001475 if (!xdr_stream_subsegment(argp->xdr, &write->wr_payload, write->wr_buflen))
Chuck Lever244e2be2020-11-03 14:44:28 -05001476 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Chuck Lever244e2be2020-11-03 14:44:28 -05001478 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
Al Virob37ad282006-10-19 23:28:59 -07001481static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1483{
Chuck Levera4a80c12020-11-04 13:42:25 -05001484 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001486 if (argp->minorversion >= 1)
1487 return nfserr_notsupp;
1488
Chuck Levera4a80c12020-11-04 13:42:25 -05001489 status = nfsd4_decode_state_owner4(argp, &rlockowner->rl_clientid,
1490 &rlockowner->rl_owner);
1491 if (status)
1492 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Andy Adamson60adfc52009-04-03 08:28:50 +03001494 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1495 return nfserr_inval;
Chuck Levera4a80c12020-11-04 13:42:25 -05001496
1497 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
Chuck Lever0f81d962020-11-03 13:14:35 -05001500static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp, struct nfsd4_backchannel_ctl *bc)
1501{
1502 if (xdr_stream_decode_u32(argp->xdr, &bc->bc_cb_program) < 0)
1503 return nfserr_bad_xdr;
1504 return nfsd4_decode_cb_sec(argp, &bc->bc_cb_sec);
1505}
1506
Chuck Lever571e0452020-11-03 13:16:23 -05001507static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
1508{
1509 u32 use_conn_in_rdma_mode;
1510 __be32 status;
1511
1512 status = nfsd4_decode_sessionid4(argp, &bcts->sessionid);
1513 if (status)
1514 return status;
1515 if (xdr_stream_decode_u32(argp->xdr, &bcts->dir) < 0)
1516 return nfserr_bad_xdr;
1517 if (xdr_stream_decode_u32(argp->xdr, &use_conn_in_rdma_mode) < 0)
1518 return nfserr_bad_xdr;
1519
1520 return nfs_ok;
1521}
1522
Al Virob37ad282006-10-19 23:28:59 -07001523static __be32
Chuck Lever2548aa72020-11-03 11:13:00 -05001524nfsd4_decode_state_protect_ops(struct nfsd4_compoundargs *argp,
1525 struct nfsd4_exchange_id *exid)
1526{
1527 __be32 status;
1528
1529 status = nfsd4_decode_bitmap4(argp, exid->spo_must_enforce,
1530 ARRAY_SIZE(exid->spo_must_enforce));
1531 if (status)
1532 return nfserr_bad_xdr;
1533 status = nfsd4_decode_bitmap4(argp, exid->spo_must_allow,
1534 ARRAY_SIZE(exid->spo_must_allow));
1535 if (status)
1536 return nfserr_bad_xdr;
1537
1538 return nfs_ok;
1539}
1540
Chuck Lever547bfeb2020-11-03 11:17:50 -05001541/*
1542 * This implementation currently does not support SP4_SSV.
1543 * This decoder simply skips over these arguments.
1544 */
1545static noinline __be32
1546nfsd4_decode_ssv_sp_parms(struct nfsd4_compoundargs *argp,
1547 struct nfsd4_exchange_id *exid)
1548{
1549 u32 count, window, num_gss_handles;
1550 __be32 status;
1551
1552 /* ssp_ops */
1553 status = nfsd4_decode_state_protect_ops(argp, exid);
1554 if (status)
1555 return status;
1556
1557 /* ssp_hash_algs<> */
1558 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
1559 return nfserr_bad_xdr;
1560 while (count--) {
1561 status = nfsd4_decode_ignored_string(argp, 0);
1562 if (status)
1563 return status;
1564 }
1565
1566 /* ssp_encr_algs<> */
1567 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
1568 return nfserr_bad_xdr;
1569 while (count--) {
1570 status = nfsd4_decode_ignored_string(argp, 0);
1571 if (status)
1572 return status;
1573 }
1574
1575 if (xdr_stream_decode_u32(argp->xdr, &window) < 0)
1576 return nfserr_bad_xdr;
1577 if (xdr_stream_decode_u32(argp->xdr, &num_gss_handles) < 0)
1578 return nfserr_bad_xdr;
1579
1580 return nfs_ok;
1581}
1582
Chuck Lever2548aa72020-11-03 11:13:00 -05001583static __be32
Chuck Lever523ec6e2020-11-02 15:19:12 -05001584nfsd4_decode_state_protect4_a(struct nfsd4_compoundargs *argp,
1585 struct nfsd4_exchange_id *exid)
Andy Adamson2db134e2009-04-03 08:27:55 +03001586{
Chuck Lever523ec6e2020-11-02 15:19:12 -05001587 __be32 status;
Andy Adamson0733d212009-04-03 08:28:01 +03001588
Chuck Lever523ec6e2020-11-02 15:19:12 -05001589 if (xdr_stream_decode_u32(argp->xdr, &exid->spa_how) < 0)
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001590 return nfserr_bad_xdr;
Andy Adamson0733d212009-04-03 08:28:01 +03001591 switch (exid->spa_how) {
1592 case SP4_NONE:
1593 break;
1594 case SP4_MACH_CRED:
Chuck Lever2548aa72020-11-03 11:13:00 -05001595 status = nfsd4_decode_state_protect_ops(argp, exid);
Andrew Elbleed941642016-06-15 12:52:09 -04001596 if (status)
Chuck Lever2548aa72020-11-03 11:13:00 -05001597 return status;
Andy Adamson0733d212009-04-03 08:28:01 +03001598 break;
1599 case SP4_SSV:
Chuck Lever547bfeb2020-11-03 11:17:50 -05001600 status = nfsd4_decode_ssv_sp_parms(argp, exid);
Chuck Lever2548aa72020-11-03 11:13:00 -05001601 if (status)
1602 return status;
Andy Adamson0733d212009-04-03 08:28:01 +03001603 break;
1604 default:
Chuck Lever523ec6e2020-11-02 15:19:12 -05001605 return nfserr_bad_xdr;
Andy Adamson0733d212009-04-03 08:28:01 +03001606 }
1607
Chuck Lever523ec6e2020-11-02 15:19:12 -05001608 return nfs_ok;
1609}
1610
1611static __be32
Chuck Lever10ff8422020-11-16 15:21:55 -05001612nfsd4_decode_nfs_impl_id4(struct nfsd4_compoundargs *argp,
1613 struct nfsd4_exchange_id *exid)
1614{
1615 __be32 status;
1616 u32 count;
1617
1618 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
1619 return nfserr_bad_xdr;
1620 switch (count) {
1621 case 0:
1622 break;
1623 case 1:
1624 /* Note that RFC 8881 places no length limit on
1625 * nii_domain, but this implementation permits no
1626 * more than NFS4_OPAQUE_LIMIT bytes */
1627 status = nfsd4_decode_opaque(argp, &exid->nii_domain);
1628 if (status)
1629 return status;
1630 /* Note that RFC 8881 places no length limit on
1631 * nii_name, but this implementation permits no
1632 * more than NFS4_OPAQUE_LIMIT bytes */
1633 status = nfsd4_decode_opaque(argp, &exid->nii_name);
1634 if (status)
1635 return status;
1636 status = nfsd4_decode_nfstime4(argp, &exid->nii_time);
1637 if (status)
1638 return status;
1639 break;
1640 default:
1641 return nfserr_bad_xdr;
1642 }
1643
1644 return nfs_ok;
1645}
1646
1647static __be32
Chuck Lever523ec6e2020-11-02 15:19:12 -05001648nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
1649 struct nfsd4_exchange_id *exid)
1650{
Chuck Lever10ff8422020-11-16 15:21:55 -05001651 __be32 status;
Chuck Lever523ec6e2020-11-02 15:19:12 -05001652
1653 status = nfsd4_decode_verifier4(argp, &exid->verifier);
1654 if (status)
1655 return status;
1656 status = nfsd4_decode_opaque(argp, &exid->clname);
1657 if (status)
1658 return status;
1659 if (xdr_stream_decode_u32(argp->xdr, &exid->flags) < 0)
1660 return nfserr_bad_xdr;
1661 status = nfsd4_decode_state_protect4_a(argp, exid);
1662 if (status)
1663 return status;
Chuck Lever10ff8422020-11-16 15:21:55 -05001664 return nfsd4_decode_nfs_impl_id4(argp, exid);
Andy Adamson2db134e2009-04-03 08:27:55 +03001665}
1666
1667static __be32
Chuck Lever3a3f1fb2020-11-16 15:35:05 -05001668nfsd4_decode_channel_attrs4(struct nfsd4_compoundargs *argp,
1669 struct nfsd4_channel_attrs *ca)
1670{
1671 __be32 *p;
1672
1673 p = xdr_inline_decode(argp->xdr, XDR_UNIT * 7);
1674 if (!p)
1675 return nfserr_bad_xdr;
1676
1677 /* headerpadsz is ignored */
1678 p++;
1679 ca->maxreq_sz = be32_to_cpup(p++);
1680 ca->maxresp_sz = be32_to_cpup(p++);
1681 ca->maxresp_cached = be32_to_cpup(p++);
1682 ca->maxops = be32_to_cpup(p++);
1683 ca->maxreqs = be32_to_cpup(p++);
1684 ca->nr_rdma_attrs = be32_to_cpup(p);
1685 switch (ca->nr_rdma_attrs) {
1686 case 0:
1687 break;
1688 case 1:
1689 if (xdr_stream_decode_u32(argp->xdr, &ca->rdma_attrs) < 0)
1690 return nfserr_bad_xdr;
1691 break;
1692 default:
1693 return nfserr_bad_xdr;
1694 }
1695
1696 return nfs_ok;
1697}
1698
1699static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001700nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1701 struct nfsd4_create_session *sess)
1702{
Chuck Lever81243e32020-11-03 14:52:44 -05001703 __be32 status;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001704
Chuck Lever81243e32020-11-03 14:52:44 -05001705 status = nfsd4_decode_clientid4(argp, &sess->clientid);
1706 if (status)
1707 return status;
1708 if (xdr_stream_decode_u32(argp->xdr, &sess->seqid) < 0)
1709 return nfserr_bad_xdr;
1710 if (xdr_stream_decode_u32(argp->xdr, &sess->flags) < 0)
1711 return nfserr_bad_xdr;
Chuck Lever3a3f1fb2020-11-16 15:35:05 -05001712 status = nfsd4_decode_channel_attrs4(argp, &sess->fore_channel);
1713 if (status)
1714 return status;
1715 status = nfsd4_decode_channel_attrs4(argp, &sess->back_channel);
1716 if (status)
1717 return status;
Chuck Lever81243e32020-11-03 14:52:44 -05001718 if (xdr_stream_decode_u32(argp->xdr, &sess->callback_prog) < 0)
1719 return nfserr_bad_xdr;
1720 status = nfsd4_decode_cb_sec(argp, &sess->cb_sec);
1721 if (status)
1722 return status;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001723
Chuck Lever81243e32020-11-03 14:52:44 -05001724 return nfs_ok;
Andy Adamson2db134e2009-04-03 08:27:55 +03001725}
1726
1727static __be32
1728nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1729 struct nfsd4_destroy_session *destroy_session)
1730{
Chuck Lever94e254a2020-11-04 13:50:55 -05001731 return nfsd4_decode_sessionid4(argp, &destroy_session->sessionid);
Andy Adamson2db134e2009-04-03 08:27:55 +03001732}
1733
1734static __be32
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001735nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1736 struct nfsd4_free_stateid *free_stateid)
1737{
Chuck Leveraec387d2020-11-01 13:38:27 -05001738 return nfsd4_decode_stateid4(argp, &free_stateid->fr_stateid);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001739}
1740
1741static __be32
Bryan Schumaker17456802011-07-13 10:50:48 -04001742nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1743{
Bryan Schumaker17456802011-07-13 10:50:48 -04001744 int i;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001745 __be32 *p, status;
1746 struct nfsd4_test_stateid_id *stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04001747
1748 READ_BUF(4);
1749 test_stateid->ts_num_ids = ntohl(*p++);
1750
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001751 INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
Bryan Schumaker17456802011-07-13 10:50:48 -04001752
1753 for (i = 0; i < test_stateid->ts_num_ids; i++) {
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -04001754 stateid = svcxdr_tmpalloc(argp, sizeof(*stateid));
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001755 if (!stateid) {
Al Viroafcf6792012-04-13 00:15:37 -04001756 status = nfserrno(-ENOMEM);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001757 goto out;
1758 }
1759
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001760 INIT_LIST_HEAD(&stateid->ts_id_list);
1761 list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
1762
1763 status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
Bryan Schumaker17456802011-07-13 10:50:48 -04001764 if (status)
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001765 goto out;
Bryan Schumaker17456802011-07-13 10:50:48 -04001766 }
1767
1768 status = 0;
1769out:
1770 return status;
1771xdr_error:
1772 dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
1773 status = nfserr_bad_xdr;
1774 goto out;
1775}
1776
Mi Jinlong345c2842011-10-20 17:51:39 +08001777static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
1778{
1779 DECODE_HEAD;
1780
1781 READ_BUF(8);
1782 COPYMEM(&dc->clientid, 8);
1783
1784 DECODE_TAIL;
1785}
1786
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001787static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
1788{
1789 DECODE_HEAD;
1790
1791 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001792 rc->rca_one_fs = be32_to_cpup(p++);
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001793
1794 DECODE_TAIL;
1795}
1796
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001797#ifdef CONFIG_NFSD_PNFS
1798static __be32
1799nfsd4_decode_getdeviceinfo(struct nfsd4_compoundargs *argp,
1800 struct nfsd4_getdeviceinfo *gdev)
1801{
Chuck Lever04495972020-11-03 15:03:50 -05001802 __be32 status;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001803
Chuck Lever04495972020-11-03 15:03:50 -05001804 status = nfsd4_decode_deviceid4(argp, &gdev->gd_devid);
1805 if (status)
1806 return status;
1807 if (xdr_stream_decode_u32(argp->xdr, &gdev->gd_layout_type) < 0)
1808 return nfserr_bad_xdr;
1809 if (xdr_stream_decode_u32(argp->xdr, &gdev->gd_maxcount) < 0)
1810 return nfserr_bad_xdr;
1811 if (xdr_stream_decode_uint32_array(argp->xdr,
1812 &gdev->gd_notify_types, 1) < 0)
1813 return nfserr_bad_xdr;
1814
1815 return nfs_ok;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001816}
1817
1818static __be32
Chuck Lever51859802020-11-04 10:40:07 -05001819nfsd4_decode_layoutcommit(struct nfsd4_compoundargs *argp,
1820 struct nfsd4_layoutcommit *lcp)
1821{
1822 __be32 *p, status;
1823
1824 if (xdr_stream_decode_u64(argp->xdr, &lcp->lc_seg.offset) < 0)
1825 return nfserr_bad_xdr;
1826 if (xdr_stream_decode_u64(argp->xdr, &lcp->lc_seg.length) < 0)
1827 return nfserr_bad_xdr;
1828 if (xdr_stream_decode_bool(argp->xdr, &lcp->lc_reclaim) < 0)
1829 return nfserr_bad_xdr;
1830 status = nfsd4_decode_stateid4(argp, &lcp->lc_sid);
1831 if (status)
1832 return status;
1833 if (xdr_stream_decode_u32(argp->xdr, &lcp->lc_newoffset) < 0)
1834 return nfserr_bad_xdr;
1835 if (lcp->lc_newoffset) {
1836 if (xdr_stream_decode_u64(argp->xdr, &lcp->lc_last_wr) < 0)
1837 return nfserr_bad_xdr;
1838 } else
1839 lcp->lc_last_wr = 0;
1840 p = xdr_inline_decode(argp->xdr, XDR_UNIT);
1841 if (!p)
1842 return nfserr_bad_xdr;
1843 if (xdr_item_is_present(p)) {
1844 status = nfsd4_decode_nfstime4(argp, &lcp->lc_mtime);
1845 if (status)
1846 return status;
1847 } else {
1848 lcp->lc_mtime.tv_nsec = UTIME_NOW;
1849 }
1850 return nfsd4_decode_layoutupdate4(argp, lcp);
1851}
1852
1853static __be32
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001854nfsd4_decode_layoutget(struct nfsd4_compoundargs *argp,
1855 struct nfsd4_layoutget *lgp)
1856{
Chuck Leverc8e88e32020-11-03 15:06:04 -05001857 __be32 status;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001858
Chuck Leverc8e88e32020-11-03 15:06:04 -05001859 if (xdr_stream_decode_u32(argp->xdr, &lgp->lg_signal) < 0)
1860 return nfserr_bad_xdr;
1861 if (xdr_stream_decode_u32(argp->xdr, &lgp->lg_layout_type) < 0)
1862 return nfserr_bad_xdr;
1863 if (xdr_stream_decode_u32(argp->xdr, &lgp->lg_seg.iomode) < 0)
1864 return nfserr_bad_xdr;
1865 if (xdr_stream_decode_u64(argp->xdr, &lgp->lg_seg.offset) < 0)
1866 return nfserr_bad_xdr;
1867 if (xdr_stream_decode_u64(argp->xdr, &lgp->lg_seg.length) < 0)
1868 return nfserr_bad_xdr;
1869 if (xdr_stream_decode_u64(argp->xdr, &lgp->lg_minlength) < 0)
1870 return nfserr_bad_xdr;
1871 status = nfsd4_decode_stateid4(argp, &lgp->lg_sid);
Kinglong Meedb59c0e2015-03-19 19:04:41 +08001872 if (status)
1873 return status;
Chuck Leverc8e88e32020-11-03 15:06:04 -05001874 if (xdr_stream_decode_u32(argp->xdr, &lgp->lg_maxcount) < 0)
1875 return nfserr_bad_xdr;
Kinglong Meedb59c0e2015-03-19 19:04:41 +08001876
Chuck Leverc8e88e32020-11-03 15:06:04 -05001877 return nfs_ok;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001878}
1879
1880static __be32
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001881nfsd4_decode_layoutreturn(struct nfsd4_compoundargs *argp,
1882 struct nfsd4_layoutreturn *lrp)
1883{
Chuck Lever645fcad2020-11-04 10:42:25 -05001884 if (xdr_stream_decode_bool(argp->xdr, &lrp->lr_reclaim) < 0)
1885 return nfserr_bad_xdr;
1886 if (xdr_stream_decode_u32(argp->xdr, &lrp->lr_layout_type) < 0)
1887 return nfserr_bad_xdr;
1888 if (xdr_stream_decode_u32(argp->xdr, &lrp->lr_seg.iomode) < 0)
1889 return nfserr_bad_xdr;
1890 return nfsd4_decode_layoutreturn4(argp, lrp);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001891}
1892#endif /* CONFIG_NFSD_PNFS */
1893
Chuck Lever53d70872020-11-03 14:33:12 -05001894static __be32 nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
1895 struct nfsd4_secinfo_no_name *sin)
1896{
1897 if (xdr_stream_decode_u32(argp->xdr, &sin->sin_style) < 0)
1898 return nfserr_bad_xdr;
1899 return nfs_ok;
1900}
1901
Andy Adamson2db134e2009-04-03 08:27:55 +03001902static __be32
Chuck Levercf907b12020-11-03 14:55:19 -05001903nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1904 struct nfsd4_sequence *seq)
1905{
1906 __be32 *p, status;
1907
1908 status = nfsd4_decode_sessionid4(argp, &seq->sessionid);
1909 if (status)
1910 return status;
1911 p = xdr_inline_decode(argp->xdr, XDR_UNIT * 4);
1912 if (!p)
1913 return nfserr_bad_xdr;
1914 seq->seqid = be32_to_cpup(p++);
1915 seq->slotid = be32_to_cpup(p++);
1916 seq->maxslots = be32_to_cpup(p++);
1917 seq->cachethis = be32_to_cpup(p);
1918
1919 return nfs_ok;
1920}
1921
1922static __be32
Anna Schumaker95d871f2014-11-07 14:44:26 -05001923nfsd4_decode_fallocate(struct nfsd4_compoundargs *argp,
1924 struct nfsd4_fallocate *fallocate)
1925{
1926 DECODE_HEAD;
1927
1928 status = nfsd4_decode_stateid(argp, &fallocate->falloc_stateid);
1929 if (status)
1930 return status;
1931
1932 READ_BUF(16);
1933 p = xdr_decode_hyper(p, &fallocate->falloc_offset);
1934 xdr_decode_hyper(p, &fallocate->falloc_length);
1935
1936 DECODE_TAIL;
1937}
1938
1939static __be32
Christoph Hellwigffa01602015-12-03 12:59:52 +01001940nfsd4_decode_clone(struct nfsd4_compoundargs *argp, struct nfsd4_clone *clone)
1941{
1942 DECODE_HEAD;
1943
1944 status = nfsd4_decode_stateid(argp, &clone->cl_src_stateid);
1945 if (status)
1946 return status;
1947 status = nfsd4_decode_stateid(argp, &clone->cl_dst_stateid);
1948 if (status)
1949 return status;
1950
1951 READ_BUF(8 + 8 + 8);
1952 p = xdr_decode_hyper(p, &clone->cl_src_pos);
1953 p = xdr_decode_hyper(p, &clone->cl_dst_pos);
1954 p = xdr_decode_hyper(p, &clone->cl_count);
1955 DECODE_TAIL;
1956}
1957
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001958static __be32 nfsd4_decode_nl4_server(struct nfsd4_compoundargs *argp,
1959 struct nl4_server *ns)
1960{
1961 DECODE_HEAD;
1962 struct nfs42_netaddr *naddr;
1963
1964 READ_BUF(4);
1965 ns->nl4_type = be32_to_cpup(p++);
1966
1967 /* currently support for 1 inter-server source server */
1968 switch (ns->nl4_type) {
1969 case NL4_NETADDR:
1970 naddr = &ns->u.nl4_addr;
1971
1972 READ_BUF(4);
1973 naddr->netid_len = be32_to_cpup(p++);
1974 if (naddr->netid_len > RPCBIND_MAXNETIDLEN)
1975 goto xdr_error;
1976
1977 READ_BUF(naddr->netid_len + 4); /* 4 for uaddr len */
1978 COPYMEM(naddr->netid, naddr->netid_len);
1979
1980 naddr->addr_len = be32_to_cpup(p++);
1981 if (naddr->addr_len > RPCBIND_MAXUADDRLEN)
1982 goto xdr_error;
1983
1984 READ_BUF(naddr->addr_len);
1985 COPYMEM(naddr->addr, naddr->addr_len);
1986 break;
1987 default:
1988 goto xdr_error;
1989 }
1990 DECODE_TAIL;
1991}
1992
Christoph Hellwigffa01602015-12-03 12:59:52 +01001993static __be32
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001994nfsd4_decode_copy(struct nfsd4_compoundargs *argp, struct nfsd4_copy *copy)
1995{
1996 DECODE_HEAD;
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001997 struct nl4_server *ns_dummy;
1998 int i, count;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001999
2000 status = nfsd4_decode_stateid(argp, &copy->cp_src_stateid);
2001 if (status)
2002 return status;
2003 status = nfsd4_decode_stateid(argp, &copy->cp_dst_stateid);
2004 if (status)
2005 return status;
2006
2007 READ_BUF(8 + 8 + 8 + 4 + 4 + 4);
2008 p = xdr_decode_hyper(p, &copy->cp_src_pos);
2009 p = xdr_decode_hyper(p, &copy->cp_dst_pos);
2010 p = xdr_decode_hyper(p, &copy->cp_count);
J. Bruce Fieldsedcc8452018-03-07 15:37:35 -05002011 p++; /* ca_consecutive: we always do consecutive copies */
Anna Schumaker29ae7f92016-09-07 15:57:30 -04002012 copy->cp_synchronous = be32_to_cpup(p++);
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04002013
2014 count = be32_to_cpup(p++);
2015
2016 copy->cp_intra = false;
2017 if (count == 0) { /* intra-server copy */
2018 copy->cp_intra = true;
2019 goto intra;
2020 }
2021
2022 /* decode all the supplied server addresses but use first */
2023 status = nfsd4_decode_nl4_server(argp, &copy->cp_src);
2024 if (status)
2025 return status;
2026
2027 ns_dummy = kmalloc(sizeof(struct nl4_server), GFP_KERNEL);
2028 if (ns_dummy == NULL)
2029 return nfserrno(-ENOMEM);
2030 for (i = 0; i < count - 1; i++) {
2031 status = nfsd4_decode_nl4_server(argp, ns_dummy);
2032 if (status) {
2033 kfree(ns_dummy);
2034 return status;
2035 }
2036 }
2037 kfree(ns_dummy);
2038intra:
Anna Schumaker29ae7f92016-09-07 15:57:30 -04002039
2040 DECODE_TAIL;
2041}
2042
2043static __be32
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04002044nfsd4_decode_offload_status(struct nfsd4_compoundargs *argp,
2045 struct nfsd4_offload_status *os)
2046{
2047 return nfsd4_decode_stateid(argp, &os->stateid);
2048}
2049
2050static __be32
Olga Kornievskaia51911862019-08-08 11:14:59 -04002051nfsd4_decode_copy_notify(struct nfsd4_compoundargs *argp,
2052 struct nfsd4_copy_notify *cn)
2053{
Chuck Lever5aff7d02020-09-11 14:47:59 -04002054 __be32 status;
Olga Kornievskaia51911862019-08-08 11:14:59 -04002055
2056 status = nfsd4_decode_stateid(argp, &cn->cpn_src_stateid);
2057 if (status)
2058 return status;
2059 return nfsd4_decode_nl4_server(argp, &cn->cpn_dst);
2060}
2061
2062static __be32
Anna Schumaker24bab492014-09-26 13:58:27 -04002063nfsd4_decode_seek(struct nfsd4_compoundargs *argp, struct nfsd4_seek *seek)
2064{
2065 DECODE_HEAD;
2066
2067 status = nfsd4_decode_stateid(argp, &seek->seek_stateid);
2068 if (status)
2069 return status;
2070
2071 READ_BUF(8 + 4);
2072 p = xdr_decode_hyper(p, &seek->seek_offset);
2073 seek->seek_whence = be32_to_cpup(p);
2074
2075 DECODE_TAIL;
2076}
2077
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002078/*
2079 * XDR data that is more than PAGE_SIZE in size is normally part of a
2080 * read or write. However, the size of extended attributes is limited
2081 * by the maximum request size, and then further limited by the underlying
2082 * filesystem limits. This can exceed PAGE_SIZE (currently, XATTR_SIZE_MAX
2083 * is 64k). Since there is no kvec- or page-based interface to xattrs,
2084 * and we're not dealing with contiguous pages, we need to do some copying.
2085 */
2086
2087/*
Chuck Leverc1346a122020-11-03 11:54:23 -05002088 * Decode data into buffer.
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002089 */
2090static __be32
Chuck Leverc1346a122020-11-03 11:54:23 -05002091nfsd4_vbuf_from_vector(struct nfsd4_compoundargs *argp, struct xdr_buf *xdr,
2092 char **bufp, u32 buflen)
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002093{
Chuck Leverc1346a122020-11-03 11:54:23 -05002094 struct page **pages = xdr->pages;
2095 struct kvec *head = xdr->head;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002096 char *tmp, *dp;
2097 u32 len;
2098
2099 if (buflen <= head->iov_len) {
2100 /*
2101 * We're in luck, the head has enough space. Just return
2102 * the head, no need for copying.
2103 */
2104 *bufp = head->iov_base;
2105 return 0;
2106 }
2107
2108 tmp = svcxdr_tmpalloc(argp, buflen);
2109 if (tmp == NULL)
2110 return nfserr_jukebox;
2111
2112 dp = tmp;
2113 memcpy(dp, head->iov_base, head->iov_len);
2114 buflen -= head->iov_len;
2115 dp += head->iov_len;
2116
2117 while (buflen > 0) {
2118 len = min_t(u32, buflen, PAGE_SIZE);
2119 memcpy(dp, page_address(*pages), len);
2120
2121 buflen -= len;
2122 dp += len;
2123 pages++;
2124 }
2125
2126 *bufp = tmp;
2127 return 0;
2128}
2129
2130/*
2131 * Get a user extended attribute name from the XDR buffer.
2132 * It will not have the "user." prefix, so prepend it.
2133 * Lastly, check for nul characters in the name.
2134 */
2135static __be32
2136nfsd4_decode_xattr_name(struct nfsd4_compoundargs *argp, char **namep)
2137{
2138 DECODE_HEAD;
2139 char *name, *sp, *dp;
2140 u32 namelen, cnt;
2141
2142 READ_BUF(4);
2143 namelen = be32_to_cpup(p++);
2144
2145 if (namelen > (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN))
2146 return nfserr_nametoolong;
2147
2148 if (namelen == 0)
2149 goto xdr_error;
2150
2151 READ_BUF(namelen);
2152
2153 name = svcxdr_tmpalloc(argp, namelen + XATTR_USER_PREFIX_LEN + 1);
2154 if (!name)
2155 return nfserr_jukebox;
2156
2157 memcpy(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
2158
2159 /*
2160 * Copy the extended attribute name over while checking for 0
2161 * characters.
2162 */
2163 sp = (char *)p;
2164 dp = name + XATTR_USER_PREFIX_LEN;
2165 cnt = namelen;
2166
2167 while (cnt-- > 0) {
2168 if (*sp == '\0')
2169 goto xdr_error;
2170 *dp++ = *sp++;
2171 }
2172 *dp = '\0';
2173
2174 *namep = name;
2175
2176 DECODE_TAIL;
2177}
2178
2179/*
2180 * A GETXATTR op request comes without a length specifier. We just set the
2181 * maximum length for the reply based on XATTR_SIZE_MAX and the maximum
2182 * channel reply size. nfsd_getxattr will probe the length of the xattr,
2183 * check it against getxa_len, and allocate + return the value.
2184 */
2185static __be32
2186nfsd4_decode_getxattr(struct nfsd4_compoundargs *argp,
2187 struct nfsd4_getxattr *getxattr)
2188{
2189 __be32 status;
2190 u32 maxcount;
2191
2192 status = nfsd4_decode_xattr_name(argp, &getxattr->getxa_name);
2193 if (status)
2194 return status;
2195
2196 maxcount = svc_max_payload(argp->rqstp);
2197 maxcount = min_t(u32, XATTR_SIZE_MAX, maxcount);
2198
2199 getxattr->getxa_len = maxcount;
2200
2201 return status;
2202}
2203
2204static __be32
2205nfsd4_decode_setxattr(struct nfsd4_compoundargs *argp,
2206 struct nfsd4_setxattr *setxattr)
2207{
2208 DECODE_HEAD;
2209 u32 flags, maxcount, size;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002210
2211 READ_BUF(4);
2212 flags = be32_to_cpup(p++);
2213
2214 if (flags > SETXATTR4_REPLACE)
2215 return nfserr_inval;
2216 setxattr->setxa_flags = flags;
2217
2218 status = nfsd4_decode_xattr_name(argp, &setxattr->setxa_name);
2219 if (status)
2220 return status;
2221
2222 maxcount = svc_max_payload(argp->rqstp);
2223 maxcount = min_t(u32, XATTR_SIZE_MAX, maxcount);
2224
2225 READ_BUF(4);
2226 size = be32_to_cpup(p++);
2227 if (size > maxcount)
2228 return nfserr_xattr2big;
2229
2230 setxattr->setxa_len = size;
2231 if (size > 0) {
Chuck Leverc1346a122020-11-03 11:54:23 -05002232 struct xdr_buf payload;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002233
Chuck Leverc1346a122020-11-03 11:54:23 -05002234 if (!xdr_stream_subsegment(argp->xdr, &payload, size))
2235 goto xdr_error;
2236 status = nfsd4_vbuf_from_vector(argp, &payload,
2237 &setxattr->setxa_buf, size);
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002238 }
2239
2240 DECODE_TAIL;
2241}
2242
2243static __be32
2244nfsd4_decode_listxattrs(struct nfsd4_compoundargs *argp,
2245 struct nfsd4_listxattrs *listxattrs)
2246{
2247 DECODE_HEAD;
2248 u32 maxcount;
2249
2250 READ_BUF(12);
2251 p = xdr_decode_hyper(p, &listxattrs->lsxa_cookie);
2252
2253 /*
2254 * If the cookie is too large to have even one user.x attribute
2255 * plus trailing '\0' left in a maximum size buffer, it's invalid.
2256 */
2257 if (listxattrs->lsxa_cookie >=
2258 (XATTR_LIST_MAX / (XATTR_USER_PREFIX_LEN + 2)))
2259 return nfserr_badcookie;
2260
2261 maxcount = be32_to_cpup(p++);
2262 if (maxcount < 8)
2263 /* Always need at least 2 words (length and one character) */
2264 return nfserr_inval;
2265
2266 maxcount = min(maxcount, svc_max_payload(argp->rqstp));
2267 listxattrs->lsxa_maxcount = maxcount;
2268
2269 DECODE_TAIL;
2270}
2271
2272static __be32
2273nfsd4_decode_removexattr(struct nfsd4_compoundargs *argp,
2274 struct nfsd4_removexattr *removexattr)
2275{
2276 return nfsd4_decode_xattr_name(argp, &removexattr->rmxa_name);
2277}
2278
Anna Schumaker24bab492014-09-26 13:58:27 -04002279static __be32
Benny Halevy347e0ad2008-07-02 11:13:41 +03002280nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
2281{
2282 return nfs_ok;
2283}
2284
Benny Halevy3c375c62008-07-02 11:14:01 +03002285static __be32
2286nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
2287{
Benny Halevy1e685ec2009-03-04 23:06:06 +02002288 return nfserr_notsupp;
Benny Halevy3c375c62008-07-02 11:14:01 +03002289}
2290
Benny Halevy347e0ad2008-07-02 11:13:41 +03002291typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
2292
Chuck Leverc1df6092017-08-01 11:59:58 -04002293static const nfsd4_dec nfsd4_dec_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04002294 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
2295 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
2296 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
2297 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
2298 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
2299 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
2300 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
2301 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
2302 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
2303 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
2304 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
2305 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
2306 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
2307 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
2308 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
2309 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
2310 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
2311 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
2312 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
2313 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002314 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_putpubfh,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04002315 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
2316 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
2317 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
2318 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
2319 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
2320 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
2321 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
2322 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
2323 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
2324 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
2325 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
2326 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
2327 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
2328 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
2329 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
2330 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
Andy Adamson2db134e2009-04-03 08:27:55 +03002331
2332 /* new operations for NFSv4.1 */
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002333 [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002334 [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
Randy Dunlap9064caa2009-04-28 16:48:25 -07002335 [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
2336 [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
2337 [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04002338 [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07002339 [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002340#ifdef CONFIG_NFSD_PNFS
2341 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_getdeviceinfo,
2342 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
2343 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_layoutcommit,
2344 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_layoutget,
2345 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_layoutreturn,
2346#else
Randy Dunlap9064caa2009-04-28 16:48:25 -07002347 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
2348 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
2349 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
2350 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
2351 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002352#endif
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05002353 [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
Randy Dunlap9064caa2009-04-28 16:48:25 -07002354 [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
2355 [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
Bryan Schumaker17456802011-07-13 10:50:48 -04002356 [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07002357 [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
Mi Jinlong345c2842011-10-20 17:51:39 +08002358 [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002359 [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
Anna Schumaker87a15a82014-09-26 13:58:26 -04002360
2361 /* new operations for NFSv4.2 */
Anna Schumaker95d871f2014-11-07 14:44:26 -05002362 [OP_ALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
Anna Schumaker29ae7f92016-09-07 15:57:30 -04002363 [OP_COPY] = (nfsd4_dec)nfsd4_decode_copy,
Olga Kornievskaia51911862019-08-08 11:14:59 -04002364 [OP_COPY_NOTIFY] = (nfsd4_dec)nfsd4_decode_copy_notify,
Anna Schumakerb0cb9082014-11-07 14:44:27 -05002365 [OP_DEALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
Anna Schumaker87a15a82014-09-26 13:58:26 -04002366 [OP_IO_ADVISE] = (nfsd4_dec)nfsd4_decode_notsupp,
2367 [OP_LAYOUTERROR] = (nfsd4_dec)nfsd4_decode_notsupp,
2368 [OP_LAYOUTSTATS] = (nfsd4_dec)nfsd4_decode_notsupp,
Olga Kornievskaia885e2bf2018-07-20 18:19:19 -04002369 [OP_OFFLOAD_CANCEL] = (nfsd4_dec)nfsd4_decode_offload_status,
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04002370 [OP_OFFLOAD_STATUS] = (nfsd4_dec)nfsd4_decode_offload_status,
Anna Schumaker528b8492020-09-28 13:08:58 -04002371 [OP_READ_PLUS] = (nfsd4_dec)nfsd4_decode_read,
Anna Schumaker24bab492014-09-26 13:58:27 -04002372 [OP_SEEK] = (nfsd4_dec)nfsd4_decode_seek,
Anna Schumaker87a15a82014-09-26 13:58:26 -04002373 [OP_WRITE_SAME] = (nfsd4_dec)nfsd4_decode_notsupp,
Christoph Hellwigffa01602015-12-03 12:59:52 +01002374 [OP_CLONE] = (nfsd4_dec)nfsd4_decode_clone,
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002375 /* RFC 8276 extended atributes operations */
2376 [OP_GETXATTR] = (nfsd4_dec)nfsd4_decode_getxattr,
2377 [OP_SETXATTR] = (nfsd4_dec)nfsd4_decode_setxattr,
2378 [OP_LISTXATTRS] = (nfsd4_dec)nfsd4_decode_listxattrs,
2379 [OP_REMOVEXATTR] = (nfsd4_dec)nfsd4_decode_removexattr,
Andy Adamson2db134e2009-04-03 08:27:55 +03002380};
2381
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002382static inline bool
2383nfsd4_opnum_in_range(struct nfsd4_compoundargs *argp, struct nfsd4_op *op)
2384{
Anna Schumaker8217d142013-10-30 13:38:13 -04002385 if (op->opnum < FIRST_NFS4_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002386 return false;
Anna Schumaker8217d142013-10-30 13:38:13 -04002387 else if (argp->minorversion == 0 && op->opnum > LAST_NFS40_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002388 return false;
Anna Schumaker8217d142013-10-30 13:38:13 -04002389 else if (argp->minorversion == 1 && op->opnum > LAST_NFS41_OP)
2390 return false;
2391 else if (argp->minorversion == 2 && op->opnum > LAST_NFS42_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002392 return false;
2393 return true;
2394}
Benny Halevyf2feb962008-07-02 11:14:22 +03002395
Benny Halevy347e0ad2008-07-02 11:13:41 +03002396static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
2398{
2399 DECODE_HEAD;
2400 struct nfsd4_op *op;
J. Bruce Fields10910062011-01-24 12:11:02 -05002401 bool cachethis = false;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002402 int auth_slack= argp->rqstp->rq_auth_slack;
2403 int max_reply = auth_slack + 8; /* opcnt, status */
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05002404 int readcount = 0;
2405 int readbytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 int i;
2407
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04002409 argp->taglen = be32_to_cpup(p++);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05002410 READ_BUF(argp->taglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 SAVEMEM(argp->tag, argp->taglen);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05002412 READ_BUF(8);
J. Bruce Fields06553992014-06-02 12:32:51 -04002413 argp->minorversion = be32_to_cpup(p++);
2414 argp->opcnt = be32_to_cpup(p++);
J. Bruce Fields4f0cefb2014-03-11 15:39:13 -04002415 max_reply += 4 + (XDR_QUADLEN(argp->taglen) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
2417 if (argp->taglen > NFSD4_MAX_TAGLEN)
2418 goto xdr_error;
J. Bruce Fields00781172017-11-15 12:30:27 -05002419 /*
2420 * NFS4ERR_RESOURCE is a more helpful error than GARBAGE_ARGS
2421 * here, so we return success at the xdr level so that
2422 * nfsd4_proc can handle this is an NFS-level error.
2423 */
2424 if (argp->opcnt > NFSD_MAX_OPS_PER_COMPOUND)
2425 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
Tobias Klausere8c96f82006-03-24 03:15:34 -08002427 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
J. Bruce Fields5d6031c2014-07-17 16:20:39 -04002428 argp->ops = kzalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 if (!argp->ops) {
2430 argp->ops = argp->iops;
Chuck Lever817cb9d2007-09-11 18:01:20 -04002431 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 goto xdr_error;
2433 }
2434 }
2435
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002436 if (argp->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
Benny Halevy30cff1f2008-07-02 11:13:18 +03002437 argp->opcnt = 0;
2438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 for (i = 0; i < argp->opcnt; i++) {
2440 op = &argp->ops[i];
2441 op->replay = NULL;
2442
J. Bruce Fields8a61b182012-11-16 22:28:38 -05002443 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04002444 op->opnum = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
Chuck Lever08281342020-11-21 11:36:42 -05002446 if (nfsd4_opnum_in_range(argp, op)) {
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002447 op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
Chuck Lever08281342020-11-21 11:36:42 -05002448 if (op->status != nfs_ok)
2449 trace_nfsd_compound_decode_err(argp->rqstp,
2450 argp->opcnt, i,
2451 op->opnum,
2452 op->status);
2453 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 op->opnum = OP_ILLEGAL;
2455 op->status = nfserr_op_illegal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 }
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002457 op->opdesc = OPDESC(op);
J. Bruce Fields10910062011-01-24 12:11:02 -05002458 /*
2459 * We'll try to cache the result in the DRC if any one
2460 * op in the compound wants to be cached:
2461 */
2462 cachethis |= nfsd4_cache_this_op(op);
J. Bruce Fields6ff40de2013-11-05 15:07:16 -05002463
Anna Schumaker528b8492020-09-28 13:08:58 -04002464 if (op->opnum == OP_READ || op->opnum == OP_READ_PLUS) {
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05002465 readcount++;
2466 readbytes += nfsd4_max_reply(argp->rqstp, op);
2467 } else
2468 max_reply += nfsd4_max_reply(argp->rqstp, op);
J. Bruce Fieldsf7b43d02014-08-12 11:41:40 -04002469 /*
Kinglong Mee7323f0d2017-02-03 22:51:46 +08002470 * OP_LOCK and OP_LOCKT may return a conflicting lock.
2471 * (Special case because it will just skip encoding this
2472 * if it runs out of xdr buffer space, and it is the only
2473 * operation that behaves this way.)
J. Bruce Fieldsf7b43d02014-08-12 11:41:40 -04002474 */
Kinglong Mee7323f0d2017-02-03 22:51:46 +08002475 if (op->opnum == OP_LOCK || op->opnum == OP_LOCKT)
J. Bruce Fieldsf7b43d02014-08-12 11:41:40 -04002476 max_reply += NFS4_OPAQUE_LIMIT;
J. Bruce Fieldse372ba62014-05-19 12:27:11 -04002477
2478 if (op->status) {
2479 argp->opcnt = i+1;
2480 break;
2481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 }
J. Bruce Fields10910062011-01-24 12:11:02 -05002483 /* Sessions make the DRC unnecessary: */
2484 if (argp->minorversion)
2485 cachethis = false;
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05002486 svc_reserve(argp->rqstp, max_reply + readbytes);
J. Bruce Fields10910062011-01-24 12:11:02 -05002487 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002489 if (readcount > 1 || max_reply > PAGE_SIZE - auth_slack)
Jeff Layton779fb0f2014-11-19 07:51:18 -05002490 clear_bit(RQ_SPLICE_OK, &argp->rqstp->rq_flags);
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05002491
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 DECODE_TAIL;
2493}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
NeilBrownb8800922017-01-30 17:17:00 +11002495static __be32 *encode_change(__be32 *p, struct kstat *stat, struct inode *inode,
2496 struct svc_export *exp)
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002497{
NeilBrownb8800922017-01-30 17:17:00 +11002498 if (exp->ex_flags & NFSEXP_V4ROOT) {
2499 *p++ = cpu_to_be32(convert_to_wallclock(exp->cd->flush_time));
2500 *p++ = 0;
2501 } else if (IS_I_VERSION(inode)) {
Amir Goldstein39ca1bf2018-01-03 17:14:35 +02002502 p = xdr_encode_hyper(p, nfsd4_change_attribute(stat, inode));
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002503 } else {
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002504 *p++ = cpu_to_be32(stat->ctime.tv_sec);
2505 *p++ = cpu_to_be32(stat->ctime.tv_nsec);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002506 }
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002507 return p;
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002508}
2509
J. Bruce Fields16945142018-04-25 14:34:11 -04002510/*
2511 * ctime (in NFSv4, time_metadata) is not writeable, and the client
2512 * doesn't really care what resolution could theoretically be stored by
2513 * the filesystem.
2514 *
2515 * The client cares how close together changes can be while still
2516 * guaranteeing ctime changes. For most filesystems (which have
2517 * timestamps with nanosecond fields) that is limited by the resolution
2518 * of the time returned from current_time() (which I'm assuming to be
2519 * 1/HZ).
2520 */
2521static __be32 *encode_time_delta(__be32 *p, struct inode *inode)
2522{
Arnd Bergmanne4598e32019-10-31 15:14:03 +01002523 struct timespec64 ts;
J. Bruce Fields16945142018-04-25 14:34:11 -04002524 u32 ns;
2525
2526 ns = max_t(u32, NSEC_PER_SEC/HZ, inode->i_sb->s_time_gran);
Arnd Bergmanne4598e32019-10-31 15:14:03 +01002527 ts = ns_to_timespec64(ns);
J. Bruce Fields16945142018-04-25 14:34:11 -04002528
2529 p = xdr_encode_hyper(p, ts.tv_sec);
2530 *p++ = cpu_to_be32(ts.tv_nsec);
2531
2532 return p;
2533}
2534
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002535static __be32 *encode_cinfo(__be32 *p, struct nfsd4_change_info *c)
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002536{
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002537 *p++ = cpu_to_be32(c->atomic);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002538 if (c->change_supported) {
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002539 p = xdr_encode_hyper(p, c->before_change);
2540 p = xdr_encode_hyper(p, c->after_change);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002541 } else {
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002542 *p++ = cpu_to_be32(c->before_ctime_sec);
2543 *p++ = cpu_to_be32(c->before_ctime_nsec);
2544 *p++ = cpu_to_be32(c->after_ctime_sec);
2545 *p++ = cpu_to_be32(c->after_ctime_nsec);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002546 }
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002547 return p;
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002548}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002550/* Encode as an array of strings the string given with components
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002551 * separated @sep, escaped with esc_enter and esc_exit.
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002552 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002553static __be32 nfsd4_encode_components_esc(struct xdr_stream *xdr, char sep,
2554 char *components, char esc_enter,
2555 char esc_exit)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002556{
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002557 __be32 *p;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002558 __be32 pathlen;
2559 int pathlen_offset;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002560 int strlen, count=0;
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002561 char *str, *end, *next;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002562
2563 dprintk("nfsd4_encode_components(%s)\n", components);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002564
2565 pathlen_offset = xdr->buf->len;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002566 p = xdr_reserve_space(xdr, 4);
2567 if (!p)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002568 return nfserr_resource;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002569 p++; /* We will fill this in with @count later */
2570
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002571 end = str = components;
2572 while (*end) {
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002573 bool found_esc = false;
2574
2575 /* try to parse as esc_start, ..., esc_end, sep */
2576 if (*str == esc_enter) {
2577 for (; *end && (*end != esc_exit); end++)
2578 /* find esc_exit or end of string */;
2579 next = end + 1;
2580 if (*end && (!*next || *next == sep)) {
2581 str++;
2582 found_esc = true;
2583 }
2584 }
2585
2586 if (!found_esc)
2587 for (; *end && (*end != sep); end++)
2588 /* find sep or end of string */;
2589
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002590 strlen = end - str;
2591 if (strlen) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002592 p = xdr_reserve_space(xdr, strlen + 4);
2593 if (!p)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002594 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04002595 p = xdr_encode_opaque(p, str, strlen);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002596 count++;
2597 }
2598 else
2599 end++;
Benjamin Coddington5a64e562014-12-07 16:05:47 -05002600 if (found_esc)
2601 end = next;
2602
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002603 str = end;
2604 }
Benjamin Coddingtonbf7491f2014-12-07 16:05:48 -05002605 pathlen = htonl(count);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002606 write_bytes_to_xdr_buf(xdr->buf, pathlen_offset, &pathlen, 4);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002607 return 0;
2608}
2609
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002610/* Encode as an array of strings the string given with components
2611 * separated @sep.
2612 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002613static __be32 nfsd4_encode_components(struct xdr_stream *xdr, char sep,
2614 char *components)
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002615{
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002616 return nfsd4_encode_components_esc(xdr, sep, components, 0, 0);
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002617}
2618
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002619/*
2620 * encode a location element of a fs_locations structure
2621 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002622static __be32 nfsd4_encode_fs_location4(struct xdr_stream *xdr,
2623 struct nfsd4_fs_location *location)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002624{
Al Virob37ad282006-10-19 23:28:59 -07002625 __be32 status;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002626
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002627 status = nfsd4_encode_components_esc(xdr, ':', location->hosts,
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002628 '[', ']');
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002629 if (status)
2630 return status;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002631 status = nfsd4_encode_components(xdr, '/', location->path);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002632 if (status)
2633 return status;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002634 return 0;
2635}
2636
2637/*
Trond Myklebusted748aa2011-09-12 19:37:06 -04002638 * Encode a path in RFC3530 'pathname4' format
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002639 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002640static __be32 nfsd4_encode_path(struct xdr_stream *xdr,
2641 const struct path *root,
2642 const struct path *path)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002643{
Al Viro301f0262013-09-01 16:06:39 -04002644 struct path cur = *path;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002645 __be32 *p;
Trond Myklebusted748aa2011-09-12 19:37:06 -04002646 struct dentry **components = NULL;
2647 unsigned int ncomponents = 0;
2648 __be32 err = nfserr_jukebox;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002649
Trond Myklebusted748aa2011-09-12 19:37:06 -04002650 dprintk("nfsd4_encode_components(");
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002651
Trond Myklebusted748aa2011-09-12 19:37:06 -04002652 path_get(&cur);
2653 /* First walk the path up to the nfsd root, and store the
2654 * dentries/path components in an array.
2655 */
2656 for (;;) {
Kinglong Meeb77a4b22015-03-15 23:13:32 +08002657 if (path_equal(&cur, root))
Trond Myklebusted748aa2011-09-12 19:37:06 -04002658 break;
2659 if (cur.dentry == cur.mnt->mnt_root) {
2660 if (follow_up(&cur))
2661 continue;
2662 goto out_free;
2663 }
2664 if ((ncomponents & 15) == 0) {
2665 struct dentry **new;
2666 new = krealloc(components,
2667 sizeof(*new) * (ncomponents + 16),
2668 GFP_KERNEL);
2669 if (!new)
2670 goto out_free;
2671 components = new;
2672 }
2673 components[ncomponents++] = cur.dentry;
2674 cur.dentry = dget_parent(cur.dentry);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002675 }
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002676 err = nfserr_resource;
2677 p = xdr_reserve_space(xdr, 4);
2678 if (!p)
Trond Myklebusted748aa2011-09-12 19:37:06 -04002679 goto out_free;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002680 *p++ = cpu_to_be32(ncomponents);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002681
2682 while (ncomponents) {
2683 struct dentry *dentry = components[ncomponents - 1];
Al Viro301f0262013-09-01 16:06:39 -04002684 unsigned int len;
Trond Myklebusted748aa2011-09-12 19:37:06 -04002685
Al Viro301f0262013-09-01 16:06:39 -04002686 spin_lock(&dentry->d_lock);
2687 len = dentry->d_name.len;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002688 p = xdr_reserve_space(xdr, len + 4);
2689 if (!p) {
Al Viro301f0262013-09-01 16:06:39 -04002690 spin_unlock(&dentry->d_lock);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002691 goto out_free;
Al Viro301f0262013-09-01 16:06:39 -04002692 }
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04002693 p = xdr_encode_opaque(p, dentry->d_name.name, len);
Al Viroa4555892014-10-21 20:11:25 -04002694 dprintk("/%pd", dentry);
Al Viro301f0262013-09-01 16:06:39 -04002695 spin_unlock(&dentry->d_lock);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002696 dput(dentry);
2697 ncomponents--;
2698 }
2699
Trond Myklebusted748aa2011-09-12 19:37:06 -04002700 err = 0;
2701out_free:
2702 dprintk(")\n");
2703 while (ncomponents)
2704 dput(components[--ncomponents]);
2705 kfree(components);
2706 path_put(&cur);
2707 return err;
2708}
2709
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002710static __be32 nfsd4_encode_fsloc_fsroot(struct xdr_stream *xdr,
2711 struct svc_rqst *rqstp, const struct path *path)
Trond Myklebusted748aa2011-09-12 19:37:06 -04002712{
2713 struct svc_export *exp_ps;
2714 __be32 res;
2715
2716 exp_ps = rqst_find_fsidzero_export(rqstp);
2717 if (IS_ERR(exp_ps))
2718 return nfserrno(PTR_ERR(exp_ps));
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002719 res = nfsd4_encode_path(xdr, &exp_ps->ex_path, path);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002720 exp_put(exp_ps);
2721 return res;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002722}
2723
2724/*
2725 * encode a fs_locations structure
2726 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002727static __be32 nfsd4_encode_fs_locations(struct xdr_stream *xdr,
2728 struct svc_rqst *rqstp, struct svc_export *exp)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002729{
Al Virob37ad282006-10-19 23:28:59 -07002730 __be32 status;
Al Virocc45f012006-10-19 23:28:44 -07002731 int i;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002732 __be32 *p;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002733 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002734
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002735 status = nfsd4_encode_fsloc_fsroot(xdr, rqstp, &exp->ex_path);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002736 if (status)
2737 return status;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002738 p = xdr_reserve_space(xdr, 4);
2739 if (!p)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002740 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002741 *p++ = cpu_to_be32(fslocs->locations_count);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002742 for (i=0; i<fslocs->locations_count; i++) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002743 status = nfsd4_encode_fs_location4(xdr, &fslocs->locations[i]);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002744 if (status)
2745 return status;
2746 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002747 return 0;
2748}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002750static u32 nfs4_file_type(umode_t mode)
2751{
2752 switch (mode & S_IFMT) {
2753 case S_IFIFO: return NF4FIFO;
2754 case S_IFCHR: return NF4CHR;
2755 case S_IFDIR: return NF4DIR;
2756 case S_IFBLK: return NF4BLK;
2757 case S_IFLNK: return NF4LNK;
2758 case S_IFREG: return NF4REG;
2759 case S_IFSOCK: return NF4SOCK;
2760 default: return NF4BAD;
Tom Rix25fef482020-11-01 07:32:34 -08002761 }
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002762}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
Al Virob37ad282006-10-19 23:28:59 -07002764static inline __be32
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002765nfsd4_encode_aclname(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2766 struct nfs4_ace *ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767{
J. Bruce Fields35541162014-01-08 09:49:01 -05002768 if (ace->whotype != NFS4_ACL_WHO_NAMED)
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002769 return nfs4_acl_write_who(xdr, ace->whotype);
J. Bruce Fields35541162014-01-08 09:49:01 -05002770 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002771 return nfsd4_encode_group(xdr, rqstp, ace->who_gid);
J. Bruce Fields35541162014-01-08 09:49:01 -05002772 else
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002773 return nfsd4_encode_user(xdr, rqstp, ace->who_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774}
2775
Kinglong Mee6896f152015-07-30 21:52:44 +08002776static inline __be32
Jeff Layton8a4c3922016-07-10 15:55:58 -04002777nfsd4_encode_layout_types(struct xdr_stream *xdr, u32 layout_types)
Kinglong Mee6896f152015-07-30 21:52:44 +08002778{
Jeff Layton8a4c3922016-07-10 15:55:58 -04002779 __be32 *p;
2780 unsigned long i = hweight_long(layout_types);
Kinglong Mee6896f152015-07-30 21:52:44 +08002781
Jeff Layton8a4c3922016-07-10 15:55:58 -04002782 p = xdr_reserve_space(xdr, 4 + 4 * i);
2783 if (!p)
2784 return nfserr_resource;
2785
2786 *p++ = cpu_to_be32(i);
2787
2788 for (i = LAYOUT_NFSV4_1_FILES; i < LAYOUT_TYPE_MAX; ++i)
2789 if (layout_types & (1 << i))
2790 *p++ = cpu_to_be32(i);
Kinglong Mee6896f152015-07-30 21:52:44 +08002791
2792 return 0;
2793}
2794
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002795#define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
2796 FATTR4_WORD0_RDATTR_ERROR)
2797#define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
Kinglong Meec2227a32015-07-07 10:16:37 +08002798#define WORD2_ABSENT_FS_ATTRS 0
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002799
David Quigley18032ca2013-05-02 13:19:10 -04002800#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2801static inline __be32
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002802nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2803 void *context, int len)
David Quigley18032ca2013-05-02 13:19:10 -04002804{
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002805 __be32 *p;
David Quigley18032ca2013-05-02 13:19:10 -04002806
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002807 p = xdr_reserve_space(xdr, len + 4 + 4 + 4);
2808 if (!p)
David Quigley18032ca2013-05-02 13:19:10 -04002809 return nfserr_resource;
2810
2811 /*
2812 * For now we use a 0 here to indicate the null translation; in
2813 * the future we may place a call to translation code here.
2814 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002815 *p++ = cpu_to_be32(0); /* lfs */
2816 *p++ = cpu_to_be32(0); /* pi */
David Quigley18032ca2013-05-02 13:19:10 -04002817 p = xdr_encode_opaque(p, context, len);
David Quigley18032ca2013-05-02 13:19:10 -04002818 return 0;
2819}
2820#else
2821static inline __be32
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002822nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2823 void *context, int len)
David Quigley18032ca2013-05-02 13:19:10 -04002824{ return 0; }
2825#endif
2826
Kinglong Meec2227a32015-07-07 10:16:37 +08002827static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *bmval2, u32 *rdattr_err)
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002828{
2829 /* As per referral draft: */
2830 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
2831 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
2832 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
2833 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
2834 *rdattr_err = NFSERR_MOVED;
2835 else
2836 return nfserr_moved;
2837 }
2838 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
2839 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
Kinglong Meec2227a32015-07-07 10:16:37 +08002840 *bmval2 &= WORD2_ABSENT_FS_ATTRS;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002841 return 0;
2842}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002844
2845static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
2846{
2847 struct path path = exp->ex_path;
2848 int err;
2849
2850 path_get(&path);
2851 while (follow_up(&path)) {
2852 if (path.dentry != path.mnt->mnt_root)
2853 break;
2854 }
David Howellsa528d352017-01-31 16:46:22 +00002855 err = vfs_getattr(&path, stat, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002856 path_put(&path);
2857 return err;
2858}
2859
Kinglong Mee75976de2015-07-30 21:55:30 +08002860static __be32
2861nfsd4_encode_bitmap(struct xdr_stream *xdr, u32 bmval0, u32 bmval1, u32 bmval2)
2862{
2863 __be32 *p;
2864
2865 if (bmval2) {
2866 p = xdr_reserve_space(xdr, 16);
2867 if (!p)
2868 goto out_resource;
2869 *p++ = cpu_to_be32(3);
2870 *p++ = cpu_to_be32(bmval0);
2871 *p++ = cpu_to_be32(bmval1);
2872 *p++ = cpu_to_be32(bmval2);
2873 } else if (bmval1) {
2874 p = xdr_reserve_space(xdr, 12);
2875 if (!p)
2876 goto out_resource;
2877 *p++ = cpu_to_be32(2);
2878 *p++ = cpu_to_be32(bmval0);
2879 *p++ = cpu_to_be32(bmval1);
2880 } else {
2881 p = xdr_reserve_space(xdr, 8);
2882 if (!p)
2883 goto out_resource;
2884 *p++ = cpu_to_be32(1);
2885 *p++ = cpu_to_be32(bmval0);
2886 }
2887
2888 return 0;
2889out_resource:
2890 return nfserr_resource;
2891}
2892
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893/*
2894 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
2895 * ourselves.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 */
Jeff Laytonda2ebce2014-05-30 09:09:25 -04002897static __be32
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04002898nfsd4_encode_fattr(struct xdr_stream *xdr, struct svc_fh *fhp,
2899 struct svc_export *exp,
2900 struct dentry *dentry, u32 *bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002901 struct svc_rqst *rqstp, int ignore_crossmnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
2903 u32 bmval0 = bmval[0];
2904 u32 bmval1 = bmval[1];
Andy Adamson7e705702009-04-03 08:29:11 +03002905 u32 bmval2 = bmval[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 struct kstat stat;
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002907 struct svc_fh *tempfh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 struct kstatfs statfs;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002909 __be32 *p;
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05002910 int starting_len = xdr->buf->len;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002911 int attrlen_offset;
2912 __be32 attrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 u32 dummy;
2914 u64 dummy64;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002915 u32 rdattr_err = 0;
Al Virob37ad282006-10-19 23:28:59 -07002916 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07002917 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 struct nfs4_acl *acl = NULL;
Arnd Bergmann0ab88ca2019-03-22 15:07:11 +01002919#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
David Quigley18032ca2013-05-02 13:19:10 -04002920 void *context = NULL;
2921 int contextlen;
Arnd Bergmann0ab88ca2019-03-22 15:07:11 +01002922#endif
David Quigley18032ca2013-05-02 13:19:10 -04002923 bool contextsupport = false;
Andy Adamson7e705702009-04-03 08:29:11 +03002924 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2925 u32 minorversion = resp->cstate.minorversion;
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002926 struct path path = {
2927 .mnt = exp->ex_path.mnt,
2928 .dentry = dentry,
2929 };
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03002930 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
2932 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002933 BUG_ON(!nfsd_attrs_supported(minorversion, bmval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002935 if (exp->ex_fslocs.migrated) {
Kinglong Meec2227a32015-07-07 10:16:37 +08002936 status = fattr_handle_absent_fs(&bmval0, &bmval1, &bmval2, &rdattr_err);
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002937 if (status)
2938 goto out;
2939 }
2940
David Howellsa528d352017-01-31 16:46:22 +00002941 err = vfs_getattr(&path, &stat, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
Al Virob8dd7b92006-10-19 23:29:01 -07002942 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 goto out_nfserr;
Christoph Hellwig12337902014-05-28 10:46:13 +02002944 if ((bmval0 & (FATTR4_WORD0_FILES_AVAIL | FATTR4_WORD0_FILES_FREE |
2945 FATTR4_WORD0_FILES_TOTAL | FATTR4_WORD0_MAXNAME)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2947 FATTR4_WORD1_SPACE_TOTAL))) {
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002948 err = vfs_statfs(&path, &statfs);
Al Virob8dd7b92006-10-19 23:29:01 -07002949 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 goto out_nfserr;
2951 }
2952 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002953 tempfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
2954 status = nfserr_jukebox;
2955 if (!tempfh)
2956 goto out;
2957 fh_init(tempfh, NFS4_FHSIZE);
2958 status = fh_compose(tempfh, exp, dentry, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 if (status)
2960 goto out;
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002961 fhp = tempfh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 }
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02002963 if (bmval0 & FATTR4_WORD0_ACL) {
Al Virob8dd7b92006-10-19 23:29:01 -07002964 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02002965 if (err == -EOPNOTSUPP)
2966 bmval0 &= ~FATTR4_WORD0_ACL;
2967 else if (err == -EINVAL) {
2968 status = nfserr_attrnotsupp;
2969 goto out;
2970 } else if (err != 0)
2971 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
David Quigley18032ca2013-05-02 13:19:10 -04002974#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
Kinglong Meec2227a32015-07-07 10:16:37 +08002975 if ((bmval2 & FATTR4_WORD2_SECURITY_LABEL) ||
2976 bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
J. Bruce Fields32ddd942017-01-03 12:30:11 -05002977 if (exp->ex_flags & NFSEXP_SECURITY_LABEL)
2978 err = security_inode_getsecctx(d_inode(dentry),
David Quigley18032ca2013-05-02 13:19:10 -04002979 &context, &contextlen);
J. Bruce Fields32ddd942017-01-03 12:30:11 -05002980 else
2981 err = -EOPNOTSUPP;
David Quigley18032ca2013-05-02 13:19:10 -04002982 contextsupport = (err == 0);
2983 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2984 if (err == -EOPNOTSUPP)
2985 bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2986 else if (err)
2987 goto out_nfserr;
2988 }
2989 }
2990#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2991
Kinglong Mee75976de2015-07-30 21:55:30 +08002992 status = nfsd4_encode_bitmap(xdr, bmval0, bmval1, bmval2);
2993 if (status)
2994 goto out;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002995
2996 attrlen_offset = xdr->buf->len;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002997 p = xdr_reserve_space(xdr, 4);
2998 if (!p)
2999 goto out_resource;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04003000 p++; /* to be backfilled later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
3002 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
J. Bruce Fieldsdcd208692017-01-11 20:34:50 -05003003 u32 supp[3];
3004
3005 memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
Andy Adamson7e705702009-04-03 08:29:11 +03003006
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02003007 if (!IS_POSIXACL(dentry->d_inode))
J. Bruce Fields916d2d82016-10-18 14:18:40 -04003008 supp[0] &= ~FATTR4_WORD0_ACL;
David Quigley18032ca2013-05-02 13:19:10 -04003009 if (!contextsupport)
J. Bruce Fields916d2d82016-10-18 14:18:40 -04003010 supp[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3011 if (!supp[2]) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003012 p = xdr_reserve_space(xdr, 12);
3013 if (!p)
Benny Halevy2b44f1b2010-09-30 20:47:46 +02003014 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003015 *p++ = cpu_to_be32(2);
J. Bruce Fields916d2d82016-10-18 14:18:40 -04003016 *p++ = cpu_to_be32(supp[0]);
3017 *p++ = cpu_to_be32(supp[1]);
Andy Adamson7e705702009-04-03 08:29:11 +03003018 } else {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003019 p = xdr_reserve_space(xdr, 16);
3020 if (!p)
Benny Halevy2b44f1b2010-09-30 20:47:46 +02003021 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003022 *p++ = cpu_to_be32(3);
J. Bruce Fields916d2d82016-10-18 14:18:40 -04003023 *p++ = cpu_to_be32(supp[0]);
3024 *p++ = cpu_to_be32(supp[1]);
3025 *p++ = cpu_to_be32(supp[2]);
Andy Adamson7e705702009-04-03 08:29:11 +03003026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 }
3028 if (bmval0 & FATTR4_WORD0_TYPE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003029 p = xdr_reserve_space(xdr, 4);
3030 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 goto out_resource;
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04003032 dummy = nfs4_file_type(stat.mode);
J. Bruce Fields6b6d81372013-01-16 17:11:11 -05003033 if (dummy == NF4BAD) {
3034 status = nfserr_serverfault;
3035 goto out;
3036 }
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003037 *p++ = cpu_to_be32(dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 }
3039 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
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;
NeilBrown49640002005-06-23 22:02:58 -07003043 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003044 *p++ = cpu_to_be32(NFS4_FH_PERSISTENT);
NeilBrown49640002005-06-23 22:02:58 -07003045 else
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003046 *p++ = cpu_to_be32(NFS4_FH_PERSISTENT|
3047 NFS4_FH_VOL_RENAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 }
3049 if (bmval0 & FATTR4_WORD0_CHANGE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003050 p = xdr_reserve_space(xdr, 8);
3051 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 goto out_resource;
NeilBrownb8800922017-01-30 17:17:00 +11003053 p = encode_change(p, &stat, d_inode(dentry), exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 }
3055 if (bmval0 & FATTR4_WORD0_SIZE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003056 p = xdr_reserve_space(xdr, 8);
3057 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003059 p = xdr_encode_hyper(p, stat.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 }
3061 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003062 p = xdr_reserve_space(xdr, 4);
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(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 }
3067 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003068 p = xdr_reserve_space(xdr, 4);
3069 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003071 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 }
3073 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003074 p = xdr_reserve_space(xdr, 4);
3075 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003077 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 }
3079 if (bmval0 & FATTR4_WORD0_FSID) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003080 p = xdr_reserve_space(xdr, 16);
3081 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07003083 if (exp->ex_fslocs.migrated) {
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003084 p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MAJOR);
3085 p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MINOR);
NeilBrownaf6a4e22007-02-14 00:33:12 -08003086 } else switch(fsid_source(fhp)) {
3087 case FSIDSOURCE_FSID:
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003088 p = xdr_encode_hyper(p, (u64)exp->ex_fsid);
3089 p = xdr_encode_hyper(p, (u64)0);
NeilBrownaf6a4e22007-02-14 00:33:12 -08003090 break;
3091 case FSIDSOURCE_DEV:
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003092 *p++ = cpu_to_be32(0);
3093 *p++ = cpu_to_be32(MAJOR(stat.dev));
3094 *p++ = cpu_to_be32(0);
3095 *p++ = cpu_to_be32(MINOR(stat.dev));
NeilBrownaf6a4e22007-02-14 00:33:12 -08003096 break;
3097 case FSIDSOURCE_UUID:
Kinglong Mee94eb3682014-05-23 20:00:19 +08003098 p = xdr_encode_opaque_fixed(p, exp->ex_uuid,
3099 EX_UUID_LEN);
NeilBrownaf6a4e22007-02-14 00:33:12 -08003100 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 }
3102 }
3103 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003104 p = xdr_reserve_space(xdr, 4);
3105 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003107 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 }
3109 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003110 p = xdr_reserve_space(xdr, 4);
3111 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003113 *p++ = cpu_to_be32(nn->nfsd4_lease);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 }
3115 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003116 p = xdr_reserve_space(xdr, 4);
3117 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003119 *p++ = cpu_to_be32(rdattr_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 }
3121 if (bmval0 & FATTR4_WORD0_ACL) {
3122 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
3124 if (acl == NULL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003125 p = xdr_reserve_space(xdr, 4);
3126 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 goto out_resource;
3128
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003129 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 goto out_acl;
3131 }
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003132 p = xdr_reserve_space(xdr, 4);
3133 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003135 *p++ = cpu_to_be32(acl->naces);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08003137 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003138 p = xdr_reserve_space(xdr, 4*3);
3139 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003141 *p++ = cpu_to_be32(ace->type);
3142 *p++ = cpu_to_be32(ace->flag);
3143 *p++ = cpu_to_be32(ace->access_mask &
3144 NFS4_ACE_MASK_ALL);
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003145 status = nfsd4_encode_aclname(xdr, rqstp, ace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 if (status)
3147 goto out;
3148 }
3149 }
3150out_acl:
3151 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003152 p = xdr_reserve_space(xdr, 4);
3153 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 goto out_resource;
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02003155 *p++ = cpu_to_be32(IS_POSIXACL(dentry->d_inode) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
3157 }
3158 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003159 p = xdr_reserve_space(xdr, 4);
3160 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003162 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 }
3164 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003165 p = xdr_reserve_space(xdr, 4);
3166 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003168 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 }
3170 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003171 p = xdr_reserve_space(xdr, 4);
3172 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003174 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 }
3176 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003177 p = xdr_reserve_space(xdr, 4);
3178 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003180 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 }
3182 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003183 p = xdr_reserve_space(xdr, fhp->fh_handle.fh_size + 4);
3184 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 goto out_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003186 p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base,
3187 fhp->fh_handle.fh_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 }
3189 if (bmval0 & FATTR4_WORD0_FILEID) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003190 p = xdr_reserve_space(xdr, 8);
3191 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003193 p = xdr_encode_hyper(p, stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 }
3195 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003196 p = xdr_reserve_space(xdr, 8);
3197 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003199 p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 }
3201 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003202 p = xdr_reserve_space(xdr, 8);
3203 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003205 p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 }
3207 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003208 p = xdr_reserve_space(xdr, 8);
3209 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003211 p = xdr_encode_hyper(p, (u64) statfs.f_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07003213 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003214 status = nfsd4_encode_fs_locations(xdr, rqstp, exp);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07003215 if (status)
3216 goto out;
3217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003219 p = xdr_reserve_space(xdr, 4);
3220 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003222 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 }
3224 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003225 p = xdr_reserve_space(xdr, 8);
3226 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003228 p = xdr_encode_hyper(p, exp->ex_path.mnt->mnt_sb->s_maxbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 }
3230 if (bmval0 & FATTR4_WORD0_MAXLINK) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003231 p = xdr_reserve_space(xdr, 4);
3232 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003234 *p++ = cpu_to_be32(255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 }
3236 if (bmval0 & FATTR4_WORD0_MAXNAME) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003237 p = xdr_reserve_space(xdr, 4);
3238 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003240 *p++ = cpu_to_be32(statfs.f_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 }
3242 if (bmval0 & FATTR4_WORD0_MAXREAD) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003243 p = xdr_reserve_space(xdr, 8);
3244 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003246 p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 }
3248 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003249 p = xdr_reserve_space(xdr, 8);
3250 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003252 p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 }
3254 if (bmval1 & FATTR4_WORD1_MODE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003255 p = xdr_reserve_space(xdr, 4);
3256 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003258 *p++ = cpu_to_be32(stat.mode & S_IALLUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 }
3260 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003261 p = xdr_reserve_space(xdr, 4);
3262 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003264 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 }
3266 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003267 p = xdr_reserve_space(xdr, 4);
3268 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003270 *p++ = cpu_to_be32(stat.nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 }
3272 if (bmval1 & FATTR4_WORD1_OWNER) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003273 status = nfsd4_encode_user(xdr, rqstp, stat.uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 if (status)
3275 goto out;
3276 }
3277 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003278 status = nfsd4_encode_group(xdr, rqstp, stat.gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 if (status)
3280 goto out;
3281 }
3282 if (bmval1 & FATTR4_WORD1_RAWDEV) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003283 p = xdr_reserve_space(xdr, 8);
3284 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003286 *p++ = cpu_to_be32((u32) MAJOR(stat.rdev));
3287 *p++ = cpu_to_be32((u32) MINOR(stat.rdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 }
3289 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003290 p = xdr_reserve_space(xdr, 8);
3291 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 goto out_resource;
3293 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003294 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 }
3296 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003297 p = xdr_reserve_space(xdr, 8);
3298 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 goto out_resource;
3300 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003301 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 }
3303 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003304 p = xdr_reserve_space(xdr, 8);
3305 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 goto out_resource;
3307 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003308 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 }
3310 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003311 p = xdr_reserve_space(xdr, 8);
3312 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 goto out_resource;
3314 dummy64 = (u64)stat.blocks << 9;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003315 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 }
3317 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003318 p = xdr_reserve_space(xdr, 12);
3319 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003321 p = xdr_encode_hyper(p, (s64)stat.atime.tv_sec);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003322 *p++ = cpu_to_be32(stat.atime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 }
3324 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003325 p = xdr_reserve_space(xdr, 12);
3326 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 goto out_resource;
J. Bruce Fields16945142018-04-25 14:34:11 -04003328 p = encode_time_delta(p, d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 }
3330 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003331 p = xdr_reserve_space(xdr, 12);
3332 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003334 p = xdr_encode_hyper(p, (s64)stat.ctime.tv_sec);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003335 *p++ = cpu_to_be32(stat.ctime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 }
3337 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003338 p = xdr_reserve_space(xdr, 12);
3339 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003341 p = xdr_encode_hyper(p, (s64)stat.mtime.tv_sec);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003342 *p++ = cpu_to_be32(stat.mtime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 }
3344 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
Kinglong Mee0a2050d2015-07-30 21:51:54 +08003345 struct kstat parent_stat;
3346 u64 ino = stat.ino;
3347
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003348 p = xdr_reserve_space(xdr, 8);
3349 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 goto out_resource;
Frank Filz406a7ea2007-11-27 11:34:05 -08003351 /*
3352 * Get parent's attributes if not ignoring crossmount
3353 * and this is the root of a cross-mounted filesystem.
3354 */
3355 if (ignore_crossmnt == 0 &&
Kinglong Mee0a2050d2015-07-30 21:51:54 +08003356 dentry == exp->ex_path.mnt->mnt_root) {
3357 err = get_parent_attributes(exp, &parent_stat);
3358 if (err)
3359 goto out_nfserr;
3360 ino = parent_stat.ino;
3361 }
3362 p = xdr_encode_hyper(p, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003364#ifdef CONFIG_NFSD_PNFS
Kinglong Mee6896f152015-07-30 21:52:44 +08003365 if (bmval1 & FATTR4_WORD1_FS_LAYOUT_TYPES) {
Jeff Layton8a4c3922016-07-10 15:55:58 -04003366 status = nfsd4_encode_layout_types(xdr, exp->ex_layout_types);
Kinglong Mee6896f152015-07-30 21:52:44 +08003367 if (status)
3368 goto out;
3369 }
3370
3371 if (bmval2 & FATTR4_WORD2_LAYOUT_TYPES) {
Jeff Layton8a4c3922016-07-10 15:55:58 -04003372 status = nfsd4_encode_layout_types(xdr, exp->ex_layout_types);
Kinglong Mee6896f152015-07-30 21:52:44 +08003373 if (status)
3374 goto out;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003375 }
3376
3377 if (bmval2 & FATTR4_WORD2_LAYOUT_BLKSIZE) {
3378 p = xdr_reserve_space(xdr, 4);
3379 if (!p)
3380 goto out_resource;
3381 *p++ = cpu_to_be32(stat.blksize);
3382 }
3383#endif /* CONFIG_NFSD_PNFS */
Benny Halevy8c18f202009-04-03 08:29:14 +03003384 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
Trond Myklebustb26b78c2017-05-09 16:24:59 -04003385 u32 supp[3];
3386
3387 memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
3388 supp[0] &= NFSD_SUPPATTR_EXCLCREAT_WORD0;
3389 supp[1] &= NFSD_SUPPATTR_EXCLCREAT_WORD1;
3390 supp[2] &= NFSD_SUPPATTR_EXCLCREAT_WORD2;
3391
3392 status = nfsd4_encode_bitmap(xdr, supp[0], supp[1], supp[2]);
Kinglong Mee75976de2015-07-30 21:55:30 +08003393 if (status)
3394 goto out;
Benny Halevy8c18f202009-04-03 08:29:14 +03003395 }
Andy Adamson7e705702009-04-03 08:29:11 +03003396
J. Bruce Fieldsa8585762018-04-25 13:26:23 -04003397 if (bmval2 & FATTR4_WORD2_CHANGE_ATTR_TYPE) {
3398 p = xdr_reserve_space(xdr, 4);
3399 if (!p)
3400 goto out_resource;
3401 if (IS_I_VERSION(d_inode(dentry)))
3402 *p++ = cpu_to_be32(NFS4_CHANGE_TYPE_IS_MONOTONIC_INCR);
3403 else
3404 *p++ = cpu_to_be32(NFS4_CHANGE_TYPE_IS_TIME_METADATA);
3405 }
3406
Arnd Bergmann0ab88ca2019-03-22 15:07:11 +01003407#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
Kinglong Mee7d580722015-07-30 21:54:26 +08003408 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
3409 status = nfsd4_encode_security_label(xdr, rqstp, context,
3410 contextlen);
3411 if (status)
3412 goto out;
3413 }
Arnd Bergmann0ab88ca2019-03-22 15:07:11 +01003414#endif
Kinglong Mee7d580722015-07-30 21:54:26 +08003415
Frank van der Linden0e885e82020-06-23 22:39:27 +00003416 if (bmval2 & FATTR4_WORD2_XATTR_SUPPORT) {
3417 p = xdr_reserve_space(xdr, 4);
3418 if (!p)
3419 goto out_resource;
3420 err = xattr_supported_namespace(d_inode(dentry),
3421 XATTR_USER_PREFIX);
3422 *p++ = cpu_to_be32(err == 0);
3423 }
3424
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04003425 attrlen = htonl(xdr->buf->len - attrlen_offset - 4);
3426 write_bytes_to_xdr_buf(xdr->buf, attrlen_offset, &attrlen, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 status = nfs_ok;
3428
3429out:
J. Bruce Fieldsba4e55b2013-05-15 10:27:52 -04003430#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
David Quigley18032ca2013-05-02 13:19:10 -04003431 if (context)
3432 security_release_secctx(context, contextlen);
J. Bruce Fieldsba4e55b2013-05-15 10:27:52 -04003433#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08003434 kfree(acl);
Yan, Zheng18df11d02014-03-10 12:52:07 +08003435 if (tempfh) {
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05003436 fh_put(tempfh);
Yan, Zheng18df11d02014-03-10 12:52:07 +08003437 kfree(tempfh);
3438 }
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05003439 if (status)
3440 xdr_truncate_encode(xdr, starting_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 return status;
3442out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07003443 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 goto out;
3445out_resource:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 status = nfserr_resource;
3447 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448}
3449
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04003450static void svcxdr_init_encode_from_buffer(struct xdr_stream *xdr,
3451 struct xdr_buf *buf, __be32 *p, int bytes)
3452{
3453 xdr->scratch.iov_len = 0;
3454 memset(buf, 0, sizeof(struct xdr_buf));
3455 buf->head[0].iov_base = p;
3456 buf->head[0].iov_len = 0;
3457 buf->len = 0;
3458 xdr->buf = buf;
3459 xdr->iov = buf->head;
3460 xdr->p = p;
3461 xdr->end = (void *)p + bytes;
3462 buf->buflen = bytes;
3463}
3464
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04003465__be32 nfsd4_encode_fattr_to_buf(__be32 **p, int words,
3466 struct svc_fh *fhp, struct svc_export *exp,
3467 struct dentry *dentry, u32 *bmval,
3468 struct svc_rqst *rqstp, int ignore_crossmnt)
3469{
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04003470 struct xdr_buf dummy;
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04003471 struct xdr_stream xdr;
3472 __be32 ret;
3473
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04003474 svcxdr_init_encode_from_buffer(&xdr, &dummy, *p, words << 2);
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04003475 ret = nfsd4_encode_fattr(&xdr, fhp, exp, dentry, bmval, rqstp,
3476 ignore_crossmnt);
3477 *p = xdr.p;
3478 return ret;
3479}
3480
J. Bruce Fieldsc0ce6ec2008-02-11 15:48:47 -05003481static inline int attributes_need_mount(u32 *bmval)
3482{
3483 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
3484 return 1;
3485 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
3486 return 1;
3487 return 0;
3488}
3489
Al Virob37ad282006-10-19 23:28:59 -07003490static __be32
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003491nfsd4_encode_dirent_fattr(struct xdr_stream *xdr, struct nfsd4_readdir *cd,
3492 const char *name, int namlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493{
3494 struct svc_export *exp = cd->rd_fhp->fh_export;
3495 struct dentry *dentry;
Al Virob37ad282006-10-19 23:28:59 -07003496 __be32 nfserr;
Frank Filz406a7ea2007-11-27 11:34:05 -08003497 int ignore_crossmnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498
Al Viro6c2d47982019-10-31 01:21:58 -04003499 dentry = lookup_positive_unlocked(name, cd->rd_fhp->fh_dentry, namlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 if (IS_ERR(dentry))
3501 return nfserrno(PTR_ERR(dentry));
3502
3503 exp_get(exp);
Frank Filz406a7ea2007-11-27 11:34:05 -08003504 /*
3505 * In the case of a mountpoint, the client may be asking for
3506 * attributes that are only properties of the underlying filesystem
3507 * as opposed to the cross-mounted file system. In such a case,
3508 * we will not follow the cross mount and will fill the attribtutes
3509 * directly from the mountpoint dentry.
3510 */
J. Bruce Fields3227fa42009-10-25 21:43:01 -04003511 if (nfsd_mountpoint(dentry, exp)) {
J.Bruce Fields021d3a72006-12-13 00:35:24 -08003512 int err;
3513
J. Bruce Fields3227fa42009-10-25 21:43:01 -04003514 if (!(exp->ex_flags & NFSEXP_V4ROOT)
3515 && !attributes_need_mount(cd->rd_bmval)) {
3516 ignore_crossmnt = 1;
3517 goto out_encode;
3518 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07003519 /*
3520 * Why the heck aren't we just using nfsd_lookup??
3521 * Different "."/".." handling? Something else?
3522 * At least, add a comment here to explain....
3523 */
J.Bruce Fields021d3a72006-12-13 00:35:24 -08003524 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
3525 if (err) {
3526 nfserr = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 goto out_put;
3528 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07003529 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
3530 if (nfserr)
3531 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532
3533 }
J. Bruce Fields3227fa42009-10-25 21:43:01 -04003534out_encode:
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003535 nfserr = nfsd4_encode_fattr(xdr, NULL, exp, dentry, cd->rd_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08003536 cd->rd_rqstp, ignore_crossmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537out_put:
3538 dput(dentry);
3539 exp_put(exp);
3540 return nfserr;
3541}
3542
Al Viro2ebbc012006-10-19 23:28:58 -07003543static __be32 *
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003544nfsd4_encode_rdattr_error(struct xdr_stream *xdr, __be32 nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545{
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003546 __be32 *p;
3547
Kinglong Meec3a45612014-07-06 11:34:43 +08003548 p = xdr_reserve_space(xdr, 20);
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003549 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 return NULL;
3551 *p++ = htonl(2);
3552 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
3553 *p++ = htonl(0); /* bmval1 */
3554
J. Bruce Fields87915c62013-01-16 17:33:28 -05003555 *p++ = htonl(4); /* attribute length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 *p++ = nfserr; /* no htonl */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 return p;
3558}
3559
3560static int
NeilBrowna0ad13e2007-01-26 00:57:10 -08003561nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
3562 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563{
NeilBrowna0ad13e2007-01-26 00:57:10 -08003564 struct readdir_cd *ccd = ccdv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003566 struct xdr_stream *xdr = cd->xdr;
3567 int start_offset = xdr->buf->len;
3568 int cookie_offset;
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04003569 u32 name_and_cookie;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003570 int entry_bytes;
Al Virob37ad282006-10-19 23:28:59 -07003571 __be32 nfserr = nfserr_toosmall;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003572 __be64 wire_offset;
3573 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574
3575 /* In nfsv4, "." and ".." never make it onto the wire.. */
3576 if (name && isdotent(name, namlen)) {
3577 cd->common.err = nfs_ok;
3578 return 0;
3579 }
3580
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003581 if (cd->cookie_offset) {
3582 wire_offset = cpu_to_be64(offset);
3583 write_bytes_to_xdr_buf(xdr->buf, cd->cookie_offset,
3584 &wire_offset, 8);
3585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003587 p = xdr_reserve_space(xdr, 4);
3588 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 *p++ = xdr_one; /* mark entry present */
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003591 cookie_offset = xdr->buf->len;
3592 p = xdr_reserve_space(xdr, 3*4 + namlen);
3593 if (!p)
3594 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
3596 p = xdr_encode_array(p, name, namlen); /* name length & name */
3597
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003598 nfserr = nfsd4_encode_dirent_fattr(xdr, cd, name, namlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 switch (nfserr) {
3600 case nfs_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 break;
3602 case nfserr_resource:
3603 nfserr = nfserr_toosmall;
3604 goto fail;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04003605 case nfserr_noent:
Kinglong Meef41c5ad2014-06-13 17:32:42 +08003606 xdr_truncate_encode(xdr, start_offset);
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04003607 goto skip_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 default:
3609 /*
3610 * If the client requested the RDATTR_ERROR attribute,
3611 * we stuff the error code into this attribute
3612 * and continue. If this attribute was not requested,
3613 * then in accordance with the spec, we fail the
3614 * entire READDIR operation(!)
3615 */
3616 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
3617 goto fail;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003618 p = nfsd4_encode_rdattr_error(xdr, nfserr);
Fred Isaman34081ef2006-01-18 17:43:40 -08003619 if (p == NULL) {
3620 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 goto fail;
Fred Isaman34081ef2006-01-18 17:43:40 -08003622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 }
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003624 nfserr = nfserr_toosmall;
3625 entry_bytes = xdr->buf->len - start_offset;
3626 if (entry_bytes > cd->rd_maxcount)
3627 goto fail;
3628 cd->rd_maxcount -= entry_bytes;
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04003629 /*
3630 * RFC 3530 14.2.24 describes rd_dircount as only a "hint", so
3631 * let's always let through the first entry, at least:
3632 */
J. Bruce Fields0ec016e2014-12-19 18:01:35 -05003633 if (!cd->rd_dircount)
3634 goto fail;
3635 name_and_cookie = 4 + 4 * XDR_QUADLEN(namlen) + 8;
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04003636 if (name_and_cookie > cd->rd_dircount && cd->cookie_offset)
3637 goto fail;
3638 cd->rd_dircount -= min(cd->rd_dircount, name_and_cookie);
J. Bruce Fields0ec016e2014-12-19 18:01:35 -05003639
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003640 cd->cookie_offset = cookie_offset;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04003641skip_entry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 cd->common.err = nfs_ok;
3643 return 0;
3644fail:
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003645 xdr_truncate_encode(xdr, start_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 cd->common.err = nfserr;
3647 return -EINVAL;
3648}
3649
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003650static __be32
3651nfsd4_encode_stateid(struct xdr_stream *xdr, stateid_t *sid)
Benny Halevye2f282b2008-08-12 20:45:07 +03003652{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003653 __be32 *p;
Benny Halevye2f282b2008-08-12 20:45:07 +03003654
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003655 p = xdr_reserve_space(xdr, sizeof(stateid_t));
3656 if (!p)
3657 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003658 *p++ = cpu_to_be32(sid->si_generation);
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003659 p = xdr_encode_opaque_fixed(p, &sid->si_opaque,
3660 sizeof(stateid_opaque_t));
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003661 return 0;
Benny Halevye2f282b2008-08-12 20:45:07 +03003662}
3663
Benny Halevy695e12f2008-07-04 14:38:33 +03003664static __be32
Al Virob37ad282006-10-19 23:28:59 -07003665nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003667 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003668 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003670 p = xdr_reserve_space(xdr, 8);
3671 if (!p)
3672 return nfserr_resource;
3673 *p++ = cpu_to_be32(access->ac_supported);
3674 *p++ = cpu_to_be32(access->ac_resp_access);
3675 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676}
3677
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003678static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
3679{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003680 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003681 __be32 *p;
3682
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003683 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 8);
3684 if (!p)
3685 return nfserr_resource;
3686 p = xdr_encode_opaque_fixed(p, bcts->sessionid.data,
3687 NFS4_MAX_SESSIONID_LEN);
3688 *p++ = cpu_to_be32(bcts->dir);
3689 /* Upshifting from TCP to RDMA is not supported */
3690 *p++ = cpu_to_be32(0);
3691 return 0;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003692}
3693
Benny Halevy695e12f2008-07-04 14:38:33 +03003694static __be32
Al Virob37ad282006-10-19 23:28:59 -07003695nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003697 struct xdr_stream *xdr = &resp->xdr;
3698
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003699 return nfsd4_encode_stateid(xdr, &close->cl_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700}
3701
3702
Benny Halevy695e12f2008-07-04 14:38:33 +03003703static __be32
Al Virob37ad282006-10-19 23:28:59 -07003704nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003706 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003707 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003709 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
3710 if (!p)
3711 return nfserr_resource;
3712 p = xdr_encode_opaque_fixed(p, commit->co_verf.data,
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003713 NFS4_VERIFIER_SIZE);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003714 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715}
3716
Benny Halevy695e12f2008-07-04 14:38:33 +03003717static __be32
Al Virob37ad282006-10-19 23:28:59 -07003718nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003720 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003721 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003723 p = xdr_reserve_space(xdr, 20);
3724 if (!p)
3725 return nfserr_resource;
3726 encode_cinfo(p, &create->cr_cinfo);
Trond Myklebustb96811c2019-08-18 14:18:58 -04003727 return nfsd4_encode_bitmap(xdr, create->cr_bmval[0],
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003728 create->cr_bmval[1], create->cr_bmval[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729}
3730
Al Virob37ad282006-10-19 23:28:59 -07003731static __be32
3732nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733{
3734 struct svc_fh *fhp = getattr->ga_fhp;
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04003735 struct xdr_stream *xdr = &resp->xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003737 return nfsd4_encode_fattr(xdr, fhp, fhp->fh_export, fhp->fh_dentry,
3738 getattr->ga_bmval, resp->rqstp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739}
3740
Benny Halevy695e12f2008-07-04 14:38:33 +03003741static __be32
3742nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003744 struct xdr_stream *xdr = &resp->xdr;
Benny Halevy695e12f2008-07-04 14:38:33 +03003745 struct svc_fh *fhp = *fhpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 unsigned int len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003747 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003749 len = fhp->fh_handle.fh_size;
3750 p = xdr_reserve_space(xdr, len + 4);
3751 if (!p)
3752 return nfserr_resource;
3753 p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base, len);
3754 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755}
3756
3757/*
3758* Including all fields other than the name, a LOCK4denied structure requires
3759* 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
3760*/
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003761static __be32
3762nfsd4_encode_lock_denied(struct xdr_stream *xdr, struct nfsd4_lock_denied *ld)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763{
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003764 struct xdr_netobj *conf = &ld->ld_owner;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003765 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003767again:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003768 p = xdr_reserve_space(xdr, 32 + XDR_LEN(conf->len));
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003769 if (!p) {
3770 /*
3771 * Don't fail to return the result just because we can't
3772 * return the conflicting open:
3773 */
3774 if (conf->len) {
Kinglong Meef98bac52014-07-07 22:10:56 +08003775 kfree(conf->data);
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003776 conf->len = 0;
3777 conf->data = NULL;
3778 goto again;
3779 }
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003780 return nfserr_resource;
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003781 }
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003782 p = xdr_encode_hyper(p, ld->ld_start);
3783 p = xdr_encode_hyper(p, ld->ld_length);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003784 *p++ = cpu_to_be32(ld->ld_type);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003785 if (conf->len) {
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003786 p = xdr_encode_opaque_fixed(p, &ld->ld_clientid, 8);
3787 p = xdr_encode_opaque(p, conf->data, conf->len);
Kinglong Meef98bac52014-07-07 22:10:56 +08003788 kfree(conf->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003790 p = xdr_encode_hyper(p, (u64)0); /* clientid */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003791 *p++ = cpu_to_be32(0); /* length of owner name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 }
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003793 return nfserr_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794}
3795
Benny Halevy695e12f2008-07-04 14:38:33 +03003796static __be32
Al Virob37ad282006-10-19 23:28:59 -07003797nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003799 struct xdr_stream *xdr = &resp->xdr;
3800
Benny Halevye2f282b2008-08-12 20:45:07 +03003801 if (!nfserr)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003802 nfserr = nfsd4_encode_stateid(xdr, &lock->lk_resp_stateid);
Benny Halevye2f282b2008-08-12 20:45:07 +03003803 else if (nfserr == nfserr_denied)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003804 nfserr = nfsd4_encode_lock_denied(xdr, &lock->lk_denied);
Kinglong Meef98bac52014-07-07 22:10:56 +08003805
Benny Halevy695e12f2008-07-04 14:38:33 +03003806 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807}
3808
Benny Halevy695e12f2008-07-04 14:38:33 +03003809static __be32
Al Virob37ad282006-10-19 23:28:59 -07003810nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003812 struct xdr_stream *xdr = &resp->xdr;
3813
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 if (nfserr == nfserr_denied)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003815 nfsd4_encode_lock_denied(xdr, &lockt->lt_denied);
Benny Halevy695e12f2008-07-04 14:38:33 +03003816 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817}
3818
Benny Halevy695e12f2008-07-04 14:38:33 +03003819static __be32
Al Virob37ad282006-10-19 23:28:59 -07003820nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003822 struct xdr_stream *xdr = &resp->xdr;
3823
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003824 return nfsd4_encode_stateid(xdr, &locku->lu_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825}
3826
3827
Benny Halevy695e12f2008-07-04 14:38:33 +03003828static __be32
Al Virob37ad282006-10-19 23:28:59 -07003829nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003831 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003832 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003834 p = xdr_reserve_space(xdr, 20);
3835 if (!p)
3836 return nfserr_resource;
3837 p = encode_cinfo(p, &link->li_cinfo);
3838 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839}
3840
3841
Benny Halevy695e12f2008-07-04 14:38:33 +03003842static __be32
Al Virob37ad282006-10-19 23:28:59 -07003843nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003845 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003846 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003848 nfserr = nfsd4_encode_stateid(xdr, &open->op_stateid);
3849 if (nfserr)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003850 return nfserr;
Kinglong Mee75976de2015-07-30 21:55:30 +08003851 p = xdr_reserve_space(xdr, 24);
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003852 if (!p)
3853 return nfserr_resource;
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04003854 p = encode_cinfo(p, &open->op_cinfo);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003855 *p++ = cpu_to_be32(open->op_rflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856
Kinglong Mee75976de2015-07-30 21:55:30 +08003857 nfserr = nfsd4_encode_bitmap(xdr, open->op_bmval[0], open->op_bmval[1],
3858 open->op_bmval[2]);
3859 if (nfserr)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003860 return nfserr;
Kinglong Mee75976de2015-07-30 21:55:30 +08003861
3862 p = xdr_reserve_space(xdr, 4);
3863 if (!p)
3864 return nfserr_resource;
3865
3866 *p++ = cpu_to_be32(open->op_delegate_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 switch (open->op_delegate_type) {
3868 case NFS4_OPEN_DELEGATE_NONE:
3869 break;
3870 case NFS4_OPEN_DELEGATE_READ:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003871 nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
3872 if (nfserr)
3873 return nfserr;
3874 p = xdr_reserve_space(xdr, 20);
3875 if (!p)
3876 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003877 *p++ = cpu_to_be32(open->op_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878
3879 /*
3880 * TODO: ACE's in delegations
3881 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003882 *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
3883 *p++ = cpu_to_be32(0);
3884 *p++ = cpu_to_be32(0);
3885 *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 break;
3887 case NFS4_OPEN_DELEGATE_WRITE:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003888 nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
3889 if (nfserr)
3890 return nfserr;
3891 p = xdr_reserve_space(xdr, 32);
3892 if (!p)
3893 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003894 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895
3896 /*
3897 * TODO: space_limit's in delegations
3898 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003899 *p++ = cpu_to_be32(NFS4_LIMIT_SIZE);
3900 *p++ = cpu_to_be32(~(u32)0);
3901 *p++ = cpu_to_be32(~(u32)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902
3903 /*
3904 * TODO: ACE's in delegations
3905 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003906 *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
3907 *p++ = cpu_to_be32(0);
3908 *p++ = cpu_to_be32(0);
3909 *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 break;
Benny Halevyd24433c2012-02-16 20:57:17 +02003911 case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
3912 switch (open->op_why_no_deleg) {
3913 case WND4_CONTENTION:
3914 case WND4_RESOURCE:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003915 p = xdr_reserve_space(xdr, 8);
3916 if (!p)
3917 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003918 *p++ = cpu_to_be32(open->op_why_no_deleg);
3919 /* deleg signaling not supported yet: */
3920 *p++ = cpu_to_be32(0);
Benny Halevyd24433c2012-02-16 20:57:17 +02003921 break;
3922 default:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003923 p = xdr_reserve_space(xdr, 4);
3924 if (!p)
3925 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003926 *p++ = cpu_to_be32(open->op_why_no_deleg);
Benny Halevyd24433c2012-02-16 20:57:17 +02003927 }
Benny Halevyd24433c2012-02-16 20:57:17 +02003928 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 default:
3930 BUG();
3931 }
3932 /* XXX save filehandle here */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003933 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934}
3935
Benny Halevy695e12f2008-07-04 14:38:33 +03003936static __be32
Al Virob37ad282006-10-19 23:28:59 -07003937nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003939 struct xdr_stream *xdr = &resp->xdr;
3940
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003941 return nfsd4_encode_stateid(xdr, &oc->oc_resp_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942}
3943
Benny Halevy695e12f2008-07-04 14:38:33 +03003944static __be32
Al Virob37ad282006-10-19 23:28:59 -07003945nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003947 struct xdr_stream *xdr = &resp->xdr;
3948
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003949 return nfsd4_encode_stateid(xdr, &od->od_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950}
3951
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003952static __be32 nfsd4_encode_splice_read(
3953 struct nfsd4_compoundres *resp,
3954 struct nfsd4_read *read,
3955 struct file *file, unsigned long maxcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956{
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003957 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003958 struct xdr_buf *buf = xdr->buf;
Chuck Lever76e54922020-11-05 10:24:19 -05003959 int status, space_left;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003960 u32 eof;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003961 __be32 nfserr;
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003962 __be32 *p = xdr->p - 2;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003963
Kinglong Meed5d5c302014-07-09 21:51:27 +08003964 /* Make sure there will be room for padding if needed */
3965 if (xdr->end - xdr->p < 1)
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003966 return nfserr_resource;
3967
Chuck Lever87c59422018-03-28 13:29:11 -04003968 nfserr = nfsd_splice_read(read->rd_rqstp, read->rd_fhp,
Trond Myklebust83a63072019-08-26 13:03:11 -04003969 file, read->rd_offset, &maxcount, &eof);
Chuck Lever87c59422018-03-28 13:29:11 -04003970 read->rd_length = maxcount;
Chuck Lever76e54922020-11-05 10:24:19 -05003971 if (nfserr)
3972 goto out_err;
3973 status = svc_encode_result_payload(read->rd_rqstp,
3974 buf->head[0].iov_len, maxcount);
3975 if (status) {
3976 nfserr = nfserrno(status);
3977 goto out_err;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003978 }
3979
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003980 *(p++) = htonl(eof);
3981 *(p++) = htonl(maxcount);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003982
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003983 buf->page_len = maxcount;
3984 buf->len += maxcount;
J. Bruce Fields15b23ef2014-09-24 16:32:34 -04003985 xdr->page_ptr += (buf->page_base + maxcount + PAGE_SIZE - 1)
3986 / PAGE_SIZE;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003987
3988 /* Use rest of head for padding and remaining ops: */
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003989 buf->tail[0].iov_base = xdr->p;
3990 buf->tail[0].iov_len = 0;
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003991 xdr->iov = buf->tail;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003992 if (maxcount&3) {
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003993 int pad = 4 - (maxcount&3);
3994
3995 *(xdr->p++) = 0;
3996
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003997 buf->tail[0].iov_base += maxcount&3;
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003998 buf->tail[0].iov_len = pad;
3999 buf->len += pad;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004000 }
4001
4002 space_left = min_t(int, (void *)xdr->end - (void *)xdr->p,
J. Bruce Fields34a78b482014-05-13 16:32:04 -04004003 buf->buflen - buf->len);
4004 buf->buflen = buf->len + space_left;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004005 xdr->end = (__be32 *)((void *)xdr->end + space_left);
4006
4007 return 0;
Chuck Lever76e54922020-11-05 10:24:19 -05004008
4009out_err:
4010 /*
4011 * nfsd_splice_actor may have already messed with the
4012 * page length; reset it so as not to confuse
4013 * xdr_truncate_encode in our caller.
4014 */
4015 buf->page_len = 0;
4016 return nfserr;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004017}
4018
4019static __be32 nfsd4_encode_readv(struct nfsd4_compoundres *resp,
4020 struct nfsd4_read *read,
4021 struct file *file, unsigned long maxcount)
4022{
4023 struct xdr_stream *xdr = &resp->xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 u32 eof;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004025 int starting_len = xdr->buf->len - 8;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004026 __be32 nfserr;
4027 __be32 tmp;
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04004028 int pad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
Anna Schumaker403217f2020-08-17 12:53:06 -04004030 read->rd_vlen = xdr_reserve_space_vec(xdr, resp->rqstp->rq_vec, maxcount);
4031 if (read->rd_vlen < 0)
4032 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033
Chuck Lever87c59422018-03-28 13:29:11 -04004034 nfserr = nfsd_readv(resp->rqstp, read->rd_fhp, file, read->rd_offset,
Trond Myklebust83a63072019-08-26 13:03:11 -04004035 resp->rqstp->rq_vec, read->rd_vlen, &maxcount,
4036 &eof);
Chuck Lever87c59422018-03-28 13:29:11 -04004037 read->rd_length = maxcount;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004038 if (nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 return nfserr;
Chuck Lever03493bc2020-06-10 10:36:42 -04004040 if (svc_encode_result_payload(resp->rqstp, starting_len + 8, maxcount))
Chuck Lever41205532020-03-02 14:45:53 -05004041 return nfserr_io;
Chuck Lever7dcf4ab2020-03-02 15:00:04 -05004042 xdr_truncate_encode(xdr, starting_len + 8 + xdr_align_size(maxcount));
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004043
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004044 tmp = htonl(eof);
4045 write_bytes_to_xdr_buf(xdr->buf, starting_len , &tmp, 4);
4046 tmp = htonl(maxcount);
4047 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp, 4);
4048
Chuck Lever7dcf4ab2020-03-02 15:00:04 -05004049 tmp = xdr_zero;
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04004050 pad = (maxcount&3) ? 4 - (maxcount&3) : 0;
4051 write_bytes_to_xdr_buf(xdr->buf, starting_len + 8 + maxcount,
Chuck Lever7dcf4ab2020-03-02 15:00:04 -05004052 &tmp, pad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 return 0;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004054
4055}
4056
4057static __be32
4058nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
4059 struct nfsd4_read *read)
4060{
4061 unsigned long maxcount;
4062 struct xdr_stream *xdr = &resp->xdr;
Jeff Layton5c4583b2019-08-18 14:18:54 -04004063 struct file *file;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004064 int starting_len = xdr->buf->len;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004065 __be32 *p;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004066
Jeff Layton5c4583b2019-08-18 14:18:54 -04004067 if (nfserr)
4068 return nfserr;
4069 file = read->rd_nf->nf_file;
4070
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004071 p = xdr_reserve_space(xdr, 8); /* eof flag and byte count */
4072 if (!p) {
Jeff Layton779fb0f2014-11-19 07:51:18 -05004073 WARN_ON_ONCE(test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags));
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004074 return nfserr_resource;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004075 }
Christoph Hellwig68e8bb02015-06-18 16:45:02 +02004076 if (resp->xdr.buf->page_len &&
4077 test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags)) {
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04004078 WARN_ON_ONCE(1);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004079 return nfserr_resource;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004080 }
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004081 xdr_commit_encode(xdr);
4082
4083 maxcount = svc_max_payload(resp->rqstp);
Christoph Hellwig68e8bb02015-06-18 16:45:02 +02004084 maxcount = min_t(unsigned long, maxcount,
4085 (xdr->buf->buflen - xdr->buf->len));
Kinglong Mee3c7aa152014-06-10 18:08:19 +08004086 maxcount = min_t(unsigned long, maxcount, read->rd_length);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004087
Christoph Hellwig68e8bb02015-06-18 16:45:02 +02004088 if (file->f_op->splice_read &&
4089 test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags))
Christoph Hellwig96bcad52015-06-18 16:45:01 +02004090 nfserr = nfsd4_encode_splice_read(resp, read, file, maxcount);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004091 else
Christoph Hellwig96bcad52015-06-18 16:45:01 +02004092 nfserr = nfsd4_encode_readv(resp, read, file, maxcount);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004093
Christoph Hellwig96bcad52015-06-18 16:45:01 +02004094 if (nfserr)
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04004095 xdr_truncate_encode(xdr, starting_len);
Christoph Hellwig96bcad52015-06-18 16:45:01 +02004096
Christoph Hellwig96bcad52015-06-18 16:45:01 +02004097 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098}
4099
Al Virob37ad282006-10-19 23:28:59 -07004100static __be32
4101nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102{
4103 int maxcount;
J. Bruce Fields476a7b12014-01-20 17:08:27 -05004104 __be32 wire_count;
4105 int zero = 0;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04004106 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05004107 int length_offset = xdr->buf->len;
Chuck Lever76e54922020-11-05 10:24:19 -05004108 int status;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004109 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04004111 p = xdr_reserve_space(xdr, 4);
4112 if (!p)
4113 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 maxcount = PAGE_SIZE;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004115
J. Bruce Fields476a7b12014-01-20 17:08:27 -05004116 p = xdr_reserve_space(xdr, maxcount);
4117 if (!p)
J. Bruce Fields4e21ac42014-03-22 15:15:11 -04004118 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 /*
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004120 * XXX: By default, vfs_readlink() will truncate symlinks if they
4121 * would overflow the buffer. Is this kosher in NFSv4? If not, one
4122 * easy fix is: if vfs_readlink() precisely fills the buffer, assume
4123 * that truncation occurred, and return NFS4ERR_RESOURCE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 */
J. Bruce Fields476a7b12014-01-20 17:08:27 -05004125 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp,
4126 (char *)p, &maxcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 if (nfserr == nfserr_isdir)
J. Bruce Fieldsd3f627c2014-02-26 17:00:38 -05004128 nfserr = nfserr_inval;
Chuck Lever76e54922020-11-05 10:24:19 -05004129 if (nfserr)
4130 goto out_err;
4131 status = svc_encode_result_payload(readlink->rl_rqstp, length_offset,
4132 maxcount);
4133 if (status) {
4134 nfserr = nfserrno(status);
4135 goto out_err;
J. Bruce Fieldsd3f627c2014-02-26 17:00:38 -05004136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137
J. Bruce Fields476a7b12014-01-20 17:08:27 -05004138 wire_count = htonl(maxcount);
4139 write_bytes_to_xdr_buf(xdr->buf, length_offset, &wire_count, 4);
Avi Kivity69bbd9c2014-06-26 17:23:12 +03004140 xdr_truncate_encode(xdr, length_offset + 4 + ALIGN(maxcount, 4));
J. Bruce Fields476a7b12014-01-20 17:08:27 -05004141 if (maxcount & 3)
4142 write_bytes_to_xdr_buf(xdr->buf, length_offset + 4 + maxcount,
4143 &zero, 4 - (maxcount&3));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 return 0;
Chuck Lever76e54922020-11-05 10:24:19 -05004145
4146out_err:
4147 xdr_truncate_encode(xdr, length_offset);
4148 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149}
4150
Al Virob37ad282006-10-19 23:28:59 -07004151static __be32
4152nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153{
4154 int maxcount;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004155 int bytes_left;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 loff_t offset;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004157 __be64 wire_offset;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04004158 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05004159 int starting_len = xdr->buf->len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004160 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004162 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
4163 if (!p)
4164 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165
4166 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004167 *p++ = cpu_to_be32(0);
4168 *p++ = cpu_to_be32(0);
J. Bruce Fields4aea24b2014-01-15 15:17:58 -05004169 resp->xdr.buf->head[0].iov_len = ((char *)resp->xdr.p)
4170 - (char *)resp->xdr.buf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
4172 /*
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004173 * Number of bytes left for directory entries allowing for the
4174 * final 8 bytes of the readdir and a following failed op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 */
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004176 bytes_left = xdr->buf->buflen - xdr->buf->len
4177 - COMPOUND_ERR_SLACK_SPACE - 8;
4178 if (bytes_left < 0) {
4179 nfserr = nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 goto err_no_verf;
4181 }
Scott Mayhew9c2ece62018-05-07 09:01:08 -04004182 maxcount = svc_max_payload(resp->rqstp);
4183 maxcount = min_t(u32, readdir->rd_maxcount, maxcount);
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004184 /*
4185 * Note the rfc defines rd_maxcount as the size of the
4186 * READDIR4resok structure, which includes the verifier above
4187 * and the 8 bytes encoded at the end of this function:
4188 */
4189 if (maxcount < 16) {
4190 nfserr = nfserr_toosmall;
4191 goto err_no_verf;
4192 }
4193 maxcount = min_t(int, maxcount-16, bytes_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04004195 /* RFC 3530 14.2.24 allows us to ignore dircount when it's 0: */
4196 if (!readdir->rd_dircount)
Scott Mayhew9c2ece62018-05-07 09:01:08 -04004197 readdir->rd_dircount = svc_max_payload(resp->rqstp);
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04004198
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004199 readdir->xdr = xdr;
4200 readdir->rd_maxcount = maxcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 readdir->common.err = 0;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004202 readdir->cookie_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
4204 offset = readdir->rd_cookie;
4205 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
4206 &offset,
4207 &readdir->common, nfsd4_encode_dirent);
4208 if (nfserr == nfs_ok &&
4209 readdir->common.err == nfserr_toosmall &&
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004210 xdr->buf->len == starting_len + 8) {
4211 /* nothing encoded; which limit did we hit?: */
4212 if (maxcount - 16 < bytes_left)
4213 /* It was the fault of rd_maxcount: */
4214 nfserr = nfserr_toosmall;
4215 else
4216 /* We ran out of buffer space: */
4217 nfserr = nfserr_resource;
4218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 if (nfserr)
4220 goto err_no_verf;
4221
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004222 if (readdir->cookie_offset) {
4223 wire_offset = cpu_to_be64(offset);
4224 write_bytes_to_xdr_buf(xdr->buf, readdir->cookie_offset,
4225 &wire_offset, 8);
4226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004228 p = xdr_reserve_space(xdr, 8);
4229 if (!p) {
4230 WARN_ON_ONCE(1);
4231 goto err_no_verf;
4232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 *p++ = 0; /* no more entries */
4234 *p++ = htonl(readdir->common.err == nfserr_eof);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235
4236 return 0;
4237err_no_verf:
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05004238 xdr_truncate_encode(xdr, starting_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 return nfserr;
4240}
4241
Benny Halevy695e12f2008-07-04 14:38:33 +03004242static __be32
Al Virob37ad282006-10-19 23:28:59 -07004243nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004245 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004246 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004248 p = xdr_reserve_space(xdr, 20);
4249 if (!p)
4250 return nfserr_resource;
4251 p = encode_cinfo(p, &remove->rm_cinfo);
4252 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253}
4254
Benny Halevy695e12f2008-07-04 14:38:33 +03004255static __be32
Al Virob37ad282006-10-19 23:28:59 -07004256nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004258 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004259 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004261 p = xdr_reserve_space(xdr, 40);
4262 if (!p)
4263 return nfserr_resource;
4264 p = encode_cinfo(p, &rename->rn_sinfo);
4265 p = encode_cinfo(p, &rename->rn_tinfo);
4266 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267}
4268
Benny Halevy695e12f2008-07-04 14:38:33 +03004269static __be32
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004270nfsd4_do_encode_secinfo(struct xdr_stream *xdr, struct svc_export *exp)
Andy Adamsondcb488a32007-07-17 04:04:51 -07004271{
Chuck Lever676e4eb2013-04-30 18:48:54 -04004272 u32 i, nflavs, supported;
J. Bruce Fields4796f452007-07-17 04:04:51 -07004273 struct exp_flavor_info *flavs;
4274 struct exp_flavor_info def_flavs[2];
Chuck Lever676e4eb2013-04-30 18:48:54 -04004275 __be32 *p, *flavorsp;
4276 static bool report = true;
Andy Adamsondcb488a32007-07-17 04:04:51 -07004277
J. Bruce Fields4796f452007-07-17 04:04:51 -07004278 if (exp->ex_nflavors) {
4279 flavs = exp->ex_flavors;
4280 nflavs = exp->ex_nflavors;
4281 } else { /* Handling of some defaults in absence of real secinfo: */
4282 flavs = def_flavs;
4283 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
4284 nflavs = 2;
4285 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
4286 flavs[1].pseudoflavor = RPC_AUTH_NULL;
4287 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
4288 nflavs = 1;
4289 flavs[0].pseudoflavor
4290 = svcauth_gss_flavor(exp->ex_client);
4291 } else {
4292 nflavs = 1;
4293 flavs[0].pseudoflavor
4294 = exp->ex_client->flavour->flavour;
4295 }
4296 }
4297
Chuck Lever676e4eb2013-04-30 18:48:54 -04004298 supported = 0;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004299 p = xdr_reserve_space(xdr, 4);
4300 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004301 return nfserr_resource;
Chuck Lever676e4eb2013-04-30 18:48:54 -04004302 flavorsp = p++; /* to be backfilled later */
Chuck Lever676e4eb2013-04-30 18:48:54 -04004303
J. Bruce Fields4796f452007-07-17 04:04:51 -07004304 for (i = 0; i < nflavs; i++) {
Chuck Lever676e4eb2013-04-30 18:48:54 -04004305 rpc_authflavor_t pf = flavs[i].pseudoflavor;
Chuck Levera77c8062013-03-16 15:55:10 -04004306 struct rpcsec_gss_info info;
Andy Adamsondcb488a32007-07-17 04:04:51 -07004307
Chuck Lever676e4eb2013-04-30 18:48:54 -04004308 if (rpcauth_get_gssinfo(pf, &info) == 0) {
4309 supported++;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004310 p = xdr_reserve_space(xdr, 4 + 4 +
4311 XDR_LEN(info.oid.len) + 4 + 4);
4312 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004313 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004314 *p++ = cpu_to_be32(RPC_AUTH_GSS);
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004315 p = xdr_encode_opaque(p, info.oid.data, info.oid.len);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004316 *p++ = cpu_to_be32(info.qop);
4317 *p++ = cpu_to_be32(info.service);
Chuck Lever676e4eb2013-04-30 18:48:54 -04004318 } else if (pf < RPC_AUTH_MAXFLAVOR) {
4319 supported++;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004320 p = xdr_reserve_space(xdr, 4);
4321 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004322 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004323 *p++ = cpu_to_be32(pf);
Chuck Lever676e4eb2013-04-30 18:48:54 -04004324 } else {
4325 if (report)
4326 pr_warn("NFS: SECINFO: security flavor %u "
4327 "is not supported\n", pf);
Andy Adamsondcb488a32007-07-17 04:04:51 -07004328 }
4329 }
Chuck Levera77c8062013-03-16 15:55:10 -04004330
Chuck Lever676e4eb2013-04-30 18:48:54 -04004331 if (nflavs != supported)
4332 report = false;
4333 *flavorsp = htonl(supported);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004334 return 0;
Andy Adamsondcb488a32007-07-17 04:04:51 -07004335}
4336
Mi Jinlong22b6dee2010-12-27 14:29:57 +08004337static __be32
4338nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
4339 struct nfsd4_secinfo *secinfo)
4340{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004341 struct xdr_stream *xdr = &resp->xdr;
4342
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004343 return nfsd4_do_encode_secinfo(xdr, secinfo->si_exp);
Mi Jinlong22b6dee2010-12-27 14:29:57 +08004344}
4345
4346static __be32
4347nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
4348 struct nfsd4_secinfo_no_name *secinfo)
4349{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004350 struct xdr_stream *xdr = &resp->xdr;
4351
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004352 return nfsd4_do_encode_secinfo(xdr, secinfo->sin_exp);
Mi Jinlong22b6dee2010-12-27 14:29:57 +08004353}
4354
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355/*
4356 * The SETATTR encode routine is special -- it always encodes a bitmap,
4357 * regardless of the error status.
4358 */
Benny Halevy695e12f2008-07-04 14:38:33 +03004359static __be32
Al Virob37ad282006-10-19 23:28:59 -07004360nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004362 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004363 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004365 p = xdr_reserve_space(xdr, 16);
4366 if (!p)
4367 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 if (nfserr) {
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004369 *p++ = cpu_to_be32(3);
4370 *p++ = cpu_to_be32(0);
4371 *p++ = cpu_to_be32(0);
4372 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 }
4374 else {
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004375 *p++ = cpu_to_be32(3);
4376 *p++ = cpu_to_be32(setattr->sa_bmval[0]);
4377 *p++ = cpu_to_be32(setattr->sa_bmval[1]);
4378 *p++ = cpu_to_be32(setattr->sa_bmval[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 }
Benny Halevy695e12f2008-07-04 14:38:33 +03004380 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381}
4382
Benny Halevy695e12f2008-07-04 14:38:33 +03004383static __be32
Al Virob37ad282006-10-19 23:28:59 -07004384nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004386 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004387 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
4389 if (!nfserr) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004390 p = xdr_reserve_space(xdr, 8 + NFS4_VERIFIER_SIZE);
4391 if (!p)
4392 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004393 p = xdr_encode_opaque_fixed(p, &scd->se_clientid, 8);
4394 p = xdr_encode_opaque_fixed(p, &scd->se_confirm,
4395 NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 }
4397 else if (nfserr == nfserr_clid_inuse) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004398 p = xdr_reserve_space(xdr, 8);
4399 if (!p)
4400 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004401 *p++ = cpu_to_be32(0);
4402 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 }
Benny Halevy695e12f2008-07-04 14:38:33 +03004404 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405}
4406
Benny Halevy695e12f2008-07-04 14:38:33 +03004407static __be32
Al Virob37ad282006-10-19 23:28:59 -07004408nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004410 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004411 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004413 p = xdr_reserve_space(xdr, 16);
4414 if (!p)
4415 return nfserr_resource;
4416 *p++ = cpu_to_be32(write->wr_bytes_written);
4417 *p++ = cpu_to_be32(write->wr_how_written);
4418 p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
4419 NFS4_VERIFIER_SIZE);
4420 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421}
4422
Benny Halevy695e12f2008-07-04 14:38:33 +03004423static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004424nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03004425 struct nfsd4_exchange_id *exid)
4426{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004427 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004428 __be32 *p;
Andy Adamson0733d212009-04-03 08:28:01 +03004429 char *major_id;
4430 char *server_scope;
4431 int major_id_sz;
4432 int server_scope_sz;
4433 uint64_t minor_id = 0;
Scott Mayhew7627d7d2020-02-19 15:52:15 -05004434 struct nfsd_net *nn = net_generic(SVC_NET(resp->rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03004435
Scott Mayhew7627d7d2020-02-19 15:52:15 -05004436 major_id = nn->nfsd_name;
4437 major_id_sz = strlen(nn->nfsd_name);
4438 server_scope = nn->nfsd_name;
4439 server_scope_sz = strlen(nn->nfsd_name);
Andy Adamson0733d212009-04-03 08:28:01 +03004440
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004441 p = xdr_reserve_space(xdr,
Andy Adamson0733d212009-04-03 08:28:01 +03004442 8 /* eir_clientid */ +
4443 4 /* eir_sequenceid */ +
4444 4 /* eir_flags */ +
Kinglong Meea8bb84b2013-12-10 15:24:36 +08004445 4 /* spr_how */);
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004446 if (!p)
4447 return nfserr_resource;
Andy Adamson0733d212009-04-03 08:28:01 +03004448
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004449 p = xdr_encode_opaque_fixed(p, &exid->clientid, 8);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004450 *p++ = cpu_to_be32(exid->seqid);
4451 *p++ = cpu_to_be32(exid->flags);
Andy Adamson0733d212009-04-03 08:28:01 +03004452
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004453 *p++ = cpu_to_be32(exid->spa_how);
Kinglong Meea8bb84b2013-12-10 15:24:36 +08004454
J. Bruce Fields57266a62013-04-13 14:27:29 -04004455 switch (exid->spa_how) {
4456 case SP4_NONE:
4457 break;
4458 case SP4_MACH_CRED:
4459 /* spo_must_enforce bitmap: */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004460 nfserr = nfsd4_encode_bitmap(xdr,
Andrew Elbleed941642016-06-15 12:52:09 -04004461 exid->spo_must_enforce[0],
4462 exid->spo_must_enforce[1],
4463 exid->spo_must_enforce[2]);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004464 if (nfserr)
4465 return nfserr;
Andrew Elbleed941642016-06-15 12:52:09 -04004466 /* spo_must_allow bitmap: */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004467 nfserr = nfsd4_encode_bitmap(xdr,
Andrew Elbleed941642016-06-15 12:52:09 -04004468 exid->spo_must_allow[0],
4469 exid->spo_must_allow[1],
4470 exid->spo_must_allow[2]);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004471 if (nfserr)
4472 return nfserr;
J. Bruce Fields57266a62013-04-13 14:27:29 -04004473 break;
4474 default:
4475 WARN_ON_ONCE(1);
4476 }
Andy Adamson0733d212009-04-03 08:28:01 +03004477
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004478 p = xdr_reserve_space(xdr,
Kinglong Meea8bb84b2013-12-10 15:24:36 +08004479 8 /* so_minor_id */ +
4480 4 /* so_major_id.len */ +
4481 (XDR_QUADLEN(major_id_sz) * 4) +
4482 4 /* eir_server_scope.len */ +
4483 (XDR_QUADLEN(server_scope_sz) * 4) +
4484 4 /* eir_server_impl_id.count (0) */);
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004485 if (!p)
4486 return nfserr_resource;
Kinglong Meea8bb84b2013-12-10 15:24:36 +08004487
Andy Adamson0733d212009-04-03 08:28:01 +03004488 /* The server_owner struct */
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04004489 p = xdr_encode_hyper(p, minor_id); /* Minor id */
Andy Adamson0733d212009-04-03 08:28:01 +03004490 /* major id */
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004491 p = xdr_encode_opaque(p, major_id, major_id_sz);
Andy Adamson0733d212009-04-03 08:28:01 +03004492
4493 /* Server scope */
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004494 p = xdr_encode_opaque(p, server_scope, server_scope_sz);
Andy Adamson0733d212009-04-03 08:28:01 +03004495
4496 /* Implementation id */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004497 *p++ = cpu_to_be32(0); /* zero length nfs_impl_id4 array */
Andy Adamson0733d212009-04-03 08:28:01 +03004498 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03004499}
4500
4501static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004502nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03004503 struct nfsd4_create_session *sess)
4504{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004505 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004506 __be32 *p;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004507
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004508 p = xdr_reserve_space(xdr, 24);
4509 if (!p)
4510 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004511 p = xdr_encode_opaque_fixed(p, sess->sessionid.data,
4512 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004513 *p++ = cpu_to_be32(sess->seqid);
4514 *p++ = cpu_to_be32(sess->flags);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004515
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004516 p = xdr_reserve_space(xdr, 28);
4517 if (!p)
4518 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004519 *p++ = cpu_to_be32(0); /* headerpadsz */
4520 *p++ = cpu_to_be32(sess->fore_channel.maxreq_sz);
4521 *p++ = cpu_to_be32(sess->fore_channel.maxresp_sz);
4522 *p++ = cpu_to_be32(sess->fore_channel.maxresp_cached);
4523 *p++ = cpu_to_be32(sess->fore_channel.maxops);
4524 *p++ = cpu_to_be32(sess->fore_channel.maxreqs);
4525 *p++ = cpu_to_be32(sess->fore_channel.nr_rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004526
4527 if (sess->fore_channel.nr_rdma_attrs) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004528 p = xdr_reserve_space(xdr, 4);
4529 if (!p)
4530 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004531 *p++ = cpu_to_be32(sess->fore_channel.rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004532 }
4533
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004534 p = xdr_reserve_space(xdr, 28);
4535 if (!p)
4536 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004537 *p++ = cpu_to_be32(0); /* headerpadsz */
4538 *p++ = cpu_to_be32(sess->back_channel.maxreq_sz);
4539 *p++ = cpu_to_be32(sess->back_channel.maxresp_sz);
4540 *p++ = cpu_to_be32(sess->back_channel.maxresp_cached);
4541 *p++ = cpu_to_be32(sess->back_channel.maxops);
4542 *p++ = cpu_to_be32(sess->back_channel.maxreqs);
4543 *p++ = cpu_to_be32(sess->back_channel.nr_rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004544
4545 if (sess->back_channel.nr_rdma_attrs) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004546 p = xdr_reserve_space(xdr, 4);
4547 if (!p)
4548 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004549 *p++ = cpu_to_be32(sess->back_channel.rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004550 }
4551 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03004552}
4553
4554static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004555nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03004556 struct nfsd4_sequence *seq)
4557{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004558 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004559 __be32 *p;
Benny Halevyb85d4c02009-04-03 08:28:08 +03004560
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004561 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 20);
4562 if (!p)
4563 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004564 p = xdr_encode_opaque_fixed(p, seq->sessionid.data,
4565 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004566 *p++ = cpu_to_be32(seq->seqid);
4567 *p++ = cpu_to_be32(seq->slotid);
J. Bruce Fieldsb7d7ca32011-08-31 15:39:30 -04004568 /* Note slotid's are numbered from zero: */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004569 *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_highest_slotid */
4570 *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_target_highest_slotid */
4571 *p++ = cpu_to_be32(seq->status_flags);
Benny Halevyb85d4c02009-04-03 08:28:08 +03004572
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04004573 resp->cstate.data_offset = xdr->buf->len; /* DRC cache data pointer */
Benny Halevyb85d4c02009-04-03 08:28:08 +03004574 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03004575}
4576
J. Bruce Fields2355c592012-04-25 16:56:22 -04004577static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004578nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
Bryan Schumaker17456802011-07-13 10:50:48 -04004579 struct nfsd4_test_stateid *test_stateid)
4580{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004581 struct xdr_stream *xdr = &resp->xdr;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004582 struct nfsd4_test_stateid_id *stateid, *next;
Bryan Schumaker17456802011-07-13 10:50:48 -04004583 __be32 *p;
Bryan Schumaker17456802011-07-13 10:50:48 -04004584
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004585 p = xdr_reserve_space(xdr, 4 + (4 * test_stateid->ts_num_ids));
4586 if (!p)
4587 return nfserr_resource;
Bryan Schumaker17456802011-07-13 10:50:48 -04004588 *p++ = htonl(test_stateid->ts_num_ids);
Bryan Schumaker17456802011-07-13 10:50:48 -04004589
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004590 list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
Al Viro02f5fde2012-04-13 00:10:34 -04004591 *p++ = stateid->ts_id_status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004592 }
Bryan Schumaker17456802011-07-13 10:50:48 -04004593
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004594 return 0;
Bryan Schumaker17456802011-07-13 10:50:48 -04004595}
4596
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004597#ifdef CONFIG_NFSD_PNFS
4598static __be32
4599nfsd4_encode_getdeviceinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
4600 struct nfsd4_getdeviceinfo *gdev)
4601{
4602 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004603 const struct nfsd4_layout_ops *ops;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004604 u32 starting_len = xdr->buf->len, needed_len;
4605 __be32 *p;
4606
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004607 p = xdr_reserve_space(xdr, 4);
4608 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004609 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004610
4611 *p++ = cpu_to_be32(gdev->gd_layout_type);
4612
4613 /* If maxcount is 0 then just update notifications */
4614 if (gdev->gd_maxcount != 0) {
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004615 ops = nfsd4_layout_ops[gdev->gd_layout_type];
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004616 nfserr = ops->encode_getdeviceinfo(xdr, gdev);
4617 if (nfserr) {
4618 /*
4619 * We don't bother to burden the layout drivers with
4620 * enforcing gd_maxcount, just tell the client to
4621 * come back with a bigger buffer if it's not enough.
4622 */
4623 if (xdr->buf->len + 4 > gdev->gd_maxcount)
4624 goto toosmall;
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004625 return nfserr;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004626 }
4627 }
4628
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004629 if (gdev->gd_notify_types) {
4630 p = xdr_reserve_space(xdr, 4 + 4);
4631 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004632 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004633 *p++ = cpu_to_be32(1); /* bitmap length */
4634 *p++ = cpu_to_be32(gdev->gd_notify_types);
4635 } else {
4636 p = xdr_reserve_space(xdr, 4);
4637 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004638 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004639 *p++ = 0;
4640 }
4641
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004642 return 0;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004643toosmall:
4644 dprintk("%s: maxcount too small\n", __func__);
4645 needed_len = xdr->buf->len + 4 /* notifications */;
4646 xdr_truncate_encode(xdr, starting_len);
4647 p = xdr_reserve_space(xdr, 4);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004648 if (!p)
4649 return nfserr_resource;
4650 *p++ = cpu_to_be32(needed_len);
4651 return nfserr_toosmall;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004652}
4653
4654static __be32
4655nfsd4_encode_layoutget(struct nfsd4_compoundres *resp, __be32 nfserr,
4656 struct nfsd4_layoutget *lgp)
4657{
4658 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004659 const struct nfsd4_layout_ops *ops;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004660 __be32 *p;
4661
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004662 p = xdr_reserve_space(xdr, 36 + sizeof(stateid_opaque_t));
4663 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004664 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004665
4666 *p++ = cpu_to_be32(1); /* we always set return-on-close */
4667 *p++ = cpu_to_be32(lgp->lg_sid.si_generation);
4668 p = xdr_encode_opaque_fixed(p, &lgp->lg_sid.si_opaque,
4669 sizeof(stateid_opaque_t));
4670
4671 *p++ = cpu_to_be32(1); /* we always return a single layout */
4672 p = xdr_encode_hyper(p, lgp->lg_seg.offset);
4673 p = xdr_encode_hyper(p, lgp->lg_seg.length);
4674 *p++ = cpu_to_be32(lgp->lg_seg.iomode);
4675 *p++ = cpu_to_be32(lgp->lg_layout_type);
4676
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004677 ops = nfsd4_layout_ops[lgp->lg_layout_type];
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004678 return ops->encode_layoutget(xdr, lgp);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004679}
4680
4681static __be32
4682nfsd4_encode_layoutcommit(struct nfsd4_compoundres *resp, __be32 nfserr,
4683 struct nfsd4_layoutcommit *lcp)
4684{
4685 struct xdr_stream *xdr = &resp->xdr;
4686 __be32 *p;
4687
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004688 p = xdr_reserve_space(xdr, 4);
4689 if (!p)
4690 return nfserr_resource;
4691 *p++ = cpu_to_be32(lcp->lc_size_chg);
4692 if (lcp->lc_size_chg) {
4693 p = xdr_reserve_space(xdr, 8);
4694 if (!p)
4695 return nfserr_resource;
4696 p = xdr_encode_hyper(p, lcp->lc_newsize);
4697 }
4698
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004699 return 0;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004700}
4701
4702static __be32
4703nfsd4_encode_layoutreturn(struct nfsd4_compoundres *resp, __be32 nfserr,
4704 struct nfsd4_layoutreturn *lrp)
4705{
4706 struct xdr_stream *xdr = &resp->xdr;
4707 __be32 *p;
4708
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004709 p = xdr_reserve_space(xdr, 4);
4710 if (!p)
4711 return nfserr_resource;
4712 *p++ = cpu_to_be32(lrp->lrs_present);
4713 if (lrp->lrs_present)
Kinglong Mee376675d2015-03-22 22:17:10 +08004714 return nfsd4_encode_stateid(xdr, &lrp->lr_sid);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004715 return 0;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004716}
4717#endif /* CONFIG_NFSD_PNFS */
4718
Andy Adamson2db134e2009-04-03 08:27:55 +03004719static __be32
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004720nfsd42_encode_write_res(struct nfsd4_compoundres *resp,
4721 struct nfsd42_write_res *write, bool sync)
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004722{
4723 __be32 *p;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004724 p = xdr_reserve_space(&resp->xdr, 4);
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004725 if (!p)
4726 return nfserr_resource;
4727
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004728 if (sync)
4729 *p++ = cpu_to_be32(0);
4730 else {
4731 __be32 nfserr;
4732 *p++ = cpu_to_be32(1);
4733 nfserr = nfsd4_encode_stateid(&resp->xdr, &write->cb_stateid);
4734 if (nfserr)
4735 return nfserr;
4736 }
4737 p = xdr_reserve_space(&resp->xdr, 8 + 4 + NFS4_VERIFIER_SIZE);
4738 if (!p)
4739 return nfserr_resource;
4740
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004741 p = xdr_encode_hyper(p, write->wr_bytes_written);
4742 *p++ = cpu_to_be32(write->wr_stable_how);
4743 p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
4744 NFS4_VERIFIER_SIZE);
4745 return nfs_ok;
4746}
4747
4748static __be32
Olga Kornievskaia51911862019-08-08 11:14:59 -04004749nfsd42_encode_nl4_server(struct nfsd4_compoundres *resp, struct nl4_server *ns)
4750{
4751 struct xdr_stream *xdr = &resp->xdr;
4752 struct nfs42_netaddr *addr;
4753 __be32 *p;
4754
4755 p = xdr_reserve_space(xdr, 4);
4756 *p++ = cpu_to_be32(ns->nl4_type);
4757
4758 switch (ns->nl4_type) {
4759 case NL4_NETADDR:
4760 addr = &ns->u.nl4_addr;
4761
4762 /* netid_len, netid, uaddr_len, uaddr (port included
4763 * in RPCBIND_MAXUADDRLEN)
4764 */
4765 p = xdr_reserve_space(xdr,
4766 4 /* netid len */ +
4767 (XDR_QUADLEN(addr->netid_len) * 4) +
4768 4 /* uaddr len */ +
4769 (XDR_QUADLEN(addr->addr_len) * 4));
4770 if (!p)
4771 return nfserr_resource;
4772
4773 *p++ = cpu_to_be32(addr->netid_len);
4774 p = xdr_encode_opaque_fixed(p, addr->netid,
4775 addr->netid_len);
4776 *p++ = cpu_to_be32(addr->addr_len);
4777 p = xdr_encode_opaque_fixed(p, addr->addr,
4778 addr->addr_len);
4779 break;
4780 default:
4781 WARN_ON_ONCE(ns->nl4_type != NL4_NETADDR);
4782 return nfserr_inval;
4783 }
4784
4785 return 0;
4786}
4787
4788static __be32
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004789nfsd4_encode_copy(struct nfsd4_compoundres *resp, __be32 nfserr,
4790 struct nfsd4_copy *copy)
4791{
4792 __be32 *p;
4793
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004794 nfserr = nfsd42_encode_write_res(resp, &copy->cp_res,
4795 copy->cp_synchronous);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004796 if (nfserr)
4797 return nfserr;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004798
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004799 p = xdr_reserve_space(&resp->xdr, 4 + 4);
J. Bruce Fieldsedcc8452018-03-07 15:37:35 -05004800 *p++ = xdr_one; /* cr_consecutive */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004801 *p++ = cpu_to_be32(copy->cp_synchronous);
4802 return 0;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004803}
4804
4805static __be32
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04004806nfsd4_encode_offload_status(struct nfsd4_compoundres *resp, __be32 nfserr,
4807 struct nfsd4_offload_status *os)
4808{
4809 struct xdr_stream *xdr = &resp->xdr;
4810 __be32 *p;
4811
4812 p = xdr_reserve_space(xdr, 8 + 4);
4813 if (!p)
4814 return nfserr_resource;
4815 p = xdr_encode_hyper(p, os->count);
4816 *p++ = cpu_to_be32(0);
Anna Schumaker528b8492020-09-28 13:08:58 -04004817 return nfserr;
4818}
4819
4820static __be32
4821nfsd4_encode_read_plus_data(struct nfsd4_compoundres *resp,
4822 struct nfsd4_read *read,
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004823 unsigned long *maxcount, u32 *eof,
4824 loff_t *pos)
Anna Schumaker528b8492020-09-28 13:08:58 -04004825{
4826 struct xdr_stream *xdr = &resp->xdr;
4827 struct file *file = read->rd_nf->nf_file;
4828 int starting_len = xdr->buf->len;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004829 loff_t hole_pos;
Anna Schumaker528b8492020-09-28 13:08:58 -04004830 __be32 nfserr;
4831 __be32 *p, tmp;
4832 __be64 tmp64;
4833
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004834 hole_pos = pos ? *pos : vfs_llseek(file, read->rd_offset, SEEK_HOLE);
Anna Schumaker2db27992020-09-28 13:08:59 -04004835 if (hole_pos > read->rd_offset)
Anna Schumaker278765e2020-09-28 13:09:00 -04004836 *maxcount = min_t(unsigned long, *maxcount, hole_pos - read->rd_offset);
4837 *maxcount = min_t(unsigned long, *maxcount, (xdr->buf->buflen - xdr->buf->len));
Anna Schumaker528b8492020-09-28 13:08:58 -04004838
4839 /* Content type, offset, byte count */
4840 p = xdr_reserve_space(xdr, 4 + 8 + 4);
4841 if (!p)
4842 return nfserr_resource;
4843
Anna Schumaker278765e2020-09-28 13:09:00 -04004844 read->rd_vlen = xdr_reserve_space_vec(xdr, resp->rqstp->rq_vec, *maxcount);
Anna Schumaker528b8492020-09-28 13:08:58 -04004845 if (read->rd_vlen < 0)
4846 return nfserr_resource;
4847
4848 nfserr = nfsd_readv(resp->rqstp, read->rd_fhp, file, read->rd_offset,
Anna Schumaker278765e2020-09-28 13:09:00 -04004849 resp->rqstp->rq_vec, read->rd_vlen, maxcount, eof);
Anna Schumaker528b8492020-09-28 13:08:58 -04004850 if (nfserr)
4851 return nfserr;
4852
4853 tmp = htonl(NFS4_CONTENT_DATA);
4854 write_bytes_to_xdr_buf(xdr->buf, starting_len, &tmp, 4);
4855 tmp64 = cpu_to_be64(read->rd_offset);
4856 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp64, 8);
Anna Schumaker278765e2020-09-28 13:09:00 -04004857 tmp = htonl(*maxcount);
Anna Schumaker528b8492020-09-28 13:08:58 -04004858 write_bytes_to_xdr_buf(xdr->buf, starting_len + 12, &tmp, 4);
4859 return nfs_ok;
4860}
4861
4862static __be32
Anna Schumaker2db27992020-09-28 13:08:59 -04004863nfsd4_encode_read_plus_hole(struct nfsd4_compoundres *resp,
4864 struct nfsd4_read *read,
Anna Schumaker278765e2020-09-28 13:09:00 -04004865 unsigned long *maxcount, u32 *eof)
Anna Schumaker2db27992020-09-28 13:08:59 -04004866{
4867 struct file *file = read->rd_nf->nf_file;
Anna Schumaker278765e2020-09-28 13:09:00 -04004868 loff_t data_pos = vfs_llseek(file, read->rd_offset, SEEK_DATA);
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004869 loff_t f_size = i_size_read(file_inode(file));
Anna Schumaker278765e2020-09-28 13:09:00 -04004870 unsigned long count;
Anna Schumaker2db27992020-09-28 13:08:59 -04004871 __be32 *p;
4872
Anna Schumaker278765e2020-09-28 13:09:00 -04004873 if (data_pos == -ENXIO)
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004874 data_pos = f_size;
4875 else if (data_pos <= read->rd_offset || (data_pos < f_size && data_pos % PAGE_SIZE))
4876 return nfsd4_encode_read_plus_data(resp, read, maxcount, eof, &f_size);
Anna Schumaker278765e2020-09-28 13:09:00 -04004877 count = data_pos - read->rd_offset;
4878
Anna Schumaker2db27992020-09-28 13:08:59 -04004879 /* Content type, offset, byte count */
4880 p = xdr_reserve_space(&resp->xdr, 4 + 8 + 8);
4881 if (!p)
4882 return nfserr_resource;
4883
4884 *p++ = htonl(NFS4_CONTENT_HOLE);
4885 p = xdr_encode_hyper(p, read->rd_offset);
Anna Schumaker278765e2020-09-28 13:09:00 -04004886 p = xdr_encode_hyper(p, count);
Anna Schumaker2db27992020-09-28 13:08:59 -04004887
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004888 *eof = (read->rd_offset + count) >= f_size;
Anna Schumaker278765e2020-09-28 13:09:00 -04004889 *maxcount = min_t(unsigned long, count, *maxcount);
Anna Schumaker2db27992020-09-28 13:08:59 -04004890 return nfs_ok;
4891}
4892
4893static __be32
Anna Schumaker528b8492020-09-28 13:08:58 -04004894nfsd4_encode_read_plus(struct nfsd4_compoundres *resp, __be32 nfserr,
4895 struct nfsd4_read *read)
4896{
Anna Schumaker278765e2020-09-28 13:09:00 -04004897 unsigned long maxcount, count;
Anna Schumaker528b8492020-09-28 13:08:58 -04004898 struct xdr_stream *xdr = &resp->xdr;
4899 struct file *file;
4900 int starting_len = xdr->buf->len;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004901 int last_segment = xdr->buf->len;
Anna Schumaker528b8492020-09-28 13:08:58 -04004902 int segments = 0;
4903 __be32 *p, tmp;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004904 bool is_data;
Anna Schumaker2db27992020-09-28 13:08:59 -04004905 loff_t pos;
Anna Schumaker528b8492020-09-28 13:08:58 -04004906 u32 eof;
4907
4908 if (nfserr)
4909 return nfserr;
4910 file = read->rd_nf->nf_file;
4911
4912 /* eof flag, segment count */
4913 p = xdr_reserve_space(xdr, 4 + 4);
4914 if (!p)
4915 return nfserr_resource;
4916 xdr_commit_encode(xdr);
4917
4918 maxcount = svc_max_payload(resp->rqstp);
4919 maxcount = min_t(unsigned long, maxcount,
4920 (xdr->buf->buflen - xdr->buf->len));
4921 maxcount = min_t(unsigned long, maxcount, read->rd_length);
Anna Schumaker278765e2020-09-28 13:09:00 -04004922 count = maxcount;
Anna Schumaker528b8492020-09-28 13:08:58 -04004923
4924 eof = read->rd_offset >= i_size_read(file_inode(file));
Anna Schumaker2db27992020-09-28 13:08:59 -04004925 if (eof)
4926 goto out;
4927
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004928 pos = vfs_llseek(file, read->rd_offset, SEEK_HOLE);
4929 is_data = pos > read->rd_offset;
Anna Schumaker2db27992020-09-28 13:08:59 -04004930
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004931 while (count > 0 && !eof) {
Anna Schumaker278765e2020-09-28 13:09:00 -04004932 maxcount = count;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004933 if (is_data)
4934 nfserr = nfsd4_encode_read_plus_data(resp, read, &maxcount, &eof,
4935 segments == 0 ? &pos : NULL);
4936 else
4937 nfserr = nfsd4_encode_read_plus_hole(resp, read, &maxcount, &eof);
Anna Schumaker278765e2020-09-28 13:09:00 -04004938 if (nfserr)
4939 goto out;
4940 count -= maxcount;
4941 read->rd_offset += maxcount;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004942 is_data = !is_data;
4943 last_segment = xdr->buf->len;
Anna Schumaker528b8492020-09-28 13:08:58 -04004944 segments++;
4945 }
4946
Anna Schumaker2db27992020-09-28 13:08:59 -04004947out:
Anna Schumaker278765e2020-09-28 13:09:00 -04004948 if (nfserr && segments == 0)
Anna Schumaker528b8492020-09-28 13:08:58 -04004949 xdr_truncate_encode(xdr, starting_len);
4950 else {
4951 tmp = htonl(eof);
4952 write_bytes_to_xdr_buf(xdr->buf, starting_len, &tmp, 4);
4953 tmp = htonl(segments);
4954 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp, 4);
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004955 if (nfserr) {
4956 xdr_truncate_encode(xdr, last_segment);
4957 nfserr = nfs_ok;
4958 }
Anna Schumaker528b8492020-09-28 13:08:58 -04004959 }
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04004960
4961 return nfserr;
4962}
4963
4964static __be32
Olga Kornievskaia51911862019-08-08 11:14:59 -04004965nfsd4_encode_copy_notify(struct nfsd4_compoundres *resp, __be32 nfserr,
4966 struct nfsd4_copy_notify *cn)
4967{
4968 struct xdr_stream *xdr = &resp->xdr;
4969 __be32 *p;
4970
4971 if (nfserr)
4972 return nfserr;
4973
4974 /* 8 sec, 4 nsec */
4975 p = xdr_reserve_space(xdr, 12);
4976 if (!p)
4977 return nfserr_resource;
4978
4979 /* cnr_lease_time */
4980 p = xdr_encode_hyper(p, cn->cpn_sec);
4981 *p++ = cpu_to_be32(cn->cpn_nsec);
4982
4983 /* cnr_stateid */
4984 nfserr = nfsd4_encode_stateid(xdr, &cn->cpn_cnr_stateid);
4985 if (nfserr)
4986 return nfserr;
4987
4988 /* cnr_src.nl_nsvr */
4989 p = xdr_reserve_space(xdr, 4);
4990 if (!p)
4991 return nfserr_resource;
4992
4993 *p++ = cpu_to_be32(1);
4994
4995 return nfsd42_encode_nl4_server(resp, &cn->cpn_src);
4996}
4997
4998static __be32
Anna Schumaker24bab492014-09-26 13:58:27 -04004999nfsd4_encode_seek(struct nfsd4_compoundres *resp, __be32 nfserr,
5000 struct nfsd4_seek *seek)
5001{
5002 __be32 *p;
5003
Anna Schumaker24bab492014-09-26 13:58:27 -04005004 p = xdr_reserve_space(&resp->xdr, 4 + 8);
5005 *p++ = cpu_to_be32(seek->seek_eof);
5006 p = xdr_encode_hyper(p, seek->seek_pos);
5007
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04005008 return 0;
Anna Schumaker24bab492014-09-26 13:58:27 -04005009}
5010
5011static __be32
Benny Halevy695e12f2008-07-04 14:38:33 +03005012nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
5013{
5014 return nfserr;
5015}
5016
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005017/*
5018 * Encode kmalloc-ed buffer in to XDR stream.
5019 */
Chuck Leverb9a49232020-09-11 14:47:53 -04005020static __be32
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005021nfsd4_vbuf_to_stream(struct xdr_stream *xdr, char *buf, u32 buflen)
5022{
5023 u32 cplen;
5024 __be32 *p;
5025
5026 cplen = min_t(unsigned long, buflen,
5027 ((void *)xdr->end - (void *)xdr->p));
5028 p = xdr_reserve_space(xdr, cplen);
5029 if (!p)
5030 return nfserr_resource;
5031
5032 memcpy(p, buf, cplen);
5033 buf += cplen;
5034 buflen -= cplen;
5035
5036 while (buflen) {
5037 cplen = min_t(u32, buflen, PAGE_SIZE);
5038 p = xdr_reserve_space(xdr, cplen);
5039 if (!p)
5040 return nfserr_resource;
5041
5042 memcpy(p, buf, cplen);
5043
5044 if (cplen < PAGE_SIZE) {
5045 /*
5046 * We're done, with a length that wasn't page
5047 * aligned, so possibly not word aligned. Pad
5048 * any trailing bytes with 0.
5049 */
5050 xdr_encode_opaque_fixed(p, NULL, cplen);
5051 break;
5052 }
5053
5054 buflen -= PAGE_SIZE;
5055 buf += PAGE_SIZE;
5056 }
5057
5058 return 0;
5059}
5060
5061static __be32
5062nfsd4_encode_getxattr(struct nfsd4_compoundres *resp, __be32 nfserr,
5063 struct nfsd4_getxattr *getxattr)
5064{
5065 struct xdr_stream *xdr = &resp->xdr;
5066 __be32 *p, err;
5067
5068 p = xdr_reserve_space(xdr, 4);
5069 if (!p)
5070 return nfserr_resource;
5071
5072 *p = cpu_to_be32(getxattr->getxa_len);
5073
5074 if (getxattr->getxa_len == 0)
5075 return 0;
5076
5077 err = nfsd4_vbuf_to_stream(xdr, getxattr->getxa_buf,
5078 getxattr->getxa_len);
5079
5080 kvfree(getxattr->getxa_buf);
5081
5082 return err;
5083}
5084
5085static __be32
5086nfsd4_encode_setxattr(struct nfsd4_compoundres *resp, __be32 nfserr,
5087 struct nfsd4_setxattr *setxattr)
5088{
5089 struct xdr_stream *xdr = &resp->xdr;
5090 __be32 *p;
5091
5092 p = xdr_reserve_space(xdr, 20);
5093 if (!p)
5094 return nfserr_resource;
5095
5096 encode_cinfo(p, &setxattr->setxa_cinfo);
5097
5098 return 0;
5099}
5100
5101/*
5102 * See if there are cookie values that can be rejected outright.
5103 */
5104static __be32
5105nfsd4_listxattr_validate_cookie(struct nfsd4_listxattrs *listxattrs,
5106 u32 *offsetp)
5107{
5108 u64 cookie = listxattrs->lsxa_cookie;
5109
5110 /*
5111 * If the cookie is larger than the maximum number we can fit
5112 * in either the buffer we just got back from vfs_listxattr, or,
5113 * XDR-encoded, in the return buffer, it's invalid.
5114 */
5115 if (cookie > (listxattrs->lsxa_len) / (XATTR_USER_PREFIX_LEN + 2))
5116 return nfserr_badcookie;
5117
5118 if (cookie > (listxattrs->lsxa_maxcount /
5119 (XDR_QUADLEN(XATTR_USER_PREFIX_LEN + 2) + 4)))
5120 return nfserr_badcookie;
5121
5122 *offsetp = (u32)cookie;
5123 return 0;
5124}
5125
5126static __be32
5127nfsd4_encode_listxattrs(struct nfsd4_compoundres *resp, __be32 nfserr,
5128 struct nfsd4_listxattrs *listxattrs)
5129{
5130 struct xdr_stream *xdr = &resp->xdr;
5131 u32 cookie_offset, count_offset, eof;
5132 u32 left, xdrleft, slen, count;
5133 u32 xdrlen, offset;
5134 u64 cookie;
5135 char *sp;
Chuck Leverb9a49232020-09-11 14:47:53 -04005136 __be32 status, tmp;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005137 __be32 *p;
5138 u32 nuser;
5139
5140 eof = 1;
5141
5142 status = nfsd4_listxattr_validate_cookie(listxattrs, &offset);
5143 if (status)
5144 goto out;
5145
5146 /*
5147 * Reserve space for the cookie and the name array count. Record
5148 * the offsets to save them later.
5149 */
5150 cookie_offset = xdr->buf->len;
5151 count_offset = cookie_offset + 8;
5152 p = xdr_reserve_space(xdr, 12);
5153 if (!p) {
5154 status = nfserr_resource;
5155 goto out;
5156 }
5157
5158 count = 0;
5159 left = listxattrs->lsxa_len;
5160 sp = listxattrs->lsxa_buf;
5161 nuser = 0;
5162
5163 xdrleft = listxattrs->lsxa_maxcount;
5164
5165 while (left > 0 && xdrleft > 0) {
5166 slen = strlen(sp);
5167
5168 /*
Alex Dewar4cce11fa2020-08-17 18:51:26 +01005169 * Check if this is a "user." attribute, skip it if not.
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005170 */
5171 if (strncmp(sp, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
5172 goto contloop;
5173
5174 slen -= XATTR_USER_PREFIX_LEN;
5175 xdrlen = 4 + ((slen + 3) & ~3);
5176 if (xdrlen > xdrleft) {
5177 if (count == 0) {
5178 /*
5179 * Can't even fit the first attribute name.
5180 */
5181 status = nfserr_toosmall;
5182 goto out;
5183 }
5184 eof = 0;
5185 goto wreof;
5186 }
5187
5188 left -= XATTR_USER_PREFIX_LEN;
5189 sp += XATTR_USER_PREFIX_LEN;
5190 if (nuser++ < offset)
5191 goto contloop;
5192
5193
5194 p = xdr_reserve_space(xdr, xdrlen);
5195 if (!p) {
5196 status = nfserr_resource;
5197 goto out;
5198 }
5199
Alex Deware2a18402020-08-12 15:12:51 +01005200 xdr_encode_opaque(p, sp, slen);
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005201
5202 xdrleft -= xdrlen;
5203 count++;
5204contloop:
5205 sp += slen + 1;
5206 left -= slen + 1;
5207 }
5208
5209 /*
5210 * If there were user attributes to copy, but we didn't copy
5211 * any, the offset was too large (e.g. the cookie was invalid).
5212 */
5213 if (nuser > 0 && count == 0) {
5214 status = nfserr_badcookie;
5215 goto out;
5216 }
5217
5218wreof:
5219 p = xdr_reserve_space(xdr, 4);
5220 if (!p) {
5221 status = nfserr_resource;
5222 goto out;
5223 }
5224 *p = cpu_to_be32(eof);
5225
5226 cookie = offset + count;
5227
5228 write_bytes_to_xdr_buf(xdr->buf, cookie_offset, &cookie, 8);
Chuck Leverb9a49232020-09-11 14:47:53 -04005229 tmp = cpu_to_be32(count);
5230 write_bytes_to_xdr_buf(xdr->buf, count_offset, &tmp, 4);
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005231out:
5232 if (listxattrs->lsxa_len)
5233 kvfree(listxattrs->lsxa_buf);
5234 return status;
5235}
5236
5237static __be32
5238nfsd4_encode_removexattr(struct nfsd4_compoundres *resp, __be32 nfserr,
5239 struct nfsd4_removexattr *removexattr)
5240{
5241 struct xdr_stream *xdr = &resp->xdr;
5242 __be32 *p;
5243
5244 p = xdr_reserve_space(xdr, 20);
5245 if (!p)
5246 return nfserr_resource;
5247
5248 p = encode_cinfo(p, &removexattr->rmxa_cinfo);
5249 return 0;
5250}
5251
Benny Halevy695e12f2008-07-04 14:38:33 +03005252typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
5253
Andy Adamson2db134e2009-04-03 08:27:55 +03005254/*
5255 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
5256 * since we don't need to filter out obsolete ops as this is
5257 * done in the decoding phase.
5258 */
Chuck Leverc1df6092017-08-01 11:59:58 -04005259static const nfsd4_enc nfsd4_enc_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04005260 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
5261 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
5262 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
5263 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
5264 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
5265 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
5266 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
5267 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
5268 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
5269 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
5270 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
5271 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
5272 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
5273 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
5274 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
5275 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
Benny Halevy84f09f42009-03-04 23:05:35 +02005276 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04005277 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
5278 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
5279 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
5280 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
5281 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
5282 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
5283 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
5284 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
5285 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
5286 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
5287 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
5288 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
5289 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
5290 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
5291 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
5292 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
5293 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
5294 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
5295 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
5296 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03005297
5298 /* NFSv4.1 operations */
5299 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04005300 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
Andy Adamson2db134e2009-04-03 08:27:55 +03005301 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
5302 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
Kinglong Mee43212cc2013-12-09 19:04:23 +08005303 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_noop,
5304 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03005305 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02005306#ifdef CONFIG_NFSD_PNFS
5307 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_getdeviceinfo,
5308 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
5309 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_layoutcommit,
5310 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_layoutget,
5311 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_layoutreturn,
5312#else
Andy Adamson2db134e2009-04-03 08:27:55 +03005313 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
5314 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
5315 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
5316 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
5317 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02005318#endif
Mi Jinlong22b6dee2010-12-27 14:29:57 +08005319 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
Andy Adamson2db134e2009-04-03 08:27:55 +03005320 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
5321 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
Bryan Schumaker17456802011-07-13 10:50:48 -04005322 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
Andy Adamson2db134e2009-04-03 08:27:55 +03005323 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
5324 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
5325 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
Anna Schumaker87a15a82014-09-26 13:58:26 -04005326
5327 /* NFSv4.2 operations */
5328 [OP_ALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
Anna Schumaker29ae7f92016-09-07 15:57:30 -04005329 [OP_COPY] = (nfsd4_enc)nfsd4_encode_copy,
Olga Kornievskaia51911862019-08-08 11:14:59 -04005330 [OP_COPY_NOTIFY] = (nfsd4_enc)nfsd4_encode_copy_notify,
Anna Schumaker87a15a82014-09-26 13:58:26 -04005331 [OP_DEALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
5332 [OP_IO_ADVISE] = (nfsd4_enc)nfsd4_encode_noop,
5333 [OP_LAYOUTERROR] = (nfsd4_enc)nfsd4_encode_noop,
5334 [OP_LAYOUTSTATS] = (nfsd4_enc)nfsd4_encode_noop,
5335 [OP_OFFLOAD_CANCEL] = (nfsd4_enc)nfsd4_encode_noop,
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04005336 [OP_OFFLOAD_STATUS] = (nfsd4_enc)nfsd4_encode_offload_status,
Anna Schumaker528b8492020-09-28 13:08:58 -04005337 [OP_READ_PLUS] = (nfsd4_enc)nfsd4_encode_read_plus,
Anna Schumaker24bab492014-09-26 13:58:27 -04005338 [OP_SEEK] = (nfsd4_enc)nfsd4_encode_seek,
Anna Schumaker87a15a82014-09-26 13:58:26 -04005339 [OP_WRITE_SAME] = (nfsd4_enc)nfsd4_encode_noop,
Christoph Hellwigffa01602015-12-03 12:59:52 +01005340 [OP_CLONE] = (nfsd4_enc)nfsd4_encode_noop,
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005341
5342 /* RFC 8276 extended atributes operations */
5343 [OP_GETXATTR] = (nfsd4_enc)nfsd4_encode_getxattr,
5344 [OP_SETXATTR] = (nfsd4_enc)nfsd4_encode_setxattr,
5345 [OP_LISTXATTRS] = (nfsd4_enc)nfsd4_encode_listxattrs,
5346 [OP_REMOVEXATTR] = (nfsd4_enc)nfsd4_encode_removexattr,
Benny Halevy695e12f2008-07-04 14:38:33 +03005347};
5348
Andy Adamson496c2622009-04-03 08:28:48 +03005349/*
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04005350 * Calculate whether we still have space to encode repsize bytes.
5351 * There are two considerations:
5352 * - For NFS versions >=4.1, the size of the reply must stay within
5353 * session limits
5354 * - For all NFS versions, we must stay within limited preallocated
5355 * buffer space.
Andy Adamson496c2622009-04-03 08:28:48 +03005356 *
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04005357 * This is called before the operation is processed, so can only provide
5358 * an upper estimate. For some nonidempotent operations (such as
5359 * getattr), it's not necessarily a problem if that estimate is wrong,
5360 * as we can fail it after processing without significant side effects.
Andy Adamson496c2622009-04-03 08:28:48 +03005361 */
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04005362__be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 respsize)
Andy Adamson496c2622009-04-03 08:28:48 +03005363{
J. Bruce Fields67492c92014-03-08 16:42:05 -05005364 struct xdr_buf *buf = &resp->rqstp->rq_res;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04005365 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson496c2622009-04-03 08:28:48 +03005366
J. Bruce Fields47ee5292014-03-12 21:39:35 -04005367 if (buf->len + respsize <= buf->buflen)
5368 return nfs_ok;
5369 if (!nfsd4_has_session(&resp->cstate))
J. Bruce Fieldsea8d7722014-03-08 17:19:55 -05005370 return nfserr_resource;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04005371 if (slot->sl_flags & NFSD4_SLOT_CACHETHIS) {
5372 WARN_ON_ONCE(1);
5373 return nfserr_rep_too_big_to_cache;
J. Bruce Fieldsea8d7722014-03-08 17:19:55 -05005374 }
J. Bruce Fields47ee5292014-03-12 21:39:35 -04005375 return nfserr_rep_too_big;
Andy Adamson496c2622009-04-03 08:28:48 +03005376}
5377
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378void
5379nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
5380{
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005381 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005382 struct nfs4_stateowner *so = resp->cstate.replay_owner;
J. Bruce Fields5f4ab942014-03-07 11:01:37 -05005383 struct svc_rqst *rqstp = resp->rqstp;
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04005384 const struct nfsd4_operation *opdesc = op->opdesc;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005385 int post_err_offset;
J. Bruce Fields07d1f802014-03-06 20:39:29 -05005386 nfsd4_enc encoder;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07005387 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05005389 p = xdr_reserve_space(xdr, 8);
5390 if (!p) {
5391 WARN_ON_ONCE(1);
5392 return;
5393 }
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04005394 *p++ = cpu_to_be32(op->opnum);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005395 post_err_offset = xdr->buf->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396
Benny Halevy695e12f2008-07-04 14:38:33 +03005397 if (op->opnum == OP_ILLEGAL)
5398 goto status;
J. Bruce Fieldsb7571e42017-05-06 10:49:21 -04005399 if (op->status && opdesc &&
5400 !(opdesc->op_flags & OP_NONTRIVIAL_ERROR_ENCODE))
5401 goto status;
Benny Halevy695e12f2008-07-04 14:38:33 +03005402 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
5403 !nfsd4_enc_ops[op->opnum]);
J. Bruce Fields07d1f802014-03-06 20:39:29 -05005404 encoder = nfsd4_enc_ops[op->opnum];
5405 op->status = encoder(resp, op->status, &op->u);
Chuck Lever08281342020-11-21 11:36:42 -05005406 if (op->status)
5407 trace_nfsd_compound_encode_err(rqstp, op->opnum, op->status);
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04005408 if (opdesc && opdesc->op_release)
5409 opdesc->op_release(&op->u);
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04005410 xdr_commit_encode(xdr);
5411
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04005412 /* nfsd4_check_resp_size guarantees enough room for error status */
J. Bruce Fields5f4ab942014-03-07 11:01:37 -05005413 if (!op->status) {
5414 int space_needed = 0;
5415 if (!nfsd4_last_compound_op(rqstp))
5416 space_needed = COMPOUND_ERR_SLACK_SPACE;
5417 op->status = nfsd4_check_resp_size(resp, space_needed);
5418 }
J. Bruce Fieldsc8f13d92014-05-08 17:38:18 -04005419 if (op->status == nfserr_resource && nfsd4_has_session(&resp->cstate)) {
5420 struct nfsd4_slot *slot = resp->cstate.slot;
5421
5422 if (slot->sl_flags & NFSD4_SLOT_CACHETHIS)
5423 op->status = nfserr_rep_too_big_to_cache;
5424 else
5425 op->status = nfserr_rep_too_big;
5426 }
J. Bruce Fields07d1f802014-03-06 20:39:29 -05005427 if (op->status == nfserr_resource ||
5428 op->status == nfserr_rep_too_big ||
5429 op->status == nfserr_rep_too_big_to_cache) {
5430 /*
5431 * The operation may have already been encoded or
5432 * partially encoded. No op returns anything additional
5433 * in the case of one of these three errors, so we can
5434 * just truncate back to after the status. But it's a
5435 * bug if we had to do this on a non-idempotent op:
5436 */
5437 warn_on_nonidempotent_op(op);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005438 xdr_truncate_encode(xdr, post_err_offset);
J. Bruce Fields07d1f802014-03-06 20:39:29 -05005439 }
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005440 if (so) {
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005441 int len = xdr->buf->len - post_err_offset;
5442
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005443 so->so_replay.rp_status = op->status;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005444 so->so_replay.rp_buflen = len;
5445 read_bytes_from_xdr_buf(xdr->buf, post_err_offset,
5446 so->so_replay.rp_buf, len);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005447 }
Benny Halevy695e12f2008-07-04 14:38:33 +03005448status:
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005449 /* Note that op->status is already in network byte order: */
5450 write_bytes_to_xdr_buf(xdr->buf, post_err_offset - 4, &op->status, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451}
5452
5453/*
5454 * Encode the reply stored in the stateowner reply cache
5455 *
5456 * XDR note: do not encode rp->rp_buflen: the buffer contains the
5457 * previously sent already encoded operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 */
5459void
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05005460nfsd4_encode_replay(struct xdr_stream *xdr, struct nfsd4_op *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07005462 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 struct nfs4_replay *rp = op->replay;
5464
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05005465 p = xdr_reserve_space(xdr, 8 + rp->rp_buflen);
5466 if (!p) {
5467 WARN_ON_ONCE(1);
5468 return;
5469 }
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04005470 *p++ = cpu_to_be32(op->opnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 *p++ = rp->rp_status; /* already xdr'ed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04005473 p = xdr_encode_opaque_fixed(p, rp->rp_buf, rp->rp_buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474}
5475
Christoph Hellwig85374882017-05-08 18:48:24 +02005476void nfsd4_release_compoundargs(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477{
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04005478 struct nfsd4_compoundargs *args = rqstp->rq_argp;
5479
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 if (args->ops != args->iops) {
5481 kfree(args->ops);
5482 args->ops = args->iops;
5483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 while (args->to_free) {
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -04005485 struct svcxdr_tmpbuf *tb = args->to_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 args->to_free = tb->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 kfree(tb);
5488 }
5489}
5490
5491int
Christoph Hellwig026fec72017-05-08 19:01:48 +02005492nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493{
Christoph Hellwig026fec72017-05-08 19:01:48 +02005494 struct nfsd4_compoundargs *args = rqstp->rq_argp;
5495
Chuck Leverc1346a122020-11-03 11:54:23 -05005496 /* svcxdr_tmp_alloc */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 args->tmpp = NULL;
5498 args->to_free = NULL;
Chuck Leverc1346a122020-11-03 11:54:23 -05005499
5500 args->xdr = &rqstp->rq_arg_stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 args->ops = args->iops;
5502 args->rqstp = rqstp;
5503
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04005504 return !nfsd4_decode_compound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505}
5506
5507int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02005508nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02005510 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields6ac90392014-02-26 17:39:35 -05005511 struct xdr_buf *buf = resp->xdr.buf;
5512
5513 WARN_ON_ONCE(buf->len != buf->head[0].iov_len + buf->page_len +
5514 buf->tail[0].iov_len);
J. Bruce Fieldsdd97fdd2014-02-26 17:16:27 -05005515
Chuck Levercc028a12020-10-02 15:52:44 -04005516 *p = resp->cstate.status;
5517
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04005518 rqstp->rq_next_page = resp->xdr.page_ptr + 1;
5519
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 p = resp->tagp;
5521 *p++ = htonl(resp->taglen);
5522 memcpy(p, resp->tag, resp->taglen);
5523 p += XDR_QUADLEN(resp->taglen);
5524 *p++ = htonl(resp->opcnt);
5525
Trond Myklebustb6076642014-06-30 11:48:35 -04005526 nfsd4_sequence_done(resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 return 1;
5528}
5529
5530/*
5531 * Local variables:
5532 * c-basic-offset: 8
5533 * End:
5534 */