]> git.ipfire.org Git - people/ms/linux.git/blame - fs/nfsd/nfs4xdr.c
NFSD: Replace READ* macros in nfsd4_decode_free_stateid()
[people/ms/linux.git] / fs / nfsd / nfs4xdr.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * 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.
1da177e4
LT
34 */
35
96bcad50 36#include <linux/file.h>
5a0e3ad6 37#include <linux/slab.h>
1da177e4 38#include <linux/namei.h>
341eb184 39#include <linux/statfs.h>
0733d213 40#include <linux/utsname.h>
17456804 41#include <linux/pagemap.h>
4796f457 42#include <linux/sunrpc/svcauth_gss.h>
84e1b21d 43#include <linux/sunrpc/addr.h>
23e50fe3
FL
44#include <linux/xattr.h>
45#include <uapi/linux/xattr.h>
9a74af21 46
2ca72e17
BF
47#include "idmap.h"
48#include "acl.h"
9a74af21 49#include "xdr4.h"
0a3adade 50#include "vfs.h"
17456804 51#include "state.h"
1091006c 52#include "cache.h"
3d733711 53#include "netns.h"
9cf514cc 54#include "pnfs.h"
5c4583b2 55#include "filecache.h"
2ca72e17 56
08281341
CL
57#include "trace.h"
58
18032ca0
DQ
59#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
60#include <linux/security.h>
61#endif
62
63
1da177e4
LT
64#define NFSDDBG_FACILITY NFSDDBG_XDR
65
5cf23dbb 66const u32 nfsd_suppattrs[3][3] = {
916d2d84
BF
67 {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
42ca0993
BF
80/*
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
b37ad28b 88static __be32
a36b1725 89check_filename(char *str, int len)
1da177e4
LT
90{
91 int i;
92
93 if (len == 0)
94 return nfserr_inval;
000dfa18
CL
95 if (len > NFS4_MAXNAMLEN)
96 return nfserr_nametoolong;
1da177e4 97 if (isdotent(str, len))
a36b1725 98 return nfserr_badname;
1da177e4
LT
99 for (i = 0; i < len; i++)
100 if (str[i] == '/')
a36b1725 101 return nfserr_badname;
1da177e4
LT
102 return 0;
103}
104
1da177e4 105#define DECODE_HEAD \
2ebbc012 106 __be32 *p; \
b37ad28b 107 __be32 status
1da177e4
LT
108#define DECODE_TAIL \
109 status = 0; \
110out: \
111 return status; \
112xdr_error: \
817cb9d4
CL
113 dprintk("NFSD: xdr error (%s:%d)\n", \
114 __FILE__, __LINE__); \
1da177e4
LT
115 status = nfserr_bad_xdr; \
116 goto out
117
1da177e4
LT
118#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)) { \
817cb9d4
CL
126 dprintk("NFSD: xdr error (%s:%d)\n", \
127 __FILE__, __LINE__); \
1da177e4
LT
128 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)
c1346a12
CL
136#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)
2b86e3aa 143
60adfc50
AA
144static int zero_clientid(clientid_t *clid)
145{
146 return (clid->cl_boot == 0) && (clid->cl_id == 0);
147}
148
2d8498db 149/**
d5e23383 150 * svcxdr_tmpalloc - allocate memory to be freed after compound processing
ce043ac8 151 * @argp: NFSv4 compound argument structure
ed992753 152 * @len: length of buffer to allocate
2d8498db 153 *
ed992753
TM
154 * Allocates a buffer of size @len to be freed when processing the compound
155 * operation described in @argp finishes.
2d8498db 156 */
d5e23383
BF
157static void *
158svcxdr_tmpalloc(struct nfsd4_compoundargs *argp, u32 len)
1da177e4 159{
d5e23383 160 struct svcxdr_tmpbuf *tb;
1da177e4 161
d5e23383 162 tb = kmalloc(sizeof(*tb) + len, GFP_KERNEL);
1da177e4 163 if (!tb)
d5e23383 164 return NULL;
1da177e4
LT
165 tb->next = argp->to_free;
166 argp->to_free = tb;
d5e23383 167 return tb->buf;
1da177e4
LT
168}
169
29c353b3
BF
170/*
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{
d5e23383 180 char *p = svcxdr_tmpalloc(argp, len + 1);
29c353b3
BF
181
182 if (!p)
183 return NULL;
184 memcpy(p, buf, len);
185 p[len] = '\0';
29c353b3 186 return p;
1da177e4
LT
187}
188
2d8498db
CH
189/**
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 */
2ebbc012 199static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
1da177e4 200{
d5e23383
BF
201 void *ret;
202
203 ret = svcxdr_tmpalloc(argp, nbytes);
204 if (!ret)
1da177e4 205 return NULL;
d5e23383
BF
206 memcpy(ret, p, nbytes);
207 return ret;
1da177e4
LT
208}
209
5dcbfabb
CL
210
211/*
212 * NFSv4 basic data type decoders
213 */
214
1a994408
CL
215/*
216 * This helper handles variable-length opaques which belong to protocol
217 * elements that this implementation does not support.
218 */
219static __be32
220nfsd4_decode_ignored_string(struct nfsd4_compoundargs *argp, u32 maxlen)
221{
222 u32 len;
223
224 if (xdr_stream_decode_u32(argp->xdr, &len) < 0)
225 return nfserr_bad_xdr;
226 if (maxlen && len > maxlen)
227 return nfserr_bad_xdr;
228 if (!xdr_inline_decode(argp->xdr, len))
229 return nfserr_bad_xdr;
230
231 return nfs_ok;
232}
233
5dcbfabb
CL
234static __be32
235nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
236{
237 __be32 *p;
238 u32 len;
239
240 if (xdr_stream_decode_u32(argp->xdr, &len) < 0)
241 return nfserr_bad_xdr;
242 if (len == 0 || len > NFS4_OPAQUE_LIMIT)
243 return nfserr_bad_xdr;
244 p = xdr_inline_decode(argp->xdr, len);
245 if (!p)
246 return nfserr_bad_xdr;
247 o->data = svcxdr_tmpalloc(argp, len);
248 if (!o->data)
249 return nfserr_jukebox;
250 o->len = len;
251 memcpy(o->data, p, len);
252
253 return nfs_ok;
254}
255
000dfa18
CL
256static __be32
257nfsd4_decode_component4(struct nfsd4_compoundargs *argp, char **namp, u32 *lenp)
258{
259 __be32 *p, status;
260
261 if (xdr_stream_decode_u32(argp->xdr, lenp) < 0)
262 return nfserr_bad_xdr;
263 p = xdr_inline_decode(argp->xdr, *lenp);
264 if (!p)
265 return nfserr_bad_xdr;
266 status = check_filename((char *)p, *lenp);
267 if (status)
268 return status;
269 *namp = svcxdr_tmpalloc(argp, *lenp);
270 if (!*namp)
271 return nfserr_jukebox;
272 memcpy(*namp, p, *lenp);
273
274 return nfs_ok;
275}
276
4c94e13e 277static __be32
bdba5368 278nfsd4_decode_time(struct nfsd4_compoundargs *argp, struct timespec64 *tv)
4c94e13e
CH
279{
280 DECODE_HEAD;
4c94e13e
CH
281
282 READ_BUF(12);
bdba5368 283 p = xdr_decode_hyper(p, &tv->tv_sec);
4c94e13e
CH
284 tv->tv_nsec = be32_to_cpup(p++);
285 if (tv->tv_nsec >= (u32)1000000000)
286 return nfserr_inval;
287
288 DECODE_TAIL;
289}
290
1c3eff7e
CL
291static __be32
292nfsd4_decode_nfstime4(struct nfsd4_compoundargs *argp, struct timespec64 *tv)
293{
294 __be32 *p;
295
296 p = xdr_inline_decode(argp->xdr, XDR_UNIT * 3);
297 if (!p)
298 return nfserr_bad_xdr;
299 p = xdr_decode_hyper(p, &tv->tv_sec);
300 tv->tv_nsec = be32_to_cpup(p++);
301 if (tv->tv_nsec >= (u32)1000000000)
302 return nfserr_inval;
303 return nfs_ok;
304}
305
796dd1c6
CL
306static __be32
307nfsd4_decode_verifier4(struct nfsd4_compoundargs *argp, nfs4_verifier *verf)
308{
309 __be32 *p;
310
311 p = xdr_inline_decode(argp->xdr, NFS4_VERIFIER_SIZE);
312 if (!p)
313 return nfserr_bad_xdr;
314 memcpy(verf->data, p, sizeof(verf->data));
315 return nfs_ok;
316}
317
d1c263a0
CL
318/**
319 * nfsd4_decode_bitmap4 - Decode an NFSv4 bitmap4
320 * @argp: NFSv4 compound argument structure
321 * @bmval: pointer to an array of u32's to decode into
322 * @bmlen: size of the @bmval array
323 *
324 * The server needs to return nfs_ok rather than nfserr_bad_xdr when
325 * encountering bitmaps containing bits it does not recognize. This
326 * includes bits in bitmap words past WORDn, where WORDn is the last
327 * bitmap WORD the implementation currently supports. Thus we are
328 * careful here to simply ignore bits in bitmap words that this
329 * implementation has yet to support explicitly.
330 *
331 * Return values:
332 * %nfs_ok: @bmval populated successfully
333 * %nfserr_bad_xdr: the encoded bitmap was invalid
334 */
335static __be32
336nfsd4_decode_bitmap4(struct nfsd4_compoundargs *argp, u32 *bmval, u32 bmlen)
337{
338 u32 i, count;
339 __be32 *p;
340
341 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
342 return nfserr_bad_xdr;
343 /* request sanity */
344 if (count > 1000)
345 return nfserr_bad_xdr;
346 p = xdr_inline_decode(argp->xdr, count << 2);
347 if (!p)
348 return nfserr_bad_xdr;
349 i = 0;
350 while (i < count)
351 bmval[i++] = be32_to_cpup(p++);
352 while (i < bmlen)
353 bmval[i++] = 0;
354
355 return nfs_ok;
356}
357
c941a968
CL
358static __be32
359nfsd4_decode_nfsace4(struct nfsd4_compoundargs *argp, struct nfs4_ace *ace)
360{
361 __be32 *p, status;
362 u32 length;
363
364 if (xdr_stream_decode_u32(argp->xdr, &ace->type) < 0)
365 return nfserr_bad_xdr;
366 if (xdr_stream_decode_u32(argp->xdr, &ace->flag) < 0)
367 return nfserr_bad_xdr;
368 if (xdr_stream_decode_u32(argp->xdr, &ace->access_mask) < 0)
369 return nfserr_bad_xdr;
370
371 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
372 return nfserr_bad_xdr;
373 p = xdr_inline_decode(argp->xdr, length);
374 if (!p)
375 return nfserr_bad_xdr;
376 ace->whotype = nfs4_acl_get_whotype((char *)p, length);
377 if (ace->whotype != NFS4_ACL_WHO_NAMED)
378 status = nfs_ok;
379 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
380 status = nfsd_map_name_to_gid(argp->rqstp,
381 (char *)p, length, &ace->who_gid);
382 else
383 status = nfsd_map_name_to_uid(argp->rqstp,
384 (char *)p, length, &ace->who_uid);
385
386 return status;
387}
388
389/* A counted array of nfsace4's */
390static noinline __be32
391nfsd4_decode_acl(struct nfsd4_compoundargs *argp, struct nfs4_acl **acl)
392{
393 struct nfs4_ace *ace;
394 __be32 status;
395 u32 count;
396
397 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
398 return nfserr_bad_xdr;
399
400 if (count > xdr_stream_remaining(argp->xdr) / 20)
401 /*
402 * Even with 4-byte names there wouldn't be
403 * space for that many aces; something fishy is
404 * going on:
405 */
406 return nfserr_fbig;
407
408 *acl = svcxdr_tmpalloc(argp, nfs4_acl_bytes(count));
409 if (*acl == NULL)
410 return nfserr_jukebox;
411
412 (*acl)->naces = count;
413 for (ace = (*acl)->aces; ace < (*acl)->aces + count; ace++) {
414 status = nfsd4_decode_nfsace4(argp, ace);
415 if (status)
416 return status;
417 }
418
419 return nfs_ok;
420}
421
dabe9182
CL
422static noinline __be32
423nfsd4_decode_security_label(struct nfsd4_compoundargs *argp,
424 struct xdr_netobj *label)
425{
426 u32 lfs, pi, length;
427 __be32 *p;
428
429 if (xdr_stream_decode_u32(argp->xdr, &lfs) < 0)
430 return nfserr_bad_xdr;
431 if (xdr_stream_decode_u32(argp->xdr, &pi) < 0)
432 return nfserr_bad_xdr;
433
434 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
435 return nfserr_bad_xdr;
436 if (length > NFS4_MAXLABELLEN)
437 return nfserr_badlabel;
438 p = xdr_inline_decode(argp->xdr, length);
439 if (!p)
440 return nfserr_bad_xdr;
441 label->len = length;
442 label->data = svcxdr_dupstr(argp, p, length);
443 if (!label->data)
444 return nfserr_jukebox;
445
446 return nfs_ok;
447}
448
b37ad28b 449static __be32
d1c263a0
CL
450nfsd4_decode_fattr4(struct nfsd4_compoundargs *argp, u32 *bmval, u32 bmlen,
451 struct iattr *iattr, struct nfs4_acl **acl,
452 struct xdr_netobj *label, int *umask)
1da177e4 453{
081d53fe
CL
454 unsigned int starting_pos;
455 u32 attrlist4_count;
d1c263a0 456 __be32 *p, status;
1da177e4 457
1da177e4 458 iattr->ia_valid = 0;
d1c263a0
CL
459 status = nfsd4_decode_bitmap4(argp, bmval, bmlen);
460 if (status)
461 return nfserr_bad_xdr;
1da177e4 462
e864c189
BF
463 if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
464 || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
465 || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2) {
466 if (nfsd_attrs_supported(argp->minorversion, bmval))
467 return nfserr_inval;
468 return nfserr_attrnotsupp;
469 }
470
081d53fe
CL
471 if (xdr_stream_decode_u32(argp->xdr, &attrlist4_count) < 0)
472 return nfserr_bad_xdr;
473 starting_pos = xdr_stream_pos(argp->xdr);
1da177e4
LT
474
475 if (bmval[0] & FATTR4_WORD0_SIZE) {
2ac1b9b2
CL
476 u64 size;
477
478 if (xdr_stream_decode_u64(argp->xdr, &size) < 0)
479 return nfserr_bad_xdr;
480 iattr->ia_size = size;
1da177e4
LT
481 iattr->ia_valid |= ATTR_SIZE;
482 }
483 if (bmval[0] & FATTR4_WORD0_ACL) {
c941a968
CL
484 status = nfsd4_decode_acl(argp, acl);
485 if (status)
486 return status;
1da177e4
LT
487 } else
488 *acl = NULL;
489 if (bmval[1] & FATTR4_WORD1_MODE) {
1c8f0ad7
CL
490 u32 mode;
491
492 if (xdr_stream_decode_u32(argp->xdr, &mode) < 0)
493 return nfserr_bad_xdr;
494 iattr->ia_mode = mode;
1da177e4
LT
495 iattr->ia_mode &= (S_IFMT | S_IALLUGO);
496 iattr->ia_valid |= ATTR_MODE;
497 }
498 if (bmval[1] & FATTR4_WORD1_OWNER) {
9853a5ac
CL
499 u32 length;
500
501 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
502 return nfserr_bad_xdr;
503 p = xdr_inline_decode(argp->xdr, length);
504 if (!p)
505 return nfserr_bad_xdr;
506 status = nfsd_map_name_to_uid(argp->rqstp, (char *)p, length,
507 &iattr->ia_uid);
508 if (status)
47c85291 509 return status;
1da177e4
LT
510 iattr->ia_valid |= ATTR_UID;
511 }
512 if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
393c31dd
CL
513 u32 length;
514
515 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
516 return nfserr_bad_xdr;
517 p = xdr_inline_decode(argp->xdr, length);
518 if (!p)
519 return nfserr_bad_xdr;
520 status = nfsd_map_name_to_gid(argp->rqstp, (char *)p, length,
521 &iattr->ia_gid);
522 if (status)
47c85291 523 return status;
1da177e4
LT
524 iattr->ia_valid |= ATTR_GID;
525 }
526 if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
1c3eff7e
CL
527 u32 set_it;
528
529 if (xdr_stream_decode_u32(argp->xdr, &set_it) < 0)
530 return nfserr_bad_xdr;
531 switch (set_it) {
1da177e4 532 case NFS4_SET_TO_CLIENT_TIME:
1c3eff7e 533 status = nfsd4_decode_nfstime4(argp, &iattr->ia_atime);
4c94e13e
CH
534 if (status)
535 return status;
1da177e4
LT
536 iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
537 break;
538 case NFS4_SET_TO_SERVER_TIME:
539 iattr->ia_valid |= ATTR_ATIME;
540 break;
541 default:
1c3eff7e 542 return nfserr_bad_xdr;
1da177e4
LT
543 }
544 }
1da177e4 545 if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
1c3eff7e
CL
546 u32 set_it;
547
548 if (xdr_stream_decode_u32(argp->xdr, &set_it) < 0)
549 return nfserr_bad_xdr;
550 switch (set_it) {
1da177e4 551 case NFS4_SET_TO_CLIENT_TIME:
1c3eff7e 552 status = nfsd4_decode_nfstime4(argp, &iattr->ia_mtime);
4c94e13e
CH
553 if (status)
554 return status;
1da177e4
LT
555 iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
556 break;
557 case NFS4_SET_TO_SERVER_TIME:
558 iattr->ia_valid |= ATTR_MTIME;
559 break;
560 default:
1c3eff7e 561 return nfserr_bad_xdr;
1da177e4
LT
562 }
563 }
18032ca0 564 label->len = 0;
2285ae76
AB
565 if (IS_ENABLED(CONFIG_NFSD_V4_SECURITY_LABEL) &&
566 bmval[2] & FATTR4_WORD2_SECURITY_LABEL) {
dabe9182
CL
567 status = nfsd4_decode_security_label(argp, label);
568 if (status)
569 return status;
18032ca0 570 }
47057abd 571 if (bmval[2] & FATTR4_WORD2_MODE_UMASK) {
66f0476c
CL
572 u32 mode, mask;
573
47057abd 574 if (!umask)
66f0476c
CL
575 return nfserr_bad_xdr;
576 if (xdr_stream_decode_u32(argp->xdr, &mode) < 0)
577 return nfserr_bad_xdr;
578 iattr->ia_mode = mode & (S_IFMT | S_IALLUGO);
579 if (xdr_stream_decode_u32(argp->xdr, &mask) < 0)
580 return nfserr_bad_xdr;
581 *umask = mask & S_IRWXUGO;
47057abd
AG
582 iattr->ia_valid |= ATTR_MODE;
583 }
081d53fe
CL
584
585 /* request sanity: did attrlist4 contain the expected number of words? */
586 if (attrlist4_count != xdr_stream_pos(argp->xdr) - starting_pos)
587 return nfserr_bad_xdr;
1da177e4 588
d1c263a0 589 return nfs_ok;
1da177e4
LT
590}
591
e31a1b66
BH
592static __be32
593nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
594{
595 DECODE_HEAD;
596
597 READ_BUF(sizeof(stateid_t));
06553991 598 sid->si_generation = be32_to_cpup(p++);
e31a1b66
BH
599 COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
600
601 DECODE_TAIL;
602}
603
d3d2f381
CL
604static __be32
605nfsd4_decode_stateid4(struct nfsd4_compoundargs *argp, stateid_t *sid)
606{
607 __be32 *p;
608
609 p = xdr_inline_decode(argp->xdr, NFS4_STATEID_SIZE);
610 if (!p)
611 return nfserr_bad_xdr;
612 sid->si_generation = be32_to_cpup(p++);
613 memcpy(&sid->si_opaque, p, sizeof(sid->si_opaque));
614 return nfs_ok;
615}
616
144e8269
CL
617static __be32
618nfsd4_decode_clientid4(struct nfsd4_compoundargs *argp, clientid_t *clientid)
619{
620 __be32 *p;
621
622 p = xdr_inline_decode(argp->xdr, sizeof(__be64));
623 if (!p)
624 return nfserr_bad_xdr;
625 memcpy(clientid, p, sizeof(*clientid));
626 return nfs_ok;
627}
628
629static __be32
630nfsd4_decode_state_owner4(struct nfsd4_compoundargs *argp,
631 clientid_t *clientid, struct xdr_netobj *owner)
632{
633 __be32 status;
634
635 status = nfsd4_decode_clientid4(argp, clientid);
636 if (status)
637 return status;
638 return nfsd4_decode_opaque(argp, owner);
639}
640
571e0451
CL
641static __be32
642nfsd4_decode_sessionid4(struct nfsd4_compoundargs *argp,
643 struct nfs4_sessionid *sessionid)
644{
645 __be32 *p;
646
647 p = xdr_inline_decode(argp->xdr, NFS4_MAX_SESSIONID_LEN);
648 if (!p)
649 return nfserr_bad_xdr;
650 memcpy(sessionid->data, p, sizeof(sessionid->data));
651 return nfs_ok;
652}
653
1a994408
CL
654/* Defined in Appendix A of RFC 5531 */
655static __be32
656nfsd4_decode_authsys_parms(struct nfsd4_compoundargs *argp,
657 struct nfsd4_cb_sec *cbs)
acb2887e 658{
1a994408
CL
659 u32 stamp, gidcount, uid, gid;
660 __be32 *p, status;
661
662 if (xdr_stream_decode_u32(argp->xdr, &stamp) < 0)
663 return nfserr_bad_xdr;
664 /* machine name */
665 status = nfsd4_decode_ignored_string(argp, 255);
666 if (status)
667 return status;
668 if (xdr_stream_decode_u32(argp->xdr, &uid) < 0)
669 return nfserr_bad_xdr;
670 if (xdr_stream_decode_u32(argp->xdr, &gid) < 0)
671 return nfserr_bad_xdr;
672 if (xdr_stream_decode_u32(argp->xdr, &gidcount) < 0)
673 return nfserr_bad_xdr;
674 if (gidcount > 16)
675 return nfserr_bad_xdr;
676 p = xdr_inline_decode(argp->xdr, gidcount << 2);
677 if (!p)
678 return nfserr_bad_xdr;
679 if (cbs->flavor == (u32)(-1)) {
680 struct user_namespace *userns = nfsd_user_namespace(argp->rqstp);
681
682 kuid_t kuid = make_kuid(userns, uid);
683 kgid_t kgid = make_kgid(userns, gid);
684 if (uid_valid(kuid) && gid_valid(kgid)) {
685 cbs->uid = kuid;
686 cbs->gid = kgid;
687 cbs->flavor = RPC_AUTH_UNIX;
688 } else {
689 dprintk("RPC_AUTH_UNIX with invalid uid or gid, ignoring!\n");
690 }
691 }
692
693 return nfs_ok;
694}
695
696static __be32
697nfsd4_decode_gss_cb_handles4(struct nfsd4_compoundargs *argp,
698 struct nfsd4_cb_sec *cbs)
699{
700 __be32 status;
701 u32 service;
702
703 dprintk("RPC_AUTH_GSS callback secflavor not supported!\n");
704
705 if (xdr_stream_decode_u32(argp->xdr, &service) < 0)
706 return nfserr_bad_xdr;
707 if (service < RPC_GSS_SVC_NONE || service > RPC_GSS_SVC_PRIVACY)
708 return nfserr_bad_xdr;
709 /* gcbp_handle_from_server */
710 status = nfsd4_decode_ignored_string(argp, 0);
711 if (status)
712 return status;
713 /* gcbp_handle_from_client */
714 status = nfsd4_decode_ignored_string(argp, 0);
715 if (status)
716 return status;
717
718 return nfs_ok;
719}
720
721/* a counted array of callback_sec_parms4 items */
722static __be32
723nfsd4_decode_cb_sec(struct nfsd4_compoundargs *argp, struct nfsd4_cb_sec *cbs)
724{
725 u32 i, secflavor, nr_secflavs;
726 __be32 status;
acb2887e
BF
727
728 /* callback_sec_params4 */
1a994408
CL
729 if (xdr_stream_decode_u32(argp->xdr, &nr_secflavs) < 0)
730 return nfserr_bad_xdr;
57569a70
BF
731 if (nr_secflavs)
732 cbs->flavor = (u32)(-1);
733 else
734 /* Is this legal? Be generous, take it to mean AUTH_NONE: */
735 cbs->flavor = 0;
1a994408 736
acb2887e 737 for (i = 0; i < nr_secflavs; ++i) {
1a994408
CL
738 if (xdr_stream_decode_u32(argp->xdr, &secflavor) < 0)
739 return nfserr_bad_xdr;
740 switch (secflavor) {
acb2887e 741 case RPC_AUTH_NULL:
1a994408 742 /* void */
12fc3e92
BF
743 if (cbs->flavor == (u32)(-1))
744 cbs->flavor = RPC_AUTH_NULL;
acb2887e
BF
745 break;
746 case RPC_AUTH_UNIX:
1a994408
CL
747 status = nfsd4_decode_authsys_parms(argp, cbs);
748 if (status)
749 return status;
acb2887e
BF
750 break;
751 case RPC_AUTH_GSS:
1a994408
CL
752 status = nfsd4_decode_gss_cb_handles4(argp, cbs);
753 if (status)
754 return status;
acb2887e
BF
755 break;
756 default:
acb2887e
BF
757 return nfserr_inval;
758 }
759 }
1a994408
CL
760
761 return nfs_ok;
acb2887e
BF
762}
763
1a994408 764
d169a6a9
CL
765/*
766 * NFSv4 operation argument decoders
767 */
768
769static __be32
770nfsd4_decode_access(struct nfsd4_compoundargs *argp,
771 struct nfsd4_access *access)
772{
773 if (xdr_stream_decode_u32(argp->xdr, &access->ac_req_access) < 0)
774 return nfserr_bad_xdr;
775 return nfs_ok;
776}
777
b37ad28b 778static __be32
1da177e4
LT
779nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
780{
d3d2f381
CL
781 if (xdr_stream_decode_u32(argp->xdr, &close->cl_seqid) < 0)
782 return nfserr_bad_xdr;
783 return nfsd4_decode_stateid4(argp, &close->cl_stateid);
1da177e4
LT
784}
785
786
b37ad28b 787static __be32
1da177e4
LT
788nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
789{
cbd9abb3
CL
790 if (xdr_stream_decode_u64(argp->xdr, &commit->co_offset) < 0)
791 return nfserr_bad_xdr;
792 if (xdr_stream_decode_u32(argp->xdr, &commit->co_count) < 0)
793 return nfserr_bad_xdr;
794 return nfs_ok;
1da177e4
LT
795}
796
b37ad28b 797static __be32
1da177e4
LT
798nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
799{
000dfa18 800 __be32 *p, status;
1da177e4 801
000dfa18
CL
802 if (xdr_stream_decode_u32(argp->xdr, &create->cr_type) < 0)
803 return nfserr_bad_xdr;
1da177e4
LT
804 switch (create->cr_type) {
805 case NF4LNK:
000dfa18
CL
806 if (xdr_stream_decode_u32(argp->xdr, &create->cr_datalen) < 0)
807 return nfserr_bad_xdr;
808 p = xdr_inline_decode(argp->xdr, create->cr_datalen);
809 if (!p)
810 return nfserr_bad_xdr;
29c353b3 811 create->cr_data = svcxdr_dupstr(argp, p, create->cr_datalen);
7fb84306 812 if (!create->cr_data)
76f47128 813 return nfserr_jukebox;
1da177e4
LT
814 break;
815 case NF4BLK:
816 case NF4CHR:
000dfa18
CL
817 if (xdr_stream_decode_u32(argp->xdr, &create->cr_specdata1) < 0)
818 return nfserr_bad_xdr;
819 if (xdr_stream_decode_u32(argp->xdr, &create->cr_specdata2) < 0)
820 return nfserr_bad_xdr;
1da177e4
LT
821 break;
822 case NF4SOCK:
823 case NF4FIFO:
824 case NF4DIR:
825 default:
826 break;
827 }
000dfa18
CL
828 status = nfsd4_decode_component4(argp, &create->cr_name,
829 &create->cr_namelen);
830 if (status)
1da177e4 831 return status;
d1c263a0
CL
832 status = nfsd4_decode_fattr4(argp, create->cr_bmval,
833 ARRAY_SIZE(create->cr_bmval),
834 &create->cr_iattr, &create->cr_acl,
835 &create->cr_label, &create->cr_umask);
c0d6fc8a 836 if (status)
000dfa18 837 return status;
1da177e4 838
000dfa18 839 return nfs_ok;
1da177e4
LT
840}
841
b37ad28b 842static inline __be32
1da177e4
LT
843nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
844{
95e6482c 845 return nfsd4_decode_stateid4(argp, &dr->dr_stateid);
1da177e4
LT
846}
847
b37ad28b 848static inline __be32
1da177e4
LT
849nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
850{
f759eff2
CL
851 return nfsd4_decode_bitmap4(argp, getattr->ga_bmval,
852 ARRAY_SIZE(getattr->ga_bmval));
1da177e4
LT
853}
854
b37ad28b 855static __be32
1da177e4
LT
856nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
857{
5c505d12 858 return nfsd4_decode_component4(argp, &link->li_name, &link->li_namelen);
1da177e4
LT
859}
860
8918cc0d
CL
861static __be32
862nfsd4_decode_open_to_lock_owner4(struct nfsd4_compoundargs *argp,
863 struct nfsd4_lock *lock)
864{
865 __be32 status;
866
867 if (xdr_stream_decode_u32(argp->xdr, &lock->lk_new_open_seqid) < 0)
868 return nfserr_bad_xdr;
869 status = nfsd4_decode_stateid4(argp, &lock->lk_new_open_stateid);
870 if (status)
871 return status;
872 if (xdr_stream_decode_u32(argp->xdr, &lock->lk_new_lock_seqid) < 0)
873 return nfserr_bad_xdr;
874 return nfsd4_decode_state_owner4(argp, &lock->lk_new_clientid,
875 &lock->lk_new_owner);
876}
877
878static __be32
879nfsd4_decode_exist_lock_owner4(struct nfsd4_compoundargs *argp,
880 struct nfsd4_lock *lock)
881{
882 __be32 status;
883
884 status = nfsd4_decode_stateid4(argp, &lock->lk_old_lock_stateid);
885 if (status)
886 return status;
887 if (xdr_stream_decode_u32(argp->xdr, &lock->lk_old_lock_seqid) < 0)
888 return nfserr_bad_xdr;
889
890 return nfs_ok;
891}
892
893static __be32
894nfsd4_decode_locker4(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
895{
896 if (xdr_stream_decode_bool(argp->xdr, &lock->lk_is_new) < 0)
897 return nfserr_bad_xdr;
898 if (lock->lk_is_new)
899 return nfsd4_decode_open_to_lock_owner4(argp, lock);
900 return nfsd4_decode_exist_lock_owner4(argp, lock);
901}
902
b37ad28b 903static __be32
1da177e4
LT
904nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
905{
7c59deed
CL
906 if (xdr_stream_decode_u32(argp->xdr, &lock->lk_type) < 0)
907 return nfserr_bad_xdr;
1da177e4 908 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
7c59deed
CL
909 return nfserr_bad_xdr;
910 if (xdr_stream_decode_bool(argp->xdr, &lock->lk_reclaim) < 0)
911 return nfserr_bad_xdr;
912 if (xdr_stream_decode_u64(argp->xdr, &lock->lk_offset) < 0)
913 return nfserr_bad_xdr;
914 if (xdr_stream_decode_u64(argp->xdr, &lock->lk_length) < 0)
915 return nfserr_bad_xdr;
916 return nfsd4_decode_locker4(argp, lock);
1da177e4
LT
917}
918
b37ad28b 919static __be32
1da177e4
LT
920nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
921{
0a146f04
CL
922 if (xdr_stream_decode_u32(argp->xdr, &lockt->lt_type) < 0)
923 return nfserr_bad_xdr;
924 if ((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
925 return nfserr_bad_xdr;
926 if (xdr_stream_decode_u64(argp->xdr, &lockt->lt_offset) < 0)
927 return nfserr_bad_xdr;
928 if (xdr_stream_decode_u64(argp->xdr, &lockt->lt_length) < 0)
929 return nfserr_bad_xdr;
930 return nfsd4_decode_state_owner4(argp, &lockt->lt_clientid,
931 &lockt->lt_owner);
1da177e4
LT
932}
933
b37ad28b 934static __be32
1da177e4
LT
935nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
936{
ca9cf9fc 937 __be32 status;
1da177e4 938
ca9cf9fc
CL
939 if (xdr_stream_decode_u32(argp->xdr, &locku->lu_type) < 0)
940 return nfserr_bad_xdr;
1da177e4 941 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
ca9cf9fc
CL
942 return nfserr_bad_xdr;
943 if (xdr_stream_decode_u32(argp->xdr, &locku->lu_seqid) < 0)
944 return nfserr_bad_xdr;
945 status = nfsd4_decode_stateid4(argp, &locku->lu_stateid);
e31a1b66
BH
946 if (status)
947 return status;
ca9cf9fc
CL
948 if (xdr_stream_decode_u64(argp->xdr, &locku->lu_offset) < 0)
949 return nfserr_bad_xdr;
950 if (xdr_stream_decode_u64(argp->xdr, &locku->lu_length) < 0)
951 return nfserr_bad_xdr;
1da177e4 952
ca9cf9fc 953 return nfs_ok;
1da177e4
LT
954}
955
b37ad28b 956static __be32
1da177e4
LT
957nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
958{
3d5877e8 959 return nfsd4_decode_component4(argp, &lookup->lo_name, &lookup->lo_len);
1da177e4
LT
960}
961
bf33bab3
CL
962static __be32
963nfsd4_decode_createhow4(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
964{
965 __be32 status;
966
967 if (xdr_stream_decode_u32(argp->xdr, &open->op_createmode) < 0)
968 return nfserr_bad_xdr;
969 switch (open->op_createmode) {
970 case NFS4_CREATE_UNCHECKED:
971 case NFS4_CREATE_GUARDED:
972 status = nfsd4_decode_fattr4(argp, open->op_bmval,
973 ARRAY_SIZE(open->op_bmval),
974 &open->op_iattr, &open->op_acl,
975 &open->op_label, &open->op_umask);
976 if (status)
977 return status;
978 break;
979 case NFS4_CREATE_EXCLUSIVE:
980 status = nfsd4_decode_verifier4(argp, &open->op_verf);
981 if (status)
982 return status;
983 break;
984 case NFS4_CREATE_EXCLUSIVE4_1:
985 if (argp->minorversion < 1)
986 return nfserr_bad_xdr;
987 status = nfsd4_decode_verifier4(argp, &open->op_verf);
988 if (status)
989 return status;
990 status = nfsd4_decode_fattr4(argp, open->op_bmval,
991 ARRAY_SIZE(open->op_bmval),
992 &open->op_iattr, &open->op_acl,
993 &open->op_label, &open->op_umask);
994 if (status)
995 return status;
996 break;
997 default:
998 return nfserr_bad_xdr;
999 }
1000
1001 return nfs_ok;
1002}
1003
e6ec04b2
CL
1004static __be32
1005nfsd4_decode_openflag4(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
1006{
1007 __be32 status;
1008
1009 if (xdr_stream_decode_u32(argp->xdr, &open->op_create) < 0)
1010 return nfserr_bad_xdr;
1011 switch (open->op_create) {
1012 case NFS4_OPEN_NOCREATE:
1013 break;
1014 case NFS4_OPEN_CREATE:
1015 status = nfsd4_decode_createhow4(argp, open);
1016 if (status)
1017 return status;
1018 break;
1019 default:
1020 return nfserr_bad_xdr;
1021 }
1022
1023 return nfs_ok;
1024}
1025
2c8bd7e0 1026static __be32 nfsd4_decode_share_access(struct nfsd4_compoundargs *argp, u32 *share_access, u32 *deleg_want, u32 *deleg_when)
04f9e664 1027{
04f9e664
BF
1028 u32 w;
1029
9aa62f51
CL
1030 if (xdr_stream_decode_u32(argp->xdr, &w) < 0)
1031 return nfserr_bad_xdr;
2c8bd7e0
BH
1032 *share_access = w & NFS4_SHARE_ACCESS_MASK;
1033 *deleg_want = w & NFS4_SHARE_WANT_MASK;
1034 if (deleg_when)
1035 *deleg_when = w & NFS4_SHARE_WHEN_MASK;
1036
04f9e664
BF
1037 switch (w & NFS4_SHARE_ACCESS_MASK) {
1038 case NFS4_SHARE_ACCESS_READ:
1039 case NFS4_SHARE_ACCESS_WRITE:
1040 case NFS4_SHARE_ACCESS_BOTH:
1041 break;
1042 default:
1043 return nfserr_bad_xdr;
1044 }
fc0d14fe 1045 w &= ~NFS4_SHARE_ACCESS_MASK;
04f9e664
BF
1046 if (!w)
1047 return nfs_ok;
1048 if (!argp->minorversion)
1049 return nfserr_bad_xdr;
1050 switch (w & NFS4_SHARE_WANT_MASK) {
1051 case NFS4_SHARE_WANT_NO_PREFERENCE:
1052 case NFS4_SHARE_WANT_READ_DELEG:
1053 case NFS4_SHARE_WANT_WRITE_DELEG:
1054 case NFS4_SHARE_WANT_ANY_DELEG:
1055 case NFS4_SHARE_WANT_NO_DELEG:
1056 case NFS4_SHARE_WANT_CANCEL:
1057 break;
1058 default:
1059 return nfserr_bad_xdr;
1060 }
92bac8c5 1061 w &= ~NFS4_SHARE_WANT_MASK;
04f9e664
BF
1062 if (!w)
1063 return nfs_ok;
2c8bd7e0
BH
1064
1065 if (!deleg_when) /* open_downgrade */
1066 return nfserr_inval;
04f9e664
BF
1067 switch (w) {
1068 case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
1069 case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
c668fc6d
BH
1070 case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
1071 NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
04f9e664
BF
1072 return nfs_ok;
1073 }
04f9e664
BF
1074 return nfserr_bad_xdr;
1075}
1076
1077static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
1078{
b07bebd9
CL
1079 if (xdr_stream_decode_u32(argp->xdr, x) < 0)
1080 return nfserr_bad_xdr;
1081 /* Note: unlike access bits, deny bits may be zero. */
01cd4afa 1082 if (*x & ~NFS4_SHARE_DENY_BOTH)
04f9e664 1083 return nfserr_bad_xdr;
b07bebd9 1084
04f9e664 1085 return nfs_ok;
04f9e664
BF
1086}
1087
1708e50b
CL
1088static __be32
1089nfsd4_decode_open_claim4(struct nfsd4_compoundargs *argp,
1090 struct nfsd4_open *open)
1091{
1092 __be32 status;
1093
1094 if (xdr_stream_decode_u32(argp->xdr, &open->op_claim_type) < 0)
1095 return nfserr_bad_xdr;
1096 switch (open->op_claim_type) {
1097 case NFS4_OPEN_CLAIM_NULL:
1098 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
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_PREVIOUS:
1105 if (xdr_stream_decode_u32(argp->xdr, &open->op_delegate_type) < 0)
1106 return nfserr_bad_xdr;
1107 break;
1108 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1109 status = nfsd4_decode_stateid4(argp, &open->op_delegate_stateid);
1110 if (status)
1111 return status;
1112 status = nfsd4_decode_component4(argp, &open->op_fname,
1113 &open->op_fnamelen);
1114 if (status)
1115 return status;
1116 break;
1117 case NFS4_OPEN_CLAIM_FH:
1118 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1119 if (argp->minorversion < 1)
1120 return nfserr_bad_xdr;
1121 /* void */
1122 break;
1123 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1124 if (argp->minorversion < 1)
1125 return nfserr_bad_xdr;
1126 status = nfsd4_decode_stateid4(argp, &open->op_delegate_stateid);
1127 if (status)
1128 return status;
1129 break;
1130 default:
1131 return nfserr_bad_xdr;
1132 }
1133
1134 return nfs_ok;
1135}
1136
b37ad28b 1137static __be32
1da177e4
LT
1138nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
1139{
61e5e0b3 1140 __be32 status;
2c8bd7e0 1141 u32 dummy;
1da177e4
LT
1142
1143 memset(open->op_bmval, 0, sizeof(open->op_bmval));
1144 open->op_iattr.ia_valid = 0;
fe0750e5 1145 open->op_openowner = NULL;
1da177e4 1146
9d313b17 1147 open->op_xdr_error = 0;
61e5e0b3
CL
1148 if (xdr_stream_decode_u32(argp->xdr, &open->op_seqid) < 0)
1149 return nfserr_bad_xdr;
1150 /* deleg_want is ignored */
2c8bd7e0
BH
1151 status = nfsd4_decode_share_access(argp, &open->op_share_access,
1152 &open->op_deleg_want, &dummy);
04f9e664 1153 if (status)
61e5e0b3 1154 return status;
04f9e664
BF
1155 status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
1156 if (status)
61e5e0b3
CL
1157 return status;
1158 status = nfsd4_decode_state_owner4(argp, &open->op_clientid,
1159 &open->op_owner);
a084daf5 1160 if (status)
61e5e0b3 1161 return status;
e6ec04b2
CL
1162 status = nfsd4_decode_openflag4(argp, open);
1163 if (status)
1164 return status;
61e5e0b3 1165 return nfsd4_decode_open_claim4(argp, open);
1da177e4
LT
1166}
1167
b37ad28b 1168static __be32
1da177e4
LT
1169nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
1170{
06bee693 1171 __be32 status;
e1a90ebd
AS
1172
1173 if (argp->minorversion >= 1)
1174 return nfserr_notsupp;
1175
06bee693 1176 status = nfsd4_decode_stateid4(argp, &open_conf->oc_req_stateid);
e31a1b66
BH
1177 if (status)
1178 return status;
06bee693
CL
1179 if (xdr_stream_decode_u32(argp->xdr, &open_conf->oc_seqid) < 0)
1180 return nfserr_bad_xdr;
e1a90ebd 1181
06bee693 1182 return nfs_ok;
1da177e4
LT
1183}
1184
b37ad28b 1185static __be32
1da177e4
LT
1186nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
1187{
dca71651
CL
1188 __be32 status;
1189
1190 status = nfsd4_decode_stateid4(argp, &open_down->od_stateid);
e31a1b66
BH
1191 if (status)
1192 return status;
dca71651
CL
1193 if (xdr_stream_decode_u32(argp->xdr, &open_down->od_seqid) < 0)
1194 return nfserr_bad_xdr;
1195 /* deleg_want is ignored */
2c8bd7e0
BH
1196 status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
1197 &open_down->od_deleg_want, NULL);
04f9e664
BF
1198 if (status)
1199 return status;
dca71651 1200 return nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
1da177e4
LT
1201}
1202
b37ad28b 1203static __be32
1da177e4
LT
1204nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
1205{
a73bed98 1206 __be32 *p;
1da177e4 1207
a73bed98
CL
1208 if (xdr_stream_decode_u32(argp->xdr, &putfh->pf_fhlen) < 0)
1209 return nfserr_bad_xdr;
1da177e4 1210 if (putfh->pf_fhlen > NFS4_FHSIZE)
a73bed98
CL
1211 return nfserr_bad_xdr;
1212 p = xdr_inline_decode(argp->xdr, putfh->pf_fhlen);
1213 if (!p)
1214 return nfserr_bad_xdr;
1215 putfh->pf_fhval = svcxdr_tmpalloc(argp, putfh->pf_fhlen);
1216 if (!putfh->pf_fhval)
1217 return nfserr_jukebox;
1218 memcpy(putfh->pf_fhval, p, putfh->pf_fhlen);
1da177e4 1219
a73bed98 1220 return nfs_ok;
1da177e4
LT
1221}
1222
e1a90ebd
AS
1223static __be32
1224nfsd4_decode_putpubfh(struct nfsd4_compoundargs *argp, void *p)
1225{
1226 if (argp->minorversion == 0)
1227 return nfs_ok;
1228 return nfserr_notsupp;
1229}
1230
b37ad28b 1231static __be32
1da177e4
LT
1232nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
1233{
3909c3bc 1234 __be32 status;
1da177e4 1235
3909c3bc 1236 status = nfsd4_decode_stateid4(argp, &read->rd_stateid);
e31a1b66
BH
1237 if (status)
1238 return status;
3909c3bc
CL
1239 if (xdr_stream_decode_u64(argp->xdr, &read->rd_offset) < 0)
1240 return nfserr_bad_xdr;
1241 if (xdr_stream_decode_u32(argp->xdr, &read->rd_length) < 0)
1242 return nfserr_bad_xdr;
1da177e4 1243
3909c3bc 1244 return nfs_ok;
1da177e4
LT
1245}
1246
b37ad28b 1247static __be32
1da177e4
LT
1248nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
1249{
0dfaf2a3 1250 __be32 status;
1da177e4 1251
0dfaf2a3
CL
1252 if (xdr_stream_decode_u64(argp->xdr, &readdir->rd_cookie) < 0)
1253 return nfserr_bad_xdr;
1254 status = nfsd4_decode_verifier4(argp, &readdir->rd_verf);
1255 if (status)
1256 return status;
1257 if (xdr_stream_decode_u32(argp->xdr, &readdir->rd_dircount) < 0)
1258 return nfserr_bad_xdr;
1259 if (xdr_stream_decode_u32(argp->xdr, &readdir->rd_maxcount) < 0)
1260 return nfserr_bad_xdr;
1261 if (xdr_stream_decode_uint32_array(argp->xdr, readdir->rd_bmval,
1262 ARRAY_SIZE(readdir->rd_bmval)) < 0)
1263 return nfserr_bad_xdr;
1da177e4 1264
0dfaf2a3 1265 return nfs_ok;
1da177e4
LT
1266}
1267
b37ad28b 1268static __be32
1da177e4
LT
1269nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
1270{
b7f5fbf2 1271 return nfsd4_decode_component4(argp, &remove->rm_name, &remove->rm_namelen);
1da177e4
LT
1272}
1273
b37ad28b 1274static __be32
1da177e4
LT
1275nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
1276{
ba881a0a 1277 __be32 status;
1da177e4 1278
ba881a0a
CL
1279 status = nfsd4_decode_component4(argp, &rename->rn_sname, &rename->rn_snamelen);
1280 if (status)
1da177e4 1281 return status;
ba881a0a 1282 return nfsd4_decode_component4(argp, &rename->rn_tname, &rename->rn_tnamelen);
1da177e4
LT
1283}
1284
b37ad28b 1285static __be32
1da177e4
LT
1286nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
1287{
d12f9045 1288 return nfsd4_decode_clientid4(argp, clientid);
1da177e4
LT
1289}
1290
dcb488a3
AA
1291static __be32
1292nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
1293 struct nfsd4_secinfo *secinfo)
1294{
d0abdae5 1295 return nfsd4_decode_component4(argp, &secinfo->si_name, &secinfo->si_namelen);
dcb488a3
AA
1296}
1297
04f4ad16
BF
1298static __be32
1299nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
1300 struct nfsd4_secinfo_no_name *sin)
1301{
1302 DECODE_HEAD;
1303
1304 READ_BUF(4);
06553991 1305 sin->sin_style = be32_to_cpup(p++);
04f4ad16
BF
1306 DECODE_TAIL;
1307}
1308
b37ad28b 1309static __be32
1da177e4
LT
1310nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
1311{
e31a1b66 1312 __be32 status;
1da177e4 1313
44592fe9 1314 status = nfsd4_decode_stateid4(argp, &setattr->sa_stateid);
e31a1b66
BH
1315 if (status)
1316 return status;
d1c263a0
CL
1317 return nfsd4_decode_fattr4(argp, setattr->sa_bmval,
1318 ARRAY_SIZE(setattr->sa_bmval),
1319 &setattr->sa_iattr, &setattr->sa_acl,
1320 &setattr->sa_label, NULL);
1da177e4
LT
1321}
1322
b37ad28b 1323static __be32
1da177e4
LT
1324nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
1325{
92fa6c08 1326 __be32 *p, status;
1da177e4 1327
e1a90ebd
AS
1328 if (argp->minorversion >= 1)
1329 return nfserr_notsupp;
1330
92fa6c08
CL
1331 status = nfsd4_decode_verifier4(argp, &setclientid->se_verf);
1332 if (status)
1333 return status;
a084daf5
BF
1334 status = nfsd4_decode_opaque(argp, &setclientid->se_name);
1335 if (status)
92fa6c08
CL
1336 return status;
1337 if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_prog) < 0)
a084daf5 1338 return nfserr_bad_xdr;
92fa6c08
CL
1339 if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_netid_len) < 0)
1340 return nfserr_bad_xdr;
1341 p = xdr_inline_decode(argp->xdr, setclientid->se_callback_netid_len);
1342 if (!p)
1343 return nfserr_bad_xdr;
1344 setclientid->se_callback_netid_val = svcxdr_tmpalloc(argp,
1345 setclientid->se_callback_netid_len);
1346 if (!setclientid->se_callback_netid_val)
1347 return nfserr_jukebox;
1348 memcpy(setclientid->se_callback_netid_val, p,
1349 setclientid->se_callback_netid_len);
1da177e4 1350
92fa6c08
CL
1351 if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_addr_len) < 0)
1352 return nfserr_bad_xdr;
1353 p = xdr_inline_decode(argp->xdr, setclientid->se_callback_addr_len);
1354 if (!p)
1355 return nfserr_bad_xdr;
1356 setclientid->se_callback_addr_val = svcxdr_tmpalloc(argp,
1357 setclientid->se_callback_addr_len);
1358 if (!setclientid->se_callback_addr_val)
1359 return nfserr_jukebox;
1360 memcpy(setclientid->se_callback_addr_val, p,
1361 setclientid->se_callback_addr_len);
1362 if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_ident) < 0)
1363 return nfserr_bad_xdr;
1da177e4 1364
92fa6c08 1365 return nfs_ok;
1da177e4
LT
1366}
1367
b37ad28b 1368static __be32
1da177e4
LT
1369nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
1370{
d1ca5514 1371 __be32 status;
1da177e4 1372
e1a90ebd
AS
1373 if (argp->minorversion >= 1)
1374 return nfserr_notsupp;
1375
d1ca5514
CL
1376 status = nfsd4_decode_clientid4(argp, &scd_c->sc_clientid);
1377 if (status)
1378 return status;
1379 return nfsd4_decode_verifier4(argp, &scd_c->sc_confirm);
1da177e4
LT
1380}
1381
1382/* Also used for NVERIFY */
b37ad28b 1383static __be32
1da177e4
LT
1384nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1385{
67cd453e 1386 __be32 *p, status;
1da177e4 1387
67cd453e
CL
1388 status = nfsd4_decode_bitmap4(argp, verify->ve_bmval,
1389 ARRAY_SIZE(verify->ve_bmval));
1390 if (status)
1391 return status;
1da177e4
LT
1392
1393 /* For convenience's sake, we compare raw xdr'd attributes in
e5f95703
BF
1394 * nfsd4_proc_verify */
1395
67cd453e
CL
1396 if (xdr_stream_decode_u32(argp->xdr, &verify->ve_attrlen) < 0)
1397 return nfserr_bad_xdr;
1398 p = xdr_inline_decode(argp->xdr, verify->ve_attrlen);
1399 if (!p)
1400 return nfserr_bad_xdr;
1401 verify->ve_attrval = svcxdr_tmpalloc(argp, verify->ve_attrlen);
1402 if (!verify->ve_attrval)
1403 return nfserr_jukebox;
1404 memcpy(verify->ve_attrval, p, verify->ve_attrlen);
1da177e4 1405
67cd453e 1406 return nfs_ok;
1da177e4
LT
1407}
1408
b37ad28b 1409static __be32
1da177e4
LT
1410nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1411{
244e2bef 1412 __be32 status;
1da177e4 1413
244e2bef 1414 status = nfsd4_decode_stateid4(argp, &write->wr_stateid);
e31a1b66
BH
1415 if (status)
1416 return status;
244e2bef
CL
1417 if (xdr_stream_decode_u64(argp->xdr, &write->wr_offset) < 0)
1418 return nfserr_bad_xdr;
1419 if (xdr_stream_decode_u32(argp->xdr, &write->wr_stable_how) < 0)
1420 return nfserr_bad_xdr;
54bbb7d2 1421 if (write->wr_stable_how > NFS_FILE_SYNC)
244e2bef
CL
1422 return nfserr_bad_xdr;
1423 if (xdr_stream_decode_u32(argp->xdr, &write->wr_buflen) < 0)
1424 return nfserr_bad_xdr;
c1346a12 1425 if (!xdr_stream_subsegment(argp->xdr, &write->wr_payload, write->wr_buflen))
244e2bef 1426 return nfserr_bad_xdr;
1da177e4 1427
244e2bef 1428 return nfs_ok;
1da177e4
LT
1429}
1430
b37ad28b 1431static __be32
1da177e4
LT
1432nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1433{
a4a80c15 1434 __be32 status;
1da177e4 1435
e1a90ebd
AS
1436 if (argp->minorversion >= 1)
1437 return nfserr_notsupp;
1438
a4a80c15
CL
1439 status = nfsd4_decode_state_owner4(argp, &rlockowner->rl_clientid,
1440 &rlockowner->rl_owner);
1441 if (status)
1442 return status;
1da177e4 1443
60adfc50
AA
1444 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1445 return nfserr_inval;
a4a80c15
CL
1446
1447 return nfs_ok;
1da177e4
LT
1448}
1449
0f81d960
CL
1450static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp, struct nfsd4_backchannel_ctl *bc)
1451{
1452 if (xdr_stream_decode_u32(argp->xdr, &bc->bc_cb_program) < 0)
1453 return nfserr_bad_xdr;
1454 return nfsd4_decode_cb_sec(argp, &bc->bc_cb_sec);
1455}
1456
571e0451
CL
1457static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
1458{
1459 u32 use_conn_in_rdma_mode;
1460 __be32 status;
1461
1462 status = nfsd4_decode_sessionid4(argp, &bcts->sessionid);
1463 if (status)
1464 return status;
1465 if (xdr_stream_decode_u32(argp->xdr, &bcts->dir) < 0)
1466 return nfserr_bad_xdr;
1467 if (xdr_stream_decode_u32(argp->xdr, &use_conn_in_rdma_mode) < 0)
1468 return nfserr_bad_xdr;
1469
1470 return nfs_ok;
1471}
1472
2548aa78
CL
1473static __be32
1474nfsd4_decode_state_protect_ops(struct nfsd4_compoundargs *argp,
1475 struct nfsd4_exchange_id *exid)
1476{
1477 __be32 status;
1478
1479 status = nfsd4_decode_bitmap4(argp, exid->spo_must_enforce,
1480 ARRAY_SIZE(exid->spo_must_enforce));
1481 if (status)
1482 return nfserr_bad_xdr;
1483 status = nfsd4_decode_bitmap4(argp, exid->spo_must_allow,
1484 ARRAY_SIZE(exid->spo_must_allow));
1485 if (status)
1486 return nfserr_bad_xdr;
1487
1488 return nfs_ok;
1489}
1490
547bfeb4
CL
1491/*
1492 * This implementation currently does not support SP4_SSV.
1493 * This decoder simply skips over these arguments.
1494 */
1495static noinline __be32
1496nfsd4_decode_ssv_sp_parms(struct nfsd4_compoundargs *argp,
1497 struct nfsd4_exchange_id *exid)
1498{
1499 u32 count, window, num_gss_handles;
1500 __be32 status;
1501
1502 /* ssp_ops */
1503 status = nfsd4_decode_state_protect_ops(argp, exid);
1504 if (status)
1505 return status;
1506
1507 /* ssp_hash_algs<> */
1508 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
1509 return nfserr_bad_xdr;
1510 while (count--) {
1511 status = nfsd4_decode_ignored_string(argp, 0);
1512 if (status)
1513 return status;
1514 }
1515
1516 /* ssp_encr_algs<> */
1517 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
1518 return nfserr_bad_xdr;
1519 while (count--) {
1520 status = nfsd4_decode_ignored_string(argp, 0);
1521 if (status)
1522 return status;
1523 }
1524
1525 if (xdr_stream_decode_u32(argp->xdr, &window) < 0)
1526 return nfserr_bad_xdr;
1527 if (xdr_stream_decode_u32(argp->xdr, &num_gss_handles) < 0)
1528 return nfserr_bad_xdr;
1529
1530 return nfs_ok;
1531}
1532
2db134eb 1533static __be32
523ec6ed
CL
1534nfsd4_decode_state_protect4_a(struct nfsd4_compoundargs *argp,
1535 struct nfsd4_exchange_id *exid)
2db134eb 1536{
523ec6ed 1537 __be32 status;
0733d213 1538
523ec6ed 1539 if (xdr_stream_decode_u32(argp->xdr, &exid->spa_how) < 0)
a084daf5 1540 return nfserr_bad_xdr;
0733d213
AA
1541 switch (exid->spa_how) {
1542 case SP4_NONE:
1543 break;
1544 case SP4_MACH_CRED:
2548aa78 1545 status = nfsd4_decode_state_protect_ops(argp, exid);
ed941643 1546 if (status)
2548aa78 1547 return status;
0733d213
AA
1548 break;
1549 case SP4_SSV:
547bfeb4 1550 status = nfsd4_decode_ssv_sp_parms(argp, exid);
2548aa78
CL
1551 if (status)
1552 return status;
0733d213
AA
1553 break;
1554 default:
523ec6ed 1555 return nfserr_bad_xdr;
0733d213
AA
1556 }
1557
523ec6ed
CL
1558 return nfs_ok;
1559}
1560
10ff8422
CL
1561static __be32
1562nfsd4_decode_nfs_impl_id4(struct nfsd4_compoundargs *argp,
1563 struct nfsd4_exchange_id *exid)
1564{
1565 __be32 status;
1566 u32 count;
1567
1568 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
1569 return nfserr_bad_xdr;
1570 switch (count) {
1571 case 0:
1572 break;
1573 case 1:
1574 /* Note that RFC 8881 places no length limit on
1575 * nii_domain, but this implementation permits no
1576 * more than NFS4_OPAQUE_LIMIT bytes */
1577 status = nfsd4_decode_opaque(argp, &exid->nii_domain);
1578 if (status)
1579 return status;
1580 /* Note that RFC 8881 places no length limit on
1581 * nii_name, but this implementation permits no
1582 * more than NFS4_OPAQUE_LIMIT bytes */
1583 status = nfsd4_decode_opaque(argp, &exid->nii_name);
1584 if (status)
1585 return status;
1586 status = nfsd4_decode_nfstime4(argp, &exid->nii_time);
1587 if (status)
1588 return status;
1589 break;
1590 default:
1591 return nfserr_bad_xdr;
1592 }
1593
1594 return nfs_ok;
1595}
1596
523ec6ed
CL
1597static __be32
1598nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
1599 struct nfsd4_exchange_id *exid)
1600{
10ff8422 1601 __be32 status;
523ec6ed
CL
1602
1603 status = nfsd4_decode_verifier4(argp, &exid->verifier);
1604 if (status)
1605 return status;
1606 status = nfsd4_decode_opaque(argp, &exid->clname);
1607 if (status)
1608 return status;
1609 if (xdr_stream_decode_u32(argp->xdr, &exid->flags) < 0)
1610 return nfserr_bad_xdr;
1611 status = nfsd4_decode_state_protect4_a(argp, exid);
1612 if (status)
1613 return status;
10ff8422 1614 return nfsd4_decode_nfs_impl_id4(argp, exid);
2db134eb
AA
1615}
1616
3a3f1fba
CL
1617static __be32
1618nfsd4_decode_channel_attrs4(struct nfsd4_compoundargs *argp,
1619 struct nfsd4_channel_attrs *ca)
1620{
1621 __be32 *p;
1622
1623 p = xdr_inline_decode(argp->xdr, XDR_UNIT * 7);
1624 if (!p)
1625 return nfserr_bad_xdr;
1626
1627 /* headerpadsz is ignored */
1628 p++;
1629 ca->maxreq_sz = be32_to_cpup(p++);
1630 ca->maxresp_sz = be32_to_cpup(p++);
1631 ca->maxresp_cached = be32_to_cpup(p++);
1632 ca->maxops = be32_to_cpup(p++);
1633 ca->maxreqs = be32_to_cpup(p++);
1634 ca->nr_rdma_attrs = be32_to_cpup(p);
1635 switch (ca->nr_rdma_attrs) {
1636 case 0:
1637 break;
1638 case 1:
1639 if (xdr_stream_decode_u32(argp->xdr, &ca->rdma_attrs) < 0)
1640 return nfserr_bad_xdr;
1641 break;
1642 default:
1643 return nfserr_bad_xdr;
1644 }
1645
1646 return nfs_ok;
1647}
1648
2db134eb
AA
1649static __be32
1650nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1651 struct nfsd4_create_session *sess)
1652{
81243e3f 1653 __be32 status;
ec6b5d7b 1654
81243e3f
CL
1655 status = nfsd4_decode_clientid4(argp, &sess->clientid);
1656 if (status)
1657 return status;
1658 if (xdr_stream_decode_u32(argp->xdr, &sess->seqid) < 0)
1659 return nfserr_bad_xdr;
1660 if (xdr_stream_decode_u32(argp->xdr, &sess->flags) < 0)
1661 return nfserr_bad_xdr;
3a3f1fba
CL
1662 status = nfsd4_decode_channel_attrs4(argp, &sess->fore_channel);
1663 if (status)
1664 return status;
1665 status = nfsd4_decode_channel_attrs4(argp, &sess->back_channel);
81243e3f
CL
1666 if (status)
1667 return status;
1668 if (xdr_stream_decode_u32(argp->xdr, &sess->callback_prog) < 0)
1669 return nfserr_bad_xdr;
1670 status = nfsd4_decode_cb_sec(argp, &sess->cb_sec);
3a3f1fba
CL
1671 if (status)
1672 return status;
ec6b5d7b 1673
81243e3f 1674 return nfs_ok;
2db134eb
AA
1675}
1676
1677static __be32
1678nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1679 struct nfsd4_destroy_session *destroy_session)
1680{
94e254af 1681 return nfsd4_decode_sessionid4(argp, &destroy_session->sessionid);
2db134eb
AA
1682}
1683
e1ca12df
BS
1684static __be32
1685nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1686 struct nfsd4_free_stateid *free_stateid)
1687{
aec387d5 1688 return nfsd4_decode_stateid4(argp, &free_stateid->fr_stateid);
e1ca12df
BS
1689}
1690
2db134eb
AA
1691static __be32
1692nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1693 struct nfsd4_sequence *seq)
1694{
b85d4c01
BH
1695 DECODE_HEAD;
1696
1697 READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
1698 COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
06553991
BF
1699 seq->seqid = be32_to_cpup(p++);
1700 seq->slotid = be32_to_cpup(p++);
1701 seq->maxslots = be32_to_cpup(p++);
1702 seq->cachethis = be32_to_cpup(p++);
b85d4c01
BH
1703
1704 DECODE_TAIL;
2db134eb
AA
1705}
1706
17456804
BS
1707static __be32
1708nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1709{
17456804 1710 int i;
03cfb420
BS
1711 __be32 *p, status;
1712 struct nfsd4_test_stateid_id *stateid;
17456804
BS
1713
1714 READ_BUF(4);
1715 test_stateid->ts_num_ids = ntohl(*p++);
1716
03cfb420 1717 INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
17456804
BS
1718
1719 for (i = 0; i < test_stateid->ts_num_ids; i++) {
d5e23383 1720 stateid = svcxdr_tmpalloc(argp, sizeof(*stateid));
03cfb420 1721 if (!stateid) {
afcf6792 1722 status = nfserrno(-ENOMEM);
03cfb420
BS
1723 goto out;
1724 }
1725
03cfb420
BS
1726 INIT_LIST_HEAD(&stateid->ts_id_list);
1727 list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
1728
1729 status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
17456804 1730 if (status)
03cfb420 1731 goto out;
17456804
BS
1732 }
1733
1734 status = 0;
1735out:
1736 return status;
1737xdr_error:
1738 dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
1739 status = nfserr_bad_xdr;
1740 goto out;
1741}
1742
345c2842
MJ
1743static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
1744{
1745 DECODE_HEAD;
1746
1747 READ_BUF(8);
1748 COPYMEM(&dc->clientid, 8);
1749
1750 DECODE_TAIL;
1751}
1752
4dc6ec00
BF
1753static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
1754{
1755 DECODE_HEAD;
1756
1757 READ_BUF(4);
06553991 1758 rc->rca_one_fs = be32_to_cpup(p++);
4dc6ec00
BF
1759
1760 DECODE_TAIL;
1761}
1762
9cf514cc
CH
1763#ifdef CONFIG_NFSD_PNFS
1764static __be32
1765nfsd4_decode_getdeviceinfo(struct nfsd4_compoundargs *argp,
1766 struct nfsd4_getdeviceinfo *gdev)
1767{
1768 DECODE_HEAD;
1769 u32 num, i;
1770
1771 READ_BUF(sizeof(struct nfsd4_deviceid) + 3 * 4);
1772 COPYMEM(&gdev->gd_devid, sizeof(struct nfsd4_deviceid));
1773 gdev->gd_layout_type = be32_to_cpup(p++);
1774 gdev->gd_maxcount = be32_to_cpup(p++);
1775 num = be32_to_cpup(p++);
1776 if (num) {
3171822f
SM
1777 if (num > 1000)
1778 goto xdr_error;
9cf514cc
CH
1779 READ_BUF(4 * num);
1780 gdev->gd_notify_types = be32_to_cpup(p++);
1781 for (i = 1; i < num; i++) {
1782 if (be32_to_cpup(p++)) {
1783 status = nfserr_inval;
1784 goto out;
1785 }
1786 }
1787 }
1788 DECODE_TAIL;
1789}
1790
1791static __be32
1792nfsd4_decode_layoutget(struct nfsd4_compoundargs *argp,
1793 struct nfsd4_layoutget *lgp)
1794{
1795 DECODE_HEAD;
1796
1797 READ_BUF(36);
1798 lgp->lg_signal = be32_to_cpup(p++);
1799 lgp->lg_layout_type = be32_to_cpup(p++);
1800 lgp->lg_seg.iomode = be32_to_cpup(p++);
1801 p = xdr_decode_hyper(p, &lgp->lg_seg.offset);
1802 p = xdr_decode_hyper(p, &lgp->lg_seg.length);
1803 p = xdr_decode_hyper(p, &lgp->lg_minlength);
db59c0ef
KM
1804
1805 status = nfsd4_decode_stateid(argp, &lgp->lg_sid);
1806 if (status)
1807 return status;
1808
9cf514cc
CH
1809 READ_BUF(4);
1810 lgp->lg_maxcount = be32_to_cpup(p++);
1811
1812 DECODE_TAIL;
1813}
1814
1815static __be32
1816nfsd4_decode_layoutcommit(struct nfsd4_compoundargs *argp,
1817 struct nfsd4_layoutcommit *lcp)
1818{
1819 DECODE_HEAD;
1820 u32 timechange;
1821
1822 READ_BUF(20);
1823 p = xdr_decode_hyper(p, &lcp->lc_seg.offset);
1824 p = xdr_decode_hyper(p, &lcp->lc_seg.length);
1825 lcp->lc_reclaim = be32_to_cpup(p++);
db59c0ef
KM
1826
1827 status = nfsd4_decode_stateid(argp, &lcp->lc_sid);
1828 if (status)
1829 return status;
1830
9cf514cc
CH
1831 READ_BUF(4);
1832 lcp->lc_newoffset = be32_to_cpup(p++);
1833 if (lcp->lc_newoffset) {
1834 READ_BUF(8);
1835 p = xdr_decode_hyper(p, &lcp->lc_last_wr);
1836 } else
1837 lcp->lc_last_wr = 0;
1838 READ_BUF(4);
1839 timechange = be32_to_cpup(p++);
1840 if (timechange) {
1841 status = nfsd4_decode_time(argp, &lcp->lc_mtime);
1842 if (status)
1843 return status;
1844 } else {
1845 lcp->lc_mtime.tv_nsec = UTIME_NOW;
1846 }
1847 READ_BUF(8);
1848 lcp->lc_layout_type = be32_to_cpup(p++);
1849
1850 /*
1851 * Save the layout update in XDR format and let the layout driver deal
1852 * with it later.
1853 */
1854 lcp->lc_up_len = be32_to_cpup(p++);
1855 if (lcp->lc_up_len > 0) {
1856 READ_BUF(lcp->lc_up_len);
1857 READMEM(lcp->lc_up_layout, lcp->lc_up_len);
1858 }
1859
1860 DECODE_TAIL;
1861}
1862
1863static __be32
1864nfsd4_decode_layoutreturn(struct nfsd4_compoundargs *argp,
1865 struct nfsd4_layoutreturn *lrp)
1866{
1867 DECODE_HEAD;
1868
1869 READ_BUF(16);
1870 lrp->lr_reclaim = be32_to_cpup(p++);
1871 lrp->lr_layout_type = be32_to_cpup(p++);
1872 lrp->lr_seg.iomode = be32_to_cpup(p++);
1873 lrp->lr_return_type = be32_to_cpup(p++);
1874 if (lrp->lr_return_type == RETURN_FILE) {
1875 READ_BUF(16);
1876 p = xdr_decode_hyper(p, &lrp->lr_seg.offset);
1877 p = xdr_decode_hyper(p, &lrp->lr_seg.length);
db59c0ef
KM
1878
1879 status = nfsd4_decode_stateid(argp, &lrp->lr_sid);
1880 if (status)
1881 return status;
1882
9cf514cc
CH
1883 READ_BUF(4);
1884 lrp->lrf_body_len = be32_to_cpup(p++);
1885 if (lrp->lrf_body_len > 0) {
1886 READ_BUF(lrp->lrf_body_len);
1887 READMEM(lrp->lrf_body, lrp->lrf_body_len);
1888 }
1889 } else {
1890 lrp->lr_seg.offset = 0;
1891 lrp->lr_seg.length = NFS4_MAX_UINT64;
1892 }
1893
1894 DECODE_TAIL;
1895}
1896#endif /* CONFIG_NFSD_PNFS */
1897
95d871f0
AS
1898static __be32
1899nfsd4_decode_fallocate(struct nfsd4_compoundargs *argp,
1900 struct nfsd4_fallocate *fallocate)
1901{
1902 DECODE_HEAD;
1903
1904 status = nfsd4_decode_stateid(argp, &fallocate->falloc_stateid);
1905 if (status)
1906 return status;
1907
1908 READ_BUF(16);
1909 p = xdr_decode_hyper(p, &fallocate->falloc_offset);
1910 xdr_decode_hyper(p, &fallocate->falloc_length);
1911
1912 DECODE_TAIL;
1913}
1914
ffa0160a
CH
1915static __be32
1916nfsd4_decode_clone(struct nfsd4_compoundargs *argp, struct nfsd4_clone *clone)
1917{
1918 DECODE_HEAD;
1919
1920 status = nfsd4_decode_stateid(argp, &clone->cl_src_stateid);
1921 if (status)
1922 return status;
1923 status = nfsd4_decode_stateid(argp, &clone->cl_dst_stateid);
1924 if (status)
1925 return status;
1926
1927 READ_BUF(8 + 8 + 8);
1928 p = xdr_decode_hyper(p, &clone->cl_src_pos);
1929 p = xdr_decode_hyper(p, &clone->cl_dst_pos);
1930 p = xdr_decode_hyper(p, &clone->cl_count);
1931 DECODE_TAIL;
1932}
1933
84e1b21d
OK
1934static __be32 nfsd4_decode_nl4_server(struct nfsd4_compoundargs *argp,
1935 struct nl4_server *ns)
1936{
1937 DECODE_HEAD;
1938 struct nfs42_netaddr *naddr;
1939
1940 READ_BUF(4);
1941 ns->nl4_type = be32_to_cpup(p++);
1942
1943 /* currently support for 1 inter-server source server */
1944 switch (ns->nl4_type) {
1945 case NL4_NETADDR:
1946 naddr = &ns->u.nl4_addr;
1947
1948 READ_BUF(4);
1949 naddr->netid_len = be32_to_cpup(p++);
1950 if (naddr->netid_len > RPCBIND_MAXNETIDLEN)
1951 goto xdr_error;
1952
1953 READ_BUF(naddr->netid_len + 4); /* 4 for uaddr len */
1954 COPYMEM(naddr->netid, naddr->netid_len);
1955
1956 naddr->addr_len = be32_to_cpup(p++);
1957 if (naddr->addr_len > RPCBIND_MAXUADDRLEN)
1958 goto xdr_error;
1959
1960 READ_BUF(naddr->addr_len);
1961 COPYMEM(naddr->addr, naddr->addr_len);
1962 break;
1963 default:
1964 goto xdr_error;
1965 }
1966 DECODE_TAIL;
1967}
1968
29ae7f9d
AS
1969static __be32
1970nfsd4_decode_copy(struct nfsd4_compoundargs *argp, struct nfsd4_copy *copy)
1971{
1972 DECODE_HEAD;
84e1b21d
OK
1973 struct nl4_server *ns_dummy;
1974 int i, count;
29ae7f9d
AS
1975
1976 status = nfsd4_decode_stateid(argp, &copy->cp_src_stateid);
1977 if (status)
1978 return status;
1979 status = nfsd4_decode_stateid(argp, &copy->cp_dst_stateid);
1980 if (status)
1981 return status;
1982
1983 READ_BUF(8 + 8 + 8 + 4 + 4 + 4);
1984 p = xdr_decode_hyper(p, &copy->cp_src_pos);
1985 p = xdr_decode_hyper(p, &copy->cp_dst_pos);
1986 p = xdr_decode_hyper(p, &copy->cp_count);
edcc8452 1987 p++; /* ca_consecutive: we always do consecutive copies */
29ae7f9d 1988 copy->cp_synchronous = be32_to_cpup(p++);
84e1b21d
OK
1989
1990 count = be32_to_cpup(p++);
1991
1992 copy->cp_intra = false;
1993 if (count == 0) { /* intra-server copy */
1994 copy->cp_intra = true;
1995 goto intra;
1996 }
1997
1998 /* decode all the supplied server addresses but use first */
1999 status = nfsd4_decode_nl4_server(argp, &copy->cp_src);
2000 if (status)
2001 return status;
2002
2003 ns_dummy = kmalloc(sizeof(struct nl4_server), GFP_KERNEL);
2004 if (ns_dummy == NULL)
2005 return nfserrno(-ENOMEM);
2006 for (i = 0; i < count - 1; i++) {
2007 status = nfsd4_decode_nl4_server(argp, ns_dummy);
2008 if (status) {
2009 kfree(ns_dummy);
2010 return status;
2011 }
2012 }
2013 kfree(ns_dummy);
2014intra:
29ae7f9d
AS
2015
2016 DECODE_TAIL;
2017}
2018
6308bc98
OK
2019static __be32
2020nfsd4_decode_offload_status(struct nfsd4_compoundargs *argp,
2021 struct nfsd4_offload_status *os)
2022{
2023 return nfsd4_decode_stateid(argp, &os->stateid);
2024}
2025
51911868
OK
2026static __be32
2027nfsd4_decode_copy_notify(struct nfsd4_compoundargs *argp,
2028 struct nfsd4_copy_notify *cn)
2029{
5aff7d08 2030 __be32 status;
51911868
OK
2031
2032 status = nfsd4_decode_stateid(argp, &cn->cpn_src_stateid);
2033 if (status)
2034 return status;
2035 return nfsd4_decode_nl4_server(argp, &cn->cpn_dst);
2036}
2037
24bab491
AS
2038static __be32
2039nfsd4_decode_seek(struct nfsd4_compoundargs *argp, struct nfsd4_seek *seek)
2040{
2041 DECODE_HEAD;
2042
2043 status = nfsd4_decode_stateid(argp, &seek->seek_stateid);
2044 if (status)
2045 return status;
2046
2047 READ_BUF(8 + 4);
2048 p = xdr_decode_hyper(p, &seek->seek_offset);
2049 seek->seek_whence = be32_to_cpup(p);
2050
2051 DECODE_TAIL;
2052}
2053
23e50fe3
FL
2054/*
2055 * XDR data that is more than PAGE_SIZE in size is normally part of a
2056 * read or write. However, the size of extended attributes is limited
2057 * by the maximum request size, and then further limited by the underlying
2058 * filesystem limits. This can exceed PAGE_SIZE (currently, XATTR_SIZE_MAX
2059 * is 64k). Since there is no kvec- or page-based interface to xattrs,
2060 * and we're not dealing with contiguous pages, we need to do some copying.
2061 */
2062
2063/*
c1346a12 2064 * Decode data into buffer.
23e50fe3
FL
2065 */
2066static __be32
c1346a12
CL
2067nfsd4_vbuf_from_vector(struct nfsd4_compoundargs *argp, struct xdr_buf *xdr,
2068 char **bufp, u32 buflen)
23e50fe3 2069{
c1346a12
CL
2070 struct page **pages = xdr->pages;
2071 struct kvec *head = xdr->head;
23e50fe3
FL
2072 char *tmp, *dp;
2073 u32 len;
2074
2075 if (buflen <= head->iov_len) {
2076 /*
2077 * We're in luck, the head has enough space. Just return
2078 * the head, no need for copying.
2079 */
2080 *bufp = head->iov_base;
2081 return 0;
2082 }
2083
2084 tmp = svcxdr_tmpalloc(argp, buflen);
2085 if (tmp == NULL)
2086 return nfserr_jukebox;
2087
2088 dp = tmp;
2089 memcpy(dp, head->iov_base, head->iov_len);
2090 buflen -= head->iov_len;
2091 dp += head->iov_len;
2092
2093 while (buflen > 0) {
2094 len = min_t(u32, buflen, PAGE_SIZE);
2095 memcpy(dp, page_address(*pages), len);
2096
2097 buflen -= len;
2098 dp += len;
2099 pages++;
2100 }
2101
2102 *bufp = tmp;
2103 return 0;
2104}
2105
2106/*
2107 * Get a user extended attribute name from the XDR buffer.
2108 * It will not have the "user." prefix, so prepend it.
2109 * Lastly, check for nul characters in the name.
2110 */
2111static __be32
2112nfsd4_decode_xattr_name(struct nfsd4_compoundargs *argp, char **namep)
2113{
2114 DECODE_HEAD;
2115 char *name, *sp, *dp;
2116 u32 namelen, cnt;
2117
2118 READ_BUF(4);
2119 namelen = be32_to_cpup(p++);
2120
2121 if (namelen > (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN))
2122 return nfserr_nametoolong;
2123
2124 if (namelen == 0)
2125 goto xdr_error;
2126
2127 READ_BUF(namelen);
2128
2129 name = svcxdr_tmpalloc(argp, namelen + XATTR_USER_PREFIX_LEN + 1);
2130 if (!name)
2131 return nfserr_jukebox;
2132
2133 memcpy(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
2134
2135 /*
2136 * Copy the extended attribute name over while checking for 0
2137 * characters.
2138 */
2139 sp = (char *)p;
2140 dp = name + XATTR_USER_PREFIX_LEN;
2141 cnt = namelen;
2142
2143 while (cnt-- > 0) {
2144 if (*sp == '\0')
2145 goto xdr_error;
2146 *dp++ = *sp++;
2147 }
2148 *dp = '\0';
2149
2150 *namep = name;
2151
2152 DECODE_TAIL;
2153}
2154
2155/*
2156 * A GETXATTR op request comes without a length specifier. We just set the
2157 * maximum length for the reply based on XATTR_SIZE_MAX and the maximum
2158 * channel reply size. nfsd_getxattr will probe the length of the xattr,
2159 * check it against getxa_len, and allocate + return the value.
2160 */
2161static __be32
2162nfsd4_decode_getxattr(struct nfsd4_compoundargs *argp,
2163 struct nfsd4_getxattr *getxattr)
2164{
2165 __be32 status;
2166 u32 maxcount;
2167
2168 status = nfsd4_decode_xattr_name(argp, &getxattr->getxa_name);
2169 if (status)
2170 return status;
2171
2172 maxcount = svc_max_payload(argp->rqstp);
2173 maxcount = min_t(u32, XATTR_SIZE_MAX, maxcount);
2174
2175 getxattr->getxa_len = maxcount;
2176
2177 return status;
2178}
2179
2180static __be32
2181nfsd4_decode_setxattr(struct nfsd4_compoundargs *argp,
2182 struct nfsd4_setxattr *setxattr)
2183{
2184 DECODE_HEAD;
2185 u32 flags, maxcount, size;
23e50fe3
FL
2186
2187 READ_BUF(4);
2188 flags = be32_to_cpup(p++);
2189
2190 if (flags > SETXATTR4_REPLACE)
2191 return nfserr_inval;
2192 setxattr->setxa_flags = flags;
2193
2194 status = nfsd4_decode_xattr_name(argp, &setxattr->setxa_name);
2195 if (status)
2196 return status;
2197
2198 maxcount = svc_max_payload(argp->rqstp);
2199 maxcount = min_t(u32, XATTR_SIZE_MAX, maxcount);
2200
2201 READ_BUF(4);
2202 size = be32_to_cpup(p++);
2203 if (size > maxcount)
2204 return nfserr_xattr2big;
2205
2206 setxattr->setxa_len = size;
2207 if (size > 0) {
c1346a12 2208 struct xdr_buf payload;
23e50fe3 2209
c1346a12
CL
2210 if (!xdr_stream_subsegment(argp->xdr, &payload, size))
2211 goto xdr_error;
2212 status = nfsd4_vbuf_from_vector(argp, &payload,
2213 &setxattr->setxa_buf, size);
23e50fe3
FL
2214 }
2215
2216 DECODE_TAIL;
2217}
2218
2219static __be32
2220nfsd4_decode_listxattrs(struct nfsd4_compoundargs *argp,
2221 struct nfsd4_listxattrs *listxattrs)
2222{
2223 DECODE_HEAD;
2224 u32 maxcount;
2225
2226 READ_BUF(12);
2227 p = xdr_decode_hyper(p, &listxattrs->lsxa_cookie);
2228
2229 /*
2230 * If the cookie is too large to have even one user.x attribute
2231 * plus trailing '\0' left in a maximum size buffer, it's invalid.
2232 */
2233 if (listxattrs->lsxa_cookie >=
2234 (XATTR_LIST_MAX / (XATTR_USER_PREFIX_LEN + 2)))
2235 return nfserr_badcookie;
2236
2237 maxcount = be32_to_cpup(p++);
2238 if (maxcount < 8)
2239 /* Always need at least 2 words (length and one character) */
2240 return nfserr_inval;
2241
2242 maxcount = min(maxcount, svc_max_payload(argp->rqstp));
2243 listxattrs->lsxa_maxcount = maxcount;
2244
2245 DECODE_TAIL;
2246}
2247
2248static __be32
2249nfsd4_decode_removexattr(struct nfsd4_compoundargs *argp,
2250 struct nfsd4_removexattr *removexattr)
2251{
2252 return nfsd4_decode_xattr_name(argp, &removexattr->rmxa_name);
2253}
2254
347e0ad9
BH
2255static __be32
2256nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
2257{
2258 return nfs_ok;
2259}
2260
3c375c6f
BH
2261static __be32
2262nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
2263{
1e685ec2 2264 return nfserr_notsupp;
3c375c6f
BH
2265}
2266
347e0ad9
BH
2267typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
2268
c1df609d 2269static const nfsd4_dec nfsd4_dec_ops[] = {
ad1060c8
BF
2270 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
2271 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
2272 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
2273 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
2274 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
2275 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
2276 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
2277 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
2278 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
2279 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
2280 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
2281 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
2282 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
2283 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
2284 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
2285 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
2286 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
2287 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
2288 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
2289 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
e1a90ebd 2290 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_putpubfh,
ad1060c8
BF
2291 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
2292 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
2293 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
2294 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
2295 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
2296 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
2297 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
2298 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
2299 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
2300 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
2301 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
2302 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
2303 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
2304 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
2305 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
2306 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
2db134eb
AA
2307
2308 /* new operations for NFSv4.1 */
cb73a9f4 2309 [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
1d1bc8f2 2310 [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
9064caae
RD
2311 [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
2312 [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
2313 [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
e1ca12df 2314 [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
9064caae 2315 [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
9cf514cc
CH
2316#ifdef CONFIG_NFSD_PNFS
2317 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_getdeviceinfo,
2318 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
2319 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_layoutcommit,
2320 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_layoutget,
2321 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_layoutreturn,
2322#else
9064caae
RD
2323 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
2324 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
2325 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
2326 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
2327 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
9cf514cc 2328#endif
04f4ad16 2329 [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
9064caae
RD
2330 [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
2331 [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
17456804 2332 [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
9064caae 2333 [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
345c2842 2334 [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
4dc6ec00 2335 [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
87a15a80
AS
2336
2337 /* new operations for NFSv4.2 */
95d871f0 2338 [OP_ALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
29ae7f9d 2339 [OP_COPY] = (nfsd4_dec)nfsd4_decode_copy,
51911868 2340 [OP_COPY_NOTIFY] = (nfsd4_dec)nfsd4_decode_copy_notify,
b0cb9085 2341 [OP_DEALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
87a15a80
AS
2342 [OP_IO_ADVISE] = (nfsd4_dec)nfsd4_decode_notsupp,
2343 [OP_LAYOUTERROR] = (nfsd4_dec)nfsd4_decode_notsupp,
2344 [OP_LAYOUTSTATS] = (nfsd4_dec)nfsd4_decode_notsupp,
885e2bf3 2345 [OP_OFFLOAD_CANCEL] = (nfsd4_dec)nfsd4_decode_offload_status,
6308bc98 2346 [OP_OFFLOAD_STATUS] = (nfsd4_dec)nfsd4_decode_offload_status,
528b8493 2347 [OP_READ_PLUS] = (nfsd4_dec)nfsd4_decode_read,
24bab491 2348 [OP_SEEK] = (nfsd4_dec)nfsd4_decode_seek,
87a15a80 2349 [OP_WRITE_SAME] = (nfsd4_dec)nfsd4_decode_notsupp,
ffa0160a 2350 [OP_CLONE] = (nfsd4_dec)nfsd4_decode_clone,
23e50fe3
FL
2351 /* RFC 8276 extended atributes operations */
2352 [OP_GETXATTR] = (nfsd4_dec)nfsd4_decode_getxattr,
2353 [OP_SETXATTR] = (nfsd4_dec)nfsd4_decode_setxattr,
2354 [OP_LISTXATTRS] = (nfsd4_dec)nfsd4_decode_listxattrs,
2355 [OP_REMOVEXATTR] = (nfsd4_dec)nfsd4_decode_removexattr,
2db134eb
AA
2356};
2357
e1a90ebd
AS
2358static inline bool
2359nfsd4_opnum_in_range(struct nfsd4_compoundargs *argp, struct nfsd4_op *op)
2360{
8217d146 2361 if (op->opnum < FIRST_NFS4_OP)
e1a90ebd 2362 return false;
8217d146 2363 else if (argp->minorversion == 0 && op->opnum > LAST_NFS40_OP)
e1a90ebd 2364 return false;
8217d146
AS
2365 else if (argp->minorversion == 1 && op->opnum > LAST_NFS41_OP)
2366 return false;
2367 else if (argp->minorversion == 2 && op->opnum > LAST_NFS42_OP)
e1a90ebd
AS
2368 return false;
2369 return true;
2370}
f2feb96b 2371
b37ad28b 2372static __be32
1da177e4
LT
2373nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
2374{
2375 DECODE_HEAD;
2376 struct nfsd4_op *op;
1091006c 2377 bool cachethis = false;
a5cddc88
BF
2378 int auth_slack= argp->rqstp->rq_auth_slack;
2379 int max_reply = auth_slack + 8; /* opcnt, status */
b0e35fda
BF
2380 int readcount = 0;
2381 int readbytes = 0;
1da177e4
LT
2382 int i;
2383
1da177e4 2384 READ_BUF(4);
06553991 2385 argp->taglen = be32_to_cpup(p++);
4aed9c46 2386 READ_BUF(argp->taglen);
1da177e4 2387 SAVEMEM(argp->tag, argp->taglen);
4aed9c46 2388 READ_BUF(8);
06553991
BF
2389 argp->minorversion = be32_to_cpup(p++);
2390 argp->opcnt = be32_to_cpup(p++);
4f0cefbf 2391 max_reply += 4 + (XDR_QUADLEN(argp->taglen) << 2);
1da177e4
LT
2392
2393 if (argp->taglen > NFSD4_MAX_TAGLEN)
2394 goto xdr_error;
0078117c
BF
2395 /*
2396 * NFS4ERR_RESOURCE is a more helpful error than GARBAGE_ARGS
2397 * here, so we return success at the xdr level so that
2398 * nfsd4_proc can handle this is an NFS-level error.
2399 */
2400 if (argp->opcnt > NFSD_MAX_OPS_PER_COMPOUND)
2401 return 0;
1da177e4 2402
e8c96f8c 2403 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
5d6031ca 2404 argp->ops = kzalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
1da177e4
LT
2405 if (!argp->ops) {
2406 argp->ops = argp->iops;
817cb9d4 2407 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
1da177e4
LT
2408 goto xdr_error;
2409 }
2410 }
2411
e1a90ebd 2412 if (argp->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
30cff1ff
BH
2413 argp->opcnt = 0;
2414
1da177e4
LT
2415 for (i = 0; i < argp->opcnt; i++) {
2416 op = &argp->ops[i];
2417 op->replay = NULL;
2418
8a61b18c 2419 READ_BUF(4);
06553991 2420 op->opnum = be32_to_cpup(p++);
1da177e4 2421
08281341 2422 if (nfsd4_opnum_in_range(argp, op)) {
e1a90ebd 2423 op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
08281341
CL
2424 if (op->status != nfs_ok)
2425 trace_nfsd_compound_decode_err(argp->rqstp,
2426 argp->opcnt, i,
2427 op->opnum,
2428 op->status);
2429 } else {
1da177e4
LT
2430 op->opnum = OP_ILLEGAL;
2431 op->status = nfserr_op_illegal;
1da177e4 2432 }
f4f9ef4a 2433 op->opdesc = OPDESC(op);
1091006c
BF
2434 /*
2435 * We'll try to cache the result in the DRC if any one
2436 * op in the compound wants to be cached:
2437 */
2438 cachethis |= nfsd4_cache_this_op(op);
6ff40dec 2439
528b8493 2440 if (op->opnum == OP_READ || op->opnum == OP_READ_PLUS) {
b0e35fda
BF
2441 readcount++;
2442 readbytes += nfsd4_max_reply(argp->rqstp, op);
2443 } else
2444 max_reply += nfsd4_max_reply(argp->rqstp, op);
f7b43d0c 2445 /*
7323f0d2
KM
2446 * OP_LOCK and OP_LOCKT may return a conflicting lock.
2447 * (Special case because it will just skip encoding this
2448 * if it runs out of xdr buffer space, and it is the only
2449 * operation that behaves this way.)
f7b43d0c 2450 */
7323f0d2 2451 if (op->opnum == OP_LOCK || op->opnum == OP_LOCKT)
f7b43d0c 2452 max_reply += NFS4_OPAQUE_LIMIT;
e372ba60
BF
2453
2454 if (op->status) {
2455 argp->opcnt = i+1;
2456 break;
2457 }
1da177e4 2458 }
1091006c
BF
2459 /* Sessions make the DRC unnecessary: */
2460 if (argp->minorversion)
2461 cachethis = false;
b0e35fda 2462 svc_reserve(argp->rqstp, max_reply + readbytes);
1091006c 2463 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
1da177e4 2464
a5cddc88 2465 if (readcount > 1 || max_reply > PAGE_SIZE - auth_slack)
779fb0f3 2466 clear_bit(RQ_SPLICE_OK, &argp->rqstp->rq_flags);
b0e35fda 2467
1da177e4
LT
2468 DECODE_TAIL;
2469}
1da177e4 2470
b8800921
N
2471static __be32 *encode_change(__be32 *p, struct kstat *stat, struct inode *inode,
2472 struct svc_export *exp)
c654b8a9 2473{
b8800921
N
2474 if (exp->ex_flags & NFSEXP_V4ROOT) {
2475 *p++ = cpu_to_be32(convert_to_wallclock(exp->cd->flush_time));
2476 *p++ = 0;
2477 } else if (IS_I_VERSION(inode)) {
39ca1bf6 2478 p = xdr_encode_hyper(p, nfsd4_change_attribute(stat, inode));
c654b8a9 2479 } else {
d05d5744
BF
2480 *p++ = cpu_to_be32(stat->ctime.tv_sec);
2481 *p++ = cpu_to_be32(stat->ctime.tv_nsec);
c654b8a9 2482 }
d05d5744 2483 return p;
c654b8a9
BF
2484}
2485
16945141
BF
2486/*
2487 * ctime (in NFSv4, time_metadata) is not writeable, and the client
2488 * doesn't really care what resolution could theoretically be stored by
2489 * the filesystem.
2490 *
2491 * The client cares how close together changes can be while still
2492 * guaranteeing ctime changes. For most filesystems (which have
2493 * timestamps with nanosecond fields) that is limited by the resolution
2494 * of the time returned from current_time() (which I'm assuming to be
2495 * 1/HZ).
2496 */
2497static __be32 *encode_time_delta(__be32 *p, struct inode *inode)
2498{
e4598e38 2499 struct timespec64 ts;
16945141
BF
2500 u32 ns;
2501
2502 ns = max_t(u32, NSEC_PER_SEC/HZ, inode->i_sb->s_time_gran);
e4598e38 2503 ts = ns_to_timespec64(ns);
16945141
BF
2504
2505 p = xdr_encode_hyper(p, ts.tv_sec);
2506 *p++ = cpu_to_be32(ts.tv_nsec);
2507
2508 return p;
2509}
2510
d05d5744 2511static __be32 *encode_cinfo(__be32 *p, struct nfsd4_change_info *c)
c654b8a9 2512{
d05d5744 2513 *p++ = cpu_to_be32(c->atomic);
c654b8a9 2514 if (c->change_supported) {
d05d5744
BF
2515 p = xdr_encode_hyper(p, c->before_change);
2516 p = xdr_encode_hyper(p, c->after_change);
c654b8a9 2517 } else {
d05d5744
BF
2518 *p++ = cpu_to_be32(c->before_ctime_sec);
2519 *p++ = cpu_to_be32(c->before_ctime_nsec);
2520 *p++ = cpu_to_be32(c->after_ctime_sec);
2521 *p++ = cpu_to_be32(c->after_ctime_nsec);
c654b8a9 2522 }
d05d5744 2523 return p;
c654b8a9 2524}
1da177e4 2525
81c3f413 2526/* Encode as an array of strings the string given with components
e7a0444a 2527 * separated @sep, escaped with esc_enter and esc_exit.
81c3f413 2528 */
ddd1ea56
BF
2529static __be32 nfsd4_encode_components_esc(struct xdr_stream *xdr, char sep,
2530 char *components, char esc_enter,
2531 char esc_exit)
81c3f413 2532{
ddd1ea56 2533 __be32 *p;
082d4bd7
BF
2534 __be32 pathlen;
2535 int pathlen_offset;
81c3f413 2536 int strlen, count=0;
e7a0444a 2537 char *str, *end, *next;
81c3f413
BF
2538
2539 dprintk("nfsd4_encode_components(%s)\n", components);
082d4bd7
BF
2540
2541 pathlen_offset = xdr->buf->len;
ddd1ea56
BF
2542 p = xdr_reserve_space(xdr, 4);
2543 if (!p)
81c3f413 2544 return nfserr_resource;
082d4bd7
BF
2545 p++; /* We will fill this in with @count later */
2546
81c3f413
BF
2547 end = str = components;
2548 while (*end) {
e7a0444a
WAA
2549 bool found_esc = false;
2550
2551 /* try to parse as esc_start, ..., esc_end, sep */
2552 if (*str == esc_enter) {
2553 for (; *end && (*end != esc_exit); end++)
2554 /* find esc_exit or end of string */;
2555 next = end + 1;
2556 if (*end && (!*next || *next == sep)) {
2557 str++;
2558 found_esc = true;
2559 }
2560 }
2561
2562 if (!found_esc)
2563 for (; *end && (*end != sep); end++)
2564 /* find sep or end of string */;
2565
81c3f413
BF
2566 strlen = end - str;
2567 if (strlen) {
ddd1ea56
BF
2568 p = xdr_reserve_space(xdr, strlen + 4);
2569 if (!p)
81c3f413 2570 return nfserr_resource;
0c0c267b 2571 p = xdr_encode_opaque(p, str, strlen);
81c3f413
BF
2572 count++;
2573 }
2574 else
2575 end++;
5a64e569
BC
2576 if (found_esc)
2577 end = next;
2578
81c3f413
BF
2579 str = end;
2580 }
bf7491f1 2581 pathlen = htonl(count);
082d4bd7 2582 write_bytes_to_xdr_buf(xdr->buf, pathlen_offset, &pathlen, 4);
81c3f413
BF
2583 return 0;
2584}
2585
e7a0444a
WAA
2586/* Encode as an array of strings the string given with components
2587 * separated @sep.
2588 */
ddd1ea56
BF
2589static __be32 nfsd4_encode_components(struct xdr_stream *xdr, char sep,
2590 char *components)
e7a0444a 2591{
ddd1ea56 2592 return nfsd4_encode_components_esc(xdr, sep, components, 0, 0);
e7a0444a
WAA
2593}
2594
81c3f413
BF
2595/*
2596 * encode a location element of a fs_locations structure
2597 */
ddd1ea56
BF
2598static __be32 nfsd4_encode_fs_location4(struct xdr_stream *xdr,
2599 struct nfsd4_fs_location *location)
81c3f413 2600{
b37ad28b 2601 __be32 status;
81c3f413 2602
ddd1ea56 2603 status = nfsd4_encode_components_esc(xdr, ':', location->hosts,
e7a0444a 2604 '[', ']');
81c3f413
BF
2605 if (status)
2606 return status;
ddd1ea56 2607 status = nfsd4_encode_components(xdr, '/', location->path);
81c3f413
BF
2608 if (status)
2609 return status;
81c3f413
BF
2610 return 0;
2611}
2612
2613/*
ed748aac 2614 * Encode a path in RFC3530 'pathname4' format
81c3f413 2615 */
ddd1ea56
BF
2616static __be32 nfsd4_encode_path(struct xdr_stream *xdr,
2617 const struct path *root,
2618 const struct path *path)
81c3f413 2619{
301f0268 2620 struct path cur = *path;
ddd1ea56 2621 __be32 *p;
ed748aac
TM
2622 struct dentry **components = NULL;
2623 unsigned int ncomponents = 0;
2624 __be32 err = nfserr_jukebox;
81c3f413 2625
ed748aac 2626 dprintk("nfsd4_encode_components(");
81c3f413 2627
ed748aac
TM
2628 path_get(&cur);
2629 /* First walk the path up to the nfsd root, and store the
2630 * dentries/path components in an array.
2631 */
2632 for (;;) {
b77a4b2e 2633 if (path_equal(&cur, root))
ed748aac
TM
2634 break;
2635 if (cur.dentry == cur.mnt->mnt_root) {
2636 if (follow_up(&cur))
2637 continue;
2638 goto out_free;
2639 }
2640 if ((ncomponents & 15) == 0) {
2641 struct dentry **new;
2642 new = krealloc(components,
2643 sizeof(*new) * (ncomponents + 16),
2644 GFP_KERNEL);
2645 if (!new)
2646 goto out_free;
2647 components = new;
2648 }
2649 components[ncomponents++] = cur.dentry;
2650 cur.dentry = dget_parent(cur.dentry);
2651 }
ddd1ea56
BF
2652 err = nfserr_resource;
2653 p = xdr_reserve_space(xdr, 4);
2654 if (!p)
ed748aac 2655 goto out_free;
c373b0a4 2656 *p++ = cpu_to_be32(ncomponents);
ed748aac
TM
2657
2658 while (ncomponents) {
2659 struct dentry *dentry = components[ncomponents - 1];
301f0268 2660 unsigned int len;
ed748aac 2661
301f0268
AV
2662 spin_lock(&dentry->d_lock);
2663 len = dentry->d_name.len;
ddd1ea56
BF
2664 p = xdr_reserve_space(xdr, len + 4);
2665 if (!p) {
301f0268 2666 spin_unlock(&dentry->d_lock);
ed748aac 2667 goto out_free;
301f0268 2668 }
0c0c267b 2669 p = xdr_encode_opaque(p, dentry->d_name.name, len);
a455589f 2670 dprintk("/%pd", dentry);
301f0268 2671 spin_unlock(&dentry->d_lock);
ed748aac
TM
2672 dput(dentry);
2673 ncomponents--;
81c3f413 2674 }
ed748aac 2675
ed748aac
TM
2676 err = 0;
2677out_free:
2678 dprintk(")\n");
2679 while (ncomponents)
2680 dput(components[--ncomponents]);
2681 kfree(components);
2682 path_put(&cur);
2683 return err;
2684}
2685
ddd1ea56
BF
2686static __be32 nfsd4_encode_fsloc_fsroot(struct xdr_stream *xdr,
2687 struct svc_rqst *rqstp, const struct path *path)
ed748aac
TM
2688{
2689 struct svc_export *exp_ps;
2690 __be32 res;
2691
2692 exp_ps = rqst_find_fsidzero_export(rqstp);
2693 if (IS_ERR(exp_ps))
2694 return nfserrno(PTR_ERR(exp_ps));
ddd1ea56 2695 res = nfsd4_encode_path(xdr, &exp_ps->ex_path, path);
ed748aac
TM
2696 exp_put(exp_ps);
2697 return res;
81c3f413
BF
2698}
2699
2700/*
2701 * encode a fs_locations structure
2702 */
ddd1ea56
BF
2703static __be32 nfsd4_encode_fs_locations(struct xdr_stream *xdr,
2704 struct svc_rqst *rqstp, struct svc_export *exp)
81c3f413 2705{
b37ad28b 2706 __be32 status;
cc45f017 2707 int i;
ddd1ea56 2708 __be32 *p;
81c3f413 2709 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
81c3f413 2710
ddd1ea56 2711 status = nfsd4_encode_fsloc_fsroot(xdr, rqstp, &exp->ex_path);
81c3f413
BF
2712 if (status)
2713 return status;
ddd1ea56
BF
2714 p = xdr_reserve_space(xdr, 4);
2715 if (!p)
81c3f413 2716 return nfserr_resource;
c373b0a4 2717 *p++ = cpu_to_be32(fslocs->locations_count);
81c3f413 2718 for (i=0; i<fslocs->locations_count; i++) {
ddd1ea56 2719 status = nfsd4_encode_fs_location4(xdr, &fslocs->locations[i]);
81c3f413
BF
2720 if (status)
2721 return status;
2722 }
81c3f413
BF
2723 return 0;
2724}
1da177e4 2725
3d2544b1
BF
2726static u32 nfs4_file_type(umode_t mode)
2727{
2728 switch (mode & S_IFMT) {
2729 case S_IFIFO: return NF4FIFO;
2730 case S_IFCHR: return NF4CHR;
2731 case S_IFDIR: return NF4DIR;
2732 case S_IFBLK: return NF4BLK;
2733 case S_IFLNK: return NF4LNK;
2734 case S_IFREG: return NF4REG;
2735 case S_IFSOCK: return NF4SOCK;
2736 default: return NF4BAD;
25fef48b 2737 }
3d2544b1 2738}
1da177e4 2739
b37ad28b 2740static inline __be32
ddd1ea56
BF
2741nfsd4_encode_aclname(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2742 struct nfs4_ace *ace)
1da177e4 2743{
3554116d 2744 if (ace->whotype != NFS4_ACL_WHO_NAMED)
ddd1ea56 2745 return nfs4_acl_write_who(xdr, ace->whotype);
3554116d 2746 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
ddd1ea56 2747 return nfsd4_encode_group(xdr, rqstp, ace->who_gid);
3554116d 2748 else
ddd1ea56 2749 return nfsd4_encode_user(xdr, rqstp, ace->who_uid);
1da177e4
LT
2750}
2751
6896f15a 2752static inline __be32
8a4c3926 2753nfsd4_encode_layout_types(struct xdr_stream *xdr, u32 layout_types)
6896f15a 2754{
8a4c3926
JL
2755 __be32 *p;
2756 unsigned long i = hweight_long(layout_types);
6896f15a 2757
8a4c3926
JL
2758 p = xdr_reserve_space(xdr, 4 + 4 * i);
2759 if (!p)
2760 return nfserr_resource;
2761
2762 *p++ = cpu_to_be32(i);
2763
2764 for (i = LAYOUT_NFSV4_1_FILES; i < LAYOUT_TYPE_MAX; ++i)
2765 if (layout_types & (1 << i))
2766 *p++ = cpu_to_be32(i);
6896f15a
KM
2767
2768 return 0;
2769}
2770
42ca0993
BF
2771#define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
2772 FATTR4_WORD0_RDATTR_ERROR)
2773#define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
c2227a39 2774#define WORD2_ABSENT_FS_ATTRS 0
42ca0993 2775
18032ca0
DQ
2776#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2777static inline __be32
ddd1ea56
BF
2778nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2779 void *context, int len)
18032ca0 2780{
ddd1ea56 2781 __be32 *p;
18032ca0 2782
ddd1ea56
BF
2783 p = xdr_reserve_space(xdr, len + 4 + 4 + 4);
2784 if (!p)
18032ca0
DQ
2785 return nfserr_resource;
2786
2787 /*
2788 * For now we use a 0 here to indicate the null translation; in
2789 * the future we may place a call to translation code here.
2790 */
c373b0a4
BF
2791 *p++ = cpu_to_be32(0); /* lfs */
2792 *p++ = cpu_to_be32(0); /* pi */
18032ca0 2793 p = xdr_encode_opaque(p, context, len);
18032ca0
DQ
2794 return 0;
2795}
2796#else
2797static inline __be32
ddd1ea56
BF
2798nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2799 void *context, int len)
18032ca0
DQ
2800{ return 0; }
2801#endif
2802
c2227a39 2803static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *bmval2, u32 *rdattr_err)
42ca0993
BF
2804{
2805 /* As per referral draft: */
2806 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
2807 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
2808 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
2809 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
2810 *rdattr_err = NFSERR_MOVED;
2811 else
2812 return nfserr_moved;
2813 }
2814 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
2815 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
c2227a39 2816 *bmval2 &= WORD2_ABSENT_FS_ATTRS;
42ca0993
BF
2817 return 0;
2818}
1da177e4 2819
ae7095a7
BF
2820
2821static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
2822{
2823 struct path path = exp->ex_path;
2824 int err;
2825
2826 path_get(&path);
2827 while (follow_up(&path)) {
2828 if (path.dentry != path.mnt->mnt_root)
2829 break;
2830 }
a528d35e 2831 err = vfs_getattr(&path, stat, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
ae7095a7
BF
2832 path_put(&path);
2833 return err;
2834}
2835
75976de6
KM
2836static __be32
2837nfsd4_encode_bitmap(struct xdr_stream *xdr, u32 bmval0, u32 bmval1, u32 bmval2)
2838{
2839 __be32 *p;
2840
2841 if (bmval2) {
2842 p = xdr_reserve_space(xdr, 16);
2843 if (!p)
2844 goto out_resource;
2845 *p++ = cpu_to_be32(3);
2846 *p++ = cpu_to_be32(bmval0);
2847 *p++ = cpu_to_be32(bmval1);
2848 *p++ = cpu_to_be32(bmval2);
2849 } else if (bmval1) {
2850 p = xdr_reserve_space(xdr, 12);
2851 if (!p)
2852 goto out_resource;
2853 *p++ = cpu_to_be32(2);
2854 *p++ = cpu_to_be32(bmval0);
2855 *p++ = cpu_to_be32(bmval1);
2856 } else {
2857 p = xdr_reserve_space(xdr, 8);
2858 if (!p)
2859 goto out_resource;
2860 *p++ = cpu_to_be32(1);
2861 *p++ = cpu_to_be32(bmval0);
2862 }
2863
2864 return 0;
2865out_resource:
2866 return nfserr_resource;
2867}
2868
1da177e4
LT
2869/*
2870 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
2871 * ourselves.
1da177e4 2872 */
da2ebce6 2873static __be32
d5184658
BF
2874nfsd4_encode_fattr(struct xdr_stream *xdr, struct svc_fh *fhp,
2875 struct svc_export *exp,
2876 struct dentry *dentry, u32 *bmval,
406a7ea9 2877 struct svc_rqst *rqstp, int ignore_crossmnt)
1da177e4
LT
2878{
2879 u32 bmval0 = bmval[0];
2880 u32 bmval1 = bmval[1];
7e705706 2881 u32 bmval2 = bmval[2];
1da177e4 2882 struct kstat stat;
d50e6136 2883 struct svc_fh *tempfh = NULL;
1da177e4 2884 struct kstatfs statfs;
ddd1ea56 2885 __be32 *p;
1fcea5b2 2886 int starting_len = xdr->buf->len;
082d4bd7
BF
2887 int attrlen_offset;
2888 __be32 attrlen;
1da177e4
LT
2889 u32 dummy;
2890 u64 dummy64;
42ca0993 2891 u32 rdattr_err = 0;
b37ad28b 2892 __be32 status;
b8dd7b9a 2893 int err;
1da177e4 2894 struct nfs4_acl *acl = NULL;
0ab88ca4 2895#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
18032ca0
DQ
2896 void *context = NULL;
2897 int contextlen;
0ab88ca4 2898#endif
18032ca0 2899 bool contextsupport = false;
7e705706
AA
2900 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2901 u32 minorversion = resp->cstate.minorversion;
ebabe9a9
CH
2902 struct path path = {
2903 .mnt = exp->ex_path.mnt,
2904 .dentry = dentry,
2905 };
3d733711 2906 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4
LT
2907
2908 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
916d2d84 2909 BUG_ON(!nfsd_attrs_supported(minorversion, bmval));
1da177e4 2910
42ca0993 2911 if (exp->ex_fslocs.migrated) {
c2227a39 2912 status = fattr_handle_absent_fs(&bmval0, &bmval1, &bmval2, &rdattr_err);
42ca0993
BF
2913 if (status)
2914 goto out;
2915 }
2916
a528d35e 2917 err = vfs_getattr(&path, &stat, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
b8dd7b9a 2918 if (err)
1da177e4 2919 goto out_nfserr;
12337901
CH
2920 if ((bmval0 & (FATTR4_WORD0_FILES_AVAIL | FATTR4_WORD0_FILES_FREE |
2921 FATTR4_WORD0_FILES_TOTAL | FATTR4_WORD0_MAXNAME)) ||
1da177e4
LT
2922 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2923 FATTR4_WORD1_SPACE_TOTAL))) {
ebabe9a9 2924 err = vfs_statfs(&path, &statfs);
b8dd7b9a 2925 if (err)
1da177e4
LT
2926 goto out_nfserr;
2927 }
2928 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
d50e6136
BF
2929 tempfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
2930 status = nfserr_jukebox;
2931 if (!tempfh)
2932 goto out;
2933 fh_init(tempfh, NFS4_FHSIZE);
2934 status = fh_compose(tempfh, exp, dentry, NULL);
1da177e4
LT
2935 if (status)
2936 goto out;
d50e6136 2937 fhp = tempfh;
1da177e4 2938 }
0c9d65e7 2939 if (bmval0 & FATTR4_WORD0_ACL) {
b8dd7b9a 2940 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
0c9d65e7
AG
2941 if (err == -EOPNOTSUPP)
2942 bmval0 &= ~FATTR4_WORD0_ACL;
2943 else if (err == -EINVAL) {
2944 status = nfserr_attrnotsupp;
2945 goto out;
2946 } else if (err != 0)
2947 goto out_nfserr;
1da177e4 2948 }
1da177e4 2949
18032ca0 2950#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
c2227a39
KM
2951 if ((bmval2 & FATTR4_WORD2_SECURITY_LABEL) ||
2952 bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
32ddd944
BF
2953 if (exp->ex_flags & NFSEXP_SECURITY_LABEL)
2954 err = security_inode_getsecctx(d_inode(dentry),
18032ca0 2955 &context, &contextlen);
32ddd944
BF
2956 else
2957 err = -EOPNOTSUPP;
18032ca0
DQ
2958 contextsupport = (err == 0);
2959 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2960 if (err == -EOPNOTSUPP)
2961 bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2962 else if (err)
2963 goto out_nfserr;
2964 }
2965 }
2966#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2967
75976de6
KM
2968 status = nfsd4_encode_bitmap(xdr, bmval0, bmval1, bmval2);
2969 if (status)
2970 goto out;
082d4bd7
BF
2971
2972 attrlen_offset = xdr->buf->len;
ddd1ea56
BF
2973 p = xdr_reserve_space(xdr, 4);
2974 if (!p)
2975 goto out_resource;
082d4bd7 2976 p++; /* to be backfilled later */
1da177e4
LT
2977
2978 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
dcd20869
BF
2979 u32 supp[3];
2980
2981 memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
7e705706 2982
0c9d65e7 2983 if (!IS_POSIXACL(dentry->d_inode))
916d2d84 2984 supp[0] &= ~FATTR4_WORD0_ACL;
18032ca0 2985 if (!contextsupport)
916d2d84
BF
2986 supp[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2987 if (!supp[2]) {
ddd1ea56
BF
2988 p = xdr_reserve_space(xdr, 12);
2989 if (!p)
2b44f1ba 2990 goto out_resource;
c373b0a4 2991 *p++ = cpu_to_be32(2);
916d2d84
BF
2992 *p++ = cpu_to_be32(supp[0]);
2993 *p++ = cpu_to_be32(supp[1]);
7e705706 2994 } else {
ddd1ea56
BF
2995 p = xdr_reserve_space(xdr, 16);
2996 if (!p)
2b44f1ba 2997 goto out_resource;
c373b0a4 2998 *p++ = cpu_to_be32(3);
916d2d84
BF
2999 *p++ = cpu_to_be32(supp[0]);
3000 *p++ = cpu_to_be32(supp[1]);
3001 *p++ = cpu_to_be32(supp[2]);
7e705706 3002 }
1da177e4
LT
3003 }
3004 if (bmval0 & FATTR4_WORD0_TYPE) {
ddd1ea56
BF
3005 p = xdr_reserve_space(xdr, 4);
3006 if (!p)
1da177e4 3007 goto out_resource;
3d2544b1 3008 dummy = nfs4_file_type(stat.mode);
6b6d8137
BF
3009 if (dummy == NF4BAD) {
3010 status = nfserr_serverfault;
3011 goto out;
3012 }
c373b0a4 3013 *p++ = cpu_to_be32(dummy);
1da177e4
LT
3014 }
3015 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
ddd1ea56
BF
3016 p = xdr_reserve_space(xdr, 4);
3017 if (!p)
1da177e4 3018 goto out_resource;
49640001 3019 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
c373b0a4 3020 *p++ = cpu_to_be32(NFS4_FH_PERSISTENT);
49640001 3021 else
c373b0a4
BF
3022 *p++ = cpu_to_be32(NFS4_FH_PERSISTENT|
3023 NFS4_FH_VOL_RENAME);
1da177e4
LT
3024 }
3025 if (bmval0 & FATTR4_WORD0_CHANGE) {
ddd1ea56
BF
3026 p = xdr_reserve_space(xdr, 8);
3027 if (!p)
1da177e4 3028 goto out_resource;
b8800921 3029 p = encode_change(p, &stat, d_inode(dentry), exp);
1da177e4
LT
3030 }
3031 if (bmval0 & FATTR4_WORD0_SIZE) {
ddd1ea56
BF
3032 p = xdr_reserve_space(xdr, 8);
3033 if (!p)
1da177e4 3034 goto out_resource;
b64c7f3b 3035 p = xdr_encode_hyper(p, stat.size);
1da177e4
LT
3036 }
3037 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
ddd1ea56
BF
3038 p = xdr_reserve_space(xdr, 4);
3039 if (!p)
1da177e4 3040 goto out_resource;
c373b0a4 3041 *p++ = cpu_to_be32(1);
1da177e4
LT
3042 }
3043 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
ddd1ea56
BF
3044 p = xdr_reserve_space(xdr, 4);
3045 if (!p)
1da177e4 3046 goto out_resource;
c373b0a4 3047 *p++ = cpu_to_be32(1);
1da177e4
LT
3048 }
3049 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
ddd1ea56
BF
3050 p = xdr_reserve_space(xdr, 4);
3051 if (!p)
1da177e4 3052 goto out_resource;
c373b0a4 3053 *p++ = cpu_to_be32(0);
1da177e4
LT
3054 }
3055 if (bmval0 & FATTR4_WORD0_FSID) {
ddd1ea56
BF
3056 p = xdr_reserve_space(xdr, 16);
3057 if (!p)
1da177e4 3058 goto out_resource;
42ca0993 3059 if (exp->ex_fslocs.migrated) {
b64c7f3b
BF
3060 p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MAJOR);
3061 p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MINOR);
af6a4e28
N
3062 } else switch(fsid_source(fhp)) {
3063 case FSIDSOURCE_FSID:
b64c7f3b
BF
3064 p = xdr_encode_hyper(p, (u64)exp->ex_fsid);
3065 p = xdr_encode_hyper(p, (u64)0);
af6a4e28
N
3066 break;
3067 case FSIDSOURCE_DEV:
c373b0a4
BF
3068 *p++ = cpu_to_be32(0);
3069 *p++ = cpu_to_be32(MAJOR(stat.dev));
3070 *p++ = cpu_to_be32(0);
3071 *p++ = cpu_to_be32(MINOR(stat.dev));
af6a4e28
N
3072 break;
3073 case FSIDSOURCE_UUID:
94eb3689
KM
3074 p = xdr_encode_opaque_fixed(p, exp->ex_uuid,
3075 EX_UUID_LEN);
af6a4e28 3076 break;
1da177e4
LT
3077 }
3078 }
3079 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
ddd1ea56
BF
3080 p = xdr_reserve_space(xdr, 4);
3081 if (!p)
1da177e4 3082 goto out_resource;
c373b0a4 3083 *p++ = cpu_to_be32(0);
1da177e4
LT
3084 }
3085 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
ddd1ea56
BF
3086 p = xdr_reserve_space(xdr, 4);
3087 if (!p)
1da177e4 3088 goto out_resource;
c373b0a4 3089 *p++ = cpu_to_be32(nn->nfsd4_lease);
1da177e4
LT
3090 }
3091 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
ddd1ea56
BF
3092 p = xdr_reserve_space(xdr, 4);
3093 if (!p)
1da177e4 3094 goto out_resource;
c373b0a4 3095 *p++ = cpu_to_be32(rdattr_err);
1da177e4
LT
3096 }
3097 if (bmval0 & FATTR4_WORD0_ACL) {
3098 struct nfs4_ace *ace;
1da177e4
LT
3099
3100 if (acl == NULL) {
ddd1ea56
BF
3101 p = xdr_reserve_space(xdr, 4);
3102 if (!p)
1da177e4
LT
3103 goto out_resource;
3104
c373b0a4 3105 *p++ = cpu_to_be32(0);
1da177e4
LT
3106 goto out_acl;
3107 }
ddd1ea56
BF
3108 p = xdr_reserve_space(xdr, 4);
3109 if (!p)
1da177e4 3110 goto out_resource;
c373b0a4 3111 *p++ = cpu_to_be32(acl->naces);
1da177e4 3112
28e05dd8 3113 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
ddd1ea56
BF
3114 p = xdr_reserve_space(xdr, 4*3);
3115 if (!p)
1da177e4 3116 goto out_resource;
c373b0a4
BF
3117 *p++ = cpu_to_be32(ace->type);
3118 *p++ = cpu_to_be32(ace->flag);
3119 *p++ = cpu_to_be32(ace->access_mask &
3120 NFS4_ACE_MASK_ALL);
ddd1ea56 3121 status = nfsd4_encode_aclname(xdr, rqstp, ace);
1da177e4
LT
3122 if (status)
3123 goto out;
3124 }
3125 }
3126out_acl:
3127 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
ddd1ea56
BF
3128 p = xdr_reserve_space(xdr, 4);
3129 if (!p)
1da177e4 3130 goto out_resource;
0c9d65e7 3131 *p++ = cpu_to_be32(IS_POSIXACL(dentry->d_inode) ?
1da177e4
LT
3132 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
3133 }
3134 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
ddd1ea56
BF
3135 p = xdr_reserve_space(xdr, 4);
3136 if (!p)
1da177e4 3137 goto out_resource;
c373b0a4 3138 *p++ = cpu_to_be32(1);
1da177e4
LT
3139 }
3140 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
ddd1ea56
BF
3141 p = xdr_reserve_space(xdr, 4);
3142 if (!p)
1da177e4 3143 goto out_resource;
c373b0a4 3144 *p++ = cpu_to_be32(0);
1da177e4
LT
3145 }
3146 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
ddd1ea56
BF
3147 p = xdr_reserve_space(xdr, 4);
3148 if (!p)
1da177e4 3149 goto out_resource;
c373b0a4 3150 *p++ = cpu_to_be32(1);
1da177e4
LT
3151 }
3152 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
ddd1ea56
BF
3153 p = xdr_reserve_space(xdr, 4);
3154 if (!p)
1da177e4 3155 goto out_resource;
c373b0a4 3156 *p++ = cpu_to_be32(1);
1da177e4
LT
3157 }
3158 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
ddd1ea56
BF
3159 p = xdr_reserve_space(xdr, fhp->fh_handle.fh_size + 4);
3160 if (!p)
1da177e4 3161 goto out_resource;
0c0c267b
BF
3162 p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base,
3163 fhp->fh_handle.fh_size);
1da177e4
LT
3164 }
3165 if (bmval0 & FATTR4_WORD0_FILEID) {
ddd1ea56
BF
3166 p = xdr_reserve_space(xdr, 8);
3167 if (!p)
1da177e4 3168 goto out_resource;
b64c7f3b 3169 p = xdr_encode_hyper(p, stat.ino);
1da177e4
LT
3170 }
3171 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
ddd1ea56
BF
3172 p = xdr_reserve_space(xdr, 8);
3173 if (!p)
1da177e4 3174 goto out_resource;
b64c7f3b 3175 p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
1da177e4
LT
3176 }
3177 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
ddd1ea56
BF
3178 p = xdr_reserve_space(xdr, 8);
3179 if (!p)
1da177e4 3180 goto out_resource;
b64c7f3b 3181 p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
1da177e4
LT
3182 }
3183 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
ddd1ea56
BF
3184 p = xdr_reserve_space(xdr, 8);
3185 if (!p)
1da177e4 3186 goto out_resource;
b64c7f3b 3187 p = xdr_encode_hyper(p, (u64) statfs.f_files);
1da177e4 3188 }
81c3f413 3189 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
ddd1ea56 3190 status = nfsd4_encode_fs_locations(xdr, rqstp, exp);
81c3f413
BF
3191 if (status)
3192 goto out;
3193 }
1da177e4 3194 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
ddd1ea56
BF
3195 p = xdr_reserve_space(xdr, 4);
3196 if (!p)
1da177e4 3197 goto out_resource;
c373b0a4 3198 *p++ = cpu_to_be32(1);
1da177e4
LT
3199 }
3200 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
ddd1ea56
BF
3201 p = xdr_reserve_space(xdr, 8);
3202 if (!p)
1da177e4 3203 goto out_resource;
b64c7f3b 3204 p = xdr_encode_hyper(p, exp->ex_path.mnt->mnt_sb->s_maxbytes);
1da177e4
LT
3205 }
3206 if (bmval0 & FATTR4_WORD0_MAXLINK) {
ddd1ea56
BF
3207 p = xdr_reserve_space(xdr, 4);
3208 if (!p)
1da177e4 3209 goto out_resource;
c373b0a4 3210 *p++ = cpu_to_be32(255);
1da177e4
LT
3211 }
3212 if (bmval0 & FATTR4_WORD0_MAXNAME) {
ddd1ea56
BF
3213 p = xdr_reserve_space(xdr, 4);
3214 if (!p)
1da177e4 3215 goto out_resource;
c373b0a4 3216 *p++ = cpu_to_be32(statfs.f_namelen);
1da177e4
LT
3217 }
3218 if (bmval0 & FATTR4_WORD0_MAXREAD) {
ddd1ea56
BF
3219 p = xdr_reserve_space(xdr, 8);
3220 if (!p)
1da177e4 3221 goto out_resource;
b64c7f3b 3222 p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
1da177e4
LT
3223 }
3224 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
ddd1ea56
BF
3225 p = xdr_reserve_space(xdr, 8);
3226 if (!p)
1da177e4 3227 goto out_resource;
b64c7f3b 3228 p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
1da177e4
LT
3229 }
3230 if (bmval1 & FATTR4_WORD1_MODE) {
ddd1ea56
BF
3231 p = xdr_reserve_space(xdr, 4);
3232 if (!p)
1da177e4 3233 goto out_resource;
c373b0a4 3234 *p++ = cpu_to_be32(stat.mode & S_IALLUGO);
1da177e4
LT
3235 }
3236 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
ddd1ea56
BF
3237 p = xdr_reserve_space(xdr, 4);
3238 if (!p)
1da177e4 3239 goto out_resource;
c373b0a4 3240 *p++ = cpu_to_be32(1);
1da177e4
LT
3241 }
3242 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
ddd1ea56
BF
3243 p = xdr_reserve_space(xdr, 4);
3244 if (!p)
1da177e4 3245 goto out_resource;
c373b0a4 3246 *p++ = cpu_to_be32(stat.nlink);
1da177e4
LT
3247 }
3248 if (bmval1 & FATTR4_WORD1_OWNER) {
ddd1ea56 3249 status = nfsd4_encode_user(xdr, rqstp, stat.uid);
1da177e4
LT
3250 if (status)
3251 goto out;
3252 }
3253 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
ddd1ea56 3254 status = nfsd4_encode_group(xdr, rqstp, stat.gid);
1da177e4
LT
3255 if (status)
3256 goto out;
3257 }
3258 if (bmval1 & FATTR4_WORD1_RAWDEV) {
ddd1ea56
BF
3259 p = xdr_reserve_space(xdr, 8);
3260 if (!p)
1da177e4 3261 goto out_resource;
c373b0a4
BF
3262 *p++ = cpu_to_be32((u32) MAJOR(stat.rdev));
3263 *p++ = cpu_to_be32((u32) MINOR(stat.rdev));
1da177e4
LT
3264 }
3265 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
ddd1ea56
BF
3266 p = xdr_reserve_space(xdr, 8);
3267 if (!p)
1da177e4
LT
3268 goto out_resource;
3269 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
b64c7f3b 3270 p = xdr_encode_hyper(p, dummy64);
1da177e4
LT
3271 }
3272 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
ddd1ea56
BF
3273 p = xdr_reserve_space(xdr, 8);
3274 if (!p)
1da177e4
LT
3275 goto out_resource;
3276 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
b64c7f3b 3277 p = xdr_encode_hyper(p, dummy64);
1da177e4
LT
3278 }
3279 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
ddd1ea56
BF
3280 p = xdr_reserve_space(xdr, 8);
3281 if (!p)
1da177e4
LT
3282 goto out_resource;
3283 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
b64c7f3b 3284 p = xdr_encode_hyper(p, dummy64);
1da177e4
LT
3285 }
3286 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
ddd1ea56
BF
3287 p = xdr_reserve_space(xdr, 8);
3288 if (!p)
1da177e4
LT
3289 goto out_resource;
3290 dummy64 = (u64)stat.blocks << 9;
b64c7f3b 3291 p = xdr_encode_hyper(p, dummy64);
1da177e4
LT
3292 }
3293 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
ddd1ea56
BF
3294 p = xdr_reserve_space(xdr, 12);
3295 if (!p)
1da177e4 3296 goto out_resource;
b64c7f3b 3297 p = xdr_encode_hyper(p, (s64)stat.atime.tv_sec);
c373b0a4 3298 *p++ = cpu_to_be32(stat.atime.tv_nsec);
1da177e4
LT
3299 }
3300 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
ddd1ea56
BF
3301 p = xdr_reserve_space(xdr, 12);
3302 if (!p)
1da177e4 3303 goto out_resource;
16945141 3304 p = encode_time_delta(p, d_inode(dentry));
1da177e4
LT
3305 }
3306 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
ddd1ea56
BF
3307 p = xdr_reserve_space(xdr, 12);
3308 if (!p)
1da177e4 3309 goto out_resource;
b64c7f3b 3310 p = xdr_encode_hyper(p, (s64)stat.ctime.tv_sec);
c373b0a4 3311 *p++ = cpu_to_be32(stat.ctime.tv_nsec);
1da177e4
LT
3312 }
3313 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
ddd1ea56
BF
3314 p = xdr_reserve_space(xdr, 12);
3315 if (!p)
1da177e4 3316 goto out_resource;
b64c7f3b 3317 p = xdr_encode_hyper(p, (s64)stat.mtime.tv_sec);
c373b0a4 3318 *p++ = cpu_to_be32(stat.mtime.tv_nsec);
1da177e4
LT
3319 }
3320 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
0a2050d7
KM
3321 struct kstat parent_stat;
3322 u64 ino = stat.ino;
3323
ddd1ea56
BF
3324 p = xdr_reserve_space(xdr, 8);
3325 if (!p)
1da177e4 3326 goto out_resource;
406a7ea9
FF
3327 /*
3328 * Get parent's attributes if not ignoring crossmount
3329 * and this is the root of a cross-mounted filesystem.
3330 */
3331 if (ignore_crossmnt == 0 &&
0a2050d7
KM
3332 dentry == exp->ex_path.mnt->mnt_root) {
3333 err = get_parent_attributes(exp, &parent_stat);
3334 if (err)
3335 goto out_nfserr;
3336 ino = parent_stat.ino;
3337 }
3338 p = xdr_encode_hyper(p, ino);
1da177e4 3339 }
9cf514cc 3340#ifdef CONFIG_NFSD_PNFS
6896f15a 3341 if (bmval1 & FATTR4_WORD1_FS_LAYOUT_TYPES) {
8a4c3926 3342 status = nfsd4_encode_layout_types(xdr, exp->ex_layout_types);
6896f15a
KM
3343 if (status)
3344 goto out;
3345 }
3346
3347 if (bmval2 & FATTR4_WORD2_LAYOUT_TYPES) {
8a4c3926 3348 status = nfsd4_encode_layout_types(xdr, exp->ex_layout_types);
6896f15a
KM
3349 if (status)
3350 goto out;
9cf514cc
CH
3351 }
3352
3353 if (bmval2 & FATTR4_WORD2_LAYOUT_BLKSIZE) {
3354 p = xdr_reserve_space(xdr, 4);
3355 if (!p)
3356 goto out_resource;
3357 *p++ = cpu_to_be32(stat.blksize);
3358 }
3359#endif /* CONFIG_NFSD_PNFS */
8c18f205 3360 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
b26b78cb
TM
3361 u32 supp[3];
3362
3363 memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
3364 supp[0] &= NFSD_SUPPATTR_EXCLCREAT_WORD0;
3365 supp[1] &= NFSD_SUPPATTR_EXCLCREAT_WORD1;
3366 supp[2] &= NFSD_SUPPATTR_EXCLCREAT_WORD2;
3367
3368 status = nfsd4_encode_bitmap(xdr, supp[0], supp[1], supp[2]);
75976de6
KM
3369 if (status)
3370 goto out;
8c18f205 3371 }
7e705706 3372
a8585763
BF
3373 if (bmval2 & FATTR4_WORD2_CHANGE_ATTR_TYPE) {
3374 p = xdr_reserve_space(xdr, 4);
3375 if (!p)
3376 goto out_resource;
3377 if (IS_I_VERSION(d_inode(dentry)))
3378 *p++ = cpu_to_be32(NFS4_CHANGE_TYPE_IS_MONOTONIC_INCR);
3379 else
3380 *p++ = cpu_to_be32(NFS4_CHANGE_TYPE_IS_TIME_METADATA);
3381 }
3382
0ab88ca4 3383#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
7d580722
KM
3384 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
3385 status = nfsd4_encode_security_label(xdr, rqstp, context,
3386 contextlen);
3387 if (status)
3388 goto out;
3389 }
0ab88ca4 3390#endif
7d580722 3391
0e885e84
FL
3392 if (bmval2 & FATTR4_WORD2_XATTR_SUPPORT) {
3393 p = xdr_reserve_space(xdr, 4);
3394 if (!p)
3395 goto out_resource;
3396 err = xattr_supported_namespace(d_inode(dentry),
3397 XATTR_USER_PREFIX);
3398 *p++ = cpu_to_be32(err == 0);
3399 }
3400
082d4bd7
BF
3401 attrlen = htonl(xdr->buf->len - attrlen_offset - 4);
3402 write_bytes_to_xdr_buf(xdr->buf, attrlen_offset, &attrlen, 4);
1da177e4
LT
3403 status = nfs_ok;
3404
3405out:
ba4e55bb 3406#ifdef CONFIG_NFSD_V4_SECURITY_LABEL
18032ca0
DQ
3407 if (context)
3408 security_release_secctx(context, contextlen);
ba4e55bb 3409#endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
28e05dd8 3410 kfree(acl);
18df11d0 3411 if (tempfh) {
d50e6136 3412 fh_put(tempfh);
18df11d0
YZ
3413 kfree(tempfh);
3414 }
1fcea5b2
BF
3415 if (status)
3416 xdr_truncate_encode(xdr, starting_len);
1da177e4
LT
3417 return status;
3418out_nfserr:
b8dd7b9a 3419 status = nfserrno(err);
1da177e4
LT
3420 goto out;
3421out_resource:
1da177e4
LT
3422 status = nfserr_resource;
3423 goto out;
1da177e4
LT
3424}
3425
2825a7f9
BF
3426static void svcxdr_init_encode_from_buffer(struct xdr_stream *xdr,
3427 struct xdr_buf *buf, __be32 *p, int bytes)
3428{
3429 xdr->scratch.iov_len = 0;
3430 memset(buf, 0, sizeof(struct xdr_buf));
3431 buf->head[0].iov_base = p;
3432 buf->head[0].iov_len = 0;
3433 buf->len = 0;
3434 xdr->buf = buf;
3435 xdr->iov = buf->head;
3436 xdr->p = p;
3437 xdr->end = (void *)p + bytes;
3438 buf->buflen = bytes;
3439}
3440
d5184658
BF
3441__be32 nfsd4_encode_fattr_to_buf(__be32 **p, int words,
3442 struct svc_fh *fhp, struct svc_export *exp,
3443 struct dentry *dentry, u32 *bmval,
3444 struct svc_rqst *rqstp, int ignore_crossmnt)
3445{
2825a7f9 3446 struct xdr_buf dummy;
d5184658
BF
3447 struct xdr_stream xdr;
3448 __be32 ret;
3449
2825a7f9 3450 svcxdr_init_encode_from_buffer(&xdr, &dummy, *p, words << 2);
d5184658
BF
3451 ret = nfsd4_encode_fattr(&xdr, fhp, exp, dentry, bmval, rqstp,
3452 ignore_crossmnt);
3453 *p = xdr.p;
3454 return ret;
3455}
3456
c0ce6ec8
BF
3457static inline int attributes_need_mount(u32 *bmval)
3458{
3459 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
3460 return 1;
3461 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
3462 return 1;
3463 return 0;
3464}
3465
b37ad28b 3466static __be32
561f0ed4
BF
3467nfsd4_encode_dirent_fattr(struct xdr_stream *xdr, struct nfsd4_readdir *cd,
3468 const char *name, int namlen)
1da177e4
LT
3469{
3470 struct svc_export *exp = cd->rd_fhp->fh_export;
3471 struct dentry *dentry;
b37ad28b 3472 __be32 nfserr;
406a7ea9 3473 int ignore_crossmnt = 0;
1da177e4 3474
6c2d4798 3475 dentry = lookup_positive_unlocked(name, cd->rd_fhp->fh_dentry, namlen);
1da177e4
LT
3476 if (IS_ERR(dentry))
3477 return nfserrno(PTR_ERR(dentry));
3478
3479 exp_get(exp);
406a7ea9
FF
3480 /*
3481 * In the case of a mountpoint, the client may be asking for
3482 * attributes that are only properties of the underlying filesystem
3483 * as opposed to the cross-mounted file system. In such a case,
3484 * we will not follow the cross mount and will fill the attribtutes
3485 * directly from the mountpoint dentry.
3486 */
3227fa41 3487 if (nfsd_mountpoint(dentry, exp)) {
021d3a72
BF
3488 int err;
3489
3227fa41
BF
3490 if (!(exp->ex_flags & NFSEXP_V4ROOT)
3491 && !attributes_need_mount(cd->rd_bmval)) {
3492 ignore_crossmnt = 1;
3493 goto out_encode;
3494 }
dcb488a3
AA
3495 /*
3496 * Why the heck aren't we just using nfsd_lookup??
3497 * Different "."/".." handling? Something else?
3498 * At least, add a comment here to explain....
3499 */
021d3a72
BF
3500 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
3501 if (err) {
3502 nfserr = nfserrno(err);
1da177e4
LT
3503 goto out_put;
3504 }
dcb488a3
AA
3505 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
3506 if (nfserr)
3507 goto out_put;
1da177e4
LT
3508
3509 }
3227fa41 3510out_encode:
561f0ed4 3511 nfserr = nfsd4_encode_fattr(xdr, NULL, exp, dentry, cd->rd_bmval,
406a7ea9 3512 cd->rd_rqstp, ignore_crossmnt);
1da177e4
LT
3513out_put:
3514 dput(dentry);
3515 exp_put(exp);
3516 return nfserr;
3517}
3518
2ebbc012 3519static __be32 *
561f0ed4 3520nfsd4_encode_rdattr_error(struct xdr_stream *xdr, __be32 nfserr)
1da177e4 3521{
561f0ed4
BF
3522 __be32 *p;
3523
c3a45617 3524 p = xdr_reserve_space(xdr, 20);
561f0ed4 3525 if (!p)
1da177e4
LT
3526 return NULL;
3527 *p++ = htonl(2);
3528 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
3529 *p++ = htonl(0); /* bmval1 */
3530
87915c64 3531 *p++ = htonl(4); /* attribute length */
1da177e4 3532 *p++ = nfserr; /* no htonl */
1da177e4
LT
3533 return p;
3534}
3535
3536static int
a0ad13ef
N
3537nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
3538 loff_t offset, u64 ino, unsigned int d_type)
1da177e4 3539{
a0ad13ef 3540 struct readdir_cd *ccd = ccdv;
1da177e4 3541 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
561f0ed4
BF
3542 struct xdr_stream *xdr = cd->xdr;
3543 int start_offset = xdr->buf->len;
3544 int cookie_offset;
aee37764 3545 u32 name_and_cookie;
561f0ed4 3546 int entry_bytes;
b37ad28b 3547 __be32 nfserr = nfserr_toosmall;
561f0ed4
BF
3548 __be64 wire_offset;
3549 __be32 *p;
1da177e4
LT
3550
3551 /* In nfsv4, "." and ".." never make it onto the wire.. */
3552 if (name && isdotent(name, namlen)) {
3553 cd->common.err = nfs_ok;
3554 return 0;
3555 }
3556
561f0ed4
BF
3557 if (cd->cookie_offset) {
3558 wire_offset = cpu_to_be64(offset);
3559 write_bytes_to_xdr_buf(xdr->buf, cd->cookie_offset,
3560 &wire_offset, 8);
3561 }
1da177e4 3562
561f0ed4
BF
3563 p = xdr_reserve_space(xdr, 4);
3564 if (!p)
1da177e4 3565 goto fail;
1da177e4 3566 *p++ = xdr_one; /* mark entry present */
561f0ed4
BF
3567 cookie_offset = xdr->buf->len;
3568 p = xdr_reserve_space(xdr, 3*4 + namlen);
3569 if (!p)
3570 goto fail;
1da177e4
LT
3571 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
3572 p = xdr_encode_array(p, name, namlen); /* name length & name */
3573
561f0ed4 3574 nfserr = nfsd4_encode_dirent_fattr(xdr, cd, name, namlen);
1da177e4
LT
3575 switch (nfserr) {
3576 case nfs_ok:
1da177e4
LT
3577 break;
3578 case nfserr_resource:
3579 nfserr = nfserr_toosmall;
3580 goto fail;
b2c0cea6 3581 case nfserr_noent:
f41c5ad2 3582 xdr_truncate_encode(xdr, start_offset);
b2c0cea6 3583 goto skip_entry;
1da177e4
LT
3584 default:
3585 /*
3586 * If the client requested the RDATTR_ERROR attribute,
3587 * we stuff the error code into this attribute
3588 * and continue. If this attribute was not requested,
3589 * then in accordance with the spec, we fail the
3590 * entire READDIR operation(!)
3591 */
3592 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
3593 goto fail;
561f0ed4 3594 p = nfsd4_encode_rdattr_error(xdr, nfserr);
34081efc
FI
3595 if (p == NULL) {
3596 nfserr = nfserr_toosmall;
1da177e4 3597 goto fail;
34081efc 3598 }
1da177e4 3599 }
561f0ed4
BF
3600 nfserr = nfserr_toosmall;
3601 entry_bytes = xdr->buf->len - start_offset;
3602 if (entry_bytes > cd->rd_maxcount)
3603 goto fail;
3604 cd->rd_maxcount -= entry_bytes;
aee37764
BF
3605 /*
3606 * RFC 3530 14.2.24 describes rd_dircount as only a "hint", so
3607 * let's always let through the first entry, at least:
3608 */
0ec016e3
BF
3609 if (!cd->rd_dircount)
3610 goto fail;
3611 name_and_cookie = 4 + 4 * XDR_QUADLEN(namlen) + 8;
aee37764
BF
3612 if (name_and_cookie > cd->rd_dircount && cd->cookie_offset)
3613 goto fail;
3614 cd->rd_dircount -= min(cd->rd_dircount, name_and_cookie);
0ec016e3 3615
561f0ed4 3616 cd->cookie_offset = cookie_offset;
b2c0cea6 3617skip_entry:
1da177e4
LT
3618 cd->common.err = nfs_ok;
3619 return 0;
3620fail:
561f0ed4 3621 xdr_truncate_encode(xdr, start_offset);
1da177e4
LT
3622 cd->common.err = nfserr;
3623 return -EINVAL;
3624}
3625
d0a381dd
BF
3626static __be32
3627nfsd4_encode_stateid(struct xdr_stream *xdr, stateid_t *sid)
e2f282b9 3628{
bc749ca4 3629 __be32 *p;
e2f282b9 3630
d0a381dd
BF
3631 p = xdr_reserve_space(xdr, sizeof(stateid_t));
3632 if (!p)
3633 return nfserr_resource;
c373b0a4 3634 *p++ = cpu_to_be32(sid->si_generation);
0c0c267b
BF
3635 p = xdr_encode_opaque_fixed(p, &sid->si_opaque,
3636 sizeof(stateid_opaque_t));
d0a381dd 3637 return 0;
e2f282b9
BH
3638}
3639
695e12f8 3640static __be32
b37ad28b 3641nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
1da177e4 3642{
d0a381dd 3643 struct xdr_stream *xdr = &resp->xdr;
bc749ca4 3644 __be32 *p;
1da177e4 3645
bac966d6
BF
3646 p = xdr_reserve_space(xdr, 8);
3647 if (!p)
3648 return nfserr_resource;
3649 *p++ = cpu_to_be32(access->ac_supported);
3650 *p++ = cpu_to_be32(access->ac_resp_access);
3651 return 0;
1da177e4
LT
3652}
3653
1d1bc8f2
BF
3654static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
3655{
d0a381dd 3656 struct xdr_stream *xdr = &resp->xdr;
1d1bc8f2
BF
3657 __be32 *p;
3658
bac966d6
BF
3659 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 8);
3660 if (!p)
3661 return nfserr_resource;
3662 p = xdr_encode_opaque_fixed(p, bcts->sessionid.data,
3663 NFS4_MAX_SESSIONID_LEN);
3664 *p++ = cpu_to_be32(bcts->dir);
3665 /* Upshifting from TCP to RDMA is not supported */
3666 *p++ = cpu_to_be32(0);
3667 return 0;
1d1bc8f2
BF
3668}
3669
695e12f8 3670static __be32
b37ad28b 3671nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
1da177e4 3672{
d0a381dd
BF
3673 struct xdr_stream *xdr = &resp->xdr;
3674
bac966d6 3675 return nfsd4_encode_stateid(xdr, &close->cl_stateid);
1da177e4
LT
3676}
3677
3678
695e12f8 3679static __be32
b37ad28b 3680nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
1da177e4 3681{
d0a381dd 3682 struct xdr_stream *xdr = &resp->xdr;
bc749ca4 3683 __be32 *p;
1da177e4 3684
bac966d6
BF
3685 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
3686 if (!p)
3687 return nfserr_resource;
3688 p = xdr_encode_opaque_fixed(p, commit->co_verf.data,
0c0c267b 3689 NFS4_VERIFIER_SIZE);
bac966d6 3690 return 0;
1da177e4
LT
3691}
3692
695e12f8 3693static __be32
b37ad28b 3694nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
1da177e4 3695{
d0a381dd 3696 struct xdr_stream *xdr = &resp->xdr;
bc749ca4 3697 __be32 *p;
1da177e4 3698
bac966d6
BF
3699 p = xdr_reserve_space(xdr, 20);
3700 if (!p)
3701 return nfserr_resource;
3702 encode_cinfo(p, &create->cr_cinfo);
b96811cd 3703 return nfsd4_encode_bitmap(xdr, create->cr_bmval[0],
bac966d6 3704 create->cr_bmval[1], create->cr_bmval[2]);
1da177e4
LT
3705}
3706
b37ad28b
AV
3707static __be32
3708nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
1da177e4
LT
3709{
3710 struct svc_fh *fhp = getattr->ga_fhp;
d5184658 3711 struct xdr_stream *xdr = &resp->xdr;
1da177e4 3712
bac966d6
BF
3713 return nfsd4_encode_fattr(xdr, fhp, fhp->fh_export, fhp->fh_dentry,
3714 getattr->ga_bmval, resp->rqstp, 0);
1da177e4
LT
3715}
3716
695e12f8
BH
3717static __be32
3718nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
1da177e4 3719{
d0a381dd 3720 struct xdr_stream *xdr = &resp->xdr;
695e12f8 3721 struct svc_fh *fhp = *fhpp;
1da177e4 3722 unsigned int len;
bc749ca4 3723 __be32 *p;
1da177e4 3724
bac966d6
BF
3725 len = fhp->fh_handle.fh_size;
3726 p = xdr_reserve_space(xdr, len + 4);
3727 if (!p)
3728 return nfserr_resource;
3729 p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base, len);
3730 return 0;
1da177e4
LT
3731}
3732
3733/*
3734* Including all fields other than the name, a LOCK4denied structure requires
3735* 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
3736*/
d0a381dd
BF
3737static __be32
3738nfsd4_encode_lock_denied(struct xdr_stream *xdr, struct nfsd4_lock_denied *ld)
1da177e4 3739{
7c13f344 3740 struct xdr_netobj *conf = &ld->ld_owner;
bc749ca4 3741 __be32 *p;
1da177e4 3742
8c7424cf 3743again:
d0a381dd 3744 p = xdr_reserve_space(xdr, 32 + XDR_LEN(conf->len));
8c7424cf
BF
3745 if (!p) {
3746 /*
3747 * Don't fail to return the result just because we can't
3748 * return the conflicting open:
3749 */
3750 if (conf->len) {
f98bac5a 3751 kfree(conf->data);
8c7424cf
BF
3752 conf->len = 0;
3753 conf->data = NULL;
3754 goto again;
3755 }
d0a381dd 3756 return nfserr_resource;
8c7424cf 3757 }
b64c7f3b
BF
3758 p = xdr_encode_hyper(p, ld->ld_start);
3759 p = xdr_encode_hyper(p, ld->ld_length);
c373b0a4 3760 *p++ = cpu_to_be32(ld->ld_type);
7c13f344 3761 if (conf->len) {
0c0c267b
BF
3762 p = xdr_encode_opaque_fixed(p, &ld->ld_clientid, 8);
3763 p = xdr_encode_opaque(p, conf->data, conf->len);
f98bac5a 3764 kfree(conf->data);
1da177e4 3765 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
b64c7f3b 3766 p = xdr_encode_hyper(p, (u64)0); /* clientid */
c373b0a4 3767 *p++ = cpu_to_be32(0); /* length of owner name */
1da177e4 3768 }
d0a381dd 3769 return nfserr_denied;
1da177e4
LT
3770}
3771
695e12f8 3772static __be32
b37ad28b 3773nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
1da177e4 3774{
d0a381dd
BF
3775 struct xdr_stream *xdr = &resp->xdr;
3776
e2f282b9 3777 if (!nfserr)
d0a381dd 3778 nfserr = nfsd4_encode_stateid(xdr, &lock->lk_resp_stateid);
e2f282b9 3779 else if (nfserr == nfserr_denied)
d0a381dd 3780 nfserr = nfsd4_encode_lock_denied(xdr, &lock->lk_denied);
f98bac5a 3781
695e12f8 3782 return nfserr;
1da177e4
LT
3783}
3784
695e12f8 3785static __be32
b37ad28b 3786nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
1da177e4 3787{
d0a381dd
BF
3788 struct xdr_stream *xdr = &resp->xdr;
3789
1da177e4 3790 if (nfserr == nfserr_denied)
d0a381dd 3791 nfsd4_encode_lock_denied(xdr, &lockt->lt_denied);
695e12f8 3792 return nfserr;
1da177e4
LT
3793}
3794
695e12f8 3795static __be32
b37ad28b 3796nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
1da177e4 3797{
d0a381dd
BF
3798 struct xdr_stream *xdr = &resp->xdr;
3799
bac966d6 3800 return nfsd4_encode_stateid(xdr, &locku->lu_stateid);
1da177e4
LT
3801}
3802
3803
695e12f8 3804static __be32
b37ad28b 3805nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
1da177e4 3806{
d0a381dd 3807 struct xdr_stream *xdr = &resp->xdr;
bc749ca4 3808 __be32 *p;
1da177e4 3809
bac966d6
BF
3810 p = xdr_reserve_space(xdr, 20);
3811 if (!p)
3812 return nfserr_resource;
3813 p = encode_cinfo(p, &link->li_cinfo);
3814 return 0;
1da177e4
LT
3815}
3816
3817
695e12f8 3818static __be32
b37ad28b 3819nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
1da177e4 3820{
d0a381dd 3821 struct xdr_stream *xdr = &resp->xdr;
bc749ca4 3822 __be32 *p;
1da177e4 3823
d0a381dd
BF
3824 nfserr = nfsd4_encode_stateid(xdr, &open->op_stateid);
3825 if (nfserr)
bac966d6 3826 return nfserr;
75976de6 3827 p = xdr_reserve_space(xdr, 24);
d0a381dd
BF
3828 if (!p)
3829 return nfserr_resource;
d05d5744 3830 p = encode_cinfo(p, &open->op_cinfo);
c373b0a4 3831 *p++ = cpu_to_be32(open->op_rflags);
1da177e4 3832
75976de6
KM
3833 nfserr = nfsd4_encode_bitmap(xdr, open->op_bmval[0], open->op_bmval[1],
3834 open->op_bmval[2]);
3835 if (nfserr)
bac966d6 3836 return nfserr;
75976de6
KM
3837
3838 p = xdr_reserve_space(xdr, 4);
3839 if (!p)
3840 return nfserr_resource;
3841
3842 *p++ = cpu_to_be32(open->op_delegate_type);
1da177e4
LT
3843 switch (open->op_delegate_type) {
3844 case NFS4_OPEN_DELEGATE_NONE:
3845 break;
3846 case NFS4_OPEN_DELEGATE_READ:
d0a381dd
BF
3847 nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
3848 if (nfserr)
3849 return nfserr;
3850 p = xdr_reserve_space(xdr, 20);
3851 if (!p)
3852 return nfserr_resource;
c373b0a4 3853 *p++ = cpu_to_be32(open->op_recall);
1da177e4
LT
3854
3855 /*
3856 * TODO: ACE's in delegations
3857 */
c373b0a4
BF
3858 *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
3859 *p++ = cpu_to_be32(0);
3860 *p++ = cpu_to_be32(0);
3861 *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
1da177e4
LT
3862 break;
3863 case NFS4_OPEN_DELEGATE_WRITE:
d0a381dd
BF
3864 nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
3865 if (nfserr)
3866 return nfserr;
3867 p = xdr_reserve_space(xdr, 32);
3868 if (!p)
3869 return nfserr_resource;
c373b0a4 3870 *p++ = cpu_to_be32(0);
1da177e4
LT
3871
3872 /*
3873 * TODO: space_limit's in delegations
3874 */
c373b0a4
BF
3875 *p++ = cpu_to_be32(NFS4_LIMIT_SIZE);
3876 *p++ = cpu_to_be32(~(u32)0);
3877 *p++ = cpu_to_be32(~(u32)0);
1da177e4
LT
3878
3879 /*
3880 * TODO: ACE's in delegations
3881 */
c373b0a4
BF
3882 *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
3883 *p++ = cpu_to_be32(0);
3884 *p++ = cpu_to_be32(0);
3885 *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
1da177e4 3886 break;
d24433cd
BH
3887 case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
3888 switch (open->op_why_no_deleg) {
3889 case WND4_CONTENTION:
3890 case WND4_RESOURCE:
d0a381dd
BF
3891 p = xdr_reserve_space(xdr, 8);
3892 if (!p)
3893 return nfserr_resource;
c373b0a4
BF
3894 *p++ = cpu_to_be32(open->op_why_no_deleg);
3895 /* deleg signaling not supported yet: */
3896 *p++ = cpu_to_be32(0);
d24433cd
BH
3897 break;
3898 default:
d0a381dd
BF
3899 p = xdr_reserve_space(xdr, 4);
3900 if (!p)
3901 return nfserr_resource;
c373b0a4 3902 *p++ = cpu_to_be32(open->op_why_no_deleg);
d24433cd 3903 }
d24433cd 3904 break;
1da177e4
LT
3905 default:
3906 BUG();
3907 }
3908 /* XXX save filehandle here */
bac966d6 3909 return 0;
1da177e4
LT
3910}
3911
695e12f8 3912static __be32
b37ad28b 3913nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
1da177e4 3914{
d0a381dd
BF
3915 struct xdr_stream *xdr = &resp->xdr;
3916
bac966d6 3917 return nfsd4_encode_stateid(xdr, &oc->oc_resp_stateid);
1da177e4
LT
3918}
3919
695e12f8 3920static __be32
b37ad28b 3921nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
1da177e4 3922{
d0a381dd
BF
3923 struct xdr_stream *xdr = &resp->xdr;
3924
bac966d6 3925 return nfsd4_encode_stateid(xdr, &od->od_stateid);
1da177e4
LT
3926}
3927
dc97618d
BF
3928static __be32 nfsd4_encode_splice_read(
3929 struct nfsd4_compoundres *resp,
3930 struct nfsd4_read *read,
3931 struct file *file, unsigned long maxcount)
1da177e4 3932{
ddd1ea56 3933 struct xdr_stream *xdr = &resp->xdr;
34a78b48 3934 struct xdr_buf *buf = xdr->buf;
76e5492b 3935 int status, space_left;
dc97618d 3936 u32 eof;
dc97618d 3937 __be32 nfserr;
fec25fa4 3938 __be32 *p = xdr->p - 2;
1da177e4 3939
d5d5c304
KM
3940 /* Make sure there will be room for padding if needed */
3941 if (xdr->end - xdr->p < 1)
d0a381dd 3942 return nfserr_resource;
dc97618d 3943
87c5942e 3944 nfserr = nfsd_splice_read(read->rd_rqstp, read->rd_fhp,
83a63072 3945 file, read->rd_offset, &maxcount, &eof);
87c5942e 3946 read->rd_length = maxcount;
76e5492b
CL
3947 if (nfserr)
3948 goto out_err;
3949 status = svc_encode_result_payload(read->rd_rqstp,
3950 buf->head[0].iov_len, maxcount);
3951 if (status) {
3952 nfserr = nfserrno(status);
3953 goto out_err;
b0e35fda 3954 }
1da177e4 3955
fec25fa4
BF
3956 *(p++) = htonl(eof);
3957 *(p++) = htonl(maxcount);
dc97618d 3958
34a78b48
BF
3959 buf->page_len = maxcount;
3960 buf->len += maxcount;
15b23ef5
BF
3961 xdr->page_ptr += (buf->page_base + maxcount + PAGE_SIZE - 1)
3962 / PAGE_SIZE;
dc97618d
BF
3963
3964 /* Use rest of head for padding and remaining ops: */
34a78b48
BF
3965 buf->tail[0].iov_base = xdr->p;
3966 buf->tail[0].iov_len = 0;
fec25fa4 3967 xdr->iov = buf->tail;
dc97618d 3968 if (maxcount&3) {
fec25fa4
BF
3969 int pad = 4 - (maxcount&3);
3970
3971 *(xdr->p++) = 0;
3972
34a78b48 3973 buf->tail[0].iov_base += maxcount&3;
fec25fa4
BF
3974 buf->tail[0].iov_len = pad;
3975 buf->len += pad;
b0e35fda 3976 }
2825a7f9 3977
dc97618d 3978 space_left = min_t(int, (void *)xdr->end - (void *)xdr->p,
34a78b48
BF
3979 buf->buflen - buf->len);
3980 buf->buflen = buf->len + space_left;
dc97618d
BF
3981 xdr->end = (__be32 *)((void *)xdr->end + space_left);
3982
3983 return 0;
76e5492b
CL
3984
3985out_err:
3986 /*
3987 * nfsd_splice_actor may have already messed with the
3988 * page length; reset it so as not to confuse
3989 * xdr_truncate_encode in our caller.
3990 */
3991 buf->page_len = 0;
3992 return nfserr;
dc97618d
BF
3993}
3994
3995static __be32 nfsd4_encode_readv(struct nfsd4_compoundres *resp,
3996 struct nfsd4_read *read,
3997 struct file *file, unsigned long maxcount)
3998{
3999 struct xdr_stream *xdr = &resp->xdr;
4000 u32 eof;
dc97618d 4001 int starting_len = xdr->buf->len - 8;
dc97618d
BF
4002 __be32 nfserr;
4003 __be32 tmp;
b0420980 4004 int pad;
1da177e4 4005
403217f3
AS
4006 read->rd_vlen = xdr_reserve_space_vec(xdr, resp->rqstp->rq_vec, maxcount);
4007 if (read->rd_vlen < 0)
4008 return nfserr_resource;
1da177e4 4009
87c5942e 4010 nfserr = nfsd_readv(resp->rqstp, read->rd_fhp, file, read->rd_offset,
83a63072
TM
4011 resp->rqstp->rq_vec, read->rd_vlen, &maxcount,
4012 &eof);
87c5942e 4013 read->rd_length = maxcount;
dc97618d 4014 if (nfserr)
1da177e4 4015 return nfserr;
03493bca 4016 if (svc_encode_result_payload(resp->rqstp, starting_len + 8, maxcount))
41205539 4017 return nfserr_io;
7dcf4ab9 4018 xdr_truncate_encode(xdr, starting_len + 8 + xdr_align_size(maxcount));
dc97618d 4019
dc97618d
BF
4020 tmp = htonl(eof);
4021 write_bytes_to_xdr_buf(xdr->buf, starting_len , &tmp, 4);
4022 tmp = htonl(maxcount);
4023 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp, 4);
4024
7dcf4ab9 4025 tmp = xdr_zero;
b0420980
BF
4026 pad = (maxcount&3) ? 4 - (maxcount&3) : 0;
4027 write_bytes_to_xdr_buf(xdr->buf, starting_len + 8 + maxcount,
7dcf4ab9 4028 &tmp, pad);
1da177e4 4029 return 0;
dc97618d
BF
4030
4031}
4032
4033static __be32
4034nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
4035 struct nfsd4_read *read)
4036{
4037 unsigned long maxcount;
4038 struct xdr_stream *xdr = &resp->xdr;
5c4583b2 4039 struct file *file;
dc97618d 4040 int starting_len = xdr->buf->len;
dc97618d 4041 __be32 *p;
dc97618d 4042
5c4583b2
JL
4043 if (nfserr)
4044 return nfserr;
4045 file = read->rd_nf->nf_file;
4046
dc97618d
BF
4047 p = xdr_reserve_space(xdr, 8); /* eof flag and byte count */
4048 if (!p) {
779fb0f3 4049 WARN_ON_ONCE(test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags));
bac966d6 4050 return nfserr_resource;
dc97618d 4051 }
68e8bb03
CH
4052 if (resp->xdr.buf->page_len &&
4053 test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags)) {
b0420980 4054 WARN_ON_ONCE(1);
bac966d6 4055 return nfserr_resource;
dc97618d 4056 }
dc97618d
BF
4057 xdr_commit_encode(xdr);
4058
4059 maxcount = svc_max_payload(resp->rqstp);
68e8bb03
CH
4060 maxcount = min_t(unsigned long, maxcount,
4061 (xdr->buf->buflen - xdr->buf->len));
3c7aa15d 4062 maxcount = min_t(unsigned long, maxcount, read->rd_length);
dc97618d 4063
68e8bb03
CH
4064 if (file->f_op->splice_read &&
4065 test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags))
96bcad50 4066 nfserr = nfsd4_encode_splice_read(resp, read, file, maxcount);
dc97618d 4067 else
96bcad50 4068 nfserr = nfsd4_encode_readv(resp, read, file, maxcount);
dc97618d 4069
96bcad50 4070 if (nfserr)
dc97618d 4071 xdr_truncate_encode(xdr, starting_len);
96bcad50 4072
96bcad50 4073 return nfserr;
1da177e4
LT
4074}
4075
b37ad28b
AV
4076static __be32
4077nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
1da177e4
LT
4078{
4079 int maxcount;
476a7b1f
BF
4080 __be32 wire_count;
4081 int zero = 0;
ddd1ea56 4082 struct xdr_stream *xdr = &resp->xdr;
1fcea5b2 4083 int length_offset = xdr->buf->len;
76e5492b 4084 int status;
bc749ca4 4085 __be32 *p;
1da177e4 4086
2825a7f9
BF
4087 p = xdr_reserve_space(xdr, 4);
4088 if (!p)
4089 return nfserr_resource;
1da177e4 4090 maxcount = PAGE_SIZE;
d0a381dd 4091
476a7b1f
BF
4092 p = xdr_reserve_space(xdr, maxcount);
4093 if (!p)
4e21ac4b 4094 return nfserr_resource;
1da177e4 4095 /*
fd4a0edf
MS
4096 * XXX: By default, vfs_readlink() will truncate symlinks if they
4097 * would overflow the buffer. Is this kosher in NFSv4? If not, one
4098 * easy fix is: if vfs_readlink() precisely fills the buffer, assume
4099 * that truncation occurred, and return NFS4ERR_RESOURCE.
1da177e4 4100 */
476a7b1f
BF
4101 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp,
4102 (char *)p, &maxcount);
1da177e4 4103 if (nfserr == nfserr_isdir)
d3f627c8 4104 nfserr = nfserr_inval;
76e5492b
CL
4105 if (nfserr)
4106 goto out_err;
4107 status = svc_encode_result_payload(readlink->rl_rqstp, length_offset,
4108 maxcount);
4109 if (status) {
4110 nfserr = nfserrno(status);
4111 goto out_err;
d3f627c8 4112 }
1da177e4 4113
476a7b1f
BF
4114 wire_count = htonl(maxcount);
4115 write_bytes_to_xdr_buf(xdr->buf, length_offset, &wire_count, 4);
69bbd9c7 4116 xdr_truncate_encode(xdr, length_offset + 4 + ALIGN(maxcount, 4));
476a7b1f
BF
4117 if (maxcount & 3)
4118 write_bytes_to_xdr_buf(xdr->buf, length_offset + 4 + maxcount,
4119 &zero, 4 - (maxcount&3));
1da177e4 4120 return 0;
76e5492b
CL
4121
4122out_err:
4123 xdr_truncate_encode(xdr, length_offset);
4124 return nfserr;
1da177e4
LT
4125}
4126
b37ad28b
AV
4127static __be32
4128nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
1da177e4
LT
4129{
4130 int maxcount;
561f0ed4 4131 int bytes_left;
1da177e4 4132 loff_t offset;
561f0ed4 4133 __be64 wire_offset;
ddd1ea56 4134 struct xdr_stream *xdr = &resp->xdr;
1fcea5b2 4135 int starting_len = xdr->buf->len;
bc749ca4 4136 __be32 *p;
1da177e4 4137
d0a381dd
BF
4138 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
4139 if (!p)
4140 return nfserr_resource;
1da177e4
LT
4141
4142 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
c373b0a4
BF
4143 *p++ = cpu_to_be32(0);
4144 *p++ = cpu_to_be32(0);
4aea24b2
BF
4145 resp->xdr.buf->head[0].iov_len = ((char *)resp->xdr.p)
4146 - (char *)resp->xdr.buf->head[0].iov_base;
1da177e4
LT
4147
4148 /*
561f0ed4
BF
4149 * Number of bytes left for directory entries allowing for the
4150 * final 8 bytes of the readdir and a following failed op:
4151 */
4152 bytes_left = xdr->buf->buflen - xdr->buf->len
4153 - COMPOUND_ERR_SLACK_SPACE - 8;
4154 if (bytes_left < 0) {
4155 nfserr = nfserr_resource;
4156 goto err_no_verf;
4157 }
9c2ece6e
SM
4158 maxcount = svc_max_payload(resp->rqstp);
4159 maxcount = min_t(u32, readdir->rd_maxcount, maxcount);
561f0ed4
BF
4160 /*
4161 * Note the rfc defines rd_maxcount as the size of the
4162 * READDIR4resok structure, which includes the verifier above
4163 * and the 8 bytes encoded at the end of this function:
1da177e4 4164 */
561f0ed4
BF
4165 if (maxcount < 16) {
4166 nfserr = nfserr_toosmall;
1da177e4
LT
4167 goto err_no_verf;
4168 }
561f0ed4 4169 maxcount = min_t(int, maxcount-16, bytes_left);
1da177e4 4170
aee37764
BF
4171 /* RFC 3530 14.2.24 allows us to ignore dircount when it's 0: */
4172 if (!readdir->rd_dircount)
9c2ece6e 4173 readdir->rd_dircount = svc_max_payload(resp->rqstp);
aee37764 4174
561f0ed4
BF
4175 readdir->xdr = xdr;
4176 readdir->rd_maxcount = maxcount;
1da177e4 4177 readdir->common.err = 0;
561f0ed4 4178 readdir->cookie_offset = 0;
1da177e4
LT
4179
4180 offset = readdir->rd_cookie;
4181 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
4182 &offset,
4183 &readdir->common, nfsd4_encode_dirent);
4184 if (nfserr == nfs_ok &&
4185 readdir->common.err == nfserr_toosmall &&
561f0ed4
BF
4186 xdr->buf->len == starting_len + 8) {
4187 /* nothing encoded; which limit did we hit?: */
4188 if (maxcount - 16 < bytes_left)
4189 /* It was the fault of rd_maxcount: */
4190 nfserr = nfserr_toosmall;
4191 else
4192 /* We ran out of buffer space: */
4193 nfserr = nfserr_resource;
4194 }
1da177e4
LT
4195 if (nfserr)
4196 goto err_no_verf;
4197
561f0ed4
BF
4198 if (readdir->cookie_offset) {
4199 wire_offset = cpu_to_be64(offset);
4200 write_bytes_to_xdr_buf(xdr->buf, readdir->cookie_offset,
4201 &wire_offset, 8);
4202 }
1da177e4 4203
561f0ed4
BF
4204 p = xdr_reserve_space(xdr, 8);
4205 if (!p) {
4206 WARN_ON_ONCE(1);
4207 goto err_no_verf;
4208 }
1da177e4
LT
4209 *p++ = 0; /* no more entries */
4210 *p++ = htonl(readdir->common.err == nfserr_eof);
1da177e4
LT
4211
4212 return 0;
4213err_no_verf:
1fcea5b2 4214 xdr_truncate_encode(xdr, starting_len);
1da177e4
LT
4215 return nfserr;
4216}
4217
695e12f8 4218static __be32
b37ad28b 4219nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
1da177e4 4220{
d0a381dd 4221 struct xdr_stream *xdr = &resp->xdr;
bc749ca4 4222 __be32 *p;
1da177e4 4223
bac966d6
BF
4224 p = xdr_reserve_space(xdr, 20);
4225 if (!p)
4226 return nfserr_resource;
4227 p = encode_cinfo(p, &remove->rm_cinfo);
4228 return 0;
1da177e4
LT
4229}
4230
695e12f8 4231static __be32
b37ad28b 4232nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
1da177e4 4233{
d0a381dd 4234 struct xdr_stream *xdr = &resp->xdr;
bc749ca4 4235 __be32 *p;
1da177e4 4236
bac966d6
BF
4237 p = xdr_reserve_space(xdr, 40);
4238 if (!p)
4239 return nfserr_resource;
4240 p = encode_cinfo(p, &rename->rn_sinfo);
4241 p = encode_cinfo(p, &rename->rn_tinfo);
4242 return 0;
1da177e4
LT
4243}
4244
695e12f8 4245static __be32
bac966d6 4246nfsd4_do_encode_secinfo(struct xdr_stream *xdr, struct svc_export *exp)
dcb488a3 4247{
676e4ebd 4248 u32 i, nflavs, supported;
4796f457
BF
4249 struct exp_flavor_info *flavs;
4250 struct exp_flavor_info def_flavs[2];
676e4ebd
CL
4251 __be32 *p, *flavorsp;
4252 static bool report = true;
dcb488a3 4253
4796f457
BF
4254 if (exp->ex_nflavors) {
4255 flavs = exp->ex_flavors;
4256 nflavs = exp->ex_nflavors;
4257 } else { /* Handling of some defaults in absence of real secinfo: */
4258 flavs = def_flavs;
4259 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
4260 nflavs = 2;
4261 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
4262 flavs[1].pseudoflavor = RPC_AUTH_NULL;
4263 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
4264 nflavs = 1;
4265 flavs[0].pseudoflavor
4266 = svcauth_gss_flavor(exp->ex_client);
4267 } else {
4268 nflavs = 1;
4269 flavs[0].pseudoflavor
4270 = exp->ex_client->flavour->flavour;
4271 }
4272 }
4273
676e4ebd 4274 supported = 0;
d0a381dd
BF
4275 p = xdr_reserve_space(xdr, 4);
4276 if (!p)
bac966d6 4277 return nfserr_resource;
676e4ebd 4278 flavorsp = p++; /* to be backfilled later */
676e4ebd 4279
4796f457 4280 for (i = 0; i < nflavs; i++) {
676e4ebd 4281 rpc_authflavor_t pf = flavs[i].pseudoflavor;
a77c806f 4282 struct rpcsec_gss_info info;
dcb488a3 4283
676e4ebd
CL
4284 if (rpcauth_get_gssinfo(pf, &info) == 0) {
4285 supported++;
d0a381dd
BF
4286 p = xdr_reserve_space(xdr, 4 + 4 +
4287 XDR_LEN(info.oid.len) + 4 + 4);
4288 if (!p)
bac966d6 4289 return nfserr_resource;
c373b0a4 4290 *p++ = cpu_to_be32(RPC_AUTH_GSS);
0c0c267b 4291 p = xdr_encode_opaque(p, info.oid.data, info.oid.len);
c373b0a4
BF
4292 *p++ = cpu_to_be32(info.qop);
4293 *p++ = cpu_to_be32(info.service);
676e4ebd
CL
4294 } else if (pf < RPC_AUTH_MAXFLAVOR) {
4295 supported++;
d0a381dd
BF
4296 p = xdr_reserve_space(xdr, 4);
4297 if (!p)
bac966d6 4298 return nfserr_resource;
c373b0a4 4299 *p++ = cpu_to_be32(pf);
676e4ebd
CL
4300 } else {
4301 if (report)
4302 pr_warn("NFS: SECINFO: security flavor %u "
4303 "is not supported\n", pf);
dcb488a3
AA
4304 }
4305 }
a77c806f 4306
676e4ebd
CL
4307 if (nflavs != supported)
4308 report = false;
4309 *flavorsp = htonl(supported);
bac966d6 4310 return 0;
dcb488a3
AA
4311}
4312
22b6dee8
MJ
4313static __be32
4314nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
4315 struct nfsd4_secinfo *secinfo)
4316{
d0a381dd
BF
4317 struct xdr_stream *xdr = &resp->xdr;
4318
bac966d6 4319 return nfsd4_do_encode_secinfo(xdr, secinfo->si_exp);
22b6dee8
MJ
4320}
4321
4322static __be32
4323nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
4324 struct nfsd4_secinfo_no_name *secinfo)
4325{
d0a381dd
BF
4326 struct xdr_stream *xdr = &resp->xdr;
4327
bac966d6 4328 return nfsd4_do_encode_secinfo(xdr, secinfo->sin_exp);
22b6dee8
MJ
4329}
4330
1da177e4
LT
4331/*
4332 * The SETATTR encode routine is special -- it always encodes a bitmap,
4333 * regardless of the error status.
4334 */
695e12f8 4335static __be32
b37ad28b 4336nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
1da177e4 4337{
d0a381dd 4338 struct xdr_stream *xdr = &resp->xdr;
bc749ca4 4339 __be32 *p;
1da177e4 4340
d0a381dd
BF
4341 p = xdr_reserve_space(xdr, 16);
4342 if (!p)
4343 return nfserr_resource;
1da177e4 4344 if (nfserr) {
c373b0a4
BF
4345 *p++ = cpu_to_be32(3);
4346 *p++ = cpu_to_be32(0);
4347 *p++ = cpu_to_be32(0);
4348 *p++ = cpu_to_be32(0);
1da177e4
LT
4349 }
4350 else {
c373b0a4
BF
4351 *p++ = cpu_to_be32(3);
4352 *p++ = cpu_to_be32(setattr->sa_bmval[0]);
4353 *p++ = cpu_to_be32(setattr->sa_bmval[1]);
4354 *p++ = cpu_to_be32(setattr->sa_bmval[2]);
1da177e4 4355 }
695e12f8 4356 return nfserr;
1da177e4
LT
4357}
4358
695e12f8 4359static __be32
b37ad28b 4360nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
1da177e4 4361{
d0a381dd 4362 struct xdr_stream *xdr = &resp->xdr;
bc749ca4 4363 __be32 *p;
1da177e4
LT
4364
4365 if (!nfserr) {
d0a381dd
BF
4366 p = xdr_reserve_space(xdr, 8 + NFS4_VERIFIER_SIZE);
4367 if (!p)
4368 return nfserr_resource;
0c0c267b
BF
4369 p = xdr_encode_opaque_fixed(p, &scd->se_clientid, 8);
4370 p = xdr_encode_opaque_fixed(p, &scd->se_confirm,
4371 NFS4_VERIFIER_SIZE);
1da177e4
LT
4372 }
4373 else if (nfserr == nfserr_clid_inuse) {
d0a381dd
BF
4374 p = xdr_reserve_space(xdr, 8);
4375 if (!p)
4376 return nfserr_resource;
c373b0a4
BF
4377 *p++ = cpu_to_be32(0);
4378 *p++ = cpu_to_be32(0);
1da177e4 4379 }
695e12f8 4380 return nfserr;
1da177e4
LT
4381}
4382
695e12f8 4383static __be32
b37ad28b 4384nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
1da177e4 4385{
d0a381dd 4386 struct xdr_stream *xdr = &resp->xdr;
bc749ca4 4387 __be32 *p;
1da177e4 4388
bac966d6
BF
4389 p = xdr_reserve_space(xdr, 16);
4390 if (!p)
4391 return nfserr_resource;
4392 *p++ = cpu_to_be32(write->wr_bytes_written);
4393 *p++ = cpu_to_be32(write->wr_how_written);
4394 p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
4395 NFS4_VERIFIER_SIZE);
4396 return 0;
1da177e4
LT
4397}
4398
2db134eb 4399static __be32
57b7b43b 4400nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
2db134eb
AA
4401 struct nfsd4_exchange_id *exid)
4402{
d0a381dd 4403 struct xdr_stream *xdr = &resp->xdr;
bc749ca4 4404 __be32 *p;
0733d213
AA
4405 char *major_id;
4406 char *server_scope;
4407 int major_id_sz;
4408 int server_scope_sz;
4409 uint64_t minor_id = 0;
7627d7dc 4410 struct nfsd_net *nn = net_generic(SVC_NET(resp->rqstp), nfsd_net_id);
0733d213 4411
7627d7dc
SM
4412 major_id = nn->nfsd_name;
4413 major_id_sz = strlen(nn->nfsd_name);
4414 server_scope = nn->nfsd_name;
4415 server_scope_sz = strlen(nn->nfsd_name);
0733d213 4416
d0a381dd 4417 p = xdr_reserve_space(xdr,
0733d213
AA
4418 8 /* eir_clientid */ +
4419 4 /* eir_sequenceid */ +
4420 4 /* eir_flags */ +
a8bb84bc 4421 4 /* spr_how */);
d0a381dd
BF
4422 if (!p)
4423 return nfserr_resource;
0733d213 4424
0c0c267b 4425 p = xdr_encode_opaque_fixed(p, &exid->clientid, 8);
c373b0a4
BF
4426 *p++ = cpu_to_be32(exid->seqid);
4427 *p++ = cpu_to_be32(exid->flags);
0733d213 4428
c373b0a4 4429 *p++ = cpu_to_be32(exid->spa_how);
a8bb84bc 4430
57266a6e
BF
4431 switch (exid->spa_how) {
4432 case SP4_NONE:
4433 break;
4434 case SP4_MACH_CRED:
4435 /* spo_must_enforce bitmap: */
bac966d6 4436 nfserr = nfsd4_encode_bitmap(xdr,
ed941643
AE
4437 exid->spo_must_enforce[0],
4438 exid->spo_must_enforce[1],
4439 exid->spo_must_enforce[2]);
bac966d6
BF
4440 if (nfserr)
4441 return nfserr;
ed941643 4442 /* spo_must_allow bitmap: */
bac966d6 4443 nfserr = nfsd4_encode_bitmap(xdr,
ed941643
AE
4444 exid->spo_must_allow[0],
4445 exid->spo_must_allow[1],
4446 exid->spo_must_allow[2]);
bac966d6
BF
4447 if (nfserr)
4448 return nfserr;
57266a6e
BF
4449 break;
4450 default:
4451 WARN_ON_ONCE(1);
4452 }
0733d213 4453
d0a381dd 4454 p = xdr_reserve_space(xdr,
a8bb84bc
KM
4455 8 /* so_minor_id */ +
4456 4 /* so_major_id.len */ +
4457 (XDR_QUADLEN(major_id_sz) * 4) +
4458 4 /* eir_server_scope.len */ +
4459 (XDR_QUADLEN(server_scope_sz) * 4) +
4460 4 /* eir_server_impl_id.count (0) */);
d0a381dd
BF
4461 if (!p)
4462 return nfserr_resource;
a8bb84bc 4463
0733d213 4464 /* The server_owner struct */
b64c7f3b 4465 p = xdr_encode_hyper(p, minor_id); /* Minor id */
0733d213 4466 /* major id */
0c0c267b 4467 p = xdr_encode_opaque(p, major_id, major_id_sz);
0733d213
AA
4468
4469 /* Server scope */
0c0c267b 4470 p = xdr_encode_opaque(p, server_scope, server_scope_sz);
0733d213
AA
4471
4472 /* Implementation id */
c373b0a4 4473 *p++ = cpu_to_be32(0); /* zero length nfs_impl_id4 array */
0733d213 4474 return 0;
2db134eb
AA
4475}
4476
4477static __be32
57b7b43b 4478nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
2db134eb
AA
4479 struct nfsd4_create_session *sess)
4480{
d0a381dd 4481 struct xdr_stream *xdr = &resp->xdr;
bc749ca4 4482 __be32 *p;
ec6b5d7b 4483
d0a381dd
BF
4484 p = xdr_reserve_space(xdr, 24);
4485 if (!p)
4486 return nfserr_resource;
0c0c267b
BF
4487 p = xdr_encode_opaque_fixed(p, sess->sessionid.data,
4488 NFS4_MAX_SESSIONID_LEN);
c373b0a4
BF
4489 *p++ = cpu_to_be32(sess->seqid);
4490 *p++ = cpu_to_be32(sess->flags);
ec6b5d7b 4491
d0a381dd
BF
4492 p = xdr_reserve_space(xdr, 28);
4493 if (!p)
4494 return nfserr_resource;
c373b0a4
BF
4495 *p++ = cpu_to_be32(0); /* headerpadsz */
4496 *p++ = cpu_to_be32(sess->fore_channel.maxreq_sz);
4497 *p++ = cpu_to_be32(sess->fore_channel.maxresp_sz);
4498 *p++ = cpu_to_be32(sess->fore_channel.maxresp_cached);
4499 *p++ = cpu_to_be32(sess->fore_channel.maxops);
4500 *p++ = cpu_to_be32(sess->fore_channel.maxreqs);
4501 *p++ = cpu_to_be32(sess->fore_channel.nr_rdma_attrs);
ec6b5d7b
AA
4502
4503 if (sess->fore_channel.nr_rdma_attrs) {
d0a381dd
BF
4504 p = xdr_reserve_space(xdr, 4);
4505 if (!p)
4506 return nfserr_resource;
c373b0a4 4507 *p++ = cpu_to_be32(sess->fore_channel.rdma_attrs);
ec6b5d7b
AA
4508 }
4509
d0a381dd
BF
4510 p = xdr_reserve_space(xdr, 28);
4511 if (!p)
4512 return nfserr_resource;
c373b0a4
BF
4513 *p++ = cpu_to_be32(0); /* headerpadsz */
4514 *p++ = cpu_to_be32(sess->back_channel.maxreq_sz);
4515 *p++ = cpu_to_be32(sess->back_channel.maxresp_sz);
4516 *p++ = cpu_to_be32(sess->back_channel.maxresp_cached);
4517 *p++ = cpu_to_be32(sess->back_channel.maxops);
4518 *p++ = cpu_to_be32(sess->back_channel.maxreqs);
4519 *p++ = cpu_to_be32(sess->back_channel.nr_rdma_attrs);
ec6b5d7b
AA
4520
4521 if (sess->back_channel.nr_rdma_attrs) {
d0a381dd
BF
4522 p = xdr_reserve_space(xdr, 4);
4523 if (!p)
4524 return nfserr_resource;
c373b0a4 4525 *p++ = cpu_to_be32(sess->back_channel.rdma_attrs);
ec6b5d7b
AA
4526 }
4527 return 0;
2db134eb
AA
4528}
4529
c47d832b 4530static __be32
57b7b43b 4531nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
2db134eb
AA
4532 struct nfsd4_sequence *seq)
4533{
d0a381dd 4534 struct xdr_stream *xdr = &resp->xdr;
bc749ca4 4535 __be32 *p;
b85d4c01 4536
d0a381dd
BF
4537 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 20);
4538 if (!p)
4539 return nfserr_resource;
0c0c267b
BF
4540 p = xdr_encode_opaque_fixed(p, seq->sessionid.data,
4541 NFS4_MAX_SESSIONID_LEN);
c373b0a4
BF
4542 *p++ = cpu_to_be32(seq->seqid);
4543 *p++ = cpu_to_be32(seq->slotid);
b7d7ca35 4544 /* Note slotid's are numbered from zero: */
c373b0a4
BF
4545 *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_highest_slotid */
4546 *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_target_highest_slotid */
4547 *p++ = cpu_to_be32(seq->status_flags);
b85d4c01 4548
f5236013 4549 resp->cstate.data_offset = xdr->buf->len; /* DRC cache data pointer */
b85d4c01 4550 return 0;
2db134eb
AA
4551}
4552
2355c596 4553static __be32
57b7b43b 4554nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
17456804
BS
4555 struct nfsd4_test_stateid *test_stateid)
4556{
d0a381dd 4557 struct xdr_stream *xdr = &resp->xdr;
03cfb420 4558 struct nfsd4_test_stateid_id *stateid, *next;
17456804 4559 __be32 *p;
17456804 4560
d0a381dd
BF
4561 p = xdr_reserve_space(xdr, 4 + (4 * test_stateid->ts_num_ids));
4562 if (!p)
4563 return nfserr_resource;
17456804 4564 *p++ = htonl(test_stateid->ts_num_ids);
17456804 4565
03cfb420 4566 list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
02f5fde5 4567 *p++ = stateid->ts_id_status;
17456804 4568 }
17456804 4569
bac966d6 4570 return 0;
17456804
BS
4571}
4572
9cf514cc
CH
4573#ifdef CONFIG_NFSD_PNFS
4574static __be32
4575nfsd4_encode_getdeviceinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
4576 struct nfsd4_getdeviceinfo *gdev)
4577{
4578 struct xdr_stream *xdr = &resp->xdr;
f961e3f2 4579 const struct nfsd4_layout_ops *ops;
9cf514cc
CH
4580 u32 starting_len = xdr->buf->len, needed_len;
4581 __be32 *p;
4582
9cf514cc
CH
4583 p = xdr_reserve_space(xdr, 4);
4584 if (!p)
bac966d6 4585 return nfserr_resource;
9cf514cc
CH
4586
4587 *p++ = cpu_to_be32(gdev->gd_layout_type);
4588
4589 /* If maxcount is 0 then just update notifications */
4590 if (gdev->gd_maxcount != 0) {
f961e3f2 4591 ops = nfsd4_layout_ops[gdev->gd_layout_type];
9cf514cc
CH
4592 nfserr = ops->encode_getdeviceinfo(xdr, gdev);
4593 if (nfserr) {
4594 /*
4595 * We don't bother to burden the layout drivers with
4596 * enforcing gd_maxcount, just tell the client to
4597 * come back with a bigger buffer if it's not enough.
4598 */
4599 if (xdr->buf->len + 4 > gdev->gd_maxcount)
4600 goto toosmall;
bac966d6 4601 return nfserr;
9cf514cc
CH
4602 }
4603 }
4604
9cf514cc
CH
4605 if (gdev->gd_notify_types) {
4606 p = xdr_reserve_space(xdr, 4 + 4);
4607 if (!p)
bac966d6 4608 return nfserr_resource;
9cf514cc
CH
4609 *p++ = cpu_to_be32(1); /* bitmap length */
4610 *p++ = cpu_to_be32(gdev->gd_notify_types);
4611 } else {
4612 p = xdr_reserve_space(xdr, 4);
4613 if (!p)
bac966d6 4614 return nfserr_resource;
9cf514cc
CH
4615 *p++ = 0;
4616 }
4617
bac966d6 4618 return 0;
9cf514cc
CH
4619toosmall:
4620 dprintk("%s: maxcount too small\n", __func__);
4621 needed_len = xdr->buf->len + 4 /* notifications */;
4622 xdr_truncate_encode(xdr, starting_len);
4623 p = xdr_reserve_space(xdr, 4);
bac966d6
BF
4624 if (!p)
4625 return nfserr_resource;
4626 *p++ = cpu_to_be32(needed_len);
4627 return nfserr_toosmall;
9cf514cc
CH
4628}
4629
4630static __be32
4631nfsd4_encode_layoutget(struct nfsd4_compoundres *resp, __be32 nfserr,
4632 struct nfsd4_layoutget *lgp)
4633{
4634 struct xdr_stream *xdr = &resp->xdr;
f961e3f2 4635 const struct nfsd4_layout_ops *ops;
9cf514cc
CH
4636 __be32 *p;
4637
9cf514cc
CH
4638 p = xdr_reserve_space(xdr, 36 + sizeof(stateid_opaque_t));
4639 if (!p)
bac966d6 4640 return nfserr_resource;
9cf514cc
CH
4641
4642 *p++ = cpu_to_be32(1); /* we always set return-on-close */
4643 *p++ = cpu_to_be32(lgp->lg_sid.si_generation);
4644 p = xdr_encode_opaque_fixed(p, &lgp->lg_sid.si_opaque,
4645 sizeof(stateid_opaque_t));
4646
4647 *p++ = cpu_to_be32(1); /* we always return a single layout */
4648 p = xdr_encode_hyper(p, lgp->lg_seg.offset);
4649 p = xdr_encode_hyper(p, lgp->lg_seg.length);
4650 *p++ = cpu_to_be32(lgp->lg_seg.iomode);
4651 *p++ = cpu_to_be32(lgp->lg_layout_type);
4652
f961e3f2 4653 ops = nfsd4_layout_ops[lgp->lg_layout_type];
bac966d6 4654 return ops->encode_layoutget(xdr, lgp);
9cf514cc
CH
4655}
4656
4657static __be32
4658nfsd4_encode_layoutcommit(struct nfsd4_compoundres *resp, __be32 nfserr,
4659 struct nfsd4_layoutcommit *lcp)
4660{
4661 struct xdr_stream *xdr = &resp->xdr;
4662 __be32 *p;
4663
9cf514cc
CH
4664 p = xdr_reserve_space(xdr, 4);
4665 if (!p)
4666 return nfserr_resource;
4667 *p++ = cpu_to_be32(lcp->lc_size_chg);
4668 if (lcp->lc_size_chg) {
4669 p = xdr_reserve_space(xdr, 8);
4670 if (!p)
4671 return nfserr_resource;
4672 p = xdr_encode_hyper(p, lcp->lc_newsize);
4673 }
4674
bac966d6 4675 return 0;
9cf514cc
CH
4676}
4677
4678static __be32
4679nfsd4_encode_layoutreturn(struct nfsd4_compoundres *resp, __be32 nfserr,
4680 struct nfsd4_layoutreturn *lrp)
4681{
4682 struct xdr_stream *xdr = &resp->xdr;
4683 __be32 *p;
4684
9cf514cc
CH
4685 p = xdr_reserve_space(xdr, 4);
4686 if (!p)
4687 return nfserr_resource;
4688 *p++ = cpu_to_be32(lrp->lrs_present);
4689 if (lrp->lrs_present)
376675da 4690 return nfsd4_encode_stateid(xdr, &lrp->lr_sid);
bac966d6 4691 return 0;
9cf514cc
CH
4692}
4693#endif /* CONFIG_NFSD_PNFS */
4694
29ae7f9d 4695static __be32
e0639dc5
OK
4696nfsd42_encode_write_res(struct nfsd4_compoundres *resp,
4697 struct nfsd42_write_res *write, bool sync)
29ae7f9d
AS
4698{
4699 __be32 *p;
e0639dc5
OK
4700 p = xdr_reserve_space(&resp->xdr, 4);
4701 if (!p)
4702 return nfserr_resource;
29ae7f9d 4703
e0639dc5
OK
4704 if (sync)
4705 *p++ = cpu_to_be32(0);
4706 else {
4707 __be32 nfserr;
4708 *p++ = cpu_to_be32(1);
4709 nfserr = nfsd4_encode_stateid(&resp->xdr, &write->cb_stateid);
4710 if (nfserr)
4711 return nfserr;
4712 }
4713 p = xdr_reserve_space(&resp->xdr, 8 + 4 + NFS4_VERIFIER_SIZE);
29ae7f9d
AS
4714 if (!p)
4715 return nfserr_resource;
4716
29ae7f9d
AS
4717 p = xdr_encode_hyper(p, write->wr_bytes_written);
4718 *p++ = cpu_to_be32(write->wr_stable_how);
4719 p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
4720 NFS4_VERIFIER_SIZE);
4721 return nfs_ok;
4722}
4723
51911868
OK
4724static __be32
4725nfsd42_encode_nl4_server(struct nfsd4_compoundres *resp, struct nl4_server *ns)
4726{
4727 struct xdr_stream *xdr = &resp->xdr;
4728 struct nfs42_netaddr *addr;
4729 __be32 *p;
4730
4731 p = xdr_reserve_space(xdr, 4);
4732 *p++ = cpu_to_be32(ns->nl4_type);
4733
4734 switch (ns->nl4_type) {
4735 case NL4_NETADDR:
4736 addr = &ns->u.nl4_addr;
4737
4738 /* netid_len, netid, uaddr_len, uaddr (port included
4739 * in RPCBIND_MAXUADDRLEN)
4740 */
4741 p = xdr_reserve_space(xdr,
4742 4 /* netid len */ +
4743 (XDR_QUADLEN(addr->netid_len) * 4) +
4744 4 /* uaddr len */ +
4745 (XDR_QUADLEN(addr->addr_len) * 4));
4746 if (!p)
4747 return nfserr_resource;
4748
4749 *p++ = cpu_to_be32(addr->netid_len);
4750 p = xdr_encode_opaque_fixed(p, addr->netid,
4751 addr->netid_len);
4752 *p++ = cpu_to_be32(addr->addr_len);
4753 p = xdr_encode_opaque_fixed(p, addr->addr,
4754 addr->addr_len);
4755 break;
4756 default:
4757 WARN_ON_ONCE(ns->nl4_type != NL4_NETADDR);
4758 return nfserr_inval;
4759 }
4760
4761 return 0;
4762}
4763
29ae7f9d
AS
4764static __be32
4765nfsd4_encode_copy(struct nfsd4_compoundres *resp, __be32 nfserr,
4766 struct nfsd4_copy *copy)
4767{
4768 __be32 *p;
4769
e0639dc5
OK
4770 nfserr = nfsd42_encode_write_res(resp, &copy->cp_res,
4771 copy->cp_synchronous);
bac966d6
BF
4772 if (nfserr)
4773 return nfserr;
29ae7f9d 4774
bac966d6 4775 p = xdr_reserve_space(&resp->xdr, 4 + 4);
edcc8452 4776 *p++ = xdr_one; /* cr_consecutive */
bac966d6
BF
4777 *p++ = cpu_to_be32(copy->cp_synchronous);
4778 return 0;
29ae7f9d
AS
4779}
4780
6308bc98
OK
4781static __be32
4782nfsd4_encode_offload_status(struct nfsd4_compoundres *resp, __be32 nfserr,
4783 struct nfsd4_offload_status *os)
4784{
4785 struct xdr_stream *xdr = &resp->xdr;
4786 __be32 *p;
4787
4788 p = xdr_reserve_space(xdr, 8 + 4);
4789 if (!p)
4790 return nfserr_resource;
4791 p = xdr_encode_hyper(p, os->count);
4792 *p++ = cpu_to_be32(0);
528b8493
AS
4793 return nfserr;
4794}
4795
4796static __be32
4797nfsd4_encode_read_plus_data(struct nfsd4_compoundres *resp,
4798 struct nfsd4_read *read,
9f0b5792
AS
4799 unsigned long *maxcount, u32 *eof,
4800 loff_t *pos)
528b8493
AS
4801{
4802 struct xdr_stream *xdr = &resp->xdr;
4803 struct file *file = read->rd_nf->nf_file;
4804 int starting_len = xdr->buf->len;
9f0b5792 4805 loff_t hole_pos;
528b8493
AS
4806 __be32 nfserr;
4807 __be32 *p, tmp;
4808 __be64 tmp64;
4809
9f0b5792 4810 hole_pos = pos ? *pos : vfs_llseek(file, read->rd_offset, SEEK_HOLE);
2db27992 4811 if (hole_pos > read->rd_offset)
278765ea
AS
4812 *maxcount = min_t(unsigned long, *maxcount, hole_pos - read->rd_offset);
4813 *maxcount = min_t(unsigned long, *maxcount, (xdr->buf->buflen - xdr->buf->len));
528b8493
AS
4814
4815 /* Content type, offset, byte count */
4816 p = xdr_reserve_space(xdr, 4 + 8 + 4);
4817 if (!p)
4818 return nfserr_resource;
4819
278765ea 4820 read->rd_vlen = xdr_reserve_space_vec(xdr, resp->rqstp->rq_vec, *maxcount);
528b8493
AS
4821 if (read->rd_vlen < 0)
4822 return nfserr_resource;
4823
4824 nfserr = nfsd_readv(resp->rqstp, read->rd_fhp, file, read->rd_offset,
278765ea 4825 resp->rqstp->rq_vec, read->rd_vlen, maxcount, eof);
528b8493
AS
4826 if (nfserr)
4827 return nfserr;
4828
4829 tmp = htonl(NFS4_CONTENT_DATA);
4830 write_bytes_to_xdr_buf(xdr->buf, starting_len, &tmp, 4);
4831 tmp64 = cpu_to_be64(read->rd_offset);
4832 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp64, 8);
278765ea 4833 tmp = htonl(*maxcount);
528b8493
AS
4834 write_bytes_to_xdr_buf(xdr->buf, starting_len + 12, &tmp, 4);
4835 return nfs_ok;
4836}
4837
2db27992
AS
4838static __be32
4839nfsd4_encode_read_plus_hole(struct nfsd4_compoundres *resp,
4840 struct nfsd4_read *read,
278765ea 4841 unsigned long *maxcount, u32 *eof)
2db27992
AS
4842{
4843 struct file *file = read->rd_nf->nf_file;
278765ea 4844 loff_t data_pos = vfs_llseek(file, read->rd_offset, SEEK_DATA);
9f0b5792 4845 loff_t f_size = i_size_read(file_inode(file));
278765ea 4846 unsigned long count;
2db27992
AS
4847 __be32 *p;
4848
278765ea 4849 if (data_pos == -ENXIO)
9f0b5792
AS
4850 data_pos = f_size;
4851 else if (data_pos <= read->rd_offset || (data_pos < f_size && data_pos % PAGE_SIZE))
4852 return nfsd4_encode_read_plus_data(resp, read, maxcount, eof, &f_size);
278765ea
AS
4853 count = data_pos - read->rd_offset;
4854
2db27992
AS
4855 /* Content type, offset, byte count */
4856 p = xdr_reserve_space(&resp->xdr, 4 + 8 + 8);
4857 if (!p)
4858 return nfserr_resource;
4859
4860 *p++ = htonl(NFS4_CONTENT_HOLE);
4861 p = xdr_encode_hyper(p, read->rd_offset);
278765ea 4862 p = xdr_encode_hyper(p, count);
2db27992 4863
9f0b5792 4864 *eof = (read->rd_offset + count) >= f_size;
278765ea 4865 *maxcount = min_t(unsigned long, count, *maxcount);
2db27992
AS
4866 return nfs_ok;
4867}
4868
528b8493
AS
4869static __be32
4870nfsd4_encode_read_plus(struct nfsd4_compoundres *resp, __be32 nfserr,
4871 struct nfsd4_read *read)
4872{
278765ea 4873 unsigned long maxcount, count;
528b8493
AS
4874 struct xdr_stream *xdr = &resp->xdr;
4875 struct file *file;
4876 int starting_len = xdr->buf->len;
9f0b5792 4877 int last_segment = xdr->buf->len;
528b8493
AS
4878 int segments = 0;
4879 __be32 *p, tmp;
9f0b5792 4880 bool is_data;
2db27992 4881 loff_t pos;
528b8493
AS
4882 u32 eof;
4883
4884 if (nfserr)
4885 return nfserr;
4886 file = read->rd_nf->nf_file;
4887
4888 /* eof flag, segment count */
4889 p = xdr_reserve_space(xdr, 4 + 4);
4890 if (!p)
4891 return nfserr_resource;
4892 xdr_commit_encode(xdr);
4893
4894 maxcount = svc_max_payload(resp->rqstp);
4895 maxcount = min_t(unsigned long, maxcount,
4896 (xdr->buf->buflen - xdr->buf->len));
4897 maxcount = min_t(unsigned long, maxcount, read->rd_length);
278765ea 4898 count = maxcount;
528b8493
AS
4899
4900 eof = read->rd_offset >= i_size_read(file_inode(file));
2db27992
AS
4901 if (eof)
4902 goto out;
4903
9f0b5792
AS
4904 pos = vfs_llseek(file, read->rd_offset, SEEK_HOLE);
4905 is_data = pos > read->rd_offset;
2db27992 4906
9f0b5792 4907 while (count > 0 && !eof) {
278765ea 4908 maxcount = count;
9f0b5792
AS
4909 if (is_data)
4910 nfserr = nfsd4_encode_read_plus_data(resp, read, &maxcount, &eof,
4911 segments == 0 ? &pos : NULL);
4912 else
4913 nfserr = nfsd4_encode_read_plus_hole(resp, read, &maxcount, &eof);
278765ea
AS
4914 if (nfserr)
4915 goto out;
4916 count -= maxcount;
4917 read->rd_offset += maxcount;
9f0b5792
AS
4918 is_data = !is_data;
4919 last_segment = xdr->buf->len;
528b8493
AS
4920 segments++;
4921 }
4922
2db27992 4923out:
278765ea 4924 if (nfserr && segments == 0)
528b8493
AS
4925 xdr_truncate_encode(xdr, starting_len);
4926 else {
4927 tmp = htonl(eof);
4928 write_bytes_to_xdr_buf(xdr->buf, starting_len, &tmp, 4);
4929 tmp = htonl(segments);
4930 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp, 4);
9f0b5792
AS
4931 if (nfserr) {
4932 xdr_truncate_encode(xdr, last_segment);
4933 nfserr = nfs_ok;
4934 }
528b8493 4935 }
6308bc98
OK
4936
4937 return nfserr;
4938}
4939
51911868
OK
4940static __be32
4941nfsd4_encode_copy_notify(struct nfsd4_compoundres *resp, __be32 nfserr,
4942 struct nfsd4_copy_notify *cn)
4943{
4944 struct xdr_stream *xdr = &resp->xdr;
4945 __be32 *p;
4946
4947 if (nfserr)
4948 return nfserr;
4949
4950 /* 8 sec, 4 nsec */
4951 p = xdr_reserve_space(xdr, 12);
4952 if (!p)
4953 return nfserr_resource;
4954
4955 /* cnr_lease_time */
4956 p = xdr_encode_hyper(p, cn->cpn_sec);
4957 *p++ = cpu_to_be32(cn->cpn_nsec);
4958
4959 /* cnr_stateid */
4960 nfserr = nfsd4_encode_stateid(xdr, &cn->cpn_cnr_stateid);
4961 if (nfserr)
4962 return nfserr;
4963
4964 /* cnr_src.nl_nsvr */
4965 p = xdr_reserve_space(xdr, 4);
4966 if (!p)
4967 return nfserr_resource;
4968
4969 *p++ = cpu_to_be32(1);
4970
4971 return nfsd42_encode_nl4_server(resp, &cn->cpn_src);
4972}
4973
24bab491
AS
4974static __be32
4975nfsd4_encode_seek(struct nfsd4_compoundres *resp, __be32 nfserr,
4976 struct nfsd4_seek *seek)
4977{
4978 __be32 *p;
4979
24bab491
AS
4980 p = xdr_reserve_space(&resp->xdr, 4 + 8);
4981 *p++ = cpu_to_be32(seek->seek_eof);
4982 p = xdr_encode_hyper(p, seek->seek_pos);
4983
bac966d6 4984 return 0;
24bab491
AS
4985}
4986
695e12f8
BH
4987static __be32
4988nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
4989{
4990 return nfserr;
4991}
4992
23e50fe3
FL
4993/*
4994 * Encode kmalloc-ed buffer in to XDR stream.
4995 */
b9a49237 4996static __be32
23e50fe3
FL
4997nfsd4_vbuf_to_stream(struct xdr_stream *xdr, char *buf, u32 buflen)
4998{
4999 u32 cplen;
5000 __be32 *p;
5001
5002 cplen = min_t(unsigned long, buflen,
5003 ((void *)xdr->end - (void *)xdr->p));
5004 p = xdr_reserve_space(xdr, cplen);
5005 if (!p)
5006 return nfserr_resource;
5007
5008 memcpy(p, buf, cplen);
5009 buf += cplen;
5010 buflen -= cplen;
5011
5012 while (buflen) {
5013 cplen = min_t(u32, buflen, PAGE_SIZE);
5014 p = xdr_reserve_space(xdr, cplen);
5015 if (!p)
5016 return nfserr_resource;
5017
5018 memcpy(p, buf, cplen);
5019
5020 if (cplen < PAGE_SIZE) {
5021 /*
5022 * We're done, with a length that wasn't page
5023 * aligned, so possibly not word aligned. Pad
5024 * any trailing bytes with 0.
5025 */
5026 xdr_encode_opaque_fixed(p, NULL, cplen);
5027 break;
5028 }
5029
5030 buflen -= PAGE_SIZE;
5031 buf += PAGE_SIZE;
5032 }
5033
5034 return 0;
5035}
5036
5037static __be32
5038nfsd4_encode_getxattr(struct nfsd4_compoundres *resp, __be32 nfserr,
5039 struct nfsd4_getxattr *getxattr)
5040{
5041 struct xdr_stream *xdr = &resp->xdr;
5042 __be32 *p, err;
5043
5044 p = xdr_reserve_space(xdr, 4);
5045 if (!p)
5046 return nfserr_resource;
5047
5048 *p = cpu_to_be32(getxattr->getxa_len);
5049
5050 if (getxattr->getxa_len == 0)
5051 return 0;
5052
5053 err = nfsd4_vbuf_to_stream(xdr, getxattr->getxa_buf,
5054 getxattr->getxa_len);
5055
5056 kvfree(getxattr->getxa_buf);
5057
5058 return err;
5059}
5060
5061static __be32
5062nfsd4_encode_setxattr(struct nfsd4_compoundres *resp, __be32 nfserr,
5063 struct nfsd4_setxattr *setxattr)
5064{
5065 struct xdr_stream *xdr = &resp->xdr;
5066 __be32 *p;
5067
5068 p = xdr_reserve_space(xdr, 20);
5069 if (!p)
5070 return nfserr_resource;
5071
5072 encode_cinfo(p, &setxattr->setxa_cinfo);
5073
5074 return 0;
5075}
5076
5077/*
5078 * See if there are cookie values that can be rejected outright.
5079 */
5080static __be32
5081nfsd4_listxattr_validate_cookie(struct nfsd4_listxattrs *listxattrs,
5082 u32 *offsetp)
5083{
5084 u64 cookie = listxattrs->lsxa_cookie;
5085
5086 /*
5087 * If the cookie is larger than the maximum number we can fit
5088 * in either the buffer we just got back from vfs_listxattr, or,
5089 * XDR-encoded, in the return buffer, it's invalid.
5090 */
5091 if (cookie > (listxattrs->lsxa_len) / (XATTR_USER_PREFIX_LEN + 2))
5092 return nfserr_badcookie;
5093
5094 if (cookie > (listxattrs->lsxa_maxcount /
5095 (XDR_QUADLEN(XATTR_USER_PREFIX_LEN + 2) + 4)))
5096 return nfserr_badcookie;
5097
5098 *offsetp = (u32)cookie;
5099 return 0;
5100}
5101
5102static __be32
5103nfsd4_encode_listxattrs(struct nfsd4_compoundres *resp, __be32 nfserr,
5104 struct nfsd4_listxattrs *listxattrs)
5105{
5106 struct xdr_stream *xdr = &resp->xdr;
5107 u32 cookie_offset, count_offset, eof;
5108 u32 left, xdrleft, slen, count;
5109 u32 xdrlen, offset;
5110 u64 cookie;
5111 char *sp;
b9a49237 5112 __be32 status, tmp;
23e50fe3
FL
5113 __be32 *p;
5114 u32 nuser;
5115
5116 eof = 1;
5117
5118 status = nfsd4_listxattr_validate_cookie(listxattrs, &offset);
5119 if (status)
5120 goto out;
5121
5122 /*
5123 * Reserve space for the cookie and the name array count. Record
5124 * the offsets to save them later.
5125 */
5126 cookie_offset = xdr->buf->len;
5127 count_offset = cookie_offset + 8;
5128 p = xdr_reserve_space(xdr, 12);
5129 if (!p) {
5130 status = nfserr_resource;
5131 goto out;
5132 }
5133
5134 count = 0;
5135 left = listxattrs->lsxa_len;
5136 sp = listxattrs->lsxa_buf;
5137 nuser = 0;
5138
5139 xdrleft = listxattrs->lsxa_maxcount;
5140
5141 while (left > 0 && xdrleft > 0) {
5142 slen = strlen(sp);
5143
5144 /*
4cce11fa 5145 * Check if this is a "user." attribute, skip it if not.
23e50fe3
FL
5146 */
5147 if (strncmp(sp, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
5148 goto contloop;
5149
5150 slen -= XATTR_USER_PREFIX_LEN;
5151 xdrlen = 4 + ((slen + 3) & ~3);
5152 if (xdrlen > xdrleft) {
5153 if (count == 0) {
5154 /*
5155 * Can't even fit the first attribute name.
5156 */
5157 status = nfserr_toosmall;
5158 goto out;
5159 }
5160 eof = 0;
5161 goto wreof;
5162 }
5163
5164 left -= XATTR_USER_PREFIX_LEN;
5165 sp += XATTR_USER_PREFIX_LEN;
5166 if (nuser++ < offset)
5167 goto contloop;
5168
5169
5170 p = xdr_reserve_space(xdr, xdrlen);
5171 if (!p) {
5172 status = nfserr_resource;
5173 goto out;
5174 }
5175
e2a1840e 5176 xdr_encode_opaque(p, sp, slen);
23e50fe3
FL
5177
5178 xdrleft -= xdrlen;
5179 count++;
5180contloop:
5181 sp += slen + 1;
5182 left -= slen + 1;
5183 }
5184
5185 /*
5186 * If there were user attributes to copy, but we didn't copy
5187 * any, the offset was too large (e.g. the cookie was invalid).
5188 */
5189 if (nuser > 0 && count == 0) {
5190 status = nfserr_badcookie;
5191 goto out;
5192 }
5193
5194wreof:
5195 p = xdr_reserve_space(xdr, 4);
5196 if (!p) {
5197 status = nfserr_resource;
5198 goto out;
5199 }
5200 *p = cpu_to_be32(eof);
5201
5202 cookie = offset + count;
5203
5204 write_bytes_to_xdr_buf(xdr->buf, cookie_offset, &cookie, 8);
b9a49237
CL
5205 tmp = cpu_to_be32(count);
5206 write_bytes_to_xdr_buf(xdr->buf, count_offset, &tmp, 4);
23e50fe3
FL
5207out:
5208 if (listxattrs->lsxa_len)
5209 kvfree(listxattrs->lsxa_buf);
5210 return status;
5211}
5212
5213static __be32
5214nfsd4_encode_removexattr(struct nfsd4_compoundres *resp, __be32 nfserr,
5215 struct nfsd4_removexattr *removexattr)
5216{
5217 struct xdr_stream *xdr = &resp->xdr;
5218 __be32 *p;
5219
5220 p = xdr_reserve_space(xdr, 20);
5221 if (!p)
5222 return nfserr_resource;
5223
5224 p = encode_cinfo(p, &removexattr->rmxa_cinfo);
5225 return 0;
5226}
5227
695e12f8
BH
5228typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
5229
2db134eb
AA
5230/*
5231 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
5232 * since we don't need to filter out obsolete ops as this is
5233 * done in the decoding phase.
5234 */
c1df609d 5235static const nfsd4_enc nfsd4_enc_ops[] = {
ad1060c8
BF
5236 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
5237 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
5238 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
5239 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
5240 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
5241 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
5242 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
5243 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
5244 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
5245 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
5246 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
5247 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
5248 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
5249 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
5250 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
5251 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
84f09f46 5252 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
ad1060c8
BF
5253 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
5254 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
5255 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
5256 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
5257 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
5258 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
5259 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
5260 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
5261 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
5262 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
5263 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
5264 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
5265 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
5266 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
5267 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
5268 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
5269 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
5270 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
5271 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
5272 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
2db134eb
AA
5273
5274 /* NFSv4.1 operations */
5275 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
1d1bc8f2 5276 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
2db134eb
AA
5277 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
5278 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
43212cc7
KM
5279 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_noop,
5280 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
2db134eb 5281 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
9cf514cc
CH
5282#ifdef CONFIG_NFSD_PNFS
5283 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_getdeviceinfo,
5284 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
5285 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_layoutcommit,
5286 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_layoutget,
5287 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_layoutreturn,
5288#else
2db134eb
AA
5289 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
5290 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
5291 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
5292 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
5293 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
9cf514cc 5294#endif
22b6dee8 5295 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
2db134eb
AA
5296 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
5297 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
17456804 5298 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
2db134eb
AA
5299 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
5300 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
5301 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
87a15a80
AS
5302
5303 /* NFSv4.2 operations */
5304 [OP_ALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
29ae7f9d 5305 [OP_COPY] = (nfsd4_enc)nfsd4_encode_copy,
51911868 5306 [OP_COPY_NOTIFY] = (nfsd4_enc)nfsd4_encode_copy_notify,
87a15a80
AS
5307 [OP_DEALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
5308 [OP_IO_ADVISE] = (nfsd4_enc)nfsd4_encode_noop,
5309 [OP_LAYOUTERROR] = (nfsd4_enc)nfsd4_encode_noop,
5310 [OP_LAYOUTSTATS] = (nfsd4_enc)nfsd4_encode_noop,
5311 [OP_OFFLOAD_CANCEL] = (nfsd4_enc)nfsd4_encode_noop,
6308bc98 5312 [OP_OFFLOAD_STATUS] = (nfsd4_enc)nfsd4_encode_offload_status,
528b8493 5313 [OP_READ_PLUS] = (nfsd4_enc)nfsd4_encode_read_plus,
24bab491 5314 [OP_SEEK] = (nfsd4_enc)nfsd4_encode_seek,
87a15a80 5315 [OP_WRITE_SAME] = (nfsd4_enc)nfsd4_encode_noop,
ffa0160a 5316 [OP_CLONE] = (nfsd4_enc)nfsd4_encode_noop,
23e50fe3
FL
5317
5318 /* RFC 8276 extended atributes operations */
5319 [OP_GETXATTR] = (nfsd4_enc)nfsd4_encode_getxattr,
5320 [OP_SETXATTR] = (nfsd4_enc)nfsd4_encode_setxattr,
5321 [OP_LISTXATTRS] = (nfsd4_enc)nfsd4_encode_listxattrs,
5322 [OP_REMOVEXATTR] = (nfsd4_enc)nfsd4_encode_removexattr,
695e12f8
BH
5323};
5324
496c262c 5325/*
a8095f7e
BF
5326 * Calculate whether we still have space to encode repsize bytes.
5327 * There are two considerations:
5328 * - For NFS versions >=4.1, the size of the reply must stay within
5329 * session limits
5330 * - For all NFS versions, we must stay within limited preallocated
5331 * buffer space.
496c262c 5332 *
a8095f7e
BF
5333 * This is called before the operation is processed, so can only provide
5334 * an upper estimate. For some nonidempotent operations (such as
5335 * getattr), it's not necessarily a problem if that estimate is wrong,
5336 * as we can fail it after processing without significant side effects.
496c262c 5337 */
a8095f7e 5338__be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 respsize)
496c262c 5339{
67492c99 5340 struct xdr_buf *buf = &resp->rqstp->rq_res;
47ee5298 5341 struct nfsd4_slot *slot = resp->cstate.slot;
496c262c 5342
47ee5298
BF
5343 if (buf->len + respsize <= buf->buflen)
5344 return nfs_ok;
5345 if (!nfsd4_has_session(&resp->cstate))
ea8d7720 5346 return nfserr_resource;
47ee5298
BF
5347 if (slot->sl_flags & NFSD4_SLOT_CACHETHIS) {
5348 WARN_ON_ONCE(1);
5349 return nfserr_rep_too_big_to_cache;
ea8d7720 5350 }
47ee5298 5351 return nfserr_rep_too_big;
496c262c
AA
5352}
5353
1da177e4
LT
5354void
5355nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
5356{
082d4bd7 5357 struct xdr_stream *xdr = &resp->xdr;
9411b1d4 5358 struct nfs4_stateowner *so = resp->cstate.replay_owner;
5f4ab945 5359 struct svc_rqst *rqstp = resp->rqstp;
34b1744c 5360 const struct nfsd4_operation *opdesc = op->opdesc;
082d4bd7 5361 int post_err_offset;
07d1f802 5362 nfsd4_enc encoder;
bc749ca4 5363 __be32 *p;
1da177e4 5364
d0a381dd
BF
5365 p = xdr_reserve_space(xdr, 8);
5366 if (!p) {
5367 WARN_ON_ONCE(1);
5368 return;
5369 }
c373b0a4 5370 *p++ = cpu_to_be32(op->opnum);
082d4bd7 5371 post_err_offset = xdr->buf->len;
1da177e4 5372
695e12f8
BH
5373 if (op->opnum == OP_ILLEGAL)
5374 goto status;
b7571e4c
BF
5375 if (op->status && opdesc &&
5376 !(opdesc->op_flags & OP_NONTRIVIAL_ERROR_ENCODE))
5377 goto status;
695e12f8
BH
5378 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
5379 !nfsd4_enc_ops[op->opnum]);
07d1f802
BF
5380 encoder = nfsd4_enc_ops[op->opnum];
5381 op->status = encoder(resp, op->status, &op->u);
08281341
CL
5382 if (op->status)
5383 trace_nfsd_compound_encode_err(rqstp, op->opnum, op->status);
34b1744c
BF
5384 if (opdesc && opdesc->op_release)
5385 opdesc->op_release(&op->u);
2825a7f9
BF
5386 xdr_commit_encode(xdr);
5387
067e1ace 5388 /* nfsd4_check_resp_size guarantees enough room for error status */
5f4ab945
BF
5389 if (!op->status) {
5390 int space_needed = 0;
5391 if (!nfsd4_last_compound_op(rqstp))
5392 space_needed = COMPOUND_ERR_SLACK_SPACE;
5393 op->status = nfsd4_check_resp_size(resp, space_needed);
5394 }
c8f13d97
BF
5395 if (op->status == nfserr_resource && nfsd4_has_session(&resp->cstate)) {
5396 struct nfsd4_slot *slot = resp->cstate.slot;
5397
5398 if (slot->sl_flags & NFSD4_SLOT_CACHETHIS)
5399 op->status = nfserr_rep_too_big_to_cache;
5400 else
5401 op->status = nfserr_rep_too_big;
5402 }
07d1f802
BF
5403 if (op->status == nfserr_resource ||
5404 op->status == nfserr_rep_too_big ||
5405 op->status == nfserr_rep_too_big_to_cache) {
5406 /*
5407 * The operation may have already been encoded or
5408 * partially encoded. No op returns anything additional
5409 * in the case of one of these three errors, so we can
5410 * just truncate back to after the status. But it's a
5411 * bug if we had to do this on a non-idempotent op:
5412 */
5413 warn_on_nonidempotent_op(op);
082d4bd7 5414 xdr_truncate_encode(xdr, post_err_offset);
07d1f802 5415 }
9411b1d4 5416 if (so) {
082d4bd7
BF
5417 int len = xdr->buf->len - post_err_offset;
5418
9411b1d4 5419 so->so_replay.rp_status = op->status;
082d4bd7
BF
5420 so->so_replay.rp_buflen = len;
5421 read_bytes_from_xdr_buf(xdr->buf, post_err_offset,
5422 so->so_replay.rp_buf, len);
9411b1d4 5423 }
695e12f8 5424status:
082d4bd7
BF
5425 /* Note that op->status is already in network byte order: */
5426 write_bytes_to_xdr_buf(xdr->buf, post_err_offset - 4, &op->status, 4);
1da177e4
LT
5427}
5428
5429/*
5430 * Encode the reply stored in the stateowner reply cache
5431 *
5432 * XDR note: do not encode rp->rp_buflen: the buffer contains the
5433 * previously sent already encoded operation.
1da177e4
LT
5434 */
5435void
d0a381dd 5436nfsd4_encode_replay(struct xdr_stream *xdr, struct nfsd4_op *op)
1da177e4 5437{
bc749ca4 5438 __be32 *p;
1da177e4
LT
5439 struct nfs4_replay *rp = op->replay;
5440
d0a381dd
BF
5441 p = xdr_reserve_space(xdr, 8 + rp->rp_buflen);
5442 if (!p) {
5443 WARN_ON_ONCE(1);
5444 return;
5445 }
c373b0a4 5446 *p++ = cpu_to_be32(op->opnum);
1da177e4 5447 *p++ = rp->rp_status; /* already xdr'ed */
1da177e4 5448
0c0c267b 5449 p = xdr_encode_opaque_fixed(p, rp->rp_buf, rp->rp_buflen);
1da177e4
LT
5450}
5451
8537488b 5452void nfsd4_release_compoundargs(struct svc_rqst *rqstp)
1da177e4 5453{
3e98abff
BF
5454 struct nfsd4_compoundargs *args = rqstp->rq_argp;
5455
1da177e4
LT
5456 if (args->ops != args->iops) {
5457 kfree(args->ops);
5458 args->ops = args->iops;
5459 }
1da177e4 5460 while (args->to_free) {
d5e23383 5461 struct svcxdr_tmpbuf *tb = args->to_free;
1da177e4 5462 args->to_free = tb->next;
1da177e4
LT
5463 kfree(tb);
5464 }
5465}
5466
5467int
026fec7e 5468nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p)
1da177e4 5469{
026fec7e
CH
5470 struct nfsd4_compoundargs *args = rqstp->rq_argp;
5471
c1346a12 5472 /* svcxdr_tmp_alloc */
1da177e4
LT
5473 args->tmpp = NULL;
5474 args->to_free = NULL;
c1346a12
CL
5475
5476 args->xdr = &rqstp->rq_arg_stream;
1da177e4
LT
5477 args->ops = args->iops;
5478 args->rqstp = rqstp;
5479
3e98abff 5480 return !nfsd4_decode_compound(args);
1da177e4
LT
5481}
5482
5483int
63f8de37 5484nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p)
1da177e4 5485{
63f8de37 5486 struct nfsd4_compoundres *resp = rqstp->rq_resp;
6ac90391
BF
5487 struct xdr_buf *buf = resp->xdr.buf;
5488
5489 WARN_ON_ONCE(buf->len != buf->head[0].iov_len + buf->page_len +
5490 buf->tail[0].iov_len);
dd97fdde 5491
cc028a10
CL
5492 *p = resp->cstate.status;
5493
2825a7f9
BF
5494 rqstp->rq_next_page = resp->xdr.page_ptr + 1;
5495
1da177e4
LT
5496 p = resp->tagp;
5497 *p++ = htonl(resp->taglen);
5498 memcpy(p, resp->tag, resp->taglen);
5499 p += XDR_QUADLEN(resp->taglen);
5500 *p++ = htonl(resp->opcnt);
5501
b607664e 5502 nfsd4_sequence_done(resp);
1da177e4
LT
5503 return 1;
5504}
5505
5506/*
5507 * Local variables:
5508 * c-basic-offset: 8
5509 * End:
5510 */