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