blob: 5f7917014778a27a6ee87aa08e38e80850c4248f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Server-side XDR for NFSv4
3 *
4 * Copyright (c) 2002 The Regents of the University of Michigan.
5 * All rights reserved.
6 *
7 * Kendrick Smith <kmsmith@umich.edu>
8 * Andy Adamson <andros@umich.edu>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 */
35
Christoph Hellwig96bcad52015-06-18 16:45:01 +020036#include <linux/file.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/namei.h>
Boaz Harrosh341eb182009-12-03 20:29:12 +020039#include <linux/statfs.h>
Andy Adamson0733d212009-04-03 08:28:01 +030040#include <linux/utsname.h>
Bryan Schumaker17456802011-07-13 10:50:48 -040041#include <linux/pagemap.h>
J. Bruce Fields4796f452007-07-17 04:04:51 -070042#include <linux/sunrpc/svcauth_gss.h>
Olga Kornievskaia84e1b212019-09-13 14:00:57 -040043#include <linux/sunrpc/addr.h>
Frank van der Linden23e50fe2020-06-23 22:39:26 +000044#include <linux/xattr.h>
45#include <uapi/linux/xattr.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020046
J. Bruce Fields2ca72e12011-01-04 17:37:15 -050047#include "idmap.h"
48#include "acl.h"
Boaz Harrosh9a74af22009-12-03 20:30:56 +020049#include "xdr4.h"
J. Bruce Fields0a3adad2009-11-04 18:12:35 -050050#include "vfs.h"
Bryan Schumaker17456802011-07-13 10:50:48 -040051#include "state.h"
J. Bruce Fields10910062011-01-24 12:11:02 -050052#include "cache.h"
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +030053#include "netns.h"
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020054#include "pnfs.h"
Jeff Layton5c4583b2019-08-18 14:18:54 -040055#include "filecache.h"
J. Bruce Fields2ca72e12011-01-04 17:37:15 -050056
Chuck Lever08281342020-11-21 11:36:42 -050057#include "trace.h"
58
David Quigley18032ca2013-05-02 13:19:10 -040059#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
60#include <linux/security.h>
61#endif
62
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#define NFSDDBG_FACILITY NFSDDBG_XDR
65
J. Bruce Fields5cf23db2017-01-11 20:40:36 -050066const u32 nfsd_suppattrs[3][3] = {
J. Bruce Fields916d2d82016-10-18 14:18:40 -040067 {NFSD4_SUPPORTED_ATTRS_WORD0,
68 NFSD4_SUPPORTED_ATTRS_WORD1,
69 NFSD4_SUPPORTED_ATTRS_WORD2},
70
71 {NFSD4_1_SUPPORTED_ATTRS_WORD0,
72 NFSD4_1_SUPPORTED_ATTRS_WORD1,
73 NFSD4_1_SUPPORTED_ATTRS_WORD2},
74
75 {NFSD4_1_SUPPORTED_ATTRS_WORD0,
76 NFSD4_1_SUPPORTED_ATTRS_WORD1,
77 NFSD4_2_SUPPORTED_ATTRS_WORD2},
78};
79
J.Bruce Fields42ca0992006-10-04 02:16:20 -070080/*
81 * As per referral draft, the fsid for a referral MUST be different from the fsid of the containing
82 * directory in order to indicate to the client that a filesystem boundary is present
83 * We use a fixed fsid for a referral
84 */
85#define NFS4_REFERRAL_FSID_MAJOR 0x8000000ULL
86#define NFS4_REFERRAL_FSID_MINOR 0x8000000ULL
87
Al Virob37ad282006-10-19 23:28:59 -070088static __be32
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -050089check_filename(char *str, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 int i;
92
93 if (len == 0)
94 return nfserr_inval;
Chuck Lever000dfa12020-11-03 13:24:10 -050095 if (len > NFS4_MAXNAMLEN)
96 return nfserr_nametoolong;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (isdotent(str, len))
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -050098 return nfserr_badname;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 for (i = 0; i < len; i++)
100 if (str[i] == '/')
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -0500101 return nfserr_badname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return 0;
103}
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#define DECODE_HEAD \
Al Viro2ebbc012006-10-19 23:28:58 -0700106 __be32 *p; \
Al Virob37ad282006-10-19 23:28:59 -0700107 __be32 status
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#define DECODE_TAIL \
109 status = 0; \
110out: \
111 return status; \
112xdr_error: \
Chuck Lever817cb9d2007-09-11 18:01:20 -0400113 dprintk("NFSD: xdr error (%s:%d)\n", \
114 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 status = nfserr_bad_xdr; \
116 goto out
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#define READMEM(x,nbytes) do { \
119 x = (char *)p; \
120 p += XDR_QUADLEN(nbytes); \
121} while (0)
122#define SAVEMEM(x,nbytes) do { \
123 if (!(x = (p==argp->tmp || p == argp->tmpp) ? \
124 savemem(argp, p, nbytes) : \
125 (char *)p)) { \
Chuck Lever817cb9d2007-09-11 18:01:20 -0400126 dprintk("NFSD: xdr error (%s:%d)\n", \
127 __FILE__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 goto xdr_error; \
129 } \
130 p += XDR_QUADLEN(nbytes); \
131} while (0)
132#define COPYMEM(x,nbytes) do { \
133 memcpy((x), p, nbytes); \
134 p += XDR_QUADLEN(nbytes); \
135} while (0)
Chuck Leverc1346a122020-11-03 11:54:23 -0500136#define READ_BUF(nbytes) \
137 do { \
138 p = xdr_inline_decode(argp->xdr,\
139 nbytes); \
140 if (!p) \
141 goto xdr_error; \
142 } while (0)
J. Bruce Fields2b86e3a2019-08-28 21:13:45 -0400143
Andy Adamson60adfc52009-04-03 08:28:50 +0300144static int zero_clientid(clientid_t *clid)
145{
146 return (clid->cl_boot == 0) && (clid->cl_id == 0);
147}
148
Christoph Hellwig2d8498d2013-11-20 00:24:11 -0800149/**
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400150 * svcxdr_tmpalloc - allocate memory to be freed after compound processing
J. Bruce Fieldsce043ac2014-06-24 16:51:12 -0400151 * @argp: NFSv4 compound argument structure
Trond Myklebusted992752019-08-18 14:18:59 -0400152 * @len: length of buffer to allocate
Christoph Hellwig2d8498d2013-11-20 00:24:11 -0800153 *
Trond Myklebusted992752019-08-18 14:18:59 -0400154 * Allocates a buffer of size @len to be freed when processing the compound
155 * operation described in @argp finishes.
Christoph Hellwig2d8498d2013-11-20 00:24:11 -0800156 */
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400157static void *
158svcxdr_tmpalloc(struct nfsd4_compoundargs *argp, u32 len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400160 struct svcxdr_tmpbuf *tb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400162 tb = kmalloc(sizeof(*tb) + len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (!tb)
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400164 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 tb->next = argp->to_free;
166 argp->to_free = tb;
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400167 return tb->buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
J. Bruce Fields29c353b2014-06-24 17:06:51 -0400170/*
171 * For xdr strings that need to be passed to other kernel api's
172 * as null-terminated strings.
173 *
174 * Note null-terminating in place usually isn't safe since the
175 * buffer might end on a page boundary.
176 */
177static char *
178svcxdr_dupstr(struct nfsd4_compoundargs *argp, void *buf, u32 len)
179{
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400180 char *p = svcxdr_tmpalloc(argp, len + 1);
J. Bruce Fields29c353b2014-06-24 17:06:51 -0400181
182 if (!p)
183 return NULL;
184 memcpy(p, buf, len);
185 p[len] = '\0';
J. Bruce Fields29c353b2014-06-24 17:06:51 -0400186 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Christoph Hellwig2d8498d2013-11-20 00:24:11 -0800189/**
190 * savemem - duplicate a chunk of memory for later processing
191 * @argp: NFSv4 compound argument structure to be freed with
192 * @p: pointer to be duplicated
193 * @nbytes: length to be duplicated
194 *
195 * Returns a pointer to a copy of @nbytes bytes of memory at @p
196 * that are preserved until processing of the NFSv4 compound
197 * operation described by @argp finishes.
198 */
Al Viro2ebbc012006-10-19 23:28:58 -0700199static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400201 void *ret;
202
203 ret = svcxdr_tmpalloc(argp, nbytes);
204 if (!ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return NULL;
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -0400206 memcpy(ret, p, nbytes);
207 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
Chuck Lever5dcbfab2020-11-04 11:41:55 -0500210
211/*
212 * NFSv4 basic data type decoders
213 */
214
215static __be32
216nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
217{
218 __be32 *p;
219 u32 len;
220
221 if (xdr_stream_decode_u32(argp->xdr, &len) < 0)
222 return nfserr_bad_xdr;
223 if (len == 0 || len > NFS4_OPAQUE_LIMIT)
224 return nfserr_bad_xdr;
225 p = xdr_inline_decode(argp->xdr, len);
226 if (!p)
227 return nfserr_bad_xdr;
228 o->data = svcxdr_tmpalloc(argp, len);
229 if (!o->data)
230 return nfserr_jukebox;
231 o->len = len;
232 memcpy(o->data, p, len);
233
234 return nfs_ok;
235}
236
Christoph Hellwig4c94e132015-01-22 12:09:50 +0100237static __be32
Chuck Lever000dfa12020-11-03 13:24:10 -0500238nfsd4_decode_component4(struct nfsd4_compoundargs *argp, char **namp, u32 *lenp)
239{
240 __be32 *p, status;
241
242 if (xdr_stream_decode_u32(argp->xdr, lenp) < 0)
243 return nfserr_bad_xdr;
244 p = xdr_inline_decode(argp->xdr, *lenp);
245 if (!p)
246 return nfserr_bad_xdr;
247 status = check_filename((char *)p, *lenp);
248 if (status)
249 return status;
250 *namp = svcxdr_tmpalloc(argp, *lenp);
251 if (!*namp)
252 return nfserr_jukebox;
253 memcpy(*namp, p, *lenp);
254
255 return nfs_ok;
256}
257
258static __be32
J. Bruce Fieldsbdba5362019-06-05 14:17:42 -0400259nfsd4_decode_time(struct nfsd4_compoundargs *argp, struct timespec64 *tv)
Christoph Hellwig4c94e132015-01-22 12:09:50 +0100260{
261 DECODE_HEAD;
Christoph Hellwig4c94e132015-01-22 12:09:50 +0100262
263 READ_BUF(12);
J. Bruce Fieldsbdba5362019-06-05 14:17:42 -0400264 p = xdr_decode_hyper(p, &tv->tv_sec);
Christoph Hellwig4c94e132015-01-22 12:09:50 +0100265 tv->tv_nsec = be32_to_cpup(p++);
266 if (tv->tv_nsec >= (u32)1000000000)
267 return nfserr_inval;
268
269 DECODE_TAIL;
270}
271
Al Virob37ad282006-10-19 23:28:59 -0700272static __be32
Chuck Lever1c3eff72020-11-19 14:01:08 -0500273nfsd4_decode_nfstime4(struct nfsd4_compoundargs *argp, struct timespec64 *tv)
274{
275 __be32 *p;
276
277 p = xdr_inline_decode(argp->xdr, XDR_UNIT * 3);
278 if (!p)
279 return nfserr_bad_xdr;
280 p = xdr_decode_hyper(p, &tv->tv_sec);
281 tv->tv_nsec = be32_to_cpup(p++);
282 if (tv->tv_nsec >= (u32)1000000000)
283 return nfserr_inval;
284 return nfs_ok;
285}
286
287static __be32
Chuck Lever796dd1c2020-11-16 17:34:01 -0500288nfsd4_decode_verifier4(struct nfsd4_compoundargs *argp, nfs4_verifier *verf)
289{
290 __be32 *p;
291
292 p = xdr_inline_decode(argp->xdr, NFS4_VERIFIER_SIZE);
293 if (!p)
294 return nfserr_bad_xdr;
295 memcpy(verf->data, p, sizeof(verf->data));
296 return nfs_ok;
297}
298
299static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
301{
302 u32 bmlen;
303 DECODE_HEAD;
304
305 bmval[0] = 0;
306 bmval[1] = 0;
Andy Adamson7e705702009-04-03 08:29:11 +0300307 bmval[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400310 bmlen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (bmlen > 1000)
312 goto xdr_error;
313
314 READ_BUF(bmlen << 2);
315 if (bmlen > 0)
J. Bruce Fields06553992014-06-02 12:32:51 -0400316 bmval[0] = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (bmlen > 1)
J. Bruce Fields06553992014-06-02 12:32:51 -0400318 bmval[1] = be32_to_cpup(p++);
Andy Adamson7e705702009-04-03 08:29:11 +0300319 if (bmlen > 2)
J. Bruce Fields06553992014-06-02 12:32:51 -0400320 bmval[2] = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 DECODE_TAIL;
323}
324
Chuck Leverd1c263a2020-11-03 12:56:05 -0500325/**
326 * nfsd4_decode_bitmap4 - Decode an NFSv4 bitmap4
327 * @argp: NFSv4 compound argument structure
328 * @bmval: pointer to an array of u32's to decode into
329 * @bmlen: size of the @bmval array
330 *
331 * The server needs to return nfs_ok rather than nfserr_bad_xdr when
332 * encountering bitmaps containing bits it does not recognize. This
333 * includes bits in bitmap words past WORDn, where WORDn is the last
334 * bitmap WORD the implementation currently supports. Thus we are
335 * careful here to simply ignore bits in bitmap words that this
336 * implementation has yet to support explicitly.
337 *
338 * Return values:
339 * %nfs_ok: @bmval populated successfully
340 * %nfserr_bad_xdr: the encoded bitmap was invalid
341 */
342static __be32
343nfsd4_decode_bitmap4(struct nfsd4_compoundargs *argp, u32 *bmval, u32 bmlen)
344{
345 u32 i, count;
346 __be32 *p;
347
348 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
349 return nfserr_bad_xdr;
350 /* request sanity */
351 if (count > 1000)
352 return nfserr_bad_xdr;
353 p = xdr_inline_decode(argp->xdr, count << 2);
354 if (!p)
355 return nfserr_bad_xdr;
356 i = 0;
357 while (i < count)
358 bmval[i++] = be32_to_cpup(p++);
359 while (i < bmlen)
360 bmval[i++] = 0;
361
362 return nfs_ok;
363}
364
Al Virob37ad282006-10-19 23:28:59 -0700365static __be32
Chuck Leverc941a962020-11-19 13:02:54 -0500366nfsd4_decode_nfsace4(struct nfsd4_compoundargs *argp, struct nfs4_ace *ace)
367{
368 __be32 *p, status;
369 u32 length;
370
371 if (xdr_stream_decode_u32(argp->xdr, &ace->type) < 0)
372 return nfserr_bad_xdr;
373 if (xdr_stream_decode_u32(argp->xdr, &ace->flag) < 0)
374 return nfserr_bad_xdr;
375 if (xdr_stream_decode_u32(argp->xdr, &ace->access_mask) < 0)
376 return nfserr_bad_xdr;
377
378 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
379 return nfserr_bad_xdr;
380 p = xdr_inline_decode(argp->xdr, length);
381 if (!p)
382 return nfserr_bad_xdr;
383 ace->whotype = nfs4_acl_get_whotype((char *)p, length);
384 if (ace->whotype != NFS4_ACL_WHO_NAMED)
385 status = nfs_ok;
386 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
387 status = nfsd_map_name_to_gid(argp->rqstp,
388 (char *)p, length, &ace->who_gid);
389 else
390 status = nfsd_map_name_to_uid(argp->rqstp,
391 (char *)p, length, &ace->who_uid);
392
393 return status;
394}
395
396/* A counted array of nfsace4's */
397static noinline __be32
398nfsd4_decode_acl(struct nfsd4_compoundargs *argp, struct nfs4_acl **acl)
399{
400 struct nfs4_ace *ace;
401 __be32 status;
402 u32 count;
403
404 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
405 return nfserr_bad_xdr;
406
407 if (count > xdr_stream_remaining(argp->xdr) / 20)
408 /*
409 * Even with 4-byte names there wouldn't be
410 * space for that many aces; something fishy is
411 * going on:
412 */
413 return nfserr_fbig;
414
415 *acl = svcxdr_tmpalloc(argp, nfs4_acl_bytes(count));
416 if (*acl == NULL)
417 return nfserr_jukebox;
418
419 (*acl)->naces = count;
420 for (ace = (*acl)->aces; ace < (*acl)->aces + count; ace++) {
421 status = nfsd4_decode_nfsace4(argp, ace);
422 if (status)
423 return status;
424 }
425
426 return nfs_ok;
427}
428
Chuck Leverdabe9182020-11-19 14:05:51 -0500429static noinline __be32
430nfsd4_decode_security_label(struct nfsd4_compoundargs *argp,
431 struct xdr_netobj *label)
432{
433 u32 lfs, pi, length;
434 __be32 *p;
435
436 if (xdr_stream_decode_u32(argp->xdr, &lfs) < 0)
437 return nfserr_bad_xdr;
438 if (xdr_stream_decode_u32(argp->xdr, &pi) < 0)
439 return nfserr_bad_xdr;
440
441 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
442 return nfserr_bad_xdr;
443 if (length > NFS4_MAXLABELLEN)
444 return nfserr_badlabel;
445 p = xdr_inline_decode(argp->xdr, length);
446 if (!p)
447 return nfserr_bad_xdr;
448 label->len = length;
449 label->data = svcxdr_dupstr(argp, p, length);
450 if (!label->data)
451 return nfserr_jukebox;
452
453 return nfs_ok;
454}
455
Chuck Leverc941a962020-11-19 13:02:54 -0500456static __be32
Chuck Leverd1c263a2020-11-03 12:56:05 -0500457nfsd4_decode_fattr4(struct nfsd4_compoundargs *argp, u32 *bmval, u32 bmlen,
458 struct iattr *iattr, struct nfs4_acl **acl,
459 struct xdr_netobj *label, int *umask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Chuck Lever081d53f2020-11-19 13:09:13 -0500461 unsigned int starting_pos;
462 u32 attrlist4_count;
Chuck Leverd1c263a2020-11-03 12:56:05 -0500463 __be32 *p, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 iattr->ia_valid = 0;
Chuck Leverd1c263a2020-11-03 12:56:05 -0500466 status = nfsd4_decode_bitmap4(argp, bmval, bmlen);
467 if (status)
468 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
J. Bruce Fieldse864c182016-06-10 16:56:05 -0400470 if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
471 || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
472 || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2) {
473 if (nfsd_attrs_supported(argp->minorversion, bmval))
474 return nfserr_inval;
475 return nfserr_attrnotsupp;
476 }
477
Chuck Lever081d53f2020-11-19 13:09:13 -0500478 if (xdr_stream_decode_u32(argp->xdr, &attrlist4_count) < 0)
479 return nfserr_bad_xdr;
480 starting_pos = xdr_stream_pos(argp->xdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 if (bmval[0] & FATTR4_WORD0_SIZE) {
Chuck Lever2ac1b9b2020-11-19 13:47:16 -0500483 u64 size;
484
485 if (xdr_stream_decode_u64(argp->xdr, &size) < 0)
486 return nfserr_bad_xdr;
487 iattr->ia_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 iattr->ia_valid |= ATTR_SIZE;
489 }
490 if (bmval[0] & FATTR4_WORD0_ACL) {
Chuck Leverc941a962020-11-19 13:02:54 -0500491 status = nfsd4_decode_acl(argp, acl);
492 if (status)
493 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 } else
495 *acl = NULL;
496 if (bmval[1] & FATTR4_WORD1_MODE) {
Chuck Lever1c8f0ad2020-11-19 13:54:26 -0500497 u32 mode;
498
499 if (xdr_stream_decode_u32(argp->xdr, &mode) < 0)
500 return nfserr_bad_xdr;
501 iattr->ia_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 iattr->ia_mode &= (S_IFMT | S_IALLUGO);
503 iattr->ia_valid |= ATTR_MODE;
504 }
505 if (bmval[1] & FATTR4_WORD1_OWNER) {
Chuck Lever9853a5a2020-11-19 13:56:42 -0500506 u32 length;
507
508 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
509 return nfserr_bad_xdr;
510 p = xdr_inline_decode(argp->xdr, length);
511 if (!p)
512 return nfserr_bad_xdr;
513 status = nfsd_map_name_to_uid(argp->rqstp, (char *)p, length,
514 &iattr->ia_uid);
515 if (status)
NeilBrown47c85292011-02-16 13:08:35 +1100516 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 iattr->ia_valid |= ATTR_UID;
518 }
519 if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
Chuck Lever393c31d2020-11-19 13:58:18 -0500520 u32 length;
521
522 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
523 return nfserr_bad_xdr;
524 p = xdr_inline_decode(argp->xdr, length);
525 if (!p)
526 return nfserr_bad_xdr;
527 status = nfsd_map_name_to_gid(argp->rqstp, (char *)p, length,
528 &iattr->ia_gid);
529 if (status)
NeilBrown47c85292011-02-16 13:08:35 +1100530 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 iattr->ia_valid |= ATTR_GID;
532 }
533 if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
Chuck Lever1c3eff72020-11-19 14:01:08 -0500534 u32 set_it;
535
536 if (xdr_stream_decode_u32(argp->xdr, &set_it) < 0)
537 return nfserr_bad_xdr;
538 switch (set_it) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 case NFS4_SET_TO_CLIENT_TIME:
Chuck Lever1c3eff72020-11-19 14:01:08 -0500540 status = nfsd4_decode_nfstime4(argp, &iattr->ia_atime);
Christoph Hellwig4c94e132015-01-22 12:09:50 +0100541 if (status)
542 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
544 break;
545 case NFS4_SET_TO_SERVER_TIME:
546 iattr->ia_valid |= ATTR_ATIME;
547 break;
548 default:
Chuck Lever1c3eff72020-11-19 14:01:08 -0500549 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
Chuck Lever1c3eff72020-11-19 14:01:08 -0500553 u32 set_it;
554
555 if (xdr_stream_decode_u32(argp->xdr, &set_it) < 0)
556 return nfserr_bad_xdr;
557 switch (set_it) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 case NFS4_SET_TO_CLIENT_TIME:
Chuck Lever1c3eff72020-11-19 14:01:08 -0500559 status = nfsd4_decode_nfstime4(argp, &iattr->ia_mtime);
Christoph Hellwig4c94e132015-01-22 12:09:50 +0100560 if (status)
561 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
563 break;
564 case NFS4_SET_TO_SERVER_TIME:
565 iattr->ia_valid |= ATTR_MTIME;
566 break;
567 default:
Chuck Lever1c3eff72020-11-19 14:01:08 -0500568 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570 }
David Quigley18032ca2013-05-02 13:19:10 -0400571 label->len = 0;
Arnd Bergmann2285ae72018-01-22 22:09:12 +0100572 if (IS_ENABLED(CONFIG_NFSD_V4_SECURITY_LABEL) &&
573 bmval[2] & FATTR4_WORD2_SECURITY_LABEL) {
Chuck Leverdabe9182020-11-19 14:05:51 -0500574 status = nfsd4_decode_security_label(argp, label);
575 if (status)
576 return status;
David Quigley18032ca2013-05-02 13:19:10 -0400577 }
Andreas Gruenbacher47057ab2016-01-12 20:24:14 +0100578 if (bmval[2] & FATTR4_WORD2_MODE_UMASK) {
Chuck Lever66f04762020-11-19 14:07:43 -0500579 u32 mode, mask;
580
Andreas Gruenbacher47057ab2016-01-12 20:24:14 +0100581 if (!umask)
Chuck Lever66f04762020-11-19 14:07:43 -0500582 return nfserr_bad_xdr;
583 if (xdr_stream_decode_u32(argp->xdr, &mode) < 0)
584 return nfserr_bad_xdr;
585 iattr->ia_mode = mode & (S_IFMT | S_IALLUGO);
586 if (xdr_stream_decode_u32(argp->xdr, &mask) < 0)
587 return nfserr_bad_xdr;
588 *umask = mask & S_IRWXUGO;
Andreas Gruenbacher47057ab2016-01-12 20:24:14 +0100589 iattr->ia_valid |= ATTR_MODE;
590 }
Chuck Lever081d53f2020-11-19 13:09:13 -0500591
592 /* request sanity: did attrlist4 contain the expected number of words? */
593 if (attrlist4_count != xdr_stream_pos(argp->xdr) - starting_pos)
594 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Chuck Leverd1c263a2020-11-03 12:56:05 -0500596 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
Al Virob37ad282006-10-19 23:28:59 -0700599static __be32
Benny Halevye31a1b62008-08-12 20:46:18 +0300600nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
601{
602 DECODE_HEAD;
603
604 READ_BUF(sizeof(stateid_t));
J. Bruce Fields06553992014-06-02 12:32:51 -0400605 sid->si_generation = be32_to_cpup(p++);
Benny Halevye31a1b62008-08-12 20:46:18 +0300606 COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
607
608 DECODE_TAIL;
609}
610
Chuck Leverd3d2f382020-11-03 13:18:23 -0500611static __be32
612nfsd4_decode_stateid4(struct nfsd4_compoundargs *argp, stateid_t *sid)
613{
614 __be32 *p;
615
616 p = xdr_inline_decode(argp->xdr, NFS4_STATEID_SIZE);
617 if (!p)
618 return nfserr_bad_xdr;
619 sid->si_generation = be32_to_cpup(p++);
620 memcpy(&sid->si_opaque, p, sizeof(sid->si_opaque));
621 return nfs_ok;
622}
623
Chuck Lever144e8262020-11-16 17:25:02 -0500624static __be32
625nfsd4_decode_clientid4(struct nfsd4_compoundargs *argp, clientid_t *clientid)
626{
627 __be32 *p;
628
629 p = xdr_inline_decode(argp->xdr, sizeof(__be64));
630 if (!p)
631 return nfserr_bad_xdr;
632 memcpy(clientid, p, sizeof(*clientid));
633 return nfs_ok;
634}
635
636static __be32
637nfsd4_decode_state_owner4(struct nfsd4_compoundargs *argp,
638 clientid_t *clientid, struct xdr_netobj *owner)
639{
640 __be32 status;
641
642 status = nfsd4_decode_clientid4(argp, clientid);
643 if (status)
644 return status;
645 return nfsd4_decode_opaque(argp, owner);
646}
647
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400648static __be32 nfsd4_decode_cb_sec(struct nfsd4_compoundargs *argp, struct nfsd4_cb_sec *cbs)
649{
650 DECODE_HEAD;
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400651 struct user_namespace *userns = nfsd_user_namespace(argp->rqstp);
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500652 u32 dummy, uid, gid;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400653 char *machine_name;
654 int i;
655 int nr_secflavs;
656
657 /* callback_sec_params4 */
658 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400659 nr_secflavs = be32_to_cpup(p++);
J. Bruce Fields57569a72013-05-17 16:25:32 -0400660 if (nr_secflavs)
661 cbs->flavor = (u32)(-1);
662 else
663 /* Is this legal? Be generous, take it to mean AUTH_NONE: */
664 cbs->flavor = 0;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400665 for (i = 0; i < nr_secflavs; ++i) {
666 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400667 dummy = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400668 switch (dummy) {
669 case RPC_AUTH_NULL:
670 /* Nothing to read */
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500671 if (cbs->flavor == (u32)(-1))
672 cbs->flavor = RPC_AUTH_NULL;
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400673 break;
674 case RPC_AUTH_UNIX:
675 READ_BUF(8);
676 /* stamp */
J. Bruce Fields06553992014-06-02 12:32:51 -0400677 dummy = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400678
679 /* machine name */
J. Bruce Fields06553992014-06-02 12:32:51 -0400680 dummy = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400681 READ_BUF(dummy);
682 SAVEMEM(machine_name, dummy);
683
684 /* uid, gid */
685 READ_BUF(8);
J. Bruce Fields06553992014-06-02 12:32:51 -0400686 uid = be32_to_cpup(p++);
687 gid = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400688
689 /* more gids */
690 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400691 dummy = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400692 READ_BUF(dummy * 4);
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500693 if (cbs->flavor == (u32)(-1)) {
Trond Myklebuste45d1a12019-04-09 12:13:42 -0400694 kuid_t kuid = make_kuid(userns, uid);
695 kgid_t kgid = make_kgid(userns, gid);
Eric W. Biederman03bc6d12013-02-02 06:24:49 -0800696 if (uid_valid(kuid) && gid_valid(kgid)) {
697 cbs->uid = kuid;
698 cbs->gid = kgid;
699 cbs->flavor = RPC_AUTH_UNIX;
700 } else {
701 dprintk("RPC_AUTH_UNIX with invalid"
702 "uid or gid ignoring!\n");
703 }
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500704 }
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400705 break;
706 case RPC_AUTH_GSS:
707 dprintk("RPC_AUTH_GSS callback secflavor "
708 "not supported!\n");
709 READ_BUF(8);
710 /* gcbp_service */
J. Bruce Fields06553992014-06-02 12:32:51 -0400711 dummy = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400712 /* gcbp_handle_from_server */
J. Bruce Fields06553992014-06-02 12:32:51 -0400713 dummy = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400714 READ_BUF(dummy);
715 p += XDR_QUADLEN(dummy);
716 /* gcbp_handle_from_client */
717 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400718 dummy = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -0400719 READ_BUF(dummy);
720 break;
721 default:
722 dprintk("Illegal callback secflavor\n");
723 return nfserr_inval;
724 }
725 }
726 DECODE_TAIL;
727}
728
Chuck Leverd169a6a2020-11-03 13:12:27 -0500729/*
730 * NFSv4 operation argument decoders
731 */
732
733static __be32
734nfsd4_decode_access(struct nfsd4_compoundargs *argp,
735 struct nfsd4_access *access)
736{
737 if (xdr_stream_decode_u32(argp->xdr, &access->ac_req_access) < 0)
738 return nfserr_bad_xdr;
739 return nfs_ok;
740}
741
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -0400742static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp, struct nfsd4_backchannel_ctl *bc)
743{
744 DECODE_HEAD;
745
746 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -0400747 bc->bc_cb_program = be32_to_cpup(p++);
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -0400748 nfsd4_decode_cb_sec(argp, &bc->bc_cb_sec);
749
750 DECODE_TAIL;
751}
752
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400753static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
754{
755 DECODE_HEAD;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400756
757 READ_BUF(NFS4_MAX_SESSIONID_LEN + 8);
758 COPYMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields06553992014-06-02 12:32:51 -0400759 bcts->dir = be32_to_cpup(p++);
Bryan Schumaker6ce23572011-04-27 15:47:16 -0400760 /* XXX: skipping ctsa_use_conn_in_rdma_mode. Perhaps Tom Tucker
761 * could help us figure out we should be using it. */
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -0400762 DECODE_TAIL;
763}
764
Al Virob37ad282006-10-19 23:28:59 -0700765static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
767{
Chuck Leverd3d2f382020-11-03 13:18:23 -0500768 if (xdr_stream_decode_u32(argp->xdr, &close->cl_seqid) < 0)
769 return nfserr_bad_xdr;
770 return nfsd4_decode_stateid4(argp, &close->cl_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
773
Al Virob37ad282006-10-19 23:28:59 -0700774static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
776{
Chuck Levercbd9abb2020-11-03 13:19:51 -0500777 if (xdr_stream_decode_u64(argp->xdr, &commit->co_offset) < 0)
778 return nfserr_bad_xdr;
779 if (xdr_stream_decode_u32(argp->xdr, &commit->co_count) < 0)
780 return nfserr_bad_xdr;
781 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
Al Virob37ad282006-10-19 23:28:59 -0700784static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
786{
Chuck Lever000dfa12020-11-03 13:24:10 -0500787 __be32 *p, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Chuck Lever000dfa12020-11-03 13:24:10 -0500789 if (xdr_stream_decode_u32(argp->xdr, &create->cr_type) < 0)
790 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 switch (create->cr_type) {
792 case NF4LNK:
Chuck Lever000dfa12020-11-03 13:24:10 -0500793 if (xdr_stream_decode_u32(argp->xdr, &create->cr_datalen) < 0)
794 return nfserr_bad_xdr;
795 p = xdr_inline_decode(argp->xdr, create->cr_datalen);
796 if (!p)
797 return nfserr_bad_xdr;
J. Bruce Fields29c353b2014-06-24 17:06:51 -0400798 create->cr_data = svcxdr_dupstr(argp, p, create->cr_datalen);
J. Bruce Fields7fb84302014-06-24 15:06:41 -0400799 if (!create->cr_data)
J. Bruce Fields76f471282014-06-19 16:44:48 -0400800 return nfserr_jukebox;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 break;
802 case NF4BLK:
803 case NF4CHR:
Chuck Lever000dfa12020-11-03 13:24:10 -0500804 if (xdr_stream_decode_u32(argp->xdr, &create->cr_specdata1) < 0)
805 return nfserr_bad_xdr;
806 if (xdr_stream_decode_u32(argp->xdr, &create->cr_specdata2) < 0)
807 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 break;
809 case NF4SOCK:
810 case NF4FIFO:
811 case NF4DIR:
812 default:
813 break;
814 }
Chuck Lever000dfa12020-11-03 13:24:10 -0500815 status = nfsd4_decode_component4(argp, &create->cr_name,
816 &create->cr_namelen);
817 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return status;
Chuck Leverd1c263a2020-11-03 12:56:05 -0500819 status = nfsd4_decode_fattr4(argp, create->cr_bmval,
820 ARRAY_SIZE(create->cr_bmval),
821 &create->cr_iattr, &create->cr_acl,
822 &create->cr_label, &create->cr_umask);
Benny Halevyc0d6fc82009-04-03 08:29:05 +0300823 if (status)
Chuck Lever000dfa12020-11-03 13:24:10 -0500824 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Chuck Lever000dfa12020-11-03 13:24:10 -0500826 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
828
Al Virob37ad282006-10-19 23:28:59 -0700829static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
831{
Chuck Lever95e64822020-11-21 14:11:58 -0500832 return nfsd4_decode_stateid4(argp, &dr->dr_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
Al Virob37ad282006-10-19 23:28:59 -0700835static inline __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
837{
Chuck Leverf759eff2020-11-19 14:40:20 -0500838 return nfsd4_decode_bitmap4(argp, getattr->ga_bmval,
839 ARRAY_SIZE(getattr->ga_bmval));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841
Al Virob37ad282006-10-19 23:28:59 -0700842static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
844{
Chuck Lever5c505d12020-11-04 15:01:24 -0500845 return nfsd4_decode_component4(argp, &link->li_name, &link->li_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
Al Virob37ad282006-10-19 23:28:59 -0700848static __be32
Chuck Lever8918cc02020-11-16 17:16:52 -0500849nfsd4_decode_open_to_lock_owner4(struct nfsd4_compoundargs *argp,
850 struct nfsd4_lock *lock)
851{
852 __be32 status;
853
854 if (xdr_stream_decode_u32(argp->xdr, &lock->lk_new_open_seqid) < 0)
855 return nfserr_bad_xdr;
856 status = nfsd4_decode_stateid4(argp, &lock->lk_new_open_stateid);
857 if (status)
858 return status;
859 if (xdr_stream_decode_u32(argp->xdr, &lock->lk_new_lock_seqid) < 0)
860 return nfserr_bad_xdr;
861 return nfsd4_decode_state_owner4(argp, &lock->lk_new_clientid,
862 &lock->lk_new_owner);
863}
864
865static __be32
866nfsd4_decode_exist_lock_owner4(struct nfsd4_compoundargs *argp,
867 struct nfsd4_lock *lock)
868{
869 __be32 status;
870
871 status = nfsd4_decode_stateid4(argp, &lock->lk_old_lock_stateid);
872 if (status)
873 return status;
874 if (xdr_stream_decode_u32(argp->xdr, &lock->lk_old_lock_seqid) < 0)
875 return nfserr_bad_xdr;
876
877 return nfs_ok;
878}
879
880static __be32
881nfsd4_decode_locker4(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
882{
883 if (xdr_stream_decode_bool(argp->xdr, &lock->lk_is_new) < 0)
884 return nfserr_bad_xdr;
885 if (lock->lk_is_new)
886 return nfsd4_decode_open_to_lock_owner4(argp, lock);
887 return nfsd4_decode_exist_lock_owner4(argp, lock);
888}
889
890static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
892{
Chuck Lever7c59dee2020-11-03 13:29:27 -0500893 if (xdr_stream_decode_u32(argp->xdr, &lock->lk_type) < 0)
894 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
Chuck Lever7c59dee2020-11-03 13:29:27 -0500896 return nfserr_bad_xdr;
897 if (xdr_stream_decode_bool(argp->xdr, &lock->lk_reclaim) < 0)
898 return nfserr_bad_xdr;
899 if (xdr_stream_decode_u64(argp->xdr, &lock->lk_offset) < 0)
900 return nfserr_bad_xdr;
901 if (xdr_stream_decode_u64(argp->xdr, &lock->lk_length) < 0)
902 return nfserr_bad_xdr;
903 return nfsd4_decode_locker4(argp, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
Al Virob37ad282006-10-19 23:28:59 -0700906static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
908{
Chuck Lever0a146f02020-11-03 13:31:44 -0500909 if (xdr_stream_decode_u32(argp->xdr, &lockt->lt_type) < 0)
910 return nfserr_bad_xdr;
911 if ((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
912 return nfserr_bad_xdr;
913 if (xdr_stream_decode_u64(argp->xdr, &lockt->lt_offset) < 0)
914 return nfserr_bad_xdr;
915 if (xdr_stream_decode_u64(argp->xdr, &lockt->lt_length) < 0)
916 return nfserr_bad_xdr;
917 return nfsd4_decode_state_owner4(argp, &lockt->lt_clientid,
918 &lockt->lt_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
Al Virob37ad282006-10-19 23:28:59 -0700921static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
923{
Chuck Leverca9cf9f2020-11-03 13:33:28 -0500924 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Chuck Leverca9cf9f2020-11-03 13:33:28 -0500926 if (xdr_stream_decode_u32(argp->xdr, &locku->lu_type) < 0)
927 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
Chuck Leverca9cf9f2020-11-03 13:33:28 -0500929 return nfserr_bad_xdr;
930 if (xdr_stream_decode_u32(argp->xdr, &locku->lu_seqid) < 0)
931 return nfserr_bad_xdr;
932 status = nfsd4_decode_stateid4(argp, &locku->lu_stateid);
Benny Halevye31a1b62008-08-12 20:46:18 +0300933 if (status)
934 return status;
Chuck Leverca9cf9f2020-11-03 13:33:28 -0500935 if (xdr_stream_decode_u64(argp->xdr, &locku->lu_offset) < 0)
936 return nfserr_bad_xdr;
937 if (xdr_stream_decode_u64(argp->xdr, &locku->lu_length) < 0)
938 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Chuck Leverca9cf9f2020-11-03 13:33:28 -0500940 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
Al Virob37ad282006-10-19 23:28:59 -0700943static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
945{
Chuck Lever3d5877e2020-11-04 15:02:40 -0500946 return nfsd4_decode_component4(argp, &lookup->lo_name, &lookup->lo_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
Chuck Leverbf33bab2020-11-16 17:37:42 -0500949static __be32
950nfsd4_decode_createhow4(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
951{
952 __be32 status;
953
954 if (xdr_stream_decode_u32(argp->xdr, &open->op_createmode) < 0)
955 return nfserr_bad_xdr;
956 switch (open->op_createmode) {
957 case NFS4_CREATE_UNCHECKED:
958 case NFS4_CREATE_GUARDED:
959 status = nfsd4_decode_fattr4(argp, open->op_bmval,
960 ARRAY_SIZE(open->op_bmval),
961 &open->op_iattr, &open->op_acl,
962 &open->op_label, &open->op_umask);
963 if (status)
964 return status;
965 break;
966 case NFS4_CREATE_EXCLUSIVE:
967 status = nfsd4_decode_verifier4(argp, &open->op_verf);
968 if (status)
969 return status;
970 break;
971 case NFS4_CREATE_EXCLUSIVE4_1:
972 if (argp->minorversion < 1)
973 return nfserr_bad_xdr;
974 status = nfsd4_decode_verifier4(argp, &open->op_verf);
975 if (status)
976 return status;
977 status = nfsd4_decode_fattr4(argp, open->op_bmval,
978 ARRAY_SIZE(open->op_bmval),
979 &open->op_iattr, &open->op_acl,
980 &open->op_label, &open->op_umask);
981 if (status)
982 return status;
983 break;
984 default:
985 return nfserr_bad_xdr;
986 }
987
988 return nfs_ok;
989}
990
Chuck Levere6ec04b2020-11-16 17:41:21 -0500991static __be32
992nfsd4_decode_openflag4(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
993{
994 __be32 status;
995
996 if (xdr_stream_decode_u32(argp->xdr, &open->op_create) < 0)
997 return nfserr_bad_xdr;
998 switch (open->op_create) {
999 case NFS4_OPEN_NOCREATE:
1000 break;
1001 case NFS4_OPEN_CREATE:
1002 status = nfsd4_decode_createhow4(argp, open);
1003 if (status)
1004 return status;
1005 break;
1006 default:
1007 return nfserr_bad_xdr;
1008 }
1009
1010 return nfs_ok;
1011}
1012
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02001013static __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 -04001014{
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001015 u32 w;
1016
Chuck Lever9aa62f52020-11-16 17:54:48 -05001017 if (xdr_stream_decode_u32(argp->xdr, &w) < 0)
1018 return nfserr_bad_xdr;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02001019 *share_access = w & NFS4_SHARE_ACCESS_MASK;
1020 *deleg_want = w & NFS4_SHARE_WANT_MASK;
1021 if (deleg_when)
1022 *deleg_when = w & NFS4_SHARE_WHEN_MASK;
1023
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001024 switch (w & NFS4_SHARE_ACCESS_MASK) {
1025 case NFS4_SHARE_ACCESS_READ:
1026 case NFS4_SHARE_ACCESS_WRITE:
1027 case NFS4_SHARE_ACCESS_BOTH:
1028 break;
1029 default:
1030 return nfserr_bad_xdr;
1031 }
Benny Halevyfc0d14f2011-10-27 20:43:01 +02001032 w &= ~NFS4_SHARE_ACCESS_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001033 if (!w)
1034 return nfs_ok;
1035 if (!argp->minorversion)
1036 return nfserr_bad_xdr;
1037 switch (w & NFS4_SHARE_WANT_MASK) {
1038 case NFS4_SHARE_WANT_NO_PREFERENCE:
1039 case NFS4_SHARE_WANT_READ_DELEG:
1040 case NFS4_SHARE_WANT_WRITE_DELEG:
1041 case NFS4_SHARE_WANT_ANY_DELEG:
1042 case NFS4_SHARE_WANT_NO_DELEG:
1043 case NFS4_SHARE_WANT_CANCEL:
1044 break;
1045 default:
1046 return nfserr_bad_xdr;
1047 }
Benny Halevy92bac8c2011-10-19 19:13:29 -07001048 w &= ~NFS4_SHARE_WANT_MASK;
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001049 if (!w)
1050 return nfs_ok;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02001051
1052 if (!deleg_when) /* open_downgrade */
1053 return nfserr_inval;
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001054 switch (w) {
1055 case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
1056 case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
Benny Halevyc668fc62011-10-19 19:13:13 -07001057 case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
1058 NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001059 return nfs_ok;
1060 }
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001061 return nfserr_bad_xdr;
1062}
1063
1064static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
1065{
Chuck Leverb07bebd2020-11-16 17:56:17 -05001066 if (xdr_stream_decode_u32(argp->xdr, x) < 0)
1067 return nfserr_bad_xdr;
1068 /* Note: unlike access bits, deny bits may be zero. */
Dan Carpenter01cd4af2011-10-17 10:41:17 +03001069 if (*x & ~NFS4_SHARE_DENY_BOTH)
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001070 return nfserr_bad_xdr;
Chuck Leverb07bebd2020-11-16 17:56:17 -05001071
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001072 return nfs_ok;
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001073}
1074
Al Virob37ad282006-10-19 23:28:59 -07001075static __be32
Chuck Lever1708e502020-11-16 17:45:04 -05001076nfsd4_decode_open_claim4(struct nfsd4_compoundargs *argp,
1077 struct nfsd4_open *open)
1078{
1079 __be32 status;
1080
1081 if (xdr_stream_decode_u32(argp->xdr, &open->op_claim_type) < 0)
1082 return nfserr_bad_xdr;
1083 switch (open->op_claim_type) {
1084 case NFS4_OPEN_CLAIM_NULL:
1085 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1086 status = nfsd4_decode_component4(argp, &open->op_fname,
1087 &open->op_fnamelen);
1088 if (status)
1089 return status;
1090 break;
1091 case NFS4_OPEN_CLAIM_PREVIOUS:
1092 if (xdr_stream_decode_u32(argp->xdr, &open->op_delegate_type) < 0)
1093 return nfserr_bad_xdr;
1094 break;
1095 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1096 status = nfsd4_decode_stateid4(argp, &open->op_delegate_stateid);
1097 if (status)
1098 return status;
1099 status = nfsd4_decode_component4(argp, &open->op_fname,
1100 &open->op_fnamelen);
1101 if (status)
1102 return status;
1103 break;
1104 case NFS4_OPEN_CLAIM_FH:
1105 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1106 if (argp->minorversion < 1)
1107 return nfserr_bad_xdr;
1108 /* void */
1109 break;
1110 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1111 if (argp->minorversion < 1)
1112 return nfserr_bad_xdr;
1113 status = nfsd4_decode_stateid4(argp, &open->op_delegate_stateid);
1114 if (status)
1115 return status;
1116 break;
1117 default:
1118 return nfserr_bad_xdr;
1119 }
1120
1121 return nfs_ok;
1122}
1123
1124static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
1126{
Chuck Lever61e5e0b2020-11-01 12:04:06 -05001127 __be32 status;
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02001128 u32 dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 memset(open->op_bmval, 0, sizeof(open->op_bmval));
1131 open->op_iattr.ia_valid = 0;
J. Bruce Fieldsfe0750e2011-07-30 23:33:59 -04001132 open->op_openowner = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
J. Bruce Fields9d313b12013-02-28 12:51:49 -08001134 open->op_xdr_error = 0;
Chuck Lever61e5e0b2020-11-01 12:04:06 -05001135 if (xdr_stream_decode_u32(argp->xdr, &open->op_seqid) < 0)
1136 return nfserr_bad_xdr;
1137 /* deleg_want is ignored */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02001138 status = nfsd4_decode_share_access(argp, &open->op_share_access,
1139 &open->op_deleg_want, &dummy);
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001140 if (status)
Chuck Lever61e5e0b2020-11-01 12:04:06 -05001141 return status;
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001142 status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
1143 if (status)
Chuck Lever61e5e0b2020-11-01 12:04:06 -05001144 return status;
1145 status = nfsd4_decode_state_owner4(argp, &open->op_clientid,
1146 &open->op_owner);
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001147 if (status)
Chuck Lever61e5e0b2020-11-01 12:04:06 -05001148 return status;
Chuck Levere6ec04b2020-11-16 17:41:21 -05001149 status = nfsd4_decode_openflag4(argp, open);
1150 if (status)
1151 return status;
Chuck Lever61e5e0b2020-11-01 12:04:06 -05001152 return nfsd4_decode_open_claim4(argp, open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
Al Virob37ad282006-10-19 23:28:59 -07001155static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
1157{
Chuck Lever06bee692020-11-03 14:18:57 -05001158 __be32 status;
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001159
1160 if (argp->minorversion >= 1)
1161 return nfserr_notsupp;
1162
Chuck Lever06bee692020-11-03 14:18:57 -05001163 status = nfsd4_decode_stateid4(argp, &open_conf->oc_req_stateid);
Benny Halevye31a1b62008-08-12 20:46:18 +03001164 if (status)
1165 return status;
Chuck Lever06bee692020-11-03 14:18:57 -05001166 if (xdr_stream_decode_u32(argp->xdr, &open_conf->oc_seqid) < 0)
1167 return nfserr_bad_xdr;
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001168
Chuck Lever06bee692020-11-03 14:18:57 -05001169 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
Al Virob37ad282006-10-19 23:28:59 -07001172static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
1174{
Chuck Leverdca716512020-11-03 14:21:01 -05001175 __be32 status;
1176
1177 status = nfsd4_decode_stateid4(argp, &open_down->od_stateid);
Benny Halevye31a1b62008-08-12 20:46:18 +03001178 if (status)
1179 return status;
Chuck Leverdca716512020-11-03 14:21:01 -05001180 if (xdr_stream_decode_u32(argp->xdr, &open_down->od_seqid) < 0)
1181 return nfserr_bad_xdr;
1182 /* deleg_want is ignored */
Benny Halevy2c8bd7e2012-02-16 20:57:09 +02001183 status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
1184 &open_down->od_deleg_want, NULL);
J. Bruce Fields04f9e662011-10-10 14:37:13 -04001185 if (status)
1186 return status;
Chuck Leverdca716512020-11-03 14:21:01 -05001187 return nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
Al Virob37ad282006-10-19 23:28:59 -07001190static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
1192{
Chuck Levera73bed92020-11-03 14:23:02 -05001193 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Chuck Levera73bed92020-11-03 14:23:02 -05001195 if (xdr_stream_decode_u32(argp->xdr, &putfh->pf_fhlen) < 0)
1196 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if (putfh->pf_fhlen > NFS4_FHSIZE)
Chuck Levera73bed92020-11-03 14:23:02 -05001198 return nfserr_bad_xdr;
1199 p = xdr_inline_decode(argp->xdr, putfh->pf_fhlen);
1200 if (!p)
1201 return nfserr_bad_xdr;
1202 putfh->pf_fhval = svcxdr_tmpalloc(argp, putfh->pf_fhlen);
1203 if (!putfh->pf_fhval)
1204 return nfserr_jukebox;
1205 memcpy(putfh->pf_fhval, p, putfh->pf_fhlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Chuck Levera73bed92020-11-03 14:23:02 -05001207 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
Al Virob37ad282006-10-19 23:28:59 -07001210static __be32
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001211nfsd4_decode_putpubfh(struct nfsd4_compoundargs *argp, void *p)
1212{
1213 if (argp->minorversion == 0)
1214 return nfs_ok;
1215 return nfserr_notsupp;
1216}
1217
1218static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
1220{
Chuck Lever3909c3b2020-11-03 14:28:24 -05001221 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Chuck Lever3909c3b2020-11-03 14:28:24 -05001223 status = nfsd4_decode_stateid4(argp, &read->rd_stateid);
Benny Halevye31a1b62008-08-12 20:46:18 +03001224 if (status)
1225 return status;
Chuck Lever3909c3b2020-11-03 14:28:24 -05001226 if (xdr_stream_decode_u64(argp->xdr, &read->rd_offset) < 0)
1227 return nfserr_bad_xdr;
1228 if (xdr_stream_decode_u32(argp->xdr, &read->rd_length) < 0)
1229 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Chuck Lever3909c3b2020-11-03 14:28:24 -05001231 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
Al Virob37ad282006-10-19 23:28:59 -07001234static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
1236{
Chuck Lever0dfaf2a2020-11-03 14:30:59 -05001237 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Chuck Lever0dfaf2a2020-11-03 14:30:59 -05001239 if (xdr_stream_decode_u64(argp->xdr, &readdir->rd_cookie) < 0)
1240 return nfserr_bad_xdr;
1241 status = nfsd4_decode_verifier4(argp, &readdir->rd_verf);
1242 if (status)
1243 return status;
1244 if (xdr_stream_decode_u32(argp->xdr, &readdir->rd_dircount) < 0)
1245 return nfserr_bad_xdr;
1246 if (xdr_stream_decode_u32(argp->xdr, &readdir->rd_maxcount) < 0)
1247 return nfserr_bad_xdr;
1248 if (xdr_stream_decode_uint32_array(argp->xdr, readdir->rd_bmval,
1249 ARRAY_SIZE(readdir->rd_bmval)) < 0)
1250 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Chuck Lever0dfaf2a2020-11-03 14:30:59 -05001252 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
Al Virob37ad282006-10-19 23:28:59 -07001255static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
1257{
Chuck Leverb7f5fbf2020-11-04 15:04:36 -05001258 return nfsd4_decode_component4(argp, &remove->rm_name, &remove->rm_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259}
1260
Al Virob37ad282006-10-19 23:28:59 -07001261static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
1263{
Chuck Leverba881a02020-11-04 15:05:58 -05001264 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Chuck Leverba881a02020-11-04 15:05:58 -05001266 status = nfsd4_decode_component4(argp, &rename->rn_sname, &rename->rn_snamelen);
1267 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 return status;
Chuck Leverba881a02020-11-04 15:05:58 -05001269 return nfsd4_decode_component4(argp, &rename->rn_tname, &rename->rn_tnamelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
Al Virob37ad282006-10-19 23:28:59 -07001272static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
1274{
Chuck Leverd12f9042020-11-04 15:08:50 -05001275 return nfsd4_decode_clientid4(argp, clientid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
1277
Al Virob37ad282006-10-19 23:28:59 -07001278static __be32
Andy Adamsondcb488a32007-07-17 04:04:51 -07001279nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
1280 struct nfsd4_secinfo *secinfo)
1281{
Chuck Leverd0abdae2020-11-04 15:09:42 -05001282 return nfsd4_decode_component4(argp, &secinfo->si_name, &secinfo->si_namelen);
Andy Adamsondcb488a32007-07-17 04:04:51 -07001283}
1284
1285static __be32
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05001286nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
1287 struct nfsd4_secinfo_no_name *sin)
1288{
1289 DECODE_HEAD;
1290
1291 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001292 sin->sin_style = be32_to_cpup(p++);
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05001293 DECODE_TAIL;
1294}
1295
1296static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
1298{
Benny Halevye31a1b62008-08-12 20:46:18 +03001299 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Chuck Lever44592fe2020-11-21 14:14:59 -05001301 status = nfsd4_decode_stateid4(argp, &setattr->sa_stateid);
Benny Halevye31a1b62008-08-12 20:46:18 +03001302 if (status)
1303 return status;
Chuck Leverd1c263a2020-11-03 12:56:05 -05001304 return nfsd4_decode_fattr4(argp, setattr->sa_bmval,
1305 ARRAY_SIZE(setattr->sa_bmval),
1306 &setattr->sa_iattr, &setattr->sa_acl,
1307 &setattr->sa_label, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
Al Virob37ad282006-10-19 23:28:59 -07001310static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
1312{
Chuck Lever92fa6c02020-11-03 14:35:02 -05001313 __be32 *p, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001315 if (argp->minorversion >= 1)
1316 return nfserr_notsupp;
1317
Chuck Lever92fa6c02020-11-03 14:35:02 -05001318 status = nfsd4_decode_verifier4(argp, &setclientid->se_verf);
1319 if (status)
1320 return status;
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001321 status = nfsd4_decode_opaque(argp, &setclientid->se_name);
1322 if (status)
Chuck Lever92fa6c02020-11-03 14:35:02 -05001323 return status;
1324 if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_prog) < 0)
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001325 return nfserr_bad_xdr;
Chuck Lever92fa6c02020-11-03 14:35:02 -05001326 if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_netid_len) < 0)
1327 return nfserr_bad_xdr;
1328 p = xdr_inline_decode(argp->xdr, setclientid->se_callback_netid_len);
1329 if (!p)
1330 return nfserr_bad_xdr;
1331 setclientid->se_callback_netid_val = svcxdr_tmpalloc(argp,
1332 setclientid->se_callback_netid_len);
1333 if (!setclientid->se_callback_netid_val)
1334 return nfserr_jukebox;
1335 memcpy(setclientid->se_callback_netid_val, p,
1336 setclientid->se_callback_netid_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Chuck Lever92fa6c02020-11-03 14:35:02 -05001338 if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_addr_len) < 0)
1339 return nfserr_bad_xdr;
1340 p = xdr_inline_decode(argp->xdr, setclientid->se_callback_addr_len);
1341 if (!p)
1342 return nfserr_bad_xdr;
1343 setclientid->se_callback_addr_val = svcxdr_tmpalloc(argp,
1344 setclientid->se_callback_addr_len);
1345 if (!setclientid->se_callback_addr_val)
1346 return nfserr_jukebox;
1347 memcpy(setclientid->se_callback_addr_val, p,
1348 setclientid->se_callback_addr_len);
1349 if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_ident) < 0)
1350 return nfserr_bad_xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Chuck Lever92fa6c02020-11-03 14:35:02 -05001352 return nfs_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
Al Virob37ad282006-10-19 23:28:59 -07001355static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
1357{
Chuck Leverd1ca5512020-11-04 15:12:33 -05001358 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001360 if (argp->minorversion >= 1)
1361 return nfserr_notsupp;
1362
Chuck Leverd1ca5512020-11-04 15:12:33 -05001363 status = nfsd4_decode_clientid4(argp, &scd_c->sc_clientid);
1364 if (status)
1365 return status;
1366 return nfsd4_decode_verifier4(argp, &scd_c->sc_confirm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
1368
1369/* Also used for NVERIFY */
Al Virob37ad282006-10-19 23:28:59 -07001370static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1372{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 DECODE_HEAD;
1374
1375 if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
1376 goto out;
1377
1378 /* For convenience's sake, we compare raw xdr'd attributes in
J. Bruce Fieldse5f95702012-11-30 17:24:18 -05001379 * nfsd4_proc_verify */
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001382 verify->ve_attrlen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 READ_BUF(verify->ve_attrlen);
1384 SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
1385
1386 DECODE_TAIL;
1387}
1388
Al Virob37ad282006-10-19 23:28:59 -07001389static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1391{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 DECODE_HEAD;
1393
Benny Halevye31a1b62008-08-12 20:46:18 +03001394 status = nfsd4_decode_stateid(argp, &write->wr_stateid);
1395 if (status)
1396 return status;
1397 READ_BUF(16);
J. Bruce Fields542d1ab2014-06-02 12:45:31 -04001398 p = xdr_decode_hyper(p, &write->wr_offset);
J. Bruce Fields06553992014-06-02 12:32:51 -04001399 write->wr_stable_how = be32_to_cpup(p++);
Kinglong Mee54bbb7d2016-12-31 20:59:53 +08001400 if (write->wr_stable_how > NFS_FILE_SYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 goto xdr_error;
J. Bruce Fields06553992014-06-02 12:32:51 -04001402 write->wr_buflen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Chuck Leverc1346a122020-11-03 11:54:23 -05001404 if (!xdr_stream_subsegment(argp->xdr, &write->wr_payload, write->wr_buflen))
1405 goto xdr_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 DECODE_TAIL;
1408}
1409
Al Virob37ad282006-10-19 23:28:59 -07001410static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1412{
1413 DECODE_HEAD;
1414
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001415 if (argp->minorversion >= 1)
1416 return nfserr_notsupp;
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 READ_BUF(12);
1419 COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
J. Bruce Fields06553992014-06-02 12:32:51 -04001420 rlockowner->rl_owner.len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 READ_BUF(rlockowner->rl_owner.len);
1422 READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
1423
Andy Adamson60adfc52009-04-03 08:28:50 +03001424 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1425 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 DECODE_TAIL;
1427}
1428
Al Virob37ad282006-10-19 23:28:59 -07001429static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001430nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
Andy Adamson0733d212009-04-03 08:28:01 +03001431 struct nfsd4_exchange_id *exid)
Andy Adamson2db134e2009-04-03 08:27:55 +03001432{
Mi Jinlong5afa0402010-11-09 09:39:23 +08001433 int dummy, tmp;
Andy Adamson0733d212009-04-03 08:28:01 +03001434 DECODE_HEAD;
1435
1436 READ_BUF(NFS4_VERIFIER_SIZE);
1437 COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
1438
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001439 status = nfsd4_decode_opaque(argp, &exid->clname);
1440 if (status)
1441 return nfserr_bad_xdr;
Andy Adamson0733d212009-04-03 08:28:01 +03001442
1443 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001444 exid->flags = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001445
1446 /* Ignore state_protect4_a */
1447 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001448 exid->spa_how = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001449 switch (exid->spa_how) {
1450 case SP4_NONE:
1451 break;
1452 case SP4_MACH_CRED:
1453 /* spo_must_enforce */
Andrew Elbleed941642016-06-15 12:52:09 -04001454 status = nfsd4_decode_bitmap(argp,
1455 exid->spo_must_enforce);
1456 if (status)
1457 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001458 /* spo_must_allow */
Andrew Elbleed941642016-06-15 12:52:09 -04001459 status = nfsd4_decode_bitmap(argp, exid->spo_must_allow);
1460 if (status)
1461 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001462 break;
1463 case SP4_SSV:
1464 /* ssp_ops */
1465 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001466 dummy = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001467 READ_BUF(dummy * 4);
1468 p += dummy;
1469
1470 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001471 dummy = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001472 READ_BUF(dummy * 4);
1473 p += dummy;
1474
1475 /* ssp_hash_algs<> */
1476 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001477 tmp = be32_to_cpup(p++);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001478 while (tmp--) {
1479 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001480 dummy = be32_to_cpup(p++);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001481 READ_BUF(dummy);
1482 p += XDR_QUADLEN(dummy);
1483 }
Andy Adamson0733d212009-04-03 08:28:01 +03001484
1485 /* ssp_encr_algs<> */
1486 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001487 tmp = be32_to_cpup(p++);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001488 while (tmp--) {
1489 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001490 dummy = be32_to_cpup(p++);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001491 READ_BUF(dummy);
1492 p += XDR_QUADLEN(dummy);
1493 }
Andy Adamson0733d212009-04-03 08:28:01 +03001494
nixiaoming5ed96bc2018-07-23 09:57:11 +08001495 /* ignore ssp_window and ssp_num_gss_handles: */
Andy Adamson0733d212009-04-03 08:28:01 +03001496 READ_BUF(8);
Andy Adamson0733d212009-04-03 08:28:01 +03001497 break;
1498 default:
1499 goto xdr_error;
1500 }
1501
Andy Adamson0733d212009-04-03 08:28:01 +03001502 READ_BUF(4); /* nfs_impl_id4 array length */
J. Bruce Fields06553992014-06-02 12:32:51 -04001503 dummy = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001504
1505 if (dummy > 1)
1506 goto xdr_error;
1507
1508 if (dummy == 1) {
J. Bruce Fields79123442019-06-05 12:42:05 -04001509 status = nfsd4_decode_opaque(argp, &exid->nii_domain);
1510 if (status)
1511 goto xdr_error;
Andy Adamson0733d212009-04-03 08:28:01 +03001512
1513 /* nii_name */
J. Bruce Fields79123442019-06-05 12:42:05 -04001514 status = nfsd4_decode_opaque(argp, &exid->nii_name);
1515 if (status)
1516 goto xdr_error;
Andy Adamson0733d212009-04-03 08:28:01 +03001517
1518 /* nii_date */
J. Bruce Fields79123442019-06-05 12:42:05 -04001519 status = nfsd4_decode_time(argp, &exid->nii_time);
1520 if (status)
1521 goto xdr_error;
Andy Adamson0733d212009-04-03 08:28:01 +03001522 }
1523 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001524}
1525
1526static __be32
1527nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1528 struct nfsd4_create_session *sess)
1529{
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001530 DECODE_HEAD;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001531
1532 READ_BUF(16);
1533 COPYMEM(&sess->clientid, 8);
J. Bruce Fields06553992014-06-02 12:32:51 -04001534 sess->seqid = be32_to_cpup(p++);
1535 sess->flags = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001536
1537 /* Fore channel attrs */
1538 READ_BUF(28);
Trond Myklebustb96811c2019-08-18 14:18:58 -04001539 p++; /* headerpadsz is always 0 */
J. Bruce Fields06553992014-06-02 12:32:51 -04001540 sess->fore_channel.maxreq_sz = be32_to_cpup(p++);
1541 sess->fore_channel.maxresp_sz = be32_to_cpup(p++);
1542 sess->fore_channel.maxresp_cached = be32_to_cpup(p++);
1543 sess->fore_channel.maxops = be32_to_cpup(p++);
1544 sess->fore_channel.maxreqs = be32_to_cpup(p++);
1545 sess->fore_channel.nr_rdma_attrs = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001546 if (sess->fore_channel.nr_rdma_attrs == 1) {
1547 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001548 sess->fore_channel.rdma_attrs = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001549 } else if (sess->fore_channel.nr_rdma_attrs > 1) {
1550 dprintk("Too many fore channel attr bitmaps!\n");
1551 goto xdr_error;
1552 }
1553
1554 /* Back channel attrs */
1555 READ_BUF(28);
Trond Myklebustb96811c2019-08-18 14:18:58 -04001556 p++; /* headerpadsz is always 0 */
J. Bruce Fields06553992014-06-02 12:32:51 -04001557 sess->back_channel.maxreq_sz = be32_to_cpup(p++);
1558 sess->back_channel.maxresp_sz = be32_to_cpup(p++);
1559 sess->back_channel.maxresp_cached = be32_to_cpup(p++);
1560 sess->back_channel.maxops = be32_to_cpup(p++);
1561 sess->back_channel.maxreqs = be32_to_cpup(p++);
1562 sess->back_channel.nr_rdma_attrs = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001563 if (sess->back_channel.nr_rdma_attrs == 1) {
1564 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001565 sess->back_channel.rdma_attrs = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001566 } else if (sess->back_channel.nr_rdma_attrs > 1) {
1567 dprintk("Too many back channel attr bitmaps!\n");
1568 goto xdr_error;
1569 }
1570
J. Bruce Fieldsacb28872012-03-27 14:50:26 -04001571 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001572 sess->callback_prog = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -04001573 nfsd4_decode_cb_sec(argp, &sess->cb_sec);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001574 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001575}
1576
1577static __be32
1578nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1579 struct nfsd4_destroy_session *destroy_session)
1580{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001581 DECODE_HEAD;
1582 READ_BUF(NFS4_MAX_SESSIONID_LEN);
1583 COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1584
1585 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001586}
1587
1588static __be32
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001589nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1590 struct nfsd4_free_stateid *free_stateid)
1591{
1592 DECODE_HEAD;
1593
1594 READ_BUF(sizeof(stateid_t));
J. Bruce Fields06553992014-06-02 12:32:51 -04001595 free_stateid->fr_stateid.si_generation = be32_to_cpup(p++);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001596 COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
1597
1598 DECODE_TAIL;
1599}
1600
1601static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001602nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1603 struct nfsd4_sequence *seq)
1604{
Benny Halevyb85d4c02009-04-03 08:28:08 +03001605 DECODE_HEAD;
1606
1607 READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
1608 COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields06553992014-06-02 12:32:51 -04001609 seq->seqid = be32_to_cpup(p++);
1610 seq->slotid = be32_to_cpup(p++);
1611 seq->maxslots = be32_to_cpup(p++);
1612 seq->cachethis = be32_to_cpup(p++);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001613
1614 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001615}
1616
Bryan Schumaker17456802011-07-13 10:50:48 -04001617static __be32
1618nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1619{
Bryan Schumaker17456802011-07-13 10:50:48 -04001620 int i;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001621 __be32 *p, status;
1622 struct nfsd4_test_stateid_id *stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04001623
1624 READ_BUF(4);
1625 test_stateid->ts_num_ids = ntohl(*p++);
1626
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001627 INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
Bryan Schumaker17456802011-07-13 10:50:48 -04001628
1629 for (i = 0; i < test_stateid->ts_num_ids; i++) {
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -04001630 stateid = svcxdr_tmpalloc(argp, sizeof(*stateid));
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001631 if (!stateid) {
Al Viroafcf6792012-04-13 00:15:37 -04001632 status = nfserrno(-ENOMEM);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001633 goto out;
1634 }
1635
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001636 INIT_LIST_HEAD(&stateid->ts_id_list);
1637 list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
1638
1639 status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
Bryan Schumaker17456802011-07-13 10:50:48 -04001640 if (status)
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001641 goto out;
Bryan Schumaker17456802011-07-13 10:50:48 -04001642 }
1643
1644 status = 0;
1645out:
1646 return status;
1647xdr_error:
1648 dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
1649 status = nfserr_bad_xdr;
1650 goto out;
1651}
1652
Mi Jinlong345c2842011-10-20 17:51:39 +08001653static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
1654{
1655 DECODE_HEAD;
1656
1657 READ_BUF(8);
1658 COPYMEM(&dc->clientid, 8);
1659
1660 DECODE_TAIL;
1661}
1662
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001663static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
1664{
1665 DECODE_HEAD;
1666
1667 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001668 rc->rca_one_fs = be32_to_cpup(p++);
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001669
1670 DECODE_TAIL;
1671}
1672
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001673#ifdef CONFIG_NFSD_PNFS
1674static __be32
1675nfsd4_decode_getdeviceinfo(struct nfsd4_compoundargs *argp,
1676 struct nfsd4_getdeviceinfo *gdev)
1677{
1678 DECODE_HEAD;
1679 u32 num, i;
1680
1681 READ_BUF(sizeof(struct nfsd4_deviceid) + 3 * 4);
1682 COPYMEM(&gdev->gd_devid, sizeof(struct nfsd4_deviceid));
1683 gdev->gd_layout_type = be32_to_cpup(p++);
1684 gdev->gd_maxcount = be32_to_cpup(p++);
1685 num = be32_to_cpup(p++);
1686 if (num) {
Scott Mayhew3171822f2018-06-08 16:31:46 -04001687 if (num > 1000)
1688 goto xdr_error;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001689 READ_BUF(4 * num);
1690 gdev->gd_notify_types = be32_to_cpup(p++);
1691 for (i = 1; i < num; i++) {
1692 if (be32_to_cpup(p++)) {
1693 status = nfserr_inval;
1694 goto out;
1695 }
1696 }
1697 }
1698 DECODE_TAIL;
1699}
1700
1701static __be32
1702nfsd4_decode_layoutget(struct nfsd4_compoundargs *argp,
1703 struct nfsd4_layoutget *lgp)
1704{
1705 DECODE_HEAD;
1706
1707 READ_BUF(36);
1708 lgp->lg_signal = be32_to_cpup(p++);
1709 lgp->lg_layout_type = be32_to_cpup(p++);
1710 lgp->lg_seg.iomode = be32_to_cpup(p++);
1711 p = xdr_decode_hyper(p, &lgp->lg_seg.offset);
1712 p = xdr_decode_hyper(p, &lgp->lg_seg.length);
1713 p = xdr_decode_hyper(p, &lgp->lg_minlength);
Kinglong Meedb59c0e2015-03-19 19:04:41 +08001714
1715 status = nfsd4_decode_stateid(argp, &lgp->lg_sid);
1716 if (status)
1717 return status;
1718
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001719 READ_BUF(4);
1720 lgp->lg_maxcount = be32_to_cpup(p++);
1721
1722 DECODE_TAIL;
1723}
1724
1725static __be32
1726nfsd4_decode_layoutcommit(struct nfsd4_compoundargs *argp,
1727 struct nfsd4_layoutcommit *lcp)
1728{
1729 DECODE_HEAD;
1730 u32 timechange;
1731
1732 READ_BUF(20);
1733 p = xdr_decode_hyper(p, &lcp->lc_seg.offset);
1734 p = xdr_decode_hyper(p, &lcp->lc_seg.length);
1735 lcp->lc_reclaim = be32_to_cpup(p++);
Kinglong Meedb59c0e2015-03-19 19:04:41 +08001736
1737 status = nfsd4_decode_stateid(argp, &lcp->lc_sid);
1738 if (status)
1739 return status;
1740
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001741 READ_BUF(4);
1742 lcp->lc_newoffset = be32_to_cpup(p++);
1743 if (lcp->lc_newoffset) {
1744 READ_BUF(8);
1745 p = xdr_decode_hyper(p, &lcp->lc_last_wr);
1746 } else
1747 lcp->lc_last_wr = 0;
1748 READ_BUF(4);
1749 timechange = be32_to_cpup(p++);
1750 if (timechange) {
1751 status = nfsd4_decode_time(argp, &lcp->lc_mtime);
1752 if (status)
1753 return status;
1754 } else {
1755 lcp->lc_mtime.tv_nsec = UTIME_NOW;
1756 }
1757 READ_BUF(8);
1758 lcp->lc_layout_type = be32_to_cpup(p++);
1759
1760 /*
1761 * Save the layout update in XDR format and let the layout driver deal
1762 * with it later.
1763 */
1764 lcp->lc_up_len = be32_to_cpup(p++);
1765 if (lcp->lc_up_len > 0) {
1766 READ_BUF(lcp->lc_up_len);
1767 READMEM(lcp->lc_up_layout, lcp->lc_up_len);
1768 }
1769
1770 DECODE_TAIL;
1771}
1772
1773static __be32
1774nfsd4_decode_layoutreturn(struct nfsd4_compoundargs *argp,
1775 struct nfsd4_layoutreturn *lrp)
1776{
1777 DECODE_HEAD;
1778
1779 READ_BUF(16);
1780 lrp->lr_reclaim = be32_to_cpup(p++);
1781 lrp->lr_layout_type = be32_to_cpup(p++);
1782 lrp->lr_seg.iomode = be32_to_cpup(p++);
1783 lrp->lr_return_type = be32_to_cpup(p++);
1784 if (lrp->lr_return_type == RETURN_FILE) {
1785 READ_BUF(16);
1786 p = xdr_decode_hyper(p, &lrp->lr_seg.offset);
1787 p = xdr_decode_hyper(p, &lrp->lr_seg.length);
Kinglong Meedb59c0e2015-03-19 19:04:41 +08001788
1789 status = nfsd4_decode_stateid(argp, &lrp->lr_sid);
1790 if (status)
1791 return status;
1792
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001793 READ_BUF(4);
1794 lrp->lrf_body_len = be32_to_cpup(p++);
1795 if (lrp->lrf_body_len > 0) {
1796 READ_BUF(lrp->lrf_body_len);
1797 READMEM(lrp->lrf_body, lrp->lrf_body_len);
1798 }
1799 } else {
1800 lrp->lr_seg.offset = 0;
1801 lrp->lr_seg.length = NFS4_MAX_UINT64;
1802 }
1803
1804 DECODE_TAIL;
1805}
1806#endif /* CONFIG_NFSD_PNFS */
1807
Andy Adamson2db134e2009-04-03 08:27:55 +03001808static __be32
Anna Schumaker95d871f2014-11-07 14:44:26 -05001809nfsd4_decode_fallocate(struct nfsd4_compoundargs *argp,
1810 struct nfsd4_fallocate *fallocate)
1811{
1812 DECODE_HEAD;
1813
1814 status = nfsd4_decode_stateid(argp, &fallocate->falloc_stateid);
1815 if (status)
1816 return status;
1817
1818 READ_BUF(16);
1819 p = xdr_decode_hyper(p, &fallocate->falloc_offset);
1820 xdr_decode_hyper(p, &fallocate->falloc_length);
1821
1822 DECODE_TAIL;
1823}
1824
1825static __be32
Christoph Hellwigffa01602015-12-03 12:59:52 +01001826nfsd4_decode_clone(struct nfsd4_compoundargs *argp, struct nfsd4_clone *clone)
1827{
1828 DECODE_HEAD;
1829
1830 status = nfsd4_decode_stateid(argp, &clone->cl_src_stateid);
1831 if (status)
1832 return status;
1833 status = nfsd4_decode_stateid(argp, &clone->cl_dst_stateid);
1834 if (status)
1835 return status;
1836
1837 READ_BUF(8 + 8 + 8);
1838 p = xdr_decode_hyper(p, &clone->cl_src_pos);
1839 p = xdr_decode_hyper(p, &clone->cl_dst_pos);
1840 p = xdr_decode_hyper(p, &clone->cl_count);
1841 DECODE_TAIL;
1842}
1843
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001844static __be32 nfsd4_decode_nl4_server(struct nfsd4_compoundargs *argp,
1845 struct nl4_server *ns)
1846{
1847 DECODE_HEAD;
1848 struct nfs42_netaddr *naddr;
1849
1850 READ_BUF(4);
1851 ns->nl4_type = be32_to_cpup(p++);
1852
1853 /* currently support for 1 inter-server source server */
1854 switch (ns->nl4_type) {
1855 case NL4_NETADDR:
1856 naddr = &ns->u.nl4_addr;
1857
1858 READ_BUF(4);
1859 naddr->netid_len = be32_to_cpup(p++);
1860 if (naddr->netid_len > RPCBIND_MAXNETIDLEN)
1861 goto xdr_error;
1862
1863 READ_BUF(naddr->netid_len + 4); /* 4 for uaddr len */
1864 COPYMEM(naddr->netid, naddr->netid_len);
1865
1866 naddr->addr_len = be32_to_cpup(p++);
1867 if (naddr->addr_len > RPCBIND_MAXUADDRLEN)
1868 goto xdr_error;
1869
1870 READ_BUF(naddr->addr_len);
1871 COPYMEM(naddr->addr, naddr->addr_len);
1872 break;
1873 default:
1874 goto xdr_error;
1875 }
1876 DECODE_TAIL;
1877}
1878
Christoph Hellwigffa01602015-12-03 12:59:52 +01001879static __be32
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001880nfsd4_decode_copy(struct nfsd4_compoundargs *argp, struct nfsd4_copy *copy)
1881{
1882 DECODE_HEAD;
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001883 struct nl4_server *ns_dummy;
1884 int i, count;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001885
1886 status = nfsd4_decode_stateid(argp, &copy->cp_src_stateid);
1887 if (status)
1888 return status;
1889 status = nfsd4_decode_stateid(argp, &copy->cp_dst_stateid);
1890 if (status)
1891 return status;
1892
1893 READ_BUF(8 + 8 + 8 + 4 + 4 + 4);
1894 p = xdr_decode_hyper(p, &copy->cp_src_pos);
1895 p = xdr_decode_hyper(p, &copy->cp_dst_pos);
1896 p = xdr_decode_hyper(p, &copy->cp_count);
J. Bruce Fieldsedcc8452018-03-07 15:37:35 -05001897 p++; /* ca_consecutive: we always do consecutive copies */
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001898 copy->cp_synchronous = be32_to_cpup(p++);
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001899
1900 count = be32_to_cpup(p++);
1901
1902 copy->cp_intra = false;
1903 if (count == 0) { /* intra-server copy */
1904 copy->cp_intra = true;
1905 goto intra;
1906 }
1907
1908 /* decode all the supplied server addresses but use first */
1909 status = nfsd4_decode_nl4_server(argp, &copy->cp_src);
1910 if (status)
1911 return status;
1912
1913 ns_dummy = kmalloc(sizeof(struct nl4_server), GFP_KERNEL);
1914 if (ns_dummy == NULL)
1915 return nfserrno(-ENOMEM);
1916 for (i = 0; i < count - 1; i++) {
1917 status = nfsd4_decode_nl4_server(argp, ns_dummy);
1918 if (status) {
1919 kfree(ns_dummy);
1920 return status;
1921 }
1922 }
1923 kfree(ns_dummy);
1924intra:
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001925
1926 DECODE_TAIL;
1927}
1928
1929static __be32
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04001930nfsd4_decode_offload_status(struct nfsd4_compoundargs *argp,
1931 struct nfsd4_offload_status *os)
1932{
1933 return nfsd4_decode_stateid(argp, &os->stateid);
1934}
1935
1936static __be32
Olga Kornievskaia51911862019-08-08 11:14:59 -04001937nfsd4_decode_copy_notify(struct nfsd4_compoundargs *argp,
1938 struct nfsd4_copy_notify *cn)
1939{
Chuck Lever5aff7d02020-09-11 14:47:59 -04001940 __be32 status;
Olga Kornievskaia51911862019-08-08 11:14:59 -04001941
1942 status = nfsd4_decode_stateid(argp, &cn->cpn_src_stateid);
1943 if (status)
1944 return status;
1945 return nfsd4_decode_nl4_server(argp, &cn->cpn_dst);
1946}
1947
1948static __be32
Anna Schumaker24bab492014-09-26 13:58:27 -04001949nfsd4_decode_seek(struct nfsd4_compoundargs *argp, struct nfsd4_seek *seek)
1950{
1951 DECODE_HEAD;
1952
1953 status = nfsd4_decode_stateid(argp, &seek->seek_stateid);
1954 if (status)
1955 return status;
1956
1957 READ_BUF(8 + 4);
1958 p = xdr_decode_hyper(p, &seek->seek_offset);
1959 seek->seek_whence = be32_to_cpup(p);
1960
1961 DECODE_TAIL;
1962}
1963
Frank van der Linden23e50fe2020-06-23 22:39:26 +00001964/*
1965 * XDR data that is more than PAGE_SIZE in size is normally part of a
1966 * read or write. However, the size of extended attributes is limited
1967 * by the maximum request size, and then further limited by the underlying
1968 * filesystem limits. This can exceed PAGE_SIZE (currently, XATTR_SIZE_MAX
1969 * is 64k). Since there is no kvec- or page-based interface to xattrs,
1970 * and we're not dealing with contiguous pages, we need to do some copying.
1971 */
1972
1973/*
Chuck Leverc1346a122020-11-03 11:54:23 -05001974 * Decode data into buffer.
Frank van der Linden23e50fe2020-06-23 22:39:26 +00001975 */
1976static __be32
Chuck Leverc1346a122020-11-03 11:54:23 -05001977nfsd4_vbuf_from_vector(struct nfsd4_compoundargs *argp, struct xdr_buf *xdr,
1978 char **bufp, u32 buflen)
Frank van der Linden23e50fe2020-06-23 22:39:26 +00001979{
Chuck Leverc1346a122020-11-03 11:54:23 -05001980 struct page **pages = xdr->pages;
1981 struct kvec *head = xdr->head;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00001982 char *tmp, *dp;
1983 u32 len;
1984
1985 if (buflen <= head->iov_len) {
1986 /*
1987 * We're in luck, the head has enough space. Just return
1988 * the head, no need for copying.
1989 */
1990 *bufp = head->iov_base;
1991 return 0;
1992 }
1993
1994 tmp = svcxdr_tmpalloc(argp, buflen);
1995 if (tmp == NULL)
1996 return nfserr_jukebox;
1997
1998 dp = tmp;
1999 memcpy(dp, head->iov_base, head->iov_len);
2000 buflen -= head->iov_len;
2001 dp += head->iov_len;
2002
2003 while (buflen > 0) {
2004 len = min_t(u32, buflen, PAGE_SIZE);
2005 memcpy(dp, page_address(*pages), len);
2006
2007 buflen -= len;
2008 dp += len;
2009 pages++;
2010 }
2011
2012 *bufp = tmp;
2013 return 0;
2014}
2015
2016/*
2017 * Get a user extended attribute name from the XDR buffer.
2018 * It will not have the "user." prefix, so prepend it.
2019 * Lastly, check for nul characters in the name.
2020 */
2021static __be32
2022nfsd4_decode_xattr_name(struct nfsd4_compoundargs *argp, char **namep)
2023{
2024 DECODE_HEAD;
2025 char *name, *sp, *dp;
2026 u32 namelen, cnt;
2027
2028 READ_BUF(4);
2029 namelen = be32_to_cpup(p++);
2030
2031 if (namelen > (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN))
2032 return nfserr_nametoolong;
2033
2034 if (namelen == 0)
2035 goto xdr_error;
2036
2037 READ_BUF(namelen);
2038
2039 name = svcxdr_tmpalloc(argp, namelen + XATTR_USER_PREFIX_LEN + 1);
2040 if (!name)
2041 return nfserr_jukebox;
2042
2043 memcpy(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
2044
2045 /*
2046 * Copy the extended attribute name over while checking for 0
2047 * characters.
2048 */
2049 sp = (char *)p;
2050 dp = name + XATTR_USER_PREFIX_LEN;
2051 cnt = namelen;
2052
2053 while (cnt-- > 0) {
2054 if (*sp == '\0')
2055 goto xdr_error;
2056 *dp++ = *sp++;
2057 }
2058 *dp = '\0';
2059
2060 *namep = name;
2061
2062 DECODE_TAIL;
2063}
2064
2065/*
2066 * A GETXATTR op request comes without a length specifier. We just set the
2067 * maximum length for the reply based on XATTR_SIZE_MAX and the maximum
2068 * channel reply size. nfsd_getxattr will probe the length of the xattr,
2069 * check it against getxa_len, and allocate + return the value.
2070 */
2071static __be32
2072nfsd4_decode_getxattr(struct nfsd4_compoundargs *argp,
2073 struct nfsd4_getxattr *getxattr)
2074{
2075 __be32 status;
2076 u32 maxcount;
2077
2078 status = nfsd4_decode_xattr_name(argp, &getxattr->getxa_name);
2079 if (status)
2080 return status;
2081
2082 maxcount = svc_max_payload(argp->rqstp);
2083 maxcount = min_t(u32, XATTR_SIZE_MAX, maxcount);
2084
2085 getxattr->getxa_len = maxcount;
2086
2087 return status;
2088}
2089
2090static __be32
2091nfsd4_decode_setxattr(struct nfsd4_compoundargs *argp,
2092 struct nfsd4_setxattr *setxattr)
2093{
2094 DECODE_HEAD;
2095 u32 flags, maxcount, size;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002096
2097 READ_BUF(4);
2098 flags = be32_to_cpup(p++);
2099
2100 if (flags > SETXATTR4_REPLACE)
2101 return nfserr_inval;
2102 setxattr->setxa_flags = flags;
2103
2104 status = nfsd4_decode_xattr_name(argp, &setxattr->setxa_name);
2105 if (status)
2106 return status;
2107
2108 maxcount = svc_max_payload(argp->rqstp);
2109 maxcount = min_t(u32, XATTR_SIZE_MAX, maxcount);
2110
2111 READ_BUF(4);
2112 size = be32_to_cpup(p++);
2113 if (size > maxcount)
2114 return nfserr_xattr2big;
2115
2116 setxattr->setxa_len = size;
2117 if (size > 0) {
Chuck Leverc1346a122020-11-03 11:54:23 -05002118 struct xdr_buf payload;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002119
Chuck Leverc1346a122020-11-03 11:54:23 -05002120 if (!xdr_stream_subsegment(argp->xdr, &payload, size))
2121 goto xdr_error;
2122 status = nfsd4_vbuf_from_vector(argp, &payload,
2123 &setxattr->setxa_buf, size);
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002124 }
2125
2126 DECODE_TAIL;
2127}
2128
2129static __be32
2130nfsd4_decode_listxattrs(struct nfsd4_compoundargs *argp,
2131 struct nfsd4_listxattrs *listxattrs)
2132{
2133 DECODE_HEAD;
2134 u32 maxcount;
2135
2136 READ_BUF(12);
2137 p = xdr_decode_hyper(p, &listxattrs->lsxa_cookie);
2138
2139 /*
2140 * If the cookie is too large to have even one user.x attribute
2141 * plus trailing '\0' left in a maximum size buffer, it's invalid.
2142 */
2143 if (listxattrs->lsxa_cookie >=
2144 (XATTR_LIST_MAX / (XATTR_USER_PREFIX_LEN + 2)))
2145 return nfserr_badcookie;
2146
2147 maxcount = be32_to_cpup(p++);
2148 if (maxcount < 8)
2149 /* Always need at least 2 words (length and one character) */
2150 return nfserr_inval;
2151
2152 maxcount = min(maxcount, svc_max_payload(argp->rqstp));
2153 listxattrs->lsxa_maxcount = maxcount;
2154
2155 DECODE_TAIL;
2156}
2157
2158static __be32
2159nfsd4_decode_removexattr(struct nfsd4_compoundargs *argp,
2160 struct nfsd4_removexattr *removexattr)
2161{
2162 return nfsd4_decode_xattr_name(argp, &removexattr->rmxa_name);
2163}
2164
Anna Schumaker24bab492014-09-26 13:58:27 -04002165static __be32
Benny Halevy347e0ad2008-07-02 11:13:41 +03002166nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
2167{
2168 return nfs_ok;
2169}
2170
Benny Halevy3c375c62008-07-02 11:14:01 +03002171static __be32
2172nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
2173{
Benny Halevy1e685ec2009-03-04 23:06:06 +02002174 return nfserr_notsupp;
Benny Halevy3c375c62008-07-02 11:14:01 +03002175}
2176
Benny Halevy347e0ad2008-07-02 11:13:41 +03002177typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
2178
Chuck Leverc1df6092017-08-01 11:59:58 -04002179static const nfsd4_dec nfsd4_dec_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04002180 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
2181 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
2182 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
2183 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
2184 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
2185 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
2186 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
2187 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
2188 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
2189 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
2190 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
2191 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
2192 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
2193 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
2194 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
2195 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
2196 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
2197 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
2198 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
2199 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002200 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_putpubfh,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04002201 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
2202 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
2203 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
2204 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
2205 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
2206 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
2207 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
2208 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
2209 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
2210 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
2211 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
2212 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
2213 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
2214 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
2215 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
2216 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
Andy Adamson2db134e2009-04-03 08:27:55 +03002217
2218 /* new operations for NFSv4.1 */
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002219 [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002220 [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
Randy Dunlap9064caa2009-04-28 16:48:25 -07002221 [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
2222 [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
2223 [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04002224 [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07002225 [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002226#ifdef CONFIG_NFSD_PNFS
2227 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_getdeviceinfo,
2228 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
2229 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_layoutcommit,
2230 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_layoutget,
2231 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_layoutreturn,
2232#else
Randy Dunlap9064caa2009-04-28 16:48:25 -07002233 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
2234 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
2235 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
2236 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
2237 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002238#endif
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05002239 [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
Randy Dunlap9064caa2009-04-28 16:48:25 -07002240 [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
2241 [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
Bryan Schumaker17456802011-07-13 10:50:48 -04002242 [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07002243 [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
Mi Jinlong345c2842011-10-20 17:51:39 +08002244 [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002245 [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
Anna Schumaker87a15a82014-09-26 13:58:26 -04002246
2247 /* new operations for NFSv4.2 */
Anna Schumaker95d871f2014-11-07 14:44:26 -05002248 [OP_ALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
Anna Schumaker29ae7f92016-09-07 15:57:30 -04002249 [OP_COPY] = (nfsd4_dec)nfsd4_decode_copy,
Olga Kornievskaia51911862019-08-08 11:14:59 -04002250 [OP_COPY_NOTIFY] = (nfsd4_dec)nfsd4_decode_copy_notify,
Anna Schumakerb0cb9082014-11-07 14:44:27 -05002251 [OP_DEALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
Anna Schumaker87a15a82014-09-26 13:58:26 -04002252 [OP_IO_ADVISE] = (nfsd4_dec)nfsd4_decode_notsupp,
2253 [OP_LAYOUTERROR] = (nfsd4_dec)nfsd4_decode_notsupp,
2254 [OP_LAYOUTSTATS] = (nfsd4_dec)nfsd4_decode_notsupp,
Olga Kornievskaia885e2bf2018-07-20 18:19:19 -04002255 [OP_OFFLOAD_CANCEL] = (nfsd4_dec)nfsd4_decode_offload_status,
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04002256 [OP_OFFLOAD_STATUS] = (nfsd4_dec)nfsd4_decode_offload_status,
Anna Schumaker528b8492020-09-28 13:08:58 -04002257 [OP_READ_PLUS] = (nfsd4_dec)nfsd4_decode_read,
Anna Schumaker24bab492014-09-26 13:58:27 -04002258 [OP_SEEK] = (nfsd4_dec)nfsd4_decode_seek,
Anna Schumaker87a15a82014-09-26 13:58:26 -04002259 [OP_WRITE_SAME] = (nfsd4_dec)nfsd4_decode_notsupp,
Christoph Hellwigffa01602015-12-03 12:59:52 +01002260 [OP_CLONE] = (nfsd4_dec)nfsd4_decode_clone,
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002261 /* RFC 8276 extended atributes operations */
2262 [OP_GETXATTR] = (nfsd4_dec)nfsd4_decode_getxattr,
2263 [OP_SETXATTR] = (nfsd4_dec)nfsd4_decode_setxattr,
2264 [OP_LISTXATTRS] = (nfsd4_dec)nfsd4_decode_listxattrs,
2265 [OP_REMOVEXATTR] = (nfsd4_dec)nfsd4_decode_removexattr,
Andy Adamson2db134e2009-04-03 08:27:55 +03002266};
2267
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002268static inline bool
2269nfsd4_opnum_in_range(struct nfsd4_compoundargs *argp, struct nfsd4_op *op)
2270{
Anna Schumaker8217d142013-10-30 13:38:13 -04002271 if (op->opnum < FIRST_NFS4_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002272 return false;
Anna Schumaker8217d142013-10-30 13:38:13 -04002273 else if (argp->minorversion == 0 && op->opnum > LAST_NFS40_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002274 return false;
Anna Schumaker8217d142013-10-30 13:38:13 -04002275 else if (argp->minorversion == 1 && op->opnum > LAST_NFS41_OP)
2276 return false;
2277 else if (argp->minorversion == 2 && op->opnum > LAST_NFS42_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002278 return false;
2279 return true;
2280}
Benny Halevyf2feb962008-07-02 11:14:22 +03002281
Benny Halevy347e0ad2008-07-02 11:13:41 +03002282static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
2284{
2285 DECODE_HEAD;
2286 struct nfsd4_op *op;
J. Bruce Fields10910062011-01-24 12:11:02 -05002287 bool cachethis = false;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002288 int auth_slack= argp->rqstp->rq_auth_slack;
2289 int max_reply = auth_slack + 8; /* opcnt, status */
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05002290 int readcount = 0;
2291 int readbytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 int i;
2293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04002295 argp->taglen = be32_to_cpup(p++);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05002296 READ_BUF(argp->taglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 SAVEMEM(argp->tag, argp->taglen);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05002298 READ_BUF(8);
J. Bruce Fields06553992014-06-02 12:32:51 -04002299 argp->minorversion = be32_to_cpup(p++);
2300 argp->opcnt = be32_to_cpup(p++);
J. Bruce Fields4f0cefb2014-03-11 15:39:13 -04002301 max_reply += 4 + (XDR_QUADLEN(argp->taglen) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
2303 if (argp->taglen > NFSD4_MAX_TAGLEN)
2304 goto xdr_error;
J. Bruce Fields00781172017-11-15 12:30:27 -05002305 /*
2306 * NFS4ERR_RESOURCE is a more helpful error than GARBAGE_ARGS
2307 * here, so we return success at the xdr level so that
2308 * nfsd4_proc can handle this is an NFS-level error.
2309 */
2310 if (argp->opcnt > NFSD_MAX_OPS_PER_COMPOUND)
2311 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Tobias Klausere8c96f82006-03-24 03:15:34 -08002313 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
J. Bruce Fields5d6031c2014-07-17 16:20:39 -04002314 argp->ops = kzalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 if (!argp->ops) {
2316 argp->ops = argp->iops;
Chuck Lever817cb9d2007-09-11 18:01:20 -04002317 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 goto xdr_error;
2319 }
2320 }
2321
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002322 if (argp->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
Benny Halevy30cff1f2008-07-02 11:13:18 +03002323 argp->opcnt = 0;
2324
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 for (i = 0; i < argp->opcnt; i++) {
2326 op = &argp->ops[i];
2327 op->replay = NULL;
2328
J. Bruce Fields8a61b182012-11-16 22:28:38 -05002329 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04002330 op->opnum = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Chuck Lever08281342020-11-21 11:36:42 -05002332 if (nfsd4_opnum_in_range(argp, op)) {
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002333 op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
Chuck Lever08281342020-11-21 11:36:42 -05002334 if (op->status != nfs_ok)
2335 trace_nfsd_compound_decode_err(argp->rqstp,
2336 argp->opcnt, i,
2337 op->opnum,
2338 op->status);
2339 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 op->opnum = OP_ILLEGAL;
2341 op->status = nfserr_op_illegal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 }
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002343 op->opdesc = OPDESC(op);
J. Bruce Fields10910062011-01-24 12:11:02 -05002344 /*
2345 * We'll try to cache the result in the DRC if any one
2346 * op in the compound wants to be cached:
2347 */
2348 cachethis |= nfsd4_cache_this_op(op);
J. Bruce Fields6ff40de2013-11-05 15:07:16 -05002349
Anna Schumaker528b8492020-09-28 13:08:58 -04002350 if (op->opnum == OP_READ || op->opnum == OP_READ_PLUS) {
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05002351 readcount++;
2352 readbytes += nfsd4_max_reply(argp->rqstp, op);
2353 } else
2354 max_reply += nfsd4_max_reply(argp->rqstp, op);
J. Bruce Fieldsf7b43d02014-08-12 11:41:40 -04002355 /*
Kinglong Mee7323f0d2017-02-03 22:51:46 +08002356 * OP_LOCK and OP_LOCKT may return a conflicting lock.
2357 * (Special case because it will just skip encoding this
2358 * if it runs out of xdr buffer space, and it is the only
2359 * operation that behaves this way.)
J. Bruce Fieldsf7b43d02014-08-12 11:41:40 -04002360 */
Kinglong Mee7323f0d2017-02-03 22:51:46 +08002361 if (op->opnum == OP_LOCK || op->opnum == OP_LOCKT)
J. Bruce Fieldsf7b43d02014-08-12 11:41:40 -04002362 max_reply += NFS4_OPAQUE_LIMIT;
J. Bruce Fieldse372ba62014-05-19 12:27:11 -04002363
2364 if (op->status) {
2365 argp->opcnt = i+1;
2366 break;
2367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 }
J. Bruce Fields10910062011-01-24 12:11:02 -05002369 /* Sessions make the DRC unnecessary: */
2370 if (argp->minorversion)
2371 cachethis = false;
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05002372 svc_reserve(argp->rqstp, max_reply + readbytes);
J. Bruce Fields10910062011-01-24 12:11:02 -05002373 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002375 if (readcount > 1 || max_reply > PAGE_SIZE - auth_slack)
Jeff Layton779fb0f2014-11-19 07:51:18 -05002376 clear_bit(RQ_SPLICE_OK, &argp->rqstp->rq_flags);
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05002377
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 DECODE_TAIL;
2379}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
NeilBrownb8800922017-01-30 17:17:00 +11002381static __be32 *encode_change(__be32 *p, struct kstat *stat, struct inode *inode,
2382 struct svc_export *exp)
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002383{
NeilBrownb8800922017-01-30 17:17:00 +11002384 if (exp->ex_flags & NFSEXP_V4ROOT) {
2385 *p++ = cpu_to_be32(convert_to_wallclock(exp->cd->flush_time));
2386 *p++ = 0;
2387 } else if (IS_I_VERSION(inode)) {
Amir Goldstein39ca1bf2018-01-03 17:14:35 +02002388 p = xdr_encode_hyper(p, nfsd4_change_attribute(stat, inode));
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002389 } else {
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002390 *p++ = cpu_to_be32(stat->ctime.tv_sec);
2391 *p++ = cpu_to_be32(stat->ctime.tv_nsec);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002392 }
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002393 return p;
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002394}
2395
J. Bruce Fields16945142018-04-25 14:34:11 -04002396/*
2397 * ctime (in NFSv4, time_metadata) is not writeable, and the client
2398 * doesn't really care what resolution could theoretically be stored by
2399 * the filesystem.
2400 *
2401 * The client cares how close together changes can be while still
2402 * guaranteeing ctime changes. For most filesystems (which have
2403 * timestamps with nanosecond fields) that is limited by the resolution
2404 * of the time returned from current_time() (which I'm assuming to be
2405 * 1/HZ).
2406 */
2407static __be32 *encode_time_delta(__be32 *p, struct inode *inode)
2408{
Arnd Bergmanne4598e32019-10-31 15:14:03 +01002409 struct timespec64 ts;
J. Bruce Fields16945142018-04-25 14:34:11 -04002410 u32 ns;
2411
2412 ns = max_t(u32, NSEC_PER_SEC/HZ, inode->i_sb->s_time_gran);
Arnd Bergmanne4598e32019-10-31 15:14:03 +01002413 ts = ns_to_timespec64(ns);
J. Bruce Fields16945142018-04-25 14:34:11 -04002414
2415 p = xdr_encode_hyper(p, ts.tv_sec);
2416 *p++ = cpu_to_be32(ts.tv_nsec);
2417
2418 return p;
2419}
2420
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002421static __be32 *encode_cinfo(__be32 *p, struct nfsd4_change_info *c)
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002422{
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002423 *p++ = cpu_to_be32(c->atomic);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002424 if (c->change_supported) {
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002425 p = xdr_encode_hyper(p, c->before_change);
2426 p = xdr_encode_hyper(p, c->after_change);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002427 } else {
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002428 *p++ = cpu_to_be32(c->before_ctime_sec);
2429 *p++ = cpu_to_be32(c->before_ctime_nsec);
2430 *p++ = cpu_to_be32(c->after_ctime_sec);
2431 *p++ = cpu_to_be32(c->after_ctime_nsec);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002432 }
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002433 return p;
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002434}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002436/* Encode as an array of strings the string given with components
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002437 * separated @sep, escaped with esc_enter and esc_exit.
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002438 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002439static __be32 nfsd4_encode_components_esc(struct xdr_stream *xdr, char sep,
2440 char *components, char esc_enter,
2441 char esc_exit)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002442{
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002443 __be32 *p;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002444 __be32 pathlen;
2445 int pathlen_offset;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002446 int strlen, count=0;
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002447 char *str, *end, *next;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002448
2449 dprintk("nfsd4_encode_components(%s)\n", components);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002450
2451 pathlen_offset = xdr->buf->len;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002452 p = xdr_reserve_space(xdr, 4);
2453 if (!p)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002454 return nfserr_resource;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002455 p++; /* We will fill this in with @count later */
2456
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002457 end = str = components;
2458 while (*end) {
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002459 bool found_esc = false;
2460
2461 /* try to parse as esc_start, ..., esc_end, sep */
2462 if (*str == esc_enter) {
2463 for (; *end && (*end != esc_exit); end++)
2464 /* find esc_exit or end of string */;
2465 next = end + 1;
2466 if (*end && (!*next || *next == sep)) {
2467 str++;
2468 found_esc = true;
2469 }
2470 }
2471
2472 if (!found_esc)
2473 for (; *end && (*end != sep); end++)
2474 /* find sep or end of string */;
2475
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002476 strlen = end - str;
2477 if (strlen) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002478 p = xdr_reserve_space(xdr, strlen + 4);
2479 if (!p)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002480 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04002481 p = xdr_encode_opaque(p, str, strlen);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002482 count++;
2483 }
2484 else
2485 end++;
Benjamin Coddington5a64e562014-12-07 16:05:47 -05002486 if (found_esc)
2487 end = next;
2488
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002489 str = end;
2490 }
Benjamin Coddingtonbf7491f2014-12-07 16:05:48 -05002491 pathlen = htonl(count);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002492 write_bytes_to_xdr_buf(xdr->buf, pathlen_offset, &pathlen, 4);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002493 return 0;
2494}
2495
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002496/* Encode as an array of strings the string given with components
2497 * separated @sep.
2498 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002499static __be32 nfsd4_encode_components(struct xdr_stream *xdr, char sep,
2500 char *components)
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002501{
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002502 return nfsd4_encode_components_esc(xdr, sep, components, 0, 0);
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002503}
2504
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002505/*
2506 * encode a location element of a fs_locations structure
2507 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002508static __be32 nfsd4_encode_fs_location4(struct xdr_stream *xdr,
2509 struct nfsd4_fs_location *location)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002510{
Al Virob37ad282006-10-19 23:28:59 -07002511 __be32 status;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002512
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002513 status = nfsd4_encode_components_esc(xdr, ':', location->hosts,
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002514 '[', ']');
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002515 if (status)
2516 return status;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002517 status = nfsd4_encode_components(xdr, '/', location->path);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002518 if (status)
2519 return status;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002520 return 0;
2521}
2522
2523/*
Trond Myklebusted748aa2011-09-12 19:37:06 -04002524 * Encode a path in RFC3530 'pathname4' format
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002525 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002526static __be32 nfsd4_encode_path(struct xdr_stream *xdr,
2527 const struct path *root,
2528 const struct path *path)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002529{
Al Viro301f0262013-09-01 16:06:39 -04002530 struct path cur = *path;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002531 __be32 *p;
Trond Myklebusted748aa2011-09-12 19:37:06 -04002532 struct dentry **components = NULL;
2533 unsigned int ncomponents = 0;
2534 __be32 err = nfserr_jukebox;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002535
Trond Myklebusted748aa2011-09-12 19:37:06 -04002536 dprintk("nfsd4_encode_components(");
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002537
Trond Myklebusted748aa2011-09-12 19:37:06 -04002538 path_get(&cur);
2539 /* First walk the path up to the nfsd root, and store the
2540 * dentries/path components in an array.
2541 */
2542 for (;;) {
Kinglong Meeb77a4b22015-03-15 23:13:32 +08002543 if (path_equal(&cur, root))
Trond Myklebusted748aa2011-09-12 19:37:06 -04002544 break;
2545 if (cur.dentry == cur.mnt->mnt_root) {
2546 if (follow_up(&cur))
2547 continue;
2548 goto out_free;
2549 }
2550 if ((ncomponents & 15) == 0) {
2551 struct dentry **new;
2552 new = krealloc(components,
2553 sizeof(*new) * (ncomponents + 16),
2554 GFP_KERNEL);
2555 if (!new)
2556 goto out_free;
2557 components = new;
2558 }
2559 components[ncomponents++] = cur.dentry;
2560 cur.dentry = dget_parent(cur.dentry);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002561 }
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002562 err = nfserr_resource;
2563 p = xdr_reserve_space(xdr, 4);
2564 if (!p)
Trond Myklebusted748aa2011-09-12 19:37:06 -04002565 goto out_free;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002566 *p++ = cpu_to_be32(ncomponents);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002567
2568 while (ncomponents) {
2569 struct dentry *dentry = components[ncomponents - 1];
Al Viro301f0262013-09-01 16:06:39 -04002570 unsigned int len;
Trond Myklebusted748aa2011-09-12 19:37:06 -04002571
Al Viro301f0262013-09-01 16:06:39 -04002572 spin_lock(&dentry->d_lock);
2573 len = dentry->d_name.len;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002574 p = xdr_reserve_space(xdr, len + 4);
2575 if (!p) {
Al Viro301f0262013-09-01 16:06:39 -04002576 spin_unlock(&dentry->d_lock);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002577 goto out_free;
Al Viro301f0262013-09-01 16:06:39 -04002578 }
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04002579 p = xdr_encode_opaque(p, dentry->d_name.name, len);
Al Viroa4555892014-10-21 20:11:25 -04002580 dprintk("/%pd", dentry);
Al Viro301f0262013-09-01 16:06:39 -04002581 spin_unlock(&dentry->d_lock);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002582 dput(dentry);
2583 ncomponents--;
2584 }
2585
Trond Myklebusted748aa2011-09-12 19:37:06 -04002586 err = 0;
2587out_free:
2588 dprintk(")\n");
2589 while (ncomponents)
2590 dput(components[--ncomponents]);
2591 kfree(components);
2592 path_put(&cur);
2593 return err;
2594}
2595
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002596static __be32 nfsd4_encode_fsloc_fsroot(struct xdr_stream *xdr,
2597 struct svc_rqst *rqstp, const struct path *path)
Trond Myklebusted748aa2011-09-12 19:37:06 -04002598{
2599 struct svc_export *exp_ps;
2600 __be32 res;
2601
2602 exp_ps = rqst_find_fsidzero_export(rqstp);
2603 if (IS_ERR(exp_ps))
2604 return nfserrno(PTR_ERR(exp_ps));
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002605 res = nfsd4_encode_path(xdr, &exp_ps->ex_path, path);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002606 exp_put(exp_ps);
2607 return res;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002608}
2609
2610/*
2611 * encode a fs_locations structure
2612 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002613static __be32 nfsd4_encode_fs_locations(struct xdr_stream *xdr,
2614 struct svc_rqst *rqstp, struct svc_export *exp)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002615{
Al Virob37ad282006-10-19 23:28:59 -07002616 __be32 status;
Al Virocc45f012006-10-19 23:28:44 -07002617 int i;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002618 __be32 *p;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002619 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002620
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002621 status = nfsd4_encode_fsloc_fsroot(xdr, rqstp, &exp->ex_path);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002622 if (status)
2623 return status;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002624 p = xdr_reserve_space(xdr, 4);
2625 if (!p)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002626 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002627 *p++ = cpu_to_be32(fslocs->locations_count);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002628 for (i=0; i<fslocs->locations_count; i++) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002629 status = nfsd4_encode_fs_location4(xdr, &fslocs->locations[i]);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002630 if (status)
2631 return status;
2632 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002633 return 0;
2634}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002636static u32 nfs4_file_type(umode_t mode)
2637{
2638 switch (mode & S_IFMT) {
2639 case S_IFIFO: return NF4FIFO;
2640 case S_IFCHR: return NF4CHR;
2641 case S_IFDIR: return NF4DIR;
2642 case S_IFBLK: return NF4BLK;
2643 case S_IFLNK: return NF4LNK;
2644 case S_IFREG: return NF4REG;
2645 case S_IFSOCK: return NF4SOCK;
2646 default: return NF4BAD;
Tom Rix25fef482020-11-01 07:32:34 -08002647 }
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002648}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
Al Virob37ad282006-10-19 23:28:59 -07002650static inline __be32
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002651nfsd4_encode_aclname(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2652 struct nfs4_ace *ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653{
J. Bruce Fields35541162014-01-08 09:49:01 -05002654 if (ace->whotype != NFS4_ACL_WHO_NAMED)
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002655 return nfs4_acl_write_who(xdr, ace->whotype);
J. Bruce Fields35541162014-01-08 09:49:01 -05002656 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002657 return nfsd4_encode_group(xdr, rqstp, ace->who_gid);
J. Bruce Fields35541162014-01-08 09:49:01 -05002658 else
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002659 return nfsd4_encode_user(xdr, rqstp, ace->who_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660}
2661
Kinglong Mee6896f152015-07-30 21:52:44 +08002662static inline __be32
Jeff Layton8a4c3922016-07-10 15:55:58 -04002663nfsd4_encode_layout_types(struct xdr_stream *xdr, u32 layout_types)
Kinglong Mee6896f152015-07-30 21:52:44 +08002664{
Jeff Layton8a4c3922016-07-10 15:55:58 -04002665 __be32 *p;
2666 unsigned long i = hweight_long(layout_types);
Kinglong Mee6896f152015-07-30 21:52:44 +08002667
Jeff Layton8a4c3922016-07-10 15:55:58 -04002668 p = xdr_reserve_space(xdr, 4 + 4 * i);
2669 if (!p)
2670 return nfserr_resource;
2671
2672 *p++ = cpu_to_be32(i);
2673
2674 for (i = LAYOUT_NFSV4_1_FILES; i < LAYOUT_TYPE_MAX; ++i)
2675 if (layout_types & (1 << i))
2676 *p++ = cpu_to_be32(i);
Kinglong Mee6896f152015-07-30 21:52:44 +08002677
2678 return 0;
2679}
2680
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002681#define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
2682 FATTR4_WORD0_RDATTR_ERROR)
2683#define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
Kinglong Meec2227a32015-07-07 10:16:37 +08002684#define WORD2_ABSENT_FS_ATTRS 0
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002685
David Quigley18032ca2013-05-02 13:19:10 -04002686#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2687static inline __be32
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002688nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2689 void *context, int len)
David Quigley18032ca2013-05-02 13:19:10 -04002690{
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002691 __be32 *p;
David Quigley18032ca2013-05-02 13:19:10 -04002692
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002693 p = xdr_reserve_space(xdr, len + 4 + 4 + 4);
2694 if (!p)
David Quigley18032ca2013-05-02 13:19:10 -04002695 return nfserr_resource;
2696
2697 /*
2698 * For now we use a 0 here to indicate the null translation; in
2699 * the future we may place a call to translation code here.
2700 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002701 *p++ = cpu_to_be32(0); /* lfs */
2702 *p++ = cpu_to_be32(0); /* pi */
David Quigley18032ca2013-05-02 13:19:10 -04002703 p = xdr_encode_opaque(p, context, len);
David Quigley18032ca2013-05-02 13:19:10 -04002704 return 0;
2705}
2706#else
2707static inline __be32
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002708nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2709 void *context, int len)
David Quigley18032ca2013-05-02 13:19:10 -04002710{ return 0; }
2711#endif
2712
Kinglong Meec2227a32015-07-07 10:16:37 +08002713static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *bmval2, u32 *rdattr_err)
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002714{
2715 /* As per referral draft: */
2716 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
2717 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
2718 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
2719 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
2720 *rdattr_err = NFSERR_MOVED;
2721 else
2722 return nfserr_moved;
2723 }
2724 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
2725 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
Kinglong Meec2227a32015-07-07 10:16:37 +08002726 *bmval2 &= WORD2_ABSENT_FS_ATTRS;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002727 return 0;
2728}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002730
2731static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
2732{
2733 struct path path = exp->ex_path;
2734 int err;
2735
2736 path_get(&path);
2737 while (follow_up(&path)) {
2738 if (path.dentry != path.mnt->mnt_root)
2739 break;
2740 }
David Howellsa528d352017-01-31 16:46:22 +00002741 err = vfs_getattr(&path, stat, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002742 path_put(&path);
2743 return err;
2744}
2745
Kinglong Mee75976de2015-07-30 21:55:30 +08002746static __be32
2747nfsd4_encode_bitmap(struct xdr_stream *xdr, u32 bmval0, u32 bmval1, u32 bmval2)
2748{
2749 __be32 *p;
2750
2751 if (bmval2) {
2752 p = xdr_reserve_space(xdr, 16);
2753 if (!p)
2754 goto out_resource;
2755 *p++ = cpu_to_be32(3);
2756 *p++ = cpu_to_be32(bmval0);
2757 *p++ = cpu_to_be32(bmval1);
2758 *p++ = cpu_to_be32(bmval2);
2759 } else if (bmval1) {
2760 p = xdr_reserve_space(xdr, 12);
2761 if (!p)
2762 goto out_resource;
2763 *p++ = cpu_to_be32(2);
2764 *p++ = cpu_to_be32(bmval0);
2765 *p++ = cpu_to_be32(bmval1);
2766 } else {
2767 p = xdr_reserve_space(xdr, 8);
2768 if (!p)
2769 goto out_resource;
2770 *p++ = cpu_to_be32(1);
2771 *p++ = cpu_to_be32(bmval0);
2772 }
2773
2774 return 0;
2775out_resource:
2776 return nfserr_resource;
2777}
2778
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779/*
2780 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
2781 * ourselves.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 */
Jeff Laytonda2ebce2014-05-30 09:09:25 -04002783static __be32
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04002784nfsd4_encode_fattr(struct xdr_stream *xdr, struct svc_fh *fhp,
2785 struct svc_export *exp,
2786 struct dentry *dentry, u32 *bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002787 struct svc_rqst *rqstp, int ignore_crossmnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788{
2789 u32 bmval0 = bmval[0];
2790 u32 bmval1 = bmval[1];
Andy Adamson7e705702009-04-03 08:29:11 +03002791 u32 bmval2 = bmval[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 struct kstat stat;
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002793 struct svc_fh *tempfh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 struct kstatfs statfs;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002795 __be32 *p;
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05002796 int starting_len = xdr->buf->len;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002797 int attrlen_offset;
2798 __be32 attrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 u32 dummy;
2800 u64 dummy64;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002801 u32 rdattr_err = 0;
Al Virob37ad282006-10-19 23:28:59 -07002802 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07002803 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 struct nfs4_acl *acl = NULL;
Arnd Bergmann0ab88ca2019-03-22 15:07:11 +01002805#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
David Quigley18032ca2013-05-02 13:19:10 -04002806 void *context = NULL;
2807 int contextlen;
Arnd Bergmann0ab88ca2019-03-22 15:07:11 +01002808#endif
David Quigley18032ca2013-05-02 13:19:10 -04002809 bool contextsupport = false;
Andy Adamson7e705702009-04-03 08:29:11 +03002810 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2811 u32 minorversion = resp->cstate.minorversion;
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002812 struct path path = {
2813 .mnt = exp->ex_path.mnt,
2814 .dentry = dentry,
2815 };
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03002816 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
2818 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002819 BUG_ON(!nfsd_attrs_supported(minorversion, bmval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002821 if (exp->ex_fslocs.migrated) {
Kinglong Meec2227a32015-07-07 10:16:37 +08002822 status = fattr_handle_absent_fs(&bmval0, &bmval1, &bmval2, &rdattr_err);
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002823 if (status)
2824 goto out;
2825 }
2826
David Howellsa528d352017-01-31 16:46:22 +00002827 err = vfs_getattr(&path, &stat, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
Al Virob8dd7b92006-10-19 23:29:01 -07002828 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 goto out_nfserr;
Christoph Hellwig12337902014-05-28 10:46:13 +02002830 if ((bmval0 & (FATTR4_WORD0_FILES_AVAIL | FATTR4_WORD0_FILES_FREE |
2831 FATTR4_WORD0_FILES_TOTAL | FATTR4_WORD0_MAXNAME)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2833 FATTR4_WORD1_SPACE_TOTAL))) {
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002834 err = vfs_statfs(&path, &statfs);
Al Virob8dd7b92006-10-19 23:29:01 -07002835 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 goto out_nfserr;
2837 }
2838 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002839 tempfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
2840 status = nfserr_jukebox;
2841 if (!tempfh)
2842 goto out;
2843 fh_init(tempfh, NFS4_FHSIZE);
2844 status = fh_compose(tempfh, exp, dentry, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 if (status)
2846 goto out;
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002847 fhp = tempfh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 }
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02002849 if (bmval0 & FATTR4_WORD0_ACL) {
Al Virob8dd7b92006-10-19 23:29:01 -07002850 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02002851 if (err == -EOPNOTSUPP)
2852 bmval0 &= ~FATTR4_WORD0_ACL;
2853 else if (err == -EINVAL) {
2854 status = nfserr_attrnotsupp;
2855 goto out;
2856 } else if (err != 0)
2857 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
David Quigley18032ca2013-05-02 13:19:10 -04002860#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
Kinglong Meec2227a32015-07-07 10:16:37 +08002861 if ((bmval2 & FATTR4_WORD2_SECURITY_LABEL) ||
2862 bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
J. Bruce Fields32ddd942017-01-03 12:30:11 -05002863 if (exp->ex_flags & NFSEXP_SECURITY_LABEL)
2864 err = security_inode_getsecctx(d_inode(dentry),
David Quigley18032ca2013-05-02 13:19:10 -04002865 &context, &contextlen);
J. Bruce Fields32ddd942017-01-03 12:30:11 -05002866 else
2867 err = -EOPNOTSUPP;
David Quigley18032ca2013-05-02 13:19:10 -04002868 contextsupport = (err == 0);
2869 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2870 if (err == -EOPNOTSUPP)
2871 bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2872 else if (err)
2873 goto out_nfserr;
2874 }
2875 }
2876#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2877
Kinglong Mee75976de2015-07-30 21:55:30 +08002878 status = nfsd4_encode_bitmap(xdr, bmval0, bmval1, bmval2);
2879 if (status)
2880 goto out;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002881
2882 attrlen_offset = xdr->buf->len;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002883 p = xdr_reserve_space(xdr, 4);
2884 if (!p)
2885 goto out_resource;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002886 p++; /* to be backfilled later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887
2888 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
J. Bruce Fieldsdcd208692017-01-11 20:34:50 -05002889 u32 supp[3];
2890
2891 memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
Andy Adamson7e705702009-04-03 08:29:11 +03002892
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02002893 if (!IS_POSIXACL(dentry->d_inode))
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002894 supp[0] &= ~FATTR4_WORD0_ACL;
David Quigley18032ca2013-05-02 13:19:10 -04002895 if (!contextsupport)
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002896 supp[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2897 if (!supp[2]) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002898 p = xdr_reserve_space(xdr, 12);
2899 if (!p)
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002900 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002901 *p++ = cpu_to_be32(2);
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002902 *p++ = cpu_to_be32(supp[0]);
2903 *p++ = cpu_to_be32(supp[1]);
Andy Adamson7e705702009-04-03 08:29:11 +03002904 } else {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002905 p = xdr_reserve_space(xdr, 16);
2906 if (!p)
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002907 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002908 *p++ = cpu_to_be32(3);
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002909 *p++ = cpu_to_be32(supp[0]);
2910 *p++ = cpu_to_be32(supp[1]);
2911 *p++ = cpu_to_be32(supp[2]);
Andy Adamson7e705702009-04-03 08:29:11 +03002912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 }
2914 if (bmval0 & FATTR4_WORD0_TYPE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002915 p = xdr_reserve_space(xdr, 4);
2916 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 goto out_resource;
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002918 dummy = nfs4_file_type(stat.mode);
J. Bruce Fields6b6d81372013-01-16 17:11:11 -05002919 if (dummy == NF4BAD) {
2920 status = nfserr_serverfault;
2921 goto out;
2922 }
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002923 *p++ = cpu_to_be32(dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 }
2925 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002926 p = xdr_reserve_space(xdr, 4);
2927 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 goto out_resource;
NeilBrown49640002005-06-23 22:02:58 -07002929 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002930 *p++ = cpu_to_be32(NFS4_FH_PERSISTENT);
NeilBrown49640002005-06-23 22:02:58 -07002931 else
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002932 *p++ = cpu_to_be32(NFS4_FH_PERSISTENT|
2933 NFS4_FH_VOL_RENAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 }
2935 if (bmval0 & FATTR4_WORD0_CHANGE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002936 p = xdr_reserve_space(xdr, 8);
2937 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 goto out_resource;
NeilBrownb8800922017-01-30 17:17:00 +11002939 p = encode_change(p, &stat, d_inode(dentry), exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 }
2941 if (bmval0 & FATTR4_WORD0_SIZE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002942 p = xdr_reserve_space(xdr, 8);
2943 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002945 p = xdr_encode_hyper(p, stat.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 }
2947 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
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(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 }
2953 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002954 p = xdr_reserve_space(xdr, 4);
2955 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002957 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 }
2959 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002960 p = xdr_reserve_space(xdr, 4);
2961 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002963 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 }
2965 if (bmval0 & FATTR4_WORD0_FSID) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002966 p = xdr_reserve_space(xdr, 16);
2967 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002969 if (exp->ex_fslocs.migrated) {
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002970 p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MAJOR);
2971 p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MINOR);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002972 } else switch(fsid_source(fhp)) {
2973 case FSIDSOURCE_FSID:
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002974 p = xdr_encode_hyper(p, (u64)exp->ex_fsid);
2975 p = xdr_encode_hyper(p, (u64)0);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002976 break;
2977 case FSIDSOURCE_DEV:
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002978 *p++ = cpu_to_be32(0);
2979 *p++ = cpu_to_be32(MAJOR(stat.dev));
2980 *p++ = cpu_to_be32(0);
2981 *p++ = cpu_to_be32(MINOR(stat.dev));
NeilBrownaf6a4e22007-02-14 00:33:12 -08002982 break;
2983 case FSIDSOURCE_UUID:
Kinglong Mee94eb3682014-05-23 20:00:19 +08002984 p = xdr_encode_opaque_fixed(p, exp->ex_uuid,
2985 EX_UUID_LEN);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002986 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 }
2988 }
2989 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002990 p = xdr_reserve_space(xdr, 4);
2991 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002993 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 }
2995 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002996 p = xdr_reserve_space(xdr, 4);
2997 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002999 *p++ = cpu_to_be32(nn->nfsd4_lease);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 }
3001 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003002 p = xdr_reserve_space(xdr, 4);
3003 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003005 *p++ = cpu_to_be32(rdattr_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 }
3007 if (bmval0 & FATTR4_WORD0_ACL) {
3008 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
3010 if (acl == NULL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003011 p = xdr_reserve_space(xdr, 4);
3012 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 goto out_resource;
3014
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003015 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 goto out_acl;
3017 }
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003018 p = xdr_reserve_space(xdr, 4);
3019 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003021 *p++ = cpu_to_be32(acl->naces);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08003023 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003024 p = xdr_reserve_space(xdr, 4*3);
3025 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003027 *p++ = cpu_to_be32(ace->type);
3028 *p++ = cpu_to_be32(ace->flag);
3029 *p++ = cpu_to_be32(ace->access_mask &
3030 NFS4_ACE_MASK_ALL);
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003031 status = nfsd4_encode_aclname(xdr, rqstp, ace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 if (status)
3033 goto out;
3034 }
3035 }
3036out_acl:
3037 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003038 p = xdr_reserve_space(xdr, 4);
3039 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 goto out_resource;
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02003041 *p++ = cpu_to_be32(IS_POSIXACL(dentry->d_inode) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
3043 }
3044 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003045 p = xdr_reserve_space(xdr, 4);
3046 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003048 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 }
3050 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
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(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 }
3056 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003057 p = xdr_reserve_space(xdr, 4);
3058 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003060 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 }
3062 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
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(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 }
3068 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003069 p = xdr_reserve_space(xdr, fhp->fh_handle.fh_size + 4);
3070 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 goto out_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003072 p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base,
3073 fhp->fh_handle.fh_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 }
3075 if (bmval0 & FATTR4_WORD0_FILEID) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003076 p = xdr_reserve_space(xdr, 8);
3077 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003079 p = xdr_encode_hyper(p, stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 }
3081 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003082 p = xdr_reserve_space(xdr, 8);
3083 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003085 p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 }
3087 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003088 p = xdr_reserve_space(xdr, 8);
3089 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003091 p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 }
3093 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003094 p = xdr_reserve_space(xdr, 8);
3095 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003097 p = xdr_encode_hyper(p, (u64) statfs.f_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07003099 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003100 status = nfsd4_encode_fs_locations(xdr, rqstp, exp);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07003101 if (status)
3102 goto out;
3103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003105 p = xdr_reserve_space(xdr, 4);
3106 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003108 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 }
3110 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003111 p = xdr_reserve_space(xdr, 8);
3112 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003114 p = xdr_encode_hyper(p, exp->ex_path.mnt->mnt_sb->s_maxbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 }
3116 if (bmval0 & FATTR4_WORD0_MAXLINK) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003117 p = xdr_reserve_space(xdr, 4);
3118 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003120 *p++ = cpu_to_be32(255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 }
3122 if (bmval0 & FATTR4_WORD0_MAXNAME) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003123 p = xdr_reserve_space(xdr, 4);
3124 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003126 *p++ = cpu_to_be32(statfs.f_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 }
3128 if (bmval0 & FATTR4_WORD0_MAXREAD) {
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;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003132 p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 }
3134 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003135 p = xdr_reserve_space(xdr, 8);
3136 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003138 p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 }
3140 if (bmval1 & FATTR4_WORD1_MODE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003141 p = xdr_reserve_space(xdr, 4);
3142 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003144 *p++ = cpu_to_be32(stat.mode & S_IALLUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 }
3146 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003147 p = xdr_reserve_space(xdr, 4);
3148 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003150 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 }
3152 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003153 p = xdr_reserve_space(xdr, 4);
3154 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003156 *p++ = cpu_to_be32(stat.nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 }
3158 if (bmval1 & FATTR4_WORD1_OWNER) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003159 status = nfsd4_encode_user(xdr, rqstp, stat.uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 if (status)
3161 goto out;
3162 }
3163 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003164 status = nfsd4_encode_group(xdr, rqstp, stat.gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 if (status)
3166 goto out;
3167 }
3168 if (bmval1 & FATTR4_WORD1_RAWDEV) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003169 p = xdr_reserve_space(xdr, 8);
3170 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003172 *p++ = cpu_to_be32((u32) MAJOR(stat.rdev));
3173 *p++ = cpu_to_be32((u32) MINOR(stat.rdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 }
3175 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003176 p = xdr_reserve_space(xdr, 8);
3177 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 goto out_resource;
3179 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003180 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 }
3182 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003183 p = xdr_reserve_space(xdr, 8);
3184 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 goto out_resource;
3186 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003187 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 }
3189 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003190 p = xdr_reserve_space(xdr, 8);
3191 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 goto out_resource;
3193 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003194 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 }
3196 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003197 p = xdr_reserve_space(xdr, 8);
3198 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 goto out_resource;
3200 dummy64 = (u64)stat.blocks << 9;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003201 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 }
3203 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003204 p = xdr_reserve_space(xdr, 12);
3205 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003207 p = xdr_encode_hyper(p, (s64)stat.atime.tv_sec);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003208 *p++ = cpu_to_be32(stat.atime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 }
3210 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003211 p = xdr_reserve_space(xdr, 12);
3212 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 goto out_resource;
J. Bruce Fields16945142018-04-25 14:34:11 -04003214 p = encode_time_delta(p, d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 }
3216 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003217 p = xdr_reserve_space(xdr, 12);
3218 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003220 p = xdr_encode_hyper(p, (s64)stat.ctime.tv_sec);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003221 *p++ = cpu_to_be32(stat.ctime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 }
3223 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003224 p = xdr_reserve_space(xdr, 12);
3225 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003227 p = xdr_encode_hyper(p, (s64)stat.mtime.tv_sec);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003228 *p++ = cpu_to_be32(stat.mtime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 }
3230 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
Kinglong Mee0a2050d2015-07-30 21:51:54 +08003231 struct kstat parent_stat;
3232 u64 ino = stat.ino;
3233
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003234 p = xdr_reserve_space(xdr, 8);
3235 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 goto out_resource;
Frank Filz406a7ea2007-11-27 11:34:05 -08003237 /*
3238 * Get parent's attributes if not ignoring crossmount
3239 * and this is the root of a cross-mounted filesystem.
3240 */
3241 if (ignore_crossmnt == 0 &&
Kinglong Mee0a2050d2015-07-30 21:51:54 +08003242 dentry == exp->ex_path.mnt->mnt_root) {
3243 err = get_parent_attributes(exp, &parent_stat);
3244 if (err)
3245 goto out_nfserr;
3246 ino = parent_stat.ino;
3247 }
3248 p = xdr_encode_hyper(p, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003250#ifdef CONFIG_NFSD_PNFS
Kinglong Mee6896f152015-07-30 21:52:44 +08003251 if (bmval1 & FATTR4_WORD1_FS_LAYOUT_TYPES) {
Jeff Layton8a4c3922016-07-10 15:55:58 -04003252 status = nfsd4_encode_layout_types(xdr, exp->ex_layout_types);
Kinglong Mee6896f152015-07-30 21:52:44 +08003253 if (status)
3254 goto out;
3255 }
3256
3257 if (bmval2 & FATTR4_WORD2_LAYOUT_TYPES) {
Jeff Layton8a4c3922016-07-10 15:55:58 -04003258 status = nfsd4_encode_layout_types(xdr, exp->ex_layout_types);
Kinglong Mee6896f152015-07-30 21:52:44 +08003259 if (status)
3260 goto out;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003261 }
3262
3263 if (bmval2 & FATTR4_WORD2_LAYOUT_BLKSIZE) {
3264 p = xdr_reserve_space(xdr, 4);
3265 if (!p)
3266 goto out_resource;
3267 *p++ = cpu_to_be32(stat.blksize);
3268 }
3269#endif /* CONFIG_NFSD_PNFS */
Benny Halevy8c18f202009-04-03 08:29:14 +03003270 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
Trond Myklebustb26b78c2017-05-09 16:24:59 -04003271 u32 supp[3];
3272
3273 memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
3274 supp[0] &= NFSD_SUPPATTR_EXCLCREAT_WORD0;
3275 supp[1] &= NFSD_SUPPATTR_EXCLCREAT_WORD1;
3276 supp[2] &= NFSD_SUPPATTR_EXCLCREAT_WORD2;
3277
3278 status = nfsd4_encode_bitmap(xdr, supp[0], supp[1], supp[2]);
Kinglong Mee75976de2015-07-30 21:55:30 +08003279 if (status)
3280 goto out;
Benny Halevy8c18f202009-04-03 08:29:14 +03003281 }
Andy Adamson7e705702009-04-03 08:29:11 +03003282
J. Bruce Fieldsa8585762018-04-25 13:26:23 -04003283 if (bmval2 & FATTR4_WORD2_CHANGE_ATTR_TYPE) {
3284 p = xdr_reserve_space(xdr, 4);
3285 if (!p)
3286 goto out_resource;
3287 if (IS_I_VERSION(d_inode(dentry)))
3288 *p++ = cpu_to_be32(NFS4_CHANGE_TYPE_IS_MONOTONIC_INCR);
3289 else
3290 *p++ = cpu_to_be32(NFS4_CHANGE_TYPE_IS_TIME_METADATA);
3291 }
3292
Arnd Bergmann0ab88ca2019-03-22 15:07:11 +01003293#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
Kinglong Mee7d580722015-07-30 21:54:26 +08003294 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
3295 status = nfsd4_encode_security_label(xdr, rqstp, context,
3296 contextlen);
3297 if (status)
3298 goto out;
3299 }
Arnd Bergmann0ab88ca2019-03-22 15:07:11 +01003300#endif
Kinglong Mee7d580722015-07-30 21:54:26 +08003301
Frank van der Linden0e885e82020-06-23 22:39:27 +00003302 if (bmval2 & FATTR4_WORD2_XATTR_SUPPORT) {
3303 p = xdr_reserve_space(xdr, 4);
3304 if (!p)
3305 goto out_resource;
3306 err = xattr_supported_namespace(d_inode(dentry),
3307 XATTR_USER_PREFIX);
3308 *p++ = cpu_to_be32(err == 0);
3309 }
3310
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04003311 attrlen = htonl(xdr->buf->len - attrlen_offset - 4);
3312 write_bytes_to_xdr_buf(xdr->buf, attrlen_offset, &attrlen, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 status = nfs_ok;
3314
3315out:
J. Bruce Fieldsba4e55b2013-05-15 10:27:52 -04003316#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
David Quigley18032ca2013-05-02 13:19:10 -04003317 if (context)
3318 security_release_secctx(context, contextlen);
J. Bruce Fieldsba4e55b2013-05-15 10:27:52 -04003319#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08003320 kfree(acl);
Yan, Zheng18df11d02014-03-10 12:52:07 +08003321 if (tempfh) {
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05003322 fh_put(tempfh);
Yan, Zheng18df11d02014-03-10 12:52:07 +08003323 kfree(tempfh);
3324 }
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05003325 if (status)
3326 xdr_truncate_encode(xdr, starting_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 return status;
3328out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07003329 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 goto out;
3331out_resource:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 status = nfserr_resource;
3333 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334}
3335
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04003336static void svcxdr_init_encode_from_buffer(struct xdr_stream *xdr,
3337 struct xdr_buf *buf, __be32 *p, int bytes)
3338{
3339 xdr->scratch.iov_len = 0;
3340 memset(buf, 0, sizeof(struct xdr_buf));
3341 buf->head[0].iov_base = p;
3342 buf->head[0].iov_len = 0;
3343 buf->len = 0;
3344 xdr->buf = buf;
3345 xdr->iov = buf->head;
3346 xdr->p = p;
3347 xdr->end = (void *)p + bytes;
3348 buf->buflen = bytes;
3349}
3350
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04003351__be32 nfsd4_encode_fattr_to_buf(__be32 **p, int words,
3352 struct svc_fh *fhp, struct svc_export *exp,
3353 struct dentry *dentry, u32 *bmval,
3354 struct svc_rqst *rqstp, int ignore_crossmnt)
3355{
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04003356 struct xdr_buf dummy;
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04003357 struct xdr_stream xdr;
3358 __be32 ret;
3359
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04003360 svcxdr_init_encode_from_buffer(&xdr, &dummy, *p, words << 2);
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04003361 ret = nfsd4_encode_fattr(&xdr, fhp, exp, dentry, bmval, rqstp,
3362 ignore_crossmnt);
3363 *p = xdr.p;
3364 return ret;
3365}
3366
J. Bruce Fieldsc0ce6ec2008-02-11 15:48:47 -05003367static inline int attributes_need_mount(u32 *bmval)
3368{
3369 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
3370 return 1;
3371 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
3372 return 1;
3373 return 0;
3374}
3375
Al Virob37ad282006-10-19 23:28:59 -07003376static __be32
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003377nfsd4_encode_dirent_fattr(struct xdr_stream *xdr, struct nfsd4_readdir *cd,
3378 const char *name, int namlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379{
3380 struct svc_export *exp = cd->rd_fhp->fh_export;
3381 struct dentry *dentry;
Al Virob37ad282006-10-19 23:28:59 -07003382 __be32 nfserr;
Frank Filz406a7ea2007-11-27 11:34:05 -08003383 int ignore_crossmnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384
Al Viro6c2d47982019-10-31 01:21:58 -04003385 dentry = lookup_positive_unlocked(name, cd->rd_fhp->fh_dentry, namlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 if (IS_ERR(dentry))
3387 return nfserrno(PTR_ERR(dentry));
3388
3389 exp_get(exp);
Frank Filz406a7ea2007-11-27 11:34:05 -08003390 /*
3391 * In the case of a mountpoint, the client may be asking for
3392 * attributes that are only properties of the underlying filesystem
3393 * as opposed to the cross-mounted file system. In such a case,
3394 * we will not follow the cross mount and will fill the attribtutes
3395 * directly from the mountpoint dentry.
3396 */
J. Bruce Fields3227fa42009-10-25 21:43:01 -04003397 if (nfsd_mountpoint(dentry, exp)) {
J.Bruce Fields021d3a72006-12-13 00:35:24 -08003398 int err;
3399
J. Bruce Fields3227fa42009-10-25 21:43:01 -04003400 if (!(exp->ex_flags & NFSEXP_V4ROOT)
3401 && !attributes_need_mount(cd->rd_bmval)) {
3402 ignore_crossmnt = 1;
3403 goto out_encode;
3404 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07003405 /*
3406 * Why the heck aren't we just using nfsd_lookup??
3407 * Different "."/".." handling? Something else?
3408 * At least, add a comment here to explain....
3409 */
J.Bruce Fields021d3a72006-12-13 00:35:24 -08003410 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
3411 if (err) {
3412 nfserr = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 goto out_put;
3414 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07003415 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
3416 if (nfserr)
3417 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
3419 }
J. Bruce Fields3227fa42009-10-25 21:43:01 -04003420out_encode:
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003421 nfserr = nfsd4_encode_fattr(xdr, NULL, exp, dentry, cd->rd_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08003422 cd->rd_rqstp, ignore_crossmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423out_put:
3424 dput(dentry);
3425 exp_put(exp);
3426 return nfserr;
3427}
3428
Al Viro2ebbc012006-10-19 23:28:58 -07003429static __be32 *
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003430nfsd4_encode_rdattr_error(struct xdr_stream *xdr, __be32 nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431{
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003432 __be32 *p;
3433
Kinglong Meec3a45612014-07-06 11:34:43 +08003434 p = xdr_reserve_space(xdr, 20);
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003435 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 return NULL;
3437 *p++ = htonl(2);
3438 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
3439 *p++ = htonl(0); /* bmval1 */
3440
J. Bruce Fields87915c62013-01-16 17:33:28 -05003441 *p++ = htonl(4); /* attribute length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 *p++ = nfserr; /* no htonl */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 return p;
3444}
3445
3446static int
NeilBrowna0ad13e2007-01-26 00:57:10 -08003447nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
3448 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449{
NeilBrowna0ad13e2007-01-26 00:57:10 -08003450 struct readdir_cd *ccd = ccdv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003452 struct xdr_stream *xdr = cd->xdr;
3453 int start_offset = xdr->buf->len;
3454 int cookie_offset;
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04003455 u32 name_and_cookie;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003456 int entry_bytes;
Al Virob37ad282006-10-19 23:28:59 -07003457 __be32 nfserr = nfserr_toosmall;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003458 __be64 wire_offset;
3459 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
3461 /* In nfsv4, "." and ".." never make it onto the wire.. */
3462 if (name && isdotent(name, namlen)) {
3463 cd->common.err = nfs_ok;
3464 return 0;
3465 }
3466
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003467 if (cd->cookie_offset) {
3468 wire_offset = cpu_to_be64(offset);
3469 write_bytes_to_xdr_buf(xdr->buf, cd->cookie_offset,
3470 &wire_offset, 8);
3471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003473 p = xdr_reserve_space(xdr, 4);
3474 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 *p++ = xdr_one; /* mark entry present */
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003477 cookie_offset = xdr->buf->len;
3478 p = xdr_reserve_space(xdr, 3*4 + namlen);
3479 if (!p)
3480 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
3482 p = xdr_encode_array(p, name, namlen); /* name length & name */
3483
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003484 nfserr = nfsd4_encode_dirent_fattr(xdr, cd, name, namlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 switch (nfserr) {
3486 case nfs_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 break;
3488 case nfserr_resource:
3489 nfserr = nfserr_toosmall;
3490 goto fail;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04003491 case nfserr_noent:
Kinglong Meef41c5ad2014-06-13 17:32:42 +08003492 xdr_truncate_encode(xdr, start_offset);
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04003493 goto skip_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 default:
3495 /*
3496 * If the client requested the RDATTR_ERROR attribute,
3497 * we stuff the error code into this attribute
3498 * and continue. If this attribute was not requested,
3499 * then in accordance with the spec, we fail the
3500 * entire READDIR operation(!)
3501 */
3502 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
3503 goto fail;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003504 p = nfsd4_encode_rdattr_error(xdr, nfserr);
Fred Isaman34081ef2006-01-18 17:43:40 -08003505 if (p == NULL) {
3506 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 goto fail;
Fred Isaman34081ef2006-01-18 17:43:40 -08003508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 }
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003510 nfserr = nfserr_toosmall;
3511 entry_bytes = xdr->buf->len - start_offset;
3512 if (entry_bytes > cd->rd_maxcount)
3513 goto fail;
3514 cd->rd_maxcount -= entry_bytes;
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04003515 /*
3516 * RFC 3530 14.2.24 describes rd_dircount as only a "hint", so
3517 * let's always let through the first entry, at least:
3518 */
J. Bruce Fields0ec016e2014-12-19 18:01:35 -05003519 if (!cd->rd_dircount)
3520 goto fail;
3521 name_and_cookie = 4 + 4 * XDR_QUADLEN(namlen) + 8;
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04003522 if (name_and_cookie > cd->rd_dircount && cd->cookie_offset)
3523 goto fail;
3524 cd->rd_dircount -= min(cd->rd_dircount, name_and_cookie);
J. Bruce Fields0ec016e2014-12-19 18:01:35 -05003525
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003526 cd->cookie_offset = cookie_offset;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04003527skip_entry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 cd->common.err = nfs_ok;
3529 return 0;
3530fail:
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003531 xdr_truncate_encode(xdr, start_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 cd->common.err = nfserr;
3533 return -EINVAL;
3534}
3535
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003536static __be32
3537nfsd4_encode_stateid(struct xdr_stream *xdr, stateid_t *sid)
Benny Halevye2f282b2008-08-12 20:45:07 +03003538{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003539 __be32 *p;
Benny Halevye2f282b2008-08-12 20:45:07 +03003540
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003541 p = xdr_reserve_space(xdr, sizeof(stateid_t));
3542 if (!p)
3543 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003544 *p++ = cpu_to_be32(sid->si_generation);
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003545 p = xdr_encode_opaque_fixed(p, &sid->si_opaque,
3546 sizeof(stateid_opaque_t));
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003547 return 0;
Benny Halevye2f282b2008-08-12 20:45:07 +03003548}
3549
Benny Halevy695e12f2008-07-04 14:38:33 +03003550static __be32
Al Virob37ad282006-10-19 23:28:59 -07003551nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003553 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003554 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003556 p = xdr_reserve_space(xdr, 8);
3557 if (!p)
3558 return nfserr_resource;
3559 *p++ = cpu_to_be32(access->ac_supported);
3560 *p++ = cpu_to_be32(access->ac_resp_access);
3561 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562}
3563
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003564static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
3565{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003566 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003567 __be32 *p;
3568
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003569 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 8);
3570 if (!p)
3571 return nfserr_resource;
3572 p = xdr_encode_opaque_fixed(p, bcts->sessionid.data,
3573 NFS4_MAX_SESSIONID_LEN);
3574 *p++ = cpu_to_be32(bcts->dir);
3575 /* Upshifting from TCP to RDMA is not supported */
3576 *p++ = cpu_to_be32(0);
3577 return 0;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003578}
3579
Benny Halevy695e12f2008-07-04 14:38:33 +03003580static __be32
Al Virob37ad282006-10-19 23:28:59 -07003581nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003583 struct xdr_stream *xdr = &resp->xdr;
3584
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003585 return nfsd4_encode_stateid(xdr, &close->cl_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586}
3587
3588
Benny Halevy695e12f2008-07-04 14:38:33 +03003589static __be32
Al Virob37ad282006-10-19 23:28:59 -07003590nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003592 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003593 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003595 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
3596 if (!p)
3597 return nfserr_resource;
3598 p = xdr_encode_opaque_fixed(p, commit->co_verf.data,
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003599 NFS4_VERIFIER_SIZE);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003600 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601}
3602
Benny Halevy695e12f2008-07-04 14:38:33 +03003603static __be32
Al Virob37ad282006-10-19 23:28:59 -07003604nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003606 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003607 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003609 p = xdr_reserve_space(xdr, 20);
3610 if (!p)
3611 return nfserr_resource;
3612 encode_cinfo(p, &create->cr_cinfo);
Trond Myklebustb96811c2019-08-18 14:18:58 -04003613 return nfsd4_encode_bitmap(xdr, create->cr_bmval[0],
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003614 create->cr_bmval[1], create->cr_bmval[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615}
3616
Al Virob37ad282006-10-19 23:28:59 -07003617static __be32
3618nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619{
3620 struct svc_fh *fhp = getattr->ga_fhp;
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04003621 struct xdr_stream *xdr = &resp->xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003623 return nfsd4_encode_fattr(xdr, fhp, fhp->fh_export, fhp->fh_dentry,
3624 getattr->ga_bmval, resp->rqstp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625}
3626
Benny Halevy695e12f2008-07-04 14:38:33 +03003627static __be32
3628nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003630 struct xdr_stream *xdr = &resp->xdr;
Benny Halevy695e12f2008-07-04 14:38:33 +03003631 struct svc_fh *fhp = *fhpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 unsigned int len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003633 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003635 len = fhp->fh_handle.fh_size;
3636 p = xdr_reserve_space(xdr, len + 4);
3637 if (!p)
3638 return nfserr_resource;
3639 p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base, len);
3640 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641}
3642
3643/*
3644* Including all fields other than the name, a LOCK4denied structure requires
3645* 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
3646*/
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003647static __be32
3648nfsd4_encode_lock_denied(struct xdr_stream *xdr, struct nfsd4_lock_denied *ld)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649{
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003650 struct xdr_netobj *conf = &ld->ld_owner;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003651 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003653again:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003654 p = xdr_reserve_space(xdr, 32 + XDR_LEN(conf->len));
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003655 if (!p) {
3656 /*
3657 * Don't fail to return the result just because we can't
3658 * return the conflicting open:
3659 */
3660 if (conf->len) {
Kinglong Meef98bac52014-07-07 22:10:56 +08003661 kfree(conf->data);
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003662 conf->len = 0;
3663 conf->data = NULL;
3664 goto again;
3665 }
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003666 return nfserr_resource;
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003667 }
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003668 p = xdr_encode_hyper(p, ld->ld_start);
3669 p = xdr_encode_hyper(p, ld->ld_length);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003670 *p++ = cpu_to_be32(ld->ld_type);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003671 if (conf->len) {
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003672 p = xdr_encode_opaque_fixed(p, &ld->ld_clientid, 8);
3673 p = xdr_encode_opaque(p, conf->data, conf->len);
Kinglong Meef98bac52014-07-07 22:10:56 +08003674 kfree(conf->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003676 p = xdr_encode_hyper(p, (u64)0); /* clientid */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003677 *p++ = cpu_to_be32(0); /* length of owner name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 }
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003679 return nfserr_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680}
3681
Benny Halevy695e12f2008-07-04 14:38:33 +03003682static __be32
Al Virob37ad282006-10-19 23:28:59 -07003683nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003685 struct xdr_stream *xdr = &resp->xdr;
3686
Benny Halevye2f282b2008-08-12 20:45:07 +03003687 if (!nfserr)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003688 nfserr = nfsd4_encode_stateid(xdr, &lock->lk_resp_stateid);
Benny Halevye2f282b2008-08-12 20:45:07 +03003689 else if (nfserr == nfserr_denied)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003690 nfserr = nfsd4_encode_lock_denied(xdr, &lock->lk_denied);
Kinglong Meef98bac52014-07-07 22:10:56 +08003691
Benny Halevy695e12f2008-07-04 14:38:33 +03003692 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693}
3694
Benny Halevy695e12f2008-07-04 14:38:33 +03003695static __be32
Al Virob37ad282006-10-19 23:28:59 -07003696nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003698 struct xdr_stream *xdr = &resp->xdr;
3699
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 if (nfserr == nfserr_denied)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003701 nfsd4_encode_lock_denied(xdr, &lockt->lt_denied);
Benny Halevy695e12f2008-07-04 14:38:33 +03003702 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703}
3704
Benny Halevy695e12f2008-07-04 14:38:33 +03003705static __be32
Al Virob37ad282006-10-19 23:28:59 -07003706nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003708 struct xdr_stream *xdr = &resp->xdr;
3709
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003710 return nfsd4_encode_stateid(xdr, &locku->lu_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711}
3712
3713
Benny Halevy695e12f2008-07-04 14:38:33 +03003714static __be32
Al Virob37ad282006-10-19 23:28:59 -07003715nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003717 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003718 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003720 p = xdr_reserve_space(xdr, 20);
3721 if (!p)
3722 return nfserr_resource;
3723 p = encode_cinfo(p, &link->li_cinfo);
3724 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725}
3726
3727
Benny Halevy695e12f2008-07-04 14:38:33 +03003728static __be32
Al Virob37ad282006-10-19 23:28:59 -07003729nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003731 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003732 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003734 nfserr = nfsd4_encode_stateid(xdr, &open->op_stateid);
3735 if (nfserr)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003736 return nfserr;
Kinglong Mee75976de2015-07-30 21:55:30 +08003737 p = xdr_reserve_space(xdr, 24);
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003738 if (!p)
3739 return nfserr_resource;
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04003740 p = encode_cinfo(p, &open->op_cinfo);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003741 *p++ = cpu_to_be32(open->op_rflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742
Kinglong Mee75976de2015-07-30 21:55:30 +08003743 nfserr = nfsd4_encode_bitmap(xdr, open->op_bmval[0], open->op_bmval[1],
3744 open->op_bmval[2]);
3745 if (nfserr)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003746 return nfserr;
Kinglong Mee75976de2015-07-30 21:55:30 +08003747
3748 p = xdr_reserve_space(xdr, 4);
3749 if (!p)
3750 return nfserr_resource;
3751
3752 *p++ = cpu_to_be32(open->op_delegate_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 switch (open->op_delegate_type) {
3754 case NFS4_OPEN_DELEGATE_NONE:
3755 break;
3756 case NFS4_OPEN_DELEGATE_READ:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003757 nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
3758 if (nfserr)
3759 return nfserr;
3760 p = xdr_reserve_space(xdr, 20);
3761 if (!p)
3762 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003763 *p++ = cpu_to_be32(open->op_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764
3765 /*
3766 * TODO: ACE's in delegations
3767 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003768 *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
3769 *p++ = cpu_to_be32(0);
3770 *p++ = cpu_to_be32(0);
3771 *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 break;
3773 case NFS4_OPEN_DELEGATE_WRITE:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003774 nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
3775 if (nfserr)
3776 return nfserr;
3777 p = xdr_reserve_space(xdr, 32);
3778 if (!p)
3779 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003780 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
3782 /*
3783 * TODO: space_limit's in delegations
3784 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003785 *p++ = cpu_to_be32(NFS4_LIMIT_SIZE);
3786 *p++ = cpu_to_be32(~(u32)0);
3787 *p++ = cpu_to_be32(~(u32)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788
3789 /*
3790 * TODO: ACE's in delegations
3791 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003792 *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
3793 *p++ = cpu_to_be32(0);
3794 *p++ = cpu_to_be32(0);
3795 *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 break;
Benny Halevyd24433c2012-02-16 20:57:17 +02003797 case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
3798 switch (open->op_why_no_deleg) {
3799 case WND4_CONTENTION:
3800 case WND4_RESOURCE:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003801 p = xdr_reserve_space(xdr, 8);
3802 if (!p)
3803 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003804 *p++ = cpu_to_be32(open->op_why_no_deleg);
3805 /* deleg signaling not supported yet: */
3806 *p++ = cpu_to_be32(0);
Benny Halevyd24433c2012-02-16 20:57:17 +02003807 break;
3808 default:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003809 p = xdr_reserve_space(xdr, 4);
3810 if (!p)
3811 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003812 *p++ = cpu_to_be32(open->op_why_no_deleg);
Benny Halevyd24433c2012-02-16 20:57:17 +02003813 }
Benny Halevyd24433c2012-02-16 20:57:17 +02003814 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 default:
3816 BUG();
3817 }
3818 /* XXX save filehandle here */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003819 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820}
3821
Benny Halevy695e12f2008-07-04 14:38:33 +03003822static __be32
Al Virob37ad282006-10-19 23:28:59 -07003823nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003825 struct xdr_stream *xdr = &resp->xdr;
3826
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003827 return nfsd4_encode_stateid(xdr, &oc->oc_resp_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828}
3829
Benny Halevy695e12f2008-07-04 14:38:33 +03003830static __be32
Al Virob37ad282006-10-19 23:28:59 -07003831nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003833 struct xdr_stream *xdr = &resp->xdr;
3834
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003835 return nfsd4_encode_stateid(xdr, &od->od_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836}
3837
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003838static __be32 nfsd4_encode_splice_read(
3839 struct nfsd4_compoundres *resp,
3840 struct nfsd4_read *read,
3841 struct file *file, unsigned long maxcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842{
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003843 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003844 struct xdr_buf *buf = xdr->buf;
Chuck Lever76e54922020-11-05 10:24:19 -05003845 int status, space_left;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003846 u32 eof;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003847 __be32 nfserr;
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003848 __be32 *p = xdr->p - 2;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003849
Kinglong Meed5d5c302014-07-09 21:51:27 +08003850 /* Make sure there will be room for padding if needed */
3851 if (xdr->end - xdr->p < 1)
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003852 return nfserr_resource;
3853
Chuck Lever87c59422018-03-28 13:29:11 -04003854 nfserr = nfsd_splice_read(read->rd_rqstp, read->rd_fhp,
Trond Myklebust83a63072019-08-26 13:03:11 -04003855 file, read->rd_offset, &maxcount, &eof);
Chuck Lever87c59422018-03-28 13:29:11 -04003856 read->rd_length = maxcount;
Chuck Lever76e54922020-11-05 10:24:19 -05003857 if (nfserr)
3858 goto out_err;
3859 status = svc_encode_result_payload(read->rd_rqstp,
3860 buf->head[0].iov_len, maxcount);
3861 if (status) {
3862 nfserr = nfserrno(status);
3863 goto out_err;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003864 }
3865
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003866 *(p++) = htonl(eof);
3867 *(p++) = htonl(maxcount);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003868
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003869 buf->page_len = maxcount;
3870 buf->len += maxcount;
J. Bruce Fields15b23ef2014-09-24 16:32:34 -04003871 xdr->page_ptr += (buf->page_base + maxcount + PAGE_SIZE - 1)
3872 / PAGE_SIZE;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003873
3874 /* Use rest of head for padding and remaining ops: */
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003875 buf->tail[0].iov_base = xdr->p;
3876 buf->tail[0].iov_len = 0;
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003877 xdr->iov = buf->tail;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003878 if (maxcount&3) {
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003879 int pad = 4 - (maxcount&3);
3880
3881 *(xdr->p++) = 0;
3882
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003883 buf->tail[0].iov_base += maxcount&3;
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003884 buf->tail[0].iov_len = pad;
3885 buf->len += pad;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003886 }
3887
3888 space_left = min_t(int, (void *)xdr->end - (void *)xdr->p,
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003889 buf->buflen - buf->len);
3890 buf->buflen = buf->len + space_left;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003891 xdr->end = (__be32 *)((void *)xdr->end + space_left);
3892
3893 return 0;
Chuck Lever76e54922020-11-05 10:24:19 -05003894
3895out_err:
3896 /*
3897 * nfsd_splice_actor may have already messed with the
3898 * page length; reset it so as not to confuse
3899 * xdr_truncate_encode in our caller.
3900 */
3901 buf->page_len = 0;
3902 return nfserr;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003903}
3904
3905static __be32 nfsd4_encode_readv(struct nfsd4_compoundres *resp,
3906 struct nfsd4_read *read,
3907 struct file *file, unsigned long maxcount)
3908{
3909 struct xdr_stream *xdr = &resp->xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 u32 eof;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003911 int starting_len = xdr->buf->len - 8;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003912 __be32 nfserr;
3913 __be32 tmp;
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003914 int pad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915
Anna Schumaker403217f2020-08-17 12:53:06 -04003916 read->rd_vlen = xdr_reserve_space_vec(xdr, resp->rqstp->rq_vec, maxcount);
3917 if (read->rd_vlen < 0)
3918 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919
Chuck Lever87c59422018-03-28 13:29:11 -04003920 nfserr = nfsd_readv(resp->rqstp, read->rd_fhp, file, read->rd_offset,
Trond Myklebust83a63072019-08-26 13:03:11 -04003921 resp->rqstp->rq_vec, read->rd_vlen, &maxcount,
3922 &eof);
Chuck Lever87c59422018-03-28 13:29:11 -04003923 read->rd_length = maxcount;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003924 if (nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 return nfserr;
Chuck Lever03493bc2020-06-10 10:36:42 -04003926 if (svc_encode_result_payload(resp->rqstp, starting_len + 8, maxcount))
Chuck Lever41205532020-03-02 14:45:53 -05003927 return nfserr_io;
Chuck Lever7dcf4ab2020-03-02 15:00:04 -05003928 xdr_truncate_encode(xdr, starting_len + 8 + xdr_align_size(maxcount));
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003929
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003930 tmp = htonl(eof);
3931 write_bytes_to_xdr_buf(xdr->buf, starting_len , &tmp, 4);
3932 tmp = htonl(maxcount);
3933 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp, 4);
3934
Chuck Lever7dcf4ab2020-03-02 15:00:04 -05003935 tmp = xdr_zero;
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003936 pad = (maxcount&3) ? 4 - (maxcount&3) : 0;
3937 write_bytes_to_xdr_buf(xdr->buf, starting_len + 8 + maxcount,
Chuck Lever7dcf4ab2020-03-02 15:00:04 -05003938 &tmp, pad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 return 0;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003940
3941}
3942
3943static __be32
3944nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
3945 struct nfsd4_read *read)
3946{
3947 unsigned long maxcount;
3948 struct xdr_stream *xdr = &resp->xdr;
Jeff Layton5c4583b2019-08-18 14:18:54 -04003949 struct file *file;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003950 int starting_len = xdr->buf->len;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003951 __be32 *p;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003952
Jeff Layton5c4583b2019-08-18 14:18:54 -04003953 if (nfserr)
3954 return nfserr;
3955 file = read->rd_nf->nf_file;
3956
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003957 p = xdr_reserve_space(xdr, 8); /* eof flag and byte count */
3958 if (!p) {
Jeff Layton779fb0f2014-11-19 07:51:18 -05003959 WARN_ON_ONCE(test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags));
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003960 return nfserr_resource;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003961 }
Christoph Hellwig68e8bb02015-06-18 16:45:02 +02003962 if (resp->xdr.buf->page_len &&
3963 test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags)) {
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003964 WARN_ON_ONCE(1);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003965 return nfserr_resource;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003966 }
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003967 xdr_commit_encode(xdr);
3968
3969 maxcount = svc_max_payload(resp->rqstp);
Christoph Hellwig68e8bb02015-06-18 16:45:02 +02003970 maxcount = min_t(unsigned long, maxcount,
3971 (xdr->buf->buflen - xdr->buf->len));
Kinglong Mee3c7aa152014-06-10 18:08:19 +08003972 maxcount = min_t(unsigned long, maxcount, read->rd_length);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003973
Christoph Hellwig68e8bb02015-06-18 16:45:02 +02003974 if (file->f_op->splice_read &&
3975 test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags))
Christoph Hellwig96bcad52015-06-18 16:45:01 +02003976 nfserr = nfsd4_encode_splice_read(resp, read, file, maxcount);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003977 else
Christoph Hellwig96bcad52015-06-18 16:45:01 +02003978 nfserr = nfsd4_encode_readv(resp, read, file, maxcount);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003979
Christoph Hellwig96bcad52015-06-18 16:45:01 +02003980 if (nfserr)
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003981 xdr_truncate_encode(xdr, starting_len);
Christoph Hellwig96bcad52015-06-18 16:45:01 +02003982
Christoph Hellwig96bcad52015-06-18 16:45:01 +02003983 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984}
3985
Al Virob37ad282006-10-19 23:28:59 -07003986static __be32
3987nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988{
3989 int maxcount;
J. Bruce Fields476a7b12014-01-20 17:08:27 -05003990 __be32 wire_count;
3991 int zero = 0;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003992 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05003993 int length_offset = xdr->buf->len;
Chuck Lever76e54922020-11-05 10:24:19 -05003994 int status;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003995 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04003997 p = xdr_reserve_space(xdr, 4);
3998 if (!p)
3999 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 maxcount = PAGE_SIZE;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004001
J. Bruce Fields476a7b12014-01-20 17:08:27 -05004002 p = xdr_reserve_space(xdr, maxcount);
4003 if (!p)
J. Bruce Fields4e21ac42014-03-22 15:15:11 -04004004 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 /*
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004006 * XXX: By default, vfs_readlink() will truncate symlinks if they
4007 * would overflow the buffer. Is this kosher in NFSv4? If not, one
4008 * easy fix is: if vfs_readlink() precisely fills the buffer, assume
4009 * that truncation occurred, and return NFS4ERR_RESOURCE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 */
J. Bruce Fields476a7b12014-01-20 17:08:27 -05004011 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp,
4012 (char *)p, &maxcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 if (nfserr == nfserr_isdir)
J. Bruce Fieldsd3f627c2014-02-26 17:00:38 -05004014 nfserr = nfserr_inval;
Chuck Lever76e54922020-11-05 10:24:19 -05004015 if (nfserr)
4016 goto out_err;
4017 status = svc_encode_result_payload(readlink->rl_rqstp, length_offset,
4018 maxcount);
4019 if (status) {
4020 nfserr = nfserrno(status);
4021 goto out_err;
J. Bruce Fieldsd3f627c2014-02-26 17:00:38 -05004022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023
J. Bruce Fields476a7b12014-01-20 17:08:27 -05004024 wire_count = htonl(maxcount);
4025 write_bytes_to_xdr_buf(xdr->buf, length_offset, &wire_count, 4);
Avi Kivity69bbd9c2014-06-26 17:23:12 +03004026 xdr_truncate_encode(xdr, length_offset + 4 + ALIGN(maxcount, 4));
J. Bruce Fields476a7b12014-01-20 17:08:27 -05004027 if (maxcount & 3)
4028 write_bytes_to_xdr_buf(xdr->buf, length_offset + 4 + maxcount,
4029 &zero, 4 - (maxcount&3));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 return 0;
Chuck Lever76e54922020-11-05 10:24:19 -05004031
4032out_err:
4033 xdr_truncate_encode(xdr, length_offset);
4034 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035}
4036
Al Virob37ad282006-10-19 23:28:59 -07004037static __be32
4038nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039{
4040 int maxcount;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004041 int bytes_left;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 loff_t offset;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004043 __be64 wire_offset;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04004044 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05004045 int starting_len = xdr->buf->len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004046 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004048 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
4049 if (!p)
4050 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051
4052 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004053 *p++ = cpu_to_be32(0);
4054 *p++ = cpu_to_be32(0);
J. Bruce Fields4aea24b2014-01-15 15:17:58 -05004055 resp->xdr.buf->head[0].iov_len = ((char *)resp->xdr.p)
4056 - (char *)resp->xdr.buf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057
4058 /*
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004059 * Number of bytes left for directory entries allowing for the
4060 * final 8 bytes of the readdir and a following failed op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 */
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004062 bytes_left = xdr->buf->buflen - xdr->buf->len
4063 - COMPOUND_ERR_SLACK_SPACE - 8;
4064 if (bytes_left < 0) {
4065 nfserr = nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 goto err_no_verf;
4067 }
Scott Mayhew9c2ece62018-05-07 09:01:08 -04004068 maxcount = svc_max_payload(resp->rqstp);
4069 maxcount = min_t(u32, readdir->rd_maxcount, maxcount);
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004070 /*
4071 * Note the rfc defines rd_maxcount as the size of the
4072 * READDIR4resok structure, which includes the verifier above
4073 * and the 8 bytes encoded at the end of this function:
4074 */
4075 if (maxcount < 16) {
4076 nfserr = nfserr_toosmall;
4077 goto err_no_verf;
4078 }
4079 maxcount = min_t(int, maxcount-16, bytes_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04004081 /* RFC 3530 14.2.24 allows us to ignore dircount when it's 0: */
4082 if (!readdir->rd_dircount)
Scott Mayhew9c2ece62018-05-07 09:01:08 -04004083 readdir->rd_dircount = svc_max_payload(resp->rqstp);
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04004084
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004085 readdir->xdr = xdr;
4086 readdir->rd_maxcount = maxcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 readdir->common.err = 0;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004088 readdir->cookie_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089
4090 offset = readdir->rd_cookie;
4091 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
4092 &offset,
4093 &readdir->common, nfsd4_encode_dirent);
4094 if (nfserr == nfs_ok &&
4095 readdir->common.err == nfserr_toosmall &&
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004096 xdr->buf->len == starting_len + 8) {
4097 /* nothing encoded; which limit did we hit?: */
4098 if (maxcount - 16 < bytes_left)
4099 /* It was the fault of rd_maxcount: */
4100 nfserr = nfserr_toosmall;
4101 else
4102 /* We ran out of buffer space: */
4103 nfserr = nfserr_resource;
4104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 if (nfserr)
4106 goto err_no_verf;
4107
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004108 if (readdir->cookie_offset) {
4109 wire_offset = cpu_to_be64(offset);
4110 write_bytes_to_xdr_buf(xdr->buf, readdir->cookie_offset,
4111 &wire_offset, 8);
4112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004114 p = xdr_reserve_space(xdr, 8);
4115 if (!p) {
4116 WARN_ON_ONCE(1);
4117 goto err_no_verf;
4118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 *p++ = 0; /* no more entries */
4120 *p++ = htonl(readdir->common.err == nfserr_eof);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121
4122 return 0;
4123err_no_verf:
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05004124 xdr_truncate_encode(xdr, starting_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 return nfserr;
4126}
4127
Benny Halevy695e12f2008-07-04 14:38:33 +03004128static __be32
Al Virob37ad282006-10-19 23:28:59 -07004129nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004131 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004132 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004134 p = xdr_reserve_space(xdr, 20);
4135 if (!p)
4136 return nfserr_resource;
4137 p = encode_cinfo(p, &remove->rm_cinfo);
4138 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139}
4140
Benny Halevy695e12f2008-07-04 14:38:33 +03004141static __be32
Al Virob37ad282006-10-19 23:28:59 -07004142nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004144 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004145 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004147 p = xdr_reserve_space(xdr, 40);
4148 if (!p)
4149 return nfserr_resource;
4150 p = encode_cinfo(p, &rename->rn_sinfo);
4151 p = encode_cinfo(p, &rename->rn_tinfo);
4152 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153}
4154
Benny Halevy695e12f2008-07-04 14:38:33 +03004155static __be32
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004156nfsd4_do_encode_secinfo(struct xdr_stream *xdr, struct svc_export *exp)
Andy Adamsondcb488a32007-07-17 04:04:51 -07004157{
Chuck Lever676e4eb2013-04-30 18:48:54 -04004158 u32 i, nflavs, supported;
J. Bruce Fields4796f452007-07-17 04:04:51 -07004159 struct exp_flavor_info *flavs;
4160 struct exp_flavor_info def_flavs[2];
Chuck Lever676e4eb2013-04-30 18:48:54 -04004161 __be32 *p, *flavorsp;
4162 static bool report = true;
Andy Adamsondcb488a32007-07-17 04:04:51 -07004163
J. Bruce Fields4796f452007-07-17 04:04:51 -07004164 if (exp->ex_nflavors) {
4165 flavs = exp->ex_flavors;
4166 nflavs = exp->ex_nflavors;
4167 } else { /* Handling of some defaults in absence of real secinfo: */
4168 flavs = def_flavs;
4169 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
4170 nflavs = 2;
4171 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
4172 flavs[1].pseudoflavor = RPC_AUTH_NULL;
4173 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
4174 nflavs = 1;
4175 flavs[0].pseudoflavor
4176 = svcauth_gss_flavor(exp->ex_client);
4177 } else {
4178 nflavs = 1;
4179 flavs[0].pseudoflavor
4180 = exp->ex_client->flavour->flavour;
4181 }
4182 }
4183
Chuck Lever676e4eb2013-04-30 18:48:54 -04004184 supported = 0;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004185 p = xdr_reserve_space(xdr, 4);
4186 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004187 return nfserr_resource;
Chuck Lever676e4eb2013-04-30 18:48:54 -04004188 flavorsp = p++; /* to be backfilled later */
Chuck Lever676e4eb2013-04-30 18:48:54 -04004189
J. Bruce Fields4796f452007-07-17 04:04:51 -07004190 for (i = 0; i < nflavs; i++) {
Chuck Lever676e4eb2013-04-30 18:48:54 -04004191 rpc_authflavor_t pf = flavs[i].pseudoflavor;
Chuck Levera77c8062013-03-16 15:55:10 -04004192 struct rpcsec_gss_info info;
Andy Adamsondcb488a32007-07-17 04:04:51 -07004193
Chuck Lever676e4eb2013-04-30 18:48:54 -04004194 if (rpcauth_get_gssinfo(pf, &info) == 0) {
4195 supported++;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004196 p = xdr_reserve_space(xdr, 4 + 4 +
4197 XDR_LEN(info.oid.len) + 4 + 4);
4198 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004199 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004200 *p++ = cpu_to_be32(RPC_AUTH_GSS);
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004201 p = xdr_encode_opaque(p, info.oid.data, info.oid.len);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004202 *p++ = cpu_to_be32(info.qop);
4203 *p++ = cpu_to_be32(info.service);
Chuck Lever676e4eb2013-04-30 18:48:54 -04004204 } else if (pf < RPC_AUTH_MAXFLAVOR) {
4205 supported++;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004206 p = xdr_reserve_space(xdr, 4);
4207 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004208 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004209 *p++ = cpu_to_be32(pf);
Chuck Lever676e4eb2013-04-30 18:48:54 -04004210 } else {
4211 if (report)
4212 pr_warn("NFS: SECINFO: security flavor %u "
4213 "is not supported\n", pf);
Andy Adamsondcb488a32007-07-17 04:04:51 -07004214 }
4215 }
Chuck Levera77c8062013-03-16 15:55:10 -04004216
Chuck Lever676e4eb2013-04-30 18:48:54 -04004217 if (nflavs != supported)
4218 report = false;
4219 *flavorsp = htonl(supported);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004220 return 0;
Andy Adamsondcb488a32007-07-17 04:04:51 -07004221}
4222
Mi Jinlong22b6dee2010-12-27 14:29:57 +08004223static __be32
4224nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
4225 struct nfsd4_secinfo *secinfo)
4226{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004227 struct xdr_stream *xdr = &resp->xdr;
4228
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004229 return nfsd4_do_encode_secinfo(xdr, secinfo->si_exp);
Mi Jinlong22b6dee2010-12-27 14:29:57 +08004230}
4231
4232static __be32
4233nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
4234 struct nfsd4_secinfo_no_name *secinfo)
4235{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004236 struct xdr_stream *xdr = &resp->xdr;
4237
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004238 return nfsd4_do_encode_secinfo(xdr, secinfo->sin_exp);
Mi Jinlong22b6dee2010-12-27 14:29:57 +08004239}
4240
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241/*
4242 * The SETATTR encode routine is special -- it always encodes a bitmap,
4243 * regardless of the error status.
4244 */
Benny Halevy695e12f2008-07-04 14:38:33 +03004245static __be32
Al Virob37ad282006-10-19 23:28:59 -07004246nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004248 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004249 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004251 p = xdr_reserve_space(xdr, 16);
4252 if (!p)
4253 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 if (nfserr) {
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004255 *p++ = cpu_to_be32(3);
4256 *p++ = cpu_to_be32(0);
4257 *p++ = cpu_to_be32(0);
4258 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 }
4260 else {
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004261 *p++ = cpu_to_be32(3);
4262 *p++ = cpu_to_be32(setattr->sa_bmval[0]);
4263 *p++ = cpu_to_be32(setattr->sa_bmval[1]);
4264 *p++ = cpu_to_be32(setattr->sa_bmval[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 }
Benny Halevy695e12f2008-07-04 14:38:33 +03004266 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267}
4268
Benny Halevy695e12f2008-07-04 14:38:33 +03004269static __be32
Al Virob37ad282006-10-19 23:28:59 -07004270nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004272 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004273 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274
4275 if (!nfserr) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004276 p = xdr_reserve_space(xdr, 8 + NFS4_VERIFIER_SIZE);
4277 if (!p)
4278 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004279 p = xdr_encode_opaque_fixed(p, &scd->se_clientid, 8);
4280 p = xdr_encode_opaque_fixed(p, &scd->se_confirm,
4281 NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 }
4283 else if (nfserr == nfserr_clid_inuse) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004284 p = xdr_reserve_space(xdr, 8);
4285 if (!p)
4286 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004287 *p++ = cpu_to_be32(0);
4288 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 }
Benny Halevy695e12f2008-07-04 14:38:33 +03004290 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291}
4292
Benny Halevy695e12f2008-07-04 14:38:33 +03004293static __be32
Al Virob37ad282006-10-19 23:28:59 -07004294nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004296 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004297 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004299 p = xdr_reserve_space(xdr, 16);
4300 if (!p)
4301 return nfserr_resource;
4302 *p++ = cpu_to_be32(write->wr_bytes_written);
4303 *p++ = cpu_to_be32(write->wr_how_written);
4304 p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
4305 NFS4_VERIFIER_SIZE);
4306 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307}
4308
Benny Halevy695e12f2008-07-04 14:38:33 +03004309static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004310nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03004311 struct nfsd4_exchange_id *exid)
4312{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004313 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004314 __be32 *p;
Andy Adamson0733d212009-04-03 08:28:01 +03004315 char *major_id;
4316 char *server_scope;
4317 int major_id_sz;
4318 int server_scope_sz;
4319 uint64_t minor_id = 0;
Scott Mayhew7627d7d2020-02-19 15:52:15 -05004320 struct nfsd_net *nn = net_generic(SVC_NET(resp->rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03004321
Scott Mayhew7627d7d2020-02-19 15:52:15 -05004322 major_id = nn->nfsd_name;
4323 major_id_sz = strlen(nn->nfsd_name);
4324 server_scope = nn->nfsd_name;
4325 server_scope_sz = strlen(nn->nfsd_name);
Andy Adamson0733d212009-04-03 08:28:01 +03004326
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004327 p = xdr_reserve_space(xdr,
Andy Adamson0733d212009-04-03 08:28:01 +03004328 8 /* eir_clientid */ +
4329 4 /* eir_sequenceid */ +
4330 4 /* eir_flags */ +
Kinglong Meea8bb84b2013-12-10 15:24:36 +08004331 4 /* spr_how */);
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004332 if (!p)
4333 return nfserr_resource;
Andy Adamson0733d212009-04-03 08:28:01 +03004334
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004335 p = xdr_encode_opaque_fixed(p, &exid->clientid, 8);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004336 *p++ = cpu_to_be32(exid->seqid);
4337 *p++ = cpu_to_be32(exid->flags);
Andy Adamson0733d212009-04-03 08:28:01 +03004338
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004339 *p++ = cpu_to_be32(exid->spa_how);
Kinglong Meea8bb84b2013-12-10 15:24:36 +08004340
J. Bruce Fields57266a62013-04-13 14:27:29 -04004341 switch (exid->spa_how) {
4342 case SP4_NONE:
4343 break;
4344 case SP4_MACH_CRED:
4345 /* spo_must_enforce bitmap: */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004346 nfserr = nfsd4_encode_bitmap(xdr,
Andrew Elbleed941642016-06-15 12:52:09 -04004347 exid->spo_must_enforce[0],
4348 exid->spo_must_enforce[1],
4349 exid->spo_must_enforce[2]);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004350 if (nfserr)
4351 return nfserr;
Andrew Elbleed941642016-06-15 12:52:09 -04004352 /* spo_must_allow bitmap: */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004353 nfserr = nfsd4_encode_bitmap(xdr,
Andrew Elbleed941642016-06-15 12:52:09 -04004354 exid->spo_must_allow[0],
4355 exid->spo_must_allow[1],
4356 exid->spo_must_allow[2]);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004357 if (nfserr)
4358 return nfserr;
J. Bruce Fields57266a62013-04-13 14:27:29 -04004359 break;
4360 default:
4361 WARN_ON_ONCE(1);
4362 }
Andy Adamson0733d212009-04-03 08:28:01 +03004363
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004364 p = xdr_reserve_space(xdr,
Kinglong Meea8bb84b2013-12-10 15:24:36 +08004365 8 /* so_minor_id */ +
4366 4 /* so_major_id.len */ +
4367 (XDR_QUADLEN(major_id_sz) * 4) +
4368 4 /* eir_server_scope.len */ +
4369 (XDR_QUADLEN(server_scope_sz) * 4) +
4370 4 /* eir_server_impl_id.count (0) */);
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004371 if (!p)
4372 return nfserr_resource;
Kinglong Meea8bb84b2013-12-10 15:24:36 +08004373
Andy Adamson0733d212009-04-03 08:28:01 +03004374 /* The server_owner struct */
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04004375 p = xdr_encode_hyper(p, minor_id); /* Minor id */
Andy Adamson0733d212009-04-03 08:28:01 +03004376 /* major id */
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004377 p = xdr_encode_opaque(p, major_id, major_id_sz);
Andy Adamson0733d212009-04-03 08:28:01 +03004378
4379 /* Server scope */
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004380 p = xdr_encode_opaque(p, server_scope, server_scope_sz);
Andy Adamson0733d212009-04-03 08:28:01 +03004381
4382 /* Implementation id */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004383 *p++ = cpu_to_be32(0); /* zero length nfs_impl_id4 array */
Andy Adamson0733d212009-04-03 08:28:01 +03004384 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03004385}
4386
4387static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004388nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03004389 struct nfsd4_create_session *sess)
4390{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004391 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004392 __be32 *p;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004393
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004394 p = xdr_reserve_space(xdr, 24);
4395 if (!p)
4396 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004397 p = xdr_encode_opaque_fixed(p, sess->sessionid.data,
4398 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004399 *p++ = cpu_to_be32(sess->seqid);
4400 *p++ = cpu_to_be32(sess->flags);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004401
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004402 p = xdr_reserve_space(xdr, 28);
4403 if (!p)
4404 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004405 *p++ = cpu_to_be32(0); /* headerpadsz */
4406 *p++ = cpu_to_be32(sess->fore_channel.maxreq_sz);
4407 *p++ = cpu_to_be32(sess->fore_channel.maxresp_sz);
4408 *p++ = cpu_to_be32(sess->fore_channel.maxresp_cached);
4409 *p++ = cpu_to_be32(sess->fore_channel.maxops);
4410 *p++ = cpu_to_be32(sess->fore_channel.maxreqs);
4411 *p++ = cpu_to_be32(sess->fore_channel.nr_rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004412
4413 if (sess->fore_channel.nr_rdma_attrs) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004414 p = xdr_reserve_space(xdr, 4);
4415 if (!p)
4416 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004417 *p++ = cpu_to_be32(sess->fore_channel.rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004418 }
4419
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004420 p = xdr_reserve_space(xdr, 28);
4421 if (!p)
4422 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004423 *p++ = cpu_to_be32(0); /* headerpadsz */
4424 *p++ = cpu_to_be32(sess->back_channel.maxreq_sz);
4425 *p++ = cpu_to_be32(sess->back_channel.maxresp_sz);
4426 *p++ = cpu_to_be32(sess->back_channel.maxresp_cached);
4427 *p++ = cpu_to_be32(sess->back_channel.maxops);
4428 *p++ = cpu_to_be32(sess->back_channel.maxreqs);
4429 *p++ = cpu_to_be32(sess->back_channel.nr_rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004430
4431 if (sess->back_channel.nr_rdma_attrs) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004432 p = xdr_reserve_space(xdr, 4);
4433 if (!p)
4434 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004435 *p++ = cpu_to_be32(sess->back_channel.rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004436 }
4437 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03004438}
4439
4440static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004441nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03004442 struct nfsd4_sequence *seq)
4443{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004444 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004445 __be32 *p;
Benny Halevyb85d4c02009-04-03 08:28:08 +03004446
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004447 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 20);
4448 if (!p)
4449 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004450 p = xdr_encode_opaque_fixed(p, seq->sessionid.data,
4451 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004452 *p++ = cpu_to_be32(seq->seqid);
4453 *p++ = cpu_to_be32(seq->slotid);
J. Bruce Fieldsb7d7ca32011-08-31 15:39:30 -04004454 /* Note slotid's are numbered from zero: */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004455 *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_highest_slotid */
4456 *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_target_highest_slotid */
4457 *p++ = cpu_to_be32(seq->status_flags);
Benny Halevyb85d4c02009-04-03 08:28:08 +03004458
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04004459 resp->cstate.data_offset = xdr->buf->len; /* DRC cache data pointer */
Benny Halevyb85d4c02009-04-03 08:28:08 +03004460 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03004461}
4462
J. Bruce Fields2355c592012-04-25 16:56:22 -04004463static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004464nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
Bryan Schumaker17456802011-07-13 10:50:48 -04004465 struct nfsd4_test_stateid *test_stateid)
4466{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004467 struct xdr_stream *xdr = &resp->xdr;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004468 struct nfsd4_test_stateid_id *stateid, *next;
Bryan Schumaker17456802011-07-13 10:50:48 -04004469 __be32 *p;
Bryan Schumaker17456802011-07-13 10:50:48 -04004470
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004471 p = xdr_reserve_space(xdr, 4 + (4 * test_stateid->ts_num_ids));
4472 if (!p)
4473 return nfserr_resource;
Bryan Schumaker17456802011-07-13 10:50:48 -04004474 *p++ = htonl(test_stateid->ts_num_ids);
Bryan Schumaker17456802011-07-13 10:50:48 -04004475
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004476 list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
Al Viro02f5fde2012-04-13 00:10:34 -04004477 *p++ = stateid->ts_id_status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004478 }
Bryan Schumaker17456802011-07-13 10:50:48 -04004479
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004480 return 0;
Bryan Schumaker17456802011-07-13 10:50:48 -04004481}
4482
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004483#ifdef CONFIG_NFSD_PNFS
4484static __be32
4485nfsd4_encode_getdeviceinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
4486 struct nfsd4_getdeviceinfo *gdev)
4487{
4488 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004489 const struct nfsd4_layout_ops *ops;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004490 u32 starting_len = xdr->buf->len, needed_len;
4491 __be32 *p;
4492
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004493 p = xdr_reserve_space(xdr, 4);
4494 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004495 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004496
4497 *p++ = cpu_to_be32(gdev->gd_layout_type);
4498
4499 /* If maxcount is 0 then just update notifications */
4500 if (gdev->gd_maxcount != 0) {
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004501 ops = nfsd4_layout_ops[gdev->gd_layout_type];
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004502 nfserr = ops->encode_getdeviceinfo(xdr, gdev);
4503 if (nfserr) {
4504 /*
4505 * We don't bother to burden the layout drivers with
4506 * enforcing gd_maxcount, just tell the client to
4507 * come back with a bigger buffer if it's not enough.
4508 */
4509 if (xdr->buf->len + 4 > gdev->gd_maxcount)
4510 goto toosmall;
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004511 return nfserr;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004512 }
4513 }
4514
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004515 if (gdev->gd_notify_types) {
4516 p = xdr_reserve_space(xdr, 4 + 4);
4517 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004518 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004519 *p++ = cpu_to_be32(1); /* bitmap length */
4520 *p++ = cpu_to_be32(gdev->gd_notify_types);
4521 } else {
4522 p = xdr_reserve_space(xdr, 4);
4523 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004524 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004525 *p++ = 0;
4526 }
4527
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004528 return 0;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004529toosmall:
4530 dprintk("%s: maxcount too small\n", __func__);
4531 needed_len = xdr->buf->len + 4 /* notifications */;
4532 xdr_truncate_encode(xdr, starting_len);
4533 p = xdr_reserve_space(xdr, 4);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004534 if (!p)
4535 return nfserr_resource;
4536 *p++ = cpu_to_be32(needed_len);
4537 return nfserr_toosmall;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004538}
4539
4540static __be32
4541nfsd4_encode_layoutget(struct nfsd4_compoundres *resp, __be32 nfserr,
4542 struct nfsd4_layoutget *lgp)
4543{
4544 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004545 const struct nfsd4_layout_ops *ops;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004546 __be32 *p;
4547
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004548 p = xdr_reserve_space(xdr, 36 + sizeof(stateid_opaque_t));
4549 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004550 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004551
4552 *p++ = cpu_to_be32(1); /* we always set return-on-close */
4553 *p++ = cpu_to_be32(lgp->lg_sid.si_generation);
4554 p = xdr_encode_opaque_fixed(p, &lgp->lg_sid.si_opaque,
4555 sizeof(stateid_opaque_t));
4556
4557 *p++ = cpu_to_be32(1); /* we always return a single layout */
4558 p = xdr_encode_hyper(p, lgp->lg_seg.offset);
4559 p = xdr_encode_hyper(p, lgp->lg_seg.length);
4560 *p++ = cpu_to_be32(lgp->lg_seg.iomode);
4561 *p++ = cpu_to_be32(lgp->lg_layout_type);
4562
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004563 ops = nfsd4_layout_ops[lgp->lg_layout_type];
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004564 return ops->encode_layoutget(xdr, lgp);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004565}
4566
4567static __be32
4568nfsd4_encode_layoutcommit(struct nfsd4_compoundres *resp, __be32 nfserr,
4569 struct nfsd4_layoutcommit *lcp)
4570{
4571 struct xdr_stream *xdr = &resp->xdr;
4572 __be32 *p;
4573
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004574 p = xdr_reserve_space(xdr, 4);
4575 if (!p)
4576 return nfserr_resource;
4577 *p++ = cpu_to_be32(lcp->lc_size_chg);
4578 if (lcp->lc_size_chg) {
4579 p = xdr_reserve_space(xdr, 8);
4580 if (!p)
4581 return nfserr_resource;
4582 p = xdr_encode_hyper(p, lcp->lc_newsize);
4583 }
4584
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004585 return 0;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004586}
4587
4588static __be32
4589nfsd4_encode_layoutreturn(struct nfsd4_compoundres *resp, __be32 nfserr,
4590 struct nfsd4_layoutreturn *lrp)
4591{
4592 struct xdr_stream *xdr = &resp->xdr;
4593 __be32 *p;
4594
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004595 p = xdr_reserve_space(xdr, 4);
4596 if (!p)
4597 return nfserr_resource;
4598 *p++ = cpu_to_be32(lrp->lrs_present);
4599 if (lrp->lrs_present)
Kinglong Mee376675d2015-03-22 22:17:10 +08004600 return nfsd4_encode_stateid(xdr, &lrp->lr_sid);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004601 return 0;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004602}
4603#endif /* CONFIG_NFSD_PNFS */
4604
Andy Adamson2db134e2009-04-03 08:27:55 +03004605static __be32
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004606nfsd42_encode_write_res(struct nfsd4_compoundres *resp,
4607 struct nfsd42_write_res *write, bool sync)
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004608{
4609 __be32 *p;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004610 p = xdr_reserve_space(&resp->xdr, 4);
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004611 if (!p)
4612 return nfserr_resource;
4613
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004614 if (sync)
4615 *p++ = cpu_to_be32(0);
4616 else {
4617 __be32 nfserr;
4618 *p++ = cpu_to_be32(1);
4619 nfserr = nfsd4_encode_stateid(&resp->xdr, &write->cb_stateid);
4620 if (nfserr)
4621 return nfserr;
4622 }
4623 p = xdr_reserve_space(&resp->xdr, 8 + 4 + NFS4_VERIFIER_SIZE);
4624 if (!p)
4625 return nfserr_resource;
4626
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004627 p = xdr_encode_hyper(p, write->wr_bytes_written);
4628 *p++ = cpu_to_be32(write->wr_stable_how);
4629 p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
4630 NFS4_VERIFIER_SIZE);
4631 return nfs_ok;
4632}
4633
4634static __be32
Olga Kornievskaia51911862019-08-08 11:14:59 -04004635nfsd42_encode_nl4_server(struct nfsd4_compoundres *resp, struct nl4_server *ns)
4636{
4637 struct xdr_stream *xdr = &resp->xdr;
4638 struct nfs42_netaddr *addr;
4639 __be32 *p;
4640
4641 p = xdr_reserve_space(xdr, 4);
4642 *p++ = cpu_to_be32(ns->nl4_type);
4643
4644 switch (ns->nl4_type) {
4645 case NL4_NETADDR:
4646 addr = &ns->u.nl4_addr;
4647
4648 /* netid_len, netid, uaddr_len, uaddr (port included
4649 * in RPCBIND_MAXUADDRLEN)
4650 */
4651 p = xdr_reserve_space(xdr,
4652 4 /* netid len */ +
4653 (XDR_QUADLEN(addr->netid_len) * 4) +
4654 4 /* uaddr len */ +
4655 (XDR_QUADLEN(addr->addr_len) * 4));
4656 if (!p)
4657 return nfserr_resource;
4658
4659 *p++ = cpu_to_be32(addr->netid_len);
4660 p = xdr_encode_opaque_fixed(p, addr->netid,
4661 addr->netid_len);
4662 *p++ = cpu_to_be32(addr->addr_len);
4663 p = xdr_encode_opaque_fixed(p, addr->addr,
4664 addr->addr_len);
4665 break;
4666 default:
4667 WARN_ON_ONCE(ns->nl4_type != NL4_NETADDR);
4668 return nfserr_inval;
4669 }
4670
4671 return 0;
4672}
4673
4674static __be32
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004675nfsd4_encode_copy(struct nfsd4_compoundres *resp, __be32 nfserr,
4676 struct nfsd4_copy *copy)
4677{
4678 __be32 *p;
4679
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004680 nfserr = nfsd42_encode_write_res(resp, &copy->cp_res,
4681 copy->cp_synchronous);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004682 if (nfserr)
4683 return nfserr;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004684
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004685 p = xdr_reserve_space(&resp->xdr, 4 + 4);
J. Bruce Fieldsedcc8452018-03-07 15:37:35 -05004686 *p++ = xdr_one; /* cr_consecutive */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004687 *p++ = cpu_to_be32(copy->cp_synchronous);
4688 return 0;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004689}
4690
4691static __be32
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04004692nfsd4_encode_offload_status(struct nfsd4_compoundres *resp, __be32 nfserr,
4693 struct nfsd4_offload_status *os)
4694{
4695 struct xdr_stream *xdr = &resp->xdr;
4696 __be32 *p;
4697
4698 p = xdr_reserve_space(xdr, 8 + 4);
4699 if (!p)
4700 return nfserr_resource;
4701 p = xdr_encode_hyper(p, os->count);
4702 *p++ = cpu_to_be32(0);
Anna Schumaker528b8492020-09-28 13:08:58 -04004703 return nfserr;
4704}
4705
4706static __be32
4707nfsd4_encode_read_plus_data(struct nfsd4_compoundres *resp,
4708 struct nfsd4_read *read,
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004709 unsigned long *maxcount, u32 *eof,
4710 loff_t *pos)
Anna Schumaker528b8492020-09-28 13:08:58 -04004711{
4712 struct xdr_stream *xdr = &resp->xdr;
4713 struct file *file = read->rd_nf->nf_file;
4714 int starting_len = xdr->buf->len;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004715 loff_t hole_pos;
Anna Schumaker528b8492020-09-28 13:08:58 -04004716 __be32 nfserr;
4717 __be32 *p, tmp;
4718 __be64 tmp64;
4719
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004720 hole_pos = pos ? *pos : vfs_llseek(file, read->rd_offset, SEEK_HOLE);
Anna Schumaker2db27992020-09-28 13:08:59 -04004721 if (hole_pos > read->rd_offset)
Anna Schumaker278765e2020-09-28 13:09:00 -04004722 *maxcount = min_t(unsigned long, *maxcount, hole_pos - read->rd_offset);
4723 *maxcount = min_t(unsigned long, *maxcount, (xdr->buf->buflen - xdr->buf->len));
Anna Schumaker528b8492020-09-28 13:08:58 -04004724
4725 /* Content type, offset, byte count */
4726 p = xdr_reserve_space(xdr, 4 + 8 + 4);
4727 if (!p)
4728 return nfserr_resource;
4729
Anna Schumaker278765e2020-09-28 13:09:00 -04004730 read->rd_vlen = xdr_reserve_space_vec(xdr, resp->rqstp->rq_vec, *maxcount);
Anna Schumaker528b8492020-09-28 13:08:58 -04004731 if (read->rd_vlen < 0)
4732 return nfserr_resource;
4733
4734 nfserr = nfsd_readv(resp->rqstp, read->rd_fhp, file, read->rd_offset,
Anna Schumaker278765e2020-09-28 13:09:00 -04004735 resp->rqstp->rq_vec, read->rd_vlen, maxcount, eof);
Anna Schumaker528b8492020-09-28 13:08:58 -04004736 if (nfserr)
4737 return nfserr;
4738
4739 tmp = htonl(NFS4_CONTENT_DATA);
4740 write_bytes_to_xdr_buf(xdr->buf, starting_len, &tmp, 4);
4741 tmp64 = cpu_to_be64(read->rd_offset);
4742 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp64, 8);
Anna Schumaker278765e2020-09-28 13:09:00 -04004743 tmp = htonl(*maxcount);
Anna Schumaker528b8492020-09-28 13:08:58 -04004744 write_bytes_to_xdr_buf(xdr->buf, starting_len + 12, &tmp, 4);
4745 return nfs_ok;
4746}
4747
4748static __be32
Anna Schumaker2db27992020-09-28 13:08:59 -04004749nfsd4_encode_read_plus_hole(struct nfsd4_compoundres *resp,
4750 struct nfsd4_read *read,
Anna Schumaker278765e2020-09-28 13:09:00 -04004751 unsigned long *maxcount, u32 *eof)
Anna Schumaker2db27992020-09-28 13:08:59 -04004752{
4753 struct file *file = read->rd_nf->nf_file;
Anna Schumaker278765e2020-09-28 13:09:00 -04004754 loff_t data_pos = vfs_llseek(file, read->rd_offset, SEEK_DATA);
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004755 loff_t f_size = i_size_read(file_inode(file));
Anna Schumaker278765e2020-09-28 13:09:00 -04004756 unsigned long count;
Anna Schumaker2db27992020-09-28 13:08:59 -04004757 __be32 *p;
4758
Anna Schumaker278765e2020-09-28 13:09:00 -04004759 if (data_pos == -ENXIO)
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004760 data_pos = f_size;
4761 else if (data_pos <= read->rd_offset || (data_pos < f_size && data_pos % PAGE_SIZE))
4762 return nfsd4_encode_read_plus_data(resp, read, maxcount, eof, &f_size);
Anna Schumaker278765e2020-09-28 13:09:00 -04004763 count = data_pos - read->rd_offset;
4764
Anna Schumaker2db27992020-09-28 13:08:59 -04004765 /* Content type, offset, byte count */
4766 p = xdr_reserve_space(&resp->xdr, 4 + 8 + 8);
4767 if (!p)
4768 return nfserr_resource;
4769
4770 *p++ = htonl(NFS4_CONTENT_HOLE);
4771 p = xdr_encode_hyper(p, read->rd_offset);
Anna Schumaker278765e2020-09-28 13:09:00 -04004772 p = xdr_encode_hyper(p, count);
Anna Schumaker2db27992020-09-28 13:08:59 -04004773
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004774 *eof = (read->rd_offset + count) >= f_size;
Anna Schumaker278765e2020-09-28 13:09:00 -04004775 *maxcount = min_t(unsigned long, count, *maxcount);
Anna Schumaker2db27992020-09-28 13:08:59 -04004776 return nfs_ok;
4777}
4778
4779static __be32
Anna Schumaker528b8492020-09-28 13:08:58 -04004780nfsd4_encode_read_plus(struct nfsd4_compoundres *resp, __be32 nfserr,
4781 struct nfsd4_read *read)
4782{
Anna Schumaker278765e2020-09-28 13:09:00 -04004783 unsigned long maxcount, count;
Anna Schumaker528b8492020-09-28 13:08:58 -04004784 struct xdr_stream *xdr = &resp->xdr;
4785 struct file *file;
4786 int starting_len = xdr->buf->len;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004787 int last_segment = xdr->buf->len;
Anna Schumaker528b8492020-09-28 13:08:58 -04004788 int segments = 0;
4789 __be32 *p, tmp;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004790 bool is_data;
Anna Schumaker2db27992020-09-28 13:08:59 -04004791 loff_t pos;
Anna Schumaker528b8492020-09-28 13:08:58 -04004792 u32 eof;
4793
4794 if (nfserr)
4795 return nfserr;
4796 file = read->rd_nf->nf_file;
4797
4798 /* eof flag, segment count */
4799 p = xdr_reserve_space(xdr, 4 + 4);
4800 if (!p)
4801 return nfserr_resource;
4802 xdr_commit_encode(xdr);
4803
4804 maxcount = svc_max_payload(resp->rqstp);
4805 maxcount = min_t(unsigned long, maxcount,
4806 (xdr->buf->buflen - xdr->buf->len));
4807 maxcount = min_t(unsigned long, maxcount, read->rd_length);
Anna Schumaker278765e2020-09-28 13:09:00 -04004808 count = maxcount;
Anna Schumaker528b8492020-09-28 13:08:58 -04004809
4810 eof = read->rd_offset >= i_size_read(file_inode(file));
Anna Schumaker2db27992020-09-28 13:08:59 -04004811 if (eof)
4812 goto out;
4813
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004814 pos = vfs_llseek(file, read->rd_offset, SEEK_HOLE);
4815 is_data = pos > read->rd_offset;
Anna Schumaker2db27992020-09-28 13:08:59 -04004816
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004817 while (count > 0 && !eof) {
Anna Schumaker278765e2020-09-28 13:09:00 -04004818 maxcount = count;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004819 if (is_data)
4820 nfserr = nfsd4_encode_read_plus_data(resp, read, &maxcount, &eof,
4821 segments == 0 ? &pos : NULL);
4822 else
4823 nfserr = nfsd4_encode_read_plus_hole(resp, read, &maxcount, &eof);
Anna Schumaker278765e2020-09-28 13:09:00 -04004824 if (nfserr)
4825 goto out;
4826 count -= maxcount;
4827 read->rd_offset += maxcount;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004828 is_data = !is_data;
4829 last_segment = xdr->buf->len;
Anna Schumaker528b8492020-09-28 13:08:58 -04004830 segments++;
4831 }
4832
Anna Schumaker2db27992020-09-28 13:08:59 -04004833out:
Anna Schumaker278765e2020-09-28 13:09:00 -04004834 if (nfserr && segments == 0)
Anna Schumaker528b8492020-09-28 13:08:58 -04004835 xdr_truncate_encode(xdr, starting_len);
4836 else {
4837 tmp = htonl(eof);
4838 write_bytes_to_xdr_buf(xdr->buf, starting_len, &tmp, 4);
4839 tmp = htonl(segments);
4840 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp, 4);
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004841 if (nfserr) {
4842 xdr_truncate_encode(xdr, last_segment);
4843 nfserr = nfs_ok;
4844 }
Anna Schumaker528b8492020-09-28 13:08:58 -04004845 }
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04004846
4847 return nfserr;
4848}
4849
4850static __be32
Olga Kornievskaia51911862019-08-08 11:14:59 -04004851nfsd4_encode_copy_notify(struct nfsd4_compoundres *resp, __be32 nfserr,
4852 struct nfsd4_copy_notify *cn)
4853{
4854 struct xdr_stream *xdr = &resp->xdr;
4855 __be32 *p;
4856
4857 if (nfserr)
4858 return nfserr;
4859
4860 /* 8 sec, 4 nsec */
4861 p = xdr_reserve_space(xdr, 12);
4862 if (!p)
4863 return nfserr_resource;
4864
4865 /* cnr_lease_time */
4866 p = xdr_encode_hyper(p, cn->cpn_sec);
4867 *p++ = cpu_to_be32(cn->cpn_nsec);
4868
4869 /* cnr_stateid */
4870 nfserr = nfsd4_encode_stateid(xdr, &cn->cpn_cnr_stateid);
4871 if (nfserr)
4872 return nfserr;
4873
4874 /* cnr_src.nl_nsvr */
4875 p = xdr_reserve_space(xdr, 4);
4876 if (!p)
4877 return nfserr_resource;
4878
4879 *p++ = cpu_to_be32(1);
4880
4881 return nfsd42_encode_nl4_server(resp, &cn->cpn_src);
4882}
4883
4884static __be32
Anna Schumaker24bab492014-09-26 13:58:27 -04004885nfsd4_encode_seek(struct nfsd4_compoundres *resp, __be32 nfserr,
4886 struct nfsd4_seek *seek)
4887{
4888 __be32 *p;
4889
Anna Schumaker24bab492014-09-26 13:58:27 -04004890 p = xdr_reserve_space(&resp->xdr, 4 + 8);
4891 *p++ = cpu_to_be32(seek->seek_eof);
4892 p = xdr_encode_hyper(p, seek->seek_pos);
4893
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004894 return 0;
Anna Schumaker24bab492014-09-26 13:58:27 -04004895}
4896
4897static __be32
Benny Halevy695e12f2008-07-04 14:38:33 +03004898nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
4899{
4900 return nfserr;
4901}
4902
Frank van der Linden23e50fe2020-06-23 22:39:26 +00004903/*
4904 * Encode kmalloc-ed buffer in to XDR stream.
4905 */
Chuck Leverb9a49232020-09-11 14:47:53 -04004906static __be32
Frank van der Linden23e50fe2020-06-23 22:39:26 +00004907nfsd4_vbuf_to_stream(struct xdr_stream *xdr, char *buf, u32 buflen)
4908{
4909 u32 cplen;
4910 __be32 *p;
4911
4912 cplen = min_t(unsigned long, buflen,
4913 ((void *)xdr->end - (void *)xdr->p));
4914 p = xdr_reserve_space(xdr, cplen);
4915 if (!p)
4916 return nfserr_resource;
4917
4918 memcpy(p, buf, cplen);
4919 buf += cplen;
4920 buflen -= cplen;
4921
4922 while (buflen) {
4923 cplen = min_t(u32, buflen, PAGE_SIZE);
4924 p = xdr_reserve_space(xdr, cplen);
4925 if (!p)
4926 return nfserr_resource;
4927
4928 memcpy(p, buf, cplen);
4929
4930 if (cplen < PAGE_SIZE) {
4931 /*
4932 * We're done, with a length that wasn't page
4933 * aligned, so possibly not word aligned. Pad
4934 * any trailing bytes with 0.
4935 */
4936 xdr_encode_opaque_fixed(p, NULL, cplen);
4937 break;
4938 }
4939
4940 buflen -= PAGE_SIZE;
4941 buf += PAGE_SIZE;
4942 }
4943
4944 return 0;
4945}
4946
4947static __be32
4948nfsd4_encode_getxattr(struct nfsd4_compoundres *resp, __be32 nfserr,
4949 struct nfsd4_getxattr *getxattr)
4950{
4951 struct xdr_stream *xdr = &resp->xdr;
4952 __be32 *p, err;
4953
4954 p = xdr_reserve_space(xdr, 4);
4955 if (!p)
4956 return nfserr_resource;
4957
4958 *p = cpu_to_be32(getxattr->getxa_len);
4959
4960 if (getxattr->getxa_len == 0)
4961 return 0;
4962
4963 err = nfsd4_vbuf_to_stream(xdr, getxattr->getxa_buf,
4964 getxattr->getxa_len);
4965
4966 kvfree(getxattr->getxa_buf);
4967
4968 return err;
4969}
4970
4971static __be32
4972nfsd4_encode_setxattr(struct nfsd4_compoundres *resp, __be32 nfserr,
4973 struct nfsd4_setxattr *setxattr)
4974{
4975 struct xdr_stream *xdr = &resp->xdr;
4976 __be32 *p;
4977
4978 p = xdr_reserve_space(xdr, 20);
4979 if (!p)
4980 return nfserr_resource;
4981
4982 encode_cinfo(p, &setxattr->setxa_cinfo);
4983
4984 return 0;
4985}
4986
4987/*
4988 * See if there are cookie values that can be rejected outright.
4989 */
4990static __be32
4991nfsd4_listxattr_validate_cookie(struct nfsd4_listxattrs *listxattrs,
4992 u32 *offsetp)
4993{
4994 u64 cookie = listxattrs->lsxa_cookie;
4995
4996 /*
4997 * If the cookie is larger than the maximum number we can fit
4998 * in either the buffer we just got back from vfs_listxattr, or,
4999 * XDR-encoded, in the return buffer, it's invalid.
5000 */
5001 if (cookie > (listxattrs->lsxa_len) / (XATTR_USER_PREFIX_LEN + 2))
5002 return nfserr_badcookie;
5003
5004 if (cookie > (listxattrs->lsxa_maxcount /
5005 (XDR_QUADLEN(XATTR_USER_PREFIX_LEN + 2) + 4)))
5006 return nfserr_badcookie;
5007
5008 *offsetp = (u32)cookie;
5009 return 0;
5010}
5011
5012static __be32
5013nfsd4_encode_listxattrs(struct nfsd4_compoundres *resp, __be32 nfserr,
5014 struct nfsd4_listxattrs *listxattrs)
5015{
5016 struct xdr_stream *xdr = &resp->xdr;
5017 u32 cookie_offset, count_offset, eof;
5018 u32 left, xdrleft, slen, count;
5019 u32 xdrlen, offset;
5020 u64 cookie;
5021 char *sp;
Chuck Leverb9a49232020-09-11 14:47:53 -04005022 __be32 status, tmp;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005023 __be32 *p;
5024 u32 nuser;
5025
5026 eof = 1;
5027
5028 status = nfsd4_listxattr_validate_cookie(listxattrs, &offset);
5029 if (status)
5030 goto out;
5031
5032 /*
5033 * Reserve space for the cookie and the name array count. Record
5034 * the offsets to save them later.
5035 */
5036 cookie_offset = xdr->buf->len;
5037 count_offset = cookie_offset + 8;
5038 p = xdr_reserve_space(xdr, 12);
5039 if (!p) {
5040 status = nfserr_resource;
5041 goto out;
5042 }
5043
5044 count = 0;
5045 left = listxattrs->lsxa_len;
5046 sp = listxattrs->lsxa_buf;
5047 nuser = 0;
5048
5049 xdrleft = listxattrs->lsxa_maxcount;
5050
5051 while (left > 0 && xdrleft > 0) {
5052 slen = strlen(sp);
5053
5054 /*
Alex Dewar4cce11fa2020-08-17 18:51:26 +01005055 * Check if this is a "user." attribute, skip it if not.
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005056 */
5057 if (strncmp(sp, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
5058 goto contloop;
5059
5060 slen -= XATTR_USER_PREFIX_LEN;
5061 xdrlen = 4 + ((slen + 3) & ~3);
5062 if (xdrlen > xdrleft) {
5063 if (count == 0) {
5064 /*
5065 * Can't even fit the first attribute name.
5066 */
5067 status = nfserr_toosmall;
5068 goto out;
5069 }
5070 eof = 0;
5071 goto wreof;
5072 }
5073
5074 left -= XATTR_USER_PREFIX_LEN;
5075 sp += XATTR_USER_PREFIX_LEN;
5076 if (nuser++ < offset)
5077 goto contloop;
5078
5079
5080 p = xdr_reserve_space(xdr, xdrlen);
5081 if (!p) {
5082 status = nfserr_resource;
5083 goto out;
5084 }
5085
Alex Deware2a18402020-08-12 15:12:51 +01005086 xdr_encode_opaque(p, sp, slen);
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005087
5088 xdrleft -= xdrlen;
5089 count++;
5090contloop:
5091 sp += slen + 1;
5092 left -= slen + 1;
5093 }
5094
5095 /*
5096 * If there were user attributes to copy, but we didn't copy
5097 * any, the offset was too large (e.g. the cookie was invalid).
5098 */
5099 if (nuser > 0 && count == 0) {
5100 status = nfserr_badcookie;
5101 goto out;
5102 }
5103
5104wreof:
5105 p = xdr_reserve_space(xdr, 4);
5106 if (!p) {
5107 status = nfserr_resource;
5108 goto out;
5109 }
5110 *p = cpu_to_be32(eof);
5111
5112 cookie = offset + count;
5113
5114 write_bytes_to_xdr_buf(xdr->buf, cookie_offset, &cookie, 8);
Chuck Leverb9a49232020-09-11 14:47:53 -04005115 tmp = cpu_to_be32(count);
5116 write_bytes_to_xdr_buf(xdr->buf, count_offset, &tmp, 4);
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005117out:
5118 if (listxattrs->lsxa_len)
5119 kvfree(listxattrs->lsxa_buf);
5120 return status;
5121}
5122
5123static __be32
5124nfsd4_encode_removexattr(struct nfsd4_compoundres *resp, __be32 nfserr,
5125 struct nfsd4_removexattr *removexattr)
5126{
5127 struct xdr_stream *xdr = &resp->xdr;
5128 __be32 *p;
5129
5130 p = xdr_reserve_space(xdr, 20);
5131 if (!p)
5132 return nfserr_resource;
5133
5134 p = encode_cinfo(p, &removexattr->rmxa_cinfo);
5135 return 0;
5136}
5137
Benny Halevy695e12f2008-07-04 14:38:33 +03005138typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
5139
Andy Adamson2db134e2009-04-03 08:27:55 +03005140/*
5141 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
5142 * since we don't need to filter out obsolete ops as this is
5143 * done in the decoding phase.
5144 */
Chuck Leverc1df6092017-08-01 11:59:58 -04005145static const nfsd4_enc nfsd4_enc_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04005146 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
5147 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
5148 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
5149 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
5150 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
5151 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
5152 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
5153 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
5154 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
5155 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
5156 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
5157 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
5158 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
5159 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
5160 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
5161 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
Benny Halevy84f09f42009-03-04 23:05:35 +02005162 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04005163 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
5164 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
5165 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
5166 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
5167 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
5168 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
5169 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
5170 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
5171 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
5172 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
5173 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
5174 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
5175 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
5176 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
5177 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
5178 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
5179 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
5180 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
5181 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
5182 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03005183
5184 /* NFSv4.1 operations */
5185 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04005186 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
Andy Adamson2db134e2009-04-03 08:27:55 +03005187 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
5188 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
Kinglong Mee43212cc2013-12-09 19:04:23 +08005189 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_noop,
5190 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03005191 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02005192#ifdef CONFIG_NFSD_PNFS
5193 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_getdeviceinfo,
5194 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
5195 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_layoutcommit,
5196 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_layoutget,
5197 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_layoutreturn,
5198#else
Andy Adamson2db134e2009-04-03 08:27:55 +03005199 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
5200 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
5201 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
5202 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
5203 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02005204#endif
Mi Jinlong22b6dee2010-12-27 14:29:57 +08005205 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
Andy Adamson2db134e2009-04-03 08:27:55 +03005206 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
5207 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
Bryan Schumaker17456802011-07-13 10:50:48 -04005208 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
Andy Adamson2db134e2009-04-03 08:27:55 +03005209 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
5210 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
5211 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
Anna Schumaker87a15a82014-09-26 13:58:26 -04005212
5213 /* NFSv4.2 operations */
5214 [OP_ALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
Anna Schumaker29ae7f92016-09-07 15:57:30 -04005215 [OP_COPY] = (nfsd4_enc)nfsd4_encode_copy,
Olga Kornievskaia51911862019-08-08 11:14:59 -04005216 [OP_COPY_NOTIFY] = (nfsd4_enc)nfsd4_encode_copy_notify,
Anna Schumaker87a15a82014-09-26 13:58:26 -04005217 [OP_DEALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
5218 [OP_IO_ADVISE] = (nfsd4_enc)nfsd4_encode_noop,
5219 [OP_LAYOUTERROR] = (nfsd4_enc)nfsd4_encode_noop,
5220 [OP_LAYOUTSTATS] = (nfsd4_enc)nfsd4_encode_noop,
5221 [OP_OFFLOAD_CANCEL] = (nfsd4_enc)nfsd4_encode_noop,
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04005222 [OP_OFFLOAD_STATUS] = (nfsd4_enc)nfsd4_encode_offload_status,
Anna Schumaker528b8492020-09-28 13:08:58 -04005223 [OP_READ_PLUS] = (nfsd4_enc)nfsd4_encode_read_plus,
Anna Schumaker24bab492014-09-26 13:58:27 -04005224 [OP_SEEK] = (nfsd4_enc)nfsd4_encode_seek,
Anna Schumaker87a15a82014-09-26 13:58:26 -04005225 [OP_WRITE_SAME] = (nfsd4_enc)nfsd4_encode_noop,
Christoph Hellwigffa01602015-12-03 12:59:52 +01005226 [OP_CLONE] = (nfsd4_enc)nfsd4_encode_noop,
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005227
5228 /* RFC 8276 extended atributes operations */
5229 [OP_GETXATTR] = (nfsd4_enc)nfsd4_encode_getxattr,
5230 [OP_SETXATTR] = (nfsd4_enc)nfsd4_encode_setxattr,
5231 [OP_LISTXATTRS] = (nfsd4_enc)nfsd4_encode_listxattrs,
5232 [OP_REMOVEXATTR] = (nfsd4_enc)nfsd4_encode_removexattr,
Benny Halevy695e12f2008-07-04 14:38:33 +03005233};
5234
Andy Adamson496c2622009-04-03 08:28:48 +03005235/*
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04005236 * Calculate whether we still have space to encode repsize bytes.
5237 * There are two considerations:
5238 * - For NFS versions >=4.1, the size of the reply must stay within
5239 * session limits
5240 * - For all NFS versions, we must stay within limited preallocated
5241 * buffer space.
Andy Adamson496c2622009-04-03 08:28:48 +03005242 *
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04005243 * This is called before the operation is processed, so can only provide
5244 * an upper estimate. For some nonidempotent operations (such as
5245 * getattr), it's not necessarily a problem if that estimate is wrong,
5246 * as we can fail it after processing without significant side effects.
Andy Adamson496c2622009-04-03 08:28:48 +03005247 */
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04005248__be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 respsize)
Andy Adamson496c2622009-04-03 08:28:48 +03005249{
J. Bruce Fields67492c92014-03-08 16:42:05 -05005250 struct xdr_buf *buf = &resp->rqstp->rq_res;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04005251 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson496c2622009-04-03 08:28:48 +03005252
J. Bruce Fields47ee5292014-03-12 21:39:35 -04005253 if (buf->len + respsize <= buf->buflen)
5254 return nfs_ok;
5255 if (!nfsd4_has_session(&resp->cstate))
J. Bruce Fieldsea8d7722014-03-08 17:19:55 -05005256 return nfserr_resource;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04005257 if (slot->sl_flags & NFSD4_SLOT_CACHETHIS) {
5258 WARN_ON_ONCE(1);
5259 return nfserr_rep_too_big_to_cache;
J. Bruce Fieldsea8d7722014-03-08 17:19:55 -05005260 }
J. Bruce Fields47ee5292014-03-12 21:39:35 -04005261 return nfserr_rep_too_big;
Andy Adamson496c2622009-04-03 08:28:48 +03005262}
5263
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264void
5265nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
5266{
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005267 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005268 struct nfs4_stateowner *so = resp->cstate.replay_owner;
J. Bruce Fields5f4ab942014-03-07 11:01:37 -05005269 struct svc_rqst *rqstp = resp->rqstp;
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04005270 const struct nfsd4_operation *opdesc = op->opdesc;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005271 int post_err_offset;
J. Bruce Fields07d1f802014-03-06 20:39:29 -05005272 nfsd4_enc encoder;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07005273 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05005275 p = xdr_reserve_space(xdr, 8);
5276 if (!p) {
5277 WARN_ON_ONCE(1);
5278 return;
5279 }
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04005280 *p++ = cpu_to_be32(op->opnum);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005281 post_err_offset = xdr->buf->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282
Benny Halevy695e12f2008-07-04 14:38:33 +03005283 if (op->opnum == OP_ILLEGAL)
5284 goto status;
J. Bruce Fieldsb7571e42017-05-06 10:49:21 -04005285 if (op->status && opdesc &&
5286 !(opdesc->op_flags & OP_NONTRIVIAL_ERROR_ENCODE))
5287 goto status;
Benny Halevy695e12f2008-07-04 14:38:33 +03005288 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
5289 !nfsd4_enc_ops[op->opnum]);
J. Bruce Fields07d1f802014-03-06 20:39:29 -05005290 encoder = nfsd4_enc_ops[op->opnum];
5291 op->status = encoder(resp, op->status, &op->u);
Chuck Lever08281342020-11-21 11:36:42 -05005292 if (op->status)
5293 trace_nfsd_compound_encode_err(rqstp, op->opnum, op->status);
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04005294 if (opdesc && opdesc->op_release)
5295 opdesc->op_release(&op->u);
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04005296 xdr_commit_encode(xdr);
5297
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04005298 /* nfsd4_check_resp_size guarantees enough room for error status */
J. Bruce Fields5f4ab942014-03-07 11:01:37 -05005299 if (!op->status) {
5300 int space_needed = 0;
5301 if (!nfsd4_last_compound_op(rqstp))
5302 space_needed = COMPOUND_ERR_SLACK_SPACE;
5303 op->status = nfsd4_check_resp_size(resp, space_needed);
5304 }
J. Bruce Fieldsc8f13d92014-05-08 17:38:18 -04005305 if (op->status == nfserr_resource && nfsd4_has_session(&resp->cstate)) {
5306 struct nfsd4_slot *slot = resp->cstate.slot;
5307
5308 if (slot->sl_flags & NFSD4_SLOT_CACHETHIS)
5309 op->status = nfserr_rep_too_big_to_cache;
5310 else
5311 op->status = nfserr_rep_too_big;
5312 }
J. Bruce Fields07d1f802014-03-06 20:39:29 -05005313 if (op->status == nfserr_resource ||
5314 op->status == nfserr_rep_too_big ||
5315 op->status == nfserr_rep_too_big_to_cache) {
5316 /*
5317 * The operation may have already been encoded or
5318 * partially encoded. No op returns anything additional
5319 * in the case of one of these three errors, so we can
5320 * just truncate back to after the status. But it's a
5321 * bug if we had to do this on a non-idempotent op:
5322 */
5323 warn_on_nonidempotent_op(op);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005324 xdr_truncate_encode(xdr, post_err_offset);
J. Bruce Fields07d1f802014-03-06 20:39:29 -05005325 }
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005326 if (so) {
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005327 int len = xdr->buf->len - post_err_offset;
5328
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005329 so->so_replay.rp_status = op->status;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005330 so->so_replay.rp_buflen = len;
5331 read_bytes_from_xdr_buf(xdr->buf, post_err_offset,
5332 so->so_replay.rp_buf, len);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005333 }
Benny Halevy695e12f2008-07-04 14:38:33 +03005334status:
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005335 /* Note that op->status is already in network byte order: */
5336 write_bytes_to_xdr_buf(xdr->buf, post_err_offset - 4, &op->status, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337}
5338
5339/*
5340 * Encode the reply stored in the stateowner reply cache
5341 *
5342 * XDR note: do not encode rp->rp_buflen: the buffer contains the
5343 * previously sent already encoded operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 */
5345void
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05005346nfsd4_encode_replay(struct xdr_stream *xdr, struct nfsd4_op *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07005348 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 struct nfs4_replay *rp = op->replay;
5350
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05005351 p = xdr_reserve_space(xdr, 8 + rp->rp_buflen);
5352 if (!p) {
5353 WARN_ON_ONCE(1);
5354 return;
5355 }
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04005356 *p++ = cpu_to_be32(op->opnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 *p++ = rp->rp_status; /* already xdr'ed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04005359 p = xdr_encode_opaque_fixed(p, rp->rp_buf, rp->rp_buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360}
5361
Christoph Hellwig85374882017-05-08 18:48:24 +02005362void nfsd4_release_compoundargs(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363{
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04005364 struct nfsd4_compoundargs *args = rqstp->rq_argp;
5365
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 if (args->ops != args->iops) {
5367 kfree(args->ops);
5368 args->ops = args->iops;
5369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 while (args->to_free) {
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -04005371 struct svcxdr_tmpbuf *tb = args->to_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 args->to_free = tb->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 kfree(tb);
5374 }
5375}
5376
5377int
Christoph Hellwig026fec72017-05-08 19:01:48 +02005378nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379{
Christoph Hellwig026fec72017-05-08 19:01:48 +02005380 struct nfsd4_compoundargs *args = rqstp->rq_argp;
5381
Chuck Leverc1346a122020-11-03 11:54:23 -05005382 /* svcxdr_tmp_alloc */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 args->tmpp = NULL;
5384 args->to_free = NULL;
Chuck Leverc1346a122020-11-03 11:54:23 -05005385
5386 args->xdr = &rqstp->rq_arg_stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 args->ops = args->iops;
5388 args->rqstp = rqstp;
5389
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04005390 return !nfsd4_decode_compound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391}
5392
5393int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02005394nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02005396 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields6ac90392014-02-26 17:39:35 -05005397 struct xdr_buf *buf = resp->xdr.buf;
5398
5399 WARN_ON_ONCE(buf->len != buf->head[0].iov_len + buf->page_len +
5400 buf->tail[0].iov_len);
J. Bruce Fieldsdd97fdd2014-02-26 17:16:27 -05005401
Chuck Levercc028a12020-10-02 15:52:44 -04005402 *p = resp->cstate.status;
5403
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04005404 rqstp->rq_next_page = resp->xdr.page_ptr + 1;
5405
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 p = resp->tagp;
5407 *p++ = htonl(resp->taglen);
5408 memcpy(p, resp->tag, resp->taglen);
5409 p += XDR_QUADLEN(resp->taglen);
5410 *p++ = htonl(resp->opcnt);
5411
Trond Myklebustb6076642014-06-30 11:48:35 -04005412 nfsd4_sequence_done(resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 return 1;
5414}
5415
5416/*
5417 * Local variables:
5418 * c-basic-offset: 8
5419 * End:
5420 */