blob: 6f32901f804839ad9c3eec6f5430fcb40377f3be [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{
782 DECODE_HEAD;
783
784 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400785 link->li_namelen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 READ_BUF(link->li_namelen);
787 SAVEMEM(link->li_name, link->li_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500788 if ((status = check_filename(link->li_name, link->li_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return status;
790
791 DECODE_TAIL;
792}
793
Al Virob37ad282006-10-19 23:28:59 -0700794static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
796{
797 DECODE_HEAD;
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 /*
800 * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
801 */
802 READ_BUF(28);
J. Bruce Fields06553992014-06-02 12:32:51 -0400803 lock->lk_type = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
805 goto xdr_error;
J. Bruce Fields06553992014-06-02 12:32:51 -0400806 lock->lk_reclaim = be32_to_cpup(p++);
J. Bruce Fields542d1ab2014-06-02 12:45:31 -0400807 p = xdr_decode_hyper(p, &lock->lk_offset);
808 p = xdr_decode_hyper(p, &lock->lk_length);
J. Bruce Fields06553992014-06-02 12:32:51 -0400809 lock->lk_is_new = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 if (lock->lk_is_new) {
Benny Halevye31a1b62008-08-12 20:46:18 +0300812 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400813 lock->lk_new_open_seqid = be32_to_cpup(p++);
Benny Halevye31a1b62008-08-12 20:46:18 +0300814 status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
815 if (status)
816 return status;
817 READ_BUF(8 + sizeof(clientid_t));
J. Bruce Fields06553992014-06-02 12:32:51 -0400818 lock->lk_new_lock_seqid = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
J. Bruce Fields06553992014-06-02 12:32:51 -0400820 lock->lk_new_owner.len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 READ_BUF(lock->lk_new_owner.len);
822 READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
823 } else {
Benny Halevye31a1b62008-08-12 20:46:18 +0300824 status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
825 if (status)
826 return status;
827 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400828 lock->lk_old_lock_seqid = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830
831 DECODE_TAIL;
832}
833
Al Virob37ad282006-10-19 23:28:59 -0700834static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
836{
837 DECODE_HEAD;
838
839 READ_BUF(32);
J. Bruce Fields06553992014-06-02 12:32:51 -0400840 lockt->lt_type = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
842 goto xdr_error;
J. Bruce Fields542d1ab2014-06-02 12:45:31 -0400843 p = xdr_decode_hyper(p, &lockt->lt_offset);
844 p = xdr_decode_hyper(p, &lockt->lt_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 COPYMEM(&lockt->lt_clientid, 8);
J. Bruce Fields06553992014-06-02 12:32:51 -0400846 lockt->lt_owner.len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 READ_BUF(lockt->lt_owner.len);
848 READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
849
850 DECODE_TAIL;
851}
852
Al Virob37ad282006-10-19 23:28:59 -0700853static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
855{
856 DECODE_HEAD;
857
Benny Halevye31a1b62008-08-12 20:46:18 +0300858 READ_BUF(8);
J. Bruce Fields06553992014-06-02 12:32:51 -0400859 locku->lu_type = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
861 goto xdr_error;
J. Bruce Fields06553992014-06-02 12:32:51 -0400862 locku->lu_seqid = be32_to_cpup(p++);
Benny Halevye31a1b62008-08-12 20:46:18 +0300863 status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
864 if (status)
865 return status;
866 READ_BUF(16);
J. Bruce Fields542d1ab2014-06-02 12:45:31 -0400867 p = xdr_decode_hyper(p, &locku->lu_offset);
868 p = xdr_decode_hyper(p, &locku->lu_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 DECODE_TAIL;
871}
872
Al Virob37ad282006-10-19 23:28:59 -0700873static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
875{
876 DECODE_HEAD;
877
878 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400879 lookup->lo_len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 READ_BUF(lookup->lo_len);
881 SAVEMEM(lookup->lo_name, lookup->lo_len);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500882 if ((status = check_filename(lookup->lo_name, lookup->lo_len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return status;
884
885 DECODE_TAIL;
886}
887
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200888static __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 -0400889{
890 __be32 *p;
891 u32 w;
892
893 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400894 w = be32_to_cpup(p++);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200895 *share_access = w & NFS4_SHARE_ACCESS_MASK;
896 *deleg_want = w & NFS4_SHARE_WANT_MASK;
897 if (deleg_when)
898 *deleg_when = w & NFS4_SHARE_WHEN_MASK;
899
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400900 switch (w & NFS4_SHARE_ACCESS_MASK) {
901 case NFS4_SHARE_ACCESS_READ:
902 case NFS4_SHARE_ACCESS_WRITE:
903 case NFS4_SHARE_ACCESS_BOTH:
904 break;
905 default:
906 return nfserr_bad_xdr;
907 }
Benny Halevyfc0d14f2011-10-27 20:43:01 +0200908 w &= ~NFS4_SHARE_ACCESS_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400909 if (!w)
910 return nfs_ok;
911 if (!argp->minorversion)
912 return nfserr_bad_xdr;
913 switch (w & NFS4_SHARE_WANT_MASK) {
914 case NFS4_SHARE_WANT_NO_PREFERENCE:
915 case NFS4_SHARE_WANT_READ_DELEG:
916 case NFS4_SHARE_WANT_WRITE_DELEG:
917 case NFS4_SHARE_WANT_ANY_DELEG:
918 case NFS4_SHARE_WANT_NO_DELEG:
919 case NFS4_SHARE_WANT_CANCEL:
920 break;
921 default:
922 return nfserr_bad_xdr;
923 }
Benny Halevy92bac8c2011-10-19 19:13:29 -0700924 w &= ~NFS4_SHARE_WANT_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400925 if (!w)
926 return nfs_ok;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200927
928 if (!deleg_when) /* open_downgrade */
929 return nfserr_inval;
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400930 switch (w) {
931 case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
932 case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
Benny Halevyc668fc62011-10-19 19:13:13 -0700933 case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
934 NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400935 return nfs_ok;
936 }
937xdr_error:
938 return nfserr_bad_xdr;
939}
940
941static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
942{
943 __be32 *p;
944
945 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400946 *x = be32_to_cpup(p++);
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400947 /* Note: unlinke access bits, deny bits may be zero. */
Dan Carpenter01cd4af2011-10-17 10:41:17 +0300948 if (*x & ~NFS4_SHARE_DENY_BOTH)
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400949 return nfserr_bad_xdr;
950 return nfs_ok;
951xdr_error:
952 return nfserr_bad_xdr;
953}
954
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400955static __be32 nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
956{
Chuck Leverc1346a122020-11-03 11:54:23 -0500957 DECODE_HEAD;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400958
959 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400960 o->len = be32_to_cpup(p++);
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400961
962 if (o->len == 0 || o->len > NFS4_OPAQUE_LIMIT)
963 return nfserr_bad_xdr;
964
965 READ_BUF(o->len);
966 SAVEMEM(o->data, o->len);
Chuck Leverc1346a122020-11-03 11:54:23 -0500967
968 DECODE_TAIL;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400969}
970
Al Virob37ad282006-10-19 23:28:59 -0700971static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
973{
974 DECODE_HEAD;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200975 u32 dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 memset(open->op_bmval, 0, sizeof(open->op_bmval));
978 open->op_iattr.ia_valid = 0;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -0400979 open->op_openowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
J. Bruce Fields9d313b12013-02-28 12:51:49 -0800981 open->op_xdr_error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 /* seqid, share_access, share_deny, clientid, ownerlen */
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400983 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400984 open->op_seqid = be32_to_cpup(p++);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +0200985 /* decode, yet ignore deleg_when until supported */
986 status = nfsd4_decode_share_access(argp, &open->op_share_access,
987 &open->op_deleg_want, &dummy);
J. Bruce Fields04f9e662011-10-10 14:37:13 -0400988 if (status)
989 goto xdr_error;
990 status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
991 if (status)
992 goto xdr_error;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400993 READ_BUF(sizeof(clientid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 COPYMEM(&open->op_clientid, sizeof(clientid_t));
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -0400995 status = nfsd4_decode_opaque(argp, &open->op_owner);
996 if (status)
997 goto xdr_error;
998 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400999 open->op_create = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 switch (open->op_create) {
1001 case NFS4_OPEN_NOCREATE:
1002 break;
1003 case NFS4_OPEN_CREATE:
1004 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001005 open->op_createmode = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 switch (open->op_createmode) {
1007 case NFS4_CREATE_UNCHECKED:
1008 case NFS4_CREATE_GUARDED:
Chuck Leverd1c263a2020-11-03 12:56:05 -05001009 status = nfsd4_decode_fattr4(argp, open->op_bmval,
1010 ARRAY_SIZE(open->op_bmval),
1011 &open->op_iattr, &open->op_acl,
1012 &open->op_label, &open->op_umask);
Benny Halevyc0d6fc82009-04-03 08:29:05 +03001013 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 goto out;
1015 break;
1016 case NFS4_CREATE_EXCLUSIVE:
Chuck Leverab4684d2012-03-02 17:13:50 -05001017 READ_BUF(NFS4_VERIFIER_SIZE);
1018 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 break;
Benny Halevy79fb54a2009-04-03 08:29:17 +03001020 case NFS4_CREATE_EXCLUSIVE4_1:
1021 if (argp->minorversion < 1)
1022 goto xdr_error;
Chuck Leverab4684d2012-03-02 17:13:50 -05001023 READ_BUF(NFS4_VERIFIER_SIZE);
1024 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
Chuck Leverd1c263a2020-11-03 12:56:05 -05001025 status = nfsd4_decode_fattr4(argp, open->op_bmval,
1026 ARRAY_SIZE(open->op_bmval),
1027 &open->op_iattr, &open->op_acl,
1028 &open->op_label, &open->op_umask);
Benny Halevy79fb54a2009-04-03 08:29:17 +03001029 if (status)
1030 goto out;
1031 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 default:
1033 goto xdr_error;
1034 }
1035 break;
1036 default:
1037 goto xdr_error;
1038 }
1039
1040 /* open_claim */
1041 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001042 open->op_claim_type = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 switch (open->op_claim_type) {
1044 case NFS4_OPEN_CLAIM_NULL:
1045 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1046 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001047 open->op_fname.len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 READ_BUF(open->op_fname.len);
1049 SAVEMEM(open->op_fname.data, open->op_fname.len);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001050 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return status;
1052 break;
1053 case NFS4_OPEN_CLAIM_PREVIOUS:
1054 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001055 open->op_delegate_type = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 break;
1057 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
Benny Halevye31a1b62008-08-12 20:46:18 +03001058 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
1059 if (status)
1060 return status;
1061 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001062 open->op_fname.len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 READ_BUF(open->op_fname.len);
1064 SAVEMEM(open->op_fname.data, open->op_fname.len);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001065 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return status;
1067 break;
J. Bruce Fields8b289b22011-10-19 11:52:12 -04001068 case NFS4_OPEN_CLAIM_FH:
1069 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1070 if (argp->minorversion < 1)
1071 goto xdr_error;
1072 /* void */
1073 break;
1074 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1075 if (argp->minorversion < 1)
1076 goto xdr_error;
1077 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
1078 if (status)
1079 return status;
1080 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 default:
1082 goto xdr_error;
1083 }
1084
1085 DECODE_TAIL;
1086}
1087
Al Virob37ad282006-10-19 23:28:59 -07001088static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
1090{
1091 DECODE_HEAD;
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001092
1093 if (argp->minorversion >= 1)
1094 return nfserr_notsupp;
1095
Benny Halevye31a1b62008-08-12 20:46:18 +03001096 status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
1097 if (status)
1098 return status;
1099 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001100 open_conf->oc_seqid = be32_to_cpup(p++);
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 DECODE_TAIL;
1103}
1104
Al Virob37ad282006-10-19 23:28:59 -07001105static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
1107{
1108 DECODE_HEAD;
1109
Benny Halevye31a1b62008-08-12 20:46:18 +03001110 status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
1111 if (status)
1112 return status;
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001113 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001114 open_down->od_seqid = be32_to_cpup(p++);
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02001115 status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
1116 &open_down->od_deleg_want, NULL);
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001117 if (status)
1118 return status;
1119 status = nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
1120 if (status)
1121 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 DECODE_TAIL;
1123}
1124
Al Virob37ad282006-10-19 23:28:59 -07001125static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
1127{
1128 DECODE_HEAD;
1129
1130 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001131 putfh->pf_fhlen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 if (putfh->pf_fhlen > NFS4_FHSIZE)
1133 goto xdr_error;
1134 READ_BUF(putfh->pf_fhlen);
1135 SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
1136
1137 DECODE_TAIL;
1138}
1139
Al Virob37ad282006-10-19 23:28:59 -07001140static __be32
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001141nfsd4_decode_putpubfh(struct nfsd4_compoundargs *argp, void *p)
1142{
1143 if (argp->minorversion == 0)
1144 return nfs_ok;
1145 return nfserr_notsupp;
1146}
1147
1148static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
1150{
1151 DECODE_HEAD;
1152
Benny Halevye31a1b62008-08-12 20:46:18 +03001153 status = nfsd4_decode_stateid(argp, &read->rd_stateid);
1154 if (status)
1155 return status;
1156 READ_BUF(12);
J. Bruce Fields542d1ab2014-06-02 12:45:31 -04001157 p = xdr_decode_hyper(p, &read->rd_offset);
J. Bruce Fields06553992014-06-02 12:32:51 -04001158 read->rd_length = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 DECODE_TAIL;
1161}
1162
Al Virob37ad282006-10-19 23:28:59 -07001163static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
1165{
1166 DECODE_HEAD;
1167
1168 READ_BUF(24);
J. Bruce Fields542d1ab2014-06-02 12:45:31 -04001169 p = xdr_decode_hyper(p, &readdir->rd_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
J. Bruce Fields06553992014-06-02 12:32:51 -04001171 readdir->rd_dircount = be32_to_cpup(p++);
1172 readdir->rd_maxcount = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
1174 goto out;
1175
1176 DECODE_TAIL;
1177}
1178
Al Virob37ad282006-10-19 23:28:59 -07001179static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
1181{
1182 DECODE_HEAD;
1183
1184 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001185 remove->rm_namelen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 READ_BUF(remove->rm_namelen);
1187 SAVEMEM(remove->rm_name, remove->rm_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001188 if ((status = check_filename(remove->rm_name, remove->rm_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return status;
1190
1191 DECODE_TAIL;
1192}
1193
Al Virob37ad282006-10-19 23:28:59 -07001194static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
1196{
1197 DECODE_HEAD;
1198
1199 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001200 rename->rn_snamelen = be32_to_cpup(p++);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05001201 READ_BUF(rename->rn_snamelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 SAVEMEM(rename->rn_sname, rename->rn_snamelen);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05001203 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001204 rename->rn_tnamelen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 READ_BUF(rename->rn_tnamelen);
1206 SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001207 if ((status = check_filename(rename->rn_sname, rename->rn_snamelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return status;
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001209 if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return status;
1211
1212 DECODE_TAIL;
1213}
1214
Al Virob37ad282006-10-19 23:28:59 -07001215static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
1217{
1218 DECODE_HEAD;
1219
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001220 if (argp->minorversion >= 1)
1221 return nfserr_notsupp;
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 READ_BUF(sizeof(clientid_t));
1224 COPYMEM(clientid, sizeof(clientid_t));
1225
1226 DECODE_TAIL;
1227}
1228
Al Virob37ad282006-10-19 23:28:59 -07001229static __be32
Andy Adamsondcb488a32007-07-17 04:04:51 -07001230nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
1231 struct nfsd4_secinfo *secinfo)
1232{
1233 DECODE_HEAD;
1234
1235 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001236 secinfo->si_namelen = be32_to_cpup(p++);
Andy Adamsondcb488a32007-07-17 04:04:51 -07001237 READ_BUF(secinfo->si_namelen);
1238 SAVEMEM(secinfo->si_name, secinfo->si_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001239 status = check_filename(secinfo->si_name, secinfo->si_namelen);
Andy Adamsondcb488a32007-07-17 04:04:51 -07001240 if (status)
1241 return status;
1242 DECODE_TAIL;
1243}
1244
1245static __be32
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05001246nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
1247 struct nfsd4_secinfo_no_name *sin)
1248{
1249 DECODE_HEAD;
1250
1251 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001252 sin->sin_style = be32_to_cpup(p++);
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05001253 DECODE_TAIL;
1254}
1255
1256static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
1258{
Benny Halevye31a1b62008-08-12 20:46:18 +03001259 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Benny Halevye31a1b62008-08-12 20:46:18 +03001261 status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
1262 if (status)
1263 return status;
Chuck Leverd1c263a2020-11-03 12:56:05 -05001264 return nfsd4_decode_fattr4(argp, setattr->sa_bmval,
1265 ARRAY_SIZE(setattr->sa_bmval),
1266 &setattr->sa_iattr, &setattr->sa_acl,
1267 &setattr->sa_label, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
Al Virob37ad282006-10-19 23:28:59 -07001270static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
1272{
1273 DECODE_HEAD;
1274
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001275 if (argp->minorversion >= 1)
1276 return nfserr_notsupp;
1277
Chuck Leverab4684d2012-03-02 17:13:50 -05001278 READ_BUF(NFS4_VERIFIER_SIZE);
1279 COPYMEM(setclientid->se_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001281 status = nfsd4_decode_opaque(argp, &setclientid->se_name);
1282 if (status)
1283 return nfserr_bad_xdr;
1284 READ_BUF(8);
J. Bruce Fields06553992014-06-02 12:32:51 -04001285 setclientid->se_callback_prog = be32_to_cpup(p++);
1286 setclientid->se_callback_netid_len = be32_to_cpup(p++);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05001287 READ_BUF(setclientid->se_callback_netid_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05001289 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001290 setclientid->se_callback_addr_len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05001292 READ_BUF(setclientid->se_callback_addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05001294 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001295 setclientid->se_callback_ident = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 DECODE_TAIL;
1298}
1299
Al Virob37ad282006-10-19 23:28:59 -07001300static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
1302{
1303 DECODE_HEAD;
1304
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001305 if (argp->minorversion >= 1)
1306 return nfserr_notsupp;
1307
Chuck Leverab4684d2012-03-02 17:13:50 -05001308 READ_BUF(8 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 COPYMEM(&scd_c->sc_clientid, 8);
Chuck Leverab4684d2012-03-02 17:13:50 -05001310 COPYMEM(&scd_c->sc_confirm, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 DECODE_TAIL;
1313}
1314
1315/* Also used for NVERIFY */
Al Virob37ad282006-10-19 23:28:59 -07001316static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1318{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 DECODE_HEAD;
1320
1321 if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
1322 goto out;
1323
1324 /* For convenience's sake, we compare raw xdr'd attributes in
J. Bruce Fieldse5f95702012-11-30 17:24:18 -05001325 * nfsd4_proc_verify */
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001328 verify->ve_attrlen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 READ_BUF(verify->ve_attrlen);
1330 SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
1331
1332 DECODE_TAIL;
1333}
1334
Al Virob37ad282006-10-19 23:28:59 -07001335static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1337{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 DECODE_HEAD;
1339
Benny Halevye31a1b62008-08-12 20:46:18 +03001340 status = nfsd4_decode_stateid(argp, &write->wr_stateid);
1341 if (status)
1342 return status;
1343 READ_BUF(16);
J. Bruce Fields542d1ab2014-06-02 12:45:31 -04001344 p = xdr_decode_hyper(p, &write->wr_offset);
J. Bruce Fields06553992014-06-02 12:32:51 -04001345 write->wr_stable_how = be32_to_cpup(p++);
Kinglong Mee54bbb7d2016-12-31 20:59:53 +08001346 if (write->wr_stable_how > NFS_FILE_SYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 goto xdr_error;
J. Bruce Fields06553992014-06-02 12:32:51 -04001348 write->wr_buflen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Chuck Leverc1346a122020-11-03 11:54:23 -05001350 if (!xdr_stream_subsegment(argp->xdr, &write->wr_payload, write->wr_buflen))
1351 goto xdr_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 DECODE_TAIL;
1354}
1355
Al Virob37ad282006-10-19 23:28:59 -07001356static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1358{
1359 DECODE_HEAD;
1360
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001361 if (argp->minorversion >= 1)
1362 return nfserr_notsupp;
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 READ_BUF(12);
1365 COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
J. Bruce Fields06553992014-06-02 12:32:51 -04001366 rlockowner->rl_owner.len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 READ_BUF(rlockowner->rl_owner.len);
1368 READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
1369
Andy Adamson60adfc52009-04-03 08:28:50 +03001370 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1371 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 DECODE_TAIL;
1373}
1374
Al Virob37ad282006-10-19 23:28:59 -07001375static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001376nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
Andy Adamson0733d212009-04-03 08:28:01 +03001377 struct nfsd4_exchange_id *exid)
Andy Adamson2db134e2009-04-03 08:27:55 +03001378{
Mi Jinlong5afa0402010-11-09 09:39:23 +08001379 int dummy, tmp;
Andy Adamson0733d212009-04-03 08:28:01 +03001380 DECODE_HEAD;
1381
1382 READ_BUF(NFS4_VERIFIER_SIZE);
1383 COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
1384
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001385 status = nfsd4_decode_opaque(argp, &exid->clname);
1386 if (status)
1387 return nfserr_bad_xdr;
Andy Adamson0733d212009-04-03 08:28:01 +03001388
1389 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001390 exid->flags = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001391
1392 /* Ignore state_protect4_a */
1393 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001394 exid->spa_how = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001395 switch (exid->spa_how) {
1396 case SP4_NONE:
1397 break;
1398 case SP4_MACH_CRED:
1399 /* spo_must_enforce */
Andrew Elbleed941642016-06-15 12:52:09 -04001400 status = nfsd4_decode_bitmap(argp,
1401 exid->spo_must_enforce);
1402 if (status)
1403 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001404 /* spo_must_allow */
Andrew Elbleed941642016-06-15 12:52:09 -04001405 status = nfsd4_decode_bitmap(argp, exid->spo_must_allow);
1406 if (status)
1407 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001408 break;
1409 case SP4_SSV:
1410 /* ssp_ops */
1411 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001412 dummy = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001413 READ_BUF(dummy * 4);
1414 p += dummy;
1415
1416 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001417 dummy = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001418 READ_BUF(dummy * 4);
1419 p += dummy;
1420
1421 /* ssp_hash_algs<> */
1422 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001423 tmp = be32_to_cpup(p++);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001424 while (tmp--) {
1425 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001426 dummy = be32_to_cpup(p++);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001427 READ_BUF(dummy);
1428 p += XDR_QUADLEN(dummy);
1429 }
Andy Adamson0733d212009-04-03 08:28:01 +03001430
1431 /* ssp_encr_algs<> */
1432 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001433 tmp = be32_to_cpup(p++);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001434 while (tmp--) {
1435 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001436 dummy = be32_to_cpup(p++);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001437 READ_BUF(dummy);
1438 p += XDR_QUADLEN(dummy);
1439 }
Andy Adamson0733d212009-04-03 08:28:01 +03001440
nixiaoming5ed96bc2018-07-23 09:57:11 +08001441 /* ignore ssp_window and ssp_num_gss_handles: */
Andy Adamson0733d212009-04-03 08:28:01 +03001442 READ_BUF(8);
Andy Adamson0733d212009-04-03 08:28:01 +03001443 break;
1444 default:
1445 goto xdr_error;
1446 }
1447
Andy Adamson0733d212009-04-03 08:28:01 +03001448 READ_BUF(4); /* nfs_impl_id4 array length */
J. Bruce Fields06553992014-06-02 12:32:51 -04001449 dummy = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001450
1451 if (dummy > 1)
1452 goto xdr_error;
1453
1454 if (dummy == 1) {
J. Bruce Fields79123442019-06-05 12:42:05 -04001455 status = nfsd4_decode_opaque(argp, &exid->nii_domain);
1456 if (status)
1457 goto xdr_error;
Andy Adamson0733d212009-04-03 08:28:01 +03001458
1459 /* nii_name */
J. Bruce Fields79123442019-06-05 12:42:05 -04001460 status = nfsd4_decode_opaque(argp, &exid->nii_name);
1461 if (status)
1462 goto xdr_error;
Andy Adamson0733d212009-04-03 08:28:01 +03001463
1464 /* nii_date */
J. Bruce Fields79123442019-06-05 12:42:05 -04001465 status = nfsd4_decode_time(argp, &exid->nii_time);
1466 if (status)
1467 goto xdr_error;
Andy Adamson0733d212009-04-03 08:28:01 +03001468 }
1469 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001470}
1471
1472static __be32
1473nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1474 struct nfsd4_create_session *sess)
1475{
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001476 DECODE_HEAD;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001477
1478 READ_BUF(16);
1479 COPYMEM(&sess->clientid, 8);
J. Bruce Fields06553992014-06-02 12:32:51 -04001480 sess->seqid = be32_to_cpup(p++);
1481 sess->flags = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001482
1483 /* Fore channel attrs */
1484 READ_BUF(28);
Trond Myklebustb96811c2019-08-18 14:18:58 -04001485 p++; /* headerpadsz is always 0 */
J. Bruce Fields06553992014-06-02 12:32:51 -04001486 sess->fore_channel.maxreq_sz = be32_to_cpup(p++);
1487 sess->fore_channel.maxresp_sz = be32_to_cpup(p++);
1488 sess->fore_channel.maxresp_cached = be32_to_cpup(p++);
1489 sess->fore_channel.maxops = be32_to_cpup(p++);
1490 sess->fore_channel.maxreqs = be32_to_cpup(p++);
1491 sess->fore_channel.nr_rdma_attrs = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001492 if (sess->fore_channel.nr_rdma_attrs == 1) {
1493 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001494 sess->fore_channel.rdma_attrs = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001495 } else if (sess->fore_channel.nr_rdma_attrs > 1) {
1496 dprintk("Too many fore channel attr bitmaps!\n");
1497 goto xdr_error;
1498 }
1499
1500 /* Back channel attrs */
1501 READ_BUF(28);
Trond Myklebustb96811c2019-08-18 14:18:58 -04001502 p++; /* headerpadsz is always 0 */
J. Bruce Fields06553992014-06-02 12:32:51 -04001503 sess->back_channel.maxreq_sz = be32_to_cpup(p++);
1504 sess->back_channel.maxresp_sz = be32_to_cpup(p++);
1505 sess->back_channel.maxresp_cached = be32_to_cpup(p++);
1506 sess->back_channel.maxops = be32_to_cpup(p++);
1507 sess->back_channel.maxreqs = be32_to_cpup(p++);
1508 sess->back_channel.nr_rdma_attrs = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001509 if (sess->back_channel.nr_rdma_attrs == 1) {
1510 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001511 sess->back_channel.rdma_attrs = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001512 } else if (sess->back_channel.nr_rdma_attrs > 1) {
1513 dprintk("Too many back channel attr bitmaps!\n");
1514 goto xdr_error;
1515 }
1516
J. Bruce Fieldsacb28872012-03-27 14:50:26 -04001517 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001518 sess->callback_prog = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -04001519 nfsd4_decode_cb_sec(argp, &sess->cb_sec);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001520 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001521}
1522
1523static __be32
1524nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1525 struct nfsd4_destroy_session *destroy_session)
1526{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001527 DECODE_HEAD;
1528 READ_BUF(NFS4_MAX_SESSIONID_LEN);
1529 COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1530
1531 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001532}
1533
1534static __be32
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001535nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1536 struct nfsd4_free_stateid *free_stateid)
1537{
1538 DECODE_HEAD;
1539
1540 READ_BUF(sizeof(stateid_t));
J. Bruce Fields06553992014-06-02 12:32:51 -04001541 free_stateid->fr_stateid.si_generation = be32_to_cpup(p++);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001542 COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
1543
1544 DECODE_TAIL;
1545}
1546
1547static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001548nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1549 struct nfsd4_sequence *seq)
1550{
Benny Halevyb85d4c02009-04-03 08:28:08 +03001551 DECODE_HEAD;
1552
1553 READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
1554 COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields06553992014-06-02 12:32:51 -04001555 seq->seqid = be32_to_cpup(p++);
1556 seq->slotid = be32_to_cpup(p++);
1557 seq->maxslots = be32_to_cpup(p++);
1558 seq->cachethis = be32_to_cpup(p++);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001559
1560 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001561}
1562
Bryan Schumaker17456802011-07-13 10:50:48 -04001563static __be32
1564nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1565{
Bryan Schumaker17456802011-07-13 10:50:48 -04001566 int i;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001567 __be32 *p, status;
1568 struct nfsd4_test_stateid_id *stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04001569
1570 READ_BUF(4);
1571 test_stateid->ts_num_ids = ntohl(*p++);
1572
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001573 INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
Bryan Schumaker17456802011-07-13 10:50:48 -04001574
1575 for (i = 0; i < test_stateid->ts_num_ids; i++) {
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -04001576 stateid = svcxdr_tmpalloc(argp, sizeof(*stateid));
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001577 if (!stateid) {
Al Viroafcf6792012-04-13 00:15:37 -04001578 status = nfserrno(-ENOMEM);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001579 goto out;
1580 }
1581
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001582 INIT_LIST_HEAD(&stateid->ts_id_list);
1583 list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
1584
1585 status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
Bryan Schumaker17456802011-07-13 10:50:48 -04001586 if (status)
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001587 goto out;
Bryan Schumaker17456802011-07-13 10:50:48 -04001588 }
1589
1590 status = 0;
1591out:
1592 return status;
1593xdr_error:
1594 dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
1595 status = nfserr_bad_xdr;
1596 goto out;
1597}
1598
Mi Jinlong345c2842011-10-20 17:51:39 +08001599static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
1600{
1601 DECODE_HEAD;
1602
1603 READ_BUF(8);
1604 COPYMEM(&dc->clientid, 8);
1605
1606 DECODE_TAIL;
1607}
1608
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001609static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
1610{
1611 DECODE_HEAD;
1612
1613 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001614 rc->rca_one_fs = be32_to_cpup(p++);
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001615
1616 DECODE_TAIL;
1617}
1618
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001619#ifdef CONFIG_NFSD_PNFS
1620static __be32
1621nfsd4_decode_getdeviceinfo(struct nfsd4_compoundargs *argp,
1622 struct nfsd4_getdeviceinfo *gdev)
1623{
1624 DECODE_HEAD;
1625 u32 num, i;
1626
1627 READ_BUF(sizeof(struct nfsd4_deviceid) + 3 * 4);
1628 COPYMEM(&gdev->gd_devid, sizeof(struct nfsd4_deviceid));
1629 gdev->gd_layout_type = be32_to_cpup(p++);
1630 gdev->gd_maxcount = be32_to_cpup(p++);
1631 num = be32_to_cpup(p++);
1632 if (num) {
Scott Mayhew3171822f2018-06-08 16:31:46 -04001633 if (num > 1000)
1634 goto xdr_error;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001635 READ_BUF(4 * num);
1636 gdev->gd_notify_types = be32_to_cpup(p++);
1637 for (i = 1; i < num; i++) {
1638 if (be32_to_cpup(p++)) {
1639 status = nfserr_inval;
1640 goto out;
1641 }
1642 }
1643 }
1644 DECODE_TAIL;
1645}
1646
1647static __be32
1648nfsd4_decode_layoutget(struct nfsd4_compoundargs *argp,
1649 struct nfsd4_layoutget *lgp)
1650{
1651 DECODE_HEAD;
1652
1653 READ_BUF(36);
1654 lgp->lg_signal = be32_to_cpup(p++);
1655 lgp->lg_layout_type = be32_to_cpup(p++);
1656 lgp->lg_seg.iomode = be32_to_cpup(p++);
1657 p = xdr_decode_hyper(p, &lgp->lg_seg.offset);
1658 p = xdr_decode_hyper(p, &lgp->lg_seg.length);
1659 p = xdr_decode_hyper(p, &lgp->lg_minlength);
Kinglong Meedb59c0e2015-03-19 19:04:41 +08001660
1661 status = nfsd4_decode_stateid(argp, &lgp->lg_sid);
1662 if (status)
1663 return status;
1664
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001665 READ_BUF(4);
1666 lgp->lg_maxcount = be32_to_cpup(p++);
1667
1668 DECODE_TAIL;
1669}
1670
1671static __be32
1672nfsd4_decode_layoutcommit(struct nfsd4_compoundargs *argp,
1673 struct nfsd4_layoutcommit *lcp)
1674{
1675 DECODE_HEAD;
1676 u32 timechange;
1677
1678 READ_BUF(20);
1679 p = xdr_decode_hyper(p, &lcp->lc_seg.offset);
1680 p = xdr_decode_hyper(p, &lcp->lc_seg.length);
1681 lcp->lc_reclaim = be32_to_cpup(p++);
Kinglong Meedb59c0e2015-03-19 19:04:41 +08001682
1683 status = nfsd4_decode_stateid(argp, &lcp->lc_sid);
1684 if (status)
1685 return status;
1686
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001687 READ_BUF(4);
1688 lcp->lc_newoffset = be32_to_cpup(p++);
1689 if (lcp->lc_newoffset) {
1690 READ_BUF(8);
1691 p = xdr_decode_hyper(p, &lcp->lc_last_wr);
1692 } else
1693 lcp->lc_last_wr = 0;
1694 READ_BUF(4);
1695 timechange = be32_to_cpup(p++);
1696 if (timechange) {
1697 status = nfsd4_decode_time(argp, &lcp->lc_mtime);
1698 if (status)
1699 return status;
1700 } else {
1701 lcp->lc_mtime.tv_nsec = UTIME_NOW;
1702 }
1703 READ_BUF(8);
1704 lcp->lc_layout_type = be32_to_cpup(p++);
1705
1706 /*
1707 * Save the layout update in XDR format and let the layout driver deal
1708 * with it later.
1709 */
1710 lcp->lc_up_len = be32_to_cpup(p++);
1711 if (lcp->lc_up_len > 0) {
1712 READ_BUF(lcp->lc_up_len);
1713 READMEM(lcp->lc_up_layout, lcp->lc_up_len);
1714 }
1715
1716 DECODE_TAIL;
1717}
1718
1719static __be32
1720nfsd4_decode_layoutreturn(struct nfsd4_compoundargs *argp,
1721 struct nfsd4_layoutreturn *lrp)
1722{
1723 DECODE_HEAD;
1724
1725 READ_BUF(16);
1726 lrp->lr_reclaim = be32_to_cpup(p++);
1727 lrp->lr_layout_type = be32_to_cpup(p++);
1728 lrp->lr_seg.iomode = be32_to_cpup(p++);
1729 lrp->lr_return_type = be32_to_cpup(p++);
1730 if (lrp->lr_return_type == RETURN_FILE) {
1731 READ_BUF(16);
1732 p = xdr_decode_hyper(p, &lrp->lr_seg.offset);
1733 p = xdr_decode_hyper(p, &lrp->lr_seg.length);
Kinglong Meedb59c0e2015-03-19 19:04:41 +08001734
1735 status = nfsd4_decode_stateid(argp, &lrp->lr_sid);
1736 if (status)
1737 return status;
1738
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001739 READ_BUF(4);
1740 lrp->lrf_body_len = be32_to_cpup(p++);
1741 if (lrp->lrf_body_len > 0) {
1742 READ_BUF(lrp->lrf_body_len);
1743 READMEM(lrp->lrf_body, lrp->lrf_body_len);
1744 }
1745 } else {
1746 lrp->lr_seg.offset = 0;
1747 lrp->lr_seg.length = NFS4_MAX_UINT64;
1748 }
1749
1750 DECODE_TAIL;
1751}
1752#endif /* CONFIG_NFSD_PNFS */
1753
Andy Adamson2db134e2009-04-03 08:27:55 +03001754static __be32
Anna Schumaker95d871f2014-11-07 14:44:26 -05001755nfsd4_decode_fallocate(struct nfsd4_compoundargs *argp,
1756 struct nfsd4_fallocate *fallocate)
1757{
1758 DECODE_HEAD;
1759
1760 status = nfsd4_decode_stateid(argp, &fallocate->falloc_stateid);
1761 if (status)
1762 return status;
1763
1764 READ_BUF(16);
1765 p = xdr_decode_hyper(p, &fallocate->falloc_offset);
1766 xdr_decode_hyper(p, &fallocate->falloc_length);
1767
1768 DECODE_TAIL;
1769}
1770
1771static __be32
Christoph Hellwigffa01602015-12-03 12:59:52 +01001772nfsd4_decode_clone(struct nfsd4_compoundargs *argp, struct nfsd4_clone *clone)
1773{
1774 DECODE_HEAD;
1775
1776 status = nfsd4_decode_stateid(argp, &clone->cl_src_stateid);
1777 if (status)
1778 return status;
1779 status = nfsd4_decode_stateid(argp, &clone->cl_dst_stateid);
1780 if (status)
1781 return status;
1782
1783 READ_BUF(8 + 8 + 8);
1784 p = xdr_decode_hyper(p, &clone->cl_src_pos);
1785 p = xdr_decode_hyper(p, &clone->cl_dst_pos);
1786 p = xdr_decode_hyper(p, &clone->cl_count);
1787 DECODE_TAIL;
1788}
1789
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001790static __be32 nfsd4_decode_nl4_server(struct nfsd4_compoundargs *argp,
1791 struct nl4_server *ns)
1792{
1793 DECODE_HEAD;
1794 struct nfs42_netaddr *naddr;
1795
1796 READ_BUF(4);
1797 ns->nl4_type = be32_to_cpup(p++);
1798
1799 /* currently support for 1 inter-server source server */
1800 switch (ns->nl4_type) {
1801 case NL4_NETADDR:
1802 naddr = &ns->u.nl4_addr;
1803
1804 READ_BUF(4);
1805 naddr->netid_len = be32_to_cpup(p++);
1806 if (naddr->netid_len > RPCBIND_MAXNETIDLEN)
1807 goto xdr_error;
1808
1809 READ_BUF(naddr->netid_len + 4); /* 4 for uaddr len */
1810 COPYMEM(naddr->netid, naddr->netid_len);
1811
1812 naddr->addr_len = be32_to_cpup(p++);
1813 if (naddr->addr_len > RPCBIND_MAXUADDRLEN)
1814 goto xdr_error;
1815
1816 READ_BUF(naddr->addr_len);
1817 COPYMEM(naddr->addr, naddr->addr_len);
1818 break;
1819 default:
1820 goto xdr_error;
1821 }
1822 DECODE_TAIL;
1823}
1824
Christoph Hellwigffa01602015-12-03 12:59:52 +01001825static __be32
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001826nfsd4_decode_copy(struct nfsd4_compoundargs *argp, struct nfsd4_copy *copy)
1827{
1828 DECODE_HEAD;
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001829 struct nl4_server *ns_dummy;
1830 int i, count;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001831
1832 status = nfsd4_decode_stateid(argp, &copy->cp_src_stateid);
1833 if (status)
1834 return status;
1835 status = nfsd4_decode_stateid(argp, &copy->cp_dst_stateid);
1836 if (status)
1837 return status;
1838
1839 READ_BUF(8 + 8 + 8 + 4 + 4 + 4);
1840 p = xdr_decode_hyper(p, &copy->cp_src_pos);
1841 p = xdr_decode_hyper(p, &copy->cp_dst_pos);
1842 p = xdr_decode_hyper(p, &copy->cp_count);
J. Bruce Fieldsedcc8452018-03-07 15:37:35 -05001843 p++; /* ca_consecutive: we always do consecutive copies */
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001844 copy->cp_synchronous = be32_to_cpup(p++);
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001845
1846 count = be32_to_cpup(p++);
1847
1848 copy->cp_intra = false;
1849 if (count == 0) { /* intra-server copy */
1850 copy->cp_intra = true;
1851 goto intra;
1852 }
1853
1854 /* decode all the supplied server addresses but use first */
1855 status = nfsd4_decode_nl4_server(argp, &copy->cp_src);
1856 if (status)
1857 return status;
1858
1859 ns_dummy = kmalloc(sizeof(struct nl4_server), GFP_KERNEL);
1860 if (ns_dummy == NULL)
1861 return nfserrno(-ENOMEM);
1862 for (i = 0; i < count - 1; i++) {
1863 status = nfsd4_decode_nl4_server(argp, ns_dummy);
1864 if (status) {
1865 kfree(ns_dummy);
1866 return status;
1867 }
1868 }
1869 kfree(ns_dummy);
1870intra:
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001871
1872 DECODE_TAIL;
1873}
1874
1875static __be32
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04001876nfsd4_decode_offload_status(struct nfsd4_compoundargs *argp,
1877 struct nfsd4_offload_status *os)
1878{
1879 return nfsd4_decode_stateid(argp, &os->stateid);
1880}
1881
1882static __be32
Olga Kornievskaia51911862019-08-08 11:14:59 -04001883nfsd4_decode_copy_notify(struct nfsd4_compoundargs *argp,
1884 struct nfsd4_copy_notify *cn)
1885{
Chuck Lever5aff7d02020-09-11 14:47:59 -04001886 __be32 status;
Olga Kornievskaia51911862019-08-08 11:14:59 -04001887
1888 status = nfsd4_decode_stateid(argp, &cn->cpn_src_stateid);
1889 if (status)
1890 return status;
1891 return nfsd4_decode_nl4_server(argp, &cn->cpn_dst);
1892}
1893
1894static __be32
Anna Schumaker24bab492014-09-26 13:58:27 -04001895nfsd4_decode_seek(struct nfsd4_compoundargs *argp, struct nfsd4_seek *seek)
1896{
1897 DECODE_HEAD;
1898
1899 status = nfsd4_decode_stateid(argp, &seek->seek_stateid);
1900 if (status)
1901 return status;
1902
1903 READ_BUF(8 + 4);
1904 p = xdr_decode_hyper(p, &seek->seek_offset);
1905 seek->seek_whence = be32_to_cpup(p);
1906
1907 DECODE_TAIL;
1908}
1909
Frank van der Linden23e50fe2020-06-23 22:39:26 +00001910/*
1911 * XDR data that is more than PAGE_SIZE in size is normally part of a
1912 * read or write. However, the size of extended attributes is limited
1913 * by the maximum request size, and then further limited by the underlying
1914 * filesystem limits. This can exceed PAGE_SIZE (currently, XATTR_SIZE_MAX
1915 * is 64k). Since there is no kvec- or page-based interface to xattrs,
1916 * and we're not dealing with contiguous pages, we need to do some copying.
1917 */
1918
1919/*
Chuck Leverc1346a122020-11-03 11:54:23 -05001920 * Decode data into buffer.
Frank van der Linden23e50fe2020-06-23 22:39:26 +00001921 */
1922static __be32
Chuck Leverc1346a122020-11-03 11:54:23 -05001923nfsd4_vbuf_from_vector(struct nfsd4_compoundargs *argp, struct xdr_buf *xdr,
1924 char **bufp, u32 buflen)
Frank van der Linden23e50fe2020-06-23 22:39:26 +00001925{
Chuck Leverc1346a122020-11-03 11:54:23 -05001926 struct page **pages = xdr->pages;
1927 struct kvec *head = xdr->head;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00001928 char *tmp, *dp;
1929 u32 len;
1930
1931 if (buflen <= head->iov_len) {
1932 /*
1933 * We're in luck, the head has enough space. Just return
1934 * the head, no need for copying.
1935 */
1936 *bufp = head->iov_base;
1937 return 0;
1938 }
1939
1940 tmp = svcxdr_tmpalloc(argp, buflen);
1941 if (tmp == NULL)
1942 return nfserr_jukebox;
1943
1944 dp = tmp;
1945 memcpy(dp, head->iov_base, head->iov_len);
1946 buflen -= head->iov_len;
1947 dp += head->iov_len;
1948
1949 while (buflen > 0) {
1950 len = min_t(u32, buflen, PAGE_SIZE);
1951 memcpy(dp, page_address(*pages), len);
1952
1953 buflen -= len;
1954 dp += len;
1955 pages++;
1956 }
1957
1958 *bufp = tmp;
1959 return 0;
1960}
1961
1962/*
1963 * Get a user extended attribute name from the XDR buffer.
1964 * It will not have the "user." prefix, so prepend it.
1965 * Lastly, check for nul characters in the name.
1966 */
1967static __be32
1968nfsd4_decode_xattr_name(struct nfsd4_compoundargs *argp, char **namep)
1969{
1970 DECODE_HEAD;
1971 char *name, *sp, *dp;
1972 u32 namelen, cnt;
1973
1974 READ_BUF(4);
1975 namelen = be32_to_cpup(p++);
1976
1977 if (namelen > (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN))
1978 return nfserr_nametoolong;
1979
1980 if (namelen == 0)
1981 goto xdr_error;
1982
1983 READ_BUF(namelen);
1984
1985 name = svcxdr_tmpalloc(argp, namelen + XATTR_USER_PREFIX_LEN + 1);
1986 if (!name)
1987 return nfserr_jukebox;
1988
1989 memcpy(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
1990
1991 /*
1992 * Copy the extended attribute name over while checking for 0
1993 * characters.
1994 */
1995 sp = (char *)p;
1996 dp = name + XATTR_USER_PREFIX_LEN;
1997 cnt = namelen;
1998
1999 while (cnt-- > 0) {
2000 if (*sp == '\0')
2001 goto xdr_error;
2002 *dp++ = *sp++;
2003 }
2004 *dp = '\0';
2005
2006 *namep = name;
2007
2008 DECODE_TAIL;
2009}
2010
2011/*
2012 * A GETXATTR op request comes without a length specifier. We just set the
2013 * maximum length for the reply based on XATTR_SIZE_MAX and the maximum
2014 * channel reply size. nfsd_getxattr will probe the length of the xattr,
2015 * check it against getxa_len, and allocate + return the value.
2016 */
2017static __be32
2018nfsd4_decode_getxattr(struct nfsd4_compoundargs *argp,
2019 struct nfsd4_getxattr *getxattr)
2020{
2021 __be32 status;
2022 u32 maxcount;
2023
2024 status = nfsd4_decode_xattr_name(argp, &getxattr->getxa_name);
2025 if (status)
2026 return status;
2027
2028 maxcount = svc_max_payload(argp->rqstp);
2029 maxcount = min_t(u32, XATTR_SIZE_MAX, maxcount);
2030
2031 getxattr->getxa_len = maxcount;
2032
2033 return status;
2034}
2035
2036static __be32
2037nfsd4_decode_setxattr(struct nfsd4_compoundargs *argp,
2038 struct nfsd4_setxattr *setxattr)
2039{
2040 DECODE_HEAD;
2041 u32 flags, maxcount, size;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002042
2043 READ_BUF(4);
2044 flags = be32_to_cpup(p++);
2045
2046 if (flags > SETXATTR4_REPLACE)
2047 return nfserr_inval;
2048 setxattr->setxa_flags = flags;
2049
2050 status = nfsd4_decode_xattr_name(argp, &setxattr->setxa_name);
2051 if (status)
2052 return status;
2053
2054 maxcount = svc_max_payload(argp->rqstp);
2055 maxcount = min_t(u32, XATTR_SIZE_MAX, maxcount);
2056
2057 READ_BUF(4);
2058 size = be32_to_cpup(p++);
2059 if (size > maxcount)
2060 return nfserr_xattr2big;
2061
2062 setxattr->setxa_len = size;
2063 if (size > 0) {
Chuck Leverc1346a122020-11-03 11:54:23 -05002064 struct xdr_buf payload;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002065
Chuck Leverc1346a122020-11-03 11:54:23 -05002066 if (!xdr_stream_subsegment(argp->xdr, &payload, size))
2067 goto xdr_error;
2068 status = nfsd4_vbuf_from_vector(argp, &payload,
2069 &setxattr->setxa_buf, size);
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002070 }
2071
2072 DECODE_TAIL;
2073}
2074
2075static __be32
2076nfsd4_decode_listxattrs(struct nfsd4_compoundargs *argp,
2077 struct nfsd4_listxattrs *listxattrs)
2078{
2079 DECODE_HEAD;
2080 u32 maxcount;
2081
2082 READ_BUF(12);
2083 p = xdr_decode_hyper(p, &listxattrs->lsxa_cookie);
2084
2085 /*
2086 * If the cookie is too large to have even one user.x attribute
2087 * plus trailing '\0' left in a maximum size buffer, it's invalid.
2088 */
2089 if (listxattrs->lsxa_cookie >=
2090 (XATTR_LIST_MAX / (XATTR_USER_PREFIX_LEN + 2)))
2091 return nfserr_badcookie;
2092
2093 maxcount = be32_to_cpup(p++);
2094 if (maxcount < 8)
2095 /* Always need at least 2 words (length and one character) */
2096 return nfserr_inval;
2097
2098 maxcount = min(maxcount, svc_max_payload(argp->rqstp));
2099 listxattrs->lsxa_maxcount = maxcount;
2100
2101 DECODE_TAIL;
2102}
2103
2104static __be32
2105nfsd4_decode_removexattr(struct nfsd4_compoundargs *argp,
2106 struct nfsd4_removexattr *removexattr)
2107{
2108 return nfsd4_decode_xattr_name(argp, &removexattr->rmxa_name);
2109}
2110
Anna Schumaker24bab492014-09-26 13:58:27 -04002111static __be32
Benny Halevy347e0ad2008-07-02 11:13:41 +03002112nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
2113{
2114 return nfs_ok;
2115}
2116
Benny Halevy3c375c62008-07-02 11:14:01 +03002117static __be32
2118nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
2119{
Benny Halevy1e685ec2009-03-04 23:06:06 +02002120 return nfserr_notsupp;
Benny Halevy3c375c62008-07-02 11:14:01 +03002121}
2122
Benny Halevy347e0ad2008-07-02 11:13:41 +03002123typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
2124
Chuck Leverc1df6092017-08-01 11:59:58 -04002125static const nfsd4_dec nfsd4_dec_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04002126 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
2127 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
2128 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
2129 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
2130 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
2131 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
2132 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
2133 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
2134 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
2135 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
2136 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
2137 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
2138 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
2139 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
2140 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
2141 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
2142 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
2143 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
2144 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
2145 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002146 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_putpubfh,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04002147 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
2148 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
2149 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
2150 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
2151 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
2152 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
2153 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
2154 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
2155 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
2156 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
2157 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
2158 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
2159 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
2160 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
2161 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
2162 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
Andy Adamson2db134e2009-04-03 08:27:55 +03002163
2164 /* new operations for NFSv4.1 */
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002165 [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002166 [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
Randy Dunlap9064caa2009-04-28 16:48:25 -07002167 [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
2168 [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
2169 [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04002170 [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07002171 [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002172#ifdef CONFIG_NFSD_PNFS
2173 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_getdeviceinfo,
2174 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
2175 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_layoutcommit,
2176 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_layoutget,
2177 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_layoutreturn,
2178#else
Randy Dunlap9064caa2009-04-28 16:48:25 -07002179 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
2180 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
2181 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
2182 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
2183 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002184#endif
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05002185 [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
Randy Dunlap9064caa2009-04-28 16:48:25 -07002186 [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
2187 [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
Bryan Schumaker17456802011-07-13 10:50:48 -04002188 [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07002189 [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
Mi Jinlong345c2842011-10-20 17:51:39 +08002190 [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002191 [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
Anna Schumaker87a15a82014-09-26 13:58:26 -04002192
2193 /* new operations for NFSv4.2 */
Anna Schumaker95d871f2014-11-07 14:44:26 -05002194 [OP_ALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
Anna Schumaker29ae7f92016-09-07 15:57:30 -04002195 [OP_COPY] = (nfsd4_dec)nfsd4_decode_copy,
Olga Kornievskaia51911862019-08-08 11:14:59 -04002196 [OP_COPY_NOTIFY] = (nfsd4_dec)nfsd4_decode_copy_notify,
Anna Schumakerb0cb9082014-11-07 14:44:27 -05002197 [OP_DEALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
Anna Schumaker87a15a82014-09-26 13:58:26 -04002198 [OP_IO_ADVISE] = (nfsd4_dec)nfsd4_decode_notsupp,
2199 [OP_LAYOUTERROR] = (nfsd4_dec)nfsd4_decode_notsupp,
2200 [OP_LAYOUTSTATS] = (nfsd4_dec)nfsd4_decode_notsupp,
Olga Kornievskaia885e2bf2018-07-20 18:19:19 -04002201 [OP_OFFLOAD_CANCEL] = (nfsd4_dec)nfsd4_decode_offload_status,
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04002202 [OP_OFFLOAD_STATUS] = (nfsd4_dec)nfsd4_decode_offload_status,
Anna Schumaker528b8492020-09-28 13:08:58 -04002203 [OP_READ_PLUS] = (nfsd4_dec)nfsd4_decode_read,
Anna Schumaker24bab492014-09-26 13:58:27 -04002204 [OP_SEEK] = (nfsd4_dec)nfsd4_decode_seek,
Anna Schumaker87a15a82014-09-26 13:58:26 -04002205 [OP_WRITE_SAME] = (nfsd4_dec)nfsd4_decode_notsupp,
Christoph Hellwigffa01602015-12-03 12:59:52 +01002206 [OP_CLONE] = (nfsd4_dec)nfsd4_decode_clone,
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002207 /* RFC 8276 extended atributes operations */
2208 [OP_GETXATTR] = (nfsd4_dec)nfsd4_decode_getxattr,
2209 [OP_SETXATTR] = (nfsd4_dec)nfsd4_decode_setxattr,
2210 [OP_LISTXATTRS] = (nfsd4_dec)nfsd4_decode_listxattrs,
2211 [OP_REMOVEXATTR] = (nfsd4_dec)nfsd4_decode_removexattr,
Andy Adamson2db134e2009-04-03 08:27:55 +03002212};
2213
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002214static inline bool
2215nfsd4_opnum_in_range(struct nfsd4_compoundargs *argp, struct nfsd4_op *op)
2216{
Anna Schumaker8217d142013-10-30 13:38:13 -04002217 if (op->opnum < FIRST_NFS4_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002218 return false;
Anna Schumaker8217d142013-10-30 13:38:13 -04002219 else if (argp->minorversion == 0 && op->opnum > LAST_NFS40_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002220 return false;
Anna Schumaker8217d142013-10-30 13:38:13 -04002221 else if (argp->minorversion == 1 && op->opnum > LAST_NFS41_OP)
2222 return false;
2223 else if (argp->minorversion == 2 && op->opnum > LAST_NFS42_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002224 return false;
2225 return true;
2226}
Benny Halevyf2feb962008-07-02 11:14:22 +03002227
Benny Halevy347e0ad2008-07-02 11:13:41 +03002228static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
2230{
2231 DECODE_HEAD;
2232 struct nfsd4_op *op;
J. Bruce Fields10910062011-01-24 12:11:02 -05002233 bool cachethis = false;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002234 int auth_slack= argp->rqstp->rq_auth_slack;
2235 int max_reply = auth_slack + 8; /* opcnt, status */
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05002236 int readcount = 0;
2237 int readbytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 int i;
2239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04002241 argp->taglen = be32_to_cpup(p++);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05002242 READ_BUF(argp->taglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 SAVEMEM(argp->tag, argp->taglen);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05002244 READ_BUF(8);
J. Bruce Fields06553992014-06-02 12:32:51 -04002245 argp->minorversion = be32_to_cpup(p++);
2246 argp->opcnt = be32_to_cpup(p++);
J. Bruce Fields4f0cefb2014-03-11 15:39:13 -04002247 max_reply += 4 + (XDR_QUADLEN(argp->taglen) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
2249 if (argp->taglen > NFSD4_MAX_TAGLEN)
2250 goto xdr_error;
J. Bruce Fields00781172017-11-15 12:30:27 -05002251 /*
2252 * NFS4ERR_RESOURCE is a more helpful error than GARBAGE_ARGS
2253 * here, so we return success at the xdr level so that
2254 * nfsd4_proc can handle this is an NFS-level error.
2255 */
2256 if (argp->opcnt > NFSD_MAX_OPS_PER_COMPOUND)
2257 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Tobias Klausere8c96f82006-03-24 03:15:34 -08002259 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
J. Bruce Fields5d6031c2014-07-17 16:20:39 -04002260 argp->ops = kzalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 if (!argp->ops) {
2262 argp->ops = argp->iops;
Chuck Lever817cb9d2007-09-11 18:01:20 -04002263 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 goto xdr_error;
2265 }
2266 }
2267
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002268 if (argp->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
Benny Halevy30cff1f2008-07-02 11:13:18 +03002269 argp->opcnt = 0;
2270
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 for (i = 0; i < argp->opcnt; i++) {
2272 op = &argp->ops[i];
2273 op->replay = NULL;
2274
J. Bruce Fields8a61b182012-11-16 22:28:38 -05002275 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04002276 op->opnum = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Chuck Lever08281342020-11-21 11:36:42 -05002278 if (nfsd4_opnum_in_range(argp, op)) {
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002279 op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
Chuck Lever08281342020-11-21 11:36:42 -05002280 if (op->status != nfs_ok)
2281 trace_nfsd_compound_decode_err(argp->rqstp,
2282 argp->opcnt, i,
2283 op->opnum,
2284 op->status);
2285 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 op->opnum = OP_ILLEGAL;
2287 op->status = nfserr_op_illegal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 }
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002289 op->opdesc = OPDESC(op);
J. Bruce Fields10910062011-01-24 12:11:02 -05002290 /*
2291 * We'll try to cache the result in the DRC if any one
2292 * op in the compound wants to be cached:
2293 */
2294 cachethis |= nfsd4_cache_this_op(op);
J. Bruce Fields6ff40de2013-11-05 15:07:16 -05002295
Anna Schumaker528b8492020-09-28 13:08:58 -04002296 if (op->opnum == OP_READ || op->opnum == OP_READ_PLUS) {
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05002297 readcount++;
2298 readbytes += nfsd4_max_reply(argp->rqstp, op);
2299 } else
2300 max_reply += nfsd4_max_reply(argp->rqstp, op);
J. Bruce Fieldsf7b43d02014-08-12 11:41:40 -04002301 /*
Kinglong Mee7323f0d2017-02-03 22:51:46 +08002302 * OP_LOCK and OP_LOCKT may return a conflicting lock.
2303 * (Special case because it will just skip encoding this
2304 * if it runs out of xdr buffer space, and it is the only
2305 * operation that behaves this way.)
J. Bruce Fieldsf7b43d02014-08-12 11:41:40 -04002306 */
Kinglong Mee7323f0d2017-02-03 22:51:46 +08002307 if (op->opnum == OP_LOCK || op->opnum == OP_LOCKT)
J. Bruce Fieldsf7b43d02014-08-12 11:41:40 -04002308 max_reply += NFS4_OPAQUE_LIMIT;
J. Bruce Fieldse372ba62014-05-19 12:27:11 -04002309
2310 if (op->status) {
2311 argp->opcnt = i+1;
2312 break;
2313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 }
J. Bruce Fields10910062011-01-24 12:11:02 -05002315 /* Sessions make the DRC unnecessary: */
2316 if (argp->minorversion)
2317 cachethis = false;
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05002318 svc_reserve(argp->rqstp, max_reply + readbytes);
J. Bruce Fields10910062011-01-24 12:11:02 -05002319 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002321 if (readcount > 1 || max_reply > PAGE_SIZE - auth_slack)
Jeff Layton779fb0f2014-11-19 07:51:18 -05002322 clear_bit(RQ_SPLICE_OK, &argp->rqstp->rq_flags);
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05002323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 DECODE_TAIL;
2325}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
NeilBrownb8800922017-01-30 17:17:00 +11002327static __be32 *encode_change(__be32 *p, struct kstat *stat, struct inode *inode,
2328 struct svc_export *exp)
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002329{
NeilBrownb8800922017-01-30 17:17:00 +11002330 if (exp->ex_flags & NFSEXP_V4ROOT) {
2331 *p++ = cpu_to_be32(convert_to_wallclock(exp->cd->flush_time));
2332 *p++ = 0;
2333 } else if (IS_I_VERSION(inode)) {
Amir Goldstein39ca1bf2018-01-03 17:14:35 +02002334 p = xdr_encode_hyper(p, nfsd4_change_attribute(stat, inode));
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002335 } else {
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002336 *p++ = cpu_to_be32(stat->ctime.tv_sec);
2337 *p++ = cpu_to_be32(stat->ctime.tv_nsec);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002338 }
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002339 return p;
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002340}
2341
J. Bruce Fields16945142018-04-25 14:34:11 -04002342/*
2343 * ctime (in NFSv4, time_metadata) is not writeable, and the client
2344 * doesn't really care what resolution could theoretically be stored by
2345 * the filesystem.
2346 *
2347 * The client cares how close together changes can be while still
2348 * guaranteeing ctime changes. For most filesystems (which have
2349 * timestamps with nanosecond fields) that is limited by the resolution
2350 * of the time returned from current_time() (which I'm assuming to be
2351 * 1/HZ).
2352 */
2353static __be32 *encode_time_delta(__be32 *p, struct inode *inode)
2354{
Arnd Bergmanne4598e32019-10-31 15:14:03 +01002355 struct timespec64 ts;
J. Bruce Fields16945142018-04-25 14:34:11 -04002356 u32 ns;
2357
2358 ns = max_t(u32, NSEC_PER_SEC/HZ, inode->i_sb->s_time_gran);
Arnd Bergmanne4598e32019-10-31 15:14:03 +01002359 ts = ns_to_timespec64(ns);
J. Bruce Fields16945142018-04-25 14:34:11 -04002360
2361 p = xdr_encode_hyper(p, ts.tv_sec);
2362 *p++ = cpu_to_be32(ts.tv_nsec);
2363
2364 return p;
2365}
2366
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002367static __be32 *encode_cinfo(__be32 *p, struct nfsd4_change_info *c)
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002368{
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002369 *p++ = cpu_to_be32(c->atomic);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002370 if (c->change_supported) {
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002371 p = xdr_encode_hyper(p, c->before_change);
2372 p = xdr_encode_hyper(p, c->after_change);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002373 } else {
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002374 *p++ = cpu_to_be32(c->before_ctime_sec);
2375 *p++ = cpu_to_be32(c->before_ctime_nsec);
2376 *p++ = cpu_to_be32(c->after_ctime_sec);
2377 *p++ = cpu_to_be32(c->after_ctime_nsec);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002378 }
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002379 return p;
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002380}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002382/* Encode as an array of strings the string given with components
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002383 * separated @sep, escaped with esc_enter and esc_exit.
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002384 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002385static __be32 nfsd4_encode_components_esc(struct xdr_stream *xdr, char sep,
2386 char *components, char esc_enter,
2387 char esc_exit)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002388{
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002389 __be32 *p;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002390 __be32 pathlen;
2391 int pathlen_offset;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002392 int strlen, count=0;
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002393 char *str, *end, *next;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002394
2395 dprintk("nfsd4_encode_components(%s)\n", components);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002396
2397 pathlen_offset = xdr->buf->len;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002398 p = xdr_reserve_space(xdr, 4);
2399 if (!p)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002400 return nfserr_resource;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002401 p++; /* We will fill this in with @count later */
2402
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002403 end = str = components;
2404 while (*end) {
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002405 bool found_esc = false;
2406
2407 /* try to parse as esc_start, ..., esc_end, sep */
2408 if (*str == esc_enter) {
2409 for (; *end && (*end != esc_exit); end++)
2410 /* find esc_exit or end of string */;
2411 next = end + 1;
2412 if (*end && (!*next || *next == sep)) {
2413 str++;
2414 found_esc = true;
2415 }
2416 }
2417
2418 if (!found_esc)
2419 for (; *end && (*end != sep); end++)
2420 /* find sep or end of string */;
2421
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002422 strlen = end - str;
2423 if (strlen) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002424 p = xdr_reserve_space(xdr, strlen + 4);
2425 if (!p)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002426 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04002427 p = xdr_encode_opaque(p, str, strlen);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002428 count++;
2429 }
2430 else
2431 end++;
Benjamin Coddington5a64e562014-12-07 16:05:47 -05002432 if (found_esc)
2433 end = next;
2434
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002435 str = end;
2436 }
Benjamin Coddingtonbf7491f2014-12-07 16:05:48 -05002437 pathlen = htonl(count);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002438 write_bytes_to_xdr_buf(xdr->buf, pathlen_offset, &pathlen, 4);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002439 return 0;
2440}
2441
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002442/* Encode as an array of strings the string given with components
2443 * separated @sep.
2444 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002445static __be32 nfsd4_encode_components(struct xdr_stream *xdr, char sep,
2446 char *components)
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002447{
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002448 return nfsd4_encode_components_esc(xdr, sep, components, 0, 0);
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002449}
2450
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002451/*
2452 * encode a location element of a fs_locations structure
2453 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002454static __be32 nfsd4_encode_fs_location4(struct xdr_stream *xdr,
2455 struct nfsd4_fs_location *location)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002456{
Al Virob37ad282006-10-19 23:28:59 -07002457 __be32 status;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002458
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002459 status = nfsd4_encode_components_esc(xdr, ':', location->hosts,
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002460 '[', ']');
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002461 if (status)
2462 return status;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002463 status = nfsd4_encode_components(xdr, '/', location->path);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002464 if (status)
2465 return status;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002466 return 0;
2467}
2468
2469/*
Trond Myklebusted748aa2011-09-12 19:37:06 -04002470 * Encode a path in RFC3530 'pathname4' format
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002471 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002472static __be32 nfsd4_encode_path(struct xdr_stream *xdr,
2473 const struct path *root,
2474 const struct path *path)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002475{
Al Viro301f0262013-09-01 16:06:39 -04002476 struct path cur = *path;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002477 __be32 *p;
Trond Myklebusted748aa2011-09-12 19:37:06 -04002478 struct dentry **components = NULL;
2479 unsigned int ncomponents = 0;
2480 __be32 err = nfserr_jukebox;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002481
Trond Myklebusted748aa2011-09-12 19:37:06 -04002482 dprintk("nfsd4_encode_components(");
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002483
Trond Myklebusted748aa2011-09-12 19:37:06 -04002484 path_get(&cur);
2485 /* First walk the path up to the nfsd root, and store the
2486 * dentries/path components in an array.
2487 */
2488 for (;;) {
Kinglong Meeb77a4b22015-03-15 23:13:32 +08002489 if (path_equal(&cur, root))
Trond Myklebusted748aa2011-09-12 19:37:06 -04002490 break;
2491 if (cur.dentry == cur.mnt->mnt_root) {
2492 if (follow_up(&cur))
2493 continue;
2494 goto out_free;
2495 }
2496 if ((ncomponents & 15) == 0) {
2497 struct dentry **new;
2498 new = krealloc(components,
2499 sizeof(*new) * (ncomponents + 16),
2500 GFP_KERNEL);
2501 if (!new)
2502 goto out_free;
2503 components = new;
2504 }
2505 components[ncomponents++] = cur.dentry;
2506 cur.dentry = dget_parent(cur.dentry);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002507 }
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002508 err = nfserr_resource;
2509 p = xdr_reserve_space(xdr, 4);
2510 if (!p)
Trond Myklebusted748aa2011-09-12 19:37:06 -04002511 goto out_free;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002512 *p++ = cpu_to_be32(ncomponents);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002513
2514 while (ncomponents) {
2515 struct dentry *dentry = components[ncomponents - 1];
Al Viro301f0262013-09-01 16:06:39 -04002516 unsigned int len;
Trond Myklebusted748aa2011-09-12 19:37:06 -04002517
Al Viro301f0262013-09-01 16:06:39 -04002518 spin_lock(&dentry->d_lock);
2519 len = dentry->d_name.len;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002520 p = xdr_reserve_space(xdr, len + 4);
2521 if (!p) {
Al Viro301f0262013-09-01 16:06:39 -04002522 spin_unlock(&dentry->d_lock);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002523 goto out_free;
Al Viro301f0262013-09-01 16:06:39 -04002524 }
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04002525 p = xdr_encode_opaque(p, dentry->d_name.name, len);
Al Viroa4555892014-10-21 20:11:25 -04002526 dprintk("/%pd", dentry);
Al Viro301f0262013-09-01 16:06:39 -04002527 spin_unlock(&dentry->d_lock);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002528 dput(dentry);
2529 ncomponents--;
2530 }
2531
Trond Myklebusted748aa2011-09-12 19:37:06 -04002532 err = 0;
2533out_free:
2534 dprintk(")\n");
2535 while (ncomponents)
2536 dput(components[--ncomponents]);
2537 kfree(components);
2538 path_put(&cur);
2539 return err;
2540}
2541
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002542static __be32 nfsd4_encode_fsloc_fsroot(struct xdr_stream *xdr,
2543 struct svc_rqst *rqstp, const struct path *path)
Trond Myklebusted748aa2011-09-12 19:37:06 -04002544{
2545 struct svc_export *exp_ps;
2546 __be32 res;
2547
2548 exp_ps = rqst_find_fsidzero_export(rqstp);
2549 if (IS_ERR(exp_ps))
2550 return nfserrno(PTR_ERR(exp_ps));
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002551 res = nfsd4_encode_path(xdr, &exp_ps->ex_path, path);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002552 exp_put(exp_ps);
2553 return res;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002554}
2555
2556/*
2557 * encode a fs_locations structure
2558 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002559static __be32 nfsd4_encode_fs_locations(struct xdr_stream *xdr,
2560 struct svc_rqst *rqstp, struct svc_export *exp)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002561{
Al Virob37ad282006-10-19 23:28:59 -07002562 __be32 status;
Al Virocc45f012006-10-19 23:28:44 -07002563 int i;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002564 __be32 *p;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002565 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002566
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002567 status = nfsd4_encode_fsloc_fsroot(xdr, rqstp, &exp->ex_path);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002568 if (status)
2569 return status;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002570 p = xdr_reserve_space(xdr, 4);
2571 if (!p)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002572 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002573 *p++ = cpu_to_be32(fslocs->locations_count);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002574 for (i=0; i<fslocs->locations_count; i++) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002575 status = nfsd4_encode_fs_location4(xdr, &fslocs->locations[i]);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002576 if (status)
2577 return status;
2578 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002579 return 0;
2580}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002582static u32 nfs4_file_type(umode_t mode)
2583{
2584 switch (mode & S_IFMT) {
2585 case S_IFIFO: return NF4FIFO;
2586 case S_IFCHR: return NF4CHR;
2587 case S_IFDIR: return NF4DIR;
2588 case S_IFBLK: return NF4BLK;
2589 case S_IFLNK: return NF4LNK;
2590 case S_IFREG: return NF4REG;
2591 case S_IFSOCK: return NF4SOCK;
2592 default: return NF4BAD;
Tom Rix25fef482020-11-01 07:32:34 -08002593 }
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002594}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Al Virob37ad282006-10-19 23:28:59 -07002596static inline __be32
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002597nfsd4_encode_aclname(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2598 struct nfs4_ace *ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599{
J. Bruce Fields35541162014-01-08 09:49:01 -05002600 if (ace->whotype != NFS4_ACL_WHO_NAMED)
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002601 return nfs4_acl_write_who(xdr, ace->whotype);
J. Bruce Fields35541162014-01-08 09:49:01 -05002602 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002603 return nfsd4_encode_group(xdr, rqstp, ace->who_gid);
J. Bruce Fields35541162014-01-08 09:49:01 -05002604 else
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002605 return nfsd4_encode_user(xdr, rqstp, ace->who_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606}
2607
Kinglong Mee6896f152015-07-30 21:52:44 +08002608static inline __be32
Jeff Layton8a4c3922016-07-10 15:55:58 -04002609nfsd4_encode_layout_types(struct xdr_stream *xdr, u32 layout_types)
Kinglong Mee6896f152015-07-30 21:52:44 +08002610{
Jeff Layton8a4c3922016-07-10 15:55:58 -04002611 __be32 *p;
2612 unsigned long i = hweight_long(layout_types);
Kinglong Mee6896f152015-07-30 21:52:44 +08002613
Jeff Layton8a4c3922016-07-10 15:55:58 -04002614 p = xdr_reserve_space(xdr, 4 + 4 * i);
2615 if (!p)
2616 return nfserr_resource;
2617
2618 *p++ = cpu_to_be32(i);
2619
2620 for (i = LAYOUT_NFSV4_1_FILES; i < LAYOUT_TYPE_MAX; ++i)
2621 if (layout_types & (1 << i))
2622 *p++ = cpu_to_be32(i);
Kinglong Mee6896f152015-07-30 21:52:44 +08002623
2624 return 0;
2625}
2626
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002627#define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
2628 FATTR4_WORD0_RDATTR_ERROR)
2629#define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
Kinglong Meec2227a32015-07-07 10:16:37 +08002630#define WORD2_ABSENT_FS_ATTRS 0
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002631
David Quigley18032ca2013-05-02 13:19:10 -04002632#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2633static inline __be32
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002634nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2635 void *context, int len)
David Quigley18032ca2013-05-02 13:19:10 -04002636{
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002637 __be32 *p;
David Quigley18032ca2013-05-02 13:19:10 -04002638
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002639 p = xdr_reserve_space(xdr, len + 4 + 4 + 4);
2640 if (!p)
David Quigley18032ca2013-05-02 13:19:10 -04002641 return nfserr_resource;
2642
2643 /*
2644 * For now we use a 0 here to indicate the null translation; in
2645 * the future we may place a call to translation code here.
2646 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002647 *p++ = cpu_to_be32(0); /* lfs */
2648 *p++ = cpu_to_be32(0); /* pi */
David Quigley18032ca2013-05-02 13:19:10 -04002649 p = xdr_encode_opaque(p, context, len);
David Quigley18032ca2013-05-02 13:19:10 -04002650 return 0;
2651}
2652#else
2653static inline __be32
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002654nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2655 void *context, int len)
David Quigley18032ca2013-05-02 13:19:10 -04002656{ return 0; }
2657#endif
2658
Kinglong Meec2227a32015-07-07 10:16:37 +08002659static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *bmval2, u32 *rdattr_err)
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002660{
2661 /* As per referral draft: */
2662 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
2663 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
2664 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
2665 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
2666 *rdattr_err = NFSERR_MOVED;
2667 else
2668 return nfserr_moved;
2669 }
2670 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
2671 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
Kinglong Meec2227a32015-07-07 10:16:37 +08002672 *bmval2 &= WORD2_ABSENT_FS_ATTRS;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002673 return 0;
2674}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002676
2677static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
2678{
2679 struct path path = exp->ex_path;
2680 int err;
2681
2682 path_get(&path);
2683 while (follow_up(&path)) {
2684 if (path.dentry != path.mnt->mnt_root)
2685 break;
2686 }
David Howellsa528d352017-01-31 16:46:22 +00002687 err = vfs_getattr(&path, stat, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002688 path_put(&path);
2689 return err;
2690}
2691
Kinglong Mee75976de2015-07-30 21:55:30 +08002692static __be32
2693nfsd4_encode_bitmap(struct xdr_stream *xdr, u32 bmval0, u32 bmval1, u32 bmval2)
2694{
2695 __be32 *p;
2696
2697 if (bmval2) {
2698 p = xdr_reserve_space(xdr, 16);
2699 if (!p)
2700 goto out_resource;
2701 *p++ = cpu_to_be32(3);
2702 *p++ = cpu_to_be32(bmval0);
2703 *p++ = cpu_to_be32(bmval1);
2704 *p++ = cpu_to_be32(bmval2);
2705 } else if (bmval1) {
2706 p = xdr_reserve_space(xdr, 12);
2707 if (!p)
2708 goto out_resource;
2709 *p++ = cpu_to_be32(2);
2710 *p++ = cpu_to_be32(bmval0);
2711 *p++ = cpu_to_be32(bmval1);
2712 } else {
2713 p = xdr_reserve_space(xdr, 8);
2714 if (!p)
2715 goto out_resource;
2716 *p++ = cpu_to_be32(1);
2717 *p++ = cpu_to_be32(bmval0);
2718 }
2719
2720 return 0;
2721out_resource:
2722 return nfserr_resource;
2723}
2724
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725/*
2726 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
2727 * ourselves.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 */
Jeff Laytonda2ebce2014-05-30 09:09:25 -04002729static __be32
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04002730nfsd4_encode_fattr(struct xdr_stream *xdr, struct svc_fh *fhp,
2731 struct svc_export *exp,
2732 struct dentry *dentry, u32 *bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002733 struct svc_rqst *rqstp, int ignore_crossmnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734{
2735 u32 bmval0 = bmval[0];
2736 u32 bmval1 = bmval[1];
Andy Adamson7e705702009-04-03 08:29:11 +03002737 u32 bmval2 = bmval[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 struct kstat stat;
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002739 struct svc_fh *tempfh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 struct kstatfs statfs;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002741 __be32 *p;
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05002742 int starting_len = xdr->buf->len;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002743 int attrlen_offset;
2744 __be32 attrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 u32 dummy;
2746 u64 dummy64;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002747 u32 rdattr_err = 0;
Al Virob37ad282006-10-19 23:28:59 -07002748 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07002749 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 struct nfs4_acl *acl = NULL;
Arnd Bergmann0ab88ca2019-03-22 15:07:11 +01002751#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
David Quigley18032ca2013-05-02 13:19:10 -04002752 void *context = NULL;
2753 int contextlen;
Arnd Bergmann0ab88ca2019-03-22 15:07:11 +01002754#endif
David Quigley18032ca2013-05-02 13:19:10 -04002755 bool contextsupport = false;
Andy Adamson7e705702009-04-03 08:29:11 +03002756 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2757 u32 minorversion = resp->cstate.minorversion;
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002758 struct path path = {
2759 .mnt = exp->ex_path.mnt,
2760 .dentry = dentry,
2761 };
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03002762 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
2764 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002765 BUG_ON(!nfsd_attrs_supported(minorversion, bmval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002767 if (exp->ex_fslocs.migrated) {
Kinglong Meec2227a32015-07-07 10:16:37 +08002768 status = fattr_handle_absent_fs(&bmval0, &bmval1, &bmval2, &rdattr_err);
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002769 if (status)
2770 goto out;
2771 }
2772
David Howellsa528d352017-01-31 16:46:22 +00002773 err = vfs_getattr(&path, &stat, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
Al Virob8dd7b92006-10-19 23:29:01 -07002774 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 goto out_nfserr;
Christoph Hellwig12337902014-05-28 10:46:13 +02002776 if ((bmval0 & (FATTR4_WORD0_FILES_AVAIL | FATTR4_WORD0_FILES_FREE |
2777 FATTR4_WORD0_FILES_TOTAL | FATTR4_WORD0_MAXNAME)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2779 FATTR4_WORD1_SPACE_TOTAL))) {
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002780 err = vfs_statfs(&path, &statfs);
Al Virob8dd7b92006-10-19 23:29:01 -07002781 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 goto out_nfserr;
2783 }
2784 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002785 tempfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
2786 status = nfserr_jukebox;
2787 if (!tempfh)
2788 goto out;
2789 fh_init(tempfh, NFS4_FHSIZE);
2790 status = fh_compose(tempfh, exp, dentry, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 if (status)
2792 goto out;
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002793 fhp = tempfh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 }
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02002795 if (bmval0 & FATTR4_WORD0_ACL) {
Al Virob8dd7b92006-10-19 23:29:01 -07002796 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02002797 if (err == -EOPNOTSUPP)
2798 bmval0 &= ~FATTR4_WORD0_ACL;
2799 else if (err == -EINVAL) {
2800 status = nfserr_attrnotsupp;
2801 goto out;
2802 } else if (err != 0)
2803 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
David Quigley18032ca2013-05-02 13:19:10 -04002806#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
Kinglong Meec2227a32015-07-07 10:16:37 +08002807 if ((bmval2 & FATTR4_WORD2_SECURITY_LABEL) ||
2808 bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
J. Bruce Fields32ddd942017-01-03 12:30:11 -05002809 if (exp->ex_flags & NFSEXP_SECURITY_LABEL)
2810 err = security_inode_getsecctx(d_inode(dentry),
David Quigley18032ca2013-05-02 13:19:10 -04002811 &context, &contextlen);
J. Bruce Fields32ddd942017-01-03 12:30:11 -05002812 else
2813 err = -EOPNOTSUPP;
David Quigley18032ca2013-05-02 13:19:10 -04002814 contextsupport = (err == 0);
2815 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2816 if (err == -EOPNOTSUPP)
2817 bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2818 else if (err)
2819 goto out_nfserr;
2820 }
2821 }
2822#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2823
Kinglong Mee75976de2015-07-30 21:55:30 +08002824 status = nfsd4_encode_bitmap(xdr, bmval0, bmval1, bmval2);
2825 if (status)
2826 goto out;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002827
2828 attrlen_offset = xdr->buf->len;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002829 p = xdr_reserve_space(xdr, 4);
2830 if (!p)
2831 goto out_resource;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002832 p++; /* to be backfilled later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
2834 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
J. Bruce Fieldsdcd208692017-01-11 20:34:50 -05002835 u32 supp[3];
2836
2837 memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
Andy Adamson7e705702009-04-03 08:29:11 +03002838
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02002839 if (!IS_POSIXACL(dentry->d_inode))
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002840 supp[0] &= ~FATTR4_WORD0_ACL;
David Quigley18032ca2013-05-02 13:19:10 -04002841 if (!contextsupport)
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002842 supp[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2843 if (!supp[2]) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002844 p = xdr_reserve_space(xdr, 12);
2845 if (!p)
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002846 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002847 *p++ = cpu_to_be32(2);
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002848 *p++ = cpu_to_be32(supp[0]);
2849 *p++ = cpu_to_be32(supp[1]);
Andy Adamson7e705702009-04-03 08:29:11 +03002850 } else {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002851 p = xdr_reserve_space(xdr, 16);
2852 if (!p)
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002853 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002854 *p++ = cpu_to_be32(3);
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002855 *p++ = cpu_to_be32(supp[0]);
2856 *p++ = cpu_to_be32(supp[1]);
2857 *p++ = cpu_to_be32(supp[2]);
Andy Adamson7e705702009-04-03 08:29:11 +03002858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 }
2860 if (bmval0 & FATTR4_WORD0_TYPE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002861 p = xdr_reserve_space(xdr, 4);
2862 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 goto out_resource;
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002864 dummy = nfs4_file_type(stat.mode);
J. Bruce Fields6b6d81372013-01-16 17:11:11 -05002865 if (dummy == NF4BAD) {
2866 status = nfserr_serverfault;
2867 goto out;
2868 }
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002869 *p++ = cpu_to_be32(dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 }
2871 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002872 p = xdr_reserve_space(xdr, 4);
2873 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 goto out_resource;
NeilBrown49640002005-06-23 22:02:58 -07002875 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002876 *p++ = cpu_to_be32(NFS4_FH_PERSISTENT);
NeilBrown49640002005-06-23 22:02:58 -07002877 else
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002878 *p++ = cpu_to_be32(NFS4_FH_PERSISTENT|
2879 NFS4_FH_VOL_RENAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 }
2881 if (bmval0 & FATTR4_WORD0_CHANGE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002882 p = xdr_reserve_space(xdr, 8);
2883 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 goto out_resource;
NeilBrownb8800922017-01-30 17:17:00 +11002885 p = encode_change(p, &stat, d_inode(dentry), exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 }
2887 if (bmval0 & FATTR4_WORD0_SIZE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002888 p = xdr_reserve_space(xdr, 8);
2889 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002891 p = xdr_encode_hyper(p, stat.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 }
2893 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002894 p = xdr_reserve_space(xdr, 4);
2895 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002897 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 }
2899 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002900 p = xdr_reserve_space(xdr, 4);
2901 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002903 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 }
2905 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002906 p = xdr_reserve_space(xdr, 4);
2907 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002909 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 }
2911 if (bmval0 & FATTR4_WORD0_FSID) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002912 p = xdr_reserve_space(xdr, 16);
2913 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002915 if (exp->ex_fslocs.migrated) {
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002916 p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MAJOR);
2917 p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MINOR);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002918 } else switch(fsid_source(fhp)) {
2919 case FSIDSOURCE_FSID:
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002920 p = xdr_encode_hyper(p, (u64)exp->ex_fsid);
2921 p = xdr_encode_hyper(p, (u64)0);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002922 break;
2923 case FSIDSOURCE_DEV:
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002924 *p++ = cpu_to_be32(0);
2925 *p++ = cpu_to_be32(MAJOR(stat.dev));
2926 *p++ = cpu_to_be32(0);
2927 *p++ = cpu_to_be32(MINOR(stat.dev));
NeilBrownaf6a4e22007-02-14 00:33:12 -08002928 break;
2929 case FSIDSOURCE_UUID:
Kinglong Mee94eb3682014-05-23 20:00:19 +08002930 p = xdr_encode_opaque_fixed(p, exp->ex_uuid,
2931 EX_UUID_LEN);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002932 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 }
2934 }
2935 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002936 p = xdr_reserve_space(xdr, 4);
2937 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002939 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 }
2941 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002942 p = xdr_reserve_space(xdr, 4);
2943 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002945 *p++ = cpu_to_be32(nn->nfsd4_lease);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 }
2947 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
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;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002951 *p++ = cpu_to_be32(rdattr_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 }
2953 if (bmval0 & FATTR4_WORD0_ACL) {
2954 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
2956 if (acl == NULL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002957 p = xdr_reserve_space(xdr, 4);
2958 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 goto out_resource;
2960
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002961 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 goto out_acl;
2963 }
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002964 p = xdr_reserve_space(xdr, 4);
2965 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002967 *p++ = cpu_to_be32(acl->naces);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08002969 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002970 p = xdr_reserve_space(xdr, 4*3);
2971 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002973 *p++ = cpu_to_be32(ace->type);
2974 *p++ = cpu_to_be32(ace->flag);
2975 *p++ = cpu_to_be32(ace->access_mask &
2976 NFS4_ACE_MASK_ALL);
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002977 status = nfsd4_encode_aclname(xdr, rqstp, ace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 if (status)
2979 goto out;
2980 }
2981 }
2982out_acl:
2983 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002984 p = xdr_reserve_space(xdr, 4);
2985 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 goto out_resource;
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02002987 *p++ = cpu_to_be32(IS_POSIXACL(dentry->d_inode) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
2989 }
2990 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002991 p = xdr_reserve_space(xdr, 4);
2992 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002994 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 }
2996 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002997 p = xdr_reserve_space(xdr, 4);
2998 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003000 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 }
3002 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003003 p = xdr_reserve_space(xdr, 4);
3004 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003006 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 }
3008 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003009 p = xdr_reserve_space(xdr, 4);
3010 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003012 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 }
3014 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003015 p = xdr_reserve_space(xdr, fhp->fh_handle.fh_size + 4);
3016 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 goto out_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003018 p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base,
3019 fhp->fh_handle.fh_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 }
3021 if (bmval0 & FATTR4_WORD0_FILEID) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003022 p = xdr_reserve_space(xdr, 8);
3023 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003025 p = xdr_encode_hyper(p, stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 }
3027 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003028 p = xdr_reserve_space(xdr, 8);
3029 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003031 p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 }
3033 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003034 p = xdr_reserve_space(xdr, 8);
3035 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003037 p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 }
3039 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003040 p = xdr_reserve_space(xdr, 8);
3041 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003043 p = xdr_encode_hyper(p, (u64) statfs.f_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07003045 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003046 status = nfsd4_encode_fs_locations(xdr, rqstp, exp);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07003047 if (status)
3048 goto out;
3049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003051 p = xdr_reserve_space(xdr, 4);
3052 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003054 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 }
3056 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003057 p = xdr_reserve_space(xdr, 8);
3058 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003060 p = xdr_encode_hyper(p, exp->ex_path.mnt->mnt_sb->s_maxbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 }
3062 if (bmval0 & FATTR4_WORD0_MAXLINK) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003063 p = xdr_reserve_space(xdr, 4);
3064 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003066 *p++ = cpu_to_be32(255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 }
3068 if (bmval0 & FATTR4_WORD0_MAXNAME) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003069 p = xdr_reserve_space(xdr, 4);
3070 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003072 *p++ = cpu_to_be32(statfs.f_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 }
3074 if (bmval0 & FATTR4_WORD0_MAXREAD) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003075 p = xdr_reserve_space(xdr, 8);
3076 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003078 p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 }
3080 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003081 p = xdr_reserve_space(xdr, 8);
3082 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003084 p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 }
3086 if (bmval1 & FATTR4_WORD1_MODE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003087 p = xdr_reserve_space(xdr, 4);
3088 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003090 *p++ = cpu_to_be32(stat.mode & S_IALLUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 }
3092 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003093 p = xdr_reserve_space(xdr, 4);
3094 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003096 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 }
3098 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003099 p = xdr_reserve_space(xdr, 4);
3100 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003102 *p++ = cpu_to_be32(stat.nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 }
3104 if (bmval1 & FATTR4_WORD1_OWNER) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003105 status = nfsd4_encode_user(xdr, rqstp, stat.uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 if (status)
3107 goto out;
3108 }
3109 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003110 status = nfsd4_encode_group(xdr, rqstp, stat.gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 if (status)
3112 goto out;
3113 }
3114 if (bmval1 & FATTR4_WORD1_RAWDEV) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003115 p = xdr_reserve_space(xdr, 8);
3116 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003118 *p++ = cpu_to_be32((u32) MAJOR(stat.rdev));
3119 *p++ = cpu_to_be32((u32) MINOR(stat.rdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 }
3121 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003122 p = xdr_reserve_space(xdr, 8);
3123 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 goto out_resource;
3125 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003126 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 }
3128 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003129 p = xdr_reserve_space(xdr, 8);
3130 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 goto out_resource;
3132 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003133 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 }
3135 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003136 p = xdr_reserve_space(xdr, 8);
3137 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 goto out_resource;
3139 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003140 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 }
3142 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003143 p = xdr_reserve_space(xdr, 8);
3144 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 goto out_resource;
3146 dummy64 = (u64)stat.blocks << 9;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003147 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 }
3149 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003150 p = xdr_reserve_space(xdr, 12);
3151 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003153 p = xdr_encode_hyper(p, (s64)stat.atime.tv_sec);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003154 *p++ = cpu_to_be32(stat.atime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 }
3156 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003157 p = xdr_reserve_space(xdr, 12);
3158 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 goto out_resource;
J. Bruce Fields16945142018-04-25 14:34:11 -04003160 p = encode_time_delta(p, d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 }
3162 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003163 p = xdr_reserve_space(xdr, 12);
3164 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003166 p = xdr_encode_hyper(p, (s64)stat.ctime.tv_sec);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003167 *p++ = cpu_to_be32(stat.ctime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 }
3169 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003170 p = xdr_reserve_space(xdr, 12);
3171 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003173 p = xdr_encode_hyper(p, (s64)stat.mtime.tv_sec);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003174 *p++ = cpu_to_be32(stat.mtime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 }
3176 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
Kinglong Mee0a2050d2015-07-30 21:51:54 +08003177 struct kstat parent_stat;
3178 u64 ino = stat.ino;
3179
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003180 p = xdr_reserve_space(xdr, 8);
3181 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 goto out_resource;
Frank Filz406a7ea2007-11-27 11:34:05 -08003183 /*
3184 * Get parent's attributes if not ignoring crossmount
3185 * and this is the root of a cross-mounted filesystem.
3186 */
3187 if (ignore_crossmnt == 0 &&
Kinglong Mee0a2050d2015-07-30 21:51:54 +08003188 dentry == exp->ex_path.mnt->mnt_root) {
3189 err = get_parent_attributes(exp, &parent_stat);
3190 if (err)
3191 goto out_nfserr;
3192 ino = parent_stat.ino;
3193 }
3194 p = xdr_encode_hyper(p, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003196#ifdef CONFIG_NFSD_PNFS
Kinglong Mee6896f152015-07-30 21:52:44 +08003197 if (bmval1 & FATTR4_WORD1_FS_LAYOUT_TYPES) {
Jeff Layton8a4c3922016-07-10 15:55:58 -04003198 status = nfsd4_encode_layout_types(xdr, exp->ex_layout_types);
Kinglong Mee6896f152015-07-30 21:52:44 +08003199 if (status)
3200 goto out;
3201 }
3202
3203 if (bmval2 & FATTR4_WORD2_LAYOUT_TYPES) {
Jeff Layton8a4c3922016-07-10 15:55:58 -04003204 status = nfsd4_encode_layout_types(xdr, exp->ex_layout_types);
Kinglong Mee6896f152015-07-30 21:52:44 +08003205 if (status)
3206 goto out;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003207 }
3208
3209 if (bmval2 & FATTR4_WORD2_LAYOUT_BLKSIZE) {
3210 p = xdr_reserve_space(xdr, 4);
3211 if (!p)
3212 goto out_resource;
3213 *p++ = cpu_to_be32(stat.blksize);
3214 }
3215#endif /* CONFIG_NFSD_PNFS */
Benny Halevy8c18f202009-04-03 08:29:14 +03003216 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
Trond Myklebustb26b78c2017-05-09 16:24:59 -04003217 u32 supp[3];
3218
3219 memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
3220 supp[0] &= NFSD_SUPPATTR_EXCLCREAT_WORD0;
3221 supp[1] &= NFSD_SUPPATTR_EXCLCREAT_WORD1;
3222 supp[2] &= NFSD_SUPPATTR_EXCLCREAT_WORD2;
3223
3224 status = nfsd4_encode_bitmap(xdr, supp[0], supp[1], supp[2]);
Kinglong Mee75976de2015-07-30 21:55:30 +08003225 if (status)
3226 goto out;
Benny Halevy8c18f202009-04-03 08:29:14 +03003227 }
Andy Adamson7e705702009-04-03 08:29:11 +03003228
J. Bruce Fieldsa8585762018-04-25 13:26:23 -04003229 if (bmval2 & FATTR4_WORD2_CHANGE_ATTR_TYPE) {
3230 p = xdr_reserve_space(xdr, 4);
3231 if (!p)
3232 goto out_resource;
3233 if (IS_I_VERSION(d_inode(dentry)))
3234 *p++ = cpu_to_be32(NFS4_CHANGE_TYPE_IS_MONOTONIC_INCR);
3235 else
3236 *p++ = cpu_to_be32(NFS4_CHANGE_TYPE_IS_TIME_METADATA);
3237 }
3238
Arnd Bergmann0ab88ca2019-03-22 15:07:11 +01003239#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
Kinglong Mee7d580722015-07-30 21:54:26 +08003240 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
3241 status = nfsd4_encode_security_label(xdr, rqstp, context,
3242 contextlen);
3243 if (status)
3244 goto out;
3245 }
Arnd Bergmann0ab88ca2019-03-22 15:07:11 +01003246#endif
Kinglong Mee7d580722015-07-30 21:54:26 +08003247
Frank van der Linden0e885e82020-06-23 22:39:27 +00003248 if (bmval2 & FATTR4_WORD2_XATTR_SUPPORT) {
3249 p = xdr_reserve_space(xdr, 4);
3250 if (!p)
3251 goto out_resource;
3252 err = xattr_supported_namespace(d_inode(dentry),
3253 XATTR_USER_PREFIX);
3254 *p++ = cpu_to_be32(err == 0);
3255 }
3256
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04003257 attrlen = htonl(xdr->buf->len - attrlen_offset - 4);
3258 write_bytes_to_xdr_buf(xdr->buf, attrlen_offset, &attrlen, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 status = nfs_ok;
3260
3261out:
J. Bruce Fieldsba4e55b2013-05-15 10:27:52 -04003262#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
David Quigley18032ca2013-05-02 13:19:10 -04003263 if (context)
3264 security_release_secctx(context, contextlen);
J. Bruce Fieldsba4e55b2013-05-15 10:27:52 -04003265#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08003266 kfree(acl);
Yan, Zheng18df11d02014-03-10 12:52:07 +08003267 if (tempfh) {
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05003268 fh_put(tempfh);
Yan, Zheng18df11d02014-03-10 12:52:07 +08003269 kfree(tempfh);
3270 }
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05003271 if (status)
3272 xdr_truncate_encode(xdr, starting_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 return status;
3274out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07003275 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 goto out;
3277out_resource:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 status = nfserr_resource;
3279 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280}
3281
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04003282static void svcxdr_init_encode_from_buffer(struct xdr_stream *xdr,
3283 struct xdr_buf *buf, __be32 *p, int bytes)
3284{
3285 xdr->scratch.iov_len = 0;
3286 memset(buf, 0, sizeof(struct xdr_buf));
3287 buf->head[0].iov_base = p;
3288 buf->head[0].iov_len = 0;
3289 buf->len = 0;
3290 xdr->buf = buf;
3291 xdr->iov = buf->head;
3292 xdr->p = p;
3293 xdr->end = (void *)p + bytes;
3294 buf->buflen = bytes;
3295}
3296
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04003297__be32 nfsd4_encode_fattr_to_buf(__be32 **p, int words,
3298 struct svc_fh *fhp, struct svc_export *exp,
3299 struct dentry *dentry, u32 *bmval,
3300 struct svc_rqst *rqstp, int ignore_crossmnt)
3301{
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04003302 struct xdr_buf dummy;
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04003303 struct xdr_stream xdr;
3304 __be32 ret;
3305
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04003306 svcxdr_init_encode_from_buffer(&xdr, &dummy, *p, words << 2);
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04003307 ret = nfsd4_encode_fattr(&xdr, fhp, exp, dentry, bmval, rqstp,
3308 ignore_crossmnt);
3309 *p = xdr.p;
3310 return ret;
3311}
3312
J. Bruce Fieldsc0ce6ec2008-02-11 15:48:47 -05003313static inline int attributes_need_mount(u32 *bmval)
3314{
3315 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
3316 return 1;
3317 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
3318 return 1;
3319 return 0;
3320}
3321
Al Virob37ad282006-10-19 23:28:59 -07003322static __be32
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003323nfsd4_encode_dirent_fattr(struct xdr_stream *xdr, struct nfsd4_readdir *cd,
3324 const char *name, int namlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325{
3326 struct svc_export *exp = cd->rd_fhp->fh_export;
3327 struct dentry *dentry;
Al Virob37ad282006-10-19 23:28:59 -07003328 __be32 nfserr;
Frank Filz406a7ea2007-11-27 11:34:05 -08003329 int ignore_crossmnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330
Al Viro6c2d47982019-10-31 01:21:58 -04003331 dentry = lookup_positive_unlocked(name, cd->rd_fhp->fh_dentry, namlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 if (IS_ERR(dentry))
3333 return nfserrno(PTR_ERR(dentry));
3334
3335 exp_get(exp);
Frank Filz406a7ea2007-11-27 11:34:05 -08003336 /*
3337 * In the case of a mountpoint, the client may be asking for
3338 * attributes that are only properties of the underlying filesystem
3339 * as opposed to the cross-mounted file system. In such a case,
3340 * we will not follow the cross mount and will fill the attribtutes
3341 * directly from the mountpoint dentry.
3342 */
J. Bruce Fields3227fa42009-10-25 21:43:01 -04003343 if (nfsd_mountpoint(dentry, exp)) {
J.Bruce Fields021d3a72006-12-13 00:35:24 -08003344 int err;
3345
J. Bruce Fields3227fa42009-10-25 21:43:01 -04003346 if (!(exp->ex_flags & NFSEXP_V4ROOT)
3347 && !attributes_need_mount(cd->rd_bmval)) {
3348 ignore_crossmnt = 1;
3349 goto out_encode;
3350 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07003351 /*
3352 * Why the heck aren't we just using nfsd_lookup??
3353 * Different "."/".." handling? Something else?
3354 * At least, add a comment here to explain....
3355 */
J.Bruce Fields021d3a72006-12-13 00:35:24 -08003356 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
3357 if (err) {
3358 nfserr = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 goto out_put;
3360 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07003361 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
3362 if (nfserr)
3363 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364
3365 }
J. Bruce Fields3227fa42009-10-25 21:43:01 -04003366out_encode:
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003367 nfserr = nfsd4_encode_fattr(xdr, NULL, exp, dentry, cd->rd_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08003368 cd->rd_rqstp, ignore_crossmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369out_put:
3370 dput(dentry);
3371 exp_put(exp);
3372 return nfserr;
3373}
3374
Al Viro2ebbc012006-10-19 23:28:58 -07003375static __be32 *
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003376nfsd4_encode_rdattr_error(struct xdr_stream *xdr, __be32 nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377{
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003378 __be32 *p;
3379
Kinglong Meec3a45612014-07-06 11:34:43 +08003380 p = xdr_reserve_space(xdr, 20);
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003381 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 return NULL;
3383 *p++ = htonl(2);
3384 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
3385 *p++ = htonl(0); /* bmval1 */
3386
J. Bruce Fields87915c62013-01-16 17:33:28 -05003387 *p++ = htonl(4); /* attribute length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 *p++ = nfserr; /* no htonl */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 return p;
3390}
3391
3392static int
NeilBrowna0ad13e2007-01-26 00:57:10 -08003393nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
3394 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395{
NeilBrowna0ad13e2007-01-26 00:57:10 -08003396 struct readdir_cd *ccd = ccdv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003398 struct xdr_stream *xdr = cd->xdr;
3399 int start_offset = xdr->buf->len;
3400 int cookie_offset;
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04003401 u32 name_and_cookie;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003402 int entry_bytes;
Al Virob37ad282006-10-19 23:28:59 -07003403 __be32 nfserr = nfserr_toosmall;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003404 __be64 wire_offset;
3405 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406
3407 /* In nfsv4, "." and ".." never make it onto the wire.. */
3408 if (name && isdotent(name, namlen)) {
3409 cd->common.err = nfs_ok;
3410 return 0;
3411 }
3412
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003413 if (cd->cookie_offset) {
3414 wire_offset = cpu_to_be64(offset);
3415 write_bytes_to_xdr_buf(xdr->buf, cd->cookie_offset,
3416 &wire_offset, 8);
3417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003419 p = xdr_reserve_space(xdr, 4);
3420 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 *p++ = xdr_one; /* mark entry present */
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003423 cookie_offset = xdr->buf->len;
3424 p = xdr_reserve_space(xdr, 3*4 + namlen);
3425 if (!p)
3426 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
3428 p = xdr_encode_array(p, name, namlen); /* name length & name */
3429
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003430 nfserr = nfsd4_encode_dirent_fattr(xdr, cd, name, namlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 switch (nfserr) {
3432 case nfs_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 break;
3434 case nfserr_resource:
3435 nfserr = nfserr_toosmall;
3436 goto fail;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04003437 case nfserr_noent:
Kinglong Meef41c5ad2014-06-13 17:32:42 +08003438 xdr_truncate_encode(xdr, start_offset);
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04003439 goto skip_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 default:
3441 /*
3442 * If the client requested the RDATTR_ERROR attribute,
3443 * we stuff the error code into this attribute
3444 * and continue. If this attribute was not requested,
3445 * then in accordance with the spec, we fail the
3446 * entire READDIR operation(!)
3447 */
3448 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
3449 goto fail;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003450 p = nfsd4_encode_rdattr_error(xdr, nfserr);
Fred Isaman34081ef2006-01-18 17:43:40 -08003451 if (p == NULL) {
3452 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 goto fail;
Fred Isaman34081ef2006-01-18 17:43:40 -08003454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 }
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003456 nfserr = nfserr_toosmall;
3457 entry_bytes = xdr->buf->len - start_offset;
3458 if (entry_bytes > cd->rd_maxcount)
3459 goto fail;
3460 cd->rd_maxcount -= entry_bytes;
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04003461 /*
3462 * RFC 3530 14.2.24 describes rd_dircount as only a "hint", so
3463 * let's always let through the first entry, at least:
3464 */
J. Bruce Fields0ec016e2014-12-19 18:01:35 -05003465 if (!cd->rd_dircount)
3466 goto fail;
3467 name_and_cookie = 4 + 4 * XDR_QUADLEN(namlen) + 8;
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04003468 if (name_and_cookie > cd->rd_dircount && cd->cookie_offset)
3469 goto fail;
3470 cd->rd_dircount -= min(cd->rd_dircount, name_and_cookie);
J. Bruce Fields0ec016e2014-12-19 18:01:35 -05003471
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003472 cd->cookie_offset = cookie_offset;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04003473skip_entry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 cd->common.err = nfs_ok;
3475 return 0;
3476fail:
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003477 xdr_truncate_encode(xdr, start_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 cd->common.err = nfserr;
3479 return -EINVAL;
3480}
3481
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003482static __be32
3483nfsd4_encode_stateid(struct xdr_stream *xdr, stateid_t *sid)
Benny Halevye2f282b2008-08-12 20:45:07 +03003484{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003485 __be32 *p;
Benny Halevye2f282b2008-08-12 20:45:07 +03003486
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003487 p = xdr_reserve_space(xdr, sizeof(stateid_t));
3488 if (!p)
3489 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003490 *p++ = cpu_to_be32(sid->si_generation);
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003491 p = xdr_encode_opaque_fixed(p, &sid->si_opaque,
3492 sizeof(stateid_opaque_t));
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003493 return 0;
Benny Halevye2f282b2008-08-12 20:45:07 +03003494}
3495
Benny Halevy695e12f2008-07-04 14:38:33 +03003496static __be32
Al Virob37ad282006-10-19 23:28:59 -07003497nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003499 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003500 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003502 p = xdr_reserve_space(xdr, 8);
3503 if (!p)
3504 return nfserr_resource;
3505 *p++ = cpu_to_be32(access->ac_supported);
3506 *p++ = cpu_to_be32(access->ac_resp_access);
3507 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508}
3509
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003510static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
3511{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003512 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003513 __be32 *p;
3514
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003515 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 8);
3516 if (!p)
3517 return nfserr_resource;
3518 p = xdr_encode_opaque_fixed(p, bcts->sessionid.data,
3519 NFS4_MAX_SESSIONID_LEN);
3520 *p++ = cpu_to_be32(bcts->dir);
3521 /* Upshifting from TCP to RDMA is not supported */
3522 *p++ = cpu_to_be32(0);
3523 return 0;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003524}
3525
Benny Halevy695e12f2008-07-04 14:38:33 +03003526static __be32
Al Virob37ad282006-10-19 23:28:59 -07003527nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003529 struct xdr_stream *xdr = &resp->xdr;
3530
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003531 return nfsd4_encode_stateid(xdr, &close->cl_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532}
3533
3534
Benny Halevy695e12f2008-07-04 14:38:33 +03003535static __be32
Al Virob37ad282006-10-19 23:28:59 -07003536nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003538 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003539 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003541 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
3542 if (!p)
3543 return nfserr_resource;
3544 p = xdr_encode_opaque_fixed(p, commit->co_verf.data,
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003545 NFS4_VERIFIER_SIZE);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003546 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547}
3548
Benny Halevy695e12f2008-07-04 14:38:33 +03003549static __be32
Al Virob37ad282006-10-19 23:28:59 -07003550nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003552 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003553 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003555 p = xdr_reserve_space(xdr, 20);
3556 if (!p)
3557 return nfserr_resource;
3558 encode_cinfo(p, &create->cr_cinfo);
Trond Myklebustb96811c2019-08-18 14:18:58 -04003559 return nfsd4_encode_bitmap(xdr, create->cr_bmval[0],
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003560 create->cr_bmval[1], create->cr_bmval[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561}
3562
Al Virob37ad282006-10-19 23:28:59 -07003563static __be32
3564nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565{
3566 struct svc_fh *fhp = getattr->ga_fhp;
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04003567 struct xdr_stream *xdr = &resp->xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003569 return nfsd4_encode_fattr(xdr, fhp, fhp->fh_export, fhp->fh_dentry,
3570 getattr->ga_bmval, resp->rqstp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571}
3572
Benny Halevy695e12f2008-07-04 14:38:33 +03003573static __be32
3574nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003576 struct xdr_stream *xdr = &resp->xdr;
Benny Halevy695e12f2008-07-04 14:38:33 +03003577 struct svc_fh *fhp = *fhpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 unsigned int len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003579 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003581 len = fhp->fh_handle.fh_size;
3582 p = xdr_reserve_space(xdr, len + 4);
3583 if (!p)
3584 return nfserr_resource;
3585 p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base, len);
3586 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587}
3588
3589/*
3590* Including all fields other than the name, a LOCK4denied structure requires
3591* 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
3592*/
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003593static __be32
3594nfsd4_encode_lock_denied(struct xdr_stream *xdr, struct nfsd4_lock_denied *ld)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595{
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003596 struct xdr_netobj *conf = &ld->ld_owner;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003597 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003599again:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003600 p = xdr_reserve_space(xdr, 32 + XDR_LEN(conf->len));
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003601 if (!p) {
3602 /*
3603 * Don't fail to return the result just because we can't
3604 * return the conflicting open:
3605 */
3606 if (conf->len) {
Kinglong Meef98bac52014-07-07 22:10:56 +08003607 kfree(conf->data);
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003608 conf->len = 0;
3609 conf->data = NULL;
3610 goto again;
3611 }
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003612 return nfserr_resource;
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003613 }
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003614 p = xdr_encode_hyper(p, ld->ld_start);
3615 p = xdr_encode_hyper(p, ld->ld_length);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003616 *p++ = cpu_to_be32(ld->ld_type);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003617 if (conf->len) {
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003618 p = xdr_encode_opaque_fixed(p, &ld->ld_clientid, 8);
3619 p = xdr_encode_opaque(p, conf->data, conf->len);
Kinglong Meef98bac52014-07-07 22:10:56 +08003620 kfree(conf->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003622 p = xdr_encode_hyper(p, (u64)0); /* clientid */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003623 *p++ = cpu_to_be32(0); /* length of owner name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 }
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003625 return nfserr_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626}
3627
Benny Halevy695e12f2008-07-04 14:38:33 +03003628static __be32
Al Virob37ad282006-10-19 23:28:59 -07003629nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003631 struct xdr_stream *xdr = &resp->xdr;
3632
Benny Halevye2f282b2008-08-12 20:45:07 +03003633 if (!nfserr)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003634 nfserr = nfsd4_encode_stateid(xdr, &lock->lk_resp_stateid);
Benny Halevye2f282b2008-08-12 20:45:07 +03003635 else if (nfserr == nfserr_denied)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003636 nfserr = nfsd4_encode_lock_denied(xdr, &lock->lk_denied);
Kinglong Meef98bac52014-07-07 22:10:56 +08003637
Benny Halevy695e12f2008-07-04 14:38:33 +03003638 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639}
3640
Benny Halevy695e12f2008-07-04 14:38:33 +03003641static __be32
Al Virob37ad282006-10-19 23:28:59 -07003642nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003644 struct xdr_stream *xdr = &resp->xdr;
3645
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 if (nfserr == nfserr_denied)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003647 nfsd4_encode_lock_denied(xdr, &lockt->lt_denied);
Benny Halevy695e12f2008-07-04 14:38:33 +03003648 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649}
3650
Benny Halevy695e12f2008-07-04 14:38:33 +03003651static __be32
Al Virob37ad282006-10-19 23:28:59 -07003652nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003654 struct xdr_stream *xdr = &resp->xdr;
3655
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003656 return nfsd4_encode_stateid(xdr, &locku->lu_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657}
3658
3659
Benny Halevy695e12f2008-07-04 14:38:33 +03003660static __be32
Al Virob37ad282006-10-19 23:28:59 -07003661nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003663 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003664 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003666 p = xdr_reserve_space(xdr, 20);
3667 if (!p)
3668 return nfserr_resource;
3669 p = encode_cinfo(p, &link->li_cinfo);
3670 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671}
3672
3673
Benny Halevy695e12f2008-07-04 14:38:33 +03003674static __be32
Al Virob37ad282006-10-19 23:28:59 -07003675nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003677 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003678 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003680 nfserr = nfsd4_encode_stateid(xdr, &open->op_stateid);
3681 if (nfserr)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003682 return nfserr;
Kinglong Mee75976de2015-07-30 21:55:30 +08003683 p = xdr_reserve_space(xdr, 24);
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003684 if (!p)
3685 return nfserr_resource;
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04003686 p = encode_cinfo(p, &open->op_cinfo);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003687 *p++ = cpu_to_be32(open->op_rflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688
Kinglong Mee75976de2015-07-30 21:55:30 +08003689 nfserr = nfsd4_encode_bitmap(xdr, open->op_bmval[0], open->op_bmval[1],
3690 open->op_bmval[2]);
3691 if (nfserr)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003692 return nfserr;
Kinglong Mee75976de2015-07-30 21:55:30 +08003693
3694 p = xdr_reserve_space(xdr, 4);
3695 if (!p)
3696 return nfserr_resource;
3697
3698 *p++ = cpu_to_be32(open->op_delegate_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 switch (open->op_delegate_type) {
3700 case NFS4_OPEN_DELEGATE_NONE:
3701 break;
3702 case NFS4_OPEN_DELEGATE_READ:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003703 nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
3704 if (nfserr)
3705 return nfserr;
3706 p = xdr_reserve_space(xdr, 20);
3707 if (!p)
3708 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003709 *p++ = cpu_to_be32(open->op_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710
3711 /*
3712 * TODO: ACE's in delegations
3713 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003714 *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
3715 *p++ = cpu_to_be32(0);
3716 *p++ = cpu_to_be32(0);
3717 *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 break;
3719 case NFS4_OPEN_DELEGATE_WRITE:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003720 nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
3721 if (nfserr)
3722 return nfserr;
3723 p = xdr_reserve_space(xdr, 32);
3724 if (!p)
3725 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003726 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727
3728 /*
3729 * TODO: space_limit's in delegations
3730 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003731 *p++ = cpu_to_be32(NFS4_LIMIT_SIZE);
3732 *p++ = cpu_to_be32(~(u32)0);
3733 *p++ = cpu_to_be32(~(u32)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734
3735 /*
3736 * TODO: ACE's in delegations
3737 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003738 *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
3739 *p++ = cpu_to_be32(0);
3740 *p++ = cpu_to_be32(0);
3741 *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 break;
Benny Halevyd24433c2012-02-16 20:57:17 +02003743 case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
3744 switch (open->op_why_no_deleg) {
3745 case WND4_CONTENTION:
3746 case WND4_RESOURCE:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003747 p = xdr_reserve_space(xdr, 8);
3748 if (!p)
3749 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003750 *p++ = cpu_to_be32(open->op_why_no_deleg);
3751 /* deleg signaling not supported yet: */
3752 *p++ = cpu_to_be32(0);
Benny Halevyd24433c2012-02-16 20:57:17 +02003753 break;
3754 default:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003755 p = xdr_reserve_space(xdr, 4);
3756 if (!p)
3757 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003758 *p++ = cpu_to_be32(open->op_why_no_deleg);
Benny Halevyd24433c2012-02-16 20:57:17 +02003759 }
Benny Halevyd24433c2012-02-16 20:57:17 +02003760 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 default:
3762 BUG();
3763 }
3764 /* XXX save filehandle here */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003765 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766}
3767
Benny Halevy695e12f2008-07-04 14:38:33 +03003768static __be32
Al Virob37ad282006-10-19 23:28:59 -07003769nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003771 struct xdr_stream *xdr = &resp->xdr;
3772
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003773 return nfsd4_encode_stateid(xdr, &oc->oc_resp_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774}
3775
Benny Halevy695e12f2008-07-04 14:38:33 +03003776static __be32
Al Virob37ad282006-10-19 23:28:59 -07003777nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003779 struct xdr_stream *xdr = &resp->xdr;
3780
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003781 return nfsd4_encode_stateid(xdr, &od->od_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782}
3783
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003784static __be32 nfsd4_encode_splice_read(
3785 struct nfsd4_compoundres *resp,
3786 struct nfsd4_read *read,
3787 struct file *file, unsigned long maxcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788{
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003789 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003790 struct xdr_buf *buf = xdr->buf;
Chuck Lever76e54922020-11-05 10:24:19 -05003791 int status, space_left;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003792 u32 eof;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003793 __be32 nfserr;
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003794 __be32 *p = xdr->p - 2;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003795
Kinglong Meed5d5c302014-07-09 21:51:27 +08003796 /* Make sure there will be room for padding if needed */
3797 if (xdr->end - xdr->p < 1)
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003798 return nfserr_resource;
3799
Chuck Lever87c59422018-03-28 13:29:11 -04003800 nfserr = nfsd_splice_read(read->rd_rqstp, read->rd_fhp,
Trond Myklebust83a63072019-08-26 13:03:11 -04003801 file, read->rd_offset, &maxcount, &eof);
Chuck Lever87c59422018-03-28 13:29:11 -04003802 read->rd_length = maxcount;
Chuck Lever76e54922020-11-05 10:24:19 -05003803 if (nfserr)
3804 goto out_err;
3805 status = svc_encode_result_payload(read->rd_rqstp,
3806 buf->head[0].iov_len, maxcount);
3807 if (status) {
3808 nfserr = nfserrno(status);
3809 goto out_err;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003810 }
3811
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003812 *(p++) = htonl(eof);
3813 *(p++) = htonl(maxcount);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003814
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003815 buf->page_len = maxcount;
3816 buf->len += maxcount;
J. Bruce Fields15b23ef2014-09-24 16:32:34 -04003817 xdr->page_ptr += (buf->page_base + maxcount + PAGE_SIZE - 1)
3818 / PAGE_SIZE;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003819
3820 /* Use rest of head for padding and remaining ops: */
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003821 buf->tail[0].iov_base = xdr->p;
3822 buf->tail[0].iov_len = 0;
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003823 xdr->iov = buf->tail;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003824 if (maxcount&3) {
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003825 int pad = 4 - (maxcount&3);
3826
3827 *(xdr->p++) = 0;
3828
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003829 buf->tail[0].iov_base += maxcount&3;
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003830 buf->tail[0].iov_len = pad;
3831 buf->len += pad;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003832 }
3833
3834 space_left = min_t(int, (void *)xdr->end - (void *)xdr->p,
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003835 buf->buflen - buf->len);
3836 buf->buflen = buf->len + space_left;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003837 xdr->end = (__be32 *)((void *)xdr->end + space_left);
3838
3839 return 0;
Chuck Lever76e54922020-11-05 10:24:19 -05003840
3841out_err:
3842 /*
3843 * nfsd_splice_actor may have already messed with the
3844 * page length; reset it so as not to confuse
3845 * xdr_truncate_encode in our caller.
3846 */
3847 buf->page_len = 0;
3848 return nfserr;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003849}
3850
3851static __be32 nfsd4_encode_readv(struct nfsd4_compoundres *resp,
3852 struct nfsd4_read *read,
3853 struct file *file, unsigned long maxcount)
3854{
3855 struct xdr_stream *xdr = &resp->xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 u32 eof;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003857 int starting_len = xdr->buf->len - 8;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003858 __be32 nfserr;
3859 __be32 tmp;
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003860 int pad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861
Anna Schumaker403217f2020-08-17 12:53:06 -04003862 read->rd_vlen = xdr_reserve_space_vec(xdr, resp->rqstp->rq_vec, maxcount);
3863 if (read->rd_vlen < 0)
3864 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865
Chuck Lever87c59422018-03-28 13:29:11 -04003866 nfserr = nfsd_readv(resp->rqstp, read->rd_fhp, file, read->rd_offset,
Trond Myklebust83a63072019-08-26 13:03:11 -04003867 resp->rqstp->rq_vec, read->rd_vlen, &maxcount,
3868 &eof);
Chuck Lever87c59422018-03-28 13:29:11 -04003869 read->rd_length = maxcount;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003870 if (nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 return nfserr;
Chuck Lever03493bc2020-06-10 10:36:42 -04003872 if (svc_encode_result_payload(resp->rqstp, starting_len + 8, maxcount))
Chuck Lever41205532020-03-02 14:45:53 -05003873 return nfserr_io;
Chuck Lever7dcf4ab2020-03-02 15:00:04 -05003874 xdr_truncate_encode(xdr, starting_len + 8 + xdr_align_size(maxcount));
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003875
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003876 tmp = htonl(eof);
3877 write_bytes_to_xdr_buf(xdr->buf, starting_len , &tmp, 4);
3878 tmp = htonl(maxcount);
3879 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp, 4);
3880
Chuck Lever7dcf4ab2020-03-02 15:00:04 -05003881 tmp = xdr_zero;
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003882 pad = (maxcount&3) ? 4 - (maxcount&3) : 0;
3883 write_bytes_to_xdr_buf(xdr->buf, starting_len + 8 + maxcount,
Chuck Lever7dcf4ab2020-03-02 15:00:04 -05003884 &tmp, pad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 return 0;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003886
3887}
3888
3889static __be32
3890nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
3891 struct nfsd4_read *read)
3892{
3893 unsigned long maxcount;
3894 struct xdr_stream *xdr = &resp->xdr;
Jeff Layton5c4583b2019-08-18 14:18:54 -04003895 struct file *file;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003896 int starting_len = xdr->buf->len;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003897 __be32 *p;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003898
Jeff Layton5c4583b2019-08-18 14:18:54 -04003899 if (nfserr)
3900 return nfserr;
3901 file = read->rd_nf->nf_file;
3902
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003903 p = xdr_reserve_space(xdr, 8); /* eof flag and byte count */
3904 if (!p) {
Jeff Layton779fb0f2014-11-19 07:51:18 -05003905 WARN_ON_ONCE(test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags));
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003906 return nfserr_resource;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003907 }
Christoph Hellwig68e8bb02015-06-18 16:45:02 +02003908 if (resp->xdr.buf->page_len &&
3909 test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags)) {
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003910 WARN_ON_ONCE(1);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003911 return nfserr_resource;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003912 }
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003913 xdr_commit_encode(xdr);
3914
3915 maxcount = svc_max_payload(resp->rqstp);
Christoph Hellwig68e8bb02015-06-18 16:45:02 +02003916 maxcount = min_t(unsigned long, maxcount,
3917 (xdr->buf->buflen - xdr->buf->len));
Kinglong Mee3c7aa152014-06-10 18:08:19 +08003918 maxcount = min_t(unsigned long, maxcount, read->rd_length);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003919
Christoph Hellwig68e8bb02015-06-18 16:45:02 +02003920 if (file->f_op->splice_read &&
3921 test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags))
Christoph Hellwig96bcad52015-06-18 16:45:01 +02003922 nfserr = nfsd4_encode_splice_read(resp, read, file, maxcount);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003923 else
Christoph Hellwig96bcad52015-06-18 16:45:01 +02003924 nfserr = nfsd4_encode_readv(resp, read, file, maxcount);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003925
Christoph Hellwig96bcad52015-06-18 16:45:01 +02003926 if (nfserr)
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003927 xdr_truncate_encode(xdr, starting_len);
Christoph Hellwig96bcad52015-06-18 16:45:01 +02003928
Christoph Hellwig96bcad52015-06-18 16:45:01 +02003929 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930}
3931
Al Virob37ad282006-10-19 23:28:59 -07003932static __be32
3933nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934{
3935 int maxcount;
J. Bruce Fields476a7b12014-01-20 17:08:27 -05003936 __be32 wire_count;
3937 int zero = 0;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003938 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05003939 int length_offset = xdr->buf->len;
Chuck Lever76e54922020-11-05 10:24:19 -05003940 int status;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003941 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04003943 p = xdr_reserve_space(xdr, 4);
3944 if (!p)
3945 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 maxcount = PAGE_SIZE;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003947
J. Bruce Fields476a7b12014-01-20 17:08:27 -05003948 p = xdr_reserve_space(xdr, maxcount);
3949 if (!p)
J. Bruce Fields4e21ac42014-03-22 15:15:11 -04003950 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 /*
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01003952 * XXX: By default, vfs_readlink() will truncate symlinks if they
3953 * would overflow the buffer. Is this kosher in NFSv4? If not, one
3954 * easy fix is: if vfs_readlink() precisely fills the buffer, assume
3955 * that truncation occurred, and return NFS4ERR_RESOURCE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 */
J. Bruce Fields476a7b12014-01-20 17:08:27 -05003957 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp,
3958 (char *)p, &maxcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 if (nfserr == nfserr_isdir)
J. Bruce Fieldsd3f627c2014-02-26 17:00:38 -05003960 nfserr = nfserr_inval;
Chuck Lever76e54922020-11-05 10:24:19 -05003961 if (nfserr)
3962 goto out_err;
3963 status = svc_encode_result_payload(readlink->rl_rqstp, length_offset,
3964 maxcount);
3965 if (status) {
3966 nfserr = nfserrno(status);
3967 goto out_err;
J. Bruce Fieldsd3f627c2014-02-26 17:00:38 -05003968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969
J. Bruce Fields476a7b12014-01-20 17:08:27 -05003970 wire_count = htonl(maxcount);
3971 write_bytes_to_xdr_buf(xdr->buf, length_offset, &wire_count, 4);
Avi Kivity69bbd9c2014-06-26 17:23:12 +03003972 xdr_truncate_encode(xdr, length_offset + 4 + ALIGN(maxcount, 4));
J. Bruce Fields476a7b12014-01-20 17:08:27 -05003973 if (maxcount & 3)
3974 write_bytes_to_xdr_buf(xdr->buf, length_offset + 4 + maxcount,
3975 &zero, 4 - (maxcount&3));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 return 0;
Chuck Lever76e54922020-11-05 10:24:19 -05003977
3978out_err:
3979 xdr_truncate_encode(xdr, length_offset);
3980 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981}
3982
Al Virob37ad282006-10-19 23:28:59 -07003983static __be32
3984nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985{
3986 int maxcount;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003987 int bytes_left;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 loff_t offset;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003989 __be64 wire_offset;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003990 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05003991 int starting_len = xdr->buf->len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003992 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003994 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
3995 if (!p)
3996 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997
3998 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003999 *p++ = cpu_to_be32(0);
4000 *p++ = cpu_to_be32(0);
J. Bruce Fields4aea24b2014-01-15 15:17:58 -05004001 resp->xdr.buf->head[0].iov_len = ((char *)resp->xdr.p)
4002 - (char *)resp->xdr.buf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003
4004 /*
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004005 * Number of bytes left for directory entries allowing for the
4006 * final 8 bytes of the readdir and a following failed op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 */
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004008 bytes_left = xdr->buf->buflen - xdr->buf->len
4009 - COMPOUND_ERR_SLACK_SPACE - 8;
4010 if (bytes_left < 0) {
4011 nfserr = nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 goto err_no_verf;
4013 }
Scott Mayhew9c2ece62018-05-07 09:01:08 -04004014 maxcount = svc_max_payload(resp->rqstp);
4015 maxcount = min_t(u32, readdir->rd_maxcount, maxcount);
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004016 /*
4017 * Note the rfc defines rd_maxcount as the size of the
4018 * READDIR4resok structure, which includes the verifier above
4019 * and the 8 bytes encoded at the end of this function:
4020 */
4021 if (maxcount < 16) {
4022 nfserr = nfserr_toosmall;
4023 goto err_no_verf;
4024 }
4025 maxcount = min_t(int, maxcount-16, bytes_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04004027 /* RFC 3530 14.2.24 allows us to ignore dircount when it's 0: */
4028 if (!readdir->rd_dircount)
Scott Mayhew9c2ece62018-05-07 09:01:08 -04004029 readdir->rd_dircount = svc_max_payload(resp->rqstp);
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04004030
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004031 readdir->xdr = xdr;
4032 readdir->rd_maxcount = maxcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 readdir->common.err = 0;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004034 readdir->cookie_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035
4036 offset = readdir->rd_cookie;
4037 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
4038 &offset,
4039 &readdir->common, nfsd4_encode_dirent);
4040 if (nfserr == nfs_ok &&
4041 readdir->common.err == nfserr_toosmall &&
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004042 xdr->buf->len == starting_len + 8) {
4043 /* nothing encoded; which limit did we hit?: */
4044 if (maxcount - 16 < bytes_left)
4045 /* It was the fault of rd_maxcount: */
4046 nfserr = nfserr_toosmall;
4047 else
4048 /* We ran out of buffer space: */
4049 nfserr = nfserr_resource;
4050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 if (nfserr)
4052 goto err_no_verf;
4053
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004054 if (readdir->cookie_offset) {
4055 wire_offset = cpu_to_be64(offset);
4056 write_bytes_to_xdr_buf(xdr->buf, readdir->cookie_offset,
4057 &wire_offset, 8);
4058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004060 p = xdr_reserve_space(xdr, 8);
4061 if (!p) {
4062 WARN_ON_ONCE(1);
4063 goto err_no_verf;
4064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 *p++ = 0; /* no more entries */
4066 *p++ = htonl(readdir->common.err == nfserr_eof);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067
4068 return 0;
4069err_no_verf:
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05004070 xdr_truncate_encode(xdr, starting_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 return nfserr;
4072}
4073
Benny Halevy695e12f2008-07-04 14:38:33 +03004074static __be32
Al Virob37ad282006-10-19 23:28:59 -07004075nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004077 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004078 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004080 p = xdr_reserve_space(xdr, 20);
4081 if (!p)
4082 return nfserr_resource;
4083 p = encode_cinfo(p, &remove->rm_cinfo);
4084 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085}
4086
Benny Halevy695e12f2008-07-04 14:38:33 +03004087static __be32
Al Virob37ad282006-10-19 23:28:59 -07004088nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004090 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004091 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004093 p = xdr_reserve_space(xdr, 40);
4094 if (!p)
4095 return nfserr_resource;
4096 p = encode_cinfo(p, &rename->rn_sinfo);
4097 p = encode_cinfo(p, &rename->rn_tinfo);
4098 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099}
4100
Benny Halevy695e12f2008-07-04 14:38:33 +03004101static __be32
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004102nfsd4_do_encode_secinfo(struct xdr_stream *xdr, struct svc_export *exp)
Andy Adamsondcb488a32007-07-17 04:04:51 -07004103{
Chuck Lever676e4eb2013-04-30 18:48:54 -04004104 u32 i, nflavs, supported;
J. Bruce Fields4796f452007-07-17 04:04:51 -07004105 struct exp_flavor_info *flavs;
4106 struct exp_flavor_info def_flavs[2];
Chuck Lever676e4eb2013-04-30 18:48:54 -04004107 __be32 *p, *flavorsp;
4108 static bool report = true;
Andy Adamsondcb488a32007-07-17 04:04:51 -07004109
J. Bruce Fields4796f452007-07-17 04:04:51 -07004110 if (exp->ex_nflavors) {
4111 flavs = exp->ex_flavors;
4112 nflavs = exp->ex_nflavors;
4113 } else { /* Handling of some defaults in absence of real secinfo: */
4114 flavs = def_flavs;
4115 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
4116 nflavs = 2;
4117 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
4118 flavs[1].pseudoflavor = RPC_AUTH_NULL;
4119 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
4120 nflavs = 1;
4121 flavs[0].pseudoflavor
4122 = svcauth_gss_flavor(exp->ex_client);
4123 } else {
4124 nflavs = 1;
4125 flavs[0].pseudoflavor
4126 = exp->ex_client->flavour->flavour;
4127 }
4128 }
4129
Chuck Lever676e4eb2013-04-30 18:48:54 -04004130 supported = 0;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004131 p = xdr_reserve_space(xdr, 4);
4132 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004133 return nfserr_resource;
Chuck Lever676e4eb2013-04-30 18:48:54 -04004134 flavorsp = p++; /* to be backfilled later */
Chuck Lever676e4eb2013-04-30 18:48:54 -04004135
J. Bruce Fields4796f452007-07-17 04:04:51 -07004136 for (i = 0; i < nflavs; i++) {
Chuck Lever676e4eb2013-04-30 18:48:54 -04004137 rpc_authflavor_t pf = flavs[i].pseudoflavor;
Chuck Levera77c8062013-03-16 15:55:10 -04004138 struct rpcsec_gss_info info;
Andy Adamsondcb488a32007-07-17 04:04:51 -07004139
Chuck Lever676e4eb2013-04-30 18:48:54 -04004140 if (rpcauth_get_gssinfo(pf, &info) == 0) {
4141 supported++;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004142 p = xdr_reserve_space(xdr, 4 + 4 +
4143 XDR_LEN(info.oid.len) + 4 + 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(RPC_AUTH_GSS);
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004147 p = xdr_encode_opaque(p, info.oid.data, info.oid.len);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004148 *p++ = cpu_to_be32(info.qop);
4149 *p++ = cpu_to_be32(info.service);
Chuck Lever676e4eb2013-04-30 18:48:54 -04004150 } else if (pf < RPC_AUTH_MAXFLAVOR) {
4151 supported++;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004152 p = xdr_reserve_space(xdr, 4);
4153 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004154 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004155 *p++ = cpu_to_be32(pf);
Chuck Lever676e4eb2013-04-30 18:48:54 -04004156 } else {
4157 if (report)
4158 pr_warn("NFS: SECINFO: security flavor %u "
4159 "is not supported\n", pf);
Andy Adamsondcb488a32007-07-17 04:04:51 -07004160 }
4161 }
Chuck Levera77c8062013-03-16 15:55:10 -04004162
Chuck Lever676e4eb2013-04-30 18:48:54 -04004163 if (nflavs != supported)
4164 report = false;
4165 *flavorsp = htonl(supported);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004166 return 0;
Andy Adamsondcb488a32007-07-17 04:04:51 -07004167}
4168
Mi Jinlong22b6dee2010-12-27 14:29:57 +08004169static __be32
4170nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
4171 struct nfsd4_secinfo *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->si_exp);
Mi Jinlong22b6dee2010-12-27 14:29:57 +08004176}
4177
4178static __be32
4179nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
4180 struct nfsd4_secinfo_no_name *secinfo)
4181{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004182 struct xdr_stream *xdr = &resp->xdr;
4183
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004184 return nfsd4_do_encode_secinfo(xdr, secinfo->sin_exp);
Mi Jinlong22b6dee2010-12-27 14:29:57 +08004185}
4186
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187/*
4188 * The SETATTR encode routine is special -- it always encodes a bitmap,
4189 * regardless of the error status.
4190 */
Benny Halevy695e12f2008-07-04 14:38:33 +03004191static __be32
Al Virob37ad282006-10-19 23:28:59 -07004192nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004194 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004195 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004197 p = xdr_reserve_space(xdr, 16);
4198 if (!p)
4199 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 if (nfserr) {
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004201 *p++ = cpu_to_be32(3);
4202 *p++ = cpu_to_be32(0);
4203 *p++ = cpu_to_be32(0);
4204 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 }
4206 else {
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004207 *p++ = cpu_to_be32(3);
4208 *p++ = cpu_to_be32(setattr->sa_bmval[0]);
4209 *p++ = cpu_to_be32(setattr->sa_bmval[1]);
4210 *p++ = cpu_to_be32(setattr->sa_bmval[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 }
Benny Halevy695e12f2008-07-04 14:38:33 +03004212 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213}
4214
Benny Halevy695e12f2008-07-04 14:38:33 +03004215static __be32
Al Virob37ad282006-10-19 23:28:59 -07004216nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004218 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004219 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220
4221 if (!nfserr) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004222 p = xdr_reserve_space(xdr, 8 + NFS4_VERIFIER_SIZE);
4223 if (!p)
4224 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004225 p = xdr_encode_opaque_fixed(p, &scd->se_clientid, 8);
4226 p = xdr_encode_opaque_fixed(p, &scd->se_confirm,
4227 NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 }
4229 else if (nfserr == nfserr_clid_inuse) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004230 p = xdr_reserve_space(xdr, 8);
4231 if (!p)
4232 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004233 *p++ = cpu_to_be32(0);
4234 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 }
Benny Halevy695e12f2008-07-04 14:38:33 +03004236 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237}
4238
Benny Halevy695e12f2008-07-04 14:38:33 +03004239static __be32
Al Virob37ad282006-10-19 23:28:59 -07004240nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004242 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004243 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004245 p = xdr_reserve_space(xdr, 16);
4246 if (!p)
4247 return nfserr_resource;
4248 *p++ = cpu_to_be32(write->wr_bytes_written);
4249 *p++ = cpu_to_be32(write->wr_how_written);
4250 p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
4251 NFS4_VERIFIER_SIZE);
4252 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253}
4254
Benny Halevy695e12f2008-07-04 14:38:33 +03004255static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004256nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03004257 struct nfsd4_exchange_id *exid)
4258{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004259 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004260 __be32 *p;
Andy Adamson0733d212009-04-03 08:28:01 +03004261 char *major_id;
4262 char *server_scope;
4263 int major_id_sz;
4264 int server_scope_sz;
4265 uint64_t minor_id = 0;
Scott Mayhew7627d7d2020-02-19 15:52:15 -05004266 struct nfsd_net *nn = net_generic(SVC_NET(resp->rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03004267
Scott Mayhew7627d7d2020-02-19 15:52:15 -05004268 major_id = nn->nfsd_name;
4269 major_id_sz = strlen(nn->nfsd_name);
4270 server_scope = nn->nfsd_name;
4271 server_scope_sz = strlen(nn->nfsd_name);
Andy Adamson0733d212009-04-03 08:28:01 +03004272
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004273 p = xdr_reserve_space(xdr,
Andy Adamson0733d212009-04-03 08:28:01 +03004274 8 /* eir_clientid */ +
4275 4 /* eir_sequenceid */ +
4276 4 /* eir_flags */ +
Kinglong Meea8bb84b2013-12-10 15:24:36 +08004277 4 /* spr_how */);
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004278 if (!p)
4279 return nfserr_resource;
Andy Adamson0733d212009-04-03 08:28:01 +03004280
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004281 p = xdr_encode_opaque_fixed(p, &exid->clientid, 8);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004282 *p++ = cpu_to_be32(exid->seqid);
4283 *p++ = cpu_to_be32(exid->flags);
Andy Adamson0733d212009-04-03 08:28:01 +03004284
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004285 *p++ = cpu_to_be32(exid->spa_how);
Kinglong Meea8bb84b2013-12-10 15:24:36 +08004286
J. Bruce Fields57266a62013-04-13 14:27:29 -04004287 switch (exid->spa_how) {
4288 case SP4_NONE:
4289 break;
4290 case SP4_MACH_CRED:
4291 /* spo_must_enforce bitmap: */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004292 nfserr = nfsd4_encode_bitmap(xdr,
Andrew Elbleed941642016-06-15 12:52:09 -04004293 exid->spo_must_enforce[0],
4294 exid->spo_must_enforce[1],
4295 exid->spo_must_enforce[2]);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004296 if (nfserr)
4297 return nfserr;
Andrew Elbleed941642016-06-15 12:52:09 -04004298 /* spo_must_allow bitmap: */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004299 nfserr = nfsd4_encode_bitmap(xdr,
Andrew Elbleed941642016-06-15 12:52:09 -04004300 exid->spo_must_allow[0],
4301 exid->spo_must_allow[1],
4302 exid->spo_must_allow[2]);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004303 if (nfserr)
4304 return nfserr;
J. Bruce Fields57266a62013-04-13 14:27:29 -04004305 break;
4306 default:
4307 WARN_ON_ONCE(1);
4308 }
Andy Adamson0733d212009-04-03 08:28:01 +03004309
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004310 p = xdr_reserve_space(xdr,
Kinglong Meea8bb84b2013-12-10 15:24:36 +08004311 8 /* so_minor_id */ +
4312 4 /* so_major_id.len */ +
4313 (XDR_QUADLEN(major_id_sz) * 4) +
4314 4 /* eir_server_scope.len */ +
4315 (XDR_QUADLEN(server_scope_sz) * 4) +
4316 4 /* eir_server_impl_id.count (0) */);
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004317 if (!p)
4318 return nfserr_resource;
Kinglong Meea8bb84b2013-12-10 15:24:36 +08004319
Andy Adamson0733d212009-04-03 08:28:01 +03004320 /* The server_owner struct */
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04004321 p = xdr_encode_hyper(p, minor_id); /* Minor id */
Andy Adamson0733d212009-04-03 08:28:01 +03004322 /* major id */
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004323 p = xdr_encode_opaque(p, major_id, major_id_sz);
Andy Adamson0733d212009-04-03 08:28:01 +03004324
4325 /* Server scope */
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004326 p = xdr_encode_opaque(p, server_scope, server_scope_sz);
Andy Adamson0733d212009-04-03 08:28:01 +03004327
4328 /* Implementation id */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004329 *p++ = cpu_to_be32(0); /* zero length nfs_impl_id4 array */
Andy Adamson0733d212009-04-03 08:28:01 +03004330 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03004331}
4332
4333static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004334nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03004335 struct nfsd4_create_session *sess)
4336{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004337 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004338 __be32 *p;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004339
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004340 p = xdr_reserve_space(xdr, 24);
4341 if (!p)
4342 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004343 p = xdr_encode_opaque_fixed(p, sess->sessionid.data,
4344 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004345 *p++ = cpu_to_be32(sess->seqid);
4346 *p++ = cpu_to_be32(sess->flags);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004347
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004348 p = xdr_reserve_space(xdr, 28);
4349 if (!p)
4350 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004351 *p++ = cpu_to_be32(0); /* headerpadsz */
4352 *p++ = cpu_to_be32(sess->fore_channel.maxreq_sz);
4353 *p++ = cpu_to_be32(sess->fore_channel.maxresp_sz);
4354 *p++ = cpu_to_be32(sess->fore_channel.maxresp_cached);
4355 *p++ = cpu_to_be32(sess->fore_channel.maxops);
4356 *p++ = cpu_to_be32(sess->fore_channel.maxreqs);
4357 *p++ = cpu_to_be32(sess->fore_channel.nr_rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004358
4359 if (sess->fore_channel.nr_rdma_attrs) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004360 p = xdr_reserve_space(xdr, 4);
4361 if (!p)
4362 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004363 *p++ = cpu_to_be32(sess->fore_channel.rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004364 }
4365
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004366 p = xdr_reserve_space(xdr, 28);
4367 if (!p)
4368 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004369 *p++ = cpu_to_be32(0); /* headerpadsz */
4370 *p++ = cpu_to_be32(sess->back_channel.maxreq_sz);
4371 *p++ = cpu_to_be32(sess->back_channel.maxresp_sz);
4372 *p++ = cpu_to_be32(sess->back_channel.maxresp_cached);
4373 *p++ = cpu_to_be32(sess->back_channel.maxops);
4374 *p++ = cpu_to_be32(sess->back_channel.maxreqs);
4375 *p++ = cpu_to_be32(sess->back_channel.nr_rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004376
4377 if (sess->back_channel.nr_rdma_attrs) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004378 p = xdr_reserve_space(xdr, 4);
4379 if (!p)
4380 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004381 *p++ = cpu_to_be32(sess->back_channel.rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004382 }
4383 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03004384}
4385
4386static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004387nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03004388 struct nfsd4_sequence *seq)
4389{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004390 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004391 __be32 *p;
Benny Halevyb85d4c02009-04-03 08:28:08 +03004392
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004393 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 20);
4394 if (!p)
4395 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004396 p = xdr_encode_opaque_fixed(p, seq->sessionid.data,
4397 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004398 *p++ = cpu_to_be32(seq->seqid);
4399 *p++ = cpu_to_be32(seq->slotid);
J. Bruce Fieldsb7d7ca32011-08-31 15:39:30 -04004400 /* Note slotid's are numbered from zero: */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004401 *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_highest_slotid */
4402 *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_target_highest_slotid */
4403 *p++ = cpu_to_be32(seq->status_flags);
Benny Halevyb85d4c02009-04-03 08:28:08 +03004404
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04004405 resp->cstate.data_offset = xdr->buf->len; /* DRC cache data pointer */
Benny Halevyb85d4c02009-04-03 08:28:08 +03004406 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03004407}
4408
J. Bruce Fields2355c592012-04-25 16:56:22 -04004409static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004410nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
Bryan Schumaker17456802011-07-13 10:50:48 -04004411 struct nfsd4_test_stateid *test_stateid)
4412{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004413 struct xdr_stream *xdr = &resp->xdr;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004414 struct nfsd4_test_stateid_id *stateid, *next;
Bryan Schumaker17456802011-07-13 10:50:48 -04004415 __be32 *p;
Bryan Schumaker17456802011-07-13 10:50:48 -04004416
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004417 p = xdr_reserve_space(xdr, 4 + (4 * test_stateid->ts_num_ids));
4418 if (!p)
4419 return nfserr_resource;
Bryan Schumaker17456802011-07-13 10:50:48 -04004420 *p++ = htonl(test_stateid->ts_num_ids);
Bryan Schumaker17456802011-07-13 10:50:48 -04004421
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004422 list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
Al Viro02f5fde2012-04-13 00:10:34 -04004423 *p++ = stateid->ts_id_status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004424 }
Bryan Schumaker17456802011-07-13 10:50:48 -04004425
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004426 return 0;
Bryan Schumaker17456802011-07-13 10:50:48 -04004427}
4428
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004429#ifdef CONFIG_NFSD_PNFS
4430static __be32
4431nfsd4_encode_getdeviceinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
4432 struct nfsd4_getdeviceinfo *gdev)
4433{
4434 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004435 const struct nfsd4_layout_ops *ops;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004436 u32 starting_len = xdr->buf->len, needed_len;
4437 __be32 *p;
4438
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004439 p = xdr_reserve_space(xdr, 4);
4440 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004441 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004442
4443 *p++ = cpu_to_be32(gdev->gd_layout_type);
4444
4445 /* If maxcount is 0 then just update notifications */
4446 if (gdev->gd_maxcount != 0) {
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004447 ops = nfsd4_layout_ops[gdev->gd_layout_type];
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004448 nfserr = ops->encode_getdeviceinfo(xdr, gdev);
4449 if (nfserr) {
4450 /*
4451 * We don't bother to burden the layout drivers with
4452 * enforcing gd_maxcount, just tell the client to
4453 * come back with a bigger buffer if it's not enough.
4454 */
4455 if (xdr->buf->len + 4 > gdev->gd_maxcount)
4456 goto toosmall;
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004457 return nfserr;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004458 }
4459 }
4460
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004461 if (gdev->gd_notify_types) {
4462 p = xdr_reserve_space(xdr, 4 + 4);
4463 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004464 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004465 *p++ = cpu_to_be32(1); /* bitmap length */
4466 *p++ = cpu_to_be32(gdev->gd_notify_types);
4467 } else {
4468 p = xdr_reserve_space(xdr, 4);
4469 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004470 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004471 *p++ = 0;
4472 }
4473
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004474 return 0;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004475toosmall:
4476 dprintk("%s: maxcount too small\n", __func__);
4477 needed_len = xdr->buf->len + 4 /* notifications */;
4478 xdr_truncate_encode(xdr, starting_len);
4479 p = xdr_reserve_space(xdr, 4);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004480 if (!p)
4481 return nfserr_resource;
4482 *p++ = cpu_to_be32(needed_len);
4483 return nfserr_toosmall;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004484}
4485
4486static __be32
4487nfsd4_encode_layoutget(struct nfsd4_compoundres *resp, __be32 nfserr,
4488 struct nfsd4_layoutget *lgp)
4489{
4490 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004491 const struct nfsd4_layout_ops *ops;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004492 __be32 *p;
4493
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004494 p = xdr_reserve_space(xdr, 36 + sizeof(stateid_opaque_t));
4495 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004496 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004497
4498 *p++ = cpu_to_be32(1); /* we always set return-on-close */
4499 *p++ = cpu_to_be32(lgp->lg_sid.si_generation);
4500 p = xdr_encode_opaque_fixed(p, &lgp->lg_sid.si_opaque,
4501 sizeof(stateid_opaque_t));
4502
4503 *p++ = cpu_to_be32(1); /* we always return a single layout */
4504 p = xdr_encode_hyper(p, lgp->lg_seg.offset);
4505 p = xdr_encode_hyper(p, lgp->lg_seg.length);
4506 *p++ = cpu_to_be32(lgp->lg_seg.iomode);
4507 *p++ = cpu_to_be32(lgp->lg_layout_type);
4508
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004509 ops = nfsd4_layout_ops[lgp->lg_layout_type];
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004510 return ops->encode_layoutget(xdr, lgp);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004511}
4512
4513static __be32
4514nfsd4_encode_layoutcommit(struct nfsd4_compoundres *resp, __be32 nfserr,
4515 struct nfsd4_layoutcommit *lcp)
4516{
4517 struct xdr_stream *xdr = &resp->xdr;
4518 __be32 *p;
4519
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004520 p = xdr_reserve_space(xdr, 4);
4521 if (!p)
4522 return nfserr_resource;
4523 *p++ = cpu_to_be32(lcp->lc_size_chg);
4524 if (lcp->lc_size_chg) {
4525 p = xdr_reserve_space(xdr, 8);
4526 if (!p)
4527 return nfserr_resource;
4528 p = xdr_encode_hyper(p, lcp->lc_newsize);
4529 }
4530
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004531 return 0;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004532}
4533
4534static __be32
4535nfsd4_encode_layoutreturn(struct nfsd4_compoundres *resp, __be32 nfserr,
4536 struct nfsd4_layoutreturn *lrp)
4537{
4538 struct xdr_stream *xdr = &resp->xdr;
4539 __be32 *p;
4540
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004541 p = xdr_reserve_space(xdr, 4);
4542 if (!p)
4543 return nfserr_resource;
4544 *p++ = cpu_to_be32(lrp->lrs_present);
4545 if (lrp->lrs_present)
Kinglong Mee376675d2015-03-22 22:17:10 +08004546 return nfsd4_encode_stateid(xdr, &lrp->lr_sid);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004547 return 0;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004548}
4549#endif /* CONFIG_NFSD_PNFS */
4550
Andy Adamson2db134e2009-04-03 08:27:55 +03004551static __be32
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004552nfsd42_encode_write_res(struct nfsd4_compoundres *resp,
4553 struct nfsd42_write_res *write, bool sync)
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004554{
4555 __be32 *p;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004556 p = xdr_reserve_space(&resp->xdr, 4);
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004557 if (!p)
4558 return nfserr_resource;
4559
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004560 if (sync)
4561 *p++ = cpu_to_be32(0);
4562 else {
4563 __be32 nfserr;
4564 *p++ = cpu_to_be32(1);
4565 nfserr = nfsd4_encode_stateid(&resp->xdr, &write->cb_stateid);
4566 if (nfserr)
4567 return nfserr;
4568 }
4569 p = xdr_reserve_space(&resp->xdr, 8 + 4 + NFS4_VERIFIER_SIZE);
4570 if (!p)
4571 return nfserr_resource;
4572
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004573 p = xdr_encode_hyper(p, write->wr_bytes_written);
4574 *p++ = cpu_to_be32(write->wr_stable_how);
4575 p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
4576 NFS4_VERIFIER_SIZE);
4577 return nfs_ok;
4578}
4579
4580static __be32
Olga Kornievskaia51911862019-08-08 11:14:59 -04004581nfsd42_encode_nl4_server(struct nfsd4_compoundres *resp, struct nl4_server *ns)
4582{
4583 struct xdr_stream *xdr = &resp->xdr;
4584 struct nfs42_netaddr *addr;
4585 __be32 *p;
4586
4587 p = xdr_reserve_space(xdr, 4);
4588 *p++ = cpu_to_be32(ns->nl4_type);
4589
4590 switch (ns->nl4_type) {
4591 case NL4_NETADDR:
4592 addr = &ns->u.nl4_addr;
4593
4594 /* netid_len, netid, uaddr_len, uaddr (port included
4595 * in RPCBIND_MAXUADDRLEN)
4596 */
4597 p = xdr_reserve_space(xdr,
4598 4 /* netid len */ +
4599 (XDR_QUADLEN(addr->netid_len) * 4) +
4600 4 /* uaddr len */ +
4601 (XDR_QUADLEN(addr->addr_len) * 4));
4602 if (!p)
4603 return nfserr_resource;
4604
4605 *p++ = cpu_to_be32(addr->netid_len);
4606 p = xdr_encode_opaque_fixed(p, addr->netid,
4607 addr->netid_len);
4608 *p++ = cpu_to_be32(addr->addr_len);
4609 p = xdr_encode_opaque_fixed(p, addr->addr,
4610 addr->addr_len);
4611 break;
4612 default:
4613 WARN_ON_ONCE(ns->nl4_type != NL4_NETADDR);
4614 return nfserr_inval;
4615 }
4616
4617 return 0;
4618}
4619
4620static __be32
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004621nfsd4_encode_copy(struct nfsd4_compoundres *resp, __be32 nfserr,
4622 struct nfsd4_copy *copy)
4623{
4624 __be32 *p;
4625
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004626 nfserr = nfsd42_encode_write_res(resp, &copy->cp_res,
4627 copy->cp_synchronous);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004628 if (nfserr)
4629 return nfserr;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004630
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004631 p = xdr_reserve_space(&resp->xdr, 4 + 4);
J. Bruce Fieldsedcc8452018-03-07 15:37:35 -05004632 *p++ = xdr_one; /* cr_consecutive */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004633 *p++ = cpu_to_be32(copy->cp_synchronous);
4634 return 0;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004635}
4636
4637static __be32
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04004638nfsd4_encode_offload_status(struct nfsd4_compoundres *resp, __be32 nfserr,
4639 struct nfsd4_offload_status *os)
4640{
4641 struct xdr_stream *xdr = &resp->xdr;
4642 __be32 *p;
4643
4644 p = xdr_reserve_space(xdr, 8 + 4);
4645 if (!p)
4646 return nfserr_resource;
4647 p = xdr_encode_hyper(p, os->count);
4648 *p++ = cpu_to_be32(0);
Anna Schumaker528b8492020-09-28 13:08:58 -04004649 return nfserr;
4650}
4651
4652static __be32
4653nfsd4_encode_read_plus_data(struct nfsd4_compoundres *resp,
4654 struct nfsd4_read *read,
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004655 unsigned long *maxcount, u32 *eof,
4656 loff_t *pos)
Anna Schumaker528b8492020-09-28 13:08:58 -04004657{
4658 struct xdr_stream *xdr = &resp->xdr;
4659 struct file *file = read->rd_nf->nf_file;
4660 int starting_len = xdr->buf->len;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004661 loff_t hole_pos;
Anna Schumaker528b8492020-09-28 13:08:58 -04004662 __be32 nfserr;
4663 __be32 *p, tmp;
4664 __be64 tmp64;
4665
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004666 hole_pos = pos ? *pos : vfs_llseek(file, read->rd_offset, SEEK_HOLE);
Anna Schumaker2db27992020-09-28 13:08:59 -04004667 if (hole_pos > read->rd_offset)
Anna Schumaker278765e2020-09-28 13:09:00 -04004668 *maxcount = min_t(unsigned long, *maxcount, hole_pos - read->rd_offset);
4669 *maxcount = min_t(unsigned long, *maxcount, (xdr->buf->buflen - xdr->buf->len));
Anna Schumaker528b8492020-09-28 13:08:58 -04004670
4671 /* Content type, offset, byte count */
4672 p = xdr_reserve_space(xdr, 4 + 8 + 4);
4673 if (!p)
4674 return nfserr_resource;
4675
Anna Schumaker278765e2020-09-28 13:09:00 -04004676 read->rd_vlen = xdr_reserve_space_vec(xdr, resp->rqstp->rq_vec, *maxcount);
Anna Schumaker528b8492020-09-28 13:08:58 -04004677 if (read->rd_vlen < 0)
4678 return nfserr_resource;
4679
4680 nfserr = nfsd_readv(resp->rqstp, read->rd_fhp, file, read->rd_offset,
Anna Schumaker278765e2020-09-28 13:09:00 -04004681 resp->rqstp->rq_vec, read->rd_vlen, maxcount, eof);
Anna Schumaker528b8492020-09-28 13:08:58 -04004682 if (nfserr)
4683 return nfserr;
4684
4685 tmp = htonl(NFS4_CONTENT_DATA);
4686 write_bytes_to_xdr_buf(xdr->buf, starting_len, &tmp, 4);
4687 tmp64 = cpu_to_be64(read->rd_offset);
4688 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp64, 8);
Anna Schumaker278765e2020-09-28 13:09:00 -04004689 tmp = htonl(*maxcount);
Anna Schumaker528b8492020-09-28 13:08:58 -04004690 write_bytes_to_xdr_buf(xdr->buf, starting_len + 12, &tmp, 4);
4691 return nfs_ok;
4692}
4693
4694static __be32
Anna Schumaker2db27992020-09-28 13:08:59 -04004695nfsd4_encode_read_plus_hole(struct nfsd4_compoundres *resp,
4696 struct nfsd4_read *read,
Anna Schumaker278765e2020-09-28 13:09:00 -04004697 unsigned long *maxcount, u32 *eof)
Anna Schumaker2db27992020-09-28 13:08:59 -04004698{
4699 struct file *file = read->rd_nf->nf_file;
Anna Schumaker278765e2020-09-28 13:09:00 -04004700 loff_t data_pos = vfs_llseek(file, read->rd_offset, SEEK_DATA);
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004701 loff_t f_size = i_size_read(file_inode(file));
Anna Schumaker278765e2020-09-28 13:09:00 -04004702 unsigned long count;
Anna Schumaker2db27992020-09-28 13:08:59 -04004703 __be32 *p;
4704
Anna Schumaker278765e2020-09-28 13:09:00 -04004705 if (data_pos == -ENXIO)
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004706 data_pos = f_size;
4707 else if (data_pos <= read->rd_offset || (data_pos < f_size && data_pos % PAGE_SIZE))
4708 return nfsd4_encode_read_plus_data(resp, read, maxcount, eof, &f_size);
Anna Schumaker278765e2020-09-28 13:09:00 -04004709 count = data_pos - read->rd_offset;
4710
Anna Schumaker2db27992020-09-28 13:08:59 -04004711 /* Content type, offset, byte count */
4712 p = xdr_reserve_space(&resp->xdr, 4 + 8 + 8);
4713 if (!p)
4714 return nfserr_resource;
4715
4716 *p++ = htonl(NFS4_CONTENT_HOLE);
4717 p = xdr_encode_hyper(p, read->rd_offset);
Anna Schumaker278765e2020-09-28 13:09:00 -04004718 p = xdr_encode_hyper(p, count);
Anna Schumaker2db27992020-09-28 13:08:59 -04004719
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004720 *eof = (read->rd_offset + count) >= f_size;
Anna Schumaker278765e2020-09-28 13:09:00 -04004721 *maxcount = min_t(unsigned long, count, *maxcount);
Anna Schumaker2db27992020-09-28 13:08:59 -04004722 return nfs_ok;
4723}
4724
4725static __be32
Anna Schumaker528b8492020-09-28 13:08:58 -04004726nfsd4_encode_read_plus(struct nfsd4_compoundres *resp, __be32 nfserr,
4727 struct nfsd4_read *read)
4728{
Anna Schumaker278765e2020-09-28 13:09:00 -04004729 unsigned long maxcount, count;
Anna Schumaker528b8492020-09-28 13:08:58 -04004730 struct xdr_stream *xdr = &resp->xdr;
4731 struct file *file;
4732 int starting_len = xdr->buf->len;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004733 int last_segment = xdr->buf->len;
Anna Schumaker528b8492020-09-28 13:08:58 -04004734 int segments = 0;
4735 __be32 *p, tmp;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004736 bool is_data;
Anna Schumaker2db27992020-09-28 13:08:59 -04004737 loff_t pos;
Anna Schumaker528b8492020-09-28 13:08:58 -04004738 u32 eof;
4739
4740 if (nfserr)
4741 return nfserr;
4742 file = read->rd_nf->nf_file;
4743
4744 /* eof flag, segment count */
4745 p = xdr_reserve_space(xdr, 4 + 4);
4746 if (!p)
4747 return nfserr_resource;
4748 xdr_commit_encode(xdr);
4749
4750 maxcount = svc_max_payload(resp->rqstp);
4751 maxcount = min_t(unsigned long, maxcount,
4752 (xdr->buf->buflen - xdr->buf->len));
4753 maxcount = min_t(unsigned long, maxcount, read->rd_length);
Anna Schumaker278765e2020-09-28 13:09:00 -04004754 count = maxcount;
Anna Schumaker528b8492020-09-28 13:08:58 -04004755
4756 eof = read->rd_offset >= i_size_read(file_inode(file));
Anna Schumaker2db27992020-09-28 13:08:59 -04004757 if (eof)
4758 goto out;
4759
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004760 pos = vfs_llseek(file, read->rd_offset, SEEK_HOLE);
4761 is_data = pos > read->rd_offset;
Anna Schumaker2db27992020-09-28 13:08:59 -04004762
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004763 while (count > 0 && !eof) {
Anna Schumaker278765e2020-09-28 13:09:00 -04004764 maxcount = count;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004765 if (is_data)
4766 nfserr = nfsd4_encode_read_plus_data(resp, read, &maxcount, &eof,
4767 segments == 0 ? &pos : NULL);
4768 else
4769 nfserr = nfsd4_encode_read_plus_hole(resp, read, &maxcount, &eof);
Anna Schumaker278765e2020-09-28 13:09:00 -04004770 if (nfserr)
4771 goto out;
4772 count -= maxcount;
4773 read->rd_offset += maxcount;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004774 is_data = !is_data;
4775 last_segment = xdr->buf->len;
Anna Schumaker528b8492020-09-28 13:08:58 -04004776 segments++;
4777 }
4778
Anna Schumaker2db27992020-09-28 13:08:59 -04004779out:
Anna Schumaker278765e2020-09-28 13:09:00 -04004780 if (nfserr && segments == 0)
Anna Schumaker528b8492020-09-28 13:08:58 -04004781 xdr_truncate_encode(xdr, starting_len);
4782 else {
4783 tmp = htonl(eof);
4784 write_bytes_to_xdr_buf(xdr->buf, starting_len, &tmp, 4);
4785 tmp = htonl(segments);
4786 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp, 4);
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004787 if (nfserr) {
4788 xdr_truncate_encode(xdr, last_segment);
4789 nfserr = nfs_ok;
4790 }
Anna Schumaker528b8492020-09-28 13:08:58 -04004791 }
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04004792
4793 return nfserr;
4794}
4795
4796static __be32
Olga Kornievskaia51911862019-08-08 11:14:59 -04004797nfsd4_encode_copy_notify(struct nfsd4_compoundres *resp, __be32 nfserr,
4798 struct nfsd4_copy_notify *cn)
4799{
4800 struct xdr_stream *xdr = &resp->xdr;
4801 __be32 *p;
4802
4803 if (nfserr)
4804 return nfserr;
4805
4806 /* 8 sec, 4 nsec */
4807 p = xdr_reserve_space(xdr, 12);
4808 if (!p)
4809 return nfserr_resource;
4810
4811 /* cnr_lease_time */
4812 p = xdr_encode_hyper(p, cn->cpn_sec);
4813 *p++ = cpu_to_be32(cn->cpn_nsec);
4814
4815 /* cnr_stateid */
4816 nfserr = nfsd4_encode_stateid(xdr, &cn->cpn_cnr_stateid);
4817 if (nfserr)
4818 return nfserr;
4819
4820 /* cnr_src.nl_nsvr */
4821 p = xdr_reserve_space(xdr, 4);
4822 if (!p)
4823 return nfserr_resource;
4824
4825 *p++ = cpu_to_be32(1);
4826
4827 return nfsd42_encode_nl4_server(resp, &cn->cpn_src);
4828}
4829
4830static __be32
Anna Schumaker24bab492014-09-26 13:58:27 -04004831nfsd4_encode_seek(struct nfsd4_compoundres *resp, __be32 nfserr,
4832 struct nfsd4_seek *seek)
4833{
4834 __be32 *p;
4835
Anna Schumaker24bab492014-09-26 13:58:27 -04004836 p = xdr_reserve_space(&resp->xdr, 4 + 8);
4837 *p++ = cpu_to_be32(seek->seek_eof);
4838 p = xdr_encode_hyper(p, seek->seek_pos);
4839
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004840 return 0;
Anna Schumaker24bab492014-09-26 13:58:27 -04004841}
4842
4843static __be32
Benny Halevy695e12f2008-07-04 14:38:33 +03004844nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
4845{
4846 return nfserr;
4847}
4848
Frank van der Linden23e50fe2020-06-23 22:39:26 +00004849/*
4850 * Encode kmalloc-ed buffer in to XDR stream.
4851 */
Chuck Leverb9a49232020-09-11 14:47:53 -04004852static __be32
Frank van der Linden23e50fe2020-06-23 22:39:26 +00004853nfsd4_vbuf_to_stream(struct xdr_stream *xdr, char *buf, u32 buflen)
4854{
4855 u32 cplen;
4856 __be32 *p;
4857
4858 cplen = min_t(unsigned long, buflen,
4859 ((void *)xdr->end - (void *)xdr->p));
4860 p = xdr_reserve_space(xdr, cplen);
4861 if (!p)
4862 return nfserr_resource;
4863
4864 memcpy(p, buf, cplen);
4865 buf += cplen;
4866 buflen -= cplen;
4867
4868 while (buflen) {
4869 cplen = min_t(u32, buflen, PAGE_SIZE);
4870 p = xdr_reserve_space(xdr, cplen);
4871 if (!p)
4872 return nfserr_resource;
4873
4874 memcpy(p, buf, cplen);
4875
4876 if (cplen < PAGE_SIZE) {
4877 /*
4878 * We're done, with a length that wasn't page
4879 * aligned, so possibly not word aligned. Pad
4880 * any trailing bytes with 0.
4881 */
4882 xdr_encode_opaque_fixed(p, NULL, cplen);
4883 break;
4884 }
4885
4886 buflen -= PAGE_SIZE;
4887 buf += PAGE_SIZE;
4888 }
4889
4890 return 0;
4891}
4892
4893static __be32
4894nfsd4_encode_getxattr(struct nfsd4_compoundres *resp, __be32 nfserr,
4895 struct nfsd4_getxattr *getxattr)
4896{
4897 struct xdr_stream *xdr = &resp->xdr;
4898 __be32 *p, err;
4899
4900 p = xdr_reserve_space(xdr, 4);
4901 if (!p)
4902 return nfserr_resource;
4903
4904 *p = cpu_to_be32(getxattr->getxa_len);
4905
4906 if (getxattr->getxa_len == 0)
4907 return 0;
4908
4909 err = nfsd4_vbuf_to_stream(xdr, getxattr->getxa_buf,
4910 getxattr->getxa_len);
4911
4912 kvfree(getxattr->getxa_buf);
4913
4914 return err;
4915}
4916
4917static __be32
4918nfsd4_encode_setxattr(struct nfsd4_compoundres *resp, __be32 nfserr,
4919 struct nfsd4_setxattr *setxattr)
4920{
4921 struct xdr_stream *xdr = &resp->xdr;
4922 __be32 *p;
4923
4924 p = xdr_reserve_space(xdr, 20);
4925 if (!p)
4926 return nfserr_resource;
4927
4928 encode_cinfo(p, &setxattr->setxa_cinfo);
4929
4930 return 0;
4931}
4932
4933/*
4934 * See if there are cookie values that can be rejected outright.
4935 */
4936static __be32
4937nfsd4_listxattr_validate_cookie(struct nfsd4_listxattrs *listxattrs,
4938 u32 *offsetp)
4939{
4940 u64 cookie = listxattrs->lsxa_cookie;
4941
4942 /*
4943 * If the cookie is larger than the maximum number we can fit
4944 * in either the buffer we just got back from vfs_listxattr, or,
4945 * XDR-encoded, in the return buffer, it's invalid.
4946 */
4947 if (cookie > (listxattrs->lsxa_len) / (XATTR_USER_PREFIX_LEN + 2))
4948 return nfserr_badcookie;
4949
4950 if (cookie > (listxattrs->lsxa_maxcount /
4951 (XDR_QUADLEN(XATTR_USER_PREFIX_LEN + 2) + 4)))
4952 return nfserr_badcookie;
4953
4954 *offsetp = (u32)cookie;
4955 return 0;
4956}
4957
4958static __be32
4959nfsd4_encode_listxattrs(struct nfsd4_compoundres *resp, __be32 nfserr,
4960 struct nfsd4_listxattrs *listxattrs)
4961{
4962 struct xdr_stream *xdr = &resp->xdr;
4963 u32 cookie_offset, count_offset, eof;
4964 u32 left, xdrleft, slen, count;
4965 u32 xdrlen, offset;
4966 u64 cookie;
4967 char *sp;
Chuck Leverb9a49232020-09-11 14:47:53 -04004968 __be32 status, tmp;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00004969 __be32 *p;
4970 u32 nuser;
4971
4972 eof = 1;
4973
4974 status = nfsd4_listxattr_validate_cookie(listxattrs, &offset);
4975 if (status)
4976 goto out;
4977
4978 /*
4979 * Reserve space for the cookie and the name array count. Record
4980 * the offsets to save them later.
4981 */
4982 cookie_offset = xdr->buf->len;
4983 count_offset = cookie_offset + 8;
4984 p = xdr_reserve_space(xdr, 12);
4985 if (!p) {
4986 status = nfserr_resource;
4987 goto out;
4988 }
4989
4990 count = 0;
4991 left = listxattrs->lsxa_len;
4992 sp = listxattrs->lsxa_buf;
4993 nuser = 0;
4994
4995 xdrleft = listxattrs->lsxa_maxcount;
4996
4997 while (left > 0 && xdrleft > 0) {
4998 slen = strlen(sp);
4999
5000 /*
Alex Dewar4cce11fa2020-08-17 18:51:26 +01005001 * Check if this is a "user." attribute, skip it if not.
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005002 */
5003 if (strncmp(sp, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
5004 goto contloop;
5005
5006 slen -= XATTR_USER_PREFIX_LEN;
5007 xdrlen = 4 + ((slen + 3) & ~3);
5008 if (xdrlen > xdrleft) {
5009 if (count == 0) {
5010 /*
5011 * Can't even fit the first attribute name.
5012 */
5013 status = nfserr_toosmall;
5014 goto out;
5015 }
5016 eof = 0;
5017 goto wreof;
5018 }
5019
5020 left -= XATTR_USER_PREFIX_LEN;
5021 sp += XATTR_USER_PREFIX_LEN;
5022 if (nuser++ < offset)
5023 goto contloop;
5024
5025
5026 p = xdr_reserve_space(xdr, xdrlen);
5027 if (!p) {
5028 status = nfserr_resource;
5029 goto out;
5030 }
5031
Alex Deware2a18402020-08-12 15:12:51 +01005032 xdr_encode_opaque(p, sp, slen);
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005033
5034 xdrleft -= xdrlen;
5035 count++;
5036contloop:
5037 sp += slen + 1;
5038 left -= slen + 1;
5039 }
5040
5041 /*
5042 * If there were user attributes to copy, but we didn't copy
5043 * any, the offset was too large (e.g. the cookie was invalid).
5044 */
5045 if (nuser > 0 && count == 0) {
5046 status = nfserr_badcookie;
5047 goto out;
5048 }
5049
5050wreof:
5051 p = xdr_reserve_space(xdr, 4);
5052 if (!p) {
5053 status = nfserr_resource;
5054 goto out;
5055 }
5056 *p = cpu_to_be32(eof);
5057
5058 cookie = offset + count;
5059
5060 write_bytes_to_xdr_buf(xdr->buf, cookie_offset, &cookie, 8);
Chuck Leverb9a49232020-09-11 14:47:53 -04005061 tmp = cpu_to_be32(count);
5062 write_bytes_to_xdr_buf(xdr->buf, count_offset, &tmp, 4);
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005063out:
5064 if (listxattrs->lsxa_len)
5065 kvfree(listxattrs->lsxa_buf);
5066 return status;
5067}
5068
5069static __be32
5070nfsd4_encode_removexattr(struct nfsd4_compoundres *resp, __be32 nfserr,
5071 struct nfsd4_removexattr *removexattr)
5072{
5073 struct xdr_stream *xdr = &resp->xdr;
5074 __be32 *p;
5075
5076 p = xdr_reserve_space(xdr, 20);
5077 if (!p)
5078 return nfserr_resource;
5079
5080 p = encode_cinfo(p, &removexattr->rmxa_cinfo);
5081 return 0;
5082}
5083
Benny Halevy695e12f2008-07-04 14:38:33 +03005084typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
5085
Andy Adamson2db134e2009-04-03 08:27:55 +03005086/*
5087 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
5088 * since we don't need to filter out obsolete ops as this is
5089 * done in the decoding phase.
5090 */
Chuck Leverc1df6092017-08-01 11:59:58 -04005091static const nfsd4_enc nfsd4_enc_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04005092 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
5093 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
5094 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
5095 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
5096 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
5097 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
5098 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
5099 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
5100 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
5101 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
5102 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
5103 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
5104 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
5105 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
5106 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
5107 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
Benny Halevy84f09f42009-03-04 23:05:35 +02005108 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04005109 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
5110 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
5111 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
5112 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
5113 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
5114 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
5115 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
5116 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
5117 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
5118 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
5119 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
5120 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
5121 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
5122 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
5123 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
5124 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
5125 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
5126 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
5127 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
5128 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03005129
5130 /* NFSv4.1 operations */
5131 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04005132 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
Andy Adamson2db134e2009-04-03 08:27:55 +03005133 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
5134 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
Kinglong Mee43212cc2013-12-09 19:04:23 +08005135 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_noop,
5136 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03005137 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02005138#ifdef CONFIG_NFSD_PNFS
5139 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_getdeviceinfo,
5140 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
5141 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_layoutcommit,
5142 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_layoutget,
5143 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_layoutreturn,
5144#else
Andy Adamson2db134e2009-04-03 08:27:55 +03005145 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
5146 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
5147 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
5148 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
5149 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02005150#endif
Mi Jinlong22b6dee2010-12-27 14:29:57 +08005151 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
Andy Adamson2db134e2009-04-03 08:27:55 +03005152 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
5153 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
Bryan Schumaker17456802011-07-13 10:50:48 -04005154 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
Andy Adamson2db134e2009-04-03 08:27:55 +03005155 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
5156 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
5157 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
Anna Schumaker87a15a82014-09-26 13:58:26 -04005158
5159 /* NFSv4.2 operations */
5160 [OP_ALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
Anna Schumaker29ae7f92016-09-07 15:57:30 -04005161 [OP_COPY] = (nfsd4_enc)nfsd4_encode_copy,
Olga Kornievskaia51911862019-08-08 11:14:59 -04005162 [OP_COPY_NOTIFY] = (nfsd4_enc)nfsd4_encode_copy_notify,
Anna Schumaker87a15a82014-09-26 13:58:26 -04005163 [OP_DEALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
5164 [OP_IO_ADVISE] = (nfsd4_enc)nfsd4_encode_noop,
5165 [OP_LAYOUTERROR] = (nfsd4_enc)nfsd4_encode_noop,
5166 [OP_LAYOUTSTATS] = (nfsd4_enc)nfsd4_encode_noop,
5167 [OP_OFFLOAD_CANCEL] = (nfsd4_enc)nfsd4_encode_noop,
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04005168 [OP_OFFLOAD_STATUS] = (nfsd4_enc)nfsd4_encode_offload_status,
Anna Schumaker528b8492020-09-28 13:08:58 -04005169 [OP_READ_PLUS] = (nfsd4_enc)nfsd4_encode_read_plus,
Anna Schumaker24bab492014-09-26 13:58:27 -04005170 [OP_SEEK] = (nfsd4_enc)nfsd4_encode_seek,
Anna Schumaker87a15a82014-09-26 13:58:26 -04005171 [OP_WRITE_SAME] = (nfsd4_enc)nfsd4_encode_noop,
Christoph Hellwigffa01602015-12-03 12:59:52 +01005172 [OP_CLONE] = (nfsd4_enc)nfsd4_encode_noop,
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005173
5174 /* RFC 8276 extended atributes operations */
5175 [OP_GETXATTR] = (nfsd4_enc)nfsd4_encode_getxattr,
5176 [OP_SETXATTR] = (nfsd4_enc)nfsd4_encode_setxattr,
5177 [OP_LISTXATTRS] = (nfsd4_enc)nfsd4_encode_listxattrs,
5178 [OP_REMOVEXATTR] = (nfsd4_enc)nfsd4_encode_removexattr,
Benny Halevy695e12f2008-07-04 14:38:33 +03005179};
5180
Andy Adamson496c2622009-04-03 08:28:48 +03005181/*
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04005182 * Calculate whether we still have space to encode repsize bytes.
5183 * There are two considerations:
5184 * - For NFS versions >=4.1, the size of the reply must stay within
5185 * session limits
5186 * - For all NFS versions, we must stay within limited preallocated
5187 * buffer space.
Andy Adamson496c2622009-04-03 08:28:48 +03005188 *
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04005189 * This is called before the operation is processed, so can only provide
5190 * an upper estimate. For some nonidempotent operations (such as
5191 * getattr), it's not necessarily a problem if that estimate is wrong,
5192 * as we can fail it after processing without significant side effects.
Andy Adamson496c2622009-04-03 08:28:48 +03005193 */
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04005194__be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 respsize)
Andy Adamson496c2622009-04-03 08:28:48 +03005195{
J. Bruce Fields67492c92014-03-08 16:42:05 -05005196 struct xdr_buf *buf = &resp->rqstp->rq_res;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04005197 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson496c2622009-04-03 08:28:48 +03005198
J. Bruce Fields47ee5292014-03-12 21:39:35 -04005199 if (buf->len + respsize <= buf->buflen)
5200 return nfs_ok;
5201 if (!nfsd4_has_session(&resp->cstate))
J. Bruce Fieldsea8d7722014-03-08 17:19:55 -05005202 return nfserr_resource;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04005203 if (slot->sl_flags & NFSD4_SLOT_CACHETHIS) {
5204 WARN_ON_ONCE(1);
5205 return nfserr_rep_too_big_to_cache;
J. Bruce Fieldsea8d7722014-03-08 17:19:55 -05005206 }
J. Bruce Fields47ee5292014-03-12 21:39:35 -04005207 return nfserr_rep_too_big;
Andy Adamson496c2622009-04-03 08:28:48 +03005208}
5209
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210void
5211nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
5212{
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005213 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005214 struct nfs4_stateowner *so = resp->cstate.replay_owner;
J. Bruce Fields5f4ab942014-03-07 11:01:37 -05005215 struct svc_rqst *rqstp = resp->rqstp;
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04005216 const struct nfsd4_operation *opdesc = op->opdesc;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005217 int post_err_offset;
J. Bruce Fields07d1f802014-03-06 20:39:29 -05005218 nfsd4_enc encoder;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07005219 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05005221 p = xdr_reserve_space(xdr, 8);
5222 if (!p) {
5223 WARN_ON_ONCE(1);
5224 return;
5225 }
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04005226 *p++ = cpu_to_be32(op->opnum);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005227 post_err_offset = xdr->buf->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228
Benny Halevy695e12f2008-07-04 14:38:33 +03005229 if (op->opnum == OP_ILLEGAL)
5230 goto status;
J. Bruce Fieldsb7571e42017-05-06 10:49:21 -04005231 if (op->status && opdesc &&
5232 !(opdesc->op_flags & OP_NONTRIVIAL_ERROR_ENCODE))
5233 goto status;
Benny Halevy695e12f2008-07-04 14:38:33 +03005234 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
5235 !nfsd4_enc_ops[op->opnum]);
J. Bruce Fields07d1f802014-03-06 20:39:29 -05005236 encoder = nfsd4_enc_ops[op->opnum];
5237 op->status = encoder(resp, op->status, &op->u);
Chuck Lever08281342020-11-21 11:36:42 -05005238 if (op->status)
5239 trace_nfsd_compound_encode_err(rqstp, op->opnum, op->status);
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04005240 if (opdesc && opdesc->op_release)
5241 opdesc->op_release(&op->u);
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04005242 xdr_commit_encode(xdr);
5243
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04005244 /* nfsd4_check_resp_size guarantees enough room for error status */
J. Bruce Fields5f4ab942014-03-07 11:01:37 -05005245 if (!op->status) {
5246 int space_needed = 0;
5247 if (!nfsd4_last_compound_op(rqstp))
5248 space_needed = COMPOUND_ERR_SLACK_SPACE;
5249 op->status = nfsd4_check_resp_size(resp, space_needed);
5250 }
J. Bruce Fieldsc8f13d92014-05-08 17:38:18 -04005251 if (op->status == nfserr_resource && nfsd4_has_session(&resp->cstate)) {
5252 struct nfsd4_slot *slot = resp->cstate.slot;
5253
5254 if (slot->sl_flags & NFSD4_SLOT_CACHETHIS)
5255 op->status = nfserr_rep_too_big_to_cache;
5256 else
5257 op->status = nfserr_rep_too_big;
5258 }
J. Bruce Fields07d1f802014-03-06 20:39:29 -05005259 if (op->status == nfserr_resource ||
5260 op->status == nfserr_rep_too_big ||
5261 op->status == nfserr_rep_too_big_to_cache) {
5262 /*
5263 * The operation may have already been encoded or
5264 * partially encoded. No op returns anything additional
5265 * in the case of one of these three errors, so we can
5266 * just truncate back to after the status. But it's a
5267 * bug if we had to do this on a non-idempotent op:
5268 */
5269 warn_on_nonidempotent_op(op);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005270 xdr_truncate_encode(xdr, post_err_offset);
J. Bruce Fields07d1f802014-03-06 20:39:29 -05005271 }
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005272 if (so) {
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005273 int len = xdr->buf->len - post_err_offset;
5274
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005275 so->so_replay.rp_status = op->status;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005276 so->so_replay.rp_buflen = len;
5277 read_bytes_from_xdr_buf(xdr->buf, post_err_offset,
5278 so->so_replay.rp_buf, len);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005279 }
Benny Halevy695e12f2008-07-04 14:38:33 +03005280status:
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005281 /* Note that op->status is already in network byte order: */
5282 write_bytes_to_xdr_buf(xdr->buf, post_err_offset - 4, &op->status, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283}
5284
5285/*
5286 * Encode the reply stored in the stateowner reply cache
5287 *
5288 * XDR note: do not encode rp->rp_buflen: the buffer contains the
5289 * previously sent already encoded operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 */
5291void
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05005292nfsd4_encode_replay(struct xdr_stream *xdr, struct nfsd4_op *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07005294 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 struct nfs4_replay *rp = op->replay;
5296
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05005297 p = xdr_reserve_space(xdr, 8 + rp->rp_buflen);
5298 if (!p) {
5299 WARN_ON_ONCE(1);
5300 return;
5301 }
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04005302 *p++ = cpu_to_be32(op->opnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 *p++ = rp->rp_status; /* already xdr'ed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04005305 p = xdr_encode_opaque_fixed(p, rp->rp_buf, rp->rp_buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306}
5307
Christoph Hellwig85374882017-05-08 18:48:24 +02005308void nfsd4_release_compoundargs(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309{
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04005310 struct nfsd4_compoundargs *args = rqstp->rq_argp;
5311
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 if (args->ops != args->iops) {
5313 kfree(args->ops);
5314 args->ops = args->iops;
5315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 while (args->to_free) {
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -04005317 struct svcxdr_tmpbuf *tb = args->to_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 args->to_free = tb->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 kfree(tb);
5320 }
5321}
5322
5323int
Christoph Hellwig026fec72017-05-08 19:01:48 +02005324nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325{
Christoph Hellwig026fec72017-05-08 19:01:48 +02005326 struct nfsd4_compoundargs *args = rqstp->rq_argp;
5327
Chuck Leverc1346a122020-11-03 11:54:23 -05005328 /* svcxdr_tmp_alloc */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 args->tmpp = NULL;
5330 args->to_free = NULL;
Chuck Leverc1346a122020-11-03 11:54:23 -05005331
5332 args->xdr = &rqstp->rq_arg_stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 args->ops = args->iops;
5334 args->rqstp = rqstp;
5335
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04005336 return !nfsd4_decode_compound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337}
5338
5339int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02005340nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02005342 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields6ac90392014-02-26 17:39:35 -05005343 struct xdr_buf *buf = resp->xdr.buf;
5344
5345 WARN_ON_ONCE(buf->len != buf->head[0].iov_len + buf->page_len +
5346 buf->tail[0].iov_len);
J. Bruce Fieldsdd97fdd2014-02-26 17:16:27 -05005347
Chuck Levercc028a12020-10-02 15:52:44 -04005348 *p = resp->cstate.status;
5349
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04005350 rqstp->rq_next_page = resp->xdr.page_ptr + 1;
5351
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 p = resp->tagp;
5353 *p++ = htonl(resp->taglen);
5354 memcpy(p, resp->tag, resp->taglen);
5355 p += XDR_QUADLEN(resp->taglen);
5356 *p++ = htonl(resp->opcnt);
5357
Trond Myklebustb6076642014-06-30 11:48:35 -04005358 nfsd4_sequence_done(resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 return 1;
5360}
5361
5362/*
5363 * Local variables:
5364 * c-basic-offset: 8
5365 * End:
5366 */