2 * Server-side XDR for NFSv4
4 * Copyright (c) 2002 The Regents of the University of Michigan.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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>
53 #include "filecache.h"
55 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
56 #include <linux/security.h>
60 #define NFSDDBG_FACILITY NFSDDBG_XDR
62 const u32 nfsd_suppattrs[3][3] = {
63 {NFSD4_SUPPORTED_ATTRS_WORD0,
64 NFSD4_SUPPORTED_ATTRS_WORD1,
65 NFSD4_SUPPORTED_ATTRS_WORD2},
67 {NFSD4_1_SUPPORTED_ATTRS_WORD0,
68 NFSD4_1_SUPPORTED_ATTRS_WORD1,
69 NFSD4_1_SUPPORTED_ATTRS_WORD2},
71 {NFSD4_1_SUPPORTED_ATTRS_WORD0,
72 NFSD4_1_SUPPORTED_ATTRS_WORD1,
73 NFSD4_2_SUPPORTED_ATTRS_WORD2},
77 * As per referral draft, the fsid for a referral MUST be different from the fsid of the containing
78 * directory in order to indicate to the client that a filesystem boundary is present
79 * We use a fixed fsid for a referral
81 #define NFS4_REFERRAL_FSID_MAJOR 0x8000000ULL
82 #define NFS4_REFERRAL_FSID_MINOR 0x8000000ULL
85 check_filename(char *str, int len)
91 if (isdotent(str, len))
92 return nfserr_badname;
93 for (i = 0; i < len; i++)
95 return nfserr_badname;
102 #define DECODE_TAIL \
107 dprintk("NFSD: xdr error (%s:%d)\n", \
108 __FILE__, __LINE__); \
109 status = nfserr_bad_xdr; \
112 #define READMEM(x,nbytes) do { \
114 p += XDR_QUADLEN(nbytes); \
116 #define SAVEMEM(x,nbytes) do { \
117 if (!(x = (p==argp->tmp || p == argp->tmpp) ? \
118 savemem(argp, p, nbytes) : \
120 dprintk("NFSD: xdr error (%s:%d)\n", \
121 __FILE__, __LINE__); \
124 p += XDR_QUADLEN(nbytes); \
126 #define COPYMEM(x,nbytes) do { \
127 memcpy((x), p, nbytes); \
128 p += XDR_QUADLEN(nbytes); \
131 /* READ_BUF, read_buf(): nbytes must be <= PAGE_SIZE */
132 #define READ_BUF(nbytes) do { \
133 if (nbytes <= (u32)((char *)argp->end - (char *)argp->p)) { \
135 argp->p += XDR_QUADLEN(nbytes); \
136 } else if (!(p = read_buf(argp, nbytes))) { \
137 dprintk("NFSD: xdr error (%s:%d)\n", \
138 __FILE__, __LINE__); \
143 static void next_decode_page(struct nfsd4_compoundargs *argp)
145 argp->p = page_address(argp->pagelist[0]);
147 if (argp->pagelen < PAGE_SIZE) {
148 argp->end = argp->p + XDR_QUADLEN(argp->pagelen);
151 argp->end = argp->p + (PAGE_SIZE>>2);
152 argp->pagelen -= PAGE_SIZE;
156 static __be32 *read_buf(struct nfsd4_compoundargs *argp, u32 nbytes)
158 /* We want more bytes than seem to be available.
159 * Maybe we need a new page, maybe we have just run out
161 unsigned int avail = (char *)argp->end - (char *)argp->p;
164 if (argp->pagelen == 0) {
165 struct kvec *vec = &argp->rqstp->rq_arg.tail[0];
169 avail = vec->iov_len;
170 argp->p = vec->iov_base;
171 argp->end = vec->iov_base + avail;
178 argp->p += XDR_QUADLEN(nbytes);
182 if (avail + argp->pagelen < nbytes)
184 if (avail + PAGE_SIZE < nbytes) /* need more than a page !! */
186 /* ok, we can do it with the current plus the next page */
187 if (nbytes <= sizeof(argp->tmp))
191 p = argp->tmpp = kmalloc(nbytes, GFP_KERNEL);
197 * The following memcpy is safe because read_buf is always
198 * called with nbytes > avail, and the two cases above both
199 * guarantee p points to at least nbytes bytes.
201 memcpy(p, argp->p, avail);
202 next_decode_page(argp);
203 memcpy(((char*)p)+avail, argp->p, (nbytes - avail));
204 argp->p += XDR_QUADLEN(nbytes - avail);
208 static unsigned int compoundargs_bytes_left(struct nfsd4_compoundargs *argp)
210 unsigned int this = (char *)argp->end - (char *)argp->p;
212 return this + argp->pagelen;
215 static int zero_clientid(clientid_t *clid)
217 return (clid->cl_boot == 0) && (clid->cl_id == 0);
221 * svcxdr_tmpalloc - allocate memory to be freed after compound processing
222 * @argp: NFSv4 compound argument structure
223 * @len: length of buffer to allocate
225 * Allocates a buffer of size @len to be freed when processing the compound
226 * operation described in @argp finishes.
229 svcxdr_tmpalloc(struct nfsd4_compoundargs *argp, u32 len)
231 struct svcxdr_tmpbuf *tb;
233 tb = kmalloc(sizeof(*tb) + len, GFP_KERNEL);
236 tb->next = argp->to_free;
242 * For xdr strings that need to be passed to other kernel api's
243 * as null-terminated strings.
245 * Note null-terminating in place usually isn't safe since the
246 * buffer might end on a page boundary.
249 svcxdr_dupstr(struct nfsd4_compoundargs *argp, void *buf, u32 len)
251 char *p = svcxdr_tmpalloc(argp, len + 1);
261 * savemem - duplicate a chunk of memory for later processing
262 * @argp: NFSv4 compound argument structure to be freed with
263 * @p: pointer to be duplicated
264 * @nbytes: length to be duplicated
266 * Returns a pointer to a copy of @nbytes bytes of memory at @p
267 * that are preserved until processing of the NFSv4 compound
268 * operation described by @argp finishes.
270 static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
274 ret = svcxdr_tmpalloc(argp, nbytes);
277 memcpy(ret, p, nbytes);
282 nfsd4_decode_time(struct nfsd4_compoundargs *argp, struct timespec64 *tv)
287 p = xdr_decode_hyper(p, &tv->tv_sec);
288 tv->tv_nsec = be32_to_cpup(p++);
289 if (tv->tv_nsec >= (u32)1000000000)
296 nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
306 bmlen = be32_to_cpup(p++);
310 READ_BUF(bmlen << 2);
312 bmval[0] = be32_to_cpup(p++);
314 bmval[1] = be32_to_cpup(p++);
316 bmval[2] = be32_to_cpup(p++);
322 nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval,
323 struct iattr *iattr, struct nfs4_acl **acl,
324 struct xdr_netobj *label, int *umask)
326 int expected_len, len = 0;
332 if ((status = nfsd4_decode_bitmap(argp, bmval)))
335 if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
336 || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
337 || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2) {
338 if (nfsd_attrs_supported(argp->minorversion, bmval))
340 return nfserr_attrnotsupp;
344 expected_len = be32_to_cpup(p++);
346 if (bmval[0] & FATTR4_WORD0_SIZE) {
349 p = xdr_decode_hyper(p, &iattr->ia_size);
350 iattr->ia_valid |= ATTR_SIZE;
352 if (bmval[0] & FATTR4_WORD0_ACL) {
354 struct nfs4_ace *ace;
356 READ_BUF(4); len += 4;
357 nace = be32_to_cpup(p++);
359 if (nace > compoundargs_bytes_left(argp)/20)
361 * Even with 4-byte names there wouldn't be
362 * space for that many aces; something fishy is
367 *acl = svcxdr_tmpalloc(argp, nfs4_acl_bytes(nace));
369 return nfserr_jukebox;
371 (*acl)->naces = nace;
372 for (ace = (*acl)->aces; ace < (*acl)->aces + nace; ace++) {
373 READ_BUF(16); len += 16;
374 ace->type = be32_to_cpup(p++);
375 ace->flag = be32_to_cpup(p++);
376 ace->access_mask = be32_to_cpup(p++);
377 dummy32 = be32_to_cpup(p++);
379 len += XDR_QUADLEN(dummy32) << 2;
380 READMEM(buf, dummy32);
381 ace->whotype = nfs4_acl_get_whotype(buf, dummy32);
383 if (ace->whotype != NFS4_ACL_WHO_NAMED)
385 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
386 status = nfsd_map_name_to_gid(argp->rqstp,
387 buf, dummy32, &ace->who_gid);
389 status = nfsd_map_name_to_uid(argp->rqstp,
390 buf, dummy32, &ace->who_uid);
396 if (bmval[1] & FATTR4_WORD1_MODE) {
399 iattr->ia_mode = be32_to_cpup(p++);
400 iattr->ia_mode &= (S_IFMT | S_IALLUGO);
401 iattr->ia_valid |= ATTR_MODE;
403 if (bmval[1] & FATTR4_WORD1_OWNER) {
406 dummy32 = be32_to_cpup(p++);
408 len += (XDR_QUADLEN(dummy32) << 2);
409 READMEM(buf, dummy32);
410 if ((status = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
412 iattr->ia_valid |= ATTR_UID;
414 if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
417 dummy32 = be32_to_cpup(p++);
419 len += (XDR_QUADLEN(dummy32) << 2);
420 READMEM(buf, dummy32);
421 if ((status = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
423 iattr->ia_valid |= ATTR_GID;
425 if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
428 dummy32 = be32_to_cpup(p++);
430 case NFS4_SET_TO_CLIENT_TIME:
432 status = nfsd4_decode_time(argp, &iattr->ia_atime);
435 iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
437 case NFS4_SET_TO_SERVER_TIME:
438 iattr->ia_valid |= ATTR_ATIME;
444 if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
447 dummy32 = be32_to_cpup(p++);
449 case NFS4_SET_TO_CLIENT_TIME:
451 status = nfsd4_decode_time(argp, &iattr->ia_mtime);
454 iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
456 case NFS4_SET_TO_SERVER_TIME:
457 iattr->ia_valid |= ATTR_MTIME;
465 if (IS_ENABLED(CONFIG_NFSD_V4_SECURITY_LABEL) &&
466 bmval[2] & FATTR4_WORD2_SECURITY_LABEL) {
469 dummy32 = be32_to_cpup(p++); /* lfs: we don't use it */
472 dummy32 = be32_to_cpup(p++); /* pi: we don't use it either */
475 dummy32 = be32_to_cpup(p++);
477 if (dummy32 > NFS4_MAXLABELLEN)
478 return nfserr_badlabel;
479 len += (XDR_QUADLEN(dummy32) << 2);
480 READMEM(buf, dummy32);
481 label->len = dummy32;
482 label->data = svcxdr_dupstr(argp, buf, dummy32);
484 return nfserr_jukebox;
486 if (bmval[2] & FATTR4_WORD2_MODE_UMASK) {
491 dummy32 = be32_to_cpup(p++);
492 iattr->ia_mode = dummy32 & (S_IFMT | S_IALLUGO);
493 dummy32 = be32_to_cpup(p++);
494 *umask = dummy32 & S_IRWXUGO;
495 iattr->ia_valid |= ATTR_MODE;
497 if (len != expected_len)
504 nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
508 READ_BUF(sizeof(stateid_t));
509 sid->si_generation = be32_to_cpup(p++);
510 COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
516 nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
521 access->ac_req_access = be32_to_cpup(p++);
526 static __be32 nfsd4_decode_cb_sec(struct nfsd4_compoundargs *argp, struct nfsd4_cb_sec *cbs)
529 struct user_namespace *userns = nfsd_user_namespace(argp->rqstp);
535 /* callback_sec_params4 */
537 nr_secflavs = be32_to_cpup(p++);
539 cbs->flavor = (u32)(-1);
541 /* Is this legal? Be generous, take it to mean AUTH_NONE: */
543 for (i = 0; i < nr_secflavs; ++i) {
545 dummy = be32_to_cpup(p++);
548 /* Nothing to read */
549 if (cbs->flavor == (u32)(-1))
550 cbs->flavor = RPC_AUTH_NULL;
555 dummy = be32_to_cpup(p++);
558 dummy = be32_to_cpup(p++);
560 SAVEMEM(machine_name, dummy);
564 uid = be32_to_cpup(p++);
565 gid = be32_to_cpup(p++);
569 dummy = be32_to_cpup(p++);
571 if (cbs->flavor == (u32)(-1)) {
572 kuid_t kuid = make_kuid(userns, uid);
573 kgid_t kgid = make_kgid(userns, gid);
574 if (uid_valid(kuid) && gid_valid(kgid)) {
577 cbs->flavor = RPC_AUTH_UNIX;
579 dprintk("RPC_AUTH_UNIX with invalid"
580 "uid or gid ignoring!\n");
585 dprintk("RPC_AUTH_GSS callback secflavor "
589 dummy = be32_to_cpup(p++);
590 /* gcbp_handle_from_server */
591 dummy = be32_to_cpup(p++);
593 p += XDR_QUADLEN(dummy);
594 /* gcbp_handle_from_client */
596 dummy = be32_to_cpup(p++);
600 dprintk("Illegal callback secflavor\n");
607 static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp, struct nfsd4_backchannel_ctl *bc)
612 bc->bc_cb_program = be32_to_cpup(p++);
613 nfsd4_decode_cb_sec(argp, &bc->bc_cb_sec);
618 static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
622 READ_BUF(NFS4_MAX_SESSIONID_LEN + 8);
623 COPYMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
624 bcts->dir = be32_to_cpup(p++);
625 /* XXX: skipping ctsa_use_conn_in_rdma_mode. Perhaps Tom Tucker
626 * could help us figure out we should be using it. */
631 nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
636 close->cl_seqid = be32_to_cpup(p++);
637 return nfsd4_decode_stateid(argp, &close->cl_stateid);
644 nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
649 p = xdr_decode_hyper(p, &commit->co_offset);
650 commit->co_count = be32_to_cpup(p++);
656 nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
661 create->cr_type = be32_to_cpup(p++);
662 switch (create->cr_type) {
665 create->cr_datalen = be32_to_cpup(p++);
666 READ_BUF(create->cr_datalen);
667 create->cr_data = svcxdr_dupstr(argp, p, create->cr_datalen);
668 if (!create->cr_data)
669 return nfserr_jukebox;
674 create->cr_specdata1 = be32_to_cpup(p++);
675 create->cr_specdata2 = be32_to_cpup(p++);
685 create->cr_namelen = be32_to_cpup(p++);
686 READ_BUF(create->cr_namelen);
687 SAVEMEM(create->cr_name, create->cr_namelen);
688 if ((status = check_filename(create->cr_name, create->cr_namelen)))
691 status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr,
692 &create->cr_acl, &create->cr_label,
701 nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
703 return nfsd4_decode_stateid(argp, &dr->dr_stateid);
707 nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
709 return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
713 nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
718 link->li_namelen = be32_to_cpup(p++);
719 READ_BUF(link->li_namelen);
720 SAVEMEM(link->li_name, link->li_namelen);
721 if ((status = check_filename(link->li_name, link->li_namelen)))
728 nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
733 * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
736 lock->lk_type = be32_to_cpup(p++);
737 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
739 lock->lk_reclaim = be32_to_cpup(p++);
740 p = xdr_decode_hyper(p, &lock->lk_offset);
741 p = xdr_decode_hyper(p, &lock->lk_length);
742 lock->lk_is_new = be32_to_cpup(p++);
744 if (lock->lk_is_new) {
746 lock->lk_new_open_seqid = be32_to_cpup(p++);
747 status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
750 READ_BUF(8 + sizeof(clientid_t));
751 lock->lk_new_lock_seqid = be32_to_cpup(p++);
752 COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
753 lock->lk_new_owner.len = be32_to_cpup(p++);
754 READ_BUF(lock->lk_new_owner.len);
755 READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
757 status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
761 lock->lk_old_lock_seqid = be32_to_cpup(p++);
768 nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
773 lockt->lt_type = be32_to_cpup(p++);
774 if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
776 p = xdr_decode_hyper(p, &lockt->lt_offset);
777 p = xdr_decode_hyper(p, &lockt->lt_length);
778 COPYMEM(&lockt->lt_clientid, 8);
779 lockt->lt_owner.len = be32_to_cpup(p++);
780 READ_BUF(lockt->lt_owner.len);
781 READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
787 nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
792 locku->lu_type = be32_to_cpup(p++);
793 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
795 locku->lu_seqid = be32_to_cpup(p++);
796 status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
800 p = xdr_decode_hyper(p, &locku->lu_offset);
801 p = xdr_decode_hyper(p, &locku->lu_length);
807 nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
812 lookup->lo_len = be32_to_cpup(p++);
813 READ_BUF(lookup->lo_len);
814 SAVEMEM(lookup->lo_name, lookup->lo_len);
815 if ((status = check_filename(lookup->lo_name, lookup->lo_len)))
821 static __be32 nfsd4_decode_share_access(struct nfsd4_compoundargs *argp, u32 *share_access, u32 *deleg_want, u32 *deleg_when)
827 w = be32_to_cpup(p++);
828 *share_access = w & NFS4_SHARE_ACCESS_MASK;
829 *deleg_want = w & NFS4_SHARE_WANT_MASK;
831 *deleg_when = w & NFS4_SHARE_WHEN_MASK;
833 switch (w & NFS4_SHARE_ACCESS_MASK) {
834 case NFS4_SHARE_ACCESS_READ:
835 case NFS4_SHARE_ACCESS_WRITE:
836 case NFS4_SHARE_ACCESS_BOTH:
839 return nfserr_bad_xdr;
841 w &= ~NFS4_SHARE_ACCESS_MASK;
844 if (!argp->minorversion)
845 return nfserr_bad_xdr;
846 switch (w & NFS4_SHARE_WANT_MASK) {
847 case NFS4_SHARE_WANT_NO_PREFERENCE:
848 case NFS4_SHARE_WANT_READ_DELEG:
849 case NFS4_SHARE_WANT_WRITE_DELEG:
850 case NFS4_SHARE_WANT_ANY_DELEG:
851 case NFS4_SHARE_WANT_NO_DELEG:
852 case NFS4_SHARE_WANT_CANCEL:
855 return nfserr_bad_xdr;
857 w &= ~NFS4_SHARE_WANT_MASK;
861 if (!deleg_when) /* open_downgrade */
864 case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
865 case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
866 case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
867 NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
871 return nfserr_bad_xdr;
874 static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
879 *x = be32_to_cpup(p++);
880 /* Note: unlinke access bits, deny bits may be zero. */
881 if (*x & ~NFS4_SHARE_DENY_BOTH)
882 return nfserr_bad_xdr;
885 return nfserr_bad_xdr;
888 static __be32 nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
893 o->len = be32_to_cpup(p++);
895 if (o->len == 0 || o->len > NFS4_OPAQUE_LIMIT)
896 return nfserr_bad_xdr;
899 SAVEMEM(o->data, o->len);
902 return nfserr_bad_xdr;
906 nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
911 memset(open->op_bmval, 0, sizeof(open->op_bmval));
912 open->op_iattr.ia_valid = 0;
913 open->op_openowner = NULL;
915 open->op_xdr_error = 0;
916 /* seqid, share_access, share_deny, clientid, ownerlen */
918 open->op_seqid = be32_to_cpup(p++);
919 /* decode, yet ignore deleg_when until supported */
920 status = nfsd4_decode_share_access(argp, &open->op_share_access,
921 &open->op_deleg_want, &dummy);
924 status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
927 READ_BUF(sizeof(clientid_t));
928 COPYMEM(&open->op_clientid, sizeof(clientid_t));
929 status = nfsd4_decode_opaque(argp, &open->op_owner);
933 open->op_create = be32_to_cpup(p++);
934 switch (open->op_create) {
935 case NFS4_OPEN_NOCREATE:
937 case NFS4_OPEN_CREATE:
939 open->op_createmode = be32_to_cpup(p++);
940 switch (open->op_createmode) {
941 case NFS4_CREATE_UNCHECKED:
942 case NFS4_CREATE_GUARDED:
943 status = nfsd4_decode_fattr(argp, open->op_bmval,
944 &open->op_iattr, &open->op_acl, &open->op_label,
949 case NFS4_CREATE_EXCLUSIVE:
950 READ_BUF(NFS4_VERIFIER_SIZE);
951 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
953 case NFS4_CREATE_EXCLUSIVE4_1:
954 if (argp->minorversion < 1)
956 READ_BUF(NFS4_VERIFIER_SIZE);
957 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
958 status = nfsd4_decode_fattr(argp, open->op_bmval,
959 &open->op_iattr, &open->op_acl, &open->op_label,
974 open->op_claim_type = be32_to_cpup(p++);
975 switch (open->op_claim_type) {
976 case NFS4_OPEN_CLAIM_NULL:
977 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
979 open->op_fname.len = be32_to_cpup(p++);
980 READ_BUF(open->op_fname.len);
981 SAVEMEM(open->op_fname.data, open->op_fname.len);
982 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
985 case NFS4_OPEN_CLAIM_PREVIOUS:
987 open->op_delegate_type = be32_to_cpup(p++);
989 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
990 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
994 open->op_fname.len = be32_to_cpup(p++);
995 READ_BUF(open->op_fname.len);
996 SAVEMEM(open->op_fname.data, open->op_fname.len);
997 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
1000 case NFS4_OPEN_CLAIM_FH:
1001 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1002 if (argp->minorversion < 1)
1006 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1007 if (argp->minorversion < 1)
1009 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
1021 nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
1025 if (argp->minorversion >= 1)
1026 return nfserr_notsupp;
1028 status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
1032 open_conf->oc_seqid = be32_to_cpup(p++);
1038 nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
1042 status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
1046 open_down->od_seqid = be32_to_cpup(p++);
1047 status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
1048 &open_down->od_deleg_want, NULL);
1051 status = nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
1058 nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
1063 putfh->pf_fhlen = be32_to_cpup(p++);
1064 if (putfh->pf_fhlen > NFS4_FHSIZE)
1066 READ_BUF(putfh->pf_fhlen);
1067 SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
1073 nfsd4_decode_putpubfh(struct nfsd4_compoundargs *argp, void *p)
1075 if (argp->minorversion == 0)
1077 return nfserr_notsupp;
1081 nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
1085 status = nfsd4_decode_stateid(argp, &read->rd_stateid);
1089 p = xdr_decode_hyper(p, &read->rd_offset);
1090 read->rd_length = be32_to_cpup(p++);
1096 nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
1101 p = xdr_decode_hyper(p, &readdir->rd_cookie);
1102 COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
1103 readdir->rd_dircount = be32_to_cpup(p++);
1104 readdir->rd_maxcount = be32_to_cpup(p++);
1105 if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
1112 nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
1117 remove->rm_namelen = be32_to_cpup(p++);
1118 READ_BUF(remove->rm_namelen);
1119 SAVEMEM(remove->rm_name, remove->rm_namelen);
1120 if ((status = check_filename(remove->rm_name, remove->rm_namelen)))
1127 nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
1132 rename->rn_snamelen = be32_to_cpup(p++);
1133 READ_BUF(rename->rn_snamelen);
1134 SAVEMEM(rename->rn_sname, rename->rn_snamelen);
1136 rename->rn_tnamelen = be32_to_cpup(p++);
1137 READ_BUF(rename->rn_tnamelen);
1138 SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
1139 if ((status = check_filename(rename->rn_sname, rename->rn_snamelen)))
1141 if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen)))
1148 nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
1152 if (argp->minorversion >= 1)
1153 return nfserr_notsupp;
1155 READ_BUF(sizeof(clientid_t));
1156 COPYMEM(clientid, sizeof(clientid_t));
1162 nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
1163 struct nfsd4_secinfo *secinfo)
1168 secinfo->si_namelen = be32_to_cpup(p++);
1169 READ_BUF(secinfo->si_namelen);
1170 SAVEMEM(secinfo->si_name, secinfo->si_namelen);
1171 status = check_filename(secinfo->si_name, secinfo->si_namelen);
1178 nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
1179 struct nfsd4_secinfo_no_name *sin)
1184 sin->sin_style = be32_to_cpup(p++);
1189 nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
1193 status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
1196 return nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr,
1197 &setattr->sa_acl, &setattr->sa_label, NULL);
1201 nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
1205 if (argp->minorversion >= 1)
1206 return nfserr_notsupp;
1208 READ_BUF(NFS4_VERIFIER_SIZE);
1209 COPYMEM(setclientid->se_verf.data, NFS4_VERIFIER_SIZE);
1211 status = nfsd4_decode_opaque(argp, &setclientid->se_name);
1213 return nfserr_bad_xdr;
1215 setclientid->se_callback_prog = be32_to_cpup(p++);
1216 setclientid->se_callback_netid_len = be32_to_cpup(p++);
1217 READ_BUF(setclientid->se_callback_netid_len);
1218 SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
1220 setclientid->se_callback_addr_len = be32_to_cpup(p++);
1222 READ_BUF(setclientid->se_callback_addr_len);
1223 SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
1225 setclientid->se_callback_ident = be32_to_cpup(p++);
1231 nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
1235 if (argp->minorversion >= 1)
1236 return nfserr_notsupp;
1238 READ_BUF(8 + NFS4_VERIFIER_SIZE);
1239 COPYMEM(&scd_c->sc_clientid, 8);
1240 COPYMEM(&scd_c->sc_confirm, NFS4_VERIFIER_SIZE);
1245 /* Also used for NVERIFY */
1247 nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1251 if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
1254 /* For convenience's sake, we compare raw xdr'd attributes in
1255 * nfsd4_proc_verify */
1258 verify->ve_attrlen = be32_to_cpup(p++);
1259 READ_BUF(verify->ve_attrlen);
1260 SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
1266 nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1272 status = nfsd4_decode_stateid(argp, &write->wr_stateid);
1276 p = xdr_decode_hyper(p, &write->wr_offset);
1277 write->wr_stable_how = be32_to_cpup(p++);
1278 if (write->wr_stable_how > NFS_FILE_SYNC)
1280 write->wr_buflen = be32_to_cpup(p++);
1282 /* Sorry .. no magic macros for this.. *
1283 * READ_BUF(write->wr_buflen);
1284 * SAVEMEM(write->wr_buf, write->wr_buflen);
1286 avail = (char*)argp->end - (char*)argp->p;
1287 if (avail + argp->pagelen < write->wr_buflen) {
1288 dprintk("NFSD: xdr error (%s:%d)\n",
1289 __FILE__, __LINE__);
1292 write->wr_head.iov_base = p;
1293 write->wr_head.iov_len = avail;
1294 write->wr_pagelist = argp->pagelist;
1296 len = XDR_QUADLEN(write->wr_buflen) << 2;
1302 pages = len >> PAGE_SHIFT;
1303 argp->pagelist += pages;
1304 argp->pagelen -= pages * PAGE_SIZE;
1305 len -= pages * PAGE_SIZE;
1307 next_decode_page(argp);
1309 argp->p += XDR_QUADLEN(len);
1315 nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1319 if (argp->minorversion >= 1)
1320 return nfserr_notsupp;
1323 COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
1324 rlockowner->rl_owner.len = be32_to_cpup(p++);
1325 READ_BUF(rlockowner->rl_owner.len);
1326 READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
1328 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1329 return nfserr_inval;
1334 nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
1335 struct nfsd4_exchange_id *exid)
1340 READ_BUF(NFS4_VERIFIER_SIZE);
1341 COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
1343 status = nfsd4_decode_opaque(argp, &exid->clname);
1345 return nfserr_bad_xdr;
1348 exid->flags = be32_to_cpup(p++);
1350 /* Ignore state_protect4_a */
1352 exid->spa_how = be32_to_cpup(p++);
1353 switch (exid->spa_how) {
1357 /* spo_must_enforce */
1358 status = nfsd4_decode_bitmap(argp,
1359 exid->spo_must_enforce);
1362 /* spo_must_allow */
1363 status = nfsd4_decode_bitmap(argp, exid->spo_must_allow);
1370 dummy = be32_to_cpup(p++);
1371 READ_BUF(dummy * 4);
1375 dummy = be32_to_cpup(p++);
1376 READ_BUF(dummy * 4);
1379 /* ssp_hash_algs<> */
1381 tmp = be32_to_cpup(p++);
1384 dummy = be32_to_cpup(p++);
1386 p += XDR_QUADLEN(dummy);
1389 /* ssp_encr_algs<> */
1391 tmp = be32_to_cpup(p++);
1394 dummy = be32_to_cpup(p++);
1396 p += XDR_QUADLEN(dummy);
1399 /* ignore ssp_window and ssp_num_gss_handles: */
1406 READ_BUF(4); /* nfs_impl_id4 array length */
1407 dummy = be32_to_cpup(p++);
1413 status = nfsd4_decode_opaque(argp, &exid->nii_domain);
1418 status = nfsd4_decode_opaque(argp, &exid->nii_name);
1423 status = nfsd4_decode_time(argp, &exid->nii_time);
1431 nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1432 struct nfsd4_create_session *sess)
1437 COPYMEM(&sess->clientid, 8);
1438 sess->seqid = be32_to_cpup(p++);
1439 sess->flags = be32_to_cpup(p++);
1441 /* Fore channel attrs */
1443 p++; /* headerpadsz is always 0 */
1444 sess->fore_channel.maxreq_sz = be32_to_cpup(p++);
1445 sess->fore_channel.maxresp_sz = be32_to_cpup(p++);
1446 sess->fore_channel.maxresp_cached = be32_to_cpup(p++);
1447 sess->fore_channel.maxops = be32_to_cpup(p++);
1448 sess->fore_channel.maxreqs = be32_to_cpup(p++);
1449 sess->fore_channel.nr_rdma_attrs = be32_to_cpup(p++);
1450 if (sess->fore_channel.nr_rdma_attrs == 1) {
1452 sess->fore_channel.rdma_attrs = be32_to_cpup(p++);
1453 } else if (sess->fore_channel.nr_rdma_attrs > 1) {
1454 dprintk("Too many fore channel attr bitmaps!\n");
1458 /* Back channel attrs */
1460 p++; /* headerpadsz is always 0 */
1461 sess->back_channel.maxreq_sz = be32_to_cpup(p++);
1462 sess->back_channel.maxresp_sz = be32_to_cpup(p++);
1463 sess->back_channel.maxresp_cached = be32_to_cpup(p++);
1464 sess->back_channel.maxops = be32_to_cpup(p++);
1465 sess->back_channel.maxreqs = be32_to_cpup(p++);
1466 sess->back_channel.nr_rdma_attrs = be32_to_cpup(p++);
1467 if (sess->back_channel.nr_rdma_attrs == 1) {
1469 sess->back_channel.rdma_attrs = be32_to_cpup(p++);
1470 } else if (sess->back_channel.nr_rdma_attrs > 1) {
1471 dprintk("Too many back channel attr bitmaps!\n");
1476 sess->callback_prog = be32_to_cpup(p++);
1477 nfsd4_decode_cb_sec(argp, &sess->cb_sec);
1482 nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1483 struct nfsd4_destroy_session *destroy_session)
1486 READ_BUF(NFS4_MAX_SESSIONID_LEN);
1487 COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1493 nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1494 struct nfsd4_free_stateid *free_stateid)
1498 READ_BUF(sizeof(stateid_t));
1499 free_stateid->fr_stateid.si_generation = be32_to_cpup(p++);
1500 COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
1506 nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1507 struct nfsd4_sequence *seq)
1511 READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
1512 COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1513 seq->seqid = be32_to_cpup(p++);
1514 seq->slotid = be32_to_cpup(p++);
1515 seq->maxslots = be32_to_cpup(p++);
1516 seq->cachethis = be32_to_cpup(p++);
1522 nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1526 struct nfsd4_test_stateid_id *stateid;
1529 test_stateid->ts_num_ids = ntohl(*p++);
1531 INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
1533 for (i = 0; i < test_stateid->ts_num_ids; i++) {
1534 stateid = svcxdr_tmpalloc(argp, sizeof(*stateid));
1536 status = nfserrno(-ENOMEM);
1540 INIT_LIST_HEAD(&stateid->ts_id_list);
1541 list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
1543 status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
1552 dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
1553 status = nfserr_bad_xdr;
1557 static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
1562 COPYMEM(&dc->clientid, 8);
1567 static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
1572 rc->rca_one_fs = be32_to_cpup(p++);
1577 #ifdef CONFIG_NFSD_PNFS
1579 nfsd4_decode_getdeviceinfo(struct nfsd4_compoundargs *argp,
1580 struct nfsd4_getdeviceinfo *gdev)
1585 READ_BUF(sizeof(struct nfsd4_deviceid) + 3 * 4);
1586 COPYMEM(&gdev->gd_devid, sizeof(struct nfsd4_deviceid));
1587 gdev->gd_layout_type = be32_to_cpup(p++);
1588 gdev->gd_maxcount = be32_to_cpup(p++);
1589 num = be32_to_cpup(p++);
1594 gdev->gd_notify_types = be32_to_cpup(p++);
1595 for (i = 1; i < num; i++) {
1596 if (be32_to_cpup(p++)) {
1597 status = nfserr_inval;
1606 nfsd4_decode_layoutget(struct nfsd4_compoundargs *argp,
1607 struct nfsd4_layoutget *lgp)
1612 lgp->lg_signal = be32_to_cpup(p++);
1613 lgp->lg_layout_type = be32_to_cpup(p++);
1614 lgp->lg_seg.iomode = be32_to_cpup(p++);
1615 p = xdr_decode_hyper(p, &lgp->lg_seg.offset);
1616 p = xdr_decode_hyper(p, &lgp->lg_seg.length);
1617 p = xdr_decode_hyper(p, &lgp->lg_minlength);
1619 status = nfsd4_decode_stateid(argp, &lgp->lg_sid);
1624 lgp->lg_maxcount = be32_to_cpup(p++);
1630 nfsd4_decode_layoutcommit(struct nfsd4_compoundargs *argp,
1631 struct nfsd4_layoutcommit *lcp)
1637 p = xdr_decode_hyper(p, &lcp->lc_seg.offset);
1638 p = xdr_decode_hyper(p, &lcp->lc_seg.length);
1639 lcp->lc_reclaim = be32_to_cpup(p++);
1641 status = nfsd4_decode_stateid(argp, &lcp->lc_sid);
1646 lcp->lc_newoffset = be32_to_cpup(p++);
1647 if (lcp->lc_newoffset) {
1649 p = xdr_decode_hyper(p, &lcp->lc_last_wr);
1651 lcp->lc_last_wr = 0;
1653 timechange = be32_to_cpup(p++);
1655 status = nfsd4_decode_time(argp, &lcp->lc_mtime);
1659 lcp->lc_mtime.tv_nsec = UTIME_NOW;
1662 lcp->lc_layout_type = be32_to_cpup(p++);
1665 * Save the layout update in XDR format and let the layout driver deal
1668 lcp->lc_up_len = be32_to_cpup(p++);
1669 if (lcp->lc_up_len > 0) {
1670 READ_BUF(lcp->lc_up_len);
1671 READMEM(lcp->lc_up_layout, lcp->lc_up_len);
1678 nfsd4_decode_layoutreturn(struct nfsd4_compoundargs *argp,
1679 struct nfsd4_layoutreturn *lrp)
1684 lrp->lr_reclaim = be32_to_cpup(p++);
1685 lrp->lr_layout_type = be32_to_cpup(p++);
1686 lrp->lr_seg.iomode = be32_to_cpup(p++);
1687 lrp->lr_return_type = be32_to_cpup(p++);
1688 if (lrp->lr_return_type == RETURN_FILE) {
1690 p = xdr_decode_hyper(p, &lrp->lr_seg.offset);
1691 p = xdr_decode_hyper(p, &lrp->lr_seg.length);
1693 status = nfsd4_decode_stateid(argp, &lrp->lr_sid);
1698 lrp->lrf_body_len = be32_to_cpup(p++);
1699 if (lrp->lrf_body_len > 0) {
1700 READ_BUF(lrp->lrf_body_len);
1701 READMEM(lrp->lrf_body, lrp->lrf_body_len);
1704 lrp->lr_seg.offset = 0;
1705 lrp->lr_seg.length = NFS4_MAX_UINT64;
1710 #endif /* CONFIG_NFSD_PNFS */
1713 nfsd4_decode_fallocate(struct nfsd4_compoundargs *argp,
1714 struct nfsd4_fallocate *fallocate)
1718 status = nfsd4_decode_stateid(argp, &fallocate->falloc_stateid);
1723 p = xdr_decode_hyper(p, &fallocate->falloc_offset);
1724 xdr_decode_hyper(p, &fallocate->falloc_length);
1730 nfsd4_decode_clone(struct nfsd4_compoundargs *argp, struct nfsd4_clone *clone)
1734 status = nfsd4_decode_stateid(argp, &clone->cl_src_stateid);
1737 status = nfsd4_decode_stateid(argp, &clone->cl_dst_stateid);
1741 READ_BUF(8 + 8 + 8);
1742 p = xdr_decode_hyper(p, &clone->cl_src_pos);
1743 p = xdr_decode_hyper(p, &clone->cl_dst_pos);
1744 p = xdr_decode_hyper(p, &clone->cl_count);
1748 static __be32 nfsd4_decode_nl4_server(struct nfsd4_compoundargs *argp,
1749 struct nl4_server *ns)
1752 struct nfs42_netaddr *naddr;
1755 ns->nl4_type = be32_to_cpup(p++);
1757 /* currently support for 1 inter-server source server */
1758 switch (ns->nl4_type) {
1760 naddr = &ns->u.nl4_addr;
1763 naddr->netid_len = be32_to_cpup(p++);
1764 if (naddr->netid_len > RPCBIND_MAXNETIDLEN)
1767 READ_BUF(naddr->netid_len + 4); /* 4 for uaddr len */
1768 COPYMEM(naddr->netid, naddr->netid_len);
1770 naddr->addr_len = be32_to_cpup(p++);
1771 if (naddr->addr_len > RPCBIND_MAXUADDRLEN)
1774 READ_BUF(naddr->addr_len);
1775 COPYMEM(naddr->addr, naddr->addr_len);
1784 nfsd4_decode_copy(struct nfsd4_compoundargs *argp, struct nfsd4_copy *copy)
1787 struct nl4_server *ns_dummy;
1790 status = nfsd4_decode_stateid(argp, ©->cp_src_stateid);
1793 status = nfsd4_decode_stateid(argp, ©->cp_dst_stateid);
1797 READ_BUF(8 + 8 + 8 + 4 + 4 + 4);
1798 p = xdr_decode_hyper(p, ©->cp_src_pos);
1799 p = xdr_decode_hyper(p, ©->cp_dst_pos);
1800 p = xdr_decode_hyper(p, ©->cp_count);
1801 p++; /* ca_consecutive: we always do consecutive copies */
1802 copy->cp_synchronous = be32_to_cpup(p++);
1804 count = be32_to_cpup(p++);
1806 copy->cp_intra = false;
1807 if (count == 0) { /* intra-server copy */
1808 copy->cp_intra = true;
1812 /* decode all the supplied server addresses but use first */
1813 status = nfsd4_decode_nl4_server(argp, ©->cp_src);
1817 ns_dummy = kmalloc(sizeof(struct nl4_server), GFP_KERNEL);
1818 if (ns_dummy == NULL)
1819 return nfserrno(-ENOMEM);
1820 for (i = 0; i < count - 1; i++) {
1821 status = nfsd4_decode_nl4_server(argp, ns_dummy);
1834 nfsd4_decode_offload_status(struct nfsd4_compoundargs *argp,
1835 struct nfsd4_offload_status *os)
1837 return nfsd4_decode_stateid(argp, &os->stateid);
1841 nfsd4_decode_copy_notify(struct nfsd4_compoundargs *argp,
1842 struct nfsd4_copy_notify *cn)
1846 status = nfsd4_decode_stateid(argp, &cn->cpn_src_stateid);
1849 return nfsd4_decode_nl4_server(argp, &cn->cpn_dst);
1853 nfsd4_decode_seek(struct nfsd4_compoundargs *argp, struct nfsd4_seek *seek)
1857 status = nfsd4_decode_stateid(argp, &seek->seek_stateid);
1862 p = xdr_decode_hyper(p, &seek->seek_offset);
1863 seek->seek_whence = be32_to_cpup(p);
1869 nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
1875 nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
1877 return nfserr_notsupp;
1880 typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
1882 static const nfsd4_dec nfsd4_dec_ops[] = {
1883 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1884 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1885 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1886 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1887 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1888 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1889 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1890 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1891 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1892 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1893 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1894 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1895 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1896 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1897 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1898 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1899 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1900 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
1901 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1902 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
1903 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_putpubfh,
1904 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1905 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1906 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1907 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1908 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1909 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1910 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
1911 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1912 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1913 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1914 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1915 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
1916 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
1917 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1918 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1919 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
1921 /* new operations for NFSv4.1 */
1922 [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
1923 [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
1924 [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
1925 [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
1926 [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
1927 [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
1928 [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
1929 #ifdef CONFIG_NFSD_PNFS
1930 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_getdeviceinfo,
1931 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
1932 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_layoutcommit,
1933 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_layoutget,
1934 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_layoutreturn,
1936 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
1937 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
1938 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
1939 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
1940 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
1942 [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
1943 [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
1944 [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
1945 [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
1946 [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
1947 [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
1948 [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
1950 /* new operations for NFSv4.2 */
1951 [OP_ALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
1952 [OP_COPY] = (nfsd4_dec)nfsd4_decode_copy,
1953 [OP_COPY_NOTIFY] = (nfsd4_dec)nfsd4_decode_copy_notify,
1954 [OP_DEALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
1955 [OP_IO_ADVISE] = (nfsd4_dec)nfsd4_decode_notsupp,
1956 [OP_LAYOUTERROR] = (nfsd4_dec)nfsd4_decode_notsupp,
1957 [OP_LAYOUTSTATS] = (nfsd4_dec)nfsd4_decode_notsupp,
1958 [OP_OFFLOAD_CANCEL] = (nfsd4_dec)nfsd4_decode_offload_status,
1959 [OP_OFFLOAD_STATUS] = (nfsd4_dec)nfsd4_decode_offload_status,
1960 [OP_READ_PLUS] = (nfsd4_dec)nfsd4_decode_notsupp,
1961 [OP_SEEK] = (nfsd4_dec)nfsd4_decode_seek,
1962 [OP_WRITE_SAME] = (nfsd4_dec)nfsd4_decode_notsupp,
1963 [OP_CLONE] = (nfsd4_dec)nfsd4_decode_clone,
1967 nfsd4_opnum_in_range(struct nfsd4_compoundargs *argp, struct nfsd4_op *op)
1969 if (op->opnum < FIRST_NFS4_OP)
1971 else if (argp->minorversion == 0 && op->opnum > LAST_NFS40_OP)
1973 else if (argp->minorversion == 1 && op->opnum > LAST_NFS41_OP)
1975 else if (argp->minorversion == 2 && op->opnum > LAST_NFS42_OP)
1981 nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
1984 struct nfsd4_op *op;
1985 bool cachethis = false;
1986 int auth_slack= argp->rqstp->rq_auth_slack;
1987 int max_reply = auth_slack + 8; /* opcnt, status */
1993 argp->taglen = be32_to_cpup(p++);
1994 READ_BUF(argp->taglen);
1995 SAVEMEM(argp->tag, argp->taglen);
1997 argp->minorversion = be32_to_cpup(p++);
1998 argp->opcnt = be32_to_cpup(p++);
1999 max_reply += 4 + (XDR_QUADLEN(argp->taglen) << 2);
2001 if (argp->taglen > NFSD4_MAX_TAGLEN)
2004 * NFS4ERR_RESOURCE is a more helpful error than GARBAGE_ARGS
2005 * here, so we return success at the xdr level so that
2006 * nfsd4_proc can handle this is an NFS-level error.
2008 if (argp->opcnt > NFSD_MAX_OPS_PER_COMPOUND)
2011 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
2012 argp->ops = kzalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
2014 argp->ops = argp->iops;
2015 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
2020 if (argp->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
2023 for (i = 0; i < argp->opcnt; i++) {
2028 op->opnum = be32_to_cpup(p++);
2030 if (nfsd4_opnum_in_range(argp, op))
2031 op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
2033 op->opnum = OP_ILLEGAL;
2034 op->status = nfserr_op_illegal;
2036 op->opdesc = OPDESC(op);
2038 * We'll try to cache the result in the DRC if any one
2039 * op in the compound wants to be cached:
2041 cachethis |= nfsd4_cache_this_op(op);
2043 if (op->opnum == OP_READ) {
2045 readbytes += nfsd4_max_reply(argp->rqstp, op);
2047 max_reply += nfsd4_max_reply(argp->rqstp, op);
2049 * OP_LOCK and OP_LOCKT may return a conflicting lock.
2050 * (Special case because it will just skip encoding this
2051 * if it runs out of xdr buffer space, and it is the only
2052 * operation that behaves this way.)
2054 if (op->opnum == OP_LOCK || op->opnum == OP_LOCKT)
2055 max_reply += NFS4_OPAQUE_LIMIT;
2062 /* Sessions make the DRC unnecessary: */
2063 if (argp->minorversion)
2065 svc_reserve(argp->rqstp, max_reply + readbytes);
2066 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
2068 if (readcount > 1 || max_reply > PAGE_SIZE - auth_slack)
2069 clear_bit(RQ_SPLICE_OK, &argp->rqstp->rq_flags);
2074 static __be32 *encode_change(__be32 *p, struct kstat *stat, struct inode *inode,
2075 struct svc_export *exp)
2077 if (exp->ex_flags & NFSEXP_V4ROOT) {
2078 *p++ = cpu_to_be32(convert_to_wallclock(exp->cd->flush_time));
2080 } else if (IS_I_VERSION(inode)) {
2081 p = xdr_encode_hyper(p, nfsd4_change_attribute(stat, inode));
2083 *p++ = cpu_to_be32(stat->ctime.tv_sec);
2084 *p++ = cpu_to_be32(stat->ctime.tv_nsec);
2090 * ctime (in NFSv4, time_metadata) is not writeable, and the client
2091 * doesn't really care what resolution could theoretically be stored by
2094 * The client cares how close together changes can be while still
2095 * guaranteeing ctime changes. For most filesystems (which have
2096 * timestamps with nanosecond fields) that is limited by the resolution
2097 * of the time returned from current_time() (which I'm assuming to be
2100 static __be32 *encode_time_delta(__be32 *p, struct inode *inode)
2102 struct timespec64 ts;
2105 ns = max_t(u32, NSEC_PER_SEC/HZ, inode->i_sb->s_time_gran);
2106 ts = ns_to_timespec64(ns);
2108 p = xdr_encode_hyper(p, ts.tv_sec);
2109 *p++ = cpu_to_be32(ts.tv_nsec);
2114 static __be32 *encode_cinfo(__be32 *p, struct nfsd4_change_info *c)
2116 *p++ = cpu_to_be32(c->atomic);
2117 if (c->change_supported) {
2118 p = xdr_encode_hyper(p, c->before_change);
2119 p = xdr_encode_hyper(p, c->after_change);
2121 *p++ = cpu_to_be32(c->before_ctime_sec);
2122 *p++ = cpu_to_be32(c->before_ctime_nsec);
2123 *p++ = cpu_to_be32(c->after_ctime_sec);
2124 *p++ = cpu_to_be32(c->after_ctime_nsec);
2129 /* Encode as an array of strings the string given with components
2130 * separated @sep, escaped with esc_enter and esc_exit.
2132 static __be32 nfsd4_encode_components_esc(struct xdr_stream *xdr, char sep,
2133 char *components, char esc_enter,
2139 int strlen, count=0;
2140 char *str, *end, *next;
2142 dprintk("nfsd4_encode_components(%s)\n", components);
2144 pathlen_offset = xdr->buf->len;
2145 p = xdr_reserve_space(xdr, 4);
2147 return nfserr_resource;
2148 p++; /* We will fill this in with @count later */
2150 end = str = components;
2152 bool found_esc = false;
2154 /* try to parse as esc_start, ..., esc_end, sep */
2155 if (*str == esc_enter) {
2156 for (; *end && (*end != esc_exit); end++)
2157 /* find esc_exit or end of string */;
2159 if (*end && (!*next || *next == sep)) {
2166 for (; *end && (*end != sep); end++)
2167 /* find sep or end of string */;
2171 p = xdr_reserve_space(xdr, strlen + 4);
2173 return nfserr_resource;
2174 p = xdr_encode_opaque(p, str, strlen);
2184 pathlen = htonl(count);
2185 write_bytes_to_xdr_buf(xdr->buf, pathlen_offset, &pathlen, 4);
2189 /* Encode as an array of strings the string given with components
2192 static __be32 nfsd4_encode_components(struct xdr_stream *xdr, char sep,
2195 return nfsd4_encode_components_esc(xdr, sep, components, 0, 0);
2199 * encode a location element of a fs_locations structure
2201 static __be32 nfsd4_encode_fs_location4(struct xdr_stream *xdr,
2202 struct nfsd4_fs_location *location)
2206 status = nfsd4_encode_components_esc(xdr, ':', location->hosts,
2210 status = nfsd4_encode_components(xdr, '/', location->path);
2217 * Encode a path in RFC3530 'pathname4' format
2219 static __be32 nfsd4_encode_path(struct xdr_stream *xdr,
2220 const struct path *root,
2221 const struct path *path)
2223 struct path cur = *path;
2225 struct dentry **components = NULL;
2226 unsigned int ncomponents = 0;
2227 __be32 err = nfserr_jukebox;
2229 dprintk("nfsd4_encode_components(");
2232 /* First walk the path up to the nfsd root, and store the
2233 * dentries/path components in an array.
2236 if (path_equal(&cur, root))
2238 if (cur.dentry == cur.mnt->mnt_root) {
2239 if (follow_up(&cur))
2243 if ((ncomponents & 15) == 0) {
2244 struct dentry **new;
2245 new = krealloc(components,
2246 sizeof(*new) * (ncomponents + 16),
2252 components[ncomponents++] = cur.dentry;
2253 cur.dentry = dget_parent(cur.dentry);
2255 err = nfserr_resource;
2256 p = xdr_reserve_space(xdr, 4);
2259 *p++ = cpu_to_be32(ncomponents);
2261 while (ncomponents) {
2262 struct dentry *dentry = components[ncomponents - 1];
2265 spin_lock(&dentry->d_lock);
2266 len = dentry->d_name.len;
2267 p = xdr_reserve_space(xdr, len + 4);
2269 spin_unlock(&dentry->d_lock);
2272 p = xdr_encode_opaque(p, dentry->d_name.name, len);
2273 dprintk("/%pd", dentry);
2274 spin_unlock(&dentry->d_lock);
2283 dput(components[--ncomponents]);
2289 static __be32 nfsd4_encode_fsloc_fsroot(struct xdr_stream *xdr,
2290 struct svc_rqst *rqstp, const struct path *path)
2292 struct svc_export *exp_ps;
2295 exp_ps = rqst_find_fsidzero_export(rqstp);
2297 return nfserrno(PTR_ERR(exp_ps));
2298 res = nfsd4_encode_path(xdr, &exp_ps->ex_path, path);
2304 * encode a fs_locations structure
2306 static __be32 nfsd4_encode_fs_locations(struct xdr_stream *xdr,
2307 struct svc_rqst *rqstp, struct svc_export *exp)
2312 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
2314 status = nfsd4_encode_fsloc_fsroot(xdr, rqstp, &exp->ex_path);
2317 p = xdr_reserve_space(xdr, 4);
2319 return nfserr_resource;
2320 *p++ = cpu_to_be32(fslocs->locations_count);
2321 for (i=0; i<fslocs->locations_count; i++) {
2322 status = nfsd4_encode_fs_location4(xdr, &fslocs->locations[i]);
2329 static u32 nfs4_file_type(umode_t mode)
2331 switch (mode & S_IFMT) {
2332 case S_IFIFO: return NF4FIFO;
2333 case S_IFCHR: return NF4CHR;
2334 case S_IFDIR: return NF4DIR;
2335 case S_IFBLK: return NF4BLK;
2336 case S_IFLNK: return NF4LNK;
2337 case S_IFREG: return NF4REG;
2338 case S_IFSOCK: return NF4SOCK;
2339 default: return NF4BAD;
2343 static inline __be32
2344 nfsd4_encode_aclname(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2345 struct nfs4_ace *ace)
2347 if (ace->whotype != NFS4_ACL_WHO_NAMED)
2348 return nfs4_acl_write_who(xdr, ace->whotype);
2349 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
2350 return nfsd4_encode_group(xdr, rqstp, ace->who_gid);
2352 return nfsd4_encode_user(xdr, rqstp, ace->who_uid);
2355 static inline __be32
2356 nfsd4_encode_layout_types(struct xdr_stream *xdr, u32 layout_types)
2359 unsigned long i = hweight_long(layout_types);
2361 p = xdr_reserve_space(xdr, 4 + 4 * i);
2363 return nfserr_resource;
2365 *p++ = cpu_to_be32(i);
2367 for (i = LAYOUT_NFSV4_1_FILES; i < LAYOUT_TYPE_MAX; ++i)
2368 if (layout_types & (1 << i))
2369 *p++ = cpu_to_be32(i);
2374 #define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
2375 FATTR4_WORD0_RDATTR_ERROR)
2376 #define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
2377 #define WORD2_ABSENT_FS_ATTRS 0
2379 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2380 static inline __be32
2381 nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2382 void *context, int len)
2386 p = xdr_reserve_space(xdr, len + 4 + 4 + 4);
2388 return nfserr_resource;
2391 * For now we use a 0 here to indicate the null translation; in
2392 * the future we may place a call to translation code here.
2394 *p++ = cpu_to_be32(0); /* lfs */
2395 *p++ = cpu_to_be32(0); /* pi */
2396 p = xdr_encode_opaque(p, context, len);
2400 static inline __be32
2401 nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
2402 void *context, int len)
2406 static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *bmval2, u32 *rdattr_err)
2408 /* As per referral draft: */
2409 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
2410 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
2411 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
2412 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
2413 *rdattr_err = NFSERR_MOVED;
2415 return nfserr_moved;
2417 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
2418 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
2419 *bmval2 &= WORD2_ABSENT_FS_ATTRS;
2424 static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
2426 struct path path = exp->ex_path;
2430 while (follow_up(&path)) {
2431 if (path.dentry != path.mnt->mnt_root)
2434 err = vfs_getattr(&path, stat, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
2440 nfsd4_encode_bitmap(struct xdr_stream *xdr, u32 bmval0, u32 bmval1, u32 bmval2)
2445 p = xdr_reserve_space(xdr, 16);
2448 *p++ = cpu_to_be32(3);
2449 *p++ = cpu_to_be32(bmval0);
2450 *p++ = cpu_to_be32(bmval1);
2451 *p++ = cpu_to_be32(bmval2);
2452 } else if (bmval1) {
2453 p = xdr_reserve_space(xdr, 12);
2456 *p++ = cpu_to_be32(2);
2457 *p++ = cpu_to_be32(bmval0);
2458 *p++ = cpu_to_be32(bmval1);
2460 p = xdr_reserve_space(xdr, 8);
2463 *p++ = cpu_to_be32(1);
2464 *p++ = cpu_to_be32(bmval0);
2469 return nfserr_resource;
2473 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
2477 nfsd4_encode_fattr(struct xdr_stream *xdr, struct svc_fh *fhp,
2478 struct svc_export *exp,
2479 struct dentry *dentry, u32 *bmval,
2480 struct svc_rqst *rqstp, int ignore_crossmnt)
2482 u32 bmval0 = bmval[0];
2483 u32 bmval1 = bmval[1];
2484 u32 bmval2 = bmval[2];
2486 struct svc_fh *tempfh = NULL;
2487 struct kstatfs statfs;
2489 int starting_len = xdr->buf->len;
2497 struct nfs4_acl *acl = NULL;
2498 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2499 void *context = NULL;
2502 bool contextsupport = false;
2503 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2504 u32 minorversion = resp->cstate.minorversion;
2505 struct path path = {
2506 .mnt = exp->ex_path.mnt,
2509 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2511 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
2512 BUG_ON(!nfsd_attrs_supported(minorversion, bmval));
2514 if (exp->ex_fslocs.migrated) {
2515 status = fattr_handle_absent_fs(&bmval0, &bmval1, &bmval2, &rdattr_err);
2520 err = vfs_getattr(&path, &stat, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
2523 if ((bmval0 & (FATTR4_WORD0_FILES_AVAIL | FATTR4_WORD0_FILES_FREE |
2524 FATTR4_WORD0_FILES_TOTAL | FATTR4_WORD0_MAXNAME)) ||
2525 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2526 FATTR4_WORD1_SPACE_TOTAL))) {
2527 err = vfs_statfs(&path, &statfs);
2531 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
2532 tempfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
2533 status = nfserr_jukebox;
2536 fh_init(tempfh, NFS4_FHSIZE);
2537 status = fh_compose(tempfh, exp, dentry, NULL);
2542 if (bmval0 & FATTR4_WORD0_ACL) {
2543 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
2544 if (err == -EOPNOTSUPP)
2545 bmval0 &= ~FATTR4_WORD0_ACL;
2546 else if (err == -EINVAL) {
2547 status = nfserr_attrnotsupp;
2549 } else if (err != 0)
2553 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2554 if ((bmval2 & FATTR4_WORD2_SECURITY_LABEL) ||
2555 bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
2556 if (exp->ex_flags & NFSEXP_SECURITY_LABEL)
2557 err = security_inode_getsecctx(d_inode(dentry),
2558 &context, &contextlen);
2561 contextsupport = (err == 0);
2562 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2563 if (err == -EOPNOTSUPP)
2564 bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2569 #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2571 status = nfsd4_encode_bitmap(xdr, bmval0, bmval1, bmval2);
2575 attrlen_offset = xdr->buf->len;
2576 p = xdr_reserve_space(xdr, 4);
2579 p++; /* to be backfilled later */
2581 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
2584 memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
2586 if (!IS_POSIXACL(dentry->d_inode))
2587 supp[0] &= ~FATTR4_WORD0_ACL;
2588 if (!contextsupport)
2589 supp[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2591 p = xdr_reserve_space(xdr, 12);
2594 *p++ = cpu_to_be32(2);
2595 *p++ = cpu_to_be32(supp[0]);
2596 *p++ = cpu_to_be32(supp[1]);
2598 p = xdr_reserve_space(xdr, 16);
2601 *p++ = cpu_to_be32(3);
2602 *p++ = cpu_to_be32(supp[0]);
2603 *p++ = cpu_to_be32(supp[1]);
2604 *p++ = cpu_to_be32(supp[2]);
2607 if (bmval0 & FATTR4_WORD0_TYPE) {
2608 p = xdr_reserve_space(xdr, 4);
2611 dummy = nfs4_file_type(stat.mode);
2612 if (dummy == NF4BAD) {
2613 status = nfserr_serverfault;
2616 *p++ = cpu_to_be32(dummy);
2618 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
2619 p = xdr_reserve_space(xdr, 4);
2622 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
2623 *p++ = cpu_to_be32(NFS4_FH_PERSISTENT);
2625 *p++ = cpu_to_be32(NFS4_FH_PERSISTENT|
2626 NFS4_FH_VOL_RENAME);
2628 if (bmval0 & FATTR4_WORD0_CHANGE) {
2629 p = xdr_reserve_space(xdr, 8);
2632 p = encode_change(p, &stat, d_inode(dentry), exp);
2634 if (bmval0 & FATTR4_WORD0_SIZE) {
2635 p = xdr_reserve_space(xdr, 8);
2638 p = xdr_encode_hyper(p, stat.size);
2640 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
2641 p = xdr_reserve_space(xdr, 4);
2644 *p++ = cpu_to_be32(1);
2646 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
2647 p = xdr_reserve_space(xdr, 4);
2650 *p++ = cpu_to_be32(1);
2652 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
2653 p = xdr_reserve_space(xdr, 4);
2656 *p++ = cpu_to_be32(0);
2658 if (bmval0 & FATTR4_WORD0_FSID) {
2659 p = xdr_reserve_space(xdr, 16);
2662 if (exp->ex_fslocs.migrated) {
2663 p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MAJOR);
2664 p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MINOR);
2665 } else switch(fsid_source(fhp)) {
2666 case FSIDSOURCE_FSID:
2667 p = xdr_encode_hyper(p, (u64)exp->ex_fsid);
2668 p = xdr_encode_hyper(p, (u64)0);
2670 case FSIDSOURCE_DEV:
2671 *p++ = cpu_to_be32(0);
2672 *p++ = cpu_to_be32(MAJOR(stat.dev));
2673 *p++ = cpu_to_be32(0);
2674 *p++ = cpu_to_be32(MINOR(stat.dev));
2676 case FSIDSOURCE_UUID:
2677 p = xdr_encode_opaque_fixed(p, exp->ex_uuid,
2682 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
2683 p = xdr_reserve_space(xdr, 4);
2686 *p++ = cpu_to_be32(0);
2688 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
2689 p = xdr_reserve_space(xdr, 4);
2692 *p++ = cpu_to_be32(nn->nfsd4_lease);
2694 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
2695 p = xdr_reserve_space(xdr, 4);
2698 *p++ = cpu_to_be32(rdattr_err);
2700 if (bmval0 & FATTR4_WORD0_ACL) {
2701 struct nfs4_ace *ace;
2704 p = xdr_reserve_space(xdr, 4);
2708 *p++ = cpu_to_be32(0);
2711 p = xdr_reserve_space(xdr, 4);
2714 *p++ = cpu_to_be32(acl->naces);
2716 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
2717 p = xdr_reserve_space(xdr, 4*3);
2720 *p++ = cpu_to_be32(ace->type);
2721 *p++ = cpu_to_be32(ace->flag);
2722 *p++ = cpu_to_be32(ace->access_mask &
2724 status = nfsd4_encode_aclname(xdr, rqstp, ace);
2730 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
2731 p = xdr_reserve_space(xdr, 4);
2734 *p++ = cpu_to_be32(IS_POSIXACL(dentry->d_inode) ?
2735 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
2737 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
2738 p = xdr_reserve_space(xdr, 4);
2741 *p++ = cpu_to_be32(1);
2743 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
2744 p = xdr_reserve_space(xdr, 4);
2747 *p++ = cpu_to_be32(0);
2749 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
2750 p = xdr_reserve_space(xdr, 4);
2753 *p++ = cpu_to_be32(1);
2755 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
2756 p = xdr_reserve_space(xdr, 4);
2759 *p++ = cpu_to_be32(1);
2761 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
2762 p = xdr_reserve_space(xdr, fhp->fh_handle.fh_size + 4);
2765 p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base,
2766 fhp->fh_handle.fh_size);
2768 if (bmval0 & FATTR4_WORD0_FILEID) {
2769 p = xdr_reserve_space(xdr, 8);
2772 p = xdr_encode_hyper(p, stat.ino);
2774 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
2775 p = xdr_reserve_space(xdr, 8);
2778 p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
2780 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
2781 p = xdr_reserve_space(xdr, 8);
2784 p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
2786 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
2787 p = xdr_reserve_space(xdr, 8);
2790 p = xdr_encode_hyper(p, (u64) statfs.f_files);
2792 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
2793 status = nfsd4_encode_fs_locations(xdr, rqstp, exp);
2797 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
2798 p = xdr_reserve_space(xdr, 4);
2801 *p++ = cpu_to_be32(1);
2803 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
2804 p = xdr_reserve_space(xdr, 8);
2807 p = xdr_encode_hyper(p, exp->ex_path.mnt->mnt_sb->s_maxbytes);
2809 if (bmval0 & FATTR4_WORD0_MAXLINK) {
2810 p = xdr_reserve_space(xdr, 4);
2813 *p++ = cpu_to_be32(255);
2815 if (bmval0 & FATTR4_WORD0_MAXNAME) {
2816 p = xdr_reserve_space(xdr, 4);
2819 *p++ = cpu_to_be32(statfs.f_namelen);
2821 if (bmval0 & FATTR4_WORD0_MAXREAD) {
2822 p = xdr_reserve_space(xdr, 8);
2825 p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
2827 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
2828 p = xdr_reserve_space(xdr, 8);
2831 p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
2833 if (bmval1 & FATTR4_WORD1_MODE) {
2834 p = xdr_reserve_space(xdr, 4);
2837 *p++ = cpu_to_be32(stat.mode & S_IALLUGO);
2839 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
2840 p = xdr_reserve_space(xdr, 4);
2843 *p++ = cpu_to_be32(1);
2845 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
2846 p = xdr_reserve_space(xdr, 4);
2849 *p++ = cpu_to_be32(stat.nlink);
2851 if (bmval1 & FATTR4_WORD1_OWNER) {
2852 status = nfsd4_encode_user(xdr, rqstp, stat.uid);
2856 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
2857 status = nfsd4_encode_group(xdr, rqstp, stat.gid);
2861 if (bmval1 & FATTR4_WORD1_RAWDEV) {
2862 p = xdr_reserve_space(xdr, 8);
2865 *p++ = cpu_to_be32((u32) MAJOR(stat.rdev));
2866 *p++ = cpu_to_be32((u32) MINOR(stat.rdev));
2868 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
2869 p = xdr_reserve_space(xdr, 8);
2872 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
2873 p = xdr_encode_hyper(p, dummy64);
2875 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
2876 p = xdr_reserve_space(xdr, 8);
2879 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
2880 p = xdr_encode_hyper(p, dummy64);
2882 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
2883 p = xdr_reserve_space(xdr, 8);
2886 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
2887 p = xdr_encode_hyper(p, dummy64);
2889 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
2890 p = xdr_reserve_space(xdr, 8);
2893 dummy64 = (u64)stat.blocks << 9;
2894 p = xdr_encode_hyper(p, dummy64);
2896 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
2897 p = xdr_reserve_space(xdr, 12);
2900 p = xdr_encode_hyper(p, (s64)stat.atime.tv_sec);
2901 *p++ = cpu_to_be32(stat.atime.tv_nsec);
2903 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
2904 p = xdr_reserve_space(xdr, 12);
2907 p = encode_time_delta(p, d_inode(dentry));
2909 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
2910 p = xdr_reserve_space(xdr, 12);
2913 p = xdr_encode_hyper(p, (s64)stat.ctime.tv_sec);
2914 *p++ = cpu_to_be32(stat.ctime.tv_nsec);
2916 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
2917 p = xdr_reserve_space(xdr, 12);
2920 p = xdr_encode_hyper(p, (s64)stat.mtime.tv_sec);
2921 *p++ = cpu_to_be32(stat.mtime.tv_nsec);
2923 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
2924 struct kstat parent_stat;
2927 p = xdr_reserve_space(xdr, 8);
2931 * Get parent's attributes if not ignoring crossmount
2932 * and this is the root of a cross-mounted filesystem.
2934 if (ignore_crossmnt == 0 &&
2935 dentry == exp->ex_path.mnt->mnt_root) {
2936 err = get_parent_attributes(exp, &parent_stat);
2939 ino = parent_stat.ino;
2941 p = xdr_encode_hyper(p, ino);
2943 #ifdef CONFIG_NFSD_PNFS
2944 if (bmval1 & FATTR4_WORD1_FS_LAYOUT_TYPES) {
2945 status = nfsd4_encode_layout_types(xdr, exp->ex_layout_types);
2950 if (bmval2 & FATTR4_WORD2_LAYOUT_TYPES) {
2951 status = nfsd4_encode_layout_types(xdr, exp->ex_layout_types);
2956 if (bmval2 & FATTR4_WORD2_LAYOUT_BLKSIZE) {
2957 p = xdr_reserve_space(xdr, 4);
2960 *p++ = cpu_to_be32(stat.blksize);
2962 #endif /* CONFIG_NFSD_PNFS */
2963 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
2966 memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
2967 supp[0] &= NFSD_SUPPATTR_EXCLCREAT_WORD0;
2968 supp[1] &= NFSD_SUPPATTR_EXCLCREAT_WORD1;
2969 supp[2] &= NFSD_SUPPATTR_EXCLCREAT_WORD2;
2971 status = nfsd4_encode_bitmap(xdr, supp[0], supp[1], supp[2]);
2976 if (bmval2 & FATTR4_WORD2_CHANGE_ATTR_TYPE) {
2977 p = xdr_reserve_space(xdr, 4);
2980 if (IS_I_VERSION(d_inode(dentry)))
2981 *p++ = cpu_to_be32(NFS4_CHANGE_TYPE_IS_MONOTONIC_INCR);
2983 *p++ = cpu_to_be32(NFS4_CHANGE_TYPE_IS_TIME_METADATA);
2986 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2987 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2988 status = nfsd4_encode_security_label(xdr, rqstp, context,
2995 attrlen = htonl(xdr->buf->len - attrlen_offset - 4);
2996 write_bytes_to_xdr_buf(xdr->buf, attrlen_offset, &attrlen, 4);
3000 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
3002 security_release_secctx(context, contextlen);
3003 #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
3010 xdr_truncate_encode(xdr, starting_len);
3013 status = nfserrno(err);
3016 status = nfserr_resource;
3020 static void svcxdr_init_encode_from_buffer(struct xdr_stream *xdr,
3021 struct xdr_buf *buf, __be32 *p, int bytes)
3023 xdr->scratch.iov_len = 0;
3024 memset(buf, 0, sizeof(struct xdr_buf));
3025 buf->head[0].iov_base = p;
3026 buf->head[0].iov_len = 0;
3029 xdr->iov = buf->head;
3031 xdr->end = (void *)p + bytes;
3032 buf->buflen = bytes;
3035 __be32 nfsd4_encode_fattr_to_buf(__be32 **p, int words,
3036 struct svc_fh *fhp, struct svc_export *exp,
3037 struct dentry *dentry, u32 *bmval,
3038 struct svc_rqst *rqstp, int ignore_crossmnt)
3040 struct xdr_buf dummy;
3041 struct xdr_stream xdr;
3044 svcxdr_init_encode_from_buffer(&xdr, &dummy, *p, words << 2);
3045 ret = nfsd4_encode_fattr(&xdr, fhp, exp, dentry, bmval, rqstp,
3051 static inline int attributes_need_mount(u32 *bmval)
3053 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
3055 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
3061 nfsd4_encode_dirent_fattr(struct xdr_stream *xdr, struct nfsd4_readdir *cd,
3062 const char *name, int namlen)
3064 struct svc_export *exp = cd->rd_fhp->fh_export;
3065 struct dentry *dentry;
3067 int ignore_crossmnt = 0;
3069 dentry = lookup_positive_unlocked(name, cd->rd_fhp->fh_dentry, namlen);
3071 return nfserrno(PTR_ERR(dentry));
3075 * In the case of a mountpoint, the client may be asking for
3076 * attributes that are only properties of the underlying filesystem
3077 * as opposed to the cross-mounted file system. In such a case,
3078 * we will not follow the cross mount and will fill the attribtutes
3079 * directly from the mountpoint dentry.
3081 if (nfsd_mountpoint(dentry, exp)) {
3084 if (!(exp->ex_flags & NFSEXP_V4ROOT)
3085 && !attributes_need_mount(cd->rd_bmval)) {
3086 ignore_crossmnt = 1;
3090 * Why the heck aren't we just using nfsd_lookup??
3091 * Different "."/".." handling? Something else?
3092 * At least, add a comment here to explain....
3094 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
3096 nfserr = nfserrno(err);
3099 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
3105 nfserr = nfsd4_encode_fattr(xdr, NULL, exp, dentry, cd->rd_bmval,
3106 cd->rd_rqstp, ignore_crossmnt);
3114 nfsd4_encode_rdattr_error(struct xdr_stream *xdr, __be32 nfserr)
3118 p = xdr_reserve_space(xdr, 20);
3122 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
3123 *p++ = htonl(0); /* bmval1 */
3125 *p++ = htonl(4); /* attribute length */
3126 *p++ = nfserr; /* no htonl */
3131 nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
3132 loff_t offset, u64 ino, unsigned int d_type)
3134 struct readdir_cd *ccd = ccdv;
3135 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
3136 struct xdr_stream *xdr = cd->xdr;
3137 int start_offset = xdr->buf->len;
3139 u32 name_and_cookie;
3141 __be32 nfserr = nfserr_toosmall;
3145 /* In nfsv4, "." and ".." never make it onto the wire.. */
3146 if (name && isdotent(name, namlen)) {
3147 cd->common.err = nfs_ok;
3151 if (cd->cookie_offset) {
3152 wire_offset = cpu_to_be64(offset);
3153 write_bytes_to_xdr_buf(xdr->buf, cd->cookie_offset,
3157 p = xdr_reserve_space(xdr, 4);
3160 *p++ = xdr_one; /* mark entry present */
3161 cookie_offset = xdr->buf->len;
3162 p = xdr_reserve_space(xdr, 3*4 + namlen);
3165 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
3166 p = xdr_encode_array(p, name, namlen); /* name length & name */
3168 nfserr = nfsd4_encode_dirent_fattr(xdr, cd, name, namlen);
3172 case nfserr_resource:
3173 nfserr = nfserr_toosmall;
3176 xdr_truncate_encode(xdr, start_offset);
3180 * If the client requested the RDATTR_ERROR attribute,
3181 * we stuff the error code into this attribute
3182 * and continue. If this attribute was not requested,
3183 * then in accordance with the spec, we fail the
3184 * entire READDIR operation(!)
3186 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
3188 p = nfsd4_encode_rdattr_error(xdr, nfserr);
3190 nfserr = nfserr_toosmall;
3194 nfserr = nfserr_toosmall;
3195 entry_bytes = xdr->buf->len - start_offset;
3196 if (entry_bytes > cd->rd_maxcount)
3198 cd->rd_maxcount -= entry_bytes;
3200 * RFC 3530 14.2.24 describes rd_dircount as only a "hint", so
3201 * let's always let through the first entry, at least:
3203 if (!cd->rd_dircount)
3205 name_and_cookie = 4 + 4 * XDR_QUADLEN(namlen) + 8;
3206 if (name_and_cookie > cd->rd_dircount && cd->cookie_offset)
3208 cd->rd_dircount -= min(cd->rd_dircount, name_and_cookie);
3210 cd->cookie_offset = cookie_offset;
3212 cd->common.err = nfs_ok;
3215 xdr_truncate_encode(xdr, start_offset);
3216 cd->common.err = nfserr;
3221 nfsd4_encode_stateid(struct xdr_stream *xdr, stateid_t *sid)
3225 p = xdr_reserve_space(xdr, sizeof(stateid_t));
3227 return nfserr_resource;
3228 *p++ = cpu_to_be32(sid->si_generation);
3229 p = xdr_encode_opaque_fixed(p, &sid->si_opaque,
3230 sizeof(stateid_opaque_t));
3235 nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
3237 struct xdr_stream *xdr = &resp->xdr;
3240 p = xdr_reserve_space(xdr, 8);
3242 return nfserr_resource;
3243 *p++ = cpu_to_be32(access->ac_supported);
3244 *p++ = cpu_to_be32(access->ac_resp_access);
3248 static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
3250 struct xdr_stream *xdr = &resp->xdr;
3253 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 8);
3255 return nfserr_resource;
3256 p = xdr_encode_opaque_fixed(p, bcts->sessionid.data,
3257 NFS4_MAX_SESSIONID_LEN);
3258 *p++ = cpu_to_be32(bcts->dir);
3259 /* Upshifting from TCP to RDMA is not supported */
3260 *p++ = cpu_to_be32(0);
3265 nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
3267 struct xdr_stream *xdr = &resp->xdr;
3269 return nfsd4_encode_stateid(xdr, &close->cl_stateid);
3274 nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
3276 struct xdr_stream *xdr = &resp->xdr;
3279 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
3281 return nfserr_resource;
3282 p = xdr_encode_opaque_fixed(p, commit->co_verf.data,
3283 NFS4_VERIFIER_SIZE);
3288 nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
3290 struct xdr_stream *xdr = &resp->xdr;
3293 p = xdr_reserve_space(xdr, 20);
3295 return nfserr_resource;
3296 encode_cinfo(p, &create->cr_cinfo);
3297 return nfsd4_encode_bitmap(xdr, create->cr_bmval[0],
3298 create->cr_bmval[1], create->cr_bmval[2]);
3302 nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
3304 struct svc_fh *fhp = getattr->ga_fhp;
3305 struct xdr_stream *xdr = &resp->xdr;
3307 return nfsd4_encode_fattr(xdr, fhp, fhp->fh_export, fhp->fh_dentry,
3308 getattr->ga_bmval, resp->rqstp, 0);
3312 nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
3314 struct xdr_stream *xdr = &resp->xdr;
3315 struct svc_fh *fhp = *fhpp;
3319 len = fhp->fh_handle.fh_size;
3320 p = xdr_reserve_space(xdr, len + 4);
3322 return nfserr_resource;
3323 p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base, len);
3328 * Including all fields other than the name, a LOCK4denied structure requires
3329 * 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
3332 nfsd4_encode_lock_denied(struct xdr_stream *xdr, struct nfsd4_lock_denied *ld)
3334 struct xdr_netobj *conf = &ld->ld_owner;
3338 p = xdr_reserve_space(xdr, 32 + XDR_LEN(conf->len));
3341 * Don't fail to return the result just because we can't
3342 * return the conflicting open:
3350 return nfserr_resource;
3352 p = xdr_encode_hyper(p, ld->ld_start);
3353 p = xdr_encode_hyper(p, ld->ld_length);
3354 *p++ = cpu_to_be32(ld->ld_type);
3356 p = xdr_encode_opaque_fixed(p, &ld->ld_clientid, 8);
3357 p = xdr_encode_opaque(p, conf->data, conf->len);
3359 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
3360 p = xdr_encode_hyper(p, (u64)0); /* clientid */
3361 *p++ = cpu_to_be32(0); /* length of owner name */
3363 return nfserr_denied;
3367 nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
3369 struct xdr_stream *xdr = &resp->xdr;
3372 nfserr = nfsd4_encode_stateid(xdr, &lock->lk_resp_stateid);
3373 else if (nfserr == nfserr_denied)
3374 nfserr = nfsd4_encode_lock_denied(xdr, &lock->lk_denied);
3380 nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
3382 struct xdr_stream *xdr = &resp->xdr;
3384 if (nfserr == nfserr_denied)
3385 nfsd4_encode_lock_denied(xdr, &lockt->lt_denied);
3390 nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
3392 struct xdr_stream *xdr = &resp->xdr;
3394 return nfsd4_encode_stateid(xdr, &locku->lu_stateid);
3399 nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
3401 struct xdr_stream *xdr = &resp->xdr;
3404 p = xdr_reserve_space(xdr, 20);
3406 return nfserr_resource;
3407 p = encode_cinfo(p, &link->li_cinfo);
3413 nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
3415 struct xdr_stream *xdr = &resp->xdr;
3418 nfserr = nfsd4_encode_stateid(xdr, &open->op_stateid);
3421 p = xdr_reserve_space(xdr, 24);
3423 return nfserr_resource;
3424 p = encode_cinfo(p, &open->op_cinfo);
3425 *p++ = cpu_to_be32(open->op_rflags);
3427 nfserr = nfsd4_encode_bitmap(xdr, open->op_bmval[0], open->op_bmval[1],
3432 p = xdr_reserve_space(xdr, 4);
3434 return nfserr_resource;
3436 *p++ = cpu_to_be32(open->op_delegate_type);
3437 switch (open->op_delegate_type) {
3438 case NFS4_OPEN_DELEGATE_NONE:
3440 case NFS4_OPEN_DELEGATE_READ:
3441 nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
3444 p = xdr_reserve_space(xdr, 20);
3446 return nfserr_resource;
3447 *p++ = cpu_to_be32(open->op_recall);
3450 * TODO: ACE's in delegations
3452 *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
3453 *p++ = cpu_to_be32(0);
3454 *p++ = cpu_to_be32(0);
3455 *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
3457 case NFS4_OPEN_DELEGATE_WRITE:
3458 nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
3461 p = xdr_reserve_space(xdr, 32);
3463 return nfserr_resource;
3464 *p++ = cpu_to_be32(0);
3467 * TODO: space_limit's in delegations
3469 *p++ = cpu_to_be32(NFS4_LIMIT_SIZE);
3470 *p++ = cpu_to_be32(~(u32)0);
3471 *p++ = cpu_to_be32(~(u32)0);
3474 * TODO: ACE's in delegations
3476 *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
3477 *p++ = cpu_to_be32(0);
3478 *p++ = cpu_to_be32(0);
3479 *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
3481 case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
3482 switch (open->op_why_no_deleg) {
3483 case WND4_CONTENTION:
3485 p = xdr_reserve_space(xdr, 8);
3487 return nfserr_resource;
3488 *p++ = cpu_to_be32(open->op_why_no_deleg);
3489 /* deleg signaling not supported yet: */
3490 *p++ = cpu_to_be32(0);
3493 p = xdr_reserve_space(xdr, 4);
3495 return nfserr_resource;
3496 *p++ = cpu_to_be32(open->op_why_no_deleg);
3502 /* XXX save filehandle here */
3507 nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
3509 struct xdr_stream *xdr = &resp->xdr;
3511 return nfsd4_encode_stateid(xdr, &oc->oc_resp_stateid);
3515 nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
3517 struct xdr_stream *xdr = &resp->xdr;
3519 return nfsd4_encode_stateid(xdr, &od->od_stateid);
3522 static __be32 nfsd4_encode_splice_read(
3523 struct nfsd4_compoundres *resp,
3524 struct nfsd4_read *read,
3525 struct file *file, unsigned long maxcount)
3527 struct xdr_stream *xdr = &resp->xdr;
3528 struct xdr_buf *buf = xdr->buf;
3532 __be32 *p = xdr->p - 2;
3534 /* Make sure there will be room for padding if needed */
3535 if (xdr->end - xdr->p < 1)
3536 return nfserr_resource;
3538 nfserr = nfsd_splice_read(read->rd_rqstp, read->rd_fhp,
3539 file, read->rd_offset, &maxcount, &eof);
3540 read->rd_length = maxcount;
3543 * nfsd_splice_actor may have already messed with the
3544 * page length; reset it so as not to confuse
3545 * xdr_truncate_encode:
3551 *(p++) = htonl(eof);
3552 *(p++) = htonl(maxcount);
3554 buf->page_len = maxcount;
3555 buf->len += maxcount;
3556 xdr->page_ptr += (buf->page_base + maxcount + PAGE_SIZE - 1)
3559 /* Use rest of head for padding and remaining ops: */
3560 buf->tail[0].iov_base = xdr->p;
3561 buf->tail[0].iov_len = 0;
3562 xdr->iov = buf->tail;
3564 int pad = 4 - (maxcount&3);
3568 buf->tail[0].iov_base += maxcount&3;
3569 buf->tail[0].iov_len = pad;
3573 space_left = min_t(int, (void *)xdr->end - (void *)xdr->p,
3574 buf->buflen - buf->len);
3575 buf->buflen = buf->len + space_left;
3576 xdr->end = (__be32 *)((void *)xdr->end + space_left);
3581 static __be32 nfsd4_encode_readv(struct nfsd4_compoundres *resp,
3582 struct nfsd4_read *read,
3583 struct file *file, unsigned long maxcount)
3585 struct xdr_stream *xdr = &resp->xdr;
3588 int starting_len = xdr->buf->len - 8;
3597 * svcrdma requires every READ payload to start somewhere
3600 if (xdr->iov == xdr->buf->head) {
3608 thislen = min_t(long, len, PAGE_SIZE);
3609 p = xdr_reserve_space(xdr, thislen);
3611 resp->rqstp->rq_vec[v].iov_base = p;
3612 resp->rqstp->rq_vec[v].iov_len = thislen;
3618 nfserr = nfsd_readv(resp->rqstp, read->rd_fhp, file, read->rd_offset,
3619 resp->rqstp->rq_vec, read->rd_vlen, &maxcount,
3621 read->rd_length = maxcount;
3624 if (svc_encode_read_payload(resp->rqstp, starting_len + 8, maxcount))
3626 xdr_truncate_encode(xdr, starting_len + 8 + xdr_align_size(maxcount));
3629 write_bytes_to_xdr_buf(xdr->buf, starting_len , &tmp, 4);
3630 tmp = htonl(maxcount);
3631 write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp, 4);
3634 pad = (maxcount&3) ? 4 - (maxcount&3) : 0;
3635 write_bytes_to_xdr_buf(xdr->buf, starting_len + 8 + maxcount,
3642 nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
3643 struct nfsd4_read *read)
3645 unsigned long maxcount;
3646 struct xdr_stream *xdr = &resp->xdr;
3648 int starting_len = xdr->buf->len;
3653 file = read->rd_nf->nf_file;
3655 p = xdr_reserve_space(xdr, 8); /* eof flag and byte count */
3657 WARN_ON_ONCE(test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags));
3658 return nfserr_resource;
3660 if (resp->xdr.buf->page_len &&
3661 test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags)) {
3663 return nfserr_resource;
3665 xdr_commit_encode(xdr);
3667 maxcount = svc_max_payload(resp->rqstp);
3668 maxcount = min_t(unsigned long, maxcount,
3669 (xdr->buf->buflen - xdr->buf->len));
3670 maxcount = min_t(unsigned long, maxcount, read->rd_length);
3672 if (file->f_op->splice_read &&
3673 test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags))
3674 nfserr = nfsd4_encode_splice_read(resp, read, file, maxcount);
3676 nfserr = nfsd4_encode_readv(resp, read, file, maxcount);
3679 xdr_truncate_encode(xdr, starting_len);
3685 nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
3690 struct xdr_stream *xdr = &resp->xdr;
3691 int length_offset = xdr->buf->len;
3694 p = xdr_reserve_space(xdr, 4);
3696 return nfserr_resource;
3697 maxcount = PAGE_SIZE;
3699 p = xdr_reserve_space(xdr, maxcount);
3701 return nfserr_resource;
3703 * XXX: By default, vfs_readlink() will truncate symlinks if they
3704 * would overflow the buffer. Is this kosher in NFSv4? If not, one
3705 * easy fix is: if vfs_readlink() precisely fills the buffer, assume
3706 * that truncation occurred, and return NFS4ERR_RESOURCE.
3708 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp,
3709 (char *)p, &maxcount);
3710 if (nfserr == nfserr_isdir)
3711 nfserr = nfserr_inval;
3713 xdr_truncate_encode(xdr, length_offset);
3717 wire_count = htonl(maxcount);
3718 write_bytes_to_xdr_buf(xdr->buf, length_offset, &wire_count, 4);
3719 xdr_truncate_encode(xdr, length_offset + 4 + ALIGN(maxcount, 4));
3721 write_bytes_to_xdr_buf(xdr->buf, length_offset + 4 + maxcount,
3722 &zero, 4 - (maxcount&3));
3727 nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
3733 struct xdr_stream *xdr = &resp->xdr;
3734 int starting_len = xdr->buf->len;
3737 p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
3739 return nfserr_resource;
3741 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
3742 *p++ = cpu_to_be32(0);
3743 *p++ = cpu_to_be32(0);
3744 resp->xdr.buf->head[0].iov_len = ((char *)resp->xdr.p)
3745 - (char *)resp->xdr.buf->head[0].iov_base;
3748 * Number of bytes left for directory entries allowing for the
3749 * final 8 bytes of the readdir and a following failed op:
3751 bytes_left = xdr->buf->buflen - xdr->buf->len
3752 - COMPOUND_ERR_SLACK_SPACE - 8;
3753 if (bytes_left < 0) {
3754 nfserr = nfserr_resource;
3757 maxcount = svc_max_payload(resp->rqstp);
3758 maxcount = min_t(u32, readdir->rd_maxcount, maxcount);
3760 * Note the rfc defines rd_maxcount as the size of the
3761 * READDIR4resok structure, which includes the verifier above
3762 * and the 8 bytes encoded at the end of this function:
3764 if (maxcount < 16) {
3765 nfserr = nfserr_toosmall;
3768 maxcount = min_t(int, maxcount-16, bytes_left);
3770 /* RFC 3530 14.2.24 allows us to ignore dircount when it's 0: */
3771 if (!readdir->rd_dircount)
3772 readdir->rd_dircount = svc_max_payload(resp->rqstp);
3775 readdir->rd_maxcount = maxcount;
3776 readdir->common.err = 0;
3777 readdir->cookie_offset = 0;
3779 offset = readdir->rd_cookie;
3780 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
3782 &readdir->common, nfsd4_encode_dirent);
3783 if (nfserr == nfs_ok &&
3784 readdir->common.err == nfserr_toosmall &&
3785 xdr->buf->len == starting_len + 8) {
3786 /* nothing encoded; which limit did we hit?: */
3787 if (maxcount - 16 < bytes_left)
3788 /* It was the fault of rd_maxcount: */
3789 nfserr = nfserr_toosmall;
3791 /* We ran out of buffer space: */
3792 nfserr = nfserr_resource;
3797 if (readdir->cookie_offset) {
3798 wire_offset = cpu_to_be64(offset);
3799 write_bytes_to_xdr_buf(xdr->buf, readdir->cookie_offset,
3803 p = xdr_reserve_space(xdr, 8);
3808 *p++ = 0; /* no more entries */
3809 *p++ = htonl(readdir->common.err == nfserr_eof);
3813 xdr_truncate_encode(xdr, starting_len);
3818 nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
3820 struct xdr_stream *xdr = &resp->xdr;
3823 p = xdr_reserve_space(xdr, 20);
3825 return nfserr_resource;
3826 p = encode_cinfo(p, &remove->rm_cinfo);
3831 nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
3833 struct xdr_stream *xdr = &resp->xdr;
3836 p = xdr_reserve_space(xdr, 40);
3838 return nfserr_resource;
3839 p = encode_cinfo(p, &rename->rn_sinfo);
3840 p = encode_cinfo(p, &rename->rn_tinfo);
3845 nfsd4_do_encode_secinfo(struct xdr_stream *xdr, struct svc_export *exp)
3847 u32 i, nflavs, supported;
3848 struct exp_flavor_info *flavs;
3849 struct exp_flavor_info def_flavs[2];
3850 __be32 *p, *flavorsp;
3851 static bool report = true;
3853 if (exp->ex_nflavors) {
3854 flavs = exp->ex_flavors;
3855 nflavs = exp->ex_nflavors;
3856 } else { /* Handling of some defaults in absence of real secinfo: */
3858 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
3860 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
3861 flavs[1].pseudoflavor = RPC_AUTH_NULL;
3862 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
3864 flavs[0].pseudoflavor
3865 = svcauth_gss_flavor(exp->ex_client);
3868 flavs[0].pseudoflavor
3869 = exp->ex_client->flavour->flavour;
3874 p = xdr_reserve_space(xdr, 4);
3876 return nfserr_resource;
3877 flavorsp = p++; /* to be backfilled later */
3879 for (i = 0; i < nflavs; i++) {
3880 rpc_authflavor_t pf = flavs[i].pseudoflavor;
3881 struct rpcsec_gss_info info;
3883 if (rpcauth_get_gssinfo(pf, &info) == 0) {
3885 p = xdr_reserve_space(xdr, 4 + 4 +
3886 XDR_LEN(info.oid.len) + 4 + 4);
3888 return nfserr_resource;
3889 *p++ = cpu_to_be32(RPC_AUTH_GSS);
3890 p = xdr_encode_opaque(p, info.oid.data, info.oid.len);
3891 *p++ = cpu_to_be32(info.qop);
3892 *p++ = cpu_to_be32(info.service);
3893 } else if (pf < RPC_AUTH_MAXFLAVOR) {
3895 p = xdr_reserve_space(xdr, 4);
3897 return nfserr_resource;
3898 *p++ = cpu_to_be32(pf);
3901 pr_warn("NFS: SECINFO: security flavor %u "
3902 "is not supported\n", pf);
3906 if (nflavs != supported)
3908 *flavorsp = htonl(supported);
3913 nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
3914 struct nfsd4_secinfo *secinfo)
3916 struct xdr_stream *xdr = &resp->xdr;
3918 return nfsd4_do_encode_secinfo(xdr, secinfo->si_exp);
3922 nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
3923 struct nfsd4_secinfo_no_name *secinfo)
3925 struct xdr_stream *xdr = &resp->xdr;
3927 return nfsd4_do_encode_secinfo(xdr, secinfo->sin_exp);
3931 * The SETATTR encode routine is special -- it always encodes a bitmap,
3932 * regardless of the error status.
3935 nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
3937 struct xdr_stream *xdr = &resp->xdr;
3940 p = xdr_reserve_space(xdr, 16);
3942 return nfserr_resource;
3944 *p++ = cpu_to_be32(3);
3945 *p++ = cpu_to_be32(0);
3946 *p++ = cpu_to_be32(0);
3947 *p++ = cpu_to_be32(0);
3950 *p++ = cpu_to_be32(3);
3951 *p++ = cpu_to_be32(setattr->sa_bmval[0]);
3952 *p++ = cpu_to_be32(setattr->sa_bmval[1]);
3953 *p++ = cpu_to_be32(setattr->sa_bmval[2]);
3959 nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
3961 struct xdr_stream *xdr = &resp->xdr;
3965 p = xdr_reserve_space(xdr, 8 + NFS4_VERIFIER_SIZE);
3967 return nfserr_resource;
3968 p = xdr_encode_opaque_fixed(p, &scd->se_clientid, 8);
3969 p = xdr_encode_opaque_fixed(p, &scd->se_confirm,
3970 NFS4_VERIFIER_SIZE);
3972 else if (nfserr == nfserr_clid_inuse) {
3973 p = xdr_reserve_space(xdr, 8);
3975 return nfserr_resource;
3976 *p++ = cpu_to_be32(0);
3977 *p++ = cpu_to_be32(0);
3983 nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
3985 struct xdr_stream *xdr = &resp->xdr;
3988 p = xdr_reserve_space(xdr, 16);
3990 return nfserr_resource;
3991 *p++ = cpu_to_be32(write->wr_bytes_written);
3992 *p++ = cpu_to_be32(write->wr_how_written);
3993 p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
3994 NFS4_VERIFIER_SIZE);
3999 nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
4000 struct nfsd4_exchange_id *exid)
4002 struct xdr_stream *xdr = &resp->xdr;
4007 int server_scope_sz;
4008 uint64_t minor_id = 0;
4009 struct nfsd_net *nn = net_generic(SVC_NET(resp->rqstp), nfsd_net_id);
4011 major_id = nn->nfsd_name;
4012 major_id_sz = strlen(nn->nfsd_name);
4013 server_scope = nn->nfsd_name;
4014 server_scope_sz = strlen(nn->nfsd_name);
4016 p = xdr_reserve_space(xdr,
4017 8 /* eir_clientid */ +
4018 4 /* eir_sequenceid */ +
4022 return nfserr_resource;
4024 p = xdr_encode_opaque_fixed(p, &exid->clientid, 8);
4025 *p++ = cpu_to_be32(exid->seqid);
4026 *p++ = cpu_to_be32(exid->flags);
4028 *p++ = cpu_to_be32(exid->spa_how);
4030 switch (exid->spa_how) {
4034 /* spo_must_enforce bitmap: */
4035 nfserr = nfsd4_encode_bitmap(xdr,
4036 exid->spo_must_enforce[0],
4037 exid->spo_must_enforce[1],
4038 exid->spo_must_enforce[2]);
4041 /* spo_must_allow bitmap: */
4042 nfserr = nfsd4_encode_bitmap(xdr,
4043 exid->spo_must_allow[0],
4044 exid->spo_must_allow[1],
4045 exid->spo_must_allow[2]);
4053 p = xdr_reserve_space(xdr,
4054 8 /* so_minor_id */ +
4055 4 /* so_major_id.len */ +
4056 (XDR_QUADLEN(major_id_sz) * 4) +
4057 4 /* eir_server_scope.len */ +
4058 (XDR_QUADLEN(server_scope_sz) * 4) +
4059 4 /* eir_server_impl_id.count (0) */);
4061 return nfserr_resource;
4063 /* The server_owner struct */
4064 p = xdr_encode_hyper(p, minor_id); /* Minor id */
4066 p = xdr_encode_opaque(p, major_id, major_id_sz);
4069 p = xdr_encode_opaque(p, server_scope, server_scope_sz);
4071 /* Implementation id */
4072 *p++ = cpu_to_be32(0); /* zero length nfs_impl_id4 array */
4077 nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
4078 struct nfsd4_create_session *sess)
4080 struct xdr_stream *xdr = &resp->xdr;
4083 p = xdr_reserve_space(xdr, 24);
4085 return nfserr_resource;
4086 p = xdr_encode_opaque_fixed(p, sess->sessionid.data,
4087 NFS4_MAX_SESSIONID_LEN);
4088 *p++ = cpu_to_be32(sess->seqid);
4089 *p++ = cpu_to_be32(sess->flags);
4091 p = xdr_reserve_space(xdr, 28);
4093 return nfserr_resource;
4094 *p++ = cpu_to_be32(0); /* headerpadsz */
4095 *p++ = cpu_to_be32(sess->fore_channel.maxreq_sz);
4096 *p++ = cpu_to_be32(sess->fore_channel.maxresp_sz);
4097 *p++ = cpu_to_be32(sess->fore_channel.maxresp_cached);
4098 *p++ = cpu_to_be32(sess->fore_channel.maxops);
4099 *p++ = cpu_to_be32(sess->fore_channel.maxreqs);
4100 *p++ = cpu_to_be32(sess->fore_channel.nr_rdma_attrs);
4102 if (sess->fore_channel.nr_rdma_attrs) {
4103 p = xdr_reserve_space(xdr, 4);
4105 return nfserr_resource;
4106 *p++ = cpu_to_be32(sess->fore_channel.rdma_attrs);
4109 p = xdr_reserve_space(xdr, 28);
4111 return nfserr_resource;
4112 *p++ = cpu_to_be32(0); /* headerpadsz */
4113 *p++ = cpu_to_be32(sess->back_channel.maxreq_sz);
4114 *p++ = cpu_to_be32(sess->back_channel.maxresp_sz);
4115 *p++ = cpu_to_be32(sess->back_channel.maxresp_cached);
4116 *p++ = cpu_to_be32(sess->back_channel.maxops);
4117 *p++ = cpu_to_be32(sess->back_channel.maxreqs);
4118 *p++ = cpu_to_be32(sess->back_channel.nr_rdma_attrs);
4120 if (sess->back_channel.nr_rdma_attrs) {
4121 p = xdr_reserve_space(xdr, 4);
4123 return nfserr_resource;
4124 *p++ = cpu_to_be32(sess->back_channel.rdma_attrs);
4130 nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
4131 struct nfsd4_sequence *seq)
4133 struct xdr_stream *xdr = &resp->xdr;
4136 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 20);
4138 return nfserr_resource;
4139 p = xdr_encode_opaque_fixed(p, seq->sessionid.data,
4140 NFS4_MAX_SESSIONID_LEN);
4141 *p++ = cpu_to_be32(seq->seqid);
4142 *p++ = cpu_to_be32(seq->slotid);
4143 /* Note slotid's are numbered from zero: */
4144 *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_highest_slotid */
4145 *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_target_highest_slotid */
4146 *p++ = cpu_to_be32(seq->status_flags);
4148 resp->cstate.data_offset = xdr->buf->len; /* DRC cache data pointer */
4153 nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
4154 struct nfsd4_test_stateid *test_stateid)
4156 struct xdr_stream *xdr = &resp->xdr;
4157 struct nfsd4_test_stateid_id *stateid, *next;
4160 p = xdr_reserve_space(xdr, 4 + (4 * test_stateid->ts_num_ids));
4162 return nfserr_resource;
4163 *p++ = htonl(test_stateid->ts_num_ids);
4165 list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
4166 *p++ = stateid->ts_id_status;
4172 #ifdef CONFIG_NFSD_PNFS
4174 nfsd4_encode_getdeviceinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
4175 struct nfsd4_getdeviceinfo *gdev)
4177 struct xdr_stream *xdr = &resp->xdr;
4178 const struct nfsd4_layout_ops *ops;
4179 u32 starting_len = xdr->buf->len, needed_len;
4182 p = xdr_reserve_space(xdr, 4);
4184 return nfserr_resource;
4186 *p++ = cpu_to_be32(gdev->gd_layout_type);
4188 /* If maxcount is 0 then just update notifications */
4189 if (gdev->gd_maxcount != 0) {
4190 ops = nfsd4_layout_ops[gdev->gd_layout_type];
4191 nfserr = ops->encode_getdeviceinfo(xdr, gdev);
4194 * We don't bother to burden the layout drivers with
4195 * enforcing gd_maxcount, just tell the client to
4196 * come back with a bigger buffer if it's not enough.
4198 if (xdr->buf->len + 4 > gdev->gd_maxcount)
4204 if (gdev->gd_notify_types) {
4205 p = xdr_reserve_space(xdr, 4 + 4);
4207 return nfserr_resource;
4208 *p++ = cpu_to_be32(1); /* bitmap length */
4209 *p++ = cpu_to_be32(gdev->gd_notify_types);
4211 p = xdr_reserve_space(xdr, 4);
4213 return nfserr_resource;
4219 dprintk("%s: maxcount too small\n", __func__);
4220 needed_len = xdr->buf->len + 4 /* notifications */;
4221 xdr_truncate_encode(xdr, starting_len);
4222 p = xdr_reserve_space(xdr, 4);
4224 return nfserr_resource;
4225 *p++ = cpu_to_be32(needed_len);
4226 return nfserr_toosmall;
4230 nfsd4_encode_layoutget(struct nfsd4_compoundres *resp, __be32 nfserr,
4231 struct nfsd4_layoutget *lgp)
4233 struct xdr_stream *xdr = &resp->xdr;
4234 const struct nfsd4_layout_ops *ops;
4237 p = xdr_reserve_space(xdr, 36 + sizeof(stateid_opaque_t));
4239 return nfserr_resource;
4241 *p++ = cpu_to_be32(1); /* we always set return-on-close */
4242 *p++ = cpu_to_be32(lgp->lg_sid.si_generation);
4243 p = xdr_encode_opaque_fixed(p, &lgp->lg_sid.si_opaque,
4244 sizeof(stateid_opaque_t));
4246 *p++ = cpu_to_be32(1); /* we always return a single layout */
4247 p = xdr_encode_hyper(p, lgp->lg_seg.offset);
4248 p = xdr_encode_hyper(p, lgp->lg_seg.length);
4249 *p++ = cpu_to_be32(lgp->lg_seg.iomode);
4250 *p++ = cpu_to_be32(lgp->lg_layout_type);
4252 ops = nfsd4_layout_ops[lgp->lg_layout_type];
4253 return ops->encode_layoutget(xdr, lgp);
4257 nfsd4_encode_layoutcommit(struct nfsd4_compoundres *resp, __be32 nfserr,
4258 struct nfsd4_layoutcommit *lcp)
4260 struct xdr_stream *xdr = &resp->xdr;
4263 p = xdr_reserve_space(xdr, 4);
4265 return nfserr_resource;
4266 *p++ = cpu_to_be32(lcp->lc_size_chg);
4267 if (lcp->lc_size_chg) {
4268 p = xdr_reserve_space(xdr, 8);
4270 return nfserr_resource;
4271 p = xdr_encode_hyper(p, lcp->lc_newsize);
4278 nfsd4_encode_layoutreturn(struct nfsd4_compoundres *resp, __be32 nfserr,
4279 struct nfsd4_layoutreturn *lrp)
4281 struct xdr_stream *xdr = &resp->xdr;
4284 p = xdr_reserve_space(xdr, 4);
4286 return nfserr_resource;
4287 *p++ = cpu_to_be32(lrp->lrs_present);
4288 if (lrp->lrs_present)
4289 return nfsd4_encode_stateid(xdr, &lrp->lr_sid);
4292 #endif /* CONFIG_NFSD_PNFS */
4295 nfsd42_encode_write_res(struct nfsd4_compoundres *resp,
4296 struct nfsd42_write_res *write, bool sync)
4299 p = xdr_reserve_space(&resp->xdr, 4);
4301 return nfserr_resource;
4304 *p++ = cpu_to_be32(0);
4307 *p++ = cpu_to_be32(1);
4308 nfserr = nfsd4_encode_stateid(&resp->xdr, &write->cb_stateid);
4312 p = xdr_reserve_space(&resp->xdr, 8 + 4 + NFS4_VERIFIER_SIZE);
4314 return nfserr_resource;
4316 p = xdr_encode_hyper(p, write->wr_bytes_written);
4317 *p++ = cpu_to_be32(write->wr_stable_how);
4318 p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
4319 NFS4_VERIFIER_SIZE);
4324 nfsd42_encode_nl4_server(struct nfsd4_compoundres *resp, struct nl4_server *ns)
4326 struct xdr_stream *xdr = &resp->xdr;
4327 struct nfs42_netaddr *addr;
4330 p = xdr_reserve_space(xdr, 4);
4331 *p++ = cpu_to_be32(ns->nl4_type);
4333 switch (ns->nl4_type) {
4335 addr = &ns->u.nl4_addr;
4337 /* netid_len, netid, uaddr_len, uaddr (port included
4338 * in RPCBIND_MAXUADDRLEN)
4340 p = xdr_reserve_space(xdr,
4342 (XDR_QUADLEN(addr->netid_len) * 4) +
4344 (XDR_QUADLEN(addr->addr_len) * 4));
4346 return nfserr_resource;
4348 *p++ = cpu_to_be32(addr->netid_len);
4349 p = xdr_encode_opaque_fixed(p, addr->netid,
4351 *p++ = cpu_to_be32(addr->addr_len);
4352 p = xdr_encode_opaque_fixed(p, addr->addr,
4356 WARN_ON_ONCE(ns->nl4_type != NL4_NETADDR);
4357 return nfserr_inval;
4364 nfsd4_encode_copy(struct nfsd4_compoundres *resp, __be32 nfserr,
4365 struct nfsd4_copy *copy)
4369 nfserr = nfsd42_encode_write_res(resp, ©->cp_res,
4370 copy->cp_synchronous);
4374 p = xdr_reserve_space(&resp->xdr, 4 + 4);
4375 *p++ = xdr_one; /* cr_consecutive */
4376 *p++ = cpu_to_be32(copy->cp_synchronous);
4381 nfsd4_encode_offload_status(struct nfsd4_compoundres *resp, __be32 nfserr,
4382 struct nfsd4_offload_status *os)
4384 struct xdr_stream *xdr = &resp->xdr;
4387 p = xdr_reserve_space(xdr, 8 + 4);
4389 return nfserr_resource;
4390 p = xdr_encode_hyper(p, os->count);
4391 *p++ = cpu_to_be32(0);
4397 nfsd4_encode_copy_notify(struct nfsd4_compoundres *resp, __be32 nfserr,
4398 struct nfsd4_copy_notify *cn)
4400 struct xdr_stream *xdr = &resp->xdr;
4407 p = xdr_reserve_space(xdr, 12);
4409 return nfserr_resource;
4411 /* cnr_lease_time */
4412 p = xdr_encode_hyper(p, cn->cpn_sec);
4413 *p++ = cpu_to_be32(cn->cpn_nsec);
4416 nfserr = nfsd4_encode_stateid(xdr, &cn->cpn_cnr_stateid);
4420 /* cnr_src.nl_nsvr */
4421 p = xdr_reserve_space(xdr, 4);
4423 return nfserr_resource;
4425 *p++ = cpu_to_be32(1);
4427 return nfsd42_encode_nl4_server(resp, &cn->cpn_src);
4431 nfsd4_encode_seek(struct nfsd4_compoundres *resp, __be32 nfserr,
4432 struct nfsd4_seek *seek)
4436 p = xdr_reserve_space(&resp->xdr, 4 + 8);
4437 *p++ = cpu_to_be32(seek->seek_eof);
4438 p = xdr_encode_hyper(p, seek->seek_pos);
4444 nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
4449 typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
4452 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
4453 * since we don't need to filter out obsolete ops as this is
4454 * done in the decoding phase.
4456 static const nfsd4_enc nfsd4_enc_ops[] = {
4457 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
4458 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
4459 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
4460 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
4461 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
4462 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
4463 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
4464 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
4465 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
4466 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
4467 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
4468 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
4469 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
4470 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
4471 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
4472 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
4473 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
4474 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
4475 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
4476 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
4477 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
4478 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
4479 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
4480 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
4481 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
4482 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
4483 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
4484 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
4485 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
4486 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
4487 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
4488 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
4489 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
4490 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
4491 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
4492 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
4493 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
4495 /* NFSv4.1 operations */
4496 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
4497 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
4498 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
4499 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
4500 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_noop,
4501 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
4502 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
4503 #ifdef CONFIG_NFSD_PNFS
4504 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_getdeviceinfo,
4505 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
4506 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_layoutcommit,
4507 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_layoutget,
4508 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_layoutreturn,
4510 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
4511 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
4512 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
4513 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
4514 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
4516 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
4517 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
4518 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
4519 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
4520 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
4521 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
4522 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
4524 /* NFSv4.2 operations */
4525 [OP_ALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
4526 [OP_COPY] = (nfsd4_enc)nfsd4_encode_copy,
4527 [OP_COPY_NOTIFY] = (nfsd4_enc)nfsd4_encode_copy_notify,
4528 [OP_DEALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
4529 [OP_IO_ADVISE] = (nfsd4_enc)nfsd4_encode_noop,
4530 [OP_LAYOUTERROR] = (nfsd4_enc)nfsd4_encode_noop,
4531 [OP_LAYOUTSTATS] = (nfsd4_enc)nfsd4_encode_noop,
4532 [OP_OFFLOAD_CANCEL] = (nfsd4_enc)nfsd4_encode_noop,
4533 [OP_OFFLOAD_STATUS] = (nfsd4_enc)nfsd4_encode_offload_status,
4534 [OP_READ_PLUS] = (nfsd4_enc)nfsd4_encode_noop,
4535 [OP_SEEK] = (nfsd4_enc)nfsd4_encode_seek,
4536 [OP_WRITE_SAME] = (nfsd4_enc)nfsd4_encode_noop,
4537 [OP_CLONE] = (nfsd4_enc)nfsd4_encode_noop,
4541 * Calculate whether we still have space to encode repsize bytes.
4542 * There are two considerations:
4543 * - For NFS versions >=4.1, the size of the reply must stay within
4545 * - For all NFS versions, we must stay within limited preallocated
4548 * This is called before the operation is processed, so can only provide
4549 * an upper estimate. For some nonidempotent operations (such as
4550 * getattr), it's not necessarily a problem if that estimate is wrong,
4551 * as we can fail it after processing without significant side effects.
4553 __be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 respsize)
4555 struct xdr_buf *buf = &resp->rqstp->rq_res;
4556 struct nfsd4_slot *slot = resp->cstate.slot;
4558 if (buf->len + respsize <= buf->buflen)
4560 if (!nfsd4_has_session(&resp->cstate))
4561 return nfserr_resource;
4562 if (slot->sl_flags & NFSD4_SLOT_CACHETHIS) {
4564 return nfserr_rep_too_big_to_cache;
4566 return nfserr_rep_too_big;
4570 nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
4572 struct xdr_stream *xdr = &resp->xdr;
4573 struct nfs4_stateowner *so = resp->cstate.replay_owner;
4574 struct svc_rqst *rqstp = resp->rqstp;
4575 const struct nfsd4_operation *opdesc = op->opdesc;
4576 int post_err_offset;
4580 p = xdr_reserve_space(xdr, 8);
4585 *p++ = cpu_to_be32(op->opnum);
4586 post_err_offset = xdr->buf->len;
4588 if (op->opnum == OP_ILLEGAL)
4590 if (op->status && opdesc &&
4591 !(opdesc->op_flags & OP_NONTRIVIAL_ERROR_ENCODE))
4593 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
4594 !nfsd4_enc_ops[op->opnum]);
4595 encoder = nfsd4_enc_ops[op->opnum];
4596 op->status = encoder(resp, op->status, &op->u);
4597 if (opdesc && opdesc->op_release)
4598 opdesc->op_release(&op->u);
4599 xdr_commit_encode(xdr);
4601 /* nfsd4_check_resp_size guarantees enough room for error status */
4603 int space_needed = 0;
4604 if (!nfsd4_last_compound_op(rqstp))
4605 space_needed = COMPOUND_ERR_SLACK_SPACE;
4606 op->status = nfsd4_check_resp_size(resp, space_needed);
4608 if (op->status == nfserr_resource && nfsd4_has_session(&resp->cstate)) {
4609 struct nfsd4_slot *slot = resp->cstate.slot;
4611 if (slot->sl_flags & NFSD4_SLOT_CACHETHIS)
4612 op->status = nfserr_rep_too_big_to_cache;
4614 op->status = nfserr_rep_too_big;
4616 if (op->status == nfserr_resource ||
4617 op->status == nfserr_rep_too_big ||
4618 op->status == nfserr_rep_too_big_to_cache) {
4620 * The operation may have already been encoded or
4621 * partially encoded. No op returns anything additional
4622 * in the case of one of these three errors, so we can
4623 * just truncate back to after the status. But it's a
4624 * bug if we had to do this on a non-idempotent op:
4626 warn_on_nonidempotent_op(op);
4627 xdr_truncate_encode(xdr, post_err_offset);
4630 int len = xdr->buf->len - post_err_offset;
4632 so->so_replay.rp_status = op->status;
4633 so->so_replay.rp_buflen = len;
4634 read_bytes_from_xdr_buf(xdr->buf, post_err_offset,
4635 so->so_replay.rp_buf, len);
4638 /* Note that op->status is already in network byte order: */
4639 write_bytes_to_xdr_buf(xdr->buf, post_err_offset - 4, &op->status, 4);
4643 * Encode the reply stored in the stateowner reply cache
4645 * XDR note: do not encode rp->rp_buflen: the buffer contains the
4646 * previously sent already encoded operation.
4649 nfsd4_encode_replay(struct xdr_stream *xdr, struct nfsd4_op *op)
4652 struct nfs4_replay *rp = op->replay;
4654 p = xdr_reserve_space(xdr, 8 + rp->rp_buflen);
4659 *p++ = cpu_to_be32(op->opnum);
4660 *p++ = rp->rp_status; /* already xdr'ed */
4662 p = xdr_encode_opaque_fixed(p, rp->rp_buf, rp->rp_buflen);
4666 nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p)
4668 return xdr_ressize_check(rqstp, p);
4671 void nfsd4_release_compoundargs(struct svc_rqst *rqstp)
4673 struct nfsd4_compoundargs *args = rqstp->rq_argp;
4675 if (args->ops != args->iops) {
4677 args->ops = args->iops;
4681 while (args->to_free) {
4682 struct svcxdr_tmpbuf *tb = args->to_free;
4683 args->to_free = tb->next;
4689 nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p)
4691 struct nfsd4_compoundargs *args = rqstp->rq_argp;
4693 if (rqstp->rq_arg.head[0].iov_len % 4) {
4694 /* client is nuts */
4695 dprintk("%s: compound not properly padded! (peeraddr=%pISc xid=0x%x)",
4696 __func__, svc_addr(rqstp), be32_to_cpu(rqstp->rq_xid));
4700 args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
4701 args->pagelist = rqstp->rq_arg.pages;
4702 args->pagelen = rqstp->rq_arg.page_len;
4705 args->to_free = NULL;
4706 args->ops = args->iops;
4707 args->rqstp = rqstp;
4709 return !nfsd4_decode_compound(args);
4713 nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p)
4716 * All that remains is to write the tag and operation count...
4718 struct nfsd4_compoundres *resp = rqstp->rq_resp;
4719 struct xdr_buf *buf = resp->xdr.buf;
4721 WARN_ON_ONCE(buf->len != buf->head[0].iov_len + buf->page_len +
4722 buf->tail[0].iov_len);
4724 rqstp->rq_next_page = resp->xdr.page_ptr + 1;
4727 *p++ = htonl(resp->taglen);
4728 memcpy(p, resp->tag, resp->taglen);
4729 p += XDR_QUADLEN(resp->taglen);
4730 *p++ = htonl(resp->opcnt);
4732 nfsd4_sequence_done(resp);