blob: 8982f2717acf40a45926592bc77cecb874d87aa1 [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{
1237 DECODE_HEAD;
1238
1239 READ_BUF(24);
J. Bruce Fields542d1ab2014-06-02 12:45:31 -04001240 p = xdr_decode_hyper(p, &readdir->rd_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
J. Bruce Fields06553992014-06-02 12:32:51 -04001242 readdir->rd_dircount = be32_to_cpup(p++);
1243 readdir->rd_maxcount = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
1245 goto out;
1246
1247 DECODE_TAIL;
1248}
1249
Al Virob37ad282006-10-19 23:28:59 -07001250static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
1252{
1253 DECODE_HEAD;
1254
1255 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001256 remove->rm_namelen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 READ_BUF(remove->rm_namelen);
1258 SAVEMEM(remove->rm_name, remove->rm_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001259 if ((status = check_filename(remove->rm_name, remove->rm_namelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 return status;
1261
1262 DECODE_TAIL;
1263}
1264
Al Virob37ad282006-10-19 23:28:59 -07001265static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
1267{
1268 DECODE_HEAD;
1269
1270 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001271 rename->rn_snamelen = be32_to_cpup(p++);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05001272 READ_BUF(rename->rn_snamelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 SAVEMEM(rename->rn_sname, rename->rn_snamelen);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05001274 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001275 rename->rn_tnamelen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 READ_BUF(rename->rn_tnamelen);
1277 SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001278 if ((status = check_filename(rename->rn_sname, rename->rn_snamelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 return status;
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001280 if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return status;
1282
1283 DECODE_TAIL;
1284}
1285
Al Virob37ad282006-10-19 23:28:59 -07001286static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
1288{
1289 DECODE_HEAD;
1290
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001291 if (argp->minorversion >= 1)
1292 return nfserr_notsupp;
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 READ_BUF(sizeof(clientid_t));
1295 COPYMEM(clientid, sizeof(clientid_t));
1296
1297 DECODE_TAIL;
1298}
1299
Al Virob37ad282006-10-19 23:28:59 -07001300static __be32
Andy Adamsondcb488a32007-07-17 04:04:51 -07001301nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
1302 struct nfsd4_secinfo *secinfo)
1303{
1304 DECODE_HEAD;
1305
1306 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001307 secinfo->si_namelen = be32_to_cpup(p++);
Andy Adamsondcb488a32007-07-17 04:04:51 -07001308 READ_BUF(secinfo->si_namelen);
1309 SAVEMEM(secinfo->si_name, secinfo->si_namelen);
J. Bruce Fieldsa36b1722012-11-25 16:31:00 -05001310 status = check_filename(secinfo->si_name, secinfo->si_namelen);
Andy Adamsondcb488a32007-07-17 04:04:51 -07001311 if (status)
1312 return status;
1313 DECODE_TAIL;
1314}
1315
1316static __be32
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05001317nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
1318 struct nfsd4_secinfo_no_name *sin)
1319{
1320 DECODE_HEAD;
1321
1322 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001323 sin->sin_style = be32_to_cpup(p++);
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05001324 DECODE_TAIL;
1325}
1326
1327static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
1329{
Benny Halevye31a1b62008-08-12 20:46:18 +03001330 __be32 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Benny Halevye31a1b62008-08-12 20:46:18 +03001332 status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
1333 if (status)
1334 return status;
Chuck Leverd1c263a2020-11-03 12:56:05 -05001335 return nfsd4_decode_fattr4(argp, setattr->sa_bmval,
1336 ARRAY_SIZE(setattr->sa_bmval),
1337 &setattr->sa_iattr, &setattr->sa_acl,
1338 &setattr->sa_label, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
Al Virob37ad282006-10-19 23:28:59 -07001341static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
1343{
1344 DECODE_HEAD;
1345
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001346 if (argp->minorversion >= 1)
1347 return nfserr_notsupp;
1348
Chuck Leverab4684d2012-03-02 17:13:50 -05001349 READ_BUF(NFS4_VERIFIER_SIZE);
1350 COPYMEM(setclientid->se_verf.data, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001352 status = nfsd4_decode_opaque(argp, &setclientid->se_name);
1353 if (status)
1354 return nfserr_bad_xdr;
1355 READ_BUF(8);
J. Bruce Fields06553992014-06-02 12:32:51 -04001356 setclientid->se_callback_prog = be32_to_cpup(p++);
1357 setclientid->se_callback_netid_len = be32_to_cpup(p++);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05001358 READ_BUF(setclientid->se_callback_netid_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05001360 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001361 setclientid->se_callback_addr_len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05001363 READ_BUF(setclientid->se_callback_addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05001365 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001366 setclientid->se_callback_ident = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 DECODE_TAIL;
1369}
1370
Al Virob37ad282006-10-19 23:28:59 -07001371static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
1373{
1374 DECODE_HEAD;
1375
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001376 if (argp->minorversion >= 1)
1377 return nfserr_notsupp;
1378
Chuck Leverab4684d2012-03-02 17:13:50 -05001379 READ_BUF(8 + NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 COPYMEM(&scd_c->sc_clientid, 8);
Chuck Leverab4684d2012-03-02 17:13:50 -05001381 COPYMEM(&scd_c->sc_confirm, NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 DECODE_TAIL;
1384}
1385
1386/* Also used for NVERIFY */
Al Virob37ad282006-10-19 23:28:59 -07001387static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1389{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 DECODE_HEAD;
1391
1392 if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
1393 goto out;
1394
1395 /* For convenience's sake, we compare raw xdr'd attributes in
J. Bruce Fieldse5f95702012-11-30 17:24:18 -05001396 * nfsd4_proc_verify */
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001399 verify->ve_attrlen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 READ_BUF(verify->ve_attrlen);
1401 SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
1402
1403 DECODE_TAIL;
1404}
1405
Al Virob37ad282006-10-19 23:28:59 -07001406static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1408{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 DECODE_HEAD;
1410
Benny Halevye31a1b62008-08-12 20:46:18 +03001411 status = nfsd4_decode_stateid(argp, &write->wr_stateid);
1412 if (status)
1413 return status;
1414 READ_BUF(16);
J. Bruce Fields542d1ab2014-06-02 12:45:31 -04001415 p = xdr_decode_hyper(p, &write->wr_offset);
J. Bruce Fields06553992014-06-02 12:32:51 -04001416 write->wr_stable_how = be32_to_cpup(p++);
Kinglong Mee54bbb7d2016-12-31 20:59:53 +08001417 if (write->wr_stable_how > NFS_FILE_SYNC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 goto xdr_error;
J. Bruce Fields06553992014-06-02 12:32:51 -04001419 write->wr_buflen = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Chuck Leverc1346a122020-11-03 11:54:23 -05001421 if (!xdr_stream_subsegment(argp->xdr, &write->wr_payload, write->wr_buflen))
1422 goto xdr_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 DECODE_TAIL;
1425}
1426
Al Virob37ad282006-10-19 23:28:59 -07001427static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1429{
1430 DECODE_HEAD;
1431
Anna Schumakere1a90eb2013-10-30 10:00:20 -04001432 if (argp->minorversion >= 1)
1433 return nfserr_notsupp;
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 READ_BUF(12);
1436 COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
J. Bruce Fields06553992014-06-02 12:32:51 -04001437 rlockowner->rl_owner.len = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 READ_BUF(rlockowner->rl_owner.len);
1439 READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
1440
Andy Adamson60adfc52009-04-03 08:28:50 +03001441 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1442 return nfserr_inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 DECODE_TAIL;
1444}
1445
Al Virob37ad282006-10-19 23:28:59 -07001446static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001447nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
Andy Adamson0733d212009-04-03 08:28:01 +03001448 struct nfsd4_exchange_id *exid)
Andy Adamson2db134e2009-04-03 08:27:55 +03001449{
Mi Jinlong5afa0402010-11-09 09:39:23 +08001450 int dummy, tmp;
Andy Adamson0733d212009-04-03 08:28:01 +03001451 DECODE_HEAD;
1452
1453 READ_BUF(NFS4_VERIFIER_SIZE);
1454 COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
1455
J. Bruce Fieldsa084daf2011-10-10 15:07:40 -04001456 status = nfsd4_decode_opaque(argp, &exid->clname);
1457 if (status)
1458 return nfserr_bad_xdr;
Andy Adamson0733d212009-04-03 08:28:01 +03001459
1460 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001461 exid->flags = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001462
1463 /* Ignore state_protect4_a */
1464 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001465 exid->spa_how = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001466 switch (exid->spa_how) {
1467 case SP4_NONE:
1468 break;
1469 case SP4_MACH_CRED:
1470 /* spo_must_enforce */
Andrew Elbleed941642016-06-15 12:52:09 -04001471 status = nfsd4_decode_bitmap(argp,
1472 exid->spo_must_enforce);
1473 if (status)
1474 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001475 /* spo_must_allow */
Andrew Elbleed941642016-06-15 12:52:09 -04001476 status = nfsd4_decode_bitmap(argp, exid->spo_must_allow);
1477 if (status)
1478 goto out;
Andy Adamson0733d212009-04-03 08:28:01 +03001479 break;
1480 case SP4_SSV:
1481 /* ssp_ops */
1482 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001483 dummy = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001484 READ_BUF(dummy * 4);
1485 p += dummy;
1486
1487 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001488 dummy = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001489 READ_BUF(dummy * 4);
1490 p += dummy;
1491
1492 /* ssp_hash_algs<> */
1493 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001494 tmp = be32_to_cpup(p++);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001495 while (tmp--) {
1496 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001497 dummy = be32_to_cpup(p++);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001498 READ_BUF(dummy);
1499 p += XDR_QUADLEN(dummy);
1500 }
Andy Adamson0733d212009-04-03 08:28:01 +03001501
1502 /* ssp_encr_algs<> */
1503 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001504 tmp = be32_to_cpup(p++);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001505 while (tmp--) {
1506 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001507 dummy = be32_to_cpup(p++);
Mi Jinlong5afa0402010-11-09 09:39:23 +08001508 READ_BUF(dummy);
1509 p += XDR_QUADLEN(dummy);
1510 }
Andy Adamson0733d212009-04-03 08:28:01 +03001511
nixiaoming5ed96bc2018-07-23 09:57:11 +08001512 /* ignore ssp_window and ssp_num_gss_handles: */
Andy Adamson0733d212009-04-03 08:28:01 +03001513 READ_BUF(8);
Andy Adamson0733d212009-04-03 08:28:01 +03001514 break;
1515 default:
1516 goto xdr_error;
1517 }
1518
Andy Adamson0733d212009-04-03 08:28:01 +03001519 READ_BUF(4); /* nfs_impl_id4 array length */
J. Bruce Fields06553992014-06-02 12:32:51 -04001520 dummy = be32_to_cpup(p++);
Andy Adamson0733d212009-04-03 08:28:01 +03001521
1522 if (dummy > 1)
1523 goto xdr_error;
1524
1525 if (dummy == 1) {
J. Bruce Fields79123442019-06-05 12:42:05 -04001526 status = nfsd4_decode_opaque(argp, &exid->nii_domain);
1527 if (status)
1528 goto xdr_error;
Andy Adamson0733d212009-04-03 08:28:01 +03001529
1530 /* nii_name */
J. Bruce Fields79123442019-06-05 12:42:05 -04001531 status = nfsd4_decode_opaque(argp, &exid->nii_name);
1532 if (status)
1533 goto xdr_error;
Andy Adamson0733d212009-04-03 08:28:01 +03001534
1535 /* nii_date */
J. Bruce Fields79123442019-06-05 12:42:05 -04001536 status = nfsd4_decode_time(argp, &exid->nii_time);
1537 if (status)
1538 goto xdr_error;
Andy Adamson0733d212009-04-03 08:28:01 +03001539 }
1540 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001541}
1542
1543static __be32
1544nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1545 struct nfsd4_create_session *sess)
1546{
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001547 DECODE_HEAD;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001548
1549 READ_BUF(16);
1550 COPYMEM(&sess->clientid, 8);
J. Bruce Fields06553992014-06-02 12:32:51 -04001551 sess->seqid = be32_to_cpup(p++);
1552 sess->flags = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001553
1554 /* Fore 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->fore_channel.maxreq_sz = be32_to_cpup(p++);
1558 sess->fore_channel.maxresp_sz = be32_to_cpup(p++);
1559 sess->fore_channel.maxresp_cached = be32_to_cpup(p++);
1560 sess->fore_channel.maxops = be32_to_cpup(p++);
1561 sess->fore_channel.maxreqs = be32_to_cpup(p++);
1562 sess->fore_channel.nr_rdma_attrs = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001563 if (sess->fore_channel.nr_rdma_attrs == 1) {
1564 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001565 sess->fore_channel.rdma_attrs = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001566 } else if (sess->fore_channel.nr_rdma_attrs > 1) {
1567 dprintk("Too many fore channel attr bitmaps!\n");
1568 goto xdr_error;
1569 }
1570
1571 /* Back channel attrs */
1572 READ_BUF(28);
Trond Myklebustb96811c2019-08-18 14:18:58 -04001573 p++; /* headerpadsz is always 0 */
J. Bruce Fields06553992014-06-02 12:32:51 -04001574 sess->back_channel.maxreq_sz = be32_to_cpup(p++);
1575 sess->back_channel.maxresp_sz = be32_to_cpup(p++);
1576 sess->back_channel.maxresp_cached = be32_to_cpup(p++);
1577 sess->back_channel.maxops = be32_to_cpup(p++);
1578 sess->back_channel.maxreqs = be32_to_cpup(p++);
1579 sess->back_channel.nr_rdma_attrs = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001580 if (sess->back_channel.nr_rdma_attrs == 1) {
1581 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001582 sess->back_channel.rdma_attrs = be32_to_cpup(p++);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001583 } else if (sess->back_channel.nr_rdma_attrs > 1) {
1584 dprintk("Too many back channel attr bitmaps!\n");
1585 goto xdr_error;
1586 }
1587
J. Bruce Fieldsacb28872012-03-27 14:50:26 -04001588 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001589 sess->callback_prog = be32_to_cpup(p++);
J. Bruce Fieldsacb28872012-03-27 14:50:26 -04001590 nfsd4_decode_cb_sec(argp, &sess->cb_sec);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03001591 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001592}
1593
1594static __be32
1595nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1596 struct nfsd4_destroy_session *destroy_session)
1597{
Benny Halevye10e0cf2009-04-03 08:28:38 +03001598 DECODE_HEAD;
1599 READ_BUF(NFS4_MAX_SESSIONID_LEN);
1600 COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1601
1602 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001603}
1604
1605static __be32
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001606nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1607 struct nfsd4_free_stateid *free_stateid)
1608{
1609 DECODE_HEAD;
1610
1611 READ_BUF(sizeof(stateid_t));
J. Bruce Fields06553992014-06-02 12:32:51 -04001612 free_stateid->fr_stateid.si_generation = be32_to_cpup(p++);
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04001613 COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
1614
1615 DECODE_TAIL;
1616}
1617
1618static __be32
Andy Adamson2db134e2009-04-03 08:27:55 +03001619nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1620 struct nfsd4_sequence *seq)
1621{
Benny Halevyb85d4c02009-04-03 08:28:08 +03001622 DECODE_HEAD;
1623
1624 READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
1625 COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
J. Bruce Fields06553992014-06-02 12:32:51 -04001626 seq->seqid = be32_to_cpup(p++);
1627 seq->slotid = be32_to_cpup(p++);
1628 seq->maxslots = be32_to_cpup(p++);
1629 seq->cachethis = be32_to_cpup(p++);
Benny Halevyb85d4c02009-04-03 08:28:08 +03001630
1631 DECODE_TAIL;
Andy Adamson2db134e2009-04-03 08:27:55 +03001632}
1633
Bryan Schumaker17456802011-07-13 10:50:48 -04001634static __be32
1635nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1636{
Bryan Schumaker17456802011-07-13 10:50:48 -04001637 int i;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001638 __be32 *p, status;
1639 struct nfsd4_test_stateid_id *stateid;
Bryan Schumaker17456802011-07-13 10:50:48 -04001640
1641 READ_BUF(4);
1642 test_stateid->ts_num_ids = ntohl(*p++);
1643
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001644 INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
Bryan Schumaker17456802011-07-13 10:50:48 -04001645
1646 for (i = 0; i < test_stateid->ts_num_ids; i++) {
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -04001647 stateid = svcxdr_tmpalloc(argp, sizeof(*stateid));
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001648 if (!stateid) {
Al Viroafcf6792012-04-13 00:15:37 -04001649 status = nfserrno(-ENOMEM);
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001650 goto out;
1651 }
1652
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001653 INIT_LIST_HEAD(&stateid->ts_id_list);
1654 list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
1655
1656 status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
Bryan Schumaker17456802011-07-13 10:50:48 -04001657 if (status)
Bryan Schumaker03cfb422012-01-27 10:22:49 -05001658 goto out;
Bryan Schumaker17456802011-07-13 10:50:48 -04001659 }
1660
1661 status = 0;
1662out:
1663 return status;
1664xdr_error:
1665 dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
1666 status = nfserr_bad_xdr;
1667 goto out;
1668}
1669
Mi Jinlong345c2842011-10-20 17:51:39 +08001670static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
1671{
1672 DECODE_HEAD;
1673
1674 READ_BUF(8);
1675 COPYMEM(&dc->clientid, 8);
1676
1677 DECODE_TAIL;
1678}
1679
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001680static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
1681{
1682 DECODE_HEAD;
1683
1684 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04001685 rc->rca_one_fs = be32_to_cpup(p++);
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04001686
1687 DECODE_TAIL;
1688}
1689
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001690#ifdef CONFIG_NFSD_PNFS
1691static __be32
1692nfsd4_decode_getdeviceinfo(struct nfsd4_compoundargs *argp,
1693 struct nfsd4_getdeviceinfo *gdev)
1694{
1695 DECODE_HEAD;
1696 u32 num, i;
1697
1698 READ_BUF(sizeof(struct nfsd4_deviceid) + 3 * 4);
1699 COPYMEM(&gdev->gd_devid, sizeof(struct nfsd4_deviceid));
1700 gdev->gd_layout_type = be32_to_cpup(p++);
1701 gdev->gd_maxcount = be32_to_cpup(p++);
1702 num = be32_to_cpup(p++);
1703 if (num) {
Scott Mayhew3171822f2018-06-08 16:31:46 -04001704 if (num > 1000)
1705 goto xdr_error;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001706 READ_BUF(4 * num);
1707 gdev->gd_notify_types = be32_to_cpup(p++);
1708 for (i = 1; i < num; i++) {
1709 if (be32_to_cpup(p++)) {
1710 status = nfserr_inval;
1711 goto out;
1712 }
1713 }
1714 }
1715 DECODE_TAIL;
1716}
1717
1718static __be32
1719nfsd4_decode_layoutget(struct nfsd4_compoundargs *argp,
1720 struct nfsd4_layoutget *lgp)
1721{
1722 DECODE_HEAD;
1723
1724 READ_BUF(36);
1725 lgp->lg_signal = be32_to_cpup(p++);
1726 lgp->lg_layout_type = be32_to_cpup(p++);
1727 lgp->lg_seg.iomode = be32_to_cpup(p++);
1728 p = xdr_decode_hyper(p, &lgp->lg_seg.offset);
1729 p = xdr_decode_hyper(p, &lgp->lg_seg.length);
1730 p = xdr_decode_hyper(p, &lgp->lg_minlength);
Kinglong Meedb59c0e2015-03-19 19:04:41 +08001731
1732 status = nfsd4_decode_stateid(argp, &lgp->lg_sid);
1733 if (status)
1734 return status;
1735
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001736 READ_BUF(4);
1737 lgp->lg_maxcount = be32_to_cpup(p++);
1738
1739 DECODE_TAIL;
1740}
1741
1742static __be32
1743nfsd4_decode_layoutcommit(struct nfsd4_compoundargs *argp,
1744 struct nfsd4_layoutcommit *lcp)
1745{
1746 DECODE_HEAD;
1747 u32 timechange;
1748
1749 READ_BUF(20);
1750 p = xdr_decode_hyper(p, &lcp->lc_seg.offset);
1751 p = xdr_decode_hyper(p, &lcp->lc_seg.length);
1752 lcp->lc_reclaim = be32_to_cpup(p++);
Kinglong Meedb59c0e2015-03-19 19:04:41 +08001753
1754 status = nfsd4_decode_stateid(argp, &lcp->lc_sid);
1755 if (status)
1756 return status;
1757
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001758 READ_BUF(4);
1759 lcp->lc_newoffset = be32_to_cpup(p++);
1760 if (lcp->lc_newoffset) {
1761 READ_BUF(8);
1762 p = xdr_decode_hyper(p, &lcp->lc_last_wr);
1763 } else
1764 lcp->lc_last_wr = 0;
1765 READ_BUF(4);
1766 timechange = be32_to_cpup(p++);
1767 if (timechange) {
1768 status = nfsd4_decode_time(argp, &lcp->lc_mtime);
1769 if (status)
1770 return status;
1771 } else {
1772 lcp->lc_mtime.tv_nsec = UTIME_NOW;
1773 }
1774 READ_BUF(8);
1775 lcp->lc_layout_type = be32_to_cpup(p++);
1776
1777 /*
1778 * Save the layout update in XDR format and let the layout driver deal
1779 * with it later.
1780 */
1781 lcp->lc_up_len = be32_to_cpup(p++);
1782 if (lcp->lc_up_len > 0) {
1783 READ_BUF(lcp->lc_up_len);
1784 READMEM(lcp->lc_up_layout, lcp->lc_up_len);
1785 }
1786
1787 DECODE_TAIL;
1788}
1789
1790static __be32
1791nfsd4_decode_layoutreturn(struct nfsd4_compoundargs *argp,
1792 struct nfsd4_layoutreturn *lrp)
1793{
1794 DECODE_HEAD;
1795
1796 READ_BUF(16);
1797 lrp->lr_reclaim = be32_to_cpup(p++);
1798 lrp->lr_layout_type = be32_to_cpup(p++);
1799 lrp->lr_seg.iomode = be32_to_cpup(p++);
1800 lrp->lr_return_type = be32_to_cpup(p++);
1801 if (lrp->lr_return_type == RETURN_FILE) {
1802 READ_BUF(16);
1803 p = xdr_decode_hyper(p, &lrp->lr_seg.offset);
1804 p = xdr_decode_hyper(p, &lrp->lr_seg.length);
Kinglong Meedb59c0e2015-03-19 19:04:41 +08001805
1806 status = nfsd4_decode_stateid(argp, &lrp->lr_sid);
1807 if (status)
1808 return status;
1809
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001810 READ_BUF(4);
1811 lrp->lrf_body_len = be32_to_cpup(p++);
1812 if (lrp->lrf_body_len > 0) {
1813 READ_BUF(lrp->lrf_body_len);
1814 READMEM(lrp->lrf_body, lrp->lrf_body_len);
1815 }
1816 } else {
1817 lrp->lr_seg.offset = 0;
1818 lrp->lr_seg.length = NFS4_MAX_UINT64;
1819 }
1820
1821 DECODE_TAIL;
1822}
1823#endif /* CONFIG_NFSD_PNFS */
1824
Andy Adamson2db134e2009-04-03 08:27:55 +03001825static __be32
Anna Schumaker95d871f2014-11-07 14:44:26 -05001826nfsd4_decode_fallocate(struct nfsd4_compoundargs *argp,
1827 struct nfsd4_fallocate *fallocate)
1828{
1829 DECODE_HEAD;
1830
1831 status = nfsd4_decode_stateid(argp, &fallocate->falloc_stateid);
1832 if (status)
1833 return status;
1834
1835 READ_BUF(16);
1836 p = xdr_decode_hyper(p, &fallocate->falloc_offset);
1837 xdr_decode_hyper(p, &fallocate->falloc_length);
1838
1839 DECODE_TAIL;
1840}
1841
1842static __be32
Christoph Hellwigffa01602015-12-03 12:59:52 +01001843nfsd4_decode_clone(struct nfsd4_compoundargs *argp, struct nfsd4_clone *clone)
1844{
1845 DECODE_HEAD;
1846
1847 status = nfsd4_decode_stateid(argp, &clone->cl_src_stateid);
1848 if (status)
1849 return status;
1850 status = nfsd4_decode_stateid(argp, &clone->cl_dst_stateid);
1851 if (status)
1852 return status;
1853
1854 READ_BUF(8 + 8 + 8);
1855 p = xdr_decode_hyper(p, &clone->cl_src_pos);
1856 p = xdr_decode_hyper(p, &clone->cl_dst_pos);
1857 p = xdr_decode_hyper(p, &clone->cl_count);
1858 DECODE_TAIL;
1859}
1860
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001861static __be32 nfsd4_decode_nl4_server(struct nfsd4_compoundargs *argp,
1862 struct nl4_server *ns)
1863{
1864 DECODE_HEAD;
1865 struct nfs42_netaddr *naddr;
1866
1867 READ_BUF(4);
1868 ns->nl4_type = be32_to_cpup(p++);
1869
1870 /* currently support for 1 inter-server source server */
1871 switch (ns->nl4_type) {
1872 case NL4_NETADDR:
1873 naddr = &ns->u.nl4_addr;
1874
1875 READ_BUF(4);
1876 naddr->netid_len = be32_to_cpup(p++);
1877 if (naddr->netid_len > RPCBIND_MAXNETIDLEN)
1878 goto xdr_error;
1879
1880 READ_BUF(naddr->netid_len + 4); /* 4 for uaddr len */
1881 COPYMEM(naddr->netid, naddr->netid_len);
1882
1883 naddr->addr_len = be32_to_cpup(p++);
1884 if (naddr->addr_len > RPCBIND_MAXUADDRLEN)
1885 goto xdr_error;
1886
1887 READ_BUF(naddr->addr_len);
1888 COPYMEM(naddr->addr, naddr->addr_len);
1889 break;
1890 default:
1891 goto xdr_error;
1892 }
1893 DECODE_TAIL;
1894}
1895
Christoph Hellwigffa01602015-12-03 12:59:52 +01001896static __be32
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001897nfsd4_decode_copy(struct nfsd4_compoundargs *argp, struct nfsd4_copy *copy)
1898{
1899 DECODE_HEAD;
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001900 struct nl4_server *ns_dummy;
1901 int i, count;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001902
1903 status = nfsd4_decode_stateid(argp, &copy->cp_src_stateid);
1904 if (status)
1905 return status;
1906 status = nfsd4_decode_stateid(argp, &copy->cp_dst_stateid);
1907 if (status)
1908 return status;
1909
1910 READ_BUF(8 + 8 + 8 + 4 + 4 + 4);
1911 p = xdr_decode_hyper(p, &copy->cp_src_pos);
1912 p = xdr_decode_hyper(p, &copy->cp_dst_pos);
1913 p = xdr_decode_hyper(p, &copy->cp_count);
J. Bruce Fieldsedcc8452018-03-07 15:37:35 -05001914 p++; /* ca_consecutive: we always do consecutive copies */
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001915 copy->cp_synchronous = be32_to_cpup(p++);
Olga Kornievskaia84e1b212019-09-13 14:00:57 -04001916
1917 count = be32_to_cpup(p++);
1918
1919 copy->cp_intra = false;
1920 if (count == 0) { /* intra-server copy */
1921 copy->cp_intra = true;
1922 goto intra;
1923 }
1924
1925 /* decode all the supplied server addresses but use first */
1926 status = nfsd4_decode_nl4_server(argp, &copy->cp_src);
1927 if (status)
1928 return status;
1929
1930 ns_dummy = kmalloc(sizeof(struct nl4_server), GFP_KERNEL);
1931 if (ns_dummy == NULL)
1932 return nfserrno(-ENOMEM);
1933 for (i = 0; i < count - 1; i++) {
1934 status = nfsd4_decode_nl4_server(argp, ns_dummy);
1935 if (status) {
1936 kfree(ns_dummy);
1937 return status;
1938 }
1939 }
1940 kfree(ns_dummy);
1941intra:
Anna Schumaker29ae7f92016-09-07 15:57:30 -04001942
1943 DECODE_TAIL;
1944}
1945
1946static __be32
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04001947nfsd4_decode_offload_status(struct nfsd4_compoundargs *argp,
1948 struct nfsd4_offload_status *os)
1949{
1950 return nfsd4_decode_stateid(argp, &os->stateid);
1951}
1952
1953static __be32
Olga Kornievskaia51911862019-08-08 11:14:59 -04001954nfsd4_decode_copy_notify(struct nfsd4_compoundargs *argp,
1955 struct nfsd4_copy_notify *cn)
1956{
Chuck Lever5aff7d02020-09-11 14:47:59 -04001957 __be32 status;
Olga Kornievskaia51911862019-08-08 11:14:59 -04001958
1959 status = nfsd4_decode_stateid(argp, &cn->cpn_src_stateid);
1960 if (status)
1961 return status;
1962 return nfsd4_decode_nl4_server(argp, &cn->cpn_dst);
1963}
1964
1965static __be32
Anna Schumaker24bab492014-09-26 13:58:27 -04001966nfsd4_decode_seek(struct nfsd4_compoundargs *argp, struct nfsd4_seek *seek)
1967{
1968 DECODE_HEAD;
1969
1970 status = nfsd4_decode_stateid(argp, &seek->seek_stateid);
1971 if (status)
1972 return status;
1973
1974 READ_BUF(8 + 4);
1975 p = xdr_decode_hyper(p, &seek->seek_offset);
1976 seek->seek_whence = be32_to_cpup(p);
1977
1978 DECODE_TAIL;
1979}
1980
Frank van der Linden23e50fe2020-06-23 22:39:26 +00001981/*
1982 * XDR data that is more than PAGE_SIZE in size is normally part of a
1983 * read or write. However, the size of extended attributes is limited
1984 * by the maximum request size, and then further limited by the underlying
1985 * filesystem limits. This can exceed PAGE_SIZE (currently, XATTR_SIZE_MAX
1986 * is 64k). Since there is no kvec- or page-based interface to xattrs,
1987 * and we're not dealing with contiguous pages, we need to do some copying.
1988 */
1989
1990/*
Chuck Leverc1346a122020-11-03 11:54:23 -05001991 * Decode data into buffer.
Frank van der Linden23e50fe2020-06-23 22:39:26 +00001992 */
1993static __be32
Chuck Leverc1346a122020-11-03 11:54:23 -05001994nfsd4_vbuf_from_vector(struct nfsd4_compoundargs *argp, struct xdr_buf *xdr,
1995 char **bufp, u32 buflen)
Frank van der Linden23e50fe2020-06-23 22:39:26 +00001996{
Chuck Leverc1346a122020-11-03 11:54:23 -05001997 struct page **pages = xdr->pages;
1998 struct kvec *head = xdr->head;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00001999 char *tmp, *dp;
2000 u32 len;
2001
2002 if (buflen <= head->iov_len) {
2003 /*
2004 * We're in luck, the head has enough space. Just return
2005 * the head, no need for copying.
2006 */
2007 *bufp = head->iov_base;
2008 return 0;
2009 }
2010
2011 tmp = svcxdr_tmpalloc(argp, buflen);
2012 if (tmp == NULL)
2013 return nfserr_jukebox;
2014
2015 dp = tmp;
2016 memcpy(dp, head->iov_base, head->iov_len);
2017 buflen -= head->iov_len;
2018 dp += head->iov_len;
2019
2020 while (buflen > 0) {
2021 len = min_t(u32, buflen, PAGE_SIZE);
2022 memcpy(dp, page_address(*pages), len);
2023
2024 buflen -= len;
2025 dp += len;
2026 pages++;
2027 }
2028
2029 *bufp = tmp;
2030 return 0;
2031}
2032
2033/*
2034 * Get a user extended attribute name from the XDR buffer.
2035 * It will not have the "user." prefix, so prepend it.
2036 * Lastly, check for nul characters in the name.
2037 */
2038static __be32
2039nfsd4_decode_xattr_name(struct nfsd4_compoundargs *argp, char **namep)
2040{
2041 DECODE_HEAD;
2042 char *name, *sp, *dp;
2043 u32 namelen, cnt;
2044
2045 READ_BUF(4);
2046 namelen = be32_to_cpup(p++);
2047
2048 if (namelen > (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN))
2049 return nfserr_nametoolong;
2050
2051 if (namelen == 0)
2052 goto xdr_error;
2053
2054 READ_BUF(namelen);
2055
2056 name = svcxdr_tmpalloc(argp, namelen + XATTR_USER_PREFIX_LEN + 1);
2057 if (!name)
2058 return nfserr_jukebox;
2059
2060 memcpy(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
2061
2062 /*
2063 * Copy the extended attribute name over while checking for 0
2064 * characters.
2065 */
2066 sp = (char *)p;
2067 dp = name + XATTR_USER_PREFIX_LEN;
2068 cnt = namelen;
2069
2070 while (cnt-- > 0) {
2071 if (*sp == '\0')
2072 goto xdr_error;
2073 *dp++ = *sp++;
2074 }
2075 *dp = '\0';
2076
2077 *namep = name;
2078
2079 DECODE_TAIL;
2080}
2081
2082/*
2083 * A GETXATTR op request comes without a length specifier. We just set the
2084 * maximum length for the reply based on XATTR_SIZE_MAX and the maximum
2085 * channel reply size. nfsd_getxattr will probe the length of the xattr,
2086 * check it against getxa_len, and allocate + return the value.
2087 */
2088static __be32
2089nfsd4_decode_getxattr(struct nfsd4_compoundargs *argp,
2090 struct nfsd4_getxattr *getxattr)
2091{
2092 __be32 status;
2093 u32 maxcount;
2094
2095 status = nfsd4_decode_xattr_name(argp, &getxattr->getxa_name);
2096 if (status)
2097 return status;
2098
2099 maxcount = svc_max_payload(argp->rqstp);
2100 maxcount = min_t(u32, XATTR_SIZE_MAX, maxcount);
2101
2102 getxattr->getxa_len = maxcount;
2103
2104 return status;
2105}
2106
2107static __be32
2108nfsd4_decode_setxattr(struct nfsd4_compoundargs *argp,
2109 struct nfsd4_setxattr *setxattr)
2110{
2111 DECODE_HEAD;
2112 u32 flags, maxcount, size;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002113
2114 READ_BUF(4);
2115 flags = be32_to_cpup(p++);
2116
2117 if (flags > SETXATTR4_REPLACE)
2118 return nfserr_inval;
2119 setxattr->setxa_flags = flags;
2120
2121 status = nfsd4_decode_xattr_name(argp, &setxattr->setxa_name);
2122 if (status)
2123 return status;
2124
2125 maxcount = svc_max_payload(argp->rqstp);
2126 maxcount = min_t(u32, XATTR_SIZE_MAX, maxcount);
2127
2128 READ_BUF(4);
2129 size = be32_to_cpup(p++);
2130 if (size > maxcount)
2131 return nfserr_xattr2big;
2132
2133 setxattr->setxa_len = size;
2134 if (size > 0) {
Chuck Leverc1346a122020-11-03 11:54:23 -05002135 struct xdr_buf payload;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002136
Chuck Leverc1346a122020-11-03 11:54:23 -05002137 if (!xdr_stream_subsegment(argp->xdr, &payload, size))
2138 goto xdr_error;
2139 status = nfsd4_vbuf_from_vector(argp, &payload,
2140 &setxattr->setxa_buf, size);
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002141 }
2142
2143 DECODE_TAIL;
2144}
2145
2146static __be32
2147nfsd4_decode_listxattrs(struct nfsd4_compoundargs *argp,
2148 struct nfsd4_listxattrs *listxattrs)
2149{
2150 DECODE_HEAD;
2151 u32 maxcount;
2152
2153 READ_BUF(12);
2154 p = xdr_decode_hyper(p, &listxattrs->lsxa_cookie);
2155
2156 /*
2157 * If the cookie is too large to have even one user.x attribute
2158 * plus trailing '\0' left in a maximum size buffer, it's invalid.
2159 */
2160 if (listxattrs->lsxa_cookie >=
2161 (XATTR_LIST_MAX / (XATTR_USER_PREFIX_LEN + 2)))
2162 return nfserr_badcookie;
2163
2164 maxcount = be32_to_cpup(p++);
2165 if (maxcount < 8)
2166 /* Always need at least 2 words (length and one character) */
2167 return nfserr_inval;
2168
2169 maxcount = min(maxcount, svc_max_payload(argp->rqstp));
2170 listxattrs->lsxa_maxcount = maxcount;
2171
2172 DECODE_TAIL;
2173}
2174
2175static __be32
2176nfsd4_decode_removexattr(struct nfsd4_compoundargs *argp,
2177 struct nfsd4_removexattr *removexattr)
2178{
2179 return nfsd4_decode_xattr_name(argp, &removexattr->rmxa_name);
2180}
2181
Anna Schumaker24bab492014-09-26 13:58:27 -04002182static __be32
Benny Halevy347e0ad2008-07-02 11:13:41 +03002183nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
2184{
2185 return nfs_ok;
2186}
2187
Benny Halevy3c375c62008-07-02 11:14:01 +03002188static __be32
2189nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
2190{
Benny Halevy1e685ec2009-03-04 23:06:06 +02002191 return nfserr_notsupp;
Benny Halevy3c375c62008-07-02 11:14:01 +03002192}
2193
Benny Halevy347e0ad2008-07-02 11:13:41 +03002194typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
2195
Chuck Leverc1df6092017-08-01 11:59:58 -04002196static const nfsd4_dec nfsd4_dec_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04002197 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
2198 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
2199 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
2200 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
2201 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
2202 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
2203 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
2204 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
2205 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
2206 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
2207 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
2208 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
2209 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
2210 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
2211 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
2212 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
2213 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
2214 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
2215 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
2216 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002217 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_putpubfh,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04002218 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
2219 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
2220 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
2221 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
2222 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
2223 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
2224 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
2225 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
2226 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
2227 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
2228 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
2229 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
2230 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
2231 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
2232 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
2233 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
Andy Adamson2db134e2009-04-03 08:27:55 +03002234
2235 /* new operations for NFSv4.1 */
J. Bruce Fieldscb73a9f2012-11-01 18:09:48 -04002236 [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04002237 [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
Randy Dunlap9064caa2009-04-28 16:48:25 -07002238 [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
2239 [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
2240 [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
Bryan Schumakere1ca12d2011-07-13 11:04:21 -04002241 [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07002242 [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002243#ifdef CONFIG_NFSD_PNFS
2244 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_getdeviceinfo,
2245 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
2246 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_layoutcommit,
2247 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_layoutget,
2248 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_layoutreturn,
2249#else
Randy Dunlap9064caa2009-04-28 16:48:25 -07002250 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
2251 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
2252 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
2253 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
2254 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002255#endif
J. Bruce Fields04f4ad12010-12-16 09:51:13 -05002256 [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
Randy Dunlap9064caa2009-04-28 16:48:25 -07002257 [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
2258 [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
Bryan Schumaker17456802011-07-13 10:50:48 -04002259 [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
Randy Dunlap9064caa2009-04-28 16:48:25 -07002260 [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
Mi Jinlong345c2842011-10-20 17:51:39 +08002261 [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
J. Bruce Fields4dc6ec02010-04-19 15:11:28 -04002262 [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
Anna Schumaker87a15a82014-09-26 13:58:26 -04002263
2264 /* new operations for NFSv4.2 */
Anna Schumaker95d871f2014-11-07 14:44:26 -05002265 [OP_ALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
Anna Schumaker29ae7f92016-09-07 15:57:30 -04002266 [OP_COPY] = (nfsd4_dec)nfsd4_decode_copy,
Olga Kornievskaia51911862019-08-08 11:14:59 -04002267 [OP_COPY_NOTIFY] = (nfsd4_dec)nfsd4_decode_copy_notify,
Anna Schumakerb0cb9082014-11-07 14:44:27 -05002268 [OP_DEALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
Anna Schumaker87a15a82014-09-26 13:58:26 -04002269 [OP_IO_ADVISE] = (nfsd4_dec)nfsd4_decode_notsupp,
2270 [OP_LAYOUTERROR] = (nfsd4_dec)nfsd4_decode_notsupp,
2271 [OP_LAYOUTSTATS] = (nfsd4_dec)nfsd4_decode_notsupp,
Olga Kornievskaia885e2bf2018-07-20 18:19:19 -04002272 [OP_OFFLOAD_CANCEL] = (nfsd4_dec)nfsd4_decode_offload_status,
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04002273 [OP_OFFLOAD_STATUS] = (nfsd4_dec)nfsd4_decode_offload_status,
Anna Schumaker528b8492020-09-28 13:08:58 -04002274 [OP_READ_PLUS] = (nfsd4_dec)nfsd4_decode_read,
Anna Schumaker24bab492014-09-26 13:58:27 -04002275 [OP_SEEK] = (nfsd4_dec)nfsd4_decode_seek,
Anna Schumaker87a15a82014-09-26 13:58:26 -04002276 [OP_WRITE_SAME] = (nfsd4_dec)nfsd4_decode_notsupp,
Christoph Hellwigffa01602015-12-03 12:59:52 +01002277 [OP_CLONE] = (nfsd4_dec)nfsd4_decode_clone,
Frank van der Linden23e50fe2020-06-23 22:39:26 +00002278 /* RFC 8276 extended atributes operations */
2279 [OP_GETXATTR] = (nfsd4_dec)nfsd4_decode_getxattr,
2280 [OP_SETXATTR] = (nfsd4_dec)nfsd4_decode_setxattr,
2281 [OP_LISTXATTRS] = (nfsd4_dec)nfsd4_decode_listxattrs,
2282 [OP_REMOVEXATTR] = (nfsd4_dec)nfsd4_decode_removexattr,
Andy Adamson2db134e2009-04-03 08:27:55 +03002283};
2284
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002285static inline bool
2286nfsd4_opnum_in_range(struct nfsd4_compoundargs *argp, struct nfsd4_op *op)
2287{
Anna Schumaker8217d142013-10-30 13:38:13 -04002288 if (op->opnum < FIRST_NFS4_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002289 return false;
Anna Schumaker8217d142013-10-30 13:38:13 -04002290 else if (argp->minorversion == 0 && op->opnum > LAST_NFS40_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002291 return false;
Anna Schumaker8217d142013-10-30 13:38:13 -04002292 else if (argp->minorversion == 1 && op->opnum > LAST_NFS41_OP)
2293 return false;
2294 else if (argp->minorversion == 2 && op->opnum > LAST_NFS42_OP)
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002295 return false;
2296 return true;
2297}
Benny Halevyf2feb962008-07-02 11:14:22 +03002298
Benny Halevy347e0ad2008-07-02 11:13:41 +03002299static __be32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
2301{
2302 DECODE_HEAD;
2303 struct nfsd4_op *op;
J. Bruce Fields10910062011-01-24 12:11:02 -05002304 bool cachethis = false;
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002305 int auth_slack= argp->rqstp->rq_auth_slack;
2306 int max_reply = auth_slack + 8; /* opcnt, status */
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05002307 int readcount = 0;
2308 int readbytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 int i;
2310
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04002312 argp->taglen = be32_to_cpup(p++);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05002313 READ_BUF(argp->taglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 SAVEMEM(argp->tag, argp->taglen);
J. Bruce Fields4aed9c42016-02-29 20:21:21 -05002315 READ_BUF(8);
J. Bruce Fields06553992014-06-02 12:32:51 -04002316 argp->minorversion = be32_to_cpup(p++);
2317 argp->opcnt = be32_to_cpup(p++);
J. Bruce Fields4f0cefb2014-03-11 15:39:13 -04002318 max_reply += 4 + (XDR_QUADLEN(argp->taglen) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
2320 if (argp->taglen > NFSD4_MAX_TAGLEN)
2321 goto xdr_error;
J. Bruce Fields00781172017-11-15 12:30:27 -05002322 /*
2323 * NFS4ERR_RESOURCE is a more helpful error than GARBAGE_ARGS
2324 * here, so we return success at the xdr level so that
2325 * nfsd4_proc can handle this is an NFS-level error.
2326 */
2327 if (argp->opcnt > NFSD_MAX_OPS_PER_COMPOUND)
2328 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Tobias Klausere8c96f82006-03-24 03:15:34 -08002330 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
J. Bruce Fields5d6031c2014-07-17 16:20:39 -04002331 argp->ops = kzalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 if (!argp->ops) {
2333 argp->ops = argp->iops;
Chuck Lever817cb9d2007-09-11 18:01:20 -04002334 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 goto xdr_error;
2336 }
2337 }
2338
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002339 if (argp->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
Benny Halevy30cff1f2008-07-02 11:13:18 +03002340 argp->opcnt = 0;
2341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 for (i = 0; i < argp->opcnt; i++) {
2343 op = &argp->ops[i];
2344 op->replay = NULL;
2345
J. Bruce Fields8a61b182012-11-16 22:28:38 -05002346 READ_BUF(4);
J. Bruce Fields06553992014-06-02 12:32:51 -04002347 op->opnum = be32_to_cpup(p++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Chuck Lever08281342020-11-21 11:36:42 -05002349 if (nfsd4_opnum_in_range(argp, op)) {
Anna Schumakere1a90eb2013-10-30 10:00:20 -04002350 op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
Chuck Lever08281342020-11-21 11:36:42 -05002351 if (op->status != nfs_ok)
2352 trace_nfsd_compound_decode_err(argp->rqstp,
2353 argp->opcnt, i,
2354 op->opnum,
2355 op->status);
2356 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 op->opnum = OP_ILLEGAL;
2358 op->status = nfserr_op_illegal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 }
J. Bruce Fieldsf4f9ef42017-07-06 17:51:29 -04002360 op->opdesc = OPDESC(op);
J. Bruce Fields10910062011-01-24 12:11:02 -05002361 /*
2362 * We'll try to cache the result in the DRC if any one
2363 * op in the compound wants to be cached:
2364 */
2365 cachethis |= nfsd4_cache_this_op(op);
J. Bruce Fields6ff40de2013-11-05 15:07:16 -05002366
Anna Schumaker528b8492020-09-28 13:08:58 -04002367 if (op->opnum == OP_READ || op->opnum == OP_READ_PLUS) {
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05002368 readcount++;
2369 readbytes += nfsd4_max_reply(argp->rqstp, op);
2370 } else
2371 max_reply += nfsd4_max_reply(argp->rqstp, op);
J. Bruce Fieldsf7b43d02014-08-12 11:41:40 -04002372 /*
Kinglong Mee7323f0d2017-02-03 22:51:46 +08002373 * OP_LOCK and OP_LOCKT may return a conflicting lock.
2374 * (Special case because it will just skip encoding this
2375 * if it runs out of xdr buffer space, and it is the only
2376 * operation that behaves this way.)
J. Bruce Fieldsf7b43d02014-08-12 11:41:40 -04002377 */
Kinglong Mee7323f0d2017-02-03 22:51:46 +08002378 if (op->opnum == OP_LOCK || op->opnum == OP_LOCKT)
J. Bruce Fieldsf7b43d02014-08-12 11:41:40 -04002379 max_reply += NFS4_OPAQUE_LIMIT;
J. Bruce Fieldse372ba62014-05-19 12:27:11 -04002380
2381 if (op->status) {
2382 argp->opcnt = i+1;
2383 break;
2384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 }
J. Bruce Fields10910062011-01-24 12:11:02 -05002386 /* Sessions make the DRC unnecessary: */
2387 if (argp->minorversion)
2388 cachethis = false;
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05002389 svc_reserve(argp->rqstp, max_reply + readbytes);
J. Bruce Fields10910062011-01-24 12:11:02 -05002390 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
J. Bruce Fieldsa5cddc82014-05-12 18:10:58 -04002392 if (readcount > 1 || max_reply > PAGE_SIZE - auth_slack)
Jeff Layton779fb0f2014-11-19 07:51:18 -05002393 clear_bit(RQ_SPLICE_OK, &argp->rqstp->rq_flags);
J. Bruce Fieldsb0e35fd2014-02-04 10:36:59 -05002394
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 DECODE_TAIL;
2396}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
NeilBrownb8800922017-01-30 17:17:00 +11002398static __be32 *encode_change(__be32 *p, struct kstat *stat, struct inode *inode,
2399 struct svc_export *exp)
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002400{
NeilBrownb8800922017-01-30 17:17:00 +11002401 if (exp->ex_flags & NFSEXP_V4ROOT) {
2402 *p++ = cpu_to_be32(convert_to_wallclock(exp->cd->flush_time));
2403 *p++ = 0;
2404 } else if (IS_I_VERSION(inode)) {
Amir Goldstein39ca1bf2018-01-03 17:14:35 +02002405 p = xdr_encode_hyper(p, nfsd4_change_attribute(stat, inode));
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002406 } else {
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002407 *p++ = cpu_to_be32(stat->ctime.tv_sec);
2408 *p++ = cpu_to_be32(stat->ctime.tv_nsec);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002409 }
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002410 return p;
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002411}
2412
J. Bruce Fields16945142018-04-25 14:34:11 -04002413/*
2414 * ctime (in NFSv4, time_metadata) is not writeable, and the client
2415 * doesn't really care what resolution could theoretically be stored by
2416 * the filesystem.
2417 *
2418 * The client cares how close together changes can be while still
2419 * guaranteeing ctime changes. For most filesystems (which have
2420 * timestamps with nanosecond fields) that is limited by the resolution
2421 * of the time returned from current_time() (which I'm assuming to be
2422 * 1/HZ).
2423 */
2424static __be32 *encode_time_delta(__be32 *p, struct inode *inode)
2425{
Arnd Bergmanne4598e32019-10-31 15:14:03 +01002426 struct timespec64 ts;
J. Bruce Fields16945142018-04-25 14:34:11 -04002427 u32 ns;
2428
2429 ns = max_t(u32, NSEC_PER_SEC/HZ, inode->i_sb->s_time_gran);
Arnd Bergmanne4598e32019-10-31 15:14:03 +01002430 ts = ns_to_timespec64(ns);
J. Bruce Fields16945142018-04-25 14:34:11 -04002431
2432 p = xdr_encode_hyper(p, ts.tv_sec);
2433 *p++ = cpu_to_be32(ts.tv_nsec);
2434
2435 return p;
2436}
2437
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002438static __be32 *encode_cinfo(__be32 *p, struct nfsd4_change_info *c)
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002439{
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002440 *p++ = cpu_to_be32(c->atomic);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002441 if (c->change_supported) {
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002442 p = xdr_encode_hyper(p, c->before_change);
2443 p = xdr_encode_hyper(p, c->after_change);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002444 } else {
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002445 *p++ = cpu_to_be32(c->before_ctime_sec);
2446 *p++ = cpu_to_be32(c->before_ctime_nsec);
2447 *p++ = cpu_to_be32(c->after_ctime_sec);
2448 *p++ = cpu_to_be32(c->after_ctime_nsec);
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002449 }
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04002450 return p;
J. Bruce Fieldsc654b8a2009-04-16 17:33:25 -04002451}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002453/* Encode as an array of strings the string given with components
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002454 * separated @sep, escaped with esc_enter and esc_exit.
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002455 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002456static __be32 nfsd4_encode_components_esc(struct xdr_stream *xdr, char sep,
2457 char *components, char esc_enter,
2458 char esc_exit)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002459{
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002460 __be32 *p;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002461 __be32 pathlen;
2462 int pathlen_offset;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002463 int strlen, count=0;
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002464 char *str, *end, *next;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002465
2466 dprintk("nfsd4_encode_components(%s)\n", components);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002467
2468 pathlen_offset = xdr->buf->len;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002469 p = xdr_reserve_space(xdr, 4);
2470 if (!p)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002471 return nfserr_resource;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002472 p++; /* We will fill this in with @count later */
2473
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002474 end = str = components;
2475 while (*end) {
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002476 bool found_esc = false;
2477
2478 /* try to parse as esc_start, ..., esc_end, sep */
2479 if (*str == esc_enter) {
2480 for (; *end && (*end != esc_exit); end++)
2481 /* find esc_exit or end of string */;
2482 next = end + 1;
2483 if (*end && (!*next || *next == sep)) {
2484 str++;
2485 found_esc = true;
2486 }
2487 }
2488
2489 if (!found_esc)
2490 for (; *end && (*end != sep); end++)
2491 /* find sep or end of string */;
2492
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002493 strlen = end - str;
2494 if (strlen) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002495 p = xdr_reserve_space(xdr, strlen + 4);
2496 if (!p)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002497 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04002498 p = xdr_encode_opaque(p, str, strlen);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002499 count++;
2500 }
2501 else
2502 end++;
Benjamin Coddington5a64e562014-12-07 16:05:47 -05002503 if (found_esc)
2504 end = next;
2505
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002506 str = end;
2507 }
Benjamin Coddingtonbf7491f2014-12-07 16:05:48 -05002508 pathlen = htonl(count);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002509 write_bytes_to_xdr_buf(xdr->buf, pathlen_offset, &pathlen, 4);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002510 return 0;
2511}
2512
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002513/* Encode as an array of strings the string given with components
2514 * separated @sep.
2515 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002516static __be32 nfsd4_encode_components(struct xdr_stream *xdr, char sep,
2517 char *components)
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002518{
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002519 return nfsd4_encode_components_esc(xdr, sep, components, 0, 0);
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002520}
2521
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002522/*
2523 * encode a location element of a fs_locations structure
2524 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002525static __be32 nfsd4_encode_fs_location4(struct xdr_stream *xdr,
2526 struct nfsd4_fs_location *location)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002527{
Al Virob37ad282006-10-19 23:28:59 -07002528 __be32 status;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002529
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002530 status = nfsd4_encode_components_esc(xdr, ':', location->hosts,
Weston Andros Adamsone7a04442012-04-24 11:07:59 -04002531 '[', ']');
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002532 if (status)
2533 return status;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002534 status = nfsd4_encode_components(xdr, '/', location->path);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002535 if (status)
2536 return status;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002537 return 0;
2538}
2539
2540/*
Trond Myklebusted748aa2011-09-12 19:37:06 -04002541 * Encode a path in RFC3530 'pathname4' format
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002542 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002543static __be32 nfsd4_encode_path(struct xdr_stream *xdr,
2544 const struct path *root,
2545 const struct path *path)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002546{
Al Viro301f0262013-09-01 16:06:39 -04002547 struct path cur = *path;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002548 __be32 *p;
Trond Myklebusted748aa2011-09-12 19:37:06 -04002549 struct dentry **components = NULL;
2550 unsigned int ncomponents = 0;
2551 __be32 err = nfserr_jukebox;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002552
Trond Myklebusted748aa2011-09-12 19:37:06 -04002553 dprintk("nfsd4_encode_components(");
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002554
Trond Myklebusted748aa2011-09-12 19:37:06 -04002555 path_get(&cur);
2556 /* First walk the path up to the nfsd root, and store the
2557 * dentries/path components in an array.
2558 */
2559 for (;;) {
Kinglong Meeb77a4b22015-03-15 23:13:32 +08002560 if (path_equal(&cur, root))
Trond Myklebusted748aa2011-09-12 19:37:06 -04002561 break;
2562 if (cur.dentry == cur.mnt->mnt_root) {
2563 if (follow_up(&cur))
2564 continue;
2565 goto out_free;
2566 }
2567 if ((ncomponents & 15) == 0) {
2568 struct dentry **new;
2569 new = krealloc(components,
2570 sizeof(*new) * (ncomponents + 16),
2571 GFP_KERNEL);
2572 if (!new)
2573 goto out_free;
2574 components = new;
2575 }
2576 components[ncomponents++] = cur.dentry;
2577 cur.dentry = dget_parent(cur.dentry);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002578 }
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002579 err = nfserr_resource;
2580 p = xdr_reserve_space(xdr, 4);
2581 if (!p)
Trond Myklebusted748aa2011-09-12 19:37:06 -04002582 goto out_free;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002583 *p++ = cpu_to_be32(ncomponents);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002584
2585 while (ncomponents) {
2586 struct dentry *dentry = components[ncomponents - 1];
Al Viro301f0262013-09-01 16:06:39 -04002587 unsigned int len;
Trond Myklebusted748aa2011-09-12 19:37:06 -04002588
Al Viro301f0262013-09-01 16:06:39 -04002589 spin_lock(&dentry->d_lock);
2590 len = dentry->d_name.len;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002591 p = xdr_reserve_space(xdr, len + 4);
2592 if (!p) {
Al Viro301f0262013-09-01 16:06:39 -04002593 spin_unlock(&dentry->d_lock);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002594 goto out_free;
Al Viro301f0262013-09-01 16:06:39 -04002595 }
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04002596 p = xdr_encode_opaque(p, dentry->d_name.name, len);
Al Viroa4555892014-10-21 20:11:25 -04002597 dprintk("/%pd", dentry);
Al Viro301f0262013-09-01 16:06:39 -04002598 spin_unlock(&dentry->d_lock);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002599 dput(dentry);
2600 ncomponents--;
2601 }
2602
Trond Myklebusted748aa2011-09-12 19:37:06 -04002603 err = 0;
2604out_free:
2605 dprintk(")\n");
2606 while (ncomponents)
2607 dput(components[--ncomponents]);
2608 kfree(components);
2609 path_put(&cur);
2610 return err;
2611}
2612
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002613static __be32 nfsd4_encode_fsloc_fsroot(struct xdr_stream *xdr,
2614 struct svc_rqst *rqstp, const struct path *path)
Trond Myklebusted748aa2011-09-12 19:37:06 -04002615{
2616 struct svc_export *exp_ps;
2617 __be32 res;
2618
2619 exp_ps = rqst_find_fsidzero_export(rqstp);
2620 if (IS_ERR(exp_ps))
2621 return nfserrno(PTR_ERR(exp_ps));
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002622 res = nfsd4_encode_path(xdr, &exp_ps->ex_path, path);
Trond Myklebusted748aa2011-09-12 19:37:06 -04002623 exp_put(exp_ps);
2624 return res;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002625}
2626
2627/*
2628 * encode a fs_locations structure
2629 */
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002630static __be32 nfsd4_encode_fs_locations(struct xdr_stream *xdr,
2631 struct svc_rqst *rqstp, struct svc_export *exp)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002632{
Al Virob37ad282006-10-19 23:28:59 -07002633 __be32 status;
Al Virocc45f012006-10-19 23:28:44 -07002634 int i;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002635 __be32 *p;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002636 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002637
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002638 status = nfsd4_encode_fsloc_fsroot(xdr, rqstp, &exp->ex_path);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002639 if (status)
2640 return status;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002641 p = xdr_reserve_space(xdr, 4);
2642 if (!p)
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002643 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002644 *p++ = cpu_to_be32(fslocs->locations_count);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002645 for (i=0; i<fslocs->locations_count; i++) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002646 status = nfsd4_encode_fs_location4(xdr, &fslocs->locations[i]);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002647 if (status)
2648 return status;
2649 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07002650 return 0;
2651}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002653static u32 nfs4_file_type(umode_t mode)
2654{
2655 switch (mode & S_IFMT) {
2656 case S_IFIFO: return NF4FIFO;
2657 case S_IFCHR: return NF4CHR;
2658 case S_IFDIR: return NF4DIR;
2659 case S_IFBLK: return NF4BLK;
2660 case S_IFLNK: return NF4LNK;
2661 case S_IFREG: return NF4REG;
2662 case S_IFSOCK: return NF4SOCK;
2663 default: return NF4BAD;
Tom Rix25fef482020-11-01 07:32:34 -08002664 }
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002665}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Al Virob37ad282006-10-19 23:28:59 -07002667static inline __be32
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002668nfsd4_encode_aclname(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2669 struct nfs4_ace *ace)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670{
J. Bruce Fields35541162014-01-08 09:49:01 -05002671 if (ace->whotype != NFS4_ACL_WHO_NAMED)
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002672 return nfs4_acl_write_who(xdr, ace->whotype);
J. Bruce Fields35541162014-01-08 09:49:01 -05002673 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002674 return nfsd4_encode_group(xdr, rqstp, ace->who_gid);
J. Bruce Fields35541162014-01-08 09:49:01 -05002675 else
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002676 return nfsd4_encode_user(xdr, rqstp, ace->who_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677}
2678
Kinglong Mee6896f152015-07-30 21:52:44 +08002679static inline __be32
Jeff Layton8a4c3922016-07-10 15:55:58 -04002680nfsd4_encode_layout_types(struct xdr_stream *xdr, u32 layout_types)
Kinglong Mee6896f152015-07-30 21:52:44 +08002681{
Jeff Layton8a4c3922016-07-10 15:55:58 -04002682 __be32 *p;
2683 unsigned long i = hweight_long(layout_types);
Kinglong Mee6896f152015-07-30 21:52:44 +08002684
Jeff Layton8a4c3922016-07-10 15:55:58 -04002685 p = xdr_reserve_space(xdr, 4 + 4 * i);
2686 if (!p)
2687 return nfserr_resource;
2688
2689 *p++ = cpu_to_be32(i);
2690
2691 for (i = LAYOUT_NFSV4_1_FILES; i < LAYOUT_TYPE_MAX; ++i)
2692 if (layout_types & (1 << i))
2693 *p++ = cpu_to_be32(i);
Kinglong Mee6896f152015-07-30 21:52:44 +08002694
2695 return 0;
2696}
2697
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002698#define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
2699 FATTR4_WORD0_RDATTR_ERROR)
2700#define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
Kinglong Meec2227a32015-07-07 10:16:37 +08002701#define WORD2_ABSENT_FS_ATTRS 0
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002702
David Quigley18032ca2013-05-02 13:19:10 -04002703#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2704static inline __be32
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002705nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2706 void *context, int len)
David Quigley18032ca2013-05-02 13:19:10 -04002707{
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002708 __be32 *p;
David Quigley18032ca2013-05-02 13:19:10 -04002709
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002710 p = xdr_reserve_space(xdr, len + 4 + 4 + 4);
2711 if (!p)
David Quigley18032ca2013-05-02 13:19:10 -04002712 return nfserr_resource;
2713
2714 /*
2715 * For now we use a 0 here to indicate the null translation; in
2716 * the future we may place a call to translation code here.
2717 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002718 *p++ = cpu_to_be32(0); /* lfs */
2719 *p++ = cpu_to_be32(0); /* pi */
David Quigley18032ca2013-05-02 13:19:10 -04002720 p = xdr_encode_opaque(p, context, len);
David Quigley18032ca2013-05-02 13:19:10 -04002721 return 0;
2722}
2723#else
2724static inline __be32
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002725nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2726 void *context, int len)
David Quigley18032ca2013-05-02 13:19:10 -04002727{ return 0; }
2728#endif
2729
Kinglong Meec2227a32015-07-07 10:16:37 +08002730static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *bmval2, u32 *rdattr_err)
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002731{
2732 /* As per referral draft: */
2733 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
2734 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
2735 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
2736 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
2737 *rdattr_err = NFSERR_MOVED;
2738 else
2739 return nfserr_moved;
2740 }
2741 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
2742 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
Kinglong Meec2227a32015-07-07 10:16:37 +08002743 *bmval2 &= WORD2_ABSENT_FS_ATTRS;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002744 return 0;
2745}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002747
2748static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
2749{
2750 struct path path = exp->ex_path;
2751 int err;
2752
2753 path_get(&path);
2754 while (follow_up(&path)) {
2755 if (path.dentry != path.mnt->mnt_root)
2756 break;
2757 }
David Howellsa528d352017-01-31 16:46:22 +00002758 err = vfs_getattr(&path, stat, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
J. Bruce Fieldsae7095a2012-10-01 17:50:56 -04002759 path_put(&path);
2760 return err;
2761}
2762
Kinglong Mee75976de2015-07-30 21:55:30 +08002763static __be32
2764nfsd4_encode_bitmap(struct xdr_stream *xdr, u32 bmval0, u32 bmval1, u32 bmval2)
2765{
2766 __be32 *p;
2767
2768 if (bmval2) {
2769 p = xdr_reserve_space(xdr, 16);
2770 if (!p)
2771 goto out_resource;
2772 *p++ = cpu_to_be32(3);
2773 *p++ = cpu_to_be32(bmval0);
2774 *p++ = cpu_to_be32(bmval1);
2775 *p++ = cpu_to_be32(bmval2);
2776 } else if (bmval1) {
2777 p = xdr_reserve_space(xdr, 12);
2778 if (!p)
2779 goto out_resource;
2780 *p++ = cpu_to_be32(2);
2781 *p++ = cpu_to_be32(bmval0);
2782 *p++ = cpu_to_be32(bmval1);
2783 } else {
2784 p = xdr_reserve_space(xdr, 8);
2785 if (!p)
2786 goto out_resource;
2787 *p++ = cpu_to_be32(1);
2788 *p++ = cpu_to_be32(bmval0);
2789 }
2790
2791 return 0;
2792out_resource:
2793 return nfserr_resource;
2794}
2795
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796/*
2797 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
2798 * ourselves.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 */
Jeff Laytonda2ebce2014-05-30 09:09:25 -04002800static __be32
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04002801nfsd4_encode_fattr(struct xdr_stream *xdr, struct svc_fh *fhp,
2802 struct svc_export *exp,
2803 struct dentry *dentry, u32 *bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08002804 struct svc_rqst *rqstp, int ignore_crossmnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805{
2806 u32 bmval0 = bmval[0];
2807 u32 bmval1 = bmval[1];
Andy Adamson7e705702009-04-03 08:29:11 +03002808 u32 bmval2 = bmval[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 struct kstat stat;
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002810 struct svc_fh *tempfh = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 struct kstatfs statfs;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002812 __be32 *p;
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05002813 int starting_len = xdr->buf->len;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002814 int attrlen_offset;
2815 __be32 attrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 u32 dummy;
2817 u64 dummy64;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002818 u32 rdattr_err = 0;
Al Virob37ad282006-10-19 23:28:59 -07002819 __be32 status;
Al Virob8dd7b92006-10-19 23:29:01 -07002820 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 struct nfs4_acl *acl = NULL;
Arnd Bergmann0ab88ca2019-03-22 15:07:11 +01002822#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
David Quigley18032ca2013-05-02 13:19:10 -04002823 void *context = NULL;
2824 int contextlen;
Arnd Bergmann0ab88ca2019-03-22 15:07:11 +01002825#endif
David Quigley18032ca2013-05-02 13:19:10 -04002826 bool contextsupport = false;
Andy Adamson7e705702009-04-03 08:29:11 +03002827 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2828 u32 minorversion = resp->cstate.minorversion;
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002829 struct path path = {
2830 .mnt = exp->ex_path.mnt,
2831 .dentry = dentry,
2832 };
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +03002833 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
2835 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002836 BUG_ON(!nfsd_attrs_supported(minorversion, bmval));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002838 if (exp->ex_fslocs.migrated) {
Kinglong Meec2227a32015-07-07 10:16:37 +08002839 status = fattr_handle_absent_fs(&bmval0, &bmval1, &bmval2, &rdattr_err);
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002840 if (status)
2841 goto out;
2842 }
2843
David Howellsa528d352017-01-31 16:46:22 +00002844 err = vfs_getattr(&path, &stat, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
Al Virob8dd7b92006-10-19 23:29:01 -07002845 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 goto out_nfserr;
Christoph Hellwig12337902014-05-28 10:46:13 +02002847 if ((bmval0 & (FATTR4_WORD0_FILES_AVAIL | FATTR4_WORD0_FILES_FREE |
2848 FATTR4_WORD0_FILES_TOTAL | FATTR4_WORD0_MAXNAME)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2850 FATTR4_WORD1_SPACE_TOTAL))) {
Christoph Hellwigebabe9a2010-07-07 18:53:11 +02002851 err = vfs_statfs(&path, &statfs);
Al Virob8dd7b92006-10-19 23:29:01 -07002852 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 goto out_nfserr;
2854 }
2855 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002856 tempfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
2857 status = nfserr_jukebox;
2858 if (!tempfh)
2859 goto out;
2860 fh_init(tempfh, NFS4_FHSIZE);
2861 status = fh_compose(tempfh, exp, dentry, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 if (status)
2863 goto out;
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05002864 fhp = tempfh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 }
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02002866 if (bmval0 & FATTR4_WORD0_ACL) {
Al Virob8dd7b92006-10-19 23:29:01 -07002867 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02002868 if (err == -EOPNOTSUPP)
2869 bmval0 &= ~FATTR4_WORD0_ACL;
2870 else if (err == -EINVAL) {
2871 status = nfserr_attrnotsupp;
2872 goto out;
2873 } else if (err != 0)
2874 goto out_nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
David Quigley18032ca2013-05-02 13:19:10 -04002877#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
Kinglong Meec2227a32015-07-07 10:16:37 +08002878 if ((bmval2 & FATTR4_WORD2_SECURITY_LABEL) ||
2879 bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
J. Bruce Fields32ddd942017-01-03 12:30:11 -05002880 if (exp->ex_flags & NFSEXP_SECURITY_LABEL)
2881 err = security_inode_getsecctx(d_inode(dentry),
David Quigley18032ca2013-05-02 13:19:10 -04002882 &context, &contextlen);
J. Bruce Fields32ddd942017-01-03 12:30:11 -05002883 else
2884 err = -EOPNOTSUPP;
David Quigley18032ca2013-05-02 13:19:10 -04002885 contextsupport = (err == 0);
2886 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2887 if (err == -EOPNOTSUPP)
2888 bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2889 else if (err)
2890 goto out_nfserr;
2891 }
2892 }
2893#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2894
Kinglong Mee75976de2015-07-30 21:55:30 +08002895 status = nfsd4_encode_bitmap(xdr, bmval0, bmval1, bmval2);
2896 if (status)
2897 goto out;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002898
2899 attrlen_offset = xdr->buf->len;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002900 p = xdr_reserve_space(xdr, 4);
2901 if (!p)
2902 goto out_resource;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04002903 p++; /* to be backfilled later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
2905 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
J. Bruce Fieldsdcd208692017-01-11 20:34:50 -05002906 u32 supp[3];
2907
2908 memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
Andy Adamson7e705702009-04-03 08:29:11 +03002909
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02002910 if (!IS_POSIXACL(dentry->d_inode))
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002911 supp[0] &= ~FATTR4_WORD0_ACL;
David Quigley18032ca2013-05-02 13:19:10 -04002912 if (!contextsupport)
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002913 supp[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2914 if (!supp[2]) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002915 p = xdr_reserve_space(xdr, 12);
2916 if (!p)
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002917 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002918 *p++ = cpu_to_be32(2);
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002919 *p++ = cpu_to_be32(supp[0]);
2920 *p++ = cpu_to_be32(supp[1]);
Andy Adamson7e705702009-04-03 08:29:11 +03002921 } else {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002922 p = xdr_reserve_space(xdr, 16);
2923 if (!p)
Benny Halevy2b44f1b2010-09-30 20:47:46 +02002924 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002925 *p++ = cpu_to_be32(3);
J. Bruce Fields916d2d82016-10-18 14:18:40 -04002926 *p++ = cpu_to_be32(supp[0]);
2927 *p++ = cpu_to_be32(supp[1]);
2928 *p++ = cpu_to_be32(supp[2]);
Andy Adamson7e705702009-04-03 08:29:11 +03002929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 }
2931 if (bmval0 & FATTR4_WORD0_TYPE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002932 p = xdr_reserve_space(xdr, 4);
2933 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 goto out_resource;
J. Bruce Fields3d2544b2011-08-15 11:49:30 -04002935 dummy = nfs4_file_type(stat.mode);
J. Bruce Fields6b6d81372013-01-16 17:11:11 -05002936 if (dummy == NF4BAD) {
2937 status = nfserr_serverfault;
2938 goto out;
2939 }
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002940 *p++ = cpu_to_be32(dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 }
2942 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002943 p = xdr_reserve_space(xdr, 4);
2944 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 goto out_resource;
NeilBrown49640002005-06-23 22:02:58 -07002946 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002947 *p++ = cpu_to_be32(NFS4_FH_PERSISTENT);
NeilBrown49640002005-06-23 22:02:58 -07002948 else
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002949 *p++ = cpu_to_be32(NFS4_FH_PERSISTENT|
2950 NFS4_FH_VOL_RENAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 }
2952 if (bmval0 & FATTR4_WORD0_CHANGE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002953 p = xdr_reserve_space(xdr, 8);
2954 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 goto out_resource;
NeilBrownb8800922017-01-30 17:17:00 +11002956 p = encode_change(p, &stat, d_inode(dentry), exp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 }
2958 if (bmval0 & FATTR4_WORD0_SIZE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002959 p = xdr_reserve_space(xdr, 8);
2960 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002962 p = xdr_encode_hyper(p, stat.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 }
2964 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002965 p = xdr_reserve_space(xdr, 4);
2966 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002968 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 }
2970 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002971 p = xdr_reserve_space(xdr, 4);
2972 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002974 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 }
2976 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002977 p = xdr_reserve_space(xdr, 4);
2978 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002980 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 }
2982 if (bmval0 & FATTR4_WORD0_FSID) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04002983 p = xdr_reserve_space(xdr, 16);
2984 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 goto out_resource;
J.Bruce Fields42ca0992006-10-04 02:16:20 -07002986 if (exp->ex_fslocs.migrated) {
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002987 p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MAJOR);
2988 p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MINOR);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002989 } else switch(fsid_source(fhp)) {
2990 case FSIDSOURCE_FSID:
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04002991 p = xdr_encode_hyper(p, (u64)exp->ex_fsid);
2992 p = xdr_encode_hyper(p, (u64)0);
NeilBrownaf6a4e22007-02-14 00:33:12 -08002993 break;
2994 case FSIDSOURCE_DEV:
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04002995 *p++ = cpu_to_be32(0);
2996 *p++ = cpu_to_be32(MAJOR(stat.dev));
2997 *p++ = cpu_to_be32(0);
2998 *p++ = cpu_to_be32(MINOR(stat.dev));
NeilBrownaf6a4e22007-02-14 00:33:12 -08002999 break;
3000 case FSIDSOURCE_UUID:
Kinglong Mee94eb3682014-05-23 20:00:19 +08003001 p = xdr_encode_opaque_fixed(p, exp->ex_uuid,
3002 EX_UUID_LEN);
NeilBrownaf6a4e22007-02-14 00:33:12 -08003003 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 }
3005 }
3006 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003007 p = xdr_reserve_space(xdr, 4);
3008 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003010 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 }
3012 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003013 p = xdr_reserve_space(xdr, 4);
3014 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003016 *p++ = cpu_to_be32(nn->nfsd4_lease);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 }
3018 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003019 p = xdr_reserve_space(xdr, 4);
3020 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003022 *p++ = cpu_to_be32(rdattr_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 }
3024 if (bmval0 & FATTR4_WORD0_ACL) {
3025 struct nfs4_ace *ace;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
3027 if (acl == NULL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003028 p = xdr_reserve_space(xdr, 4);
3029 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 goto out_resource;
3031
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003032 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 goto out_acl;
3034 }
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003035 p = xdr_reserve_space(xdr, 4);
3036 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003038 *p++ = cpu_to_be32(acl->naces);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08003040 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003041 p = xdr_reserve_space(xdr, 4*3);
3042 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003044 *p++ = cpu_to_be32(ace->type);
3045 *p++ = cpu_to_be32(ace->flag);
3046 *p++ = cpu_to_be32(ace->access_mask &
3047 NFS4_ACE_MASK_ALL);
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003048 status = nfsd4_encode_aclname(xdr, rqstp, ace);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 if (status)
3050 goto out;
3051 }
3052 }
3053out_acl:
3054 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003055 p = xdr_reserve_space(xdr, 4);
3056 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 goto out_resource;
Andreas Gruenbacher0c9d65e2015-04-24 13:04:00 +02003058 *p++ = cpu_to_be32(IS_POSIXACL(dentry->d_inode) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
3060 }
3061 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003062 p = xdr_reserve_space(xdr, 4);
3063 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003065 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 }
3067 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003068 p = xdr_reserve_space(xdr, 4);
3069 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003071 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 }
3073 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003074 p = xdr_reserve_space(xdr, 4);
3075 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003077 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 }
3079 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003080 p = xdr_reserve_space(xdr, 4);
3081 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003083 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 }
3085 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003086 p = xdr_reserve_space(xdr, fhp->fh_handle.fh_size + 4);
3087 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 goto out_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003089 p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base,
3090 fhp->fh_handle.fh_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 }
3092 if (bmval0 & FATTR4_WORD0_FILEID) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003093 p = xdr_reserve_space(xdr, 8);
3094 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003096 p = xdr_encode_hyper(p, stat.ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 }
3098 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003099 p = xdr_reserve_space(xdr, 8);
3100 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003102 p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 }
3104 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003105 p = xdr_reserve_space(xdr, 8);
3106 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003108 p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 }
3110 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
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, (u64) statfs.f_files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 }
J.Bruce Fields81c3f412006-10-04 02:16:19 -07003116 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003117 status = nfsd4_encode_fs_locations(xdr, rqstp, exp);
J.Bruce Fields81c3f412006-10-04 02:16:19 -07003118 if (status)
3119 goto out;
3120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003122 p = xdr_reserve_space(xdr, 4);
3123 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003125 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 }
3127 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003128 p = xdr_reserve_space(xdr, 8);
3129 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003131 p = xdr_encode_hyper(p, exp->ex_path.mnt->mnt_sb->s_maxbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 }
3133 if (bmval0 & FATTR4_WORD0_MAXLINK) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003134 p = xdr_reserve_space(xdr, 4);
3135 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003137 *p++ = cpu_to_be32(255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 }
3139 if (bmval0 & FATTR4_WORD0_MAXNAME) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003140 p = xdr_reserve_space(xdr, 4);
3141 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003143 *p++ = cpu_to_be32(statfs.f_namelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 }
3145 if (bmval0 & FATTR4_WORD0_MAXREAD) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003146 p = xdr_reserve_space(xdr, 8);
3147 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003149 p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 }
3151 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003152 p = xdr_reserve_space(xdr, 8);
3153 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003155 p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 }
3157 if (bmval1 & FATTR4_WORD1_MODE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003158 p = xdr_reserve_space(xdr, 4);
3159 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003161 *p++ = cpu_to_be32(stat.mode & S_IALLUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 }
3163 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003164 p = xdr_reserve_space(xdr, 4);
3165 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003167 *p++ = cpu_to_be32(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 }
3169 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003170 p = xdr_reserve_space(xdr, 4);
3171 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003173 *p++ = cpu_to_be32(stat.nlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 }
3175 if (bmval1 & FATTR4_WORD1_OWNER) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003176 status = nfsd4_encode_user(xdr, rqstp, stat.uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 if (status)
3178 goto out;
3179 }
3180 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003181 status = nfsd4_encode_group(xdr, rqstp, stat.gid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 if (status)
3183 goto out;
3184 }
3185 if (bmval1 & FATTR4_WORD1_RAWDEV) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003186 p = xdr_reserve_space(xdr, 8);
3187 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 goto out_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003189 *p++ = cpu_to_be32((u32) MAJOR(stat.rdev));
3190 *p++ = cpu_to_be32((u32) MINOR(stat.rdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 }
3192 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003193 p = xdr_reserve_space(xdr, 8);
3194 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 goto out_resource;
3196 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003197 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 }
3199 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003200 p = xdr_reserve_space(xdr, 8);
3201 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 goto out_resource;
3203 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003204 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 }
3206 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003207 p = xdr_reserve_space(xdr, 8);
3208 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 goto out_resource;
3210 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003211 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 }
3213 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003214 p = xdr_reserve_space(xdr, 8);
3215 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 goto out_resource;
3217 dummy64 = (u64)stat.blocks << 9;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003218 p = xdr_encode_hyper(p, dummy64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 }
3220 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003221 p = xdr_reserve_space(xdr, 12);
3222 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003224 p = xdr_encode_hyper(p, (s64)stat.atime.tv_sec);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003225 *p++ = cpu_to_be32(stat.atime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 }
3227 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003228 p = xdr_reserve_space(xdr, 12);
3229 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 goto out_resource;
J. Bruce Fields16945142018-04-25 14:34:11 -04003231 p = encode_time_delta(p, d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 }
3233 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003234 p = xdr_reserve_space(xdr, 12);
3235 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003237 p = xdr_encode_hyper(p, (s64)stat.ctime.tv_sec);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003238 *p++ = cpu_to_be32(stat.ctime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 }
3240 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003241 p = xdr_reserve_space(xdr, 12);
3242 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 goto out_resource;
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003244 p = xdr_encode_hyper(p, (s64)stat.mtime.tv_sec);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003245 *p++ = cpu_to_be32(stat.mtime.tv_nsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 }
3247 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
Kinglong Mee0a2050d2015-07-30 21:51:54 +08003248 struct kstat parent_stat;
3249 u64 ino = stat.ino;
3250
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04003251 p = xdr_reserve_space(xdr, 8);
3252 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 goto out_resource;
Frank Filz406a7ea2007-11-27 11:34:05 -08003254 /*
3255 * Get parent's attributes if not ignoring crossmount
3256 * and this is the root of a cross-mounted filesystem.
3257 */
3258 if (ignore_crossmnt == 0 &&
Kinglong Mee0a2050d2015-07-30 21:51:54 +08003259 dentry == exp->ex_path.mnt->mnt_root) {
3260 err = get_parent_attributes(exp, &parent_stat);
3261 if (err)
3262 goto out_nfserr;
3263 ino = parent_stat.ino;
3264 }
3265 p = xdr_encode_hyper(p, ino);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003267#ifdef CONFIG_NFSD_PNFS
Kinglong Mee6896f152015-07-30 21:52:44 +08003268 if (bmval1 & FATTR4_WORD1_FS_LAYOUT_TYPES) {
Jeff Layton8a4c3922016-07-10 15:55:58 -04003269 status = nfsd4_encode_layout_types(xdr, exp->ex_layout_types);
Kinglong Mee6896f152015-07-30 21:52:44 +08003270 if (status)
3271 goto out;
3272 }
3273
3274 if (bmval2 & FATTR4_WORD2_LAYOUT_TYPES) {
Jeff Layton8a4c3922016-07-10 15:55:58 -04003275 status = nfsd4_encode_layout_types(xdr, exp->ex_layout_types);
Kinglong Mee6896f152015-07-30 21:52:44 +08003276 if (status)
3277 goto out;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02003278 }
3279
3280 if (bmval2 & FATTR4_WORD2_LAYOUT_BLKSIZE) {
3281 p = xdr_reserve_space(xdr, 4);
3282 if (!p)
3283 goto out_resource;
3284 *p++ = cpu_to_be32(stat.blksize);
3285 }
3286#endif /* CONFIG_NFSD_PNFS */
Benny Halevy8c18f202009-04-03 08:29:14 +03003287 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
Trond Myklebustb26b78c2017-05-09 16:24:59 -04003288 u32 supp[3];
3289
3290 memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
3291 supp[0] &= NFSD_SUPPATTR_EXCLCREAT_WORD0;
3292 supp[1] &= NFSD_SUPPATTR_EXCLCREAT_WORD1;
3293 supp[2] &= NFSD_SUPPATTR_EXCLCREAT_WORD2;
3294
3295 status = nfsd4_encode_bitmap(xdr, supp[0], supp[1], supp[2]);
Kinglong Mee75976de2015-07-30 21:55:30 +08003296 if (status)
3297 goto out;
Benny Halevy8c18f202009-04-03 08:29:14 +03003298 }
Andy Adamson7e705702009-04-03 08:29:11 +03003299
J. Bruce Fieldsa8585762018-04-25 13:26:23 -04003300 if (bmval2 & FATTR4_WORD2_CHANGE_ATTR_TYPE) {
3301 p = xdr_reserve_space(xdr, 4);
3302 if (!p)
3303 goto out_resource;
3304 if (IS_I_VERSION(d_inode(dentry)))
3305 *p++ = cpu_to_be32(NFS4_CHANGE_TYPE_IS_MONOTONIC_INCR);
3306 else
3307 *p++ = cpu_to_be32(NFS4_CHANGE_TYPE_IS_TIME_METADATA);
3308 }
3309
Arnd Bergmann0ab88ca2019-03-22 15:07:11 +01003310#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
Kinglong Mee7d580722015-07-30 21:54:26 +08003311 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
3312 status = nfsd4_encode_security_label(xdr, rqstp, context,
3313 contextlen);
3314 if (status)
3315 goto out;
3316 }
Arnd Bergmann0ab88ca2019-03-22 15:07:11 +01003317#endif
Kinglong Mee7d580722015-07-30 21:54:26 +08003318
Frank van der Linden0e885e82020-06-23 22:39:27 +00003319 if (bmval2 & FATTR4_WORD2_XATTR_SUPPORT) {
3320 p = xdr_reserve_space(xdr, 4);
3321 if (!p)
3322 goto out_resource;
3323 err = xattr_supported_namespace(d_inode(dentry),
3324 XATTR_USER_PREFIX);
3325 *p++ = cpu_to_be32(err == 0);
3326 }
3327
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04003328 attrlen = htonl(xdr->buf->len - attrlen_offset - 4);
3329 write_bytes_to_xdr_buf(xdr->buf, attrlen_offset, &attrlen, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 status = nfs_ok;
3331
3332out:
J. Bruce Fieldsba4e55b2013-05-15 10:27:52 -04003333#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
David Quigley18032ca2013-05-02 13:19:10 -04003334 if (context)
3335 security_release_secctx(context, contextlen);
J. Bruce Fieldsba4e55b2013-05-15 10:27:52 -04003336#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
J. Bruce Fields28e05dd2007-02-16 01:28:30 -08003337 kfree(acl);
Yan, Zheng18df11d02014-03-10 12:52:07 +08003338 if (tempfh) {
J. Bruce Fieldsd50e61362014-01-15 12:21:12 -05003339 fh_put(tempfh);
Yan, Zheng18df11d02014-03-10 12:52:07 +08003340 kfree(tempfh);
3341 }
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05003342 if (status)
3343 xdr_truncate_encode(xdr, starting_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 return status;
3345out_nfserr:
Al Virob8dd7b92006-10-19 23:29:01 -07003346 status = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 goto out;
3348out_resource:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 status = nfserr_resource;
3350 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351}
3352
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04003353static void svcxdr_init_encode_from_buffer(struct xdr_stream *xdr,
3354 struct xdr_buf *buf, __be32 *p, int bytes)
3355{
3356 xdr->scratch.iov_len = 0;
3357 memset(buf, 0, sizeof(struct xdr_buf));
3358 buf->head[0].iov_base = p;
3359 buf->head[0].iov_len = 0;
3360 buf->len = 0;
3361 xdr->buf = buf;
3362 xdr->iov = buf->head;
3363 xdr->p = p;
3364 xdr->end = (void *)p + bytes;
3365 buf->buflen = bytes;
3366}
3367
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04003368__be32 nfsd4_encode_fattr_to_buf(__be32 **p, int words,
3369 struct svc_fh *fhp, struct svc_export *exp,
3370 struct dentry *dentry, u32 *bmval,
3371 struct svc_rqst *rqstp, int ignore_crossmnt)
3372{
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04003373 struct xdr_buf dummy;
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04003374 struct xdr_stream xdr;
3375 __be32 ret;
3376
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04003377 svcxdr_init_encode_from_buffer(&xdr, &dummy, *p, words << 2);
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04003378 ret = nfsd4_encode_fattr(&xdr, fhp, exp, dentry, bmval, rqstp,
3379 ignore_crossmnt);
3380 *p = xdr.p;
3381 return ret;
3382}
3383
J. Bruce Fieldsc0ce6ec2008-02-11 15:48:47 -05003384static inline int attributes_need_mount(u32 *bmval)
3385{
3386 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
3387 return 1;
3388 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
3389 return 1;
3390 return 0;
3391}
3392
Al Virob37ad282006-10-19 23:28:59 -07003393static __be32
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003394nfsd4_encode_dirent_fattr(struct xdr_stream *xdr, struct nfsd4_readdir *cd,
3395 const char *name, int namlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396{
3397 struct svc_export *exp = cd->rd_fhp->fh_export;
3398 struct dentry *dentry;
Al Virob37ad282006-10-19 23:28:59 -07003399 __be32 nfserr;
Frank Filz406a7ea2007-11-27 11:34:05 -08003400 int ignore_crossmnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401
Al Viro6c2d47982019-10-31 01:21:58 -04003402 dentry = lookup_positive_unlocked(name, cd->rd_fhp->fh_dentry, namlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 if (IS_ERR(dentry))
3404 return nfserrno(PTR_ERR(dentry));
3405
3406 exp_get(exp);
Frank Filz406a7ea2007-11-27 11:34:05 -08003407 /*
3408 * In the case of a mountpoint, the client may be asking for
3409 * attributes that are only properties of the underlying filesystem
3410 * as opposed to the cross-mounted file system. In such a case,
3411 * we will not follow the cross mount and will fill the attribtutes
3412 * directly from the mountpoint dentry.
3413 */
J. Bruce Fields3227fa42009-10-25 21:43:01 -04003414 if (nfsd_mountpoint(dentry, exp)) {
J.Bruce Fields021d3a72006-12-13 00:35:24 -08003415 int err;
3416
J. Bruce Fields3227fa42009-10-25 21:43:01 -04003417 if (!(exp->ex_flags & NFSEXP_V4ROOT)
3418 && !attributes_need_mount(cd->rd_bmval)) {
3419 ignore_crossmnt = 1;
3420 goto out_encode;
3421 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07003422 /*
3423 * Why the heck aren't we just using nfsd_lookup??
3424 * Different "."/".." handling? Something else?
3425 * At least, add a comment here to explain....
3426 */
J.Bruce Fields021d3a72006-12-13 00:35:24 -08003427 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
3428 if (err) {
3429 nfserr = nfserrno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 goto out_put;
3431 }
Andy Adamsondcb488a32007-07-17 04:04:51 -07003432 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
3433 if (nfserr)
3434 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
3436 }
J. Bruce Fields3227fa42009-10-25 21:43:01 -04003437out_encode:
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003438 nfserr = nfsd4_encode_fattr(xdr, NULL, exp, dentry, cd->rd_bmval,
Frank Filz406a7ea2007-11-27 11:34:05 -08003439 cd->rd_rqstp, ignore_crossmnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440out_put:
3441 dput(dentry);
3442 exp_put(exp);
3443 return nfserr;
3444}
3445
Al Viro2ebbc012006-10-19 23:28:58 -07003446static __be32 *
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003447nfsd4_encode_rdattr_error(struct xdr_stream *xdr, __be32 nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448{
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003449 __be32 *p;
3450
Kinglong Meec3a45612014-07-06 11:34:43 +08003451 p = xdr_reserve_space(xdr, 20);
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003452 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 return NULL;
3454 *p++ = htonl(2);
3455 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
3456 *p++ = htonl(0); /* bmval1 */
3457
J. Bruce Fields87915c62013-01-16 17:33:28 -05003458 *p++ = htonl(4); /* attribute length */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 *p++ = nfserr; /* no htonl */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 return p;
3461}
3462
3463static int
NeilBrowna0ad13e2007-01-26 00:57:10 -08003464nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
3465 loff_t offset, u64 ino, unsigned int d_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466{
NeilBrowna0ad13e2007-01-26 00:57:10 -08003467 struct readdir_cd *ccd = ccdv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003469 struct xdr_stream *xdr = cd->xdr;
3470 int start_offset = xdr->buf->len;
3471 int cookie_offset;
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04003472 u32 name_and_cookie;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003473 int entry_bytes;
Al Virob37ad282006-10-19 23:28:59 -07003474 __be32 nfserr = nfserr_toosmall;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003475 __be64 wire_offset;
3476 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477
3478 /* In nfsv4, "." and ".." never make it onto the wire.. */
3479 if (name && isdotent(name, namlen)) {
3480 cd->common.err = nfs_ok;
3481 return 0;
3482 }
3483
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003484 if (cd->cookie_offset) {
3485 wire_offset = cpu_to_be64(offset);
3486 write_bytes_to_xdr_buf(xdr->buf, cd->cookie_offset,
3487 &wire_offset, 8);
3488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003490 p = xdr_reserve_space(xdr, 4);
3491 if (!p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 *p++ = xdr_one; /* mark entry present */
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003494 cookie_offset = xdr->buf->len;
3495 p = xdr_reserve_space(xdr, 3*4 + namlen);
3496 if (!p)
3497 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
3499 p = xdr_encode_array(p, name, namlen); /* name length & name */
3500
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003501 nfserr = nfsd4_encode_dirent_fattr(xdr, cd, name, namlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 switch (nfserr) {
3503 case nfs_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 break;
3505 case nfserr_resource:
3506 nfserr = nfserr_toosmall;
3507 goto fail;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04003508 case nfserr_noent:
Kinglong Meef41c5ad2014-06-13 17:32:42 +08003509 xdr_truncate_encode(xdr, start_offset);
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04003510 goto skip_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 default:
3512 /*
3513 * If the client requested the RDATTR_ERROR attribute,
3514 * we stuff the error code into this attribute
3515 * and continue. If this attribute was not requested,
3516 * then in accordance with the spec, we fail the
3517 * entire READDIR operation(!)
3518 */
3519 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
3520 goto fail;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003521 p = nfsd4_encode_rdattr_error(xdr, nfserr);
Fred Isaman34081ef2006-01-18 17:43:40 -08003522 if (p == NULL) {
3523 nfserr = nfserr_toosmall;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 goto fail;
Fred Isaman34081ef2006-01-18 17:43:40 -08003525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 }
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003527 nfserr = nfserr_toosmall;
3528 entry_bytes = xdr->buf->len - start_offset;
3529 if (entry_bytes > cd->rd_maxcount)
3530 goto fail;
3531 cd->rd_maxcount -= entry_bytes;
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04003532 /*
3533 * RFC 3530 14.2.24 describes rd_dircount as only a "hint", so
3534 * let's always let through the first entry, at least:
3535 */
J. Bruce Fields0ec016e2014-12-19 18:01:35 -05003536 if (!cd->rd_dircount)
3537 goto fail;
3538 name_and_cookie = 4 + 4 * XDR_QUADLEN(namlen) + 8;
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04003539 if (name_and_cookie > cd->rd_dircount && cd->cookie_offset)
3540 goto fail;
3541 cd->rd_dircount -= min(cd->rd_dircount, name_and_cookie);
J. Bruce Fields0ec016e2014-12-19 18:01:35 -05003542
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003543 cd->cookie_offset = cookie_offset;
J. Bruce Fieldsb2c0cea2009-05-05 19:04:29 -04003544skip_entry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 cd->common.err = nfs_ok;
3546 return 0;
3547fail:
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05003548 xdr_truncate_encode(xdr, start_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 cd->common.err = nfserr;
3550 return -EINVAL;
3551}
3552
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003553static __be32
3554nfsd4_encode_stateid(struct xdr_stream *xdr, stateid_t *sid)
Benny Halevye2f282b2008-08-12 20:45:07 +03003555{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003556 __be32 *p;
Benny Halevye2f282b2008-08-12 20:45:07 +03003557
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003558 p = xdr_reserve_space(xdr, sizeof(stateid_t));
3559 if (!p)
3560 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003561 *p++ = cpu_to_be32(sid->si_generation);
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003562 p = xdr_encode_opaque_fixed(p, &sid->si_opaque,
3563 sizeof(stateid_opaque_t));
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003564 return 0;
Benny Halevye2f282b2008-08-12 20:45:07 +03003565}
3566
Benny Halevy695e12f2008-07-04 14:38:33 +03003567static __be32
Al Virob37ad282006-10-19 23:28:59 -07003568nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003570 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003571 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003573 p = xdr_reserve_space(xdr, 8);
3574 if (!p)
3575 return nfserr_resource;
3576 *p++ = cpu_to_be32(access->ac_supported);
3577 *p++ = cpu_to_be32(access->ac_resp_access);
3578 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579}
3580
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003581static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
3582{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003583 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003584 __be32 *p;
3585
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003586 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 8);
3587 if (!p)
3588 return nfserr_resource;
3589 p = xdr_encode_opaque_fixed(p, bcts->sessionid.data,
3590 NFS4_MAX_SESSIONID_LEN);
3591 *p++ = cpu_to_be32(bcts->dir);
3592 /* Upshifting from TCP to RDMA is not supported */
3593 *p++ = cpu_to_be32(0);
3594 return 0;
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04003595}
3596
Benny Halevy695e12f2008-07-04 14:38:33 +03003597static __be32
Al Virob37ad282006-10-19 23:28:59 -07003598nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003600 struct xdr_stream *xdr = &resp->xdr;
3601
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003602 return nfsd4_encode_stateid(xdr, &close->cl_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603}
3604
3605
Benny Halevy695e12f2008-07-04 14:38:33 +03003606static __be32
Al Virob37ad282006-10-19 23:28:59 -07003607nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003609 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003610 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003612 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
3613 if (!p)
3614 return nfserr_resource;
3615 p = xdr_encode_opaque_fixed(p, commit->co_verf.data,
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003616 NFS4_VERIFIER_SIZE);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003617 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618}
3619
Benny Halevy695e12f2008-07-04 14:38:33 +03003620static __be32
Al Virob37ad282006-10-19 23:28:59 -07003621nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003623 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003624 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003626 p = xdr_reserve_space(xdr, 20);
3627 if (!p)
3628 return nfserr_resource;
3629 encode_cinfo(p, &create->cr_cinfo);
Trond Myklebustb96811c2019-08-18 14:18:58 -04003630 return nfsd4_encode_bitmap(xdr, create->cr_bmval[0],
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003631 create->cr_bmval[1], create->cr_bmval[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632}
3633
Al Virob37ad282006-10-19 23:28:59 -07003634static __be32
3635nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636{
3637 struct svc_fh *fhp = getattr->ga_fhp;
J. Bruce Fieldsd5184652013-08-26 16:04:46 -04003638 struct xdr_stream *xdr = &resp->xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003640 return nfsd4_encode_fattr(xdr, fhp, fhp->fh_export, fhp->fh_dentry,
3641 getattr->ga_bmval, resp->rqstp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642}
3643
Benny Halevy695e12f2008-07-04 14:38:33 +03003644static __be32
3645nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003647 struct xdr_stream *xdr = &resp->xdr;
Benny Halevy695e12f2008-07-04 14:38:33 +03003648 struct svc_fh *fhp = *fhpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 unsigned int len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003650 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003652 len = fhp->fh_handle.fh_size;
3653 p = xdr_reserve_space(xdr, len + 4);
3654 if (!p)
3655 return nfserr_resource;
3656 p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base, len);
3657 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658}
3659
3660/*
3661* Including all fields other than the name, a LOCK4denied structure requires
3662* 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
3663*/
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003664static __be32
3665nfsd4_encode_lock_denied(struct xdr_stream *xdr, struct nfsd4_lock_denied *ld)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666{
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003667 struct xdr_netobj *conf = &ld->ld_owner;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003668 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003670again:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003671 p = xdr_reserve_space(xdr, 32 + XDR_LEN(conf->len));
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003672 if (!p) {
3673 /*
3674 * Don't fail to return the result just because we can't
3675 * return the conflicting open:
3676 */
3677 if (conf->len) {
Kinglong Meef98bac52014-07-07 22:10:56 +08003678 kfree(conf->data);
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003679 conf->len = 0;
3680 conf->data = NULL;
3681 goto again;
3682 }
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003683 return nfserr_resource;
J. Bruce Fields8c7424c2014-03-10 12:19:10 -04003684 }
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003685 p = xdr_encode_hyper(p, ld->ld_start);
3686 p = xdr_encode_hyper(p, ld->ld_length);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003687 *p++ = cpu_to_be32(ld->ld_type);
J. Bruce Fields7c13f342011-08-30 22:15:47 -04003688 if (conf->len) {
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04003689 p = xdr_encode_opaque_fixed(p, &ld->ld_clientid, 8);
3690 p = xdr_encode_opaque(p, conf->data, conf->len);
Kinglong Meef98bac52014-07-07 22:10:56 +08003691 kfree(conf->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04003693 p = xdr_encode_hyper(p, (u64)0); /* clientid */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003694 *p++ = cpu_to_be32(0); /* length of owner name */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 }
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003696 return nfserr_denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697}
3698
Benny Halevy695e12f2008-07-04 14:38:33 +03003699static __be32
Al Virob37ad282006-10-19 23:28:59 -07003700nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003702 struct xdr_stream *xdr = &resp->xdr;
3703
Benny Halevye2f282b2008-08-12 20:45:07 +03003704 if (!nfserr)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003705 nfserr = nfsd4_encode_stateid(xdr, &lock->lk_resp_stateid);
Benny Halevye2f282b2008-08-12 20:45:07 +03003706 else if (nfserr == nfserr_denied)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003707 nfserr = nfsd4_encode_lock_denied(xdr, &lock->lk_denied);
Kinglong Meef98bac52014-07-07 22:10:56 +08003708
Benny Halevy695e12f2008-07-04 14:38:33 +03003709 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710}
3711
Benny Halevy695e12f2008-07-04 14:38:33 +03003712static __be32
Al Virob37ad282006-10-19 23:28:59 -07003713nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003715 struct xdr_stream *xdr = &resp->xdr;
3716
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 if (nfserr == nfserr_denied)
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003718 nfsd4_encode_lock_denied(xdr, &lockt->lt_denied);
Benny Halevy695e12f2008-07-04 14:38:33 +03003719 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720}
3721
Benny Halevy695e12f2008-07-04 14:38:33 +03003722static __be32
Al Virob37ad282006-10-19 23:28:59 -07003723nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003725 struct xdr_stream *xdr = &resp->xdr;
3726
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003727 return nfsd4_encode_stateid(xdr, &locku->lu_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728}
3729
3730
Benny Halevy695e12f2008-07-04 14:38:33 +03003731static __be32
Al Virob37ad282006-10-19 23:28:59 -07003732nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003734 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003735 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003737 p = xdr_reserve_space(xdr, 20);
3738 if (!p)
3739 return nfserr_resource;
3740 p = encode_cinfo(p, &link->li_cinfo);
3741 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742}
3743
3744
Benny Halevy695e12f2008-07-04 14:38:33 +03003745static __be32
Al Virob37ad282006-10-19 23:28:59 -07003746nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003748 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07003749 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003751 nfserr = nfsd4_encode_stateid(xdr, &open->op_stateid);
3752 if (nfserr)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003753 return nfserr;
Kinglong Mee75976de2015-07-30 21:55:30 +08003754 p = xdr_reserve_space(xdr, 24);
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003755 if (!p)
3756 return nfserr_resource;
J. Bruce Fieldsd05d5742014-03-22 18:48:39 -04003757 p = encode_cinfo(p, &open->op_cinfo);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003758 *p++ = cpu_to_be32(open->op_rflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759
Kinglong Mee75976de2015-07-30 21:55:30 +08003760 nfserr = nfsd4_encode_bitmap(xdr, open->op_bmval[0], open->op_bmval[1],
3761 open->op_bmval[2]);
3762 if (nfserr)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003763 return nfserr;
Kinglong Mee75976de2015-07-30 21:55:30 +08003764
3765 p = xdr_reserve_space(xdr, 4);
3766 if (!p)
3767 return nfserr_resource;
3768
3769 *p++ = cpu_to_be32(open->op_delegate_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 switch (open->op_delegate_type) {
3771 case NFS4_OPEN_DELEGATE_NONE:
3772 break;
3773 case NFS4_OPEN_DELEGATE_READ:
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, 20);
3778 if (!p)
3779 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003780 *p++ = cpu_to_be32(open->op_recall);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
3782 /*
3783 * TODO: ACE's in delegations
3784 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003785 *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
3786 *p++ = cpu_to_be32(0);
3787 *p++ = cpu_to_be32(0);
3788 *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 break;
3790 case NFS4_OPEN_DELEGATE_WRITE:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003791 nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
3792 if (nfserr)
3793 return nfserr;
3794 p = xdr_reserve_space(xdr, 32);
3795 if (!p)
3796 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003797 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
3799 /*
3800 * TODO: space_limit's in delegations
3801 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003802 *p++ = cpu_to_be32(NFS4_LIMIT_SIZE);
3803 *p++ = cpu_to_be32(~(u32)0);
3804 *p++ = cpu_to_be32(~(u32)0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805
3806 /*
3807 * TODO: ACE's in delegations
3808 */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003809 *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
3810 *p++ = cpu_to_be32(0);
3811 *p++ = cpu_to_be32(0);
3812 *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 break;
Benny Halevyd24433c2012-02-16 20:57:17 +02003814 case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
3815 switch (open->op_why_no_deleg) {
3816 case WND4_CONTENTION:
3817 case WND4_RESOURCE:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003818 p = xdr_reserve_space(xdr, 8);
3819 if (!p)
3820 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003821 *p++ = cpu_to_be32(open->op_why_no_deleg);
3822 /* deleg signaling not supported yet: */
3823 *p++ = cpu_to_be32(0);
Benny Halevyd24433c2012-02-16 20:57:17 +02003824 break;
3825 default:
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003826 p = xdr_reserve_space(xdr, 4);
3827 if (!p)
3828 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04003829 *p++ = cpu_to_be32(open->op_why_no_deleg);
Benny Halevyd24433c2012-02-16 20:57:17 +02003830 }
Benny Halevyd24433c2012-02-16 20:57:17 +02003831 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 default:
3833 BUG();
3834 }
3835 /* XXX save filehandle here */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003836 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837}
3838
Benny Halevy695e12f2008-07-04 14:38:33 +03003839static __be32
Al Virob37ad282006-10-19 23:28:59 -07003840nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003842 struct xdr_stream *xdr = &resp->xdr;
3843
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003844 return nfsd4_encode_stateid(xdr, &oc->oc_resp_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845}
3846
Benny Halevy695e12f2008-07-04 14:38:33 +03003847static __be32
Al Virob37ad282006-10-19 23:28:59 -07003848nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05003850 struct xdr_stream *xdr = &resp->xdr;
3851
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003852 return nfsd4_encode_stateid(xdr, &od->od_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853}
3854
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003855static __be32 nfsd4_encode_splice_read(
3856 struct nfsd4_compoundres *resp,
3857 struct nfsd4_read *read,
3858 struct file *file, unsigned long maxcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859{
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003860 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003861 struct xdr_buf *buf = xdr->buf;
Chuck Lever76e54922020-11-05 10:24:19 -05003862 int status, space_left;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003863 u32 eof;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003864 __be32 nfserr;
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003865 __be32 *p = xdr->p - 2;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003866
Kinglong Meed5d5c302014-07-09 21:51:27 +08003867 /* Make sure there will be room for padding if needed */
3868 if (xdr->end - xdr->p < 1)
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003869 return nfserr_resource;
3870
Chuck Lever87c59422018-03-28 13:29:11 -04003871 nfserr = nfsd_splice_read(read->rd_rqstp, read->rd_fhp,
Trond Myklebust83a63072019-08-26 13:03:11 -04003872 file, read->rd_offset, &maxcount, &eof);
Chuck Lever87c59422018-03-28 13:29:11 -04003873 read->rd_length = maxcount;
Chuck Lever76e54922020-11-05 10:24:19 -05003874 if (nfserr)
3875 goto out_err;
3876 status = svc_encode_result_payload(read->rd_rqstp,
3877 buf->head[0].iov_len, maxcount);
3878 if (status) {
3879 nfserr = nfserrno(status);
3880 goto out_err;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003881 }
3882
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003883 *(p++) = htonl(eof);
3884 *(p++) = htonl(maxcount);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003885
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003886 buf->page_len = maxcount;
3887 buf->len += maxcount;
J. Bruce Fields15b23ef2014-09-24 16:32:34 -04003888 xdr->page_ptr += (buf->page_base + maxcount + PAGE_SIZE - 1)
3889 / PAGE_SIZE;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003890
3891 /* Use rest of head for padding and remaining ops: */
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003892 buf->tail[0].iov_base = xdr->p;
3893 buf->tail[0].iov_len = 0;
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003894 xdr->iov = buf->tail;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003895 if (maxcount&3) {
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003896 int pad = 4 - (maxcount&3);
3897
3898 *(xdr->p++) = 0;
3899
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003900 buf->tail[0].iov_base += maxcount&3;
J. Bruce Fieldsfec25fa2014-05-13 17:27:03 -04003901 buf->tail[0].iov_len = pad;
3902 buf->len += pad;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003903 }
3904
3905 space_left = min_t(int, (void *)xdr->end - (void *)xdr->p,
J. Bruce Fields34a78b482014-05-13 16:32:04 -04003906 buf->buflen - buf->len);
3907 buf->buflen = buf->len + space_left;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003908 xdr->end = (__be32 *)((void *)xdr->end + space_left);
3909
3910 return 0;
Chuck Lever76e54922020-11-05 10:24:19 -05003911
3912out_err:
3913 /*
3914 * nfsd_splice_actor may have already messed with the
3915 * page length; reset it so as not to confuse
3916 * xdr_truncate_encode in our caller.
3917 */
3918 buf->page_len = 0;
3919 return nfserr;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003920}
3921
3922static __be32 nfsd4_encode_readv(struct nfsd4_compoundres *resp,
3923 struct nfsd4_read *read,
3924 struct file *file, unsigned long maxcount)
3925{
3926 struct xdr_stream *xdr = &resp->xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 u32 eof;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003928 int starting_len = xdr->buf->len - 8;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003929 __be32 nfserr;
3930 __be32 tmp;
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003931 int pad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932
Anna Schumaker403217f2020-08-17 12:53:06 -04003933 read->rd_vlen = xdr_reserve_space_vec(xdr, resp->rqstp->rq_vec, maxcount);
3934 if (read->rd_vlen < 0)
3935 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936
Chuck Lever87c59422018-03-28 13:29:11 -04003937 nfserr = nfsd_readv(resp->rqstp, read->rd_fhp, file, read->rd_offset,
Trond Myklebust83a63072019-08-26 13:03:11 -04003938 resp->rqstp->rq_vec, read->rd_vlen, &maxcount,
3939 &eof);
Chuck Lever87c59422018-03-28 13:29:11 -04003940 read->rd_length = maxcount;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003941 if (nfserr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 return nfserr;
Chuck Lever03493bc2020-06-10 10:36:42 -04003943 if (svc_encode_result_payload(resp->rqstp, starting_len + 8, maxcount))
Chuck Lever41205532020-03-02 14:45:53 -05003944 return nfserr_io;
Chuck Lever7dcf4ab2020-03-02 15:00:04 -05003945 xdr_truncate_encode(xdr, starting_len + 8 + xdr_align_size(maxcount));
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003946
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003947 tmp = htonl(eof);
3948 write_bytes_to_xdr_buf(xdr->buf, starting_len , &tmp, 4);
3949 tmp = htonl(maxcount);
3950 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp, 4);
3951
Chuck Lever7dcf4ab2020-03-02 15:00:04 -05003952 tmp = xdr_zero;
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003953 pad = (maxcount&3) ? 4 - (maxcount&3) : 0;
3954 write_bytes_to_xdr_buf(xdr->buf, starting_len + 8 + maxcount,
Chuck Lever7dcf4ab2020-03-02 15:00:04 -05003955 &tmp, pad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 return 0;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003957
3958}
3959
3960static __be32
3961nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
3962 struct nfsd4_read *read)
3963{
3964 unsigned long maxcount;
3965 struct xdr_stream *xdr = &resp->xdr;
Jeff Layton5c4583b2019-08-18 14:18:54 -04003966 struct file *file;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003967 int starting_len = xdr->buf->len;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003968 __be32 *p;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003969
Jeff Layton5c4583b2019-08-18 14:18:54 -04003970 if (nfserr)
3971 return nfserr;
3972 file = read->rd_nf->nf_file;
3973
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003974 p = xdr_reserve_space(xdr, 8); /* eof flag and byte count */
3975 if (!p) {
Jeff Layton779fb0f2014-11-19 07:51:18 -05003976 WARN_ON_ONCE(test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags));
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003977 return nfserr_resource;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003978 }
Christoph Hellwig68e8bb02015-06-18 16:45:02 +02003979 if (resp->xdr.buf->page_len &&
3980 test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags)) {
J. Bruce Fieldsb0420982014-03-18 17:44:10 -04003981 WARN_ON_ONCE(1);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04003982 return nfserr_resource;
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003983 }
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003984 xdr_commit_encode(xdr);
3985
3986 maxcount = svc_max_payload(resp->rqstp);
Christoph Hellwig68e8bb02015-06-18 16:45:02 +02003987 maxcount = min_t(unsigned long, maxcount,
3988 (xdr->buf->buflen - xdr->buf->len));
Kinglong Mee3c7aa152014-06-10 18:08:19 +08003989 maxcount = min_t(unsigned long, maxcount, read->rd_length);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003990
Christoph Hellwig68e8bb02015-06-18 16:45:02 +02003991 if (file->f_op->splice_read &&
3992 test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags))
Christoph Hellwig96bcad52015-06-18 16:45:01 +02003993 nfserr = nfsd4_encode_splice_read(resp, read, file, maxcount);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003994 else
Christoph Hellwig96bcad52015-06-18 16:45:01 +02003995 nfserr = nfsd4_encode_readv(resp, read, file, maxcount);
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003996
Christoph Hellwig96bcad52015-06-18 16:45:01 +02003997 if (nfserr)
J. Bruce Fieldsdc976182014-03-18 17:01:51 -04003998 xdr_truncate_encode(xdr, starting_len);
Christoph Hellwig96bcad52015-06-18 16:45:01 +02003999
Christoph Hellwig96bcad52015-06-18 16:45:01 +02004000 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001}
4002
Al Virob37ad282006-10-19 23:28:59 -07004003static __be32
4004nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005{
4006 int maxcount;
J. Bruce Fields476a7b12014-01-20 17:08:27 -05004007 __be32 wire_count;
4008 int zero = 0;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04004009 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05004010 int length_offset = xdr->buf->len;
Chuck Lever76e54922020-11-05 10:24:19 -05004011 int status;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004012 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04004014 p = xdr_reserve_space(xdr, 4);
4015 if (!p)
4016 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 maxcount = PAGE_SIZE;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004018
J. Bruce Fields476a7b12014-01-20 17:08:27 -05004019 p = xdr_reserve_space(xdr, maxcount);
4020 if (!p)
J. Bruce Fields4e21ac42014-03-22 15:15:11 -04004021 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 /*
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004023 * XXX: By default, vfs_readlink() will truncate symlinks if they
4024 * would overflow the buffer. Is this kosher in NFSv4? If not, one
4025 * easy fix is: if vfs_readlink() precisely fills the buffer, assume
4026 * that truncation occurred, and return NFS4ERR_RESOURCE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 */
J. Bruce Fields476a7b12014-01-20 17:08:27 -05004028 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp,
4029 (char *)p, &maxcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 if (nfserr == nfserr_isdir)
J. Bruce Fieldsd3f627c2014-02-26 17:00:38 -05004031 nfserr = nfserr_inval;
Chuck Lever76e54922020-11-05 10:24:19 -05004032 if (nfserr)
4033 goto out_err;
4034 status = svc_encode_result_payload(readlink->rl_rqstp, length_offset,
4035 maxcount);
4036 if (status) {
4037 nfserr = nfserrno(status);
4038 goto out_err;
J. Bruce Fieldsd3f627c2014-02-26 17:00:38 -05004039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040
J. Bruce Fields476a7b12014-01-20 17:08:27 -05004041 wire_count = htonl(maxcount);
4042 write_bytes_to_xdr_buf(xdr->buf, length_offset, &wire_count, 4);
Avi Kivity69bbd9c2014-06-26 17:23:12 +03004043 xdr_truncate_encode(xdr, length_offset + 4 + ALIGN(maxcount, 4));
J. Bruce Fields476a7b12014-01-20 17:08:27 -05004044 if (maxcount & 3)
4045 write_bytes_to_xdr_buf(xdr->buf, length_offset + 4 + maxcount,
4046 &zero, 4 - (maxcount&3));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 return 0;
Chuck Lever76e54922020-11-05 10:24:19 -05004048
4049out_err:
4050 xdr_truncate_encode(xdr, length_offset);
4051 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052}
4053
Al Virob37ad282006-10-19 23:28:59 -07004054static __be32
4055nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056{
4057 int maxcount;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004058 int bytes_left;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 loff_t offset;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004060 __be64 wire_offset;
J. Bruce Fieldsddd1ea52013-08-27 21:32:25 -04004061 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05004062 int starting_len = xdr->buf->len;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004063 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004065 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
4066 if (!p)
4067 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068
4069 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004070 *p++ = cpu_to_be32(0);
4071 *p++ = cpu_to_be32(0);
J. Bruce Fields4aea24b2014-01-15 15:17:58 -05004072 resp->xdr.buf->head[0].iov_len = ((char *)resp->xdr.p)
4073 - (char *)resp->xdr.buf->head[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
4075 /*
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004076 * Number of bytes left for directory entries allowing for the
4077 * final 8 bytes of the readdir and a following failed op:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 */
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004079 bytes_left = xdr->buf->buflen - xdr->buf->len
4080 - COMPOUND_ERR_SLACK_SPACE - 8;
4081 if (bytes_left < 0) {
4082 nfserr = nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 goto err_no_verf;
4084 }
Scott Mayhew9c2ece62018-05-07 09:01:08 -04004085 maxcount = svc_max_payload(resp->rqstp);
4086 maxcount = min_t(u32, readdir->rd_maxcount, maxcount);
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004087 /*
4088 * Note the rfc defines rd_maxcount as the size of the
4089 * READDIR4resok structure, which includes the verifier above
4090 * and the 8 bytes encoded at the end of this function:
4091 */
4092 if (maxcount < 16) {
4093 nfserr = nfserr_toosmall;
4094 goto err_no_verf;
4095 }
4096 maxcount = min_t(int, maxcount-16, bytes_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04004098 /* RFC 3530 14.2.24 allows us to ignore dircount when it's 0: */
4099 if (!readdir->rd_dircount)
Scott Mayhew9c2ece62018-05-07 09:01:08 -04004100 readdir->rd_dircount = svc_max_payload(resp->rqstp);
J. Bruce Fieldsaee37762014-08-20 14:49:50 -04004101
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004102 readdir->xdr = xdr;
4103 readdir->rd_maxcount = maxcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 readdir->common.err = 0;
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004105 readdir->cookie_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106
4107 offset = readdir->rd_cookie;
4108 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
4109 &offset,
4110 &readdir->common, nfsd4_encode_dirent);
4111 if (nfserr == nfs_ok &&
4112 readdir->common.err == nfserr_toosmall &&
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004113 xdr->buf->len == starting_len + 8) {
4114 /* nothing encoded; which limit did we hit?: */
4115 if (maxcount - 16 < bytes_left)
4116 /* It was the fault of rd_maxcount: */
4117 nfserr = nfserr_toosmall;
4118 else
4119 /* We ran out of buffer space: */
4120 nfserr = nfserr_resource;
4121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 if (nfserr)
4123 goto err_no_verf;
4124
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004125 if (readdir->cookie_offset) {
4126 wire_offset = cpu_to_be64(offset);
4127 write_bytes_to_xdr_buf(xdr->buf, readdir->cookie_offset,
4128 &wire_offset, 8);
4129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130
J. Bruce Fields561f0ed2014-01-20 16:37:11 -05004131 p = xdr_reserve_space(xdr, 8);
4132 if (!p) {
4133 WARN_ON_ONCE(1);
4134 goto err_no_verf;
4135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 *p++ = 0; /* no more entries */
4137 *p++ = htonl(readdir->common.err == nfserr_eof);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138
4139 return 0;
4140err_no_verf:
J. Bruce Fields1fcea5b2014-02-26 20:17:02 -05004141 xdr_truncate_encode(xdr, starting_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 return nfserr;
4143}
4144
Benny Halevy695e12f2008-07-04 14:38:33 +03004145static __be32
Al Virob37ad282006-10-19 23:28:59 -07004146nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004148 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004149 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004151 p = xdr_reserve_space(xdr, 20);
4152 if (!p)
4153 return nfserr_resource;
4154 p = encode_cinfo(p, &remove->rm_cinfo);
4155 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156}
4157
Benny Halevy695e12f2008-07-04 14:38:33 +03004158static __be32
Al Virob37ad282006-10-19 23:28:59 -07004159nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004161 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004162 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004164 p = xdr_reserve_space(xdr, 40);
4165 if (!p)
4166 return nfserr_resource;
4167 p = encode_cinfo(p, &rename->rn_sinfo);
4168 p = encode_cinfo(p, &rename->rn_tinfo);
4169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170}
4171
Benny Halevy695e12f2008-07-04 14:38:33 +03004172static __be32
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004173nfsd4_do_encode_secinfo(struct xdr_stream *xdr, struct svc_export *exp)
Andy Adamsondcb488a32007-07-17 04:04:51 -07004174{
Chuck Lever676e4eb2013-04-30 18:48:54 -04004175 u32 i, nflavs, supported;
J. Bruce Fields4796f452007-07-17 04:04:51 -07004176 struct exp_flavor_info *flavs;
4177 struct exp_flavor_info def_flavs[2];
Chuck Lever676e4eb2013-04-30 18:48:54 -04004178 __be32 *p, *flavorsp;
4179 static bool report = true;
Andy Adamsondcb488a32007-07-17 04:04:51 -07004180
J. Bruce Fields4796f452007-07-17 04:04:51 -07004181 if (exp->ex_nflavors) {
4182 flavs = exp->ex_flavors;
4183 nflavs = exp->ex_nflavors;
4184 } else { /* Handling of some defaults in absence of real secinfo: */
4185 flavs = def_flavs;
4186 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
4187 nflavs = 2;
4188 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
4189 flavs[1].pseudoflavor = RPC_AUTH_NULL;
4190 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
4191 nflavs = 1;
4192 flavs[0].pseudoflavor
4193 = svcauth_gss_flavor(exp->ex_client);
4194 } else {
4195 nflavs = 1;
4196 flavs[0].pseudoflavor
4197 = exp->ex_client->flavour->flavour;
4198 }
4199 }
4200
Chuck Lever676e4eb2013-04-30 18:48:54 -04004201 supported = 0;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004202 p = xdr_reserve_space(xdr, 4);
4203 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004204 return nfserr_resource;
Chuck Lever676e4eb2013-04-30 18:48:54 -04004205 flavorsp = p++; /* to be backfilled later */
Chuck Lever676e4eb2013-04-30 18:48:54 -04004206
J. Bruce Fields4796f452007-07-17 04:04:51 -07004207 for (i = 0; i < nflavs; i++) {
Chuck Lever676e4eb2013-04-30 18:48:54 -04004208 rpc_authflavor_t pf = flavs[i].pseudoflavor;
Chuck Levera77c8062013-03-16 15:55:10 -04004209 struct rpcsec_gss_info info;
Andy Adamsondcb488a32007-07-17 04:04:51 -07004210
Chuck Lever676e4eb2013-04-30 18:48:54 -04004211 if (rpcauth_get_gssinfo(pf, &info) == 0) {
4212 supported++;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004213 p = xdr_reserve_space(xdr, 4 + 4 +
4214 XDR_LEN(info.oid.len) + 4 + 4);
4215 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004216 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004217 *p++ = cpu_to_be32(RPC_AUTH_GSS);
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004218 p = xdr_encode_opaque(p, info.oid.data, info.oid.len);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004219 *p++ = cpu_to_be32(info.qop);
4220 *p++ = cpu_to_be32(info.service);
Chuck Lever676e4eb2013-04-30 18:48:54 -04004221 } else if (pf < RPC_AUTH_MAXFLAVOR) {
4222 supported++;
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004223 p = xdr_reserve_space(xdr, 4);
4224 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004225 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004226 *p++ = cpu_to_be32(pf);
Chuck Lever676e4eb2013-04-30 18:48:54 -04004227 } else {
4228 if (report)
4229 pr_warn("NFS: SECINFO: security flavor %u "
4230 "is not supported\n", pf);
Andy Adamsondcb488a32007-07-17 04:04:51 -07004231 }
4232 }
Chuck Levera77c8062013-03-16 15:55:10 -04004233
Chuck Lever676e4eb2013-04-30 18:48:54 -04004234 if (nflavs != supported)
4235 report = false;
4236 *flavorsp = htonl(supported);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004237 return 0;
Andy Adamsondcb488a32007-07-17 04:04:51 -07004238}
4239
Mi Jinlong22b6dee2010-12-27 14:29:57 +08004240static __be32
4241nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
4242 struct nfsd4_secinfo *secinfo)
4243{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004244 struct xdr_stream *xdr = &resp->xdr;
4245
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004246 return nfsd4_do_encode_secinfo(xdr, secinfo->si_exp);
Mi Jinlong22b6dee2010-12-27 14:29:57 +08004247}
4248
4249static __be32
4250nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
4251 struct nfsd4_secinfo_no_name *secinfo)
4252{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004253 struct xdr_stream *xdr = &resp->xdr;
4254
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004255 return nfsd4_do_encode_secinfo(xdr, secinfo->sin_exp);
Mi Jinlong22b6dee2010-12-27 14:29:57 +08004256}
4257
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258/*
4259 * The SETATTR encode routine is special -- it always encodes a bitmap,
4260 * regardless of the error status.
4261 */
Benny Halevy695e12f2008-07-04 14:38:33 +03004262static __be32
Al Virob37ad282006-10-19 23:28:59 -07004263nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004265 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004266 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004268 p = xdr_reserve_space(xdr, 16);
4269 if (!p)
4270 return nfserr_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 if (nfserr) {
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004272 *p++ = cpu_to_be32(3);
4273 *p++ = cpu_to_be32(0);
4274 *p++ = cpu_to_be32(0);
4275 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 }
4277 else {
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004278 *p++ = cpu_to_be32(3);
4279 *p++ = cpu_to_be32(setattr->sa_bmval[0]);
4280 *p++ = cpu_to_be32(setattr->sa_bmval[1]);
4281 *p++ = cpu_to_be32(setattr->sa_bmval[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 }
Benny Halevy695e12f2008-07-04 14:38:33 +03004283 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284}
4285
Benny Halevy695e12f2008-07-04 14:38:33 +03004286static __be32
Al Virob37ad282006-10-19 23:28:59 -07004287nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004289 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004290 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291
4292 if (!nfserr) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004293 p = xdr_reserve_space(xdr, 8 + NFS4_VERIFIER_SIZE);
4294 if (!p)
4295 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004296 p = xdr_encode_opaque_fixed(p, &scd->se_clientid, 8);
4297 p = xdr_encode_opaque_fixed(p, &scd->se_confirm,
4298 NFS4_VERIFIER_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 }
4300 else if (nfserr == nfserr_clid_inuse) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004301 p = xdr_reserve_space(xdr, 8);
4302 if (!p)
4303 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004304 *p++ = cpu_to_be32(0);
4305 *p++ = cpu_to_be32(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 }
Benny Halevy695e12f2008-07-04 14:38:33 +03004307 return nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308}
4309
Benny Halevy695e12f2008-07-04 14:38:33 +03004310static __be32
Al Virob37ad282006-10-19 23:28:59 -07004311nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312{
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004316 p = xdr_reserve_space(xdr, 16);
4317 if (!p)
4318 return nfserr_resource;
4319 *p++ = cpu_to_be32(write->wr_bytes_written);
4320 *p++ = cpu_to_be32(write->wr_how_written);
4321 p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
4322 NFS4_VERIFIER_SIZE);
4323 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324}
4325
Benny Halevy695e12f2008-07-04 14:38:33 +03004326static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004327nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03004328 struct nfsd4_exchange_id *exid)
4329{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004330 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004331 __be32 *p;
Andy Adamson0733d212009-04-03 08:28:01 +03004332 char *major_id;
4333 char *server_scope;
4334 int major_id_sz;
4335 int server_scope_sz;
4336 uint64_t minor_id = 0;
Scott Mayhew7627d7d2020-02-19 15:52:15 -05004337 struct nfsd_net *nn = net_generic(SVC_NET(resp->rqstp), nfsd_net_id);
Andy Adamson0733d212009-04-03 08:28:01 +03004338
Scott Mayhew7627d7d2020-02-19 15:52:15 -05004339 major_id = nn->nfsd_name;
4340 major_id_sz = strlen(nn->nfsd_name);
4341 server_scope = nn->nfsd_name;
4342 server_scope_sz = strlen(nn->nfsd_name);
Andy Adamson0733d212009-04-03 08:28:01 +03004343
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004344 p = xdr_reserve_space(xdr,
Andy Adamson0733d212009-04-03 08:28:01 +03004345 8 /* eir_clientid */ +
4346 4 /* eir_sequenceid */ +
4347 4 /* eir_flags */ +
Kinglong Meea8bb84b2013-12-10 15:24:36 +08004348 4 /* spr_how */);
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004349 if (!p)
4350 return nfserr_resource;
Andy Adamson0733d212009-04-03 08:28:01 +03004351
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004352 p = xdr_encode_opaque_fixed(p, &exid->clientid, 8);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004353 *p++ = cpu_to_be32(exid->seqid);
4354 *p++ = cpu_to_be32(exid->flags);
Andy Adamson0733d212009-04-03 08:28:01 +03004355
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004356 *p++ = cpu_to_be32(exid->spa_how);
Kinglong Meea8bb84b2013-12-10 15:24:36 +08004357
J. Bruce Fields57266a62013-04-13 14:27:29 -04004358 switch (exid->spa_how) {
4359 case SP4_NONE:
4360 break;
4361 case SP4_MACH_CRED:
4362 /* spo_must_enforce bitmap: */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004363 nfserr = nfsd4_encode_bitmap(xdr,
Andrew Elbleed941642016-06-15 12:52:09 -04004364 exid->spo_must_enforce[0],
4365 exid->spo_must_enforce[1],
4366 exid->spo_must_enforce[2]);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004367 if (nfserr)
4368 return nfserr;
Andrew Elbleed941642016-06-15 12:52:09 -04004369 /* spo_must_allow bitmap: */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004370 nfserr = nfsd4_encode_bitmap(xdr,
Andrew Elbleed941642016-06-15 12:52:09 -04004371 exid->spo_must_allow[0],
4372 exid->spo_must_allow[1],
4373 exid->spo_must_allow[2]);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004374 if (nfserr)
4375 return nfserr;
J. Bruce Fields57266a62013-04-13 14:27:29 -04004376 break;
4377 default:
4378 WARN_ON_ONCE(1);
4379 }
Andy Adamson0733d212009-04-03 08:28:01 +03004380
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004381 p = xdr_reserve_space(xdr,
Kinglong Meea8bb84b2013-12-10 15:24:36 +08004382 8 /* so_minor_id */ +
4383 4 /* so_major_id.len */ +
4384 (XDR_QUADLEN(major_id_sz) * 4) +
4385 4 /* eir_server_scope.len */ +
4386 (XDR_QUADLEN(server_scope_sz) * 4) +
4387 4 /* eir_server_impl_id.count (0) */);
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004388 if (!p)
4389 return nfserr_resource;
Kinglong Meea8bb84b2013-12-10 15:24:36 +08004390
Andy Adamson0733d212009-04-03 08:28:01 +03004391 /* The server_owner struct */
J. Bruce Fieldsb64c7f32014-03-22 17:11:35 -04004392 p = xdr_encode_hyper(p, minor_id); /* Minor id */
Andy Adamson0733d212009-04-03 08:28:01 +03004393 /* major id */
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004394 p = xdr_encode_opaque(p, major_id, major_id_sz);
Andy Adamson0733d212009-04-03 08:28:01 +03004395
4396 /* Server scope */
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004397 p = xdr_encode_opaque(p, server_scope, server_scope_sz);
Andy Adamson0733d212009-04-03 08:28:01 +03004398
4399 /* Implementation id */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004400 *p++ = cpu_to_be32(0); /* zero length nfs_impl_id4 array */
Andy Adamson0733d212009-04-03 08:28:01 +03004401 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03004402}
4403
4404static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004405nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03004406 struct nfsd4_create_session *sess)
4407{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004408 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004409 __be32 *p;
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004410
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004411 p = xdr_reserve_space(xdr, 24);
4412 if (!p)
4413 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004414 p = xdr_encode_opaque_fixed(p, sess->sessionid.data,
4415 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004416 *p++ = cpu_to_be32(sess->seqid);
4417 *p++ = cpu_to_be32(sess->flags);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004418
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004419 p = xdr_reserve_space(xdr, 28);
4420 if (!p)
4421 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004422 *p++ = cpu_to_be32(0); /* headerpadsz */
4423 *p++ = cpu_to_be32(sess->fore_channel.maxreq_sz);
4424 *p++ = cpu_to_be32(sess->fore_channel.maxresp_sz);
4425 *p++ = cpu_to_be32(sess->fore_channel.maxresp_cached);
4426 *p++ = cpu_to_be32(sess->fore_channel.maxops);
4427 *p++ = cpu_to_be32(sess->fore_channel.maxreqs);
4428 *p++ = cpu_to_be32(sess->fore_channel.nr_rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004429
4430 if (sess->fore_channel.nr_rdma_attrs) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004431 p = xdr_reserve_space(xdr, 4);
4432 if (!p)
4433 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004434 *p++ = cpu_to_be32(sess->fore_channel.rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004435 }
4436
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004437 p = xdr_reserve_space(xdr, 28);
4438 if (!p)
4439 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004440 *p++ = cpu_to_be32(0); /* headerpadsz */
4441 *p++ = cpu_to_be32(sess->back_channel.maxreq_sz);
4442 *p++ = cpu_to_be32(sess->back_channel.maxresp_sz);
4443 *p++ = cpu_to_be32(sess->back_channel.maxresp_cached);
4444 *p++ = cpu_to_be32(sess->back_channel.maxops);
4445 *p++ = cpu_to_be32(sess->back_channel.maxreqs);
4446 *p++ = cpu_to_be32(sess->back_channel.nr_rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004447
4448 if (sess->back_channel.nr_rdma_attrs) {
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004449 p = xdr_reserve_space(xdr, 4);
4450 if (!p)
4451 return nfserr_resource;
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004452 *p++ = cpu_to_be32(sess->back_channel.rdma_attrs);
Andy Adamsonec6b5d72009-04-03 08:28:28 +03004453 }
4454 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03004455}
4456
4457static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004458nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
Andy Adamson2db134e2009-04-03 08:27:55 +03004459 struct nfsd4_sequence *seq)
4460{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004461 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07004462 __be32 *p;
Benny Halevyb85d4c02009-04-03 08:28:08 +03004463
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004464 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 20);
4465 if (!p)
4466 return nfserr_resource;
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04004467 p = xdr_encode_opaque_fixed(p, seq->sessionid.data,
4468 NFS4_MAX_SESSIONID_LEN);
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004469 *p++ = cpu_to_be32(seq->seqid);
4470 *p++ = cpu_to_be32(seq->slotid);
J. Bruce Fieldsb7d7ca32011-08-31 15:39:30 -04004471 /* Note slotid's are numbered from zero: */
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04004472 *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_highest_slotid */
4473 *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_target_highest_slotid */
4474 *p++ = cpu_to_be32(seq->status_flags);
Benny Halevyb85d4c02009-04-03 08:28:08 +03004475
J. Bruce Fieldsf5236012014-03-21 17:57:57 -04004476 resp->cstate.data_offset = xdr->buf->len; /* DRC cache data pointer */
Benny Halevyb85d4c02009-04-03 08:28:08 +03004477 return 0;
Andy Adamson2db134e2009-04-03 08:27:55 +03004478}
4479
J. Bruce Fields2355c592012-04-25 16:56:22 -04004480static __be32
J. Bruce Fields57b7b432012-04-25 17:58:50 -04004481nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
Bryan Schumaker17456802011-07-13 10:50:48 -04004482 struct nfsd4_test_stateid *test_stateid)
4483{
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004484 struct xdr_stream *xdr = &resp->xdr;
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004485 struct nfsd4_test_stateid_id *stateid, *next;
Bryan Schumaker17456802011-07-13 10:50:48 -04004486 __be32 *p;
Bryan Schumaker17456802011-07-13 10:50:48 -04004487
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05004488 p = xdr_reserve_space(xdr, 4 + (4 * test_stateid->ts_num_ids));
4489 if (!p)
4490 return nfserr_resource;
Bryan Schumaker17456802011-07-13 10:50:48 -04004491 *p++ = htonl(test_stateid->ts_num_ids);
Bryan Schumaker17456802011-07-13 10:50:48 -04004492
Bryan Schumaker03cfb422012-01-27 10:22:49 -05004493 list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
Al Viro02f5fde2012-04-13 00:10:34 -04004494 *p++ = stateid->ts_id_status;
Bryan Schumaker17456802011-07-13 10:50:48 -04004495 }
Bryan Schumaker17456802011-07-13 10:50:48 -04004496
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004497 return 0;
Bryan Schumaker17456802011-07-13 10:50:48 -04004498}
4499
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004500#ifdef CONFIG_NFSD_PNFS
4501static __be32
4502nfsd4_encode_getdeviceinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
4503 struct nfsd4_getdeviceinfo *gdev)
4504{
4505 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004506 const struct nfsd4_layout_ops *ops;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004507 u32 starting_len = xdr->buf->len, needed_len;
4508 __be32 *p;
4509
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004510 p = xdr_reserve_space(xdr, 4);
4511 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004512 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004513
4514 *p++ = cpu_to_be32(gdev->gd_layout_type);
4515
4516 /* If maxcount is 0 then just update notifications */
4517 if (gdev->gd_maxcount != 0) {
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004518 ops = nfsd4_layout_ops[gdev->gd_layout_type];
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004519 nfserr = ops->encode_getdeviceinfo(xdr, gdev);
4520 if (nfserr) {
4521 /*
4522 * We don't bother to burden the layout drivers with
4523 * enforcing gd_maxcount, just tell the client to
4524 * come back with a bigger buffer if it's not enough.
4525 */
4526 if (xdr->buf->len + 4 > gdev->gd_maxcount)
4527 goto toosmall;
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004528 return nfserr;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004529 }
4530 }
4531
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004532 if (gdev->gd_notify_types) {
4533 p = xdr_reserve_space(xdr, 4 + 4);
4534 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004535 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004536 *p++ = cpu_to_be32(1); /* bitmap length */
4537 *p++ = cpu_to_be32(gdev->gd_notify_types);
4538 } else {
4539 p = xdr_reserve_space(xdr, 4);
4540 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004541 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004542 *p++ = 0;
4543 }
4544
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004545 return 0;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004546toosmall:
4547 dprintk("%s: maxcount too small\n", __func__);
4548 needed_len = xdr->buf->len + 4 /* notifications */;
4549 xdr_truncate_encode(xdr, starting_len);
4550 p = xdr_reserve_space(xdr, 4);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004551 if (!p)
4552 return nfserr_resource;
4553 *p++ = cpu_to_be32(needed_len);
4554 return nfserr_toosmall;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004555}
4556
4557static __be32
4558nfsd4_encode_layoutget(struct nfsd4_compoundres *resp, __be32 nfserr,
4559 struct nfsd4_layoutget *lgp)
4560{
4561 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004562 const struct nfsd4_layout_ops *ops;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004563 __be32 *p;
4564
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004565 p = xdr_reserve_space(xdr, 36 + sizeof(stateid_opaque_t));
4566 if (!p)
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004567 return nfserr_resource;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004568
4569 *p++ = cpu_to_be32(1); /* we always set return-on-close */
4570 *p++ = cpu_to_be32(lgp->lg_sid.si_generation);
4571 p = xdr_encode_opaque_fixed(p, &lgp->lg_sid.si_opaque,
4572 sizeof(stateid_opaque_t));
4573
4574 *p++ = cpu_to_be32(1); /* we always return a single layout */
4575 p = xdr_encode_hyper(p, lgp->lg_seg.offset);
4576 p = xdr_encode_hyper(p, lgp->lg_seg.length);
4577 *p++ = cpu_to_be32(lgp->lg_seg.iomode);
4578 *p++ = cpu_to_be32(lgp->lg_layout_type);
4579
J. Bruce Fieldsf961e3f2017-05-05 16:17:57 -04004580 ops = nfsd4_layout_ops[lgp->lg_layout_type];
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004581 return ops->encode_layoutget(xdr, lgp);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004582}
4583
4584static __be32
4585nfsd4_encode_layoutcommit(struct nfsd4_compoundres *resp, __be32 nfserr,
4586 struct nfsd4_layoutcommit *lcp)
4587{
4588 struct xdr_stream *xdr = &resp->xdr;
4589 __be32 *p;
4590
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004591 p = xdr_reserve_space(xdr, 4);
4592 if (!p)
4593 return nfserr_resource;
4594 *p++ = cpu_to_be32(lcp->lc_size_chg);
4595 if (lcp->lc_size_chg) {
4596 p = xdr_reserve_space(xdr, 8);
4597 if (!p)
4598 return nfserr_resource;
4599 p = xdr_encode_hyper(p, lcp->lc_newsize);
4600 }
4601
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004602 return 0;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004603}
4604
4605static __be32
4606nfsd4_encode_layoutreturn(struct nfsd4_compoundres *resp, __be32 nfserr,
4607 struct nfsd4_layoutreturn *lrp)
4608{
4609 struct xdr_stream *xdr = &resp->xdr;
4610 __be32 *p;
4611
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004612 p = xdr_reserve_space(xdr, 4);
4613 if (!p)
4614 return nfserr_resource;
4615 *p++ = cpu_to_be32(lrp->lrs_present);
4616 if (lrp->lrs_present)
Kinglong Mee376675d2015-03-22 22:17:10 +08004617 return nfsd4_encode_stateid(xdr, &lrp->lr_sid);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004618 return 0;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02004619}
4620#endif /* CONFIG_NFSD_PNFS */
4621
Andy Adamson2db134e2009-04-03 08:27:55 +03004622static __be32
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004623nfsd42_encode_write_res(struct nfsd4_compoundres *resp,
4624 struct nfsd42_write_res *write, bool sync)
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004625{
4626 __be32 *p;
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004627 p = xdr_reserve_space(&resp->xdr, 4);
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004628 if (!p)
4629 return nfserr_resource;
4630
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004631 if (sync)
4632 *p++ = cpu_to_be32(0);
4633 else {
4634 __be32 nfserr;
4635 *p++ = cpu_to_be32(1);
4636 nfserr = nfsd4_encode_stateid(&resp->xdr, &write->cb_stateid);
4637 if (nfserr)
4638 return nfserr;
4639 }
4640 p = xdr_reserve_space(&resp->xdr, 8 + 4 + NFS4_VERIFIER_SIZE);
4641 if (!p)
4642 return nfserr_resource;
4643
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004644 p = xdr_encode_hyper(p, write->wr_bytes_written);
4645 *p++ = cpu_to_be32(write->wr_stable_how);
4646 p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
4647 NFS4_VERIFIER_SIZE);
4648 return nfs_ok;
4649}
4650
4651static __be32
Olga Kornievskaia51911862019-08-08 11:14:59 -04004652nfsd42_encode_nl4_server(struct nfsd4_compoundres *resp, struct nl4_server *ns)
4653{
4654 struct xdr_stream *xdr = &resp->xdr;
4655 struct nfs42_netaddr *addr;
4656 __be32 *p;
4657
4658 p = xdr_reserve_space(xdr, 4);
4659 *p++ = cpu_to_be32(ns->nl4_type);
4660
4661 switch (ns->nl4_type) {
4662 case NL4_NETADDR:
4663 addr = &ns->u.nl4_addr;
4664
4665 /* netid_len, netid, uaddr_len, uaddr (port included
4666 * in RPCBIND_MAXUADDRLEN)
4667 */
4668 p = xdr_reserve_space(xdr,
4669 4 /* netid len */ +
4670 (XDR_QUADLEN(addr->netid_len) * 4) +
4671 4 /* uaddr len */ +
4672 (XDR_QUADLEN(addr->addr_len) * 4));
4673 if (!p)
4674 return nfserr_resource;
4675
4676 *p++ = cpu_to_be32(addr->netid_len);
4677 p = xdr_encode_opaque_fixed(p, addr->netid,
4678 addr->netid_len);
4679 *p++ = cpu_to_be32(addr->addr_len);
4680 p = xdr_encode_opaque_fixed(p, addr->addr,
4681 addr->addr_len);
4682 break;
4683 default:
4684 WARN_ON_ONCE(ns->nl4_type != NL4_NETADDR);
4685 return nfserr_inval;
4686 }
4687
4688 return 0;
4689}
4690
4691static __be32
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004692nfsd4_encode_copy(struct nfsd4_compoundres *resp, __be32 nfserr,
4693 struct nfsd4_copy *copy)
4694{
4695 __be32 *p;
4696
Olga Kornievskaiae0639dc2018-07-20 18:19:20 -04004697 nfserr = nfsd42_encode_write_res(resp, &copy->cp_res,
4698 copy->cp_synchronous);
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004699 if (nfserr)
4700 return nfserr;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004701
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004702 p = xdr_reserve_space(&resp->xdr, 4 + 4);
J. Bruce Fieldsedcc8452018-03-07 15:37:35 -05004703 *p++ = xdr_one; /* cr_consecutive */
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004704 *p++ = cpu_to_be32(copy->cp_synchronous);
4705 return 0;
Anna Schumaker29ae7f92016-09-07 15:57:30 -04004706}
4707
4708static __be32
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04004709nfsd4_encode_offload_status(struct nfsd4_compoundres *resp, __be32 nfserr,
4710 struct nfsd4_offload_status *os)
4711{
4712 struct xdr_stream *xdr = &resp->xdr;
4713 __be32 *p;
4714
4715 p = xdr_reserve_space(xdr, 8 + 4);
4716 if (!p)
4717 return nfserr_resource;
4718 p = xdr_encode_hyper(p, os->count);
4719 *p++ = cpu_to_be32(0);
Anna Schumaker528b8492020-09-28 13:08:58 -04004720 return nfserr;
4721}
4722
4723static __be32
4724nfsd4_encode_read_plus_data(struct nfsd4_compoundres *resp,
4725 struct nfsd4_read *read,
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004726 unsigned long *maxcount, u32 *eof,
4727 loff_t *pos)
Anna Schumaker528b8492020-09-28 13:08:58 -04004728{
4729 struct xdr_stream *xdr = &resp->xdr;
4730 struct file *file = read->rd_nf->nf_file;
4731 int starting_len = xdr->buf->len;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004732 loff_t hole_pos;
Anna Schumaker528b8492020-09-28 13:08:58 -04004733 __be32 nfserr;
4734 __be32 *p, tmp;
4735 __be64 tmp64;
4736
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004737 hole_pos = pos ? *pos : vfs_llseek(file, read->rd_offset, SEEK_HOLE);
Anna Schumaker2db27992020-09-28 13:08:59 -04004738 if (hole_pos > read->rd_offset)
Anna Schumaker278765e2020-09-28 13:09:00 -04004739 *maxcount = min_t(unsigned long, *maxcount, hole_pos - read->rd_offset);
4740 *maxcount = min_t(unsigned long, *maxcount, (xdr->buf->buflen - xdr->buf->len));
Anna Schumaker528b8492020-09-28 13:08:58 -04004741
4742 /* Content type, offset, byte count */
4743 p = xdr_reserve_space(xdr, 4 + 8 + 4);
4744 if (!p)
4745 return nfserr_resource;
4746
Anna Schumaker278765e2020-09-28 13:09:00 -04004747 read->rd_vlen = xdr_reserve_space_vec(xdr, resp->rqstp->rq_vec, *maxcount);
Anna Schumaker528b8492020-09-28 13:08:58 -04004748 if (read->rd_vlen < 0)
4749 return nfserr_resource;
4750
4751 nfserr = nfsd_readv(resp->rqstp, read->rd_fhp, file, read->rd_offset,
Anna Schumaker278765e2020-09-28 13:09:00 -04004752 resp->rqstp->rq_vec, read->rd_vlen, maxcount, eof);
Anna Schumaker528b8492020-09-28 13:08:58 -04004753 if (nfserr)
4754 return nfserr;
4755
4756 tmp = htonl(NFS4_CONTENT_DATA);
4757 write_bytes_to_xdr_buf(xdr->buf, starting_len, &tmp, 4);
4758 tmp64 = cpu_to_be64(read->rd_offset);
4759 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp64, 8);
Anna Schumaker278765e2020-09-28 13:09:00 -04004760 tmp = htonl(*maxcount);
Anna Schumaker528b8492020-09-28 13:08:58 -04004761 write_bytes_to_xdr_buf(xdr->buf, starting_len + 12, &tmp, 4);
4762 return nfs_ok;
4763}
4764
4765static __be32
Anna Schumaker2db27992020-09-28 13:08:59 -04004766nfsd4_encode_read_plus_hole(struct nfsd4_compoundres *resp,
4767 struct nfsd4_read *read,
Anna Schumaker278765e2020-09-28 13:09:00 -04004768 unsigned long *maxcount, u32 *eof)
Anna Schumaker2db27992020-09-28 13:08:59 -04004769{
4770 struct file *file = read->rd_nf->nf_file;
Anna Schumaker278765e2020-09-28 13:09:00 -04004771 loff_t data_pos = vfs_llseek(file, read->rd_offset, SEEK_DATA);
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004772 loff_t f_size = i_size_read(file_inode(file));
Anna Schumaker278765e2020-09-28 13:09:00 -04004773 unsigned long count;
Anna Schumaker2db27992020-09-28 13:08:59 -04004774 __be32 *p;
4775
Anna Schumaker278765e2020-09-28 13:09:00 -04004776 if (data_pos == -ENXIO)
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004777 data_pos = f_size;
4778 else if (data_pos <= read->rd_offset || (data_pos < f_size && data_pos % PAGE_SIZE))
4779 return nfsd4_encode_read_plus_data(resp, read, maxcount, eof, &f_size);
Anna Schumaker278765e2020-09-28 13:09:00 -04004780 count = data_pos - read->rd_offset;
4781
Anna Schumaker2db27992020-09-28 13:08:59 -04004782 /* Content type, offset, byte count */
4783 p = xdr_reserve_space(&resp->xdr, 4 + 8 + 8);
4784 if (!p)
4785 return nfserr_resource;
4786
4787 *p++ = htonl(NFS4_CONTENT_HOLE);
4788 p = xdr_encode_hyper(p, read->rd_offset);
Anna Schumaker278765e2020-09-28 13:09:00 -04004789 p = xdr_encode_hyper(p, count);
Anna Schumaker2db27992020-09-28 13:08:59 -04004790
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004791 *eof = (read->rd_offset + count) >= f_size;
Anna Schumaker278765e2020-09-28 13:09:00 -04004792 *maxcount = min_t(unsigned long, count, *maxcount);
Anna Schumaker2db27992020-09-28 13:08:59 -04004793 return nfs_ok;
4794}
4795
4796static __be32
Anna Schumaker528b8492020-09-28 13:08:58 -04004797nfsd4_encode_read_plus(struct nfsd4_compoundres *resp, __be32 nfserr,
4798 struct nfsd4_read *read)
4799{
Anna Schumaker278765e2020-09-28 13:09:00 -04004800 unsigned long maxcount, count;
Anna Schumaker528b8492020-09-28 13:08:58 -04004801 struct xdr_stream *xdr = &resp->xdr;
4802 struct file *file;
4803 int starting_len = xdr->buf->len;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004804 int last_segment = xdr->buf->len;
Anna Schumaker528b8492020-09-28 13:08:58 -04004805 int segments = 0;
4806 __be32 *p, tmp;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004807 bool is_data;
Anna Schumaker2db27992020-09-28 13:08:59 -04004808 loff_t pos;
Anna Schumaker528b8492020-09-28 13:08:58 -04004809 u32 eof;
4810
4811 if (nfserr)
4812 return nfserr;
4813 file = read->rd_nf->nf_file;
4814
4815 /* eof flag, segment count */
4816 p = xdr_reserve_space(xdr, 4 + 4);
4817 if (!p)
4818 return nfserr_resource;
4819 xdr_commit_encode(xdr);
4820
4821 maxcount = svc_max_payload(resp->rqstp);
4822 maxcount = min_t(unsigned long, maxcount,
4823 (xdr->buf->buflen - xdr->buf->len));
4824 maxcount = min_t(unsigned long, maxcount, read->rd_length);
Anna Schumaker278765e2020-09-28 13:09:00 -04004825 count = maxcount;
Anna Schumaker528b8492020-09-28 13:08:58 -04004826
4827 eof = read->rd_offset >= i_size_read(file_inode(file));
Anna Schumaker2db27992020-09-28 13:08:59 -04004828 if (eof)
4829 goto out;
4830
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004831 pos = vfs_llseek(file, read->rd_offset, SEEK_HOLE);
4832 is_data = pos > read->rd_offset;
Anna Schumaker2db27992020-09-28 13:08:59 -04004833
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004834 while (count > 0 && !eof) {
Anna Schumaker278765e2020-09-28 13:09:00 -04004835 maxcount = count;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004836 if (is_data)
4837 nfserr = nfsd4_encode_read_plus_data(resp, read, &maxcount, &eof,
4838 segments == 0 ? &pos : NULL);
4839 else
4840 nfserr = nfsd4_encode_read_plus_hole(resp, read, &maxcount, &eof);
Anna Schumaker278765e2020-09-28 13:09:00 -04004841 if (nfserr)
4842 goto out;
4843 count -= maxcount;
4844 read->rd_offset += maxcount;
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004845 is_data = !is_data;
4846 last_segment = xdr->buf->len;
Anna Schumaker528b8492020-09-28 13:08:58 -04004847 segments++;
4848 }
4849
Anna Schumaker2db27992020-09-28 13:08:59 -04004850out:
Anna Schumaker278765e2020-09-28 13:09:00 -04004851 if (nfserr && segments == 0)
Anna Schumaker528b8492020-09-28 13:08:58 -04004852 xdr_truncate_encode(xdr, starting_len);
4853 else {
4854 tmp = htonl(eof);
4855 write_bytes_to_xdr_buf(xdr->buf, starting_len, &tmp, 4);
4856 tmp = htonl(segments);
4857 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp, 4);
Anna Schumaker9f0b5792020-09-28 13:09:01 -04004858 if (nfserr) {
4859 xdr_truncate_encode(xdr, last_segment);
4860 nfserr = nfs_ok;
4861 }
Anna Schumaker528b8492020-09-28 13:08:58 -04004862 }
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04004863
4864 return nfserr;
4865}
4866
4867static __be32
Olga Kornievskaia51911862019-08-08 11:14:59 -04004868nfsd4_encode_copy_notify(struct nfsd4_compoundres *resp, __be32 nfserr,
4869 struct nfsd4_copy_notify *cn)
4870{
4871 struct xdr_stream *xdr = &resp->xdr;
4872 __be32 *p;
4873
4874 if (nfserr)
4875 return nfserr;
4876
4877 /* 8 sec, 4 nsec */
4878 p = xdr_reserve_space(xdr, 12);
4879 if (!p)
4880 return nfserr_resource;
4881
4882 /* cnr_lease_time */
4883 p = xdr_encode_hyper(p, cn->cpn_sec);
4884 *p++ = cpu_to_be32(cn->cpn_nsec);
4885
4886 /* cnr_stateid */
4887 nfserr = nfsd4_encode_stateid(xdr, &cn->cpn_cnr_stateid);
4888 if (nfserr)
4889 return nfserr;
4890
4891 /* cnr_src.nl_nsvr */
4892 p = xdr_reserve_space(xdr, 4);
4893 if (!p)
4894 return nfserr_resource;
4895
4896 *p++ = cpu_to_be32(1);
4897
4898 return nfsd42_encode_nl4_server(resp, &cn->cpn_src);
4899}
4900
4901static __be32
Anna Schumaker24bab492014-09-26 13:58:27 -04004902nfsd4_encode_seek(struct nfsd4_compoundres *resp, __be32 nfserr,
4903 struct nfsd4_seek *seek)
4904{
4905 __be32 *p;
4906
Anna Schumaker24bab492014-09-26 13:58:27 -04004907 p = xdr_reserve_space(&resp->xdr, 4 + 8);
4908 *p++ = cpu_to_be32(seek->seek_eof);
4909 p = xdr_encode_hyper(p, seek->seek_pos);
4910
J. Bruce Fieldsbac966d2017-05-06 11:12:04 -04004911 return 0;
Anna Schumaker24bab492014-09-26 13:58:27 -04004912}
4913
4914static __be32
Benny Halevy695e12f2008-07-04 14:38:33 +03004915nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
4916{
4917 return nfserr;
4918}
4919
Frank van der Linden23e50fe2020-06-23 22:39:26 +00004920/*
4921 * Encode kmalloc-ed buffer in to XDR stream.
4922 */
Chuck Leverb9a49232020-09-11 14:47:53 -04004923static __be32
Frank van der Linden23e50fe2020-06-23 22:39:26 +00004924nfsd4_vbuf_to_stream(struct xdr_stream *xdr, char *buf, u32 buflen)
4925{
4926 u32 cplen;
4927 __be32 *p;
4928
4929 cplen = min_t(unsigned long, buflen,
4930 ((void *)xdr->end - (void *)xdr->p));
4931 p = xdr_reserve_space(xdr, cplen);
4932 if (!p)
4933 return nfserr_resource;
4934
4935 memcpy(p, buf, cplen);
4936 buf += cplen;
4937 buflen -= cplen;
4938
4939 while (buflen) {
4940 cplen = min_t(u32, buflen, PAGE_SIZE);
4941 p = xdr_reserve_space(xdr, cplen);
4942 if (!p)
4943 return nfserr_resource;
4944
4945 memcpy(p, buf, cplen);
4946
4947 if (cplen < PAGE_SIZE) {
4948 /*
4949 * We're done, with a length that wasn't page
4950 * aligned, so possibly not word aligned. Pad
4951 * any trailing bytes with 0.
4952 */
4953 xdr_encode_opaque_fixed(p, NULL, cplen);
4954 break;
4955 }
4956
4957 buflen -= PAGE_SIZE;
4958 buf += PAGE_SIZE;
4959 }
4960
4961 return 0;
4962}
4963
4964static __be32
4965nfsd4_encode_getxattr(struct nfsd4_compoundres *resp, __be32 nfserr,
4966 struct nfsd4_getxattr *getxattr)
4967{
4968 struct xdr_stream *xdr = &resp->xdr;
4969 __be32 *p, err;
4970
4971 p = xdr_reserve_space(xdr, 4);
4972 if (!p)
4973 return nfserr_resource;
4974
4975 *p = cpu_to_be32(getxattr->getxa_len);
4976
4977 if (getxattr->getxa_len == 0)
4978 return 0;
4979
4980 err = nfsd4_vbuf_to_stream(xdr, getxattr->getxa_buf,
4981 getxattr->getxa_len);
4982
4983 kvfree(getxattr->getxa_buf);
4984
4985 return err;
4986}
4987
4988static __be32
4989nfsd4_encode_setxattr(struct nfsd4_compoundres *resp, __be32 nfserr,
4990 struct nfsd4_setxattr *setxattr)
4991{
4992 struct xdr_stream *xdr = &resp->xdr;
4993 __be32 *p;
4994
4995 p = xdr_reserve_space(xdr, 20);
4996 if (!p)
4997 return nfserr_resource;
4998
4999 encode_cinfo(p, &setxattr->setxa_cinfo);
5000
5001 return 0;
5002}
5003
5004/*
5005 * See if there are cookie values that can be rejected outright.
5006 */
5007static __be32
5008nfsd4_listxattr_validate_cookie(struct nfsd4_listxattrs *listxattrs,
5009 u32 *offsetp)
5010{
5011 u64 cookie = listxattrs->lsxa_cookie;
5012
5013 /*
5014 * If the cookie is larger than the maximum number we can fit
5015 * in either the buffer we just got back from vfs_listxattr, or,
5016 * XDR-encoded, in the return buffer, it's invalid.
5017 */
5018 if (cookie > (listxattrs->lsxa_len) / (XATTR_USER_PREFIX_LEN + 2))
5019 return nfserr_badcookie;
5020
5021 if (cookie > (listxattrs->lsxa_maxcount /
5022 (XDR_QUADLEN(XATTR_USER_PREFIX_LEN + 2) + 4)))
5023 return nfserr_badcookie;
5024
5025 *offsetp = (u32)cookie;
5026 return 0;
5027}
5028
5029static __be32
5030nfsd4_encode_listxattrs(struct nfsd4_compoundres *resp, __be32 nfserr,
5031 struct nfsd4_listxattrs *listxattrs)
5032{
5033 struct xdr_stream *xdr = &resp->xdr;
5034 u32 cookie_offset, count_offset, eof;
5035 u32 left, xdrleft, slen, count;
5036 u32 xdrlen, offset;
5037 u64 cookie;
5038 char *sp;
Chuck Leverb9a49232020-09-11 14:47:53 -04005039 __be32 status, tmp;
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005040 __be32 *p;
5041 u32 nuser;
5042
5043 eof = 1;
5044
5045 status = nfsd4_listxattr_validate_cookie(listxattrs, &offset);
5046 if (status)
5047 goto out;
5048
5049 /*
5050 * Reserve space for the cookie and the name array count. Record
5051 * the offsets to save them later.
5052 */
5053 cookie_offset = xdr->buf->len;
5054 count_offset = cookie_offset + 8;
5055 p = xdr_reserve_space(xdr, 12);
5056 if (!p) {
5057 status = nfserr_resource;
5058 goto out;
5059 }
5060
5061 count = 0;
5062 left = listxattrs->lsxa_len;
5063 sp = listxattrs->lsxa_buf;
5064 nuser = 0;
5065
5066 xdrleft = listxattrs->lsxa_maxcount;
5067
5068 while (left > 0 && xdrleft > 0) {
5069 slen = strlen(sp);
5070
5071 /*
Alex Dewar4cce11fa2020-08-17 18:51:26 +01005072 * Check if this is a "user." attribute, skip it if not.
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005073 */
5074 if (strncmp(sp, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
5075 goto contloop;
5076
5077 slen -= XATTR_USER_PREFIX_LEN;
5078 xdrlen = 4 + ((slen + 3) & ~3);
5079 if (xdrlen > xdrleft) {
5080 if (count == 0) {
5081 /*
5082 * Can't even fit the first attribute name.
5083 */
5084 status = nfserr_toosmall;
5085 goto out;
5086 }
5087 eof = 0;
5088 goto wreof;
5089 }
5090
5091 left -= XATTR_USER_PREFIX_LEN;
5092 sp += XATTR_USER_PREFIX_LEN;
5093 if (nuser++ < offset)
5094 goto contloop;
5095
5096
5097 p = xdr_reserve_space(xdr, xdrlen);
5098 if (!p) {
5099 status = nfserr_resource;
5100 goto out;
5101 }
5102
Alex Deware2a18402020-08-12 15:12:51 +01005103 xdr_encode_opaque(p, sp, slen);
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005104
5105 xdrleft -= xdrlen;
5106 count++;
5107contloop:
5108 sp += slen + 1;
5109 left -= slen + 1;
5110 }
5111
5112 /*
5113 * If there were user attributes to copy, but we didn't copy
5114 * any, the offset was too large (e.g. the cookie was invalid).
5115 */
5116 if (nuser > 0 && count == 0) {
5117 status = nfserr_badcookie;
5118 goto out;
5119 }
5120
5121wreof:
5122 p = xdr_reserve_space(xdr, 4);
5123 if (!p) {
5124 status = nfserr_resource;
5125 goto out;
5126 }
5127 *p = cpu_to_be32(eof);
5128
5129 cookie = offset + count;
5130
5131 write_bytes_to_xdr_buf(xdr->buf, cookie_offset, &cookie, 8);
Chuck Leverb9a49232020-09-11 14:47:53 -04005132 tmp = cpu_to_be32(count);
5133 write_bytes_to_xdr_buf(xdr->buf, count_offset, &tmp, 4);
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005134out:
5135 if (listxattrs->lsxa_len)
5136 kvfree(listxattrs->lsxa_buf);
5137 return status;
5138}
5139
5140static __be32
5141nfsd4_encode_removexattr(struct nfsd4_compoundres *resp, __be32 nfserr,
5142 struct nfsd4_removexattr *removexattr)
5143{
5144 struct xdr_stream *xdr = &resp->xdr;
5145 __be32 *p;
5146
5147 p = xdr_reserve_space(xdr, 20);
5148 if (!p)
5149 return nfserr_resource;
5150
5151 p = encode_cinfo(p, &removexattr->rmxa_cinfo);
5152 return 0;
5153}
5154
Benny Halevy695e12f2008-07-04 14:38:33 +03005155typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
5156
Andy Adamson2db134e2009-04-03 08:27:55 +03005157/*
5158 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
5159 * since we don't need to filter out obsolete ops as this is
5160 * done in the decoding phase.
5161 */
Chuck Leverc1df6092017-08-01 11:59:58 -04005162static const nfsd4_enc nfsd4_enc_ops[] = {
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04005163 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
5164 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
5165 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
5166 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
5167 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
5168 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
5169 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
5170 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
5171 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
5172 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
5173 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
5174 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
5175 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
5176 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
5177 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
5178 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
Benny Halevy84f09f42009-03-04 23:05:35 +02005179 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fieldsad1060c2008-07-18 15:04:16 -04005180 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
5181 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
5182 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
5183 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
5184 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
5185 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
5186 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
5187 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
5188 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
5189 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
5190 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
5191 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
5192 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
5193 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
5194 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
5195 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
5196 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
5197 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
5198 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
5199 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03005200
5201 /* NFSv4.1 operations */
5202 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
J. Bruce Fields1d1bc8f2010-10-04 23:12:59 -04005203 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
Andy Adamson2db134e2009-04-03 08:27:55 +03005204 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
5205 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
Kinglong Mee43212cc2013-12-09 19:04:23 +08005206 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_noop,
5207 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
Andy Adamson2db134e2009-04-03 08:27:55 +03005208 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02005209#ifdef CONFIG_NFSD_PNFS
5210 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_getdeviceinfo,
5211 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
5212 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_layoutcommit,
5213 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_layoutget,
5214 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_layoutreturn,
5215#else
Andy Adamson2db134e2009-04-03 08:27:55 +03005216 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
5217 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
5218 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
5219 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
5220 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02005221#endif
Mi Jinlong22b6dee2010-12-27 14:29:57 +08005222 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
Andy Adamson2db134e2009-04-03 08:27:55 +03005223 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
5224 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
Bryan Schumaker17456802011-07-13 10:50:48 -04005225 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
Andy Adamson2db134e2009-04-03 08:27:55 +03005226 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
5227 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
5228 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
Anna Schumaker87a15a82014-09-26 13:58:26 -04005229
5230 /* NFSv4.2 operations */
5231 [OP_ALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
Anna Schumaker29ae7f92016-09-07 15:57:30 -04005232 [OP_COPY] = (nfsd4_enc)nfsd4_encode_copy,
Olga Kornievskaia51911862019-08-08 11:14:59 -04005233 [OP_COPY_NOTIFY] = (nfsd4_enc)nfsd4_encode_copy_notify,
Anna Schumaker87a15a82014-09-26 13:58:26 -04005234 [OP_DEALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
5235 [OP_IO_ADVISE] = (nfsd4_enc)nfsd4_encode_noop,
5236 [OP_LAYOUTERROR] = (nfsd4_enc)nfsd4_encode_noop,
5237 [OP_LAYOUTSTATS] = (nfsd4_enc)nfsd4_encode_noop,
5238 [OP_OFFLOAD_CANCEL] = (nfsd4_enc)nfsd4_encode_noop,
Olga Kornievskaia6308bc92018-07-20 18:19:18 -04005239 [OP_OFFLOAD_STATUS] = (nfsd4_enc)nfsd4_encode_offload_status,
Anna Schumaker528b8492020-09-28 13:08:58 -04005240 [OP_READ_PLUS] = (nfsd4_enc)nfsd4_encode_read_plus,
Anna Schumaker24bab492014-09-26 13:58:27 -04005241 [OP_SEEK] = (nfsd4_enc)nfsd4_encode_seek,
Anna Schumaker87a15a82014-09-26 13:58:26 -04005242 [OP_WRITE_SAME] = (nfsd4_enc)nfsd4_encode_noop,
Christoph Hellwigffa01602015-12-03 12:59:52 +01005243 [OP_CLONE] = (nfsd4_enc)nfsd4_encode_noop,
Frank van der Linden23e50fe2020-06-23 22:39:26 +00005244
5245 /* RFC 8276 extended atributes operations */
5246 [OP_GETXATTR] = (nfsd4_enc)nfsd4_encode_getxattr,
5247 [OP_SETXATTR] = (nfsd4_enc)nfsd4_encode_setxattr,
5248 [OP_LISTXATTRS] = (nfsd4_enc)nfsd4_encode_listxattrs,
5249 [OP_REMOVEXATTR] = (nfsd4_enc)nfsd4_encode_removexattr,
Benny Halevy695e12f2008-07-04 14:38:33 +03005250};
5251
Andy Adamson496c2622009-04-03 08:28:48 +03005252/*
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04005253 * Calculate whether we still have space to encode repsize bytes.
5254 * There are two considerations:
5255 * - For NFS versions >=4.1, the size of the reply must stay within
5256 * session limits
5257 * - For all NFS versions, we must stay within limited preallocated
5258 * buffer space.
Andy Adamson496c2622009-04-03 08:28:48 +03005259 *
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04005260 * This is called before the operation is processed, so can only provide
5261 * an upper estimate. For some nonidempotent operations (such as
5262 * getattr), it's not necessarily a problem if that estimate is wrong,
5263 * as we can fail it after processing without significant side effects.
Andy Adamson496c2622009-04-03 08:28:48 +03005264 */
J. Bruce Fieldsa8095f72014-03-11 17:54:34 -04005265__be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 respsize)
Andy Adamson496c2622009-04-03 08:28:48 +03005266{
J. Bruce Fields67492c92014-03-08 16:42:05 -05005267 struct xdr_buf *buf = &resp->rqstp->rq_res;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04005268 struct nfsd4_slot *slot = resp->cstate.slot;
Andy Adamson496c2622009-04-03 08:28:48 +03005269
J. Bruce Fields47ee5292014-03-12 21:39:35 -04005270 if (buf->len + respsize <= buf->buflen)
5271 return nfs_ok;
5272 if (!nfsd4_has_session(&resp->cstate))
J. Bruce Fieldsea8d7722014-03-08 17:19:55 -05005273 return nfserr_resource;
J. Bruce Fields47ee5292014-03-12 21:39:35 -04005274 if (slot->sl_flags & NFSD4_SLOT_CACHETHIS) {
5275 WARN_ON_ONCE(1);
5276 return nfserr_rep_too_big_to_cache;
J. Bruce Fieldsea8d7722014-03-08 17:19:55 -05005277 }
J. Bruce Fields47ee5292014-03-12 21:39:35 -04005278 return nfserr_rep_too_big;
Andy Adamson496c2622009-04-03 08:28:48 +03005279}
5280
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281void
5282nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
5283{
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005284 struct xdr_stream *xdr = &resp->xdr;
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005285 struct nfs4_stateowner *so = resp->cstate.replay_owner;
J. Bruce Fields5f4ab942014-03-07 11:01:37 -05005286 struct svc_rqst *rqstp = resp->rqstp;
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04005287 const struct nfsd4_operation *opdesc = op->opdesc;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005288 int post_err_offset;
J. Bruce Fields07d1f802014-03-06 20:39:29 -05005289 nfsd4_enc encoder;
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07005290 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05005292 p = xdr_reserve_space(xdr, 8);
5293 if (!p) {
5294 WARN_ON_ONCE(1);
5295 return;
5296 }
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04005297 *p++ = cpu_to_be32(op->opnum);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005298 post_err_offset = xdr->buf->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299
Benny Halevy695e12f2008-07-04 14:38:33 +03005300 if (op->opnum == OP_ILLEGAL)
5301 goto status;
J. Bruce Fieldsb7571e42017-05-06 10:49:21 -04005302 if (op->status && opdesc &&
5303 !(opdesc->op_flags & OP_NONTRIVIAL_ERROR_ENCODE))
5304 goto status;
Benny Halevy695e12f2008-07-04 14:38:33 +03005305 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
5306 !nfsd4_enc_ops[op->opnum]);
J. Bruce Fields07d1f802014-03-06 20:39:29 -05005307 encoder = nfsd4_enc_ops[op->opnum];
5308 op->status = encoder(resp, op->status, &op->u);
Chuck Lever08281342020-11-21 11:36:42 -05005309 if (op->status)
5310 trace_nfsd_compound_encode_err(rqstp, op->opnum, op->status);
J. Bruce Fields34b1744c2017-05-05 17:09:37 -04005311 if (opdesc && opdesc->op_release)
5312 opdesc->op_release(&op->u);
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04005313 xdr_commit_encode(xdr);
5314
J. Bruce Fields067e1ac2014-03-21 17:26:44 -04005315 /* nfsd4_check_resp_size guarantees enough room for error status */
J. Bruce Fields5f4ab942014-03-07 11:01:37 -05005316 if (!op->status) {
5317 int space_needed = 0;
5318 if (!nfsd4_last_compound_op(rqstp))
5319 space_needed = COMPOUND_ERR_SLACK_SPACE;
5320 op->status = nfsd4_check_resp_size(resp, space_needed);
5321 }
J. Bruce Fieldsc8f13d92014-05-08 17:38:18 -04005322 if (op->status == nfserr_resource && nfsd4_has_session(&resp->cstate)) {
5323 struct nfsd4_slot *slot = resp->cstate.slot;
5324
5325 if (slot->sl_flags & NFSD4_SLOT_CACHETHIS)
5326 op->status = nfserr_rep_too_big_to_cache;
5327 else
5328 op->status = nfserr_rep_too_big;
5329 }
J. Bruce Fields07d1f802014-03-06 20:39:29 -05005330 if (op->status == nfserr_resource ||
5331 op->status == nfserr_rep_too_big ||
5332 op->status == nfserr_rep_too_big_to_cache) {
5333 /*
5334 * The operation may have already been encoded or
5335 * partially encoded. No op returns anything additional
5336 * in the case of one of these three errors, so we can
5337 * just truncate back to after the status. But it's a
5338 * bug if we had to do this on a non-idempotent op:
5339 */
5340 warn_on_nonidempotent_op(op);
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005341 xdr_truncate_encode(xdr, post_err_offset);
J. Bruce Fields07d1f802014-03-06 20:39:29 -05005342 }
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005343 if (so) {
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005344 int len = xdr->buf->len - post_err_offset;
5345
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005346 so->so_replay.rp_status = op->status;
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005347 so->so_replay.rp_buflen = len;
5348 read_bytes_from_xdr_buf(xdr->buf, post_err_offset,
5349 so->so_replay.rp_buf, len);
J. Bruce Fields9411b1d2013-04-01 16:37:12 -04005350 }
Benny Halevy695e12f2008-07-04 14:38:33 +03005351status:
J. Bruce Fields082d4bd2013-08-29 15:42:52 -04005352 /* Note that op->status is already in network byte order: */
5353 write_bytes_to_xdr_buf(xdr->buf, post_err_offset - 4, &op->status, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354}
5355
5356/*
5357 * Encode the reply stored in the stateowner reply cache
5358 *
5359 * XDR note: do not encode rp->rp_buflen: the buffer contains the
5360 * previously sent already encoded operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 */
5362void
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05005363nfsd4_encode_replay(struct xdr_stream *xdr, struct nfsd4_op *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364{
J. Bruce Fieldsbc749ca2009-04-07 16:55:27 -07005365 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 struct nfs4_replay *rp = op->replay;
5367
J. Bruce Fieldsd0a381d2014-01-30 17:18:38 -05005368 p = xdr_reserve_space(xdr, 8 + rp->rp_buflen);
5369 if (!p) {
5370 WARN_ON_ONCE(1);
5371 return;
5372 }
J. Bruce Fieldsc373b0a2014-03-22 17:09:18 -04005373 *p++ = cpu_to_be32(op->opnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 *p++ = rp->rp_status; /* already xdr'ed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375
J. Bruce Fields0c0c2672014-03-22 18:43:16 -04005376 p = xdr_encode_opaque_fixed(p, rp->rp_buf, rp->rp_buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377}
5378
Christoph Hellwig85374882017-05-08 18:48:24 +02005379void nfsd4_release_compoundargs(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380{
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04005381 struct nfsd4_compoundargs *args = rqstp->rq_argp;
5382
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 if (args->ops != args->iops) {
5384 kfree(args->ops);
5385 args->ops = args->iops;
5386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 while (args->to_free) {
J. Bruce Fieldsd5e23382014-06-24 17:43:45 -04005388 struct svcxdr_tmpbuf *tb = args->to_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 args->to_free = tb->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 kfree(tb);
5391 }
5392}
5393
5394int
Christoph Hellwig026fec72017-05-08 19:01:48 +02005395nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396{
Christoph Hellwig026fec72017-05-08 19:01:48 +02005397 struct nfsd4_compoundargs *args = rqstp->rq_argp;
5398
Chuck Leverc1346a122020-11-03 11:54:23 -05005399 /* svcxdr_tmp_alloc */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 args->tmpp = NULL;
5401 args->to_free = NULL;
Chuck Leverc1346a122020-11-03 11:54:23 -05005402
5403 args->xdr = &rqstp->rq_arg_stream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 args->ops = args->iops;
5405 args->rqstp = rqstp;
5406
J. Bruce Fields3e98abf2011-07-16 17:15:10 -04005407 return !nfsd4_decode_compound(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408}
5409
5410int
Christoph Hellwig63f8de32017-05-08 19:42:02 +02005411nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412{
Christoph Hellwig63f8de32017-05-08 19:42:02 +02005413 struct nfsd4_compoundres *resp = rqstp->rq_resp;
J. Bruce Fields6ac90392014-02-26 17:39:35 -05005414 struct xdr_buf *buf = resp->xdr.buf;
5415
5416 WARN_ON_ONCE(buf->len != buf->head[0].iov_len + buf->page_len +
5417 buf->tail[0].iov_len);
J. Bruce Fieldsdd97fdd2014-02-26 17:16:27 -05005418
Chuck Levercc028a12020-10-02 15:52:44 -04005419 *p = resp->cstate.status;
5420
J. Bruce Fields2825a7f2013-08-26 16:04:46 -04005421 rqstp->rq_next_page = resp->xdr.page_ptr + 1;
5422
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 p = resp->tagp;
5424 *p++ = htonl(resp->taglen);
5425 memcpy(p, resp->tag, resp->taglen);
5426 p += XDR_QUADLEN(resp->taglen);
5427 *p++ = htonl(resp->opcnt);
5428
Trond Myklebustb6076642014-06-30 11:48:35 -04005429 nfsd4_sequence_done(resp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 return 1;
5431}
5432
5433/*
5434 * Local variables:
5435 * c-basic-offset: 8
5436 * End:
5437 */