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