2 * Server-side XDR for NFSv4
4 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * Kendrick Smith <kmsmith@umich.edu>
8 * Andy Adamson <andros@umich.edu>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
35 * TODO: Neil Brown made the following observation: We currently
36 * initially reserve NFSD_BUFSIZE space on the transmit queue and
37 * never release any of that until the request is complete.
38 * It would be good to calculate a new maximum response size while
39 * decoding the COMPOUND, and call svc_reserve with this number
40 * at the end of nfs4svc_decode_compoundargs.
43 #include <linux/slab.h>
44 #include <linux/namei.h>
45 #include <linux/statfs.h>
46 #include <linux/utsname.h>
47 #include <linux/pagemap.h>
48 #include <linux/sunrpc/svcauth_gss.h>
58 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
59 #include <linux/security.h>
63 #define NFSDDBG_FACILITY NFSDDBG_XDR
66 * As per referral draft, the fsid for a referral MUST be different from the fsid of the containing
67 * directory in order to indicate to the client that a filesystem boundary is present
68 * We use a fixed fsid for a referral
70 #define NFS4_REFERRAL_FSID_MAJOR 0x8000000ULL
71 #define NFS4_REFERRAL_FSID_MINOR 0x8000000ULL
74 check_filename(char *str, int len)
80 if (isdotent(str, len))
81 return nfserr_badname;
82 for (i = 0; i < len; i++)
84 return nfserr_badname;
96 dprintk("NFSD: xdr error (%s:%d)\n", \
97 __FILE__, __LINE__); \
98 status = nfserr_bad_xdr; \
101 #define READ32(x) (x) = ntohl(*p++)
102 #define READ64(x) do { \
103 (x) = (u64)ntohl(*p++) << 32; \
104 (x) |= ntohl(*p++); \
106 #define READTIME(x) do { \
111 #define READMEM(x,nbytes) do { \
113 p += XDR_QUADLEN(nbytes); \
115 #define SAVEMEM(x,nbytes) do { \
116 if (!(x = (p==argp->tmp || p == argp->tmpp) ? \
117 savemem(argp, p, nbytes) : \
119 dprintk("NFSD: xdr error (%s:%d)\n", \
120 __FILE__, __LINE__); \
123 p += XDR_QUADLEN(nbytes); \
125 #define COPYMEM(x,nbytes) do { \
126 memcpy((x), p, nbytes); \
127 p += XDR_QUADLEN(nbytes); \
130 /* READ_BUF, read_buf(): nbytes must be <= PAGE_SIZE */
131 #define READ_BUF(nbytes) do { \
132 if (nbytes <= (u32)((char *)argp->end - (char *)argp->p)) { \
134 argp->p += XDR_QUADLEN(nbytes); \
135 } else if (!(p = read_buf(argp, nbytes))) { \
136 dprintk("NFSD: xdr error (%s:%d)\n", \
137 __FILE__, __LINE__); \
142 static void next_decode_page(struct nfsd4_compoundargs *argp)
145 argp->p = page_address(argp->pagelist[0]);
146 if (argp->pagelen < PAGE_SIZE) {
147 argp->end = argp->p + (argp->pagelen>>2);
150 argp->end = argp->p + (PAGE_SIZE>>2);
151 argp->pagelen -= PAGE_SIZE;
155 static __be32 *read_buf(struct nfsd4_compoundargs *argp, u32 nbytes)
157 /* We want more bytes than seem to be available.
158 * Maybe we need a new page, maybe we have just run out
160 unsigned int avail = (char *)argp->end - (char *)argp->p;
162 if (avail + argp->pagelen < nbytes)
164 if (avail + PAGE_SIZE < nbytes) /* need more than a page !! */
166 /* ok, we can do it with the current plus the next page */
167 if (nbytes <= sizeof(argp->tmp))
171 p = argp->tmpp = kmalloc(nbytes, GFP_KERNEL);
177 * The following memcpy is safe because read_buf is always
178 * called with nbytes > avail, and the two cases above both
179 * guarantee p points to at least nbytes bytes.
181 memcpy(p, argp->p, avail);
182 next_decode_page(argp);
183 memcpy(((char*)p)+avail, argp->p, (nbytes - avail));
184 argp->p += XDR_QUADLEN(nbytes - avail);
188 static int zero_clientid(clientid_t *clid)
190 return (clid->cl_boot == 0) && (clid->cl_id == 0);
194 defer_free(struct nfsd4_compoundargs *argp,
195 void (*release)(const void *), void *p)
199 tb = kmalloc(sizeof(*tb), GFP_KERNEL);
203 tb->release = release;
204 tb->next = argp->to_free;
209 static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
211 if (p == argp->tmp) {
212 p = kmemdup(argp->tmp, nbytes, GFP_KERNEL);
216 BUG_ON(p != argp->tmpp);
219 if (defer_free(argp, kfree, p)) {
227 nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
241 READ_BUF(bmlen << 2);
253 nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval,
254 struct iattr *iattr, struct nfs4_acl **acl,
255 struct xdr_netobj *label)
257 int expected_len, len = 0;
264 if ((status = nfsd4_decode_bitmap(argp, bmval)))
268 READ32(expected_len);
270 if (bmval[0] & FATTR4_WORD0_SIZE) {
273 READ64(iattr->ia_size);
274 iattr->ia_valid |= ATTR_SIZE;
276 if (bmval[0] & FATTR4_WORD0_ACL) {
278 struct nfs4_ace *ace;
280 READ_BUF(4); len += 4;
283 if (nace > NFS4_ACL_MAX)
284 return nfserr_resource;
286 *acl = nfs4_acl_new(nace);
291 defer_free(argp, kfree, *acl);
293 (*acl)->naces = nace;
294 for (ace = (*acl)->aces; ace < (*acl)->aces + nace; ace++) {
295 READ_BUF(16); len += 16;
298 READ32(ace->access_mask);
301 len += XDR_QUADLEN(dummy32) << 2;
302 READMEM(buf, dummy32);
303 ace->whotype = nfs4_acl_get_whotype(buf, dummy32);
305 if (ace->whotype != NFS4_ACL_WHO_NAMED)
307 else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
308 status = nfsd_map_name_to_gid(argp->rqstp,
309 buf, dummy32, &ace->who_gid);
311 status = nfsd_map_name_to_uid(argp->rqstp,
312 buf, dummy32, &ace->who_uid);
318 if (bmval[1] & FATTR4_WORD1_MODE) {
321 READ32(iattr->ia_mode);
322 iattr->ia_mode &= (S_IFMT | S_IALLUGO);
323 iattr->ia_valid |= ATTR_MODE;
325 if (bmval[1] & FATTR4_WORD1_OWNER) {
330 len += (XDR_QUADLEN(dummy32) << 2);
331 READMEM(buf, dummy32);
332 if ((status = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
334 iattr->ia_valid |= ATTR_UID;
336 if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
341 len += (XDR_QUADLEN(dummy32) << 2);
342 READMEM(buf, dummy32);
343 if ((status = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
345 iattr->ia_valid |= ATTR_GID;
347 if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
352 case NFS4_SET_TO_CLIENT_TIME:
353 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
354 all 32 bits of 'nseconds'. */
357 READ64(iattr->ia_atime.tv_sec);
358 READ32(iattr->ia_atime.tv_nsec);
359 if (iattr->ia_atime.tv_nsec >= (u32)1000000000)
361 iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
363 case NFS4_SET_TO_SERVER_TIME:
364 iattr->ia_valid |= ATTR_ATIME;
370 if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
375 case NFS4_SET_TO_CLIENT_TIME:
376 /* We require the high 32 bits of 'seconds' to be 0, and we ignore
377 all 32 bits of 'nseconds'. */
380 READ64(iattr->ia_mtime.tv_sec);
381 READ32(iattr->ia_mtime.tv_nsec);
382 if (iattr->ia_mtime.tv_nsec >= (u32)1000000000)
384 iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
386 case NFS4_SET_TO_SERVER_TIME:
387 iattr->ia_valid |= ATTR_MTIME;
395 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
396 if (bmval[2] & FATTR4_WORD2_SECURITY_LABEL) {
399 READ32(dummy32); /* lfs: we don't use it */
402 READ32(dummy32); /* pi: we don't use it either */
407 if (dummy32 > NFSD4_MAX_SEC_LABEL_LEN)
408 return nfserr_badlabel;
409 len += (XDR_QUADLEN(dummy32) << 2);
410 READMEM(buf, dummy32);
411 label->data = kzalloc(dummy32 + 1, GFP_KERNEL);
413 return nfserr_jukebox;
414 defer_free(argp, kfree, label->data);
415 memcpy(label->data, buf, dummy32);
419 if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
420 || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
421 || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2)
422 READ_BUF(expected_len - len);
423 else if (len != expected_len)
429 status = nfserrno(host_err);
434 nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
438 READ_BUF(sizeof(stateid_t));
439 READ32(sid->si_generation);
440 COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
446 nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
451 READ32(access->ac_req_access);
456 static __be32 nfsd4_decode_cb_sec(struct nfsd4_compoundargs *argp, struct nfsd4_cb_sec *cbs)
464 /* callback_sec_params4 */
468 cbs->flavor = (u32)(-1);
470 /* Is this legal? Be generous, take it to mean AUTH_NONE: */
472 for (i = 0; i < nr_secflavs; ++i) {
477 /* Nothing to read */
478 if (cbs->flavor == (u32)(-1))
479 cbs->flavor = RPC_AUTH_NULL;
489 SAVEMEM(machine_name, dummy);
500 if (cbs->flavor == (u32)(-1)) {
501 kuid_t kuid = make_kuid(&init_user_ns, uid);
502 kgid_t kgid = make_kgid(&init_user_ns, gid);
503 if (uid_valid(kuid) && gid_valid(kgid)) {
506 cbs->flavor = RPC_AUTH_UNIX;
508 dprintk("RPC_AUTH_UNIX with invalid"
509 "uid or gid ignoring!\n");
514 dprintk("RPC_AUTH_GSS callback secflavor "
519 /* gcbp_handle_from_server */
522 p += XDR_QUADLEN(dummy);
523 /* gcbp_handle_from_client */
529 dprintk("Illegal callback secflavor\n");
536 static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp, struct nfsd4_backchannel_ctl *bc)
541 READ32(bc->bc_cb_program);
542 nfsd4_decode_cb_sec(argp, &bc->bc_cb_sec);
547 static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
551 READ_BUF(NFS4_MAX_SESSIONID_LEN + 8);
552 COPYMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
554 /* XXX: skipping ctsa_use_conn_in_rdma_mode. Perhaps Tom Tucker
555 * could help us figure out we should be using it. */
560 nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
565 READ32(close->cl_seqid);
566 return nfsd4_decode_stateid(argp, &close->cl_stateid);
573 nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
578 READ64(commit->co_offset);
579 READ32(commit->co_count);
585 nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
590 READ32(create->cr_type);
591 switch (create->cr_type) {
594 READ32(create->cr_linklen);
595 READ_BUF(create->cr_linklen);
596 SAVEMEM(create->cr_linkname, create->cr_linklen);
601 READ32(create->cr_specdata1);
602 READ32(create->cr_specdata2);
612 READ32(create->cr_namelen);
613 READ_BUF(create->cr_namelen);
614 SAVEMEM(create->cr_name, create->cr_namelen);
615 if ((status = check_filename(create->cr_name, create->cr_namelen)))
618 status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr,
619 &create->cr_acl, &create->cr_label);
627 nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
629 return nfsd4_decode_stateid(argp, &dr->dr_stateid);
633 nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
635 return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
639 nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
644 READ32(link->li_namelen);
645 READ_BUF(link->li_namelen);
646 SAVEMEM(link->li_name, link->li_namelen);
647 if ((status = check_filename(link->li_name, link->li_namelen)))
654 nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
659 * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
662 READ32(lock->lk_type);
663 if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
665 READ32(lock->lk_reclaim);
666 READ64(lock->lk_offset);
667 READ64(lock->lk_length);
668 READ32(lock->lk_is_new);
670 if (lock->lk_is_new) {
672 READ32(lock->lk_new_open_seqid);
673 status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
676 READ_BUF(8 + sizeof(clientid_t));
677 READ32(lock->lk_new_lock_seqid);
678 COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
679 READ32(lock->lk_new_owner.len);
680 READ_BUF(lock->lk_new_owner.len);
681 READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
683 status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
687 READ32(lock->lk_old_lock_seqid);
694 nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
699 READ32(lockt->lt_type);
700 if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
702 READ64(lockt->lt_offset);
703 READ64(lockt->lt_length);
704 COPYMEM(&lockt->lt_clientid, 8);
705 READ32(lockt->lt_owner.len);
706 READ_BUF(lockt->lt_owner.len);
707 READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
713 nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
718 READ32(locku->lu_type);
719 if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
721 READ32(locku->lu_seqid);
722 status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
726 READ64(locku->lu_offset);
727 READ64(locku->lu_length);
733 nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
738 READ32(lookup->lo_len);
739 READ_BUF(lookup->lo_len);
740 SAVEMEM(lookup->lo_name, lookup->lo_len);
741 if ((status = check_filename(lookup->lo_name, lookup->lo_len)))
747 static __be32 nfsd4_decode_share_access(struct nfsd4_compoundargs *argp, u32 *share_access, u32 *deleg_want, u32 *deleg_when)
754 *share_access = w & NFS4_SHARE_ACCESS_MASK;
755 *deleg_want = w & NFS4_SHARE_WANT_MASK;
757 *deleg_when = w & NFS4_SHARE_WHEN_MASK;
759 switch (w & NFS4_SHARE_ACCESS_MASK) {
760 case NFS4_SHARE_ACCESS_READ:
761 case NFS4_SHARE_ACCESS_WRITE:
762 case NFS4_SHARE_ACCESS_BOTH:
765 return nfserr_bad_xdr;
767 w &= ~NFS4_SHARE_ACCESS_MASK;
770 if (!argp->minorversion)
771 return nfserr_bad_xdr;
772 switch (w & NFS4_SHARE_WANT_MASK) {
773 case NFS4_SHARE_WANT_NO_PREFERENCE:
774 case NFS4_SHARE_WANT_READ_DELEG:
775 case NFS4_SHARE_WANT_WRITE_DELEG:
776 case NFS4_SHARE_WANT_ANY_DELEG:
777 case NFS4_SHARE_WANT_NO_DELEG:
778 case NFS4_SHARE_WANT_CANCEL:
781 return nfserr_bad_xdr;
783 w &= ~NFS4_SHARE_WANT_MASK;
787 if (!deleg_when) /* open_downgrade */
790 case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
791 case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
792 case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
793 NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
797 return nfserr_bad_xdr;
800 static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
806 /* Note: unlinke access bits, deny bits may be zero. */
807 if (*x & ~NFS4_SHARE_DENY_BOTH)
808 return nfserr_bad_xdr;
811 return nfserr_bad_xdr;
814 static __be32 nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
821 if (o->len == 0 || o->len > NFS4_OPAQUE_LIMIT)
822 return nfserr_bad_xdr;
825 SAVEMEM(o->data, o->len);
828 return nfserr_bad_xdr;
832 nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
837 memset(open->op_bmval, 0, sizeof(open->op_bmval));
838 open->op_iattr.ia_valid = 0;
839 open->op_openowner = NULL;
841 open->op_xdr_error = 0;
842 /* seqid, share_access, share_deny, clientid, ownerlen */
844 READ32(open->op_seqid);
845 /* decode, yet ignore deleg_when until supported */
846 status = nfsd4_decode_share_access(argp, &open->op_share_access,
847 &open->op_deleg_want, &dummy);
850 status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
853 READ_BUF(sizeof(clientid_t));
854 COPYMEM(&open->op_clientid, sizeof(clientid_t));
855 status = nfsd4_decode_opaque(argp, &open->op_owner);
859 READ32(open->op_create);
860 switch (open->op_create) {
861 case NFS4_OPEN_NOCREATE:
863 case NFS4_OPEN_CREATE:
865 READ32(open->op_createmode);
866 switch (open->op_createmode) {
867 case NFS4_CREATE_UNCHECKED:
868 case NFS4_CREATE_GUARDED:
869 status = nfsd4_decode_fattr(argp, open->op_bmval,
870 &open->op_iattr, &open->op_acl, &open->op_label);
874 case NFS4_CREATE_EXCLUSIVE:
875 READ_BUF(NFS4_VERIFIER_SIZE);
876 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
878 case NFS4_CREATE_EXCLUSIVE4_1:
879 if (argp->minorversion < 1)
881 READ_BUF(NFS4_VERIFIER_SIZE);
882 COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
883 status = nfsd4_decode_fattr(argp, open->op_bmval,
884 &open->op_iattr, &open->op_acl, &open->op_label);
898 READ32(open->op_claim_type);
899 switch (open->op_claim_type) {
900 case NFS4_OPEN_CLAIM_NULL:
901 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
903 READ32(open->op_fname.len);
904 READ_BUF(open->op_fname.len);
905 SAVEMEM(open->op_fname.data, open->op_fname.len);
906 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
909 case NFS4_OPEN_CLAIM_PREVIOUS:
911 READ32(open->op_delegate_type);
913 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
914 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
918 READ32(open->op_fname.len);
919 READ_BUF(open->op_fname.len);
920 SAVEMEM(open->op_fname.data, open->op_fname.len);
921 if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
924 case NFS4_OPEN_CLAIM_FH:
925 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
926 if (argp->minorversion < 1)
930 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
931 if (argp->minorversion < 1)
933 status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
945 nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
949 status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
953 READ32(open_conf->oc_seqid);
959 nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
963 status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
967 READ32(open_down->od_seqid);
968 status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
969 &open_down->od_deleg_want, NULL);
972 status = nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
979 nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
984 READ32(putfh->pf_fhlen);
985 if (putfh->pf_fhlen > NFS4_FHSIZE)
987 READ_BUF(putfh->pf_fhlen);
988 SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
994 nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
998 status = nfsd4_decode_stateid(argp, &read->rd_stateid);
1002 READ64(read->rd_offset);
1003 READ32(read->rd_length);
1009 nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
1014 READ64(readdir->rd_cookie);
1015 COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
1016 READ32(readdir->rd_dircount); /* just in case you needed a useless field... */
1017 READ32(readdir->rd_maxcount);
1018 if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
1025 nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
1030 READ32(remove->rm_namelen);
1031 READ_BUF(remove->rm_namelen);
1032 SAVEMEM(remove->rm_name, remove->rm_namelen);
1033 if ((status = check_filename(remove->rm_name, remove->rm_namelen)))
1040 nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
1045 READ32(rename->rn_snamelen);
1046 READ_BUF(rename->rn_snamelen + 4);
1047 SAVEMEM(rename->rn_sname, rename->rn_snamelen);
1048 READ32(rename->rn_tnamelen);
1049 READ_BUF(rename->rn_tnamelen);
1050 SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
1051 if ((status = check_filename(rename->rn_sname, rename->rn_snamelen)))
1053 if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen)))
1060 nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
1064 READ_BUF(sizeof(clientid_t));
1065 COPYMEM(clientid, sizeof(clientid_t));
1071 nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
1072 struct nfsd4_secinfo *secinfo)
1077 READ32(secinfo->si_namelen);
1078 READ_BUF(secinfo->si_namelen);
1079 SAVEMEM(secinfo->si_name, secinfo->si_namelen);
1080 status = check_filename(secinfo->si_name, secinfo->si_namelen);
1087 nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
1088 struct nfsd4_secinfo_no_name *sin)
1093 READ32(sin->sin_style);
1098 nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
1102 status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
1105 return nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr,
1106 &setattr->sa_acl, &setattr->sa_label);
1110 nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
1114 READ_BUF(NFS4_VERIFIER_SIZE);
1115 COPYMEM(setclientid->se_verf.data, NFS4_VERIFIER_SIZE);
1117 status = nfsd4_decode_opaque(argp, &setclientid->se_name);
1119 return nfserr_bad_xdr;
1121 READ32(setclientid->se_callback_prog);
1122 READ32(setclientid->se_callback_netid_len);
1124 READ_BUF(setclientid->se_callback_netid_len + 4);
1125 SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
1126 READ32(setclientid->se_callback_addr_len);
1128 READ_BUF(setclientid->se_callback_addr_len + 4);
1129 SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
1130 READ32(setclientid->se_callback_ident);
1136 nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
1140 READ_BUF(8 + NFS4_VERIFIER_SIZE);
1141 COPYMEM(&scd_c->sc_clientid, 8);
1142 COPYMEM(&scd_c->sc_confirm, NFS4_VERIFIER_SIZE);
1147 /* Also used for NVERIFY */
1149 nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
1153 if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
1156 /* For convenience's sake, we compare raw xdr'd attributes in
1157 * nfsd4_proc_verify */
1160 READ32(verify->ve_attrlen);
1161 READ_BUF(verify->ve_attrlen);
1162 SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
1168 nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
1174 status = nfsd4_decode_stateid(argp, &write->wr_stateid);
1178 READ64(write->wr_offset);
1179 READ32(write->wr_stable_how);
1180 if (write->wr_stable_how > 2)
1182 READ32(write->wr_buflen);
1184 /* Sorry .. no magic macros for this.. *
1185 * READ_BUF(write->wr_buflen);
1186 * SAVEMEM(write->wr_buf, write->wr_buflen);
1188 avail = (char*)argp->end - (char*)argp->p;
1189 if (avail + argp->pagelen < write->wr_buflen) {
1190 dprintk("NFSD: xdr error (%s:%d)\n",
1191 __FILE__, __LINE__);
1194 write->wr_head.iov_base = p;
1195 write->wr_head.iov_len = avail;
1196 WARN_ON(avail != (XDR_QUADLEN(avail) << 2));
1197 write->wr_pagelist = argp->pagelist;
1199 len = XDR_QUADLEN(write->wr_buflen) << 2;
1205 pages = len >> PAGE_SHIFT;
1206 argp->pagelist += pages;
1207 argp->pagelen -= pages * PAGE_SIZE;
1208 len -= pages * PAGE_SIZE;
1210 argp->p = (__be32 *)page_address(argp->pagelist[0]);
1211 argp->end = argp->p + XDR_QUADLEN(PAGE_SIZE);
1213 argp->p += XDR_QUADLEN(len);
1219 nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
1224 COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
1225 READ32(rlockowner->rl_owner.len);
1226 READ_BUF(rlockowner->rl_owner.len);
1227 READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
1229 if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
1230 return nfserr_inval;
1235 nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
1236 struct nfsd4_exchange_id *exid)
1241 READ_BUF(NFS4_VERIFIER_SIZE);
1242 COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
1244 status = nfsd4_decode_opaque(argp, &exid->clname);
1246 return nfserr_bad_xdr;
1249 READ32(exid->flags);
1251 /* Ignore state_protect4_a */
1253 READ32(exid->spa_how);
1254 switch (exid->spa_how) {
1258 /* spo_must_enforce */
1261 READ_BUF(dummy * 4);
1264 /* spo_must_allow */
1267 READ_BUF(dummy * 4);
1274 READ_BUF(dummy * 4);
1279 READ_BUF(dummy * 4);
1282 /* ssp_hash_algs<> */
1289 p += XDR_QUADLEN(dummy);
1292 /* ssp_encr_algs<> */
1299 p += XDR_QUADLEN(dummy);
1302 /* ssp_window and ssp_num_gss_handles */
1311 /* Ignore Implementation ID */
1312 READ_BUF(4); /* nfs_impl_id4 array length */
1323 p += XDR_QUADLEN(dummy);
1329 p += XDR_QUADLEN(dummy);
1339 nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
1340 struct nfsd4_create_session *sess)
1346 COPYMEM(&sess->clientid, 8);
1347 READ32(sess->seqid);
1348 READ32(sess->flags);
1350 /* Fore channel attrs */
1352 READ32(dummy); /* headerpadsz is always 0 */
1353 READ32(sess->fore_channel.maxreq_sz);
1354 READ32(sess->fore_channel.maxresp_sz);
1355 READ32(sess->fore_channel.maxresp_cached);
1356 READ32(sess->fore_channel.maxops);
1357 READ32(sess->fore_channel.maxreqs);
1358 READ32(sess->fore_channel.nr_rdma_attrs);
1359 if (sess->fore_channel.nr_rdma_attrs == 1) {
1361 READ32(sess->fore_channel.rdma_attrs);
1362 } else if (sess->fore_channel.nr_rdma_attrs > 1) {
1363 dprintk("Too many fore channel attr bitmaps!\n");
1367 /* Back channel attrs */
1369 READ32(dummy); /* headerpadsz is always 0 */
1370 READ32(sess->back_channel.maxreq_sz);
1371 READ32(sess->back_channel.maxresp_sz);
1372 READ32(sess->back_channel.maxresp_cached);
1373 READ32(sess->back_channel.maxops);
1374 READ32(sess->back_channel.maxreqs);
1375 READ32(sess->back_channel.nr_rdma_attrs);
1376 if (sess->back_channel.nr_rdma_attrs == 1) {
1378 READ32(sess->back_channel.rdma_attrs);
1379 } else if (sess->back_channel.nr_rdma_attrs > 1) {
1380 dprintk("Too many back channel attr bitmaps!\n");
1385 READ32(sess->callback_prog);
1386 nfsd4_decode_cb_sec(argp, &sess->cb_sec);
1391 nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
1392 struct nfsd4_destroy_session *destroy_session)
1395 READ_BUF(NFS4_MAX_SESSIONID_LEN);
1396 COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1402 nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
1403 struct nfsd4_free_stateid *free_stateid)
1407 READ_BUF(sizeof(stateid_t));
1408 READ32(free_stateid->fr_stateid.si_generation);
1409 COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
1415 nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
1416 struct nfsd4_sequence *seq)
1420 READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
1421 COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
1423 READ32(seq->slotid);
1424 READ32(seq->maxslots);
1425 READ32(seq->cachethis);
1431 nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
1435 struct nfsd4_test_stateid_id *stateid;
1438 test_stateid->ts_num_ids = ntohl(*p++);
1440 INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
1442 for (i = 0; i < test_stateid->ts_num_ids; i++) {
1443 stateid = kmalloc(sizeof(struct nfsd4_test_stateid_id), GFP_KERNEL);
1445 status = nfserrno(-ENOMEM);
1449 defer_free(argp, kfree, stateid);
1450 INIT_LIST_HEAD(&stateid->ts_id_list);
1451 list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
1453 status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
1462 dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
1463 status = nfserr_bad_xdr;
1467 static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
1472 COPYMEM(&dc->clientid, 8);
1477 static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
1482 READ32(rc->rca_one_fs);
1488 nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
1494 nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
1496 return nfserr_notsupp;
1499 typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
1501 static nfsd4_dec nfsd4_dec_ops[] = {
1502 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1503 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1504 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1505 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1506 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1507 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1508 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1509 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1510 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1511 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1512 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1513 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1514 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1515 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1516 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1517 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1518 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1519 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
1520 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1521 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
1522 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_noop,
1523 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1524 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1525 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1526 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1527 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1528 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1529 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
1530 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1531 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1532 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1533 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1534 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
1535 [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
1536 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1537 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1538 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
1541 static nfsd4_dec nfsd41_dec_ops[] = {
1542 [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
1543 [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
1544 [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
1545 [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
1546 [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
1547 [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
1548 [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
1549 [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
1550 [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
1551 [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
1552 [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
1553 [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
1554 [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
1555 [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
1556 [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1557 [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
1558 [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
1559 [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_notsupp,
1560 [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
1561 [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
1562 [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_notsupp,
1563 [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
1564 [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
1565 [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
1566 [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
1567 [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
1568 [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
1569 [OP_RENEW] = (nfsd4_dec)nfsd4_decode_notsupp,
1570 [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
1571 [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
1572 [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
1573 [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
1574 [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_notsupp,
1575 [OP_SETCLIENTID_CONFIRM]= (nfsd4_dec)nfsd4_decode_notsupp,
1576 [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
1577 [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
1578 [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_notsupp,
1580 /* new operations for NFSv4.1 */
1581 [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
1582 [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
1583 [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
1584 [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
1585 [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
1586 [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
1587 [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
1588 [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
1589 [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
1590 [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
1591 [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
1592 [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
1593 [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
1594 [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
1595 [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
1596 [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
1597 [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
1598 [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
1599 [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
1602 struct nfsd4_minorversion_ops {
1603 nfsd4_dec *decoders;
1607 static struct nfsd4_minorversion_ops nfsd4_minorversion[] = {
1608 [0] = { nfsd4_dec_ops, ARRAY_SIZE(nfsd4_dec_ops) },
1609 [1] = { nfsd41_dec_ops, ARRAY_SIZE(nfsd41_dec_ops) },
1610 [2] = { nfsd41_dec_ops, ARRAY_SIZE(nfsd41_dec_ops) },
1614 nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
1617 struct nfsd4_op *op;
1618 struct nfsd4_minorversion_ops *ops;
1619 bool cachethis = false;
1623 READ32(argp->taglen);
1624 READ_BUF(argp->taglen + 8);
1625 SAVEMEM(argp->tag, argp->taglen);
1626 READ32(argp->minorversion);
1627 READ32(argp->opcnt);
1629 if (argp->taglen > NFSD4_MAX_TAGLEN)
1631 if (argp->opcnt > 100)
1634 if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
1635 argp->ops = kmalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
1637 argp->ops = argp->iops;
1638 dprintk("nfsd: couldn't allocate room for COMPOUND\n");
1643 if (argp->minorversion >= ARRAY_SIZE(nfsd4_minorversion))
1646 ops = &nfsd4_minorversion[argp->minorversion];
1647 for (i = 0; i < argp->opcnt; i++) {
1654 if (op->opnum >= FIRST_NFS4_OP && op->opnum <= LAST_NFS4_OP)
1655 op->status = ops->decoders[op->opnum](argp, &op->u);
1657 op->opnum = OP_ILLEGAL;
1658 op->status = nfserr_op_illegal;
1666 * We'll try to cache the result in the DRC if any one
1667 * op in the compound wants to be cached:
1669 cachethis |= nfsd4_cache_this_op(op);
1671 /* Sessions make the DRC unnecessary: */
1672 if (argp->minorversion)
1674 argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
1679 #define WRITE32(n) *p++ = htonl(n)
1680 #define WRITE64(n) do { \
1681 *p++ = htonl((u32)((n) >> 32)); \
1682 *p++ = htonl((u32)(n)); \
1684 #define WRITEMEM(ptr,nbytes) do { if (nbytes > 0) { \
1685 *(p + XDR_QUADLEN(nbytes) -1) = 0; \
1686 memcpy(p, ptr, nbytes); \
1687 p += XDR_QUADLEN(nbytes); \
1690 static void write32(__be32 **p, u32 n)
1695 static void write64(__be32 **p, u64 n)
1697 write32(p, (n >> 32));
1701 static void write_change(__be32 **p, struct kstat *stat, struct inode *inode)
1703 if (IS_I_VERSION(inode)) {
1704 write64(p, inode->i_version);
1706 write32(p, stat->ctime.tv_sec);
1707 write32(p, stat->ctime.tv_nsec);
1711 static void write_cinfo(__be32 **p, struct nfsd4_change_info *c)
1713 write32(p, c->atomic);
1714 if (c->change_supported) {
1715 write64(p, c->before_change);
1716 write64(p, c->after_change);
1718 write32(p, c->before_ctime_sec);
1719 write32(p, c->before_ctime_nsec);
1720 write32(p, c->after_ctime_sec);
1721 write32(p, c->after_ctime_nsec);
1725 #define RESERVE_SPACE(nbytes) do { \
1727 BUG_ON(p + XDR_QUADLEN(nbytes) > resp->end); \
1729 #define ADJUST_ARGS() resp->p = p
1731 /* Encode as an array of strings the string given with components
1732 * separated @sep, escaped with esc_enter and esc_exit.
1734 static __be32 nfsd4_encode_components_esc(char sep, char *components,
1735 __be32 **pp, int *buflen,
1736 char esc_enter, char esc_exit)
1740 int strlen, count=0;
1741 char *str, *end, *next;
1743 dprintk("nfsd4_encode_components(%s)\n", components);
1744 if ((*buflen -= 4) < 0)
1745 return nfserr_resource;
1746 WRITE32(0); /* We will fill this in with @count later */
1747 end = str = components;
1749 bool found_esc = false;
1751 /* try to parse as esc_start, ..., esc_end, sep */
1752 if (*str == esc_enter) {
1753 for (; *end && (*end != esc_exit); end++)
1754 /* find esc_exit or end of string */;
1756 if (*end && (!*next || *next == sep)) {
1763 for (; *end && (*end != sep); end++)
1764 /* find sep or end of string */;
1768 if ((*buflen -= ((XDR_QUADLEN(strlen) << 2) + 4)) < 0)
1769 return nfserr_resource;
1771 WRITEMEM(str, strlen);
1784 /* Encode as an array of strings the string given with components
1787 static __be32 nfsd4_encode_components(char sep, char *components,
1788 __be32 **pp, int *buflen)
1790 return nfsd4_encode_components_esc(sep, components, pp, buflen, 0, 0);
1794 * encode a location element of a fs_locations structure
1796 static __be32 nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
1797 __be32 **pp, int *buflen)
1802 status = nfsd4_encode_components_esc(':', location->hosts, &p, buflen,
1806 status = nfsd4_encode_components('/', location->path, &p, buflen);
1814 * Encode a path in RFC3530 'pathname4' format
1816 static __be32 nfsd4_encode_path(const struct path *root,
1817 const struct path *path, __be32 **pp, int *buflen)
1821 .dentry = path->dentry,
1824 struct dentry **components = NULL;
1825 unsigned int ncomponents = 0;
1826 __be32 err = nfserr_jukebox;
1828 dprintk("nfsd4_encode_components(");
1831 /* First walk the path up to the nfsd root, and store the
1832 * dentries/path components in an array.
1835 if (cur.dentry == root->dentry && cur.mnt == root->mnt)
1837 if (cur.dentry == cur.mnt->mnt_root) {
1838 if (follow_up(&cur))
1842 if ((ncomponents & 15) == 0) {
1843 struct dentry **new;
1844 new = krealloc(components,
1845 sizeof(*new) * (ncomponents + 16),
1851 components[ncomponents++] = cur.dentry;
1852 cur.dentry = dget_parent(cur.dentry);
1858 WRITE32(ncomponents);
1860 while (ncomponents) {
1861 struct dentry *dentry = components[ncomponents - 1];
1862 unsigned int len = dentry->d_name.len;
1864 *buflen -= 4 + (XDR_QUADLEN(len) << 2);
1868 WRITEMEM(dentry->d_name.name, len);
1869 dprintk("/%s", dentry->d_name.name);
1879 dput(components[--ncomponents]);
1885 static __be32 nfsd4_encode_fsloc_fsroot(struct svc_rqst *rqstp,
1886 const struct path *path, __be32 **pp, int *buflen)
1888 struct svc_export *exp_ps;
1891 exp_ps = rqst_find_fsidzero_export(rqstp);
1893 return nfserrno(PTR_ERR(exp_ps));
1894 res = nfsd4_encode_path(&exp_ps->ex_path, path, pp, buflen);
1900 * encode a fs_locations structure
1902 static __be32 nfsd4_encode_fs_locations(struct svc_rqst *rqstp,
1903 struct svc_export *exp,
1904 __be32 **pp, int *buflen)
1909 struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
1911 status = nfsd4_encode_fsloc_fsroot(rqstp, &exp->ex_path, &p, buflen);
1914 if ((*buflen -= 4) < 0)
1915 return nfserr_resource;
1916 WRITE32(fslocs->locations_count);
1917 for (i=0; i<fslocs->locations_count; i++) {
1918 status = nfsd4_encode_fs_location4(&fslocs->locations[i],
1927 static u32 nfs4_file_type(umode_t mode)
1929 switch (mode & S_IFMT) {
1930 case S_IFIFO: return NF4FIFO;
1931 case S_IFCHR: return NF4CHR;
1932 case S_IFDIR: return NF4DIR;
1933 case S_IFBLK: return NF4BLK;
1934 case S_IFLNK: return NF4LNK;
1935 case S_IFREG: return NF4REG;
1936 case S_IFSOCK: return NF4SOCK;
1937 default: return NF4BAD;
1942 nfsd4_encode_name(struct svc_rqst *rqstp, int whotype, kuid_t uid, kgid_t gid,
1943 __be32 **p, int *buflen)
1947 if (*buflen < (XDR_QUADLEN(IDMAP_NAMESZ) << 2) + 4)
1948 return nfserr_resource;
1949 if (whotype != NFS4_ACL_WHO_NAMED)
1950 status = nfs4_acl_write_who(whotype, (u8 *)(*p + 1));
1951 else if (gid_valid(gid))
1952 status = nfsd_map_gid_to_name(rqstp, gid, (u8 *)(*p + 1));
1954 status = nfsd_map_uid_to_name(rqstp, uid, (u8 *)(*p + 1));
1956 return nfserrno(status);
1957 *p = xdr_encode_opaque(*p, NULL, status);
1958 *buflen -= (XDR_QUADLEN(status) << 2) + 4;
1959 BUG_ON(*buflen < 0);
1963 static inline __be32
1964 nfsd4_encode_user(struct svc_rqst *rqstp, kuid_t user, __be32 **p, int *buflen)
1966 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, user, INVALID_GID,
1970 static inline __be32
1971 nfsd4_encode_group(struct svc_rqst *rqstp, kgid_t group, __be32 **p, int *buflen)
1973 return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, INVALID_UID, group,
1977 static inline __be32
1978 nfsd4_encode_aclname(struct svc_rqst *rqstp, struct nfs4_ace *ace,
1979 __be32 **p, int *buflen)
1981 kuid_t uid = INVALID_UID;
1982 kgid_t gid = INVALID_GID;
1984 if (ace->whotype == NFS4_ACL_WHO_NAMED) {
1985 if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
1990 return nfsd4_encode_name(rqstp, ace->whotype, uid, gid, p, buflen);
1993 #define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
1994 FATTR4_WORD0_RDATTR_ERROR)
1995 #define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
1997 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
1998 static inline __be32
1999 nfsd4_encode_security_label(struct svc_rqst *rqstp, void *context, int len, __be32 **pp, int *buflen)
2003 if (*buflen < ((XDR_QUADLEN(len) << 2) + 4 + 4 + 4))
2004 return nfserr_resource;
2007 * For now we use a 0 here to indicate the null translation; in
2008 * the future we may place a call to translation code here.
2010 if ((*buflen -= 8) < 0)
2011 return nfserr_resource;
2013 WRITE32(0); /* lfs */
2014 WRITE32(0); /* pi */
2015 p = xdr_encode_opaque(p, context, len);
2016 *buflen -= (XDR_QUADLEN(len) << 2) + 4;
2022 static inline __be32
2023 nfsd4_encode_security_label(struct svc_rqst *rqstp, void *context, int len, __be32 **pp, int *buflen)
2027 static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
2029 /* As per referral draft: */
2030 if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
2031 *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
2032 if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
2033 *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
2034 *rdattr_err = NFSERR_MOVED;
2036 return nfserr_moved;
2038 *bmval0 &= WORD0_ABSENT_FS_ATTRS;
2039 *bmval1 &= WORD1_ABSENT_FS_ATTRS;
2044 static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
2046 struct path path = exp->ex_path;
2050 while (follow_up(&path)) {
2051 if (path.dentry != path.mnt->mnt_root)
2054 err = vfs_getattr(&path, stat);
2060 * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
2063 * countp is the buffer size in _words_
2066 nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
2067 struct dentry *dentry, __be32 **buffer, int count, u32 *bmval,
2068 struct svc_rqst *rqstp, int ignore_crossmnt)
2070 u32 bmval0 = bmval[0];
2071 u32 bmval1 = bmval[1];
2072 u32 bmval2 = bmval[2];
2074 struct svc_fh tempfh;
2075 struct kstatfs statfs;
2076 int buflen = count << 2;
2081 __be32 *p = *buffer;
2085 struct nfs4_acl *acl = NULL;
2086 void *context = NULL;
2088 bool contextsupport = false;
2089 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2090 u32 minorversion = resp->cstate.minorversion;
2091 struct path path = {
2092 .mnt = exp->ex_path.mnt,
2095 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2097 BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
2098 BUG_ON(bmval0 & ~nfsd_suppattrs0(minorversion));
2099 BUG_ON(bmval1 & ~nfsd_suppattrs1(minorversion));
2100 BUG_ON(bmval2 & ~nfsd_suppattrs2(minorversion));
2102 if (exp->ex_fslocs.migrated) {
2104 status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
2109 err = vfs_getattr(&path, &stat);
2112 if ((bmval0 & (FATTR4_WORD0_FILES_FREE | FATTR4_WORD0_FILES_TOTAL |
2113 FATTR4_WORD0_MAXNAME)) ||
2114 (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
2115 FATTR4_WORD1_SPACE_TOTAL))) {
2116 err = vfs_statfs(&path, &statfs);
2120 if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
2121 fh_init(&tempfh, NFS4_FHSIZE);
2122 status = fh_compose(&tempfh, exp, dentry, NULL);
2127 if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
2128 | FATTR4_WORD0_SUPPORTED_ATTRS)) {
2129 err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
2130 aclsupport = (err == 0);
2131 if (bmval0 & FATTR4_WORD0_ACL) {
2132 if (err == -EOPNOTSUPP)
2133 bmval0 &= ~FATTR4_WORD0_ACL;
2134 else if (err == -EINVAL) {
2135 status = nfserr_attrnotsupp;
2137 } else if (err != 0)
2142 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2143 if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) ||
2144 bmval[0] & FATTR4_WORD0_SUPPORTED_ATTRS) {
2145 err = security_inode_getsecctx(dentry->d_inode,
2146 &context, &contextlen);
2147 contextsupport = (err == 0);
2148 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2149 if (err == -EOPNOTSUPP)
2150 bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2155 #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2158 if ((buflen -= 16) < 0)
2164 } else if (bmval1) {
2165 if ((buflen -= 12) < 0)
2171 if ((buflen -= 8) < 0)
2176 attrlenp = p++; /* to be backfilled later */
2178 if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
2179 u32 word0 = nfsd_suppattrs0(minorversion);
2180 u32 word1 = nfsd_suppattrs1(minorversion);
2181 u32 word2 = nfsd_suppattrs2(minorversion);
2184 word0 &= ~FATTR4_WORD0_ACL;
2185 if (!contextsupport)
2186 word2 &= ~FATTR4_WORD2_SECURITY_LABEL;
2188 if ((buflen -= 12) < 0)
2194 if ((buflen -= 16) < 0)
2202 if (bmval0 & FATTR4_WORD0_TYPE) {
2203 if ((buflen -= 4) < 0)
2205 dummy = nfs4_file_type(stat.mode);
2206 if (dummy == NF4BAD)
2207 goto out_serverfault;
2210 if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
2211 if ((buflen -= 4) < 0)
2213 if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
2214 WRITE32(NFS4_FH_PERSISTENT);
2216 WRITE32(NFS4_FH_PERSISTENT|NFS4_FH_VOL_RENAME);
2218 if (bmval0 & FATTR4_WORD0_CHANGE) {
2219 if ((buflen -= 8) < 0)
2221 write_change(&p, &stat, dentry->d_inode);
2223 if (bmval0 & FATTR4_WORD0_SIZE) {
2224 if ((buflen -= 8) < 0)
2228 if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
2229 if ((buflen -= 4) < 0)
2233 if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
2234 if ((buflen -= 4) < 0)
2238 if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
2239 if ((buflen -= 4) < 0)
2243 if (bmval0 & FATTR4_WORD0_FSID) {
2244 if ((buflen -= 16) < 0)
2246 if (exp->ex_fslocs.migrated) {
2247 WRITE64(NFS4_REFERRAL_FSID_MAJOR);
2248 WRITE64(NFS4_REFERRAL_FSID_MINOR);
2249 } else switch(fsid_source(fhp)) {
2250 case FSIDSOURCE_FSID:
2251 WRITE64((u64)exp->ex_fsid);
2254 case FSIDSOURCE_DEV:
2256 WRITE32(MAJOR(stat.dev));
2258 WRITE32(MINOR(stat.dev));
2260 case FSIDSOURCE_UUID:
2261 WRITEMEM(exp->ex_uuid, 16);
2265 if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
2266 if ((buflen -= 4) < 0)
2270 if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
2271 if ((buflen -= 4) < 0)
2273 WRITE32(nn->nfsd4_lease);
2275 if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
2276 if ((buflen -= 4) < 0)
2278 WRITE32(rdattr_err);
2280 if (bmval0 & FATTR4_WORD0_ACL) {
2281 struct nfs4_ace *ace;
2284 if ((buflen -= 4) < 0)
2290 if ((buflen -= 4) < 0)
2292 WRITE32(acl->naces);
2294 for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
2295 if ((buflen -= 4*3) < 0)
2299 WRITE32(ace->access_mask & NFS4_ACE_MASK_ALL);
2300 status = nfsd4_encode_aclname(rqstp, ace, &p, &buflen);
2301 if (status == nfserr_resource)
2308 if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
2309 if ((buflen -= 4) < 0)
2311 WRITE32(aclsupport ?
2312 ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
2314 if (bmval0 & FATTR4_WORD0_CANSETTIME) {
2315 if ((buflen -= 4) < 0)
2319 if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
2320 if ((buflen -= 4) < 0)
2324 if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
2325 if ((buflen -= 4) < 0)
2329 if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
2330 if ((buflen -= 4) < 0)
2334 if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
2335 buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
2338 WRITE32(fhp->fh_handle.fh_size);
2339 WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
2341 if (bmval0 & FATTR4_WORD0_FILEID) {
2342 if ((buflen -= 8) < 0)
2346 if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
2347 if ((buflen -= 8) < 0)
2349 WRITE64((u64) statfs.f_ffree);
2351 if (bmval0 & FATTR4_WORD0_FILES_FREE) {
2352 if ((buflen -= 8) < 0)
2354 WRITE64((u64) statfs.f_ffree);
2356 if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
2357 if ((buflen -= 8) < 0)
2359 WRITE64((u64) statfs.f_files);
2361 if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
2362 status = nfsd4_encode_fs_locations(rqstp, exp, &p, &buflen);
2363 if (status == nfserr_resource)
2368 if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
2369 if ((buflen -= 4) < 0)
2373 if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
2374 if ((buflen -= 8) < 0)
2378 if (bmval0 & FATTR4_WORD0_MAXLINK) {
2379 if ((buflen -= 4) < 0)
2383 if (bmval0 & FATTR4_WORD0_MAXNAME) {
2384 if ((buflen -= 4) < 0)
2386 WRITE32(statfs.f_namelen);
2388 if (bmval0 & FATTR4_WORD0_MAXREAD) {
2389 if ((buflen -= 8) < 0)
2391 WRITE64((u64) svc_max_payload(rqstp));
2393 if (bmval0 & FATTR4_WORD0_MAXWRITE) {
2394 if ((buflen -= 8) < 0)
2396 WRITE64((u64) svc_max_payload(rqstp));
2398 if (bmval1 & FATTR4_WORD1_MODE) {
2399 if ((buflen -= 4) < 0)
2401 WRITE32(stat.mode & S_IALLUGO);
2403 if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
2404 if ((buflen -= 4) < 0)
2408 if (bmval1 & FATTR4_WORD1_NUMLINKS) {
2409 if ((buflen -= 4) < 0)
2411 WRITE32(stat.nlink);
2413 if (bmval1 & FATTR4_WORD1_OWNER) {
2414 status = nfsd4_encode_user(rqstp, stat.uid, &p, &buflen);
2415 if (status == nfserr_resource)
2420 if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
2421 status = nfsd4_encode_group(rqstp, stat.gid, &p, &buflen);
2422 if (status == nfserr_resource)
2427 if (bmval1 & FATTR4_WORD1_RAWDEV) {
2428 if ((buflen -= 8) < 0)
2430 WRITE32((u32) MAJOR(stat.rdev));
2431 WRITE32((u32) MINOR(stat.rdev));
2433 if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
2434 if ((buflen -= 8) < 0)
2436 dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
2439 if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
2440 if ((buflen -= 8) < 0)
2442 dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
2445 if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
2446 if ((buflen -= 8) < 0)
2448 dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
2451 if (bmval1 & FATTR4_WORD1_SPACE_USED) {
2452 if ((buflen -= 8) < 0)
2454 dummy64 = (u64)stat.blocks << 9;
2457 if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
2458 if ((buflen -= 12) < 0)
2460 WRITE64((s64)stat.atime.tv_sec);
2461 WRITE32(stat.atime.tv_nsec);
2463 if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
2464 if ((buflen -= 12) < 0)
2470 if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
2471 if ((buflen -= 12) < 0)
2473 WRITE64((s64)stat.ctime.tv_sec);
2474 WRITE32(stat.ctime.tv_nsec);
2476 if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
2477 if ((buflen -= 12) < 0)
2479 WRITE64((s64)stat.mtime.tv_sec);
2480 WRITE32(stat.mtime.tv_nsec);
2482 if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
2483 if ((buflen -= 8) < 0)
2486 * Get parent's attributes if not ignoring crossmount
2487 * and this is the root of a cross-mounted filesystem.
2489 if (ignore_crossmnt == 0 &&
2490 dentry == exp->ex_path.mnt->mnt_root)
2491 get_parent_attributes(exp, &stat);
2494 if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
2495 status = nfsd4_encode_security_label(rqstp, context,
2496 contextlen, &p, &buflen);
2500 if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
2502 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD0);
2503 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD1);
2504 WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD2);
2507 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2512 #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
2514 security_release_secctx(context, contextlen);
2515 #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
2521 status = nfserrno(err);
2524 status = nfserr_resource;
2527 status = nfserr_serverfault;
2531 static inline int attributes_need_mount(u32 *bmval)
2533 if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
2535 if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
2541 nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
2542 const char *name, int namlen, __be32 **p, int buflen)
2544 struct svc_export *exp = cd->rd_fhp->fh_export;
2545 struct dentry *dentry;
2547 int ignore_crossmnt = 0;
2549 dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
2551 return nfserrno(PTR_ERR(dentry));
2552 if (!dentry->d_inode) {
2554 * nfsd_buffered_readdir drops the i_mutex between
2555 * readdir and calling this callback, leaving a window
2556 * where this directory entry could have gone away.
2559 return nfserr_noent;
2564 * In the case of a mountpoint, the client may be asking for
2565 * attributes that are only properties of the underlying filesystem
2566 * as opposed to the cross-mounted file system. In such a case,
2567 * we will not follow the cross mount and will fill the attribtutes
2568 * directly from the mountpoint dentry.
2570 if (nfsd_mountpoint(dentry, exp)) {
2573 if (!(exp->ex_flags & NFSEXP_V4ROOT)
2574 && !attributes_need_mount(cd->rd_bmval)) {
2575 ignore_crossmnt = 1;
2579 * Why the heck aren't we just using nfsd_lookup??
2580 * Different "."/".." handling? Something else?
2581 * At least, add a comment here to explain....
2583 err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
2585 nfserr = nfserrno(err);
2588 nfserr = check_nfsd_access(exp, cd->rd_rqstp);
2594 nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
2595 cd->rd_rqstp, ignore_crossmnt);
2603 nfsd4_encode_rdattr_error(__be32 *p, int buflen, __be32 nfserr)
2610 *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
2611 *p++ = htonl(0); /* bmval1 */
2614 *p++ = nfserr; /* no htonl */
2615 *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
2620 nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
2621 loff_t offset, u64 ino, unsigned int d_type)
2623 struct readdir_cd *ccd = ccdv;
2624 struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
2626 __be32 *p = cd->buffer;
2628 __be32 nfserr = nfserr_toosmall;
2630 /* In nfsv4, "." and ".." never make it onto the wire.. */
2631 if (name && isdotent(name, namlen)) {
2632 cd->common.err = nfs_ok;
2637 xdr_encode_hyper(cd->offset, (u64) offset);
2639 buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
2643 *p++ = xdr_one; /* mark entry present */
2645 p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
2646 p = xdr_encode_array(p, name, namlen); /* name length & name */
2648 nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, &p, buflen);
2652 case nfserr_resource:
2653 nfserr = nfserr_toosmall;
2659 * If the client requested the RDATTR_ERROR attribute,
2660 * we stuff the error code into this attribute
2661 * and continue. If this attribute was not requested,
2662 * then in accordance with the spec, we fail the
2663 * entire READDIR operation(!)
2665 if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
2667 p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
2669 nfserr = nfserr_toosmall;
2673 cd->buflen -= (p - cd->buffer);
2675 cd->offset = cookiep;
2677 cd->common.err = nfs_ok;
2680 cd->common.err = nfserr;
2685 nfsd4_encode_stateid(struct nfsd4_compoundres *resp, stateid_t *sid)
2689 RESERVE_SPACE(sizeof(stateid_t));
2690 WRITE32(sid->si_generation);
2691 WRITEMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
2696 nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
2702 WRITE32(access->ac_supported);
2703 WRITE32(access->ac_resp_access);
2709 static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
2714 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 8);
2715 WRITEMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
2717 /* Sorry, we do not yet support RDMA over 4.1: */
2725 nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
2728 nfsd4_encode_stateid(resp, &close->cl_stateid);
2735 nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
2740 RESERVE_SPACE(NFS4_VERIFIER_SIZE);
2741 WRITEMEM(commit->co_verf.data, NFS4_VERIFIER_SIZE);
2748 nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
2754 write_cinfo(&p, &create->cr_cinfo);
2756 WRITE32(create->cr_bmval[0]);
2757 WRITE32(create->cr_bmval[1]);
2764 nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
2766 struct svc_fh *fhp = getattr->ga_fhp;
2772 buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
2773 nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
2774 &resp->p, buflen, getattr->ga_bmval,
2780 nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
2782 struct svc_fh *fhp = *fhpp;
2787 len = fhp->fh_handle.fh_size;
2788 RESERVE_SPACE(len + 4);
2790 WRITEMEM(&fhp->fh_handle.fh_base, len);
2797 * Including all fields other than the name, a LOCK4denied structure requires
2798 * 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
2801 nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
2803 struct xdr_netobj *conf = &ld->ld_owner;
2806 RESERVE_SPACE(32 + XDR_LEN(conf->len));
2807 WRITE64(ld->ld_start);
2808 WRITE64(ld->ld_length);
2809 WRITE32(ld->ld_type);
2811 WRITEMEM(&ld->ld_clientid, 8);
2813 WRITEMEM(conf->data, conf->len);
2815 } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
2816 WRITE64((u64)0); /* clientid */
2817 WRITE32(0); /* length of owner name */
2823 nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
2826 nfsd4_encode_stateid(resp, &lock->lk_resp_stateid);
2827 else if (nfserr == nfserr_denied)
2828 nfsd4_encode_lock_denied(resp, &lock->lk_denied);
2834 nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
2836 if (nfserr == nfserr_denied)
2837 nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
2842 nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
2845 nfsd4_encode_stateid(resp, &locku->lu_stateid);
2852 nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
2858 write_cinfo(&p, &link->li_cinfo);
2866 nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
2873 nfsd4_encode_stateid(resp, &open->op_stateid);
2875 write_cinfo(&p, &open->op_cinfo);
2876 WRITE32(open->op_rflags);
2878 WRITE32(open->op_bmval[0]);
2879 WRITE32(open->op_bmval[1]);
2880 WRITE32(open->op_delegate_type);
2883 switch (open->op_delegate_type) {
2884 case NFS4_OPEN_DELEGATE_NONE:
2886 case NFS4_OPEN_DELEGATE_READ:
2887 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2889 WRITE32(open->op_recall);
2892 * TODO: ACE's in delegations
2894 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2897 WRITE32(0); /* XXX: is NULL principal ok? */
2900 case NFS4_OPEN_DELEGATE_WRITE:
2901 nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
2906 * TODO: space_limit's in delegations
2908 WRITE32(NFS4_LIMIT_SIZE);
2913 * TODO: ACE's in delegations
2915 WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
2918 WRITE32(0); /* XXX: is NULL principal ok? */
2921 case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
2922 switch (open->op_why_no_deleg) {
2923 case WND4_CONTENTION:
2926 WRITE32(open->op_why_no_deleg);
2927 WRITE32(0); /* deleg signaling not supported yet */
2931 WRITE32(open->op_why_no_deleg);
2938 /* XXX save filehandle here */
2944 nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
2947 nfsd4_encode_stateid(resp, &oc->oc_resp_stateid);
2953 nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
2956 nfsd4_encode_stateid(resp, &od->od_stateid);
2962 nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
2963 struct nfsd4_read *read)
2968 unsigned long maxcount;
2974 if (resp->xbuf->page_len)
2975 return nfserr_resource;
2977 RESERVE_SPACE(8); /* eof flag and byte count */
2979 maxcount = svc_max_payload(resp->rqstp);
2980 if (maxcount > read->rd_length)
2981 maxcount = read->rd_length;
2986 page = *(resp->rqstp->rq_next_page);
2987 if (!page) { /* ran out of pages */
2991 resp->rqstp->rq_vec[v].iov_base = page_address(page);
2992 resp->rqstp->rq_vec[v].iov_len =
2993 len < PAGE_SIZE ? len : PAGE_SIZE;
2994 resp->rqstp->rq_next_page++;
3000 nfserr = nfsd_read_file(read->rd_rqstp, read->rd_fhp, read->rd_filp,
3001 read->rd_offset, resp->rqstp->rq_vec, read->rd_vlen,
3006 eof = (read->rd_offset + maxcount >=
3007 read->rd_fhp->fh_dentry->d_inode->i_size);
3012 resp->xbuf->head[0].iov_len = (char*)p
3013 - (char*)resp->xbuf->head[0].iov_base;
3014 resp->xbuf->page_len = maxcount;
3016 /* Use rest of head for padding and remaining ops: */
3017 resp->xbuf->tail[0].iov_base = p;
3018 resp->xbuf->tail[0].iov_len = 0;
3022 resp->xbuf->tail[0].iov_base += maxcount&3;
3023 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
3030 nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
3038 if (resp->xbuf->page_len)
3039 return nfserr_resource;
3040 if (!*resp->rqstp->rq_next_page)
3041 return nfserr_resource;
3043 page = page_address(*(resp->rqstp->rq_next_page++));
3045 maxcount = PAGE_SIZE;
3049 * XXX: By default, the ->readlink() VFS op will truncate symlinks
3050 * if they would overflow the buffer. Is this kosher in NFSv4? If
3051 * not, one easy fix is: if ->readlink() precisely fills the buffer,
3052 * assume that truncation occurred, and return NFS4ERR_RESOURCE.
3054 nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
3055 if (nfserr == nfserr_isdir)
3056 return nfserr_inval;
3062 resp->xbuf->head[0].iov_len = (char*)p
3063 - (char*)resp->xbuf->head[0].iov_base;
3064 resp->xbuf->page_len = maxcount;
3066 /* Use rest of head for padding and remaining ops: */
3067 resp->xbuf->tail[0].iov_base = p;
3068 resp->xbuf->tail[0].iov_len = 0;
3072 resp->xbuf->tail[0].iov_base += maxcount&3;
3073 resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
3080 nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
3084 __be32 *page, *savep, *tailbase;
3089 if (resp->xbuf->page_len)
3090 return nfserr_resource;
3091 if (!*resp->rqstp->rq_next_page)
3092 return nfserr_resource;
3094 RESERVE_SPACE(NFS4_VERIFIER_SIZE);
3097 /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
3101 resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
3104 maxcount = PAGE_SIZE;
3105 if (maxcount > readdir->rd_maxcount)
3106 maxcount = readdir->rd_maxcount;
3109 * Convert from bytes to words, account for the two words already
3110 * written, make sure to leave two words at the end for the next
3111 * pointer and eof field.
3113 maxcount = (maxcount >> 2) - 4;
3115 nfserr = nfserr_toosmall;
3119 page = page_address(*(resp->rqstp->rq_next_page++));
3120 readdir->common.err = 0;
3121 readdir->buflen = maxcount;
3122 readdir->buffer = page;
3123 readdir->offset = NULL;
3125 offset = readdir->rd_cookie;
3126 nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
3128 &readdir->common, nfsd4_encode_dirent);
3129 if (nfserr == nfs_ok &&
3130 readdir->common.err == nfserr_toosmall &&
3131 readdir->buffer == page)
3132 nfserr = nfserr_toosmall;
3136 if (readdir->offset)
3137 xdr_encode_hyper(readdir->offset, offset);
3139 p = readdir->buffer;
3140 *p++ = 0; /* no more entries */
3141 *p++ = htonl(readdir->common.err == nfserr_eof);
3142 resp->xbuf->page_len = ((char*)p) -
3143 (char*)page_address(*(resp->rqstp->rq_next_page-1));
3145 /* Use rest of head for padding and remaining ops: */
3146 resp->xbuf->tail[0].iov_base = tailbase;
3147 resp->xbuf->tail[0].iov_len = 0;
3148 resp->p = resp->xbuf->tail[0].iov_base;
3149 resp->end = resp->p + (PAGE_SIZE - resp->xbuf->head[0].iov_len)/4;
3159 nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
3165 write_cinfo(&p, &remove->rm_cinfo);
3172 nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
3178 write_cinfo(&p, &rename->rn_sinfo);
3179 write_cinfo(&p, &rename->rn_tinfo);
3186 nfsd4_do_encode_secinfo(struct nfsd4_compoundres *resp,
3187 __be32 nfserr, struct svc_export *exp)
3189 u32 i, nflavs, supported;
3190 struct exp_flavor_info *flavs;
3191 struct exp_flavor_info def_flavs[2];
3192 __be32 *p, *flavorsp;
3193 static bool report = true;
3197 if (exp->ex_nflavors) {
3198 flavs = exp->ex_flavors;
3199 nflavs = exp->ex_nflavors;
3200 } else { /* Handling of some defaults in absence of real secinfo: */
3202 if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
3204 flavs[0].pseudoflavor = RPC_AUTH_UNIX;
3205 flavs[1].pseudoflavor = RPC_AUTH_NULL;
3206 } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
3208 flavs[0].pseudoflavor
3209 = svcauth_gss_flavor(exp->ex_client);
3212 flavs[0].pseudoflavor
3213 = exp->ex_client->flavour->flavour;
3219 flavorsp = p++; /* to be backfilled later */
3222 for (i = 0; i < nflavs; i++) {
3223 rpc_authflavor_t pf = flavs[i].pseudoflavor;
3224 struct rpcsec_gss_info info;
3226 if (rpcauth_get_gssinfo(pf, &info) == 0) {
3228 RESERVE_SPACE(4 + 4 + info.oid.len + 4 + 4);
3229 WRITE32(RPC_AUTH_GSS);
3230 WRITE32(info.oid.len);
3231 WRITEMEM(info.oid.data, info.oid.len);
3233 WRITE32(info.service);
3235 } else if (pf < RPC_AUTH_MAXFLAVOR) {
3242 pr_warn("NFS: SECINFO: security flavor %u "
3243 "is not supported\n", pf);
3247 if (nflavs != supported)
3249 *flavorsp = htonl(supported);
3258 nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
3259 struct nfsd4_secinfo *secinfo)
3261 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->si_exp);
3265 nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
3266 struct nfsd4_secinfo_no_name *secinfo)
3268 return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->sin_exp);
3272 * The SETATTR encode routine is special -- it always encodes a bitmap,
3273 * regardless of the error status.
3276 nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
3289 WRITE32(setattr->sa_bmval[0]);
3290 WRITE32(setattr->sa_bmval[1]);
3291 WRITE32(setattr->sa_bmval[2]);
3298 nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
3303 RESERVE_SPACE(8 + NFS4_VERIFIER_SIZE);
3304 WRITEMEM(&scd->se_clientid, 8);
3305 WRITEMEM(&scd->se_confirm, NFS4_VERIFIER_SIZE);
3308 else if (nfserr == nfserr_clid_inuse) {
3318 nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
3324 WRITE32(write->wr_bytes_written);
3325 WRITE32(write->wr_how_written);
3326 WRITEMEM(write->wr_verifier.data, NFS4_VERIFIER_SIZE);
3332 static const u32 nfs4_minimal_spo_must_enforce[2] = {
3333 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3334 1 << (OP_EXCHANGE_ID - 32) |
3335 1 << (OP_CREATE_SESSION - 32) |
3336 1 << (OP_DESTROY_SESSION - 32) |
3337 1 << (OP_DESTROY_CLIENTID - 32)
3341 nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
3342 struct nfsd4_exchange_id *exid)
3348 int server_scope_sz;
3349 uint64_t minor_id = 0;
3354 major_id = utsname()->nodename;
3355 major_id_sz = strlen(major_id);
3356 server_scope = utsname()->nodename;
3357 server_scope_sz = strlen(server_scope);
3360 8 /* eir_clientid */ +
3361 4 /* eir_sequenceid */ +
3364 8 /* spo_must_enforce, spo_must_allow */ +
3365 8 /* so_minor_id */ +
3366 4 /* so_major_id.len */ +
3367 (XDR_QUADLEN(major_id_sz) * 4) +
3368 4 /* eir_server_scope.len */ +
3369 (XDR_QUADLEN(server_scope_sz) * 4) +
3370 4 /* eir_server_impl_id.count (0) */);
3372 WRITEMEM(&exid->clientid, 8);
3373 WRITE32(exid->seqid);
3374 WRITE32(exid->flags);
3376 WRITE32(exid->spa_how);
3377 switch (exid->spa_how) {
3381 /* spo_must_enforce bitmap: */
3383 WRITE32(nfs4_minimal_spo_must_enforce[0]);
3384 WRITE32(nfs4_minimal_spo_must_enforce[1]);
3385 /* empty spo_must_allow bitmap: */
3392 /* The server_owner struct */
3393 WRITE64(minor_id); /* Minor id */
3395 WRITE32(major_id_sz);
3396 WRITEMEM(major_id, major_id_sz);
3399 WRITE32(server_scope_sz);
3400 WRITEMEM(server_scope, server_scope_sz);
3402 /* Implementation id */
3403 WRITE32(0); /* zero length nfs_impl_id4 array */
3409 nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
3410 struct nfsd4_create_session *sess)
3418 WRITEMEM(sess->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3419 WRITE32(sess->seqid);
3420 WRITE32(sess->flags);
3424 WRITE32(0); /* headerpadsz */
3425 WRITE32(sess->fore_channel.maxreq_sz);
3426 WRITE32(sess->fore_channel.maxresp_sz);
3427 WRITE32(sess->fore_channel.maxresp_cached);
3428 WRITE32(sess->fore_channel.maxops);
3429 WRITE32(sess->fore_channel.maxreqs);
3430 WRITE32(sess->fore_channel.nr_rdma_attrs);
3433 if (sess->fore_channel.nr_rdma_attrs) {
3435 WRITE32(sess->fore_channel.rdma_attrs);
3440 WRITE32(0); /* headerpadsz */
3441 WRITE32(sess->back_channel.maxreq_sz);
3442 WRITE32(sess->back_channel.maxresp_sz);
3443 WRITE32(sess->back_channel.maxresp_cached);
3444 WRITE32(sess->back_channel.maxops);
3445 WRITE32(sess->back_channel.maxreqs);
3446 WRITE32(sess->back_channel.nr_rdma_attrs);
3449 if (sess->back_channel.nr_rdma_attrs) {
3451 WRITE32(sess->back_channel.rdma_attrs);
3458 nfsd4_encode_destroy_session(struct nfsd4_compoundres *resp, __be32 nfserr,
3459 struct nfsd4_destroy_session *destroy_session)
3465 nfsd4_encode_free_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
3466 struct nfsd4_free_stateid *free_stateid)
3480 nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
3481 struct nfsd4_sequence *seq)
3488 RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 20);
3489 WRITEMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
3490 WRITE32(seq->seqid);
3491 WRITE32(seq->slotid);
3492 /* Note slotid's are numbered from zero: */
3493 WRITE32(seq->maxslots - 1); /* sr_highest_slotid */
3494 WRITE32(seq->maxslots - 1); /* sr_target_highest_slotid */
3495 WRITE32(seq->status_flags);
3498 resp->cstate.datap = p; /* DRC cache data pointer */
3503 nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
3504 struct nfsd4_test_stateid *test_stateid)
3506 struct nfsd4_test_stateid_id *stateid, *next;
3509 RESERVE_SPACE(4 + (4 * test_stateid->ts_num_ids));
3510 *p++ = htonl(test_stateid->ts_num_ids);
3512 list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
3513 *p++ = stateid->ts_id_status;
3521 nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
3526 typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
3529 * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
3530 * since we don't need to filter out obsolete ops as this is
3531 * done in the decoding phase.
3533 static nfsd4_enc nfsd4_enc_ops[] = {
3534 [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
3535 [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
3536 [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
3537 [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
3538 [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
3539 [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
3540 [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
3541 [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
3542 [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
3543 [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
3544 [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
3545 [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
3546 [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
3547 [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
3548 [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3549 [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
3550 [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
3551 [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
3552 [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
3553 [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
3554 [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
3555 [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
3556 [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
3557 [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
3558 [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
3559 [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
3560 [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
3561 [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
3562 [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
3563 [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
3564 [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
3565 [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
3566 [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
3567 [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
3568 [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
3569 [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
3570 [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
3572 /* NFSv4.1 operations */
3573 [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
3574 [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
3575 [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
3576 [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
3577 [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_destroy_session,
3578 [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_free_stateid,
3579 [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3580 [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
3581 [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
3582 [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
3583 [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
3584 [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
3585 [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
3586 [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
3587 [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
3588 [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
3589 [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
3590 [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
3591 [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
3595 * Calculate the total amount of memory that the compound response has taken
3596 * after encoding the current operation with pad.
3598 * pad: if operation is non-idempotent, pad was calculate by op_rsize_bop()
3599 * which was specified at nfsd4_operation, else pad is zero.
3601 * Compare this length to the session se_fmaxresp_sz and se_fmaxresp_cached.
3603 * Our se_fmaxresp_cached will always be a multiple of PAGE_SIZE, and so
3604 * will be at least a page and will therefore hold the xdr_buf head.
3606 __be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 pad)
3608 struct xdr_buf *xb = &resp->rqstp->rq_res;
3609 struct nfsd4_session *session = NULL;
3610 struct nfsd4_slot *slot = resp->cstate.slot;
3611 u32 length, tlen = 0;
3613 if (!nfsd4_has_session(&resp->cstate))
3616 session = resp->cstate.session;
3617 if (session == NULL)
3620 if (xb->page_len == 0) {
3621 length = (char *)resp->p - (char *)xb->head[0].iov_base + pad;
3623 if (xb->tail[0].iov_base && xb->tail[0].iov_len > 0)
3624 tlen = (char *)resp->p - (char *)xb->tail[0].iov_base;
3626 length = xb->head[0].iov_len + xb->page_len + tlen + pad;
3628 dprintk("%s length %u, xb->page_len %u tlen %u pad %u\n", __func__,
3629 length, xb->page_len, tlen, pad);
3631 if (length > session->se_fchannel.maxresp_sz)
3632 return nfserr_rep_too_big;
3634 if ((slot->sl_flags & NFSD4_SLOT_CACHETHIS) &&
3635 length > session->se_fchannel.maxresp_cached)
3636 return nfserr_rep_too_big_to_cache;
3642 nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3644 struct nfs4_stateowner *so = resp->cstate.replay_owner;
3650 statp = p++; /* to be backfilled at the end */
3653 if (op->opnum == OP_ILLEGAL)
3655 BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
3656 !nfsd4_enc_ops[op->opnum]);
3657 op->status = nfsd4_enc_ops[op->opnum](resp, op->status, &op->u);
3658 /* nfsd4_check_drc_limit guarantees enough room for error status */
3660 op->status = nfsd4_check_resp_size(resp, 0);
3662 so->so_replay.rp_status = op->status;
3663 so->so_replay.rp_buflen = (char *)resp->p - (char *)(statp+1);
3664 memcpy(so->so_replay.rp_buf, statp+1, so->so_replay.rp_buflen);
3668 * Note: We write the status directly, instead of using WRITE32(),
3669 * since it is already in network byte order.
3671 *statp = op->status;
3675 * Encode the reply stored in the stateowner reply cache
3677 * XDR note: do not encode rp->rp_buflen: the buffer contains the
3678 * previously sent already encoded operation.
3680 * called with nfs4_lock_state() held
3683 nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
3686 struct nfs4_replay *rp = op->replay;
3692 *p++ = rp->rp_status; /* already xdr'ed */
3695 RESERVE_SPACE(rp->rp_buflen);
3696 WRITEMEM(rp->rp_buf, rp->rp_buflen);
3701 nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
3703 return xdr_ressize_check(rqstp, p);
3706 int nfsd4_release_compoundargs(void *rq, __be32 *p, void *resp)
3708 struct svc_rqst *rqstp = rq;
3709 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3711 if (args->ops != args->iops) {
3713 args->ops = args->iops;
3717 while (args->to_free) {
3718 struct tmpbuf *tb = args->to_free;
3719 args->to_free = tb->next;
3720 tb->release(tb->buf);
3727 nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
3730 args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
3731 args->pagelist = rqstp->rq_arg.pages;
3732 args->pagelen = rqstp->rq_arg.page_len;
3734 args->to_free = NULL;
3735 args->ops = args->iops;
3736 args->rqstp = rqstp;
3738 return !nfsd4_decode_compound(args);
3742 nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
3745 * All that remains is to write the tag and operation count...
3747 struct nfsd4_compound_state *cs = &resp->cstate;
3750 *p++ = htonl(resp->taglen);
3751 memcpy(p, resp->tag, resp->taglen);
3752 p += XDR_QUADLEN(resp->taglen);
3753 *p++ = htonl(resp->opcnt);
3755 if (rqstp->rq_res.page_len)
3756 iov = &rqstp->rq_res.tail[0];
3758 iov = &rqstp->rq_res.head[0];
3759 iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
3760 BUG_ON(iov->iov_len > PAGE_SIZE);
3761 if (nfsd4_has_session(cs)) {
3762 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3763 struct nfs4_client *clp = cs->session->se_client;
3764 if (cs->status != nfserr_replay_cache) {
3765 nfsd4_store_cache_entry(resp);
3766 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3768 /* Renew the clientid on success and on replay */
3769 spin_lock(&nn->client_lock);
3770 nfsd4_put_session(cs->session);
3771 spin_unlock(&nn->client_lock);
3772 put_client_renew(clp);