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