blob: 59f091f9e237efa38614241b1636c262ab5611d4 [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
Christoph Hellwig4c94e132015-01-22 12:09:50 +0100210static __be32
Chuck Lever000dfa12020-11-03 13:24:10 -0500211nfsd4_decode_component4(struct nfsd4_compoundargs *argp, char **namp, u32 *lenp)
212{
213 __be32 *p, status;
214
215 if (xdr_stream_decode_u32(argp->xdr, lenp) < 0)
216 return nfserr_bad_xdr;
217 p = xdr_inline_decode(argp->xdr, *lenp);
218 if (!p)
219 return nfserr_bad_xdr;
220 status = check_filename((char *)p, *lenp);
221 if (status)
222 return status;
223 *namp = svcxdr_tmpalloc(argp, *lenp);
224 if (!*namp)
225 return nfserr_jukebox;
226 memcpy(*namp, p, *lenp);
227
228 return nfs_ok;
229}
230
231static __be32
J. Bruce Fieldsbdba5362019-06-05 14:17:42 -0400232nfsd4_decode_time(struct nfsd4_compoundargs *argp, struct timespec64 *tv)
Christoph Hellwig4c94e132015-01-22 12:09:50 +0100233{
234 DECODE_HEAD;
Christoph Hellwig4c94e132015-01-22 12:09:50 +0100235
236 READ_BUF(12);
J. Bruce Fieldsbdba5362019-06-05 14:17:42 -0400237 p = xdr_decode_hyper(p, &tv->tv_sec);
Christoph Hellwig4c94e132015-01-22 12:09:50 +0100238 tv->tv_nsec = be32_to_cpup(p++);
239 if (tv->tv_nsec >= (u32)1000000000)
240 return nfserr_inval;
241
242 DECODE_TAIL;
243}
244
Al Virob37ad282006-10-19 23:28:59 -0700245static __be32
Chuck Lever1c3eff72020-11-19 14:01:08 -0500246nfsd4_decode_nfstime4(struct nfsd4_compoundargs *argp, struct timespec64 *tv)
247{
248 __be32 *p;
249
250 p = xdr_inline_decode(argp->xdr, XDR_UNIT * 3);
251 if (!p)
252 return nfserr_bad_xdr;
253 p = xdr_decode_hyper(p, &tv->tv_sec);
254 tv->tv_nsec = be32_to_cpup(p++);
255 if (tv->tv_nsec >= (u32)1000000000)
256 return nfserr_inval;
257 return nfs_ok;
258}
259
260static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
262{
263 u32 bmlen;
264 DECODE_HEAD;
265
266 bmval[0] = 0;
267 bmval[1] = 0;
Andy Adamson7e705702009-04-03 08:29:11 +0300268 bmval[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400271 bmlen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (bmlen > 1000)
273 goto xdr_error;
274
275 READ_BUF(bmlen << 2);
276 if (bmlen > 0)
J. Bruce Fields06553992014-06-02 12:32:51 -0400277 bmval[0] = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (bmlen > 1)
J. Bruce Fields06553992014-06-02 12:32:51 -0400279 bmval[1] = be32_to_cpup(p++);
Andy Adamson7e705702009-04-03 08:29:11 +0300280 if (bmlen > 2)
J. Bruce Fields06553992014-06-02 12:32:51 -0400281 bmval[2] = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 DECODE_TAIL;
284}
285
Chuck Leverd1c263a2020-11-03 12:56:05 -0500286/**
287 * nfsd4_decode_bitmap4 - Decode an NFSv4 bitmap4
288 * @argp: NFSv4 compound argument structure
289 * @bmval: pointer to an array of u32's to decode into
290 * @bmlen: size of the @bmval array
291 *
292 * The server needs to return nfs_ok rather than nfserr_bad_xdr when
293 * encountering bitmaps containing bits it does not recognize. This
294 * includes bits in bitmap words past WORDn, where WORDn is the last
295 * bitmap WORD the implementation currently supports. Thus we are
296 * careful here to simply ignore bits in bitmap words that this
297 * implementation has yet to support explicitly.
298 *
299 * Return values:
300 * %nfs_ok: @bmval populated successfully
301 * %nfserr_bad_xdr: the encoded bitmap was invalid
302 */
303static __be32
304nfsd4_decode_bitmap4(struct nfsd4_compoundargs *argp, u32 *bmval, u32 bmlen)
305{
306 u32 i, count;
307 __be32 *p;
308
309 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
310 return nfserr_bad_xdr;
311 /* request sanity */
312 if (count > 1000)
313 return nfserr_bad_xdr;
314 p = xdr_inline_decode(argp->xdr, count << 2);
315 if (!p)
316 return nfserr_bad_xdr;
317 i = 0;
318 while (i < count)
319 bmval[i++] = be32_to_cpup(p++);
320 while (i < bmlen)
321 bmval[i++] = 0;
322
323 return nfs_ok;
324}
325
Al Virob37ad282006-10-19 23:28:59 -0700326static __be32
Chuck Leverc941a962020-11-19 13:02:54 -0500327nfsd4_decode_nfsace4(struct nfsd4_compoundargs *argp, struct nfs4_ace *ace)
328{
329 __be32 *p, status;
330 u32 length;
331
332 if (xdr_stream_decode_u32(argp->xdr, &ace->type) < 0)
333 return nfserr_bad_xdr;
334 if (xdr_stream_decode_u32(argp->xdr, &ace->flag) < 0)
335 return nfserr_bad_xdr;
336 if (xdr_stream_decode_u32(argp->xdr, &ace->access_mask) < 0)
337 return nfserr_bad_xdr;
338
339 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
340 return nfserr_bad_xdr;
341 p = xdr_inline_decode(argp->xdr, length);
342 if (!p)
343 return nfserr_bad_xdr;
344 ace->whotype = nfs4_acl_get_whotype((char *)p, length);
345 if (ace->whotype != NFS4_ACL_WHO_NAMED)
346 status = nfs_ok;
347 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
348 status = nfsd_map_name_to_gid(argp->rqstp,
349 (char *)p, length, &ace->who_gid);
350 else
351 status = nfsd_map_name_to_uid(argp->rqstp,
352 (char *)p, length, &ace->who_uid);
353
354 return status;
355}
356
357/* A counted array of nfsace4's */
358static noinline __be32
359nfsd4_decode_acl(struct nfsd4_compoundargs *argp, struct nfs4_acl **acl)
360{
361 struct nfs4_ace *ace;
362 __be32 status;
363 u32 count;
364
365 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
366 return nfserr_bad_xdr;
367
368 if (count > xdr_stream_remaining(argp->xdr) / 20)
369 /*
370 * Even with 4-byte names there wouldn't be
371 * space for that many aces; something fishy is
372 * going on:
373 */
374 return nfserr_fbig;
375
376 *acl = svcxdr_tmpalloc(argp, nfs4_acl_bytes(count));
377 if (*acl == NULL)
378 return nfserr_jukebox;
379
380 (*acl)->naces = count;
381 for (ace = (*acl)->aces; ace < (*acl)->aces + count; ace++) {
382 status = nfsd4_decode_nfsace4(argp, ace);
383 if (status)
384 return status;
385 }
386
387 return nfs_ok;
388}
389
Chuck Leverdabe9182020-11-19 14:05:51 -0500390static noinline __be32
391nfsd4_decode_security_label(struct nfsd4_compoundargs *argp,
392 struct xdr_netobj *label)
393{
394 u32 lfs, pi, length;
395 __be32 *p;
396
397 if (xdr_stream_decode_u32(argp->xdr, &lfs) < 0)
398 return nfserr_bad_xdr;
399 if (xdr_stream_decode_u32(argp->xdr, &pi) < 0)
400 return nfserr_bad_xdr;
401
402 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
403 return nfserr_bad_xdr;
404 if (length > NFS4_MAXLABELLEN)
405 return nfserr_badlabel;
406 p = xdr_inline_decode(argp->xdr, length);
407 if (!p)
408 return nfserr_bad_xdr;
409 label->len = length;
410 label->data = svcxdr_dupstr(argp, p, length);
411 if (!label->data)
412 return nfserr_jukebox;
413
414 return nfs_ok;
415}
416
Chuck Leverc941a962020-11-19 13:02:54 -0500417static __be32
Chuck Leverd1c263a2020-11-03 12:56:05 -0500418nfsd4_decode_fattr4(struct nfsd4_compoundargs *argp, u32 *bmval, u32 bmlen,
419 struct iattr *iattr, struct nfs4_acl **acl,
420 struct xdr_netobj *label, int *umask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Chuck Lever081d53f2020-11-19 13:09:13 -0500422 unsigned int starting_pos;
423 u32 attrlist4_count;
Chuck Leverd1c263a2020-11-03 12:56:05 -0500424 __be32 *p, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 iattr->ia_valid = 0;
Chuck Leverd1c263a2020-11-03 12:56:05 -0500427 status = nfsd4_decode_bitmap4(argp, bmval, bmlen);
428 if (status)
429 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
J. Bruce Fieldse864c182016-06-10 16:56:05 -0400431 if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
432 || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
433 || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2) {
434 if (nfsd_attrs_supported(argp->minorversion, bmval))
435 return nfserr_inval;
436 return nfserr_attrnotsupp;
437 }
438
Chuck Lever081d53f2020-11-19 13:09:13 -0500439 if (xdr_stream_decode_u32(argp->xdr, &attrlist4_count) < 0)
440 return nfserr_bad_xdr;
441 starting_pos = xdr_stream_pos(argp->xdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 if (bmval[0] & FATTR4_WORD0_SIZE) {
Chuck Lever2ac1b9b2020-11-19 13:47:16 -0500444 u64 size;
445
446 if (xdr_stream_decode_u64(argp->xdr, &size) < 0)
447 return nfserr_bad_xdr;
448 iattr->ia_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 iattr->ia_valid |= ATTR_SIZE;
450 }
451 if (bmval[0] & FATTR4_WORD0_ACL) {
Chuck Leverc941a962020-11-19 13:02:54 -0500452 status = nfsd4_decode_acl(argp, acl);
453 if (status)
454 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 } else
456 *acl = NULL;
457 if (bmval[1] & FATTR4_WORD1_MODE) {
Chuck Lever1c8f0ad2020-11-19 13:54:26 -0500458 u32 mode;
459
460 if (xdr_stream_decode_u32(argp->xdr, &mode) < 0)
461 return nfserr_bad_xdr;
462 iattr->ia_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 iattr->ia_mode &= (S_IFMT | S_IALLUGO);
464 iattr->ia_valid |= ATTR_MODE;
465 }
466 if (bmval[1] & FATTR4_WORD1_OWNER) {
Chuck Lever9853a5a2020-11-19 13:56:42 -0500467 u32 length;
468
469 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
470 return nfserr_bad_xdr;
471 p = xdr_inline_decode(argp->xdr, length);
472 if (!p)
473 return nfserr_bad_xdr;
474 status = nfsd_map_name_to_uid(argp->rqstp, (char *)p, length,
475 &iattr->ia_uid);
476 if (status)
NeilBrown47c85292011-02-16 13:08:35 +1100477 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 iattr->ia_valid |= ATTR_UID;
479 }
480 if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
Chuck Lever393c31d2020-11-19 13:58:18 -0500481 u32 length;
482
483 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
484 return nfserr_bad_xdr;
485 p = xdr_inline_decode(argp->xdr, length);
486 if (!p)
487 return nfserr_bad_xdr;
488 status = nfsd_map_name_to_gid(argp->rqstp, (char *)p, length,
489 &iattr->ia_gid);
490 if (status)
NeilBrown47c85292011-02-16 13:08:35 +1100491 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 iattr->ia_valid |= ATTR_GID;
493 }
494 if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
Chuck Lever1c3eff72020-11-19 14:01:08 -0500495 u32 set_it;
496
497 if (xdr_stream_decode_u32(argp->xdr, &set_it) < 0)
498 return nfserr_bad_xdr;
499 switch (set_it) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 case NFS4_SET_TO_CLIENT_TIME:
Chuck Lever1c3eff72020-11-19 14:01:08 -0500501 status = nfsd4_decode_nfstime4(argp, &iattr->ia_atime);
Christoph Hellwig4c94e132015-01-22 12:09:50 +0100502 if (status)
503 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
505 break;
506 case NFS4_SET_TO_SERVER_TIME:
507 iattr->ia_valid |= ATTR_ATIME;
508 break;
509 default:
Chuck Lever1c3eff72020-11-19 14:01:08 -0500510 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
Chuck Lever1c3eff72020-11-19 14:01:08 -0500514 u32 set_it;
515
516 if (xdr_stream_decode_u32(argp->xdr, &set_it) < 0)
517 return nfserr_bad_xdr;
518 switch (set_it) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 case NFS4_SET_TO_CLIENT_TIME:
Chuck Lever1c3eff72020-11-19 14:01:08 -0500520 status = nfsd4_decode_nfstime4(argp, &iattr->ia_mtime);
Christoph Hellwig4c94e132015-01-22 12:09:50 +0100521 if (status)
522 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
524 break;
525 case NFS4_SET_TO_SERVER_TIME:
526 iattr->ia_valid |= ATTR_MTIME;
527 break;
528 default:
Chuck Lever1c3eff72020-11-19 14:01:08 -0500529 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531 }
David Quigley18032ca2013-05-02 13:19:10 -0400532 label->len = 0;
Arnd Bergmann2285ae72018-01-22 22:09:12 +0100533 if (IS_ENABLED(CONFIG_NFSD_V4_SECURITY_LABEL) &&
534 bmval[2] & FATTR4_WORD2_SECURITY_LABEL) {
Chuck Leverdabe9182020-11-19 14:05:51 -0500535 status = nfsd4_decode_security_label(argp, label);
536 if (status)
537 return status;
David Quigley18032ca2013-05-02 13:19:10 -0400538 }
Andreas Gruenbacher47057ab2016-01-12 20:24:14 +0100539 if (bmval[2] & FATTR4_WORD2_MODE_UMASK) {
Chuck Lever66f04762020-11-19 14:07:43 -0500540 u32 mode, mask;
541
Andreas Gruenbacher47057ab2016-01-12 20:24:14 +0100542 if (!umask)
Chuck Lever66f04762020-11-19 14:07:43 -0500543 return nfserr_bad_xdr;
544 if (xdr_stream_decode_u32(argp->xdr, &mode) < 0)
545 return nfserr_bad_xdr;
546 iattr->ia_mode = mode & (S_IFMT | S_IALLUGO);
547 if (xdr_stream_decode_u32(argp->xdr, &mask) < 0)
548 return nfserr_bad_xdr;
549 *umask = mask & S_IRWXUGO;
Andreas Gruenbacher47057ab2016-01-12 20:24:14 +0100550 iattr->ia_valid |= ATTR_MODE;
551 }
Chuck Lever081d53f2020-11-19 13:09:13 -0500552
553 /* request sanity: did attrlist4 contain the expected number of words? */
554 if (attrlist4_count != xdr_stream_pos(argp->xdr) - starting_pos)
555 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Chuck Leverd1c263a2020-11-03 12:56:05 -0500557 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Al Virob37ad282006-10-19 23:28:59 -0700560static __be32
Benny Halevye31a1b62008-08-12 20:46:18 +0300561nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
562{
563 DECODE_HEAD;
564
565 READ_BUF(sizeof(stateid_t));
J. Bruce Fields06553992014-06-02 12:32:51 -0400566 sid->si_generation = be32_to_cpup(p++);
Benny Halevye31a1b62008-08-12 20:46:18 +0300567 COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
568
569 DECODE_TAIL;
570}
571
Chuck Leverd3d2f382020-11-03 13:18:23 -0500572static __be32
573nfsd4_decode_stateid4(struct nfsd4_compoundargs *argp, stateid_t *sid)
574{
575 __be32 *p;
576
577 p = xdr_inline_decode(argp->xdr, NFS4_STATEID_SIZE);
578 if (!p)
579 return nfserr_bad_xdr;
580 sid->si_generation = be32_to_cpup(p++);
581 memcpy(&sid->si_opaque, p, sizeof(sid->si_opaque));
582 return nfs_ok;
583}
584
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400585static __be32 nfsd4_decode_cb_sec(struct nfsd4_compoundargs *argp, struct nfsd4_cb_sec *cbs)
586{
587 DECODE_HEAD;
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400588 struct user_namespace *userns = nfsd_user_namespace(argp->rqstp);
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500589 u32 dummy, uid, gid;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400590 char *machine_name;
591 int i;
592 int nr_secflavs;
593
594 /* callback_sec_params4 */
595 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400596 nr_secflavs = be32_to_cpup(p++);
J. Bruce Fields57569a72013-05-17 16:25:32 -0400597 if (nr_secflavs)
598 cbs->flavor = (u32)(-1);
599 else
600 /* Is this legal? Be generous, take it to mean AUTH_NONE: */
601 cbs->flavor = 0;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400602 for (i = 0; i < nr_secflavs; ++i) {
603 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400604 dummy = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400605 switch (dummy) {
606 case RPC_AUTH_NULL:
607 /* Nothing to read */
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500608 if (cbs->flavor == (u32)(-1))
609 cbs->flavor = RPC_AUTH_NULL;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400610 break;
611 case RPC_AUTH_UNIX:
612 READ_BUF(8);
613 /* stamp */
J. Bruce Fields06553992014-06-02 12:32:51 -0400614 dummy = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400615
616 /* machine name */
J. Bruce Fields06553992014-06-02 12:32:51 -0400617 dummy = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400618 READ_BUF(dummy);
619 SAVEMEM(machine_name, dummy);
620
621 /* uid, gid */
622 READ_BUF(8);
J. Bruce Fields06553992014-06-02 12:32:51 -0400623 uid = be32_to_cpup(p++);
624 gid = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400625
626 /* more gids */
627 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400628 dummy = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400629 READ_BUF(dummy * 4);
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500630 if (cbs->flavor == (u32)(-1)) {
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400631 kuid_t kuid = make_kuid(userns, uid);
632 kgid_t kgid = make_kgid(userns, gid);
Eric W. Biederman03bc6d12013-02-02 06:24:49 -0800633 if (uid_valid(kuid) && gid_valid(kgid)) {
634 cbs->uid = kuid;
635 cbs->gid = kgid;
636 cbs->flavor = RPC_AUTH_UNIX;
637 } else {
638 dprintk("RPC_AUTH_UNIX with invalid"
639 "uid or gid ignoring!\n");
640 }
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500641 }
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400642 break;
643 case RPC_AUTH_GSS:
644 dprintk("RPC_AUTH_GSS callback secflavor "
645 "not supported!\n");
646 READ_BUF(8);
647 /* gcbp_service */
J. Bruce Fields06553992014-06-02 12:32:51 -0400648 dummy = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400649 /* gcbp_handle_from_server */
J. Bruce Fields06553992014-06-02 12:32:51 -0400650 dummy = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400651 READ_BUF(dummy);
652 p += XDR_QUADLEN(dummy);
653 /* gcbp_handle_from_client */
654 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400655 dummy = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400656 READ_BUF(dummy);
657 break;
658 default:
659 dprintk("Illegal callback secflavor\n");
660 return nfserr_inval;
661 }
662 }
663 DECODE_TAIL;
664}
665
Chuck Leverd169a6a2020-11-03 13:12:27 -0500666/*
667 * NFSv4 operation argument decoders
668 */
669
670static __be32
671nfsd4_decode_access(struct nfsd4_compoundargs *argp,
672 struct nfsd4_access *access)
673{
674 if (xdr_stream_decode_u32(argp->xdr, &access->ac_req_access) < 0)
675 return nfserr_bad_xdr;
676 return nfs_ok;
677}
678
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -0400679static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp, struct nfsd4_backchannel_ctl *bc)
680{
681 DECODE_HEAD;
682
683 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400684 bc->bc_cb_program = be32_to_cpup(p++);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -0400685 nfsd4_decode_cb_sec(argp, &bc->bc_cb_sec);
686
687 DECODE_TAIL;
688}
689
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400690static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
691{
692 DECODE_HEAD;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400693
694 READ_BUF(NFS4_MAX_SESSIONID_LEN + 8);
695 COPYMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields06553992014-06-02 12:32:51 -0400696 bcts->dir = be32_to_cpup(p++);
Bryan Schumaker6ce23572011-04-27 15:47:16 -0400697 /* XXX: skipping ctsa_use_conn_in_rdma_mode. Perhaps Tom Tucker
698 * could help us figure out we should be using it. */
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400699 DECODE_TAIL;
700}
701
Al Virob37ad282006-10-19 23:28:59 -0700702static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
704{
Chuck Leverd3d2f382020-11-03 13:18:23 -0500705 if (xdr_stream_decode_u32(argp->xdr, &close->cl_seqid) < 0)
706 return nfserr_bad_xdr;
707 return nfsd4_decode_stateid4(argp, &close->cl_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710
Al Virob37ad282006-10-19 23:28:59 -0700711static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
713{
Chuck Levercbd9abb2020-11-03 13:19:51 -0500714 if (xdr_stream_decode_u64(argp->xdr, &commit->co_offset) < 0)
715 return nfserr_bad_xdr;
716 if (xdr_stream_decode_u32(argp->xdr, &commit->co_count) < 0)
717 return nfserr_bad_xdr;
718 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
Al Virob37ad282006-10-19 23:28:59 -0700721static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
723{
Chuck Lever000dfa12020-11-03 13:24:10 -0500724 __be32 *p, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Chuck Lever000dfa12020-11-03 13:24:10 -0500726 if (xdr_stream_decode_u32(argp->xdr, &create->cr_type) < 0)
727 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 switch (create->cr_type) {
729 case NF4LNK:
Chuck Lever000dfa12020-11-03 13:24:10 -0500730 if (xdr_stream_decode_u32(argp->xdr, &create->cr_datalen) < 0)
731 return nfserr_bad_xdr;
732 p = xdr_inline_decode(argp->xdr, create->cr_datalen);
733 if (!p)
734 return nfserr_bad_xdr;
J. Bruce Fields29c353b2014-06-24 17:06:51 -0400735 create->cr_data = svcxdr_dupstr(argp, p, create->cr_datalen);
J. Bruce Fields7fb84302014-06-24 15:06:41 -0400736 if (!create->cr_data)
J. Bruce Fields76f471282014-06-19 16:44:48 -0400737 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 break;
739 case NF4BLK:
740 case NF4CHR:
Chuck Lever000dfa12020-11-03 13:24:10 -0500741 if (xdr_stream_decode_u32(argp->xdr, &create->cr_specdata1) < 0)
742 return nfserr_bad_xdr;
743 if (xdr_stream_decode_u32(argp->xdr, &create->cr_specdata2) < 0)
744 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 break;
746 case NF4SOCK:
747 case NF4FIFO:
748 case NF4DIR:
749 default:
750 break;
751 }
Chuck Lever000dfa12020-11-03 13:24:10 -0500752 status = nfsd4_decode_component4(argp, &create->cr_name,
753 &create->cr_namelen);
754 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return status;
Chuck Leverd1c263a2020-11-03 12:56:05 -0500756 status = nfsd4_decode_fattr4(argp, create->cr_bmval,
757 ARRAY_SIZE(create->cr_bmval),
758 &create->cr_iattr, &create->cr_acl,
759 &create->cr_label, &create->cr_umask);
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300760 if (status)
Chuck Lever000dfa12020-11-03 13:24:10 -0500761 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Chuck Lever000dfa12020-11-03 13:24:10 -0500763 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
Al Virob37ad282006-10-19 23:28:59 -0700766static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
768{
Chuck Lever95e64822020-11-21 14:11:58 -0500769 return nfsd4_decode_stateid4(argp, &dr->dr_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
Al Virob37ad282006-10-19 23:28:59 -0700772static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
774{
Chuck Leverf759eff2020-11-19 14:40:20 -0500775 return nfsd4_decode_bitmap4(argp, getattr->ga_bmval,
776 ARRAY_SIZE(getattr->ga_bmval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
Al Virob37ad282006-10-19 23:28:59 -0700779static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
781{
Chuck Lever5c505d12020-11-04 15:01:24 -0500782 return nfsd4_decode_component4(argp, &link->li_name, &link->li_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
Al Virob37ad282006-10-19 23:28:59 -0700785static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
787{
788 DECODE_HEAD;
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 /*
791 * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
792 */
793 READ_BUF(28);
J. Bruce Fields06553992014-06-02 12:32:51 -0400794 lock->lk_type = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
796 goto xdr_error;
J. Bruce Fields06553992014-06-02 12:32:51 -0400797 lock->lk_reclaim = be32_to_cpup(p++);
J. Bruce Fields542d1ab2014-06-02 12:45:31 -0400798 p = xdr_decode_hyper(p, &lock->lk_offset);
799 p = xdr_decode_hyper(p, &lock->lk_length);
J. Bruce Fields06553992014-06-02 12:32:51 -0400800 lock->lk_is_new = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 if (lock->lk_is_new) {
Benny Halevye31a1b62008-08-12 20:46:18 +0300803 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400804 lock->lk_new_open_seqid = be32_to_cpup(p++);
Benny Halevye31a1b62008-08-12 20:46:18 +0300805 status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
806 if (status)
807 return status;
808 READ_BUF(8 + sizeof(clientid_t));
J. Bruce Fields06553992014-06-02 12:32:51 -0400809 lock->lk_new_lock_seqid = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
J. Bruce Fields06553992014-06-02 12:32:51 -0400811 lock->lk_new_owner.len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 READ_BUF(lock->lk_new_owner.len);
813 READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
814 } else {
Benny Halevye31a1b62008-08-12 20:46:18 +0300815 status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
816 if (status)
817 return status;
818 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400819 lock->lk_old_lock_seqid = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821
822 DECODE_TAIL;
823}
824
Al Virob37ad282006-10-19 23:28:59 -0700825static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
827{
828 DECODE_HEAD;
829
830 READ_BUF(32);
J. Bruce Fields06553992014-06-02 12:32:51 -0400831 lockt->lt_type = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
833 goto xdr_error;
J. Bruce Fields542d1ab2014-06-02 12:45:31 -0400834 p = xdr_decode_hyper(p, &lockt->lt_offset);
835 p = xdr_decode_hyper(p, &lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 COPYMEM(&lockt->lt_clientid, 8);
J. Bruce Fields06553992014-06-02 12:32:51 -0400837 lockt->lt_owner.len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 READ_BUF(lockt->lt_owner.len);
839 READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
840
841 DECODE_TAIL;
842}
843
Al Virob37ad282006-10-19 23:28:59 -0700844static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
846{
847 DECODE_HEAD;
848
Benny Halevye31a1b62008-08-12 20:46:18 +0300849 READ_BUF(8);
J. Bruce Fields06553992014-06-02 12:32:51 -0400850 locku->lu_type = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
852 goto xdr_error;
J. Bruce Fields06553992014-06-02 12:32:51 -0400853 locku->lu_seqid = be32_to_cpup(p++);
Benny Halevye31a1b62008-08-12 20:46:18 +0300854 status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
855 if (status)
856 return status;
857 READ_BUF(16);
J. Bruce Fields542d1ab2014-06-02 12:45:31 -0400858 p = xdr_decode_hyper(p, &locku->lu_offset);
859 p = xdr_decode_hyper(p, &locku->lu_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 DECODE_TAIL;
862}
863
Al Virob37ad282006-10-19 23:28:59 -0700864static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
866{
867 DECODE_HEAD;
868
869 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400870 lookup->lo_len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 READ_BUF(lookup->lo_len);
872 SAVEMEM(lookup->lo_name, lookup->lo_len);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500873 if ((status = check_filename(lookup->lo_name, lookup->lo_len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return status;
875
876 DECODE_TAIL;
877}
878
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200879static __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 -0400880{
881 __be32 *p;
882 u32 w;
883
884 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400885 w = be32_to_cpup(p++);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200886 *share_access = w & NFS4_SHARE_ACCESS_MASK;
887 *deleg_want = w & NFS4_SHARE_WANT_MASK;
888 if (deleg_when)
889 *deleg_when = w & NFS4_SHARE_WHEN_MASK;
890
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400891 switch (w & NFS4_SHARE_ACCESS_MASK) {
892 case NFS4_SHARE_ACCESS_READ:
893 case NFS4_SHARE_ACCESS_WRITE:
894 case NFS4_SHARE_ACCESS_BOTH:
895 break;
896 default:
897 return nfserr_bad_xdr;
898 }
Benny Halevyfc0d14f2011-10-27 20:43:01 +0200899 w &= ~NFS4_SHARE_ACCESS_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400900 if (!w)
901 return nfs_ok;
902 if (!argp->minorversion)
903 return nfserr_bad_xdr;
904 switch (w & NFS4_SHARE_WANT_MASK) {
905 case NFS4_SHARE_WANT_NO_PREFERENCE:
906 case NFS4_SHARE_WANT_READ_DELEG:
907 case NFS4_SHARE_WANT_WRITE_DELEG:
908 case NFS4_SHARE_WANT_ANY_DELEG:
909 case NFS4_SHARE_WANT_NO_DELEG:
910 case NFS4_SHARE_WANT_CANCEL:
911 break;
912 default:
913 return nfserr_bad_xdr;
914 }
Benny Halevy92bac8c2011-10-19 19:13:29 -0700915 w &= ~NFS4_SHARE_WANT_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400916 if (!w)
917 return nfs_ok;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200918
919 if (!deleg_when) /* open_downgrade */
920 return nfserr_inval;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400921 switch (w) {
922 case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
923 case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
Benny Halevyc668fc62011-10-19 19:13:13 -0700924 case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
925 NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400926 return nfs_ok;
927 }
928xdr_error:
929 return nfserr_bad_xdr;
930}
931
932static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
933{
934 __be32 *p;
935
936 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400937 *x = be32_to_cpup(p++);
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400938 /* Note: unlinke access bits, deny bits may be zero. */
Dan Carpenter01cd4af2011-10-17 10:41:17 +0300939 if (*x & ~NFS4_SHARE_DENY_BOTH)
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400940 return nfserr_bad_xdr;
941 return nfs_ok;
942xdr_error:
943 return nfserr_bad_xdr;
944}
945
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400946static __be32 nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
947{
Chuck Leverc1346a122020-11-03 11:54:23 -0500948 DECODE_HEAD;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400949
950 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400951 o->len = be32_to_cpup(p++);
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400952
953 if (o->len == 0 || o->len > NFS4_OPAQUE_LIMIT)
954 return nfserr_bad_xdr;
955
956 READ_BUF(o->len);
957 SAVEMEM(o->data, o->len);
Chuck Leverc1346a122020-11-03 11:54:23 -0500958
959 DECODE_TAIL;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400960}
961
Al Virob37ad282006-10-19 23:28:59 -0700962static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
964{
965 DECODE_HEAD;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200966 u32 dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 memset(open->op_bmval, 0, sizeof(open->op_bmval));
969 open->op_iattr.ia_valid = 0;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400970 open->op_openowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
J. Bruce Fields9d313b12013-02-28 12:51:49 -0800972 open->op_xdr_error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 /* seqid, share_access, share_deny, clientid, ownerlen */
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400974 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400975 open->op_seqid = be32_to_cpup(p++);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200976 /* decode, yet ignore deleg_when until supported */
977 status = nfsd4_decode_share_access(argp, &open->op_share_access,
978 &open->op_deleg_want, &dummy);
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400979 if (status)
980 goto xdr_error;
981 status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
982 if (status)
983 goto xdr_error;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400984 READ_BUF(sizeof(clientid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 COPYMEM(&open->op_clientid, sizeof(clientid_t));
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400986 status = nfsd4_decode_opaque(argp, &open->op_owner);
987 if (status)
988 goto xdr_error;
989 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400990 open->op_create = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 switch (open->op_create) {
992 case NFS4_OPEN_NOCREATE:
993 break;
994 case NFS4_OPEN_CREATE:
995 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400996 open->op_createmode = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 switch (open->op_createmode) {
998 case NFS4_CREATE_UNCHECKED:
999 case NFS4_CREATE_GUARDED:
Chuck Leverd1c263a2020-11-03 12:56:05 -05001000 status = nfsd4_decode_fattr4(argp, open->op_bmval,
1001 ARRAY_SIZE(open->op_bmval),
1002 &open->op_iattr, &open->op_acl,
1003 &open->op_label, &open->op_umask);
Benny Halevyc0d6fc82009-04-03 08:29:05 +03001004 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 goto out;
1006 break;
1007 case NFS4_CREATE_EXCLUSIVE:
Chuck Leverab4684d2012-03-02 17:13:50 -05001008 READ_BUF(NFS4_VERIFIER_SIZE);
1009 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 break;
Benny Halevy79fb54a2009-04-03 08:29:17 +03001011 case NFS4_CREATE_EXCLUSIVE4_1:
1012 if (argp->minorversion < 1)
1013 goto xdr_error;
Chuck Leverab4684d2012-03-02 17:13:50 -05001014 READ_BUF(NFS4_VERIFIER_SIZE);
1015 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
Chuck Leverd1c263a2020-11-03 12:56:05 -05001016 status = nfsd4_decode_fattr4(argp, open->op_bmval,
1017 ARRAY_SIZE(open->op_bmval),
1018 &open->op_iattr, &open->op_acl,
1019 &open->op_label, &open->op_umask);
Benny Halevy79fb54a2009-04-03 08:29:17 +03001020 if (status)
1021 goto out;
1022 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 default:
1024 goto xdr_error;
1025 }
1026 break;
1027 default:
1028 goto xdr_error;
1029 }
1030
1031 /* open_claim */
1032 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001033 open->op_claim_type = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 switch (open->op_claim_type) {
1035 case NFS4_OPEN_CLAIM_NULL:
1036 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1037 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001038 open->op_fname.len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 READ_BUF(open->op_fname.len);
1040 SAVEMEM(open->op_fname.data, open->op_fname.len);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001041 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 return status;
1043 break;
1044 case NFS4_OPEN_CLAIM_PREVIOUS:
1045 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001046 open->op_delegate_type = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 break;
1048 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
Benny Halevye31a1b62008-08-12 20:46:18 +03001049 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
1050 if (status)
1051 return status;
1052 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001053 open->op_fname.len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 READ_BUF(open->op_fname.len);
1055 SAVEMEM(open->op_fname.data, open->op_fname.len);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001056 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return status;
1058 break;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04001059 case NFS4_OPEN_CLAIM_FH:
1060 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1061 if (argp->minorversion < 1)
1062 goto xdr_error;
1063 /* void */
1064 break;
1065 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1066 if (argp->minorversion < 1)
1067 goto xdr_error;
1068 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
1069 if (status)
1070 return status;
1071 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 default:
1073 goto xdr_error;
1074 }
1075
1076 DECODE_TAIL;
1077}
1078
Al Virob37ad282006-10-19 23:28:59 -07001079static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
1081{
1082 DECODE_HEAD;
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001083
1084 if (argp->minorversion >= 1)
1085 return nfserr_notsupp;
1086
Benny Halevye31a1b62008-08-12 20:46:18 +03001087 status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
1088 if (status)
1089 return status;
1090 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001091 open_conf->oc_seqid = be32_to_cpup(p++);
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 DECODE_TAIL;
1094}
1095
Al Virob37ad282006-10-19 23:28:59 -07001096static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
1098{
1099 DECODE_HEAD;
1100
Benny Halevye31a1b62008-08-12 20:46:18 +03001101 status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
1102 if (status)
1103 return status;
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001104 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001105 open_down->od_seqid = be32_to_cpup(p++);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02001106 status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
1107 &open_down->od_deleg_want, NULL);
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001108 if (status)
1109 return status;
1110 status = nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
1111 if (status)
1112 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 DECODE_TAIL;
1114}
1115
Al Virob37ad282006-10-19 23:28:59 -07001116static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
1118{
1119 DECODE_HEAD;
1120
1121 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001122 putfh->pf_fhlen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (putfh->pf_fhlen > NFS4_FHSIZE)
1124 goto xdr_error;
1125 READ_BUF(putfh->pf_fhlen);
1126 SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
1127
1128 DECODE_TAIL;
1129}
1130
Al Virob37ad282006-10-19 23:28:59 -07001131static __be32
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001132nfsd4_decode_putpubfh(struct nfsd4_compoundargs *argp, void *p)
1133{
1134 if (argp->minorversion == 0)
1135 return nfs_ok;
1136 return nfserr_notsupp;
1137}
1138
1139static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
1141{
1142 DECODE_HEAD;
1143
Benny Halevye31a1b62008-08-12 20:46:18 +03001144 status = nfsd4_decode_stateid(argp, &read->rd_stateid);
1145 if (status)
1146 return status;
1147 READ_BUF(12);
J. Bruce Fields542d1ab2014-06-02 12:45:31 -04001148 p = xdr_decode_hyper(p, &read->rd_offset);
J. Bruce Fields06553992014-06-02 12:32:51 -04001149 read->rd_length = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 DECODE_TAIL;
1152}
1153
Al Virob37ad282006-10-19 23:28:59 -07001154static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
1156{
1157 DECODE_HEAD;
1158
1159 READ_BUF(24);
J. Bruce Fields542d1ab2014-06-02 12:45:31 -04001160 p = xdr_decode_hyper(p, &readdir->rd_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
J. Bruce Fields06553992014-06-02 12:32:51 -04001162 readdir->rd_dircount = be32_to_cpup(p++);
1163 readdir->rd_maxcount = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
1165 goto out;
1166
1167 DECODE_TAIL;
1168}
1169
Al Virob37ad282006-10-19 23:28:59 -07001170static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
1172{
1173 DECODE_HEAD;
1174
1175 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001176 remove->rm_namelen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 READ_BUF(remove->rm_namelen);
1178 SAVEMEM(remove->rm_name, remove->rm_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001179 if ((status = check_filename(remove->rm_name, remove->rm_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 return status;
1181
1182 DECODE_TAIL;
1183}
1184
Al Virob37ad282006-10-19 23:28:59 -07001185static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
1187{
1188 DECODE_HEAD;
1189
1190 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001191 rename->rn_snamelen = be32_to_cpup(p++);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05001192 READ_BUF(rename->rn_snamelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 SAVEMEM(rename->rn_sname, rename->rn_snamelen);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05001194 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001195 rename->rn_tnamelen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 READ_BUF(rename->rn_tnamelen);
1197 SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001198 if ((status = check_filename(rename->rn_sname, rename->rn_snamelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 return status;
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001200 if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return status;
1202
1203 DECODE_TAIL;
1204}
1205
Al Virob37ad282006-10-19 23:28:59 -07001206static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
1208{
1209 DECODE_HEAD;
1210
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001211 if (argp->minorversion >= 1)
1212 return nfserr_notsupp;
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 READ_BUF(sizeof(clientid_t));
1215 COPYMEM(clientid, sizeof(clientid_t));
1216
1217 DECODE_TAIL;
1218}
1219
Al Virob37ad282006-10-19 23:28:59 -07001220static __be32
Andy Adamsondcb488a32007-07-17 04:04:51 -07001221nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
1222 struct nfsd4_secinfo *secinfo)
1223{
1224 DECODE_HEAD;
1225
1226 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001227 secinfo->si_namelen = be32_to_cpup(p++);
Andy Adamsondcb488a32007-07-17 04:04:51 -07001228 READ_BUF(secinfo->si_namelen);
1229 SAVEMEM(secinfo->si_name, secinfo->si_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001230 status = check_filename(secinfo->si_name, secinfo->si_namelen);
Andy Adamsondcb488a32007-07-17 04:04:51 -07001231 if (status)
1232 return status;
1233 DECODE_TAIL;
1234}
1235
1236static __be32
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05001237nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
1238 struct nfsd4_secinfo_no_name *sin)
1239{
1240 DECODE_HEAD;
1241
1242 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001243 sin->sin_style = be32_to_cpup(p++);
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05001244 DECODE_TAIL;
1245}
1246
1247static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
1249{
Benny Halevye31a1b62008-08-12 20:46:18 +03001250 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Benny Halevye31a1b62008-08-12 20:46:18 +03001252 status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
1253 if (status)
1254 return status;
Chuck Leverd1c263a2020-11-03 12:56:05 -05001255 return nfsd4_decode_fattr4(argp, setattr->sa_bmval,
1256 ARRAY_SIZE(setattr->sa_bmval),
1257 &setattr->sa_iattr, &setattr->sa_acl,
1258 &setattr->sa_label, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259}
1260
Al Virob37ad282006-10-19 23:28:59 -07001261static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
1263{
1264 DECODE_HEAD;
1265
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001266 if (argp->minorversion >= 1)
1267 return nfserr_notsupp;
1268
Chuck Leverab4684d2012-03-02 17:13:50 -05001269 READ_BUF(NFS4_VERIFIER_SIZE);
1270 COPYMEM(setclientid->se_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001272 status = nfsd4_decode_opaque(argp, &setclientid->se_name);
1273 if (status)
1274 return nfserr_bad_xdr;
1275 READ_BUF(8);
J. Bruce Fields06553992014-06-02 12:32:51 -04001276 setclientid->se_callback_prog = be32_to_cpup(p++);
1277 setclientid->se_callback_netid_len = be32_to_cpup(p++);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05001278 READ_BUF(setclientid->se_callback_netid_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05001280 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001281 setclientid->se_callback_addr_len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05001283 READ_BUF(setclientid->se_callback_addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05001285 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001286 setclientid->se_callback_ident = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 DECODE_TAIL;
1289}
1290
Al Virob37ad282006-10-19 23:28:59 -07001291static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
1293{
1294 DECODE_HEAD;
1295
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001296 if (argp->minorversion >= 1)
1297 return nfserr_notsupp;
1298
Chuck Leverab4684d2012-03-02 17:13:50 -05001299 READ_BUF(8 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 COPYMEM(&scd_c->sc_clientid, 8);
Chuck Leverab4684d2012-03-02 17:13:50 -05001301 COPYMEM(&scd_c->sc_confirm, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 DECODE_TAIL;
1304}
1305
1306/* Also used for NVERIFY */
Al Virob37ad282006-10-19 23:28:59 -07001307static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1309{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 DECODE_HEAD;
1311
1312 if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
1313 goto out;
1314
1315 /* For convenience's sake, we compare raw xdr'd attributes in
J. Bruce Fieldse5f95702012-11-30 17:24:18 -05001316 * nfsd4_proc_verify */
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001319 verify->ve_attrlen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 READ_BUF(verify->ve_attrlen);
1321 SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
1322
1323 DECODE_TAIL;
1324}
1325
Al Virob37ad282006-10-19 23:28:59 -07001326static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1328{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 DECODE_HEAD;
1330
Benny Halevye31a1b62008-08-12 20:46:18 +03001331 status = nfsd4_decode_stateid(argp, &write->wr_stateid);
1332 if (status)
1333 return status;
1334 READ_BUF(16);
J. Bruce Fields542d1ab2014-06-02 12:45:31 -04001335 p = xdr_decode_hyper(p, &write->wr_offset);
J. Bruce Fields06553992014-06-02 12:32:51 -04001336 write->wr_stable_how = be32_to_cpup(p++);
Kinglong Mee54bbb7d2016-12-31 20:59:53 +08001337 if (write->wr_stable_how > NFS_FILE_SYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 goto xdr_error;
J. Bruce Fields06553992014-06-02 12:32:51 -04001339 write->wr_buflen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Chuck Leverc1346a122020-11-03 11:54:23 -05001341 if (!xdr_stream_subsegment(argp->xdr, &write->wr_payload, write->wr_buflen))
1342 goto xdr_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 DECODE_TAIL;
1345}
1346
Al Virob37ad282006-10-19 23:28:59 -07001347static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1349{
1350 DECODE_HEAD;
1351
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001352 if (argp->minorversion >= 1)
1353 return nfserr_notsupp;
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 READ_BUF(12);
1356 COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
J. Bruce Fields06553992014-06-02 12:32:51 -04001357 rlockowner->rl_owner.len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 READ_BUF(rlockowner->rl_owner.len);
1359 READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
1360
Andy Adamson60adfc52009-04-03 08:28:50 +03001361 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1362 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 DECODE_TAIL;
1364}
1365
Al Virob37ad282006-10-19 23:28:59 -07001366static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001367nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
Andy Adamson0733d212009-04-03 08:28:01 +03001368 struct nfsd4_exchange_id *exid)
Andy Adamson2db134e2009-04-03 08:27:55 +03001369{
Mi Jinlong5afa0402010-11-09 09:39:23 +08001370 int dummy, tmp;
Andy Adamson0733d212009-04-03 08:28:01 +03001371 DECODE_HEAD;
1372
1373 READ_BUF(NFS4_VERIFIER_SIZE);
1374 COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
1375
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001376 status = nfsd4_decode_opaque(argp, &exid->clname);
1377 if (status)
1378 return nfserr_bad_xdr;
Andy Adamson0733d212009-04-03 08:28:01 +03001379
1380 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001381 exid->flags = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001382
1383 /* Ignore state_protect4_a */
1384 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001385 exid->spa_how = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001386 switch (exid->spa_how) {
1387 case SP4_NONE:
1388 break;
1389 case SP4_MACH_CRED:
1390 /* spo_must_enforce */
Andrew Elbleed941642016-06-15 12:52:09 -04001391 status = nfsd4_decode_bitmap(argp,
1392 exid->spo_must_enforce);
1393 if (status)
1394 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001395 /* spo_must_allow */
Andrew Elbleed941642016-06-15 12:52:09 -04001396 status = nfsd4_decode_bitmap(argp, exid->spo_must_allow);
1397 if (status)
1398 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001399 break;
1400 case SP4_SSV:
1401 /* ssp_ops */
1402 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001403 dummy = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001404 READ_BUF(dummy * 4);
1405 p += dummy;
1406
1407 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001408 dummy = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001409 READ_BUF(dummy * 4);
1410 p += dummy;
1411
1412 /* ssp_hash_algs<> */
1413 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001414 tmp = be32_to_cpup(p++);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001415 while (tmp--) {
1416 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001417 dummy = be32_to_cpup(p++);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001418 READ_BUF(dummy);
1419 p += XDR_QUADLEN(dummy);
1420 }
Andy Adamson0733d212009-04-03 08:28:01 +03001421
1422 /* ssp_encr_algs<> */
1423 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001424 tmp = be32_to_cpup(p++);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001425 while (tmp--) {
1426 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001427 dummy = be32_to_cpup(p++);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001428 READ_BUF(dummy);
1429 p += XDR_QUADLEN(dummy);
1430 }
Andy Adamson0733d212009-04-03 08:28:01 +03001431
nixiaoming5ed96bc2018-07-23 09:57:11 +08001432 /* ignore ssp_window and ssp_num_gss_handles: */
Andy Adamson0733d212009-04-03 08:28:01 +03001433 READ_BUF(8);
Andy Adamson0733d212009-04-03 08:28:01 +03001434 break;
1435 default:
1436 goto xdr_error;
1437 }
1438
Andy Adamson0733d212009-04-03 08:28:01 +03001439 READ_BUF(4); /* nfs_impl_id4 array length */
J. Bruce Fields06553992014-06-02 12:32:51 -04001440 dummy = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001441
1442 if (dummy > 1)
1443 goto xdr_error;
1444
1445 if (dummy == 1) {
J. Bruce Fields79123442019-06-05 12:42:05 -04001446 status = nfsd4_decode_opaque(argp, &exid->nii_domain);
1447 if (status)
1448 goto xdr_error;
Andy Adamson0733d212009-04-03 08:28:01 +03001449
1450 /* nii_name */
J. Bruce Fields79123442019-06-05 12:42:05 -04001451 status = nfsd4_decode_opaque(argp, &exid->nii_name);
1452 if (status)
1453 goto xdr_error;
Andy Adamson0733d212009-04-03 08:28:01 +03001454
1455 /* nii_date */
J. Bruce Fields79123442019-06-05 12:42:05 -04001456 status = nfsd4_decode_time(argp, &exid->nii_time);
1457 if (status)
1458 goto xdr_error;
Andy Adamson0733d212009-04-03 08:28:01 +03001459 }
1460 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001461}
1462
1463static __be32
1464nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1465 struct nfsd4_create_session *sess)
1466{
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001467 DECODE_HEAD;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001468
1469 READ_BUF(16);
1470 COPYMEM(&sess->clientid, 8);
J. Bruce Fields06553992014-06-02 12:32:51 -04001471 sess->seqid = be32_to_cpup(p++);
1472 sess->flags = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001473
1474 /* Fore channel attrs */
1475 READ_BUF(28);
Trond Myklebustb96811c2019-08-18 14:18:58 -04001476 p++; /* headerpadsz is always 0 */
J. Bruce Fields06553992014-06-02 12:32:51 -04001477 sess->fore_channel.maxreq_sz = be32_to_cpup(p++);
1478 sess->fore_channel.maxresp_sz = be32_to_cpup(p++);
1479 sess->fore_channel.maxresp_cached = be32_to_cpup(p++);
1480 sess->fore_channel.maxops = be32_to_cpup(p++);
1481 sess->fore_channel.maxreqs = be32_to_cpup(p++);
1482 sess->fore_channel.nr_rdma_attrs = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001483 if (sess->fore_channel.nr_rdma_attrs == 1) {
1484 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001485 sess->fore_channel.rdma_attrs = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001486 } else if (sess->fore_channel.nr_rdma_attrs > 1) {
1487 dprintk("Too many fore channel attr bitmaps!\n");
1488 goto xdr_error;
1489 }
1490
1491 /* Back channel attrs */
1492 READ_BUF(28);
Trond Myklebustb96811c2019-08-18 14:18:58 -04001493 p++; /* headerpadsz is always 0 */
J. Bruce Fields06553992014-06-02 12:32:51 -04001494 sess->back_channel.maxreq_sz = be32_to_cpup(p++);
1495 sess->back_channel.maxresp_sz = be32_to_cpup(p++);
1496 sess->back_channel.maxresp_cached = be32_to_cpup(p++);
1497 sess->back_channel.maxops = be32_to_cpup(p++);
1498 sess->back_channel.maxreqs = be32_to_cpup(p++);
1499 sess->back_channel.nr_rdma_attrs = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001500 if (sess->back_channel.nr_rdma_attrs == 1) {
1501 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001502 sess->back_channel.rdma_attrs = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001503 } else if (sess->back_channel.nr_rdma_attrs > 1) {
1504 dprintk("Too many back channel attr bitmaps!\n");
1505 goto xdr_error;
1506 }
1507
J. Bruce Fieldsacb28872012-03-27 14:50:26 -04001508 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001509 sess->callback_prog = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -04001510 nfsd4_decode_cb_sec(argp, &sess->cb_sec);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001511 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001512}
1513
1514static __be32
1515nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1516 struct nfsd4_destroy_session *destroy_session)
1517{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001518 DECODE_HEAD;
1519 READ_BUF(NFS4_MAX_SESSIONID_LEN);
1520 COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1521
1522 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001523}
1524
1525static __be32
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001526nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1527 struct nfsd4_free_stateid *free_stateid)
1528{
1529 DECODE_HEAD;
1530
1531 READ_BUF(sizeof(stateid_t));
J. Bruce Fields06553992014-06-02 12:32:51 -04001532 free_stateid->fr_stateid.si_generation = be32_to_cpup(p++);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001533 COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
1534
1535 DECODE_TAIL;
1536}
1537
1538static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001539nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1540 struct nfsd4_sequence *seq)
1541{
Benny Halevyb85d4c02009-04-03 08:28:08 +03001542 DECODE_HEAD;
1543
1544 READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
1545 COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields06553992014-06-02 12:32:51 -04001546 seq->seqid = be32_to_cpup(p++);
1547 seq->slotid = be32_to_cpup(p++);
1548 seq->maxslots = be32_to_cpup(p++);
1549 seq->cachethis = be32_to_cpup(p++);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001550
1551 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001552}
1553
Bryan Schumaker17456802011-07-13 10:50:48 -04001554static __be32
1555nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1556{
Bryan Schumaker17456802011-07-13 10:50:48 -04001557 int i;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001558 __be32 *p, status;
1559 struct nfsd4_test_stateid_id *stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04001560
1561 READ_BUF(4);
1562 test_stateid->ts_num_ids = ntohl(*p++);
1563
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001564 INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
Bryan Schumaker17456802011-07-13 10:50:48 -04001565
1566 for (i = 0; i < test_stateid->ts_num_ids; i++) {
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -04001567 stateid = svcxdr_tmpalloc(argp, sizeof(*stateid));
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001568 if (!stateid) {
Al Viroafcf6792012-04-13 00:15:37 -04001569 status = nfserrno(-ENOMEM);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001570 goto out;
1571 }
1572
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001573 INIT_LIST_HEAD(&stateid->ts_id_list);
1574 list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
1575
1576 status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
Bryan Schumaker17456802011-07-13 10:50:48 -04001577 if (status)
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001578 goto out;
Bryan Schumaker17456802011-07-13 10:50:48 -04001579 }
1580
1581 status = 0;
1582out:
1583 return status;
1584xdr_error:
1585 dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
1586 status = nfserr_bad_xdr;
1587 goto out;
1588}
1589
Mi Jinlong345c2842011-10-20 17:51:39 +08001590static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
1591{
1592 DECODE_HEAD;
1593
1594 READ_BUF(8);
1595 COPYMEM(&dc->clientid, 8);
1596
1597 DECODE_TAIL;
1598}
1599
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001600static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
1601{
1602 DECODE_HEAD;
1603
1604 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001605 rc->rca_one_fs = be32_to_cpup(p++);
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001606
1607 DECODE_TAIL;
1608}
1609
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001610#ifdef CONFIG_NFSD_PNFS
1611static __be32
1612nfsd4_decode_getdeviceinfo(struct nfsd4_compoundargs *argp,
1613 struct nfsd4_getdeviceinfo *gdev)
1614{
1615 DECODE_HEAD;
1616 u32 num, i;
1617
1618 READ_BUF(sizeof(struct nfsd4_deviceid) + 3 * 4);
1619 COPYMEM(&gdev->gd_devid, sizeof(struct nfsd4_deviceid));
1620 gdev->gd_layout_type = be32_to_cpup(p++);
1621 gdev->gd_maxcount = be32_to_cpup(p++);
1622 num = be32_to_cpup(p++);
1623 if (num) {
Scott Mayhew3171822f2018-06-08 16:31:46 -04001624 if (num > 1000)
1625 goto xdr_error;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001626 READ_BUF(4 * num);
1627 gdev->gd_notify_types = be32_to_cpup(p++);
1628 for (i = 1; i < num; i++) {
1629 if (be32_to_cpup(p++)) {
1630 status = nfserr_inval;
1631 goto out;
1632 }
1633 }
1634 }
1635 DECODE_TAIL;
1636}
1637
1638static __be32
1639nfsd4_decode_layoutget(struct nfsd4_compoundargs *argp,
1640 struct nfsd4_layoutget *lgp)
1641{
1642 DECODE_HEAD;
1643
1644 READ_BUF(36);
1645 lgp->lg_signal = be32_to_cpup(p++);
1646 lgp->lg_layout_type = be32_to_cpup(p++);
1647 lgp->lg_seg.iomode = be32_to_cpup(p++);
1648 p = xdr_decode_hyper(p, &lgp->lg_seg.offset);
1649 p = xdr_decode_hyper(p, &lgp->lg_seg.length);
1650 p = xdr_decode_hyper(p, &lgp->lg_minlength);
Kinglong Meedb59c0e2015-03-19 19:04:41 +08001651
1652 status = nfsd4_decode_stateid(argp, &lgp->lg_sid);
1653 if (status)
1654 return status;
1655
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001656 READ_BUF(4);
1657 lgp->lg_maxcount = be32_to_cpup(p++);
1658
1659 DECODE_TAIL;
1660}
1661
1662static __be32
1663nfsd4_decode_layoutcommit(struct nfsd4_compoundargs *argp,
1664 struct nfsd4_layoutcommit *lcp)
1665{
1666 DECODE_HEAD;
1667 u32 timechange;
1668
1669 READ_BUF(20);
1670 p = xdr_decode_hyper(p, &lcp->lc_seg.offset);
1671 p = xdr_decode_hyper(p, &lcp->lc_seg.length);
1672 lcp->lc_reclaim = be32_to_cpup(p++);
Kinglong Meedb59c0e2015-03-19 19:04:41 +08001673
1674 status = nfsd4_decode_stateid(argp, &lcp->lc_sid);
1675 if (status)
1676 return status;
1677
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001678 READ_BUF(4);
1679 lcp->lc_newoffset = be32_to_cpup(p++);
1680 if (lcp->lc_newoffset) {
1681 READ_BUF(8);
1682 p = xdr_decode_hyper(p, &lcp->lc_last_wr);
1683 } else
1684 lcp->lc_last_wr = 0;
1685 READ_BUF(4);
1686 timechange = be32_to_cpup(p++);
1687 if (timechange) {
1688 status = nfsd4_decode_time(argp, &lcp->lc_mtime);
1689 if (status)
1690 return status;
1691 } else {
1692 lcp->lc_mtime.tv_nsec = UTIME_NOW;
1693 }
1694 READ_BUF(8);
1695 lcp->lc_layout_type = be32_to_cpup(p++);
1696
1697 /*
1698 * Save the layout update in XDR format and let the layout driver deal
1699 * with it later.
1700 */
1701 lcp->lc_up_len = be32_to_cpup(p++);
1702 if (lcp->lc_up_len > 0) {
1703 READ_BUF(lcp->lc_up_len);
1704 READMEM(lcp->lc_up_layout, lcp->lc_up_len);
1705 }
1706
1707 DECODE_TAIL;
1708}
1709
1710static __be32
1711nfsd4_decode_layoutreturn(struct nfsd4_compoundargs *argp,
1712 struct nfsd4_layoutreturn *lrp)
1713{
1714 DECODE_HEAD;
1715
1716 READ_BUF(16);
1717 lrp->lr_reclaim = be32_to_cpup(p++);
1718 lrp->lr_layout_type = be32_to_cpup(p++);
1719 lrp->lr_seg.iomode = be32_to_cpup(p++);
1720 lrp->lr_return_type = be32_to_cpup(p++);
1721 if (lrp->lr_return_type == RETURN_FILE) {
1722 READ_BUF(16);
1723 p = xdr_decode_hyper(p, &lrp->lr_seg.offset);
1724 p = xdr_decode_hyper(p, &lrp->lr_seg.length);
Kinglong Meedb59c0e2015-03-19 19:04:41 +08001725
1726 status = nfsd4_decode_stateid(argp, &lrp->lr_sid);
1727 if (status)
1728 return status;
1729
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001730 READ_BUF(4);
1731 lrp->lrf_body_len = be32_to_cpup(p++);
1732 if (lrp->lrf_body_len > 0) {
1733 READ_BUF(lrp->lrf_body_len);
1734 READMEM(lrp->lrf_body, lrp->lrf_body_len);
1735 }
1736 } else {
1737 lrp->lr_seg.offset = 0;
1738 lrp->lr_seg.length = NFS4_MAX_UINT64;
1739 }
1740
1741 DECODE_TAIL;
1742}
1743#endif /* CONFIG_NFSD_PNFS */
1744
Andy Adamson2db134e2009-04-03 08:27:55 +03001745static __be32
Anna Schumaker95d871f2014-11-07 14:44:26 -05001746nfsd4_decode_fallocate(struct nfsd4_compoundargs *argp,
1747 struct nfsd4_fallocate *fallocate)
1748{
1749 DECODE_HEAD;
1750
1751 status = nfsd4_decode_stateid(argp, &fallocate->falloc_stateid);
1752 if (status)
1753 return status;
1754
1755 READ_BUF(16);
1756 p = xdr_decode_hyper(p, &fallocate->falloc_offset);
1757 xdr_decode_hyper(p, &fallocate->falloc_length);
1758
1759 DECODE_TAIL;
1760}
1761
1762static __be32
Christoph Hellwigffa01602015-12-03 12:59:52 +01001763nfsd4_decode_clone(struct nfsd4_compoundargs *argp, struct nfsd4_clone *clone)
1764{
1765 DECODE_HEAD;
1766
1767 status = nfsd4_decode_stateid(argp, &clone->cl_src_stateid);
1768 if (status)
1769 return status;
1770 status = nfsd4_decode_stateid(argp, &clone->cl_dst_stateid);
1771 if (status)
1772 return status;
1773
1774 READ_BUF(8 + 8 + 8);
1775 p = xdr_decode_hyper(p, &clone->cl_src_pos);
1776 p = xdr_decode_hyper(p, &clone->cl_dst_pos);
1777 p = xdr_decode_hyper(p, &clone->cl_count);
1778 DECODE_TAIL;
1779}
1780
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001781static __be32 nfsd4_decode_nl4_server(struct nfsd4_compoundargs *argp,
1782 struct nl4_server *ns)
1783{
1784 DECODE_HEAD;
1785 struct nfs42_netaddr *naddr;
1786
1787 READ_BUF(4);
1788 ns->nl4_type = be32_to_cpup(p++);
1789
1790 /* currently support for 1 inter-server source server */
1791 switch (ns->nl4_type) {
1792 case NL4_NETADDR:
1793 naddr = &ns->u.nl4_addr;
1794
1795 READ_BUF(4);
1796 naddr->netid_len = be32_to_cpup(p++);
1797 if (naddr->netid_len > RPCBIND_MAXNETIDLEN)
1798 goto xdr_error;
1799
1800 READ_BUF(naddr->netid_len + 4); /* 4 for uaddr len */
1801 COPYMEM(naddr->netid, naddr->netid_len);
1802
1803 naddr->addr_len = be32_to_cpup(p++);
1804 if (naddr->addr_len > RPCBIND_MAXUADDRLEN)
1805 goto xdr_error;
1806
1807 READ_BUF(naddr->addr_len);
1808 COPYMEM(naddr->addr, naddr->addr_len);
1809 break;
1810 default:
1811 goto xdr_error;
1812 }
1813 DECODE_TAIL;
1814}
1815
Christoph Hellwigffa01602015-12-03 12:59:52 +01001816static __be32
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001817nfsd4_decode_copy(struct nfsd4_compoundargs *argp, struct nfsd4_copy *copy)
1818{
1819 DECODE_HEAD;
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001820 struct nl4_server *ns_dummy;
1821 int i, count;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001822
1823 status = nfsd4_decode_stateid(argp, &copy->cp_src_stateid);
1824 if (status)
1825 return status;
1826 status = nfsd4_decode_stateid(argp, &copy->cp_dst_stateid);
1827 if (status)
1828 return status;
1829
1830 READ_BUF(8 + 8 + 8 + 4 + 4 + 4);
1831 p = xdr_decode_hyper(p, &copy->cp_src_pos);
1832 p = xdr_decode_hyper(p, &copy->cp_dst_pos);
1833 p = xdr_decode_hyper(p, &copy->cp_count);
J. Bruce Fieldsedcc8452018-03-07 15:37:35 -05001834 p++; /* ca_consecutive: we always do consecutive copies */
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001835 copy->cp_synchronous = be32_to_cpup(p++);
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001836
1837 count = be32_to_cpup(p++);
1838
1839 copy->cp_intra = false;
1840 if (count == 0) { /* intra-server copy */
1841 copy->cp_intra = true;
1842 goto intra;
1843 }
1844
1845 /* decode all the supplied server addresses but use first */
1846 status = nfsd4_decode_nl4_server(argp, &copy->cp_src);
1847 if (status)
1848 return status;
1849
1850 ns_dummy = kmalloc(sizeof(struct nl4_server), GFP_KERNEL);
1851 if (ns_dummy == NULL)
1852 return nfserrno(-ENOMEM);
1853 for (i = 0; i < count - 1; i++) {
1854 status = nfsd4_decode_nl4_server(argp, ns_dummy);
1855 if (status) {
1856 kfree(ns_dummy);
1857 return status;
1858 }
1859 }
1860 kfree(ns_dummy);
1861intra:
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001862
1863 DECODE_TAIL;
1864}
1865
1866static __be32
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04001867nfsd4_decode_offload_status(struct nfsd4_compoundargs *argp,
1868 struct nfsd4_offload_status *os)
1869{
1870 return nfsd4_decode_stateid(argp, &os->stateid);
1871}
1872
1873static __be32
Olga Kornievskaia51911862019-08-08 11:14:59 -04001874nfsd4_decode_copy_notify(struct nfsd4_compoundargs *argp,
1875 struct nfsd4_copy_notify *cn)
1876{
Chuck Lever5aff7d02020-09-11 14:47:59 -04001877 __be32 status;
Olga Kornievskaia51911862019-08-08 11:14:59 -04001878
1879 status = nfsd4_decode_stateid(argp, &cn->cpn_src_stateid);
1880 if (status)
1881 return status;
1882 return nfsd4_decode_nl4_server(argp, &cn->cpn_dst);
1883}
1884
1885static __be32
Anna Schumaker24bab492014-09-26 13:58:27 -04001886nfsd4_decode_seek(struct nfsd4_compoundargs *argp, struct nfsd4_seek *seek)
1887{
1888 DECODE_HEAD;
1889
1890 status = nfsd4_decode_stateid(argp, &seek->seek_stateid);
1891 if (status)
1892 return status;
1893
1894 READ_BUF(8 + 4);
1895 p = xdr_decode_hyper(p, &seek->seek_offset);
1896 seek->seek_whence = be32_to_cpup(p);
1897
1898 DECODE_TAIL;
1899}
1900
Frank van der Linden23e50fe2020-06-23 22:39:26 +00001901/*
1902 * XDR data that is more than PAGE_SIZE in size is normally part of a
1903 * read or write. However, the size of extended attributes is limited
1904 * by the maximum request size, and then further limited by the underlying
1905 * filesystem limits. This can exceed PAGE_SIZE (currently, XATTR_SIZE_MAX
1906 * is 64k). Since there is no kvec- or page-based interface to xattrs,
1907 * and we're not dealing with contiguous pages, we need to do some copying.
1908 */
1909
1910/*
Chuck Leverc1346a122020-11-03 11:54:23 -05001911 * Decode data into buffer.
Frank van der Linden23e50fe2020-06-23 22:39:26 +00001912 */
1913static __be32
Chuck Leverc1346a122020-11-03 11:54:23 -05001914nfsd4_vbuf_from_vector(struct nfsd4_compoundargs *argp, struct xdr_buf *xdr,
1915 char **bufp, u32 buflen)
Frank van der Linden23e50fe2020-06-23 22:39:26 +00001916{
Chuck Leverc1346a122020-11-03 11:54:23 -05001917 struct page **pages = xdr->pages;
1918 struct kvec *head = xdr->head;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00001919 char *tmp, *dp;
1920 u32 len;
1921
1922 if (buflen <= head->iov_len) {
1923 /*
1924 * We're in luck, the head has enough space. Just return
1925 * the head, no need for copying.
1926 */
1927 *bufp = head->iov_base;
1928 return 0;
1929 }
1930
1931 tmp = svcxdr_tmpalloc(argp, buflen);
1932 if (tmp == NULL)
1933 return nfserr_jukebox;
1934
1935 dp = tmp;
1936 memcpy(dp, head->iov_base, head->iov_len);
1937 buflen -= head->iov_len;
1938 dp += head->iov_len;
1939
1940 while (buflen > 0) {
1941 len = min_t(u32, buflen, PAGE_SIZE);
1942 memcpy(dp, page_address(*pages), len);
1943
1944 buflen -= len;
1945 dp += len;
1946 pages++;
1947 }
1948
1949 *bufp = tmp;
1950 return 0;
1951}
1952
1953/*
1954 * Get a user extended attribute name from the XDR buffer.
1955 * It will not have the "user." prefix, so prepend it.
1956 * Lastly, check for nul characters in the name.
1957 */
1958static __be32
1959nfsd4_decode_xattr_name(struct nfsd4_compoundargs *argp, char **namep)
1960{
1961 DECODE_HEAD;
1962 char *name, *sp, *dp;
1963 u32 namelen, cnt;
1964
1965 READ_BUF(4);
1966 namelen = be32_to_cpup(p++);
1967
1968 if (namelen > (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN))
1969 return nfserr_nametoolong;
1970
1971 if (namelen == 0)
1972 goto xdr_error;
1973
1974 READ_BUF(namelen);
1975
1976 name = svcxdr_tmpalloc(argp, namelen + XATTR_USER_PREFIX_LEN + 1);
1977 if (!name)
1978 return nfserr_jukebox;
1979
1980 memcpy(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
1981
1982 /*
1983 * Copy the extended attribute name over while checking for 0
1984 * characters.
1985 */
1986 sp = (char *)p;
1987 dp = name + XATTR_USER_PREFIX_LEN;
1988 cnt = namelen;
1989
1990 while (cnt-- > 0) {
1991 if (*sp == '\0')
1992 goto xdr_error;
1993 *dp++ = *sp++;
1994 }
1995 *dp = '\0';
1996
1997 *namep = name;
1998
1999 DECODE_TAIL;
2000}
2001
2002/*
2003 * A GETXATTR op request comes without a length specifier. We just set the
2004 * maximum length for the reply based on XATTR_SIZE_MAX and the maximum
2005 * channel reply size. nfsd_getxattr will probe the length of the xattr,
2006 * check it against getxa_len, and allocate + return the value.
2007 */
2008static __be32
2009nfsd4_decode_getxattr(struct nfsd4_compoundargs *argp,
2010 struct nfsd4_getxattr *getxattr)
2011{
2012 __be32 status;
2013 u32 maxcount;
2014
2015 status = nfsd4_decode_xattr_name(argp, &getxattr->getxa_name);
2016 if (status)
2017 return status;
2018
2019 maxcount = svc_max_payload(argp->rqstp);
2020 maxcount = min_t(u32, XATTR_SIZE_MAX, maxcount);
2021
2022 getxattr->getxa_len = maxcount;
2023
2024 return status;
2025}
2026
2027static __be32
2028nfsd4_decode_setxattr(struct nfsd4_compoundargs *argp,
2029 struct nfsd4_setxattr *setxattr)
2030{
2031 DECODE_HEAD;
2032 u32 flags, maxcount, size;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002033
2034 READ_BUF(4);
2035 flags = be32_to_cpup(p++);
2036
2037 if (flags > SETXATTR4_REPLACE)
2038 return nfserr_inval;
2039 setxattr->setxa_flags = flags;
2040
2041 status = nfsd4_decode_xattr_name(argp, &setxattr->setxa_name);
2042 if (status)
2043 return status;
2044
2045 maxcount = svc_max_payload(argp->rqstp);
2046 maxcount = min_t(u32, XATTR_SIZE_MAX, maxcount);
2047
2048 READ_BUF(4);
2049 size = be32_to_cpup(p++);
2050 if (size > maxcount)
2051 return nfserr_xattr2big;
2052
2053 setxattr->setxa_len = size;
2054 if (size > 0) {
Chuck Leverc1346a122020-11-03 11:54:23 -05002055 struct xdr_buf payload;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002056
Chuck Leverc1346a122020-11-03 11:54:23 -05002057 if (!xdr_stream_subsegment(argp->xdr, &payload, size))
2058 goto xdr_error;
2059 status = nfsd4_vbuf_from_vector(argp, &payload,
2060 &setxattr->setxa_buf, size);
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002061 }
2062
2063 DECODE_TAIL;
2064}
2065
2066static __be32
2067nfsd4_decode_listxattrs(struct nfsd4_compoundargs *argp,
2068 struct nfsd4_listxattrs *listxattrs)
2069{
2070 DECODE_HEAD;
2071 u32 maxcount;
2072
2073 READ_BUF(12);
2074 p = xdr_decode_hyper(p, &listxattrs->lsxa_cookie);
2075
2076 /*
2077 * If the cookie is too large to have even one user.x attribute
2078 * plus trailing '\0' left in a maximum size buffer, it's invalid.
2079 */
2080 if (listxattrs->lsxa_cookie >=
2081 (XATTR_LIST_MAX / (XATTR_USER_PREFIX_LEN + 2)))
2082 return nfserr_badcookie;
2083
2084 maxcount = be32_to_cpup(p++);
2085 if (maxcount < 8)
2086 /* Always need at least 2 words (length and one character) */
2087 return nfserr_inval;
2088
2089 maxcount = min(maxcount, svc_max_payload(argp->rqstp));
2090 listxattrs->lsxa_maxcount = maxcount;
2091
2092 DECODE_TAIL;
2093}
2094
2095static __be32
2096nfsd4_decode_removexattr(struct nfsd4_compoundargs *argp,
2097 struct nfsd4_removexattr *removexattr)
2098{
2099 return nfsd4_decode_xattr_name(argp, &removexattr->rmxa_name);
2100}
2101
Anna Schumaker24bab492014-09-26 13:58:27 -04002102static __be32
Benny Halevy347e0ad2008-07-02 11:13:41 +03002103nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
2104{
2105 return nfs_ok;
2106}
2107
Benny Halevy3c375c62008-07-02 11:14:01 +03002108static __be32
2109nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
2110{
Benny Halevy1e685ec2009-03-04 23:06:06 +02002111 return nfserr_notsupp;
Benny Halevy3c375c62008-07-02 11:14:01 +03002112}
2113
Benny Halevy347e0ad2008-07-02 11:13:41 +03002114typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
2115
Chuck Leverc1df6092017-08-01 11:59:58 -04002116static const nfsd4_dec nfsd4_dec_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04002117 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
2118 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
2119 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
2120 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
2121 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
2122 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
2123 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
2124 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
2125 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
2126 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
2127 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
2128 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
2129 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
2130 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
2131 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
2132 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
2133 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
2134 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
2135 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
2136 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002137 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_putpubfh,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04002138 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
2139 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
2140 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
2141 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
2142 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
2143 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
2144 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
2145 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
2146 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
2147 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
2148 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
2149 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
2150 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
2151 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
2152 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
2153 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
Andy Adamson2db134e2009-04-03 08:27:55 +03002154
2155 /* new operations for NFSv4.1 */
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002156 [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002157 [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
Randy Dunlap9064caa2009-04-28 16:48:25 -07002158 [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
2159 [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
2160 [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04002161 [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07002162 [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002163#ifdef CONFIG_NFSD_PNFS
2164 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_getdeviceinfo,
2165 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
2166 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_layoutcommit,
2167 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_layoutget,
2168 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_layoutreturn,
2169#else
Randy Dunlap9064caa2009-04-28 16:48:25 -07002170 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
2171 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
2172 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
2173 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
2174 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002175#endif
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05002176 [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
Randy Dunlap9064caa2009-04-28 16:48:25 -07002177 [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
2178 [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
Bryan Schumaker17456802011-07-13 10:50:48 -04002179 [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07002180 [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
Mi Jinlong345c2842011-10-20 17:51:39 +08002181 [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002182 [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
Anna Schumaker87a15a82014-09-26 13:58:26 -04002183
2184 /* new operations for NFSv4.2 */
Anna Schumaker95d871f2014-11-07 14:44:26 -05002185 [OP_ALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
Anna Schumaker29ae7f92016-09-07 15:57:30 -04002186 [OP_COPY] = (nfsd4_dec)nfsd4_decode_copy,
Olga Kornievskaia51911862019-08-08 11:14:59 -04002187 [OP_COPY_NOTIFY] = (nfsd4_dec)nfsd4_decode_copy_notify,
Anna Schumakerb0cb9082014-11-07 14:44:27 -05002188 [OP_DEALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
Anna Schumaker87a15a82014-09-26 13:58:26 -04002189 [OP_IO_ADVISE] = (nfsd4_dec)nfsd4_decode_notsupp,
2190 [OP_LAYOUTERROR] = (nfsd4_dec)nfsd4_decode_notsupp,
2191 [OP_LAYOUTSTATS] = (nfsd4_dec)nfsd4_decode_notsupp,
Olga Kornievskaia885e2bf2018-07-20 18:19:19 -04002192 [OP_OFFLOAD_CANCEL] = (nfsd4_dec)nfsd4_decode_offload_status,
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04002193 [OP_OFFLOAD_STATUS] = (nfsd4_dec)nfsd4_decode_offload_status,
Anna Schumaker528b8492020-09-28 13:08:58 -04002194 [OP_READ_PLUS] = (nfsd4_dec)nfsd4_decode_read,
Anna Schumaker24bab492014-09-26 13:58:27 -04002195 [OP_SEEK] = (nfsd4_dec)nfsd4_decode_seek,
Anna Schumaker87a15a82014-09-26 13:58:26 -04002196 [OP_WRITE_SAME] = (nfsd4_dec)nfsd4_decode_notsupp,
Christoph Hellwigffa01602015-12-03 12:59:52 +01002197 [OP_CLONE] = (nfsd4_dec)nfsd4_decode_clone,
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002198 /* RFC 8276 extended atributes operations */
2199 [OP_GETXATTR] = (nfsd4_dec)nfsd4_decode_getxattr,
2200 [OP_SETXATTR] = (nfsd4_dec)nfsd4_decode_setxattr,
2201 [OP_LISTXATTRS] = (nfsd4_dec)nfsd4_decode_listxattrs,
2202 [OP_REMOVEXATTR] = (nfsd4_dec)nfsd4_decode_removexattr,
Andy Adamson2db134e2009-04-03 08:27:55 +03002203};
2204
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002205static inline bool
2206nfsd4_opnum_in_range(struct nfsd4_compoundargs *argp, struct nfsd4_op *op)
2207{
Anna Schumaker8217d142013-10-30 13:38:13 -04002208 if (op->opnum < FIRST_NFS4_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002209 return false;
Anna Schumaker8217d142013-10-30 13:38:13 -04002210 else if (argp->minorversion == 0 && op->opnum > LAST_NFS40_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002211 return false;
Anna Schumaker8217d142013-10-30 13:38:13 -04002212 else if (argp->minorversion == 1 && op->opnum > LAST_NFS41_OP)
2213 return false;
2214 else if (argp->minorversion == 2 && op->opnum > LAST_NFS42_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002215 return false;
2216 return true;
2217}
Benny Halevyf2feb962008-07-02 11:14:22 +03002218
Benny Halevy347e0ad2008-07-02 11:13:41 +03002219static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
2221{
2222 DECODE_HEAD;
2223 struct nfsd4_op *op;
J. Bruce Fields10910062011-01-24 12:11:02 -05002224 bool cachethis = false;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002225 int auth_slack= argp->rqstp->rq_auth_slack;
2226 int max_reply = auth_slack + 8; /* opcnt, status */
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05002227 int readcount = 0;
2228 int readbytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 int i;
2230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04002232 argp->taglen = be32_to_cpup(p++);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05002233 READ_BUF(argp->taglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 SAVEMEM(argp->tag, argp->taglen);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05002235 READ_BUF(8);
J. Bruce Fields06553992014-06-02 12:32:51 -04002236 argp->minorversion = be32_to_cpup(p++);
2237 argp->opcnt = be32_to_cpup(p++);
J. Bruce Fields4f0cefb2014-03-11 15:39:13 -04002238 max_reply += 4 + (XDR_QUADLEN(argp->taglen) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
2240 if (argp->taglen > NFSD4_MAX_TAGLEN)
2241 goto xdr_error;
J. Bruce Fields00781172017-11-15 12:30:27 -05002242 /*
2243 * NFS4ERR_RESOURCE is a more helpful error than GARBAGE_ARGS
2244 * here, so we return success at the xdr level so that
2245 * nfsd4_proc can handle this is an NFS-level error.
2246 */
2247 if (argp->opcnt > NFSD_MAX_OPS_PER_COMPOUND)
2248 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Tobias Klausere8c96f82006-03-24 03:15:34 -08002250 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
J. Bruce Fields5d6031c2014-07-17 16:20:39 -04002251 argp->ops = kzalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 if (!argp->ops) {
2253 argp->ops = argp->iops;
Chuck Lever817cb9d2007-09-11 18:01:20 -04002254 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 goto xdr_error;
2256 }
2257 }
2258
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002259 if (argp->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
Benny Halevy30cff1f2008-07-02 11:13:18 +03002260 argp->opcnt = 0;
2261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 for (i = 0; i < argp->opcnt; i++) {
2263 op = &argp->ops[i];
2264 op->replay = NULL;
2265
J. Bruce Fields8a61b182012-11-16 22:28:38 -05002266 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04002267 op->opnum = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Chuck Lever08281342020-11-21 11:36:42 -05002269 if (nfsd4_opnum_in_range(argp, op)) {
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002270 op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
Chuck Lever08281342020-11-21 11:36:42 -05002271 if (op->status != nfs_ok)
2272 trace_nfsd_compound_decode_err(argp->rqstp,
2273 argp->opcnt, i,
2274 op->opnum,
2275 op->status);
2276 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 op->opnum = OP_ILLEGAL;
2278 op->status = nfserr_op_illegal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 }
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002280 op->opdesc = OPDESC(op);
J. Bruce Fields10910062011-01-24 12:11:02 -05002281 /*
2282 * We'll try to cache the result in the DRC if any one
2283 * op in the compound wants to be cached:
2284 */
2285 cachethis |= nfsd4_cache_this_op(op);
J. Bruce Fields6ff40de2013-11-05 15:07:16 -05002286
Anna Schumaker528b8492020-09-28 13:08:58 -04002287 if (op->opnum == OP_READ || op->opnum == OP_READ_PLUS) {
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05002288 readcount++;
2289 readbytes += nfsd4_max_reply(argp->rqstp, op);
2290 } else
2291 max_reply += nfsd4_max_reply(argp->rqstp, op);
J. Bruce Fieldsf7b43d02014-08-12 11:41:40 -04002292 /*
Kinglong Mee7323f0d2017-02-03 22:51:46 +08002293 * OP_LOCK and OP_LOCKT may return a conflicting lock.
2294 * (Special case because it will just skip encoding this
2295 * if it runs out of xdr buffer space, and it is the only
2296 * operation that behaves this way.)
J. Bruce Fieldsf7b43d02014-08-12 11:41:40 -04002297 */
Kinglong Mee7323f0d2017-02-03 22:51:46 +08002298 if (op->opnum == OP_LOCK || op->opnum == OP_LOCKT)
J. Bruce Fieldsf7b43d02014-08-12 11:41:40 -04002299 max_reply += NFS4_OPAQUE_LIMIT;
J. Bruce Fieldse372ba62014-05-19 12:27:11 -04002300
2301 if (op->status) {
2302 argp->opcnt = i+1;
2303 break;
2304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 }
J. Bruce Fields10910062011-01-24 12:11:02 -05002306 /* Sessions make the DRC unnecessary: */
2307 if (argp->minorversion)
2308 cachethis = false;
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05002309 svc_reserve(argp->rqstp, max_reply + readbytes);
J. Bruce Fields10910062011-01-24 12:11:02 -05002310 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002312 if (readcount > 1 || max_reply > PAGE_SIZE - auth_slack)
Jeff Layton779fb0f2014-11-19 07:51:18 -05002313 clear_bit(RQ_SPLICE_OK, &argp->rqstp->rq_flags);
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05002314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 DECODE_TAIL;
2316}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
NeilBrownb8800922017-01-30 17:17:00 +11002318static __be32 *encode_change(__be32 *p, struct kstat *stat, struct inode *inode,
2319 struct svc_export *exp)
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002320{
NeilBrownb8800922017-01-30 17:17:00 +11002321 if (exp->ex_flags & NFSEXP_V4ROOT) {
2322 *p++ = cpu_to_be32(convert_to_wallclock(exp->cd->flush_time));
2323 *p++ = 0;
2324 } else if (IS_I_VERSION(inode)) {
Amir Goldstein39ca1bf2018-01-03 17:14:35 +02002325 p = xdr_encode_hyper(p, nfsd4_change_attribute(stat, inode));
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002326 } else {
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002327 *p++ = cpu_to_be32(stat->ctime.tv_sec);
2328 *p++ = cpu_to_be32(stat->ctime.tv_nsec);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002329 }
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002330 return p;
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002331}
2332
J. Bruce Fields16945142018-04-25 14:34:11 -04002333/*
2334 * ctime (in NFSv4, time_metadata) is not writeable, and the client
2335 * doesn't really care what resolution could theoretically be stored by
2336 * the filesystem.
2337 *
2338 * The client cares how close together changes can be while still
2339 * guaranteeing ctime changes. For most filesystems (which have
2340 * timestamps with nanosecond fields) that is limited by the resolution
2341 * of the time returned from current_time() (which I'm assuming to be
2342 * 1/HZ).
2343 */
2344static __be32 *encode_time_delta(__be32 *p, struct inode *inode)
2345{
Arnd Bergmanne4598e32019-10-31 15:14:03 +01002346 struct timespec64 ts;
J. Bruce Fields16945142018-04-25 14:34:11 -04002347 u32 ns;
2348
2349 ns = max_t(u32, NSEC_PER_SEC/HZ, inode->i_sb->s_time_gran);
Arnd Bergmanne4598e32019-10-31 15:14:03 +01002350 ts = ns_to_timespec64(ns);
J. Bruce Fields16945142018-04-25 14:34:11 -04002351
2352 p = xdr_encode_hyper(p, ts.tv_sec);
2353 *p++ = cpu_to_be32(ts.tv_nsec);
2354
2355 return p;
2356}
2357
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002358static __be32 *encode_cinfo(__be32 *p, struct nfsd4_change_info *c)
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002359{
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002360 *p++ = cpu_to_be32(c->atomic);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002361 if (c->change_supported) {
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002362 p = xdr_encode_hyper(p, c->before_change);
2363 p = xdr_encode_hyper(p, c->after_change);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002364 } else {
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002365 *p++ = cpu_to_be32(c->before_ctime_sec);
2366 *p++ = cpu_to_be32(c->before_ctime_nsec);
2367 *p++ = cpu_to_be32(c->after_ctime_sec);
2368 *p++ = cpu_to_be32(c->after_ctime_nsec);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002369 }
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002370 return p;
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002371}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002373/* Encode as an array of strings the string given with components
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002374 * separated @sep, escaped with esc_enter and esc_exit.
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002375 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002376static __be32 nfsd4_encode_components_esc(struct xdr_stream *xdr, char sep,
2377 char *components, char esc_enter,
2378 char esc_exit)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002379{
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002380 __be32 *p;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002381 __be32 pathlen;
2382 int pathlen_offset;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002383 int strlen, count=0;
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002384 char *str, *end, *next;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002385
2386 dprintk("nfsd4_encode_components(%s)\n", components);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002387
2388 pathlen_offset = xdr->buf->len;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002389 p = xdr_reserve_space(xdr, 4);
2390 if (!p)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002391 return nfserr_resource;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002392 p++; /* We will fill this in with @count later */
2393
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002394 end = str = components;
2395 while (*end) {
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002396 bool found_esc = false;
2397
2398 /* try to parse as esc_start, ..., esc_end, sep */
2399 if (*str == esc_enter) {
2400 for (; *end && (*end != esc_exit); end++)
2401 /* find esc_exit or end of string */;
2402 next = end + 1;
2403 if (*end && (!*next || *next == sep)) {
2404 str++;
2405 found_esc = true;
2406 }
2407 }
2408
2409 if (!found_esc)
2410 for (; *end && (*end != sep); end++)
2411 /* find sep or end of string */;
2412
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002413 strlen = end - str;
2414 if (strlen) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002415 p = xdr_reserve_space(xdr, strlen + 4);
2416 if (!p)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002417 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04002418 p = xdr_encode_opaque(p, str, strlen);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002419 count++;
2420 }
2421 else
2422 end++;
Benjamin Coddington5a64e562014-12-07 16:05:47 -05002423 if (found_esc)
2424 end = next;
2425
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002426 str = end;
2427 }
Benjamin Coddingtonbf7491f2014-12-07 16:05:48 -05002428 pathlen = htonl(count);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002429 write_bytes_to_xdr_buf(xdr->buf, pathlen_offset, &pathlen, 4);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002430 return 0;
2431}
2432
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002433/* Encode as an array of strings the string given with components
2434 * separated @sep.
2435 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002436static __be32 nfsd4_encode_components(struct xdr_stream *xdr, char sep,
2437 char *components)
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002438{
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002439 return nfsd4_encode_components_esc(xdr, sep, components, 0, 0);
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002440}
2441
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002442/*
2443 * encode a location element of a fs_locations structure
2444 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002445static __be32 nfsd4_encode_fs_location4(struct xdr_stream *xdr,
2446 struct nfsd4_fs_location *location)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002447{
Al Virob37ad282006-10-19 23:28:59 -07002448 __be32 status;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002449
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002450 status = nfsd4_encode_components_esc(xdr, ':', location->hosts,
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002451 '[', ']');
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002452 if (status)
2453 return status;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002454 status = nfsd4_encode_components(xdr, '/', location->path);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002455 if (status)
2456 return status;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002457 return 0;
2458}
2459
2460/*
Trond Myklebusted748aa2011-09-12 19:37:06 -04002461 * Encode a path in RFC3530 'pathname4' format
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002462 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002463static __be32 nfsd4_encode_path(struct xdr_stream *xdr,
2464 const struct path *root,
2465 const struct path *path)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002466{
Al Viro301f0262013-09-01 16:06:39 -04002467 struct path cur = *path;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002468 __be32 *p;
Trond Myklebusted748aa2011-09-12 19:37:06 -04002469 struct dentry **components = NULL;
2470 unsigned int ncomponents = 0;
2471 __be32 err = nfserr_jukebox;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002472
Trond Myklebusted748aa2011-09-12 19:37:06 -04002473 dprintk("nfsd4_encode_components(");
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002474
Trond Myklebusted748aa2011-09-12 19:37:06 -04002475 path_get(&cur);
2476 /* First walk the path up to the nfsd root, and store the
2477 * dentries/path components in an array.
2478 */
2479 for (;;) {
Kinglong Meeb77a4b22015-03-15 23:13:32 +08002480 if (path_equal(&cur, root))
Trond Myklebusted748aa2011-09-12 19:37:06 -04002481 break;
2482 if (cur.dentry == cur.mnt->mnt_root) {
2483 if (follow_up(&cur))
2484 continue;
2485 goto out_free;
2486 }
2487 if ((ncomponents & 15) == 0) {
2488 struct dentry **new;
2489 new = krealloc(components,
2490 sizeof(*new) * (ncomponents + 16),
2491 GFP_KERNEL);
2492 if (!new)
2493 goto out_free;
2494 components = new;
2495 }
2496 components[ncomponents++] = cur.dentry;
2497 cur.dentry = dget_parent(cur.dentry);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002498 }
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002499 err = nfserr_resource;
2500 p = xdr_reserve_space(xdr, 4);
2501 if (!p)
Trond Myklebusted748aa2011-09-12 19:37:06 -04002502 goto out_free;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002503 *p++ = cpu_to_be32(ncomponents);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002504
2505 while (ncomponents) {
2506 struct dentry *dentry = components[ncomponents - 1];
Al Viro301f0262013-09-01 16:06:39 -04002507 unsigned int len;
Trond Myklebusted748aa2011-09-12 19:37:06 -04002508
Al Viro301f0262013-09-01 16:06:39 -04002509 spin_lock(&dentry->d_lock);
2510 len = dentry->d_name.len;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002511 p = xdr_reserve_space(xdr, len + 4);
2512 if (!p) {
Al Viro301f0262013-09-01 16:06:39 -04002513 spin_unlock(&dentry->d_lock);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002514 goto out_free;
Al Viro301f0262013-09-01 16:06:39 -04002515 }
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04002516 p = xdr_encode_opaque(p, dentry->d_name.name, len);
Al Viroa4555892014-10-21 20:11:25 -04002517 dprintk("/%pd", dentry);
Al Viro301f0262013-09-01 16:06:39 -04002518 spin_unlock(&dentry->d_lock);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002519 dput(dentry);
2520 ncomponents--;
2521 }
2522
Trond Myklebusted748aa2011-09-12 19:37:06 -04002523 err = 0;
2524out_free:
2525 dprintk(")\n");
2526 while (ncomponents)
2527 dput(components[--ncomponents]);
2528 kfree(components);
2529 path_put(&cur);
2530 return err;
2531}
2532
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002533static __be32 nfsd4_encode_fsloc_fsroot(struct xdr_stream *xdr,
2534 struct svc_rqst *rqstp, const struct path *path)
Trond Myklebusted748aa2011-09-12 19:37:06 -04002535{
2536 struct svc_export *exp_ps;
2537 __be32 res;
2538
2539 exp_ps = rqst_find_fsidzero_export(rqstp);
2540 if (IS_ERR(exp_ps))
2541 return nfserrno(PTR_ERR(exp_ps));
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002542 res = nfsd4_encode_path(xdr, &exp_ps->ex_path, path);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002543 exp_put(exp_ps);
2544 return res;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002545}
2546
2547/*
2548 * encode a fs_locations structure
2549 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002550static __be32 nfsd4_encode_fs_locations(struct xdr_stream *xdr,
2551 struct svc_rqst *rqstp, struct svc_export *exp)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002552{
Al Virob37ad282006-10-19 23:28:59 -07002553 __be32 status;
Al Virocc45f012006-10-19 23:28:44 -07002554 int i;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002555 __be32 *p;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002556 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002557
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002558 status = nfsd4_encode_fsloc_fsroot(xdr, rqstp, &exp->ex_path);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002559 if (status)
2560 return status;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002561 p = xdr_reserve_space(xdr, 4);
2562 if (!p)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002563 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002564 *p++ = cpu_to_be32(fslocs->locations_count);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002565 for (i=0; i<fslocs->locations_count; i++) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002566 status = nfsd4_encode_fs_location4(xdr, &fslocs->locations[i]);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002567 if (status)
2568 return status;
2569 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002570 return 0;
2571}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002573static u32 nfs4_file_type(umode_t mode)
2574{
2575 switch (mode & S_IFMT) {
2576 case S_IFIFO: return NF4FIFO;
2577 case S_IFCHR: return NF4CHR;
2578 case S_IFDIR: return NF4DIR;
2579 case S_IFBLK: return NF4BLK;
2580 case S_IFLNK: return NF4LNK;
2581 case S_IFREG: return NF4REG;
2582 case S_IFSOCK: return NF4SOCK;
2583 default: return NF4BAD;
Tom Rix25fef482020-11-01 07:32:34 -08002584 }
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002585}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
Al Virob37ad282006-10-19 23:28:59 -07002587static inline __be32
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002588nfsd4_encode_aclname(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2589 struct nfs4_ace *ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590{
J. Bruce Fields35541162014-01-08 09:49:01 -05002591 if (ace->whotype != NFS4_ACL_WHO_NAMED)
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002592 return nfs4_acl_write_who(xdr, ace->whotype);
J. Bruce Fields35541162014-01-08 09:49:01 -05002593 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002594 return nfsd4_encode_group(xdr, rqstp, ace->who_gid);
J. Bruce Fields35541162014-01-08 09:49:01 -05002595 else
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002596 return nfsd4_encode_user(xdr, rqstp, ace->who_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597}
2598
Kinglong Mee6896f152015-07-30 21:52:44 +08002599static inline __be32
Jeff Layton8a4c3922016-07-10 15:55:58 -04002600nfsd4_encode_layout_types(struct xdr_stream *xdr, u32 layout_types)
Kinglong Mee6896f152015-07-30 21:52:44 +08002601{
Jeff Layton8a4c3922016-07-10 15:55:58 -04002602 __be32 *p;
2603 unsigned long i = hweight_long(layout_types);
Kinglong Mee6896f152015-07-30 21:52:44 +08002604
Jeff Layton8a4c3922016-07-10 15:55:58 -04002605 p = xdr_reserve_space(xdr, 4 + 4 * i);
2606 if (!p)
2607 return nfserr_resource;
2608
2609 *p++ = cpu_to_be32(i);
2610
2611 for (i = LAYOUT_NFSV4_1_FILES; i < LAYOUT_TYPE_MAX; ++i)
2612 if (layout_types & (1 << i))
2613 *p++ = cpu_to_be32(i);
Kinglong Mee6896f152015-07-30 21:52:44 +08002614
2615 return 0;
2616}
2617
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002618#define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
2619 FATTR4_WORD0_RDATTR_ERROR)
2620#define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
Kinglong Meec2227a32015-07-07 10:16:37 +08002621#define WORD2_ABSENT_FS_ATTRS 0
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002622
David Quigley18032ca2013-05-02 13:19:10 -04002623#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2624static inline __be32
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002625nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2626 void *context, int len)
David Quigley18032ca2013-05-02 13:19:10 -04002627{
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002628 __be32 *p;
David Quigley18032ca2013-05-02 13:19:10 -04002629
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002630 p = xdr_reserve_space(xdr, len + 4 + 4 + 4);
2631 if (!p)
David Quigley18032ca2013-05-02 13:19:10 -04002632 return nfserr_resource;
2633
2634 /*
2635 * For now we use a 0 here to indicate the null translation; in
2636 * the future we may place a call to translation code here.
2637 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002638 *p++ = cpu_to_be32(0); /* lfs */
2639 *p++ = cpu_to_be32(0); /* pi */
David Quigley18032ca2013-05-02 13:19:10 -04002640 p = xdr_encode_opaque(p, context, len);
David Quigley18032ca2013-05-02 13:19:10 -04002641 return 0;
2642}
2643#else
2644static inline __be32
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002645nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2646 void *context, int len)
David Quigley18032ca2013-05-02 13:19:10 -04002647{ return 0; }
2648#endif
2649
Kinglong Meec2227a32015-07-07 10:16:37 +08002650static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *bmval2, u32 *rdattr_err)
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002651{
2652 /* As per referral draft: */
2653 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
2654 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
2655 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
2656 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
2657 *rdattr_err = NFSERR_MOVED;
2658 else
2659 return nfserr_moved;
2660 }
2661 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
2662 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
Kinglong Meec2227a32015-07-07 10:16:37 +08002663 *bmval2 &= WORD2_ABSENT_FS_ATTRS;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002664 return 0;
2665}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002667
2668static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
2669{
2670 struct path path = exp->ex_path;
2671 int err;
2672
2673 path_get(&path);
2674 while (follow_up(&path)) {
2675 if (path.dentry != path.mnt->mnt_root)
2676 break;
2677 }
David Howellsa528d352017-01-31 16:46:22 +00002678 err = vfs_getattr(&path, stat, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002679 path_put(&path);
2680 return err;
2681}
2682
Kinglong Mee75976de2015-07-30 21:55:30 +08002683static __be32
2684nfsd4_encode_bitmap(struct xdr_stream *xdr, u32 bmval0, u32 bmval1, u32 bmval2)
2685{
2686 __be32 *p;
2687
2688 if (bmval2) {
2689 p = xdr_reserve_space(xdr, 16);
2690 if (!p)
2691 goto out_resource;
2692 *p++ = cpu_to_be32(3);
2693 *p++ = cpu_to_be32(bmval0);
2694 *p++ = cpu_to_be32(bmval1);
2695 *p++ = cpu_to_be32(bmval2);
2696 } else if (bmval1) {
2697 p = xdr_reserve_space(xdr, 12);
2698 if (!p)
2699 goto out_resource;
2700 *p++ = cpu_to_be32(2);
2701 *p++ = cpu_to_be32(bmval0);
2702 *p++ = cpu_to_be32(bmval1);
2703 } else {
2704 p = xdr_reserve_space(xdr, 8);
2705 if (!p)
2706 goto out_resource;
2707 *p++ = cpu_to_be32(1);
2708 *p++ = cpu_to_be32(bmval0);
2709 }
2710
2711 return 0;
2712out_resource:
2713 return nfserr_resource;
2714}
2715
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716/*
2717 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
2718 * ourselves.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 */
Jeff Laytonda2ebce2014-05-30 09:09:25 -04002720static __be32
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04002721nfsd4_encode_fattr(struct xdr_stream *xdr, struct svc_fh *fhp,
2722 struct svc_export *exp,
2723 struct dentry *dentry, u32 *bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002724 struct svc_rqst *rqstp, int ignore_crossmnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725{
2726 u32 bmval0 = bmval[0];
2727 u32 bmval1 = bmval[1];
Andy Adamson7e705702009-04-03 08:29:11 +03002728 u32 bmval2 = bmval[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 struct kstat stat;
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002730 struct svc_fh *tempfh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 struct kstatfs statfs;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002732 __be32 *p;
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05002733 int starting_len = xdr->buf->len;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002734 int attrlen_offset;
2735 __be32 attrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 u32 dummy;
2737 u64 dummy64;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002738 u32 rdattr_err = 0;
Al Virob37ad282006-10-19 23:28:59 -07002739 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07002740 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 struct nfs4_acl *acl = NULL;
Arnd Bergmann0ab88ca2019-03-22 15:07:11 +01002742#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
David Quigley18032ca2013-05-02 13:19:10 -04002743 void *context = NULL;
2744 int contextlen;
Arnd Bergmann0ab88ca2019-03-22 15:07:11 +01002745#endif
David Quigley18032ca2013-05-02 13:19:10 -04002746 bool contextsupport = false;
Andy Adamson7e705702009-04-03 08:29:11 +03002747 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2748 u32 minorversion = resp->cstate.minorversion;
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002749 struct path path = {
2750 .mnt = exp->ex_path.mnt,
2751 .dentry = dentry,
2752 };
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03002753 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
2755 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002756 BUG_ON(!nfsd_attrs_supported(minorversion, bmval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002758 if (exp->ex_fslocs.migrated) {
Kinglong Meec2227a32015-07-07 10:16:37 +08002759 status = fattr_handle_absent_fs(&bmval0, &bmval1, &bmval2, &rdattr_err);
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002760 if (status)
2761 goto out;
2762 }
2763
David Howellsa528d352017-01-31 16:46:22 +00002764 err = vfs_getattr(&path, &stat, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
Al Virob8dd7b92006-10-19 23:29:01 -07002765 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 goto out_nfserr;
Christoph Hellwig12337902014-05-28 10:46:13 +02002767 if ((bmval0 & (FATTR4_WORD0_FILES_AVAIL | FATTR4_WORD0_FILES_FREE |
2768 FATTR4_WORD0_FILES_TOTAL | FATTR4_WORD0_MAXNAME)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2770 FATTR4_WORD1_SPACE_TOTAL))) {
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002771 err = vfs_statfs(&path, &statfs);
Al Virob8dd7b92006-10-19 23:29:01 -07002772 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 goto out_nfserr;
2774 }
2775 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002776 tempfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
2777 status = nfserr_jukebox;
2778 if (!tempfh)
2779 goto out;
2780 fh_init(tempfh, NFS4_FHSIZE);
2781 status = fh_compose(tempfh, exp, dentry, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 if (status)
2783 goto out;
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002784 fhp = tempfh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 }
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02002786 if (bmval0 & FATTR4_WORD0_ACL) {
Al Virob8dd7b92006-10-19 23:29:01 -07002787 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02002788 if (err == -EOPNOTSUPP)
2789 bmval0 &= ~FATTR4_WORD0_ACL;
2790 else if (err == -EINVAL) {
2791 status = nfserr_attrnotsupp;
2792 goto out;
2793 } else if (err != 0)
2794 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
David Quigley18032ca2013-05-02 13:19:10 -04002797#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
Kinglong Meec2227a32015-07-07 10:16:37 +08002798 if ((bmval2 & FATTR4_WORD2_SECURITY_LABEL) ||
2799 bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
J. Bruce Fields32ddd942017-01-03 12:30:11 -05002800 if (exp->ex_flags & NFSEXP_SECURITY_LABEL)
2801 err = security_inode_getsecctx(d_inode(dentry),
David Quigley18032ca2013-05-02 13:19:10 -04002802 &context, &contextlen);
J. Bruce Fields32ddd942017-01-03 12:30:11 -05002803 else
2804 err = -EOPNOTSUPP;
David Quigley18032ca2013-05-02 13:19:10 -04002805 contextsupport = (err == 0);
2806 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2807 if (err == -EOPNOTSUPP)
2808 bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2809 else if (err)
2810 goto out_nfserr;
2811 }
2812 }
2813#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2814
Kinglong Mee75976de2015-07-30 21:55:30 +08002815 status = nfsd4_encode_bitmap(xdr, bmval0, bmval1, bmval2);
2816 if (status)
2817 goto out;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002818
2819 attrlen_offset = xdr->buf->len;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002820 p = xdr_reserve_space(xdr, 4);
2821 if (!p)
2822 goto out_resource;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002823 p++; /* to be backfilled later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
2825 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
J. Bruce Fieldsdcd208692017-01-11 20:34:50 -05002826 u32 supp[3];
2827
2828 memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
Andy Adamson7e705702009-04-03 08:29:11 +03002829
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02002830 if (!IS_POSIXACL(dentry->d_inode))
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002831 supp[0] &= ~FATTR4_WORD0_ACL;
David Quigley18032ca2013-05-02 13:19:10 -04002832 if (!contextsupport)
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002833 supp[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2834 if (!supp[2]) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002835 p = xdr_reserve_space(xdr, 12);
2836 if (!p)
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002837 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002838 *p++ = cpu_to_be32(2);
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002839 *p++ = cpu_to_be32(supp[0]);
2840 *p++ = cpu_to_be32(supp[1]);
Andy Adamson7e705702009-04-03 08:29:11 +03002841 } else {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002842 p = xdr_reserve_space(xdr, 16);
2843 if (!p)
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002844 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002845 *p++ = cpu_to_be32(3);
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002846 *p++ = cpu_to_be32(supp[0]);
2847 *p++ = cpu_to_be32(supp[1]);
2848 *p++ = cpu_to_be32(supp[2]);
Andy Adamson7e705702009-04-03 08:29:11 +03002849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 }
2851 if (bmval0 & FATTR4_WORD0_TYPE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002852 p = xdr_reserve_space(xdr, 4);
2853 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 goto out_resource;
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002855 dummy = nfs4_file_type(stat.mode);
J. Bruce Fields6b6d81372013-01-16 17:11:11 -05002856 if (dummy == NF4BAD) {
2857 status = nfserr_serverfault;
2858 goto out;
2859 }
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002860 *p++ = cpu_to_be32(dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 }
2862 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002863 p = xdr_reserve_space(xdr, 4);
2864 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 goto out_resource;
NeilBrown49640002005-06-23 22:02:58 -07002866 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002867 *p++ = cpu_to_be32(NFS4_FH_PERSISTENT);
NeilBrown49640002005-06-23 22:02:58 -07002868 else
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002869 *p++ = cpu_to_be32(NFS4_FH_PERSISTENT|
2870 NFS4_FH_VOL_RENAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 }
2872 if (bmval0 & FATTR4_WORD0_CHANGE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002873 p = xdr_reserve_space(xdr, 8);
2874 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 goto out_resource;
NeilBrownb8800922017-01-30 17:17:00 +11002876 p = encode_change(p, &stat, d_inode(dentry), exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 }
2878 if (bmval0 & FATTR4_WORD0_SIZE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002879 p = xdr_reserve_space(xdr, 8);
2880 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002882 p = xdr_encode_hyper(p, stat.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 }
2884 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002885 p = xdr_reserve_space(xdr, 4);
2886 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002888 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 }
2890 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002891 p = xdr_reserve_space(xdr, 4);
2892 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002894 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 }
2896 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002897 p = xdr_reserve_space(xdr, 4);
2898 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002900 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 }
2902 if (bmval0 & FATTR4_WORD0_FSID) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002903 p = xdr_reserve_space(xdr, 16);
2904 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002906 if (exp->ex_fslocs.migrated) {
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002907 p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MAJOR);
2908 p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MINOR);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002909 } else switch(fsid_source(fhp)) {
2910 case FSIDSOURCE_FSID:
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002911 p = xdr_encode_hyper(p, (u64)exp->ex_fsid);
2912 p = xdr_encode_hyper(p, (u64)0);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002913 break;
2914 case FSIDSOURCE_DEV:
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002915 *p++ = cpu_to_be32(0);
2916 *p++ = cpu_to_be32(MAJOR(stat.dev));
2917 *p++ = cpu_to_be32(0);
2918 *p++ = cpu_to_be32(MINOR(stat.dev));
NeilBrownaf6a4e22007-02-14 00:33:12 -08002919 break;
2920 case FSIDSOURCE_UUID:
Kinglong Mee94eb3682014-05-23 20:00:19 +08002921 p = xdr_encode_opaque_fixed(p, exp->ex_uuid,
2922 EX_UUID_LEN);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002923 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 }
2925 }
2926 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002927 p = xdr_reserve_space(xdr, 4);
2928 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002930 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 }
2932 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002933 p = xdr_reserve_space(xdr, 4);
2934 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002936 *p++ = cpu_to_be32(nn->nfsd4_lease);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 }
2938 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002939 p = xdr_reserve_space(xdr, 4);
2940 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002942 *p++ = cpu_to_be32(rdattr_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 }
2944 if (bmval0 & FATTR4_WORD0_ACL) {
2945 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
2947 if (acl == NULL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002948 p = xdr_reserve_space(xdr, 4);
2949 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 goto out_resource;
2951
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002952 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 goto out_acl;
2954 }
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002955 p = xdr_reserve_space(xdr, 4);
2956 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002958 *p++ = cpu_to_be32(acl->naces);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08002960 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002961 p = xdr_reserve_space(xdr, 4*3);
2962 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002964 *p++ = cpu_to_be32(ace->type);
2965 *p++ = cpu_to_be32(ace->flag);
2966 *p++ = cpu_to_be32(ace->access_mask &
2967 NFS4_ACE_MASK_ALL);
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002968 status = nfsd4_encode_aclname(xdr, rqstp, ace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 if (status)
2970 goto out;
2971 }
2972 }
2973out_acl:
2974 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002975 p = xdr_reserve_space(xdr, 4);
2976 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 goto out_resource;
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02002978 *p++ = cpu_to_be32(IS_POSIXACL(dentry->d_inode) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
2980 }
2981 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002982 p = xdr_reserve_space(xdr, 4);
2983 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002985 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 }
2987 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002988 p = xdr_reserve_space(xdr, 4);
2989 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002991 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 }
2993 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002994 p = xdr_reserve_space(xdr, 4);
2995 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002997 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 }
2999 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003000 p = xdr_reserve_space(xdr, 4);
3001 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003003 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 }
3005 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003006 p = xdr_reserve_space(xdr, fhp->fh_handle.fh_size + 4);
3007 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 goto out_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003009 p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base,
3010 fhp->fh_handle.fh_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 }
3012 if (bmval0 & FATTR4_WORD0_FILEID) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003013 p = xdr_reserve_space(xdr, 8);
3014 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003016 p = xdr_encode_hyper(p, stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 }
3018 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003019 p = xdr_reserve_space(xdr, 8);
3020 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003022 p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 }
3024 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003025 p = xdr_reserve_space(xdr, 8);
3026 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003028 p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 }
3030 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003031 p = xdr_reserve_space(xdr, 8);
3032 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003034 p = xdr_encode_hyper(p, (u64) statfs.f_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07003036 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003037 status = nfsd4_encode_fs_locations(xdr, rqstp, exp);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07003038 if (status)
3039 goto out;
3040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003042 p = xdr_reserve_space(xdr, 4);
3043 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003045 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 }
3047 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003048 p = xdr_reserve_space(xdr, 8);
3049 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003051 p = xdr_encode_hyper(p, exp->ex_path.mnt->mnt_sb->s_maxbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 }
3053 if (bmval0 & FATTR4_WORD0_MAXLINK) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003054 p = xdr_reserve_space(xdr, 4);
3055 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003057 *p++ = cpu_to_be32(255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 }
3059 if (bmval0 & FATTR4_WORD0_MAXNAME) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003060 p = xdr_reserve_space(xdr, 4);
3061 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003063 *p++ = cpu_to_be32(statfs.f_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 }
3065 if (bmval0 & FATTR4_WORD0_MAXREAD) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003066 p = xdr_reserve_space(xdr, 8);
3067 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003069 p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 }
3071 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003072 p = xdr_reserve_space(xdr, 8);
3073 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003075 p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 }
3077 if (bmval1 & FATTR4_WORD1_MODE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003078 p = xdr_reserve_space(xdr, 4);
3079 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003081 *p++ = cpu_to_be32(stat.mode & S_IALLUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 }
3083 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003084 p = xdr_reserve_space(xdr, 4);
3085 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003087 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 }
3089 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003090 p = xdr_reserve_space(xdr, 4);
3091 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003093 *p++ = cpu_to_be32(stat.nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 }
3095 if (bmval1 & FATTR4_WORD1_OWNER) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003096 status = nfsd4_encode_user(xdr, rqstp, stat.uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 if (status)
3098 goto out;
3099 }
3100 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003101 status = nfsd4_encode_group(xdr, rqstp, stat.gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 if (status)
3103 goto out;
3104 }
3105 if (bmval1 & FATTR4_WORD1_RAWDEV) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003106 p = xdr_reserve_space(xdr, 8);
3107 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003109 *p++ = cpu_to_be32((u32) MAJOR(stat.rdev));
3110 *p++ = cpu_to_be32((u32) MINOR(stat.rdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 }
3112 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003113 p = xdr_reserve_space(xdr, 8);
3114 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 goto out_resource;
3116 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003117 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 }
3119 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003120 p = xdr_reserve_space(xdr, 8);
3121 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 goto out_resource;
3123 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003124 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 }
3126 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003127 p = xdr_reserve_space(xdr, 8);
3128 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 goto out_resource;
3130 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003131 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 }
3133 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003134 p = xdr_reserve_space(xdr, 8);
3135 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 goto out_resource;
3137 dummy64 = (u64)stat.blocks << 9;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003138 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 }
3140 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003141 p = xdr_reserve_space(xdr, 12);
3142 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003144 p = xdr_encode_hyper(p, (s64)stat.atime.tv_sec);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003145 *p++ = cpu_to_be32(stat.atime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 }
3147 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003148 p = xdr_reserve_space(xdr, 12);
3149 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 goto out_resource;
J. Bruce Fields16945142018-04-25 14:34:11 -04003151 p = encode_time_delta(p, d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 }
3153 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003154 p = xdr_reserve_space(xdr, 12);
3155 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003157 p = xdr_encode_hyper(p, (s64)stat.ctime.tv_sec);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003158 *p++ = cpu_to_be32(stat.ctime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 }
3160 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003161 p = xdr_reserve_space(xdr, 12);
3162 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003164 p = xdr_encode_hyper(p, (s64)stat.mtime.tv_sec);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003165 *p++ = cpu_to_be32(stat.mtime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 }
3167 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
Kinglong Mee0a2050d2015-07-30 21:51:54 +08003168 struct kstat parent_stat;
3169 u64 ino = stat.ino;
3170
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003171 p = xdr_reserve_space(xdr, 8);
3172 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 goto out_resource;
Frank Filz406a7ea2007-11-27 11:34:05 -08003174 /*
3175 * Get parent's attributes if not ignoring crossmount
3176 * and this is the root of a cross-mounted filesystem.
3177 */
3178 if (ignore_crossmnt == 0 &&
Kinglong Mee0a2050d2015-07-30 21:51:54 +08003179 dentry == exp->ex_path.mnt->mnt_root) {
3180 err = get_parent_attributes(exp, &parent_stat);
3181 if (err)
3182 goto out_nfserr;
3183 ino = parent_stat.ino;
3184 }
3185 p = xdr_encode_hyper(p, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003187#ifdef CONFIG_NFSD_PNFS
Kinglong Mee6896f152015-07-30 21:52:44 +08003188 if (bmval1 & FATTR4_WORD1_FS_LAYOUT_TYPES) {
Jeff Layton8a4c3922016-07-10 15:55:58 -04003189 status = nfsd4_encode_layout_types(xdr, exp->ex_layout_types);
Kinglong Mee6896f152015-07-30 21:52:44 +08003190 if (status)
3191 goto out;
3192 }
3193
3194 if (bmval2 & FATTR4_WORD2_LAYOUT_TYPES) {
Jeff Layton8a4c3922016-07-10 15:55:58 -04003195 status = nfsd4_encode_layout_types(xdr, exp->ex_layout_types);
Kinglong Mee6896f152015-07-30 21:52:44 +08003196 if (status)
3197 goto out;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003198 }
3199
3200 if (bmval2 & FATTR4_WORD2_LAYOUT_BLKSIZE) {
3201 p = xdr_reserve_space(xdr, 4);
3202 if (!p)
3203 goto out_resource;
3204 *p++ = cpu_to_be32(stat.blksize);
3205 }
3206#endif /* CONFIG_NFSD_PNFS */
Benny Halevy8c18f202009-04-03 08:29:14 +03003207 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
Trond Myklebustb26b78c2017-05-09 16:24:59 -04003208 u32 supp[3];
3209
3210 memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
3211 supp[0] &= NFSD_SUPPATTR_EXCLCREAT_WORD0;
3212 supp[1] &= NFSD_SUPPATTR_EXCLCREAT_WORD1;
3213 supp[2] &= NFSD_SUPPATTR_EXCLCREAT_WORD2;
3214
3215 status = nfsd4_encode_bitmap(xdr, supp[0], supp[1], supp[2]);
Kinglong Mee75976de2015-07-30 21:55:30 +08003216 if (status)
3217 goto out;
Benny Halevy8c18f202009-04-03 08:29:14 +03003218 }
Andy Adamson7e705702009-04-03 08:29:11 +03003219
J. Bruce Fieldsa8585762018-04-25 13:26:23 -04003220 if (bmval2 & FATTR4_WORD2_CHANGE_ATTR_TYPE) {
3221 p = xdr_reserve_space(xdr, 4);
3222 if (!p)
3223 goto out_resource;
3224 if (IS_I_VERSION(d_inode(dentry)))
3225 *p++ = cpu_to_be32(NFS4_CHANGE_TYPE_IS_MONOTONIC_INCR);
3226 else
3227 *p++ = cpu_to_be32(NFS4_CHANGE_TYPE_IS_TIME_METADATA);
3228 }
3229
Arnd Bergmann0ab88ca2019-03-22 15:07:11 +01003230#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
Kinglong Mee7d580722015-07-30 21:54:26 +08003231 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
3232 status = nfsd4_encode_security_label(xdr, rqstp, context,
3233 contextlen);
3234 if (status)
3235 goto out;
3236 }
Arnd Bergmann0ab88ca2019-03-22 15:07:11 +01003237#endif
Kinglong Mee7d580722015-07-30 21:54:26 +08003238
Frank van der Linden0e885e82020-06-23 22:39:27 +00003239 if (bmval2 & FATTR4_WORD2_XATTR_SUPPORT) {
3240 p = xdr_reserve_space(xdr, 4);
3241 if (!p)
3242 goto out_resource;
3243 err = xattr_supported_namespace(d_inode(dentry),
3244 XATTR_USER_PREFIX);
3245 *p++ = cpu_to_be32(err == 0);
3246 }
3247
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04003248 attrlen = htonl(xdr->buf->len - attrlen_offset - 4);
3249 write_bytes_to_xdr_buf(xdr->buf, attrlen_offset, &attrlen, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 status = nfs_ok;
3251
3252out:
J. Bruce Fieldsba4e55b2013-05-15 10:27:52 -04003253#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
David Quigley18032ca2013-05-02 13:19:10 -04003254 if (context)
3255 security_release_secctx(context, contextlen);
J. Bruce Fieldsba4e55b2013-05-15 10:27:52 -04003256#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08003257 kfree(acl);
Yan, Zheng18df11d02014-03-10 12:52:07 +08003258 if (tempfh) {
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05003259 fh_put(tempfh);
Yan, Zheng18df11d02014-03-10 12:52:07 +08003260 kfree(tempfh);
3261 }
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05003262 if (status)
3263 xdr_truncate_encode(xdr, starting_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 return status;
3265out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07003266 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 goto out;
3268out_resource:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 status = nfserr_resource;
3270 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271}
3272
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04003273static void svcxdr_init_encode_from_buffer(struct xdr_stream *xdr,
3274 struct xdr_buf *buf, __be32 *p, int bytes)
3275{
3276 xdr->scratch.iov_len = 0;
3277 memset(buf, 0, sizeof(struct xdr_buf));
3278 buf->head[0].iov_base = p;
3279 buf->head[0].iov_len = 0;
3280 buf->len = 0;
3281 xdr->buf = buf;
3282 xdr->iov = buf->head;
3283 xdr->p = p;
3284 xdr->end = (void *)p + bytes;
3285 buf->buflen = bytes;
3286}
3287
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04003288__be32 nfsd4_encode_fattr_to_buf(__be32 **p, int words,
3289 struct svc_fh *fhp, struct svc_export *exp,
3290 struct dentry *dentry, u32 *bmval,
3291 struct svc_rqst *rqstp, int ignore_crossmnt)
3292{
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04003293 struct xdr_buf dummy;
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04003294 struct xdr_stream xdr;
3295 __be32 ret;
3296
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04003297 svcxdr_init_encode_from_buffer(&xdr, &dummy, *p, words << 2);
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04003298 ret = nfsd4_encode_fattr(&xdr, fhp, exp, dentry, bmval, rqstp,
3299 ignore_crossmnt);
3300 *p = xdr.p;
3301 return ret;
3302}
3303
J. Bruce Fieldsc0ce6ec2008-02-11 15:48:47 -05003304static inline int attributes_need_mount(u32 *bmval)
3305{
3306 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
3307 return 1;
3308 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
3309 return 1;
3310 return 0;
3311}
3312
Al Virob37ad282006-10-19 23:28:59 -07003313static __be32
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003314nfsd4_encode_dirent_fattr(struct xdr_stream *xdr, struct nfsd4_readdir *cd,
3315 const char *name, int namlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316{
3317 struct svc_export *exp = cd->rd_fhp->fh_export;
3318 struct dentry *dentry;
Al Virob37ad282006-10-19 23:28:59 -07003319 __be32 nfserr;
Frank Filz406a7ea2007-11-27 11:34:05 -08003320 int ignore_crossmnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321
Al Viro6c2d47982019-10-31 01:21:58 -04003322 dentry = lookup_positive_unlocked(name, cd->rd_fhp->fh_dentry, namlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 if (IS_ERR(dentry))
3324 return nfserrno(PTR_ERR(dentry));
3325
3326 exp_get(exp);
Frank Filz406a7ea2007-11-27 11:34:05 -08003327 /*
3328 * In the case of a mountpoint, the client may be asking for
3329 * attributes that are only properties of the underlying filesystem
3330 * as opposed to the cross-mounted file system. In such a case,
3331 * we will not follow the cross mount and will fill the attribtutes
3332 * directly from the mountpoint dentry.
3333 */
J. Bruce Fields3227fa42009-10-25 21:43:01 -04003334 if (nfsd_mountpoint(dentry, exp)) {
J.Bruce Fields021d3a72006-12-13 00:35:24 -08003335 int err;
3336
J. Bruce Fields3227fa42009-10-25 21:43:01 -04003337 if (!(exp->ex_flags & NFSEXP_V4ROOT)
3338 && !attributes_need_mount(cd->rd_bmval)) {
3339 ignore_crossmnt = 1;
3340 goto out_encode;
3341 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07003342 /*
3343 * Why the heck aren't we just using nfsd_lookup??
3344 * Different "."/".." handling? Something else?
3345 * At least, add a comment here to explain....
3346 */
J.Bruce Fields021d3a72006-12-13 00:35:24 -08003347 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
3348 if (err) {
3349 nfserr = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 goto out_put;
3351 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07003352 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
3353 if (nfserr)
3354 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
3356 }
J. Bruce Fields3227fa42009-10-25 21:43:01 -04003357out_encode:
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003358 nfserr = nfsd4_encode_fattr(xdr, NULL, exp, dentry, cd->rd_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08003359 cd->rd_rqstp, ignore_crossmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360out_put:
3361 dput(dentry);
3362 exp_put(exp);
3363 return nfserr;
3364}
3365
Al Viro2ebbc012006-10-19 23:28:58 -07003366static __be32 *
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003367nfsd4_encode_rdattr_error(struct xdr_stream *xdr, __be32 nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368{
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003369 __be32 *p;
3370
Kinglong Meec3a45612014-07-06 11:34:43 +08003371 p = xdr_reserve_space(xdr, 20);
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003372 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 return NULL;
3374 *p++ = htonl(2);
3375 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
3376 *p++ = htonl(0); /* bmval1 */
3377
J. Bruce Fields87915c62013-01-16 17:33:28 -05003378 *p++ = htonl(4); /* attribute length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 *p++ = nfserr; /* no htonl */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 return p;
3381}
3382
3383static int
NeilBrowna0ad13e2007-01-26 00:57:10 -08003384nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
3385 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386{
NeilBrowna0ad13e2007-01-26 00:57:10 -08003387 struct readdir_cd *ccd = ccdv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003389 struct xdr_stream *xdr = cd->xdr;
3390 int start_offset = xdr->buf->len;
3391 int cookie_offset;
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04003392 u32 name_and_cookie;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003393 int entry_bytes;
Al Virob37ad282006-10-19 23:28:59 -07003394 __be32 nfserr = nfserr_toosmall;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003395 __be64 wire_offset;
3396 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
3398 /* In nfsv4, "." and ".." never make it onto the wire.. */
3399 if (name && isdotent(name, namlen)) {
3400 cd->common.err = nfs_ok;
3401 return 0;
3402 }
3403
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003404 if (cd->cookie_offset) {
3405 wire_offset = cpu_to_be64(offset);
3406 write_bytes_to_xdr_buf(xdr->buf, cd->cookie_offset,
3407 &wire_offset, 8);
3408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003410 p = xdr_reserve_space(xdr, 4);
3411 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 *p++ = xdr_one; /* mark entry present */
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003414 cookie_offset = xdr->buf->len;
3415 p = xdr_reserve_space(xdr, 3*4 + namlen);
3416 if (!p)
3417 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
3419 p = xdr_encode_array(p, name, namlen); /* name length & name */
3420
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003421 nfserr = nfsd4_encode_dirent_fattr(xdr, cd, name, namlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 switch (nfserr) {
3423 case nfs_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 break;
3425 case nfserr_resource:
3426 nfserr = nfserr_toosmall;
3427 goto fail;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04003428 case nfserr_noent:
Kinglong Meef41c5ad2014-06-13 17:32:42 +08003429 xdr_truncate_encode(xdr, start_offset);
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04003430 goto skip_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 default:
3432 /*
3433 * If the client requested the RDATTR_ERROR attribute,
3434 * we stuff the error code into this attribute
3435 * and continue. If this attribute was not requested,
3436 * then in accordance with the spec, we fail the
3437 * entire READDIR operation(!)
3438 */
3439 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
3440 goto fail;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003441 p = nfsd4_encode_rdattr_error(xdr, nfserr);
Fred Isaman34081ef2006-01-18 17:43:40 -08003442 if (p == NULL) {
3443 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 goto fail;
Fred Isaman34081ef2006-01-18 17:43:40 -08003445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 }
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003447 nfserr = nfserr_toosmall;
3448 entry_bytes = xdr->buf->len - start_offset;
3449 if (entry_bytes > cd->rd_maxcount)
3450 goto fail;
3451 cd->rd_maxcount -= entry_bytes;
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04003452 /*
3453 * RFC 3530 14.2.24 describes rd_dircount as only a "hint", so
3454 * let's always let through the first entry, at least:
3455 */
J. Bruce Fields0ec016e2014-12-19 18:01:35 -05003456 if (!cd->rd_dircount)
3457 goto fail;
3458 name_and_cookie = 4 + 4 * XDR_QUADLEN(namlen) + 8;
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04003459 if (name_and_cookie > cd->rd_dircount && cd->cookie_offset)
3460 goto fail;
3461 cd->rd_dircount -= min(cd->rd_dircount, name_and_cookie);
J. Bruce Fields0ec016e2014-12-19 18:01:35 -05003462
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003463 cd->cookie_offset = cookie_offset;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04003464skip_entry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 cd->common.err = nfs_ok;
3466 return 0;
3467fail:
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003468 xdr_truncate_encode(xdr, start_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 cd->common.err = nfserr;
3470 return -EINVAL;
3471}
3472
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003473static __be32
3474nfsd4_encode_stateid(struct xdr_stream *xdr, stateid_t *sid)
Benny Halevye2f282b2008-08-12 20:45:07 +03003475{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003476 __be32 *p;
Benny Halevye2f282b2008-08-12 20:45:07 +03003477
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003478 p = xdr_reserve_space(xdr, sizeof(stateid_t));
3479 if (!p)
3480 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003481 *p++ = cpu_to_be32(sid->si_generation);
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003482 p = xdr_encode_opaque_fixed(p, &sid->si_opaque,
3483 sizeof(stateid_opaque_t));
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003484 return 0;
Benny Halevye2f282b2008-08-12 20:45:07 +03003485}
3486
Benny Halevy695e12f2008-07-04 14:38:33 +03003487static __be32
Al Virob37ad282006-10-19 23:28:59 -07003488nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003490 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003491 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003493 p = xdr_reserve_space(xdr, 8);
3494 if (!p)
3495 return nfserr_resource;
3496 *p++ = cpu_to_be32(access->ac_supported);
3497 *p++ = cpu_to_be32(access->ac_resp_access);
3498 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499}
3500
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003501static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
3502{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003503 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003504 __be32 *p;
3505
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003506 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 8);
3507 if (!p)
3508 return nfserr_resource;
3509 p = xdr_encode_opaque_fixed(p, bcts->sessionid.data,
3510 NFS4_MAX_SESSIONID_LEN);
3511 *p++ = cpu_to_be32(bcts->dir);
3512 /* Upshifting from TCP to RDMA is not supported */
3513 *p++ = cpu_to_be32(0);
3514 return 0;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003515}
3516
Benny Halevy695e12f2008-07-04 14:38:33 +03003517static __be32
Al Virob37ad282006-10-19 23:28:59 -07003518nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003520 struct xdr_stream *xdr = &resp->xdr;
3521
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003522 return nfsd4_encode_stateid(xdr, &close->cl_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523}
3524
3525
Benny Halevy695e12f2008-07-04 14:38:33 +03003526static __be32
Al Virob37ad282006-10-19 23:28:59 -07003527nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003529 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003530 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003532 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
3533 if (!p)
3534 return nfserr_resource;
3535 p = xdr_encode_opaque_fixed(p, commit->co_verf.data,
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003536 NFS4_VERIFIER_SIZE);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003537 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538}
3539
Benny Halevy695e12f2008-07-04 14:38:33 +03003540static __be32
Al Virob37ad282006-10-19 23:28:59 -07003541nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003543 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003544 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003546 p = xdr_reserve_space(xdr, 20);
3547 if (!p)
3548 return nfserr_resource;
3549 encode_cinfo(p, &create->cr_cinfo);
Trond Myklebustb96811c2019-08-18 14:18:58 -04003550 return nfsd4_encode_bitmap(xdr, create->cr_bmval[0],
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003551 create->cr_bmval[1], create->cr_bmval[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552}
3553
Al Virob37ad282006-10-19 23:28:59 -07003554static __be32
3555nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556{
3557 struct svc_fh *fhp = getattr->ga_fhp;
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04003558 struct xdr_stream *xdr = &resp->xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003560 return nfsd4_encode_fattr(xdr, fhp, fhp->fh_export, fhp->fh_dentry,
3561 getattr->ga_bmval, resp->rqstp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562}
3563
Benny Halevy695e12f2008-07-04 14:38:33 +03003564static __be32
3565nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003567 struct xdr_stream *xdr = &resp->xdr;
Benny Halevy695e12f2008-07-04 14:38:33 +03003568 struct svc_fh *fhp = *fhpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 unsigned int len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003570 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003572 len = fhp->fh_handle.fh_size;
3573 p = xdr_reserve_space(xdr, len + 4);
3574 if (!p)
3575 return nfserr_resource;
3576 p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base, len);
3577 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578}
3579
3580/*
3581* Including all fields other than the name, a LOCK4denied structure requires
3582* 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
3583*/
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003584static __be32
3585nfsd4_encode_lock_denied(struct xdr_stream *xdr, struct nfsd4_lock_denied *ld)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586{
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003587 struct xdr_netobj *conf = &ld->ld_owner;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003588 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003590again:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003591 p = xdr_reserve_space(xdr, 32 + XDR_LEN(conf->len));
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003592 if (!p) {
3593 /*
3594 * Don't fail to return the result just because we can't
3595 * return the conflicting open:
3596 */
3597 if (conf->len) {
Kinglong Meef98bac52014-07-07 22:10:56 +08003598 kfree(conf->data);
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003599 conf->len = 0;
3600 conf->data = NULL;
3601 goto again;
3602 }
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003603 return nfserr_resource;
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003604 }
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003605 p = xdr_encode_hyper(p, ld->ld_start);
3606 p = xdr_encode_hyper(p, ld->ld_length);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003607 *p++ = cpu_to_be32(ld->ld_type);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003608 if (conf->len) {
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003609 p = xdr_encode_opaque_fixed(p, &ld->ld_clientid, 8);
3610 p = xdr_encode_opaque(p, conf->data, conf->len);
Kinglong Meef98bac52014-07-07 22:10:56 +08003611 kfree(conf->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003613 p = xdr_encode_hyper(p, (u64)0); /* clientid */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003614 *p++ = cpu_to_be32(0); /* length of owner name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 }
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003616 return nfserr_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617}
3618
Benny Halevy695e12f2008-07-04 14:38:33 +03003619static __be32
Al Virob37ad282006-10-19 23:28:59 -07003620nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003622 struct xdr_stream *xdr = &resp->xdr;
3623
Benny Halevye2f282b2008-08-12 20:45:07 +03003624 if (!nfserr)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003625 nfserr = nfsd4_encode_stateid(xdr, &lock->lk_resp_stateid);
Benny Halevye2f282b2008-08-12 20:45:07 +03003626 else if (nfserr == nfserr_denied)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003627 nfserr = nfsd4_encode_lock_denied(xdr, &lock->lk_denied);
Kinglong Meef98bac52014-07-07 22:10:56 +08003628
Benny Halevy695e12f2008-07-04 14:38:33 +03003629 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630}
3631
Benny Halevy695e12f2008-07-04 14:38:33 +03003632static __be32
Al Virob37ad282006-10-19 23:28:59 -07003633nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003635 struct xdr_stream *xdr = &resp->xdr;
3636
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 if (nfserr == nfserr_denied)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003638 nfsd4_encode_lock_denied(xdr, &lockt->lt_denied);
Benny Halevy695e12f2008-07-04 14:38:33 +03003639 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640}
3641
Benny Halevy695e12f2008-07-04 14:38:33 +03003642static __be32
Al Virob37ad282006-10-19 23:28:59 -07003643nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003645 struct xdr_stream *xdr = &resp->xdr;
3646
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003647 return nfsd4_encode_stateid(xdr, &locku->lu_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648}
3649
3650
Benny Halevy695e12f2008-07-04 14:38:33 +03003651static __be32
Al Virob37ad282006-10-19 23:28:59 -07003652nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003654 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003655 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003657 p = xdr_reserve_space(xdr, 20);
3658 if (!p)
3659 return nfserr_resource;
3660 p = encode_cinfo(p, &link->li_cinfo);
3661 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662}
3663
3664
Benny Halevy695e12f2008-07-04 14:38:33 +03003665static __be32
Al Virob37ad282006-10-19 23:28:59 -07003666nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003668 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003669 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003671 nfserr = nfsd4_encode_stateid(xdr, &open->op_stateid);
3672 if (nfserr)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003673 return nfserr;
Kinglong Mee75976de2015-07-30 21:55:30 +08003674 p = xdr_reserve_space(xdr, 24);
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003675 if (!p)
3676 return nfserr_resource;
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04003677 p = encode_cinfo(p, &open->op_cinfo);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003678 *p++ = cpu_to_be32(open->op_rflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679
Kinglong Mee75976de2015-07-30 21:55:30 +08003680 nfserr = nfsd4_encode_bitmap(xdr, open->op_bmval[0], open->op_bmval[1],
3681 open->op_bmval[2]);
3682 if (nfserr)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003683 return nfserr;
Kinglong Mee75976de2015-07-30 21:55:30 +08003684
3685 p = xdr_reserve_space(xdr, 4);
3686 if (!p)
3687 return nfserr_resource;
3688
3689 *p++ = cpu_to_be32(open->op_delegate_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 switch (open->op_delegate_type) {
3691 case NFS4_OPEN_DELEGATE_NONE:
3692 break;
3693 case NFS4_OPEN_DELEGATE_READ:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003694 nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
3695 if (nfserr)
3696 return nfserr;
3697 p = xdr_reserve_space(xdr, 20);
3698 if (!p)
3699 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003700 *p++ = cpu_to_be32(open->op_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701
3702 /*
3703 * TODO: ACE's in delegations
3704 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003705 *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
3706 *p++ = cpu_to_be32(0);
3707 *p++ = cpu_to_be32(0);
3708 *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 break;
3710 case NFS4_OPEN_DELEGATE_WRITE:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003711 nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
3712 if (nfserr)
3713 return nfserr;
3714 p = xdr_reserve_space(xdr, 32);
3715 if (!p)
3716 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003717 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718
3719 /*
3720 * TODO: space_limit's in delegations
3721 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003722 *p++ = cpu_to_be32(NFS4_LIMIT_SIZE);
3723 *p++ = cpu_to_be32(~(u32)0);
3724 *p++ = cpu_to_be32(~(u32)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725
3726 /*
3727 * TODO: ACE's in delegations
3728 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003729 *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
3730 *p++ = cpu_to_be32(0);
3731 *p++ = cpu_to_be32(0);
3732 *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 break;
Benny Halevyd24433c2012-02-16 20:57:17 +02003734 case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
3735 switch (open->op_why_no_deleg) {
3736 case WND4_CONTENTION:
3737 case WND4_RESOURCE:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003738 p = xdr_reserve_space(xdr, 8);
3739 if (!p)
3740 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003741 *p++ = cpu_to_be32(open->op_why_no_deleg);
3742 /* deleg signaling not supported yet: */
3743 *p++ = cpu_to_be32(0);
Benny Halevyd24433c2012-02-16 20:57:17 +02003744 break;
3745 default:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003746 p = xdr_reserve_space(xdr, 4);
3747 if (!p)
3748 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003749 *p++ = cpu_to_be32(open->op_why_no_deleg);
Benny Halevyd24433c2012-02-16 20:57:17 +02003750 }
Benny Halevyd24433c2012-02-16 20:57:17 +02003751 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 default:
3753 BUG();
3754 }
3755 /* XXX save filehandle here */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003756 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757}
3758
Benny Halevy695e12f2008-07-04 14:38:33 +03003759static __be32
Al Virob37ad282006-10-19 23:28:59 -07003760nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003762 struct xdr_stream *xdr = &resp->xdr;
3763
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003764 return nfsd4_encode_stateid(xdr, &oc->oc_resp_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765}
3766
Benny Halevy695e12f2008-07-04 14:38:33 +03003767static __be32
Al Virob37ad282006-10-19 23:28:59 -07003768nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003770 struct xdr_stream *xdr = &resp->xdr;
3771
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003772 return nfsd4_encode_stateid(xdr, &od->od_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773}
3774
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003775static __be32 nfsd4_encode_splice_read(
3776 struct nfsd4_compoundres *resp,
3777 struct nfsd4_read *read,
3778 struct file *file, unsigned long maxcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779{
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003780 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003781 struct xdr_buf *buf = xdr->buf;
Chuck Lever76e54922020-11-05 10:24:19 -05003782 int status, space_left;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003783 u32 eof;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003784 __be32 nfserr;
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003785 __be32 *p = xdr->p - 2;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003786
Kinglong Meed5d5c302014-07-09 21:51:27 +08003787 /* Make sure there will be room for padding if needed */
3788 if (xdr->end - xdr->p < 1)
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003789 return nfserr_resource;
3790
Chuck Lever87c59422018-03-28 13:29:11 -04003791 nfserr = nfsd_splice_read(read->rd_rqstp, read->rd_fhp,
Trond Myklebust83a63072019-08-26 13:03:11 -04003792 file, read->rd_offset, &maxcount, &eof);
Chuck Lever87c59422018-03-28 13:29:11 -04003793 read->rd_length = maxcount;
Chuck Lever76e54922020-11-05 10:24:19 -05003794 if (nfserr)
3795 goto out_err;
3796 status = svc_encode_result_payload(read->rd_rqstp,
3797 buf->head[0].iov_len, maxcount);
3798 if (status) {
3799 nfserr = nfserrno(status);
3800 goto out_err;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003801 }
3802
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003803 *(p++) = htonl(eof);
3804 *(p++) = htonl(maxcount);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003805
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003806 buf->page_len = maxcount;
3807 buf->len += maxcount;
J. Bruce Fields15b23ef2014-09-24 16:32:34 -04003808 xdr->page_ptr += (buf->page_base + maxcount + PAGE_SIZE - 1)
3809 / PAGE_SIZE;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003810
3811 /* Use rest of head for padding and remaining ops: */
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003812 buf->tail[0].iov_base = xdr->p;
3813 buf->tail[0].iov_len = 0;
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003814 xdr->iov = buf->tail;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003815 if (maxcount&3) {
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003816 int pad = 4 - (maxcount&3);
3817
3818 *(xdr->p++) = 0;
3819
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003820 buf->tail[0].iov_base += maxcount&3;
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003821 buf->tail[0].iov_len = pad;
3822 buf->len += pad;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003823 }
3824
3825 space_left = min_t(int, (void *)xdr->end - (void *)xdr->p,
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003826 buf->buflen - buf->len);
3827 buf->buflen = buf->len + space_left;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003828 xdr->end = (__be32 *)((void *)xdr->end + space_left);
3829
3830 return 0;
Chuck Lever76e54922020-11-05 10:24:19 -05003831
3832out_err:
3833 /*
3834 * nfsd_splice_actor may have already messed with the
3835 * page length; reset it so as not to confuse
3836 * xdr_truncate_encode in our caller.
3837 */
3838 buf->page_len = 0;
3839 return nfserr;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003840}
3841
3842static __be32 nfsd4_encode_readv(struct nfsd4_compoundres *resp,
3843 struct nfsd4_read *read,
3844 struct file *file, unsigned long maxcount)
3845{
3846 struct xdr_stream *xdr = &resp->xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 u32 eof;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003848 int starting_len = xdr->buf->len - 8;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003849 __be32 nfserr;
3850 __be32 tmp;
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003851 int pad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852
Anna Schumaker403217f2020-08-17 12:53:06 -04003853 read->rd_vlen = xdr_reserve_space_vec(xdr, resp->rqstp->rq_vec, maxcount);
3854 if (read->rd_vlen < 0)
3855 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856
Chuck Lever87c59422018-03-28 13:29:11 -04003857 nfserr = nfsd_readv(resp->rqstp, read->rd_fhp, file, read->rd_offset,
Trond Myklebust83a63072019-08-26 13:03:11 -04003858 resp->rqstp->rq_vec, read->rd_vlen, &maxcount,
3859 &eof);
Chuck Lever87c59422018-03-28 13:29:11 -04003860 read->rd_length = maxcount;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003861 if (nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 return nfserr;
Chuck Lever03493bc2020-06-10 10:36:42 -04003863 if (svc_encode_result_payload(resp->rqstp, starting_len + 8, maxcount))
Chuck Lever41205532020-03-02 14:45:53 -05003864 return nfserr_io;
Chuck Lever7dcf4ab2020-03-02 15:00:04 -05003865 xdr_truncate_encode(xdr, starting_len + 8 + xdr_align_size(maxcount));
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003866
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003867 tmp = htonl(eof);
3868 write_bytes_to_xdr_buf(xdr->buf, starting_len , &tmp, 4);
3869 tmp = htonl(maxcount);
3870 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp, 4);
3871
Chuck Lever7dcf4ab2020-03-02 15:00:04 -05003872 tmp = xdr_zero;
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003873 pad = (maxcount&3) ? 4 - (maxcount&3) : 0;
3874 write_bytes_to_xdr_buf(xdr->buf, starting_len + 8 + maxcount,
Chuck Lever7dcf4ab2020-03-02 15:00:04 -05003875 &tmp, pad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 return 0;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003877
3878}
3879
3880static __be32
3881nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
3882 struct nfsd4_read *read)
3883{
3884 unsigned long maxcount;
3885 struct xdr_stream *xdr = &resp->xdr;
Jeff Layton5c4583b2019-08-18 14:18:54 -04003886 struct file *file;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003887 int starting_len = xdr->buf->len;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003888 __be32 *p;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003889
Jeff Layton5c4583b2019-08-18 14:18:54 -04003890 if (nfserr)
3891 return nfserr;
3892 file = read->rd_nf->nf_file;
3893
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003894 p = xdr_reserve_space(xdr, 8); /* eof flag and byte count */
3895 if (!p) {
Jeff Layton779fb0f2014-11-19 07:51:18 -05003896 WARN_ON_ONCE(test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags));
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003897 return nfserr_resource;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003898 }
Christoph Hellwig68e8bb02015-06-18 16:45:02 +02003899 if (resp->xdr.buf->page_len &&
3900 test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags)) {
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003901 WARN_ON_ONCE(1);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003902 return nfserr_resource;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003903 }
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003904 xdr_commit_encode(xdr);
3905
3906 maxcount = svc_max_payload(resp->rqstp);
Christoph Hellwig68e8bb02015-06-18 16:45:02 +02003907 maxcount = min_t(unsigned long, maxcount,
3908 (xdr->buf->buflen - xdr->buf->len));
Kinglong Mee3c7aa152014-06-10 18:08:19 +08003909 maxcount = min_t(unsigned long, maxcount, read->rd_length);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003910
Christoph Hellwig68e8bb02015-06-18 16:45:02 +02003911 if (file->f_op->splice_read &&
3912 test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags))
Christoph Hellwig96bcad52015-06-18 16:45:01 +02003913 nfserr = nfsd4_encode_splice_read(resp, read, file, maxcount);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003914 else
Christoph Hellwig96bcad52015-06-18 16:45:01 +02003915 nfserr = nfsd4_encode_readv(resp, read, file, maxcount);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003916
Christoph Hellwig96bcad52015-06-18 16:45:01 +02003917 if (nfserr)
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003918 xdr_truncate_encode(xdr, starting_len);
Christoph Hellwig96bcad52015-06-18 16:45:01 +02003919
Christoph Hellwig96bcad52015-06-18 16:45:01 +02003920 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921}
3922
Al Virob37ad282006-10-19 23:28:59 -07003923static __be32
3924nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925{
3926 int maxcount;
J. Bruce Fields476a7b12014-01-20 17:08:27 -05003927 __be32 wire_count;
3928 int zero = 0;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003929 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05003930 int length_offset = xdr->buf->len;
Chuck Lever76e54922020-11-05 10:24:19 -05003931 int status;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003932 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04003934 p = xdr_reserve_space(xdr, 4);
3935 if (!p)
3936 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 maxcount = PAGE_SIZE;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003938
J. Bruce Fields476a7b12014-01-20 17:08:27 -05003939 p = xdr_reserve_space(xdr, maxcount);
3940 if (!p)
J. Bruce Fields4e21ac42014-03-22 15:15:11 -04003941 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 /*
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01003943 * XXX: By default, vfs_readlink() will truncate symlinks if they
3944 * would overflow the buffer. Is this kosher in NFSv4? If not, one
3945 * easy fix is: if vfs_readlink() precisely fills the buffer, assume
3946 * that truncation occurred, and return NFS4ERR_RESOURCE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 */
J. Bruce Fields476a7b12014-01-20 17:08:27 -05003948 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp,
3949 (char *)p, &maxcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 if (nfserr == nfserr_isdir)
J. Bruce Fieldsd3f627c2014-02-26 17:00:38 -05003951 nfserr = nfserr_inval;
Chuck Lever76e54922020-11-05 10:24:19 -05003952 if (nfserr)
3953 goto out_err;
3954 status = svc_encode_result_payload(readlink->rl_rqstp, length_offset,
3955 maxcount);
3956 if (status) {
3957 nfserr = nfserrno(status);
3958 goto out_err;
J. Bruce Fieldsd3f627c2014-02-26 17:00:38 -05003959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960
J. Bruce Fields476a7b12014-01-20 17:08:27 -05003961 wire_count = htonl(maxcount);
3962 write_bytes_to_xdr_buf(xdr->buf, length_offset, &wire_count, 4);
Avi Kivity69bbd9c2014-06-26 17:23:12 +03003963 xdr_truncate_encode(xdr, length_offset + 4 + ALIGN(maxcount, 4));
J. Bruce Fields476a7b12014-01-20 17:08:27 -05003964 if (maxcount & 3)
3965 write_bytes_to_xdr_buf(xdr->buf, length_offset + 4 + maxcount,
3966 &zero, 4 - (maxcount&3));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 return 0;
Chuck Lever76e54922020-11-05 10:24:19 -05003968
3969out_err:
3970 xdr_truncate_encode(xdr, length_offset);
3971 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972}
3973
Al Virob37ad282006-10-19 23:28:59 -07003974static __be32
3975nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976{
3977 int maxcount;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003978 int bytes_left;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 loff_t offset;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003980 __be64 wire_offset;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003981 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05003982 int starting_len = xdr->buf->len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003983 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003985 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
3986 if (!p)
3987 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988
3989 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003990 *p++ = cpu_to_be32(0);
3991 *p++ = cpu_to_be32(0);
J. Bruce Fields4aea24b2014-01-15 15:17:58 -05003992 resp->xdr.buf->head[0].iov_len = ((char *)resp->xdr.p)
3993 - (char *)resp->xdr.buf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994
3995 /*
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003996 * Number of bytes left for directory entries allowing for the
3997 * final 8 bytes of the readdir and a following failed op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 */
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003999 bytes_left = xdr->buf->buflen - xdr->buf->len
4000 - COMPOUND_ERR_SLACK_SPACE - 8;
4001 if (bytes_left < 0) {
4002 nfserr = nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 goto err_no_verf;
4004 }
Scott Mayhew9c2ece62018-05-07 09:01:08 -04004005 maxcount = svc_max_payload(resp->rqstp);
4006 maxcount = min_t(u32, readdir->rd_maxcount, maxcount);
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004007 /*
4008 * Note the rfc defines rd_maxcount as the size of the
4009 * READDIR4resok structure, which includes the verifier above
4010 * and the 8 bytes encoded at the end of this function:
4011 */
4012 if (maxcount < 16) {
4013 nfserr = nfserr_toosmall;
4014 goto err_no_verf;
4015 }
4016 maxcount = min_t(int, maxcount-16, bytes_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04004018 /* RFC 3530 14.2.24 allows us to ignore dircount when it's 0: */
4019 if (!readdir->rd_dircount)
Scott Mayhew9c2ece62018-05-07 09:01:08 -04004020 readdir->rd_dircount = svc_max_payload(resp->rqstp);
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04004021
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004022 readdir->xdr = xdr;
4023 readdir->rd_maxcount = maxcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 readdir->common.err = 0;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004025 readdir->cookie_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026
4027 offset = readdir->rd_cookie;
4028 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
4029 &offset,
4030 &readdir->common, nfsd4_encode_dirent);
4031 if (nfserr == nfs_ok &&
4032 readdir->common.err == nfserr_toosmall &&
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004033 xdr->buf->len == starting_len + 8) {
4034 /* nothing encoded; which limit did we hit?: */
4035 if (maxcount - 16 < bytes_left)
4036 /* It was the fault of rd_maxcount: */
4037 nfserr = nfserr_toosmall;
4038 else
4039 /* We ran out of buffer space: */
4040 nfserr = nfserr_resource;
4041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 if (nfserr)
4043 goto err_no_verf;
4044
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004045 if (readdir->cookie_offset) {
4046 wire_offset = cpu_to_be64(offset);
4047 write_bytes_to_xdr_buf(xdr->buf, readdir->cookie_offset,
4048 &wire_offset, 8);
4049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004051 p = xdr_reserve_space(xdr, 8);
4052 if (!p) {
4053 WARN_ON_ONCE(1);
4054 goto err_no_verf;
4055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 *p++ = 0; /* no more entries */
4057 *p++ = htonl(readdir->common.err == nfserr_eof);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058
4059 return 0;
4060err_no_verf:
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05004061 xdr_truncate_encode(xdr, starting_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 return nfserr;
4063}
4064
Benny Halevy695e12f2008-07-04 14:38:33 +03004065static __be32
Al Virob37ad282006-10-19 23:28:59 -07004066nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004068 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004069 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004071 p = xdr_reserve_space(xdr, 20);
4072 if (!p)
4073 return nfserr_resource;
4074 p = encode_cinfo(p, &remove->rm_cinfo);
4075 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076}
4077
Benny Halevy695e12f2008-07-04 14:38:33 +03004078static __be32
Al Virob37ad282006-10-19 23:28:59 -07004079nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004081 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004082 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004084 p = xdr_reserve_space(xdr, 40);
4085 if (!p)
4086 return nfserr_resource;
4087 p = encode_cinfo(p, &rename->rn_sinfo);
4088 p = encode_cinfo(p, &rename->rn_tinfo);
4089 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090}
4091
Benny Halevy695e12f2008-07-04 14:38:33 +03004092static __be32
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004093nfsd4_do_encode_secinfo(struct xdr_stream *xdr, struct svc_export *exp)
Andy Adamsondcb488a32007-07-17 04:04:51 -07004094{
Chuck Lever676e4eb2013-04-30 18:48:54 -04004095 u32 i, nflavs, supported;
J. Bruce Fields4796f452007-07-17 04:04:51 -07004096 struct exp_flavor_info *flavs;
4097 struct exp_flavor_info def_flavs[2];
Chuck Lever676e4eb2013-04-30 18:48:54 -04004098 __be32 *p, *flavorsp;
4099 static bool report = true;
Andy Adamsondcb488a32007-07-17 04:04:51 -07004100
J. Bruce Fields4796f452007-07-17 04:04:51 -07004101 if (exp->ex_nflavors) {
4102 flavs = exp->ex_flavors;
4103 nflavs = exp->ex_nflavors;
4104 } else { /* Handling of some defaults in absence of real secinfo: */
4105 flavs = def_flavs;
4106 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
4107 nflavs = 2;
4108 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
4109 flavs[1].pseudoflavor = RPC_AUTH_NULL;
4110 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
4111 nflavs = 1;
4112 flavs[0].pseudoflavor
4113 = svcauth_gss_flavor(exp->ex_client);
4114 } else {
4115 nflavs = 1;
4116 flavs[0].pseudoflavor
4117 = exp->ex_client->flavour->flavour;
4118 }
4119 }
4120
Chuck Lever676e4eb2013-04-30 18:48:54 -04004121 supported = 0;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004122 p = xdr_reserve_space(xdr, 4);
4123 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004124 return nfserr_resource;
Chuck Lever676e4eb2013-04-30 18:48:54 -04004125 flavorsp = p++; /* to be backfilled later */
Chuck Lever676e4eb2013-04-30 18:48:54 -04004126
J. Bruce Fields4796f452007-07-17 04:04:51 -07004127 for (i = 0; i < nflavs; i++) {
Chuck Lever676e4eb2013-04-30 18:48:54 -04004128 rpc_authflavor_t pf = flavs[i].pseudoflavor;
Chuck Levera77c8062013-03-16 15:55:10 -04004129 struct rpcsec_gss_info info;
Andy Adamsondcb488a32007-07-17 04:04:51 -07004130
Chuck Lever676e4eb2013-04-30 18:48:54 -04004131 if (rpcauth_get_gssinfo(pf, &info) == 0) {
4132 supported++;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004133 p = xdr_reserve_space(xdr, 4 + 4 +
4134 XDR_LEN(info.oid.len) + 4 + 4);
4135 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004136 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004137 *p++ = cpu_to_be32(RPC_AUTH_GSS);
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004138 p = xdr_encode_opaque(p, info.oid.data, info.oid.len);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004139 *p++ = cpu_to_be32(info.qop);
4140 *p++ = cpu_to_be32(info.service);
Chuck Lever676e4eb2013-04-30 18:48:54 -04004141 } else if (pf < RPC_AUTH_MAXFLAVOR) {
4142 supported++;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004143 p = xdr_reserve_space(xdr, 4);
4144 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004145 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004146 *p++ = cpu_to_be32(pf);
Chuck Lever676e4eb2013-04-30 18:48:54 -04004147 } else {
4148 if (report)
4149 pr_warn("NFS: SECINFO: security flavor %u "
4150 "is not supported\n", pf);
Andy Adamsondcb488a32007-07-17 04:04:51 -07004151 }
4152 }
Chuck Levera77c8062013-03-16 15:55:10 -04004153
Chuck Lever676e4eb2013-04-30 18:48:54 -04004154 if (nflavs != supported)
4155 report = false;
4156 *flavorsp = htonl(supported);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004157 return 0;
Andy Adamsondcb488a32007-07-17 04:04:51 -07004158}
4159
Mi Jinlong22b6dee2010-12-27 14:29:57 +08004160static __be32
4161nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
4162 struct nfsd4_secinfo *secinfo)
4163{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004164 struct xdr_stream *xdr = &resp->xdr;
4165
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004166 return nfsd4_do_encode_secinfo(xdr, secinfo->si_exp);
Mi Jinlong22b6dee2010-12-27 14:29:57 +08004167}
4168
4169static __be32
4170nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
4171 struct nfsd4_secinfo_no_name *secinfo)
4172{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004173 struct xdr_stream *xdr = &resp->xdr;
4174
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004175 return nfsd4_do_encode_secinfo(xdr, secinfo->sin_exp);
Mi Jinlong22b6dee2010-12-27 14:29:57 +08004176}
4177
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178/*
4179 * The SETATTR encode routine is special -- it always encodes a bitmap,
4180 * regardless of the error status.
4181 */
Benny Halevy695e12f2008-07-04 14:38:33 +03004182static __be32
Al Virob37ad282006-10-19 23:28:59 -07004183nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004185 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004186 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004188 p = xdr_reserve_space(xdr, 16);
4189 if (!p)
4190 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 if (nfserr) {
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004192 *p++ = cpu_to_be32(3);
4193 *p++ = cpu_to_be32(0);
4194 *p++ = cpu_to_be32(0);
4195 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 }
4197 else {
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004198 *p++ = cpu_to_be32(3);
4199 *p++ = cpu_to_be32(setattr->sa_bmval[0]);
4200 *p++ = cpu_to_be32(setattr->sa_bmval[1]);
4201 *p++ = cpu_to_be32(setattr->sa_bmval[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 }
Benny Halevy695e12f2008-07-04 14:38:33 +03004203 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204}
4205
Benny Halevy695e12f2008-07-04 14:38:33 +03004206static __be32
Al Virob37ad282006-10-19 23:28:59 -07004207nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004209 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004210 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211
4212 if (!nfserr) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004213 p = xdr_reserve_space(xdr, 8 + NFS4_VERIFIER_SIZE);
4214 if (!p)
4215 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004216 p = xdr_encode_opaque_fixed(p, &scd->se_clientid, 8);
4217 p = xdr_encode_opaque_fixed(p, &scd->se_confirm,
4218 NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 }
4220 else if (nfserr == nfserr_clid_inuse) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004221 p = xdr_reserve_space(xdr, 8);
4222 if (!p)
4223 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004224 *p++ = cpu_to_be32(0);
4225 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 }
Benny Halevy695e12f2008-07-04 14:38:33 +03004227 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228}
4229
Benny Halevy695e12f2008-07-04 14:38:33 +03004230static __be32
Al Virob37ad282006-10-19 23:28:59 -07004231nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004233 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004234 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004236 p = xdr_reserve_space(xdr, 16);
4237 if (!p)
4238 return nfserr_resource;
4239 *p++ = cpu_to_be32(write->wr_bytes_written);
4240 *p++ = cpu_to_be32(write->wr_how_written);
4241 p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
4242 NFS4_VERIFIER_SIZE);
4243 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244}
4245
Benny Halevy695e12f2008-07-04 14:38:33 +03004246static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004247nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03004248 struct nfsd4_exchange_id *exid)
4249{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004250 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004251 __be32 *p;
Andy Adamson0733d212009-04-03 08:28:01 +03004252 char *major_id;
4253 char *server_scope;
4254 int major_id_sz;
4255 int server_scope_sz;
4256 uint64_t minor_id = 0;
Scott Mayhew7627d7d2020-02-19 15:52:15 -05004257 struct nfsd_net *nn = net_generic(SVC_NET(resp->rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03004258
Scott Mayhew7627d7d2020-02-19 15:52:15 -05004259 major_id = nn->nfsd_name;
4260 major_id_sz = strlen(nn->nfsd_name);
4261 server_scope = nn->nfsd_name;
4262 server_scope_sz = strlen(nn->nfsd_name);
Andy Adamson0733d212009-04-03 08:28:01 +03004263
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004264 p = xdr_reserve_space(xdr,
Andy Adamson0733d212009-04-03 08:28:01 +03004265 8 /* eir_clientid */ +
4266 4 /* eir_sequenceid */ +
4267 4 /* eir_flags */ +
Kinglong Meea8bb84b2013-12-10 15:24:36 +08004268 4 /* spr_how */);
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004269 if (!p)
4270 return nfserr_resource;
Andy Adamson0733d212009-04-03 08:28:01 +03004271
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004272 p = xdr_encode_opaque_fixed(p, &exid->clientid, 8);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004273 *p++ = cpu_to_be32(exid->seqid);
4274 *p++ = cpu_to_be32(exid->flags);
Andy Adamson0733d212009-04-03 08:28:01 +03004275
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004276 *p++ = cpu_to_be32(exid->spa_how);
Kinglong Meea8bb84b2013-12-10 15:24:36 +08004277
J. Bruce Fields57266a62013-04-13 14:27:29 -04004278 switch (exid->spa_how) {
4279 case SP4_NONE:
4280 break;
4281 case SP4_MACH_CRED:
4282 /* spo_must_enforce bitmap: */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004283 nfserr = nfsd4_encode_bitmap(xdr,
Andrew Elbleed941642016-06-15 12:52:09 -04004284 exid->spo_must_enforce[0],
4285 exid->spo_must_enforce[1],
4286 exid->spo_must_enforce[2]);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004287 if (nfserr)
4288 return nfserr;
Andrew Elbleed941642016-06-15 12:52:09 -04004289 /* spo_must_allow bitmap: */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004290 nfserr = nfsd4_encode_bitmap(xdr,
Andrew Elbleed941642016-06-15 12:52:09 -04004291 exid->spo_must_allow[0],
4292 exid->spo_must_allow[1],
4293 exid->spo_must_allow[2]);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004294 if (nfserr)
4295 return nfserr;
J. Bruce Fields57266a62013-04-13 14:27:29 -04004296 break;
4297 default:
4298 WARN_ON_ONCE(1);
4299 }
Andy Adamson0733d212009-04-03 08:28:01 +03004300
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004301 p = xdr_reserve_space(xdr,
Kinglong Meea8bb84b2013-12-10 15:24:36 +08004302 8 /* so_minor_id */ +
4303 4 /* so_major_id.len */ +
4304 (XDR_QUADLEN(major_id_sz) * 4) +
4305 4 /* eir_server_scope.len */ +
4306 (XDR_QUADLEN(server_scope_sz) * 4) +
4307 4 /* eir_server_impl_id.count (0) */);
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004308 if (!p)
4309 return nfserr_resource;
Kinglong Meea8bb84b2013-12-10 15:24:36 +08004310
Andy Adamson0733d212009-04-03 08:28:01 +03004311 /* The server_owner struct */
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04004312 p = xdr_encode_hyper(p, minor_id); /* Minor id */
Andy Adamson0733d212009-04-03 08:28:01 +03004313 /* major id */
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004314 p = xdr_encode_opaque(p, major_id, major_id_sz);
Andy Adamson0733d212009-04-03 08:28:01 +03004315
4316 /* Server scope */
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004317 p = xdr_encode_opaque(p, server_scope, server_scope_sz);
Andy Adamson0733d212009-04-03 08:28:01 +03004318
4319 /* Implementation id */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004320 *p++ = cpu_to_be32(0); /* zero length nfs_impl_id4 array */
Andy Adamson0733d212009-04-03 08:28:01 +03004321 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03004322}
4323
4324static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004325nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03004326 struct nfsd4_create_session *sess)
4327{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004328 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004329 __be32 *p;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004330
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004331 p = xdr_reserve_space(xdr, 24);
4332 if (!p)
4333 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004334 p = xdr_encode_opaque_fixed(p, sess->sessionid.data,
4335 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004336 *p++ = cpu_to_be32(sess->seqid);
4337 *p++ = cpu_to_be32(sess->flags);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004338
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004339 p = xdr_reserve_space(xdr, 28);
4340 if (!p)
4341 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004342 *p++ = cpu_to_be32(0); /* headerpadsz */
4343 *p++ = cpu_to_be32(sess->fore_channel.maxreq_sz);
4344 *p++ = cpu_to_be32(sess->fore_channel.maxresp_sz);
4345 *p++ = cpu_to_be32(sess->fore_channel.maxresp_cached);
4346 *p++ = cpu_to_be32(sess->fore_channel.maxops);
4347 *p++ = cpu_to_be32(sess->fore_channel.maxreqs);
4348 *p++ = cpu_to_be32(sess->fore_channel.nr_rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004349
4350 if (sess->fore_channel.nr_rdma_attrs) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004351 p = xdr_reserve_space(xdr, 4);
4352 if (!p)
4353 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004354 *p++ = cpu_to_be32(sess->fore_channel.rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004355 }
4356
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004357 p = xdr_reserve_space(xdr, 28);
4358 if (!p)
4359 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004360 *p++ = cpu_to_be32(0); /* headerpadsz */
4361 *p++ = cpu_to_be32(sess->back_channel.maxreq_sz);
4362 *p++ = cpu_to_be32(sess->back_channel.maxresp_sz);
4363 *p++ = cpu_to_be32(sess->back_channel.maxresp_cached);
4364 *p++ = cpu_to_be32(sess->back_channel.maxops);
4365 *p++ = cpu_to_be32(sess->back_channel.maxreqs);
4366 *p++ = cpu_to_be32(sess->back_channel.nr_rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004367
4368 if (sess->back_channel.nr_rdma_attrs) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004369 p = xdr_reserve_space(xdr, 4);
4370 if (!p)
4371 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004372 *p++ = cpu_to_be32(sess->back_channel.rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004373 }
4374 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03004375}
4376
4377static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004378nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03004379 struct nfsd4_sequence *seq)
4380{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004381 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004382 __be32 *p;
Benny Halevyb85d4c02009-04-03 08:28:08 +03004383
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004384 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 20);
4385 if (!p)
4386 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004387 p = xdr_encode_opaque_fixed(p, seq->sessionid.data,
4388 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004389 *p++ = cpu_to_be32(seq->seqid);
4390 *p++ = cpu_to_be32(seq->slotid);
J. Bruce Fieldsb7d7ca32011-08-31 15:39:30 -04004391 /* Note slotid's are numbered from zero: */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004392 *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_highest_slotid */
4393 *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_target_highest_slotid */
4394 *p++ = cpu_to_be32(seq->status_flags);
Benny Halevyb85d4c02009-04-03 08:28:08 +03004395
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04004396 resp->cstate.data_offset = xdr->buf->len; /* DRC cache data pointer */
Benny Halevyb85d4c02009-04-03 08:28:08 +03004397 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03004398}
4399
J. Bruce Fields2355c592012-04-25 16:56:22 -04004400static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004401nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
Bryan Schumaker17456802011-07-13 10:50:48 -04004402 struct nfsd4_test_stateid *test_stateid)
4403{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004404 struct xdr_stream *xdr = &resp->xdr;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004405 struct nfsd4_test_stateid_id *stateid, *next;
Bryan Schumaker17456802011-07-13 10:50:48 -04004406 __be32 *p;
Bryan Schumaker17456802011-07-13 10:50:48 -04004407
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004408 p = xdr_reserve_space(xdr, 4 + (4 * test_stateid->ts_num_ids));
4409 if (!p)
4410 return nfserr_resource;
Bryan Schumaker17456802011-07-13 10:50:48 -04004411 *p++ = htonl(test_stateid->ts_num_ids);
Bryan Schumaker17456802011-07-13 10:50:48 -04004412
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004413 list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
Al Viro02f5fde2012-04-13 00:10:34 -04004414 *p++ = stateid->ts_id_status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004415 }
Bryan Schumaker17456802011-07-13 10:50:48 -04004416
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004417 return 0;
Bryan Schumaker17456802011-07-13 10:50:48 -04004418}
4419
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004420#ifdef CONFIG_NFSD_PNFS
4421static __be32
4422nfsd4_encode_getdeviceinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
4423 struct nfsd4_getdeviceinfo *gdev)
4424{
4425 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004426 const struct nfsd4_layout_ops *ops;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004427 u32 starting_len = xdr->buf->len, needed_len;
4428 __be32 *p;
4429
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004430 p = xdr_reserve_space(xdr, 4);
4431 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004432 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004433
4434 *p++ = cpu_to_be32(gdev->gd_layout_type);
4435
4436 /* If maxcount is 0 then just update notifications */
4437 if (gdev->gd_maxcount != 0) {
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004438 ops = nfsd4_layout_ops[gdev->gd_layout_type];
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004439 nfserr = ops->encode_getdeviceinfo(xdr, gdev);
4440 if (nfserr) {
4441 /*
4442 * We don't bother to burden the layout drivers with
4443 * enforcing gd_maxcount, just tell the client to
4444 * come back with a bigger buffer if it's not enough.
4445 */
4446 if (xdr->buf->len + 4 > gdev->gd_maxcount)
4447 goto toosmall;
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004448 return nfserr;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004449 }
4450 }
4451
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004452 if (gdev->gd_notify_types) {
4453 p = xdr_reserve_space(xdr, 4 + 4);
4454 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004455 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004456 *p++ = cpu_to_be32(1); /* bitmap length */
4457 *p++ = cpu_to_be32(gdev->gd_notify_types);
4458 } else {
4459 p = xdr_reserve_space(xdr, 4);
4460 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004461 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004462 *p++ = 0;
4463 }
4464
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004465 return 0;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004466toosmall:
4467 dprintk("%s: maxcount too small\n", __func__);
4468 needed_len = xdr->buf->len + 4 /* notifications */;
4469 xdr_truncate_encode(xdr, starting_len);
4470 p = xdr_reserve_space(xdr, 4);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004471 if (!p)
4472 return nfserr_resource;
4473 *p++ = cpu_to_be32(needed_len);
4474 return nfserr_toosmall;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004475}
4476
4477static __be32
4478nfsd4_encode_layoutget(struct nfsd4_compoundres *resp, __be32 nfserr,
4479 struct nfsd4_layoutget *lgp)
4480{
4481 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004482 const struct nfsd4_layout_ops *ops;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004483 __be32 *p;
4484
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004485 p = xdr_reserve_space(xdr, 36 + sizeof(stateid_opaque_t));
4486 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004487 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004488
4489 *p++ = cpu_to_be32(1); /* we always set return-on-close */
4490 *p++ = cpu_to_be32(lgp->lg_sid.si_generation);
4491 p = xdr_encode_opaque_fixed(p, &lgp->lg_sid.si_opaque,
4492 sizeof(stateid_opaque_t));
4493
4494 *p++ = cpu_to_be32(1); /* we always return a single layout */
4495 p = xdr_encode_hyper(p, lgp->lg_seg.offset);
4496 p = xdr_encode_hyper(p, lgp->lg_seg.length);
4497 *p++ = cpu_to_be32(lgp->lg_seg.iomode);
4498 *p++ = cpu_to_be32(lgp->lg_layout_type);
4499
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004500 ops = nfsd4_layout_ops[lgp->lg_layout_type];
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004501 return ops->encode_layoutget(xdr, lgp);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004502}
4503
4504static __be32
4505nfsd4_encode_layoutcommit(struct nfsd4_compoundres *resp, __be32 nfserr,
4506 struct nfsd4_layoutcommit *lcp)
4507{
4508 struct xdr_stream *xdr = &resp->xdr;
4509 __be32 *p;
4510
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004511 p = xdr_reserve_space(xdr, 4);
4512 if (!p)
4513 return nfserr_resource;
4514 *p++ = cpu_to_be32(lcp->lc_size_chg);
4515 if (lcp->lc_size_chg) {
4516 p = xdr_reserve_space(xdr, 8);
4517 if (!p)
4518 return nfserr_resource;
4519 p = xdr_encode_hyper(p, lcp->lc_newsize);
4520 }
4521
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004522 return 0;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004523}
4524
4525static __be32
4526nfsd4_encode_layoutreturn(struct nfsd4_compoundres *resp, __be32 nfserr,
4527 struct nfsd4_layoutreturn *lrp)
4528{
4529 struct xdr_stream *xdr = &resp->xdr;
4530 __be32 *p;
4531
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004532 p = xdr_reserve_space(xdr, 4);
4533 if (!p)
4534 return nfserr_resource;
4535 *p++ = cpu_to_be32(lrp->lrs_present);
4536 if (lrp->lrs_present)
Kinglong Mee376675d2015-03-22 22:17:10 +08004537 return nfsd4_encode_stateid(xdr, &lrp->lr_sid);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004538 return 0;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004539}
4540#endif /* CONFIG_NFSD_PNFS */
4541
Andy Adamson2db134e2009-04-03 08:27:55 +03004542static __be32
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004543nfsd42_encode_write_res(struct nfsd4_compoundres *resp,
4544 struct nfsd42_write_res *write, bool sync)
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004545{
4546 __be32 *p;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004547 p = xdr_reserve_space(&resp->xdr, 4);
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004548 if (!p)
4549 return nfserr_resource;
4550
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004551 if (sync)
4552 *p++ = cpu_to_be32(0);
4553 else {
4554 __be32 nfserr;
4555 *p++ = cpu_to_be32(1);
4556 nfserr = nfsd4_encode_stateid(&resp->xdr, &write->cb_stateid);
4557 if (nfserr)
4558 return nfserr;
4559 }
4560 p = xdr_reserve_space(&resp->xdr, 8 + 4 + NFS4_VERIFIER_SIZE);
4561 if (!p)
4562 return nfserr_resource;
4563
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004564 p = xdr_encode_hyper(p, write->wr_bytes_written);
4565 *p++ = cpu_to_be32(write->wr_stable_how);
4566 p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
4567 NFS4_VERIFIER_SIZE);
4568 return nfs_ok;
4569}
4570
4571static __be32
Olga Kornievskaia51911862019-08-08 11:14:59 -04004572nfsd42_encode_nl4_server(struct nfsd4_compoundres *resp, struct nl4_server *ns)
4573{
4574 struct xdr_stream *xdr = &resp->xdr;
4575 struct nfs42_netaddr *addr;
4576 __be32 *p;
4577
4578 p = xdr_reserve_space(xdr, 4);
4579 *p++ = cpu_to_be32(ns->nl4_type);
4580
4581 switch (ns->nl4_type) {
4582 case NL4_NETADDR:
4583 addr = &ns->u.nl4_addr;
4584
4585 /* netid_len, netid, uaddr_len, uaddr (port included
4586 * in RPCBIND_MAXUADDRLEN)
4587 */
4588 p = xdr_reserve_space(xdr,
4589 4 /* netid len */ +
4590 (XDR_QUADLEN(addr->netid_len) * 4) +
4591 4 /* uaddr len */ +
4592 (XDR_QUADLEN(addr->addr_len) * 4));
4593 if (!p)
4594 return nfserr_resource;
4595
4596 *p++ = cpu_to_be32(addr->netid_len);
4597 p = xdr_encode_opaque_fixed(p, addr->netid,
4598 addr->netid_len);
4599 *p++ = cpu_to_be32(addr->addr_len);
4600 p = xdr_encode_opaque_fixed(p, addr->addr,
4601 addr->addr_len);
4602 break;
4603 default:
4604 WARN_ON_ONCE(ns->nl4_type != NL4_NETADDR);
4605 return nfserr_inval;
4606 }
4607
4608 return 0;
4609}
4610
4611static __be32
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004612nfsd4_encode_copy(struct nfsd4_compoundres *resp, __be32 nfserr,
4613 struct nfsd4_copy *copy)
4614{
4615 __be32 *p;
4616
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004617 nfserr = nfsd42_encode_write_res(resp, &copy->cp_res,
4618 copy->cp_synchronous);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004619 if (nfserr)
4620 return nfserr;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004621
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004622 p = xdr_reserve_space(&resp->xdr, 4 + 4);
J. Bruce Fieldsedcc8452018-03-07 15:37:35 -05004623 *p++ = xdr_one; /* cr_consecutive */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004624 *p++ = cpu_to_be32(copy->cp_synchronous);
4625 return 0;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004626}
4627
4628static __be32
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04004629nfsd4_encode_offload_status(struct nfsd4_compoundres *resp, __be32 nfserr,
4630 struct nfsd4_offload_status *os)
4631{
4632 struct xdr_stream *xdr = &resp->xdr;
4633 __be32 *p;
4634
4635 p = xdr_reserve_space(xdr, 8 + 4);
4636 if (!p)
4637 return nfserr_resource;
4638 p = xdr_encode_hyper(p, os->count);
4639 *p++ = cpu_to_be32(0);
Anna Schumaker528b8492020-09-28 13:08:58 -04004640 return nfserr;
4641}
4642
4643static __be32
4644nfsd4_encode_read_plus_data(struct nfsd4_compoundres *resp,
4645 struct nfsd4_read *read,
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004646 unsigned long *maxcount, u32 *eof,
4647 loff_t *pos)
Anna Schumaker528b8492020-09-28 13:08:58 -04004648{
4649 struct xdr_stream *xdr = &resp->xdr;
4650 struct file *file = read->rd_nf->nf_file;
4651 int starting_len = xdr->buf->len;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004652 loff_t hole_pos;
Anna Schumaker528b8492020-09-28 13:08:58 -04004653 __be32 nfserr;
4654 __be32 *p, tmp;
4655 __be64 tmp64;
4656
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004657 hole_pos = pos ? *pos : vfs_llseek(file, read->rd_offset, SEEK_HOLE);
Anna Schumaker2db27992020-09-28 13:08:59 -04004658 if (hole_pos > read->rd_offset)
Anna Schumaker278765e2020-09-28 13:09:00 -04004659 *maxcount = min_t(unsigned long, *maxcount, hole_pos - read->rd_offset);
4660 *maxcount = min_t(unsigned long, *maxcount, (xdr->buf->buflen - xdr->buf->len));
Anna Schumaker528b8492020-09-28 13:08:58 -04004661
4662 /* Content type, offset, byte count */
4663 p = xdr_reserve_space(xdr, 4 + 8 + 4);
4664 if (!p)
4665 return nfserr_resource;
4666
Anna Schumaker278765e2020-09-28 13:09:00 -04004667 read->rd_vlen = xdr_reserve_space_vec(xdr, resp->rqstp->rq_vec, *maxcount);
Anna Schumaker528b8492020-09-28 13:08:58 -04004668 if (read->rd_vlen < 0)
4669 return nfserr_resource;
4670
4671 nfserr = nfsd_readv(resp->rqstp, read->rd_fhp, file, read->rd_offset,
Anna Schumaker278765e2020-09-28 13:09:00 -04004672 resp->rqstp->rq_vec, read->rd_vlen, maxcount, eof);
Anna Schumaker528b8492020-09-28 13:08:58 -04004673 if (nfserr)
4674 return nfserr;
4675
4676 tmp = htonl(NFS4_CONTENT_DATA);
4677 write_bytes_to_xdr_buf(xdr->buf, starting_len, &tmp, 4);
4678 tmp64 = cpu_to_be64(read->rd_offset);
4679 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp64, 8);
Anna Schumaker278765e2020-09-28 13:09:00 -04004680 tmp = htonl(*maxcount);
Anna Schumaker528b8492020-09-28 13:08:58 -04004681 write_bytes_to_xdr_buf(xdr->buf, starting_len + 12, &tmp, 4);
4682 return nfs_ok;
4683}
4684
4685static __be32
Anna Schumaker2db27992020-09-28 13:08:59 -04004686nfsd4_encode_read_plus_hole(struct nfsd4_compoundres *resp,
4687 struct nfsd4_read *read,
Anna Schumaker278765e2020-09-28 13:09:00 -04004688 unsigned long *maxcount, u32 *eof)
Anna Schumaker2db27992020-09-28 13:08:59 -04004689{
4690 struct file *file = read->rd_nf->nf_file;
Anna Schumaker278765e2020-09-28 13:09:00 -04004691 loff_t data_pos = vfs_llseek(file, read->rd_offset, SEEK_DATA);
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004692 loff_t f_size = i_size_read(file_inode(file));
Anna Schumaker278765e2020-09-28 13:09:00 -04004693 unsigned long count;
Anna Schumaker2db27992020-09-28 13:08:59 -04004694 __be32 *p;
4695
Anna Schumaker278765e2020-09-28 13:09:00 -04004696 if (data_pos == -ENXIO)
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004697 data_pos = f_size;
4698 else if (data_pos <= read->rd_offset || (data_pos < f_size && data_pos % PAGE_SIZE))
4699 return nfsd4_encode_read_plus_data(resp, read, maxcount, eof, &f_size);
Anna Schumaker278765e2020-09-28 13:09:00 -04004700 count = data_pos - read->rd_offset;
4701
Anna Schumaker2db27992020-09-28 13:08:59 -04004702 /* Content type, offset, byte count */
4703 p = xdr_reserve_space(&resp->xdr, 4 + 8 + 8);
4704 if (!p)
4705 return nfserr_resource;
4706
4707 *p++ = htonl(NFS4_CONTENT_HOLE);
4708 p = xdr_encode_hyper(p, read->rd_offset);
Anna Schumaker278765e2020-09-28 13:09:00 -04004709 p = xdr_encode_hyper(p, count);
Anna Schumaker2db27992020-09-28 13:08:59 -04004710
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004711 *eof = (read->rd_offset + count) >= f_size;
Anna Schumaker278765e2020-09-28 13:09:00 -04004712 *maxcount = min_t(unsigned long, count, *maxcount);
Anna Schumaker2db27992020-09-28 13:08:59 -04004713 return nfs_ok;
4714}
4715
4716static __be32
Anna Schumaker528b8492020-09-28 13:08:58 -04004717nfsd4_encode_read_plus(struct nfsd4_compoundres *resp, __be32 nfserr,
4718 struct nfsd4_read *read)
4719{
Anna Schumaker278765e2020-09-28 13:09:00 -04004720 unsigned long maxcount, count;
Anna Schumaker528b8492020-09-28 13:08:58 -04004721 struct xdr_stream *xdr = &resp->xdr;
4722 struct file *file;
4723 int starting_len = xdr->buf->len;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004724 int last_segment = xdr->buf->len;
Anna Schumaker528b8492020-09-28 13:08:58 -04004725 int segments = 0;
4726 __be32 *p, tmp;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004727 bool is_data;
Anna Schumaker2db27992020-09-28 13:08:59 -04004728 loff_t pos;
Anna Schumaker528b8492020-09-28 13:08:58 -04004729 u32 eof;
4730
4731 if (nfserr)
4732 return nfserr;
4733 file = read->rd_nf->nf_file;
4734
4735 /* eof flag, segment count */
4736 p = xdr_reserve_space(xdr, 4 + 4);
4737 if (!p)
4738 return nfserr_resource;
4739 xdr_commit_encode(xdr);
4740
4741 maxcount = svc_max_payload(resp->rqstp);
4742 maxcount = min_t(unsigned long, maxcount,
4743 (xdr->buf->buflen - xdr->buf->len));
4744 maxcount = min_t(unsigned long, maxcount, read->rd_length);
Anna Schumaker278765e2020-09-28 13:09:00 -04004745 count = maxcount;
Anna Schumaker528b8492020-09-28 13:08:58 -04004746
4747 eof = read->rd_offset >= i_size_read(file_inode(file));
Anna Schumaker2db27992020-09-28 13:08:59 -04004748 if (eof)
4749 goto out;
4750
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004751 pos = vfs_llseek(file, read->rd_offset, SEEK_HOLE);
4752 is_data = pos > read->rd_offset;
Anna Schumaker2db27992020-09-28 13:08:59 -04004753
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004754 while (count > 0 && !eof) {
Anna Schumaker278765e2020-09-28 13:09:00 -04004755 maxcount = count;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004756 if (is_data)
4757 nfserr = nfsd4_encode_read_plus_data(resp, read, &maxcount, &eof,
4758 segments == 0 ? &pos : NULL);
4759 else
4760 nfserr = nfsd4_encode_read_plus_hole(resp, read, &maxcount, &eof);
Anna Schumaker278765e2020-09-28 13:09:00 -04004761 if (nfserr)
4762 goto out;
4763 count -= maxcount;
4764 read->rd_offset += maxcount;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004765 is_data = !is_data;
4766 last_segment = xdr->buf->len;
Anna Schumaker528b8492020-09-28 13:08:58 -04004767 segments++;
4768 }
4769
Anna Schumaker2db27992020-09-28 13:08:59 -04004770out:
Anna Schumaker278765e2020-09-28 13:09:00 -04004771 if (nfserr && segments == 0)
Anna Schumaker528b8492020-09-28 13:08:58 -04004772 xdr_truncate_encode(xdr, starting_len);
4773 else {
4774 tmp = htonl(eof);
4775 write_bytes_to_xdr_buf(xdr->buf, starting_len, &tmp, 4);
4776 tmp = htonl(segments);
4777 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp, 4);
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004778 if (nfserr) {
4779 xdr_truncate_encode(xdr, last_segment);
4780 nfserr = nfs_ok;
4781 }
Anna Schumaker528b8492020-09-28 13:08:58 -04004782 }
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04004783
4784 return nfserr;
4785}
4786
4787static __be32
Olga Kornievskaia51911862019-08-08 11:14:59 -04004788nfsd4_encode_copy_notify(struct nfsd4_compoundres *resp, __be32 nfserr,
4789 struct nfsd4_copy_notify *cn)
4790{
4791 struct xdr_stream *xdr = &resp->xdr;
4792 __be32 *p;
4793
4794 if (nfserr)
4795 return nfserr;
4796
4797 /* 8 sec, 4 nsec */
4798 p = xdr_reserve_space(xdr, 12);
4799 if (!p)
4800 return nfserr_resource;
4801
4802 /* cnr_lease_time */
4803 p = xdr_encode_hyper(p, cn->cpn_sec);
4804 *p++ = cpu_to_be32(cn->cpn_nsec);
4805
4806 /* cnr_stateid */
4807 nfserr = nfsd4_encode_stateid(xdr, &cn->cpn_cnr_stateid);
4808 if (nfserr)
4809 return nfserr;
4810
4811 /* cnr_src.nl_nsvr */
4812 p = xdr_reserve_space(xdr, 4);
4813 if (!p)
4814 return nfserr_resource;
4815
4816 *p++ = cpu_to_be32(1);
4817
4818 return nfsd42_encode_nl4_server(resp, &cn->cpn_src);
4819}
4820
4821static __be32
Anna Schumaker24bab492014-09-26 13:58:27 -04004822nfsd4_encode_seek(struct nfsd4_compoundres *resp, __be32 nfserr,
4823 struct nfsd4_seek *seek)
4824{
4825 __be32 *p;
4826
Anna Schumaker24bab492014-09-26 13:58:27 -04004827 p = xdr_reserve_space(&resp->xdr, 4 + 8);
4828 *p++ = cpu_to_be32(seek->seek_eof);
4829 p = xdr_encode_hyper(p, seek->seek_pos);
4830
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004831 return 0;
Anna Schumaker24bab492014-09-26 13:58:27 -04004832}
4833
4834static __be32
Benny Halevy695e12f2008-07-04 14:38:33 +03004835nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
4836{
4837 return nfserr;
4838}
4839
Frank van der Linden23e50fe2020-06-23 22:39:26 +00004840/*
4841 * Encode kmalloc-ed buffer in to XDR stream.
4842 */
Chuck Leverb9a49232020-09-11 14:47:53 -04004843static __be32
Frank van der Linden23e50fe2020-06-23 22:39:26 +00004844nfsd4_vbuf_to_stream(struct xdr_stream *xdr, char *buf, u32 buflen)
4845{
4846 u32 cplen;
4847 __be32 *p;
4848
4849 cplen = min_t(unsigned long, buflen,
4850 ((void *)xdr->end - (void *)xdr->p));
4851 p = xdr_reserve_space(xdr, cplen);
4852 if (!p)
4853 return nfserr_resource;
4854
4855 memcpy(p, buf, cplen);
4856 buf += cplen;
4857 buflen -= cplen;
4858
4859 while (buflen) {
4860 cplen = min_t(u32, buflen, PAGE_SIZE);
4861 p = xdr_reserve_space(xdr, cplen);
4862 if (!p)
4863 return nfserr_resource;
4864
4865 memcpy(p, buf, cplen);
4866
4867 if (cplen < PAGE_SIZE) {
4868 /*
4869 * We're done, with a length that wasn't page
4870 * aligned, so possibly not word aligned. Pad
4871 * any trailing bytes with 0.
4872 */
4873 xdr_encode_opaque_fixed(p, NULL, cplen);
4874 break;
4875 }
4876
4877 buflen -= PAGE_SIZE;
4878 buf += PAGE_SIZE;
4879 }
4880
4881 return 0;
4882}
4883
4884static __be32
4885nfsd4_encode_getxattr(struct nfsd4_compoundres *resp, __be32 nfserr,
4886 struct nfsd4_getxattr *getxattr)
4887{
4888 struct xdr_stream *xdr = &resp->xdr;
4889 __be32 *p, err;
4890
4891 p = xdr_reserve_space(xdr, 4);
4892 if (!p)
4893 return nfserr_resource;
4894
4895 *p = cpu_to_be32(getxattr->getxa_len);
4896
4897 if (getxattr->getxa_len == 0)
4898 return 0;
4899
4900 err = nfsd4_vbuf_to_stream(xdr, getxattr->getxa_buf,
4901 getxattr->getxa_len);
4902
4903 kvfree(getxattr->getxa_buf);
4904
4905 return err;
4906}
4907
4908static __be32
4909nfsd4_encode_setxattr(struct nfsd4_compoundres *resp, __be32 nfserr,
4910 struct nfsd4_setxattr *setxattr)
4911{
4912 struct xdr_stream *xdr = &resp->xdr;
4913 __be32 *p;
4914
4915 p = xdr_reserve_space(xdr, 20);
4916 if (!p)
4917 return nfserr_resource;
4918
4919 encode_cinfo(p, &setxattr->setxa_cinfo);
4920
4921 return 0;
4922}
4923
4924/*
4925 * See if there are cookie values that can be rejected outright.
4926 */
4927static __be32
4928nfsd4_listxattr_validate_cookie(struct nfsd4_listxattrs *listxattrs,
4929 u32 *offsetp)
4930{
4931 u64 cookie = listxattrs->lsxa_cookie;
4932
4933 /*
4934 * If the cookie is larger than the maximum number we can fit
4935 * in either the buffer we just got back from vfs_listxattr, or,
4936 * XDR-encoded, in the return buffer, it's invalid.
4937 */
4938 if (cookie > (listxattrs->lsxa_len) / (XATTR_USER_PREFIX_LEN + 2))
4939 return nfserr_badcookie;
4940
4941 if (cookie > (listxattrs->lsxa_maxcount /
4942 (XDR_QUADLEN(XATTR_USER_PREFIX_LEN + 2) + 4)))
4943 return nfserr_badcookie;
4944
4945 *offsetp = (u32)cookie;
4946 return 0;
4947}
4948
4949static __be32
4950nfsd4_encode_listxattrs(struct nfsd4_compoundres *resp, __be32 nfserr,
4951 struct nfsd4_listxattrs *listxattrs)
4952{
4953 struct xdr_stream *xdr = &resp->xdr;
4954 u32 cookie_offset, count_offset, eof;
4955 u32 left, xdrleft, slen, count;
4956 u32 xdrlen, offset;
4957 u64 cookie;
4958 char *sp;
Chuck Leverb9a49232020-09-11 14:47:53 -04004959 __be32 status, tmp;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00004960 __be32 *p;
4961 u32 nuser;
4962
4963 eof = 1;
4964
4965 status = nfsd4_listxattr_validate_cookie(listxattrs, &offset);
4966 if (status)
4967 goto out;
4968
4969 /*
4970 * Reserve space for the cookie and the name array count. Record
4971 * the offsets to save them later.
4972 */
4973 cookie_offset = xdr->buf->len;
4974 count_offset = cookie_offset + 8;
4975 p = xdr_reserve_space(xdr, 12);
4976 if (!p) {
4977 status = nfserr_resource;
4978 goto out;
4979 }
4980
4981 count = 0;
4982 left = listxattrs->lsxa_len;
4983 sp = listxattrs->lsxa_buf;
4984 nuser = 0;
4985
4986 xdrleft = listxattrs->lsxa_maxcount;
4987
4988 while (left > 0 && xdrleft > 0) {
4989 slen = strlen(sp);
4990
4991 /*
Alex Dewar4cce11fa2020-08-17 18:51:26 +01004992 * Check if this is a "user." attribute, skip it if not.
Frank van der Linden23e50fe2020-06-23 22:39:26 +00004993 */
4994 if (strncmp(sp, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
4995 goto contloop;
4996
4997 slen -= XATTR_USER_PREFIX_LEN;
4998 xdrlen = 4 + ((slen + 3) & ~3);
4999 if (xdrlen > xdrleft) {
5000 if (count == 0) {
5001 /*
5002 * Can't even fit the first attribute name.
5003 */
5004 status = nfserr_toosmall;
5005 goto out;
5006 }
5007 eof = 0;
5008 goto wreof;
5009 }
5010
5011 left -= XATTR_USER_PREFIX_LEN;
5012 sp += XATTR_USER_PREFIX_LEN;
5013 if (nuser++ < offset)
5014 goto contloop;
5015
5016
5017 p = xdr_reserve_space(xdr, xdrlen);
5018 if (!p) {
5019 status = nfserr_resource;
5020 goto out;
5021 }
5022
Alex Deware2a18402020-08-12 15:12:51 +01005023 xdr_encode_opaque(p, sp, slen);
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005024
5025 xdrleft -= xdrlen;
5026 count++;
5027contloop:
5028 sp += slen + 1;
5029 left -= slen + 1;
5030 }
5031
5032 /*
5033 * If there were user attributes to copy, but we didn't copy
5034 * any, the offset was too large (e.g. the cookie was invalid).
5035 */
5036 if (nuser > 0 && count == 0) {
5037 status = nfserr_badcookie;
5038 goto out;
5039 }
5040
5041wreof:
5042 p = xdr_reserve_space(xdr, 4);
5043 if (!p) {
5044 status = nfserr_resource;
5045 goto out;
5046 }
5047 *p = cpu_to_be32(eof);
5048
5049 cookie = offset + count;
5050
5051 write_bytes_to_xdr_buf(xdr->buf, cookie_offset, &cookie, 8);
Chuck Leverb9a49232020-09-11 14:47:53 -04005052 tmp = cpu_to_be32(count);
5053 write_bytes_to_xdr_buf(xdr->buf, count_offset, &tmp, 4);
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005054out:
5055 if (listxattrs->lsxa_len)
5056 kvfree(listxattrs->lsxa_buf);
5057 return status;
5058}
5059
5060static __be32
5061nfsd4_encode_removexattr(struct nfsd4_compoundres *resp, __be32 nfserr,
5062 struct nfsd4_removexattr *removexattr)
5063{
5064 struct xdr_stream *xdr = &resp->xdr;
5065 __be32 *p;
5066
5067 p = xdr_reserve_space(xdr, 20);
5068 if (!p)
5069 return nfserr_resource;
5070
5071 p = encode_cinfo(p, &removexattr->rmxa_cinfo);
5072 return 0;
5073}
5074
Benny Halevy695e12f2008-07-04 14:38:33 +03005075typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
5076
Andy Adamson2db134e2009-04-03 08:27:55 +03005077/*
5078 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
5079 * since we don't need to filter out obsolete ops as this is
5080 * done in the decoding phase.
5081 */
Chuck Leverc1df6092017-08-01 11:59:58 -04005082static const nfsd4_enc nfsd4_enc_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04005083 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
5084 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
5085 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
5086 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
5087 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
5088 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
5089 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
5090 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
5091 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
5092 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
5093 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
5094 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
5095 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
5096 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
5097 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
5098 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
Benny Halevy84f09f42009-03-04 23:05:35 +02005099 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04005100 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
5101 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
5102 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
5103 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
5104 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
5105 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
5106 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
5107 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
5108 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
5109 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
5110 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
5111 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
5112 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
5113 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
5114 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
5115 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
5116 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
5117 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
5118 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
5119 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03005120
5121 /* NFSv4.1 operations */
5122 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04005123 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
Andy Adamson2db134e2009-04-03 08:27:55 +03005124 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
5125 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
Kinglong Mee43212cc2013-12-09 19:04:23 +08005126 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_noop,
5127 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03005128 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02005129#ifdef CONFIG_NFSD_PNFS
5130 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_getdeviceinfo,
5131 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
5132 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_layoutcommit,
5133 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_layoutget,
5134 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_layoutreturn,
5135#else
Andy Adamson2db134e2009-04-03 08:27:55 +03005136 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
5137 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
5138 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
5139 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
5140 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02005141#endif
Mi Jinlong22b6dee2010-12-27 14:29:57 +08005142 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
Andy Adamson2db134e2009-04-03 08:27:55 +03005143 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
5144 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
Bryan Schumaker17456802011-07-13 10:50:48 -04005145 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
Andy Adamson2db134e2009-04-03 08:27:55 +03005146 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
5147 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
5148 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
Anna Schumaker87a15a82014-09-26 13:58:26 -04005149
5150 /* NFSv4.2 operations */
5151 [OP_ALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
Anna Schumaker29ae7f92016-09-07 15:57:30 -04005152 [OP_COPY] = (nfsd4_enc)nfsd4_encode_copy,
Olga Kornievskaia51911862019-08-08 11:14:59 -04005153 [OP_COPY_NOTIFY] = (nfsd4_enc)nfsd4_encode_copy_notify,
Anna Schumaker87a15a82014-09-26 13:58:26 -04005154 [OP_DEALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
5155 [OP_IO_ADVISE] = (nfsd4_enc)nfsd4_encode_noop,
5156 [OP_LAYOUTERROR] = (nfsd4_enc)nfsd4_encode_noop,
5157 [OP_LAYOUTSTATS] = (nfsd4_enc)nfsd4_encode_noop,
5158 [OP_OFFLOAD_CANCEL] = (nfsd4_enc)nfsd4_encode_noop,
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04005159 [OP_OFFLOAD_STATUS] = (nfsd4_enc)nfsd4_encode_offload_status,
Anna Schumaker528b8492020-09-28 13:08:58 -04005160 [OP_READ_PLUS] = (nfsd4_enc)nfsd4_encode_read_plus,
Anna Schumaker24bab492014-09-26 13:58:27 -04005161 [OP_SEEK] = (nfsd4_enc)nfsd4_encode_seek,
Anna Schumaker87a15a82014-09-26 13:58:26 -04005162 [OP_WRITE_SAME] = (nfsd4_enc)nfsd4_encode_noop,
Christoph Hellwigffa01602015-12-03 12:59:52 +01005163 [OP_CLONE] = (nfsd4_enc)nfsd4_encode_noop,
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005164
5165 /* RFC 8276 extended atributes operations */
5166 [OP_GETXATTR] = (nfsd4_enc)nfsd4_encode_getxattr,
5167 [OP_SETXATTR] = (nfsd4_enc)nfsd4_encode_setxattr,
5168 [OP_LISTXATTRS] = (nfsd4_enc)nfsd4_encode_listxattrs,
5169 [OP_REMOVEXATTR] = (nfsd4_enc)nfsd4_encode_removexattr,
Benny Halevy695e12f2008-07-04 14:38:33 +03005170};
5171
Andy Adamson496c2622009-04-03 08:28:48 +03005172/*
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04005173 * Calculate whether we still have space to encode repsize bytes.
5174 * There are two considerations:
5175 * - For NFS versions >=4.1, the size of the reply must stay within
5176 * session limits
5177 * - For all NFS versions, we must stay within limited preallocated
5178 * buffer space.
Andy Adamson496c2622009-04-03 08:28:48 +03005179 *
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04005180 * This is called before the operation is processed, so can only provide
5181 * an upper estimate. For some nonidempotent operations (such as
5182 * getattr), it's not necessarily a problem if that estimate is wrong,
5183 * as we can fail it after processing without significant side effects.
Andy Adamson496c2622009-04-03 08:28:48 +03005184 */
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04005185__be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 respsize)
Andy Adamson496c2622009-04-03 08:28:48 +03005186{
J. Bruce Fields67492c92014-03-08 16:42:05 -05005187 struct xdr_buf *buf = &resp->rqstp->rq_res;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04005188 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson496c2622009-04-03 08:28:48 +03005189
J. Bruce Fields47ee5292014-03-12 21:39:35 -04005190 if (buf->len + respsize <= buf->buflen)
5191 return nfs_ok;
5192 if (!nfsd4_has_session(&resp->cstate))
J. Bruce Fieldsea8d7722014-03-08 17:19:55 -05005193 return nfserr_resource;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04005194 if (slot->sl_flags & NFSD4_SLOT_CACHETHIS) {
5195 WARN_ON_ONCE(1);
5196 return nfserr_rep_too_big_to_cache;
J. Bruce Fieldsea8d7722014-03-08 17:19:55 -05005197 }
J. Bruce Fields47ee5292014-03-12 21:39:35 -04005198 return nfserr_rep_too_big;
Andy Adamson496c2622009-04-03 08:28:48 +03005199}
5200
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201void
5202nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
5203{
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005204 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005205 struct nfs4_stateowner *so = resp->cstate.replay_owner;
J. Bruce Fields5f4ab942014-03-07 11:01:37 -05005206 struct svc_rqst *rqstp = resp->rqstp;
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04005207 const struct nfsd4_operation *opdesc = op->opdesc;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005208 int post_err_offset;
J. Bruce Fields07d1f802014-03-06 20:39:29 -05005209 nfsd4_enc encoder;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07005210 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05005212 p = xdr_reserve_space(xdr, 8);
5213 if (!p) {
5214 WARN_ON_ONCE(1);
5215 return;
5216 }
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04005217 *p++ = cpu_to_be32(op->opnum);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005218 post_err_offset = xdr->buf->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219
Benny Halevy695e12f2008-07-04 14:38:33 +03005220 if (op->opnum == OP_ILLEGAL)
5221 goto status;
J. Bruce Fieldsb7571e42017-05-06 10:49:21 -04005222 if (op->status && opdesc &&
5223 !(opdesc->op_flags & OP_NONTRIVIAL_ERROR_ENCODE))
5224 goto status;
Benny Halevy695e12f2008-07-04 14:38:33 +03005225 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
5226 !nfsd4_enc_ops[op->opnum]);
J. Bruce Fields07d1f802014-03-06 20:39:29 -05005227 encoder = nfsd4_enc_ops[op->opnum];
5228 op->status = encoder(resp, op->status, &op->u);
Chuck Lever08281342020-11-21 11:36:42 -05005229 if (op->status)
5230 trace_nfsd_compound_encode_err(rqstp, op->opnum, op->status);
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04005231 if (opdesc && opdesc->op_release)
5232 opdesc->op_release(&op->u);
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04005233 xdr_commit_encode(xdr);
5234
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04005235 /* nfsd4_check_resp_size guarantees enough room for error status */
J. Bruce Fields5f4ab942014-03-07 11:01:37 -05005236 if (!op->status) {
5237 int space_needed = 0;
5238 if (!nfsd4_last_compound_op(rqstp))
5239 space_needed = COMPOUND_ERR_SLACK_SPACE;
5240 op->status = nfsd4_check_resp_size(resp, space_needed);
5241 }
J. Bruce Fieldsc8f13d92014-05-08 17:38:18 -04005242 if (op->status == nfserr_resource && nfsd4_has_session(&resp->cstate)) {
5243 struct nfsd4_slot *slot = resp->cstate.slot;
5244
5245 if (slot->sl_flags & NFSD4_SLOT_CACHETHIS)
5246 op->status = nfserr_rep_too_big_to_cache;
5247 else
5248 op->status = nfserr_rep_too_big;
5249 }
J. Bruce Fields07d1f802014-03-06 20:39:29 -05005250 if (op->status == nfserr_resource ||
5251 op->status == nfserr_rep_too_big ||
5252 op->status == nfserr_rep_too_big_to_cache) {
5253 /*
5254 * The operation may have already been encoded or
5255 * partially encoded. No op returns anything additional
5256 * in the case of one of these three errors, so we can
5257 * just truncate back to after the status. But it's a
5258 * bug if we had to do this on a non-idempotent op:
5259 */
5260 warn_on_nonidempotent_op(op);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005261 xdr_truncate_encode(xdr, post_err_offset);
J. Bruce Fields07d1f802014-03-06 20:39:29 -05005262 }
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005263 if (so) {
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005264 int len = xdr->buf->len - post_err_offset;
5265
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005266 so->so_replay.rp_status = op->status;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005267 so->so_replay.rp_buflen = len;
5268 read_bytes_from_xdr_buf(xdr->buf, post_err_offset,
5269 so->so_replay.rp_buf, len);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005270 }
Benny Halevy695e12f2008-07-04 14:38:33 +03005271status:
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005272 /* Note that op->status is already in network byte order: */
5273 write_bytes_to_xdr_buf(xdr->buf, post_err_offset - 4, &op->status, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274}
5275
5276/*
5277 * Encode the reply stored in the stateowner reply cache
5278 *
5279 * XDR note: do not encode rp->rp_buflen: the buffer contains the
5280 * previously sent already encoded operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 */
5282void
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05005283nfsd4_encode_replay(struct xdr_stream *xdr, struct nfsd4_op *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07005285 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 struct nfs4_replay *rp = op->replay;
5287
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05005288 p = xdr_reserve_space(xdr, 8 + rp->rp_buflen);
5289 if (!p) {
5290 WARN_ON_ONCE(1);
5291 return;
5292 }
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04005293 *p++ = cpu_to_be32(op->opnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 *p++ = rp->rp_status; /* already xdr'ed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04005296 p = xdr_encode_opaque_fixed(p, rp->rp_buf, rp->rp_buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297}
5298
Christoph Hellwig85374882017-05-08 18:48:24 +02005299void nfsd4_release_compoundargs(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300{
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04005301 struct nfsd4_compoundargs *args = rqstp->rq_argp;
5302
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 if (args->ops != args->iops) {
5304 kfree(args->ops);
5305 args->ops = args->iops;
5306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 while (args->to_free) {
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -04005308 struct svcxdr_tmpbuf *tb = args->to_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 args->to_free = tb->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 kfree(tb);
5311 }
5312}
5313
5314int
Christoph Hellwig026fec72017-05-08 19:01:48 +02005315nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316{
Christoph Hellwig026fec72017-05-08 19:01:48 +02005317 struct nfsd4_compoundargs *args = rqstp->rq_argp;
5318
Chuck Leverc1346a122020-11-03 11:54:23 -05005319 /* svcxdr_tmp_alloc */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 args->tmpp = NULL;
5321 args->to_free = NULL;
Chuck Leverc1346a122020-11-03 11:54:23 -05005322
5323 args->xdr = &rqstp->rq_arg_stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 args->ops = args->iops;
5325 args->rqstp = rqstp;
5326
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04005327 return !nfsd4_decode_compound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328}
5329
5330int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02005331nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02005333 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields6ac90392014-02-26 17:39:35 -05005334 struct xdr_buf *buf = resp->xdr.buf;
5335
5336 WARN_ON_ONCE(buf->len != buf->head[0].iov_len + buf->page_len +
5337 buf->tail[0].iov_len);
J. Bruce Fieldsdd97fdd2014-02-26 17:16:27 -05005338
Chuck Levercc028a12020-10-02 15:52:44 -04005339 *p = resp->cstate.status;
5340
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04005341 rqstp->rq_next_page = resp->xdr.page_ptr + 1;
5342
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 p = resp->tagp;
5344 *p++ = htonl(resp->taglen);
5345 memcpy(p, resp->tag, resp->taglen);
5346 p += XDR_QUADLEN(resp->taglen);
5347 *p++ = htonl(resp->opcnt);
5348
Trond Myklebustb6076642014-06-30 11:48:35 -04005349 nfsd4_sequence_done(resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 return 1;
5351}
5352
5353/*
5354 * Local variables:
5355 * c-basic-offset: 8
5356 * End:
5357 */