Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / fs / nfsd / nfs4xdr.c
blob45ee6b12ce5b70d327d08bb9a10745d659383a90
1 /*
2 * Server-side XDR for NFSv4
4 * Copyright (c) 2002 The Regents of the University of Michigan.
5 * All rights reserved.
7 * Kendrick Smith <kmsmith@umich.edu>
8 * Andy Adamson <andros@umich.edu>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
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.
36 #include <linux/file.h>
37 #include <linux/slab.h>
38 #include <linux/namei.h>
39 #include <linux/statfs.h>
40 #include <linux/utsname.h>
41 #include <linux/pagemap.h>
42 #include <linux/sunrpc/svcauth_gss.h>
43 #include <linux/sunrpc/addr.h>
44 #include <linux/xattr.h>
45 #include <uapi/linux/xattr.h>
47 #include "idmap.h"
48 #include "acl.h"
49 #include "xdr4.h"
50 #include "vfs.h"
51 #include "state.h"
52 #include "cache.h"
53 #include "netns.h"
54 #include "pnfs.h"
55 #include "filecache.h"
57 #include "trace.h"
59 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
60 #include <linux/security.h>
61 #endif
64 #define NFSDDBG_FACILITY NFSDDBG_XDR
66 const u32 nfsd_suppattrs[3][3] = {
67 {NFSD4_SUPPORTED_ATTRS_WORD0,
68 NFSD4_SUPPORTED_ATTRS_WORD1,
69 NFSD4_SUPPORTED_ATTRS_WORD2},
71 {NFSD4_1_SUPPORTED_ATTRS_WORD0,
72 NFSD4_1_SUPPORTED_ATTRS_WORD1,
73 NFSD4_1_SUPPORTED_ATTRS_WORD2},
75 {NFSD4_1_SUPPORTED_ATTRS_WORD0,
76 NFSD4_1_SUPPORTED_ATTRS_WORD1,
77 NFSD4_2_SUPPORTED_ATTRS_WORD2},
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
85 #define NFS4_REFERRAL_FSID_MAJOR 0x8000000ULL
86 #define NFS4_REFERRAL_FSID_MINOR 0x8000000ULL
88 static __be32
89 check_filename(char *str, int len)
91 int i;
93 if (len == 0)
94 return nfserr_inval;
95 if (len > NFS4_MAXNAMLEN)
96 return nfserr_nametoolong;
97 if (isdotent(str, len))
98 return nfserr_badname;
99 for (i = 0; i < len; i++)
100 if (str[i] == '/')
101 return nfserr_badname;
102 return 0;
105 static int zero_clientid(clientid_t *clid)
107 return (clid->cl_boot == 0) && (clid->cl_id == 0);
111 * svcxdr_tmpalloc - allocate memory to be freed after compound processing
112 * @argp: NFSv4 compound argument structure
113 * @len: length of buffer to allocate
115 * Allocates a buffer of size @len to be freed when processing the compound
116 * operation described in @argp finishes.
118 static void *
119 svcxdr_tmpalloc(struct nfsd4_compoundargs *argp, u32 len)
121 struct svcxdr_tmpbuf *tb;
123 tb = kmalloc(sizeof(*tb) + len, GFP_KERNEL);
124 if (!tb)
125 return NULL;
126 tb->next = argp->to_free;
127 argp->to_free = tb;
128 return tb->buf;
132 * For xdr strings that need to be passed to other kernel api's
133 * as null-terminated strings.
135 * Note null-terminating in place usually isn't safe since the
136 * buffer might end on a page boundary.
138 static char *
139 svcxdr_dupstr(struct nfsd4_compoundargs *argp, void *buf, u32 len)
141 char *p = svcxdr_tmpalloc(argp, len + 1);
143 if (!p)
144 return NULL;
145 memcpy(p, buf, len);
146 p[len] = '\0';
147 return p;
151 * NFSv4 basic data type decoders
155 * This helper handles variable-length opaques which belong to protocol
156 * elements that this implementation does not support.
158 static __be32
159 nfsd4_decode_ignored_string(struct nfsd4_compoundargs *argp, u32 maxlen)
161 u32 len;
163 if (xdr_stream_decode_u32(argp->xdr, &len) < 0)
164 return nfserr_bad_xdr;
165 if (maxlen && len > maxlen)
166 return nfserr_bad_xdr;
167 if (!xdr_inline_decode(argp->xdr, len))
168 return nfserr_bad_xdr;
170 return nfs_ok;
173 static __be32
174 nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
176 __be32 *p;
177 u32 len;
179 if (xdr_stream_decode_u32(argp->xdr, &len) < 0)
180 return nfserr_bad_xdr;
181 if (len == 0 || len > NFS4_OPAQUE_LIMIT)
182 return nfserr_bad_xdr;
183 p = xdr_inline_decode(argp->xdr, len);
184 if (!p)
185 return nfserr_bad_xdr;
186 o->data = svcxdr_tmpalloc(argp, len);
187 if (!o->data)
188 return nfserr_jukebox;
189 o->len = len;
190 memcpy(o->data, p, len);
192 return nfs_ok;
195 static __be32
196 nfsd4_decode_component4(struct nfsd4_compoundargs *argp, char **namp, u32 *lenp)
198 __be32 *p, status;
200 if (xdr_stream_decode_u32(argp->xdr, lenp) < 0)
201 return nfserr_bad_xdr;
202 p = xdr_inline_decode(argp->xdr, *lenp);
203 if (!p)
204 return nfserr_bad_xdr;
205 status = check_filename((char *)p, *lenp);
206 if (status)
207 return status;
208 *namp = svcxdr_tmpalloc(argp, *lenp);
209 if (!*namp)
210 return nfserr_jukebox;
211 memcpy(*namp, p, *lenp);
213 return nfs_ok;
216 static __be32
217 nfsd4_decode_nfstime4(struct nfsd4_compoundargs *argp, struct timespec64 *tv)
219 __be32 *p;
221 p = xdr_inline_decode(argp->xdr, XDR_UNIT * 3);
222 if (!p)
223 return nfserr_bad_xdr;
224 p = xdr_decode_hyper(p, &tv->tv_sec);
225 tv->tv_nsec = be32_to_cpup(p++);
226 if (tv->tv_nsec >= (u32)1000000000)
227 return nfserr_inval;
228 return nfs_ok;
231 static __be32
232 nfsd4_decode_verifier4(struct nfsd4_compoundargs *argp, nfs4_verifier *verf)
234 __be32 *p;
236 p = xdr_inline_decode(argp->xdr, NFS4_VERIFIER_SIZE);
237 if (!p)
238 return nfserr_bad_xdr;
239 memcpy(verf->data, p, sizeof(verf->data));
240 return nfs_ok;
244 * nfsd4_decode_bitmap4 - Decode an NFSv4 bitmap4
245 * @argp: NFSv4 compound argument structure
246 * @bmval: pointer to an array of u32's to decode into
247 * @bmlen: size of the @bmval array
249 * The server needs to return nfs_ok rather than nfserr_bad_xdr when
250 * encountering bitmaps containing bits it does not recognize. This
251 * includes bits in bitmap words past WORDn, where WORDn is the last
252 * bitmap WORD the implementation currently supports. Thus we are
253 * careful here to simply ignore bits in bitmap words that this
254 * implementation has yet to support explicitly.
256 * Return values:
257 * %nfs_ok: @bmval populated successfully
258 * %nfserr_bad_xdr: the encoded bitmap was invalid
260 static __be32
261 nfsd4_decode_bitmap4(struct nfsd4_compoundargs *argp, u32 *bmval, u32 bmlen)
263 u32 i, count;
264 __be32 *p;
266 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
267 return nfserr_bad_xdr;
268 /* request sanity */
269 if (count > 1000)
270 return nfserr_bad_xdr;
271 p = xdr_inline_decode(argp->xdr, count << 2);
272 if (!p)
273 return nfserr_bad_xdr;
274 i = 0;
275 while (i < count)
276 bmval[i++] = be32_to_cpup(p++);
277 while (i < bmlen)
278 bmval[i++] = 0;
280 return nfs_ok;
283 static __be32
284 nfsd4_decode_nfsace4(struct nfsd4_compoundargs *argp, struct nfs4_ace *ace)
286 __be32 *p, status;
287 u32 length;
289 if (xdr_stream_decode_u32(argp->xdr, &ace->type) < 0)
290 return nfserr_bad_xdr;
291 if (xdr_stream_decode_u32(argp->xdr, &ace->flag) < 0)
292 return nfserr_bad_xdr;
293 if (xdr_stream_decode_u32(argp->xdr, &ace->access_mask) < 0)
294 return nfserr_bad_xdr;
296 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
297 return nfserr_bad_xdr;
298 p = xdr_inline_decode(argp->xdr, length);
299 if (!p)
300 return nfserr_bad_xdr;
301 ace->whotype = nfs4_acl_get_whotype((char *)p, length);
302 if (ace->whotype != NFS4_ACL_WHO_NAMED)
303 status = nfs_ok;
304 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
305 status = nfsd_map_name_to_gid(argp->rqstp,
306 (char *)p, length, &ace->who_gid);
307 else
308 status = nfsd_map_name_to_uid(argp->rqstp,
309 (char *)p, length, &ace->who_uid);
311 return status;
314 /* A counted array of nfsace4's */
315 static noinline __be32
316 nfsd4_decode_acl(struct nfsd4_compoundargs *argp, struct nfs4_acl **acl)
318 struct nfs4_ace *ace;
319 __be32 status;
320 u32 count;
322 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
323 return nfserr_bad_xdr;
325 if (count > xdr_stream_remaining(argp->xdr) / 20)
327 * Even with 4-byte names there wouldn't be
328 * space for that many aces; something fishy is
329 * going on:
331 return nfserr_fbig;
333 *acl = svcxdr_tmpalloc(argp, nfs4_acl_bytes(count));
334 if (*acl == NULL)
335 return nfserr_jukebox;
337 (*acl)->naces = count;
338 for (ace = (*acl)->aces; ace < (*acl)->aces + count; ace++) {
339 status = nfsd4_decode_nfsace4(argp, ace);
340 if (status)
341 return status;
344 return nfs_ok;
347 static noinline __be32
348 nfsd4_decode_security_label(struct nfsd4_compoundargs *argp,
349 struct xdr_netobj *label)
351 u32 lfs, pi, length;
352 __be32 *p;
354 if (xdr_stream_decode_u32(argp->xdr, &lfs) < 0)
355 return nfserr_bad_xdr;
356 if (xdr_stream_decode_u32(argp->xdr, &pi) < 0)
357 return nfserr_bad_xdr;
359 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
360 return nfserr_bad_xdr;
361 if (length > NFS4_MAXLABELLEN)
362 return nfserr_badlabel;
363 p = xdr_inline_decode(argp->xdr, length);
364 if (!p)
365 return nfserr_bad_xdr;
366 label->len = length;
367 label->data = svcxdr_dupstr(argp, p, length);
368 if (!label->data)
369 return nfserr_jukebox;
371 return nfs_ok;
374 static __be32
375 nfsd4_decode_fattr4(struct nfsd4_compoundargs *argp, u32 *bmval, u32 bmlen,
376 struct iattr *iattr, struct nfs4_acl **acl,
377 struct xdr_netobj *label, int *umask)
379 unsigned int starting_pos;
380 u32 attrlist4_count;
381 __be32 *p, status;
383 iattr->ia_valid = 0;
384 status = nfsd4_decode_bitmap4(argp, bmval, bmlen);
385 if (status)
386 return nfserr_bad_xdr;
388 if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
389 || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
390 || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2) {
391 if (nfsd_attrs_supported(argp->minorversion, bmval))
392 return nfserr_inval;
393 return nfserr_attrnotsupp;
396 if (xdr_stream_decode_u32(argp->xdr, &attrlist4_count) < 0)
397 return nfserr_bad_xdr;
398 starting_pos = xdr_stream_pos(argp->xdr);
400 if (bmval[0] & FATTR4_WORD0_SIZE) {
401 u64 size;
403 if (xdr_stream_decode_u64(argp->xdr, &size) < 0)
404 return nfserr_bad_xdr;
405 iattr->ia_size = size;
406 iattr->ia_valid |= ATTR_SIZE;
408 if (bmval[0] & FATTR4_WORD0_ACL) {
409 status = nfsd4_decode_acl(argp, acl);
410 if (status)
411 return status;
412 } else
413 *acl = NULL;
414 if (bmval[1] & FATTR4_WORD1_MODE) {
415 u32 mode;
417 if (xdr_stream_decode_u32(argp->xdr, &mode) < 0)
418 return nfserr_bad_xdr;
419 iattr->ia_mode = mode;
420 iattr->ia_mode &= (S_IFMT | S_IALLUGO);
421 iattr->ia_valid |= ATTR_MODE;
423 if (bmval[1] & FATTR4_WORD1_OWNER) {
424 u32 length;
426 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
427 return nfserr_bad_xdr;
428 p = xdr_inline_decode(argp->xdr, length);
429 if (!p)
430 return nfserr_bad_xdr;
431 status = nfsd_map_name_to_uid(argp->rqstp, (char *)p, length,
432 &iattr->ia_uid);
433 if (status)
434 return status;
435 iattr->ia_valid |= ATTR_UID;
437 if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
438 u32 length;
440 if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
441 return nfserr_bad_xdr;
442 p = xdr_inline_decode(argp->xdr, length);
443 if (!p)
444 return nfserr_bad_xdr;
445 status = nfsd_map_name_to_gid(argp->rqstp, (char *)p, length,
446 &iattr->ia_gid);
447 if (status)
448 return status;
449 iattr->ia_valid |= ATTR_GID;
451 if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
452 u32 set_it;
454 if (xdr_stream_decode_u32(argp->xdr, &set_it) < 0)
455 return nfserr_bad_xdr;
456 switch (set_it) {
457 case NFS4_SET_TO_CLIENT_TIME:
458 status = nfsd4_decode_nfstime4(argp, &iattr->ia_atime);
459 if (status)
460 return status;
461 iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
462 break;
463 case NFS4_SET_TO_SERVER_TIME:
464 iattr->ia_valid |= ATTR_ATIME;
465 break;
466 default:
467 return nfserr_bad_xdr;
470 if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
471 u32 set_it;
473 if (xdr_stream_decode_u32(argp->xdr, &set_it) < 0)
474 return nfserr_bad_xdr;
475 switch (set_it) {
476 case NFS4_SET_TO_CLIENT_TIME:
477 status = nfsd4_decode_nfstime4(argp, &iattr->ia_mtime);
478 if (status)
479 return status;
480 iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
481 break;
482 case NFS4_SET_TO_SERVER_TIME:
483 iattr->ia_valid |= ATTR_MTIME;
484 break;
485 default:
486 return nfserr_bad_xdr;
489 label->len = 0;
490 if (IS_ENABLED(CONFIG_NFSD_V4_SECURITY_LABEL) &&
491 bmval[2] & FATTR4_WORD2_SECURITY_LABEL) {
492 status = nfsd4_decode_security_label(argp, label);
493 if (status)
494 return status;
496 if (bmval[2] & FATTR4_WORD2_MODE_UMASK) {
497 u32 mode, mask;
499 if (!umask)
500 return nfserr_bad_xdr;
501 if (xdr_stream_decode_u32(argp->xdr, &mode) < 0)
502 return nfserr_bad_xdr;
503 iattr->ia_mode = mode & (S_IFMT | S_IALLUGO);
504 if (xdr_stream_decode_u32(argp->xdr, &mask) < 0)
505 return nfserr_bad_xdr;
506 *umask = mask & S_IRWXUGO;
507 iattr->ia_valid |= ATTR_MODE;
510 /* request sanity: did attrlist4 contain the expected number of words? */
511 if (attrlist4_count != xdr_stream_pos(argp->xdr) - starting_pos)
512 return nfserr_bad_xdr;
514 return nfs_ok;
517 static __be32
518 nfsd4_decode_stateid4(struct nfsd4_compoundargs *argp, stateid_t *sid)
520 __be32 *p;
522 p = xdr_inline_decode(argp->xdr, NFS4_STATEID_SIZE);
523 if (!p)
524 return nfserr_bad_xdr;
525 sid->si_generation = be32_to_cpup(p++);
526 memcpy(&sid->si_opaque, p, sizeof(sid->si_opaque));
527 return nfs_ok;
530 static __be32
531 nfsd4_decode_clientid4(struct nfsd4_compoundargs *argp, clientid_t *clientid)
533 __be32 *p;
535 p = xdr_inline_decode(argp->xdr, sizeof(__be64));
536 if (!p)
537 return nfserr_bad_xdr;
538 memcpy(clientid, p, sizeof(*clientid));
539 return nfs_ok;
542 static __be32
543 nfsd4_decode_state_owner4(struct nfsd4_compoundargs *argp,
544 clientid_t *clientid, struct xdr_netobj *owner)
546 __be32 status;
548 status = nfsd4_decode_clientid4(argp, clientid);
549 if (status)
550 return status;
551 return nfsd4_decode_opaque(argp, owner);
554 #ifdef CONFIG_NFSD_PNFS
555 static __be32
556 nfsd4_decode_deviceid4(struct nfsd4_compoundargs *argp,
557 struct nfsd4_deviceid *devid)
559 __be32 *p;
561 p = xdr_inline_decode(argp->xdr, NFS4_DEVICEID4_SIZE);
562 if (!p)
563 return nfserr_bad_xdr;
564 memcpy(devid, p, sizeof(*devid));
565 return nfs_ok;
568 static __be32
569 nfsd4_decode_layoutupdate4(struct nfsd4_compoundargs *argp,
570 struct nfsd4_layoutcommit *lcp)
572 if (xdr_stream_decode_u32(argp->xdr, &lcp->lc_layout_type) < 0)
573 return nfserr_bad_xdr;
574 if (lcp->lc_layout_type < LAYOUT_NFSV4_1_FILES)
575 return nfserr_bad_xdr;
576 if (lcp->lc_layout_type >= LAYOUT_TYPE_MAX)
577 return nfserr_bad_xdr;
579 if (xdr_stream_decode_u32(argp->xdr, &lcp->lc_up_len) < 0)
580 return nfserr_bad_xdr;
581 if (lcp->lc_up_len > 0) {
582 lcp->lc_up_layout = xdr_inline_decode(argp->xdr, lcp->lc_up_len);
583 if (!lcp->lc_up_layout)
584 return nfserr_bad_xdr;
587 return nfs_ok;
590 static __be32
591 nfsd4_decode_layoutreturn4(struct nfsd4_compoundargs *argp,
592 struct nfsd4_layoutreturn *lrp)
594 __be32 status;
596 if (xdr_stream_decode_u32(argp->xdr, &lrp->lr_return_type) < 0)
597 return nfserr_bad_xdr;
598 switch (lrp->lr_return_type) {
599 case RETURN_FILE:
600 if (xdr_stream_decode_u64(argp->xdr, &lrp->lr_seg.offset) < 0)
601 return nfserr_bad_xdr;
602 if (xdr_stream_decode_u64(argp->xdr, &lrp->lr_seg.length) < 0)
603 return nfserr_bad_xdr;
604 status = nfsd4_decode_stateid4(argp, &lrp->lr_sid);
605 if (status)
606 return status;
607 if (xdr_stream_decode_u32(argp->xdr, &lrp->lrf_body_len) < 0)
608 return nfserr_bad_xdr;
609 if (lrp->lrf_body_len > 0) {
610 lrp->lrf_body = xdr_inline_decode(argp->xdr, lrp->lrf_body_len);
611 if (!lrp->lrf_body)
612 return nfserr_bad_xdr;
614 break;
615 case RETURN_FSID:
616 case RETURN_ALL:
617 lrp->lr_seg.offset = 0;
618 lrp->lr_seg.length = NFS4_MAX_UINT64;
619 break;
620 default:
621 return nfserr_bad_xdr;
624 return nfs_ok;
627 #endif /* CONFIG_NFSD_PNFS */
629 static __be32
630 nfsd4_decode_sessionid4(struct nfsd4_compoundargs *argp,
631 struct nfs4_sessionid *sessionid)
633 __be32 *p;
635 p = xdr_inline_decode(argp->xdr, NFS4_MAX_SESSIONID_LEN);
636 if (!p)
637 return nfserr_bad_xdr;
638 memcpy(sessionid->data, p, sizeof(sessionid->data));
639 return nfs_ok;
642 /* Defined in Appendix A of RFC 5531 */
643 static __be32
644 nfsd4_decode_authsys_parms(struct nfsd4_compoundargs *argp,
645 struct nfsd4_cb_sec *cbs)
647 u32 stamp, gidcount, uid, gid;
648 __be32 *p, status;
650 if (xdr_stream_decode_u32(argp->xdr, &stamp) < 0)
651 return nfserr_bad_xdr;
652 /* machine name */
653 status = nfsd4_decode_ignored_string(argp, 255);
654 if (status)
655 return status;
656 if (xdr_stream_decode_u32(argp->xdr, &uid) < 0)
657 return nfserr_bad_xdr;
658 if (xdr_stream_decode_u32(argp->xdr, &gid) < 0)
659 return nfserr_bad_xdr;
660 if (xdr_stream_decode_u32(argp->xdr, &gidcount) < 0)
661 return nfserr_bad_xdr;
662 if (gidcount > 16)
663 return nfserr_bad_xdr;
664 p = xdr_inline_decode(argp->xdr, gidcount << 2);
665 if (!p)
666 return nfserr_bad_xdr;
667 if (cbs->flavor == (u32)(-1)) {
668 struct user_namespace *userns = nfsd_user_namespace(argp->rqstp);
670 kuid_t kuid = make_kuid(userns, uid);
671 kgid_t kgid = make_kgid(userns, gid);
672 if (uid_valid(kuid) && gid_valid(kgid)) {
673 cbs->uid = kuid;
674 cbs->gid = kgid;
675 cbs->flavor = RPC_AUTH_UNIX;
676 } else {
677 dprintk("RPC_AUTH_UNIX with invalid uid or gid, ignoring!\n");
681 return nfs_ok;
684 static __be32
685 nfsd4_decode_gss_cb_handles4(struct nfsd4_compoundargs *argp,
686 struct nfsd4_cb_sec *cbs)
688 __be32 status;
689 u32 service;
691 dprintk("RPC_AUTH_GSS callback secflavor not supported!\n");
693 if (xdr_stream_decode_u32(argp->xdr, &service) < 0)
694 return nfserr_bad_xdr;
695 if (service < RPC_GSS_SVC_NONE || service > RPC_GSS_SVC_PRIVACY)
696 return nfserr_bad_xdr;
697 /* gcbp_handle_from_server */
698 status = nfsd4_decode_ignored_string(argp, 0);
699 if (status)
700 return status;
701 /* gcbp_handle_from_client */
702 status = nfsd4_decode_ignored_string(argp, 0);
703 if (status)
704 return status;
706 return nfs_ok;
709 /* a counted array of callback_sec_parms4 items */
710 static __be32
711 nfsd4_decode_cb_sec(struct nfsd4_compoundargs *argp, struct nfsd4_cb_sec *cbs)
713 u32 i, secflavor, nr_secflavs;
714 __be32 status;
716 /* callback_sec_params4 */
717 if (xdr_stream_decode_u32(argp->xdr, &nr_secflavs) < 0)
718 return nfserr_bad_xdr;
719 if (nr_secflavs)
720 cbs->flavor = (u32)(-1);
721 else
722 /* Is this legal? Be generous, take it to mean AUTH_NONE: */
723 cbs->flavor = 0;
725 for (i = 0; i < nr_secflavs; ++i) {
726 if (xdr_stream_decode_u32(argp->xdr, &secflavor) < 0)
727 return nfserr_bad_xdr;
728 switch (secflavor) {
729 case RPC_AUTH_NULL:
730 /* void */
731 if (cbs->flavor == (u32)(-1))
732 cbs->flavor = RPC_AUTH_NULL;
733 break;
734 case RPC_AUTH_UNIX:
735 status = nfsd4_decode_authsys_parms(argp, cbs);
736 if (status)
737 return status;
738 break;
739 case RPC_AUTH_GSS:
740 status = nfsd4_decode_gss_cb_handles4(argp, cbs);
741 if (status)
742 return status;
743 break;
744 default:
745 return nfserr_inval;
749 return nfs_ok;
754 * NFSv4 operation argument decoders
757 static __be32
758 nfsd4_decode_access(struct nfsd4_compoundargs *argp,
759 struct nfsd4_access *access)
761 if (xdr_stream_decode_u32(argp->xdr, &access->ac_req_access) < 0)
762 return nfserr_bad_xdr;
763 return nfs_ok;
766 static __be32
767 nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
769 if (xdr_stream_decode_u32(argp->xdr, &close->cl_seqid) < 0)
770 return nfserr_bad_xdr;
771 return nfsd4_decode_stateid4(argp, &close->cl_stateid);
775 static __be32
776 nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
778 if (xdr_stream_decode_u64(argp->xdr, &commit->co_offset) < 0)
779 return nfserr_bad_xdr;
780 if (xdr_stream_decode_u32(argp->xdr, &commit->co_count) < 0)
781 return nfserr_bad_xdr;
782 return nfs_ok;
785 static __be32
786 nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
788 __be32 *p, status;
790 if (xdr_stream_decode_u32(argp->xdr, &create->cr_type) < 0)
791 return nfserr_bad_xdr;
792 switch (create->cr_type) {
793 case NF4LNK:
794 if (xdr_stream_decode_u32(argp->xdr, &create->cr_datalen) < 0)
795 return nfserr_bad_xdr;
796 p = xdr_inline_decode(argp->xdr, create->cr_datalen);
797 if (!p)
798 return nfserr_bad_xdr;
799 create->cr_data = svcxdr_dupstr(argp, p, create->cr_datalen);
800 if (!create->cr_data)
801 return nfserr_jukebox;
802 break;
803 case NF4BLK:
804 case NF4CHR:
805 if (xdr_stream_decode_u32(argp->xdr, &create->cr_specdata1) < 0)
806 return nfserr_bad_xdr;
807 if (xdr_stream_decode_u32(argp->xdr, &create->cr_specdata2) < 0)
808 return nfserr_bad_xdr;
809 break;
810 case NF4SOCK:
811 case NF4FIFO:
812 case NF4DIR:
813 default:
814 break;
816 status = nfsd4_decode_component4(argp, &create->cr_name,
817 &create->cr_namelen);
818 if (status)
819 return status;
820 status = nfsd4_decode_fattr4(argp, create->cr_bmval,
821 ARRAY_SIZE(create->cr_bmval),
822 &create->cr_iattr, &create->cr_acl,
823 &create->cr_label, &create->cr_umask);
824 if (status)
825 return status;
827 return nfs_ok;
830 static inline __be32
831 nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
833 return nfsd4_decode_stateid4(argp, &dr->dr_stateid);
836 static inline __be32
837 nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
839 return nfsd4_decode_bitmap4(argp, getattr->ga_bmval,
840 ARRAY_SIZE(getattr->ga_bmval));
843 static __be32
844 nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
846 return nfsd4_decode_component4(argp, &link->li_name, &link->li_namelen);
849 static __be32
850 nfsd4_decode_open_to_lock_owner4(struct nfsd4_compoundargs *argp,
851 struct nfsd4_lock *lock)
853 __be32 status;
855 if (xdr_stream_decode_u32(argp->xdr, &lock->lk_new_open_seqid) < 0)
856 return nfserr_bad_xdr;
857 status = nfsd4_decode_stateid4(argp, &lock->lk_new_open_stateid);
858 if (status)
859 return status;
860 if (xdr_stream_decode_u32(argp->xdr, &lock->lk_new_lock_seqid) < 0)
861 return nfserr_bad_xdr;
862 return nfsd4_decode_state_owner4(argp, &lock->lk_new_clientid,
863 &lock->lk_new_owner);
866 static __be32
867 nfsd4_decode_exist_lock_owner4(struct nfsd4_compoundargs *argp,
868 struct nfsd4_lock *lock)
870 __be32 status;
872 status = nfsd4_decode_stateid4(argp, &lock->lk_old_lock_stateid);
873 if (status)
874 return status;
875 if (xdr_stream_decode_u32(argp->xdr, &lock->lk_old_lock_seqid) < 0)
876 return nfserr_bad_xdr;
878 return nfs_ok;
881 static __be32
882 nfsd4_decode_locker4(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
884 if (xdr_stream_decode_bool(argp->xdr, &lock->lk_is_new) < 0)
885 return nfserr_bad_xdr;
886 if (lock->lk_is_new)
887 return nfsd4_decode_open_to_lock_owner4(argp, lock);
888 return nfsd4_decode_exist_lock_owner4(argp, lock);
891 static __be32
892 nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
894 if (xdr_stream_decode_u32(argp->xdr, &lock->lk_type) < 0)
895 return nfserr_bad_xdr;
896 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
897 return nfserr_bad_xdr;
898 if (xdr_stream_decode_bool(argp->xdr, &lock->lk_reclaim) < 0)
899 return nfserr_bad_xdr;
900 if (xdr_stream_decode_u64(argp->xdr, &lock->lk_offset) < 0)
901 return nfserr_bad_xdr;
902 if (xdr_stream_decode_u64(argp->xdr, &lock->lk_length) < 0)
903 return nfserr_bad_xdr;
904 return nfsd4_decode_locker4(argp, lock);
907 static __be32
908 nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
910 if (xdr_stream_decode_u32(argp->xdr, &lockt->lt_type) < 0)
911 return nfserr_bad_xdr;
912 if ((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
913 return nfserr_bad_xdr;
914 if (xdr_stream_decode_u64(argp->xdr, &lockt->lt_offset) < 0)
915 return nfserr_bad_xdr;
916 if (xdr_stream_decode_u64(argp->xdr, &lockt->lt_length) < 0)
917 return nfserr_bad_xdr;
918 return nfsd4_decode_state_owner4(argp, &lockt->lt_clientid,
919 &lockt->lt_owner);
922 static __be32
923 nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
925 __be32 status;
927 if (xdr_stream_decode_u32(argp->xdr, &locku->lu_type) < 0)
928 return nfserr_bad_xdr;
929 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
930 return nfserr_bad_xdr;
931 if (xdr_stream_decode_u32(argp->xdr, &locku->lu_seqid) < 0)
932 return nfserr_bad_xdr;
933 status = nfsd4_decode_stateid4(argp, &locku->lu_stateid);
934 if (status)
935 return status;
936 if (xdr_stream_decode_u64(argp->xdr, &locku->lu_offset) < 0)
937 return nfserr_bad_xdr;
938 if (xdr_stream_decode_u64(argp->xdr, &locku->lu_length) < 0)
939 return nfserr_bad_xdr;
941 return nfs_ok;
944 static __be32
945 nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
947 return nfsd4_decode_component4(argp, &lookup->lo_name, &lookup->lo_len);
950 static __be32
951 nfsd4_decode_createhow4(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
953 __be32 status;
955 if (xdr_stream_decode_u32(argp->xdr, &open->op_createmode) < 0)
956 return nfserr_bad_xdr;
957 switch (open->op_createmode) {
958 case NFS4_CREATE_UNCHECKED:
959 case NFS4_CREATE_GUARDED:
960 status = nfsd4_decode_fattr4(argp, open->op_bmval,
961 ARRAY_SIZE(open->op_bmval),
962 &open->op_iattr, &open->op_acl,
963 &open->op_label, &open->op_umask);
964 if (status)
965 return status;
966 break;
967 case NFS4_CREATE_EXCLUSIVE:
968 status = nfsd4_decode_verifier4(argp, &open->op_verf);
969 if (status)
970 return status;
971 break;
972 case NFS4_CREATE_EXCLUSIVE4_1:
973 if (argp->minorversion < 1)
974 return nfserr_bad_xdr;
975 status = nfsd4_decode_verifier4(argp, &open->op_verf);
976 if (status)
977 return status;
978 status = nfsd4_decode_fattr4(argp, open->op_bmval,
979 ARRAY_SIZE(open->op_bmval),
980 &open->op_iattr, &open->op_acl,
981 &open->op_label, &open->op_umask);
982 if (status)
983 return status;
984 break;
985 default:
986 return nfserr_bad_xdr;
989 return nfs_ok;
992 static __be32
993 nfsd4_decode_openflag4(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
995 __be32 status;
997 if (xdr_stream_decode_u32(argp->xdr, &open->op_create) < 0)
998 return nfserr_bad_xdr;
999 switch (open->op_create) {
1000 case NFS4_OPEN_NOCREATE:
1001 break;
1002 case NFS4_OPEN_CREATE:
1003 status = nfsd4_decode_createhow4(argp, open);
1004 if (status)
1005 return status;
1006 break;
1007 default:
1008 return nfserr_bad_xdr;
1011 return nfs_ok;
1014 static __be32 nfsd4_decode_share_access(struct nfsd4_compoundargs *argp, u32 *share_access, u32 *deleg_want, u32 *deleg_when)
1016 u32 w;
1018 if (xdr_stream_decode_u32(argp->xdr, &w) < 0)
1019 return nfserr_bad_xdr;
1020 *share_access = w & NFS4_SHARE_ACCESS_MASK;
1021 *deleg_want = w & NFS4_SHARE_WANT_MASK;
1022 if (deleg_when)
1023 *deleg_when = w & NFS4_SHARE_WHEN_MASK;
1025 switch (w & NFS4_SHARE_ACCESS_MASK) {
1026 case NFS4_SHARE_ACCESS_READ:
1027 case NFS4_SHARE_ACCESS_WRITE:
1028 case NFS4_SHARE_ACCESS_BOTH:
1029 break;
1030 default:
1031 return nfserr_bad_xdr;
1033 w &= ~NFS4_SHARE_ACCESS_MASK;
1034 if (!w)
1035 return nfs_ok;
1036 if (!argp->minorversion)
1037 return nfserr_bad_xdr;
1038 switch (w & NFS4_SHARE_WANT_MASK) {
1039 case NFS4_SHARE_WANT_NO_PREFERENCE:
1040 case NFS4_SHARE_WANT_READ_DELEG:
1041 case NFS4_SHARE_WANT_WRITE_DELEG:
1042 case NFS4_SHARE_WANT_ANY_DELEG:
1043 case NFS4_SHARE_WANT_NO_DELEG:
1044 case NFS4_SHARE_WANT_CANCEL:
1045 break;
1046 default:
1047 return nfserr_bad_xdr;
1049 w &= ~NFS4_SHARE_WANT_MASK;
1050 if (!w)
1051 return nfs_ok;
1053 if (!deleg_when) /* open_downgrade */
1054 return nfserr_inval;
1055 switch (w) {
1056 case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
1057 case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
1058 case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
1059 NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
1060 return nfs_ok;
1062 return nfserr_bad_xdr;
1065 static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
1067 if (xdr_stream_decode_u32(argp->xdr, x) < 0)
1068 return nfserr_bad_xdr;
1069 /* Note: unlike access bits, deny bits may be zero. */
1070 if (*x & ~NFS4_SHARE_DENY_BOTH)
1071 return nfserr_bad_xdr;
1073 return nfs_ok;
1076 static __be32
1077 nfsd4_decode_open_claim4(struct nfsd4_compoundargs *argp,
1078 struct nfsd4_open *open)
1080 __be32 status;
1082 if (xdr_stream_decode_u32(argp->xdr, &open->op_claim_type) < 0)
1083 return nfserr_bad_xdr;
1084 switch (open->op_claim_type) {
1085 case NFS4_OPEN_CLAIM_NULL:
1086 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1087 status = nfsd4_decode_component4(argp, &open->op_fname,
1088 &open->op_fnamelen);
1089 if (status)
1090 return status;
1091 break;
1092 case NFS4_OPEN_CLAIM_PREVIOUS:
1093 if (xdr_stream_decode_u32(argp->xdr, &open->op_delegate_type) < 0)
1094 return nfserr_bad_xdr;
1095 break;
1096 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1097 status = nfsd4_decode_stateid4(argp, &open->op_delegate_stateid);
1098 if (status)
1099 return status;
1100 status = nfsd4_decode_component4(argp, &open->op_fname,
1101 &open->op_fnamelen);
1102 if (status)
1103 return status;
1104 break;
1105 case NFS4_OPEN_CLAIM_FH:
1106 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1107 if (argp->minorversion < 1)
1108 return nfserr_bad_xdr;
1109 /* void */
1110 break;
1111 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1112 if (argp->minorversion < 1)
1113 return nfserr_bad_xdr;
1114 status = nfsd4_decode_stateid4(argp, &open->op_delegate_stateid);
1115 if (status)
1116 return status;
1117 break;
1118 default:
1119 return nfserr_bad_xdr;
1122 return nfs_ok;
1125 static __be32
1126 nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
1128 __be32 status;
1129 u32 dummy;
1131 memset(open->op_bmval, 0, sizeof(open->op_bmval));
1132 open->op_iattr.ia_valid = 0;
1133 open->op_openowner = NULL;
1135 open->op_xdr_error = 0;
1136 if (xdr_stream_decode_u32(argp->xdr, &open->op_seqid) < 0)
1137 return nfserr_bad_xdr;
1138 /* deleg_want is ignored */
1139 status = nfsd4_decode_share_access(argp, &open->op_share_access,
1140 &open->op_deleg_want, &dummy);
1141 if (status)
1142 return status;
1143 status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
1144 if (status)
1145 return status;
1146 status = nfsd4_decode_state_owner4(argp, &open->op_clientid,
1147 &open->op_owner);
1148 if (status)
1149 return status;
1150 status = nfsd4_decode_openflag4(argp, open);
1151 if (status)
1152 return status;
1153 return nfsd4_decode_open_claim4(argp, open);
1156 static __be32
1157 nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
1159 __be32 status;
1161 if (argp->minorversion >= 1)
1162 return nfserr_notsupp;
1164 status = nfsd4_decode_stateid4(argp, &open_conf->oc_req_stateid);
1165 if (status)
1166 return status;
1167 if (xdr_stream_decode_u32(argp->xdr, &open_conf->oc_seqid) < 0)
1168 return nfserr_bad_xdr;
1170 return nfs_ok;
1173 static __be32
1174 nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
1176 __be32 status;
1178 status = nfsd4_decode_stateid4(argp, &open_down->od_stateid);
1179 if (status)
1180 return status;
1181 if (xdr_stream_decode_u32(argp->xdr, &open_down->od_seqid) < 0)
1182 return nfserr_bad_xdr;
1183 /* deleg_want is ignored */
1184 status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
1185 &open_down->od_deleg_want, NULL);
1186 if (status)
1187 return status;
1188 return nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
1191 static __be32
1192 nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
1194 __be32 *p;
1196 if (xdr_stream_decode_u32(argp->xdr, &putfh->pf_fhlen) < 0)
1197 return nfserr_bad_xdr;
1198 if (putfh->pf_fhlen > NFS4_FHSIZE)
1199 return nfserr_bad_xdr;
1200 p = xdr_inline_decode(argp->xdr, putfh->pf_fhlen);
1201 if (!p)
1202 return nfserr_bad_xdr;
1203 putfh->pf_fhval = svcxdr_tmpalloc(argp, putfh->pf_fhlen);
1204 if (!putfh->pf_fhval)
1205 return nfserr_jukebox;
1206 memcpy(putfh->pf_fhval, p, putfh->pf_fhlen);
1208 return nfs_ok;
1211 static __be32
1212 nfsd4_decode_putpubfh(struct nfsd4_compoundargs *argp, void *p)
1214 if (argp->minorversion == 0)
1215 return nfs_ok;
1216 return nfserr_notsupp;
1219 static __be32
1220 nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
1222 __be32 status;
1224 status = nfsd4_decode_stateid4(argp, &read->rd_stateid);
1225 if (status)
1226 return status;
1227 if (xdr_stream_decode_u64(argp->xdr, &read->rd_offset) < 0)
1228 return nfserr_bad_xdr;
1229 if (xdr_stream_decode_u32(argp->xdr, &read->rd_length) < 0)
1230 return nfserr_bad_xdr;
1232 return nfs_ok;
1235 static __be32
1236 nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
1238 __be32 status;
1240 if (xdr_stream_decode_u64(argp->xdr, &readdir->rd_cookie) < 0)
1241 return nfserr_bad_xdr;
1242 status = nfsd4_decode_verifier4(argp, &readdir->rd_verf);
1243 if (status)
1244 return status;
1245 if (xdr_stream_decode_u32(argp->xdr, &readdir->rd_dircount) < 0)
1246 return nfserr_bad_xdr;
1247 if (xdr_stream_decode_u32(argp->xdr, &readdir->rd_maxcount) < 0)
1248 return nfserr_bad_xdr;
1249 if (xdr_stream_decode_uint32_array(argp->xdr, readdir->rd_bmval,
1250 ARRAY_SIZE(readdir->rd_bmval)) < 0)
1251 return nfserr_bad_xdr;
1253 return nfs_ok;
1256 static __be32
1257 nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
1259 return nfsd4_decode_component4(argp, &remove->rm_name, &remove->rm_namelen);
1262 static __be32
1263 nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
1265 __be32 status;
1267 status = nfsd4_decode_component4(argp, &rename->rn_sname, &rename->rn_snamelen);
1268 if (status)
1269 return status;
1270 return nfsd4_decode_component4(argp, &rename->rn_tname, &rename->rn_tnamelen);
1273 static __be32
1274 nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
1276 return nfsd4_decode_clientid4(argp, clientid);
1279 static __be32
1280 nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
1281 struct nfsd4_secinfo *secinfo)
1283 return nfsd4_decode_component4(argp, &secinfo->si_name, &secinfo->si_namelen);
1286 static __be32
1287 nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
1289 __be32 status;
1291 status = nfsd4_decode_stateid4(argp, &setattr->sa_stateid);
1292 if (status)
1293 return status;
1294 return nfsd4_decode_fattr4(argp, setattr->sa_bmval,
1295 ARRAY_SIZE(setattr->sa_bmval),
1296 &setattr->sa_iattr, &setattr->sa_acl,
1297 &setattr->sa_label, NULL);
1300 static __be32
1301 nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
1303 __be32 *p, status;
1305 if (argp->minorversion >= 1)
1306 return nfserr_notsupp;
1308 status = nfsd4_decode_verifier4(argp, &setclientid->se_verf);
1309 if (status)
1310 return status;
1311 status = nfsd4_decode_opaque(argp, &setclientid->se_name);
1312 if (status)
1313 return status;
1314 if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_prog) < 0)
1315 return nfserr_bad_xdr;
1316 if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_netid_len) < 0)
1317 return nfserr_bad_xdr;
1318 p = xdr_inline_decode(argp->xdr, setclientid->se_callback_netid_len);
1319 if (!p)
1320 return nfserr_bad_xdr;
1321 setclientid->se_callback_netid_val = svcxdr_tmpalloc(argp,
1322 setclientid->se_callback_netid_len);
1323 if (!setclientid->se_callback_netid_val)
1324 return nfserr_jukebox;
1325 memcpy(setclientid->se_callback_netid_val, p,
1326 setclientid->se_callback_netid_len);
1328 if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_addr_len) < 0)
1329 return nfserr_bad_xdr;
1330 p = xdr_inline_decode(argp->xdr, setclientid->se_callback_addr_len);
1331 if (!p)
1332 return nfserr_bad_xdr;
1333 setclientid->se_callback_addr_val = svcxdr_tmpalloc(argp,
1334 setclientid->se_callback_addr_len);
1335 if (!setclientid->se_callback_addr_val)
1336 return nfserr_jukebox;
1337 memcpy(setclientid->se_callback_addr_val, p,
1338 setclientid->se_callback_addr_len);
1339 if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_ident) < 0)
1340 return nfserr_bad_xdr;
1342 return nfs_ok;
1345 static __be32
1346 nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
1348 __be32 status;
1350 if (argp->minorversion >= 1)
1351 return nfserr_notsupp;
1353 status = nfsd4_decode_clientid4(argp, &scd_c->sc_clientid);
1354 if (status)
1355 return status;
1356 return nfsd4_decode_verifier4(argp, &scd_c->sc_confirm);
1359 /* Also used for NVERIFY */
1360 static __be32
1361 nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1363 __be32 *p, status;
1365 status = nfsd4_decode_bitmap4(argp, verify->ve_bmval,
1366 ARRAY_SIZE(verify->ve_bmval));
1367 if (status)
1368 return status;
1370 /* For convenience's sake, we compare raw xdr'd attributes in
1371 * nfsd4_proc_verify */
1373 if (xdr_stream_decode_u32(argp->xdr, &verify->ve_attrlen) < 0)
1374 return nfserr_bad_xdr;
1375 p = xdr_inline_decode(argp->xdr, verify->ve_attrlen);
1376 if (!p)
1377 return nfserr_bad_xdr;
1378 verify->ve_attrval = svcxdr_tmpalloc(argp, verify->ve_attrlen);
1379 if (!verify->ve_attrval)
1380 return nfserr_jukebox;
1381 memcpy(verify->ve_attrval, p, verify->ve_attrlen);
1383 return nfs_ok;
1386 static __be32
1387 nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1389 __be32 status;
1391 status = nfsd4_decode_stateid4(argp, &write->wr_stateid);
1392 if (status)
1393 return status;
1394 if (xdr_stream_decode_u64(argp->xdr, &write->wr_offset) < 0)
1395 return nfserr_bad_xdr;
1396 if (xdr_stream_decode_u32(argp->xdr, &write->wr_stable_how) < 0)
1397 return nfserr_bad_xdr;
1398 if (write->wr_stable_how > NFS_FILE_SYNC)
1399 return nfserr_bad_xdr;
1400 if (xdr_stream_decode_u32(argp->xdr, &write->wr_buflen) < 0)
1401 return nfserr_bad_xdr;
1402 if (!xdr_stream_subsegment(argp->xdr, &write->wr_payload, write->wr_buflen))
1403 return nfserr_bad_xdr;
1405 return nfs_ok;
1408 static __be32
1409 nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1411 __be32 status;
1413 if (argp->minorversion >= 1)
1414 return nfserr_notsupp;
1416 status = nfsd4_decode_state_owner4(argp, &rlockowner->rl_clientid,
1417 &rlockowner->rl_owner);
1418 if (status)
1419 return status;
1421 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1422 return nfserr_inval;
1424 return nfs_ok;
1427 static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp, struct nfsd4_backchannel_ctl *bc)
1429 if (xdr_stream_decode_u32(argp->xdr, &bc->bc_cb_program) < 0)
1430 return nfserr_bad_xdr;
1431 return nfsd4_decode_cb_sec(argp, &bc->bc_cb_sec);
1434 static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
1436 u32 use_conn_in_rdma_mode;
1437 __be32 status;
1439 status = nfsd4_decode_sessionid4(argp, &bcts->sessionid);
1440 if (status)
1441 return status;
1442 if (xdr_stream_decode_u32(argp->xdr, &bcts->dir) < 0)
1443 return nfserr_bad_xdr;
1444 if (xdr_stream_decode_u32(argp->xdr, &use_conn_in_rdma_mode) < 0)
1445 return nfserr_bad_xdr;
1447 return nfs_ok;
1450 static __be32
1451 nfsd4_decode_state_protect_ops(struct nfsd4_compoundargs *argp,
1452 struct nfsd4_exchange_id *exid)
1454 __be32 status;
1456 status = nfsd4_decode_bitmap4(argp, exid->spo_must_enforce,
1457 ARRAY_SIZE(exid->spo_must_enforce));
1458 if (status)
1459 return nfserr_bad_xdr;
1460 status = nfsd4_decode_bitmap4(argp, exid->spo_must_allow,
1461 ARRAY_SIZE(exid->spo_must_allow));
1462 if (status)
1463 return nfserr_bad_xdr;
1465 return nfs_ok;
1469 * This implementation currently does not support SP4_SSV.
1470 * This decoder simply skips over these arguments.
1472 static noinline __be32
1473 nfsd4_decode_ssv_sp_parms(struct nfsd4_compoundargs *argp,
1474 struct nfsd4_exchange_id *exid)
1476 u32 count, window, num_gss_handles;
1477 __be32 status;
1479 /* ssp_ops */
1480 status = nfsd4_decode_state_protect_ops(argp, exid);
1481 if (status)
1482 return status;
1484 /* ssp_hash_algs<> */
1485 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
1486 return nfserr_bad_xdr;
1487 while (count--) {
1488 status = nfsd4_decode_ignored_string(argp, 0);
1489 if (status)
1490 return status;
1493 /* ssp_encr_algs<> */
1494 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
1495 return nfserr_bad_xdr;
1496 while (count--) {
1497 status = nfsd4_decode_ignored_string(argp, 0);
1498 if (status)
1499 return status;
1502 if (xdr_stream_decode_u32(argp->xdr, &window) < 0)
1503 return nfserr_bad_xdr;
1504 if (xdr_stream_decode_u32(argp->xdr, &num_gss_handles) < 0)
1505 return nfserr_bad_xdr;
1507 return nfs_ok;
1510 static __be32
1511 nfsd4_decode_state_protect4_a(struct nfsd4_compoundargs *argp,
1512 struct nfsd4_exchange_id *exid)
1514 __be32 status;
1516 if (xdr_stream_decode_u32(argp->xdr, &exid->spa_how) < 0)
1517 return nfserr_bad_xdr;
1518 switch (exid->spa_how) {
1519 case SP4_NONE:
1520 break;
1521 case SP4_MACH_CRED:
1522 status = nfsd4_decode_state_protect_ops(argp, exid);
1523 if (status)
1524 return status;
1525 break;
1526 case SP4_SSV:
1527 status = nfsd4_decode_ssv_sp_parms(argp, exid);
1528 if (status)
1529 return status;
1530 break;
1531 default:
1532 return nfserr_bad_xdr;
1535 return nfs_ok;
1538 static __be32
1539 nfsd4_decode_nfs_impl_id4(struct nfsd4_compoundargs *argp,
1540 struct nfsd4_exchange_id *exid)
1542 __be32 status;
1543 u32 count;
1545 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
1546 return nfserr_bad_xdr;
1547 switch (count) {
1548 case 0:
1549 break;
1550 case 1:
1551 /* Note that RFC 8881 places no length limit on
1552 * nii_domain, but this implementation permits no
1553 * more than NFS4_OPAQUE_LIMIT bytes */
1554 status = nfsd4_decode_opaque(argp, &exid->nii_domain);
1555 if (status)
1556 return status;
1557 /* Note that RFC 8881 places no length limit on
1558 * nii_name, but this implementation permits no
1559 * more than NFS4_OPAQUE_LIMIT bytes */
1560 status = nfsd4_decode_opaque(argp, &exid->nii_name);
1561 if (status)
1562 return status;
1563 status = nfsd4_decode_nfstime4(argp, &exid->nii_time);
1564 if (status)
1565 return status;
1566 break;
1567 default:
1568 return nfserr_bad_xdr;
1571 return nfs_ok;
1574 static __be32
1575 nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
1576 struct nfsd4_exchange_id *exid)
1578 __be32 status;
1580 status = nfsd4_decode_verifier4(argp, &exid->verifier);
1581 if (status)
1582 return status;
1583 status = nfsd4_decode_opaque(argp, &exid->clname);
1584 if (status)
1585 return status;
1586 if (xdr_stream_decode_u32(argp->xdr, &exid->flags) < 0)
1587 return nfserr_bad_xdr;
1588 status = nfsd4_decode_state_protect4_a(argp, exid);
1589 if (status)
1590 return status;
1591 return nfsd4_decode_nfs_impl_id4(argp, exid);
1594 static __be32
1595 nfsd4_decode_channel_attrs4(struct nfsd4_compoundargs *argp,
1596 struct nfsd4_channel_attrs *ca)
1598 __be32 *p;
1600 p = xdr_inline_decode(argp->xdr, XDR_UNIT * 7);
1601 if (!p)
1602 return nfserr_bad_xdr;
1604 /* headerpadsz is ignored */
1605 p++;
1606 ca->maxreq_sz = be32_to_cpup(p++);
1607 ca->maxresp_sz = be32_to_cpup(p++);
1608 ca->maxresp_cached = be32_to_cpup(p++);
1609 ca->maxops = be32_to_cpup(p++);
1610 ca->maxreqs = be32_to_cpup(p++);
1611 ca->nr_rdma_attrs = be32_to_cpup(p);
1612 switch (ca->nr_rdma_attrs) {
1613 case 0:
1614 break;
1615 case 1:
1616 if (xdr_stream_decode_u32(argp->xdr, &ca->rdma_attrs) < 0)
1617 return nfserr_bad_xdr;
1618 break;
1619 default:
1620 return nfserr_bad_xdr;
1623 return nfs_ok;
1626 static __be32
1627 nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1628 struct nfsd4_create_session *sess)
1630 __be32 status;
1632 status = nfsd4_decode_clientid4(argp, &sess->clientid);
1633 if (status)
1634 return status;
1635 if (xdr_stream_decode_u32(argp->xdr, &sess->seqid) < 0)
1636 return nfserr_bad_xdr;
1637 if (xdr_stream_decode_u32(argp->xdr, &sess->flags) < 0)
1638 return nfserr_bad_xdr;
1639 status = nfsd4_decode_channel_attrs4(argp, &sess->fore_channel);
1640 if (status)
1641 return status;
1642 status = nfsd4_decode_channel_attrs4(argp, &sess->back_channel);
1643 if (status)
1644 return status;
1645 if (xdr_stream_decode_u32(argp->xdr, &sess->callback_prog) < 0)
1646 return nfserr_bad_xdr;
1647 status = nfsd4_decode_cb_sec(argp, &sess->cb_sec);
1648 if (status)
1649 return status;
1651 return nfs_ok;
1654 static __be32
1655 nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1656 struct nfsd4_destroy_session *destroy_session)
1658 return nfsd4_decode_sessionid4(argp, &destroy_session->sessionid);
1661 static __be32
1662 nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1663 struct nfsd4_free_stateid *free_stateid)
1665 return nfsd4_decode_stateid4(argp, &free_stateid->fr_stateid);
1668 #ifdef CONFIG_NFSD_PNFS
1669 static __be32
1670 nfsd4_decode_getdeviceinfo(struct nfsd4_compoundargs *argp,
1671 struct nfsd4_getdeviceinfo *gdev)
1673 __be32 status;
1675 status = nfsd4_decode_deviceid4(argp, &gdev->gd_devid);
1676 if (status)
1677 return status;
1678 if (xdr_stream_decode_u32(argp->xdr, &gdev->gd_layout_type) < 0)
1679 return nfserr_bad_xdr;
1680 if (xdr_stream_decode_u32(argp->xdr, &gdev->gd_maxcount) < 0)
1681 return nfserr_bad_xdr;
1682 if (xdr_stream_decode_uint32_array(argp->xdr,
1683 &gdev->gd_notify_types, 1) < 0)
1684 return nfserr_bad_xdr;
1686 return nfs_ok;
1689 static __be32
1690 nfsd4_decode_layoutcommit(struct nfsd4_compoundargs *argp,
1691 struct nfsd4_layoutcommit *lcp)
1693 __be32 *p, status;
1695 if (xdr_stream_decode_u64(argp->xdr, &lcp->lc_seg.offset) < 0)
1696 return nfserr_bad_xdr;
1697 if (xdr_stream_decode_u64(argp->xdr, &lcp->lc_seg.length) < 0)
1698 return nfserr_bad_xdr;
1699 if (xdr_stream_decode_bool(argp->xdr, &lcp->lc_reclaim) < 0)
1700 return nfserr_bad_xdr;
1701 status = nfsd4_decode_stateid4(argp, &lcp->lc_sid);
1702 if (status)
1703 return status;
1704 if (xdr_stream_decode_u32(argp->xdr, &lcp->lc_newoffset) < 0)
1705 return nfserr_bad_xdr;
1706 if (lcp->lc_newoffset) {
1707 if (xdr_stream_decode_u64(argp->xdr, &lcp->lc_last_wr) < 0)
1708 return nfserr_bad_xdr;
1709 } else
1710 lcp->lc_last_wr = 0;
1711 p = xdr_inline_decode(argp->xdr, XDR_UNIT);
1712 if (!p)
1713 return nfserr_bad_xdr;
1714 if (xdr_item_is_present(p)) {
1715 status = nfsd4_decode_nfstime4(argp, &lcp->lc_mtime);
1716 if (status)
1717 return status;
1718 } else {
1719 lcp->lc_mtime.tv_nsec = UTIME_NOW;
1721 return nfsd4_decode_layoutupdate4(argp, lcp);
1724 static __be32
1725 nfsd4_decode_layoutget(struct nfsd4_compoundargs *argp,
1726 struct nfsd4_layoutget *lgp)
1728 __be32 status;
1730 if (xdr_stream_decode_u32(argp->xdr, &lgp->lg_signal) < 0)
1731 return nfserr_bad_xdr;
1732 if (xdr_stream_decode_u32(argp->xdr, &lgp->lg_layout_type) < 0)
1733 return nfserr_bad_xdr;
1734 if (xdr_stream_decode_u32(argp->xdr, &lgp->lg_seg.iomode) < 0)
1735 return nfserr_bad_xdr;
1736 if (xdr_stream_decode_u64(argp->xdr, &lgp->lg_seg.offset) < 0)
1737 return nfserr_bad_xdr;
1738 if (xdr_stream_decode_u64(argp->xdr, &lgp->lg_seg.length) < 0)
1739 return nfserr_bad_xdr;
1740 if (xdr_stream_decode_u64(argp->xdr, &lgp->lg_minlength) < 0)
1741 return nfserr_bad_xdr;
1742 status = nfsd4_decode_stateid4(argp, &lgp->lg_sid);
1743 if (status)
1744 return status;
1745 if (xdr_stream_decode_u32(argp->xdr, &lgp->lg_maxcount) < 0)
1746 return nfserr_bad_xdr;
1748 return nfs_ok;
1751 static __be32
1752 nfsd4_decode_layoutreturn(struct nfsd4_compoundargs *argp,
1753 struct nfsd4_layoutreturn *lrp)
1755 if (xdr_stream_decode_bool(argp->xdr, &lrp->lr_reclaim) < 0)
1756 return nfserr_bad_xdr;
1757 if (xdr_stream_decode_u32(argp->xdr, &lrp->lr_layout_type) < 0)
1758 return nfserr_bad_xdr;
1759 if (xdr_stream_decode_u32(argp->xdr, &lrp->lr_seg.iomode) < 0)
1760 return nfserr_bad_xdr;
1761 return nfsd4_decode_layoutreturn4(argp, lrp);
1763 #endif /* CONFIG_NFSD_PNFS */
1765 static __be32 nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
1766 struct nfsd4_secinfo_no_name *sin)
1768 if (xdr_stream_decode_u32(argp->xdr, &sin->sin_style) < 0)
1769 return nfserr_bad_xdr;
1770 return nfs_ok;
1773 static __be32
1774 nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1775 struct nfsd4_sequence *seq)
1777 __be32 *p, status;
1779 status = nfsd4_decode_sessionid4(argp, &seq->sessionid);
1780 if (status)
1781 return status;
1782 p = xdr_inline_decode(argp->xdr, XDR_UNIT * 4);
1783 if (!p)
1784 return nfserr_bad_xdr;
1785 seq->seqid = be32_to_cpup(p++);
1786 seq->slotid = be32_to_cpup(p++);
1787 seq->maxslots = be32_to_cpup(p++);
1788 seq->cachethis = be32_to_cpup(p);
1790 return nfs_ok;
1793 static __be32
1794 nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1796 struct nfsd4_test_stateid_id *stateid;
1797 __be32 status;
1798 u32 i;
1800 if (xdr_stream_decode_u32(argp->xdr, &test_stateid->ts_num_ids) < 0)
1801 return nfserr_bad_xdr;
1803 INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
1804 for (i = 0; i < test_stateid->ts_num_ids; i++) {
1805 stateid = svcxdr_tmpalloc(argp, sizeof(*stateid));
1806 if (!stateid)
1807 return nfserrno(-ENOMEM); /* XXX: not jukebox? */
1808 INIT_LIST_HEAD(&stateid->ts_id_list);
1809 list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
1810 status = nfsd4_decode_stateid4(argp, &stateid->ts_id_stateid);
1811 if (status)
1812 return status;
1815 return nfs_ok;
1818 static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp,
1819 struct nfsd4_destroy_clientid *dc)
1821 return nfsd4_decode_clientid4(argp, &dc->clientid);
1824 static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp,
1825 struct nfsd4_reclaim_complete *rc)
1827 if (xdr_stream_decode_bool(argp->xdr, &rc->rca_one_fs) < 0)
1828 return nfserr_bad_xdr;
1829 return nfs_ok;
1832 static __be32
1833 nfsd4_decode_fallocate(struct nfsd4_compoundargs *argp,
1834 struct nfsd4_fallocate *fallocate)
1836 __be32 status;
1838 status = nfsd4_decode_stateid4(argp, &fallocate->falloc_stateid);
1839 if (status)
1840 return status;
1841 if (xdr_stream_decode_u64(argp->xdr, &fallocate->falloc_offset) < 0)
1842 return nfserr_bad_xdr;
1843 if (xdr_stream_decode_u64(argp->xdr, &fallocate->falloc_length) < 0)
1844 return nfserr_bad_xdr;
1846 return nfs_ok;
1849 static __be32 nfsd4_decode_nl4_server(struct nfsd4_compoundargs *argp,
1850 struct nl4_server *ns)
1852 struct nfs42_netaddr *naddr;
1853 __be32 *p;
1855 if (xdr_stream_decode_u32(argp->xdr, &ns->nl4_type) < 0)
1856 return nfserr_bad_xdr;
1858 /* currently support for 1 inter-server source server */
1859 switch (ns->nl4_type) {
1860 case NL4_NETADDR:
1861 naddr = &ns->u.nl4_addr;
1863 if (xdr_stream_decode_u32(argp->xdr, &naddr->netid_len) < 0)
1864 return nfserr_bad_xdr;
1865 if (naddr->netid_len > RPCBIND_MAXNETIDLEN)
1866 return nfserr_bad_xdr;
1868 p = xdr_inline_decode(argp->xdr, naddr->netid_len);
1869 if (!p)
1870 return nfserr_bad_xdr;
1871 memcpy(naddr->netid, p, naddr->netid_len);
1873 if (xdr_stream_decode_u32(argp->xdr, &naddr->addr_len) < 0)
1874 return nfserr_bad_xdr;
1875 if (naddr->addr_len > RPCBIND_MAXUADDRLEN)
1876 return nfserr_bad_xdr;
1878 p = xdr_inline_decode(argp->xdr, naddr->addr_len);
1879 if (!p)
1880 return nfserr_bad_xdr;
1881 memcpy(naddr->addr, p, naddr->addr_len);
1882 break;
1883 default:
1884 return nfserr_bad_xdr;
1887 return nfs_ok;
1890 static __be32
1891 nfsd4_decode_copy(struct nfsd4_compoundargs *argp, struct nfsd4_copy *copy)
1893 struct nl4_server *ns_dummy;
1894 u32 consecutive, i, count;
1895 __be32 status;
1897 status = nfsd4_decode_stateid4(argp, &copy->cp_src_stateid);
1898 if (status)
1899 return status;
1900 status = nfsd4_decode_stateid4(argp, &copy->cp_dst_stateid);
1901 if (status)
1902 return status;
1903 if (xdr_stream_decode_u64(argp->xdr, &copy->cp_src_pos) < 0)
1904 return nfserr_bad_xdr;
1905 if (xdr_stream_decode_u64(argp->xdr, &copy->cp_dst_pos) < 0)
1906 return nfserr_bad_xdr;
1907 if (xdr_stream_decode_u64(argp->xdr, &copy->cp_count) < 0)
1908 return nfserr_bad_xdr;
1909 /* ca_consecutive: we always do consecutive copies */
1910 if (xdr_stream_decode_u32(argp->xdr, &consecutive) < 0)
1911 return nfserr_bad_xdr;
1912 if (xdr_stream_decode_u32(argp->xdr, &copy->cp_synchronous) < 0)
1913 return nfserr_bad_xdr;
1915 if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
1916 return nfserr_bad_xdr;
1917 copy->cp_intra = false;
1918 if (count == 0) { /* intra-server copy */
1919 copy->cp_intra = true;
1920 return nfs_ok;
1923 /* decode all the supplied server addresses but use only the first */
1924 status = nfsd4_decode_nl4_server(argp, &copy->cp_src);
1925 if (status)
1926 return status;
1928 ns_dummy = kmalloc(sizeof(struct nl4_server), GFP_KERNEL);
1929 if (ns_dummy == NULL)
1930 return nfserrno(-ENOMEM); /* XXX: jukebox? */
1931 for (i = 0; i < count - 1; i++) {
1932 status = nfsd4_decode_nl4_server(argp, ns_dummy);
1933 if (status) {
1934 kfree(ns_dummy);
1935 return status;
1938 kfree(ns_dummy);
1940 return nfs_ok;
1943 static __be32
1944 nfsd4_decode_copy_notify(struct nfsd4_compoundargs *argp,
1945 struct nfsd4_copy_notify *cn)
1947 __be32 status;
1949 status = nfsd4_decode_stateid4(argp, &cn->cpn_src_stateid);
1950 if (status)
1951 return status;
1952 return nfsd4_decode_nl4_server(argp, &cn->cpn_dst);
1955 static __be32
1956 nfsd4_decode_offload_status(struct nfsd4_compoundargs *argp,
1957 struct nfsd4_offload_status *os)
1959 return nfsd4_decode_stateid4(argp, &os->stateid);
1962 static __be32
1963 nfsd4_decode_seek(struct nfsd4_compoundargs *argp, struct nfsd4_seek *seek)
1965 __be32 status;
1967 status = nfsd4_decode_stateid4(argp, &seek->seek_stateid);
1968 if (status)
1969 return status;
1970 if (xdr_stream_decode_u64(argp->xdr, &seek->seek_offset) < 0)
1971 return nfserr_bad_xdr;
1972 if (xdr_stream_decode_u32(argp->xdr, &seek->seek_whence) < 0)
1973 return nfserr_bad_xdr;
1975 return nfs_ok;
1978 static __be32
1979 nfsd4_decode_clone(struct nfsd4_compoundargs *argp, struct nfsd4_clone *clone)
1981 __be32 status;
1983 status = nfsd4_decode_stateid4(argp, &clone->cl_src_stateid);
1984 if (status)
1985 return status;
1986 status = nfsd4_decode_stateid4(argp, &clone->cl_dst_stateid);
1987 if (status)
1988 return status;
1989 if (xdr_stream_decode_u64(argp->xdr, &clone->cl_src_pos) < 0)
1990 return nfserr_bad_xdr;
1991 if (xdr_stream_decode_u64(argp->xdr, &clone->cl_dst_pos) < 0)
1992 return nfserr_bad_xdr;
1993 if (xdr_stream_decode_u64(argp->xdr, &clone->cl_count) < 0)
1994 return nfserr_bad_xdr;
1996 return nfs_ok;
2000 * XDR data that is more than PAGE_SIZE in size is normally part of a
2001 * read or write. However, the size of extended attributes is limited
2002 * by the maximum request size, and then further limited by the underlying
2003 * filesystem limits. This can exceed PAGE_SIZE (currently, XATTR_SIZE_MAX
2004 * is 64k). Since there is no kvec- or page-based interface to xattrs,
2005 * and we're not dealing with contiguous pages, we need to do some copying.
2009 * Decode data into buffer.
2011 static __be32
2012 nfsd4_vbuf_from_vector(struct nfsd4_compoundargs *argp, struct xdr_buf *xdr,
2013 char **bufp, u32 buflen)
2015 struct page **pages = xdr->pages;
2016 struct kvec *head = xdr->head;
2017 char *tmp, *dp;
2018 u32 len;
2020 if (buflen <= head->iov_len) {
2022 * We're in luck, the head has enough space. Just return
2023 * the head, no need for copying.
2025 *bufp = head->iov_base;
2026 return 0;
2029 tmp = svcxdr_tmpalloc(argp, buflen);
2030 if (tmp == NULL)
2031 return nfserr_jukebox;
2033 dp = tmp;
2034 memcpy(dp, head->iov_base, head->iov_len);
2035 buflen -= head->iov_len;
2036 dp += head->iov_len;
2038 while (buflen > 0) {
2039 len = min_t(u32, buflen, PAGE_SIZE);
2040 memcpy(dp, page_address(*pages), len);
2042 buflen -= len;
2043 dp += len;
2044 pages++;
2047 *bufp = tmp;
2048 return 0;
2052 * Get a user extended attribute name from the XDR buffer.
2053 * It will not have the "user." prefix, so prepend it.
2054 * Lastly, check for nul characters in the name.
2056 static __be32
2057 nfsd4_decode_xattr_name(struct nfsd4_compoundargs *argp, char **namep)
2059 char *name, *sp, *dp;
2060 u32 namelen, cnt;
2061 __be32 *p;
2063 if (xdr_stream_decode_u32(argp->xdr, &namelen) < 0)
2064 return nfserr_bad_xdr;
2065 if (namelen > (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN))
2066 return nfserr_nametoolong;
2067 if (namelen == 0)
2068 return nfserr_bad_xdr;
2069 p = xdr_inline_decode(argp->xdr, namelen);
2070 if (!p)
2071 return nfserr_bad_xdr;
2072 name = svcxdr_tmpalloc(argp, namelen + XATTR_USER_PREFIX_LEN + 1);
2073 if (!name)
2074 return nfserr_jukebox;
2075 memcpy(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
2078 * Copy the extended attribute name over while checking for 0
2079 * characters.
2081 sp = (char *)p;
2082 dp = name + XATTR_USER_PREFIX_LEN;
2083 cnt = namelen;
2085 while (cnt-- > 0) {
2086 if (*sp == '\0')
2087 return nfserr_bad_xdr;
2088 *dp++ = *sp++;
2090 *dp = '\0';
2092 *namep = name;
2094 return nfs_ok;
2098 * A GETXATTR op request comes without a length specifier. We just set the
2099 * maximum length for the reply based on XATTR_SIZE_MAX and the maximum
2100 * channel reply size. nfsd_getxattr will probe the length of the xattr,
2101 * check it against getxa_len, and allocate + return the value.
2103 static __be32
2104 nfsd4_decode_getxattr(struct nfsd4_compoundargs *argp,
2105 struct nfsd4_getxattr *getxattr)
2107 __be32 status;
2108 u32 maxcount;
2110 status = nfsd4_decode_xattr_name(argp, &getxattr->getxa_name);
2111 if (status)
2112 return status;
2114 maxcount = svc_max_payload(argp->rqstp);
2115 maxcount = min_t(u32, XATTR_SIZE_MAX, maxcount);
2117 getxattr->getxa_len = maxcount;
2119 return status;
2122 static __be32
2123 nfsd4_decode_setxattr(struct nfsd4_compoundargs *argp,
2124 struct nfsd4_setxattr *setxattr)
2126 u32 flags, maxcount, size;
2127 __be32 status;
2129 if (xdr_stream_decode_u32(argp->xdr, &flags) < 0)
2130 return nfserr_bad_xdr;
2132 if (flags > SETXATTR4_REPLACE)
2133 return nfserr_inval;
2134 setxattr->setxa_flags = flags;
2136 status = nfsd4_decode_xattr_name(argp, &setxattr->setxa_name);
2137 if (status)
2138 return status;
2140 maxcount = svc_max_payload(argp->rqstp);
2141 maxcount = min_t(u32, XATTR_SIZE_MAX, maxcount);
2143 if (xdr_stream_decode_u32(argp->xdr, &size) < 0)
2144 return nfserr_bad_xdr;
2145 if (size > maxcount)
2146 return nfserr_xattr2big;
2148 setxattr->setxa_len = size;
2149 if (size > 0) {
2150 struct xdr_buf payload;
2152 if (!xdr_stream_subsegment(argp->xdr, &payload, size))
2153 return nfserr_bad_xdr;
2154 status = nfsd4_vbuf_from_vector(argp, &payload,
2155 &setxattr->setxa_buf, size);
2158 return nfs_ok;
2161 static __be32
2162 nfsd4_decode_listxattrs(struct nfsd4_compoundargs *argp,
2163 struct nfsd4_listxattrs *listxattrs)
2165 u32 maxcount;
2167 if (xdr_stream_decode_u64(argp->xdr, &listxattrs->lsxa_cookie) < 0)
2168 return nfserr_bad_xdr;
2171 * If the cookie is too large to have even one user.x attribute
2172 * plus trailing '\0' left in a maximum size buffer, it's invalid.
2174 if (listxattrs->lsxa_cookie >=
2175 (XATTR_LIST_MAX / (XATTR_USER_PREFIX_LEN + 2)))
2176 return nfserr_badcookie;
2178 if (xdr_stream_decode_u32(argp->xdr, &maxcount) < 0)
2179 return nfserr_bad_xdr;
2180 if (maxcount < 8)
2181 /* Always need at least 2 words (length and one character) */
2182 return nfserr_inval;
2184 maxcount = min(maxcount, svc_max_payload(argp->rqstp));
2185 listxattrs->lsxa_maxcount = maxcount;
2187 return nfs_ok;
2190 static __be32
2191 nfsd4_decode_removexattr(struct nfsd4_compoundargs *argp,
2192 struct nfsd4_removexattr *removexattr)
2194 return nfsd4_decode_xattr_name(argp, &removexattr->rmxa_name);
2197 static __be32
2198 nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
2200 return nfs_ok;
2203 static __be32
2204 nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
2206 return nfserr_notsupp;
2209 typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
2211 static const nfsd4_dec nfsd4_dec_ops[] = {
2212 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
2213 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
2214 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
2215 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
2216 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
2217 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
2218 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
2219 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
2220 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
2221 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
2222 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
2223 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
2224 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
2225 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
2226 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
2227 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
2228 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
2229 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
2230 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
2231 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
2232 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_putpubfh,
2233 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
2234 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
2235 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
2236 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
2237 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
2238 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
2239 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
2240 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
2241 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
2242 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
2243 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
2244 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
2245 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
2246 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
2247 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
2248 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
2250 /* new operations for NFSv4.1 */
2251 [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
2252 [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
2253 [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
2254 [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
2255 [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
2256 [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
2257 [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
2258 #ifdef CONFIG_NFSD_PNFS
2259 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_getdeviceinfo,
2260 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
2261 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_layoutcommit,
2262 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_layoutget,
2263 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_layoutreturn,
2264 #else
2265 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
2266 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
2267 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
2268 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
2269 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
2270 #endif
2271 [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
2272 [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
2273 [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
2274 [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
2275 [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
2276 [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
2277 [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
2279 /* new operations for NFSv4.2 */
2280 [OP_ALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
2281 [OP_COPY] = (nfsd4_dec)nfsd4_decode_copy,
2282 [OP_COPY_NOTIFY] = (nfsd4_dec)nfsd4_decode_copy_notify,
2283 [OP_DEALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
2284 [OP_IO_ADVISE] = (nfsd4_dec)nfsd4_decode_notsupp,
2285 [OP_LAYOUTERROR] = (nfsd4_dec)nfsd4_decode_notsupp,
2286 [OP_LAYOUTSTATS] = (nfsd4_dec)nfsd4_decode_notsupp,
2287 [OP_OFFLOAD_CANCEL] = (nfsd4_dec)nfsd4_decode_offload_status,
2288 [OP_OFFLOAD_STATUS] = (nfsd4_dec)nfsd4_decode_offload_status,
2289 [OP_READ_PLUS] = (nfsd4_dec)nfsd4_decode_read,
2290 [OP_SEEK] = (nfsd4_dec)nfsd4_decode_seek,
2291 [OP_WRITE_SAME] = (nfsd4_dec)nfsd4_decode_notsupp,
2292 [OP_CLONE] = (nfsd4_dec)nfsd4_decode_clone,
2293 /* RFC 8276 extended atributes operations */
2294 [OP_GETXATTR] = (nfsd4_dec)nfsd4_decode_getxattr,
2295 [OP_SETXATTR] = (nfsd4_dec)nfsd4_decode_setxattr,
2296 [OP_LISTXATTRS] = (nfsd4_dec)nfsd4_decode_listxattrs,
2297 [OP_REMOVEXATTR] = (nfsd4_dec)nfsd4_decode_removexattr,
2300 static inline bool
2301 nfsd4_opnum_in_range(struct nfsd4_compoundargs *argp, struct nfsd4_op *op)
2303 if (op->opnum < FIRST_NFS4_OP)
2304 return false;
2305 else if (argp->minorversion == 0 && op->opnum > LAST_NFS40_OP)
2306 return false;
2307 else if (argp->minorversion == 1 && op->opnum > LAST_NFS41_OP)
2308 return false;
2309 else if (argp->minorversion == 2 && op->opnum > LAST_NFS42_OP)
2310 return false;
2311 return true;
2314 static int
2315 nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
2317 struct nfsd4_op *op;
2318 bool cachethis = false;
2319 int auth_slack= argp->rqstp->rq_auth_slack;
2320 int max_reply = auth_slack + 8; /* opcnt, status */
2321 int readcount = 0;
2322 int readbytes = 0;
2323 __be32 *p;
2324 int i;
2326 if (xdr_stream_decode_u32(argp->xdr, &argp->taglen) < 0)
2327 return 0;
2328 max_reply += XDR_UNIT;
2329 argp->tag = NULL;
2330 if (unlikely(argp->taglen)) {
2331 if (argp->taglen > NFSD4_MAX_TAGLEN)
2332 return 0;
2333 p = xdr_inline_decode(argp->xdr, argp->taglen);
2334 if (!p)
2335 return 0;
2336 argp->tag = svcxdr_tmpalloc(argp, argp->taglen);
2337 if (!argp->tag)
2338 return 0;
2339 memcpy(argp->tag, p, argp->taglen);
2340 max_reply += xdr_align_size(argp->taglen);
2343 if (xdr_stream_decode_u32(argp->xdr, &argp->minorversion) < 0)
2344 return 0;
2345 if (xdr_stream_decode_u32(argp->xdr, &argp->opcnt) < 0)
2346 return 0;
2349 * NFS4ERR_RESOURCE is a more helpful error than GARBAGE_ARGS
2350 * here, so we return success at the xdr level so that
2351 * nfsd4_proc can handle this is an NFS-level error.
2353 if (argp->opcnt > NFSD_MAX_OPS_PER_COMPOUND)
2354 return 1;
2356 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
2357 argp->ops = kzalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
2358 if (!argp->ops) {
2359 argp->ops = argp->iops;
2360 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
2361 return 0;
2365 if (argp->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
2366 argp->opcnt = 0;
2368 for (i = 0; i < argp->opcnt; i++) {
2369 op = &argp->ops[i];
2370 op->replay = NULL;
2372 if (xdr_stream_decode_u32(argp->xdr, &op->opnum) < 0)
2373 return 0;
2374 if (nfsd4_opnum_in_range(argp, op)) {
2375 op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
2376 if (op->status != nfs_ok)
2377 trace_nfsd_compound_decode_err(argp->rqstp,
2378 argp->opcnt, i,
2379 op->opnum,
2380 op->status);
2381 } else {
2382 op->opnum = OP_ILLEGAL;
2383 op->status = nfserr_op_illegal;
2385 op->opdesc = OPDESC(op);
2387 * We'll try to cache the result in the DRC if any one
2388 * op in the compound wants to be cached:
2390 cachethis |= nfsd4_cache_this_op(op);
2392 if (op->opnum == OP_READ || op->opnum == OP_READ_PLUS) {
2393 readcount++;
2394 readbytes += nfsd4_max_reply(argp->rqstp, op);
2395 } else
2396 max_reply += nfsd4_max_reply(argp->rqstp, op);
2398 * OP_LOCK and OP_LOCKT may return a conflicting lock.
2399 * (Special case because it will just skip encoding this
2400 * if it runs out of xdr buffer space, and it is the only
2401 * operation that behaves this way.)
2403 if (op->opnum == OP_LOCK || op->opnum == OP_LOCKT)
2404 max_reply += NFS4_OPAQUE_LIMIT;
2406 if (op->status) {
2407 argp->opcnt = i+1;
2408 break;
2411 /* Sessions make the DRC unnecessary: */
2412 if (argp->minorversion)
2413 cachethis = false;
2414 svc_reserve(argp->rqstp, max_reply + readbytes);
2415 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
2417 if (readcount > 1 || max_reply > PAGE_SIZE - auth_slack)
2418 clear_bit(RQ_SPLICE_OK, &argp->rqstp->rq_flags);
2420 return 1;
2423 static __be32 *encode_change(__be32 *p, struct kstat *stat, struct inode *inode,
2424 struct svc_export *exp)
2426 if (exp->ex_flags & NFSEXP_V4ROOT) {
2427 *p++ = cpu_to_be32(convert_to_wallclock(exp->cd->flush_time));
2428 *p++ = 0;
2429 } else
2430 p = xdr_encode_hyper(p, nfsd4_change_attribute(stat, inode));
2431 return p;
2435 * ctime (in NFSv4, time_metadata) is not writeable, and the client
2436 * doesn't really care what resolution could theoretically be stored by
2437 * the filesystem.
2439 * The client cares how close together changes can be while still
2440 * guaranteeing ctime changes. For most filesystems (which have
2441 * timestamps with nanosecond fields) that is limited by the resolution
2442 * of the time returned from current_time() (which I'm assuming to be
2443 * 1/HZ).
2445 static __be32 *encode_time_delta(__be32 *p, struct inode *inode)
2447 struct timespec64 ts;
2448 u32 ns;
2450 ns = max_t(u32, NSEC_PER_SEC/HZ, inode->i_sb->s_time_gran);
2451 ts = ns_to_timespec64(ns);
2453 p = xdr_encode_hyper(p, ts.tv_sec);
2454 *p++ = cpu_to_be32(ts.tv_nsec);
2456 return p;
2459 static __be32 *encode_cinfo(__be32 *p, struct nfsd4_change_info *c)
2461 *p++ = cpu_to_be32(c->atomic);
2462 p = xdr_encode_hyper(p, c->before_change);
2463 p = xdr_encode_hyper(p, c->after_change);
2464 return p;
2467 /* Encode as an array of strings the string given with components
2468 * separated @sep, escaped with esc_enter and esc_exit.
2470 static __be32 nfsd4_encode_components_esc(struct xdr_stream *xdr, char sep,
2471 char *components, char esc_enter,
2472 char esc_exit)
2474 __be32 *p;
2475 __be32 pathlen;
2476 int pathlen_offset;
2477 int strlen, count=0;
2478 char *str, *end, *next;
2480 dprintk("nfsd4_encode_components(%s)\n", components);
2482 pathlen_offset = xdr->buf->len;
2483 p = xdr_reserve_space(xdr, 4);
2484 if (!p)
2485 return nfserr_resource;
2486 p++; /* We will fill this in with @count later */
2488 end = str = components;
2489 while (*end) {
2490 bool found_esc = false;
2492 /* try to parse as esc_start, ..., esc_end, sep */
2493 if (*str == esc_enter) {
2494 for (; *end && (*end != esc_exit); end++)
2495 /* find esc_exit or end of string */;
2496 next = end + 1;
2497 if (*end && (!*next || *next == sep)) {
2498 str++;
2499 found_esc = true;
2503 if (!found_esc)
2504 for (; *end && (*end != sep); end++)
2505 /* find sep or end of string */;
2507 strlen = end - str;
2508 if (strlen) {
2509 p = xdr_reserve_space(xdr, strlen + 4);
2510 if (!p)
2511 return nfserr_resource;
2512 p = xdr_encode_opaque(p, str, strlen);
2513 count++;
2515 else
2516 end++;
2517 if (found_esc)
2518 end = next;
2520 str = end;
2522 pathlen = htonl(count);
2523 write_bytes_to_xdr_buf(xdr->buf, pathlen_offset, &pathlen, 4);
2524 return 0;
2527 /* Encode as an array of strings the string given with components
2528 * separated @sep.
2530 static __be32 nfsd4_encode_components(struct xdr_stream *xdr, char sep,
2531 char *components)
2533 return nfsd4_encode_components_esc(xdr, sep, components, 0, 0);
2537 * encode a location element of a fs_locations structure
2539 static __be32 nfsd4_encode_fs_location4(struct xdr_stream *xdr,
2540 struct nfsd4_fs_location *location)
2542 __be32 status;
2544 status = nfsd4_encode_components_esc(xdr, ':', location->hosts,
2545 '[', ']');
2546 if (status)
2547 return status;
2548 status = nfsd4_encode_components(xdr, '/', location->path);
2549 if (status)
2550 return status;
2551 return 0;
2555 * Encode a path in RFC3530 'pathname4' format
2557 static __be32 nfsd4_encode_path(struct xdr_stream *xdr,
2558 const struct path *root,
2559 const struct path *path)
2561 struct path cur = *path;
2562 __be32 *p;
2563 struct dentry **components = NULL;
2564 unsigned int ncomponents = 0;
2565 __be32 err = nfserr_jukebox;
2567 dprintk("nfsd4_encode_components(");
2569 path_get(&cur);
2570 /* First walk the path up to the nfsd root, and store the
2571 * dentries/path components in an array.
2573 for (;;) {
2574 if (path_equal(&cur, root))
2575 break;
2576 if (cur.dentry == cur.mnt->mnt_root) {
2577 if (follow_up(&cur))
2578 continue;
2579 goto out_free;
2581 if ((ncomponents & 15) == 0) {
2582 struct dentry **new;
2583 new = krealloc(components,
2584 sizeof(*new) * (ncomponents + 16),
2585 GFP_KERNEL);
2586 if (!new)
2587 goto out_free;
2588 components = new;
2590 components[ncomponents++] = cur.dentry;
2591 cur.dentry = dget_parent(cur.dentry);
2593 err = nfserr_resource;
2594 p = xdr_reserve_space(xdr, 4);
2595 if (!p)
2596 goto out_free;
2597 *p++ = cpu_to_be32(ncomponents);
2599 while (ncomponents) {
2600 struct dentry *dentry = components[ncomponents - 1];
2601 unsigned int len;
2603 spin_lock(&dentry->d_lock);
2604 len = dentry->d_name.len;
2605 p = xdr_reserve_space(xdr, len + 4);
2606 if (!p) {
2607 spin_unlock(&dentry->d_lock);
2608 goto out_free;
2610 p = xdr_encode_opaque(p, dentry->d_name.name, len);
2611 dprintk("/%pd", dentry);
2612 spin_unlock(&dentry->d_lock);
2613 dput(dentry);
2614 ncomponents--;
2617 err = 0;
2618 out_free:
2619 dprintk(")\n");
2620 while (ncomponents)
2621 dput(components[--ncomponents]);
2622 kfree(components);
2623 path_put(&cur);
2624 return err;
2627 static __be32 nfsd4_encode_fsloc_fsroot(struct xdr_stream *xdr,
2628 struct svc_rqst *rqstp, const struct path *path)
2630 struct svc_export *exp_ps;
2631 __be32 res;
2633 exp_ps = rqst_find_fsidzero_export(rqstp);
2634 if (IS_ERR(exp_ps))
2635 return nfserrno(PTR_ERR(exp_ps));
2636 res = nfsd4_encode_path(xdr, &exp_ps->ex_path, path);
2637 exp_put(exp_ps);
2638 return res;
2642 * encode a fs_locations structure
2644 static __be32 nfsd4_encode_fs_locations(struct xdr_stream *xdr,
2645 struct svc_rqst *rqstp, struct svc_export *exp)
2647 __be32 status;
2648 int i;
2649 __be32 *p;
2650 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
2652 status = nfsd4_encode_fsloc_fsroot(xdr, rqstp, &exp->ex_path);
2653 if (status)
2654 return status;
2655 p = xdr_reserve_space(xdr, 4);
2656 if (!p)
2657 return nfserr_resource;
2658 *p++ = cpu_to_be32(fslocs->locations_count);
2659 for (i=0; i<fslocs->locations_count; i++) {
2660 status = nfsd4_encode_fs_location4(xdr, &fslocs->locations[i]);
2661 if (status)
2662 return status;
2664 return 0;
2667 static u32 nfs4_file_type(umode_t mode)
2669 switch (mode & S_IFMT) {
2670 case S_IFIFO: return NF4FIFO;
2671 case S_IFCHR: return NF4CHR;
2672 case S_IFDIR: return NF4DIR;
2673 case S_IFBLK: return NF4BLK;
2674 case S_IFLNK: return NF4LNK;
2675 case S_IFREG: return NF4REG;
2676 case S_IFSOCK: return NF4SOCK;
2677 default: return NF4BAD;
2681 static inline __be32
2682 nfsd4_encode_aclname(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2683 struct nfs4_ace *ace)
2685 if (ace->whotype != NFS4_ACL_WHO_NAMED)
2686 return nfs4_acl_write_who(xdr, ace->whotype);
2687 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
2688 return nfsd4_encode_group(xdr, rqstp, ace->who_gid);
2689 else
2690 return nfsd4_encode_user(xdr, rqstp, ace->who_uid);
2693 static inline __be32
2694 nfsd4_encode_layout_types(struct xdr_stream *xdr, u32 layout_types)
2696 __be32 *p;
2697 unsigned long i = hweight_long(layout_types);
2699 p = xdr_reserve_space(xdr, 4 + 4 * i);
2700 if (!p)
2701 return nfserr_resource;
2703 *p++ = cpu_to_be32(i);
2705 for (i = LAYOUT_NFSV4_1_FILES; i < LAYOUT_TYPE_MAX; ++i)
2706 if (layout_types & (1 << i))
2707 *p++ = cpu_to_be32(i);
2709 return 0;
2712 #define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
2713 FATTR4_WORD0_RDATTR_ERROR)
2714 #define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
2715 #define WORD2_ABSENT_FS_ATTRS 0
2717 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2718 static inline __be32
2719 nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2720 void *context, int len)
2722 __be32 *p;
2724 p = xdr_reserve_space(xdr, len + 4 + 4 + 4);
2725 if (!p)
2726 return nfserr_resource;
2729 * For now we use a 0 here to indicate the null translation; in
2730 * the future we may place a call to translation code here.
2732 *p++ = cpu_to_be32(0); /* lfs */
2733 *p++ = cpu_to_be32(0); /* pi */
2734 p = xdr_encode_opaque(p, context, len);
2735 return 0;
2737 #else
2738 static inline __be32
2739 nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2740 void *context, int len)
2741 { return 0; }
2742 #endif
2744 static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *bmval2, u32 *rdattr_err)
2746 /* As per referral draft: */
2747 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
2748 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
2749 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
2750 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
2751 *rdattr_err = NFSERR_MOVED;
2752 else
2753 return nfserr_moved;
2755 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
2756 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
2757 *bmval2 &= WORD2_ABSENT_FS_ATTRS;
2758 return 0;
2762 static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
2764 struct path path = exp->ex_path;
2765 int err;
2767 path_get(&path);
2768 while (follow_up(&path)) {
2769 if (path.dentry != path.mnt->mnt_root)
2770 break;
2772 err = vfs_getattr(&path, stat, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
2773 path_put(&path);
2774 return err;
2777 static __be32
2778 nfsd4_encode_bitmap(struct xdr_stream *xdr, u32 bmval0, u32 bmval1, u32 bmval2)
2780 __be32 *p;
2782 if (bmval2) {
2783 p = xdr_reserve_space(xdr, 16);
2784 if (!p)
2785 goto out_resource;
2786 *p++ = cpu_to_be32(3);
2787 *p++ = cpu_to_be32(bmval0);
2788 *p++ = cpu_to_be32(bmval1);
2789 *p++ = cpu_to_be32(bmval2);
2790 } else if (bmval1) {
2791 p = xdr_reserve_space(xdr, 12);
2792 if (!p)
2793 goto out_resource;
2794 *p++ = cpu_to_be32(2);
2795 *p++ = cpu_to_be32(bmval0);
2796 *p++ = cpu_to_be32(bmval1);
2797 } else {
2798 p = xdr_reserve_space(xdr, 8);
2799 if (!p)
2800 goto out_resource;
2801 *p++ = cpu_to_be32(1);
2802 *p++ = cpu_to_be32(bmval0);
2805 return 0;
2806 out_resource:
2807 return nfserr_resource;
2811 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
2812 * ourselves.
2814 static __be32
2815 nfsd4_encode_fattr(struct xdr_stream *xdr, struct svc_fh *fhp,
2816 struct svc_export *exp,
2817 struct dentry *dentry, u32 *bmval,
2818 struct svc_rqst *rqstp, int ignore_crossmnt)
2820 u32 bmval0 = bmval[0];
2821 u32 bmval1 = bmval[1];
2822 u32 bmval2 = bmval[2];
2823 struct kstat stat;
2824 struct svc_fh *tempfh = NULL;
2825 struct kstatfs statfs;
2826 __be32 *p;
2827 int starting_len = xdr->buf->len;
2828 int attrlen_offset;
2829 __be32 attrlen;
2830 u32 dummy;
2831 u64 dummy64;
2832 u32 rdattr_err = 0;
2833 __be32 status;
2834 int err;
2835 struct nfs4_acl *acl = NULL;
2836 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2837 void *context = NULL;
2838 int contextlen;
2839 #endif
2840 bool contextsupport = false;
2841 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2842 u32 minorversion = resp->cstate.minorversion;
2843 struct path path = {
2844 .mnt = exp->ex_path.mnt,
2845 .dentry = dentry,
2847 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2849 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
2850 BUG_ON(!nfsd_attrs_supported(minorversion, bmval));
2852 if (exp->ex_fslocs.migrated) {
2853 status = fattr_handle_absent_fs(&bmval0, &bmval1, &bmval2, &rdattr_err);
2854 if (status)
2855 goto out;
2858 err = vfs_getattr(&path, &stat, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
2859 if (err)
2860 goto out_nfserr;
2861 if ((bmval0 & (FATTR4_WORD0_FILES_AVAIL | FATTR4_WORD0_FILES_FREE |
2862 FATTR4_WORD0_FILES_TOTAL | FATTR4_WORD0_MAXNAME)) ||
2863 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2864 FATTR4_WORD1_SPACE_TOTAL))) {
2865 err = vfs_statfs(&path, &statfs);
2866 if (err)
2867 goto out_nfserr;
2869 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
2870 tempfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
2871 status = nfserr_jukebox;
2872 if (!tempfh)
2873 goto out;
2874 fh_init(tempfh, NFS4_FHSIZE);
2875 status = fh_compose(tempfh, exp, dentry, NULL);
2876 if (status)
2877 goto out;
2878 fhp = tempfh;
2880 if (bmval0 & FATTR4_WORD0_ACL) {
2881 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
2882 if (err == -EOPNOTSUPP)
2883 bmval0 &= ~FATTR4_WORD0_ACL;
2884 else if (err == -EINVAL) {
2885 status = nfserr_attrnotsupp;
2886 goto out;
2887 } else if (err != 0)
2888 goto out_nfserr;
2891 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2892 if ((bmval2 & FATTR4_WORD2_SECURITY_LABEL) ||
2893 bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
2894 if (exp->ex_flags & NFSEXP_SECURITY_LABEL)
2895 err = security_inode_getsecctx(d_inode(dentry),
2896 &context, &contextlen);
2897 else
2898 err = -EOPNOTSUPP;
2899 contextsupport = (err == 0);
2900 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2901 if (err == -EOPNOTSUPP)
2902 bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2903 else if (err)
2904 goto out_nfserr;
2907 #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2909 status = nfsd4_encode_bitmap(xdr, bmval0, bmval1, bmval2);
2910 if (status)
2911 goto out;
2913 attrlen_offset = xdr->buf->len;
2914 p = xdr_reserve_space(xdr, 4);
2915 if (!p)
2916 goto out_resource;
2917 p++; /* to be backfilled later */
2919 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
2920 u32 supp[3];
2922 memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
2924 if (!IS_POSIXACL(dentry->d_inode))
2925 supp[0] &= ~FATTR4_WORD0_ACL;
2926 if (!contextsupport)
2927 supp[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2928 if (!supp[2]) {
2929 p = xdr_reserve_space(xdr, 12);
2930 if (!p)
2931 goto out_resource;
2932 *p++ = cpu_to_be32(2);
2933 *p++ = cpu_to_be32(supp[0]);
2934 *p++ = cpu_to_be32(supp[1]);
2935 } else {
2936 p = xdr_reserve_space(xdr, 16);
2937 if (!p)
2938 goto out_resource;
2939 *p++ = cpu_to_be32(3);
2940 *p++ = cpu_to_be32(supp[0]);
2941 *p++ = cpu_to_be32(supp[1]);
2942 *p++ = cpu_to_be32(supp[2]);
2945 if (bmval0 & FATTR4_WORD0_TYPE) {
2946 p = xdr_reserve_space(xdr, 4);
2947 if (!p)
2948 goto out_resource;
2949 dummy = nfs4_file_type(stat.mode);
2950 if (dummy == NF4BAD) {
2951 status = nfserr_serverfault;
2952 goto out;
2954 *p++ = cpu_to_be32(dummy);
2956 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
2957 p = xdr_reserve_space(xdr, 4);
2958 if (!p)
2959 goto out_resource;
2960 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
2961 *p++ = cpu_to_be32(NFS4_FH_PERSISTENT);
2962 else
2963 *p++ = cpu_to_be32(NFS4_FH_PERSISTENT|
2964 NFS4_FH_VOL_RENAME);
2966 if (bmval0 & FATTR4_WORD0_CHANGE) {
2967 p = xdr_reserve_space(xdr, 8);
2968 if (!p)
2969 goto out_resource;
2970 p = encode_change(p, &stat, d_inode(dentry), exp);
2972 if (bmval0 & FATTR4_WORD0_SIZE) {
2973 p = xdr_reserve_space(xdr, 8);
2974 if (!p)
2975 goto out_resource;
2976 p = xdr_encode_hyper(p, stat.size);
2978 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
2979 p = xdr_reserve_space(xdr, 4);
2980 if (!p)
2981 goto out_resource;
2982 *p++ = cpu_to_be32(1);
2984 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
2985 p = xdr_reserve_space(xdr, 4);
2986 if (!p)
2987 goto out_resource;
2988 *p++ = cpu_to_be32(1);
2990 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
2991 p = xdr_reserve_space(xdr, 4);
2992 if (!p)
2993 goto out_resource;
2994 *p++ = cpu_to_be32(0);
2996 if (bmval0 & FATTR4_WORD0_FSID) {
2997 p = xdr_reserve_space(xdr, 16);
2998 if (!p)
2999 goto out_resource;
3000 if (exp->ex_fslocs.migrated) {
3001 p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MAJOR);
3002 p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MINOR);
3003 } else switch(fsid_source(fhp)) {
3004 case FSIDSOURCE_FSID:
3005 p = xdr_encode_hyper(p, (u64)exp->ex_fsid);
3006 p = xdr_encode_hyper(p, (u64)0);
3007 break;
3008 case FSIDSOURCE_DEV:
3009 *p++ = cpu_to_be32(0);
3010 *p++ = cpu_to_be32(MAJOR(stat.dev));
3011 *p++ = cpu_to_be32(0);
3012 *p++ = cpu_to_be32(MINOR(stat.dev));
3013 break;
3014 case FSIDSOURCE_UUID:
3015 p = xdr_encode_opaque_fixed(p, exp->ex_uuid,
3016 EX_UUID_LEN);
3017 break;
3020 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
3021 p = xdr_reserve_space(xdr, 4);
3022 if (!p)
3023 goto out_resource;
3024 *p++ = cpu_to_be32(0);
3026 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
3027 p = xdr_reserve_space(xdr, 4);
3028 if (!p)
3029 goto out_resource;
3030 *p++ = cpu_to_be32(nn->nfsd4_lease);
3032 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
3033 p = xdr_reserve_space(xdr, 4);
3034 if (!p)
3035 goto out_resource;
3036 *p++ = cpu_to_be32(rdattr_err);
3038 if (bmval0 & FATTR4_WORD0_ACL) {
3039 struct nfs4_ace *ace;
3041 if (acl == NULL) {
3042 p = xdr_reserve_space(xdr, 4);
3043 if (!p)
3044 goto out_resource;
3046 *p++ = cpu_to_be32(0);
3047 goto out_acl;
3049 p = xdr_reserve_space(xdr, 4);
3050 if (!p)
3051 goto out_resource;
3052 *p++ = cpu_to_be32(acl->naces);
3054 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
3055 p = xdr_reserve_space(xdr, 4*3);
3056 if (!p)
3057 goto out_resource;
3058 *p++ = cpu_to_be32(ace->type);
3059 *p++ = cpu_to_be32(ace->flag);
3060 *p++ = cpu_to_be32(ace->access_mask &
3061 NFS4_ACE_MASK_ALL);
3062 status = nfsd4_encode_aclname(xdr, rqstp, ace);
3063 if (status)
3064 goto out;
3067 out_acl:
3068 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
3069 p = xdr_reserve_space(xdr, 4);
3070 if (!p)
3071 goto out_resource;
3072 *p++ = cpu_to_be32(IS_POSIXACL(dentry->d_inode) ?
3073 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
3075 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
3076 p = xdr_reserve_space(xdr, 4);
3077 if (!p)
3078 goto out_resource;
3079 *p++ = cpu_to_be32(1);
3081 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
3082 p = xdr_reserve_space(xdr, 4);
3083 if (!p)
3084 goto out_resource;
3085 *p++ = cpu_to_be32(0);
3087 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
3088 p = xdr_reserve_space(xdr, 4);
3089 if (!p)
3090 goto out_resource;
3091 *p++ = cpu_to_be32(1);
3093 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
3094 p = xdr_reserve_space(xdr, 4);
3095 if (!p)
3096 goto out_resource;
3097 *p++ = cpu_to_be32(1);
3099 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
3100 p = xdr_reserve_space(xdr, fhp->fh_handle.fh_size + 4);
3101 if (!p)
3102 goto out_resource;
3103 p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base,
3104 fhp->fh_handle.fh_size);
3106 if (bmval0 & FATTR4_WORD0_FILEID) {
3107 p = xdr_reserve_space(xdr, 8);
3108 if (!p)
3109 goto out_resource;
3110 p = xdr_encode_hyper(p, stat.ino);
3112 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
3113 p = xdr_reserve_space(xdr, 8);
3114 if (!p)
3115 goto out_resource;
3116 p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
3118 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
3119 p = xdr_reserve_space(xdr, 8);
3120 if (!p)
3121 goto out_resource;
3122 p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
3124 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
3125 p = xdr_reserve_space(xdr, 8);
3126 if (!p)
3127 goto out_resource;
3128 p = xdr_encode_hyper(p, (u64) statfs.f_files);
3130 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
3131 status = nfsd4_encode_fs_locations(xdr, rqstp, exp);
3132 if (status)
3133 goto out;
3135 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
3136 p = xdr_reserve_space(xdr, 4);
3137 if (!p)
3138 goto out_resource;
3139 *p++ = cpu_to_be32(1);
3141 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
3142 p = xdr_reserve_space(xdr, 8);
3143 if (!p)
3144 goto out_resource;
3145 p = xdr_encode_hyper(p, exp->ex_path.mnt->mnt_sb->s_maxbytes);
3147 if (bmval0 & FATTR4_WORD0_MAXLINK) {
3148 p = xdr_reserve_space(xdr, 4);
3149 if (!p)
3150 goto out_resource;
3151 *p++ = cpu_to_be32(255);
3153 if (bmval0 & FATTR4_WORD0_MAXNAME) {
3154 p = xdr_reserve_space(xdr, 4);
3155 if (!p)
3156 goto out_resource;
3157 *p++ = cpu_to_be32(statfs.f_namelen);
3159 if (bmval0 & FATTR4_WORD0_MAXREAD) {
3160 p = xdr_reserve_space(xdr, 8);
3161 if (!p)
3162 goto out_resource;
3163 p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
3165 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
3166 p = xdr_reserve_space(xdr, 8);
3167 if (!p)
3168 goto out_resource;
3169 p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
3171 if (bmval1 & FATTR4_WORD1_MODE) {
3172 p = xdr_reserve_space(xdr, 4);
3173 if (!p)
3174 goto out_resource;
3175 *p++ = cpu_to_be32(stat.mode & S_IALLUGO);
3177 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
3178 p = xdr_reserve_space(xdr, 4);
3179 if (!p)
3180 goto out_resource;
3181 *p++ = cpu_to_be32(1);
3183 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
3184 p = xdr_reserve_space(xdr, 4);
3185 if (!p)
3186 goto out_resource;
3187 *p++ = cpu_to_be32(stat.nlink);
3189 if (bmval1 & FATTR4_WORD1_OWNER) {
3190 status = nfsd4_encode_user(xdr, rqstp, stat.uid);
3191 if (status)
3192 goto out;
3194 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
3195 status = nfsd4_encode_group(xdr, rqstp, stat.gid);
3196 if (status)
3197 goto out;
3199 if (bmval1 & FATTR4_WORD1_RAWDEV) {
3200 p = xdr_reserve_space(xdr, 8);
3201 if (!p)
3202 goto out_resource;
3203 *p++ = cpu_to_be32((u32) MAJOR(stat.rdev));
3204 *p++ = cpu_to_be32((u32) MINOR(stat.rdev));
3206 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
3207 p = xdr_reserve_space(xdr, 8);
3208 if (!p)
3209 goto out_resource;
3210 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
3211 p = xdr_encode_hyper(p, dummy64);
3213 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
3214 p = xdr_reserve_space(xdr, 8);
3215 if (!p)
3216 goto out_resource;
3217 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
3218 p = xdr_encode_hyper(p, dummy64);
3220 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
3221 p = xdr_reserve_space(xdr, 8);
3222 if (!p)
3223 goto out_resource;
3224 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
3225 p = xdr_encode_hyper(p, dummy64);
3227 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
3228 p = xdr_reserve_space(xdr, 8);
3229 if (!p)
3230 goto out_resource;
3231 dummy64 = (u64)stat.blocks << 9;
3232 p = xdr_encode_hyper(p, dummy64);
3234 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
3235 p = xdr_reserve_space(xdr, 12);
3236 if (!p)
3237 goto out_resource;
3238 p = xdr_encode_hyper(p, (s64)stat.atime.tv_sec);
3239 *p++ = cpu_to_be32(stat.atime.tv_nsec);
3241 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
3242 p = xdr_reserve_space(xdr, 12);
3243 if (!p)
3244 goto out_resource;
3245 p = encode_time_delta(p, d_inode(dentry));
3247 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
3248 p = xdr_reserve_space(xdr, 12);
3249 if (!p)
3250 goto out_resource;
3251 p = xdr_encode_hyper(p, (s64)stat.ctime.tv_sec);
3252 *p++ = cpu_to_be32(stat.ctime.tv_nsec);
3254 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
3255 p = xdr_reserve_space(xdr, 12);
3256 if (!p)
3257 goto out_resource;
3258 p = xdr_encode_hyper(p, (s64)stat.mtime.tv_sec);
3259 *p++ = cpu_to_be32(stat.mtime.tv_nsec);
3261 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
3262 struct kstat parent_stat;
3263 u64 ino = stat.ino;
3265 p = xdr_reserve_space(xdr, 8);
3266 if (!p)
3267 goto out_resource;
3269 * Get parent's attributes if not ignoring crossmount
3270 * and this is the root of a cross-mounted filesystem.
3272 if (ignore_crossmnt == 0 &&
3273 dentry == exp->ex_path.mnt->mnt_root) {
3274 err = get_parent_attributes(exp, &parent_stat);
3275 if (err)
3276 goto out_nfserr;
3277 ino = parent_stat.ino;
3279 p = xdr_encode_hyper(p, ino);
3281 #ifdef CONFIG_NFSD_PNFS
3282 if (bmval1 & FATTR4_WORD1_FS_LAYOUT_TYPES) {
3283 status = nfsd4_encode_layout_types(xdr, exp->ex_layout_types);
3284 if (status)
3285 goto out;
3288 if (bmval2 & FATTR4_WORD2_LAYOUT_TYPES) {
3289 status = nfsd4_encode_layout_types(xdr, exp->ex_layout_types);
3290 if (status)
3291 goto out;
3294 if (bmval2 & FATTR4_WORD2_LAYOUT_BLKSIZE) {
3295 p = xdr_reserve_space(xdr, 4);
3296 if (!p)
3297 goto out_resource;
3298 *p++ = cpu_to_be32(stat.blksize);
3300 #endif /* CONFIG_NFSD_PNFS */
3301 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
3302 u32 supp[3];
3304 memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
3305 supp[0] &= NFSD_SUPPATTR_EXCLCREAT_WORD0;
3306 supp[1] &= NFSD_SUPPATTR_EXCLCREAT_WORD1;
3307 supp[2] &= NFSD_SUPPATTR_EXCLCREAT_WORD2;
3309 status = nfsd4_encode_bitmap(xdr, supp[0], supp[1], supp[2]);
3310 if (status)
3311 goto out;
3314 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
3315 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
3316 status = nfsd4_encode_security_label(xdr, rqstp, context,
3317 contextlen);
3318 if (status)
3319 goto out;
3321 #endif
3323 if (bmval2 & FATTR4_WORD2_XATTR_SUPPORT) {
3324 p = xdr_reserve_space(xdr, 4);
3325 if (!p)
3326 goto out_resource;
3327 err = xattr_supported_namespace(d_inode(dentry),
3328 XATTR_USER_PREFIX);
3329 *p++ = cpu_to_be32(err == 0);
3332 attrlen = htonl(xdr->buf->len - attrlen_offset - 4);
3333 write_bytes_to_xdr_buf(xdr->buf, attrlen_offset, &attrlen, 4);
3334 status = nfs_ok;
3336 out:
3337 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
3338 if (context)
3339 security_release_secctx(context, contextlen);
3340 #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
3341 kfree(acl);
3342 if (tempfh) {
3343 fh_put(tempfh);
3344 kfree(tempfh);
3346 if (status)
3347 xdr_truncate_encode(xdr, starting_len);
3348 return status;
3349 out_nfserr:
3350 status = nfserrno(err);
3351 goto out;
3352 out_resource:
3353 status = nfserr_resource;
3354 goto out;
3357 static void svcxdr_init_encode_from_buffer(struct xdr_stream *xdr,
3358 struct xdr_buf *buf, __be32 *p, int bytes)
3360 xdr->scratch.iov_len = 0;
3361 memset(buf, 0, sizeof(struct xdr_buf));
3362 buf->head[0].iov_base = p;
3363 buf->head[0].iov_len = 0;
3364 buf->len = 0;
3365 xdr->buf = buf;
3366 xdr->iov = buf->head;
3367 xdr->p = p;
3368 xdr->end = (void *)p + bytes;
3369 buf->buflen = bytes;
3372 __be32 nfsd4_encode_fattr_to_buf(__be32 **p, int words,
3373 struct svc_fh *fhp, struct svc_export *exp,
3374 struct dentry *dentry, u32 *bmval,
3375 struct svc_rqst *rqstp, int ignore_crossmnt)
3377 struct xdr_buf dummy;
3378 struct xdr_stream xdr;
3379 __be32 ret;
3381 svcxdr_init_encode_from_buffer(&xdr, &dummy, *p, words << 2);
3382 ret = nfsd4_encode_fattr(&xdr, fhp, exp, dentry, bmval, rqstp,
3383 ignore_crossmnt);
3384 *p = xdr.p;
3385 return ret;
3388 static inline int attributes_need_mount(u32 *bmval)
3390 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
3391 return 1;
3392 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
3393 return 1;
3394 return 0;
3397 static __be32
3398 nfsd4_encode_dirent_fattr(struct xdr_stream *xdr, struct nfsd4_readdir *cd,
3399 const char *name, int namlen)
3401 struct svc_export *exp = cd->rd_fhp->fh_export;
3402 struct dentry *dentry;
3403 __be32 nfserr;
3404 int ignore_crossmnt = 0;
3406 dentry = lookup_positive_unlocked(name, cd->rd_fhp->fh_dentry, namlen);
3407 if (IS_ERR(dentry))
3408 return nfserrno(PTR_ERR(dentry));
3410 exp_get(exp);
3412 * In the case of a mountpoint, the client may be asking for
3413 * attributes that are only properties of the underlying filesystem
3414 * as opposed to the cross-mounted file system. In such a case,
3415 * we will not follow the cross mount and will fill the attribtutes
3416 * directly from the mountpoint dentry.
3418 if (nfsd_mountpoint(dentry, exp)) {
3419 int err;
3421 if (!(exp->ex_flags & NFSEXP_V4ROOT)
3422 && !attributes_need_mount(cd->rd_bmval)) {
3423 ignore_crossmnt = 1;
3424 goto out_encode;
3427 * Why the heck aren't we just using nfsd_lookup??
3428 * Different "."/".." handling? Something else?
3429 * At least, add a comment here to explain....
3431 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
3432 if (err) {
3433 nfserr = nfserrno(err);
3434 goto out_put;
3436 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
3437 if (nfserr)
3438 goto out_put;
3441 out_encode:
3442 nfserr = nfsd4_encode_fattr(xdr, NULL, exp, dentry, cd->rd_bmval,
3443 cd->rd_rqstp, ignore_crossmnt);
3444 out_put:
3445 dput(dentry);
3446 exp_put(exp);
3447 return nfserr;
3450 static __be32 *
3451 nfsd4_encode_rdattr_error(struct xdr_stream *xdr, __be32 nfserr)
3453 __be32 *p;
3455 p = xdr_reserve_space(xdr, 20);
3456 if (!p)
3457 return NULL;
3458 *p++ = htonl(2);
3459 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
3460 *p++ = htonl(0); /* bmval1 */
3462 *p++ = htonl(4); /* attribute length */
3463 *p++ = nfserr; /* no htonl */
3464 return p;
3467 static int
3468 nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
3469 loff_t offset, u64 ino, unsigned int d_type)
3471 struct readdir_cd *ccd = ccdv;
3472 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
3473 struct xdr_stream *xdr = cd->xdr;
3474 int start_offset = xdr->buf->len;
3475 int cookie_offset;
3476 u32 name_and_cookie;
3477 int entry_bytes;
3478 __be32 nfserr = nfserr_toosmall;
3479 __be64 wire_offset;
3480 __be32 *p;
3482 /* In nfsv4, "." and ".." never make it onto the wire.. */
3483 if (name && isdotent(name, namlen)) {
3484 cd->common.err = nfs_ok;
3485 return 0;
3488 if (cd->cookie_offset) {
3489 wire_offset = cpu_to_be64(offset);
3490 write_bytes_to_xdr_buf(xdr->buf, cd->cookie_offset,
3491 &wire_offset, 8);
3494 p = xdr_reserve_space(xdr, 4);
3495 if (!p)
3496 goto fail;
3497 *p++ = xdr_one; /* mark entry present */
3498 cookie_offset = xdr->buf->len;
3499 p = xdr_reserve_space(xdr, 3*4 + namlen);
3500 if (!p)
3501 goto fail;
3502 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
3503 p = xdr_encode_array(p, name, namlen); /* name length & name */
3505 nfserr = nfsd4_encode_dirent_fattr(xdr, cd, name, namlen);
3506 switch (nfserr) {
3507 case nfs_ok:
3508 break;
3509 case nfserr_resource:
3510 nfserr = nfserr_toosmall;
3511 goto fail;
3512 case nfserr_noent:
3513 xdr_truncate_encode(xdr, start_offset);
3514 goto skip_entry;
3515 default:
3517 * If the client requested the RDATTR_ERROR attribute,
3518 * we stuff the error code into this attribute
3519 * and continue. If this attribute was not requested,
3520 * then in accordance with the spec, we fail the
3521 * entire READDIR operation(!)
3523 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
3524 goto fail;
3525 p = nfsd4_encode_rdattr_error(xdr, nfserr);
3526 if (p == NULL) {
3527 nfserr = nfserr_toosmall;
3528 goto fail;
3531 nfserr = nfserr_toosmall;
3532 entry_bytes = xdr->buf->len - start_offset;
3533 if (entry_bytes > cd->rd_maxcount)
3534 goto fail;
3535 cd->rd_maxcount -= entry_bytes;
3537 * RFC 3530 14.2.24 describes rd_dircount as only a "hint", so
3538 * let's always let through the first entry, at least:
3540 if (!cd->rd_dircount)
3541 goto fail;
3542 name_and_cookie = 4 + 4 * XDR_QUADLEN(namlen) + 8;
3543 if (name_and_cookie > cd->rd_dircount && cd->cookie_offset)
3544 goto fail;
3545 cd->rd_dircount -= min(cd->rd_dircount, name_and_cookie);
3547 cd->cookie_offset = cookie_offset;
3548 skip_entry:
3549 cd->common.err = nfs_ok;
3550 return 0;
3551 fail:
3552 xdr_truncate_encode(xdr, start_offset);
3553 cd->common.err = nfserr;
3554 return -EINVAL;
3557 static __be32
3558 nfsd4_encode_stateid(struct xdr_stream *xdr, stateid_t *sid)
3560 __be32 *p;
3562 p = xdr_reserve_space(xdr, sizeof(stateid_t));
3563 if (!p)
3564 return nfserr_resource;
3565 *p++ = cpu_to_be32(sid->si_generation);
3566 p = xdr_encode_opaque_fixed(p, &sid->si_opaque,
3567 sizeof(stateid_opaque_t));
3568 return 0;
3571 static __be32
3572 nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
3574 struct xdr_stream *xdr = &resp->xdr;
3575 __be32 *p;
3577 p = xdr_reserve_space(xdr, 8);
3578 if (!p)
3579 return nfserr_resource;
3580 *p++ = cpu_to_be32(access->ac_supported);
3581 *p++ = cpu_to_be32(access->ac_resp_access);
3582 return 0;
3585 static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
3587 struct xdr_stream *xdr = &resp->xdr;
3588 __be32 *p;
3590 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 8);
3591 if (!p)
3592 return nfserr_resource;
3593 p = xdr_encode_opaque_fixed(p, bcts->sessionid.data,
3594 NFS4_MAX_SESSIONID_LEN);
3595 *p++ = cpu_to_be32(bcts->dir);
3596 /* Upshifting from TCP to RDMA is not supported */
3597 *p++ = cpu_to_be32(0);
3598 return 0;
3601 static __be32
3602 nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
3604 struct xdr_stream *xdr = &resp->xdr;
3606 return nfsd4_encode_stateid(xdr, &close->cl_stateid);
3610 static __be32
3611 nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
3613 struct xdr_stream *xdr = &resp->xdr;
3614 __be32 *p;
3616 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
3617 if (!p)
3618 return nfserr_resource;
3619 p = xdr_encode_opaque_fixed(p, commit->co_verf.data,
3620 NFS4_VERIFIER_SIZE);
3621 return 0;
3624 static __be32
3625 nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
3627 struct xdr_stream *xdr = &resp->xdr;
3628 __be32 *p;
3630 p = xdr_reserve_space(xdr, 20);
3631 if (!p)
3632 return nfserr_resource;
3633 encode_cinfo(p, &create->cr_cinfo);
3634 return nfsd4_encode_bitmap(xdr, create->cr_bmval[0],
3635 create->cr_bmval[1], create->cr_bmval[2]);
3638 static __be32
3639 nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
3641 struct svc_fh *fhp = getattr->ga_fhp;
3642 struct xdr_stream *xdr = &resp->xdr;
3644 return nfsd4_encode_fattr(xdr, fhp, fhp->fh_export, fhp->fh_dentry,
3645 getattr->ga_bmval, resp->rqstp, 0);
3648 static __be32
3649 nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
3651 struct xdr_stream *xdr = &resp->xdr;
3652 struct svc_fh *fhp = *fhpp;
3653 unsigned int len;
3654 __be32 *p;
3656 len = fhp->fh_handle.fh_size;
3657 p = xdr_reserve_space(xdr, len + 4);
3658 if (!p)
3659 return nfserr_resource;
3660 p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base, len);
3661 return 0;
3665 * Including all fields other than the name, a LOCK4denied structure requires
3666 * 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
3668 static __be32
3669 nfsd4_encode_lock_denied(struct xdr_stream *xdr, struct nfsd4_lock_denied *ld)
3671 struct xdr_netobj *conf = &ld->ld_owner;
3672 __be32 *p;
3674 again:
3675 p = xdr_reserve_space(xdr, 32 + XDR_LEN(conf->len));
3676 if (!p) {
3678 * Don't fail to return the result just because we can't
3679 * return the conflicting open:
3681 if (conf->len) {
3682 kfree(conf->data);
3683 conf->len = 0;
3684 conf->data = NULL;
3685 goto again;
3687 return nfserr_resource;
3689 p = xdr_encode_hyper(p, ld->ld_start);
3690 p = xdr_encode_hyper(p, ld->ld_length);
3691 *p++ = cpu_to_be32(ld->ld_type);
3692 if (conf->len) {
3693 p = xdr_encode_opaque_fixed(p, &ld->ld_clientid, 8);
3694 p = xdr_encode_opaque(p, conf->data, conf->len);
3695 kfree(conf->data);
3696 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
3697 p = xdr_encode_hyper(p, (u64)0); /* clientid */
3698 *p++ = cpu_to_be32(0); /* length of owner name */
3700 return nfserr_denied;
3703 static __be32
3704 nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
3706 struct xdr_stream *xdr = &resp->xdr;
3708 if (!nfserr)
3709 nfserr = nfsd4_encode_stateid(xdr, &lock->lk_resp_stateid);
3710 else if (nfserr == nfserr_denied)
3711 nfserr = nfsd4_encode_lock_denied(xdr, &lock->lk_denied);
3713 return nfserr;
3716 static __be32
3717 nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
3719 struct xdr_stream *xdr = &resp->xdr;
3721 if (nfserr == nfserr_denied)
3722 nfsd4_encode_lock_denied(xdr, &lockt->lt_denied);
3723 return nfserr;
3726 static __be32
3727 nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
3729 struct xdr_stream *xdr = &resp->xdr;
3731 return nfsd4_encode_stateid(xdr, &locku->lu_stateid);
3735 static __be32
3736 nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
3738 struct xdr_stream *xdr = &resp->xdr;
3739 __be32 *p;
3741 p = xdr_reserve_space(xdr, 20);
3742 if (!p)
3743 return nfserr_resource;
3744 p = encode_cinfo(p, &link->li_cinfo);
3745 return 0;
3749 static __be32
3750 nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
3752 struct xdr_stream *xdr = &resp->xdr;
3753 __be32 *p;
3755 nfserr = nfsd4_encode_stateid(xdr, &open->op_stateid);
3756 if (nfserr)
3757 return nfserr;
3758 p = xdr_reserve_space(xdr, 24);
3759 if (!p)
3760 return nfserr_resource;
3761 p = encode_cinfo(p, &open->op_cinfo);
3762 *p++ = cpu_to_be32(open->op_rflags);
3764 nfserr = nfsd4_encode_bitmap(xdr, open->op_bmval[0], open->op_bmval[1],
3765 open->op_bmval[2]);
3766 if (nfserr)
3767 return nfserr;
3769 p = xdr_reserve_space(xdr, 4);
3770 if (!p)
3771 return nfserr_resource;
3773 *p++ = cpu_to_be32(open->op_delegate_type);
3774 switch (open->op_delegate_type) {
3775 case NFS4_OPEN_DELEGATE_NONE:
3776 break;
3777 case NFS4_OPEN_DELEGATE_READ:
3778 nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
3779 if (nfserr)
3780 return nfserr;
3781 p = xdr_reserve_space(xdr, 20);
3782 if (!p)
3783 return nfserr_resource;
3784 *p++ = cpu_to_be32(open->op_recall);
3787 * TODO: ACE's in delegations
3789 *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
3790 *p++ = cpu_to_be32(0);
3791 *p++ = cpu_to_be32(0);
3792 *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
3793 break;
3794 case NFS4_OPEN_DELEGATE_WRITE:
3795 nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
3796 if (nfserr)
3797 return nfserr;
3798 p = xdr_reserve_space(xdr, 32);
3799 if (!p)
3800 return nfserr_resource;
3801 *p++ = cpu_to_be32(0);
3804 * TODO: space_limit's in delegations
3806 *p++ = cpu_to_be32(NFS4_LIMIT_SIZE);
3807 *p++ = cpu_to_be32(~(u32)0);
3808 *p++ = cpu_to_be32(~(u32)0);
3811 * TODO: ACE's in delegations
3813 *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
3814 *p++ = cpu_to_be32(0);
3815 *p++ = cpu_to_be32(0);
3816 *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
3817 break;
3818 case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
3819 switch (open->op_why_no_deleg) {
3820 case WND4_CONTENTION:
3821 case WND4_RESOURCE:
3822 p = xdr_reserve_space(xdr, 8);
3823 if (!p)
3824 return nfserr_resource;
3825 *p++ = cpu_to_be32(open->op_why_no_deleg);
3826 /* deleg signaling not supported yet: */
3827 *p++ = cpu_to_be32(0);
3828 break;
3829 default:
3830 p = xdr_reserve_space(xdr, 4);
3831 if (!p)
3832 return nfserr_resource;
3833 *p++ = cpu_to_be32(open->op_why_no_deleg);
3835 break;
3836 default:
3837 BUG();
3839 /* XXX save filehandle here */
3840 return 0;
3843 static __be32
3844 nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
3846 struct xdr_stream *xdr = &resp->xdr;
3848 return nfsd4_encode_stateid(xdr, &oc->oc_resp_stateid);
3851 static __be32
3852 nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
3854 struct xdr_stream *xdr = &resp->xdr;
3856 return nfsd4_encode_stateid(xdr, &od->od_stateid);
3859 static __be32 nfsd4_encode_splice_read(
3860 struct nfsd4_compoundres *resp,
3861 struct nfsd4_read *read,
3862 struct file *file, unsigned long maxcount)
3864 struct xdr_stream *xdr = &resp->xdr;
3865 struct xdr_buf *buf = xdr->buf;
3866 int status, space_left;
3867 u32 eof;
3868 __be32 nfserr;
3869 __be32 *p = xdr->p - 2;
3871 /* Make sure there will be room for padding if needed */
3872 if (xdr->end - xdr->p < 1)
3873 return nfserr_resource;
3875 nfserr = nfsd_splice_read(read->rd_rqstp, read->rd_fhp,
3876 file, read->rd_offset, &maxcount, &eof);
3877 read->rd_length = maxcount;
3878 if (nfserr)
3879 goto out_err;
3880 status = svc_encode_result_payload(read->rd_rqstp,
3881 buf->head[0].iov_len, maxcount);
3882 if (status) {
3883 nfserr = nfserrno(status);
3884 goto out_err;
3887 *(p++) = htonl(eof);
3888 *(p++) = htonl(maxcount);
3890 buf->page_len = maxcount;
3891 buf->len += maxcount;
3892 xdr->page_ptr += (buf->page_base + maxcount + PAGE_SIZE - 1)
3893 / PAGE_SIZE;
3895 /* Use rest of head for padding and remaining ops: */
3896 buf->tail[0].iov_base = xdr->p;
3897 buf->tail[0].iov_len = 0;
3898 xdr->iov = buf->tail;
3899 if (maxcount&3) {
3900 int pad = 4 - (maxcount&3);
3902 *(xdr->p++) = 0;
3904 buf->tail[0].iov_base += maxcount&3;
3905 buf->tail[0].iov_len = pad;
3906 buf->len += pad;
3909 space_left = min_t(int, (void *)xdr->end - (void *)xdr->p,
3910 buf->buflen - buf->len);
3911 buf->buflen = buf->len + space_left;
3912 xdr->end = (__be32 *)((void *)xdr->end + space_left);
3914 return 0;
3916 out_err:
3918 * nfsd_splice_actor may have already messed with the
3919 * page length; reset it so as not to confuse
3920 * xdr_truncate_encode in our caller.
3922 buf->page_len = 0;
3923 return nfserr;
3926 static __be32 nfsd4_encode_readv(struct nfsd4_compoundres *resp,
3927 struct nfsd4_read *read,
3928 struct file *file, unsigned long maxcount)
3930 struct xdr_stream *xdr = &resp->xdr;
3931 u32 eof;
3932 int starting_len = xdr->buf->len - 8;
3933 __be32 nfserr;
3934 __be32 tmp;
3935 int pad;
3937 read->rd_vlen = xdr_reserve_space_vec(xdr, resp->rqstp->rq_vec, maxcount);
3938 if (read->rd_vlen < 0)
3939 return nfserr_resource;
3941 nfserr = nfsd_readv(resp->rqstp, read->rd_fhp, file, read->rd_offset,
3942 resp->rqstp->rq_vec, read->rd_vlen, &maxcount,
3943 &eof);
3944 read->rd_length = maxcount;
3945 if (nfserr)
3946 return nfserr;
3947 if (svc_encode_result_payload(resp->rqstp, starting_len + 8, maxcount))
3948 return nfserr_io;
3949 xdr_truncate_encode(xdr, starting_len + 8 + xdr_align_size(maxcount));
3951 tmp = htonl(eof);
3952 write_bytes_to_xdr_buf(xdr->buf, starting_len , &tmp, 4);
3953 tmp = htonl(maxcount);
3954 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp, 4);
3956 tmp = xdr_zero;
3957 pad = (maxcount&3) ? 4 - (maxcount&3) : 0;
3958 write_bytes_to_xdr_buf(xdr->buf, starting_len + 8 + maxcount,
3959 &tmp, pad);
3960 return 0;
3964 static __be32
3965 nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
3966 struct nfsd4_read *read)
3968 unsigned long maxcount;
3969 struct xdr_stream *xdr = &resp->xdr;
3970 struct file *file;
3971 int starting_len = xdr->buf->len;
3972 __be32 *p;
3974 if (nfserr)
3975 return nfserr;
3976 file = read->rd_nf->nf_file;
3978 p = xdr_reserve_space(xdr, 8); /* eof flag and byte count */
3979 if (!p) {
3980 WARN_ON_ONCE(test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags));
3981 return nfserr_resource;
3983 if (resp->xdr.buf->page_len &&
3984 test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags)) {
3985 WARN_ON_ONCE(1);
3986 return nfserr_resource;
3988 xdr_commit_encode(xdr);
3990 maxcount = svc_max_payload(resp->rqstp);
3991 maxcount = min_t(unsigned long, maxcount,
3992 (xdr->buf->buflen - xdr->buf->len));
3993 maxcount = min_t(unsigned long, maxcount, read->rd_length);
3995 if (file->f_op->splice_read &&
3996 test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags))
3997 nfserr = nfsd4_encode_splice_read(resp, read, file, maxcount);
3998 else
3999 nfserr = nfsd4_encode_readv(resp, read, file, maxcount);
4001 if (nfserr)
4002 xdr_truncate_encode(xdr, starting_len);
4004 return nfserr;
4007 static __be32
4008 nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
4010 int maxcount;
4011 __be32 wire_count;
4012 int zero = 0;
4013 struct xdr_stream *xdr = &resp->xdr;
4014 int length_offset = xdr->buf->len;
4015 int status;
4016 __be32 *p;
4018 p = xdr_reserve_space(xdr, 4);
4019 if (!p)
4020 return nfserr_resource;
4021 maxcount = PAGE_SIZE;
4023 p = xdr_reserve_space(xdr, maxcount);
4024 if (!p)
4025 return nfserr_resource;
4027 * XXX: By default, vfs_readlink() will truncate symlinks if they
4028 * would overflow the buffer. Is this kosher in NFSv4? If not, one
4029 * easy fix is: if vfs_readlink() precisely fills the buffer, assume
4030 * that truncation occurred, and return NFS4ERR_RESOURCE.
4032 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp,
4033 (char *)p, &maxcount);
4034 if (nfserr == nfserr_isdir)
4035 nfserr = nfserr_inval;
4036 if (nfserr)
4037 goto out_err;
4038 status = svc_encode_result_payload(readlink->rl_rqstp, length_offset,
4039 maxcount);
4040 if (status) {
4041 nfserr = nfserrno(status);
4042 goto out_err;
4045 wire_count = htonl(maxcount);
4046 write_bytes_to_xdr_buf(xdr->buf, length_offset, &wire_count, 4);
4047 xdr_truncate_encode(xdr, length_offset + 4 + ALIGN(maxcount, 4));
4048 if (maxcount & 3)
4049 write_bytes_to_xdr_buf(xdr->buf, length_offset + 4 + maxcount,
4050 &zero, 4 - (maxcount&3));
4051 return 0;
4053 out_err:
4054 xdr_truncate_encode(xdr, length_offset);
4055 return nfserr;
4058 static __be32
4059 nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
4061 int maxcount;
4062 int bytes_left;
4063 loff_t offset;
4064 __be64 wire_offset;
4065 struct xdr_stream *xdr = &resp->xdr;
4066 int starting_len = xdr->buf->len;
4067 __be32 *p;
4069 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
4070 if (!p)
4071 return nfserr_resource;
4073 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
4074 *p++ = cpu_to_be32(0);
4075 *p++ = cpu_to_be32(0);
4076 resp->xdr.buf->head[0].iov_len = ((char *)resp->xdr.p)
4077 - (char *)resp->xdr.buf->head[0].iov_base;
4080 * Number of bytes left for directory entries allowing for the
4081 * final 8 bytes of the readdir and a following failed op:
4083 bytes_left = xdr->buf->buflen - xdr->buf->len
4084 - COMPOUND_ERR_SLACK_SPACE - 8;
4085 if (bytes_left < 0) {
4086 nfserr = nfserr_resource;
4087 goto err_no_verf;
4089 maxcount = svc_max_payload(resp->rqstp);
4090 maxcount = min_t(u32, readdir->rd_maxcount, maxcount);
4092 * Note the rfc defines rd_maxcount as the size of the
4093 * READDIR4resok structure, which includes the verifier above
4094 * and the 8 bytes encoded at the end of this function:
4096 if (maxcount < 16) {
4097 nfserr = nfserr_toosmall;
4098 goto err_no_verf;
4100 maxcount = min_t(int, maxcount-16, bytes_left);
4102 /* RFC 3530 14.2.24 allows us to ignore dircount when it's 0: */
4103 if (!readdir->rd_dircount)
4104 readdir->rd_dircount = svc_max_payload(resp->rqstp);
4106 readdir->xdr = xdr;
4107 readdir->rd_maxcount = maxcount;
4108 readdir->common.err = 0;
4109 readdir->cookie_offset = 0;
4111 offset = readdir->rd_cookie;
4112 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
4113 &offset,
4114 &readdir->common, nfsd4_encode_dirent);
4115 if (nfserr == nfs_ok &&
4116 readdir->common.err == nfserr_toosmall &&
4117 xdr->buf->len == starting_len + 8) {
4118 /* nothing encoded; which limit did we hit?: */
4119 if (maxcount - 16 < bytes_left)
4120 /* It was the fault of rd_maxcount: */
4121 nfserr = nfserr_toosmall;
4122 else
4123 /* We ran out of buffer space: */
4124 nfserr = nfserr_resource;
4126 if (nfserr)
4127 goto err_no_verf;
4129 if (readdir->cookie_offset) {
4130 wire_offset = cpu_to_be64(offset);
4131 write_bytes_to_xdr_buf(xdr->buf, readdir->cookie_offset,
4132 &wire_offset, 8);
4135 p = xdr_reserve_space(xdr, 8);
4136 if (!p) {
4137 WARN_ON_ONCE(1);
4138 goto err_no_verf;
4140 *p++ = 0; /* no more entries */
4141 *p++ = htonl(readdir->common.err == nfserr_eof);
4143 return 0;
4144 err_no_verf:
4145 xdr_truncate_encode(xdr, starting_len);
4146 return nfserr;
4149 static __be32
4150 nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
4152 struct xdr_stream *xdr = &resp->xdr;
4153 __be32 *p;
4155 p = xdr_reserve_space(xdr, 20);
4156 if (!p)
4157 return nfserr_resource;
4158 p = encode_cinfo(p, &remove->rm_cinfo);
4159 return 0;
4162 static __be32
4163 nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
4165 struct xdr_stream *xdr = &resp->xdr;
4166 __be32 *p;
4168 p = xdr_reserve_space(xdr, 40);
4169 if (!p)
4170 return nfserr_resource;
4171 p = encode_cinfo(p, &rename->rn_sinfo);
4172 p = encode_cinfo(p, &rename->rn_tinfo);
4173 return 0;
4176 static __be32
4177 nfsd4_do_encode_secinfo(struct xdr_stream *xdr, struct svc_export *exp)
4179 u32 i, nflavs, supported;
4180 struct exp_flavor_info *flavs;
4181 struct exp_flavor_info def_flavs[2];
4182 __be32 *p, *flavorsp;
4183 static bool report = true;
4185 if (exp->ex_nflavors) {
4186 flavs = exp->ex_flavors;
4187 nflavs = exp->ex_nflavors;
4188 } else { /* Handling of some defaults in absence of real secinfo: */
4189 flavs = def_flavs;
4190 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
4191 nflavs = 2;
4192 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
4193 flavs[1].pseudoflavor = RPC_AUTH_NULL;
4194 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
4195 nflavs = 1;
4196 flavs[0].pseudoflavor
4197 = svcauth_gss_flavor(exp->ex_client);
4198 } else {
4199 nflavs = 1;
4200 flavs[0].pseudoflavor
4201 = exp->ex_client->flavour->flavour;
4205 supported = 0;
4206 p = xdr_reserve_space(xdr, 4);
4207 if (!p)
4208 return nfserr_resource;
4209 flavorsp = p++; /* to be backfilled later */
4211 for (i = 0; i < nflavs; i++) {
4212 rpc_authflavor_t pf = flavs[i].pseudoflavor;
4213 struct rpcsec_gss_info info;
4215 if (rpcauth_get_gssinfo(pf, &info) == 0) {
4216 supported++;
4217 p = xdr_reserve_space(xdr, 4 + 4 +
4218 XDR_LEN(info.oid.len) + 4 + 4);
4219 if (!p)
4220 return nfserr_resource;
4221 *p++ = cpu_to_be32(RPC_AUTH_GSS);
4222 p = xdr_encode_opaque(p, info.oid.data, info.oid.len);
4223 *p++ = cpu_to_be32(info.qop);
4224 *p++ = cpu_to_be32(info.service);
4225 } else if (pf < RPC_AUTH_MAXFLAVOR) {
4226 supported++;
4227 p = xdr_reserve_space(xdr, 4);
4228 if (!p)
4229 return nfserr_resource;
4230 *p++ = cpu_to_be32(pf);
4231 } else {
4232 if (report)
4233 pr_warn("NFS: SECINFO: security flavor %u "
4234 "is not supported\n", pf);
4238 if (nflavs != supported)
4239 report = false;
4240 *flavorsp = htonl(supported);
4241 return 0;
4244 static __be32
4245 nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
4246 struct nfsd4_secinfo *secinfo)
4248 struct xdr_stream *xdr = &resp->xdr;
4250 return nfsd4_do_encode_secinfo(xdr, secinfo->si_exp);
4253 static __be32
4254 nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
4255 struct nfsd4_secinfo_no_name *secinfo)
4257 struct xdr_stream *xdr = &resp->xdr;
4259 return nfsd4_do_encode_secinfo(xdr, secinfo->sin_exp);
4263 * The SETATTR encode routine is special -- it always encodes a bitmap,
4264 * regardless of the error status.
4266 static __be32
4267 nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
4269 struct xdr_stream *xdr = &resp->xdr;
4270 __be32 *p;
4272 p = xdr_reserve_space(xdr, 16);
4273 if (!p)
4274 return nfserr_resource;
4275 if (nfserr) {
4276 *p++ = cpu_to_be32(3);
4277 *p++ = cpu_to_be32(0);
4278 *p++ = cpu_to_be32(0);
4279 *p++ = cpu_to_be32(0);
4281 else {
4282 *p++ = cpu_to_be32(3);
4283 *p++ = cpu_to_be32(setattr->sa_bmval[0]);
4284 *p++ = cpu_to_be32(setattr->sa_bmval[1]);
4285 *p++ = cpu_to_be32(setattr->sa_bmval[2]);
4287 return nfserr;
4290 static __be32
4291 nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
4293 struct xdr_stream *xdr = &resp->xdr;
4294 __be32 *p;
4296 if (!nfserr) {
4297 p = xdr_reserve_space(xdr, 8 + NFS4_VERIFIER_SIZE);
4298 if (!p)
4299 return nfserr_resource;
4300 p = xdr_encode_opaque_fixed(p, &scd->se_clientid, 8);
4301 p = xdr_encode_opaque_fixed(p, &scd->se_confirm,
4302 NFS4_VERIFIER_SIZE);
4304 else if (nfserr == nfserr_clid_inuse) {
4305 p = xdr_reserve_space(xdr, 8);
4306 if (!p)
4307 return nfserr_resource;
4308 *p++ = cpu_to_be32(0);
4309 *p++ = cpu_to_be32(0);
4311 return nfserr;
4314 static __be32
4315 nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
4317 struct xdr_stream *xdr = &resp->xdr;
4318 __be32 *p;
4320 p = xdr_reserve_space(xdr, 16);
4321 if (!p)
4322 return nfserr_resource;
4323 *p++ = cpu_to_be32(write->wr_bytes_written);
4324 *p++ = cpu_to_be32(write->wr_how_written);
4325 p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
4326 NFS4_VERIFIER_SIZE);
4327 return 0;
4330 static __be32
4331 nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
4332 struct nfsd4_exchange_id *exid)
4334 struct xdr_stream *xdr = &resp->xdr;
4335 __be32 *p;
4336 char *major_id;
4337 char *server_scope;
4338 int major_id_sz;
4339 int server_scope_sz;
4340 uint64_t minor_id = 0;
4341 struct nfsd_net *nn = net_generic(SVC_NET(resp->rqstp), nfsd_net_id);
4343 major_id = nn->nfsd_name;
4344 major_id_sz = strlen(nn->nfsd_name);
4345 server_scope = nn->nfsd_name;
4346 server_scope_sz = strlen(nn->nfsd_name);
4348 p = xdr_reserve_space(xdr,
4349 8 /* eir_clientid */ +
4350 4 /* eir_sequenceid */ +
4351 4 /* eir_flags */ +
4352 4 /* spr_how */);
4353 if (!p)
4354 return nfserr_resource;
4356 p = xdr_encode_opaque_fixed(p, &exid->clientid, 8);
4357 *p++ = cpu_to_be32(exid->seqid);
4358 *p++ = cpu_to_be32(exid->flags);
4360 *p++ = cpu_to_be32(exid->spa_how);
4362 switch (exid->spa_how) {
4363 case SP4_NONE:
4364 break;
4365 case SP4_MACH_CRED:
4366 /* spo_must_enforce bitmap: */
4367 nfserr = nfsd4_encode_bitmap(xdr,
4368 exid->spo_must_enforce[0],
4369 exid->spo_must_enforce[1],
4370 exid->spo_must_enforce[2]);
4371 if (nfserr)
4372 return nfserr;
4373 /* spo_must_allow bitmap: */
4374 nfserr = nfsd4_encode_bitmap(xdr,
4375 exid->spo_must_allow[0],
4376 exid->spo_must_allow[1],
4377 exid->spo_must_allow[2]);
4378 if (nfserr)
4379 return nfserr;
4380 break;
4381 default:
4382 WARN_ON_ONCE(1);
4385 p = xdr_reserve_space(xdr,
4386 8 /* so_minor_id */ +
4387 4 /* so_major_id.len */ +
4388 (XDR_QUADLEN(major_id_sz) * 4) +
4389 4 /* eir_server_scope.len */ +
4390 (XDR_QUADLEN(server_scope_sz) * 4) +
4391 4 /* eir_server_impl_id.count (0) */);
4392 if (!p)
4393 return nfserr_resource;
4395 /* The server_owner struct */
4396 p = xdr_encode_hyper(p, minor_id); /* Minor id */
4397 /* major id */
4398 p = xdr_encode_opaque(p, major_id, major_id_sz);
4400 /* Server scope */
4401 p = xdr_encode_opaque(p, server_scope, server_scope_sz);
4403 /* Implementation id */
4404 *p++ = cpu_to_be32(0); /* zero length nfs_impl_id4 array */
4405 return 0;
4408 static __be32
4409 nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
4410 struct nfsd4_create_session *sess)
4412 struct xdr_stream *xdr = &resp->xdr;
4413 __be32 *p;
4415 p = xdr_reserve_space(xdr, 24);
4416 if (!p)
4417 return nfserr_resource;
4418 p = xdr_encode_opaque_fixed(p, sess->sessionid.data,
4419 NFS4_MAX_SESSIONID_LEN);
4420 *p++ = cpu_to_be32(sess->seqid);
4421 *p++ = cpu_to_be32(sess->flags);
4423 p = xdr_reserve_space(xdr, 28);
4424 if (!p)
4425 return nfserr_resource;
4426 *p++ = cpu_to_be32(0); /* headerpadsz */
4427 *p++ = cpu_to_be32(sess->fore_channel.maxreq_sz);
4428 *p++ = cpu_to_be32(sess->fore_channel.maxresp_sz);
4429 *p++ = cpu_to_be32(sess->fore_channel.maxresp_cached);
4430 *p++ = cpu_to_be32(sess->fore_channel.maxops);
4431 *p++ = cpu_to_be32(sess->fore_channel.maxreqs);
4432 *p++ = cpu_to_be32(sess->fore_channel.nr_rdma_attrs);
4434 if (sess->fore_channel.nr_rdma_attrs) {
4435 p = xdr_reserve_space(xdr, 4);
4436 if (!p)
4437 return nfserr_resource;
4438 *p++ = cpu_to_be32(sess->fore_channel.rdma_attrs);
4441 p = xdr_reserve_space(xdr, 28);
4442 if (!p)
4443 return nfserr_resource;
4444 *p++ = cpu_to_be32(0); /* headerpadsz */
4445 *p++ = cpu_to_be32(sess->back_channel.maxreq_sz);
4446 *p++ = cpu_to_be32(sess->back_channel.maxresp_sz);
4447 *p++ = cpu_to_be32(sess->back_channel.maxresp_cached);
4448 *p++ = cpu_to_be32(sess->back_channel.maxops);
4449 *p++ = cpu_to_be32(sess->back_channel.maxreqs);
4450 *p++ = cpu_to_be32(sess->back_channel.nr_rdma_attrs);
4452 if (sess->back_channel.nr_rdma_attrs) {
4453 p = xdr_reserve_space(xdr, 4);
4454 if (!p)
4455 return nfserr_resource;
4456 *p++ = cpu_to_be32(sess->back_channel.rdma_attrs);
4458 return 0;
4461 static __be32
4462 nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
4463 struct nfsd4_sequence *seq)
4465 struct xdr_stream *xdr = &resp->xdr;
4466 __be32 *p;
4468 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 20);
4469 if (!p)
4470 return nfserr_resource;
4471 p = xdr_encode_opaque_fixed(p, seq->sessionid.data,
4472 NFS4_MAX_SESSIONID_LEN);
4473 *p++ = cpu_to_be32(seq->seqid);
4474 *p++ = cpu_to_be32(seq->slotid);
4475 /* Note slotid's are numbered from zero: */
4476 *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_highest_slotid */
4477 *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_target_highest_slotid */
4478 *p++ = cpu_to_be32(seq->status_flags);
4480 resp->cstate.data_offset = xdr->buf->len; /* DRC cache data pointer */
4481 return 0;
4484 static __be32
4485 nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
4486 struct nfsd4_test_stateid *test_stateid)
4488 struct xdr_stream *xdr = &resp->xdr;
4489 struct nfsd4_test_stateid_id *stateid, *next;
4490 __be32 *p;
4492 p = xdr_reserve_space(xdr, 4 + (4 * test_stateid->ts_num_ids));
4493 if (!p)
4494 return nfserr_resource;
4495 *p++ = htonl(test_stateid->ts_num_ids);
4497 list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
4498 *p++ = stateid->ts_id_status;
4501 return 0;
4504 #ifdef CONFIG_NFSD_PNFS
4505 static __be32
4506 nfsd4_encode_getdeviceinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
4507 struct nfsd4_getdeviceinfo *gdev)
4509 struct xdr_stream *xdr = &resp->xdr;
4510 const struct nfsd4_layout_ops *ops;
4511 u32 starting_len = xdr->buf->len, needed_len;
4512 __be32 *p;
4514 p = xdr_reserve_space(xdr, 4);
4515 if (!p)
4516 return nfserr_resource;
4518 *p++ = cpu_to_be32(gdev->gd_layout_type);
4520 /* If maxcount is 0 then just update notifications */
4521 if (gdev->gd_maxcount != 0) {
4522 ops = nfsd4_layout_ops[gdev->gd_layout_type];
4523 nfserr = ops->encode_getdeviceinfo(xdr, gdev);
4524 if (nfserr) {
4526 * We don't bother to burden the layout drivers with
4527 * enforcing gd_maxcount, just tell the client to
4528 * come back with a bigger buffer if it's not enough.
4530 if (xdr->buf->len + 4 > gdev->gd_maxcount)
4531 goto toosmall;
4532 return nfserr;
4536 if (gdev->gd_notify_types) {
4537 p = xdr_reserve_space(xdr, 4 + 4);
4538 if (!p)
4539 return nfserr_resource;
4540 *p++ = cpu_to_be32(1); /* bitmap length */
4541 *p++ = cpu_to_be32(gdev->gd_notify_types);
4542 } else {
4543 p = xdr_reserve_space(xdr, 4);
4544 if (!p)
4545 return nfserr_resource;
4546 *p++ = 0;
4549 return 0;
4550 toosmall:
4551 dprintk("%s: maxcount too small\n", __func__);
4552 needed_len = xdr->buf->len + 4 /* notifications */;
4553 xdr_truncate_encode(xdr, starting_len);
4554 p = xdr_reserve_space(xdr, 4);
4555 if (!p)
4556 return nfserr_resource;
4557 *p++ = cpu_to_be32(needed_len);
4558 return nfserr_toosmall;
4561 static __be32
4562 nfsd4_encode_layoutget(struct nfsd4_compoundres *resp, __be32 nfserr,
4563 struct nfsd4_layoutget *lgp)
4565 struct xdr_stream *xdr = &resp->xdr;
4566 const struct nfsd4_layout_ops *ops;
4567 __be32 *p;
4569 p = xdr_reserve_space(xdr, 36 + sizeof(stateid_opaque_t));
4570 if (!p)
4571 return nfserr_resource;
4573 *p++ = cpu_to_be32(1); /* we always set return-on-close */
4574 *p++ = cpu_to_be32(lgp->lg_sid.si_generation);
4575 p = xdr_encode_opaque_fixed(p, &lgp->lg_sid.si_opaque,
4576 sizeof(stateid_opaque_t));
4578 *p++ = cpu_to_be32(1); /* we always return a single layout */
4579 p = xdr_encode_hyper(p, lgp->lg_seg.offset);
4580 p = xdr_encode_hyper(p, lgp->lg_seg.length);
4581 *p++ = cpu_to_be32(lgp->lg_seg.iomode);
4582 *p++ = cpu_to_be32(lgp->lg_layout_type);
4584 ops = nfsd4_layout_ops[lgp->lg_layout_type];
4585 return ops->encode_layoutget(xdr, lgp);
4588 static __be32
4589 nfsd4_encode_layoutcommit(struct nfsd4_compoundres *resp, __be32 nfserr,
4590 struct nfsd4_layoutcommit *lcp)
4592 struct xdr_stream *xdr = &resp->xdr;
4593 __be32 *p;
4595 p = xdr_reserve_space(xdr, 4);
4596 if (!p)
4597 return nfserr_resource;
4598 *p++ = cpu_to_be32(lcp->lc_size_chg);
4599 if (lcp->lc_size_chg) {
4600 p = xdr_reserve_space(xdr, 8);
4601 if (!p)
4602 return nfserr_resource;
4603 p = xdr_encode_hyper(p, lcp->lc_newsize);
4606 return 0;
4609 static __be32
4610 nfsd4_encode_layoutreturn(struct nfsd4_compoundres *resp, __be32 nfserr,
4611 struct nfsd4_layoutreturn *lrp)
4613 struct xdr_stream *xdr = &resp->xdr;
4614 __be32 *p;
4616 p = xdr_reserve_space(xdr, 4);
4617 if (!p)
4618 return nfserr_resource;
4619 *p++ = cpu_to_be32(lrp->lrs_present);
4620 if (lrp->lrs_present)
4621 return nfsd4_encode_stateid(xdr, &lrp->lr_sid);
4622 return 0;
4624 #endif /* CONFIG_NFSD_PNFS */
4626 static __be32
4627 nfsd42_encode_write_res(struct nfsd4_compoundres *resp,
4628 struct nfsd42_write_res *write, bool sync)
4630 __be32 *p;
4631 p = xdr_reserve_space(&resp->xdr, 4);
4632 if (!p)
4633 return nfserr_resource;
4635 if (sync)
4636 *p++ = cpu_to_be32(0);
4637 else {
4638 __be32 nfserr;
4639 *p++ = cpu_to_be32(1);
4640 nfserr = nfsd4_encode_stateid(&resp->xdr, &write->cb_stateid);
4641 if (nfserr)
4642 return nfserr;
4644 p = xdr_reserve_space(&resp->xdr, 8 + 4 + NFS4_VERIFIER_SIZE);
4645 if (!p)
4646 return nfserr_resource;
4648 p = xdr_encode_hyper(p, write->wr_bytes_written);
4649 *p++ = cpu_to_be32(write->wr_stable_how);
4650 p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
4651 NFS4_VERIFIER_SIZE);
4652 return nfs_ok;
4655 static __be32
4656 nfsd42_encode_nl4_server(struct nfsd4_compoundres *resp, struct nl4_server *ns)
4658 struct xdr_stream *xdr = &resp->xdr;
4659 struct nfs42_netaddr *addr;
4660 __be32 *p;
4662 p = xdr_reserve_space(xdr, 4);
4663 *p++ = cpu_to_be32(ns->nl4_type);
4665 switch (ns->nl4_type) {
4666 case NL4_NETADDR:
4667 addr = &ns->u.nl4_addr;
4669 /* netid_len, netid, uaddr_len, uaddr (port included
4670 * in RPCBIND_MAXUADDRLEN)
4672 p = xdr_reserve_space(xdr,
4673 4 /* netid len */ +
4674 (XDR_QUADLEN(addr->netid_len) * 4) +
4675 4 /* uaddr len */ +
4676 (XDR_QUADLEN(addr->addr_len) * 4));
4677 if (!p)
4678 return nfserr_resource;
4680 *p++ = cpu_to_be32(addr->netid_len);
4681 p = xdr_encode_opaque_fixed(p, addr->netid,
4682 addr->netid_len);
4683 *p++ = cpu_to_be32(addr->addr_len);
4684 p = xdr_encode_opaque_fixed(p, addr->addr,
4685 addr->addr_len);
4686 break;
4687 default:
4688 WARN_ON_ONCE(ns->nl4_type != NL4_NETADDR);
4689 return nfserr_inval;
4692 return 0;
4695 static __be32
4696 nfsd4_encode_copy(struct nfsd4_compoundres *resp, __be32 nfserr,
4697 struct nfsd4_copy *copy)
4699 __be32 *p;
4701 nfserr = nfsd42_encode_write_res(resp, &copy->cp_res,
4702 !!copy->cp_synchronous);
4703 if (nfserr)
4704 return nfserr;
4706 p = xdr_reserve_space(&resp->xdr, 4 + 4);
4707 *p++ = xdr_one; /* cr_consecutive */
4708 *p++ = cpu_to_be32(copy->cp_synchronous);
4709 return 0;
4712 static __be32
4713 nfsd4_encode_offload_status(struct nfsd4_compoundres *resp, __be32 nfserr,
4714 struct nfsd4_offload_status *os)
4716 struct xdr_stream *xdr = &resp->xdr;
4717 __be32 *p;
4719 p = xdr_reserve_space(xdr, 8 + 4);
4720 if (!p)
4721 return nfserr_resource;
4722 p = xdr_encode_hyper(p, os->count);
4723 *p++ = cpu_to_be32(0);
4724 return nfserr;
4727 static __be32
4728 nfsd4_encode_read_plus_data(struct nfsd4_compoundres *resp,
4729 struct nfsd4_read *read,
4730 unsigned long *maxcount, u32 *eof,
4731 loff_t *pos)
4733 struct xdr_stream *xdr = &resp->xdr;
4734 struct file *file = read->rd_nf->nf_file;
4735 int starting_len = xdr->buf->len;
4736 loff_t hole_pos;
4737 __be32 nfserr;
4738 __be32 *p, tmp;
4739 __be64 tmp64;
4741 hole_pos = pos ? *pos : vfs_llseek(file, read->rd_offset, SEEK_HOLE);
4742 if (hole_pos > read->rd_offset)
4743 *maxcount = min_t(unsigned long, *maxcount, hole_pos - read->rd_offset);
4744 *maxcount = min_t(unsigned long, *maxcount, (xdr->buf->buflen - xdr->buf->len));
4746 /* Content type, offset, byte count */
4747 p = xdr_reserve_space(xdr, 4 + 8 + 4);
4748 if (!p)
4749 return nfserr_resource;
4751 read->rd_vlen = xdr_reserve_space_vec(xdr, resp->rqstp->rq_vec, *maxcount);
4752 if (read->rd_vlen < 0)
4753 return nfserr_resource;
4755 nfserr = nfsd_readv(resp->rqstp, read->rd_fhp, file, read->rd_offset,
4756 resp->rqstp->rq_vec, read->rd_vlen, maxcount, eof);
4757 if (nfserr)
4758 return nfserr;
4760 tmp = htonl(NFS4_CONTENT_DATA);
4761 write_bytes_to_xdr_buf(xdr->buf, starting_len, &tmp, 4);
4762 tmp64 = cpu_to_be64(read->rd_offset);
4763 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp64, 8);
4764 tmp = htonl(*maxcount);
4765 write_bytes_to_xdr_buf(xdr->buf, starting_len + 12, &tmp, 4);
4766 return nfs_ok;
4769 static __be32
4770 nfsd4_encode_read_plus_hole(struct nfsd4_compoundres *resp,
4771 struct nfsd4_read *read,
4772 unsigned long *maxcount, u32 *eof)
4774 struct file *file = read->rd_nf->nf_file;
4775 loff_t data_pos = vfs_llseek(file, read->rd_offset, SEEK_DATA);
4776 loff_t f_size = i_size_read(file_inode(file));
4777 unsigned long count;
4778 __be32 *p;
4780 if (data_pos == -ENXIO)
4781 data_pos = f_size;
4782 else if (data_pos <= read->rd_offset || (data_pos < f_size && data_pos % PAGE_SIZE))
4783 return nfsd4_encode_read_plus_data(resp, read, maxcount, eof, &f_size);
4784 count = data_pos - read->rd_offset;
4786 /* Content type, offset, byte count */
4787 p = xdr_reserve_space(&resp->xdr, 4 + 8 + 8);
4788 if (!p)
4789 return nfserr_resource;
4791 *p++ = htonl(NFS4_CONTENT_HOLE);
4792 p = xdr_encode_hyper(p, read->rd_offset);
4793 p = xdr_encode_hyper(p, count);
4795 *eof = (read->rd_offset + count) >= f_size;
4796 *maxcount = min_t(unsigned long, count, *maxcount);
4797 return nfs_ok;
4800 static __be32
4801 nfsd4_encode_read_plus(struct nfsd4_compoundres *resp, __be32 nfserr,
4802 struct nfsd4_read *read)
4804 unsigned long maxcount, count;
4805 struct xdr_stream *xdr = &resp->xdr;
4806 struct file *file;
4807 int starting_len = xdr->buf->len;
4808 int last_segment = xdr->buf->len;
4809 int segments = 0;
4810 __be32 *p, tmp;
4811 bool is_data;
4812 loff_t pos;
4813 u32 eof;
4815 if (nfserr)
4816 return nfserr;
4817 file = read->rd_nf->nf_file;
4819 /* eof flag, segment count */
4820 p = xdr_reserve_space(xdr, 4 + 4);
4821 if (!p)
4822 return nfserr_resource;
4823 xdr_commit_encode(xdr);
4825 maxcount = svc_max_payload(resp->rqstp);
4826 maxcount = min_t(unsigned long, maxcount,
4827 (xdr->buf->buflen - xdr->buf->len));
4828 maxcount = min_t(unsigned long, maxcount, read->rd_length);
4829 count = maxcount;
4831 eof = read->rd_offset >= i_size_read(file_inode(file));
4832 if (eof)
4833 goto out;
4835 pos = vfs_llseek(file, read->rd_offset, SEEK_HOLE);
4836 is_data = pos > read->rd_offset;
4838 while (count > 0 && !eof) {
4839 maxcount = count;
4840 if (is_data)
4841 nfserr = nfsd4_encode_read_plus_data(resp, read, &maxcount, &eof,
4842 segments == 0 ? &pos : NULL);
4843 else
4844 nfserr = nfsd4_encode_read_plus_hole(resp, read, &maxcount, &eof);
4845 if (nfserr)
4846 goto out;
4847 count -= maxcount;
4848 read->rd_offset += maxcount;
4849 is_data = !is_data;
4850 last_segment = xdr->buf->len;
4851 segments++;
4854 out:
4855 if (nfserr && segments == 0)
4856 xdr_truncate_encode(xdr, starting_len);
4857 else {
4858 tmp = htonl(eof);
4859 write_bytes_to_xdr_buf(xdr->buf, starting_len, &tmp, 4);
4860 tmp = htonl(segments);
4861 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp, 4);
4862 if (nfserr) {
4863 xdr_truncate_encode(xdr, last_segment);
4864 nfserr = nfs_ok;
4868 return nfserr;
4871 static __be32
4872 nfsd4_encode_copy_notify(struct nfsd4_compoundres *resp, __be32 nfserr,
4873 struct nfsd4_copy_notify *cn)
4875 struct xdr_stream *xdr = &resp->xdr;
4876 __be32 *p;
4878 if (nfserr)
4879 return nfserr;
4881 /* 8 sec, 4 nsec */
4882 p = xdr_reserve_space(xdr, 12);
4883 if (!p)
4884 return nfserr_resource;
4886 /* cnr_lease_time */
4887 p = xdr_encode_hyper(p, cn->cpn_sec);
4888 *p++ = cpu_to_be32(cn->cpn_nsec);
4890 /* cnr_stateid */
4891 nfserr = nfsd4_encode_stateid(xdr, &cn->cpn_cnr_stateid);
4892 if (nfserr)
4893 return nfserr;
4895 /* cnr_src.nl_nsvr */
4896 p = xdr_reserve_space(xdr, 4);
4897 if (!p)
4898 return nfserr_resource;
4900 *p++ = cpu_to_be32(1);
4902 return nfsd42_encode_nl4_server(resp, &cn->cpn_src);
4905 static __be32
4906 nfsd4_encode_seek(struct nfsd4_compoundres *resp, __be32 nfserr,
4907 struct nfsd4_seek *seek)
4909 __be32 *p;
4911 p = xdr_reserve_space(&resp->xdr, 4 + 8);
4912 *p++ = cpu_to_be32(seek->seek_eof);
4913 p = xdr_encode_hyper(p, seek->seek_pos);
4915 return 0;
4918 static __be32
4919 nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
4921 return nfserr;
4925 * Encode kmalloc-ed buffer in to XDR stream.
4927 static __be32
4928 nfsd4_vbuf_to_stream(struct xdr_stream *xdr, char *buf, u32 buflen)
4930 u32 cplen;
4931 __be32 *p;
4933 cplen = min_t(unsigned long, buflen,
4934 ((void *)xdr->end - (void *)xdr->p));
4935 p = xdr_reserve_space(xdr, cplen);
4936 if (!p)
4937 return nfserr_resource;
4939 memcpy(p, buf, cplen);
4940 buf += cplen;
4941 buflen -= cplen;
4943 while (buflen) {
4944 cplen = min_t(u32, buflen, PAGE_SIZE);
4945 p = xdr_reserve_space(xdr, cplen);
4946 if (!p)
4947 return nfserr_resource;
4949 memcpy(p, buf, cplen);
4951 if (cplen < PAGE_SIZE) {
4953 * We're done, with a length that wasn't page
4954 * aligned, so possibly not word aligned. Pad
4955 * any trailing bytes with 0.
4957 xdr_encode_opaque_fixed(p, NULL, cplen);
4958 break;
4961 buflen -= PAGE_SIZE;
4962 buf += PAGE_SIZE;
4965 return 0;
4968 static __be32
4969 nfsd4_encode_getxattr(struct nfsd4_compoundres *resp, __be32 nfserr,
4970 struct nfsd4_getxattr *getxattr)
4972 struct xdr_stream *xdr = &resp->xdr;
4973 __be32 *p, err;
4975 p = xdr_reserve_space(xdr, 4);
4976 if (!p)
4977 return nfserr_resource;
4979 *p = cpu_to_be32(getxattr->getxa_len);
4981 if (getxattr->getxa_len == 0)
4982 return 0;
4984 err = nfsd4_vbuf_to_stream(xdr, getxattr->getxa_buf,
4985 getxattr->getxa_len);
4987 kvfree(getxattr->getxa_buf);
4989 return err;
4992 static __be32
4993 nfsd4_encode_setxattr(struct nfsd4_compoundres *resp, __be32 nfserr,
4994 struct nfsd4_setxattr *setxattr)
4996 struct xdr_stream *xdr = &resp->xdr;
4997 __be32 *p;
4999 p = xdr_reserve_space(xdr, 20);
5000 if (!p)
5001 return nfserr_resource;
5003 encode_cinfo(p, &setxattr->setxa_cinfo);
5005 return 0;
5009 * See if there are cookie values that can be rejected outright.
5011 static __be32
5012 nfsd4_listxattr_validate_cookie(struct nfsd4_listxattrs *listxattrs,
5013 u32 *offsetp)
5015 u64 cookie = listxattrs->lsxa_cookie;
5018 * If the cookie is larger than the maximum number we can fit
5019 * in either the buffer we just got back from vfs_listxattr, or,
5020 * XDR-encoded, in the return buffer, it's invalid.
5022 if (cookie > (listxattrs->lsxa_len) / (XATTR_USER_PREFIX_LEN + 2))
5023 return nfserr_badcookie;
5025 if (cookie > (listxattrs->lsxa_maxcount /
5026 (XDR_QUADLEN(XATTR_USER_PREFIX_LEN + 2) + 4)))
5027 return nfserr_badcookie;
5029 *offsetp = (u32)cookie;
5030 return 0;
5033 static __be32
5034 nfsd4_encode_listxattrs(struct nfsd4_compoundres *resp, __be32 nfserr,
5035 struct nfsd4_listxattrs *listxattrs)
5037 struct xdr_stream *xdr = &resp->xdr;
5038 u32 cookie_offset, count_offset, eof;
5039 u32 left, xdrleft, slen, count;
5040 u32 xdrlen, offset;
5041 u64 cookie;
5042 char *sp;
5043 __be32 status, tmp;
5044 __be32 *p;
5045 u32 nuser;
5047 eof = 1;
5049 status = nfsd4_listxattr_validate_cookie(listxattrs, &offset);
5050 if (status)
5051 goto out;
5054 * Reserve space for the cookie and the name array count. Record
5055 * the offsets to save them later.
5057 cookie_offset = xdr->buf->len;
5058 count_offset = cookie_offset + 8;
5059 p = xdr_reserve_space(xdr, 12);
5060 if (!p) {
5061 status = nfserr_resource;
5062 goto out;
5065 count = 0;
5066 left = listxattrs->lsxa_len;
5067 sp = listxattrs->lsxa_buf;
5068 nuser = 0;
5070 xdrleft = listxattrs->lsxa_maxcount;
5072 while (left > 0 && xdrleft > 0) {
5073 slen = strlen(sp);
5076 * Check if this is a "user." attribute, skip it if not.
5078 if (strncmp(sp, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
5079 goto contloop;
5081 slen -= XATTR_USER_PREFIX_LEN;
5082 xdrlen = 4 + ((slen + 3) & ~3);
5083 if (xdrlen > xdrleft) {
5084 if (count == 0) {
5086 * Can't even fit the first attribute name.
5088 status = nfserr_toosmall;
5089 goto out;
5091 eof = 0;
5092 goto wreof;
5095 left -= XATTR_USER_PREFIX_LEN;
5096 sp += XATTR_USER_PREFIX_LEN;
5097 if (nuser++ < offset)
5098 goto contloop;
5101 p = xdr_reserve_space(xdr, xdrlen);
5102 if (!p) {
5103 status = nfserr_resource;
5104 goto out;
5107 xdr_encode_opaque(p, sp, slen);
5109 xdrleft -= xdrlen;
5110 count++;
5111 contloop:
5112 sp += slen + 1;
5113 left -= slen + 1;
5117 * If there were user attributes to copy, but we didn't copy
5118 * any, the offset was too large (e.g. the cookie was invalid).
5120 if (nuser > 0 && count == 0) {
5121 status = nfserr_badcookie;
5122 goto out;
5125 wreof:
5126 p = xdr_reserve_space(xdr, 4);
5127 if (!p) {
5128 status = nfserr_resource;
5129 goto out;
5131 *p = cpu_to_be32(eof);
5133 cookie = offset + count;
5135 write_bytes_to_xdr_buf(xdr->buf, cookie_offset, &cookie, 8);
5136 tmp = cpu_to_be32(count);
5137 write_bytes_to_xdr_buf(xdr->buf, count_offset, &tmp, 4);
5138 out:
5139 if (listxattrs->lsxa_len)
5140 kvfree(listxattrs->lsxa_buf);
5141 return status;
5144 static __be32
5145 nfsd4_encode_removexattr(struct nfsd4_compoundres *resp, __be32 nfserr,
5146 struct nfsd4_removexattr *removexattr)
5148 struct xdr_stream *xdr = &resp->xdr;
5149 __be32 *p;
5151 p = xdr_reserve_space(xdr, 20);
5152 if (!p)
5153 return nfserr_resource;
5155 p = encode_cinfo(p, &removexattr->rmxa_cinfo);
5156 return 0;
5159 typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
5162 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
5163 * since we don't need to filter out obsolete ops as this is
5164 * done in the decoding phase.
5166 static const nfsd4_enc nfsd4_enc_ops[] = {
5167 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
5168 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
5169 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
5170 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
5171 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
5172 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
5173 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
5174 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
5175 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
5176 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
5177 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
5178 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
5179 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
5180 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
5181 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
5182 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
5183 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
5184 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
5185 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
5186 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
5187 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
5188 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
5189 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
5190 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
5191 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
5192 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
5193 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
5194 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
5195 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
5196 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
5197 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
5198 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
5199 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
5200 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
5201 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
5202 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
5203 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
5205 /* NFSv4.1 operations */
5206 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
5207 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
5208 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
5209 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
5210 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_noop,
5211 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
5212 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
5213 #ifdef CONFIG_NFSD_PNFS
5214 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_getdeviceinfo,
5215 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
5216 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_layoutcommit,
5217 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_layoutget,
5218 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_layoutreturn,
5219 #else
5220 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
5221 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
5222 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
5223 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
5224 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
5225 #endif
5226 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
5227 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
5228 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
5229 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
5230 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
5231 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
5232 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
5234 /* NFSv4.2 operations */
5235 [OP_ALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
5236 [OP_COPY] = (nfsd4_enc)nfsd4_encode_copy,
5237 [OP_COPY_NOTIFY] = (nfsd4_enc)nfsd4_encode_copy_notify,
5238 [OP_DEALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
5239 [OP_IO_ADVISE] = (nfsd4_enc)nfsd4_encode_noop,
5240 [OP_LAYOUTERROR] = (nfsd4_enc)nfsd4_encode_noop,
5241 [OP_LAYOUTSTATS] = (nfsd4_enc)nfsd4_encode_noop,
5242 [OP_OFFLOAD_CANCEL] = (nfsd4_enc)nfsd4_encode_noop,
5243 [OP_OFFLOAD_STATUS] = (nfsd4_enc)nfsd4_encode_offload_status,
5244 [OP_READ_PLUS] = (nfsd4_enc)nfsd4_encode_read_plus,
5245 [OP_SEEK] = (nfsd4_enc)nfsd4_encode_seek,
5246 [OP_WRITE_SAME] = (nfsd4_enc)nfsd4_encode_noop,
5247 [OP_CLONE] = (nfsd4_enc)nfsd4_encode_noop,
5249 /* RFC 8276 extended atributes operations */
5250 [OP_GETXATTR] = (nfsd4_enc)nfsd4_encode_getxattr,
5251 [OP_SETXATTR] = (nfsd4_enc)nfsd4_encode_setxattr,
5252 [OP_LISTXATTRS] = (nfsd4_enc)nfsd4_encode_listxattrs,
5253 [OP_REMOVEXATTR] = (nfsd4_enc)nfsd4_encode_removexattr,
5257 * Calculate whether we still have space to encode repsize bytes.
5258 * There are two considerations:
5259 * - For NFS versions >=4.1, the size of the reply must stay within
5260 * session limits
5261 * - For all NFS versions, we must stay within limited preallocated
5262 * buffer space.
5264 * This is called before the operation is processed, so can only provide
5265 * an upper estimate. For some nonidempotent operations (such as
5266 * getattr), it's not necessarily a problem if that estimate is wrong,
5267 * as we can fail it after processing without significant side effects.
5269 __be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 respsize)
5271 struct xdr_buf *buf = &resp->rqstp->rq_res;
5272 struct nfsd4_slot *slot = resp->cstate.slot;
5274 if (buf->len + respsize <= buf->buflen)
5275 return nfs_ok;
5276 if (!nfsd4_has_session(&resp->cstate))
5277 return nfserr_resource;
5278 if (slot->sl_flags & NFSD4_SLOT_CACHETHIS) {
5279 WARN_ON_ONCE(1);
5280 return nfserr_rep_too_big_to_cache;
5282 return nfserr_rep_too_big;
5285 void
5286 nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
5288 struct xdr_stream *xdr = &resp->xdr;
5289 struct nfs4_stateowner *so = resp->cstate.replay_owner;
5290 struct svc_rqst *rqstp = resp->rqstp;
5291 const struct nfsd4_operation *opdesc = op->opdesc;
5292 int post_err_offset;
5293 nfsd4_enc encoder;
5294 __be32 *p;
5296 p = xdr_reserve_space(xdr, 8);
5297 if (!p) {
5298 WARN_ON_ONCE(1);
5299 return;
5301 *p++ = cpu_to_be32(op->opnum);
5302 post_err_offset = xdr->buf->len;
5304 if (op->opnum == OP_ILLEGAL)
5305 goto status;
5306 if (op->status && opdesc &&
5307 !(opdesc->op_flags & OP_NONTRIVIAL_ERROR_ENCODE))
5308 goto status;
5309 BUG_ON(op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
5310 !nfsd4_enc_ops[op->opnum]);
5311 encoder = nfsd4_enc_ops[op->opnum];
5312 op->status = encoder(resp, op->status, &op->u);
5313 if (op->status)
5314 trace_nfsd_compound_encode_err(rqstp, op->opnum, op->status);
5315 if (opdesc && opdesc->op_release)
5316 opdesc->op_release(&op->u);
5317 xdr_commit_encode(xdr);
5319 /* nfsd4_check_resp_size guarantees enough room for error status */
5320 if (!op->status) {
5321 int space_needed = 0;
5322 if (!nfsd4_last_compound_op(rqstp))
5323 space_needed = COMPOUND_ERR_SLACK_SPACE;
5324 op->status = nfsd4_check_resp_size(resp, space_needed);
5326 if (op->status == nfserr_resource && nfsd4_has_session(&resp->cstate)) {
5327 struct nfsd4_slot *slot = resp->cstate.slot;
5329 if (slot->sl_flags & NFSD4_SLOT_CACHETHIS)
5330 op->status = nfserr_rep_too_big_to_cache;
5331 else
5332 op->status = nfserr_rep_too_big;
5334 if (op->status == nfserr_resource ||
5335 op->status == nfserr_rep_too_big ||
5336 op->status == nfserr_rep_too_big_to_cache) {
5338 * The operation may have already been encoded or
5339 * partially encoded. No op returns anything additional
5340 * in the case of one of these three errors, so we can
5341 * just truncate back to after the status. But it's a
5342 * bug if we had to do this on a non-idempotent op:
5344 warn_on_nonidempotent_op(op);
5345 xdr_truncate_encode(xdr, post_err_offset);
5347 if (so) {
5348 int len = xdr->buf->len - post_err_offset;
5350 so->so_replay.rp_status = op->status;
5351 so->so_replay.rp_buflen = len;
5352 read_bytes_from_xdr_buf(xdr->buf, post_err_offset,
5353 so->so_replay.rp_buf, len);
5355 status:
5356 /* Note that op->status is already in network byte order: */
5357 write_bytes_to_xdr_buf(xdr->buf, post_err_offset - 4, &op->status, 4);
5361 * Encode the reply stored in the stateowner reply cache
5363 * XDR note: do not encode rp->rp_buflen: the buffer contains the
5364 * previously sent already encoded operation.
5366 void
5367 nfsd4_encode_replay(struct xdr_stream *xdr, struct nfsd4_op *op)
5369 __be32 *p;
5370 struct nfs4_replay *rp = op->replay;
5372 p = xdr_reserve_space(xdr, 8 + rp->rp_buflen);
5373 if (!p) {
5374 WARN_ON_ONCE(1);
5375 return;
5377 *p++ = cpu_to_be32(op->opnum);
5378 *p++ = rp->rp_status; /* already xdr'ed */
5380 p = xdr_encode_opaque_fixed(p, rp->rp_buf, rp->rp_buflen);
5383 void nfsd4_release_compoundargs(struct svc_rqst *rqstp)
5385 struct nfsd4_compoundargs *args = rqstp->rq_argp;
5387 if (args->ops != args->iops) {
5388 kfree(args->ops);
5389 args->ops = args->iops;
5391 while (args->to_free) {
5392 struct svcxdr_tmpbuf *tb = args->to_free;
5393 args->to_free = tb->next;
5394 kfree(tb);
5399 nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p)
5401 struct nfsd4_compoundargs *args = rqstp->rq_argp;
5403 /* svcxdr_tmp_alloc */
5404 args->to_free = NULL;
5406 args->xdr = &rqstp->rq_arg_stream;
5407 args->ops = args->iops;
5408 args->rqstp = rqstp;
5410 return nfsd4_decode_compound(args);
5414 nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p)
5416 struct nfsd4_compoundres *resp = rqstp->rq_resp;
5417 struct xdr_buf *buf = resp->xdr.buf;
5419 WARN_ON_ONCE(buf->len != buf->head[0].iov_len + buf->page_len +
5420 buf->tail[0].iov_len);
5422 *p = resp->cstate.status;
5424 rqstp->rq_next_page = resp->xdr.page_ptr + 1;
5426 p = resp->tagp;
5427 *p++ = htonl(resp->taglen);
5428 memcpy(p, resp->tag, resp->taglen);
5429 p += XDR_QUADLEN(resp->taglen);
5430 *p++ = htonl(resp->opcnt);
5432 nfsd4_sequence_done(resp);
5433 return 1;
5437 * Local variables:
5438 * c-basic-offset: 8
5439 * End: