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